xref: /aosp_15_r20/external/llvm/lib/CodeGen/StackMapLivenessAnalysis.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1 *9880d681SAndroid Build Coastguard Worker //===-- StackMapLivenessAnalysis.cpp - StackMap live Out Analysis ----------===//
2 *9880d681SAndroid Build Coastguard Worker //
3 *9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4 *9880d681SAndroid Build Coastguard Worker //
5 *9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6 *9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7 *9880d681SAndroid Build Coastguard Worker //
8 *9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9 *9880d681SAndroid Build Coastguard Worker //
10 *9880d681SAndroid Build Coastguard Worker // This file implements the StackMap Liveness analysis pass. The pass calculates
11 *9880d681SAndroid Build Coastguard Worker // the liveness for each basic block in a function and attaches the register
12 *9880d681SAndroid Build Coastguard Worker // live-out information to a stackmap or patchpoint intrinsic if present.
13 *9880d681SAndroid Build Coastguard Worker //
14 *9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15 *9880d681SAndroid Build Coastguard Worker 
16 *9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
17 *9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LivePhysRegs.h"
18 *9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
19 *9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
20 *9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionAnalysis.h"
21 *9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
22 *9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
23 *9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
24 *9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
25 *9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
26 *9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
27 *9880d681SAndroid Build Coastguard Worker 
28 *9880d681SAndroid Build Coastguard Worker using namespace llvm;
29 *9880d681SAndroid Build Coastguard Worker 
30 *9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "stackmaps"
31 *9880d681SAndroid Build Coastguard Worker 
32 *9880d681SAndroid Build Coastguard Worker static cl::opt<bool> EnablePatchPointLiveness(
33 *9880d681SAndroid Build Coastguard Worker     "enable-patchpoint-liveness", cl::Hidden, cl::init(true),
34 *9880d681SAndroid Build Coastguard Worker     cl::desc("Enable PatchPoint Liveness Analysis Pass"));
35 *9880d681SAndroid Build Coastguard Worker 
36 *9880d681SAndroid Build Coastguard Worker STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
37 *9880d681SAndroid Build Coastguard Worker STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
38 *9880d681SAndroid Build Coastguard Worker STATISTIC(NumBBsVisited,          "Number of basic blocks visited");
39 *9880d681SAndroid Build Coastguard Worker STATISTIC(NumBBsHaveNoStackmap,   "Number of basic blocks with no stackmap");
40 *9880d681SAndroid Build Coastguard Worker STATISTIC(NumStackMaps,           "Number of StackMaps visited");
41 *9880d681SAndroid Build Coastguard Worker 
42 *9880d681SAndroid Build Coastguard Worker namespace {
43 *9880d681SAndroid Build Coastguard Worker /// \brief This pass calculates the liveness information for each basic block in
44 *9880d681SAndroid Build Coastguard Worker /// a function and attaches the register live-out information to a patchpoint
45 *9880d681SAndroid Build Coastguard Worker /// intrinsic if present.
46 *9880d681SAndroid Build Coastguard Worker ///
47 *9880d681SAndroid Build Coastguard Worker /// This pass can be disabled via the -enable-patchpoint-liveness=false flag.
48 *9880d681SAndroid Build Coastguard Worker /// The pass skips functions that don't have any patchpoint intrinsics. The
49 *9880d681SAndroid Build Coastguard Worker /// information provided by this pass is optional and not required by the
50 *9880d681SAndroid Build Coastguard Worker /// aformentioned intrinsic to function.
51 *9880d681SAndroid Build Coastguard Worker class StackMapLiveness : public MachineFunctionPass {
52 *9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo *TRI;
53 *9880d681SAndroid Build Coastguard Worker   LivePhysRegs LiveRegs;
54 *9880d681SAndroid Build Coastguard Worker 
55 *9880d681SAndroid Build Coastguard Worker public:
56 *9880d681SAndroid Build Coastguard Worker   static char ID;
57 *9880d681SAndroid Build Coastguard Worker 
58 *9880d681SAndroid Build Coastguard Worker   /// \brief Default construct and initialize the pass.
59 *9880d681SAndroid Build Coastguard Worker   StackMapLiveness();
60 *9880d681SAndroid Build Coastguard Worker 
61 *9880d681SAndroid Build Coastguard Worker   /// \brief Tell the pass manager which passes we depend on and what
62 *9880d681SAndroid Build Coastguard Worker   /// information we preserve.
63 *9880d681SAndroid Build Coastguard Worker   void getAnalysisUsage(AnalysisUsage &AU) const override;
64 *9880d681SAndroid Build Coastguard Worker 
getRequiredProperties() const65 *9880d681SAndroid Build Coastguard Worker   MachineFunctionProperties getRequiredProperties() const override {
66 *9880d681SAndroid Build Coastguard Worker     return MachineFunctionProperties().set(
67 *9880d681SAndroid Build Coastguard Worker         MachineFunctionProperties::Property::AllVRegsAllocated);
68 *9880d681SAndroid Build Coastguard Worker   }
69 *9880d681SAndroid Build Coastguard Worker 
70 *9880d681SAndroid Build Coastguard Worker   /// \brief Calculate the liveness information for the given machine function.
71 *9880d681SAndroid Build Coastguard Worker   bool runOnMachineFunction(MachineFunction &MF) override;
72 *9880d681SAndroid Build Coastguard Worker 
73 *9880d681SAndroid Build Coastguard Worker private:
74 *9880d681SAndroid Build Coastguard Worker   /// \brief Performs the actual liveness calculation for the function.
75 *9880d681SAndroid Build Coastguard Worker   bool calculateLiveness(MachineFunction &MF);
76 *9880d681SAndroid Build Coastguard Worker 
77 *9880d681SAndroid Build Coastguard Worker   /// \brief Add the current register live set to the instruction.
78 *9880d681SAndroid Build Coastguard Worker   void addLiveOutSetToMI(MachineFunction &MF, MachineInstr &MI);
79 *9880d681SAndroid Build Coastguard Worker 
80 *9880d681SAndroid Build Coastguard Worker   /// \brief Create a register mask and initialize it with the registers from
81 *9880d681SAndroid Build Coastguard Worker   /// the register live set.
82 *9880d681SAndroid Build Coastguard Worker   uint32_t *createRegisterMask(MachineFunction &MF) const;
83 *9880d681SAndroid Build Coastguard Worker };
84 *9880d681SAndroid Build Coastguard Worker } // namespace
85 *9880d681SAndroid Build Coastguard Worker 
86 *9880d681SAndroid Build Coastguard Worker char StackMapLiveness::ID = 0;
87 *9880d681SAndroid Build Coastguard Worker char &llvm::StackMapLivenessID = StackMapLiveness::ID;
88 *9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
89 *9880d681SAndroid Build Coastguard Worker                 "StackMap Liveness Analysis", false, false)
90 *9880d681SAndroid Build Coastguard Worker 
91 *9880d681SAndroid Build Coastguard Worker /// Default construct and initialize the pass.
StackMapLiveness()92 *9880d681SAndroid Build Coastguard Worker StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
93 *9880d681SAndroid Build Coastguard Worker   initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
94 *9880d681SAndroid Build Coastguard Worker }
95 *9880d681SAndroid Build Coastguard Worker 
96 *9880d681SAndroid Build Coastguard Worker /// Tell the pass manager which passes we depend on and what information we
97 *9880d681SAndroid Build Coastguard Worker /// preserve.
getAnalysisUsage(AnalysisUsage & AU) const98 *9880d681SAndroid Build Coastguard Worker void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
99 *9880d681SAndroid Build Coastguard Worker   // We preserve all information.
100 *9880d681SAndroid Build Coastguard Worker   AU.setPreservesAll();
101 *9880d681SAndroid Build Coastguard Worker   AU.setPreservesCFG();
102 *9880d681SAndroid Build Coastguard Worker   MachineFunctionPass::getAnalysisUsage(AU);
103 *9880d681SAndroid Build Coastguard Worker }
104 *9880d681SAndroid Build Coastguard Worker 
105 *9880d681SAndroid Build Coastguard Worker /// Calculate the liveness information for the given machine function.
runOnMachineFunction(MachineFunction & MF)106 *9880d681SAndroid Build Coastguard Worker bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) {
107 *9880d681SAndroid Build Coastguard Worker   if (!EnablePatchPointLiveness)
108 *9880d681SAndroid Build Coastguard Worker     return false;
109 *9880d681SAndroid Build Coastguard Worker 
110 *9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: " << MF.getName()
111 *9880d681SAndroid Build Coastguard Worker                << " **********\n");
112 *9880d681SAndroid Build Coastguard Worker   TRI = MF.getSubtarget().getRegisterInfo();
113 *9880d681SAndroid Build Coastguard Worker   ++NumStackMapFuncVisited;
114 *9880d681SAndroid Build Coastguard Worker 
115 *9880d681SAndroid Build Coastguard Worker   // Skip this function if there are no patchpoints to process.
116 *9880d681SAndroid Build Coastguard Worker   if (!MF.getFrameInfo()->hasPatchPoint()) {
117 *9880d681SAndroid Build Coastguard Worker     ++NumStackMapFuncSkipped;
118 *9880d681SAndroid Build Coastguard Worker     return false;
119 *9880d681SAndroid Build Coastguard Worker   }
120 *9880d681SAndroid Build Coastguard Worker   return calculateLiveness(MF);
121 *9880d681SAndroid Build Coastguard Worker }
122 *9880d681SAndroid Build Coastguard Worker 
123 *9880d681SAndroid Build Coastguard Worker /// Performs the actual liveness calculation for the function.
calculateLiveness(MachineFunction & MF)124 *9880d681SAndroid Build Coastguard Worker bool StackMapLiveness::calculateLiveness(MachineFunction &MF) {
125 *9880d681SAndroid Build Coastguard Worker   bool HasChanged = false;
126 *9880d681SAndroid Build Coastguard Worker   // For all basic blocks in the function.
127 *9880d681SAndroid Build Coastguard Worker   for (auto &MBB : MF) {
128 *9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "****** BB " << MBB.getName() << " ******\n");
129 *9880d681SAndroid Build Coastguard Worker     LiveRegs.init(TRI);
130 *9880d681SAndroid Build Coastguard Worker     // FIXME: This should probably be addLiveOuts().
131 *9880d681SAndroid Build Coastguard Worker     LiveRegs.addLiveOutsNoPristines(MBB);
132 *9880d681SAndroid Build Coastguard Worker     bool HasStackMap = false;
133 *9880d681SAndroid Build Coastguard Worker     // Reverse iterate over all instructions and add the current live register
134 *9880d681SAndroid Build Coastguard Worker     // set to an instruction if we encounter a patchpoint instruction.
135 *9880d681SAndroid Build Coastguard Worker     for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) {
136 *9880d681SAndroid Build Coastguard Worker       if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
137 *9880d681SAndroid Build Coastguard Worker         addLiveOutSetToMI(MF, *I);
138 *9880d681SAndroid Build Coastguard Worker         HasChanged = true;
139 *9880d681SAndroid Build Coastguard Worker         HasStackMap = true;
140 *9880d681SAndroid Build Coastguard Worker         ++NumStackMaps;
141 *9880d681SAndroid Build Coastguard Worker       }
142 *9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "   " << LiveRegs << "   " << *I);
143 *9880d681SAndroid Build Coastguard Worker       LiveRegs.stepBackward(*I);
144 *9880d681SAndroid Build Coastguard Worker     }
145 *9880d681SAndroid Build Coastguard Worker     ++NumBBsVisited;
146 *9880d681SAndroid Build Coastguard Worker     if (!HasStackMap)
147 *9880d681SAndroid Build Coastguard Worker       ++NumBBsHaveNoStackmap;
148 *9880d681SAndroid Build Coastguard Worker   }
149 *9880d681SAndroid Build Coastguard Worker   return HasChanged;
150 *9880d681SAndroid Build Coastguard Worker }
151 *9880d681SAndroid Build Coastguard Worker 
152 *9880d681SAndroid Build Coastguard Worker /// Add the current register live set to the instruction.
addLiveOutSetToMI(MachineFunction & MF,MachineInstr & MI)153 *9880d681SAndroid Build Coastguard Worker void StackMapLiveness::addLiveOutSetToMI(MachineFunction &MF,
154 *9880d681SAndroid Build Coastguard Worker                                          MachineInstr &MI) {
155 *9880d681SAndroid Build Coastguard Worker   uint32_t *Mask = createRegisterMask(MF);
156 *9880d681SAndroid Build Coastguard Worker   MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
157 *9880d681SAndroid Build Coastguard Worker   MI.addOperand(MF, MO);
158 *9880d681SAndroid Build Coastguard Worker }
159 *9880d681SAndroid Build Coastguard Worker 
160 *9880d681SAndroid Build Coastguard Worker /// Create a register mask and initialize it with the registers from the
161 *9880d681SAndroid Build Coastguard Worker /// register live set.
createRegisterMask(MachineFunction & MF) const162 *9880d681SAndroid Build Coastguard Worker uint32_t *StackMapLiveness::createRegisterMask(MachineFunction &MF) const {
163 *9880d681SAndroid Build Coastguard Worker   // The mask is owned and cleaned up by the Machine Function.
164 *9880d681SAndroid Build Coastguard Worker   uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
165 *9880d681SAndroid Build Coastguard Worker   for (auto Reg : LiveRegs)
166 *9880d681SAndroid Build Coastguard Worker     Mask[Reg / 32] |= 1U << (Reg % 32);
167 *9880d681SAndroid Build Coastguard Worker 
168 *9880d681SAndroid Build Coastguard Worker   // Give the target a chance to adjust the mask.
169 *9880d681SAndroid Build Coastguard Worker   TRI->adjustStackMapLiveOutMask(Mask);
170 *9880d681SAndroid Build Coastguard Worker 
171 *9880d681SAndroid Build Coastguard Worker   return Mask;
172 *9880d681SAndroid Build Coastguard Worker }
173