15 #include "llvm/Config/llvm-config.h" 35 #define DEBUG_TYPE "safestackcoloring" 39 cl::desc(
"enable safe stack coloring"),
43 const auto IT = AllocaNumbering.find(AI);
45 return LiveRanges[
IT->second];
48 bool StackColoring::readMarker(
Instruction *
I,
bool *IsStart) {
50 if (!II || (II->getIntrinsicID() != Intrinsic::lifetime_start &&
51 II->getIntrinsicID() != Intrinsic::lifetime_end))
54 *IsStart = II->getIntrinsicID() == Intrinsic::lifetime_start;
59 for (
auto *I : Markers) {
63 if (
Op &&
Op->use_empty())
64 Op->eraseFromParent();
68 void StackColoring::collectMarkers() {
69 InterestingAllocas.
resize(NumAllocas);
73 for (
unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
77 while (!WorkList.
empty()) {
80 if (
auto *BI = dyn_cast<BitCastInst>(U)) {
88 if (!readMarker(UI, &IsStart))
91 InterestingAllocas.
set(AllocaNo);
92 BBMarkerSet[UI->getParent()][UI] = {AllocaNo, IsStart};
93 Markers.push_back(UI);
108 LLVM_DEBUG(
dbgs() <<
" " << InstNo <<
": BB " << BB->getName() <<
"\n");
109 unsigned BBStart = InstNo++;
111 BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
112 BlockInfo.Begin.resize(NumAllocas);
113 BlockInfo.End.resize(NumAllocas);
114 BlockInfo.LiveIn.resize(NumAllocas);
115 BlockInfo.LiveOut.resize(NumAllocas);
117 auto &BlockMarkerSet = BBMarkerSet[BB];
118 if (BlockMarkerSet.empty()) {
119 unsigned BBEnd = InstNo;
120 BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
124 auto ProcessMarker = [&](
Instruction *
I,
const Marker &M) {
126 << (M.IsStart ?
"start " :
"end ") << M.AllocaNo
127 <<
", " << *I <<
"\n");
129 BBMarkers[BB].push_back({InstNo, M});
131 InstructionNumbering[
I] = InstNo++;
134 if (BlockInfo.End.test(M.AllocaNo))
135 BlockInfo.End.reset(M.AllocaNo);
136 BlockInfo.Begin.set(M.AllocaNo);
138 if (BlockInfo.Begin.test(M.AllocaNo))
139 BlockInfo.Begin.reset(M.AllocaNo);
140 BlockInfo.End.set(M.AllocaNo);
144 if (BlockMarkerSet.size() == 1) {
145 ProcessMarker(BlockMarkerSet.begin()->getFirst(),
146 BlockMarkerSet.begin()->getSecond());
150 auto It = BlockMarkerSet.find(&I);
151 if (It == BlockMarkerSet.end())
153 ProcessMarker(&I, It->getSecond());
157 unsigned BBEnd = InstNo;
158 BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
163 void StackColoring::calculateLocalLiveness() {
169 BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
175 assert(I != BlockLiveness.
end() &&
"Predecessor not found");
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.
void push_back(const T &Elt)
This class represents a set of interesting instructions where an alloca is live.
Compute iterated dominance frontiers using a linear time algorithm.
bool test(unsigned Idx) const
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
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.
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")))
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())
A wrapper class for inspecting calls to intrinsic functions.
an instruction to allocate memory on the stack