LLVM 17.0.0git
LoopRerollPass.cpp
Go to the documentation of this file.
1//===- LoopReroll.cpp - Loop rerolling pass -------------------------------===//
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 pass implements a simple loop reroller.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ADT/APInt.h"
14#include "llvm/ADT/BitVector.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/MapVector.h"
18#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/Statistic.h"
30#include "llvm/IR/BasicBlock.h"
31#include "llvm/IR/Constants.h"
32#include "llvm/IR/Dominators.h"
33#include "llvm/IR/InstrTypes.h"
34#include "llvm/IR/Instruction.h"
37#include "llvm/IR/Module.h"
38#include "llvm/IR/Type.h"
39#include "llvm/IR/Use.h"
40#include "llvm/IR/User.h"
41#include "llvm/IR/Value.h"
43#include "llvm/Pass.h"
46#include "llvm/Support/Debug.h"
55#include <cassert>
56#include <cstddef>
57#include <cstdint>
58#include <iterator>
59#include <map>
60#include <utility>
61
62using namespace llvm;
63
64#define DEBUG_TYPE "loop-reroll"
65
66STATISTIC(NumRerolledLoops, "Number of rerolled loops");
67
69NumToleratedFailedMatches("reroll-num-tolerated-failed-matches", cl::init(400),
71 cl::desc("The maximum number of failures to tolerate"
72 " during fuzzy matching. (default: 400)"));
73
74// This loop re-rolling transformation aims to transform loops like this:
75//
76// int foo(int a);
77// void bar(int *x) {
78// for (int i = 0; i < 500; i += 3) {
79// foo(i);
80// foo(i+1);
81// foo(i+2);
82// }
83// }
84//
85// into a loop like this:
86//
87// void bar(int *x) {
88// for (int i = 0; i < 500; ++i)
89// foo(i);
90// }
91//
92// It does this by looking for loops that, besides the latch code, are composed
93// of isomorphic DAGs of instructions, with each DAG rooted at some increment
94// to the induction variable, and where each DAG is isomorphic to the DAG
95// rooted at the induction variable (excepting the sub-DAGs which root the
96// other induction-variable increments). In other words, we're looking for loop
97// bodies of the form:
98//
99// %iv = phi [ (preheader, ...), (body, %iv.next) ]
100// f(%iv)
101// %iv.1 = add %iv, 1 <-- a root increment
102// f(%iv.1)
103// %iv.2 = add %iv, 2 <-- a root increment
104// f(%iv.2)
105// %iv.scale_m_1 = add %iv, scale-1 <-- a root increment
106// f(%iv.scale_m_1)
107// ...
108// %iv.next = add %iv, scale
109// %cmp = icmp(%iv, ...)
110// br %cmp, header, exit
111//
112// where each f(i) is a set of instructions that, collectively, are a function
113// only of i (and other loop-invariant values).
114//
115// As a special case, we can also reroll loops like this:
116//
117// int foo(int);
118// void bar(int *x) {
119// for (int i = 0; i < 500; ++i) {
120// x[3*i] = foo(0);
121// x[3*i+1] = foo(0);
122// x[3*i+2] = foo(0);
123// }
124// }
125//
126// into this:
127//
128// void bar(int *x) {
129// for (int i = 0; i < 1500; ++i)
130// x[i] = foo(0);
131// }
132//
133// in which case, we're looking for inputs like this:
134//
135// %iv = phi [ (preheader, ...), (body, %iv.next) ]
136// %scaled.iv = mul %iv, scale
137// f(%scaled.iv)
138// %scaled.iv.1 = add %scaled.iv, 1
139// f(%scaled.iv.1)
140// %scaled.iv.2 = add %scaled.iv, 2
141// f(%scaled.iv.2)
142// %scaled.iv.scale_m_1 = add %scaled.iv, scale-1
143// f(%scaled.iv.scale_m_1)
144// ...
145// %iv.next = add %iv, 1
146// %cmp = icmp(%iv, ...)
147// br %cmp, header, exit
148
149namespace {
150
151 enum IterationLimits {
152 /// The maximum number of iterations that we'll try and reroll.
153 IL_MaxRerollIterations = 32,
154 /// The bitvector index used by loop induction variables and other
155 /// instructions that belong to all iterations.
156 IL_All,
157 IL_End
158 };
159
160 class LoopReroll {
161 public:
162 LoopReroll(AliasAnalysis *AA, LoopInfo *LI, ScalarEvolution *SE,
163 TargetLibraryInfo *TLI, DominatorTree *DT, bool PreserveLCSSA)
164 : AA(AA), LI(LI), SE(SE), TLI(TLI), DT(DT),
165 PreserveLCSSA(PreserveLCSSA) {}
166 bool runOnLoop(Loop *L);
167
168 protected:
169 AliasAnalysis *AA;
170 LoopInfo *LI;
171 ScalarEvolution *SE;
173 DominatorTree *DT;
174 bool PreserveLCSSA;
175
176 using SmallInstructionVector = SmallVector<Instruction *, 16>;
177 using SmallInstructionSet = SmallPtrSet<Instruction *, 16>;
178 using TinyInstructionVector = SmallVector<Instruction *, 1>;
179
180 // Map between induction variable and its increment
182
183 // For loop with multiple induction variables, remember the ones used only to
184 // control the loop.
185 TinyInstructionVector LoopControlIVs;
186
187 // A chain of isomorphic instructions, identified by a single-use PHI
188 // representing a reduction. Only the last value may be used outside the
189 // loop.
190 struct SimpleLoopReduction {
191 SimpleLoopReduction(Instruction *P, Loop *L) : Instructions(1, P) {
192 assert(isa<PHINode>(P) && "First reduction instruction must be a PHI");
193 add(L);
194 }
195
196 bool valid() const {
197 return Valid;
198 }
199
200 Instruction *getPHI() const {
201 assert(Valid && "Using invalid reduction");
202 return Instructions.front();
203 }
204
205 Instruction *getReducedValue() const {
206 assert(Valid && "Using invalid reduction");
207 return Instructions.back();
208 }
209
210 Instruction *get(size_t i) const {
211 assert(Valid && "Using invalid reduction");
212 return Instructions[i+1];
213 }
214
215 Instruction *operator [] (size_t i) const { return get(i); }
216
217 // The size, ignoring the initial PHI.
218 size_t size() const {
219 assert(Valid && "Using invalid reduction");
220 return Instructions.size()-1;
221 }
222
223 using iterator = SmallInstructionVector::iterator;
225
226 iterator begin() {
227 assert(Valid && "Using invalid reduction");
228 return std::next(Instructions.begin());
229 }
230
231 const_iterator begin() const {
232 assert(Valid && "Using invalid reduction");
233 return std::next(Instructions.begin());
234 }
235
236 iterator end() { return Instructions.end(); }
237 const_iterator end() const { return Instructions.end(); }
238
239 protected:
240 bool Valid = false;
241 SmallInstructionVector Instructions;
242
243 void add(Loop *L);
244 };
245
246 // The set of all reductions, and state tracking of possible reductions
247 // during loop instruction processing.
248 struct ReductionTracker {
249 using SmallReductionVector = SmallVector<SimpleLoopReduction, 16>;
250
251 // Add a new possible reduction.
252 void addSLR(SimpleLoopReduction &SLR) { PossibleReds.push_back(SLR); }
253
254 // Setup to track possible reductions corresponding to the provided
255 // rerolling scale. Only reductions with a number of non-PHI instructions
256 // that is divisible by the scale are considered. Three instructions sets
257 // are filled in:
258 // - A set of all possible instructions in eligible reductions.
259 // - A set of all PHIs in eligible reductions
260 // - A set of all reduced values (last instructions) in eligible
261 // reductions.
262 void restrictToScale(uint64_t Scale,
263 SmallInstructionSet &PossibleRedSet,
264 SmallInstructionSet &PossibleRedPHISet,
265 SmallInstructionSet &PossibleRedLastSet) {
266 PossibleRedIdx.clear();
267 PossibleRedIter.clear();
268 Reds.clear();
269
270 for (unsigned i = 0, e = PossibleReds.size(); i != e; ++i)
271 if (PossibleReds[i].size() % Scale == 0) {
272 PossibleRedLastSet.insert(PossibleReds[i].getReducedValue());
273 PossibleRedPHISet.insert(PossibleReds[i].getPHI());
274
275 PossibleRedSet.insert(PossibleReds[i].getPHI());
276 PossibleRedIdx[PossibleReds[i].getPHI()] = i;
277 for (Instruction *J : PossibleReds[i]) {
278 PossibleRedSet.insert(J);
279 PossibleRedIdx[J] = i;
280 }
281 }
282 }
283
284 // The functions below are used while processing the loop instructions.
285
286 // Are the two instructions both from reductions, and furthermore, from
287 // the same reduction?
288 bool isPairInSame(Instruction *J1, Instruction *J2) {
289 DenseMap<Instruction *, int>::iterator J1I = PossibleRedIdx.find(J1);
290 if (J1I != PossibleRedIdx.end()) {
291 DenseMap<Instruction *, int>::iterator J2I = PossibleRedIdx.find(J2);
292 if (J2I != PossibleRedIdx.end() && J1I->second == J2I->second)
293 return true;
294 }
295
296 return false;
297 }
298
299 // The two provided instructions, the first from the base iteration, and
300 // the second from iteration i, form a matched pair. If these are part of
301 // a reduction, record that fact.
302 void recordPair(Instruction *J1, Instruction *J2, unsigned i) {
303 if (PossibleRedIdx.count(J1)) {
304 assert(PossibleRedIdx.count(J2) &&
305 "Recording reduction vs. non-reduction instruction?");
306
307 PossibleRedIter[J1] = 0;
308 PossibleRedIter[J2] = i;
309
310 int Idx = PossibleRedIdx[J1];
311 assert(Idx == PossibleRedIdx[J2] &&
312 "Recording pair from different reductions?");
313 Reds.insert(Idx);
314 }
315 }
316
317 // The functions below can be called after we've finished processing all
318 // instructions in the loop, and we know which reductions were selected.
319
320 bool validateSelected();
321 void replaceSelected();
322
323 protected:
324 // The vector of all possible reductions (for any scale).
325 SmallReductionVector PossibleReds;
326
327 DenseMap<Instruction *, int> PossibleRedIdx;
328 DenseMap<Instruction *, int> PossibleRedIter;
329 DenseSet<int> Reds;
330 };
331
332 // A DAGRootSet models an induction variable being used in a rerollable
333 // loop. For example,
334 //
335 // x[i*3+0] = y1
336 // x[i*3+1] = y2
337 // x[i*3+2] = y3
338 //
339 // Base instruction -> i*3
340 // +---+----+
341 // / | \
342 // ST[y1] +1 +2 <-- Roots
343 // | |
344 // ST[y2] ST[y3]
345 //
346 // There may be multiple DAGRoots, for example:
347 //
348 // x[i*2+0] = ... (1)
349 // x[i*2+1] = ... (1)
350 // x[i*2+4] = ... (2)
351 // x[i*2+5] = ... (2)
352 // x[(i+1234)*2+5678] = ... (3)
353 // x[(i+1234)*2+5679] = ... (3)
354 //
355 // The loop will be rerolled by adding a new loop induction variable,
356 // one for the Base instruction in each DAGRootSet.
357 //
358 struct DAGRootSet {
359 Instruction *BaseInst;
360 SmallInstructionVector Roots;
361
362 // The instructions between IV and BaseInst (but not including BaseInst).
363 SmallInstructionSet SubsumedInsts;
364 };
365
366 // The set of all DAG roots, and state tracking of all roots
367 // for a particular induction variable.
368 struct DAGRootTracker {
369 DAGRootTracker(LoopReroll *Parent, Loop *L, Instruction *IV,
372 bool PreserveLCSSA,
374 TinyInstructionVector LoopCtrlIVs)
375 : Parent(Parent), L(L), SE(SE), AA(AA), TLI(TLI), DT(DT), LI(LI),
376 PreserveLCSSA(PreserveLCSSA), IV(IV), IVToIncMap(IncrMap),
377 LoopControlIVs(LoopCtrlIVs) {}
378
379 /// Stage 1: Find all the DAG roots for the induction variable.
380 bool findRoots();
381
382 /// Stage 2: Validate if the found roots are valid.
383 bool validate(ReductionTracker &Reductions);
384
385 /// Stage 3: Assuming validate() returned true, perform the
386 /// replacement.
387 /// @param BackedgeTakenCount The backedge-taken count of L.
388 void replace(const SCEV *BackedgeTakenCount);
389
390 protected:
392
393 void findRootsRecursive(Instruction *IVU,
394 SmallInstructionSet SubsumedInsts);
395 bool findRootsBase(Instruction *IVU, SmallInstructionSet SubsumedInsts);
396 bool collectPossibleRoots(Instruction *Base,
397 std::map<int64_t,Instruction*> &Roots);
398 bool validateRootSet(DAGRootSet &DRS);
399
400 bool collectUsedInstructions(SmallInstructionSet &PossibleRedSet);
401 void collectInLoopUserSet(const SmallInstructionVector &Roots,
402 const SmallInstructionSet &Exclude,
403 const SmallInstructionSet &Final,
405 void collectInLoopUserSet(Instruction *Root,
406 const SmallInstructionSet &Exclude,
407 const SmallInstructionSet &Final,
409
410 UsesTy::iterator nextInstr(int Val, UsesTy &In,
411 const SmallInstructionSet &Exclude,
412 UsesTy::iterator *StartI=nullptr);
413 bool isBaseInst(Instruction *I);
414 bool isRootInst(Instruction *I);
415 bool instrDependsOn(Instruction *I,
416 UsesTy::iterator Start,
417 UsesTy::iterator End);
418 void replaceIV(DAGRootSet &DRS, const SCEV *Start, const SCEV *IncrExpr);
419
420 LoopReroll *Parent;
421
422 // Members of Parent, replicated here for brevity.
423 Loop *L;
424 ScalarEvolution *SE;
425 AliasAnalysis *AA;
427 DominatorTree *DT;
428 LoopInfo *LI;
429 bool PreserveLCSSA;
430
431 // The loop induction variable.
432 Instruction *IV;
433
434 // Loop step amount.
435 int64_t Inc;
436
437 // Loop reroll count; if Inc == 1, this records the scaling applied
438 // to the indvar: a[i*2+0] = ...; a[i*2+1] = ... ;
439 // If Inc is not 1, Scale = Inc.
440 uint64_t Scale;
441
442 // The roots themselves.
444
445 // All increment instructions for IV.
446 SmallInstructionVector LoopIncs;
447
448 // Map of all instructions in the loop (in order) to the iterations
449 // they are used in (or specially, IL_All for instructions
450 // used in the loop increment mechanism).
451 UsesTy Uses;
452
453 // Map between induction variable and its increment
455
456 TinyInstructionVector LoopControlIVs;
457 };
458
459 // Check if it is a compare-like instruction whose user is a branch
460 bool isCompareUsedByBranch(Instruction *I) {
461 auto *TI = I->getParent()->getTerminator();
462 if (!isa<BranchInst>(TI) || !isa<CmpInst>(I))
463 return false;
464 return I->hasOneUse() && TI->getOperand(0) == I;
465 };
466
467 bool isLoopControlIV(Loop *L, Instruction *IV);
468 void collectPossibleIVs(Loop *L, SmallInstructionVector &PossibleIVs);
469 void collectPossibleReductions(Loop *L,
470 ReductionTracker &Reductions);
471 bool reroll(Instruction *IV, Loop *L, BasicBlock *Header,
472 const SCEV *BackedgeTakenCount, ReductionTracker &Reductions);
473 };
474
475} // end anonymous namespace
476
477// Returns true if the provided instruction is used outside the given loop.
478// This operates like Instruction::isUsedOutsideOfBlock, but considers PHIs in
479// non-loop blocks to be outside the loop.
481 for (User *U : I->users()) {
482 if (!L->contains(cast<Instruction>(U)))
483 return true;
484 }
485 return false;
486}
487
488// Check if an IV is only used to control the loop. There are two cases:
489// 1. It only has one use which is loop increment, and the increment is only
490// used by comparison and the PHI (could has sext with nsw in between), and the
491// comparison is only used by branch.
492// 2. It is used by loop increment and the comparison, the loop increment is
493// only used by the PHI, and the comparison is used only by the branch.
494bool LoopReroll::isLoopControlIV(Loop *L, Instruction *IV) {
495 unsigned IVUses = IV->getNumUses();
496 if (IVUses != 2 && IVUses != 1)
497 return false;
498
499 for (auto *User : IV->users()) {
500 int32_t IncOrCmpUses = User->getNumUses();
501 bool IsCompInst = isCompareUsedByBranch(cast<Instruction>(User));
502
503 // User can only have one or two uses.
504 if (IncOrCmpUses != 2 && IncOrCmpUses != 1)
505 return false;
506
507 // Case 1
508 if (IVUses == 1) {
509 // The only user must be the loop increment.
510 // The loop increment must have two uses.
511 if (IsCompInst || IncOrCmpUses != 2)
512 return false;
513 }
514
515 // Case 2
516 if (IVUses == 2 && IncOrCmpUses != 1)
517 return false;
518
519 // The users of the IV must be a binary operation or a comparison
520 if (auto *BO = dyn_cast<BinaryOperator>(User)) {
521 if (BO->getOpcode() == Instruction::Add) {
522 // Loop Increment
523 // User of Loop Increment should be either PHI or CMP
524 for (auto *UU : User->users()) {
525 if (PHINode *PN = dyn_cast<PHINode>(UU)) {
526 if (PN != IV)
527 return false;
528 }
529 // Must be a CMP or an ext (of a value with nsw) then CMP
530 else {
531 auto *UUser = cast<Instruction>(UU);
532 // Skip SExt if we are extending an nsw value
533 // TODO: Allow ZExt too
534 if (BO->hasNoSignedWrap() && UUser->hasOneUse() &&
535 isa<SExtInst>(UUser))
536 UUser = cast<Instruction>(*(UUser->user_begin()));
537 if (!isCompareUsedByBranch(UUser))
538 return false;
539 }
540 }
541 } else
542 return false;
543 // Compare : can only have one use, and must be branch
544 } else if (!IsCompInst)
545 return false;
546 }
547 return true;
548}
549
550// Collect the list of loop induction variables with respect to which it might
551// be possible to reroll the loop.
552void LoopReroll::collectPossibleIVs(Loop *L,
553 SmallInstructionVector &PossibleIVs) {
554 for (Instruction &IV : L->getHeader()->phis()) {
555 if (!IV.getType()->isIntegerTy() && !IV.getType()->isPointerTy())
556 continue;
557
558 if (const SCEVAddRecExpr *PHISCEV =
559 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(&IV))) {
560 if (PHISCEV->getLoop() != L)
561 continue;
562 if (!PHISCEV->isAffine())
563 continue;
564 const auto *IncSCEV = dyn_cast<SCEVConstant>(PHISCEV->getStepRecurrence(*SE));
565 if (IncSCEV) {
566 IVToIncMap[&IV] = IncSCEV->getValue()->getSExtValue();
567 LLVM_DEBUG(dbgs() << "LRR: Possible IV: " << IV << " = " << *PHISCEV
568 << "\n");
569
570 if (isLoopControlIV(L, &IV)) {
571 LoopControlIVs.push_back(&IV);
572 LLVM_DEBUG(dbgs() << "LRR: Loop control only IV: " << IV
573 << " = " << *PHISCEV << "\n");
574 } else
575 PossibleIVs.push_back(&IV);
576 }
577 }
578 }
579}
580
581// Add the remainder of the reduction-variable chain to the instruction vector
582// (the initial PHINode has already been added). If successful, the object is
583// marked as valid.
584void LoopReroll::SimpleLoopReduction::add(Loop *L) {
585 assert(!Valid && "Cannot add to an already-valid chain");
586
587 // The reduction variable must be a chain of single-use instructions
588 // (including the PHI), except for the last value (which is used by the PHI
589 // and also outside the loop).
590 Instruction *C = Instructions.front();
591 if (C->user_empty())
592 return;
593
594 do {
595 C = cast<Instruction>(*C->user_begin());
596 if (C->hasOneUse()) {
597 if (!C->isBinaryOp())
598 return;
599
600 if (!(isa<PHINode>(Instructions.back()) ||
601 C->isSameOperationAs(Instructions.back())))
602 return;
603
604 Instructions.push_back(C);
605 }
606 } while (C->hasOneUse());
607
608 if (Instructions.size() < 2 ||
609 !C->isSameOperationAs(Instructions.back()) ||
610 C->use_empty())
611 return;
612
613 // C is now the (potential) last instruction in the reduction chain.
614 for (User *U : C->users()) {
615 // The only in-loop user can be the initial PHI.
616 if (L->contains(cast<Instruction>(U)))
617 if (cast<Instruction>(U) != Instructions.front())
618 return;
619 }
620
621 Instructions.push_back(C);
622 Valid = true;
623}
624
625// Collect the vector of possible reduction variables.
626void LoopReroll::collectPossibleReductions(Loop *L,
627 ReductionTracker &Reductions) {
628 BasicBlock *Header = L->getHeader();
629 for (BasicBlock::iterator I = Header->begin(),
630 IE = Header->getFirstInsertionPt(); I != IE; ++I) {
631 if (!isa<PHINode>(I))
632 continue;
633 if (!I->getType()->isSingleValueType())
634 continue;
635
636 SimpleLoopReduction SLR(&*I, L);
637 if (!SLR.valid())
638 continue;
639
640 LLVM_DEBUG(dbgs() << "LRR: Possible reduction: " << *I << " (with "
641 << SLR.size() << " chained instructions)\n");
642 Reductions.addSLR(SLR);
643 }
644}
645
646// Collect the set of all users of the provided root instruction. This set of
647// users contains not only the direct users of the root instruction, but also
648// all users of those users, and so on. There are two exceptions:
649//
650// 1. Instructions in the set of excluded instructions are never added to the
651// use set (even if they are users). This is used, for example, to exclude
652// including root increments in the use set of the primary IV.
653//
654// 2. Instructions in the set of final instructions are added to the use set
655// if they are users, but their users are not added. This is used, for
656// example, to prevent a reduction update from forcing all later reduction
657// updates into the use set.
658void LoopReroll::DAGRootTracker::collectInLoopUserSet(
659 Instruction *Root, const SmallInstructionSet &Exclude,
660 const SmallInstructionSet &Final,
662 SmallInstructionVector Queue(1, Root);
663 while (!Queue.empty()) {
664 Instruction *I = Queue.pop_back_val();
665 if (!Users.insert(I).second)
666 continue;
667
668 if (!Final.count(I))
669 for (Use &U : I->uses()) {
670 Instruction *User = cast<Instruction>(U.getUser());
671 if (PHINode *PN = dyn_cast<PHINode>(User)) {
672 // Ignore "wrap-around" uses to PHIs of this loop's header.
673 if (PN->getIncomingBlock(U) == L->getHeader())
674 continue;
675 }
676
677 if (L->contains(User) && !Exclude.count(User)) {
678 Queue.push_back(User);
679 }
680 }
681
682 // We also want to collect single-user "feeder" values.
683 for (Use &U : I->operands()) {
684 if (Instruction *Op = dyn_cast<Instruction>(U))
685 if (Op->hasOneUse() && L->contains(Op) && !Exclude.count(Op) &&
686 !Final.count(Op))
687 Queue.push_back(Op);
688 }
689 }
690}
691
692// Collect all of the users of all of the provided root instructions (combined
693// into a single set).
694void LoopReroll::DAGRootTracker::collectInLoopUserSet(
695 const SmallInstructionVector &Roots,
696 const SmallInstructionSet &Exclude,
697 const SmallInstructionSet &Final,
699 for (Instruction *Root : Roots)
700 collectInLoopUserSet(Root, Exclude, Final, Users);
701}
702
704 if (LoadInst *LI = dyn_cast<LoadInst>(I))
705 return LI->isUnordered();
706 if (StoreInst *SI = dyn_cast<StoreInst>(I))
707 return SI->isUnordered();
708 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
709 return !MI->isVolatile();
710 return false;
711}
712
713/// Return true if IVU is a "simple" arithmetic operation.
714/// This is used for narrowing the search space for DAGRoots; only arithmetic
715/// and GEPs can be part of a DAGRoot.
716static bool isSimpleArithmeticOp(User *IVU) {
717 if (Instruction *I = dyn_cast<Instruction>(IVU)) {
718 switch (I->getOpcode()) {
719 default: return false;
720 case Instruction::Add:
721 case Instruction::Sub:
722 case Instruction::Mul:
723 case Instruction::Shl:
724 case Instruction::AShr:
725 case Instruction::LShr:
726 case Instruction::GetElementPtr:
727 case Instruction::Trunc:
728 case Instruction::ZExt:
729 case Instruction::SExt:
730 return true;
731 }
732 }
733 return false;
734}
735
737 BinaryOperator *BO = dyn_cast<BinaryOperator>(U);
738
739 if ((BO && BO->getOpcode() != Instruction::Add) ||
740 (!BO && !isa<GetElementPtrInst>(U)))
741 return false;
742
743 for (auto *UU : U->users()) {
744 PHINode *PN = dyn_cast<PHINode>(UU);
745 if (PN && PN == IV)
746 return true;
747 }
748 return false;
749}
750
751bool LoopReroll::DAGRootTracker::
752collectPossibleRoots(Instruction *Base, std::map<int64_t,Instruction*> &Roots) {
753 SmallInstructionVector BaseUsers;
754
755 for (auto *I : Base->users()) {
756 ConstantInt *CI = nullptr;
757
758 if (isLoopIncrement(I, IV)) {
759 LoopIncs.push_back(cast<Instruction>(I));
760 continue;
761 }
762
763 // The root nodes must be either GEPs, ORs or ADDs.
764 if (auto *BO = dyn_cast<BinaryOperator>(I)) {
765 if (BO->getOpcode() == Instruction::Add ||
766 BO->getOpcode() == Instruction::Or)
767 CI = dyn_cast<ConstantInt>(BO->getOperand(1));
768 } else if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
769 Value *LastOperand = GEP->getOperand(GEP->getNumOperands()-1);
770 CI = dyn_cast<ConstantInt>(LastOperand);
771 }
772
773 if (!CI) {
774 if (Instruction *II = dyn_cast<Instruction>(I)) {
775 BaseUsers.push_back(II);
776 continue;
777 } else {
778 LLVM_DEBUG(dbgs() << "LRR: Aborting due to non-instruction: " << *I
779 << "\n");
780 return false;
781 }
782 }
783
784 int64_t V = std::abs(CI->getValue().getSExtValue());
785 if (Roots.find(V) != Roots.end())
786 // No duplicates, please.
787 return false;
788
789 Roots[V] = cast<Instruction>(I);
790 }
791
792 // Make sure we have at least two roots.
793 if (Roots.empty() || (Roots.size() == 1 && BaseUsers.empty()))
794 return false;
795
796 // If we found non-loop-inc, non-root users of Base, assume they are
797 // for the zeroth root index. This is because "add %a, 0" gets optimized
798 // away.
799 if (BaseUsers.size()) {
800 if (Roots.find(0) != Roots.end()) {
801 LLVM_DEBUG(dbgs() << "LRR: Multiple roots found for base - aborting!\n");
802 return false;
803 }
804 Roots[0] = Base;
805 }
806
807 // Calculate the number of users of the base, or lowest indexed, iteration.
808 unsigned NumBaseUses = BaseUsers.size();
809 if (NumBaseUses == 0)
810 NumBaseUses = Roots.begin()->second->getNumUses();
811
812 // Check that every node has the same number of users.
813 for (auto &KV : Roots) {
814 if (KV.first == 0)
815 continue;
816 if (!KV.second->hasNUses(NumBaseUses)) {
817 LLVM_DEBUG(dbgs() << "LRR: Aborting - Root and Base #users not the same: "
818 << "#Base=" << NumBaseUses
819 << ", #Root=" << KV.second->getNumUses() << "\n");
820 return false;
821 }
822 }
823
824 return true;
825}
826
827void LoopReroll::DAGRootTracker::
828findRootsRecursive(Instruction *I, SmallInstructionSet SubsumedInsts) {
829 // Does the user look like it could be part of a root set?
830 // All its users must be simple arithmetic ops.
831 if (I->hasNUsesOrMore(IL_MaxRerollIterations + 1))
832 return;
833
834 if (I != IV && findRootsBase(I, SubsumedInsts))
835 return;
836
837 SubsumedInsts.insert(I);
838
839 for (User *V : I->users()) {
840 Instruction *I = cast<Instruction>(V);
841 if (is_contained(LoopIncs, I))
842 continue;
843
845 continue;
846
847 // The recursive call makes a copy of SubsumedInsts.
848 findRootsRecursive(I, SubsumedInsts);
849 }
850}
851
852bool LoopReroll::DAGRootTracker::validateRootSet(DAGRootSet &DRS) {
853 if (DRS.Roots.empty())
854 return false;
855
856 // If the value of the base instruction is used outside the loop, we cannot
857 // reroll the loop. Check for other root instructions is unnecessary because
858 // they don't match any base instructions if their values are used outside.
859 if (hasUsesOutsideLoop(DRS.BaseInst, L))
860 return false;
861
862 // Consider a DAGRootSet with N-1 roots (so N different values including
863 // BaseInst).
864 // Define d = Roots[0] - BaseInst, which should be the same as
865 // Roots[I] - Roots[I-1] for all I in [1..N).
866 // Define D = BaseInst@J - BaseInst@J-1, where "@J" means the value at the
867 // loop iteration J.
868 //
869 // Now, For the loop iterations to be consecutive:
870 // D = d * N
871 const auto *ADR = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(DRS.BaseInst));
872 if (!ADR)
873 return false;
874
875 // Check that the first root is evenly spaced.
876 unsigned N = DRS.Roots.size() + 1;
877 const SCEV *StepSCEV = SE->getMinusSCEV(SE->getSCEV(DRS.Roots[0]), ADR);
878 if (isa<SCEVCouldNotCompute>(StepSCEV) || StepSCEV->getType()->isPointerTy())
879 return false;
880 const SCEV *ScaleSCEV = SE->getConstant(StepSCEV->getType(), N);
881 if (ADR->getStepRecurrence(*SE) != SE->getMulExpr(StepSCEV, ScaleSCEV))
882 return false;
883
884 // Check that the remainling roots are evenly spaced.
885 for (unsigned i = 1; i < N - 1; ++i) {
886 const SCEV *NewStepSCEV = SE->getMinusSCEV(SE->getSCEV(DRS.Roots[i]),
887 SE->getSCEV(DRS.Roots[i-1]));
888 if (NewStepSCEV != StepSCEV)
889 return false;
890 }
891
892 return true;
893}
894
895bool LoopReroll::DAGRootTracker::
896findRootsBase(Instruction *IVU, SmallInstructionSet SubsumedInsts) {
897 // The base of a RootSet must be an AddRec, so it can be erased.
898 const auto *IVU_ADR = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(IVU));
899 if (!IVU_ADR || IVU_ADR->getLoop() != L)
900 return false;
901
902 std::map<int64_t, Instruction*> V;
903 if (!collectPossibleRoots(IVU, V))
904 return false;
905
906 // If we didn't get a root for index zero, then IVU must be
907 // subsumed.
908 if (V.find(0) == V.end())
909 SubsumedInsts.insert(IVU);
910
911 // Partition the vector into monotonically increasing indexes.
912 DAGRootSet DRS;
913 DRS.BaseInst = nullptr;
914
915 SmallVector<DAGRootSet, 16> PotentialRootSets;
916
917 for (auto &KV : V) {
918 if (!DRS.BaseInst) {
919 DRS.BaseInst = KV.second;
920 DRS.SubsumedInsts = SubsumedInsts;
921 } else if (DRS.Roots.empty()) {
922 DRS.Roots.push_back(KV.second);
923 } else if (V.find(KV.first - 1) != V.end()) {
924 DRS.Roots.push_back(KV.second);
925 } else {
926 // Linear sequence terminated.
927 if (!validateRootSet(DRS))
928 return false;
929
930 // Construct a new DAGRootSet with the next sequence.
931 PotentialRootSets.push_back(DRS);
932 DRS.BaseInst = KV.second;
933 DRS.Roots.clear();
934 }
935 }
936
937 if (!validateRootSet(DRS))
938 return false;
939
940 PotentialRootSets.push_back(DRS);
941
942 RootSets.append(PotentialRootSets.begin(), PotentialRootSets.end());
943
944 return true;
945}
946
947bool LoopReroll::DAGRootTracker::findRoots() {
948 Inc = IVToIncMap[IV];
949
950 assert(RootSets.empty() && "Unclean state!");
951 if (std::abs(Inc) == 1) {
952 for (auto *IVU : IV->users()) {
953 if (isLoopIncrement(IVU, IV))
954 LoopIncs.push_back(cast<Instruction>(IVU));
955 }
956 findRootsRecursive(IV, SmallInstructionSet());
957 LoopIncs.push_back(IV);
958 } else {
959 if (!findRootsBase(IV, SmallInstructionSet()))
960 return false;
961 }
962
963 // Ensure all sets have the same size.
964 if (RootSets.empty()) {
965 LLVM_DEBUG(dbgs() << "LRR: Aborting because no root sets found!\n");
966 return false;
967 }
968 for (auto &V : RootSets) {
969 if (V.Roots.empty() || V.Roots.size() != RootSets[0].Roots.size()) {
971 dbgs()
972 << "LRR: Aborting because not all root sets have the same size\n");
973 return false;
974 }
975 }
976
977 Scale = RootSets[0].Roots.size() + 1;
978
979 if (Scale > IL_MaxRerollIterations) {
980 LLVM_DEBUG(dbgs() << "LRR: Aborting - too many iterations found. "
981 << "#Found=" << Scale
982 << ", #Max=" << IL_MaxRerollIterations << "\n");
983 return false;
984 }
985
986 LLVM_DEBUG(dbgs() << "LRR: Successfully found roots: Scale=" << Scale
987 << "\n");
988
989 return true;
990}
991
992bool LoopReroll::DAGRootTracker::collectUsedInstructions(SmallInstructionSet &PossibleRedSet) {
993 // Populate the MapVector with all instructions in the block, in order first,
994 // so we can iterate over the contents later in perfect order.
995 for (auto &I : *L->getHeader()) {
996 Uses[&I].resize(IL_End);
997 }
998
999 SmallInstructionSet Exclude;
1000 for (auto &DRS : RootSets) {
1001 Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
1002 Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
1003 Exclude.insert(DRS.BaseInst);
1004 }
1005 Exclude.insert(LoopIncs.begin(), LoopIncs.end());
1006
1007 for (auto &DRS : RootSets) {
1009 collectInLoopUserSet(DRS.BaseInst, Exclude, PossibleRedSet, VBase);
1010 for (auto *I : VBase) {
1011 Uses[I].set(0);
1012 }
1013
1014 unsigned Idx = 1;
1015 for (auto *Root : DRS.Roots) {
1017 collectInLoopUserSet(Root, Exclude, PossibleRedSet, V);
1018
1019 // While we're here, check the use sets are the same size.
1020 if (V.size() != VBase.size()) {
1021 LLVM_DEBUG(dbgs() << "LRR: Aborting - use sets are different sizes\n");
1022 return false;
1023 }
1024
1025 for (auto *I : V) {
1026 Uses[I].set(Idx);
1027 }
1028 ++Idx;
1029 }
1030
1031 // Make sure our subsumed instructions are remembered too.
1032 for (auto *I : DRS.SubsumedInsts) {
1033 Uses[I].set(IL_All);
1034 }
1035 }
1036
1037 // Make sure the loop increments are also accounted for.
1038
1039 Exclude.clear();
1040 for (auto &DRS : RootSets) {
1041 Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
1042 Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
1043 Exclude.insert(DRS.BaseInst);
1044 }
1045
1047 collectInLoopUserSet(LoopIncs, Exclude, PossibleRedSet, V);
1048 for (auto *I : V) {
1049 if (I->mayHaveSideEffects()) {
1050 LLVM_DEBUG(dbgs() << "LRR: Aborting - "
1051 << "An instruction which does not belong to any root "
1052 << "sets must not have side effects: " << *I);
1053 return false;
1054 }
1055 Uses[I].set(IL_All);
1056 }
1057
1058 return true;
1059}
1060
1061/// Get the next instruction in "In" that is a member of set Val.
1062/// Start searching from StartI, and do not return anything in Exclude.
1063/// If StartI is not given, start from In.begin().
1065LoopReroll::DAGRootTracker::nextInstr(int Val, UsesTy &In,
1066 const SmallInstructionSet &Exclude,
1067 UsesTy::iterator *StartI) {
1068 UsesTy::iterator I = StartI ? *StartI : In.begin();
1069 while (I != In.end() && (I->second.test(Val) == 0 ||
1070 Exclude.contains(I->first)))
1071 ++I;
1072 return I;
1073}
1074
1075bool LoopReroll::DAGRootTracker::isBaseInst(Instruction *I) {
1076 for (auto &DRS : RootSets) {
1077 if (DRS.BaseInst == I)
1078 return true;
1079 }
1080 return false;
1081}
1082
1083bool LoopReroll::DAGRootTracker::isRootInst(Instruction *I) {
1084 for (auto &DRS : RootSets) {
1085 if (is_contained(DRS.Roots, I))
1086 return true;
1087 }
1088 return false;
1089}
1090
1091/// Return true if instruction I depends on any instruction between
1092/// Start and End.
1093bool LoopReroll::DAGRootTracker::instrDependsOn(Instruction *I,
1094 UsesTy::iterator Start,
1095 UsesTy::iterator End) {
1096 for (auto *U : I->users()) {
1097 for (auto It = Start; It != End; ++It)
1098 if (U == It->first)
1099 return true;
1100 }
1101 return false;
1102}
1103
1104static bool isIgnorableInst(const Instruction *I) {
1105 if (isa<DbgInfoIntrinsic>(I))
1106 return true;
1107 const IntrinsicInst* II = dyn_cast<IntrinsicInst>(I);
1108 if (!II)
1109 return false;
1110 switch (II->getIntrinsicID()) {
1111 default:
1112 return false;
1113 case Intrinsic::annotation:
1114 case Intrinsic::ptr_annotation:
1115 case Intrinsic::var_annotation:
1116 // TODO: the following intrinsics may also be allowed:
1117 // lifetime_start, lifetime_end, invariant_start, invariant_end
1118 return true;
1119 }
1120 return false;
1121}
1122
1123bool LoopReroll::DAGRootTracker::validate(ReductionTracker &Reductions) {
1124 // We now need to check for equivalence of the use graph of each root with
1125 // that of the primary induction variable (excluding the roots). Our goal
1126 // here is not to solve the full graph isomorphism problem, but rather to
1127 // catch common cases without a lot of work. As a result, we will assume
1128 // that the relative order of the instructions in each unrolled iteration
1129 // is the same (although we will not make an assumption about how the
1130 // different iterations are intermixed). Note that while the order must be
1131 // the same, the instructions may not be in the same basic block.
1132
1133 // An array of just the possible reductions for this scale factor. When we
1134 // collect the set of all users of some root instructions, these reduction
1135 // instructions are treated as 'final' (their uses are not considered).
1136 // This is important because we don't want the root use set to search down
1137 // the reduction chain.
1138 SmallInstructionSet PossibleRedSet;
1139 SmallInstructionSet PossibleRedLastSet;
1140 SmallInstructionSet PossibleRedPHISet;
1141 Reductions.restrictToScale(Scale, PossibleRedSet,
1142 PossibleRedPHISet, PossibleRedLastSet);
1143
1144 // Populate "Uses" with where each instruction is used.
1145 if (!collectUsedInstructions(PossibleRedSet))
1146 return false;
1147
1148 // Make sure we mark the reduction PHIs as used in all iterations.
1149 for (auto *I : PossibleRedPHISet) {
1150 Uses[I].set(IL_All);
1151 }
1152
1153 // Make sure we mark loop-control-only PHIs as used in all iterations. See
1154 // comment above LoopReroll::isLoopControlIV for more information.
1155 BasicBlock *Header = L->getHeader();
1156 for (Instruction *LoopControlIV : LoopControlIVs) {
1157 for (auto *U : LoopControlIV->users()) {
1158 Instruction *IVUser = dyn_cast<Instruction>(U);
1159 // IVUser could be loop increment or compare
1160 Uses[IVUser].set(IL_All);
1161 for (auto *UU : IVUser->users()) {
1162 Instruction *UUser = dyn_cast<Instruction>(UU);
1163 // UUser could be compare, PHI or branch
1164 Uses[UUser].set(IL_All);
1165 // Skip SExt
1166 if (isa<SExtInst>(UUser)) {
1167 UUser = dyn_cast<Instruction>(*(UUser->user_begin()));
1168 Uses[UUser].set(IL_All);
1169 }
1170 // Is UUser a compare instruction?
1171 if (UU->hasOneUse()) {
1172 Instruction *BI = dyn_cast<BranchInst>(*UUser->user_begin());
1173 if (BI == cast<BranchInst>(Header->getTerminator()))
1174 Uses[BI].set(IL_All);
1175 }
1176 }
1177 }
1178 }
1179
1180 // Make sure all instructions in the loop are in one and only one
1181 // set.
1182 for (auto &KV : Uses) {
1183 if (KV.second.count() != 1 && !isIgnorableInst(KV.first)) {
1184 LLVM_DEBUG(
1185 dbgs() << "LRR: Aborting - instruction is not used in 1 iteration: "
1186 << *KV.first << " (#uses=" << KV.second.count() << ")\n");
1187 return false;
1188 }
1189 }
1190
1191 LLVM_DEBUG(for (auto &KV
1192 : Uses) {
1193 dbgs() << "LRR: " << KV.second.find_first() << "\t" << *KV.first << "\n";
1194 });
1195
1196 BatchAAResults BatchAA(*AA);
1197 for (unsigned Iter = 1; Iter < Scale; ++Iter) {
1198 // In addition to regular aliasing information, we need to look for
1199 // instructions from later (future) iterations that have side effects
1200 // preventing us from reordering them past other instructions with side
1201 // effects.
1202 bool FutureSideEffects = false;
1203 AliasSetTracker AST(BatchAA);
1204 // The map between instructions in f(%iv.(i+1)) and f(%iv).
1206
1207 // Compare iteration Iter to the base.
1208 SmallInstructionSet Visited;
1209 auto BaseIt = nextInstr(0, Uses, Visited);
1210 auto RootIt = nextInstr(Iter, Uses, Visited);
1211 auto LastRootIt = Uses.begin();
1212
1213 while (BaseIt != Uses.end() && RootIt != Uses.end()) {
1214 Instruction *BaseInst = BaseIt->first;
1215 Instruction *RootInst = RootIt->first;
1216
1217 // Skip over the IV or root instructions; only match their users.
1218 bool Continue = false;
1219 if (isBaseInst(BaseInst)) {
1220 Visited.insert(BaseInst);
1221 BaseIt = nextInstr(0, Uses, Visited);
1222 Continue = true;
1223 }
1224 if (isRootInst(RootInst)) {
1225 LastRootIt = RootIt;
1226 Visited.insert(RootInst);
1227 RootIt = nextInstr(Iter, Uses, Visited);
1228 Continue = true;
1229 }
1230 if (Continue) continue;
1231
1232 if (!BaseInst->isSameOperationAs(RootInst)) {
1233 // Last chance saloon. We don't try and solve the full isomorphism
1234 // problem, but try and at least catch the case where two instructions
1235 // *of different types* are round the wrong way. We won't be able to
1236 // efficiently tell, given two ADD instructions, which way around we
1237 // should match them, but given an ADD and a SUB, we can at least infer
1238 // which one is which.
1239 //
1240 // This should allow us to deal with a greater subset of the isomorphism
1241 // problem. It does however change a linear algorithm into a quadratic
1242 // one, so limit the number of probes we do.
1243 auto TryIt = RootIt;
1244 unsigned N = NumToleratedFailedMatches;
1245 while (TryIt != Uses.end() &&
1246 !BaseInst->isSameOperationAs(TryIt->first) &&
1247 N--) {
1248 ++TryIt;
1249 TryIt = nextInstr(Iter, Uses, Visited, &TryIt);
1250 }
1251
1252 if (TryIt == Uses.end() || TryIt == RootIt ||
1253 instrDependsOn(TryIt->first, RootIt, TryIt)) {
1254 LLVM_DEBUG(dbgs() << "LRR: iteration root match failed at "
1255 << *BaseInst << " vs. " << *RootInst << "\n");
1256 return false;
1257 }
1258
1259 RootIt = TryIt;
1260 RootInst = TryIt->first;
1261 }
1262
1263 // All instructions between the last root and this root
1264 // may belong to some other iteration. If they belong to a
1265 // future iteration, then they're dangerous to alias with.
1266 //
1267 // Note that because we allow a limited amount of flexibility in the order
1268 // that we visit nodes, LastRootIt might be *before* RootIt, in which
1269 // case we've already checked this set of instructions so we shouldn't
1270 // do anything.
1271 for (; LastRootIt < RootIt; ++LastRootIt) {
1272 Instruction *I = LastRootIt->first;
1273 if (LastRootIt->second.find_first() < (int)Iter)
1274 continue;
1275 if (I->mayWriteToMemory())
1276 AST.add(I);
1277 // Note: This is specifically guarded by a check on isa<PHINode>,
1278 // which while a valid (somewhat arbitrary) micro-optimization, is
1279 // needed because otherwise isSafeToSpeculativelyExecute returns
1280 // false on PHI nodes.
1281 if (!isa<PHINode>(I) && !isUnorderedLoadStore(I) &&
1283 // Intervening instructions cause side effects.
1284 FutureSideEffects = true;
1285 }
1286
1287 // Make sure that this instruction, which is in the use set of this
1288 // root instruction, does not also belong to the base set or the set of
1289 // some other root instruction.
1290 if (RootIt->second.count() > 1) {
1291 LLVM_DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst
1292 << " vs. " << *RootInst << " (prev. case overlap)\n");
1293 return false;
1294 }
1295
1296 // Make sure that we don't alias with any instruction in the alias set
1297 // tracker. If we do, then we depend on a future iteration, and we
1298 // can't reroll.
1299 if (RootInst->mayReadFromMemory()) {
1300 for (auto &K : AST) {
1301 if (isModOrRefSet(K.aliasesUnknownInst(RootInst, BatchAA))) {
1302 LLVM_DEBUG(dbgs() << "LRR: iteration root match failed at "
1303 << *BaseInst << " vs. " << *RootInst
1304 << " (depends on future store)\n");
1305 return false;
1306 }
1307 }
1308 }
1309
1310 // If we've past an instruction from a future iteration that may have
1311 // side effects, and this instruction might also, then we can't reorder
1312 // them, and this matching fails. As an exception, we allow the alias
1313 // set tracker to handle regular (unordered) load/store dependencies.
1314 if (FutureSideEffects && ((!isUnorderedLoadStore(BaseInst) &&
1315 !isSafeToSpeculativelyExecute(BaseInst)) ||
1316 (!isUnorderedLoadStore(RootInst) &&
1317 !isSafeToSpeculativelyExecute(RootInst)))) {
1318 LLVM_DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst
1319 << " vs. " << *RootInst
1320 << " (side effects prevent reordering)\n");
1321 return false;
1322 }
1323
1324 // For instructions that are part of a reduction, if the operation is
1325 // associative, then don't bother matching the operands (because we
1326 // already know that the instructions are isomorphic, and the order
1327 // within the iteration does not matter). For non-associative reductions,
1328 // we do need to match the operands, because we need to reject
1329 // out-of-order instructions within an iteration!
1330 // For example (assume floating-point addition), we need to reject this:
1331 // x += a[i]; x += b[i];
1332 // x += a[i+1]; x += b[i+1];
1333 // x += b[i+2]; x += a[i+2];
1334 bool InReduction = Reductions.isPairInSame(BaseInst, RootInst);
1335
1336 if (!(InReduction && BaseInst->isAssociative())) {
1337 bool Swapped = false, SomeOpMatched = false;
1338 for (unsigned j = 0; j < BaseInst->getNumOperands(); ++j) {
1339 Value *Op2 = RootInst->getOperand(j);
1340
1341 // If this is part of a reduction (and the operation is not
1342 // associatve), then we match all operands, but not those that are
1343 // part of the reduction.
1344 if (InReduction)
1345 if (Instruction *Op2I = dyn_cast<Instruction>(Op2))
1346 if (Reductions.isPairInSame(RootInst, Op2I))
1347 continue;
1348
1349 DenseMap<Value *, Value *>::iterator BMI = BaseMap.find(Op2);
1350 if (BMI != BaseMap.end()) {
1351 Op2 = BMI->second;
1352 } else {
1353 for (auto &DRS : RootSets) {
1354 if (DRS.Roots[Iter-1] == (Instruction*) Op2) {
1355 Op2 = DRS.BaseInst;
1356 break;
1357 }
1358 }
1359 }
1360
1361 if (BaseInst->getOperand(Swapped ? unsigned(!j) : j) != Op2) {
1362 // If we've not already decided to swap the matched operands, and
1363 // we've not already matched our first operand (note that we could
1364 // have skipped matching the first operand because it is part of a
1365 // reduction above), and the instruction is commutative, then try
1366 // the swapped match.
1367 if (!Swapped && BaseInst->isCommutative() && !SomeOpMatched &&
1368 BaseInst->getOperand(!j) == Op2) {
1369 Swapped = true;
1370 } else {
1372 << "LRR: iteration root match failed at " << *BaseInst
1373 << " vs. " << *RootInst << " (operand " << j << ")\n");
1374 return false;
1375 }
1376 }
1377
1378 SomeOpMatched = true;
1379 }
1380 }
1381
1382 if ((!PossibleRedLastSet.count(BaseInst) &&
1383 hasUsesOutsideLoop(BaseInst, L)) ||
1384 (!PossibleRedLastSet.count(RootInst) &&
1385 hasUsesOutsideLoop(RootInst, L))) {
1386 LLVM_DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst
1387 << " vs. " << *RootInst << " (uses outside loop)\n");
1388 return false;
1389 }
1390
1391 Reductions.recordPair(BaseInst, RootInst, Iter);
1392 BaseMap.insert(std::make_pair(RootInst, BaseInst));
1393
1394 LastRootIt = RootIt;
1395 Visited.insert(BaseInst);
1396 Visited.insert(RootInst);
1397 BaseIt = nextInstr(0, Uses, Visited);
1398 RootIt = nextInstr(Iter, Uses, Visited);
1399 }
1400 assert(BaseIt == Uses.end() && RootIt == Uses.end() &&
1401 "Mismatched set sizes!");
1402 }
1403
1404 LLVM_DEBUG(dbgs() << "LRR: Matched all iteration increments for " << *IV
1405 << "\n");
1406
1407 return true;
1408}
1409
1410void LoopReroll::DAGRootTracker::replace(const SCEV *BackedgeTakenCount) {
1411 BasicBlock *Header = L->getHeader();
1412
1413 // Compute the start and increment for each BaseInst before we start erasing
1414 // instructions.
1417 for (auto &DRS : RootSets) {
1418 const SCEVAddRecExpr *IVSCEV =
1419 cast<SCEVAddRecExpr>(SE->getSCEV(DRS.BaseInst));
1420 StartExprs.push_back(IVSCEV->getStart());
1421 IncrExprs.push_back(SE->getMinusSCEV(SE->getSCEV(DRS.Roots[0]), IVSCEV));
1422 }
1423
1424 // Remove instructions associated with non-base iterations.
1425 for (Instruction &Inst : llvm::make_early_inc_range(llvm::reverse(*Header))) {
1426 unsigned I = Uses[&Inst].find_first();
1427 if (I > 0 && I < IL_All) {
1428 LLVM_DEBUG(dbgs() << "LRR: removing: " << Inst << "\n");
1429 Inst.eraseFromParent();
1430 }
1431 }
1432
1433 // Rewrite each BaseInst using SCEV.
1434 for (size_t i = 0, e = RootSets.size(); i != e; ++i)
1435 // Insert the new induction variable.
1436 replaceIV(RootSets[i], StartExprs[i], IncrExprs[i]);
1437
1438 { // Limit the lifetime of SCEVExpander.
1439 BranchInst *BI = cast<BranchInst>(Header->getTerminator());
1440 const DataLayout &DL = Header->getModule()->getDataLayout();
1441 SCEVExpander Expander(*SE, DL, "reroll");
1442 auto Zero = SE->getZero(BackedgeTakenCount->getType());
1443 auto One = SE->getOne(BackedgeTakenCount->getType());
1444 auto NewIVSCEV = SE->getAddRecExpr(Zero, One, L, SCEV::FlagAnyWrap);
1445 Value *NewIV =
1446 Expander.expandCodeFor(NewIVSCEV, BackedgeTakenCount->getType(),
1447 Header->getFirstNonPHIOrDbg());
1448 // FIXME: This arithmetic can overflow.
1449 auto TripCount = SE->getAddExpr(BackedgeTakenCount, One);
1450 auto ScaledTripCount = SE->getMulExpr(
1451 TripCount, SE->getConstant(BackedgeTakenCount->getType(), Scale));
1452 auto ScaledBECount = SE->getMinusSCEV(ScaledTripCount, One);
1453 Value *TakenCount =
1454 Expander.expandCodeFor(ScaledBECount, BackedgeTakenCount->getType(),
1455 Header->getFirstNonPHIOrDbg());
1456 Value *Cond =
1457 new ICmpInst(BI, CmpInst::ICMP_EQ, NewIV, TakenCount, "exitcond");
1458 BI->setCondition(Cond);
1459
1460 if (BI->getSuccessor(1) != Header)
1461 BI->swapSuccessors();
1462 }
1463
1464 SimplifyInstructionsInBlock(Header, TLI);
1465 DeleteDeadPHIs(Header, TLI);
1466}
1467
1468void LoopReroll::DAGRootTracker::replaceIV(DAGRootSet &DRS,
1469 const SCEV *Start,
1470 const SCEV *IncrExpr) {
1471 BasicBlock *Header = L->getHeader();
1472 Instruction *Inst = DRS.BaseInst;
1473
1474 const SCEV *NewIVSCEV =
1475 SE->getAddRecExpr(Start, IncrExpr, L, SCEV::FlagAnyWrap);
1476
1477 { // Limit the lifetime of SCEVExpander.
1478 const DataLayout &DL = Header->getModule()->getDataLayout();
1479 SCEVExpander Expander(*SE, DL, "reroll");
1480 Value *NewIV = Expander.expandCodeFor(NewIVSCEV, Inst->getType(),
1481 Header->getFirstNonPHIOrDbg());
1482
1483 for (auto &KV : Uses)
1484 if (KV.second.find_first() == 0)
1485 KV.first->replaceUsesOfWith(Inst, NewIV);
1486 }
1487}
1488
1489// Validate the selected reductions. All iterations must have an isomorphic
1490// part of the reduction chain and, for non-associative reductions, the chain
1491// entries must appear in order.
1492bool LoopReroll::ReductionTracker::validateSelected() {
1493 // For a non-associative reduction, the chain entries must appear in order.
1494 for (int i : Reds) {
1495 int PrevIter = 0, BaseCount = 0, Count = 0;
1496 for (Instruction *J : PossibleReds[i]) {
1497 // Note that all instructions in the chain must have been found because
1498 // all instructions in the function must have been assigned to some
1499 // iteration.
1500 int Iter = PossibleRedIter[J];
1501 if (Iter != PrevIter && Iter != PrevIter + 1 &&
1502 !PossibleReds[i].getReducedValue()->isAssociative()) {
1503 LLVM_DEBUG(dbgs() << "LRR: Out-of-order non-associative reduction: "
1504 << J << "\n");
1505 return false;
1506 }
1507
1508 if (Iter != PrevIter) {
1509 if (Count != BaseCount) {
1511 << "LRR: Iteration " << PrevIter << " reduction use count "
1512 << Count << " is not equal to the base use count "
1513 << BaseCount << "\n");
1514 return false;
1515 }
1516
1517 Count = 0;
1518 }
1519
1520 ++Count;
1521 if (Iter == 0)
1522 ++BaseCount;
1523
1524 PrevIter = Iter;
1525 }
1526 }
1527
1528 return true;
1529}
1530
1531// For all selected reductions, remove all parts except those in the first
1532// iteration (and the PHI). Replace outside uses of the reduced value with uses
1533// of the first-iteration reduced value (in other words, reroll the selected
1534// reductions).
1535void LoopReroll::ReductionTracker::replaceSelected() {
1536 // Fixup reductions to refer to the last instruction associated with the
1537 // first iteration (not the last).
1538 for (int i : Reds) {
1539 int j = 0;
1540 for (int e = PossibleReds[i].size(); j != e; ++j)
1541 if (PossibleRedIter[PossibleReds[i][j]] != 0) {
1542 --j;
1543 break;
1544 }
1545
1546 // Replace users with the new end-of-chain value.
1547 SmallInstructionVector Users;
1548 for (User *U : PossibleReds[i].getReducedValue()->users()) {
1549 Users.push_back(cast<Instruction>(U));
1550 }
1551
1552 for (Instruction *User : Users)
1553 User->replaceUsesOfWith(PossibleReds[i].getReducedValue(),
1554 PossibleReds[i][j]);
1555 }
1556}
1557
1558// Reroll the provided loop with respect to the provided induction variable.
1559// Generally, we're looking for a loop like this:
1560//
1561// %iv = phi [ (preheader, ...), (body, %iv.next) ]
1562// f(%iv)
1563// %iv.1 = add %iv, 1 <-- a root increment
1564// f(%iv.1)
1565// %iv.2 = add %iv, 2 <-- a root increment
1566// f(%iv.2)
1567// %iv.scale_m_1 = add %iv, scale-1 <-- a root increment
1568// f(%iv.scale_m_1)
1569// ...
1570// %iv.next = add %iv, scale
1571// %cmp = icmp(%iv, ...)
1572// br %cmp, header, exit
1573//
1574// Notably, we do not require that f(%iv), f(%iv.1), etc. be isolated groups of
1575// instructions. In other words, the instructions in f(%iv), f(%iv.1), etc. can
1576// be intermixed with eachother. The restriction imposed by this algorithm is
1577// that the relative order of the isomorphic instructions in f(%iv), f(%iv.1),
1578// etc. be the same.
1579//
1580// First, we collect the use set of %iv, excluding the other increment roots.
1581// This gives us f(%iv). Then we iterate over the loop instructions (scale-1)
1582// times, having collected the use set of f(%iv.(i+1)), during which we:
1583// - Ensure that the next unmatched instruction in f(%iv) is isomorphic to
1584// the next unmatched instruction in f(%iv.(i+1)).
1585// - Ensure that both matched instructions don't have any external users
1586// (with the exception of last-in-chain reduction instructions).
1587// - Track the (aliasing) write set, and other side effects, of all
1588// instructions that belong to future iterations that come before the matched
1589// instructions. If the matched instructions read from that write set, then
1590// f(%iv) or f(%iv.(i+1)) has some dependency on instructions in
1591// f(%iv.(j+1)) for some j > i, and we cannot reroll the loop. Similarly,
1592// if any of these future instructions had side effects (could not be
1593// speculatively executed), and so do the matched instructions, when we
1594// cannot reorder those side-effect-producing instructions, and rerolling
1595// fails.
1596//
1597// Finally, we make sure that all loop instructions are either loop increment
1598// roots, belong to simple latch code, parts of validated reductions, part of
1599// f(%iv) or part of some f(%iv.i). If all of that is true (and all reductions
1600// have been validated), then we reroll the loop.
1601bool LoopReroll::reroll(Instruction *IV, Loop *L, BasicBlock *Header,
1602 const SCEV *BackedgeTakenCount,
1603 ReductionTracker &Reductions) {
1604 DAGRootTracker DAGRoots(this, L, IV, SE, AA, TLI, DT, LI, PreserveLCSSA,
1605 IVToIncMap, LoopControlIVs);
1606
1607 if (!DAGRoots.findRoots())
1608 return false;
1609 LLVM_DEBUG(dbgs() << "LRR: Found all root induction increments for: " << *IV
1610 << "\n");
1611
1612 if (!DAGRoots.validate(Reductions))
1613 return false;
1614 if (!Reductions.validateSelected())
1615 return false;
1616 // At this point, we've validated the rerolling, and we're committed to
1617 // making changes!
1618
1619 Reductions.replaceSelected();
1620 DAGRoots.replace(BackedgeTakenCount);
1621
1622 ++NumRerolledLoops;
1623 return true;
1624}
1625
1626bool LoopReroll::runOnLoop(Loop *L) {
1627 BasicBlock *Header = L->getHeader();
1628 LLVM_DEBUG(dbgs() << "LRR: F[" << Header->getParent()->getName() << "] Loop %"
1629 << Header->getName() << " (" << L->getNumBlocks()
1630 << " block(s))\n");
1631
1632 // For now, we'll handle only single BB loops.
1633 if (L->getNumBlocks() > 1)
1634 return false;
1635
1637 return false;
1638
1639 const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L);
1640 LLVM_DEBUG(dbgs() << "\n Before Reroll:\n" << *(L->getHeader()) << "\n");
1641 LLVM_DEBUG(dbgs() << "LRR: backedge-taken count = " << *BackedgeTakenCount
1642 << "\n");
1643
1644 // First, we need to find the induction variable with respect to which we can
1645 // reroll (there may be several possible options).
1646 SmallInstructionVector PossibleIVs;
1647 IVToIncMap.clear();
1648 LoopControlIVs.clear();
1649 collectPossibleIVs(L, PossibleIVs);
1650
1651 if (PossibleIVs.empty()) {
1652 LLVM_DEBUG(dbgs() << "LRR: No possible IVs found\n");
1653 return false;
1654 }
1655
1656 ReductionTracker Reductions;
1657 collectPossibleReductions(L, Reductions);
1658 bool Changed = false;
1659
1660 // For each possible IV, collect the associated possible set of 'root' nodes
1661 // (i+1, i+2, etc.).
1662 for (Instruction *PossibleIV : PossibleIVs)
1663 if (reroll(PossibleIV, L, Header, BackedgeTakenCount, Reductions)) {
1664 Changed = true;
1665 break;
1666 }
1667 LLVM_DEBUG(dbgs() << "\n After Reroll:\n" << *(L->getHeader()) << "\n");
1668
1669 // Trip count of L has changed so SE must be re-evaluated.
1670 if (Changed)
1671 SE->forgetLoop(L);
1672
1673 return Changed;
1674}
1675
1678 LPMUpdater &U) {
1679 return LoopReroll(&AR.AA, &AR.LI, &AR.SE, &AR.TLI, &AR.DT, true).runOnLoop(&L)
1682}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements a class to represent arbitrary precision integral constant values and operations...
SmallPtrSet< MachineInstr *, 2 > Uses
SmallVector< MachineOperand, 4 > Cond
This file implements the BitVector class.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
Hexagon Common GEP
IRTranslator LLVM IR MI
iv Induction Variable Users
Definition: IVUsers.cpp:48
iv users
Definition: IVUsers.cpp:48
static bool isIgnorableInst(const Instruction *I)
static cl::opt< unsigned > NumToleratedFailedMatches("reroll-num-tolerated-failed-matches", cl::init(400), cl::Hidden, cl::desc("The maximum number of failures to tolerate" " during fuzzy matching. (default: 400)"))
static bool isLoopIncrement(User *U, Instruction *IV)
static bool isSimpleArithmeticOp(User *IVU)
Return true if IVU is a "simple" arithmetic operation.
static bool isUnorderedLoadStore(Instruction *I)
static bool hasUsesOutsideLoop(Instruction *I, Loop *L)
#define I(x, y, z)
Definition: MD5.cpp:58
This file implements a map that provides insertion order iteration.
Module.h This file contains the declarations for the Module class.
#define P(N)
@ SI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:167
This defines the Use class.
static bool isAssociative(const COFFSection &Section)
static const uint32_t IV[8]
Definition: blake3_impl.h:77
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1516
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:87
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
BinaryOps getOpcode() const
Definition: InstrTypes.h:391
Conditional or Unconditional Branch instruction.
void setCondition(Value *V)
void swapSuccessors()
Swap the successors of this branch instruction.
BasicBlock * getSuccessor(unsigned i) const
@ ICMP_EQ
equal
Definition: InstrTypes.h:739
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:136
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
This instruction compares its operands according to the predicate given to the constructor.
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const LLVM_READONLY
This function determines if the specified instruction executes the same operation as the current one.
bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
bool mayReadFromMemory() const LLVM_READONLY
Return true if this instruction may read memory.
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:47
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:54
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
An instruction for reading from memory.
Definition: Instructions.h:177
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:547
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:37
typename VectorType::iterator iterator
Definition: MapVector.h:50
This is the common base class for memset/memcpy/memmove.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
This node represents a polynomial recurrence on the trip count of the specified loop.
This class uses information about analyze scalars to rewrite expressions in canonical form.
This class represents an analyzed expression in the program.
Type * getType() const
Return the LLVM type of this SCEV expression.
The main scalar evolution driver.
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...
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
const SCEV * getConstant(ConstantInt *V)
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.
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...
const SCEV * getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L, SCEV::NoWrapFlags Flags)
Get an add recurrence expression for the specified loop.
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
bool hasLoopInvariantBackedgeTakenCount(const Loop *L)
Return true if the specified loop has an analyzable loop-invariant backedge-taken count.
const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
An instruction for storing to memory.
Definition: Instructions.h:301
Provides information about what library functions are available for the current target.
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:258
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:21
Value * getOperand(unsigned i) const
Definition: User.h:169
unsigned getNumOperands() const
Definition: User.h:191
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
user_iterator user_begin()
Definition: Value.h:397
iterator_range< user_iterator > users()
Definition: Value.h:421
unsigned getNumUses() const
This method computes the number of uses of this Value.
Definition: Value.cpp:254
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
constexpr double e
Definition: MathExtras.h:31
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:226
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:235
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1777
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:748
bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
Scan the specified basic block and try to simplify any instructions in it and recursively delete dead...
Definition: Local.cpp:725
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:511
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
bool isModOrRefSet(const ModRefInfo MRI)
Definition: ModRef.h:42
bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
void replace(Container &Cont, typename Container::iterator ContIt, typename Container::iterator ContEnd, RandomAccessIterator ValIt, RandomAccessIterator ValEnd)
Given a sequence container Cont, replace the range [ContIt, ContEnd) with the range [ValIt,...
Definition: STLExtras.h:2136
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1976
#define N
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...