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/Debug.h"
28 STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
29 STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
30 STATISTIC(NumBBsVisited, "Number of basic blocks visited");
31 STATISTIC(NumBBsHaveNoStackmap, "Number of basic blocks with no stackmap");
32 STATISTIC(NumStackMaps, "Number of StackMaps visited");
34 char StackMapLiveness::ID = 0;
35 char &llvm::StackMapLivenessID = StackMapLiveness::ID;
36 INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
37 "StackMap Liveness Analysis", false, false)
39 /// Default construct and initialize the pass.
40 StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
41 initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
44 /// Tell the pass manager which passes we depend on and what information we
46 void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
47 // We preserve all information.
50 // Default dependencie for all MachineFunction passes.
51 AU.addRequired<MachineFunctionAnalysis>();
54 /// Calculate the liveness information for the given machine function.
55 bool StackMapLiveness::runOnMachineFunction(MachineFunction &_MF) {
56 DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
57 << _MF.getName() << " **********\n");
59 TRI = MF->getTarget().getRegisterInfo();
60 ++NumStackMapFuncVisited;
62 // Skip this function if there are no stackmaps.
63 if (!MF->getFrameInfo()->hasStackMap()) {
64 ++NumStackMapFuncSkipped;
67 return calculateLiveness();
70 /// Performs the actual liveness calculation for the function.
71 bool StackMapLiveness::calculateLiveness() {
72 bool HasChanged = false;
73 // For all basic blocks in the function.
74 for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
75 MBBI != MBBE; ++MBBI) {
76 DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n");
78 LiveRegs.addLiveOuts(MBBI);
79 bool HasStackMap = false;
80 // Reverse iterate over all instructions and add the current live register
81 // set to an instruction if we encounter a stackmap or patchpoint
83 for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(),
84 E = MBBI->rend(); I != E; ++I) {
85 if (I->getOpcode() == TargetOpcode::STACKMAP ||
86 I->getOpcode() == TargetOpcode::PATCHPOINT) {
87 addLiveOutSetToMI(*I);
92 LiveRegs.stepBackward(*I);
96 ++NumBBsHaveNoStackmap;
101 /// Add the current register live set to the instruction.
102 void StackMapLiveness::addLiveOutSetToMI(MachineInstr &MI) {
103 uint32_t *Mask = createRegisterMask();
104 MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
105 MI.addOperand(*MF, MO);
106 DEBUG(dbgs() << " " << MI);
107 DEBUG(printLiveOutSet(dbgs()));
110 /// Create a register mask and initialize it with the registers from the
111 /// register live set.
112 uint32_t *StackMapLiveness::createRegisterMask() const {
113 // The mask is owned and cleaned up by the Machine Function.
114 uint32_t *Mask = MF->allocateRegisterMask(TRI->getNumRegs());
115 for (LivePhysRegs::const_iterator RI = LiveRegs.begin(), RE = LiveRegs.end();
117 Mask[*RI / 32] |= 1U << (*RI % 32);
121 /// Print the current register live set for debugging.
122 void StackMapLiveness::printLiveOutSet(raw_ostream &OS) const {
123 OS << " Register live-out:";
124 for (LivePhysRegs::const_iterator RI = LiveRegs.begin(), RE = LiveRegs.end();
126 OS << " " << TRI->getName(*RI);