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/IR/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, nullptr)); 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 // Normalize this SCEV by subtracting the expression for the final step. 00123 // We only allow affine AddRecs to be normalized, otherwise we would not 00124 // be able to correctly denormalize. 00125 // e.g. {1,+,3,+,2} == {-2,+,1,+,2} + {3,+,2} 00126 // Normalized form: {-2,+,1,+,2} 00127 // Denormalized form: {1,+,3,+,2} 00128 // 00129 // However, denormalization would use a different step expression than 00130 // normalization (see getPostIncExpr), generating the wrong final 00131 // expression: {-2,+,1,+,2} + {1,+,2} => {-1,+,3,+,2} 00132 if (AR->isAffine() && 00133 IVUseShouldUsePostIncValue(User, OperandValToReplace, L, &DT)) { 00134 const SCEV *TransformedStep = 00135 TransformSubExpr(AR->getStepRecurrence(SE), 00136 User, OperandValToReplace); 00137 Result = SE.getMinusSCEV(Result, TransformedStep); 00138 Loops.insert(L); 00139 } 00140 #if 0 00141 // This assert is conceptually correct, but ScalarEvolution currently 00142 // sometimes fails to canonicalize two equal SCEVs to exactly the same 00143 // form. It's possibly a pessimization when this happens, but it isn't a 00144 // correctness problem, so disable this assert for now. 00145 assert(S == TransformSubExpr(Result, User, OperandValToReplace) && 00146 "SCEV normalization is not invertible!"); 00147 #endif 00148 break; 00149 case Normalize: 00150 // We want to normalize step expression, because otherwise we might not be 00151 // able to denormalize to the original expression. 00152 // 00153 // Here is an example what will happen if we don't normalize step: 00154 // ORIGINAL ISE: 00155 // {(100 /u {1,+,1}<%bb16>),+,(100 /u {1,+,1}<%bb16>)}<%bb25> 00156 // NORMALIZED ISE: 00157 // {((-1 * (100 /u {1,+,1}<%bb16>)) + (100 /u {0,+,1}<%bb16>)),+, 00158 // (100 /u {0,+,1}<%bb16>)}<%bb25> 00159 // DENORMALIZED BACK ISE: 00160 // {((2 * (100 /u {1,+,1}<%bb16>)) + (-1 * (100 /u {2,+,1}<%bb16>))),+, 00161 // (100 /u {1,+,1}<%bb16>)}<%bb25> 00162 // Note that the initial value changes after normalization + 00163 // denormalization, which isn't correct. 00164 if (Loops.count(L)) { 00165 const SCEV *TransformedStep = 00166 TransformSubExpr(AR->getStepRecurrence(SE), 00167 User, OperandValToReplace); 00168 Result = SE.getMinusSCEV(Result, TransformedStep); 00169 } 00170 #if 0 00171 // See the comment on the assert above. 00172 assert(S == TransformSubExpr(Result, User, OperandValToReplace) && 00173 "SCEV normalization is not invertible!"); 00174 #endif 00175 break; 00176 case Denormalize: 00177 // Here we want to normalize step expressions for the same reasons, as 00178 // stated above. 00179 if (Loops.count(L)) { 00180 const SCEV *TransformedStep = 00181 TransformSubExpr(AR->getStepRecurrence(SE), 00182 User, OperandValToReplace); 00183 Result = SE.getAddExpr(Result, TransformedStep); 00184 } 00185 break; 00186 } 00187 return Result; 00188 } 00189 00190 if (const SCEVNAryExpr *X = dyn_cast<SCEVNAryExpr>(S)) { 00191 SmallVector<const SCEV *, 8> Operands; 00192 bool Changed = false; 00193 // Transform each operand. 00194 for (SCEVNAryExpr::op_iterator I = X->op_begin(), E = X->op_end(); 00195 I != E; ++I) { 00196 const SCEV *O = *I; 00197 const SCEV *N = TransformSubExpr(O, User, OperandValToReplace); 00198 Changed |= N != O; 00199 Operands.push_back(N); 00200 } 00201 // If any operand actually changed, return a transformed result. 00202 if (Changed) 00203 switch (S->getSCEVType()) { 00204 case scAddExpr: return SE.getAddExpr(Operands); 00205 case scMulExpr: return SE.getMulExpr(Operands); 00206 case scSMaxExpr: return SE.getSMaxExpr(Operands); 00207 case scUMaxExpr: return SE.getUMaxExpr(Operands); 00208 default: llvm_unreachable("Unexpected SCEVNAryExpr kind!"); 00209 } 00210 return S; 00211 } 00212 00213 if (const SCEVUDivExpr *X = dyn_cast<SCEVUDivExpr>(S)) { 00214 const SCEV *LO = X->getLHS(); 00215 const SCEV *RO = X->getRHS(); 00216 const SCEV *LN = TransformSubExpr(LO, User, OperandValToReplace); 00217 const SCEV *RN = TransformSubExpr(RO, User, OperandValToReplace); 00218 if (LO != LN || RO != RN) 00219 return SE.getUDivExpr(LN, RN); 00220 return S; 00221 } 00222 00223 llvm_unreachable("Unexpected SCEV kind!"); 00224 } 00225 00226 /// Manage recursive transformation across an expression DAG. Revisiting 00227 /// expressions would lead to exponential recursion. 00228 const SCEV *PostIncTransform:: 00229 TransformSubExpr(const SCEV *S, Instruction *User, Value *OperandValToReplace) { 00230 00231 if (isa<SCEVConstant>(S) || isa<SCEVUnknown>(S)) 00232 return S; 00233 00234 const SCEV *Result = Transformed.lookup(S); 00235 if (Result) 00236 return Result; 00237 00238 Result = TransformImpl(S, User, OperandValToReplace); 00239 Transformed[S] = Result; 00240 return Result; 00241 } 00242 00243 /// Top level driver for transforming an expression DAG into its requested 00244 /// post-inc form (either "Normalized" or "Denormalized"). 00245 const SCEV *llvm::TransformForPostIncUse(TransformKind Kind, 00246 const SCEV *S, 00247 Instruction *User, 00248 Value *OperandValToReplace, 00249 PostIncLoopSet &Loops, 00250 ScalarEvolution &SE, 00251 DominatorTree &DT) { 00252 PostIncTransform Transform(Kind, Loops, SE, DT); 00253 return Transform.TransformSubExpr(S, User, OperandValToReplace); 00254 }