Skip to content

Commit 776e6de

Browse files
author
Quentin Colombet
committed
[MachineBlockPlacement] Let the target optimize the branches at the end.
After the layout of the basic blocks is set, the target may be able to get rid of unconditional branches to fallthrough blocks that the generic code does not catch. This happens any time TargetInstrInfo::AnalyzeBranch is not able to analyze all the branches involved in the terminators sequence, while still understanding a few of them. In such situation, AnalyzeBranch can directly modify the branches if it has been instructed to do so. This patch takes advantage of that. llvm-svn: 268328
1 parent 4e1d389 commit 776e6de

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

llvm/lib/CodeGen/MachineBlockPlacement.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,19 @@ void MachineBlockPlacement::buildCFGChains(MachineFunction &F) {
13581358
MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For AnalyzeBranch.
13591359
if (!TII->AnalyzeBranch(F.back(), TBB, FBB, Cond))
13601360
F.back().updateTerminator();
1361+
1362+
// Now that all the basic blocks in the chain have the proper layout,
1363+
// make a final call to AnalyzeBranch with AllowModify set.
1364+
// Indeed, the target may be able to optimize the branches in a way we
1365+
// cannot because all branches may not be analyzable.
1366+
// E.g., the target may be able to remove an unconditional branch to
1367+
// a fallthrough when it occurs after predicated terminators.
1368+
for (MachineBasicBlock *ChainBB : FunctionChain) {
1369+
Cond.clear();
1370+
TBB = nullptr;
1371+
FBB = nullptr; // For AnalyzeBranch.
1372+
(void)TII->AnalyzeBranch(*ChainBB, TBB, FBB, Cond, /*AllowModify*/ true);
1373+
}
13611374
}
13621375

13631376
void MachineBlockPlacement::alignBlocks(MachineFunction &F) {

llvm/test/CodeGen/X86/implicit-null-check.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ define i32 @imp_null_check_via_mem_comparision(i32* %x, i32 %val) {
218218
; OBJDUMP-NEXT: Version: 0x1
219219
; OBJDUMP-NEXT: NumFunctions: 5
220220
; OBJDUMP-NEXT: FunctionAddress: 0x000000, NumFaultingPCs: 1
221-
; OBJDUMP-NEXT: Fault kind: FaultingLoad, faulting PC offset: 0, handling PC offset: 7
221+
; OBJDUMP-NEXT: Fault kind: FaultingLoad, faulting PC offset: 0, handling PC offset: 5
222222
; OBJDUMP-NEXT: FunctionAddress: 0x000000, NumFaultingPCs: 1
223-
; OBJDUMP-NEXT: Fault kind: FaultingLoad, faulting PC offset: 0, handling PC offset: 9
223+
; OBJDUMP-NEXT: Fault kind: FaultingLoad, faulting PC offset: 0, handling PC offset: 7
224224
; OBJDUMP-NEXT: FunctionAddress: 0x000000, NumFaultingPCs: 1
225-
; OBJDUMP-NEXT: Fault kind: FaultingLoad, faulting PC offset: 0, handling PC offset: 9
225+
; OBJDUMP-NEXT: Fault kind: FaultingLoad, faulting PC offset: 0, handling PC offset: 7
226226
; OBJDUMP-NEXT: FunctionAddress: 0x000000, NumFaultingPCs: 1
227-
; OBJDUMP-NEXT: Fault kind: FaultingLoad, faulting PC offset: 0, handling PC offset: 5
227+
; OBJDUMP-NEXT: Fault kind: FaultingLoad, faulting PC offset: 0, handling PC offset: 3

0 commit comments

Comments
 (0)