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