[coco] Assign InstrIndex for each Instr (#1673)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 28 Sep 2018 10:32:26 +0000 (19:32 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 28 Sep 2018 10:32:26 +0000 (19:32 +0900)
This commit introduces a field/method related with instruction index, and
revises the implementation of Instr to automatically update this index
per change.

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

index f0028aa..1218f3f 100644 (file)
@@ -20,6 +20,7 @@
 #include "coco/IR/Bag.h"
 #include "coco/IR/Block.forward.h"
 #include "coco/IR/Instr.forward.h"
+#include "coco/IR/InstrIndex.h"
 #include "coco/IR/Entity.h"
 
 #include "coco/ADT/DLinkedList.h"
@@ -46,6 +47,10 @@ using InstrList = coco::DLinkedList<Instr, Block>::Head;
 class Instr : public coco::DLinkedList<Instr, Block>::Node, public Entity
 {
 public:
+  friend void DLinkedList<Instr, Block>::joined(Block *, Instr *);
+  friend void DLinkedList<Instr, Block>::leaving(Block *, Instr *);
+
+public:
   Instr() = default;
 
 public:
@@ -166,6 +171,12 @@ public:
    * TODO Make this method as a pure virtual method
    */
   virtual void dispose(void) { return; }
+
+public:
+  const InstrIndex &index(void) const { return _index; }
+
+private:
+  InstrIndex _index;
 };
 
 } // namespace coco
index e80db5e..2e94c2a 100644 (file)
 namespace coco
 {
 
-template <> void DLinkedList<Instr, Block>::joined(Block *, Instr *)
+template <> void DLinkedList<Instr, Block>::joined(Block *, Instr *curr_ins)
 {
-  // TODO Implement this
+  assert(!curr_ins->index().valid());
+  uint32_t value = 0;
+
+  if (auto prev_ins = curr_ins->prev())
+  {
+    value = prev_ins->index().value() + 1;
+  }
+
+  for (auto ins = curr_ins; ins; ins = ins->next())
+  {
+    ins->_index.set(value++);
+  }
 }
 
-template <> void DLinkedList<Instr, Block>::leaving(Block *, Instr *)
+template <> void DLinkedList<Instr, Block>::leaving(Block *, Instr *curr_ins)
 {
-  // TODO Implement this
+  assert(curr_ins->index().valid());
+  uint32_t value = curr_ins->index().value();
+
+  for (auto ins = curr_ins->next(); ins; ins = ins->next())
+  {
+    ins->_index.set(value++);
+  }
+
+  curr_ins->_index.reset();
 }
 
 template <> InstrList *DLinkedList<Instr, Block>::head(Block *b) { return b->instr(); }
index c69e163..a891c8a 100644 (file)
@@ -105,6 +105,9 @@ TEST(IR_MODULE, append_two_instrs)
   ASSERT_EQ(ins_2->parent(), blk);
   ASSERT_EQ(ins_2->prev(), ins_1);
   ASSERT_EQ(ins_2->next(), nullptr);
+
+  ASSERT_EQ(ins_1->index().value(), 0);
+  ASSERT_EQ(ins_2->index().value(), 1);
 }
 
 TEST(IR_MODULE, iterate_constant_block)