1 //===-- StackMapLivenessAnalysis.cpp - StackMap live Out Analysis ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
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"
28 #define DEBUG_TYPE "stackmaps"
31 cl::opt<bool> EnablePatchPointLiveness("enable-patchpoint-liveness",
32 cl::Hidden, cl::init(true),
33 cl::desc("Enable PatchPoint Liveness Analysis Pass"));
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");
42 char StackMapLiveness::ID = 0;
43 char &llvm::StackMapLivenessID = StackMapLiveness::ID;
44 INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
45 "StackMap Liveness Analysis", false, false)
47 /// Default construct and initialize the pass.
48 StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
49 initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
52 /// Tell the pass manager which passes we depend on and what information we
54 void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
55 // We preserve all information.
58 // Default dependencie for all MachineFunction passes.
59 AU.addRequired<MachineFunctionAnalysis>();
62 /// Calculate the liveness information for the given machine function.
63 bool StackMapLiveness::runOnMachineFunction(MachineFunction &_MF) {
64 if (!EnablePatchPointLiveness)
67 DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
68 << _MF.getName() << " **********\n");
70 TRI = MF->getSubtarget().getRegisterInfo();
71 ++NumStackMapFuncVisited;
73 // Skip this function if there are no patchpoints to process.
74 if (!MF->getFrameInfo()->hasPatchPoint()) {
75 ++NumStackMapFuncSkipped;
78 return calculateLiveness();
81 /// Performs the actual liveness calculation for the function.
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");
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);
101 DEBUG(dbgs() << " " << LiveRegs << " " << *I);
102 LiveRegs.stepBackward(*I);
106 ++NumBBsHaveNoStackmap;
111 /// Add the current register live set to the instruction.
112 void StackMapLiveness::addLiveOutSetToMI(MachineInstr &MI) {
113 uint32_t *Mask = createRegisterMask();
114 MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
115 MI.addOperand(*MF, MO);
118 /// Create a register mask and initialize it with the registers from the
119 /// register live set.
120 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();
125 Mask[*RI / 32] |= 1U << (*RI % 32);
127 TRI->adjustStackMapLiveOutMask(Mask);