llvm.org GIT mirror llvm / 6f8abf9
Crash less. The i64 restriction in BinomialCoefficient caused some problems with code that was expecting different bit widths for different values. Make getTruncateOrZeroExtend a method on ScalarEvolution, and use it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@52248 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 12 years ago
3 changed file(s) with 78 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
252252 SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
253253 const SCEVHandle &RHS);
254254
255 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
256 /// of the input value to the specified type. If the type must be
257 /// extended, it is zero extended.
258 SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty);
259
255260 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
256261 /// specified signed integer value and return a SCEV for the constant.
257262 SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
498498 return getUnknown(C);
499499 }
500500
501 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
502 /// input value to the specified type. If the type must be extended, it is zero
503 /// extended.
504 static SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty,
505 ScalarEvolution &SE) {
506 const Type *SrcTy = V->getType();
507 assert(SrcTy->isInteger() && Ty->isInteger() &&
508 "Cannot truncate or zero extend with non-integer arguments!");
509 if (SrcTy->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
510 return V; // No conversion
511 if (SrcTy->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits())
512 return SE.getTruncateExpr(V, Ty);
513 return SE.getZeroExtendExpr(V, Ty);
514 }
515
516501 /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
517502 ///
518503 SCEVHandle ScalarEvolution::getNegativeSCEV(const SCEVHandle &V) {
584569 #endif
585570
586571 const IntegerType *DividendTy = IntegerType::get(DividendBits);
587 const SCEVHandle ExIt = SE.getZeroExtendExpr(It, DividendTy);
572 const SCEVHandle ExIt = SE.getTruncateOrZeroExtend(It, DividendTy);
588573
589574 // The final number of bits we need to perform the division is the maximum of
590575 // dividend and divisor bitwidths.
606591 Dividend *= N-(K-1);
607592 if (DividendTy != DivisionTy)
608593 Dividend = Dividend.zext(DivisionTy->getBitWidth());
609 return SE.getConstant(Dividend.udiv(Divisor).trunc(It->getBitWidth()));
594
595 APInt Result = Dividend.udiv(Divisor);
596 if (Result.getBitWidth() != It->getBitWidth())
597 Result = Result.trunc(It->getBitWidth());
598
599 return SE.getConstant(Result);
610600 }
611601
612602 SCEVHandle Dividend = ExIt;
614604 Dividend =
615605 SE.getMulExpr(Dividend,
616606 SE.getMinusSCEV(ExIt, SE.getIntegerSCEV(i, DividendTy)));
617 if (DividendTy != DivisionTy)
618 Dividend = SE.getZeroExtendExpr(Dividend, DivisionTy);
619 return
620 SE.getTruncateExpr(SE.getUDivExpr(Dividend, SE.getConstant(Divisor)),
621 It->getType());
607
608 return SE.getTruncateOrZeroExtend(
609 SE.getUDivExpr(
610 SE.getTruncateOrZeroExtend(Dividend, DivisionTy),
611 SE.getConstant(Divisor)
612 ), It->getType());
622613 }
623614
624615 /// evaluateAtIteration - Return the value of this chain of recurrences at
700691 SCEVSignExtendExpr *&Result = (*SCEVSignExtends)[std::make_pair(Op, Ty)];
701692 if (Result == 0) Result = new SCEVSignExtendExpr(Op, Ty);
702693 return Result;
694 }
695
696 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
697 /// of the input value to the specified type. If the type must be
698 /// extended, it is zero extended.
699 SCEVHandle ScalarEvolution::getTruncateOrZeroExtend(const SCEVHandle &V,
700 const Type *Ty) {
701 const Type *SrcTy = V->getType();
702 assert(SrcTy->isInteger() && Ty->isInteger() &&
703 "Cannot truncate or zero extend with non-integer arguments!");
704 if (SrcTy->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
705 return V; // No conversion
706 if (SrcTy->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits())
707 return getTruncateExpr(V, Ty);
708 return getZeroExtendExpr(V, Ty);
703709 }
704710
705711 // get - Get a canonical add expression, or something simpler if possible.
24312437 // loop iterates. Compute this now.
24322438 SCEVHandle IterationCount = getIterationCount(AddRec->getLoop());
24332439 if (IterationCount == UnknownValue) return UnknownValue;
2434 IterationCount = getTruncateOrZeroExtend(IterationCount,
2435 AddRec->getType(), SE);
2440 IterationCount = SE.getTruncateOrZeroExtend(IterationCount,
2441 AddRec->getType());
24362442
24372443 // If the value is affine, simplify the expression evaluation to just
24382444 // Start + Step*IterationCount.
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution 2>/dev/null
1 ; PR2433
2
3 define i32 @main1(i32 %argc, i8** %argv) nounwind {
4 entry:
5 br i1 false, label %bb10, label %bb23
6
7 bb10: ; preds = %bb10, %entry
8 %accum.03 = phi i64 [ %tmp14, %bb10 ], [ 0, %entry ] ; [#uses=1]
9 %i.02 = phi i32 [ %tmp16, %bb10 ], [ 0, %entry ] ; [#uses=1]
10 %d.1.01 = phi i64 [ %tmp5.i, %bb10 ], [ 0, %entry ] ; [#uses=1]
11 %tmp5.i = add i64 %d.1.01, 1 ; [#uses=2]
12 %tmp14 = add i64 %accum.03, %tmp5.i ; [#uses=2]
13 %tmp16 = add i32 %i.02, 1 ; [#uses=2]
14 %tmp20 = icmp slt i32 %tmp16, 0 ; [#uses=1]
15 br i1 %tmp20, label %bb10, label %bb23
16
17 bb23: ; preds = %bb10, %entry
18 %accum.0.lcssa = phi i64 [ 0, %entry ], [ %tmp14, %bb10 ] ; [#uses=0]
19 ret i32 0
20 }
21
22 define i32 @main2(i32 %argc, i8** %argv) {
23 entry:
24 %tmp8 = tail call i32 @atoi( i8* null ) nounwind readonly ; [#uses=1]
25 br i1 false, label %bb9, label %bb21
26
27 bb9: ; preds = %bb9, %entry
28 %accum.03 = phi i64 [ %tmp12, %bb9 ], [ 0, %entry ] ; [#uses=1]
29 %i.02 = phi i32 [ %tmp14, %bb9 ], [ 0, %entry ] ; [#uses=1]
30 %d.1.01 = phi i64 [ %tmp4.i, %bb9 ], [ 0, %entry ] ; [#uses=1]
31 %tmp4.i = add i64 %d.1.01, 1 ; [#uses=2]
32 %tmp12 = add i64 %accum.03, %tmp4.i ; [#uses=2]
33 %tmp14 = add i32 %i.02, 1 ; [#uses=2]
34 %tmp18 = icmp slt i32 %tmp14, %tmp8 ; [#uses=1]
35 br i1 %tmp18, label %bb9, label %bb21
36
37 bb21: ; preds = %bb9, %entry
38 %accum.0.lcssa = phi i64 [ 0, %entry ], [ %tmp12, %bb9 ] ; [#uses=0]
39 ret i32 0
40 }
41
42 declare i32 @atoi(i8*) nounwind readonly