blob: 736dc69661ca46348ad4c14ccaff9cea24c89619 [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"
Benjamin Kramer722ff282015-03-24 13:20:54 +000017#include "llvm/CodeGen/LivePhysRegs.h"
Juergen Ributzkae8294752013-12-14 06:53:06 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineFunctionAnalysis.h"
Benjamin Kramer722ff282015-03-24 13:20:54 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
Juergen Ributzkae8294752013-12-14 06:53:06 +000022#include "llvm/CodeGen/Passes.h"
Juergen Ributzkae8294752013-12-14 06:53:06 +000023#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/Debug.h"
Benjamin Kramerb85d3752015-03-23 18:45:56 +000025#include "llvm/Support/raw_ostream.h"
Eric Christopherd9134482014-08-04 21:25:23 +000026#include "llvm/Target/TargetSubtargetInfo.h"
Juergen Ributzkae8294752013-12-14 06:53:06 +000027
28using namespace llvm;
29
Chandler Carruth1b9dde02014-04-22 02:02:50 +000030#define DEBUG_TYPE "stackmaps"
31
Benjamin Kramer722ff282015-03-24 13:20:54 +000032static cl::opt<bool> EnablePatchPointLiveness(
33 "enable-patchpoint-liveness", cl::Hidden, cl::init(true),
34 cl::desc("Enable PatchPoint Liveness Analysis Pass"));
Juergen Ributzkae8294752013-12-14 06:53:06 +000035
36STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
37STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
38STATISTIC(NumBBsVisited, "Number of basic blocks visited");
39STATISTIC(NumBBsHaveNoStackmap, "Number of basic blocks with no stackmap");
40STATISTIC(NumStackMaps, "Number of StackMaps visited");
41
Benjamin Kramer722ff282015-03-24 13:20:54 +000042namespace {
43/// \brief This pass calculates the liveness information for each basic block in
44/// a function and attaches the register live-out information to a patchpoint
45/// intrinsic if present.
46///
47/// This pass can be disabled via the -enable-patchpoint-liveness=false flag.
48/// The pass skips functions that don't have any patchpoint intrinsics. The
49/// information provided by this pass is optional and not required by the
50/// aformentioned intrinsic to function.
51class StackMapLiveness : public MachineFunctionPass {
Benjamin Kramer722ff282015-03-24 13:20:54 +000052 const TargetRegisterInfo *TRI;
53 LivePhysRegs LiveRegs;
54
55public:
56 static char ID;
57
58 /// \brief Default construct and initialize the pass.
59 StackMapLiveness();
60
61 /// \brief Tell the pass manager which passes we depend on and what
62 /// information we preserve.
63 void getAnalysisUsage(AnalysisUsage &AU) const override;
64
65 /// \brief Calculate the liveness information for the given machine function.
66 bool runOnMachineFunction(MachineFunction &MF) override;
67
68private:
69 /// \brief Performs the actual liveness calculation for the function.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +000070 bool calculateLiveness(MachineFunction &MF);
Benjamin Kramer722ff282015-03-24 13:20:54 +000071
72 /// \brief Add the current register live set to the instruction.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +000073 void addLiveOutSetToMI(MachineFunction &MF, MachineInstr &MI);
Benjamin Kramer722ff282015-03-24 13:20:54 +000074
75 /// \brief Create a register mask and initialize it with the registers from
76 /// the register live set.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +000077 uint32_t *createRegisterMask(MachineFunction &MF) const;
Benjamin Kramer722ff282015-03-24 13:20:54 +000078};
79} // namespace
80
Juergen Ributzkae8294752013-12-14 06:53:06 +000081char StackMapLiveness::ID = 0;
82char &llvm::StackMapLivenessID = StackMapLiveness::ID;
83INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
84 "StackMap Liveness Analysis", false, false)
85
86/// Default construct and initialize the pass.
87StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
88 initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
89}
90
91/// Tell the pass manager which passes we depend on and what information we
92/// preserve.
93void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
94 // We preserve all information.
95 AU.setPreservesAll();
96 AU.setPreservesCFG();
97 // Default dependencie for all MachineFunction passes.
98 AU.addRequired<MachineFunctionAnalysis>();
99}
100
101/// Calculate the liveness information for the given machine function.
David Blaikie9f380a32015-03-16 18:06:57 +0000102bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) {
Juergen Ributzka009bff22014-06-26 23:39:52 +0000103 if (!EnablePatchPointLiveness)
104 return false;
105
David Blaikie9f380a32015-03-16 18:06:57 +0000106 DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: " << MF.getName()
107 << " **********\n");
David Blaikie9f380a32015-03-16 18:06:57 +0000108 TRI = MF.getSubtarget().getRegisterInfo();
Juergen Ributzkae8294752013-12-14 06:53:06 +0000109 ++NumStackMapFuncVisited;
110
Juergen Ributzka14871f72014-06-26 23:39:44 +0000111 // Skip this function if there are no patchpoints to process.
David Blaikie9f380a32015-03-16 18:06:57 +0000112 if (!MF.getFrameInfo()->hasPatchPoint()) {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000113 ++NumStackMapFuncSkipped;
114 return false;
115 }
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000116 return calculateLiveness(MF);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000117}
118
119/// Performs the actual liveness calculation for the function.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000120bool StackMapLiveness::calculateLiveness(MachineFunction &MF) {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000121 bool HasChanged = false;
122 // For all basic blocks in the function.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000123 for (auto &MBB : MF) {
124 DEBUG(dbgs() << "****** BB " << MBB.getName() << " ******\n");
Juergen Ributzkae8294752013-12-14 06:53:06 +0000125 LiveRegs.init(TRI);
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000126 LiveRegs.addLiveOuts(&MBB);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000127 bool HasStackMap = false;
128 // Reverse iterate over all instructions and add the current live register
Juergen Ributzka14871f72014-06-26 23:39:44 +0000129 // set to an instruction if we encounter a patchpoint instruction.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000130 for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) {
Juergen Ributzka009bff22014-06-26 23:39:52 +0000131 if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000132 addLiveOutSetToMI(MF, *I);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000133 HasChanged = true;
134 HasStackMap = true;
135 ++NumStackMaps;
136 }
Andrew Trick326c1f682014-04-04 23:49:35 +0000137 DEBUG(dbgs() << " " << LiveRegs << " " << *I);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000138 LiveRegs.stepBackward(*I);
139 }
140 ++NumBBsVisited;
141 if (!HasStackMap)
142 ++NumBBsHaveNoStackmap;
143 }
144 return HasChanged;
145}
146
147/// Add the current register live set to the instruction.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000148void StackMapLiveness::addLiveOutSetToMI(MachineFunction &MF,
149 MachineInstr &MI) {
150 uint32_t *Mask = createRegisterMask(MF);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000151 MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000152 MI.addOperand(MF, MO);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000153}
154
155/// Create a register mask and initialize it with the registers from the
156/// register live set.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000157uint32_t *StackMapLiveness::createRegisterMask(MachineFunction &MF) const {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000158 // The mask is owned and cleaned up by the Machine Function.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000159 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
160 for (auto Reg : LiveRegs)
161 Mask[Reg / 32] |= 1U << (Reg % 32);
Hal Finkeldf87f932015-01-13 17:47:59 +0000162
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000163 // Give the target a chance to adjust the mask.
Hal Finkeldf87f932015-01-13 17:47:59 +0000164 TRI->adjustStackMapLiveOutMask(Mask);
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000165
Juergen Ributzkae8294752013-12-14 06:53:06 +0000166 return Mask;
167}