[llvm] r310859 - [ValueTracking] Don't delete assumes of side-effectful instructions

Hal Finkel via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 14 10:11:43 PDT 2017


Author: hfinkel
Date: Mon Aug 14 10:11:43 2017
New Revision: 310859

URL: http://llvm.org/viewvc/llvm-project?rev=310859&view=rev
Log:
[ValueTracking] Don't delete assumes of side-effectful instructions

ValueTracking has to strike a balance when attempting to propagate information
backwards from assumes, because if the information is trivially propagated
backwards, it can appear to LLVM that the assumption is known to be true, and
therefore can be removed.

This is sound (because an assumption has no semantic effect except for causing
UB), but prevents the assume from allowing further optimizations.

The isEphemeralValueOf check exists to try and prevent this issue by not
removing the source of an assumption. This tries to make it a little bit more
general to handle the case of side-effectful instructions, such as in

  %0 = call i1 @get_val()
  %1 = xor i1 %0, true
  call void @llvm.assume(i1 %1)

Patch by Ariel Ben-Yehuda, thanks!

Differential Revision: https://reviews.llvm.org/D36590

Modified:
    llvm/trunk/lib/Analysis/ValueTracking.cpp
    llvm/trunk/test/Analysis/ValueTracking/assume.ll

Modified: llvm/trunk/lib/Analysis/ValueTracking.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ValueTracking.cpp?rev=310859&r1=310858&r2=310859&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ValueTracking.cpp (original)
+++ llvm/trunk/lib/Analysis/ValueTracking.cpp Mon Aug 14 10:11:43 2017
@@ -384,13 +384,13 @@ static bool isEphemeralValueOf(const Ins
       if (V == E)
         return true;
 
-      EphValues.insert(V);
-      if (const User *U = dyn_cast<User>(V))
-        for (User::const_op_iterator J = U->op_begin(), JE = U->op_end();
-             J != JE; ++J) {
-          if (isSafeToSpeculativelyExecute(*J))
-            WorkSet.push_back(*J);
-        }
+      if (V == I || isSafeToSpeculativelyExecute(V)) {
+       EphValues.insert(V);
+       if (const User *U = dyn_cast<User>(V))
+         for (User::const_op_iterator J = U->op_begin(), JE = U->op_end();
+              J != JE; ++J)
+           WorkSet.push_back(*J);
+      }
     }
   }
 

Modified: llvm/trunk/test/Analysis/ValueTracking/assume.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ValueTracking/assume.ll?rev=310859&r1=310858&r2=310859&view=diff
==============================================================================
--- llvm/trunk/test/Analysis/ValueTracking/assume.ll (original)
+++ llvm/trunk/test/Analysis/ValueTracking/assume.ll Mon Aug 14 10:11:43 2017
@@ -18,5 +18,16 @@ define i32 @assume_add(i32 %a, i32 %b) {
   ret i32 %t3
 }
 
-declare void @llvm.assume(i1)
 
+define void @assume_not() {
+; CHECK-LABEL: @assume_not(
+entry-block:
+  %0 = call i1 @get_val()
+; CHECK: call void @llvm.assume
+  %1 = xor i1 %0, true
+  call void @llvm.assume(i1 %1)
+  ret void
+}
+
+declare i1 @get_val()
+declare void @llvm.assume(i1)




More information about the llvm-commits mailing list