LLVM  3.7.0
X86ExpandPseudo.cpp
Go to the documentation of this file.
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/CodeGen/Passes.h" // For IDs of passes that are preserved.
25 #include "llvm/IR/GlobalValue.h"
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "x86-pseudo"
29 
30 namespace {
31 class X86ExpandPseudo : public MachineFunctionPass {
32 public:
33  static char ID;
34  X86ExpandPseudo() : MachineFunctionPass(ID) {}
35 
36  void getAnalysisUsage(AnalysisUsage &AU) const override {
37  AU.setPreservesCFG();
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 
54 private:
55  bool ExpandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI);
56  bool ExpandMBB(MachineBasicBlock &MBB);
57 };
58 char 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.
64 bool X86ExpandPseudo::ExpandMI(MachineBasicBlock &MBB,
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  // Check for possible merge with preceding ADD instruction.
88  StackAdj += X86FL->mergeSPUpdates(MBB, MBBI, true);
89  X86FL->emitSPUpdate(MBB, MBBI, StackAdj, /*InEpilogue=*/true);
90  }
91 
92  // Jump to label or value in register.
93  bool IsWin64 = STI->isTargetWin64();
94  if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdi64) {
95  unsigned Op = (Opcode == X86::TCRETURNdi)
96  ? X86::TAILJMPd
97  : (IsWin64 ? X86::TAILJMPd64_REX : X86::TAILJMPd64);
98  MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
99  if (JumpTarget.isGlobal())
100  MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
101  JumpTarget.getTargetFlags());
102  else {
103  assert(JumpTarget.isSymbol());
104  MIB.addExternalSymbol(JumpTarget.getSymbolName(),
105  JumpTarget.getTargetFlags());
106  }
107  } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64) {
108  unsigned Op = (Opcode == X86::TCRETURNmi)
109  ? X86::TAILJMPm
110  : (IsWin64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64);
111  MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op));
112  for (unsigned i = 0; i != 5; ++i)
113  MIB.addOperand(MBBI->getOperand(i));
114  } else if (Opcode == X86::TCRETURNri64) {
115  BuildMI(MBB, MBBI, DL,
116  TII->get(IsWin64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64))
117  .addReg(JumpTarget.getReg(), RegState::Kill);
118  } else {
119  BuildMI(MBB, MBBI, DL, TII->get(X86::TAILJMPr))
120  .addReg(JumpTarget.getReg(), RegState::Kill);
121  }
122 
123  MachineInstr *NewMI = std::prev(MBBI);
124  NewMI->copyImplicitOps(*MBBI->getParent()->getParent(), MBBI);
125 
126  // Delete the pseudo instruction TCRETURN.
127  MBB.erase(MBBI);
128 
129  return true;
130  }
131  case X86::EH_RETURN:
132  case X86::EH_RETURN64: {
133  MachineOperand &DestAddr = MBBI->getOperand(0);
134  assert(DestAddr.isReg() && "Offset should be in register!");
135  const bool Uses64BitFramePtr =
136  STI->isTarget64BitLP64() || STI->isTargetNaCl64();
137  unsigned StackPtr = TRI->getStackRegister();
138  BuildMI(MBB, MBBI, DL,
139  TII->get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), StackPtr)
140  .addReg(DestAddr.getReg());
141  // The EH_RETURN pseudo is really removed during the MC Lowering.
142  return true;
143  }
144  }
145  llvm_unreachable("Previous switch has a fallthrough?");
146 }
147 
148 /// Expand all pseudo instructions contained in \p MBB.
149 /// \returns true if any expansion occurred for \p MBB.
150 bool X86ExpandPseudo::ExpandMBB(MachineBasicBlock &MBB) {
151  bool Modified = false;
152 
153  // MBBI may be invalidated by the expansion.
154  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
155  while (MBBI != E) {
156  MachineBasicBlock::iterator NMBBI = std::next(MBBI);
157  Modified |= ExpandMI(MBB, MBBI);
158  MBBI = NMBBI;
159  }
160 
161  return Modified;
162 }
163 
164 bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
165  STI = &static_cast<const X86Subtarget &>(MF.getSubtarget());
166  TII = STI->getInstrInfo();
167  TRI = STI->getRegisterInfo();
168  X86FL = STI->getFrameLowering();
169 
170  bool Modified = false;
171  for (MachineBasicBlock &MBB : MF)
172  Modified |= ExpandMBB(MBB);
173  return Modified;
174 }
175 
176 /// Returns an instance of the pseudo instruction expansion pass.
178  return new X86ExpandPseudo();
179 }
const GlobalValue * getGlobal() const
char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
const char * getSymbolName() const
A debug info location.
Definition: DebugLoc.h:34
char & MachineLoopInfoID
MachineLoopInfo - This pass is a loop analysis pass.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
const HexagonRegisterInfo & getRegisterInfo() const
getRegisterInfo - TargetInstrInfo is a superset of MRegister info.
AnalysisUsage & addPreservedID(const void *ID)
int64_t getImm() const
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:267
bundle_iterator< MachineInstr, instr_iterator > iterator
unsigned getTargetFlags() const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
FunctionPass * createX86ExpandPseudoPass()
Return a Machine IR pass that expands X86-specific pseudo instructions into a sequence of actual inst...
Represent the analysis usage information of a pass.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
int64_t getOffset() const
Return the offset from the symbol in this operand.
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
BuildMI - Builder interface.
MachineOperand class - Representation of each machine instruction operand.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned char TargetFlags=0) const
Representation of each machine instruction.
Definition: MachineInstr.h:51
void copyImplicitOps(MachineFunction &MF, const MachineInstr *MI)
Copy implicit register operands from specified instruction to this instruction.
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned char TargetFlags=0) const
static bool isMem(const MachineInstr *MI, unsigned Op)
Definition: X86InstrInfo.h:147
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin...
Definition: ISDOpcodes.h:97
unsigned getReg() const
getReg - Returns the register number.
const MachineInstrBuilder & addOperand(const MachineOperand &MO) const