15ffd83dbSDimitry Andric //===- StackLifetime.cpp - Alloca Lifetime Analysis -----------------------===// 25ffd83dbSDimitry Andric // 35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65ffd83dbSDimitry Andric // 75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===// 85ffd83dbSDimitry Andric 95ffd83dbSDimitry Andric #include "llvm/Analysis/StackLifetime.h" 105ffd83dbSDimitry Andric #include "llvm/ADT/DepthFirstIterator.h" 115ffd83dbSDimitry Andric #include "llvm/ADT/STLExtras.h" 125ffd83dbSDimitry Andric #include "llvm/ADT/SmallVector.h" 135ffd83dbSDimitry Andric #include "llvm/ADT/StringExtras.h" 14*e8d8bef9SDimitry Andric #include "llvm/Analysis/ValueTracking.h" 155ffd83dbSDimitry Andric #include "llvm/Config/llvm-config.h" 165ffd83dbSDimitry Andric #include "llvm/IR/AssemblyAnnotationWriter.h" 175ffd83dbSDimitry Andric #include "llvm/IR/BasicBlock.h" 185ffd83dbSDimitry Andric #include "llvm/IR/CFG.h" 195ffd83dbSDimitry Andric #include "llvm/IR/InstIterator.h" 205ffd83dbSDimitry Andric #include "llvm/IR/Instructions.h" 215ffd83dbSDimitry Andric #include "llvm/IR/IntrinsicInst.h" 225ffd83dbSDimitry Andric #include "llvm/IR/Intrinsics.h" 235ffd83dbSDimitry Andric #include "llvm/IR/User.h" 245ffd83dbSDimitry Andric #include "llvm/IR/Value.h" 255ffd83dbSDimitry Andric #include "llvm/Pass.h" 265ffd83dbSDimitry Andric #include "llvm/Support/Casting.h" 275ffd83dbSDimitry Andric #include "llvm/Support/CommandLine.h" 285ffd83dbSDimitry Andric #include "llvm/Support/Compiler.h" 295ffd83dbSDimitry Andric #include "llvm/Support/Debug.h" 305ffd83dbSDimitry Andric #include "llvm/Support/FormattedStream.h" 315ffd83dbSDimitry Andric #include <algorithm> 325ffd83dbSDimitry Andric #include <memory> 335ffd83dbSDimitry Andric #include <tuple> 345ffd83dbSDimitry Andric 355ffd83dbSDimitry Andric using namespace llvm; 365ffd83dbSDimitry Andric 375ffd83dbSDimitry Andric #define DEBUG_TYPE "stack-lifetime" 385ffd83dbSDimitry Andric 395ffd83dbSDimitry Andric const StackLifetime::LiveRange & 405ffd83dbSDimitry Andric StackLifetime::getLiveRange(const AllocaInst *AI) const { 415ffd83dbSDimitry Andric const auto IT = AllocaNumbering.find(AI); 425ffd83dbSDimitry Andric assert(IT != AllocaNumbering.end()); 435ffd83dbSDimitry Andric return LiveRanges[IT->second]; 445ffd83dbSDimitry Andric } 455ffd83dbSDimitry Andric 465ffd83dbSDimitry Andric bool StackLifetime::isReachable(const Instruction *I) const { 475ffd83dbSDimitry Andric return BlockInstRange.find(I->getParent()) != BlockInstRange.end(); 485ffd83dbSDimitry Andric } 495ffd83dbSDimitry Andric 505ffd83dbSDimitry Andric bool StackLifetime::isAliveAfter(const AllocaInst *AI, 515ffd83dbSDimitry Andric const Instruction *I) const { 525ffd83dbSDimitry Andric const BasicBlock *BB = I->getParent(); 535ffd83dbSDimitry Andric auto ItBB = BlockInstRange.find(BB); 545ffd83dbSDimitry Andric assert(ItBB != BlockInstRange.end() && "Unreachable is not expected"); 555ffd83dbSDimitry Andric 565ffd83dbSDimitry Andric // Search the block for the first instruction following 'I'. 575ffd83dbSDimitry Andric auto It = std::upper_bound(Instructions.begin() + ItBB->getSecond().first + 1, 585ffd83dbSDimitry Andric Instructions.begin() + ItBB->getSecond().second, I, 595ffd83dbSDimitry Andric [](const Instruction *L, const Instruction *R) { 605ffd83dbSDimitry Andric return L->comesBefore(R); 615ffd83dbSDimitry Andric }); 625ffd83dbSDimitry Andric --It; 635ffd83dbSDimitry Andric unsigned InstNum = It - Instructions.begin(); 645ffd83dbSDimitry Andric return getLiveRange(AI).test(InstNum); 655ffd83dbSDimitry Andric } 665ffd83dbSDimitry Andric 67*e8d8bef9SDimitry Andric // Returns unique alloca annotated by lifetime marker only if 68*e8d8bef9SDimitry Andric // markers has the same size and points to the alloca start. 69*e8d8bef9SDimitry Andric static const AllocaInst *findMatchingAlloca(const IntrinsicInst &II, 70*e8d8bef9SDimitry Andric const DataLayout &DL) { 71*e8d8bef9SDimitry Andric const AllocaInst *AI = findAllocaForValue(II.getArgOperand(1), true); 72*e8d8bef9SDimitry Andric if (!AI) 73*e8d8bef9SDimitry Andric return nullptr; 745ffd83dbSDimitry Andric 75*e8d8bef9SDimitry Andric auto AllocaSizeInBits = AI->getAllocationSizeInBits(DL); 76*e8d8bef9SDimitry Andric if (!AllocaSizeInBits) 77*e8d8bef9SDimitry Andric return nullptr; 78*e8d8bef9SDimitry Andric int64_t AllocaSize = AllocaSizeInBits.getValue() / 8; 79*e8d8bef9SDimitry Andric 80*e8d8bef9SDimitry Andric auto *Size = dyn_cast<ConstantInt>(II.getArgOperand(0)); 81*e8d8bef9SDimitry Andric if (!Size) 82*e8d8bef9SDimitry Andric return nullptr; 83*e8d8bef9SDimitry Andric int64_t LifetimeSize = Size->getSExtValue(); 84*e8d8bef9SDimitry Andric 85*e8d8bef9SDimitry Andric if (LifetimeSize != -1 && LifetimeSize != AllocaSize) 86*e8d8bef9SDimitry Andric return nullptr; 87*e8d8bef9SDimitry Andric 88*e8d8bef9SDimitry Andric return AI; 895ffd83dbSDimitry Andric } 905ffd83dbSDimitry Andric 915ffd83dbSDimitry Andric void StackLifetime::collectMarkers() { 925ffd83dbSDimitry Andric InterestingAllocas.resize(NumAllocas); 935ffd83dbSDimitry Andric DenseMap<const BasicBlock *, SmallDenseMap<const IntrinsicInst *, Marker>> 945ffd83dbSDimitry Andric BBMarkerSet; 955ffd83dbSDimitry Andric 96*e8d8bef9SDimitry Andric const DataLayout &DL = F.getParent()->getDataLayout(); 97*e8d8bef9SDimitry Andric 985ffd83dbSDimitry Andric // Compute the set of start/end markers per basic block. 99*e8d8bef9SDimitry Andric for (const BasicBlock *BB : depth_first(&F)) { 100*e8d8bef9SDimitry Andric for (const Instruction &I : *BB) { 101*e8d8bef9SDimitry Andric const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I); 102*e8d8bef9SDimitry Andric if (!II || !II->isLifetimeStartOrEnd()) 103*e8d8bef9SDimitry Andric continue; 104*e8d8bef9SDimitry Andric const AllocaInst *AI = findMatchingAlloca(*II, DL); 105*e8d8bef9SDimitry Andric if (!AI) { 106*e8d8bef9SDimitry Andric HasUnknownLifetimeStartOrEnd = true; 1075ffd83dbSDimitry Andric continue; 1085ffd83dbSDimitry Andric } 109*e8d8bef9SDimitry Andric auto It = AllocaNumbering.find(AI); 110*e8d8bef9SDimitry Andric if (It == AllocaNumbering.end()) 1115ffd83dbSDimitry Andric continue; 112*e8d8bef9SDimitry Andric auto AllocaNo = It->second; 113*e8d8bef9SDimitry Andric bool IsStart = II->getIntrinsicID() == Intrinsic::lifetime_start; 1145ffd83dbSDimitry Andric if (IsStart) 1155ffd83dbSDimitry Andric InterestingAllocas.set(AllocaNo); 116*e8d8bef9SDimitry Andric BBMarkerSet[BB][II] = {AllocaNo, IsStart}; 1175ffd83dbSDimitry Andric } 1185ffd83dbSDimitry Andric } 1195ffd83dbSDimitry Andric 1205ffd83dbSDimitry Andric // Compute instruction numbering. Only the following instructions are 1215ffd83dbSDimitry Andric // considered: 1225ffd83dbSDimitry Andric // * Basic block entries 1235ffd83dbSDimitry Andric // * Lifetime markers 1245ffd83dbSDimitry Andric // For each basic block, compute 1255ffd83dbSDimitry Andric // * the list of markers in the instruction order 1265ffd83dbSDimitry Andric // * the sets of allocas whose lifetime starts or ends in this BB 1275ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Instructions:\n"); 1285ffd83dbSDimitry Andric for (const BasicBlock *BB : depth_first(&F)) { 1295ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << " " << Instructions.size() << ": BB " << BB->getName() 1305ffd83dbSDimitry Andric << "\n"); 1315ffd83dbSDimitry Andric auto BBStart = Instructions.size(); 1325ffd83dbSDimitry Andric Instructions.push_back(nullptr); 1335ffd83dbSDimitry Andric 1345ffd83dbSDimitry Andric BlockLifetimeInfo &BlockInfo = 1355ffd83dbSDimitry Andric BlockLiveness.try_emplace(BB, NumAllocas).first->getSecond(); 1365ffd83dbSDimitry Andric 1375ffd83dbSDimitry Andric auto &BlockMarkerSet = BBMarkerSet[BB]; 1385ffd83dbSDimitry Andric if (BlockMarkerSet.empty()) { 1395ffd83dbSDimitry Andric BlockInstRange[BB] = std::make_pair(BBStart, Instructions.size()); 1405ffd83dbSDimitry Andric continue; 1415ffd83dbSDimitry Andric } 1425ffd83dbSDimitry Andric 1435ffd83dbSDimitry Andric auto ProcessMarker = [&](const IntrinsicInst *I, const Marker &M) { 1445ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << " " << Instructions.size() << ": " 1455ffd83dbSDimitry Andric << (M.IsStart ? "start " : "end ") << M.AllocaNo 1465ffd83dbSDimitry Andric << ", " << *I << "\n"); 1475ffd83dbSDimitry Andric 1485ffd83dbSDimitry Andric BBMarkers[BB].push_back({Instructions.size(), M}); 1495ffd83dbSDimitry Andric Instructions.push_back(I); 1505ffd83dbSDimitry Andric 1515ffd83dbSDimitry Andric if (M.IsStart) { 1525ffd83dbSDimitry Andric BlockInfo.End.reset(M.AllocaNo); 1535ffd83dbSDimitry Andric BlockInfo.Begin.set(M.AllocaNo); 1545ffd83dbSDimitry Andric } else { 1555ffd83dbSDimitry Andric BlockInfo.Begin.reset(M.AllocaNo); 1565ffd83dbSDimitry Andric BlockInfo.End.set(M.AllocaNo); 1575ffd83dbSDimitry Andric } 1585ffd83dbSDimitry Andric }; 1595ffd83dbSDimitry Andric 1605ffd83dbSDimitry Andric if (BlockMarkerSet.size() == 1) { 1615ffd83dbSDimitry Andric ProcessMarker(BlockMarkerSet.begin()->getFirst(), 1625ffd83dbSDimitry Andric BlockMarkerSet.begin()->getSecond()); 1635ffd83dbSDimitry Andric } else { 1645ffd83dbSDimitry Andric // Scan the BB to determine the marker order. 1655ffd83dbSDimitry Andric for (const Instruction &I : *BB) { 1665ffd83dbSDimitry Andric const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I); 1675ffd83dbSDimitry Andric if (!II) 1685ffd83dbSDimitry Andric continue; 1695ffd83dbSDimitry Andric auto It = BlockMarkerSet.find(II); 1705ffd83dbSDimitry Andric if (It == BlockMarkerSet.end()) 1715ffd83dbSDimitry Andric continue; 1725ffd83dbSDimitry Andric ProcessMarker(II, It->getSecond()); 1735ffd83dbSDimitry Andric } 1745ffd83dbSDimitry Andric } 1755ffd83dbSDimitry Andric 1765ffd83dbSDimitry Andric BlockInstRange[BB] = std::make_pair(BBStart, Instructions.size()); 1775ffd83dbSDimitry Andric } 1785ffd83dbSDimitry Andric } 1795ffd83dbSDimitry Andric 1805ffd83dbSDimitry Andric void StackLifetime::calculateLocalLiveness() { 1815ffd83dbSDimitry Andric bool Changed = true; 1825ffd83dbSDimitry Andric while (Changed) { 1835ffd83dbSDimitry Andric Changed = false; 1845ffd83dbSDimitry Andric 1855ffd83dbSDimitry Andric for (const BasicBlock *BB : depth_first(&F)) { 1865ffd83dbSDimitry Andric BlockLifetimeInfo &BlockInfo = BlockLiveness.find(BB)->getSecond(); 1875ffd83dbSDimitry Andric 1885ffd83dbSDimitry Andric // Compute LiveIn by unioning together the LiveOut sets of all preds. 1895ffd83dbSDimitry Andric BitVector LocalLiveIn; 1905ffd83dbSDimitry Andric for (auto *PredBB : predecessors(BB)) { 1915ffd83dbSDimitry Andric LivenessMap::const_iterator I = BlockLiveness.find(PredBB); 1925ffd83dbSDimitry Andric // If a predecessor is unreachable, ignore it. 1935ffd83dbSDimitry Andric if (I == BlockLiveness.end()) 1945ffd83dbSDimitry Andric continue; 1955ffd83dbSDimitry Andric switch (Type) { 1965ffd83dbSDimitry Andric case LivenessType::May: 1975ffd83dbSDimitry Andric LocalLiveIn |= I->second.LiveOut; 1985ffd83dbSDimitry Andric break; 1995ffd83dbSDimitry Andric case LivenessType::Must: 2005ffd83dbSDimitry Andric if (LocalLiveIn.empty()) 2015ffd83dbSDimitry Andric LocalLiveIn = I->second.LiveOut; 2025ffd83dbSDimitry Andric else 2035ffd83dbSDimitry Andric LocalLiveIn &= I->second.LiveOut; 2045ffd83dbSDimitry Andric break; 2055ffd83dbSDimitry Andric } 2065ffd83dbSDimitry Andric } 2075ffd83dbSDimitry Andric 2085ffd83dbSDimitry Andric // Compute LiveOut by subtracting out lifetimes that end in this 2095ffd83dbSDimitry Andric // block, then adding in lifetimes that begin in this block. If 2105ffd83dbSDimitry Andric // we have both BEGIN and END markers in the same basic block 2115ffd83dbSDimitry Andric // then we know that the BEGIN marker comes after the END, 2125ffd83dbSDimitry Andric // because we already handle the case where the BEGIN comes 2135ffd83dbSDimitry Andric // before the END when collecting the markers (and building the 2145ffd83dbSDimitry Andric // BEGIN/END vectors). 2155ffd83dbSDimitry Andric BitVector LocalLiveOut = LocalLiveIn; 2165ffd83dbSDimitry Andric LocalLiveOut.reset(BlockInfo.End); 2175ffd83dbSDimitry Andric LocalLiveOut |= BlockInfo.Begin; 2185ffd83dbSDimitry Andric 2195ffd83dbSDimitry Andric // Update block LiveIn set, noting whether it has changed. 2205ffd83dbSDimitry Andric if (LocalLiveIn.test(BlockInfo.LiveIn)) { 2215ffd83dbSDimitry Andric BlockInfo.LiveIn |= LocalLiveIn; 2225ffd83dbSDimitry Andric } 2235ffd83dbSDimitry Andric 2245ffd83dbSDimitry Andric // Update block LiveOut set, noting whether it has changed. 2255ffd83dbSDimitry Andric if (LocalLiveOut.test(BlockInfo.LiveOut)) { 2265ffd83dbSDimitry Andric Changed = true; 2275ffd83dbSDimitry Andric BlockInfo.LiveOut |= LocalLiveOut; 2285ffd83dbSDimitry Andric } 2295ffd83dbSDimitry Andric } 2305ffd83dbSDimitry Andric } // while changed. 2315ffd83dbSDimitry Andric } 2325ffd83dbSDimitry Andric 2335ffd83dbSDimitry Andric void StackLifetime::calculateLiveIntervals() { 2345ffd83dbSDimitry Andric for (auto IT : BlockLiveness) { 2355ffd83dbSDimitry Andric const BasicBlock *BB = IT.getFirst(); 2365ffd83dbSDimitry Andric BlockLifetimeInfo &BlockInfo = IT.getSecond(); 2375ffd83dbSDimitry Andric unsigned BBStart, BBEnd; 2385ffd83dbSDimitry Andric std::tie(BBStart, BBEnd) = BlockInstRange[BB]; 2395ffd83dbSDimitry Andric 2405ffd83dbSDimitry Andric BitVector Started, Ended; 2415ffd83dbSDimitry Andric Started.resize(NumAllocas); 2425ffd83dbSDimitry Andric Ended.resize(NumAllocas); 2435ffd83dbSDimitry Andric SmallVector<unsigned, 8> Start; 2445ffd83dbSDimitry Andric Start.resize(NumAllocas); 2455ffd83dbSDimitry Andric 2465ffd83dbSDimitry Andric // LiveIn ranges start at the first instruction. 2475ffd83dbSDimitry Andric for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) { 2485ffd83dbSDimitry Andric if (BlockInfo.LiveIn.test(AllocaNo)) { 2495ffd83dbSDimitry Andric Started.set(AllocaNo); 2505ffd83dbSDimitry Andric Start[AllocaNo] = BBStart; 2515ffd83dbSDimitry Andric } 2525ffd83dbSDimitry Andric } 2535ffd83dbSDimitry Andric 2545ffd83dbSDimitry Andric for (auto &It : BBMarkers[BB]) { 2555ffd83dbSDimitry Andric unsigned InstNo = It.first; 2565ffd83dbSDimitry Andric bool IsStart = It.second.IsStart; 2575ffd83dbSDimitry Andric unsigned AllocaNo = It.second.AllocaNo; 2585ffd83dbSDimitry Andric 2595ffd83dbSDimitry Andric if (IsStart) { 2605ffd83dbSDimitry Andric assert(!Started.test(AllocaNo) || Start[AllocaNo] == BBStart); 2615ffd83dbSDimitry Andric if (!Started.test(AllocaNo)) { 2625ffd83dbSDimitry Andric Started.set(AllocaNo); 2635ffd83dbSDimitry Andric Ended.reset(AllocaNo); 2645ffd83dbSDimitry Andric Start[AllocaNo] = InstNo; 2655ffd83dbSDimitry Andric } 2665ffd83dbSDimitry Andric } else { 2675ffd83dbSDimitry Andric assert(!Ended.test(AllocaNo)); 2685ffd83dbSDimitry Andric if (Started.test(AllocaNo)) { 2695ffd83dbSDimitry Andric LiveRanges[AllocaNo].addRange(Start[AllocaNo], InstNo); 2705ffd83dbSDimitry Andric Started.reset(AllocaNo); 2715ffd83dbSDimitry Andric } 2725ffd83dbSDimitry Andric Ended.set(AllocaNo); 2735ffd83dbSDimitry Andric } 2745ffd83dbSDimitry Andric } 2755ffd83dbSDimitry Andric 2765ffd83dbSDimitry Andric for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) 2775ffd83dbSDimitry Andric if (Started.test(AllocaNo)) 2785ffd83dbSDimitry Andric LiveRanges[AllocaNo].addRange(Start[AllocaNo], BBEnd); 2795ffd83dbSDimitry Andric } 2805ffd83dbSDimitry Andric } 2815ffd83dbSDimitry Andric 2825ffd83dbSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2835ffd83dbSDimitry Andric LLVM_DUMP_METHOD void StackLifetime::dumpAllocas() const { 2845ffd83dbSDimitry Andric dbgs() << "Allocas:\n"; 2855ffd83dbSDimitry Andric for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) 2865ffd83dbSDimitry Andric dbgs() << " " << AllocaNo << ": " << *Allocas[AllocaNo] << "\n"; 2875ffd83dbSDimitry Andric } 2885ffd83dbSDimitry Andric 2895ffd83dbSDimitry Andric LLVM_DUMP_METHOD void StackLifetime::dumpBlockLiveness() const { 2905ffd83dbSDimitry Andric dbgs() << "Block liveness:\n"; 2915ffd83dbSDimitry Andric for (auto IT : BlockLiveness) { 2925ffd83dbSDimitry Andric const BasicBlock *BB = IT.getFirst(); 2935ffd83dbSDimitry Andric const BlockLifetimeInfo &BlockInfo = BlockLiveness.find(BB)->getSecond(); 2945ffd83dbSDimitry Andric auto BlockRange = BlockInstRange.find(BB)->getSecond(); 295*e8d8bef9SDimitry Andric dbgs() << " BB (" << BB->getName() << ") [" << BlockRange.first << ", " << BlockRange.second 2965ffd83dbSDimitry Andric << "): begin " << BlockInfo.Begin << ", end " << BlockInfo.End 2975ffd83dbSDimitry Andric << ", livein " << BlockInfo.LiveIn << ", liveout " 2985ffd83dbSDimitry Andric << BlockInfo.LiveOut << "\n"; 2995ffd83dbSDimitry Andric } 3005ffd83dbSDimitry Andric } 3015ffd83dbSDimitry Andric 3025ffd83dbSDimitry Andric LLVM_DUMP_METHOD void StackLifetime::dumpLiveRanges() const { 3035ffd83dbSDimitry Andric dbgs() << "Alloca liveness:\n"; 3045ffd83dbSDimitry Andric for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) 3055ffd83dbSDimitry Andric dbgs() << " " << AllocaNo << ": " << LiveRanges[AllocaNo] << "\n"; 3065ffd83dbSDimitry Andric } 3075ffd83dbSDimitry Andric #endif 3085ffd83dbSDimitry Andric 3095ffd83dbSDimitry Andric StackLifetime::StackLifetime(const Function &F, 3105ffd83dbSDimitry Andric ArrayRef<const AllocaInst *> Allocas, 3115ffd83dbSDimitry Andric LivenessType Type) 3125ffd83dbSDimitry Andric : F(F), Type(Type), Allocas(Allocas), NumAllocas(Allocas.size()) { 3135ffd83dbSDimitry Andric LLVM_DEBUG(dumpAllocas()); 3145ffd83dbSDimitry Andric 3155ffd83dbSDimitry Andric for (unsigned I = 0; I < NumAllocas; ++I) 3165ffd83dbSDimitry Andric AllocaNumbering[Allocas[I]] = I; 3175ffd83dbSDimitry Andric 3185ffd83dbSDimitry Andric collectMarkers(); 3195ffd83dbSDimitry Andric } 3205ffd83dbSDimitry Andric 3215ffd83dbSDimitry Andric void StackLifetime::run() { 322*e8d8bef9SDimitry Andric if (HasUnknownLifetimeStartOrEnd) { 323*e8d8bef9SDimitry Andric // There is marker which we can't assign to a specific alloca, so we 324*e8d8bef9SDimitry Andric // fallback to the most conservative results for the type. 325*e8d8bef9SDimitry Andric switch (Type) { 326*e8d8bef9SDimitry Andric case LivenessType::May: 327*e8d8bef9SDimitry Andric LiveRanges.resize(NumAllocas, getFullLiveRange()); 328*e8d8bef9SDimitry Andric break; 329*e8d8bef9SDimitry Andric case LivenessType::Must: 330*e8d8bef9SDimitry Andric LiveRanges.resize(NumAllocas, LiveRange(Instructions.size())); 331*e8d8bef9SDimitry Andric break; 332*e8d8bef9SDimitry Andric } 333*e8d8bef9SDimitry Andric return; 334*e8d8bef9SDimitry Andric } 335*e8d8bef9SDimitry Andric 3365ffd83dbSDimitry Andric LiveRanges.resize(NumAllocas, LiveRange(Instructions.size())); 3375ffd83dbSDimitry Andric for (unsigned I = 0; I < NumAllocas; ++I) 3385ffd83dbSDimitry Andric if (!InterestingAllocas.test(I)) 3395ffd83dbSDimitry Andric LiveRanges[I] = getFullLiveRange(); 3405ffd83dbSDimitry Andric 3415ffd83dbSDimitry Andric calculateLocalLiveness(); 3425ffd83dbSDimitry Andric LLVM_DEBUG(dumpBlockLiveness()); 3435ffd83dbSDimitry Andric calculateLiveIntervals(); 3445ffd83dbSDimitry Andric LLVM_DEBUG(dumpLiveRanges()); 3455ffd83dbSDimitry Andric } 3465ffd83dbSDimitry Andric 3475ffd83dbSDimitry Andric class StackLifetime::LifetimeAnnotationWriter 3485ffd83dbSDimitry Andric : public AssemblyAnnotationWriter { 3495ffd83dbSDimitry Andric const StackLifetime &SL; 3505ffd83dbSDimitry Andric 3515ffd83dbSDimitry Andric void printInstrAlive(unsigned InstrNo, formatted_raw_ostream &OS) { 3525ffd83dbSDimitry Andric SmallVector<StringRef, 16> Names; 3535ffd83dbSDimitry Andric for (const auto &KV : SL.AllocaNumbering) { 3545ffd83dbSDimitry Andric if (SL.LiveRanges[KV.getSecond()].test(InstrNo)) 3555ffd83dbSDimitry Andric Names.push_back(KV.getFirst()->getName()); 3565ffd83dbSDimitry Andric } 3575ffd83dbSDimitry Andric llvm::sort(Names); 3585ffd83dbSDimitry Andric OS << " ; Alive: <" << llvm::join(Names, " ") << ">\n"; 3595ffd83dbSDimitry Andric } 3605ffd83dbSDimitry Andric 3615ffd83dbSDimitry Andric void emitBasicBlockStartAnnot(const BasicBlock *BB, 3625ffd83dbSDimitry Andric formatted_raw_ostream &OS) override { 3635ffd83dbSDimitry Andric auto ItBB = SL.BlockInstRange.find(BB); 3645ffd83dbSDimitry Andric if (ItBB == SL.BlockInstRange.end()) 3655ffd83dbSDimitry Andric return; // Unreachable. 3665ffd83dbSDimitry Andric printInstrAlive(ItBB->getSecond().first, OS); 3675ffd83dbSDimitry Andric } 3685ffd83dbSDimitry Andric 3695ffd83dbSDimitry Andric void printInfoComment(const Value &V, formatted_raw_ostream &OS) override { 3705ffd83dbSDimitry Andric const Instruction *Instr = dyn_cast<Instruction>(&V); 3715ffd83dbSDimitry Andric if (!Instr || !SL.isReachable(Instr)) 3725ffd83dbSDimitry Andric return; 3735ffd83dbSDimitry Andric 3745ffd83dbSDimitry Andric SmallVector<StringRef, 16> Names; 3755ffd83dbSDimitry Andric for (const auto &KV : SL.AllocaNumbering) { 3765ffd83dbSDimitry Andric if (SL.isAliveAfter(KV.getFirst(), Instr)) 3775ffd83dbSDimitry Andric Names.push_back(KV.getFirst()->getName()); 3785ffd83dbSDimitry Andric } 3795ffd83dbSDimitry Andric llvm::sort(Names); 3805ffd83dbSDimitry Andric OS << "\n ; Alive: <" << llvm::join(Names, " ") << ">\n"; 3815ffd83dbSDimitry Andric } 3825ffd83dbSDimitry Andric 3835ffd83dbSDimitry Andric public: 3845ffd83dbSDimitry Andric LifetimeAnnotationWriter(const StackLifetime &SL) : SL(SL) {} 3855ffd83dbSDimitry Andric }; 3865ffd83dbSDimitry Andric 3875ffd83dbSDimitry Andric void StackLifetime::print(raw_ostream &OS) { 3885ffd83dbSDimitry Andric LifetimeAnnotationWriter AAW(*this); 3895ffd83dbSDimitry Andric F.print(OS, &AAW); 3905ffd83dbSDimitry Andric } 3915ffd83dbSDimitry Andric 3925ffd83dbSDimitry Andric PreservedAnalyses StackLifetimePrinterPass::run(Function &F, 3935ffd83dbSDimitry Andric FunctionAnalysisManager &AM) { 3945ffd83dbSDimitry Andric SmallVector<const AllocaInst *, 8> Allocas; 3955ffd83dbSDimitry Andric for (auto &I : instructions(F)) 3965ffd83dbSDimitry Andric if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) 3975ffd83dbSDimitry Andric Allocas.push_back(AI); 3985ffd83dbSDimitry Andric StackLifetime SL(F, Allocas, Type); 3995ffd83dbSDimitry Andric SL.run(); 4005ffd83dbSDimitry Andric SL.print(OS); 4015ffd83dbSDimitry Andric return PreservedAnalyses::all(); 4025ffd83dbSDimitry Andric } 403