[coco] Automatic BlockIndex update (#1656)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 28 Sep 2018 01:21:29 +0000 (10:21 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 28 Sep 2018 01:21:29 +0000 (10:21 +0900)
With this commit, each Block includes BlockIndex which will be
automatically updated upon Block link update.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/IR/Block.h
contrib/coco/core/src/IR/Block.cpp
contrib/coco/core/src/IR/Module.test.cpp

index 203c01e..b7c5926 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "coco/IR/Module.forward.h"
 #include "coco/IR/Block.forward.h"
+#include "coco/IR/BlockIndex.h"
 #include "coco/IR/Instr.h"
 #include "coco/IR/Entity.h"
 
@@ -38,6 +39,10 @@ using BlockList = DLinkedList<Block, Module>::Head;
 class Block final : public DLinkedList<Block, Module>::Node, public Entity
 {
 public:
+  friend void DLinkedList<Block, Module>::joined(Module *, Block *);
+  friend void DLinkedList<Block, Module>::leaving(Module *, Block *);
+
+public:
   Block() : _instr{this}
   {
     // DO NOTHING
@@ -51,7 +56,11 @@ public:
   InstrList *instr(void) { return &_instr; }
   const InstrList *instr(void) const { return &_instr; }
 
+public:
+  const BlockIndex &index(void) const { return _index; }
+
 private:
+  BlockIndex _index;
   DLinkedList<Instr, Block>::Head _instr;
 };
 
index 2235007..14c0260 100644 (file)
 #include "coco/IR/Block.h"
 #include "coco/IR/Module.h"
 
+#include <cassert>
+
 namespace coco
 {
 
-template <> void DLinkedList<Block, Module>::joined(Module *, Block *)
+template <> void DLinkedList<Block, Module>::joined(Module *, Block *curr_blk)
 {
-  // TODO Implement this
+  assert(!curr_blk->index().valid());
+  uint32_t value = 0;
+
+  if (auto prev_blk = curr_blk->prev())
+  {
+    value = prev_blk->index().value() + 1;
+  }
+
+  for (auto blk = curr_blk; blk; blk = blk->next())
+  {
+    blk->_index.set(value++);
+  }
 }
 
-template <> void DLinkedList<Block, Module>::leaving(Module *, Block *)
+template <> void DLinkedList<Block, Module>::leaving(Module *, Block *curr_blk)
 {
-  // TODO Implement this
+  assert(curr_blk->index().valid());
+  uint32_t value = curr_blk->index().value();
+
+  for (auto blk = curr_blk->next(); blk; blk = blk->next())
+  {
+    blk->_index.set(value++);
+  }
+
+  curr_blk->_index.reset();
 }
 
 template <> BlockList *DLinkedList<Block, Module>::head(Module *m) { return m->block(); }
index 41cd383..c69e163 100644 (file)
@@ -79,6 +79,9 @@ TEST(IR_MODULE, append_two_blocks)
 
   ASSERT_EQ(blk_2->prev(), blk_1);
   ASSERT_EQ(blk_2->next(), nullptr);
+
+  ASSERT_EQ(blk_1->index().value(), 0);
+  ASSERT_EQ(blk_2->index().value(), 1);
 }
 
 TEST(IR_MODULE, append_two_instrs)