llvm.org GIT mirror llvm / b20b851
Part one of switching to using a more sane heuristic for determining if-conversion profitability. Rather than having arbitrary cutoffs, actually try to cost model the conversion. For now, the constants are tuned to more or less match our existing behavior, but these will be changed to reflect realistic values as this work proceeds. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@114973 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 10 years ago
7 changed file(s) with 46 addition(s) and 55 deletion(s). Raw diff Collapse all Expand all
303303 }
304304
305305 /// isProfitableToIfCvt - Return true if it's profitable to first "NumInstrs"
306 /// of the specified basic block.
306 /// of the specified basic block, where the probability of the instructions
307 /// being executed is given by Probability.
307308 virtual
308 bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs) const {
309 bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs,
310 float Probability) const {
309311 return false;
310312 }
311313
312314 /// isProfitableToIfCvt - Second variant of isProfitableToIfCvt, this one
313315 /// checks for the case where two basic blocks from true and false path
314316 /// of a if-then-else (diamond) are predicated on mutally exclusive
315 /// predicates.
317 /// predicates, where the probability of the true path being taken is given
318 /// by Probability.
316319 virtual bool
317320 isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumTInstrs,
318 MachineBasicBlock &FMBB, unsigned NumFInstrs) const {
321 MachineBasicBlock &FMBB, unsigned NumFInstrs,
322 float Probability) const {
319323 return false;
320324 }
321325
322326 /// isProfitableToDupForIfCvt - Return true if it's profitable for
323327 /// if-converter to duplicate a specific number of instructions in the
324 /// specified MBB to enable if-conversion.
328 /// specified MBB to enable if-conversion, where the probability of the
329 /// instructions being executed is given by Probability.
325330 virtual bool
326 isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs) const {
331 isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs,
332 float Probability) const {
327333 return false;
328334 }
329335
190190 void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true);
191191
192192 bool MeetIfcvtSizeLimit(MachineBasicBlock &BB, unsigned Size) const {
193 return Size > 0 && TII->isProfitableToIfCvt(BB, Size);
193 return Size > 0 && TII->isProfitableToIfCvt(BB, Size, 0.5);
194194 }
195195
196196 bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB, unsigned TSize,
197197 MachineBasicBlock &FBB, unsigned FSize) const {
198198 return TSize > 0 && FSize > 0 &&
199 TII->isProfitableToIfCvt(TBB, TSize, FBB, FSize);
199 TII->isProfitableToIfCvt(TBB, TSize, FBB, FSize, 0.5);
200200 }
201201
202202 // blockAlwaysFallThrough - Block ends without a terminator.
443443
444444 if (TrueBBI.BB->pred_size() > 1) {
445445 if (TrueBBI.CannotBeCopied ||
446 !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize))
446 !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize, 0.5))
447447 return false;
448448 Dups = TrueBBI.NonPredSize;
449449 }
480480 ++Size;
481481 }
482482 }
483 if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size))
483 if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, 0.5))
484484 return false;
485485 Dups = Size;
486486 }
11941194 return false;
11951195 }
11961196
1197 bool ARMBaseInstrInfo::
1198 isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs) const {
1197 bool ARMBaseInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
1198 unsigned NumInstrs,
1199 float Probability) const {
11991200 if (!NumInstrs)
12001201 return false;
1201 if (Subtarget.getCPUString() == "generic")
1202 // Generic (and overly aggressive) if-conversion limits for testing.
1203 return NumInstrs <= 10;
1204 else if (Subtarget.hasV7Ops())
1205 return NumInstrs <= 3;
1206 return NumInstrs <= 2;
1202
1203 // Attempt to estimate the relative costs of predication versus branching.
1204 float UnpredCost = Probability * NumInstrs;
1205 UnpredCost += 2.0; // FIXME: Should model a misprediction cost.
1206
1207 float PredCost = NumInstrs;
1208
1209 return PredCost < UnpredCost;
1210
12071211 }
12081212
12091213 bool ARMBaseInstrInfo::
12101214 isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,
1211 MachineBasicBlock &FMBB, unsigned NumF) const {
1212 return NumT && NumF && NumT <= 2 && NumF <= 2;
1215 MachineBasicBlock &FMBB, unsigned NumF,
1216 float Probability) const {
1217 if (!NumT || !NumF)
1218 return false;
1219
1220 // Attempt to estimate the relative costs of predication versus branching.
1221 float UnpredCost = Probability * NumT + (1.0 - Probability) * NumF;
1222 UnpredCost += 2.0; // FIXME: Should model a misprediction cost.
1223
1224 float PredCost = NumT + NumF;
1225
1226 return PredCost < UnpredCost;
12131227 }
12141228
12151229 /// getInstrPredicate - If instruction is predicated, returns its predicate
311311 const MachineFunction &MF) const;
312312
313313 virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
314 unsigned NumInstrs) const;
314 unsigned NumInstrs, float Prob) const;
315315
316316 virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,unsigned NumT,
317 MachineBasicBlock &FMBB,unsigned NumF) const;
317 MachineBasicBlock &FMBB,unsigned NumF,
318 float Probability) const;
318319
319320 virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
320 unsigned NumInstrs) const {
321 unsigned NumInstrs,
322 float Probability) const {
321323 return NumInstrs && NumInstrs == 1;
322324 }
323325
2727
2828 using namespace llvm;
2929
30 static cl::opt
31 IfCvtLimit("thumb2-ifcvt-limit", cl::Hidden,
32 cl::desc("Thumb2 if-conversion limit (default 3)"),
33 cl::init(3));
34
35 static cl::opt
36 IfCvtDiamondLimit("thumb2-ifcvt-diamond-limit", cl::Hidden,
37 cl::desc("Thumb2 diamond if-conversion limit (default 3)"),
38 cl::init(3));
39
4030 Thumb2InstrInfo::Thumb2InstrInfo(const ARMSubtarget &STI)
4131 : ARMBaseInstrInfo(STI), RI(*this, STI) {
4232 }
10292 MachineBasicBlock::iterator MBBI) const {
10393 unsigned PredReg = 0;
10494 return llvm::getITInstrPredicate(MBBI, PredReg) == ARMCC::AL;
105 }
106
107 bool Thumb2InstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
108 unsigned NumInstrs) const {
109 return NumInstrs && NumInstrs <= IfCvtLimit;
110 }
111
112 bool Thumb2InstrInfo::
113 isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,
114 MachineBasicBlock &FMBB, unsigned NumF) const {
115 // FIXME: Catch optimization such as:
116 // r0 = movne
117 // r0 = moveq
118 return NumT && NumF &&
119 NumT <= (IfCvtDiamondLimit) && NumF <= (IfCvtDiamondLimit);
12095 }
12196
12297 void Thumb2InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
3636
3737 bool isLegalToSplitMBBAt(MachineBasicBlock &MBB,
3838 MachineBasicBlock::iterator MBBI) const;
39
40 bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs) const;
41
42 bool isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumTInstrs,
43 MachineBasicBlock &FMBB, unsigned NumFInstrs) const;
4439
4540 void copyPhysReg(MachineBasicBlock &MBB,
4641 MachineBasicBlock::iterator I, DebugLoc DL,
626626 ; in a register, and it shouldn't require any reloads here.
627627
628628 ; CHECK: @ %bb24
629 ; CHECK-NEXT: @ in Loop: Header=BB1_1 Depth=1
630 ; CHECK-NEXT: sub{{.*}} [[REGISTER:(r[0-9]+)|(lr)]], #1
629 ; CHECK: subs{{.*}} [[REGISTER:(r[0-9]+)|(lr)]], #1
631630 ; CHECK-NEXT: bne.w
632631
633632 %92 = icmp eq i32 %tmp81, %indvar78 ; [#uses=1]