Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.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
13#include "llvm/Transforms/Utils/LoopUtils.h"
14#include "llvm/ADT/DenseSet.h"
15#include "llvm/ADT/PriorityWorklist.h"
16#include "llvm/ADT/ScopeExit.h"
17#include "llvm/ADT/SetVector.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/Analysis/AliasAnalysis.h"
21#include "llvm/Analysis/BasicAliasAnalysis.h"
22#include "llvm/Analysis/DomTreeUpdater.h"
23#include "llvm/Analysis/GlobalsModRef.h"
24#include "llvm/Analysis/InstSimplifyFolder.h"
25#include "llvm/Analysis/LoopAccessAnalysis.h"
26#include "llvm/Analysis/LoopInfo.h"
27#include "llvm/Analysis/LoopPass.h"
28#include "llvm/Analysis/MemorySSA.h"
29#include "llvm/Analysis/MemorySSAUpdater.h"
30#include "llvm/Analysis/ScalarEvolution.h"
31#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
32#include "llvm/Analysis/ScalarEvolutionExpressions.h"
33#include "llvm/IR/DIBuilder.h"
34#include "llvm/IR/Dominators.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/IntrinsicInst.h"
37#include "llvm/IR/MDBuilder.h"
38#include "llvm/IR/Module.h"
39#include "llvm/IR/PatternMatch.h"
40#include "llvm/IR/ProfDataUtils.h"
41#include "llvm/IR/ValueHandle.h"
42#include "llvm/InitializePasses.h"
43#include "llvm/Pass.h"
44#include "llvm/Support/Debug.h"
45#include "llvm/Transforms/Utils/BasicBlockUtils.h"
46#include "llvm/Transforms/Utils/Local.h"
47#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
48
49using namespacellvm;
50using namespacellvm::PatternMatch;
51
52#define DEBUG_TYPE "loop-utils"
53
54staticconstchar *LLVMLoopDisableNonforced ="llvm.loop.disable_nonforced";
55staticconstchar *LLVMLoopDisableLICM ="llvm.licm.disable";
56
57boolllvm::formDedicatedExitBlocks(Loop *L,DominatorTree *DT,LoopInfo *LI,
58MemorySSAUpdater *MSSAU,
59bool PreserveLCSSA) {
60bool Changed =false;
61
62// We re-use a vector for the in-loop predecesosrs.
63SmallVector<BasicBlock *, 4> InLoopPredecessors;
64
65auto RewriteExit = [&](BasicBlock *BB) {
66assert(InLoopPredecessors.empty() &&
67"Must start with an empty predecessors list!");
68autoCleanup =make_scope_exit([&] { InLoopPredecessors.clear(); });
69
70// See if there are any non-loop predecessors of this exit block and
71// keep track of the in-loop predecessors.
72bool IsDedicatedExit =true;
73for (auto *PredBB :predecessors(BB))
74if (L->contains(PredBB)) {
75if (isa<IndirectBrInst>(PredBB->getTerminator()))
76// We cannot rewrite exiting edges from an indirectbr.
77returnfalse;
78
79 InLoopPredecessors.push_back(PredBB);
80 }else {
81 IsDedicatedExit =false;
82 }
83
84assert(!InLoopPredecessors.empty() &&"Must have *some* loop predecessor!");
85
86// Nothing to do if this is already a dedicated exit.
87if (IsDedicatedExit)
88returnfalse;
89
90auto *NewExitBB =SplitBlockPredecessors(
91 BB, InLoopPredecessors,".loopexit", DT, LI, MSSAU, PreserveLCSSA);
92
93if (!NewExitBB)
94LLVM_DEBUG(
95dbgs() <<"WARNING: Can't create a dedicated exit block for loop: "
96 << *L <<"\n");
97else
98LLVM_DEBUG(dbgs() <<"LoopSimplify: Creating dedicated exit block "
99 << NewExitBB->getName() <<"\n");
100returntrue;
101 };
102
103// Walk the exit blocks directly rather than building up a data structure for
104// them, but only visit each one once.
105SmallPtrSet<BasicBlock *, 4> Visited;
106for (auto *BB : L->blocks())
107for (auto *SuccBB :successors(BB)) {
108// We're looking for exit blocks so skip in-loop successors.
109if (L->contains(SuccBB))
110continue;
111
112// Visit each exit block exactly once.
113if (!Visited.insert(SuccBB).second)
114continue;
115
116 Changed |= RewriteExit(SuccBB);
117 }
118
119return Changed;
120}
121
122/// Returns the instructions that use values defined in the loop.
123SmallVector<Instruction *, 8>llvm::findDefsUsedOutsideOfLoop(Loop *L) {
124SmallVector<Instruction *, 8> UsedOutside;
125
126for (auto *Block : L->getBlocks())
127// FIXME: I believe that this could use copy_if if the Inst reference could
128// be adapted into a pointer.
129for (auto &Inst : *Block) {
130autoUsers = Inst.users();
131if (any_of(Users, [&](User *U) {
132auto *Use = cast<Instruction>(U);
133return !L->contains(Use->getParent());
134 }))
135 UsedOutside.push_back(&Inst);
136 }
137
138return UsedOutside;
139}
140
141voidllvm::getLoopAnalysisUsage(AnalysisUsage &AU) {
142// By definition, all loop passes need the LoopInfo analysis and the
143// Dominator tree it depends on. Because they all participate in the loop
144// pass manager, they must also preserve these.
145 AU.addRequired<DominatorTreeWrapperPass>();
146 AU.addPreserved<DominatorTreeWrapperPass>();
147 AU.addRequired<LoopInfoWrapperPass>();
148 AU.addPreserved<LoopInfoWrapperPass>();
149
150// We must also preserve LoopSimplify and LCSSA. We locally access their IDs
151// here because users shouldn't directly get them from this header.
152externchar &LoopSimplifyID;
153externchar &LCSSAID;
154 AU.addRequiredID(LoopSimplifyID);
155 AU.addPreservedID(LoopSimplifyID);
156 AU.addRequiredID(LCSSAID);
157 AU.addPreservedID(LCSSAID);
158// This is used in the LPPassManager to perform LCSSA verification on passes
159// which preserve lcssa form
160 AU.addRequired<LCSSAVerificationPass>();
161 AU.addPreserved<LCSSAVerificationPass>();
162
163// Loop passes are designed to run inside of a loop pass manager which means
164// that any function analyses they require must be required by the first loop
165// pass in the manager (so that it is computed before the loop pass manager
166// runs) and preserved by all loop pasess in the manager. To make this
167// reasonably robust, the set needed for most loop passes is maintained here.
168// If your loop pass requires an analysis not listed here, you will need to
169// carefully audit the loop pass manager nesting structure that results.
170 AU.addRequired<AAResultsWrapperPass>();
171 AU.addPreserved<AAResultsWrapperPass>();
172 AU.addPreserved<BasicAAWrapperPass>();
173 AU.addPreserved<GlobalsAAWrapperPass>();
174 AU.addPreserved<SCEVAAWrapperPass>();
175 AU.addRequired<ScalarEvolutionWrapperPass>();
176 AU.addPreserved<ScalarEvolutionWrapperPass>();
177// FIXME: When all loop passes preserve MemorySSA, it can be required and
178// preserved here instead of the individual handling in each pass.
179}
180
181/// Manually defined generic "LoopPass" dependency initialization. This is used
182/// to initialize the exact set of passes from above in \c
183/// getLoopAnalysisUsage. It can be used within a loop pass's initialization
184/// with:
185///
186/// INITIALIZE_PASS_DEPENDENCY(LoopPass)
187///
188/// As-if "LoopPass" were a pass.
189voidllvm::initializeLoopPassPass(PassRegistry &Registry) {
190INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
191INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
192INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
193INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
194INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
195INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
196INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
197INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
198INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
199INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
200}
201
202/// Create MDNode for input string.
203staticMDNode *createStringMetadata(Loop *TheLoop,StringRefName,unsigned V) {
204LLVMContext &Context = TheLoop->getHeader()->getContext();
205Metadata *MDs[] = {
206MDString::get(Context,Name),
207ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), V))};
208returnMDNode::get(Context, MDs);
209}
210
211/// Set input string into loop metadata by keeping other values intact.
212/// If the string is already in loop metadata update value if it is
213/// different.
214voidllvm::addStringMetadataToLoop(Loop *TheLoop,constchar *StringMD,
215unsigned V) {
216SmallVector<Metadata *, 4> MDs(1);
217// If the loop already has metadata, retain it.
218MDNode *LoopID = TheLoop->getLoopID();
219if (LoopID) {
220for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
221MDNode *Node = cast<MDNode>(LoopID->getOperand(i));
222// If it is of form key = value, try to parse it.
223if (Node->getNumOperands() == 2) {
224MDString *S = dyn_cast<MDString>(Node->getOperand(0));
225if (S && S->getString() == StringMD) {
226ConstantInt *IntMD =
227 mdconst::extract_or_null<ConstantInt>(Node->getOperand(1));
228if (IntMD && IntMD->getSExtValue() == V)
229// It is already in place. Do nothing.
230return;
231// We need to update the value, so just skip it here and it will
232// be added after copying other existed nodes.
233continue;
234 }
235 }
236 MDs.push_back(Node);
237 }
238 }
239// Add new metadata.
240 MDs.push_back(createStringMetadata(TheLoop, StringMD, V));
241// Replace current metadata node with new one.
242LLVMContext &Context = TheLoop->getHeader()->getContext();
243MDNode *NewLoopID =MDNode::get(Context, MDs);
244// Set operand 0 to refer to the loop id itself.
245 NewLoopID->replaceOperandWith(0, NewLoopID);
246 TheLoop->setLoopID(NewLoopID);
247}
248
249std::optional<ElementCount>
250llvm::getOptionalElementCountLoopAttribute(constLoop *TheLoop) {
251 std::optional<int> Width =
252getOptionalIntLoopAttribute(TheLoop,"llvm.loop.vectorize.width");
253
254if (Width) {
255 std::optional<int> IsScalable =getOptionalIntLoopAttribute(
256 TheLoop,"llvm.loop.vectorize.scalable.enable");
257returnElementCount::get(*Width, IsScalable.value_or(false));
258 }
259
260return std::nullopt;
261}
262
263std::optional<MDNode *>llvm::makeFollowupLoopID(
264MDNode *OrigLoopID,ArrayRef<StringRef> FollowupOptions,
265constchar *InheritOptionsExceptPrefix,bool AlwaysNew) {
266if (!OrigLoopID) {
267if (AlwaysNew)
268returnnullptr;
269return std::nullopt;
270 }
271
272assert(OrigLoopID->getOperand(0) == OrigLoopID);
273
274bool InheritAllAttrs = !InheritOptionsExceptPrefix;
275bool InheritSomeAttrs =
276 InheritOptionsExceptPrefix && InheritOptionsExceptPrefix[0] !='\0';
277SmallVector<Metadata *, 8> MDs;
278 MDs.push_back(nullptr);
279
280bool Changed =false;
281if (InheritAllAttrs || InheritSomeAttrs) {
282for (constMDOperand &Existing :drop_begin(OrigLoopID->operands())) {
283MDNode *Op = cast<MDNode>(Existing.get());
284
285auto InheritThisAttribute = [InheritSomeAttrs,
286 InheritOptionsExceptPrefix](MDNode *Op) {
287if (!InheritSomeAttrs)
288returnfalse;
289
290// Skip malformatted attribute metadata nodes.
291if (Op->getNumOperands() == 0)
292returntrue;
293Metadata *NameMD =Op->getOperand(0).get();
294if (!isa<MDString>(NameMD))
295returntrue;
296StringRef AttrName = cast<MDString>(NameMD)->getString();
297
298// Do not inherit excluded attributes.
299return !AttrName.starts_with(InheritOptionsExceptPrefix);
300 };
301
302if (InheritThisAttribute(Op))
303 MDs.push_back(Op);
304else
305 Changed =true;
306 }
307 }else {
308// Modified if we dropped at least one attribute.
309 Changed = OrigLoopID->getNumOperands() > 1;
310 }
311
312bool HasAnyFollowup =false;
313for (StringRef OptionName : FollowupOptions) {
314MDNode *FollowupNode =findOptionMDForLoopID(OrigLoopID, OptionName);
315if (!FollowupNode)
316continue;
317
318 HasAnyFollowup =true;
319for (constMDOperand &Option :drop_begin(FollowupNode->operands())) {
320 MDs.push_back(Option.get());
321 Changed =true;
322 }
323 }
324
325// Attributes of the followup loop not specified explicity, so signal to the
326// transformation pass to add suitable attributes.
327if (!AlwaysNew && !HasAnyFollowup)
328return std::nullopt;
329
330// If no attributes were added or remove, the previous loop Id can be reused.
331if (!AlwaysNew && !Changed)
332return OrigLoopID;
333
334// No attributes is equivalent to having no !llvm.loop metadata at all.
335if (MDs.size() == 1)
336returnnullptr;
337
338// Build the new loop ID.
339MDTuple *FollowupLoopID =MDNode::get(OrigLoopID->getContext(), MDs);
340 FollowupLoopID->replaceOperandWith(0, FollowupLoopID);
341return FollowupLoopID;
342}
343
344boolllvm::hasDisableAllTransformsHint(constLoop *L) {
345returngetBooleanLoopAttribute(L,LLVMLoopDisableNonforced);
346}
347
348boolllvm::hasDisableLICMTransformsHint(constLoop *L) {
349returngetBooleanLoopAttribute(L,LLVMLoopDisableLICM);
350}
351
352TransformationModellvm::hasUnrollTransformation(constLoop *L) {
353if (getBooleanLoopAttribute(L,"llvm.loop.unroll.disable"))
354returnTM_SuppressedByUser;
355
356 std::optional<int> Count =
357getOptionalIntLoopAttribute(L,"llvm.loop.unroll.count");
358if (Count)
359return *Count == 1 ?TM_SuppressedByUser :TM_ForcedByUser;
360
361if (getBooleanLoopAttribute(L,"llvm.loop.unroll.enable"))
362returnTM_ForcedByUser;
363
364if (getBooleanLoopAttribute(L,"llvm.loop.unroll.full"))
365returnTM_ForcedByUser;
366
367if (hasDisableAllTransformsHint(L))
368returnTM_Disable;
369
370returnTM_Unspecified;
371}
372
373TransformationModellvm::hasUnrollAndJamTransformation(constLoop *L) {
374if (getBooleanLoopAttribute(L,"llvm.loop.unroll_and_jam.disable"))
375returnTM_SuppressedByUser;
376
377 std::optional<int> Count =
378getOptionalIntLoopAttribute(L,"llvm.loop.unroll_and_jam.count");
379if (Count)
380return *Count == 1 ?TM_SuppressedByUser :TM_ForcedByUser;
381
382if (getBooleanLoopAttribute(L,"llvm.loop.unroll_and_jam.enable"))
383returnTM_ForcedByUser;
384
385if (hasDisableAllTransformsHint(L))
386returnTM_Disable;
387
388returnTM_Unspecified;
389}
390
391TransformationModellvm::hasVectorizeTransformation(constLoop *L) {
392 std::optional<bool>Enable =
393getOptionalBoolLoopAttribute(L,"llvm.loop.vectorize.enable");
394
395if (Enable ==false)
396returnTM_SuppressedByUser;
397
398 std::optional<ElementCount> VectorizeWidth =
399getOptionalElementCountLoopAttribute(L);
400 std::optional<int> InterleaveCount =
401getOptionalIntLoopAttribute(L,"llvm.loop.interleave.count");
402
403// 'Forcing' vector width and interleave count to one effectively disables
404// this tranformation.
405if (Enable ==true && VectorizeWidth && VectorizeWidth->isScalar() &&
406 InterleaveCount == 1)
407returnTM_SuppressedByUser;
408
409if (getBooleanLoopAttribute(L,"llvm.loop.isvectorized"))
410returnTM_Disable;
411
412if (Enable ==true)
413returnTM_ForcedByUser;
414
415if ((VectorizeWidth && VectorizeWidth->isScalar()) && InterleaveCount == 1)
416returnTM_Disable;
417
418if ((VectorizeWidth && VectorizeWidth->isVector()) || InterleaveCount > 1)
419returnTM_Enable;
420
421if (hasDisableAllTransformsHint(L))
422returnTM_Disable;
423
424returnTM_Unspecified;
425}
426
427TransformationModellvm::hasDistributeTransformation(constLoop *L) {
428if (getBooleanLoopAttribute(L,"llvm.loop.distribute.enable"))
429returnTM_ForcedByUser;
430
431if (hasDisableAllTransformsHint(L))
432returnTM_Disable;
433
434returnTM_Unspecified;
435}
436
437TransformationModellvm::hasLICMVersioningTransformation(constLoop *L) {
438if (getBooleanLoopAttribute(L,"llvm.loop.licm_versioning.disable"))
439returnTM_SuppressedByUser;
440
441if (hasDisableAllTransformsHint(L))
442returnTM_Disable;
443
444returnTM_Unspecified;
445}
446
447/// Does a BFS from a given node to all of its children inside a given loop.
448/// The returned vector of basic blocks includes the starting point.
449SmallVector<BasicBlock *, 16>llvm::collectChildrenInLoop(DominatorTree *DT,
450DomTreeNode *N,
451constLoop *CurLoop) {
452SmallVector<BasicBlock *, 16> Worklist;
453auto AddRegionToWorklist = [&](DomTreeNode *DTN) {
454// Only include subregions in the top level loop.
455BasicBlock *BB = DTN->getBlock();
456if (CurLoop->contains(BB))
457 Worklist.push_back(DTN->getBlock());
458 };
459
460 AddRegionToWorklist(N);
461
462for (size_tI = 0;I < Worklist.size();I++) {
463for (DomTreeNode *Child : DT->getNode(Worklist[I])->children())
464 AddRegionToWorklist(Child);
465 }
466
467return Worklist;
468}
469
470boolllvm::isAlmostDeadIV(PHINode *PN,BasicBlock *LatchBlock,Value *Cond) {
471int LatchIdx = PN->getBasicBlockIndex(LatchBlock);
472assert(LatchIdx != -1 &&"LatchBlock is not a case in this PHINode");
473Value *IncV = PN->getIncomingValue(LatchIdx);
474
475for (User *U : PN->users())
476if (U !=Cond && U != IncV)returnfalse;
477
478for (User *U : IncV->users())
479if (U !=Cond && U != PN)returnfalse;
480returntrue;
481}
482
483
484voidllvm::deleteDeadLoop(Loop *L,DominatorTree *DT,ScalarEvolution *SE,
485LoopInfo *LI,MemorySSA *MSSA) {
486assert((!DT || L->isLCSSAForm(*DT)) &&"Expected LCSSA!");
487auto *Preheader = L->getLoopPreheader();
488assert(Preheader &&"Preheader should exist!");
489
490 std::unique_ptr<MemorySSAUpdater> MSSAU;
491if (MSSA)
492 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
493
494// Now that we know the removal is safe, remove the loop by changing the
495// branch from the preheader to go to the single exit block.
496//
497// Because we're deleting a large chunk of code at once, the sequence in which
498// we remove things is very important to avoid invalidation issues.
499
500// Tell ScalarEvolution that the loop is deleted. Do this before
501// deleting the loop so that ScalarEvolution can look at the loop
502// to determine what it needs to clean up.
503if (SE) {
504 SE->forgetLoop(L);
505 SE->forgetBlockAndLoopDispositions();
506 }
507
508Instruction *OldTerm = Preheader->getTerminator();
509assert(!OldTerm->mayHaveSideEffects() &&
510"Preheader must end with a side-effect-free terminator");
511assert(OldTerm->getNumSuccessors() == 1 &&
512"Preheader must have a single successor");
513// Connect the preheader to the exit block. Keep the old edge to the header
514// around to perform the dominator tree update in two separate steps
515// -- #1 insertion of the edge preheader -> exit and #2 deletion of the edge
516// preheader -> header.
517//
518//
519// 0. Preheader 1. Preheader 2. Preheader
520// | | | |
521// V | V |
522// Header <--\ | Header <--\ | Header <--\
523 // | | | | | | | | | | |
524// | V | | | V | | | V |
525// | Body --/ | | Body --/ | | Body --/
526// V V V V V
527// Exit Exit Exit
528//
529// By doing this is two separate steps we can perform the dominator tree
530// update without using the batch update API.
531//
532// Even when the loop is never executed, we cannot remove the edge from the
533// source block to the exit block. Consider the case where the unexecuted loop
534// branches back to an outer loop. If we deleted the loop and removed the edge
535// coming to this inner loop, this will break the outer loop structure (by
536// deleting the backedge of the outer loop). If the outer loop is indeed a
537// non-loop, it will be deleted in a future iteration of loop deletion pass.
538IRBuilder<> Builder(OldTerm);
539
540auto *ExitBlock = L->getUniqueExitBlock();
541DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
542if (ExitBlock) {
543assert(ExitBlock &&"Should have a unique exit block!");
544assert(L->hasDedicatedExits() &&"Loop should have dedicated exits!");
545
546 Builder.CreateCondBr(Builder.getFalse(), L->getHeader(), ExitBlock);
547// Remove the old branch. The conditional branch becomes a new terminator.
548 OldTerm->eraseFromParent();
549
550// Rewrite phis in the exit block to get their inputs from the Preheader
551// instead of the exiting block.
552for (PHINode &P : ExitBlock->phis()) {
553// Set the zero'th element of Phi to be from the preheader and remove all
554// other incoming values. Given the loop has dedicated exits, all other
555// incoming values must be from the exiting blocks.
556int PredIndex = 0;
557P.setIncomingBlock(PredIndex, Preheader);
558// Removes all incoming values from all other exiting blocks (including
559// duplicate values from an exiting block).
560// Nuke all entries except the zero'th entry which is the preheader entry.
561P.removeIncomingValueIf([](unsignedIdx) {returnIdx != 0; },
562/* DeletePHIIfEmpty */false);
563
564assert((P.getNumIncomingValues() == 1 &&
565P.getIncomingBlock(PredIndex) == Preheader) &&
566"Should have exactly one value and that's from the preheader!");
567 }
568
569if (DT) {
570 DTU.applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}});
571if (MSSA) {
572 MSSAU->applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}},
573 *DT);
574if (VerifyMemorySSA)
575 MSSA->verifyMemorySSA();
576 }
577 }
578
579// Disconnect the loop body by branching directly to its exit.
580 Builder.SetInsertPoint(Preheader->getTerminator());
581 Builder.CreateBr(ExitBlock);
582// Remove the old branch.
583 Preheader->getTerminator()->eraseFromParent();
584 }else {
585assert(L->hasNoExitBlocks() &&
586"Loop should have either zero or one exit blocks.");
587
588 Builder.SetInsertPoint(OldTerm);
589 Builder.CreateUnreachable();
590 Preheader->getTerminator()->eraseFromParent();
591 }
592
593if (DT) {
594 DTU.applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}});
595if (MSSA) {
596 MSSAU->applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}},
597 *DT);
598SmallSetVector<BasicBlock *, 8> DeadBlockSet(L->block_begin(),
599 L->block_end());
600 MSSAU->removeBlocks(DeadBlockSet);
601if (VerifyMemorySSA)
602 MSSA->verifyMemorySSA();
603 }
604 }
605
606// Use a map to unique and a vector to guarantee deterministic ordering.
607llvm::SmallDenseSet<DebugVariable, 4> DeadDebugSet;
608llvm::SmallVector<DbgVariableIntrinsic *, 4> DeadDebugInst;
609llvm::SmallVector<DbgVariableRecord *, 4> DeadDbgVariableRecords;
610
611if (ExitBlock) {
612// Given LCSSA form is satisfied, we should not have users of instructions
613// within the dead loop outside of the loop. However, LCSSA doesn't take
614// unreachable uses into account. We handle them here.
615// We could do it after drop all references (in this case all users in the
616// loop will be already eliminated and we have less work to do but according
617// to API doc of User::dropAllReferences only valid operation after dropping
618// references, is deletion. So let's substitute all usages of
619// instruction from the loop with poison value of corresponding type first.
620for (auto *Block : L->blocks())
621for (Instruction &I : *Block) {
622auto *Poison =PoisonValue::get(I.getType());
623for (Use &U :llvm::make_early_inc_range(I.uses())) {
624if (auto *Usr = dyn_cast<Instruction>(U.getUser()))
625if (L->contains(Usr->getParent()))
626continue;
627// If we have a DT then we can check that uses outside a loop only in
628// unreachable block.
629if (DT)
630assert(!DT->isReachableFromEntry(U) &&
631"Unexpected user in reachable block");
632 U.set(Poison);
633 }
634
635// RemoveDIs: do the same as below for DbgVariableRecords.
636if (Block->IsNewDbgInfoFormat) {
637for (DbgVariableRecord &DVR :llvm::make_early_inc_range(
638filterDbgVars(I.getDbgRecordRange()))) {
639DebugVariable Key(DVR.getVariable(), DVR.getExpression(),
640 DVR.getDebugLoc().get());
641if (!DeadDebugSet.insert(Key).second)
642continue;
643// Unlinks the DVR from it's container, for later insertion.
644 DVR.removeFromParent();
645 DeadDbgVariableRecords.push_back(&DVR);
646 }
647 }
648
649// For one of each variable encountered, preserve a debug intrinsic (set
650// to Poison) and transfer it to the loop exit. This terminates any
651// variable locations that were set during the loop.
652auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I);
653if (!DVI)
654continue;
655if (!DeadDebugSet.insert(DebugVariable(DVI)).second)
656continue;
657 DeadDebugInst.push_back(DVI);
658 }
659
660// After the loop has been deleted all the values defined and modified
661// inside the loop are going to be unavailable. Values computed in the
662// loop will have been deleted, automatically causing their debug uses
663// be be replaced with undef. Loop invariant values will still be available.
664// Move dbg.values out the loop so that earlier location ranges are still
665// terminated and loop invariant assignments are preserved.
666DIBuilder DIB(*ExitBlock->getModule());
667BasicBlock::iterator InsertDbgValueBefore =
668 ExitBlock->getFirstInsertionPt();
669assert(InsertDbgValueBefore != ExitBlock->end() &&
670"There should be a non-PHI instruction in exit block, else these "
671"instructions will have no parent.");
672
673for (auto *DVI : DeadDebugInst)
674 DVI->moveBefore(*ExitBlock, InsertDbgValueBefore);
675
676// Due to the "head" bit in BasicBlock::iterator, we're going to insert
677// each DbgVariableRecord right at the start of the block, wheras dbg.values
678// would be repeatedly inserted before the first instruction. To replicate
679// this behaviour, do it backwards.
680for (DbgVariableRecord *DVR :llvm::reverse(DeadDbgVariableRecords))
681 ExitBlock->insertDbgRecordBefore(DVR, InsertDbgValueBefore);
682 }
683
684// Remove the block from the reference counting scheme, so that we can
685// delete it freely later.
686for (auto *Block : L->blocks())
687Block->dropAllReferences();
688
689if (MSSA &&VerifyMemorySSA)
690 MSSA->verifyMemorySSA();
691
692if (LI) {
693// Erase the instructions and the blocks without having to worry
694// about ordering because we already dropped the references.
695// NOTE: This iteration is safe because erasing the block does not remove
696// its entry from the loop's block list. We do that in the next section.
697for (BasicBlock *BB : L->blocks())
698 BB->eraseFromParent();
699
700// Finally, the blocks from loopinfo. This has to happen late because
701// otherwise our loop iterators won't work.
702
703SmallPtrSet<BasicBlock *, 8>blocks;
704blocks.insert(L->block_begin(), L->block_end());
705for (BasicBlock *BB :blocks)
706 LI->removeBlock(BB);
707
708// The last step is to update LoopInfo now that we've eliminated this loop.
709// Note: LoopInfo::erase remove the given loop and relink its subloops with
710// its parent. While removeLoop/removeChildLoop remove the given loop but
711// not relink its subloops, which is what we want.
712if (Loop *ParentLoop = L->getParentLoop()) {
713Loop::iteratorI =find(*ParentLoop, L);
714assert(I != ParentLoop->end() &&"Couldn't find loop");
715 ParentLoop->removeChildLoop(I);
716 }else {
717Loop::iteratorI =find(*LI, L);
718assert(I != LI->end() &&"Couldn't find loop");
719 LI->removeLoop(I);
720 }
721 LI->destroy(L);
722 }
723}
724
725voidllvm::breakLoopBackedge(Loop *L,DominatorTree &DT,ScalarEvolution &SE,
726LoopInfo &LI,MemorySSA *MSSA) {
727auto *Latch = L->getLoopLatch();
728assert(Latch &&"multiple latches not yet supported");
729auto *Header = L->getHeader();
730Loop *OutermostLoop = L->getOutermostLoop();
731
732 SE.forgetLoop(L);
733 SE.forgetBlockAndLoopDispositions();
734
735 std::unique_ptr<MemorySSAUpdater> MSSAU;
736if (MSSA)
737 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
738
739// Update the CFG and domtree. We chose to special case a couple of
740// of common cases for code quality and test readability reasons.
741 [&]() ->void {
742if (auto *BI = dyn_cast<BranchInst>(Latch->getTerminator())) {
743if (!BI->isConditional()) {
744DomTreeUpdater DTU(&DT, DomTreeUpdater::UpdateStrategy::Eager);
745 (void)changeToUnreachable(BI,/*PreserveLCSSA*/true, &DTU,
746 MSSAU.get());
747return;
748 }
749
750// Conditional latch/exit - note that latch can be shared by inner
751// and outer loop so the other target doesn't need to an exit
752if (L->isLoopExiting(Latch)) {
753// TODO: Generalize ConstantFoldTerminator so that it can be used
754// here without invalidating LCSSA or MemorySSA. (Tricky case for
755// LCSSA: header is an exit block of a preceeding sibling loop w/o
756// dedicated exits.)
757constunsigned ExitIdx = L->contains(BI->getSuccessor(0)) ? 1 : 0;
758BasicBlock *ExitBB = BI->getSuccessor(ExitIdx);
759
760DomTreeUpdater DTU(&DT, DomTreeUpdater::UpdateStrategy::Eager);
761 Header->removePredecessor(Latch,true);
762
763IRBuilder<> Builder(BI);
764auto *NewBI = Builder.CreateBr(ExitBB);
765// Transfer the metadata to the new branch instruction (minus the
766// loop info since this is no longer a loop)
767 NewBI->copyMetadata(*BI, {LLVMContext::MD_dbg,
768 LLVMContext::MD_annotation});
769
770 BI->eraseFromParent();
771 DTU.applyUpdates({{DominatorTree::Delete, Latch, Header}});
772if (MSSA)
773 MSSAU->applyUpdates({{DominatorTree::Delete, Latch, Header}}, DT);
774return;
775 }
776 }
777
778// General case. By splitting the backedge, and then explicitly making it
779// unreachable we gracefully handle corner cases such as switch and invoke
780// termiantors.
781auto *BackedgeBB =SplitEdge(Latch, Header, &DT, &LI, MSSAU.get());
782
783DomTreeUpdater DTU(&DT, DomTreeUpdater::UpdateStrategy::Eager);
784 (void)changeToUnreachable(BackedgeBB->getTerminator(),
785/*PreserveLCSSA*/true, &DTU, MSSAU.get());
786 }();
787
788// Erase (and destroy) this loop instance. Handles relinking sub-loops
789// and blocks within the loop as needed.
790 LI.erase(L);
791
792// If the loop we broke had a parent, then changeToUnreachable might have
793// caused a block to be removed from the parent loop (see loop_nest_lcssa
794// test case in zero-btc.ll for an example), thus changing the parent's
795// exit blocks. If that happened, we need to rebuild LCSSA on the outermost
796// loop which might have a had a block removed.
797if (OutermostLoop != L)
798formLCSSARecursively(*OutermostLoop, DT, &LI, &SE);
799}
800
801
802/// Checks if \p L has an exiting latch branch. There may also be other
803/// exiting blocks. Returns branch instruction terminating the loop
804/// latch if above check is successful, nullptr otherwise.
805staticBranchInst *getExpectedExitLoopLatchBranch(Loop *L) {
806BasicBlock *Latch = L->getLoopLatch();
807if (!Latch)
808returnnullptr;
809
810BranchInst *LatchBR = dyn_cast<BranchInst>(Latch->getTerminator());
811if (!LatchBR || LatchBR->getNumSuccessors() != 2 || !L->isLoopExiting(Latch))
812returnnullptr;
813
814assert((LatchBR->getSuccessor(0) == L->getHeader() ||
815 LatchBR->getSuccessor(1) == L->getHeader()) &&
816"At least one edge out of the latch must go to the header");
817
818return LatchBR;
819}
820
821/// Return the estimated trip count for any exiting branch which dominates
822/// the loop latch.
823static std::optional<uint64_t>getEstimatedTripCount(BranchInst *ExitingBranch,
824Loop *L,
825uint64_t &OrigExitWeight) {
826// To estimate the number of times the loop body was executed, we want to
827// know the number of times the backedge was taken, vs. the number of times
828// we exited the loop.
829uint64_t LoopWeight, ExitWeight;
830if (!extractBranchWeights(*ExitingBranch, LoopWeight, ExitWeight))
831return std::nullopt;
832
833if (L->contains(ExitingBranch->getSuccessor(1)))
834std::swap(LoopWeight, ExitWeight);
835
836if (!ExitWeight)
837// Don't have a way to return predicated infinite
838return std::nullopt;
839
840 OrigExitWeight = ExitWeight;
841
842// Estimated exit count is a ratio of the loop weight by the weight of the
843// edge exiting the loop, rounded to nearest.
844uint64_t ExitCount =llvm::divideNearest(LoopWeight, ExitWeight);
845// Estimated trip count is one plus estimated exit count.
846return ExitCount + 1;
847}
848
849std::optional<unsigned>
850llvm::getLoopEstimatedTripCount(Loop *L,
851unsigned *EstimatedLoopInvocationWeight) {
852// Currently we take the estimate exit count only from the loop latch,
853// ignoring other exiting blocks. This can overestimate the trip count
854// if we exit through another exit, but can never underestimate it.
855// TODO: incorporate information from other exits
856if (BranchInst *LatchBranch =getExpectedExitLoopLatchBranch(L)) {
857uint64_t ExitWeight;
858if (std::optional<uint64_t> EstTripCount =
859getEstimatedTripCount(LatchBranch, L, ExitWeight)) {
860if (EstimatedLoopInvocationWeight)
861 *EstimatedLoopInvocationWeight = ExitWeight;
862return *EstTripCount;
863 }
864 }
865return std::nullopt;
866}
867
868boolllvm::setLoopEstimatedTripCount(Loop *L,unsigned EstimatedTripCount,
869unsigned EstimatedloopInvocationWeight) {
870// At the moment, we currently support changing the estimate trip count of
871// the latch branch only. We could extend this API to manipulate estimated
872// trip counts for any exit.
873BranchInst *LatchBranch =getExpectedExitLoopLatchBranch(L);
874if (!LatchBranch)
875returnfalse;
876
877// Calculate taken and exit weights.
878unsigned LatchExitWeight = 0;
879unsigned BackedgeTakenWeight = 0;
880
881if (EstimatedTripCount > 0) {
882 LatchExitWeight = EstimatedloopInvocationWeight;
883 BackedgeTakenWeight = (EstimatedTripCount - 1) * LatchExitWeight;
884 }
885
886// Make a swap if back edge is taken when condition is "false".
887if (LatchBranch->getSuccessor(0) != L->getHeader())
888std::swap(BackedgeTakenWeight, LatchExitWeight);
889
890MDBuilder MDB(LatchBranch->getContext());
891
892// Set/Update profile metadata.
893 LatchBranch->setMetadata(
894 LLVMContext::MD_prof,
895 MDB.createBranchWeights(BackedgeTakenWeight, LatchExitWeight));
896
897returntrue;
898}
899
900boolllvm::hasIterationCountInvariantInParent(Loop *InnerLoop,
901ScalarEvolution &SE) {
902Loop *OuterL = InnerLoop->getParentLoop();
903if (!OuterL)
904returntrue;
905
906// Get the backedge taken count for the inner loop
907BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
908constSCEV *InnerLoopBECountSC = SE.getExitCount(InnerLoop, InnerLoopLatch);
909if (isa<SCEVCouldNotCompute>(InnerLoopBECountSC) ||
910 !InnerLoopBECountSC->getType()->isIntegerTy())
911returnfalse;
912
913// Get whether count is invariant to the outer loop
914ScalarEvolution::LoopDisposition LD =
915 SE.getLoopDisposition(InnerLoopBECountSC, OuterL);
916if (LD !=ScalarEvolution::LoopInvariant)
917returnfalse;
918
919returntrue;
920}
921
922constexprIntrinsic::IDllvm::getReductionIntrinsicID(RecurKind RK) {
923switch (RK) {
924default:
925llvm_unreachable("Unexpected recurrence kind");
926case RecurKind::Add:
927return Intrinsic::vector_reduce_add;
928case RecurKind::Mul:
929return Intrinsic::vector_reduce_mul;
930case RecurKind::And:
931return Intrinsic::vector_reduce_and;
932case RecurKind::Or:
933return Intrinsic::vector_reduce_or;
934case RecurKind::Xor:
935return Intrinsic::vector_reduce_xor;
936case RecurKind::FMulAdd:
937case RecurKind::FAdd:
938return Intrinsic::vector_reduce_fadd;
939case RecurKind::FMul:
940return Intrinsic::vector_reduce_fmul;
941case RecurKind::SMax:
942return Intrinsic::vector_reduce_smax;
943case RecurKind::SMin:
944return Intrinsic::vector_reduce_smin;
945case RecurKind::UMax:
946return Intrinsic::vector_reduce_umax;
947case RecurKind::UMin:
948return Intrinsic::vector_reduce_umin;
949case RecurKind::FMax:
950return Intrinsic::vector_reduce_fmax;
951case RecurKind::FMin:
952return Intrinsic::vector_reduce_fmin;
953case RecurKind::FMaximum:
954return Intrinsic::vector_reduce_fmaximum;
955case RecurKind::FMinimum:
956return Intrinsic::vector_reduce_fminimum;
957 }
958}
959
960unsignedllvm::getArithmeticReductionInstruction(Intrinsic::ID RdxID) {
961switch (RdxID) {
962case Intrinsic::vector_reduce_fadd:
963return Instruction::FAdd;
964case Intrinsic::vector_reduce_fmul:
965return Instruction::FMul;
966case Intrinsic::vector_reduce_add:
967return Instruction::Add;
968case Intrinsic::vector_reduce_mul:
969return Instruction::Mul;
970case Intrinsic::vector_reduce_and:
971return Instruction::And;
972case Intrinsic::vector_reduce_or:
973return Instruction::Or;
974case Intrinsic::vector_reduce_xor:
975return Instruction::Xor;
976case Intrinsic::vector_reduce_smax:
977case Intrinsic::vector_reduce_smin:
978case Intrinsic::vector_reduce_umax:
979case Intrinsic::vector_reduce_umin:
980return Instruction::ICmp;
981case Intrinsic::vector_reduce_fmax:
982case Intrinsic::vector_reduce_fmin:
983return Instruction::FCmp;
984default:
985llvm_unreachable("Unexpected ID");
986 }
987}
988
989Intrinsic::IDllvm::getMinMaxReductionIntrinsicOp(Intrinsic::ID RdxID) {
990switch (RdxID) {
991default:
992llvm_unreachable("Unknown min/max recurrence kind");
993case Intrinsic::vector_reduce_umin:
994return Intrinsic::umin;
995case Intrinsic::vector_reduce_umax:
996return Intrinsic::umax;
997case Intrinsic::vector_reduce_smin:
998return Intrinsic::smin;
999case Intrinsic::vector_reduce_smax:
1000return Intrinsic::smax;
1001case Intrinsic::vector_reduce_fmin:
1002return Intrinsic::minnum;
1003case Intrinsic::vector_reduce_fmax:
1004return Intrinsic::maxnum;
1005case Intrinsic::vector_reduce_fminimum:
1006return Intrinsic::minimum;
1007case Intrinsic::vector_reduce_fmaximum:
1008return Intrinsic::maximum;
1009 }
1010}
1011
1012Intrinsic::IDllvm::getMinMaxReductionIntrinsicOp(RecurKind RK) {
1013switch (RK) {
1014default:
1015llvm_unreachable("Unknown min/max recurrence kind");
1016case RecurKind::UMin:
1017return Intrinsic::umin;
1018case RecurKind::UMax:
1019return Intrinsic::umax;
1020case RecurKind::SMin:
1021return Intrinsic::smin;
1022case RecurKind::SMax:
1023return Intrinsic::smax;
1024case RecurKind::FMin:
1025return Intrinsic::minnum;
1026case RecurKind::FMax:
1027return Intrinsic::maxnum;
1028case RecurKind::FMinimum:
1029return Intrinsic::minimum;
1030case RecurKind::FMaximum:
1031return Intrinsic::maximum;
1032 }
1033}
1034
1035RecurKindllvm::getMinMaxReductionRecurKind(Intrinsic::ID RdxID) {
1036switch (RdxID) {
1037case Intrinsic::vector_reduce_smax:
1038return RecurKind::SMax;
1039case Intrinsic::vector_reduce_smin:
1040return RecurKind::SMin;
1041case Intrinsic::vector_reduce_umax:
1042return RecurKind::UMax;
1043case Intrinsic::vector_reduce_umin:
1044return RecurKind::UMin;
1045case Intrinsic::vector_reduce_fmax:
1046return RecurKind::FMax;
1047case Intrinsic::vector_reduce_fmin:
1048return RecurKind::FMin;
1049default:
1050return RecurKind::None;
1051 }
1052}
1053
1054CmpInst::Predicatellvm::getMinMaxReductionPredicate(RecurKind RK) {
1055switch (RK) {
1056default:
1057llvm_unreachable("Unknown min/max recurrence kind");
1058case RecurKind::UMin:
1059returnCmpInst::ICMP_ULT;
1060case RecurKind::UMax:
1061returnCmpInst::ICMP_UGT;
1062case RecurKind::SMin:
1063returnCmpInst::ICMP_SLT;
1064case RecurKind::SMax:
1065returnCmpInst::ICMP_SGT;
1066case RecurKind::FMin:
1067returnCmpInst::FCMP_OLT;
1068case RecurKind::FMax:
1069returnCmpInst::FCMP_OGT;
1070// We do not add FMinimum/FMaximum recurrence kind here since there is no
1071// equivalent predicate which compares signed zeroes according to the
1072// semantics of the intrinsics (llvm.minimum/maximum).
1073 }
1074}
1075
1076Value *llvm::createMinMaxOp(IRBuilderBase &Builder,RecurKind RK,Value *Left,
1077Value *Right) {
1078Type *Ty =Left->getType();
1079if (Ty->isIntOrIntVectorTy() ||
1080 (RK == RecurKind::FMinimum || RK == RecurKind::FMaximum)) {
1081// TODO: Add float minnum/maxnum support when FMF nnan is set.
1082Intrinsic::ID Id =getMinMaxReductionIntrinsicOp(RK);
1083return Builder.CreateIntrinsic(Ty, Id, {Left,Right},nullptr,
1084"rdx.minmax");
1085 }
1086CmpInst::Predicate Pred =getMinMaxReductionPredicate(RK);
1087Value *Cmp = Builder.CreateCmp(Pred,Left,Right,"rdx.minmax.cmp");
1088Value *Select = Builder.CreateSelect(Cmp,Left,Right,"rdx.minmax.select");
1089returnSelect;
1090}
1091
1092// Helper to generate an ordered reduction.
1093Value *llvm::getOrderedReduction(IRBuilderBase &Builder,Value *Acc,Value *Src,
1094unsignedOp,RecurKind RdxKind) {
1095unsigned VF = cast<FixedVectorType>(Src->getType())->getNumElements();
1096
1097// Extract and apply reduction ops in ascending order:
1098// e.g. ((((Acc + Scl[0]) + Scl[1]) + Scl[2]) + ) ... + Scl[VF-1]
1099Value *Result = Acc;
1100for (unsigned ExtractIdx = 0; ExtractIdx != VF; ++ExtractIdx) {
1101Value *Ext =
1102 Builder.CreateExtractElement(Src, Builder.getInt32(ExtractIdx));
1103
1104if (Op != Instruction::ICmp &&Op != Instruction::FCmp) {
1105 Result = Builder.CreateBinOp((Instruction::BinaryOps)Op, Result, Ext,
1106"bin.rdx");
1107 }else {
1108assert(RecurrenceDescriptor::isMinMaxRecurrenceKind(RdxKind) &&
1109"Invalid min/max");
1110 Result =createMinMaxOp(Builder, RdxKind, Result, Ext);
1111 }
1112 }
1113
1114return Result;
1115}
1116
1117// Helper to generate a log2 shuffle reduction.
1118Value *llvm::getShuffleReduction(IRBuilderBase &Builder,Value *Src,
1119unsignedOp,
1120TargetTransformInfo::ReductionShuffle RS,
1121RecurKind RdxKind) {
1122unsigned VF = cast<FixedVectorType>(Src->getType())->getNumElements();
1123// VF is a power of 2 so we can emit the reduction using log2(VF) shuffles
1124// and vector ops, reducing the set of values being computed by half each
1125// round.
1126assert(isPowerOf2_32(VF) &&
1127"Reduction emission only supported for pow2 vectors!");
1128// Note: fast-math-flags flags are controlled by the builder configuration
1129// and are assumed to apply to all generated arithmetic instructions. Other
1130// poison generating flags (nsw/nuw/inbounds/inrange/exact) are not part
1131// of the builder configuration, and since they're not passed explicitly,
1132// will never be relevant here. Note that it would be generally unsound to
1133// propagate these from an intrinsic call to the expansion anyways as we/
1134// change the order of operations.
1135auto BuildShuffledOp = [&Builder, &Op,
1136 &RdxKind](SmallVectorImpl<int> &ShuffleMask,
1137Value *&TmpVec) ->void {
1138Value *Shuf = Builder.CreateShuffleVector(TmpVec, ShuffleMask,"rdx.shuf");
1139if (Op != Instruction::ICmp &&Op != Instruction::FCmp) {
1140 TmpVec = Builder.CreateBinOp((Instruction::BinaryOps)Op, TmpVec, Shuf,
1141"bin.rdx");
1142 }else {
1143assert(RecurrenceDescriptor::isMinMaxRecurrenceKind(RdxKind) &&
1144"Invalid min/max");
1145 TmpVec =createMinMaxOp(Builder, RdxKind, TmpVec, Shuf);
1146 }
1147 };
1148
1149Value *TmpVec = Src;
1150if (TargetTransformInfo::ReductionShuffle::Pairwise == RS) {
1151SmallVector<int, 32> ShuffleMask(VF);
1152for (unsigned stride = 1; stride < VF; stride <<= 1) {
1153// Initialise the mask with undef.
1154 std::fill(ShuffleMask.begin(), ShuffleMask.end(), -1);
1155for (unsigned j = 0; j < VF; j += stride << 1) {
1156 ShuffleMask[j] = j + stride;
1157 }
1158 BuildShuffledOp(ShuffleMask, TmpVec);
1159 }
1160 }else {
1161SmallVector<int, 32> ShuffleMask(VF);
1162for (unsigned i = VF; i != 1; i >>= 1) {
1163// Move the upper half of the vector to the lower half.
1164for (unsigned j = 0; j != i / 2; ++j)
1165 ShuffleMask[j] = i / 2 + j;
1166
1167// Fill the rest of the mask with undef.
1168 std::fill(&ShuffleMask[i / 2], ShuffleMask.end(), -1);
1169 BuildShuffledOp(ShuffleMask, TmpVec);
1170 }
1171 }
1172// The result is in the first element of the vector.
1173return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0));
1174}
1175
1176Value *llvm::createAnyOfReduction(IRBuilderBase &Builder,Value *Src,
1177constRecurrenceDescriptor &Desc,
1178PHINode *OrigPhi) {
1179assert(
1180RecurrenceDescriptor::isAnyOfRecurrenceKind(Desc.getRecurrenceKind()) &&
1181"Unexpected reduction kind");
1182Value *InitVal =Desc.getRecurrenceStartValue();
1183Value *NewVal =nullptr;
1184
1185// First use the original phi to determine the new value we're trying to
1186// select from in the loop.
1187SelectInst *SI =nullptr;
1188for (auto *U : OrigPhi->users()) {
1189if ((SI = dyn_cast<SelectInst>(U)))
1190break;
1191 }
1192assert(SI &&"One user of the original phi should be a select");
1193
1194if (SI->getTrueValue() == OrigPhi)
1195 NewVal = SI->getFalseValue();
1196else {
1197assert(SI->getFalseValue() == OrigPhi &&
1198"At least one input to the select should be the original Phi");
1199 NewVal = SI->getTrueValue();
1200 }
1201
1202// If any predicate is true it means that we want to select the new value.
1203Value *AnyOf =
1204 Src->getType()->isVectorTy() ? Builder.CreateOrReduce(Src) : Src;
1205// The compares in the loop may yield poison, which propagates through the
1206// bitwise ORs. Freeze it here before the condition is used.
1207 AnyOf = Builder.CreateFreeze(AnyOf);
1208return Builder.CreateSelect(AnyOf, NewVal, InitVal,"rdx.select");
1209}
1210
1211Value *llvm::createFindLastIVReduction(IRBuilderBase &Builder,Value *Src,
1212constRecurrenceDescriptor &Desc) {
1213assert(RecurrenceDescriptor::isFindLastIVRecurrenceKind(
1214Desc.getRecurrenceKind()) &&
1215"Unexpected reduction kind");
1216Value *StartVal =Desc.getRecurrenceStartValue();
1217Value *Sentinel =Desc.getSentinelValue();
1218Value *MaxRdx = Src->getType()->isVectorTy()
1219 ? Builder.CreateIntMaxReduce(Src,true)
1220 : Src;
1221// Correct the final reduction result back to the start value if the maximum
1222// reduction is sentinel value.
1223Value *Cmp =
1224 Builder.CreateCmp(CmpInst::ICMP_NE, MaxRdx,Sentinel,"rdx.select.cmp");
1225return Builder.CreateSelect(Cmp, MaxRdx, StartVal,"rdx.select");
1226}
1227
1228Value *llvm::getReductionIdentity(Intrinsic::ID RdxID,Type *Ty,
1229FastMathFlags Flags) {
1230bool Negative =false;
1231switch (RdxID) {
1232default:
1233llvm_unreachable("Expecting a reduction intrinsic");
1234case Intrinsic::vector_reduce_add:
1235case Intrinsic::vector_reduce_mul:
1236case Intrinsic::vector_reduce_or:
1237case Intrinsic::vector_reduce_xor:
1238case Intrinsic::vector_reduce_and:
1239case Intrinsic::vector_reduce_fadd:
1240case Intrinsic::vector_reduce_fmul: {
1241unsigned Opc =getArithmeticReductionInstruction(RdxID);
1242returnConstantExpr::getBinOpIdentity(Opc, Ty,false,
1243 Flags.noSignedZeros());
1244 }
1245case Intrinsic::vector_reduce_umax:
1246case Intrinsic::vector_reduce_umin:
1247case Intrinsic::vector_reduce_smin:
1248case Intrinsic::vector_reduce_smax: {
1249Intrinsic::ID ScalarID =getMinMaxReductionIntrinsicOp(RdxID);
1250returnConstantExpr::getIntrinsicIdentity(ScalarID, Ty);
1251 }
1252case Intrinsic::vector_reduce_fmax:
1253case Intrinsic::vector_reduce_fmaximum:
1254 Negative =true;
1255 [[fallthrough]];
1256case Intrinsic::vector_reduce_fmin:
1257case Intrinsic::vector_reduce_fminimum: {
1258bool PropagatesNaN = RdxID == Intrinsic::vector_reduce_fminimum ||
1259 RdxID == Intrinsic::vector_reduce_fmaximum;
1260constfltSemantics &Semantics = Ty->getFltSemantics();
1261return (!Flags.noNaNs() && !PropagatesNaN)
1262 ?ConstantFP::getQNaN(Ty, Negative)
1263 : !Flags.noInfs()
1264 ?ConstantFP::getInfinity(Ty, Negative)
1265 : ConstantFP::get(Ty,APFloat::getLargest(Semantics, Negative));
1266 }
1267 }
1268}
1269
1270Value *llvm::getRecurrenceIdentity(RecurKind K,Type *Tp,FastMathFlags FMF) {
1271assert((!(K == RecurKind::FMin || K == RecurKind::FMax) ||
1272 (FMF.noNaNs() && FMF.noSignedZeros())) &&
1273"nnan, nsz is expected to be set for FP min/max reduction.");
1274Intrinsic::ID RdxID =getReductionIntrinsicID(K);
1275returngetReductionIdentity(RdxID, Tp, FMF);
1276}
1277
1278Value *llvm::createSimpleReduction(IRBuilderBase &Builder,Value *Src,
1279RecurKind RdxKind) {
1280auto *SrcVecEltTy = cast<VectorType>(Src->getType())->getElementType();
1281auto getIdentity = [&]() {
1282returngetRecurrenceIdentity(RdxKind, SrcVecEltTy,
1283 Builder.getFastMathFlags());
1284 };
1285switch (RdxKind) {
1286case RecurKind::Add:
1287case RecurKind::Mul:
1288case RecurKind::And:
1289case RecurKind::Or:
1290case RecurKind::Xor:
1291case RecurKind::SMax:
1292case RecurKind::SMin:
1293case RecurKind::UMax:
1294case RecurKind::UMin:
1295case RecurKind::FMax:
1296case RecurKind::FMin:
1297case RecurKind::FMinimum:
1298case RecurKind::FMaximum:
1299return Builder.CreateUnaryIntrinsic(getReductionIntrinsicID(RdxKind), Src);
1300case RecurKind::FMulAdd:
1301case RecurKind::FAdd:
1302return Builder.CreateFAddReduce(getIdentity(), Src);
1303case RecurKind::FMul:
1304return Builder.CreateFMulReduce(getIdentity(), Src);
1305default:
1306llvm_unreachable("Unhandled opcode");
1307 }
1308}
1309
1310Value *llvm::createSimpleReduction(VectorBuilder &VBuilder,Value *Src,
1311constRecurrenceDescriptor &Desc) {
1312RecurKind Kind =Desc.getRecurrenceKind();
1313assert(!RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) &&
1314"AnyOf reduction is not supported.");
1315Intrinsic::ID Id =getReductionIntrinsicID(Kind);
1316auto *SrcTy = cast<VectorType>(Src->getType());
1317Type *SrcEltTy = SrcTy->getElementType();
1318Value *Iden =getRecurrenceIdentity(Kind, SrcEltTy,Desc.getFastMathFlags());
1319Value *Ops[] = {Iden, Src};
1320return VBuilder.createSimpleReduction(Id, SrcTy, Ops);
1321}
1322
1323Value *llvm::createReduction(IRBuilderBase &B,
1324constRecurrenceDescriptor &Desc,Value *Src,
1325PHINode *OrigPhi) {
1326// TODO: Support in-order reductions based on the recurrence descriptor.
1327// All ops in the reduction inherit fast-math-flags from the recurrence
1328// descriptor.
1329IRBuilderBase::FastMathFlagGuard FMFGuard(B);
1330B.setFastMathFlags(Desc.getFastMathFlags());
1331
1332RecurKind RK =Desc.getRecurrenceKind();
1333if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK))
1334returncreateAnyOfReduction(B, Src,Desc, OrigPhi);
1335if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK))
1336returncreateFindLastIVReduction(B, Src,Desc);
1337
1338returncreateSimpleReduction(B, Src, RK);
1339}
1340
1341Value *llvm::createOrderedReduction(IRBuilderBase &B,
1342constRecurrenceDescriptor &Desc,
1343Value *Src,Value *Start) {
1344assert((Desc.getRecurrenceKind() == RecurKind::FAdd ||
1345Desc.getRecurrenceKind() == RecurKind::FMulAdd) &&
1346"Unexpected reduction kind");
1347assert(Src->getType()->isVectorTy() &&"Expected a vector type");
1348assert(!Start->getType()->isVectorTy() &&"Expected a scalar type");
1349
1350returnB.CreateFAddReduce(Start, Src);
1351}
1352
1353Value *llvm::createOrderedReduction(VectorBuilder &VBuilder,
1354constRecurrenceDescriptor &Desc,
1355Value *Src,Value *Start) {
1356assert((Desc.getRecurrenceKind() == RecurKind::FAdd ||
1357Desc.getRecurrenceKind() == RecurKind::FMulAdd) &&
1358"Unexpected reduction kind");
1359assert(Src->getType()->isVectorTy() &&"Expected a vector type");
1360assert(!Start->getType()->isVectorTy() &&"Expected a scalar type");
1361
1362Intrinsic::ID Id =getReductionIntrinsicID(RecurKind::FAdd);
1363auto *SrcTy = cast<VectorType>(Src->getType());
1364Value *Ops[] = {Start, Src};
1365return VBuilder.createSimpleReduction(Id, SrcTy, Ops);
1366}
1367
1368voidllvm::propagateIRFlags(Value *I,ArrayRef<Value *> VL,Value *OpValue,
1369bool IncludeWrapFlags) {
1370auto *VecOp = dyn_cast<Instruction>(I);
1371if (!VecOp)
1372return;
1373auto *Intersection = (OpValue ==nullptr) ? dyn_cast<Instruction>(VL[0])
1374 : dyn_cast<Instruction>(OpValue);
1375if (!Intersection)
1376return;
1377constunsigned Opcode = Intersection->getOpcode();
1378 VecOp->copyIRFlags(Intersection, IncludeWrapFlags);
1379for (auto *V : VL) {
1380auto *Instr = dyn_cast<Instruction>(V);
1381if (!Instr)
1382continue;
1383if (OpValue ==nullptr || Opcode == Instr->getOpcode())
1384 VecOp->andIRFlags(V);
1385 }
1386}
1387
1388boolllvm::isKnownNegativeInLoop(constSCEV *S,constLoop *L,
1389ScalarEvolution &SE) {
1390constSCEV *Zero = SE.getZero(S->getType());
1391return SE.isAvailableAtLoopEntry(S, L) &&
1392 SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SLT, S, Zero);
1393}
1394
1395boolllvm::isKnownNonNegativeInLoop(constSCEV *S,constLoop *L,
1396ScalarEvolution &SE) {
1397constSCEV *Zero = SE.getZero(S->getType());
1398return SE.isAvailableAtLoopEntry(S, L) &&
1399 SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGE, S, Zero);
1400}
1401
1402boolllvm::isKnownPositiveInLoop(constSCEV *S,constLoop *L,
1403ScalarEvolution &SE) {
1404constSCEV *Zero = SE.getZero(S->getType());
1405return SE.isAvailableAtLoopEntry(S, L) &&
1406 SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGT, S, Zero);
1407}
1408
1409boolllvm::isKnownNonPositiveInLoop(constSCEV *S,constLoop *L,
1410ScalarEvolution &SE) {
1411constSCEV *Zero = SE.getZero(S->getType());
1412return SE.isAvailableAtLoopEntry(S, L) &&
1413 SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SLE, S, Zero);
1414}
1415
1416boolllvm::cannotBeMinInLoop(constSCEV *S,constLoop *L,ScalarEvolution &SE,
1417boolSigned) {
1418unsignedBitWidth = cast<IntegerType>(S->getType())->getBitWidth();
1419APInt Min =Signed ?APInt::getSignedMinValue(BitWidth) :
1420APInt::getMinValue(BitWidth);
1421auto Predicate =Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1422return SE.isAvailableAtLoopEntry(S, L) &&
1423 SE.isLoopEntryGuardedByCond(L, Predicate, S,
1424 SE.getConstant(Min));
1425}
1426
1427boolllvm::cannotBeMaxInLoop(constSCEV *S,constLoop *L,ScalarEvolution &SE,
1428boolSigned) {
1429unsignedBitWidth = cast<IntegerType>(S->getType())->getBitWidth();
1430APInt Max =Signed ?APInt::getSignedMaxValue(BitWidth) :
1431APInt::getMaxValue(BitWidth);
1432auto Predicate =Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1433return SE.isAvailableAtLoopEntry(S, L) &&
1434 SE.isLoopEntryGuardedByCond(L, Predicate, S,
1435 SE.getConstant(Max));
1436}
1437
1438//===----------------------------------------------------------------------===//
1439// rewriteLoopExitValues - Optimize IV users outside the loop.
1440// As a side effect, reduces the amount of IV processing within the loop.
1441//===----------------------------------------------------------------------===//
1442
1443staticboolhasHardUserWithinLoop(constLoop *L,constInstruction *I) {
1444SmallPtrSet<const Instruction *, 8> Visited;
1445SmallVector<const Instruction *, 8> WorkList;
1446 Visited.insert(I);
1447 WorkList.push_back(I);
1448while (!WorkList.empty()) {
1449constInstruction *Curr = WorkList.pop_back_val();
1450// This use is outside the loop, nothing to do.
1451if (!L->contains(Curr))
1452continue;
1453// Do we assume it is a "hard" use which will not be eliminated easily?
1454if (Curr->mayHaveSideEffects())
1455returntrue;
1456// Otherwise, add all its users to worklist.
1457for (constauto *U : Curr->users()) {
1458auto *UI = cast<Instruction>(U);
1459if (Visited.insert(UI).second)
1460 WorkList.push_back(UI);
1461 }
1462 }
1463returnfalse;
1464}
1465
1466// Collect information about PHI nodes which can be transformed in
1467// rewriteLoopExitValues.
1468structRewritePhi {
1469PHINode *PN;// For which PHI node is this replacement?
1470unsignedIth;// For which incoming value?
1471constSCEV *ExpansionSCEV;// The SCEV of the incoming value we are rewriting.
1472Instruction *ExpansionPoint;// Where we'd like to expand that SCEV?
1473boolHighCost;// Is this expansion a high-cost?
1474
1475RewritePhi(PHINode *P,unsignedI,constSCEV *Val,Instruction *ExpansionPt,
1476boolH)
1477 :PN(P),Ith(I),ExpansionSCEV(Val),ExpansionPoint(ExpansionPt),
1478HighCost(H) {}
1479};
1480
1481// Check whether it is possible to delete the loop after rewriting exit
1482// value. If it is possible, ignore ReplaceExitValue and do rewriting
1483// aggressively.
1484staticboolcanLoopBeDeleted(Loop *L,SmallVector<RewritePhi, 8> &RewritePhiSet) {
1485BasicBlock *Preheader = L->getLoopPreheader();
1486// If there is no preheader, the loop will not be deleted.
1487if (!Preheader)
1488returnfalse;
1489
1490// In LoopDeletion pass Loop can be deleted when ExitingBlocks.size() > 1.
1491// We obviate multiple ExitingBlocks case for simplicity.
1492// TODO: If we see testcase with multiple ExitingBlocks can be deleted
1493// after exit value rewriting, we can enhance the logic here.
1494SmallVector<BasicBlock *, 4> ExitingBlocks;
1495 L->getExitingBlocks(ExitingBlocks);
1496SmallVector<BasicBlock *, 8> ExitBlocks;
1497 L->getUniqueExitBlocks(ExitBlocks);
1498if (ExitBlocks.size() != 1 || ExitingBlocks.size() != 1)
1499returnfalse;
1500
1501BasicBlock *ExitBlock = ExitBlocks[0];
1502BasicBlock::iterator BI = ExitBlock->begin();
1503while (PHINode *P = dyn_cast<PHINode>(BI)) {
1504Value *Incoming =P->getIncomingValueForBlock(ExitingBlocks[0]);
1505
1506// If the Incoming value of P is found in RewritePhiSet, we know it
1507// could be rewritten to use a loop invariant value in transformation
1508// phase later. Skip it in the loop invariant check below.
1509bool found =false;
1510for (constRewritePhi &Phi : RewritePhiSet) {
1511unsigned i = Phi.Ith;
1512if (Phi.PN ==P && (Phi.PN)->getIncomingValue(i) ==Incoming) {
1513 found =true;
1514break;
1515 }
1516 }
1517
1518Instruction *I;
1519if (!found && (I = dyn_cast<Instruction>(Incoming)))
1520if (!L->hasLoopInvariantOperands(I))
1521returnfalse;
1522
1523 ++BI;
1524 }
1525
1526for (auto *BB : L->blocks())
1527if (llvm::any_of(*BB, [](Instruction &I) {
1528returnI.mayHaveSideEffects();
1529 }))
1530returnfalse;
1531
1532returntrue;
1533}
1534
1535/// Checks if it is safe to call InductionDescriptor::isInductionPHI for \p Phi,
1536/// and returns true if this Phi is an induction phi in the loop. When
1537/// isInductionPHI returns true, \p ID will be also be set by isInductionPHI.
1538staticboolcheckIsIndPhi(PHINode *Phi,Loop *L,ScalarEvolution *SE,
1539InductionDescriptor &ID) {
1540if (!Phi)
1541returnfalse;
1542if (!L->getLoopPreheader())
1543returnfalse;
1544if (Phi->getParent() != L->getHeader())
1545returnfalse;
1546returnInductionDescriptor::isInductionPHI(Phi, L, SE,ID);
1547}
1548
1549intllvm::rewriteLoopExitValues(Loop *L,LoopInfo *LI,TargetLibraryInfo *TLI,
1550ScalarEvolution *SE,
1551constTargetTransformInfo *TTI,
1552SCEVExpander &Rewriter,DominatorTree *DT,
1553ReplaceExitValReplaceExitValue,
1554SmallVector<WeakTrackingVH, 16> &DeadInsts) {
1555// Check a pre-condition.
1556assert(L->isRecursivelyLCSSAForm(*DT, *LI) &&
1557"Indvars did not preserve LCSSA!");
1558
1559SmallVector<BasicBlock*, 8> ExitBlocks;
1560 L->getUniqueExitBlocks(ExitBlocks);
1561
1562SmallVector<RewritePhi, 8> RewritePhiSet;
1563// Find all values that are computed inside the loop, but used outside of it.
1564// Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan
1565// the exit blocks of the loop to find them.
1566for (BasicBlock *ExitBB : ExitBlocks) {
1567// If there are no PHI nodes in this exit block, then no values defined
1568// inside the loop are used on this path, skip it.
1569PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
1570if (!PN)continue;
1571
1572unsigned NumPreds = PN->getNumIncomingValues();
1573
1574// Iterate over all of the PHI nodes.
1575BasicBlock::iterator BBI = ExitBB->begin();
1576while ((PN = dyn_cast<PHINode>(BBI++))) {
1577if (PN->use_empty())
1578continue;// dead use, don't replace it
1579
1580if (!SE->isSCEVable(PN->getType()))
1581continue;
1582
1583// Iterate over all of the values in all the PHI nodes.
1584for (unsigned i = 0; i != NumPreds; ++i) {
1585// If the value being merged in is not integer or is not defined
1586// in the loop, skip it.
1587Value *InVal = PN->getIncomingValue(i);
1588if (!isa<Instruction>(InVal))
1589continue;
1590
1591// If this pred is for a subloop, not L itself, skip it.
1592if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
1593continue;// The Block is in a subloop, skip it.
1594
1595// Check that InVal is defined in the loop.
1596Instruction *Inst = cast<Instruction>(InVal);
1597if (!L->contains(Inst))
1598continue;
1599
1600// Find exit values which are induction variables in the loop, and are
1601// unused in the loop, with the only use being the exit block PhiNode,
1602// and the induction variable update binary operator.
1603// The exit value can be replaced with the final value when it is cheap
1604// to do so.
1605if (ReplaceExitValue ==UnusedIndVarInLoop) {
1606InductionDescriptorID;
1607PHINode *IndPhi = dyn_cast<PHINode>(Inst);
1608if (IndPhi) {
1609if (!checkIsIndPhi(IndPhi, L, SE,ID))
1610continue;
1611// This is an induction PHI. Check that the only users are PHI
1612// nodes, and induction variable update binary operators.
1613if (llvm::any_of(Inst->users(), [&](User *U) {
1614 if (!isa<PHINode>(U) && !isa<BinaryOperator>(U))
1615 return true;
1616 BinaryOperator *B = dyn_cast<BinaryOperator>(U);
1617 if (B && B != ID.getInductionBinOp())
1618 return true;
1619 return false;
1620 }))
1621continue;
1622 }else {
1623// If it is not an induction phi, it must be an induction update
1624// binary operator with an induction phi user.
1625BinaryOperator *B = dyn_cast<BinaryOperator>(Inst);
1626if (!B)
1627continue;
1628if (llvm::any_of(Inst->users(), [&](User *U) {
1629 PHINode *Phi = dyn_cast<PHINode>(U);
1630 if (Phi != PN && !checkIsIndPhi(Phi, L, SE, ID))
1631 return true;
1632 return false;
1633 }))
1634continue;
1635if (B !=ID.getInductionBinOp())
1636continue;
1637 }
1638 }
1639
1640// Okay, this instruction has a user outside of the current loop
1641// and varies predictably *inside* the loop. Evaluate the value it
1642// contains when the loop exits, if possible. We prefer to start with
1643// expressions which are true for all exits (so as to maximize
1644// expression reuse by the SCEVExpander), but resort to per-exit
1645// evaluation if that fails.
1646constSCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
1647if (isa<SCEVCouldNotCompute>(ExitValue) ||
1648 !SE->isLoopInvariant(ExitValue, L) ||
1649 !Rewriter.isSafeToExpand(ExitValue)) {
1650// TODO: This should probably be sunk into SCEV in some way; maybe a
1651// getSCEVForExit(SCEV*, L, ExitingBB)? It can be generalized for
1652// most SCEV expressions and other recurrence types (e.g. shift
1653// recurrences). Is there existing code we can reuse?
1654constSCEV *ExitCount = SE->getExitCount(L, PN->getIncomingBlock(i));
1655if (isa<SCEVCouldNotCompute>(ExitCount))
1656continue;
1657if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Inst)))
1658if (AddRec->getLoop() == L)
1659 ExitValue = AddRec->evaluateAtIteration(ExitCount, *SE);
1660if (isa<SCEVCouldNotCompute>(ExitValue) ||
1661 !SE->isLoopInvariant(ExitValue, L) ||
1662 !Rewriter.isSafeToExpand(ExitValue))
1663continue;
1664 }
1665
1666// Computing the value outside of the loop brings no benefit if it is
1667// definitely used inside the loop in a way which can not be optimized
1668// away. Avoid doing so unless we know we have a value which computes
1669// the ExitValue already. TODO: This should be merged into SCEV
1670// expander to leverage its knowledge of existing expressions.
1671if (ReplaceExitValue !=AlwaysRepl && !isa<SCEVConstant>(ExitValue) &&
1672 !isa<SCEVUnknown>(ExitValue) &&hasHardUserWithinLoop(L, Inst))
1673continue;
1674
1675// Check if expansions of this SCEV would count as being high cost.
1676bool HighCost =Rewriter.isHighCostExpansion(
1677 ExitValue, L,SCEVCheapExpansionBudget,TTI, Inst);
1678
1679// Note that we must not perform expansions until after
1680// we query *all* the costs, because if we perform temporary expansion
1681// inbetween, one that we might not intend to keep, said expansion
1682// *may* affect cost calculation of the next SCEV's we'll query,
1683// and next SCEV may errneously get smaller cost.
1684
1685// Collect all the candidate PHINodes to be rewritten.
1686Instruction *InsertPt =
1687 (isa<PHINode>(Inst) || isa<LandingPadInst>(Inst)) ?
1688 &*Inst->getParent()->getFirstInsertionPt() : Inst;
1689 RewritePhiSet.emplace_back(PN, i, ExitValue, InsertPt, HighCost);
1690 }
1691 }
1692 }
1693
1694// TODO: evaluate whether it is beneficial to change how we calculate
1695// high-cost: if we have SCEV 'A' which we know we will expand, should we
1696// calculate the cost of other SCEV's after expanding SCEV 'A', thus
1697// potentially giving cost bonus to those other SCEV's?
1698
1699bool LoopCanBeDel =canLoopBeDeleted(L, RewritePhiSet);
1700int NumReplaced = 0;
1701
1702// Transformation.
1703for (constRewritePhi &Phi : RewritePhiSet) {
1704PHINode *PN = Phi.PN;
1705
1706// Only do the rewrite when the ExitValue can be expanded cheaply.
1707// If LoopCanBeDel is true, rewrite exit value aggressively.
1708if ((ReplaceExitValue ==OnlyCheapRepl ||
1709ReplaceExitValue ==UnusedIndVarInLoop) &&
1710 !LoopCanBeDel && Phi.HighCost)
1711continue;
1712
1713Value *ExitVal =Rewriter.expandCodeFor(
1714 Phi.ExpansionSCEV, Phi.PN->getType(), Phi.ExpansionPoint);
1715
1716LLVM_DEBUG(dbgs() <<"rewriteLoopExitValues: AfterLoopVal = " << *ExitVal
1717 <<'\n'
1718 <<" LoopVal = " << *(Phi.ExpansionPoint) <<"\n");
1719
1720#ifndef NDEBUG
1721// If we reuse an instruction from a loop which is neither L nor one of
1722// its containing loops, we end up breaking LCSSA form for this loop by
1723// creating a new use of its instruction.
1724if (auto *ExitInsn = dyn_cast<Instruction>(ExitVal))
1725if (auto *EVL = LI->getLoopFor(ExitInsn->getParent()))
1726if (EVL != L)
1727assert(EVL->contains(L) &&"LCSSA breach detected!");
1728#endif
1729
1730 NumReplaced++;
1731Instruction *Inst = cast<Instruction>(PN->getIncomingValue(Phi.Ith));
1732 PN->setIncomingValue(Phi.Ith, ExitVal);
1733// It's necessary to tell ScalarEvolution about this explicitly so that
1734// it can walk the def-use list and forget all SCEVs, as it may not be
1735// watching the PHI itself. Once the new exit value is in place, there
1736// may not be a def-use connection between the loop and every instruction
1737// which got a SCEVAddRecExpr for that loop.
1738 SE->forgetValue(PN);
1739
1740// If this instruction is dead now, delete it. Don't do it now to avoid
1741// invalidating iterators.
1742if (isInstructionTriviallyDead(Inst, TLI))
1743 DeadInsts.push_back(Inst);
1744
1745// Replace PN with ExitVal if that is legal and does not break LCSSA.
1746if (PN->getNumIncomingValues() == 1 &&
1747 LI->replacementPreservesLCSSAForm(PN, ExitVal)) {
1748 PN->replaceAllUsesWith(ExitVal);
1749 PN->eraseFromParent();
1750 }
1751 }
1752
1753// The insertion point instruction may have been deleted; clear it out
1754// so that the rewriter doesn't trip over it later.
1755Rewriter.clearInsertPoint();
1756return NumReplaced;
1757}
1758
1759/// Set weights for \p UnrolledLoop and \p RemainderLoop based on weights for
1760/// \p OrigLoop.
1761voidllvm::setProfileInfoAfterUnrolling(Loop *OrigLoop,Loop *UnrolledLoop,
1762Loop *RemainderLoop,uint64_t UF) {
1763assert(UF > 0 &&"Zero unrolled factor is not supported");
1764assert(UnrolledLoop != RemainderLoop &&
1765"Unrolled and Remainder loops are expected to distinct");
1766
1767// Get number of iterations in the original scalar loop.
1768unsigned OrigLoopInvocationWeight = 0;
1769 std::optional<unsigned> OrigAverageTripCount =
1770getLoopEstimatedTripCount(OrigLoop, &OrigLoopInvocationWeight);
1771if (!OrigAverageTripCount)
1772return;
1773
1774// Calculate number of iterations in unrolled loop.
1775unsigned UnrolledAverageTripCount = *OrigAverageTripCount / UF;
1776// Calculate number of iterations for remainder loop.
1777unsigned RemainderAverageTripCount = *OrigAverageTripCount % UF;
1778
1779setLoopEstimatedTripCount(UnrolledLoop, UnrolledAverageTripCount,
1780 OrigLoopInvocationWeight);
1781setLoopEstimatedTripCount(RemainderLoop, RemainderAverageTripCount,
1782 OrigLoopInvocationWeight);
1783}
1784
1785/// Utility that implements appending of loops onto a worklist.
1786/// Loops are added in preorder (analogous for reverse postorder for trees),
1787/// and the worklist is processed LIFO.
1788template <typename RangeT>
1789voidllvm::appendReversedLoopsToWorklist(
1790 RangeT &&Loops,SmallPriorityWorklist<Loop *, 4> &Worklist) {
1791// We use an internal worklist to build up the preorder traversal without
1792// recursion.
1793SmallVector<Loop *, 4> PreOrderLoops, PreOrderWorklist;
1794
1795// We walk the initial sequence of loops in reverse because we generally want
1796// to visit defs before uses and the worklist is LIFO.
1797for (Loop *RootL :Loops) {
1798assert(PreOrderLoops.empty() &&"Must start with an empty preorder walk.");
1799assert(PreOrderWorklist.empty() &&
1800"Must start with an empty preorder walk worklist.");
1801 PreOrderWorklist.push_back(RootL);
1802do {
1803Loop *L = PreOrderWorklist.pop_back_val();
1804 PreOrderWorklist.append(L->begin(), L->end());
1805 PreOrderLoops.push_back(L);
1806 }while (!PreOrderWorklist.empty());
1807
1808 Worklist.insert(std::move(PreOrderLoops));
1809 PreOrderLoops.clear();
1810 }
1811}
1812
1813template <typename RangeT>
1814voidllvm::appendLoopsToWorklist(RangeT &&Loops,
1815SmallPriorityWorklist<Loop *, 4> &Worklist) {
1816appendReversedLoopsToWorklist(reverse(Loops), Worklist);
1817}
1818
1819templatevoid llvm::appendLoopsToWorklist<ArrayRef<Loop *> &>(
1820ArrayRef<Loop *> &Loops,SmallPriorityWorklist<Loop *, 4> &Worklist);
1821
1822templatevoid
1823llvm::appendLoopsToWorklist<Loop &>(Loop &L,
1824SmallPriorityWorklist<Loop *, 4> &Worklist);
1825
1826voidllvm::appendLoopsToWorklist(LoopInfo &LI,
1827SmallPriorityWorklist<Loop *, 4> &Worklist) {
1828appendReversedLoopsToWorklist(LI, Worklist);
1829}
1830
1831Loop *llvm::cloneLoop(Loop *L,Loop *PL,ValueToValueMapTy &VM,
1832LoopInfo *LI,LPPassManager *LPM) {
1833Loop &New = *LI->AllocateLoop();
1834if (PL)
1835 PL->addChildLoop(&New);
1836else
1837 LI->addTopLevelLoop(&New);
1838
1839if (LPM)
1840 LPM->addLoop(New);
1841
1842// Add all of the blocks in L to the new loop.
1843for (BasicBlock *BB : L->blocks())
1844if (LI->getLoopFor(BB) == L)
1845 New.addBasicBlockToLoop(cast<BasicBlock>(VM[BB]), *LI);
1846
1847// Add all of the subloops to the new loop.
1848for (Loop *I : *L)
1849cloneLoop(I, &New, VM, LI, LPM);
1850
1851return &New;
1852}
1853
1854/// IR Values for the lower and upper bounds of a pointer evolution. We
1855/// need to use value-handles because SCEV expansion can invalidate previously
1856/// expanded values. Thus expansion of a pointer can invalidate the bounds for
1857/// a previous one.
1858structPointerBounds {
1859TrackingVH<Value>Start;
1860TrackingVH<Value>End;
1861Value *StrideToCheck;
1862};
1863
1864/// Expand code for the lower and upper bound of the pointer group \p CG
1865/// in \p TheLoop. \return the values for the bounds.
1866staticPointerBoundsexpandBounds(constRuntimeCheckingPtrGroup *CG,
1867Loop *TheLoop,Instruction *Loc,
1868SCEVExpander &Exp,boolHoistRuntimeChecks) {
1869LLVMContext &Ctx = Loc->getContext();
1870Type *PtrArithTy = PointerType::get(Ctx, CG->AddressSpace);
1871
1872Value *Start =nullptr, *End =nullptr;
1873LLVM_DEBUG(dbgs() <<"LAA: Adding RT check for range:\n");
1874constSCEV *Low = CG->Low, *High = CG->High, *Stride =nullptr;
1875
1876// If the Low and High values are themselves loop-variant, then we may want
1877// to expand the range to include those covered by the outer loop as well.
1878// There is a trade-off here with the advantage being that creating checks
1879// using the expanded range permits the runtime memory checks to be hoisted
1880// out of the outer loop. This reduces the cost of entering the inner loop,
1881// which can be significant for low trip counts. The disadvantage is that
1882// there is a chance we may now never enter the vectorized inner loop,
1883// whereas using a restricted range check could have allowed us to enter at
1884// least once. This is why the behaviour is not currently the default and is
1885// controlled by the parameter 'HoistRuntimeChecks'.
1886if (HoistRuntimeChecks && TheLoop->getParentLoop() &&
1887 isa<SCEVAddRecExpr>(High) && isa<SCEVAddRecExpr>(Low)) {
1888auto *HighAR = cast<SCEVAddRecExpr>(High);
1889auto *LowAR = cast<SCEVAddRecExpr>(Low);
1890constLoop *OuterLoop = TheLoop->getParentLoop();
1891ScalarEvolution &SE = *Exp.getSE();
1892constSCEV *Recur = LowAR->getStepRecurrence(SE);
1893if (Recur == HighAR->getStepRecurrence(SE) &&
1894 HighAR->getLoop() == OuterLoop && LowAR->getLoop() == OuterLoop) {
1895BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
1896constSCEV *OuterExitCount = SE.getExitCount(OuterLoop, OuterLoopLatch);
1897if (!isa<SCEVCouldNotCompute>(OuterExitCount) &&
1898 OuterExitCount->getType()->isIntegerTy()) {
1899constSCEV *NewHigh =
1900 cast<SCEVAddRecExpr>(High)->evaluateAtIteration(OuterExitCount, SE);
1901if (!isa<SCEVCouldNotCompute>(NewHigh)) {
1902LLVM_DEBUG(dbgs() <<"LAA: Expanded RT check for range to include "
1903"outer loop in order to permit hoisting\n");
1904High = NewHigh;
1905Low = cast<SCEVAddRecExpr>(Low)->getStart();
1906// If there is a possibility that the stride is negative then we have
1907// to generate extra checks to ensure the stride is positive.
1908if (!SE.isKnownNonNegative(
1909 SE.applyLoopGuards(Recur, HighAR->getLoop()))) {
1910 Stride = Recur;
1911LLVM_DEBUG(dbgs() <<"LAA: ... but need to check stride is "
1912"positive: "
1913 << *Stride <<'\n');
1914 }
1915 }
1916 }
1917 }
1918 }
1919
1920 Start = Exp.expandCodeFor(Low, PtrArithTy, Loc);
1921End = Exp.expandCodeFor(High, PtrArithTy, Loc);
1922if (CG->NeedsFreeze) {
1923IRBuilder<> Builder(Loc);
1924 Start = Builder.CreateFreeze(Start, Start->getName() +".fr");
1925End = Builder.CreateFreeze(End,End->getName() +".fr");
1926 }
1927Value *StrideVal =
1928 Stride ? Exp.expandCodeFor(Stride, Stride->getType(), Loc) :nullptr;
1929LLVM_DEBUG(dbgs() <<"Start: " << *Low <<" End: " << *High <<"\n");
1930return {Start,End, StrideVal};
1931}
1932
1933/// Turns a collection of checks into a collection of expanded upper and
1934/// lower bounds for both pointers in the check.
1935staticSmallVector<std::pair<PointerBounds, PointerBounds>, 4>
1936expandBounds(constSmallVectorImpl<RuntimePointerCheck> &PointerChecks,Loop *L,
1937Instruction *Loc,SCEVExpander &Exp,boolHoistRuntimeChecks) {
1938SmallVector<std::pair<PointerBounds, PointerBounds>, 4> ChecksWithBounds;
1939
1940// Here we're relying on the SCEV Expander's cache to only emit code for the
1941// same bounds once.
1942transform(PointerChecks, std::back_inserter(ChecksWithBounds),
1943 [&](constRuntimePointerCheck &Check) {
1944PointerBoundsFirst =expandBounds(Check.first, L, Loc, Exp,
1945HoistRuntimeChecks),
1946 Second =expandBounds(Check.second, L, Loc, Exp,
1947HoistRuntimeChecks);
1948return std::make_pair(First, Second);
1949 });
1950
1951return ChecksWithBounds;
1952}
1953
1954Value *llvm::addRuntimeChecks(
1955Instruction *Loc,Loop *TheLoop,
1956constSmallVectorImpl<RuntimePointerCheck> &PointerChecks,
1957SCEVExpander &Exp,boolHoistRuntimeChecks) {
1958// TODO: Move noalias annotation code from LoopVersioning here and share with LV if possible.
1959// TODO: Pass RtPtrChecking instead of PointerChecks and SE separately, if possible
1960auto ExpandedChecks =
1961expandBounds(PointerChecks, TheLoop, Loc, Exp,HoistRuntimeChecks);
1962
1963LLVMContext &Ctx = Loc->getContext();
1964IRBuilder ChkBuilder(Ctx,InstSimplifyFolder(Loc->getDataLayout()));
1965 ChkBuilder.SetInsertPoint(Loc);
1966// Our instructions might fold to a constant.
1967Value *MemoryRuntimeCheck =nullptr;
1968
1969for (constauto &[A,B] : ExpandedChecks) {
1970// Check if two pointers (A and B) conflict where conflict is computed as:
1971// start(A) <= end(B) && start(B) <= end(A)
1972
1973assert((A.Start->getType()->getPointerAddressSpace() ==
1974B.End->getType()->getPointerAddressSpace()) &&
1975 (B.Start->getType()->getPointerAddressSpace() ==
1976A.End->getType()->getPointerAddressSpace()) &&
1977"Trying to bounds check pointers with different address spaces");
1978
1979// [A|B].Start points to the first accessed byte under base [A|B].
1980// [A|B].End points to the last accessed byte, plus one.
1981// There is no conflict when the intervals are disjoint:
1982// NoConflict = (B.Start >= A.End) || (A.Start >= B.End)
1983//
1984// bound0 = (B.Start < A.End)
1985// bound1 = (A.Start < B.End)
1986// IsConflict = bound0 & bound1
1987Value *Cmp0 = ChkBuilder.CreateICmpULT(A.Start,B.End,"bound0");
1988Value *Cmp1 = ChkBuilder.CreateICmpULT(B.Start,A.End,"bound1");
1989Value *IsConflict = ChkBuilder.CreateAnd(Cmp0, Cmp1,"found.conflict");
1990if (A.StrideToCheck) {
1991Value *IsNegativeStride = ChkBuilder.CreateICmpSLT(
1992A.StrideToCheck, ConstantInt::get(A.StrideToCheck->getType(), 0),
1993"stride.check");
1994 IsConflict = ChkBuilder.CreateOr(IsConflict, IsNegativeStride);
1995 }
1996if (B.StrideToCheck) {
1997Value *IsNegativeStride = ChkBuilder.CreateICmpSLT(
1998B.StrideToCheck, ConstantInt::get(B.StrideToCheck->getType(), 0),
1999"stride.check");
2000 IsConflict = ChkBuilder.CreateOr(IsConflict, IsNegativeStride);
2001 }
2002if (MemoryRuntimeCheck) {
2003 IsConflict =
2004 ChkBuilder.CreateOr(MemoryRuntimeCheck, IsConflict,"conflict.rdx");
2005 }
2006 MemoryRuntimeCheck = IsConflict;
2007 }
2008
2009return MemoryRuntimeCheck;
2010}
2011
2012Value *llvm::addDiffRuntimeChecks(
2013Instruction *Loc,ArrayRef<PointerDiffInfo> Checks,SCEVExpander &Expander,
2014function_ref<Value *(IRBuilderBase &,unsigned)> GetVF,unsigned IC) {
2015
2016LLVMContext &Ctx = Loc->getContext();
2017IRBuilder ChkBuilder(Ctx,InstSimplifyFolder(Loc->getDataLayout()));
2018 ChkBuilder.SetInsertPoint(Loc);
2019// Our instructions might fold to a constant.
2020Value *MemoryRuntimeCheck =nullptr;
2021
2022auto &SE = *Expander.getSE();
2023// Map to keep track of created compares, The key is the pair of operands for
2024// the compare, to allow detecting and re-using redundant compares.
2025DenseMap<std::pair<Value *, Value *>,Value *> SeenCompares;
2026for (constauto &[SrcStart, SinkStart, AccessSize, NeedsFreeze] : Checks) {
2027Type *Ty = SinkStart->getType();
2028// Compute VF * IC * AccessSize.
2029auto *VFTimesUFTimesSize =
2030 ChkBuilder.CreateMul(GetVF(ChkBuilder, Ty->getScalarSizeInBits()),
2031 ConstantInt::get(Ty, IC * AccessSize));
2032Value *Diff =
2033 Expander.expandCodeFor(SE.getMinusSCEV(SinkStart, SrcStart), Ty, Loc);
2034
2035// Check if the same compare has already been created earlier. In that case,
2036// there is no need to check it again.
2037Value *IsConflict = SeenCompares.lookup({Diff, VFTimesUFTimesSize});
2038if (IsConflict)
2039continue;
2040
2041 IsConflict =
2042 ChkBuilder.CreateICmpULT(Diff, VFTimesUFTimesSize,"diff.check");
2043 SeenCompares.insert({{Diff, VFTimesUFTimesSize}, IsConflict});
2044if (NeedsFreeze)
2045 IsConflict =
2046 ChkBuilder.CreateFreeze(IsConflict, IsConflict->getName() +".fr");
2047if (MemoryRuntimeCheck) {
2048 IsConflict =
2049 ChkBuilder.CreateOr(MemoryRuntimeCheck, IsConflict,"conflict.rdx");
2050 }
2051 MemoryRuntimeCheck = IsConflict;
2052 }
2053
2054return MemoryRuntimeCheck;
2055}
2056
2057std::optional<IVConditionInfo>
2058llvm::hasPartialIVCondition(constLoop &L,unsignedMSSAThreshold,
2059constMemorySSA &MSSA,AAResults &AA) {
2060auto *TI = dyn_cast<BranchInst>(L.getHeader()->getTerminator());
2061if (!TI || !TI->isConditional())
2062return {};
2063
2064auto *CondI = dyn_cast<Instruction>(TI->getCondition());
2065// The case with the condition outside the loop should already be handled
2066// earlier.
2067// Allow CmpInst and TruncInsts as they may be users of load instructions
2068// and have potential for partial unswitching
2069if (!CondI || !isa<CmpInst, TruncInst>(CondI) || !L.contains(CondI))
2070return {};
2071
2072SmallVector<Instruction *> InstToDuplicate;
2073 InstToDuplicate.push_back(CondI);
2074
2075SmallVector<Value *, 4> WorkList;
2076 WorkList.append(CondI->op_begin(), CondI->op_end());
2077
2078SmallVector<MemoryAccess *, 4> AccessesToCheck;
2079SmallVector<MemoryLocation, 4> AccessedLocs;
2080while (!WorkList.empty()) {
2081Instruction *I = dyn_cast<Instruction>(WorkList.pop_back_val());
2082if (!I || !L.contains(I))
2083continue;
2084
2085// TODO: support additional instructions.
2086if (!isa<LoadInst>(I) && !isa<GetElementPtrInst>(I))
2087return {};
2088
2089// Do not duplicate volatile and atomic loads.
2090if (auto *LI = dyn_cast<LoadInst>(I))
2091if (LI->isVolatile() || LI->isAtomic())
2092return {};
2093
2094 InstToDuplicate.push_back(I);
2095if (MemoryAccess *MA = MSSA.getMemoryAccess(I)) {
2096if (auto *MemUse = dyn_cast_or_null<MemoryUse>(MA)) {
2097// Queue the defining access to check for alias checks.
2098 AccessesToCheck.push_back(MemUse->getDefiningAccess());
2099 AccessedLocs.push_back(MemoryLocation::get(I));
2100 }else {
2101// MemoryDefs may clobber the location or may be atomic memory
2102// operations. Bail out.
2103return {};
2104 }
2105 }
2106 WorkList.append(I->op_begin(),I->op_end());
2107 }
2108
2109if (InstToDuplicate.empty())
2110return {};
2111
2112SmallVector<BasicBlock *, 4> ExitingBlocks;
2113 L.getExitingBlocks(ExitingBlocks);
2114auto HasNoClobbersOnPath =
2115 [&L, &AA, &AccessedLocs, &ExitingBlocks, &InstToDuplicate,
2116MSSAThreshold](BasicBlock *Succ,BasicBlock *Header,
2117SmallVector<MemoryAccess *, 4> AccessesToCheck)
2118 -> std::optional<IVConditionInfo> {
2119IVConditionInfoInfo;
2120// First, collect all blocks in the loop that are on a patch from Succ
2121// to the header.
2122SmallVector<BasicBlock *, 4> WorkList;
2123 WorkList.push_back(Succ);
2124 WorkList.push_back(Header);
2125SmallPtrSet<BasicBlock *, 4> Seen;
2126 Seen.insert(Header);
2127Info.PathIsNoop &=
2128all_of(*Header, [](Instruction &I) {return !I.mayHaveSideEffects(); });
2129
2130while (!WorkList.empty()) {
2131BasicBlock *Current = WorkList.pop_back_val();
2132if (!L.contains(Current))
2133continue;
2134constauto &SeenIns = Seen.insert(Current);
2135if (!SeenIns.second)
2136continue;
2137
2138Info.PathIsNoop &=all_of(
2139 *Current, [](Instruction &I) {return !I.mayHaveSideEffects(); });
2140 WorkList.append(succ_begin(Current),succ_end(Current));
2141 }
2142
2143// Require at least 2 blocks on a path through the loop. This skips
2144// paths that directly exit the loop.
2145if (Seen.size() < 2)
2146return {};
2147
2148// Next, check if there are any MemoryDefs that are on the path through
2149// the loop (in the Seen set) and they may-alias any of the locations in
2150// AccessedLocs. If that is the case, they may modify the condition and
2151// partial unswitching is not possible.
2152SmallPtrSet<MemoryAccess *, 4> SeenAccesses;
2153while (!AccessesToCheck.empty()) {
2154MemoryAccess *Current = AccessesToCheck.pop_back_val();
2155auto SeenI = SeenAccesses.insert(Current);
2156if (!SeenI.second || !Seen.contains(Current->getBlock()))
2157continue;
2158
2159// Bail out if exceeded the threshold.
2160if (SeenAccesses.size() >=MSSAThreshold)
2161return {};
2162
2163// MemoryUse are read-only accesses.
2164if (isa<MemoryUse>(Current))
2165continue;
2166
2167// For a MemoryDef, check if is aliases any of the location feeding
2168// the original condition.
2169if (auto *CurrentDef = dyn_cast<MemoryDef>(Current)) {
2170if (any_of(AccessedLocs, [&AA, CurrentDef](MemoryLocation &Loc) {
2171returnisModSet(
2172 AA.getModRefInfo(CurrentDef->getMemoryInst(), Loc));
2173 }))
2174return {};
2175 }
2176
2177for (Use &U : Current->uses())
2178 AccessesToCheck.push_back(cast<MemoryAccess>(U.getUser()));
2179 }
2180
2181// We could also allow loops with known trip counts without mustprogress,
2182// but ScalarEvolution may not be available.
2183Info.PathIsNoop &=isMustProgress(&L);
2184
2185// If the path is considered a no-op so far, check if it reaches a
2186// single exit block without any phis. This ensures no values from the
2187// loop are used outside of the loop.
2188if (Info.PathIsNoop) {
2189for (auto *Exiting : ExitingBlocks) {
2190if (!Seen.contains(Exiting))
2191continue;
2192for (auto *Succ :successors(Exiting)) {
2193if (L.contains(Succ))
2194continue;
2195
2196Info.PathIsNoop &= Succ->phis().empty() &&
2197 (!Info.ExitForPath ||Info.ExitForPath == Succ);
2198if (!Info.PathIsNoop)
2199break;
2200assert((!Info.ExitForPath ||Info.ExitForPath == Succ) &&
2201"cannot have multiple exit blocks");
2202Info.ExitForPath = Succ;
2203 }
2204 }
2205 }
2206if (!Info.ExitForPath)
2207Info.PathIsNoop =false;
2208
2209Info.InstToDuplicate = InstToDuplicate;
2210returnInfo;
2211 };
2212
2213// If we branch to the same successor, partial unswitching will not be
2214// beneficial.
2215if (TI->getSuccessor(0) == TI->getSuccessor(1))
2216return {};
2217
2218if (autoInfo = HasNoClobbersOnPath(TI->getSuccessor(0), L.getHeader(),
2219 AccessesToCheck)) {
2220Info->KnownValue =ConstantInt::getTrue(TI->getContext());
2221returnInfo;
2222 }
2223if (autoInfo = HasNoClobbersOnPath(TI->getSuccessor(1), L.getHeader(),
2224 AccessesToCheck)) {
2225Info->KnownValue =ConstantInt::getFalse(TI->getContext());
2226returnInfo;
2227 }
2228
2229return {};
2230}
Poison
@ Poison
Definition:AArch64AsmPrinter.cpp:72
Select
AMDGPU Register Bank Select
Definition:AMDGPURegBankSelect.cpp:71
AliasAnalysis.h
BasicAliasAnalysis.h
This is the interface for LLVM's primary stateless and local alias analysis.
blocks
bbsections Prepares for basic block by splitting functions into clusters of basic blocks
Definition:BasicBlockSections.cpp:140
BasicBlockUtils.h
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Info
Analysis containing CSE Info
Definition:CSEInfo.cpp:27
DIBuilder.h
Idx
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Definition:DeadArgumentElimination.cpp:353
Debug.h
LLVM_DEBUG
#define LLVM_DEBUG(...)
Definition:Debug.h:106
DenseSet.h
This file defines the DenseSet and SmallDenseSet classes.
DomTreeUpdater.h
Dominators.h
Enable
@ Enable
Definition:DwarfDebug.cpp:87
Name
std::string Name
Definition:ELFObjHandler.cpp:77
End
bool End
Definition:ELF_riscv.cpp:480
Check
#define Check(C,...)
Definition:GenericConvergenceVerifierImpl.h:34
GlobalsModRef.h
This is the interface for a simple mod/ref and alias analysis over globals.
Cleanup
static const HTTPClientCleanup Cleanup
Definition:HTTPClient.cpp:42
Loops
Hexagon Hardware Loops
Definition:HexagonHardwareLoops.cpp:373
IntrinsicInst.h
Module.h
Module.h This file contains the declarations for the Module class.
Users
iv Induction Variable Users
Definition:IVUsers.cpp:48
ReplaceExitValue
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")))
InitializePasses.h
InstSimplifyFolder.h
Instructions.h
HoistRuntimeChecks
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))
LoopAccessAnalysis.h
LoopInfo.h
LoopPass.h
getEstimatedTripCount
static std::optional< uint64_t > getEstimatedTripCount(BranchInst *ExitingBranch, Loop *L, uint64_t &OrigExitWeight)
Return the estimated trip count for any exiting branch which dominates the loop latch.
Definition:LoopUtils.cpp:823
hasHardUserWithinLoop
static bool hasHardUserWithinLoop(const Loop *L, const Instruction *I)
Definition:LoopUtils.cpp:1443
LLVMLoopDisableLICM
static const char * LLVMLoopDisableLICM
Definition:LoopUtils.cpp:55
expandBounds
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.
Definition:LoopUtils.cpp:1866
canLoopBeDeleted
static bool canLoopBeDeleted(Loop *L, SmallVector< RewritePhi, 8 > &RewritePhiSet)
Definition:LoopUtils.cpp:1484
LLVMLoopDisableNonforced
static const char * LLVMLoopDisableNonforced
Definition:LoopUtils.cpp:54
createStringMetadata
static MDNode * createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V)
Create MDNode for input string.
Definition:LoopUtils.cpp:203
getExpectedExitLoopLatchBranch
static BranchInst * getExpectedExitLoopLatchBranch(Loop *L)
Checks if L has an exiting latch branch.
Definition:LoopUtils.cpp:805
checkIsIndPhi
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...
Definition:LoopUtils.cpp:1538
LoopUtils.h
I
#define I(x, y, z)
Definition:MD5.cpp:58
H
#define H(x, y, z)
Definition:MD5.cpp:57
MDBuilder.h
MemorySSAUpdater.h
MemorySSA.h
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
Signed
@ Signed
Definition:NVPTXISelLowering.cpp:4789
High
uint64_t High
Definition:NVVMIntrRange.cpp:51
P
#define P(N)
INITIALIZE_PASS_DEPENDENCY
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition:PassSupport.h:55
Pass.h
PatternMatch.h
PriorityWorklist.h
This file provides a priority worklist.
ProfDataUtils.h
This file contains the declarations for profiling metadata utility functions.
Cond
const SmallVectorImpl< MachineOperand > & Cond
Definition:RISCVRedundantCopyElimination.cpp:75
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ScalarEvolutionAliasAnalysis.h
This is the interface for a SCEV-based alias analysis.
ScalarEvolutionExpander.h
ScalarEvolutionExpressions.h
ScalarEvolution.h
ScopeExit.h
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
SetVector.h
This file implements a set that has insertion order iteration characteristics.
MSSAThreshold
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)
SmallPtrSet.h
This file defines the SmallPtrSet class.
SmallVector.h
This file defines the SmallVector class.
Local.h
ValueHandle.h
Rewriter
Virtual Register Rewriter
Definition:VirtRegMap.cpp:261
llvm::AAResultsWrapperPass
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Definition:AliasAnalysis.h:981
llvm::AAResults
Definition:AliasAnalysis.h:314
llvm::AAResults::getModRefInfo
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
Check whether or not an instruction may read or write the optionally specified memory location.
Definition:AliasAnalysis.h:508
llvm::APFloat::getLargest
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition:APFloat.h:1140
llvm::APInt
Class for arbitrary precision integers.
Definition:APInt.h:78
llvm::APInt::getMaxValue
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition:APInt.h:206
llvm::APInt::getSignedMaxValue
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition:APInt.h:209
llvm::APInt::getMinValue
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition:APInt.h:216
llvm::APInt::getSignedMinValue
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition:APInt.h:219
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition:PassAnalysisSupport.h:47
llvm::AnalysisUsage::addRequiredID
AnalysisUsage & addRequiredID(const void *ID)
Definition:Pass.cpp:270
llvm::AnalysisUsage::addPreservedID
AnalysisUsage & addPreservedID(const void *ID)
Definition:PassAnalysisSupport.h:88
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition:PassAnalysisSupport.h:75
llvm::AnalysisUsage::addPreserved
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Definition:PassAnalysisSupport.h:98
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition:ArrayRef.h:41
llvm::BasicAAWrapperPass
Legacy wrapper pass to provide the BasicAAResult object.
Definition:BasicAliasAnalysis.h:164
llvm::BasicBlock
LLVM Basic Block Representation.
Definition:BasicBlock.h:61
llvm::BasicBlock::begin
iterator begin()
Instruction iterator methods.
Definition:BasicBlock.h:451
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition:BasicBlock.h:177
llvm::BasicBlock::getContext
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition:BasicBlock.cpp:168
llvm::BasicBlock::getTerminator
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:240
llvm::BinaryOperator
Definition:InstrTypes.h:170
llvm::BranchInst
Conditional or Unconditional Branch instruction.
Definition:Instructions.h:3016
llvm::BranchInst::getNumSuccessors
unsigned getNumSuccessors() const
Definition:Instructions.h:3102
llvm::BranchInst::getSuccessor
BasicBlock * getSuccessor(unsigned i) const
Definition:Instructions.h:3104
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition:InstrTypes.h:673
llvm::CmpInst::ICMP_SLT
@ ICMP_SLT
signed less than
Definition:InstrTypes.h:702
llvm::CmpInst::FCMP_OLT
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition:InstrTypes.h:679
llvm::CmpInst::FCMP_OGT
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition:InstrTypes.h:677
llvm::CmpInst::ICMP_UGT
@ ICMP_UGT
unsigned greater than
Definition:InstrTypes.h:696
llvm::CmpInst::ICMP_SGT
@ ICMP_SGT
signed greater than
Definition:InstrTypes.h:700
llvm::CmpInst::ICMP_ULT
@ ICMP_ULT
unsigned less than
Definition:InstrTypes.h:698
llvm::CmpInst::ICMP_NE
@ ICMP_NE
not equal
Definition:InstrTypes.h:695
llvm::ConstantAsMetadata::get
static ConstantAsMetadata * get(Constant *C)
Definition:Metadata.h:532
llvm::ConstantExpr::getIntrinsicIdentity
static Constant * getIntrinsicIdentity(Intrinsic::ID, Type *Ty)
Definition:Constants.cpp:2737
llvm::ConstantExpr::getBinOpIdentity
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
Definition:Constants.cpp:2692
llvm::ConstantFP::getInfinity
static Constant * getInfinity(Type *Ty, bool Negative=false)
Definition:Constants.cpp:1103
llvm::ConstantFP::getQNaN
static Constant * getQNaN(Type *Ty, bool Negative=false, APInt *Payload=nullptr)
Definition:Constants.cpp:1035
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition:Constants.h:83
llvm::ConstantInt::getTrue
static ConstantInt * getTrue(LLVMContext &Context)
Definition:Constants.cpp:866
llvm::ConstantInt::getFalse
static ConstantInt * getFalse(LLVMContext &Context)
Definition:Constants.cpp:873
llvm::ConstantInt::getSExtValue
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:163
llvm::DIBuilder
Definition:DIBuilder.h:45
llvm::DWARFExpression::Operation
This class represents an Operation in the Expression.
Definition:DWARFExpression.h:32
llvm::DWARFExpression::Operation::getNumOperands
uint64_t getNumOperands() const
Definition:DWARFExpression.h:90
llvm::DbgVariableRecord
Record of a variable value-assignment, aka a non instruction representation of the dbg....
Definition:DebugProgramInstruction.h:270
llvm::DebugVariable
Identifies a unique instance of a variable.
Definition:DebugInfoMetadata.h:4024
llvm::DenseMapBase::lookup
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:194
llvm::DenseMapBase::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition:DenseMap.h:211
llvm::DenseMap
Definition:DenseMap.h:727
llvm::DomTreeNodeBase< BasicBlock >
llvm::DomTreeNodeBase::children
iterator_range< iterator > children()
Definition:GenericDomTree.h:84
llvm::DomTreeUpdater
Definition:DomTreeUpdater.h:30
llvm::DominatorTreeBase::getNode
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Definition:GenericDomTree.h:401
llvm::DominatorTreeWrapperPass
Legacy analysis pass which computes a DominatorTree.
Definition:Dominators.h:317
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition:Dominators.h:162
llvm::DominatorTree::isReachableFromEntry
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition:Dominators.cpp:321
llvm::ElementCount::get
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition:TypeSize.h:317
llvm::FastMathFlags
Convenience struct for specifying and reasoning about fast-math flags.
Definition:FMF.h:20
llvm::FastMathFlags::noSignedZeros
bool noSignedZeros() const
Definition:FMF.h:68
llvm::FastMathFlags::noNaNs
bool noNaNs() const
Definition:FMF.h:66
llvm::GenericDomTreeUpdater::applyUpdates
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
Definition:GenericDomTreeUpdaterImpl.h:59
llvm::GlobalsAAWrapperPass
Legacy wrapper pass to provide the GlobalsAAResult object.
Definition:GlobalsModRef.h:142
llvm::IRBuilderBase::FastMathFlagGuard
Definition:IRBuilder.h:416
llvm::IRBuilderBase
Common base class shared among various IRBuilders.
Definition:IRBuilder.h:113
llvm::IRBuilderBase::CreateFAddReduce
CallInst * CreateFAddReduce(Value *Acc, Value *Src)
Create a sequential vector fadd reduction intrinsic of the source vector.
Definition:IRBuilder.cpp:402
llvm::IRBuilderBase::CreateICmpULT
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:2286
llvm::IRBuilderBase::CreateExtractElement
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition:IRBuilder.h:2499
llvm::IRBuilderBase::CreateUnreachable
UnreachableInst * CreateUnreachable()
Definition:IRBuilder.h:1306
llvm::IRBuilderBase::CreateSelect
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition:IRBuilder.cpp:1053
llvm::IRBuilderBase::CreateFreeze
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition:IRBuilder.h:2574
llvm::IRBuilderBase::CreateOrReduce
CallInst * CreateOrReduce(Value *Src)
Create a vector int OR reduction intrinsic of the source vector.
Definition:IRBuilder.cpp:424
llvm::IRBuilderBase::CreateIntrinsic
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition:IRBuilder.cpp:900
llvm::IRBuilderBase::getInt32
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition:IRBuilder.h:505
llvm::IRBuilderBase::CreateCmp
Value * CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition:IRBuilder.h:2404
llvm::IRBuilderBase::CreateCondBr
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
Definition:IRBuilder.h:1164
llvm::IRBuilderBase::CreateUnaryIntrinsic
CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
Definition:IRBuilder.cpp:881
llvm::IRBuilderBase::getFastMathFlags
FastMathFlags getFastMathFlags() const
Get the flags to be applied to created floating point ops.
Definition:IRBuilder.h:319
llvm::IRBuilderBase::CreateShuffleVector
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition:IRBuilder.h:2533
llvm::IRBuilderBase::CreateAnd
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:1518
llvm::IRBuilderBase::CreateIntMaxReduce
CallInst * CreateIntMaxReduce(Value *Src, bool IsSigned=false)
Create a vector integer max reduction intrinsic of the source vector.
Definition:IRBuilder.cpp:432
llvm::IRBuilderBase::getFalse
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition:IRBuilder.h:490
llvm::IRBuilderBase::CreateOr
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:1540
llvm::IRBuilderBase::CreateBinOp
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition:IRBuilder.h:1671
llvm::IRBuilderBase::CreateBr
BranchInst * CreateBr(BasicBlock *Dest)
Create an unconditional 'br label X' instruction.
Definition:IRBuilder.h:1158
llvm::IRBuilderBase::CreateICmpSLT
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition:IRBuilder.h:2302
llvm::IRBuilderBase::SetInsertPoint
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition:IRBuilder.h:199
llvm::IRBuilderBase::CreateFMulReduce
CallInst * CreateFMulReduce(Value *Acc, Value *Src)
Create a sequential vector fmul reduction intrinsic of the source vector.
Definition:IRBuilder.cpp:407
llvm::IRBuilderBase::CreateMul
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition:IRBuilder.h:1404
llvm::IRBuilder
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition:IRBuilder.h:2705
llvm::InductionDescriptor
A struct for saving information about induction variables.
Definition:IVDescriptors.h:334
llvm::InductionDescriptor::isInductionPHI
static 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.
Definition:IVDescriptors.cpp:1513
llvm::InstSimplifyFolder
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
Definition:InstSimplifyFolder.h:35
llvm::Instruction
Definition:Instruction.h:68
llvm::Instruction::getNumSuccessors
unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
Definition:Instruction.cpp:1275
llvm::Instruction::eraseFromParent
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition:Instruction.cpp:94
llvm::Instruction::mayHaveSideEffects
bool mayHaveSideEffects() const LLVM_READONLY
Return true if the instruction may have side effects.
Definition:Instruction.cpp:1185
llvm::Instruction::setMetadata
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition:Metadata.cpp:1679
llvm::Instruction::BinaryOps
BinaryOps
Definition:Instruction.h:989
llvm::Instruction::copyMetadata
void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
Definition:Instruction.cpp:1345
llvm::Instruction::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Definition:Instruction.cpp:76
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition:LLVMContext.h:67
llvm::LPPassManager
Definition:LoopPass.h:76
llvm::LPPassManager::addLoop
void addLoop(Loop &L)
Definition:LoopPass.cpp:77
llvm::LoopBase::contains
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition:GenericLoopInfo.h:124
llvm::LoopBase::getLoopLatch
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
Definition:GenericLoopInfoImpl.h:256
llvm::LoopBase::getHeader
BlockT * getHeader() const
Definition:GenericLoopInfo.h:90
llvm::LoopBase< BasicBlock, Loop >::iterator
std::vector< Loop * >::const_iterator iterator
Definition:GenericLoopInfo.h:153
llvm::LoopBase::getParentLoop
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
Definition:GenericLoopInfo.h:99
llvm::LoopInfoBase::addTopLevelLoop
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
Definition:GenericLoopInfo.h:663
llvm::LoopInfoBase::end
iterator end() const
Definition:GenericLoopInfo.h:582
llvm::LoopInfoBase::removeBlock
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
Definition:GenericLoopInfo.h:671
llvm::LoopInfoBase::AllocateLoop
LoopT * AllocateLoop(ArgsTy &&...Args)
Definition:GenericLoopInfo.h:570
llvm::LoopInfoBase::removeLoop
LoopT * removeLoop(iterator I)
This removes the specified top-level loop from this loop info object.
Definition:GenericLoopInfo.h:633
llvm::LoopInfoBase::getLoopFor
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition:GenericLoopInfo.h:606
llvm::LoopInfoBase::destroy
void destroy(LoopT *L)
Destroy a loop that has been removed from the LoopInfo nest.
Definition:GenericLoopInfo.h:710
llvm::LoopInfoWrapperPass
The legacy pass manager's analysis pass to compute loop information.
Definition:LoopInfo.h:593
llvm::LoopInfo
Definition:LoopInfo.h:407
llvm::LoopInfo::replacementPreservesLCSSAForm
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form.
Definition:LoopInfo.h:439
llvm::LoopInfo::erase
void erase(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
Definition:LoopInfo.cpp:887
llvm::Loop
Represents a single loop in the control flow graph.
Definition:LoopInfo.h:39
llvm::Loop::setLoopID
void setLoopID(MDNode *LoopID) const
Set the llvm.loop loop id metadata for this loop.
Definition:LoopInfo.cpp:526
llvm::Loop::getLoopID
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
Definition:LoopInfo.cpp:502
llvm::MDBuilder
Definition:MDBuilder.h:36
llvm::MDBuilder::createBranchWeights
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
Definition:MDBuilder.cpp:37
llvm::MDNode
Metadata node.
Definition:Metadata.h:1073
llvm::MDNode::replaceOperandWith
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition:Metadata.cpp:1077
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition:Metadata.h:1434
llvm::MDNode::operands
ArrayRef< MDOperand > operands() const
Definition:Metadata.h:1432
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition:Metadata.h:1549
llvm::MDNode::getNumOperands
unsigned getNumOperands() const
Return number of MDNode operands.
Definition:Metadata.h:1440
llvm::MDNode::getContext
LLVMContext & getContext() const
Definition:Metadata.h:1237
llvm::MDOperand
Tracking metadata reference owned by Metadata.
Definition:Metadata.h:895
llvm::MDString
A single uniqued string.
Definition:Metadata.h:724
llvm::MDString::getString
StringRef getString() const
Definition:Metadata.cpp:616
llvm::MDString::get
static MDString * get(LLVMContext &Context, StringRef Str)
Definition:Metadata.cpp:606
llvm::MDTuple
Tuple of metadata.
Definition:Metadata.h:1479
llvm::MemoryAccess
Definition:MemorySSA.h:142
llvm::MemoryAccess::getBlock
BasicBlock * getBlock() const
Definition:MemorySSA.h:161
llvm::MemoryLocation
Representation for a specific memory location.
Definition:MemoryLocation.h:227
llvm::MemoryLocation::get
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
Definition:MemoryLocation.cpp:35
llvm::MemorySSAUpdater
Definition:MemorySSAUpdater.h:54
llvm::MemorySSAWrapperPass
Legacy analysis pass which computes MemorySSA.
Definition:MemorySSA.h:985
llvm::MemorySSA
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition:MemorySSA.h:701
llvm::MemorySSA::verifyMemorySSA
void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
Definition:MemorySSA.cpp:1905
llvm::MemorySSA::getMemoryAccess
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
Definition:MemorySSA.h:719
llvm::Metadata
Root of the metadata hierarchy.
Definition:Metadata.h:62
llvm::PHINode
Definition:Instructions.h:2600
llvm::PHINode::setIncomingValue
void setIncomingValue(unsigned i, Value *V)
Definition:Instructions.h:2678
llvm::PHINode::getIncomingBlock
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Definition:Instructions.h:2695
llvm::PHINode::getIncomingValue
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
Definition:Instructions.h:2675
llvm::PHINode::getBasicBlockIndex
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
Definition:Instructions.h:2768
llvm::PHINode::getNumIncomingValues
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Definition:Instructions.h:2671
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition:PassRegistry.h:37
llvm::PoisonValue::get
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition:Constants.cpp:1878
llvm::PriorityWorklist::insert
bool insert(const T &X)
Insert a new element into the PriorityWorklist.
Definition:PriorityWorklist.h:90
llvm::RecurrenceDescriptor
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
Definition:IVDescriptors.h:77
llvm::RecurrenceDescriptor::isAnyOfRecurrenceKind
static bool isAnyOfRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
Definition:IVDescriptors.h:252
llvm::RecurrenceDescriptor::isFindLastIVRecurrenceKind
static bool isFindLastIVRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
Definition:IVDescriptors.h:258
llvm::RecurrenceDescriptor::isMinMaxRecurrenceKind
static bool isMinMaxRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is any min/max kind.
Definition:IVDescriptors.h:246
llvm::Registry
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition:Registry.h:44
llvm::SCEVAAWrapperPass
Legacy wrapper pass to provide the SCEVAAResult object.
Definition:ScalarEvolutionAliasAnalysis.h:56
llvm::SCEVExpander
This class uses information about analyze scalars to rewrite expressions in canonical form.
Definition:ScalarEvolutionExpander.h:63
llvm::SCEVExpander::getSE
ScalarEvolution * getSE()
Definition:ScalarEvolutionExpander.h:220
llvm::SCEVExpander::expandCodeFor
Value * expandCodeFor(const SCEV *SH, Type *Ty, BasicBlock::iterator I)
Insert code to directly compute the specified SCEV expression into the program.
Definition:ScalarEvolutionExpander.cpp:1443
llvm::SCEV
This class represents an analyzed expression in the program.
Definition:ScalarEvolution.h:71
llvm::SCEV::getType
Type * getType() const
Return the LLVM type of this SCEV expression.
Definition:ScalarEvolution.cpp:386
llvm::ScalarEvolutionWrapperPass
Definition:ScalarEvolution.h:2352
llvm::ScalarEvolution
The main scalar evolution driver.
Definition:ScalarEvolution.h:447
llvm::ScalarEvolution::isKnownNonNegative
bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
Definition:ScalarEvolution.cpp:10952
llvm::ScalarEvolution::isLoopEntryGuardedByCond
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.
Definition:ScalarEvolution.cpp:11721
llvm::ScalarEvolution::getSCEVAtScope
const SCEV * getSCEVAtScope(const SCEV *S, const Loop *L)
Return a SCEV expression for the specified value at the specified scope in the program.
Definition:ScalarEvolution.cpp:9856
llvm::ScalarEvolution::getZero
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
Definition:ScalarEvolution.h:653
llvm::ScalarEvolution::getConstant
const SCEV * getConstant(ConstantInt *V)
Definition:ScalarEvolution.cpp:473
llvm::ScalarEvolution::getSCEV
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
Definition:ScalarEvolution.cpp:4547
llvm::ScalarEvolution::forgetLoop
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...
Definition:ScalarEvolution.cpp:8496
llvm::ScalarEvolution::isLoopInvariant
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
Definition:ScalarEvolution.cpp:14100
llvm::ScalarEvolution::getLoopDisposition
LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L)
Return the "disposition" of the given SCEV with respect to the given loop.
Definition:ScalarEvolution.cpp:14010
llvm::ScalarEvolution::isSCEVable
bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
Definition:ScalarEvolution.cpp:4441
llvm::ScalarEvolution::forgetValue
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...
Definition:ScalarEvolution.cpp:8542
llvm::ScalarEvolution::forgetBlockAndLoopDispositions
void forgetBlockAndLoopDispositions(Value *V=nullptr)
Called when the client has changed the disposition of values in a loop or block.
Definition:ScalarEvolution.cpp:8597
llvm::ScalarEvolution::LoopDisposition
LoopDisposition
An enum describing the relationship between a SCEV and a loop.
Definition:ScalarEvolution.h:452
llvm::ScalarEvolution::LoopInvariant
@ LoopInvariant
The SCEV is loop-invariant.
Definition:ScalarEvolution.h:454
llvm::ScalarEvolution::isAvailableAtLoopEntry
bool isAvailableAtLoopEntry(const SCEV *S, const Loop *L)
Determine if the SCEV can be evaluated at loop's entry.
Definition:ScalarEvolution.cpp:2521
llvm::ScalarEvolution::getExitCount
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...
Definition:ScalarEvolution.cpp:8314
llvm::ScalarEvolution::applyLoopGuards
const SCEV * applyLoopGuards(const SCEV *Expr, const Loop *L)
Try to apply information from loop guards for L to Expr.
Definition:ScalarEvolution.cpp:15967
llvm::SelectInst
This class represents the LLVM 'select' instruction.
Definition:Instructions.h:1657
llvm::SmallDenseSet
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition:DenseSet.h:298
llvm::SmallPriorityWorklist
A version of PriorityWorklist that selects small size optimized data structures for the vector and ma...
Definition:PriorityWorklist.h:257
llvm::SmallPtrSetImplBase::size
size_type size() const
Definition:SmallPtrSet.h:94
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition:SmallPtrSet.h:384
llvm::SmallPtrSetImpl::contains
bool contains(ConstPtrType Ptr) const
Definition:SmallPtrSet.h:458
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition:SmallPtrSet.h:519
llvm::SmallSetVector
A SetVector that performs no allocations if smaller than a certain size.
Definition:SetVector.h:370
llvm::SmallVectorBase::empty
bool empty() const
Definition:SmallVector.h:81
llvm::SmallVectorBase::size
size_t size() const
Definition:SmallVector.h:78
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition:SmallVector.h:573
llvm::SmallVectorImpl::pop_back_val
T pop_back_val()
Definition:SmallVector.h:673
llvm::SmallVectorImpl::emplace_back
reference emplace_back(ArgTypes &&... Args)
Definition:SmallVector.h:937
llvm::SmallVectorImpl::append
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition:SmallVector.h:683
llvm::SmallVectorImpl::clear
void clear()
Definition:SmallVector.h:610
llvm::SmallVectorTemplateBase::push_back
void push_back(const T &Elt)
Definition:SmallVector.h:413
llvm::SmallVectorTemplateCommon::end
iterator end()
Definition:SmallVector.h:269
llvm::SmallVectorTemplateCommon::begin
iterator begin()
Definition:SmallVector.h:267
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::StringRef::starts_with
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition:StringRef.h:265
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition:TargetLibraryInfo.h:280
llvm::TargetTransformInfo
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Definition:TargetTransformInfo.h:212
llvm::TargetTransformInfo::ReductionShuffle
ReductionShuffle
Definition:TargetTransformInfo.h:1788
llvm::TrackingVH
Value handle that tracks a Value across RAUW.
Definition:ValueHandle.h:331
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
llvm::Type::getFltSemantics
const fltSemantics & getFltSemantics() const
llvm::Type::isIntOrIntVectorTy
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition:Type.h:243
llvm::Type::getScalarSizeInBits
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
llvm::Type::isIntegerTy
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition:Type.h:237
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition:Use.h:43
llvm::User
Definition:User.h:44
llvm::ValueMap< const Value *, WeakTrackingVH >
llvm::Value
LLVM Value Representation.
Definition:Value.h:74
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition:Value.h:255
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition:Value.cpp:534
llvm::Value::users
iterator_range< user_iterator > users()
Definition:Value.h:421
llvm::Value::use_empty
bool use_empty() const
Definition:Value.h:344
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition:Value.cpp:1075
llvm::Value::uses
iterator_range< use_iterator > uses()
Definition:Value.h:376
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition:Value.cpp:309
llvm::VectorBuilder
Definition:VectorBuilder.h:25
llvm::VectorBuilder::createSimpleReduction
Value * createSimpleReduction(Intrinsic::ID RdxID, Type *ValTy, ArrayRef< Value * > VecOpArray, const Twine &Name=Twine())
Emit a VP reduction intrinsic call for recurrence kind.
Definition:VectorBuilder.cpp:63
llvm::detail::DenseSetImpl::insert
std::pair< iterator, bool > insert(const ValueT &V)
Definition:DenseSet.h:213
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition:STLFunctionalExtras.h:37
llvm::ilist_detail::node_parent_access::getParent
const ParentTy * getParent() const
Definition:ilist_node.h:32
uint64_t
unsigned
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition:CallingConv.h:24
llvm::PatternMatch
Definition:PatternMatch.h:47
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::drop_begin
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition:STLExtras.h:329
llvm::createSimpleReduction
Value * createSimpleReduction(IRBuilderBase &B, Value *Src, RecurKind RdxKind)
Create a reduction of the given vector.
Definition:LoopUtils.cpp:1278
llvm::getOptionalElementCountLoopAttribute
std::optional< ElementCount > getOptionalElementCountLoopAttribute(const Loop *TheLoop)
Find a combination of metadata ("llvm.loop.vectorize.width" and "llvm.loop.vectorize....
Definition:LoopUtils.cpp:250
llvm::ThreadPriority::Low
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
llvm::addRuntimeChecks
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.
Definition:LoopUtils.cpp:1954
llvm::find
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:1759
llvm::getLoopEstimatedTripCount
std::optional< unsigned > getLoopEstimatedTripCount(Loop *L, unsigned *EstimatedLoopInvocationWeight=nullptr)
Returns a loop's estimated trip count based on branch weight metadata.
Definition:LoopUtils.cpp:850
llvm::all_of
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1739
llvm::PseudoProbeType::Block
@ Block
llvm::getMinMaxReductionIntrinsicOp
Intrinsic::ID getMinMaxReductionIntrinsicOp(Intrinsic::ID RdxID)
Returns the min/max intrinsic used when expanding a min/max reduction.
Definition:LoopUtils.cpp:989
llvm::getBooleanLoopAttribute
bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name)
Returns true if Name is applied to TheLoop and enabled.
Definition:LoopInfo.cpp:1109
llvm::RuntimePointerCheck
std::pair< const RuntimeCheckingPtrGroup *, const RuntimeCheckingPtrGroup * > RuntimePointerCheck
A memcheck which made up of a pair of grouped pointers.
Definition:LoopAccessAnalysis.h:441
llvm::make_scope_exit
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition:ScopeExit.h:59
llvm::isKnownNonPositiveInLoop
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.
Definition:LoopUtils.cpp:1409
llvm::getOptionalBoolLoopAttribute
std::optional< bool > getOptionalBoolLoopAttribute(const Loop *TheLoop, StringRef Name)
Definition:LoopInfo.cpp:1091
llvm::appendReversedLoopsToWorklist
void appendReversedLoopsToWorklist(RangeT &&, SmallPriorityWorklist< Loop *, 4 > &)
Utility that implements appending of loops onto a worklist given a range.
Definition:LoopUtils.cpp:1789
llvm::AlignStyle::Right
@ Right
llvm::AlignStyle::Left
@ Left
llvm::successors
auto successors(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1376
llvm::initializeLoopPassPass
void initializeLoopPassPass(PassRegistry &)
Manually defined generic "LoopPass" dependency initialization.
Definition:LoopUtils.cpp:189
llvm::formLCSSARecursively
bool formLCSSARecursively(Loop &L, const DominatorTree &DT, const LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
Definition:LCSSA.cpp:465
llvm::getReductionIdentity
Value * getReductionIdentity(Intrinsic::ID RdxID, Type *Ty, FastMathFlags FMF)
Given information about an @llvm.vector.reduce.
Definition:LoopUtils.cpp:1228
llvm::makeFollowupLoopID
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.
Definition:LoopUtils.cpp:263
llvm::getArithmeticReductionInstruction
unsigned getArithmeticReductionInstruction(Intrinsic::ID RdxID)
Returns the arithmetic instruction opcode used when expanding a reduction.
Definition:LoopUtils.cpp:960
llvm::make_early_inc_range
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:657
llvm::LCSSAID
char & LCSSAID
Definition:LCSSA.cpp:542
llvm::LoopSimplifyID
char & LoopSimplifyID
Definition:LoopSimplify.cpp:784
llvm::createMinMaxOp
Value * createMinMaxOp(IRBuilderBase &Builder, RecurKind RK, Value *Left, Value *Right)
Returns a Min/Max operation corresponding to MinMaxRecurrenceKind.
Definition:LoopUtils.cpp:1076
llvm::collectChildrenInLoop
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.
Definition:LoopUtils.cpp:449
llvm::addStringMetadataToLoop
void addStringMetadataToLoop(Loop *TheLoop, const char *MDString, unsigned V=0)
Set input string into loop metadata by keeping other values intact.
Definition:LoopUtils.cpp:214
llvm::cannotBeMaxInLoop
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.
Definition:LoopUtils.cpp:1427
llvm::createAnyOfReduction
Value * createAnyOfReduction(IRBuilderBase &B, Value *Src, const RecurrenceDescriptor &Desc, PHINode *OrigPhi)
Create a reduction of the given vector Src for a reduction of the kind RecurKind::IAnyOf or RecurKind...
Definition:LoopUtils.cpp:1176
llvm::divideNearest
constexpr T divideNearest(U Numerator, V Denominator)
Returns (Numerator / Denominator) rounded by round-half-up.
Definition:MathExtras.h:468
llvm::hasVectorizeTransformation
TransformationMode hasVectorizeTransformation(const Loop *L)
Definition:LoopUtils.cpp:391
llvm::transform
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:1952
llvm::any_of
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1746
llvm::isInstructionTriviallyDead
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:406
llvm::findDefsUsedOutsideOfLoop
SmallVector< Instruction *, 8 > findDefsUsedOutsideOfLoop(Loop *L)
Returns the instructions that use values defined in the loop.
Definition:LoopUtils.cpp:123
llvm::reverse
auto reverse(ContainerTy &&C)
Definition:STLExtras.h:420
llvm::getReductionIntrinsicID
constexpr Intrinsic::ID getReductionIntrinsicID(RecurKind RK)
Returns the llvm.vector.reduce intrinsic that corresponds to the recurrence kind.
Definition:LoopUtils.cpp:922
llvm::isMustProgress
bool isMustProgress(const Loop *L)
Return true if this loop can be assumed to make progress.
Definition:LoopInfo.cpp:1162
llvm::isPowerOf2_32
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition:MathExtras.h:292
llvm::isModSet
bool isModSet(const ModRefInfo MRI)
Definition:ModRef.h:48
llvm::hasUnrollAndJamTransformation
TransformationMode hasUnrollAndJamTransformation(const Loop *L)
Definition:LoopUtils.cpp:373
llvm::deleteDeadLoop
void deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE, LoopInfo *LI, MemorySSA *MSSA=nullptr)
This function deletes dead loops.
Definition:LoopUtils.cpp:484
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition:Debug.cpp:163
llvm::hasDisableAllTransformsHint
bool hasDisableAllTransformsHint(const Loop *L)
Look for the loop attribute that disables all transformation heuristic.
Definition:LoopUtils.cpp:344
llvm::createOrderedReduction
Value * createOrderedReduction(IRBuilderBase &B, const RecurrenceDescriptor &Desc, Value *Src, Value *Start)
Create an ordered reduction intrinsic using the given recurrence descriptor Desc.
Definition:LoopUtils.cpp:1341
llvm::SCEVCheapExpansionBudget
cl::opt< unsigned > SCEVCheapExpansionBudget
llvm::getShuffleReduction
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.
Definition:LoopUtils.cpp:1118
llvm::createReduction
Value * createReduction(IRBuilderBase &B, const RecurrenceDescriptor &Desc, Value *Src, PHINode *OrigPhi=nullptr)
Create a generic reduction using a recurrence descriptor Desc Fast-math-flags are propagated using th...
Definition:LoopUtils.cpp:1323
llvm::hasUnrollTransformation
TransformationMode hasUnrollTransformation(const Loop *L)
Definition:LoopUtils.cpp:352
llvm::hasDistributeTransformation
TransformationMode hasDistributeTransformation(const Loop *L)
Definition:LoopUtils.cpp:427
llvm::breakLoopBackedge
void breakLoopBackedge(Loop *L, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI, MemorySSA *MSSA)
Remove the backedge of the specified loop.
Definition:LoopUtils.cpp:725
llvm::getLoopAnalysisUsage
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
Definition:LoopUtils.cpp:141
llvm::propagateIRFlags
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...
Definition:LoopUtils.cpp:1368
llvm::isKnownPositiveInLoop
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.
Definition:LoopUtils.cpp:1402
llvm::changeToUnreachable
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:2909
llvm::succ_begin
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
Definition:RegionIterator.h:249
llvm::getOptionalIntLoopAttribute
std::optional< int > getOptionalIntLoopAttribute(const Loop *TheLoop, StringRef Name)
Find named metadata for a loop with an integer value.
Definition:LoopInfo.cpp:1113
llvm::SplitBlockPredecessors
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...
Definition:BasicBlockUtils.cpp:1419
llvm::IRMemLocation::First
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
llvm::getMinMaxReductionPredicate
CmpInst::Predicate getMinMaxReductionPredicate(RecurKind RK)
Returns the comparison predicate used when expanding a min/max reduction.
Definition:LoopUtils.cpp:1054
llvm::hasLICMVersioningTransformation
TransformationMode hasLICMVersioningTransformation(const Loop *L)
Definition:LoopUtils.cpp:437
llvm::VerifyMemorySSA
bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition:MemorySSA.cpp:84
llvm::createFindLastIVReduction
Value * createFindLastIVReduction(IRBuilderBase &B, Value *Src, const RecurrenceDescriptor &Desc)
Create a reduction of the given vector Src for a reduction of the kind RecurKind::IFindLastIV or Recu...
Definition:LoopUtils.cpp:1211
llvm::TransformationMode
TransformationMode
The mode sets how eager a transformation should be applied.
Definition:LoopUtils.h:277
llvm::TM_Unspecified
@ TM_Unspecified
The pass can use heuristics to determine whether a transformation should be applied.
Definition:LoopUtils.h:280
llvm::TM_SuppressedByUser
@ TM_SuppressedByUser
The transformation must not be applied.
Definition:LoopUtils.h:300
llvm::TM_ForcedByUser
@ TM_ForcedByUser
The transformation was directed by the user, e.g.
Definition:LoopUtils.h:294
llvm::TM_Disable
@ TM_Disable
The transformation should not be applied.
Definition:LoopUtils.h:286
llvm::TM_Enable
@ TM_Enable
The transformation should be applied without considering a cost model.
Definition:LoopUtils.h:283
llvm::succ_end
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
Definition:RegionIterator.h:254
llvm::hasDisableLICMTransformsHint
bool hasDisableLICMTransformsHint(const Loop *L)
Look for the loop attribute that disables the LICM transformation heuristics.
Definition:LoopUtils.cpp:348
llvm::RecurKind
RecurKind
These are the kinds of recurrences that we support.
Definition:IVDescriptors.h:33
llvm::setLoopEstimatedTripCount
bool setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount, unsigned EstimatedLoopInvocationWeight)
Set a loop's branch weight metadata to reflect that loop has EstimatedTripCount iterations and Estima...
Definition:LoopUtils.cpp:868
llvm::getRecurrenceIdentity
Value * getRecurrenceIdentity(RecurKind K, Type *Tp, FastMathFlags FMF)
Given information about an recurrence kind, return the identity for the @llvm.vector....
Definition:LoopUtils.cpp:1270
llvm::setProfileInfoAfterUnrolling
void setProfileInfoAfterUnrolling(Loop *OrigLoop, Loop *UnrolledLoop, Loop *RemainderLoop, uint64_t UF)
Set weights for UnrolledLoop and RemainderLoop based on weights for OrigLoop and the following distri...
Definition:LoopUtils.cpp:1761
llvm::formDedicatedExitBlocks
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:57
llvm::Op
DWARFExpression::Operation Op
Definition:DWARFExpression.cpp:22
llvm::appendLoopsToWorklist
void appendLoopsToWorklist(RangeT &&, SmallPriorityWorklist< Loop *, 4 > &)
Utility that implements appending of loops onto a worklist given a range.
Definition:LoopUtils.cpp:1814
llvm::isKnownNegativeInLoop
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.
Definition:LoopUtils.cpp:1388
llvm::BitWidth
constexpr unsigned BitWidth
Definition:BitmaskEnum.h:217
llvm::extractBranchWeights
bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
Definition:ProfDataUtils.cpp:170
llvm::hasIterationCountInvariantInParent
bool hasIterationCountInvariantInParent(Loop *L, ScalarEvolution &SE)
Check inner loop (L) backedge count is known to be invariant on all iterations of its outer loop.
Definition:LoopUtils.cpp:900
llvm::isAlmostDeadIV
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...
Definition:LoopUtils.cpp:470
llvm::predecessors
auto predecessors(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1377
llvm::rewriteLoopExitValues
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,...
Definition:LoopUtils.cpp:1549
llvm::addDiffRuntimeChecks
Value * addDiffRuntimeChecks(Instruction *Loc, ArrayRef< PointerDiffInfo > Checks, SCEVExpander &Expander, function_ref< Value *(IRBuilderBase &, unsigned)> GetVF, unsigned IC)
Definition:LoopUtils.cpp:2012
llvm::getMinMaxReductionRecurKind
RecurKind getMinMaxReductionRecurKind(Intrinsic::ID RdxID)
Returns the recurence kind used when expanding a min/max reduction.
Definition:LoopUtils.cpp:1035
llvm::ReplaceExitVal
ReplaceExitVal
Definition:LoopUtils.h:478
llvm::UnusedIndVarInLoop
@ UnusedIndVarInLoop
Definition:LoopUtils.h:482
llvm::OnlyCheapRepl
@ OnlyCheapRepl
Definition:LoopUtils.h:480
llvm::AlwaysRepl
@ AlwaysRepl
Definition:LoopUtils.h:483
llvm::SplitEdge
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...
Definition:BasicBlockUtils.cpp:762
llvm::hasPartialIVCondition
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...
Definition:LoopUtils.cpp:2058
llvm::filterDbgVars
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
Definition:DebugProgramInstruction.h:555
llvm::PseudoProbeAttributes::Sentinel
@ Sentinel
llvm::cannotBeMinInLoop
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.
Definition:LoopUtils.cpp:1416
llvm::isKnownNonNegativeInLoop
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.
Definition:LoopUtils.cpp:1395
llvm::getOrderedReduction
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.
Definition:LoopUtils.cpp:1093
llvm::findOptionMDForLoopID
MDNode * findOptionMDForLoopID(MDNode *LoopID, StringRef Name)
Find and return the loop attribute node for the attribute Name in LoopID.
Definition:LoopInfo.cpp:1041
llvm::cloneLoop
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...
Definition:LoopUtils.cpp:1831
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition:BitVector.h:860
N
#define N
PointerBounds
IR Values for the lower and upper bounds of a pointer evolution.
Definition:LoopUtils.cpp:1858
PointerBounds::Start
TrackingVH< Value > Start
Definition:LoopUtils.cpp:1859
PointerBounds::End
TrackingVH< Value > End
Definition:LoopUtils.cpp:1860
PointerBounds::StrideToCheck
Value * StrideToCheck
Definition:LoopUtils.cpp:1861
RewritePhi
Definition:LoopUtils.cpp:1468
RewritePhi::HighCost
bool HighCost
Definition:LoopUtils.cpp:1473
RewritePhi::Ith
unsigned Ith
Definition:LoopUtils.cpp:1470
RewritePhi::RewritePhi
RewritePhi(PHINode *P, unsigned I, const SCEV *Val, Instruction *ExpansionPt, bool H)
Definition:LoopUtils.cpp:1475
RewritePhi::ExpansionSCEV
const SCEV * ExpansionSCEV
Definition:LoopUtils.cpp:1471
RewritePhi::PN
PHINode * PN
Definition:LoopUtils.cpp:1469
RewritePhi::ExpansionPoint
Instruction * ExpansionPoint
Definition:LoopUtils.cpp:1472
llvm::DWARFExpression::Operation::Description
Description of the encoding of one expression Op.
Definition:DWARFExpression.h:66
llvm::IVConditionInfo
Struct to hold information about a partially invariant condition.
Definition:LoopUtils.h:556
llvm::Incoming
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
Definition:SILowerI1Copies.h:25
llvm::LCSSAVerificationPass
Definition:LoopPass.h:124
llvm::RuntimeCheckingPtrGroup
A grouping of pointers.
Definition:LoopAccessAnalysis.h:408
llvm::RuntimeCheckingPtrGroup::AddressSpace
unsigned AddressSpace
Address space of the involved pointers.
Definition:LoopAccessAnalysis.h:432
llvm::RuntimeCheckingPtrGroup::NeedsFreeze
bool NeedsFreeze
Whether the pointer needs to be frozen after expansion, e.g.
Definition:LoopAccessAnalysis.h:435
llvm::RuntimeCheckingPtrGroup::High
const SCEV * High
The SCEV expression which represents the upper bound of all the pointers in this group.
Definition:LoopAccessAnalysis.h:425
llvm::RuntimeCheckingPtrGroup::Low
const SCEV * Low
The SCEV expression which represents the lower bound of all the pointers in this group.
Definition:LoopAccessAnalysis.h:428
llvm::fltSemantics
Definition:APFloat.cpp:103

Generated on Thu Jul 17 2025 17:45:41 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp