LLVM 22.0.0git
IndVarSimplify.cpp
Go to the documentation of this file.
1//===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
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 transformation analyzes and transforms the induction variables (and
10// computations derived from them) into simpler forms suitable for subsequent
11// analysis and transformation.
12//
13// If the trip count of a loop is computable, this pass also makes the following
14// changes:
15// 1. The exit condition for the loop is canonicalized to compare the
16// induction value against the exit value. This turns loops like:
17// 'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)'
18// 2. Any use outside of the loop of an expression derived from the indvar
19// is changed to compute the derived value outside of the loop, eliminating
20// the dependence on the exit value of the induction variable. If the only
21// purpose of the loop is to compute the exit value of some derived
22// expression, this transformation will make the loop dead.
23//
24//===----------------------------------------------------------------------===//
25
27#include "llvm/ADT/APFloat.h"
28#include "llvm/ADT/ArrayRef.h"
29#include "llvm/ADT/STLExtras.h"
32#include "llvm/ADT/Statistic.h"
44#include "llvm/IR/BasicBlock.h"
45#include "llvm/IR/Constant.h"
47#include "llvm/IR/Constants.h"
48#include "llvm/IR/DataLayout.h"
50#include "llvm/IR/Dominators.h"
51#include "llvm/IR/Function.h"
52#include "llvm/IR/IRBuilder.h"
53#include "llvm/IR/InstrTypes.h"
54#include "llvm/IR/Instruction.h"
57#include "llvm/IR/Intrinsics.h"
58#include "llvm/IR/PassManager.h"
60#include "llvm/IR/Type.h"
61#include "llvm/IR/Use.h"
62#include "llvm/IR/User.h"
63#include "llvm/IR/Value.h"
64#include "llvm/IR/ValueHandle.h"
67#include "llvm/Support/Debug.h"
76#include <cassert>
77#include <cstdint>
78#include <utility>
79
80using namespace llvm;
81using namespace PatternMatch;
82using namespace SCEVPatternMatch;
83
84#define DEBUG_TYPE "indvars"
85
86STATISTIC(NumWidened , "Number of indvars widened");
87STATISTIC(NumReplaced , "Number of exit values replaced");
88STATISTIC(NumLFTR , "Number of loop exit tests replaced");
89STATISTIC(NumElimExt , "Number of IV sign/zero extends eliminated");
90STATISTIC(NumElimIV , "Number of congruent IVs eliminated");
91
93 "replexitval", cl::Hidden, cl::init(OnlyCheapRepl),
94 cl::desc("Choose the strategy to replace exit value in IndVarSimplify"),
96 clEnumValN(NeverRepl, "never", "never replace exit value"),
98 "only replace exit value when the cost is cheap"),
100 UnusedIndVarInLoop, "unusedindvarinloop",
101 "only replace exit value when it is an unused "
102 "induction variable in the loop and has cheap replacement cost"),
103 clEnumValN(NoHardUse, "noharduse",
104 "only replace exit values when loop def likely dead"),
105 clEnumValN(AlwaysRepl, "always",
106 "always replace exit value whenever possible")));
107
109 "indvars-post-increment-ranges", cl::Hidden,
110 cl::desc("Use post increment control-dependent ranges in IndVarSimplify"),
111 cl::init(true));
112
113static cl::opt<bool>
114DisableLFTR("disable-lftr", cl::Hidden, cl::init(false),
115 cl::desc("Disable Linear Function Test Replace optimization"));
116
117static cl::opt<bool>
118LoopPredication("indvars-predicate-loops", cl::Hidden, cl::init(true),
119 cl::desc("Predicate conditions in read only loops"));
120
122 "indvars-predicate-loop-traps", cl::Hidden, cl::init(true),
123 cl::desc("Predicate conditions that trap in loops with only local writes"));
124
125static cl::opt<bool>
126AllowIVWidening("indvars-widen-indvars", cl::Hidden, cl::init(true),
127 cl::desc("Allow widening of indvars to eliminate s/zext"));
128
129namespace {
130
131class IndVarSimplify {
132 LoopInfo *LI;
133 ScalarEvolution *SE;
134 DominatorTree *DT;
135 const DataLayout &DL;
138 std::unique_ptr<MemorySSAUpdater> MSSAU;
139
141 bool WidenIndVars;
142
143 bool RunUnswitching = false;
144
145 bool handleFloatingPointIV(Loop *L, PHINode *PH);
146 bool rewriteNonIntegerIVs(Loop *L);
147
148 bool simplifyAndExtend(Loop *L, SCEVExpander &Rewriter, LoopInfo *LI);
149 /// Try to improve our exit conditions by converting condition from signed
150 /// to unsigned or rotating computation out of the loop.
151 /// (See inline comment about why this is duplicated from simplifyAndExtend)
152 bool canonicalizeExitCondition(Loop *L);
153 /// Try to eliminate loop exits based on analyzeable exit counts
154 bool optimizeLoopExits(Loop *L, SCEVExpander &Rewriter);
155 /// Try to form loop invariant tests for loop exits by changing how many
156 /// iterations of the loop run when that is unobservable.
157 bool predicateLoopExits(Loop *L, SCEVExpander &Rewriter);
158
159 bool rewriteFirstIterationLoopExitValues(Loop *L);
160
161 bool linearFunctionTestReplace(Loop *L, BasicBlock *ExitingBB,
162 const SCEV *ExitCount,
163 PHINode *IndVar, SCEVExpander &Rewriter);
164
165 bool sinkUnusedInvariants(Loop *L);
166
167public:
168 IndVarSimplify(LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT,
169 const DataLayout &DL, TargetLibraryInfo *TLI,
170 TargetTransformInfo *TTI, MemorySSA *MSSA, bool WidenIndVars)
171 : LI(LI), SE(SE), DT(DT), DL(DL), TLI(TLI), TTI(TTI),
172 WidenIndVars(WidenIndVars) {
173 if (MSSA)
174 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
175 }
176
177 bool run(Loop *L);
178
179 bool runUnswitching() const { return RunUnswitching; }
180};
181
182} // end anonymous namespace
183
184//===----------------------------------------------------------------------===//
185// rewriteNonIntegerIVs and helpers. Prefer integer IVs.
186//===----------------------------------------------------------------------===//
187
188/// Convert APF to an integer, if possible.
189static bool ConvertToSInt(const APFloat &APF, int64_t &IntVal) {
190 bool isExact = false;
191 // See if we can convert this to an int64_t
192 uint64_t UIntVal;
193 if (APF.convertToInteger(MutableArrayRef(UIntVal), 64, true,
194 APFloat::rmTowardZero, &isExact) != APFloat::opOK ||
195 !isExact)
196 return false;
197 IntVal = UIntVal;
198 return true;
199}
200
201// Ensure we stay within the bounds of fp values that can be represented as
202// integers without gaps, which are 2^24 and 2^53 for IEEE-754 single and double
203// precision respectively (both on negative and positive side).
204static bool isRepresentableAsExactInteger(ConstantFP *FPVal, int64_t IntVal) {
205 const auto &InitValueFltSema = FPVal->getValueAPF().getSemantics();
206 if (!APFloat::isIEEELikeFP(InitValueFltSema))
207 return false;
208
209 return isUIntN(APFloat::semanticsPrecision(InitValueFltSema),
210 AbsoluteValue(IntVal));
211}
212
213/// If the loop has floating induction variable then insert corresponding
214/// integer induction variable if possible.
215/// For example,
216/// for(double i = 0; i < 10000; ++i)
217/// bar(i)
218/// is converted into
219/// for(int i = 0; i < 10000; ++i)
220/// bar((double)i);
221bool IndVarSimplify::handleFloatingPointIV(Loop *L, PHINode *PN) {
222 unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
223 unsigned BackEdge = IncomingEdge^1;
224
225 // Check incoming value.
226 auto *InitValueVal = dyn_cast<ConstantFP>(PN->getIncomingValue(IncomingEdge));
227
228 int64_t InitValue;
229 if (!InitValueVal || !ConvertToSInt(InitValueVal->getValueAPF(), InitValue) ||
230 !isRepresentableAsExactInteger(InitValueVal, InitValue))
231 return false;
232
233 // Check IV increment. Reject this PN if increment operation is not
234 // an add or increment value can not be represented by an integer.
235 auto *Incr = dyn_cast<BinaryOperator>(PN->getIncomingValue(BackEdge));
236 if (Incr == nullptr || Incr->getOpcode() != Instruction::FAdd) return false;
237
238 // If this is not an add of the PHI with a constantfp, or if the constant fp
239 // is not an integer, bail out.
240 ConstantFP *IncValueVal = dyn_cast<ConstantFP>(Incr->getOperand(1));
241 int64_t IncValue;
242 if (IncValueVal == nullptr || Incr->getOperand(0) != PN ||
243 !ConvertToSInt(IncValueVal->getValueAPF(), IncValue))
244 return false;
245
246 // Check Incr uses. One user is PN and the other user is an exit condition
247 // used by the conditional terminator.
248 Value::user_iterator IncrUse = Incr->user_begin();
249 Instruction *U1 = cast<Instruction>(*IncrUse++);
250 if (IncrUse == Incr->user_end()) return false;
251 Instruction *U2 = cast<Instruction>(*IncrUse++);
252 if (IncrUse != Incr->user_end()) return false;
253
254 // Find exit condition, which is an fcmp. If it doesn't exist, or if it isn't
255 // only used by a branch, we can't transform it.
256 FCmpInst *Compare = dyn_cast<FCmpInst>(U1);
257 if (!Compare)
259 if (!Compare || !Compare->hasOneUse() ||
260 !isa<BranchInst>(Compare->user_back()))
261 return false;
262
263 BranchInst *TheBr = cast<BranchInst>(Compare->user_back());
264
265 // We need to verify that the branch actually controls the iteration count
266 // of the loop. If not, the new IV can overflow and no one will notice.
267 // The branch block must be in the loop and one of the successors must be out
268 // of the loop.
269 assert(TheBr->isConditional() && "Can't use fcmp if not conditional");
270 if (!L->contains(TheBr->getParent()) ||
271 (L->contains(TheBr->getSuccessor(0)) &&
272 L->contains(TheBr->getSuccessor(1))))
273 return false;
274
275 // If it isn't a comparison with an integer-as-fp (the exit value), we can't
276 // transform it.
277 ConstantFP *ExitValueVal = dyn_cast<ConstantFP>(Compare->getOperand(1));
278 int64_t ExitValue;
279 if (ExitValueVal == nullptr ||
280 !ConvertToSInt(ExitValueVal->getValueAPF(), ExitValue) ||
281 !isRepresentableAsExactInteger(ExitValueVal, ExitValue))
282 return false;
283
284 // Find new predicate for integer comparison.
286 switch (Compare->getPredicate()) {
287 default: return false; // Unknown comparison.
289 case CmpInst::FCMP_UEQ: NewPred = CmpInst::ICMP_EQ; break;
291 case CmpInst::FCMP_UNE: NewPred = CmpInst::ICMP_NE; break;
293 case CmpInst::FCMP_UGT: NewPred = CmpInst::ICMP_SGT; break;
295 case CmpInst::FCMP_UGE: NewPred = CmpInst::ICMP_SGE; break;
297 case CmpInst::FCMP_ULT: NewPred = CmpInst::ICMP_SLT; break;
299 case CmpInst::FCMP_ULE: NewPred = CmpInst::ICMP_SLE; break;
300 }
301
302 // We convert the floating point induction variable to a signed i32 value if
303 // we can. This is only safe if the comparison will not overflow in a way
304 // that won't be trapped by the integer equivalent operations. Check for this
305 // now.
306 // TODO: We could use i64 if it is native and the range requires it.
307
308 // The start/stride/exit values must all fit in signed i32.
309 if (!isInt<32>(InitValue) || !isInt<32>(IncValue) || !isInt<32>(ExitValue))
310 return false;
311
312 // If not actually striding (add x, 0.0), avoid touching the code.
313 if (IncValue == 0)
314 return false;
315
316 // Positive and negative strides have different safety conditions.
317 if (IncValue > 0) {
318 // If we have a positive stride, we require the init to be less than the
319 // exit value.
320 if (InitValue >= ExitValue)
321 return false;
322
323 uint32_t Range = uint32_t(ExitValue-InitValue);
324 // Check for infinite loop, either:
325 // while (i <= Exit) or until (i > Exit)
326 if (NewPred == CmpInst::ICMP_SLE || NewPred == CmpInst::ICMP_SGT) {
327 if (++Range == 0) return false; // Range overflows.
328 }
329
330 unsigned Leftover = Range % uint32_t(IncValue);
331
332 // If this is an equality comparison, we require that the strided value
333 // exactly land on the exit value, otherwise the IV condition will wrap
334 // around and do things the fp IV wouldn't.
335 if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) &&
336 Leftover != 0)
337 return false;
338
339 // If the stride would wrap around the i32 before exiting, we can't
340 // transform the IV.
341 if (Leftover != 0 && int32_t(ExitValue+IncValue) < ExitValue)
342 return false;
343 } else {
344 // If we have a negative stride, we require the init to be greater than the
345 // exit value.
346 if (InitValue <= ExitValue)
347 return false;
348
349 uint32_t Range = uint32_t(InitValue-ExitValue);
350 // Check for infinite loop, either:
351 // while (i >= Exit) or until (i < Exit)
352 if (NewPred == CmpInst::ICMP_SGE || NewPred == CmpInst::ICMP_SLT) {
353 if (++Range == 0) return false; // Range overflows.
354 }
355
356 unsigned Leftover = Range % uint32_t(-IncValue);
357
358 // If this is an equality comparison, we require that the strided value
359 // exactly land on the exit value, otherwise the IV condition will wrap
360 // around and do things the fp IV wouldn't.
361 if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) &&
362 Leftover != 0)
363 return false;
364
365 // If the stride would wrap around the i32 before exiting, we can't
366 // transform the IV.
367 if (Leftover != 0 && int32_t(ExitValue+IncValue) > ExitValue)
368 return false;
369 }
370
371 IntegerType *Int32Ty = Type::getInt32Ty(PN->getContext());
372
373 // Insert new integer induction variable.
374 PHINode *NewPHI =
375 PHINode::Create(Int32Ty, 2, PN->getName() + ".int", PN->getIterator());
376 NewPHI->addIncoming(ConstantInt::getSigned(Int32Ty, InitValue),
377 PN->getIncomingBlock(IncomingEdge));
378 NewPHI->setDebugLoc(PN->getDebugLoc());
379
380 Instruction *NewAdd = BinaryOperator::CreateAdd(
381 NewPHI, ConstantInt::getSigned(Int32Ty, IncValue),
382 Incr->getName() + ".int", Incr->getIterator());
383 NewAdd->setDebugLoc(Incr->getDebugLoc());
384 NewPHI->addIncoming(NewAdd, PN->getIncomingBlock(BackEdge));
385
386 ICmpInst *NewCompare = new ICmpInst(
387 TheBr->getIterator(), NewPred, NewAdd,
388 ConstantInt::getSigned(Int32Ty, ExitValue), Compare->getName());
389 NewCompare->setDebugLoc(Compare->getDebugLoc());
390
391 // In the following deletions, PN may become dead and may be deleted.
392 // Use a WeakTrackingVH to observe whether this happens.
393 WeakTrackingVH WeakPH = PN;
394
395 // Delete the old floating point exit comparison. The branch starts using the
396 // new comparison.
397 NewCompare->takeName(Compare);
398 Compare->replaceAllUsesWith(NewCompare);
399 RecursivelyDeleteTriviallyDeadInstructions(Compare, TLI, MSSAU.get());
400
401 // Delete the old floating point increment.
402 Incr->replaceAllUsesWith(PoisonValue::get(Incr->getType()));
403 RecursivelyDeleteTriviallyDeadInstructions(Incr, TLI, MSSAU.get());
404
405 // If the FP induction variable still has uses, this is because something else
406 // in the loop uses its value. In order to canonicalize the induction
407 // variable, we chose to eliminate the IV and rewrite it in terms of an
408 // int->fp cast.
409 //
410 // We give preference to sitofp over uitofp because it is faster on most
411 // platforms.
412 if (WeakPH) {
413 Instruction *Conv = new SIToFPInst(NewPHI, PN->getType(), "indvar.conv",
414 PN->getParent()->getFirstInsertionPt());
415 Conv->setDebugLoc(PN->getDebugLoc());
416 PN->replaceAllUsesWith(Conv);
417 RecursivelyDeleteTriviallyDeadInstructions(PN, TLI, MSSAU.get());
418 }
419 return true;
420}
421
422bool IndVarSimplify::rewriteNonIntegerIVs(Loop *L) {
423 // First step. Check to see if there are any floating-point recurrences.
424 // If there are, change them into integer recurrences, permitting analysis by
425 // the SCEV routines.
426 BasicBlock *Header = L->getHeader();
427
429
430 bool Changed = false;
431 for (WeakTrackingVH &PHI : PHIs)
432 if (PHINode *PN = dyn_cast_or_null<PHINode>(&*PHI))
433 Changed |= handleFloatingPointIV(L, PN);
434
435 // If the loop previously had floating-point IV, ScalarEvolution
436 // may not have been able to compute a trip count. Now that we've done some
437 // re-writing, the trip count may be computable.
438 if (Changed)
439 SE->forgetLoop(L);
440 return Changed;
441}
442
443//===---------------------------------------------------------------------===//
444// rewriteFirstIterationLoopExitValues: Rewrite loop exit values if we know
445// they will exit at the first iteration.
446//===---------------------------------------------------------------------===//
447
448/// Check to see if this loop has loop invariant conditions which lead to loop
449/// exits. If so, we know that if the exit path is taken, it is at the first
450/// loop iteration. This lets us predict exit values of PHI nodes that live in
451/// loop header.
452bool IndVarSimplify::rewriteFirstIterationLoopExitValues(Loop *L) {
453 // Verify the input to the pass is already in LCSSA form.
454 assert(L->isLCSSAForm(*DT));
455
456 SmallVector<BasicBlock *, 8> ExitBlocks;
457 L->getUniqueExitBlocks(ExitBlocks);
458
459 bool MadeAnyChanges = false;
460 for (auto *ExitBB : ExitBlocks) {
461 // If there are no more PHI nodes in this exit block, then no more
462 // values defined inside the loop are used on this path.
463 for (PHINode &PN : ExitBB->phis()) {
464 for (unsigned IncomingValIdx = 0, E = PN.getNumIncomingValues();
465 IncomingValIdx != E; ++IncomingValIdx) {
466 auto *IncomingBB = PN.getIncomingBlock(IncomingValIdx);
467
468 // Can we prove that the exit must run on the first iteration if it
469 // runs at all? (i.e. early exits are fine for our purposes, but
470 // traces which lead to this exit being taken on the 2nd iteration
471 // aren't.) Note that this is about whether the exit branch is
472 // executed, not about whether it is taken.
473 if (!L->getLoopLatch() ||
474 !DT->dominates(IncomingBB, L->getLoopLatch()))
475 continue;
476
477 // Get condition that leads to the exit path.
478 auto *TermInst = IncomingBB->getTerminator();
479
480 Value *Cond = nullptr;
481 if (auto *BI = dyn_cast<BranchInst>(TermInst)) {
482 // Must be a conditional branch, otherwise the block
483 // should not be in the loop.
484 Cond = BI->getCondition();
485 } else if (auto *SI = dyn_cast<SwitchInst>(TermInst))
486 Cond = SI->getCondition();
487 else
488 continue;
489
490 if (!L->isLoopInvariant(Cond))
491 continue;
492
493 auto *ExitVal = dyn_cast<PHINode>(PN.getIncomingValue(IncomingValIdx));
494
495 // Only deal with PHIs in the loop header.
496 if (!ExitVal || ExitVal->getParent() != L->getHeader())
497 continue;
498
499 // If ExitVal is a PHI on the loop header, then we know its
500 // value along this exit because the exit can only be taken
501 // on the first iteration.
502 auto *LoopPreheader = L->getLoopPreheader();
503 assert(LoopPreheader && "Invalid loop");
504 int PreheaderIdx = ExitVal->getBasicBlockIndex(LoopPreheader);
505 if (PreheaderIdx != -1) {
506 assert(ExitVal->getParent() == L->getHeader() &&
507 "ExitVal must be in loop header");
508 MadeAnyChanges = true;
509 PN.setIncomingValue(IncomingValIdx,
510 ExitVal->getIncomingValue(PreheaderIdx));
511 SE->forgetValue(&PN);
512 }
513 }
514 }
515 }
516 return MadeAnyChanges;
517}
518
519//===----------------------------------------------------------------------===//
520// IV Widening - Extend the width of an IV to cover its widest uses.
521//===----------------------------------------------------------------------===//
522
523/// Update information about the induction variable that is extended by this
524/// sign or zero extend operation. This is used to determine the final width of
525/// the IV before actually widening it.
526static void visitIVCast(CastInst *Cast, WideIVInfo &WI,
527 ScalarEvolution *SE,
528 const TargetTransformInfo *TTI) {
529 bool IsSigned = Cast->getOpcode() == Instruction::SExt;
530 if (!IsSigned && Cast->getOpcode() != Instruction::ZExt)
531 return;
532
533 Type *Ty = Cast->getType();
534 uint64_t Width = SE->getTypeSizeInBits(Ty);
535 if (!Cast->getDataLayout().isLegalInteger(Width))
536 return;
537
538 // Check that `Cast` actually extends the induction variable (we rely on this
539 // later). This takes care of cases where `Cast` is extending a truncation of
540 // the narrow induction variable, and thus can end up being narrower than the
541 // "narrow" induction variable.
542 uint64_t NarrowIVWidth = SE->getTypeSizeInBits(WI.NarrowIV->getType());
543 if (NarrowIVWidth >= Width)
544 return;
545
546 // Cast is either an sext or zext up to this point.
547 // We should not widen an indvar if arithmetics on the wider indvar are more
548 // expensive than those on the narrower indvar. We check only the cost of ADD
549 // because at least an ADD is required to increment the induction variable. We
550 // could compute more comprehensively the cost of all instructions on the
551 // induction variable when necessary.
552 if (TTI &&
553 TTI->getArithmeticInstrCost(Instruction::Add, Ty) >
554 TTI->getArithmeticInstrCost(Instruction::Add,
555 Cast->getOperand(0)->getType())) {
556 return;
557 }
558
559 if (!WI.WidestNativeType ||
560 Width > SE->getTypeSizeInBits(WI.WidestNativeType)) {
562 WI.IsSigned = IsSigned;
563 return;
564 }
565
566 // We extend the IV to satisfy the sign of its user(s), or 'signed'
567 // if there are multiple users with both sign- and zero extensions,
568 // in order not to introduce nondeterministic behaviour based on the
569 // unspecified order of a PHI nodes' users-iterator.
570 WI.IsSigned |= IsSigned;
571}
572
573//===----------------------------------------------------------------------===//
574// Live IV Reduction - Minimize IVs live across the loop.
575//===----------------------------------------------------------------------===//
576
577//===----------------------------------------------------------------------===//
578// Simplification of IV users based on SCEV evaluation.
579//===----------------------------------------------------------------------===//
580
581namespace {
582
583class IndVarSimplifyVisitor : public IVVisitor {
584 ScalarEvolution *SE;
585 const TargetTransformInfo *TTI;
586 PHINode *IVPhi;
587
588public:
589 WideIVInfo WI;
590
591 IndVarSimplifyVisitor(PHINode *IV, ScalarEvolution *SCEV,
592 const TargetTransformInfo *TTI,
593 const DominatorTree *DTree)
594 : SE(SCEV), TTI(TTI), IVPhi(IV) {
595 DT = DTree;
596 WI.NarrowIV = IVPhi;
597 }
598
599 // Implement the interface used by simplifyUsersOfIV.
600 void visitCast(CastInst *Cast) override { visitIVCast(Cast, WI, SE, TTI); }
601};
602
603} // end anonymous namespace
604
605/// Iteratively perform simplification on a worklist of IV users. Each
606/// successive simplification may push more users which may themselves be
607/// candidates for simplification.
608///
609/// Sign/Zero extend elimination is interleaved with IV simplification.
610bool IndVarSimplify::simplifyAndExtend(Loop *L,
611 SCEVExpander &Rewriter,
612 LoopInfo *LI) {
614
615 auto *GuardDecl = Intrinsic::getDeclarationIfExists(
616 L->getBlocks()[0]->getModule(), Intrinsic::experimental_guard);
617 bool HasGuards = GuardDecl && !GuardDecl->use_empty();
618
620 llvm::make_pointer_range(L->getHeader()->phis()));
621
622 // Each round of simplification iterates through the SimplifyIVUsers worklist
623 // for all current phis, then determines whether any IVs can be
624 // widened. Widening adds new phis to LoopPhis, inducing another round of
625 // simplification on the wide IVs.
626 bool Changed = false;
627 while (!LoopPhis.empty()) {
628 // Evaluate as many IV expressions as possible before widening any IVs. This
629 // forces SCEV to set no-wrap flags before evaluating sign/zero
630 // extension. The first time SCEV attempts to normalize sign/zero extension,
631 // the result becomes final. So for the most predictable results, we delay
632 // evaluation of sign/zero extend evaluation until needed, and avoid running
633 // other SCEV based analysis prior to simplifyAndExtend.
634 do {
635 PHINode *CurrIV = LoopPhis.pop_back_val();
636
637 // Information about sign/zero extensions of CurrIV.
638 IndVarSimplifyVisitor Visitor(CurrIV, SE, TTI, DT);
639
640 const auto &[C, U] = simplifyUsersOfIV(CurrIV, SE, DT, LI, TTI, DeadInsts,
641 Rewriter, &Visitor);
642
643 Changed |= C;
644 RunUnswitching |= U;
645 if (Visitor.WI.WidestNativeType) {
646 WideIVs.push_back(Visitor.WI);
647 }
648 } while(!LoopPhis.empty());
649
650 // Continue if we disallowed widening.
651 if (!WidenIndVars)
652 continue;
653
654 for (; !WideIVs.empty(); WideIVs.pop_back()) {
655 unsigned ElimExt;
656 unsigned Widened;
657 if (PHINode *WidePhi = createWideIV(WideIVs.back(), LI, SE, Rewriter,
658 DT, DeadInsts, ElimExt, Widened,
659 HasGuards, UsePostIncrementRanges)) {
660 NumElimExt += ElimExt;
661 NumWidened += Widened;
662 Changed = true;
663 LoopPhis.push_back(WidePhi);
664 }
665 }
666 }
667 return Changed;
668}
669
670//===----------------------------------------------------------------------===//
671// linearFunctionTestReplace and its kin. Rewrite the loop exit condition.
672//===----------------------------------------------------------------------===//
673
674/// Given an Value which is hoped to be part of an add recurance in the given
675/// loop, return the associated Phi node if so. Otherwise, return null. Note
676/// that this is less general than SCEVs AddRec checking.
679 if (!IncI)
680 return nullptr;
681
682 switch (IncI->getOpcode()) {
683 case Instruction::Add:
684 case Instruction::Sub:
685 break;
686 case Instruction::GetElementPtr:
687 // An IV counter must preserve its type.
688 if (IncI->getNumOperands() == 2)
689 break;
690 [[fallthrough]];
691 default:
692 return nullptr;
693 }
694
695 PHINode *Phi = dyn_cast<PHINode>(IncI->getOperand(0));
696 if (Phi && Phi->getParent() == L->getHeader()) {
697 if (L->isLoopInvariant(IncI->getOperand(1)))
698 return Phi;
699 return nullptr;
700 }
701 if (IncI->getOpcode() == Instruction::GetElementPtr)
702 return nullptr;
703
704 // Allow add/sub to be commuted.
705 Phi = dyn_cast<PHINode>(IncI->getOperand(1));
706 if (Phi && Phi->getParent() == L->getHeader()) {
707 if (L->isLoopInvariant(IncI->getOperand(0)))
708 return Phi;
709 }
710 return nullptr;
711}
712
713/// Whether the current loop exit test is based on this value. Currently this
714/// is limited to a direct use in the loop condition.
715static bool isLoopExitTestBasedOn(Value *V, BasicBlock *ExitingBB) {
716 BranchInst *BI = cast<BranchInst>(ExitingBB->getTerminator());
718 // TODO: Allow non-icmp loop test.
719 if (!ICmp)
720 return false;
721
722 // TODO: Allow indirect use.
723 return ICmp->getOperand(0) == V || ICmp->getOperand(1) == V;
724}
725
726/// linearFunctionTestReplace policy. Return true unless we can show that the
727/// current exit test is already sufficiently canonical.
728static bool needsLFTR(Loop *L, BasicBlock *ExitingBB) {
729 assert(L->getLoopLatch() && "Must be in simplified form");
730
731 // Avoid converting a constant or loop invariant test back to a runtime
732 // test. This is critical for when SCEV's cached ExitCount is less precise
733 // than the current IR (such as after we've proven a particular exit is
734 // actually dead and thus the BE count never reaches our ExitCount.)
735 BranchInst *BI = cast<BranchInst>(ExitingBB->getTerminator());
736 if (L->isLoopInvariant(BI->getCondition()))
737 return false;
738
739 // Do LFTR to simplify the exit condition to an ICMP.
741 if (!Cond)
742 return true;
743
744 // Do LFTR to simplify the exit ICMP to EQ/NE
745 ICmpInst::Predicate Pred = Cond->getPredicate();
746 if (Pred != ICmpInst::ICMP_NE && Pred != ICmpInst::ICMP_EQ)
747 return true;
748
749 // Look for a loop invariant RHS
750 Value *LHS = Cond->getOperand(0);
751 Value *RHS = Cond->getOperand(1);
752 if (!L->isLoopInvariant(RHS)) {
753 if (!L->isLoopInvariant(LHS))
754 return true;
755 std::swap(LHS, RHS);
756 }
757 // Look for a simple IV counter LHS
759 if (!Phi)
760 Phi = getLoopPhiForCounter(LHS, L);
761
762 if (!Phi)
763 return true;
764
765 // Do LFTR if PHI node is defined in the loop, but is *not* a counter.
766 int Idx = Phi->getBasicBlockIndex(L->getLoopLatch());
767 if (Idx < 0)
768 return true;
769
770 // Do LFTR if the exit condition's IV is *not* a simple counter.
771 Value *IncV = Phi->getIncomingValue(Idx);
772 return Phi != getLoopPhiForCounter(IncV, L);
773}
774
775/// Recursive helper for hasConcreteDef(). Unfortunately, this currently boils
776/// down to checking that all operands are constant and listing instructions
777/// that may hide undef.
779 unsigned Depth) {
780 if (isa<Constant>(V))
781 return !isa<UndefValue>(V);
782
783 if (Depth >= 6)
784 return false;
785
786 // Conservatively handle non-constant non-instructions. For example, Arguments
787 // may be undef.
789 if (!I)
790 return false;
791
792 // Load and return values may be undef.
793 if(I->mayReadFromMemory() || isa<CallInst>(I) || isa<InvokeInst>(I))
794 return false;
795
796 // Optimistically handle other instructions.
797 for (Value *Op : I->operands()) {
798 if (!Visited.insert(Op).second)
799 continue;
800 if (!hasConcreteDefImpl(Op, Visited, Depth+1))
801 return false;
802 }
803 return true;
804}
805
806/// Return true if the given value is concrete. We must prove that undef can
807/// never reach it.
808///
809/// TODO: If we decide that this is a good approach to checking for undef, we
810/// may factor it into a common location.
811static bool hasConcreteDef(Value *V) {
813 Visited.insert(V);
814 return hasConcreteDefImpl(V, Visited, 0);
815}
816
817/// Return true if the given phi is a "counter" in L. A counter is an
818/// add recurance (of integer or pointer type) with an arbitrary start, and a
819/// step of 1. Note that L must have exactly one latch.
820static bool isLoopCounter(PHINode* Phi, Loop *L,
821 ScalarEvolution *SE) {
822 assert(Phi->getParent() == L->getHeader());
823 assert(L->getLoopLatch());
824
825 if (!SE->isSCEVable(Phi->getType()))
826 return false;
827
828 const SCEV *S = SE->getSCEV(Phi);
830 return false;
831
832 int LatchIdx = Phi->getBasicBlockIndex(L->getLoopLatch());
833 Value *IncV = Phi->getIncomingValue(LatchIdx);
834 return (getLoopPhiForCounter(IncV, L) == Phi &&
835 isa<SCEVAddRecExpr>(SE->getSCEV(IncV)));
836}
837
838/// Search the loop header for a loop counter (anadd rec w/step of one)
839/// suitable for use by LFTR. If multiple counters are available, select the
840/// "best" one based profitable heuristics.
841///
842/// BECount may be an i8* pointer type. The pointer difference is already
843/// valid count without scaling the address stride, so it remains a pointer
844/// expression as far as SCEV is concerned.
845static PHINode *FindLoopCounter(Loop *L, BasicBlock *ExitingBB,
846 const SCEV *BECount,
848 uint64_t BCWidth = SE->getTypeSizeInBits(BECount->getType());
849
850 Value *Cond = cast<BranchInst>(ExitingBB->getTerminator())->getCondition();
851
852 // Loop over all of the PHI nodes, looking for a simple counter.
853 PHINode *BestPhi = nullptr;
854 const SCEV *BestInit = nullptr;
855 BasicBlock *LatchBlock = L->getLoopLatch();
856 assert(LatchBlock && "Must be in simplified form");
857 const DataLayout &DL = L->getHeader()->getDataLayout();
858
859 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
860 PHINode *Phi = cast<PHINode>(I);
861 if (!isLoopCounter(Phi, L, SE))
862 continue;
863
864 const auto *AR = cast<SCEVAddRecExpr>(SE->getSCEV(Phi));
865
866 // AR may be a pointer type, while BECount is an integer type.
867 // AR may be wider than BECount. With eq/ne tests overflow is immaterial.
868 // AR may not be a narrower type, or we may never exit.
869 uint64_t PhiWidth = SE->getTypeSizeInBits(AR->getType());
870 if (PhiWidth < BCWidth || !DL.isLegalInteger(PhiWidth))
871 continue;
872
873 // Avoid reusing a potentially undef value to compute other values that may
874 // have originally had a concrete definition.
875 if (!hasConcreteDef(Phi)) {
876 // We explicitly allow unknown phis as long as they are already used by
877 // the loop exit test. This is legal since performing LFTR could not
878 // increase the number of undef users.
879 Value *IncPhi = Phi->getIncomingValueForBlock(LatchBlock);
880 if (!isLoopExitTestBasedOn(Phi, ExitingBB) &&
881 !isLoopExitTestBasedOn(IncPhi, ExitingBB))
882 continue;
883 }
884
885 // Avoid introducing undefined behavior due to poison which didn't exist in
886 // the original program. (Annoyingly, the rules for poison and undef
887 // propagation are distinct, so this does NOT cover the undef case above.)
888 // We have to ensure that we don't introduce UB by introducing a use on an
889 // iteration where said IV produces poison. Our strategy here differs for
890 // pointers and integer IVs. For integers, we strip and reinfer as needed,
891 // see code in linearFunctionTestReplace. For pointers, we restrict
892 // transforms as there is no good way to reinfer inbounds once lost.
893 if (!Phi->getType()->isIntegerTy() &&
894 !mustExecuteUBIfPoisonOnPathTo(Phi, ExitingBB->getTerminator(), DT))
895 continue;
896
897 const SCEV *Init = AR->getStart();
898
899 if (BestPhi && !isAlmostDeadIV(BestPhi, LatchBlock, Cond)) {
900 // Don't force a live loop counter if another IV can be used.
901 if (isAlmostDeadIV(Phi, LatchBlock, Cond))
902 continue;
903
904 // Prefer to count-from-zero. This is a more "canonical" counter form. It
905 // also prefers integer to pointer IVs.
906 if (BestInit->isZero() != Init->isZero()) {
907 if (BestInit->isZero())
908 continue;
909 }
910 // If two IVs both count from zero or both count from nonzero then the
911 // narrower is likely a dead phi that has been widened. Use the wider phi
912 // to allow the other to be eliminated.
913 else if (PhiWidth <= SE->getTypeSizeInBits(BestPhi->getType()))
914 continue;
915 }
916 BestPhi = Phi;
917 BestInit = Init;
918 }
919 return BestPhi;
920}
921
922/// Insert an IR expression which computes the value held by the IV IndVar
923/// (which must be an loop counter w/unit stride) after the backedge of loop L
924/// is taken ExitCount times.
925static Value *genLoopLimit(PHINode *IndVar, BasicBlock *ExitingBB,
926 const SCEV *ExitCount, bool UsePostInc, Loop *L,
927 SCEVExpander &Rewriter, ScalarEvolution *SE) {
928 assert(isLoopCounter(IndVar, L, SE));
929 assert(ExitCount->getType()->isIntegerTy() && "exit count must be integer");
930 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV(IndVar));
931 assert(AR->getStepRecurrence(*SE)->isOne() && "only handles unit stride");
932
933 // For integer IVs, truncate the IV before computing the limit unless we
934 // know apriori that the limit must be a constant when evaluated in the
935 // bitwidth of the IV. We prefer (potentially) keeping a truncate of the
936 // IV in the loop over a (potentially) expensive expansion of the widened
937 // exit count add(zext(add)) expression.
938 if (IndVar->getType()->isIntegerTy() &&
939 SE->getTypeSizeInBits(AR->getType()) >
940 SE->getTypeSizeInBits(ExitCount->getType())) {
941 const SCEV *IVInit = AR->getStart();
942 if (!isa<SCEVConstant>(IVInit) || !isa<SCEVConstant>(ExitCount))
943 AR = cast<SCEVAddRecExpr>(SE->getTruncateExpr(AR, ExitCount->getType()));
944 }
945
946 const SCEVAddRecExpr *ARBase = UsePostInc ? AR->getPostIncExpr(*SE) : AR;
947 const SCEV *IVLimit = ARBase->evaluateAtIteration(ExitCount, *SE);
948 assert(SE->isLoopInvariant(IVLimit, L) &&
949 "Computed iteration count is not loop invariant!");
950 return Rewriter.expandCodeFor(IVLimit, ARBase->getType(),
951 ExitingBB->getTerminator());
952}
953
954/// This method rewrites the exit condition of the loop to be a canonical !=
955/// comparison against the incremented loop induction variable. This pass is
956/// able to rewrite the exit tests of any loop where the SCEV analysis can
957/// determine a loop-invariant trip count of the loop, which is actually a much
958/// broader range than just linear tests.
959bool IndVarSimplify::
960linearFunctionTestReplace(Loop *L, BasicBlock *ExitingBB,
961 const SCEV *ExitCount,
962 PHINode *IndVar, SCEVExpander &Rewriter) {
963 assert(L->getLoopLatch() && "Loop no longer in simplified form?");
964 assert(isLoopCounter(IndVar, L, SE));
965 Instruction * const IncVar =
966 cast<Instruction>(IndVar->getIncomingValueForBlock(L->getLoopLatch()));
967
968 // Initialize CmpIndVar to the preincremented IV.
969 Value *CmpIndVar = IndVar;
970 bool UsePostInc = false;
971
972 // If the exiting block is the same as the backedge block, we prefer to
973 // compare against the post-incremented value, otherwise we must compare
974 // against the preincremented value.
975 if (ExitingBB == L->getLoopLatch()) {
976 // For pointer IVs, we chose to not strip inbounds which requires us not
977 // to add a potentially UB introducing use. We need to either a) show
978 // the loop test we're modifying is already in post-inc form, or b) show
979 // that adding a use must not introduce UB.
980 bool SafeToPostInc =
981 IndVar->getType()->isIntegerTy() ||
982 isLoopExitTestBasedOn(IncVar, ExitingBB) ||
983 mustExecuteUBIfPoisonOnPathTo(IncVar, ExitingBB->getTerminator(), DT);
984 if (SafeToPostInc) {
985 UsePostInc = true;
986 CmpIndVar = IncVar;
987 }
988 }
989
990 // It may be necessary to drop nowrap flags on the incrementing instruction
991 // if either LFTR moves from a pre-inc check to a post-inc check (in which
992 // case the increment might have previously been poison on the last iteration
993 // only) or if LFTR switches to a different IV that was previously dynamically
994 // dead (and as such may be arbitrarily poison). We remove any nowrap flags
995 // that SCEV didn't infer for the post-inc addrec (even if we use a pre-inc
996 // check), because the pre-inc addrec flags may be adopted from the original
997 // instruction, while SCEV has to explicitly prove the post-inc nowrap flags.
998 // TODO: This handling is inaccurate for one case: If we switch to a
999 // dynamically dead IV that wraps on the first loop iteration only, which is
1000 // not covered by the post-inc addrec. (If the new IV was not dynamically
1001 // dead, it could not be poison on the first iteration in the first place.)
1002 if (auto *BO = dyn_cast<BinaryOperator>(IncVar)) {
1003 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV(IncVar));
1004 if (BO->hasNoUnsignedWrap())
1005 BO->setHasNoUnsignedWrap(AR->hasNoUnsignedWrap());
1006 if (BO->hasNoSignedWrap())
1007 BO->setHasNoSignedWrap(AR->hasNoSignedWrap());
1008 }
1009
1010 Value *ExitCnt = genLoopLimit(
1011 IndVar, ExitingBB, ExitCount, UsePostInc, L, Rewriter, SE);
1012 assert(ExitCnt->getType()->isPointerTy() ==
1013 IndVar->getType()->isPointerTy() &&
1014 "genLoopLimit missed a cast");
1015
1016 // Insert a new icmp_ne or icmp_eq instruction before the branch.
1017 BranchInst *BI = cast<BranchInst>(ExitingBB->getTerminator());
1018 ICmpInst::Predicate P;
1019 if (L->contains(BI->getSuccessor(0)))
1020 P = ICmpInst::ICMP_NE;
1021 else
1022 P = ICmpInst::ICMP_EQ;
1023
1024 IRBuilder<> Builder(BI);
1025
1026 // The new loop exit condition should reuse the debug location of the
1027 // original loop exit condition.
1028 if (auto *Cond = dyn_cast<Instruction>(BI->getCondition()))
1029 Builder.SetCurrentDebugLocation(Cond->getDebugLoc());
1030
1031 // For integer IVs, if we evaluated the limit in the narrower bitwidth to
1032 // avoid the expensive expansion of the limit expression in the wider type,
1033 // emit a truncate to narrow the IV to the ExitCount type. This is safe
1034 // since we know (from the exit count bitwidth), that we can't self-wrap in
1035 // the narrower type.
1036 unsigned CmpIndVarSize = SE->getTypeSizeInBits(CmpIndVar->getType());
1037 unsigned ExitCntSize = SE->getTypeSizeInBits(ExitCnt->getType());
1038 if (CmpIndVarSize > ExitCntSize) {
1039 assert(!CmpIndVar->getType()->isPointerTy() &&
1040 !ExitCnt->getType()->isPointerTy());
1041
1042 // Before resorting to actually inserting the truncate, use the same
1043 // reasoning as from SimplifyIndvar::eliminateTrunc to see if we can extend
1044 // the other side of the comparison instead. We still evaluate the limit
1045 // in the narrower bitwidth, we just prefer a zext/sext outside the loop to
1046 // a truncate within in.
1047 bool Extended = false;
1048 const SCEV *IV = SE->getSCEV(CmpIndVar);
1049 const SCEV *TruncatedIV = SE->getTruncateExpr(IV, ExitCnt->getType());
1050 const SCEV *ZExtTrunc =
1051 SE->getZeroExtendExpr(TruncatedIV, CmpIndVar->getType());
1052
1053 if (ZExtTrunc == IV) {
1054 Extended = true;
1055 ExitCnt = Builder.CreateZExt(ExitCnt, IndVar->getType(),
1056 "wide.trip.count");
1057 } else {
1058 const SCEV *SExtTrunc =
1059 SE->getSignExtendExpr(TruncatedIV, CmpIndVar->getType());
1060 if (SExtTrunc == IV) {
1061 Extended = true;
1062 ExitCnt = Builder.CreateSExt(ExitCnt, IndVar->getType(),
1063 "wide.trip.count");
1064 }
1065 }
1066
1067 if (Extended) {
1068 bool Discard;
1069 L->makeLoopInvariant(ExitCnt, Discard);
1070 } else
1071 CmpIndVar = Builder.CreateTrunc(CmpIndVar, ExitCnt->getType(),
1072 "lftr.wideiv");
1073 }
1074 LLVM_DEBUG(dbgs() << "INDVARS: Rewriting loop exit condition to:\n"
1075 << " LHS:" << *CmpIndVar << '\n'
1076 << " op:\t" << (P == ICmpInst::ICMP_NE ? "!=" : "==")
1077 << "\n"
1078 << " RHS:\t" << *ExitCnt << "\n"
1079 << "ExitCount:\t" << *ExitCount << "\n"
1080 << " was: " << *BI->getCondition() << "\n");
1081
1082 Value *Cond = Builder.CreateICmp(P, CmpIndVar, ExitCnt, "exitcond");
1083 Value *OrigCond = BI->getCondition();
1084 // It's tempting to use replaceAllUsesWith here to fully replace the old
1085 // comparison, but that's not immediately safe, since users of the old
1086 // comparison may not be dominated by the new comparison. Instead, just
1087 // update the branch to use the new comparison; in the common case this
1088 // will make old comparison dead.
1089 BI->setCondition(Cond);
1090 DeadInsts.emplace_back(OrigCond);
1091
1092 ++NumLFTR;
1093 return true;
1094}
1095
1096//===----------------------------------------------------------------------===//
1097// sinkUnusedInvariants. A late subpass to cleanup loop preheaders.
1098//===----------------------------------------------------------------------===//
1099
1100/// If there's a single exit block, sink any loop-invariant values that
1101/// were defined in the preheader but not used inside the loop into the
1102/// exit block to reduce register pressure in the loop.
1103bool IndVarSimplify::sinkUnusedInvariants(Loop *L) {
1104 BasicBlock *ExitBlock = L->getExitBlock();
1105 if (!ExitBlock) return false;
1106
1107 BasicBlock *Preheader = L->getLoopPreheader();
1108 if (!Preheader) return false;
1109
1110 bool MadeAnyChanges = false;
1111 for (Instruction &I : llvm::make_early_inc_range(llvm::reverse(*Preheader))) {
1112
1113 // Skip BB Terminator.
1114 if (Preheader->getTerminator() == &I)
1115 continue;
1116
1117 // New instructions were inserted at the end of the preheader.
1118 if (isa<PHINode>(I))
1119 break;
1120
1121 // Don't move instructions which might have side effects, since the side
1122 // effects need to complete before instructions inside the loop. Also don't
1123 // move instructions which might read memory, since the loop may modify
1124 // memory. Note that it's okay if the instruction might have undefined
1125 // behavior: LoopSimplify guarantees that the preheader dominates the exit
1126 // block.
1127 if (I.mayHaveSideEffects() || I.mayReadFromMemory())
1128 continue;
1129
1130 // Skip debug or pseudo instructions.
1131 if (I.isDebugOrPseudoInst())
1132 continue;
1133
1134 // Skip eh pad instructions.
1135 if (I.isEHPad())
1136 continue;
1137
1138 // Don't sink alloca: we never want to sink static alloca's out of the
1139 // entry block, and correctly sinking dynamic alloca's requires
1140 // checks for stacksave/stackrestore intrinsics.
1141 // FIXME: Refactor this check somehow?
1142 if (isa<AllocaInst>(&I))
1143 continue;
1144
1145 // Determine if there is a use in or before the loop (direct or
1146 // otherwise).
1147 bool UsedInLoop = false;
1148 for (Use &U : I.uses()) {
1149 Instruction *User = cast<Instruction>(U.getUser());
1150 BasicBlock *UseBB = User->getParent();
1151 if (PHINode *P = dyn_cast<PHINode>(User)) {
1152 unsigned i =
1154 UseBB = P->getIncomingBlock(i);
1155 }
1156 if (UseBB == Preheader || L->contains(UseBB)) {
1157 UsedInLoop = true;
1158 break;
1159 }
1160 }
1161
1162 // If there is, the def must remain in the preheader.
1163 if (UsedInLoop)
1164 continue;
1165
1166 // Otherwise, sink it to the exit block.
1167 I.moveBefore(ExitBlock->getFirstInsertionPt());
1168 SE->forgetValue(&I);
1169 MadeAnyChanges = true;
1170 }
1171
1172 return MadeAnyChanges;
1173}
1174
1175static void replaceExitCond(BranchInst *BI, Value *NewCond,
1177 auto *OldCond = BI->getCondition();
1178 LLVM_DEBUG(dbgs() << "Replacing condition of loop-exiting branch " << *BI
1179 << " with " << *NewCond << "\n");
1180 BI->setCondition(NewCond);
1181 if (OldCond->use_empty())
1182 DeadInsts.emplace_back(OldCond);
1183}
1184
1185static Constant *createFoldedExitCond(const Loop *L, BasicBlock *ExitingBB,
1186 bool IsTaken) {
1187 BranchInst *BI = cast<BranchInst>(ExitingBB->getTerminator());
1188 bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB));
1189 auto *OldCond = BI->getCondition();
1190 return ConstantInt::get(OldCond->getType(),
1191 IsTaken ? ExitIfTrue : !ExitIfTrue);
1192}
1193
1194static void foldExit(const Loop *L, BasicBlock *ExitingBB, bool IsTaken,
1196 BranchInst *BI = cast<BranchInst>(ExitingBB->getTerminator());
1197 auto *NewCond = createFoldedExitCond(L, ExitingBB, IsTaken);
1198 replaceExitCond(BI, NewCond, DeadInsts);
1199}
1200
1202 LoopInfo *LI, Loop *L, SmallVectorImpl<WeakTrackingVH> &DeadInsts,
1203 ScalarEvolution &SE) {
1204 assert(L->isLoopSimplifyForm() && "Should only do it in simplify form!");
1205 auto *LoopPreheader = L->getLoopPreheader();
1206 auto *LoopHeader = L->getHeader();
1208 for (auto &PN : LoopHeader->phis()) {
1209 auto *PreheaderIncoming = PN.getIncomingValueForBlock(LoopPreheader);
1210 for (User *U : PN.users())
1211 Worklist.push_back(cast<Instruction>(U));
1212 SE.forgetValue(&PN);
1213 PN.replaceAllUsesWith(PreheaderIncoming);
1214 DeadInsts.emplace_back(&PN);
1215 }
1216
1217 // Replacing with the preheader value will often allow IV users to simplify
1218 // (especially if the preheader value is a constant).
1220 while (!Worklist.empty()) {
1221 auto *I = cast<Instruction>(Worklist.pop_back_val());
1222 if (!Visited.insert(I).second)
1223 continue;
1224
1225 // Don't simplify instructions outside the loop.
1226 if (!L->contains(I))
1227 continue;
1228
1229 Value *Res = simplifyInstruction(I, I->getDataLayout());
1230 if (Res && LI->replacementPreservesLCSSAForm(I, Res)) {
1231 for (User *U : I->users())
1232 Worklist.push_back(cast<Instruction>(U));
1233 I->replaceAllUsesWith(Res);
1234 DeadInsts.emplace_back(I);
1235 }
1236 }
1237}
1238
1239static Value *
1242 SCEVExpander &Rewriter) {
1243 ICmpInst::Predicate InvariantPred = LIP.Pred;
1244 BasicBlock *Preheader = L->getLoopPreheader();
1245 assert(Preheader && "Preheader doesn't exist");
1246 Rewriter.setInsertPoint(Preheader->getTerminator());
1247 auto *LHSV = Rewriter.expandCodeFor(LIP.LHS);
1248 auto *RHSV = Rewriter.expandCodeFor(LIP.RHS);
1249 bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB));
1250 if (ExitIfTrue)
1251 InvariantPred = ICmpInst::getInversePredicate(InvariantPred);
1252 IRBuilder<> Builder(Preheader->getTerminator());
1253 BranchInst *BI = cast<BranchInst>(ExitingBB->getTerminator());
1254 return Builder.CreateICmp(InvariantPred, LHSV, RHSV,
1255 BI->getCondition()->getName());
1256}
1257
1258static std::optional<Value *>
1259createReplacement(ICmpInst *ICmp, const Loop *L, BasicBlock *ExitingBB,
1260 const SCEV *MaxIter, bool Inverted, bool SkipLastIter,
1261 ScalarEvolution *SE, SCEVExpander &Rewriter) {
1262 CmpPredicate Pred = ICmp->getCmpPredicate();
1263 Value *LHS = ICmp->getOperand(0);
1264 Value *RHS = ICmp->getOperand(1);
1265
1266 // 'LHS pred RHS' should now mean that we stay in loop.
1267 auto *BI = cast<BranchInst>(ExitingBB->getTerminator());
1268 if (Inverted)
1270
1271 const SCEV *LHSS = SE->getSCEVAtScope(LHS, L);
1272 const SCEV *RHSS = SE->getSCEVAtScope(RHS, L);
1273 // Can we prove it to be trivially true or false?
1274 if (auto EV = SE->evaluatePredicateAt(Pred, LHSS, RHSS, BI))
1275 return createFoldedExitCond(L, ExitingBB, /*IsTaken*/ !*EV);
1276
1277 auto *ARTy = LHSS->getType();
1278 auto *MaxIterTy = MaxIter->getType();
1279 // If possible, adjust types.
1280 if (SE->getTypeSizeInBits(ARTy) > SE->getTypeSizeInBits(MaxIterTy))
1281 MaxIter = SE->getZeroExtendExpr(MaxIter, ARTy);
1282 else if (SE->getTypeSizeInBits(ARTy) < SE->getTypeSizeInBits(MaxIterTy)) {
1283 const SCEV *MinusOne = SE->getMinusOne(ARTy);
1284 const SCEV *MaxAllowedIter = SE->getZeroExtendExpr(MinusOne, MaxIterTy);
1285 if (SE->isKnownPredicateAt(ICmpInst::ICMP_ULE, MaxIter, MaxAllowedIter, BI))
1286 MaxIter = SE->getTruncateExpr(MaxIter, ARTy);
1287 }
1288
1289 if (SkipLastIter) {
1290 // Semantically skip last iter is "subtract 1, do not bother about unsigned
1291 // wrap". getLoopInvariantExitCondDuringFirstIterations knows how to deal
1292 // with umin in a smart way, but umin(a, b) - 1 will likely not simplify.
1293 // So we manually construct umin(a - 1, b - 1).
1295 if (auto *UMin = dyn_cast<SCEVUMinExpr>(MaxIter)) {
1296 for (const SCEV *Op : UMin->operands())
1297 Elements.push_back(SE->getMinusSCEV(Op, SE->getOne(Op->getType())));
1298 MaxIter = SE->getUMinFromMismatchedTypes(Elements);
1299 } else
1300 MaxIter = SE->getMinusSCEV(MaxIter, SE->getOne(MaxIter->getType()));
1301 }
1302
1303 // Check if there is a loop-invariant predicate equivalent to our check.
1304 auto LIP = SE->getLoopInvariantExitCondDuringFirstIterations(Pred, LHSS, RHSS,
1305 L, BI, MaxIter);
1306 if (!LIP)
1307 return std::nullopt;
1308
1309 // Can we prove it to be trivially true?
1310 if (SE->isKnownPredicateAt(LIP->Pred, LIP->LHS, LIP->RHS, BI))
1311 return createFoldedExitCond(L, ExitingBB, /*IsTaken*/ false);
1312 else
1313 return createInvariantCond(L, ExitingBB, *LIP, Rewriter);
1314}
1315
1317 const Loop *L, BranchInst *BI, BasicBlock *ExitingBB, const SCEV *MaxIter,
1318 bool SkipLastIter, ScalarEvolution *SE, SCEVExpander &Rewriter,
1320 assert(
1321 (L->contains(BI->getSuccessor(0)) != L->contains(BI->getSuccessor(1))) &&
1322 "Not a loop exit!");
1323
1324 // For branch that stays in loop by TRUE condition, go through AND. For branch
1325 // that stays in loop by FALSE condition, go through OR. Both gives the
1326 // similar logic: "stay in loop iff all conditions are true(false)".
1327 bool Inverted = L->contains(BI->getSuccessor(1));
1328 SmallVector<ICmpInst *, 4> LeafConditions;
1329 SmallVector<Value *, 4> Worklist;
1331 Value *OldCond = BI->getCondition();
1332 Visited.insert(OldCond);
1333 Worklist.push_back(OldCond);
1334
1335 auto GoThrough = [&](Value *V) {
1336 Value *LHS = nullptr, *RHS = nullptr;
1337 if (Inverted) {
1338 if (!match(V, m_LogicalOr(m_Value(LHS), m_Value(RHS))))
1339 return false;
1340 } else {
1341 if (!match(V, m_LogicalAnd(m_Value(LHS), m_Value(RHS))))
1342 return false;
1343 }
1344 if (Visited.insert(LHS).second)
1345 Worklist.push_back(LHS);
1346 if (Visited.insert(RHS).second)
1347 Worklist.push_back(RHS);
1348 return true;
1349 };
1350
1351 do {
1352 Value *Curr = Worklist.pop_back_val();
1353 // Go through AND/OR conditions. Collect leaf ICMPs. We only care about
1354 // those with one use, to avoid instruction duplication.
1355 if (Curr->hasOneUse())
1356 if (!GoThrough(Curr))
1357 if (auto *ICmp = dyn_cast<ICmpInst>(Curr))
1358 LeafConditions.push_back(ICmp);
1359 } while (!Worklist.empty());
1360
1361 // If the current basic block has the same exit count as the whole loop, and
1362 // it consists of multiple icmp's, try to collect all icmp's that give exact
1363 // same exit count. For all other icmp's, we could use one less iteration,
1364 // because their value on the last iteration doesn't really matter.
1365 SmallPtrSet<ICmpInst *, 4> ICmpsFailingOnLastIter;
1366 if (!SkipLastIter && LeafConditions.size() > 1 &&
1367 SE->getExitCount(L, ExitingBB,
1369 MaxIter)
1370 for (auto *ICmp : LeafConditions) {
1371 auto EL = SE->computeExitLimitFromCond(L, ICmp, Inverted,
1372 /*ControlsExit*/ false);
1373 const SCEV *ExitMax = EL.SymbolicMaxNotTaken;
1374 if (isa<SCEVCouldNotCompute>(ExitMax))
1375 continue;
1376 // They could be of different types (specifically this happens after
1377 // IV widening).
1378 auto *WiderType =
1379 SE->getWiderType(ExitMax->getType(), MaxIter->getType());
1380 const SCEV *WideExitMax = SE->getNoopOrZeroExtend(ExitMax, WiderType);
1381 const SCEV *WideMaxIter = SE->getNoopOrZeroExtend(MaxIter, WiderType);
1382 if (WideExitMax == WideMaxIter)
1383 ICmpsFailingOnLastIter.insert(ICmp);
1384 }
1385
1386 bool Changed = false;
1387 for (auto *OldCond : LeafConditions) {
1388 // Skip last iteration for this icmp under one of two conditions:
1389 // - We do it for all conditions;
1390 // - There is another ICmp that would fail on last iter, so this one doesn't
1391 // really matter.
1392 bool OptimisticSkipLastIter = SkipLastIter;
1393 if (!OptimisticSkipLastIter) {
1394 if (ICmpsFailingOnLastIter.size() > 1)
1395 OptimisticSkipLastIter = true;
1396 else if (ICmpsFailingOnLastIter.size() == 1)
1397 OptimisticSkipLastIter = !ICmpsFailingOnLastIter.count(OldCond);
1398 }
1399 if (auto Replaced =
1400 createReplacement(OldCond, L, ExitingBB, MaxIter, Inverted,
1401 OptimisticSkipLastIter, SE, Rewriter)) {
1402 Changed = true;
1403 auto *NewCond = *Replaced;
1404 if (auto *NCI = dyn_cast<Instruction>(NewCond)) {
1405 NCI->setName(OldCond->getName() + ".first_iter");
1406 }
1407 LLVM_DEBUG(dbgs() << "Unknown exit count: Replacing " << *OldCond
1408 << " with " << *NewCond << "\n");
1409 assert(OldCond->hasOneUse() && "Must be!");
1410 OldCond->replaceAllUsesWith(NewCond);
1411 DeadInsts.push_back(OldCond);
1412 // Make sure we no longer consider this condition as failing on last
1413 // iteration.
1414 ICmpsFailingOnLastIter.erase(OldCond);
1415 }
1416 }
1417 return Changed;
1418}
1419
1420bool IndVarSimplify::canonicalizeExitCondition(Loop *L) {
1421 // Note: This is duplicating a particular part on SimplifyIndVars reasoning.
1422 // We need to duplicate it because given icmp zext(small-iv), C, IVUsers
1423 // never reaches the icmp since the zext doesn't fold to an AddRec unless
1424 // it already has flags. The alternative to this would be to extending the
1425 // set of "interesting" IV users to include the icmp, but doing that
1426 // regresses results in practice by querying SCEVs before trip counts which
1427 // rely on them which results in SCEV caching sub-optimal answers. The
1428 // concern about caching sub-optimal results is why we only query SCEVs of
1429 // the loop invariant RHS here.
1430 SmallVector<BasicBlock*, 16> ExitingBlocks;
1431 L->getExitingBlocks(ExitingBlocks);
1432 bool Changed = false;
1433 for (auto *ExitingBB : ExitingBlocks) {
1434 auto *BI = dyn_cast<BranchInst>(ExitingBB->getTerminator());
1435 if (!BI)
1436 continue;
1437 assert(BI->isConditional() && "exit branch must be conditional");
1438
1439 auto *ICmp = dyn_cast<ICmpInst>(BI->getCondition());
1440 if (!ICmp || !ICmp->hasOneUse())
1441 continue;
1442
1443 auto *LHS = ICmp->getOperand(0);
1444 auto *RHS = ICmp->getOperand(1);
1445 // For the range reasoning, avoid computing SCEVs in the loop to avoid
1446 // poisoning cache with sub-optimal results. For the must-execute case,
1447 // this is a neccessary precondition for correctness.
1448 if (!L->isLoopInvariant(RHS)) {
1449 if (!L->isLoopInvariant(LHS))
1450 continue;
1451 // Same logic applies for the inverse case
1452 std::swap(LHS, RHS);
1453 }
1454
1455 // Match (icmp signed-cond zext, RHS)
1456 Value *LHSOp = nullptr;
1457 if (!match(LHS, m_ZExt(m_Value(LHSOp))) || !ICmp->isSigned())
1458 continue;
1459
1460 const unsigned InnerBitWidth = DL.getTypeSizeInBits(LHSOp->getType());
1461 const unsigned OuterBitWidth = DL.getTypeSizeInBits(RHS->getType());
1462 auto FullCR = ConstantRange::getFull(InnerBitWidth);
1463 FullCR = FullCR.zeroExtend(OuterBitWidth);
1464 auto RHSCR = SE->getUnsignedRange(SE->applyLoopGuards(SE->getSCEV(RHS), L));
1465 if (FullCR.contains(RHSCR)) {
1466 // We have now matched icmp signed-cond zext(X), zext(Y'), and can thus
1467 // replace the signed condition with the unsigned version.
1468 ICmp->setPredicate(ICmp->getUnsignedPredicate());
1469 Changed = true;
1470 // Note: No SCEV invalidation needed. We've changed the predicate, but
1471 // have not changed exit counts, or the values produced by the compare.
1472 continue;
1473 }
1474 }
1475
1476 // Now that we've canonicalized the condition to match the extend,
1477 // see if we can rotate the extend out of the loop.
1478 for (auto *ExitingBB : ExitingBlocks) {
1479 auto *BI = dyn_cast<BranchInst>(ExitingBB->getTerminator());
1480 if (!BI)
1481 continue;
1482 assert(BI->isConditional() && "exit branch must be conditional");
1483
1484 auto *ICmp = dyn_cast<ICmpInst>(BI->getCondition());
1485 if (!ICmp || !ICmp->hasOneUse() || !ICmp->isUnsigned())
1486 continue;
1487
1488 bool Swapped = false;
1489 auto *LHS = ICmp->getOperand(0);
1490 auto *RHS = ICmp->getOperand(1);
1491 if (L->isLoopInvariant(LHS) == L->isLoopInvariant(RHS))
1492 // Nothing to rotate
1493 continue;
1494 if (L->isLoopInvariant(LHS)) {
1495 // Same logic applies for the inverse case until we actually pick
1496 // which operand of the compare to update.
1497 Swapped = true;
1498 std::swap(LHS, RHS);
1499 }
1500 assert(!L->isLoopInvariant(LHS) && L->isLoopInvariant(RHS));
1501
1502 // Match (icmp unsigned-cond zext, RHS)
1503 // TODO: Extend to handle corresponding sext/signed-cmp case
1504 // TODO: Extend to other invertible functions
1505 Value *LHSOp = nullptr;
1506 if (!match(LHS, m_ZExt(m_Value(LHSOp))))
1507 continue;
1508
1509 // In general, we only rotate if we can do so without increasing the number
1510 // of instructions. The exception is when we have an zext(add-rec). The
1511 // reason for allowing this exception is that we know we need to get rid
1512 // of the zext for SCEV to be able to compute a trip count for said loops;
1513 // we consider the new trip count valuable enough to increase instruction
1514 // count by one.
1515 if (!LHS->hasOneUse() && !isa<SCEVAddRecExpr>(SE->getSCEV(LHSOp)))
1516 continue;
1517
1518 // Given a icmp unsigned-cond zext(Op) where zext(trunc(RHS)) == RHS
1519 // replace with an icmp of the form icmp unsigned-cond Op, trunc(RHS)
1520 // when zext is loop varying and RHS is loop invariant. This converts
1521 // loop varying work to loop-invariant work.
1522 auto doRotateTransform = [&]() {
1523 assert(ICmp->isUnsigned() && "must have proven unsigned already");
1524 auto *NewRHS = CastInst::Create(
1525 Instruction::Trunc, RHS, LHSOp->getType(), "",
1526 L->getLoopPreheader()->getTerminator()->getIterator());
1527 // NewRHS is an operation that has been hoisted out of the loop, and
1528 // therefore should have a dropped location.
1529 NewRHS->setDebugLoc(DebugLoc::getDropped());
1530 ICmp->setOperand(Swapped ? 1 : 0, LHSOp);
1531 ICmp->setOperand(Swapped ? 0 : 1, NewRHS);
1532 // Samesign flag cannot be preserved after narrowing the compare.
1533 ICmp->setSameSign(false);
1534 if (LHS->use_empty())
1535 DeadInsts.push_back(LHS);
1536 };
1537
1538 const unsigned InnerBitWidth = DL.getTypeSizeInBits(LHSOp->getType());
1539 const unsigned OuterBitWidth = DL.getTypeSizeInBits(RHS->getType());
1540 auto FullCR = ConstantRange::getFull(InnerBitWidth);
1541 FullCR = FullCR.zeroExtend(OuterBitWidth);
1542 auto RHSCR = SE->getUnsignedRange(SE->applyLoopGuards(SE->getSCEV(RHS), L));
1543 if (FullCR.contains(RHSCR)) {
1544 doRotateTransform();
1545 Changed = true;
1546 // Note, we are leaving SCEV in an unfortunately imprecise case here
1547 // as rotation tends to reveal information about trip counts not
1548 // previously visible.
1549 continue;
1550 }
1551 }
1552
1553 return Changed;
1554}
1555
1556bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) {
1557 SmallVector<BasicBlock*, 16> ExitingBlocks;
1558 L->getExitingBlocks(ExitingBlocks);
1559
1560 // Remove all exits which aren't both rewriteable and execute on every
1561 // iteration.
1562 llvm::erase_if(ExitingBlocks, [&](BasicBlock *ExitingBB) {
1563 // If our exitting block exits multiple loops, we can only rewrite the
1564 // innermost one. Otherwise, we're changing how many times the innermost
1565 // loop runs before it exits.
1566 if (LI->getLoopFor(ExitingBB) != L)
1567 return true;
1568
1569 // Can't rewrite non-branch yet.
1570 BranchInst *BI = dyn_cast<BranchInst>(ExitingBB->getTerminator());
1571 if (!BI)
1572 return true;
1573
1574 // Likewise, the loop latch must be dominated by the exiting BB.
1575 if (!DT->dominates(ExitingBB, L->getLoopLatch()))
1576 return true;
1577
1578 if (auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) {
1579 // If already constant, nothing to do. However, if this is an
1580 // unconditional exit, we can still replace header phis with their
1581 // preheader value.
1582 if (!L->contains(BI->getSuccessor(CI->isNullValue())))
1583 replaceLoopPHINodesWithPreheaderValues(LI, L, DeadInsts, *SE);
1584 return true;
1585 }
1586
1587 return false;
1588 });
1589
1590 if (ExitingBlocks.empty())
1591 return false;
1592
1593 // Get a symbolic upper bound on the loop backedge taken count.
1594 const SCEV *MaxBECount = SE->getSymbolicMaxBackedgeTakenCount(L);
1595 if (isa<SCEVCouldNotCompute>(MaxBECount))
1596 return false;
1597
1598 // Visit our exit blocks in order of dominance. We know from the fact that
1599 // all exits must dominate the latch, so there is a total dominance order
1600 // between them.
1601 llvm::sort(ExitingBlocks, [&](BasicBlock *A, BasicBlock *B) {
1602 // std::sort sorts in ascending order, so we want the inverse of
1603 // the normal dominance relation.
1604 if (A == B) return false;
1605 if (DT->properlyDominates(A, B))
1606 return true;
1607 else {
1608 assert(DT->properlyDominates(B, A) &&
1609 "expected total dominance order!");
1610 return false;
1611 }
1612 });
1613#ifdef ASSERT
1614 for (unsigned i = 1; i < ExitingBlocks.size(); i++) {
1615 assert(DT->dominates(ExitingBlocks[i-1], ExitingBlocks[i]));
1616 }
1617#endif
1618
1619 bool Changed = false;
1620 bool SkipLastIter = false;
1621 const SCEV *CurrMaxExit = SE->getCouldNotCompute();
1622 auto UpdateSkipLastIter = [&](const SCEV *MaxExitCount) {
1623 if (SkipLastIter || isa<SCEVCouldNotCompute>(MaxExitCount))
1624 return;
1625 if (isa<SCEVCouldNotCompute>(CurrMaxExit))
1626 CurrMaxExit = MaxExitCount;
1627 else
1628 CurrMaxExit = SE->getUMinFromMismatchedTypes(CurrMaxExit, MaxExitCount);
1629 // If the loop has more than 1 iteration, all further checks will be
1630 // executed 1 iteration less.
1631 if (CurrMaxExit == MaxBECount)
1632 SkipLastIter = true;
1633 };
1634 SmallPtrSet<const SCEV *, 8> DominatingExactExitCounts;
1635 for (BasicBlock *ExitingBB : ExitingBlocks) {
1636 const SCEV *ExactExitCount = SE->getExitCount(L, ExitingBB);
1637 const SCEV *MaxExitCount = SE->getExitCount(
1638 L, ExitingBB, ScalarEvolution::ExitCountKind::SymbolicMaximum);
1639 if (isa<SCEVCouldNotCompute>(ExactExitCount)) {
1640 // Okay, we do not know the exit count here. Can we at least prove that it
1641 // will remain the same within iteration space?
1642 auto *BI = cast<BranchInst>(ExitingBB->getTerminator());
1643 auto OptimizeCond = [&](bool SkipLastIter) {
1644 return optimizeLoopExitWithUnknownExitCount(L, BI, ExitingBB,
1645 MaxBECount, SkipLastIter,
1646 SE, Rewriter, DeadInsts);
1647 };
1648
1649 // TODO: We might have proved that we can skip the last iteration for
1650 // this check. In this case, we only want to check the condition on the
1651 // pre-last iteration (MaxBECount - 1). However, there is a nasty
1652 // corner case:
1653 //
1654 // for (i = len; i != 0; i--) { ... check (i ult X) ... }
1655 //
1656 // If we could not prove that len != 0, then we also could not prove that
1657 // (len - 1) is not a UINT_MAX. If we simply query (len - 1), then
1658 // OptimizeCond will likely not prove anything for it, even if it could
1659 // prove the same fact for len.
1660 //
1661 // As a temporary solution, we query both last and pre-last iterations in
1662 // hope that we will be able to prove triviality for at least one of
1663 // them. We can stop querying MaxBECount for this case once SCEV
1664 // understands that (MaxBECount - 1) will not overflow here.
1665 if (OptimizeCond(false))
1666 Changed = true;
1667 else if (SkipLastIter && OptimizeCond(true))
1668 Changed = true;
1669 UpdateSkipLastIter(MaxExitCount);
1670 continue;
1671 }
1672
1673 UpdateSkipLastIter(ExactExitCount);
1674
1675 // If we know we'd exit on the first iteration, rewrite the exit to
1676 // reflect this. This does not imply the loop must exit through this
1677 // exit; there may be an earlier one taken on the first iteration.
1678 // We know that the backedge can't be taken, so we replace all
1679 // the header PHIs with values coming from the preheader.
1680 if (ExactExitCount->isZero()) {
1681 foldExit(L, ExitingBB, true, DeadInsts);
1682 replaceLoopPHINodesWithPreheaderValues(LI, L, DeadInsts, *SE);
1683 Changed = true;
1684 continue;
1685 }
1686
1687 assert(ExactExitCount->getType()->isIntegerTy() &&
1688 MaxBECount->getType()->isIntegerTy() &&
1689 "Exit counts must be integers");
1690
1691 Type *WiderType =
1692 SE->getWiderType(MaxBECount->getType(), ExactExitCount->getType());
1693 ExactExitCount = SE->getNoopOrZeroExtend(ExactExitCount, WiderType);
1694 MaxBECount = SE->getNoopOrZeroExtend(MaxBECount, WiderType);
1695 assert(MaxBECount->getType() == ExactExitCount->getType());
1696
1697 // Can we prove that some other exit must be taken strictly before this
1698 // one?
1699 if (SE->isLoopEntryGuardedByCond(L, CmpInst::ICMP_ULT, MaxBECount,
1700 ExactExitCount)) {
1701 foldExit(L, ExitingBB, false, DeadInsts);
1702 Changed = true;
1703 continue;
1704 }
1705
1706 // As we run, keep track of which exit counts we've encountered. If we
1707 // find a duplicate, we've found an exit which would have exited on the
1708 // exiting iteration, but (from the visit order) strictly follows another
1709 // which does the same and is thus dead.
1710 if (!DominatingExactExitCounts.insert(ExactExitCount).second) {
1711 foldExit(L, ExitingBB, false, DeadInsts);
1712 Changed = true;
1713 continue;
1714 }
1715
1716 // TODO: There might be another oppurtunity to leverage SCEV's reasoning
1717 // here. If we kept track of the min of dominanting exits so far, we could
1718 // discharge exits with EC >= MDEC. This is less powerful than the existing
1719 // transform (since later exits aren't considered), but potentially more
1720 // powerful for any case where SCEV can prove a >=u b, but neither a == b
1721 // or a >u b. Such a case is not currently known.
1722 }
1723 return Changed;
1724}
1725
1726static bool crashingBBWithoutEffect(const BasicBlock &BB) {
1727 return llvm::all_of(BB, [](const Instruction &I) {
1728 // TODO: for now this is overly restrictive, to make sure nothing in this
1729 // BB can depend on the loop body.
1730 // It's not enough to check for !I.mayHaveSideEffects(), because e.g. a
1731 // load does not have a side effect, but we could have
1732 // %a = load ptr, ptr %ptr
1733 // %b = load i32, ptr %a
1734 // Now if the loop stored a non-nullptr to %a, we could cause a nullptr
1735 // dereference by skipping over loop iterations.
1736 if (const auto *CB = dyn_cast<CallBase>(&I)) {
1737 if (CB->onlyAccessesInaccessibleMemory())
1738 return true;
1739 }
1740 return isa<UnreachableInst>(I);
1741 });
1742}
1743
1744bool IndVarSimplify::predicateLoopExits(Loop *L, SCEVExpander &Rewriter) {
1745 SmallVector<BasicBlock*, 16> ExitingBlocks;
1746 L->getExitingBlocks(ExitingBlocks);
1747
1748 // Finally, see if we can rewrite our exit conditions into a loop invariant
1749 // form. If we have a read-only loop, and we can tell that we must exit down
1750 // a path which does not need any of the values computed within the loop, we
1751 // can rewrite the loop to exit on the first iteration. Note that this
1752 // doesn't either a) tell us the loop exits on the first iteration (unless
1753 // *all* exits are predicateable) or b) tell us *which* exit might be taken.
1754 // This transformation looks a lot like a restricted form of dead loop
1755 // elimination, but restricted to read-only loops and without neccesssarily
1756 // needing to kill the loop entirely.
1757 if (!LoopPredication)
1758 return false;
1759
1760 // Note: ExactBTC is the exact backedge taken count *iff* the loop exits
1761 // through *explicit* control flow. We have to eliminate the possibility of
1762 // implicit exits (see below) before we know it's truly exact.
1763 const SCEV *ExactBTC = SE->getBackedgeTakenCount(L);
1764 if (isa<SCEVCouldNotCompute>(ExactBTC) || !Rewriter.isSafeToExpand(ExactBTC))
1765 return false;
1766
1767 assert(SE->isLoopInvariant(ExactBTC, L) && "BTC must be loop invariant");
1768 assert(ExactBTC->getType()->isIntegerTy() && "BTC must be integer");
1769
1770 auto BadExit = [&](BasicBlock *ExitingBB) {
1771 // If our exiting block exits multiple loops, we can only rewrite the
1772 // innermost one. Otherwise, we're changing how many times the innermost
1773 // loop runs before it exits.
1774 if (LI->getLoopFor(ExitingBB) != L)
1775 return true;
1776
1777 // Can't rewrite non-branch yet.
1778 BranchInst *BI = dyn_cast<BranchInst>(ExitingBB->getTerminator());
1779 if (!BI)
1780 return true;
1781
1782 // If already constant, nothing to do.
1783 if (isa<Constant>(BI->getCondition()))
1784 return true;
1785
1786 // If the exit block has phis, we need to be able to compute the values
1787 // within the loop which contains them. This assumes trivially lcssa phis
1788 // have already been removed; TODO: generalize
1789 BasicBlock *ExitBlock =
1790 BI->getSuccessor(L->contains(BI->getSuccessor(0)) ? 1 : 0);
1791 if (!ExitBlock->phis().empty())
1792 return true;
1793
1794 const SCEV *ExitCount = SE->getExitCount(L, ExitingBB);
1795 if (isa<SCEVCouldNotCompute>(ExitCount) ||
1796 !Rewriter.isSafeToExpand(ExitCount))
1797 return true;
1798
1799 assert(SE->isLoopInvariant(ExitCount, L) &&
1800 "Exit count must be loop invariant");
1801 assert(ExitCount->getType()->isIntegerTy() && "Exit count must be integer");
1802 return false;
1803 };
1804
1805 // Make sure all exits dominate the latch. This means there is a linear chain
1806 // of exits. We check this before sorting so we have a total order.
1807 BasicBlock *Latch = L->getLoopLatch();
1808 for (BasicBlock *ExitingBB : ExitingBlocks)
1809 if (!DT->dominates(ExitingBB, Latch))
1810 return false;
1811
1812 // If we have any exits which can't be predicated themselves, than we can't
1813 // predicate any exit which isn't guaranteed to execute before it. Consider
1814 // two exits (a) and (b) which would both exit on the same iteration. If we
1815 // can predicate (b), but not (a), and (a) preceeds (b) along some path, then
1816 // we could convert a loop from exiting through (a) to one exiting through
1817 // (b). Note that this problem exists only for exits with the same exit
1818 // count, and we could be more aggressive when exit counts are known inequal.
1819 llvm::sort(ExitingBlocks, [&](BasicBlock *A, BasicBlock *B) {
1820 // llvm::sort sorts in ascending order, so we want the inverse of
1821 // the normal dominance relation.
1822 if (A == B)
1823 return false;
1824 if (DT->properlyDominates(A, B))
1825 return true;
1826 if (DT->properlyDominates(B, A))
1827 return false;
1828 llvm_unreachable("Should have total dominance order");
1829 });
1830
1831 // Make sure our exit blocks are really a total order (i.e. a linear chain of
1832 // exits before the backedge).
1833 for (unsigned i = 1; i < ExitingBlocks.size(); i++)
1834 assert(DT->dominates(ExitingBlocks[i - 1], ExitingBlocks[i]) &&
1835 "Not sorted by dominance");
1836
1837 // Given our sorted total order, we know that exit[j] must be evaluated
1838 // after all exit[i] such j > i.
1839 for (unsigned i = 0, e = ExitingBlocks.size(); i < e; i++)
1840 if (BadExit(ExitingBlocks[i])) {
1841 ExitingBlocks.resize(i);
1842 break;
1843 }
1844
1845 if (ExitingBlocks.empty())
1846 return false;
1847
1848 // At this point, ExitingBlocks consists of only those blocks which are
1849 // predicatable. Given that, we know we have at least one exit we can
1850 // predicate if the loop is doesn't have side effects and doesn't have any
1851 // implicit exits (because then our exact BTC isn't actually exact).
1852 // @Reviewers - As structured, this is O(I^2) for loop nests. Any
1853 // suggestions on how to improve this? I can obviously bail out for outer
1854 // loops, but that seems less than ideal. MemorySSA can find memory writes,
1855 // is that enough for *all* side effects?
1856 bool HasThreadLocalSideEffects = false;
1857 for (BasicBlock *BB : L->blocks())
1858 for (auto &I : *BB)
1859 // TODO:isGuaranteedToTransfer
1860 if (I.mayHaveSideEffects()) {
1862 return false;
1863 HasThreadLocalSideEffects = true;
1864 if (StoreInst *SI = dyn_cast<StoreInst>(&I)) {
1865 // Simple stores cannot be observed by other threads.
1866 // If HasThreadLocalSideEffects is set, we check
1867 // crashingBBWithoutEffect to make sure that the crashing BB cannot
1868 // observe them either.
1869 if (!SI->isSimple())
1870 return false;
1871 } else {
1872 return false;
1873 }
1874 }
1875
1876 bool Changed = false;
1877 // Finally, do the actual predication for all predicatable blocks. A couple
1878 // of notes here:
1879 // 1) We don't bother to constant fold dominated exits with identical exit
1880 // counts; that's simply a form of CSE/equality propagation and we leave
1881 // it for dedicated passes.
1882 // 2) We insert the comparison at the branch. Hoisting introduces additional
1883 // legality constraints and we leave that to dedicated logic. We want to
1884 // predicate even if we can't insert a loop invariant expression as
1885 // peeling or unrolling will likely reduce the cost of the otherwise loop
1886 // varying check.
1887 Rewriter.setInsertPoint(L->getLoopPreheader()->getTerminator());
1888 IRBuilder<> B(L->getLoopPreheader()->getTerminator());
1889 Value *ExactBTCV = nullptr; // Lazily generated if needed.
1890 for (BasicBlock *ExitingBB : ExitingBlocks) {
1891 const SCEV *ExitCount = SE->getExitCount(L, ExitingBB);
1892
1893 auto *BI = cast<BranchInst>(ExitingBB->getTerminator());
1894 if (HasThreadLocalSideEffects) {
1895 const BasicBlock *Unreachable = nullptr;
1896 for (const BasicBlock *Succ : BI->successors()) {
1897 if (isa<UnreachableInst>(Succ->getTerminator()))
1898 Unreachable = Succ;
1899 }
1900 // Exit BB which have one branch back into the loop and another one to
1901 // a trap can still be optimized, because local side effects cannot
1902 // be observed in the exit case (the trap). We could be smarter about
1903 // this, but for now lets pattern match common cases that directly trap.
1904 if (Unreachable == nullptr || !crashingBBWithoutEffect(*Unreachable))
1905 return Changed;
1906 }
1907 Value *NewCond;
1908 if (ExitCount == ExactBTC) {
1909 NewCond = L->contains(BI->getSuccessor(0)) ?
1910 B.getFalse() : B.getTrue();
1911 } else {
1912 Value *ECV = Rewriter.expandCodeFor(ExitCount);
1913 if (!ExactBTCV)
1914 ExactBTCV = Rewriter.expandCodeFor(ExactBTC);
1915 Value *RHS = ExactBTCV;
1916 if (ECV->getType() != RHS->getType()) {
1917 Type *WiderTy = SE->getWiderType(ECV->getType(), RHS->getType());
1918 ECV = B.CreateZExt(ECV, WiderTy);
1919 RHS = B.CreateZExt(RHS, WiderTy);
1920 }
1921 auto Pred = L->contains(BI->getSuccessor(0)) ?
1922 ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;
1923 NewCond = B.CreateICmp(Pred, ECV, RHS);
1924 }
1925 Value *OldCond = BI->getCondition();
1926 BI->setCondition(NewCond);
1927 if (OldCond->use_empty())
1928 DeadInsts.emplace_back(OldCond);
1929 Changed = true;
1930 RunUnswitching = true;
1931 }
1932
1933 return Changed;
1934}
1935
1936//===----------------------------------------------------------------------===//
1937// IndVarSimplify driver. Manage several subpasses of IV simplification.
1938//===----------------------------------------------------------------------===//
1939
1940bool IndVarSimplify::run(Loop *L) {
1941 // We need (and expect!) the incoming loop to be in LCSSA.
1942 assert(L->isRecursivelyLCSSAForm(*DT, *LI) &&
1943 "LCSSA required to run indvars!");
1944
1945 // If LoopSimplify form is not available, stay out of trouble. Some notes:
1946 // - LSR currently only supports LoopSimplify-form loops. Indvars'
1947 // canonicalization can be a pessimization without LSR to "clean up"
1948 // afterwards.
1949 // - We depend on having a preheader; in particular,
1950 // Loop::getCanonicalInductionVariable only supports loops with preheaders,
1951 // and we're in trouble if we can't find the induction variable even when
1952 // we've manually inserted one.
1953 // - LFTR relies on having a single backedge.
1954 if (!L->isLoopSimplifyForm())
1955 return false;
1956
1957 bool Changed = false;
1958 // If there are any floating-point recurrences, attempt to
1959 // transform them to use integer recurrences.
1960 Changed |= rewriteNonIntegerIVs(L);
1961
1962 // Create a rewriter object which we'll use to transform the code with.
1963 SCEVExpander Rewriter(*SE, DL, "indvars");
1964#if LLVM_ENABLE_ABI_BREAKING_CHECKS
1965 Rewriter.setDebugType(DEBUG_TYPE);
1966#endif
1967
1968 // Eliminate redundant IV users.
1969 //
1970 // Simplification works best when run before other consumers of SCEV. We
1971 // attempt to avoid evaluating SCEVs for sign/zero extend operations until
1972 // other expressions involving loop IVs have been evaluated. This helps SCEV
1973 // set no-wrap flags before normalizing sign/zero extension.
1974 Rewriter.disableCanonicalMode();
1975 Changed |= simplifyAndExtend(L, Rewriter, LI);
1976
1977 // Check to see if we can compute the final value of any expressions
1978 // that are recurrent in the loop, and substitute the exit values from the
1979 // loop into any instructions outside of the loop that use the final values
1980 // of the current expressions.
1981 if (ReplaceExitValue != NeverRepl) {
1982 if (int Rewrites = rewriteLoopExitValues(L, LI, TLI, SE, TTI, Rewriter, DT,
1983 ReplaceExitValue, DeadInsts)) {
1984 NumReplaced += Rewrites;
1985 Changed = true;
1986 }
1987 }
1988
1989 // Eliminate redundant IV cycles.
1990 NumElimIV += Rewriter.replaceCongruentIVs(L, DT, DeadInsts, TTI);
1991
1992 // Try to convert exit conditions to unsigned and rotate computation
1993 // out of the loop. Note: Handles invalidation internally if needed.
1994 Changed |= canonicalizeExitCondition(L);
1995
1996 // Try to eliminate loop exits based on analyzeable exit counts
1997 if (optimizeLoopExits(L, Rewriter)) {
1998 Changed = true;
1999 // Given we've changed exit counts, notify SCEV
2000 // Some nested loops may share same folded exit basic block,
2001 // thus we need to notify top most loop.
2002 SE->forgetTopmostLoop(L);
2003 }
2004
2005 // Try to form loop invariant tests for loop exits by changing how many
2006 // iterations of the loop run when that is unobservable.
2007 if (predicateLoopExits(L, Rewriter)) {
2008 Changed = true;
2009 // Given we've changed exit counts, notify SCEV
2010 SE->forgetLoop(L);
2011 }
2012
2013 // If we have a trip count expression, rewrite the loop's exit condition
2014 // using it.
2015 if (!DisableLFTR) {
2016 BasicBlock *PreHeader = L->getLoopPreheader();
2017
2018 SmallVector<BasicBlock*, 16> ExitingBlocks;
2019 L->getExitingBlocks(ExitingBlocks);
2020 for (BasicBlock *ExitingBB : ExitingBlocks) {
2021 // Can't rewrite non-branch yet.
2022 if (!isa<BranchInst>(ExitingBB->getTerminator()))
2023 continue;
2024
2025 // If our exitting block exits multiple loops, we can only rewrite the
2026 // innermost one. Otherwise, we're changing how many times the innermost
2027 // loop runs before it exits.
2028 if (LI->getLoopFor(ExitingBB) != L)
2029 continue;
2030
2031 if (!needsLFTR(L, ExitingBB))
2032 continue;
2033
2034 const SCEV *ExitCount = SE->getExitCount(L, ExitingBB);
2035 if (isa<SCEVCouldNotCompute>(ExitCount))
2036 continue;
2037
2038 // This was handled above, but as we form SCEVs, we can sometimes refine
2039 // existing ones; this allows exit counts to be folded to zero which
2040 // weren't when optimizeLoopExits saw them. Arguably, we should iterate
2041 // until stable to handle cases like this better.
2042 if (ExitCount->isZero())
2043 continue;
2044
2045 PHINode *IndVar = FindLoopCounter(L, ExitingBB, ExitCount, SE, DT);
2046 if (!IndVar)
2047 continue;
2048
2049 // Avoid high cost expansions. Note: This heuristic is questionable in
2050 // that our definition of "high cost" is not exactly principled.
2051 if (Rewriter.isHighCostExpansion(ExitCount, L, SCEVCheapExpansionBudget,
2052 TTI, PreHeader->getTerminator()))
2053 continue;
2054
2055 if (!Rewriter.isSafeToExpand(ExitCount))
2056 continue;
2057
2058 Changed |= linearFunctionTestReplace(L, ExitingBB,
2059 ExitCount, IndVar,
2060 Rewriter);
2061 }
2062 }
2063 // Clear the rewriter cache, because values that are in the rewriter's cache
2064 // can be deleted in the loop below, causing the AssertingVH in the cache to
2065 // trigger.
2066 Rewriter.clear();
2067
2068 // Now that we're done iterating through lists, clean up any instructions
2069 // which are now dead.
2070 while (!DeadInsts.empty()) {
2071 Value *V = DeadInsts.pop_back_val();
2072
2073 if (PHINode *PHI = dyn_cast_or_null<PHINode>(V))
2074 Changed |= RecursivelyDeleteDeadPHINode(PHI, TLI, MSSAU.get());
2075 else if (Instruction *Inst = dyn_cast_or_null<Instruction>(V))
2076 Changed |=
2077 RecursivelyDeleteTriviallyDeadInstructions(Inst, TLI, MSSAU.get());
2078 }
2079
2080 // The Rewriter may not be used from this point on.
2081
2082 // Loop-invariant instructions in the preheader that aren't used in the
2083 // loop may be sunk below the loop to reduce register pressure.
2084 Changed |= sinkUnusedInvariants(L);
2085
2086 // rewriteFirstIterationLoopExitValues does not rely on the computation of
2087 // trip count and therefore can further simplify exit values in addition to
2088 // rewriteLoopExitValues.
2089 Changed |= rewriteFirstIterationLoopExitValues(L);
2090
2091 // Clean up dead instructions.
2092 Changed |= DeleteDeadPHIs(L->getHeader(), TLI, MSSAU.get());
2093
2094 // Check a post-condition.
2095 assert(L->isRecursivelyLCSSAForm(*DT, *LI) &&
2096 "Indvars did not preserve LCSSA!");
2097 if (VerifyMemorySSA && MSSAU)
2098 MSSAU->getMemorySSA()->verifyMemorySSA();
2099
2100 return Changed;
2101}
2102
2105 LPMUpdater &) {
2106 Function *F = L.getHeader()->getParent();
2107 const DataLayout &DL = F->getDataLayout();
2108
2109 IndVarSimplify IVS(&AR.LI, &AR.SE, &AR.DT, DL, &AR.TLI, &AR.TTI, AR.MSSA,
2110 WidenIndVars && AllowIVWidening);
2111 if (!IVS.run(&L))
2112 return PreservedAnalyses::all();
2113
2114 auto PA = getLoopPassPreservedAnalyses();
2115 PA.preserveSet<CFGAnalyses>();
2116 if (IVS.runUnswitching()) {
2118 PA.preserve<ShouldRunExtraSimpleLoopUnswitch>();
2119 }
2120
2121 if (AR.MSSA)
2122 PA.preserve<MemorySSAAnalysis>();
2123 return PA;
2124}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define DEBUG_TYPE
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
static Value * genLoopLimit(PHINode *IndVar, BasicBlock *ExitingBB, const SCEV *ExitCount, bool UsePostInc, Loop *L, SCEVExpander &Rewriter, ScalarEvolution *SE)
Insert an IR expression which computes the value held by the IV IndVar (which must be an loop counter...
static void replaceExitCond(BranchInst *BI, Value *NewCond, SmallVectorImpl< WeakTrackingVH > &DeadInsts)
static cl::opt< bool > DisableLFTR("disable-lftr", cl::Hidden, cl::init(false), cl::desc("Disable Linear Function Test Replace optimization"))
static bool isLoopExitTestBasedOn(Value *V, BasicBlock *ExitingBB)
Whether the current loop exit test is based on this value.
static cl::opt< ReplaceExitVal > ReplaceExitValue("replexitval", cl::Hidden, cl::init(OnlyCheapRepl), cl::desc("Choose the strategy to replace exit value in IndVarSimplify"), cl::values(clEnumValN(NeverRepl, "never", "never replace exit value"), clEnumValN(OnlyCheapRepl, "cheap", "only replace exit value when the cost is cheap"), clEnumValN(UnusedIndVarInLoop, "unusedindvarinloop", "only replace exit value when it is an unused " "induction variable in the loop and has cheap replacement cost"), clEnumValN(NoHardUse, "noharduse", "only replace exit values when loop def likely dead"), clEnumValN(AlwaysRepl, "always", "always replace exit value whenever possible")))
static bool isRepresentableAsExactInteger(ConstantFP *FPVal, int64_t IntVal)
static void visitIVCast(CastInst *Cast, WideIVInfo &WI, ScalarEvolution *SE, const TargetTransformInfo *TTI)
Update information about the induction variable that is extended by this sign or zero extend operatio...
static void replaceLoopPHINodesWithPreheaderValues(LoopInfo *LI, Loop *L, SmallVectorImpl< WeakTrackingVH > &DeadInsts, ScalarEvolution &SE)
static bool needsLFTR(Loop *L, BasicBlock *ExitingBB)
linearFunctionTestReplace policy.
static bool optimizeLoopExitWithUnknownExitCount(const Loop *L, BranchInst *BI, BasicBlock *ExitingBB, const SCEV *MaxIter, bool SkipLastIter, ScalarEvolution *SE, SCEVExpander &Rewriter, SmallVectorImpl< WeakTrackingVH > &DeadInsts)
static Value * createInvariantCond(const Loop *L, BasicBlock *ExitingBB, const ScalarEvolution::LoopInvariantPredicate &LIP, SCEVExpander &Rewriter)
static bool isLoopCounter(PHINode *Phi, Loop *L, ScalarEvolution *SE)
Return true if the given phi is a "counter" in L.
static std::optional< Value * > createReplacement(ICmpInst *ICmp, const Loop *L, BasicBlock *ExitingBB, const SCEV *MaxIter, bool Inverted, bool SkipLastIter, ScalarEvolution *SE, SCEVExpander &Rewriter)
static bool hasConcreteDefImpl(Value *V, SmallPtrSetImpl< Value * > &Visited, unsigned Depth)
Recursive helper for hasConcreteDef().
static bool hasConcreteDef(Value *V)
Return true if the given value is concrete.
static void foldExit(const Loop *L, BasicBlock *ExitingBB, bool IsTaken, SmallVectorImpl< WeakTrackingVH > &DeadInsts)
static PHINode * getLoopPhiForCounter(Value *IncV, Loop *L)
Given an Value which is hoped to be part of an add recurance in the given loop, return the associated...
static Constant * createFoldedExitCond(const Loop *L, BasicBlock *ExitingBB, bool IsTaken)
static cl::opt< bool > LoopPredicationTraps("indvars-predicate-loop-traps", cl::Hidden, cl::init(true), cl::desc("Predicate conditions that trap in loops with only local writes"))
static cl::opt< bool > UsePostIncrementRanges("indvars-post-increment-ranges", cl::Hidden, cl::desc("Use post increment control-dependent ranges in IndVarSimplify"), cl::init(true))
static PHINode * FindLoopCounter(Loop *L, BasicBlock *ExitingBB, const SCEV *BECount, ScalarEvolution *SE, DominatorTree *DT)
Search the loop header for a loop counter (anadd rec w/step of one) suitable for use by LFTR.
static cl::opt< bool > AllowIVWidening("indvars-widen-indvars", cl::Hidden, cl::init(true), cl::desc("Allow widening of indvars to eliminate s/zext"))
static bool crashingBBWithoutEffect(const BasicBlock &BB)
static bool ConvertToSInt(const APFloat &APF, int64_t &IntVal)
Convert APF to an integer, if possible.
static cl::opt< bool > LoopPredication("indvars-predicate-loops", cl::Hidden, cl::init(true), cl::desc("Predicate conditions in read only loops"))
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
This pass exposes codegen information to IR-level passes.
Virtual Register Rewriter
Value * RHS
Value * LHS
static const uint32_t IV[8]
Definition blake3_impl.h:83
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:290
static LLVM_ABI bool isIEEELikeFP(const fltSemantics &)
Definition APFloat.cpp:331
const fltSemantics & getSemantics() const
Definition APFloat.h:1439
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1314
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:528
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
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:233
Conditional or Unconditional Branch instruction.
iterator_range< succ_op_iterator > successors()
void setCondition(Value *V)
bool isConditional() const
BasicBlock * getSuccessor(unsigned i) const
Value * getCondition() const
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:448
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Definition InstrTypes.h:610
static LLVM_ABI CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:679
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:684
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:687
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:692
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:789
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
const APFloat & getValueAPF() const
Definition Constants.h:320
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition Constants.h:131
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
bool isLegalInteger(uint64_t Width) const
Returns true if the specified type is known to be a native integer type supported by the CPU.
Definition DataLayout.h:229
static DebugLoc getDropped()
Definition DebugLoc.h:164
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:164
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getCmpPredicate() const
CmpPredicate getInverseCmpPredicate() const
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form.
Definition LoopInfo.h:441
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
An analysis that produces MemorySSA for a function.
Definition MemorySSA.h:936
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition MemorySSA.h:702
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
void setIncomingValue(unsigned i, Value *V)
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
static unsigned getIncomingValueNumForOperand(unsigned i)
unsigned getNumIncomingValues() const
Return the number of incoming edges.
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...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
This node represents a polynomial recurrence on the trip count of the specified loop.
LLVM_ABI const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
LLVM_ABI const SCEVAddRecExpr * getPostIncExpr(ScalarEvolution &SE) const
Return an expression representing the value of this expression one iteration of the loop ahead.
This class uses information about analyze scalars to rewrite expressions in canonical form.
This class represents an analyzed expression in the program.
LLVM_ABI bool isOne() const
Return true if the expression is a constant one.
LLVM_ABI bool isZero() const
Return true if the expression is a constant zero.
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
The main scalar evolution driver.
LLVM_ABI std::optional< LoopInvariantPredicate > getLoopInvariantExitCondDuringFirstIterations(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, const Instruction *CtxI, const SCEV *MaxIter)
If the result of the predicate LHS Pred RHS is loop invariant with respect to L at given Context duri...
LLVM_ABI Type * getWiderType(Type *Ty1, Type *Ty2) const
LLVM_ABI bool isLoopEntryGuardedByCond(const Loop *L, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether entry to the loop is protected by a conditional between LHS and RHS.
LLVM_ABI const SCEV * getSCEVAtScope(const SCEV *S, const Loop *L)
Return a SCEV expression for the specified value at the specified scope in the program.
LLVM_ABI const SCEV * getBackedgeTakenCount(const Loop *L, ExitCountKind Kind=Exact)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
LLVM_ABI ExitLimit computeExitLimitFromCond(const Loop *L, Value *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit, bool AllowPredicates=false)
Compute the number of times the backedge of the specified loop will execute if its exit condition wer...
LLVM_ABI uint64_t getTypeSizeInBits(Type *Ty) const
Return the size in bits of the specified type, for which isSCEVable must return true.
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
LLVM_ABI std::optional< bool > evaluatePredicateAt(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI)
Check whether the condition described by Pred, LHS, and RHS is true or false in the given Context.
LLVM_ABI void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
LLVM_ABI bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
LLVM_ABI 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...
ConstantRange getUnsignedRange(const SCEV *S)
Determine the unsigned range for a particular SCEV.
LLVM_ABI void forgetTopmostLoop(const Loop *L)
LLVM_ABI 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...
LLVM_ABI const SCEV * getTruncateExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
LLVM_ABI const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
const SCEV * getMinusOne(Type *Ty)
Return a SCEV for the constant -1 of a specific type.
LLVM_ABI const SCEV * getNoopOrZeroExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI const SCEV * getUMinFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS, bool Sequential=false)
Promote the operands to the wider of the types using zero-extension, and then perform a umin operatio...
LLVM_ABI const SCEV * getCouldNotCompute()
LLVM_ABI const SCEV * getExitCount(const Loop *L, const BasicBlock *ExitingBlock, ExitCountKind Kind=Exact)
Return the number of times the backedge executes before the given exit would be taken; if not exactly...
LLVM_ABI const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
@ SymbolicMaximum
An expression which provides an upper bound on the exact trip count.
LLVM_ABI const SCEV * applyLoopGuards(const SCEV *Expr, const Loop *L)
Try to apply information from loop guards for L to Expr.
LLVM_ABI bool isKnownPredicateAt(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
const SCEV * getSymbolicMaxBackedgeTakenCount(const Loop *L)
When successful, this returns a SCEV that is greater than or equal to (i.e.
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:546
iterator_range< user_iterator > users()
Definition Value.h:426
bool use_empty() const
Definition Value.h:346
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:396
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
Changed
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
cst_pred_ty< is_one > m_scev_One()
Match an integer 1.
specificloop_ty m_SpecificLoop(const Loop *L)
SCEVAffineAddRec_match< Op0_t, Op1_t, class_match< const Loop > > m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1)
bool match(const SCEV *S, const Pattern &P)
class_match< const SCEV > m_SCEV()
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
@ User
could "use" a pointer
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
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:1725
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition Local.cpp:533
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
FunctionAddr VTableAddr uintptr_t uintptr_t Int32Ty
Definition InstrProf.h:296
PHINode * createWideIV(const WideIVInfo &WI, LoopInfo *LI, ScalarEvolution *SE, SCEVExpander &Rewriter, DominatorTree *DT, SmallVectorImpl< WeakTrackingVH > &DeadInsts, unsigned &NumElimExt, unsigned &NumWidened, bool HasGuards, bool UsePostIncrementRanges)
Widen Induction Variables - Extend the width of an IV to cover its widest uses.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
LLVM_ABI bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Examine each PHI in the given block and delete it if it is dead.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1622
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI cl::opt< unsigned > SCEVCheapExpansionBudget
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
std::pair< bool, bool > simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT, LoopInfo *LI, const TargetTransformInfo *TTI, SmallVectorImpl< WeakTrackingVH > &Dead, SCEVExpander &Rewriter, IVVisitor *V=nullptr)
simplifyUsersOfIV - Simplify instructions that use this induction variable by using ScalarEvolution t...
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
TargetTransformInfo TTI
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
LLVM_ABI bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition MemorySSA.cpp:84
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
DWARFExpression::Operation Op
constexpr U AbsoluteValue(T X)
Return the absolute value of a signed integer, converted to the corresponding unsigned integer type.
Definition MathExtras.h:592
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2120
LLVM_ABI bool isAlmostDeadIV(PHINode *IV, BasicBlock *LatchBlock, Value *Cond)
Return true if the induction variable IV in a Loop whose latch is LatchBlock would become dead if the...
LLVM_ABI int rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI, ScalarEvolution *SE, const TargetTransformInfo *TTI, SCEVExpander &Rewriter, DominatorTree *DT, ReplaceExitVal ReplaceExitValue, SmallVector< WeakTrackingVH, 16 > &DeadInsts)
If the final value of any expressions that are recurrent in the loop can be computed,...
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:363
LLVM_ABI bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is an effectively dead PHI node, due to being a def-use chain of single-use no...
Definition Local.cpp:641
@ UnusedIndVarInLoop
Definition LoopUtils.h:554
@ OnlyCheapRepl
Definition LoopUtils.h:552
@ NeverRepl
Definition LoopUtils.h:551
@ NoHardUse
Definition LoopUtils.h:553
@ AlwaysRepl
Definition LoopUtils.h:555
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
A marker analysis to determine if SimpleLoopUnswitch should run again on a given loop.
Collect information about induction variables that are used by sign/zero extend operations.