LLVM API Documentation
00001 //===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the classes used to generate code from scalar expressions. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H 00015 #define LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H 00016 00017 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 00018 #include "llvm/Analysis/ScalarEvolutionNormalization.h" 00019 #include "llvm/Analysis/TargetFolder.h" 00020 #include "llvm/IR/IRBuilder.h" 00021 #include "llvm/IR/ValueHandle.h" 00022 #include <set> 00023 00024 namespace llvm { 00025 class TargetTransformInfo; 00026 00027 /// Return true if the given expression is safe to expand in the sense that 00028 /// all materialized values are safe to speculate. 00029 bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE); 00030 00031 /// SCEVExpander - This class uses information about analyze scalars to 00032 /// rewrite expressions in canonical form. 00033 /// 00034 /// Clients should create an instance of this class when rewriting is needed, 00035 /// and destroy it when finished to allow the release of the associated 00036 /// memory. 00037 class SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> { 00038 ScalarEvolution &SE; 00039 00040 // New instructions receive a name to identifies them with the current pass. 00041 const char* IVName; 00042 00043 // InsertedExpressions caches Values for reuse, so must track RAUW. 00044 std::map<std::pair<const SCEV *, Instruction *>, TrackingVH<Value> > 00045 InsertedExpressions; 00046 // InsertedValues only flags inserted instructions so needs no RAUW. 00047 std::set<AssertingVH<Value> > InsertedValues; 00048 std::set<AssertingVH<Value> > InsertedPostIncValues; 00049 00050 /// RelevantLoops - A memoization of the "relevant" loop for a given SCEV. 00051 DenseMap<const SCEV *, const Loop *> RelevantLoops; 00052 00053 /// PostIncLoops - Addrecs referring to any of the given loops are expanded 00054 /// in post-inc mode. For example, expanding {1,+,1}<L> in post-inc mode 00055 /// returns the add instruction that adds one to the phi for {0,+,1}<L>, 00056 /// as opposed to a new phi starting at 1. This is only supported in 00057 /// non-canonical mode. 00058 PostIncLoopSet PostIncLoops; 00059 00060 /// IVIncInsertPos - When this is non-null, addrecs expanded in the 00061 /// loop it indicates should be inserted with increments at 00062 /// IVIncInsertPos. 00063 const Loop *IVIncInsertLoop; 00064 00065 /// IVIncInsertPos - When expanding addrecs in the IVIncInsertLoop loop, 00066 /// insert the IV increment at this position. 00067 Instruction *IVIncInsertPos; 00068 00069 /// Phis that complete an IV chain. Reuse 00070 std::set<AssertingVH<PHINode> > ChainedPhis; 00071 00072 /// CanonicalMode - When true, expressions are expanded in "canonical" 00073 /// form. In particular, addrecs are expanded as arithmetic based on 00074 /// a canonical induction variable. When false, expression are expanded 00075 /// in a more literal form. 00076 bool CanonicalMode; 00077 00078 /// When invoked from LSR, the expander is in "strength reduction" mode. The 00079 /// only difference is that phi's are only reused if they are already in 00080 /// "expanded" form. 00081 bool LSRMode; 00082 00083 typedef IRBuilder<true, TargetFolder> BuilderType; 00084 BuilderType Builder; 00085 00086 #ifndef NDEBUG 00087 const char *DebugType; 00088 #endif 00089 00090 friend struct SCEVVisitor<SCEVExpander, Value*>; 00091 00092 public: 00093 /// SCEVExpander - Construct a SCEVExpander in "canonical" mode. 00094 explicit SCEVExpander(ScalarEvolution &se, const char *name) 00095 : SE(se), IVName(name), IVIncInsertLoop(nullptr), IVIncInsertPos(nullptr), 00096 CanonicalMode(true), LSRMode(false), 00097 Builder(se.getContext(), TargetFolder(se.DL)) { 00098 #ifndef NDEBUG 00099 DebugType = ""; 00100 #endif 00101 } 00102 00103 #ifndef NDEBUG 00104 void setDebugType(const char* s) { DebugType = s; } 00105 #endif 00106 00107 /// clear - Erase the contents of the InsertedExpressions map so that users 00108 /// trying to expand the same expression into multiple BasicBlocks or 00109 /// different places within the same BasicBlock can do so. 00110 void clear() { 00111 InsertedExpressions.clear(); 00112 InsertedValues.clear(); 00113 InsertedPostIncValues.clear(); 00114 ChainedPhis.clear(); 00115 } 00116 00117 /// getOrInsertCanonicalInductionVariable - This method returns the 00118 /// canonical induction variable of the specified type for the specified 00119 /// loop (inserting one if there is none). A canonical induction variable 00120 /// starts at zero and steps by one on each iteration. 00121 PHINode *getOrInsertCanonicalInductionVariable(const Loop *L, Type *Ty); 00122 00123 /// getIVIncOperand - Return the induction variable increment's IV operand. 00124 Instruction *getIVIncOperand(Instruction *IncV, Instruction *InsertPos, 00125 bool allowScale); 00126 00127 /// hoistIVInc - Utility for hoisting an IV increment. 00128 bool hoistIVInc(Instruction *IncV, Instruction *InsertPos); 00129 00130 /// replaceCongruentIVs - replace congruent phis with their most canonical 00131 /// representative. Return the number of phis eliminated. 00132 unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT, 00133 SmallVectorImpl<WeakVH> &DeadInsts, 00134 const TargetTransformInfo *TTI = nullptr); 00135 00136 /// expandCodeFor - Insert code to directly compute the specified SCEV 00137 /// expression into the program. The inserted code is inserted into the 00138 /// specified block. 00139 Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I); 00140 00141 /// setIVIncInsertPos - Set the current IV increment loop and position. 00142 void setIVIncInsertPos(const Loop *L, Instruction *Pos) { 00143 assert(!CanonicalMode && 00144 "IV increment positions are not supported in CanonicalMode"); 00145 IVIncInsertLoop = L; 00146 IVIncInsertPos = Pos; 00147 } 00148 00149 /// setPostInc - Enable post-inc expansion for addrecs referring to the 00150 /// given loops. Post-inc expansion is only supported in non-canonical 00151 /// mode. 00152 void setPostInc(const PostIncLoopSet &L) { 00153 assert(!CanonicalMode && 00154 "Post-inc expansion is not supported in CanonicalMode"); 00155 PostIncLoops = L; 00156 } 00157 00158 /// clearPostInc - Disable all post-inc expansion. 00159 void clearPostInc() { 00160 PostIncLoops.clear(); 00161 00162 // When we change the post-inc loop set, cached expansions may no 00163 // longer be valid. 00164 InsertedPostIncValues.clear(); 00165 } 00166 00167 /// disableCanonicalMode - Disable the behavior of expanding expressions in 00168 /// canonical form rather than in a more literal form. Non-canonical mode 00169 /// is useful for late optimization passes. 00170 void disableCanonicalMode() { CanonicalMode = false; } 00171 00172 void enableLSRMode() { LSRMode = true; } 00173 00174 /// clearInsertPoint - Clear the current insertion point. This is useful 00175 /// if the instruction that had been serving as the insertion point may 00176 /// have been deleted. 00177 void clearInsertPoint() { 00178 Builder.ClearInsertionPoint(); 00179 } 00180 00181 /// isInsertedInstruction - Return true if the specified instruction was 00182 /// inserted by the code rewriter. If so, the client should not modify the 00183 /// instruction. 00184 bool isInsertedInstruction(Instruction *I) const { 00185 return InsertedValues.count(I) || InsertedPostIncValues.count(I); 00186 } 00187 00188 void setChainedPhi(PHINode *PN) { ChainedPhis.insert(PN); } 00189 00190 private: 00191 LLVMContext &getContext() const { return SE.getContext(); } 00192 00193 /// InsertBinop - Insert the specified binary operator, doing a small amount 00194 /// of work to avoid inserting an obviously redundant operation. 00195 Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS); 00196 00197 /// ReuseOrCreateCast - Arange for there to be a cast of V to Ty at IP, 00198 /// reusing an existing cast if a suitable one exists, moving an existing 00199 /// cast if a suitable one exists but isn't in the right place, or 00200 /// or creating a new one. 00201 Value *ReuseOrCreateCast(Value *V, Type *Ty, 00202 Instruction::CastOps Op, 00203 BasicBlock::iterator IP); 00204 00205 /// InsertNoopCastOfTo - Insert a cast of V to the specified type, 00206 /// which must be possible with a noop cast, doing what we can to 00207 /// share the casts. 00208 Value *InsertNoopCastOfTo(Value *V, Type *Ty); 00209 00210 /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP 00211 /// instead of using ptrtoint+arithmetic+inttoptr. 00212 Value *expandAddToGEP(const SCEV *const *op_begin, 00213 const SCEV *const *op_end, 00214 PointerType *PTy, Type *Ty, Value *V); 00215 00216 Value *expand(const SCEV *S); 00217 00218 /// expandCodeFor - Insert code to directly compute the specified SCEV 00219 /// expression into the program. The inserted code is inserted into the 00220 /// SCEVExpander's current insertion point. If a type is specified, the 00221 /// result will be expanded to have that type, with a cast if necessary. 00222 Value *expandCodeFor(const SCEV *SH, Type *Ty = nullptr); 00223 00224 /// getRelevantLoop - Determine the most "relevant" loop for the given SCEV. 00225 const Loop *getRelevantLoop(const SCEV *); 00226 00227 Value *visitConstant(const SCEVConstant *S) { 00228 return S->getValue(); 00229 } 00230 00231 Value *visitTruncateExpr(const SCEVTruncateExpr *S); 00232 00233 Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S); 00234 00235 Value *visitSignExtendExpr(const SCEVSignExtendExpr *S); 00236 00237 Value *visitAddExpr(const SCEVAddExpr *S); 00238 00239 Value *visitMulExpr(const SCEVMulExpr *S); 00240 00241 Value *visitUDivExpr(const SCEVUDivExpr *S); 00242 00243 Value *visitAddRecExpr(const SCEVAddRecExpr *S); 00244 00245 Value *visitSMaxExpr(const SCEVSMaxExpr *S); 00246 00247 Value *visitUMaxExpr(const SCEVUMaxExpr *S); 00248 00249 Value *visitUnknown(const SCEVUnknown *S) { 00250 return S->getValue(); 00251 } 00252 00253 void rememberInstruction(Value *I); 00254 00255 bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L); 00256 00257 bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L); 00258 00259 Value *expandAddRecExprLiterally(const SCEVAddRecExpr *); 00260 PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized, 00261 const Loop *L, 00262 Type *ExpandTy, 00263 Type *IntTy, 00264 Type *&TruncTy, 00265 bool &InvertStep); 00266 Value *expandIVInc(PHINode *PN, Value *StepV, const Loop *L, 00267 Type *ExpandTy, Type *IntTy, bool useSubtract); 00268 }; 00269 } 00270 00271 #endif