Changes in directory llvm/lib/Analysis:
BasicAliasAnalysis.cpp updated: 1.94 -> 1.95 ConstantRange.cpp updated: 1.22 -> 1.23 LoopInfo.cpp updated: 1.81 -> 1.82 ScalarEvolution.cpp updated: 1.76 -> 1.77 ValueNumbering.cpp updated: 1.23 -> 1.24 --- Log message: For PR950: http://llvm.org/PR950 : This patch removes the SetCC instructions and replaces them with the ICmp and FCmp instructions. The SetCondInst instruction has been removed and been replaced with ICmpInst and FCmpInst. --- Diffs of the changes: (+221 -202) BasicAliasAnalysis.cpp | 3 ConstantRange.cpp | 199 +++++++++++++++++++++++++------------------------ LoopInfo.cpp | 18 ++-- ScalarEvolution.cpp | 198 +++++++++++++++++++++++++----------------------- ValueNumbering.cpp | 5 + 5 files changed, 221 insertions(+), 202 deletions(-) Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp diff -u llvm/lib/Analysis/BasicAliasAnalysis.cpp:1.94 llvm/lib/Analysis/BasicAliasAnalysis.cpp:1.95 --- llvm/lib/Analysis/BasicAliasAnalysis.cpp:1.94 Tue Dec 12 17:36:14 2006 +++ llvm/lib/Analysis/BasicAliasAnalysis.cpp Sat Dec 23 00:05:40 2006 @@ -590,7 +590,8 @@ // Make sure they are comparable (ie, not constant expressions), and // make sure the GEP with the smaller leading constant is GEP1. if (G1OC) { - Constant *Compare = ConstantExpr::getSetGT(G1OC, G2OC); + Constant *Compare = ConstantExpr::getICmp(ICmpInst::ICMP_SGT, + G1OC, G2OC); if (ConstantBool *CV = dyn_cast<ConstantBool>(Compare)) { if (CV->getValue()) // If they are comparable and G2 > G1 std::swap(GEP1Ops, GEP2Ops); // Make GEP1 < GEP2 Index: llvm/lib/Analysis/ConstantRange.cpp diff -u llvm/lib/Analysis/ConstantRange.cpp:1.22 llvm/lib/Analysis/ConstantRange.cpp:1.23 --- llvm/lib/Analysis/ConstantRange.cpp:1.22 Tue Dec 12 17:36:14 2006 +++ llvm/lib/Analysis/ConstantRange.cpp Sat Dec 23 00:05:40 2006 @@ -24,56 +24,43 @@ #include "llvm/Support/ConstantRange.h" #include "llvm/Constants.h" #include "llvm/Instruction.h" +#include "llvm/Instructions.h" #include "llvm/Type.h" #include "llvm/Support/Streams.h" #include <ostream> using namespace llvm; -static ConstantIntegral *getMaxValue(const Type *Ty) { - switch (Ty->getTypeID()) { - case Type::BoolTyID: return ConstantBool::getTrue(); - case Type::SByteTyID: - case Type::ShortTyID: - case Type::IntTyID: - case Type::LongTyID: { - // Calculate 011111111111111... - unsigned TypeBits = Ty->getPrimitiveSize()*8; - int64_t Val = INT64_MAX; // All ones - Val >>= 64-TypeBits; // Shift out unwanted 1 bits... - return ConstantInt::get(Ty, Val); - } - - case Type::UByteTyID: - case Type::UShortTyID: - case Type::UIntTyID: - case Type::ULongTyID: return ConstantInt::getAllOnesValue(Ty); - - default: return 0; +static ConstantIntegral *getMaxValue(const Type *Ty, bool isSigned = false) { + if (Ty == Type::BoolTy) + return ConstantBool::getTrue(); + if (Ty->isInteger()) { + if (isSigned) { + // Calculate 011111111111111... + unsigned TypeBits = Ty->getPrimitiveSize()*8; + int64_t Val = INT64_MAX; // All ones + Val >>= 64-TypeBits; // Shift out unwanted 1 bits... + return ConstantInt::get(Ty, Val); + } + return ConstantInt::getAllOnesValue(Ty); } + return 0; } // Static constructor to create the minimum constant for an integral type... -static ConstantIntegral *getMinValue(const Type *Ty) { - switch (Ty->getTypeID()) { - case Type::BoolTyID: return ConstantBool::getFalse(); - case Type::SByteTyID: - case Type::ShortTyID: - case Type::IntTyID: - case Type::LongTyID: { - // Calculate 1111111111000000000000 - unsigned TypeBits = Ty->getPrimitiveSize()*8; - int64_t Val = -1; // All ones - Val <<= TypeBits-1; // Shift over to the right spot - return ConstantInt::get(Ty, Val); - } - - case Type::UByteTyID: - case Type::UShortTyID: - case Type::UIntTyID: - case Type::ULongTyID: return ConstantInt::get(Ty, 0); - - default: return 0; +static ConstantIntegral *getMinValue(const Type *Ty, bool isSigned = false) { + if (Ty == Type::BoolTy) + return ConstantBool::getFalse(); + if (Ty->isInteger()) { + if (isSigned) { + // Calculate 1111111111000000000000 + unsigned TypeBits = Ty->getPrimitiveSize()*8; + int64_t Val = -1; // All ones + Val <<= TypeBits-1; // Shift over to the right spot + return ConstantInt::get(Ty, Val); + } + return ConstantInt::get(Ty, 0); } + return 0; } static ConstantIntegral *Next(ConstantIntegral *CI) { if (ConstantBool *CB = dyn_cast<ConstantBool>(CI)) @@ -84,25 +71,30 @@ return cast<ConstantIntegral>(Result); } -static bool LT(ConstantIntegral *A, ConstantIntegral *B) { - Constant *C = ConstantExpr::getSetLT(A, B); +static bool LT(ConstantIntegral *A, ConstantIntegral *B, bool isSigned) { + Constant *C = ConstantExpr::getICmp( + (isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT), A, B); assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??"); return cast<ConstantBool>(C)->getValue(); } -static bool LTE(ConstantIntegral *A, ConstantIntegral *B) { - Constant *C = ConstantExpr::getSetLE(A, B); +static bool LTE(ConstantIntegral *A, ConstantIntegral *B, bool isSigned) { + Constant *C = ConstantExpr::getICmp( + (isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE), A, B); assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??"); return cast<ConstantBool>(C)->getValue(); } -static bool GT(ConstantIntegral *A, ConstantIntegral *B) { return LT(B, A); } +static bool GT(ConstantIntegral *A, ConstantIntegral *B, bool isSigned) { + return LT(B, A, isSigned); } -static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) { - return LT(A, B) ? A : B; -} -static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) { - return GT(A, B) ? A : B; +static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B, + bool isSigned) { + return LT(A, B, isSigned) ? A : B; +} +static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B, + bool isSigned) { + return GT(A, B, isSigned) ? A : B; } /// Initialize a full (the default) or empty set for the specified type. @@ -118,47 +110,62 @@ /// Initialize a range to hold the single specified value. /// -ConstantRange::ConstantRange(Constant *V) - : Lower(cast<ConstantIntegral>(V)), Upper(Next(cast<ConstantIntegral>(V))) { -} +ConstantRange::ConstantRange(Constant *V) + : Lower(cast<ConstantIntegral>(V)), Upper(Next(cast<ConstantIntegral>(V))) { } /// Initialize a range of values explicitly... this will assert out if /// Lower==Upper and Lower != Min or Max for its type (or if the two constants /// have different types) /// -ConstantRange::ConstantRange(Constant *L, Constant *U) +ConstantRange::ConstantRange(Constant *L, Constant *U) : Lower(cast<ConstantIntegral>(L)), Upper(cast<ConstantIntegral>(U)) { assert(Lower->getType() == Upper->getType() && "Incompatible types for ConstantRange!"); // Make sure that if L & U are equal that they are either Min or Max... assert((L != U || (L == getMaxValue(L->getType()) || - L == getMinValue(L->getType()))) && - "Lower == Upper, but they aren't min or max for type!"); + L == getMinValue(L->getType()))) + && "Lower == Upper, but they aren't min or max for type!"); } /// Initialize a set of values that all satisfy the condition with C. /// -ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) { - switch (SetCCOpcode) { - default: assert(0 && "Invalid SetCC opcode to ConstantRange ctor!"); - case Instruction::SetEQ: Lower = C; Upper = Next(C); return; - case Instruction::SetNE: Upper = C; Lower = Next(C); return; - case Instruction::SetLT: +ConstantRange::ConstantRange(unsigned short ICmpOpcode, ConstantIntegral *C) { + switch (ICmpOpcode) { + default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!"); + case ICmpInst::ICMP_EQ: Lower = C; Upper = Next(C); return; + case ICmpInst::ICMP_NE: Upper = C; Lower = Next(C); return; + case ICmpInst::ICMP_ULT: Lower = getMinValue(C->getType()); Upper = C; return; - case Instruction::SetGT: + case ICmpInst::ICMP_SLT: + Lower = getMinValue(C->getType(), true); + Upper = C; + return; + case ICmpInst::ICMP_UGT: + Lower = Next(C); + Upper = getMinValue(C->getType()); // Min = Next(Max) + return; + case ICmpInst::ICMP_SGT: Lower = Next(C); - Upper = getMinValue(C->getType()); // Min = Next(Max) + Upper = getMinValue(C->getType(), true); // Min = Next(Max) return; - case Instruction::SetLE: + case ICmpInst::ICMP_ULE: Lower = getMinValue(C->getType()); Upper = Next(C); return; - case Instruction::SetGE: + case ICmpInst::ICMP_SLE: + Lower = getMinValue(C->getType(), true); + Upper = Next(C); + return; + case ICmpInst::ICMP_UGE: + Lower = C; + Upper = getMinValue(C->getType()); // Min = Next(Max) + return; + case ICmpInst::ICMP_SGE: Lower = C; - Upper = getMinValue(C->getType()); // Min = Next(Max) + Upper = getMinValue(C->getType(), true); // Min = Next(Max) return; } } @@ -182,11 +189,10 @@ /// isWrappedSet - Return true if this set wraps around the top of the range, /// for example: [100, 8) /// -bool ConstantRange::isWrappedSet() const { - return GT(Lower, Upper); +bool ConstantRange::isWrappedSet(bool isSigned) const { + return GT(Lower, Upper, isSigned); } - /// getSingleElement - If this set contains a single element, return it, /// otherwise return null. ConstantIntegral *ConstantRange::getSingleElement() const { @@ -212,19 +218,17 @@ /// contains - Return true if the specified value is in the set. /// -bool ConstantRange::contains(ConstantInt *Val) const { +bool ConstantRange::contains(ConstantInt *Val, bool isSigned) const { if (Lower == Upper) { if (isFullSet()) return true; return false; } - if (!isWrappedSet()) - return LTE(Lower, Val) && LT(Val, Upper); - return LTE(Lower, Val) || LT(Val, Upper); + if (!isWrappedSet(isSigned)) + return LTE(Lower, Val, isSigned) && LT(Val, Upper, isSigned); + return LTE(Lower, Val, isSigned) || LT(Val, Upper, isSigned); } - - /// subtract - Subtract the specified constant from the endpoints of this /// constant range. ConstantRange ConstantRange::subtract(ConstantInt *CI) const { @@ -241,15 +245,16 @@ // it is known that LHS is wrapped and RHS isn't. // static ConstantRange intersect1Wrapped(const ConstantRange &LHS, - const ConstantRange &RHS) { - assert(LHS.isWrappedSet() && !RHS.isWrappedSet()); + const ConstantRange &RHS, + bool isSigned) { + assert(LHS.isWrappedSet(isSigned) && !RHS.isWrappedSet(isSigned)); // Check to see if we overlap on the Left side of RHS... // - if (LT(RHS.getLower(), LHS.getUpper())) { + if (LT(RHS.getLower(), LHS.getUpper(), isSigned)) { // We do overlap on the left side of RHS, see if we overlap on the right of // RHS... - if (GT(RHS.getUpper(), LHS.getLower())) { + if (GT(RHS.getUpper(), LHS.getLower(), isSigned)) { // Ok, the result overlaps on both the left and right sides. See if the // resultant interval will be smaller if we wrap or not... // @@ -262,11 +267,10 @@ // No overlap on the right, just on the left. return ConstantRange(RHS.getLower(), LHS.getUpper()); } - } else { // We don't overlap on the left side of RHS, see if we overlap on the right // of RHS... - if (GT(RHS.getUpper(), LHS.getLower())) { + if (GT(RHS.getUpper(), LHS.getLower(), isSigned)) { // Simple overlap... return ConstantRange(LHS.getLower(), RHS.getUpper()); } else { @@ -279,30 +283,31 @@ /// intersect - Return the range that results from the intersection of this /// range with another range. /// -ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { +ConstantRange ConstantRange::intersectWith(const ConstantRange &CR, + bool isSigned) const { assert(getType() == CR.getType() && "ConstantRange types don't agree!"); // Handle common special cases if (isEmptySet() || CR.isFullSet()) return *this; if (isFullSet() || CR.isEmptySet()) return CR; - if (!isWrappedSet()) { - if (!CR.isWrappedSet()) { - ConstantIntegral *L = Max(Lower, CR.Lower); - ConstantIntegral *U = Min(Upper, CR.Upper); + if (!isWrappedSet(isSigned)) { + if (!CR.isWrappedSet(isSigned)) { + ConstantIntegral *L = Max(Lower, CR.Lower, isSigned); + ConstantIntegral *U = Min(Upper, CR.Upper, isSigned); - if (LT(L, U)) // If range isn't empty... + if (LT(L, U, isSigned)) // If range isn't empty... return ConstantRange(L, U); else return ConstantRange(getType(), false); // Otherwise, return empty set } else - return intersect1Wrapped(CR, *this); + return intersect1Wrapped(CR, *this, isSigned); } else { // We know "this" is wrapped... - if (!CR.isWrappedSet()) - return intersect1Wrapped(*this, CR); + if (!CR.isWrappedSet(isSigned)) + return intersect1Wrapped(*this, CR, isSigned); else { // Both ranges are wrapped... - ConstantIntegral *L = Max(Lower, CR.Lower); - ConstantIntegral *U = Min(Upper, CR.Upper); + ConstantIntegral *L = Max(Lower, CR.Lower, isSigned); + ConstantIntegral *U = Min(Upper, CR.Upper, isSigned); return ConstantRange(L, U); } } @@ -315,7 +320,8 @@ /// 15), which includes 9, 10, and 11, which were not included in either set /// before. /// -ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { +ConstantRange ConstantRange::unionWith(const ConstantRange &CR, + bool isSigned) const { assert(getType() == CR.getType() && "ConstantRange types don't agree!"); assert(0 && "Range union not implemented yet!"); @@ -325,7 +331,7 @@ /// zeroExtend - Return a new range in the specified integer type, which must /// be strictly larger than the current type. The returned range will -/// correspond to the possible range of values if the source range had been +/// correspond to the possible range of values as if the source range had been /// zero extended. ConstantRange ConstantRange::zeroExtend(const Type *Ty) const { assert(getLower()->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() && @@ -346,7 +352,7 @@ /// truncate - Return a new range in the specified integer type, which must be /// strictly smaller than the current type. The returned range will -/// correspond to the possible range of values if the source range had been +/// correspond to the possible range of values as if the source range had been /// truncated to the specified type. ConstantRange ConstantRange::truncate(const Type *Ty) const { assert(getLower()->getType()->getPrimitiveSize() > Ty->getPrimitiveSize() && @@ -360,7 +366,6 @@ ConstantExpr::getTrunc(getUpper(), Ty)); } - /// print - Print out the bounds to a stream... /// void ConstantRange::print(std::ostream &OS) const { Index: llvm/lib/Analysis/LoopInfo.cpp diff -u llvm/lib/Analysis/LoopInfo.cpp:1.81 llvm/lib/Analysis/LoopInfo.cpp:1.82 --- llvm/lib/Analysis/LoopInfo.cpp:1.81 Wed Dec 6 19:30:31 2006 +++ llvm/lib/Analysis/LoopInfo.cpp Sat Dec 23 00:05:40 2006 @@ -536,7 +536,7 @@ /// returns null. /// Value *Loop::getTripCount() const { - // Canonical loops will end with a 'setne I, V', where I is the incremented + // Canonical loops will end with a 'cmp ne I, V', where I is the incremented // canonical induction variable and V is the trip count of the loop. Instruction *Inc = getCanonicalInductionVariableIncrement(); if (Inc == 0) return 0; @@ -546,15 +546,17 @@ IV->getIncomingBlock(contains(IV->getIncomingBlock(1))); if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator())) - if (BI->isConditional()) - if (SetCondInst *SCI = dyn_cast<SetCondInst>(BI->getCondition())) - if (SCI->getOperand(0) == Inc) + if (BI->isConditional()) { + if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) { + if (ICI->getOperand(0) == Inc) if (BI->getSuccessor(0) == getHeader()) { - if (SCI->getOpcode() == Instruction::SetNE) - return SCI->getOperand(1); - } else if (SCI->getOpcode() == Instruction::SetEQ) { - return SCI->getOperand(1); + if (ICI->getPredicate() == ICmpInst::ICMP_NE) + return ICI->getOperand(1); + } else if (ICI->getPredicate() == ICmpInst::ICMP_EQ) { + return ICI->getOperand(1); } + } + } return 0; } Index: llvm/lib/Analysis/ScalarEvolution.cpp diff -u llvm/lib/Analysis/ScalarEvolution.cpp:1.76 llvm/lib/Analysis/ScalarEvolution.cpp:1.77 --- llvm/lib/Analysis/ScalarEvolution.cpp:1.76 Thu Dec 21 12:59:16 2006 +++ llvm/lib/Analysis/ScalarEvolution.cpp Sat Dec 23 00:05:40 2006 @@ -177,8 +177,7 @@ // are signless. There won't be a need to bitcast then. if (V->getType()->isSigned()) { const Type *NewTy = V->getType()->getUnsignedVersion(); - V = cast<ConstantInt>( - ConstantExpr::getBitCast(V, NewTy)); + V = cast<ConstantInt>(ConstantExpr::getBitCast(V, NewTy)); } SCEVConstant *&R = (*SCEVConstants)[V]; @@ -461,15 +460,8 @@ C = Constant::getNullValue(Ty); else if (Ty->isFloatingPoint()) C = ConstantFP::get(Ty, Val); - /// FIXME:Signless. when integer types are signless, just change this to: - /// else - /// C = ConstantInt::get(Ty, Val); - else if (Ty->isSigned()) + else C = ConstantInt::get(Ty, Val); - else { - C = ConstantInt::get(Ty->getSignedVersion(), Val); - C = ConstantExpr::getBitCast(C, Ty); - } return SCEVUnknown::get(C); } @@ -514,8 +506,7 @@ for (; NumSteps; --NumSteps) Result *= Val-(NumSteps-1); Constant *Res = ConstantInt::get(Type::ULongTy, Result); - return SCEVUnknown::get( - ConstantExpr::getTruncOrBitCast(Res, V->getType())); + return SCEVUnknown::get(ConstantExpr::getTruncOrBitCast(Res, V->getType())); } const Type *Ty = V->getType(); @@ -1162,7 +1153,7 @@ SCEVHandle ComputeLoadConstantCompareIterationCount(LoadInst *LI, Constant *RHS, const Loop *L, - unsigned SetCCOpcode); + ICmpInst::Predicate p); /// ComputeIterationCountExhaustively - If the trip is known to execute a /// constant number of times (the condition evolves only from constants), @@ -1521,17 +1512,21 @@ BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); if (ExitBr == 0) return UnknownValue; assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); - SetCondInst *ExitCond = dyn_cast<SetCondInst>(ExitBr->getCondition()); - if (ExitCond == 0) // Not a setcc + ICmpInst *ExitCond = dyn_cast<ICmpInst>(ExitBr->getCondition()); + + // If its not an integer comparison then compute it the hard way. + // Note that ICmpInst deals with pointer comparisons too so we must check + // the type of the operand. + if (ExitCond == 0 || !ExitCond->getOperand(0)->getType()->isIntegral()) return ComputeIterationCountExhaustively(L, ExitBr->getCondition(), ExitBr->getSuccessor(0) == ExitBlock); - // If the condition was exit on true, convert the condition to exit on false. - Instruction::BinaryOps Cond; + // If the condition was exit on true, convert the condition to exit on false + ICmpInst::Predicate Cond; if (ExitBr->getSuccessor(1) == ExitBlock) - Cond = ExitCond->getOpcode(); + Cond = ExitCond->getPredicate(); else - Cond = ExitCond->getInverseCondition(); + Cond = ExitCond->getInversePredicate(); // Handle common loops like: for (X = "string"; *X; ++X) if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) @@ -1550,12 +1545,12 @@ Tmp = getSCEVAtScope(RHS, L); if (!isa<SCEVCouldNotCompute>(Tmp)) RHS = Tmp; - // At this point, we would like to compute how many iterations of the loop the - // predicate will return true for these inputs. + // At this point, we would like to compute how many iterations of the + // loop the predicate will return true for these inputs. if (isa<SCEVConstant>(LHS) && !isa<SCEVConstant>(RHS)) { // If there is a constant, force it into the RHS. std::swap(LHS, RHS); - Cond = SetCondInst::getSwappedCondition(Cond); + Cond = ICmpInst::getSwappedPredicate(Cond); } // FIXME: think about handling pointer comparisons! i.e.: @@ -1590,53 +1585,48 @@ CompRange = ConstantRange(NewL, NewU); } - SCEVHandle Ret = AddRec->getNumIterationsInRange(CompRange); + SCEVHandle Ret = AddRec->getNumIterationsInRange(CompRange, + ICmpInst::isSignedPredicate(Cond)); if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; } } switch (Cond) { - case Instruction::SetNE: // while (X != Y) + case ICmpInst::ICMP_NE: { // while (X != Y) // Convert to: while (X-Y != 0) - if (LHS->getType()->isInteger()) { - SCEVHandle TC = HowFarToZero(SCEV::getMinusSCEV(LHS, RHS), L); - if (!isa<SCEVCouldNotCompute>(TC)) return TC; - } + SCEVHandle TC = HowFarToZero(SCEV::getMinusSCEV(LHS, RHS), L); + if (!isa<SCEVCouldNotCompute>(TC)) return TC; break; - case Instruction::SetEQ: + } + case ICmpInst::ICMP_EQ: { // Convert to: while (X-Y == 0) // while (X == Y) - if (LHS->getType()->isInteger()) { - SCEVHandle TC = HowFarToNonZero(SCEV::getMinusSCEV(LHS, RHS), L); - if (!isa<SCEVCouldNotCompute>(TC)) return TC; - } + SCEVHandle TC = HowFarToNonZero(SCEV::getMinusSCEV(LHS, RHS), L); + if (!isa<SCEVCouldNotCompute>(TC)) return TC; break; - case Instruction::SetLT: - if (LHS->getType()->isInteger() && - ExitCond->getOperand(0)->getType()->isSigned()) { - SCEVHandle TC = HowManyLessThans(LHS, RHS, L); - if (!isa<SCEVCouldNotCompute>(TC)) return TC; - } + } + case ICmpInst::ICMP_SLT: { + SCEVHandle TC = HowManyLessThans(LHS, RHS, L); + if (!isa<SCEVCouldNotCompute>(TC)) return TC; break; - case Instruction::SetGT: - if (LHS->getType()->isInteger() && - ExitCond->getOperand(0)->getType()->isSigned()) { - SCEVHandle TC = HowManyLessThans(RHS, LHS, L); - if (!isa<SCEVCouldNotCompute>(TC)) return TC; - } + } + case ICmpInst::ICMP_SGT: { + SCEVHandle TC = HowManyLessThans(RHS, LHS, L); + if (!isa<SCEVCouldNotCompute>(TC)) return TC; break; + } default: #if 0 cerr << "ComputeIterationCount "; if (ExitCond->getOperand(0)->getType()->isUnsigned()) cerr << "[unsigned] "; cerr << *LHS << " " - << Instruction::getOpcodeName(Cond) << " " << *RHS << "\n"; + << Instruction::getOpcodeName(Instruction::ICmp) + << " " << *RHS << "\n"; #endif break; } - return ComputeIterationCountExhaustively(L, ExitCond, - ExitBr->getSuccessor(0) == ExitBlock); + ExitBr->getSuccessor(0) == ExitBlock); } static ConstantInt * @@ -1686,7 +1676,8 @@ /// 'setcc load X, cst', try to se if we can compute the trip count. SCEVHandle ScalarEvolutionsImpl:: ComputeLoadConstantCompareIterationCount(LoadInst *LI, Constant *RHS, - const Loop *L, unsigned SetCCOpcode) { + const Loop *L, + ICmpInst::Predicate predicate) { if (LI->isVolatile()) return UnknownValue; // Check to see if the loaded pointer is a getelementptr of a global. @@ -1742,7 +1733,7 @@ if (Result == 0) break; // Cannot compute! // Evaluate the condition for this iteration. - Result = ConstantExpr::get(SetCCOpcode, Result, RHS); + Result = ConstantExpr::getICmp(predicate, Result, RHS); if (!isa<ConstantBool>(Result)) break; // Couldn't decide for sure if (cast<ConstantBool>(Result)->getValue() == false) { #if 0 @@ -1761,7 +1752,7 @@ /// CanConstantFold - Return true if we can constant fold an instruction of the /// specified type, assuming that all operands were constants. static bool CanConstantFold(const Instruction *I) { - if (isa<BinaryOperator>(I) || isa<ShiftInst>(I) || + if (isa<BinaryOperator>(I) || isa<ShiftInst>(I) || isa<CmpInst>(I) || isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) return true; @@ -1790,11 +1781,18 @@ return ConstantFoldCall(cast<Function>(GV), Operands); } return 0; - case Instruction::GetElementPtr: + case Instruction::GetElementPtr: { Constant *Base = Operands[0]; Operands.erase(Operands.begin()); return ConstantExpr::getGetElementPtr(Base, Operands); } + case Instruction::ICmp: + return ConstantExpr::getICmp( + cast<ICmpInst>(I)->getPredicate(), Operands[0], Operands[1]); + case Instruction::FCmp: + return ConstantExpr::getFCmp( + cast<FCmpInst>(I)->getPredicate(), Operands[0], Operands[1]); + } return 0; } @@ -2226,8 +2224,8 @@ // Pick the smallest positive root value. assert(R1->getType()->isUnsigned()&&"Didn't canonicalize to unsigned?"); if (ConstantBool *CB = - dyn_cast<ConstantBool>(ConstantExpr::getSetLT(R1->getValue(), - R2->getValue()))) { + dyn_cast<ConstantBool>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, + R1->getValue(), R2->getValue()))) { if (CB->getValue() == false) std::swap(R1, R2); // R1 is the minimum root now. @@ -2257,7 +2255,8 @@ // already. If so, the backedge will execute zero times. if (SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { Constant *Zero = Constant::getNullValue(C->getValue()->getType()); - Constant *NonZero = ConstantExpr::getSetNE(C->getValue(), Zero); + Constant *NonZero = + ConstantExpr::getICmp(ICmpInst::ICMP_NE, C->getValue(), Zero); if (NonZero == ConstantBool::getTrue()) return getSCEV(Zero); return UnknownValue; // Otherwise it will loop infinitely. @@ -2318,40 +2317,46 @@ // Now that we found a conditional branch that dominates the loop, check to // see if it is the comparison we are looking for. - SetCondInst *SCI =dyn_cast<SetCondInst>(LoopEntryPredicate->getCondition()); - if (!SCI) return UnknownValue; - Value *PreCondLHS = SCI->getOperand(0); - Value *PreCondRHS = SCI->getOperand(1); - Instruction::BinaryOps Cond; - if (LoopEntryPredicate->getSuccessor(0) == PreheaderDest) - Cond = SCI->getOpcode(); - else - Cond = SCI->getInverseCondition(); + if (ICmpInst *ICI = dyn_cast<ICmpInst>(LoopEntryPredicate->getCondition())){ + Value *PreCondLHS = ICI->getOperand(0); + Value *PreCondRHS = ICI->getOperand(1); + ICmpInst::Predicate Cond; + if (LoopEntryPredicate->getSuccessor(0) == PreheaderDest) + Cond = ICI->getPredicate(); + else + Cond = ICI->getInversePredicate(); - switch (Cond) { - case Instruction::SetGT: - std::swap(PreCondLHS, PreCondRHS); - Cond = Instruction::SetLT; - // Fall Through. - case Instruction::SetLT: - if (PreCondLHS->getType()->isInteger() && - PreCondLHS->getType()->isSigned()) { - if (RHS != getSCEV(PreCondRHS)) - return UnknownValue; // Not a comparison against 'm'. - - if (SCEV::getMinusSCEV(AddRec->getOperand(0), One) - != getSCEV(PreCondLHS)) - return UnknownValue; // Not a comparison against 'n-1'. + switch (Cond) { + case ICmpInst::ICMP_UGT: + std::swap(PreCondLHS, PreCondRHS); + Cond = ICmpInst::ICMP_ULT; break; - } else { - return UnknownValue; + case ICmpInst::ICMP_SGT: + std::swap(PreCondLHS, PreCondRHS); + Cond = ICmpInst::ICMP_SLT; + break; + default: break; } - default: break; - } - //cerr << "Computed Loop Trip Count as: " - // << *SCEV::getMinusSCEV(RHS, AddRec->getOperand(0)) << "\n"; - return SCEV::getMinusSCEV(RHS, AddRec->getOperand(0)); + if (Cond == ICmpInst::ICMP_SLT) { + if (PreCondLHS->getType()->isInteger()) { + if (RHS != getSCEV(PreCondRHS)) + return UnknownValue; // Not a comparison against 'm'. + + if (SCEV::getMinusSCEV(AddRec->getOperand(0), One) + != getSCEV(PreCondLHS)) + return UnknownValue; // Not a comparison against 'n-1'. + } + else return UnknownValue; + } else if (Cond == ICmpInst::ICMP_ULT) + return UnknownValue; + + // cerr << "Computed Loop Trip Count as: " + // << // *SCEV::getMinusSCEV(RHS, AddRec->getOperand(0)) << "\n"; + return SCEV::getMinusSCEV(RHS, AddRec->getOperand(0)); + } + else + return UnknownValue; } return UnknownValue; @@ -2362,7 +2367,8 @@ /// this is that it returns the first iteration number where the value is not in /// the condition, thus computing the exit count. If the iteration count can't /// be computed, an instance of SCEVCouldNotCompute is returned. -SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range) const { +SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, + bool isSigned) const { if (Range.isFullSet()) // Infinite loop. return new SCEVCouldNotCompute(); @@ -2374,7 +2380,7 @@ SCEVHandle Shifted = SCEVAddRecExpr::get(Operands, getLoop()); if (SCEVAddRecExpr *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted)) return ShiftedAddRec->getNumIterationsInRange( - Range.subtract(SC->getValue())); + Range.subtract(SC->getValue()),isSigned); // This is strange and shouldn't happen. return new SCEVCouldNotCompute(); } @@ -2392,7 +2398,7 @@ // First check to see if the range contains zero. If not, the first // iteration exits. ConstantInt *Zero = ConstantInt::get(getType(), 0); - if (!Range.contains(Zero)) return SCEVConstant::get(Zero); + if (!Range.contains(Zero, isSigned)) return SCEVConstant::get(Zero); if (isAffine()) { // If this is an affine expression then we have this situation: @@ -2418,12 +2424,12 @@ // range, then we computed our trip count, otherwise wrap around or other // things must have happened. ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue); - if (Range.contains(Val)) + if (Range.contains(Val, isSigned)) return new SCEVCouldNotCompute(); // Something strange happened // Ensure that the previous value is in the range. This is a sanity check. assert(Range.contains(EvaluateConstantChrecAtConstant(this, - ConstantExpr::getSub(ExitValue, One))) && + ConstantExpr::getSub(ExitValue, One)), isSigned) && "Linear scev computation is off in a bad way!"); return SCEVConstant::get(cast<ConstantInt>(ExitValue)); } else if (isQuadratic()) { @@ -2444,8 +2450,8 @@ // Pick the smallest positive root value. assert(R1->getType()->isUnsigned() && "Didn't canonicalize to unsigned?"); if (ConstantBool *CB = - dyn_cast<ConstantBool>(ConstantExpr::getSetLT(R1->getValue(), - R2->getValue()))) { + dyn_cast<ConstantBool>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, + R1->getValue(), R2->getValue()))) { if (CB->getValue() == false) std::swap(R1, R2); // R1 is the minimum root now. @@ -2454,14 +2460,14 @@ // for "X*X < 5", for example, we should not return a root of 2. ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, R1->getValue()); - if (Range.contains(R1Val)) { + if (Range.contains(R1Val, isSigned)) { // The next iteration must be out of the range... Constant *NextVal = ConstantExpr::getAdd(R1->getValue(), ConstantInt::get(R1->getType(), 1)); R1Val = EvaluateConstantChrecAtConstant(this, NextVal); - if (!Range.contains(R1Val)) + if (!Range.contains(R1Val, isSigned)) return SCEVUnknown::get(NextVal); return new SCEVCouldNotCompute(); // Something strange happened } @@ -2472,7 +2478,7 @@ ConstantExpr::getSub(R1->getValue(), ConstantInt::get(R1->getType(), 1)); R1Val = EvaluateConstantChrecAtConstant(this, NextVal); - if (Range.contains(R1Val)) + if (Range.contains(R1Val, isSigned)) return R1; return new SCEVCouldNotCompute(); // Something strange happened } @@ -2494,7 +2500,7 @@ return new SCEVCouldNotCompute(); // Check to see if we found the value! - if (!Range.contains(cast<SCEVConstant>(Val)->getValue())) + if (!Range.contains(cast<SCEVConstant>(Val)->getValue(), isSigned)) return SCEVConstant::get(TestVal); // Increment to test the next index. Index: llvm/lib/Analysis/ValueNumbering.cpp diff -u llvm/lib/Analysis/ValueNumbering.cpp:1.23 llvm/lib/Analysis/ValueNumbering.cpp:1.24 --- llvm/lib/Analysis/ValueNumbering.cpp:1.23 Sun Nov 26 19:05:09 2006 +++ llvm/lib/Analysis/ValueNumbering.cpp Sat Dec 23 00:05:40 2006 @@ -161,6 +161,11 @@ I1.getParent()->getParent() != I2->getParent()->getParent()) return false; + // If they are CmpInst instructions, check their predicates + if (CmpInst *CI1 = dyn_cast<CmpInst>(&const_cast<Instruction&>(I1))) + if (CI1->getPredicate() != cast<CmpInst>(I2)->getPredicate()) + return false; + // They are identical if both operands are the same! if (I1.getOperand(0) == I2->getOperand(0) && I1.getOperand(1) == I2->getOperand(1)) _______________________________________________ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits