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> EnablePatchPointLiveness("enable-patchpoint-liveness",
32 cl::Hidden, cl::desc("Enable PatchPoint Liveness Analysis Pass"));
35 STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
36 STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
37 STATISTIC(NumBBsVisited, "Number of basic blocks visited");
38 STATISTIC(NumBBsHaveNoStackmap, "Number of basic blocks with no stackmap");
39 STATISTIC(NumStackMaps, "Number of StackMaps visited");
41 char StackMapLiveness::ID = 0;
42 char &llvm::StackMapLivenessID = StackMapLiveness::ID;
43 INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
44 "StackMap Liveness Analysis", false, false)
46 /// Default construct and initialize the pass.
47 StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
48 initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
51 /// Tell the pass manager which passes we depend on and what information we
53 void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
54 // We preserve all information.
57 // Default dependencie for all MachineFunction passes.
58 AU.addRequired<MachineFunctionAnalysis>();
61 /// Calculate the liveness information for the given machine function.
62 bool StackMapLiveness::runOnMachineFunction(MachineFunction &_MF) {
63 DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
64 << _MF.getName() << " **********\n");
66 TRI = MF->getTarget().getRegisterInfo();
67 ++NumStackMapFuncVisited;
69 // Skip this function if there are no patchpoints to process.
70 if (!(MF->getFrameInfo()->hasPatchPoint() && EnablePatchPointLiveness)) {
71 ++NumStackMapFuncSkipped;
74 return calculateLiveness();
77 /// Performs the actual liveness calculation for the function.
78 bool StackMapLiveness::calculateLiveness() {
79 bool HasChanged = false;
80 // For all basic blocks in the function.
81 for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
82 MBBI != MBBE; ++MBBI) {
83 DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n");
85 LiveRegs.addLiveOuts(MBBI);
86 bool HasStackMap = false;
87 // Reverse iterate over all instructions and add the current live register
88 // set to an instruction if we encounter a patchpoint instruction.
89 for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(),
90 E = MBBI->rend(); I != E; ++I) {
91 int Opc = I->getOpcode();
92 if (Opc == TargetOpcode::PATCHPOINT) {
93 addLiveOutSetToMI(*I);
98 DEBUG(dbgs() << " " << LiveRegs << " " << *I);
99 LiveRegs.stepBackward(*I);
103 ++NumBBsHaveNoStackmap;
108 /// Add the current register live set to the instruction.
109 void StackMapLiveness::addLiveOutSetToMI(MachineInstr &MI) {
110 uint32_t *Mask = createRegisterMask();
111 MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
112 MI.addOperand(*MF, MO);
115 /// Create a register mask and initialize it with the registers from the
116 /// register live set.
117 uint32_t *StackMapLiveness::createRegisterMask() const {
118 // The mask is owned and cleaned up by the Machine Function.
119 uint32_t *Mask = MF->allocateRegisterMask(TRI->getNumRegs());
120 for (LivePhysRegs::const_iterator RI = LiveRegs.begin(), RE = LiveRegs.end();
122 Mask[*RI / 32] |= 1U << (*RI % 32);