[llvm-branch-commits] [llvm-branch] r69845 - in /llvm/branches/Apple/Dib: lib/Transforms/Scalar/IndVarSimplify.cpp test/Transforms/IndVarSimplify/2009-04-22-IndvarCrash.ll

Bill Wendling isanbard at gmail.com
Wed Apr 22 16:55:16 PDT 2009


Author: void
Date: Wed Apr 22 18:55:16 2009
New Revision: 69845

URL: http://llvm.org/viewvc/llvm-project?rev=69845&view=rev
Log:
--- Merging (from foreign repository) r69836 into '.':
A    test/Transforms/IndVarSimplify/2009-04-22-IndvarCrash.ll
U    lib/Transforms/Scalar/IndVarSimplify.cpp

Avoid deferencing use_begin() if value does not have a use.

--- Merging (from foreign repository) r69842 into '.':
G    lib/Transforms/Scalar/IndVarSimplify.cpp

A few more places where the check of use_empty is needed.

--- Merging (from foreign repository) r69844 into '.':
U    test/Transforms/IndVarSimplify/2009-04-22-IndvarCrash.ll
G    lib/Transforms/Scalar/IndVarSimplify.cpp

Make sure both operands have binary instructions have the same type.

Added:
    llvm/branches/Apple/Dib/test/Transforms/IndVarSimplify/2009-04-22-IndvarCrash.ll
Modified:
    llvm/branches/Apple/Dib/lib/Transforms/Scalar/IndVarSimplify.cpp

