LLVM 23.0.0git
LoopFuse.cpp
Go to the documentation of this file.
1//===- LoopFuse.cpp - Loop Fusion 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/// \file
10/// This file implements the loop fusion pass.
11/// The implementation is largely based on the following document:
12///
13/// Code Transformations to Augment the Scope of Loop Fusion in a
14/// Production Compiler
15/// Christopher Mark Barton
16/// MSc Thesis
17/// https://webdocs.cs.ualberta.ca/~amaral/thesis/ChristopherBartonMSc.pdf
18///
19/// The general approach taken is to collect sets of control flow equivalent
20/// loops and test whether they can be fused. The necessary conditions for
21/// fusion are:
22/// 1. The loops must be adjacent (there cannot be any statements between
23/// the two loops).
24/// 2. The loops must be conforming (they must execute the same number of
25/// iterations).
26/// 3. The loops must be control flow equivalent (if one loop executes, the
27/// other is guaranteed to execute).
28/// 4. There cannot be any negative distance dependencies between the loops.
29/// If all of these conditions are satisfied, it is safe to fuse the loops.
30///
31/// This implementation creates FusionCandidates that represent the loop and the
32/// necessary information needed by fusion. It then operates on the fusion
33/// candidates, first confirming that the candidate is eligible for fusion. The
34/// candidates are then collected into control flow equivalent sets, sorted in
35/// dominance order. Each set of control flow equivalent candidates is then
36/// traversed, attempting to fuse pairs of candidates in the set. If all
37/// requirements for fusion are met, the two candidates are fused, creating a
38/// new (fused) candidate which is then added back into the set to consider for
39/// additional fusion.
40///
41/// This implementation currently does not make any modifications to remove
42/// conditions for fusion. Code transformations to make loops conform to each of
43/// the conditions for fusion are discussed in more detail in the document
44/// above. These can be added to the current implementation in the future.
45//===----------------------------------------------------------------------===//
46
48#include "llvm/ADT/Statistic.h"
57#include "llvm/IR/Function.h"
58#include "llvm/IR/Verifier.h"
60#include "llvm/Support/Debug.h"
66#include <list>
67
68using namespace llvm;
69
70#define DEBUG_TYPE "loop-fusion"
71
72STATISTIC(FuseCounter, "Loops fused");
73STATISTIC(NumFusionCandidates, "Number of candidates for loop fusion");
74STATISTIC(InvalidLoopStructure, "Loop has invalid structure");
75STATISTIC(AddressTakenBB, "Basic block has address taken");
76STATISTIC(MayThrowException, "Loop may throw an exception");
77STATISTIC(ContainsVolatileAccess, "Loop contains a volatile access");
78STATISTIC(ContainsAtomicAccess, "Loop contains an atomic access");
79STATISTIC(NotSimplifiedForm, "Loop is not in simplified form");
80STATISTIC(InvalidDependencies, "Dependencies prevent fusion");
81STATISTIC(UnknownTripCount, "Loop has unknown trip count");
82STATISTIC(UncomputableTripCount, "SCEV cannot compute trip count of loop");
83STATISTIC(NonEqualTripCount, "Loop trip counts are not the same");
85 NonEmptyPreheader,
86 "Loop has a non-empty preheader with instructions that cannot be moved");
87STATISTIC(FusionNotBeneficial, "Fusion is not beneficial");
88STATISTIC(NonIdenticalGuards, "Candidates have different guards");
89STATISTIC(NonEmptyExitBlock, "Candidate has a non-empty exit block with "
90 "instructions that cannot be moved");
91STATISTIC(NonEmptyGuardBlock, "Candidate has a non-empty guard block with "
92 "instructions that cannot be moved");
93STATISTIC(NotRotated, "Candidate is not rotated");
94STATISTIC(OnlySecondCandidateIsGuarded,
95 "The second candidate is guarded while the first one is not");
96STATISTIC(NumHoistedInsts, "Number of hoisted preheader instructions.");
97STATISTIC(NumSunkInsts, "Number of sunk preheader instructions.");
98STATISTIC(NumDA, "DA checks passed");
99
101 "loop-fusion-peel-max-count", cl::init(0), cl::Hidden,
102 cl::desc("Max number of iterations to be peeled from a loop, such that "
103 "fusion can take place"));
104
105#ifndef NDEBUG
106static cl::opt<bool>
107 VerboseFusionDebugging("loop-fusion-verbose-debug",
108 cl::desc("Enable verbose debugging for Loop Fusion"),
109 cl::Hidden, cl::init(false));
110#endif
111
112namespace {
113/// This class is used to represent a candidate for loop fusion. When it is
114/// constructed, it checks the conditions for loop fusion to ensure that it
115/// represents a valid candidate. It caches several parts of a loop that are
116/// used throughout loop fusion (e.g., loop preheader, loop header, etc) instead
117/// of continually querying the underlying Loop to retrieve these values. It is
118/// assumed these will not change throughout loop fusion.
119///
120/// The invalidate method should be used to indicate that the FusionCandidate is
121/// no longer a valid candidate for fusion. Similarly, the isValid() method can
122/// be used to ensure that the FusionCandidate is still valid for fusion.
123struct FusionCandidate {
124 /// Cache of parts of the loop used throughout loop fusion. These should not
125 /// need to change throughout the analysis and transformation.
126 /// These parts are cached to avoid repeatedly looking up in the Loop class.
127
128 /// Preheader of the loop this candidate represents
129 BasicBlock *Preheader;
130 /// Header of the loop this candidate represents
131 BasicBlock *Header;
132 /// Blocks in the loop that exit the loop
133 BasicBlock *ExitingBlock;
134 /// The successor block of this loop (where the exiting blocks go to)
135 BasicBlock *ExitBlock;
136 /// Latch of the loop
137 BasicBlock *Latch;
138 /// The loop that this fusion candidate represents
139 Loop *L;
140 /// Vector of instructions in this loop that read from memory
142 /// Vector of instructions in this loop that write to memory
144 /// Are all of the members of this fusion candidate still valid
145 bool Valid;
146 /// Guard branch of the loop, if it exists
147 CondBrInst *GuardBranch;
148 /// Peeling Paramaters of the Loop.
150 /// Can you Peel this Loop?
151 bool AbleToPeel;
152 /// Has this loop been Peeled
153 bool Peeled;
154
155 DominatorTree &DT;
156 const PostDominatorTree *PDT;
157
159
160 FusionCandidate(Loop *L, DominatorTree &DT, const PostDominatorTree *PDT,
162 : Preheader(L->getLoopPreheader()), Header(L->getHeader()),
163 ExitingBlock(L->getExitingBlock()), ExitBlock(L->getExitBlock()),
164 Latch(L->getLoopLatch()), L(L), Valid(true),
165 GuardBranch(L->getLoopGuardBranch()), PP(PP), AbleToPeel(canPeel(L)),
166 Peeled(false), DT(DT), PDT(PDT), ORE(ORE) {
167
168 // Walk over all blocks in the loop and check for conditions that may
169 // prevent fusion. For each block, walk over all instructions and collect
170 // the memory reads and writes If any instructions that prevent fusion are
171 // found, invalidate this object and return.
172 for (BasicBlock *BB : L->blocks()) {
173 if (BB->hasAddressTaken()) {
174 invalidate();
175 ++AddressTakenBB;
176 reportInvalidCandidate("AddressTakenBB",
177 "Basic block has address taken");
178 return;
179 }
180
181 for (Instruction &I : *BB) {
182 if (I.mayThrow()) {
183 invalidate();
184 ++MayThrowException;
185 reportInvalidCandidate("MayThrowException",
186 "Loop may throw an exception");
187 return;
188 }
189 if (I.isVolatile()) {
190 invalidate();
191 ++ContainsVolatileAccess;
192 reportInvalidCandidate("ContainsVolatileAccess",
193 "Loop contains a volatile access");
194 return;
195 }
196 // Atomic accesses impose ordering/synchronization constraints that the
197 // dependence analysis used for fusion does not model, so reordering
198 // them across the fused body could be unsafe.
199 if (I.isAtomic()) {
200 invalidate();
201 ++ContainsAtomicAccess;
202 reportInvalidCandidate("ContainsAtomicAccess",
203 "Loop contains an atomic access");
204 return;
205 }
206 if (I.mayWriteToMemory())
207 MemWrites.push_back(&I);
208 if (I.mayReadFromMemory())
209 MemReads.push_back(&I);
210 }
211 }
212 }
213
214 /// Check if all members of the class are valid.
215 bool isValid() const {
216 return Preheader && ExitingBlock && ExitBlock && Latch && L &&
217 !L->isInvalid() && Valid;
218 }
219
220 /// Verify that all members are in sync with the Loop object.
221 void verify() const {
222 assert(isValid() && "Candidate is not valid!!");
223 assert(!L->isInvalid() && "Loop is invalid!");
224 assert(Preheader == L->getLoopPreheader() && "Preheader is out of sync");
225 assert(Header == L->getHeader() && "Header is out of sync");
226 assert(ExitingBlock == L->getExitingBlock() &&
227 "Exiting Blocks is out of sync");
228 assert(ExitBlock == L->getExitBlock() && "Exit block is out of sync");
229 assert(Latch == L->getLoopLatch() && "Latch is out of sync");
230 }
231
232 /// Get the entry block for this fusion candidate.
233 ///
234 /// If this fusion candidate represents a guarded loop, the entry block is the
235 /// loop guard block. If it represents an unguarded loop, the entry block is
236 /// the preheader of the loop.
237 BasicBlock *getEntryBlock() const {
238 if (GuardBranch)
239 return GuardBranch->getParent();
240 return Preheader;
241 }
242
243 /// After Peeling the loop is modified quite a bit, hence all of the Blocks
244 /// need to be updated accordingly.
245 void updateAfterPeeling() {
246 Preheader = L->getLoopPreheader();
247 Header = L->getHeader();
248 ExitingBlock = L->getExitingBlock();
249 ExitBlock = L->getExitBlock();
250 Latch = L->getLoopLatch();
251 verify();
252 }
253
254 /// Given a guarded loop, get the successor of the guard that is not in the
255 /// loop.
256 ///
257 /// This method returns the successor of the loop guard that is not located
258 /// within the loop (i.e., the successor of the guard that is not the
259 /// preheader).
260 /// This method is only valid for guarded loops.
261 BasicBlock *getNonLoopBlock() const {
262 assert(GuardBranch && "Only valid on guarded loops.");
263 if (Peeled)
264 return GuardBranch->getSuccessor(1);
265 return (GuardBranch->getSuccessor(0) == Preheader)
266 ? GuardBranch->getSuccessor(1)
267 : GuardBranch->getSuccessor(0);
268 }
269
270#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
271 LLVM_DUMP_METHOD void dump() const {
272 dbgs() << "\tGuardBranch: ";
273 if (GuardBranch)
274 dbgs() << *GuardBranch;
275 else
276 dbgs() << "nullptr";
277 dbgs() << "\n"
278 << (GuardBranch ? GuardBranch->getName() : "nullptr") << "\n"
279 << "\tPreheader: " << (Preheader ? Preheader->getName() : "nullptr")
280 << "\n"
281 << "\tHeader: " << (Header ? Header->getName() : "nullptr") << "\n"
282 << "\tExitingBB: "
283 << (ExitingBlock ? ExitingBlock->getName() : "nullptr") << "\n"
284 << "\tExitBB: " << (ExitBlock ? ExitBlock->getName() : "nullptr")
285 << "\n"
286 << "\tLatch: " << (Latch ? Latch->getName() : "nullptr") << "\n"
287 << "\tEntryBlock: "
288 << (getEntryBlock() ? getEntryBlock()->getName() : "nullptr")
289 << "\n";
290 }
291#endif
292
293 /// Determine if a fusion candidate (representing a loop) is eligible for
294 /// fusion. Note that this only checks whether a single loop can be fused - it
295 /// does not check whether it is *legal* to fuse two loops together.
296 bool isEligibleForFusion(ScalarEvolution &SE) const {
297 if (!isValid()) {
298 LLVM_DEBUG(dbgs() << "FC has invalid CFG requirements!\n");
299 assert(Header && "Header should be guaranteed to exist!");
300 ++InvalidLoopStructure;
301 return false;
302 }
303
304 // Require ScalarEvolution to be able to determine a trip count.
306 LLVM_DEBUG(dbgs() << "Loop " << L->getName()
307 << " trip count not computable!\n");
308 ++UnknownTripCount;
309 return reportInvalidCandidate("UnknownTripCount",
310 "Loop has unknown trip count");
311 }
312
313 if (!L->isLoopSimplifyForm()) {
314 LLVM_DEBUG(dbgs() << "Loop " << L->getName()
315 << " is not in simplified form!\n");
316 ++NotSimplifiedForm;
317 return reportInvalidCandidate("NotSimplifiedForm",
318 "Loop is not in simplified form");
319 }
320
321 if (!L->isRotatedForm()) {
322 LLVM_DEBUG(dbgs() << "Loop " << L->getName() << " is not rotated!\n");
323 ++NotRotated;
324 return reportInvalidCandidate("NotRotated", "Candidate is not rotated");
325 }
326
327 return true;
328 }
329
330private:
331 // This is only used internally for now, to clear the MemWrites and MemReads
332 // list and setting Valid to false. I can't envision other uses of this right
333 // now, since once FusionCandidates are put into the FusionCandidateList they
334 // are immutable. Thus, any time we need to change/update a FusionCandidate,
335 // we must create a new one and insert it into the FusionCandidateList to
336 // ensure the FusionCandidateList remains ordered correctly.
337 void invalidate() {
338 MemWrites.clear();
339 MemReads.clear();
340 Valid = false;
341 }
342
343 // Emit an analysis remark explaining why this loop cannot be fused. The
344 // remark is built from explicit strings so it does not depend on whether
345 // statistics are enabled. \p RemarkName is the -Rpass remark identifier and
346 // \p RemarkMsg the human-readable reason.
347 bool reportInvalidCandidate(StringRef RemarkName, StringRef RemarkMsg) const {
348 using namespace ore;
349 ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "InvalidCandidate",
350 L->getStartLoc(), L->getHeader())
351 << "Loop is not a candidate for fusion");
352
354 L->getStartLoc(), L->getHeader())
355 << "[" << L->getHeader()->getParent()->getName() << "]: "
356 << "Loop is not a candidate for fusion: " << RemarkMsg);
357 return false;
358 }
359};
360} // namespace
361
363
364// List of adjacent fusion candidates in order. Thus, if FC0 comes *before* FC1
365// in a FusionCandidateList, then FC0 dominates FC1, FC1 post-dominates FC0,
366// and they are adjacent.
367using FusionCandidateList = std::list<FusionCandidate>;
369
370#ifndef NDEBUG
371static void printLoopVector(const LoopVector &LV) {
372 dbgs() << "****************************\n";
373 for (const Loop *L : LV)
374 printLoop(*L, dbgs());
375 dbgs() << "****************************\n";
376}
377
378static raw_ostream &operator<<(raw_ostream &OS, const FusionCandidate &FC) {
379 if (FC.isValid())
380 OS << FC.Preheader->getName();
381 else
382 OS << "<Invalid>";
383
384 return OS;
385}
386
388 const FusionCandidateList &CandList) {
389 for (const FusionCandidate &FC : CandList)
390 OS << FC << '\n';
391
392 return OS;
393}
394
395static void
397 dbgs() << "Fusion Candidates: \n";
398 for (const auto &CandidateList : FusionCandidates) {
399 dbgs() << "*** Fusion Candidate List ***\n";
400 dbgs() << CandidateList;
401 dbgs() << "****************************\n";
402 }
403}
404#endif // NDEBUG
405
406namespace {
407
408/// Collect all loops in function at the same nest level, starting at the
409/// outermost level.
410///
411/// This data structure collects all loops at the same nest level for a
412/// given function (specified by the LoopInfo object). It starts at the
413/// outermost level.
414struct LoopDepthTree {
415 using LoopsOnLevelTy = SmallVector<LoopVector, 4>;
416 using iterator = LoopsOnLevelTy::iterator;
417 using const_iterator = LoopsOnLevelTy::const_iterator;
418
419 LoopDepthTree(LoopInfo &LI) : Depth(1) {
420 if (!LI.empty())
421 LoopsOnLevel.emplace_back(LoopVector(LI.rbegin(), LI.rend()));
422 }
423
424 /// Test whether a given loop has been removed from the function, and thus is
425 /// no longer valid.
426 bool isRemovedLoop(const Loop *L) const { return RemovedLoops.count(L); }
427
428 /// Record that a given loop has been removed from the function and is no
429 /// longer valid.
430 void removeLoop(const Loop *L) { RemovedLoops.insert(L); }
431
432 /// Descend the tree to the next (inner) nesting level
433 void descend() {
434 LoopsOnLevelTy LoopsOnNextLevel;
435
436 for (const LoopVector &LV : *this)
437 for (Loop *L : LV)
438 if (!isRemovedLoop(L) && L->begin() != L->end())
439 LoopsOnNextLevel.emplace_back(LoopVector(L->begin(), L->end()));
440
441 LoopsOnLevel = LoopsOnNextLevel;
442 RemovedLoops.clear();
443 Depth++;
444 }
445
446 bool empty() const { return size() == 0; }
447 size_t size() const { return LoopsOnLevel.size() - RemovedLoops.size(); }
448 unsigned getDepth() const { return Depth; }
449
450 iterator begin() { return LoopsOnLevel.begin(); }
451 iterator end() { return LoopsOnLevel.end(); }
452 const_iterator begin() const { return LoopsOnLevel.begin(); }
453 const_iterator end() const { return LoopsOnLevel.end(); }
454
455private:
456 /// Set of loops that have been removed from the function and are no longer
457 /// valid.
458 SmallPtrSet<const Loop *, 8> RemovedLoops;
459
460 /// Depth of the current level, starting at 1 (outermost loops).
461 unsigned Depth;
462
463 /// Vector of loops at the current depth level that have the same parent loop
464 LoopsOnLevelTy LoopsOnLevel;
465};
466
467struct LoopFuser {
468private:
469 // Sets of control flow equivalent fusion candidates for a given nest level.
470 FusionCandidateCollection FusionCandidates;
471
472 LoopDepthTree LDT;
473 DomTreeUpdater DTU;
474
475 LoopInfo &LI;
476 DominatorTree &DT;
477 DependenceInfo &DI;
478 ScalarEvolution &SE;
479 PostDominatorTree &PDT;
480 OptimizationRemarkEmitter &ORE;
481 AssumptionCache &AC;
482 const TargetTransformInfo &TTI;
483
484public:
485 LoopFuser(LoopInfo &LI, DominatorTree &DT, DependenceInfo &DI,
486 ScalarEvolution &SE, PostDominatorTree &PDT,
487 OptimizationRemarkEmitter &ORE, AssumptionCache &AC,
488 const TargetTransformInfo &TTI)
489 : LDT(LI), DTU(DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy), LI(LI),
490 DT(DT), DI(DI), SE(SE), PDT(PDT), ORE(ORE), AC(AC), TTI(TTI) {}
491
492 /// This is the main entry point for loop fusion. It will traverse the
493 /// specified function and collect candidate loops to fuse, starting at the
494 /// outermost nesting level and working inwards.
495 bool fuseLoops(Function &F) {
496#ifndef NDEBUG
498 LI.print(dbgs());
499 }
500#endif
501
502 LLVM_DEBUG(dbgs() << "Performing Loop Fusion on function " << F.getName()
503 << "\n");
504 bool Changed = false;
505
506 while (!LDT.empty()) {
507 LLVM_DEBUG(dbgs() << "Got " << LDT.size() << " loop sets for depth "
508 << LDT.getDepth() << "\n";);
509
510 for (const LoopVector &LV : LDT) {
511 assert(LV.size() > 0 && "Empty loop set was build!");
512
513 // Skip singleton loop sets as they do not offer fusion opportunities on
514 // this level.
515 if (LV.size() == 1)
516 continue;
517#ifndef NDEBUG
519 LLVM_DEBUG({
520 dbgs() << " Visit loop set (#" << LV.size() << "):\n";
521 printLoopVector(LV);
522 });
523 }
524#endif
525
526 collectFusionCandidates(LV);
527 Changed |= fuseCandidates();
528 // All loops in the candidate sets have a common parent (or no parent).
529 // Next loop vector will correspond to a different parent. It is safe
530 // to remove all the candidates currently in the set.
531 FusionCandidates.clear();
532 }
533
534 // Finished analyzing candidates at this level. Descend to the next level.
535 LLVM_DEBUG(dbgs() << "Descend one level!\n");
536 LDT.descend();
537 }
538
539 if (Changed)
540 LLVM_DEBUG(dbgs() << "Function after Loop Fusion: \n"; F.dump(););
541
542#ifndef NDEBUG
543 assert(DT.verify());
544 assert(PDT.verify());
545 LI.verify(DT);
546 SE.verify();
547#endif
548
549 LLVM_DEBUG(dbgs() << "Loop Fusion complete\n");
550 return Changed;
551 }
552
553private:
554 /// Iterate over all loops in the given loop set and identify the loops that
555 /// are eligible for fusion. Place all eligible fusion candidates into Control
556 /// Flow Equivalent sets, sorted by dominance.
557 void collectFusionCandidates(const LoopVector &LV) {
558 for (Loop *L : LV) {
560 gatherPeelingPreferences(L, SE, TTI, std::nullopt, std::nullopt);
561 FusionCandidate CurrCand(L, DT, &PDT, ORE, PP);
562 if (!CurrCand.isEligibleForFusion(SE))
563 continue;
564
565 // Go through each list in FusionCandidates and determine if the first or
566 // last loop in the list is strictly adjacent to L. If it is, append L.
567 // If not, go to the next list.
568 // If no suitable list is found, start another list and add it to
569 // FusionCandidates.
570 bool FoundAdjacent = false;
571 for (auto &CurrCandList : FusionCandidates) {
572 if (isStrictlyAdjacent(CurrCandList.back(), CurrCand)) {
573 CurrCandList.push_back(CurrCand);
574 FoundAdjacent = true;
575 NumFusionCandidates++;
576#ifndef NDEBUG
578 LLVM_DEBUG(dbgs() << "Adding " << CurrCand
579 << " to existing candidate list\n");
580#endif
581 break;
582 }
583 }
584 if (!FoundAdjacent) {
585 // No list was found. Create a new list and add to FusionCandidates
586#ifndef NDEBUG
588 LLVM_DEBUG(dbgs() << "Adding " << CurrCand << " to new list\n");
589#endif
590 FusionCandidateList NewCandList;
591 NewCandList.push_back(CurrCand);
592 FusionCandidates.push_back(NewCandList);
593 }
594 }
595 }
596
597 /// Determine if it is beneficial to fuse two loops.
598 ///
599 /// For now, this method simply returns true because we want to fuse as much
600 /// as possible (primarily to test the pass). This method will evolve, over
601 /// time, to add heuristics for profitability of fusion.
602 bool isBeneficialFusion(const FusionCandidate &FC0,
603 const FusionCandidate &FC1) {
604 return true;
605 }
606
607 /// Computes the integer difference in trip counts:
608 /// TripCount(FC0) - TripCount(FC1).
609 ///
610 /// \returns The integer difference, or std::nullopt if it
611 /// cannot be determined.
612 std::optional<int64_t>
613 calculateTripCountDiff(const FusionCandidate &FC0,
614 const FusionCandidate &FC1) const {
615 const SCEV *TripCount0 = SE.getBackedgeTakenCount(FC0.L);
616 if (isa<SCEVCouldNotCompute>(TripCount0)) {
617 UncomputableTripCount++;
618 LLVM_DEBUG(dbgs() << "Trip count of first loop could not be computed!");
619 return std::nullopt;
620 }
621
622 const SCEV *TripCount1 = SE.getBackedgeTakenCount(FC1.L);
623 if (isa<SCEVCouldNotCompute>(TripCount1)) {
624 UncomputableTripCount++;
625 LLVM_DEBUG(dbgs() << "Trip count of second loop could not be computed!");
626 return std::nullopt;
627 }
628
629 LLVM_DEBUG(dbgs() << "\tTrip counts: " << *TripCount0 << " & "
630 << *TripCount1 << " are "
631 << (TripCount0 == TripCount1 ? "identical" : "different")
632 << "\n");
633
634 if (TripCount0 == TripCount1)
635 return 0;
636
637 LLVM_DEBUG(dbgs() << "The loops do not have the same tripcount, "
638 "determining the difference between trip counts\n");
639
640 // Currently only considering loops with a single exit point
641 // and a non-constant trip count. Note that the return value
642 // of getSmallConstantTripCount is a 32 bit number, based on
643 // the existing implementation.
644 const int64_t TC0 =
645 static_cast<int64_t>(SE.getSmallConstantTripCount(FC0.L));
646 const int64_t TC1 =
647 static_cast<int64_t>(SE.getSmallConstantTripCount(FC1.L));
648
649 // If any of the tripcounts are zero that means that loop(s) do not have
650 // a single exit or a constant tripcount.
651 if (TC0 == 0 || TC1 == 0) {
652 LLVM_DEBUG(dbgs() << "Loop(s) do not have a single exit point or do not "
653 "have a constant number of iterations. Peeling "
654 "is not benefical\n");
655 return std::nullopt;
656 }
657
658 return TC0 - TC1;
659 }
660
661 void peelFusionCandidate(FusionCandidate &FC0, const FusionCandidate &FC1,
662 unsigned PeelCount) {
663 assert(FC0.AbleToPeel && "Should be able to peel loop");
664
665 LLVM_DEBUG(dbgs() << "Attempting to peel first " << PeelCount
666 << " iterations of the first loop. \n");
667
669 // LoopFusion is a function pass that neither requires nor preserves
670 // LCSSA, so peelLoop need not preserve it across its internal
671 // simplifyLoop call.
672 peelLoop(FC0.L, PeelCount, /*PeelLast=*/false, &LI, &SE, DT, &AC,
673 /*PreserveLCSSA=*/false, VMap);
674 FC0.Peeled = true;
675 LLVM_DEBUG(dbgs() << "Done Peeling\n");
676
677#ifndef NDEBUG
678 auto TCDiff = calculateTripCountDiff(FC0, FC1);
679
680 assert(TCDiff && *TCDiff == 0 &&
681 "Loops should have identical trip counts after peeling");
682#endif
683
684 FC0.PP.PeelCount += PeelCount;
685
686 // Peeling does not update the PDT
687 PDT.recalculate(*FC0.Preheader->getParent());
688
689 FC0.updateAfterPeeling();
690
691 // In this case the iterations of the loop are constant, so the first
692 // loop will execute completely (will not jump from one of
693 // the peeled blocks to the second loop). Here we are updating the
694 // branch conditions of each of the peeled blocks, such that it will
695 // branch to its successor which is not the preheader of the second loop
696 // in the case of unguarded loops, or the succesors of the exit block of
697 // the first loop otherwise. Doing this update will ensure that the entry
698 // block of the first loop dominates the entry block of the second loop.
699 BasicBlock *BB =
700 FC0.GuardBranch ? FC0.ExitBlock->getUniqueSuccessor() : FC1.Preheader;
701 if (BB) {
703 SmallVector<Instruction *, 8> WorkList;
704 for (BasicBlock *Pred : predecessors(BB)) {
705 if (Pred != FC0.ExitBlock) {
706 WorkList.emplace_back(Pred->getTerminator());
707 TreeUpdates.emplace_back(
708 DominatorTree::UpdateType(DominatorTree::Delete, Pred, BB));
709 }
710 }
711 // Cannot modify the predecessors inside the above loop as it will cause
712 // the iterators to be nullptrs, causing memory errors.
713 for (Instruction *CurrentBranch : WorkList) {
714 BasicBlock *Succ = CurrentBranch->getSuccessor(0);
715 if (Succ == BB)
716 Succ = CurrentBranch->getSuccessor(1);
717 ReplaceInstWithInst(CurrentBranch, UncondBrInst::Create(Succ));
718 }
719
720 DTU.applyUpdates(TreeUpdates);
721 DTU.flush();
722 }
724 dbgs() << "Sucessfully peeled " << FC0.PP.PeelCount
725 << " iterations from the first loop.\n"
726 "Both Loops have the same number of iterations now.\n");
727 }
728
729 /// Walk each set of strictly adjacent fusion candidates and attempt to fuse
730 /// them. This does a single linear traversal of all candidates in the list.
731 /// The conditions for legal fusion are checked at this point. If a pair of
732 /// fusion candidates passes all legality checks, they are fused together and
733 /// a new fusion candidate is created and added to the FusionCandidateList.
734 /// The original fusion candidates are then removed, as they are no longer
735 /// valid.
736 bool fuseCandidates() {
737 bool Fused = false;
738 LLVM_DEBUG(printFusionCandidates(FusionCandidates));
739 for (auto &CandidateList : FusionCandidates) {
740 if (CandidateList.size() < 2)
741 continue;
742
743 LLVM_DEBUG(dbgs() << "Attempting fusion on Candidate List:\n"
744 << CandidateList << "\n");
745
746 for (auto It = CandidateList.begin(), NextIt = std::next(It);
747 NextIt != CandidateList.end(); It = NextIt, NextIt = std::next(It)) {
748
749 auto FC0 = *It;
750 auto FC1 = *NextIt;
751
752 assert(!LDT.isRemovedLoop(FC0.L) &&
753 "Should not have removed loops in CandidateList!");
754 assert(!LDT.isRemovedLoop(FC1.L) &&
755 "Should not have removed loops in CandidateList!");
756
757 LLVM_DEBUG(dbgs() << "Attempting to fuse candidate \n"; FC0.dump();
758 dbgs() << " with\n"; FC1.dump(); dbgs() << "\n");
759
760 FC0.verify();
761 FC1.verify();
762
763 std::optional<int64_t> TCDifference = calculateTripCountDiff(FC0, FC1);
764 // Here we are checking that FC0 (the first loop) can be peeled, and
765 // the first loop has a larger trip count. In this case it is possible
766 // that the first loop is peeled to expose the fusion opportunity.
767 // Peeling the second loop is not currently supported.
768 bool WillPeel =
769 FC0.AbleToPeel && TCDifference && *TCDifference > 0 &&
770 *TCDifference <= static_cast<int64_t>(FusionPeelMaxCount);
771
772 if (!WillPeel && (!TCDifference || *TCDifference != 0)) {
773 LLVM_DEBUG(dbgs() << "Fusion candidates do not have identical trip "
774 "counts and peeling is not supported for this "
775 "case. Not fusing.\n");
776 ++NonEqualTripCount;
777 reportLoopFusion<OptimizationRemarkMissed>(
778 FC0, FC1, "NonEqualTripCount",
779 "Loop trip counts are not the same");
780 continue;
781 }
782
783 if ((!FC0.GuardBranch && FC1.GuardBranch) ||
784 (FC0.GuardBranch && !FC1.GuardBranch)) {
785 LLVM_DEBUG(dbgs() << "The one of candidate is guarded while the "
786 "another one is not. Not fusing.\n");
787 ++OnlySecondCandidateIsGuarded;
788 reportLoopFusion<OptimizationRemarkMissed>(
789 FC0, FC1, "OnlySecondCandidateIsGuarded",
790 "The second candidate is guarded while the first one is not");
791 continue;
792 }
793
794 // If TCDifference is not set or if it is zero, peeling is not needed.
795 // In this case we must ensure if the loops are guarded the guards
796 // are identical.
797 if (!TCDifference || *TCDifference == 0) {
798 if (FC0.GuardBranch && FC1.GuardBranch &&
799 !haveIdenticalGuards(FC0, FC1)) {
800 LLVM_DEBUG(dbgs() << "Fusion candidates do not have identical "
801 "guards. Not Fusing.\n");
802 ++NonIdenticalGuards;
803 reportLoopFusion<OptimizationRemarkMissed>(
804 FC0, FC1, "NonIdenticalGuards",
805 "Candidates have different guards");
806 continue;
807 }
808 }
809
810 if (FC0.GuardBranch) {
811 assert(FC1.GuardBranch && "Expecting valid FC1 guard branch");
812
813 if (!isSafeToMoveBefore(*FC0.ExitBlock,
814 *FC1.ExitBlock->getFirstNonPHIOrDbg(), DT,
815 &PDT, &DI)) {
816 LLVM_DEBUG(dbgs() << "Fusion candidate contains unsafe "
817 "instructions in exit block. Not fusing.\n");
818 ++NonEmptyExitBlock;
819 reportLoopFusion<OptimizationRemarkMissed>(
820 FC0, FC1, "NonEmptyExitBlock",
821 "Candidate has a non-empty exit block with "
822 "instructions that cannot be moved");
823 continue;
824 }
825
827 *FC1.GuardBranch->getParent(),
828 *FC0.GuardBranch->getParent()->getTerminator(), DT, &PDT,
829 &DI)) {
830 LLVM_DEBUG(dbgs() << "Fusion candidate contains unsafe "
831 "instructions in guard block. Not fusing.\n");
832 ++NonEmptyGuardBlock;
833 reportLoopFusion<OptimizationRemarkMissed>(
834 FC0, FC1, "NonEmptyGuardBlock",
835 "Candidate has a non-empty guard block with "
836 "instructions that cannot be moved");
837 continue;
838 }
839 }
840
841 // Check the dependencies across the loops and do not fuse if it would
842 // violate them.
843 if (!dependencesAllowFusion(FC0, FC1)) {
844 LLVM_DEBUG(dbgs() << "Memory dependencies do not allow fusion!\n");
845 ++InvalidDependencies;
846 reportLoopFusion<OptimizationRemarkMissed>(
847 FC0, FC1, "InvalidDependencies", "Dependencies prevent fusion");
848 continue;
849 }
850
851 // If the second loop has instructions in the pre-header, attempt to
852 // hoist them up to the first loop's pre-header or sink them into the
853 // body of the second loop.
854 SmallVector<Instruction *, 4> SafeToHoist;
855 SmallVector<Instruction *, 4> SafeToSink;
856 // At this point, this is the last remaining legality check.
857 // Which means if we can make this pre-header empty, we can fuse
858 // these loops
859 if (!isEmptyPreheader(FC1)) {
860 LLVM_DEBUG(dbgs() << "Fusion candidate does not have empty "
861 "preheader.\n");
862
863 // If it is not safe to hoist/sink all instructions in the
864 // pre-header, we cannot fuse these loops.
865 if (!collectMovablePreheaderInsts(FC0, FC1, SafeToHoist,
866 SafeToSink)) {
867 LLVM_DEBUG(dbgs() << "Could not hoist/sink all instructions in "
868 "Fusion Candidate Pre-header.\n"
869 << "Not Fusing.\n");
870 ++NonEmptyPreheader;
871 reportLoopFusion<OptimizationRemarkMissed>(
872 FC0, FC1, "NonEmptyPreheader",
873 "Loop has a non-empty preheader with instructions that "
874 "cannot be moved");
875 continue;
876 }
877 }
878
879 bool BeneficialToFuse = isBeneficialFusion(FC0, FC1);
880 LLVM_DEBUG(dbgs() << "\tFusion appears to be "
881 << (BeneficialToFuse ? "" : "un") << "profitable!\n");
882 if (!BeneficialToFuse) {
883 ++FusionNotBeneficial;
884 reportLoopFusion<OptimizationRemarkMissed>(
885 FC0, FC1, "FusionNotBeneficial", "Fusion is not beneficial");
886 continue;
887 }
888 // All analysis has completed and has determined that fusion is legal
889 // and profitable. At this point, start transforming the code and
890 // perform fusion.
891
892 // Execute the hoist/sink operations on preheader instructions
893 movePreheaderInsts(FC0, FC1, SafeToHoist, SafeToSink);
894
895 LLVM_DEBUG(dbgs() << "\tFusion is performed: " << FC0 << " and " << FC1
896 << "\n");
897
898 FusionCandidate FC0Copy = FC0;
899 // Peel the loop after determining that fusion is legal. The Loops
900 // will still be safe to fuse after the peeling is performed.
901 bool Peel = TCDifference && *TCDifference > 0;
902 if (Peel)
903 peelFusionCandidate(FC0Copy, FC1, *TCDifference);
904
905 // Report fusion to the Optimization Remarks.
906 // Note this needs to be done *before* performFusion because
907 // performFusion will change the original loops, making it not
908 // possible to identify them after fusion is complete.
909 ++FuseCounter;
910 reportLoopFusion<OptimizationRemark>((Peel ? FC0Copy : FC0), FC1,
911 "FuseCounter", "Loops fused");
912
913 FusionCandidate FusedCand(performFusion((Peel ? FC0Copy : FC0), FC1),
914 DT, &PDT, ORE, FC0Copy.PP);
915 FusedCand.verify();
916 assert(FusedCand.isEligibleForFusion(SE) &&
917 "Fused candidate should be eligible for fusion!");
918
919 // Notify the loop-depth-tree that these loops are not valid objects
920 LDT.removeLoop(FC1.L);
921
922 // Replace FC0 and FC1 with their fused loop
923 It = CandidateList.erase(It);
924 It = CandidateList.erase(It);
925 It = CandidateList.insert(It, FusedCand);
926
927 // Start from FusedCand in the next iteration
928 NextIt = It;
929
930 LLVM_DEBUG(dbgs() << "Candidate List (after fusion): " << CandidateList
931 << "\n");
932
933 Fused = true;
934 }
935 }
936 return Fused;
937 }
938
939 // Returns true if the instruction \p I can be hoisted to the end of the
940 // preheader of \p FC0. \p SafeToHoist contains the instructions that are
941 // known to be safe to hoist. The instructions encountered that cannot be
942 // hoisted are in \p NotHoisting.
943 // TODO: Move functionality into CodeMoverUtils
944 bool canHoistInst(Instruction &I,
945 const SmallVector<Instruction *, 4> &SafeToHoist,
946 const SmallVector<Instruction *, 4> &NotHoisting,
947 const FusionCandidate &FC0) const {
948 const BasicBlock *FC0PreheaderTarget = FC0.Preheader->getSingleSuccessor();
949 assert(FC0PreheaderTarget &&
950 "Expected single successor for loop preheader.");
951
952 for (Use &Op : I.operands()) {
953 if (auto *OpInst = dyn_cast<Instruction>(Op)) {
954 bool OpHoisted = is_contained(SafeToHoist, OpInst);
955 // Check if we have already decided to hoist this operand. In this
956 // case, it does not dominate FC0 *yet*, but will after we hoist it.
957 if (!(OpHoisted || DT.dominates(OpInst, FC0PreheaderTarget))) {
958 return false;
959 }
960 }
961 }
962
963 // PHIs in FC1's header only have FC0 blocks as predecessors. PHIs
964 // cannot be hoisted and should be sunk to the exit of the fused loop.
965 if (isa<PHINode>(I))
966 return false;
967
968 // If this isn't a memory inst, hoisting is safe
969 if (!I.mayReadOrWriteMemory())
970 return true;
971
972 LLVM_DEBUG(dbgs() << "Checking if this mem inst can be hoisted.\n");
973 for (Instruction *NotHoistedInst : NotHoisting) {
974 if (auto D = DI.depends(&I, NotHoistedInst)) {
975 // Dependency is not read-before-write, write-before-read or
976 // write-before-write
977 if (D->isFlow() || D->isAnti() || D->isOutput()) {
978 LLVM_DEBUG(dbgs() << "Inst depends on an instruction in FC1's "
979 "preheader that is not being hoisted.\n");
980 return false;
981 }
982 }
983 }
984
985 for (Instruction *ReadInst : FC0.MemReads) {
986 if (auto D = DI.depends(ReadInst, &I)) {
987 // Dependency is not read-before-write
988 if (D->isAnti()) {
989 LLVM_DEBUG(dbgs() << "Inst depends on a read instruction in FC0.\n");
990 return false;
991 }
992 }
993 }
994
995 for (Instruction *WriteInst : FC0.MemWrites) {
996 if (auto D = DI.depends(WriteInst, &I)) {
997 // Dependency is not write-before-read or write-before-write
998 if (D->isFlow() || D->isOutput()) {
999 LLVM_DEBUG(dbgs() << "Inst depends on a write instruction in FC0.\n");
1000 return false;
1001 }
1002 }
1003 }
1004 return true;
1005 }
1006
1007 // Returns true if the instruction \p I can be sunk to the top of the exit
1008 // block of \p FC1.
1009 // TODO: Move functionality into CodeMoverUtils
1010 bool canSinkInst(Instruction &I, const FusionCandidate &FC1) const {
1011 for (User *U : I.users()) {
1012 if (auto *UI{dyn_cast<Instruction>(U)}) {
1013 // Cannot sink if user in loop
1014 // If FC1 has phi users of this value, we cannot sink it into FC1.
1015 if (FC1.L->contains(UI)) {
1016 // Cannot hoist or sink this instruction. No hoisting/sinking
1017 // should take place, loops should not fuse
1018 return false;
1019 }
1020 }
1021 }
1022
1023 // If this isn't a memory inst, sinking is safe
1024 if (!I.mayReadOrWriteMemory())
1025 return true;
1026
1027 for (Instruction *ReadInst : FC1.MemReads) {
1028 if (auto D = DI.depends(&I, ReadInst)) {
1029 // Dependency is not write-before-read
1030 if (D->isFlow()) {
1031 LLVM_DEBUG(dbgs() << "Inst depends on a read instruction in FC1.\n");
1032 return false;
1033 }
1034 }
1035 }
1036
1037 for (Instruction *WriteInst : FC1.MemWrites) {
1038 if (auto D = DI.depends(&I, WriteInst)) {
1039 // Dependency is not write-before-write or read-before-write
1040 if (D->isOutput() || D->isAnti()) {
1041 LLVM_DEBUG(dbgs() << "Inst depends on a write instruction in FC1.\n");
1042 return false;
1043 }
1044 }
1045 }
1046
1047 return true;
1048 }
1049
1050 /// Collect instructions in the \p FC1 Preheader that can be hoisted
1051 /// to the \p FC0 Preheader or sunk into the \p FC1 Body
1052 bool collectMovablePreheaderInsts(
1053 const FusionCandidate &FC0, const FusionCandidate &FC1,
1054 SmallVector<Instruction *, 4> &SafeToHoist,
1055 SmallVector<Instruction *, 4> &SafeToSink) const {
1056 BasicBlock *FC1Preheader = FC1.Preheader;
1057 // Save the instructions that are not being hoisted, so we know not to hoist
1058 // mem insts that they dominate.
1059 SmallVector<Instruction *, 4> NotHoisting;
1060
1061 for (Instruction &I : *FC1Preheader) {
1062 // Can't move a branch
1063 if (&I == FC1Preheader->getTerminator())
1064 continue;
1065 // If the instruction has side-effects, give up.
1066 // TODO: The case of mayReadFromMemory we can handle but requires
1067 // additional work with a dependence analysis so for now we give
1068 // up on memory reads.
1069 if (I.mayThrow() || !I.willReturn()) {
1070 LLVM_DEBUG(dbgs() << "Inst: " << I << " may throw or won't return.\n");
1071 return false;
1072 }
1073
1074 LLVM_DEBUG(dbgs() << "Checking Inst: " << I << "\n");
1075
1076 if (I.isAtomic() || I.isVolatile()) {
1077 LLVM_DEBUG(
1078 dbgs() << "\tInstruction is volatile or atomic. Cannot move it.\n");
1079 return false;
1080 }
1081
1082 if (canHoistInst(I, SafeToHoist, NotHoisting, FC0)) {
1083 SafeToHoist.push_back(&I);
1084 LLVM_DEBUG(dbgs() << "\tSafe to hoist.\n");
1085 } else {
1086 LLVM_DEBUG(dbgs() << "\tCould not hoist. Trying to sink...\n");
1087 NotHoisting.push_back(&I);
1088
1089 if (canSinkInst(I, FC1)) {
1090 SafeToSink.push_back(&I);
1091 LLVM_DEBUG(dbgs() << "\tSafe to sink.\n");
1092 } else {
1093 LLVM_DEBUG(dbgs() << "\tCould not sink.\n");
1094 return false;
1095 }
1096 }
1097 }
1098 LLVM_DEBUG(
1099 dbgs() << "All preheader instructions could be sunk or hoisted!\n");
1100 return true;
1101 }
1102
1103 /// Return true if the dependences between @p I0 (in @p L0) and @p I1 (in
1104 /// @p L1) allow loop fusion of @p L0 and @p L1.
1105 bool dependencesAllowFusion(const FusionCandidate &FC0,
1106 const FusionCandidate &FC1, Instruction &I0,
1107 Instruction &I1) {
1108#ifndef NDEBUG
1110 LLVM_DEBUG(dbgs() << "Check dep: " << I0 << " vs " << I1 << "\n");
1111 }
1112#endif
1113 auto DepResult = DI.depends(&I0, &I1);
1114 if (!DepResult)
1115 return true;
1116#ifndef NDEBUG
1118 LLVM_DEBUG(dbgs() << "DA res: "; DepResult->dump(dbgs());
1119 dbgs() << " [#l: " << DepResult->getLevels() << "][Ordered: "
1120 << (DepResult->isOrdered() ? "true" : "false")
1121 << "]\n");
1122 LLVM_DEBUG(dbgs() << "DepResult Levels: " << DepResult->getLevels()
1123 << "\n");
1124 }
1125#endif
1126 unsigned Levels = DepResult->getLevels();
1127 unsigned SameSDLevels = DepResult->getSameSDLevels();
1128 unsigned CurLoopLevel = FC0.L->getLoopDepth();
1129
1130 // Check if DA is missing info regarding the current loop level
1131 if (CurLoopLevel > Levels + SameSDLevels)
1132 return false;
1133
1134 // Iterating over the outer levels.
1135 for (unsigned Level = 1; Level <= std::min(CurLoopLevel - 1, Levels);
1136 ++Level) {
1137 unsigned Direction = DepResult->getDirection(Level, false);
1138
1139 // Check if the direction vector does not include equality. If an outer
1140 // loop has a non-equal direction, outer indicies are different and it
1141 // is safe to fuse.
1143 LLVM_DEBUG(dbgs() << "Safe to fuse due to non-equal acceses in the "
1144 "outer loops\n");
1145 NumDA++;
1146 return true;
1147 }
1148 }
1149
1150 assert(CurLoopLevel > Levels && "Fusion candidates are not separated");
1151
1152 if (DepResult->isScalar(CurLoopLevel, true)) {
1153 if (DepResult->isInput() || DepResult->isOutput()) {
1154 LLVM_DEBUG(dbgs() << "Safe to fuse due to a loop-invariant "
1155 << (DepResult->isInput() ? "input" : "output")
1156 << " dependency\n");
1157 NumDA++;
1158 return true;
1159 }
1160 LLVM_DEBUG(
1161 dbgs() << "Not safe to fuse due to a scalar flow dependency\n");
1162 return false;
1163 }
1164
1165 unsigned CurDir = DepResult->getDirection(CurLoopLevel, true);
1166
1167 // Check if the direction vector does not include greater direction. In
1168 // that case, the dependency is not a backward loop-carried and is legal
1169 // to fuse. For example here we have a forward dependency
1170 // for (int i = 0; i < n; i++)
1171 // A[i] = ...;
1172 // for (int i = 0; i < n; i++)
1173 // ... = A[i-1];
1174 if (!(CurDir & Dependence::DVEntry::GT)) {
1175 LLVM_DEBUG(dbgs() << "Safe to fuse with no backward loop-carried "
1176 "dependency\n");
1177 NumDA++;
1178 return true;
1179 }
1180
1181 if (DepResult->getNextPredecessor() || DepResult->getNextSuccessor())
1182 LLVM_DEBUG(dbgs() << "TODO: Implement pred/succ dependence handling!\n");
1183
1184 return false;
1185 }
1186
1187 /// Perform a dependence check and return if @p FC0 and @p FC1 can be fused.
1188 bool dependencesAllowFusion(const FusionCandidate &FC0,
1189 const FusionCandidate &FC1) {
1190 LLVM_DEBUG(dbgs() << "Check if " << FC0 << " can be fused with " << FC1
1191 << "\n");
1192 assert(FC0.L->getLoopDepth() == FC1.L->getLoopDepth());
1193 assert(DT.dominates(FC0.getEntryBlock(), FC1.getEntryBlock()));
1194
1195 for (Instruction *WriteL0 : FC0.MemWrites) {
1196 for (Instruction *WriteL1 : FC1.MemWrites)
1197 if (!dependencesAllowFusion(FC0, FC1, *WriteL0, *WriteL1)) {
1198 return false;
1199 }
1200 for (Instruction *ReadL1 : FC1.MemReads)
1201 if (!dependencesAllowFusion(FC0, FC1, *WriteL0, *ReadL1)) {
1202 return false;
1203 }
1204 }
1205
1206 // Write-write and write-read pairs are already covered above; only the
1207 // read-before-write pairs from FC0 reads to FC1 writes remain.
1208 for (Instruction *ReadL0 : FC0.MemReads)
1209 for (Instruction *WriteL1 : FC1.MemWrites)
1210 if (!dependencesAllowFusion(FC0, FC1, *ReadL0, *WriteL1)) {
1211 return false;
1212 }
1213
1214 // Walk through all uses in FC1. For each use, find the reaching def. If the
1215 // def is located in FC0 then it is not safe to fuse.
1216 for (BasicBlock *BB : FC1.L->blocks())
1217 for (Instruction &I : *BB)
1218 for (auto &Op : I.operands())
1219 if (Instruction *Def = dyn_cast<Instruction>(Op))
1220 if (FC0.L->contains(Def->getParent())) {
1221 return false;
1222 }
1223
1224 return true;
1225 }
1226
1227 /// Determine if two fusion candidates are strictly adjacent in the CFG.
1228 ///
1229 /// This method will determine if there are additional basic blocks in the CFG
1230 /// between the exit of \p FC0 and the entry of \p FC1.
1231 /// If the two candidates are guarded loops, then it checks whether the
1232 /// exit block of the \p FC0 is the predecessor of the \p FC1 preheader. This
1233 /// implicitly ensures that the non-loop successor of the \p FC0 guard branch
1234 /// is the entry block of \p FC1. If not, then the loops are not adjacent. If
1235 /// the two candidates are not guarded loops, then it checks whether the exit
1236 /// block of \p FC0 is the preheader of \p FC1.
1237 /// Strictly means there is no predecessor for FC1 unless it is from FC0,
1238 /// i.e., FC0 dominates FC1.
1239 bool isStrictlyAdjacent(const FusionCandidate &FC0,
1240 const FusionCandidate &FC1) const {
1241 // If the successor of the guard branch is FC1, then the loops are adjacent
1242 if (FC0.GuardBranch)
1243 return DT.dominates(FC0.getEntryBlock(), FC1.getEntryBlock()) &&
1244 FC0.ExitBlock->getSingleSuccessor() == FC1.getEntryBlock();
1245 return FC0.ExitBlock == FC1.getEntryBlock();
1246 }
1247
1248 bool isEmptyPreheader(const FusionCandidate &FC) const {
1249 return FC.Preheader->size() == 1;
1250 }
1251
1252 /// Hoist \p FC1 Preheader instructions to \p FC0 Preheader
1253 /// and sink others into the body of \p FC1.
1254 void movePreheaderInsts(const FusionCandidate &FC0,
1255 const FusionCandidate &FC1,
1256 SmallVector<Instruction *, 4> &HoistInsts,
1257 SmallVector<Instruction *, 4> &SinkInsts) const {
1258 // All preheader instructions except the branch must be hoisted or sunk
1259 assert(HoistInsts.size() + SinkInsts.size() == FC1.Preheader->size() - 1 &&
1260 "Attempting to sink and hoist preheader instructions, but not all "
1261 "the preheader instructions are accounted for.");
1262
1263 NumHoistedInsts += HoistInsts.size();
1264 NumSunkInsts += SinkInsts.size();
1265
1267 if (!HoistInsts.empty())
1268 dbgs() << "Hoisting: \n";
1269 for (Instruction *I : HoistInsts)
1270 dbgs() << *I << "\n";
1271 if (!SinkInsts.empty())
1272 dbgs() << "Sinking: \n";
1273 for (Instruction *I : SinkInsts)
1274 dbgs() << *I << "\n";
1275 });
1276
1277 for (Instruction *I : HoistInsts) {
1278 assert(I->getParent() == FC1.Preheader);
1279 I->moveBefore(*FC0.Preheader,
1280 FC0.Preheader->getTerminator()->getIterator());
1281 }
1282 // insert instructions in reverse order to maintain dominance relationship
1283 for (Instruction *I : reverse(SinkInsts)) {
1284 assert(I->getParent() == FC1.Preheader);
1285 if (isa<PHINode>(I)) {
1286 // The Phis to be sunk should have only one incoming value, as is
1287 // assured by the condition that the second loop is dominated by the
1288 // first one which is enforced by isStrictlyAdjacent().
1289 // Replace the phi uses with the corresponding incoming value to clean
1290 // up the code.
1291 assert(cast<PHINode>(I)->getNumIncomingValues() == 1 &&
1292 "Expected the sunk PHI node to have 1 incoming value.");
1293 I->replaceAllUsesWith(I->getOperand(0));
1294 I->eraseFromParent();
1295 } else
1296 I->moveBefore(*FC1.ExitBlock, FC1.ExitBlock->getFirstInsertionPt());
1297 }
1298 }
1299
1300 /// Determine if two fusion candidates have identical guards
1301 ///
1302 /// This method will determine if two fusion candidates have the same guards.
1303 /// The guards are considered the same if:
1304 /// 1. The instructions to compute the condition used in the compare are
1305 /// identical.
1306 /// 2. The successors of the guard have the same flow into/around the loop.
1307 /// If the compare instructions are identical, then the first successor of the
1308 /// guard must go to the same place (either the preheader of the loop or the
1309 /// NonLoopBlock). In other words, the first successor of both loops must
1310 /// both go into the loop (i.e., the preheader) or go around the loop (i.e.,
1311 /// the NonLoopBlock). The same must be true for the second successor.
1312 bool haveIdenticalGuards(const FusionCandidate &FC0,
1313 const FusionCandidate &FC1) const {
1314 assert(FC0.GuardBranch && FC1.GuardBranch &&
1315 "Expecting FC0 and FC1 to be guarded loops.");
1316
1317 auto *FC0CmpInst = dyn_cast<Instruction>(FC0.GuardBranch->getCondition());
1318 auto *FC1CmpInst = dyn_cast<Instruction>(FC1.GuardBranch->getCondition());
1319 if ((!FC0CmpInst || !FC1CmpInst) &&
1320 FC0.GuardBranch->getCondition() != FC1.GuardBranch->getCondition())
1321 return false;
1322
1323 if (FC0CmpInst && FC1CmpInst && !FC0CmpInst->isIdenticalTo(FC1CmpInst))
1324 return false;
1325
1326 // The compare instructions are identical.
1327 // Now make sure the successor of the guards have the same flow into/around
1328 // the loop
1329 if (FC0.GuardBranch->getSuccessor(0) == FC0.Preheader)
1330 return (FC1.GuardBranch->getSuccessor(0) == FC1.Preheader);
1331 return (FC1.GuardBranch->getSuccessor(1) == FC1.Preheader);
1332 }
1333
1334 /// Modify the latch branch of FC to be unconditional since successors of the
1335 /// branch are the same.
1336 void simplifyLatchBranch(const FusionCandidate &FC) const {
1337 CondBrInst *FCLatchBranch = dyn_cast<CondBrInst>(FC.Latch->getTerminator());
1338 if (FCLatchBranch) {
1339 assert(FCLatchBranch->getSuccessor(0) == FCLatchBranch->getSuccessor(1) &&
1340 "Expecting the two successors of FCLatchBranch to be the same");
1341 UncondBrInst *NewBranch =
1342 UncondBrInst::Create(FCLatchBranch->getSuccessor(0));
1343 ReplaceInstWithInst(FCLatchBranch, NewBranch);
1344 }
1345 }
1346
1347 /// Move instructions from FC0.Latch to FC1.Latch. If FC0.Latch has an unique
1348 /// successor, then merge FC0.Latch with its unique successor.
1349 void mergeLatch(const FusionCandidate &FC0, const FusionCandidate &FC1) {
1350 moveInstructionsToTheBeginning(*FC0.Latch, *FC1.Latch, DT, PDT, DI, SE);
1351 if (BasicBlock *Succ = FC0.Latch->getUniqueSuccessor()) {
1352 MergeBlockIntoPredecessor(Succ, &DTU, &LI);
1353 DTU.flush();
1354 }
1355 }
1356
1357 /// Fuse two fusion candidates, creating a new fused loop.
1358 ///
1359 /// This method contains the mechanics of fusing two loops, represented by \p
1360 /// FC0 and \p FC1. It is assumed that \p FC0 dominates \p FC1 and \p FC1
1361 /// postdominates \p FC0 (making them control flow equivalent). It also
1362 /// assumes that the other conditions for fusion have been met: adjacent,
1363 /// identical trip counts, and no negative distance dependencies exist that
1364 /// would prevent fusion. Thus, there is no checking for these conditions in
1365 /// this method.
1366 ///
1367 /// Fusion is performed by rewiring the CFG to update successor blocks of the
1368 /// components of tho loop. Specifically, the following changes are done:
1369 ///
1370 /// 1. The preheader of \p FC1 is removed as it is no longer necessary
1371 /// (because it is currently only a single statement block).
1372 /// 2. The latch of \p FC0 is modified to jump to the header of \p FC1.
1373 /// 3. The latch of \p FC1 i modified to jump to the header of \p FC0.
1374 /// 4. All blocks from \p FC1 are removed from FC1 and added to FC0.
1375 ///
1376 /// All of these modifications are done with dominator tree updates, thus
1377 /// keeping the dominator (and post dominator) information up-to-date.
1378 ///
1379 /// This can be improved in the future by actually merging blocks during
1380 /// fusion. For example, the preheader of \p FC1 can be merged with the
1381 /// preheader of \p FC0. This would allow loops with more than a single
1382 /// statement in the preheader to be fused. Similarly, the latch blocks of the
1383 /// two loops could also be fused into a single block. This will require
1384 /// analysis to prove it is safe to move the contents of the block past
1385 /// existing code, which currently has not been implemented.
1386 Loop *performFusion(const FusionCandidate &FC0, const FusionCandidate &FC1) {
1387 assert(FC0.isValid() && FC1.isValid() &&
1388 "Expecting valid fusion candidates");
1389
1390 LLVM_DEBUG(dbgs() << "Fusion Candidate 0: \n"; FC0.dump();
1391 dbgs() << "Fusion Candidate 1: \n"; FC1.dump(););
1392
1393 // Move instructions from the preheader of FC1 to the end of the preheader
1394 // of FC0.
1395 moveInstructionsToTheEnd(*FC1.Preheader, *FC0.Preheader, DT, PDT, DI, SE);
1396
1397 // Fusing guarded loops is handled slightly differently than non-guarded
1398 // loops and has been broken out into a separate method instead of trying to
1399 // intersperse the logic within a single method.
1400 if (FC0.GuardBranch)
1401 return fuseGuardedLoops(FC0, FC1);
1402
1403 assert(FC1.Preheader ==
1404 (FC0.Peeled ? FC0.ExitBlock->getUniqueSuccessor() : FC0.ExitBlock));
1405 assert(FC1.Preheader->size() == 1 &&
1406 FC1.Preheader->getSingleSuccessor() == FC1.Header);
1407
1408 // Remember the phi nodes originally in the header of FC0 in order to rewire
1409 // them later. However, this is only necessary if the new loop carried
1410 // values might not dominate the exiting branch. While we do not generally
1411 // test if this is the case but simply insert intermediate phi nodes, we
1412 // need to make sure these intermediate phi nodes have different
1413 // predecessors. To this end, we filter the special case where the exiting
1414 // block is the latch block of the first loop. Nothing needs to be done
1415 // anyway as all loop carried values dominate the latch and thereby also the
1416 // exiting branch.
1417 SmallVector<PHINode *, 8> OriginalFC0PHIs;
1418 if (FC0.ExitingBlock != FC0.Latch)
1419 for (PHINode &PHI : FC0.Header->phis())
1420 OriginalFC0PHIs.push_back(&PHI);
1421
1422 // Replace incoming blocks for header PHIs first.
1423 FC1.Preheader->replaceSuccessorsPhiUsesWith(FC0.Preheader);
1424 FC0.Latch->replaceSuccessorsPhiUsesWith(FC1.Latch);
1425
1426 // Then modify the control flow and update DT and PDT.
1428
1429 // The old exiting block of the first loop (FC0) has to jump to the header
1430 // of the second as we need to execute the code in the second header block
1431 // regardless of the trip count. That is, if the trip count is 0, so the
1432 // back edge is never taken, we still have to execute both loop headers,
1433 // especially (but not only!) if the second is a do-while style loop.
1434 // However, doing so might invalidate the phi nodes of the first loop as
1435 // the new values do only need to dominate their latch and not the exiting
1436 // predicate. To remedy this potential problem we always introduce phi
1437 // nodes in the header of the second loop later that select the loop carried
1438 // value, if the second header was reached through an old latch of the
1439 // first, or undef otherwise. This is sound as exiting the first implies the
1440 // second will exit too, __without__ taking the back-edge. [Their
1441 // trip-counts are equal after all.
1442 // KB: Would this sequence be simpler to just make FC0.ExitingBlock go
1443 // to FC1.Header? I think this is basically what the three sequences are
1444 // trying to accomplish; however, doing this directly in the CFG may mean
1445 // the DT/PDT becomes invalid
1446 if (!FC0.Peeled) {
1447 FC0.ExitingBlock->getTerminator()->replaceUsesOfWith(FC1.Preheader,
1448 FC1.Header);
1449 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1450 DominatorTree::Delete, FC0.ExitingBlock, FC1.Preheader));
1451 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1452 DominatorTree::Insert, FC0.ExitingBlock, FC1.Header));
1453 } else {
1454 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1455 DominatorTree::Delete, FC0.ExitBlock, FC1.Preheader));
1456
1457 // Remove the ExitBlock of the first Loop (also not needed)
1458 FC0.ExitingBlock->getTerminator()->replaceUsesOfWith(FC0.ExitBlock,
1459 FC1.Header);
1460 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1461 DominatorTree::Delete, FC0.ExitingBlock, FC0.ExitBlock));
1462 FC0.ExitBlock->getTerminator()->eraseFromParent();
1463 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1464 DominatorTree::Insert, FC0.ExitingBlock, FC1.Header));
1465 new UnreachableInst(FC0.ExitBlock->getContext(), FC0.ExitBlock);
1466 }
1467
1468 // The pre-header of L1 is not necessary anymore.
1469 assert(pred_empty(FC1.Preheader));
1470 FC1.Preheader->getTerminator()->eraseFromParent();
1471 new UnreachableInst(FC1.Preheader->getContext(), FC1.Preheader);
1472 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1473 DominatorTree::Delete, FC1.Preheader, FC1.Header));
1474
1475 // Moves the phi nodes from the second to the first loops header block.
1476 while (PHINode *PHI = dyn_cast<PHINode>(&FC1.Header->front())) {
1477 if (SE.isSCEVable(PHI->getType()))
1478 SE.forgetValue(PHI);
1479 if (PHI->hasNUsesOrMore(1))
1480 PHI->moveBefore(FC0.Header->getFirstInsertionPt());
1481 else
1482 PHI->eraseFromParent();
1483 }
1484
1485 // Introduce new phi nodes in the second loop header to ensure
1486 // exiting the first and jumping to the header of the second does not break
1487 // the SSA property of the phis originally in the first loop. See also the
1488 // comment above.
1489 BasicBlock::iterator L1HeaderIP = FC1.Header->begin();
1490 for (PHINode *LCPHI : OriginalFC0PHIs) {
1491 int L1LatchBBIdx = LCPHI->getBasicBlockIndex(FC1.Latch);
1492 assert(L1LatchBBIdx >= 0 &&
1493 "Expected loop carried value to be rewired at this point!");
1494
1495 Value *LCV = LCPHI->getIncomingValue(L1LatchBBIdx);
1496
1497 PHINode *L1HeaderPHI =
1498 PHINode::Create(LCV->getType(), 2, LCPHI->getName() + ".afterFC0");
1499 L1HeaderPHI->insertBefore(L1HeaderIP);
1500 L1HeaderPHI->addIncoming(LCV, FC0.Latch);
1501 L1HeaderPHI->addIncoming(PoisonValue::get(LCV->getType()),
1502 FC0.ExitingBlock);
1503
1504 LCPHI->setIncomingValue(L1LatchBBIdx, L1HeaderPHI);
1505 }
1506
1507 // Replace latch terminator destinations.
1508 FC0.Latch->getTerminator()->replaceUsesOfWith(FC0.Header, FC1.Header);
1509 FC1.Latch->getTerminator()->replaceUsesOfWith(FC1.Header, FC0.Header);
1510
1511 // Modify the latch branch of FC0 to be unconditional as both successors of
1512 // the branch are the same.
1513 simplifyLatchBranch(FC0);
1514
1515 // If FC0.Latch and FC0.ExitingBlock are the same then we have already
1516 // performed the updates above.
1517 if (FC0.Latch != FC0.ExitingBlock)
1518 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1519 DominatorTree::Insert, FC0.Latch, FC1.Header));
1520
1521 TreeUpdates.emplace_back(DominatorTree::UpdateType(DominatorTree::Delete,
1522 FC0.Latch, FC0.Header));
1523 TreeUpdates.emplace_back(DominatorTree::UpdateType(DominatorTree::Insert,
1524 FC1.Latch, FC0.Header));
1525 TreeUpdates.emplace_back(DominatorTree::UpdateType(DominatorTree::Delete,
1526 FC1.Latch, FC1.Header));
1527
1528 // Update DT/PDT
1529 DTU.applyUpdates(TreeUpdates);
1530
1531 LI.removeBlock(FC1.Preheader);
1532 DTU.deleteBB(FC1.Preheader);
1533 if (FC0.Peeled) {
1534 LI.removeBlock(FC0.ExitBlock);
1535 DTU.deleteBB(FC0.ExitBlock);
1536 }
1537
1538 DTU.flush();
1539
1540 // Is there a way to keep SE up-to-date so we don't need to forget the loops
1541 // and rebuild the information in subsequent passes of fusion?
1542 // Note: Need to forget the loops before merging the loop latches, as
1543 // mergeLatch may remove the only block in FC1.
1544 SE.forgetLoop(FC1.L);
1545 SE.forgetLoop(FC0.L);
1546
1547 // Merge the loops.
1548 SmallVector<BasicBlock *, 8> Blocks(FC1.L->blocks());
1549 for (BasicBlock *BB : Blocks) {
1550 FC0.L->addBlockEntry(BB);
1551 FC1.L->removeBlockFromLoop(BB);
1552 if (LI.getLoopFor(BB) != FC1.L)
1553 continue;
1554 LI.changeLoopFor(BB, FC0.L);
1555 }
1556 while (!FC1.L->isInnermost()) {
1557 const auto &ChildLoopIt = FC1.L->begin();
1558 Loop *ChildLoop = *ChildLoopIt;
1559 FC1.L->removeChildLoop(ChildLoopIt);
1560 FC0.L->addChildLoop(ChildLoop);
1561 }
1562
1563 // Delete the now empty loop L1.
1564 LI.erase(FC1.L);
1565
1566 // Forget block dispositions as well, so that there are no dangling
1567 // pointers to erased/free'ed blocks. It should be done after mergeLatch()
1568 // since merging the latches may affect the dispositions.
1569 SE.forgetBlockAndLoopDispositions();
1570
1571 // Move instructions from FC0.Latch to FC1.Latch.
1572 // Note: mergeLatch requires an updated DT.
1573 mergeLatch(FC0, FC1);
1574
1575#ifndef NDEBUG
1576 assert(!verifyFunction(*FC0.Header->getParent(), &errs()));
1577 assert(DT.verify(DominatorTree::VerificationLevel::Fast));
1578 assert(PDT.verify());
1579 LI.verify(DT);
1580 SE.verify();
1581#endif
1582
1583 LLVM_DEBUG(dbgs() << "Fusion done:\n");
1584
1585 return FC0.L;
1586 }
1587
1588 /// Report details on loop fusion opportunities.
1589 ///
1590 /// This template function can be used to report both successful and missed
1591 /// loop fusion opportunities, based on the RemarkKind. The RemarkKind should
1592 /// be one of:
1593 /// - OptimizationRemarkMissed to report when loop fusion is unsuccessful
1594 /// given two valid fusion candidates.
1595 /// - OptimizationRemark to report successful fusion of two fusion
1596 /// candidates.
1597 /// The remarks will be printed using the form:
1598 /// <path/filename>:<line number>:<column number>: [<function name>]:
1599 /// <Cand1 Preheader> and <Cand2 Preheader>: <Stat Description>
1600 template <typename RemarkKind>
1601 void reportLoopFusion(const FusionCandidate &FC0, const FusionCandidate &FC1,
1602 StringRef RemarkName, StringRef RemarkMsg) {
1603 assert(FC0.Preheader && FC1.Preheader &&
1604 "Expecting valid fusion candidates");
1605 using namespace ore;
1606 ORE.emit(
1607 RemarkKind(DEBUG_TYPE, RemarkName, FC0.L->getStartLoc(), FC0.Preheader)
1608 << "[" << FC0.Preheader->getParent()->getName()
1609 << "]: " << NV("Cand1", StringRef(FC0.Preheader->getName())) << " and "
1610 << NV("Cand2", StringRef(FC1.Preheader->getName())) << ": "
1611 << RemarkMsg);
1612 }
1613
1614 /// Fuse two guarded fusion candidates, creating a new fused loop.
1615 ///
1616 /// Fusing guarded loops is handled much the same way as fusing non-guarded
1617 /// loops. The rewiring of the CFG is slightly different though, because of
1618 /// the presence of the guards around the loops and the exit blocks after the
1619 /// loop body. As such, the new loop is rewired as follows:
1620 /// 1. Keep the guard branch from FC0 and use the non-loop block target
1621 /// from the FC1 guard branch.
1622 /// 2. Remove the exit block from FC0 (this exit block should be empty
1623 /// right now).
1624 /// 3. Remove the guard branch for FC1
1625 /// 4. Remove the preheader for FC1.
1626 /// The exit block successor for the latch of FC0 is updated to be the header
1627 /// of FC1 and the non-exit block successor of the latch of FC1 is updated to
1628 /// be the header of FC0, thus creating the fused loop.
1629 Loop *fuseGuardedLoops(const FusionCandidate &FC0,
1630 const FusionCandidate &FC1) {
1631 assert(FC0.GuardBranch && FC1.GuardBranch && "Expecting guarded loops");
1632
1633 BasicBlock *FC0GuardBlock = FC0.GuardBranch->getParent();
1634 BasicBlock *FC1GuardBlock = FC1.GuardBranch->getParent();
1635 BasicBlock *FC0NonLoopBlock = FC0.getNonLoopBlock();
1636 BasicBlock *FC1NonLoopBlock = FC1.getNonLoopBlock();
1637 BasicBlock *FC0ExitBlockSuccessor = FC0.ExitBlock->getUniqueSuccessor();
1638
1639 // Move instructions from the exit block of FC0 to the beginning of the exit
1640 // block of FC1, in the case that the FC0 loop has not been peeled. In the
1641 // case that FC0 loop is peeled, then move the instructions of the successor
1642 // of the FC0 Exit block to the beginning of the exit block of FC1.
1644 (FC0.Peeled ? *FC0ExitBlockSuccessor : *FC0.ExitBlock), *FC1.ExitBlock,
1645 DT, PDT, DI, SE);
1646
1647 // Move instructions from the guard block of FC1 to the end of the guard
1648 // block of FC0.
1649 moveInstructionsToTheEnd(*FC1GuardBlock, *FC0GuardBlock, DT, PDT, DI, SE);
1650
1651 assert(FC0NonLoopBlock == FC1GuardBlock && "Loops are not adjacent");
1652
1654
1655 ////////////////////////////////////////////////////////////////////////////
1656 // Update the Loop Guard
1657 ////////////////////////////////////////////////////////////////////////////
1658 // The guard for FC0 is updated to guard both FC0 and FC1. This is done by
1659 // changing the NonLoopGuardBlock for FC0 to the NonLoopGuardBlock for FC1.
1660 // Thus, one path from the guard goes to the preheader for FC0 (and thus
1661 // executes the new fused loop) and the other path goes to the NonLoopBlock
1662 // for FC1 (where FC1 guard would have gone if FC1 was not executed).
1663 FC1NonLoopBlock->replacePhiUsesWith(FC1GuardBlock, FC0GuardBlock);
1664 FC0.GuardBranch->replaceUsesOfWith(FC0NonLoopBlock, FC1NonLoopBlock);
1665
1666 BasicBlock *BBToUpdate = FC0.Peeled ? FC0ExitBlockSuccessor : FC0.ExitBlock;
1667 BBToUpdate->getTerminator()->replaceUsesOfWith(FC1GuardBlock, FC1.Header);
1668
1669 // The guard of FC1 is not necessary anymore.
1670 FC1.GuardBranch->eraseFromParent();
1671 new UnreachableInst(FC1GuardBlock->getContext(), FC1GuardBlock);
1672
1673 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1674 DominatorTree::Delete, FC1GuardBlock, FC1.Preheader));
1675 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1676 DominatorTree::Delete, FC1GuardBlock, FC1NonLoopBlock));
1677 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1678 DominatorTree::Delete, FC0GuardBlock, FC1GuardBlock));
1679 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1680 DominatorTree::Insert, FC0GuardBlock, FC1NonLoopBlock));
1681
1682 if (FC0.Peeled) {
1683 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1684 DominatorTree::Delete, FC0.ExitBlock, FC0ExitBlockSuccessor));
1685 // Remove the Block after the ExitBlock of FC0
1686 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1687 DominatorTree::Delete, FC0ExitBlockSuccessor, FC1GuardBlock));
1688 FC0ExitBlockSuccessor->getTerminator()->eraseFromParent();
1689 new UnreachableInst(FC0ExitBlockSuccessor->getContext(),
1690 FC0ExitBlockSuccessor);
1691 }
1692
1693 assert(pred_empty(FC1GuardBlock) &&
1694 "Expecting guard block to have no predecessors");
1695 assert(succ_empty(FC1GuardBlock) &&
1696 "Expecting guard block to have no successors");
1697
1698 // Remember the phi nodes originally in the header of FC0 in order to rewire
1699 // them later. However, this is only necessary if the new loop carried
1700 // values might not dominate the exiting branch. While we do not generally
1701 // test if this is the case but simply insert intermediate phi nodes, we
1702 // need to make sure these intermediate phi nodes have different
1703 // predecessors. To this end, we filter the special case where the exiting
1704 // block is the latch block of the first loop. Nothing needs to be done
1705 // anyway as all loop carried values dominate the latch and thereby also the
1706 // exiting branch.
1707 // KB: This is no longer necessary because FC0.ExitingBlock == FC0.Latch
1708 // (because the loops are rotated. Thus, nothing will ever be added to
1709 // OriginalFC0PHIs.
1710 SmallVector<PHINode *, 8> OriginalFC0PHIs;
1711 if (FC0.ExitingBlock != FC0.Latch)
1712 for (PHINode &PHI : FC0.Header->phis())
1713 OriginalFC0PHIs.push_back(&PHI);
1714
1715 assert(OriginalFC0PHIs.empty() && "Expecting OriginalFC0PHIs to be empty!");
1716
1717 // Replace incoming blocks for header PHIs first.
1718 FC1.Preheader->replaceSuccessorsPhiUsesWith(FC0.Preheader);
1719 FC0.Latch->replaceSuccessorsPhiUsesWith(FC1.Latch);
1720
1721 // The old exiting block of the first loop (FC0) has to jump to the header
1722 // of the second as we need to execute the code in the second header block
1723 // regardless of the trip count. That is, if the trip count is 0, so the
1724 // back edge is never taken, we still have to execute both loop headers,
1725 // especially (but not only!) if the second is a do-while style loop.
1726 // However, doing so might invalidate the phi nodes of the first loop as
1727 // the new values do only need to dominate their latch and not the exiting
1728 // predicate. To remedy this potential problem we always introduce phi
1729 // nodes in the header of the second loop later that select the loop carried
1730 // value, if the second header was reached through an old latch of the
1731 // first, or undef otherwise. This is sound as exiting the first implies the
1732 // second will exit too, __without__ taking the back-edge (their
1733 // trip-counts are equal after all).
1734 FC0.ExitingBlock->getTerminator()->replaceUsesOfWith(FC0.ExitBlock,
1735 FC1.Header);
1736
1737 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1738 DominatorTree::Delete, FC0.ExitingBlock, FC0.ExitBlock));
1739 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1740 DominatorTree::Insert, FC0.ExitingBlock, FC1.Header));
1741
1742 // Remove FC0 Exit Block
1743 // The exit block for FC0 is no longer needed since control will flow
1744 // directly to the header of FC1. Since it is an empty block, it can be
1745 // removed at this point.
1746 // TODO: In the future, we can handle non-empty exit blocks my merging any
1747 // instructions from FC0 exit block into FC1 exit block prior to removing
1748 // the block.
1749 assert(pred_empty(FC0.ExitBlock) && "Expecting exit block to be empty");
1750 FC0.ExitBlock->getTerminator()->eraseFromParent();
1751 new UnreachableInst(FC0.ExitBlock->getContext(), FC0.ExitBlock);
1752
1753 // Remove FC1 Preheader
1754 // The pre-header of L1 is not necessary anymore.
1755 assert(pred_empty(FC1.Preheader));
1756 FC1.Preheader->getTerminator()->eraseFromParent();
1757 new UnreachableInst(FC1.Preheader->getContext(), FC1.Preheader);
1758 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1759 DominatorTree::Delete, FC1.Preheader, FC1.Header));
1760
1761 // Moves the phi nodes from the second to the first loops header block.
1762 while (PHINode *PHI = dyn_cast<PHINode>(&FC1.Header->front())) {
1763 if (SE.isSCEVable(PHI->getType()))
1764 SE.forgetValue(PHI);
1765 if (PHI->hasNUsesOrMore(1))
1766 PHI->moveBefore(FC0.Header->getFirstInsertionPt());
1767 else
1768 PHI->eraseFromParent();
1769 }
1770
1771 // Introduce new phi nodes in the second loop header to ensure
1772 // exiting the first and jumping to the header of the second does not break
1773 // the SSA property of the phis originally in the first loop. See also the
1774 // comment above.
1775 BasicBlock::iterator L1HeaderIP = FC1.Header->begin();
1776 for (PHINode *LCPHI : OriginalFC0PHIs) {
1777 int L1LatchBBIdx = LCPHI->getBasicBlockIndex(FC1.Latch);
1778 assert(L1LatchBBIdx >= 0 &&
1779 "Expected loop carried value to be rewired at this point!");
1780
1781 Value *LCV = LCPHI->getIncomingValue(L1LatchBBIdx);
1782
1783 PHINode *L1HeaderPHI =
1784 PHINode::Create(LCV->getType(), 2, LCPHI->getName() + ".afterFC0");
1785 L1HeaderPHI->insertBefore(L1HeaderIP);
1786 L1HeaderPHI->addIncoming(LCV, FC0.Latch);
1787 L1HeaderPHI->addIncoming(PoisonValue::get(LCV->getType()),
1788 FC0.ExitingBlock);
1789
1790 LCPHI->setIncomingValue(L1LatchBBIdx, L1HeaderPHI);
1791 }
1792
1793 // Update the latches
1794
1795 // Replace latch terminator destinations.
1796 FC0.Latch->getTerminator()->replaceUsesOfWith(FC0.Header, FC1.Header);
1797 FC1.Latch->getTerminator()->replaceUsesOfWith(FC1.Header, FC0.Header);
1798
1799 // Modify the latch branch of FC0 to be unconditional as both successors of
1800 // the branch are the same.
1801 simplifyLatchBranch(FC0);
1802
1803 // If FC0.Latch and FC0.ExitingBlock are the same then we have already
1804 // performed the updates above.
1805 if (FC0.Latch != FC0.ExitingBlock)
1806 TreeUpdates.emplace_back(DominatorTree::UpdateType(
1807 DominatorTree::Insert, FC0.Latch, FC1.Header));
1808
1809 TreeUpdates.emplace_back(DominatorTree::UpdateType(DominatorTree::Delete,
1810 FC0.Latch, FC0.Header));
1811 TreeUpdates.emplace_back(DominatorTree::UpdateType(DominatorTree::Insert,
1812 FC1.Latch, FC0.Header));
1813 TreeUpdates.emplace_back(DominatorTree::UpdateType(DominatorTree::Delete,
1814 FC1.Latch, FC1.Header));
1815
1816 // All done
1817 // Apply the updates to the Dominator Tree and cleanup.
1818
1819 assert(succ_empty(FC1GuardBlock) && "FC1GuardBlock has successors!!");
1820 assert(pred_empty(FC1GuardBlock) && "FC1GuardBlock has predecessors!!");
1821
1822 // Update DT/PDT
1823 DTU.applyUpdates(TreeUpdates);
1824
1825 LI.removeBlock(FC1GuardBlock);
1826 LI.removeBlock(FC1.Preheader);
1827 LI.removeBlock(FC0.ExitBlock);
1828 if (FC0.Peeled) {
1829 LI.removeBlock(FC0ExitBlockSuccessor);
1830 DTU.deleteBB(FC0ExitBlockSuccessor);
1831 }
1832 DTU.deleteBB(FC1GuardBlock);
1833 DTU.deleteBB(FC1.Preheader);
1834 DTU.deleteBB(FC0.ExitBlock);
1835 DTU.flush();
1836
1837 // Is there a way to keep SE up-to-date so we don't need to forget the loops
1838 // and rebuild the information in subsequent passes of fusion?
1839 // Note: Need to forget the loops before merging the loop latches, as
1840 // mergeLatch may remove the only block in FC1.
1841 SE.forgetLoop(FC1.L);
1842 SE.forgetLoop(FC0.L);
1843
1844 // Merge the loops.
1845 SmallVector<BasicBlock *, 8> Blocks(FC1.L->blocks());
1846 for (BasicBlock *BB : Blocks) {
1847 FC0.L->addBlockEntry(BB);
1848 FC1.L->removeBlockFromLoop(BB);
1849 if (LI.getLoopFor(BB) != FC1.L)
1850 continue;
1851 LI.changeLoopFor(BB, FC0.L);
1852 }
1853 while (!FC1.L->isInnermost()) {
1854 const auto &ChildLoopIt = FC1.L->begin();
1855 Loop *ChildLoop = *ChildLoopIt;
1856 FC1.L->removeChildLoop(ChildLoopIt);
1857 FC0.L->addChildLoop(ChildLoop);
1858 }
1859
1860 // Delete the now empty loop L1.
1861 LI.erase(FC1.L);
1862
1863 // Forget block dispositions as well, so that there are no dangling
1864 // pointers to erased/free'ed blocks. It should be done after mergeLatch()
1865 // since merging the latches may affect the dispositions.
1866 SE.forgetBlockAndLoopDispositions();
1867
1868 // Move instructions from FC0.Latch to FC1.Latch.
1869 // Note: mergeLatch requires an updated DT.
1870 mergeLatch(FC0, FC1);
1871
1872#ifndef NDEBUG
1873 assert(!verifyFunction(*FC0.Header->getParent(), &errs()));
1874 assert(DT.verify(DominatorTree::VerificationLevel::Fast));
1875 assert(PDT.verify());
1876 LI.verify(DT);
1877 SE.verify();
1878#endif
1879
1880 LLVM_DEBUG(dbgs() << "Fusion done:\n");
1881
1882 return FC0.L;
1883 }
1884};
1885} // namespace
1886
1888 auto &LI = AM.getResult<LoopAnalysis>(F);
1889 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
1890 auto &DI = AM.getResult<DependenceAnalysis>(F);
1891 auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
1892 auto &PDT = AM.getResult<PostDominatorTreeAnalysis>(F);
1894 auto &AC = AM.getResult<AssumptionAnalysis>(F);
1896
1897 // Ensure loops are in simplifed form which is a pre-requisite for loop fusion
1898 // pass. Added only for new PM since the legacy PM has already added
1899 // LoopSimplify pass as a dependency.
1900 bool Changed = false;
1901 for (auto &L : LI) {
1902 Changed |=
1903 simplifyLoop(L, &DT, &LI, &SE, &AC, nullptr, false /* PreserveLCSSA */);
1904 }
1905 if (Changed)
1906 PDT.recalculate(F);
1907
1908 LoopFuser LF(LI, DT, DI, SE, PDT, ORE, AC, TTI);
1909 Changed |= LF.fuseLoops(F);
1910 if (!Changed)
1911 return PreservedAnalyses::all();
1912
1917 PA.preserve<LoopAnalysis>();
1918 return PA;
1919}
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Rewrite undef for PHI
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static bool reportInvalidCandidate(const Instruction &I, llvm::Statistic &Stat)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:661
#define DEBUG_TYPE
static cl::opt< uint32_t > FusionPeelMaxCount("loop-fusion-peel-max-count", cl::init(0), cl::Hidden, cl::desc("Max number of iterations to be peeled from a loop, such that " "fusion can take place"))
static void printFusionCandidates(const FusionCandidateCollection &FusionCandidates)
Definition LoopFuse.cpp:396
std::list< FusionCandidate > FusionCandidateList
Definition LoopFuse.cpp:367
SmallVector< FusionCandidateList, 4 > FusionCandidateCollection
Definition LoopFuse.cpp:368
static void printLoopVector(const LoopVector &LV)
Definition LoopFuse.cpp:371
SmallVector< Loop *, 4 > LoopVector
Definition LoopFuse.cpp:362
static cl::opt< bool > VerboseFusionDebugging("loop-fusion-verbose-debug", cl::desc("Enable verbose debugging for Loop Fusion"), cl::Hidden, cl::init(false))
#define DEBUG_TYPE
Definition LoopFuse.cpp:70
This file implements the Loop Fusion pass.
Loop::LoopBounds::Direction Direction
Definition LoopInfo.cpp:253
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
ppc ctr loops verify
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
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:119
This pass exposes codegen information to IR-level passes.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
A function analysis which provides an AssumptionCache.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
LLVM_ABI void replaceSuccessorsPhiUsesWith(BasicBlock *Old, BasicBlock *New)
Update all phi nodes in this basic block's successors to refer to basic block New instead of basic bl...
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:461
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:530
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...
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
LLVM_ABI const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
const Instruction & front() const
Definition BasicBlock.h:484
LLVM_ABI void replacePhiUsesWith(BasicBlock *Old, BasicBlock *New)
Update all phi nodes in this basic block to refer to basic block New instead of basic block Old.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
size_t size() const
Definition BasicBlock.h:482
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
Conditional Branch instruction.
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
AnalysisPass to compute dependence information in a function.
Analysis pass which computes a DominatorTree.
Definition Dominators.h:270
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:151
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:587
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
bool isInnermost() const
Return true if the loop does not contain any (natural) loops.
void removeBlockFromLoop(BlockT *BB)
This removes the specified basic block from the current loop, updating the Blocks as appropriate.
unsigned getLoopDepth() const
Return the nesting level of this loop.
iterator_range< block_iterator > blocks() const
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
void addBlockEntry(BlockT *BB)
This adds a basic block directly to the basic block list.
iterator begin() const
LoopT * removeChildLoop(iterator I)
This removes the specified child from being a subloop of this loop.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
reverse_iterator rend() const
reverse_iterator rbegin() const
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
Definition LoopInfo.cpp:659
Diagnostic information for optimization analysis remarks.
The optimization diagnostic interface.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
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.
Analysis pass which computes a PostDominatorTree.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
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
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
LLVM_ABI bool hasLoopInvariantBackedgeTakenCount(const Loop *L)
Return true if the specified loop has an analyzable loop-invariant backedge-taken count.
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Analysis pass providing the TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
static UncondBrInst * Create(BasicBlock *Target, InsertPosition InsertBefore=nullptr)
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:25
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:318
self_iterator getIterator()
Definition ilist_node.h:123
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ Valid
The data is already valid.
initializer< Ty > init(const Ty &Val)
Add a small namespace to avoid name clashes with the classes used in the streaming interface.
DiagnosticInfoOptimizationBase::Argument NV
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
bool empty() const
Definition BasicBlock.h:101
iterator end() const
Definition BasicBlock.h:89
LLVM_ABI iterator begin() const
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Simplify each loop in a loop nest recursively.
LLVM_ABI void ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI, Instruction *I)
Replace the instruction specified by BI with the instruction specified by I.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
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:1668
bool succ_empty(const Instruction *I)
Definition CFG.h:141
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
LLVM_ABI void moveInstructionsToTheEnd(BasicBlock &FromBB, BasicBlock &ToBB, DominatorTree &DT, const PostDominatorTree &PDT, DependenceInfo &DI, ScalarEvolution &SE)
Move instructions, in an order-preserving manner, from FromBB to the end of ToBB when proven safe.
LLVM_ABI void moveInstructionsToTheBeginning(BasicBlock &FromBB, BasicBlock &ToBB, DominatorTree &DT, const PostDominatorTree &PDT, DependenceInfo &DI, ScalarEvolution &SE)
Move instructions, in an order-preserving manner, from FromBB to the beginning of ToBB when proven sa...
LLVM_ABI bool canPeel(const Loop *L)
Definition LoopPeel.cpp:97
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
LLVM_ABI TargetTransformInfo::PeelingPreferences gatherPeelingPreferences(Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI, std::optional< bool > UserAllowPeeling, std::optional< bool > UserAllowProfileBasedPeeling, bool UnrollingSpecficValues=false)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
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
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI void peelLoop(Loop *L, unsigned PeelCount, bool PeelLast, LoopInfo *LI, ScalarEvolution *SE, DominatorTree &DT, AssumptionCache *AC, bool PreserveLCSSA, ValueToValueMapTy &VMap)
VMap is the value-map that maps instructions from the original loop to instructions in the last peele...
TargetTransformInfo TTI
LLVM_ABI bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr, bool PredecessorWithTwoSuccessors=false, DominatorTree *DT=nullptr)
Attempts to merge a block into its predecessor, if possible.
LLVM_ABI void printLoop(const Loop &L, raw_ostream &OS, const std::string &Banner="")
Function to print a loop's contents as LLVM's text IR assembly.
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:107
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI bool isSafeToMoveBefore(Instruction &I, Instruction &InsertPoint, DominatorTree &DT, const PostDominatorTree *PDT=nullptr, DependenceInfo *DI=nullptr, bool CheckForEntireBlock=false)
Return true if I can be safely moved before InsertPoint.
unsigned PeelCount
A forced peeling factor (the number of bodied of the original loop that should be peeled off before t...