Skip to content

Commit 0e61a45

Browse files
committed
[NFC][EarlyIfConverter] Replace boolean Preadicate for a class
This will help when adding new EarlyIfConverter transformations (other than the current 2)
1 parent 09a4c23 commit 0e61a45

File tree

1 file changed

+117
-137
lines changed

1 file changed

+117
-137
lines changed

llvm/lib/CodeGen/EarlyIfConversion.cpp

Lines changed: 117 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ class SSAIfConv {
122122
/// The branch condition determined by analyzeBranch.
123123
SmallVector<MachineOperand, 4> Cond;
124124

125+
struct PredicationStrategyBase {
126+
virtual bool canConvertIf(MachineBasicBlock *Tail) { return true; }
127+
virtual bool canPredicateInstr(const MachineInstr &I) = 0;
128+
virtual void predicateBlock(MachineBasicBlock *MBB,
129+
ArrayRef<MachineOperand> Cond,
130+
bool Reverse) = 0;
131+
virtual ~PredicationStrategyBase() = default;
132+
};
133+
134+
PredicationStrategyBase &Predicate;
135+
125136
private:
126137
/// Instructions in Head that define values used by the conditional blocks.
127138
/// The hoisted instructions must be inserted after these instructions.
@@ -137,10 +148,6 @@ class SSAIfConv {
137148
/// and FBB.
138149
MachineBasicBlock::iterator InsertionPoint;
139150

140-
/// Return true if all non-terminator instructions in MBB can be safely
141-
/// speculated.
142-
bool canSpeculateInstrs(MachineBasicBlock *MBB);
143-
144151
/// Return true if all non-terminator instructions in MBB can be safely
145152
/// predicated.
146153
bool canPredicateInstrs(MachineBasicBlock *MBB);
@@ -149,10 +156,6 @@ class SSAIfConv {
149156
/// Return false if any dependency is incompatible with if conversion.
150157
bool InstrDependenciesAllowIfConv(MachineInstr *I);
151158

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);
155-
156159
/// Find a valid insertion point in Head.
157160
bool findInsertionPoint();
158161

@@ -163,7 +166,8 @@ class SSAIfConv {
163166
void rewritePHIOperands();
164167

165168
public:
166-
SSAIfConv(MachineFunction &MF) {
169+
SSAIfConv(PredicationStrategyBase &Predicate, MachineFunction &MF)
170+
: Predicate(Predicate) {
167171
TII = MF.getSubtarget().getInstrInfo();
168172
TRI = MF.getSubtarget().getRegisterInfo();
169173
MRI = &MF.getRegInfo();
@@ -175,77 +179,14 @@ class SSAIfConv {
175179

176180
/// canConvertIf - If the sub-CFG headed by MBB can be if-converted,
177181
/// initialize the internal state, and return true.
178-
/// If predicate is set try to predicate the block otherwise try to
179-
/// speculatively execute it.
180-
bool canConvertIf(MachineBasicBlock *MBB, bool Predicate = false);
182+
bool canConvertIf(MachineBasicBlock *MBB);
181183

182184
/// convertIf - If-convert the last block passed to canConvertIf(), assuming
183185
/// it is possible. Add any blocks that are to be erased to RemoveBlocks.
184-
void convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
185-
bool Predicate = false);
186+
void convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks);
186187
};
187188
} // end anonymous namespace
188189

189-
190-
/// canSpeculateInstrs - Returns true if all the instructions in MBB can safely
191-
/// be speculated. The terminators are not considered.
192-
///
193-
/// If instructions use any values that are defined in the head basic block,
194-
/// the defining instructions are added to InsertAfter.
195-
///
196-
/// Any clobbered regunits are added to ClobberedRegUnits.
197-
///
198-
bool SSAIfConv::canSpeculateInstrs(MachineBasicBlock *MBB) {
199-
// Reject any live-in physregs. It's probably CPSR/EFLAGS, and very hard to
200-
// get right.
201-
if (!MBB->livein_empty()) {
202-
LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has live-ins.\n");
203-
return false;
204-
}
205-
206-
unsigned InstrCount = 0;
207-
208-
// Check all instructions, except the terminators. It is assumed that
209-
// terminators never have side effects or define any used register values.
210-
for (MachineInstr &MI :
211-
llvm::make_range(MBB->begin(), MBB->getFirstTerminator())) {
212-
if (MI.isDebugInstr())
213-
continue;
214-
215-
if (++InstrCount > BlockInstrLimit && !Stress) {
216-
LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has more than "
217-
<< BlockInstrLimit << " instructions.\n");
218-
return false;
219-
}
220-
221-
// There shouldn't normally be any phis in a single-predecessor block.
222-
if (MI.isPHI()) {
223-
LLVM_DEBUG(dbgs() << "Can't hoist: " << MI);
224-
return false;
225-
}
226-
227-
// Don't speculate loads. Note that it may be possible and desirable to
228-
// speculate GOT or constant pool loads that are guaranteed not to trap,
229-
// but we don't support that for now.
230-
if (MI.mayLoad()) {
231-
LLVM_DEBUG(dbgs() << "Won't speculate load: " << MI);
232-
return false;
233-
}
234-
235-
// We never speculate stores, so an AA pointer isn't necessary.
236-
bool DontMoveAcrossStore = true;
237-
if (!MI.isSafeToMove(DontMoveAcrossStore)) {
238-
LLVM_DEBUG(dbgs() << "Can't speculate: " << MI);
239-
return false;
240-
}
241-
242-
// Check for any dependencies on Head instructions.
243-
if (!InstrDependenciesAllowIfConv(&MI))
244-
return false;
245-
}
246-
return true;
247-
}
248-
249190
/// Check that there is no dependencies preventing if conversion.
250191
///
251192
/// If instruction uses any values that are defined in the head basic block,
@@ -319,17 +260,8 @@ bool SSAIfConv::canPredicateInstrs(MachineBasicBlock *MBB) {
319260
return false;
320261
}
321262

322-
// Check that instruction is predicable
323-
if (!TII->isPredicable(*I)) {
324-
LLVM_DEBUG(dbgs() << "Isn't predicable: " << *I);
325-
return false;
326-
}
327-
328-
// Check that instruction is not already predicated.
329-
if (TII->isPredicated(*I) && !TII->canPredicatePredicatedInstr(*I)) {
330-
LLVM_DEBUG(dbgs() << "Is already predicated: " << *I);
263+
if (!Predicate.canPredicateInstr(*I))
331264
return false;
332-
}
333265

334266
// Check for any dependencies on Head instructions.
335267
if (!InstrDependenciesAllowIfConv(&(*I)))
@@ -338,24 +270,6 @@ bool SSAIfConv::canPredicateInstrs(MachineBasicBlock *MBB) {
338270
return true;
339271
}
340272

341-
// Apply predicate to all instructions in the machine block.
342-
void SSAIfConv::PredicateBlock(MachineBasicBlock *MBB, bool ReversePredicate) {
343-
auto Condition = Cond;
344-
if (ReversePredicate) {
345-
bool CanRevCond = !TII->reverseBranchCondition(Condition);
346-
assert(CanRevCond && "Reversed predicate is not supported");
347-
(void)CanRevCond;
348-
}
349-
// Terminators don't need to be predicated as they will be removed.
350-
for (MachineBasicBlock::iterator I = MBB->begin(),
351-
E = MBB->getFirstTerminator();
352-
I != E; ++I) {
353-
if (I->isDebugInstr())
354-
continue;
355-
TII->PredicateInstruction(*I, Condition);
356-
}
357-
}
358-
359273
/// Find an insertion point in Head for the speculated instructions. The
360274
/// insertion point must be:
361275
///
@@ -434,7 +348,7 @@ bool SSAIfConv::findInsertionPoint() {
434348
/// canConvertIf - analyze the sub-cfg rooted in MBB, and return true if it is
435349
/// a potential candidate for if-conversion. Fill out the internal state.
436350
///
437-
bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB, bool Predicate) {
351+
bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB) {
438352
Head = MBB;
439353
TBB = FBB = Tail = nullptr;
440354

@@ -474,21 +388,17 @@ bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB, bool Predicate) {
474388
<< printMBBReference(*Tail) << '\n');
475389
}
476390

