@@ -122,18 +122,6 @@ class SSAIfConv {
122
122
// / The branch condition determined by analyzeBranch.
123
123
SmallVector<MachineOperand, 4 > Cond;
124
124
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
-
137
125
private:
138
126
// / Instructions in Head that define values used by the conditional blocks.
139
127
// / The hoisted instructions must be inserted after these instructions.
@@ -149,6 +137,10 @@ class SSAIfConv {
149
137
// / and FBB.
150
138
MachineBasicBlock::iterator InsertionPoint;
151
139
140
+ // / Return true if all non-terminator instructions in MBB can be safely
141
+ // / speculated.
142
+ bool canSpeculateInstrs (MachineBasicBlock *MBB);
143
+
152
144
// / Return true if all non-terminator instructions in MBB can be safely
153
145
// / predicated.
154
146
bool canPredicateInstrs (MachineBasicBlock *MBB);
@@ -157,6 +149,10 @@ class SSAIfConv {
157
149
// / Return false if any dependency is incompatible with if conversion.
158
150
bool InstrDependenciesAllowIfConv (MachineInstr *I);
159
151
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
+
160
156
// / Find a valid insertion point in Head.
161
157
bool findInsertionPoint ();
162
158
@@ -167,8 +163,7 @@ class SSAIfConv {
167
163
void rewritePHIOperands ();
168
164
169
165
public:
170
- SSAIfConv (PredicationStrategyBase &Predicate, MachineFunction &MF)
171
- : Predicate(Predicate) {
166
+ SSAIfConv (MachineFunction &MF) {
172
167
TII = MF.getSubtarget ().getInstrInfo ();
173
168
TRI = MF.getSubtarget ().getRegisterInfo ();
174
169
MRI = &MF.getRegInfo ();
@@ -180,14 +175,76 @@ class SSAIfConv {
180
175
181
176
// / canConvertIf - If the sub-CFG headed by MBB can be if-converted,
182
177
// / initialize the internal state, and return true.
183
- bool canConvertIf (MachineBasicBlock *MBB);
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 );
184
181
185
182
// / convertIf - If-convert the last block passed to canConvertIf(), assuming
186
183
// / it is possible. Add any blocks that are to be erased to RemoveBlocks.
187
- void convertIf (SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks);
184
+ void convertIf (SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
185
+ bool Predicate = false );
188
186
};
189
187
} // end anonymous namespace
190
188
189
+ // / canSpeculateInstrs - Returns true if all the instructions in MBB can safely
190
+ // / be speculated. The terminators are not considered.
191
+ // /
192
+ // / If instructions use any values that are defined in the head basic block,
193
+ // / the defining instructions are added to InsertAfter.
194
+ // /
195
+ // / Any clobbered regunits are added to ClobberedRegUnits.
196
+ // /
197
+ bool SSAIfConv::canSpeculateInstrs (MachineBasicBlock *MBB) {
198
+ // Reject any live-in physregs. It's probably CPSR/EFLAGS, and very hard to
199
+ // get right.
200
+ if (!MBB->livein_empty ()) {
201
+ LLVM_DEBUG (dbgs () << printMBBReference (*MBB) << " has live-ins.\n " );
202
+ return false ;
203
+ }
204
+
205
+ unsigned InstrCount = 0 ;
206
+
207
+ // Check all instructions, except the terminators. It is assumed that
208
+ // terminators never have side effects or define any used register values.
209
+ for (MachineInstr &MI :
210
+ llvm::make_range (MBB->begin (), MBB->getFirstTerminator ())) {
211
+ if (MI.isDebugInstr ())
212
+ continue ;
213
+
214
+ if (++InstrCount > BlockInstrLimit && !Stress) {
215
+ LLVM_DEBUG (dbgs () << printMBBReference (*MBB) << " has more than "
216
+ << BlockInstrLimit << " instructions.\n " );
217
+ return false ;
218
+ }
219
+
220
+ // There shouldn't normally be any phis in a single-predecessor block.
221
+ if (MI.isPHI ()) {
222
+ LLVM_DEBUG (dbgs () << " Can't hoist: " << MI);
223
+ return false ;
224
+ }
225
+
226
+ // Don't speculate loads. Note that it may be possible and desirable to
227
+ // speculate GOT or constant pool loads that are guaranteed not to trap,
228
+ // but we don't support that for now.
229
+ if (MI.mayLoad ()) {
230
+ LLVM_DEBUG (dbgs () << " Won't speculate load: " << MI);
231
+ return false ;
232
+ }
233
+
234
+ // We never speculate stores, so an AA pointer isn't necessary.
235
+ bool DontMoveAcrossStore = true ;
236
+ if (!MI.isSafeToMove (DontMoveAcrossStore)) {
237
+ LLVM_DEBUG (dbgs () << " Can't speculate: " << MI);
238
+ return false ;
239
+ }
240
+
241
+ // Check for any dependencies on Head instructions.
242
+ if (!InstrDependenciesAllowIfConv (&MI))
243
+ return false ;
244
+ }
245
+ return true ;
246
+ }
247
+
191
248
// / Check that there is no dependencies preventing if conversion.
192
249
// /
193
250
// / If instruction uses any values that are defined in the head basic block,
@@ -261,8 +318,17 @@ bool SSAIfConv::canPredicateInstrs(MachineBasicBlock *MBB) {
261
318
return false ;
262
319
}
263
320
264
- if (!Predicate.canPredicateInstr (*I))
321
+ // Check that instruction is predicable
322
+ if (!TII->isPredicable (*I)) {
323
+ LLVM_DEBUG (dbgs () << " Isn't predicable: " << *I);
324
+ return false ;
325
+ }
326
+
327
+ // Check that instruction is not already predicated.
328
+ if (TII->isPredicated (*I) && !TII->canPredicatePredicatedInstr (*I)) {
329
+ LLVM_DEBUG (dbgs () << " Is already predicated: " << *I);
265
330
return false ;
331
+ }
266
332
267
333
// Check for any dependencies on Head instructions.
268
334
if (!InstrDependenciesAllowIfConv (&(*I)))
@@ -271,6 +337,24 @@ bool SSAIfConv::canPredicateInstrs(MachineBasicBlock *MBB) {
271
337
return true ;
272
338
}
273
339
340
+ // Apply predicate to all instructions in the machine block.
341
+ void SSAIfConv::PredicateBlock (MachineBasicBlock *MBB, bool ReversePredicate) {
342
+ auto Condition = Cond;
343
+ if (ReversePredicate) {
344
+ bool CanRevCond = !TII->reverseBranchCondition (Condition);
345
+ assert (CanRevCond && " Reversed predicate is not supported" );
346
+ (void )CanRevCond;
347
+ }
348
+ // Terminators don't need to be predicated as they will be removed.
349
+ for (MachineBasicBlock::iterator I = MBB->begin (),
350
+ E = MBB->getFirstTerminator ();
351
+ I != E; ++I) {
352
+ if (I->isDebugInstr ())
353
+ continue ;
354
+ TII->PredicateInstruction (*I, Condition);
355
+ }
356
+ }
357
+
274
358
// / Find an insertion point in Head for the speculated instructions. The
275
359
// / insertion point must be:
276
360
// /
@@ -349,7 +433,7 @@ bool SSAIfConv::findInsertionPoint() {
349
433
// / canConvertIf - analyze the sub-cfg rooted in MBB, and return true if it is
350
434
// / a potential candidate for if-conversion. Fill out the internal state.
351
435
// /
352
- bool SSAIfConv::canConvertIf (MachineBasicBlock *MBB) {
436
+ bool SSAIfConv::canConvertIf (MachineBasicBlock *MBB, bool Predicate ) {
353
437
Head = MBB;
354
438
TBB = FBB = Tail = nullptr ;
355
439
@@ -389,17 +473,21 @@ bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB) {
389
473
<< printMBBReference (*Tail) << ' \n ' );
390
474
}
391
475
476
+ // This is a triangle or a diamond.
477
+ // Skip if we cannot predicate and there are no phis skip as there must be
478
+ // side effects that can only be handled with predication.
479
+ if (!Predicate && (Tail->empty () || !Tail->front ().isPHI ())) {
480
+ LLVM_DEBUG (dbgs () << " No phis in tail.\n " );
481
+ return false ;
482
+ }
483
+
392
484
// The branch we're looking to eliminate must be analyzable.
393
485
Cond.clear ();
394
486
if (TII->analyzeBranch (*Head, TBB, FBB, Cond)) {
395
487
LLVM_DEBUG (dbgs () << " Branch not analyzable.\n " );
396
488
return false ;
397
489
}
398
490
399
- if (!Predicate.canConvertIf (Tail)) {
400
- return false ;
401
- }
402
-
403
491
// This is weird, probably some sort of degenerate CFG.
404
492
if (!TBB) {
405
493
LLVM_DEBUG (dbgs () << " analyzeBranch didn't find conditional branch.\n " );
@@ -447,9 +535,17 @@ bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB) {
447
535
// Check that the conditional instructions can be speculated.
448
536
InsertAfter.clear ();
449
537
ClobberedRegUnits.reset ();
450
- for (MachineBasicBlock *MBB : {TBB, FBB})
451
- if (MBB != Tail && !canPredicateInstrs (MBB ))
538
+ if (Predicate) {
539
+ if (TBB != Tail && !canPredicateInstrs (TBB ))
452
540
return false ;
541
+ if (FBB != Tail && !canPredicateInstrs (FBB))
542
+ return false ;
543
+ } else {
544
+ if (TBB != Tail && !canSpeculateInstrs (TBB))
545
+ return false ;
546
+ if (FBB != Tail && !canSpeculateInstrs (FBB))
547
+ return false ;
548
+ }
453
549
454
550
// Try to find a valid insertion point for the speculated instructions in the
455
551
// head basic block.
@@ -582,7 +678,8 @@ void SSAIfConv::rewritePHIOperands() {
582
678
// /
583
679
// / Any basic blocks that need to be erased will be added to RemoveBlocks.
584
680
// /
585
- void SSAIfConv::convertIf (SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks) {
681
+ void SSAIfConv::convertIf (SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks,
682
+ bool Predicate) {
586
683
assert (Head && Tail && TBB && FBB && " Call canConvertIf first." );
587
684
588
685
// Update statistics.
@@ -592,15 +689,16 @@ void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks) {
592
689
++NumDiamondsConv;
593
690
594
691
// Move all instructions into Head, except for the terminators.
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
- }
692
+ if (TBB != Tail) {
693
+ if (Predicate)
694
+ PredicateBlock (TBB, /* ReversePredicate=*/ false );
695
+ Head->splice (InsertionPoint, TBB, TBB->begin (), TBB->getFirstTerminator ());
696
+ }
697
+ if (FBB != Tail) {
698
+ if (Predicate)
699
+ PredicateBlock (FBB, /* ReversePredicate=*/ true );
700
+ Head->splice (InsertionPoint, FBB, FBB->begin (), FBB->getFirstTerminator ());
602
701
}
603
-
604
702
// Are there extra Tail predecessors?
605
703
bool ExtraPreds = Tail->pred_size () != 2 ;
606
704
if (ExtraPreds)
@@ -764,46 +862,6 @@ template <typename Remark> Remark &operator<<(Remark &R, Cycles C) {
764
862
}
765
863
} // anonymous namespace
766
864
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
-
807
865
// / Apply cost model and heuristics to the if-conversion in IfConv.
808
866
// / Return true if the conversion is a good idea.
809
867
// /
@@ -1037,8 +1095,7 @@ bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) {
1037
1095
MinInstr = nullptr ;
1038
1096
1039
1097
bool Changed = false ;
1040
- SpeculateStrategy Speculate;
1041
- SSAIfConv IfConv (Speculate, MF);
1098
+ SSAIfConv IfConv (MF);
1042
1099
1043
1100
// Visit blocks in dominator tree post-order. The post-order enables nested
1044
1101
// if-conversion in a single pass. The tryConvertIf() function may erase
@@ -1100,48 +1157,6 @@ void EarlyIfPredicator::getAnalysisUsage(AnalysisUsage &AU) const {
1100
1157
MachineFunctionPass::getAnalysisUsage (AU);
1101
1158
}
1102
1159
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);
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
-
1145
1160
// / Apply the target heuristic to decide if the transformation is profitable.
1146
1161
bool EarlyIfPredicator::shouldConvertIf (SSAIfConv &IfConv) {
1147
1162
auto TrueProbability = MBPI->getEdgeProbability (IfConv.Head , IfConv.TBB );
@@ -1186,10 +1201,11 @@ bool EarlyIfPredicator::shouldConvertIf(SSAIfConv &IfConv) {
1186
1201
bool EarlyIfPredicator::tryConvertIf (SSAIfConv &IfConv,
1187
1202
MachineBasicBlock *MBB) {
1188
1203
bool Changed = false ;
1189
- while (IfConv.canConvertIf (MBB) && shouldConvertIf (IfConv)) {
1204
+ while (IfConv.canConvertIf (MBB, /* Predicate=*/ true ) &&
1205
+ shouldConvertIf (IfConv)) {
1190
1206
// If-convert MBB and update analyses.
1191
1207
SmallVector<MachineBasicBlock *, 4 > RemoveBlocks;
1192
- IfConv.convertIf (RemoveBlocks);
1208
+ IfConv.convertIf (RemoveBlocks, /* Predicate= */ true );
1193
1209
Changed = true ;
1194
1210
updateDomTree (DomTree, IfConv, RemoveBlocks);
1195
1211
for (MachineBasicBlock *MBB : RemoveBlocks)
@@ -1215,8 +1231,7 @@ bool EarlyIfPredicator::runOnMachineFunction(MachineFunction &MF) {
1215
1231
MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI ();
1216
1232
1217
1233
bool Changed = false ;
1218
- PredicatorStrategy Predicate (TII);
1219
- SSAIfConv IfConv (Predicate, MF);
1234
+ SSAIfConv IfConv (MF);
1220
1235
1221
1236
// Visit blocks in dominator tree post-order. The post-order enables nested
1222
1237
// if-conversion in a single pass. The tryConvertIf() function may erase
0 commit comments