blob: 29ca3736acaa362a846007b9f73cff3506b10caf [file] [log] [blame]
Quentin Colombet494eb602015-05-22 18:10:47 +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/CodeGen/Passes.h" // For IDs of passes that are preserved.
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/IR/GlobalValue.h"
26using namespace llvm;
27
28#define DEBUG_TYPE "x86-pseudo"
29
30namespace {
31class X86ExpandPseudo : public MachineFunctionPass {
32public:
33 static char ID;
34 X86ExpandPseudo() : MachineFunctionPass(ID) {}
35
36 void getAnalysisUsage(AnalysisUsage &AU) const override {
37 AU.setPreservesCFG();
38 AU.addPreservedID(MachineLoopInfoID);
39 AU.addPreservedID(MachineDominatorsID);
40 MachineFunctionPass::getAnalysisUsage(AU);
41 }
42
43 const X86Subtarget *STI;
44 const X86InstrInfo *TII;
45 const X86RegisterInfo *TRI;
46 const X86FrameLowering *X86FL;
47
48 bool runOnMachineFunction(MachineFunction &Fn) override;
49
50 const char *getPassName() const override {
51 return "X86 pseudo instruction expansion pass";
52 }
53
54private:
55 bool ExpandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI);
56 bool ExpandMBB(MachineBasicBlock &MBB);
57};
58char X86ExpandPseudo::ID = 0;
59} // End anonymous namespace.
60
61/// If \p MBBI is a pseudo instruction, this method expands
62/// it to the corresponding (sequence of) actual instruction(s).
63/// \returns true if \p MBBI has been expanded.
64bool X86ExpandPseudo::ExpandMI(MachineBasicBlock &MBB,
65 MachineBasicBlock::iterator MBBI) {
66 MachineInstr &MI = *MBBI;
67 unsigned Opcode = MI.getOpcode();
68 DebugLoc DL = MBBI->getDebugLoc();
69 switch (Opcode) {
70 default:
71 return false;
72 case X86::TCRETURNdi:
73 case X86::TCRETURNri:
74 case X86::TCRETURNmi:
75 case X86::TCRETURNdi64:
76 case X86::TCRETURNri64:
77 case X86::TCRETURNmi64: {
78 bool isMem = Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64;
79 MachineOperand &JumpTarget = MBBI->getOperand(0);
80 MachineOperand &StackAdjust = MBBI->getOperand(isMem ? 5 : 1);
81 assert(StackAdjust.isImm() && "Expecting immediate value.");
82
83 // Adjust stack pointer.
84 int StackAdj = StackAdjust.getImm();
85
86 if (StackAdj) {
87 bool Is64Bit = STI->is64Bit();
88 // standard x86_64 and NaCl use 64-bit frame/stack pointers, x32 - 32-bit.
89 const bool Uses64BitFramePtr =
90 STI->isTarget64BitLP64() || STI->isTargetNaCl64();
91 bool UseLEAForSP =
92 X86FL->useLEAForSPInProlog(*MBB.getParent());
93 unsigned StackPtr = TRI->getStackRegister();
94 // Check for possible merge with preceding ADD instruction.
95 StackAdj += X86FrameLowering::mergeSPUpdates(MBB, MBBI, StackPtr, true);
96 X86FrameLowering::emitSPUpdate(MBB, MBBI, StackPtr, StackAdj, Is64Bit,
97 Uses64BitFramePtr, UseLEAForSP, *TII,
98 *TRI);
99 }
100
101 // Jump to label or value in register.
102 bool IsWin64 = STI->isTargetWin64();
103 if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdi64) {
104 unsigned Op = (Opcode == X86::TCRETURNdi)
105 ? X86::TAILJMPd
106 : (IsWin64 ? X86::TAILJMPd64_REX : X86::TAILJMPd64);
107 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
108 if (JumpTarget.isGlobal())
109 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
110 JumpTarget.getTargetFlags());
111 else {
112 assert(JumpTarget.isSymbol());
113 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
114 JumpTarget.getTargetFlags());
115 }
116 } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64) {
117 unsigned Op = (Opcode == X86::TCRETURNmi)
118 ? X86::TAILJMPm
119 : (IsWin64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64);
120 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
121 for (unsigned i = 0; i != 5; ++i)
122 MIB.addOperand(MBBI->getOperand(i));
123 } else if (Opcode == X86::TCRETURNri64) {
124 BuildMI(MBB, MBBI, DL,
125 TII->get(IsWin64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64))
126 .addReg(JumpTarget.getReg(), RegState::Kill);
127 } else {
128 BuildMI(MBB, MBBI, DL, TII->get(X86::TAILJMPr))
129 .addReg(JumpTarget.getReg(), RegState::Kill);
130 }
131
132 MachineInstr *NewMI = std::prev(MBBI);
133 NewMI->copyImplicitOps(*MBBI->getParent()->getParent(), MBBI);
134
135 // Delete the pseudo instruction TCRETURN.
136 MBB.erase(MBBI);
137
138 return true;
139 }
140 case X86::EH_RETURN:
141 case X86::EH_RETURN64: {
142 MachineOperand &DestAddr = MBBI->getOperand(0);
143 assert(DestAddr.isReg() && "Offset should be in register!");
144 const bool Uses64BitFramePtr =
145 STI->isTarget64BitLP64() || STI->isTargetNaCl64();
146 unsigned StackPtr = TRI->getStackRegister();
147 BuildMI(MBB, MBBI, DL,
148 TII->get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), StackPtr)
149 .addReg(DestAddr.getReg());
150 // The EH_RETURN pseudo is really removed during the MC Lowering.
151 return true;
152 }
153 }
154 llvm_unreachable("Previous switch has a fallthrough?");
155}
156
157/// Expand all pseudo instructions contained in \p MBB.
158/// \returns true if any expansion occurred for \p MBB.
159bool X86ExpandPseudo::ExpandMBB(MachineBasicBlock &MBB) {
160 bool Modified = false;
161
162 // MBBI may be invalidated by the expansion.
163 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
164 while (MBBI != E) {
165 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
166 Modified |= ExpandMI(MBB, MBBI);
167 MBBI = NMBBI;
168 }
169
170 return Modified;
171}
172
173bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
174 STI = &static_cast<const X86Subtarget &>(MF.getSubtarget());
175 TII = STI->getInstrInfo();
176 TRI = STI->getRegisterInfo();
177 X86FL = STI->getFrameLowering();
178
179 bool Modified = false;
180 for (MachineBasicBlock &MBB : MF)
181 Modified |= ExpandMBB(MBB);
182 return Modified;
183}
184
185/// Returns an instance of the pseudo instruction expansion pass.
186FunctionPass *llvm::createX86ExpandPseudoPass() {
187 return new X86ExpandPseudo();
188}