Use more const qualifiers with SCEV interfaces.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69450 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2009-04-18 17:56:28 +00:00
parent f34c921713
commit 890f92b744
5 changed files with 73 additions and 72 deletions

View File

@ -69,7 +69,7 @@ namespace llvm {
/// addInsertedValue - Remember the specified instruction as being the /// addInsertedValue - Remember the specified instruction as being the
/// canonical form for the specified SCEV. /// canonical form for the specified SCEV.
void addInsertedValue(Instruction *I, SCEV *S) { void addInsertedValue(Instruction *I, const SCEV *S) {
InsertedExpressions[S] = (Value*)I; InsertedExpressions[S] = (Value*)I;
InsertedInstructions.insert(I); InsertedInstructions.insert(I);
} }
@ -90,31 +90,31 @@ namespace llvm {
static Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, static Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
Value *RHS, Instruction *InsertPt); Value *RHS, Instruction *InsertPt);
protected: protected:
Value *expand(SCEV *S); Value *expand(const SCEV *S);
Value *visitConstant(SCEVConstant *S) { Value *visitConstant(const SCEVConstant *S) {
return S->getValue(); return S->getValue();
} }
Value *visitTruncateExpr(SCEVTruncateExpr *S); Value *visitTruncateExpr(const SCEVTruncateExpr *S);
Value *visitZeroExtendExpr(SCEVZeroExtendExpr *S); Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
Value *visitSignExtendExpr(SCEVSignExtendExpr *S); Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
Value *visitAddExpr(SCEVAddExpr *S); Value *visitAddExpr(const SCEVAddExpr *S);
Value *visitMulExpr(SCEVMulExpr *S); Value *visitMulExpr(const SCEVMulExpr *S);
Value *visitUDivExpr(SCEVUDivExpr *S); Value *visitUDivExpr(const SCEVUDivExpr *S);
Value *visitAddRecExpr(SCEVAddRecExpr *S); Value *visitAddRecExpr(const SCEVAddRecExpr *S);
Value *visitSMaxExpr(SCEVSMaxExpr *S); Value *visitSMaxExpr(const SCEVSMaxExpr *S);
Value *visitUMaxExpr(SCEVUMaxExpr *S); Value *visitUMaxExpr(const SCEVUMaxExpr *S);
Value *visitUnknown(SCEVUnknown *S) { Value *visitUnknown(const SCEVUnknown *S) {
return S->getValue(); return S->getValue();
} }
}; };

View File

