14 #include "llvm/Config/llvm-config.h" 34 #define DEBUG_TYPE "safestackcoloring" 38 cl::desc(
"enable safe stack coloring"),
42 const auto IT = AllocaNumbering.find(AI);
44 return LiveRanges[
IT->second];
47 bool StackColoring::readMarker(
Instruction *
I,
bool *IsStart) {
51 auto *II = cast<IntrinsicInst>(
I);
52 *IsStart = II->getIntrinsicID() == Intrinsic::lifetime_start;
57 for (
auto *I : Markers) {
61 if (
Op &&
Op->use_empty())
62 Op->eraseFromParent();
66 void StackColoring::collectMarkers() {
67 InterestingAllocas.
resize(NumAllocas);
71 for (
unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
75 while (!WorkList.
empty()) {
78 if (
auto *BI = dyn_cast<BitCastInst>(U)) {
86 if (!readMarker(UI, &IsStart))
89 InterestingAllocas.
set(AllocaNo);
90 BBMarkerSet[UI->getParent()][UI] = {AllocaNo, IsStart};
91 Markers.push_back(UI);
106 LLVM_DEBUG(
dbgs() <<
" " << InstNo <<
": BB " << BB->getName() <<
"\n");
107 unsigned BBStart = InstNo++;
109 BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
110 BlockInfo.Begin.resize(NumAllocas);
111 BlockInfo.End.resize(NumAllocas);
112 BlockInfo.LiveIn.resize(NumAllocas);
113 BlockInfo.LiveOut.resize(NumAllocas);
115 auto &BlockMarkerSet = BBMarkerSet[BB];
116 if (BlockMarkerSet.empty()) {
117 unsigned BBEnd = InstNo;
118 BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
122 auto ProcessMarker = [&](
Instruction *
I,
const Marker &M) {
124 << (M.IsStart ?
"start " :
"end ") << M.AllocaNo
125 <<
", " << *I <<
"\n");
127 BBMarkers[BB].push_back({InstNo, M});
129 InstructionNumbering[
I] = InstNo++;
132 if (BlockInfo.End.test(M.AllocaNo))
133 BlockInfo.End.reset(M.AllocaNo);
134 BlockInfo.Begin.set(M.AllocaNo);
136 if (BlockInfo.Begin.test(M.AllocaNo))
137 BlockInfo.Begin.reset(M.AllocaNo);
138 BlockInfo.End.set(M.AllocaNo);
142 if (BlockMarkerSet.size() == 1) {
143 ProcessMarker(BlockMarkerSet.begin()->getFirst(),
144 BlockMarkerSet.begin()->getSecond());
148 auto It = BlockMarkerSet.find(&I);
149 if (It == BlockMarkerSet.end())
151 ProcessMarker(&I, It->getSecond());
155 unsigned BBEnd = InstNo;
156 BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
161 void StackColoring::calculateLocalLiveness() {
167 BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
174 if (I == BlockLiveness.
end())
176 LocalLiveIn |= I->second.LiveOut;
187 LocalLiveOut.
reset(BlockInfo.End);
188 LocalLiveOut |= BlockInfo.Begin;
191 if (LocalLiveIn.
test(BlockInfo.LiveIn)) {
193 BlockInfo.LiveIn |= LocalLiveIn;
197 if (LocalLiveOut.
test(BlockInfo.LiveOut)) {
199 BlockInfo.LiveOut |= LocalLiveOut;
205 void StackColoring::calculateLiveIntervals() {
206 for (
auto IT : BlockLiveness) {
208 BlockLifetimeInfo &BlockInfo =
IT.getSecond();
209 unsigned BBStart, BBEnd;
210 std::tie(BBStart, BBEnd) = BlockInstRange[BB];
213 Started.
resize(NumAllocas);
219 for (
unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
220 if (BlockInfo.LiveIn.test(AllocaNo)) {
221 Started.
set(AllocaNo);
222 Start[AllocaNo] = BBStart;
226 for (
auto &It : BBMarkers[BB]) {
227 unsigned InstNo = It.first;
228 bool IsStart = It.second.IsStart;
229 unsigned AllocaNo = It.second.AllocaNo;
232 assert(!Started.
test(AllocaNo) || Start[AllocaNo] == BBStart);
233 if (!Started.
test(AllocaNo)) {
234 Started.
set(AllocaNo);
235 Ended.
reset(AllocaNo);
236 Start[AllocaNo] = InstNo;
240 if (Started.
test(AllocaNo)) {
241 LiveRanges[AllocaNo].AddRange(Start[AllocaNo], InstNo);
242 Started.
reset(AllocaNo);
248 for (
unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
249 if (Started.
test(AllocaNo))
250 LiveRanges[AllocaNo].AddRange(Start[AllocaNo], BBEnd);
254 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 256 dbgs() <<
"Allocas:\n";
257 for (
unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
258 dbgs() <<
" " << AllocaNo <<
": " << *Allocas[AllocaNo] <<
"\n";
262 dbgs() <<
"Block liveness:\n";
263 for (
auto IT : BlockLiveness) {
265 BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
266 auto BlockRange = BlockInstRange[BB];
267 dbgs() <<
" BB [" << BlockRange.first <<
", " << BlockRange.second
268 <<
"): begin " << BlockInfo.Begin <<
", end " << BlockInfo.End
269 <<
", livein " << BlockInfo.LiveIn <<
", liveout " 270 << BlockInfo.LiveOut <<
"\n";
275 dbgs() <<
"Alloca liveness:\n";
276 for (
unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
278 dbgs() <<
" " << AllocaNo <<
": " << Range <<
"\n";
286 for (
unsigned I = 0; I < NumAllocas; ++
I)
287 AllocaNumbering[Allocas[I]] = I;
288 LiveRanges.resize(NumAllocas);
293 for (
auto &R : LiveRanges) {
300 for (
auto &R : LiveRanges)
301 R.SetMaximum(NumInst);
302 for (
unsigned I = 0; I < NumAllocas; ++
I)
303 if (!InterestingAllocas.
test(I))
306 calculateLocalLiveness();
308 calculateLiveIntervals();
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
This class represents a set of interesting instructions where an alloca is live.
This class represents lattice values for constants.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
void push_back(const T &Elt)
bool test(unsigned Idx) const
const LiveRange & getLiveRange(AllocaInst *AI)
Returns a set of "interesting" instructions where the given alloca is live.
LiveRange getFullLiveRange()
Returns a live range that represents an alloca that is live throughout the entire function...
static cl::opt< bool > ClColoring("safe-stack-coloring", cl::desc("enable safe stack coloring"), cl::Hidden, cl::init(false))
Value * getOperand(unsigned i) const
iterator find(const_arg_type_t< KeyT > Val)
initializer< Ty > init(const Ty &Val)
LLVM Basic Block Representation.
bool isLifetimeStartOrEnd() const
Return true if the instruction is a llvm.lifetime.start or llvm.lifetime.end marker.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD T pop_back_val()
pred_range predecessors(BasicBlock *BB)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
iterator_range< user_iterator > users()
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate IT block based on arch"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT, "arm-no-restrict-it", "Allow IT blocks based on ARMv7")))
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
LLVM_NODISCARD bool empty() const
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
iterator_range< df_iterator< T > > depth_first(const T &G)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
an instruction to allocate memory on the stack