[PATCH] D32407: [JumpThread] Do RAUW in case Cond folds to a constant in the CFG

Xin Tong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 23 14:45:01 PDT 2017


trentxintong updated this revision to Diff 96326.
trentxintong added a comment.

Accidentally removed a few lines.


https://reviews.llvm.org/D32407

Files:
  lib/Transforms/Scalar/JumpThreading.cpp
  test/Transforms/JumpThreading/fold-not-thread.ll


Index: test/Transforms/JumpThreading/fold-not-thread.ll
===================================================================
--- test/Transforms/JumpThreading/fold-not-thread.ll
+++ test/Transforms/JumpThreading/fold-not-thread.ll
@@ -133,3 +133,32 @@
   ret void
 }
 
+; Make sure we can do the RAUW for %add...
+;
+; CHECK-LABEL: @rauw_if_possible(
+; CHECK: call void @f4(i32 96) 
+define void @rauw_if_possible(i32 %value) nounwind {
+entry:
+  %cmp = icmp eq i32 %value, 32
+  br i1 %cmp, label %L0, label %L3 
+L0:
+  call i32 @f2()
+  call i32 @f2()
+  %add = add i32 %value, 64
+  switch i32 %add, label %L3 [
+    i32 32, label %L1
+    i32 96, label %L2
+    ]
+
+L1:
+  call void @f3()
+  ret void
+L2:
+  call void @f4(i32 %add)
+  ret void
+L3:
+  call void @f3()
+  ret void
+}
+
+
Index: lib/Transforms/Scalar/JumpThreading.cpp
===================================================================
--- lib/Transforms/Scalar/JumpThreading.cpp
+++ lib/Transforms/Scalar/JumpThreading.cpp
@@ -1250,6 +1250,8 @@
 
   BasicBlock *OnlyDest = nullptr;
   BasicBlock *MultipleDestSentinel = (BasicBlock*)(intptr_t)~0ULL;
+  Constant *OnlyVal = nullptr;
+  Constant *MultipleVal = (Constant *)(intptr_t)~0ULL;
 
   for (const auto &PredValue : PredValues) {
     BasicBlock *Pred = PredValue.second;
@@ -1277,10 +1279,17 @@
     }
 
     // If we have exactly one destination, remember it for efficiency below.
-    if (PredToDestList.empty())
+    if (PredToDestList.empty()) {
       OnlyDest = DestBB;
-    else if (OnlyDest != DestBB)
-      OnlyDest = MultipleDestSentinel;
+      OnlyVal = Val;
+    } else {
+      if (OnlyDest != DestBB)
+        OnlyDest = MultipleDestSentinel;
+      // It possible we have same destination, but different value, e.g. default
+      // case in switchinst.
+      if (Val != OnlyVal)
+        OnlyVal = MultipleVal;
+    }
 
     PredToDestList.push_back(std::make_pair(Pred, DestBB));
   }
@@ -1313,8 +1322,13 @@
       auto *CondInst = dyn_cast<Instruction>(Cond);
       if (CondInst && CondInst->use_empty())
         CondInst->eraseFromParent();
-      // FIXME: in case this instruction is defined in the current BB and it
-      // resolves to a single value from all predecessors, we can do RAUW.
+      else if (CondInst && OnlyVal && OnlyVal != MultipleVal &&
+               CondInst->getParent() == BB) {
+        // If we just learned Cond is the same value for all uses of the
+        // condition, replace it with a constant value
+        CondInst->replaceAllUsesWith(OnlyVal);
+        CondInst->eraseFromParent();
+      }
       return true;
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32407.96326.patch
Type: text/x-patch
Size: 2620 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170423/b00f23e6/attachment.bin>


More information about the llvm-commits mailing list