LLVM 20.0.0git
RegisterUsageInfo.cpp
Go to the documentation of this file.
1//===- RegisterUsageInfo.cpp - Register Usage Information Storage ---------===//
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//===----------------------------------------------------------------------===//
13
19#include "llvm/IR/Analysis.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/Module.h"
22#include "llvm/IR/PassManager.h"
23#include "llvm/Pass.h"
27#include <cstdint>
28#include <utility>
29#include <vector>
30
31using namespace llvm;
32
34 "print-regusage", cl::init(false), cl::Hidden,
35 cl::desc("print register usage details collected for analysis."));
36
38 "Register Usage Information Storage", false, true)
39
41
42void PhysicalRegisterUsageInfo::setTargetMachine(const TargetMachine &TM) {
43 this->TM = &TM;
44}
45
47 RegMasks.grow(M.size());
48 return false;
49}
50
52 if (DumpRegUsage)
53 print(errs());
54
55 RegMasks.shrink_and_clear();
56 return false;
57}
58
60 const Function &FP, ArrayRef<uint32_t> RegMask) {
61 RegMasks[&FP] = RegMask;
62}
63
66 auto It = RegMasks.find(&FP);
67 if (It != RegMasks.end())
68 return ArrayRef<uint32_t>(It->second);
69 return ArrayRef<uint32_t>();
70}
71
73 using FuncPtrRegMaskPair = std::pair<const Function *, std::vector<uint32_t>>;
74
76
77 // Create a vector of pointer to RegMasks entries
78 for (const auto &RegMask : RegMasks)
79 FPRMPairVector.push_back(&RegMask);
80
81 // sort the vector to print analysis in alphabatic order of function name.
83 FPRMPairVector,
84 [](const FuncPtrRegMaskPair *A, const FuncPtrRegMaskPair *B) -> bool {
85 return A->first->getName() < B->first->getName();
86 });
87
88 for (const FuncPtrRegMaskPair *FPRMPair : FPRMPairVector) {
89 OS << FPRMPair->first->getName() << " "
90 << "Clobbered Registers: ";
92 = TM->getSubtarget<TargetSubtargetInfo>(*(FPRMPair->first))
94
95 for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
96 if (MachineOperand::clobbersPhysReg(&(FPRMPair->second[0]), PReg))
97 OS << printReg(PReg, TRI) << " ";
98 }
99 OS << "\n";
100 }
101}
102
104 Module &M, const PreservedAnalyses &PA,
107 return !PAC.preservedWhenStateless();
108}
109
110AnalysisKey PhysicalRegisterUsageAnalysis::Key;
114 PRUI.doInitialization(M);
115 return PRUI;
116}
117
121 auto *PRUI = &AM.getResult<PhysicalRegisterUsageAnalysis>(M);
122 PRUI->print(OS, &M);
123 return PreservedAnalyses::all();
124}
aarch64 promote const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
static cl::opt< bool > DumpRegUsage("print-regusage", cl::init(false), cl::Hidden, cl::desc("print register usage details collected for analysis."))
This pass is required to take advantage of the interprocedural register allocation infrastructure.
raw_pwrite_stream & OS
This file defines the SmallVector class.
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
PhysicalRegisterUsageInfo run(Module &M, ModuleAnalysisManager &)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
bool invalidate(Module &M, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &Inv)
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...
void print(raw_ostream &OS, const Module *M=nullptr) const
void storeUpdateRegUsageInfo(const Function &FP, ArrayRef< uint32_t > RegMask)
To store RegMask for given Function *.
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: Analysis.h:264
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
const STC & getSubtarget(const Function &F) const
This method returns a pointer to the specified type of TargetSubtargetInfo.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Pass manager infrastructure for declaring and invalidating analyses.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1664
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28