@@ -285,14 +285,113 @@ namespace {
285
285
Prediction);
286
286
}
287
287
288
- bool MeetIfcvtSizeLimit (MachineBasicBlock &TBB,
289
- unsigned TCycle, unsigned TExtra,
290
- MachineBasicBlock &FBB,
291
- unsigned FCycle, unsigned FExtra,
292
- BranchProbability Prediction) const {
293
- return TCycle > 0 && FCycle > 0 &&
294
- TII->isProfitableToIfCvt (TBB, TCycle, TExtra, FBB, FCycle, FExtra,
295
- Prediction);
288
+ bool MeetIfcvtSizeLimit (BBInfo &TBBInfo, BBInfo &FBBInfo,
289
+ MachineBasicBlock &CommBB, unsigned Dups,
290
+ BranchProbability Prediction, bool Forked) const {
291
+ const MachineFunction &MF = *TBBInfo.BB ->getParent ();
292
+ if (MF.getFunction ().hasMinSize ()) {
293
+ MachineBasicBlock::iterator TIB = TBBInfo.BB ->begin ();
294
+ MachineBasicBlock::iterator FIB = FBBInfo.BB ->begin ();
295
+ MachineBasicBlock::iterator TIE = TBBInfo.BB ->end ();
296
+ MachineBasicBlock::iterator FIE = FBBInfo.BB ->end ();
297
+
298
+ unsigned Dups1, Dups2;
299
+ if (!CountDuplicatedInstructions (TIB, FIB, TIE, FIE, Dups1, Dups2,
300
+ *TBBInfo.BB , *FBBInfo.BB ,
301
+ /* SkipUnconditionalBranches*/ true ))
302
+ llvm_unreachable (" should already have been checked by ValidDiamond" );
303
+
304
+ unsigned BranchBytes = 0 ;
305
+ unsigned CommonBytes = 0 ;
306
+
307
+ // Count common instructions at the start of the true and false blocks.
308
+ for (auto &I : make_range (TBBInfo.BB ->begin (), TIB)) {
309
+ LLVM_DEBUG (dbgs () << " Common inst: " << I);
310
+ CommonBytes += TII->getInstSizeInBytes (I);
311
+ }
312
+ for (auto &I : make_range (FBBInfo.BB ->begin (), FIB)) {
313
+ LLVM_DEBUG (dbgs () << " Common inst: " << I);
314
+ CommonBytes += TII->getInstSizeInBytes (I);
315
+ }
316
+
317
+ // Count instructions at the end of the true and false blocks, after
318
+ // the ones we plan to predicate. Analyzable branches will be removed
319
+ // (unless this is a forked diamond), and all other instructions are
320
+ // common between the two blocks.
321
+ for (auto &I : make_range (TIE, TBBInfo.BB ->end ())) {
322
+ if (I.isBranch () && TBBInfo.IsBrAnalyzable && !Forked) {
323
+ LLVM_DEBUG (dbgs () << " Saving branch: " << I);
324
+ BranchBytes += TII->predictBranchSizeForIfCvt (I);
325
+ } else {
326
+ LLVM_DEBUG (dbgs () << " Common inst: " << I);
327
+ CommonBytes += TII->getInstSizeInBytes (I);
328
+ }
329
+ }
330
+ for (auto &I : make_range (FIE, FBBInfo.BB ->end ())) {
331
+ if (I.isBranch () && FBBInfo.IsBrAnalyzable && !Forked) {
332
+ LLVM_DEBUG (dbgs () << " Saving branch: " << I);
333
+ BranchBytes += TII->predictBranchSizeForIfCvt (I);
334
+ } else {
335
+ LLVM_DEBUG (dbgs () << " Common inst: " << I);
336
+ CommonBytes += TII->getInstSizeInBytes (I);
337
+ }
338
+ }
339
+ for (auto &I : CommBB.terminators ()) {
340
+ if (I.isBranch ()) {
341
+ LLVM_DEBUG (dbgs () << " Saving branch: " << I);
342
+ BranchBytes += TII->predictBranchSizeForIfCvt (I);
343
+ }
344
+ }
345
+
346
+ // The common instructions in one branch will be eliminated, halving
347
+ // their code size.
348
+ CommonBytes /= 2 ;
349
+
350
+ // Count the instructions which we need to predicate.
351
+ unsigned NumPredicatedInstructions = 0 ;
352
+ for (auto &I : make_range (TIB, TIE)) {
353
+ if (!I.isDebugInstr ()) {
354
+ LLVM_DEBUG (dbgs () << " Predicating: " << I);
355
+ NumPredicatedInstructions++;
356
+ }
357
+ }
358
+ for (auto &I : make_range (FIB, FIE)) {
359
+ if (!I.isDebugInstr ()) {
360
+ LLVM_DEBUG (dbgs () << " Predicating: " << I);
361
+ NumPredicatedInstructions++;
362
+ }
363
+ }
364
+
365
+ // Even though we're optimising for size at the expense of performance,
366
+ // avoid creating really long predicated blocks.
367
+ if (NumPredicatedInstructions > 15 )
368
+ return false ;
369
+
370
+ // Some targets (e.g. Thumb2) need to insert extra instructions to
371
+ // start predicated blocks.
372
+ unsigned ExtraPredicateBytes = TII->extraSizeToPredicateInstructions (
373
+ MF, NumPredicatedInstructions);
374
+
375
+ LLVM_DEBUG (dbgs () << " MeetIfcvtSizeLimit(BranchBytes=" << BranchBytes
376
+ << " , CommonBytes=" << CommonBytes
377
+ << " , NumPredicatedInstructions="
378
+ << NumPredicatedInstructions
379
+ << " , ExtraPredicateBytes=" << ExtraPredicateBytes
380
+ << " )\n " );
381
+ return (BranchBytes + CommonBytes) > ExtraPredicateBytes;
382
+ } else {
383
+ unsigned TCycle = TBBInfo.NonPredSize + TBBInfo.ExtraCost - Dups;
384
+ unsigned FCycle = FBBInfo.NonPredSize + FBBInfo.ExtraCost - Dups;
385
+ bool Res = TCycle > 0 && FCycle > 0 &&
386
+ TII->isProfitableToIfCvt (
387
+ *TBBInfo.BB , TCycle, TBBInfo.ExtraCost2 , *FBBInfo.BB ,
388
+ FCycle, FBBInfo.ExtraCost2 , Prediction);
389
+ LLVM_DEBUG (dbgs () << " MeetIfcvtSizeLimit(TCycle=" << TCycle
390
+ << " , FCycle=" << FCycle
391
+ << " , TExtra=" << TBBInfo.ExtraCost2 << " , FExtra="
392
+ << FBBInfo.ExtraCost2 << " ) = " << Res << " \n " );
393
+ return Res;
394
+ }
296
395
}
297
396
298
397
// / Returns true if Block ends without a terminator.
@@ -842,6 +941,8 @@ bool IfConverter::ValidForkedDiamond(
842
941
843
942
TrueBBICalc.BB = TrueBBI.BB ;
844
943
FalseBBICalc.BB = FalseBBI.BB ;
944
+ TrueBBICalc.IsBrAnalyzable = TrueBBI.IsBrAnalyzable ;
945
+ FalseBBICalc.IsBrAnalyzable = FalseBBI.IsBrAnalyzable ;
845
946
if (!RescanInstructions (TIB, FIB, TIE, FIE, TrueBBICalc, FalseBBICalc))
846
947
return false ;
847
948
@@ -899,6 +1000,8 @@ bool IfConverter::ValidDiamond(
899
1000
900
1001
TrueBBICalc.BB = TrueBBI.BB ;
901
1002
FalseBBICalc.BB = FalseBBI.BB ;
1003
+ TrueBBICalc.IsBrAnalyzable = TrueBBI.IsBrAnalyzable ;
1004
+ FalseBBICalc.IsBrAnalyzable = FalseBBI.IsBrAnalyzable ;
902
1005
if (!RescanInstructions (TIB, FIB, TIE, FIE, TrueBBICalc, FalseBBICalc))
903
1006
return false ;
904
1007
// The size is used to decide whether to if-convert, and the shared portions
@@ -1186,13 +1289,9 @@ void IfConverter::AnalyzeBlock(
1186
1289
1187
1290
if (CanRevCond) {
1188
1291
BBInfo TrueBBICalc, FalseBBICalc;
1189
- auto feasibleDiamond = [&]() {
1190
- bool MeetsSize = MeetIfcvtSizeLimit (
1191
- *TrueBBI.BB , (TrueBBICalc.NonPredSize - (Dups + Dups2) +
1192
- TrueBBICalc.ExtraCost ), TrueBBICalc.ExtraCost2 ,
1193
- *FalseBBI.BB , (FalseBBICalc.NonPredSize - (Dups + Dups2) +
1194
- FalseBBICalc.ExtraCost ), FalseBBICalc.ExtraCost2 ,
1195
- Prediction);
1292
+ auto feasibleDiamond = [&](bool Forked) {
1293
+ bool MeetsSize = MeetIfcvtSizeLimit (TrueBBICalc, FalseBBICalc, *BB,
1294
+ Dups + Dups2, Prediction, Forked);
1196
1295
bool TrueFeasible = FeasibilityAnalysis (TrueBBI, BBI.BrCond ,
1197
1296
/* IsTriangle */ false , /* RevCond */ false ,
1198
1297
/* hasCommonTail */ true );
@@ -1204,7 +1303,7 @@ void IfConverter::AnalyzeBlock(
1204
1303
1205
1304
if (ValidDiamond (TrueBBI, FalseBBI, Dups, Dups2,
1206
1305
TrueBBICalc, FalseBBICalc)) {
1207
- if (feasibleDiamond ()) {
1306
+ if (feasibleDiamond (false )) {
1208
1307
// Diamond:
1209
1308
// EBB
1210
1309
// / \_
@@ -1220,7 +1319,7 @@ void IfConverter::AnalyzeBlock(
1220
1319
}
1221
1320
} else if (ValidForkedDiamond (TrueBBI, FalseBBI, Dups, Dups2,
1222
1321
TrueBBICalc, FalseBBICalc)) {
1223
- if (feasibleDiamond ()) {
1322
+ if (feasibleDiamond (true )) {
1224
1323
// ForkedDiamond:
1225
1324
// if TBB and FBB have a common tail that includes their conditional
1226
1325
// branch instructions, then we can If Convert this pattern.
0 commit comments