Skip to content

Commit 0480a8f

Browse files
author
Jakub Staszak
committed
Do not lose branch weights when lowering SwitchInst.
llvm-svn: 136529
1 parent b5c2d32 commit 0480a8f

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,10 +1282,12 @@ uint32_t SelectionDAGBuilder::getEdgeWeight(MachineBasicBlock *Src,
12821282
return BPI->getEdgeWeight(SrcBB, DstBB);
12831283
}
12841284

1285-
void SelectionDAGBuilder::addSuccessorWithWeight(MachineBasicBlock *Src,
1286-
MachineBasicBlock *Dst) {
1287-
uint32_t weight = getEdgeWeight(Src, Dst);
1288-
Src->addSuccessor(Dst, weight);
1285+
void SelectionDAGBuilder::
1286+
addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst,
1287+
uint32_t Weight /* = 0 */) {
1288+
if (!Weight)
1289+
Weight = getEdgeWeight(Src, Dst);
1290+
Src->addSuccessor(Dst, Weight);
12891291
}
12901292

12911293

@@ -1558,8 +1560,8 @@ void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
15581560
}
15591561

15601562
// Update successor info
1561-
addSuccessorWithWeight(SwitchBB, CB.TrueBB);
1562-
addSuccessorWithWeight(SwitchBB, CB.FalseBB);
1563+
addSuccessorWithWeight(SwitchBB, CB.TrueBB, CB.TrueWeight);
1564+
addSuccessorWithWeight(SwitchBB, CB.FalseBB, CB.FalseWeight);
15631565

15641566
// Set NextBlock to be the MBB immediately after the current one, if any.
15651567
// This is used to avoid emitting unnecessary branches to the next block.
@@ -1910,8 +1912,8 @@ bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
19101912
ISD::SETEQ);
19111913

19121914
// Update successor info.
1913-
SwitchBB->addSuccessor(Small.BB);
1914-
SwitchBB->addSuccessor(Default);
1915+
addSuccessorWithWeight(SwitchBB, Small.BB);
1916+
addSuccessorWithWeight(SwitchBB, Default);
19151917

19161918
// Insert the true branch.
19171919
SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other,
@@ -1967,7 +1969,11 @@ bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
19671969
CC = ISD::SETLE;
19681970
LHS = I->Low; MHS = SV; RHS = I->High;
19691971
}
1970-
CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
1972+
1973+
uint32_t ExtraWeight = I->ExtraWeight;
1974+
CaseBlock CB(CC, LHS, RHS, MHS, /* truebb */ I->BB, /* falsebb */ FallThrough,
1975+
/* me */ CurBlock,
1976+
/* trueweight */ ExtraWeight / 2, /* falseweight */ ExtraWeight / 2);
19711977

19721978
// If emitting the first comparison, just call visitSwitchCase to emit the
19731979
// code into the current block. Otherwise, push the CaseBlock onto the
@@ -2362,12 +2368,17 @@ size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
23622368
const SwitchInst& SI) {
23632369
size_t numCmps = 0;
23642370

2371+
BranchProbabilityInfo *BPI = FuncInfo.BPI;
23652372
// Start with "simple" cases
23662373
for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
2367-
MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2374+
BasicBlock *SuccBB = SI.getSuccessor(i);
2375+
MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB];
2376+
2377+
uint32_t ExtraWeight = BPI ? BPI->getEdgeWeight(SI.getParent(), SuccBB) : 0;
2378+
23682379
Cases.push_back(Case(SI.getSuccessorValue(i),
23692380
SI.getSuccessorValue(i),
2370-
SMBB));
2381+
SMBB, ExtraWeight));
23712382
}
23722383
std::sort(Cases.begin(), Cases.end(), CaseCmp());
23732384

@@ -2387,6 +2398,16 @@ size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
23872398
if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
23882399
I->High = J->High;
23892400
J = Cases.erase(J);
2401+
2402+
if (BranchProbabilityInfo *BPI = FuncInfo.BPI) {
2403+
uint32_t CurWeight = currentBB->getBasicBlock() ?
2404+
BPI->getEdgeWeight(SI.getParent(), currentBB->getBasicBlock()) : 16;
2405+
uint32_t NextWeight = nextBB->getBasicBlock() ?
2406+
BPI->getEdgeWeight(SI.getParent(), nextBB->getBasicBlock()) : 16;
2407+
2408+
BPI->setEdgeWeight(SI.getParent(), currentBB->getBasicBlock(),
2409+
CurWeight + NextWeight);
2410+
}
23902411
} else {
23912412
I = J++;
23922413
}

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,13 @@ class SelectionDAGBuilder {
132132
Constant* Low;
133133
Constant* High;
134134
MachineBasicBlock* BB;
135+
uint32_t ExtraWeight;
136+
137+
Case() : Low(0), High(0), BB(0), ExtraWeight(0) { }
138+
Case(Constant* low, Constant* high, MachineBasicBlock* bb,
139+
uint32_t extraweight) : Low(low), High(high), BB(bb),
140+
ExtraWeight(extraweight) { }
135141

136-
Case() : Low(0), High(0), BB(0) { }
137-
Case(Constant* low, Constant* high, MachineBasicBlock* bb) :
138-
Low(low), High(high), BB(bb) { }
139142
APInt size() const {
140143
const APInt &rHigh = cast<ConstantInt>(High)->getValue();
141144
const APInt &rLow = cast<ConstantInt>(Low)->getValue();
@@ -203,20 +206,30 @@ class SelectionDAGBuilder {
203206
CaseBlock(ISD::CondCode cc, const Value *cmplhs, const Value *cmprhs,
204207
const Value *cmpmiddle,
205208
MachineBasicBlock *truebb, MachineBasicBlock *falsebb,
206-
MachineBasicBlock *me)
209+
MachineBasicBlock *me,
210+
uint32_t trueweight = 0, uint32_t falseweight = 0)
207211
: CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs),
208-
TrueBB(truebb), FalseBB(falsebb), ThisBB(me) {}
212+
TrueBB(truebb), FalseBB(falsebb), ThisBB(me),
213+
TrueWeight(trueweight), FalseWeight(falseweight) { }
214+
209215
// CC - the condition code to use for the case block's setcc node
210216
ISD::CondCode CC;
217+
211218
// CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit.
212219
// Emit by default LHS op RHS. MHS is used for range comparisons:
213220
// If MHS is not null: (LHS <= MHS) and (MHS <= RHS).
214221
const Value *CmpLHS, *CmpMHS, *CmpRHS;
222+
215223
// TrueBB/FalseBB - the block to branch to if the setcc is true/false.
216224
MachineBasicBlock *TrueBB, *FalseBB;
225+
217226
// ThisBB - the block into which to emit the code for the setcc and branches
218227
MachineBasicBlock *ThisBB;
228+
229+
// TrueWeight/FalseWeight - branch weights.
230+
uint32_t TrueWeight, FalseWeight;
219231
};
232+
220233
struct JumpTable {
221234
JumpTable(unsigned R, unsigned J, MachineBasicBlock *M,
222235
MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {}
@@ -436,7 +449,8 @@ class SelectionDAGBuilder {
436449
MachineBasicBlock *SwitchBB);
437450

438451
uint32_t getEdgeWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst);
439-
void addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst);
452+
void addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst,
453+
uint32_t Weight = 0);
440454
public:
441455
void visitSwitchCase(CaseBlock &CB,
442456
MachineBasicBlock *SwitchBB);

0 commit comments

Comments
 (0)