xref: /minix3/external/bsd/llvm/dist/llvm/lib/CodeGen/StackMapLivenessAnalysis.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===-- StackMapLivenessAnalysis.cpp - StackMap live Out Analysis ----------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // This file implements the StackMap Liveness analysis pass. The pass calculates
11*0a6a1f1dSLionel Sambuc // the liveness for each basic block in a function and attaches the register
12*0a6a1f1dSLionel Sambuc // live-out information to a stackmap or patchpoint intrinsic if present.
13*0a6a1f1dSLionel Sambuc //
14*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
15*0a6a1f1dSLionel Sambuc 
16*0a6a1f1dSLionel Sambuc #include "llvm/ADT/Statistic.h"
17*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/MachineFrameInfo.h"
18*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/MachineFunction.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/MachineFunctionAnalysis.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/Passes.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/StackMapLivenessAnalysis.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/Support/CommandLine.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/Support/Debug.h"
24*0a6a1f1dSLionel Sambuc #include "llvm/Target/TargetSubtargetInfo.h"
25*0a6a1f1dSLionel Sambuc 
26*0a6a1f1dSLionel Sambuc using namespace llvm;
27*0a6a1f1dSLionel Sambuc 
28*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "stackmaps"
29*0a6a1f1dSLionel Sambuc 
30*0a6a1f1dSLionel Sambuc namespace llvm {
31*0a6a1f1dSLionel Sambuc cl::opt<bool> EnablePatchPointLiveness("enable-patchpoint-liveness",
32*0a6a1f1dSLionel Sambuc   cl::Hidden, cl::init(true),
33*0a6a1f1dSLionel Sambuc   cl::desc("Enable PatchPoint Liveness Analysis Pass"));
34*0a6a1f1dSLionel Sambuc }
35*0a6a1f1dSLionel Sambuc 
36*0a6a1f1dSLionel Sambuc STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
37*0a6a1f1dSLionel Sambuc STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
38*0a6a1f1dSLionel Sambuc STATISTIC(NumBBsVisited,          "Number of basic blocks visited");
39*0a6a1f1dSLionel Sambuc STATISTIC(NumBBsHaveNoStackmap,   "Number of basic blocks with no stackmap");
40*0a6a1f1dSLionel Sambuc STATISTIC(NumStackMaps,           "Number of StackMaps visited");
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc char StackMapLiveness::ID = 0;
43*0a6a1f1dSLionel Sambuc char &llvm::StackMapLivenessID = StackMapLiveness::ID;
44*0a6a1f1dSLionel Sambuc INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
45*0a6a1f1dSLionel Sambuc                 "StackMap Liveness Analysis", false, false)
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc /// Default construct and initialize the pass.
StackMapLiveness()48*0a6a1f1dSLionel Sambuc StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
49*0a6a1f1dSLionel Sambuc   initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
50*0a6a1f1dSLionel Sambuc }
51*0a6a1f1dSLionel Sambuc 
52*0a6a1f1dSLionel Sambuc /// Tell the pass manager which passes we depend on and what information we
53*0a6a1f1dSLionel Sambuc /// preserve.
getAnalysisUsage(AnalysisUsage & AU) const54*0a6a1f1dSLionel Sambuc void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
55*0a6a1f1dSLionel Sambuc   // We preserve all information.
56*0a6a1f1dSLionel Sambuc   AU.setPreservesAll();
57*0a6a1f1dSLionel Sambuc   AU.setPreservesCFG();
58*0a6a1f1dSLionel Sambuc   // Default dependencie for all MachineFunction passes.
59*0a6a1f1dSLionel Sambuc   AU.addRequired<MachineFunctionAnalysis>();
60*0a6a1f1dSLionel Sambuc }
61*0a6a1f1dSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc /// Calculate the liveness information for the given machine function.
runOnMachineFunction(MachineFunction & _MF)63*0a6a1f1dSLionel Sambuc bool StackMapLiveness::runOnMachineFunction(MachineFunction &_MF) {
64*0a6a1f1dSLionel Sambuc   if (!EnablePatchPointLiveness)
65*0a6a1f1dSLionel Sambuc     return false;
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc   DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
68*0a6a1f1dSLionel Sambuc                << _MF.getName() << " **********\n");
69*0a6a1f1dSLionel Sambuc   MF = &_MF;
70*0a6a1f1dSLionel Sambuc   TRI = MF->getSubtarget().getRegisterInfo();
71*0a6a1f1dSLionel Sambuc   ++NumStackMapFuncVisited;
72*0a6a1f1dSLionel Sambuc 
73*0a6a1f1dSLionel Sambuc   // Skip this function if there are no patchpoints to process.
74*0a6a1f1dSLionel Sambuc   if (!MF->getFrameInfo()->hasPatchPoint()) {
75*0a6a1f1dSLionel Sambuc     ++NumStackMapFuncSkipped;
76*0a6a1f1dSLionel Sambuc     return false;
77*0a6a1f1dSLionel Sambuc   }
78*0a6a1f1dSLionel Sambuc   return calculateLiveness();
79*0a6a1f1dSLionel Sambuc }
80*0a6a1f1dSLionel Sambuc 
81*0a6a1f1dSLionel Sambuc /// Performs the actual liveness calculation for the function.
calculateLiveness()82*0a6a1f1dSLionel Sambuc bool StackMapLiveness::calculateLiveness() {
83*0a6a1f1dSLionel Sambuc   bool HasChanged = false;
84*0a6a1f1dSLionel Sambuc   // For all basic blocks in the function.
85*0a6a1f1dSLionel Sambuc   for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
86*0a6a1f1dSLionel Sambuc        MBBI != MBBE; ++MBBI) {
87*0a6a1f1dSLionel Sambuc     DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n");
88*0a6a1f1dSLionel Sambuc     LiveRegs.init(TRI);
89*0a6a1f1dSLionel Sambuc     LiveRegs.addLiveOuts(MBBI);
90*0a6a1f1dSLionel Sambuc     bool HasStackMap = false;
91*0a6a1f1dSLionel Sambuc     // Reverse iterate over all instructions and add the current live register
92*0a6a1f1dSLionel Sambuc     // set to an instruction if we encounter a patchpoint instruction.
93*0a6a1f1dSLionel Sambuc     for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(),
94*0a6a1f1dSLionel Sambuc          E = MBBI->rend(); I != E; ++I) {
95*0a6a1f1dSLionel Sambuc       if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
96*0a6a1f1dSLionel Sambuc         addLiveOutSetToMI(*I);
97*0a6a1f1dSLionel Sambuc         HasChanged = true;
98*0a6a1f1dSLionel Sambuc         HasStackMap = true;
99*0a6a1f1dSLionel Sambuc         ++NumStackMaps;
100*0a6a1f1dSLionel Sambuc       }
101*0a6a1f1dSLionel Sambuc       DEBUG(dbgs() << "   " << LiveRegs << "   " << *I);
102*0a6a1f1dSLionel Sambuc       LiveRegs.stepBackward(*I);
103*0a6a1f1dSLionel Sambuc     }
104*0a6a1f1dSLionel Sambuc     ++NumBBsVisited;
105*0a6a1f1dSLionel Sambuc     if (!HasStackMap)
106*0a6a1f1dSLionel Sambuc       ++NumBBsHaveNoStackmap;
107*0a6a1f1dSLionel Sambuc   }
108*0a6a1f1dSLionel Sambuc   return HasChanged;
109*0a6a1f1dSLionel Sambuc }
110*0a6a1f1dSLionel Sambuc 
111*0a6a1f1dSLionel Sambuc /// Add the current register live set to the instruction.
addLiveOutSetToMI(MachineInstr & MI)112*0a6a1f1dSLionel Sambuc void StackMapLiveness::addLiveOutSetToMI(MachineInstr &MI) {
113*0a6a1f1dSLionel Sambuc   uint32_t *Mask = createRegisterMask();
114*0a6a1f1dSLionel Sambuc   MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
115*0a6a1f1dSLionel Sambuc   MI.addOperand(*MF, MO);
116*0a6a1f1dSLionel Sambuc }
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc /// Create a register mask and initialize it with the registers from the
119*0a6a1f1dSLionel Sambuc /// register live set.
createRegisterMask() const120*0a6a1f1dSLionel Sambuc uint32_t *StackMapLiveness::createRegisterMask() const {
121*0a6a1f1dSLionel Sambuc   // The mask is owned and cleaned up by the Machine Function.
122*0a6a1f1dSLionel Sambuc   uint32_t *Mask = MF->allocateRegisterMask(TRI->getNumRegs());
123*0a6a1f1dSLionel Sambuc   for (LivePhysRegs::const_iterator RI = LiveRegs.begin(), RE = LiveRegs.end();
124*0a6a1f1dSLionel Sambuc        RI != RE; ++RI)
125*0a6a1f1dSLionel Sambuc     Mask[*RI / 32] |= 1U << (*RI % 32);
126*0a6a1f1dSLionel Sambuc 
127*0a6a1f1dSLionel Sambuc   TRI->adjustStackMapLiveOutMask(Mask);
128*0a6a1f1dSLionel Sambuc   return Mask;
129*0a6a1f1dSLionel Sambuc }
130