LLVM  15.0.0git
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"
29 #include "llvm/Pass.h"
30 #include "llvm/Support/Debug.h"
32 
33 using namespace llvm;
34 
35 #define DEBUG_TYPE "ip-regalloc"
36 
37 #define RUIP_NAME "Register Usage Information Propagation"
38 
39 namespace {
40 
41 class RegUsageInfoPropagation : public MachineFunctionPass {
42 public:
43  RegUsageInfoPropagation() : MachineFunctionPass(ID) {
46  }
47 
48  StringRef getPassName() const override { return RUIP_NAME; }
49 
50  bool runOnMachineFunction(MachineFunction &MF) override;
51 
52  void getAnalysisUsage(AnalysisUsage &AU) const override {
54  AU.setPreservesAll();
56  }
57 
58  static char ID;
59 
60 private:
61  static void setRegMask(MachineInstr &MI, ArrayRef<uint32_t> RegMask) {
62  assert(RegMask.size() ==
63  MachineOperand::getRegMaskSize(MI.getParent()->getParent()
64  ->getRegInfo().getTargetRegisterInfo()
65  ->getNumRegs())
66  && "expected register mask size");
67  for (MachineOperand &MO : MI.operands()) {
68  if (MO.isRegMask())
69  MO.setRegMask(RegMask.data());
70  }
71  }
72 };
73 
74 } // end of anonymous namespace
75 
76 INITIALIZE_PASS_BEGIN(RegUsageInfoPropagation, "reg-usage-propagation",
77  RUIP_NAME, false, false)
79 INITIALIZE_PASS_END(RegUsageInfoPropagation, "reg-usage-propagation",
81 
82 char RegUsageInfoPropagation::ID = 0;
83 
84 // Assumes call instructions have a single reference to a function.
87  for (const MachineOperand &MO : MI.operands()) {
88  if (MO.isGlobal())
89  return dyn_cast<const Function>(MO.getGlobal());
90 
91  if (MO.isSymbol())
92  return M.getFunction(MO.getSymbolName());
93  }
94 
95  return nullptr;
96 }
97 
98 bool RegUsageInfoPropagation::runOnMachineFunction(MachineFunction &MF) {
99  const Module &M = *MF.getFunction().getParent();
100  PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
101 
102  LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName()
103  << " ++++++++++++++++++++ \n");
104  LLVM_DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
105 
106  const MachineFrameInfo &MFI = MF.getFrameInfo();
107  if (!MFI.hasCalls() && !MFI.hasTailCall())
108  return false;
109 
110  bool Changed = false;
111 
112  for (MachineBasicBlock &MBB : MF) {
113  for (MachineInstr &MI : MBB) {
114  if (!MI.isCall())
115  continue;
116  LLVM_DEBUG(
117  dbgs()
118  << "Call Instruction Before Register Usage Info Propagation : \n"
119  << MI << "\n");
120 
121  auto UpdateRegMask = [&](const Function &F) {
122  const ArrayRef<uint32_t> RegMask = PRUI->getRegUsageInfo(F);
123  if (RegMask.empty())
124  return;
125  setRegMask(MI, RegMask);
126  Changed = true;
127  };
128 
129  if (const Function *F = findCalledFunction(M, MI)) {
130  if (F->isDefinitionExact()) {
131  UpdateRegMask(*F);
132  } else {
133  LLVM_DEBUG(dbgs() << "Function definition is not exact\n");
134  }
135  } else {
136  LLVM_DEBUG(dbgs() << "Failed to find call target function\n");
137  }
138 
139  LLVM_DEBUG(
140  dbgs()
141  << "Call Instruction After Register Usage Info Propagation : \n"
142  << MI << '\n');
143  }
144  }
145 
146  LLVM_DEBUG(
147  dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
148  "++++++ \n");
149  return Changed;
150 }
151 
153  return new RegUsageInfoPropagation();
154 }
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
MachineInstr.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::Function
Definition: Function.h:60
Pass.h
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(RegUsageInfoPropagation, "reg-usage-propagation", RUIP_NAME, false, false) INITIALIZE_PASS_END(RegUsageInfoPropagation
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
MachineBasicBlock.h
Module.h
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:159
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineFunctionPass.cpp:103
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::ArrayRef::data
const T * data() const
Definition: ArrayRef.h:161
F
#define F(x, y, z)
Definition: MD5.cpp:55
MachineRegisterInfo.h
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::PhysicalRegisterUsageInfo::getRegUsageInfo
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...
Definition: RegisterUsageInfo.cpp:63
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
findCalledFunction
static const Function * findCalledFunction(const Module &M, const MachineInstr &MI)
Definition: RegUsageInfoPropagate.cpp:85
llvm::MachineOperand::getRegMaskSize
static unsigned getRegMaskSize(unsigned NumRegs)
Returns number of elements needed for a regmask array.
Definition: MachineOperand.h:645
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
false
Definition: StackSlotColoring.cpp:141
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:48
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
RUIP_NAME
#define RUIP_NAME
Definition: RegUsageInfoPropagate.cpp:37
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
Passes.h
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:577
const
aarch64 promote const
Definition: AArch64PromoteConstant.cpp:232
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
llvm::createRegUsageInfoPropPass
FunctionPass * createRegUsageInfoPropPass()
Return a MachineFunction pass that identifies call sites and propagates register usage information of...
Definition: RegUsageInfoPropagate.cpp:152
MachineFunctionPass.h
llvm::MachineFunction::getName
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Definition: MachineFunction.cpp:567
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MachineFunction::getFrameInfo
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Definition: MachineFunction.h:656
llvm::initializeRegUsageInfoPropagationPass
void initializeRegUsageInfoPropagationPass(PassRegistry &)
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::PhysicalRegisterUsageInfo
Definition: RegisterUsageInfo.h:34
llvm::MachineFunction
Definition: MachineFunction.h:241
llvm::ArrayRef< uint32_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
RegisterUsageInfo.h
llvm::MachineFrameInfo::hasCalls
bool hasCalls() const
Return true if the current function has any function calls.
Definition: MachineFrameInfo.h:605
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:606
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
MachineFrameInfo.h
llvm::MachineFrameInfo
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Definition: MachineFrameInfo.h:105
llvm::Registry
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
propagation
reg usage propagation
Definition: RegUsageInfoPropagate.cpp:79
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::MachineFrameInfo::hasTailCall
bool hasTailCall() const
Returns true if the function contains a tail call.
Definition: MachineFrameInfo.h:630
raw_ostream.h
Debug.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37