Heuristics are in descending priority now. If we use one of them, skip the rest.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@136402 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jakub Staszak 2011-07-28 21:45:07 +00:00
parent f6c690019b
commit 7241caf7b8

View File

@ -134,13 +134,13 @@ public:
} }
// Return Heuristics // Return Heuristics
void calcReturnHeuristics(BasicBlock *BB); bool calcReturnHeuristics(BasicBlock *BB);
// Pointer Heuristics // Pointer Heuristics
void calcPointerHeuristics(BasicBlock *BB); bool calcPointerHeuristics(BasicBlock *BB);
// Loop Branch Heuristics // Loop Branch Heuristics
void calcLoopBranchHeuristics(BasicBlock *BB); bool calcLoopBranchHeuristics(BasicBlock *BB);
bool runOnFunction(Function &F); bool runOnFunction(Function &F);
}; };
@ -148,34 +148,38 @@ public:
// Calculate Edge Weights using "Return Heuristics". Predict a successor which // Calculate Edge Weights using "Return Heuristics". Predict a successor which
// leads directly to Return Instruction will not be taken. // leads directly to Return Instruction will not be taken.
void BranchProbabilityAnalysis::calcReturnHeuristics(BasicBlock *BB){ bool BranchProbabilityAnalysis::calcReturnHeuristics(BasicBlock *BB){
if (BB->getTerminator()->getNumSuccessors() == 1) if (BB->getTerminator()->getNumSuccessors() == 1)
return; return false;
bool Any = false;
for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
BasicBlock *Succ = *I; BasicBlock *Succ = *I;
if (isReturningBlock(Succ)) { if (isReturningBlock(Succ)) {
decEdgeWeight(BB, Succ); decEdgeWeight(BB, Succ);
Any = true;
} }
} }
return Any;
} }
// Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
// between two pointer or pointer and NULL will fail. // between two pointer or pointer and NULL will fail.
void BranchProbabilityAnalysis::calcPointerHeuristics(BasicBlock *BB) { bool BranchProbabilityAnalysis::calcPointerHeuristics(BasicBlock *BB) {
BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator()); BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
if (!BI || !BI->isConditional()) if (!BI || !BI->isConditional())
return; return false;
Value *Cond = BI->getCondition(); Value *Cond = BI->getCondition();
ICmpInst *CI = dyn_cast<ICmpInst>(Cond); ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
if (!CI || !CI->isEquality()) if (!CI || !CI->isEquality())
return; return false;
Value *LHS = CI->getOperand(0); Value *LHS = CI->getOperand(0);
if (!LHS->getType()->isPointerTy()) if (!LHS->getType()->isPointerTy())
return; return false;
assert(CI->getOperand(1)->getType()->isPointerTy()); assert(CI->getOperand(1)->getType()->isPointerTy());
@ -192,16 +196,17 @@ void BranchProbabilityAnalysis::calcPointerHeuristics(BasicBlock *BB) {
incEdgeWeight(BB, Taken); incEdgeWeight(BB, Taken);
decEdgeWeight(BB, NonTaken); decEdgeWeight(BB, NonTaken);
return true;
} }
// Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
// as taken, exiting edges as not-taken. // as taken, exiting edges as not-taken.
void BranchProbabilityAnalysis::calcLoopBranchHeuristics(BasicBlock *BB) { bool BranchProbabilityAnalysis::calcLoopBranchHeuristics(BasicBlock *BB) {
uint32_t numSuccs = BB->getTerminator()->getNumSuccessors(); uint32_t numSuccs = BB->getTerminator()->getNumSuccessors();
Loop *L = LI->getLoopFor(BB); Loop *L = LI->getLoopFor(BB);
if (!L) if (!L)
return; return false;
SmallVector<BasicBlock *, 8> BackEdges; SmallVector<BasicBlock *, 8> BackEdges;
SmallVector<BasicBlock *, 8> ExitingEdges; SmallVector<BasicBlock *, 8> ExitingEdges;
@ -256,6 +261,8 @@ void BranchProbabilityAnalysis::calcLoopBranchHeuristics(BasicBlock *BB) {
BP->setEdgeWeight(BB, Exiting, exitWeight); BP->setEdgeWeight(BB, Exiting, exitWeight);
} }
} }
return true;
} }
bool BranchProbabilityAnalysis::runOnFunction(Function &F) { bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
@ -264,12 +271,15 @@ bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
BasicBlock *BB = I++; BasicBlock *BB = I++;
// Only LBH uses setEdgeWeight method. // Only LBH uses setEdgeWeight method.
calcLoopBranchHeuristics(BB); if (calcLoopBranchHeuristics(BB))
continue;
// PH and RH use only incEdgeWeight and decEwdgeWeight methods to // PH and RH use only incEdgeWeight and decEwdgeWeight methods to
// not efface LBH results. // not efface LBH results.
if (calcReturnHeuristics(BB))
continue;
calcPointerHeuristics(BB); calcPointerHeuristics(BB);
calcReturnHeuristics(BB);
} }
return false; return false;