@ -552,39 +552,39 @@ namespace llvm {
/// for various SCEV analysis purposes. /// for various SCEV analysis purposes.
template<typename SC, typename RetVal=void> template<typename SC, typename RetVal=void>
struct SCEVVisitor { struct SCEVVisitor {
RetVal visit(SCEV *S) { RetVal visit(const SCEV *S) {
switch (S->getSCEVType()) { switch (S->getSCEVType()) {
case scConstant: case scConstant:
return ((SC*)this)->visitConstant((SCEVConstant*)S); return ((SC*)this)->visitConstant((const SCEVConstant*)S);
case scTruncate: case scTruncate:
return ((SC*)this)->visitTruncateExpr((SCEVTruncateExpr*)S); return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
case scZeroExtend: case scZeroExtend:
return ((SC*)this)->visitZeroExtendExpr((SCEVZeroExtendExpr*)S); return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
case scSignExtend: case scSignExtend:
return ((SC*)this)->visitSignExtendExpr((SCEVSignExtendExpr*)S); return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
case scAddExpr: case scAddExpr:
return ((SC*)this)->visitAddExpr((SCEVAddExpr*)S); return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
case scMulExpr: case scMulExpr:
return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S); return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
case scUDivExpr: case scUDivExpr:
return ((SC*)this)->visitUDivExpr((SCEVUDivExpr*)S); return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
case scAddRecExpr: case scAddRecExpr:
return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S); return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
case scSMaxExpr: case scSMaxExpr:
return ((SC*)this)->visitSMaxExpr((SCEVSMaxExpr*)S); return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
case scUMaxExpr: case scUMaxExpr:
return ((SC*)this)->visitUMaxExpr((SCEVUMaxExpr*)S); return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
case scUnknown: case scUnknown:
return ((SC*)this)->visitUnknown((SCEVUnknown*)S); return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
case scCouldNotCompute: case scCouldNotCompute:
return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S); return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
default: default:
assert(0 && "Unknown SCEV type!"); assert(0 && "Unknown SCEV type!");
abort(); abort();
} }
} }
RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) { RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
assert(0 && "Invalid use of SCEVCouldNotCompute!"); assert(0 && "Invalid use of SCEVCouldNotCompute!");
abort(); abort();
return RetVal(); return RetVal();

View File

@ -112,7 +112,7 @@ Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
return BinaryOperator::Create(Opcode, LHS, RHS, "tmp", InsertPt); return BinaryOperator::Create(Opcode, LHS, RHS, "tmp", InsertPt);
} }
Value *SCEVExpander::visitAddExpr(SCEVAddExpr *S) { Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
const Type *Ty = S->getType(); const Type *Ty = S->getType();
if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType();
Value *V = expand(S->getOperand(S->getNumOperands()-1)); Value *V = expand(S->getOperand(S->getNumOperands()-1));
@ -127,11 +127,11 @@ Value *SCEVExpander::visitAddExpr(SCEVAddExpr *S) {
return V; return V;
} }
Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) { Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) {
const Type *Ty = S->getType(); const Type *Ty = S->getType();
if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType();
int FirstOp = 0; // Set if we should emit a subtract. int FirstOp = 0; // Set if we should emit a subtract.
if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0)))
if (SC->getValue()->isAllOnesValue()) if (SC->getValue()->isAllOnesValue())
FirstOp = 1; FirstOp = 1;
@ -152,13 +152,13 @@ Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) {
return V; return V;
} }
Value *SCEVExpander::visitUDivExpr(SCEVUDivExpr *S) { Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
const Type *Ty = S->getType(); const Type *Ty = S->getType();
if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType();
Value *LHS = expand(S->getLHS()); Value *LHS = expand(S->getLHS());
LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty); LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty);
if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) { if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) {
const APInt &RHS = SC->getValue()->getValue(); const APInt &RHS = SC->getValue()->getValue();
if (RHS.isPowerOf2()) if (RHS.isPowerOf2())
return InsertBinop(Instruction::LShr, LHS, return InsertBinop(Instruction::LShr, LHS,
@ -171,7 +171,7 @@ Value *SCEVExpander::visitUDivExpr(SCEVUDivExpr *S) {
return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt); return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt);
} }
Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) { Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
const Type *Ty = S->getType(); const Type *Ty = S->getType();
const Loop *L = S->getLoop(); const Loop *L = S->getLoop();
// We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F} // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F}
@ -275,14 +275,14 @@ Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
return expand(V); return expand(V);
} }
Value *SCEVExpander::visitTruncateExpr(SCEVTruncateExpr *S) { Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) {
Value *V = expand(S->getOperand()); Value *V = expand(S->getOperand());
if (isa<PointerType>(V->getType())) if (isa<PointerType>(V->getType()))
V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType()); V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType());
return CastInst::CreateTruncOrBitCast(V, S->getType(), "tmp.", InsertPt); return CastInst::CreateTruncOrBitCast(V, S->getType(), "tmp.", InsertPt);
} }
Value *SCEVExpander::visitZeroExtendExpr(SCEVZeroExtendExpr *S) { Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) {
const Type *Ty = S->getType(); const Type *Ty = S->getType();
if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType();
Value *V = expand(S->getOperand()); Value *V = expand(S->getOperand());
@ -291,7 +291,7 @@ Value *SCEVExpander::visitZeroExtendExpr(SCEVZeroExtendExpr *S) {
return CastInst::CreateZExtOrBitCast(V, Ty, "tmp.", InsertPt); return CastInst::CreateZExtOrBitCast(V, Ty, "tmp.", InsertPt);
} }
Value *SCEVExpander::visitSignExtendExpr(SCEVSignExtendExpr *S) { Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) {
const Type *Ty = S->getType(); const Type *Ty = S->getType();
if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType();
Value *V = expand(S->getOperand()); Value *V = expand(S->getOperand());
@ -300,7 +300,7 @@ Value *SCEVExpander::visitSignExtendExpr(SCEVSignExtendExpr *S) {
return CastInst::CreateSExtOrBitCast(V, Ty, "tmp.", InsertPt); return CastInst::CreateSExtOrBitCast(V, Ty, "tmp.", InsertPt);
} }
Value *SCEVExpander::visitSMaxExpr(SCEVSMaxExpr *S) { Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) {
const Type *Ty = S->getType(); const Type *Ty = S->getType();
Value *LHS = expand(S->getOperand(0)); Value *LHS = expand(S->getOperand(0));
LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty); LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty);
@ -314,7 +314,7 @@ Value *SCEVExpander::visitSMaxExpr(SCEVSMaxExpr *S) {
return LHS; return LHS;
} }
Value *SCEVExpander::visitUMaxExpr(SCEVUMaxExpr *S) { Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
const Type *Ty = S->getType(); const Type *Ty = S->getType();
Value *LHS = expand(S->getOperand(0)); Value *LHS = expand(S->getOperand(0));
LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty); LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty);
@ -338,7 +338,7 @@ Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty,
return InsertCastOfTo(CastInst::getCastOpcode(V, false, Ty, false), V, Ty); return InsertCastOfTo(CastInst::getCastOpcode(V, false, Ty, false), V, Ty);
} }
Value *SCEVExpander::expand(SCEV *S) { Value *SCEVExpander::expand(const SCEV *S) {
// Check to see if we already expanded this. // Check to see if we already expanded this.
std::map<SCEVHandle, Value*>::iterator I = InsertedExpressions.find(S); std::map<SCEVHandle, Value*>::iterator I = InsertedExpressions.find(S);
if (I != InsertedExpressions.end()) if (I != InsertedExpressions.end())

View File

@ -98,7 +98,7 @@ namespace {
BasicBlock *ExitingBlock, BasicBlock *ExitingBlock,
BranchInst *BI, BranchInst *BI,
SCEVExpander &Rewriter); SCEVExpander &Rewriter);
void RewriteLoopExitValues(Loop *L, SCEV *BackedgeTakenCount); void RewriteLoopExitValues(Loop *L, const SCEV *BackedgeTakenCount);
void DeleteTriviallyDeadInstructions(SmallPtrSet<Instruction*, 16> &Insts); void DeleteTriviallyDeadInstructions(SmallPtrSet<Instruction*, 16> &Insts);
@ -211,7 +211,8 @@ void IndVarSimplify::LinearFunctionTestReplace(Loop *L,
/// final value of any expressions that are recurrent in the loop, and /// final value of any expressions that are recurrent in the loop, and
/// substitute the exit values from the loop into any instructions outside of /// substitute the exit values from the loop into any instructions outside of
/// the loop that use the final values of the current expressions. /// the loop that use the final values of the current expressions.
void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEV *BackedgeTakenCount) { void IndVarSimplify::RewriteLoopExitValues(Loop *L,
const SCEV *BackedgeTakenCount) {
BasicBlock *Preheader = L->getLoopPreheader(); BasicBlock *Preheader = L->getLoopPreheader();
// Scan all of the instructions in the loop, looking at those that have // Scan all of the instructions in the loop, looking at those that have
@ -627,7 +628,7 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
// polynomial evaluations inside of the loop, and the str reduction pass // polynomial evaluations inside of the loop, and the str reduction pass
// currently can only reduce affine polynomials. For now just disable // currently can only reduce affine polynomials. For now just disable
// indvar subst on anything more complex than an affine addrec. // indvar subst on anything more complex than an affine addrec.
if (SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SCEV)) if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SCEV))
if (AR->getLoop() == L && AR->isAffine()) if (AR->getLoop() == L && AR->isAffine())
IndVars.push_back(std::make_pair(PN, SCEV)); IndVars.push_back(std::make_pair(PN, SCEV));
} }
@ -716,7 +717,7 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
// variable. // variable.
while (!IndVars.empty()) { while (!IndVars.empty()) {
PHINode *PN = IndVars.back().first; PHINode *PN = IndVars.back().first;
SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(IndVars.back().second); const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(IndVars.back().second);
Value *NewVal = Rewriter.expandCodeFor(AR, PN->getType(), InsertPt); Value *NewVal = Rewriter.expandCodeFor(AR, PN->getType(), InsertPt);
DOUT << "INDVARS: Rewrote IV '" << *AR << "' " << *PN DOUT << "INDVARS: Rewrote IV '" << *AR << "' " << *PN
<< " into = " << *NewVal << "\n"; << " into = " << *NewVal << "\n";

View File

@ -278,13 +278,13 @@ static bool containsAddRecFromDifferentLoop(SCEVHandle S, Loop *L) {
// This is very common, put it first. // This is very common, put it first.
if (isa<SCEVConstant>(S)) if (isa<SCEVConstant>(S))
return false; return false;
if (SCEVCommutativeExpr *AE = dyn_cast<SCEVCommutativeExpr>(S)) { if (const SCEVCommutativeExpr *AE = dyn_cast<SCEVCommutativeExpr>(S)) {
for (unsigned int i=0; i< AE->getNumOperands(); i++) for (unsigned int i=0; i< AE->getNumOperands(); i++)
if (containsAddRecFromDifferentLoop(AE->getOperand(i), L)) if (containsAddRecFromDifferentLoop(AE->getOperand(i), L))
return true; return true;
return false; return false;
} }
if (SCEVAddRecExpr *AE = dyn_cast<SCEVAddRecExpr>(S)) { if (const SCEVAddRecExpr *AE = dyn_cast<SCEVAddRecExpr>(S)) {
if (const Loop *newLoop = AE->getLoop()) { if (const Loop *newLoop = AE->getLoop()) {
if (newLoop == L) if (newLoop == L)
return false; return false;
@ -294,21 +294,21 @@ static bool containsAddRecFromDifferentLoop(SCEVHandle S, Loop *L) {
} }
return true; return true;
} }
if (SCEVUDivExpr *DE = dyn_cast<SCEVUDivExpr>(S)) if (const SCEVUDivExpr *DE = dyn_cast<SCEVUDivExpr>(S))
return containsAddRecFromDifferentLoop(DE->getLHS(), L) || return containsAddRecFromDifferentLoop(DE->getLHS(), L) ||
containsAddRecFromDifferentLoop(DE->getRHS(), L); containsAddRecFromDifferentLoop(DE->getRHS(), L);
#if 0 #if 0
// SCEVSDivExpr has been backed out temporarily, but will be back; we'll // SCEVSDivExpr has been backed out temporarily, but will be back; we'll
// need this when it is. // need this when it is.
if (SCEVSDivExpr *DE = dyn_cast<SCEVSDivExpr>(S)) if (const SCEVSDivExpr *DE = dyn_cast<SCEVSDivExpr>(S))
return containsAddRecFromDifferentLoop(DE->getLHS(), L) || return containsAddRecFromDifferentLoop(DE->getLHS(), L) ||
containsAddRecFromDifferentLoop(DE->getRHS(), L); containsAddRecFromDifferentLoop(DE->getRHS(), L);
#endif #endif
if (SCEVTruncateExpr *TE = dyn_cast<SCEVTruncateExpr>(S)) if (const SCEVTruncateExpr *TE = dyn_cast<SCEVTruncateExpr>(S))
return containsAddRecFromDifferentLoop(TE->getOperand(), L); return containsAddRecFromDifferentLoop(TE->getOperand(), L);
if (SCEVZeroExtendExpr *ZE = dyn_cast<SCEVZeroExtendExpr>(S)) if (const SCEVZeroExtendExpr *ZE = dyn_cast<SCEVZeroExtendExpr>(S))
return containsAddRecFromDifferentLoop(ZE->getOperand(), L); return containsAddRecFromDifferentLoop(ZE->getOperand(), L);
if (SCEVSignExtendExpr *SE = dyn_cast<SCEVSignExtendExpr>(S)) if (const SCEVSignExtendExpr *SE = dyn_cast<SCEVSignExtendExpr>(S))
return containsAddRecFromDifferentLoop(SE->getOperand(), L); return containsAddRecFromDifferentLoop(SE->getOperand(), L);
return false; return false;
} }
@ -326,7 +326,7 @@ static bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
// If the outer level is an AddExpr, the operands are all start values except // If the outer level is an AddExpr, the operands are all start values except
// for a nested AddRecExpr. // for a nested AddRecExpr.
if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) { if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) {
for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i)
if (SCEVAddRecExpr *AddRec = if (SCEVAddRecExpr *AddRec =
dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) { dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) {
@ -344,7 +344,7 @@ static bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
return false; // not analyzable. return false; // not analyzable.
} }
SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec); const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec);
if (!AddRec || AddRec->getLoop() != L) return false; if (!AddRec || AddRec->getLoop() != L) return false;
// FIXME: Generalize to non-affine IV's. // FIXME: Generalize to non-affine IV's.
@ -787,7 +787,7 @@ void BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
/// mode, and does not need to be put in a register first. /// mode, and does not need to be put in a register first.
static bool fitsInAddressMode(const SCEVHandle &V, const Type *UseTy, static bool fitsInAddressMode(const SCEVHandle &V, const Type *UseTy,
const TargetLowering *TLI, bool HasBaseReg) { const TargetLowering *TLI, bool HasBaseReg) {
if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) { if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) {
int64_t VC = SC->getValue()->getSExtValue(); int64_t VC = SC->getValue()->getSExtValue();
if (TLI) { if (TLI) {
TargetLowering::AddrMode AM; TargetLowering::AddrMode AM;
@ -800,7 +800,7 @@ static bool fitsInAddressMode(const SCEVHandle &V, const Type *UseTy,
} }
} }
if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V))
if (GlobalValue *GV = dyn_cast<GlobalValue>(SU->getValue())) { if (GlobalValue *GV = dyn_cast<GlobalValue>(SU->getValue())) {
TargetLowering::AddrMode AM; TargetLowering::AddrMode AM;
AM.BaseGV = GV; AM.BaseGV = GV;
@ -817,7 +817,7 @@ static void MoveLoopVariantsToImmediateField(SCEVHandle &Val, SCEVHandle &Imm,
Loop *L, ScalarEvolution *SE) { Loop *L, ScalarEvolution *SE) {
if (Val->isLoopInvariant(L)) return; // Nothing to do. if (Val->isLoopInvariant(L)) return; // Nothing to do.
if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) { if (const SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
std::vector<SCEVHandle> NewOps; std::vector<SCEVHandle> NewOps;
NewOps.reserve(SAE->getNumOperands()); NewOps.reserve(SAE->getNumOperands());
@ -834,7 +834,7 @@ static void MoveLoopVariantsToImmediateField(SCEVHandle &Val, SCEVHandle &Imm,
Val = SE->getIntegerSCEV(0, Val->getType()); Val = SE->getIntegerSCEV(0, Val->getType());
else else
Val = SE->getAddExpr(NewOps); Val = SE->getAddExpr(NewOps);
} else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) { } else if (const SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
// Try to pull immediates out of the start value of nested addrec's. // Try to pull immediates out of the start value of nested addrec's.
SCEVHandle Start = SARE->getStart(); SCEVHandle Start = SARE->getStart();
MoveLoopVariantsToImmediateField(Start, Imm, L, SE); MoveLoopVariantsToImmediateField(Start, Imm, L, SE);
@ -858,7 +858,7 @@ static void MoveImmediateValues(const TargetLowering *TLI,
SCEVHandle &Val, SCEVHandle &Imm, SCEVHandle &Val, SCEVHandle &Imm,
bool isAddress, Loop *L, bool isAddress, Loop *L,
ScalarEvolution *SE) { ScalarEvolution *SE) {
if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) { if (const SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
std::vector<SCEVHandle> NewOps; std::vector<SCEVHandle> NewOps;
NewOps.reserve(SAE->getNumOperands()); NewOps.reserve(SAE->getNumOperands());
@ -880,7 +880,7 @@ static void MoveImmediateValues(const TargetLowering *TLI,
else else
Val = SE->getAddExpr(NewOps); Val = SE->getAddExpr(NewOps);
return; return;
} else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) { } else if (const SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
// Try to pull immediates out of the start value of nested addrec's. // Try to pull immediates out of the start value of nested addrec's.
SCEVHandle Start = SARE->getStart(); SCEVHandle Start = SARE->getStart();
MoveImmediateValues(TLI, UseTy, Start, Imm, isAddress, L, SE); MoveImmediateValues(TLI, UseTy, Start, Imm, isAddress, L, SE);
@ -891,7 +891,7 @@ static void MoveImmediateValues(const TargetLowering *TLI,
Val = SE->getAddRecExpr(Ops, SARE->getLoop()); Val = SE->getAddRecExpr(Ops, SARE->getLoop());
} }
return; return;
} else if (SCEVMulExpr *SME = dyn_cast<SCEVMulExpr>(Val)) { } else if (const SCEVMulExpr *SME = dyn_cast<SCEVMulExpr>(Val)) {
// Transform "8 * (4 + v)" -> "32 + 8*V" if "32" fits in the immed field. // Transform "8 * (4 + v)" -> "32 + 8*V" if "32" fits in the immed field.
if (isAddress && fitsInAddressMode(SME->getOperand(0), UseTy, TLI, false) && if (isAddress && fitsInAddressMode(SME->getOperand(0), UseTy, TLI, false) &&
SME->getNumOperands() == 2 && SME->isLoopInvariant(L)) { SME->getNumOperands() == 2 && SME->isLoopInvariant(L)) {
@ -945,10 +945,10 @@ static void MoveImmediateValues(const TargetLowering *TLI,
static void SeparateSubExprs(std::vector<SCEVHandle> &SubExprs, static void SeparateSubExprs(std::vector<SCEVHandle> &SubExprs,
SCEVHandle Expr, SCEVHandle Expr,
ScalarEvolution *SE) { ScalarEvolution *SE) {
if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(Expr)) { if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(Expr)) {
for (unsigned j = 0, e = AE->getNumOperands(); j != e; ++j) for (unsigned j = 0, e = AE->getNumOperands(); j != e; ++j)
SeparateSubExprs(SubExprs, AE->getOperand(j), SE); SeparateSubExprs(SubExprs, AE->getOperand(j), SE);
} else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Expr)) { } else if (const SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Expr)) {
SCEVHandle Zero = SE->getIntegerSCEV(0, Expr->getType()); SCEVHandle Zero = SE->getIntegerSCEV(0, Expr->getType());
if (SARE->getOperand(0) == Zero) { if (SARE->getOperand(0) == Zero) {
SubExprs.push_back(Expr); SubExprs.push_back(Expr);
@ -1156,7 +1156,7 @@ bool LoopStrengthReduce::ValidStride(bool HasBaseReg,
continue; continue;
TargetLowering::AddrMode AM; TargetLowering::AddrMode AM;
if (SCEVConstant *SC = dyn_cast<SCEVConstant>(UsersToProcess[i].Imm)) if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(UsersToProcess[i].Imm))
AM.BaseOffs = SC->getValue()->getSExtValue(); AM.BaseOffs = SC->getValue()->getSExtValue();
AM.HasBaseReg = HasBaseReg || !UsersToProcess[i].Base->isZero(); AM.HasBaseReg = HasBaseReg || !UsersToProcess[i].Base->isZero();
AM.Scale = Scale; AM.Scale = Scale;
@ -1201,7 +1201,7 @@ SCEVHandle LoopStrengthReduce::CheckForIVReuse(bool HasBaseReg,
const SCEVHandle &Stride, const SCEVHandle &Stride,
IVExpr &IV, const Type *Ty, IVExpr &IV, const Type *Ty,
const std::vector<BasedUser>& UsersToProcess) { const std::vector<BasedUser>& UsersToProcess) {
if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Stride)) { if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Stride)) {
int64_t SInt = SC->getValue()->getSExtValue(); int64_t SInt = SC->getValue()->getSExtValue();
for (unsigned NewStride = 0, e = StrideOrder.size(); NewStride != e; for (unsigned NewStride = 0, e = StrideOrder.size(); NewStride != e;
++NewStride) { ++NewStride) {
@ -1261,8 +1261,8 @@ SCEVHandle LoopStrengthReduce::CheckForIVReuse(bool HasBaseReg,
IVsByStride.find(StrideOrder[NewStride]); IVsByStride.find(StrideOrder[NewStride]);
if (SI == IVsByStride.end()) if (SI == IVsByStride.end())
continue; continue;
if (SCEVMulExpr *ME = dyn_cast<SCEVMulExpr>(SI->first)) if (const SCEVMulExpr *ME = dyn_cast<SCEVMulExpr>(SI->first))
if (SCEVConstant *SC = dyn_cast<SCEVConstant>(ME->getOperand(0))) if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(ME->getOperand(0)))
if (Stride == ME->getOperand(1) && if (Stride == ME->getOperand(1) &&
SC->getValue()->getSExtValue() == -1LL) SC->getValue()->getSExtValue() == -1LL)
for (std::vector<IVExpr>::iterator II = SI->second.IVs.begin(), for (std::vector<IVExpr>::iterator II = SI->second.IVs.begin(),
@ -1287,11 +1287,11 @@ static bool PartitionByIsUseOfPostIncrementedValue(const BasedUser &Val) {
/// isNonConstantNegative - Return true if the specified scev is negated, but /// isNonConstantNegative - Return true if the specified scev is negated, but
/// not a constant. /// not a constant.
static bool isNonConstantNegative(const SCEVHandle &Expr) { static bool isNonConstantNegative(const SCEVHandle &Expr) {
SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Expr); const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Expr);
if (!Mul) return false; if (!Mul) return false;
// If there is a constant factor, it will be first. // If there is a constant factor, it will be first.
SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0)); const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
if (!SC) return false; if (!SC) return false;
// Return true if the value is negative, this matches things like (-42 * V). // Return true if the value is negative, this matches things like (-42 * V).
@ -1711,10 +1711,10 @@ void LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
// If the immediate part of the common expression is a GV, check if it's // If the immediate part of the common expression is a GV, check if it's
// possible to fold it into the target addressing mode. // possible to fold it into the target addressing mode.
GlobalValue *GV = 0; GlobalValue *GV = 0;
if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(Imm)) if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(Imm))
GV = dyn_cast<GlobalValue>(SU->getValue()); GV = dyn_cast<GlobalValue>(SU->getValue());
int64_t Offset = 0; int64_t Offset = 0;
if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Imm)) if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Imm))
Offset = SC->getValue()->getSExtValue(); Offset = SC->getValue()->getSExtValue();
if (GV || Offset) if (GV || Offset)
// Pass VoidTy as the AccessTy to be conservative, because // Pass VoidTy as the AccessTy to be conservative, because
@ -1963,8 +1963,8 @@ namespace {
explicit StrideCompare(const TargetData *td) : TD(td) {} explicit StrideCompare(const TargetData *td) : TD(td) {}
bool operator()(const SCEVHandle &LHS, const SCEVHandle &RHS) { bool operator()(const SCEVHandle &LHS, const SCEVHandle &RHS) {
SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS); const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS);
SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS); const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS);
if (LHSC && RHSC) { if (LHSC && RHSC) {
int64_t LV = LHSC->getValue()->getSExtValue(); int64_t LV = LHSC->getValue()->getSExtValue();
int64_t RV = RHSC->getValue()->getSExtValue(); int64_t RV = RHSC->getValue()->getSExtValue();
@ -2239,7 +2239,7 @@ ICmpInst *LoopStrengthReduce::OptimizeSMax(Loop *L, ICmpInst *Cond,
// Check the relevant induction variable for conformance to // Check the relevant induction variable for conformance to
// the pattern. // the pattern.
SCEVHandle IV = SE->getSCEV(Cond->getOperand(0)); SCEVHandle IV = SE->getSCEV(Cond->getOperand(0));
SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(IV); const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(IV);
if (!AR || !AR->isAffine() || if (!AR || !AR->isAffine() ||
AR->getStart() != One || AR->getStart() != One ||
AR->getStepRecurrence(*SE) != One) AR->getStepRecurrence(*SE) != One)