LLVM 24.0.0git
X86SuppressAPXForReloc.cpp
Go to the documentation of this file.
1//===- X86SuppressAPXForReloc.cpp - Suppress APX features for relocations -===//
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/// \file
9///
10/// This pass is added to suppress APX features for relocations. It's used to
11/// keep backward compatibility with old version of linker having no APX
12/// support. It can be removed after APX support is included in the default
13/// linker on OS.
14///
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
18#include "X86InstrInfo.h"
19#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
21
26#include "llvm/CodeGen/Passes.h"
30
31using namespace llvm;
32
33#define DEBUG_TYPE "x86-suppress-apx-for-relocation"
34
36 "x86-enable-apx-for-relocation",
37 cl::desc("Enable APX features (EGPR, NDD and NF) for instructions with "
38 "relocations on x86-64 ELF"),
39 cl::init(false));
40
41namespace {
42class X86SuppressAPXForRelocationLegacy : public MachineFunctionPass {
43public:
44 X86SuppressAPXForRelocationLegacy() : MachineFunctionPass(ID) {}
45
46 StringRef getPassName() const override {
47 return "X86 Suppress APX features for relocation";
48 }
49
50 bool runOnMachineFunction(MachineFunction &MF) override;
51
52 void getAnalysisUsage(AnalysisUsage &AU) const override {
53 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
55 }
56
57 static char ID;
58};
59} // namespace
60
61char X86SuppressAPXForRelocationLegacy::ID = 0;
62
63INITIALIZE_PASS_BEGIN(X86SuppressAPXForRelocationLegacy, DEBUG_TYPE,
64 "X86 Suppress APX features for relocation", false, false)
65INITIALIZE_PASS_END(X86SuppressAPXForRelocationLegacy, DEBUG_TYPE,
66 "X86 Suppress APX features for relocation", false, false)
67
69 return new X86SuppressAPXForRelocationLegacy();
70}
71
73 const X86Subtarget &ST, unsigned int OpNum) {
74 Register Reg = MI.getOperand(OpNum).getReg();
75 if (!Reg.isVirtual()) {
76 assert(!X86II::isApxExtendedReg(Reg) && "APX EGPR is used unexpectedly.");
77 return;
78 }
79 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
80 const X86RegisterInfo *RI = ST.getRegisterInfo();
82 MRI->setRegClass(Reg, NewRC);
83}
84
85// Suppress EGPR in operand 0 of uses to avoid APX relocation types emitted. The
86// register in operand 0 of instruction with relocation may be replaced with
87// operand 0 of uses which may be EGPR. That may lead to emit APX relocation
88// types which breaks the backward compatibility with builtin linkers on
89// existing OS. For example, the register in operand 0 of instruction with
90// relocation is used in PHI instruction, and it may be replaced with operand 0
91// of PHI instruction after PHI elimination and Machine Copy Propagation pass.
94 const X86Subtarget &ST,
95 unsigned int OpNum) {
96 suppressEGPRRegClass(MRI, MI, ST, OpNum);
97 Register Reg = MI.getOperand(OpNum).getReg();
99 if (Use.getOpcode() == X86::PHI)
100 suppressEGPRRegClass(MRI, Use, ST, 0);
101}
102
104 const X86Subtarget &ST) {
105 if (!ST.hasEGPR())
106 return false;
107
108 MachineRegisterInfo *MRI = &MF.getRegInfo();
109 auto suppressEGPRInInstrWithReloc = [&](MachineInstr &MI,
110 ArrayRef<unsigned> OpNoArray) {
111 int MemOpNo = X86II::getMemoryOperandNo(MI.getDesc().TSFlags) +
112 X86II::getOperandBias(MI.getDesc());
113 const MachineOperand &MO = MI.getOperand(X86::AddrDisp + MemOpNo);
116 LLVM_DEBUG(dbgs() << "Transform instruction with relocation type:\n "
117 << MI);
118 for (unsigned OpNo : OpNoArray)
119 suppressEGPRRegClassInRegAndUses(MRI, MI, ST, OpNo);
120 LLVM_DEBUG(dbgs() << "to:\n " << MI << "\n");
121 }
122 };
123
124 for (MachineBasicBlock &MBB : MF) {
125 for (MachineInstr &MI : MBB) {
126 unsigned Opcode = MI.getOpcode();
127 switch (Opcode) {
128 // For GOTPC32_TLSDESC, it's emitted with physical register (EAX/RAX) in
129 // X86AsmPrinter::LowerTlsAddr, and there is no corresponding target
130 // flag for it, so we don't need to handle LEA64r with TLSDESC and EGPR
131 // in this pass (before emitting assembly).
132 case X86::TEST32mr:
133 case X86::TEST64mr: {
134 suppressEGPRInInstrWithReloc(MI, {5});
135 break;
136 }
137 case X86::CMP32rm:
138 case X86::CMP64rm:
139 case X86::MOV32rm:
140 case X86::MOV64rm: {
141 suppressEGPRInInstrWithReloc(MI, {0});
142 break;
143 }
144 case X86::ADC32rm:
145 case X86::ADD32rm:
146 case X86::AND32rm:
147 case X86::OR32rm:
148 case X86::SBB32rm:
149 case X86::SUB32rm:
150 case X86::XOR32rm:
151 case X86::ADC64rm:
152 case X86::ADD64rm:
153 case X86::AND64rm:
154 case X86::OR64rm:
155 case X86::SBB64rm:
156 case X86::SUB64rm:
157 case X86::XOR64rm: {
158 suppressEGPRInInstrWithReloc(MI, {0, 1});
159 break;
160 }
161 }
162 }
163 }
164 return true;
165}
166
168 const X86Subtarget &ST) {
169 if (!ST.hasNDD() && !ST.hasNF())
170 return false;
171
172 const X86InstrInfo *TII = ST.getInstrInfo();
173 MachineRegisterInfo *MRI = &MF.getRegInfo();
174 for (MachineBasicBlock &MBB : MF) {
176 unsigned Opcode = MI.getOpcode();
177 switch (Opcode) {
178 case X86::ADD64rm_NF:
179 case X86::ADD64mr_NF_ND:
180 case X86::ADD64rm_NF_ND: {
181 int MemOpNo = X86II::getMemoryOperandNo(MI.getDesc().TSFlags) +
182 X86II::getOperandBias(MI.getDesc());
183 const MachineOperand &MO = MI.getOperand(X86::AddrDisp + MemOpNo);
185 llvm_unreachable("Unexpected NF instruction!");
186 break;
187 }
188 case X86::ADD64rm_ND: {
189 int MemOpNo = X86II::getMemoryOperandNo(MI.getDesc().TSFlags) +
190 X86II::getOperandBias(MI.getDesc());
191 const MachineOperand &MO = MI.getOperand(X86::AddrDisp + MemOpNo);
194 LLVM_DEBUG(dbgs() << "Transform instruction with relocation type:\n "
195 << MI);
196 Register Reg = MRI->createVirtualRegister(&X86::GR64_NOREX2RegClass);
197 [[maybe_unused]] MachineInstrBuilder CopyMIB =
198 BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(TargetOpcode::COPY),
199 Reg)
200 .addReg(MI.getOperand(1).getReg());
201 MI.getOperand(1).setReg(Reg);
202 const MCInstrDesc &NewDesc = TII->get(X86::ADD64rm);
203 MI.setDesc(NewDesc);
205 MI.tieOperands(0, 1);
206 LLVM_DEBUG(dbgs() << "to:\n " << *CopyMIB << "\n");
207 LLVM_DEBUG(dbgs() << " " << MI << "\n");
208 }
209 break;
210 }
211 case X86::ADD64mr_ND: {
212 int MemRefBegin = X86II::getMemoryOperandNo(MI.getDesc().TSFlags);
213 const MachineOperand &MO = MI.getOperand(MemRefBegin + X86::AddrDisp);
215 LLVM_DEBUG(dbgs() << "Transform instruction with relocation type:\n "
216 << MI);
218 Register Reg = MRI->createVirtualRegister(&X86::GR64_NOREX2RegClass);
219 [[maybe_unused]] MachineInstrBuilder CopyMIB =
220 BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(TargetOpcode::COPY),
221 Reg)
222 .addReg(MI.getOperand(6).getReg());
223 MachineInstrBuilder NewMIB =
224 BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(X86::ADD64rm),
225 MI.getOperand(0).getReg())
226 .addReg(Reg)
227 .addReg(MI.getOperand(1).getReg())
228 .addImm(MI.getOperand(2).getImm())
229 .addReg(MI.getOperand(3).getReg())
230 .add(MI.getOperand(4))
231 .addReg(MI.getOperand(5).getReg());
232 MachineOperand *FlagDef =
233 MI.findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
234 if (FlagDef && FlagDef->isDead()) {
235 MachineOperand *NewFlagDef =
236 NewMIB->findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
237 if (NewFlagDef)
238 NewFlagDef->setIsDead();
239 }
240 MI.eraseFromParent();
241 LLVM_DEBUG(dbgs() << "to:\n " << *CopyMIB << "\n");
242 LLVM_DEBUG(dbgs() << " " << *NewMIB << "\n");
243 }
244 break;
245 }
246 }
247 }
248 }
249 return true;
250}
251
254 return false;
255 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
256 bool Changed = handleInstructionWithEGPR(MF, ST);
258
259 return Changed;
260}
261
262bool X86SuppressAPXForRelocationLegacy::runOnMachineFunction(
263 MachineFunction &MF) {
264 return suppressAPXForRelocation(MF);
265}
266
267PreservedAnalyses
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Register Reg
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
#define LLVM_DEBUG(...)
Definition Debug.h:119
cl::opt< bool > X86EnableAPXForRelocation
static bool handleInstructionWithEGPR(MachineFunction &MF, const X86Subtarget &ST)
static bool suppressAPXForRelocation(MachineFunction &MF)
cl::opt< bool > X86EnableAPXForRelocation("x86-enable-apx-for-relocation", cl::desc("Enable APX features (EGPR, NDD and NF) for instructions with " "relocations on x86-64 ELF"), cl::init(false))
static void suppressEGPRRegClass(MachineRegisterInfo *MRI, MachineInstr &MI, const X86Subtarget &ST, unsigned int OpNum)
static void suppressEGPRRegClassInRegAndUses(MachineRegisterInfo *MRI, MachineInstr &MI, const X86Subtarget &ST, unsigned int OpNum)
static bool handleNDDOrNFInstructions(MachineFunction &MF, const X86Subtarget &ST)
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Describe properties that are true of each instruction in the target description file.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
Representation of each machine instruction.
MachineOperand * findRegisterDefOperand(Register Reg, const TargetRegisterInfo *TRI, bool isDead=false, bool Overlap=false)
Wrapper for findRegisterDefOperandIdx, it returns a pointer to the MachineOperand rather than an inde...
MachineOperand class - Representation of each machine instruction operand.
void setIsDead(bool Val=true)
unsigned getTargetFlags() const
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLVM_ABI void setRegClass(Register Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
iterator_range< use_instr_iterator > use_instructions(Register Reg) const
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Wrapper class representing virtual and physical registers.
Definition Register.h:20
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
const TargetRegisterClass * constrainRegClassToNonRex2(const TargetRegisterClass *RC) const
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ MO_GOTTPOFF
MO_GOTTPOFF - On a symbol operand this indicates that the immediate is the offset of the GOT entry wi...
@ MO_GOTPCREL
MO_GOTPCREL - On a symbol operand this indicates that the immediate is offset to the GOT entry for th...
int getMemoryOperandNo(uint64_t TSFlags)
bool isApxExtendedReg(MCRegister Reg)
unsigned getOperandBias(const MCInstrDesc &Desc)
Compute whether all of the def operands are repeated in the uses and therefore should be skipped.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
FunctionPass * createX86SuppressAPXForRelocationLegacyPass()
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58