[PATCH] D48230: [PredicateInfo] Order instructions in different BBs by DFSNumIn.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 15 12:17:44 PDT 2018


fhahn created this revision.
fhahn added reviewers: dberlin, efriedma, davide.

Using OrderedInstructions::dominates as comparator for instructions in
BBs without dominance relation can cause a non-deterministic order
between such instructions. That in turn can cause us to materialize
copies in a non-deterministic order. While this does not effect
correctness, it causes some minor non-determinism in the final generated
code, because values have slightly different labels.

Without this patch, running -print-predicateinfo on a reasonably large
module produces slightly different output on each run.

This patch uses the dominator trees DFSInNum to order instruction from
different BBs, which should enforce a deterministic ordering and
guarantee that dominated instructions come after the instructions that
dominate them.


https://reviews.llvm.org/D48230

Files:
  lib/Transforms/Utils/PredicateInfo.cpp


Index: lib/Transforms/Utils/PredicateInfo.cpp
===================================================================
--- lib/Transforms/Utils/PredicateInfo.cpp
+++ lib/Transforms/Utils/PredicateInfo.cpp
@@ -551,6 +551,16 @@
   // Sort OpsToRename since we are going to iterate it.
   SmallVector<Value *, 8> OpsToRename(OpSet.begin(), OpSet.end());
   auto Comparator = [&](const Value *A, const Value *B) {
+    // Order instructions in different basic blocks by their dominator tree
+    // DFSIn number, which guarantees a deterministic ordering and that
+    // dominated instructions come after the instructions that dominate them.
+    const Instruction *InstA = dyn_cast_or_null<Instruction>(A);
+    const Instruction *InstB = dyn_cast_or_null<Instruction>(B);
+    if (InstA && InstB && InstA->getParent() != InstB->getParent()) {
+      DomTreeNode *DA = DT.getNode(InstA->getParent());
+      DomTreeNode *DB = DT.getNode(InstB->getParent());
+      return DA->getDFSNumIn() < DB->getDFSNumIn();
+    }
     return valueComesBefore(OI, A, B);
   };
   llvm::sort(OpsToRename.begin(), OpsToRename.end(), Comparator);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48230.151539.patch
Type: text/x-patch
Size: 1124 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180615/5aa963f3/attachment.bin>


More information about the llvm-commits mailing list