blob: 051df721af8147c28b5aa5c6ec9ba44bb494ebfc [file] [log] [blame]
Juergen Ributzkae8294752013-12-14 06:53:06 +00001//===-- StackMapLivenessAnalysis.cpp - StackMap live Out Analysis ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
13//
14//===----------------------------------------------------------------------===//
15
Juergen Ributzkae8294752013-12-14 06:53:06 +000016#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"
Benjamin Kramerb85d3752015-03-23 18:45:56 +000024#include "llvm/Support/raw_ostream.h"
Eric Christopherd9134482014-08-04 21:25:23 +000025#include "llvm/Target/TargetSubtargetInfo.h"
Juergen Ributzkae8294752013-12-14 06:53:06 +000026
27using namespace llvm;
28
Chandler Carruth1b9dde02014-04-22 02:02:50 +000029#define DEBUG_TYPE "stackmaps"
30
Juergen Ributzkae8294752013-12-14 06:53:06 +000031namespace llvm {
Juergen Ributzkae8294752013-12-14 06:53:06 +000032cl::opt<bool> EnablePatchPointLiveness("enable-patchpoint-liveness",
Juergen Ributzka009bff22014-06-26 23:39:52 +000033 cl::Hidden, cl::init(true),
34 cl::desc("Enable PatchPoint Liveness Analysis Pass"));
Juergen Ributzkae8294752013-12-14 06:53:06 +000035}
36
37STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
38STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
39STATISTIC(NumBBsVisited, "Number of basic blocks visited");
40STATISTIC(NumBBsHaveNoStackmap, "Number of basic blocks with no stackmap");
41STATISTIC(NumStackMaps, "Number of StackMaps visited");
42
43char StackMapLiveness::ID = 0;
44char &llvm::StackMapLivenessID = StackMapLiveness::ID;
45INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
46 "StackMap Liveness Analysis", false, false)
47
48/// Default construct and initialize the pass.
49StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
50 initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
51}
52
53/// Tell the pass manager which passes we depend on and what information we
54/// preserve.
55void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
56 // We preserve all information.
57 AU.setPreservesAll();
58 AU.setPreservesCFG();
59 // Default dependencie for all MachineFunction passes.
60 AU.addRequired<MachineFunctionAnalysis>();
61}
62
63/// Calculate the liveness information for the given machine function.
David Blaikie9f380a32015-03-16 18:06:57 +000064bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) {
Juergen Ributzka009bff22014-06-26 23:39:52 +000065 if (!EnablePatchPointLiveness)
66 return false;
67
David Blaikie9f380a32015-03-16 18:06:57 +000068 DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: " << MF.getName()
69 << " **********\n");
70 this->MF = &MF;
71 TRI = MF.getSubtarget().getRegisterInfo();
Juergen Ributzkae8294752013-12-14 06:53:06 +000072 ++NumStackMapFuncVisited;
73
Juergen Ributzka14871f72014-06-26 23:39:44 +000074 // Skip this function if there are no patchpoints to process.
David Blaikie9f380a32015-03-16 18:06:57 +000075 if (!MF.getFrameInfo()->hasPatchPoint()) {
Juergen Ributzkae8294752013-12-14 06:53:06 +000076 ++NumStackMapFuncSkipped;
77 return false;
78 }
79 return calculateLiveness();
80}
81
82/// Performs the actual liveness calculation for the function.
83bool StackMapLiveness::calculateLiveness() {
84 bool HasChanged = false;
85 // For all basic blocks in the function.
86 for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
87 MBBI != MBBE; ++MBBI) {
88 DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n");
89 LiveRegs.init(TRI);
90 LiveRegs.addLiveOuts(MBBI);
91 bool HasStackMap = false;
92 // Reverse iterate over all instructions and add the current live register
Juergen Ributzka14871f72014-06-26 23:39:44 +000093 // set to an instruction if we encounter a patchpoint instruction.
Juergen Ributzkae8294752013-12-14 06:53:06 +000094 for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(),
95 E = MBBI->rend(); I != E; ++I) {
Juergen Ributzka009bff22014-06-26 23:39:52 +000096 if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
Juergen Ributzkae8294752013-12-14 06:53:06 +000097 addLiveOutSetToMI(*I);
98 HasChanged = true;
99 HasStackMap = true;
100 ++NumStackMaps;
101 }
Andrew Trick326c1f682014-04-04 23:49:35 +0000102 DEBUG(dbgs() << " " << LiveRegs << " " << *I);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000103 LiveRegs.stepBackward(*I);
104 }
105 ++NumBBsVisited;
106 if (!HasStackMap)
107 ++NumBBsHaveNoStackmap;
108 }
109 return HasChanged;
110}
111
112/// Add the current register live set to the instruction.
113void StackMapLiveness::addLiveOutSetToMI(MachineInstr &MI) {
114 uint32_t *Mask = createRegisterMask();
115 MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
116 MI.addOperand(*MF, MO);
117}
118
119/// Create a register mask and initialize it with the registers from the
120/// register live set.
121uint32_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();
125 RI != RE; ++RI)
126 Mask[*RI / 32] |= 1U << (*RI % 32);
Hal Finkeldf87f932015-01-13 17:47:59 +0000127
128 TRI->adjustStackMapLiveOutMask(Mask);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000129 return Mask;
130}