Nick Lewycky | af50837 | 2016-04-24 17:55:41 +0000 | [diff] [blame] | 1 | //===- ScalarEvolutionNormalization.cpp - See below -----------------------===// |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements utilities for working with "normalized" expressions. |
| 11 | // See the comments at the top of ScalarEvolutionNormalization.h for details. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Dominators.h" |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/LoopInfo.h" |
| 17 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 18 | #include "llvm/Analysis/ScalarEvolutionNormalization.h" |
| 19 | using namespace llvm; |
| 20 | |
| 21 | /// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression |
| 22 | /// and now we need to decide whether the user should use the preinc or post-inc |
| 23 | /// value. If this user should use the post-inc version of the IV, return true. |
| 24 | /// |
| 25 | /// Choosing wrong here can break dominance properties (if we choose to use the |
| 26 | /// post-inc value when we cannot) or it can end up adding extra live-ranges to |
| 27 | /// the loop, resulting in reg-reg copies (if we use the pre-inc value when we |
| 28 | /// should use the post-inc value). |
Dan Gohman | 191f2e4 | 2010-07-20 16:34:50 +0000 | [diff] [blame] | 29 | static bool IVUseShouldUsePostIncValue(Instruction *User, Value *Operand, |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 30 | const Loop *L, DominatorTree *DT) { |
| 31 | // If the user is in the loop, use the preinc value. |
| 32 | if (L->contains(User)) return false; |
| 33 | |
| 34 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 35 | if (!LatchBlock) |
| 36 | return false; |
| 37 | |
| 38 | // Ok, the user is outside of the loop. If it is dominated by the latch |
| 39 | // block, use the post-inc value. |
| 40 | if (DT->dominates(LatchBlock, User->getParent())) |
| 41 | return true; |
| 42 | |
| 43 | // There is one case we have to be careful of: PHI nodes. These little guys |
| 44 | // can live in blocks that are not dominated by the latch block, but (since |
| 45 | // their uses occur in the predecessor block, not the block the PHI lives in) |
| 46 | // should still use the post-inc value. Check for this case now. |
| 47 | PHINode *PN = dyn_cast<PHINode>(User); |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 48 | if (!PN || !Operand) return false; // not a phi, not dominated by latch block. |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 49 | |
Dan Gohman | 191f2e4 | 2010-07-20 16:34:50 +0000 | [diff] [blame] | 50 | // Look at all of the uses of Operand by the PHI node. If any use corresponds |
| 51 | // to a block that is not dominated by the latch block, give up and use the |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 52 | // preincremented value. |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 53 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
Dan Gohman | 191f2e4 | 2010-07-20 16:34:50 +0000 | [diff] [blame] | 54 | if (PN->getIncomingValue(i) == Operand && |
Dan Gohman | 3ff13af | 2010-07-20 00:57:18 +0000 | [diff] [blame] | 55 | !DT->dominates(LatchBlock, PN->getIncomingBlock(i))) |
| 56 | return false; |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 57 | |
Dan Gohman | 191f2e4 | 2010-07-20 16:34:50 +0000 | [diff] [blame] | 58 | // Okay, all uses of Operand by PN are in predecessor blocks that really are |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 59 | // dominated by the latch block. Use the post-incremented value. |
| 60 | return true; |
| 61 | } |
| 62 | |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 63 | namespace { |
| 64 | |
| 65 | /// Hold the state used during post-inc expression transformation, including a |
| 66 | /// map of transformed expressions. |
| 67 | class PostIncTransform { |
| 68 | TransformKind Kind; |
| 69 | PostIncLoopSet &Loops; |
| 70 | ScalarEvolution &SE; |
| 71 | DominatorTree &DT; |
| 72 | |
| 73 | DenseMap<const SCEV*, const SCEV*> Transformed; |
| 74 | |
| 75 | public: |
| 76 | PostIncTransform(TransformKind kind, PostIncLoopSet &loops, |
| 77 | ScalarEvolution &se, DominatorTree &dt): |
| 78 | Kind(kind), Loops(loops), SE(se), DT(dt) {} |
| 79 | |
| 80 | const SCEV *TransformSubExpr(const SCEV *S, Instruction *User, |
| 81 | Value *OperandValToReplace); |
| 82 | |
| 83 | protected: |
| 84 | const SCEV *TransformImpl(const SCEV *S, Instruction *User, |
| 85 | Value *OperandValToReplace); |
| 86 | }; |
| 87 | |
| 88 | } // namespace |
| 89 | |
| 90 | /// Implement post-inc transformation for all valid expression types. |
| 91 | const SCEV *PostIncTransform:: |
| 92 | TransformImpl(const SCEV *S, Instruction *User, Value *OperandValToReplace) { |
Dan Gohman | d1488fd | 2010-07-20 16:32:11 +0000 | [diff] [blame] | 93 | |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 94 | if (const SCEVCastExpr *X = dyn_cast<SCEVCastExpr>(S)) { |
| 95 | const SCEV *O = X->getOperand(); |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 96 | const SCEV *N = TransformSubExpr(O, User, OperandValToReplace); |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 97 | if (O != N) |
| 98 | switch (S->getSCEVType()) { |
| 99 | case scZeroExtend: return SE.getZeroExtendExpr(N, S->getType()); |
| 100 | case scSignExtend: return SE.getSignExtendExpr(N, S->getType()); |
| 101 | case scTruncate: return SE.getTruncateExpr(N, S->getType()); |
| 102 | default: llvm_unreachable("Unexpected SCEVCastExpr kind!"); |
| 103 | } |
| 104 | return S; |
| 105 | } |
Dan Gohman | d1488fd | 2010-07-20 16:32:11 +0000 | [diff] [blame] | 106 | |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 107 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 108 | // An addrec. This is the interesting part. |
| 109 | SmallVector<const SCEV *, 8> Operands; |
| 110 | const Loop *L = AR->getLoop(); |
| 111 | // The addrec conceptually uses its operands at loop entry. |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 112 | Instruction *LUser = &L->getHeader()->front(); |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 113 | // Transform each operand. |
| 114 | for (SCEVNAryExpr::op_iterator I = AR->op_begin(), E = AR->op_end(); |
| 115 | I != E; ++I) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 116 | Operands.push_back(TransformSubExpr(*I, LUser, nullptr)); |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 117 | } |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 118 | // Conservatively use AnyWrap until/unless we need FlagNW. |
| 119 | const SCEV *Result = SE.getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 120 | switch (Kind) { |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 121 | case NormalizeAutodetect: |
Andrew Trick | 29abce3 | 2013-10-25 21:35:52 +0000 | [diff] [blame] | 122 | // Normalize this SCEV by subtracting the expression for the final step. |
| 123 | // We only allow affine AddRecs to be normalized, otherwise we would not |
| 124 | // be able to correctly denormalize. |
| 125 | // e.g. {1,+,3,+,2} == {-2,+,1,+,2} + {3,+,2} |
| 126 | // Normalized form: {-2,+,1,+,2} |
| 127 | // Denormalized form: {1,+,3,+,2} |
| 128 | // |
Robin Morisset | 039781e | 2014-08-29 21:53:01 +0000 | [diff] [blame] | 129 | // However, denormalization would use a different step expression than |
Andrew Trick | 29abce3 | 2013-10-25 21:35:52 +0000 | [diff] [blame] | 130 | // normalization (see getPostIncExpr), generating the wrong final |
| 131 | // expression: {-2,+,1,+,2} + {1,+,2} => {-1,+,3,+,2} |
| 132 | if (AR->isAffine() && |
| 133 | IVUseShouldUsePostIncValue(User, OperandValToReplace, L, &DT)) { |
Michael Zolotukhin | ed0a7761 | 2014-03-18 17:34:03 +0000 | [diff] [blame] | 134 | const SCEV *TransformedStep = |
| 135 | TransformSubExpr(AR->getStepRecurrence(SE), |
| 136 | User, OperandValToReplace); |
| 137 | Result = SE.getMinusSCEV(Result, TransformedStep); |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 138 | Loops.insert(L); |
| 139 | } |
Dan Gohman | 47bec3c | 2010-09-03 22:12:56 +0000 | [diff] [blame] | 140 | #if 0 |
| 141 | // This assert is conceptually correct, but ScalarEvolution currently |
| 142 | // sometimes fails to canonicalize two equal SCEVs to exactly the same |
| 143 | // form. It's possibly a pessimization when this happens, but it isn't a |
| 144 | // correctness problem, so disable this assert for now. |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 145 | assert(S == TransformSubExpr(Result, User, OperandValToReplace) && |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 146 | "SCEV normalization is not invertible!"); |
| 147 | #endif |
| 148 | break; |
| 149 | case Normalize: |
Michael Zolotukhin | ed0a7761 | 2014-03-18 17:34:03 +0000 | [diff] [blame] | 150 | // We want to normalize step expression, because otherwise we might not be |
| 151 | // able to denormalize to the original expression. |
| 152 | // |
| 153 | // Here is an example what will happen if we don't normalize step: |
| 154 | // ORIGINAL ISE: |
| 155 | // {(100 /u {1,+,1}<%bb16>),+,(100 /u {1,+,1}<%bb16>)}<%bb25> |
| 156 | // NORMALIZED ISE: |
| 157 | // {((-1 * (100 /u {1,+,1}<%bb16>)) + (100 /u {0,+,1}<%bb16>)),+, |
| 158 | // (100 /u {0,+,1}<%bb16>)}<%bb25> |
| 159 | // DENORMALIZED BACK ISE: |
| 160 | // {((2 * (100 /u {1,+,1}<%bb16>)) + (-1 * (100 /u {2,+,1}<%bb16>))),+, |
| 161 | // (100 /u {1,+,1}<%bb16>)}<%bb25> |
| 162 | // Note that the initial value changes after normalization + |
| 163 | // denormalization, which isn't correct. |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 164 | if (Loops.count(L)) { |
| 165 | const SCEV *TransformedStep = |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 166 | TransformSubExpr(AR->getStepRecurrence(SE), |
| 167 | User, OperandValToReplace); |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 168 | Result = SE.getMinusSCEV(Result, TransformedStep); |
| 169 | } |
Dan Gohman | 47bec3c | 2010-09-03 22:12:56 +0000 | [diff] [blame] | 170 | #if 0 |
| 171 | // See the comment on the assert above. |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 172 | assert(S == TransformSubExpr(Result, User, OperandValToReplace) && |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 173 | "SCEV normalization is not invertible!"); |
| 174 | #endif |
| 175 | break; |
| 176 | case Denormalize: |
Michael Zolotukhin | ed0a7761 | 2014-03-18 17:34:03 +0000 | [diff] [blame] | 177 | // Here we want to normalize step expressions for the same reasons, as |
| 178 | // stated above. |
| 179 | if (Loops.count(L)) { |
| 180 | const SCEV *TransformedStep = |
| 181 | TransformSubExpr(AR->getStepRecurrence(SE), |
| 182 | User, OperandValToReplace); |
| 183 | Result = SE.getAddExpr(Result, TransformedStep); |
| 184 | } |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 185 | break; |
| 186 | } |
| 187 | return Result; |
| 188 | } |
| 189 | |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 190 | if (const SCEVNAryExpr *X = dyn_cast<SCEVNAryExpr>(S)) { |
| 191 | SmallVector<const SCEV *, 8> Operands; |
| 192 | bool Changed = false; |
Dan Gohman | d1488fd | 2010-07-20 16:32:11 +0000 | [diff] [blame] | 193 | // Transform each operand. |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 194 | for (SCEVNAryExpr::op_iterator I = X->op_begin(), E = X->op_end(); |
| 195 | I != E; ++I) { |
| 196 | const SCEV *O = *I; |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 197 | const SCEV *N = TransformSubExpr(O, User, OperandValToReplace); |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 198 | Changed |= N != O; |
| 199 | Operands.push_back(N); |
| 200 | } |
Dan Gohman | 625fd22 | 2010-07-20 17:06:20 +0000 | [diff] [blame] | 201 | // If any operand actually changed, return a transformed result. |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 202 | if (Changed) |
| 203 | switch (S->getSCEVType()) { |
| 204 | case scAddExpr: return SE.getAddExpr(Operands); |
| 205 | case scMulExpr: return SE.getMulExpr(Operands); |
| 206 | case scSMaxExpr: return SE.getSMaxExpr(Operands); |
| 207 | case scUMaxExpr: return SE.getUMaxExpr(Operands); |
| 208 | default: llvm_unreachable("Unexpected SCEVNAryExpr kind!"); |
| 209 | } |
| 210 | return S; |
| 211 | } |
Dan Gohman | d1488fd | 2010-07-20 16:32:11 +0000 | [diff] [blame] | 212 | |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 213 | if (const SCEVUDivExpr *X = dyn_cast<SCEVUDivExpr>(S)) { |
| 214 | const SCEV *LO = X->getLHS(); |
| 215 | const SCEV *RO = X->getRHS(); |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 216 | const SCEV *LN = TransformSubExpr(LO, User, OperandValToReplace); |
| 217 | const SCEV *RN = TransformSubExpr(RO, User, OperandValToReplace); |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 218 | if (LO != LN || RO != RN) |
| 219 | return SE.getUDivExpr(LN, RN); |
| 220 | return S; |
| 221 | } |
Dan Gohman | d1488fd | 2010-07-20 16:32:11 +0000 | [diff] [blame] | 222 | |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 223 | llvm_unreachable("Unexpected SCEV kind!"); |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 224 | } |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 225 | |
| 226 | /// Manage recursive transformation across an expression DAG. Revisiting |
| 227 | /// expressions would lead to exponential recursion. |
| 228 | const SCEV *PostIncTransform:: |
| 229 | TransformSubExpr(const SCEV *S, Instruction *User, Value *OperandValToReplace) { |
| 230 | |
| 231 | if (isa<SCEVConstant>(S) || isa<SCEVUnknown>(S)) |
| 232 | return S; |
| 233 | |
Andrew Trick | 7e44256 | 2011-10-13 18:49:23 +0000 | [diff] [blame] | 234 | const SCEV *Result = Transformed.lookup(S); |
| 235 | if (Result) |
| 236 | return Result; |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 237 | |
Andrew Trick | 7e44256 | 2011-10-13 18:49:23 +0000 | [diff] [blame] | 238 | Result = TransformImpl(S, User, OperandValToReplace); |
| 239 | Transformed[S] = Result; |
| 240 | return Result; |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /// Top level driver for transforming an expression DAG into its requested |
Sanjay Patel | 26b6edc | 2014-05-28 19:03:33 +0000 | [diff] [blame] | 244 | /// post-inc form (either "Normalized" or "Denormalized"). |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 245 | const SCEV *llvm::TransformForPostIncUse(TransformKind Kind, |
| 246 | const SCEV *S, |
| 247 | Instruction *User, |
| 248 | Value *OperandValToReplace, |
| 249 | PostIncLoopSet &Loops, |
| 250 | ScalarEvolution &SE, |
| 251 | DominatorTree &DT) { |
| 252 | PostIncTransform Transform(Kind, Loops, SE, DT); |
| 253 | return Transform.TransformSubExpr(S, User, OperandValToReplace); |
| 254 | } |