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"
28 #define DEBUG_TYPE "stackmaps"
31 cl::opt<bool> EnableStackMapLiveness("enable-stackmap-liveness",
32 cl::Hidden, cl::desc("Enable StackMap Liveness Analysis Pass"));
33 cl::opt<bool> EnablePatchPointLiveness("enable-patchpoint-liveness",
34 cl::Hidden, cl::desc("Enable PatchPoint Liveness Analysis Pass"));
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");
43 char StackMapLiveness::ID = 0;
44 char &llvm::StackMapLivenessID = StackMapLiveness::ID;
45 INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
46 "StackMap Liveness Analysis", false, false)
48 /// Default construct and initialize the pass.
49 StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
50 initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
53 /// Tell the pass manager which passes we depend on and what information we
55 void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
56 // We preserve all information.
59 // Default dependencie for all MachineFunction passes.
60 AU.addRequired<MachineFunctionAnalysis>();
63 /// Calculate the liveness information for the given machine function.
64 bool StackMapLiveness::runOnMachineFunction(MachineFunction &_MF) {
65 DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
66 << _MF.getName() << " **********\n");
68 TRI = MF->getTarget().getRegisterInfo();
69 ++NumStackMapFuncVisited;
71 // Skip this function if there are no stackmaps or patchpoints to process.
72 if (!((MF->getFrameInfo()->hasStackMap() && EnableStackMapLiveness) ||
73 (MF->getFrameInfo()->hasPatchPoint() && EnablePatchPointLiveness))) {
74 ++NumStackMapFuncSkipped;
77 return calculateLiveness();
80 /// Performs the actual liveness calculation for the function.
81 bool StackMapLiveness::calculateLiveness() {
82 bool HasChanged = false;
83 // For all basic blocks in the function.
84 for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
85 MBBI != MBBE; ++MBBI) {
86 DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n");
88 LiveRegs.addLiveOuts(MBBI);
89 bool HasStackMap = false;
90 // Reverse iterate over all instructions and add the current live register
91 // set to an instruction if we encounter a stackmap or patchpoint
93 for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(),
94 E = MBBI->rend(); I != E; ++I) {
95 int Opc = I->getOpcode();
96 if ((EnableStackMapLiveness && (Opc == TargetOpcode::STACKMAP)) ||
97 (EnablePatchPointLiveness && (Opc == TargetOpcode::PATCHPOINT))) {
98 addLiveOutSetToMI(*I);
103 DEBUG(dbgs() << " " << LiveRegs << " " << *I);
104 LiveRegs.stepBackward(*I);
108 ++NumBBsHaveNoStackmap;
113 /// Add the current register live set to the instruction.
114 void StackMapLiveness::addLiveOutSetToMI(MachineInstr &MI) {
115 uint32_t *Mask = createRegisterMask();
116 MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
117 MI.addOperand(*MF, MO);
120 /// Create a register mask and initialize it with the registers from the
121 /// register live set.
122 uint32_t *StackMapLiveness::createRegisterMask() const {
123 // The mask is owned and cleaned up by the Machine Function.
124 uint32_t *Mask = MF->allocateRegisterMask(TRI->getNumRegs());
125 for (LivePhysRegs::const_iterator RI = LiveRegs.begin(), RE = LiveRegs.end();
127 Mask[*RI / 32] |= 1U << (*RI % 32);