LLVM 22.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";
57
59 MemorySSAUpdater *MSSAU,
60 bool PreserveLCSSA) {
61 bool Changed = false;
62
63 // We re-use a vector for the in-loop predecesosrs.
64 SmallVector<BasicBlock *, 4> InLoopPredecessors;
65
66 auto RewriteExit = [&](BasicBlock *BB) {
67 assert(InLoopPredecessors.empty() &&
68 "Must start with an empty predecessors list!");
69 llvm::scope_exit Cleanup([&] { InLoopPredecessors.clear(); });
70
71 // See if there are any non-loop predecessors of this exit block and
72 // keep track of the in-loop predecessors.
73 bool IsDedicatedExit = true;
74 for (auto *PredBB : predecessors(BB))
75 if (L->contains(PredBB)) {
76 if (isa<IndirectBrInst>(PredBB->getTerminator()))
77 // We cannot rewrite exiting edges from an indirectbr.
78 return false;
79
80 InLoopPredecessors.push_back(PredBB);
81 } else {
82 IsDedicatedExit = false;
83 }
84
85 assert(!InLoopPredecessors.empty() && "Must have *some* loop predecessor!");
86
87 // Nothing to do if this is already a dedicated exit.
88 if (IsDedicatedExit)
89 return false;
90
91 auto *NewExitBB = SplitBlockPredecessors(
92 BB, InLoopPredecessors, ".loopexit", DT, LI, MSSAU, PreserveLCSSA);
93
94 if (!NewExitBB)
96 dbgs() << "WARNING: Can't create a dedicated exit block for loop: "
97 << *L << "\n");
98 else
99 LLVM_DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "
100 << NewExitBB->getName() << "\n");
101 return true;
102 };
103
104 // Walk the exit blocks directly rather than building up a data structure for
105 // them, but only visit each one once.
107 for (auto *BB : L->blocks())
108 for (auto *SuccBB : successors(BB)) {
109 // We're looking for exit blocks so skip in-loop successors.
110 if (L->contains(SuccBB))
111 continue;
112
113 // Visit each exit block exactly once.
114 if (!Visited.insert(SuccBB).second)
115 continue;
116
117 Changed |= RewriteExit(SuccBB);
118 }
119
120 return Changed;
121}
122
123/// Returns the instructions that use values defined in the loop.
126
127 for (auto *Block : L->getBlocks())
128 // FIXME: I believe that this could use copy_if if the Inst reference could
129 // be adapted into a pointer.
130 for (auto &Inst : *Block) {
131 auto Users = Inst.users();
132 if (any_of(Users, [&](User *U) {
133 auto *Use = cast<Instruction>(U);
134 return !L->contains(Use->getParent());
135 }))
136 UsedOutside.push_back(&Inst);
137 }
138
139 return UsedOutside;
140}
141
143 // By definition, all loop passes need the LoopInfo analysis and the
144 // Dominator tree it depends on. Because they all participate in the loop
145 // pass manager, they must also preserve these.
150
151 // We must also preserve LoopSimplify and LCSSA. We locally access their IDs
152 // here because users shouldn't directly get them from this header.
153 extern char &LoopSimplifyID;
154 extern char &LCSSAID;
159 // This is used in the LPPassManager to perform LCSSA verification on passes
160 // which preserve lcssa form
163
164 // Loop passes are designed to run inside of a loop pass manager which means
165 // that any function analyses they require must be required by the first loop
166 // pass in the manager (so that it is computed before the loop pass manager
167 // runs) and preserved by all loop pasess in the manager. To make this
168 // reasonably robust, the set needed for most loop passes is maintained here.
169 // If your loop pass requires an analysis not listed here, you will need to
170 // carefully audit the loop pass manager nesting structure that results.
178 // FIXME: When all loop passes preserve MemorySSA, it can be required and
179 // preserved here instead of the individual handling in each pass.
180}
181
182/// Manually defined generic "LoopPass" dependency initialization. This is used
183/// to initialize the exact set of passes from above in \c
184/// getLoopAnalysisUsage. It can be used within a loop pass's initialization
185/// with:
186///
187/// INITIALIZE_PASS_DEPENDENCY(LoopPass)
188///
189/// As-if "LoopPass" were a pass.
202
203/// Create MDNode for input string.
204static MDNode *createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V) {
205 LLVMContext &Context = TheLoop->getHeader()->getContext();
206 Metadata *MDs[] = {
207 MDString::get(Context, Name),
208 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), V))};
209 return MDNode::get(Context, MDs);
210}
211
212/// Set input string into loop metadata by keeping other values intact.
213/// If the string is already in loop metadata update value if it is
214/// different.
215void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *StringMD,
216 unsigned V) {
218 // If the loop already has metadata, retain it.
219 MDNode *LoopID = TheLoop->getLoopID();
220 if (LoopID) {
221 for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
222 MDNode *Node = cast<MDNode>(LoopID->getOperand(i));
223 // If it is of form key = value, try to parse it.
224 if (Node->getNumOperands() == 2) {
225 MDString *S = dyn_cast<MDString>(Node->getOperand(0));
226 if (S && S->getString() == StringMD) {
227 ConstantInt *IntMD =
229 if (IntMD && IntMD->getSExtValue() == V)
230 // It is already in place. Do nothing.
231 return;
232 // We need to update the value, so just skip it here and it will
233 // be added after copying other existed nodes.
234 continue;
235 }
236 }
237 MDs.push_back(Node);
238 }
239 }
240 // Add new metadata.
241 MDs.push_back(createStringMetadata(TheLoop, StringMD, V));
242 // Replace current metadata node with new one.
243 LLVMContext &Context = TheLoop->getHeader()->getContext();
244 MDNode *NewLoopID = MDNode::get(Context, MDs);
245 // Set operand 0 to refer to the loop id itself.
246 NewLoopID->replaceOperandWith(0, NewLoopID);
247 TheLoop->setLoopID(NewLoopID);
248}
249
250std::optional<ElementCount>
252 std::optional<int> Width =
253 getOptionalIntLoopAttribute(TheLoop, "llvm.loop.vectorize.width");
254
255 if (Width) {
256 std::optional<int> IsScalable = getOptionalIntLoopAttribute(
257 TheLoop, "llvm.loop.vectorize.scalable.enable");
258 return ElementCount::get(*Width, IsScalable.value_or(false));
259 }
260
261 return std::nullopt;
262}
263
264std::optional<MDNode *> llvm::makeFollowupLoopID(
265 MDNode *OrigLoopID, ArrayRef<StringRef> FollowupOptions,
266 const char *InheritOptionsExceptPrefix, bool AlwaysNew) {
267 if (!OrigLoopID) {
268 if (AlwaysNew)
269 return nullptr;
270 return std::nullopt;
271 }
272
273 assert(OrigLoopID->getOperand(0) == OrigLoopID);
274
275 bool InheritAllAttrs = !InheritOptionsExceptPrefix;
276 bool InheritSomeAttrs =
277 InheritOptionsExceptPrefix && InheritOptionsExceptPrefix[0] != '\0';
279 MDs.push_back(nullptr);
280
281 bool Changed = false;
282 if (InheritAllAttrs || InheritSomeAttrs) {
283 for (const MDOperand &Existing : drop_begin(OrigLoopID->operands())) {
284 MDNode *Op = cast<MDNode>(Existing.get());
285
286 auto InheritThisAttribute = [InheritSomeAttrs,
287 InheritOptionsExceptPrefix](MDNode *Op) {
288 if (!InheritSomeAttrs)
289 return false;
290
291 // Skip malformatted attribute metadata nodes.
292 if (Op->getNumOperands() == 0)
293 return true;
294 Metadata *NameMD = Op->getOperand(0).get();
295 if (!isa<MDString>(NameMD))
296 return true;
297 StringRef AttrName = cast<MDString>(NameMD)->getString();
298
299 // Do not inherit excluded attributes.
300 return !AttrName.starts_with(InheritOptionsExceptPrefix);
301 };
302
303 if (InheritThisAttribute(Op))
304 MDs.push_back(Op);
305 else
306 Changed = true;
307 }
308 } else {
309 // Modified if we dropped at least one attribute.
310 Changed = OrigLoopID->getNumOperands() > 1;
311 }
312
313 bool HasAnyFollowup = false;
314 for (StringRef OptionName : FollowupOptions) {
315 MDNode *FollowupNode = findOptionMDForLoopID(OrigLoopID, OptionName);
316 if (!FollowupNode)
317 continue;
318
319 HasAnyFollowup = true;
320 for (const MDOperand &Option : drop_begin(FollowupNode->operands())) {
321 MDs.push_back(Option.get());
322 Changed = true;
323 }
324 }
325
326 // Attributes of the followup loop not specified explicity, so signal to the
327 // transformation pass to add suitable attributes.
328 if (!AlwaysNew && !HasAnyFollowup)
329 return std::nullopt;
330
331 // If no attributes were added or remove, the previous loop Id can be reused.
332 if (!AlwaysNew && !Changed)
333 return OrigLoopID;
334
335 // No attributes is equivalent to having no !llvm.loop metadata at all.
336 if (MDs.size() == 1)
337 return nullptr;
338
339 // Build the new loop ID.
340 MDTuple *FollowupLoopID = MDNode::get(OrigLoopID->getContext(), MDs);
341 FollowupLoopID->replaceOperandWith(0, FollowupLoopID);
342 return FollowupLoopID;
343}
344
348
352
354 if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable"))
355 return TM_SuppressedByUser;
356
357 std::optional<int> Count =
358 getOptionalIntLoopAttribute(L, "llvm.loop.unroll.count");
359 if (Count)
361
362 if (getBooleanLoopAttribute(L, "llvm.loop.unroll.enable"))
363 return TM_ForcedByUser;
364
365 if (getBooleanLoopAttribute(L, "llvm.loop.unroll.full"))
366 return TM_ForcedByUser;
367
369 return TM_Disable;
370
371 return TM_Unspecified;
372}
373
375 if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.disable"))
376 return TM_SuppressedByUser;
377
378 std::optional<int> Count =
379 getOptionalIntLoopAttribute(L, "llvm.loop.unroll_and_jam.count");
380 if (Count)
382
383 if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.enable"))
384 return TM_ForcedByUser;
385
387 return TM_Disable;
388
389 return TM_Unspecified;
390}
391
393 std::optional<bool> Enable =
394 getOptionalBoolLoopAttribute(L, "llvm.loop.vectorize.enable");
395
396 if (Enable == false)
397 return TM_SuppressedByUser;
398
399 std::optional<ElementCount> VectorizeWidth =
401 std::optional<int> InterleaveCount =
402 getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count");
403
404 // 'Forcing' vector width and interleave count to one effectively disables
405 // this tranformation.
406 if (Enable == true && VectorizeWidth && VectorizeWidth->isScalar() &&
407 InterleaveCount == 1)
408 return TM_SuppressedByUser;
409
410 if (getBooleanLoopAttribute(L, "llvm.loop.isvectorized"))
411 return TM_Disable;
412
413 if (Enable == true)
414 return TM_ForcedByUser;
415
416 if ((VectorizeWidth && VectorizeWidth->isScalar()) && InterleaveCount == 1)
417 return TM_Disable;
418
419 if ((VectorizeWidth && VectorizeWidth->isVector()) || InterleaveCount > 1)
420 return TM_Enable;
421
423 return TM_Disable;
424
425 return TM_Unspecified;
426}
427
429 if (getBooleanLoopAttribute(L, "llvm.loop.distribute.enable"))
430 return TM_ForcedByUser;
431
433 return TM_Disable;
434
435 return TM_Unspecified;
436}
437
439 if (getBooleanLoopAttribute(L, "llvm.loop.licm_versioning.disable"))
440 return TM_SuppressedByUser;
441
443 return TM_Disable;
444
445 return TM_Unspecified;
446}
447
448/// Does a BFS from a given node to all of its children inside a given loop.
449/// The returned vector of basic blocks includes the starting point.
451 DomTreeNode *N,
452 const Loop *CurLoop) {
454 auto AddRegionToWorklist = [&](DomTreeNode *DTN) {
455 // Only include subregions in the top level loop.
456 BasicBlock *BB = DTN->getBlock();
457 if (CurLoop->contains(BB))
458 Worklist.push_back(DTN->getBlock());
459 };
460
461 AddRegionToWorklist(N);
462
463 for (size_t I = 0; I < Worklist.size(); I++) {
464 for (DomTreeNode *Child : DT->getNode(Worklist[I])->children())
465 AddRegionToWorklist(Child);
466 }
467
468 return Worklist;
469}
470
472 int LatchIdx = PN->getBasicBlockIndex(LatchBlock);
473 assert(LatchIdx != -1 && "LatchBlock is not a case in this PHINode");
474 Value *IncV = PN->getIncomingValue(LatchIdx);
475
476 for (User *U : PN->users())
477 if (U != Cond && U != IncV) return false;
478
479 for (User *U : IncV->users())
480 if (U != Cond && U != PN) return false;
481 return true;
482}
483
484
486 LoopInfo *LI, MemorySSA *MSSA) {
487 assert((!DT || L->isLCSSAForm(*DT)) && "Expected LCSSA!");
488 auto *Preheader = L->getLoopPreheader();
489 assert(Preheader && "Preheader should exist!");
490
491 std::unique_ptr<MemorySSAUpdater> MSSAU;
492 if (MSSA)
493 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
494
495 // Now that we know the removal is safe, remove the loop by changing the
496 // branch from the preheader to go to the single exit block.
497 //
498 // Because we're deleting a large chunk of code at once, the sequence in which
499 // we remove things is very important to avoid invalidation issues.
500
501 // Tell ScalarEvolution that the loop is deleted. Do this before
502 // deleting the loop so that ScalarEvolution can look at the loop
503 // to determine what it needs to clean up.
504 if (SE) {
505 SE->forgetLoop(L);
507 }
508
509 Instruction *OldTerm = Preheader->getTerminator();
510 assert(!OldTerm->mayHaveSideEffects() &&
511 "Preheader must end with a side-effect-free terminator");
512 assert(OldTerm->getNumSuccessors() == 1 &&
513 "Preheader must have a single successor");
514 // Connect the preheader to the exit block. Keep the old edge to the header
515 // around to perform the dominator tree update in two separate steps
516 // -- #1 insertion of the edge preheader -> exit and #2 deletion of the edge
517 // preheader -> header.
518 //
519 //
520 // 0. Preheader 1. Preheader 2. Preheader
521 // | | | |
522 // V | V |
523 // Header <--\ | Header <--\ | Header <--\
524 // | | | | | | | | | | |
525 // | V | | | V | | | V |
526 // | Body --/ | | Body --/ | | Body --/
527 // V V V V V
528 // Exit Exit Exit
529 //
530 // By doing this is two separate steps we can perform the dominator tree
531 // update without using the batch update API.
532 //
533 // Even when the loop is never executed, we cannot remove the edge from the
534 // source block to the exit block. Consider the case where the unexecuted loop
535 // branches back to an outer loop. If we deleted the loop and removed the edge
536 // coming to this inner loop, this will break the outer loop structure (by
537 // deleting the backedge of the outer loop). If the outer loop is indeed a
538 // non-loop, it will be deleted in a future iteration of loop deletion pass.
539 IRBuilder<> Builder(OldTerm);
540
541 auto *ExitBlock = L->getUniqueExitBlock();
542 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
543 if (ExitBlock) {
544 assert(ExitBlock && "Should have a unique exit block!");
545 assert(L->hasDedicatedExits() && "Loop should have dedicated exits!");
546
547 Builder.CreateCondBr(Builder.getFalse(), L->getHeader(), ExitBlock);
548 // Remove the old branch. The conditional branch becomes a new terminator.
549 OldTerm->eraseFromParent();
550
551 // Rewrite phis in the exit block to get their inputs from the Preheader
552 // instead of the exiting block.
553 for (PHINode &P : ExitBlock->phis()) {
554 // Set the zero'th element of Phi to be from the preheader and remove all
555 // other incoming values. Given the loop has dedicated exits, all other
556 // incoming values must be from the exiting blocks.
557 int PredIndex = 0;
558 P.setIncomingBlock(PredIndex, Preheader);
559 // Removes all incoming values from all other exiting blocks (including
560 // duplicate values from an exiting block).
561 // Nuke all entries except the zero'th entry which is the preheader entry.
562 P.removeIncomingValueIf([](unsigned Idx) { return Idx != 0; },
563 /* DeletePHIIfEmpty */ false);
564
565 assert((P.getNumIncomingValues() == 1 &&
566 P.getIncomingBlock(PredIndex) == Preheader) &&
567 "Should have exactly one value and that's from the preheader!");
568 }
569
570 if (DT) {
571 DTU.applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}});
572 if (MSSA) {
573 MSSAU->applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}},
574 *DT);
575 if (VerifyMemorySSA)
576 MSSA->verifyMemorySSA();
577 }
578 }
579
580 // Disconnect the loop body by branching directly to its exit.
581 Builder.SetInsertPoint(Preheader->getTerminator());
582 Builder.CreateBr(ExitBlock);
583 // Remove the old branch.
584 Preheader->getTerminator()->eraseFromParent();
585 } else {
586 assert(L->hasNoExitBlocks() &&
587 "Loop should have either zero or one exit blocks.");
588
589 Builder.SetInsertPoint(OldTerm);
590 Builder.CreateUnreachable();
591 Preheader->getTerminator()->eraseFromParent();
592 }
593
594 if (DT) {
595 DTU.applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}});
596 if (MSSA) {
597 MSSAU->applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}},
598 *DT);
599 SmallSetVector<BasicBlock *, 8> DeadBlockSet(L->block_begin(),
600 L->block_end());
601 MSSAU->removeBlocks(DeadBlockSet);
602 if (VerifyMemorySSA)
603 MSSA->verifyMemorySSA();
604 }
605 }
606
607 // Use a map to unique and a vector to guarantee deterministic ordering.
609 llvm::SmallVector<DbgVariableRecord *, 4> DeadDbgVariableRecords;
610
611 // Given LCSSA form is satisfied, we should not have users of instructions
612 // within the dead loop outside of the loop. However, LCSSA doesn't take
613 // unreachable uses into account. We handle them here.
614 // We could do it after drop all references (in this case all users in the
615 // loop will be already eliminated and we have less work to do but according
616 // to API doc of User::dropAllReferences only valid operation after dropping
617 // references, is deletion. So let's substitute all usages of
618 // instruction from the loop with poison value of corresponding type first.
619 for (auto *Block : L->blocks())
620 for (Instruction &I : *Block) {
621 auto *Poison = PoisonValue::get(I.getType());
622 for (Use &U : llvm::make_early_inc_range(I.uses())) {
623 if (auto *Usr = dyn_cast<Instruction>(U.getUser()))
624 if (L->contains(Usr->getParent()))
625 continue;
626 // If we have a DT then we can check that uses outside a loop only in
627 // unreachable block.
628 if (DT)
630 "Unexpected user in reachable block");
631 U.set(Poison);
632 }
633
634 if (ExitBlock) {
635 // For one of each variable encountered, preserve a debug record (set
636 // to Poison) and transfer it to the loop exit. This terminates any
637 // variable locations that were set during the loop.
638 for (DbgVariableRecord &DVR :
639 llvm::make_early_inc_range(filterDbgVars(I.getDbgRecordRange()))) {
640 DebugVariable Key(DVR.getVariable(), DVR.getExpression(),
641 DVR.getDebugLoc().get());
642 if (!DeadDebugSet.insert(Key).second)
643 continue;
644 // Unlinks the DVR from it's container, for later insertion.
645 DVR.removeFromParent();
646 DeadDbgVariableRecords.push_back(&DVR);
647 }
648 }
649 }
650
651 if (ExitBlock) {
652 // After the loop has been deleted all the values defined and modified
653 // inside the loop are going to be unavailable. Values computed in the
654 // loop will have been deleted, automatically causing their debug uses
655 // be be replaced with undef. Loop invariant values will still be available.
656 // Move dbg.values out the loop so that earlier location ranges are still
657 // terminated and loop invariant assignments are preserved.
658 DIBuilder DIB(*ExitBlock->getModule());
659 BasicBlock::iterator InsertDbgValueBefore =
660 ExitBlock->getFirstInsertionPt();
661 assert(InsertDbgValueBefore != ExitBlock->end() &&
662 "There should be a non-PHI instruction in exit block, else these "
663 "instructions will have no parent.");
664
665 // Due to the "head" bit in BasicBlock::iterator, we're going to insert
666 // each DbgVariableRecord right at the start of the block, wheras dbg.values
667 // would be repeatedly inserted before the first instruction. To replicate
668 // this behaviour, do it backwards.
669 for (DbgVariableRecord *DVR : llvm::reverse(DeadDbgVariableRecords))
670 ExitBlock->insertDbgRecordBefore(DVR, InsertDbgValueBefore);
671 }
672
673 // Remove the block from the reference counting scheme, so that we can
674 // delete it freely later.
675 for (auto *Block : L->blocks())
676 Block->dropAllReferences();
677
678 if (MSSA && VerifyMemorySSA)
679 MSSA->verifyMemorySSA();
680
681 if (LI) {
682 // Erase the instructions and the blocks without having to worry
683 // about ordering because we already dropped the references.
684 // NOTE: This iteration is safe because erasing the block does not remove
685 // its entry from the loop's block list. We do that in the next section.
686 for (BasicBlock *BB : L->blocks())
687 BB->eraseFromParent();
688
689 // Finally, the blocks from loopinfo. This has to happen late because
690 // otherwise our loop iterators won't work.
691
693 for (BasicBlock *BB : blocks)
694 LI->removeBlock(BB);
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<BranchInst>(Latch->getTerminator())) {
731 if (!BI->isConditional()) {
732 DomTreeUpdater DTU(&DT, DomTreeUpdater::UpdateStrategy::Eager);
733 (void)changeToUnreachable(BI, /*PreserveLCSSA*/ true, &DTU,
734 MSSAU.get());
735 return;
736 }
737
738 // Conditional latch/exit - note that latch can be shared by inner
739 // and outer loop so the other target doesn't need to an exit
740 if (L->isLoopExiting(Latch)) {
741 // TODO: Generalize ConstantFoldTerminator so that it can be used
742 // here without invalidating LCSSA or MemorySSA. (Tricky case for
743 // LCSSA: header is an exit block of a preceeding sibling loop w/o
744 // dedicated exits.)
745 const unsigned ExitIdx = L->contains(BI->getSuccessor(0)) ? 1 : 0;
746 BasicBlock *ExitBB = BI->getSuccessor(ExitIdx);
747
748 DomTreeUpdater DTU(&DT, DomTreeUpdater::UpdateStrategy::Eager);
749 Header->removePredecessor(Latch, true);
750
751 IRBuilder<> Builder(BI);
752 auto *NewBI = Builder.CreateBr(ExitBB);
753 // Transfer the metadata to the new branch instruction (minus the
754 // loop info since this is no longer a loop)
755 NewBI->copyMetadata(*BI, {LLVMContext::MD_dbg,
756 LLVMContext::MD_annotation});
757
758 BI->eraseFromParent();
759 DTU.applyUpdates({{DominatorTree::Delete, Latch, Header}});
760 if (MSSA)
761 MSSAU->applyUpdates({{DominatorTree::Delete, Latch, Header}}, DT);
762 return;
763 }
764 }
765
766 // General case. By splitting the backedge, and then explicitly making it
767 // unreachable we gracefully handle corner cases such as switch and invoke
768 // termiantors.
769 auto *BackedgeBB = SplitEdge(Latch, Header, &DT, &LI, MSSAU.get());
770
771 DomTreeUpdater DTU(&DT, DomTreeUpdater::UpdateStrategy::Eager);
772 (void)changeToUnreachable(BackedgeBB->getTerminator(),
773 /*PreserveLCSSA*/ true, &DTU, MSSAU.get());
774 }();
775
776 // Erase (and destroy) this loop instance. Handles relinking sub-loops
777 // and blocks within the loop as needed.
778 LI.erase(L);
779
780 // If the loop we broke had a parent, then changeToUnreachable might have
781 // caused a block to be removed from the parent loop (see loop_nest_lcssa
782 // test case in zero-btc.ll for an example), thus changing the parent's
783 // exit blocks. If that happened, we need to rebuild LCSSA on the outermost
784 // loop which might have a had a block removed.
785 if (OutermostLoop != L)
786 formLCSSARecursively(*OutermostLoop, DT, &LI, &SE);
787}
788
789
790/// Checks if \p L has an exiting latch branch. There may also be other
791/// exiting blocks. Returns branch instruction terminating the loop
792/// latch if above check is successful, nullptr otherwise.
794 BasicBlock *Latch = L->getLoopLatch();
795 if (!Latch)
796 return nullptr;
797
798 BranchInst *LatchBR = dyn_cast<BranchInst>(Latch->getTerminator());
799 if (!LatchBR || LatchBR->getNumSuccessors() != 2 || !L->isLoopExiting(Latch))
800 return nullptr;
801
802 assert((LatchBR->getSuccessor(0) == L->getHeader() ||
803 LatchBR->getSuccessor(1) == L->getHeader()) &&
804 "At least one edge out of the latch must go to the header");
805
806 return LatchBR;
807}
808
809struct DbgLoop {
810 const Loop *L;
811 explicit DbgLoop(const Loop *L) : L(L) {}
812};
813
814#ifndef NDEBUG
816 OS << "function ";
817 D.L->getHeader()->getParent()->printAsOperand(OS, /*PrintType=*/false);
818 return OS << " " << *D.L;
819}
820#endif // NDEBUG
821
822static std::optional<unsigned> estimateLoopTripCount(Loop *L) {
823 // Currently we take the estimate exit count only from the loop latch,
824 // ignoring other exiting blocks. This can overestimate the trip count
825 // if we exit through another exit, but can never underestimate it.
826 // TODO: incorporate information from other exits
827 BranchInst *ExitingBranch = getExpectedExitLoopLatchBranch(L);
828 if (!ExitingBranch) {
829 LLVM_DEBUG(dbgs() << "estimateLoopTripCount: Failed to find exiting "
830 << "latch branch of required form in " << DbgLoop(L)
831 << "\n");
832 return std::nullopt;
833 }
834
835 // To estimate the number of times the loop body was executed, we want to
836 // know the number of times the backedge was taken, vs. the number of times
837 // we exited the loop.
838 uint64_t LoopWeight, ExitWeight;
839 if (!extractBranchWeights(*ExitingBranch, LoopWeight, ExitWeight)) {
840 LLVM_DEBUG(dbgs() << "estimateLoopTripCount: Failed to extract branch "
841 << "weights for " << DbgLoop(L) << "\n");
842 return std::nullopt;
843 }
844
845 if (L->contains(ExitingBranch->getSuccessor(1)))
846 std::swap(LoopWeight, ExitWeight);
847
848 if (!ExitWeight) {
849 // Don't have a way to return predicated infinite
850 LLVM_DEBUG(dbgs() << "estimateLoopTripCount: Failed because of zero exit "
851 << "probability for " << DbgLoop(L) << "\n");
852 return std::nullopt;
853 }
854
855 // Estimated exit count is a ratio of the loop weight by the weight of the
856 // edge exiting the loop, rounded to nearest.
857 uint64_t ExitCount = llvm::divideNearest(LoopWeight, ExitWeight);
858
859 // When ExitCount + 1 would wrap in unsigned, saturate at UINT_MAX.
860 if (ExitCount >= std::numeric_limits<unsigned>::max())
861 return std::numeric_limits<unsigned>::max();
862
863 // Estimated trip count is one plus estimated exit count.
864 uint64_t TC = ExitCount + 1;
865 LLVM_DEBUG(dbgs() << "estimateLoopTripCount: Estimated trip count of " << TC
866 << " for " << DbgLoop(L) << "\n");
867 return TC;
868}
869
870std::optional<unsigned>
872 unsigned *EstimatedLoopInvocationWeight) {
873 // If EstimatedLoopInvocationWeight, we do not support this loop if
874 // getExpectedExitLoopLatchBranch returns nullptr.
875 //
876 // FIXME: Also, this is a stop-gap solution for nested loops. It avoids
877 // mistaking LLVMLoopEstimatedTripCount metadata to be for an outer loop when
878 // it was created for an inner loop. The problem is that loop metadata is
879 // attached to the branch instruction in the loop latch block, but that can be
880 // shared by the loops. A solution is to attach loop metadata to loop headers
881 // instead, but that would be a large change to LLVM.
882 //
883 // Until that happens, we work around the problem as follows.
884 // getExpectedExitLoopLatchBranch (which also guards
885 // setLoopEstimatedTripCount) returns nullptr for a loop unless the loop has
886 // one latch and that latch has exactly two successors one of which is an exit
887 // from the loop. If the latch is shared by nested loops, then that condition
888 // might hold for the inner loop but cannot hold for the outer loop:
889 // - Because the latch is shared, it must have at least two successors: the
890 // inner loop header and the outer loop header, which is also an exit for
891 // the inner loop. That satisifies the condition for the inner loop.
892 // - To satsify the condition for the outer loop, the latch must have a third
893 // successor that is an exit for the outer loop. But that violates the
894 // condition for both loops.
895 BranchInst *ExitingBranch = getExpectedExitLoopLatchBranch(L);
896 if (!ExitingBranch)
897 return std::nullopt;
898
899 // If requested, either compute *EstimatedLoopInvocationWeight or return
900 // nullopt if cannot.
901 //
902 // TODO: Eventually, once all passes have migrated away from setting branch
903 // weights to indicate estimated trip counts, this function will drop the
904 // EstimatedLoopInvocationWeight parameter.
905 if (EstimatedLoopInvocationWeight) {
906 uint64_t LoopWeight = 0, ExitWeight = 0; // Inits expected to be unused.
907 if (!extractBranchWeights(*ExitingBranch, LoopWeight, ExitWeight))
908 return std::nullopt;
909 if (L->contains(ExitingBranch->getSuccessor(1)))
910 std::swap(LoopWeight, ExitWeight);
911 if (!ExitWeight)
912 return std::nullopt;
913 *EstimatedLoopInvocationWeight = ExitWeight;
914 }
915
916 // Return the estimated trip count from metadata unless the metadata is
917 // missing or has no value.
918 //
919 // Some passes set llvm.loop.estimated_trip_count to 0. For example, after
920 // peeling 10 or more iterations from a loop with an estimated trip count of
921 // 10, llvm.loop.estimated_trip_count becomes 0 on the remaining loop. It
922 // indicates that, each time execution reaches the peeled iterations,
923 // execution is estimated to exit them without reaching the remaining loop's
924 // header.
925 //
926 // Even if the probability of reaching a loop's header is low, if it is
927 // reached, it is the start of an iteration. Consequently, some passes
928 // historically assume that llvm::getLoopEstimatedTripCount always returns a
929 // positive count or std::nullopt. Thus, return std::nullopt when
930 // llvm.loop.estimated_trip_count is 0.
932 LLVM_DEBUG(dbgs() << "getLoopEstimatedTripCount: "
933 << LLVMLoopEstimatedTripCount << " metadata has trip "
934 << "count of " << *TC
935 << (*TC == 0 ? " (returning std::nullopt)" : "")
936 << " for " << DbgLoop(L) << "\n");
937 return *TC == 0 ? std::nullopt : std::optional(*TC);
938 }
939
940 // Estimate the trip count from latch branch weights.
941 return estimateLoopTripCount(L);
942}
943
945 Loop *L, unsigned EstimatedTripCount,
946 std::optional<unsigned> EstimatedloopInvocationWeight) {
947 // If EstimatedLoopInvocationWeight, we do not support this loop if
948 // getExpectedExitLoopLatchBranch returns nullptr.
949 //
950 // FIXME: See comments in getLoopEstimatedTripCount for why this is required
951 // here regardless of EstimatedLoopInvocationWeight.
953 if (!LatchBranch)
954 return false;
955
956 // Set the metadata.
958
959 // At the moment, we currently support changing the estimated trip count in
960 // the latch branch's branch weights only. We could extend this API to
961 // manipulate estimated trip counts for any exit.
962 //
963 // TODO: Eventually, once all passes have migrated away from setting branch
964 // weights to indicate estimated trip counts, we will not set branch weights
965 // here at all.
966 if (!EstimatedloopInvocationWeight)
967 return true;
968
969 // Calculate taken and exit weights.
970 unsigned LatchExitWeight = 0;
971 unsigned BackedgeTakenWeight = 0;
972
973 if (EstimatedTripCount != 0) {
974 LatchExitWeight = *EstimatedloopInvocationWeight;
975 BackedgeTakenWeight = (EstimatedTripCount - 1) * LatchExitWeight;
976 }
977
978 // Make a swap if back edge is taken when condition is "false".
979 if (LatchBranch->getSuccessor(0) != L->getHeader())
980 std::swap(BackedgeTakenWeight, LatchExitWeight);
981
982 // Set/Update profile metadata.
983 setBranchWeights(*LatchBranch, {BackedgeTakenWeight, LatchExitWeight},
984 /*IsExpected=*/false);
985
986 return true;
987}
988
991 if (!LatchBranch)
993 bool FirstTargetIsLoop = LatchBranch->getSuccessor(0) == L->getHeader();
994 return getBranchProbability(LatchBranch, FirstTargetIsLoop);
995}
996
999 if (!LatchBranch)
1000 return false;
1001 bool FirstTargetIsLoop = LatchBranch->getSuccessor(0) == L->getHeader();
1002 return setBranchProbability(LatchBranch, P, FirstTargetIsLoop);
1003}
1004
1006 bool ForFirstTarget) {
1007 if (B->getNumSuccessors() != 2)
1009 uint64_t Weight0, Weight1;
1010 if (!extractBranchWeights(*B, Weight0, Weight1))
1012 uint64_t Denominator = Weight0 + Weight1;
1013 if (Denominator == 0)
1015 if (!ForFirstTarget)
1016 std::swap(Weight0, Weight1);
1017 return BranchProbability::getBranchProbability(Weight0, Denominator);
1018}
1019
1021 bool ForFirstTarget) {
1022 if (B->getNumSuccessors() != 2)
1023 return false;
1024 BranchProbability Prob0 = P;
1025 BranchProbability Prob1 = P.getCompl();
1026 if (!ForFirstTarget)
1027 std::swap(Prob0, Prob1);
1028 setBranchWeights(*B, {Prob0.getNumerator(), Prob1.getNumerator()},
1029 /*IsExpected=*/false);
1030 return true;
1031}
1032
1034 ScalarEvolution &SE) {
1035 Loop *OuterL = InnerLoop->getParentLoop();
1036 if (!OuterL)
1037 return true;
1038
1039 // Get the backedge taken count for the inner loop
1040 BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
1041 const SCEV *InnerLoopBECountSC = SE.getExitCount(InnerLoop, InnerLoopLatch);
1042 if (isa<SCEVCouldNotCompute>(InnerLoopBECountSC) ||
1043 !InnerLoopBECountSC->getType()->isIntegerTy())
1044 return false;
1045
1046 // Get whether count is invariant to the outer loop
1048 SE.getLoopDisposition(InnerLoopBECountSC, OuterL);
1050 return false;
1051
1052 return true;
1053}
1054
1056 switch (RK) {
1057 default:
1058 llvm_unreachable("Unexpected recurrence kind");
1060 case RecurKind::Sub:
1061 case RecurKind::Add:
1062 return Intrinsic::vector_reduce_add;
1063 case RecurKind::Mul:
1064 return Intrinsic::vector_reduce_mul;
1065 case RecurKind::And:
1066 return Intrinsic::vector_reduce_and;
1067 case RecurKind::Or:
1068 return Intrinsic::vector_reduce_or;
1069 case RecurKind::Xor:
1070 return Intrinsic::vector_reduce_xor;
1071 case RecurKind::FMulAdd:
1072 case RecurKind::FAdd:
1073 return Intrinsic::vector_reduce_fadd;
1074 case RecurKind::FMul:
1075 return Intrinsic::vector_reduce_fmul;
1076 case RecurKind::SMax:
1077 return Intrinsic::vector_reduce_smax;
1078 case RecurKind::SMin:
1079 return Intrinsic::vector_reduce_smin;
1080 case RecurKind::UMax:
1081 return Intrinsic::vector_reduce_umax;
1082 case RecurKind::UMin:
1083 return Intrinsic::vector_reduce_umin;
1084 case RecurKind::FMax:
1085 case RecurKind::FMaxNum:
1086 return Intrinsic::vector_reduce_fmax;
1087 case RecurKind::FMin:
1088 case RecurKind::FMinNum:
1089 return Intrinsic::vector_reduce_fmin;
1091 return Intrinsic::vector_reduce_fmaximum;
1093 return Intrinsic::vector_reduce_fminimum;
1095 return Intrinsic::vector_reduce_fmax;
1097 return Intrinsic::vector_reduce_fmin;
1098 }
1099}
1100
1102 switch (IID) {
1103 default:
1104 llvm_unreachable("Unexpected intrinsic id");
1105 case Intrinsic::umin:
1106 return Intrinsic::vector_reduce_umin;
1107 case Intrinsic::umax:
1108 return Intrinsic::vector_reduce_umax;
1109 case Intrinsic::smin:
1110 return Intrinsic::vector_reduce_smin;
1111 case Intrinsic::smax:
1112 return Intrinsic::vector_reduce_smax;
1113 }
1114}
1115
1116// This is the inverse to getReductionForBinop
1118 switch (RdxID) {
1119 case Intrinsic::vector_reduce_fadd:
1120 return Instruction::FAdd;
1121 case Intrinsic::vector_reduce_fmul:
1122 return Instruction::FMul;
1123 case Intrinsic::vector_reduce_add:
1124 return Instruction::Add;
1125 case Intrinsic::vector_reduce_mul:
1126 return Instruction::Mul;
1127 case Intrinsic::vector_reduce_and:
1128 return Instruction::And;
1129 case Intrinsic::vector_reduce_or:
1130 return Instruction::Or;
1131 case Intrinsic::vector_reduce_xor:
1132 return Instruction::Xor;
1133 case Intrinsic::vector_reduce_smax:
1134 case Intrinsic::vector_reduce_smin:
1135 case Intrinsic::vector_reduce_umax:
1136 case Intrinsic::vector_reduce_umin:
1137 return Instruction::ICmp;
1138 case Intrinsic::vector_reduce_fmax:
1139 case Intrinsic::vector_reduce_fmin:
1140 return Instruction::FCmp;
1141 default:
1142 llvm_unreachable("Unexpected ID");
1143 }
1144}
1145
1146// This is the inverse to getArithmeticReductionInstruction
1148 switch (Opc) {
1149 default:
1150 break;
1151 case Instruction::Add:
1152 return Intrinsic::vector_reduce_add;
1153 case Instruction::Mul:
1154 return Intrinsic::vector_reduce_mul;
1155 case Instruction::And:
1156 return Intrinsic::vector_reduce_and;
1157 case Instruction::Or:
1158 return Intrinsic::vector_reduce_or;
1159 case Instruction::Xor:
1160 return Intrinsic::vector_reduce_xor;
1161 }
1163}
1164
1166 switch (RdxID) {
1167 default:
1168 llvm_unreachable("Unknown min/max recurrence kind");
1169 case Intrinsic::vector_reduce_umin:
1170 return Intrinsic::umin;
1171 case Intrinsic::vector_reduce_umax:
1172 return Intrinsic::umax;
1173 case Intrinsic::vector_reduce_smin:
1174 return Intrinsic::smin;
1175 case Intrinsic::vector_reduce_smax:
1176 return Intrinsic::smax;
1177 case Intrinsic::vector_reduce_fmin:
1178 return Intrinsic::minnum;
1179 case Intrinsic::vector_reduce_fmax:
1180 return Intrinsic::maxnum;
1181 case Intrinsic::vector_reduce_fminimum:
1182 return Intrinsic::minimum;
1183 case Intrinsic::vector_reduce_fmaximum:
1184 return Intrinsic::maximum;
1185 }
1186}
1187
1189 switch (RK) {
1190 default:
1191 llvm_unreachable("Unknown min/max recurrence kind");
1192 case RecurKind::UMin:
1193 return Intrinsic::umin;
1194 case RecurKind::UMax:
1195 return Intrinsic::umax;
1196 case RecurKind::SMin:
1197 return Intrinsic::smin;
1198 case RecurKind::SMax:
1199 return Intrinsic::smax;
1200 case RecurKind::FMin:
1201 case RecurKind::FMinNum:
1202 return Intrinsic::minnum;
1203 case RecurKind::FMax:
1204 case RecurKind::FMaxNum:
1205 return Intrinsic::maxnum;
1207 return Intrinsic::minimum;
1209 return Intrinsic::maximum;
1211 return Intrinsic::minimumnum;
1213 return Intrinsic::maximumnum;
1214 }
1215}
1216
1218 switch (RdxID) {
1219 case Intrinsic::vector_reduce_smax:
1220 return RecurKind::SMax;
1221 case Intrinsic::vector_reduce_smin:
1222 return RecurKind::SMin;
1223 case Intrinsic::vector_reduce_umax:
1224 return RecurKind::UMax;
1225 case Intrinsic::vector_reduce_umin:
1226 return RecurKind::UMin;
1227 case Intrinsic::vector_reduce_fmax:
1228 return RecurKind::FMax;
1229 case Intrinsic::vector_reduce_fmin:
1230 return RecurKind::FMin;
1231 default:
1232 return RecurKind::None;
1233 }
1234}
1235
1237 switch (RK) {
1238 default:
1239 llvm_unreachable("Unknown min/max recurrence kind");
1240 case RecurKind::UMin:
1241 return CmpInst::ICMP_ULT;
1242 case RecurKind::UMax:
1243 return CmpInst::ICMP_UGT;
1244 case RecurKind::SMin:
1245 return CmpInst::ICMP_SLT;
1246 case RecurKind::SMax:
1247 return CmpInst::ICMP_SGT;
1248 case RecurKind::FMin:
1249 return CmpInst::FCMP_OLT;
1250 case RecurKind::FMax:
1251 return CmpInst::FCMP_OGT;
1252 // We do not add FMinimum/FMaximum recurrence kind here since there is no
1253 // equivalent predicate which compares signed zeroes according to the
1254 // semantics of the intrinsics (llvm.minimum/maximum).
1255 }
1256}
1257
1259 Value *Right) {
1260 Type *Ty = Left->getType();
1261 if (Ty->isIntOrIntVectorTy() ||
1262 (RK == RecurKind::FMinNum || RK == RecurKind::FMaxNum ||
1266 return Builder.CreateIntrinsic(Ty, Id, {Left, Right}, nullptr,
1267 "rdx.minmax");
1268 }
1270 Value *Cmp = Builder.CreateCmp(Pred, Left, Right, "rdx.minmax.cmp");
1271 Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
1272 return Select;
1273}
1274
1275// Helper to generate an ordered reduction.
1277 unsigned Op, RecurKind RdxKind) {
1278 unsigned VF = cast<FixedVectorType>(Src->getType())->getNumElements();
1279
1280 // Extract and apply reduction ops in ascending order:
1281 // e.g. ((((Acc + Scl[0]) + Scl[1]) + Scl[2]) + ) ... + Scl[VF-1]
1282 Value *Result = Acc;
1283 for (unsigned ExtractIdx = 0; ExtractIdx != VF; ++ExtractIdx) {
1284 Value *Ext =
1285 Builder.CreateExtractElement(Src, Builder.getInt32(ExtractIdx));
1286
1287 if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
1288 Result = Builder.CreateBinOp((Instruction::BinaryOps)Op, Result, Ext,
1289 "bin.rdx");
1290 } else {
1292 "Invalid min/max");
1293 Result = createMinMaxOp(Builder, RdxKind, Result, Ext);
1294 }
1295 }
1296
1297 return Result;
1298}
1299
1300// Helper to generate a log2 shuffle reduction.
1302 unsigned Op,
1304 RecurKind RdxKind) {
1305 unsigned VF = cast<FixedVectorType>(Src->getType())->getNumElements();
1306 // VF is a power of 2 so we can emit the reduction using log2(VF) shuffles
1307 // and vector ops, reducing the set of values being computed by half each
1308 // round.
1309 assert(isPowerOf2_32(VF) &&
1310 "Reduction emission only supported for pow2 vectors!");
1311 // Note: fast-math-flags flags are controlled by the builder configuration
1312 // and are assumed to apply to all generated arithmetic instructions. Other
1313 // poison generating flags (nsw/nuw/inbounds/inrange/exact) are not part
1314 // of the builder configuration, and since they're not passed explicitly,
1315 // will never be relevant here. Note that it would be generally unsound to
1316 // propagate these from an intrinsic call to the expansion anyways as we/
1317 // change the order of operations.
1318 auto BuildShuffledOp = [&Builder, &Op,
1319 &RdxKind](SmallVectorImpl<int> &ShuffleMask,
1320 Value *&TmpVec) -> void {
1321 Value *Shuf = Builder.CreateShuffleVector(TmpVec, ShuffleMask, "rdx.shuf");
1322 if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
1323 TmpVec = Builder.CreateBinOp((Instruction::BinaryOps)Op, TmpVec, Shuf,
1324 "bin.rdx");
1325 } else {
1327 "Invalid min/max");
1328 TmpVec = createMinMaxOp(Builder, RdxKind, TmpVec, Shuf);
1329 }
1330 };
1331
1332 Value *TmpVec = Src;
1334 SmallVector<int, 32> ShuffleMask(VF);
1335 for (unsigned stride = 1; stride < VF; stride <<= 1) {
1336 // Initialise the mask with undef.
1337 llvm::fill(ShuffleMask, -1);
1338 for (unsigned j = 0; j < VF; j += stride << 1) {
1339 ShuffleMask[j] = j + stride;
1340 }
1341 BuildShuffledOp(ShuffleMask, TmpVec);
1342 }
1343 } else {
1344 SmallVector<int, 32> ShuffleMask(VF);
1345 for (unsigned i = VF; i != 1; i >>= 1) {
1346 // Move the upper half of the vector to the lower half.
1347 for (unsigned j = 0; j != i / 2; ++j)
1348 ShuffleMask[j] = i / 2 + j;
1349
1350 // Fill the rest of the mask with undef.
1351 std::fill(&ShuffleMask[i / 2], ShuffleMask.end(), -1);
1352 BuildShuffledOp(ShuffleMask, TmpVec);
1353 }
1354 }
1355 // The result is in the first element of the vector.
1356 return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0));
1357}
1358
1360 Value *InitVal, PHINode *OrigPhi) {
1361 Value *NewVal = nullptr;
1362
1363 // First use the original phi to determine the new value we're trying to
1364 // select from in the loop.
1365 SelectInst *SI = nullptr;
1366 for (auto *U : OrigPhi->users()) {
1367 if ((SI = dyn_cast<SelectInst>(U)))
1368 break;
1369 }
1370 assert(SI && "One user of the original phi should be a select");
1371
1372 if (SI->getTrueValue() == OrigPhi)
1373 NewVal = SI->getFalseValue();
1374 else {
1375 assert(SI->getFalseValue() == OrigPhi &&
1376 "At least one input to the select should be the original Phi");
1377 NewVal = SI->getTrueValue();
1378 }
1379
1380 // If any predicate is true it means that we want to select the new value.
1381 Value *AnyOf =
1382 Src->getType()->isVectorTy() ? Builder.CreateOrReduce(Src) : Src;
1383 // The compares in the loop may yield poison, which propagates through the
1384 // bitwise ORs. Freeze it here before the condition is used.
1385 AnyOf = Builder.CreateFreeze(AnyOf);
1386 return Builder.CreateSelect(AnyOf, NewVal, InitVal, "rdx.select");
1387}
1388
1390 FastMathFlags Flags) {
1391 bool Negative = false;
1392 switch (RdxID) {
1393 default:
1394 llvm_unreachable("Expecting a reduction intrinsic");
1395 case Intrinsic::vector_reduce_add:
1396 case Intrinsic::vector_reduce_mul:
1397 case Intrinsic::vector_reduce_or:
1398 case Intrinsic::vector_reduce_xor:
1399 case Intrinsic::vector_reduce_and:
1400 case Intrinsic::vector_reduce_fadd:
1401 case Intrinsic::vector_reduce_fmul: {
1402 unsigned Opc = getArithmeticReductionInstruction(RdxID);
1403 return ConstantExpr::getBinOpIdentity(Opc, Ty, false,
1404 Flags.noSignedZeros());
1405 }
1406 case Intrinsic::vector_reduce_umax:
1407 case Intrinsic::vector_reduce_umin:
1408 case Intrinsic::vector_reduce_smin:
1409 case Intrinsic::vector_reduce_smax: {
1411 return ConstantExpr::getIntrinsicIdentity(ScalarID, Ty);
1412 }
1413 case Intrinsic::vector_reduce_fmax:
1414 case Intrinsic::vector_reduce_fmaximum:
1415 Negative = true;
1416 [[fallthrough]];
1417 case Intrinsic::vector_reduce_fmin:
1418 case Intrinsic::vector_reduce_fminimum: {
1419 bool PropagatesNaN = RdxID == Intrinsic::vector_reduce_fminimum ||
1420 RdxID == Intrinsic::vector_reduce_fmaximum;
1421 const fltSemantics &Semantics = Ty->getFltSemantics();
1422 return (!Flags.noNaNs() && !PropagatesNaN)
1423 ? ConstantFP::getQNaN(Ty, Negative)
1424 : !Flags.noInfs()
1425 ? ConstantFP::getInfinity(Ty, Negative)
1426 : ConstantFP::get(Ty, APFloat::getLargest(Semantics, Negative));
1427 }
1428 }
1429}
1430
1432 assert((!(K == RecurKind::FMin || K == RecurKind::FMax) ||
1433 (FMF.noNaNs() && FMF.noSignedZeros())) &&
1434 "nnan, nsz is expected to be set for FP min/max reduction.");
1436 return getReductionIdentity(RdxID, Tp, FMF);
1437}
1438
1440 RecurKind RdxKind) {
1441 auto *SrcVecEltTy = cast<VectorType>(Src->getType())->getElementType();
1442 auto getIdentity = [&]() {
1443 return getRecurrenceIdentity(RdxKind, SrcVecEltTy,
1444 Builder.getFastMathFlags());
1445 };
1446 switch (RdxKind) {
1448 case RecurKind::Sub:
1449 case RecurKind::Add:
1450 case RecurKind::Mul:
1451 case RecurKind::And:
1452 case RecurKind::Or:
1453 case RecurKind::Xor:
1454 case RecurKind::SMax:
1455 case RecurKind::SMin:
1456 case RecurKind::UMax:
1457 case RecurKind::UMin:
1458 case RecurKind::FMax:
1459 case RecurKind::FMin:
1460 case RecurKind::FMinNum:
1461 case RecurKind::FMaxNum:
1466 return Builder.CreateUnaryIntrinsic(getReductionIntrinsicID(RdxKind), Src);
1467 case RecurKind::FMulAdd:
1468 case RecurKind::FAdd:
1469 return Builder.CreateFAddReduce(getIdentity(), Src);
1470 case RecurKind::FMul:
1471 return Builder.CreateFMulReduce(getIdentity(), Src);
1472 default:
1473 llvm_unreachable("Unhandled opcode");
1474 }
1475}
1476
1478 RecurKind Kind, Value *Mask, Value *EVL) {
1481 "AnyOf and FindIV reductions are not supported.");
1483 auto VPID = VPIntrinsic::getForIntrinsic(Id);
1485 "No VPIntrinsic for this reduction");
1486 auto *EltTy = cast<VectorType>(Src->getType())->getElementType();
1487 Value *Iden = getRecurrenceIdentity(Kind, EltTy, Builder.getFastMathFlags());
1488 Value *Ops[] = {Iden, Src, Mask, EVL};
1489 return Builder.CreateIntrinsic(EltTy, VPID, Ops);
1490}
1491
1493 Value *Src, Value *Start) {
1494 assert((Kind == RecurKind::FAdd || Kind == RecurKind::FMulAdd) &&
1495 "Unexpected reduction kind");
1496 assert(Src->getType()->isVectorTy() && "Expected a vector type");
1497 assert(!Start->getType()->isVectorTy() && "Expected a scalar type");
1498
1499 return B.CreateFAddReduce(Start, Src);
1500}
1501
1503 Value *Src, Value *Start, Value *Mask,
1504 Value *EVL) {
1505 assert((Kind == RecurKind::FAdd || Kind == RecurKind::FMulAdd) &&
1506 "Unexpected reduction kind");
1507 assert(Src->getType()->isVectorTy() && "Expected a vector type");
1508 assert(!Start->getType()->isVectorTy() && "Expected a scalar type");
1509
1511 auto VPID = VPIntrinsic::getForIntrinsic(Id);
1513 "No VPIntrinsic for this reduction");
1514 auto *EltTy = cast<VectorType>(Src->getType())->getElementType();
1515 Value *Ops[] = {Start, Src, Mask, EVL};
1516 return Builder.CreateIntrinsic(EltTy, VPID, Ops);
1517}
1518
1520 bool IncludeWrapFlags) {
1521 auto *VecOp = dyn_cast<Instruction>(I);
1522 if (!VecOp)
1523 return;
1524 auto *Intersection = (OpValue == nullptr) ? dyn_cast<Instruction>(VL[0])
1525 : dyn_cast<Instruction>(OpValue);
1526 if (!Intersection)
1527 return;
1528 const unsigned Opcode = Intersection->getOpcode();
1529 VecOp->copyIRFlags(Intersection, IncludeWrapFlags);
1530 for (auto *V : VL) {
1531 auto *Instr = dyn_cast<Instruction>(V);
1532 if (!Instr)
1533 continue;
1534 if (OpValue == nullptr || Opcode == Instr->getOpcode())
1535 VecOp->andIRFlags(V);
1536 }
1537}
1538
1539bool llvm::isKnownNegativeInLoop(const SCEV *S, const Loop *L,
1540 ScalarEvolution &SE) {
1541 const SCEV *Zero = SE.getZero(S->getType());
1542 return SE.isAvailableAtLoopEntry(S, L) &&
1544}
1545
1547 ScalarEvolution &SE) {
1548 const SCEV *Zero = SE.getZero(S->getType());
1549 return SE.isAvailableAtLoopEntry(S, L) &&
1551}
1552
1553bool llvm::isKnownPositiveInLoop(const SCEV *S, const Loop *L,
1554 ScalarEvolution &SE) {
1555 const SCEV *Zero = SE.getZero(S->getType());
1556 return SE.isAvailableAtLoopEntry(S, L) &&
1558}
1559
1561 ScalarEvolution &SE) {
1562 const SCEV *Zero = SE.getZero(S->getType());
1563 return SE.isAvailableAtLoopEntry(S, L) &&
1565}
1566
1568 bool Signed) {
1569 unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth();
1572 auto Predicate = Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1573 return SE.isAvailableAtLoopEntry(S, L) &&
1574 SE.isLoopEntryGuardedByCond(L, Predicate, S,
1575 SE.getConstant(Min));
1576}
1577
1579 bool Signed) {
1580 unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth();
1583 auto Predicate = Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1584 return SE.isAvailableAtLoopEntry(S, L) &&
1585 SE.isLoopEntryGuardedByCond(L, Predicate, S,
1586 SE.getConstant(Max));
1587}
1588
1589//===----------------------------------------------------------------------===//
1590// rewriteLoopExitValues - Optimize IV users outside the loop.
1591// As a side effect, reduces the amount of IV processing within the loop.
1592//===----------------------------------------------------------------------===//
1593
1594static bool hasHardUserWithinLoop(const Loop *L, const Instruction *I) {
1597 Visited.insert(I);
1598 WorkList.push_back(I);
1599 while (!WorkList.empty()) {
1600 const Instruction *Curr = WorkList.pop_back_val();
1601 // This use is outside the loop, nothing to do.
1602 if (!L->contains(Curr))
1603 continue;
1604 // Do we assume it is a "hard" use which will not be eliminated easily?
1605 if (Curr->mayHaveSideEffects())
1606 return true;
1607 // Otherwise, add all its users to worklist.
1608 for (const auto *U : Curr->users()) {
1609 auto *UI = cast<Instruction>(U);
1610 if (Visited.insert(UI).second)
1611 WorkList.push_back(UI);
1612 }
1613 }
1614 return false;
1615}
1616
1617// Collect information about PHI nodes which can be transformed in
1618// rewriteLoopExitValues.
1620 PHINode *PN; // For which PHI node is this replacement?
1621 unsigned Ith; // For which incoming value?
1622 const SCEV *ExpansionSCEV; // The SCEV of the incoming value we are rewriting.
1623 Instruction *ExpansionPoint; // Where we'd like to expand that SCEV?
1624 bool HighCost; // Is this expansion a high-cost?
1625
1626 RewritePhi(PHINode *P, unsigned I, const SCEV *Val, Instruction *ExpansionPt,
1627 bool H)
1628 : PN(P), Ith(I), ExpansionSCEV(Val), ExpansionPoint(ExpansionPt),
1629 HighCost(H) {}
1630};
1631
1632// Check whether it is possible to delete the loop after rewriting exit
1633// value. If it is possible, ignore ReplaceExitValue and do rewriting
1634// aggressively.
1635static bool canLoopBeDeleted(Loop *L, SmallVector<RewritePhi, 8> &RewritePhiSet) {
1636 BasicBlock *Preheader = L->getLoopPreheader();
1637 // If there is no preheader, the loop will not be deleted.
1638 if (!Preheader)
1639 return false;
1640
1641 // In LoopDeletion pass Loop can be deleted when ExitingBlocks.size() > 1.
1642 // We obviate multiple ExitingBlocks case for simplicity.
1643 // TODO: If we see testcase with multiple ExitingBlocks can be deleted
1644 // after exit value rewriting, we can enhance the logic here.
1645 SmallVector<BasicBlock *, 4> ExitingBlocks;
1646 L->getExitingBlocks(ExitingBlocks);
1648 L->getUniqueExitBlocks(ExitBlocks);
1649 if (ExitBlocks.size() != 1 || ExitingBlocks.size() != 1)
1650 return false;
1651
1652 BasicBlock *ExitBlock = ExitBlocks[0];
1653 BasicBlock::iterator BI = ExitBlock->begin();
1654 while (PHINode *P = dyn_cast<PHINode>(BI)) {
1655 Value *Incoming = P->getIncomingValueForBlock(ExitingBlocks[0]);
1656
1657 // If the Incoming value of P is found in RewritePhiSet, we know it
1658 // could be rewritten to use a loop invariant value in transformation
1659 // phase later. Skip it in the loop invariant check below.
1660 bool found = false;
1661 for (const RewritePhi &Phi : RewritePhiSet) {
1662 unsigned i = Phi.Ith;
1663 if (Phi.PN == P && (Phi.PN)->getIncomingValue(i) == Incoming) {
1664 found = true;
1665 break;
1666 }
1667 }
1668
1669 Instruction *I;
1670 if (!found && (I = dyn_cast<Instruction>(Incoming)))
1671 if (!L->hasLoopInvariantOperands(I))
1672 return false;
1673
1674 ++BI;
1675 }
1676
1677 for (auto *BB : L->blocks())
1678 if (llvm::any_of(*BB, [](Instruction &I) {
1679 return I.mayHaveSideEffects();
1680 }))
1681 return false;
1682
1683 return true;
1684}
1685
1686/// Checks if it is safe to call InductionDescriptor::isInductionPHI for \p Phi,
1687/// and returns true if this Phi is an induction phi in the loop. When
1688/// isInductionPHI returns true, \p ID will be also be set by isInductionPHI.
1689static bool checkIsIndPhi(PHINode *Phi, Loop *L, ScalarEvolution *SE,
1691 if (!Phi)
1692 return false;
1693 if (!L->getLoopPreheader())
1694 return false;
1695 if (Phi->getParent() != L->getHeader())
1696 return false;
1697 return InductionDescriptor::isInductionPHI(Phi, L, SE, ID);
1698}
1699
1701 ScalarEvolution *SE,
1702 const TargetTransformInfo *TTI,
1703 SCEVExpander &Rewriter, DominatorTree *DT,
1706 // Check a pre-condition.
1707 assert(L->isRecursivelyLCSSAForm(*DT, *LI) &&
1708 "Indvars did not preserve LCSSA!");
1709
1710 SmallVector<BasicBlock*, 8> ExitBlocks;
1711 L->getUniqueExitBlocks(ExitBlocks);
1712
1713 SmallVector<RewritePhi, 8> RewritePhiSet;
1714 // Find all values that are computed inside the loop, but used outside of it.
1715 // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan
1716 // the exit blocks of the loop to find them.
1717 for (BasicBlock *ExitBB : ExitBlocks) {
1718 // If there are no PHI nodes in this exit block, then no values defined
1719 // inside the loop are used on this path, skip it.
1720 PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
1721 if (!PN) continue;
1722
1723 unsigned NumPreds = PN->getNumIncomingValues();
1724
1725 // Iterate over all of the PHI nodes.
1726 BasicBlock::iterator BBI = ExitBB->begin();
1727 while ((PN = dyn_cast<PHINode>(BBI++))) {
1728 if (PN->use_empty())
1729 continue; // dead use, don't replace it
1730
1731 if (!SE->isSCEVable(PN->getType()))
1732 continue;
1733
1734 // Iterate over all of the values in all the PHI nodes.
1735 for (unsigned i = 0; i != NumPreds; ++i) {
1736 // If the value being merged in is not integer or is not defined
1737 // in the loop, skip it.
1738 Value *InVal = PN->getIncomingValue(i);
1739 if (!isa<Instruction>(InVal))
1740 continue;
1741
1742 // If this pred is for a subloop, not L itself, skip it.
1743 if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
1744 continue; // The Block is in a subloop, skip it.
1745
1746 // Check that InVal is defined in the loop.
1747 Instruction *Inst = cast<Instruction>(InVal);
1748 if (!L->contains(Inst))
1749 continue;
1750
1751 // Find exit values which are induction variables in the loop, and are
1752 // unused in the loop, with the only use being the exit block PhiNode,
1753 // and the induction variable update binary operator.
1754 // The exit value can be replaced with the final value when it is cheap
1755 // to do so.
1758 PHINode *IndPhi = dyn_cast<PHINode>(Inst);
1759 if (IndPhi) {
1760 if (!checkIsIndPhi(IndPhi, L, SE, ID))
1761 continue;
1762 // This is an induction PHI. Check that the only users are PHI
1763 // nodes, and induction variable update binary operators.
1764 if (llvm::any_of(Inst->users(), [&](User *U) {
1765 if (!isa<PHINode>(U) && !isa<BinaryOperator>(U))
1766 return true;
1767 BinaryOperator *B = dyn_cast<BinaryOperator>(U);
1768 if (B && B != ID.getInductionBinOp())
1769 return true;
1770 return false;
1771 }))
1772 continue;
1773 } else {
1774 // If it is not an induction phi, it must be an induction update
1775 // binary operator with an induction phi user.
1777 if (!B)
1778 continue;
1779 if (llvm::any_of(Inst->users(), [&](User *U) {
1780 PHINode *Phi = dyn_cast<PHINode>(U);
1781 if (Phi != PN && !checkIsIndPhi(Phi, L, SE, ID))
1782 return true;
1783 return false;
1784 }))
1785 continue;
1786 if (B != ID.getInductionBinOp())
1787 continue;
1788 }
1789 }
1790
1791 // Okay, this instruction has a user outside of the current loop
1792 // and varies predictably *inside* the loop. Evaluate the value it
1793 // contains when the loop exits, if possible. We prefer to start with
1794 // expressions which are true for all exits (so as to maximize
1795 // expression reuse by the SCEVExpander), but resort to per-exit
1796 // evaluation if that fails.
1797 const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
1798 if (isa<SCEVCouldNotCompute>(ExitValue) ||
1799 !SE->isLoopInvariant(ExitValue, L) ||
1800 !Rewriter.isSafeToExpand(ExitValue)) {
1801 // TODO: This should probably be sunk into SCEV in some way; maybe a
1802 // getSCEVForExit(SCEV*, L, ExitingBB)? It can be generalized for
1803 // most SCEV expressions and other recurrence types (e.g. shift
1804 // recurrences). Is there existing code we can reuse?
1805 const SCEV *ExitCount = SE->getExitCount(L, PN->getIncomingBlock(i));
1806 if (isa<SCEVCouldNotCompute>(ExitCount))
1807 continue;
1808 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Inst)))
1809 if (AddRec->getLoop() == L)
1810 ExitValue = AddRec->evaluateAtIteration(ExitCount, *SE);
1811 if (isa<SCEVCouldNotCompute>(ExitValue) ||
1812 !SE->isLoopInvariant(ExitValue, L) ||
1813 !Rewriter.isSafeToExpand(ExitValue))
1814 continue;
1815 }
1816
1817 // Computing the value outside of the loop brings no benefit if it is
1818 // definitely used inside the loop in a way which can not be optimized
1819 // away. Avoid doing so unless we know we have a value which computes
1820 // the ExitValue already. TODO: This should be merged into SCEV
1821 // expander to leverage its knowledge of existing expressions.
1822 if (ReplaceExitValue != AlwaysRepl && !isa<SCEVConstant>(ExitValue) &&
1823 !isa<SCEVUnknown>(ExitValue) && hasHardUserWithinLoop(L, Inst))
1824 continue;
1825
1826 // Check if expansions of this SCEV would count as being high cost.
1827 bool HighCost = Rewriter.isHighCostExpansion(
1828 ExitValue, L, SCEVCheapExpansionBudget, TTI, Inst);
1829
1830 // Note that we must not perform expansions until after
1831 // we query *all* the costs, because if we perform temporary expansion
1832 // inbetween, one that we might not intend to keep, said expansion
1833 // *may* affect cost calculation of the next SCEV's we'll query,
1834 // and next SCEV may errneously get smaller cost.
1835
1836 // Collect all the candidate PHINodes to be rewritten.
1837 Instruction *InsertPt =
1838 (isa<PHINode>(Inst) || isa<LandingPadInst>(Inst)) ?
1839 &*Inst->getParent()->getFirstInsertionPt() : Inst;
1840 RewritePhiSet.emplace_back(PN, i, ExitValue, InsertPt, HighCost);
1841 }
1842 }
1843 }
1844
1845 // TODO: evaluate whether it is beneficial to change how we calculate
1846 // high-cost: if we have SCEV 'A' which we know we will expand, should we
1847 // calculate the cost of other SCEV's after expanding SCEV 'A', thus
1848 // potentially giving cost bonus to those other SCEV's?
1849
1850 bool LoopCanBeDel = canLoopBeDeleted(L, RewritePhiSet);
1851 int NumReplaced = 0;
1852
1853 // Transformation.
1854 for (const RewritePhi &Phi : RewritePhiSet) {
1855 PHINode *PN = Phi.PN;
1856
1857 // Only do the rewrite when the ExitValue can be expanded cheaply.
1858 // If LoopCanBeDel is true, rewrite exit value aggressively.
1861 !LoopCanBeDel && Phi.HighCost)
1862 continue;
1863
1864 Value *ExitVal = Rewriter.expandCodeFor(
1865 Phi.ExpansionSCEV, Phi.PN->getType(), Phi.ExpansionPoint);
1866
1867 LLVM_DEBUG(dbgs() << "rewriteLoopExitValues: AfterLoopVal = " << *ExitVal
1868 << '\n'
1869 << " LoopVal = " << *(Phi.ExpansionPoint) << "\n");
1870
1871#ifndef NDEBUG
1872 // If we reuse an instruction from a loop which is neither L nor one of
1873 // its containing loops, we end up breaking LCSSA form for this loop by
1874 // creating a new use of its instruction.
1875 if (auto *ExitInsn = dyn_cast<Instruction>(ExitVal))
1876 if (auto *EVL = LI->getLoopFor(ExitInsn->getParent()))
1877 if (EVL != L)
1878 assert(EVL->contains(L) && "LCSSA breach detected!");
1879#endif
1880
1881 NumReplaced++;
1882 Instruction *Inst = cast<Instruction>(PN->getIncomingValue(Phi.Ith));
1883 PN->setIncomingValue(Phi.Ith, ExitVal);
1884 // It's necessary to tell ScalarEvolution about this explicitly so that
1885 // it can walk the def-use list and forget all SCEVs, as it may not be
1886 // watching the PHI itself. Once the new exit value is in place, there
1887 // may not be a def-use connection between the loop and every instruction
1888 // which got a SCEVAddRecExpr for that loop.
1889 SE->forgetValue(PN);
1890
1891 // If this instruction is dead now, delete it. Don't do it now to avoid
1892 // invalidating iterators.
1893 if (isInstructionTriviallyDead(Inst, TLI))
1894 DeadInsts.push_back(Inst);
1895
1896 // Replace PN with ExitVal if that is legal and does not break LCSSA.
1897 if (PN->getNumIncomingValues() == 1 &&
1898 LI->replacementPreservesLCSSAForm(PN, ExitVal)) {
1899 PN->replaceAllUsesWith(ExitVal);
1900 PN->eraseFromParent();
1901 }
1902 }
1903
1904 // The insertion point instruction may have been deleted; clear it out
1905 // so that the rewriter doesn't trip over it later.
1906 Rewriter.clearInsertPoint();
1907 return NumReplaced;
1908}
1909
1910/// Utility that implements appending of loops onto a worklist.
1911/// Loops are added in preorder (analogous for reverse postorder for trees),
1912/// and the worklist is processed LIFO.
1913template <typename RangeT>
1915 RangeT &&Loops, SmallPriorityWorklist<Loop *, 4> &Worklist) {
1916 // We use an internal worklist to build up the preorder traversal without
1917 // recursion.
1918 SmallVector<Loop *, 4> PreOrderLoops, PreOrderWorklist;
1919
1920 // We walk the initial sequence of loops in reverse because we generally want
1921 // to visit defs before uses and the worklist is LIFO.
1922 for (Loop *RootL : Loops) {
1923 assert(PreOrderLoops.empty() && "Must start with an empty preorder walk.");
1924 assert(PreOrderWorklist.empty() &&
1925 "Must start with an empty preorder walk worklist.");
1926 PreOrderWorklist.push_back(RootL);
1927 do {
1928 Loop *L = PreOrderWorklist.pop_back_val();
1929 PreOrderWorklist.append(L->begin(), L->end());
1930 PreOrderLoops.push_back(L);
1931 } while (!PreOrderWorklist.empty());
1932
1933 Worklist.insert(std::move(PreOrderLoops));
1934 PreOrderLoops.clear();
1935 }
1936}
1937
1938template <typename RangeT>
1942}
1943
1944template LLVM_EXPORT_TEMPLATE void
1947
1948template LLVM_EXPORT_TEMPLATE void
1951
1956
1958 LoopInfo *LI, LPPassManager *LPM) {
1959 Loop &New = *LI->AllocateLoop();
1960 if (PL)
1961 PL->addChildLoop(&New);
1962 else
1963 LI->addTopLevelLoop(&New);
1964
1965 if (LPM)
1966 LPM->addLoop(New);
1967
1968 // Add all of the blocks in L to the new loop.
1969 for (BasicBlock *BB : L->blocks())
1970 if (LI->getLoopFor(BB) == L)
1971 New.addBasicBlockToLoop(cast<BasicBlock>(VM[BB]), *LI);
1972
1973 // Add all of the subloops to the new loop.
1974 for (Loop *I : *L)
1975 cloneLoop(I, &New, VM, LI, LPM);
1976
1977 return &New;
1978}
1979
1980/// IR Values for the lower and upper bounds of a pointer evolution. We
1981/// need to use value-handles because SCEV expansion can invalidate previously
1982/// expanded values. Thus expansion of a pointer can invalidate the bounds for
1983/// a previous one.
1989
1990/// Expand code for the lower and upper bound of the pointer group \p CG
1991/// in \p TheLoop. \return the values for the bounds.
1993 Loop *TheLoop, Instruction *Loc,
1994 SCEVExpander &Exp, bool HoistRuntimeChecks) {
1995 LLVMContext &Ctx = Loc->getContext();
1996 Type *PtrArithTy = PointerType::get(Ctx, CG->AddressSpace);
1997
1998 Value *Start = nullptr, *End = nullptr;
1999 LLVM_DEBUG(dbgs() << "LAA: Adding RT check for range:\n");
2000 const SCEV *Low = CG->Low, *High = CG->High, *Stride = nullptr;
2001
2002 // If the Low and High values are themselves loop-variant, then we may want
2003 // to expand the range to include those covered by the outer loop as well.
2004 // There is a trade-off here with the advantage being that creating checks
2005 // using the expanded range permits the runtime memory checks to be hoisted
2006 // out of the outer loop. This reduces the cost of entering the inner loop,
2007 // which can be significant for low trip counts. The disadvantage is that
2008 // there is a chance we may now never enter the vectorized inner loop,
2009 // whereas using a restricted range check could have allowed us to enter at
2010 // least once. This is why the behaviour is not currently the default and is
2011 // controlled by the parameter 'HoistRuntimeChecks'.
2012 if (HoistRuntimeChecks && TheLoop->getParentLoop() &&
2014 auto *HighAR = cast<SCEVAddRecExpr>(High);
2015 auto *LowAR = cast<SCEVAddRecExpr>(Low);
2016 const Loop *OuterLoop = TheLoop->getParentLoop();
2017 ScalarEvolution &SE = *Exp.getSE();
2018 const SCEV *Recur = LowAR->getStepRecurrence(SE);
2019 if (Recur == HighAR->getStepRecurrence(SE) &&
2020 HighAR->getLoop() == OuterLoop && LowAR->getLoop() == OuterLoop) {
2021 BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
2022 const SCEV *OuterExitCount = SE.getExitCount(OuterLoop, OuterLoopLatch);
2023 if (!isa<SCEVCouldNotCompute>(OuterExitCount) &&
2024 OuterExitCount->getType()->isIntegerTy()) {
2025 const SCEV *NewHigh =
2026 cast<SCEVAddRecExpr>(High)->evaluateAtIteration(OuterExitCount, SE);
2027 if (!isa<SCEVCouldNotCompute>(NewHigh)) {
2028 LLVM_DEBUG(dbgs() << "LAA: Expanded RT check for range to include "
2029 "outer loop in order to permit hoisting\n");
2030 High = NewHigh;
2031 Low = cast<SCEVAddRecExpr>(Low)->getStart();
2032 // If there is a possibility that the stride is negative then we have
2033 // to generate extra checks to ensure the stride is positive.
2034 if (!SE.isKnownNonNegative(
2035 SE.applyLoopGuards(Recur, HighAR->getLoop()))) {
2036 Stride = Recur;
2037 LLVM_DEBUG(dbgs() << "LAA: ... but need to check stride is "
2038 "positive: "
2039 << *Stride << '\n');
2040 }
2041 }
2042 }
2043 }
2044 }
2045
2046 Start = Exp.expandCodeFor(Low, PtrArithTy, Loc);
2047 End = Exp.expandCodeFor(High, PtrArithTy, Loc);
2048 if (CG->NeedsFreeze) {
2049 IRBuilder<> Builder(Loc);
2050 Start = Builder.CreateFreeze(Start, Start->getName() + ".fr");
2051 End = Builder.CreateFreeze(End, End->getName() + ".fr");
2052 }
2053 Value *StrideVal =
2054 Stride ? Exp.expandCodeFor(Stride, Stride->getType(), Loc) : nullptr;
2055 LLVM_DEBUG(dbgs() << "Start: " << *Low << " End: " << *High << "\n");
2056 return {Start, End, StrideVal};
2057}
2058
2059/// Turns a collection of checks into a collection of expanded upper and
2060/// lower bounds for both pointers in the check.
2065
2066 // Here we're relying on the SCEV Expander's cache to only emit code for the
2067 // same bounds once.
2068 transform(PointerChecks, std::back_inserter(ChecksWithBounds),
2069 [&](const RuntimePointerCheck &Check) {
2070 PointerBounds First = expandBounds(Check.first, L, Loc, Exp,
2072 Second = expandBounds(Check.second, L, Loc, Exp,
2074 return std::make_pair(First, Second);
2075 });
2076
2077 return ChecksWithBounds;
2078}
2079
2081 Instruction *Loc, Loop *TheLoop,
2082 const SmallVectorImpl<RuntimePointerCheck> &PointerChecks,
2083 SCEVExpander &Exp, bool HoistRuntimeChecks) {
2084 // TODO: Move noalias annotation code from LoopVersioning here and share with LV if possible.
2085 // TODO: Pass RtPtrChecking instead of PointerChecks and SE separately, if possible
2086 auto ExpandedChecks =
2087 expandBounds(PointerChecks, TheLoop, Loc, Exp, HoistRuntimeChecks);
2088
2089 LLVMContext &Ctx = Loc->getContext();
2090 IRBuilder ChkBuilder(Ctx, InstSimplifyFolder(Loc->getDataLayout()));
2091 ChkBuilder.SetInsertPoint(Loc);
2092 // Our instructions might fold to a constant.
2093 Value *MemoryRuntimeCheck = nullptr;
2094
2095 for (const auto &[A, B] : ExpandedChecks) {
2096 // Check if two pointers (A and B) conflict where conflict is computed as:
2097 // start(A) <= end(B) && start(B) <= end(A)
2098
2099 assert((A.Start->getType()->getPointerAddressSpace() ==
2100 B.End->getType()->getPointerAddressSpace()) &&
2101 (B.Start->getType()->getPointerAddressSpace() ==
2102 A.End->getType()->getPointerAddressSpace()) &&
2103 "Trying to bounds check pointers with different address spaces");
2104
2105 // [A|B].Start points to the first accessed byte under base [A|B].
2106 // [A|B].End points to the last accessed byte, plus one.
2107 // There is no conflict when the intervals are disjoint:
2108 // NoConflict = (B.Start >= A.End) || (A.Start >= B.End)
2109 //
2110 // bound0 = (B.Start < A.End)
2111 // bound1 = (A.Start < B.End)
2112 // IsConflict = bound0 & bound1
2113 Value *Cmp0 = ChkBuilder.CreateICmpULT(A.Start, B.End, "bound0");
2114 Value *Cmp1 = ChkBuilder.CreateICmpULT(B.Start, A.End, "bound1");
2115 Value *IsConflict = ChkBuilder.CreateAnd(Cmp0, Cmp1, "found.conflict");
2116 if (A.StrideToCheck) {
2117 Value *IsNegativeStride = ChkBuilder.CreateICmpSLT(
2118 A.StrideToCheck, ConstantInt::get(A.StrideToCheck->getType(), 0),
2119 "stride.check");
2120 IsConflict = ChkBuilder.CreateOr(IsConflict, IsNegativeStride);
2121 }
2122 if (B.StrideToCheck) {
2123 Value *IsNegativeStride = ChkBuilder.CreateICmpSLT(
2124 B.StrideToCheck, ConstantInt::get(B.StrideToCheck->getType(), 0),
2125 "stride.check");
2126 IsConflict = ChkBuilder.CreateOr(IsConflict, IsNegativeStride);
2127 }
2128 if (MemoryRuntimeCheck) {
2129 IsConflict =
2130 ChkBuilder.CreateOr(MemoryRuntimeCheck, IsConflict, "conflict.rdx");
2131 }
2132 MemoryRuntimeCheck = IsConflict;
2133 }
2134
2135 Exp.eraseDeadInstructions(MemoryRuntimeCheck);
2136 return MemoryRuntimeCheck;
2137}
2138
2141 function_ref<Value *(IRBuilderBase &, unsigned)> GetVF, unsigned IC) {
2142
2143 LLVMContext &Ctx = Loc->getContext();
2144 IRBuilder ChkBuilder(Ctx, InstSimplifyFolder(Loc->getDataLayout()));
2145 ChkBuilder.SetInsertPoint(Loc);
2146 // Our instructions might fold to a constant.
2147 Value *MemoryRuntimeCheck = nullptr;
2148
2149 auto &SE = *Expander.getSE();
2150 // Map to keep track of created compares, The key is the pair of operands for
2151 // the compare, to allow detecting and re-using redundant compares.
2153 for (const auto &[SrcStart, SinkStart, AccessSize, NeedsFreeze] : Checks) {
2154 Type *Ty = SinkStart->getType();
2155 // Compute VF * IC * AccessSize.
2156 auto *VFTimesICTimesSize =
2157 ChkBuilder.CreateMul(GetVF(ChkBuilder, Ty->getScalarSizeInBits()),
2158 ConstantInt::get(Ty, IC * AccessSize));
2159 Value *Diff =
2160 Expander.expandCodeFor(SE.getMinusSCEV(SinkStart, SrcStart), Ty, Loc);
2161
2162 // Check if the same compare has already been created earlier. In that case,
2163 // there is no need to check it again.
2164 Value *IsConflict = SeenCompares.lookup({Diff, VFTimesICTimesSize});
2165 if (IsConflict)
2166 continue;
2167
2168 IsConflict =
2169 ChkBuilder.CreateICmpULT(Diff, VFTimesICTimesSize, "diff.check");
2170 SeenCompares.insert({{Diff, VFTimesICTimesSize}, IsConflict});
2171 if (NeedsFreeze)
2172 IsConflict =
2173 ChkBuilder.CreateFreeze(IsConflict, IsConflict->getName() + ".fr");
2174 if (MemoryRuntimeCheck) {
2175 IsConflict =
2176 ChkBuilder.CreateOr(MemoryRuntimeCheck, IsConflict, "conflict.rdx");
2177 }
2178 MemoryRuntimeCheck = IsConflict;
2179 }
2180
2181 Expander.eraseDeadInstructions(MemoryRuntimeCheck);
2182 return MemoryRuntimeCheck;
2183}
2184
2185std::optional<IVConditionInfo>
2187 const MemorySSA &MSSA, AAResults &AA) {
2188 auto *TI = dyn_cast<BranchInst>(L.getHeader()->getTerminator());
2189 if (!TI || !TI->isConditional())
2190 return {};
2191
2192 auto *CondI = dyn_cast<Instruction>(TI->getCondition());
2193 // The case with the condition outside the loop should already be handled
2194 // earlier.
2195 // Allow CmpInst and TruncInsts as they may be users of load instructions
2196 // and have potential for partial unswitching
2197 if (!CondI || !isa<CmpInst, TruncInst>(CondI) || !L.contains(CondI))
2198 return {};
2199
2200 SmallVector<Instruction *> InstToDuplicate;
2201 InstToDuplicate.push_back(CondI);
2202
2203 SmallVector<Value *, 4> WorkList;
2204 WorkList.append(CondI->op_begin(), CondI->op_end());
2205
2206 SmallVector<MemoryAccess *, 4> AccessesToCheck;
2207 SmallVector<MemoryLocation, 4> AccessedLocs;
2208 while (!WorkList.empty()) {
2210 if (!I || !L.contains(I))
2211 continue;
2212
2213 // TODO: support additional instructions.
2215 return {};
2216
2217 // Do not duplicate volatile and atomic loads.
2218 if (auto *LI = dyn_cast<LoadInst>(I))
2219 if (LI->isVolatile() || LI->isAtomic())
2220 return {};
2221
2222 InstToDuplicate.push_back(I);
2223 if (MemoryAccess *MA = MSSA.getMemoryAccess(I)) {
2224 if (auto *MemUse = dyn_cast_or_null<MemoryUse>(MA)) {
2225 // Queue the defining access to check for alias checks.
2226 AccessesToCheck.push_back(MemUse->getDefiningAccess());
2227 AccessedLocs.push_back(MemoryLocation::get(I));
2228 } else {
2229 // MemoryDefs may clobber the location or may be atomic memory
2230 // operations. Bail out.
2231 return {};
2232 }
2233 }
2234 WorkList.append(I->op_begin(), I->op_end());
2235 }
2236
2237 if (InstToDuplicate.empty())
2238 return {};
2239
2240 SmallVector<BasicBlock *, 4> ExitingBlocks;
2241 L.getExitingBlocks(ExitingBlocks);
2242 auto HasNoClobbersOnPath =
2243 [&L, &AA, &AccessedLocs, &ExitingBlocks, &InstToDuplicate,
2244 MSSAThreshold](BasicBlock *Succ, BasicBlock *Header,
2245 SmallVector<MemoryAccess *, 4> AccessesToCheck)
2246 -> std::optional<IVConditionInfo> {
2247 IVConditionInfo Info;
2248 // First, collect all blocks in the loop that are on a patch from Succ
2249 // to the header.
2251 WorkList.push_back(Succ);
2252 WorkList.push_back(Header);
2254 Seen.insert(Header);
2255 Info.PathIsNoop &=
2256 all_of(*Header, [](Instruction &I) { return !I.mayHaveSideEffects(); });
2257
2258 while (!WorkList.empty()) {
2259 BasicBlock *Current = WorkList.pop_back_val();
2260 if (!L.contains(Current))
2261 continue;
2262 const auto &SeenIns = Seen.insert(Current);
2263 if (!SeenIns.second)
2264 continue;
2265
2266 Info.PathIsNoop &= all_of(
2267 *Current, [](Instruction &I) { return !I.mayHaveSideEffects(); });
2268 WorkList.append(succ_begin(Current), succ_end(Current));
2269 }
2270
2271 // Require at least 2 blocks on a path through the loop. This skips
2272 // paths that directly exit the loop.
2273 if (Seen.size() < 2)
2274 return {};
2275
2276 // Next, check if there are any MemoryDefs that are on the path through
2277 // the loop (in the Seen set) and they may-alias any of the locations in
2278 // AccessedLocs. If that is the case, they may modify the condition and
2279 // partial unswitching is not possible.
2280 SmallPtrSet<MemoryAccess *, 4> SeenAccesses;
2281 while (!AccessesToCheck.empty()) {
2282 MemoryAccess *Current = AccessesToCheck.pop_back_val();
2283 auto SeenI = SeenAccesses.insert(Current);
2284 if (!SeenI.second || !Seen.contains(Current->getBlock()))
2285 continue;
2286
2287 // Bail out if exceeded the threshold.
2288 if (SeenAccesses.size() >= MSSAThreshold)
2289 return {};
2290
2291 // MemoryUse are read-only accesses.
2292 if (isa<MemoryUse>(Current))
2293 continue;
2294
2295 // For a MemoryDef, check if is aliases any of the location feeding
2296 // the original condition.
2297 if (auto *CurrentDef = dyn_cast<MemoryDef>(Current)) {
2298 if (any_of(AccessedLocs, [&AA, CurrentDef](MemoryLocation &Loc) {
2299 return isModSet(
2300 AA.getModRefInfo(CurrentDef->getMemoryInst(), Loc));
2301 }))
2302 return {};
2303 }
2304
2305 for (Use &U : Current->uses())
2306 AccessesToCheck.push_back(cast<MemoryAccess>(U.getUser()));
2307 }
2308
2309 // We could also allow loops with known trip counts without mustprogress,
2310 // but ScalarEvolution may not be available.
2311 Info.PathIsNoop &= isMustProgress(&L);
2312
2313 // If the path is considered a no-op so far, check if it reaches a
2314 // single exit block without any phis. This ensures no values from the
2315 // loop are used outside of the loop.
2316 if (Info.PathIsNoop) {
2317 for (auto *Exiting : ExitingBlocks) {
2318 if (!Seen.contains(Exiting))
2319 continue;
2320 for (auto *Succ : successors(Exiting)) {
2321 if (L.contains(Succ))
2322 continue;
2323
2324 Info.PathIsNoop &= Succ->phis().empty() &&
2325 (!Info.ExitForPath || Info.ExitForPath == Succ);
2326 if (!Info.PathIsNoop)
2327 break;
2328 assert((!Info.ExitForPath || Info.ExitForPath == Succ) &&
2329 "cannot have multiple exit blocks");
2330 Info.ExitForPath = Succ;
2331 }
2332 }
2333 }
2334 if (!Info.ExitForPath)
2335 Info.PathIsNoop = false;
2336
2337 Info.InstToDuplicate = InstToDuplicate;
2338 return Info;
2339 };
2340
2341 // If we branch to the same successor, partial unswitching will not be
2342 // beneficial.
2343 if (TI->getSuccessor(0) == TI->getSuccessor(1))
2344 return {};
2345
2346 if (auto Info = HasNoClobbersOnPath(TI->getSuccessor(0), L.getHeader(),
2347 AccessesToCheck)) {
2348 Info->KnownValue = ConstantInt::getTrue(TI->getContext());
2349 return Info;
2350 }
2351 if (auto Info = HasNoClobbersOnPath(TI->getSuccessor(1), L.getHeader(),
2352 AccessesToCheck)) {
2353 Info->KnownValue = ConstantInt::getFalse(TI->getContext());
2354 return Info;
2355 }
2356
2357 return {};
2358}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
This is the interface for LLVM's primary stateless and local alias analysis.
bbsections Prepares for basic block by splitting functions into clusters of basic blocks
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< 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.
static const 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 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 BranchInst * getExpectedExitLoopLatchBranch(Loop *L)
Checks if L has an exiting latch branch.
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 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
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
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:1120
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:459
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 if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
Conditional or Unconditional Branch instruction.
unsigned getNumSuccessors() const
BasicBlock * getSuccessor(unsigned i) const
static LLVM_ABI BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
static BranchProbability getUnknown()
uint32_t getNumerator() const
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
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:536
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:177
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()
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:321
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:164
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:22
bool noSignedZeros() const
Definition FMF.h:67
bool noNaNs() const
Definition FMF.h:65
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:2348
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition IRBuilder.h:2651
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:1551
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2364
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:1573
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1437
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2794
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 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
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:887
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:526
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
Definition LoopInfo.cpp:502
Metadata node.
Definition Metadata.h:1078
LLVM_ABI void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
ArrayRef< MDOperand > operands() const
Definition Metadata.h:1440
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1569
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1448
LLVMContext & getContext() const
Definition Metadata.h:1242
Tracking metadata reference owned by Metadata.
Definition Metadata.h:900
A single uniqued string.
Definition Metadata.h:721
LLVM_ABI StringRef getString() const
Definition Metadata.cpp:618
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:608
Tuple of metadata.
Definition Metadata.h:1497
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:993
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 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 isFindIVRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
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:42
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(const SCEV *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 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:261
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:45
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
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:256
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
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:322
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:682
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
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....
@ 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:1763
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:1757
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:1737
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.
std::pair< const RuntimeCheckingPtrGroup *, const RuntimeCheckingPtrGroup * > RuntimePointerCheck
A memcheck which made up of a pair of grouped pointers.
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)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
void appendReversedLoopsToWorklist(RangeT &&, SmallPriorityWorklist< Loop *, 4 > &)
Utility that implements appending of loops onto a worklist given a range.
auto successors(const MachineBasicBlock *BB)
BranchProbability getBranchProbability(BranchInst *B, bool ForFirstTarget)
Based on branch weight metadata, return either:
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:632
LLVM_ABI char & LCSSAID
Definition LCSSA.cpp:526
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)
bool setBranchProbability(BranchInst *B, BranchProbability P, bool ForFirstTarget)
Set branch weight metadata for B to indicate that P and 1 - P are the probabilities of control flowin...
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:2016
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:1744
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:402
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:406
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
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:2513
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:284
@ TM_Unspecified
The pass can use heuristics to determine whether a transformation should be applied.
Definition LoopUtils.h:287
@ TM_SuppressedByUser
The transformation must not be applied.
Definition LoopUtils.h:307
@ TM_ForcedByUser
The transformation was directed by the user, e.g.
Definition LoopUtils.h:301
@ TM_Disable
The transformation should not be applied.
Definition LoopUtils.h:293
@ TM_Enable
The transformation should be applied without considering a cost model.
Definition LoopUtils.h:290
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 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:58
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 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:548
@ UnusedIndVarInLoop
Definition LoopUtils.h:552
@ OnlyCheapRepl
Definition LoopUtils.h:550
@ AlwaysRepl
Definition LoopUtils.h:553
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:620
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
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.