LLVM  4.0.0
RegUsageInfoCollector.cpp
Go to the documentation of this file.
1 //===-- RegUsageInfoCollector.cpp - Register Usage Information Collector --===//
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 is simple MachineFunction pass which collects register usage
14 /// details by iterating through each physical registers and checking
15 /// MRI::isPhysRegUsed() then creates a RegMask based on this details.
16 /// The pass then stores this RegMask in PhysicalRegisterUsageInfo.cpp
17 ///
18 //===----------------------------------------------------------------------===//
19 
20 #include "llvm/ADT/Statistic.h"
26 #include "llvm/CodeGen/Passes.h"
28 #include "llvm/Support/Debug.h"
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "ip-regalloc"
35 
36 STATISTIC(NumCSROpt,
37  "Number of functions optimized for callee saved registers");
38 
39 namespace llvm {
41 }
42 
43 namespace {
45 public:
49  }
50 
51  StringRef getPassName() const override {
52  return "Register Usage Information Collector Pass";
53  }
54 
55  void getAnalysisUsage(AnalysisUsage &AU) const override;
56 
57  bool runOnMachineFunction(MachineFunction &MF) override;
58 
59  static char ID;
60 };
61 } // end of anonymous namespace
62 
64 
65 INITIALIZE_PASS_BEGIN(RegUsageInfoCollector, "RegUsageInfoCollector",
66  "Register Usage Information Collector", false, false)
68 INITIALIZE_PASS_END(RegUsageInfoCollector, "RegUsageInfoCollector",
69  "Register Usage Information Collector", false, false)
70 
72  return new RegUsageInfoCollector();
73 }
74 
75 void RegUsageInfoCollector::getAnalysisUsage(AnalysisUsage &AU) const {
77  AU.setPreservesAll();
79 }
80 
81 bool RegUsageInfoCollector::runOnMachineFunction(MachineFunction &MF) {
84  const TargetMachine &TM = MF.getTarget();
85 
86  DEBUG(dbgs() << " -------------------- " << getPassName()
87  << " -------------------- \n");
88  DEBUG(dbgs() << "Function Name : " << MF.getName() << "\n");
89 
90  std::vector<uint32_t> RegMask;
91 
92  // Compute the size of the bit vector to represent all the registers.
93  // The bit vector is broken into 32-bit chunks, thus takes the ceil of
94  // the number of registers divided by 32 for the size.
95  unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32;
96  RegMask.resize(RegMaskSize, 0xFFFFFFFF);
97 
98  const Function *F = MF.getFunction();
99 
100  PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
101 
102  PRUI->setTargetMachine(&TM);
103 
104  DEBUG(dbgs() << "Clobbered Registers: ");
105 
106  for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg)
107  if (MRI->isPhysRegModified(PReg, true))
108  RegMask[PReg / 32] &= ~(1u << PReg % 32);
109 
111  const uint32_t *CallPreservedMask =
112  TRI->getCallPreservedMask(MF, F->getCallingConv());
113  // Set callee saved register as preserved.
114  for (unsigned i = 0; i < RegMaskSize; ++i)
115  RegMask[i] = RegMask[i] | CallPreservedMask[i];
116  } else {
117  ++NumCSROpt;
118  DEBUG(dbgs() << MF.getName()
119  << " function optimized for not having CSR.\n");
120  }
121 
122  for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg)
123  if (MachineOperand::clobbersPhysReg(&(RegMask[0]), PReg))
124  DEBUG(dbgs() << TRI->getName(PReg) << " ");
125 
126  DEBUG(dbgs() << " \n----------------------------------------\n");
127 
128  PRUI->storeUpdateRegUsageInfo(F, std::move(RegMask));
129 
130  return false;
131 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
size_t i
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:45
void storeUpdateRegUsageInfo(const Function *FP, std::vector< uint32_t > RegMask)
To store RegMask for given Function *.
void initializeRegUsageInfoCollectorPass(PassRegistry &)
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
void setTargetMachine(const TargetMachine *TM_)
To set TargetMachine *, which is used to print analysis when command line option -print-regusage is u...
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:165
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
FunctionPass * createRegUsageInfoCollector()
This pass is executed POST-RA to collect which physical registers are preserved by given machine func...
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
#define F(x, y, z)
Definition: MD5.cpp:51
unsigned const MachineRegisterInfo * MRI
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
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
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Promote Memory to Register
Definition: Mem2Reg.cpp:100
Natural Loop Information
Definition: LoopInfo.cpp:709
Register Usage Information false
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
virtual const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID) const
Return a mask of call-preserved registers for the given calling convention on the current function...
void setPreservesAll()
Set by analyses that do not transform their input at all.
RegUsageInfoCollector
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
static bool isSafeForNoCSROpt(const Function *F)
Check if given function is safe for not having callee saved registers.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
INITIALIZE_PASS_BEGIN(RegUsageInfoCollector,"RegUsageInfoCollector","Register Usage Information Collector", false, false) INITIALIZE_PASS_END(RegUsageInfoCollector
bool isPhysRegModified(unsigned PhysReg, bool SkipNoReturnDef=false) const
Return true if the specified register is modified in this function.
This pass is required to take advantage of the interprocedural register allocation infrastructure...
#define DEBUG(X)
Definition: Debug.h:100
Primary interface to the complete machine description for the target machine.
const char * getName(unsigned RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register...
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
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.
Register Usage Information Collector