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