Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
ScalarEvolutionExpander.cpp
Go to the documentation of this file.
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/ScopeExit.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/Analysis/InstructionSimplify.h"
20#include "llvm/Analysis/LoopInfo.h"
21#include "llvm/Analysis/TargetTransformInfo.h"
22#include "llvm/Analysis/ValueTracking.h"
23#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/Dominators.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/PatternMatch.h"
27#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/Transforms/Utils/LoopUtils.h"
30
31#if LLVM_ENABLE_ABI_BREAKING_CHECKS
32#define SCEV_DEBUG_WITH_TYPE(TYPE, X) DEBUG_WITH_TYPE(TYPE, X)
33#else
34#define SCEV_DEBUG_WITH_TYPE(TYPE, X)
35#endif
36
37using namespacellvm;
38
39cl::opt<unsigned>llvm::SCEVCheapExpansionBudget(
40"scev-cheap-expansion-budget",cl::Hidden,cl::init(4),
41cl::desc("When performing SCEV expansion only if it is cheap to do, this "
42"controls the budget that is considered cheap (default = 4)"));
43
44using namespacePatternMatch;
45
46PoisonFlags::PoisonFlags(constInstruction *I) {
47NUW =false;
48NSW =false;
49Exact =false;
50Disjoint =false;
51NNeg =false;
52SameSign =false;
53GEPNW =GEPNoWrapFlags::none();
54if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(I)) {
55NUW = OBO->hasNoUnsignedWrap();
56NSW = OBO->hasNoSignedWrap();
57 }
58if (auto *PEO = dyn_cast<PossiblyExactOperator>(I))
59Exact = PEO->isExact();
60if (auto *PDI = dyn_cast<PossiblyDisjointInst>(I))
61Disjoint = PDI->isDisjoint();
62if (auto *PNI = dyn_cast<PossiblyNonNegInst>(I))
63NNeg = PNI->hasNonNeg();
64if (auto *TI = dyn_cast<TruncInst>(I)) {
65NUW = TI->hasNoUnsignedWrap();
66NSW = TI->hasNoSignedWrap();
67 }
68if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
69GEPNW =GEP->getNoWrapFlags();
70if (auto *ICmp = dyn_cast<ICmpInst>(I))
71SameSign = ICmp->hasSameSign();
72}
73
74voidPoisonFlags::apply(Instruction *I) {
75if (isa<OverflowingBinaryOperator>(I)) {
76I->setHasNoUnsignedWrap(NUW);
77I->setHasNoSignedWrap(NSW);
78 }
79if (isa<PossiblyExactOperator>(I))
80I->setIsExact(Exact);
81if (auto *PDI = dyn_cast<PossiblyDisjointInst>(I))
82 PDI->setIsDisjoint(Disjoint);
83if (auto *PNI = dyn_cast<PossiblyNonNegInst>(I))
84 PNI->setNonNeg(NNeg);
85if (isa<TruncInst>(I)) {
86I->setHasNoUnsignedWrap(NUW);
87I->setHasNoSignedWrap(NSW);
88 }
89if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
90GEP->setNoWrapFlags(GEPNW);
91if (auto *ICmp = dyn_cast<ICmpInst>(I))
92 ICmp->setSameSign(SameSign);
93}
94
95/// ReuseOrCreateCast - Arrange for there to be a cast of V to Ty at IP,
96/// reusing an existing cast if a suitable one (= dominating IP) exists, or
97/// creating a new one.
98Value *SCEVExpander::ReuseOrCreateCast(Value *V,Type *Ty,
99Instruction::CastOpsOp,
100BasicBlock::iterator IP) {
101// This function must be called with the builder having a valid insertion
102// point. It doesn't need to be the actual IP where the uses of the returned
103// cast will be added, but it must dominate such IP.
104// We use this precondition to produce a cast that will dominate all its
105// uses. In particular, this is crucial for the case where the builder's
106// insertion point *is* the point where we were asked to put the cast.
107// Since we don't know the builder's insertion point is actually
108// where the uses will be added (only that it dominates it), we are
109// not allowed to move it.
110BasicBlock::iterator BIP = Builder.GetInsertPoint();
111
112Value *Ret =nullptr;
113
114// Check to see if there is already a cast!
115for (User *U : V->users()) {
116if (U->getType() != Ty)
117continue;
118CastInst *CI = dyn_cast<CastInst>(U);
119if (!CI || CI->getOpcode() !=Op)
120continue;
121
122// Found a suitable cast that is at IP or comes before IP. Use it. Note that
123// the cast must also properly dominate the Builder's insertion point.
124if (IP->getParent() == CI->getParent() && &*BIP != CI &&
125 (&*IP == CI || CI->comesBefore(&*IP))) {
126 Ret = CI;
127break;
128 }
129 }
130
131// Create a new cast.
132if (!Ret) {
133 SCEVInsertPointGuard Guard(Builder,this);
134 Builder.SetInsertPoint(&*IP);
135Ret = Builder.CreateCast(Op, V, Ty,V->getName());
136 }
137
138// We assert at the end of the function since IP might point to an
139// instruction with different dominance properties than a cast
140// (an invoke for example) and not dominate BIP (but the cast does).
141assert(!isa<Instruction>(Ret) ||
142 SE.DT.dominates(cast<Instruction>(Ret), &*BIP));
143
144returnRet;
145}
146
147BasicBlock::iterator
148SCEVExpander::findInsertPointAfter(Instruction *I,
149Instruction *MustDominate) const{
150BasicBlock::iterator IP = ++I->getIterator();
151if (auto *II = dyn_cast<InvokeInst>(I))
152 IP =II->getNormalDest()->begin();
153
154while (isa<PHINode>(IP))
155 ++IP;
156
157if (isa<FuncletPadInst>(IP) || isa<LandingPadInst>(IP)) {
158 ++IP;
159 }elseif (isa<CatchSwitchInst>(IP)) {
160 IP = MustDominate->getParent()->getFirstInsertionPt();
161 }else {
162assert(!IP->isEHPad() &&"unexpected eh pad!");
163 }
164
165// Adjust insert point to be after instructions inserted by the expander, so
166// we can re-use already inserted instructions. Avoid skipping past the
167// original \p MustDominate, in case it is an inserted instruction.
168while (isInsertedInstruction(&*IP) && &*IP != MustDominate)
169 ++IP;
170
171return IP;
172}
173
174BasicBlock::iterator
175SCEVExpander::GetOptimalInsertionPointForCastOf(Value *V) const{
176// Cast the argument at the beginning of the entry block, after
177// any bitcasts of other arguments.
178if (Argument *A = dyn_cast<Argument>(V)) {
179BasicBlock::iterator IP =A->getParent()->getEntryBlock().begin();
180while ((isa<BitCastInst>(IP) &&
181 isa<Argument>(cast<BitCastInst>(IP)->getOperand(0)) &&
182 cast<BitCastInst>(IP)->getOperand(0) !=A) ||
183 isa<DbgInfoIntrinsic>(IP))
184 ++IP;
185return IP;
186 }
187
188// Cast the instruction immediately after the instruction.
189if (Instruction *I = dyn_cast<Instruction>(V))
190returnfindInsertPointAfter(I, &*Builder.GetInsertPoint());
191
192// Otherwise, this must be some kind of a constant,
193// so let's plop this cast into the function's entry block.
194assert(isa<Constant>(V) &&
195"Expected the cast argument to be a global/constant");
196return Builder.GetInsertBlock()
197 ->getParent()
198 ->getEntryBlock()
199 .getFirstInsertionPt();
200}
201
202/// InsertNoopCastOfTo - Insert a cast of V to the specified type,
203/// which must be possible with a noop cast, doing what we can to share
204/// the casts.
205Value *SCEVExpander::InsertNoopCastOfTo(Value *V,Type *Ty) {
206Instruction::CastOpsOp =CastInst::getCastOpcode(V,false, Ty,false);
207assert((Op == Instruction::BitCast ||
208Op == Instruction::PtrToInt ||
209Op == Instruction::IntToPtr) &&
210"InsertNoopCastOfTo cannot perform non-noop casts!");
211assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) &&
212"InsertNoopCastOfTo cannot change sizes!");
213
214// inttoptr only works for integral pointers. For non-integral pointers, we
215// can create a GEP on null with the integral value as index. Note that
216// it is safe to use GEP of null instead of inttoptr here, because only
217// expressions already based on a GEP of null should be converted to pointers
218// during expansion.
219if (Op == Instruction::IntToPtr) {
220auto *PtrTy = cast<PointerType>(Ty);
221if (DL.isNonIntegralPointerType(PtrTy))
222return Builder.CreatePtrAdd(Constant::getNullValue(PtrTy), V,"scevgep");
223 }
224// Short-circuit unnecessary bitcasts.
225if (Op == Instruction::BitCast) {
226if (V->getType() == Ty)
227returnV;
228if (CastInst *CI = dyn_cast<CastInst>(V)) {
229if (CI->getOperand(0)->getType() == Ty)
230return CI->getOperand(0);
231 }
232 }
233// Short-circuit unnecessary inttoptr<->ptrtoint casts.
234if ((Op == Instruction::PtrToInt ||Op == Instruction::IntToPtr) &&
235 SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) {
236if (CastInst *CI = dyn_cast<CastInst>(V))
237if ((CI->getOpcode() == Instruction::PtrToInt ||
238 CI->getOpcode() == Instruction::IntToPtr) &&
239 SE.getTypeSizeInBits(CI->getType()) ==
240 SE.getTypeSizeInBits(CI->getOperand(0)->getType()))
241return CI->getOperand(0);
242if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
243if ((CE->getOpcode() == Instruction::PtrToInt ||
244CE->getOpcode() == Instruction::IntToPtr) &&
245 SE.getTypeSizeInBits(CE->getType()) ==
246 SE.getTypeSizeInBits(CE->getOperand(0)->getType()))
247returnCE->getOperand(0);
248 }
249
250// Fold a cast of a constant.
251if (Constant *C = dyn_cast<Constant>(V))
252returnConstantExpr::getCast(Op,C, Ty);
253
254// Try to reuse existing cast, or insert one.
255return ReuseOrCreateCast(V, Ty,Op, GetOptimalInsertionPointForCastOf(V));
256}
257
258/// InsertBinop - Insert the specified binary operator, doing a small amount
259/// of work to avoid inserting an obviously redundant operation, and hoisting
260/// to an outer loop when the opportunity is there and it is safe.
261Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode,
262Value *LHS,Value *RHS,
263SCEV::NoWrapFlags Flags,bool IsSafeToHoist) {
264// Fold a binop with constant operands.
265if (Constant *CLHS = dyn_cast<Constant>(LHS))
266if (Constant *CRHS = dyn_cast<Constant>(RHS))
267if (Constant *Res =ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, DL))
268return Res;
269
270// Do a quick scan to see if we have this binop nearby. If so, reuse it.
271unsigned ScanLimit = 6;
272BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
273// Scanning starts from the last instruction before the insertion point.
274BasicBlock::iterator IP = Builder.GetInsertPoint();
275if (IP != BlockBegin) {
276 --IP;
277for (; ScanLimit; --IP, --ScanLimit) {
278// Don't count dbg.value against the ScanLimit, to avoid perturbing the
279// generated code.
280if (isa<DbgInfoIntrinsic>(IP))
281 ScanLimit++;
282
283auto canGenerateIncompatiblePoison = [&Flags](Instruction *I) {
284// Ensure that no-wrap flags match.
285if (isa<OverflowingBinaryOperator>(I)) {
286if (I->hasNoSignedWrap() != (Flags &SCEV::FlagNSW))
287returntrue;
288if (I->hasNoUnsignedWrap() != (Flags &SCEV::FlagNUW))
289returntrue;
290 }
291// Conservatively, do not use any instruction which has any of exact
292// flags installed.
293if (isa<PossiblyExactOperator>(I) &&I->isExact())
294returntrue;
295returnfalse;
296 };
297if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) ==LHS &&
298 IP->getOperand(1) ==RHS && !canGenerateIncompatiblePoison(&*IP))
299return &*IP;
300if (IP == BlockBegin)break;
301 }
302 }
303
304// Save the original insertion point so we can restore it when we're done.
305DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc();
306 SCEVInsertPointGuard Guard(Builder,this);
307
308if (IsSafeToHoist) {
309// Move the insertion point out of as many loops as we can.
310while (constLoop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) {
311if (!L->isLoopInvariant(LHS) || !L->isLoopInvariant(RHS))break;
312BasicBlock *Preheader =L->getLoopPreheader();
313if (!Preheader)break;
314
315// Ok, move up a level.
316 Builder.SetInsertPoint(Preheader->getTerminator());
317 }
318 }
319
320// If we haven't found this binop, insert it.
321// TODO: Use the Builder, which will make CreateBinOp below fold with
322// InstSimplifyFolder.
323Instruction *BO = Builder.Insert(BinaryOperator::Create(Opcode, LHS, RHS));
324 BO->setDebugLoc(Loc);
325if (Flags &SCEV::FlagNUW)
326 BO->setHasNoUnsignedWrap();
327if (Flags &SCEV::FlagNSW)
328 BO->setHasNoSignedWrap();
329
330return BO;
331}
332
333/// expandAddToGEP - Expand an addition expression with a pointer type into
334/// a GEP instead of using ptrtoint+arithmetic+inttoptr. This helps
335/// BasicAliasAnalysis and other passes analyze the result. See the rules
336/// for getelementptr vs. inttoptr in
337/// http://llvm.org/docs/LangRef.html#pointeraliasing
338/// for details.
339///
340/// Design note: The correctness of using getelementptr here depends on
341/// ScalarEvolution not recognizing inttoptr and ptrtoint operators, as
342/// they may introduce pointer arithmetic which may not be safely converted
343/// into getelementptr.
344///
345/// Design note: It might seem desirable for this function to be more
346/// loop-aware. If some of the indices are loop-invariant while others
347/// aren't, it might seem desirable to emit multiple GEPs, keeping the
348/// loop-invariant portions of the overall computation outside the loop.
349/// However, there are a few reasons this is not done here. Hoisting simple
350/// arithmetic is a low-level optimization that often isn't very
351/// important until late in the optimization process. In fact, passes
352/// like InstructionCombining will combine GEPs, even if it means
353/// pushing loop-invariant computation down into loops, so even if the
354/// GEPs were split here, the work would quickly be undone. The
355/// LoopStrengthReduction pass, which is usually run quite late (and
356/// after the last InstructionCombining pass), takes care of hoisting
357/// loop-invariant portions of expressions, after considering what
358/// can be folded using target addressing modes.
359///
360Value *SCEVExpander::expandAddToGEP(constSCEV *Offset,Value *V,
361SCEV::NoWrapFlags Flags) {
362assert(!isa<Instruction>(V) ||
363 SE.DT.dominates(cast<Instruction>(V), &*Builder.GetInsertPoint()));
364
365Value *Idx = expand(Offset);
366GEPNoWrapFlags NW = (Flags &SCEV::FlagNUW) ?GEPNoWrapFlags::noUnsignedWrap()
367 :GEPNoWrapFlags::none();
368
369// Fold a GEP with constant operands.
370if (Constant *CLHS = dyn_cast<Constant>(V))
371if (Constant *CRHS = dyn_cast<Constant>(Idx))
372return Builder.CreatePtrAdd(CLHS, CRHS,"", NW);
373
374// Do a quick scan to see if we have this GEP nearby. If so, reuse it.
375unsigned ScanLimit = 6;
376BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
377// Scanning starts from the last instruction before the insertion point.
378BasicBlock::iterator IP = Builder.GetInsertPoint();
379if (IP != BlockBegin) {
380 --IP;
381for (; ScanLimit; --IP, --ScanLimit) {
382// Don't count dbg.value against the ScanLimit, to avoid perturbing the
383// generated code.
384if (isa<DbgInfoIntrinsic>(IP))
385 ScanLimit++;
386if (auto *GEP = dyn_cast<GetElementPtrInst>(IP)) {
387if (GEP->getPointerOperand() == V &&
388GEP->getSourceElementType() == Builder.getInt8Ty() &&
389GEP->getOperand(1) ==Idx) {
390 rememberFlags(GEP);
391GEP->setNoWrapFlags(GEP->getNoWrapFlags() & NW);
392return &*IP;
393 }
394 }
395if (IP == BlockBegin)break;
396 }
397 }
398
399// Save the original insertion point so we can restore it when we're done.
400 SCEVInsertPointGuard Guard(Builder,this);
401
402// Move the insertion point out of as many loops as we can.
403while (constLoop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) {
404if (!L->isLoopInvariant(V) || !L->isLoopInvariant(Idx))break;
405BasicBlock *Preheader =L->getLoopPreheader();
406if (!Preheader)break;
407
408// Ok, move up a level.
409 Builder.SetInsertPoint(Preheader->getTerminator());
410 }
411
412// Emit a GEP.
413return Builder.CreatePtrAdd(V,Idx,"scevgep", NW);
414}
415
416/// PickMostRelevantLoop - Given two loops pick the one that's most relevant for
417/// SCEV expansion. If they are nested, this is the most nested. If they are
418/// neighboring, pick the later.
419staticconstLoop *PickMostRelevantLoop(constLoop *A,constLoop *B,
420DominatorTree &DT) {
421if (!A)returnB;
422if (!B)returnA;
423if (A->contains(B))returnB;
424if (B->contains(A))returnA;
425if (DT.dominates(A->getHeader(),B->getHeader()))returnB;
426if (DT.dominates(B->getHeader(),A->getHeader()))returnA;
427returnA;// Arbitrarily break the tie.
428}
429
430/// getRelevantLoop - Get the most relevant loop associated with the given
431/// expression, according to PickMostRelevantLoop.
432constLoop *SCEVExpander::getRelevantLoop(constSCEV *S) {
433// Test whether we've already computed the most relevant loop for this SCEV.
434auto Pair = RelevantLoops.insert(std::make_pair(S,nullptr));
435if (!Pair.second)
436return Pair.first->second;
437
438switch (S->getSCEVType()) {
439casescConstant:
440casescVScale:
441returnnullptr;// A constant has no relevant loops.
442casescTruncate:
443casescZeroExtend:
444casescSignExtend:
445casescPtrToInt:
446casescAddExpr:
447casescMulExpr:
448casescUDivExpr:
449casescAddRecExpr:
450casescUMaxExpr:
451casescSMaxExpr:
452casescUMinExpr:
453casescSMinExpr:
454casescSequentialUMinExpr: {
455constLoop *L =nullptr;
456if (constSCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S))
457L = AR->getLoop();
458for (constSCEV *Op : S->operands())
459L =PickMostRelevantLoop(L, getRelevantLoop(Op), SE.DT);
460return RelevantLoops[S] =L;
461 }
462casescUnknown: {
463constSCEVUnknown *U = cast<SCEVUnknown>(S);
464if (constInstruction *I = dyn_cast<Instruction>(U->getValue()))
465return Pair.first->second = SE.LI.getLoopFor(I->getParent());
466// A non-instruction has no relevant loops.
467returnnullptr;
468 }
469casescCouldNotCompute:
470llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
471 }
472llvm_unreachable("Unexpected SCEV type!");
473}
474
475namespace{
476
477/// LoopCompare - Compare loops by PickMostRelevantLoop.
478classLoopCompare {
479DominatorTree &DT;
480public:
481explicit LoopCompare(DominatorTree &dt) : DT(dt) {}
482
483bool operator()(std::pair<const Loop *, const SCEV *>LHS,
484 std::pair<const Loop *, const SCEV *>RHS) const{
485// Keep pointer operands sorted at the end.
486if (LHS.second->getType()->isPointerTy() !=
487RHS.second->getType()->isPointerTy())
488returnLHS.second->getType()->isPointerTy();
489
490// Compare loops with PickMostRelevantLoop.
491if (LHS.first !=RHS.first)
492returnPickMostRelevantLoop(LHS.first,RHS.first, DT) !=LHS.first;
493
494// If one operand is a non-constant negative and the other is not,
495// put the non-constant negative on the right so that a sub can
496// be used instead of a negate and add.
497if (LHS.second->isNonConstantNegative()) {
498if (!RHS.second->isNonConstantNegative())
499returnfalse;
500 }elseif (RHS.second->isNonConstantNegative())
501returntrue;
502
503// Otherwise they are equivalent according to this comparison.
504returnfalse;
505 }
506};
507
508}
509
510Value *SCEVExpander::visitAddExpr(constSCEVAddExpr *S) {
511// Recognize the canonical representation of an unsimplifed urem.
512constSCEV *URemLHS =nullptr;
513constSCEV *URemRHS =nullptr;
514if (SE.matchURem(S, URemLHS, URemRHS)) {
515Value *LHS = expand(URemLHS);
516Value *RHS = expand(URemRHS);
517return InsertBinop(Instruction::URem, LHS, RHS,SCEV::FlagAnyWrap,
518/*IsSafeToHoist*/false);
519 }
520
521// Collect all the add operands in a loop, along with their associated loops.
522// Iterate in reverse so that constants are emitted last, all else equal, and
523// so that pointer operands are inserted first, which the code below relies on
524// to form more involved GEPs.
525SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops;
526for (constSCEV *Op :reverse(S->operands()))
527 OpsAndLoops.push_back(std::make_pair(getRelevantLoop(Op),Op));
528
529// Sort by loop. Use a stable sort so that constants follow non-constants and
530// pointer operands precede non-pointer operands.
531llvm::stable_sort(OpsAndLoops, LoopCompare(SE.DT));
532
533// Emit instructions to add all the operands. Hoist as much as possible
534// out of loops, and form meaningful getelementptrs where possible.
535Value *Sum =nullptr;
536for (autoI = OpsAndLoops.begin(), E = OpsAndLoops.end();I != E;) {
537constLoop *CurLoop =I->first;
538constSCEV *Op =I->second;
539if (!Sum) {
540// This is the first operand. Just expand it.
541 Sum = expand(Op);
542 ++I;
543continue;
544 }
545
546assert(!Op->getType()->isPointerTy() &&"Only first op can be pointer");
547if (isa<PointerType>(Sum->getType())) {
548// The running sum expression is a pointer. Try to form a getelementptr
549// at this level with that as the base.
550SmallVector<const SCEV *, 4> NewOps;
551for (;I != E &&I->first == CurLoop; ++I) {
552// If the operand is SCEVUnknown and not instructions, peek through
553// it, to enable more of it to be folded into the GEP.
554constSCEV *X =I->second;
555if (constSCEVUnknown *U = dyn_cast<SCEVUnknown>(X))
556if (!isa<Instruction>(U->getValue()))
557X = SE.getSCEV(U->getValue());
558 NewOps.push_back(X);
559 }
560 Sum = expandAddToGEP(SE.getAddExpr(NewOps), Sum, S->getNoWrapFlags());
561 }elseif (Op->isNonConstantNegative()) {
562// Instead of doing a negate and add, just do a subtract.
563Value *W = expand(SE.getNegativeSCEV(Op));
564 Sum = InsertBinop(Instruction::Sub, Sum, W,SCEV::FlagAnyWrap,
565/*IsSafeToHoist*/true);
566 ++I;
567 }else {
568// A simple add.
569Value *W = expand(Op);
570// Canonicalize a constant to the RHS.
571if (isa<Constant>(Sum))
572std::swap(Sum, W);
573 Sum = InsertBinop(Instruction::Add, Sum, W, S->getNoWrapFlags(),
574/*IsSafeToHoist*/true);
575 ++I;
576 }
577 }
578
579return Sum;
580}
581
582Value *SCEVExpander::visitMulExpr(constSCEVMulExpr *S) {
583Type *Ty = S->getType();
584
585// Collect all the mul operands in a loop, along with their associated loops.
586// Iterate in reverse so that constants are emitted last, all else equal.
587SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops;
588for (constSCEV *Op :reverse(S->operands()))
589 OpsAndLoops.push_back(std::make_pair(getRelevantLoop(Op),Op));
590
591// Sort by loop. Use a stable sort so that constants follow non-constants.
592llvm::stable_sort(OpsAndLoops, LoopCompare(SE.DT));
593
594// Emit instructions to mul all the operands. Hoist as much as possible
595// out of loops.
596Value *Prod =nullptr;
597autoI = OpsAndLoops.begin();
598
599// Expand the calculation of X pow N in the following manner:
600// Let N = P1 + P2 + ... + PK, where all P are powers of 2. Then:
601// X pow N = (X pow P1) * (X pow P2) * ... * (X pow PK).
602constauto ExpandOpBinPowN = [this, &I, &OpsAndLoops]() {
603auto E =I;
604// Calculate how many times the same operand from the same loop is included
605// into this power.
606uint64_tExponent = 0;
607constuint64_t MaxExponent =UINT64_MAX >> 1;
608// No one sane will ever try to calculate such huge exponents, but if we
609// need this, we stop on UINT64_MAX / 2 because we need to exit the loop
610// below when the power of 2 exceeds our Exponent, and we want it to be
611// 1u << 31 at most to not deal with unsigned overflow.
612while (E != OpsAndLoops.end() && *I == *E &&Exponent != MaxExponent) {
613 ++Exponent;
614 ++E;
615 }
616assert(Exponent > 0 &&"Trying to calculate a zeroth exponent of operand?");
617
618// Calculate powers with exponents 1, 2, 4, 8 etc. and include those of them
619// that are needed into the result.
620Value *P = expand(I->second);
621Value *Result =nullptr;
622if (Exponent & 1)
623Result =P;
624for (uint64_t BinExp = 2; BinExp <=Exponent; BinExp <<= 1) {
625P = InsertBinop(Instruction::Mul,P,P,SCEV::FlagAnyWrap,
626/*IsSafeToHoist*/true);
627if (Exponent & BinExp)
628Result =Result ? InsertBinop(Instruction::Mul, Result,P,
629SCEV::FlagAnyWrap,
630/*IsSafeToHoist*/true)
631 :P;
632 }
633
634I = E;
635assert(Result &&"Nothing was expanded?");
636returnResult;
637 };
638
639while (I != OpsAndLoops.end()) {
640if (!Prod) {
641// This is the first operand. Just expand it.
642 Prod = ExpandOpBinPowN();
643 }elseif (I->second->isAllOnesValue()) {
644// Instead of doing a multiply by negative one, just do a negate.
645 Prod = InsertBinop(Instruction::Sub,Constant::getNullValue(Ty), Prod,
646SCEV::FlagAnyWrap,/*IsSafeToHoist*/true);
647 ++I;
648 }else {
649// A simple mul.
650Value *W = ExpandOpBinPowN();
651// Canonicalize a constant to the RHS.
652if (isa<Constant>(Prod))std::swap(Prod, W);
653constAPInt *RHS;
654if (match(W,m_Power2(RHS))) {
655// Canonicalize Prod*(1<<C) to Prod<<C.
656assert(!Ty->isVectorTy() &&"vector types are not SCEVable");
657auto NWFlags = S->getNoWrapFlags();
658// clear nsw flag if shl will produce poison value.
659if (RHS->logBase2() ==RHS->getBitWidth() - 1)
660 NWFlags =ScalarEvolution::clearFlags(NWFlags,SCEV::FlagNSW);
661 Prod = InsertBinop(Instruction::Shl, Prod,
662 ConstantInt::get(Ty,RHS->logBase2()), NWFlags,
663/*IsSafeToHoist*/true);
664 }else {
665 Prod = InsertBinop(Instruction::Mul, Prod, W, S->getNoWrapFlags(),
666/*IsSafeToHoist*/true);
667 }
668 }
669 }
670
671return Prod;
672}
673
674Value *SCEVExpander::visitUDivExpr(constSCEVUDivExpr *S) {
675Value *LHS = expand(S->getLHS());
676if (constSCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) {
677constAPInt &RHS =SC->getAPInt();
678if (RHS.isPowerOf2())
679return InsertBinop(Instruction::LShr, LHS,
680 ConstantInt::get(SC->getType(),RHS.logBase2()),
681SCEV::FlagAnyWrap,/*IsSafeToHoist*/true);
682 }
683
684constSCEV *RHSExpr = S->getRHS();
685Value *RHS = expand(RHSExpr);
686if (SafeUDivMode) {
687bool GuaranteedNotPoison =
688 ScalarEvolution::isGuaranteedNotToBePoison(RHSExpr);
689if (!GuaranteedNotPoison)
690RHS = Builder.CreateFreeze(RHS);
691
692// We need an umax if either RHSExpr is not known to be zero, or if it is
693// not guaranteed to be non-poison. In the later case, the frozen poison may
694// be 0.
695if (!SE.isKnownNonZero(RHSExpr) || !GuaranteedNotPoison)
696RHS = Builder.CreateIntrinsic(RHS->getType(), Intrinsic::umax,
697 {RHS, ConstantInt::get(RHS->getType(), 1)});
698 }
699return InsertBinop(Instruction::UDiv, LHS, RHS,SCEV::FlagAnyWrap,
700/*IsSafeToHoist*/ SE.isKnownNonZero(S->getRHS()));
701}
702
703/// Determine if this is a well-behaved chain of instructions leading back to
704/// the PHI. If so, it may be reused by expanded expressions.
705bool SCEVExpander::isNormalAddRecExprPHI(PHINode *PN,Instruction *IncV,
706constLoop *L) {
707if (IncV->getNumOperands() == 0 || isa<PHINode>(IncV) ||
708 (isa<CastInst>(IncV) && !isa<BitCastInst>(IncV)))
709returnfalse;
710// If any of the operands don't dominate the insert position, bail.
711// Addrec operands are always loop-invariant, so this can only happen
712// if there are instructions which haven't been hoisted.
713if (L == IVIncInsertLoop) {
714for (Use &Op :llvm::drop_begin(IncV->operands()))
715if (Instruction *OInst = dyn_cast<Instruction>(Op))
716if (!SE.DT.dominates(OInst, IVIncInsertPos))
717returnfalse;
718 }
719// Advance to the next instruction.
720 IncV = dyn_cast<Instruction>(IncV->getOperand(0));
721if (!IncV)
722returnfalse;
723
724if (IncV->mayHaveSideEffects())
725returnfalse;
726
727if (IncV == PN)
728returntrue;
729
730return isNormalAddRecExprPHI(PN, IncV, L);
731}
732
733/// getIVIncOperand returns an induction variable increment's induction
734/// variable operand.
735///
736/// If allowScale is set, any type of GEP is allowed as long as the nonIV
737/// operands dominate InsertPos.
738///
739/// If allowScale is not set, ensure that a GEP increment conforms to one of the
740/// simple patterns generated by getAddRecExprPHILiterally and
741/// expandAddtoGEP. If the pattern isn't recognized, return NULL.
742Instruction *SCEVExpander::getIVIncOperand(Instruction *IncV,
743Instruction *InsertPos,
744bool allowScale) {
745if (IncV == InsertPos)
746returnnullptr;
747
748switch (IncV->getOpcode()) {
749default:
750returnnullptr;
751// Check for a simple Add/Sub or GEP of a loop invariant step.
752case Instruction::Add:
753case Instruction::Sub: {
754Instruction *OInst = dyn_cast<Instruction>(IncV->getOperand(1));
755if (!OInst || SE.DT.dominates(OInst, InsertPos))
756return dyn_cast<Instruction>(IncV->getOperand(0));
757returnnullptr;
758 }
759case Instruction::BitCast:
760return dyn_cast<Instruction>(IncV->getOperand(0));
761case Instruction::GetElementPtr:
762for (Use &U :llvm::drop_begin(IncV->operands())) {
763if (isa<Constant>(U))
764continue;
765if (Instruction *OInst = dyn_cast<Instruction>(U)) {
766if (!SE.DT.dominates(OInst, InsertPos))
767returnnullptr;
768 }
769if (allowScale) {
770// allow any kind of GEP as long as it can be hoisted.
771continue;
772 }
773// GEPs produced by SCEVExpander use i8 element type.
774if (!cast<GEPOperator>(IncV)->getSourceElementType()->isIntegerTy(8))
775returnnullptr;
776break;
777 }
778return dyn_cast<Instruction>(IncV->getOperand(0));
779 }
780}
781
782/// If the insert point of the current builder or any of the builders on the
783/// stack of saved builders has 'I' as its insert point, update it to point to
784/// the instruction after 'I'. This is intended to be used when the instruction
785/// 'I' is being moved. If this fixup is not done and 'I' is moved to a
786/// different block, the inconsistent insert point (with a mismatched
787/// Instruction and Block) can lead to an instruction being inserted in a block
788/// other than its parent.
789void SCEVExpander::fixupInsertPoints(Instruction *I) {
790BasicBlock::iterator It(*I);
791BasicBlock::iterator NewInsertPt = std::next(It);
792if (Builder.GetInsertPoint() == It)
793 Builder.SetInsertPoint(&*NewInsertPt);
794for (auto *InsertPtGuard : InsertPointGuards)
795if (InsertPtGuard->GetInsertPoint() == It)
796 InsertPtGuard->SetInsertPoint(NewInsertPt);
797}
798
799/// hoistStep - Attempt to hoist a simple IV increment above InsertPos to make
800/// it available to other uses in this loop. Recursively hoist any operands,
801/// until we reach a value that dominates InsertPos.
802boolSCEVExpander::hoistIVInc(Instruction *IncV,Instruction *InsertPos,
803bool RecomputePoisonFlags) {
804auto FixupPoisonFlags = [this](Instruction *I) {
805// Drop flags that are potentially inferred from old context and infer flags
806// in new context.
807 rememberFlags(I);
808I->dropPoisonGeneratingFlags();
809if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(I))
810if (auto Flags = SE.getStrengthenedNoWrapFlagsFromBinOp(OBO)) {
811auto *BO = cast<BinaryOperator>(I);
812 BO->setHasNoUnsignedWrap(
813ScalarEvolution::maskFlags(*Flags,SCEV::FlagNUW) ==SCEV::FlagNUW);
814 BO->setHasNoSignedWrap(
815ScalarEvolution::maskFlags(*Flags,SCEV::FlagNSW) ==SCEV::FlagNSW);
816 }
817 };
818
819if (SE.DT.dominates(IncV, InsertPos)) {
820if (RecomputePoisonFlags)
821 FixupPoisonFlags(IncV);
822returntrue;
823 }
824
825// InsertPos must itself dominate IncV so that IncV's new position satisfies
826// its existing users.
827if (isa<PHINode>(InsertPos) ||
828 !SE.DT.dominates(InsertPos->getParent(), IncV->getParent()))
829returnfalse;
830
831if (!SE.LI.movementPreservesLCSSAForm(IncV, InsertPos))
832returnfalse;
833
834// Check that the chain of IV operands leading back to Phi can be hoisted.
835SmallVector<Instruction*, 4> IVIncs;
836for(;;) {
837Instruction *Oper =getIVIncOperand(IncV, InsertPos,/*allowScale*/true);
838if (!Oper)
839returnfalse;
840// IncV is safe to hoist.
841 IVIncs.push_back(IncV);
842 IncV = Oper;
843if (SE.DT.dominates(IncV, InsertPos))
844break;
845 }
846for (Instruction *I :llvm::reverse(IVIncs)) {
847 fixupInsertPoints(I);
848I->moveBefore(InsertPos->getIterator());
849if (RecomputePoisonFlags)
850 FixupPoisonFlags(I);
851 }
852returntrue;
853}
854
855boolSCEVExpander::canReuseFlagsFromOriginalIVInc(PHINode *OrigPhi,
856PHINode *WidePhi,
857Instruction *OrigInc,
858Instruction *WideInc) {
859returnmatch(OrigInc,m_c_BinOp(m_Specific(OrigPhi),m_Value())) &&
860match(WideInc,m_c_BinOp(m_Specific(WidePhi),m_Value())) &&
861 OrigInc->getOpcode() == WideInc->getOpcode();
862}
863
864/// Determine if this cyclic phi is in a form that would have been generated by
865/// LSR. We don't care if the phi was actually expanded in this pass, as long
866/// as it is in a low-cost form, for example, no implied multiplication. This
867/// should match any patterns generated by getAddRecExprPHILiterally and
868/// expandAddtoGEP.
869bool SCEVExpander::isExpandedAddRecExprPHI(PHINode *PN,Instruction *IncV,
870constLoop *L) {
871for(Instruction *IVOper = IncV;
872 (IVOper =getIVIncOperand(IVOper, L->getLoopPreheader()->getTerminator(),
873/*allowScale=*/false));) {
874if (IVOper == PN)
875returntrue;
876 }
877returnfalse;
878}
879
880/// expandIVInc - Expand an IV increment at Builder's current InsertPos.
881/// Typically this is the LatchBlock terminator or IVIncInsertPos, but we may
882/// need to materialize IV increments elsewhere to handle difficult situations.
883Value *SCEVExpander::expandIVInc(PHINode *PN,Value *StepV,constLoop *L,
884bool useSubtract) {
885Value *IncV;
886// If the PHI is a pointer, use a GEP, otherwise use an add or sub.
887if (PN->getType()->isPointerTy()) {
888// TODO: Change name to IVName.iv.next.
889 IncV = Builder.CreatePtrAdd(PN, StepV,"scevgep");
890 }else {
891 IncV = useSubtract ?
892 Builder.CreateSub(PN, StepV,Twine(IVName) +".iv.next") :
893 Builder.CreateAdd(PN, StepV,Twine(IVName) +".iv.next");
894 }
895return IncV;
896}
897
898/// Check whether we can cheaply express the requested SCEV in terms of
899/// the available PHI SCEV by truncation and/or inversion of the step.
900staticboolcanBeCheaplyTransformed(ScalarEvolution &SE,
901constSCEVAddRecExpr *Phi,
902constSCEVAddRecExpr *Requested,
903bool &InvertStep) {
904// We can't transform to match a pointer PHI.
905Type *PhiTy = Phi->getType();
906Type *RequestedTy = Requested->getType();
907if (PhiTy->isPointerTy() || RequestedTy->isPointerTy())
908returnfalse;
909
910if (RequestedTy->getIntegerBitWidth() > PhiTy->getIntegerBitWidth())
911returnfalse;
912
913// Try truncate it if necessary.
914 Phi = dyn_cast<SCEVAddRecExpr>(SE.getTruncateOrNoop(Phi, RequestedTy));
915if (!Phi)
916returnfalse;
917
918// Check whether truncation will help.
919if (Phi == Requested) {
920 InvertStep =false;
921returntrue;
922 }
923
924// Check whether inverting will help: {R,+,-1} == R - {0,+,1}.
925if (SE.getMinusSCEV(Requested->getStart(), Requested) == Phi) {
926 InvertStep =true;
927returntrue;
928 }
929
930returnfalse;
931}
932
933staticboolIsIncrementNSW(ScalarEvolution &SE,constSCEVAddRecExpr *AR) {
934if (!isa<IntegerType>(AR->getType()))
935returnfalse;
936
937unsignedBitWidth = cast<IntegerType>(AR->getType())->getBitWidth();
938Type *WideTy =IntegerType::get(AR->getType()->getContext(),BitWidth * 2);
939constSCEV *Step = AR->getStepRecurrence(SE);
940constSCEV *OpAfterExtend = SE.getAddExpr(SE.getSignExtendExpr(Step, WideTy),
941 SE.getSignExtendExpr(AR, WideTy));
942constSCEV *ExtendAfterOp =
943 SE.getSignExtendExpr(SE.getAddExpr(AR, Step), WideTy);
944return ExtendAfterOp == OpAfterExtend;
945}
946
947staticboolIsIncrementNUW(ScalarEvolution &SE,constSCEVAddRecExpr *AR) {
948if (!isa<IntegerType>(AR->getType()))
949returnfalse;
950
951unsignedBitWidth = cast<IntegerType>(AR->getType())->getBitWidth();
952Type *WideTy =IntegerType::get(AR->getType()->getContext(),BitWidth * 2);
953constSCEV *Step = AR->getStepRecurrence(SE);
954constSCEV *OpAfterExtend = SE.getAddExpr(SE.getZeroExtendExpr(Step, WideTy),
955 SE.getZeroExtendExpr(AR, WideTy));
956constSCEV *ExtendAfterOp =
957 SE.getZeroExtendExpr(SE.getAddExpr(AR, Step), WideTy);
958return ExtendAfterOp == OpAfterExtend;
959}
960
961/// getAddRecExprPHILiterally - Helper for expandAddRecExprLiterally. Expand
962/// the base addrec, which is the addrec without any non-loop-dominating
963/// values, and return the PHI.
964PHINode *
965SCEVExpander::getAddRecExprPHILiterally(constSCEVAddRecExpr *Normalized,
966constLoop *L,Type *&TruncTy,
967bool &InvertStep) {
968assert((!IVIncInsertLoop || IVIncInsertPos) &&
969"Uninitialized insert position");
970
971// Reuse a previously-inserted PHI, if present.
972BasicBlock *LatchBlock =L->getLoopLatch();
973if (LatchBlock) {
974PHINode *AddRecPhiMatch =nullptr;
975Instruction *IncV =nullptr;
976 TruncTy =nullptr;
977 InvertStep =false;
978
979// Only try partially matching scevs that need truncation and/or
980// step-inversion if we know this loop is outside the current loop.
981bool TryNonMatchingSCEV =
982 IVIncInsertLoop &&
983 SE.DT.properlyDominates(LatchBlock, IVIncInsertLoop->getHeader());
984
985for (PHINode &PN :L->getHeader()->phis()) {
986if (!SE.isSCEVable(PN.getType()))
987continue;
988
989// We should not look for a incomplete PHI. Getting SCEV for a incomplete
990// PHI has no meaning at all.
991if (!PN.isComplete()) {
992SCEV_DEBUG_WITH_TYPE(
993 DebugType,dbgs() <<"One incomplete PHI is found: " << PN <<"\n");
994continue;
995 }
996
997constSCEVAddRecExpr *PhiSCEV = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(&PN));
998if (!PhiSCEV)
999continue;
1000
1001bool IsMatchingSCEV = PhiSCEV == Normalized;
1002// We only handle truncation and inversion of phi recurrences for the
1003// expanded expression if the expanded expression's loop dominates the
1004// loop we insert to. Check now, so we can bail out early.
1005if (!IsMatchingSCEV && !TryNonMatchingSCEV)
1006continue;
1007
1008// TODO: this possibly can be reworked to avoid this cast at all.
1009Instruction *TempIncV =
1010 dyn_cast<Instruction>(PN.getIncomingValueForBlock(LatchBlock));
1011if (!TempIncV)
1012continue;
1013
1014// Check whether we can reuse this PHI node.
1015if (LSRMode) {
1016if (!isExpandedAddRecExprPHI(&PN, TempIncV, L))
1017continue;
1018 }else {
1019if (!isNormalAddRecExprPHI(&PN, TempIncV, L))
1020continue;
1021 }
1022
1023// Stop if we have found an exact match SCEV.
1024if (IsMatchingSCEV) {
1025 IncV = TempIncV;
1026 TruncTy =nullptr;
1027 InvertStep =false;
1028 AddRecPhiMatch = &PN;
1029break;
1030 }
1031
1032// Try whether the phi can be translated into the requested form
1033// (truncated and/or offset by a constant).
1034if ((!TruncTy || InvertStep) &&
1035canBeCheaplyTransformed(SE, PhiSCEV, Normalized, InvertStep)) {
1036// Record the phi node. But don't stop we might find an exact match
1037// later.
1038 AddRecPhiMatch = &PN;
1039 IncV = TempIncV;
1040 TruncTy = Normalized->getType();
1041 }
1042 }
1043
1044if (AddRecPhiMatch) {
1045// Ok, the add recurrence looks usable.
1046// Remember this PHI, even in post-inc mode.
1047 InsertedValues.insert(AddRecPhiMatch);
1048// Remember the increment.
1049 rememberInstruction(IncV);
1050// Those values were not actually inserted but re-used.
1051 ReusedValues.insert(AddRecPhiMatch);
1052 ReusedValues.insert(IncV);
1053return AddRecPhiMatch;
1054 }
1055 }
1056
1057// Save the original insertion point so we can restore it when we're done.
1058 SCEVInsertPointGuard Guard(Builder,this);
1059
1060// Another AddRec may need to be recursively expanded below. For example, if
1061// this AddRec is quadratic, the StepV may itself be an AddRec in this
1062// loop. Remove this loop from the PostIncLoops set before expanding such
1063// AddRecs. Otherwise, we cannot find a valid position for the step
1064// (i.e. StepV can never dominate its loop header). Ideally, we could do
1065// SavedIncLoops.swap(PostIncLoops), but we generally have a single element,
1066// so it's not worth implementing SmallPtrSet::swap.
1067PostIncLoopSet SavedPostIncLoops = PostIncLoops;
1068 PostIncLoops.clear();
1069
1070// Expand code for the start value into the loop preheader.
1071assert(L->getLoopPreheader() &&
1072"Can't expand add recurrences without a loop preheader!");
1073Value *StartV =
1074 expand(Normalized->getStart(),L->getLoopPreheader()->getTerminator());
1075
1076// StartV must have been be inserted into L's preheader to dominate the new
1077// phi.
1078assert(!isa<Instruction>(StartV) ||
1079 SE.DT.properlyDominates(cast<Instruction>(StartV)->getParent(),
1080L->getHeader()));
1081
1082// Expand code for the step value. Do this before creating the PHI so that PHI
1083// reuse code doesn't see an incomplete PHI.
1084constSCEV *Step = Normalized->getStepRecurrence(SE);
1085Type *ExpandTy = Normalized->getType();
1086// If the stride is negative, insert a sub instead of an add for the increment
1087// (unless it's a constant, because subtracts of constants are canonicalized
1088// to adds).
1089bool useSubtract = !ExpandTy->isPointerTy() && Step->isNonConstantNegative();
1090if (useSubtract)
1091 Step = SE.getNegativeSCEV(Step);
1092// Expand the step somewhere that dominates the loop header.
1093Value *StepV = expand(Step,L->getHeader()->getFirstInsertionPt());
1094
1095// The no-wrap behavior proved by IsIncrement(NUW|NSW) is only applicable if
1096// we actually do emit an addition. It does not apply if we emit a
1097// subtraction.
1098bool IncrementIsNUW = !useSubtract &&IsIncrementNUW(SE, Normalized);
1099bool IncrementIsNSW = !useSubtract &&IsIncrementNSW(SE, Normalized);
1100
1101// Create the PHI.
1102BasicBlock *Header =L->getHeader();
1103 Builder.SetInsertPoint(Header, Header->begin());
1104PHINode *PN =
1105 Builder.CreatePHI(ExpandTy,pred_size(Header),Twine(IVName) +".iv");
1106
1107// Create the step instructions and populate the PHI.
1108for (BasicBlock *Pred :predecessors(Header)) {
1109// Add a start value.
1110if (!L->contains(Pred)) {
1111 PN->addIncoming(StartV, Pred);
1112continue;
1113 }
1114
1115// Create a step value and add it to the PHI.
1116// If IVIncInsertLoop is non-null and equal to the addrec's loop, insert the
1117// instructions at IVIncInsertPos.
1118Instruction *InsertPos =L == IVIncInsertLoop ?
1119 IVIncInsertPos : Pred->getTerminator();
1120 Builder.SetInsertPoint(InsertPos);
1121Value *IncV = expandIVInc(PN, StepV, L, useSubtract);
1122
1123if (isa<OverflowingBinaryOperator>(IncV)) {
1124if (IncrementIsNUW)
1125 cast<BinaryOperator>(IncV)->setHasNoUnsignedWrap();
1126if (IncrementIsNSW)
1127 cast<BinaryOperator>(IncV)->setHasNoSignedWrap();
1128 }
1129 PN->addIncoming(IncV, Pred);
1130 }
1131
1132// After expanding subexpressions, restore the PostIncLoops set so the caller
1133// can ensure that IVIncrement dominates the current uses.
1134 PostIncLoops = SavedPostIncLoops;
1135
1136// Remember this PHI, even in post-inc mode. LSR SCEV-based salvaging is most
1137// effective when we are able to use an IV inserted here, so record it.
1138 InsertedValues.insert(PN);
1139 InsertedIVs.push_back(PN);
1140return PN;
1141}
1142
1143Value *SCEVExpander::expandAddRecExprLiterally(constSCEVAddRecExpr *S) {
1144constLoop *L = S->getLoop();
1145
1146// Determine a normalized form of this expression, which is the expression
1147// before any post-inc adjustment is made.
1148constSCEVAddRecExpr *Normalized = S;
1149if (PostIncLoops.count(L)) {
1150PostIncLoopSetLoops;
1151Loops.insert(L);
1152 Normalized = cast<SCEVAddRecExpr>(
1153normalizeForPostIncUse(S,Loops, SE,/*CheckInvertible=*/false));
1154 }
1155
1156 [[maybe_unused]]constSCEV *Start = Normalized->getStart();
1157constSCEV *Step = Normalized->getStepRecurrence(SE);
1158assert(SE.properlyDominates(Start,L->getHeader()) &&
1159"Start does not properly dominate loop header");
1160assert(SE.dominates(Step,L->getHeader()) &&"Step not dominate loop header");
1161
1162// In some cases, we decide to reuse an existing phi node but need to truncate
1163// it and/or invert the step.
1164Type *TruncTy =nullptr;
1165bool InvertStep =false;
1166PHINode *PN = getAddRecExprPHILiterally(Normalized, L, TruncTy, InvertStep);
1167
1168// Accommodate post-inc mode, if necessary.
1169Value *Result;
1170if (!PostIncLoops.count(L))
1171Result = PN;
1172else {
1173// In PostInc mode, use the post-incremented value.
1174BasicBlock *LatchBlock =L->getLoopLatch();
1175assert(LatchBlock &&"PostInc mode requires a unique loop latch!");
1176Result = PN->getIncomingValueForBlock(LatchBlock);
1177
1178// We might be introducing a new use of the post-inc IV that is not poison
1179// safe, in which case we should drop poison generating flags. Only keep
1180// those flags for which SCEV has proven that they always hold.
1181if (isa<OverflowingBinaryOperator>(Result)) {
1182auto *I = cast<Instruction>(Result);
1183if (!S->hasNoUnsignedWrap())
1184I->setHasNoUnsignedWrap(false);
1185if (!S->hasNoSignedWrap())
1186I->setHasNoSignedWrap(false);
1187 }
1188
1189// For an expansion to use the postinc form, the client must call
1190// expandCodeFor with an InsertPoint that is either outside the PostIncLoop
1191// or dominated by IVIncInsertPos.
1192if (isa<Instruction>(Result) &&
1193 !SE.DT.dominates(cast<Instruction>(Result),
1194 &*Builder.GetInsertPoint())) {
1195// The induction variable's postinc expansion does not dominate this use.
1196// IVUsers tries to prevent this case, so it is rare. However, it can
1197// happen when an IVUser outside the loop is not dominated by the latch
1198// block. Adjusting IVIncInsertPos before expansion begins cannot handle
1199// all cases. Consider a phi outside whose operand is replaced during
1200// expansion with the value of the postinc user. Without fundamentally
1201// changing the way postinc users are tracked, the only remedy is
1202// inserting an extra IV increment. StepV might fold into PostLoopOffset,
1203// but hopefully expandCodeFor handles that.
1204bool useSubtract =
1205 !S->getType()->isPointerTy() && Step->isNonConstantNegative();
1206if (useSubtract)
1207 Step = SE.getNegativeSCEV(Step);
1208Value *StepV;
1209 {
1210// Expand the step somewhere that dominates the loop header.
1211 SCEVInsertPointGuard Guard(Builder,this);
1212 StepV = expand(Step,L->getHeader()->getFirstInsertionPt());
1213 }
1214Result = expandIVInc(PN, StepV, L, useSubtract);
1215 }
1216 }
1217
1218// We have decided to reuse an induction variable of a dominating loop. Apply
1219// truncation and/or inversion of the step.
1220if (TruncTy) {
1221// Truncate the result.
1222if (TruncTy !=Result->getType())
1223Result = Builder.CreateTrunc(Result, TruncTy);
1224
1225// Invert the result.
1226if (InvertStep)
1227Result = Builder.CreateSub(expand(Normalized->getStart()), Result);
1228 }
1229
1230returnResult;
1231}
1232
1233Value *SCEVExpander::visitAddRecExpr(constSCEVAddRecExpr *S) {
1234// In canonical mode we compute the addrec as an expression of a canonical IV
1235// using evaluateAtIteration and expand the resulting SCEV expression. This
1236// way we avoid introducing new IVs to carry on the computation of the addrec
1237// throughout the loop.
1238//
1239// For nested addrecs evaluateAtIteration might need a canonical IV of a
1240// type wider than the addrec itself. Emitting a canonical IV of the
1241// proper type might produce non-legal types, for example expanding an i64
1242// {0,+,2,+,1} addrec would need an i65 canonical IV. To avoid this just fall
1243// back to non-canonical mode for nested addrecs.
1244if (!CanonicalMode || (S->getNumOperands() > 2))
1245return expandAddRecExprLiterally(S);
1246
1247Type *Ty = SE.getEffectiveSCEVType(S->getType());
1248constLoop *L = S->getLoop();
1249
1250// First check for an existing canonical IV in a suitable type.
1251PHINode *CanonicalIV =nullptr;
1252if (PHINode *PN =L->getCanonicalInductionVariable())
1253if (SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty))
1254 CanonicalIV = PN;
1255
1256// Rewrite an AddRec in terms of the canonical induction variable, if
1257// its type is more narrow.
1258if (CanonicalIV &&
1259 SE.getTypeSizeInBits(CanonicalIV->getType()) > SE.getTypeSizeInBits(Ty) &&
1260 !S->getType()->isPointerTy()) {
1261SmallVector<const SCEV *, 4> NewOps(S->getNumOperands());
1262for (unsigned i = 0, e = S->getNumOperands(); i != e; ++i)
1263 NewOps[i] = SE.getAnyExtendExpr(S->getOperand(i), CanonicalIV->getType());
1264Value *V = expand(SE.getAddRecExpr(NewOps, S->getLoop(),
1265 S->getNoWrapFlags(SCEV::FlagNW)));
1266BasicBlock::iterator NewInsertPt =
1267findInsertPointAfter(cast<Instruction>(V), &*Builder.GetInsertPoint());
1268V = expand(SE.getTruncateExpr(SE.getUnknown(V), Ty), NewInsertPt);
1269returnV;
1270 }
1271
1272// {X,+,F} --> X + {0,+,F}
1273if (!S->getStart()->isZero()) {
1274if (isa<PointerType>(S->getType())) {
1275Value *StartV = expand(SE.getPointerBase(S));
1276return expandAddToGEP(SE.removePointerBase(S), StartV,
1277 S->getNoWrapFlags(SCEV::FlagNUW));
1278 }
1279
1280SmallVector<const SCEV *, 4> NewOps(S->operands());
1281 NewOps[0] = SE.getConstant(Ty, 0);
1282constSCEV *Rest = SE.getAddRecExpr(NewOps, L,
1283 S->getNoWrapFlags(SCEV::FlagNW));
1284
1285// Just do a normal add. Pre-expand the operands to suppress folding.
1286//
1287// The LHS and RHS values are factored out of the expand call to make the
1288// output independent of the argument evaluation order.
1289constSCEV *AddExprLHS = SE.getUnknown(expand(S->getStart()));
1290constSCEV *AddExprRHS = SE.getUnknown(expand(Rest));
1291return expand(SE.getAddExpr(AddExprLHS, AddExprRHS));
1292 }
1293
1294// If we don't yet have a canonical IV, create one.
1295if (!CanonicalIV) {
1296// Create and insert the PHI node for the induction variable in the
1297// specified loop.
1298BasicBlock *Header =L->getHeader();
1299pred_iterator HPB =pred_begin(Header), HPE =pred_end(Header);
1300 CanonicalIV =PHINode::Create(Ty, std::distance(HPB, HPE),"indvar");
1301 CanonicalIV->insertBefore(Header->begin());
1302 rememberInstruction(CanonicalIV);
1303
1304SmallSet<BasicBlock *, 4> PredSeen;
1305Constant *One = ConstantInt::get(Ty, 1);
1306for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) {
1307BasicBlock *HP = *HPI;
1308if (!PredSeen.insert(HP).second) {
1309// There must be an incoming value for each predecessor, even the
1310// duplicates!
1311 CanonicalIV->addIncoming(CanonicalIV->getIncomingValueForBlock(HP), HP);
1312continue;
1313 }
1314
1315if (L->contains(HP)) {
1316// Insert a unit add instruction right before the terminator
1317// corresponding to the back-edge.
1318Instruction *Add = BinaryOperator::CreateAdd(CanonicalIV, One,
1319"indvar.next",
1320 HP->getTerminator()->getIterator());
1321Add->setDebugLoc(HP->getTerminator()->getDebugLoc());
1322 rememberInstruction(Add);
1323 CanonicalIV->addIncoming(Add, HP);
1324 }else {
1325 CanonicalIV->addIncoming(Constant::getNullValue(Ty), HP);
1326 }
1327 }
1328 }
1329
1330// {0,+,1} --> Insert a canonical induction variable into the loop!
1331if (S->isAffine() && S->getOperand(1)->isOne()) {
1332assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) &&
1333"IVs with types different from the canonical IV should "
1334"already have been handled!");
1335return CanonicalIV;
1336 }
1337
1338// {0,+,F} --> {0,+,1} * F
1339
1340// If this is a simple linear addrec, emit it now as a special case.
1341if (S->isAffine())// {0,+,F} --> i*F
1342return
1343 expand(SE.getTruncateOrNoop(
1344 SE.getMulExpr(SE.getUnknown(CanonicalIV),
1345 SE.getNoopOrAnyExtend(S->getOperand(1),
1346 CanonicalIV->getType())),
1347 Ty));
1348
1349// If this is a chain of recurrences, turn it into a closed form, using the
1350// folders, then expandCodeFor the closed form. This allows the folders to
1351// simplify the expression without having to build a bunch of special code
1352// into this folder.
1353constSCEV *IH = SE.getUnknown(CanonicalIV);// Get I as a "symbolic" SCEV.
1354
1355// Promote S up to the canonical IV type, if the cast is foldable.
1356constSCEV *NewS = S;
1357constSCEV *Ext = SE.getNoopOrAnyExtend(S, CanonicalIV->getType());
1358if (isa<SCEVAddRecExpr>(Ext))
1359 NewS =Ext;
1360
1361constSCEV *V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE);
1362
1363// Truncate the result down to the original type, if needed.
1364constSCEV *T = SE.getTruncateOrNoop(V, Ty);
1365return expand(T);
1366}
1367
1368Value *SCEVExpander::visitPtrToIntExpr(constSCEVPtrToIntExpr *S) {
1369Value *V = expand(S->getOperand());
1370return ReuseOrCreateCast(V, S->getType(), CastInst::PtrToInt,
1371 GetOptimalInsertionPointForCastOf(V));
1372}
1373
1374Value *SCEVExpander::visitTruncateExpr(constSCEVTruncateExpr *S) {
1375Value *V = expand(S->getOperand());
1376return Builder.CreateTrunc(V, S->getType());
1377}
1378
1379Value *SCEVExpander::visitZeroExtendExpr(constSCEVZeroExtendExpr *S) {
1380Value *V = expand(S->getOperand());
1381return Builder.CreateZExt(V, S->getType(),"",
1382 SE.isKnownNonNegative(S->getOperand()));
1383}
1384
1385Value *SCEVExpander::visitSignExtendExpr(constSCEVSignExtendExpr *S) {
1386Value *V = expand(S->getOperand());
1387return Builder.CreateSExt(V, S->getType());
1388}
1389
1390Value *SCEVExpander::expandMinMaxExpr(constSCEVNAryExpr *S,
1391Intrinsic::ID IntrinID,TwineName,
1392bool IsSequential) {
1393bool PrevSafeMode = SafeUDivMode;
1394 SafeUDivMode |= IsSequential;
1395Value *LHS = expand(S->getOperand(S->getNumOperands() - 1));
1396Type *Ty =LHS->getType();
1397if (IsSequential)
1398LHS = Builder.CreateFreeze(LHS);
1399for (int i = S->getNumOperands() - 2; i >= 0; --i) {
1400 SafeUDivMode = (IsSequential && i != 0) || PrevSafeMode;
1401Value *RHS = expand(S->getOperand(i));
1402if (IsSequential && i != 0)
1403RHS = Builder.CreateFreeze(RHS);
1404Value *Sel;
1405if (Ty->isIntegerTy())
1406 Sel = Builder.CreateIntrinsic(IntrinID, {Ty}, {LHS,RHS},
1407/*FMFSource=*/nullptr,Name);
1408else {
1409Value *ICmp =
1410 Builder.CreateICmp(MinMaxIntrinsic::getPredicate(IntrinID), LHS, RHS);
1411 Sel = Builder.CreateSelect(ICmp, LHS, RHS,Name);
1412 }
1413LHS = Sel;
1414 }
1415 SafeUDivMode = PrevSafeMode;
1416returnLHS;
1417}
1418
1419Value *SCEVExpander::visitSMaxExpr(constSCEVSMaxExpr *S) {
1420return expandMinMaxExpr(S, Intrinsic::smax,"smax");
1421}
1422
1423Value *SCEVExpander::visitUMaxExpr(constSCEVUMaxExpr *S) {
1424return expandMinMaxExpr(S, Intrinsic::umax,"umax");
1425}
1426
1427Value *SCEVExpander::visitSMinExpr(constSCEVSMinExpr *S) {
1428return expandMinMaxExpr(S, Intrinsic::smin,"smin");
1429}
1430
1431Value *SCEVExpander::visitUMinExpr(constSCEVUMinExpr *S) {
1432return expandMinMaxExpr(S, Intrinsic::umin,"umin");
1433}
1434
1435Value *SCEVExpander::visitSequentialUMinExpr(constSCEVSequentialUMinExpr *S) {
1436return expandMinMaxExpr(S, Intrinsic::umin,"umin",/*IsSequential*/true);
1437}
1438
1439Value *SCEVExpander::visitVScale(constSCEVVScale *S) {
1440return Builder.CreateVScale(ConstantInt::get(S->getType(), 1));
1441}
1442
1443Value *SCEVExpander::expandCodeFor(constSCEV *SH,Type *Ty,
1444BasicBlock::iterator IP) {
1445setInsertPoint(IP);
1446Value *V =expandCodeFor(SH, Ty);
1447return V;
1448}
1449
1450Value *SCEVExpander::expandCodeFor(constSCEV *SH,Type *Ty) {
1451// Expand the code for this SCEV.
1452Value *V = expand(SH);
1453
1454if (Ty) {
1455assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) &&
1456"non-trivial casts should be done with the SCEVs directly!");
1457 V = InsertNoopCastOfTo(V, Ty);
1458 }
1459return V;
1460}
1461
1462Value *SCEVExpander::FindValueInExprValueMap(
1463constSCEV *S,constInstruction *InsertPt,
1464SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts) {
1465// If the expansion is not in CanonicalMode, and the SCEV contains any
1466// sub scAddRecExpr type SCEV, it is required to expand the SCEV literally.
1467if (!CanonicalMode && SE.containsAddRecurrence(S))
1468returnnullptr;
1469
1470// If S is a constant or unknown, it may be worse to reuse an existing Value.
1471if (isa<SCEVConstant>(S) || isa<SCEVUnknown>(S))
1472returnnullptr;
1473
1474for (Value *V : SE.getSCEVValues(S)) {
1475Instruction *EntInst = dyn_cast<Instruction>(V);
1476if (!EntInst)
1477continue;
1478
1479// Choose a Value from the set which dominates the InsertPt.
1480// InsertPt should be inside the Value's parent loop so as not to break
1481// the LCSSA form.
1482assert(EntInst->getFunction() == InsertPt->getFunction());
1483if (S->getType() != V->getType() || !SE.DT.dominates(EntInst, InsertPt) ||
1484 !(SE.LI.getLoopFor(EntInst->getParent()) ==nullptr ||
1485 SE.LI.getLoopFor(EntInst->getParent())->contains(InsertPt)))
1486continue;
1487
1488// Make sure reusing the instruction is poison-safe.
1489if (SE.canReuseInstruction(S, EntInst, DropPoisonGeneratingInsts))
1490return V;
1491 DropPoisonGeneratingInsts.clear();
1492 }
1493returnnullptr;
1494}
1495
1496// The expansion of SCEV will either reuse a previous Value in ExprValueMap,
1497// or expand the SCEV literally. Specifically, if the expansion is in LSRMode,
1498// and the SCEV contains any sub scAddRecExpr type SCEV, it will be expanded
1499// literally, to prevent LSR's transformed SCEV from being reverted. Otherwise,
1500// the expansion will try to reuse Value from ExprValueMap, and only when it
1501// fails, expand the SCEV literally.
1502Value *SCEVExpander::expand(constSCEV *S) {
1503// Compute an insertion point for this SCEV object. Hoist the instructions
1504// as far out in the loop nest as possible.
1505BasicBlock::iterator InsertPt = Builder.GetInsertPoint();
1506
1507// We can move insertion point only if there is no div or rem operations
1508// otherwise we are risky to move it over the check for zero denominator.
1509auto SafeToHoist = [](constSCEV *S) {
1510return !SCEVExprContains(S, [](constSCEV *S) {
1511if (constauto *D = dyn_cast<SCEVUDivExpr>(S)) {
1512if (constauto *SC = dyn_cast<SCEVConstant>(D->getRHS()))
1513// Division by non-zero constants can be hoisted.
1514returnSC->getValue()->isZero();
1515// All other divisions should not be moved as they may be
1516// divisions by zero and should be kept within the
1517// conditions of the surrounding loops that guard their
1518// execution (see PR35406).
1519returntrue;
1520 }
1521returnfalse;
1522 });
1523 };
1524if (SafeToHoist(S)) {
1525for (Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock());;
1526 L =L->getParentLoop()) {
1527if (SE.isLoopInvariant(S, L)) {
1528if (!L)break;
1529if (BasicBlock *Preheader =L->getLoopPreheader()) {
1530 InsertPt = Preheader->getTerminator()->getIterator();
1531 }else {
1532// LSR sets the insertion point for AddRec start/step values to the
1533// block start to simplify value reuse, even though it's an invalid
1534// position. SCEVExpander must correct for this in all cases.
1535 InsertPt =L->getHeader()->getFirstInsertionPt();
1536 }
1537 }else {
1538// If the SCEV is computable at this level, insert it into the header
1539// after the PHIs (and after any other instructions that we've inserted
1540// there) so that it is guaranteed to dominate any user inside the loop.
1541if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L))
1542 InsertPt =L->getHeader()->getFirstInsertionPt();
1543
1544while (InsertPt != Builder.GetInsertPoint() &&
1545 (isInsertedInstruction(&*InsertPt) ||
1546 isa<DbgInfoIntrinsic>(&*InsertPt))) {
1547 InsertPt = std::next(InsertPt);
1548 }
1549break;
1550 }
1551 }
1552 }
1553
1554// Check to see if we already expanded this here.
1555autoI = InsertedExpressions.find(std::make_pair(S, &*InsertPt));
1556if (I != InsertedExpressions.end())
1557returnI->second;
1558
1559 SCEVInsertPointGuard Guard(Builder,this);
1560 Builder.SetInsertPoint(InsertPt->getParent(), InsertPt);
1561
1562// Expand the expression into instructions.
1563SmallVector<Instruction *> DropPoisonGeneratingInsts;
1564Value *V = FindValueInExprValueMap(S, &*InsertPt, DropPoisonGeneratingInsts);
1565if (!V) {
1566V =visit(S);
1567V = fixupLCSSAFormFor(V);
1568 }else {
1569for (Instruction *I : DropPoisonGeneratingInsts) {
1570 rememberFlags(I);
1571I->dropPoisonGeneratingAnnotations();
1572// See if we can re-infer from first principles any of the flags we just
1573// dropped.
1574if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(I))
1575if (auto Flags = SE.getStrengthenedNoWrapFlagsFromBinOp(OBO)) {
1576auto *BO = cast<BinaryOperator>(I);
1577 BO->setHasNoUnsignedWrap(
1578ScalarEvolution::maskFlags(*Flags,SCEV::FlagNUW) ==SCEV::FlagNUW);
1579 BO->setHasNoSignedWrap(
1580ScalarEvolution::maskFlags(*Flags,SCEV::FlagNSW) ==SCEV::FlagNSW);
1581 }
1582if (auto *NNI = dyn_cast<PossiblyNonNegInst>(I)) {
1583auto *Src = NNI->getOperand(0);
1584if (isImpliedByDomCondition(ICmpInst::ICMP_SGE, Src,
1585Constant::getNullValue(Src->getType()),I,
1586 DL).value_or(false))
1587 NNI->setNonNeg(true);
1588 }
1589 }
1590 }
1591// Remember the expanded value for this SCEV at this location.
1592//
1593// This is independent of PostIncLoops. The mapped value simply materializes
1594// the expression at this insertion point. If the mapped value happened to be
1595// a postinc expansion, it could be reused by a non-postinc user, but only if
1596// its insertion point was already at the head of the loop.
1597 InsertedExpressions[std::make_pair(S, &*InsertPt)] =V;
1598returnV;
1599}
1600
1601void SCEVExpander::rememberInstruction(Value *I) {
1602auto DoInsert = [this](Value *V) {
1603if (!PostIncLoops.empty())
1604 InsertedPostIncValues.insert(V);
1605else
1606 InsertedValues.insert(V);
1607 };
1608 DoInsert(I);
1609}
1610
1611void SCEVExpander::rememberFlags(Instruction *I) {
1612// If we already have flags for the instruction, keep the existing ones.
1613 OrigFlags.try_emplace(I,PoisonFlags(I));
1614}
1615
1616void SCEVExpander::replaceCongruentIVInc(
1617PHINode *&Phi,PHINode *&OrigPhi,Loop *L,constDominatorTree *DT,
1618SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
1619BasicBlock *LatchBlock =L->getLoopLatch();
1620if (!LatchBlock)
1621return;
1622
1623Instruction *OrigInc =
1624 dyn_cast<Instruction>(OrigPhi->getIncomingValueForBlock(LatchBlock));
1625Instruction *IsomorphicInc =
1626 dyn_cast<Instruction>(Phi->getIncomingValueForBlock(LatchBlock));
1627if (!OrigInc || !IsomorphicInc)
1628return;
1629
1630// If this phi has the same width but is more canonical, replace the
1631// original with it. As part of the "more canonical" determination,
1632// respect a prior decision to use an IV chain.
1633if (OrigPhi->getType() ==Phi->getType() &&
1634 !(ChainedPhis.count(Phi) ||
1635 isExpandedAddRecExprPHI(OrigPhi, OrigInc, L)) &&
1636 (ChainedPhis.count(Phi) ||
1637 isExpandedAddRecExprPHI(Phi, IsomorphicInc, L))) {
1638std::swap(OrigPhi, Phi);
1639std::swap(OrigInc, IsomorphicInc);
1640 }
1641
1642// Replacing the congruent phi is sufficient because acyclic
1643// redundancy elimination, CSE/GVN, should handle the
1644// rest. However, once SCEV proves that a phi is congruent,
1645// it's often the head of an IV user cycle that is isomorphic
1646// with the original phi. It's worth eagerly cleaning up the
1647// common case of a single IV increment so that DeleteDeadPHIs
1648// can remove cycles that had postinc uses.
1649// Because we may potentially introduce a new use of OrigIV that didn't
1650// exist before at this point, its poison flags need readjustment.
1651constSCEV *TruncExpr =
1652 SE.getTruncateOrNoop(SE.getSCEV(OrigInc), IsomorphicInc->getType());
1653if (OrigInc == IsomorphicInc || TruncExpr != SE.getSCEV(IsomorphicInc) ||
1654 !SE.LI.replacementPreservesLCSSAForm(IsomorphicInc, OrigInc))
1655return;
1656
1657bool BothHaveNUW =false;
1658bool BothHaveNSW =false;
1659auto *OBOIncV = dyn_cast<OverflowingBinaryOperator>(OrigInc);
1660auto *OBOIsomorphic = dyn_cast<OverflowingBinaryOperator>(IsomorphicInc);
1661if (OBOIncV && OBOIsomorphic) {
1662 BothHaveNUW =
1663 OBOIncV->hasNoUnsignedWrap() && OBOIsomorphic->hasNoUnsignedWrap();
1664 BothHaveNSW =
1665 OBOIncV->hasNoSignedWrap() && OBOIsomorphic->hasNoSignedWrap();
1666 }
1667
1668if (!hoistIVInc(OrigInc, IsomorphicInc,
1669/*RecomputePoisonFlags*/true))
1670return;
1671
1672// We are replacing with a wider increment. If both OrigInc and IsomorphicInc
1673// are NUW/NSW, then we can preserve them on the wider increment; the narrower
1674// IsomorphicInc would wrap before the wider OrigInc, so the replacement won't
1675// make IsomorphicInc's uses more poisonous.
1676assert(OrigInc->getType()->getScalarSizeInBits() >=
1677 IsomorphicInc->getType()->getScalarSizeInBits() &&
1678"Should only replace an increment with a wider one.");
1679if (BothHaveNUW || BothHaveNSW) {
1680 OrigInc->setHasNoUnsignedWrap(OBOIncV->hasNoUnsignedWrap() || BothHaveNUW);
1681 OrigInc->setHasNoSignedWrap(OBOIncV->hasNoSignedWrap() || BothHaveNSW);
1682 }
1683
1684SCEV_DEBUG_WITH_TYPE(DebugType,
1685dbgs() <<"INDVARS: Eliminated congruent iv.inc: "
1686 << *IsomorphicInc <<'\n');
1687Value *NewInc = OrigInc;
1688if (OrigInc->getType() != IsomorphicInc->getType()) {
1689BasicBlock::iterator IP;
1690if (PHINode *PN = dyn_cast<PHINode>(OrigInc))
1691 IP = PN->getParent()->getFirstInsertionPt();
1692else
1693 IP = OrigInc->getNextNonDebugInstruction()->getIterator();
1694
1695IRBuilder<> Builder(IP->getParent(), IP);
1696 Builder.SetCurrentDebugLocation(IsomorphicInc->getDebugLoc());
1697 NewInc =
1698 Builder.CreateTruncOrBitCast(OrigInc, IsomorphicInc->getType(), IVName);
1699 }
1700 IsomorphicInc->replaceAllUsesWith(NewInc);
1701 DeadInsts.emplace_back(IsomorphicInc);
1702}
1703
1704/// replaceCongruentIVs - Check for congruent phis in this loop header and
1705/// replace them with their most canonical representative. Return the number of
1706/// phis eliminated.
1707///
1708/// This does not depend on any SCEVExpander state but should be used in
1709/// the same context that SCEVExpander is used.
1710unsigned
1711SCEVExpander::replaceCongruentIVs(Loop *L,constDominatorTree *DT,
1712SmallVectorImpl<WeakTrackingVH> &DeadInsts,
1713constTargetTransformInfo *TTI) {
1714// Find integer phis in order of increasing width.
1715SmallVector<PHINode*, 8> Phis;
1716for (PHINode &PN : L->getHeader()->phis())
1717 Phis.push_back(&PN);
1718
1719if (TTI)
1720// Use stable_sort to preserve order of equivalent PHIs, so the order
1721// of the sorted Phis is the same from run to run on the same loop.
1722llvm::stable_sort(Phis, [](Value *LHS,Value *RHS) {
1723// Put pointers at the back and make sure pointer < pointer = false.
1724if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
1725returnRHS->getType()->isIntegerTy() && !LHS->getType()->isIntegerTy();
1726returnRHS->getType()->getPrimitiveSizeInBits().getFixedValue() <
1727LHS->getType()->getPrimitiveSizeInBits().getFixedValue();
1728 });
1729
1730unsigned NumElim = 0;
1731DenseMap<const SCEV *, PHINode *> ExprToIVMap;
1732// Process phis from wide to narrow. Map wide phis to their truncation
1733// so narrow phis can reuse them.
1734for (PHINode *Phi : Phis) {
1735auto SimplifyPHINode = [&](PHINode *PN) ->Value * {
1736if (Value *V =simplifyInstruction(PN, {DL, &SE.TLI, &SE.DT, &SE.AC}))
1737return V;
1738if (!SE.isSCEVable(PN->getType()))
1739returnnullptr;
1740auto *Const = dyn_cast<SCEVConstant>(SE.getSCEV(PN));
1741if (!Const)
1742returnnullptr;
1743return Const->getValue();
1744 };
1745
1746// Fold constant phis. They may be congruent to other constant phis and
1747// would confuse the logic below that expects proper IVs.
1748if (Value *V = SimplifyPHINode(Phi)) {
1749if (V->getType() != Phi->getType())
1750continue;
1751 SE.forgetValue(Phi);
1752 Phi->replaceAllUsesWith(V);
1753 DeadInsts.emplace_back(Phi);
1754 ++NumElim;
1755SCEV_DEBUG_WITH_TYPE(DebugType,
1756dbgs() <<"INDVARS: Eliminated constant iv: " << *Phi
1757 <<'\n');
1758continue;
1759 }
1760
1761if (!SE.isSCEVable(Phi->getType()))
1762continue;
1763
1764PHINode *&OrigPhiRef = ExprToIVMap[SE.getSCEV(Phi)];
1765if (!OrigPhiRef) {
1766 OrigPhiRef = Phi;
1767if (Phi->getType()->isIntegerTy() &&TTI &&
1768TTI->isTruncateFree(Phi->getType(), Phis.back()->getType())) {
1769// Make sure we only rewrite using simple induction variables;
1770// otherwise, we can make the trip count of a loop unanalyzable
1771// to SCEV.
1772constSCEV *PhiExpr = SE.getSCEV(Phi);
1773if (isa<SCEVAddRecExpr>(PhiExpr)) {
1774// This phi can be freely truncated to the narrowest phi type. Map the
1775// truncated expression to it so it will be reused for narrow types.
1776constSCEV *TruncExpr =
1777 SE.getTruncateExpr(PhiExpr, Phis.back()->getType());
1778 ExprToIVMap[TruncExpr] = Phi;
1779 }
1780 }
1781continue;
1782 }
1783
1784// Replacing a pointer phi with an integer phi or vice-versa doesn't make
1785// sense.
1786if (OrigPhiRef->getType()->isPointerTy() != Phi->getType()->isPointerTy())
1787continue;
1788
1789 replaceCongruentIVInc(Phi, OrigPhiRef, L, DT, DeadInsts);
1790SCEV_DEBUG_WITH_TYPE(DebugType,
1791dbgs() <<"INDVARS: Eliminated congruent iv: " << *Phi
1792 <<'\n');
1793SCEV_DEBUG_WITH_TYPE(
1794 DebugType,dbgs() <<"INDVARS: Original iv: " << *OrigPhiRef <<'\n');
1795 ++NumElim;
1796Value *NewIV = OrigPhiRef;
1797if (OrigPhiRef->getType() != Phi->getType()) {
1798IRBuilder<> Builder(L->getHeader(),
1799 L->getHeader()->getFirstInsertionPt());
1800 Builder.SetCurrentDebugLocation(Phi->getDebugLoc());
1801 NewIV = Builder.CreateTruncOrBitCast(OrigPhiRef, Phi->getType(), IVName);
1802 }
1803 Phi->replaceAllUsesWith(NewIV);
1804 DeadInsts.emplace_back(Phi);
1805 }
1806return NumElim;
1807}
1808
1809boolSCEVExpander::hasRelatedExistingExpansion(constSCEV *S,
1810constInstruction *At,
1811Loop *L) {
1812using namespacellvm::PatternMatch;
1813
1814SmallVector<BasicBlock *, 4> ExitingBlocks;
1815 L->getExitingBlocks(ExitingBlocks);
1816
1817// Look for suitable value in simple conditions at the loop exits.
1818for (BasicBlock *BB : ExitingBlocks) {
1819CmpPredicate Pred;
1820Instruction *LHS, *RHS;
1821
1822if (!match(BB->getTerminator(),
1823m_Br(m_ICmp(Pred,m_Instruction(LHS),m_Instruction(RHS)),
1824m_BasicBlock(),m_BasicBlock())))
1825continue;
1826
1827if (SE.getSCEV(LHS) == S && SE.DT.dominates(LHS, At))
1828returntrue;
1829
1830if (SE.getSCEV(RHS) == S && SE.DT.dominates(RHS, At))
1831returntrue;
1832 }
1833
1834// Use expand's logic which is used for reusing a previous Value in
1835// ExprValueMap. Note that we don't currently model the cost of
1836// needing to drop poison generating flags on the instruction if we
1837// want to reuse it. We effectively assume that has zero cost.
1838SmallVector<Instruction *> DropPoisonGeneratingInsts;
1839return FindValueInExprValueMap(S, At, DropPoisonGeneratingInsts) !=nullptr;
1840}
1841
1842template<typename T>staticInstructionCostcostAndCollectOperands(
1843constSCEVOperand &WorkItem,constTargetTransformInfo &TTI,
1844TargetTransformInfo::TargetCostKindCostKind,
1845SmallVectorImpl<SCEVOperand> &Worklist) {
1846
1847constT *S = cast<T>(WorkItem.S);
1848InstructionCostCost = 0;
1849// Object to help map SCEV operands to expanded IR instructions.
1850structOperationIndices {
1851 OperationIndices(unsigned Opc,size_t min,size_tmax) :
1852 Opcode(Opc), MinIdx(min), MaxIdx(max) { }
1853unsigned Opcode;
1854size_t MinIdx;
1855size_t MaxIdx;
1856 };
1857
1858// Collect the operations of all the instructions that will be needed to
1859// expand the SCEVExpr. This is so that when we come to cost the operands,
1860// we know what the generated user(s) will be.
1861SmallVector<OperationIndices, 2> Operations;
1862
1863auto CastCost = [&](unsigned Opcode) ->InstructionCost {
1864 Operations.emplace_back(Opcode, 0, 0);
1865returnTTI.getCastInstrCost(Opcode, S->getType(),
1866 S->getOperand(0)->getType(),
1867TTI::CastContextHint::None,CostKind);
1868 };
1869
1870auto ArithCost = [&](unsigned Opcode,unsigned NumRequired,
1871unsigned MinIdx = 0,
1872unsigned MaxIdx = 1) ->InstructionCost {
1873 Operations.emplace_back(Opcode, MinIdx, MaxIdx);
1874return NumRequired *
1875TTI.getArithmeticInstrCost(Opcode, S->getType(),CostKind);
1876 };
1877
1878auto CmpSelCost = [&](unsigned Opcode,unsigned NumRequired,unsigned MinIdx,
1879unsigned MaxIdx) ->InstructionCost {
1880 Operations.emplace_back(Opcode, MinIdx, MaxIdx);
1881Type *OpType = S->getType();
1882return NumRequired *TTI.getCmpSelInstrCost(
1883 Opcode, OpType,CmpInst::makeCmpResultType(OpType),
1884CmpInst::BAD_ICMP_PREDICATE,CostKind);
1885 };
1886
1887switch (S->getSCEVType()) {
1888casescCouldNotCompute:
1889llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
1890casescUnknown:
1891casescConstant:
1892casescVScale:
1893return 0;
1894casescPtrToInt:
1895Cost = CastCost(Instruction::PtrToInt);
1896break;
1897casescTruncate:
1898Cost = CastCost(Instruction::Trunc);
1899break;
1900casescZeroExtend:
1901Cost = CastCost(Instruction::ZExt);
1902break;
1903casescSignExtend:
1904Cost = CastCost(Instruction::SExt);
1905break;
1906casescUDivExpr: {
1907unsigned Opcode = Instruction::UDiv;
1908if (auto *SC = dyn_cast<SCEVConstant>(S->getOperand(1)))
1909if (SC->getAPInt().isPowerOf2())
1910 Opcode = Instruction::LShr;
1911Cost = ArithCost(Opcode, 1);
1912break;
1913 }
1914casescAddExpr:
1915Cost = ArithCost(Instruction::Add, S->getNumOperands() - 1);
1916break;
1917casescMulExpr:
1918// TODO: this is a very pessimistic cost modelling for Mul,
1919// because of Bin Pow algorithm actually used by the expander,
1920// see SCEVExpander::visitMulExpr(), ExpandOpBinPowN().
1921Cost = ArithCost(Instruction::Mul, S->getNumOperands() - 1);
1922break;
1923casescSMaxExpr:
1924casescUMaxExpr:
1925casescSMinExpr:
1926casescUMinExpr:
1927casescSequentialUMinExpr: {
1928// FIXME: should this ask the cost for Intrinsic's?
1929// The reduction tree.
1930Cost += CmpSelCost(Instruction::ICmp, S->getNumOperands() - 1, 0, 1);
1931Cost += CmpSelCost(Instruction::Select, S->getNumOperands() - 1, 0, 2);
1932switch (S->getSCEVType()) {
1933casescSequentialUMinExpr: {
1934// The safety net against poison.
1935// FIXME: this is broken.
1936Cost += CmpSelCost(Instruction::ICmp, S->getNumOperands() - 1, 0, 0);
1937Cost += ArithCost(Instruction::Or,
1938 S->getNumOperands() > 2 ? S->getNumOperands() - 2 : 0);
1939Cost += CmpSelCost(Instruction::Select, 1, 0, 1);
1940break;
1941 }
1942default:
1943assert(!isa<SCEVSequentialMinMaxExpr>(S) &&
1944"Unhandled SCEV expression type?");
1945break;
1946 }
1947break;
1948 }
1949casescAddRecExpr: {
1950// Addrec expands to a phi and add per recurrence.
1951unsigned NumRecurrences = S->getNumOperands() - 1;
1952Cost +=TTI.getCFInstrCost(Instruction::PHI,CostKind) * NumRecurrences;
1953Cost +=
1954TTI.getArithmeticInstrCost(Instruction::Add, S->getType(),CostKind) *
1955 NumRecurrences;
1956// AR start is used in phi.
1957 Worklist.emplace_back(Instruction::PHI, 0, S->getOperand(0));
1958// Other operands are used in add.
1959for (constSCEV *Op : S->operands().drop_front())
1960 Worklist.emplace_back(Instruction::Add, 1,Op);
1961break;
1962 }
1963 }
1964
1965for (auto &CostOp : Operations) {
1966for (auto SCEVOp :enumerate(S->operands())) {
1967// Clamp the index to account for multiple IR operations being chained.
1968size_t MinIdx = std::max(SCEVOp.index(), CostOp.MinIdx);
1969size_t OpIdx = std::min(MinIdx, CostOp.MaxIdx);
1970 Worklist.emplace_back(CostOp.Opcode, OpIdx, SCEVOp.value());
1971 }
1972 }
1973returnCost;
1974}
1975
1976bool SCEVExpander::isHighCostExpansionHelper(
1977constSCEVOperand &WorkItem,Loop *L,constInstruction &At,
1978InstructionCost &Cost,unsigned Budget,constTargetTransformInfo &TTI,
1979SmallPtrSetImpl<const SCEV *> &Processed,
1980SmallVectorImpl<SCEVOperand> &Worklist) {
1981if (Cost > Budget)
1982returntrue;// Already run out of budget, give up.
1983
1984constSCEV *S =WorkItem.S;
1985// Was the cost of expansion of this expression already accounted for?
1986if (!isa<SCEVConstant>(S) && !Processed.insert(S).second)
1987returnfalse;// We have already accounted for this expression.
1988
1989// If we can find an existing value for this scev available at the point "At"
1990// then consider the expression cheap.
1991if (hasRelatedExistingExpansion(S, &At, L))
1992returnfalse;// Consider the expression to be free.
1993
1994TargetTransformInfo::TargetCostKindCostKind =
1995L->getHeader()->getParent()->hasMinSize()
1996 ?TargetTransformInfo::TCK_CodeSize
1997 :TargetTransformInfo::TCK_RecipThroughput;
1998
1999switch (S->getSCEVType()) {
2000casescCouldNotCompute:
2001llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
2002casescUnknown:
2003casescVScale:
2004// Assume to be zero-cost.
2005returnfalse;
2006casescConstant: {
2007// Only evalulate the costs of constants when optimizing for size.
2008if (CostKind !=TargetTransformInfo::TCK_CodeSize)
2009returnfalse;
2010constAPInt &Imm = cast<SCEVConstant>(S)->getAPInt();
2011Type *Ty = S->getType();
2012Cost +=TTI.getIntImmCostInst(
2013WorkItem.ParentOpcode,WorkItem.OperandIdx, Imm, Ty,CostKind);
2014returnCost > Budget;
2015 }
2016casescTruncate:
2017casescPtrToInt:
2018casescZeroExtend:
2019casescSignExtend: {
2020Cost +=
2021 costAndCollectOperands<SCEVCastExpr>(WorkItem,TTI,CostKind, Worklist);
2022returnfalse;// Will answer upon next entry into this function.
2023 }
2024casescUDivExpr: {
2025// UDivExpr is very likely a UDiv that ScalarEvolution's HowFarToZero or
2026// HowManyLessThans produced to compute a precise expression, rather than a
2027// UDiv from the user's code. If we can't find a UDiv in the code with some
2028// simple searching, we need to account for it's cost.
2029
2030// At the beginning of this function we already tried to find existing
2031// value for plain 'S'. Now try to lookup 'S + 1' since it is common
2032// pattern involving division. This is just a simple search heuristic.
2033if (hasRelatedExistingExpansion(
2034 SE.getAddExpr(S, SE.getConstant(S->getType(), 1)), &At, L))
2035returnfalse;// Consider it to be free.
2036
2037Cost +=
2038 costAndCollectOperands<SCEVUDivExpr>(WorkItem,TTI,CostKind, Worklist);
2039returnfalse;// Will answer upon next entry into this function.
2040 }
2041casescAddExpr:
2042casescMulExpr:
2043casescUMaxExpr:
2044casescSMaxExpr:
2045casescUMinExpr:
2046casescSMinExpr:
2047casescSequentialUMinExpr: {
2048assert(cast<SCEVNAryExpr>(S)->getNumOperands() > 1 &&
2049"Nary expr should have more than 1 operand.");
2050// The simple nary expr will require one less op (or pair of ops)
2051// than the number of it's terms.
2052Cost +=
2053 costAndCollectOperands<SCEVNAryExpr>(WorkItem,TTI,CostKind, Worklist);
2054returnCost > Budget;
2055 }
2056casescAddRecExpr: {
2057assert(cast<SCEVAddRecExpr>(S)->getNumOperands() >= 2 &&
2058"Polynomial should be at least linear");
2059Cost += costAndCollectOperands<SCEVAddRecExpr>(
2060WorkItem,TTI,CostKind, Worklist);
2061returnCost > Budget;
2062 }
2063 }
2064llvm_unreachable("Unknown SCEV kind!");
2065}
2066
2067Value *SCEVExpander::expandCodeForPredicate(constSCEVPredicate *Pred,
2068Instruction *IP) {
2069assert(IP);
2070switch (Pred->getKind()) {
2071caseSCEVPredicate::P_Union:
2072returnexpandUnionPredicate(cast<SCEVUnionPredicate>(Pred), IP);
2073caseSCEVPredicate::P_Compare:
2074returnexpandComparePredicate(cast<SCEVComparePredicate>(Pred), IP);
2075caseSCEVPredicate::P_Wrap: {
2076auto *AddRecPred = cast<SCEVWrapPredicate>(Pred);
2077returnexpandWrapPredicate(AddRecPred, IP);
2078 }
2079 }
2080llvm_unreachable("Unknown SCEV predicate type");
2081}
2082
2083Value *SCEVExpander::expandComparePredicate(constSCEVComparePredicate *Pred,
2084Instruction *IP) {
2085Value *Expr0 = expand(Pred->getLHS(), IP);
2086Value *Expr1 = expand(Pred->getRHS(), IP);
2087
2088 Builder.SetInsertPoint(IP);
2089auto InvPred =ICmpInst::getInversePredicate(Pred->getPredicate());
2090auto *I = Builder.CreateICmp(InvPred, Expr0, Expr1,"ident.check");
2091returnI;
2092}
2093
2094Value *SCEVExpander::generateOverflowCheck(constSCEVAddRecExpr *AR,
2095Instruction *Loc,boolSigned) {
2096assert(AR->isAffine() &&"Cannot generate RT check for "
2097"non-affine expression");
2098
2099// FIXME: It is highly suspicious that we're ignoring the predicates here.
2100SmallVector<const SCEVPredicate *, 4> Pred;
2101constSCEV *ExitCount =
2102 SE.getPredicatedSymbolicMaxBackedgeTakenCount(AR->getLoop(), Pred);
2103
2104assert(!isa<SCEVCouldNotCompute>(ExitCount) &&"Invalid loop count");
2105
2106constSCEV *Step = AR->getStepRecurrence(SE);
2107constSCEV *Start = AR->getStart();
2108
2109Type *ARTy = AR->getType();
2110unsigned SrcBits = SE.getTypeSizeInBits(ExitCount->getType());
2111unsigned DstBits = SE.getTypeSizeInBits(ARTy);
2112
2113// The expression {Start,+,Step} has nusw/nssw if
2114// Step < 0, Start - |Step| * Backedge <= Start
2115// Step >= 0, Start + |Step| * Backedge > Start
2116// and |Step| * Backedge doesn't unsigned overflow.
2117
2118 Builder.SetInsertPoint(Loc);
2119Value *TripCountVal = expand(ExitCount, Loc);
2120
2121IntegerType *Ty =
2122IntegerType::get(Loc->getContext(), SE.getTypeSizeInBits(ARTy));
2123
2124Value *StepValue = expand(Step, Loc);
2125Value *NegStepValue = expand(SE.getNegativeSCEV(Step), Loc);
2126Value *StartValue = expand(Start, Loc);
2127
2128ConstantInt *Zero =
2129 ConstantInt::get(Loc->getContext(),APInt::getZero(DstBits));
2130
2131 Builder.SetInsertPoint(Loc);
2132// Compute |Step|
2133Value *StepCompare = Builder.CreateICmp(ICmpInst::ICMP_SLT, StepValue, Zero);
2134Value *AbsStep = Builder.CreateSelect(StepCompare, NegStepValue, StepValue);
2135
2136// Compute |Step| * Backedge
2137// Compute:
2138// 1. Start + |Step| * Backedge < Start
2139// 2. Start - |Step| * Backedge > Start
2140//
2141// And select either 1. or 2. depending on whether step is positive or
2142// negative. If Step is known to be positive or negative, only create
2143// either 1. or 2.
2144auto ComputeEndCheck = [&]() ->Value * {
2145// Checking <u 0 is always false.
2146if (!Signed && Start->isZero() && SE.isKnownPositive(Step))
2147returnConstantInt::getFalse(Loc->getContext());
2148
2149// Get the backedge taken count and truncate or extended to the AR type.
2150Value *TruncTripCount = Builder.CreateZExtOrTrunc(TripCountVal, Ty);
2151
2152Value *MulV, *OfMul;
2153if (Step->isOne()) {
2154// Special-case Step of one. Potentially-costly `umul_with_overflow` isn't
2155// needed, there is never an overflow, so to avoid artificially inflating
2156// the cost of the check, directly emit the optimized IR.
2157 MulV = TruncTripCount;
2158 OfMul =ConstantInt::getFalse(MulV->getContext());
2159 }else {
2160CallInst *Mul = Builder.CreateIntrinsic(Intrinsic::umul_with_overflow, Ty,
2161 {AbsStep, TruncTripCount},
2162/*FMFSource=*/nullptr,"mul");
2163 MulV = Builder.CreateExtractValue(Mul, 0,"mul.result");
2164 OfMul = Builder.CreateExtractValue(Mul, 1,"mul.overflow");
2165 }
2166
2167Value *Add =nullptr, *Sub =nullptr;
2168bool NeedPosCheck = !SE.isKnownNegative(Step);
2169bool NeedNegCheck = !SE.isKnownPositive(Step);
2170
2171if (isa<PointerType>(ARTy)) {
2172Value *NegMulV = Builder.CreateNeg(MulV);
2173if (NeedPosCheck)
2174Add = Builder.CreatePtrAdd(StartValue, MulV);
2175if (NeedNegCheck)
2176 Sub = Builder.CreatePtrAdd(StartValue, NegMulV);
2177 }else {
2178if (NeedPosCheck)
2179Add = Builder.CreateAdd(StartValue, MulV);
2180if (NeedNegCheck)
2181 Sub = Builder.CreateSub(StartValue, MulV);
2182 }
2183
2184Value *EndCompareLT =nullptr;
2185Value *EndCompareGT =nullptr;
2186Value *EndCheck =nullptr;
2187if (NeedPosCheck)
2188 EndCheck = EndCompareLT = Builder.CreateICmp(
2189Signed ?ICmpInst::ICMP_SLT :ICmpInst::ICMP_ULT,Add, StartValue);
2190if (NeedNegCheck)
2191 EndCheck = EndCompareGT = Builder.CreateICmp(
2192Signed ?ICmpInst::ICMP_SGT :ICmpInst::ICMP_UGT, Sub, StartValue);
2193if (NeedPosCheck && NeedNegCheck) {
2194// Select the answer based on the sign of Step.
2195 EndCheck = Builder.CreateSelect(StepCompare, EndCompareGT, EndCompareLT);
2196 }
2197return Builder.CreateOr(EndCheck, OfMul);
2198 };
2199Value *EndCheck = ComputeEndCheck();
2200
2201// If the backedge taken count type is larger than the AR type,
2202// check that we don't drop any bits by truncating it. If we are
2203// dropping bits, then we have overflow (unless the step is zero).
2204if (SrcBits > DstBits) {
2205auto MaxVal =APInt::getMaxValue(DstBits).zext(SrcBits);
2206auto *BackedgeCheck =
2207 Builder.CreateICmp(ICmpInst::ICMP_UGT, TripCountVal,
2208 ConstantInt::get(Loc->getContext(), MaxVal));
2209 BackedgeCheck = Builder.CreateAnd(
2210 BackedgeCheck, Builder.CreateICmp(ICmpInst::ICMP_NE, StepValue, Zero));
2211
2212 EndCheck = Builder.CreateOr(EndCheck, BackedgeCheck);
2213 }
2214
2215return EndCheck;
2216}
2217
2218Value *SCEVExpander::expandWrapPredicate(constSCEVWrapPredicate *Pred,
2219Instruction *IP) {
2220constauto *A = cast<SCEVAddRecExpr>(Pred->getExpr());
2221Value *NSSWCheck =nullptr, *NUSWCheck =nullptr;
2222
2223// Add a check for NUSW
2224if (Pred->getFlags() &SCEVWrapPredicate::IncrementNUSW)
2225 NUSWCheck =generateOverflowCheck(A, IP,false);
2226
2227// Add a check for NSSW
2228if (Pred->getFlags() &SCEVWrapPredicate::IncrementNSSW)
2229 NSSWCheck =generateOverflowCheck(A, IP,true);
2230
2231if (NUSWCheck && NSSWCheck)
2232return Builder.CreateOr(NUSWCheck, NSSWCheck);
2233
2234if (NUSWCheck)
2235return NUSWCheck;
2236
2237if (NSSWCheck)
2238return NSSWCheck;
2239
2240returnConstantInt::getFalse(IP->getContext());
2241}
2242
2243Value *SCEVExpander::expandUnionPredicate(constSCEVUnionPredicate *Union,
2244Instruction *IP) {
2245// Loop over all checks in this set.
2246SmallVector<Value *> Checks;
2247for (constauto *Pred : Union->getPredicates()) {
2248 Checks.push_back(expandCodeForPredicate(Pred, IP));
2249 Builder.SetInsertPoint(IP);
2250 }
2251
2252if (Checks.empty())
2253returnConstantInt::getFalse(IP->getContext());
2254return Builder.CreateOr(Checks);
2255}
2256
2257Value *SCEVExpander::fixupLCSSAFormFor(Value *V) {
2258auto *DefI = dyn_cast<Instruction>(V);
2259if (!PreserveLCSSA || !DefI)
2260return V;
2261
2262BasicBlock::iterator InsertPt = Builder.GetInsertPoint();
2263Loop *DefLoop = SE.LI.getLoopFor(DefI->getParent());
2264Loop *UseLoop = SE.LI.getLoopFor(InsertPt->getParent());
2265if (!DefLoop || UseLoop == DefLoop || DefLoop->contains(UseLoop))
2266return V;
2267
2268// Create a temporary instruction to at the current insertion point, so we
2269// can hand it off to the helper to create LCSSA PHIs if required for the
2270// new use.
2271// FIXME: Ideally formLCSSAForInstructions (used in fixupLCSSAFormFor)
2272// would accept a insertion point and return an LCSSA phi for that
2273// insertion point, so there is no need to insert & remove the temporary
2274// instruction.
2275Type *ToTy;
2276if (DefI->getType()->isIntegerTy())
2277 ToTy =PointerType::get(DefI->getContext(), 0);
2278else
2279 ToTy =Type::getInt32Ty(DefI->getContext());
2280Instruction *User =
2281CastInst::CreateBitOrPointerCast(DefI, ToTy,"tmp.lcssa.user", InsertPt);
2282auto RemoveUserOnExit =
2283make_scope_exit([User]() {User->eraseFromParent(); });
2284
2285SmallVector<Instruction *, 1> ToUpdate;
2286 ToUpdate.push_back(DefI);
2287SmallVector<PHINode *, 16> PHIsToRemove;
2288SmallVector<PHINode *, 16> InsertedPHIs;
2289formLCSSAForInstructions(ToUpdate, SE.DT, SE.LI, &SE, &PHIsToRemove,
2290 &InsertedPHIs);
2291for (PHINode *PN : InsertedPHIs)
2292 rememberInstruction(PN);
2293for (PHINode *PN : PHIsToRemove) {
2294if (!PN->use_empty())
2295continue;
2296 InsertedValues.erase(PN);
2297 InsertedPostIncValues.erase(PN);
2298 PN->eraseFromParent();
2299 }
2300
2301returnUser->getOperand(0);
2302}
2303
2304namespace{
2305// Search for a SCEV subexpression that is not safe to expand. Any expression
2306// that may expand to a !isSafeToSpeculativelyExecute value is unsafe, namely
2307// UDiv expressions. We don't know if the UDiv is derived from an IR divide
2308// instruction, but the important thing is that we prove the denominator is
2309// nonzero before expansion.
2310//
2311// IVUsers already checks that IV-derived expressions are safe. So this check is
2312// only needed when the expression includes some subexpression that is not IV
2313// derived.
2314//
2315// Currently, we only allow division by a value provably non-zero here.
2316//
2317// We cannot generally expand recurrences unless the step dominates the loop
2318// header. The expander handles the special case of affine recurrences by
2319// scaling the recurrence outside the loop, but this technique isn't generally
2320// applicable. Expanding a nested recurrence outside a loop requires computing
2321// binomial coefficients. This could be done, but the recurrence has to be in a
2322// perfectly reduced form, which can't be guaranteed.
2323structSCEVFindUnsafe {
2324ScalarEvolution &SE;
2325bool CanonicalMode;
2326bool IsUnsafe =false;
2327
2328 SCEVFindUnsafe(ScalarEvolution &SE,bool CanonicalMode)
2329 : SE(SE), CanonicalMode(CanonicalMode) {}
2330
2331bool follow(constSCEV *S) {
2332if (constSCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) {
2333if (!SE.isKnownNonZero(D->getRHS())) {
2334 IsUnsafe =true;
2335returnfalse;
2336 }
2337 }
2338if (constSCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
2339// For non-affine addrecs or in non-canonical mode we need a preheader
2340// to insert into.
2341if (!AR->getLoop()->getLoopPreheader() &&
2342 (!CanonicalMode || !AR->isAffine())) {
2343 IsUnsafe =true;
2344returnfalse;
2345 }
2346 }
2347returntrue;
2348 }
2349bool isDone() const{return IsUnsafe; }
2350};
2351}// namespace
2352
2353boolSCEVExpander::isSafeToExpand(constSCEV *S) const{
2354 SCEVFindUnsafe Search(SE, CanonicalMode);
2355visitAll(S, Search);
2356return !Search.IsUnsafe;
2357}
2358
2359boolSCEVExpander::isSafeToExpandAt(constSCEV *S,
2360constInstruction *InsertionPoint) const{
2361if (!isSafeToExpand(S))
2362returnfalse;
2363// We have to prove that the expanded site of S dominates InsertionPoint.
2364// This is easy when not in the same block, but hard when S is an instruction
2365// to be expanded somewhere inside the same block as our insertion point.
2366// What we really need here is something analogous to an OrderedBasicBlock,
2367// but for the moment, we paper over the problem by handling two common and
2368// cheap to check cases.
2369if (SE.properlyDominates(S,InsertionPoint->getParent()))
2370returntrue;
2371if (SE.dominates(S,InsertionPoint->getParent())) {
2372if (InsertionPoint->getParent()->getTerminator() ==InsertionPoint)
2373returntrue;
2374if (constSCEVUnknown *U = dyn_cast<SCEVUnknown>(S))
2375if (llvm::is_contained(InsertionPoint->operand_values(), U->getValue()))
2376returntrue;
2377 }
2378returnfalse;
2379}
2380
2381voidSCEVExpanderCleaner::cleanup() {
2382// Result is used, nothing to remove.
2383if (ResultUsed)
2384return;
2385
2386// Restore original poison flags.
2387for (auto [I, Flags] : Expander.OrigFlags)
2388 Flags.apply(I);
2389
2390auto InsertedInstructions = Expander.getAllInsertedInstructions();
2391#ifndef NDEBUG
2392SmallPtrSet<Instruction *, 8> InsertedSet(InsertedInstructions.begin(),
2393 InsertedInstructions.end());
2394 (void)InsertedSet;
2395#endif
2396// Remove sets with value handles.
2397 Expander.clear();
2398
2399// Remove all inserted instructions.
2400for (Instruction *I :reverse(InsertedInstructions)) {
2401#ifndef NDEBUG
2402assert(all_of(I->users(),
2403 [&InsertedSet](Value *U) {
2404 return InsertedSet.contains(cast<Instruction>(U));
2405 }) &&
2406"removed instruction should only be used by instructions inserted "
2407"during expansion");
2408#endif
2409assert(!I->getType()->isVoidTy() &&
2410"inserted instruction should have non-void types");
2411I->replaceAllUsesWith(PoisonValue::get(I->getType()));
2412I->eraseFromParent();
2413 }
2414}
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
CommandLine.h
CostKind
static cl::opt< TargetTransformInfo::TargetCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(TargetTransformInfo::TCK_RecipThroughput), cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(TargetTransformInfo::TCK_Latency, "latency", "Instruction latency"), clEnumValN(TargetTransformInfo::TCK_CodeSize, "code-size", "Code size"), clEnumValN(TargetTransformInfo::TCK_SizeAndLatency, "size-latency", "Code size and latency")))
DataLayout.h
Idx
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Definition:DeadArgumentElimination.cpp:353
Dominators.h
Name
std::string Name
Definition:ELFObjHandler.cpp:77
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
GEP
Hexagon Common GEP
Definition:HexagonCommonGEP.cpp:170
Loops
Hexagon Hardware Loops
Definition:HexagonHardwareLoops.cpp:373
IntrinsicInst.h
InstructionSimplify.h
LoopInfo.h
LoopUtils.h
I
#define I(x, y, z)
Definition:MD5.cpp:58
Signed
@ Signed
Definition:NVPTXISelLowering.cpp:4789
II
uint64_t IntrinsicInst * II
Definition:NVVMIntrRange.cpp:51
P
#define P(N)
PatternMatch.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
STLExtras.h
This file contains some templates that are useful if you are working with the STL at all.
IsIncrementNUW
static bool IsIncrementNUW(ScalarEvolution &SE, const SCEVAddRecExpr *AR)
Definition:ScalarEvolutionExpander.cpp:947
PickMostRelevantLoop
static const Loop * PickMostRelevantLoop(const Loop *A, const Loop *B, DominatorTree &DT)
PickMostRelevantLoop - Given two loops pick the one that's most relevant for SCEV expansion.
Definition:ScalarEvolutionExpander.cpp:419
costAndCollectOperands
static InstructionCost costAndCollectOperands(const SCEVOperand &WorkItem, const TargetTransformInfo &TTI, TargetTransformInfo::TargetCostKind CostKind, SmallVectorImpl< SCEVOperand > &Worklist)
Definition:ScalarEvolutionExpander.cpp:1842
IsIncrementNSW
static bool IsIncrementNSW(ScalarEvolution &SE, const SCEVAddRecExpr *AR)
Definition:ScalarEvolutionExpander.cpp:933
canBeCheaplyTransformed
static bool canBeCheaplyTransformed(ScalarEvolution &SE, const SCEVAddRecExpr *Phi, const SCEVAddRecExpr *Requested, bool &InvertStep)
Check whether we can cheaply express the requested SCEV in terms of the available PHI SCEV by truncat...
Definition:ScalarEvolutionExpander.cpp:900
SCEV_DEBUG_WITH_TYPE
#define SCEV_DEBUG_WITH_TYPE(TYPE, X)
Definition:ScalarEvolutionExpander.cpp:34
ScalarEvolutionExpander.h
ScopeExit.h
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
SmallSet.h
This file defines the SmallSet class.
TargetTransformInfo.h
This pass exposes codegen information to IR-level passes.
ValueTracking.h
RHS
Value * RHS
Definition:X86PartialReduction.cpp:74
LHS
Value * LHS
Definition:X86PartialReduction.cpp:73
T
llvm::APInt
Class for arbitrary precision integers.
Definition:APInt.h:78
llvm::APInt::zext
APInt zext(unsigned width) const
Zero extend to a new width.
Definition:APInt.cpp:986
llvm::APInt::getMaxValue
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition:APInt.h:206
llvm::APInt::getZero
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition:APInt.h:200
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition:Argument.h:31
llvm::BasicBlock
LLVM Basic Block Representation.
Definition:BasicBlock.h:61
llvm::BasicBlock::begin
iterator begin()
Instruction iterator methods.
Definition:BasicBlock.h:451
llvm::BasicBlock::getFirstInsertionPt
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition:BasicBlock.cpp:426
llvm::BasicBlock::getParent
const Function * getParent() const
Return the enclosing method, or null if none.
Definition:BasicBlock.h:220
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition:BasicBlock.h:177
llvm::BasicBlock::getTerminator
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition:BasicBlock.h:240
llvm::BinaryOperator::Create
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Definition:Instructions.cpp:2639
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition:Instructions.h:1479
llvm::CastInst
This is the base class for all instructions that perform data casts.
Definition:InstrTypes.h:444
llvm::CastInst::getCastOpcode
static Instruction::CastOps getCastOpcode(const Value *Val, bool SrcIsSigned, Type *Ty, bool DstIsSigned)
Returns the opcode necessary to cast Val into Ty using usual casting rules.
Definition:Instructions.cpp:3144
llvm::CastInst::getOpcode
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Definition:InstrTypes.h:608
llvm::CastInst::CreateBitOrPointerCast
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
Definition:Instructions.cpp:3047
llvm::CmpInst::makeCmpResultType
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition:InstrTypes.h:980
llvm::CmpInst::BAD_ICMP_PREDICATE
@ BAD_ICMP_PREDICATE
Definition:InstrTypes.h:706
llvm::CmpInst::ICMP_SLT
@ ICMP_SLT
signed less than
Definition:InstrTypes.h:702
llvm::CmpInst::ICMP_UGT
@ ICMP_UGT
unsigned greater than
Definition:InstrTypes.h:696
llvm::CmpInst::ICMP_SGT
@ ICMP_SGT
signed greater than
Definition:InstrTypes.h:700
llvm::CmpInst::ICMP_ULT
@ ICMP_ULT
unsigned less than
Definition:InstrTypes.h:698
llvm::CmpInst::ICMP_NE
@ ICMP_NE
not equal
Definition:InstrTypes.h:695
llvm::CmpInst::ICMP_SGE
@ ICMP_SGE
signed greater or equal
Definition:InstrTypes.h:701
llvm::CmpInst::getInversePredicate
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition:InstrTypes.h:787
llvm::CmpPredicate
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Definition:CmpPredicate.h:22
llvm::ConstantExpr
A constant value that is initialized with an expression using other constant values.
Definition:Constants.h:1108
llvm::ConstantExpr::getCast
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition:Constants.cpp:2222
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition:Constants.h:83
llvm::ConstantInt::getFalse
static ConstantInt * getFalse(LLVMContext &Context)
Definition:Constants.cpp:873
llvm::Constant
This is an important base class in LLVM.
Definition:Constant.h:42
llvm::Constant::getNullValue
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition:Constants.cpp:373
llvm::DWARFExpression::Operation
This class represents an Operation in the Expression.
Definition:DWARFExpression.h:32
llvm::DataLayout::isNonIntegralPointerType
bool isNonIntegralPointerType(PointerType *PT) const
Definition:DataLayout.h:352
llvm::DebugLoc
A debug info location.
Definition:DebugLoc.h:33
llvm::DenseMap
Definition:DenseMap.h:727
llvm::DominatorTreeBase::properlyDominates
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
Definition:GenericDomTree.h:443
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition:Dominators.h:162
llvm::DominatorTree::dominates
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition:Dominators.cpp:122
llvm::Function::getEntryBlock
const BasicBlock & getEntryBlock() const
Definition:Function.h:809
llvm::GEPNoWrapFlags
Represents flags for the getelementptr instruction/expression.
Definition:GEPNoWrapFlags.h:26
llvm::GEPNoWrapFlags::noUnsignedWrap
static GEPNoWrapFlags noUnsignedWrap()
Definition:GEPNoWrapFlags.h:56
llvm::GEPNoWrapFlags::none
static GEPNoWrapFlags none()
Definition:GEPNoWrapFlags.h:46
llvm::IRBuilderBase::CreateVScale
Value * CreateVScale(Constant *Scaling, const Twine &Name="")
Create a call to llvm.vscale, multiplied by Scaling.
Definition:IRBuilder.cpp:89
llvm::IRBuilderBase::CreateZExtOrTrunc
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition:IRBuilder.h:2051
llvm::IRBuilderBase::CreateExtractValue
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition:IRBuilder.h:2555
llvm::IRBuilderBase::CreateSelect
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition:IRBuilder.cpp:1053
llvm::IRBuilderBase::GetInsertPoint
BasicBlock::iterator GetInsertPoint() const
Definition:IRBuilder.h:194
llvm::IRBuilderBase::CreateSExt
Value * CreateSExt(Value *V, Type *DestTy, const Twine &Name="")
Definition:IRBuilder.h:2045
llvm::IRBuilderBase::CreateFreeze
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition:IRBuilder.h:2574
llvm::IRBuilderBase::CreatePtrAdd
Value * CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition:IRBuilder.h:1987
llvm::IRBuilderBase::CreateCast
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr, FMFSource FMFSource={})
Definition:IRBuilder.h:2186
llvm::IRBuilderBase::GetInsertBlock
BasicBlock * GetInsertBlock() const
Definition:IRBuilder.h:193
llvm::IRBuilderBase::SetCurrentDebugLocation
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition:IRBuilder.h:239
llvm::IRBuilderBase::CreateNeg
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Definition:IRBuilder.h:1733
llvm::IRBuilderBase::CreateIntrinsic
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition:IRBuilder.cpp:900
llvm::IRBuilderBase::CreatePHI
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition:IRBuilder.h:2435
llvm::IRBuilderBase::Insert
InstTy * Insert(InstTy *I, const Twine &Name="") const
Insert and return the specified instruction.
Definition:IRBuilder.h:164
llvm::IRBuilderBase::CreateSub
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition:IRBuilder.h:1387
llvm::IRBuilderBase::CreateZExt
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition:IRBuilder.h:2033
llvm::IRBuilderBase::CreateAnd
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:1518
llvm::IRBuilderBase::CreateAdd
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition:IRBuilder.h:1370
llvm::IRBuilderBase::CreateTrunc
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition:IRBuilder.h:2019
llvm::IRBuilderBase::CreateOr
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:1540
llvm::IRBuilderBase::SetInsertPoint
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition:IRBuilder.h:199
llvm::IRBuilderBase::CreateTruncOrBitCast
Value * CreateTruncOrBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition:IRBuilder.h:2178
llvm::IRBuilderBase::CreateICmp
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:2380
llvm::IRBuilderBase::getInt8Ty
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition:IRBuilder.h:535
llvm::IRBuilder
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition:IRBuilder.h:2705
llvm::InstructionCost
Definition:InstructionCost.h:29
llvm::Instruction
Definition:Instruction.h:68
llvm::Instruction::setHasNoUnsignedWrap
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
Definition:Instruction.cpp:379
llvm::Instruction::setHasNoSignedWrap
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
Definition:Instruction.cpp:386
llvm::Instruction::insertBefore
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction.
Definition:Instruction.cpp:99
llvm::Instruction::getDebugLoc
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition:Instruction.h:492
llvm::Instruction::eraseFromParent
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition:Instruction.cpp:94
llvm::Instruction::getFunction
const Function * getFunction() const
Return the function this instruction belongs to.
Definition:Instruction.cpp:72
llvm::Instruction::mayHaveSideEffects
bool mayHaveSideEffects() const LLVM_READONLY
Return true if the instruction may have side effects.
Definition:Instruction.cpp:1185
llvm::Instruction::comesBefore
bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
Definition:Instruction.cpp:334
llvm::Instruction::getNextNonDebugInstruction
const Instruction * getNextNonDebugInstruction(bool SkipPseudoOp=false) const
Return a pointer to the next non-debug instruction in the same basic block as 'this',...
Definition:Instruction.cpp:1226
llvm::Instruction::getOpcode
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition:Instruction.h:291
llvm::Instruction::BinaryOps
BinaryOps
Definition:Instruction.h:989
llvm::Instruction::setDebugLoc
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition:Instruction.h:489
llvm::Instruction::CastOps
CastOps
Definition:Instruction.h:1003
llvm::IntegerType
Class to represent integer types.
Definition:DerivedTypes.h:42
llvm::IntegerType::get
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition:Type.cpp:311
llvm::LoopBase::contains
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition:GenericLoopInfo.h:124
llvm::LoopBase::getHeader
BlockT * getHeader() const
Definition:GenericLoopInfo.h:90
llvm::LoopInfoBase::getLoopFor
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition:GenericLoopInfo.h:606
llvm::LoopInfo::replacementPreservesLCSSAForm
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form.
Definition:LoopInfo.h:439
llvm::LoopInfo::movementPreservesLCSSAForm
bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc)
Checks if moving a specific instruction can break LCSSA in any loop.
Definition:LoopInfo.h:465
llvm::Loop
Represents a single loop in the control flow graph.
Definition:LoopInfo.h:39
llvm::MinMaxIntrinsic::getPredicate
ICmpInst::Predicate getPredicate() const
Returns the comparison predicate underlying the intrinsic.
Definition:IntrinsicInst.h:798
llvm::PHINode
Definition:Instructions.h:2600
llvm::PHINode::addIncoming
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Definition:Instructions.h:2735
llvm::PHINode::isComplete
bool isComplete() const
If the PHI node is complete which means all of its parent's predecessors have incoming value in this ...
Definition:Instructions.h:2805
llvm::PHINode::getIncomingValueForBlock
Value * getIncomingValueForBlock(const BasicBlock *BB) const
Definition:Instructions.h:2775
llvm::PHINode::Create
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
Definition:Instructions.h:2635
llvm::PointerType::get
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
llvm::PoisonValue::get
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition:Constants.cpp:1878
llvm::PredIterator
Definition:CFG.h:42
llvm::SCEVAddExpr
This node represents an addition of some number of SCEVs.
Definition:ScalarEvolutionExpressions.h:266
llvm::SCEVAddRecExpr
This node represents a polynomial recurrence on the trip count of the specified loop.
Definition:ScalarEvolutionExpressions.h:347
llvm::SCEVAddRecExpr::getType
Type * getType() const
Definition:ScalarEvolutionExpressions.h:357
llvm::SCEVAddRecExpr::getStart
const SCEV * getStart() const
Definition:ScalarEvolutionExpressions.h:358
llvm::SCEVAddRecExpr::getStepRecurrence
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
Definition:ScalarEvolutionExpressions.h:365
llvm::SCEVAddRecExpr::isAffine
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
Definition:ScalarEvolutionExpressions.h:375
llvm::SCEVAddRecExpr::getLoop
const Loop * getLoop() const
Definition:ScalarEvolutionExpressions.h:359
llvm::SCEVCastExpr::getOperand
const SCEV * getOperand() const
Definition:ScalarEvolutionExpressions.h:112
llvm::SCEVCastExpr::getType
Type * getType() const
Definition:ScalarEvolutionExpressions.h:119
llvm::SCEVComparePredicate
This class represents an assumption that the expression LHS Pred RHS evaluates to true,...
Definition:ScalarEvolution.h:277
llvm::SCEVComparePredicate::getRHS
const SCEV * getRHS() const
Returns the right hand side of the predicate.
Definition:ScalarEvolution.h:299
llvm::SCEVComparePredicate::getPredicate
ICmpInst::Predicate getPredicate() const
Definition:ScalarEvolution.h:293
llvm::SCEVComparePredicate::getLHS
const SCEV * getLHS() const
Returns the left hand side of the predicate.
Definition:ScalarEvolution.h:296
llvm::SCEVConstant
This class represents a constant integer value.
Definition:ScalarEvolutionExpressions.h:60
llvm::SCEVExpanderCleaner::cleanup
void cleanup()
Definition:ScalarEvolutionExpander.cpp:2381
llvm::SCEVExpander::generateOverflowCheck
Value * generateOverflowCheck(const SCEVAddRecExpr *AR, Instruction *Loc, bool Signed)
Generates code that evaluates if the AR expression will overflow.
Definition:ScalarEvolutionExpander.cpp:2094
llvm::SCEVExpander::hasRelatedExistingExpansion
bool hasRelatedExistingExpansion(const SCEV *S, const Instruction *At, Loop *L)
Determine whether there is an existing expansion of S that can be reused.
Definition:ScalarEvolutionExpander.cpp:1809
llvm::SCEVExpander::getAllInsertedInstructions
SmallVector< Instruction *, 32 > getAllInsertedInstructions() const
Return a vector containing all instructions inserted during expansion.
Definition:ScalarEvolutionExpander.h:224
llvm::SCEVExpander::isSafeToExpand
bool isSafeToExpand(const SCEV *S) const
Return true if the given expression is safe to expand in the sense that all materialized values are s...
Definition:ScalarEvolutionExpander.cpp:2353
llvm::SCEVExpander::isSafeToExpandAt
bool isSafeToExpandAt(const SCEV *S, const Instruction *InsertionPoint) const
Return true if the given expression is safe to expand in the sense that all materialized values are d...
Definition:ScalarEvolutionExpander.cpp:2359
llvm::SCEVExpander::replaceCongruentIVs
unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT, SmallVectorImpl< WeakTrackingVH > &DeadInsts, const TargetTransformInfo *TTI=nullptr)
replace congruent phis with their most canonical representative.
Definition:ScalarEvolutionExpander.cpp:1711
llvm::SCEVExpander::expandUnionPredicate
Value * expandUnionPredicate(const SCEVUnionPredicate *Pred, Instruction *Loc)
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a S...
Definition:ScalarEvolutionExpander.cpp:2243
llvm::SCEVExpander::hoistIVInc
bool hoistIVInc(Instruction *IncV, Instruction *InsertPos, bool RecomputePoisonFlags=false)
Utility for hoisting IncV (with all subexpressions requried for its computation) before InsertPos.
Definition:ScalarEvolutionExpander.cpp:802
llvm::SCEVExpander::clear
void clear()
Erase the contents of the InsertedExpressions map so that users trying to expand the same expression ...
Definition:ScalarEvolutionExpander.h:210
llvm::SCEVExpander::isInsertedInstruction
bool isInsertedInstruction(Instruction *I) const
Return true if the specified instruction was inserted by the code rewriter.
Definition:ScalarEvolutionExpander.h:407
llvm::SCEVExpander::expandCodeForPredicate
Value * expandCodeForPredicate(const SCEVPredicate *Pred, Instruction *Loc)
Generates a code sequence that evaluates this predicate.
Definition:ScalarEvolutionExpander.cpp:2067
llvm::SCEVExpander::canReuseFlagsFromOriginalIVInc
static bool canReuseFlagsFromOriginalIVInc(PHINode *OrigPhi, PHINode *WidePhi, Instruction *OrigInc, Instruction *WideInc)
Return true if both increments directly increment the corresponding IV PHI nodes and have the same op...
Definition:ScalarEvolutionExpander.cpp:855
llvm::SCEVExpander::expandComparePredicate
Value * expandComparePredicate(const SCEVComparePredicate *Pred, Instruction *Loc)
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a S...
Definition:ScalarEvolutionExpander.cpp:2083
llvm::SCEVExpander::expandCodeFor
Value * expandCodeFor(const SCEV *SH, Type *Ty, BasicBlock::iterator I)
Insert code to directly compute the specified SCEV expression into the program.
Definition:ScalarEvolutionExpander.cpp:1443
llvm::SCEVExpander::expandWrapPredicate
Value * expandWrapPredicate(const SCEVWrapPredicate *P, Instruction *Loc)
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a S...
Definition:ScalarEvolutionExpander.cpp:2218
llvm::SCEVExpander::getIVIncOperand
Instruction * getIVIncOperand(Instruction *IncV, Instruction *InsertPos, bool allowScale)
Return the induction variable increment's IV operand.
Definition:ScalarEvolutionExpander.cpp:742
llvm::SCEVExpander::findInsertPointAfter
BasicBlock::iterator findInsertPointAfter(Instruction *I, Instruction *MustDominate) const
Returns a suitable insert point after I, that dominates MustDominate.
Definition:ScalarEvolutionExpander.cpp:148
llvm::SCEVExpander::setInsertPoint
void setInsertPoint(Instruction *IP)
Set the current insertion point.
Definition:ScalarEvolutionExpander.h:381
llvm::SCEVMulExpr
This node represents multiplication of some number of SCEVs.
Definition:ScalarEvolutionExpressions.h:290
llvm::SCEVMulExpr::getType
Type * getType() const
Definition:ScalarEvolutionExpressions.h:297
llvm::SCEVNAryExpr
This node is a base class providing common functionality for n'ary operators.
Definition:ScalarEvolutionExpressions.h:196
llvm::SCEVNAryExpr::hasNoUnsignedWrap
bool hasNoUnsignedWrap() const
Definition:ScalarEvolutionExpressions.h:226
llvm::SCEVNAryExpr::getNumOperands
size_t getNumOperands() const
Definition:ScalarEvolutionExpressions.h:211
llvm::SCEVNAryExpr::hasNoSignedWrap
bool hasNoSignedWrap() const
Definition:ScalarEvolutionExpressions.h:230
llvm::SCEVNAryExpr::getNoWrapFlags
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask=NoWrapMask) const
Definition:ScalarEvolutionExpressions.h:222
llvm::SCEVNAryExpr::getOperand
const SCEV * getOperand(unsigned i) const
Definition:ScalarEvolutionExpressions.h:213
llvm::SCEVNAryExpr::operands
ArrayRef< const SCEV * > operands() const
Definition:ScalarEvolutionExpressions.h:218
llvm::SCEVPredicate
This class represents an assumption made using SCEV expressions which can be checked at run-time.
Definition:ScalarEvolution.h:214
llvm::SCEVPredicate::getKind
SCEVPredicateKind getKind() const
Definition:ScalarEvolution.h:233
llvm::SCEVPredicate::P_Compare
@ P_Compare
Definition:ScalarEvolution.h:222
llvm::SCEVPredicate::P_Union
@ P_Union
Definition:ScalarEvolution.h:222
llvm::SCEVPredicate::P_Wrap
@ P_Wrap
Definition:ScalarEvolution.h:222
llvm::SCEVPtrToIntExpr
This class represents a cast from a pointer to a pointer-sized integer value.
Definition:ScalarEvolutionExpressions.h:130
llvm::SCEVSMaxExpr
This class represents a signed maximum selection.
Definition:ScalarEvolutionExpressions.h:464
llvm::SCEVSMinExpr
This class represents a signed minimum selection.
Definition:ScalarEvolutionExpressions.h:488
llvm::SCEVSequentialUMinExpr
This class represents a sequential/in-order unsigned minimum selection.
Definition:ScalarEvolutionExpressions.h:560
llvm::SCEVSignExtendExpr
This class represents a sign extension of a small integer value to a larger integer value.
Definition:ScalarEvolutionExpressions.h:182
llvm::SCEVTruncateExpr
This class represents a truncation of an integer value to a smaller integer value.
Definition:ScalarEvolutionExpressions.h:156
llvm::SCEVUDivExpr
This class represents a binary unsigned division operation.
Definition:ScalarEvolutionExpressions.h:304
llvm::SCEVUDivExpr::getLHS
const SCEV * getLHS() const
Definition:ScalarEvolutionExpressions.h:316
llvm::SCEVUDivExpr::getRHS
const SCEV * getRHS() const
Definition:ScalarEvolutionExpressions.h:317
llvm::SCEVUMaxExpr
This class represents an unsigned maximum selection.
Definition:ScalarEvolutionExpressions.h:476
llvm::SCEVUMinExpr
This class represents an unsigned minimum selection.
Definition:ScalarEvolutionExpressions.h:500
llvm::SCEVUnionPredicate
This class represents a composition of other SCEV predicates, and is the class that most clients will...
Definition:ScalarEvolution.h:412
llvm::SCEVUnknown
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
Definition:ScalarEvolutionExpressions.h:577
llvm::SCEVVScale
This class represents the value of vscale, as used when defining the length of a scalable vector or r...
Definition:ScalarEvolutionExpressions.h:80
llvm::SCEVVScale::getType
Type * getType() const
Definition:ScalarEvolutionExpressions.h:89
llvm::SCEVWrapPredicate
This class represents an assumption made on an AddRec expression.
Definition:ScalarEvolution.h:317
llvm::SCEVWrapPredicate::IncrementNUSW
@ IncrementNUSW
Definition:ScalarEvolution.h:343
llvm::SCEVWrapPredicate::IncrementNSSW
@ IncrementNSSW
Definition:ScalarEvolution.h:344
llvm::SCEVWrapPredicate::getExpr
const SCEVAddRecExpr * getExpr() const
Implementation of the SCEVPredicate interface.
Definition:ScalarEvolution.cpp:14961
llvm::SCEVWrapPredicate::getFlags
IncrementWrapFlags getFlags() const
Returns the set assumed no overflow flags.
Definition:ScalarEvolution.h:392
llvm::SCEVZeroExtendExpr
This class represents a zero extension of a small integer value to a larger integer value.
Definition:ScalarEvolutionExpressions.h:168
llvm::SCEV
This class represents an analyzed expression in the program.
Definition:ScalarEvolution.h:71
llvm::SCEV::operands
ArrayRef< const SCEV * > operands() const
Return operands of this SCEV expression.
Definition:ScalarEvolution.cpp:420
llvm::SCEV::isOne
bool isOne() const
Return true if the expression is a constant one.
Definition:ScalarEvolution.cpp:450
llvm::SCEV::isZero
bool isZero() const
Return true if the expression is a constant zero.
Definition:ScalarEvolution.cpp:448
llvm::SCEV::isNonConstantNegative
bool isNonConstantNegative() const
Return true if the specified scev is negated, but not a constant.
Definition:ScalarEvolution.cpp:454
llvm::SCEV::getSCEVType
SCEVTypes getSCEVType() const
Definition:ScalarEvolution.h:140
llvm::SCEV::getType
Type * getType() const
Return the LLVM type of this SCEV expression.
Definition:ScalarEvolution.cpp:386
llvm::SCEV::NoWrapFlags
NoWrapFlags
NoWrapFlags are bitfield indices into SubclassData.
Definition:ScalarEvolution.h:126
llvm::SCEV::FlagAnyWrap
@ FlagAnyWrap
Definition:ScalarEvolution.h:127
llvm::SCEV::FlagNSW
@ FlagNSW
Definition:ScalarEvolution.h:130
llvm::SCEV::FlagNUW
@ FlagNUW
Definition:ScalarEvolution.h:129
llvm::SCEV::FlagNW
@ FlagNW
Definition:ScalarEvolution.h:128
llvm::ScalarEvolution
The main scalar evolution driver.
Definition:ScalarEvolution.h:447
llvm::ScalarEvolution::isKnownNonNegative
bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
Definition:ScalarEvolution.cpp:10952
llvm::ScalarEvolution::getNegativeSCEV
const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
Definition:ScalarEvolution.cpp:4569
llvm::ScalarEvolution::isKnownNegative
bool isKnownNegative(const SCEV *S)
Test if the given expression is known to be negative.
Definition:ScalarEvolution.cpp:10944
llvm::ScalarEvolution::removePointerBase
const SCEV * removePointerBase(const SCEV *S)
Compute an expression equivalent to S - getPointerBase(S).
Definition:ScalarEvolution.cpp:4625
llvm::ScalarEvolution::isKnownNonZero
bool isKnownNonZero(const SCEV *S)
Test if the given expression is known to be non-zero.
Definition:ScalarEvolution.cpp:10960
llvm::ScalarEvolution::getTypeSizeInBits
uint64_t getTypeSizeInBits(Type *Ty) const
Return the size in bits of the specified type, for which isSCEVable must return true.
Definition:ScalarEvolution.cpp:4448
llvm::ScalarEvolution::getConstant
const SCEV * getConstant(ConstantInt *V)
Definition:ScalarEvolution.cpp:473
llvm::ScalarEvolution::getSCEV
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
Definition:ScalarEvolution.cpp:4547
llvm::ScalarEvolution::getTruncateOrNoop
const SCEV * getTruncateOrNoop(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
Definition:ScalarEvolution.cpp:4766
llvm::ScalarEvolution::isLoopInvariant
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
Definition:ScalarEvolution.cpp:14100
llvm::ScalarEvolution::isKnownPositive
bool isKnownPositive(const SCEV *S)
Test if the given expression is known to be positive.
Definition:ScalarEvolution.cpp:10948
llvm::ScalarEvolution::containsAddRecurrence
bool containsAddRecurrence(const SCEV *S)
Return true if the SCEV is a scAddRecExpr or it contains scAddRecExpr.
Definition:ScalarEvolution.cpp:4500
llvm::ScalarEvolution::getAddRecExpr
const SCEV * getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L, SCEV::NoWrapFlags Flags)
Get an add recurrence expression for the specified loop.
Definition:ScalarEvolution.cpp:3641
llvm::ScalarEvolution::getZeroExtendExpr
const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
Definition:ScalarEvolution.cpp:1565
llvm::ScalarEvolution::isSCEVable
bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
Definition:ScalarEvolution.cpp:4441
llvm::ScalarEvolution::getEffectiveSCEVType
Type * getEffectiveSCEVType(Type *Ty) const
Return a type with the same bitwidth as the given type and which represents how SCEV will treat the g...
Definition:ScalarEvolution.cpp:4458
llvm::ScalarEvolution::clearFlags
static SCEV::NoWrapFlags clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags)
Definition:ScalarEvolution.h:476
llvm::ScalarEvolution::forgetValue
void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
Definition:ScalarEvolution.cpp:8542
llvm::ScalarEvolution::getNoopOrAnyExtend
const SCEV * getNoopOrAnyExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
Definition:ScalarEvolution.cpp:4754
llvm::ScalarEvolution::getTruncateExpr
const SCEV * getTruncateExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
Definition:ScalarEvolution.cpp:1150
llvm::ScalarEvolution::getMinusSCEV
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
Definition:ScalarEvolution.cpp:4655
llvm::ScalarEvolution::getAnyExtendExpr
const SCEV * getAnyExtendExpr(const SCEV *Op, Type *Ty)
getAnyExtendExpr - Return a SCEV for the given operand extended with unspecified bits out to the give...
Definition:ScalarEvolution.cpp:2180
llvm::ScalarEvolution::getStrengthenedNoWrapFlagsFromBinOp
std::optional< SCEV::NoWrapFlags > getStrengthenedNoWrapFlagsFromBinOp(const OverflowingBinaryOperator *OBO)
Parse NSW/NUW flags from add/sub/mul IR binary operation Op into SCEV no-wrap flags,...
Definition:ScalarEvolution.cpp:2391
llvm::ScalarEvolution::getSignExtendExpr
const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
Definition:ScalarEvolution.cpp:1900
llvm::ScalarEvolution::hasComputableLoopEvolution
bool hasComputableLoopEvolution(const SCEV *S, const Loop *L)
Return true if the given SCEV changes value in a known way in the specified loop.
Definition:ScalarEvolution.cpp:14104
llvm::ScalarEvolution::getPointerBase
const SCEV * getPointerBase(const SCEV *V)
Transitively follow the chain of pointer-type operands until reaching a SCEV that does not have a sin...
Definition:ScalarEvolution.cpp:4823
llvm::ScalarEvolution::dominates
bool dominates(const SCEV *S, const BasicBlock *BB)
Return true if elements that makes up the given SCEV dominate the specified basic block.
Definition:ScalarEvolution.cpp:14183
llvm::ScalarEvolution::getMulExpr
const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
Definition:ScalarEvolution.cpp:3106
llvm::ScalarEvolution::getUnknown
const SCEV * getUnknown(Value *V)
Definition:ScalarEvolution.cpp:4411
llvm::ScalarEvolution::maskFlags
static SCEV::NoWrapFlags maskFlags(SCEV::NoWrapFlags Flags, int Mask)
Convenient NoWrapFlags manipulation that hides enum casts and is visible in the ScalarEvolution name ...
Definition:ScalarEvolution.h:467
llvm::ScalarEvolution::properlyDominates
bool properlyDominates(const SCEV *S, const BasicBlock *BB)
Return true if elements that makes up the given SCEV properly dominate the specified basic block.
Definition:ScalarEvolution.cpp:14187
llvm::ScalarEvolution::canReuseInstruction
bool canReuseInstruction(const SCEV *S, Instruction *I, SmallVectorImpl< Instruction * > &DropPoisonGeneratingInsts)
Check whether it is poison-safe to represent the expression S using the instruction I.
Definition:ScalarEvolution.cpp:4168
llvm::ScalarEvolution::getPredicatedSymbolicMaxBackedgeTakenCount
const SCEV * getPredicatedSymbolicMaxBackedgeTakenCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Predicates)
Similar to getSymbolicMaxBackedgeTakenCount, except it will add a set of SCEV predicates to Predicate...
Definition:ScalarEvolution.cpp:8363
llvm::ScalarEvolution::getAddExpr
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
Definition:ScalarEvolution.cpp:2526
llvm::SmallPtrSetImplBase::clear
void clear()
Definition:SmallPtrSet.h:97
llvm::SmallPtrSetImplBase::empty
bool empty() const
Definition:SmallPtrSet.h:93
llvm::SmallPtrSetImpl
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition:SmallPtrSet.h:363
llvm::SmallPtrSetImpl::count
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition:SmallPtrSet.h:452
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition:SmallPtrSet.h:384
llvm::SmallPtrSet< const Loop *, 2 >
llvm::SmallSet
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition:SmallSet.h:132
llvm::SmallSet::insert
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition:SmallSet.h:181
llvm::SmallVectorBase::empty
bool empty() const
Definition:SmallVector.h:81
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition:SmallVector.h:573
llvm::SmallVectorImpl::emplace_back
reference emplace_back(ArgTypes &&... Args)
Definition:SmallVector.h:937
llvm::SmallVectorImpl::clear
void clear()
Definition:SmallVector.h:610
llvm::SmallVectorTemplateBase::push_back
void push_back(const T &Elt)
Definition:SmallVector.h:413
llvm::SmallVectorTemplateCommon::end
iterator end()
Definition:SmallVector.h:269
llvm::SmallVectorTemplateCommon::begin
iterator begin()
Definition:SmallVector.h:267
llvm::SmallVectorTemplateCommon::back
reference back()
Definition:SmallVector.h:308
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::TargetTransformInfo
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Definition:TargetTransformInfo.h:212
llvm::TargetTransformInfo::getCmpSelInstrCost
InstructionCost getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy, CmpInst::Predicate VecPred, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput, OperandValueInfo Op1Info={OK_AnyValue, OP_None}, OperandValueInfo Op2Info={OK_AnyValue, OP_None}, const Instruction *I=nullptr) const
Definition:TargetTransformInfo.cpp:1067
llvm::TargetTransformInfo::getCastInstrCost
InstructionCost getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src, TTI::CastContextHint CCH, TTI::TargetCostKind CostKind=TTI::TCK_SizeAndLatency, const Instruction *I=nullptr) const
Definition:TargetTransformInfo.cpp:1039
llvm::TargetTransformInfo::getIntImmCostInst
InstructionCost getIntImmCostInst(unsigned Opc, unsigned Idx, const APInt &Imm, Type *Ty, TargetCostKind CostKind, Instruction *Inst=nullptr) const
Return the expected cost of materialization for the given integer immediate of the specified type for...
Definition:TargetTransformInfo.cpp:735
llvm::TargetTransformInfo::TargetCostKind
TargetCostKind
The kind of cost model.
Definition:TargetTransformInfo.h:263
llvm::TargetTransformInfo::TCK_RecipThroughput
@ TCK_RecipThroughput
Reciprocal throughput.
Definition:TargetTransformInfo.h:264
llvm::TargetTransformInfo::TCK_CodeSize
@ TCK_CodeSize
Instruction code size.
Definition:TargetTransformInfo.h:266
llvm::TargetTransformInfo::getArithmeticInstrCost
InstructionCost getArithmeticInstrCost(unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput, TTI::OperandValueInfo Opd1Info={TTI::OK_AnyValue, TTI::OP_None}, TTI::OperandValueInfo Opd2Info={TTI::OK_AnyValue, TTI::OP_None}, ArrayRef< const Value * > Args={}, const Instruction *CxtI=nullptr, const TargetLibraryInfo *TLibInfo=nullptr) const
This is an approximation of reciprocal throughput of a math/logic op.
Definition:TargetTransformInfo.cpp:940
llvm::TargetTransformInfo::isTruncateFree
bool isTruncateFree(Type *Ty1, Type *Ty2) const
Return true if it's free to truncate a value of type Ty1 to type Ty2.
Definition:TargetTransformInfo.cpp:573
llvm::TargetTransformInfo::getCFInstrCost
InstructionCost getCFInstrCost(unsigned Opcode, TTI::TargetCostKind CostKind=TTI::TCK_SizeAndLatency, const Instruction *I=nullptr) const
Definition:TargetTransformInfo.cpp:1058
llvm::TargetTransformInfo::CastContextHint::None
@ None
The cast is not used with a load/store of any kind.
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition:Twine.h:81
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
llvm::Type::getIntegerBitWidth
unsigned getIntegerBitWidth() const
llvm::Type::isVectorTy
bool isVectorTy() const
True if this is an instance of VectorType.
Definition:Type.h:270
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition:Type.h:264
llvm::Type::getScalarSizeInBits
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
llvm::Type::getContext
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition:Type.h:128
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
llvm::Type::isIntegerTy
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition:Type.h:237
llvm::Type::getPrimitiveSizeInBits
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition:Use.h:43
llvm::User
Definition:User.h:44
llvm::User::operands
op_range operands()
Definition:User.h:288
llvm::User::getOperand
Value * getOperand(unsigned i) const
Definition:User.h:228
llvm::User::getNumOperands
unsigned getNumOperands() const
Definition:User.h:250
llvm::Value
LLVM Value Representation.
Definition:Value.h:74
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition:Value.h:255
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition:Value.cpp:534
llvm::Value::use_empty
bool use_empty() const
Definition:Value.h:344
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition:Value.cpp:1075
llvm::cl::opt
Definition:CommandLine.h:1423
llvm::details::FixedOrScalableQuantity::getFixedValue
constexpr ScalarTy getFixedValue() const
Definition:TypeSize.h:202
llvm::ilist_detail::node_parent_access::getParent
const ParentTy * getParent() const
Definition:ilist_node.h:32
llvm::ilist_node_impl::getIterator
self_iterator getIterator()
Definition:ilist_node.h:132
uint64_t
unsigned
UINT64_MAX
#define UINT64_MAX
Definition:DataTypes.h:77
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm::AMDGPU::Imm
@ Imm
Definition:AMDGPURegBankLegalizeRules.h:105
llvm::CallingConv::C
@ C
The default llvm calling convention, compatible with C.
Definition:CallingConv.h:34
llvm::M68k::MemAddrModeKind::U
@ U
llvm::M68k::MemAddrModeKind::V
@ V
llvm::M68k::MemAddrModeKind::L
@ L
llvm::MipsISD::Ret
@ Ret
Definition:MipsISelLowering.h:117
llvm::MipsISD::Ext
@ Ext
Definition:MipsISelLowering.h:157
llvm::PPCISD::SC
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
Definition:PPCISelLowering.h:429
llvm::PatternMatch
Definition:PatternMatch.h:47
llvm::PatternMatch::m_Power2
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition:PatternMatch.h:619
llvm::PatternMatch::match
bool match(Val *V, const Pattern &P)
Definition:PatternMatch.h:49
llvm::PatternMatch::m_Instruction
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition:PatternMatch.h:826
llvm::PatternMatch::m_Specific
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition:PatternMatch.h:885
llvm::PatternMatch::m_Br
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
Definition:PatternMatch.h:2220
llvm::PatternMatch::m_Value
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition:PatternMatch.h:92
llvm::PatternMatch::m_c_BinOp
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
Definition:PatternMatch.h:2757
llvm::PatternMatch::m_ICmp
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Definition:PatternMatch.h:1627
llvm::PatternMatch::m_BasicBlock
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
Definition:PatternMatch.h:189
llvm::RISCVFenceField::W
@ W
Definition:RISCVBaseInfo.h:374
llvm::WinEH::EncodingType::CE
@ CE
Windows NT (Windows on ARM)
llvm::cl::Hidden
@ Hidden
Definition:CommandLine.h:137
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition:CommandLine.h:443
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::omp::RTLDependInfoFields::Flags
@ Flags
llvm::rdf::Phi
NodeAddr< PhiNode * > Phi
Definition:RDFGraph.h:390
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::visitAll
void visitAll(const SCEV *Root, SV &Visitor)
Use SCEVTraversal to visit all nodes in the given expression tree.
Definition:ScalarEvolutionExpressions.h:713
llvm::max
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
Definition:GCNRegPressure.h:132
llvm::drop_begin
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition:STLExtras.h:329
llvm::Offset
@ Offset
Definition:DWP.cpp:480
llvm::stable_sort
void stable_sort(R &&Range)
Definition:STLExtras.h:2037
llvm::all_of
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1739
llvm::make_scope_exit
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition:ScopeExit.h:59
llvm::enumerate
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition:STLExtras.h:2448
llvm::pred_end
auto pred_end(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1385
llvm::pred_size
auto pred_size(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1381
llvm::FloatStyle::Exponent
@ Exponent
llvm::simplifyInstruction
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
Definition:InstructionSimplify.cpp:7234
llvm::reverse
auto reverse(ContainerTy &&C)
Definition:STLExtras.h:420
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition:Debug.cpp:163
llvm::SCEVCheapExpansionBudget
cl::opt< unsigned > SCEVCheapExpansionBudget
llvm::ConstantFoldBinaryOpOperands
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
Definition:ConstantFolding.cpp:1300
llvm::normalizeForPostIncUse
const SCEV * normalizeForPostIncUse(const SCEV *S, const PostIncLoopSet &Loops, ScalarEvolution &SE, bool CheckInvertible=true)
Normalize S to be post-increment for all loops present in Loops.
Definition:ScalarEvolutionNormalization.cpp:97
llvm::RecurKind::Mul
@ Mul
Product of integers.
llvm::RecurKind::Add
@ Add
Sum of integers.
llvm::BitWidth
constexpr unsigned BitWidth
Definition:BitmaskEnum.h:217
llvm::formLCSSAForInstructions
bool formLCSSAForInstructions(SmallVectorImpl< Instruction * > &Worklist, const DominatorTree &DT, const LoopInfo &LI, ScalarEvolution *SE, SmallVectorImpl< PHINode * > *PHIsToRemove=nullptr, SmallVectorImpl< PHINode * > *InsertedPHIs=nullptr)
Ensures LCSSA form for every instruction from the Worklist in the scope of innermost containing loop.
Definition:LCSSA.cpp:325
llvm::pred_begin
auto pred_begin(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1383
llvm::predecessors
auto predecessors(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1377
llvm::is_contained
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition:STLExtras.h:1903
llvm::scUMinExpr
@ scUMinExpr
Definition:ScalarEvolutionExpressions.h:51
llvm::scAddRecExpr
@ scAddRecExpr
Definition:ScalarEvolutionExpressions.h:48
llvm::scSequentialUMinExpr
@ scSequentialUMinExpr
Definition:ScalarEvolutionExpressions.h:53
llvm::scAddExpr
@ scAddExpr
Definition:ScalarEvolutionExpressions.h:45
llvm::scVScale
@ scVScale
Definition:ScalarEvolutionExpressions.h:41
llvm::scSMaxExpr
@ scSMaxExpr
Definition:ScalarEvolutionExpressions.h:50
llvm::scUnknown
@ scUnknown
Definition:ScalarEvolutionExpressions.h:55
llvm::scSMinExpr
@ scSMinExpr
Definition:ScalarEvolutionExpressions.h:52
llvm::scCouldNotCompute
@ scCouldNotCompute
Definition:ScalarEvolutionExpressions.h:56
llvm::scConstant
@ scConstant
Definition:ScalarEvolutionExpressions.h:40
llvm::scSignExtend
@ scSignExtend
Definition:ScalarEvolutionExpressions.h:44
llvm::scTruncate
@ scTruncate
Definition:ScalarEvolutionExpressions.h:42
llvm::scUMaxExpr
@ scUMaxExpr
Definition:ScalarEvolutionExpressions.h:49
llvm::scZeroExtend
@ scZeroExtend
Definition:ScalarEvolutionExpressions.h:43
llvm::scPtrToInt
@ scPtrToInt
Definition:ScalarEvolutionExpressions.h:54
llvm::scUDivExpr
@ scUDivExpr
Definition:ScalarEvolutionExpressions.h:47
llvm::scMulExpr
@ scMulExpr
Definition:ScalarEvolutionExpressions.h:46
llvm::Cost
InstructionCost Cost
Definition:FunctionSpecialization.h:102
llvm::isImpliedByDomCondition
std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
Definition:ValueTracking.cpp:9680
llvm::SCEVExprContains
bool SCEVExprContains(const SCEV *Root, PredTy Pred)
Return true if any node in Root satisfies the predicate Pred.
Definition:ScalarEvolutionExpressions.h:720
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition:BitVector.h:860
raw_ostream.h
InsertionPoint
Definition:CFIFixup.cpp:129
WorkItem
Definition:WinEHPrepare.cpp:235
llvm::PoisonFlags
Definition:ScalarEvolutionExpander.h:44
llvm::PoisonFlags::NUW
unsigned NUW
Definition:ScalarEvolutionExpander.h:45
llvm::PoisonFlags::Disjoint
unsigned Disjoint
Definition:ScalarEvolutionExpander.h:48
llvm::PoisonFlags::NNeg
unsigned NNeg
Definition:ScalarEvolutionExpander.h:49
llvm::PoisonFlags::apply
void apply(Instruction *I)
Definition:ScalarEvolutionExpander.cpp:74
llvm::PoisonFlags::PoisonFlags
PoisonFlags(const Instruction *I)
Definition:ScalarEvolutionExpander.cpp:46
llvm::PoisonFlags::GEPNW
GEPNoWrapFlags GEPNW
Definition:ScalarEvolutionExpander.h:51
llvm::PoisonFlags::NSW
unsigned NSW
Definition:ScalarEvolutionExpander.h:46
llvm::PoisonFlags::SameSign
unsigned SameSign
Definition:ScalarEvolutionExpander.h:50
llvm::PoisonFlags::Exact
unsigned Exact
Definition:ScalarEvolutionExpander.h:47
llvm::SCEVOperand
struct for holding enough information to help calculate the cost of the given SCEV when expanded into...
Definition:ScalarEvolutionExpander.h:33
llvm::SCEVVisitor< SCEVExpander, Value * >::visit
Value * visit(const SCEV *S)
Definition:ScalarEvolutionExpressions.h:609
llvm::cl::desc
Definition:CommandLine.h:409

Generated on Thu Jul 17 2025 17:51:01 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp