Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit d4f04a7

Browse files
author
Cong Hou
committed
Create a new interface addSuccessorWithoutWeight(MBB*) in MBB to add successors when optimization is disabled.
When optimization is disabled, edge weights that are stored in MBB won't be used so that we don't have to store them. Currently, this is done by adding successors with default weight 0, and if all successors have default weights, the weight list will be empty. But that the weight list is empty doesn't mean disabled optimization (as is stated several times in MachineBasicBlock.cpp): it may also mean all successors just have default weights. We should discourage using default weights when adding successors, because it is very easy for users to forget update the correct edge weights instead of using default ones (one exception is that the MBB only has one successor). In order to detect such usages, it is better to differentiate using default weights from the case when optimizations is disabled. In this patch, a new interface addSuccessorWithoutWeight(MBB*) is created for when optimization is disabled. In this case, MBB will try to maintain an empty weight list, but it cannot guarantee this as for many uses of addSuccessor() whether optimization is disabled or not is not checked. But it can guarantee that if optimization is enabled, then the weight list always has the same size of the successor list. Differential revision: http://reviews.llvm.org/D13963 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@251429 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 63fe164 commit d4f04a7

File tree

7 files changed

+48
-30
lines changed

7 files changed

+48
-30
lines changed

include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,12 @@ class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
425425
/// Note that duplicate Machine CFG edges are not allowed.
426426
void addSuccessor(MachineBasicBlock *Succ, uint32_t Weight = 0);
427427

428+
/// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
429+
/// of Succ is automatically updated. The weight is not provided because BPI
430+
/// is not available (e.g. -O0 is used), in which case edge weights won't be
431+
/// used. Using this interface can save some space.
432+
void addSuccessorWithoutWeight(MachineBasicBlock *Succ);
433+
428434
/// Set successor weight of a given iterator.
429435
void setSuccWeight(succ_iterator I, uint32_t Weight);
430436

lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,19 @@ void MachineBasicBlock::updateTerminator() {
506506
}
507507

508508
void MachineBasicBlock::addSuccessor(MachineBasicBlock *Succ, uint32_t Weight) {
509-
510-
// If we see non-zero value for the first time it means we actually use Weight
511-
// list, so we fill all Weights with 0's.
512-
if (Weight != 0 && Weights.empty())
513-
Weights.resize(Successors.size());
514-
515-
if (Weight != 0 || !Weights.empty())
509+
// Weight list is either empty (if successor list isn't empty, this means
510+
// disabled optimization) or has the same size as successor list.
511+
if (!(Weights.empty() && !Successors.empty()))
516512
Weights.push_back(Weight);
513+
Successors.push_back(Succ);
514+
Succ->addPredecessor(this);
515+
}
517516

517+
void MachineBasicBlock::addSuccessorWithoutWeight(MachineBasicBlock *Succ) {
518+
// We need to make sure weight list is either empty or has the same size of
519+
// successor list. When this function is called, we can safely delete all
520+
// weight in the list.
521+
Weights.clear();
518522
Successors.push_back(Succ);
519523
Succ->addPredecessor(this);
520524
}

lib/CodeGen/SelectionDAG/FastISel.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,25 +1394,28 @@ void FastISel::fastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DbgLoc) {
13941394
TII.InsertBranch(*FuncInfo.MBB, MSucc, nullptr,
13951395
SmallVector<MachineOperand, 0>(), DbgLoc);
13961396
}
1397-
uint32_t BranchWeight = 0;
1398-
if (FuncInfo.BPI)
1399-
BranchWeight = FuncInfo.BPI->getEdgeWeight(FuncInfo.MBB->getBasicBlock(),
1400-
MSucc->getBasicBlock());
1401-
FuncInfo.MBB->addSuccessor(MSucc, BranchWeight);
1397+
if (FuncInfo.BPI) {
1398+
uint32_t BranchWeight = FuncInfo.BPI->getEdgeWeight(
1399+
FuncInfo.MBB->getBasicBlock(), MSucc->getBasicBlock());
1400+
FuncInfo.MBB->addSuccessor(MSucc, BranchWeight);
1401+
} else
1402+
FuncInfo.MBB->addSuccessorWithoutWeight(MSucc);
14021403
}
14031404

