1*700637cbSDimitry Andric //===- X86SuppressAPXForReloc.cpp - Suppress APX features for relocations -===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric /// \file
9*700637cbSDimitry Andric ///
10*700637cbSDimitry Andric /// This pass is added to suppress APX features for relocations. It's used to
11*700637cbSDimitry Andric /// keep backward compatibility with old version of linker having no APX
12*700637cbSDimitry Andric /// support. It can be removed after APX support is included in the default
13*700637cbSDimitry Andric /// linker on OS.
14*700637cbSDimitry Andric ///
15*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
16*700637cbSDimitry Andric
17*700637cbSDimitry Andric #include "X86.h"
18*700637cbSDimitry Andric #include "X86InstrInfo.h"
19*700637cbSDimitry Andric #include "X86RegisterInfo.h"
20*700637cbSDimitry Andric #include "X86Subtarget.h"
21*700637cbSDimitry Andric
22*700637cbSDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
23*700637cbSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
24*700637cbSDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
25*700637cbSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
26*700637cbSDimitry Andric #include "llvm/CodeGen/Passes.h"
27*700637cbSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
28*700637cbSDimitry Andric #include "llvm/Support/ErrorHandling.h"
29*700637cbSDimitry Andric
30*700637cbSDimitry Andric using namespace llvm;
31*700637cbSDimitry Andric
32*700637cbSDimitry Andric #define DEBUG_TYPE "x86-suppress-apx-for-relocation"
33*700637cbSDimitry Andric
34*700637cbSDimitry Andric cl::opt<bool> X86EnableAPXForRelocation(
35*700637cbSDimitry Andric "x86-enable-apx-for-relocation",
36*700637cbSDimitry Andric cl::desc("Enable APX features (EGPR, NDD and NF) for instructions with "
37*700637cbSDimitry Andric "relocations on x86-64 ELF"),
38*700637cbSDimitry Andric cl::init(false));
39*700637cbSDimitry Andric
40*700637cbSDimitry Andric namespace {
41*700637cbSDimitry Andric class X86SuppressAPXForRelocationPass : public MachineFunctionPass {
42*700637cbSDimitry Andric public:
X86SuppressAPXForRelocationPass()43*700637cbSDimitry Andric X86SuppressAPXForRelocationPass() : MachineFunctionPass(ID) {}
44*700637cbSDimitry Andric
getPassName() const45*700637cbSDimitry Andric StringRef getPassName() const override {
46*700637cbSDimitry Andric return "X86 Suppress APX features for relocation";
47*700637cbSDimitry Andric }
48*700637cbSDimitry Andric
49*700637cbSDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
50*700637cbSDimitry Andric
51*700637cbSDimitry Andric static char ID;
52*700637cbSDimitry Andric };
53*700637cbSDimitry Andric } // namespace
54*700637cbSDimitry Andric
55*700637cbSDimitry Andric char X86SuppressAPXForRelocationPass::ID = 0;
56*700637cbSDimitry Andric
57*700637cbSDimitry Andric INITIALIZE_PASS_BEGIN(X86SuppressAPXForRelocationPass, DEBUG_TYPE,
58*700637cbSDimitry Andric "X86 Suppress APX features for relocation", false, false)
59*700637cbSDimitry Andric INITIALIZE_PASS_END(X86SuppressAPXForRelocationPass, DEBUG_TYPE,
60*700637cbSDimitry Andric "X86 Suppress APX features for relocation", false, false)
61*700637cbSDimitry Andric
createX86SuppressAPXForRelocationPass()62*700637cbSDimitry Andric FunctionPass *llvm::createX86SuppressAPXForRelocationPass() {
63*700637cbSDimitry Andric return new X86SuppressAPXForRelocationPass();
64*700637cbSDimitry Andric }
65*700637cbSDimitry Andric
suppressEGPRRegClass(MachineRegisterInfo * MRI,MachineInstr & MI,const X86Subtarget & ST,unsigned int OpNum)66*700637cbSDimitry Andric static void suppressEGPRRegClass(MachineRegisterInfo *MRI, MachineInstr &MI,
67*700637cbSDimitry Andric const X86Subtarget &ST, unsigned int OpNum) {
68*700637cbSDimitry Andric Register Reg = MI.getOperand(OpNum).getReg();
69*700637cbSDimitry Andric if (!Reg.isVirtual()) {
70*700637cbSDimitry Andric assert(!X86II::isApxExtendedReg(Reg) && "APX EGPR is used unexpectedly.");
71*700637cbSDimitry Andric return;
72*700637cbSDimitry Andric }
73*700637cbSDimitry Andric const TargetRegisterClass *RC = MRI->getRegClass(Reg);
74*700637cbSDimitry Andric const X86RegisterInfo *RI = ST.getRegisterInfo();
75*700637cbSDimitry Andric const TargetRegisterClass *NewRC = RI->constrainRegClassToNonRex2(RC);
76*700637cbSDimitry Andric MRI->setRegClass(Reg, NewRC);
77*700637cbSDimitry Andric }
78*700637cbSDimitry Andric
79*700637cbSDimitry Andric // Suppress EGPR in operand 0 of uses to avoid APX relocation types emitted. The
80*700637cbSDimitry Andric // register in operand 0 of instruction with relocation may be replaced with
81*700637cbSDimitry Andric // operand 0 of uses which may be EGPR. That may lead to emit APX relocation
82*700637cbSDimitry Andric // types which breaks the backward compatibility with builtin linkers on
83*700637cbSDimitry Andric // existing OS. For example, the register in operand 0 of instruction with
84*700637cbSDimitry Andric // relocation is used in PHI instruction, and it may be replaced with operand 0
85*700637cbSDimitry Andric // of PHI instruction after PHI elimination and Machine Copy Propagation pass.
suppressEGPRRegClassInRegAndUses(MachineRegisterInfo * MRI,MachineInstr & MI,const X86Subtarget & ST,unsigned int OpNum)86*700637cbSDimitry Andric static void suppressEGPRRegClassInRegAndUses(MachineRegisterInfo *MRI,
87*700637cbSDimitry Andric MachineInstr &MI,
88*700637cbSDimitry Andric const X86Subtarget &ST,
89*700637cbSDimitry Andric unsigned int OpNum) {
90*700637cbSDimitry Andric suppressEGPRRegClass(MRI, MI, ST, OpNum);
91*700637cbSDimitry Andric Register Reg = MI.getOperand(OpNum).getReg();
92*700637cbSDimitry Andric for (MachineInstr &Use : MRI->use_instructions(Reg))
93*700637cbSDimitry Andric if (Use.getOpcode() == X86::PHI)
94*700637cbSDimitry Andric suppressEGPRRegClass(MRI, Use, ST, 0);
95*700637cbSDimitry Andric }
96*700637cbSDimitry Andric
handleInstructionWithEGPR(MachineFunction & MF,const X86Subtarget & ST)97*700637cbSDimitry Andric static bool handleInstructionWithEGPR(MachineFunction &MF,
98*700637cbSDimitry Andric const X86Subtarget &ST) {
99*700637cbSDimitry Andric if (!ST.hasEGPR())
100*700637cbSDimitry Andric return false;
101*700637cbSDimitry Andric
102*700637cbSDimitry Andric MachineRegisterInfo *MRI = &MF.getRegInfo();
103*700637cbSDimitry Andric auto suppressEGPRInInstrWithReloc = [&](MachineInstr &MI,
104*700637cbSDimitry Andric ArrayRef<unsigned> OpNoArray) {
105*700637cbSDimitry Andric int MemOpNo = X86II::getMemoryOperandNo(MI.getDesc().TSFlags) +
106*700637cbSDimitry Andric X86II::getOperandBias(MI.getDesc());
107*700637cbSDimitry Andric const MachineOperand &MO = MI.getOperand(X86::AddrDisp + MemOpNo);
108*700637cbSDimitry Andric if (MO.getTargetFlags() == X86II::MO_GOTTPOFF ||
109*700637cbSDimitry Andric MO.getTargetFlags() == X86II::MO_GOTPCREL) {
110*700637cbSDimitry Andric LLVM_DEBUG(dbgs() << "Transform instruction with relocation type:\n "
111*700637cbSDimitry Andric << MI);
112*700637cbSDimitry Andric for (unsigned OpNo : OpNoArray)
113*700637cbSDimitry Andric suppressEGPRRegClassInRegAndUses(MRI, MI, ST, OpNo);
114*700637cbSDimitry Andric LLVM_DEBUG(dbgs() << "to:\n " << MI << "\n");
115*700637cbSDimitry Andric }
116*700637cbSDimitry Andric };
117*700637cbSDimitry Andric
118*700637cbSDimitry Andric for (MachineBasicBlock &MBB : MF) {
119*700637cbSDimitry Andric for (MachineInstr &MI : MBB) {
120*700637cbSDimitry Andric unsigned Opcode = MI.getOpcode();
121*700637cbSDimitry Andric switch (Opcode) {
122*700637cbSDimitry Andric // For GOTPC32_TLSDESC, it's emitted with physical register (EAX/RAX) in
123*700637cbSDimitry Andric // X86AsmPrinter::LowerTlsAddr, and there is no corresponding target
124*700637cbSDimitry Andric // flag for it, so we don't need to handle LEA64r with TLSDESC and EGPR
125*700637cbSDimitry Andric // in this pass (before emitting assembly).
126*700637cbSDimitry Andric case X86::TEST32mr:
127*700637cbSDimitry Andric case X86::TEST64mr: {
128*700637cbSDimitry Andric suppressEGPRInInstrWithReloc(MI, {5});
129*700637cbSDimitry Andric break;
130*700637cbSDimitry Andric }
131*700637cbSDimitry Andric case X86::CMP32rm:
132*700637cbSDimitry Andric case X86::CMP64rm:
133*700637cbSDimitry Andric case X86::MOV32rm:
134*700637cbSDimitry Andric case X86::MOV64rm: {
135*700637cbSDimitry Andric suppressEGPRInInstrWithReloc(MI, {0});
136*700637cbSDimitry Andric break;
137*700637cbSDimitry Andric }
138*700637cbSDimitry Andric case X86::ADC32rm:
139*700637cbSDimitry Andric case X86::ADD32rm:
140*700637cbSDimitry Andric case X86::AND32rm:
141*700637cbSDimitry Andric case X86::OR32rm:
142*700637cbSDimitry Andric case X86::SBB32rm:
143*700637cbSDimitry Andric case X86::SUB32rm:
144*700637cbSDimitry Andric case X86::XOR32rm:
145*700637cbSDimitry Andric case X86::ADC64rm:
146*700637cbSDimitry Andric case X86::ADD64rm:
147*700637cbSDimitry Andric case X86::AND64rm:
148*700637cbSDimitry Andric case X86::OR64rm:
149*700637cbSDimitry Andric case X86::SBB64rm:
150*700637cbSDimitry Andric case X86::SUB64rm:
151*700637cbSDimitry Andric case X86::XOR64rm: {
152*700637cbSDimitry Andric suppressEGPRInInstrWithReloc(MI, {0, 1});
153*700637cbSDimitry Andric break;
154*700637cbSDimitry Andric }
155*700637cbSDimitry Andric }
156*700637cbSDimitry Andric }
157*700637cbSDimitry Andric }
158*700637cbSDimitry Andric return true;
159*700637cbSDimitry Andric }
160*700637cbSDimitry Andric
handleNDDOrNFInstructions(MachineFunction & MF,const X86Subtarget & ST)161*700637cbSDimitry Andric static bool handleNDDOrNFInstructions(MachineFunction &MF,
162*700637cbSDimitry Andric const X86Subtarget &ST) {
163*700637cbSDimitry Andric if (!ST.hasNDD() && !ST.hasNF())
164*700637cbSDimitry Andric return false;
165*700637cbSDimitry Andric
166*700637cbSDimitry Andric const X86InstrInfo *TII = ST.getInstrInfo();
167*700637cbSDimitry Andric MachineRegisterInfo *MRI = &MF.getRegInfo();
168*700637cbSDimitry Andric for (MachineBasicBlock &MBB : MF) {
169*700637cbSDimitry Andric for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
170*700637cbSDimitry Andric unsigned Opcode = MI.getOpcode();
171*700637cbSDimitry Andric switch (Opcode) {
172*700637cbSDimitry Andric case X86::ADD64rm_NF:
173*700637cbSDimitry Andric case X86::ADD64mr_NF_ND:
174*700637cbSDimitry Andric case X86::ADD64rm_NF_ND: {
175*700637cbSDimitry Andric int MemOpNo = X86II::getMemoryOperandNo(MI.getDesc().TSFlags) +
176*700637cbSDimitry Andric X86II::getOperandBias(MI.getDesc());
177*700637cbSDimitry Andric const MachineOperand &MO = MI.getOperand(X86::AddrDisp + MemOpNo);
178*700637cbSDimitry Andric if (MO.getTargetFlags() == X86II::MO_GOTTPOFF)
179*700637cbSDimitry Andric llvm_unreachable("Unexpected NF instruction!");
180*700637cbSDimitry Andric break;
181*700637cbSDimitry Andric }
182*700637cbSDimitry Andric case X86::ADD64rm_ND: {
183*700637cbSDimitry Andric int MemOpNo = X86II::getMemoryOperandNo(MI.getDesc().TSFlags) +
184*700637cbSDimitry Andric X86II::getOperandBias(MI.getDesc());
185*700637cbSDimitry Andric const MachineOperand &MO = MI.getOperand(X86::AddrDisp + MemOpNo);
186*700637cbSDimitry Andric if (MO.getTargetFlags() == X86II::MO_GOTTPOFF ||
187*700637cbSDimitry Andric MO.getTargetFlags() == X86II::MO_GOTPCREL) {
188*700637cbSDimitry Andric LLVM_DEBUG(dbgs() << "Transform instruction with relocation type:\n "
189*700637cbSDimitry Andric << MI);
190*700637cbSDimitry Andric Register Reg = MRI->createVirtualRegister(&X86::GR64_NOREX2RegClass);
191*700637cbSDimitry Andric [[maybe_unused]] MachineInstrBuilder CopyMIB =
192*700637cbSDimitry Andric BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(TargetOpcode::COPY),
193*700637cbSDimitry Andric Reg)
194*700637cbSDimitry Andric .addReg(MI.getOperand(1).getReg());
195*700637cbSDimitry Andric MI.getOperand(1).setReg(Reg);
196*700637cbSDimitry Andric const MCInstrDesc &NewDesc = TII->get(X86::ADD64rm);
197*700637cbSDimitry Andric MI.setDesc(NewDesc);
198*700637cbSDimitry Andric suppressEGPRRegClassInRegAndUses(MRI, MI, ST, 0);
199*700637cbSDimitry Andric MI.tieOperands(0, 1);
200*700637cbSDimitry Andric LLVM_DEBUG(dbgs() << "to:\n " << *CopyMIB << "\n");
201*700637cbSDimitry Andric LLVM_DEBUG(dbgs() << " " << MI << "\n");
202*700637cbSDimitry Andric }
203*700637cbSDimitry Andric break;
204*700637cbSDimitry Andric }
205*700637cbSDimitry Andric case X86::ADD64mr_ND: {
206*700637cbSDimitry Andric int MemRefBegin = X86II::getMemoryOperandNo(MI.getDesc().TSFlags);
207*700637cbSDimitry Andric const MachineOperand &MO = MI.getOperand(MemRefBegin + X86::AddrDisp);
208*700637cbSDimitry Andric if (MO.getTargetFlags() == X86II::MO_GOTTPOFF) {
209*700637cbSDimitry Andric LLVM_DEBUG(dbgs() << "Transform instruction with relocation type:\n "
210*700637cbSDimitry Andric << MI);
211*700637cbSDimitry Andric suppressEGPRRegClassInRegAndUses(MRI, MI, ST, 0);
212*700637cbSDimitry Andric Register Reg = MRI->createVirtualRegister(&X86::GR64_NOREX2RegClass);
213*700637cbSDimitry Andric [[maybe_unused]] MachineInstrBuilder CopyMIB =
214*700637cbSDimitry Andric BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(TargetOpcode::COPY),
215*700637cbSDimitry Andric Reg)
216*700637cbSDimitry Andric .addReg(MI.getOperand(6).getReg());
217*700637cbSDimitry Andric MachineInstrBuilder NewMIB =
218*700637cbSDimitry Andric BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(X86::ADD64rm),
219*700637cbSDimitry Andric MI.getOperand(0).getReg())
220*700637cbSDimitry Andric .addReg(Reg)
221*700637cbSDimitry Andric .addReg(MI.getOperand(1).getReg())
222*700637cbSDimitry Andric .addImm(MI.getOperand(2).getImm())
223*700637cbSDimitry Andric .addReg(MI.getOperand(3).getReg())
224*700637cbSDimitry Andric .add(MI.getOperand(4))
225*700637cbSDimitry Andric .addReg(MI.getOperand(5).getReg());
226*700637cbSDimitry Andric MachineOperand *FlagDef =
227*700637cbSDimitry Andric MI.findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
228*700637cbSDimitry Andric if (FlagDef && FlagDef->isDead()) {
229*700637cbSDimitry Andric MachineOperand *NewFlagDef =
230*700637cbSDimitry Andric NewMIB->findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);
231*700637cbSDimitry Andric if (NewFlagDef)
232*700637cbSDimitry Andric NewFlagDef->setIsDead();
233*700637cbSDimitry Andric }
234*700637cbSDimitry Andric MI.eraseFromParent();
235*700637cbSDimitry Andric LLVM_DEBUG(dbgs() << "to:\n " << *CopyMIB << "\n");
236*700637cbSDimitry Andric LLVM_DEBUG(dbgs() << " " << *NewMIB << "\n");
237*700637cbSDimitry Andric }
238*700637cbSDimitry Andric break;
239*700637cbSDimitry Andric }
240*700637cbSDimitry Andric }
241*700637cbSDimitry Andric }
242*700637cbSDimitry Andric }
243*700637cbSDimitry Andric return true;
244*700637cbSDimitry Andric }
245*700637cbSDimitry Andric
runOnMachineFunction(MachineFunction & MF)246*700637cbSDimitry Andric bool X86SuppressAPXForRelocationPass::runOnMachineFunction(
247*700637cbSDimitry Andric MachineFunction &MF) {
248*700637cbSDimitry Andric if (X86EnableAPXForRelocation)
249*700637cbSDimitry Andric return false;
250*700637cbSDimitry Andric const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
251*700637cbSDimitry Andric bool Changed = handleInstructionWithEGPR(MF, ST);
252*700637cbSDimitry Andric Changed |= handleNDDOrNFInstructions(MF, ST);
253*700637cbSDimitry Andric
254*700637cbSDimitry Andric return Changed;
255*700637cbSDimitry Andric }
256