LLVM  9.0.0svn
RegUsageInfoPropagate.cpp
Go to the documentation of this file.
1 //=--- RegUsageInfoPropagate.cpp - Register Usage Informartion Propagation --=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// This pass is required to take advantage of the interprocedural register
10 /// allocation infrastructure.
11 ///
12 /// This pass iterates through MachineInstrs in a given MachineFunction and at
13 /// each callsite queries RegisterUsageInfo for RegMask (calculated based on
14 /// actual register allocation) of the callee function, if the RegMask detail
15 /// is available then this pass will update the RegMask of the call instruction.
16 /// This updated RegMask will be used by the register allocator while allocating
17 /// the current MachineFunction.
18 ///
19 //===----------------------------------------------------------------------===//
20 
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 using namespace llvm;
37 
38 #define DEBUG_TYPE "ip-regalloc"
39 
40 #define RUIP_NAME "Register Usage Information Propagation"
41 
42 namespace {
43 
44 class RegUsageInfoPropagation : public MachineFunctionPass {
45 public:
46  RegUsageInfoPropagation() : MachineFunctionPass(ID) {
49  }
50 
51  StringRef getPassName() const override { return RUIP_NAME; }
52 
53  bool runOnMachineFunction(MachineFunction &MF) override;
54 
55  void getAnalysisUsage(AnalysisUsage &AU) const override {
57  AU.setPreservesAll();
59  }
60 
61  static char ID;
62 
63 private:
64  static void setRegMask(MachineInstr &MI, ArrayRef<uint32_t> RegMask) {
65  assert(RegMask.size() ==
68  ->getNumRegs())
69  && "expected register mask size");
70  for (MachineOperand &MO : MI.operands()) {
71  if (MO.isRegMask())
72  MO.setRegMask(RegMask.data());
73  }
74  }
75 };
76 
77 } // end of anonymous namespace
78 
79 INITIALIZE_PASS_BEGIN(RegUsageInfoPropagation, "reg-usage-propagation",
80  RUIP_NAME, false, false)
82 INITIALIZE_PASS_END(RegUsageInfoPropagation, "reg-usage-propagation",
84 
85 char RegUsageInfoPropagation::ID = 0;
86 
87 // Assumes call instructions have a single reference to a function.
90  for (const MachineOperand &MO : MI.operands()) {
91  if (MO.isGlobal())
92  return dyn_cast<const Function>(MO.getGlobal());
93 
94  if (MO.isSymbol())
95  return M.getFunction(MO.getSymbolName());
96  }
97 
98  return nullptr;
99 }
100 
101 bool RegUsageInfoPropagation::runOnMachineFunction(MachineFunction &MF) {
102  const Module &M = *MF.getFunction().getParent();
103  PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
104 
105  LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName()
106  << " ++++++++++++++++++++ \n");
107  LLVM_DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
108 
109  const MachineFrameInfo &MFI = MF.getFrameInfo();
110  if (!MFI.hasCalls() && !MFI.hasTailCall())
111  return false;
112 
113  bool Changed = false;
114 
115  for (MachineBasicBlock &MBB : MF) {
116  for (MachineInstr &MI : MBB) {
117  if (!MI.isCall())
118  continue;
119  LLVM_DEBUG(
120  dbgs()
121  << "Call Instruction Before Register Usage Info Propagation : \n");
122  LLVM_DEBUG(dbgs() << MI << "\n");
123 
124  auto UpdateRegMask = [&](const Function &F) {
125  const ArrayRef<uint32_t> RegMask = PRUI->getRegUsageInfo(F);
126  if (RegMask.empty())
127  return;
128  setRegMask(MI, RegMask);
129  Changed = true;
130  };
131 
132  if (const Function *F = findCalledFunction(M, MI)) {
133  UpdateRegMask(*F);
134  } else {
135  LLVM_DEBUG(dbgs() << "Failed to find call target function\n");
136  }
137 
138  LLVM_DEBUG(
139  dbgs() << "Call Instruction After Register Usage Info Propagation : "
140  << MI << '\n');
141  }
142  }
143 
144  LLVM_DEBUG(
145  dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
146  "++++++ \n");
147  return Changed;
148 }
149 
151  return new RegUsageInfoPropagation();
152 }
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:23
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:44
F(f)
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:460
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
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:148
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
const T * data() const
Definition: ArrayRef.h:145
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:132
#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:255
Representation of each machine instruction.
Definition: MachineInstr.h:63
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:575
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
#define LLVM_DEBUG(X)
Definition: Debug.h:122
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:143