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