1 //===------- X86ExpandPseudo.cpp - Expand pseudo instructions -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
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"
29 #define DEBUG_TYPE "x86-pseudo"
32 class X86ExpandPseudo : public MachineFunctionPass {
35 X86ExpandPseudo() : MachineFunctionPass(ID) {}
37 void getAnalysisUsage(AnalysisUsage &AU) const override {
39 AU.addPreservedID(MachineLoopInfoID);
40 AU.addPreservedID(MachineDominatorsID);
41 MachineFunctionPass::getAnalysisUsage(AU);
44 const X86Subtarget *STI;
45 const X86InstrInfo *TII;
46 const X86RegisterInfo *TRI;
47 const X86FrameLowering *X86FL;
49 bool runOnMachineFunction(MachineFunction &Fn) override;
51 const char *getPassName() const override {
52 return "X86 pseudo instruction expansion pass";
56 bool ExpandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI);
57 bool ExpandMBB(MachineBasicBlock &MBB);
59 char X86ExpandPseudo::ID = 0;
60 } // End anonymous namespace.
62 /// If \p MBBI is a pseudo instruction, this method expands
63 /// it to the corresponding (sequence of) actual instruction(s).
64 /// \returns true if \p MBBI has been expanded.
65 bool X86ExpandPseudo::ExpandMI(MachineBasicBlock &MBB,
66 MachineBasicBlock::iterator MBBI) {
67 MachineInstr &MI = *MBBI;
68 unsigned Opcode = MI.getOpcode();
69 DebugLoc DL = MBBI->getDebugLoc();
76 case X86::TCRETURNdi64:
77 case X86::TCRETURNri64:
78 case X86::TCRETURNmi64: {
79 bool isMem = Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64;
80 MachineOperand &JumpTarget = MBBI->getOperand(0);
81 MachineOperand &StackAdjust = MBBI->getOperand(isMem ? 5 : 1);
82 assert(StackAdjust.isImm() && "Expecting immediate value.");
84 // Adjust stack pointer.
85 int StackAdj = StackAdjust.getImm();
88 // Check for possible merge with preceding ADD instruction.
89 StackAdj += X86FL->mergeSPUpdates(MBB, MBBI, true);
90 X86FL->emitSPUpdate(MBB, MBBI, StackAdj, /*InEpilogue=*/true);
93 // Jump to label or value in register.
94 bool IsWin64 = STI->isTargetWin64();
95 if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdi64) {
96 unsigned Op = (Opcode == X86::TCRETURNdi)
98 : (IsWin64 ? X86::TAILJMPd64_REX : X86::TAILJMPd64);
99 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
100 if (JumpTarget.isGlobal())
101 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
102 JumpTarget.getTargetFlags());
104 assert(JumpTarget.isSymbol());
105 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
106 JumpTarget.getTargetFlags());
108 } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64) {
109 unsigned Op = (Opcode == X86::TCRETURNmi)
111 : (IsWin64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64);
112 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
113 for (unsigned i = 0; i != 5; ++i)
114 MIB.addOperand(MBBI->getOperand(i));
115 } else if (Opcode == X86::TCRETURNri64) {
116 BuildMI(MBB, MBBI, DL,
117 TII->get(IsWin64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64))
118 .addReg(JumpTarget.getReg(), RegState::Kill);
120 BuildMI(MBB, MBBI, DL, TII->get(X86::TAILJMPr))
121 .addReg(JumpTarget.getReg(), RegState::Kill);
124 MachineInstr *NewMI = std::prev(MBBI);
125 NewMI->copyImplicitOps(*MBBI->getParent()->getParent(), MBBI);
127 // Delete the pseudo instruction TCRETURN.
133 case X86::EH_RETURN64: {
134 MachineOperand &DestAddr = MBBI->getOperand(0);
135 assert(DestAddr.isReg() && "Offset should be in register!");
136 const bool Uses64BitFramePtr =
137 STI->isTarget64BitLP64() || STI->isTargetNaCl64();
138 unsigned StackPtr = TRI->getStackRegister();
139 BuildMI(MBB, MBBI, DL,
140 TII->get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), StackPtr)
141 .addReg(DestAddr.getReg());
142 // The EH_RETURN pseudo is really removed during the MC Lowering.
146 // Adjust stack to erase error code
147 int64_t StackAdj = MBBI->getOperand(0).getImm();
148 X86FL->emitSPUpdate(MBB, MBBI, StackAdj, true);
149 // Replace pseudo with machine iret
150 BuildMI(MBB, MBBI, DL,
151 TII->get(STI->is64Bit() ? X86::IRET64 : X86::IRET32));
155 case X86::EH_RESTORE: {
156 // Restore ESP and EBP, and optionally ESI if required.
157 bool IsSEH = isAsynchronousEHPersonality(classifyEHPersonality(
158 MBB.getParent()->getFunction()->getPersonalityFn()));
159 X86FL->restoreWin32EHStackPointers(MBB, MBBI, DL, /*RestoreSP=*/IsSEH);
160 MBBI->eraseFromParent();
164 llvm_unreachable("Previous switch has a fallthrough?");
167 /// Expand all pseudo instructions contained in \p MBB.
168 /// \returns true if any expansion occurred for \p MBB.
169 bool X86ExpandPseudo::ExpandMBB(MachineBasicBlock &MBB) {
170 bool Modified = false;
172 // MBBI may be invalidated by the expansion.
173 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
175 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
176 Modified |= ExpandMI(MBB, MBBI);
183 bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
184 STI = &static_cast<const X86Subtarget &>(MF.getSubtarget());
185 TII = STI->getInstrInfo();
186 TRI = STI->getRegisterInfo();
187 X86FL = STI->getFrameLowering();
189 bool Modified = false;
190 for (MachineBasicBlock &MBB : MF)
191 Modified |= ExpandMBB(MBB);
195 /// Returns an instance of the pseudo instruction expansion pass.
196 FunctionPass *llvm::createX86ExpandPseudoPass() {
197 return new X86ExpandPseudo();