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