477-
// This is a triangle or a diamond.
478-
// Skip if we cannot predicate and there are no phis skip as there must be
479-
// side effects that can only be handled with predication.
480-
if (!Predicate && (Tail->empty() || !Tail->front().isPHI())) {
481-
LLVM_DEBUG(dbgs() << "No phis in tail.\n");
482-
return false;
483-
}
484-
485391
// The branch we're looking to eliminate must be analyzable.
486392
Cond.clear();
487393
if (TII->analyzeBranch(*Head, TBB, FBB, Cond)) {
488394
LLVM_DEBUG(dbgs() << "Branch not analyzable.\n");
489395
return false;
490396
}
491397

398+
if (!Predicate.canConvertIf(Tail)) {
399+
return false;
400+
}
401+
492402
// This is weird, probably some sort of degenerate CFG.
493403
if (!TBB) {
494404
LLVM_DEBUG(dbgs() << "analyzeBranch didn't find conditional branch.\n");
@@ -536,17 +446,9 @@ bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB, bool Predicate) {
536446
// Check that the conditional instructions can be speculated.
537447
InsertAfter.clear();
538448
ClobberedRegUnits.reset();
539-
if (Predicate) {
540-
if (TBB != Tail && !canPredicateInstrs(TBB))
541-
return false;
542-
if (FBB != Tail && !canPredicateInstrs(FBB))
543-
return false;
544-
} else {
545-
if (TBB != Tail && !canSpeculateInstrs(TBB))
546-
return false;
547-
if (FBB != Tail && !canSpeculateInstrs(FBB))
449+
for (MachineBasicBlock *MBB : {TBB, FBB})
450+
if (MBB != Tail && !canPredicateInstrs(MBB))
548451
return false;
549-
}
550452

551453
// Try to find a valid insertion point for the speculated instructions in the
552454
// head basic block.
@@ -679,8 +581,7 @@ void SSAIfConv::rewritePHIOperands() {
679581
///
680582
/// Any basic blocks that need to be erased will be added to RemoveBlocks.
681583
///
682-
void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
683-
bool Predicate) {
584+
void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks) {
684585
assert(Head && Tail && TBB && FBB && "Call canConvertIf first.");
685586

686587
// Update statistics.
@@ -690,16 +591,15 @@ void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
690591
++NumDiamondsConv;
691592

692593
// Move all instructions into Head, except for the terminators.
693-
if (TBB != Tail) {
694-
if (Predicate)
695-
PredicateBlock(TBB, /*ReversePredicate=*/false);
696-
Head->splice(InsertionPoint, TBB, TBB->begin(), TBB->getFirstTerminator());
697-
}
698-
if (FBB != Tail) {
699-
if (Predicate)
700-
PredicateBlock(FBB, /*ReversePredicate=*/true);
701-
Head->splice(InsertionPoint, FBB, FBB->begin(), FBB->getFirstTerminator());
594+
for (MachineBasicBlock *MBB : {TBB, FBB}) {
595+
if (MBB != Tail) {
596+
// reverse the condition for the false bb
597+
Predicate.predicateBlock(MBB, Cond, MBB == FBB);
598+
Head->splice(InsertionPoint, MBB, MBB->begin(),
599+
MBB->getFirstTerminator());
600+
}
702601
}
602+
703603
// Are there extra Tail predecessors?
704604
bool ExtraPreds = Tail->pred_size() != 2;
705605
if (ExtraPreds)
@@ -863,6 +763,45 @@ template <typename Remark> Remark &operator<<(Remark &R, Cycles C) {
863763
}
864764
} // anonymous namespace
865765

766+
struct SpeculateStrategy : SSAIfConv::PredicationStrategyBase {
767+
bool canConvertIf(MachineBasicBlock *Tail) override {
768+
// This is a triangle or a diamond.
769+
// Skip if we cannot predicate and there are no phis skip as there must
770+
// be side effects that can only be handled with predication.
771+
if (Tail->empty() || !Tail->front().isPHI()) {
772+
LLVM_DEBUG(dbgs() << "No phis in tail.\n");
773+
return false;
774+
}
775+
return true;
776+
}
777+
778+
bool canPredicateInstr(const MachineInstr &I) override {
779+
// Don't speculate loads. Note that it may be possible and desirable to
780+
// speculate GOT or constant pool loads that are guaranteed not to trap,
781+
// but we don't support that for now.
782+
if (I.mayLoad()) {
783+
LLVM_DEBUG(dbgs() << "Won't speculate load: " << I);
784+
return false;
785+
}
786+
787+
// We never speculate stores, so an AA pointer isn't necessary.
788+
bool DontMoveAcrossStore = true;
789+
if (!I.isSafeToMove(DontMoveAcrossStore)) {
790+
LLVM_DEBUG(dbgs() << "Can't speculate: " << I);
791+
return false;
792+
}
793+
return true;
794+
}
795+
796+
void predicateBlock(MachineBasicBlock *MBB, ArrayRef<MachineOperand> Cond,
797+
bool Reverse)
798+
override { /* do nothing, everything is speculatable and it's valid to
799+
move the instructions into the head */
800+
}
801+
802+
~SpeculateStrategy() override = default;
803+
};
804+
866805
/// Apply cost model and heuristics to the if-conversion in IfConv.
867806
/// Return true if the conversion is a good idea.
868807
///
@@ -1096,7 +1035,8 @@ bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) {
10961035
MinInstr = nullptr;
10971036

10981037
bool Changed = false;
1099-
SSAIfConv IfConv(MF);
1038+
SpeculateStrategy Speculate;
1039+
SSAIfConv IfConv(Speculate, MF);
11001040

11011041
// Visit blocks in dominator tree post-order. The post-order enables nested
11021042
// if-conversion in a single pass. The tryConvertIf() function may erase
@@ -1158,6 +1098,46 @@ void EarlyIfPredicator::getAnalysisUsage(AnalysisUsage &AU) const {
11581098
MachineFunctionPass::getAnalysisUsage(AU);
11591099
}
11601100

1101+
struct PredicatorStrategy : SSAIfConv::PredicationStrategyBase {
1102+
const TargetInstrInfo *TII = nullptr;
1103+
PredicatorStrategy(const TargetInstrInfo *TII) : TII(TII) {}
1104+
1105+
bool canPredicateInstr(const MachineInstr &I) override {
1106+
// Check that instruction is predicable
1107+
if (!TII->isPredicable(I)) {
1108+
LLVM_DEBUG(dbgs() << "Isn't predicable: " << I);
1109+
return false;
1110+
}
1111+
1112+
// Check that instruction is not already predicated.
1113+
if (TII->isPredicated(I) && !TII->canPredicatePredicatedInstr(I)) {
1114+
LLVM_DEBUG(dbgs() << "Is already predicated: " << I);
1115+
return false;
1116+
}
1117+
return true;
1118+
}
1119+
1120+
void predicateBlock(MachineBasicBlock *MBB, ArrayRef<MachineOperand> Cond,
1121+
bool Reverse) override {
1122+
SmallVector<MachineOperand> Condition(Cond.begin(), Cond.end());
1123+
if (Reverse) {
1124+
bool CanRevCond = !TII->reverseBranchCondition(Condition);
1125+
assert(CanRevCond && "Reversed predicate is not supported");
1126+
(void)CanRevCond;
1127+
}
1128+
// Terminators don't need to be predicated as they will be removed.
1129+
for (MachineBasicBlock::iterator I = MBB->begin(),
1130+
E = MBB->getFirstTerminator();
1131+
I != E; ++I) {
1132+
if (I->isDebugInstr())
1133+
continue;
1134+
TII->PredicateInstruction(*I, Condition);
1135+
}
1136+
}
1137+
1138+
~PredicatorStrategy() override = default;
1139+
};
1140+
11611141
/// Apply the target heuristic to decide if the transformation is profitable.
11621142
bool EarlyIfPredicator::shouldConvertIf(SSAIfConv &IfConv) {
11631143
auto TrueProbability = MBPI->getEdgeProbability(IfConv.Head, IfConv.TBB);
@@ -1202,11 +1182,10 @@ bool EarlyIfPredicator::shouldConvertIf(SSAIfConv &IfConv) {
12021182
bool EarlyIfPredicator::tryConvertIf(SSAIfConv &IfConv,
12031183
MachineBasicBlock *MBB) {
12041184
bool Changed = false;
1205-
while (IfConv.canConvertIf(MBB, /*Predicate=*/true) &&
1206-
shouldConvertIf(IfConv)) {
1185+
while (IfConv.canConvertIf(MBB) && shouldConvertIf(IfConv)) {
12071186
// If-convert MBB and update analyses.
12081187
SmallVector<MachineBasicBlock *, 4> RemoveBlocks;
1209-
IfConv.convertIf(RemoveBlocks, /*Predicate=*/true);
1188+
IfConv.convertIf(RemoveBlocks);
12101189
Changed = true;
12111190
updateDomTree(DomTree, IfConv, RemoveBlocks);
12121191
for (MachineBasicBlock *MBB : RemoveBlocks)
@@ -1232,7 +1211,8 @@ bool EarlyIfPredicator::runOnMachineFunction(MachineFunction &MF) {
12321211
MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
12331212

12341213
bool Changed = false;
1235-
SSAIfConv IfConv(MF);
1214+
PredicatorStrategy Predicate(TII);
1215+
SSAIfConv IfConv(Predicate, MF);
12361216

12371217
// Visit blocks in dominator tree post-order. The post-order enables nested
12381218
// if-conversion in a single pass. The tryConvertIf() function may erase

0 commit comments

Comments
 (0)