LLVM  14.0.0git
x86_64.cpp
Go to the documentation of this file.
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.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 
15 #define DEBUG_TYPE "jitlink"
16 
17 namespace llvm {
18 namespace jitlink {
19 namespace x86_64 {
20 
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 Delta64:
30  return "Delta64";
31  case Delta32:
32  return "Delta32";
33  case NegDelta64:
34  return "NegDelta64";
35  case NegDelta32:
36  return "NegDelta32";
37  case Delta64FromGOT:
38  return "Delta64FromGOT";
39  case BranchPCRel32:
40  return "BranchPCRel32";
42  return "BranchPCRel32ToPtrJumpStub";
44  return "BranchPCRel32ToPtrJumpStubBypassable";
46  return "RequestGOTAndTransformToDelta32";
48  return "RequestGOTAndTransformToDelta64";
50  return "RequestGOTAndTransformToDelta64FromGOT";
52  return "PCRel32GOTLoadREXRelaxable";
54  return "RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable";
56  return "PCRel32GOTLoadRelaxable";
58  return "RequestGOTAndTransformToPCRel32GOTLoadRelaxable";
60  return "PCRel32TLVPLoadREXRelaxable";
62  return "RequestTLVPAndTransformToPCRel32TLVPLoadREXRelaxable";
63  default:
64  return getGenericEdgeKindName(static_cast<Edge::Kind>(K));
65  }
66 }
67 
68 const char NullPointerContent[PointerSize] = {0x00, 0x00, 0x00, 0x00,
69  0x00, 0x00, 0x00, 0x00};
70 
71 const char PointerJumpStubContent[6] = {
72  static_cast<char>(0xFFu), 0x25, 0x00, 0x00, 0x00, 0x00};
73 
75  LLVM_DEBUG(dbgs() << "Optimizing GOT entries and stubs:\n");
76 
77  for (auto *B : G.blocks())
78  for (auto &E : B->edges()) {
79  if (E.getKind() == x86_64::PCRel32GOTLoadRelaxable ||
81 #ifndef NDEBUG
82  bool REXPrefix = E.getKind() == x86_64::PCRel32GOTLoadREXRelaxable;
83  assert(E.getOffset() >= (REXPrefix ? 3u : 2u) &&
84  "GOT edge occurs too early in block");
85 #endif
86  auto *FixupData = reinterpret_cast<uint8_t *>(
87  const_cast<char *>(B->getContent().data())) +
88  E.getOffset();
89  const uint8_t Op = FixupData[-2];
90  const uint8_t ModRM = FixupData[-1];
91 
92  auto &GOTEntryBlock = E.getTarget().getBlock();
93  assert(GOTEntryBlock.getSize() == G.getPointerSize() &&
94  "GOT entry block should be pointer sized");
95  assert(GOTEntryBlock.edges_size() == 1 &&
96  "GOT entry should only have one outgoing edge");
97  auto &GOTTarget = GOTEntryBlock.edges().begin()->getTarget();
98  JITTargetAddress TargetAddr = GOTTarget.getAddress();
99  JITTargetAddress EdgeAddr = B->getFixupAddress(E);
100  int64_t Displacement = TargetAddr - EdgeAddr + 4;
101  bool TargetInRangeForImmU32 = isInRangeForImmU32(TargetAddr);
102  bool DisplacementInRangeForImmS32 = isInRangeForImmS32(Displacement);
103 
104  // If both of the Target and displacement is out of range, then
105  // there isn't optimization chance.
106  if (!(TargetInRangeForImmU32 || DisplacementInRangeForImmS32))
107  continue;
108 
109  // Transform "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
110  if (Op == 0x8b && DisplacementInRangeForImmS32) {
111  FixupData[-2] = 0x8d;
112  E.setKind(x86_64::Delta32);
113  E.setTarget(GOTTarget);
114  E.setAddend(E.getAddend() - 4);
115  LLVM_DEBUG({
116  dbgs() << " Replaced GOT load wih LEA:\n ";
117  printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
118  dbgs() << "\n";
119  });
120  continue;
121  }
122 
123  // Transform call/jmp instructions
124  if (Op == 0xff && TargetInRangeForImmU32) {
125  if (ModRM == 0x15) {
126  // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call
127  // foo" But lld convert it to "addr32 call foo, because that makes
128  // result expression to be a single instruction.
129  FixupData[-2] = 0x67;
130  FixupData[-1] = 0xe8;
131  LLVM_DEBUG({
132  dbgs() << " replaced call instruction's memory operand wih imm "
133  "operand:\n ";
134  printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
135  dbgs() << "\n";
136  });
137  } else {
138  // Transform "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop"
139  assert(ModRM == 0x25 && "Invalid ModRm for call/jmp instructions");
140  FixupData[-2] = 0xe9;
141  FixupData[3] = 0x90;
142  E.setOffset(E.getOffset() - 1);
143  LLVM_DEBUG({
144  dbgs() << " replaced jmp instruction's memory operand wih imm "
145  "operand:\n ";
146  printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
147  dbgs() << "\n";
148  });
149  }
150  E.setKind(x86_64::Pointer32);
151  E.setTarget(GOTTarget);
152  continue;
153  }
154  } else if (E.getKind() == x86_64::BranchPCRel32ToPtrJumpStubBypassable) {
155  auto &StubBlock = E.getTarget().getBlock();
156  assert(StubBlock.getSize() == sizeof(PointerJumpStubContent) &&
157  "Stub block should be stub sized");
158  assert(StubBlock.edges_size() == 1 &&
159  "Stub block should only have one outgoing edge");
160 
161  auto &GOTBlock = StubBlock.edges().begin()->getTarget().getBlock();
162  assert(GOTBlock.getSize() == G.getPointerSize() &&
163  "GOT block should be pointer sized");
164  assert(GOTBlock.edges_size() == 1 &&
165  "GOT block should only have one outgoing edge");
166 
167  auto &GOTTarget = GOTBlock.edges().begin()->getTarget();
168  JITTargetAddress EdgeAddr = B->getAddress() + E.getOffset();
169  JITTargetAddress TargetAddr = GOTTarget.getAddress();
170 
171  int64_t Displacement = TargetAddr - EdgeAddr + 4;
172  if (isInRangeForImmS32(Displacement)) {
173  E.setKind(x86_64::BranchPCRel32);
174  E.setTarget(GOTTarget);
175  LLVM_DEBUG({
176  dbgs() << " Replaced stub branch with direct branch:\n ";
177  printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
178  dbgs() << "\n";
179  });
180  }
181  }
182  }
183 
184  return Error::success();
185 }
186 
187 } // end namespace x86_64
188 } // end namespace jitlink
189 } // end namespace llvm
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
uint64_t
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
x86_64.h
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:321
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157