[llvm] r293498 - [InstCombine] fixed to propagate 'exact' on lshr

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 30 08:53:03 PST 2017


Author: spatel
Date: Mon Jan 30 10:53:03 2017
New Revision: 293498

URL: http://llvm.org/viewvc/llvm-project?rev=293498&view=rev
Log:
[InstCombine] fixed to propagate 'exact' on lshr

The original shift is bigger, so this may qualify as 'obvious', 
but here's an attempt at an Alive-based proof:

Name: exact
Pre: (C1 u< C2)
%a = shl i8 %x, C1
%b = lshr exact i8 %a, C2 
  =>
%c = lshr exact i8 %x, C2 - C1
%b = and i8 %c, ((1 << width(C1)) - 1) u>> C2

Optimization is correct!


Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp
    llvm/trunk/test/Transforms/InstCombine/shift.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp?rev=293498&r1=293497&r2=293498&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp Mon Jan 30 10:53:03 2017
@@ -773,7 +773,7 @@ Instruction *InstCombiner::visitLShr(Bin
           return NewLShr;
         }
         // (X << C1) >>u C2  --> (X >>u (C2 - C1)) & (-1 >> C2)
-        Value *NewLShr = Builder->CreateLShr(X, ShiftDiff);
+        Value *NewLShr = Builder->CreateLShr(X, ShiftDiff, "", I.isExact());
         APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
         return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask));
       }

Modified: llvm/trunk/test/Transforms/InstCombine/shift.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/shift.ll?rev=293498&r1=293497&r2=293498&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/shift.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/shift.ll Mon Jan 30 10:53:03 2017
@@ -937,7 +937,7 @@ define <2 x i32> @test51_splat_vec(<2 x
 
 define i32 @test51_no_nuw(i32 %x) {
 ; CHECK-LABEL: @test51_no_nuw(
-; CHECK-NEXT:    [[TMP1:%.*]] = lshr i32 %x, 2
+; CHECK-NEXT:    [[TMP1:%.*]] = lshr exact i32 %x, 2
 ; CHECK-NEXT:    [[B:%.*]] = and i32 [[TMP1]], 536870911
 ; CHECK-NEXT:    ret i32 [[B]]
 ;




More information about the llvm-commits mailing list