LLVM  8.0.1
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 using namespace llvm;
38 
39 #define DEBUG_TYPE "ip-regalloc"
40 
41 #define RUIP_NAME "Register Usage Information Propagation"
42 
43 namespace {
44 
45 class RegUsageInfoPropagation : public MachineFunctionPass {
46 public:
47  RegUsageInfoPropagation() : MachineFunctionPass(ID) {
50  }
51 
52  StringRef getPassName() const override { return RUIP_NAME; }
53 
54  bool runOnMachineFunction(MachineFunction &MF) override;
55 
56  void getAnalysisUsage(AnalysisUsage &AU) const override {
58  AU.setPreservesAll();
60  }
61 
62  static char ID;
63 
64 private:
65  static void setRegMask(MachineInstr &MI, ArrayRef<uint32_t> RegMask) {
66  assert(RegMask.size() ==
69  ->getNumRegs())
70  && "expected register mask size");
71  for (MachineOperand &MO : MI.operands()) {
72  if (MO.isRegMask())
73  MO.setRegMask(RegMask.data());
74  }
75  }
76 };
77 
78 } // end of anonymous namespace
79 
80 INITIALIZE_PASS_BEGIN(RegUsageInfoPropagation, "reg-usage-propagation",
81  RUIP_NAME, false, false)
83 INITIALIZE_PASS_END(RegUsageInfoPropagation, "reg-usage-propagation",
85 
86 char RegUsageInfoPropagation::ID = 0;
87 
88 // Assumes call instructions have a single reference to a function.
91  for (const MachineOperand &MO : MI.operands()) {
92  if (MO.isGlobal())
93  return dyn_cast<const Function>(MO.getGlobal());
94 
95  if (MO.isSymbol())
96  return M.getFunction(MO.getSymbolName());
97  }
98 
99  return nullptr;
100 }
101 
102 bool RegUsageInfoPropagation::runOnMachineFunction(MachineFunction &MF) {
103  const Module &M = *MF.getFunction().getParent();
104  PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
105 
106  LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName()
107  << " ++++++++++++++++++++ \n");
108  LLVM_DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
109 
110  const MachineFrameInfo &MFI = MF.getFrameInfo();
111  if (!MFI.hasCalls() && !MFI.hasTailCall())
112  return false;
113 
114  bool Changed = false;
115 
116  for (MachineBasicBlock &MBB : MF) {
117  for (MachineInstr &MI : MBB) {
118  if (!MI.isCall())
119  continue;
120  LLVM_DEBUG(
121  dbgs()
122  << "Call Instruction Before Register Usage Info Propagation : \n");
123  LLVM_DEBUG(dbgs() << MI << "\n");
124 
125  auto UpdateRegMask = [&](const Function &F) {
126  const ArrayRef<uint32_t> RegMask = PRUI->getRegUsageInfo(F);
127  if (RegMask.empty())
128  return;
129  setRegMask(MI, RegMask);
130  Changed = true;
131  };
132 
133  if (const Function *F = findCalledFunction(M, MI)) {
134  UpdateRegMask(*F);
135  } else {
136  LLVM_DEBUG(dbgs() << "Failed to find call target function\n");
137  }
138 
139  LLVM_DEBUG(
140  dbgs() << "Call Instruction After Register Usage Info Propagation : "
141  << MI << '\n');
142  }
143  }
144 
145  LLVM_DEBUG(
146  dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
147  "++++++ \n");
148  return Changed;
149 }
150 
152  return new RegUsageInfoPropagation();
153 }
INITIALIZE_PASS_BEGIN(RegUsageInfoPropagation, "reg-usage-propagation", RUIP_NAME, false, false) INITIALIZE_PASS_END(RegUsageInfoPropagation
static unsigned getRegMaskSize(unsigned NumRegs)
Returns number of elements needed for a regmask array.
ArrayRef< uint32_t > getRegUsageInfo(const Function &FP)
To query stored RegMask for given Function *, it will returns ane empty array if function is not know...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:24
static const Function * findCalledFunction(const Module &M, const MachineInstr &MI)
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
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:459
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...
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
const TargetRegisterInfo * getTargetRegisterInfo() const
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.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
const T * data() const
Definition: ArrayRef.h:146
reg usage propagation
MachineOperand class - Representation of each machine instruction operand.
Module.h This file contains the declarations for the Module class.
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
#define RUIP_NAME
void setPreservesAll()
Set by analyses that do not transform their input at all.
void initializeRegUsageInfoPropagationPass(PassRegistry &)
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:254
Representation of each machine instruction.
Definition: MachineInstr.h:64
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
This pass is required to take advantage of the interprocedural register allocation infrastructure...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
aarch64 promote const
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:566
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
#define LLVM_DEBUG(X)
Definition: Debug.h:123
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144