19 using namespace llvm::safestack;
21 #define DEBUG_TYPE "safestackcoloring"
25 cl::desc(
"enable safe stack coloring"),
29 const auto IT = AllocaNumbering.find(AI);
31 return LiveRanges[
IT->second];
34 bool StackColoring::readMarker(
Instruction *
I,
bool *IsStart) {
36 if (!II || (II->getIntrinsicID() != Intrinsic::lifetime_start &&
37 II->getIntrinsicID() != Intrinsic::lifetime_end))
40 *IsStart = II->getIntrinsicID() == Intrinsic::lifetime_start;
45 for (
auto *I : Markers) {
49 if (
Op &&
Op->use_empty())
50 Op->eraseFromParent();
54 void StackColoring::collectMarkers() {
55 InterestingAllocas.
resize(NumAllocas);
59 for (
unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
63 while (!WorkList.
empty()) {
66 if (
auto *BI = dyn_cast<BitCastInst>(U)) {
74 if (!readMarker(UI, &IsStart))
77 InterestingAllocas.
set(AllocaNo);
78 BBMarkerSet[UI->getParent()][UI] = {AllocaNo, IsStart};
79 Markers.push_back(UI);
94 DEBUG(
dbgs() <<
" " << InstNo <<
": BB " << BB->getName() <<
"\n");
95 unsigned BBStart = InstNo++;
97 BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
98 BlockInfo.Begin.resize(NumAllocas);
99 BlockInfo.End.resize(NumAllocas);
100 BlockInfo.LiveIn.resize(NumAllocas);
101 BlockInfo.LiveOut.resize(NumAllocas);
103 auto &BlockMarkerSet = BBMarkerSet[BB];
104 if (BlockMarkerSet.empty()) {
105 unsigned BBEnd = InstNo;
106 BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
110 auto ProcessMarker = [&](
Instruction *
I,
const Marker &M) {
112 << (M.IsStart ?
"start " :
"end ") << M.AllocaNo <<
", "
115 BBMarkers[BB].push_back({InstNo, M});
117 InstructionNumbering[
I] = InstNo++;
120 if (BlockInfo.End.test(M.AllocaNo))
121 BlockInfo.End.reset(M.AllocaNo);
122 BlockInfo.Begin.set(M.AllocaNo);
124 if (BlockInfo.Begin.test(M.AllocaNo))
125 BlockInfo.Begin.reset(M.AllocaNo);
126 BlockInfo.End.set(M.AllocaNo);
130 if (BlockMarkerSet.size() == 1) {
131 ProcessMarker(BlockMarkerSet.begin()->getFirst(),
132 BlockMarkerSet.begin()->getSecond());
136 auto It = BlockMarkerSet.find(&I);
137 if (It == BlockMarkerSet.end())
139 ProcessMarker(&I, It->getSecond());
143 unsigned BBEnd = InstNo;
144 BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
149 void StackColoring::calculateLocalLiveness() {
155 BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
161 assert(I != BlockLiveness.
end() &&
"Predecessor not found");
162 LocalLiveIn |= I->second.LiveOut;
173 LocalLiveOut.
reset(BlockInfo.End);
174 LocalLiveOut |= BlockInfo.Begin;
177 if (LocalLiveIn.
test(BlockInfo.LiveIn)) {
179 BlockInfo.LiveIn |= LocalLiveIn;
183 if (LocalLiveOut.
test(BlockInfo.LiveOut)) {
185 BlockInfo.LiveOut |= LocalLiveOut;
191 void StackColoring::calculateLiveIntervals() {
192 for (
auto IT : BlockLiveness) {
194 BlockLifetimeInfo &BlockInfo =
IT.getSecond();
195 unsigned BBStart, BBEnd;
196 std::tie(BBStart, BBEnd) = BlockInstRange[BB];
199 Started.
resize(NumAllocas);
205 for (
unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
206 if (BlockInfo.LiveIn.test(AllocaNo)) {
207 Started.
set(AllocaNo);
208 Start[AllocaNo] = BBStart;
212 for (
auto &It : BBMarkers[BB]) {
213 unsigned InstNo = It.first;
214 bool IsStart = It.second.IsStart;
215 unsigned AllocaNo = It.second.AllocaNo;
218 assert(!Started.
test(AllocaNo) || Start[AllocaNo] == BBStart);
219 if (!Started.
test(AllocaNo)) {
220 Started.
set(AllocaNo);
221 Ended.
reset(AllocaNo);
222 Start[AllocaNo] = InstNo;
226 if (Started.
test(AllocaNo)) {
227 LiveRanges[AllocaNo].AddRange(Start[AllocaNo], InstNo);
228 Started.
reset(AllocaNo);
234 for (
unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
235 if (Started.
test(AllocaNo))
236 LiveRanges[AllocaNo].AddRange(Start[AllocaNo], BBEnd);
240 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
242 dbgs() <<
"Allocas:\n";
243 for (
unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo)
244 dbgs() <<
" " << AllocaNo <<
": " << *Allocas[AllocaNo] <<
"\n";
248 dbgs() <<
"Block liveness:\n";
249 for (
auto IT : BlockLiveness) {
251 BlockLifetimeInfo &BlockInfo = BlockLiveness[BB];
252 auto BlockRange = BlockInstRange[BB];
253 dbgs() <<
" BB [" << BlockRange.first <<
", " << BlockRange.second
254 <<
"): begin " << BlockInfo.Begin <<
", end " << BlockInfo.End
255 <<
", livein " << BlockInfo.LiveIn <<
", liveout "
256 << BlockInfo.LiveOut <<
"\n";
261 dbgs() <<
"Alloca liveness:\n";
262 for (
unsigned AllocaNo = 0; AllocaNo < NumAllocas; ++AllocaNo) {
264 dbgs() <<
" " << AllocaNo <<
": " << Range <<
"\n";
270 DEBUG(dumpAllocas());
272 for (
unsigned I = 0; I < NumAllocas; ++
I)
273 AllocaNumbering[Allocas[I]] = I;
274 LiveRanges.resize(NumAllocas);
279 for (
auto &R : LiveRanges) {
286 for (
auto &R : LiveRanges)
287 R.SetMaximum(NumInst);
288 for (
unsigned I = 0; I < NumAllocas; ++
I)
289 if (!InterestingAllocas.
test(I))
292 calculateLocalLiveness();
293 DEBUG(dumpBlockLiveness());
294 calculateLiveIntervals();
295 DEBUG(dumpLiveRanges());
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
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")))
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.
#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.
This class represents the liveness of a register, stack slot, etc.
DenseMapIterator< BasicBlock *, BlockLifetimeInfo, DenseMapInfo< BasicBlock * >, detail::DenseMapPair< BasicBlock *, BlockLifetimeInfo >, true > const_iterator
LLVM_NODISCARD bool empty() const
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))
iterator find(const_arg_type_t< KeyT > Val)
initializer< Ty > init(const Ty &Val)
LLVM Basic Block Representation.
Value * getOperand(unsigned i) const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool test(unsigned Idx) const
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()
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