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 #define DEBUG_TYPE "stackmaps"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/CodeGen/StackMapLivenessAnalysis.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
30 cl::opt<bool> EnableStackMapLiveness("enable-stackmap-liveness",
31 cl::Hidden, cl::desc("Enable StackMap Liveness Analysis Pass"));
32 cl::opt<bool> EnablePatchPointLiveness("enable-patchpoint-liveness",
33 cl::Hidden, 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 DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
65 << _MF.getName() << " **********\n");
67 TRI = MF->getTarget().getRegisterInfo();
68 ++NumStackMapFuncVisited;
70 // Skip this function if there are no stackmaps or patchpoints to process.
71 if (!((MF->getFrameInfo()->hasStackMap() && EnableStackMapLiveness) ||
72 (MF->getFrameInfo()->hasPatchPoint() && EnablePatchPointLiveness))) {
73 ++NumStackMapFuncSkipped;
76 return calculateLiveness();
79 /// Performs the actual liveness calculation for the function.
80 bool StackMapLiveness::calculateLiveness() {
81 bool HasChanged = false;
82 // For all basic blocks in the function.
83 for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
84 MBBI != MBBE; ++MBBI) {
85 DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n");
87 LiveRegs.addLiveOuts(MBBI);
88 bool HasStackMap = false;
89 // Reverse iterate over all instructions and add the current live register
90 // set to an instruction if we encounter a stackmap or patchpoint
92 for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(),
93 E = MBBI->rend(); I != E; ++I) {
94 int Opc = I->getOpcode();
95 if ((EnableStackMapLiveness && (Opc == TargetOpcode::STACKMAP)) ||
96 (EnablePatchPointLiveness && (Opc == TargetOpcode::PATCHPOINT))) {
97 addLiveOutSetToMI(*I);
102 DEBUG(dbgs() << " " << *I << " " << LiveRegs);
103 LiveRegs.stepBackward(*I);
107 ++NumBBsHaveNoStackmap;
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);
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();
126 Mask[*RI / 32] |= 1U << (*RI % 32);