blob: 3ba502fff695f719d27afa4cb422391ada8b8ad6 [file] [log] [blame]
Stephen Hines36b56882014-04-23 16:57:46 -07001//===-- 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
Stephen Hines36b56882014-04-23 16:57:46 -070016#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"
24
25
26using namespace llvm;
27
Stephen Hinesdce4a402014-05-29 02:49:00 -070028#define DEBUG_TYPE "stackmaps"
29
Stephen Hines36b56882014-04-23 16:57:46 -070030namespace llvm {
Stephen Hines36b56882014-04-23 16:57:46 -070031cl::opt<bool> EnablePatchPointLiveness("enable-patchpoint-liveness",
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070032 cl::Hidden, cl::init(true),
33 cl::desc("Enable PatchPoint Liveness Analysis Pass"));
Stephen Hines36b56882014-04-23 16:57:46 -070034}
35
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
42char StackMapLiveness::ID = 0;
43char &llvm::StackMapLivenessID = StackMapLiveness::ID;
44INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
45 "StackMap Liveness Analysis", false, false)
46
47/// Default construct and initialize the pass.
48StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
49 initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
50}
51
52/// Tell the pass manager which passes we depend on and what information we
53/// preserve.
54void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
55 // We preserve all information.
56 AU.setPreservesAll();
57 AU.setPreservesCFG();
58 // Default dependencie for all MachineFunction passes.
59 AU.addRequired<MachineFunctionAnalysis>();
60}
61
62/// Calculate the liveness information for the given machine function.
63bool StackMapLiveness::runOnMachineFunction(MachineFunction &_MF) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070064 if (!EnablePatchPointLiveness)
65 return false;
66
Stephen Hines36b56882014-04-23 16:57:46 -070067 DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
68 << _MF.getName() << " **********\n");
69 MF = &_MF;
70 TRI = MF->getTarget().getRegisterInfo();
71 ++NumStackMapFuncVisited;
72
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070073 // Skip this function if there are no patchpoints to process.
74 if (!MF->getFrameInfo()->hasPatchPoint()) {
Stephen Hines36b56882014-04-23 16:57:46 -070075 ++NumStackMapFuncSkipped;
76 return false;
77 }
78 return calculateLiveness();
79}
80
81/// Performs the actual liveness calculation for the function.
82bool StackMapLiveness::calculateLiveness() {
83 bool HasChanged = false;
84 // For all basic blocks in the function.
85 for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
86 MBBI != MBBE; ++MBBI) {
87 DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n");
88 LiveRegs.init(TRI);
89 LiveRegs.addLiveOuts(MBBI);
90 bool HasStackMap = false;
91 // Reverse iterate over all instructions and add the current live register
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070092 // set to an instruction if we encounter a patchpoint instruction.
Stephen Hines36b56882014-04-23 16:57:46 -070093 for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(),
94 E = MBBI->rend(); I != E; ++I) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070095 if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
Stephen Hines36b56882014-04-23 16:57:46 -070096 addLiveOutSetToMI(*I);
97 HasChanged = true;
98 HasStackMap = true;
99 ++NumStackMaps;
100 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700101 DEBUG(dbgs() << " " << LiveRegs << " " << *I);
Stephen Hines36b56882014-04-23 16:57:46 -0700102 LiveRegs.stepBackward(*I);
103 }
104 ++NumBBsVisited;
105 if (!HasStackMap)
106 ++NumBBsHaveNoStackmap;
107 }
108 return HasChanged;
109}
110
111/// Add the current register live set to the instruction.
112void StackMapLiveness::addLiveOutSetToMI(MachineInstr &MI) {
113 uint32_t *Mask = createRegisterMask();
114 MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
115 MI.addOperand(*MF, MO);
116}
117
118/// Create a register mask and initialize it with the registers from the
119/// register live set.
120uint32_t *StackMapLiveness::createRegisterMask() const {
121 // The mask is owned and cleaned up by the Machine Function.
122 uint32_t *Mask = MF->allocateRegisterMask(TRI->getNumRegs());
123 for (LivePhysRegs::const_iterator RI = LiveRegs.begin(), RE = LiveRegs.end();
124 RI != RE; ++RI)
125 Mask[*RI / 32] |= 1U << (*RI % 32);
126 return Mask;
127}