[analyzer][Z3] Restore the original timeout of 15s (#118291)
[llvm-project.git] / llvm / lib / ExecutionEngine / JITLink / x86_64.cpp
blobe5b48d2c3fab0e4888f7b0569aba7aa6f8f34ae7
1 //===----- x86_64.cpp - Generic JITLink x86-64 edge kinds, utilities ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Generic utilities for graphs representing x86-64 objects.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ExecutionEngine/JITLink/x86_64.h"
15 #define DEBUG_TYPE "jitlink"
17 namespace llvm {
18 namespace jitlink {
19 namespace x86_64 {
21 const char *getEdgeKindName(Edge::Kind K) {
22 switch (K) {
23 case Pointer64:
24 return "Pointer64";
25 case Pointer32:
26 return "Pointer32";
27 case Pointer32Signed:
28 return "Pointer32Signed";
29 case Pointer16:
30 return "Pointer16";
31 case Pointer8:
32 return "Pointer8";
33 case Delta64:
34 return "Delta64";
35 case Delta32:
36 return "Delta32";
37 case Delta16:
38 return "Delta16";
39 case Delta8:
40 return "Delta8";
41 case NegDelta64:
42 return "NegDelta64";
43 case NegDelta32:
44 return "NegDelta32";
45 case Size64:
46 return "Size64";
47 case Size32:
48 return "Size32";
49 case Delta64FromGOT:
50 return "Delta64FromGOT";
51 case PCRel32:
52 return "PCRel32";
53 case BranchPCRel32:
54 return "BranchPCRel32";
55 case BranchPCRel32ToPtrJumpStub:
56 return "BranchPCRel32ToPtrJumpStub";
57 case BranchPCRel32ToPtrJumpStubBypassable:
58 return "BranchPCRel32ToPtrJumpStubBypassable";
59 case RequestGOTAndTransformToDelta32:
60 return "RequestGOTAndTransformToDelta32";
61 case RequestGOTAndTransformToDelta64:
62 return "RequestGOTAndTransformToDelta64";
63 case RequestGOTAndTransformToDelta64FromGOT:
64 return "RequestGOTAndTransformToDelta64FromGOT";
65 case PCRel32GOTLoadREXRelaxable:
66 return "PCRel32GOTLoadREXRelaxable";
67 case RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable:
68 return "RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable";
69 case PCRel32GOTLoadRelaxable:
70 return "PCRel32GOTLoadRelaxable";
71 case RequestGOTAndTransformToPCRel32GOTLoadRelaxable:
72 return "RequestGOTAndTransformToPCRel32GOTLoadRelaxable";
73 case PCRel32TLVPLoadREXRelaxable:
74 return "PCRel32TLVPLoadREXRelaxable";
75 case RequestTLVPAndTransformToPCRel32TLVPLoadREXRelaxable:
76 return "RequestTLVPAndTransformToPCRel32TLVPLoadREXRelaxable";
77 default:
78 return getGenericEdgeKindName(static_cast<Edge::Kind>(K));
82 const char NullPointerContent[PointerSize] = {0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x00};
85 const char PointerJumpStubContent[6] = {
86 static_cast<char>(0xFFu), 0x25, 0x00, 0x00, 0x00, 0x00};
88 Error optimizeGOTAndStubAccesses(LinkGraph &G) {
89 LLVM_DEBUG(dbgs() << "Optimizing GOT entries and stubs:\n");
91 for (auto *B : G.blocks())
92 for (auto &E : B->edges()) {
93 if (E.getKind() == x86_64::PCRel32GOTLoadRelaxable ||
94 E.getKind() == x86_64::PCRel32GOTLoadREXRelaxable) {
95 #ifndef NDEBUG
96 bool REXPrefix = E.getKind() == x86_64::PCRel32GOTLoadREXRelaxable;
97 assert(E.getOffset() >= (REXPrefix ? 3u : 2u) &&
98 "GOT edge occurs too early in block");
99 #endif
100 auto *FixupData = reinterpret_cast<uint8_t *>(
101 const_cast<char *>(B->getContent().data())) +
102 E.getOffset();
103 const uint8_t Op = FixupData[-2];
104 const uint8_t ModRM = FixupData[-1];
106 auto &GOTEntryBlock = E.getTarget().getBlock();
107 assert(GOTEntryBlock.getSize() == G.getPointerSize() &&
108 "GOT entry block should be pointer sized");
109 assert(GOTEntryBlock.edges_size() == 1 &&
110 "GOT entry should only have one outgoing edge");
111 auto &GOTTarget = GOTEntryBlock.edges().begin()->getTarget();
112 orc::ExecutorAddr TargetAddr = GOTTarget.getAddress();
113 orc::ExecutorAddr EdgeAddr = B->getFixupAddress(E);
114 int64_t Displacement = TargetAddr - EdgeAddr + 4;
115 bool TargetInRangeForImmU32 = isUInt<32>(TargetAddr.getValue());
116 bool DisplacementInRangeForImmS32 = isInt<32>(Displacement);
118 // If both of the Target and displacement is out of range, then
119 // there isn't optimization chance.
120 if (!(TargetInRangeForImmU32 || DisplacementInRangeForImmS32))
121 continue;
123 // Transform "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
124 if (Op == 0x8b && DisplacementInRangeForImmS32) {
125 FixupData[-2] = 0x8d;
126 E.setKind(x86_64::Delta32);
127 E.setTarget(GOTTarget);
128 E.setAddend(E.getAddend() - 4);
129 LLVM_DEBUG({
130 dbgs() << " Replaced GOT load wih LEA:\n ";
131 printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
132 dbgs() << "\n";
134 continue;
137 // Transform call/jmp instructions
138 if (Op == 0xff && TargetInRangeForImmU32) {
139 if (ModRM == 0x15) {
140 // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call
141 // foo" But lld convert it to "addr32 call foo, because that makes
142 // result expression to be a single instruction.
143 FixupData[-2] = 0x67;
144 FixupData[-1] = 0xe8;
145 LLVM_DEBUG({
146 dbgs() << " replaced call instruction's memory operand wih imm "
147 "operand:\n ";
148 printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
149 dbgs() << "\n";
151 } else {
152 // Transform "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop"
153 assert(ModRM == 0x25 && "Invalid ModRm for call/jmp instructions");
154 FixupData[-2] = 0xe9;
155 FixupData[3] = 0x90;
156 E.setOffset(E.getOffset() - 1);
157 LLVM_DEBUG({
158 dbgs() << " replaced jmp instruction's memory operand wih imm "
159 "operand:\n ";
160 printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
161 dbgs() << "\n";
164 E.setKind(x86_64::Pointer32);
165 E.setTarget(GOTTarget);
166 continue;
168 } else if (E.getKind() == x86_64::BranchPCRel32ToPtrJumpStubBypassable) {
169 auto &StubBlock = E.getTarget().getBlock();
170 assert(StubBlock.getSize() == sizeof(PointerJumpStubContent) &&
171 "Stub block should be stub sized");
172 assert(StubBlock.edges_size() == 1 &&
173 "Stub block should only have one outgoing edge");
175 auto &GOTBlock = StubBlock.edges().begin()->getTarget().getBlock();
176 assert(GOTBlock.getSize() == G.getPointerSize() &&
177 "GOT block should be pointer sized");
178 assert(GOTBlock.edges_size() == 1 &&
179 "GOT block should only have one outgoing edge");
181 auto &GOTTarget = GOTBlock.edges().begin()->getTarget();
182 orc::ExecutorAddr EdgeAddr = B->getAddress() + E.getOffset();
183 orc::ExecutorAddr TargetAddr = GOTTarget.getAddress();
185 int64_t Displacement = TargetAddr - EdgeAddr + 4;
186 if (isInt<32>(Displacement)) {
187 E.setKind(x86_64::BranchPCRel32);
188 E.setTarget(GOTTarget);
189 LLVM_DEBUG({
190 dbgs() << " Replaced stub branch with direct branch:\n ";
191 printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
192 dbgs() << "\n";
198 return Error::success();
201 } // end namespace x86_64
202 } // end namespace jitlink
203 } // end namespace llvm