blob: 1ce52da3d931ae1d83687ee2a3bc4ab8dd79073f [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;
47 const X86FrameLowering *X86FL;
48
49 bool runOnMachineFunction(MachineFunction &Fn) override;
50
Derek Schuff1dbf7a52016-04-04 17:09:25 +000051 MachineFunctionProperties getRequiredProperties() const override {
52 return MachineFunctionProperties().set(
53 MachineFunctionProperties::Property::AllVRegsAllocated);
54 }
55
Eric Christopher213a5da2015-12-21 23:04:27 +000056 const char *getPassName() const override {
57 return "X86 pseudo instruction expansion pass";
58 }
59
60private:
61 bool ExpandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI);
62 bool ExpandMBB(MachineBasicBlock &MBB);
63};
64char X86ExpandPseudo::ID = 0;
65} // End anonymous namespace.
66
67/// If \p MBBI is a pseudo instruction, this method expands
68/// it to the corresponding (sequence of) actual instruction(s).
69/// \returns true if \p MBBI has been expanded.
70bool X86ExpandPseudo::ExpandMI(MachineBasicBlock &MBB,
71 MachineBasicBlock::iterator MBBI) {
72 MachineInstr &MI = *MBBI;
73 unsigned Opcode = MI.getOpcode();
74 DebugLoc DL = MBBI->getDebugLoc();
75 switch (Opcode) {
76 default:
77 return false;
78 case X86::TCRETURNdi:
79 case X86::TCRETURNri:
80 case X86::TCRETURNmi:
81 case X86::TCRETURNdi64:
82 case X86::TCRETURNri64:
83 case X86::TCRETURNmi64: {
84 bool isMem = Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64;
85 MachineOperand &JumpTarget = MBBI->getOperand(0);
86 MachineOperand &StackAdjust = MBBI->getOperand(isMem ? 5 : 1);
87 assert(StackAdjust.isImm() && "Expecting immediate value.");
88
89 // Adjust stack pointer.
90 int StackAdj = StackAdjust.getImm();
91
92 if (StackAdj) {
93 // Check for possible merge with preceding ADD instruction.
94 StackAdj += X86FL->mergeSPUpdates(MBB, MBBI, true);
95 X86FL->emitSPUpdate(MBB, MBBI, StackAdj, /*InEpilogue=*/true);
96 }
97
98 // Jump to label or value in register.
99 bool IsWin64 = STI->isTargetWin64();
100 if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdi64) {
101 unsigned Op = (Opcode == X86::TCRETURNdi)
102 ? X86::TAILJMPd
103 : (IsWin64 ? X86::TAILJMPd64_REX : X86::TAILJMPd64);
104 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
105 if (JumpTarget.isGlobal())
106 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
107 JumpTarget.getTargetFlags());
108 else {
109 assert(JumpTarget.isSymbol());
110 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
111 JumpTarget.getTargetFlags());
112 }
113 } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64) {
114 unsigned Op = (Opcode == X86::TCRETURNmi)
115 ? X86::TAILJMPm
116 : (IsWin64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64);
117 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
118 for (unsigned i = 0; i != 5; ++i)
119 MIB.addOperand(MBBI->getOperand(i));
120 } else if (Opcode == X86::TCRETURNri64) {
121 BuildMI(MBB, MBBI, DL,
122 TII->get(IsWin64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64))
123 .addReg(JumpTarget.getReg(), RegState::Kill);
124 } else {
125 BuildMI(MBB, MBBI, DL, TII->get(X86::TAILJMPr))
126 .addReg(JumpTarget.getReg(), RegState::Kill);
127 }
128
129 MachineInstr *NewMI = std::prev(MBBI);
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000130 NewMI->copyImplicitOps(*MBBI->getParent()->getParent(), *MBBI);
Eric Christopher213a5da2015-12-21 23:04:27 +0000131
132 // Delete the pseudo instruction TCRETURN.
133 MBB.erase(MBBI);
134
135 return true;
136 }
137 case X86::EH_RETURN:
138 case X86::EH_RETURN64: {
139 MachineOperand &DestAddr = MBBI->getOperand(0);
140 assert(DestAddr.isReg() && "Offset should be in register!");
141 const bool Uses64BitFramePtr =
142 STI->isTarget64BitLP64() || STI->isTargetNaCl64();
143 unsigned StackPtr = TRI->getStackRegister();
144 BuildMI(MBB, MBBI, DL,
145 TII->get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), StackPtr)
146 .addReg(DestAddr.getReg());
147 // The EH_RETURN pseudo is really removed during the MC Lowering.
148 return true;
149 }
150 case X86::IRET: {
151 // Adjust stack to erase error code
152 int64_t StackAdj = MBBI->getOperand(0).getImm();
153 X86FL->emitSPUpdate(MBB, MBBI, StackAdj, true);
154 // Replace pseudo with machine iret
155 BuildMI(MBB, MBBI, DL,
156 TII->get(STI->is64Bit() ? X86::IRET64 : X86::IRET32));
157 MBB.erase(MBBI);
158 return true;
159 }
David Majnemerd2f767d2016-03-04 22:56:17 +0000160 case X86::RET: {
161 // Adjust stack to erase error code
162 int64_t StackAdj = MBBI->getOperand(0).getImm();
163 MachineInstrBuilder MIB;
164 if (StackAdj == 0) {
165 MIB = BuildMI(MBB, MBBI, DL,
166 TII->get(STI->is64Bit() ? X86::RETQ : X86::RETL));
167 } else if (isUInt<16>(StackAdj)) {
168 MIB = BuildMI(MBB, MBBI, DL,
169 TII->get(STI->is64Bit() ? X86::RETIQ : X86::RETIL))
170 .addImm(StackAdj);
171 } else {
David Majnemer71a1c2c2016-03-04 23:02:15 +0000172 assert(!STI->is64Bit() &&
173 "shouldn't need to do this for x86_64 targets!");
David Majnemerd2f767d2016-03-04 22:56:17 +0000174 // A ret can only handle immediates as big as 2**16-1. If we need to pop
175 // off bytes before the return address, we must do it manually.
David Majnemer71a1c2c2016-03-04 23:02:15 +0000176 BuildMI(MBB, MBBI, DL, TII->get(X86::POP32r)).addReg(X86::ECX, RegState::Define);
David Majnemerd2f767d2016-03-04 22:56:17 +0000177 X86FL->emitSPUpdate(MBB, MBBI, StackAdj, /*InEpilogue=*/true);
David Majnemer71a1c2c2016-03-04 23:02:15 +0000178 BuildMI(MBB, MBBI, DL, TII->get(X86::PUSH32r)).addReg(X86::ECX);
179 MIB = BuildMI(MBB, MBBI, DL, TII->get(X86::RETL));
David Majnemerd2f767d2016-03-04 22:56:17 +0000180 }
181 for (unsigned I = 1, E = MBBI->getNumOperands(); I != E; ++I)
182 MIB.addOperand(MBBI->getOperand(I));
183 MBB.erase(MBBI);
184 return true;
185 }
Eric Christopher213a5da2015-12-21 23:04:27 +0000186 case X86::EH_RESTORE: {
187 // Restore ESP and EBP, and optionally ESI if required.
188 bool IsSEH = isAsynchronousEHPersonality(classifyEHPersonality(
189 MBB.getParent()->getFunction()->getPersonalityFn()));
190 X86FL->restoreWin32EHStackPointers(MBB, MBBI, DL, /*RestoreSP=*/IsSEH);
191 MBBI->eraseFromParent();
192 return true;
193 }
Quentin Colombetcf9732b2016-03-12 02:25:27 +0000194 case X86::LCMPXCHG8B_SAVE_EBX:
195 case X86::LCMPXCHG16B_SAVE_RBX: {
196 // Perform the following transformation.
197 // SaveRbx = pseudocmpxchg Addr, <4 opds for the address>, InArg, SaveRbx
198 // =>
199 // [E|R]BX = InArg
200 // actualcmpxchg Addr
201 // [E|R]BX = SaveRbx
202 const MachineOperand &InArg = MBBI->getOperand(6);
203 unsigned SaveRbx = MBBI->getOperand(7).getReg();
204
205 unsigned ActualInArg =
206 Opcode == X86::LCMPXCHG8B_SAVE_EBX ? X86::EBX : X86::RBX;
207 // Copy the input argument of the pseudo into the argument of the
208 // actual instruction.
209 TII->copyPhysReg(MBB, MBBI, DL, ActualInArg, InArg.getReg(),
210 InArg.isKill());
211 // Create the actual instruction.
212 unsigned ActualOpc =
213 Opcode == X86::LCMPXCHG8B_SAVE_EBX ? X86::LCMPXCHG8B : X86::LCMPXCHG16B;
214 MachineInstr *NewInstr = BuildMI(MBB, MBBI, DL, TII->get(ActualOpc));
215 // Copy the operands related to the address.
216 for (unsigned Idx = 1; Idx < 6; ++Idx)
217 NewInstr->addOperand(MBBI->getOperand(Idx));
218 // Finally, restore the value of RBX.
219 TII->copyPhysReg(MBB, MBBI, DL, ActualInArg, SaveRbx,
220 /*SrcIsKill*/ true);
221
222 // Delete the pseudo.
223 MBBI->eraseFromParent();
224 return true;
225 }
Eric Christopher213a5da2015-12-21 23:04:27 +0000226 }
227 llvm_unreachable("Previous switch has a fallthrough?");
228}
229
230/// Expand all pseudo instructions contained in \p MBB.
231/// \returns true if any expansion occurred for \p MBB.
232bool X86ExpandPseudo::ExpandMBB(MachineBasicBlock &MBB) {
233 bool Modified = false;
234
235 // MBBI may be invalidated by the expansion.
236 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
237 while (MBBI != E) {
238 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
239 Modified |= ExpandMI(MBB, MBBI);
240 MBBI = NMBBI;
241 }
242
243 return Modified;
244}
245
246bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
247 STI = &static_cast<const X86Subtarget &>(MF.getSubtarget());
248 TII = STI->getInstrInfo();
249 TRI = STI->getRegisterInfo();
250 X86FL = STI->getFrameLowering();
251
252 bool Modified = false;
253 for (MachineBasicBlock &MBB : MF)
254 Modified |= ExpandMBB(MBB);
255 return Modified;
256}
257
258/// Returns an instance of the pseudo instruction expansion pass.
259FunctionPass *llvm::createX86ExpandPseudoPass() {
260 return new X86ExpandPseudo();
261}