16a1babd4dfa4b17b28461f02c33853931a70f49
[oota-llvm.git] / lib / CodeGen / StackMapLivenessAnalysis.cpp
1 //===-- 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
16 #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
26 using namespace llvm;
27
28 #define DEBUG_TYPE "stackmaps"
29
30 namespace llvm {
31 cl::opt<bool> EnablePatchPointLiveness("enable-patchpoint-liveness",
32   cl::Hidden, cl::desc("Enable PatchPoint Liveness Analysis Pass"));
33 }
34
35 STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
36 STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
37 STATISTIC(NumBBsVisited,          "Number of basic blocks visited");
38 STATISTIC(NumBBsHaveNoStackmap,   "Number of basic blocks with no stackmap");
39 STATISTIC(NumStackMaps,           "Number of StackMaps visited");
40
41 char StackMapLiveness::ID = 0;
42 char &llvm::StackMapLivenessID = StackMapLiveness::ID;
43 INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
44                 "StackMap Liveness Analysis", false, false)
45
46 /// Default construct and initialize the pass.
47 StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
48   initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
49 }
50
51 /// Tell the pass manager which passes we depend on and what information we
52 /// preserve.
53 void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
54   // We preserve all information.
55   AU.setPreservesAll();
56   AU.setPreservesCFG();
57   // Default dependencie for all MachineFunction passes.
58   AU.addRequired<MachineFunctionAnalysis>();
59 }
60
61 /// Calculate the liveness information for the given machine function.
62 bool StackMapLiveness::runOnMachineFunction(MachineFunction &_MF) {
63   DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
64                << _MF.getName() << " **********\n");
65   MF = &_MF;
66   TRI = MF->getTarget().getRegisterInfo();
67   ++NumStackMapFuncVisited;
68
69   // Skip this function if there are no patchpoints to process.
70   if (!(MF->getFrameInfo()->hasPatchPoint() && EnablePatchPointLiveness)) {
71     ++NumStackMapFuncSkipped;
72     return false;
73   }
74   return calculateLiveness();
75 }
76
77 /// Performs the actual liveness calculation for the function.
78 bool StackMapLiveness::calculateLiveness() {
79   bool HasChanged = false;
80   // For all basic blocks in the function.
81   for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
82        MBBI != MBBE; ++MBBI) {
83     DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n");
84     LiveRegs.init(TRI);
85     LiveRegs.addLiveOuts(MBBI);
86     bool HasStackMap = false;
87     // Reverse iterate over all instructions and add the current live register
88     // set to an instruction if we encounter a patchpoint instruction.
89     for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(),
90          E = MBBI->rend(); I != E; ++I) {
91       int Opc = I->getOpcode();
92       if (Opc == TargetOpcode::PATCHPOINT) {
93         addLiveOutSetToMI(*I);
94         HasChanged = true;
95         HasStackMap = true;
96         ++NumStackMaps;
97       }
98       DEBUG(dbgs() << "   " << LiveRegs << "   " << *I);
99       LiveRegs.stepBackward(*I);
100     }
101     ++NumBBsVisited;
102     if (!HasStackMap)
103       ++NumBBsHaveNoStackmap;
104   }
105   return HasChanged;
106 }
107
108 /// Add the current register live set to the instruction.
109 void StackMapLiveness::addLiveOutSetToMI(MachineInstr &MI) {
110   uint32_t *Mask = createRegisterMask();
111   MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
112   MI.addOperand(*MF, MO);
113 }
114
115 /// Create a register mask and initialize it with the registers from the
116 /// register live set.
117 uint32_t *StackMapLiveness::createRegisterMask() const {
118   // The mask is owned and cleaned up by the Machine Function.
119   uint32_t *Mask = MF->allocateRegisterMask(TRI->getNumRegs());
120   for (LivePhysRegs::const_iterator RI = LiveRegs.begin(), RE = LiveRegs.end();
121        RI != RE; ++RI)
122     Mask[*RI / 32] |= 1U << (*RI % 32);
123   return Mask;
124 }