Modified: llvm/branches/Apple/Dib/lib/Transforms/Scalar/IndVarSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/Transforms/Scalar/IndVarSimplify.cpp?rev=69845&r1=69844&r2=69845&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/Transforms/Scalar/IndVarSimplify.cpp (original)
+++ llvm/branches/Apple/Dib/lib/Transforms/Scalar/IndVarSimplify.cpp Wed Apr 22 18:55:16 2009
@@ -738,6 +738,7 @@
         // See if we can figure out sext(i+constant) doesn't wrap, so we can
         // use a larger add.  This is common in subscripting.
         if (UInst && UInst->getOpcode()==Instruction::Add &&
+            !UInst->use_empty() &&
             allUsesAreSameTyped(Instruction::SExt, UInst) &&
             isa<ConstantInt>(UInst->getOperand(1)) &&
             NoSignedWrap && LimitVal) {
@@ -749,11 +750,14 @@
             // We've determined this is (i+constant) and it won't overflow.
             if (isa<SExtInst>(UInst->use_begin())) {
               SExtInst* oldSext = dyn_cast<SExtInst>(UInst->use_begin());
+              uint64_t truncSize = oldSext->getType()->getPrimitiveSizeInBits();
               Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType,
                                                 L, oldSext->getType(), Rewriter,
                                                 InsertPt);
-              APInt APcopy = APInt(AddRHS->getValue());
-              ConstantInt* newAddRHS =ConstantInt::get(APcopy.sext(newBitSize));
+              APInt APnewAddRHS = APInt(AddRHS->getValue()).sext(newBitSize);
+              if (newBitSize > truncSize)
+                APnewAddRHS = APnewAddRHS.trunc(truncSize);
+              ConstantInt* newAddRHS =ConstantInt::get(APnewAddRHS);
               Value *NewAdd = 
                     BinaryOperator::CreateAdd(TruncIndVar, newAddRHS,
                                               UInst->getName()+".nosex", UInst);
@@ -770,24 +774,28 @@
         // Try for sext(i | constant).  This is safe as long as the
         // high bit of the constant is not set.
         if (UInst && UInst->getOpcode()==Instruction::Or &&
+            !UInst->use_empty() &&
             allUsesAreSameTyped(Instruction::SExt, UInst) && NoSignedWrap &&
             isa<ConstantInt>(UInst->getOperand(1))) {
           ConstantInt* RHS = dyn_cast<ConstantInt>(UInst->getOperand(1));
           if (!RHS->getValue().isNegative()) {
             uint64_t newBitSize = LargestType->getPrimitiveSizeInBits();
             SExtInst* oldSext = dyn_cast<SExtInst>(UInst->use_begin());
+            uint64_t truncSize = oldSext->getType()->getPrimitiveSizeInBits();
             Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType,
                                               L, oldSext->getType(), Rewriter,
                                               InsertPt);
-            APInt APcopy = APInt(RHS->getValue());
-            ConstantInt* newRHS =ConstantInt::get(APcopy.sext(newBitSize));
-            Value *NewAdd = 
-                  BinaryOperator::CreateOr(TruncIndVar, newRHS,
+            APInt APnewOrRHS = APInt(RHS->getValue()).sext(newBitSize);
+            if (newBitSize > truncSize)
+              APnewOrRHS = APnewOrRHS.trunc(truncSize);
+            ConstantInt* newOrRHS =ConstantInt::get(APnewOrRHS);
+            Value *NewOr = 
+                  BinaryOperator::CreateOr(TruncIndVar, newOrRHS,
                                             UInst->getName()+".nosex", UInst);
             for (Value::use_iterator UI2 = UInst->use_begin(), 
                   UE2 = UInst->use_end(); UI2 != UE2; ++UI2) {
               Instruction *II = dyn_cast<Instruction>(UI2);
-              II->replaceAllUsesWith(NewAdd);
+              II->replaceAllUsesWith(NewOr);
               DeadInsts.insert(II);
             }
             DeadInsts.insert(UInst);
@@ -805,15 +813,19 @@
         // (RHS doesn't have to be constant.  There should be a better approach
         // than bottom-up pattern matching for this...)
         if (UInst && UInst->getOpcode()==Instruction::And &&
+            !UInst->use_empty() &&
             allUsesAreSameTyped(Instruction::ZExt, UInst) &&
             isa<ConstantInt>(UInst->getOperand(1))) {
           uint64_t newBitSize = LargestType->getPrimitiveSizeInBits();
           ConstantInt* AndRHS = dyn_cast<ConstantInt>(UInst->getOperand(1));
           ZExtInst* oldZext = dyn_cast<ZExtInst>(UInst->use_begin());
+          uint64_t truncSize = oldZext->getType()->getPrimitiveSizeInBits();
           Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType,
                                   L, oldZext->getType(), Rewriter, InsertPt);
-          APInt APcopy = APInt(AndRHS->getValue());
-          ConstantInt* newAndRHS = ConstantInt::get(APcopy.zext(newBitSize));
+          APInt APnewAndRHS = APInt(AndRHS->getValue()).zext(newBitSize);
+          if (newBitSize > truncSize)
+            APnewAndRHS = APnewAndRHS.trunc(truncSize);
+          ConstantInt* newAndRHS = ConstantInt::get(APnewAndRHS);
           Value *NewAnd = 
                 BinaryOperator::CreateAnd(TruncIndVar, newAndRHS,
                                           UInst->getName()+".nozex", UInst);
@@ -839,14 +851,18 @@
           ConstantInt* AddRHS = dyn_cast<ConstantInt>(UInst->getOperand(1));
           Instruction *UInst2 = dyn_cast<Instruction>(UInst->use_begin());
           if (UInst2 && UInst2->getOpcode() == Instruction::And &&
+              !UInst2->use_empty() &&
               allUsesAreSameTyped(Instruction::ZExt, UInst2) &&
               isa<ConstantInt>(UInst2->getOperand(1))) {
             ZExtInst* oldZext = dyn_cast<ZExtInst>(UInst2->use_begin());
+            uint64_t truncSize = oldZext->getType()->getPrimitiveSizeInBits();
             Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType,
                                     L, oldZext->getType(), Rewriter, InsertPt);
             ConstantInt* AndRHS = dyn_cast<ConstantInt>(UInst2->getOperand(1));
-            APInt APcopy = APInt(AddRHS->getValue());
-            ConstantInt* newAddRHS = ConstantInt::get(APcopy.zext(newBitSize));
+            APInt APnewAddRHS = APInt(AddRHS->getValue()).zext(newBitSize);
+            if (newBitSize > truncSize)
+              APnewAddRHS = APnewAddRHS.trunc(truncSize);
+            ConstantInt* newAddRHS = ConstantInt::get(APnewAddRHS);
             Value *NewAdd = ((UInst->getOpcode()==Instruction::Add) ?
                   BinaryOperator::CreateAdd(TruncIndVar, newAddRHS,
                                             UInst->getName()+".nozex", UInst2) :

Added: llvm/branches/Apple/Dib/test/Transforms/IndVarSimplify/2009-04-22-IndvarCrash.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/test/Transforms/IndVarSimplify/2009-04-22-IndvarCrash.ll?rev=69845&view=auto

==============================================================================
--- llvm/branches/Apple/Dib/test/Transforms/IndVarSimplify/2009-04-22-IndvarCrash.ll (added)
+++ llvm/branches/Apple/Dib/test/Transforms/IndVarSimplify/2009-04-22-IndvarCrash.ll Wed Apr 22 18:55:16 2009
@@ -0,0 +1,35 @@
+; RUN: llvm-as < %s | opt -indvars
+; rdar://6817574
+
+define i32 @t1() nounwind ssp {
+entry:
+	br label %bb32
+
+bb32:		; preds = %bb32, %entry
+	%mbPartIdx.0.reg2mem.0 = phi i8 [ %2, %bb32 ], [ 0, %entry ]		; <i8> [#uses=3]
+	%0 = and i8 %mbPartIdx.0.reg2mem.0, 1		; <i8> [#uses=0]
+	%1 = zext i8 %mbPartIdx.0.reg2mem.0 to i64		; <i64> [#uses=0]
+	%2 = add i8 %mbPartIdx.0.reg2mem.0, 1		; <i8> [#uses=2]
+	%3 = icmp ugt i8 %2, 3		; <i1> [#uses=1]
+	br i1 %3, label %bb41, label %bb32
+
+bb41:		; preds = %bb32
+	ret i32 0
+}
+
+define i32 @t2() nounwind ssp {
+entry:
+	br label %bb116
+
+bb116:		; preds = %bb116, %entry
+	%mbPartIdx.1.reg2mem.0 = phi i8 [ %3, %bb116 ], [ 0, %entry ]		; <i8> [#uses=3]
+	%0 = and i8 %mbPartIdx.1.reg2mem.0, 1		; <i8> [#uses=1]
+	%1 = zext i8 %mbPartIdx.1.reg2mem.0 to i64		; <i64> [#uses=0]
+	%2 = zext i8 %0 to i32		; <i32> [#uses=0]
+	%3 = add i8 %mbPartIdx.1.reg2mem.0, 1		; <i8> [#uses=2]
+	%4 = icmp ugt i8 %3, 3		; <i1> [#uses=1]
+	br i1 %4, label %bb131, label %bb116
+
+bb131:		; preds = %bb116
+	unreachable
+}





More information about the llvm-branch-commits mailing list