[llvm-commits] [llvm] r159668 - in /llvm/trunk: lib/Transforms/Scalar/BoundsChecking.cpp test/Transforms/BoundsChecking/phi.ll
Nuno Lopes
nunoplopes at sapo.pt
Tue Jul 3 10:30:18 PDT 2012
Author: nlopes
Date: Tue Jul 3 12:30:18 2012
New Revision: 159668
URL: http://llvm.org/viewvc/llvm-project?rev=159668&view=rev
Log:
BoundsChecking: optimize out the check for offset < 0 if size is known to be >= 0 (signed).
(LLVM optimizers cannot do this optimization by themselves)
Modified:
llvm/trunk/lib/Transforms/Scalar/BoundsChecking.cpp
llvm/trunk/test/Transforms/BoundsChecking/phi.ll
Modified: llvm/trunk/lib/Transforms/Scalar/BoundsChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/BoundsChecking.cpp?rev=159668&r1=159667&r2=159668&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/BoundsChecking.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/BoundsChecking.cpp Tue Jul 3 12:30:18 2012
@@ -67,11 +67,8 @@
}
char BoundsChecking::ID = 0;
-INITIALIZE_PASS_BEGIN(BoundsChecking, "bounds-checking",
- "Run-time bounds checking", false, false)
-INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
-INITIALIZE_PASS_END(BoundsChecking, "bounds-checking",
- "Run-time bounds checking", false, false)
+INITIALIZE_PASS(BoundsChecking, "bounds-checking", "Run-time bounds checking",
+ false, false)
/// getTrapBB - create a basic block that traps. All overflowing conditions
@@ -141,6 +138,7 @@
Value *Size = SizeOffset.first;
Value *Offset = SizeOffset.second;
+ ConstantInt *SizeCI = dyn_cast<ConstantInt>(Size);
IntegerType *IntTy = TD->getIntPtrType(Inst->getContext());
Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize);
@@ -149,12 +147,17 @@
// . Offset >= 0 (since the offset is given from the base ptr)
// . Size >= Offset (unsigned)
// . Size - Offset >= NeededSize (unsigned)
+ //
+ // optimization: if Size >= 0 (signed), skip 1st check
// FIXME: add NSW/NUW here? -- we dont care if the subtraction overflows
Value *ObjSize = Builder->CreateSub(Size, Offset);
- Value *Cmp1 = Builder->CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0));
Value *Cmp2 = Builder->CreateICmpULT(Size, Offset);
Value *Cmp3 = Builder->CreateICmpULT(ObjSize, NeededSizeVal);
- Value *Or = Builder->CreateOr(Cmp1, Builder->CreateOr(Cmp2, Cmp3));
+ Value *Or = Builder->CreateOr(Cmp2, Cmp3);
+ if (!SizeCI || SizeCI->getValue().slt(0)) {
+ Value *Cmp1 = Builder->CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0));
+ Or = Builder->CreateOr(Cmp1, Or);
+ }
emitBranchToTrap(Or);
++ChecksAdded;
Modified: llvm/trunk/test/Transforms/BoundsChecking/phi.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/BoundsChecking/phi.ll?rev=159668&r1=159667&r2=159668&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/BoundsChecking/phi.ll (original)
+++ llvm/trunk/test/Transforms/BoundsChecking/phi.ll Tue Jul 3 12:30:18 2012
@@ -34,10 +34,14 @@
while.body.i:
; CHECK: phi
; CHECK-NEXT: phi
-; CHECK-NEXT: phi
-; CHECK: trap
+; CHECK-NOT: phi
%c.addr.02.i = phi i8* [ %incdec.ptr.i, %while.body.i ], [ %addr, %while.body.i.preheader ]
%incdec.ptr.i = getelementptr inbounds i8* %c.addr.02.i, i64 -1
+; CHECK: sub i64 10, %0
+; CHECK-NEXT: icmp ult i64 10, %0
+; CHECK-NEXT: icmp ult i64 {{.*}}, 1
+; CHECK-NEXT: or i1
+; CHECK-NEXT: br {{.*}}, label %trap
store i8 100, i8* %c.addr.02.i, align 1
%0 = load i8* %incdec.ptr.i, align 1
%tobool.i = icmp eq i8 %0, 0
More information about the llvm-commits
mailing list