LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExpandPostRAPseudos.cpp
Go to the documentation of this file.
1 //===-- ExpandPostRAPseudos.cpp - Pseudo instruction expansion pass -------===//
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 defines a pass that expands COPY and SUBREG_TO_REG pseudo
11 // instructions after register allocation.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #define DEBUG_TYPE "postrapseudos"
16 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/Support/Debug.h"
26 using namespace llvm;
27 
28 namespace {
29 struct ExpandPostRA : public MachineFunctionPass {
30 private:
31  const TargetRegisterInfo *TRI;
32  const TargetInstrInfo *TII;
33 
34 public:
35  static char ID; // Pass identification, replacement for typeid
36  ExpandPostRA() : MachineFunctionPass(ID) {}
37 
38  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39  AU.setPreservesCFG();
43  }
44 
45  /// runOnMachineFunction - pass entry point
46  bool runOnMachineFunction(MachineFunction&);
47 
48 private:
49  bool LowerSubregToReg(MachineInstr *MI);
50  bool LowerCopy(MachineInstr *MI);
51 
52  void TransferImplicitDefs(MachineInstr *MI);
53 };
54 } // end anonymous namespace
55 
56 char ExpandPostRA::ID = 0;
58 
59 INITIALIZE_PASS(ExpandPostRA, "postrapseudos",
60  "Post-RA pseudo instruction expansion pass", false, false)
61 
62 /// TransferImplicitDefs - MI is a pseudo-instruction, and the lowered
63 /// replacement instructions immediately precede it. Copy any implicit-def
64 /// operands from MI to the replacement instruction.
65 void
66 ExpandPostRA::TransferImplicitDefs(MachineInstr *MI) {
68  --CopyMI;
69 
70  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
71  MachineOperand &MO = MI->getOperand(i);
72  if (!MO.isReg() || !MO.isImplicit() || MO.isUse())
73  continue;
74  CopyMI->addOperand(MachineOperand::CreateReg(MO.getReg(), true, true));
75  }
76 }
77 
78 bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
79  MachineBasicBlock *MBB = MI->getParent();
80  assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
81  MI->getOperand(1).isImm() &&
82  (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
83  MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
84 
85  unsigned DstReg = MI->getOperand(0).getReg();
86  unsigned InsReg = MI->getOperand(2).getReg();
87  assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
88  unsigned SubIdx = MI->getOperand(3).getImm();
89 
90  assert(SubIdx != 0 && "Invalid index for insert_subreg");
91  unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
92 
94  "Insert destination must be in a physical register");
96  "Inserted value must be in a physical register");
97 
98  DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
99 
100  if (MI->allDefsAreDead()) {
101  MI->setDesc(TII->get(TargetOpcode::KILL));
102  DEBUG(dbgs() << "subreg: replaced by: " << *MI);
103  return true;
104  }
105 
106  if (DstSubReg == InsReg) {
107  // No need to insert an identity copy instruction.
108  // Watch out for case like this:
109  // %RAX<def> = SUBREG_TO_REG 0, %EAX<kill>, 3
110  // We must leave %RAX live.
111  if (DstReg != InsReg) {
112  MI->setDesc(TII->get(TargetOpcode::KILL));
113  MI->RemoveOperand(3); // SubIdx
114  MI->RemoveOperand(1); // Imm
115  DEBUG(dbgs() << "subreg: replace by: " << *MI);
116  return true;
117  }
118  DEBUG(dbgs() << "subreg: eliminated!");
119  } else {
120  TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
121  MI->getOperand(2).isKill());
122 
123  // Implicitly define DstReg for subsequent uses.
125  --CopyMI;
126  CopyMI->addRegisterDefined(DstReg);
127  DEBUG(dbgs() << "subreg: " << *CopyMI);
128  }
129 
130  DEBUG(dbgs() << '\n');
131  MBB->erase(MI);
132  return true;
133 }
134 
135 bool ExpandPostRA::LowerCopy(MachineInstr *MI) {
136 
137  if (MI->allDefsAreDead()) {
138  DEBUG(dbgs() << "dead copy: " << *MI);
139  MI->setDesc(TII->get(TargetOpcode::KILL));
140  DEBUG(dbgs() << "replaced by: " << *MI);
141  return true;
142  }
143 
144  MachineOperand &DstMO = MI->getOperand(0);
145  MachineOperand &SrcMO = MI->getOperand(1);
146 
147  if (SrcMO.getReg() == DstMO.getReg()) {
148  DEBUG(dbgs() << "identity copy: " << *MI);
149  // No need to insert an identity copy instruction, but replace with a KILL
150  // if liveness is changed.
151  if (SrcMO.isUndef() || MI->getNumOperands() > 2) {
152  // We must make sure the super-register gets killed. Replace the
153  // instruction with KILL.
154  MI->setDesc(TII->get(TargetOpcode::KILL));
155  DEBUG(dbgs() << "replaced by: " << *MI);
156  return true;
157  }
158  // Vanilla identity copy.
159  MI->eraseFromParent();
160  return true;
161  }
162 
163  DEBUG(dbgs() << "real copy: " << *MI);
164  TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(),
165  DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill());
166 
167  if (MI->getNumOperands() > 2)
168  TransferImplicitDefs(MI);
169  DEBUG({
171  dbgs() << "replaced by: " << *(--dMI);
172  });
173  MI->eraseFromParent();
174  return true;
175 }
176 
177 /// runOnMachineFunction - Reduce subregister inserts and extracts to register
178 /// copies.
179 ///
180 bool ExpandPostRA::runOnMachineFunction(MachineFunction &MF) {
181  DEBUG(dbgs() << "Machine Function\n"
182  << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
183  << "********** Function: " << MF.getName() << '\n');
184  TRI = MF.getTarget().getRegisterInfo();
185  TII = MF.getTarget().getInstrInfo();
186 
187  bool MadeChange = false;
188 
189  for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
190  mbbi != mbbe; ++mbbi) {
191  for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
192  mi != me;) {
193  MachineInstr *MI = mi;
194  // Advance iterator here because MI may be erased.
195  ++mi;
196 
197  // Only expand pseudos.
198  if (!MI->isPseudo())
199  continue;
200 
201  // Give targets a chance to expand even standard pseudos.
202  if (TII->expandPostRAPseudo(MI)) {
203  MadeChange = true;
204  continue;
205  }
206 
207  // Expand standard pseudos.
208  switch (MI->getOpcode()) {
210  MadeChange |= LowerSubregToReg(MI);
211  break;
212  case TargetOpcode::COPY:
213  MadeChange |= LowerCopy(MI);
214  break;
216  continue;
219  llvm_unreachable("Sub-register pseudos should have been eliminated.");
220  }
221  }
222  }
223 
224  return MadeChange;
225 }
bool isImplicit() const
instr_iterator erase(instr_iterator I)
virtual void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, DebugLoc DL, unsigned DestReg, unsigned SrcReg, bool KillSrc) const
char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
bool isPseudo(QueryType Type=IgnoreBundle) const
Definition: MachineInstr.h:341
bool allDefsAreDead() const
char & MachineLoopInfoID
MachineLoopInfo - This pass is a loop analysis pass.
const HexagonInstrInfo * TII
static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false)
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
#define llvm_unreachable(msg)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isUndef() const
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
unsigned getNumOperands() const
Definition: MachineInstr.h:265
void RemoveOperand(unsigned i)
bool isKill() const
int getOpcode() const
Definition: MachineInstr.h:261
char & ExpandPostRAPseudosID
AnalysisUsage & addPreservedID(const void *ID)
int64_t getImm() const
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:119
bundle_iterator< MachineInstr, instr_iterator > iterator
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:267
unsigned getSubReg() const
virtual const TargetInstrInfo * getInstrInfo() const
void setDesc(const MCInstrDesc &tid)
Definition: MachineInstr.h:984
void setPreservesCFG()
Definition: Pass.cpp:249
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
INITIALIZE_PASS(ExpandPostRA,"postrapseudos","Post-RA pseudo instruction expansion pass", false, false) void ExpandPostRA
static bool isPhysicalRegister(unsigned Reg)
virtual void getAnalysisUsage(AnalysisUsage &AU) const
DBG_VALUE - a mapping of the llvm.dbg.value intrinsic.
Definition: TargetOpcodes.h:69
const TargetMachine & getTarget() const
virtual const TargetRegisterInfo * getRegisterInfo() const
unsigned getReg() const
getReg - Returns the register number.
BasicBlockListType::iterator iterator
#define DEBUG(X)
Definition: Debug.h:97
StringRef getName() const
DebugLoc getDebugLoc() const
Definition: MachineInstr.h:244