LLVM 23.0.0git
LoopRotationUtils.cpp
Go to the documentation of this file.
1//===----------------- LoopRotationUtils.cpp -----------------------------===//
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 provides utilities to convert a loop into a loop with bottom test.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/Statistic.h"
24#include "llvm/IR/CFG.h"
25#include "llvm/IR/DebugInfo.h"
26#include "llvm/IR/Dominators.h"
28#include "llvm/IR/MDBuilder.h"
31#include "llvm/Support/Debug.h"
38using namespace llvm;
39
40#define DEBUG_TYPE "loop-rotate"
41
42STATISTIC(NumNotRotatedDueToHeaderSize,
43 "Number of loops not rotated due to the header size");
44STATISTIC(NumInstrsHoisted,
45 "Number of instructions hoisted into loop preheader");
46STATISTIC(NumInstrsDuplicated,
47 "Number of instructions cloned into loop preheader");
48
49// Probability that a rotated loop has zero trip count / is never entered.
50static constexpr uint32_t ZeroTripCountWeights[] = {1, 127};
51
52namespace {
53/// A simple loop rotation transformation.
54class LoopRotate {
55 const unsigned MaxHeaderSize;
56 LoopInfo *LI;
57 const TargetTransformInfo *TTI;
58 AssumptionCache *AC;
59 DominatorTree *DT;
60 ScalarEvolution *SE;
61 MemorySSAUpdater *MSSAU;
62 const SimplifyQuery &SQ;
63 bool RotationOnly;
64 bool IsUtilMode;
65 bool PrepareForLTO;
66
67public:
68 LoopRotate(unsigned MaxHeaderSize, LoopInfo *LI,
69 const TargetTransformInfo *TTI, AssumptionCache *AC,
70 DominatorTree *DT, ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
71 const SimplifyQuery &SQ, bool RotationOnly, bool IsUtilMode,
72 bool PrepareForLTO)
73 : MaxHeaderSize(MaxHeaderSize), LI(LI), TTI(TTI), AC(AC), DT(DT), SE(SE),
74 MSSAU(MSSAU), SQ(SQ), RotationOnly(RotationOnly),
75 IsUtilMode(IsUtilMode), PrepareForLTO(PrepareForLTO) {}
76 bool processLoop(Loop *L);
77
78private:
79 bool rotateLoop(Loop *L, bool SimplifiedLatch);
80 bool simplifyLoopLatch(Loop *L);
81};
82} // end anonymous namespace
83
84/// Insert (K, V) pair into the ValueToValueMap, and verify the key did not
85/// previously exist in the map, and the value was inserted.
87 bool Inserted = VM.insert({K, V}).second;
88 assert(Inserted);
89 (void)Inserted;
90}
91/// RewriteUsesOfClonedInstructions - We just cloned the instructions from the
92/// old header into the preheader. If there were uses of the values produced by
93/// these instruction that were outside of the loop, we have to insert PHI nodes
94/// to merge the two values. Do this now.
96 BasicBlock *OrigPreheader,
99 SmallVectorImpl<PHINode*> *InsertedPHIs) {
100 // Remove PHI node entries that are no longer live.
101 BasicBlock::iterator I, E = OrigHeader->end();
102 for (I = OrigHeader->begin(); PHINode *PN = dyn_cast<PHINode>(I); ++I)
103 PN->removeIncomingValue(OrigPreheader);
104
105 // Now fix up users of the instructions in OrigHeader, inserting PHI nodes
106 // as necessary.
107 SSAUpdater SSA(InsertedPHIs);
108 for (I = OrigHeader->begin(); I != E; ++I) {
109 Value *OrigHeaderVal = &*I;
110
111 // If there are no uses of the value (e.g. because it returns void), there
112 // is nothing to rewrite.
113 if (OrigHeaderVal->use_empty())
114 continue;
115
116 Value *OrigPreHeaderVal = ValueMap.lookup(OrigHeaderVal);
117
118 // The value now exits in two versions: the initial value in the preheader
119 // and the loop "next" value in the original header.
120 SSA.Initialize(OrigHeaderVal->getType(), OrigHeaderVal->getName());
121 // Force re-computation of OrigHeaderVal, as some users now need to use the
122 // new PHI node.
123 if (SE)
124 SE->forgetValue(OrigHeaderVal);
125 SSA.AddAvailableValue(OrigHeader, OrigHeaderVal);
126 SSA.AddAvailableValue(OrigPreheader, OrigPreHeaderVal);
127
128 // Visit each use of the OrigHeader instruction.
129 for (Use &U : llvm::make_early_inc_range(OrigHeaderVal->uses())) {
130 // SSAUpdater can't handle a non-PHI use in the same block as an
131 // earlier def. We can easily handle those cases manually.
132 Instruction *UserInst = cast<Instruction>(U.getUser());
133 if (!isa<PHINode>(UserInst)) {
134 BasicBlock *UserBB = UserInst->getParent();
135
136 // The original users in the OrigHeader are already using the
137 // original definitions.
138 if (UserBB == OrigHeader)
139 continue;
140
141 // Users in the OrigPreHeader need to use the value to which the
142 // original definitions are mapped.
143 if (UserBB == OrigPreheader) {
144 U = OrigPreHeaderVal;
145 continue;
146 }
147 }
148
149 // Anything else can be handled by SSAUpdater.
150 SSA.RewriteUse(U);
151 }
152
153 // Replace MetadataAsValue(ValueAsMetadata(OrigHeaderVal)) uses in debug
154 // intrinsics.
155 SmallVector<DbgVariableRecord *, 1> DbgVariableRecords;
156 llvm::findDbgValues(OrigHeaderVal, DbgVariableRecords);
157
158 for (DbgVariableRecord *DVR : DbgVariableRecords) {
159 // The original users in the OrigHeader are already using the original
160 // definitions.
161 BasicBlock *UserBB = DVR->getMarker()->getParent();
162 if (UserBB == OrigHeader)
163 continue;
164
165 // Users in the OrigPreHeader need to use the value to which the
166 // original definitions are mapped and anything else can be handled by
167 // the SSAUpdater. To avoid adding PHINodes, check if the value is
168 // available in UserBB, if not substitute poison.
169 Value *NewVal;
170 if (UserBB == OrigPreheader)
171 NewVal = OrigPreHeaderVal;
172 else if (SSA.HasValueForBlock(UserBB))
173 NewVal = SSA.GetValueInMiddleOfBlock(UserBB);
174 else
175 NewVal = PoisonValue::get(OrigHeaderVal->getType());
176 DVR->replaceVariableLocationOp(OrigHeaderVal, NewVal);
177 }
178 }
179}
180
181// Assuming both header and latch are exiting, look for a phi which is only
182// used outside the loop (via a LCSSA phi) in the exit from the header.
183// This means that rotating the loop can remove the phi.
185 BasicBlock *Header = L->getHeader();
186 CondBrInst *BI = dyn_cast<CondBrInst>(Header->getTerminator());
187 BasicBlock *HeaderExit = BI->getSuccessor(0);
188 if (L->contains(HeaderExit))
189 HeaderExit = BI->getSuccessor(1);
190
191 for (auto &Phi : Header->phis()) {
192 // Look for uses of this phi in the loop/via exits other than the header.
193 if (llvm::any_of(Phi.users(), [HeaderExit](const User *U) {
194 return cast<Instruction>(U)->getParent() != HeaderExit;
195 }))
196 continue;
197 return true;
198 }
199 return false;
200}
201
202static void updateBranchWeights(CondBrInst &PreHeaderBI, CondBrInst &LoopBI,
203 bool HasConditionalPreHeader,
204 bool SuccsSwapped) {
205 MDNode *WeightMD = getBranchWeightMDNode(PreHeaderBI);
206 if (WeightMD == nullptr)
207 return;
208
209 // LoopBI should currently be a clone of PreHeaderBI with the same
210 // metadata. But we double check to make sure we don't have a degenerate case
211 // where instsimplify changed the instructions.
212 if (WeightMD != getBranchWeightMDNode(LoopBI))
213 return;
214
216 extractFromBranchWeightMD32(WeightMD, Weights);
217 if (Weights.size() != 2)
218 return;
219 uint32_t OrigLoopExitWeight = Weights[0];
220 uint32_t OrigLoopBackedgeWeight = Weights[1];
221
222 if (SuccsSwapped)
223 std::swap(OrigLoopExitWeight, OrigLoopBackedgeWeight);
224
225 // Update branch weights. Consider the following edge-counts:
226 //
227 // | |-------- |
228 // V V | V
229 // Br i1 ... | Br i1 ...
230 // | | | | |
231 // x| y| | becomes: | y0| |-----
232 // V V | | V V |
233 // Exit Loop | | Loop |
234 // | | | Br i1 ... |
235 // ----- | | | |
236 // x0| x1| y1 | |
237 // V V ----
238 // Exit
239 //
240 // The following must hold:
241 // - x == x0 + x1 # counts to "exit" must stay the same.
242 // - y0 == x - x0 == x1 # how often loop was entered at all.
243 // - y1 == y - y0 # How often loop was repeated (after first iter.).
244 //
245 // We cannot generally deduce how often we had a zero-trip count loop so we
246 // have to make a guess for how to distribute x among the new x0 and x1.
247
248 uint32_t ExitWeight0; // aka x0
249 uint32_t ExitWeight1; // aka x1
250 uint32_t EnterWeight; // aka y0
251 uint32_t LoopBackWeight; // aka y1
252 if (OrigLoopExitWeight > 0 && OrigLoopBackedgeWeight > 0) {
253 ExitWeight0 = 0;
254 if (HasConditionalPreHeader) {
255 // Here we cannot know how many 0-trip count loops we have, so we guess:
256 if (OrigLoopBackedgeWeight >= OrigLoopExitWeight) {
257 // If the loop count is bigger than the exit count then we set
258 // probabilities as if 0-trip count nearly never happens.
259 ExitWeight0 = ZeroTripCountWeights[0];
260 // Scale up counts if necessary so we can match `ZeroTripCountWeights`
261 // for the `ExitWeight0`:`ExitWeight1` (aka `x0`:`x1` ratio`) ratio.
262 while (OrigLoopExitWeight < ZeroTripCountWeights[1] + ExitWeight0) {
263 // ... but don't overflow.
264 uint32_t const HighBit = uint32_t{1} << (sizeof(uint32_t) * 8 - 1);
265 if ((OrigLoopBackedgeWeight & HighBit) != 0 ||
266 (OrigLoopExitWeight & HighBit) != 0)
267 break;
268 OrigLoopBackedgeWeight <<= 1;
269 OrigLoopExitWeight <<= 1;
270 }
271 } else {
272 // If there's a higher exit-count than backedge-count then we set
273 // probabilities as if there are only 0-trip and 1-trip cases.
274 ExitWeight0 = OrigLoopExitWeight - OrigLoopBackedgeWeight;
275 }
276 } else {
277 // Theoretically, if the loop body must be executed at least once, the
278 // backedge count must be not less than exit count. However the branch
279 // weight collected by sampling-based PGO may be not very accurate due to
280 // sampling. Therefore this workaround is required here to avoid underflow
281 // of unsigned in following update of branch weight.
282 if (OrigLoopExitWeight > OrigLoopBackedgeWeight)
283 OrigLoopBackedgeWeight = OrigLoopExitWeight;
284 }
285 assert(OrigLoopExitWeight >= ExitWeight0 && "Bad branch weight");
286 ExitWeight1 = OrigLoopExitWeight - ExitWeight0;
287 EnterWeight = ExitWeight1;
288 assert(OrigLoopBackedgeWeight >= EnterWeight && "Bad branch weight");
289 LoopBackWeight = OrigLoopBackedgeWeight - EnterWeight;
290 } else if (OrigLoopExitWeight == 0) {
291 if (OrigLoopBackedgeWeight == 0) {
292 // degenerate case... keep everything zero...
293 ExitWeight0 = 0;
294 ExitWeight1 = 0;
295 EnterWeight = 0;
296 LoopBackWeight = 0;
297 } else {
298 // Special case "LoopExitWeight == 0" weights which behaves like an
299 // endless where we don't want loop-enttry (y0) to be the same as
300 // loop-exit (x1).
301 ExitWeight0 = 0;
302 ExitWeight1 = 0;
303 EnterWeight = 1;
304 LoopBackWeight = OrigLoopBackedgeWeight;
305 }
306 } else {
307 // loop is never entered.
308 assert(OrigLoopBackedgeWeight == 0 && "remaining case is backedge zero");
309 ExitWeight0 = 1;
310 ExitWeight1 = 1;
311 EnterWeight = 0;
312 LoopBackWeight = 0;
313 }
314
315 const uint32_t LoopBIWeights[] = {
316 SuccsSwapped ? LoopBackWeight : ExitWeight1,
317 SuccsSwapped ? ExitWeight1 : LoopBackWeight,
318 };
319 setBranchWeights(LoopBI, LoopBIWeights, /*IsExpected=*/false);
320 if (HasConditionalPreHeader) {
321 const uint32_t PreHeaderBIWeights[] = {
322 SuccsSwapped ? EnterWeight : ExitWeight0,
323 SuccsSwapped ? ExitWeight0 : EnterWeight,
324 };
325 setBranchWeights(PreHeaderBI, PreHeaderBIWeights, /*IsExpected=*/false);
326 }
327}
328
329/// Rotate loop LP. Return true if the loop is rotated.
330///
331/// \param SimplifiedLatch is true if the latch was just folded into the final
332/// loop exit. In this case we may want to rotate even though the new latch is
333/// now an exiting branch. This rotation would have happened had the latch not
334/// been simplified. However, if SimplifiedLatch is false, then we avoid
335/// rotating loops in which the latch exits to avoid excessive or endless
336/// rotation. LoopRotate should be repeatable and converge to a canonical
337/// form. This property is satisfied because simplifying the loop latch can only
338/// happen once across multiple invocations of the LoopRotate pass.
339bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) {
340 // If the loop has only one block then there is not much to rotate.
341 if (L->getBlocks().size() == 1)
342 return false;
343
344 bool Rotated = false;
345 BasicBlock *OrigHeader = L->getHeader();
346 BasicBlock *OrigLatch = L->getLoopLatch();
347
348 CondBrInst *BI = dyn_cast<CondBrInst>(OrigHeader->getTerminator());
349 if (!BI)
350 return Rotated;
351
352 // If the loop header is not one of the loop exiting blocks then
353 // either this loop is already rotated or it is not
354 // suitable for loop rotation transformations.
355 if (!L->isLoopExiting(OrigHeader))
356 return Rotated;
357
358 // If the loop latch already contains a branch that leaves the loop then the
359 // loop is already rotated.
360 if (!OrigLatch)
361 return Rotated;
362
363 // Rotate if the loop latch was just simplified. Or if it makes the loop exit
364 // count computable. Or if we think it will be profitable.
365 if (L->isLoopExiting(OrigLatch) && !SimplifiedLatch && IsUtilMode == false &&
367 return Rotated;
368
369 // Check size of original header and reject loop if it is very big or we can't
370 // duplicate blocks inside it.
371 {
372 SmallPtrSet<const Value *, 32> EphValues;
373 CodeMetrics::collectEphemeralValues(L, AC, EphValues);
374
375 CodeMetrics Metrics;
376 Metrics.analyzeBasicBlock(OrigHeader, *TTI, EphValues, PrepareForLTO);
377 if (Metrics.notDuplicatable) {
379 dbgs() << "LoopRotation: NOT rotating - contains non-duplicatable"
380 << " instructions: ";
381 L->dump());
382 return Rotated;
383 }
384 if (Metrics.Convergence != ConvergenceKind::None) {
385 LLVM_DEBUG(dbgs() << "LoopRotation: NOT rotating - contains convergent "
386 "instructions: ";
387 L->dump());
388 return Rotated;
389 }
390 if (!Metrics.NumInsts.isValid()) {
391 LLVM_DEBUG(dbgs() << "LoopRotation: NOT rotating - contains instructions"
392 " with invalid cost: ";
393 L->dump());
394 return Rotated;
395 }
396 if (Metrics.NumInsts > MaxHeaderSize) {
397 LLVM_DEBUG(dbgs() << "LoopRotation: NOT rotating - contains "
398 << Metrics.NumInsts
399 << " instructions, which is more than the threshold ("
400 << MaxHeaderSize << " instructions): ";
401 L->dump());
402 ++NumNotRotatedDueToHeaderSize;
403 return Rotated;
404 }
405
406 // When preparing for LTO, avoid rotating loops with calls that could be
407 // inlined during the LTO stage.
408 if (PrepareForLTO && Metrics.NumInlineCandidates > 0)
409 return Rotated;
410 }
411
412 // Now, this loop is suitable for rotation.
413 BasicBlock *OrigPreheader = L->getLoopPreheader();
414
415 // If the loop could not be converted to canonical form, it must have an
416 // indirectbr in it, just give up.
417 if (!OrigPreheader || !L->hasDedicatedExits())
418 return Rotated;
419
420 // Anything ScalarEvolution may know about this loop or the PHI nodes
421 // in its header will soon be invalidated. We should also invalidate
422 // all outer loops because insertion and deletion of blocks that happens
423 // during the rotation may violate invariants related to backedge taken
424 // infos in them.
425 if (SE) {
426 SE->forgetTopmostLoop(L);
427 // We may hoist some instructions out of loop. In case if they were cached
428 // as "loop variant" or "loop computable", these caches must be dropped.
429 // We also may fold basic blocks, so cached block dispositions also need
430 // to be dropped.
432 }
433
434 LLVM_DEBUG(dbgs() << "LoopRotation: rotating "; L->dump());
435 if (MSSAU && VerifyMemorySSA)
436 MSSAU->getMemorySSA()->verifyMemorySSA();
437
438 // Find new Loop header. NewHeader is a Header's one and only successor
439 // that is inside loop. Header's other successor is outside the
440 // loop. Otherwise loop is not suitable for rotation.
441 BasicBlock *Exit = BI->getSuccessor(0);
442 BasicBlock *NewHeader = BI->getSuccessor(1);
443 bool BISuccsSwapped = L->contains(Exit);
444 if (BISuccsSwapped)
445 std::swap(Exit, NewHeader);
446 assert(NewHeader && "Unable to determine new loop header");
447 assert(L->contains(NewHeader) && !L->contains(Exit) &&
448 "Unable to determine loop header and exit blocks");
449
450 // This code assumes that the new header has exactly one predecessor.
451 // Remove any single-entry PHI nodes in it.
452 assert(NewHeader->getSinglePredecessor() &&
453 "New header doesn't have one pred!");
454 FoldSingleEntryPHINodes(NewHeader);
455
456 // Begin by walking OrigHeader and populating ValueMap with an entry for
457 // each Instruction.
458 BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end();
459 ValueToValueMapTy ValueMap, ValueMapMSSA;
460
461 // For PHI nodes, the value available in OldPreHeader is just the
462 // incoming value from OldPreHeader.
463 for (; PHINode *PN = dyn_cast<PHINode>(I); ++I)
464 InsertNewValueIntoMap(ValueMap, PN,
465 PN->getIncomingValueForBlock(OrigPreheader));
466
467 // For the rest of the instructions, either hoist to the OrigPreheader if
468 // possible or create a clone in the OldPreHeader if not.
469 Instruction *LoopEntryBranch = OrigPreheader->getTerminator();
470
471 // Record all debug records preceding LoopEntryBranch to avoid
472 // duplication.
473 using DbgHash =
474 std::pair<std::pair<hash_code, DILocalVariable *>, DIExpression *>;
475 auto makeHash = [](const DbgVariableRecord *D) -> DbgHash {
476 auto VarLocOps = D->location_ops();
477 return {{hash_combine_range(VarLocOps), D->getVariable()},
478 D->getExpression()};
479 };
480
481 SmallDenseSet<DbgHash, 8> DbgRecords;
482 // Build DbgVariableRecord hashes for DbgVariableRecords attached to the
483 // terminator.
484 for (const DbgVariableRecord &DVR :
485 filterDbgVars(OrigPreheader->getTerminator()->getDbgRecordRange()))
486 DbgRecords.insert(makeHash(&DVR));
487
488 // Remember the local noalias scope declarations in the header. After the
489 // rotation, they must be duplicated and the scope must be cloned. This
490 // avoids unwanted interaction across iterations.
491 SmallVector<NoAliasScopeDeclInst *, 6> NoAliasDeclInstructions;
492 for (Instruction &I : *OrigHeader)
493 if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
494 NoAliasDeclInstructions.push_back(Decl);
495
496 Module *M = OrigHeader->getModule();
497
498 // Track the next DbgRecord to clone. If we have a sequence where an
499 // instruction is hoisted instead of being cloned:
500 // DbgRecord blah
501 // %foo = add i32 0, 0
502 // DbgRecord xyzzy
503 // %bar = call i32 @foobar()
504 // where %foo is hoisted, then the DbgRecord "blah" will be seen twice, once
505 // attached to %foo, then when %foo his hoisted it will "fall down" onto the
506 // function call:
507 // DbgRecord blah
508 // DbgRecord xyzzy
509 // %bar = call i32 @foobar()
510 // causing it to appear attached to the call too.
511 //
512 // To avoid this, cloneDebugInfoFrom takes an optional "start cloning from
513 // here" position to account for this behaviour. We point it at any
514 // DbgRecords on the next instruction, here labelled xyzzy, before we hoist
515 // %foo. Later, we only only clone DbgRecords from that position (xyzzy)
516 // onwards, which avoids cloning DbgRecord "blah" multiple times. (Stored as
517 // a range because it gives us a natural way of testing whether
518 // there were DbgRecords on the next instruction before we hoisted things).
520 (I != E) ? I->getDbgRecordRange() : DbgMarker::getEmptyDbgRecordRange();
521
522 while (I != E) {
523 Instruction *Inst = &*I++;
524
525 // If the instruction's operands are invariant and it doesn't read or write
526 // memory, then it is safe to hoist. Doing this doesn't change the order of
527 // execution in the preheader, but does prevent the instruction from
528 // executing in each iteration of the loop. This means it is safe to hoist
529 // something that might trap, but isn't safe to hoist something that reads
530 // memory (without proving that the loop doesn't write).
531 if (L->hasLoopInvariantOperands(Inst) && !Inst->mayReadFromMemory() &&
532 !Inst->mayWriteToMemory() && !Inst->isTerminator() &&
533 !isa<AllocaInst>(Inst) &&
534 // It is not safe to hoist the value of these instructions in
535 // coroutines, as the addresses of otherwise eligible variables (e.g.
536 // thread-local variables and errno) may change if the coroutine is
537 // resumed in a different thread.Therefore, we disable this
538 // optimization for correctness. However, this may block other correct
539 // optimizations.
540 // FIXME: This should be reverted once we have a better model for
541 // memory access in coroutines.
542 !Inst->getFunction()->isPresplitCoroutine()) {
543
544 if (!NextDbgInsts.empty()) {
545 auto DbgValueRange =
546 LoopEntryBranch->cloneDebugInfoFrom(Inst, NextDbgInsts.begin());
547 RemapDbgRecordRange(M, DbgValueRange, ValueMap,
549 // Erase anything we've seen before.
550 for (DbgVariableRecord &DVR :
551 make_early_inc_range(filterDbgVars(DbgValueRange)))
552 if (DbgRecords.count(makeHash(&DVR)))
553 DVR.eraseFromParent();
554 }
555
556 NextDbgInsts = I->getDbgRecordRange();
557
558 Inst->moveBefore(LoopEntryBranch->getIterator());
559
560 ++NumInstrsHoisted;
561 continue;
562 }
563
564 // Otherwise, create a duplicate of the instruction.
565 Instruction *C = Inst->clone();
566 if (const DebugLoc &DL = C->getDebugLoc())
567 mapAtomInstance(DL, ValueMap);
568
569 C->insertBefore(LoopEntryBranch->getIterator());
570
571 ++NumInstrsDuplicated;
572
573 if (!NextDbgInsts.empty()) {
574 auto Range = C->cloneDebugInfoFrom(Inst, NextDbgInsts.begin());
575 RemapDbgRecordRange(M, Range, ValueMap,
577 NextDbgInsts = DbgMarker::getEmptyDbgRecordRange();
578 // Erase anything we've seen before.
579 for (DbgVariableRecord &DVR : make_early_inc_range(filterDbgVars(Range)))
580 if (DbgRecords.count(makeHash(&DVR)))
581 DVR.eraseFromParent();
582 }
583
584 // Eagerly remap the operands of the instruction.
585 RemapInstruction(C, ValueMap,
587
588 // With the operands remapped, see if the instruction constant folds or is
589 // otherwise simplifyable. This commonly occurs because the entry from PHI
590 // nodes allows icmps and other instructions to fold.
592 if (V && LI->replacementPreservesLCSSAForm(C, V)) {
593 // If so, then delete the temporary instruction and stick the folded value
594 // in the map.
595 InsertNewValueIntoMap(ValueMap, Inst, V);
596 if (!C->mayHaveSideEffects()) {
597 C->eraseFromParent();
598 C = nullptr;
599 }
600 } else {
601 InsertNewValueIntoMap(ValueMap, Inst, C);
602 }
603 if (C) {
604 // Otherwise, stick the new instruction into the new block!
605 C->setName(Inst->getName());
606
607 if (auto *II = dyn_cast<AssumeInst>(C))
609 // MemorySSA cares whether the cloned instruction was inserted or not, and
610 // not whether it can be remapped to a simplified value.
611 if (MSSAU)
612 InsertNewValueIntoMap(ValueMapMSSA, Inst, C);
613 }
614 }
615
616 if (!NoAliasDeclInstructions.empty()) {
617 // There are noalias scope declarations:
618 // (general):
619 // Original: OrigPre { OrigHeader NewHeader ... Latch }
620 // after: (OrigPre+OrigHeader') { NewHeader ... Latch OrigHeader }
621 //
622 // with D: llvm.experimental.noalias.scope.decl,
623 // U: !noalias or !alias.scope depending on D
624 // ... { D U1 U2 } can transform into:
625 // (0) : ... { D U1 U2 } // no relevant rotation for this part
626 // (1) : ... D' { U1 U2 D } // D is part of OrigHeader
627 // (2) : ... D' U1' { U2 D U1 } // D, U1 are part of OrigHeader
628 //
629 // We now want to transform:
630 // (1) -> : ... D' { D U1 U2 D'' }
631 // (2) -> : ... D' U1' { D U2 D'' U1'' }
632 // D: original llvm.experimental.noalias.scope.decl
633 // D', U1': duplicate with replaced scopes
634 // D'', U1'': different duplicate with replaced scopes
635 // This ensures a safe fallback to 'may_alias' introduced by the rotate,
636 // as U1'' and U1' scopes will not be compatible wrt to the local restrict
637
638 // Clone the llvm.experimental.noalias.decl again for the NewHeader.
639 BasicBlock::iterator NewHeaderInsertionPoint =
640 NewHeader->getFirstNonPHIIt();
641 for (NoAliasScopeDeclInst *NAD : NoAliasDeclInstructions) {
642 LLVM_DEBUG(dbgs() << " Cloning llvm.experimental.noalias.scope.decl:"
643 << *NAD << "\n");
644 Instruction *NewNAD = NAD->clone();
645 NewNAD->insertBefore(*NewHeader, NewHeaderInsertionPoint);
646 }
647
648 // Scopes must now be duplicated, once for OrigHeader and once for
649 // OrigPreHeader'.
650 {
651 auto &Context = NewHeader->getContext();
652
653 SmallVector<MDNode *, 8> NoAliasDeclScopes;
654 for (NoAliasScopeDeclInst *NAD : NoAliasDeclInstructions)
655 NoAliasDeclScopes.push_back(NAD->getScopeList());
656
657 LLVM_DEBUG(dbgs() << " Updating OrigHeader scopes\n");
658 cloneAndAdaptNoAliasScopes(NoAliasDeclScopes, {OrigHeader}, Context,
659 "h.rot");
660 LLVM_DEBUG(OrigHeader->dump());
661
662 // Keep the compile time impact low by only adapting the inserted block
663 // of instructions in the OrigPreHeader. This might result in slightly
664 // more aliasing between these instructions and those that were already
665 // present, but it will be much faster when the original PreHeader is
666 // large.
667 LLVM_DEBUG(dbgs() << " Updating part of OrigPreheader scopes\n");
668 auto *FirstDecl =
669 cast<Instruction>(ValueMap[*NoAliasDeclInstructions.begin()]);
670 auto *LastInst = &OrigPreheader->back();
671 cloneAndAdaptNoAliasScopes(NoAliasDeclScopes, FirstDecl, LastInst,
672 Context, "pre.rot");
673 LLVM_DEBUG(OrigPreheader->dump());
674
675 LLVM_DEBUG(dbgs() << " Updated NewHeader:\n");
676 LLVM_DEBUG(NewHeader->dump());
677 }
678 }
679
680 // Along with all the other instructions, we just cloned OrigHeader's
681 // terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's
682 // successors by duplicating their incoming values for OrigHeader.
683 for (BasicBlock *SuccBB : successors(OrigHeader))
684 for (BasicBlock::iterator BI = SuccBB->begin();
685 PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
686 PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreheader);
687
688 // Now that OrigPreHeader has a clone of OrigHeader's terminator, remove
689 // OrigPreHeader's old terminator (the original branch into the loop), and
690 // remove the corresponding incoming values from the PHI nodes in OrigHeader.
691 LoopEntryBranch->eraseFromParent();
692 OrigPreheader->flushTerminatorDbgRecords();
693
694 // Update MemorySSA before the rewrite call below changes the 1:1
695 // instruction:cloned_instruction_or_value mapping.
696 if (MSSAU) {
697 InsertNewValueIntoMap(ValueMapMSSA, OrigHeader, OrigPreheader);
698 MSSAU->updateForClonedBlockIntoPred(OrigHeader, OrigPreheader,
699 ValueMapMSSA);
700 }
701
702 SmallVector<PHINode *, 2> InsertedPHIs;
703 // If there were any uses of instructions in the duplicated block outside the
704 // loop, update them, inserting PHI nodes as required
705 RewriteUsesOfClonedInstructions(OrigHeader, OrigPreheader, ValueMap, SE,
706 &InsertedPHIs);
707
708 // Attach debug records to the new phis if that phi uses a value that
709 // previously had debug metadata attached. This keeps the debug info
710 // up-to-date in the loop body.
711 if (!InsertedPHIs.empty())
712 insertDebugValuesForPHIs(OrigHeader, InsertedPHIs);
713
714 // NewHeader is now the header of the loop.
715 L->moveToHeader(NewHeader);
716 assert(L->getHeader() == NewHeader && "Latch block is our new header");
717
718 // Inform DT about changes to the CFG.
719 if (DT) {
720 // The OrigPreheader branches to the NewHeader and Exit now. Then, inform
721 // the DT about the removed edge to the OrigHeader (that got removed).
723 {DominatorTree::Insert, OrigPreheader, Exit},
724 {DominatorTree::Insert, OrigPreheader, NewHeader},
725 {DominatorTree::Delete, OrigPreheader, OrigHeader}};
726
727 if (MSSAU) {
728 MSSAU->applyUpdates(Updates, *DT, /*UpdateDT=*/true);
729 if (VerifyMemorySSA)
730 MSSAU->getMemorySSA()->verifyMemorySSA();
731 } else {
732 DT->applyUpdates(Updates);
733 }
734 }
735
736 // At this point, we've finished our major CFG changes. As part of cloning
737 // the loop into the preheader we've simplified instructions and the
738 // duplicated conditional branch may now be branching on a constant. If it is
739 // branching on a constant and if that constant means that we enter the loop,
740 // then we fold away the cond branch to an uncond branch. This simplifies the
741 // loop in cases important for nested loops, and it also means we don't have
742 // to split as many edges.
743 CondBrInst *PHBI = cast<CondBrInst>(OrigPreheader->getTerminator());
744 const Value *Cond = PHBI->getCondition();
745 const bool HasConditionalPreHeader =
747 PHBI->getSuccessor(cast<ConstantInt>(Cond)->isZero()) != NewHeader;
748
749 updateBranchWeights(*PHBI, *BI, HasConditionalPreHeader, BISuccsSwapped);
750
751 if (HasConditionalPreHeader) {
752 // The conditional branch can't be folded, handle the general case.
753 // Split edges as necessary to preserve LoopSimplify form.
754
755 // Right now OrigPreHeader has two successors, NewHeader and ExitBlock, and
756 // thus is not a preheader anymore.
757 // Split the edge to form a real preheader.
759 OrigPreheader, NewHeader,
760 CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA());
761 NewPH->setName(NewHeader->getName() + ".lr.ph");
762
763 // Preserve canonical loop form, which means that 'Exit' should have only
764 // one predecessor. Note that Exit could be an exit block for multiple
765 // nested loops, causing both of the edges to now be critical and need to
766 // be split.
768 bool SplitLatchEdge = false;
769 for (BasicBlock *ExitPred : ExitPreds) {
770 // We only need to split loop exit edges.
771 Loop *PredLoop = LI->getLoopFor(ExitPred);
772 if (!PredLoop || PredLoop->contains(Exit) ||
773 isa<IndirectBrInst>(ExitPred->getTerminator()))
774 continue;
775 SplitLatchEdge |= L->getLoopLatch() == ExitPred;
776 BasicBlock *ExitSplit = SplitCriticalEdge(
777 ExitPred, Exit,
778 CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA());
779 ExitSplit->moveBefore(Exit);
780 }
781 assert(SplitLatchEdge &&
782 "Despite splitting all preds, failed to split latch exit?");
783 (void)SplitLatchEdge;
784 } else {
785 // We can fold the conditional branch in the preheader, this makes things
786 // simpler. The first step is to remove the extra edge to the Exit block.
787 Exit->removePredecessor(OrigPreheader, true /*preserve LCSSA*/);
788 UncondBrInst *NewBI = UncondBrInst::Create(NewHeader, PHBI->getIterator());
789 NewBI->setDebugLoc(PHBI->getDebugLoc());
790 PHBI->eraseFromParent();
791
792 // With our CFG finalized, update DomTree if it is available.
793 if (DT)
794 DT->deleteEdge(OrigPreheader, Exit);
795
796 // Update MSSA too, if available.
797 if (MSSAU)
798 MSSAU->removeEdge(OrigPreheader, Exit);
799 }
800
801 assert(L->getLoopPreheader() && "Invalid loop preheader after loop rotation");
802 assert(L->getLoopLatch() && "Invalid loop latch after loop rotation");
803
804 if (MSSAU && VerifyMemorySSA)
805 MSSAU->getMemorySSA()->verifyMemorySSA();
806
807 // Now that the CFG and DomTree are in a consistent state again, try to merge
808 // the OrigHeader block into OrigLatch. This will succeed if they are
809 // connected by an unconditional branch. This is just a cleanup so the
810 // emitted code isn't too gross in this common case.
811 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
812 BasicBlock *PredBB = OrigHeader->getUniquePredecessor();
813 bool DidMerge = MergeBlockIntoPredecessor(OrigHeader, &DTU, LI, MSSAU);
814 if (DidMerge)
816
817 if (MSSAU && VerifyMemorySSA)
818 MSSAU->getMemorySSA()->verifyMemorySSA();
819
820 LLVM_DEBUG(dbgs() << "LoopRotation: into "; L->dump());
821
822 return true;
823}
824
825/// Determine whether the instructions in this range may be safely and cheaply
826/// speculated. This is not an important enough situation to develop complex
827/// heuristics. We handle a single arithmetic instruction along with any type
828/// conversions.
830 BasicBlock::iterator End, Loop *L) {
831 bool seenIncrement = false;
832 bool MultiExitLoop = false;
833
834 if (!L->getExitingBlock())
835 MultiExitLoop = true;
836
837 for (BasicBlock::iterator I = Begin; I != End; ++I) {
838
840 return false;
841
842 switch (I->getOpcode()) {
843 default:
844 return false;
845 case Instruction::GetElementPtr:
846 // GEPs are cheap if all indices are constant.
847 if (!cast<GEPOperator>(I)->hasAllConstantIndices())
848 return false;
849 // fall-thru to increment case
850 [[fallthrough]];
851 case Instruction::Add:
852 case Instruction::Sub:
853 case Instruction::And:
854 case Instruction::Or:
855 case Instruction::Xor:
856 case Instruction::Shl:
857 case Instruction::LShr:
858 case Instruction::AShr: {
859 Value *IVOpnd =
860 !isa<Constant>(I->getOperand(0))
861 ? I->getOperand(0)
862 : !isa<Constant>(I->getOperand(1)) ? I->getOperand(1) : nullptr;
863 if (!IVOpnd)
864 return false;
865
866 // If increment operand is used outside of the loop, this speculation
867 // could cause extra live range interference.
868 if (MultiExitLoop) {
869 for (User *UseI : IVOpnd->users()) {
870 auto *UserInst = cast<Instruction>(UseI);
871 if (!L->contains(UserInst))
872 return false;
873 }
874 }
875
876 if (seenIncrement)
877 return false;
878 seenIncrement = true;
879 break;
880 }
881 case Instruction::Trunc:
882 case Instruction::ZExt:
883 case Instruction::SExt:
884 // ignore type conversions
885 break;
886 }
887 }
888 return true;
889}
890
891/// Fold the loop tail into the loop exit by speculating the loop tail
892/// instructions. Typically, this is a single post-increment. In the case of a
893/// simple 2-block loop, hoisting the increment can be much better than
894/// duplicating the entire loop header. In the case of loops with early exits,
895/// rotation will not work anyway, but simplifyLoopLatch will put the loop in
896/// canonical form so downstream passes can handle it.
897///
898/// I don't believe this invalidates SCEV.
899bool LoopRotate::simplifyLoopLatch(Loop *L) {
900 BasicBlock *Latch = L->getLoopLatch();
901 if (!Latch || Latch->hasAddressTaken())
902 return false;
903
904 UncondBrInst *Jmp = dyn_cast<UncondBrInst>(Latch->getTerminator());
905 if (!Jmp)
906 return false;
907
908 BasicBlock *LastExit = Latch->getSinglePredecessor();
909 if (!LastExit || !L->isLoopExiting(LastExit))
910 return false;
911
913 return false;
914
915 if (!shouldSpeculateInstrs(Latch->begin(), Jmp->getIterator(), L))
916 return false;
917
918 LLVM_DEBUG(dbgs() << "Folding loop latch " << Latch->getName() << " into "
919 << LastExit->getName() << "\n");
920
921 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
922 MergeBlockIntoPredecessor(Latch, &DTU, LI, MSSAU, nullptr,
923 /*PredecessorWithTwoSuccessors=*/true);
924
925 if (SE) {
926 // Merging blocks may remove blocks reference in the block disposition cache. Clear the cache.
928 }
929
930 if (MSSAU && VerifyMemorySSA)
931 MSSAU->getMemorySSA()->verifyMemorySSA();
932
933 return true;
934}
935
936/// Rotate \c L, and return true if any modification was made.
937bool LoopRotate::processLoop(Loop *L) {
938 // Save the loop metadata.
939 MDNode *LoopMD = L->getLoopID();
940
941 bool SimplifiedLatch = false;
942
943 // Simplify the loop latch before attempting to rotate the header
944 // upward. Rotation may not be needed if the loop tail can be folded into the
945 // loop exit.
946 if (!RotationOnly)
947 SimplifiedLatch = simplifyLoopLatch(L);
948
949 bool MadeChange = rotateLoop(L, SimplifiedLatch);
950 assert((!MadeChange || L->isLoopExiting(L->getLoopLatch())) &&
951 "Loop latch should be exiting after loop-rotate.");
952
953 // Restore the loop metadata.
954 // NB! We presume LoopRotation DOESN'T ADD its own metadata.
955 if ((MadeChange || SimplifiedLatch) && LoopMD)
956 L->setLoopID(LoopMD);
957
958 return MadeChange || SimplifiedLatch;
959}
960
961
962/// The utility to convert a loop into a loop with bottom test.
966 const SimplifyQuery &SQ, bool RotationOnly = true,
967 unsigned Threshold = unsigned(-1),
968 bool IsUtilMode = true, bool PrepareForLTO) {
969 LoopRotate LR(Threshold, LI, TTI, AC, DT, SE, MSSAU, SQ, RotationOnly,
970 IsUtilMode, PrepareForLTO);
971 return LR.processLoop(L);
972}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
static void updateBranchWeights(CondBrInst &PreHeaderBI, CondBrInst &LoopBI, bool HasConditionalPreHeader, bool SuccsSwapped)
static constexpr uint32_t ZeroTripCountWeights[]
static bool shouldSpeculateInstrs(BasicBlock::iterator Begin, BasicBlock::iterator End, Loop *L)
Determine whether the instructions in this range may be safely and cheaply speculated.
static bool profitableToRotateLoopExitingLatch(Loop *L)
static void InsertNewValueIntoMap(ValueToValueMapTy &VM, Value *K, Value *V)
Insert (K, V) pair into the ValueToValueMap, and verify the key did not previously exist in the map,...
static void RewriteUsesOfClonedInstructions(BasicBlock *OrigHeader, BasicBlock *OrigPreheader, ValueToValueMapTy &ValueMap, ScalarEvolution *SE, SmallVectorImpl< PHINode * > *InsertedPHIs)
RewriteUsesOfClonedInstructions - We just cloned the instructions from the old header into the prehea...
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
Machine Trace Metrics
Memory SSA
Definition MemorySSA.cpp:72
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
A cache of @llvm.assume calls within a function.
LLVM_ABI void registerAssumption(AssumeInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:483
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:470
const Instruction & back() const
Definition BasicBlock.h:495
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition BasicBlock.h:696
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI void flushTerminatorDbgRecords()
Eject any debug-info trailing at the end of a block.
LLVM_ABI DbgMarker * getMarker(InstListType::iterator It)
Return the DbgMarker for the position given by It, so that DbgRecords can be inserted there.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
void moveBefore(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it into the function that MovePos lives ...
Definition BasicBlock.h:397
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
Conditional Branch instruction.
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
static iterator_range< simple_ilist< DbgRecord >::iterator > getEmptyDbgRecordRange()
LLVM_ABI const BasicBlock * getParent() const
Record of a variable value-assignment, aka a non instruction representation of the dbg....
void applyUpdates(ArrayRef< UpdateType > Updates)
Inform the dominator tree about a sequence of CFG edge insertions and deletions and perform a batch u...
void deleteEdge(NodeT *From, NodeT *To)
Inform the dominator tree about a CFG edge deletion and update the tree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
bool isPresplitCoroutine() const
Determine if the function is presplit coroutine.
Definition Function.h:547
LLVM_ABI Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
LLVM_ABI iterator_range< simple_ilist< DbgRecord >::iterator > cloneDebugInfoFrom(const Instruction *From, std::optional< simple_ilist< DbgRecord >::iterator > FromHere=std::nullopt, bool InsertAtHead=false)
Clone any debug-info attached to From onto this instruction.
LLVM_ABI bool mayWriteToMemory() const LLVM_READONLY
Return true if this instruction may modify memory.
iterator_range< simple_ilist< DbgRecord >::iterator > getDbgRecordRange() const
Return a range over the DbgRecords attached to this instruction.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
bool isTerminator() const
LLVM_ABI bool mayReadFromMemory() const LLVM_READONLY
Return true if this instruction may read memory.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form.
Definition LoopInfo.h:441
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1080
MemorySSA * getMemorySSA() const
Get handle on MemorySSA.
LLVM_ABI void removeEdge(BasicBlock *From, BasicBlock *To)
Update the MemoryPhi in To following an edge deletion between From and To.
LLVM_ABI void updateForClonedBlockIntoPred(BasicBlock *BB, BasicBlock *P1, const ValueToValueMapTy &VM)
LLVM_ABI void applyUpdates(ArrayRef< CFGUpdate > Updates, DominatorTree &DT, bool UpdateDTFirst=false)
Apply CFG updates, analogous with the DT edge updates.
LLVM_ABI void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition SSAUpdater.h:39
The main scalar evolution driver.
LLVM_ABI void forgetTopmostLoop(const Loop *L)
LLVM_ABI void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
LLVM_ABI void forgetBlockAndLoopDispositions(Value *V=nullptr)
Called when the client has changed the disposition of values in a loop or block.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
static UncondBrInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
See the file comment.
Definition ValueMap.h:84
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition ValueMap.h:167
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition ValueMap.h:175
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:397
iterator_range< user_iterator > users()
Definition Value.h:427
bool use_empty() const
Definition Value.h:347
iterator_range< use_iterator > uses()
Definition Value.h:381
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
LLVM_ABI void dump() const
Support for debugging, callable in GDB: V->dump()
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:180
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
IteratorT begin() const
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
LLVM_ABI bool RemoveRedundantDbgInstrs(BasicBlock *BB)
Try to remove redundant dbg.value instructions from given basic block.
LLVM_ABI void findDbgValues(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the dbg.values describing a value.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto successors(const MachineBasicBlock *BB)
LLVM_ABI MDNode * getBranchWeightMDNode(const Instruction &I)
Get the branch weights metadata node.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI void insertDebugValuesForPHIs(BasicBlock *BB, SmallVectorImpl< PHINode * > &InsertedPHIs)
Propagate dbg.value intrinsics through the newly inserted PHIs.
Definition Local.cpp:1894
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
LLVM_ABI void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected, bool ElideAllZero=false)
Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...
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
void RemapDbgRecordRange(Module *M, iterator_range< DbgRecordIterator > Range, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataPredicate *IdentityMD=nullptr)
Remap the Values used in the DbgRecords Range using the value map VM.
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition ValueMapper.h:98
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition ValueMapper.h:80
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
LLVM_ABI void extractFromBranchWeightMD32(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Faster version of extractBranchWeights() that skips checks and must only be called with "branch_weigh...
TargetTransformInfo TTI
LLVM_ABI bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition MemorySSA.cpp:84
LLVM_ABI bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr, bool PredecessorWithTwoSuccessors=false, DominatorTree *DT=nullptr)
Attempts to merge a block into its predecessor, if possible.
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataPredicate *IdentityMD=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM.
LLVM_ABI BasicBlock * SplitCriticalEdge(Instruction *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions(), const Twine &BBName="")
If this edge is a critical edge, insert a new node to split the critical edge.
LLVM_ABI void cloneAndAdaptNoAliasScopes(ArrayRef< MDNode * > NoAliasDeclScopes, ArrayRef< BasicBlock * > NewBlocks, LLVMContext &Context, StringRef Ext)
Clone the specified noalias decl scopes.
LLVM_ABI bool FoldSingleEntryPHINodes(BasicBlock *BB, MemoryDependenceResults *MemDep=nullptr)
We know that BB has one predecessor.
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto predecessors(const MachineBasicBlock *BB)
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
LLVM_ABI bool LoopRotation(Loop *L, LoopInfo *LI, const TargetTransformInfo *TTI, AssumptionCache *AC, DominatorTree *DT, ScalarEvolution *SE, MemorySSAUpdater *MSSAU, const SimplifyQuery &SQ, bool RotationOnly, unsigned Threshold, bool IsUtilMode, bool PrepareForLTO=false)
Convert a loop into a loop with bottom test.
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:466
LLVM_ABI void mapAtomInstance(const DebugLoc &DL, ValueToValueMapTy &VMap)
Mark a cloned instruction as a new instance so that its source loc can be updated when remapped.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
static LLVM_ABI void collectEphemeralValues(const Loop *L, AssumptionCache *AC, SmallPtrSetImpl< const Value * > &EphValues)
Collect a loop's ephemeral values (those used only by an assume or similar intrinsics in the loop).