LLVM  6.0.0svn
RegUsageInfoPropagate.cpp
Go to the documentation of this file.
1 //=--- RegUsageInfoPropagate.cpp - Register Usage Informartion Propagation --=//
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 pass is required to take advantage of the interprocedural register
11 /// allocation infrastructure.
12 ///
13 /// This pass iterates through MachineInstrs in a given MachineFunction and at
14 /// each callsite queries RegisterUsageInfo for RegMask (calculated based on
15 /// actual register allocation) of the callee function, if the RegMask detail
16 /// is available then this pass will update the RegMask of the call instruction.
17 /// This updated RegMask will be used by the register allocator while allocating
18 /// the current MachineFunction.
19 ///
20 //===----------------------------------------------------------------------===//
21 
27 #include "llvm/CodeGen/Passes.h"
29 #include "llvm/IR/Module.h"
31 #include "llvm/Support/Debug.h"
34 #include <map>
35 #include <string>
36 
37 namespace llvm {
39 }
40 
41 using namespace llvm;
42 
43 #define DEBUG_TYPE "ip-regalloc"
44 
45 #define RUIP_NAME "Register Usage Information Propagation"
46 
47 namespace {
48 class RegUsageInfoPropagationPass : public MachineFunctionPass {
49 
50 public:
51  RegUsageInfoPropagationPass() : MachineFunctionPass(ID) {
54  }
55 
56  StringRef getPassName() const override { return RUIP_NAME; }
57 
58  bool runOnMachineFunction(MachineFunction &MF) override;
59 
60  void getAnalysisUsage(AnalysisUsage &AU) const override;
61 
62  static char ID;
63 
64 private:
65  static void setRegMask(MachineInstr &MI, const uint32_t *RegMask) {
66  for (MachineOperand &MO : MI.operands()) {
67  if (MO.isRegMask())
68  MO.setRegMask(RegMask);
69  }
70  }
71 };
72 } // end of anonymous namespace
74 
75 INITIALIZE_PASS_BEGIN(RegUsageInfoPropagationPass, "reg-usage-propagation",
76  RUIP_NAME, false, false)
78 INITIALIZE_PASS_END(RegUsageInfoPropagationPass, "reg-usage-propagation",
80 
82  return new RegUsageInfoPropagationPass();
83 }
84 
85 void RegUsageInfoPropagationPass::getAnalysisUsage(AnalysisUsage &AU) const {
87  AU.setPreservesAll();
89 }
90 
91 // Assumes call instructions have a single reference to a function.
92 static const Function *findCalledFunction(const Module &M, MachineInstr &MI) {
93  for (MachineOperand &MO : MI.operands()) {
94  if (MO.isGlobal())
95  return dyn_cast<Function>(MO.getGlobal());
96 
97  if (MO.isSymbol())
98  return M.getFunction(MO.getSymbolName());
99  }
100 
101  return nullptr;
102 }
103 
104 bool RegUsageInfoPropagationPass::runOnMachineFunction(MachineFunction &MF) {
105  const Module *M = MF.getFunction()->getParent();
106  PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
107 
108  DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName()
109  << " ++++++++++++++++++++ \n");
110  DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
111 
112  const MachineFrameInfo &MFI = MF.getFrameInfo();
113  if (!MFI.hasCalls() && !MFI.hasTailCall())
114  return false;
115 
116  bool Changed = false;
117 
118  for (MachineBasicBlock &MBB : MF) {
119  for (MachineInstr &MI : MBB) {
120  if (!MI.isCall())
121  continue;
122  DEBUG(dbgs()
123  << "Call Instruction Before Register Usage Info Propagation : \n");
124  DEBUG(dbgs() << MI << "\n");
125 
126  auto UpdateRegMask = [&](const Function *F) {
127  const auto *RegMask = PRUI->getRegUsageInfo(F);
128  if (!RegMask)
129  return;
130  setRegMask(MI, &(*RegMask)[0]);
131  Changed = true;
132  };
133 
134  if (const Function *F = findCalledFunction(*M, MI)) {
135  UpdateRegMask(F);
136  } else {
137  DEBUG(dbgs() << "Failed to find call target function\n");
138  }
139 
140  DEBUG(dbgs() << "Call Instruction After Register Usage Info Propagation : "
141  << MI << '\n');
142  }
143  }
144 
145  DEBUG(dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
146  "++++++ \n");
147  return Changed;
148 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:45
F(f)
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:332
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
FunctionPass * createRegUsageInfoPropPass()
Return a MachineFunction pass that identifies call sites and propagates register usage information of...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
static const Function * findCalledFunction(const Module &M, MachineInstr &MI)
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
reg usage propagation
MachineOperand class - Representation of each machine instruction operand.
Module.h This file contains the declarations for the Module class.
void initializeRegUsageInfoPropagationPassPass(PassRegistry &)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:172
#define RUIP_NAME
void setPreservesAll()
Set by analyses that do not transform their input at all.
Representation of each machine instruction.
Definition: MachineInstr.h:59
const std::vector< uint32_t > * getRegUsageInfo(const Function *FP)
To query stored RegMask for given Function *, it will return nullptr if function is not known...
INITIALIZE_PASS_BEGIN(RegUsageInfoPropagationPass, "reg-usage-propagation", RUIP_NAME, false, false) INITIALIZE_PASS_END(RegUsageInfoPropagationPass
This pass is required to take advantage of the interprocedural register allocation infrastructure...
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:556
#define DEBUG(X)
Definition: Debug.h:118
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:39