1 //===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the implementation of the scalar evolution expander,
10 // which is used to generate the code corresponding to a given scalar evolution
11 // expression.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/Analysis/InstructionSimplify.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Dominators.h"
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/PatternMatch.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/raw_ostream.h"
30 
31 using namespace llvm;
32 
33 cl::opt<unsigned> llvm::SCEVCheapExpansionBudget(
34     "scev-cheap-expansion-budget", cl::Hidden, cl::init(4),
35     cl::desc("When performing SCEV expansion only if it is cheap to do, this "
36              "controls the budget that is considered cheap (default = 4)"));
37 
38 using namespace PatternMatch;
39 
40 /// ReuseOrCreateCast - Arrange for there to be a cast of V to Ty at IP,
41 /// reusing an existing cast if a suitable one exists, moving an existing
42 /// cast if a suitable one exists but isn't in the right place, or
43 /// creating a new one.
ReuseOrCreateCast(Value * V,Type * Ty,Instruction::CastOps Op,BasicBlock::iterator IP)44 Value *SCEVExpander::ReuseOrCreateCast(Value *V, Type *Ty,
45                                        Instruction::CastOps Op,
46                                        BasicBlock::iterator IP) {
47   // This function must be called with the builder having a valid insertion
48   // point. It doesn't need to be the actual IP where the uses of the returned
49   // cast will be added, but it must dominate such IP.
50   // We use this precondition to produce a cast that will dominate all its
51   // uses. In particular, this is crucial for the case where the builder's
52   // insertion point *is* the point where we were asked to put the cast.
53   // Since we don't know the builder's insertion point is actually
54   // where the uses will be added (only that it dominates it), we are
55   // not allowed to move it.
56   BasicBlock::iterator BIP = Builder.GetInsertPoint();
57 
58   Instruction *Ret = nullptr;
59 
60   // Check to see if there is already a cast!
61   for (User *U : V->users())
62     if (U->getType() == Ty)
63       if (CastInst *CI = dyn_cast<CastInst>(U))
64         if (CI->getOpcode() == Op) {
65           // If the cast isn't where we want it, create a new cast at IP.
66           // Likewise, do not reuse a cast at BIP because it must dominate
67           // instructions that might be inserted before BIP.
68           if (BasicBlock::iterator(CI) != IP || BIP == IP) {
69             // Create a new cast, and leave the old cast in place in case
70             // it is being used as an insert point.
71             Ret = CastInst::Create(Op, V, Ty, "", &*IP);
72             Ret->takeName(CI);
73             CI->replaceAllUsesWith(Ret);
74             break;
75           }
76           Ret = CI;
77           break;
78         }
79 
80   // Create a new cast.
81   if (!Ret)
82     Ret = CastInst::Create(Op, V, Ty, V->getName(), &*IP);
83 
84   // We assert at the end of the function since IP might point to an
85   // instruction with different dominance properties than a cast
86   // (an invoke for example) and not dominate BIP (but the cast does).
87   assert(SE.DT.dominates(Ret, &*BIP));
88 
89   rememberInstruction(Ret);
90   return Ret;
91 }
92 
findInsertPointAfter(Instruction * I,BasicBlock * MustDominate)93 static BasicBlock::iterator findInsertPointAfter(Instruction *I,
94                                                  BasicBlock *MustDominate) {
95   BasicBlock::iterator IP = ++I->getIterator();
96   if (auto *II = dyn_cast<InvokeInst>(I))
97     IP = II->getNormalDest()->begin();
98 
99   while (isa<PHINode>(IP))
100     ++IP;
101 
102   if (isa<FuncletPadInst>(IP) || isa<LandingPadInst>(IP)) {
103     ++IP;
104   } else if (isa<CatchSwitchInst>(IP)) {
105     IP = MustDominate->getFirstInsertionPt();
106   } else {
107     assert(!IP->isEHPad() && "unexpected eh pad!");
108   }
109 
110   return IP;
111 }
112 
113 /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
114 /// which must be possible with a noop cast, doing what we can to share
115 /// the casts.
InsertNoopCastOfTo(Value * V,Type * Ty)116 Value *SCEVExpander::InsertNoopCastOfTo(Value *V, Type *Ty) {
117   Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false);
118   assert((Op == Instruction::BitCast ||
119           Op == Instruction::PtrToInt ||
120           Op == Instruction::IntToPtr) &&
121          "InsertNoopCastOfTo cannot perform non-noop casts!");
122   assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) &&
123          "InsertNoopCastOfTo cannot change sizes!");
124 
125   // Short-circuit unnecessary bitcasts.
126   if (Op == Instruction::BitCast) {
127     if (V->getType() == Ty)
128       return V;
129     if (CastInst *CI = dyn_cast<CastInst>(V)) {
130       if (CI->getOperand(0)->getType() == Ty)
131         return CI->getOperand(0);
132     }
133   }
134   // Short-circuit unnecessary inttoptr<->ptrtoint casts.
135   if ((Op == Instruction::PtrToInt || Op == Instruction::IntToPtr) &&
136       SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) {
137     if (CastInst *CI = dyn_cast<CastInst>(V))
138       if ((CI->getOpcode() == Instruction::PtrToInt ||
139            CI->getOpcode() == Instruction::IntToPtr) &&
140           SE.getTypeSizeInBits(CI->getType()) ==
141           SE.getTypeSizeInBits(CI->getOperand(0)->getType()))
142         return CI->getOperand(0);
143     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
144       if ((CE->getOpcode() == Instruction::PtrToInt ||
145            CE->getOpcode() == Instruction::IntToPtr) &&
146           SE.getTypeSizeInBits(CE->getType()) ==
147           SE.getTypeSizeInBits(CE->getOperand(0)->getType()))
148         return CE->getOperand(0);
149   }
150 
151   // Fold a cast of a constant.
152   if (Constant *C = dyn_cast<Constant>(V))
153     return ConstantExpr::getCast(Op, C, Ty);
154 
155   // Cast the argument at the beginning of the entry block, after
156   // any bitcasts of other arguments.
157   if (Argument *A = dyn_cast<Argument>(V)) {
158     BasicBlock::iterator IP = A->getParent()->getEntryBlock().begin();
159     while ((isa<BitCastInst>(IP) &&
160             isa<Argument>(cast<BitCastInst>(IP)->getOperand(0)) &&
161             cast<BitCastInst>(IP)->getOperand(0) != A) ||
162            isa<DbgInfoIntrinsic>(IP))
163       ++IP;
164     return ReuseOrCreateCast(A, Ty, Op, IP);
165   }
166 
167   // Cast the instruction immediately after the instruction.
168   Instruction *I = cast<Instruction>(V);
169   BasicBlock::iterator IP = findInsertPointAfter(I, Builder.GetInsertBlock());
170   return ReuseOrCreateCast(I, Ty, Op, IP);
171 }
172 
173 /// InsertBinop - Insert the specified binary operator, doing a small amount
174 /// of work to avoid inserting an obviously redundant operation, and hoisting
175 /// to an outer loop when the opportunity is there and it is safe.
InsertBinop(Instruction::BinaryOps Opcode,Value * LHS,Value * RHS,SCEV::NoWrapFlags Flags,bool IsSafeToHoist)176 Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode,
177                                  Value *LHS, Value *RHS,
178                                  SCEV::NoWrapFlags Flags, bool IsSafeToHoist) {
179   // Fold a binop with constant operands.
180   if (Constant *CLHS = dyn_cast<Constant>(LHS))
181     if (Constant *CRHS = dyn_cast<Constant>(RHS))
182       return ConstantExpr::get(Opcode, CLHS, CRHS);
183 
184   // Do a quick scan to see if we have this binop nearby.  If so, reuse it.
185   unsigned ScanLimit = 6;
186   BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
187   // Scanning starts from the last instruction before the insertion point.
188   BasicBlock::iterator IP = Builder.GetInsertPoint();
189   if (IP != BlockBegin) {
190     --IP;
191     for (; ScanLimit; --IP, --ScanLimit) {
192       // Don't count dbg.value against the ScanLimit, to avoid perturbing the
193       // generated code.
194       if (isa<DbgInfoIntrinsic>(IP))
195         ScanLimit++;
196 
197       auto canGenerateIncompatiblePoison = [&Flags](Instruction *I) {
198         // Ensure that no-wrap flags match.
199         if (isa<OverflowingBinaryOperator>(I)) {
200           if (I->hasNoSignedWrap() != (Flags & SCEV::FlagNSW))
201             return true;
202           if (I->hasNoUnsignedWrap() != (Flags & SCEV::FlagNUW))
203             return true;
204         }
205         // Conservatively, do not use any instruction which has any of exact
206         // flags installed.
207         if (isa<PossiblyExactOperator>(I) && I->isExact())
208           return true;
209         return false;
210       };
211       if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS &&
212           IP->getOperand(1) == RHS && !canGenerateIncompatiblePoison(&*IP))
213         return &*IP;
214       if (IP == BlockBegin) break;
215     }
216   }
217 
218   // Save the original insertion point so we can restore it when we're done.
219   DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc();
220   SCEVInsertPointGuard Guard(Builder, this);
221 
222   if (IsSafeToHoist) {
223     // Move the insertion point out of as many loops as we can.
224     while (const Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) {
225       if (!L->isLoopInvariant(LHS) || !L->isLoopInvariant(RHS)) break;
226       BasicBlock *Preheader = L->getLoopPreheader();
227       if (!Preheader) break;
228 
229       // Ok, move up a level.
230       Builder.SetInsertPoint(Preheader->getTerminator());
231     }
232   }
233 
234   // If we haven't found this binop, insert it.
235   Instruction *BO = cast<Instruction>(Builder.CreateBinOp(Opcode, LHS, RHS));
236   BO->setDebugLoc(Loc);
237   if (Flags & SCEV::FlagNUW)
238     BO->setHasNoUnsignedWrap();
239   if (Flags & SCEV::FlagNSW)
240     BO->setHasNoSignedWrap();
241   rememberInstruction(BO);
242 
243   return BO;
244 }
245 
246 /// FactorOutConstant - Test if S is divisible by Factor, using signed
247 /// division. If so, update S with Factor divided out and return true.
248 /// S need not be evenly divisible if a reasonable remainder can be
249 /// computed.
FactorOutConstant(const SCEV * & S,const SCEV * & Remainder,const SCEV * Factor,ScalarEvolution & SE,const DataLayout & DL)250 static bool FactorOutConstant(const SCEV *&S, const SCEV *&Remainder,
251                               const SCEV *Factor, ScalarEvolution &SE,
252                               const DataLayout &DL) {
253   // Everything is divisible by one.
254   if (Factor->isOne())
255     return true;
256 
257   // x/x == 1.
258   if (S == Factor) {
259     S = SE.getConstant(S->getType(), 1);
260     return true;
261   }
262 
263   // For a Constant, check for a multiple of the given factor.
264   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
265     // 0/x == 0.
266     if (C->isZero())
267       return true;
268     // Check for divisibility.
269     if (const SCEVConstant *FC = dyn_cast<SCEVConstant>(Factor)) {
270       ConstantInt *CI =
271           ConstantInt::get(SE.getContext(), C->getAPInt().sdiv(FC->getAPInt()));
272       // If the quotient is zero and the remainder is non-zero, reject
273       // the value at this scale. It will be considered for subsequent
274       // smaller scales.
275       if (!CI->isZero()) {
276         const SCEV *Div = SE.getConstant(CI);
277         S = Div;
278         Remainder = SE.getAddExpr(
279             Remainder, SE.getConstant(C->getAPInt().srem(FC->getAPInt())));
280         return true;
281       }
282     }
283   }
284 
285   // In a Mul, check if there is a constant operand which is a multiple
286   // of the given factor.
287   if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
288     // Size is known, check if there is a constant operand which is a multiple
289     // of the given factor. If so, we can factor it.
290     if (const SCEVConstant *FC = dyn_cast<SCEVConstant>(Factor))
291       if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0)))
292         if (!C->getAPInt().srem(FC->getAPInt())) {
293           SmallVector<const SCEV *, 4> NewMulOps(M->op_begin(), M->op_end());
294           NewMulOps[0] = SE.getConstant(C->getAPInt().sdiv(FC->getAPInt()));
295           S = SE.getMulExpr(NewMulOps);
296           return true;
297         }
298   }
299 
300   // In an AddRec, check if both start and step are divisible.
301   if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
302     const SCEV *Step = A->getStepRecurrence(SE);
303     const SCEV *StepRem = SE.getConstant(Step->getType(), 0);
304     if (!FactorOutConstant(Step, StepRem, Factor, SE, DL))
305       return false;
306     if (!StepRem->isZero())
307       return false;
308     const SCEV *Start = A->getStart();
309     if (!FactorOutConstant(Start, Remainder, Factor, SE, DL))
310       return false;
311     S = SE.getAddRecExpr(Start, Step, A->getLoop(),
312                          A->getNoWrapFlags(SCEV::FlagNW));
313     return true;
314   }
315 
316   return false;
317 }
318 
319 /// SimplifyAddOperands - Sort and simplify a list of add operands. NumAddRecs
320 /// is the number of SCEVAddRecExprs present, which are kept at the end of
321 /// the list.
322 ///
SimplifyAddOperands(SmallVectorImpl<const SCEV * > & Ops,Type * Ty,ScalarEvolution & SE)323 static void SimplifyAddOperands(SmallVectorImpl<const SCEV *> &Ops,
324                                 Type *Ty,
325                                 ScalarEvolution &SE) {
326   unsigned NumAddRecs = 0;
327   for (unsigned i = Ops.size(); i > 0 && isa<SCEVAddRecExpr>(Ops[i-1]); --i)
328     ++NumAddRecs;
329   // Group Ops into non-addrecs and addrecs.
330   SmallVector<const SCEV *, 8> NoAddRecs(Ops.begin(), Ops.end() - NumAddRecs);
331   SmallVector<const SCEV *, 8> AddRecs(Ops.end() - NumAddRecs, Ops.end());
332   // Let ScalarEvolution sort and simplify the non-addrecs list.
333   const SCEV *Sum = NoAddRecs.empty() ?
334                     SE.getConstant(Ty, 0) :
335                     SE.getAddExpr(NoAddRecs);
336   // If it returned an add, use the operands. Otherwise it simplified
337   // the sum into a single value, so just use that.
338   Ops.clear();
339   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Sum))
340     Ops.append(Add->op_begin(), Add->op_end());
341   else if (!Sum->isZero())
342     Ops.push_back(Sum);
343   // Then append the addrecs.
344   Ops.append(AddRecs.begin(), AddRecs.end());
345 }
346 
347 /// SplitAddRecs - Flatten a list of add operands, moving addrec start values
348 /// out to the top level. For example, convert {a + b,+,c} to a, b, {0,+,d}.
349 /// This helps expose more opportunities for folding parts of the expressions
350 /// into GEP indices.
351 ///
SplitAddRecs(SmallVectorImpl<const SCEV * > & Ops,Type * Ty,ScalarEvolution & SE)352 static void SplitAddRecs(SmallVectorImpl<const SCEV *> &Ops,
353                          Type *Ty,
354                          ScalarEvolution &SE) {
355   // Find the addrecs.
356   SmallVector<const SCEV *, 8> AddRecs;
357   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
358     while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i])) {
359       const SCEV *Start = A->getStart();
360       if (Start->isZero()) break;
361       const SCEV *Zero = SE.getConstant(Ty, 0);
362       AddRecs.push_back(SE.getAddRecExpr(Zero,
363                                          A->getStepRecurrence(SE),
364                                          A->getLoop(),
365                                          A->getNoWrapFlags(SCEV::FlagNW)));
366       if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Start)) {
367         Ops[i] = Zero;
368         Ops.append(Add->op_begin(), Add->op_end());
369         e += Add->getNumOperands();
370       } else {
371         Ops[i] = Start;
372       }
373     }
374   if (!AddRecs.empty()) {
375     // Add the addrecs onto the end of the list.
376     Ops.append(AddRecs.begin(), AddRecs.end());
377     // Resort the operand list, moving any constants to the front.
378     SimplifyAddOperands(Ops, Ty, SE);
379   }
380 }
381 
382 /// expandAddToGEP - Expand an addition expression with a pointer type into
383 /// a GEP instead of using ptrtoint+arithmetic+inttoptr. This helps
384 /// BasicAliasAnalysis and other passes analyze the result. See the rules
385 /// for getelementptr vs. inttoptr in
386 /// http://llvm.org/docs/LangRef.html#pointeraliasing
387 /// for details.
388 ///
389 /// Design note: The correctness of using getelementptr here depends on
390 /// ScalarEvolution not recognizing inttoptr and ptrtoint operators, as
391 /// they may introduce pointer arithmetic which may not be safely converted
392 /// into getelementptr.
393 ///
394 /// Design note: It might seem desirable for this function to be more
395 /// loop-aware. If some of the indices are loop-invariant while others
396 /// aren't, it might seem desirable to emit multiple GEPs, keeping the
397 /// loop-invariant portions of the overall computation outside the loop.
398 /// However, there are a few reasons this is not done here. Hoisting simple
399 /// arithmetic is a low-level optimization that often isn't very
400 /// important until late in the optimization process. In fact, passes
401 /// like InstructionCombining will combine GEPs, even if it means
402 /// pushing loop-invariant computation down into loops, so even if the
403 /// GEPs were split here, the work would quickly be undone. The
404 /// LoopStrengthReduction pass, which is usually run quite late (and
405 /// after the last InstructionCombining pass), takes care of hoisting
406 /// loop-invariant portions of expressions, after considering what
407 /// can be folded using target addressing modes.
408 ///
expandAddToGEP(const SCEV * const * op_begin,const SCEV * const * op_end,PointerType * PTy,Type * Ty,Value * V)409 Value *SCEVExpander::expandAddToGEP(const SCEV *const *op_begin,
410                                     const SCEV *const *op_end,
411                                     PointerType *PTy,
412                                     Type *Ty,
413                                     Value *V) {
414   Type *OriginalElTy = PTy->getElementType();
415   Type *ElTy = OriginalElTy;
416   SmallVector<Value *, 4> GepIndices;
417   SmallVector<const SCEV *, 8> Ops(op_begin, op_end);
418   bool AnyNonZeroIndices = false;
419 
420   // Split AddRecs up into parts as either of the parts may be usable
421   // without the other.
422   SplitAddRecs(Ops, Ty, SE);
423 
424   Type *IntIdxTy = DL.getIndexType(PTy);
425 
426   // Descend down the pointer's type and attempt to convert the other
427   // operands into GEP indices, at each level. The first index in a GEP
428   // indexes into the array implied by the pointer operand; the rest of
429   // the indices index into the element or field type selected by the
430   // preceding index.
431   for (;;) {
432     // If the scale size is not 0, attempt to factor out a scale for
433     // array indexing.
434     SmallVector<const SCEV *, 8> ScaledOps;
435     if (ElTy->isSized()) {
436       const SCEV *ElSize = SE.getSizeOfExpr(IntIdxTy, ElTy);
437       if (!ElSize->isZero()) {
438         SmallVector<const SCEV *, 8> NewOps;
439         for (const SCEV *Op : Ops) {
440           const SCEV *Remainder = SE.getConstant(Ty, 0);
441           if (FactorOutConstant(Op, Remainder, ElSize, SE, DL)) {
442             // Op now has ElSize factored out.
443             ScaledOps.push_back(Op);
444             if (!Remainder->isZero())
445               NewOps.push_back(Remainder);
446             AnyNonZeroIndices = true;
447           } else {
448             // The operand was not divisible, so add it to the list of operands
449             // we'll scan next iteration.
450             NewOps.push_back(Op);
451           }
452         }
453         // If we made any changes, update Ops.
454         if (!ScaledOps.empty()) {
455           Ops = NewOps;
456           SimplifyAddOperands(Ops, Ty, SE);
457         }
458       }
459     }
460 
461     // Record the scaled array index for this level of the type. If
462     // we didn't find any operands that could be factored, tentatively
463     // assume that element zero was selected (since the zero offset
464     // would obviously be folded away).
465     Value *Scaled = ScaledOps.empty() ?
466                     Constant::getNullValue(Ty) :
467                     expandCodeFor(SE.getAddExpr(ScaledOps), Ty);
468     GepIndices.push_back(Scaled);
469 
470     // Collect struct field index operands.
471     while (StructType *STy = dyn_cast<StructType>(ElTy)) {
472       bool FoundFieldNo = false;
473       // An empty struct has no fields.
474       if (STy->getNumElements() == 0) break;
475       // Field offsets are known. See if a constant offset falls within any of
476       // the struct fields.
477       if (Ops.empty())
478         break;
479       if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0]))
480         if (SE.getTypeSizeInBits(C->getType()) <= 64) {
481           const StructLayout &SL = *DL.getStructLayout(STy);
482           uint64_t FullOffset = C->getValue()->getZExtValue();
483           if (FullOffset < SL.getSizeInBytes()) {
484             unsigned ElIdx = SL.getElementContainingOffset(FullOffset);
485             GepIndices.push_back(
486                 ConstantInt::get(Type::getInt32Ty(Ty->getContext()), ElIdx));
487             ElTy = STy->getTypeAtIndex(ElIdx);
488             Ops[0] =
489                 SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx));
490             AnyNonZeroIndices = true;
491             FoundFieldNo = true;
492           }
493         }
494       // If no struct field offsets were found, tentatively assume that
495       // field zero was selected (since the zero offset would obviously
496       // be folded away).
497       if (!FoundFieldNo) {
498         ElTy = STy->getTypeAtIndex(0u);
499         GepIndices.push_back(
500           Constant::getNullValue(Type::getInt32Ty(Ty->getContext())));
501       }
502     }
503 
504     if (ArrayType *ATy = dyn_cast<ArrayType>(ElTy))
505       ElTy = ATy->getElementType();
506     else
507       // FIXME: Handle VectorType.
508       // E.g., If ElTy is scalable vector, then ElSize is not a compile-time
509       // constant, therefore can not be factored out. The generated IR is less
510       // ideal with base 'V' cast to i8* and do ugly getelementptr over that.
511       break;
512   }
513 
514   // If none of the operands were convertible to proper GEP indices, cast
515   // the base to i8* and do an ugly getelementptr with that. It's still
516   // better than ptrtoint+arithmetic+inttoptr at least.
517   if (!AnyNonZeroIndices) {
518     // Cast the base to i8*.
519     V = InsertNoopCastOfTo(V,
520        Type::getInt8PtrTy(Ty->getContext(), PTy->getAddressSpace()));
521 
522     assert(!isa<Instruction>(V) ||
523            SE.DT.dominates(cast<Instruction>(V), &*Builder.GetInsertPoint()));
524 
525     // Expand the operands for a plain byte offset.
526     Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty);
527 
528     // Fold a GEP with constant operands.
529     if (Constant *CLHS = dyn_cast<Constant>(V))
530       if (Constant *CRHS = dyn_cast<Constant>(Idx))
531         return ConstantExpr::getGetElementPtr(Type::getInt8Ty(Ty->getContext()),
532                                               CLHS, CRHS);
533 
534     // Do a quick scan to see if we have this GEP nearby.  If so, reuse it.
535     unsigned ScanLimit = 6;
536     BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
537     // Scanning starts from the last instruction before the insertion point.
538     BasicBlock::iterator IP = Builder.GetInsertPoint();
539     if (IP != BlockBegin) {
540       --IP;
541       for (; ScanLimit; --IP, --ScanLimit) {
542         // Don't count dbg.value against the ScanLimit, to avoid perturbing the
543         // generated code.
544         if (isa<DbgInfoIntrinsic>(IP))
545           ScanLimit++;
546         if (IP->getOpcode() == Instruction::GetElementPtr &&
547             IP->getOperand(0) == V && IP->getOperand(1) == Idx)
548           return &*IP;
549         if (IP == BlockBegin) break;
550       }
551     }
552 
553     // Save the original insertion point so we can restore it when we're done.
554     SCEVInsertPointGuard Guard(Builder, this);
555 
556     // Move the insertion point out of as many loops as we can.
557     while (const Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) {
558       if (!L->isLoopInvariant(V) || !L->isLoopInvariant(Idx)) break;
559       BasicBlock *Preheader = L->getLoopPreheader();
560       if (!Preheader) break;
561 
562       // Ok, move up a level.
563       Builder.SetInsertPoint(Preheader->getTerminator());
564     }
565 
566     // Emit a GEP.
567     Value *GEP = Builder.CreateGEP(Builder.getInt8Ty(), V, Idx, "uglygep");
568     rememberInstruction(GEP);
569 
570     return GEP;
571   }
572 
573   {
574     SCEVInsertPointGuard Guard(Builder, this);
575 
576     // Move the insertion point out of as many loops as we can.
577     while (const Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) {
578       if (!L->isLoopInvariant(V)) break;
579 
580       bool AnyIndexNotLoopInvariant = any_of(
581           GepIndices, [L](Value *Op) { return !L->isLoopInvariant(Op); });
582 
583       if (AnyIndexNotLoopInvariant)
584         break;
585 
586       BasicBlock *Preheader = L->getLoopPreheader();
587       if (!Preheader) break;
588 
589       // Ok, move up a level.
590       Builder.SetInsertPoint(Preheader->getTerminator());
591     }
592 
593     // Insert a pretty getelementptr. Note that this GEP is not marked inbounds,
594     // because ScalarEvolution may have changed the address arithmetic to
595     // compute a value which is beyond the end of the allocated object.
596     Value *Casted = V;
597     if (V->getType() != PTy)
598       Casted = InsertNoopCastOfTo(Casted, PTy);
599     Value *GEP = Builder.CreateGEP(OriginalElTy, Casted, GepIndices, "scevgep");
600     Ops.push_back(SE.getUnknown(GEP));
601     rememberInstruction(GEP);
602   }
603 
604   return expand(SE.getAddExpr(Ops));
605 }
606 
expandAddToGEP(const SCEV * Op,PointerType * PTy,Type * Ty,Value * V)607 Value *SCEVExpander::expandAddToGEP(const SCEV *Op, PointerType *PTy, Type *Ty,
608                                     Value *V) {
609   const SCEV *const Ops[1] = {Op};
610   return expandAddToGEP(Ops, Ops + 1, PTy, Ty, V);
611 }
612 
613 /// PickMostRelevantLoop - Given two loops pick the one that's most relevant for
614 /// SCEV expansion. If they are nested, this is the most nested. If they are
615 /// neighboring, pick the later.
PickMostRelevantLoop(const Loop * A,const Loop * B,DominatorTree & DT)616 static const Loop *PickMostRelevantLoop(const Loop *A, const Loop *B,
617                                         DominatorTree &DT) {
618   if (!A) return B;
619   if (!B) return A;
620   if (A->contains(B)) return B;
621   if (B->contains(A)) return A;
622   if (DT.dominates(A->getHeader(), B->getHeader())) return B;
623   if (DT.dominates(B->getHeader(), A->getHeader())) return A;
624   return A; // Arbitrarily break the tie.
625 }
626 
627 /// getRelevantLoop - Get the most relevant loop associated with the given
628 /// expression, according to PickMostRelevantLoop.
getRelevantLoop(const SCEV * S)629 const Loop *SCEVExpander::getRelevantLoop(const SCEV *S) {
630   // Test whether we've already computed the most relevant loop for this SCEV.
631   auto Pair = RelevantLoops.insert(std::make_pair(S, nullptr));
632   if (!Pair.second)
633     return Pair.first->second;
634 
635   if (isa<SCEVConstant>(S))
636     // A constant has no relevant loops.
637     return nullptr;
638   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
639     if (const Instruction *I = dyn_cast<Instruction>(U->getValue()))
640       return Pair.first->second = SE.LI.getLoopFor(I->getParent());
641     // A non-instruction has no relevant loops.
642     return nullptr;
643   }
644   if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S)) {
645     const Loop *L = nullptr;
646     if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S))
647       L = AR->getLoop();
648     for (const SCEV *Op : N->operands())
649       L = PickMostRelevantLoop(L, getRelevantLoop(Op), SE.DT);
650     return RelevantLoops[N] = L;
651   }
652   if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S)) {
653     const Loop *Result = getRelevantLoop(C->getOperand());
654     return RelevantLoops[C] = Result;
655   }
656   if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) {
657     const Loop *Result = PickMostRelevantLoop(
658         getRelevantLoop(D->getLHS()), getRelevantLoop(D->getRHS()), SE.DT);
659     return RelevantLoops[D] = Result;
660   }
661   llvm_unreachable("Unexpected SCEV type!");
662 }
663 
664 namespace {
665 
666 /// LoopCompare - Compare loops by PickMostRelevantLoop.
667 class LoopCompare {
668   DominatorTree &DT;
669 public:
LoopCompare(DominatorTree & dt)670   explicit LoopCompare(DominatorTree &dt) : DT(dt) {}
671 
operator ()(std::pair<const Loop *,const SCEV * > LHS,std::pair<const Loop *,const SCEV * > RHS) const672   bool operator()(std::pair<const Loop *, const SCEV *> LHS,
673                   std::pair<const Loop *, const SCEV *> RHS) const {
674     // Keep pointer operands sorted at the end.
675     if (LHS.second->getType()->isPointerTy() !=
676         RHS.second->getType()->isPointerTy())
677       return LHS.second->getType()->isPointerTy();
678 
679     // Compare loops with PickMostRelevantLoop.
680     if (LHS.first != RHS.first)
681       return PickMostRelevantLoop(LHS.first, RHS.first, DT) != LHS.first;
682 
683     // If one operand is a non-constant negative and the other is not,
684     // put the non-constant negative on the right so that a sub can
685     // be used instead of a negate and add.
686     if (LHS.second->isNonConstantNegative()) {
687       if (!RHS.second->isNonConstantNegative())
688         return false;
689     } else if (RHS.second->isNonConstantNegative())
690       return true;
691 
692     // Otherwise they are equivalent according to this comparison.
693     return false;
694   }
695 };
696 
697 }
698 
visitAddExpr(const SCEVAddExpr * S)699 Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
700   Type *Ty = SE.getEffectiveSCEVType(S->getType());
701 
702   // Collect all the add operands in a loop, along with their associated loops.
703   // Iterate in reverse so that constants are emitted last, all else equal, and
704   // so that pointer operands are inserted first, which the code below relies on
705   // to form more involved GEPs.
706   SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops;
707   for (std::reverse_iterator<SCEVAddExpr::op_iterator> I(S->op_end()),
708        E(S->op_begin()); I != E; ++I)
709     OpsAndLoops.push_back(std::make_pair(getRelevantLoop(*I), *I));
710 
711   // Sort by loop. Use a stable sort so that constants follow non-constants and
712   // pointer operands precede non-pointer operands.
713   llvm::stable_sort(OpsAndLoops, LoopCompare(SE.DT));
714 
715   // Emit instructions to add all the operands. Hoist as much as possible
716   // out of loops, and form meaningful getelementptrs where possible.
717   Value *Sum = nullptr;
718   for (auto I = OpsAndLoops.begin(), E = OpsAndLoops.end(); I != E;) {
719     const Loop *CurLoop = I->first;
720     const SCEV *Op = I->second;
721     if (!Sum) {
722       // This is the first operand. Just expand it.
723       Sum = expand(Op);
724       ++I;
725     } else if (PointerType *PTy = dyn_cast<PointerType>(Sum->getType())) {
726       // The running sum expression is a pointer. Try to form a getelementptr
727       // at this level with that as the base.
728       SmallVector<const SCEV *, 4> NewOps;
729       for (; I != E && I->first == CurLoop; ++I) {
730         // If the operand is SCEVUnknown and not instructions, peek through
731         // it, to enable more of it to be folded into the GEP.
732         const SCEV *X = I->second;
733         if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(X))
734           if (!isa<Instruction>(U->getValue()))
735             X = SE.getSCEV(U->getValue());
736         NewOps.push_back(X);
737       }
738       Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, Sum);
739     } else if (PointerType *PTy = dyn_cast<PointerType>(Op->getType())) {
740       // The running sum is an integer, and there's a pointer at this level.
741       // Try to form a getelementptr. If the running sum is instructions,
742       // use a SCEVUnknown to avoid re-analyzing them.
743       SmallVector<const SCEV *, 4> NewOps;
744       NewOps.push_back(isa<Instruction>(Sum) ? SE.getUnknown(Sum) :
745                                                SE.getSCEV(Sum));
746       for (++I; I != E && I->first == CurLoop; ++I)
747         NewOps.push_back(I->second);
748       Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, expand(Op));
749     } else if (Op->isNonConstantNegative()) {
750       // Instead of doing a negate and add, just do a subtract.
751       Value *W = expandCodeFor(SE.getNegativeSCEV(Op), Ty);
752       Sum = InsertNoopCastOfTo(Sum, Ty);
753       Sum = InsertBinop(Instruction::Sub, Sum, W, SCEV::FlagAnyWrap,
754                         /*IsSafeToHoist*/ true);
755       ++I;
756     } else {
757       // A simple add.
758       Value *W = expandCodeFor(Op, Ty);
759       Sum = InsertNoopCastOfTo(Sum, Ty);
760       // Canonicalize a constant to the RHS.
761       if (isa<Constant>(Sum)) std::swap(Sum, W);
762       Sum = InsertBinop(Instruction::Add, Sum, W, S->getNoWrapFlags(),
763                         /*IsSafeToHoist*/ true);
764       ++I;
765     }
766   }
767 
768   return Sum;
769 }
770 
visitMulExpr(const SCEVMulExpr * S)771 Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) {
772   Type *Ty = SE.getEffectiveSCEVType(S->getType());
773 
774   // Collect all the mul operands in a loop, along with their associated loops.
775   // Iterate in reverse so that constants are emitted last, all else equal.
776   SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops;
777   for (std::reverse_iterator<SCEVMulExpr::op_iterator> I(S->op_end()),
778        E(S->op_begin()); I != E; ++I)
779     OpsAndLoops.push_back(std::make_pair(getRelevantLoop(*I), *I));
780 
781   // Sort by loop. Use a stable sort so that constants follow non-constants.
782   llvm::stable_sort(OpsAndLoops, LoopCompare(SE.DT));
783 
784   // Emit instructions to mul all the operands. Hoist as much as possible
785   // out of loops.
786   Value *Prod = nullptr;
787   auto I = OpsAndLoops.begin();
788 
789   // Expand the calculation of X pow N in the following manner:
790   // Let N = P1 + P2 + ... + PK, where all P are powers of 2. Then:
791   // X pow N = (X pow P1) * (X pow P2) * ... * (X pow PK).
792   const auto ExpandOpBinPowN = [this, &I, &OpsAndLoops, &Ty]() {
793     auto E = I;
794     // Calculate how many times the same operand from the same loop is included
795     // into this power.
796     uint64_t Exponent = 0;
797     const uint64_t MaxExponent = UINT64_MAX >> 1;
798     // No one sane will ever try to calculate such huge exponents, but if we
799     // need this, we stop on UINT64_MAX / 2 because we need to exit the loop
800     // below when the power of 2 exceeds our Exponent, and we want it to be
801     // 1u << 31 at most to not deal with unsigned overflow.
802     while (E != OpsAndLoops.end() && *I == *E && Exponent != MaxExponent) {
803       ++Exponent;
804       ++E;
805     }
806     assert(Exponent > 0 && "Trying to calculate a zeroth exponent of operand?");
807 
808     // Calculate powers with exponents 1, 2, 4, 8 etc. and include those of them
809     // that are needed into the result.
810     Value *P = expandCodeFor(I->second, Ty);
811     Value *Result = nullptr;
812     if (Exponent & 1)
813       Result = P;
814     for (uint64_t BinExp = 2; BinExp <= Exponent; BinExp <<= 1) {
815       P = InsertBinop(Instruction::Mul, P, P, SCEV::FlagAnyWrap,
816                       /*IsSafeToHoist*/ true);
817       if (Exponent & BinExp)
818         Result = Result ? InsertBinop(Instruction::Mul, Result, P,
819                                       SCEV::FlagAnyWrap,
820                                       /*IsSafeToHoist*/ true)
821                         : P;
822     }
823 
824     I = E;
825     assert(Result && "Nothing was expanded?");
826     return Result;
827   };
828 
829   while (I != OpsAndLoops.end()) {
830     if (!Prod) {
831       // This is the first operand. Just expand it.
832       Prod = ExpandOpBinPowN();
833     } else if (I->second->isAllOnesValue()) {
834       // Instead of doing a multiply by negative one, just do a negate.
835       Prod = InsertNoopCastOfTo(Prod, Ty);
836       Prod = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), Prod,
837                          SCEV::FlagAnyWrap, /*IsSafeToHoist*/ true);
838       ++I;
839     } else {
840       // A simple mul.
841       Value *W = ExpandOpBinPowN();
842       Prod = InsertNoopCastOfTo(Prod, Ty);
843       // Canonicalize a constant to the RHS.
844       if (isa<Constant>(Prod)) std::swap(Prod, W);
845       const APInt *RHS;
846       if (match(W, m_Power2(RHS))) {
847         // Canonicalize Prod*(1<<C) to Prod<<C.
848         assert(!Ty->isVectorTy() && "vector types are not SCEVable");
849         auto NWFlags = S->getNoWrapFlags();
850         // clear nsw flag if shl will produce poison value.
851         if (RHS->logBase2() == RHS->getBitWidth() - 1)
852           NWFlags = ScalarEvolution::clearFlags(NWFlags, SCEV::FlagNSW);
853         Prod = InsertBinop(Instruction::Shl, Prod,
854                            ConstantInt::get(Ty, RHS->logBase2()), NWFlags,
855                            /*IsSafeToHoist*/ true);
856       } else {
857         Prod = InsertBinop(Instruction::Mul, Prod, W, S->getNoWrapFlags(),
858                            /*IsSafeToHoist*/ true);
859       }
860     }
861   }
862 
863   return Prod;
864 }
865 
visitUDivExpr(const SCEVUDivExpr * S)866 Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
867   Type *Ty = SE.getEffectiveSCEVType(S->getType());
868 
869   Value *LHS = expandCodeFor(S->getLHS(), Ty);
870   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) {
871     const APInt &RHS = SC->getAPInt();
872     if (RHS.isPowerOf2())
873       return InsertBinop(Instruction::LShr, LHS,
874                          ConstantInt::get(Ty, RHS.logBase2()),
875                          SCEV::FlagAnyWrap, /*IsSafeToHoist*/ true);
876   }
877 
878   Value *RHS = expandCodeFor(S->getRHS(), Ty);
879   return InsertBinop(Instruction::UDiv, LHS, RHS, SCEV::FlagAnyWrap,
880                      /*IsSafeToHoist*/ SE.isKnownNonZero(S->getRHS()));
881 }
882 
883 /// Move parts of Base into Rest to leave Base with the minimal
884 /// expression that provides a pointer operand suitable for a
885 /// GEP expansion.
ExposePointerBase(const SCEV * & Base,const SCEV * & Rest,ScalarEvolution & SE)886 static void ExposePointerBase(const SCEV *&Base, const SCEV *&Rest,
887                               ScalarEvolution &SE) {
888   while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) {
889     Base = A->getStart();
890     Rest = SE.getAddExpr(Rest,
891                          SE.getAddRecExpr(SE.getConstant(A->getType(), 0),
892                                           A->getStepRecurrence(SE),
893                                           A->getLoop(),
894                                           A->getNoWrapFlags(SCEV::FlagNW)));
895   }
896   if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) {
897     Base = A->getOperand(A->getNumOperands()-1);
898     SmallVector<const SCEV *, 8> NewAddOps(A->op_begin(), A->op_end());
899     NewAddOps.back() = Rest;
900     Rest = SE.getAddExpr(NewAddOps);
901     ExposePointerBase(Base, Rest, SE);
902   }
903 }
904 
905 /// Determine if this is a well-behaved chain of instructions leading back to
906 /// the PHI. If so, it may be reused by expanded expressions.
isNormalAddRecExprPHI(PHINode * PN,Instruction * IncV,const Loop * L)907 bool SCEVExpander::isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV,
908                                          const Loop *L) {
909   if (IncV->getNumOperands() == 0 || isa<PHINode>(IncV) ||
910       (isa<CastInst>(IncV) && !isa<BitCastInst>(IncV)))
911     return false;
912   // If any of the operands don't dominate the insert position, bail.
913   // Addrec operands are always loop-invariant, so this can only happen
914   // if there are instructions which haven't been hoisted.
915   if (L == IVIncInsertLoop) {
916     for (User::op_iterator OI = IncV->op_begin()+1,
917            OE = IncV->op_end(); OI != OE; ++OI)
918       if (Instruction *OInst = dyn_cast<Instruction>(OI))
919         if (!SE.DT.dominates(OInst, IVIncInsertPos))
920           return false;
921   }
922   // Advance to the next instruction.
923   IncV = dyn_cast<Instruction>(IncV->getOperand(0));
924   if (!IncV)
925     return false;
926 
927   if (IncV->mayHaveSideEffects())
928     return false;
929 
930   if (IncV == PN)
931     return true;
932 
933   return isNormalAddRecExprPHI(PN, IncV, L);
934 }
935 
936 /// getIVIncOperand returns an induction variable increment's induction
937 /// variable operand.
938 ///
939 /// If allowScale is set, any type of GEP is allowed as long as the nonIV
940 /// operands dominate InsertPos.
941 ///
942 /// If allowScale is not set, ensure that a GEP increment conforms to one of the
943 /// simple patterns generated by getAddRecExprPHILiterally and
944 /// expandAddtoGEP. If the pattern isn't recognized, return NULL.
getIVIncOperand(Instruction * IncV,Instruction * InsertPos,bool allowScale)945 Instruction *SCEVExpander::getIVIncOperand(Instruction *IncV,
946                                            Instruction *InsertPos,
947                                            bool allowScale) {
948   if (IncV == InsertPos)
949     return nullptr;
950 
951   switch (IncV->getOpcode()) {
952   default:
953     return nullptr;
954   // Check for a simple Add/Sub or GEP of a loop invariant step.
955   case Instruction::Add:
956   case Instruction::Sub: {
957     Instruction *OInst = dyn_cast<Instruction>(IncV->getOperand(1));
958     if (!OInst || SE.DT.dominates(OInst, InsertPos))
959       return dyn_cast<Instruction>(IncV->getOperand(0));
960     return nullptr;
961   }
962   case Instruction::BitCast:
963     return dyn_cast<Instruction>(IncV->getOperand(0));
964   case Instruction::GetElementPtr:
965     for (auto I = IncV->op_begin() + 1, E = IncV->op_end(); I != E; ++I) {
966       if (isa<Constant>(*I))
967         continue;
968       if (Instruction *OInst = dyn_cast<Instruction>(*I)) {
969         if (!SE.DT.dominates(OInst, InsertPos))
970           return nullptr;
971       }
972       if (allowScale) {
973         // allow any kind of GEP as long as it can be hoisted.
974         continue;
975       }
976       // This must be a pointer addition of constants (pretty), which is already
977       // handled, or some number of address-size elements (ugly). Ugly geps
978       // have 2 operands. i1* is used by the expander to represent an
979       // address-size element.
980       if (IncV->getNumOperands() != 2)
981         return nullptr;
982       unsigned AS = cast<PointerType>(IncV->getType())->getAddressSpace();
983       if (IncV->getType() != Type::getInt1PtrTy(SE.getContext(), AS)
984           && IncV->getType() != Type::getInt8PtrTy(SE.getContext(), AS))
985         return nullptr;
986       break;
987     }
988     return dyn_cast<Instruction>(IncV->getOperand(0));
989   }
990 }
991 
992 /// If the insert point of the current builder or any of the builders on the
993 /// stack of saved builders has 'I' as its insert point, update it to point to
994 /// the instruction after 'I'.  This is intended to be used when the instruction
995 /// 'I' is being moved.  If this fixup is not done and 'I' is moved to a
996 /// different block, the inconsistent insert point (with a mismatched
997 /// Instruction and Block) can lead to an instruction being inserted in a block
998 /// other than its parent.
fixupInsertPoints(Instruction * I)999 void SCEVExpander::fixupInsertPoints(Instruction *I) {
1000   BasicBlock::iterator It(*I);
1001   BasicBlock::iterator NewInsertPt = std::next(It);
1002   if (Builder.GetInsertPoint() == It)
1003     Builder.SetInsertPoint(&*NewInsertPt);
1004   for (auto *InsertPtGuard : InsertPointGuards)
1005     if (InsertPtGuard->GetInsertPoint() == It)
1006       InsertPtGuard->SetInsertPoint(NewInsertPt);
1007 }
1008 
1009 /// hoistStep - Attempt to hoist a simple IV increment above InsertPos to make
1010 /// it available to other uses in this loop. Recursively hoist any operands,
1011 /// until we reach a value that dominates InsertPos.
hoistIVInc(Instruction * IncV,Instruction * InsertPos)1012 bool SCEVExpander::hoistIVInc(Instruction *IncV, Instruction *InsertPos) {
1013   if (SE.DT.dominates(IncV, InsertPos))
1014       return true;
1015 
1016   // InsertPos must itself dominate IncV so that IncV's new position satisfies
1017   // its existing users.
1018   if (isa<PHINode>(InsertPos) ||
1019       !SE.DT.dominates(InsertPos->getParent(), IncV->getParent()))
1020     return false;
1021 
1022   if (!SE.LI.movementPreservesLCSSAForm(IncV, InsertPos))
1023     return false;
1024 
1025   // Check that the chain of IV operands leading back to Phi can be hoisted.
1026   SmallVector<Instruction*, 4> IVIncs;
1027   for(;;) {
1028     Instruction *Oper = getIVIncOperand(IncV, InsertPos, /*allowScale*/true);
1029     if (!Oper)
1030       return false;
1031     // IncV is safe to hoist.
1032     IVIncs.push_back(IncV);
1033     IncV = Oper;
1034     if (SE.DT.dominates(IncV, InsertPos))
1035       break;
1036   }
1037   for (auto I = IVIncs.rbegin(), E = IVIncs.rend(); I != E; ++I) {
1038     fixupInsertPoints(*I);
1039     (*I)->moveBefore(InsertPos);
1040   }
1041   return true;
1042 }
1043 
1044 /// Determine if this cyclic phi is in a form that would have been generated by
1045 /// LSR. We don't care if the phi was actually expanded in this pass, as long
1046 /// as it is in a low-cost form, for example, no implied multiplication. This
1047 /// should match any patterns generated by getAddRecExprPHILiterally and
1048 /// expandAddtoGEP.
isExpandedAddRecExprPHI(PHINode * PN,Instruction * IncV,const Loop * L)1049 bool SCEVExpander::isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV,
1050                                            const Loop *L) {
1051   for(Instruction *IVOper = IncV;
1052       (IVOper = getIVIncOperand(IVOper, L->getLoopPreheader()->getTerminator(),
1053                                 /*allowScale=*/false));) {
1054     if (IVOper == PN)
1055       return true;
1056   }
1057   return false;
1058 }
1059 
1060 /// expandIVInc - Expand an IV increment at Builder's current InsertPos.
1061 /// Typically this is the LatchBlock terminator or IVIncInsertPos, but we may
1062 /// need to materialize IV increments elsewhere to handle difficult situations.
expandIVInc(PHINode * PN,Value * StepV,const Loop * L,Type * ExpandTy,Type * IntTy,bool useSubtract)1063 Value *SCEVExpander::expandIVInc(PHINode *PN, Value *StepV, const Loop *L,
1064                                  Type *ExpandTy, Type *IntTy,
1065                                  bool useSubtract) {
1066   Value *IncV;
1067   // If the PHI is a pointer, use a GEP, otherwise use an add or sub.
1068   if (ExpandTy->isPointerTy()) {
1069     PointerType *GEPPtrTy = cast<PointerType>(ExpandTy);
1070     // If the step isn't constant, don't use an implicitly scaled GEP, because
1071     // that would require a multiply inside the loop.
1072     if (!isa<ConstantInt>(StepV))
1073       GEPPtrTy = PointerType::get(Type::getInt1Ty(SE.getContext()),
1074                                   GEPPtrTy->getAddressSpace());
1075     IncV = expandAddToGEP(SE.getSCEV(StepV), GEPPtrTy, IntTy, PN);
1076     if (IncV->getType() != PN->getType()) {
1077       IncV = Builder.CreateBitCast(IncV, PN->getType());
1078       rememberInstruction(IncV);
1079     }
1080   } else {
1081     IncV = useSubtract ?
1082       Builder.CreateSub(PN, StepV, Twine(IVName) + ".iv.next") :
1083       Builder.CreateAdd(PN, StepV, Twine(IVName) + ".iv.next");
1084     rememberInstruction(IncV);
1085   }
1086   return IncV;
1087 }
1088 
1089 /// Hoist the addrec instruction chain rooted in the loop phi above the
1090 /// position. This routine assumes that this is possible (has been checked).
hoistBeforePos(DominatorTree * DT,Instruction * InstToHoist,Instruction * Pos,PHINode * LoopPhi)1091 void SCEVExpander::hoistBeforePos(DominatorTree *DT, Instruction *InstToHoist,
1092                                   Instruction *Pos, PHINode *LoopPhi) {
1093   do {
1094     if (DT->dominates(InstToHoist, Pos))
1095       break;
1096     // Make sure the increment is where we want it. But don't move it
1097     // down past a potential existing post-inc user.
1098     fixupInsertPoints(InstToHoist);
1099     InstToHoist->moveBefore(Pos);
1100     Pos = InstToHoist;
1101     InstToHoist = cast<Instruction>(InstToHoist->getOperand(0));
1102   } while (InstToHoist != LoopPhi);
1103 }
1104 
1105 /// Check whether we can cheaply express the requested SCEV in terms of
1106 /// the available PHI SCEV by truncation and/or inversion of the step.
canBeCheaplyTransformed(ScalarEvolution & SE,const SCEVAddRecExpr * Phi,const SCEVAddRecExpr * Requested,bool & InvertStep)1107 static bool canBeCheaplyTransformed(ScalarEvolution &SE,
1108                                     const SCEVAddRecExpr *Phi,
1109                                     const SCEVAddRecExpr *Requested,
1110                                     bool &InvertStep) {
1111   Type *PhiTy = SE.getEffectiveSCEVType(Phi->getType());
1112   Type *RequestedTy = SE.getEffectiveSCEVType(Requested->getType());
1113 
1114   if (RequestedTy->getIntegerBitWidth() > PhiTy->getIntegerBitWidth())
1115     return false;
1116 
1117   // Try truncate it if necessary.
1118   Phi = dyn_cast<SCEVAddRecExpr>(SE.getTruncateOrNoop(Phi, RequestedTy));
1119   if (!Phi)
1120     return false;
1121 
1122   // Check whether truncation will help.
1123   if (Phi == Requested) {
1124     InvertStep = false;
1125     return true;
1126   }
1127 
1128   // Check whether inverting will help: {R,+,-1} == R - {0,+,1}.
1129   if (SE.getAddExpr(Requested->getStart(),
1130                     SE.getNegativeSCEV(Requested)) == Phi) {
1131     InvertStep = true;
1132     return true;
1133   }
1134 
1135   return false;
1136 }
1137 
IsIncrementNSW(ScalarEvolution & SE,const SCEVAddRecExpr * AR)1138 static bool IsIncrementNSW(ScalarEvolution &SE, const SCEVAddRecExpr *AR) {
1139   if (!isa<IntegerType>(AR->getType()))
1140     return false;
1141 
1142   unsigned BitWidth = cast<IntegerType>(AR->getType())->getBitWidth();
1143   Type *WideTy = IntegerType::get(AR->getType()->getContext(), BitWidth * 2);
1144   const SCEV *Step = AR->getStepRecurrence(SE);
1145   const SCEV *OpAfterExtend = SE.getAddExpr(SE.getSignExtendExpr(Step, WideTy),
1146                                             SE.getSignExtendExpr(AR, WideTy));
1147   const SCEV *ExtendAfterOp =
1148     SE.getSignExtendExpr(SE.getAddExpr(AR, Step), WideTy);
1149   return ExtendAfterOp == OpAfterExtend;
1150 }
1151 
IsIncrementNUW(ScalarEvolution & SE,const SCEVAddRecExpr * AR)1152 static bool IsIncrementNUW(ScalarEvolution &SE, const SCEVAddRecExpr *AR) {
1153   if (!isa<IntegerType>(AR->getType()))
1154     return false;
1155 
1156   unsigned BitWidth = cast<IntegerType>(AR->getType())->getBitWidth();
1157   Type *WideTy = IntegerType::get(AR->getType()->getContext(), BitWidth * 2);
1158   const SCEV *Step = AR->getStepRecurrence(SE);
1159   const SCEV *OpAfterExtend = SE.getAddExpr(SE.getZeroExtendExpr(Step, WideTy),
1160                                             SE.getZeroExtendExpr(AR, WideTy));
1161   const SCEV *ExtendAfterOp =
1162     SE.getZeroExtendExpr(SE.getAddExpr(AR, Step), WideTy);
1163   return ExtendAfterOp == OpAfterExtend;
1164 }
1165 
1166 /// getAddRecExprPHILiterally - Helper for expandAddRecExprLiterally. Expand
1167 /// the base addrec, which is the addrec without any non-loop-dominating
1168 /// values, and return the PHI.
1169 PHINode *
getAddRecExprPHILiterally(const SCEVAddRecExpr * Normalized,const Loop * L,Type * ExpandTy,Type * IntTy,Type * & TruncTy,bool & InvertStep)1170 SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
1171                                         const Loop *L,
1172                                         Type *ExpandTy,
1173                                         Type *IntTy,
1174                                         Type *&TruncTy,
1175                                         bool &InvertStep) {
1176   assert((!IVIncInsertLoop||IVIncInsertPos) && "Uninitialized insert position");
1177 
1178   // Reuse a previously-inserted PHI, if present.
1179   BasicBlock *LatchBlock = L->getLoopLatch();
1180   if (LatchBlock) {
1181     PHINode *AddRecPhiMatch = nullptr;
1182     Instruction *IncV = nullptr;
1183     TruncTy = nullptr;
1184     InvertStep = false;
1185 
1186     // Only try partially matching scevs that need truncation and/or
1187     // step-inversion if we know this loop is outside the current loop.
1188     bool TryNonMatchingSCEV =
1189         IVIncInsertLoop &&
1190         SE.DT.properlyDominates(LatchBlock, IVIncInsertLoop->getHeader());
1191 
1192     for (PHINode &PN : L->getHeader()->phis()) {
1193       if (!SE.isSCEVable(PN.getType()))
1194         continue;
1195 
1196       const SCEVAddRecExpr *PhiSCEV = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(&PN));
1197       if (!PhiSCEV)
1198         continue;
1199 
1200       bool IsMatchingSCEV = PhiSCEV == Normalized;
1201       // We only handle truncation and inversion of phi recurrences for the
1202       // expanded expression if the expanded expression's loop dominates the
1203       // loop we insert to. Check now, so we can bail out early.
1204       if (!IsMatchingSCEV && !TryNonMatchingSCEV)
1205           continue;
1206 
1207       // TODO: this possibly can be reworked to avoid this cast at all.
1208       Instruction *TempIncV =
1209           dyn_cast<Instruction>(PN.getIncomingValueForBlock(LatchBlock));
1210       if (!TempIncV)
1211         continue;
1212 
1213       // Check whether we can reuse this PHI node.
1214       if (LSRMode) {
1215         if (!isExpandedAddRecExprPHI(&PN, TempIncV, L))
1216           continue;
1217         if (L == IVIncInsertLoop && !hoistIVInc(TempIncV, IVIncInsertPos))
1218           continue;
1219       } else {
1220         if (!isNormalAddRecExprPHI(&PN, TempIncV, L))
1221           continue;
1222       }
1223 
1224       // Stop if we have found an exact match SCEV.
1225       if (IsMatchingSCEV) {
1226         IncV = TempIncV;
1227         TruncTy = nullptr;
1228         InvertStep = false;
1229         AddRecPhiMatch = &PN;
1230         break;
1231       }
1232 
1233       // Try whether the phi can be translated into the requested form
1234       // (truncated and/or offset by a constant).
1235       if ((!TruncTy || InvertStep) &&
1236           canBeCheaplyTransformed(SE, PhiSCEV, Normalized, InvertStep)) {
1237         // Record the phi node. But don't stop we might find an exact match
1238         // later.
1239         AddRecPhiMatch = &PN;
1240         IncV = TempIncV;
1241         TruncTy = SE.getEffectiveSCEVType(Normalized->getType());
1242       }
1243     }
1244 
1245     if (AddRecPhiMatch) {
1246       // Potentially, move the increment. We have made sure in
1247       // isExpandedAddRecExprPHI or hoistIVInc that this is possible.
1248       if (L == IVIncInsertLoop)
1249         hoistBeforePos(&SE.DT, IncV, IVIncInsertPos, AddRecPhiMatch);
1250 
1251       // Ok, the add recurrence looks usable.
1252       // Remember this PHI, even in post-inc mode.
1253       InsertedValues.insert(AddRecPhiMatch);
1254       // Remember the increment.
1255       rememberInstruction(IncV);
1256       return AddRecPhiMatch;
1257     }
1258   }
1259 
1260   // Save the original insertion point so we can restore it when we're done.
1261   SCEVInsertPointGuard Guard(Builder, this);
1262 
1263   // Another AddRec may need to be recursively expanded below. For example, if
1264   // this AddRec is quadratic, the StepV may itself be an AddRec in this
1265   // loop. Remove this loop from the PostIncLoops set before expanding such
1266   // AddRecs. Otherwise, we cannot find a valid position for the step
1267   // (i.e. StepV can never dominate its loop header).  Ideally, we could do
1268   // SavedIncLoops.swap(PostIncLoops), but we generally have a single element,
1269   // so it's not worth implementing SmallPtrSet::swap.
1270   PostIncLoopSet SavedPostIncLoops = PostIncLoops;
1271   PostIncLoops.clear();
1272 
1273   // Expand code for the start value into the loop preheader.
1274   assert(L->getLoopPreheader() &&
1275          "Can't expand add recurrences without a loop preheader!");
1276   Value *StartV = expandCodeFor(Normalized->getStart(), ExpandTy,
1277                                 L->getLoopPreheader()->getTerminator());
1278 
1279   // StartV must have been be inserted into L's preheader to dominate the new
1280   // phi.
1281   assert(!isa<Instruction>(StartV) ||
1282          SE.DT.properlyDominates(cast<Instruction>(StartV)->getParent(),
1283                                  L->getHeader()));
1284 
1285   // Expand code for the step value. Do this before creating the PHI so that PHI
1286   // reuse code doesn't see an incomplete PHI.
1287   const SCEV *Step = Normalized->getStepRecurrence(SE);
1288   // If the stride is negative, insert a sub instead of an add for the increment
1289   // (unless it's a constant, because subtracts of constants are canonicalized
1290   // to adds).
1291   bool useSubtract = !ExpandTy->isPointerTy() && Step->isNonConstantNegative();
1292   if (useSubtract)
1293     Step = SE.getNegativeSCEV(Step);
1294   // Expand the step somewhere that dominates the loop header.
1295   Value *StepV = expandCodeFor(Step, IntTy, &L->getHeader()->front());
1296 
1297   // The no-wrap behavior proved by IsIncrement(NUW|NSW) is only applicable if
1298   // we actually do emit an addition.  It does not apply if we emit a
1299   // subtraction.
1300   bool IncrementIsNUW = !useSubtract && IsIncrementNUW(SE, Normalized);
1301   bool IncrementIsNSW = !useSubtract && IsIncrementNSW(SE, Normalized);
1302 
1303   // Create the PHI.
1304   BasicBlock *Header = L->getHeader();
1305   Builder.SetInsertPoint(Header, Header->begin());
1306   pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header);
1307   PHINode *PN = Builder.CreatePHI(ExpandTy, std::distance(HPB, HPE),
1308                                   Twine(IVName) + ".iv");
1309   rememberInstruction(PN);
1310 
1311   // Create the step instructions and populate the PHI.
1312   for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) {
1313     BasicBlock *Pred = *HPI;
1314 
1315     // Add a start value.
1316     if (!L->contains(Pred)) {
1317       PN->addIncoming(StartV, Pred);
1318       continue;
1319     }
1320 
1321     // Create a step value and add it to the PHI.
1322     // If IVIncInsertLoop is non-null and equal to the addrec's loop, insert the
1323     // instructions at IVIncInsertPos.
1324     Instruction *InsertPos = L == IVIncInsertLoop ?
1325       IVIncInsertPos : Pred->getTerminator();
1326     Builder.SetInsertPoint(InsertPos);
1327     Value *IncV = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract);
1328 
1329     if (isa<OverflowingBinaryOperator>(IncV)) {
1330       if (IncrementIsNUW)
1331         cast<BinaryOperator>(IncV)->setHasNoUnsignedWrap();
1332       if (IncrementIsNSW)
1333         cast<BinaryOperator>(IncV)->setHasNoSignedWrap();
1334     }
1335     PN->addIncoming(IncV, Pred);
1336   }
1337 
1338   // After expanding subexpressions, restore the PostIncLoops set so the caller
1339   // can ensure that IVIncrement dominates the current uses.
1340   PostIncLoops = SavedPostIncLoops;
1341 
1342   // Remember this PHI, even in post-inc mode.
1343   InsertedValues.insert(PN);
1344 
1345   return PN;
1346 }
1347 
expandAddRecExprLiterally(const SCEVAddRecExpr * S)1348 Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
1349   Type *STy = S->getType();
1350   Type *IntTy = SE.getEffectiveSCEVType(STy);
1351   const Loop *L = S->getLoop();
1352 
1353   // Determine a normalized form of this expression, which is the expression
1354   // before any post-inc adjustment is made.
1355   const SCEVAddRecExpr *Normalized = S;
1356   if (PostIncLoops.count(L)) {
1357     PostIncLoopSet Loops;
1358     Loops.insert(L);
1359     Normalized = cast<SCEVAddRecExpr>(normalizeForPostIncUse(S, Loops, SE));
1360   }
1361 
1362   // Strip off any non-loop-dominating component from the addrec start.
1363   const SCEV *Start = Normalized->getStart();
1364   const SCEV *PostLoopOffset = nullptr;
1365   if (!SE.properlyDominates(Start, L->getHeader())) {
1366     PostLoopOffset = Start;
1367     Start = SE.getConstant(Normalized->getType(), 0);
1368     Normalized = cast<SCEVAddRecExpr>(
1369       SE.getAddRecExpr(Start, Normalized->getStepRecurrence(SE),
1370                        Normalized->getLoop(),
1371                        Normalized->getNoWrapFlags(SCEV::FlagNW)));
1372   }
1373 
1374   // Strip off any non-loop-dominating component from the addrec step.
1375   const SCEV *Step = Normalized->getStepRecurrence(SE);
1376   const SCEV *PostLoopScale = nullptr;
1377   if (!SE.dominates(Step, L->getHeader())) {
1378     PostLoopScale = Step;
1379     Step = SE.getConstant(Normalized->getType(), 1);
1380     if (!Start->isZero()) {
1381         // The normalization below assumes that Start is constant zero, so if
1382         // it isn't re-associate Start to PostLoopOffset.
1383         assert(!PostLoopOffset && "Start not-null but PostLoopOffset set?");
1384         PostLoopOffset = Start;
1385         Start = SE.getConstant(Normalized->getType(), 0);
1386     }
1387     Normalized =
1388       cast<SCEVAddRecExpr>(SE.getAddRecExpr(
1389                              Start, Step, Normalized->getLoop(),
1390                              Normalized->getNoWrapFlags(SCEV::FlagNW)));
1391   }
1392 
1393   // Expand the core addrec. If we need post-loop scaling, force it to
1394   // expand to an integer type to avoid the need for additional casting.
1395   Type *ExpandTy = PostLoopScale ? IntTy : STy;
1396   // We can't use a pointer type for the addrec if the pointer type is
1397   // non-integral.
1398   Type *AddRecPHIExpandTy =
1399       DL.isNonIntegralPointerType(STy) ? Normalized->getType() : ExpandTy;
1400 
1401   // In some cases, we decide to reuse an existing phi node but need to truncate
1402   // it and/or invert the step.
1403   Type *TruncTy = nullptr;
1404   bool InvertStep = false;
1405   PHINode *PN = getAddRecExprPHILiterally(Normalized, L, AddRecPHIExpandTy,
1406                                           IntTy, TruncTy, InvertStep);
1407 
1408   // Accommodate post-inc mode, if necessary.
1409   Value *Result;
1410   if (!PostIncLoops.count(L))
1411     Result = PN;
1412   else {
1413     // In PostInc mode, use the post-incremented value.
1414     BasicBlock *LatchBlock = L->getLoopLatch();
1415     assert(LatchBlock && "PostInc mode requires a unique loop latch!");
1416     Result = PN->getIncomingValueForBlock(LatchBlock);
1417 
1418     // For an expansion to use the postinc form, the client must call
1419     // expandCodeFor with an InsertPoint that is either outside the PostIncLoop
1420     // or dominated by IVIncInsertPos.
1421     if (isa<Instruction>(Result) &&
1422         !SE.DT.dominates(cast<Instruction>(Result),
1423                          &*Builder.GetInsertPoint())) {
1424       // The induction variable's postinc expansion does not dominate this use.
1425       // IVUsers tries to prevent this case, so it is rare. However, it can
1426       // happen when an IVUser outside the loop is not dominated by the latch
1427       // block. Adjusting IVIncInsertPos before expansion begins cannot handle
1428       // all cases. Consider a phi outside whose operand is replaced during
1429       // expansion with the value of the postinc user. Without fundamentally
1430       // changing the way postinc users are tracked, the only remedy is
1431       // inserting an extra IV increment. StepV might fold into PostLoopOffset,
1432       // but hopefully expandCodeFor handles that.
1433       bool useSubtract =
1434         !ExpandTy->isPointerTy() && Step->isNonConstantNegative();
1435       if (useSubtract)
1436         Step = SE.getNegativeSCEV(Step);
1437       Value *StepV;
1438       {
1439         // Expand the step somewhere that dominates the loop header.
1440         SCEVInsertPointGuard Guard(Builder, this);
1441         StepV = expandCodeFor(Step, IntTy, &L->getHeader()->front());
1442       }
1443       Result = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract);
1444     }
1445   }
1446 
1447   // We have decided to reuse an induction variable of a dominating loop. Apply
1448   // truncation and/or inversion of the step.
1449   if (TruncTy) {
1450     Type *ResTy = Result->getType();
1451     // Normalize the result type.
1452     if (ResTy != SE.getEffectiveSCEVType(ResTy))
1453       Result = InsertNoopCastOfTo(Result, SE.getEffectiveSCEVType(ResTy));
1454     // Truncate the result.
1455     if (TruncTy != Result->getType()) {
1456       Result = Builder.CreateTrunc(Result, TruncTy);
1457       rememberInstruction(Result);
1458     }
1459     // Invert the result.
1460     if (InvertStep) {
1461       Result = Builder.CreateSub(expandCodeFor(Normalized->getStart(), TruncTy),
1462                                  Result);
1463       rememberInstruction(Result);
1464     }
1465   }
1466 
1467   // Re-apply any non-loop-dominating scale.
1468   if (PostLoopScale) {
1469     assert(S->isAffine() && "Can't linearly scale non-affine recurrences.");
1470     Result = InsertNoopCastOfTo(Result, IntTy);
1471     Result = Builder.CreateMul(Result,
1472                                expandCodeFor(PostLoopScale, IntTy));
1473     rememberInstruction(Result);
1474   }
1475 
1476   // Re-apply any non-loop-dominating offset.
1477   if (PostLoopOffset) {
1478     if (PointerType *PTy = dyn_cast<PointerType>(ExpandTy)) {
1479       if (Result->getType()->isIntegerTy()) {
1480         Value *Base = expandCodeFor(PostLoopOffset, ExpandTy);
1481         Result = expandAddToGEP(SE.getUnknown(Result), PTy, IntTy, Base);
1482       } else {
1483         Result = expandAddToGEP(PostLoopOffset, PTy, IntTy, Result);
1484       }
1485     } else {
1486       Result = InsertNoopCastOfTo(Result, IntTy);
1487       Result = Builder.CreateAdd(Result,
1488                                  expandCodeFor(PostLoopOffset, IntTy));
1489       rememberInstruction(Result);
1490     }
1491   }
1492 
1493   return Result;
1494 }
1495 
visitAddRecExpr(const SCEVAddRecExpr * S)1496 Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
1497   // In canonical mode we compute the addrec as an expression of a canonical IV
1498   // using evaluateAtIteration and expand the resulting SCEV expression. This
1499   // way we avoid introducing new IVs to carry on the comutation of the addrec
1500   // throughout the loop.
1501   //
1502   // For nested addrecs evaluateAtIteration might need a canonical IV of a
1503   // type wider than the addrec itself. Emitting a canonical IV of the
1504   // proper type might produce non-legal types, for example expanding an i64
1505   // {0,+,2,+,1} addrec would need an i65 canonical IV. To avoid this just fall
1506   // back to non-canonical mode for nested addrecs.
1507   if (!CanonicalMode || (S->getNumOperands() > 2))
1508     return expandAddRecExprLiterally(S);
1509 
1510   Type *Ty = SE.getEffectiveSCEVType(S->getType());
1511   const Loop *L = S->getLoop();
1512 
1513   // First check for an existing canonical IV in a suitable type.
1514   PHINode *CanonicalIV = nullptr;
1515   if (PHINode *PN = L->getCanonicalInductionVariable())
1516     if (SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty))
1517       CanonicalIV = PN;
1518 
1519   // Rewrite an AddRec in terms of the canonical induction variable, if
1520   // its type is more narrow.
1521   if (CanonicalIV &&
1522       SE.getTypeSizeInBits(CanonicalIV->getType()) >
1523       SE.getTypeSizeInBits(Ty)) {
1524     SmallVector<const SCEV *, 4> NewOps(S->getNumOperands());
1525     for (unsigned i = 0, e = S->getNumOperands(); i != e; ++i)
1526       NewOps[i] = SE.getAnyExtendExpr(S->op_begin()[i], CanonicalIV->getType());
1527     Value *V = expand(SE.getAddRecExpr(NewOps, S->getLoop(),
1528                                        S->getNoWrapFlags(SCEV::FlagNW)));
1529     BasicBlock::iterator NewInsertPt =
1530         findInsertPointAfter(cast<Instruction>(V), Builder.GetInsertBlock());
1531     V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), nullptr,
1532                       &*NewInsertPt);
1533     return V;
1534   }
1535 
1536   // {X,+,F} --> X + {0,+,F}
1537   if (!S->getStart()->isZero()) {
1538     SmallVector<const SCEV *, 4> NewOps(S->op_begin(), S->op_end());
1539     NewOps[0] = SE.getConstant(Ty, 0);
1540     const SCEV *Rest = SE.getAddRecExpr(NewOps, L,
1541                                         S->getNoWrapFlags(SCEV::FlagNW));
1542 
1543     // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the
1544     // comments on expandAddToGEP for details.
1545     const SCEV *Base = S->getStart();
1546     // Dig into the expression to find the pointer base for a GEP.
1547     const SCEV *ExposedRest = Rest;
1548     ExposePointerBase(Base, ExposedRest, SE);
1549     // If we found a pointer, expand the AddRec with a GEP.
1550     if (PointerType *PTy = dyn_cast<PointerType>(Base->getType())) {
1551       // Make sure the Base isn't something exotic, such as a multiplied
1552       // or divided pointer value. In those cases, the result type isn't
1553       // actually a pointer type.
1554       if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) {
1555         Value *StartV = expand(Base);
1556         assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!");
1557         return expandAddToGEP(ExposedRest, PTy, Ty, StartV);
1558       }
1559     }
1560 
1561     // Just do a normal add. Pre-expand the operands to suppress folding.
1562     //
1563     // The LHS and RHS values are factored out of the expand call to make the
1564     // output independent of the argument evaluation order.
1565     const SCEV *AddExprLHS = SE.getUnknown(expand(S->getStart()));
1566     const SCEV *AddExprRHS = SE.getUnknown(expand(Rest));
1567     return expand(SE.getAddExpr(AddExprLHS, AddExprRHS));
1568   }
1569 
1570   // If we don't yet have a canonical IV, create one.
1571   if (!CanonicalIV) {
1572     // Create and insert the PHI node for the induction variable in the
1573     // specified loop.
1574     BasicBlock *Header = L->getHeader();
1575     pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header);
1576     CanonicalIV = PHINode::Create(Ty, std::distance(HPB, HPE), "indvar",
1577                                   &Header->front());
1578     rememberInstruction(CanonicalIV);
1579 
1580     SmallSet<BasicBlock *, 4> PredSeen;
1581     Constant *One = ConstantInt::get(Ty, 1);
1582     for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) {
1583       BasicBlock *HP = *HPI;
1584       if (!PredSeen.insert(HP).second) {
1585         // There must be an incoming value for each predecessor, even the
1586         // duplicates!
1587         CanonicalIV->addIncoming(CanonicalIV->getIncomingValueForBlock(HP), HP);
1588         continue;
1589       }
1590 
1591       if (L->contains(HP)) {
1592         // Insert a unit add instruction right before the terminator
1593         // corresponding to the back-edge.
1594         Instruction *Add = BinaryOperator::CreateAdd(CanonicalIV, One,
1595                                                      "indvar.next",
1596                                                      HP->getTerminator());
1597         Add->setDebugLoc(HP->getTerminator()->getDebugLoc());
1598         rememberInstruction(Add);
1599         CanonicalIV->addIncoming(Add, HP);
1600       } else {
1601         CanonicalIV->addIncoming(Constant::getNullValue(Ty), HP);
1602       }
1603     }
1604   }
1605 
1606   // {0,+,1} --> Insert a canonical induction variable into the loop!
1607   if (S->isAffine() && S->getOperand(1)->isOne()) {
1608     assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) &&
1609            "IVs with types different from the canonical IV should "
1610            "already have been handled!");
1611     return CanonicalIV;
1612   }
1613 
1614   // {0,+,F} --> {0,+,1} * F
1615 
1616   // If this is a simple linear addrec, emit it now as a special case.
1617   if (S->isAffine())    // {0,+,F} --> i*F
1618     return
1619       expand(SE.getTruncateOrNoop(
1620         SE.getMulExpr(SE.getUnknown(CanonicalIV),
1621                       SE.getNoopOrAnyExtend(S->getOperand(1),
1622                                             CanonicalIV->getType())),
1623         Ty));
1624 
1625   // If this is a chain of recurrences, turn it into a closed form, using the
1626   // folders, then expandCodeFor the closed form.  This allows the folders to
1627   // simplify the expression without having to build a bunch of special code
1628   // into this folder.
1629   const SCEV *IH = SE.getUnknown(CanonicalIV);   // Get I as a "symbolic" SCEV.
1630 
1631   // Promote S up to the canonical IV type, if the cast is foldable.
1632   const SCEV *NewS = S;
1633   const SCEV *Ext = SE.getNoopOrAnyExtend(S, CanonicalIV->getType());
1634   if (isa<SCEVAddRecExpr>(Ext))
1635     NewS = Ext;
1636 
1637   const SCEV *V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE);
1638   //cerr << "Evaluated: " << *this << "\n     to: " << *V << "\n";
1639 
1640   // Truncate the result down to the original type, if needed.
1641   const SCEV *T = SE.getTruncateOrNoop(V, Ty);
1642   return expand(T);
1643 }
1644 
visitTruncateExpr(const SCEVTruncateExpr * S)1645 Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) {
1646   Type *Ty = SE.getEffectiveSCEVType(S->getType());
1647   Value *V = expandCodeFor(S->getOperand(),
1648                            SE.getEffectiveSCEVType(S->getOperand()->getType()));
1649   Value *I = Builder.CreateTrunc(V, Ty);
1650   rememberInstruction(I);
1651   return I;
1652 }
1653 
visitZeroExtendExpr(const SCEVZeroExtendExpr * S)1654 Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) {
1655   Type *Ty = SE.getEffectiveSCEVType(S->getType());
1656   Value *V = expandCodeFor(S->getOperand(),
1657                            SE.getEffectiveSCEVType(S->getOperand()->getType()));
1658   Value *I = Builder.CreateZExt(V, Ty);
1659   rememberInstruction(I);
1660   return I;
1661 }
1662 
visitSignExtendExpr(const SCEVSignExtendExpr * S)1663 Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) {
1664   Type *Ty = SE.getEffectiveSCEVType(S->getType());
1665   Value *V = expandCodeFor(S->getOperand(),
1666                            SE.getEffectiveSCEVType(S->getOperand()->getType()));
1667   Value *I = Builder.CreateSExt(V, Ty);
1668   rememberInstruction(I);
1669   return I;
1670 }
1671 
visitSMaxExpr(const SCEVSMaxExpr * S)1672 Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) {
1673   Value *LHS = expand(S->getOperand(S->getNumOperands()-1));
1674   Type *Ty = LHS->getType();
1675   for (int i = S->getNumOperands()-2; i >= 0; --i) {
1676     // In the case of mixed integer and pointer types, do the
1677     // rest of the comparisons as integer.
1678     Type *OpTy = S->getOperand(i)->getType();
1679     if (OpTy->isIntegerTy() != Ty->isIntegerTy()) {
1680       Ty = SE.getEffectiveSCEVType(Ty);
1681       LHS = InsertNoopCastOfTo(LHS, Ty);
1682     }
1683     Value *RHS = expandCodeFor(S->getOperand(i), Ty);
1684     Value *ICmp = Builder.CreateICmpSGT(LHS, RHS);
1685     rememberInstruction(ICmp);
1686     Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax");
1687     rememberInstruction(Sel);
1688     LHS = Sel;
1689   }
1690   // In the case of mixed integer and pointer types, cast the
1691   // final result back to the pointer type.
1692   if (LHS->getType() != S->getType())
1693     LHS = InsertNoopCastOfTo(LHS, S->getType());
1694   return LHS;
1695 }
1696 
visitUMaxExpr(const SCEVUMaxExpr * S)1697 Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
1698   Value *LHS = expand(S->getOperand(S->getNumOperands()-1));
1699   Type *Ty = LHS->getType();
1700   for (int i = S->getNumOperands()-2; i >= 0; --i) {
1701     // In the case of mixed integer and pointer types, do the
1702     // rest of the comparisons as integer.
1703     Type *OpTy = S->getOperand(i)->getType();
1704     if (OpTy->isIntegerTy() != Ty->isIntegerTy()) {
1705       Ty = SE.getEffectiveSCEVType(Ty);
1706       LHS = InsertNoopCastOfTo(LHS, Ty);
1707     }
1708     Value *RHS = expandCodeFor(S->getOperand(i), Ty);
1709     Value *ICmp = Builder.CreateICmpUGT(LHS, RHS);
1710     rememberInstruction(ICmp);
1711     Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax");
1712     rememberInstruction(Sel);
1713     LHS = Sel;
1714   }
1715   // In the case of mixed integer and pointer types, cast the
1716   // final result back to the pointer type.
1717   if (LHS->getType() != S->getType())
1718     LHS = InsertNoopCastOfTo(LHS, S->getType());
1719   return LHS;
1720 }
1721 
visitSMinExpr(const SCEVSMinExpr * S)1722 Value *SCEVExpander::visitSMinExpr(const SCEVSMinExpr *S) {
1723   Value *LHS = expand(S->getOperand(S->getNumOperands() - 1));
1724   Type *Ty = LHS->getType();
1725   for (int i = S->getNumOperands() - 2; i >= 0; --i) {
1726     // In the case of mixed integer and pointer types, do the
1727     // rest of the comparisons as integer.
1728     Type *OpTy = S->getOperand(i)->getType();
1729     if (OpTy->isIntegerTy() != Ty->isIntegerTy()) {
1730       Ty = SE.getEffectiveSCEVType(Ty);
1731       LHS = InsertNoopCastOfTo(LHS, Ty);
1732     }
1733     Value *RHS = expandCodeFor(S->getOperand(i), Ty);
1734     Value *ICmp = Builder.CreateICmpSLT(LHS, RHS);
1735     rememberInstruction(ICmp);
1736     Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smin");
1737     rememberInstruction(Sel);
1738     LHS = Sel;
1739   }
1740   // In the case of mixed integer and pointer types, cast the
1741   // final result back to the pointer type.
1742   if (LHS->getType() != S->getType())
1743     LHS = InsertNoopCastOfTo(LHS, S->getType());
1744   return LHS;
1745 }
1746 
visitUMinExpr(const SCEVUMinExpr * S)1747 Value *SCEVExpander::visitUMinExpr(const SCEVUMinExpr *S) {
1748   Value *LHS = expand(S->getOperand(S->getNumOperands() - 1));
1749   Type *Ty = LHS->getType();
1750   for (int i = S->getNumOperands() - 2; i >= 0; --i) {
1751     // In the case of mixed integer and pointer types, do the
1752     // rest of the comparisons as integer.
1753     Type *OpTy = S->getOperand(i)->getType();
1754     if (OpTy->isIntegerTy() != Ty->isIntegerTy()) {
1755       Ty = SE.getEffectiveSCEVType(Ty);
1756       LHS = InsertNoopCastOfTo(LHS, Ty);
1757     }
1758     Value *RHS = expandCodeFor(S->getOperand(i), Ty);
1759     Value *ICmp = Builder.CreateICmpULT(LHS, RHS);
1760     rememberInstruction(ICmp);
1761     Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umin");
1762     rememberInstruction(Sel);
1763     LHS = Sel;
1764   }
1765   // In the case of mixed integer and pointer types, cast the
1766   // final result back to the pointer type.
1767   if (LHS->getType() != S->getType())
1768     LHS = InsertNoopCastOfTo(LHS, S->getType());
1769   return LHS;
1770 }
1771 
expandCodeFor(const SCEV * SH,Type * Ty,Instruction * IP)1772 Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty,
1773                                    Instruction *IP) {
1774   setInsertPoint(IP);
1775   return expandCodeFor(SH, Ty);
1776 }
1777 
expandCodeFor(const SCEV * SH,Type * Ty)1778 Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty) {
1779   // Expand the code for this SCEV.
1780   Value *V = expand(SH);
1781   if (Ty) {
1782     assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) &&
1783            "non-trivial casts should be done with the SCEVs directly!");
1784     V = InsertNoopCastOfTo(V, Ty);
1785   }
1786   return V;
1787 }
1788 
1789 ScalarEvolution::ValueOffsetPair
FindValueInExprValueMap(const SCEV * S,const Instruction * InsertPt)1790 SCEVExpander::FindValueInExprValueMap(const SCEV *S,
1791                                       const Instruction *InsertPt) {
1792   SetVector<ScalarEvolution::ValueOffsetPair> *Set = SE.getSCEVValues(S);
1793   // If the expansion is not in CanonicalMode, and the SCEV contains any
1794   // sub scAddRecExpr type SCEV, it is required to expand the SCEV literally.
1795   if (CanonicalMode || !SE.containsAddRecurrence(S)) {
1796     // If S is scConstant, it may be worse to reuse an existing Value.
1797     if (S->getSCEVType() != scConstant && Set) {
1798       // Choose a Value from the set which dominates the insertPt.
1799       // insertPt should be inside the Value's parent loop so as not to break
1800       // the LCSSA form.
1801       for (auto const &VOPair : *Set) {
1802         Value *V = VOPair.first;
1803         ConstantInt *Offset = VOPair.second;
1804         Instruction *EntInst = nullptr;
1805         if (V && isa<Instruction>(V) && (EntInst = cast<Instruction>(V)) &&
1806             S->getType() == V->getType() &&
1807             EntInst->getFunction() == InsertPt->getFunction() &&
1808             SE.DT.dominates(EntInst, InsertPt) &&
1809             (SE.LI.getLoopFor(EntInst->getParent()) == nullptr ||
1810              SE.LI.getLoopFor(EntInst->getParent())->contains(InsertPt)))
1811           return {V, Offset};
1812       }
1813     }
1814   }
1815   return {nullptr, nullptr};
1816 }
1817 
1818 // The expansion of SCEV will either reuse a previous Value in ExprValueMap,
1819 // or expand the SCEV literally. Specifically, if the expansion is in LSRMode,
1820 // and the SCEV contains any sub scAddRecExpr type SCEV, it will be expanded
1821 // literally, to prevent LSR's transformed SCEV from being reverted. Otherwise,
1822 // the expansion will try to reuse Value from ExprValueMap, and only when it
1823 // fails, expand the SCEV literally.
expand(const SCEV * S)1824 Value *SCEVExpander::expand(const SCEV *S) {
1825   // Compute an insertion point for this SCEV object. Hoist the instructions
1826   // as far out in the loop nest as possible.
1827   Instruction *InsertPt = &*Builder.GetInsertPoint();
1828 
1829   // We can move insertion point only if there is no div or rem operations
1830   // otherwise we are risky to move it over the check for zero denominator.
1831   auto SafeToHoist = [](const SCEV *S) {
1832     return !SCEVExprContains(S, [](const SCEV *S) {
1833               if (const auto *D = dyn_cast<SCEVUDivExpr>(S)) {
1834                 if (const auto *SC = dyn_cast<SCEVConstant>(D->getRHS()))
1835                   // Division by non-zero constants can be hoisted.
1836                   return SC->getValue()->isZero();
1837                 // All other divisions should not be moved as they may be
1838                 // divisions by zero and should be kept within the
1839                 // conditions of the surrounding loops that guard their
1840                 // execution (see PR35406).
1841                 return true;
1842               }
1843               return false;
1844             });
1845   };
1846   if (SafeToHoist(S)) {
1847     for (Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock());;
1848          L = L->getParentLoop()) {
1849       if (SE.isLoopInvariant(S, L)) {
1850         if (!L) break;
1851         if (BasicBlock *Preheader = L->getLoopPreheader())
1852           InsertPt = Preheader->getTerminator();
1853         else
1854           // LSR sets the insertion point for AddRec start/step values to the
1855           // block start to simplify value reuse, even though it's an invalid
1856           // position. SCEVExpander must correct for this in all cases.
1857           InsertPt = &*L->getHeader()->getFirstInsertionPt();
1858       } else {
1859         // If the SCEV is computable at this level, insert it into the header
1860         // after the PHIs (and after any other instructions that we've inserted
1861         // there) so that it is guaranteed to dominate any user inside the loop.
1862         if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L))
1863           InsertPt = &*L->getHeader()->getFirstInsertionPt();
1864         while (InsertPt->getIterator() != Builder.GetInsertPoint() &&
1865                (isInsertedInstruction(InsertPt) ||
1866                 isa<DbgInfoIntrinsic>(InsertPt)))
1867           InsertPt = &*std::next(InsertPt->getIterator());
1868         break;
1869       }
1870     }
1871   }
1872 
1873   // IndVarSimplify sometimes sets the insertion point at the block start, even
1874   // when there are PHIs at that point.  We must correct for this.
1875   if (isa<PHINode>(*InsertPt))
1876     InsertPt = &*InsertPt->getParent()->getFirstInsertionPt();
1877 
1878   // Check to see if we already expanded this here.
1879   auto I = InsertedExpressions.find(std::make_pair(S, InsertPt));
1880   if (I != InsertedExpressions.end())
1881     return I->second;
1882 
1883   SCEVInsertPointGuard Guard(Builder, this);
1884   Builder.SetInsertPoint(InsertPt);
1885 
1886   // Expand the expression into instructions.
1887   ScalarEvolution::ValueOffsetPair VO = FindValueInExprValueMap(S, InsertPt);
1888   Value *V = VO.first;
1889 
1890   if (!V)
1891     V = visit(S);
1892   else if (VO.second) {
1893     if (PointerType *Vty = dyn_cast<PointerType>(V->getType())) {
1894       Type *Ety = Vty->getPointerElementType();
1895       int64_t Offset = VO.second->getSExtValue();
1896       int64_t ESize = SE.getTypeSizeInBits(Ety);
1897       if ((Offset * 8) % ESize == 0) {
1898         ConstantInt *Idx =
1899             ConstantInt::getSigned(VO.second->getType(), -(Offset * 8) / ESize);
1900         V = Builder.CreateGEP(Ety, V, Idx, "scevgep");
1901       } else {
1902         ConstantInt *Idx =
1903             ConstantInt::getSigned(VO.second->getType(), -Offset);
1904         unsigned AS = Vty->getAddressSpace();
1905         V = Builder.CreateBitCast(V, Type::getInt8PtrTy(SE.getContext(), AS));
1906         V = Builder.CreateGEP(Type::getInt8Ty(SE.getContext()), V, Idx,
1907                               "uglygep");
1908         V = Builder.CreateBitCast(V, Vty);
1909       }
1910     } else {
1911       V = Builder.CreateSub(V, VO.second);
1912     }
1913   }
1914   // Remember the expanded value for this SCEV at this location.
1915   //
1916   // This is independent of PostIncLoops. The mapped value simply materializes
1917   // the expression at this insertion point. If the mapped value happened to be
1918   // a postinc expansion, it could be reused by a non-postinc user, but only if
1919   // its insertion point was already at the head of the loop.
1920   InsertedExpressions[std::make_pair(S, InsertPt)] = V;
1921   return V;
1922 }
1923 
rememberInstruction(Value * I)1924 void SCEVExpander::rememberInstruction(Value *I) {
1925   if (!PostIncLoops.empty())
1926     InsertedPostIncValues.insert(I);
1927   else
1928     InsertedValues.insert(I);
1929 }
1930 
1931 /// getOrInsertCanonicalInductionVariable - This method returns the
1932 /// canonical induction variable of the specified type for the specified
1933 /// loop (inserting one if there is none).  A canonical induction variable
1934 /// starts at zero and steps by one on each iteration.
1935 PHINode *
getOrInsertCanonicalInductionVariable(const Loop * L,Type * Ty)1936 SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L,
1937                                                     Type *Ty) {
1938   assert(Ty->isIntegerTy() && "Can only insert integer induction variables!");
1939 
1940   // Build a SCEV for {0,+,1}<L>.
1941   // Conservatively use FlagAnyWrap for now.
1942   const SCEV *H = SE.getAddRecExpr(SE.getConstant(Ty, 0),
1943                                    SE.getConstant(Ty, 1), L, SCEV::FlagAnyWrap);
1944 
1945   // Emit code for it.
1946   SCEVInsertPointGuard Guard(Builder, this);
1947   PHINode *V =
1948       cast<PHINode>(expandCodeFor(H, nullptr, &L->getHeader()->front()));
1949 
1950   return V;
1951 }
1952 
1953 /// replaceCongruentIVs - Check for congruent phis in this loop header and
1954 /// replace them with their most canonical representative. Return the number of
1955 /// phis eliminated.
1956 ///
1957 /// This does not depend on any SCEVExpander state but should be used in
1958 /// the same context that SCEVExpander is used.
1959 unsigned
replaceCongruentIVs(Loop * L,const DominatorTree * DT,SmallVectorImpl<WeakTrackingVH> & DeadInsts,const TargetTransformInfo * TTI)1960 SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT,
1961                                   SmallVectorImpl<WeakTrackingVH> &DeadInsts,
1962                                   const TargetTransformInfo *TTI) {
1963   // Find integer phis in order of increasing width.
1964   SmallVector<PHINode*, 8> Phis;
1965   for (PHINode &PN : L->getHeader()->phis())
1966     Phis.push_back(&PN);
1967 
1968   if (TTI)
1969     llvm::sort(Phis, [](Value *LHS, Value *RHS) {
1970       // Put pointers at the back and make sure pointer < pointer = false.
1971       if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
1972         return RHS->getType()->isIntegerTy() && !LHS->getType()->isIntegerTy();
1973       return RHS->getType()->getPrimitiveSizeInBits() <
1974              LHS->getType()->getPrimitiveSizeInBits();
1975     });
1976 
1977   unsigned NumElim = 0;
1978   DenseMap<const SCEV *, PHINode *> ExprToIVMap;
1979   // Process phis from wide to narrow. Map wide phis to their truncation
1980   // so narrow phis can reuse them.
1981   for (PHINode *Phi : Phis) {
1982     auto SimplifyPHINode = [&](PHINode *PN) -> Value * {
1983       if (Value *V = SimplifyInstruction(PN, {DL, &SE.TLI, &SE.DT, &SE.AC}))
1984         return V;
1985       if (!SE.isSCEVable(PN->getType()))
1986         return nullptr;
1987       auto *Const = dyn_cast<SCEVConstant>(SE.getSCEV(PN));
1988       if (!Const)
1989         return nullptr;
1990       return Const->getValue();
1991     };
1992 
1993     // Fold constant phis. They may be congruent to other constant phis and
1994     // would confuse the logic below that expects proper IVs.
1995     if (Value *V = SimplifyPHINode(Phi)) {
1996       if (V->getType() != Phi->getType())
1997         continue;
1998       Phi->replaceAllUsesWith(V);
1999       DeadInsts.emplace_back(Phi);
2000       ++NumElim;
2001       DEBUG_WITH_TYPE(DebugType, dbgs()
2002                       << "INDVARS: Eliminated constant iv: " << *Phi << '\n');
2003       continue;
2004     }
2005 
2006     if (!SE.isSCEVable(Phi->getType()))
2007       continue;
2008 
2009     PHINode *&OrigPhiRef = ExprToIVMap[SE.getSCEV(Phi)];
2010     if (!OrigPhiRef) {
2011       OrigPhiRef = Phi;
2012       if (Phi->getType()->isIntegerTy() && TTI &&
2013           TTI->isTruncateFree(Phi->getType(), Phis.back()->getType())) {
2014         // This phi can be freely truncated to the narrowest phi type. Map the
2015         // truncated expression to it so it will be reused for narrow types.
2016         const SCEV *TruncExpr =
2017           SE.getTruncateExpr(SE.getSCEV(Phi), Phis.back()->getType());
2018         ExprToIVMap[TruncExpr] = Phi;
2019       }
2020       continue;
2021     }
2022 
2023     // Replacing a pointer phi with an integer phi or vice-versa doesn't make
2024     // sense.
2025     if (OrigPhiRef->getType()->isPointerTy() != Phi->getType()->isPointerTy())
2026       continue;
2027 
2028     if (BasicBlock *LatchBlock = L->getLoopLatch()) {
2029       Instruction *OrigInc = dyn_cast<Instruction>(
2030           OrigPhiRef->getIncomingValueForBlock(LatchBlock));
2031       Instruction *IsomorphicInc =
2032           dyn_cast<Instruction>(Phi->getIncomingValueForBlock(LatchBlock));
2033 
2034       if (OrigInc && IsomorphicInc) {
2035         // If this phi has the same width but is more canonical, replace the
2036         // original with it. As part of the "more canonical" determination,
2037         // respect a prior decision to use an IV chain.
2038         if (OrigPhiRef->getType() == Phi->getType() &&
2039             !(ChainedPhis.count(Phi) ||
2040               isExpandedAddRecExprPHI(OrigPhiRef, OrigInc, L)) &&
2041             (ChainedPhis.count(Phi) ||
2042              isExpandedAddRecExprPHI(Phi, IsomorphicInc, L))) {
2043           std::swap(OrigPhiRef, Phi);
2044           std::swap(OrigInc, IsomorphicInc);
2045         }
2046         // Replacing the congruent phi is sufficient because acyclic
2047         // redundancy elimination, CSE/GVN, should handle the
2048         // rest. However, once SCEV proves that a phi is congruent,
2049         // it's often the head of an IV user cycle that is isomorphic
2050         // with the original phi. It's worth eagerly cleaning up the
2051         // common case of a single IV increment so that DeleteDeadPHIs
2052         // can remove cycles that had postinc uses.
2053         const SCEV *TruncExpr =
2054             SE.getTruncateOrNoop(SE.getSCEV(OrigInc), IsomorphicInc->getType());
2055         if (OrigInc != IsomorphicInc &&
2056             TruncExpr == SE.getSCEV(IsomorphicInc) &&
2057             SE.LI.replacementPreservesLCSSAForm(IsomorphicInc, OrigInc) &&
2058             hoistIVInc(OrigInc, IsomorphicInc)) {
2059           DEBUG_WITH_TYPE(DebugType,
2060                           dbgs() << "INDVARS: Eliminated congruent iv.inc: "
2061                                  << *IsomorphicInc << '\n');
2062           Value *NewInc = OrigInc;
2063           if (OrigInc->getType() != IsomorphicInc->getType()) {
2064             Instruction *IP = nullptr;
2065             if (PHINode *PN = dyn_cast<PHINode>(OrigInc))
2066               IP = &*PN->getParent()->getFirstInsertionPt();
2067             else
2068               IP = OrigInc->getNextNode();
2069 
2070             IRBuilder<> Builder(IP);
2071             Builder.SetCurrentDebugLocation(IsomorphicInc->getDebugLoc());
2072             NewInc = Builder.CreateTruncOrBitCast(
2073                 OrigInc, IsomorphicInc->getType(), IVName);
2074           }
2075           IsomorphicInc->replaceAllUsesWith(NewInc);
2076           DeadInsts.emplace_back(IsomorphicInc);
2077         }
2078       }
2079     }
2080     DEBUG_WITH_TYPE(DebugType, dbgs() << "INDVARS: Eliminated congruent iv: "
2081                                       << *Phi << '\n');
2082     ++NumElim;
2083     Value *NewIV = OrigPhiRef;
2084     if (OrigPhiRef->getType() != Phi->getType()) {
2085       IRBuilder<> Builder(&*L->getHeader()->getFirstInsertionPt());
2086       Builder.SetCurrentDebugLocation(Phi->getDebugLoc());
2087       NewIV = Builder.CreateTruncOrBitCast(OrigPhiRef, Phi->getType(), IVName);
2088     }
2089     Phi->replaceAllUsesWith(NewIV);
2090     DeadInsts.emplace_back(Phi);
2091   }
2092   return NumElim;
2093 }
2094 
getExactExistingExpansion(const SCEV * S,const Instruction * At,Loop * L)2095 Value *SCEVExpander::getExactExistingExpansion(const SCEV *S,
2096                                                const Instruction *At, Loop *L) {
2097   Optional<ScalarEvolution::ValueOffsetPair> VO =
2098       getRelatedExistingExpansion(S, At, L);
2099   if (VO && VO.getValue().second == nullptr)
2100     return VO.getValue().first;
2101   return nullptr;
2102 }
2103 
2104 Optional<ScalarEvolution::ValueOffsetPair>
getRelatedExistingExpansion(const SCEV * S,const Instruction * At,Loop * L)2105 SCEVExpander::getRelatedExistingExpansion(const SCEV *S, const Instruction *At,
2106                                           Loop *L) {
2107   using namespace llvm::PatternMatch;
2108 
2109   SmallVector<BasicBlock *, 4> ExitingBlocks;
2110   L->getExitingBlocks(ExitingBlocks);
2111 
2112   // Look for suitable value in simple conditions at the loop exits.
2113   for (BasicBlock *BB : ExitingBlocks) {
2114     ICmpInst::Predicate Pred;
2115     Instruction *LHS, *RHS;
2116 
2117     if (!match(BB->getTerminator(),
2118                m_Br(m_ICmp(Pred, m_Instruction(LHS), m_Instruction(RHS)),
2119                     m_BasicBlock(), m_BasicBlock())))
2120       continue;
2121 
2122     if (SE.getSCEV(LHS) == S && SE.DT.dominates(LHS, At))
2123       return ScalarEvolution::ValueOffsetPair(LHS, nullptr);
2124 
2125     if (SE.getSCEV(RHS) == S && SE.DT.dominates(RHS, At))
2126       return ScalarEvolution::ValueOffsetPair(RHS, nullptr);
2127   }
2128 
2129   // Use expand's logic which is used for reusing a previous Value in
2130   // ExprValueMap.
2131   ScalarEvolution::ValueOffsetPair VO = FindValueInExprValueMap(S, At);
2132   if (VO.first)
2133     return VO;
2134 
2135   // There is potential to make this significantly smarter, but this simple
2136   // heuristic already gets some interesting cases.
2137 
2138   // Can not find suitable value.
2139   return None;
2140 }
2141 
isHighCostExpansionHelper(const SCEV * S,Loop * L,const Instruction & At,int & BudgetRemaining,const TargetTransformInfo & TTI,SmallPtrSetImpl<const SCEV * > & Processed,SmallVectorImpl<const SCEV * > & Worklist)2142 bool SCEVExpander::isHighCostExpansionHelper(
2143     const SCEV *S, Loop *L, const Instruction &At, int &BudgetRemaining,
2144     const TargetTransformInfo &TTI, SmallPtrSetImpl<const SCEV *> &Processed,
2145     SmallVectorImpl<const SCEV *> &Worklist) {
2146   if (BudgetRemaining < 0)
2147     return true; // Already run out of budget, give up.
2148 
2149   // Was the cost of expansion of this expression already accounted for?
2150   if (!Processed.insert(S).second)
2151     return false; // We have already accounted for this expression.
2152 
2153   // If we can find an existing value for this scev available at the point "At"
2154   // then consider the expression cheap.
2155   if (getRelatedExistingExpansion(S, &At, L))
2156     return false; // Consider the expression to be free.
2157 
2158   switch (S->getSCEVType()) {
2159   case scUnknown:
2160   case scConstant:
2161     return false; // Assume to be zero-cost.
2162   }
2163 
2164   TargetTransformInfo::TargetCostKind CostKind =
2165     TargetTransformInfo::TCK_RecipThroughput;
2166 
2167   if (auto *CastExpr = dyn_cast<SCEVCastExpr>(S)) {
2168     unsigned Opcode;
2169     switch (S->getSCEVType()) {
2170     case scTruncate:
2171       Opcode = Instruction::Trunc;
2172       break;
2173     case scZeroExtend:
2174       Opcode = Instruction::ZExt;
2175       break;
2176     case scSignExtend:
2177       Opcode = Instruction::SExt;
2178       break;
2179     default:
2180       llvm_unreachable("There are no other cast types.");
2181     }
2182     const SCEV *Op = CastExpr->getOperand();
2183     BudgetRemaining -= TTI.getCastInstrCost(Opcode, /*Dst=*/S->getType(),
2184                                             /*Src=*/Op->getType(), CostKind);
2185     Worklist.emplace_back(Op);
2186     return false; // Will answer upon next entry into this function.
2187   }
2188 
2189   if (auto *UDivExpr = dyn_cast<SCEVUDivExpr>(S)) {
2190     // If the divisor is a power of two count this as a logical right-shift.
2191     if (auto *SC = dyn_cast<SCEVConstant>(UDivExpr->getRHS())) {
2192       if (SC->getAPInt().isPowerOf2()) {
2193         BudgetRemaining -=
2194             TTI.getArithmeticInstrCost(Instruction::LShr, S->getType(),
2195                                        CostKind);
2196         // Note that we don't count the cost of RHS, because it is a constant,
2197         // and we consider those to be free. But if that changes, we would need
2198         // to log2() it first before calling isHighCostExpansionHelper().
2199         Worklist.emplace_back(UDivExpr->getLHS());
2200         return false; // Will answer upon next entry into this function.
2201       }
2202     }
2203 
2204     // UDivExpr is very likely a UDiv that ScalarEvolution's HowFarToZero or
2205     // HowManyLessThans produced to compute a precise expression, rather than a
2206     // UDiv from the user's code. If we can't find a UDiv in the code with some
2207     // simple searching, we need to account for it's cost.
2208 
2209     // At the beginning of this function we already tried to find existing
2210     // value for plain 'S'. Now try to lookup 'S + 1' since it is common
2211     // pattern involving division. This is just a simple search heuristic.
2212     if (getRelatedExistingExpansion(
2213             SE.getAddExpr(S, SE.getConstant(S->getType(), 1)), &At, L))
2214       return false; // Consider it to be free.
2215 
2216     // Need to count the cost of this UDiv.
2217     BudgetRemaining -=
2218         TTI.getArithmeticInstrCost(Instruction::UDiv, S->getType(),
2219                                    CostKind);
2220     Worklist.insert(Worklist.end(), {UDivExpr->getLHS(), UDivExpr->getRHS()});
2221     return false; // Will answer upon next entry into this function.
2222   }
2223 
2224   if (const auto *NAry = dyn_cast<SCEVAddRecExpr>(S)) {
2225     Type *OpType = NAry->getType();
2226 
2227     assert(NAry->getNumOperands() >= 2 &&
2228            "Polynomial should be at least linear");
2229 
2230     int AddCost =
2231       TTI.getArithmeticInstrCost(Instruction::Add, OpType, CostKind);
2232     int MulCost =
2233       TTI.getArithmeticInstrCost(Instruction::Mul, OpType, CostKind);
2234 
2235     // In this polynominal, we may have some zero operands, and we shouldn't
2236     // really charge for those. So how many non-zero coeffients are there?
2237     int NumTerms = llvm::count_if(NAry->operands(),
2238                                   [](const SCEV *S) { return !S->isZero(); });
2239     assert(NumTerms >= 1 && "Polynominal should have at least one term.");
2240     assert(!(*std::prev(NAry->operands().end()))->isZero() &&
2241            "Last operand should not be zero");
2242 
2243     // Much like with normal add expr, the polynominal will require
2244     // one less addition than the number of it's terms.
2245     BudgetRemaining -= AddCost * (NumTerms - 1);
2246     if (BudgetRemaining < 0)
2247       return true;
2248 
2249     // Ignoring constant term (operand 0), how many of the coeffients are u> 1?
2250     int NumNonZeroDegreeNonOneTerms =
2251         llvm::count_if(make_range(std::next(NAry->op_begin()), NAry->op_end()),
2252                        [](const SCEV *S) {
2253                          auto *SConst = dyn_cast<SCEVConstant>(S);
2254                          return !SConst || SConst->getAPInt().ugt(1);
2255                        });
2256     // Here, *each* one of those will require a multiplication.
2257     BudgetRemaining -= MulCost * NumNonZeroDegreeNonOneTerms;
2258     if (BudgetRemaining < 0)
2259       return true;
2260 
2261     // What is the degree of this polynominal?
2262     int PolyDegree = NAry->getNumOperands() - 1;
2263     assert(PolyDegree >= 1 && "Should be at least affine.");
2264 
2265     // The final term will be:
2266     //   Op_{PolyDegree} * x ^ {PolyDegree}
2267     // Where  x ^ {PolyDegree}  will again require PolyDegree-1 mul operations.
2268     // Note that  x ^ {PolyDegree} = x * x ^ {PolyDegree-1}  so charging for
2269     // x ^ {PolyDegree}  will give us  x ^ {2} .. x ^ {PolyDegree-1}  for free.
2270     // FIXME: this is conservatively correct, but might be overly pessimistic.
2271     BudgetRemaining -= MulCost * (PolyDegree - 1);
2272     if (BudgetRemaining < 0)
2273       return true;
2274 
2275     // And finally, the operands themselves should fit within the budget.
2276     Worklist.insert(Worklist.end(), NAry->operands().begin(),
2277                     NAry->operands().end());
2278     return false; // So far so good, though ops may be too costly?
2279   }
2280 
2281   if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(S)) {
2282     Type *OpType = NAry->getType();
2283 
2284     int PairCost;
2285     switch (S->getSCEVType()) {
2286     case scAddExpr:
2287       PairCost =
2288         TTI.getArithmeticInstrCost(Instruction::Add, OpType, CostKind);
2289       break;
2290     case scMulExpr:
2291       // TODO: this is a very pessimistic cost modelling for Mul,
2292       // because of Bin Pow algorithm actually used by the expander,
2293       // see SCEVExpander::visitMulExpr(), ExpandOpBinPowN().
2294       PairCost =
2295         TTI.getArithmeticInstrCost(Instruction::Mul, OpType, CostKind);
2296       break;
2297     case scSMaxExpr:
2298     case scUMaxExpr:
2299     case scSMinExpr:
2300     case scUMinExpr:
2301       PairCost = TTI.getCmpSelInstrCost(Instruction::ICmp, OpType,
2302                                         CmpInst::makeCmpResultType(OpType),
2303                                         CostKind) +
2304                  TTI.getCmpSelInstrCost(Instruction::Select, OpType,
2305                                         CmpInst::makeCmpResultType(OpType),
2306                                         CostKind);
2307       break;
2308     default:
2309       llvm_unreachable("There are no other variants here.");
2310     }
2311 
2312     assert(NAry->getNumOperands() > 1 &&
2313            "Nary expr should have more than 1 operand.");
2314     // The simple nary expr will require one less op (or pair of ops)
2315     // than the number of it's terms.
2316     BudgetRemaining -= PairCost * (NAry->getNumOperands() - 1);
2317     if (BudgetRemaining < 0)
2318       return true;
2319 
2320     // And finally, the operands themselves should fit within the budget.
2321     Worklist.insert(Worklist.end(), NAry->operands().begin(),
2322                     NAry->operands().end());
2323     return false; // So far so good, though ops may be too costly?
2324   }
2325 
2326   llvm_unreachable("No other scev expressions possible.");
2327 }
2328 
expandCodeForPredicate(const SCEVPredicate * Pred,Instruction * IP)2329 Value *SCEVExpander::expandCodeForPredicate(const SCEVPredicate *Pred,
2330                                             Instruction *IP) {
2331   assert(IP);
2332   switch (Pred->getKind()) {
2333   case SCEVPredicate::P_Union:
2334     return expandUnionPredicate(cast<SCEVUnionPredicate>(Pred), IP);
2335   case SCEVPredicate::P_Equal:
2336     return expandEqualPredicate(cast<SCEVEqualPredicate>(Pred), IP);
2337   case SCEVPredicate::P_Wrap: {
2338     auto *AddRecPred = cast<SCEVWrapPredicate>(Pred);
2339     return expandWrapPredicate(AddRecPred, IP);
2340   }
2341   }
2342   llvm_unreachable("Unknown SCEV predicate type");
2343 }
2344 
expandEqualPredicate(const SCEVEqualPredicate * Pred,Instruction * IP)2345 Value *SCEVExpander::expandEqualPredicate(const SCEVEqualPredicate *Pred,
2346                                           Instruction *IP) {
2347   Value *Expr0 = expandCodeFor(Pred->getLHS(), Pred->getLHS()->getType(), IP);
2348   Value *Expr1 = expandCodeFor(Pred->getRHS(), Pred->getRHS()->getType(), IP);
2349 
2350   Builder.SetInsertPoint(IP);
2351   auto *I = Builder.CreateICmpNE(Expr0, Expr1, "ident.check");
2352   return I;
2353 }
2354 
generateOverflowCheck(const SCEVAddRecExpr * AR,Instruction * Loc,bool Signed)2355 Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR,
2356                                            Instruction *Loc, bool Signed) {
2357   assert(AR->isAffine() && "Cannot generate RT check for "
2358                            "non-affine expression");
2359 
2360   SCEVUnionPredicate Pred;
2361   const SCEV *ExitCount =
2362       SE.getPredicatedBackedgeTakenCount(AR->getLoop(), Pred);
2363 
2364   assert(ExitCount != SE.getCouldNotCompute() && "Invalid loop count");
2365 
2366   const SCEV *Step = AR->getStepRecurrence(SE);
2367   const SCEV *Start = AR->getStart();
2368 
2369   Type *ARTy = AR->getType();
2370   unsigned SrcBits = SE.getTypeSizeInBits(ExitCount->getType());
2371   unsigned DstBits = SE.getTypeSizeInBits(ARTy);
2372 
2373   // The expression {Start,+,Step} has nusw/nssw if
2374   //   Step < 0, Start - |Step| * Backedge <= Start
2375   //   Step >= 0, Start + |Step| * Backedge > Start
2376   // and |Step| * Backedge doesn't unsigned overflow.
2377 
2378   IntegerType *CountTy = IntegerType::get(Loc->getContext(), SrcBits);
2379   Builder.SetInsertPoint(Loc);
2380   Value *TripCountVal = expandCodeFor(ExitCount, CountTy, Loc);
2381 
2382   IntegerType *Ty =
2383       IntegerType::get(Loc->getContext(), SE.getTypeSizeInBits(ARTy));
2384   Type *ARExpandTy = DL.isNonIntegralPointerType(ARTy) ? ARTy : Ty;
2385 
2386   Value *StepValue = expandCodeFor(Step, Ty, Loc);
2387   Value *NegStepValue = expandCodeFor(SE.getNegativeSCEV(Step), Ty, Loc);
2388   Value *StartValue = expandCodeFor(Start, ARExpandTy, Loc);
2389 
2390   ConstantInt *Zero =
2391       ConstantInt::get(Loc->getContext(), APInt::getNullValue(DstBits));
2392 
2393   Builder.SetInsertPoint(Loc);
2394   // Compute |Step|
2395   Value *StepCompare = Builder.CreateICmp(ICmpInst::ICMP_SLT, StepValue, Zero);
2396   Value *AbsStep = Builder.CreateSelect(StepCompare, NegStepValue, StepValue);
2397 
2398   // Get the backedge taken count and truncate or extended to the AR type.
2399   Value *TruncTripCount = Builder.CreateZExtOrTrunc(TripCountVal, Ty);
2400   auto *MulF = Intrinsic::getDeclaration(Loc->getModule(),
2401                                          Intrinsic::umul_with_overflow, Ty);
2402 
2403   // Compute |Step| * Backedge
2404   CallInst *Mul = Builder.CreateCall(MulF, {AbsStep, TruncTripCount}, "mul");
2405   Value *MulV = Builder.CreateExtractValue(Mul, 0, "mul.result");
2406   Value *OfMul = Builder.CreateExtractValue(Mul, 1, "mul.overflow");
2407 
2408   // Compute:
2409   //   Start + |Step| * Backedge < Start
2410   //   Start - |Step| * Backedge > Start
2411   Value *Add = nullptr, *Sub = nullptr;
2412   if (PointerType *ARPtrTy = dyn_cast<PointerType>(ARExpandTy)) {
2413     const SCEV *MulS = SE.getSCEV(MulV);
2414     const SCEV *NegMulS = SE.getNegativeSCEV(MulS);
2415     Add = Builder.CreateBitCast(expandAddToGEP(MulS, ARPtrTy, Ty, StartValue),
2416                                 ARPtrTy);
2417     Sub = Builder.CreateBitCast(
2418         expandAddToGEP(NegMulS, ARPtrTy, Ty, StartValue), ARPtrTy);
2419   } else {
2420     Add = Builder.CreateAdd(StartValue, MulV);
2421     Sub = Builder.CreateSub(StartValue, MulV);
2422   }
2423 
2424   Value *EndCompareGT = Builder.CreateICmp(
2425       Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT, Sub, StartValue);
2426 
2427   Value *EndCompareLT = Builder.CreateICmp(
2428       Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, Add, StartValue);
2429 
2430   // Select the answer based on the sign of Step.
2431   Value *EndCheck =
2432       Builder.CreateSelect(StepCompare, EndCompareGT, EndCompareLT);
2433 
2434   // If the backedge taken count type is larger than the AR type,
2435   // check that we don't drop any bits by truncating it. If we are
2436   // dropping bits, then we have overflow (unless the step is zero).
2437   if (SE.getTypeSizeInBits(CountTy) > SE.getTypeSizeInBits(Ty)) {
2438     auto MaxVal = APInt::getMaxValue(DstBits).zext(SrcBits);
2439     auto *BackedgeCheck =
2440         Builder.CreateICmp(ICmpInst::ICMP_UGT, TripCountVal,
2441                            ConstantInt::get(Loc->getContext(), MaxVal));
2442     BackedgeCheck = Builder.CreateAnd(
2443         BackedgeCheck, Builder.CreateICmp(ICmpInst::ICMP_NE, StepValue, Zero));
2444 
2445     EndCheck = Builder.CreateOr(EndCheck, BackedgeCheck);
2446   }
2447 
2448   EndCheck = Builder.CreateOr(EndCheck, OfMul);
2449   return EndCheck;
2450 }
2451 
expandWrapPredicate(const SCEVWrapPredicate * Pred,Instruction * IP)2452 Value *SCEVExpander::expandWrapPredicate(const SCEVWrapPredicate *Pred,
2453                                          Instruction *IP) {
2454   const auto *A = cast<SCEVAddRecExpr>(Pred->getExpr());
2455   Value *NSSWCheck = nullptr, *NUSWCheck = nullptr;
2456 
2457   // Add a check for NUSW
2458   if (Pred->getFlags() & SCEVWrapPredicate::IncrementNUSW)
2459     NUSWCheck = generateOverflowCheck(A, IP, false);
2460 
2461   // Add a check for NSSW
2462   if (Pred->getFlags() & SCEVWrapPredicate::IncrementNSSW)
2463     NSSWCheck = generateOverflowCheck(A, IP, true);
2464 
2465   if (NUSWCheck && NSSWCheck)
2466     return Builder.CreateOr(NUSWCheck, NSSWCheck);
2467 
2468   if (NUSWCheck)
2469     return NUSWCheck;
2470 
2471   if (NSSWCheck)
2472     return NSSWCheck;
2473 
2474   return ConstantInt::getFalse(IP->getContext());
2475 }
2476 
expandUnionPredicate(const SCEVUnionPredicate * Union,Instruction * IP)2477 Value *SCEVExpander::expandUnionPredicate(const SCEVUnionPredicate *Union,
2478                                           Instruction *IP) {
2479   auto *BoolType = IntegerType::get(IP->getContext(), 1);
2480   Value *Check = ConstantInt::getNullValue(BoolType);
2481 
2482   // Loop over all checks in this set.
2483   for (auto Pred : Union->getPredicates()) {
2484     auto *NextCheck = expandCodeForPredicate(Pred, IP);
2485     Builder.SetInsertPoint(IP);
2486     Check = Builder.CreateOr(Check, NextCheck);
2487   }
2488 
2489   return Check;
2490 }
2491 
2492 namespace {
2493 // Search for a SCEV subexpression that is not safe to expand.  Any expression
2494 // that may expand to a !isSafeToSpeculativelyExecute value is unsafe, namely
2495 // UDiv expressions. We don't know if the UDiv is derived from an IR divide
2496 // instruction, but the important thing is that we prove the denominator is
2497 // nonzero before expansion.
2498 //
2499 // IVUsers already checks that IV-derived expressions are safe. So this check is
2500 // only needed when the expression includes some subexpression that is not IV
2501 // derived.
2502 //
2503 // Currently, we only allow division by a nonzero constant here. If this is
2504 // inadequate, we could easily allow division by SCEVUnknown by using
2505 // ValueTracking to check isKnownNonZero().
2506 //
2507 // We cannot generally expand recurrences unless the step dominates the loop
2508 // header. The expander handles the special case of affine recurrences by
2509 // scaling the recurrence outside the loop, but this technique isn't generally
2510 // applicable. Expanding a nested recurrence outside a loop requires computing
2511 // binomial coefficients. This could be done, but the recurrence has to be in a
2512 // perfectly reduced form, which can't be guaranteed.
2513 struct SCEVFindUnsafe {
2514   ScalarEvolution &SE;
2515   bool IsUnsafe;
2516 
SCEVFindUnsafe__anon76c72e6e0b11::SCEVFindUnsafe2517   SCEVFindUnsafe(ScalarEvolution &se): SE(se), IsUnsafe(false) {}
2518 
follow__anon76c72e6e0b11::SCEVFindUnsafe2519   bool follow(const SCEV *S) {
2520     if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) {
2521       const SCEVConstant *SC = dyn_cast<SCEVConstant>(D->getRHS());
2522       if (!SC || SC->getValue()->isZero()) {
2523         IsUnsafe = true;
2524         return false;
2525       }
2526     }
2527     if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
2528       const SCEV *Step = AR->getStepRecurrence(SE);
2529       if (!AR->isAffine() && !SE.dominates(Step, AR->getLoop()->getHeader())) {
2530         IsUnsafe = true;
2531         return false;
2532       }
2533     }
2534     return true;
2535   }
isDone__anon76c72e6e0b11::SCEVFindUnsafe2536   bool isDone() const { return IsUnsafe; }
2537 };
2538 }
2539 
2540 namespace llvm {
isSafeToExpand(const SCEV * S,ScalarEvolution & SE)2541 bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE) {
2542   SCEVFindUnsafe Search(SE);
2543   visitAll(S, Search);
2544   return !Search.IsUnsafe;
2545 }
2546 
isSafeToExpandAt(const SCEV * S,const Instruction * InsertionPoint,ScalarEvolution & SE)2547 bool isSafeToExpandAt(const SCEV *S, const Instruction *InsertionPoint,
2548                       ScalarEvolution &SE) {
2549   if (!isSafeToExpand(S, SE))
2550     return false;
2551   // We have to prove that the expanded site of S dominates InsertionPoint.
2552   // This is easy when not in the same block, but hard when S is an instruction
2553   // to be expanded somewhere inside the same block as our insertion point.
2554   // What we really need here is something analogous to an OrderedBasicBlock,
2555   // but for the moment, we paper over the problem by handling two common and
2556   // cheap to check cases.
2557   if (SE.properlyDominates(S, InsertionPoint->getParent()))
2558     return true;
2559   if (SE.dominates(S, InsertionPoint->getParent())) {
2560     if (InsertionPoint->getParent()->getTerminator() == InsertionPoint)
2561       return true;
2562     if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S))
2563       for (const Value *V : InsertionPoint->operand_values())
2564         if (V == U->getValue())
2565           return true;
2566   }
2567   return false;
2568 }
2569 }
2570