Skip to content

Commit 38758ee

Browse files
author
Alexander Yermolovich
committed
Removed skip and moved processInstructionForFuncReferences, nits
1 parent d03ff1d commit 38758ee

File tree

6 files changed

+35
-122
lines changed

6 files changed

+35
-122
lines changed

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,9 +1350,6 @@ class BinaryContext {
13501350
return Code.size();
13511351
}
13521352

1353-
/// Processes .text section to identify function references.
1354-
void processInstructionForFuncReferences(const MCInst &Inst);
1355-
13561353
/// Compute the native code size for a range of instructions.
13571354
/// Note: this can be imprecise wrt the final binary since happening prior to
13581355
/// relaxation, as well as wrt the original binary because of opcode

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ class BinaryFunction {
428428
/// Function order for streaming into the destination binary.
429429
uint32_t Index{-1U};
430430

431-
/// Indicates function is referenced by none-control flow instruction.
431+
/// Indicates function is referenced by non-control flow instruction.
432432
bool HasAddressTaken{false};
433433

434434
/// Get basic block index assuming it belongs to this function.
@@ -820,10 +820,10 @@ class BinaryFunction {
820820
return nullptr;
821821
}
822822

823-
/// Returns true if function is referenced in none-control flow instructions..
823+
/// Return true if function is referenced in non-control flow instructions..
824824
bool hasAddressTaken() const { return HasAddressTaken; }
825825

826-
/// Sets whether function is referenced in none-control flow instructions.
826+
/// Set whether function is referenced in non-control flow instructions.
827827
void setHasAddressTaken(bool Hat) { HasAddressTaken = Hat; }
828828

829829
/// Returns the raw binary encoding of this function.
@@ -2103,6 +2103,10 @@ class BinaryFunction {
21032103
// adjustments.
21042104
void handleAArch64IndirectCall(MCInst &Instruction, const uint64_t Offset);
21052105

2106+
/// Processes code section to identify function references.
2107+
void processInstructionForFuncReferences(BinaryContext &BC,
2108+
const MCInst &Inst);
2109+
21062110
/// Scan function for references to other functions. In relocation mode,
21072111
/// add relocations for external references. In non-relocation mode, detect
21082112
/// and mark new entry points.

bolt/lib/Core/BinaryContext.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,28 +1945,6 @@ static void printDebugInfo(raw_ostream &OS, const MCInst &Instruction,
19451945
OS << " discriminator:" << Row.Discriminator;
19461946
}
19471947

1948-
/// Skip instructions that do not potentially manipulate or compare function
1949-
/// addresses.
1950-
static bool skipInstruction(const MCInst &Inst, const BinaryContext &BC) {
1951-
return (BC.MIB->isPseudo(Inst) || BC.MIB->isUnconditionalBranch(Inst) ||
1952-
BC.MIB->isConditionalBranch(Inst) || BC.MIB->isCall(Inst) ||
1953-
BC.MIB->isBranch(Inst));
1954-
}
1955-
void BinaryContext::processInstructionForFuncReferences(const MCInst &Inst) {
1956-
if (skipInstruction(Inst, *this))
1957-
return;
1958-
for (const MCOperand &Op : MCPlus::primeOperands(Inst)) {
1959-
if (!Op.isExpr())
1960-
continue;
1961-
const MCExpr &Expr = *Op.getExpr();
1962-
if (Expr.getKind() == MCExpr::SymbolRef) {
1963-
const MCSymbol &Symbol = cast<MCSymbolRefExpr>(Expr).getSymbol();
1964-
if (BinaryFunction *BF = getFunctionForSymbol(&Symbol))
1965-
BF->setHasAddressTaken(true);
1966-
}
1967-
}
1968-
}
1969-
19701948
void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
19711949
uint64_t Offset,
19721950
const BinaryFunction *Function,

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,20 @@ MCSymbol *BinaryFunction::registerBranch(uint64_t Src, uint64_t Dst) {
15131513
return Target;
15141514
}
15151515

1516+
void BinaryFunction::processInstructionForFuncReferences(BinaryContext &BC,
1517+
const MCInst &Inst) {
1518+
for (const MCOperand &Op : MCPlus::primeOperands(Inst)) {
1519+
if (!Op.isExpr())
1520+
continue;
1521+
const MCExpr &Expr = *Op.getExpr();
1522+
if (Expr.getKind() == MCExpr::SymbolRef) {
1523+
const MCSymbol &Symbol = cast<MCSymbolRefExpr>(Expr).getSymbol();
1524+
if (BinaryFunction *BF = BC.getFunctionForSymbol(&Symbol))
1525+
BF->setHasAddressTaken(true);
1526+
}
1527+
}
1528+
}
1529+
15161530
bool BinaryFunction::scanExternalRefs() {
15171531
bool Success = true;
15181532
bool DisassemblyFailed = false;
@@ -1626,15 +1640,15 @@ bool BinaryFunction::scanExternalRefs() {
16261640
if (!BC.HasRelocations)
16271641
continue;
16281642

1629-
BC.processInstructionForFuncReferences(Instruction);
1630-
16311643
if (BranchTargetSymbol) {
16321644
BC.MIB->replaceBranchTarget(Instruction, BranchTargetSymbol,
16331645
Emitter.LocalCtx.get());
16341646
} else if (!llvm::any_of(Instruction,
16351647
[](const MCOperand &Op) { return Op.isExpr(); })) {
16361648
// Skip assembly if the instruction may not have any symbolic operands.
16371649
continue;
1650+
} else {
1651+
processInstructionForFuncReferences(BC, Instruction);
16381652
}
16391653

16401654
// Emit the instruction using temp emitter and generate relocations.

bolt/lib/Passes/IdenticalCodeFolding.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,12 @@ Error IdenticalCodeFolding::markFunctionsUnsafeToFold(BinaryContext &BC) {
439439
}
440440

441441
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
442-
for (const BinaryBasicBlock *BB : BF.getLayout().blocks())
443-
for (const MCInst &Inst : *BB)
444-
BC.processInstructionForFuncReferences(Inst);
442+
for (const BinaryBasicBlock *BB : BF.getLayout().blocks()) {
443+
for (const MCInst &Inst : *BB) {
444+
if (!(BC.MIB->isCall(Inst) || BC.MIB->isBranch(Inst)))
445+
BF.processInstructionForFuncReferences(BC, Inst);
446+
}
447+
}
445448
};
446449
ParallelUtilities::PredicateTy SkipFunc =
447450
[&](const BinaryFunction &BF) -> bool {

bolt/test/X86/icf-safe-icp.test

Lines changed: 6 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## Check that BOLT handles correctly folding functions with --icf=safe that can be referenced.
2-
## The compare is generated by the ICP path with instrumentation profiling.
1+
## Check that BOLT handles correctly folding functions with --icf=safe
2+
## that can be referenced through a non control flow instruction when ICP optimization is enabled.
3+
## This tests also checks that destructors are not folded.
34

45
# REQUIRES: system-linux
56
# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-linux %s -o %t1.o
@@ -8,11 +9,10 @@
89
# RUN: llvm-bolt --no-threads %t.exe --icf=safe -debug -debug-only=bolt-icf -o %t.bolt 2>&1 | FileCheck --check-prefix=SAFEICFCHECK %s
910

1011
# ICFCHECK: ICF iteration 1
11-
# ICFCHECK-NEXT: folding _ZNK8Derived34funcEii into _ZNK8Derived24funcEii
1212
# ICFCHECK-NEXT: folding _ZN8Derived3D0Ev into _ZN8Derived2D0Ev
13+
# ICFCHECK-NEXT: folding _ZNK8Derived34funcEii into _ZNK8Derived24funcEii
1314

14-
# SAFEICFCHECK: skipping function _ZNK8Derived24funcEii
15-
# SAFEICFCHECK-NEXT: skipping function _ZNK8Derived34funcEii
15+
# SAFEICFCHECK: skipping function _ZNK8Derived34funcEii
1616
# SAFEICFCHECK-NEXT: ICF iteration 1
1717
# SAFEICFCHECK-NEXT: folding _ZN8Derived3D0Ev into _ZN8Derived2D0Ev
1818
# SAFEICFCHECK-NEXT: ===---------
@@ -88,51 +88,17 @@
8888
## return 5;
8989
## }
9090
## Manually modified to remove "extra" assembly.
91-
.section .text.hot.,"ax",@progbits
92-
.globl _Z10createTypei
93-
.type _Z10createTypei,@function
94-
_Z10createTypei:
95-
callq _Znwm@PLT
96-
leaq _ZTV8Derived2+16(%rip), %rcx
97-
leaq _ZTV8Derived3+16(%rip), %rdx
98-
cmoveq %rcx, %rdx
99-
retq
100-
.size _Z10createTypei, .-_Z10createTypei
101-
102-
.globl _Z10returnFivev
103-
.type _Z10returnFivev,@function
104-
_Z10returnFivev:
105-
movl $5, %eax
106-
retq
107-
.size _Z10returnFivev, .-_Z10returnFivev
108-
109-
.globl returnFourOrFiveFunc
110-
.type returnFourOrFiveFunc,@function
111-
returnFourOrFiveFunc:
112-
xorl %eax, %eax
113-
cmpl $1, %edi
114-
sete %al
115-
xorl $5, %eax
116-
retq
117-
.size returnFourOrFiveFunc, .-returnFourOrFiveFunc
118-
11991
.globl main
12092
.type main,@function
12193
main:
122-
callq returnFourOrFiveFunc@PLT
123-
callq _Z10returnFivev@PLT
124-
callq _Z10createTypei
125-
callq _Z10createTypei
126-
leaq _ZNK8Derived24funcEii(%rip), %rcx
127-
callq _ZNK8Derived24funcEii
12894
leaq _ZNK8Derived34funcEii(%rip), %rcx
12995
callq _ZNK8Derived34funcEii
13096
.size main, .-main
13197

13298
.section .text.hot._ZNK8Derived24funcEii,"axG",@progbits,_ZNK8Derived24funcEii,comdat
13399
.weak _ZNK8Derived24funcEii
134100
.type _ZNK8Derived24funcEii,@function
135-
_ZNK8Derived24funcEii: #
101+
_ZNK8Derived24funcEii:
136102
imull %esi, %eax
137103
retq
138104
.size _ZNK8Derived24funcEii, .-_ZNK8Derived24funcEii
@@ -141,7 +107,6 @@ _ZNK8Derived24funcEii: #
141107
.weak _ZN8Derived2D0Ev
142108
.type _ZN8Derived2D0Ev,@function
143109
_ZN8Derived2D0Ev:
144-
movl $16, %esi
145110
jmp _ZdlPvm@PLT
146111
.size _ZN8Derived2D0Ev, .-_ZN8Derived2D0Ev
147112

@@ -164,7 +129,6 @@ _ZN4BaseD2Ev:
164129
.weak _ZN8Derived3D0Ev
165130
.type _ZN8Derived3D0Ev,@function
166131
_ZN8Derived3D0Ev:
167-
movl $16, %esi
168132
jmp _ZdlPvm@PLT
169133
.size _ZN8Derived3D0Ev, .-_ZN8Derived3D0Ev
170134

@@ -179,37 +143,6 @@ _ZTV8Derived2:
179143
.quad _ZN8Derived2D0Ev
180144
.size _ZTV8Derived2, 40
181145

182-
.type _ZTS8Derived2,@object
183-
.section .rodata._ZTS8Derived2,"aG",@progbits,_ZTS8Derived2,comdat
184-
.weak _ZTS8Derived2
185-
_ZTS8Derived2:
186-
.asciz "8Derived2"
187-
.size _ZTS8Derived2, 10
188-
189-
.type _ZTS4Base,@object
190-
.section .rodata._ZTS4Base,"aG",@progbits,_ZTS4Base,comdat
191-
.weak _ZTS4Base
192-
_ZTS4Base:
193-
.asciz "4Base"
194-
.size _ZTS4Base, 6
195-
196-
.type _ZTI4Base,@object
197-
.section .data.rel.ro._ZTI4Base,"awG",@progbits,_ZTI4Base,comdat
198-
.weak _ZTI4Base
199-
_ZTI4Base:
200-
.quad _ZTVN10__cxxabiv117__class_type_infoE+16
201-
.quad _ZTS4Base
202-
.size _ZTI4Base, 16
203-
204-
.type _ZTI8Derived2,@object
205-
.section .data.rel.ro._ZTI8Derived2,"awG",@progbits,_ZTI8Derived2,comdat
206-
.weak _ZTI8Derived2
207-
_ZTI8Derived2:
208-
.quad _ZTVN10__cxxabiv120__si_class_type_infoE+16
209-
.quad _ZTS8Derived2
210-
.quad _ZTI4Base
211-
.size _ZTI8Derived2, 24
212-
213146
.type _ZTV8Derived3,@object
214147
.section .data.rel.ro._ZTV8Derived3,"awG",@progbits,_ZTV8Derived3,comdat
215148
.weak _ZTV8Derived3
@@ -220,19 +153,3 @@ _ZTV8Derived3:
220153
.quad _ZN4BaseD2Ev
221154
.quad _ZN8Derived3D0Ev
222155
.size _ZTV8Derived3, 40
223-
224-
.type _ZTS8Derived3,@object
225-
.section .rodata._ZTS8Derived3,"aG",@progbits,_ZTS8Derived3,comdat
226-
.weak _ZTS8Derived3
227-
_ZTS8Derived3:
228-
.asciz "8Derived3"
229-
.size _ZTS8Derived3, 10
230-
231-
.type _ZTI8Derived3,@object
232-
.section .data.rel.ro._ZTI8Derived3,"awG",@progbits,_ZTI8Derived3,comdat
233-
.weak _ZTI8Derived3
234-
_ZTI8Derived3:
235-
.quad _ZTVN10__cxxabiv120__si_class_type_infoE+16
236-
.quad _ZTS8Derived3
237-
.quad _ZTI4Base
238-
.size _ZTI8Derived3, 24

0 commit comments

Comments
 (0)