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" 14e8d8bef9SDimitry 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/Value.h" 235ffd83dbSDimitry Andric #include "llvm/Support/Casting.h" 245ffd83dbSDimitry Andric #include "llvm/Support/Compiler.h" 255ffd83dbSDimitry Andric #include "llvm/Support/Debug.h" 265ffd83dbSDimitry Andric #include "llvm/Support/FormattedStream.h" 275ffd83dbSDimitry Andric #include <algorithm> 285ffd83dbSDimitry Andric #include <tuple> 295ffd83dbSDimitry Andric 305ffd83dbSDimitry Andric using namespace llvm; 315ffd83dbSDimitry Andric 325ffd83dbSDimitry Andric #define DEBUG_TYPE "stack-lifetime" 335ffd83dbSDimitry Andric 345ffd83dbSDimitry Andric const StackLifetime::LiveRange & 355ffd83dbSDimitry Andric StackLifetime::getLiveRange(const AllocaInst *AI) const { 365ffd83dbSDimitry Andric const auto IT = AllocaNumbering.find(AI); 375ffd83dbSDimitry Andric assert(IT != AllocaNumbering.end()); 385ffd83dbSDimitry Andric return LiveRanges[IT->second]; 395ffd83dbSDimitry Andric } 405ffd83dbSDimitry Andric 415ffd83dbSDimitry Andric bool StackLifetime::isReachable(const Instruction *I) const { 425ffd83dbSDimitry Andric return BlockInstRange.find(I->getParent()) != BlockInstRange.end(); 435ffd83dbSDimitry Andric } 445ffd83dbSDimitry Andric 455ffd83dbSDimitry Andric bool StackLifetime::isAliveAfter(const AllocaInst *AI, 465ffd83dbSDimitry Andric const Instruction *I) const { 475ffd83dbSDimitry Andric const BasicBlock *BB = I->getParent(); 485ffd83dbSDimitry Andric auto ItBB = BlockInstRange.find(BB); 495ffd83dbSDimitry Andric assert(ItBB != BlockInstRange.end() && "Unreachable is not expected"); 505ffd83dbSDimitry Andric 515ffd83dbSDimitry Andric // Search the block for the first instruction following 'I'. 525ffd83dbSDimitry Andric auto It = std::upper_bound(Instructions.begin() + ItBB->getSecond().first + 1, 535ffd83dbSDimitry Andric Instructions.begin() + ItBB->getSecond().second, I, 545ffd83dbSDimitry Andric [](const Instruction *L, const Instruction *R) { 555ffd83dbSDimitry Andric return L->comesBefore(R); 565ffd83dbSDimitry Andric }); 575ffd83dbSDimitry Andric --It; 585ffd83dbSDimitry Andric unsigned InstNum = It - Instructions.begin(); 595ffd83dbSDimitry Andric return getLiveRange(AI).test(InstNum); 605ffd83dbSDimitry Andric } 615ffd83dbSDimitry Andric 62e8d8bef9SDimitry Andric // Returns unique alloca annotated by lifetime marker only if 63e8d8bef9SDimitry Andric // markers has the same size and points to the alloca start. 64e8d8bef9SDimitry Andric static const AllocaInst *findMatchingAlloca(const IntrinsicInst &II, 65e8d8bef9SDimitry Andric const DataLayout &DL) { 66e8d8bef9SDimitry Andric const AllocaInst *AI = findAllocaForValue(II.getArgOperand(1), true); 67e8d8bef9SDimitry Andric if (!AI) 68e8d8bef9SDimitry Andric return nullptr; 695ffd83dbSDimitry Andric 70e8d8bef9SDimitry Andric auto AllocaSizeInBits = AI->getAllocationSizeInBits(DL); 71e8d8bef9SDimitry Andric if (!AllocaSizeInBits) 72e8d8bef9SDimitry Andric return nullptr; 73*81ad6265SDimitry Andric int64_t AllocaSize = *AllocaSizeInBits / 8; 74e8d8bef9SDimitry Andric 75e8d8bef9SDimitry Andric auto *Size = dyn_cast<ConstantInt>(II.getArgOperand(0)); 76e8d8bef9SDimitry Andric if (!Size) 77e8d8bef9SDimitry Andric return nullptr; 78e8d8bef9SDimitry Andric int64_t LifetimeSize = Size->getSExtValue(); 79e8d8bef9SDimitry Andric 80e8d8bef9SDimitry Andric if (LifetimeSize != -1 && LifetimeSize != AllocaSize) 81e8d8bef9SDimitry Andric return nullptr; 82e8d8bef9SDimitry Andric 83e8d8bef9SDimitry Andric return AI; 845ffd83dbSDimitry Andric } 855ffd83dbSDimitry Andric 865ffd83dbSDimitry Andric void StackLifetime::collectMarkers() { 875ffd83dbSDimitry Andric InterestingAllocas.resize(NumAllocas); 885ffd83dbSDimitry Andric DenseMap<const BasicBlock *, SmallDenseMap<const IntrinsicInst *, Marker>> 895ffd83dbSDimitry Andric BBMarkerSet; 905ffd83dbSDimitry Andric 91e8d8bef9SDimitry Andric const DataLayout &DL = F.getParent()->getDataLayout(); 92e8d8bef9SDimitry Andric 935ffd83dbSDimitry Andric // Compute the set of start/end markers per basic block. 94e8d8bef9SDimitry Andric for (const BasicBlock *BB : depth_first(&F)) { 95e8d8bef9SDimitry Andric for (const Instruction &I : *BB) { 96e8d8bef9SDimitry Andric const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I); 97e8d8bef9SDimitry Andric if (!II || !II->isLifetimeStartOrEnd()) 98e8d8bef9SDimitry Andric continue; 99e8d8bef9SDimitry Andric const AllocaInst *AI = findMatchingAlloca(*II, DL); 100e8d8bef9SDimitry Andric if (!AI) { 101e8d8bef9SDimitry Andric HasUnknownLifetimeStartOrEnd = true; 1025ffd83dbSDimitry Andric continue; 1035ffd83dbSDimitry Andric } 104e8d8bef9SDimitry Andric auto It = AllocaNumbering.find(AI); 105e8d8bef9SDimitry Andric if (It == AllocaNumbering.end()) 1065ffd83dbSDimitry Andric continue; 107e8d8bef9SDimitry Andric auto AllocaNo = It->second; 108e8d8bef9SDimitry Andric bool IsStart = II->getIntrinsicID() == Intrinsic::lifetime_start; 1095ffd83dbSDimitry Andric if (IsStart) 1105ffd83dbSDimitry Andric InterestingAllocas.set(AllocaNo); 111e8d8bef9SDimitry Andric BBMarkerSet[BB][II] = {AllocaNo, IsStart}; 1125ffd83dbSDimitry Andric } 1135ffd83dbSDimitry Andric } 1145ffd83dbSDimitry Andric 1155ffd83dbSDimitry Andric // Compute instruction numbering. Only the following instructions are 1165ffd83dbSDimitry Andric // considered: 1175ffd83dbSDimitry Andric // * Basic block entries 1185ffd83dbSDimitry Andric // * Lifetime markers 1195ffd83dbSDimitry Andric // For each basic block, compute 1205ffd83dbSDimitry Andric // * the list of markers in the instruction order 1215ffd83dbSDimitry Andric // * the sets of allocas whose lifetime starts or ends in this BB 1225ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Instructions:\n"); 1235ffd83dbSDimitry Andric for (const BasicBlock *BB : depth_first(&F)) { 1245ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << " " << Instructions.size() << ": BB " << BB->getName() 1255ffd83dbSDimitry Andric << "\n"); 1265ffd83dbSDimitry Andric auto BBStart = Instructions.size(); 1275ffd83dbSDimitry Andric Instructions.push_back(nullptr); 1285ffd83dbSDimitry Andric 1295ffd83dbSDimitry Andric BlockLifetimeInfo &BlockInfo = 1305ffd83dbSDimitry Andric BlockLiveness.try_emplace(BB, NumAllocas).first->getSecond(); 1315ffd83dbSDimitry Andric 1325ffd83dbSDimitry Andric auto &BlockMarkerSet = BBMarkerSet[BB]; 1335ffd83dbSDimitry Andric if (BlockMarkerSet.empty()) { 1345ffd83dbSDimitry Andric BlockInstRange[BB] = std::make_pair(BBStart, Instructions.size()); 1355ffd83dbSDimitry Andric continue; 1365ffd83dbSDimitry Andric } 1375ffd83dbSDimitry Andric 1385ffd83dbSDimitry Andric auto ProcessMarker = [&](const IntrinsicInst *I, const Marker &M) { 1395ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << " " << Instructions.size() << ": " 1405ffd83dbSDimitry Andric << (M.IsStart ? "start " : "end ") << M.AllocaNo 1415ffd83dbSDimitry Andric << ", " << *I << "\n"); 1425ffd83dbSDimitry Andric 1435ffd83dbSDimitry Andric BBMarkers[BB].push_back({Instructions.size(), M}); 1445ffd83dbSDimitry Andric Instructions.push_back(I); 1455ffd83dbSDimitry Andric 1465ffd83dbSDimitry Andric if (M.IsStart) { 1475ffd83dbSDimitry Andric BlockInfo.End.reset(M.AllocaNo); 1485ffd83dbSDimitry Andric BlockInfo.Begin.set(M.AllocaNo); 1495ffd83dbSDimitry Andric } else { 1505ffd83dbSDimitry Andric BlockInfo.Begin.reset(M.AllocaNo); 1515ffd83dbSDimitry Andric BlockInfo.End.set(M.AllocaNo); 1525ffd83dbSDimitry Andric } 1535ffd83dbSDimitry Andric }; 1545ffd83dbSDimitry Andric 1555ffd83dbSDimitry Andric if (BlockMarkerSet.size() == 1) { 1565ffd83dbSDimitry Andric ProcessMarker(BlockMarkerSet.begin()->getFirst(), 1575ffd83dbSDimitry Andric BlockMarkerSet.begin()->getSecond()); 1585ffd83dbSDimitry Andric } else { 1595ffd83dbSDimitry Andric // Scan the BB to determine the marker order. 1605ffd83dbSDimitry Andric for (const Instruction &I : *BB) { 1615ffd83dbSDimitry Andric const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I); 1625ffd83dbSDimitry Andric if (!II) 1635ffd83dbSDimitry Andric continue; 1645ffd83dbSDimitry Andric auto It = BlockMarkerSet.find(II); 1655ffd83dbSDimitry Andric if (It == BlockMarkerSet.end()) 1665ffd83dbSDimitry Andric continue; 1675ffd83dbSDimitry Andric ProcessMarker(II, It->getSecond()); 1685ffd83dbSDimitry Andric } 1695ffd83dbSDimitry Andric } 1705ffd83dbSDimitry Andric 1715ffd83dbSDimitry Andric BlockInstRange[BB] = std::make_pair(BBStart, Instructions.size()); 1725ffd83dbSDimitry Andric } 1735ffd83dbSDimitry Andric } 1745ffd83dbSDimitry Andric 1755ffd83dbSDimitry Andric void StackLifetime::calculateLocalLiveness() { 1765ffd83dbSDimitry Andric bool Changed = true; 1775ffd83dbSDimitry Andric while (Changed) { 1785ffd83dbSDimitry Andric Changed = false; 1795ffd83dbSDimitry Andric 1805ffd83dbSDimitry Andric for (const BasicBlock *BB : depth_first(&F)) { 1815ffd83dbSDimitry Andric BlockLifetimeInfo &BlockInfo = BlockLiveness.find(BB)->getSecond(); 1825ffd83dbSDimitry Andric 1835ffd83dbSDimitry Andric // Compute LiveIn by unioning together the LiveOut sets of all preds. 1845ffd83dbSDimitry Andric BitVector LocalLiveIn; 1855ffd83dbSDimitry Andric for (auto *PredBB : predecessors(BB)) { 1865ffd83dbSDimitry Andric LivenessMap::const_iterator I = BlockLiveness.find(PredBB); 1875ffd83dbSDimitry Andric // If a predecessor is unreachable, ignore it. 1885ffd83dbSDimitry Andric if (I == BlockLiveness.end()) 1895ffd83dbSDimitry Andric continue; 1905ffd83dbSDimitry Andric switch (Type) { 1915ffd83dbSDimitry Andric case LivenessType::May: 1925ffd83dbSDimitry Andric LocalLiveIn |= I->second.LiveOut; 1935ffd83dbSDimitry Andric break; 1945ffd83dbSDimitry Andric case LivenessType::Must: 1955ffd83dbSDimitry Andric if (LocalLiveIn.empty()) 1965ffd83dbSDimitry Andric LocalLiveIn = I->second.LiveOut; 1975ffd83dbSDimitry Andric else 1985ffd83dbSDimitry Andric LocalLiveIn &= I->second.LiveOut; 1995ffd83dbSDimitry Andric break; 2005ffd83dbSDimitry Andric } 2015ffd83dbSDimitry Andric } 2025ffd83dbSDimitry Andric 2035ffd83dbSDimitry Andric // Compute LiveOut by subtracting out lifetimes that end in this 2045ffd83dbSDimitry Andric // block, then adding in lifetimes that begin in this block. If 2055ffd83dbSDimitry Andric // we have both BEGIN and END markers in the same basic block 2065ffd83dbSDimitry Andric // then we know that the BEGIN marker comes after the END, 2075ffd83dbSDimitry Andric // because we already handle the case where the BEGIN comes 2085ffd83dbSDimitry Andric // before the END when collecting the markers (and building the 2095ffd83dbSDimitry Andric // BEGIN/END vectors). 2105ffd83dbSDimitry Andric BitVector LocalLiveOut = LocalLiveIn; 2115ffd83dbSDimitry Andric LocalLiveOut.reset(BlockInfo.End); 2125ffd83dbSDimitry Andric LocalLiveOut |= BlockInfo.Begin; 2135ffd83dbSDimitry Andric 2145ffd83dbSDimitry Andric // Update block LiveIn set, noting whether it has changed. 2155ffd83dbSDimitry Andric if (LocalLiveIn.test(BlockInfo.LiveIn)) { 2165ffd83dbSDimitry Andric BlockInfo.LiveIn |= LocalLiveIn; 2175ffd83dbSDimitry Andric } 2185ffd83dbSDimitry Andric 2195ffd83dbSDimitry Andric // Update block LiveOut set, noting whether it has changed. 2205ffd83dbSDimitry Andric if (LocalLiveOut.test(BlockInfo.LiveOut)) { 2215ffd83dbSDimitry Andric Changed = true; 2225ffd83dbSDimitry Andric BlockInfo.LiveOut |= LocalLiveOut; 2235ffd83dbSDimitry Andric } 2245ffd83dbSDimitry Andric } 2255ffd83dbSDimitry Andric } // while changed. 2265ffd83dbSDimitry Andric } 2275ffd83dbSDimitry Andric 2285ffd83dbSDimitry Andric void StackLifetime::calculateLiveIntervals() { 2295ffd83dbSDimitry Andric for (auto IT : BlockLiveness) { 2305ffd83dbSDimitry Andric const BasicBlock *BB = IT.getFirst(); 2315ffd83dbSDimitry Andric BlockLifetimeInfo &BlockInfo = IT.getSecond(); 2325ffd83dbSDimitry Andric unsigned BBStart, BBEnd; 2335ffd83dbSDimitry Andric std::tie(BBStart, BBEnd) = BlockInstRange[BB]; 2345ffd83dbSDimitry Andric 2355ffd83dbSDimitry Andric BitVector Started, Ended; 2365ffd83dbSDimitry Andric Started.resize(NumAllocas); 2375ffd83dbSDimitry Andric Ended.resize(NumAllocas); 2385ffd83dbSDimitry Andric SmallVector<unsigned, 8> Start; 2395ffd83dbSDimitry Andric Start.resize(NumAllocas); 2405ffd83dbSDimitry Andric 2415ffd83dbSDimitry Andric // LiveIn ranges start at the first instruction. 2425ffd83dbSDimitry Andric for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) { 2435ffd83dbSDimitry Andric if (BlockInfo.LiveIn.test(AllocaNo)) { 2445ffd83dbSDimitry Andric Started.set(AllocaNo); 2455ffd83dbSDimitry Andric Start[AllocaNo] = BBStart; 2465ffd83dbSDimitry Andric } 2475ffd83dbSDimitry Andric } 2485ffd83dbSDimitry Andric 2495ffd83dbSDimitry Andric for (auto &It : BBMarkers[BB]) { 2505ffd83dbSDimitry Andric unsigned InstNo = It.first; 2515ffd83dbSDimitry Andric bool IsStart = It.second.IsStart; 2525ffd83dbSDimitry Andric unsigned AllocaNo = It.second.AllocaNo; 2535ffd83dbSDimitry Andric 2545ffd83dbSDimitry Andric if (IsStart) { 2555ffd83dbSDimitry Andric if (!Started.test(AllocaNo)) { 2565ffd83dbSDimitry Andric Started.set(AllocaNo); 2575ffd83dbSDimitry Andric Ended.reset(AllocaNo); 2585ffd83dbSDimitry Andric Start[AllocaNo] = InstNo; 2595ffd83dbSDimitry Andric } 2605ffd83dbSDimitry Andric } else { 2615ffd83dbSDimitry Andric if (Started.test(AllocaNo)) { 2625ffd83dbSDimitry Andric LiveRanges[AllocaNo].addRange(Start[AllocaNo], InstNo); 2635ffd83dbSDimitry Andric Started.reset(AllocaNo); 2645ffd83dbSDimitry Andric } 2655ffd83dbSDimitry Andric Ended.set(AllocaNo); 2665ffd83dbSDimitry Andric } 2675ffd83dbSDimitry Andric } 2685ffd83dbSDimitry Andric 2695ffd83dbSDimitry Andric for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) 2705ffd83dbSDimitry Andric if (Started.test(AllocaNo)) 2715ffd83dbSDimitry Andric LiveRanges[AllocaNo].addRange(Start[AllocaNo], BBEnd); 2725ffd83dbSDimitry Andric } 2735ffd83dbSDimitry Andric } 2745ffd83dbSDimitry Andric 2755ffd83dbSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2765ffd83dbSDimitry Andric LLVM_DUMP_METHOD void StackLifetime::dumpAllocas() const { 2775ffd83dbSDimitry Andric dbgs() << "Allocas:\n"; 2785ffd83dbSDimitry Andric for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) 2795ffd83dbSDimitry Andric dbgs() << " " << AllocaNo << ": " << *Allocas[AllocaNo] << "\n"; 2805ffd83dbSDimitry Andric } 2815ffd83dbSDimitry Andric 2825ffd83dbSDimitry Andric LLVM_DUMP_METHOD void StackLifetime::dumpBlockLiveness() const { 2835ffd83dbSDimitry Andric dbgs() << "Block liveness:\n"; 2845ffd83dbSDimitry Andric for (auto IT : BlockLiveness) { 2855ffd83dbSDimitry Andric const BasicBlock *BB = IT.getFirst(); 2865ffd83dbSDimitry Andric const BlockLifetimeInfo &BlockInfo = BlockLiveness.find(BB)->getSecond(); 2875ffd83dbSDimitry Andric auto BlockRange = BlockInstRange.find(BB)->getSecond(); 288e8d8bef9SDimitry Andric dbgs() << " BB (" << BB->getName() << ") [" << BlockRange.first << ", " << BlockRange.second 2895ffd83dbSDimitry Andric << "): begin " << BlockInfo.Begin << ", end " << BlockInfo.End 2905ffd83dbSDimitry Andric << ", livein " << BlockInfo.LiveIn << ", liveout " 2915ffd83dbSDimitry Andric << BlockInfo.LiveOut << "\n"; 2925ffd83dbSDimitry Andric } 2935ffd83dbSDimitry Andric } 2945ffd83dbSDimitry Andric 2955ffd83dbSDimitry Andric LLVM_DUMP_METHOD void StackLifetime::dumpLiveRanges() const { 2965ffd83dbSDimitry Andric dbgs() << "Alloca liveness:\n"; 2975ffd83dbSDimitry Andric for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) 2985ffd83dbSDimitry Andric dbgs() << " " << AllocaNo << ": " << LiveRanges[AllocaNo] << "\n"; 2995ffd83dbSDimitry Andric } 3005ffd83dbSDimitry Andric #endif 3015ffd83dbSDimitry Andric 3025ffd83dbSDimitry Andric StackLifetime::StackLifetime(const Function &F, 3035ffd83dbSDimitry Andric ArrayRef<const AllocaInst *> Allocas, 3045ffd83dbSDimitry Andric LivenessType Type) 3055ffd83dbSDimitry Andric : F(F), Type(Type), Allocas(Allocas), NumAllocas(Allocas.size()) { 3065ffd83dbSDimitry Andric LLVM_DEBUG(dumpAllocas()); 3075ffd83dbSDimitry Andric 3085ffd83dbSDimitry Andric for (unsigned I = 0; I < NumAllocas; ++I) 3095ffd83dbSDimitry Andric AllocaNumbering[Allocas[I]] = I; 3105ffd83dbSDimitry Andric 3115ffd83dbSDimitry Andric collectMarkers(); 3125ffd83dbSDimitry Andric } 3135ffd83dbSDimitry Andric 3145ffd83dbSDimitry Andric void StackLifetime::run() { 315e8d8bef9SDimitry Andric if (HasUnknownLifetimeStartOrEnd) { 316e8d8bef9SDimitry Andric // There is marker which we can't assign to a specific alloca, so we 317e8d8bef9SDimitry Andric // fallback to the most conservative results for the type. 318e8d8bef9SDimitry Andric switch (Type) { 319e8d8bef9SDimitry Andric case LivenessType::May: 320e8d8bef9SDimitry Andric LiveRanges.resize(NumAllocas, getFullLiveRange()); 321e8d8bef9SDimitry Andric break; 322e8d8bef9SDimitry Andric case LivenessType::Must: 323e8d8bef9SDimitry Andric LiveRanges.resize(NumAllocas, LiveRange(Instructions.size())); 324e8d8bef9SDimitry Andric break; 325e8d8bef9SDimitry Andric } 326e8d8bef9SDimitry Andric return; 327e8d8bef9SDimitry Andric } 328e8d8bef9SDimitry Andric 3295ffd83dbSDimitry Andric LiveRanges.resize(NumAllocas, LiveRange(Instructions.size())); 3305ffd83dbSDimitry Andric for (unsigned I = 0; I < NumAllocas; ++I) 3315ffd83dbSDimitry Andric if (!InterestingAllocas.test(I)) 3325ffd83dbSDimitry Andric LiveRanges[I] = getFullLiveRange(); 3335ffd83dbSDimitry Andric 3345ffd83dbSDimitry Andric calculateLocalLiveness(); 3355ffd83dbSDimitry Andric LLVM_DEBUG(dumpBlockLiveness()); 3365ffd83dbSDimitry Andric calculateLiveIntervals(); 3375ffd83dbSDimitry Andric LLVM_DEBUG(dumpLiveRanges()); 3385ffd83dbSDimitry Andric } 3395ffd83dbSDimitry Andric 3405ffd83dbSDimitry Andric class StackLifetime::LifetimeAnnotationWriter 3415ffd83dbSDimitry Andric : public AssemblyAnnotationWriter { 3425ffd83dbSDimitry Andric const StackLifetime &SL; 3435ffd83dbSDimitry Andric 3445ffd83dbSDimitry Andric void printInstrAlive(unsigned InstrNo, formatted_raw_ostream &OS) { 3455ffd83dbSDimitry Andric SmallVector<StringRef, 16> Names; 3465ffd83dbSDimitry Andric for (const auto &KV : SL.AllocaNumbering) { 3475ffd83dbSDimitry Andric if (SL.LiveRanges[KV.getSecond()].test(InstrNo)) 3485ffd83dbSDimitry Andric Names.push_back(KV.getFirst()->getName()); 3495ffd83dbSDimitry Andric } 3505ffd83dbSDimitry Andric llvm::sort(Names); 3515ffd83dbSDimitry Andric OS << " ; Alive: <" << llvm::join(Names, " ") << ">\n"; 3525ffd83dbSDimitry Andric } 3535ffd83dbSDimitry Andric 3545ffd83dbSDimitry Andric void emitBasicBlockStartAnnot(const BasicBlock *BB, 3555ffd83dbSDimitry Andric formatted_raw_ostream &OS) override { 3565ffd83dbSDimitry Andric auto ItBB = SL.BlockInstRange.find(BB); 3575ffd83dbSDimitry Andric if (ItBB == SL.BlockInstRange.end()) 3585ffd83dbSDimitry Andric return; // Unreachable. 3595ffd83dbSDimitry Andric printInstrAlive(ItBB->getSecond().first, OS); 3605ffd83dbSDimitry Andric } 3615ffd83dbSDimitry Andric 3625ffd83dbSDimitry Andric void printInfoComment(const Value &V, formatted_raw_ostream &OS) override { 3635ffd83dbSDimitry Andric const Instruction *Instr = dyn_cast<Instruction>(&V); 3645ffd83dbSDimitry Andric if (!Instr || !SL.isReachable(Instr)) 3655ffd83dbSDimitry Andric return; 3665ffd83dbSDimitry Andric 3675ffd83dbSDimitry Andric SmallVector<StringRef, 16> Names; 3685ffd83dbSDimitry Andric for (const auto &KV : SL.AllocaNumbering) { 3695ffd83dbSDimitry Andric if (SL.isAliveAfter(KV.getFirst(), Instr)) 3705ffd83dbSDimitry Andric Names.push_back(KV.getFirst()->getName()); 3715ffd83dbSDimitry Andric } 3725ffd83dbSDimitry Andric llvm::sort(Names); 3735ffd83dbSDimitry Andric OS << "\n ; Alive: <" << llvm::join(Names, " ") << ">\n"; 3745ffd83dbSDimitry Andric } 3755ffd83dbSDimitry Andric 3765ffd83dbSDimitry Andric public: 3775ffd83dbSDimitry Andric LifetimeAnnotationWriter(const StackLifetime &SL) : SL(SL) {} 3785ffd83dbSDimitry Andric }; 3795ffd83dbSDimitry Andric 3805ffd83dbSDimitry Andric void StackLifetime::print(raw_ostream &OS) { 3815ffd83dbSDimitry Andric LifetimeAnnotationWriter AAW(*this); 3825ffd83dbSDimitry Andric F.print(OS, &AAW); 3835ffd83dbSDimitry Andric } 3845ffd83dbSDimitry Andric 3855ffd83dbSDimitry Andric PreservedAnalyses StackLifetimePrinterPass::run(Function &F, 3865ffd83dbSDimitry Andric FunctionAnalysisManager &AM) { 3875ffd83dbSDimitry Andric SmallVector<const AllocaInst *, 8> Allocas; 3885ffd83dbSDimitry Andric for (auto &I : instructions(F)) 3895ffd83dbSDimitry Andric if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) 3905ffd83dbSDimitry Andric Allocas.push_back(AI); 3915ffd83dbSDimitry Andric StackLifetime SL(F, Allocas, Type); 3925ffd83dbSDimitry Andric SL.run(); 3935ffd83dbSDimitry Andric SL.print(OS); 3945ffd83dbSDimitry Andric return PreservedAnalyses::all(); 3955ffd83dbSDimitry Andric } 396349cc55cSDimitry Andric 397349cc55cSDimitry Andric void StackLifetimePrinterPass::printPipeline( 398349cc55cSDimitry Andric raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) { 399349cc55cSDimitry Andric static_cast<PassInfoMixin<StackLifetimePrinterPass> *>(this)->printPipeline( 400349cc55cSDimitry Andric OS, MapClassName2PassName); 401349cc55cSDimitry Andric OS << "<"; 402349cc55cSDimitry Andric switch (Type) { 403349cc55cSDimitry Andric case StackLifetime::LivenessType::May: 404349cc55cSDimitry Andric OS << "may"; 405349cc55cSDimitry Andric break; 406349cc55cSDimitry Andric case StackLifetime::LivenessType::Must: 407349cc55cSDimitry Andric OS << "must"; 408349cc55cSDimitry Andric break; 409349cc55cSDimitry Andric } 410349cc55cSDimitry Andric OS << ">"; 411349cc55cSDimitry Andric } 412