Skip to content

Commit 21a5729

Browse files
authored
[BOLT] Do not use HLT as split point when build the CFG (#150963)
For x86, the halt instruction is defined as a terminator instruction. When building the CFG, the instruction sequence following the hlt instruction is treated as an independent MBB. Since there is no jump information, the predecessor of this MBB cannot be identified, and it is considered an unreachable MBB that will be removed. Using this fix, the instruction sequences before and after hlt are refused to be placed in different blocks.
1 parent d0b19cf commit 21a5729

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,10 @@ class MCPlusBuilder {
740740
return false;
741741
}
742742

743+
/// Return true if the hlt instruction under the x86, otherwise, default to
744+
/// false.
745+
virtual bool isX86HLT(const MCInst &Inst) const { return false; }
746+
743747
/// Return the width, in bytes, of the memory access performed by \p Inst, if
744748
/// this is a pop instruction. Return zero otherwise.
745749
virtual int getPopSize(const MCInst &Inst) const {

bolt/lib/Core/MCPlusBuilder.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ bool MCPlusBuilder::equals(const MCSpecifierExpr &A, const MCSpecifierExpr &B,
132132
}
133133

134134
bool MCPlusBuilder::isTerminator(const MCInst &Inst) const {
135-
return Analysis->isTerminator(Inst) ||
136-
(opts::TerminalTrap && Info->get(Inst.getOpcode()).isTrap());
135+
return (opts::TerminalTrap && Info->get(Inst.getOpcode()).isTrap()) ||
136+
Analysis->isTerminator(Inst)
137+
? !isX86HLT(Inst)
138+
: false;
137139
}
138140

139141
void MCPlusBuilder::setTailCall(MCInst &Inst) const {

bolt/lib/Target/X86/X86MCPlusBuilder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ class X86MCPlusBuilder : public MCPlusBuilder {
223223
return Inst.getOpcode() == X86::ENDBR32 || Inst.getOpcode() == X86::ENDBR64;
224224
}
225225

226+
bool isX86HLT(const MCInst &Inst) const override {
227+
return Inst.getOpcode() == X86::HLT;
228+
}
229+
226230
int getPopSize(const MCInst &Inst) const override {
227231
switch (Inst.getOpcode()) {
228232
case X86::POP16r:

bolt/test/X86/cfg_build_hlt.s

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Check CFG for halt instruction
2+
3+
# RUN: %clang %cflags %s -static -o %t.exe -nostdlib
4+
# RUN: llvm-bolt %t.exe --print-cfg --print-only=main -o %t 2>&1 | FileCheck %s --check-prefix=CHECK-CFG
5+
# RUN: llvm-objdump -d %t --print-imm-hex | FileCheck %s --check-prefix=CHECK-BIN
6+
7+
# CHECK-CFG: BB Count : 1
8+
# CHECK-BIN: <main>:
9+
# CHECK-BIN-NEXT: f4 hlt
10+
# CHECK-BIN-NEXT: c3 retq
11+
12+
.global main
13+
.type main, %function
14+
main:
15+
hlt
16+
retq
17+
.size main, .-main

0 commit comments

Comments
 (0)