1 //===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the classes used to generate code from scalar expressions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
15 #define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
17 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
18 #include "llvm/Support/IRBuilder.h"
19 #include "llvm/Support/TargetFolder.h"
23 /// SCEVExpander - This class uses information about analyze scalars to
24 /// rewrite expressions in canonical form.
26 /// Clients should create an instance of this class when rewriting is needed,
27 /// and destroy it when finished to allow the release of the associated
29 struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
31 std::map<std::pair<const SCEV *, Instruction *>, AssertingVH<Value> >
33 std::set<Value*> InsertedValues;
35 typedef IRBuilder<true, TargetFolder> BuilderType;
38 friend struct SCEVVisitor<SCEVExpander, Value*>;
40 explicit SCEVExpander(ScalarEvolution &se)
41 : SE(se), Builder(se.getContext(),
42 TargetFolder(se.TD, se.getContext())) {}
44 /// clear - Erase the contents of the InsertedExpressions map so that users
45 /// trying to expand the same expression into multiple BasicBlocks or
46 /// different places within the same BasicBlock can do so.
47 void clear() { InsertedExpressions.clear(); }
49 /// getOrInsertCanonicalInductionVariable - This method returns the
50 /// canonical induction variable of the specified type for the specified
51 /// loop (inserting one if there is none). A canonical induction variable
52 /// starts at zero and steps by one on each iteration.
53 Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty);
55 /// expandCodeFor - Insert code to directly compute the specified SCEV
56 /// expression into the program. The inserted code is inserted into the
58 Value *expandCodeFor(const SCEV *SH, const Type *Ty, Instruction *IP) {
59 Builder.SetInsertPoint(IP->getParent(), IP);
60 return expandCodeFor(SH, Ty);
64 LLVMContext &getContext() const { return SE.getContext(); }
66 /// InsertBinop - Insert the specified binary operator, doing a small amount
67 /// of work to avoid inserting an obviously redundant operation.
68 Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS);
70 /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
71 /// which must be possible with a noop cast, doing what we can to
73 Value *InsertNoopCastOfTo(Value *V, const Type *Ty);
75 /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
76 /// instead of using ptrtoint+arithmetic+inttoptr.
77 Value *expandAddToGEP(const SCEV *const *op_begin,
78 const SCEV *const *op_end,
79 const PointerType *PTy, const Type *Ty, Value *V);
81 Value *expand(const SCEV *S);
83 /// expandCodeFor - Insert code to directly compute the specified SCEV
84 /// expression into the program. The inserted code is inserted into the
85 /// SCEVExpander's current insertion point. If a type is specified, the
86 /// result will be expanded to have that type, with a cast if necessary.
87 Value *expandCodeFor(const SCEV *SH, const Type *Ty = 0);
89 /// isInsertedInstruction - Return true if the specified instruction was
90 /// inserted by the code rewriter. If so, the client should not modify the
92 bool isInsertedInstruction(Instruction *I) const {
93 return InsertedValues.count(I);
96 Value *visitConstant(const SCEVConstant *S) {
100 Value *visitTruncateExpr(const SCEVTruncateExpr *S);
102 Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
104 Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
106 Value *visitAddExpr(const SCEVAddExpr *S);
108 Value *visitMulExpr(const SCEVMulExpr *S);
110 Value *visitUDivExpr(const SCEVUDivExpr *S);
112 Value *visitAddRecExpr(const SCEVAddRecExpr *S);
114 Value *visitSMaxExpr(const SCEVSMaxExpr *S);
116 Value *visitUMaxExpr(const SCEVUMaxExpr *S);
118 Value *visitFieldOffsetExpr(const SCEVFieldOffsetExpr *S);
120 Value *visitAllocSizeExpr(const SCEVAllocSizeExpr *S);
122 Value *visitUnknown(const SCEVUnknown *S) {
123 return S->getValue();