LLVM 23.0.0git
LoopUtils.cpp
Go to the documentation of this file.
1//===-- LoopUtils.cpp - Loop Utility functions -------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines common loop utility functions.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/DenseSet.h"
16#include "llvm/ADT/ScopeExit.h"
17#include "llvm/ADT/SetVector.h"
33#include "llvm/IR/DIBuilder.h"
34#include "llvm/IR/Dominators.h"
37#include "llvm/IR/MDBuilder.h"
38#include "llvm/IR/Module.h"
41#include "llvm/IR/ValueHandle.h"
43#include "llvm/Pass.h"
45#include "llvm/Support/Debug.h"
49
50using namespace llvm;
51using namespace llvm::PatternMatch;
52
53#define DEBUG_TYPE "loop-utils"
54
55static const char *LLVMLoopDisableNonforced = "llvm.loop.disable_nonforced";
56static const char *LLVMLoopDisableLICM = "llvm.licm.disable";
57namespace llvm {
59} // namespace llvm
60
62 MemorySSAUpdater *MSSAU,
63 bool PreserveLCSSA) {
64 bool Changed = false;
65
66 // We re-use a vector for the in-loop predecesosrs.
67 SmallVector<BasicBlock *, 4> InLoopPredecessors;
68
69 auto RewriteExit = [&](BasicBlock *BB) {
70 assert(InLoopPredecessors.empty() &&
71 "Must start with an empty predecessors list!");
72 llvm::scope_exit Cleanup([&] { InLoopPredecessors.clear(); });
73
74 // See if there are any non-loop predecessors of this exit block and
75 // keep track of the in-loop predecessors.
76 bool IsDedicatedExit = true;
77 for (auto *PredBB : predecessors(BB))
78 if (L->contains(PredBB)) {
79 if (isa<IndirectBrInst>(PredBB->getTerminator()))
80 // We cannot rewrite exiting edges from an indirectbr.
81 return false;
82
83 InLoopPredecessors.push_back(PredBB);
84 } else {
85 IsDedicatedExit = false;
86 }
87
88 assert(!InLoopPredecessors.empty() && "Must have *some* loop predecessor!");
89
90 // Nothing to do if this is already a dedicated exit.
91 if (IsDedicatedExit)
92 return false;
93
94 auto *NewExitBB = SplitBlockPredecessors(
95 BB, InLoopPredecessors, ".loopexit", DT, LI, MSSAU, PreserveLCSSA);
96
97 if (!NewExitBB)
99 dbgs() << "WARNING: Can't create a dedicated exit block for loop: "
100 << *L << "\n");
101 else
102 LLVM_DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "
103 << NewExitBB->getName() << "\n");
104 return true;
105 };
106
107 // Walk the exit blocks directly rather than building up a data structure for
108 // them, but only visit each one once.
110 for (auto *BB : L->blocks())
111 for (auto *SuccBB : successors(BB)) {
112 // We're looking for exit blocks so skip in-loop successors.
113 if (L->contains(SuccBB))
114 continue;
115
116 // Visit each exit block exactly once.
117 if (!Visited.insert(SuccBB).second)
118 continue;
119
120 Changed |= RewriteExit(SuccBB);
121 }
122
123 return Changed;
124}
125
126/// Returns the instructions that use values defined in the loop.
129
130 for (auto *Block : L->getBlocks())
131 // FIXME: I believe that this could use copy_if if the Inst reference could
132 // be adapted into a pointer.
133 for (auto &Inst : *Block) {
134 auto Users = Inst.users();
135 if (any_of(Users, [&](User *U) {
136 auto *Use = cast<Instruction>(U);
137 return !L->contains(Use->getParent());
138 }))
139 UsedOutside.push_back(&Inst);
140 }
141
142 return UsedOutside;
143}
144
146 // By definition, all loop passes need the LoopInfo analysis and the
147 // Dominator tree it depends on. Because they all participate in the loop
148 // pass manager, they must also preserve these.
153
154 // We must also preserve LoopSimplify and LCSSA. We locally access their IDs
155 // here because users shouldn't directly get them from this header.
156 extern char &LoopSimplifyID;
157 extern char &LCSSAID;
162 // This is used in the LPPassManager to perform LCSSA verification on passes
163 // which preserve lcssa form
166
167 // Loop passes are designed to run inside of a loop pass manager which means
168 // that any function analyses they require must be required by the first loop
169 // pass in the manager (so that it is computed before the loop pass manager
170 // runs) and preserved by all loop pasess in the manager. To make this
171 // reasonably robust, the set needed for most loop passes is maintained here.
172 // If your loop pass requires an analysis not listed here, you will need to
173 // carefully audit the loop pass manager nesting structure that results.
181 // FIXME: When all loop passes preserve MemorySSA, it can be required and
182 // preserved here instead of the individual handling in each pass.
183}
184
185/// Manually defined generic "LoopPass" dependency initialization. This is used
186/// to initialize the exact set of passes from above in \c
187/// getLoopAnalysisUsage. It can be used within a loop pass's initialization
188/// with:
189///
190/// INITIALIZE_PASS_DEPENDENCY(LoopPass)
191///
192/// As-if "LoopPass" were a pass.
205
206/// Create MDNode for input string.
207static MDNode *createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V) {
208 LLVMContext &Context = TheLoop->getHeader()->getContext();
209 Metadata *MDs[] = {
210 MDString::get(Context, Name),
211 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), V))};
212 return MDNode::get(Context, MDs);
213}
214
215/// Set input string into loop metadata by keeping other values intact.
216/// If the string is already in loop metadata update value if it is
217/// different.
218void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *StringMD,
219 unsigned V) {
221 // If the loop already has metadata, retain it.
222 MDNode *LoopID = TheLoop->getLoopID();
223 if (LoopID) {
224 for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
225 MDNode *Node = cast<MDNode>(LoopID->getOperand(i));
226 // If it is of form key = value, try to parse it.
227 if (Node->getNumOperands() == 2) {
228 MDString *S = dyn_cast<MDString>(Node->getOperand(0));
229 if (S && S->getString() == StringMD) {
230 ConstantInt *IntMD =
232 if (IntMD && IntMD->getSExtValue() == V)
233 // It is already in place. Do nothing.
234 return;
235 // We need to update the value, so just skip it here and it will
236 // be added after copying other existed nodes.
237 continue;
238 }
239 }
240 MDs.push_back(Node);
241 }
242 }
243 // Add new metadata.
244 MDs.push_back(createStringMetadata(TheLoop, StringMD, V));
245 // Replace current metadata node with new one.
246 LLVMContext &Context = TheLoop->getHeader()->getContext();
247 MDNode *NewLoopID = MDNode::get(Context, MDs);
248 // Set operand 0 to refer to the loop id itself.
249 NewLoopID->replaceOperandWith(0, NewLoopID);
250 TheLoop->setLoopID(NewLoopID);
251}
252
253std::optional<ElementCount>
255 std::optional<int> Width =
256 getOptionalIntLoopAttribute(TheLoop, "llvm.loop.vectorize.width");
257
258 if (Width) {
259 std::optional<int> IsScalable = getOptionalIntLoopAttribute(
260 TheLoop, "llvm.loop.vectorize.scalable.enable");
261 return ElementCount::get(*Width, IsScalable.value_or(false));
262 }
263
264 return std::nullopt;
265}
266
267std::optional<MDNode *> llvm::makeFollowupLoopID(
268 MDNode *OrigLoopID, ArrayRef<StringRef> FollowupOptions,
269 const char *InheritOptionsExceptPrefix, bool AlwaysNew) {
270 if (!OrigLoopID) {
271 if (AlwaysNew)
272 return nullptr;
273 return std::nullopt;
274 }
275
276 assert(OrigLoopID->getOperand(0) == OrigLoopID);
277
278 bool InheritAllAttrs = !InheritOptionsExceptPrefix;
279 bool InheritSomeAttrs =
280 InheritOptionsExceptPrefix && InheritOptionsExceptPrefix[0] != '\0';
282 MDs.push_back(nullptr);
283
284 bool Changed = false;
285 if (InheritAllAttrs || InheritSomeAttrs) {
286 for (const MDOperand &Existing : drop_begin(OrigLoopID->operands())) {
287 MDNode *Op = cast<MDNode>(Existing.get());
288
289 auto InheritThisAttribute = [InheritSomeAttrs,
290 InheritOptionsExceptPrefix](MDNode *Op) {
291 if (!InheritSomeAttrs)
292 return false;
293
294 // Skip malformatted attribute metadata nodes.
295 if (Op->getNumOperands() == 0)
296 return true;
297 Metadata *NameMD = Op->getOperand(0).get();
298 if (!isa<MDString>(NameMD))
299 return true;
300 StringRef AttrName = cast<MDString>(NameMD)->getString();
301
302 // Do not inherit excluded attributes.
303 return !AttrName.starts_with(InheritOptionsExceptPrefix);
304 };
305
306 if (InheritThisAttribute(Op))
307 MDs.push_back(Op);
308 else
309 Changed = true;
310 }
311 } else {
312 // Modified if we dropped at least one attribute.
313 Changed = OrigLoopID->getNumOperands() > 1;
314 }
315
316 bool HasAnyFollowup = false;
317 for (StringRef OptionName : FollowupOptions) {
318 MDNode *FollowupNode = findOptionMDForLoopID(OrigLoopID, OptionName);
319 if (!FollowupNode)
320 continue;
321
322 HasAnyFollowup = true;
323 for (const MDOperand &Option : drop_begin(FollowupNode->operands())) {
324 MDs.push_back(Option.get());
325 Changed = true;
326 }
327 }
328
329 // Attributes of the followup loop not specified explicity, so signal to the
330 // transformation pass to add suitable attributes.
331 if (!AlwaysNew && !HasAnyFollowup)
332 return std::nullopt;
333
334 // If no attributes were added or remove, the previous loop Id can be reused.
335 if (!AlwaysNew && !Changed)
336 return OrigLoopID;
337
338 // No attributes is equivalent to having no !llvm.loop metadata at all.
339 if (MDs.size() == 1)
340 return nullptr;
341
342 // Build the new loop ID.
343 MDTuple *FollowupLoopID = MDNode::get(OrigLoopID->getContext(), MDs);
344 FollowupLoopID->replaceOperandWith(0, FollowupLoopID);
345 return FollowupLoopID;
346}
347
351
355
357 if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable"))
358 return TM_SuppressedByUser;
359
360 std::optional<int> Count =
361 getOptionalIntLoopAttribute(L, "llvm.loop.unroll.count");
362 if (Count)
364
365 if (getBooleanLoopAttribute(L, "llvm.loop.unroll.enable"))
366 return TM_ForcedByUser;
367
368 if (getBooleanLoopAttribute(L, "llvm.loop.unroll.full"))
369 return TM_ForcedByUser;
370
372 return TM_Disable;
373
374 return TM_Unspecified;
375}
376
378 if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.disable"))
379 return TM_SuppressedByUser;
380
381 std::optional<int> Count =
382 getOptionalIntLoopAttribute(L, "llvm.loop.unroll_and_jam.count");
383 if (Count)
385
386 if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.enable"))
387 return TM_ForcedByUser;
388
390 return TM_Disable;
391
392 return TM_Unspecified;
393}
394
396 std::optional<bool> Enable =
397 getOptionalBoolLoopAttribute(L, "llvm.loop.vectorize.enable");
398
399 if (Enable == false)
400 return TM_SuppressedByUser;
401
402 std::optional<ElementCount> VectorizeWidth =
404 std::optional<int> InterleaveCount =
405 getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count");
406
407 // 'Forcing' vector width and interleave count to one effectively disables
408 // this tranformation.
409 if (Enable == true && VectorizeWidth && VectorizeWidth->isScalar() &&
410 InterleaveCount == 1)
411 return TM_SuppressedByUser;
412
413 if (getBooleanLoopAttribute(L, "llvm.loop.isvectorized"))
414 return TM_Disable;
415
416 if (Enable == true)
417 return TM_ForcedByUser;
418
419 if ((VectorizeWidth && VectorizeWidth->isScalar()) && InterleaveCount == 1)
420 return TM_Disable;
421
422 if ((VectorizeWidth && VectorizeWidth->isVector()) || InterleaveCount > 1)
423 return TM_Enable;
424
426 return TM_Disable;
427
428 return TM_Unspecified;
429}
430
432 if (getBooleanLoopAttribute(L, "llvm.loop.distribute.enable"))
433 return TM_ForcedByUser;
434
436 return TM_Disable;
437
438 return TM_Unspecified;
439}
440
442 if (getBooleanLoopAttribute(L, "llvm.loop.licm_versioning.disable"))
443 return TM_SuppressedByUser;
444
446 return TM_Disable;
447
448 return TM_Unspecified;
449}
450
451/// Does a BFS from a given node to all of its children inside a given loop.
452/// The returned vector of basic blocks includes the starting point.
454 DomTreeNode *N,
455 const Loop *CurLoop) {
457 auto AddRegionToWorklist = [&](DomTreeNode *DTN) {
458 // Only include subregions in the top level loop.
459 BasicBlock *BB = DTN->getBlock();
460 if (CurLoop->contains(BB))
461 Worklist.push_back(DTN->getBlock());
462 };
463
464 AddRegionToWorklist(N);
465
466 for (size_t I = 0; I < Worklist.size(); I++) {
467 for (DomTreeNode *Child : DT->getNode(Worklist[I])->children())
468 AddRegionToWorklist(Child);
469 }
470
471 return Worklist;
472}
473
475 int LatchIdx = PN->getBasicBlockIndex(LatchBlock);
476 assert(LatchIdx != -1 && "LatchBlock is not a case in this PHINode");
477 Value *IncV = PN->getIncomingValue(LatchIdx);
478
479 for (User *U : PN->users())
480 if (U != Cond && U != IncV) return false;
481
482 for (User *U : IncV->users())
483 if (U != Cond && U != PN) return false;
484 return true;
485}
486
487
489 LoopInfo *LI, MemorySSA *MSSA) {
490 assert((!DT || L->isLCSSAForm(*DT)) && "Expected LCSSA!");
491 auto *Preheader = L->getLoopPreheader();
492 assert(Preheader && "Preheader should exist!");
493
494 std::unique_ptr<MemorySSAUpdater> MSSAU;
495 if (MSSA)
496 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
497
498 // Now that we know the removal is safe, remove the loop by changing the
499 // branch from the preheader to go to the single exit block.
500 //
501 // Because we're deleting a large chunk of code at once, the sequence in which
502 // we remove things is very important to avoid invalidation issues.
503
504 // Tell ScalarEvolution that the loop is deleted. Do this before
505 // deleting the loop so that ScalarEvolution can look at the loop
506 // to determine what it needs to clean up.
507 if (SE) {
508 SE->forgetLoop(L);
510 }
511
512 Instruction *OldTerm = Preheader->getTerminator();
513 assert(!OldTerm->mayHaveSideEffects() &&
514 "Preheader must end with a side-effect-free terminator");
515 assert(OldTerm->getNumSuccessors() == 1 &&
516 "Preheader must have a single successor");
517 // Connect the preheader to the exit block. Keep the old edge to the header
518 // around to perform the dominator tree update in two separate steps
519 // -- #1 insertion of the edge preheader -> exit and #2 deletion of the edge
520 // preheader -> header.
521 //
522 //
523 // 0. Preheader 1. Preheader 2. Preheader
524 // | | | |
525 // V | V |
526 // Header <--\ | Header <--\ | Header <--\
527 // | | | | | | | | | | |
528 // | V | | | V | | | V |
529 // | Body --/ | | Body --/ | | Body --/
530 // V V V V V
531 // Exit Exit Exit
532 //
533 // By doing this is two separate steps we can perform the dominator tree
534 // update without using the batch update API.
535 //
536 // Even when the loop is never executed, we cannot remove the edge from the
537 // source block to the exit block. Consider the case where the unexecuted loop
538 // branches back to an outer loop. If we deleted the loop and removed the edge
539 // coming to this inner loop, this will break the outer loop structure (by
540 // deleting the backedge of the outer loop). If the outer loop is indeed a
541 // non-loop, it will be deleted in a future iteration of loop deletion pass.
542 IRBuilder<> Builder(OldTerm);
543
544 auto *ExitBlock = L->getUniqueExitBlock();
545 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
546 if (ExitBlock) {
547 assert(ExitBlock && "Should have a unique exit block!");
548 assert(L->hasDedicatedExits() && "Loop should have dedicated exits!");
549
550 Builder.CreateCondBr(Builder.getFalse(), L->getHeader(), ExitBlock);
551 // Remove the old branch. The conditional branch becomes a new terminator.
552 OldTerm->eraseFromParent();
553
554 // Rewrite phis in the exit block to get their inputs from the Preheader
555 // instead of the exiting block.
556 for (PHINode &P : ExitBlock->phis()) {
557 // Set the zero'th element of Phi to be from the preheader and remove all
558 // other incoming values. Given the loop has dedicated exits, all other
559 // incoming values must be from the exiting blocks.
560 int PredIndex = 0;
561 P.setIncomingBlock(PredIndex, Preheader);
562 // Removes all incoming values from all other exiting blocks (including
563 // duplicate values from an exiting block).
564 // Nuke all entries except the zero'th entry which is the preheader entry.
565 P.removeIncomingValueIf([](unsigned Idx) { return Idx != 0; },
566 /* DeletePHIIfEmpty */ false);
567
568 assert((P.getNumIncomingValues() == 1 &&
569 P.getIncomingBlock(PredIndex) == Preheader) &&
570 "Should have exactly one value and that's from the preheader!");
571 }
572
573 if (DT) {
574 DTU.applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}});
575 if (MSSA) {
576 MSSAU->applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}},
577 *DT);
578 if (VerifyMemorySSA)
579 MSSA->verifyMemorySSA();
580 }
581 }
582
583 // Disconnect the loop body by branching directly to its exit.
584 Builder.SetInsertPoint(Preheader->getTerminator());
585 Builder.CreateBr(ExitBlock);
586 // Remove the old branch.
587 Preheader->getTerminator()->eraseFromParent();
588 } else {
589 assert(L->hasNoExitBlocks() &&
590 "Loop should have either zero or one exit blocks.");
591
592 Builder.SetInsertPoint(OldTerm);
593 Builder.CreateUnreachable();
594 Preheader->getTerminator()->eraseFromParent();
595 }
596
597 if (DT) {
598 DTU.applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}});
599 if (MSSA) {
600 MSSAU->applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}},
601 *DT);
602 SmallSetVector<BasicBlock *, 8> DeadBlockSet(L->block_begin(),
603 L->block_end());
604 MSSAU->removeBlocks(DeadBlockSet);
605 if (VerifyMemorySSA)
606 MSSA->verifyMemorySSA();
607 }
608 }
609
610 // Use a map to unique and a vector to guarantee deterministic ordering.
612 llvm::SmallVector<DbgVariableRecord *, 4> DeadDbgVariableRecords;
613
614 // Given LCSSA form is satisfied, we should not have users of instructions
615 // within the dead loop outside of the loop. However, LCSSA doesn't take
616 // unreachable uses into account. We handle them here.
617 // We could do it after drop all references (in this case all users in the
618 // loop will be already eliminated and we have less work to do but according
619 // to API doc of User::dropAllReferences only valid operation after dropping
620 // references, is deletion. So let's substitute all usages of
621 // instruction from the loop with poison value of corresponding type first.
622 for (auto *Block : L->blocks())
623 for (Instruction &I : *Block) {
624 auto *Poison = PoisonValue::get(I.getType());
625 for (Use &U : llvm::make_early_inc_range(I.uses())) {
626 if (auto *Usr = dyn_cast<Instruction>(U.getUser()))
627 if (L->contains(Usr->getParent()))
628 continue;
629 // If we have a DT then we can check that uses outside a loop only in
630 // unreachable block.
631 if (DT)
633 "Unexpected user in reachable block");
634 U.set(Poison);
635 }
636
637 if (ExitBlock) {
638 // For one of each variable encountered, preserve a debug record (set
639 // to Poison) and transfer it to the loop exit. This terminates any
640 // variable locations that were set during the loop.
641 for (DbgVariableRecord &DVR :
642 llvm::make_early_inc_range(filterDbgVars(I.getDbgRecordRange()))) {
643 DebugVariable Key(DVR.getVariable(), DVR.getExpression(),
644 DVR.getDebugLoc().get());
645 if (!DeadDebugSet.insert(Key).second)
646 continue;
647 // Unlinks the DVR from it's container, for later insertion.
648 DVR.removeFromParent();
649 DeadDbgVariableRecords.push_back(&DVR);
650 }
651 }
652 }
653
654 if (ExitBlock) {
655 // After the loop has been deleted all the values defined and modified
656 // inside the loop are going to be unavailable. Values computed in the
657 // loop will have been deleted, automatically causing their debug uses
658 // be be replaced with undef. Loop invariant values will still be available.
659 // Move dbg.values out the loop so that earlier location ranges are still
660 // terminated and loop invariant assignments are preserved.
661 DIBuilder DIB(*ExitBlock->getModule());
662 BasicBlock::iterator InsertDbgValueBefore =
663 ExitBlock->getFirstInsertionPt();
664 assert(InsertDbgValueBefore != ExitBlock->end() &&
665 "There should be a non-PHI instruction in exit block, else these "
666 "instructions will have no parent.");
667
668 // Due to the "head" bit in BasicBlock::iterator, we're going to insert
669 // each DbgVariableRecord right at the start of the block, wheras dbg.values
670 // would be repeatedly inserted before the first instruction. To replicate
671 // this behaviour, do it backwards.
672 for (DbgVariableRecord *DVR : llvm::reverse(DeadDbgVariableRecords))
673 ExitBlock->insertDbgRecordBefore(DVR, InsertDbgValueBefore);
674 }
675
676 // Remove the block from the reference counting scheme, so that we can
677 // delete it freely later.
678 for (auto *Block : L->blocks())
679 Block->dropAllReferences();
680
681 if (MSSA && VerifyMemorySSA)
682 MSSA->verifyMemorySSA();
683
684 if (LI) {
686
687 // Erase the instructions and the blocks without having to worry
688 // about ordering because we already dropped the references.
689 // Remove blocks from loopinfo before erasing them, otherwise the loopinfo
690 // cannot find the loop using block numbers.
691 for (BasicBlock *BB : Blocks) {
692 LI->removeBlock(BB);
693 BB->eraseFromParent();
694 }
695
696 // The last step is to update LoopInfo now that we've eliminated this loop.
697 // Note: LoopInfo::erase remove the given loop and relink its subloops with
698 // its parent. While removeLoop/removeChildLoop remove the given loop but
699 // not relink its subloops, which is what we want.
700 if (Loop *ParentLoop = L->getParentLoop()) {
701 Loop::iterator I = find(*ParentLoop, L);
702 assert(I != ParentLoop->end() && "Couldn't find loop");
703 ParentLoop->removeChildLoop(I);
704 } else {
705 Loop::iterator I = find(*LI, L);
706 assert(I != LI->end() && "Couldn't find loop");
707 LI->removeLoop(I);
708 }
709 LI->destroy(L);
710 }
711}
712
714 LoopInfo &LI, MemorySSA *MSSA) {
715 auto *Latch = L->getLoopLatch();
716 assert(Latch && "multiple latches not yet supported");
717 auto *Header = L->getHeader();
718 Loop *OutermostLoop = L->getOutermostLoop();
719
720 SE.forgetLoop(L);
722
723 std::unique_ptr<MemorySSAUpdater> MSSAU;
724 if (MSSA)
725 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
726
727 // Update the CFG and domtree. We chose to special case a couple of
728 // of common cases for code quality and test readability reasons.
729 [&]() -> void {
730 if (auto *BI = dyn_cast<UncondBrInst>(Latch->getTerminator())) {
731 DomTreeUpdater DTU(&DT, DomTreeUpdater::UpdateStrategy::Eager);
732 (void)changeToUnreachable(BI, /*PreserveLCSSA*/ true, &DTU, MSSAU.get());
733 return;
734 }
735 if (auto *BI = dyn_cast<CondBrInst>(Latch->getTerminator())) {
736 // Conditional latch/exit - note that latch can be shared by inner
737 // and outer loop so the other target doesn't need to an exit
738 if (L->isLoopExiting(Latch)) {
739 // TODO: Generalize ConstantFoldTerminator so that it can be used
740 // here without invalidating LCSSA or MemorySSA. (Tricky case for
741 // LCSSA: header is an exit block of a preceeding sibling loop w/o
742 // dedicated exits.)
743 const unsigned ExitIdx = L->contains(BI->getSuccessor(0)) ? 1 : 0;
744 BasicBlock *ExitBB = BI->getSuccessor(ExitIdx);
745
746 DomTreeUpdater DTU(&DT, DomTreeUpdater::UpdateStrategy::Eager);
747 Header->removePredecessor(Latch, true);
748
749 IRBuilder<> Builder(BI);
750 auto *NewBI = Builder.CreateBr(ExitBB);
751 // Transfer the metadata to the new branch instruction (minus the
752 // loop info since this is no longer a loop)
753 NewBI->copyMetadata(*BI, {LLVMContext::MD_dbg,
754 LLVMContext::MD_annotation});
755
756 BI->eraseFromParent();
757 DTU.applyUpdates({{DominatorTree::Delete, Latch, Header}});
758 if (MSSA)
759 MSSAU->applyUpdates({{DominatorTree::Delete, Latch, Header}}, DT);
760 return;
761 }
762 }
763
764 // General case. By splitting the backedge, and then explicitly making it
765 // unreachable we gracefully handle corner cases such as switch and invoke
766 // termiantors.
767 auto *BackedgeBB = SplitEdge(Latch, Header, &DT, &LI, MSSAU.get());
768
769 DomTreeUpdater DTU(&DT, DomTreeUpdater::UpdateStrategy::Eager);
770 (void)changeToUnreachable(BackedgeBB->getTerminator(),
771 /*PreserveLCSSA*/ true, &DTU, MSSAU.get());
772 }();
773
774 // Erase (and destroy) this loop instance. Handles relinking sub-loops
775 // and blocks within the loop as needed.
776 LI.erase(L);
777
778 // If the loop we broke had a parent, then changeToUnreachable might have
779 // caused a block to be removed from the parent loop (see loop_nest_lcssa
780 // test case in zero-btc.ll for an example), thus changing the parent's
781 // exit blocks. If that happened, we need to rebuild LCSSA on the outermost
782 // loop which might have a had a block removed.
783 if (OutermostLoop != L)
784 formLCSSARecursively(*OutermostLoop, DT, &LI, &SE);
785}
786
787
788/// Checks if \p L has an exiting latch branch. There may also be other
789/// exiting blocks. Returns branch instruction terminating the loop
790/// latch if above check is successful, nullptr otherwise.
792 BasicBlock *Latch = L->getLoopLatch();
793 if (!Latch)
794 return nullptr;
795
796 CondBrInst *LatchBR = dyn_cast<CondBrInst>(Latch->getTerminator());
797 if (!LatchBR || !L->isLoopExiting(Latch))
798 return nullptr;
799
800 assert((LatchBR->getSuccessor(0) == L->getHeader() ||
801 LatchBR->getSuccessor(1) == L->getHeader()) &&
802 "At least one edge out of the latch must go to the header");
803
804 return LatchBR;
805}
806
807struct DbgLoop {
808 const Loop *L;
809 explicit DbgLoop(const Loop *L) : L(L) {}
810};
811
812#ifndef NDEBUG
814 OS << "function ";
815 D.L->getHeader()->getParent()->printAsOperand(OS, /*PrintType=*/false);
816 return OS << " " << *D.L;
817}
818#endif // NDEBUG
819
820static std::optional<unsigned> estimateLoopTripCount(Loop *L) {
821 // Currently we take the estimate exit count only from the loop latch,
822 // ignoring other exiting blocks. This can overestimate the trip count
823 // if we exit through another exit, but can never underestimate it.
824 // TODO: incorporate information from other exits
825 CondBrInst *ExitingBranch = getExpectedExitLoopLatchBranch(L);
826 if (!ExitingBranch) {
827 LLVM_DEBUG(dbgs() << "estimateLoopTripCount: Failed to find exiting "
828 << "latch branch of required form in " << DbgLoop(L)
829 << "\n");
830 return std::nullopt;
831 }
832
833 // To estimate the number of times the loop body was executed, we want to
834 // know the number of times the backedge was taken, vs. the number of times
835 // we exited the loop.
836 uint64_t LoopWeight, ExitWeight;
837 if (!extractBranchWeights(*ExitingBranch, LoopWeight, ExitWeight)) {
838 LLVM_DEBUG(dbgs() << "estimateLoopTripCount: Failed to extract branch "
839 << "weights for " << DbgLoop(L) << "\n");
840 return std::nullopt;
841 }
842
843 if (L->contains(ExitingBranch->getSuccessor(1)))
844 std::swap(LoopWeight, ExitWeight);
845
846 if (!ExitWeight) {
847 // Don't have a way to return predicated infinite
848 LLVM_DEBUG(dbgs() << "estimateLoopTripCount: Failed because of zero exit "
849 << "probability for " << DbgLoop(L) << "\n");
850 return std::nullopt;
851 }
852
853 // Estimated exit count is a ratio of the loop weight by the weight of the
854 // edge exiting the loop, rounded to nearest.
855 uint64_t ExitCount = llvm::divideNearest(LoopWeight, ExitWeight);
856
857 // When ExitCount + 1 would wrap in unsigned, saturate at UINT_MAX.
858 if (ExitCount >= std::numeric_limits<unsigned>::max())
859 return std::numeric_limits<unsigned>::max();
860
861 // Estimated trip count is one plus estimated exit count.
862 uint64_t TC = ExitCount + 1;
863 LLVM_DEBUG(dbgs() << "estimateLoopTripCount: Estimated trip count of " << TC
864 << " for " << DbgLoop(L) << "\n");
865 return TC;
866}
867
868std::optional<unsigned>
870 unsigned *EstimatedLoopInvocationWeight) {
871 // If EstimatedLoopInvocationWeight, we do not support this loop if
872 // getExpectedExitLoopLatchBranch returns nullptr.
873 //
874 // FIXME: Also, this is a stop-gap solution for nested loops. It avoids
875 // mistaking LLVMLoopEstimatedTripCount metadata to be for an outer loop when
876 // it was created for an inner loop. The problem is that loop metadata is
877 // attached to the branch instruction in the loop latch block, but that can be
878 // shared by the loops. A solution is to attach loop metadata to loop headers
879 // instead, but that would be a large change to LLVM.
880 //
881 // Until that happens, we work around the problem as follows.
882 // getExpectedExitLoopLatchBranch (which also guards
883 // setLoopEstimatedTripCount) returns nullptr for a loop unless the loop has
884 // one latch and that latch has exactly two successors one of which is an exit
885 // from the loop. If the latch is shared by nested loops, then that condition
886 // might hold for the inner loop but cannot hold for the outer loop:
887 // - Because the latch is shared, it must have at least two successors: the
888 // inner loop header and the outer loop header, which is also an exit for
889 // the inner loop. That satisifies the condition for the inner loop.
890 // - To satsify the condition for the outer loop, the latch must have a third
891 // successor that is an exit for the outer loop. But that violates the
892 // condition for both loops.
893 CondBrInst *ExitingBranch = getExpectedExitLoopLatchBranch(L);
894 if (!ExitingBranch)
895 return std::nullopt;
896
897 // If requested, either compute *EstimatedLoopInvocationWeight or return
898 // nullopt if cannot.
899 //
900 // TODO: Eventually, once all passes have migrated away from setting branch
901 // weights to indicate estimated trip counts, this function will drop the
902 // EstimatedLoopInvocationWeight parameter.
903 if (EstimatedLoopInvocationWeight) {
904 uint64_t LoopWeight = 0, ExitWeight = 0; // Inits expected to be unused.
905 if (!extractBranchWeights(*ExitingBranch, LoopWeight, ExitWeight))
906 return std::nullopt;
907 if (L->contains(ExitingBranch->getSuccessor(1)))
908 std::swap(LoopWeight, ExitWeight);
909 if (!ExitWeight)
910 return std::nullopt;
911 *EstimatedLoopInvocationWeight = ExitWeight;
912 }
913
914 // Return the estimated trip count from metadata unless the metadata is
915 // missing or has no value.
916 //
917 // Some passes set llvm.loop.estimated_trip_count to 0. For example, after
918 // peeling 10 or more iterations from a loop with an estimated trip count of
919 // 10, llvm.loop.estimated_trip_count becomes 0 on the remaining loop. It
920 // indicates that, each time execution reaches the peeled iterations,
921 // execution is estimated to exit them without reaching the remaining loop's
922 // header.
923 //
924 // Even if the probability of reaching a loop's header is low, if it is
925 // reached, it is the start of an iteration. Consequently, some passes
926 // historically assume that llvm::getLoopEstimatedTripCount always returns a
927 // positive count or std::nullopt. Thus, return std::nullopt when
928 // llvm.loop.estimated_trip_count is 0.
930 LLVM_DEBUG(dbgs() << "getLoopEstimatedTripCount: "
931 << LLVMLoopEstimatedTripCount << " metadata has trip "
932 << "count of " << *TC
933 << (*TC == 0 ? " (returning std::nullopt)" : "")
934 << " for " << DbgLoop(L) << "\n");
935 return *TC == 0 ? std::nullopt : std::optional(*TC);
936 }
937
938 // Estimate the trip count from latch branch weights.
939 return estimateLoopTripCount(L);
940}
941
943 Loop *L, unsigned EstimatedTripCount,
944 std::optional<unsigned> EstimatedloopInvocationWeight) {
945 // If EstimatedLoopInvocationWeight, we do not support this loop if
946 // getExpectedExitLoopLatchBranch returns nullptr.
947 //
948 // FIXME: See comments in getLoopEstimatedTripCount for why this is required
949 // here regardless of EstimatedLoopInvocationWeight.
951 if (!LatchBranch)
952 return false;
953
954 // Set the metadata.
956
957 // At the moment, we currently support changing the estimated trip count in
958 // the latch branch's branch weights only. We could extend this API to
959 // manipulate estimated trip counts for any exit.
960 //
961 // TODO: Eventually, once all passes have migrated away from setting branch
962 // weights to indicate estimated trip counts, we will not set branch weights
963 // here at all.
964 if (!EstimatedloopInvocationWeight)
965 return true;
966
967 // Calculate taken and exit weights.
968 unsigned LatchExitWeight = ProfcheckDisableMetadataFixes ? 0 : 1;
969 unsigned BackedgeTakenWeight = 0;
970
971 if (EstimatedTripCount != 0) {
972 LatchExitWeight = *EstimatedloopInvocationWeight;
973 BackedgeTakenWeight = (EstimatedTripCount - 1) * LatchExitWeight;
974 }
975
976 // Make a swap if back edge is taken when condition is "false".
977 if (LatchBranch->getSuccessor(0) != L->getHeader())
978 std::swap(BackedgeTakenWeight, LatchExitWeight);
979
980 // Set/Update profile metadata.
981 setBranchWeights(*LatchBranch, {BackedgeTakenWeight, LatchExitWeight},
982 /*IsExpected=*/false);
983
984 return true;
985}
986
989 if (!LatchBranch)
991 bool FirstTargetIsLoop = LatchBranch->getSuccessor(0) == L->getHeader();
992 return getBranchProbability(LatchBranch, FirstTargetIsLoop);
993}
994
997 if (!LatchBranch)
998 return false;
999 bool FirstTargetIsLoop = LatchBranch->getSuccessor(0) == L->getHeader();
1000 setBranchProbability(LatchBranch, P, FirstTargetIsLoop);
1001 return true;
1002}
1003
1005 bool ForFirstTarget) {
1006 uint64_t Weight0, Weight1;
1007 if (!extractBranchWeights(*B, Weight0, Weight1))
1009 uint64_t Denominator = Weight0 + Weight1;
1010 if (Denominator == 0)
1012 if (!ForFirstTarget)
1013 std::swap(Weight0, Weight1);
1014 return BranchProbability::getBranchProbability(Weight0, Denominator);
1015}
1016
1018 assert(Src != Dst && "Passed in same source as destination");
1019
1020 Instruction *TI = Src->getTerminator();
1021 if (!TI || TI->getNumSuccessors() == 0)
1023
1025
1026 if (!extractBranchWeights(*TI, Weights)) {
1027 // No metadata
1029 }
1030 assert(TI->getNumSuccessors() == Weights.size() &&
1031 "Missing weights in branch_weights");
1032
1033 uint64_t Total = 0;
1034 uint32_t Numerator = 0;
1035 for (auto [i, Weight] : llvm::enumerate(Weights)) {
1036 if (TI->getSuccessor(i) == Dst)
1037 Numerator += Weight;
1038 Total += Weight;
1039 }
1040
1041 // Total of edges might be 0 if the metadata is incorrect/set by hand
1042 // or missing. In such case return here to avoid division by 0 later on.
1043 // There might also be a case where the value of Total cannot fit into
1044 // uint32_t, in such case, just bail out.
1045 if (Total == 0 || Total > std::numeric_limits<uint32_t>::max())
1047
1048 return BranchProbability(Numerator, Total);
1049}
1050
1052 bool ForFirstTarget) {
1053 BranchProbability Prob0 = P;
1054 BranchProbability Prob1 = P.getCompl();
1055 if (!ForFirstTarget)
1056 std::swap(Prob0, Prob1);
1057 setBranchWeights(*B, {Prob0.getNumerator(), Prob1.getNumerator()},
1058 /*IsExpected=*/false);
1059}
1060
1062 ScalarEvolution &SE) {
1063 Loop *OuterL = InnerLoop->getParentLoop();
1064 if (!OuterL)
1065 return true;
1066
1067 // Get the backedge taken count for the inner loop
1068 BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
1069 const SCEV *InnerLoopBECountSC = SE.getExitCount(InnerLoop, InnerLoopLatch);
1070 if (isa<SCEVCouldNotCompute>(InnerLoopBECountSC) ||
1071 !InnerLoopBECountSC->getType()->isIntegerTy())
1072 return false;
1073
1074 // Get whether count is invariant to the outer loop
1076 SE.getLoopDisposition(InnerLoopBECountSC, OuterL);
1078 return false;
1079
1080 return true;
1081}
1082
1084 switch (RK) {
1085 default:
1086 llvm_unreachable("Unexpected recurrence kind");
1088 case RecurKind::Sub:
1089 case RecurKind::Add:
1090 return Intrinsic::vector_reduce_add;
1091 case RecurKind::Mul:
1092 return Intrinsic::vector_reduce_mul;
1093 case RecurKind::And:
1094 return Intrinsic::vector_reduce_and;
1095 case RecurKind::Or:
1096 return Intrinsic::vector_reduce_or;
1097 case RecurKind::Xor:
1098 return Intrinsic::vector_reduce_xor;
1099 case RecurKind::FMulAdd:
1100 case RecurKind::FAdd:
1101 return Intrinsic::vector_reduce_fadd;
1102 case RecurKind::FMul:
1103 return Intrinsic::vector_reduce_fmul;
1104 case RecurKind::SMax:
1105 return Intrinsic::vector_reduce_smax;
1106 case RecurKind::SMin:
1107 return Intrinsic::vector_reduce_smin;
1108 case RecurKind::UMax:
1109 return Intrinsic::vector_reduce_umax;
1110 case RecurKind::UMin:
1111 return Intrinsic::vector_reduce_umin;
1112 case RecurKind::FMax:
1113 case RecurKind::FMaxNum:
1114 return Intrinsic::vector_reduce_fmax;
1115 case RecurKind::FMin:
1116 case RecurKind::FMinNum:
1117 return Intrinsic::vector_reduce_fmin;
1119 return Intrinsic::vector_reduce_fmaximum;
1121 return Intrinsic::vector_reduce_fminimum;
1123 return Intrinsic::vector_reduce_fmax;
1125 return Intrinsic::vector_reduce_fmin;
1126 }
1127}
1128
1130 switch (IID) {
1131 default:
1132 llvm_unreachable("Unexpected intrinsic id");
1133 case Intrinsic::umin:
1134 return Intrinsic::vector_reduce_umin;
1135 case Intrinsic::umax:
1136 return Intrinsic::vector_reduce_umax;
1137 case Intrinsic::smin:
1138 return Intrinsic::vector_reduce_smin;
1139 case Intrinsic::smax:
1140 return Intrinsic::vector_reduce_smax;
1141 }
1142}
1143
1144// This is the inverse to getReductionForBinop
1146 switch (RdxID) {
1147 case Intrinsic::vector_reduce_fadd:
1148 return Instruction::FAdd;
1149 case Intrinsic::vector_reduce_fmul:
1150 return Instruction::FMul;
1151 case Intrinsic::vector_reduce_add:
1152 return Instruction::Add;
1153 case Intrinsic::vector_reduce_mul:
1154 return Instruction::Mul;
1155 case Intrinsic::vector_reduce_and:
1156 return Instruction::And;
1157 case Intrinsic::vector_reduce_or:
1158 return Instruction::Or;
1159 case Intrinsic::vector_reduce_xor:
1160 return Instruction::Xor;
1161 case Intrinsic::vector_reduce_smax:
1162 case Intrinsic::vector_reduce_smin:
1163 case Intrinsic::vector_reduce_umax:
1164 case Intrinsic::vector_reduce_umin:
1165 return Instruction::ICmp;
1166 case Intrinsic::vector_reduce_fmax:
1167 case Intrinsic::vector_reduce_fmin:
1168 return Instruction::FCmp;
1169 default:
1170 llvm_unreachable("Unexpected ID");
1171 }
1172}
1173
1174// This is the inverse to getArithmeticReductionInstruction
1176 switch (Opc) {
1177 default:
1178 break;
1179 case Instruction::Add:
1180 return Intrinsic::vector_reduce_add;
1181 case Instruction::Mul:
1182 return Intrinsic::vector_reduce_mul;
1183 case Instruction::And:
1184 return Intrinsic::vector_reduce_and;
1185 case Instruction::Or:
1186 return Intrinsic::vector_reduce_or;
1187 case Instruction::Xor:
1188 return Intrinsic::vector_reduce_xor;
1189 }
1191}
1192
1194 switch (RdxID) {
1195 default:
1196 llvm_unreachable("Unknown min/max recurrence kind");
1197 case Intrinsic::vector_reduce_umin:
1198 return Intrinsic::umin;
1199 case Intrinsic::vector_reduce_umax:
1200 return Intrinsic::umax;
1201 case Intrinsic::vector_reduce_smin:
1202 return Intrinsic::smin;
1203 case Intrinsic::vector_reduce_smax:
1204 return Intrinsic::smax;
1205 case Intrinsic::vector_reduce_fmin:
1206 return Intrinsic::minnum;
1207 case Intrinsic::vector_reduce_fmax:
1208 return Intrinsic::maxnum;
1209 case Intrinsic::vector_reduce_fminimum:
1210 return Intrinsic::minimum;
1211 case Intrinsic::vector_reduce_fmaximum:
1212 return Intrinsic::maximum;
1213 }
1214}
1215
1217 switch (RK) {
1218 default:
1219 llvm_unreachable("Unknown min/max recurrence kind");
1220 case RecurKind::UMin:
1221 return Intrinsic::umin;
1222 case RecurKind::UMax:
1223 return Intrinsic::umax;
1224 case RecurKind::SMin:
1225 return Intrinsic::smin;
1226 case RecurKind::SMax:
1227 return Intrinsic::smax;
1228 case RecurKind::FMin:
1229 case RecurKind::FMinNum:
1230 return Intrinsic::minnum;
1231 case RecurKind::FMax:
1232 case RecurKind::FMaxNum:
1233 return Intrinsic::maxnum;
1235 return Intrinsic::minimum;
1237 return Intrinsic::maximum;
1239 return Intrinsic::minimumnum;
1241 return Intrinsic::maximumnum;
1242 }
1243}
1244
1246 switch (RdxID) {
1247 case Intrinsic::vector_reduce_smax:
1248 return RecurKind::SMax;
1249 case Intrinsic::vector_reduce_smin:
1250 return RecurKind::SMin;
1251 case Intrinsic::vector_reduce_umax:
1252 return RecurKind::UMax;
1253 case Intrinsic::vector_reduce_umin:
1254 return RecurKind::UMin;
1255 case Intrinsic::vector_reduce_fmax:
1256 return RecurKind::FMax;
1257 case Intrinsic::vector_reduce_fmin:
1258 return RecurKind::FMin;
1259 default:
1260 return RecurKind::None;
1261 }
1262}
1263
1265 switch (RK) {
1266 default:
1267 llvm_unreachable("Unknown min/max recurrence kind");
1268 case RecurKind::UMin:
1269 return CmpInst::ICMP_ULT;
1270 case RecurKind::UMax:
1271 return CmpInst::ICMP_UGT;
1272 case RecurKind::SMin:
1273 return CmpInst::ICMP_SLT;
1274 case RecurKind::SMax:
1275 return CmpInst::ICMP_SGT;
1276 case RecurKind::FMin:
1277 return CmpInst::FCMP_OLT;
1278 case RecurKind::FMax:
1279 return CmpInst::FCMP_OGT;
1280 // We do not add FMinimum/FMaximum recurrence kind here since there is no
1281 // equivalent predicate which compares signed zeroes according to the
1282 // semantics of the intrinsics (llvm.minimum/maximum).
1283 }
1284}
1285
1287 Value *Right) {
1288 Type *Ty = Left->getType();
1289 if (Ty->isIntOrIntVectorTy() ||
1290 (RK == RecurKind::FMinNum || RK == RecurKind::FMaxNum ||
1294 return Builder.CreateIntrinsic(Ty, Id, {Left, Right}, nullptr,
1295 "rdx.minmax");
1296 }
1298 Value *Cmp = Builder.CreateCmp(Pred, Left, Right, "rdx.minmax.cmp");
1299 Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
1300 return Select;
1301}
1302
1303// Helper to generate an ordered reduction.
1305 unsigned Op, RecurKind RdxKind) {
1306 unsigned VF = cast<FixedVectorType>(Src->getType())->getNumElements();
1307
1308 // Extract and apply reduction ops in ascending order:
1309 // e.g. ((((Acc + Scl[0]) + Scl[1]) + Scl[2]) + ) ... + Scl[VF-1]
1310 Value *Result = Acc;
1311 for (unsigned ExtractIdx = 0; ExtractIdx != VF; ++ExtractIdx) {
1312 Value *Ext =
1313 Builder.CreateExtractElement(Src, Builder.getInt32(ExtractIdx));
1314
1315 if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
1316 Result = Builder.CreateBinOp((Instruction::BinaryOps)Op, Result, Ext,
1317 "bin.rdx");
1318 } else {
1320 "Invalid min/max");
1321 Result = createMinMaxOp(Builder, RdxKind, Result, Ext);
1322 }
1323 }
1324
1325 return Result;
1326}
1327
1329 unsigned RdxOpcode, Value *Acc,
1330 DominatorTree *DT, LoopInfo *LI) {
1331 auto *VTy = cast<VectorType>(Vec->getType());
1332 Type *EltTy = VTy->getElementType();
1333 Function *F = Builder.GetInsertBlock()->getParent();
1334
1335 const DataLayout &DL = F->getDataLayout();
1336 Type *IdxTy = DL.getIndexType(EltTy->getContext(), 0);
1337 unsigned MinElts = VTy->getElementCount().getKnownMinValue();
1338 Value *NumElts = Builder.CreateVScale(IdxTy);
1339 NumElts = Builder.CreateMul(NumElts, ConstantInt::get(IdxTy, MinElts));
1340
1341 BasicBlock *EntryBB = Builder.GetInsertBlock();
1342 BasicBlock *LoopBB = BasicBlock::Create(F->getContext(), "rdx.loop", F);
1343 BasicBlock *ExitBB = SplitBlock(EntryBB, Builder.GetInsertPoint(), DT, LI,
1344 nullptr, "rdx.exit");
1345
1346 EntryBB->getTerminator()->eraseFromParent();
1347 Builder.SetInsertPoint(EntryBB);
1348 Builder.CreateBr(LoopBB);
1349
1350 Builder.SetInsertPoint(LoopBB);
1351 PHINode *IV = Builder.CreatePHI(IdxTy, 2, "rdx.iv");
1352 PHINode *AccPhi = Builder.CreatePHI(EltTy, 2, "rdx.acc");
1353 IV->addIncoming(ConstantInt::get(IdxTy, 0), EntryBB);
1354 AccPhi->addIncoming(Acc, EntryBB);
1355
1356 Value *Elt = Builder.CreateExtractElement(Vec, IV);
1357 Value *Res = Builder.CreateBinOp((Instruction::BinaryOps)RdxOpcode, AccPhi,
1358 Elt, "rdx.op");
1359
1360 Value *NextIV =
1361 Builder.CreateNUWAdd(IV, ConstantInt::get(IdxTy, 1), "rdx.next");
1362 IV->addIncoming(NextIV, LoopBB);
1363 AccPhi->addIncoming(Res, LoopBB);
1364
1365 Value *Done = Builder.CreateICmpEQ(NextIV, NumElts, "rdx.done");
1366 Builder.CreateCondBr(Done, ExitBB, LoopBB);
1367
1368 // SplitBlock above updated DT/LI for EntryBB -> ExitBB. Now update
1369 // for replacing that edge with EntryBB -> LoopBB -> {ExitBB, LoopBB}.
1370 if (DT)
1371 DT->applyUpdates({{DominatorTree::Insert, EntryBB, LoopBB},
1372 {DominatorTree::Insert, LoopBB, LoopBB},
1373 {DominatorTree::Insert, LoopBB, ExitBB},
1374 {DominatorTree::Delete, EntryBB, ExitBB}});
1375
1376 if (LI) {
1377 Loop *NewLoop = LI->AllocateLoop();
1378 if (Loop *ParentLoop = LI->getLoopFor(EntryBB))
1379 ParentLoop->addChildLoop(NewLoop);
1380 else
1381 LI->addTopLevelLoop(NewLoop);
1382 NewLoop->addBasicBlockToLoop(LoopBB, *LI);
1383 }
1384
1385 Builder.SetInsertPoint(ExitBB, ExitBB->begin());
1386 return Res;
1387}
1388
1389// Helper to generate a log2 shuffle reduction.
1391 unsigned Op,
1393 RecurKind RdxKind) {
1394 unsigned VF = cast<FixedVectorType>(Src->getType())->getNumElements();
1395 // VF is a power of 2 so we can emit the reduction using log2(VF) shuffles
1396 // and vector ops, reducing the set of values being computed by half each
1397 // round.
1398 assert(isPowerOf2_32(VF) &&
1399 "Reduction emission only supported for pow2 vectors!");
1400 // Note: fast-math-flags flags are controlled by the builder configuration
1401 // and are assumed to apply to all generated arithmetic instructions. Other
1402 // poison generating flags (nsw/nuw/inbounds/inrange/exact) are not part
1403 // of the builder configuration, and since they're not passed explicitly,
1404 // will never be relevant here. Note that it would be generally unsound to
1405 // propagate these from an intrinsic call to the expansion anyways as we/
1406 // change the order of operations.
1407 auto BuildShuffledOp = [&Builder, &Op,
1408 &RdxKind](SmallVectorImpl<int> &ShuffleMask,
1409 Value *&TmpVec) -> void {
1410 Value *Shuf = Builder.CreateShuffleVector(TmpVec, ShuffleMask, "rdx.shuf");
1411 if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
1412 TmpVec = Builder.CreateBinOp((Instruction::BinaryOps)Op, TmpVec, Shuf,
1413 "bin.rdx");
1414 } else {
1416 "Invalid min/max");
1417 TmpVec = createMinMaxOp(Builder, RdxKind, TmpVec, Shuf);
1418 }
1419 };
1420
1421 Value *TmpVec = Src;
1423 SmallVector<int, 32> ShuffleMask(VF);
1424 for (unsigned stride = 1; stride < VF; stride <<= 1) {
1425 // Initialise the mask with undef.
1426 llvm::fill(ShuffleMask, -1);
1427 for (unsigned j = 0; j < VF; j += stride << 1) {
1428 ShuffleMask[j] = j + stride;
1429 }
1430 BuildShuffledOp(ShuffleMask, TmpVec);
1431 }
1432 } else {
1433 SmallVector<int, 32> ShuffleMask(VF);
1434 for (unsigned i = VF; i != 1; i >>= 1) {
1435 // Move the upper half of the vector to the lower half.
1436 for (unsigned j = 0; j != i / 2; ++j)
1437 ShuffleMask[j] = i / 2 + j;
1438
1439 // Fill the rest of the mask with undef.
1440 std::fill(&ShuffleMask[i / 2], ShuffleMask.end(), -1);
1441 BuildShuffledOp(ShuffleMask, TmpVec);
1442 }
1443 }
1444 // The result is in the first element of the vector.
1445 return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0));
1446}
1447
1449 Value *InitVal, PHINode *OrigPhi) {
1450 Value *NewVal = nullptr;
1451
1452 // First use the original phi to determine the new value we're trying to
1453 // select from in the loop.
1454 SelectInst *SI = nullptr;
1455 for (auto *U : OrigPhi->users()) {
1456 if ((SI = dyn_cast<SelectInst>(U)))
1457 break;
1458 }
1459 assert(SI && "One user of the original phi should be a select");
1460
1461 if (SI->getTrueValue() == OrigPhi)
1462 NewVal = SI->getFalseValue();
1463 else {
1464 assert(SI->getFalseValue() == OrigPhi &&
1465 "At least one input to the select should be the original Phi");
1466 NewVal = SI->getTrueValue();
1467 }
1468
1469 // If any predicate is true it means that we want to select the new value.
1470 Value *AnyOf =
1471 Src->getType()->isVectorTy() ? Builder.CreateOrReduce(Src) : Src;
1472 // The compares in the loop may yield poison, which propagates through the
1473 // bitwise ORs. Freeze it here before the condition is used.
1474 AnyOf = Builder.CreateFreeze(AnyOf);
1475 return Builder.CreateSelect(AnyOf, NewVal, InitVal, "rdx.select");
1476}
1477
1479 FastMathFlags Flags) {
1480 bool Negative = false;
1481 switch (RdxID) {
1482 default:
1483 llvm_unreachable("Expecting a reduction intrinsic");
1484 case Intrinsic::vector_reduce_add:
1485 case Intrinsic::vector_reduce_mul:
1486 case Intrinsic::vector_reduce_or:
1487 case Intrinsic::vector_reduce_xor:
1488 case Intrinsic::vector_reduce_and:
1489 case Intrinsic::vector_reduce_fadd:
1490 case Intrinsic::vector_reduce_fmul: {
1491 unsigned Opc = getArithmeticReductionInstruction(RdxID);
1492 return ConstantExpr::getBinOpIdentity(Opc, Ty, false,
1493 Flags.noSignedZeros());
1494 }
1495 case Intrinsic::vector_reduce_umax:
1496 case Intrinsic::vector_reduce_umin:
1497 case Intrinsic::vector_reduce_smin:
1498 case Intrinsic::vector_reduce_smax: {
1500 return ConstantExpr::getIntrinsicIdentity(ScalarID, Ty);
1501 }
1502 case Intrinsic::vector_reduce_fmax:
1503 case Intrinsic::vector_reduce_fmaximum:
1504 Negative = true;
1505 [[fallthrough]];
1506 case Intrinsic::vector_reduce_fmin:
1507 case Intrinsic::vector_reduce_fminimum: {
1508 bool PropagatesNaN = RdxID == Intrinsic::vector_reduce_fminimum ||
1509 RdxID == Intrinsic::vector_reduce_fmaximum;
1510 const fltSemantics &Semantics = Ty->getFltSemantics();
1511 return (!Flags.noNaNs() && !PropagatesNaN)
1512 ? ConstantFP::getQNaN(Ty, Negative)
1513 : !Flags.noInfs()
1514 ? ConstantFP::getInfinity(Ty, Negative)
1515 : ConstantFP::get(Ty, APFloat::getLargest(Semantics, Negative));
1516 }
1517 }
1518}
1519
1521 assert((!(K == RecurKind::FMin || K == RecurKind::FMax) ||
1522 (FMF.noNaNs() && FMF.noSignedZeros())) &&
1523 "nnan, nsz is expected to be set for FP min/max reduction.");
1525 return getReductionIdentity(RdxID, Tp, FMF);
1526}
1527
1529 RecurKind RdxKind) {
1530 auto *SrcVecEltTy = cast<VectorType>(Src->getType())->getElementType();
1531 auto getIdentity = [&]() {
1532 return getRecurrenceIdentity(RdxKind, SrcVecEltTy,
1533 Builder.getFastMathFlags());
1534 };
1535 switch (RdxKind) {
1537 case RecurKind::Sub:
1538 case RecurKind::Add:
1539 case RecurKind::Mul:
1540 case RecurKind::And:
1541 case RecurKind::Or:
1542 case RecurKind::Xor:
1543 case RecurKind::SMax:
1544 case RecurKind::SMin:
1545 case RecurKind::UMax:
1546 case RecurKind::UMin:
1547 case RecurKind::FMax:
1548 case RecurKind::FMin:
1549 case RecurKind::FMinNum:
1550 case RecurKind::FMaxNum:
1555 return Builder.CreateUnaryIntrinsic(getReductionIntrinsicID(RdxKind), Src);
1556 case RecurKind::FMulAdd:
1557 case RecurKind::FAdd:
1558 return Builder.CreateFAddReduce(getIdentity(), Src);
1559 case RecurKind::FMul:
1560 return Builder.CreateFMulReduce(getIdentity(), Src);
1561 default:
1562 llvm_unreachable("Unhandled opcode");
1563 }
1564}
1565
1567 RecurKind Kind, Value *Mask, Value *EVL) {
1570 "AnyOf and FindIV reductions are not supported.");
1572 auto VPID = VPIntrinsic::getForIntrinsic(Id);
1574 "No VPIntrinsic for this reduction");
1575 auto *EltTy = cast<VectorType>(Src->getType())->getElementType();
1576 Value *Iden = getRecurrenceIdentity(Kind, EltTy, Builder.getFastMathFlags());
1577 Value *Ops[] = {Iden, Src, Mask, EVL};
1578 return Builder.CreateIntrinsic(EltTy, VPID, Ops);
1579}
1580
1582 Value *Src, Value *Start) {
1583 assert((Kind == RecurKind::FAdd || Kind == RecurKind::FMulAdd) &&
1584 "Unexpected reduction kind");
1585 assert(Src->getType()->isVectorTy() && "Expected a vector type");
1586 assert(!Start->getType()->isVectorTy() && "Expected a scalar type");
1587
1588 return B.CreateFAddReduce(Start, Src);
1589}
1590
1592 Value *Src, Value *Start, Value *Mask,
1593 Value *EVL) {
1594 assert((Kind == RecurKind::FAdd || Kind == RecurKind::FMulAdd) &&
1595 "Unexpected reduction kind");
1596 assert(Src->getType()->isVectorTy() && "Expected a vector type");
1597 assert(!Start->getType()->isVectorTy() && "Expected a scalar type");
1598
1600 auto VPID = VPIntrinsic::getForIntrinsic(Id);
1602 "No VPIntrinsic for this reduction");
1603 auto *EltTy = cast<VectorType>(Src->getType())->getElementType();
1604 Value *Ops[] = {Start, Src, Mask, EVL};
1605 return Builder.CreateIntrinsic(EltTy, VPID, Ops);
1606}
1607
1609 bool IncludeWrapFlags) {
1610 auto *VecOp = dyn_cast<Instruction>(I);
1611 if (!VecOp)
1612 return;
1613 auto *Intersection = (OpValue == nullptr) ? dyn_cast<Instruction>(VL[0])
1614 : dyn_cast<Instruction>(OpValue);
1615 if (!Intersection)
1616 return;
1617 const unsigned Opcode = Intersection->getOpcode();
1618 VecOp->copyIRFlags(Intersection, IncludeWrapFlags);
1619 for (auto *V : VL) {
1620 auto *Instr = dyn_cast<Instruction>(V);
1621 if (!Instr)
1622 continue;
1623 if (OpValue == nullptr || Opcode == Instr->getOpcode())
1624 VecOp->andIRFlags(V);
1625 }
1626}
1627
1628bool llvm::isKnownNegativeInLoop(const SCEV *S, const Loop *L,
1629 ScalarEvolution &SE) {
1630 const SCEV *Zero = SE.getZero(S->getType());
1631 return SE.isAvailableAtLoopEntry(S, L) &&
1633}
1634
1636 ScalarEvolution &SE) {
1637 const SCEV *Zero = SE.getZero(S->getType());
1638 return SE.isAvailableAtLoopEntry(S, L) &&
1640}
1641
1642bool llvm::isKnownPositiveInLoop(const SCEV *S, const Loop *L,
1643 ScalarEvolution &SE) {
1644 const SCEV *Zero = SE.getZero(S->getType());
1645 return SE.isAvailableAtLoopEntry(S, L) &&
1647}
1648
1650 ScalarEvolution &SE) {
1651 const SCEV *Zero = SE.getZero(S->getType());
1652 return SE.isAvailableAtLoopEntry(S, L) &&
1654}
1655
1657 bool Signed) {
1658 unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth();
1661 auto Predicate = Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1662 return SE.isAvailableAtLoopEntry(S, L) &&
1663 SE.isLoopEntryGuardedByCond(L, Predicate, S,
1664 SE.getConstant(Min));
1665}
1666
1668 bool Signed) {
1669 unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth();
1672 auto Predicate = Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1673 return SE.isAvailableAtLoopEntry(S, L) &&
1674 SE.isLoopEntryGuardedByCond(L, Predicate, S,
1675 SE.getConstant(Max));
1676}
1677
1678//===----------------------------------------------------------------------===//
1679// rewriteLoopExitValues - Optimize IV users outside the loop.
1680// As a side effect, reduces the amount of IV processing within the loop.
1681//===----------------------------------------------------------------------===//
1682
1683static bool hasHardUserWithinLoop(const Loop *L, const Instruction *I) {
1686 Visited.insert(I);
1687 WorkList.push_back(I);
1688 while (!WorkList.empty()) {
1689 const Instruction *Curr = WorkList.pop_back_val();
1690 // This use is outside the loop, nothing to do.
1691 if (!L->contains(Curr))
1692 continue;
1693 // Do we assume it is a "hard" use which will not be eliminated easily?
1694 if (Curr->mayHaveSideEffects())
1695 return true;
1696 // Otherwise, add all its users to worklist.
1697 for (const auto *U : Curr->users()) {
1698 auto *UI = cast<Instruction>(U);
1699 if (Visited.insert(UI).second)
1700 WorkList.push_back(UI);
1701 }
1702 }
1703 return false;
1704}
1705
1706// Collect information about PHI nodes which can be transformed in
1707// rewriteLoopExitValues.
1709 PHINode *PN; // For which PHI node is this replacement?
1710 unsigned Ith; // For which incoming value?
1711 const SCEV *ExpansionSCEV; // The SCEV of the incoming value we are rewriting.
1712 Instruction *ExpansionPoint; // Where we'd like to expand that SCEV?
1713 bool HighCost; // Is this expansion a high-cost?
1714
1715 RewritePhi(PHINode *P, unsigned I, const SCEV *Val, Instruction *ExpansionPt,
1716 bool H)
1717 : PN(P), Ith(I), ExpansionSCEV(Val), ExpansionPoint(ExpansionPt),
1718 HighCost(H) {}
1719};
1720
1721// Check whether it is possible to delete the loop after rewriting exit
1722// value. If it is possible, ignore ReplaceExitValue and do rewriting
1723// aggressively.
1724static bool canLoopBeDeleted(Loop *L, SmallVector<RewritePhi, 8> &RewritePhiSet) {
1725 BasicBlock *Preheader = L->getLoopPreheader();
1726 // If there is no preheader, the loop will not be deleted.
1727 if (!Preheader)
1728 return false;
1729
1730 // In LoopDeletion pass Loop can be deleted when ExitingBlocks.size() > 1.
1731 // We obviate multiple ExitingBlocks case for simplicity.
1732 // TODO: If we see testcase with multiple ExitingBlocks can be deleted
1733 // after exit value rewriting, we can enhance the logic here.
1734 SmallVector<BasicBlock *, 4> ExitingBlocks;
1735 L->getExitingBlocks(ExitingBlocks);
1737 L->getUniqueExitBlocks(ExitBlocks);
1738 if (ExitBlocks.size() != 1 || ExitingBlocks.size() != 1)
1739 return false;
1740
1741 BasicBlock *ExitBlock = ExitBlocks[0];
1742 BasicBlock::iterator BI = ExitBlock->begin();
1743 while (PHINode *P = dyn_cast<PHINode>(BI)) {
1744 Value *Incoming = P->getIncomingValueForBlock(ExitingBlocks[0]);
1745
1746 // If the Incoming value of P is found in RewritePhiSet, we know it
1747 // could be rewritten to use a loop invariant value in transformation
1748 // phase later. Skip it in the loop invariant check below.
1749 bool found = false;
1750 for (const RewritePhi &Phi : RewritePhiSet) {
1751 unsigned i = Phi.Ith;
1752 if (Phi.PN == P && (Phi.PN)->getIncomingValue(i) == Incoming) {
1753 found = true;
1754 break;
1755 }
1756 }
1757
1758 Instruction *I;
1759 if (!found && (I = dyn_cast<Instruction>(Incoming)))
1760 if (!L->hasLoopInvariantOperands(I))
1761 return false;
1762
1763 ++BI;
1764 }
1765
1766 for (auto *BB : L->blocks())
1767 if (llvm::any_of(*BB, [](Instruction &I) {
1768 return I.mayHaveSideEffects();
1769 }))
1770 return false;
1771
1772 return true;
1773}
1774
1775/// Checks if it is safe to call InductionDescriptor::isInductionPHI for \p Phi,
1776/// and returns true if this Phi is an induction phi in the loop. When
1777/// isInductionPHI returns true, \p ID will be also be set by isInductionPHI.
1778static bool checkIsIndPhi(PHINode *Phi, Loop *L, ScalarEvolution *SE,
1780 if (!Phi)
1781 return false;
1782 if (!L->getLoopPreheader())
1783 return false;
1784 if (Phi->getParent() != L->getHeader())
1785 return false;
1786 return InductionDescriptor::isInductionPHI(Phi, L, SE, ID);
1787}
1788
1790 ScalarEvolution *SE,
1791 const TargetTransformInfo *TTI,
1792 SCEVExpander &Rewriter, DominatorTree *DT,
1795 // Check a pre-condition.
1796 assert(L->isRecursivelyLCSSAForm(*DT, *LI) &&
1797 "Caller did not preserve LCSSA!");
1798
1799 SmallVector<BasicBlock*, 8> ExitBlocks;
1800 L->getUniqueExitBlocks(ExitBlocks);
1801
1802 SmallVector<RewritePhi, 8> RewritePhiSet;
1803 // Find all values that are computed inside the loop, but used outside of it.
1804 // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan
1805 // the exit blocks of the loop to find them.
1806 for (BasicBlock *ExitBB : ExitBlocks) {
1807 // If there are no PHI nodes in this exit block, then no values defined
1808 // inside the loop are used on this path, skip it.
1809 PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
1810 if (!PN) continue;
1811
1812 unsigned NumPreds = PN->getNumIncomingValues();
1813
1814 // Iterate over all of the PHI nodes.
1815 BasicBlock::iterator BBI = ExitBB->begin();
1816 while ((PN = dyn_cast<PHINode>(BBI++))) {
1817 if (PN->use_empty())
1818 continue; // dead use, don't replace it
1819
1820 if (!SE->isSCEVable(PN->getType()))
1821 continue;
1822
1823 // Iterate over all of the values in all the PHI nodes.
1824 for (unsigned i = 0; i != NumPreds; ++i) {
1825 // If the value being merged in is not integer or is not defined
1826 // in the loop, skip it.
1827 Value *InVal = PN->getIncomingValue(i);
1828 if (!isa<Instruction>(InVal))
1829 continue;
1830
1831 // If this pred is for a subloop, not L itself, skip it.
1832 if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
1833 continue; // The Block is in a subloop, skip it.
1834
1835 // Check that InVal is defined in the loop.
1836 Instruction *Inst = cast<Instruction>(InVal);
1837 if (!L->contains(Inst))
1838 continue;
1839
1840 // Find exit values which are induction variables in the loop, and are
1841 // unused in the loop, with the only use being the exit block PhiNode,
1842 // and the induction variable update binary operator.
1843 // The exit value can be replaced with the final value when it is cheap
1844 // to do so.
1847 PHINode *IndPhi = dyn_cast<PHINode>(Inst);
1848 if (IndPhi) {
1849 if (!checkIsIndPhi(IndPhi, L, SE, ID))
1850 continue;
1851 // This is an induction PHI. Check that the only users are PHI
1852 // nodes, and induction variable update binary operators.
1853 if (llvm::any_of(Inst->users(), [&](User *U) {
1854 if (!isa<PHINode>(U) && !isa<BinaryOperator>(U))
1855 return true;
1856 BinaryOperator *B = dyn_cast<BinaryOperator>(U);
1857 if (B && B != ID.getInductionBinOp())
1858 return true;
1859 return false;
1860 }))
1861 continue;
1862 } else {
1863 // If it is not an induction phi, it must be an induction update
1864 // binary operator with an induction phi user.
1866 if (!B)
1867 continue;
1868 if (llvm::any_of(Inst->users(), [&](User *U) {
1869 PHINode *Phi = dyn_cast<PHINode>(U);
1870 if (Phi != PN && !checkIsIndPhi(Phi, L, SE, ID))
1871 return true;
1872 return false;
1873 }))
1874 continue;
1875 if (B != ID.getInductionBinOp())
1876 continue;
1877 }
1878 }
1879
1880 // Okay, this instruction has a user outside of the current loop
1881 // and varies predictably *inside* the loop. Evaluate the value it
1882 // contains when the loop exits, if possible. We prefer to start with
1883 // expressions which are true for all exits (so as to maximize
1884 // expression reuse by the SCEVExpander), but resort to per-exit
1885 // evaluation if that fails.
1886 const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
1887 if (isa<SCEVCouldNotCompute>(ExitValue) ||
1888 !SE->isLoopInvariant(ExitValue, L) ||
1889 !Rewriter.isSafeToExpand(ExitValue)) {
1890 // TODO: This should probably be sunk into SCEV in some way; maybe a
1891 // getSCEVForExit(SCEV*, L, ExitingBB)? It can be generalized for
1892 // most SCEV expressions and other recurrence types (e.g. shift
1893 // recurrences). Is there existing code we can reuse?
1894 const SCEV *ExitCount = SE->getExitCount(L, PN->getIncomingBlock(i));
1895 if (isa<SCEVCouldNotCompute>(ExitCount))
1896 continue;
1897 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Inst)))
1898 if (AddRec->getLoop() == L)
1899 ExitValue = AddRec->evaluateAtIteration(ExitCount, *SE);
1900 if (isa<SCEVCouldNotCompute>(ExitValue) ||
1901 !SE->isLoopInvariant(ExitValue, L) ||
1902 !Rewriter.isSafeToExpand(ExitValue))
1903 continue;
1904 }
1905
1906 // Computing the value outside of the loop brings no benefit if it is
1907 // definitely used inside the loop in a way which can not be optimized
1908 // away. Avoid doing so unless we know we have a value which computes
1909 // the ExitValue already. TODO: This should be merged into SCEV
1910 // expander to leverage its knowledge of existing expressions.
1911 if (ReplaceExitValue != AlwaysRepl && !isa<SCEVConstant>(ExitValue) &&
1912 !isa<SCEVUnknown>(ExitValue) && hasHardUserWithinLoop(L, Inst))
1913 continue;
1914
1915 // Check if expansions of this SCEV would count as being high cost.
1916 bool HighCost = Rewriter.isHighCostExpansion(
1917 ExitValue, L, SCEVCheapExpansionBudget, TTI, Inst);
1918
1919 // Note that we must not perform expansions until after
1920 // we query *all* the costs, because if we perform temporary expansion
1921 // inbetween, one that we might not intend to keep, said expansion
1922 // *may* affect cost calculation of the next SCEV's we'll query,
1923 // and next SCEV may errneously get smaller cost.
1924
1925 // Collect all the candidate PHINodes to be rewritten.
1926 Instruction *InsertPt =
1927 (isa<PHINode>(Inst) || isa<LandingPadInst>(Inst)) ?
1928 &*Inst->getParent()->getFirstInsertionPt() : Inst;
1929 RewritePhiSet.emplace_back(PN, i, ExitValue, InsertPt, HighCost);
1930 }
1931 }
1932 }
1933
1934 // TODO: evaluate whether it is beneficial to change how we calculate
1935 // high-cost: if we have SCEV 'A' which we know we will expand, should we
1936 // calculate the cost of other SCEV's after expanding SCEV 'A', thus
1937 // potentially giving cost bonus to those other SCEV's?
1938
1939 bool LoopCanBeDel = canLoopBeDeleted(L, RewritePhiSet);
1940 int NumReplaced = 0;
1941
1942 // Transformation.
1943 for (const RewritePhi &Phi : RewritePhiSet) {
1944 PHINode *PN = Phi.PN;
1945
1946 // Only do the rewrite when the ExitValue can be expanded cheaply.
1947 // If LoopCanBeDel is true, rewrite exit value aggressively.
1950 !LoopCanBeDel && Phi.HighCost)
1951 continue;
1952
1953 Value *ExitVal = Rewriter.expandCodeFor(
1954 Phi.ExpansionSCEV, Phi.PN->getType(), Phi.ExpansionPoint);
1955
1956 LLVM_DEBUG(dbgs() << "rewriteLoopExitValues: AfterLoopVal = " << *ExitVal
1957 << '\n'
1958 << " LoopVal = " << *(Phi.ExpansionPoint) << "\n");
1959
1960#ifndef NDEBUG
1961 // If we reuse an instruction from a loop which is neither L nor one of
1962 // its containing loops, we end up breaking LCSSA form for this loop by
1963 // creating a new use of its instruction.
1964 if (auto *ExitInsn = dyn_cast<Instruction>(ExitVal))
1965 if (auto *EVL = LI->getLoopFor(ExitInsn->getParent()))
1966 if (EVL != L)
1967 assert(EVL->contains(L) && "LCSSA breach detected!");
1968#endif
1969
1970 NumReplaced++;
1971 Instruction *Inst = cast<Instruction>(PN->getIncomingValue(Phi.Ith));
1972 PN->setIncomingValue(Phi.Ith, ExitVal);
1973 // It's necessary to tell ScalarEvolution about this explicitly so that
1974 // it can walk the def-use list and forget all SCEVs, as it may not be
1975 // watching the PHI itself. Once the new exit value is in place, there
1976 // may not be a def-use connection between the loop and every instruction
1977 // which got a SCEVAddRecExpr for that loop.
1978 SE->forgetValue(PN);
1979
1980 // If this instruction is dead now, delete it. Don't do it now to avoid
1981 // invalidating iterators.
1982 if (isInstructionTriviallyDead(Inst, TLI))
1983 DeadInsts.push_back(Inst);
1984
1985 // Replace PN with ExitVal if that is legal and does not break LCSSA.
1986 if (PN->getNumIncomingValues() == 1 &&
1987 LI->replacementPreservesLCSSAForm(PN, ExitVal)) {
1988 PN->replaceAllUsesWith(ExitVal);
1989 PN->eraseFromParent();
1990 }
1991 }
1992
1993 // The insertion point instruction may have been deleted; clear it out
1994 // so that the rewriter doesn't trip over it later.
1995 Rewriter.clearInsertPoint();
1996 return NumReplaced;
1997}
1998
1999/// Utility that implements appending of loops onto a worklist.
2000/// Loops are added in preorder (analogous for reverse postorder for trees),
2001/// and the worklist is processed LIFO.
2002template <typename RangeT>
2004 RangeT &&Loops, SmallPriorityWorklist<Loop *, 4> &Worklist) {
2005 // We use an internal worklist to build up the preorder traversal without
2006 // recursion.
2007 SmallVector<Loop *, 4> PreOrderLoops, PreOrderWorklist;
2008
2009 // We walk the initial sequence of loops in reverse because we generally want
2010 // to visit defs before uses and the worklist is LIFO.
2011 for (Loop *RootL : Loops) {
2012 assert(PreOrderLoops.empty() && "Must start with an empty preorder walk.");
2013 assert(PreOrderWorklist.empty() &&
2014 "Must start with an empty preorder walk worklist.");
2015 PreOrderWorklist.push_back(RootL);
2016 do {
2017 Loop *L = PreOrderWorklist.pop_back_val();
2018 PreOrderWorklist.append(L->begin(), L->end());
2019 PreOrderLoops.push_back(L);
2020 } while (!PreOrderWorklist.empty());
2021
2022 Worklist.insert(std::move(PreOrderLoops));
2023 PreOrderLoops.clear();
2024 }
2025}
2026
2027template <typename RangeT>
2031}
2032
2033template LLVM_EXPORT_TEMPLATE void
2036
2037template LLVM_EXPORT_TEMPLATE void
2040
2045
2047 LoopInfo *LI, LPPassManager *LPM) {
2048 Loop &New = *LI->AllocateLoop();
2049 if (PL)
2050 PL->addChildLoop(&New);
2051 else
2052 LI->addTopLevelLoop(&New);
2053
2054 if (LPM)
2055 LPM->addLoop(New);
2056
2057 // Add all of the blocks in L to the new loop.
2058 for (BasicBlock *BB : L->blocks())
2059 if (LI->getLoopFor(BB) == L)
2060 New.addBasicBlockToLoop(cast<BasicBlock>(VM[BB]), *LI);
2061
2062 // Add all of the subloops to the new loop.
2063 for (Loop *I : *L)
2064 cloneLoop(I, &New, VM, LI, LPM);
2065
2066 return &New;
2067}
2068
2069/// IR Values for the lower and upper bounds of a pointer evolution. We
2070/// need to use value-handles because SCEV expansion can invalidate previously
2071/// expanded values. Thus expansion of a pointer can invalidate the bounds for
2072/// a previous one.
2078
2079/// Expand code for the lower and upper bound of the pointer group \p CG
2080/// in \p TheLoop. \return the values for the bounds.
2082 Loop *TheLoop, Instruction *Loc,
2083 SCEVExpander &Exp, bool HoistRuntimeChecks) {
2084 LLVMContext &Ctx = Loc->getContext();
2085 Type *PtrArithTy = PointerType::get(Ctx, CG->AddressSpace);
2086
2087 Value *Start = nullptr, *End = nullptr;
2088 LLVM_DEBUG(dbgs() << "LAA: Adding RT check for range:\n");
2089 const SCEV *Low = CG->Low, *High = CG->High, *Stride = nullptr;
2090
2091 // If the Low and High values are themselves loop-variant, then we may want
2092 // to expand the range to include those covered by the outer loop as well.
2093 // There is a trade-off here with the advantage being that creating checks
2094 // using the expanded range permits the runtime memory checks to be hoisted
2095 // out of the outer loop. This reduces the cost of entering the inner loop,
2096 // which can be significant for low trip counts. The disadvantage is that
2097 // there is a chance we may now never enter the vectorized inner loop,
2098 // whereas using a restricted range check could have allowed us to enter at
2099 // least once. This is why the behaviour is not currently the default and is
2100 // controlled by the parameter 'HoistRuntimeChecks'.
2101 if (HoistRuntimeChecks && TheLoop->getParentLoop() &&
2103 auto *HighAR = cast<SCEVAddRecExpr>(High);
2104 auto *LowAR = cast<SCEVAddRecExpr>(Low);
2105 const Loop *OuterLoop = TheLoop->getParentLoop();
2106 ScalarEvolution &SE = *Exp.getSE();
2107 const SCEV *Recur = LowAR->getStepRecurrence(SE);
2108 if (Recur == HighAR->getStepRecurrence(SE) &&
2109 HighAR->getLoop() == OuterLoop && LowAR->getLoop() == OuterLoop) {
2110 BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
2111 const SCEV *OuterExitCount = SE.getExitCount(OuterLoop, OuterLoopLatch);
2112 if (!isa<SCEVCouldNotCompute>(OuterExitCount) &&
2113 OuterExitCount->getType()->isIntegerTy()) {
2114 const SCEV *NewHigh =
2115 cast<SCEVAddRecExpr>(High)->evaluateAtIteration(OuterExitCount, SE);
2116 if (!isa<SCEVCouldNotCompute>(NewHigh)) {
2117 LLVM_DEBUG(dbgs() << "LAA: Expanded RT check for range to include "
2118 "outer loop in order to permit hoisting\n");
2119 High = NewHigh;
2120 Low = cast<SCEVAddRecExpr>(Low)->getStart();
2121 // If there is a possibility that the stride is negative then we have
2122 // to generate extra checks to ensure the stride is positive.
2123 if (!SE.isKnownNonNegative(
2124 SE.applyLoopGuards(Recur, HighAR->getLoop()))) {
2125 Stride = Recur;
2126 LLVM_DEBUG(dbgs() << "LAA: ... but need to check stride is "
2127 "positive: "
2128 << *Stride << '\n');
2129 }
2130 }
2131 }
2132 }
2133 }
2134
2135 Start = Exp.expandCodeFor(Low, PtrArithTy, Loc);
2136 End = Exp.expandCodeFor(High, PtrArithTy, Loc);
2137 if (CG->NeedsFreeze) {
2138 IRBuilder<> Builder(Loc);
2139 Start = Builder.CreateFreeze(Start, Start->getName() + ".fr");
2140 End = Builder.CreateFreeze(End, End->getName() + ".fr");
2141 }
2142 Value *StrideVal =
2143 Stride ? Exp.expandCodeFor(Stride, Stride->getType(), Loc) : nullptr;
2144 LLVM_DEBUG(dbgs() << "Start: " << *Low << " End: " << *High << "\n");
2145 return {Start, End, StrideVal};
2146}
2147
2148/// Turns a collection of checks into a collection of expanded upper and
2149/// lower bounds for both pointers in the check.
2154
2155 // Here we're relying on the SCEV Expander's cache to only emit code for the
2156 // same bounds once.
2157 transform(PointerChecks, std::back_inserter(ChecksWithBounds),
2158 [&](const RuntimePointerCheck &Check) {
2159 PointerBounds First = expandBounds(Check.first, L, Loc, Exp,
2161 Second = expandBounds(Check.second, L, Loc, Exp,
2163 return std::make_pair(First, Second);
2164 });
2165
2166 return ChecksWithBounds;
2167}
2168
2170 Instruction *Loc, Loop *TheLoop,
2171 const SmallVectorImpl<RuntimePointerCheck> &PointerChecks,
2172 SCEVExpander &Exp, bool HoistRuntimeChecks) {
2173 // TODO: Move noalias annotation code from LoopVersioning here and share with LV if possible.
2174 // TODO: Pass RtPtrChecking instead of PointerChecks and SE separately, if possible
2175 auto ExpandedChecks =
2176 expandBounds(PointerChecks, TheLoop, Loc, Exp, HoistRuntimeChecks);
2177
2178 LLVMContext &Ctx = Loc->getContext();
2179 IRBuilder ChkBuilder(Ctx, InstSimplifyFolder(Loc->getDataLayout()));
2180 ChkBuilder.SetInsertPoint(Loc);
2181 // Our instructions might fold to a constant.
2182 Value *MemoryRuntimeCheck = nullptr;
2183
2184 for (const auto &[A, B] : ExpandedChecks) {
2185 // Check if two pointers (A and B) conflict where conflict is computed as:
2186 // start(A) <= end(B) && start(B) <= end(A)
2187
2188 assert((A.Start->getType()->getPointerAddressSpace() ==
2189 B.End->getType()->getPointerAddressSpace()) &&
2190 (B.Start->getType()->getPointerAddressSpace() ==
2191 A.End->getType()->getPointerAddressSpace()) &&
2192 "Trying to bounds check pointers with different address spaces");
2193
2194 // [A|B].Start points to the first accessed byte under base [A|B].
2195 // [A|B].End points to the last accessed byte, plus one.
2196 // There is no conflict when the intervals are disjoint:
2197 // NoConflict = (B.Start >= A.End) || (A.Start >= B.End)
2198 //
2199 // bound0 = (B.Start < A.End)
2200 // bound1 = (A.Start < B.End)
2201 // IsConflict = bound0 & bound1
2202 Value *Cmp0 = ChkBuilder.CreateICmpULT(A.Start, B.End, "bound0");
2203 Value *Cmp1 = ChkBuilder.CreateICmpULT(B.Start, A.End, "bound1");
2204 Value *IsConflict = ChkBuilder.CreateAnd(Cmp0, Cmp1, "found.conflict");
2205 if (A.StrideToCheck) {
2206 Value *IsNegativeStride = ChkBuilder.CreateICmpSLT(
2207 A.StrideToCheck, ConstantInt::get(A.StrideToCheck->getType(), 0),
2208 "stride.check");
2209 IsConflict = ChkBuilder.CreateOr(IsConflict, IsNegativeStride);
2210 }
2211 if (B.StrideToCheck) {
2212 Value *IsNegativeStride = ChkBuilder.CreateICmpSLT(
2213 B.StrideToCheck, ConstantInt::get(B.StrideToCheck->getType(), 0),
2214 "stride.check");
2215 IsConflict = ChkBuilder.CreateOr(IsConflict, IsNegativeStride);
2216 }
2217 if (MemoryRuntimeCheck) {
2218 IsConflict =
2219 ChkBuilder.CreateOr(MemoryRuntimeCheck, IsConflict, "conflict.rdx");
2220 }
2221 MemoryRuntimeCheck = IsConflict;
2222 }
2223
2224 Exp.eraseDeadInstructions(MemoryRuntimeCheck);
2225 return MemoryRuntimeCheck;
2226}
2227
2228namespace {
2229/// Rewriter to replace SCEVPtrToIntExpr with SCEVPtrToAddrExpr when the result
2230/// type matches the pointer address type. This allows expressions mixing
2231/// ptrtoint and ptrtoaddr to simplify properly.
2232struct SCEVPtrToAddrRewriter : SCEVRewriteVisitor<SCEVPtrToAddrRewriter> {
2233 const DataLayout &DL;
2234 SCEVPtrToAddrRewriter(ScalarEvolution &SE, const DataLayout &DL)
2235 : SCEVRewriteVisitor(SE), DL(DL) {}
2236
2237 const SCEV *visitPtrToIntExpr(const SCEVPtrToIntExpr *E) {
2238 const SCEV *Op = visit(E->getOperand());
2239 if (E->getType() == DL.getAddressType(E->getOperand()->getType()))
2240 return SE.getPtrToAddrExpr(Op);
2241 return Op == E->getOperand() ? E : SE.getPtrToIntExpr(Op, E->getType());
2242 }
2243};
2244} // namespace
2245
2248 function_ref<Value *(IRBuilderBase &, unsigned)> GetVF, unsigned IC) {
2249
2250 LLVMContext &Ctx = Loc->getContext();
2251 IRBuilder ChkBuilder(Ctx, InstSimplifyFolder(Loc->getDataLayout()));
2252 ChkBuilder.SetInsertPoint(Loc);
2253 // Our instructions might fold to a constant.
2254 Value *MemoryRuntimeCheck = nullptr;
2255
2256 auto &SE = *Expander.getSE();
2257 const DataLayout &DL = Loc->getDataLayout();
2258 SCEVPtrToAddrRewriter Rewriter(SE, DL);
2259 // Map to keep track of created compares, The key is the pair of operands for
2260 // the compare, to allow detecting and re-using redundant compares.
2262 for (const auto &[SrcStart, SinkStart, AccessSize, NeedsFreeze] : Checks) {
2263 Type *Ty = SinkStart->getType();
2264 // Compute VF * IC * AccessSize.
2265 auto *VFTimesICTimesSize =
2266 ChkBuilder.CreateMul(GetVF(ChkBuilder, Ty->getScalarSizeInBits()),
2267 ConstantInt::get(Ty, IC * AccessSize));
2268 const SCEV *SinkStartRewritten = Rewriter.visit(SinkStart);
2269 const SCEV *SrcStartRewritten = Rewriter.visit(SrcStart);
2270 Value *Diff = Expander.expandCodeFor(
2271 SE.getMinusSCEV(SinkStartRewritten, SrcStartRewritten), Ty, Loc);
2272
2273 // Check if the same compare has already been created earlier. In that case,
2274 // there is no need to check it again.
2275 Value *IsConflict = SeenCompares.lookup({Diff, VFTimesICTimesSize});
2276 if (IsConflict)
2277 continue;
2278
2279 IsConflict =
2280 ChkBuilder.CreateICmpULT(Diff, VFTimesICTimesSize, "diff.check");
2281 SeenCompares.insert({{Diff, VFTimesICTimesSize}, IsConflict});
2282 if (NeedsFreeze)
2283 IsConflict =
2284 ChkBuilder.CreateFreeze(IsConflict, IsConflict->getName() + ".fr");
2285 if (MemoryRuntimeCheck) {
2286 IsConflict =
2287 ChkBuilder.CreateOr(MemoryRuntimeCheck, IsConflict, "conflict.rdx");
2288 }
2289 MemoryRuntimeCheck = IsConflict;
2290 }
2291
2292 Expander.eraseDeadInstructions(MemoryRuntimeCheck);
2293 return MemoryRuntimeCheck;
2294}
2295
2296std::optional<IVConditionInfo>
2298 const MemorySSA &MSSA, AAResults &AA) {
2299 auto *TI = dyn_cast<CondBrInst>(L.getHeader()->getTerminator());
2300 if (!TI)
2301 return {};
2302
2303 auto *CondI = dyn_cast<Instruction>(TI->getCondition());
2304 // The case with the condition outside the loop should already be handled
2305 // earlier.
2306 // Allow CmpInst and TruncInsts as they may be users of load instructions
2307 // and have potential for partial unswitching
2308 if (!CondI || !isa<CmpInst, TruncInst>(CondI) || !L.contains(CondI))
2309 return {};
2310
2311 SmallVector<Instruction *> InstToDuplicate;
2312 InstToDuplicate.push_back(CondI);
2313
2314 SmallVector<Value *, 4> WorkList;
2315 WorkList.append(CondI->op_begin(), CondI->op_end());
2316
2317 SmallVector<MemoryAccess *, 4> AccessesToCheck;
2318 SmallVector<MemoryLocation, 4> AccessedLocs;
2319 while (!WorkList.empty()) {
2321 if (!I || !L.contains(I))
2322 continue;
2323
2324 // TODO: support additional instructions.
2326 return {};
2327
2328 // Do not duplicate volatile and atomic loads.
2329 if (auto *LI = dyn_cast<LoadInst>(I))
2330 if (LI->isVolatile() || LI->isAtomic())
2331 return {};
2332
2333 InstToDuplicate.push_back(I);
2334 if (MemoryAccess *MA = MSSA.getMemoryAccess(I)) {
2335 if (auto *MemUse = dyn_cast_or_null<MemoryUse>(MA)) {
2336 // Queue the defining access to check for alias checks.
2337 AccessesToCheck.push_back(MemUse->getDefiningAccess());
2338 AccessedLocs.push_back(MemoryLocation::get(I));
2339 } else {
2340 // MemoryDefs may clobber the location or may be atomic memory
2341 // operations. Bail out.
2342 return {};
2343 }
2344 }
2345 WorkList.append(I->op_begin(), I->op_end());
2346 }
2347
2348 if (InstToDuplicate.empty())
2349 return {};
2350
2351 SmallVector<BasicBlock *, 4> ExitingBlocks;
2352 L.getExitingBlocks(ExitingBlocks);
2353 auto HasNoClobbersOnPath =
2354 [&L, &AA, &AccessedLocs, &ExitingBlocks, &InstToDuplicate,
2355 MSSAThreshold](BasicBlock *Succ, BasicBlock *Header,
2356 SmallVector<MemoryAccess *, 4> AccessesToCheck)
2357 -> std::optional<IVConditionInfo> {
2358 IVConditionInfo Info;
2359 // First, collect all blocks in the loop that are on a patch from Succ
2360 // to the header.
2362 WorkList.push_back(Succ);
2363 WorkList.push_back(Header);
2365 Seen.insert(Header);
2366 Info.PathIsNoop &=
2367 all_of(*Header, [](Instruction &I) { return !I.mayHaveSideEffects(); });
2368
2369 while (!WorkList.empty()) {
2370 BasicBlock *Current = WorkList.pop_back_val();
2371 if (!L.contains(Current))
2372 continue;
2373 const auto &SeenIns = Seen.insert(Current);
2374 if (!SeenIns.second)
2375 continue;
2376
2377 Info.PathIsNoop &= all_of(
2378 *Current, [](Instruction &I) { return !I.mayHaveSideEffects(); });
2379 WorkList.append(succ_begin(Current), succ_end(Current));
2380 }
2381
2382 // Require at least 2 blocks on a path through the loop. This skips
2383 // paths that directly exit the loop.
2384 if (Seen.size() < 2)
2385 return {};
2386
2387 // Next, check if there are any MemoryDefs that are on the path through
2388 // the loop (in the Seen set) and they may-alias any of the locations in
2389 // AccessedLocs. If that is the case, they may modify the condition and
2390 // partial unswitching is not possible.
2391 SmallPtrSet<MemoryAccess *, 4> SeenAccesses;
2392 while (!AccessesToCheck.empty()) {
2393 MemoryAccess *Current = AccessesToCheck.pop_back_val();
2394 auto SeenI = SeenAccesses.insert(Current);
2395 if (!SeenI.second || !Seen.contains(Current->getBlock()))
2396 continue;
2397
2398 // Bail out if exceeded the threshold.
2399 if (SeenAccesses.size() >= MSSAThreshold)
2400 return {};
2401
2402 // MemoryUse are read-only accesses.
2403 if (isa<MemoryUse>(Current))
2404 continue;
2405
2406 // For a MemoryDef, check if is aliases any of the location feeding
2407 // the original condition.
2408 if (auto *CurrentDef = dyn_cast<MemoryDef>(Current)) {
2409 if (any_of(AccessedLocs, [&AA, CurrentDef](MemoryLocation &Loc) {
2410 return isModSet(
2411 AA.getModRefInfo(CurrentDef->getMemoryInst(), Loc));
2412 }))
2413 return {};
2414 }
2415
2416 for (Use &U : Current->uses())
2417 AccessesToCheck.push_back(cast<MemoryAccess>(U.getUser()));
2418 }
2419
2420 // We could also allow loops with known trip counts without mustprogress,
2421 // but ScalarEvolution may not be available.
2422 Info.PathIsNoop &= isMustProgress(&L);
2423
2424 // If the path is considered a no-op so far, check if it reaches a
2425 // single exit block without any phis. This ensures no values from the
2426 // loop are used outside of the loop.
2427 if (Info.PathIsNoop) {
2428 for (auto *Exiting : ExitingBlocks) {
2429 if (!Seen.contains(Exiting))
2430 continue;
2431 for (auto *Succ : successors(Exiting)) {
2432 if (L.contains(Succ))
2433 continue;
2434
2435 Info.PathIsNoop &= Succ->phis().empty() &&
2436 (!Info.ExitForPath || Info.ExitForPath == Succ);
2437 if (!Info.PathIsNoop)
2438 break;
2439 assert((!Info.ExitForPath || Info.ExitForPath == Succ) &&
2440 "cannot have multiple exit blocks");
2441 Info.ExitForPath = Succ;
2442 }
2443 }
2444 }
2445 if (!Info.ExitForPath)
2446 Info.PathIsNoop = false;
2447
2448 Info.InstToDuplicate = std::move(InstToDuplicate);
2449 return Info;
2450 };
2451
2452 // If we branch to the same successor, partial unswitching will not be
2453 // beneficial.
2454 if (TI->getSuccessor(0) == TI->getSuccessor(1))
2455 return {};
2456
2457 if (auto Info = HasNoClobbersOnPath(TI->getSuccessor(0), L.getHeader(),
2458 AccessesToCheck)) {
2459 Info->KnownValue = ConstantInt::getTrue(TI->getContext());
2460 return Info;
2461 }
2462 if (auto Info = HasNoClobbersOnPath(TI->getSuccessor(1), L.getHeader(),
2463 AccessesToCheck)) {
2464 Info->KnownValue = ConstantInt::getFalse(TI->getContext());
2465 return Info;
2466 }
2467
2468 return {};
2469}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This is the interface for LLVM's primary stateless and local alias analysis.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_EXPORT_TEMPLATE
Definition Compiler.h:215
This file defines the DenseSet and SmallDenseSet classes.
#define Check(C,...)
This is the interface for a simple mod/ref and alias analysis over globals.
ManagedStatic< HTTPClientCleanup > Cleanup
Hexagon Hardware Loops
Module.h This file contains the declarations for the Module class.
iv Induction Variable Users
Definition IVUsers.cpp:48
static cl::opt< ReplaceExitVal > ReplaceExitValue("replexitval", cl::Hidden, cl::init(OnlyCheapRepl), cl::desc("Choose the strategy to replace exit value in IndVarSimplify"), cl::values(clEnumValN(NeverRepl, "never", "never replace exit value"), clEnumValN(OnlyCheapRepl, "cheap", "only replace exit value when the cost is cheap"), clEnumValN(UnusedIndVarInLoop, "unusedindvarinloop", "only replace exit value when it is an unused " "induction variable in the loop and has cheap replacement cost"), clEnumValN(NoHardUse, "noharduse", "only replace exit values when loop def likely dead"), clEnumValN(AlwaysRepl, "always", "always replace exit value whenever possible")))
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static cl::opt< bool, true > HoistRuntimeChecks("hoist-runtime-checks", cl::Hidden, cl::desc("Hoist inner loop runtime memory checks to outer loop if possible"), cl::location(VectorizerParams::HoistRuntimeChecks), cl::init(true))
static bool hasHardUserWithinLoop(const Loop *L, const Instruction *I)
static CondBrInst * getExpectedExitLoopLatchBranch(Loop *L)
Checks if L has an exiting latch branch.
static const char * LLVMLoopDisableLICM
Definition LoopUtils.cpp:56
static PointerBounds expandBounds(const RuntimeCheckingPtrGroup *CG, Loop *TheLoop, Instruction *Loc, SCEVExpander &Exp, bool HoistRuntimeChecks)
Expand code for the lower and upper bound of the pointer group CG in TheLoop.
static bool canLoopBeDeleted(Loop *L, SmallVector< RewritePhi, 8 > &RewritePhiSet)
static const char * LLVMLoopDisableNonforced
Definition LoopUtils.cpp:55
static MDNode * createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V)
Create MDNode for input string.
static std::optional< unsigned > estimateLoopTripCount(Loop *L)
static bool checkIsIndPhi(PHINode *Phi, Loop *L, ScalarEvolution *SE, InductionDescriptor &ID)
Checks if it is safe to call InductionDescriptor::isInductionPHI for Phi, and returns true if this Ph...
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define H(x, y, z)
Definition MD5.cpp:56
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
uint64_t High
#define P(N)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
This file provides a priority worklist.
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
static void visit(BasicBlock &Start, std::function< bool(BasicBlock *)> op)
This is the interface for a SCEV-based alias analysis.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
Virtual Register Rewriter
static const uint32_t IV[8]
Definition blake3_impl.h:83
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1193
Class for arbitrary precision integers.
Definition APInt.h:78
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:217
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
Represent the analysis usage information of a pass.
LLVM_ABI AnalysisUsage & addRequiredID(const void *ID)
Definition Pass.cpp:284
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Legacy wrapper pass to provide the BasicAAResult object.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:461
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
static LLVM_ABI BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
static BranchProbability getUnknown()
uint32_t getNumerator() const
static BranchProbability getZero()
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
Conditional Branch instruction.
BasicBlock * getSuccessor(unsigned i) const
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:537
static LLVM_ABI Constant * getIntrinsicIdentity(Intrinsic::ID, Type *Ty)
static LLVM_ABI Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
static LLVM_ABI Constant * getInfinity(Type *Ty, bool Negative=false)
static LLVM_ABI Constant * getQNaN(Type *Ty, bool Negative=false, APInt *Payload=nullptr)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition Constants.h:174
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Record of a variable value-assignment, aka a non instruction representation of the dbg....
Identifies a unique instance of a variable.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:205
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
iterator_range< iterator > children()
void applyUpdates(ArrayRef< UpdateType > Updates)
Inform the dominator tree about a sequence of CFG edge insertions and deletions and perform a batch u...
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:316
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
bool noSignedZeros() const
Definition FMF.h:70
bool noNaNs() const
Definition FMF.h:68
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
Legacy wrapper pass to provide the GlobalsAAResult object.
Common base class shared among various IRBuilders.
Definition IRBuilder.h:114
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2371
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition IRBuilder.h:2674
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:1591
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2387
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition IRBuilder.h:207
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
Definition IRBuilder.h:1613
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1477
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2835
A struct for saving information about induction variables.
static LLVM_ABI bool isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D, const SCEV *Expr=nullptr, SmallVectorImpl< Instruction * > *CastsToIgnore=nullptr)
Returns true if Phi is an induction in the loop L.
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
LLVM_ABI bool mayHaveSideEffects() const LLVM_READONLY
Return true if the instruction may have side effects.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
void addLoop(Loop &L)
Definition LoopPass.cpp:77
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
typename std::vector< Loop * >::const_iterator iterator
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
BlockT * getHeader() const
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
iterator end() const
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
LoopT * AllocateLoop(ArgsTy &&...Args)
LoopT * removeLoop(iterator I)
This removes the specified top-level loop from this loop info object.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
void destroy(LoopT *L)
Destroy a loop that has been removed from the LoopInfo nest.
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:596
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form.
Definition LoopInfo.h:441
LLVM_ABI void erase(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
Definition LoopInfo.cpp:908
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
void setLoopID(MDNode *LoopID) const
Set the llvm.loop loop id metadata for this loop.
Definition LoopInfo.cpp:547
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
Definition LoopInfo.cpp:523
Metadata node.
Definition Metadata.h:1080
LLVM_ABI void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1444
ArrayRef< MDOperand > operands() const
Definition Metadata.h:1442
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1572
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1450
LLVMContext & getContext() const
Definition Metadata.h:1244
Tracking metadata reference owned by Metadata.
Definition Metadata.h:902
A single uniqued string.
Definition Metadata.h:722
LLVM_ABI StringRef getString() const
Definition Metadata.cpp:632
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:614
Tuple of metadata.
Definition Metadata.h:1500
BasicBlock * getBlock() const
Definition MemorySSA.h:162
Representation for a specific memory location.
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
Legacy analysis pass which computes MemorySSA.
Definition MemorySSA.h:979
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition MemorySSA.h:702
LLVM_ABI void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
Definition MemorySSA.h:720
Root of the metadata hierarchy.
Definition Metadata.h:64
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
void setIncomingValue(unsigned i, Value *V)
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
bool insert(const T &X)
Insert a new element into the PriorityWorklist.
static bool isAnyOfRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
static bool isFindRecurrenceKind(RecurKind Kind)
static bool isMinMaxRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is any min/max kind.
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition Registry.h:116
Legacy wrapper pass to provide the SCEVAAResult object.
This class uses information about analyze scalars to rewrite expressions in canonical form.
ScalarEvolution * getSE()
LLVM_ABI Value * expandCodeFor(SCEVUse SH, Type *Ty, BasicBlock::iterator I)
Insert code to directly compute the specified SCEV expression into the program.
void eraseDeadInstructions(Value *Root)
Remove inserted instructions that are dead, e.g.
This visitor recursively visits a SCEV expression and re-writes it.
This class represents an analyzed expression in the program.
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
The main scalar evolution driver.
LLVM_ABI bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
LLVM_ABI bool isLoopEntryGuardedByCond(const Loop *L, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether entry to the loop is protected by a conditional between LHS and RHS.
LLVM_ABI const SCEV * getSCEVAtScope(const SCEV *S, const Loop *L)
Return a SCEV expression for the specified value at the specified scope in the program.
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
LLVM_ABI void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L)
Return the "disposition" of the given SCEV with respect to the given loop.
LLVM_ABI bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
LLVM_ABI void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
LLVM_ABI void forgetBlockAndLoopDispositions(Value *V=nullptr)
Called when the client has changed the disposition of values in a loop or block.
LoopDisposition
An enum describing the relationship between a SCEV and a loop.
@ LoopInvariant
The SCEV is loop-invariant.
LLVM_ABI bool isAvailableAtLoopEntry(const SCEV *S, const Loop *L)
Determine if the SCEV can be evaluated at loop's entry.
LLVM_ABI const SCEV * getExitCount(const Loop *L, const BasicBlock *ExitingBlock, ExitCountKind Kind=Exact)
Return the number of times the backedge executes before the given exit would be taken; if not exactly...
LLVM_ABI const SCEV * applyLoopGuards(const SCEV *Expr, const Loop *L)
Try to apply information from loop guards for L to Expr.
This class represents the LLVM 'select' instruction.
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition DenseSet.h:291
A version of PriorityWorklist that selects small size optimized data structures for the vector and ma...
size_type size() const
Definition SmallPtrSet.h:99
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:339
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Value handle that tracks a Value across RAUW.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:313
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:130
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
static LLVM_ABI Intrinsic::ID getForIntrinsic(Intrinsic::ID Id)
The llvm.vp.
static LLVM_ABI bool isVPReduction(Intrinsic::ID ID)
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:549
iterator_range< user_iterator > users()
Definition Value.h:426
bool use_empty() const
Definition Value.h:346
iterator_range< use_iterator > uses()
Definition Value.h:380
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:318
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:165
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
Definition Metadata.h:683
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
LLVM_ABI Value * createSimpleReduction(IRBuilderBase &B, Value *Src, RecurKind RdxKind)
Create a reduction of the given vector.
LLVM_ABI std::optional< ElementCount > getOptionalElementCountLoopAttribute(const Loop *TheLoop)
Find a combination of metadata ("llvm.loop.vectorize.width" and "llvm.loop.vectorize....
BranchProbability getBranchProbability(CondBrInst *B, bool ForFirstTarget)
Based on branch weight metadata, return either:
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
LLVM_ABI Value * addRuntimeChecks(Instruction *Loc, Loop *TheLoop, const SmallVectorImpl< RuntimePointerCheck > &PointerChecks, SCEVExpander &Expander, bool HoistRuntimeChecks=false)
Add code that checks at runtime if the accessed arrays in PointerChecks overlap.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1765
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1759
cl::opt< bool > ProfcheckDisableMetadataFixes
Definition LoopInfo.cpp:60
LLVM_ABI std::optional< unsigned > getLoopEstimatedTripCount(Loop *L, unsigned *EstimatedLoopInvocationWeight=nullptr)
Return either:
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
LLVM_ABI Intrinsic::ID getMinMaxReductionIntrinsicOp(Intrinsic::ID RdxID)
Returns the min/max intrinsic used when expanding a min/max reduction.
LLVM_ABI bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name)
Returns true if Name is applied to TheLoop and enabled.
LLVM_ABI bool isKnownNonPositiveInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE)
Returns true if we can prove that S is defined and always non-positive in loop L.
LLVM_ABI std::optional< bool > getOptionalBoolLoopAttribute(const Loop *TheLoop, StringRef Name)
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Done
Definition Threading.h:60
void appendReversedLoopsToWorklist(RangeT &&, SmallPriorityWorklist< Loop *, 4 > &)
Utility that implements appending of loops onto a worklist given a range.
auto successors(const MachineBasicBlock *BB)
LLVM_ABI void initializeLoopPassPass(PassRegistry &)
Manually defined generic "LoopPass" dependency initialization.
constexpr from_range_t from_range
LLVM_ABI bool formLCSSARecursively(Loop &L, const DominatorTree &DT, const LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
Definition LCSSA.cpp:449
LLVM_ABI Value * getReductionIdentity(Intrinsic::ID RdxID, Type *Ty, FastMathFlags FMF)
Given information about an @llvm.vector.reduce.
LLVM_ABI std::optional< MDNode * > makeFollowupLoopID(MDNode *OrigLoopID, ArrayRef< StringRef > FollowupAttrs, const char *InheritOptionsAttrsPrefix="", bool AlwaysNew=false)
Create a new loop identifier for a loop created from a loop transformation.
LLVM_ABI unsigned getArithmeticReductionInstruction(Intrinsic::ID RdxID)
Returns the arithmetic instruction opcode used when expanding a reduction.
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:634
LLVM_ABI char & LCSSAID
Definition LCSSA.cpp:526
std::pair< const RuntimeCheckingPtrGroup *, const RuntimeCheckingPtrGroup * > RuntimePointerCheck
A memcheck which made up of a pair of grouped pointers.
LLVM_ABI char & LoopSimplifyID
LLVM_ABI Value * createMinMaxOp(IRBuilderBase &Builder, RecurKind RK, Value *Left, Value *Right)
Returns a Min/Max operation corresponding to MinMaxRecurrenceKind.
LLVM_ABI SmallVector< BasicBlock *, 16 > collectChildrenInLoop(DominatorTree *DT, DomTreeNode *N, const Loop *CurLoop)
Does a BFS from a given node to all of its children inside a given loop.
LLVM_ABI void addStringMetadataToLoop(Loop *TheLoop, const char *MDString, unsigned V=0)
Set input string into loop metadata by keeping other values intact.
LLVM_ABI bool cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE, bool Signed)
Returns true if S is defined and never is equal to signed/unsigned max.
LLVM_ABI void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected, bool ElideAllZero=false)
Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:94
constexpr T divideNearest(U Numerator, V Denominator)
Returns (Numerator / Denominator) rounded by round-half-up.
Definition MathExtras.h:458
LLVM_ABI TransformationMode hasVectorizeTransformation(const Loop *L)
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
Definition STLExtras.h:2026
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition Local.cpp:403
LLVM_ABI SmallVector< Instruction *, 8 > findDefsUsedOutsideOfLoop(Loop *L)
Returns the instructions that use values defined in the loop.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI constexpr Intrinsic::ID getReductionIntrinsicID(RecurKind RK)
Returns the llvm.vector.reduce intrinsic that corresponds to the recurrence kind.
LLVM_ABI bool isMustProgress(const Loop *L)
Return true if this loop can be assumed to make progress.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
void setBranchProbability(CondBrInst *B, BranchProbability P, bool ForFirstTarget)
Set branch weight metadata for B to indicate that P and 1 - P are the probabilities of control flowin...
bool isModSet(const ModRefInfo MRI)
Definition ModRef.h:49
LLVM_ABI TransformationMode hasUnrollAndJamTransformation(const Loop *L)
LLVM_ABI void deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE, LoopInfo *LI, MemorySSA *MSSA=nullptr)
This function deletes dead loops.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI bool hasDisableAllTransformsHint(const Loop *L)
Look for the loop attribute that disables all transformation heuristic.
LLVM_TEMPLATE_ABI void appendLoopsToWorklist(RangeT &&, SmallPriorityWorklist< Loop *, 4 > &)
Utility that implements appending of loops onto a worklist given a range.
LLVM_ABI cl::opt< unsigned > SCEVCheapExpansionBudget
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI Value * getShuffleReduction(IRBuilderBase &Builder, Value *Src, unsigned Op, TargetTransformInfo::ReductionShuffle RS, RecurKind MinMaxKind=RecurKind::None)
Generates a vector reduction using shufflevectors to reduce the value.
LLVM_ABI TransformationMode hasUnrollTransformation(const Loop *L)
BranchProbability getLoopProbability(Loop *L)
Based on branch weight metadata, return either:
LLVM_ABI TransformationMode hasDistributeTransformation(const Loop *L)
LLVM_ABI void breakLoopBackedge(Loop *L, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI, MemorySSA *MSSA)
Remove the backedge of the specified loop.
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 void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
LLVM_ABI void propagateIRFlags(Value *I, ArrayRef< Value * > VL, Value *OpValue=nullptr, bool IncludeWrapFlags=true)
Get the intersection (logical and) of all of the potential IR flags of each scalar operation (VL) tha...
LLVM_ABI bool isKnownPositiveInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE)
Returns true if we can prove that S is defined and always positive in loop L.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI unsigned changeToUnreachable(Instruction *I, bool PreserveLCSSA=false, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Insert an unreachable instruction before the specified instruction, making it and the rest of the cod...
Definition Local.cpp:2528
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
LLVM_ABI std::optional< int > getOptionalIntLoopAttribute(const Loop *TheLoop, StringRef Name)
Find named metadata for a loop with an integer value.
bool setLoopProbability(Loop *L, BranchProbability P)
Set branch weight metadata for the latch of L to indicate that, at the end of any iteration,...
LLVM_ABI BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
TargetTransformInfo TTI
LLVM_ABI CmpInst::Predicate getMinMaxReductionPredicate(RecurKind RK)
Returns the comparison predicate used when expanding a min/max reduction.
LLVM_ABI TransformationMode hasLICMVersioningTransformation(const Loop *L)
LLVM_ABI bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition MemorySSA.cpp:84
TransformationMode
The mode sets how eager a transformation should be applied.
Definition LoopUtils.h:283
@ TM_Unspecified
The pass can use heuristics to determine whether a transformation should be applied.
Definition LoopUtils.h:286
@ TM_SuppressedByUser
The transformation must not be applied.
Definition LoopUtils.h:306
@ TM_ForcedByUser
The transformation was directed by the user, e.g.
Definition LoopUtils.h:300
@ TM_Disable
The transformation should not be applied.
Definition LoopUtils.h:292
@ TM_Enable
The transformation should be applied without considering a cost model.
Definition LoopUtils.h:289
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
LLVM_ABI bool hasDisableLICMTransformsHint(const Loop *L)
Look for the loop attribute that disables the LICM transformation heuristics.
template LLVM_TEMPLATE_ABI void appendLoopsToWorklist< Loop & >(Loop &L, SmallPriorityWorklist< Loop *, 4 > &Worklist)
LLVM_ABI Intrinsic::ID getReductionForBinop(Instruction::BinaryOps Opc)
Returns the reduction intrinsic id corresponding to the binary operation.
RecurKind
These are the kinds of recurrences that we support.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ FMinimumNum
FP min with llvm.minimumnum semantics.
@ Or
Bitwise or logical OR of integers.
@ FMinimum
FP min with llvm.minimum semantics.
@ FMaxNum
FP max with llvm.maxnum semantics including NaNs.
@ Mul
Product of integers.
@ None
Not a recurrence.
@ AnyOf
AnyOf reduction with select(cmp(),x,y) where one of (x,y) is loop invariant, and both x and y are int...
@ Xor
Bitwise or logical XOR of integers.
@ FMax
FP max implemented in terms of select(cmp()).
@ FMaximum
FP max with llvm.maximum semantics.
@ FMulAdd
Sum of float products with llvm.fmuladd(a * b + sum).
@ FMul
Product of floats.
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ And
Bitwise or logical AND of integers.
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ FMin
FP min implemented in terms of select(cmp()).
@ FMinNum
FP min with llvm.minnum semantics including NaNs.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
@ AddChainWithSubs
A chain of adds and subs.
@ FAdd
Sum of floats.
@ FMaximumNum
FP max with llvm.maximumnum semantics.
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
LLVM_ABI Value * getRecurrenceIdentity(RecurKind K, Type *Tp, FastMathFlags FMF)
Given information about an recurrence kind, return the identity for the @llvm.vector....
LLVM_ABI BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the specified block at the specified instruction.
LLVM_ABI bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Ensure that all exit blocks of the loop are dedicated exits.
Definition LoopUtils.cpp:61
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
LLVM_ABI bool isKnownNegativeInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE)
Returns true if we can prove that S is defined and always negative in loop L.
constexpr unsigned BitWidth
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
LLVM_ABI Value * expandReductionViaLoop(IRBuilderBase &Builder, Value *Vec, unsigned RdxOpcode, Value *Acc, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Expand a scalable vector reduction into a runtime loop that applies RdxOpcode element by element,...
LLVM_ABI bool setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount, std::optional< unsigned > EstimatedLoopInvocationWeight=std::nullopt)
Set llvm.loop.estimated_trip_count with the value EstimatedTripCount in the loop metadata of L.
LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
LLVM_ABI const char * LLVMLoopEstimatedTripCount
Profile-based loop metadata that should be accessed only by using llvm::getLoopEstimatedTripCount and...
LLVM_ABI bool hasIterationCountInvariantInParent(Loop *L, ScalarEvolution &SE)
Check inner loop (L) backedge count is known to be invariant on all iterations of its outer loop.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
static cl::opt< unsigned > MSSAThreshold("simple-loop-unswitch-memoryssa-threshold", cl::desc("Max number of memory uses to explore during " "partial unswitching analysis"), cl::init(100), cl::Hidden)
LLVM_ABI bool isAlmostDeadIV(PHINode *IV, BasicBlock *LatchBlock, Value *Cond)
Return true if the induction variable IV in a Loop whose latch is LatchBlock would become dead if the...
auto predecessors(const MachineBasicBlock *BB)
LLVM_ABI int rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI, ScalarEvolution *SE, const TargetTransformInfo *TTI, SCEVExpander &Rewriter, DominatorTree *DT, ReplaceExitVal ReplaceExitValue, SmallVector< WeakTrackingVH, 16 > &DeadInsts)
If the final value of any expressions that are recurrent in the loop can be computed,...
LLVM_ABI Value * createOrderedReduction(IRBuilderBase &B, RecurKind RdxKind, Value *Src, Value *Start)
Create an ordered reduction intrinsic using the given recurrence kind RdxKind.
LLVM_ABI Value * addDiffRuntimeChecks(Instruction *Loc, ArrayRef< PointerDiffInfo > Checks, SCEVExpander &Expander, function_ref< Value *(IRBuilderBase &, unsigned)> GetVF, unsigned IC)
LLVM_ABI RecurKind getMinMaxReductionRecurKind(Intrinsic::ID RdxID)
Returns the recurence kind used when expanding a min/max reduction.
ReplaceExitVal
Definition LoopUtils.h:575
@ UnusedIndVarInLoop
Definition LoopUtils.h:579
@ OnlyCheapRepl
Definition LoopUtils.h:577
@ AlwaysRepl
Definition LoopUtils.h:580
LLVM_ABI BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the edge connecting the specified blocks, and return the newly created basic block between From...
LLVM_ABI std::optional< IVConditionInfo > hasPartialIVCondition(const Loop &L, unsigned MSSAThreshold, const MemorySSA &MSSA, AAResults &AA)
Check if the loop header has a conditional branch that is not loop-invariant, because it involves loa...
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
LLVM_ABI Value * createAnyOfReduction(IRBuilderBase &B, Value *Src, Value *InitVal, PHINode *OrigPhi)
Create a reduction of the given vector Src for a reduction of kind RecurKind::AnyOf.
LLVM_ABI bool cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE, bool Signed)
Returns true if S is defined and never is equal to signed/unsigned min.
LLVM_ABI bool isKnownNonNegativeInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE)
Returns true if we can prove that S is defined and always non-negative in loop L.
LLVM_ABI Value * getOrderedReduction(IRBuilderBase &Builder, Value *Acc, Value *Src, unsigned Op, RecurKind MinMaxKind=RecurKind::None)
Generates an ordered vector reduction using extracts to reduce the value.
LLVM_ABI MDNode * findOptionMDForLoopID(MDNode *LoopID, StringRef Name)
Find and return the loop attribute node for the attribute Name in LoopID.
LLVM_ABI Intrinsic::ID getMinMaxReductionIntrinsicID(Intrinsic::ID IID)
Returns the llvm.vector.reduce min/max intrinsic that corresponds to the intrinsic op.
@ Enable
Enable colors.
Definition WithColor.h:47
LLVM_ABI Loop * cloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM, LoopInfo *LI, LPPassManager *LPM)
Recursively clone the specified loop and all of its children, mapping the blocks with the specified m...
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
DbgLoop(const Loop *L)
const Loop * L
IR Values for the lower and upper bounds of a pointer evolution.
TrackingVH< Value > Start
TrackingVH< Value > End
Value * StrideToCheck
unsigned Ith
RewritePhi(PHINode *P, unsigned I, const SCEV *Val, Instruction *ExpansionPt, bool H)
const SCEV * ExpansionSCEV
PHINode * PN
Instruction * ExpansionPoint
Struct to hold information about a partially invariant condition.
Definition LoopUtils.h:647
unsigned AddressSpace
Address space of the involved pointers.
bool NeedsFreeze
Whether the pointer needs to be frozen after expansion, e.g.
const SCEV * High
The SCEV expression which represents the upper bound of all the pointers in this group.
const SCEV * Low
The SCEV expression which represents the lower bound of all the pointers in this group.