ScalarEvolutionExpander.h [plain text]
#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
#define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
#include "llvm/Instructions.h"
#include "llvm/Type.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
namespace llvm {
struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
ScalarEvolution &SE;
LoopInfo &LI;
std::map<SCEVHandle, Value*> InsertedExpressions;
std::set<Instruction*> InsertedInstructions;
BasicBlock::iterator InsertPt;
friend struct SCEVVisitor<SCEVExpander, Value*>;
public:
SCEVExpander(ScalarEvolution &se, LoopInfo &li)
: SE(se), LI(li) {}
LoopInfo &getLoopInfo() const { return LI; }
void clear() { InsertedExpressions.clear(); }
bool isInsertedInstruction(Instruction *I) const {
return InsertedInstructions.count(I);
}
Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty){
assert(Ty->isInteger() && "Can only insert integer induction variables!");
SCEVHandle H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty),
SE.getIntegerSCEV(1, Ty), L);
return expand(H);
}
void addInsertedValue(Instruction *I, const SCEV *S) {
InsertedExpressions[S] = (Value*)I;
InsertedInstructions.insert(I);
}
void setInsertionPoint(BasicBlock::iterator NewIP) { InsertPt = NewIP; }
BasicBlock::iterator getInsertionPoint() const { return InsertPt; }
Value *expandCodeFor(SCEVHandle SH, const Type *Ty);
Value *expandCodeFor(SCEVHandle SH, const Type *Ty,
BasicBlock::iterator IP) {
setInsertionPoint(IP);
return expandCodeFor(SH, Ty);
}
Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V,
const Type *Ty);
Value *InsertNoopCastOfTo(Value *V, const Type *Ty);
static Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
Value *RHS, BasicBlock::iterator InsertPt);
private:
Value *expand(const SCEV *S);
Value *visitConstant(const SCEVConstant *S) {
return S->getValue();
}
Value *visitTruncateExpr(const SCEVTruncateExpr *S);
Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
Value *visitAddExpr(const SCEVAddExpr *S);
Value *visitMulExpr(const SCEVMulExpr *S);
Value *visitUDivExpr(const SCEVUDivExpr *S);
Value *visitAddRecExpr(const SCEVAddRecExpr *S);
Value *visitSMaxExpr(const SCEVSMaxExpr *S);
Value *visitUMaxExpr(const SCEVUMaxExpr *S);
Value *visitUnknown(const SCEVUnknown *S) {
return S->getValue();
}
};
}
#endif