2020-06-15 17:37:19 +08:00
|
|
|
//===- StackLifetime.cpp - Alloca Lifetime Analysis -----------------------===//
|
2016-06-30 04:37:43 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-06-30 04:37:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-06-15 17:26:28 +08:00
|
|
|
#include "llvm/Analysis/StackLifetime.h"
|
2016-06-30 04:37:43 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2018-04-30 22:59:11 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2017-10-11 06:33:29 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2016-06-30 04:37:43 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2017-10-11 06:33:29 +08:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/User.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2016-06-30 04:37:43 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2017-10-11 06:33:29 +08:00
|
|
|
#include <tuple>
|
2016-06-30 04:37:43 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2020-06-15 17:37:19 +08:00
|
|
|
#define DEBUG_TYPE "stack-lifetime"
|
2016-06-30 04:37:43 +08:00
|
|
|
|
2020-06-15 17:37:19 +08:00
|
|
|
const StackLifetime::LiveRange &
|
|
|
|
StackLifetime::getLiveRange(const AllocaInst *AI) const {
|
2016-07-26 08:05:14 +08:00
|
|
|
const auto IT = AllocaNumbering.find(AI);
|
|
|
|
assert(IT != AllocaNumbering.end());
|
|
|
|
return LiveRanges[IT->second];
|
2016-06-30 04:37:43 +08:00
|
|
|
}
|
|
|
|
|
2020-06-15 07:31:09 +08:00
|
|
|
static bool readMarker(const Instruction *I, bool *IsStart) {
|
2018-12-22 05:49:40 +08:00
|
|
|
if (!I->isLifetimeStartOrEnd())
|
2016-06-30 04:37:43 +08:00
|
|
|
return false;
|
|
|
|
|
2018-12-22 05:49:40 +08:00
|
|
|
auto *II = cast<IntrinsicInst>(I);
|
2016-06-30 04:37:43 +08:00
|
|
|
*IsStart = II->getIntrinsicID() == Intrinsic::lifetime_start;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:37:19 +08:00
|
|
|
std::vector<const IntrinsicInst *> StackLifetime::getMarkers() const {
|
2020-06-15 14:03:13 +08:00
|
|
|
std::vector<const IntrinsicInst *> Markers;
|
|
|
|
for (auto &M : InstructionNumbering)
|
|
|
|
if (M.getFirst()->isLifetimeStartOrEnd())
|
|
|
|
Markers.push_back(M.getFirst());
|
|
|
|
return Markers;
|
2016-06-30 04:37:43 +08:00
|
|
|
}
|
|
|
|
|
2020-06-15 17:37:19 +08:00
|
|
|
void StackLifetime::collectMarkers() {
|
2016-06-30 04:37:43 +08:00
|
|
|
InterestingAllocas.resize(NumAllocas);
|
2020-06-15 07:31:09 +08:00
|
|
|
DenseMap<const BasicBlock *, SmallDenseMap<const IntrinsicInst *, Marker>>
|
|
|
|
BBMarkerSet;
|
2016-06-30 04:37:43 +08:00
|
|
|
|
|
|
|
// Compute the set of start/end markers per basic block.
|
|
|
|
for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
|
2020-06-15 07:31:09 +08:00
|
|
|
const AllocaInst *AI = Allocas[AllocaNo];
|
|
|
|
SmallVector<const Instruction *, 8> WorkList;
|
2016-06-30 04:37:43 +08:00
|
|
|
WorkList.push_back(AI);
|
|
|
|
while (!WorkList.empty()) {
|
2020-06-15 07:31:09 +08:00
|
|
|
const Instruction *I = WorkList.pop_back_val();
|
|
|
|
for (const User *U : I->users()) {
|
2016-06-30 04:37:43 +08:00
|
|
|
if (auto *BI = dyn_cast<BitCastInst>(U)) {
|
|
|
|
WorkList.push_back(BI);
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-15 06:58:10 +08:00
|
|
|
auto *UI = dyn_cast<IntrinsicInst>(U);
|
2016-06-30 04:37:43 +08:00
|
|
|
if (!UI)
|
|
|
|
continue;
|
|
|
|
bool IsStart;
|
|
|
|
if (!readMarker(UI, &IsStart))
|
|
|
|
continue;
|
|
|
|
if (IsStart)
|
|
|
|
InterestingAllocas.set(AllocaNo);
|
|
|
|
BBMarkerSet[UI->getParent()][UI] = {AllocaNo, IsStart};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute instruction numbering. Only the following instructions are
|
|
|
|
// considered:
|
|
|
|
// * Basic block entries
|
|
|
|
// * Lifetime markers
|
|
|
|
// For each basic block, compute
|
|
|
|
// * the list of markers in the instruction order
|
|
|
|
// * the sets of allocas whose lifetime starts or ends in this BB
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Instructions:\n");
|
2016-06-30 04:37:43 +08:00
|
|
|
unsigned InstNo = 0;
|
2020-06-15 07:31:09 +08:00
|
|
|
for (const BasicBlock *BB : depth_first(&F)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " " << InstNo << ": BB " << BB->getName() << "\n");
|
2016-06-30 04:37:43 +08:00
|
|
|
unsigned BBStart = InstNo++;
|
|
|
|
|
2020-06-15 07:06:07 +08:00
|
|
|
BlockLifetimeInfo &BlockInfo =
|
|
|
|
BlockLiveness.try_emplace(BB, NumAllocas).first->getSecond();
|
2016-06-30 04:37:43 +08:00
|
|
|
|
|
|
|
auto &BlockMarkerSet = BBMarkerSet[BB];
|
|
|
|
if (BlockMarkerSet.empty()) {
|
|
|
|
unsigned BBEnd = InstNo;
|
|
|
|
BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-15 07:31:09 +08:00
|
|
|
auto ProcessMarker = [&](const IntrinsicInst *I, const Marker &M) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " " << InstNo << ": "
|
|
|
|
<< (M.IsStart ? "start " : "end ") << M.AllocaNo
|
|
|
|
<< ", " << *I << "\n");
|
2016-06-30 04:37:43 +08:00
|
|
|
|
|
|
|
BBMarkers[BB].push_back({InstNo, M});
|
|
|
|
|
|
|
|
InstructionNumbering[I] = InstNo++;
|
|
|
|
|
|
|
|
if (M.IsStart) {
|
2020-06-15 12:28:34 +08:00
|
|
|
BlockInfo.End.reset(M.AllocaNo);
|
2016-06-30 04:37:43 +08:00
|
|
|
BlockInfo.Begin.set(M.AllocaNo);
|
|
|
|
} else {
|
2020-06-15 12:28:34 +08:00
|
|
|
BlockInfo.Begin.reset(M.AllocaNo);
|
2016-06-30 04:37:43 +08:00
|
|
|
BlockInfo.End.set(M.AllocaNo);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (BlockMarkerSet.size() == 1) {
|
|
|
|
ProcessMarker(BlockMarkerSet.begin()->getFirst(),
|
|
|
|
BlockMarkerSet.begin()->getSecond());
|
|
|
|
} else {
|
|
|
|
// Scan the BB to determine the marker order.
|
2020-06-15 07:31:09 +08:00
|
|
|
for (const Instruction &I : *BB) {
|
|
|
|
const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
|
2020-06-15 06:58:10 +08:00
|
|
|
if (!II)
|
|
|
|
continue;
|
|
|
|
auto It = BlockMarkerSet.find(II);
|
2016-06-30 04:37:43 +08:00
|
|
|
if (It == BlockMarkerSet.end())
|
|
|
|
continue;
|
2020-06-15 06:58:10 +08:00
|
|
|
ProcessMarker(II, It->getSecond());
|
2016-06-30 04:37:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned BBEnd = InstNo;
|
|
|
|
BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
|
|
|
|
}
|
|
|
|
NumInst = InstNo;
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:37:19 +08:00
|
|
|
void StackLifetime::calculateLocalLiveness() {
|
2020-06-15 10:04:47 +08:00
|
|
|
bool Changed = true;
|
|
|
|
while (Changed) {
|
|
|
|
Changed = false;
|
2016-06-30 04:37:43 +08:00
|
|
|
|
2020-06-15 07:31:09 +08:00
|
|
|
for (const BasicBlock *BB : depth_first(&F)) {
|
2020-06-15 07:06:07 +08:00
|
|
|
BlockLifetimeInfo &BlockInfo = BlockLiveness.find(BB)->getSecond();
|
2016-06-30 04:37:43 +08:00
|
|
|
|
|
|
|
// Compute LiveIn by unioning together the LiveOut sets of all preds.
|
|
|
|
BitVector LocalLiveIn;
|
|
|
|
for (auto *PredBB : predecessors(BB)) {
|
|
|
|
LivenessMap::const_iterator I = BlockLiveness.find(PredBB);
|
2018-08-23 05:38:57 +08:00
|
|
|
// If a predecessor is unreachable, ignore it.
|
|
|
|
if (I == BlockLiveness.end())
|
|
|
|
continue;
|
2016-06-30 04:37:43 +08:00
|
|
|
LocalLiveIn |= I->second.LiveOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute LiveOut by subtracting out lifetimes that end in this
|
|
|
|
// block, then adding in lifetimes that begin in this block. If
|
|
|
|
// we have both BEGIN and END markers in the same basic block
|
|
|
|
// then we know that the BEGIN marker comes after the END,
|
|
|
|
// because we already handle the case where the BEGIN comes
|
|
|
|
// before the END when collecting the markers (and building the
|
|
|
|
// BEGIN/END vectors).
|
|
|
|
BitVector LocalLiveOut = LocalLiveIn;
|
|
|
|
LocalLiveOut.reset(BlockInfo.End);
|
|
|
|
LocalLiveOut |= BlockInfo.Begin;
|
|
|
|
|
|
|
|
// Update block LiveIn set, noting whether it has changed.
|
|
|
|
if (LocalLiveIn.test(BlockInfo.LiveIn)) {
|
2020-06-15 10:04:47 +08:00
|
|
|
Changed = true;
|
2016-06-30 04:37:43 +08:00
|
|
|
BlockInfo.LiveIn |= LocalLiveIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update block LiveOut set, noting whether it has changed.
|
|
|
|
if (LocalLiveOut.test(BlockInfo.LiveOut)) {
|
2020-06-15 10:04:47 +08:00
|
|
|
Changed = true;
|
2016-06-30 04:37:43 +08:00
|
|
|
BlockInfo.LiveOut |= LocalLiveOut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // while changed.
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:37:19 +08:00
|
|
|
void StackLifetime::calculateLiveIntervals() {
|
2016-06-30 04:37:43 +08:00
|
|
|
for (auto IT : BlockLiveness) {
|
2020-06-15 07:31:09 +08:00
|
|
|
const BasicBlock *BB = IT.getFirst();
|
2016-06-30 04:37:43 +08:00
|
|
|
BlockLifetimeInfo &BlockInfo = IT.getSecond();
|
|
|
|
unsigned BBStart, BBEnd;
|
|
|
|
std::tie(BBStart, BBEnd) = BlockInstRange[BB];
|
|
|
|
|
|
|
|
BitVector Started, Ended;
|
|
|
|
Started.resize(NumAllocas);
|
|
|
|
Ended.resize(NumAllocas);
|
|
|
|
SmallVector<unsigned, 8> Start;
|
|
|
|
Start.resize(NumAllocas);
|
|
|
|
|
|
|
|
// LiveIn ranges start at the first instruction.
|
|
|
|
for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
|
|
|
|
if (BlockInfo.LiveIn.test(AllocaNo)) {
|
|
|
|
Started.set(AllocaNo);
|
|
|
|
Start[AllocaNo] = BBStart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &It : BBMarkers[BB]) {
|
|
|
|
unsigned InstNo = It.first;
|
|
|
|
bool IsStart = It.second.IsStart;
|
|
|
|
unsigned AllocaNo = It.second.AllocaNo;
|
|
|
|
|
|
|
|
if (IsStart) {
|
2016-09-17 06:04:10 +08:00
|
|
|
assert(!Started.test(AllocaNo) || Start[AllocaNo] == BBStart);
|
|
|
|
if (!Started.test(AllocaNo)) {
|
|
|
|
Started.set(AllocaNo);
|
|
|
|
Ended.reset(AllocaNo);
|
|
|
|
Start[AllocaNo] = InstNo;
|
|
|
|
}
|
2016-06-30 04:37:43 +08:00
|
|
|
} else {
|
|
|
|
assert(!Ended.test(AllocaNo));
|
|
|
|
if (Started.test(AllocaNo)) {
|
2020-06-15 10:04:47 +08:00
|
|
|
LiveRanges[AllocaNo].addRange(Start[AllocaNo], InstNo);
|
2016-06-30 04:37:43 +08:00
|
|
|
Started.reset(AllocaNo);
|
|
|
|
}
|
|
|
|
Ended.set(AllocaNo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
|
|
|
|
if (Started.test(AllocaNo))
|
2020-06-15 10:04:47 +08:00
|
|
|
LiveRanges[AllocaNo].addRange(Start[AllocaNo], BBEnd);
|
2016-06-30 04:37:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:32:27 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2020-06-15 17:37:19 +08:00
|
|
|
LLVM_DUMP_METHOD void StackLifetime::dumpAllocas() const {
|
2016-06-30 04:37:43 +08:00
|
|
|
dbgs() << "Allocas:\n";
|
|
|
|
for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
|
|
|
|
dbgs() << " " << AllocaNo << ": " << *Allocas[AllocaNo] << "\n";
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:37:19 +08:00
|
|
|
LLVM_DUMP_METHOD void StackLifetime::dumpBlockLiveness() const {
|
2016-06-30 04:37:43 +08:00
|
|
|
dbgs() << "Block liveness:\n";
|
|
|
|
for (auto IT : BlockLiveness) {
|
2020-06-15 07:31:09 +08:00
|
|
|
const BasicBlock *BB = IT.getFirst();
|
2020-06-15 07:06:07 +08:00
|
|
|
const BlockLifetimeInfo &BlockInfo = BlockLiveness.find(BB)->getSecond();
|
2020-06-15 07:31:09 +08:00
|
|
|
auto BlockRange = BlockInstRange.find(BB)->getSecond();
|
2016-06-30 04:37:43 +08:00
|
|
|
dbgs() << " BB [" << BlockRange.first << ", " << BlockRange.second
|
|
|
|
<< "): begin " << BlockInfo.Begin << ", end " << BlockInfo.End
|
|
|
|
<< ", livein " << BlockInfo.LiveIn << ", liveout "
|
|
|
|
<< BlockInfo.LiveOut << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:37:19 +08:00
|
|
|
LLVM_DUMP_METHOD void StackLifetime::dumpLiveRanges() const {
|
2016-06-30 04:37:43 +08:00
|
|
|
dbgs() << "Alloca liveness:\n";
|
2020-06-15 07:31:09 +08:00
|
|
|
for (unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
|
|
|
|
dbgs() << " " << AllocaNo << ": " << LiveRanges[AllocaNo] << "\n";
|
2016-06-30 04:37:43 +08:00
|
|
|
}
|
2017-01-28 10:02:38 +08:00
|
|
|
#endif
|
2016-06-30 04:37:43 +08:00
|
|
|
|
2020-06-15 17:37:19 +08:00
|
|
|
StackLifetime::StackLifetime(const Function &F,
|
2020-06-15 07:31:09 +08:00
|
|
|
ArrayRef<const AllocaInst *> Allocas)
|
2020-06-15 05:35:34 +08:00
|
|
|
: F(F), Allocas(Allocas), NumAllocas(Allocas.size()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dumpAllocas());
|
2016-06-30 04:37:43 +08:00
|
|
|
|
|
|
|
for (unsigned I = 0; I < NumAllocas; ++I)
|
|
|
|
AllocaNumbering[Allocas[I]] = I;
|
|
|
|
|
|
|
|
collectMarkers();
|
2020-06-15 05:35:34 +08:00
|
|
|
}
|
2016-06-30 04:37:43 +08:00
|
|
|
|
2020-06-15 17:37:19 +08:00
|
|
|
void StackLifetime::run() {
|
2020-06-15 09:59:23 +08:00
|
|
|
LiveRanges.resize(NumAllocas, LiveRange(NumInst));
|
2016-06-30 04:37:43 +08:00
|
|
|
for (unsigned I = 0; I < NumAllocas; ++I)
|
|
|
|
if (!InterestingAllocas.test(I))
|
|
|
|
LiveRanges[I] = getFullLiveRange();
|
|
|
|
|
|
|
|
calculateLocalLiveness();
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dumpBlockLiveness());
|
2016-06-30 04:37:43 +08:00
|
|
|
calculateLiveIntervals();
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dumpLiveRanges());
|
2016-06-30 04:37:43 +08:00
|
|
|
}
|