blob: fb2abf3daa7fda5c31881cc0f136b7ac7ba5ea5e [file] [log] [blame]
Juergen Ributzkae8294752013-12-14 06:53:06 +00001//===-- StackMapLivenessAnalysis.cpp - StackMap live Out Analysis ----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Juergen Ributzkae8294752013-12-14 06:53:06 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the StackMap Liveness analysis pass. The pass calculates
10// the liveness for each basic block in a function and attaches the register
11// live-out information to a stackmap or patchpoint intrinsic if present.
12//
13//===----------------------------------------------------------------------===//
14
Juergen Ributzkae8294752013-12-14 06:53:06 +000015#include "llvm/ADT/Statistic.h"
Benjamin Kramer722ff282015-03-24 13:20:54 +000016#include "llvm/CodeGen/LivePhysRegs.h"
Juergen Ributzkae8294752013-12-14 06:53:06 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
Benjamin Kramer722ff282015-03-24 13:20:54 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
Juergen Ributzkae8294752013-12-14 06:53:06 +000020#include "llvm/CodeGen/Passes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000021#include "llvm/CodeGen/TargetSubtargetInfo.h"
Juergen Ributzkae8294752013-12-14 06:53:06 +000022#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"
Juergen Ributzkae8294752013-12-14 06:53:06 +000025
26using namespace llvm;
27
Chandler Carruth1b9dde02014-04-22 02:02:50 +000028#define DEBUG_TYPE "stackmaps"
29
Benjamin Kramer722ff282015-03-24 13:20:54 +000030static cl::opt<bool> EnablePatchPointLiveness(
31 "enable-patchpoint-liveness", cl::Hidden, cl::init(true),
32 cl::desc("Enable PatchPoint Liveness Analysis Pass"));
Juergen Ributzkae8294752013-12-14 06:53:06 +000033
34STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
35STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
36STATISTIC(NumBBsVisited, "Number of basic blocks visited");
37STATISTIC(NumBBsHaveNoStackmap, "Number of basic blocks with no stackmap");
38STATISTIC(NumStackMaps, "Number of StackMaps visited");
39
Benjamin Kramer722ff282015-03-24 13:20:54 +000040namespace {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000041/// This pass calculates the liveness information for each basic block in
Benjamin Kramer722ff282015-03-24 13:20:54 +000042/// a function and attaches the register live-out information to a patchpoint
43/// intrinsic if present.
44///
45/// This pass can be disabled via the -enable-patchpoint-liveness=false flag.
46/// The pass skips functions that don't have any patchpoint intrinsics. The
47/// information provided by this pass is optional and not required by the
48/// aformentioned intrinsic to function.
49class StackMapLiveness : public MachineFunctionPass {
Benjamin Kramer722ff282015-03-24 13:20:54 +000050 const TargetRegisterInfo *TRI;
51 LivePhysRegs LiveRegs;
52
53public:
54 static char ID;
55
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000056 /// Default construct and initialize the pass.
Benjamin Kramer722ff282015-03-24 13:20:54 +000057 StackMapLiveness();
58
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000059 /// Tell the pass manager which passes we depend on and what
Benjamin Kramer722ff282015-03-24 13:20:54 +000060 /// information we preserve.
61 void getAnalysisUsage(AnalysisUsage &AU) const override;
62
Derek Schuffad154c82016-03-28 17:05:30 +000063 MachineFunctionProperties getRequiredProperties() const override {
64 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000065 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +000066 }
67
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000068 /// Calculate the liveness information for the given machine function.
Benjamin Kramer722ff282015-03-24 13:20:54 +000069 bool runOnMachineFunction(MachineFunction &MF) override;
70
71private:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000072 /// Performs the actual liveness calculation for the function.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +000073 bool calculateLiveness(MachineFunction &MF);
Benjamin Kramer722ff282015-03-24 13:20:54 +000074
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000075 /// Add the current register live set to the instruction.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +000076 void addLiveOutSetToMI(MachineFunction &MF, MachineInstr &MI);
Benjamin Kramer722ff282015-03-24 13:20:54 +000077
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000078 /// Create a register mask and initialize it with the registers from
Benjamin Kramer722ff282015-03-24 13:20:54 +000079 /// the register live set.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +000080 uint32_t *createRegisterMask(MachineFunction &MF) const;
Benjamin Kramer722ff282015-03-24 13:20:54 +000081};
82} // namespace
83
Juergen Ributzkae8294752013-12-14 06:53:06 +000084char StackMapLiveness::ID = 0;
85char &llvm::StackMapLivenessID = StackMapLiveness::ID;
86INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
87 "StackMap Liveness Analysis", false, false)
88
89/// Default construct and initialize the pass.
90StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
91 initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
92}
93
94/// Tell the pass manager which passes we depend on and what information we
95/// preserve.
96void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
97 // We preserve all information.
98 AU.setPreservesAll();
99 AU.setPreservesCFG();
Juergen Ributzka9622cdf2015-07-07 02:05:18 +0000100 MachineFunctionPass::getAnalysisUsage(AU);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000101}
102
103/// Calculate the liveness information for the given machine function.
David Blaikie9f380a32015-03-16 18:06:57 +0000104bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) {
Juergen Ributzka009bff22014-06-26 23:39:52 +0000105 if (!EnablePatchPointLiveness)
106 return false;
107
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000108 LLVM_DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
109 << MF.getName() << " **********\n");
David Blaikie9f380a32015-03-16 18:06:57 +0000110 TRI = MF.getSubtarget().getRegisterInfo();
Juergen Ributzkae8294752013-12-14 06:53:06 +0000111 ++NumStackMapFuncVisited;
112
Juergen Ributzka14871f72014-06-26 23:39:44 +0000113 // Skip this function if there are no patchpoints to process.
Matthias Braun941a7052016-07-28 18:40:00 +0000114 if (!MF.getFrameInfo().hasPatchPoint()) {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000115 ++NumStackMapFuncSkipped;
116 return false;
117 }
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000118 return calculateLiveness(MF);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000119}
120
121/// Performs the actual liveness calculation for the function.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000122bool StackMapLiveness::calculateLiveness(MachineFunction &MF) {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000123 bool HasChanged = false;
124 // For all basic blocks in the function.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000125 for (auto &MBB : MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000126 LLVM_DEBUG(dbgs() << "****** BB " << MBB.getName() << " ******\n");
Matthias Braun0c989a82016-12-08 00:15:51 +0000127 LiveRegs.init(*TRI);
Matthias Braun24f26e62016-05-03 00:08:46 +0000128 // FIXME: This should probably be addLiveOuts().
Matthias Braund1aabb22016-05-03 00:24:32 +0000129 LiveRegs.addLiveOutsNoPristines(MBB);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000130 bool HasStackMap = false;
131 // Reverse iterate over all instructions and add the current live register
Juergen Ributzka14871f72014-06-26 23:39:44 +0000132 // set to an instruction if we encounter a patchpoint instruction.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000133 for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) {
Juergen Ributzka009bff22014-06-26 23:39:52 +0000134 if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000135 addLiveOutSetToMI(MF, *I);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000136 HasChanged = true;
137 HasStackMap = true;
138 ++NumStackMaps;
139 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000140 LLVM_DEBUG(dbgs() << " " << LiveRegs << " " << *I);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000141 LiveRegs.stepBackward(*I);
142 }
143 ++NumBBsVisited;
144 if (!HasStackMap)
145 ++NumBBsHaveNoStackmap;
146 }
147 return HasChanged;
148}
149
150/// Add the current register live set to the instruction.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000151void StackMapLiveness::addLiveOutSetToMI(MachineFunction &MF,
152 MachineInstr &MI) {
153 uint32_t *Mask = createRegisterMask(MF);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000154 MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000155 MI.addOperand(MF, MO);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000156}
157
158/// Create a register mask and initialize it with the registers from the
159/// register live set.
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000160uint32_t *StackMapLiveness::createRegisterMask(MachineFunction &MF) const {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000161 // The mask is owned and cleaned up by the Machine Function.
Matthias Braun57dd5b32018-07-26 00:27:47 +0000162 uint32_t *Mask = MF.allocateRegMask();
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000163 for (auto Reg : LiveRegs)
164 Mask[Reg / 32] |= 1U << (Reg % 32);
Hal Finkeldf87f932015-01-13 17:47:59 +0000165
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000166 // Give the target a chance to adjust the mask.
Hal Finkeldf87f932015-01-13 17:47:59 +0000167 TRI->adjustStackMapLiveOutMask(Mask);
Juergen Ributzkac111fcc2015-07-07 02:05:15 +0000168
Juergen Ributzkae8294752013-12-14 06:53:06 +0000169 return Mask;
170}