blob: 4bd01b8f00f0ffb8595665dfab216874d38c211c [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
Eric Christopher213a5da2015-12-21 23:04:27 +000057 const char *getPassName() const override {
58 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 Wennborg75e25f62016-09-07 17:52:14 +000080 case X86::TCRETURNdicc:
Eric Christopher213a5da2015-12-21 23:04:27 +000081 case X86::TCRETURNri:
82 case X86::TCRETURNmi:
83 case X86::TCRETURNdi64:
84 case X86::TCRETURNri64:
85 case X86::TCRETURNmi64: {
86 bool isMem = Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64;
87 MachineOperand &JumpTarget = MBBI->getOperand(0);
88 MachineOperand &StackAdjust = MBBI->getOperand(isMem ? 5 : 1);
89 assert(StackAdjust.isImm() && "Expecting immediate value.");
90
91 // Adjust stack pointer.
92 int StackAdj = StackAdjust.getImm();
Quentin Colombetfb82c7b2016-07-11 21:03:03 +000093 int MaxTCDelta = X86FI->getTCReturnAddrDelta();
94 int Offset = 0;
95 assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive");
Eric Christopher213a5da2015-12-21 23:04:27 +000096
Quentin Colombetfb82c7b2016-07-11 21:03:03 +000097 // Incoporate the retaddr area.
Hans Wennborg75e25f62016-09-07 17:52:14 +000098 Offset = StackAdj - MaxTCDelta;
Quentin Colombetfb82c7b2016-07-11 21:03:03 +000099 assert(Offset >= 0 && "Offset should never be negative");
100
Hans Wennborg75e25f62016-09-07 17:52:14 +0000101 if (Opcode == X86::TCRETURNdicc) {
102 assert(Offset == 0 && "Conditional tail call cannot adjust the stack.");
103 }
104
Quentin Colombetfb82c7b2016-07-11 21:03:03 +0000105 if (Offset) {
Eric Christopher213a5da2015-12-21 23:04:27 +0000106 // Check for possible merge with preceding ADD instruction.
Quentin Colombetfb82c7b2016-07-11 21:03:03 +0000107 Offset += X86FL->mergeSPUpdates(MBB, MBBI, true);
108 X86FL->emitSPUpdate(MBB, MBBI, Offset, /*InEpilogue=*/true);
Eric Christopher213a5da2015-12-21 23:04:27 +0000109 }
110
111 // Jump to label or value in register.
112 bool IsWin64 = STI->isTargetWin64();
Hans Wennborg75e25f62016-09-07 17:52:14 +0000113 if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdicc ||
114 Opcode == X86::TCRETURNdi64) {
115 unsigned Op;
116 switch (Opcode) {
117 case X86::TCRETURNdi:
118 Op = X86::TAILJMPd;
119 break;
120 case X86::TCRETURNdicc:
121 Op = X86::TAILJMPd_CC;
122 break;
123 default:
124 Op = IsWin64 ? X86::TAILJMPd64_REX : X86::TAILJMPd64;
125 break;
126 }
Eric Christopher213a5da2015-12-21 23:04:27 +0000127 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
Hans Wennborg75e25f62016-09-07 17:52:14 +0000128 if (JumpTarget.isGlobal()) {
Eric Christopher213a5da2015-12-21 23:04:27 +0000129 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
130 JumpTarget.getTargetFlags());
Hans Wennborg75e25f62016-09-07 17:52:14 +0000131 } else {
Eric Christopher213a5da2015-12-21 23:04:27 +0000132 assert(JumpTarget.isSymbol());
133 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
134 JumpTarget.getTargetFlags());
135 }
Hans Wennborg75e25f62016-09-07 17:52:14 +0000136 if (Op == X86::TAILJMPd_CC) {
137 MIB.addImm(MBBI->getOperand(2).getImm());
138 }
139
Eric Christopher213a5da2015-12-21 23:04:27 +0000140 } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64) {
141 unsigned Op = (Opcode == X86::TCRETURNmi)
142 ? X86::TAILJMPm
143 : (IsWin64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64);
144 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
145 for (unsigned i = 0; i != 5; ++i)
146 MIB.addOperand(MBBI->getOperand(i));
147 } else if (Opcode == X86::TCRETURNri64) {
148 BuildMI(MBB, MBBI, DL,
149 TII->get(IsWin64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64))
150 .addReg(JumpTarget.getReg(), RegState::Kill);
151 } else {
152 BuildMI(MBB, MBBI, DL, TII->get(X86::TAILJMPr))
153 .addReg(JumpTarget.getReg(), RegState::Kill);
154 }
155
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000156 MachineInstr &NewMI = *std::prev(MBBI);
157 NewMI.copyImplicitOps(*MBBI->getParent()->getParent(), *MBBI);
Eric Christopher213a5da2015-12-21 23:04:27 +0000158
159 // Delete the pseudo instruction TCRETURN.
160 MBB.erase(MBBI);
161
162 return true;
163 }
164 case X86::EH_RETURN:
165 case X86::EH_RETURN64: {
166 MachineOperand &DestAddr = MBBI->getOperand(0);
167 assert(DestAddr.isReg() && "Offset should be in register!");
168 const bool Uses64BitFramePtr =
169 STI->isTarget64BitLP64() || STI->isTargetNaCl64();
170 unsigned StackPtr = TRI->getStackRegister();
171 BuildMI(MBB, MBBI, DL,
172 TII->get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), StackPtr)
173 .addReg(DestAddr.getReg());
174 // The EH_RETURN pseudo is really removed during the MC Lowering.
175 return true;
176 }
177 case X86::IRET: {
178 // Adjust stack to erase error code
179 int64_t StackAdj = MBBI->getOperand(0).getImm();
180 X86FL->emitSPUpdate(MBB, MBBI, StackAdj, true);
181 // Replace pseudo with machine iret
182 BuildMI(MBB, MBBI, DL,
183 TII->get(STI->is64Bit() ? X86::IRET64 : X86::IRET32));
184 MBB.erase(MBBI);
185 return true;
186 }
David Majnemerd2f767d2016-03-04 22:56:17 +0000187 case X86::RET: {
188 // Adjust stack to erase error code
189 int64_t StackAdj = MBBI->getOperand(0).getImm();
190 MachineInstrBuilder MIB;
191 if (StackAdj == 0) {
192 MIB = BuildMI(MBB, MBBI, DL,
193 TII->get(STI->is64Bit() ? X86::RETQ : X86::RETL));
194 } else if (isUInt<16>(StackAdj)) {
195 MIB = BuildMI(MBB, MBBI, DL,
196 TII->get(STI->is64Bit() ? X86::RETIQ : X86::RETIL))
197 .addImm(StackAdj);
198 } else {
David Majnemer71a1c2c2016-03-04 23:02:15 +0000199 assert(!STI->is64Bit() &&
200 "shouldn't need to do this for x86_64 targets!");
David Majnemerd2f767d2016-03-04 22:56:17 +0000201 // A ret can only handle immediates as big as 2**16-1. If we need to pop
202 // off bytes before the return address, we must do it manually.
David Majnemer71a1c2c2016-03-04 23:02:15 +0000203 BuildMI(MBB, MBBI, DL, TII->get(X86::POP32r)).addReg(X86::ECX, RegState::Define);
David Majnemerd2f767d2016-03-04 22:56:17 +0000204 X86FL->emitSPUpdate(MBB, MBBI, StackAdj, /*InEpilogue=*/true);
David Majnemer71a1c2c2016-03-04 23:02:15 +0000205 BuildMI(MBB, MBBI, DL, TII->get(X86::PUSH32r)).addReg(X86::ECX);
206 MIB = BuildMI(MBB, MBBI, DL, TII->get(X86::RETL));
David Majnemerd2f767d2016-03-04 22:56:17 +0000207 }
208 for (unsigned I = 1, E = MBBI->getNumOperands(); I != E; ++I)
209 MIB.addOperand(MBBI->getOperand(I));
210 MBB.erase(MBBI);
211 return true;
212 }
Eric Christopher213a5da2015-12-21 23:04:27 +0000213 case X86::EH_RESTORE: {
214 // Restore ESP and EBP, and optionally ESI if required.
215 bool IsSEH = isAsynchronousEHPersonality(classifyEHPersonality(
216 MBB.getParent()->getFunction()->getPersonalityFn()));
217 X86FL->restoreWin32EHStackPointers(MBB, MBBI, DL, /*RestoreSP=*/IsSEH);
218 MBBI->eraseFromParent();
219 return true;
220 }
Quentin Colombetcf9732b2016-03-12 02:25:27 +0000221 case X86::LCMPXCHG8B_SAVE_EBX:
222 case X86::LCMPXCHG16B_SAVE_RBX: {
223 // Perform the following transformation.
224 // SaveRbx = pseudocmpxchg Addr, <4 opds for the address>, InArg, SaveRbx
225 // =>
226 // [E|R]BX = InArg
227 // actualcmpxchg Addr
228 // [E|R]BX = SaveRbx
229 const MachineOperand &InArg = MBBI->getOperand(6);
230 unsigned SaveRbx = MBBI->getOperand(7).getReg();
231
232 unsigned ActualInArg =
233 Opcode == X86::LCMPXCHG8B_SAVE_EBX ? X86::EBX : X86::RBX;
234 // Copy the input argument of the pseudo into the argument of the
235 // actual instruction.
236 TII->copyPhysReg(MBB, MBBI, DL, ActualInArg, InArg.getReg(),
237 InArg.isKill());
238 // Create the actual instruction.
239 unsigned ActualOpc =
240 Opcode == X86::LCMPXCHG8B_SAVE_EBX ? X86::LCMPXCHG8B : X86::LCMPXCHG16B;
241 MachineInstr *NewInstr = BuildMI(MBB, MBBI, DL, TII->get(ActualOpc));
242 // Copy the operands related to the address.
243 for (unsigned Idx = 1; Idx < 6; ++Idx)
244 NewInstr->addOperand(MBBI->getOperand(Idx));
245 // Finally, restore the value of RBX.
246 TII->copyPhysReg(MBB, MBBI, DL, ActualInArg, SaveRbx,
247 /*SrcIsKill*/ true);
248
249 // Delete the pseudo.
250 MBBI->eraseFromParent();
251 return true;
252 }
Eric Christopher213a5da2015-12-21 23:04:27 +0000253 }
254 llvm_unreachable("Previous switch has a fallthrough?");
255}
256
257/// Expand all pseudo instructions contained in \p MBB.
258/// \returns true if any expansion occurred for \p MBB.
259bool X86ExpandPseudo::ExpandMBB(MachineBasicBlock &MBB) {
260 bool Modified = false;
261
262 // MBBI may be invalidated by the expansion.
263 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
264 while (MBBI != E) {
265 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
266 Modified |= ExpandMI(MBB, MBBI);
267 MBBI = NMBBI;
268 }
269
270 return Modified;
271}
272
273bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
274 STI = &static_cast<const X86Subtarget &>(MF.getSubtarget());
275 TII = STI->getInstrInfo();
276 TRI = STI->getRegisterInfo();
Quentin Colombetfb82c7b2016-07-11 21:03:03 +0000277 X86FI = MF.getInfo<X86MachineFunctionInfo>();
Eric Christopher213a5da2015-12-21 23:04:27 +0000278 X86FL = STI->getFrameLowering();
279
280 bool Modified = false;
281 for (MachineBasicBlock &MBB : MF)
282 Modified |= ExpandMBB(MBB);
283 return Modified;
284}
285
286/// Returns an instance of the pseudo instruction expansion pass.
287FunctionPass *llvm::createX86ExpandPseudoPass() {
288 return new X86ExpandPseudo();
289}