14041405
void FastISel::finishCondBranch(const BasicBlock *BranchBB,
14051406
MachineBasicBlock *TrueMBB,
14061407
MachineBasicBlock *FalseMBB) {
1407-
uint32_t BranchWeight = 0;
1408-
if (FuncInfo.BPI)
1409-
BranchWeight = FuncInfo.BPI->getEdgeWeight(BranchBB,
1410-
TrueMBB->getBasicBlock());
14111408
// Add TrueMBB as successor unless it is equal to the FalseMBB: This can
14121409
// happen in degenerate IR and MachineIR forbids to have a block twice in the
14131410
// successor/predecessor lists.
1414-
if (TrueMBB != FalseMBB)
1415-
FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
1411+
if (TrueMBB != FalseMBB) {
1412+
if (FuncInfo.BPI) {
1413+
uint32_t BranchWeight =
1414+
FuncInfo.BPI->getEdgeWeight(BranchBB, TrueMBB->getBasicBlock());
1415+
FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
1416+
} else
1417+
FuncInfo.MBB->addSuccessorWithoutWeight(TrueMBB);
1418+
}
14161419

14171420
fastEmitBranch(FalseMBB, DbgLoc);
14181421
}

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,9 +1499,13 @@ uint32_t SelectionDAGBuilder::getEdgeWeight(const MachineBasicBlock *Src,
14991499
void SelectionDAGBuilder::
15001500
addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst,
15011501
uint32_t Weight /* = 0 */) {
1502-
if (!Weight)
1503-
Weight = getEdgeWeight(Src, Dst);
1504-
Src->addSuccessor(Dst, Weight);
1502+
if (!FuncInfo.BPI)
1503+
Src->addSuccessorWithoutWeight(Dst);
1504+
else {
1505+
if (!Weight)
1506+
Weight = getEdgeWeight(Src, Dst);
1507+
Src->addSuccessor(Dst, Weight);
1508+
}
15051509
}
15061510

15071511

lib/Target/AArch64/AArch64FastISel.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,11 +2376,12 @@ bool AArch64FastISel::selectBranch(const Instruction *I) {
23762376
.addMBB(Target);
23772377

23782378
// Obtain the branch weight and add the target to the successor list.
2379-
uint32_t BranchWeight = 0;
2380-
if (FuncInfo.BPI)
2381-
BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2382-
Target->getBasicBlock());
2383-
FuncInfo.MBB->addSuccessor(Target, BranchWeight);
2379+
if (FuncInfo.BPI) {
2380+
uint32_t BranchWeight =
2381+
FuncInfo.BPI->getEdgeWeight(BI->getParent(), Target->getBasicBlock());
2382+
FuncInfo.MBB->addSuccessor(Target, BranchWeight);
2383+
} else
2384+
FuncInfo.MBB->addSuccessorWithoutWeight(Target);
23842385
return true;
23852386
} else if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
23862387
// Fake request the condition, otherwise the intrinsic might be completely

test/CodeGen/MIR/X86/newline-handling.mir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ liveins:
3535
# CHECK-LABEL: name: foo
3636
# CHECK: body: |
3737
# CHECK-NEXT: bb.0.entry:
38-
# CHECK-NEXT: successors: %bb.1.less, %bb.2.exit
38+
# CHECK-NEXT: successors: %bb.1.less(0), %bb.2.exit(0)
3939
# CHECK-NEXT: liveins: %edi
4040
# CHECK: CMP32ri8 %edi, 10, implicit-def %eflags
4141
# CHECK-NEXT: JG_1 %bb.2.exit, implicit killed %eflags
@@ -79,7 +79,7 @@ liveins:
7979
# CHECK-LABEL: name: bar
8080
# CHECK: body: |
8181
# CHECK-NEXT: bb.0.entry:
82-
# CHECK-NEXT: successors: %bb.1.less, %bb.2.exit
82+
# CHECK-NEXT: successors: %bb.1.less(0), %bb.2.exit(0)
8383
# CHECK-NEXT: liveins: %edi
8484
# CHECK: CMP32ri8 %edi, 10, implicit-def %eflags
8585
# CHECK-NEXT: JG_1 %bb.2.exit, implicit killed %eflags

test/CodeGen/MIR/X86/successor-basic-blocks.mir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
name: foo
3333
body: |
3434
; CHECK-LABEL: bb.0.entry:
35-
; CHECK: successors: %bb.1.less, %bb.2.exit
35+
; CHECK: successors: %bb.1.less(0), %bb.2.exit(0)
3636
; CHECK-LABEL: bb.1.less:
3737
bb.0.entry:
3838
successors: %bb.1.less, %bb.2.exit
@@ -58,7 +58,7 @@ body: |
5858
; Verify that we can have multiple lists of successors that will be merged
5959
; into one.
6060
; CHECK-LABEL: bb.0.entry:
61-
; CHECK: successors: %bb.1, %bb.2
61+
; CHECK: successors: %bb.1(0), %bb.2(0)
6262
bb.0.entry:
6363
liveins: %edi
6464
successors: %bb.1

0 commit comments

Comments
 (0)