[llvm] r235544 - [InstCombine] Clear out nsw/nuw if we modify computation in the chain

David Majnemer david.majnemer at gmail.com
Wed Apr 22 13:59:28 PDT 2015


Author: majnemer
Date: Wed Apr 22 15:59:28 2015
New Revision: 235544

URL: http://llvm.org/viewvc/llvm-project?rev=235544&view=rev
Log:
[InstCombine] Clear out nsw/nuw if we modify computation in the chain

An nsw/nuw operation relies on the values feeding into it to not
overflow if 'poison' is not to be produced.  This means that
optimizations which make modifications to the bottom of a chain (like
SimplifyDemandedBits) must strip out nsw/nuw if they cannot ensure that
they will be preserved.

This fixes PR23309.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
    llvm/trunk/test/Transforms/InstCombine/cast.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp?rev=235544&r1=235543&r2=235544&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp Wed Apr 22 15:59:28 2015
@@ -83,11 +83,18 @@ bool InstCombiner::SimplifyDemandedInstr
 bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask,
                                         APInt &KnownZero, APInt &KnownOne,
                                         unsigned Depth) {
-  Value *NewVal =
-      SimplifyDemandedUseBits(U.get(), DemandedMask, KnownZero, KnownOne, Depth,
-                              dyn_cast<Instruction>(U.getUser()));
+  auto *UserI = dyn_cast<Instruction>(U.getUser());
+  Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, KnownZero,
+                                          KnownOne, Depth, UserI);
   if (!NewVal) return false;
   U = NewVal;
+
+  // Shrinking a constant might cause a nsw/nuw violation to occur in
+  // instructions which are themselves demanded.
+  if (auto *UserOBO = dyn_cast<OverflowingBinaryOperator>(UserI)) {
+    cast<BinaryOperator>(UserOBO)->setHasNoSignedWrap(false);
+    cast<BinaryOperator>(UserOBO)->setHasNoUnsignedWrap(false);
+  }
   return true;
 }
 

Modified: llvm/trunk/test/Transforms/InstCombine/cast.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/cast.ll?rev=235544&r1=235543&r2=235544&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/cast.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/cast.ll Wed Apr 22 15:59:28 2015
@@ -1113,3 +1113,15 @@ define float @sitofp_zext(i16 %a) {
   %sitofp = sitofp i32 %zext to float
   ret float %sitofp
 }
+
+define i1 @PR23309(i32 %A, i32 %B) {
+; CHECK-LABEL: @PR23309(
+; CHECK-NEXT: %[[sub:.*]] = sub i32 %A, %B
+; CHECK-NEXT: %[[and:.*]] = and i32 %[[sub]], 1
+; CHECK-NEXT: %[[cmp:.*]] = icmp ne i32 %[[and]], 0
+; CHECK-NEXT: ret i1 %[[cmp]]
+  %add = add i32 %A, -4
+  %sub = sub nsw i32 %add, %B
+  %trunc = trunc i32 %sub to i1
+  ret i1 %trunc
+}





More information about the llvm-commits mailing list