llvm.org GIT mirror llvm / e3cc84a
Thread the determination of branch prediction hit rates back through the if-conversion heuristic APIs. For now, stick with a constant estimate of 90% (branch predictors are good!), but we might find that we want to provide more nuanced estimates in the future. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@115364 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 10 years ago
6 changed file(s) with 67 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
304304
305305 /// isProfitableToIfCvt - Return true if it's profitable to first "NumInstrs"
306306 /// of the specified basic block, where the probability of the instructions
307 /// being executed is given by Probability.
307 /// being executed is given by Probability, and Confidence is a measure
308 /// of our confidence that it will be properly predicted.
308309 virtual
309310 bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs,
310 float Probability) const {
311 float Probability, float Confidence) const {
311312 return false;
312313 }
313314
315316 /// checks for the case where two basic blocks from true and false path
316317 /// of a if-then-else (diamond) are predicated on mutally exclusive
317318 /// predicates, where the probability of the true path being taken is given
318 /// by Probability.
319 /// by Probability, and Confidence is a measure of our confidence that it
320 /// will be properly predicted.
319321 virtual bool
320322 isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumTInstrs,
321323 MachineBasicBlock &FMBB, unsigned NumFInstrs,
322 float Probability) const {
324 float Probability, float Confidence) const {
323325 return false;
324326 }
325327
326328 /// isProfitableToDupForIfCvt - Return true if it's profitable for
327329 /// if-converter to duplicate a specific number of instructions in the
328330 /// specified MBB to enable if-conversion, where the probability of the
329 /// instructions being executed is given by Probability.
331 /// instructions being executed is given by Probability, and Confidence is
332 /// a measure of our confidence that it will be properly predicted.
330333 virtual bool
331334 isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs,
332 float Probability) const {
335 float Probability, float Confidence) const {
333336 return false;
334337 }
335338
169169
170170 private:
171171 bool ReverseBranchCondition(BBInfo &BBI);
172 bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups, float Prediction) const;
172 bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
173 float Prediction, float Confidence) const;
173174 bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
174 bool FalseBranch, unsigned &Dups) const;
175 bool FalseBranch, unsigned &Dups,
176 float Prediction, float Confidence) const;
175177 bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
176178 unsigned &Dups1, unsigned &Dups2) const;
177179 void ScanInstructions(BBInfo &BBI);
197199 void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true);
198200
199201 bool MeetIfcvtSizeLimit(MachineBasicBlock &BB, unsigned Size,
200 float Prediction) const {
201 return Size > 0 && TII->isProfitableToIfCvt(BB, Size, Prediction);
202 float Prediction, float Confidence) const {
203 return Size > 0 && TII->isProfitableToIfCvt(BB, Size,
204 Prediction, Confidence);
202205 }
203206
204207 bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB, unsigned TSize,
205208 MachineBasicBlock &FBB, unsigned FSize,
206 float Prediction) const {
209 float Prediction, float Confidence) const {
207210 return TSize > 0 && FSize > 0 &&
208 TII->isProfitableToIfCvt(TBB, TSize, FBB, FSize, Prediction);
211 TII->isProfitableToIfCvt(TBB, TSize, FBB, FSize,
212 Prediction, Confidence);
209213 }
210214
211215 // blockAlwaysFallThrough - Block ends without a terminator.
444448 /// number of instructions that the ifcvt would need to duplicate if performed
445449 /// in Dups.
446450 bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
447 float Prediction) const {
451 float Prediction, float Confidence) const {
448452 Dups = 0;
449453 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
450454 return false;
455459 if (TrueBBI.BB->pred_size() > 1) {
456460 if (TrueBBI.CannotBeCopied ||
457461 !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize,
458 Prediction))
462 Prediction, Confidence))
459463 return false;
460464 Dups = TrueBBI.NonPredSize;
461465 }
470474 /// returns the number of instructions that the ifcvt would need to duplicate
471475 /// if performed in 'Dups'.
472476 bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
473 bool FalseBranch, unsigned &Dups) const {
477 bool FalseBranch, unsigned &Dups,
478 float Prediction, float Confidence) const {
474479 Dups = 0;
475480 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
476481 return false;
492497 ++Size;
493498 }
494499 }
495 if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, 0.5))
500 if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size,
501 Prediction, Confidence))
496502 return false;
497503 Dups = Size;
498504 }
785791 // General heuristics are:
786792 // - backedge -> 90% taken
787793 // - early exit -> 20% taken
794 // - branch predictor confidence -> 90%
788795 float Prediction = 0.5f;
796 float Confidence = 0.9f;
789797 MachineLoop *Loop = MLI->getLoopFor(BB);
790798 if (Loop) {
791799 if (TrueBBI.BB == Loop->getHeader())
804812 if (CanRevCond && ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2) &&
805813 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize - (Dups + Dups2),
806814 *FalseBBI.BB, FalseBBI.NonPredSize - (Dups + Dups2),
807 Prediction) &&
815 Prediction, Confidence) &&
808816 FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
809817 FeasibilityAnalysis(FalseBBI, RevCond)) {
810818 // Diamond:
820828 Enqueued = true;
821829 }
822830
823 if (ValidTriangle(TrueBBI, FalseBBI, false, Dups) &&
824 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize, Prediction) &&
831 if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction, Confidence) &&
832 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize,
833 Prediction, Confidence) &&
825834 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) {
826835 // Triangle:
827836 // EBB
834843 Enqueued = true;
835844 }
836845
837 if (ValidTriangle(TrueBBI, FalseBBI, true, Dups) &&
838 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize, Prediction) &&
846 if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction, Confidence) &&
847 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize,
848 Prediction, Confidence) &&
839849 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) {
840850 Tokens.push_back(new IfcvtToken(BBI, ICTriangleRev, TNeedSub, Dups));
841851 Enqueued = true;
842852 }
843853
844 if (ValidSimple(TrueBBI, Dups, Prediction) &&
845 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize, Prediction) &&
854 if (ValidSimple(TrueBBI, Dups, Prediction, Confidence) &&
855 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize,
856 Prediction, Confidence) &&
846857 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
847858 // Simple (split, no rejoin):
848859 // EBB
857868
858869 if (CanRevCond) {
859870 // Try the other path...
860 if (ValidTriangle(FalseBBI, TrueBBI, false, Dups) &&
861 MeetIfcvtSizeLimit(*FalseBBI.BB, FalseBBI.NonPredSize,1.0-Prediction) &&
871 if (ValidTriangle(FalseBBI, TrueBBI, false, Dups,
872 1.0-Prediction, Confidence) &&
873 MeetIfcvtSizeLimit(*FalseBBI.BB, FalseBBI.NonPredSize,
874 1.0-Prediction, Confidence) &&
862875 FeasibilityAnalysis(FalseBBI, RevCond, true)) {
863876 Tokens.push_back(new IfcvtToken(BBI, ICTriangleFalse, FNeedSub, Dups));
864877 Enqueued = true;
865878 }
866879
867 if (ValidTriangle(FalseBBI, TrueBBI, true, Dups) &&
868 MeetIfcvtSizeLimit(*FalseBBI.BB, FalseBBI.NonPredSize,1.0-Prediction) &&
880 if (ValidTriangle(FalseBBI, TrueBBI, true, Dups,
881 1.0-Prediction, Confidence) &&
882 MeetIfcvtSizeLimit(*FalseBBI.BB, FalseBBI.NonPredSize,
883 1.0-Prediction, Confidence) &&
869884 FeasibilityAnalysis(FalseBBI, RevCond, true, true)) {
870885 Tokens.push_back(new IfcvtToken(BBI, ICTriangleFRev, FNeedSub, Dups));
871886 Enqueued = true;
872887 }
873888
874 if (ValidSimple(FalseBBI, Dups, 1.0-Prediction) &&
875 MeetIfcvtSizeLimit(*FalseBBI.BB, FalseBBI.NonPredSize,1.0-Prediction) &&
889 if (ValidSimple(FalseBBI, Dups, 1.0-Prediction, Confidence) &&
890 MeetIfcvtSizeLimit(*FalseBBI.BB, FalseBBI.NonPredSize,
891 1.0-Prediction, Confidence) &&
876892 FeasibilityAnalysis(FalseBBI, RevCond)) {
877893 Tokens.push_back(new IfcvtToken(BBI, ICSimpleFalse, FNeedSub, Dups));
878894 Enqueued = true;
12001200
12011201 bool ARMBaseInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
12021202 unsigned NumInstrs,
1203 float Probability) const {
1203 float Probability,
1204 float Confidence) const {
12041205 if (!NumInstrs)
12051206 return false;
12061207
12171218 // Attempt to estimate the relative costs of predication versus branching.
12181219 float UnpredCost = Probability * NumInstrs;
12191220 UnpredCost += 1.0; // The branch itself
1220 UnpredCost += 0.1 * Subtarget.getMispredictionPenalty();
1221 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
12211222
12221223 float PredCost = NumInstrs;
12231224
12281229 bool ARMBaseInstrInfo::
12291230 isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,
12301231 MachineBasicBlock &FMBB, unsigned NumF,
1231 float Probability) const {
1232 float Probability, float Confidence) const {
12321233 // Use old-style if-conversion heuristics
12331234 if (OldARMIfCvt) {
12341235 return NumT && NumF && NumT <= 2 && NumF <= 2;
12401241 // Attempt to estimate the relative costs of predication versus branching.
12411242 float UnpredCost = Probability * NumT + (1.0 - Probability) * NumF;
12421243 UnpredCost += 1.0; // The branch itself
1243 UnpredCost += 0.1 * Subtarget.getMispredictionPenalty();
1244 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
12441245
12451246 float PredCost = NumT + NumF;
12461247
311311 const MachineFunction &MF) const;
312312
313313 virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
314 unsigned NumInstrs, float Prob) const;
314 unsigned NumInstrs,
315 float Prob, float Confidence) const;
315316
316317 virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,unsigned NumT,
317318 MachineBasicBlock &FMBB,unsigned NumF,
318 float Probability) const;
319 float Probability, float Confidence) const;
319320
320321 virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
321322 unsigned NumInstrs,
322 float Probability) const {
323 float Probability,
324 float Confidence) const {
323325 return NumInstrs && NumInstrs == 1;
324326 }
325327
4343
4444 bool Thumb2InstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
4545 unsigned NumInstrs,
46 float Prediction) const {
46 float Prediction,
47 float Confidence) const {
4748 if (!OldT2IfCvt)
48 return ARMBaseInstrInfo::isProfitableToIfCvt(MBB, NumInstrs, Prediction);
49 return ARMBaseInstrInfo::isProfitableToIfCvt(MBB, NumInstrs,
50 Prediction, Confidence);
4951 return NumInstrs && NumInstrs <= 3;
5052 }
5153
5254 bool Thumb2InstrInfo::
5355 isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,
5456 MachineBasicBlock &FMBB, unsigned NumF,
55 float Prediction) const {
57 float Prediction, float Confidence) const {
5658 if (!OldT2IfCvt)
5759 return ARMBaseInstrInfo::isProfitableToIfCvt(TMBB, NumT,
58 FMBB, NumF, Prediction);
60 FMBB, NumF,
61 Prediction, Confidence);
5962
6063 // FIXME: Catch optimization such as:
6164 // r0 = movne
3838 MachineBasicBlock::iterator MBBI) const;
3939
4040 bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs,
41 float Prediction) const;
41 float Prediction, float Confidence) const;
4242 bool isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumTInstrs,
4343 MachineBasicBlock &FMBB, unsigned NumFInstrs,
44 float Prediction) const;
44 float Prediction, float Confidence) const;
4545
4646 void copyPhysReg(MachineBasicBlock &MBB,
4747 MachineBasicBlock::iterator I, DebugLoc DL,