]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/X86/X86ExpandPseudo.cpp
Update clang to trunk r290819 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / X86 / X86ExpandPseudo.cpp
1 //===------- 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"
27 using namespace llvm;
28
29 #define DEBUG_TYPE "x86-pseudo"
30
31 namespace {
32 class X86ExpandPseudo : public MachineFunctionPass {
33 public:
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 X86MachineFunctionInfo *X86FI;
48   const X86FrameLowering *X86FL;
49
50   bool runOnMachineFunction(MachineFunction &Fn) override;
51
52   MachineFunctionProperties getRequiredProperties() const override {
53     return MachineFunctionProperties().set(
54         MachineFunctionProperties::Property::NoVRegs);
55   }
56
57   StringRef getPassName() const override {
58     return "X86 pseudo instruction expansion pass";
59   }
60
61 private:
62   bool ExpandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI);
63   bool ExpandMBB(MachineBasicBlock &MBB);
64 };
65 char 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.
71 bool 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:
80   case X86::TCRETURNdicc:
81   case X86::TCRETURNri:
82   case X86::TCRETURNmi:
83   case X86::TCRETURNdi64:
84   case X86::TCRETURNdi64cc:
85   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();
94     int MaxTCDelta = X86FI->getTCReturnAddrDelta();
95     int Offset = 0;
96     assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive");
97
98     // Incoporate the retaddr area.
99     Offset = StackAdj - MaxTCDelta;
100     assert(Offset >= 0 && "Offset should never be negative");
101
102     if (Opcode == X86::TCRETURNdicc || Opcode == X86::TCRETURNdi64cc) {
103       assert(Offset == 0 && "Conditional tail call cannot adjust the stack.");
104     }
105
106     if (Offset) {
107       // Check for possible merge with preceding ADD instruction.
108       Offset += X86FL->mergeSPUpdates(MBB, MBBI, true);
109       X86FL->emitSPUpdate(MBB, MBBI, Offset, /*InEpilogue=*/true);
110     }
111
112     // Jump to label or value in register.
113     bool IsWin64 = STI->isTargetWin64();
114     if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdicc ||
115         Opcode == X86::TCRETURNdi64 || Opcode == X86::TCRETURNdi64cc) {
116       unsigned Op;
117       switch (Opcode) {
118       case X86::TCRETURNdi:
119         Op = X86::TAILJMPd;
120         break;
121       case X86::TCRETURNdicc:
122         Op = X86::TAILJMPd_CC;
123         break;
124       case X86::TCRETURNdi64cc:
125         assert(!IsWin64 && "Conditional tail calls confuse the Win64 unwinder.");
126         // TODO: We could do it for Win64 "leaf" functions though; PR30337.
127         Op = X86::TAILJMPd64_CC;
128         break;
129       default:
130         // Note: Win64 uses REX prefixes indirect jumps out of functions, but
131         // not direct ones.
132         Op = X86::TAILJMPd64;
133         break;
134       }
135       MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
136       if (JumpTarget.isGlobal()) {
137         MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
138                              JumpTarget.getTargetFlags());
139       } else {
140         assert(JumpTarget.isSymbol());
141         MIB.addExternalSymbol(JumpTarget.getSymbolName(),
142                               JumpTarget.getTargetFlags());
143       }
144       if (Op == X86::TAILJMPd_CC || Op == X86::TAILJMPd64_CC) {
145         MIB.addImm(MBBI->getOperand(2).getImm());
146       }
147
148     } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64) {
149       unsigned Op = (Opcode == X86::TCRETURNmi)
150                         ? X86::TAILJMPm
151                         : (IsWin64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64);
152       MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
153       for (unsigned i = 0; i != 5; ++i)
154         MIB.addOperand(MBBI->getOperand(i));
155     } else if (Opcode == X86::TCRETURNri64) {
156       BuildMI(MBB, MBBI, DL,
157               TII->get(IsWin64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64))
158           .addReg(JumpTarget.getReg(), RegState::Kill);
159     } else {
160       BuildMI(MBB, MBBI, DL, TII->get(X86::TAILJMPr))
161           .addReg(JumpTarget.getReg(), RegState::Kill);
162     }
163
164     MachineInstr &NewMI = *std::prev(MBBI);
165     NewMI.copyImplicitOps(*MBBI->getParent()->getParent(), *MBBI);
166
167     // Delete the pseudo instruction TCRETURN.
168     MBB.erase(MBBI);
169
170     return true;
171   }
172   case X86::EH_RETURN:
173   case X86::EH_RETURN64: {
174     MachineOperand &DestAddr = MBBI->getOperand(0);
175     assert(DestAddr.isReg() && "Offset should be in register!");
176     const bool Uses64BitFramePtr =
177         STI->isTarget64BitLP64() || STI->isTargetNaCl64();
178     unsigned StackPtr = TRI->getStackRegister();
179     BuildMI(MBB, MBBI, DL,
180             TII->get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), StackPtr)
181         .addReg(DestAddr.getReg());
182     // The EH_RETURN pseudo is really removed during the MC Lowering.
183     return true;
184   }
185   case X86::IRET: {
186     // Adjust stack to erase error code
187     int64_t StackAdj = MBBI->getOperand(0).getImm();
188     X86FL->emitSPUpdate(MBB, MBBI, StackAdj, true);
189     // Replace pseudo with machine iret
190     BuildMI(MBB, MBBI, DL,
191             TII->get(STI->is64Bit() ? X86::IRET64 : X86::IRET32));
192     MBB.erase(MBBI);
193     return true;
194   }
195   case X86::RET: {
196     // Adjust stack to erase error code
197     int64_t StackAdj = MBBI->getOperand(0).getImm();
198     MachineInstrBuilder MIB;
199     if (StackAdj == 0) {
200       MIB = BuildMI(MBB, MBBI, DL,
201                     TII->get(STI->is64Bit() ? X86::RETQ : X86::RETL));
202     } else if (isUInt<16>(StackAdj)) {
203       MIB = BuildMI(MBB, MBBI, DL,
204                     TII->get(STI->is64Bit() ? X86::RETIQ : X86::RETIL))
205                 .addImm(StackAdj);
206     } else {
207       assert(!STI->is64Bit() &&
208              "shouldn't need to do this for x86_64 targets!");
209       // A ret can only handle immediates as big as 2**16-1.  If we need to pop
210       // off bytes before the return address, we must do it manually.
211       BuildMI(MBB, MBBI, DL, TII->get(X86::POP32r)).addReg(X86::ECX, RegState::Define);
212       X86FL->emitSPUpdate(MBB, MBBI, StackAdj, /*InEpilogue=*/true);
213       BuildMI(MBB, MBBI, DL, TII->get(X86::PUSH32r)).addReg(X86::ECX);
214       MIB = BuildMI(MBB, MBBI, DL, TII->get(X86::RETL));
215     }
216     for (unsigned I = 1, E = MBBI->getNumOperands(); I != E; ++I)
217       MIB.addOperand(MBBI->getOperand(I));
218     MBB.erase(MBBI);
219     return true;
220   }
221   case X86::EH_RESTORE: {
222     // Restore ESP and EBP, and optionally ESI if required.
223     bool IsSEH = isAsynchronousEHPersonality(classifyEHPersonality(
224         MBB.getParent()->getFunction()->getPersonalityFn()));
225     X86FL->restoreWin32EHStackPointers(MBB, MBBI, DL, /*RestoreSP=*/IsSEH);
226     MBBI->eraseFromParent();
227     return true;
228   }
229   case X86::LCMPXCHG8B_SAVE_EBX:
230   case X86::LCMPXCHG16B_SAVE_RBX: {
231     // Perform the following transformation.
232     // SaveRbx = pseudocmpxchg Addr, <4 opds for the address>, InArg, SaveRbx
233     // =>
234     // [E|R]BX = InArg
235     // actualcmpxchg Addr
236     // [E|R]BX = SaveRbx
237     const MachineOperand &InArg = MBBI->getOperand(6);
238     unsigned SaveRbx = MBBI->getOperand(7).getReg();
239
240     unsigned ActualInArg =
241         Opcode == X86::LCMPXCHG8B_SAVE_EBX ? X86::EBX : X86::RBX;
242     // Copy the input argument of the pseudo into the argument of the
243     // actual instruction.
244     TII->copyPhysReg(MBB, MBBI, DL, ActualInArg, InArg.getReg(),
245                      InArg.isKill());
246     // Create the actual instruction.
247     unsigned ActualOpc =
248         Opcode == X86::LCMPXCHG8B_SAVE_EBX ? X86::LCMPXCHG8B : X86::LCMPXCHG16B;
249     MachineInstr *NewInstr = BuildMI(MBB, MBBI, DL, TII->get(ActualOpc));
250     // Copy the operands related to the address.
251     for (unsigned Idx = 1; Idx < 6; ++Idx)
252       NewInstr->addOperand(MBBI->getOperand(Idx));
253     // Finally, restore the value of RBX.
254     TII->copyPhysReg(MBB, MBBI, DL, ActualInArg, SaveRbx,
255                      /*SrcIsKill*/ true);
256
257     // Delete the pseudo.
258     MBBI->eraseFromParent();
259     return true;
260   }
261   }
262   llvm_unreachable("Previous switch has a fallthrough?");
263 }
264
265 /// Expand all pseudo instructions contained in \p MBB.
266 /// \returns true if any expansion occurred for \p MBB.
267 bool X86ExpandPseudo::ExpandMBB(MachineBasicBlock &MBB) {
268   bool Modified = false;
269
270   // MBBI may be invalidated by the expansion.
271   MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
272   while (MBBI != E) {
273     MachineBasicBlock::iterator NMBBI = std::next(MBBI);
274     Modified |= ExpandMI(MBB, MBBI);
275     MBBI = NMBBI;
276   }
277
278   return Modified;
279 }
280
281 bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
282   STI = &static_cast<const X86Subtarget &>(MF.getSubtarget());
283   TII = STI->getInstrInfo();
284   TRI = STI->getRegisterInfo();
285   X86FI = MF.getInfo<X86MachineFunctionInfo>();
286   X86FL = STI->getFrameLowering();
287
288   bool Modified = false;
289   for (MachineBasicBlock &MBB : MF)
290     Modified |= ExpandMBB(MBB);
291   return Modified;
292 }
293
294 /// Returns an instance of the pseudo instruction expansion pass.
295 FunctionPass *llvm::createX86ExpandPseudoPass() {
296   return new X86ExpandPseudo();
297 }