LLVM  6.0.0svn
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  const BitVector &UsedPhysRegsMask = MRI->getUsedPhysRegsMask();
107  auto SetRegAsDefined = [&RegMask] (unsigned Reg) {
108  RegMask[Reg / 32] &= ~(1u << Reg % 32);
109  };
110  // Scan all the physical registers. When a register is defined in the current
111  // function set it and all the aliasing registers as defined in the regmask.
112  for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
113  // If a register is in the UsedPhysRegsMask set then mark it as defined.
114  // All it's aliases will also be in the set, so we can skip setting
115  // as defined all the aliases here.
116  if (UsedPhysRegsMask.test(PReg)) {
117  SetRegAsDefined(PReg);
118  continue;
119  }
120  // If a register is defined by an instruction mark it as defined together
121  // with all it's aliases.
122  if (!MRI->def_empty(PReg)) {
123  for (MCRegAliasIterator AI(PReg, TRI, true); AI.isValid(); ++AI)
124  SetRegAsDefined(*AI);
125  }
126  }
127 
129  const uint32_t *CallPreservedMask =
130  TRI->getCallPreservedMask(MF, F->getCallingConv());
131  if (CallPreservedMask) {
132  // Set callee saved register as preserved.
133  for (unsigned i = 0; i < RegMaskSize; ++i)
134  RegMask[i] = RegMask[i] | CallPreservedMask[i];
135  }
136  } else {
137  ++NumCSROpt;
138  DEBUG(dbgs() << MF.getName()
139  << " function optimized for not having CSR.\n");
140  }
141 
142  for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg)
143  if (MachineOperand::clobbersPhysReg(&(RegMask[0]), PReg))
144  DEBUG(dbgs() << TRI->getName(PReg) << " ");
145 
146  DEBUG(dbgs() << " \n----------------------------------------\n");
147 
148  PRUI->storeUpdateRegUsageInfo(F, std::move(RegMask));
149 
150  return false;
151 }
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
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:45
bool test(unsigned Idx) const
Definition: BitVector.h:502
void storeUpdateRegUsageInfo(const Function *FP, std::vector< uint32_t > RegMask)
To store RegMask for given Function *.
void initializeRegUsageInfoCollectorPass(PassRegistry &)
STATISTIC(NumFunctions, "Total number of functions")
F(f)
void setTargetMachine(const TargetMachine *TM_)
To set TargetMachine *, which is used to print analysis when command line option -print-regusage is u...
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Reg
All possible values of the reg field in the ModR/M byte.
FunctionPass * createRegUsageInfoCollector()
This pass is executed POST-RA to collect which physical registers are preserved by given machine func...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
const char * getName(unsigned RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register...
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
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...
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MCRegAliasIterator enumerates all registers aliasing Reg.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
bool def_empty(unsigned RegNo) const
def_empty - Return true if there are no instructions defining the specified register (it may be live-...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
INITIALIZE_PASS_BEGIN(RegUsageInfoCollector, "RegUsageInfoCollector", "Register Usage Information Collector", false, false) INITIALIZE_PASS_END(RegUsageInfoCollector
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:194
Promote Memory to Register
Definition: Mem2Reg.cpp:110
Natural Loop Information
Definition: LoopInfo.cpp:750
const BitVector & getUsedPhysRegsMask() const
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.
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.
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
#define DEBUG(X)
Definition: Debug.h:118
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:57
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
Register Usage Information Collector