File tree Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -170,6 +170,7 @@ class MCPlusBuilder {
170
170
// / MCPlusBuilder classes must use convert/lower/create* interfaces instead.
171
171
void setTailCall (MCInst &Inst);
172
172
173
+ public:
173
174
// / Transfer annotations from \p SrcInst to \p DstInst.
174
175
void moveAnnotations (MCInst &&SrcInst, MCInst &DstInst) const {
175
176
assert (!getAnnotationInst (DstInst) &&
@@ -182,7 +183,6 @@ class MCPlusBuilder {
182
183
removeAnnotationInst (SrcInst);
183
184
}
184
185
185
- public:
186
186
// / Return iterator range covering def operands.
187
187
iterator_range<MCInst::iterator> defOperands (MCInst &Inst) const {
188
188
return make_range (Inst.begin (),
Original file line number Diff line number Diff line change @@ -19,13 +19,15 @@ void FixRISCVCallsPass::runOnFunction(BinaryFunction &BF) {
19
19
auto *Target = MIB->getTargetSymbol (*II);
20
20
assert (Target && " Cannot find call target" );
21
21
22
+ MCInst OldCall = *II;
22
23
auto L = BC.scopeLock ();
23
24
24
25
if (MIB->isTailCall (*II))
25
26
MIB->createTailCall (*II, Target, Ctx);
26
27
else
27
28
MIB->createCall (*II, Target, Ctx);
28
29
30
+ MIB->moveAnnotations (std::move (OldCall), *II);
29
31
++II;
30
32
continue ;
31
33
}
@@ -39,8 +41,19 @@ void FixRISCVCallsPass::runOnFunction(BinaryFunction &BF) {
39
41
auto *Target = MIB->getTargetSymbol (*II);
40
42
assert (Target && " Cannot find call target" );
41
43
44
+ MCInst OldCall = *NextII;
42
45
auto L = BC.scopeLock ();
43
46
MIB->createCall (*II, Target, Ctx);
47
+ MIB->moveAnnotations (std::move (OldCall), *II);
48
+
49
+ // The original offset was set on the jalr of the auipc+jalr pair. Since
50
+ // the whole pair is replaced by a call, adjust the offset by -4 (the
51
+ // size of a auipc).
52
+ if (std::optional<uint32_t > Offset = MIB->getOffset (*II)) {
53
+ assert (*Offset >= 4 && " Illegal jalr offset" );
54
+ MIB->setOffset (*II, *Offset - 4 );
55
+ }
56
+
44
57
II = BB.eraseInstruction (NextII);
45
58
continue ;
46
59
}
Original file line number Diff line number Diff line change
1
+ /// Test that annotations are properly carried over to fixed calls.
2
+ /// Note that --enable-bat is used to force offsets to be kept.
3
+
4
+ // RUN: llvm-mc -triple riscv64 -filetype obj -o %t.o %s
5
+ // RUN: ld.lld --emit-relocs -o %t %t.o
6
+ // RUN: llvm-bolt --enable-bat --print-cfg --print-fix-riscv-calls \
7
+ // RUN: --print-only=_start -o /dev/null %t | FileCheck %s
8
+
9
+ .text
10
+ .global f
11
+ .p2align 1
12
+ f:
13
+ ret
14
+ .size f, .-f
15
+
16
+ // CHECK-LABEL: Binary Function "_start" after building cfg {
17
+ // CHECK: auipc ra, f
18
+ // CHECK-NEXT: jalr ra, -4(ra) # Offset: 4
19
+ // CHECK-NEXT: jal ra, f # Offset: 8
20
+ // CHECK-NEXT: jal zero, f # TAILCALL # Offset: 12
21
+
22
+ // CHECK-LABEL: Binary Function "_start" after fix-riscv-calls {
23
+ // CHECK: call f # Offset: 0
24
+ // CHECK-NEXT: call f # Offset: 8
25
+ // CHECK-NEXT: tail f # TAILCALL # Offset: 12
26
+
27
+ .globl _start
28
+ .p2align 1
29
+ _start:
30
+ call f
31
+ jal f
32
+ jal zero, f
33
+ .size _start, .-_start
You can’t perform that action at this time.
0 commit comments