blob: ab2ef26d1cc91c898a1a29902894ec9367ec6e4d [file] [log] [blame]
Eric Christopher213a5da2015-12-21 23:04:27 +00001//===------- X86ExpandPseudo.cpp - Expand pseudo instructions -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a pass that expands pseudo instructions into target
11// instructions to allow proper scheduling, if-conversion, other late
12// optimizations, or simply the encoding of the instructions.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86.h"
17#include "X86FrameLowering.h"
18#include "X86InstrBuilder.h"
19#include "X86InstrInfo.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86Subtarget.h"
22#include "llvm/Analysis/EHPersonalities.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/Passes.h" // For IDs of passes that are preserved.
26#include "llvm/IR/GlobalValue.h"
27using namespace llvm;
28
29#define DEBUG_TYPE "x86-pseudo"
30
31namespace {
32class X86ExpandPseudo : public MachineFunctionPass {
33public:
34 static char ID;
35 X86ExpandPseudo() : MachineFunctionPass(ID) {}
36
37 void getAnalysisUsage(AnalysisUsage &AU) const override {
38 AU.setPreservesCFG();
39 AU.addPreservedID(MachineLoopInfoID);
40 AU.addPreservedID(MachineDominatorsID);
41 MachineFunctionPass::getAnalysisUsage(AU);
42 }
43
44 const X86Subtarget *STI;
45 const X86InstrInfo *TII;
46 const X86RegisterInfo *TRI;
Quentin Colombetfb82c7b2016-07-11 21:03:03 +000047 const X86MachineFunctionInfo *X86FI;
Eric Christopher213a5da2015-12-21 23:04:27 +000048 const X86FrameLowering *X86FL;
49
50 bool runOnMachineFunction(MachineFunction &Fn) override;
51
Derek Schuff1dbf7a52016-04-04 17:09:25 +000052 MachineFunctionProperties getRequiredProperties() const override {
53 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000054 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000055 }
56
Mehdi Amini117296c2016-10-01 02:56:57 +000057 StringRef getPassName() const override {
Eric Christopher213a5da2015-12-21 23:04:27 +000058 return "X86 pseudo instruction expansion pass";
59 }
60
61private:
62 bool ExpandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI);
63 bool ExpandMBB(MachineBasicBlock &MBB);
64};
65char X86ExpandPseudo::ID = 0;
66} // End anonymous namespace.
67
68/// If \p MBBI is a pseudo instruction, this method expands
69/// it to the corresponding (sequence of) actual instruction(s).
70/// \returns true if \p MBBI has been expanded.
71bool X86ExpandPseudo::ExpandMI(MachineBasicBlock &MBB,
72 MachineBasicBlock::iterator MBBI) {
73 MachineInstr &MI = *MBBI;
74 unsigned Opcode = MI.getOpcode();
75 DebugLoc DL = MBBI->getDebugLoc();
76 switch (Opcode) {
77 default:
78 return false;
79 case X86::TCRETURNdi:
Hans Wennborga4686012017-02-16 00:04:05 +000080 case X86::TCRETURNdicc:
Eric Christopher213a5da2015-12-21 23:04:27 +000081 case X86::TCRETURNri:
82 case X86::TCRETURNmi:
83 case X86::TCRETURNdi64:
Hans Wennborga4686012017-02-16 00:04:05 +000084 case X86::TCRETURNdi64cc:
Eric Christopher213a5da2015-12-21 23:04:27 +000085 case X86::TCRETURNri64:
86 case X86::TCRETURNmi64: {
87 bool isMem = Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64;
88 MachineOperand &JumpTarget = MBBI->getOperand(0);
89 MachineOperand &StackAdjust = MBBI->getOperand(isMem ? 5 : 1);
90 assert(StackAdjust.isImm() && "Expecting immediate value.");
91
92 // Adjust stack pointer.
93 int StackAdj = StackAdjust.getImm();
Quentin Colombetfb82c7b2016-07-11 21:03:03 +000094 int MaxTCDelta = X86FI->getTCReturnAddrDelta();
95 int Offset = 0;
96 assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive");
Eric Christopher213a5da2015-12-21 23:04:27 +000097
Quentin Colombetfb82c7b2016-07-11 21:03:03 +000098 // Incoporate the retaddr area.
Hans Wennborg75e25f62016-09-07 17:52:14 +000099 Offset = StackAdj - MaxTCDelta;
Quentin Colombetfb82c7b2016-07-11 21:03:03 +0000100 assert(Offset >= 0 && "Offset should never be negative");
101
Hans Wennborga4686012017-02-16 00:04:05 +0000102 if (Opcode == X86::TCRETURNdicc || Opcode == X86::TCRETURNdi64cc) {
103 assert(Offset == 0 && "Conditional tail call cannot adjust the stack.");
104 }
105
Quentin Colombetfb82c7b2016-07-11 21:03:03 +0000106 if (Offset) {
Eric Christopher213a5da2015-12-21 23:04:27 +0000107 // Check for possible merge with preceding ADD instruction.
Quentin Colombetfb82c7b2016-07-11 21:03:03 +0000108 Offset += X86FL->mergeSPUpdates(MBB, MBBI, true);
109 X86FL->emitSPUpdate(MBB, MBBI, Offset, /*InEpilogue=*/true);
Eric Christopher213a5da2015-12-21 23:04:27 +0000110 }
111
112 // Jump to label or value in register.
113 bool IsWin64 = STI->isTargetWin64();
Hans Wennborga4686012017-02-16 00:04:05 +0000114 if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdicc ||
115 Opcode == X86::TCRETURNdi64 || Opcode == X86::TCRETURNdi64cc) {
Hans Wennborg75e25f62016-09-07 17:52:14 +0000116 unsigned Op;
117 switch (Opcode) {
118 case X86::TCRETURNdi:
119 Op = X86::TAILJMPd;
120 break;
Hans Wennborga4686012017-02-16 00:04:05 +0000121 case X86::TCRETURNdicc:
122 Op = X86::TAILJMPd_CC;
123 break;
124 case X86::TCRETURNdi64cc:
Hans Wennborg35905d62017-02-16 19:04:42 +0000125 assert(!MBB.getParent()->hasWinCFI() &&
126 "Conditional tail calls confuse "
127 "the Win64 unwinder.");
Hans Wennborga4686012017-02-16 00:04:05 +0000128 Op = X86::TAILJMPd64_CC;
129 break;
Hans Wennborg75e25f62016-09-07 17:52:14 +0000130 default:
Hans Wennborgc39ef772016-09-08 23:35:10 +0000131 // Note: Win64 uses REX prefixes indirect jumps out of functions, but
132 // not direct ones.
133 Op = X86::TAILJMPd64;
Hans Wennborg75e25f62016-09-07 17:52:14 +0000134 break;
135 }
Eric Christopher213a5da2015-12-21 23:04:27 +0000136 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
Hans Wennborg75e25f62016-09-07 17:52:14 +0000137 if (JumpTarget.isGlobal()) {
Eric Christopher213a5da2015-12-21 23:04:27 +0000138 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
139 JumpTarget.getTargetFlags());
Hans Wennborg75e25f62016-09-07 17:52:14 +0000140 } else {
Eric Christopher213a5da2015-12-21 23:04:27 +0000141 assert(JumpTarget.isSymbol());
142 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
143 JumpTarget.getTargetFlags());
144 }
Hans Wennborga4686012017-02-16 00:04:05 +0000145 if (Op == X86::TAILJMPd_CC || Op == X86::TAILJMPd64_CC) {
146 MIB.addImm(MBBI->getOperand(2).getImm());
147 }
148
Eric Christopher213a5da2015-12-21 23:04:27 +0000149 } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64) {
150 unsigned Op = (Opcode == X86::TCRETURNmi)
151 ? X86::TAILJMPm
152 : (IsWin64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64);
153 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
154 for (unsigned i = 0; i != 5; ++i)
Diana Picus116bbab2017-01-13 09:58:52 +0000155 MIB.add(MBBI->getOperand(i));
Eric Christopher213a5da2015-12-21 23:04:27 +0000156 } else if (Opcode == X86::TCRETURNri64) {
157 BuildMI(MBB, MBBI, DL,
158 TII->get(IsWin64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64))
159 .addReg(JumpTarget.getReg(), RegState::Kill);
160 } else {
161 BuildMI(MBB, MBBI, DL, TII->get(X86::TAILJMPr))
162 .addReg(JumpTarget.getReg(), RegState::Kill);
163 }
164
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000165 MachineInstr &NewMI = *std::prev(MBBI);
166 NewMI.copyImplicitOps(*MBBI->getParent()->getParent(), *MBBI);
Eric Christopher213a5da2015-12-21 23:04:27 +0000167
168 // Delete the pseudo instruction TCRETURN.
169 MBB.erase(MBBI);
170
171 return true;
172 }
173 case X86::EH_RETURN:
174 case X86::EH_RETURN64: {
175 MachineOperand &DestAddr = MBBI->getOperand(0);
176 assert(DestAddr.isReg() && "Offset should be in register!");
177 const bool Uses64BitFramePtr =
178 STI->isTarget64BitLP64() || STI->isTargetNaCl64();
179 unsigned StackPtr = TRI->getStackRegister();
180 BuildMI(MBB, MBBI, DL,
181 TII->get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), StackPtr)
182 .addReg(DestAddr.getReg());
183 // The EH_RETURN pseudo is really removed during the MC Lowering.
184 return true;
185 }
186 case X86::IRET: {
187 // Adjust stack to erase error code
188 int64_t StackAdj = MBBI->getOperand(0).getImm();
189 X86FL->emitSPUpdate(MBB, MBBI, StackAdj, true);
190 // Replace pseudo with machine iret
191 BuildMI(MBB, MBBI, DL,
192 TII->get(STI->is64Bit() ? X86::IRET64 : X86::IRET32));
193 MBB.erase(MBBI);
194 return true;
195 }
David Majnemerd2f767d2016-03-04 22:56:17 +0000196 case X86::RET: {
197 // Adjust stack to erase error code
198 int64_t StackAdj = MBBI->getOperand(0).getImm();
199 MachineInstrBuilder MIB;
200 if (StackAdj == 0) {
201 MIB = BuildMI(MBB, MBBI, DL,
202 TII->get(STI->is64Bit() ? X86::RETQ : X86::RETL));
203 } else if (isUInt<16>(StackAdj)) {
204 MIB = BuildMI(MBB, MBBI, DL,
205 TII->get(STI->is64Bit() ? X86::RETIQ : X86::RETIL))
206 .addImm(StackAdj);
207 } else {
David Majnemer71a1c2c2016-03-04 23:02:15 +0000208 assert(!STI->is64Bit() &&
209 "shouldn't need to do this for x86_64 targets!");
David Majnemerd2f767d2016-03-04 22:56:17 +0000210 // A ret can only handle immediates as big as 2**16-1. If we need to pop
211 // off bytes before the return address, we must do it manually.
David Majnemer71a1c2c2016-03-04 23:02:15 +0000212 BuildMI(MBB, MBBI, DL, TII->get(X86::POP32r)).addReg(X86::ECX, RegState::Define);
David Majnemerd2f767d2016-03-04 22:56:17 +0000213 X86FL->emitSPUpdate(MBB, MBBI, StackAdj, /*InEpilogue=*/true);
David Majnemer71a1c2c2016-03-04 23:02:15 +0000214 BuildMI(MBB, MBBI, DL, TII->get(X86::PUSH32r)).addReg(X86::ECX);
215 MIB = BuildMI(MBB, MBBI, DL, TII->get(X86::RETL));
David Majnemerd2f767d2016-03-04 22:56:17 +0000216 }
217 for (unsigned I = 1, E = MBBI->getNumOperands(); I != E; ++I)
Diana Picus116bbab2017-01-13 09:58:52 +0000218 MIB.add(MBBI->getOperand(I));
David Majnemerd2f767d2016-03-04 22:56:17 +0000219 MBB.erase(MBBI);
220 return true;
221 }
Eric Christopher213a5da2015-12-21 23:04:27 +0000222 case X86::EH_RESTORE: {
223 // Restore ESP and EBP, and optionally ESI if required.
224 bool IsSEH = isAsynchronousEHPersonality(classifyEHPersonality(
Matthias Braunf1caa282017-12-15 22:22:58 +0000225 MBB.getParent()->getFunction().getPersonalityFn()));
Eric Christopher213a5da2015-12-21 23:04:27 +0000226 X86FL->restoreWin32EHStackPointers(MBB, MBBI, DL, /*RestoreSP=*/IsSEH);
227 MBBI->eraseFromParent();
228 return true;
229 }
Quentin Colombetcf9732b2016-03-12 02:25:27 +0000230 case X86::LCMPXCHG8B_SAVE_EBX:
231 case X86::LCMPXCHG16B_SAVE_RBX: {
232 // Perform the following transformation.
233 // SaveRbx = pseudocmpxchg Addr, <4 opds for the address>, InArg, SaveRbx
234 // =>
235 // [E|R]BX = InArg
236 // actualcmpxchg Addr
237 // [E|R]BX = SaveRbx
238 const MachineOperand &InArg = MBBI->getOperand(6);
239 unsigned SaveRbx = MBBI->getOperand(7).getReg();
240
241 unsigned ActualInArg =
242 Opcode == X86::LCMPXCHG8B_SAVE_EBX ? X86::EBX : X86::RBX;
243 // Copy the input argument of the pseudo into the argument of the
244 // actual instruction.
245 TII->copyPhysReg(MBB, MBBI, DL, ActualInArg, InArg.getReg(),
246 InArg.isKill());
247 // Create the actual instruction.
248 unsigned ActualOpc =
249 Opcode == X86::LCMPXCHG8B_SAVE_EBX ? X86::LCMPXCHG8B : X86::LCMPXCHG16B;
250 MachineInstr *NewInstr = BuildMI(MBB, MBBI, DL, TII->get(ActualOpc));
251 // Copy the operands related to the address.
252 for (unsigned Idx = 1; Idx < 6; ++Idx)
253 NewInstr->addOperand(MBBI->getOperand(Idx));
254 // Finally, restore the value of RBX.
255 TII->copyPhysReg(MBB, MBBI, DL, ActualInArg, SaveRbx,
256 /*SrcIsKill*/ true);
257
258 // Delete the pseudo.
259 MBBI->eraseFromParent();
260 return true;
261 }
Eric Christopher213a5da2015-12-21 23:04:27 +0000262 }
263 llvm_unreachable("Previous switch has a fallthrough?");
264}
265
266/// Expand all pseudo instructions contained in \p MBB.
267/// \returns true if any expansion occurred for \p MBB.
268bool X86ExpandPseudo::ExpandMBB(MachineBasicBlock &MBB) {
269 bool Modified = false;
270
271 // MBBI may be invalidated by the expansion.
272 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
273 while (MBBI != E) {
274 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
275 Modified |= ExpandMI(MBB, MBBI);
276 MBBI = NMBBI;
277 }
278
279 return Modified;
280}
281
282bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
283 STI = &static_cast<const X86Subtarget &>(MF.getSubtarget());
284 TII = STI->getInstrInfo();
285 TRI = STI->getRegisterInfo();
Quentin Colombetfb82c7b2016-07-11 21:03:03 +0000286 X86FI = MF.getInfo<X86MachineFunctionInfo>();
Eric Christopher213a5da2015-12-21 23:04:27 +0000287 X86FL = STI->getFrameLowering();
288
289 bool Modified = false;
290 for (MachineBasicBlock &MBB : MF)
291 Modified |= ExpandMBB(MBB);
292 return Modified;
293}
294
295/// Returns an instance of the pseudo instruction expansion pass.
296FunctionPass *llvm::createX86ExpandPseudoPass() {
297 return new X86ExpandPseudo();
298}