[enco] Introduce IndirectCopyElimination (#1692)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 1 Oct 2018 07:52:55 +0000 (16:52 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 1 Oct 2018 07:52:55 +0000 (16:52 +0900)
* [enco] Introduce IndirectCopyElimination

This commit introduces Indirect Copy Elimination pass in enco.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Add assert

contrib/enco/core/src/Backend.cpp
contrib/enco/core/src/Transforms/IndirectCopyElimination.cpp [new file with mode: 0644]
contrib/enco/core/src/Transforms/IndirectCopyElimination.h [new file with mode: 0644]

index 2dbb66f..617650e 100644 (file)
@@ -23,6 +23,7 @@
 #include "Transforms/FeatureUnification.h"
 #include "Transforms/Rewrite.h"
 #include "Transforms/Normalize.h"
+#include "Transforms/IndirectCopyElimination.h"
 #include "Transforms/DeadObjectElimination.h"
 #include "Transforms/Optimizations.h"
 #include "Transforms/Split.h"
@@ -101,6 +102,8 @@ void BackendImpl::compile(coco::Module *m, coco::Data *d)
   NormalizePass normalize;
   normalize.runOnCode(&code);
 
+  eliminate_indirect_copy(&code);
+
   // Eliminate dead object
   //
   // NOTE Dead Object Elimination (DOE) is performed before Copy lowering
diff --git a/contrib/enco/core/src/Transforms/IndirectCopyElimination.cpp b/contrib/enco/core/src/Transforms/IndirectCopyElimination.cpp
new file mode 100644 (file)
index 0000000..b36620f
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "IndirectCopyElimination.h"
+
+#include <cassert>
+
+namespace
+{
+
+coco::Copy *as_copy(coco::Instr *ins) { return ins ? ins->asCopy() : nullptr; }
+
+/**
+ * @brief Return a set of copy instructions that are accessible from top-level module
+ */
+std::set<coco::Copy *> linked_copy_instrs(coco::Module *m)
+{
+  std::set<coco::Copy *> res;
+
+  for (uint32_t n = 0; n < m->entity()->instr()->size(); ++n)
+  {
+    auto ins = m->entity()->instr()->at(n);
+    assert(ins != nullptr);
+
+    if (ins->parent() && ins->parent()->parent())
+    {
+      if (auto copy = ins->asCopy())
+      {
+        res.insert(copy);
+      }
+    }
+  }
+
+  return res;
+}
+
+} // namespace
+
+namespace enco
+{
+
+void eliminate_indirect_copy(enco::Code *code)
+{
+  auto m = code->module();
+
+  for (auto child : linked_copy_instrs(m))
+  {
+    auto from = child->from();
+    assert(from != nullptr);
+
+    // Find the irreducible origin
+    while (true)
+    {
+      if (auto producer = coco::producer(from))
+      {
+        if (auto parent = as_copy(producer->loc()))
+        {
+          assert(parent->from() != nullptr);
+          from = parent->from();
+          continue;
+        }
+      }
+
+      break;
+    }
+
+    child->from(from);
+  }
+}
+
+} // namespace enco
diff --git a/contrib/enco/core/src/Transforms/IndirectCopyElimination.h b/contrib/enco/core/src/Transforms/IndirectCopyElimination.h
new file mode 100644 (file)
index 0000000..4835086
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __ENCO_TRANSFORM_INDIRECT_COPY_ELIMINATION_H__
+#define __ENCO_TRANSFORM_INDIRECT_COPY_ELIMINATION_H__
+
+#include "Code.h"
+
+namespace enco
+{
+
+/**
+ * @breif Convert all the indirect copies as a direct copy
+ *
+ * >>> BEFORE <<<
+ * %obj_0 = ...
+ * %obj_1 = ...
+ * %obj_2 = ...
+ *
+ * copy(from: %obj_0, into: %obj_1)
+ * copy(from: %obj_1, into: %obj_2)
+ *
+ * >>> AFTER <<<
+ * %obj_0 = ...
+ * %obj_1 = ...
+ * %obj_2 = ...
+ *
+ * copy(from: %obj_0, into: %obj_1)
+ * copy(from: %obj_0, into: %obj_2)
+ *
+ */
+void eliminate_indirect_copy(enco::Code *code);
+
+} // namespace enco
+
+#endif // __ENCO_TRANSFORM_INDIRECT_COPY_ELIMINATION_H__