LLVM  4.0.0
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 
26 #include "llvm/CodeGen/Passes.h"
28 #include "llvm/IR/Module.h"
30 #include "llvm/Support/Debug.h"
33 #include <map>
34 #include <string>
35 
36 namespace llvm {
38 }
39 
40 using namespace llvm;
41 
42 #define DEBUG_TYPE "ip-regalloc"
43 
44 #define RUIP_NAME "Register Usage Information Propagation"
45 
46 namespace {
47 class RegUsageInfoPropagationPass : public MachineFunctionPass {
48 
49 public:
50  RegUsageInfoPropagationPass() : MachineFunctionPass(ID) {
53  }
54 
55  StringRef getPassName() const override { return RUIP_NAME; }
56 
57  bool runOnMachineFunction(MachineFunction &MF) override;
58 
59  void getAnalysisUsage(AnalysisUsage &AU) const override;
60 
61  static char ID;
62 
63 private:
64  static void setRegMask(MachineInstr &MI, const uint32_t *RegMask) {
65  for (MachineOperand &MO : MI.operands()) {
66  if (MO.isRegMask())
67  MO.setRegMask(RegMask);
68  }
69  }
70 };
71 } // end of anonymous namespace
73 
74 INITIALIZE_PASS_BEGIN(RegUsageInfoPropagationPass, "reg-usage-propagation",
75  RUIP_NAME, false, false)
77 INITIALIZE_PASS_END(RegUsageInfoPropagationPass, "reg-usage-propagation",
78  RUIP_NAME, false, false)
79 
81  return new RegUsageInfoPropagationPass();
82 }
83 
84 void RegUsageInfoPropagationPass::getAnalysisUsage(AnalysisUsage &AU) const {
86  AU.setPreservesAll();
88 }
89 
90 bool RegUsageInfoPropagationPass::runOnMachineFunction(MachineFunction &MF) {
91  const Module *M = MF.getFunction()->getParent();
92  PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
93 
94  DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName()
95  << " ++++++++++++++++++++ \n");
96  DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
97 
98  bool Changed = false;
99 
100  for (MachineBasicBlock &MBB : MF) {
101  for (MachineInstr &MI : MBB) {
102  if (!MI.isCall())
103  continue;
104  DEBUG(dbgs()
105  << "Call Instruction Before Register Usage Info Propagation : \n");
106  DEBUG(dbgs() << MI << "\n");
107 
108  auto UpdateRegMask = [&](const Function *F) {
109  const auto *RegMask = PRUI->getRegUsageInfo(F);
110  if (!RegMask)
111  return;
112  setRegMask(MI, &(*RegMask)[0]);
113  Changed = true;
114  };
115 
116  MachineOperand &Operand = MI.getOperand(0);
117  if (Operand.isGlobal())
118  UpdateRegMask(cast<Function>(Operand.getGlobal()));
119  else if (Operand.isSymbol())
120  UpdateRegMask(M->getFunction(Operand.getSymbolName()));
121 
122  DEBUG(dbgs()
123  << "Call Instruction After Register Usage Info Propagation : \n");
124  DEBUG(dbgs() << MI << "\n");
125  }
126  }
127 
128  DEBUG(dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
129  "++++++ \n");
130  return Changed;
131 }
const GlobalValue * getGlobal() const
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:45
const char * getSymbolName() const
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:301
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
reg usage false
INITIALIZE_PASS_BEGIN(RegUsageInfoPropagationPass,"reg-usage-propagation", RUIP_NAME, false, false) INITIALIZE_PASS_END(RegUsageInfoPropagationPass
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
#define F(x, y, z)
Definition: MD5.cpp:51
MachineBasicBlock * MBB
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:196
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.
Represent the analysis usage information of a pass.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
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
#define RUIP_NAME
void setPreservesAll()
Set by analyses that do not transform their input at all.
Representation of each machine instruction.
Definition: MachineInstr.h:52
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...
This pass is required to take advantage of the interprocedural register allocation infrastructure...
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:537
#define DEBUG(X)
Definition: Debug.h:100
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:40
StringRef getName() const
getName - Return the name of the corresponding LLVM function.