LLVM 24.0.0git
EarlyIfConversion.cpp
Go to the documentation of this file.
1//===-- EarlyIfConversion.cpp - If-conversion on SSA form machine code ----===//
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// Early if-conversion is for out-of-order CPUs that don't have a lot of
10// predicable instructions. The goal is to eliminate conditional branches that
11// may mispredict.
12//
13// Instructions from both sides of the branch are executed specutatively, and a
14// cmov instruction selects the result.
15//
16//===----------------------------------------------------------------------===//
17
19#include "CodeGenOptions.h"
20#include "llvm/ADT/BitVector.h"
21#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/SparseSet.h"
25#include "llvm/ADT/Statistic.h"
45#include "llvm/Support/Debug.h"
47
48using namespace llvm;
49
50#define DEBUG_TYPE "early-ifcvt"
51
52STATISTIC(NumDiamondsSeen, "Number of diamonds");
53STATISTIC(NumDiamondsConv, "Number of diamonds converted");
54STATISTIC(NumTrianglesSeen, "Number of triangles");
55STATISTIC(NumTrianglesConv, "Number of triangles converted");
56STATISTIC(NumDataDependant,
57 "Number of data dependent conditional branches encountered");
58STATISTIC(NumLikelyBiased, "Number of branches with a hot path encountered");
59STATISTIC(NumStronglyBiased,
60 "Number of branches with reduced critical-path budget");
61
62//===----------------------------------------------------------------------===//
63// SSAIfConv
64//===----------------------------------------------------------------------===//
65//
66// The SSAIfConv class performs if-conversion on SSA form machine code after
67// determining if it is possible. The class contains no heuristics; external
68// code should be used to determine when if-conversion is a good idea.
69//
70// SSAIfConv can convert both triangles and diamonds:
71//
72// Triangle: Head Diamond: Head
73// | \ / \_
74// | \ / |
75// | [TF]BB FBB TBB
76// | / \ /
77// | / \ /
78// Tail Tail
79//
80// Instructions in the conditional blocks TBB and/or FBB are spliced into the
81// Head block, and phis in the Tail block are converted to select instructions.
82//
83namespace {
84class SSAIfConv {
85 const CodeGenOptions *Opts;
86 const TargetInstrInfo *TII;
89
90public:
91 /// The block containing the conditional branch.
93
94 /// The block containing phis after the if-then-else.
96
97 /// The 'true' conditional block as determined by analyzeBranch.
99
100 /// The 'false' conditional block as determined by analyzeBranch.
102
103 /// isTriangle - When there is no 'else' block, either TBB or FBB will be
104 /// equal to Tail.
105 bool isTriangle() const { return TBB == Tail || FBB == Tail; }
106
107 /// Returns the Tail predecessor for the True side.
108 MachineBasicBlock *getTPred() const { return TBB == Tail ? Head : TBB; }
109
110 /// Returns the Tail predecessor for the False side.
111 MachineBasicBlock *getFPred() const { return FBB == Tail ? Head : FBB; }
112
113 /// Information about each phi in the Tail block.
114 struct PHIInfo {
115 MachineInstr *PHI;
116 Register TReg, FReg;
117 /// Description of the select that will replace this phi.
118 TargetInstrInfo::SelectExpansion Select;
119
120 PHIInfo(MachineInstr *phi) : PHI(phi) {}
121 };
122
124
125 /// The branch condition determined by analyzeBranch.
126 SmallVector<MachineOperand, 4> Cond;
127
128private:
129 /// Instructions in Head that define values used by the conditional blocks.
130 /// The hoisted instructions must be inserted after these instructions.
131 SmallPtrSet<MachineInstr*, 8> InsertAfter;
132
133 /// Register units clobbered by the conditional blocks.
134 BitVector ClobberedRegUnits;
135
136 // Scratch pad for findInsertionPoint.
137 SparseSet<MCRegUnit, MCRegUnit, MCRegUnitToIndex> LiveRegUnits;
138
139 /// Insertion point in Head for speculatively executed instructions form TBB
140 /// and FBB.
141 MachineBasicBlock::iterator InsertionPoint;
142
143 /// Return true if all non-terminator instructions in MBB can be safely
144 /// speculated.
145 bool canSpeculateInstrs(MachineBasicBlock *MBB);
146
147 /// Return true if all non-terminator instructions in MBB can be safely
148 /// predicated.
149 bool canPredicateInstrs(MachineBasicBlock *MBB);
150
151 /// Scan through instruction dependencies and update InsertAfter array.
152 /// Return false if any dependency is incompatible with if conversion.
153 bool InstrDependenciesAllowIfConv(MachineInstr *I);
154
155 /// Predicate all instructions of the basic block with current condition
156 /// except for terminators. Reverse the condition if ReversePredicate is set.
157 void PredicateBlock(MachineBasicBlock *MBB, bool ReversePredicate);
158
159 /// Find a valid insertion point in Head.
160 bool findInsertionPoint();
161
162 /// Replace PHI instructions in Tail with selects.
163 void replacePHIInstrs();
164
165 /// Insert selects and rewrite PHI operands to use them.
166 void rewritePHIOperands();
167
168public:
169 /// init - Initialize per-function data structures.
170 void init(MachineFunction &MF) {
171 Opts = &getCodeGenOptions(MF);
172 TII = MF.getSubtarget().getInstrInfo();
173 TRI = MF.getSubtarget().getRegisterInfo();
174 MRI = &MF.getRegInfo();
175 LiveRegUnits.clear();
176 LiveRegUnits.setUniverse(TRI->getNumRegUnits());
177 ClobberedRegUnits.clear();
178 ClobberedRegUnits.resize(TRI->getNumRegUnits());
179 }
180
181 /// canConvertIf - If the sub-CFG headed by MBB can be if-converted,
182 /// initialize the internal state, and return true.
183 /// If predicate is set try to predicate the block otherwise try to
184 /// speculatively execute it.
185 bool canConvertIf(MachineBasicBlock *MBB, bool Predicate = false);
186
187 /// convertIf - If-convert the last block passed to canConvertIf(), assuming
188 /// it is possible. Add any blocks that are to be erased to RemoveBlocks.
189 void convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
190 bool Predicate = false);
191};
192} // end anonymous namespace
193
194/// canSpeculateInstrs - Returns true if all the instructions in MBB can safely
195/// be speculated. The terminators are not considered.
196///
197/// If instructions use any values that are defined in the head basic block,
198/// the defining instructions are added to InsertAfter.
199///
200/// Any clobbered regunits are added to ClobberedRegUnits.
201///
202bool SSAIfConv::canSpeculateInstrs(MachineBasicBlock *MBB) {
203 // Reject any live-in physregs. It's probably CPSR/EFLAGS, and very hard to
204 // get right.
205 if (!MBB->livein_empty()) {
206 LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has live-ins.\n");
207 return false;
208 }
209
210 unsigned InstrCount = 0;
211
212 // Check all instructions, except the terminators. It is assumed that
213 // terminators never have side effects or define any used register values.
214 for (MachineInstr &MI :
216 if (MI.isDebugInstr())
217 continue;
218
219 if (++InstrCount > Opts->early_ifcvt_limit && !Opts->stress_early_ifcvt) {
220 LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has more than "
221 << Opts->early_ifcvt_limit << " instructions.\n");
222 return false;
223 }
224
225 // There shouldn't normally be any phis in a single-predecessor block.
226 if (MI.isPHI()) {
227 LLVM_DEBUG(dbgs() << "Can't hoist: " << MI);
228 return false;
229 }
230
231 // Don't speculate loads. Note that it may be possible and desirable to
232 // speculate GOT or constant pool loads that are guaranteed not to trap,
233 // but we don't support that for now.
234 if (MI.mayLoad()) {
235 LLVM_DEBUG(dbgs() << "Won't speculate load: " << MI);
236 return false;
237 }
238
239 // We never speculate stores, so an AA pointer isn't necessary.
240 bool DontMoveAcrossStore = true;
241 if (!MI.isSafeToMove(DontMoveAcrossStore)) {
242 LLVM_DEBUG(dbgs() << "Can't speculate: " << MI);
243 return false;
244 }
245
246 // Check for any dependencies on Head instructions.
247 if (!InstrDependenciesAllowIfConv(&MI))
248 return false;
249 }
250 return true;
251}
252
253/// Check that there is no dependencies preventing if conversion.
254///
255/// If instruction uses any values that are defined in the head basic block,
256/// the defining instructions are added to InsertAfter.
257bool SSAIfConv::InstrDependenciesAllowIfConv(MachineInstr *I) {
258 for (const MachineOperand &MO : I->operands()) {
259 if (MO.isRegMask()) {
260 LLVM_DEBUG(dbgs() << "Won't speculate regmask: " << *I);
261 return false;
262 }
263 if (!MO.isReg())
264 continue;
265 Register Reg = MO.getReg();
266
267 // Remember clobbered regunits.
268 if (MO.isDef() && Reg.isPhysical())
269 for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg()))
270 ClobberedRegUnits.set(static_cast<unsigned>(Unit));
271
272 if (!MO.readsReg() || !Reg.isVirtual())
273 continue;
274 MachineInstr *DefMI = MRI->getVRegDef(Reg);
275 if (!DefMI || DefMI->getParent() != Head)
276 continue;
277 if (InsertAfter.insert(DefMI).second)
278 LLVM_DEBUG(dbgs() << printMBBReference(*I->getParent()) << " depends on "
279 << *DefMI);
280 if (DefMI->isTerminator()) {
281 LLVM_DEBUG(dbgs() << "Can't insert instructions below terminator.\n");
282 return false;
283 }
284 }
285 return true;
286}
287
288/// canPredicateInstrs - Returns true if all the instructions in MBB can safely
289/// be predicates. The terminators are not considered.
290///
291/// If instructions use any values that are defined in the head basic block,
292/// the defining instructions are added to InsertAfter.
293///
294/// Any clobbered regunits are added to ClobberedRegUnits.
295///
296bool SSAIfConv::canPredicateInstrs(MachineBasicBlock *MBB) {
297 // Reject any live-in physregs. It's probably CPSR/EFLAGS, and very hard to
298 // get right.
299 if (!MBB->livein_empty()) {
300 LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has live-ins.\n");
301 return false;
302 }
303
304 unsigned InstrCount = 0;
305
306 // Check all instructions, except the terminators. It is assumed that
307 // terminators never have side effects or define any used register values.
310 I != E; ++I) {
311 if (I->isDebugInstr())
312 continue;
313
314 if (++InstrCount > Opts->early_ifcvt_limit && !Opts->stress_early_ifcvt) {
315 LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has more than "
316 << Opts->early_ifcvt_limit << " instructions.\n");
317 return false;
318 }
319
320 // There shouldn't normally be any phis in a single-predecessor block.
321 if (I->isPHI()) {
322 LLVM_DEBUG(dbgs() << "Can't predicate: " << *I);
323 return false;
324 }
325
326 // Check that instruction is predicable
327 if (!TII->isPredicable(*I)) {
328 LLVM_DEBUG(dbgs() << "Isn't predicable: " << *I);
329 return false;
330 }
331
332 // Check that instruction is not already predicated.
333 if (TII->isPredicated(*I) && !TII->canPredicatePredicatedInstr(*I)) {
334 LLVM_DEBUG(dbgs() << "Is already predicated: " << *I);
335 return false;
336 }
337
338 // Check for any dependencies on Head instructions.
339 if (!InstrDependenciesAllowIfConv(&(*I)))
340 return false;
341 }
342 return true;
343}
344
345// Apply predicate to all instructions in the machine block.
346void SSAIfConv::PredicateBlock(MachineBasicBlock *MBB, bool ReversePredicate) {
347 auto Condition = Cond;
348 if (ReversePredicate) {
349 bool CanRevCond = !TII->reverseBranchCondition(Condition);
350 assert(CanRevCond && "Reversed predicate is not supported");
351 (void)CanRevCond;
352 }
353 // Terminators don't need to be predicated as they will be removed.
356 I != E; ++I) {
357 if (I->isDebugInstr())
358 continue;
359 TII->PredicateInstruction(*I, Condition);
360 }
361}
362
363/// Find an insertion point in Head for the speculated instructions. The
364/// insertion point must be:
365///
366/// 1. Before any terminators.
367/// 2. After any instructions in InsertAfter.
368/// 3. Not have any clobbered regunits live.
369///
370/// This function sets InsertionPoint and returns true when successful, it
371/// returns false if no valid insertion point could be found.
372///
373bool SSAIfConv::findInsertionPoint() {
374 // Keep track of live regunits before the current position.
375 // Only track RegUnits that are also in ClobberedRegUnits.
376 LiveRegUnits.clear();
381 while (I != B) {
382 --I;
383 // Some of the conditional code depends in I.
384 if (InsertAfter.count(&*I)) {
385 LLVM_DEBUG(dbgs() << "Can't insert code after " << *I);
386 return false;
387 }
388
389 // Update live regunits.
390 for (const MachineOperand &MO : I->operands()) {
391 // We're ignoring regmask operands. That is conservatively correct.
392 if (!MO.isReg())
393 continue;
394 Register Reg = MO.getReg();
395 if (!Reg.isPhysical())
396 continue;
397 // I clobbers Reg, so it isn't live before I.
398 if (MO.isDef())
399 for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg()))
400 LiveRegUnits.erase(Unit);
401 // Unless I reads Reg.
402 if (MO.readsReg())
403 Reads.push_back(Reg.asMCReg());
404 }
405 // Anything read by I is live before I.
406 while (!Reads.empty())
407 for (MCRegUnit Unit : TRI->regunits(Reads.pop_back_val()))
408 if (ClobberedRegUnits.test(static_cast<unsigned>(Unit)))
409 LiveRegUnits.insert(Unit);
410
411 // We can't insert before a terminator.
412 if (I != FirstTerm && I->isTerminator())
413 continue;
414
415 // Some of the clobbered registers are live before I, not a valid insertion
416 // point.
417 if (!LiveRegUnits.empty()) {
418 LLVM_DEBUG({
419 dbgs() << "Would clobber";
420 for (MCRegUnit LRU : LiveRegUnits)
421 dbgs() << ' ' << printRegUnit(LRU, TRI);
422 dbgs() << " live before " << *I;
423 });
424 continue;
425 }
426
427 // This is a valid insertion point.
428 InsertionPoint = I;
429 LLVM_DEBUG(dbgs() << "Can insert before " << *I);
430 return true;
431 }
432 LLVM_DEBUG(dbgs() << "No legal insertion point found.\n");
433 return false;
434}
435
436
437
438/// canConvertIf - analyze the sub-cfg rooted in MBB, and return true if it is
439/// a potential candidate for if-conversion. Fill out the internal state.
440///
441bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB, bool Predicate) {
442 Head = MBB;
443 TBB = FBB = Tail = nullptr;
444
445 if (Head->succ_size() != 2)
446 return false;
447 MachineBasicBlock *Succ0 = Head->succ_begin()[0];
448 MachineBasicBlock *Succ1 = Head->succ_begin()[1];
449
450 // Canonicalize so Succ0 has MBB as its single predecessor.
451 if (Succ0->pred_size() != 1)
452 std::swap(Succ0, Succ1);
453
454 if (Succ0->pred_size() != 1 || Succ0->succ_size() != 1)
455 return false;
456
457 Tail = Succ0->succ_begin()[0];
458
459 // This is not a triangle.
460 if (Tail != Succ1) {
461 // Check for a diamond. We won't deal with any critical edges.
462 if (Succ1->pred_size() != 1 || Succ1->succ_size() != 1 ||
463 Succ1->succ_begin()[0] != Tail)
464 return false;
465 LLVM_DEBUG(dbgs() << "\nDiamond: " << printMBBReference(*Head) << " -> "
466 << printMBBReference(*Succ0) << "/"
467 << printMBBReference(*Succ1) << " -> "
468 << printMBBReference(*Tail) << '\n');
469
470 // Live-in physregs are tricky to get right when speculating code.
471 if (!Tail->livein_empty()) {
472 LLVM_DEBUG(dbgs() << "Tail has live-ins.\n");
473 return false;
474 }
475 } else {
476 LLVM_DEBUG(dbgs() << "\nTriangle: " << printMBBReference(*Head) << " -> "
477 << printMBBReference(*Succ0) << " -> "
478 << printMBBReference(*Tail) << '\n');
479 }
480
481 // This is a triangle or a diamond.
482 // Skip if we cannot predicate and there are no phis skip as there must be
483 // side effects that can only be handled with predication.
484 if (!Predicate && (Tail->empty() || !Tail->front().isPHI())) {
485 LLVM_DEBUG(dbgs() << "No phis in tail.\n");
486 return false;
487 }
488
489 // The branch we're looking to eliminate must be analyzable.
490 Cond.clear();
491 if (TII->analyzeBranch(*Head, TBB, FBB, Cond)) {
492 LLVM_DEBUG(dbgs() << "Branch not analyzable.\n");
493 return false;
494 }
495
496 // This is weird, probably some sort of degenerate CFG.
497 if (!TBB) {
498 LLVM_DEBUG(dbgs() << "analyzeBranch didn't find conditional branch.\n");
499 return false;
500 }
501
502 // Make sure the analyzed branch is conditional; one of the successors
503 // could be a landing pad. (Empty landing pads can be generated on Windows.)
504 if (Cond.empty()) {
505 LLVM_DEBUG(dbgs() << "analyzeBranch found an unconditional branch.\n");
506 return false;
507 }
508
509 // analyzeBranch doesn't set FBB on a fall-through branch.
510 // Make sure it is always set.
511 FBB = TBB == Succ0 ? Succ1 : Succ0;
512
513 // Any phis in the tail block must be convertible to selects.
514 PHIs.clear();
515 MachineBasicBlock *TPred = getTPred();
516 MachineBasicBlock *FPred = getFPred();
517 for (MachineBasicBlock::iterator I = Tail->begin(), E = Tail->end();
518 I != E && I->isPHI(); ++I) {
519 PHIs.push_back(&*I);
520 PHIInfo &PI = PHIs.back();
521 // Find PHI operands corresponding to TPred and FPred.
522 for (unsigned i = 1; i != PI.PHI->getNumOperands(); i += 2) {
523 if (PI.PHI->getOperand(i+1).getMBB() == TPred)
524 PI.TReg = PI.PHI->getOperand(i).getReg();
525 if (PI.PHI->getOperand(i+1).getMBB() == FPred)
526 PI.FReg = PI.PHI->getOperand(i).getReg();
527 }
528 assert(PI.TReg.isVirtual() && "Bad PHI");
529 assert(PI.FReg.isVirtual() && "Bad PHI");
530
531 // Get target information.
532 if (!TII->canInsertSelect(*Head, Cond, PI.PHI->getOperand(0).getReg(),
533 PI.TReg, PI.FReg, PI.Select)) {
534 LLVM_DEBUG(dbgs() << "Can't convert: " << *PI.PHI);
535 return false;
536 }
537 }
538
539 // Check that the conditional instructions can be speculated.
540 InsertAfter.clear();
541 ClobberedRegUnits.reset();
542 if (Predicate) {
543 if (TBB != Tail && !canPredicateInstrs(TBB))
544 return false;
545 if (FBB != Tail && !canPredicateInstrs(FBB))
546 return false;
547 } else {
548 if (TBB != Tail && !canSpeculateInstrs(TBB))
549 return false;
550 if (FBB != Tail && !canSpeculateInstrs(FBB))
551 return false;
552 }
553
554 // Try to find a valid insertion point for the speculated instructions in the
555 // head basic block.
556 if (!findInsertionPoint())
557 return false;
558
559 if (isTriangle())
560 ++NumTrianglesSeen;
561 else
562 ++NumDiamondsSeen;
563 return true;
564}
565
566/// \return true iff the two registers are known to have the same value.
568 const TargetInstrInfo *TII, Register TReg,
569 Register FReg) {
570 if (TReg == FReg)
571 return true;
572
573 if (!TReg.isVirtual() || !FReg.isVirtual())
574 return false;
575
576 const MachineInstr *TDef = MRI.getUniqueVRegDef(TReg);
577 const MachineInstr *FDef = MRI.getUniqueVRegDef(FReg);
578 if (!TDef || !FDef)
579 return false;
580
581 // If there are side-effects, all bets are off.
582 if (TDef->hasUnmodeledSideEffects())
583 return false;
584
585 // If the instruction could modify memory, or there may be some intervening
586 // store between the two, we can't consider them to be equal.
587 if (TDef->mayLoadOrStore() && !TDef->isDereferenceableInvariantLoad())
588 return false;
589
590 // We also can't guarantee that they are the same if, for example, the
591 // instructions are both a copy from a physical reg, because some other
592 // instruction may have modified the value in that reg between the two
593 // defining insts.
594 if (any_of(TDef->uses(), [](const MachineOperand &MO) {
595 return MO.isReg() && MO.getReg().isPhysical();
596 }))
597 return false;
598
599 // Check whether the two defining instructions produce the same value(s).
600 if (!TII->produceSameValue(*TDef, *FDef, &MRI))
601 return false;
602
603 // Further, check that the two defs come from corresponding operands.
604 int TIdx = TDef->findRegisterDefOperandIdx(TReg, /*TRI=*/nullptr);
605 int FIdx = FDef->findRegisterDefOperandIdx(FReg, /*TRI=*/nullptr);
606 if (TIdx == -1 || FIdx == -1)
607 return false;
608
609 return TIdx == FIdx;
610}
611
612/// replacePHIInstrs - Completely replace PHI instructions with selects.
613/// This is possible when the only Tail predecessors are the if-converted
614/// blocks.
615void SSAIfConv::replacePHIInstrs() {
616 assert(Tail->pred_size() == 2 && "Cannot replace PHIs");
618 assert(FirstTerm != Head->end() && "No terminators");
619 DebugLoc HeadDL = FirstTerm->getDebugLoc();
620
621 // Convert all PHIs to select instructions inserted before FirstTerm.
622 for (PHIInfo &PI : PHIs) {
623 LLVM_DEBUG(dbgs() << "If-converting " << *PI.PHI);
624 Register DstReg = PI.PHI->getOperand(0).getReg();
625 if (hasSameValue(*MRI, TII, PI.TReg, PI.FReg)) {
626 // We do not need the select instruction if both incoming values are
627 // equal, but we do need a COPY.
628 BuildMI(*Head, FirstTerm, HeadDL, TII->get(TargetOpcode::COPY), DstReg)
629 .addReg(PI.TReg);
630 } else {
631 TII->insertSelect(*Head, FirstTerm, HeadDL, DstReg, Cond, PI.TReg,
632 PI.FReg);
633 }
634 LLVM_DEBUG(dbgs() << " --> " << *std::prev(FirstTerm));
635 PI.PHI->eraseFromParent();
636 PI.PHI = nullptr;
637 }
638}
639
640/// rewritePHIOperands - When there are additional Tail predecessors, insert
641/// select instructions in Head and rewrite PHI operands to use the selects.
642/// Keep the PHI instructions in Tail to handle the other predecessors.
643void SSAIfConv::rewritePHIOperands() {
645 assert(FirstTerm != Head->end() && "No terminators");
646 DebugLoc HeadDL = FirstTerm->getDebugLoc();
647
648 // Convert all PHIs to select instructions inserted before FirstTerm.
649 for (PHIInfo &PI : PHIs) {
650 Register DstReg;
651
652 LLVM_DEBUG(dbgs() << "If-converting " << *PI.PHI);
653 if (hasSameValue(*MRI, TII, PI.TReg, PI.FReg)) {
654 // We do not need the select instruction if both incoming values are
655 // equal.
656 DstReg = PI.TReg;
657 } else {
658 Register PHIDst = PI.PHI->getOperand(0).getReg();
659 DstReg = MRI->createVirtualRegister(MRI->getRegClass(PHIDst));
660 TII->insertSelect(*Head, FirstTerm, HeadDL,
661 DstReg, Cond, PI.TReg, PI.FReg);
662 LLVM_DEBUG(dbgs() << " --> " << *std::prev(FirstTerm));
663 }
664
665 // Rewrite PHI operands TPred -> (DstReg, Head), remove FPred.
666 for (unsigned i = PI.PHI->getNumOperands(); i != 1; i -= 2) {
667 MachineBasicBlock *MBB = PI.PHI->getOperand(i-1).getMBB();
668 if (MBB == getTPred()) {
669 PI.PHI->getOperand(i-1).setMBB(Head);
670 PI.PHI->getOperand(i-2).setReg(DstReg);
671 } else if (MBB == getFPred()) {
672 PI.PHI->removeOperand(i-1);
673 PI.PHI->removeOperand(i-2);
674 }
675 }
676 LLVM_DEBUG(dbgs() << " --> " << *PI.PHI);
677 }
678}
679
680/// convertIf - Execute the if conversion after canConvertIf has determined the
681/// feasibility.
682///
683/// Any basic blocks that need to be erased will be added to RemoveBlocks.
684///
685void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
686 bool Predicate) {
687 assert(Head && Tail && TBB && FBB && "Call canConvertIf first.");
688
689 // Update statistics.
690 if (isTriangle())
691 ++NumTrianglesConv;
692 else
693 ++NumDiamondsConv;
694
695 // Move all instructions into Head, except for the terminators.
696 if (TBB != Tail) {
697 if (Predicate)
698 PredicateBlock(TBB, /*ReversePredicate=*/false);
699 Head->splice(InsertionPoint, TBB, TBB->begin(), TBB->getFirstTerminator());
700 }
701 if (FBB != Tail) {
702 if (Predicate)
703 PredicateBlock(FBB, /*ReversePredicate=*/true);
704 Head->splice(InsertionPoint, FBB, FBB->begin(), FBB->getFirstTerminator());
705 }
706 // Are there extra Tail predecessors?
707 bool ExtraPreds = Tail->pred_size() != 2;
708 if (ExtraPreds)
709 rewritePHIOperands();
710 else
711 replacePHIInstrs();
712
713 // Fix up the CFG, temporarily leave Head without any successors.
714 Head->removeSuccessor(TBB);
715 Head->removeSuccessor(FBB, true);
716 if (TBB != Tail)
717 TBB->removeSuccessor(Tail, true);
718 if (FBB != Tail)
719 FBB->removeSuccessor(Tail, true);
720
721 // Fix up Head's terminators.
722 // It should become a single branch or a fallthrough.
723 DebugLoc HeadDL = Head->getFirstTerminator()->getDebugLoc();
724 TII->removeBranch(*Head);
725
726 // Mark the now empty conditional blocks for removal and move them to the end.
727 // It is likely that Head can fall
728 // through to Tail, and we can join the two blocks.
729 if (TBB != Tail) {
730 RemoveBlocks.push_back(TBB);
731 if (TBB != &TBB->getParent()->back())
732 TBB->moveAfter(&TBB->getParent()->back());
733 }
734 if (FBB != Tail) {
735 RemoveBlocks.push_back(FBB);
736 if (FBB != &FBB->getParent()->back())
737 FBB->moveAfter(&FBB->getParent()->back());
738 }
739
740 assert(Head->succ_empty() && "Additional head successors?");
741 if (!ExtraPreds && Head->isLayoutSuccessor(Tail)) {
742 // Splice Tail onto the end of Head.
743 LLVM_DEBUG(dbgs() << "Joining tail " << printMBBReference(*Tail)
744 << " into head " << printMBBReference(*Head) << '\n');
745 Head->splice(Head->end(), Tail,
746 Tail->begin(), Tail->end());
748 RemoveBlocks.push_back(Tail);
749 if (Tail != &Tail->getParent()->back())
750 Tail->moveAfter(&Tail->getParent()->back());
751 } else {
752 // We need a branch to Tail, let code placement work it out later.
753 LLVM_DEBUG(dbgs() << "Converting to unconditional branch.\n");
755 TII->insertBranch(*Head, Tail, nullptr, EmptyCond, HeadDL);
756 Head->addSuccessor(Tail);
757 }
758 LLVM_DEBUG(dbgs() << *Head);
759}
760
761//===----------------------------------------------------------------------===//
762// EarlyIfConverter Pass
763//===----------------------------------------------------------------------===//
764
765namespace {
766class EarlyIfConverter {
767 const CodeGenOptions &Opts;
768 const TargetInstrInfo *TII = nullptr;
769 const TargetSubtargetInfo *STI = nullptr;
770 MachineRegisterInfo *MRI = nullptr;
771 MachineDominatorTree *DomTree = nullptr;
772 MachineLoopInfo *Loops = nullptr;
773 MachineTraceMetrics *Traces = nullptr;
774 MachineTraceMetrics::Ensemble *MinInstr = nullptr;
775 MachineBranchProbabilityInfo &MBPI;
776 SSAIfConv IfConv;
777
778 /// Cache of basic blocks verified to contain no call instructions, mapping
779 /// each block to the number of instructions scanned in it.
780 DenseMap<const MachineBasicBlock *, unsigned> NoCallBlocksCache;
781
782public:
783 EarlyIfConverter(const CodeGenOptions &Opts, MachineDominatorTree &DT,
784 MachineLoopInfo &LI, MachineTraceMetrics &MTM,
785 MachineBranchProbabilityInfo &MBPI)
786 : Opts(Opts), DomTree(&DT), Loops(&LI), Traces(&MTM), MBPI(MBPI) {}
787 EarlyIfConverter() = delete;
788
789 bool run(MachineFunction &MF);
790
791private:
792 bool tryConvertIf(MachineBasicBlock *);
793 void invalidateTraces();
794 bool shouldConvertIf();
795 bool isConditionDataDependent();
796 bool doOperandsComeFromMemory(const MachineInstr *ConditionDef);
797 bool hasCallOrLoopInRange(const MachineInstr *From, const MachineInstr *To);
798};
799
800class EarlyIfConverterLegacy : public MachineFunctionPass {
801public:
802 static char ID;
803 EarlyIfConverterLegacy() : MachineFunctionPass(ID) {}
804 void getAnalysisUsage(AnalysisUsage &AU) const override;
805 bool runOnMachineFunction(MachineFunction &MF) override;
806 StringRef getPassName() const override { return "Early If-Conversion"; }
807};
808} // end anonymous namespace
809
810char EarlyIfConverterLegacy::ID = 0;
811char &llvm::EarlyIfConverterLegacyID = EarlyIfConverterLegacy::ID;
812
813INITIALIZE_PASS_BEGIN(EarlyIfConverterLegacy, DEBUG_TYPE, "Early If Converter",
814 false, false)
818INITIALIZE_PASS_END(EarlyIfConverterLegacy, DEBUG_TYPE, "Early If Converter",
820
821void EarlyIfConverterLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
822 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
824 AU.addRequired<MachineDominatorTreeWrapperPass>();
825 AU.addPreserved<MachineDominatorTreeWrapperPass>();
826 AU.addRequired<MachineLoopInfoWrapperPass>();
827 AU.addPreserved<MachineLoopInfoWrapperPass>();
828 AU.addRequired<MachineTraceMetricsWrapperPass>();
829 AU.addPreserved<MachineTraceMetricsWrapperPass>();
831}
832
833namespace {
834/// Update the dominator tree after if-conversion erased some blocks.
835void updateDomTree(MachineDominatorTree *DomTree, const SSAIfConv &IfConv,
837 // convertIf can remove TBB, FBB, and Tail can be merged into Head.
838 // TBB and FBB should not dominate any blocks.
839 // Tail children should be transferred to Head.
840 MachineDomTreeNode *HeadNode = DomTree->getNode(IfConv.Head);
841 for (auto *B : Removed) {
842 MachineDomTreeNode *Node = DomTree->getNode(B);
843 assert(Node != HeadNode && "Cannot erase the head node");
844 while (!Node->isLeaf()) {
845 assert(Node->getBlock() == IfConv.Tail && "Unexpected children");
846 DomTree->changeImmediateDominator(*Node->begin(), HeadNode);
847 }
848 DomTree->eraseNode(B);
849 }
850}
851
852/// Update LoopInfo after if-conversion.
853void updateLoops(MachineLoopInfo *Loops,
855 // If-conversion doesn't change loop structure, and it doesn't mess with back
856 // edges, so updating LoopInfo is simply removing the dead blocks.
857 for (auto *B : Removed)
858 Loops->removeBlock(B);
859}
860} // namespace
861
862/// Invalidate MachineTraceMetrics before if-conversion.
863void EarlyIfConverter::invalidateTraces() {
864 Traces->verifyAnalysis();
865 Traces->invalidate(IfConv.Head);
866 Traces->invalidate(IfConv.Tail);
867 Traces->invalidate(IfConv.TBB);
868 Traces->invalidate(IfConv.FBB);
869 Traces->verifyAnalysis();
870}
871
872static bool isConstantPoolLoad(const MachineInstr *MI) {
873 return MI->mayLoad() && any_of(MI->memoperands(), [](MachineMemOperand *MOp) {
874 const PseudoSourceValue *PSV = MOp->getPseudoValue();
875 return PSV && PSV->isConstantPool();
876 });
877}
878
879/// Check whether the load in From and the condition in To are far apart, i.e.
880/// whether a call or a loop can be executed between them. This is done by first
881/// scanning the instructions within From and To MBBs. If no call is found, we
882/// then scan all blocks which are dominated by From (the load) and can reach To
883/// (the condition), looking for calls and for blocks belonging to a loop the
884/// condition is not part of.
885bool EarlyIfConverter::hasCallOrLoopInRange(const MachineInstr *From,
886 const MachineInstr *To) {
887 if (From == To)
888 return false;
889
890 LLVM_DEBUG(dbgs() << " checking for a call or loop between " << *From
891 << " and " << *To);
892 assert(DomTree->dominates(From, To) && "From is expected to dominate To");
893
894 const MachineBasicBlock *FromBB = From->getParent();
895 const MachineBasicBlock *ToBB = To->getParent();
896
897 unsigned NumScanned = 0;
898 auto HitSearchLimit = [&](unsigned N) {
899 NumScanned += N;
900 if (NumScanned <= Opts.early_ifcvt_max_region_instrs)
901 return false;
902 LLVM_DEBUG(dbgs() << " hasCallOrLoopInRange scanned more than "
903 << Opts.early_ifcvt_max_region_instrs
904 << " instructions\n");
905 return true;
906 };
907 auto FoundCall = [](const MachineInstr &MI) {
908 LLVM_DEBUG(dbgs() << " found a call before the condition: " << MI);
909 return true;
910 };
911 auto IsCallOrHitSearchLimit = [&](const MachineInstr &MI) {
912 if (HitSearchLimit(1))
913 return true;
914 if (!MI.isCall())
915 return false;
916 return FoundCall(MI);
917 };
918
919 // If From and To are in the same block, just check (From, To).
920 if (FromBB == ToBB) {
921 for (const MachineInstr &MI : instructionsWithoutDebug(
922 std::next(From->getIterator()), To->getIterator(),
923 /*SkipPseudoOp=*/false))
924 if (IsCallOrHitSearchLimit(MI))
925 return true;
926 return false;
927 }
928
929 // Check (From, end of From's block] and [start of To's block, To).
930 for (const MachineInstr &MI : instructionsWithoutDebug(
931 std::next(From->getIterator()), FromBB->instr_end(),
932 /*SkipPseudoOp=*/false))
933 if (IsCallOrHitSearchLimit(MI))
934 return true;
935 for (const MachineInstr &MI :
937 /*SkipPseudoOp=*/false))
938 if (IsCallOrHitSearchLimit(MI))
939 return true;
940
941 // Enqueued guards the traversal: the endpoint blocks are traversed through
942 // but their instructions were already handled above.
943 SmallPtrSet<const MachineBasicBlock *, 16> Enqueued = {FromBB, ToBB};
945 auto Enqueue = [&](const MachineBasicBlock *BB) {
946 if (DomTree->dominates(FromBB, BB) && Enqueued.insert(BB).second)
947 Worklist.push_back(BB);
948 };
949
950 for (const MachineBasicBlock *Pred : ToBB->predecessors())
951 Enqueue(Pred);
952
953 while (!Worklist.empty()) {
954 const MachineBasicBlock *BB = Worklist.pop_back_val();
955
956 // If the block belongs to a loop containing neither the load nor the
957 // condition, that loop is executed entirely between the two, so consider
958 // them far apart.
959 if (const MachineLoop *BBLoop = Loops->getLoopFor(BB)) {
960 if (!BBLoop->contains(ToBB) && !BBLoop->contains(FromBB)) {
961 LLVM_DEBUG(dbgs() << " found a loop before the condition in "
962 << printMBBReference(*BB) << '\n');
963 return true;
964 }
965 }
966
967 // Next check for calls in the block.
968 auto CacheIt = NoCallBlocksCache.find(BB);
969 if (CacheIt != NoCallBlocksCache.end()) {
970 if (HitSearchLimit(CacheIt->second))
971 return true;
972 } else {
973 unsigned NumBlockInstrs = 0;
974 for (const MachineInstr &MI :
976 /*SkipPseudoOp=*/false)) {
977 ++NumBlockInstrs;
978 if (IsCallOrHitSearchLimit(MI))
979 return true;
980 }
981 NoCallBlocksCache[BB] = NumBlockInstrs;
982 }
983
984 for (const MachineBasicBlock *Pred : BB->predecessors())
985 Enqueue(Pred);
986 }
987
988 return false;
989}
990
991/// Check if a register's value comes from a memory load by walking the
992/// def-use chain. We want to prioritize converting branches which
993/// depend on values loaded from memory (unless they are loop invariant,
994/// or come from a constant pool). The walk starts from the definition of
995/// ConditionDef's first operand, which is not ConditionDef itself for
996/// instructions such as FCMPSrr, where that operand is a use.
997bool EarlyIfConverter::doOperandsComeFromMemory(
998 const MachineInstr *ConditionDef) {
999 Register Reg = ConditionDef->getOperand(0).getReg();
1000 if (!Reg.isVirtual())
1001 return false;
1002
1003 LLVM_DEBUG(dbgs() << " doOperandsComeFromMemory starting from reg "
1004 << printReg(Reg) << "\n");
1005
1006 // The condition is consumed by the branch terminating Head, so this is the
1007 // end of the interval a load has to survive without a call in between.
1008 const MachineInstr *Br = &*IfConv.Head->getFirstTerminator();
1009 MachineLoop *IfConvLoop = Loops->getLoopFor(IfConv.Head);
1010
1011 // Walk the def-use chain.
1012 SmallPtrSet<const MachineInstr *, 8> VisitedInstrs;
1013 SmallVector<const MachineInstr *> Worklist;
1014
1015 MachineInstr *DefMI = MRI->getVRegDef(Reg);
1016 // The operand is defined outside of the function - it does not
1017 // come from memory access.
1018 if (!DefMI)
1019 return false;
1020
1021 Worklist.push_back(DefMI);
1022
1023 while (!Worklist.empty() &&
1024 VisitedInstrs.size() < Opts.early_ifcvt_max_steps) {
1025 const MachineInstr *MI = Worklist.pop_back_val();
1026 if (!VisitedInstrs.insert(MI).second)
1027 continue;
1028
1029 // Don't walk through PHIs: a value arriving on a back edge is loaded in a
1030 // previous iteration, so the interval between the load and the branch is
1031 // not the one hasCallOrLoopInRange measures.
1032 if (MI->isPHI())
1033 continue;
1034
1035 const MachineBasicBlock *Parent = MI->getParent();
1036 MachineLoop *ParentLoop = Loops->getLoopFor(Parent);
1037
1038 // If the instruction is outside the loop, skip it (loop-invariant).
1039 if (IfConvLoop && ParentLoop != IfConvLoop)
1040 continue;
1041
1042 // Check if this instruction is a load, and there are no calls or loops
1043 // between the load and the condition (which would break the "close in
1044 // time" assumption).
1045 if (MI->mayLoad() && !isConstantPoolLoad(MI) &&
1046 !MI->isDereferenceableInvariantLoad()) {
1047 // If the load doesn't dominate the branch (e.g., comes after it in
1048 // the same block via a loop back-edge), it can't affect this iteration.
1049 // If not - check if there is a call or a loop between the load
1050 // instruction and the branch.
1051 if (!DomTree->dominates(MI, Br) || hasCallOrLoopInRange(MI, Br))
1052 continue;
1053
1054 return true;
1055 }
1056
1057 // Walk through all register use operands and find their definitions.
1058 for (const MachineOperand &MO : MI->operands()) {
1059 if (!MO.isReg() || !MO.isUse())
1060 continue;
1061 Register UseReg = MO.getReg();
1062 if (!UseReg.isVirtual())
1063 continue;
1064
1065 if (MachineInstr *UseDef = MRI->getVRegDef(UseReg)) {
1066 if (!VisitedInstrs.count(UseDef)) {
1067 Worklist.push_back(UseDef);
1068 }
1069 }
1070 }
1071 }
1072
1073 return false;
1074}
1075
1076/// Check if the branch condition is data-dependent (comes from memory loads).
1077bool EarlyIfConverter::isConditionDataDependent() {
1078 TargetInstrInfo::MachineBranchPredicate MBP;
1079 if (TII->analyzeBranchPredicate(*IfConv.Head, MBP, /*AllowModify=*/false))
1080 return false;
1081
1082 if (!MBP.ConditionDef)
1083 return false;
1084
1085 // If the branch is biased (not 50/50), don't consider it data dependent.
1086 // This is to prevent converting unprofitable checks such as
1087 // `x[i] != 0;`
1088 auto TBBProb = MBPI.getEdgeProbability(IfConv.Head, IfConv.TBB);
1089 auto FBBProb = MBPI.getEdgeProbability(IfConv.Head, IfConv.FBB);
1090 if (TBBProb != FBBProb) {
1091 ++NumLikelyBiased;
1092 return false;
1093 }
1094
1095 // Check if operands used to compute the branch condition were loaded recently
1096 // from memory, starting by the ConditionDef itself and walking up the use-def
1097 // chain.
1098 if (doOperandsComeFromMemory(MBP.ConditionDef)) {
1099 ++NumDataDependant;
1100 return true;
1101 }
1102
1103 return false;
1104}
1105
1106// Adjust cycles with downward saturation.
1107static unsigned adjCycles(unsigned Cyc, int Delta) {
1108 if (Delta < 0 && Cyc + Delta > Cyc)
1109 return 0;
1110 return Cyc + Delta;
1111}
1112
1113namespace {
1114/// Helper class to simplify emission of cycle counts into optimization remarks.
1115struct Cycles {
1116 const char *Key;
1117 unsigned Value;
1118};
1119template <typename Remark> Remark &operator<<(Remark &R, Cycles C) {
1120 return R << ore::NV(C.Key, C.Value) << (C.Value == 1 ? " cycle" : " cycles");
1121}
1122} // anonymous namespace
1123
1124/// Apply cost model and heuristics to the if-conversion in IfConv.
1125/// Return true if the conversion is a good idea.
1126///
1127bool EarlyIfConverter::shouldConvertIf() {
1128 // Stress testing mode disables all cost considerations.
1129 if (Opts.stress_early_ifcvt)
1130 return true;
1131
1132 // Do not try to if-convert if the condition has a high chance of being
1133 // predictable.
1134 MachineLoop *CurrentLoop = Loops->getLoopFor(IfConv.Head);
1135 // If the condition is in a loop, consider it predictable if the condition
1136 // itself or all its operands are loop-invariant. E.g. this considers a load
1137 // from a loop-invariant address predictable; we were unable to prove that it
1138 // doesn't alias any of the memory-writes in the loop, but it is likely to
1139 // read to same value multiple times.
1140 if (CurrentLoop && any_of(IfConv.Cond, [&](MachineOperand &MO) {
1141 if (!MO.isReg() || !MO.isUse())
1142 return false;
1143 Register Reg = MO.getReg();
1144 if (Reg.isPhysical())
1145 return false;
1146
1147 MachineInstr *Def = MRI->getVRegDef(Reg);
1148 return CurrentLoop->isLoopInvariant(*Def) ||
1149 all_of(Def->operands(), [&](MachineOperand &Op) {
1150 if (Op.isImm())
1151 return true;
1152 if (!Op.isReg() || !Op.isUse())
1153 return true;
1154 Register Reg = Op.getReg();
1155 if (Reg.isPhysical())
1156 return false;
1157
1158 MachineInstr *Def = MRI->getVRegDef(Reg);
1159 return CurrentLoop->isLoopInvariant(*Def);
1160 });
1161 }))
1162 return false;
1163
1164 if (!MinInstr)
1165 MinInstr = Traces->getEnsemble(MachineTraceStrategy::TS_MinInstrCount);
1166
1167 MachineTraceMetrics::Trace TBBTrace = MinInstr->getTrace(IfConv.getTPred());
1168 MachineTraceMetrics::Trace FBBTrace = MinInstr->getTrace(IfConv.getFPred());
1169 LLVM_DEBUG(dbgs() << "TBB: " << TBBTrace << "FBB: " << FBBTrace);
1170 unsigned MinCrit = std::min(TBBTrace.getCriticalPath(),
1171 FBBTrace.getCriticalPath());
1172
1173 // Set a somewhat arbitrary limit on the critical path extension we accept.
1174 // Use the full misprediction penalty for data-dependent branches and half
1175 // for others. A branch with a cold edge below ColdEdgeThreshold gets no
1176 // extending the critical path: it is unlikely to mispredict often enough
1177 // to recover the extra latency from if-conversion.
1178 bool DataDependent = false;
1179 if (Opts.enable_early_ifcvt_data_dependent)
1180 DataDependent = isConditionDataDependent();
1181
1182 const unsigned MispredictPenalty = STI->getMispredictionPenalty();
1183 unsigned CritLimit =
1184 DataDependent ? MispredictPenalty : MispredictPenalty / 2;
1185 const BranchProbability ColdProb =
1186 std::min(MBPI.getEdgeProbability(IfConv.Head, IfConv.TBB),
1187 MBPI.getEdgeProbability(IfConv.Head, IfConv.FBB));
1188 const unsigned ColdEdgePercent =
1189 std::min(Opts.early_ifcvt_cold_edge_threshold, 50U);
1190 const bool StronglyBiased =
1191 ColdProb < BranchProbability(ColdEdgePercent, 100);
1192 if (StronglyBiased) {
1193 CritLimit = 0;
1194 ++NumStronglyBiased;
1195 }
1196
1197 MachineBasicBlock &MBB = *IfConv.Head;
1198 MachineOptimizationRemarkEmitter MORE(*MBB.getParent(), nullptr);
1199
1200 LLVM_DEBUG(dbgs() << "Cold edge probability: " << ColdProb
1201 << ", critical-path budget: " << CritLimit << " cycles\n");
1202 if (StronglyBiased) {
1203 MORE.emit([&]() {
1204 return MachineOptimizationRemarkAnalysis(DEBUG_TYPE,
1205 "StronglyBiasedBranch",
1206 MBB.back().getDebugLoc(), &MBB)
1207 << "branch has a cold edge below "
1208 << ore::NV("ColdEdgeThreshold", ColdEdgePercent)
1209 << "%, using a critical-path "
1210 "extension budget of "
1211 << ore::NV("CritLimit", CritLimit) << " cycles";
1212 });
1213 }
1214
1215 // Emit analysis remark about data-dependent condition.
1216 if (DataDependent) {
1217 MORE.emit([&]() {
1218 return MachineOptimizationRemarkAnalysis(DEBUG_TYPE,
1219 "DataDependentCondition",
1220 MBB.back().getDebugLoc(), &MBB)
1221 << "branch condition is data-dependent (from memory load), "
1222 << "using higher CritLimit of " << ore::NV("CritLimit", CritLimit)
1223 << " cycles";
1224 });
1225 }
1226
1227 // If-conversion only makes sense when there is unexploited ILP. Compute the
1228 // maximum-ILP resource length of the trace after if-conversion. Compare it
1229 // to the shortest critical path.
1231 if (IfConv.TBB != IfConv.Tail)
1232 ExtraBlocks.push_back(IfConv.TBB);
1233 unsigned ResLength = FBBTrace.getResourceLength(ExtraBlocks);
1234 LLVM_DEBUG(dbgs() << "Resource length " << ResLength
1235 << ", minimal critical path " << MinCrit << '\n');
1236 if (ResLength > MinCrit + CritLimit) {
1237 LLVM_DEBUG(dbgs() << "Not enough available ILP.\n");
1238 MORE.emit([&]() {
1239 MachineOptimizationRemarkMissed R(DEBUG_TYPE, "IfConversion",
1240 MBB.findDebugLoc(MBB.back()), &MBB);
1241 R << "did not if-convert branch: the resulting critical path ("
1242 << Cycles{"ResLength", ResLength}
1243 << ") would extend the shorter leg's critical path ("
1244 << Cycles{"MinCrit", MinCrit} << ") by more than the threshold of "
1245 << Cycles{"CritLimit", CritLimit}
1246 << ", which cannot be hidden by available ILP.";
1247 return R;
1248 });
1249 return false;
1250 }
1251
1252 // Assume that the depth of the first head terminator will also be the depth
1253 // of the select instruction inserted, as determined by the flag dependency.
1254 // TBB / FBB data dependencies may delay the select even more.
1255 MachineTraceMetrics::Trace HeadTrace = MinInstr->getTrace(IfConv.Head);
1256 unsigned BranchDepth =
1257 HeadTrace.getInstrCycles(*IfConv.Head->getFirstTerminator()).Depth;
1258 LLVM_DEBUG(dbgs() << "Branch depth: " << BranchDepth << '\n');
1259
1260 // Look at all the tail phis, and compute the critical path extension caused
1261 // by inserting select instructions.
1262 MachineTraceMetrics::Trace TailTrace = MinInstr->getTrace(IfConv.Tail);
1263 struct CriticalPathInfo {
1264 unsigned Extra; // Count of extra cycles that the component adds.
1265 unsigned Depth; // Absolute depth of the component in cycles.
1266 };
1267 CriticalPathInfo Cond{};
1268 CriticalPathInfo TBlock{};
1269 CriticalPathInfo FBlock{};
1270 bool ShouldConvert = true;
1271 for (SSAIfConv::PHIInfo &PI : IfConv.PHIs) {
1272 unsigned Slack = TailTrace.getInstrSlack(*PI.PHI);
1273 unsigned MaxDepth = Slack + TailTrace.getInstrCycles(*PI.PHI).Depth;
1274 LLVM_DEBUG(dbgs() << "Slack " << Slack << ":\t" << *PI.PHI);
1275
1276 // The condition is pulled into the critical path.
1277 unsigned CondDepth = adjCycles(BranchDepth, PI.Select.CondCycles);
1278 if (CondDepth > MaxDepth) {
1279 unsigned Extra = CondDepth - MaxDepth;
1280 LLVM_DEBUG(dbgs() << "Condition adds " << Extra << " cycles.\n");
1281 if (Extra > Cond.Extra)
1282 Cond = {Extra, CondDepth};
1283 if (Extra > CritLimit) {
1284 LLVM_DEBUG(dbgs() << "Exceeds limit of " << CritLimit << '\n');
1285 ShouldConvert = false;
1286 }
1287 }
1288
1289 // The TBB value is pulled into the critical path.
1290 unsigned TDepth =
1291 adjCycles(TBBTrace.getPHIDepth(*PI.PHI), PI.Select.TrueCycles);
1292 if (TDepth > MaxDepth) {
1293 unsigned Extra = TDepth - MaxDepth;
1294 LLVM_DEBUG(dbgs() << "TBB data adds " << Extra << " cycles.\n");
1295 if (Extra > TBlock.Extra)
1296 TBlock = {Extra, TDepth};
1297 if (Extra > CritLimit) {
1298 LLVM_DEBUG(dbgs() << "Exceeds limit of " << CritLimit << '\n');
1299 ShouldConvert = false;
1300 }
1301 }
1302
1303 // The FBB value is pulled into the critical path.
1304 unsigned FDepth =
1305 adjCycles(FBBTrace.getPHIDepth(*PI.PHI), PI.Select.FalseCycles);
1306 if (FDepth > MaxDepth) {
1307 unsigned Extra = FDepth - MaxDepth;
1308 LLVM_DEBUG(dbgs() << "FBB data adds " << Extra << " cycles.\n");
1309 if (Extra > FBlock.Extra)
1310 FBlock = {Extra, FDepth};
1311 if (Extra > CritLimit) {
1312 LLVM_DEBUG(dbgs() << "Exceeds limit of " << CritLimit << '\n');
1313 ShouldConvert = false;
1314 }
1315 }
1316 }
1317
1318 // Organize by "short" and "long" legs, since the diagnostics get confusing
1319 // when referring to the "true" and "false" sides of the branch, given that
1320 // those don't always correlate with what the user wrote in source-terms.
1321 const CriticalPathInfo Short = TBlock.Extra > FBlock.Extra ? FBlock : TBlock;
1322 const CriticalPathInfo Long = TBlock.Extra > FBlock.Extra ? TBlock : FBlock;
1323
1324 if (ShouldConvert) {
1325 MORE.emit([&]() {
1326 MachineOptimizationRemark R(DEBUG_TYPE, "IfConversion",
1327 MBB.back().getDebugLoc(), &MBB);
1328 R << "performing if-conversion on branch: the condition adds "
1329 << Cycles{"CondCycles", Cond.Extra} << " to the critical path";
1330 if (Short.Extra > 0)
1331 R << ", and the short leg adds another "
1332 << Cycles{"ShortCycles", Short.Extra};
1333 if (Long.Extra > 0)
1334 R << ", and the long leg adds another "
1335 << Cycles{"LongCycles", Long.Extra};
1336 R << ", each staying under the threshold of "
1337 << Cycles{"CritLimit", CritLimit} << ".";
1338 return R;
1339 });
1340 } else {
1341 MORE.emit([&]() {
1342 MachineOptimizationRemarkMissed R(DEBUG_TYPE, "IfConversion",
1343 MBB.back().getDebugLoc(), &MBB);
1344 R << "did not if-convert branch: the condition would add "
1345 << Cycles{"CondCycles", Cond.Extra} << " to the critical path";
1346 if (Cond.Extra > CritLimit)
1347 R << " exceeding the limit of " << Cycles{"CritLimit", CritLimit};
1348 if (Short.Extra > 0) {
1349 R << ", and the short leg would add another "
1350 << Cycles{"ShortCycles", Short.Extra};
1351 if (Short.Extra > CritLimit)
1352 R << " exceeding the limit of " << Cycles{"CritLimit", CritLimit};
1353 }
1354 if (Long.Extra > 0) {
1355 R << ", and the long leg would add another "
1356 << Cycles{"LongCycles", Long.Extra};
1357 if (Long.Extra > CritLimit)
1358 R << " exceeding the limit of " << Cycles{"CritLimit", CritLimit};
1359 }
1360 R << ".";
1361 return R;
1362 });
1363 }
1364
1365 return ShouldConvert;
1366}
1367
1368/// Attempt repeated if-conversion on MBB, return true if successful.
1369///
1370bool EarlyIfConverter::tryConvertIf(MachineBasicBlock *MBB) {
1371 bool Changed = false;
1372 while (IfConv.canConvertIf(MBB) && shouldConvertIf()) {
1373 // If-convert MBB and update analyses.
1374 invalidateTraces();
1375 SmallVector<MachineBasicBlock *, 4> RemoveBlocks;
1376 IfConv.convertIf(RemoveBlocks);
1377 Changed = true;
1378 updateDomTree(DomTree, IfConv, RemoveBlocks);
1379 updateLoops(Loops, RemoveBlocks);
1380 // Head absorbs the instructions of the removed blocks, including any calls,
1381 // so a Head cached as call-free may no longer be.
1382 NoCallBlocksCache.erase(IfConv.Head);
1383 for (MachineBasicBlock *MBB : RemoveBlocks) {
1384 NoCallBlocksCache.erase(MBB);
1386 }
1387 }
1388 return Changed;
1389}
1390
1391bool EarlyIfConverter::run(MachineFunction &MF) {
1392 LLVM_DEBUG(dbgs() << "********** EARLY IF-CONVERSION **********\n"
1393 << "********** Function: " << MF.getName() << '\n');
1394
1395 STI = &MF.getSubtarget();
1396 // Only run if conversion if the target wants it.
1397 if (!STI->enableEarlyIfConversion())
1398 return false;
1399
1400 TII = STI->getInstrInfo();
1401 MRI = &MF.getRegInfo();
1402 MinInstr = nullptr;
1403
1404 bool Changed = false;
1405 IfConv.init(MF);
1406
1407 // Visit blocks in dominator tree post-order. The post-order enables nested
1408 // if-conversion in a single pass. The tryConvertIf() function may erase
1409 // blocks, but only blocks dominated by the head block. This makes it safe to
1410 // update the dominator tree while the post-order iterator is still active.
1411 for (auto *DomNode : post_order(DomTree))
1412 if (tryConvertIf(DomNode->getBlock()))
1413 Changed = true;
1414
1415 return Changed;
1416}
1417
1418PreservedAnalyses
1426
1427 EarlyIfConverter Impl(getCodeGenOptions(MF), MDT, LI, MTM, MBPI);
1428 bool Changed = Impl.run(MF);
1429 if (!Changed)
1430 return PreservedAnalyses::all();
1431
1433 PA.preserve<MachineDominatorTreeAnalysis>();
1434 PA.preserve<MachineLoopAnalysis>();
1435 PA.preserve<MachineTraceMetricsAnalysis>();
1436 return PA;
1437}
1438
1439bool EarlyIfConverterLegacy::runOnMachineFunction(MachineFunction &MF) {
1440 if (skipFunction(MF.getFunction()))
1441 return false;
1442
1444 getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
1445 MachineLoopInfo &LI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
1446 MachineTraceMetrics &MTM =
1447 getAnalysis<MachineTraceMetricsWrapperPass>().getMTM();
1449 getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
1450
1451 return EarlyIfConverter(getCodeGenOptions(MF), MDT, LI, MTM, MBPI).run(MF);
1452}
1453
1454//===----------------------------------------------------------------------===//
1455// EarlyIfPredicator Pass
1456//===----------------------------------------------------------------------===//
1457
1458namespace {
1459class EarlyIfPredicator : public MachineFunctionPass {
1460 const TargetInstrInfo *TII = nullptr;
1461 TargetSchedModel SchedModel;
1462 MachineDominatorTree *DomTree = nullptr;
1463 MachineBranchProbabilityInfo *MBPI = nullptr;
1464 MachineLoopInfo *Loops = nullptr;
1465 SSAIfConv IfConv;
1466
1467public:
1468 static char ID;
1469 EarlyIfPredicator() : MachineFunctionPass(ID) {}
1470 void getAnalysisUsage(AnalysisUsage &AU) const override;
1471 bool runOnMachineFunction(MachineFunction &MF) override;
1472 StringRef getPassName() const override { return "Early If-predicator"; }
1473
1474protected:
1475 bool tryConvertIf(MachineBasicBlock *);
1476 bool shouldConvertIf();
1477};
1478} // end anonymous namespace
1479
1480#undef DEBUG_TYPE
1481#define DEBUG_TYPE "early-if-predicator"
1482
1483char EarlyIfPredicator::ID = 0;
1484char &llvm::EarlyIfPredicatorID = EarlyIfPredicator::ID;
1485
1486INITIALIZE_PASS_BEGIN(EarlyIfPredicator, DEBUG_TYPE, "Early If Predicator",
1487 false, false)
1490INITIALIZE_PASS_END(EarlyIfPredicator, DEBUG_TYPE, "Early If Predicator", false,
1491 false)
1492
1493void EarlyIfPredicator::getAnalysisUsage(AnalysisUsage &AU) const {
1495 AU.addRequired<MachineDominatorTreeWrapperPass>();
1496 AU.addPreserved<MachineDominatorTreeWrapperPass>();
1497 AU.addRequired<MachineLoopInfoWrapperPass>();
1498 AU.addPreserved<MachineLoopInfoWrapperPass>();
1500}
1501
1502/// Apply the target heuristic to decide if the transformation is profitable.
1503bool EarlyIfPredicator::shouldConvertIf() {
1504 auto TrueProbability = MBPI->getEdgeProbability(IfConv.Head, IfConv.TBB);
1505 if (IfConv.isTriangle()) {
1506 MachineBasicBlock &IfBlock =
1507 (IfConv.TBB == IfConv.Tail) ? *IfConv.FBB : *IfConv.TBB;
1508
1509 unsigned ExtraPredCost = 0;
1510 unsigned Cycles = 0;
1511 for (MachineInstr &I : IfBlock) {
1512 unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
1513 if (NumCycles > 1)
1514 Cycles += NumCycles - 1;
1515 ExtraPredCost += TII->getPredicationCost(I);
1516 }
1517
1518 return TII->isProfitableToIfCvt(IfBlock, Cycles, ExtraPredCost,
1519 TrueProbability);
1520 }
1521 unsigned TExtra = 0;
1522 unsigned FExtra = 0;
1523 unsigned TCycle = 0;
1524 unsigned FCycle = 0;
1525 for (MachineInstr &I : *IfConv.TBB) {
1526 unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
1527 if (NumCycles > 1)
1528 TCycle += NumCycles - 1;
1529 TExtra += TII->getPredicationCost(I);
1530 }
1531 for (MachineInstr &I : *IfConv.FBB) {
1532 unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
1533 if (NumCycles > 1)
1534 FCycle += NumCycles - 1;
1535 FExtra += TII->getPredicationCost(I);
1536 }
1537 return TII->isProfitableToIfCvt(*IfConv.TBB, TCycle, TExtra, *IfConv.FBB,
1538 FCycle, FExtra, TrueProbability);
1539}
1540
1541/// Attempt repeated if-conversion on MBB, return true if successful.
1542///
1543bool EarlyIfPredicator::tryConvertIf(MachineBasicBlock *MBB) {
1544 bool Changed = false;
1545 while (IfConv.canConvertIf(MBB, /*Predicate*/ true) && shouldConvertIf()) {
1546 // If-convert MBB and update analyses.
1547 SmallVector<MachineBasicBlock *, 4> RemoveBlocks;
1548 IfConv.convertIf(RemoveBlocks, /*Predicate*/ true);
1549 Changed = true;
1550 updateDomTree(DomTree, IfConv, RemoveBlocks);
1551 updateLoops(Loops, RemoveBlocks);
1552 for (MachineBasicBlock *MBB : RemoveBlocks)
1554 }
1555 return Changed;
1556}
1557
1558bool EarlyIfPredicator::runOnMachineFunction(MachineFunction &MF) {
1559 LLVM_DEBUG(dbgs() << "********** EARLY IF-PREDICATOR **********\n"
1560 << "********** Function: " << MF.getName() << '\n');
1561 if (skipFunction(MF.getFunction()))
1562 return false;
1563
1564 const TargetSubtargetInfo &STI = MF.getSubtarget();
1565 TII = STI.getInstrInfo();
1566 SchedModel.init(&STI);
1567 DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
1568 Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
1569 MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
1570
1571 bool Changed = false;
1572 IfConv.init(MF);
1573
1574 // Visit blocks in dominator tree post-order. The post-order enables nested
1575 // if-conversion in a single pass. The tryConvertIf() function may erase
1576 // blocks, but only blocks dominated by the head block. This makes it safe to
1577 // update the dominator tree while the post-order iterator is still active.
1578 for (auto *DomNode : post_order(DomTree))
1579 if (tryConvertIf(DomNode->getBlock()))
1580 Changed = true;
1581
1582 return Changed;
1583}
MachineInstrBuilder MachineInstrBuilder & DefMI
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
This file implements the BitVector class.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static unsigned InstrCount
This file defines the DenseMap class.
static bool hasSameValue(const MachineRegisterInfo &MRI, const TargetInstrInfo *TII, Register TReg, Register FReg)
static unsigned adjCycles(unsigned Cyc, int Delta)
static bool isConstantPoolLoad(const MachineInstr *MI)
#define DEBUG_TYPE
static Register UseReg(const MachineOperand &MO)
const HexagonInstrInfo * TII
Hexagon Hardware Loops
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
SmallVector< MachineOperand, 4 > Cond
MachineRegisterInfo & MRI
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
This file defines the SmallPtrSet class.
This file defines the SparseSet class derived from the version described in Briggs,...
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:119
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Definition BitVector.h:483
BitVector & reset()
Reset all bits in the bitvector.
Definition BitVector.h:410
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:367
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
Remove the branching code at the end of the specific MBB.
bool isPredicated(const MachineInstr &MI) const override
Returns true if the instruction is already predicated.
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e....
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
Reverses the branch condition of the specified condition list, returning false on success and true if...
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
Insert branch code into the end of the specified MachineBasicBlock.
bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCycles, unsigned ExtraPredCycles, BranchProbability Probability) const override
Return true if it's profitable to predicate instructions with accumulated instruction latency of "Num...
bool PredicateInstruction(MachineInstr &MI, ArrayRef< MachineOperand > Cond) const override
Convert the instruction into a predicated instruction.
bool isPredicable(const MachineInstr &MI) const override
Return true if the specified instruction can be predicated.
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
LLVM_ABI void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
LLVM_ABI DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
LLVM_ABI bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< pred_iterator > predecessors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI void moveAfter(MachineBasicBlock *NewBefore)
LLVM_ABI BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
Analysis pass which computes a MachineDominatorTree.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool dominates(const MachineInstr *A, const MachineInstr *B) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & back() const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
const MachineBasicBlock * getParent() const
LLVM_ABI bool isDereferenceableInvariantLoad() const
Return true if this load instruction never traps and points to a memory location whose value doesn't ...
LLVM_ABI bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore,...
mop_range uses()
Returns all operands which may be register uses.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI int findRegisterDefOperandIdx(Register Reg, const TargetRegisterInfo *TRI, bool isDead=false, bool Overlap=false) const
Returns the operand index that is a def of the specified register or -1 if it is not found.
Analysis pass that exposes the MachineLoopInfo for a machine function.
A description of a memory reference used in the backend.
MachineOperand class - Representation of each machine instruction operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLVM_ABI unsigned getResourceLength(ArrayRef< const MachineBasicBlock * > Extrablocks={}, ArrayRef< const MCSchedClassDesc * > ExtraInstrs={}, ArrayRef< const MCSchedClassDesc * > RemoveInstrs={}) const
Return the resource length of the trace.
InstrCycles getInstrCycles(const MachineInstr &MI) const
Return the depth and height of MI.
LLVM_ABI unsigned getInstrSlack(const MachineInstr &MI) const
Return the slack of MI.
unsigned getCriticalPath() const
Return the length of the (data dependency) critical path through the trace.
LLVM_ABI unsigned getPHIDepth(const MachineInstr &PHI) const
Return the Depth of a PHI instruction in a trace center block successor.
LLVM_ABI void verifyAnalysis() const
LLVM_ABI void invalidate(const MachineBasicBlock *MBB)
Invalidate cached information about MBB.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Wrapper class representing virtual and physical registers.
Definition Register.h:20
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:107
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
size_type size() const
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void push_back(const T &Elt)
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
iterator erase(iterator I)
erase - Erases an existing element identified by a valid iterator.
Definition SparseSet.h:288
void clear()
clear - Clears the set.
Definition SparseSet.h:191
std::pair< iterator, bool > insert(const ValueT &Val)
insert - Attempts to insert a new element.
Definition SparseSet.h:254
bool empty() const
empty - Returns true if the set is empty.
Definition SparseSet.h:180
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Provide an instruction scheduling machine model to CodeGen passes.
LLVM_ABI void init(const TargetSubtargetInfo *TSInfo, bool EnableSModel=true, bool EnableSItins=true)
Initialize the machine model for instruction scheduling.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual bool enableEarlyIfConversion() const
Enable the use of the early if conversion pass.
self_iterator getIterator()
Definition ilist_node.h:123
@ Tail
Attemps to make calls as fast as possible while guaranteeing that tail call optimization can always b...
Definition CallingConv.h:76
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
constexpr double phi
DiagnosticInfoOptimizationBase::Argument NV
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI Printable printRegUnit(MCRegUnit Unit, const TargetRegisterInfo *TRI)
Create Printable object to print register units on a raw_ostream.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
LLVM_ABI char & EarlyIfConverterLegacyID
EarlyIfConverter - This pass performs if-conversion on SSA form by inserting cmov instructions.
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
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:1762
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
auto post_order(const T &G)
Post-order traversal of a graph.
auto instructionsWithoutDebug(IterT It, IterT End, bool SkipPseudoOp=true)
Construct a range iterator which begins at It and moves forwards until End is reached,...
LLVM_ABI char & EarlyIfPredicatorID
EarlyIfPredicator - This pass performs if-conversion on SSA form by predicating if/else block and ins...
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
const CodeGenOptions & getCodeGenOptions(const LLVMContext &C)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
#define N
#define MORE()
Definition regcomp.c:247
unsigned Depth
Earliest issue cycle as determined by data dependencies and instruction latencies from the beginning ...