1 //===-- EarlyIfConversion.cpp - If-conversion on SSA form machine code ----===//
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
7 //===----------------------------------------------------------------------===//
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
13 // Instructions from both sides of the branch are executed specutatively, and a
14 // cmov instruction selects the result.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/BitVector.h"
19 #include "llvm/ADT/PostOrderIterator.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SparseSet.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
24 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
25 #include "llvm/CodeGen/MachineDominators.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineLoopInfo.h"
30 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/MachineTraceMetrics.h"
33 #include "llvm/CodeGen/TargetInstrInfo.h"
34 #include "llvm/CodeGen/TargetRegisterInfo.h"
35 #include "llvm/CodeGen/TargetSubtargetInfo.h"
36 #include "llvm/InitializePasses.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/raw_ostream.h"
43 #define DEBUG_TYPE "early-ifcvt"
45 // Absolute maximum number of instructions allowed per speculated block.
46 // This bypasses all other heuristics, so it should be set fairly high.
47 static cl::opt
<unsigned>
48 BlockInstrLimit("early-ifcvt-limit", cl::init(30), cl::Hidden
,
49 cl::desc("Maximum number of instructions per speculated block."));
51 // Stress testing mode - disable heuristics.
52 static cl::opt
<bool> Stress("stress-early-ifcvt", cl::Hidden
,
53 cl::desc("Turn all knobs to 11"));
55 STATISTIC(NumDiamondsSeen
, "Number of diamonds");
56 STATISTIC(NumDiamondsConv
, "Number of diamonds converted");
57 STATISTIC(NumTrianglesSeen
, "Number of triangles");
58 STATISTIC(NumTrianglesConv
, "Number of triangles converted");
60 //===----------------------------------------------------------------------===//
62 //===----------------------------------------------------------------------===//
64 // The SSAIfConv class performs if-conversion on SSA form machine code after
65 // determining if it is possible. The class contains no heuristics; external
66 // code should be used to determine when if-conversion is a good idea.
68 // SSAIfConv can convert both triangles and diamonds:
70 // Triangle: Head Diamond: Head
78 // Instructions in the conditional blocks TBB and/or FBB are spliced into the
79 // Head block, and phis in the Tail block are converted to select instructions.
83 const TargetInstrInfo
*TII
;
84 const TargetRegisterInfo
*TRI
;
85 MachineRegisterInfo
*MRI
;
88 /// The block containing the conditional branch.
89 MachineBasicBlock
*Head
;
91 /// The block containing phis after the if-then-else.
92 MachineBasicBlock
*Tail
;
94 /// The 'true' conditional block as determined by analyzeBranch.
95 MachineBasicBlock
*TBB
;
97 /// The 'false' conditional block as determined by analyzeBranch.
98 MachineBasicBlock
*FBB
;
100 /// isTriangle - When there is no 'else' block, either TBB or FBB will be
102 bool isTriangle() const { return TBB
== Tail
|| FBB
== Tail
; }
104 /// Returns the Tail predecessor for the True side.
105 MachineBasicBlock
*getTPred() const { return TBB
== Tail
? Head
: TBB
; }
107 /// Returns the Tail predecessor for the False side.
108 MachineBasicBlock
*getFPred() const { return FBB
== Tail
? Head
: FBB
; }
110 /// Information about each phi in the Tail block.
113 unsigned TReg
= 0, FReg
= 0;
114 // Latencies from Cond+Branch, TReg, and FReg to DstReg.
115 int CondCycles
= 0, TCycles
= 0, FCycles
= 0;
117 PHIInfo(MachineInstr
*phi
) : PHI(phi
) {}
120 SmallVector
<PHIInfo
, 8> PHIs
;
122 /// The branch condition determined by analyzeBranch.
123 SmallVector
<MachineOperand
, 4> Cond
;
126 /// Instructions in Head that define values used by the conditional blocks.
127 /// The hoisted instructions must be inserted after these instructions.
128 SmallPtrSet
<MachineInstr
*, 8> InsertAfter
;
130 /// Register units clobbered by the conditional blocks.
131 BitVector ClobberedRegUnits
;
133 // Scratch pad for findInsertionPoint.
134 SparseSet
<unsigned> LiveRegUnits
;
136 /// Insertion point in Head for speculatively executed instructions form TBB
138 MachineBasicBlock::iterator InsertionPoint
;
140 /// Return true if all non-terminator instructions in MBB can be safely
142 bool canSpeculateInstrs(MachineBasicBlock
*MBB
);
144 /// Return true if all non-terminator instructions in MBB can be safely
146 bool canPredicateInstrs(MachineBasicBlock
*MBB
);
148 /// Scan through instruction dependencies and update InsertAfter array.
149 /// Return false if any dependency is incompatible with if conversion.
150 bool InstrDependenciesAllowIfConv(MachineInstr
*I
);
152 /// Predicate all instructions of the basic block with current condition
153 /// except for terminators. Reverse the condition if ReversePredicate is set.
154 void PredicateBlock(MachineBasicBlock
*MBB
, bool ReversePredicate
);
156 /// Find a valid insertion point in Head.
157 bool findInsertionPoint();
159 /// Replace PHI instructions in Tail with selects.
160 void replacePHIInstrs();
162 /// Insert selects and rewrite PHI operands to use them.
163 void rewritePHIOperands();
166 /// runOnMachineFunction - Initialize per-function data structures.
167 void runOnMachineFunction(MachineFunction
&MF
) {
168 TII
= MF
.getSubtarget().getInstrInfo();
169 TRI
= MF
.getSubtarget().getRegisterInfo();
170 MRI
= &MF
.getRegInfo();
171 LiveRegUnits
.clear();
172 LiveRegUnits
.setUniverse(TRI
->getNumRegUnits());
173 ClobberedRegUnits
.clear();
174 ClobberedRegUnits
.resize(TRI
->getNumRegUnits());
177 /// canConvertIf - If the sub-CFG headed by MBB can be if-converted,
178 /// initialize the internal state, and return true.
179 /// If predicate is set try to predicate the block otherwise try to
180 /// speculatively execute it.
181 bool canConvertIf(MachineBasicBlock
*MBB
, bool Predicate
= false);
183 /// convertIf - If-convert the last block passed to canConvertIf(), assuming
184 /// it is possible. Add any erased blocks to RemovedBlocks.
185 void convertIf(SmallVectorImpl
<MachineBasicBlock
*> &RemovedBlocks
,
186 bool Predicate
= false);
188 } // end anonymous namespace
191 /// canSpeculateInstrs - Returns true if all the instructions in MBB can safely
192 /// be speculated. The terminators are not considered.
194 /// If instructions use any values that are defined in the head basic block,
195 /// the defining instructions are added to InsertAfter.
197 /// Any clobbered regunits are added to ClobberedRegUnits.
199 bool SSAIfConv::canSpeculateInstrs(MachineBasicBlock
*MBB
) {
200 // Reject any live-in physregs. It's probably CPSR/EFLAGS, and very hard to
202 if (!MBB
->livein_empty()) {
203 LLVM_DEBUG(dbgs() << printMBBReference(*MBB
) << " has live-ins.\n");
207 unsigned InstrCount
= 0;
209 // Check all instructions, except the terminators. It is assumed that
210 // terminators never have side effects or define any used register values.
211 for (MachineInstr
&MI
:
212 llvm::make_range(MBB
->begin(), MBB
->getFirstTerminator())) {
213 if (MI
.isDebugInstr())
216 if (++InstrCount
> BlockInstrLimit
&& !Stress
) {
217 LLVM_DEBUG(dbgs() << printMBBReference(*MBB
) << " has more than "
218 << BlockInstrLimit
<< " instructions.\n");
222 // There shouldn't normally be any phis in a single-predecessor block.
224 LLVM_DEBUG(dbgs() << "Can't hoist: " << MI
);
228 // Don't speculate loads. Note that it may be possible and desirable to
229 // speculate GOT or constant pool loads that are guaranteed not to trap,
230 // but we don't support that for now.
232 LLVM_DEBUG(dbgs() << "Won't speculate load: " << MI
);
236 // We never speculate stores, so an AA pointer isn't necessary.
237 bool DontMoveAcrossStore
= true;
238 if (!MI
.isSafeToMove(nullptr, DontMoveAcrossStore
)) {
239 LLVM_DEBUG(dbgs() << "Can't speculate: " << MI
);
243 // Check for any dependencies on Head instructions.
244 if (!InstrDependenciesAllowIfConv(&MI
))
250 /// Check that there is no dependencies preventing if conversion.
252 /// If instruction uses any values that are defined in the head basic block,
253 /// the defining instructions are added to InsertAfter.
254 bool SSAIfConv::InstrDependenciesAllowIfConv(MachineInstr
*I
) {
255 for (const MachineOperand
&MO
: I
->operands()) {
256 if (MO
.isRegMask()) {
257 LLVM_DEBUG(dbgs() << "Won't speculate regmask: " << *I
);
262 Register Reg
= MO
.getReg();
264 // Remember clobbered regunits.
265 if (MO
.isDef() && Reg
.isPhysical())
266 for (MCRegUnit Unit
: TRI
->regunits(Reg
.asMCReg()))
267 ClobberedRegUnits
.set(Unit
);
269 if (!MO
.readsReg() || !Reg
.isVirtual())
271 MachineInstr
*DefMI
= MRI
->getVRegDef(Reg
);
272 if (!DefMI
|| DefMI
->getParent() != Head
)
274 if (InsertAfter
.insert(DefMI
).second
)
275 LLVM_DEBUG(dbgs() << printMBBReference(*I
->getParent()) << " depends on "
277 if (DefMI
->isTerminator()) {
278 LLVM_DEBUG(dbgs() << "Can't insert instructions below terminator.\n");
285 /// canPredicateInstrs - Returns true if all the instructions in MBB can safely
286 /// be predicates. The terminators are not considered.
288 /// If instructions use any values that are defined in the head basic block,
289 /// the defining instructions are added to InsertAfter.
291 /// Any clobbered regunits are added to ClobberedRegUnits.
293 bool SSAIfConv::canPredicateInstrs(MachineBasicBlock
*MBB
) {
294 // Reject any live-in physregs. It's probably CPSR/EFLAGS, and very hard to
296 if (!MBB
->livein_empty()) {
297 LLVM_DEBUG(dbgs() << printMBBReference(*MBB
) << " has live-ins.\n");
301 unsigned InstrCount
= 0;
303 // Check all instructions, except the terminators. It is assumed that
304 // terminators never have side effects or define any used register values.
305 for (MachineBasicBlock::iterator I
= MBB
->begin(),
306 E
= MBB
->getFirstTerminator();
308 if (I
->isDebugInstr())
311 if (++InstrCount
> BlockInstrLimit
&& !Stress
) {
312 LLVM_DEBUG(dbgs() << printMBBReference(*MBB
) << " has more than "
313 << BlockInstrLimit
<< " instructions.\n");
317 // There shouldn't normally be any phis in a single-predecessor block.
319 LLVM_DEBUG(dbgs() << "Can't predicate: " << *I
);
323 // Check that instruction is predicable
324 if (!TII
->isPredicable(*I
)) {
325 LLVM_DEBUG(dbgs() << "Isn't predicable: " << *I
);
329 // Check that instruction is not already predicated.
330 if (TII
->isPredicated(*I
) && !TII
->canPredicatePredicatedInstr(*I
)) {
331 LLVM_DEBUG(dbgs() << "Is already predicated: " << *I
);
335 // Check for any dependencies on Head instructions.
336 if (!InstrDependenciesAllowIfConv(&(*I
)))
342 // Apply predicate to all instructions in the machine block.
343 void SSAIfConv::PredicateBlock(MachineBasicBlock
*MBB
, bool ReversePredicate
) {
344 auto Condition
= Cond
;
345 if (ReversePredicate
) {
346 bool CanRevCond
= !TII
->reverseBranchCondition(Condition
);
347 assert(CanRevCond
&& "Reversed predicate is not supported");
350 // Terminators don't need to be predicated as they will be removed.
351 for (MachineBasicBlock::iterator I
= MBB
->begin(),
352 E
= MBB
->getFirstTerminator();
354 if (I
->isDebugInstr())
356 TII
->PredicateInstruction(*I
, Condition
);
360 /// Find an insertion point in Head for the speculated instructions. The
361 /// insertion point must be:
363 /// 1. Before any terminators.
364 /// 2. After any instructions in InsertAfter.
365 /// 3. Not have any clobbered regunits live.
367 /// This function sets InsertionPoint and returns true when successful, it
368 /// returns false if no valid insertion point could be found.
370 bool SSAIfConv::findInsertionPoint() {
371 // Keep track of live regunits before the current position.
372 // Only track RegUnits that are also in ClobberedRegUnits.
373 LiveRegUnits
.clear();
374 SmallVector
<MCRegister
, 8> Reads
;
375 MachineBasicBlock::iterator FirstTerm
= Head
->getFirstTerminator();
376 MachineBasicBlock::iterator I
= Head
->end();
377 MachineBasicBlock::iterator B
= Head
->begin();
380 // Some of the conditional code depends in I.
381 if (InsertAfter
.count(&*I
)) {
382 LLVM_DEBUG(dbgs() << "Can't insert code after " << *I
);
386 // Update live regunits.
387 for (const MachineOperand
&MO
: I
->operands()) {
388 // We're ignoring regmask operands. That is conservatively correct.
391 Register Reg
= MO
.getReg();
392 if (!Reg
.isPhysical())
394 // I clobbers Reg, so it isn't live before I.
396 for (MCRegUnit Unit
: TRI
->regunits(Reg
.asMCReg()))
397 LiveRegUnits
.erase(Unit
);
398 // Unless I reads Reg.
400 Reads
.push_back(Reg
.asMCReg());
402 // Anything read by I is live before I.
403 while (!Reads
.empty())
404 for (MCRegUnit Unit
: TRI
->regunits(Reads
.pop_back_val()))
405 if (ClobberedRegUnits
.test(Unit
))
406 LiveRegUnits
.insert(Unit
);
408 // We can't insert before a terminator.
409 if (I
!= FirstTerm
&& I
->isTerminator())
412 // Some of the clobbered registers are live before I, not a valid insertion
414 if (!LiveRegUnits
.empty()) {
416 dbgs() << "Would clobber";
417 for (unsigned LRU
: LiveRegUnits
)
418 dbgs() << ' ' << printRegUnit(LRU
, TRI
);
419 dbgs() << " live before " << *I
;
424 // This is a valid insertion point.
426 LLVM_DEBUG(dbgs() << "Can insert before " << *I
);
429 LLVM_DEBUG(dbgs() << "No legal insertion point found.\n");
435 /// canConvertIf - analyze the sub-cfg rooted in MBB, and return true if it is
436 /// a potential candidate for if-conversion. Fill out the internal state.
438 bool SSAIfConv::canConvertIf(MachineBasicBlock
*MBB
, bool Predicate
) {
440 TBB
= FBB
= Tail
= nullptr;
442 if (Head
->succ_size() != 2)
444 MachineBasicBlock
*Succ0
= Head
->succ_begin()[0];
445 MachineBasicBlock
*Succ1
= Head
->succ_begin()[1];
447 // Canonicalize so Succ0 has MBB as its single predecessor.
448 if (Succ0
->pred_size() != 1)
449 std::swap(Succ0
, Succ1
);
451 if (Succ0
->pred_size() != 1 || Succ0
->succ_size() != 1)
454 Tail
= Succ0
->succ_begin()[0];
456 // This is not a triangle.
458 // Check for a diamond. We won't deal with any critical edges.
459 if (Succ1
->pred_size() != 1 || Succ1
->succ_size() != 1 ||
460 Succ1
->succ_begin()[0] != Tail
)
462 LLVM_DEBUG(dbgs() << "\nDiamond: " << printMBBReference(*Head
) << " -> "
463 << printMBBReference(*Succ0
) << "/"
464 << printMBBReference(*Succ1
) << " -> "
465 << printMBBReference(*Tail
) << '\n');
467 // Live-in physregs are tricky to get right when speculating code.
468 if (!Tail
->livein_empty()) {
469 LLVM_DEBUG(dbgs() << "Tail has live-ins.\n");
473 LLVM_DEBUG(dbgs() << "\nTriangle: " << printMBBReference(*Head
) << " -> "
474 << printMBBReference(*Succ0
) << " -> "
475 << printMBBReference(*Tail
) << '\n');
478 // This is a triangle or a diamond.
479 // Skip if we cannot predicate and there are no phis skip as there must be
480 // side effects that can only be handled with predication.
481 if (!Predicate
&& (Tail
->empty() || !Tail
->front().isPHI())) {
482 LLVM_DEBUG(dbgs() << "No phis in tail.\n");
486 // The branch we're looking to eliminate must be analyzable.
488 if (TII
->analyzeBranch(*Head
, TBB
, FBB
, Cond
)) {
489 LLVM_DEBUG(dbgs() << "Branch not analyzable.\n");
493 // This is weird, probably some sort of degenerate CFG.
495 LLVM_DEBUG(dbgs() << "analyzeBranch didn't find conditional branch.\n");
499 // Make sure the analyzed branch is conditional; one of the successors
500 // could be a landing pad. (Empty landing pads can be generated on Windows.)
502 LLVM_DEBUG(dbgs() << "analyzeBranch found an unconditional branch.\n");
506 // analyzeBranch doesn't set FBB on a fall-through branch.
507 // Make sure it is always set.
508 FBB
= TBB
== Succ0
? Succ1
: Succ0
;
510 // Any phis in the tail block must be convertible to selects.
512 MachineBasicBlock
*TPred
= getTPred();
513 MachineBasicBlock
*FPred
= getFPred();
514 for (MachineBasicBlock::iterator I
= Tail
->begin(), E
= Tail
->end();
515 I
!= E
&& I
->isPHI(); ++I
) {
517 PHIInfo
&PI
= PHIs
.back();
518 // Find PHI operands corresponding to TPred and FPred.
519 for (unsigned i
= 1; i
!= PI
.PHI
->getNumOperands(); i
+= 2) {
520 if (PI
.PHI
->getOperand(i
+1).getMBB() == TPred
)
521 PI
.TReg
= PI
.PHI
->getOperand(i
).getReg();
522 if (PI
.PHI
->getOperand(i
+1).getMBB() == FPred
)
523 PI
.FReg
= PI
.PHI
->getOperand(i
).getReg();
525 assert(Register::isVirtualRegister(PI
.TReg
) && "Bad PHI");
526 assert(Register::isVirtualRegister(PI
.FReg
) && "Bad PHI");
528 // Get target information.
529 if (!TII
->canInsertSelect(*Head
, Cond
, PI
.PHI
->getOperand(0).getReg(),
530 PI
.TReg
, PI
.FReg
, PI
.CondCycles
, PI
.TCycles
,
532 LLVM_DEBUG(dbgs() << "Can't convert: " << *PI
.PHI
);
537 // Check that the conditional instructions can be speculated.
539 ClobberedRegUnits
.reset();
541 if (TBB
!= Tail
&& !canPredicateInstrs(TBB
))
543 if (FBB
!= Tail
&& !canPredicateInstrs(FBB
))
546 if (TBB
!= Tail
&& !canSpeculateInstrs(TBB
))
548 if (FBB
!= Tail
&& !canSpeculateInstrs(FBB
))
552 // Try to find a valid insertion point for the speculated instructions in the
554 if (!findInsertionPoint())
564 /// \return true iff the two registers are known to have the same value.
565 static bool hasSameValue(const MachineRegisterInfo
&MRI
,
566 const TargetInstrInfo
*TII
, Register TReg
,
571 if (!TReg
.isVirtual() || !FReg
.isVirtual())
574 const MachineInstr
*TDef
= MRI
.getUniqueVRegDef(TReg
);
575 const MachineInstr
*FDef
= MRI
.getUniqueVRegDef(FReg
);
579 // If there are side-effects, all bets are off.
580 if (TDef
->hasUnmodeledSideEffects())
583 // If the instruction could modify memory, or there may be some intervening
584 // store between the two, we can't consider them to be equal.
585 if (TDef
->mayLoadOrStore() && !TDef
->isDereferenceableInvariantLoad())
588 // We also can't guarantee that they are the same if, for example, the
589 // instructions are both a copy from a physical reg, because some other
590 // instruction may have modified the value in that reg between the two
592 if (any_of(TDef
->uses(), [](const MachineOperand
&MO
) {
593 return MO
.isReg() && MO
.getReg().isPhysical();
597 // Check whether the two defining instructions produce the same value(s).
598 if (!TII
->produceSameValue(*TDef
, *FDef
, &MRI
))
601 // Further, check that the two defs come from corresponding operands.
602 int TIdx
= TDef
->findRegisterDefOperandIdx(TReg
, /*TRI=*/nullptr);
603 int FIdx
= FDef
->findRegisterDefOperandIdx(FReg
, /*TRI=*/nullptr);
604 if (TIdx
== -1 || FIdx
== -1)
610 /// replacePHIInstrs - Completely replace PHI instructions with selects.
611 /// This is possible when the only Tail predecessors are the if-converted
613 void SSAIfConv::replacePHIInstrs() {
614 assert(Tail
->pred_size() == 2 && "Cannot replace PHIs");
615 MachineBasicBlock::iterator FirstTerm
= Head
->getFirstTerminator();
616 assert(FirstTerm
!= Head
->end() && "No terminators");
617 DebugLoc HeadDL
= FirstTerm
->getDebugLoc();
619 // Convert all PHIs to select instructions inserted before FirstTerm.
620 for (PHIInfo
&PI
: PHIs
) {
621 LLVM_DEBUG(dbgs() << "If-converting " << *PI
.PHI
);
622 Register DstReg
= PI
.PHI
->getOperand(0).getReg();
623 if (hasSameValue(*MRI
, TII
, PI
.TReg
, PI
.FReg
)) {
624 // We do not need the select instruction if both incoming values are
625 // equal, but we do need a COPY.
626 BuildMI(*Head
, FirstTerm
, HeadDL
, TII
->get(TargetOpcode::COPY
), DstReg
)
629 TII
->insertSelect(*Head
, FirstTerm
, HeadDL
, DstReg
, Cond
, PI
.TReg
,
632 LLVM_DEBUG(dbgs() << " --> " << *std::prev(FirstTerm
));
633 PI
.PHI
->eraseFromParent();
638 /// rewritePHIOperands - When there are additional Tail predecessors, insert
639 /// select instructions in Head and rewrite PHI operands to use the selects.
640 /// Keep the PHI instructions in Tail to handle the other predecessors.
641 void SSAIfConv::rewritePHIOperands() {
642 MachineBasicBlock::iterator FirstTerm
= Head
->getFirstTerminator();
643 assert(FirstTerm
!= Head
->end() && "No terminators");
644 DebugLoc HeadDL
= FirstTerm
->getDebugLoc();
646 // Convert all PHIs to select instructions inserted before FirstTerm.
647 for (PHIInfo
&PI
: PHIs
) {
650 LLVM_DEBUG(dbgs() << "If-converting " << *PI
.PHI
);
651 if (hasSameValue(*MRI
, TII
, PI
.TReg
, PI
.FReg
)) {
652 // We do not need the select instruction if both incoming values are
656 Register PHIDst
= PI
.PHI
->getOperand(0).getReg();
657 DstReg
= MRI
->createVirtualRegister(MRI
->getRegClass(PHIDst
));
658 TII
->insertSelect(*Head
, FirstTerm
, HeadDL
,
659 DstReg
, Cond
, PI
.TReg
, PI
.FReg
);
660 LLVM_DEBUG(dbgs() << " --> " << *std::prev(FirstTerm
));
663 // Rewrite PHI operands TPred -> (DstReg, Head), remove FPred.
664 for (unsigned i
= PI
.PHI
->getNumOperands(); i
!= 1; i
-= 2) {
665 MachineBasicBlock
*MBB
= PI
.PHI
->getOperand(i
-1).getMBB();
666 if (MBB
== getTPred()) {
667 PI
.PHI
->getOperand(i
-1).setMBB(Head
);
668 PI
.PHI
->getOperand(i
-2).setReg(DstReg
);
669 } else if (MBB
== getFPred()) {
670 PI
.PHI
->removeOperand(i
-1);
671 PI
.PHI
->removeOperand(i
-2);
674 LLVM_DEBUG(dbgs() << " --> " << *PI
.PHI
);
678 /// convertIf - Execute the if conversion after canConvertIf has determined the
681 /// Any basic blocks erased will be added to RemovedBlocks.
683 void SSAIfConv::convertIf(SmallVectorImpl
<MachineBasicBlock
*> &RemovedBlocks
,
685 assert(Head
&& Tail
&& TBB
&& FBB
&& "Call canConvertIf first.");
687 // Update statistics.
693 // Move all instructions into Head, except for the terminators.
696 PredicateBlock(TBB
, /*ReversePredicate=*/false);
697 Head
->splice(InsertionPoint
, TBB
, TBB
->begin(), TBB
->getFirstTerminator());
701 PredicateBlock(FBB
, /*ReversePredicate=*/true);
702 Head
->splice(InsertionPoint
, FBB
, FBB
->begin(), FBB
->getFirstTerminator());
704 // Are there extra Tail predecessors?
705 bool ExtraPreds
= Tail
->pred_size() != 2;
707 rewritePHIOperands();
711 // Fix up the CFG, temporarily leave Head without any successors.
712 Head
->removeSuccessor(TBB
);
713 Head
->removeSuccessor(FBB
, true);
715 TBB
->removeSuccessor(Tail
, true);
717 FBB
->removeSuccessor(Tail
, true);
719 // Fix up Head's terminators.
720 // It should become a single branch or a fallthrough.
721 DebugLoc HeadDL
= Head
->getFirstTerminator()->getDebugLoc();
722 TII
->removeBranch(*Head
);
724 // Erase the now empty conditional blocks. It is likely that Head can fall
725 // through to Tail, and we can join the two blocks.
727 RemovedBlocks
.push_back(TBB
);
728 TBB
->eraseFromParent();
731 RemovedBlocks
.push_back(FBB
);
732 FBB
->eraseFromParent();
735 assert(Head
->succ_empty() && "Additional head successors?");
736 if (!ExtraPreds
&& Head
->isLayoutSuccessor(Tail
)) {
737 // Splice Tail onto the end of Head.
738 LLVM_DEBUG(dbgs() << "Joining tail " << printMBBReference(*Tail
)
739 << " into head " << printMBBReference(*Head
) << '\n');
740 Head
->splice(Head
->end(), Tail
,
741 Tail
->begin(), Tail
->end());
742 Head
->transferSuccessorsAndUpdatePHIs(Tail
);
743 RemovedBlocks
.push_back(Tail
);
744 Tail
->eraseFromParent();
746 // We need a branch to Tail, let code placement work it out later.
747 LLVM_DEBUG(dbgs() << "Converting to unconditional branch.\n");
748 SmallVector
<MachineOperand
, 0> EmptyCond
;
749 TII
->insertBranch(*Head
, Tail
, nullptr, EmptyCond
, HeadDL
);
750 Head
->addSuccessor(Tail
);
752 LLVM_DEBUG(dbgs() << *Head
);
755 //===----------------------------------------------------------------------===//
756 // EarlyIfConverter Pass
757 //===----------------------------------------------------------------------===//
760 class EarlyIfConverter
: public MachineFunctionPass
{
761 const TargetInstrInfo
*TII
= nullptr;
762 const TargetRegisterInfo
*TRI
= nullptr;
763 MCSchedModel SchedModel
;
764 MachineRegisterInfo
*MRI
= nullptr;
765 MachineDominatorTree
*DomTree
= nullptr;
766 MachineLoopInfo
*Loops
= nullptr;
767 MachineTraceMetrics
*Traces
= nullptr;
768 MachineTraceMetrics::Ensemble
*MinInstr
= nullptr;
773 EarlyIfConverter() : MachineFunctionPass(ID
) {}
774 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
775 bool runOnMachineFunction(MachineFunction
&MF
) override
;
776 StringRef
getPassName() const override
{ return "Early If-Conversion"; }
779 bool tryConvertIf(MachineBasicBlock
*);
780 void invalidateTraces();
781 bool shouldConvertIf();
783 } // end anonymous namespace
785 char EarlyIfConverter::ID
= 0;
786 char &llvm::EarlyIfConverterID
= EarlyIfConverter::ID
;
788 INITIALIZE_PASS_BEGIN(EarlyIfConverter
, DEBUG_TYPE
,
789 "Early If Converter", false, false)
790 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass
)
791 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass
)
792 INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics
)
793 INITIALIZE_PASS_END(EarlyIfConverter
, DEBUG_TYPE
,
794 "Early If Converter", false, false)
796 void EarlyIfConverter::getAnalysisUsage(AnalysisUsage
&AU
) const {
797 AU
.addRequired
<MachineBranchProbabilityInfoWrapperPass
>();
798 AU
.addRequired
<MachineDominatorTreeWrapperPass
>();
799 AU
.addPreserved
<MachineDominatorTreeWrapperPass
>();
800 AU
.addRequired
<MachineLoopInfoWrapperPass
>();
801 AU
.addPreserved
<MachineLoopInfoWrapperPass
>();
802 AU
.addRequired
<MachineTraceMetrics
>();
803 AU
.addPreserved
<MachineTraceMetrics
>();
804 MachineFunctionPass::getAnalysisUsage(AU
);
808 /// Update the dominator tree after if-conversion erased some blocks.
809 void updateDomTree(MachineDominatorTree
*DomTree
, const SSAIfConv
&IfConv
,
810 ArrayRef
<MachineBasicBlock
*> Removed
) {
811 // convertIf can remove TBB, FBB, and Tail can be merged into Head.
812 // TBB and FBB should not dominate any blocks.
813 // Tail children should be transferred to Head.
814 MachineDomTreeNode
*HeadNode
= DomTree
->getNode(IfConv
.Head
);
815 for (auto *B
: Removed
) {
816 MachineDomTreeNode
*Node
= DomTree
->getNode(B
);
817 assert(Node
!= HeadNode
&& "Cannot erase the head node");
818 while (Node
->getNumChildren()) {
819 assert(Node
->getBlock() == IfConv
.Tail
&& "Unexpected children");
820 DomTree
->changeImmediateDominator(Node
->back(), HeadNode
);
822 DomTree
->eraseNode(B
);
826 /// Update LoopInfo after if-conversion.
827 void updateLoops(MachineLoopInfo
*Loops
,
828 ArrayRef
<MachineBasicBlock
*> Removed
) {
829 // If-conversion doesn't change loop structure, and it doesn't mess with back
830 // edges, so updating LoopInfo is simply removing the dead blocks.
831 for (auto *B
: Removed
)
832 Loops
->removeBlock(B
);
836 /// Invalidate MachineTraceMetrics before if-conversion.
837 void EarlyIfConverter::invalidateTraces() {
838 Traces
->verifyAnalysis();
839 Traces
->invalidate(IfConv
.Head
);
840 Traces
->invalidate(IfConv
.Tail
);
841 Traces
->invalidate(IfConv
.TBB
);
842 Traces
->invalidate(IfConv
.FBB
);
843 Traces
->verifyAnalysis();
846 // Adjust cycles with downward saturation.
847 static unsigned adjCycles(unsigned Cyc
, int Delta
) {
848 if (Delta
< 0 && Cyc
+ Delta
> Cyc
)
854 /// Helper class to simplify emission of cycle counts into optimization remarks.
859 template <typename Remark
> Remark
&operator<<(Remark
&R
, Cycles C
) {
860 return R
<< ore::NV(C
.Key
, C
.Value
) << (C
.Value
== 1 ? " cycle" : " cycles");
862 } // anonymous namespace
864 /// Apply cost model and heuristics to the if-conversion in IfConv.
865 /// Return true if the conversion is a good idea.
867 bool EarlyIfConverter::shouldConvertIf() {
868 // Stress testing mode disables all cost considerations.
872 // Do not try to if-convert if the condition has a high chance of being
874 MachineLoop
*CurrentLoop
= Loops
->getLoopFor(IfConv
.Head
);
875 // If the condition is in a loop, consider it predictable if the condition
876 // itself or all its operands are loop-invariant. E.g. this considers a load
877 // from a loop-invariant address predictable; we were unable to prove that it
878 // doesn't alias any of the memory-writes in the loop, but it is likely to
879 // read to same value multiple times.
880 if (CurrentLoop
&& any_of(IfConv
.Cond
, [&](MachineOperand
&MO
) {
881 if (!MO
.isReg() || !MO
.isUse())
883 Register Reg
= MO
.getReg();
884 if (Register::isPhysicalRegister(Reg
))
887 MachineInstr
*Def
= MRI
->getVRegDef(Reg
);
888 return CurrentLoop
->isLoopInvariant(*Def
) ||
889 all_of(Def
->operands(), [&](MachineOperand
&Op
) {
892 if (!MO
.isReg() || !MO
.isUse())
894 Register Reg
= MO
.getReg();
895 if (Register::isPhysicalRegister(Reg
))
898 MachineInstr
*Def
= MRI
->getVRegDef(Reg
);
899 return CurrentLoop
->isLoopInvariant(*Def
);
905 MinInstr
= Traces
->getEnsemble(MachineTraceStrategy::TS_MinInstrCount
);
907 MachineTraceMetrics::Trace TBBTrace
= MinInstr
->getTrace(IfConv
.getTPred());
908 MachineTraceMetrics::Trace FBBTrace
= MinInstr
->getTrace(IfConv
.getFPred());
909 LLVM_DEBUG(dbgs() << "TBB: " << TBBTrace
<< "FBB: " << FBBTrace
);
910 unsigned MinCrit
= std::min(TBBTrace
.getCriticalPath(),
911 FBBTrace
.getCriticalPath());
913 // Set a somewhat arbitrary limit on the critical path extension we accept.
914 unsigned CritLimit
= SchedModel
.MispredictPenalty
/2;
916 MachineBasicBlock
&MBB
= *IfConv
.Head
;
917 MachineOptimizationRemarkEmitter
MORE(*MBB
.getParent(), nullptr);
919 // If-conversion only makes sense when there is unexploited ILP. Compute the
920 // maximum-ILP resource length of the trace after if-conversion. Compare it
921 // to the shortest critical path.
922 SmallVector
<const MachineBasicBlock
*, 1> ExtraBlocks
;
923 if (IfConv
.TBB
!= IfConv
.Tail
)
924 ExtraBlocks
.push_back(IfConv
.TBB
);
925 unsigned ResLength
= FBBTrace
.getResourceLength(ExtraBlocks
);
926 LLVM_DEBUG(dbgs() << "Resource length " << ResLength
927 << ", minimal critical path " << MinCrit
<< '\n');
928 if (ResLength
> MinCrit
+ CritLimit
) {
929 LLVM_DEBUG(dbgs() << "Not enough available ILP.\n");
931 MachineOptimizationRemarkMissed
R(DEBUG_TYPE
, "IfConversion",
932 MBB
.findDebugLoc(MBB
.back()), &MBB
);
933 R
<< "did not if-convert branch: the resulting critical path ("
934 << Cycles
{"ResLength", ResLength
}
935 << ") would extend the shorter leg's critical path ("
936 << Cycles
{"MinCrit", MinCrit
} << ") by more than the threshold of "
937 << Cycles
{"CritLimit", CritLimit
}
938 << ", which cannot be hidden by available ILP.";
944 // Assume that the depth of the first head terminator will also be the depth
945 // of the select instruction inserted, as determined by the flag dependency.
946 // TBB / FBB data dependencies may delay the select even more.
947 MachineTraceMetrics::Trace HeadTrace
= MinInstr
->getTrace(IfConv
.Head
);
948 unsigned BranchDepth
=
949 HeadTrace
.getInstrCycles(*IfConv
.Head
->getFirstTerminator()).Depth
;
950 LLVM_DEBUG(dbgs() << "Branch depth: " << BranchDepth
<< '\n');
952 // Look at all the tail phis, and compute the critical path extension caused
953 // by inserting select instructions.
954 MachineTraceMetrics::Trace TailTrace
= MinInstr
->getTrace(IfConv
.Tail
);
955 struct CriticalPathInfo
{
956 unsigned Extra
; // Count of extra cycles that the component adds.
957 unsigned Depth
; // Absolute depth of the component in cycles.
959 CriticalPathInfo Cond
{};
960 CriticalPathInfo TBlock
{};
961 CriticalPathInfo FBlock
{};
962 bool ShouldConvert
= true;
963 for (SSAIfConv::PHIInfo
&PI
: IfConv
.PHIs
) {
964 unsigned Slack
= TailTrace
.getInstrSlack(*PI
.PHI
);
965 unsigned MaxDepth
= Slack
+ TailTrace
.getInstrCycles(*PI
.PHI
).Depth
;
966 LLVM_DEBUG(dbgs() << "Slack " << Slack
<< ":\t" << *PI
.PHI
);
968 // The condition is pulled into the critical path.
969 unsigned CondDepth
= adjCycles(BranchDepth
, PI
.CondCycles
);
970 if (CondDepth
> MaxDepth
) {
971 unsigned Extra
= CondDepth
- MaxDepth
;
972 LLVM_DEBUG(dbgs() << "Condition adds " << Extra
<< " cycles.\n");
973 if (Extra
> Cond
.Extra
)
974 Cond
= {Extra
, CondDepth
};
975 if (Extra
> CritLimit
) {
976 LLVM_DEBUG(dbgs() << "Exceeds limit of " << CritLimit
<< '\n');
977 ShouldConvert
= false;
981 // The TBB value is pulled into the critical path.
982 unsigned TDepth
= adjCycles(TBBTrace
.getPHIDepth(*PI
.PHI
), PI
.TCycles
);
983 if (TDepth
> MaxDepth
) {
984 unsigned Extra
= TDepth
- MaxDepth
;
985 LLVM_DEBUG(dbgs() << "TBB data adds " << Extra
<< " cycles.\n");
986 if (Extra
> TBlock
.Extra
)
987 TBlock
= {Extra
, TDepth
};
988 if (Extra
> CritLimit
) {
989 LLVM_DEBUG(dbgs() << "Exceeds limit of " << CritLimit
<< '\n');
990 ShouldConvert
= false;
994 // The FBB value is pulled into the critical path.
995 unsigned FDepth
= adjCycles(FBBTrace
.getPHIDepth(*PI
.PHI
), PI
.FCycles
);
996 if (FDepth
> MaxDepth
) {
997 unsigned Extra
= FDepth
- MaxDepth
;
998 LLVM_DEBUG(dbgs() << "FBB data adds " << Extra
<< " cycles.\n");
999 if (Extra
> FBlock
.Extra
)
1000 FBlock
= {Extra
, FDepth
};
1001 if (Extra
> CritLimit
) {
1002 LLVM_DEBUG(dbgs() << "Exceeds limit of " << CritLimit
<< '\n');
1003 ShouldConvert
= false;
1008 // Organize by "short" and "long" legs, since the diagnostics get confusing
1009 // when referring to the "true" and "false" sides of the branch, given that
1010 // those don't always correlate with what the user wrote in source-terms.
1011 const CriticalPathInfo Short
= TBlock
.Extra
> FBlock
.Extra
? FBlock
: TBlock
;
1012 const CriticalPathInfo Long
= TBlock
.Extra
> FBlock
.Extra
? TBlock
: FBlock
;
1014 if (ShouldConvert
) {
1016 MachineOptimizationRemark
R(DEBUG_TYPE
, "IfConversion",
1017 MBB
.back().getDebugLoc(), &MBB
);
1018 R
<< "performing if-conversion on branch: the condition adds "
1019 << Cycles
{"CondCycles", Cond
.Extra
} << " to the critical path";
1020 if (Short
.Extra
> 0)
1021 R
<< ", and the short leg adds another "
1022 << Cycles
{"ShortCycles", Short
.Extra
};
1024 R
<< ", and the long leg adds another "
1025 << Cycles
{"LongCycles", Long
.Extra
};
1026 R
<< ", each staying under the threshold of "
1027 << Cycles
{"CritLimit", CritLimit
} << ".";
1032 MachineOptimizationRemarkMissed
R(DEBUG_TYPE
, "IfConversion",
1033 MBB
.back().getDebugLoc(), &MBB
);
1034 R
<< "did not if-convert branch: the condition would add "
1035 << Cycles
{"CondCycles", Cond
.Extra
} << " to the critical path";
1036 if (Cond
.Extra
> CritLimit
)
1037 R
<< " exceeding the limit of " << Cycles
{"CritLimit", CritLimit
};
1038 if (Short
.Extra
> 0) {
1039 R
<< ", and the short leg would add another "
1040 << Cycles
{"ShortCycles", Short
.Extra
};
1041 if (Short
.Extra
> CritLimit
)
1042 R
<< " exceeding the limit of " << Cycles
{"CritLimit", CritLimit
};
1044 if (Long
.Extra
> 0) {
1045 R
<< ", and the long leg would add another "
1046 << Cycles
{"LongCycles", Long
.Extra
};
1047 if (Long
.Extra
> CritLimit
)
1048 R
<< " exceeding the limit of " << Cycles
{"CritLimit", CritLimit
};
1055 return ShouldConvert
;
1058 /// Attempt repeated if-conversion on MBB, return true if successful.
1060 bool EarlyIfConverter::tryConvertIf(MachineBasicBlock
*MBB
) {
1061 bool Changed
= false;
1062 while (IfConv
.canConvertIf(MBB
) && shouldConvertIf()) {
1063 // If-convert MBB and update analyses.
1065 SmallVector
<MachineBasicBlock
*, 4> RemovedBlocks
;
1066 IfConv
.convertIf(RemovedBlocks
);
1068 updateDomTree(DomTree
, IfConv
, RemovedBlocks
);
1069 updateLoops(Loops
, RemovedBlocks
);
1074 bool EarlyIfConverter::runOnMachineFunction(MachineFunction
&MF
) {
1075 LLVM_DEBUG(dbgs() << "********** EARLY IF-CONVERSION **********\n"
1076 << "********** Function: " << MF
.getName() << '\n');
1077 if (skipFunction(MF
.getFunction()))
1080 // Only run if conversion if the target wants it.
1081 const TargetSubtargetInfo
&STI
= MF
.getSubtarget();
1082 if (!STI
.enableEarlyIfConversion())
1085 TII
= STI
.getInstrInfo();
1086 TRI
= STI
.getRegisterInfo();
1087 SchedModel
= STI
.getSchedModel();
1088 MRI
= &MF
.getRegInfo();
1089 DomTree
= &getAnalysis
<MachineDominatorTreeWrapperPass
>().getDomTree();
1090 Loops
= &getAnalysis
<MachineLoopInfoWrapperPass
>().getLI();
1091 Traces
= &getAnalysis
<MachineTraceMetrics
>();
1094 bool Changed
= false;
1095 IfConv
.runOnMachineFunction(MF
);
1097 // Visit blocks in dominator tree post-order. The post-order enables nested
1098 // if-conversion in a single pass. The tryConvertIf() function may erase
1099 // blocks, but only blocks dominated by the head block. This makes it safe to
1100 // update the dominator tree while the post-order iterator is still active.
1101 for (auto *DomNode
: post_order(DomTree
))
1102 if (tryConvertIf(DomNode
->getBlock()))
1108 //===----------------------------------------------------------------------===//
1109 // EarlyIfPredicator Pass
1110 //===----------------------------------------------------------------------===//
1113 class EarlyIfPredicator
: public MachineFunctionPass
{
1114 const TargetInstrInfo
*TII
= nullptr;
1115 const TargetRegisterInfo
*TRI
= nullptr;
1116 TargetSchedModel SchedModel
;
1117 MachineRegisterInfo
*MRI
= nullptr;
1118 MachineDominatorTree
*DomTree
= nullptr;
1119 MachineBranchProbabilityInfo
*MBPI
= nullptr;
1120 MachineLoopInfo
*Loops
= nullptr;
1125 EarlyIfPredicator() : MachineFunctionPass(ID
) {}
1126 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
1127 bool runOnMachineFunction(MachineFunction
&MF
) override
;
1128 StringRef
getPassName() const override
{ return "Early If-predicator"; }
1131 bool tryConvertIf(MachineBasicBlock
*);
1132 bool shouldConvertIf();
1134 } // end anonymous namespace
1137 #define DEBUG_TYPE "early-if-predicator"
1139 char EarlyIfPredicator::ID
= 0;
1140 char &llvm::EarlyIfPredicatorID
= EarlyIfPredicator::ID
;
1142 INITIALIZE_PASS_BEGIN(EarlyIfPredicator
, DEBUG_TYPE
, "Early If Predicator",
1144 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass
)
1145 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass
)
1146 INITIALIZE_PASS_END(EarlyIfPredicator
, DEBUG_TYPE
, "Early If Predicator", false,
1149 void EarlyIfPredicator::getAnalysisUsage(AnalysisUsage
&AU
) const {
1150 AU
.addRequired
<MachineBranchProbabilityInfoWrapperPass
>();
1151 AU
.addRequired
<MachineDominatorTreeWrapperPass
>();
1152 AU
.addPreserved
<MachineDominatorTreeWrapperPass
>();
1153 AU
.addRequired
<MachineLoopInfoWrapperPass
>();
1154 AU
.addPreserved
<MachineLoopInfoWrapperPass
>();
1155 MachineFunctionPass::getAnalysisUsage(AU
);
1158 /// Apply the target heuristic to decide if the transformation is profitable.
1159 bool EarlyIfPredicator::shouldConvertIf() {
1160 auto TrueProbability
= MBPI
->getEdgeProbability(IfConv
.Head
, IfConv
.TBB
);
1161 if (IfConv
.isTriangle()) {
1162 MachineBasicBlock
&IfBlock
=
1163 (IfConv
.TBB
== IfConv
.Tail
) ? *IfConv
.FBB
: *IfConv
.TBB
;
1165 unsigned ExtraPredCost
= 0;
1166 unsigned Cycles
= 0;
1167 for (MachineInstr
&I
: IfBlock
) {
1168 unsigned NumCycles
= SchedModel
.computeInstrLatency(&I
, false);
1170 Cycles
+= NumCycles
- 1;
1171 ExtraPredCost
+= TII
->getPredicationCost(I
);
1174 return TII
->isProfitableToIfCvt(IfBlock
, Cycles
, ExtraPredCost
,
1177 unsigned TExtra
= 0;
1178 unsigned FExtra
= 0;
1179 unsigned TCycle
= 0;
1180 unsigned FCycle
= 0;
1181 for (MachineInstr
&I
: *IfConv
.TBB
) {
1182 unsigned NumCycles
= SchedModel
.computeInstrLatency(&I
, false);
1184 TCycle
+= NumCycles
- 1;
1185 TExtra
+= TII
->getPredicationCost(I
);
1187 for (MachineInstr
&I
: *IfConv
.FBB
) {
1188 unsigned NumCycles
= SchedModel
.computeInstrLatency(&I
, false);
1190 FCycle
+= NumCycles
- 1;
1191 FExtra
+= TII
->getPredicationCost(I
);
1193 return TII
->isProfitableToIfCvt(*IfConv
.TBB
, TCycle
, TExtra
, *IfConv
.FBB
,
1194 FCycle
, FExtra
, TrueProbability
);
1197 /// Attempt repeated if-conversion on MBB, return true if successful.
1199 bool EarlyIfPredicator::tryConvertIf(MachineBasicBlock
*MBB
) {
1200 bool Changed
= false;
1201 while (IfConv
.canConvertIf(MBB
, /*Predicate*/ true) && shouldConvertIf()) {
1202 // If-convert MBB and update analyses.
1203 SmallVector
<MachineBasicBlock
*, 4> RemovedBlocks
;
1204 IfConv
.convertIf(RemovedBlocks
, /*Predicate*/ true);
1206 updateDomTree(DomTree
, IfConv
, RemovedBlocks
);
1207 updateLoops(Loops
, RemovedBlocks
);
1212 bool EarlyIfPredicator::runOnMachineFunction(MachineFunction
&MF
) {
1213 LLVM_DEBUG(dbgs() << "********** EARLY IF-PREDICATOR **********\n"
1214 << "********** Function: " << MF
.getName() << '\n');
1215 if (skipFunction(MF
.getFunction()))
1218 const TargetSubtargetInfo
&STI
= MF
.getSubtarget();
1219 TII
= STI
.getInstrInfo();
1220 TRI
= STI
.getRegisterInfo();
1221 MRI
= &MF
.getRegInfo();
1222 SchedModel
.init(&STI
);
1223 DomTree
= &getAnalysis
<MachineDominatorTreeWrapperPass
>().getDomTree();
1224 Loops
= &getAnalysis
<MachineLoopInfoWrapperPass
>().getLI();
1225 MBPI
= &getAnalysis
<MachineBranchProbabilityInfoWrapperPass
>().getMBPI();
1227 bool Changed
= false;
1228 IfConv
.runOnMachineFunction(MF
);
1230 // Visit blocks in dominator tree post-order. The post-order enables nested
1231 // if-conversion in a single pass. The tryConvertIf() function may erase
1232 // blocks, but only blocks dominated by the head block. This makes it safe to
1233 // update the dominator tree while the post-order iterator is still active.
1234 for (auto *DomNode
: post_order(DomTree
))
1235 if (tryConvertIf(DomNode
->getBlock()))