[llvm-commits] [llvm] r115364 - in /llvm/trunk: include/llvm/Target/TargetInstrInfo.h lib/CodeGen/IfConversion.cpp lib/Target/ARM/ARMBaseInstrInfo.cpp lib/Target/ARM/ARMBaseInstrInfo.h lib/Target/ARM/Thumb2InstrInfo.cpp lib/Target/ARM/Thumb2InstrInfo.h
Evan Cheng
evan.cheng at apple.com
Fri Oct 1 15:54:29 PDT 2010
On Oct 1, 2010, at 3:45 PM, Owen Anderson wrote:
> Author: resistor
> Date: Fri Oct 1 17:45:50 2010
> New Revision: 115364
>
> URL: http://llvm.org/viewvc/llvm-project?rev=115364&view=rev
> Log:
> 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.
Should it be target specific?
Evan
>
> Modified:
> llvm/trunk/include/llvm/Target/TargetInstrInfo.h
> llvm/trunk/lib/CodeGen/IfConversion.cpp
> llvm/trunk/lib/Target/ARM/ARMBaseInstrInfo.cpp
> llvm/trunk/lib/Target/ARM/ARMBaseInstrInfo.h
> llvm/trunk/lib/Target/ARM/Thumb2InstrInfo.cpp
> llvm/trunk/lib/Target/ARM/Thumb2InstrInfo.h
>
> Modified: llvm/trunk/include/llvm/Target/TargetInstrInfo.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetInstrInfo.h?rev=115364&r1=115363&r2=115364&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/Target/TargetInstrInfo.h (original)
> +++ llvm/trunk/include/llvm/Target/TargetInstrInfo.h Fri Oct 1 17:45:50 2010
> @@ -305,10 +305,11 @@
>
> /// isProfitableToIfCvt - Return true if it's profitable to first "NumInstrs"
> /// of the specified basic block, where the probability of the instructions
> - /// being executed is given by Probability.
> + /// being executed is given by Probability, and Confidence is a measure
> + /// of our confidence that it will be properly predicted.
> virtual
> bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs,
> - float Probability) const {
> + float Probability, float Confidence) const {
> return false;
> }
>
> @@ -316,21 +317,23 @@
> /// checks for the case where two basic blocks from true and false path
> /// of a if-then-else (diamond) are predicated on mutally exclusive
> /// predicates, where the probability of the true path being taken is given
> - /// by Probability.
> + /// by Probability, and Confidence is a measure of our confidence that it
> + /// will be properly predicted.
> virtual bool
> isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumTInstrs,
> MachineBasicBlock &FMBB, unsigned NumFInstrs,
> - float Probability) const {
> + float Probability, float Confidence) const {
> return false;
> }
>
> /// isProfitableToDupForIfCvt - Return true if it's profitable for
> /// if-converter to duplicate a specific number of instructions in the
> /// specified MBB to enable if-conversion, where the probability of the
> - /// instructions being executed is given by Probability.
> + /// instructions being executed is given by Probability, and Confidence is
> + /// a measure of our confidence that it will be properly predicted.
> virtual bool
> isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs,
> - float Probability) const {
> + float Probability, float Confidence) const {
> return false;
> }
>
>
> Modified: llvm/trunk/lib/CodeGen/IfConversion.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/IfConversion.cpp?rev=115364&r1=115363&r2=115364&view=diff
> ==============================================================================
> --- llvm/trunk/lib/CodeGen/IfConversion.cpp (original)
> +++ llvm/trunk/lib/CodeGen/IfConversion.cpp Fri Oct 1 17:45:50 2010
> @@ -170,9 +170,11 @@
>
> private:
> bool ReverseBranchCondition(BBInfo &BBI);
> - bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups, float Prediction) const;
> + bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
> + float Prediction, float Confidence) const;
> bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
> - bool FalseBranch, unsigned &Dups) const;
> + bool FalseBranch, unsigned &Dups,
> + float Prediction, float Confidence) const;
> bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
> unsigned &Dups1, unsigned &Dups2) const;
> void ScanInstructions(BBInfo &BBI);
> @@ -198,15 +200,17 @@
> void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true);
>
> bool MeetIfcvtSizeLimit(MachineBasicBlock &BB, unsigned Size,
> - float Prediction) const {
> - return Size > 0 && TII->isProfitableToIfCvt(BB, Size, Prediction);
> + float Prediction, float Confidence) const {
> + return Size > 0 && TII->isProfitableToIfCvt(BB, Size,
> + Prediction, Confidence);
> }
>
> bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB, unsigned TSize,
> MachineBasicBlock &FBB, unsigned FSize,
> - float Prediction) const {
> + float Prediction, float Confidence) const {
> return TSize > 0 && FSize > 0 &&
> - TII->isProfitableToIfCvt(TBB, TSize, FBB, FSize, Prediction);
> + TII->isProfitableToIfCvt(TBB, TSize, FBB, FSize,
> + Prediction, Confidence);
> }
>
> // blockAlwaysFallThrough - Block ends without a terminator.
> @@ -445,7 +449,7 @@
> /// number of instructions that the ifcvt would need to duplicate if performed
> /// in Dups.
> bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
> - float Prediction) const {
> + float Prediction, float Confidence) const {
> Dups = 0;
> if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
> return false;
> @@ -456,7 +460,7 @@
> if (TrueBBI.BB->pred_size() > 1) {
> if (TrueBBI.CannotBeCopied ||
> !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize,
> - Prediction))
> + Prediction, Confidence))
> return false;
> Dups = TrueBBI.NonPredSize;
> }
> @@ -471,7 +475,8 @@
> /// returns the number of instructions that the ifcvt would need to duplicate
> /// if performed in 'Dups'.
> bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
> - bool FalseBranch, unsigned &Dups) const {
> + bool FalseBranch, unsigned &Dups,
> + float Prediction, float Confidence) const {
> Dups = 0;
> if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
> return false;
> @@ -493,7 +498,8 @@
> ++Size;
> }
> }
> - if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, 0.5))
> + if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size,
> + Prediction, Confidence))
> return false;
> Dups = Size;
> }
> @@ -786,7 +792,9 @@
> // General heuristics are:
> // - backedge -> 90% taken
> // - early exit -> 20% taken
> + // - branch predictor confidence -> 90%
> float Prediction = 0.5f;
> + float Confidence = 0.9f;
> MachineLoop *Loop = MLI->getLoopFor(BB);
> if (Loop) {
> if (TrueBBI.BB == Loop->getHeader())
> @@ -805,7 +813,7 @@
> if (CanRevCond && ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2) &&
> MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize - (Dups + Dups2),
> *FalseBBI.BB, FalseBBI.NonPredSize - (Dups + Dups2),
> - Prediction) &&
> + Prediction, Confidence) &&
> FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
> FeasibilityAnalysis(FalseBBI, RevCond)) {
> // Diamond:
> @@ -821,8 +829,9 @@
> Enqueued = true;
> }
>
> - if (ValidTriangle(TrueBBI, FalseBBI, false, Dups) &&
> - MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize, Prediction) &&
> + if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction, Confidence) &&
> + MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize,
> + Prediction, Confidence) &&
> FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) {
> // Triangle:
> // EBB
> @@ -835,15 +844,17 @@
> Enqueued = true;
> }
>
> - if (ValidTriangle(TrueBBI, FalseBBI, true, Dups) &&
> - MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize, Prediction) &&
> + if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction, Confidence) &&
> + MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize,
> + Prediction, Confidence) &&
> FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) {
> Tokens.push_back(new IfcvtToken(BBI, ICTriangleRev, TNeedSub, Dups));
> Enqueued = true;
> }
>
> - if (ValidSimple(TrueBBI, Dups, Prediction) &&
> - MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize, Prediction) &&
> + if (ValidSimple(TrueBBI, Dups, Prediction, Confidence) &&
> + MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize,
> + Prediction, Confidence) &&
> FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
> // Simple (split, no rejoin):
> // EBB
> @@ -858,22 +869,27 @@
>
> if (CanRevCond) {
> // Try the other path...
> - if (ValidTriangle(FalseBBI, TrueBBI, false, Dups) &&
> - MeetIfcvtSizeLimit(*FalseBBI.BB, FalseBBI.NonPredSize,1.0-Prediction) &&
> + if (ValidTriangle(FalseBBI, TrueBBI, false, Dups,
> + 1.0-Prediction, Confidence) &&
> + MeetIfcvtSizeLimit(*FalseBBI.BB, FalseBBI.NonPredSize,
> + 1.0-Prediction, Confidence) &&
> FeasibilityAnalysis(FalseBBI, RevCond, true)) {
> Tokens.push_back(new IfcvtToken(BBI, ICTriangleFalse, FNeedSub, Dups));
> Enqueued = true;
> }
>
> - if (ValidTriangle(FalseBBI, TrueBBI, true, Dups) &&
> - MeetIfcvtSizeLimit(*FalseBBI.BB, FalseBBI.NonPredSize,1.0-Prediction) &&
> + if (ValidTriangle(FalseBBI, TrueBBI, true, Dups,
> + 1.0-Prediction, Confidence) &&
> + MeetIfcvtSizeLimit(*FalseBBI.BB, FalseBBI.NonPredSize,
> + 1.0-Prediction, Confidence) &&
> FeasibilityAnalysis(FalseBBI, RevCond, true, true)) {
> Tokens.push_back(new IfcvtToken(BBI, ICTriangleFRev, FNeedSub, Dups));
> Enqueued = true;
> }
>
> - if (ValidSimple(FalseBBI, Dups, 1.0-Prediction) &&
> - MeetIfcvtSizeLimit(*FalseBBI.BB, FalseBBI.NonPredSize,1.0-Prediction) &&
> + if (ValidSimple(FalseBBI, Dups, 1.0-Prediction, Confidence) &&
> + MeetIfcvtSizeLimit(*FalseBBI.BB, FalseBBI.NonPredSize,
> + 1.0-Prediction, Confidence) &&
> FeasibilityAnalysis(FalseBBI, RevCond)) {
> Tokens.push_back(new IfcvtToken(BBI, ICSimpleFalse, FNeedSub, Dups));
> Enqueued = true;
>
> Modified: llvm/trunk/lib/Target/ARM/ARMBaseInstrInfo.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMBaseInstrInfo.cpp?rev=115364&r1=115363&r2=115364&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/ARM/ARMBaseInstrInfo.cpp (original)
> +++ llvm/trunk/lib/Target/ARM/ARMBaseInstrInfo.cpp Fri Oct 1 17:45:50 2010
> @@ -1201,7 +1201,8 @@
>
> bool ARMBaseInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
> unsigned NumInstrs,
> - float Probability) const {
> + float Probability,
> + float Confidence) const {
> if (!NumInstrs)
> return false;
>
> @@ -1218,7 +1219,7 @@
> // Attempt to estimate the relative costs of predication versus branching.
> float UnpredCost = Probability * NumInstrs;
> UnpredCost += 1.0; // The branch itself
> - UnpredCost += 0.1 * Subtarget.getMispredictionPenalty();
> + UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
>
> float PredCost = NumInstrs;
>
> @@ -1229,7 +1230,7 @@
> bool ARMBaseInstrInfo::
> isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,
> MachineBasicBlock &FMBB, unsigned NumF,
> - float Probability) const {
> + float Probability, float Confidence) const {
> // Use old-style if-conversion heuristics
> if (OldARMIfCvt) {
> return NumT && NumF && NumT <= 2 && NumF <= 2;
> @@ -1241,7 +1242,7 @@
> // Attempt to estimate the relative costs of predication versus branching.
> float UnpredCost = Probability * NumT + (1.0 - Probability) * NumF;
> UnpredCost += 1.0; // The branch itself
> - UnpredCost += 0.1 * Subtarget.getMispredictionPenalty();
> + UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
>
> float PredCost = NumT + NumF;
>
>
> Modified: llvm/trunk/lib/Target/ARM/ARMBaseInstrInfo.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMBaseInstrInfo.h?rev=115364&r1=115363&r2=115364&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/ARM/ARMBaseInstrInfo.h (original)
> +++ llvm/trunk/lib/Target/ARM/ARMBaseInstrInfo.h Fri Oct 1 17:45:50 2010
> @@ -312,15 +312,17 @@
> const MachineFunction &MF) const;
>
> virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
> - unsigned NumInstrs, float Prob) const;
> + unsigned NumInstrs,
> + float Prob, float Confidence) const;
>
> virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,unsigned NumT,
> MachineBasicBlock &FMBB,unsigned NumF,
> - float Probability) const;
> + float Probability, float Confidence) const;
>
> virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
> unsigned NumInstrs,
> - float Probability) const {
> + float Probability,
> + float Confidence) const {
> return NumInstrs && NumInstrs == 1;
> }
>
>
> Modified: llvm/trunk/lib/Target/ARM/Thumb2InstrInfo.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/Thumb2InstrInfo.cpp?rev=115364&r1=115363&r2=115364&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/ARM/Thumb2InstrInfo.cpp (original)
> +++ llvm/trunk/lib/Target/ARM/Thumb2InstrInfo.cpp Fri Oct 1 17:45:50 2010
> @@ -44,19 +44,22 @@
>
> bool Thumb2InstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
> unsigned NumInstrs,
> - float Prediction) const {
> + float Prediction,
> + float Confidence) const {
> if (!OldT2IfCvt)
> - return ARMBaseInstrInfo::isProfitableToIfCvt(MBB, NumInstrs, Prediction);
> + return ARMBaseInstrInfo::isProfitableToIfCvt(MBB, NumInstrs,
> + Prediction, Confidence);
> return NumInstrs && NumInstrs <= 3;
> }
>
> bool Thumb2InstrInfo::
> isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,
> MachineBasicBlock &FMBB, unsigned NumF,
> - float Prediction) const {
> + float Prediction, float Confidence) const {
> if (!OldT2IfCvt)
> return ARMBaseInstrInfo::isProfitableToIfCvt(TMBB, NumT,
> - FMBB, NumF, Prediction);
> + FMBB, NumF,
> + Prediction, Confidence);
>
> // FIXME: Catch optimization such as:
> // r0 = movne
>
> Modified: llvm/trunk/lib/Target/ARM/Thumb2InstrInfo.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/Thumb2InstrInfo.h?rev=115364&r1=115363&r2=115364&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/ARM/Thumb2InstrInfo.h (original)
> +++ llvm/trunk/lib/Target/ARM/Thumb2InstrInfo.h Fri Oct 1 17:45:50 2010
> @@ -39,10 +39,10 @@
> MachineBasicBlock::iterator MBBI) const;
>
> bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs,
> - float Prediction) const;
> + float Prediction, float Confidence) const;
> bool isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumTInstrs,
> MachineBasicBlock &FMBB, unsigned NumFInstrs,
> - float Prediction) const;
> + float Prediction, float Confidence) const;
>
> void copyPhysReg(MachineBasicBlock &MBB,
> MachineBasicBlock::iterator I, DebugLoc DL,
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list