LLVM API Documentation
00001 //===- ScalarEvolutionNormalization.cpp - See below -------------*- 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 implements utilities for working with "normalized" expressions. 00011 // See the comments at the top of ScalarEvolutionNormalization.h for details. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Analysis/Dominators.h" 00016 #include "llvm/Analysis/LoopInfo.h" 00017 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 00018 #include "llvm/Analysis/ScalarEvolutionNormalization.h" 00019 using namespace llvm; 00020 00021 /// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression 00022 /// and now we need to decide whether the user should use the preinc or post-inc 00023 /// value. If this user should use the post-inc version of the IV, return true. 00024 /// 00025 /// Choosing wrong here can break dominance properties (if we choose to use the 00026 /// post-inc value when we cannot) or it can end up adding extra live-ranges to 00027 /// the loop, resulting in reg-reg copies (if we use the pre-inc value when we 00028 /// should use the post-inc value). 00029 static bool IVUseShouldUsePostIncValue(Instruction *User, Value *Operand, 00030 const Loop *L, DominatorTree *DT) { 00031 // If the user is in the loop, use the preinc value. 00032 if (L->contains(User)) return false; 00033 00034 BasicBlock *LatchBlock = L->getLoopLatch(); 00035 if (!LatchBlock) 00036 return false; 00037 00038 // Ok, the user is outside of the loop. If it is dominated by the latch 00039 // block, use the post-inc value. 00040 if (DT->dominates(LatchBlock, User->getParent())) 00041 return true; 00042 00043 // There is one case we have to be careful of: PHI nodes. These little guys 00044 // can live in blocks that are not dominated by the latch block, but (since 00045 // their uses occur in the predecessor block, not the block the PHI lives in) 00046 // should still use the post-inc value. Check for this case now. 00047 PHINode *PN = dyn_cast<PHINode>(User); 00048 if (!PN || !Operand) return false; // not a phi, not dominated by latch block. 00049 00050 // Look at all of the uses of Operand by the PHI node. If any use corresponds 00051 // to a block that is not dominated by the latch block, give up and use the 00052 // preincremented value. 00053 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 00054 if (PN->getIncomingValue(i) == Operand && 00055 !DT->dominates(LatchBlock, PN->getIncomingBlock(i))) 00056 return false; 00057 00058 // Okay, all uses of Operand by PN are in predecessor blocks that really are 00059 // dominated by the latch block. Use the post-incremented value. 00060 return true; 00061 } 00062 00063 namespace { 00064 00065 /// Hold the state used during post-inc expression transformation, including a 00066 /// map of transformed expressions. 00067 class PostIncTransform { 00068 TransformKind Kind; 00069 PostIncLoopSet &Loops; 00070 ScalarEvolution &SE; 00071 DominatorTree &DT; 00072 00073 DenseMap<const SCEV*, const SCEV*> Transformed; 00074 00075 public: 00076 PostIncTransform(TransformKind kind, PostIncLoopSet &loops, 00077 ScalarEvolution &se, DominatorTree &dt): 00078 Kind(kind), Loops(loops), SE(se), DT(dt) {} 00079 00080 const SCEV *TransformSubExpr(const SCEV *S, Instruction *User, 00081 Value *OperandValToReplace); 00082 00083 protected: 00084 const SCEV *TransformImpl(const SCEV *S, Instruction *User, 00085 Value *OperandValToReplace); 00086 }; 00087 00088 } // namespace 00089 00090 /// Implement post-inc transformation for all valid expression types. 00091 const SCEV *PostIncTransform:: 00092 TransformImpl(const SCEV *S, Instruction *User, Value *OperandValToReplace) { 00093 00094 if (const SCEVCastExpr *X = dyn_cast<SCEVCastExpr>(S)) { 00095 const SCEV *O = X->getOperand(); 00096 const SCEV *N = TransformSubExpr(O, User, OperandValToReplace); 00097 if (O != N) 00098 switch (S->getSCEVType()) { 00099 case scZeroExtend: return SE.getZeroExtendExpr(N, S->getType()); 00100 case scSignExtend: return SE.getSignExtendExpr(N, S->getType()); 00101 case scTruncate: return SE.getTruncateExpr(N, S->getType()); 00102 default: llvm_unreachable("Unexpected SCEVCastExpr kind!"); 00103 } 00104 return S; 00105 } 00106 00107 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { 00108 // An addrec. This is the interesting part. 00109 SmallVector<const SCEV *, 8> Operands; 00110 const Loop *L = AR->getLoop(); 00111 // The addrec conceptually uses its operands at loop entry. 00112 Instruction *LUser = L->getHeader()->begin(); 00113 // Transform each operand. 00114 for (SCEVNAryExpr::op_iterator I = AR->op_begin(), E = AR->op_end(); 00115 I != E; ++I) { 00116 Operands.push_back(TransformSubExpr(*I, LUser, 0)); 00117 } 00118 // Conservatively use AnyWrap until/unless we need FlagNW. 00119 const SCEV *Result = SE.getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); 00120 switch (Kind) { 00121 case NormalizeAutodetect: 00122 if (IVUseShouldUsePostIncValue(User, OperandValToReplace, L, &DT)) { 00123 const SCEV *TransformedStep = 00124 TransformSubExpr(AR->getStepRecurrence(SE), 00125 User, OperandValToReplace); 00126 Result = SE.getMinusSCEV(Result, TransformedStep); 00127 Loops.insert(L); 00128 } 00129 #if 0 00130 // This assert is conceptually correct, but ScalarEvolution currently 00131 // sometimes fails to canonicalize two equal SCEVs to exactly the same 00132 // form. It's possibly a pessimization when this happens, but it isn't a 00133 // correctness problem, so disable this assert for now. 00134 assert(S == TransformSubExpr(Result, User, OperandValToReplace) && 00135 "SCEV normalization is not invertible!"); 00136 #endif 00137 break; 00138 case Normalize: 00139 if (Loops.count(L)) { 00140 const SCEV *TransformedStep = 00141 TransformSubExpr(AR->getStepRecurrence(SE), 00142 User, OperandValToReplace); 00143 Result = SE.getMinusSCEV(Result, TransformedStep); 00144 } 00145 #if 0 00146 // See the comment on the assert above. 00147 assert(S == TransformSubExpr(Result, User, OperandValToReplace) && 00148 "SCEV normalization is not invertible!"); 00149 #endif 00150 break; 00151 case Denormalize: 00152 if (Loops.count(L)) 00153 Result = cast<SCEVAddRecExpr>(Result)->getPostIncExpr(SE); 00154 break; 00155 } 00156 return Result; 00157 } 00158 00159 if (const SCEVNAryExpr *X = dyn_cast<SCEVNAryExpr>(S)) { 00160 SmallVector<const SCEV *, 8> Operands; 00161 bool Changed = false; 00162 // Transform each operand. 00163 for (SCEVNAryExpr::op_iterator I = X->op_begin(), E = X->op_end(); 00164 I != E; ++I) { 00165 const SCEV *O = *I; 00166 const SCEV *N = TransformSubExpr(O, User, OperandValToReplace); 00167 Changed |= N != O; 00168 Operands.push_back(N); 00169 } 00170 // If any operand actually changed, return a transformed result. 00171 if (Changed) 00172 switch (S->getSCEVType()) { 00173 case scAddExpr: return SE.getAddExpr(Operands); 00174 case scMulExpr: return SE.getMulExpr(Operands); 00175 case scSMaxExpr: return SE.getSMaxExpr(Operands); 00176 case scUMaxExpr: return SE.getUMaxExpr(Operands); 00177 default: llvm_unreachable("Unexpected SCEVNAryExpr kind!"); 00178 } 00179 return S; 00180 } 00181 00182 if (const SCEVUDivExpr *X = dyn_cast<SCEVUDivExpr>(S)) { 00183 const SCEV *LO = X->getLHS(); 00184 const SCEV *RO = X->getRHS(); 00185 const SCEV *LN = TransformSubExpr(LO, User, OperandValToReplace); 00186 const SCEV *RN = TransformSubExpr(RO, User, OperandValToReplace); 00187 if (LO != LN || RO != RN) 00188 return SE.getUDivExpr(LN, RN); 00189 return S; 00190 } 00191 00192 llvm_unreachable("Unexpected SCEV kind!"); 00193 } 00194 00195 /// Manage recursive transformation across an expression DAG. Revisiting 00196 /// expressions would lead to exponential recursion. 00197 const SCEV *PostIncTransform:: 00198 TransformSubExpr(const SCEV *S, Instruction *User, Value *OperandValToReplace) { 00199 00200 if (isa<SCEVConstant>(S) || isa<SCEVUnknown>(S)) 00201 return S; 00202 00203 const SCEV *Result = Transformed.lookup(S); 00204 if (Result) 00205 return Result; 00206 00207 Result = TransformImpl(S, User, OperandValToReplace); 00208 Transformed[S] = Result; 00209 return Result; 00210 } 00211 00212 /// Top level driver for transforming an expression DAG into its requested 00213 /// post-inc form (either "Normalized" or "Denormalized". 00214 const SCEV *llvm::TransformForPostIncUse(TransformKind Kind, 00215 const SCEV *S, 00216 Instruction *User, 00217 Value *OperandValToReplace, 00218 PostIncLoopSet &Loops, 00219 ScalarEvolution &SE, 00220 DominatorTree &DT) { 00221 PostIncTransform Transform(Kind, Loops, SE, DT); 00222 return Transform.TransformSubExpr(S, User, OperandValToReplace); 00223 }