@@ -122,6 +122,18 @@ class SSAIfConv {
122122 // / The branch condition determined by analyzeBranch.
123123 SmallVector<MachineOperand, 4 > Cond;
124124
125+ class PredicationStrategyBase {
126+ public:
127+ virtual bool canConvertIf (MachineBasicBlock *Tail) { return true ; }
128+ virtual bool canPredicateInstr (const MachineInstr &I) = 0;
129+ virtual void predicateBlock (MachineBasicBlock *MBB,
130+ ArrayRef<MachineOperand> Cond,
131+ bool Reverse) = 0;
132+ virtual ~PredicationStrategyBase () = default ;
133+ };
134+
135+ PredicationStrategyBase &Predicate;
136+
125137private:
126138 // / Instructions in Head that define values used by the conditional blocks.
127139 // / The hoisted instructions must be inserted after these instructions.
@@ -137,10 +149,6 @@ class SSAIfConv {
137149 // / and FBB.
138150 MachineBasicBlock::iterator InsertionPoint;
139151
140- // / Return true if all non-terminator instructions in MBB can be safely
141- // / speculated.
142- bool canSpeculateInstrs (MachineBasicBlock *MBB);
143-
144152 // / Return true if all non-terminator instructions in MBB can be safely
145153 // / predicated.
146154 bool canPredicateInstrs (MachineBasicBlock *MBB);
@@ -149,10 +157,6 @@ class SSAIfConv {
149157 // / Return false if any dependency is incompatible with if conversion.
150158 bool InstrDependenciesAllowIfConv (MachineInstr *I);
151159
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-
156160 // / Find a valid insertion point in Head.
157161 bool findInsertionPoint ();
158162
@@ -163,7 +167,8 @@ class SSAIfConv {
163167 void rewritePHIOperands ();
164168
165169public:
166- SSAIfConv (MachineFunction &MF) {
170+ SSAIfConv (PredicationStrategyBase &Predicate, MachineFunction &MF)
171+ : Predicate(Predicate) {
167172 TII = MF.getSubtarget ().getInstrInfo ();
168173 TRI = MF.getSubtarget ().getRegisterInfo ();
169174 MRI = &MF.getRegInfo ();
@@ -175,77 +180,14 @@ class SSAIfConv {
175180
176181 // / canConvertIf - If the sub-CFG headed by MBB can be if-converted,
177182 // / 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 );
183+ bool canConvertIf (MachineBasicBlock *MBB);
181184
182185 // / convertIf - If-convert the last block passed to canConvertIf(), assuming
183186 // / it is possible. Add any blocks that are to be erased to RemoveBlocks.
184- void convertIf (SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
185- bool Predicate = false );
187+ void convertIf (SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks);
186188};
187189} // end anonymous namespace
188190
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-
249191// / Check that there is no dependencies preventing if conversion.
250192// /
251193// / If instruction uses any values that are defined in the head basic block,
@@ -319,17 +261,8 @@ bool SSAIfConv::canPredicateInstrs(MachineBasicBlock *MBB) {
319261 return false ;
320262 }
321263
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);
264+ if (!Predicate.canPredicateInstr (*I))
331265 return false ;
332- }
333266
334267 // Check for any dependencies on Head instructions.
335268 if (!InstrDependenciesAllowIfConv (&(*I)))
@@ -338,24 +271,6 @@ bool SSAIfConv::canPredicateInstrs(MachineBasicBlock *MBB) {
338271 return true ;
339272}
340273
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-
359274// / Find an insertion point in Head for the speculated instructions. The
360275// / insertion point must be:
361276// /
@@ -434,7 +349,7 @@ bool SSAIfConv::findInsertionPoint() {
434349// / canConvertIf - analyze the sub-cfg rooted in MBB, and return true if it is
435350// / a potential candidate for if-conversion. Fill out the internal state.
436351// /
437- bool SSAIfConv::canConvertIf (MachineBasicBlock *MBB, bool Predicate ) {
352+ bool SSAIfConv::canConvertIf (MachineBasicBlock *MBB) {
438353 Head = MBB;
439354 TBB = FBB = Tail = nullptr ;
440355
@@ -474,21 +389,17 @@ bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB, bool Predicate) {
474389 << printMBBReference (*Tail) << ' \n ' );
475390 }
476391
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-
485392 // The branch we're looking to eliminate must be analyzable.
486393 Cond.clear ();
487394 if (TII->analyzeBranch (*Head, TBB, FBB, Cond)) {
488395 LLVM_DEBUG (dbgs () << " Branch not analyzable.\n " );
489396 return false ;
490397 }
491398
399+ if (!Predicate.canConvertIf (Tail)) {
400+ return false ;
401+ }
402+
492403 // This is weird, probably some sort of degenerate CFG.
493404 if (!TBB) {
494405 LLVM_DEBUG (dbgs () << " analyzeBranch didn't find conditional branch.\n " );
@@ -536,17 +447,9 @@ bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB, bool Predicate) {
536447 // Check that the conditional instructions can be speculated.
537448 InsertAfter.clear ();
538449 ClobberedRegUnits.reset ();
539- if (Predicate) {
540- if (TBB != Tail && !canPredicateInstrs (TBB ))
450+ for (MachineBasicBlock *MBB : {TBB, FBB})
451+ if (MBB != Tail && !canPredicateInstrs (MBB ))
541452 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))
548- return false ;
549- }
550453
551454 // Try to find a valid insertion point for the speculated instructions in the
552455 // head basic block.
@@ -679,8 +582,7 @@ void SSAIfConv::rewritePHIOperands() {
679582// /
680583// / Any basic blocks that need to be erased will be added to RemoveBlocks.
681584// /
682- void SSAIfConv::convertIf (SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
683- bool Predicate) {
585+ void SSAIfConv::convertIf (SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks) {
684586 assert (Head && Tail && TBB && FBB && " Call canConvertIf first." );
685587
686588 // Update statistics.
@@ -690,16 +592,15 @@ void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
690592 ++NumDiamondsConv;
691593
692594 // 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 ());
595+ for (MachineBasicBlock *MBB : {TBB, FBB}) {
596+ if (MBB != Tail) {
597+ // reverse the condition for the false bb
598+ Predicate.predicateBlock (MBB, Cond, MBB == FBB);
599+ Head->splice (InsertionPoint, MBB, MBB->begin (),
600+ MBB->getFirstTerminator ());
601+ }
702602 }
603+
703604 // Are there extra Tail predecessors?
704605 bool ExtraPreds = Tail->pred_size () != 2 ;
705606 if (ExtraPreds)
@@ -863,6 +764,46 @@ template <typename Remark> Remark &operator<<(Remark &R, Cycles C) {
863764}
864765} // anonymous namespace
865766
767+ class SpeculateStrategy : public SSAIfConv ::PredicationStrategyBase {
768+ public:
769+ bool canConvertIf (MachineBasicBlock *Tail) override {
770+ // This is a triangle or a diamond.
771+ // Skip if we cannot predicate and there are no phis skip as there must
772+ // be side effects that can only be handled with predication.
773+ if (Tail->empty () || !Tail->front ().isPHI ()) {
774+ LLVM_DEBUG (dbgs () << " No phis in tail.\n " );
775+ return false ;
776+ }
777+ return true ;
778+ }
779+
780+ bool canPredicateInstr (const MachineInstr &I) override {
781+ // Don't speculate loads. Note that it may be possible and desirable to
782+ // speculate GOT or constant pool loads that are guaranteed not to trap,
783+ // but we don't support that for now.
784+ if (I.mayLoad ()) {
785+ LLVM_DEBUG (dbgs () << " Won't speculate load: " << I);
786+ return false ;
787+ }
788+
789+ // We never speculate stores, so an AA pointer isn't necessary.
790+ bool DontMoveAcrossStore = true ;
791+ if (!I.isSafeToMove (DontMoveAcrossStore)) {
792+ LLVM_DEBUG (dbgs () << " Can't speculate: " << I);
793+ return false ;
794+ }
795+ return true ;
796+ }
797+
798+ void predicateBlock (MachineBasicBlock *MBB, ArrayRef<MachineOperand> Cond,
799+ bool Reverse)
800+ override { /* do nothing, everything is speculatable and it's valid to
801+ move the instructions into the head */
802+ }
803+
804+ ~SpeculateStrategy () override = default ;
805+ };
806+
866807// / Apply cost model and heuristics to the if-conversion in IfConv.
867808// / Return true if the conversion is a good idea.
868809// /
@@ -1096,7 +1037,8 @@ bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) {
10961037 MinInstr = nullptr ;
10971038
10981039 bool Changed = false ;
1099- SSAIfConv IfConv (MF);
1040+ SpeculateStrategy Speculate;
1041+ SSAIfConv IfConv (Speculate, MF);
11001042
11011043 // Visit blocks in dominator tree post-order. The post-order enables nested
11021044 // if-conversion in a single pass. The tryConvertIf() function may erase
@@ -1158,6 +1100,48 @@ void EarlyIfPredicator::getAnalysisUsage(AnalysisUsage &AU) const {
11581100 MachineFunctionPass::getAnalysisUsage (AU);
11591101}
11601102
1103+ class PredicatorStrategy : public SSAIfConv ::PredicationStrategyBase {
1104+ const TargetInstrInfo *TII = nullptr ;
1105+
1106+ public:
1107+ PredicatorStrategy (const TargetInstrInfo *TII) : TII(TII) {}
1108+
1109+ bool canPredicateInstr (const MachineInstr &I) override {
1110+ // Check that instruction is predicable
1111+ if (!TII->isPredicable (I)) {
1112+ LLVM_DEBUG (dbgs () << " Isn't predicable: " << I);
1113+ return false ;
1114+ }
1115+
1116+ // Check that instruction is not already predicated.
1117+ if (TII->isPredicated (I) && !TII->canPredicatePredicatedInstr (I)) {
1118+ LLVM_DEBUG (dbgs () << " Is already predicated: " << I);
1119+ return false ;
1120+ }
1121+ return true ;
1122+ }
1123+
1124+ void predicateBlock (MachineBasicBlock *MBB, ArrayRef<MachineOperand> Cond,
1125+ bool Reverse) override {
1126+ SmallVector<MachineOperand> Condition (Cond.begin (), Cond.end ());
1127+ if (Reverse) {
1128+ bool CanRevCond = !TII->reverseBranchCondition (Condition);
1129+ assert (CanRevCond && " Reversed predicate is not supported" );
1130+ (void )CanRevCond;
1131+ }
1132+ // Terminators don't need to be predicated as they will be removed.
1133+ for (MachineBasicBlock::iterator I = MBB->begin (),
1134+ E = MBB->getFirstTerminator ();
1135+ I != E; ++I) {
1136+ if (I->isDebugInstr ())
1137+ continue ;
1138+ TII->PredicateInstruction (*I, Condition);
1139+ }
1140+ }
1141+
1142+ ~PredicatorStrategy () override = default ;
1143+ };
1144+
11611145// / Apply the target heuristic to decide if the transformation is profitable.
11621146bool EarlyIfPredicator::shouldConvertIf (SSAIfConv &IfConv) {
11631147 auto TrueProbability = MBPI->getEdgeProbability (IfConv.Head , IfConv.TBB );
@@ -1202,11 +1186,10 @@ bool EarlyIfPredicator::shouldConvertIf(SSAIfConv &IfConv) {
12021186bool EarlyIfPredicator::tryConvertIf (SSAIfConv &IfConv,
12031187 MachineBasicBlock *MBB) {
12041188 bool Changed = false ;
1205- while (IfConv.canConvertIf (MBB, /* Predicate=*/ true ) &&
1206- shouldConvertIf (IfConv)) {
1189+ while (IfConv.canConvertIf (MBB) && shouldConvertIf (IfConv)) {
12071190 // If-convert MBB and update analyses.
12081191 SmallVector<MachineBasicBlock *, 4 > RemoveBlocks;
1209- IfConv.convertIf (RemoveBlocks, /* Predicate= */ true );
1192+ IfConv.convertIf (RemoveBlocks);
12101193 Changed = true ;
12111194 updateDomTree (DomTree, IfConv, RemoveBlocks);
12121195 for (MachineBasicBlock *MBB : RemoveBlocks)
@@ -1232,7 +1215,8 @@ bool EarlyIfPredicator::runOnMachineFunction(MachineFunction &MF) {
12321215 MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI ();
12331216
12341217 bool Changed = false ;
1235- SSAIfConv IfConv (MF);
1218+ PredicatorStrategy Predicate (TII);
1219+ SSAIfConv IfConv (Predicate, MF);
12361220
12371221 // Visit blocks in dominator tree post-order. The post-order enables nested
12381222 // if-conversion in a single pass. The tryConvertIf() function may erase
0 commit comments