LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ProcessImplicitDefs.cpp
Go to the documentation of this file.
1 //===---------------------- ProcessImplicitDefs.cpp -----------------------===//
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 #define DEBUG_TYPE "processimplicitdefs"
11 
12 #include "llvm/ADT/SetVector.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/Support/Debug.h"
21 
22 using namespace llvm;
23 
24 namespace {
25 /// Process IMPLICIT_DEF instructions and make sure there is one implicit_def
26 /// for each use. Add isUndef marker to implicit_def defs and their uses.
27 class ProcessImplicitDefs : public MachineFunctionPass {
28  const TargetInstrInfo *TII;
29  const TargetRegisterInfo *TRI;
31 
33 
34  void processImplicitDef(MachineInstr *MI);
35  bool canTurnIntoImplicitDef(MachineInstr *MI);
36 
37 public:
38  static char ID;
39 
40  ProcessImplicitDefs() : MachineFunctionPass(ID) {
42  }
43 
44  virtual void getAnalysisUsage(AnalysisUsage &au) const;
45 
46  virtual bool runOnMachineFunction(MachineFunction &fn);
47 };
48 } // end anonymous namespace
49 
52 
53 INITIALIZE_PASS_BEGIN(ProcessImplicitDefs, "processimpdefs",
54  "Process Implicit Definitions", false, false)
55 INITIALIZE_PASS_END(ProcessImplicitDefs, "processimpdefs",
57 
58 void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
59  AU.setPreservesCFG();
60  AU.addPreserved<AliasAnalysis>();
62 }
63 
64 bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
65  if (!MI->isCopyLike() &&
66  !MI->isInsertSubreg() &&
67  !MI->isRegSequence() &&
68  !MI->isPHI())
69  return false;
70  for (MIOperands MO(MI); MO.isValid(); ++MO)
71  if (MO->isReg() && MO->isUse() && MO->readsReg())
72  return false;
73  return true;
74 }
75 
76 void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
77  DEBUG(dbgs() << "Processing " << *MI);
78  unsigned Reg = MI->getOperand(0).getReg();
79 
81  // For virtual registers, mark all uses as <undef>, and convert users to
82  // implicit-def when possible.
84  MRI->use_nodbg_begin(Reg),
85  UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
86  MachineOperand &MO = UI.getOperand();
87  MO.setIsUndef();
88  MachineInstr *UserMI = MO.getParent();
89  if (!canTurnIntoImplicitDef(UserMI))
90  continue;
91  DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
92  UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
93  WorkList.insert(UserMI);
94  }
95  MI->eraseFromParent();
96  return;
97  }
98 
99  // This is a physreg implicit-def.
100  // Look for the first instruction to use or define an alias.
103  bool Found = false;
104  for (++UserMI; UserMI != UserE; ++UserMI) {
105  for (MIOperands MO(UserMI); MO.isValid(); ++MO) {
106  if (!MO->isReg())
107  continue;
108  unsigned UserReg = MO->getReg();
110  !TRI->regsOverlap(Reg, UserReg))
111  continue;
112  // UserMI uses or redefines Reg. Set <undef> flags on all uses.
113  Found = true;
114  if (MO->isUse())
115  MO->setIsUndef();
116  }
117  if (Found)
118  break;
119  }
120 
121  // If we found the using MI, we can erase the IMPLICIT_DEF.
122  if (Found) {
123  DEBUG(dbgs() << "Physreg user: " << *UserMI);
124  MI->eraseFromParent();
125  return;
126  }
127 
128  // Using instr wasn't found, it could be in another block.
129  // Leave the physreg IMPLICIT_DEF, but trim any extra operands.
130  for (unsigned i = MI->getNumOperands() - 1; i; --i)
131  MI->RemoveOperand(i);
132  DEBUG(dbgs() << "Keeping physreg: " << *MI);
133 }
134 
135 /// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
136 /// <undef> operands.
137 bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &MF) {
138 
139  DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
140  << "********** Function: " << MF.getName() << '\n');
141 
142  bool Changed = false;
143 
144  TII = MF.getTarget().getInstrInfo();
145  TRI = MF.getTarget().getRegisterInfo();
146  MRI = &MF.getRegInfo();
147  assert(MRI->isSSA() && "ProcessImplicitDefs only works on SSA form.");
148  assert(WorkList.empty() && "Inconsistent worklist state");
149 
150  for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end();
151  MFI != MFE; ++MFI) {
152  // Scan the basic block for implicit defs.
153  for (MachineBasicBlock::instr_iterator MBBI = MFI->instr_begin(),
154  MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI)
155  if (MBBI->isImplicitDef())
156  WorkList.insert(MBBI);
157 
158  if (WorkList.empty())
159  continue;
160 
161  DEBUG(dbgs() << "BB#" << MFI->getNumber() << " has " << WorkList.size()
162  << " implicit defs.\n");
163  Changed = true;
164 
165  // Drain the WorkList to recursively process any new implicit defs.
166  do processImplicitDef(WorkList.pop_back_val());
167  while (!WorkList.empty());
168  }
169  return Changed;
170 }
MachineInstr * getParent()
static PassRegistry * getPassRegistry()
instr_iterator instr_end()
void setIsUndef(bool Val=true)
static bool isVirtualRegister(unsigned Reg)
char & ProcessImplicitDefsID
ProcessImpicitDefs pass - This pass removes IMPLICIT_DEFs.
Instructions::iterator instr_iterator
INITIALIZE_PASS_BEGIN(ProcessImplicitDefs,"processimpdefs","Process Implicit Definitions", false, false) INITIALIZE_PASS_END(ProcessImplicitDefs
const HexagonInstrInfo * TII
bool isPHI() const
Definition: MachineInstr.h:648
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:172
bool isReg() const
isReg - Tests if this is a MO_Register operand.
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
unsigned getNumOperands() const
Definition: MachineInstr.h:265
void RemoveOperand(unsigned i)
Process Implicit Definitions
bool isCopyLike() const
Definition: MachineInstr.h:678
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:119
bool isInsertSubreg() const
Definition: MachineInstr.h:657
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:267
void initializeProcessImplicitDefsPass(PassRegistry &)
Process Implicit false
virtual const TargetInstrInfo * getInstrInfo() const
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:218
void setDesc(const MCInstrDesc &tid)
Definition: MachineInstr.h:984
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
A collection of legacy interfaces for querying information about the current executing process...
Definition: Process.h:144
static bool isPhysicalRegister(unsigned Reg)
MachineRegisterInfo & getRegInfo()
virtual void getAnalysisUsage(AnalysisUsage &AU) const
IMPLICIT_DEF - This is the MachineInstr-level equivalent of undef.
Definition: TargetOpcodes.h:52
const TargetMachine & getTarget() const
virtual const TargetRegisterInfo * getRegisterInfo() const
unsigned getReg() const
getReg - Returns the register number.
bool isValid() const
isValid - Returns true until all the operands have been visited.
BasicBlockListType::iterator iterator
#define DEBUG(X)
Definition: Debug.h:97
const MCRegisterInfo & MRI
StringRef getName() const
bool isRegSequence() const
Definition: MachineInstr.h:663