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