1097a140dSpatrick //===- StackLifetime.cpp - Alloca Lifetime Analysis -----------------------===//
2097a140dSpatrick //
3097a140dSpatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4097a140dSpatrick // See https://llvm.org/LICENSE.txt for license information.
5097a140dSpatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6097a140dSpatrick //
7097a140dSpatrick //===----------------------------------------------------------------------===//
8097a140dSpatrick
9097a140dSpatrick #include "llvm/Analysis/StackLifetime.h"
10097a140dSpatrick #include "llvm/ADT/DepthFirstIterator.h"
11097a140dSpatrick #include "llvm/ADT/STLExtras.h"
12097a140dSpatrick #include "llvm/ADT/SmallVector.h"
13097a140dSpatrick #include "llvm/ADT/StringExtras.h"
1473471bf0Spatrick #include "llvm/Analysis/ValueTracking.h"
15097a140dSpatrick #include "llvm/Config/llvm-config.h"
16097a140dSpatrick #include "llvm/IR/AssemblyAnnotationWriter.h"
17097a140dSpatrick #include "llvm/IR/BasicBlock.h"
18097a140dSpatrick #include "llvm/IR/CFG.h"
19097a140dSpatrick #include "llvm/IR/InstIterator.h"
20097a140dSpatrick #include "llvm/IR/Instructions.h"
21097a140dSpatrick #include "llvm/IR/IntrinsicInst.h"
22097a140dSpatrick #include "llvm/IR/Value.h"
23097a140dSpatrick #include "llvm/Support/Casting.h"
24097a140dSpatrick #include "llvm/Support/Compiler.h"
25097a140dSpatrick #include "llvm/Support/Debug.h"
26097a140dSpatrick #include "llvm/Support/FormattedStream.h"
27097a140dSpatrick #include <algorithm>
28097a140dSpatrick #include <tuple>
29097a140dSpatrick
30097a140dSpatrick using namespace llvm;
31097a140dSpatrick
32097a140dSpatrick #define DEBUG_TYPE "stack-lifetime"
33097a140dSpatrick
34097a140dSpatrick const StackLifetime::LiveRange &
getLiveRange(const AllocaInst * AI) const35097a140dSpatrick StackLifetime::getLiveRange(const AllocaInst *AI) const {
36097a140dSpatrick const auto IT = AllocaNumbering.find(AI);
37097a140dSpatrick assert(IT != AllocaNumbering.end());
38097a140dSpatrick return LiveRanges[IT->second];
39097a140dSpatrick }
40097a140dSpatrick
isReachable(const Instruction * I) const41097a140dSpatrick bool StackLifetime::isReachable(const Instruction *I) const {
42097a140dSpatrick return BlockInstRange.find(I->getParent()) != BlockInstRange.end();
43097a140dSpatrick }
44097a140dSpatrick
isAliveAfter(const AllocaInst * AI,const Instruction * I) const45097a140dSpatrick bool StackLifetime::isAliveAfter(const AllocaInst *AI,
46097a140dSpatrick const Instruction *I) const {
47097a140dSpatrick const BasicBlock *BB = I->getParent();
48097a140dSpatrick auto ItBB = BlockInstRange.find(BB);
49097a140dSpatrick assert(ItBB != BlockInstRange.end() && "Unreachable is not expected");
50097a140dSpatrick
51097a140dSpatrick // Search the block for the first instruction following 'I'.
52097a140dSpatrick auto It = std::upper_bound(Instructions.begin() + ItBB->getSecond().first + 1,
53097a140dSpatrick Instructions.begin() + ItBB->getSecond().second, I,
54097a140dSpatrick [](const Instruction *L, const Instruction *R) {
55097a140dSpatrick return L->comesBefore(R);
56097a140dSpatrick });
57097a140dSpatrick --It;
58097a140dSpatrick unsigned InstNum = It - Instructions.begin();
59097a140dSpatrick return getLiveRange(AI).test(InstNum);
60097a140dSpatrick }
61097a140dSpatrick
6273471bf0Spatrick // Returns unique alloca annotated by lifetime marker only if
6373471bf0Spatrick // markers has the same size and points to the alloca start.
findMatchingAlloca(const IntrinsicInst & II,const DataLayout & DL)6473471bf0Spatrick static const AllocaInst *findMatchingAlloca(const IntrinsicInst &II,
6573471bf0Spatrick const DataLayout &DL) {
6673471bf0Spatrick const AllocaInst *AI = findAllocaForValue(II.getArgOperand(1), true);
6773471bf0Spatrick if (!AI)
6873471bf0Spatrick return nullptr;
69097a140dSpatrick
70*d415bd75Srobert auto AllocaSize = AI->getAllocationSize(DL);
71*d415bd75Srobert if (!AllocaSize)
7273471bf0Spatrick return nullptr;
7373471bf0Spatrick
7473471bf0Spatrick auto *Size = dyn_cast<ConstantInt>(II.getArgOperand(0));
7573471bf0Spatrick if (!Size)
7673471bf0Spatrick return nullptr;
7773471bf0Spatrick int64_t LifetimeSize = Size->getSExtValue();
7873471bf0Spatrick
79*d415bd75Srobert if (LifetimeSize != -1 && uint64_t(LifetimeSize) != *AllocaSize)
8073471bf0Spatrick return nullptr;
8173471bf0Spatrick
8273471bf0Spatrick return AI;
83097a140dSpatrick }
84097a140dSpatrick
collectMarkers()85097a140dSpatrick void StackLifetime::collectMarkers() {
86097a140dSpatrick InterestingAllocas.resize(NumAllocas);
87097a140dSpatrick DenseMap<const BasicBlock *, SmallDenseMap<const IntrinsicInst *, Marker>>
88097a140dSpatrick BBMarkerSet;
89097a140dSpatrick
9073471bf0Spatrick const DataLayout &DL = F.getParent()->getDataLayout();
9173471bf0Spatrick
92097a140dSpatrick // Compute the set of start/end markers per basic block.
9373471bf0Spatrick for (const BasicBlock *BB : depth_first(&F)) {
9473471bf0Spatrick for (const Instruction &I : *BB) {
9573471bf0Spatrick const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
9673471bf0Spatrick if (!II || !II->isLifetimeStartOrEnd())
9773471bf0Spatrick continue;
9873471bf0Spatrick const AllocaInst *AI = findMatchingAlloca(*II, DL);
9973471bf0Spatrick if (!AI) {
10073471bf0Spatrick HasUnknownLifetimeStartOrEnd = true;
101097a140dSpatrick continue;
102097a140dSpatrick }
10373471bf0Spatrick auto It = AllocaNumbering.find(AI);
10473471bf0Spatrick if (It == AllocaNumbering.end())
105097a140dSpatrick continue;
10673471bf0Spatrick auto AllocaNo = It->second;
10773471bf0Spatrick bool IsStart = II->getIntrinsicID() == Intrinsic::lifetime_start;
108097a140dSpatrick if (IsStart)
109097a140dSpatrick InterestingAllocas.set(AllocaNo);
11073471bf0Spatrick BBMarkerSet[BB][II] = {AllocaNo, IsStart};
111097a140dSpatrick }
112097a140dSpatrick }
113097a140dSpatrick
114097a140dSpatrick // Compute instruction numbering. Only the following instructions are
115097a140dSpatrick // considered:
116097a140dSpatrick // * Basic block entries
117097a140dSpatrick // * Lifetime markers
118097a140dSpatrick // For each basic block, compute
119097a140dSpatrick // * the list of markers in the instruction order
120097a140dSpatrick // * the sets of allocas whose lifetime starts or ends in this BB
121097a140dSpatrick LLVM_DEBUG(dbgs() << "Instructions:\n");
122097a140dSpatrick for (const BasicBlock *BB : depth_first(&F)) {
123097a140dSpatrick LLVM_DEBUG(dbgs() << " " << Instructions.size() << ": BB " << BB->getName()
124097a140dSpatrick << "\n");
125097a140dSpatrick auto BBStart = Instructions.size();
126097a140dSpatrick Instructions.push_back(nullptr);
127097a140dSpatrick
128097a140dSpatrick BlockLifetimeInfo &BlockInfo =
129097a140dSpatrick BlockLiveness.try_emplace(BB, NumAllocas).first->getSecond();
130097a140dSpatrick
131097a140dSpatrick auto &BlockMarkerSet = BBMarkerSet[BB];
132097a140dSpatrick if (BlockMarkerSet.empty()) {
133097a140dSpatrick BlockInstRange[BB] = std::make_pair(BBStart, Instructions.size());
134097a140dSpatrick continue;
135097a140dSpatrick }
136097a140dSpatrick
137097a140dSpatrick auto ProcessMarker = [&](const IntrinsicInst *I, const Marker &M) {
138097a140dSpatrick LLVM_DEBUG(dbgs() << " " << Instructions.size() << ": "
139097a140dSpatrick << (M.IsStart ? "start " : "end ") << M.AllocaNo
140097a140dSpatrick << ", " << *I << "\n");
141097a140dSpatrick
142097a140dSpatrick BBMarkers[BB].push_back({Instructions.size(), M});
143097a140dSpatrick Instructions.push_back(I);
144097a140dSpatrick
145097a140dSpatrick if (M.IsStart) {
146097a140dSpatrick BlockInfo.End.reset(M.AllocaNo);
147097a140dSpatrick BlockInfo.Begin.set(M.AllocaNo);
148097a140dSpatrick } else {
149097a140dSpatrick BlockInfo.Begin.reset(M.AllocaNo);
150097a140dSpatrick BlockInfo.End.set(M.AllocaNo);
151097a140dSpatrick }
152097a140dSpatrick };
153097a140dSpatrick
154097a140dSpatrick if (BlockMarkerSet.size() == 1) {
155097a140dSpatrick ProcessMarker(BlockMarkerSet.begin()->getFirst(),
156097a140dSpatrick BlockMarkerSet.begin()->getSecond());
157097a140dSpatrick } else {
158097a140dSpatrick // Scan the BB to determine the marker order.
159097a140dSpatrick for (const Instruction &I : *BB) {
160097a140dSpatrick const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
161097a140dSpatrick if (!II)
162097a140dSpatrick continue;
163097a140dSpatrick auto It = BlockMarkerSet.find(II);
164097a140dSpatrick if (It == BlockMarkerSet.end())
165097a140dSpatrick continue;
166097a140dSpatrick ProcessMarker(II, It->getSecond());
167097a140dSpatrick }
168097a140dSpatrick }
169097a140dSpatrick
170097a140dSpatrick BlockInstRange[BB] = std::make_pair(BBStart, Instructions.size());
171097a140dSpatrick }
172097a140dSpatrick }
173097a140dSpatrick
calculateLocalLiveness()174097a140dSpatrick void StackLifetime::calculateLocalLiveness() {
175097a140dSpatrick bool Changed = true;
176*d415bd75Srobert
177*d415bd75Srobert // LiveIn, LiveOut and BitsIn have a different meaning deppends on type.
178*d415bd75Srobert // ::Maybe true bits represent "may be alive" allocas, ::Must true bits
179*d415bd75Srobert // represent "may be dead". After the loop we will convert ::Must bits from
180*d415bd75Srobert // "may be dead" to "must be alive".
181097a140dSpatrick while (Changed) {
182*d415bd75Srobert // TODO: Consider switching to worklist instead of traversing entire graph.
183097a140dSpatrick Changed = false;
184097a140dSpatrick
185097a140dSpatrick for (const BasicBlock *BB : depth_first(&F)) {
186097a140dSpatrick BlockLifetimeInfo &BlockInfo = BlockLiveness.find(BB)->getSecond();
187097a140dSpatrick
188*d415bd75Srobert // Compute BitsIn by unioning together the LiveOut sets of all preds.
189*d415bd75Srobert BitVector BitsIn;
190*d415bd75Srobert for (const auto *PredBB : predecessors(BB)) {
191097a140dSpatrick LivenessMap::const_iterator I = BlockLiveness.find(PredBB);
192097a140dSpatrick // If a predecessor is unreachable, ignore it.
193097a140dSpatrick if (I == BlockLiveness.end())
194097a140dSpatrick continue;
195*d415bd75Srobert BitsIn |= I->second.LiveOut;
196097a140dSpatrick }
197*d415bd75Srobert
198*d415bd75Srobert // Everything is "may be dead" for entry without predecessors.
199*d415bd75Srobert if (Type == LivenessType::Must && BitsIn.empty())
200*d415bd75Srobert BitsIn.resize(NumAllocas, true);
201*d415bd75Srobert
202*d415bd75Srobert // Update block LiveIn set, noting whether it has changed.
203*d415bd75Srobert if (BitsIn.test(BlockInfo.LiveIn)) {
204*d415bd75Srobert BlockInfo.LiveIn |= BitsIn;
205097a140dSpatrick }
206097a140dSpatrick
207097a140dSpatrick // Compute LiveOut by subtracting out lifetimes that end in this
208097a140dSpatrick // block, then adding in lifetimes that begin in this block. If
209097a140dSpatrick // we have both BEGIN and END markers in the same basic block
210097a140dSpatrick // then we know that the BEGIN marker comes after the END,
211097a140dSpatrick // because we already handle the case where the BEGIN comes
212097a140dSpatrick // before the END when collecting the markers (and building the
213097a140dSpatrick // BEGIN/END vectors).
214*d415bd75Srobert switch (Type) {
215*d415bd75Srobert case LivenessType::May:
216*d415bd75Srobert BitsIn.reset(BlockInfo.End);
217*d415bd75Srobert // "may be alive" is set by lifetime start.
218*d415bd75Srobert BitsIn |= BlockInfo.Begin;
219*d415bd75Srobert break;
220*d415bd75Srobert case LivenessType::Must:
221*d415bd75Srobert BitsIn.reset(BlockInfo.Begin);
222*d415bd75Srobert // "may be dead" is set by lifetime end.
223*d415bd75Srobert BitsIn |= BlockInfo.End;
224*d415bd75Srobert break;
225097a140dSpatrick }
226097a140dSpatrick
227097a140dSpatrick // Update block LiveOut set, noting whether it has changed.
228*d415bd75Srobert if (BitsIn.test(BlockInfo.LiveOut)) {
229097a140dSpatrick Changed = true;
230*d415bd75Srobert BlockInfo.LiveOut |= BitsIn;
231097a140dSpatrick }
232097a140dSpatrick }
233097a140dSpatrick } // while changed.
234*d415bd75Srobert
235*d415bd75Srobert if (Type == LivenessType::Must) {
236*d415bd75Srobert // Convert from "may be dead" to "must be alive".
237*d415bd75Srobert for (auto &[BB, BlockInfo] : BlockLiveness) {
238*d415bd75Srobert BlockInfo.LiveIn.flip();
239*d415bd75Srobert BlockInfo.LiveOut.flip();
240*d415bd75Srobert }
241*d415bd75Srobert }
242097a140dSpatrick }
243097a140dSpatrick
calculateLiveIntervals()244097a140dSpatrick void StackLifetime::calculateLiveIntervals() {
245097a140dSpatrick for (auto IT : BlockLiveness) {
246097a140dSpatrick const BasicBlock *BB = IT.getFirst();
247097a140dSpatrick BlockLifetimeInfo &BlockInfo = IT.getSecond();
248097a140dSpatrick unsigned BBStart, BBEnd;
249097a140dSpatrick std::tie(BBStart, BBEnd) = BlockInstRange[BB];
250097a140dSpatrick
251097a140dSpatrick BitVector Started, Ended;
252097a140dSpatrick Started.resize(NumAllocas);
253097a140dSpatrick Ended.resize(NumAllocas);
254097a140dSpatrick SmallVector<unsigned, 8> Start;
255097a140dSpatrick Start.resize(NumAllocas);
256097a140dSpatrick
257097a140dSpatrick // LiveIn ranges start at the first instruction.
258097a140dSpatrick for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
259097a140dSpatrick if (BlockInfo.LiveIn.test(AllocaNo)) {
260097a140dSpatrick Started.set(AllocaNo);
261097a140dSpatrick Start[AllocaNo] = BBStart;
262097a140dSpatrick }
263097a140dSpatrick }
264097a140dSpatrick
265097a140dSpatrick for (auto &It : BBMarkers[BB]) {
266097a140dSpatrick unsigned InstNo = It.first;
267097a140dSpatrick bool IsStart = It.second.IsStart;
268097a140dSpatrick unsigned AllocaNo = It.second.AllocaNo;
269097a140dSpatrick
270097a140dSpatrick if (IsStart) {
271097a140dSpatrick if (!Started.test(AllocaNo)) {
272097a140dSpatrick Started.set(AllocaNo);
273097a140dSpatrick Ended.reset(AllocaNo);
274097a140dSpatrick Start[AllocaNo] = InstNo;
275097a140dSpatrick }
276097a140dSpatrick } else {
277097a140dSpatrick if (Started.test(AllocaNo)) {
278097a140dSpatrick LiveRanges[AllocaNo].addRange(Start[AllocaNo], InstNo);
279097a140dSpatrick Started.reset(AllocaNo);
280097a140dSpatrick }
281097a140dSpatrick Ended.set(AllocaNo);
282097a140dSpatrick }
283097a140dSpatrick }
284097a140dSpatrick
285097a140dSpatrick for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
286097a140dSpatrick if (Started.test(AllocaNo))
287097a140dSpatrick LiveRanges[AllocaNo].addRange(Start[AllocaNo], BBEnd);
288097a140dSpatrick }
289097a140dSpatrick }
290097a140dSpatrick
291097a140dSpatrick #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dumpAllocas() const292097a140dSpatrick LLVM_DUMP_METHOD void StackLifetime::dumpAllocas() const {
293097a140dSpatrick dbgs() << "Allocas:\n";
294097a140dSpatrick for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
295097a140dSpatrick dbgs() << " " << AllocaNo << ": " << *Allocas[AllocaNo] << "\n";
296097a140dSpatrick }
297097a140dSpatrick
dumpBlockLiveness() const298097a140dSpatrick LLVM_DUMP_METHOD void StackLifetime::dumpBlockLiveness() const {
299097a140dSpatrick dbgs() << "Block liveness:\n";
300097a140dSpatrick for (auto IT : BlockLiveness) {
301097a140dSpatrick const BasicBlock *BB = IT.getFirst();
302097a140dSpatrick const BlockLifetimeInfo &BlockInfo = BlockLiveness.find(BB)->getSecond();
303097a140dSpatrick auto BlockRange = BlockInstRange.find(BB)->getSecond();
30473471bf0Spatrick dbgs() << " BB (" << BB->getName() << ") [" << BlockRange.first << ", " << BlockRange.second
305097a140dSpatrick << "): begin " << BlockInfo.Begin << ", end " << BlockInfo.End
306097a140dSpatrick << ", livein " << BlockInfo.LiveIn << ", liveout "
307097a140dSpatrick << BlockInfo.LiveOut << "\n";
308097a140dSpatrick }
309097a140dSpatrick }
310097a140dSpatrick
dumpLiveRanges() const311097a140dSpatrick LLVM_DUMP_METHOD void StackLifetime::dumpLiveRanges() const {
312097a140dSpatrick dbgs() << "Alloca liveness:\n";
313097a140dSpatrick for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
314097a140dSpatrick dbgs() << " " << AllocaNo << ": " << LiveRanges[AllocaNo] << "\n";
315097a140dSpatrick }
316097a140dSpatrick #endif
317097a140dSpatrick
StackLifetime(const Function & F,ArrayRef<const AllocaInst * > Allocas,LivenessType Type)318097a140dSpatrick StackLifetime::StackLifetime(const Function &F,
319097a140dSpatrick ArrayRef<const AllocaInst *> Allocas,
320097a140dSpatrick LivenessType Type)
321097a140dSpatrick : F(F), Type(Type), Allocas(Allocas), NumAllocas(Allocas.size()) {
322097a140dSpatrick LLVM_DEBUG(dumpAllocas());
323097a140dSpatrick
324097a140dSpatrick for (unsigned I = 0; I < NumAllocas; ++I)
325097a140dSpatrick AllocaNumbering[Allocas[I]] = I;
326097a140dSpatrick
327097a140dSpatrick collectMarkers();
328097a140dSpatrick }
329097a140dSpatrick
run()330097a140dSpatrick void StackLifetime::run() {
33173471bf0Spatrick if (HasUnknownLifetimeStartOrEnd) {
33273471bf0Spatrick // There is marker which we can't assign to a specific alloca, so we
33373471bf0Spatrick // fallback to the most conservative results for the type.
33473471bf0Spatrick switch (Type) {
33573471bf0Spatrick case LivenessType::May:
33673471bf0Spatrick LiveRanges.resize(NumAllocas, getFullLiveRange());
33773471bf0Spatrick break;
33873471bf0Spatrick case LivenessType::Must:
33973471bf0Spatrick LiveRanges.resize(NumAllocas, LiveRange(Instructions.size()));
34073471bf0Spatrick break;
34173471bf0Spatrick }
34273471bf0Spatrick return;
34373471bf0Spatrick }
34473471bf0Spatrick
345097a140dSpatrick LiveRanges.resize(NumAllocas, LiveRange(Instructions.size()));
346097a140dSpatrick for (unsigned I = 0; I < NumAllocas; ++I)
347097a140dSpatrick if (!InterestingAllocas.test(I))
348097a140dSpatrick LiveRanges[I] = getFullLiveRange();
349097a140dSpatrick
350097a140dSpatrick calculateLocalLiveness();
351097a140dSpatrick LLVM_DEBUG(dumpBlockLiveness());
352097a140dSpatrick calculateLiveIntervals();
353097a140dSpatrick LLVM_DEBUG(dumpLiveRanges());
354097a140dSpatrick }
355097a140dSpatrick
356097a140dSpatrick class StackLifetime::LifetimeAnnotationWriter
357097a140dSpatrick : public AssemblyAnnotationWriter {
358097a140dSpatrick const StackLifetime &SL;
359097a140dSpatrick
printInstrAlive(unsigned InstrNo,formatted_raw_ostream & OS)360097a140dSpatrick void printInstrAlive(unsigned InstrNo, formatted_raw_ostream &OS) {
361097a140dSpatrick SmallVector<StringRef, 16> Names;
362097a140dSpatrick for (const auto &KV : SL.AllocaNumbering) {
363097a140dSpatrick if (SL.LiveRanges[KV.getSecond()].test(InstrNo))
364097a140dSpatrick Names.push_back(KV.getFirst()->getName());
365097a140dSpatrick }
366097a140dSpatrick llvm::sort(Names);
367097a140dSpatrick OS << " ; Alive: <" << llvm::join(Names, " ") << ">\n";
368097a140dSpatrick }
369097a140dSpatrick
emitBasicBlockStartAnnot(const BasicBlock * BB,formatted_raw_ostream & OS)370097a140dSpatrick void emitBasicBlockStartAnnot(const BasicBlock *BB,
371097a140dSpatrick formatted_raw_ostream &OS) override {
372097a140dSpatrick auto ItBB = SL.BlockInstRange.find(BB);
373097a140dSpatrick if (ItBB == SL.BlockInstRange.end())
374097a140dSpatrick return; // Unreachable.
375097a140dSpatrick printInstrAlive(ItBB->getSecond().first, OS);
376097a140dSpatrick }
377097a140dSpatrick
printInfoComment(const Value & V,formatted_raw_ostream & OS)378097a140dSpatrick void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
379097a140dSpatrick const Instruction *Instr = dyn_cast<Instruction>(&V);
380097a140dSpatrick if (!Instr || !SL.isReachable(Instr))
381097a140dSpatrick return;
382097a140dSpatrick
383097a140dSpatrick SmallVector<StringRef, 16> Names;
384097a140dSpatrick for (const auto &KV : SL.AllocaNumbering) {
385097a140dSpatrick if (SL.isAliveAfter(KV.getFirst(), Instr))
386097a140dSpatrick Names.push_back(KV.getFirst()->getName());
387097a140dSpatrick }
388097a140dSpatrick llvm::sort(Names);
389097a140dSpatrick OS << "\n ; Alive: <" << llvm::join(Names, " ") << ">\n";
390097a140dSpatrick }
391097a140dSpatrick
392097a140dSpatrick public:
LifetimeAnnotationWriter(const StackLifetime & SL)393097a140dSpatrick LifetimeAnnotationWriter(const StackLifetime &SL) : SL(SL) {}
394097a140dSpatrick };
395097a140dSpatrick
print(raw_ostream & OS)396097a140dSpatrick void StackLifetime::print(raw_ostream &OS) {
397097a140dSpatrick LifetimeAnnotationWriter AAW(*this);
398097a140dSpatrick F.print(OS, &AAW);
399097a140dSpatrick }
400097a140dSpatrick
run(Function & F,FunctionAnalysisManager & AM)401097a140dSpatrick PreservedAnalyses StackLifetimePrinterPass::run(Function &F,
402097a140dSpatrick FunctionAnalysisManager &AM) {
403097a140dSpatrick SmallVector<const AllocaInst *, 8> Allocas;
404097a140dSpatrick for (auto &I : instructions(F))
405097a140dSpatrick if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I))
406097a140dSpatrick Allocas.push_back(AI);
407097a140dSpatrick StackLifetime SL(F, Allocas, Type);
408097a140dSpatrick SL.run();
409097a140dSpatrick SL.print(OS);
410097a140dSpatrick return PreservedAnalyses::all();
411097a140dSpatrick }
412*d415bd75Srobert
printPipeline(raw_ostream & OS,function_ref<StringRef (StringRef)> MapClassName2PassName)413*d415bd75Srobert void StackLifetimePrinterPass::printPipeline(
414*d415bd75Srobert raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
415*d415bd75Srobert static_cast<PassInfoMixin<StackLifetimePrinterPass> *>(this)->printPipeline(
416*d415bd75Srobert OS, MapClassName2PassName);
417*d415bd75Srobert OS << "<";
418*d415bd75Srobert switch (Type) {
419*d415bd75Srobert case StackLifetime::LivenessType::May:
420*d415bd75Srobert OS << "may";
421*d415bd75Srobert break;
422*d415bd75Srobert case StackLifetime::LivenessType::Must:
423*d415bd75Srobert OS << "must";
424*d415bd75Srobert break;
425*d415bd75Srobert }
426*d415bd75Srobert OS << ">";
427*d415bd75Srobert }
428