blob: 00cf8070be5ec90de4040671951fca3b47d0997e [file] [log] [blame]
Juergen Ributzkaaaecc0f2013-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 Ributzkaaaecc0f2013-12-14 06:53:06 +000016#include "llvm/ADT/Statistic.h"
Benjamin Kramer1989d622015-03-24 13:20:54 +000017#include "llvm/CodeGen/LivePhysRegs.h"
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
Benjamin Kramer1989d622015-03-24 13:20:54 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +000021#include "llvm/CodeGen/Passes.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000022#include "llvm/CodeGen/TargetSubtargetInfo.h"
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +000023#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/Debug.h"
Benjamin Kramereef4a332015-03-23 18:45:56 +000025#include "llvm/Support/raw_ostream.h"
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +000026
27using namespace llvm;
28
Chandler Carruth8677f2f2014-04-22 02:02:50 +000029#define DEBUG_TYPE "stackmaps"
30
Benjamin Kramer1989d622015-03-24 13:20:54 +000031static cl::opt<bool> EnablePatchPointLiveness(
32 "enable-patchpoint-liveness", cl::Hidden, cl::init(true),
33 cl::desc("Enable PatchPoint Liveness Analysis Pass"));
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +000034
35STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
36STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
37STATISTIC(NumBBsVisited, "Number of basic blocks visited");
38STATISTIC(NumBBsHaveNoStackmap, "Number of basic blocks with no stackmap");
39STATISTIC(NumStackMaps, "Number of StackMaps visited");
40
Benjamin Kramer1989d622015-03-24 13:20:54 +000041namespace {
Adrian Prantl26b584c2018-05-01 15:54:18 +000042/// This pass calculates the liveness information for each basic block in
Benjamin Kramer1989d622015-03-24 13:20:54 +000043/// a function and attaches the register live-out information to a patchpoint
44/// intrinsic if present.
45///
46/// This pass can be disabled via the -enable-patchpoint-liveness=false flag.
47/// The pass skips functions that don't have any patchpoint intrinsics. The
48/// information provided by this pass is optional and not required by the
49/// aformentioned intrinsic to function.
50class StackMapLiveness : public MachineFunctionPass {
Benjamin Kramer1989d622015-03-24 13:20:54 +000051 const TargetRegisterInfo *TRI;
52 LivePhysRegs LiveRegs;
53
54public:
55 static char ID;
56
Adrian Prantl26b584c2018-05-01 15:54:18 +000057 /// Default construct and initialize the pass.
Benjamin Kramer1989d622015-03-24 13:20:54 +000058 StackMapLiveness();
59
Adrian Prantl26b584c2018-05-01 15:54:18 +000060 /// Tell the pass manager which passes we depend on and what
Benjamin Kramer1989d622015-03-24 13:20:54 +000061 /// information we preserve.
62 void getAnalysisUsage(AnalysisUsage &AU) const override;
63
Derek Schufffadd1132016-03-28 17:05:30 +000064 MachineFunctionProperties getRequiredProperties() const override {
65 return MachineFunctionProperties().set(
Matthias Braun690a3cb2016-08-25 01:27:13 +000066 MachineFunctionProperties::Property::NoVRegs);
Derek Schufffadd1132016-03-28 17:05:30 +000067 }
68
Adrian Prantl26b584c2018-05-01 15:54:18 +000069 /// Calculate the liveness information for the given machine function.
Benjamin Kramer1989d622015-03-24 13:20:54 +000070 bool runOnMachineFunction(MachineFunction &MF) override;
71
72private:
Adrian Prantl26b584c2018-05-01 15:54:18 +000073 /// Performs the actual liveness calculation for the function.
Juergen Ributzka083b7912015-07-07 02:05:15 +000074 bool calculateLiveness(MachineFunction &MF);
Benjamin Kramer1989d622015-03-24 13:20:54 +000075
Adrian Prantl26b584c2018-05-01 15:54:18 +000076 /// Add the current register live set to the instruction.
Juergen Ributzka083b7912015-07-07 02:05:15 +000077 void addLiveOutSetToMI(MachineFunction &MF, MachineInstr &MI);
Benjamin Kramer1989d622015-03-24 13:20:54 +000078
Adrian Prantl26b584c2018-05-01 15:54:18 +000079 /// Create a register mask and initialize it with the registers from
Benjamin Kramer1989d622015-03-24 13:20:54 +000080 /// the register live set.
Juergen Ributzka083b7912015-07-07 02:05:15 +000081 uint32_t *createRegisterMask(MachineFunction &MF) const;
Benjamin Kramer1989d622015-03-24 13:20:54 +000082};
83} // namespace
84
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +000085char StackMapLiveness::ID = 0;
86char &llvm::StackMapLivenessID = StackMapLiveness::ID;
87INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
88 "StackMap Liveness Analysis", false, false)
89
90/// Default construct and initialize the pass.
91StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
92 initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
93}
94
95/// Tell the pass manager which passes we depend on and what information we
96/// preserve.
97void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
98 // We preserve all information.
99 AU.setPreservesAll();
100 AU.setPreservesCFG();
Juergen Ributzkab062a9b2015-07-07 02:05:18 +0000101 MachineFunctionPass::getAnalysisUsage(AU);
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000102}
103
104/// Calculate the liveness information for the given machine function.
David Blaikie7610ba72015-03-16 18:06:57 +0000105bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) {
Juergen Ributzkab1b6d102014-06-26 23:39:52 +0000106 if (!EnablePatchPointLiveness)
107 return false;
108
Nicola Zaghen0818e782018-05-14 12:53:11 +0000109 LLVM_DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
110 << MF.getName() << " **********\n");
David Blaikie7610ba72015-03-16 18:06:57 +0000111 TRI = MF.getSubtarget().getRegisterInfo();
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000112 ++NumStackMapFuncVisited;
113
Juergen Ributzka307a6442014-06-26 23:39:44 +0000114 // Skip this function if there are no patchpoints to process.
Matthias Braunf79c57a2016-07-28 18:40:00 +0000115 if (!MF.getFrameInfo().hasPatchPoint()) {
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000116 ++NumStackMapFuncSkipped;
117 return false;
118 }
Juergen Ributzka083b7912015-07-07 02:05:15 +0000119 return calculateLiveness(MF);
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000120}
121
122/// Performs the actual liveness calculation for the function.
Juergen Ributzka083b7912015-07-07 02:05:15 +0000123bool StackMapLiveness::calculateLiveness(MachineFunction &MF) {
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000124 bool HasChanged = false;
125 // For all basic blocks in the function.
Juergen Ributzka083b7912015-07-07 02:05:15 +0000126 for (auto &MBB : MF) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000127 LLVM_DEBUG(dbgs() << "****** BB " << MBB.getName() << " ******\n");
Matthias Braunf3e629e2016-12-08 00:15:51 +0000128 LiveRegs.init(*TRI);
Matthias Braunb4756d62016-05-03 00:08:46 +0000129 // FIXME: This should probably be addLiveOuts().
Matthias Braun02073cb2016-05-03 00:24:32 +0000130 LiveRegs.addLiveOutsNoPristines(MBB);
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000131 bool HasStackMap = false;
132 // Reverse iterate over all instructions and add the current live register
Juergen Ributzka307a6442014-06-26 23:39:44 +0000133 // set to an instruction if we encounter a patchpoint instruction.
Juergen Ributzka083b7912015-07-07 02:05:15 +0000134 for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) {
Juergen Ributzkab1b6d102014-06-26 23:39:52 +0000135 if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
Juergen Ributzka083b7912015-07-07 02:05:15 +0000136 addLiveOutSetToMI(MF, *I);
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000137 HasChanged = true;
138 HasStackMap = true;
139 ++NumStackMaps;
140 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000141 LLVM_DEBUG(dbgs() << " " << LiveRegs << " " << *I);
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000142 LiveRegs.stepBackward(*I);
143 }
144 ++NumBBsVisited;
145 if (!HasStackMap)
146 ++NumBBsHaveNoStackmap;
147 }
148 return HasChanged;
149}
150
151/// Add the current register live set to the instruction.
Juergen Ributzka083b7912015-07-07 02:05:15 +0000152void StackMapLiveness::addLiveOutSetToMI(MachineFunction &MF,
153 MachineInstr &MI) {
154 uint32_t *Mask = createRegisterMask(MF);
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000155 MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
Juergen Ributzka083b7912015-07-07 02:05:15 +0000156 MI.addOperand(MF, MO);
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000157}
158
159/// Create a register mask and initialize it with the registers from the
160/// register live set.
Juergen Ributzka083b7912015-07-07 02:05:15 +0000161uint32_t *StackMapLiveness::createRegisterMask(MachineFunction &MF) const {
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000162 // The mask is owned and cleaned up by the Machine Function.
Matthias Braunee571f22018-07-26 00:27:47 +0000163 uint32_t *Mask = MF.allocateRegMask();
Juergen Ributzka083b7912015-07-07 02:05:15 +0000164 for (auto Reg : LiveRegs)
165 Mask[Reg / 32] |= 1U << (Reg % 32);
Hal Finkel5e508852015-01-13 17:47:59 +0000166
Juergen Ributzka083b7912015-07-07 02:05:15 +0000167 // Give the target a chance to adjust the mask.
Hal Finkel5e508852015-01-13 17:47:59 +0000168 TRI->adjustStackMapLiveOutMask(Mask);
Juergen Ributzka083b7912015-07-07 02:05:15 +0000169
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000170 return Mask;
171}