blob: c66ecd6e872794942b5f032d96b246a7a4e9dbd0 [file] [log] [blame]
Dan Gohman448db1c2010-04-07 22:27:08 +00001//===- ScalarEvolutionNormalization.cpp - See below -------------*- C++ -*-===//
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
15#include "llvm/Analysis/Dominators.h"
16#include "llvm/Analysis/LoopInfo.h"
17#include "llvm/Analysis/ScalarEvolutionExpressions.h"
18#include "llvm/Analysis/ScalarEvolutionNormalization.h"
19using 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 Gohmanb7391fa2010-07-20 16:34:50 +000029static bool IVUseShouldUsePostIncValue(Instruction *User, Value *Operand,
Dan Gohman448db1c2010-04-07 22:27:08 +000030 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 Gohmanfc3678a2010-07-20 17:06:20 +000048 if (!PN || !Operand) return false; // not a phi, not dominated by latch block.
Dan Gohman448db1c2010-04-07 22:27:08 +000049
Dan Gohmanb7391fa2010-07-20 16:34:50 +000050 // 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 Gohman448db1c2010-04-07 22:27:08 +000052 // preincremented value.
Dan Gohman448db1c2010-04-07 22:27:08 +000053 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
Dan Gohmanb7391fa2010-07-20 16:34:50 +000054 if (PN->getIncomingValue(i) == Operand &&
Dan Gohmanfb272ad2010-07-20 00:57:18 +000055 !DT->dominates(LatchBlock, PN->getIncomingBlock(i)))
56 return false;
Dan Gohman448db1c2010-04-07 22:27:08 +000057
Dan Gohmanb7391fa2010-07-20 16:34:50 +000058 // Okay, all uses of Operand by PN are in predecessor blocks that really are
Dan Gohman448db1c2010-04-07 22:27:08 +000059 // dominated by the latch block. Use the post-incremented value.
60 return true;
61}
62
Andrew Trick94f01db2011-10-13 17:21:09 +000063namespace {
64
65/// Hold the state used during post-inc expression transformation, including a
66/// map of transformed expressions.
67class PostIncTransform {
68 TransformKind Kind;
69 PostIncLoopSet &Loops;
70 ScalarEvolution &SE;
71 DominatorTree &DT;
72
73 DenseMap<const SCEV*, const SCEV*> Transformed;
74
75public:
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
83protected:
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.
91const SCEV *PostIncTransform::
92TransformImpl(const SCEV *S, Instruction *User, Value *OperandValToReplace) {
Dan Gohman082d6b62010-07-20 16:32:11 +000093
Dan Gohman448db1c2010-04-07 22:27:08 +000094 if (const SCEVCastExpr *X = dyn_cast<SCEVCastExpr>(S)) {
95 const SCEV *O = X->getOperand();
Andrew Trick94f01db2011-10-13 17:21:09 +000096 const SCEV *N = TransformSubExpr(O, User, OperandValToReplace);
Dan Gohman448db1c2010-04-07 22:27:08 +000097 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 Gohman082d6b62010-07-20 16:32:11 +0000106
Dan Gohmanfc3678a2010-07-20 17:06:20 +0000107 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.
112 Instruction *LUser = L->getHeader()->begin();
113 // Transform each operand.
114 for (SCEVNAryExpr::op_iterator I = AR->op_begin(), E = AR->op_end();
115 I != E; ++I) {
Andrew Trick94f01db2011-10-13 17:21:09 +0000116 Operands.push_back(TransformSubExpr(*I, LUser, 0));
Dan Gohmanfc3678a2010-07-20 17:06:20 +0000117 }
Andrew Trick3228cc22011-03-14 16:50:06 +0000118 // Conservatively use AnyWrap until/unless we need FlagNW.
119 const SCEV *Result = SE.getAddRecExpr(Operands, L, SCEV::FlagAnyWrap);
Dan Gohmanfc3678a2010-07-20 17:06:20 +0000120 switch (Kind) {
121 default: llvm_unreachable("Unexpected transform name!");
122 case NormalizeAutodetect:
123 if (IVUseShouldUsePostIncValue(User, OperandValToReplace, L, &DT)) {
124 const SCEV *TransformedStep =
Andrew Trick94f01db2011-10-13 17:21:09 +0000125 TransformSubExpr(AR->getStepRecurrence(SE),
126 User, OperandValToReplace);
Dan Gohmanfc3678a2010-07-20 17:06:20 +0000127 Result = SE.getMinusSCEV(Result, TransformedStep);
128 Loops.insert(L);
129 }
Dan Gohman46ffb232010-09-03 22:12:56 +0000130#if 0
131 // This assert is conceptually correct, but ScalarEvolution currently
132 // sometimes fails to canonicalize two equal SCEVs to exactly the same
133 // form. It's possibly a pessimization when this happens, but it isn't a
134 // correctness problem, so disable this assert for now.
Andrew Trick94f01db2011-10-13 17:21:09 +0000135 assert(S == TransformSubExpr(Result, User, OperandValToReplace) &&
Dan Gohmanfc3678a2010-07-20 17:06:20 +0000136 "SCEV normalization is not invertible!");
137#endif
138 break;
139 case Normalize:
140 if (Loops.count(L)) {
141 const SCEV *TransformedStep =
Andrew Trick94f01db2011-10-13 17:21:09 +0000142 TransformSubExpr(AR->getStepRecurrence(SE),
143 User, OperandValToReplace);
Dan Gohmanfc3678a2010-07-20 17:06:20 +0000144 Result = SE.getMinusSCEV(Result, TransformedStep);
145 }
Dan Gohman46ffb232010-09-03 22:12:56 +0000146#if 0
147 // See the comment on the assert above.
Andrew Trick94f01db2011-10-13 17:21:09 +0000148 assert(S == TransformSubExpr(Result, User, OperandValToReplace) &&
Dan Gohmanfc3678a2010-07-20 17:06:20 +0000149 "SCEV normalization is not invertible!");
150#endif
151 break;
152 case Denormalize:
153 if (Loops.count(L))
154 Result = cast<SCEVAddRecExpr>(Result)->getPostIncExpr(SE);
155 break;
156 }
157 return Result;
158 }
159
Dan Gohman448db1c2010-04-07 22:27:08 +0000160 if (const SCEVNAryExpr *X = dyn_cast<SCEVNAryExpr>(S)) {
161 SmallVector<const SCEV *, 8> Operands;
162 bool Changed = false;
Dan Gohman082d6b62010-07-20 16:32:11 +0000163 // Transform each operand.
Dan Gohman448db1c2010-04-07 22:27:08 +0000164 for (SCEVNAryExpr::op_iterator I = X->op_begin(), E = X->op_end();
165 I != E; ++I) {
166 const SCEV *O = *I;
Andrew Trick94f01db2011-10-13 17:21:09 +0000167 const SCEV *N = TransformSubExpr(O, User, OperandValToReplace);
Dan Gohman448db1c2010-04-07 22:27:08 +0000168 Changed |= N != O;
169 Operands.push_back(N);
170 }
Dan Gohmanfc3678a2010-07-20 17:06:20 +0000171 // If any operand actually changed, return a transformed result.
Dan Gohman448db1c2010-04-07 22:27:08 +0000172 if (Changed)
173 switch (S->getSCEVType()) {
174 case scAddExpr: return SE.getAddExpr(Operands);
175 case scMulExpr: return SE.getMulExpr(Operands);
176 case scSMaxExpr: return SE.getSMaxExpr(Operands);
177 case scUMaxExpr: return SE.getUMaxExpr(Operands);
178 default: llvm_unreachable("Unexpected SCEVNAryExpr kind!");
179 }
180 return S;
181 }
Dan Gohman082d6b62010-07-20 16:32:11 +0000182
Dan Gohman448db1c2010-04-07 22:27:08 +0000183 if (const SCEVUDivExpr *X = dyn_cast<SCEVUDivExpr>(S)) {
184 const SCEV *LO = X->getLHS();
185 const SCEV *RO = X->getRHS();
Andrew Trick94f01db2011-10-13 17:21:09 +0000186 const SCEV *LN = TransformSubExpr(LO, User, OperandValToReplace);
187 const SCEV *RN = TransformSubExpr(RO, User, OperandValToReplace);
Dan Gohman448db1c2010-04-07 22:27:08 +0000188 if (LO != LN || RO != RN)
189 return SE.getUDivExpr(LN, RN);
190 return S;
191 }
Dan Gohman082d6b62010-07-20 16:32:11 +0000192
Dan Gohman448db1c2010-04-07 22:27:08 +0000193 llvm_unreachable("Unexpected SCEV kind!");
194 return 0;
195}
Andrew Trick94f01db2011-10-13 17:21:09 +0000196
197/// Manage recursive transformation across an expression DAG. Revisiting
198/// expressions would lead to exponential recursion.
199const SCEV *PostIncTransform::
200TransformSubExpr(const SCEV *S, Instruction *User, Value *OperandValToReplace) {
201
202 if (isa<SCEVConstant>(S) || isa<SCEVUnknown>(S))
203 return S;
204
Andrew Trick7f1653a2011-10-13 18:49:23 +0000205 const SCEV *Result = Transformed.lookup(S);
206 if (Result)
207 return Result;
Andrew Trick94f01db2011-10-13 17:21:09 +0000208
Andrew Trick7f1653a2011-10-13 18:49:23 +0000209 Result = TransformImpl(S, User, OperandValToReplace);
210 Transformed[S] = Result;
211 return Result;
Andrew Trick94f01db2011-10-13 17:21:09 +0000212}
213
214/// Top level driver for transforming an expression DAG into its requested
215/// post-inc form (either "Normalized" or "Denormalized".
216const SCEV *llvm::TransformForPostIncUse(TransformKind Kind,
217 const SCEV *S,
218 Instruction *User,
219 Value *OperandValToReplace,
220 PostIncLoopSet &Loops,
221 ScalarEvolution &SE,
222 DominatorTree &DT) {
223 PostIncTransform Transform(Kind, Loops, SE, DT);
224 return Transform.TransformSubExpr(S, User, OperandValToReplace);
225}