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