[PATCH] CVP: Improve handling of Selects used as incoming PHI values

Björn Steinbrink bsteinbr at gmail.com
Thu Apr 16 05:44:57 PDT 2015


If the branch that leads to the PHI node and the Select instruction
depend on correlated conditions, we might be able to directly use the
corresponding value from the Select instruction as the incoming value
for the PHI node, allowing later removal of the select instruction.

http://reviews.llvm.org/D9051

Files:
  lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
  test/Transforms/CorrelatedValuePropagation/select.ll

Index: lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
===================================================================
--- lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -103,24 +103,37 @@
 
     Value *V = LVI->getConstantOnEdge(Incoming, P->getIncomingBlock(i), BB, P);
 
-    // Look if the incoming value is a select with a constant but LVI tells us
+    // Look if the incoming value is a select for which LVI can tells us the
+    // condition value, or if its a select with a constant but LVI tells us
     // that the incoming value can never be that constant. In that case replace
-    // the incoming value with the other value of the select. This often allows
-    // us to remove the select later.
+    // the incoming value with the appropriate value of the select. This often
+    // allows us to remove the select later.
     if (!V) {
       SelectInst *SI = dyn_cast<SelectInst>(Incoming);
       if (!SI) continue;
 
-      Constant *C = dyn_cast<Constant>(SI->getFalseValue());
-      if (!C) continue;
-
-      if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C,
-                                  P->getIncomingBlock(i), BB, P) !=
-          LazyValueInfo::False)
-        continue;
+      Value *Condition = SI->getCondition();
+      switch (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, Condition,
+                                      ConstantInt::getTrue(Condition->getType()),
+                                      P->getIncomingBlock(i), BB, P)) {
+      case LazyValueInfo::False:
+        V = SI->getFalseValue();
+        break;
+      case LazyValueInfo::True:
+        V = SI->getTrueValue();
+        break;
+      case LazyValueInfo::Unknown:
+        Constant *C = dyn_cast<Constant>(SI->getFalseValue());
+        if (!C) continue;
+
+        if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C,
+              P->getIncomingBlock(i), BB, P) !=
+            LazyValueInfo::False)
+          continue;
+        V = SI->getTrueValue();
+      }
 
       DEBUG(dbgs() << "CVP: Threading PHI over " << *SI << '\n');
-      V = SI->getTrueValue();
     }
 
     P->setIncomingValue(i, V);
Index: test/Transforms/CorrelatedValuePropagation/select.ll
===================================================================
--- /dev/null
+++ test/Transforms/CorrelatedValuePropagation/select.ll
@@ -0,0 +1,18 @@
+; RUN: opt < %s -correlated-propagation -S | FileCheck %s
+
+; CHECK-LABEL: @test(
+define void @test(i32) {
+entry:
+  br label %loop
+
+loop:
+  %idx = phi i32 [ %0, %entry ], [ %sel, %loop ]
+; CHECK: %idx = phi i32 [ %0, %entry ], [ %2, %loop ]
+  %1 = icmp eq i32 %idx, 0
+  %2 = add i32 %idx, -1
+  %sel = select i1 %1, i32 0, i32 %2
+  br i1 %1, label %out, label %loop
+
+out:
+  ret void
+}

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9051.23850.patch
Type: text/x-patch
Size: 2815 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150416/cabeecac/attachment.bin>


More information about the llvm-commits mailing list