blob: 2bca7cf60fdd2f3aa722e5400135d0fdff09b492 [file] [log] [blame]
Vitaly Buka4493fe12018-11-26 21:57:47 +00001//===- StackSafetyAnalysis.cpp - Stack memory safety analysis -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Vitaly Buka4493fe12018-11-26 21:57:47 +00006//
7//===----------------------------------------------------------------------===//
8//
9//===----------------------------------------------------------------------===//
10
11#include "llvm/Analysis/StackSafetyAnalysis.h"
Vitaly Buka4493fe12018-11-26 21:57:47 +000012#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Vitaly Bukafa98c072018-11-26 21:57:59 +000013#include "llvm/IR/CallSite.h"
14#include "llvm/IR/InstIterator.h"
15#include "llvm/IR/IntrinsicInst.h"
Vitaly Buka4493fe12018-11-26 21:57:47 +000016#include "llvm/Support/raw_ostream.h"
17
18using namespace llvm;
19
20#define DEBUG_TYPE "stack-safety"
21
Vitaly Buka42b05062018-11-26 23:05:58 +000022static cl::opt<int> StackSafetyMaxIterations("stack-safety-max-iterations",
23 cl::init(20), cl::Hidden);
24
Vitaly Bukafa98c072018-11-26 21:57:59 +000025namespace {
Vitaly Buka4493fe12018-11-26 21:57:47 +000026
Vitaly Bukafa98c072018-11-26 21:57:59 +000027/// Rewrite an SCEV expression for a memory access address to an expression that
28/// represents offset from the given alloca.
29class AllocaOffsetRewriter : public SCEVRewriteVisitor<AllocaOffsetRewriter> {
30 const Value *AllocaPtr;
31
32public:
33 AllocaOffsetRewriter(ScalarEvolution &SE, const Value *AllocaPtr)
34 : SCEVRewriteVisitor(SE), AllocaPtr(AllocaPtr) {}
35
36 const SCEV *visit(const SCEV *Expr) {
37 // Only re-write the expression if the alloca is used in an addition
38 // expression (it can be used in other types of expressions if it's cast to
39 // an int and passed as an argument.)
40 if (!isa<SCEVAddRecExpr>(Expr) && !isa<SCEVAddExpr>(Expr) &&
41 !isa<SCEVUnknown>(Expr))
42 return Expr;
43 return SCEVRewriteVisitor<AllocaOffsetRewriter>::visit(Expr);
44 }
45
46 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
47 // FIXME: look through one or several levels of definitions?
48 // This can be inttoptr(AllocaPtr) and SCEV would not unwrap
49 // it for us.
50 if (Expr->getValue() == AllocaPtr)
51 return SE.getZero(Expr->getType());
52 return Expr;
53 }
54};
55
56/// Describes use of address in as a function call argument.
57struct PassAsArgInfo {
58 /// Function being called.
59 const GlobalValue *Callee = nullptr;
60 /// Index of argument which pass address.
61 size_t ParamNo = 0;
62 // Offset range of address from base address (alloca or calling function
63 // argument).
64 // Range should never set to empty-set, that is an invalid access range
65 // that can cause empty-set to be propagated with ConstantRange::add
66 ConstantRange Offset;
67 PassAsArgInfo(const GlobalValue *Callee, size_t ParamNo, ConstantRange Offset)
68 : Callee(Callee), ParamNo(ParamNo), Offset(Offset) {}
69
70 StringRef getName() const { return Callee->getName(); }
71};
72
73raw_ostream &operator<<(raw_ostream &OS, const PassAsArgInfo &P) {
74 return OS << "@" << P.getName() << "(arg" << P.ParamNo << ", " << P.Offset
75 << ")";
76}
77
78/// Describe uses of address (alloca or parameter) inside of the function.
79struct UseInfo {
80 // Access range if the address (alloca or parameters).
81 // It is allowed to be empty-set when there are no known accesses.
82 ConstantRange Range;
83
84 // List of calls which pass address as an argument.
85 SmallVector<PassAsArgInfo, 4> Calls;
86
87 explicit UseInfo(unsigned PointerSize) : Range{PointerSize, false} {}
88
89 void updateRange(ConstantRange R) { Range = Range.unionWith(R); }
90};
91
92raw_ostream &operator<<(raw_ostream &OS, const UseInfo &U) {
93 OS << U.Range;
94 for (auto &Call : U.Calls)
95 OS << ", " << Call;
96 return OS;
97}
98
99struct AllocaInfo {
100 const AllocaInst *AI = nullptr;
101 uint64_t Size = 0;
102 UseInfo Use;
103
104 AllocaInfo(unsigned PointerSize, const AllocaInst *AI, uint64_t Size)
105 : AI(AI), Size(Size), Use(PointerSize) {}
106
107 StringRef getName() const { return AI->getName(); }
108};
109
110raw_ostream &operator<<(raw_ostream &OS, const AllocaInfo &A) {
111 return OS << A.getName() << "[" << A.Size << "]: " << A.Use;
112}
113
114struct ParamInfo {
115 const Argument *Arg = nullptr;
116 UseInfo Use;
117
118 explicit ParamInfo(unsigned PointerSize, const Argument *Arg)
119 : Arg(Arg), Use(PointerSize) {}
120
121 StringRef getName() const { return Arg ? Arg->getName() : "<N/A>"; }
122};
123
124raw_ostream &operator<<(raw_ostream &OS, const ParamInfo &P) {
125 return OS << P.getName() << "[]: " << P.Use;
126}
127
128/// Calculate the allocation size of a given alloca. Returns 0 if the
129/// size can not be statically determined.
130uint64_t getStaticAllocaAllocationSize(const AllocaInst *AI) {
131 const DataLayout &DL = AI->getModule()->getDataLayout();
132 uint64_t Size = DL.getTypeAllocSize(AI->getAllocatedType());
133 if (AI->isArrayAllocation()) {
134 auto C = dyn_cast<ConstantInt>(AI->getArraySize());
135 if (!C)
136 return 0;
137 Size *= C->getZExtValue();
138 }
139 return Size;
140}
141
142} // end anonymous namespace
143
144/// Describes uses of allocas and parameters inside of a single function.
145struct StackSafetyInfo::FunctionInfo {
146 // May be a Function or a GlobalAlias
147 const GlobalValue *GV = nullptr;
148 // Informations about allocas uses.
149 SmallVector<AllocaInfo, 4> Allocas;
150 // Informations about parameters uses.
151 SmallVector<ParamInfo, 4> Params;
152 // TODO: describe return value as depending on one or more of its arguments.
153
Vitaly Buka42b05062018-11-26 23:05:58 +0000154 // StackSafetyDataFlowAnalysis counter stored here for faster access.
155 int UpdateCount = 0;
156
Vitaly Bukafa98c072018-11-26 21:57:59 +0000157 FunctionInfo(const StackSafetyInfo &SSI) : FunctionInfo(*SSI.Info) {}
158
159 explicit FunctionInfo(const Function *F) : GV(F){};
Vitaly Buka42b05062018-11-26 23:05:58 +0000160 // Creates FunctionInfo that forwards all the parameters to the aliasee.
161 explicit FunctionInfo(const GlobalAlias *A);
Vitaly Bukafa98c072018-11-26 21:57:59 +0000162
163 FunctionInfo(FunctionInfo &&) = default;
164
165 bool IsDSOLocal() const { return GV->isDSOLocal(); };
166
167 bool IsInterposable() const { return GV->isInterposable(); };
168
169 StringRef getName() const { return GV->getName(); }
170
171 void print(raw_ostream &O) const {
Vitaly Buka42b05062018-11-26 23:05:58 +0000172 // TODO: Consider different printout format after
173 // StackSafetyDataFlowAnalysis. Calls and parameters are irrelevant then.
Vitaly Bukafa98c072018-11-26 21:57:59 +0000174 O << " @" << getName() << (IsDSOLocal() ? "" : " dso_preemptable")
175 << (IsInterposable() ? " interposable" : "") << "\n";
176 O << " args uses:\n";
177 for (auto &P : Params)
178 O << " " << P << "\n";
179 O << " allocas uses:\n";
180 for (auto &AS : Allocas)
181 O << " " << AS << "\n";
182 }
183
184private:
185 FunctionInfo(const FunctionInfo &) = default;
186};
187
Vitaly Buka42b05062018-11-26 23:05:58 +0000188StackSafetyInfo::FunctionInfo::FunctionInfo(const GlobalAlias *A) : GV(A) {
189 unsigned PointerSize = A->getParent()->getDataLayout().getPointerSizeInBits();
190 const GlobalObject *Aliasee = A->getBaseObject();
191 const FunctionType *Type = cast<FunctionType>(Aliasee->getValueType());
192 // 'Forward' all parameters to this alias to the aliasee
193 for (unsigned ArgNo = 0; ArgNo < Type->getNumParams(); ArgNo++) {
194 Params.emplace_back(PointerSize, nullptr);
195 UseInfo &US = Params.back().Use;
196 US.Calls.emplace_back(Aliasee, ArgNo, ConstantRange(APInt(PointerSize, 0)));
197 }
198}
199
Vitaly Bukafa98c072018-11-26 21:57:59 +0000200namespace {
201
202class StackSafetyLocalAnalysis {
203 const Function &F;
204 const DataLayout &DL;
205 ScalarEvolution &SE;
206 unsigned PointerSize = 0;
207
208 const ConstantRange UnknownRange;
209
210 ConstantRange offsetFromAlloca(Value *Addr, const Value *AllocaPtr);
211 ConstantRange getAccessRange(Value *Addr, const Value *AllocaPtr,
212 uint64_t AccessSize);
213 ConstantRange getMemIntrinsicAccessRange(const MemIntrinsic *MI, const Use &U,
214 const Value *AllocaPtr);
215
216 bool analyzeAllUses(const Value *Ptr, UseInfo &AS);
217
218 ConstantRange getRange(uint64_t Lower, uint64_t Upper) const {
219 return ConstantRange(APInt(PointerSize, Lower), APInt(PointerSize, Upper));
220 }
221
222public:
223 StackSafetyLocalAnalysis(const Function &F, ScalarEvolution &SE)
224 : F(F), DL(F.getParent()->getDataLayout()), SE(SE),
225 PointerSize(DL.getPointerSizeInBits()),
226 UnknownRange(PointerSize, true) {}
227
228 // Run the transformation on the associated function.
229 StackSafetyInfo run();
230};
231
232ConstantRange
233StackSafetyLocalAnalysis::offsetFromAlloca(Value *Addr,
234 const Value *AllocaPtr) {
235 if (!SE.isSCEVable(Addr->getType()))
236 return UnknownRange;
237
238 AllocaOffsetRewriter Rewriter(SE, AllocaPtr);
239 const SCEV *Expr = Rewriter.visit(SE.getSCEV(Addr));
240 ConstantRange Offset = SE.getUnsignedRange(Expr).zextOrTrunc(PointerSize);
241 assert(!Offset.isEmptySet());
242 return Offset;
243}
244
245ConstantRange StackSafetyLocalAnalysis::getAccessRange(Value *Addr,
246 const Value *AllocaPtr,
247 uint64_t AccessSize) {
248 if (!SE.isSCEVable(Addr->getType()))
249 return UnknownRange;
250
251 AllocaOffsetRewriter Rewriter(SE, AllocaPtr);
252 const SCEV *Expr = Rewriter.visit(SE.getSCEV(Addr));
253
254 ConstantRange AccessStartRange =
255 SE.getUnsignedRange(Expr).zextOrTrunc(PointerSize);
256 ConstantRange SizeRange = getRange(0, AccessSize);
257 ConstantRange AccessRange = AccessStartRange.add(SizeRange);
258 assert(!AccessRange.isEmptySet());
259 return AccessRange;
260}
261
262ConstantRange StackSafetyLocalAnalysis::getMemIntrinsicAccessRange(
263 const MemIntrinsic *MI, const Use &U, const Value *AllocaPtr) {
264 if (auto MTI = dyn_cast<MemTransferInst>(MI)) {
265 if (MTI->getRawSource() != U && MTI->getRawDest() != U)
266 return getRange(0, 1);
267 } else {
268 if (MI->getRawDest() != U)
269 return getRange(0, 1);
270 }
271 const auto *Len = dyn_cast<ConstantInt>(MI->getLength());
272 // Non-constant size => unsafe. FIXME: try SCEV getRange.
273 if (!Len)
274 return UnknownRange;
275 ConstantRange AccessRange = getAccessRange(U, AllocaPtr, Len->getZExtValue());
276 return AccessRange;
277}
278
279/// The function analyzes all local uses of Ptr (alloca or argument) and
280/// calculates local access range and all function calls where it was used.
281bool StackSafetyLocalAnalysis::analyzeAllUses(const Value *Ptr, UseInfo &US) {
282 SmallPtrSet<const Value *, 16> Visited;
283 SmallVector<const Value *, 8> WorkList;
284 WorkList.push_back(Ptr);
285
286 // A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc.
287 while (!WorkList.empty()) {
288 const Value *V = WorkList.pop_back_val();
289 for (const Use &UI : V->uses()) {
290 auto I = cast<const Instruction>(UI.getUser());
291 assert(V == UI.get());
292
293 switch (I->getOpcode()) {
294 case Instruction::Load: {
295 US.updateRange(
296 getAccessRange(UI, Ptr, DL.getTypeStoreSize(I->getType())));
297 break;
298 }
299
300 case Instruction::VAArg:
301 // "va-arg" from a pointer is safe.
302 break;
303 case Instruction::Store: {
304 if (V == I->getOperand(0)) {
305 // Stored the pointer - conservatively assume it may be unsafe.
306 US.updateRange(UnknownRange);
307 return false;
308 }
309 US.updateRange(getAccessRange(
310 UI, Ptr, DL.getTypeStoreSize(I->getOperand(0)->getType())));
311 break;
312 }
313
314 case Instruction::Ret:
315 // Information leak.
316 // FIXME: Process parameters correctly. This is a leak only if we return
317 // alloca.
318 US.updateRange(UnknownRange);
319 return false;
320
321 case Instruction::Call:
322 case Instruction::Invoke: {
323 ImmutableCallSite CS(I);
324
Vedant Kumarb264d692018-12-21 21:49:40 +0000325 if (I->isLifetimeStartOrEnd())
326 break;
Vitaly Bukafa98c072018-11-26 21:57:59 +0000327
328 if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
329 US.updateRange(getMemIntrinsicAccessRange(MI, UI, Ptr));
330 break;
331 }
332
333 // FIXME: consult devirt?
334 // Do not follow aliases, otherwise we could inadvertently follow
335 // dso_preemptable aliases or aliases with interposable linkage.
336 const GlobalValue *Callee = dyn_cast<GlobalValue>(
337 CS.getCalledValue()->stripPointerCastsNoFollowAliases());
338 if (!Callee) {
339 US.updateRange(UnknownRange);
340 return false;
341 }
342
343 assert(isa<Function>(Callee) || isa<GlobalAlias>(Callee));
344
345 ImmutableCallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
346 for (ImmutableCallSite::arg_iterator A = B; A != E; ++A) {
347 if (A->get() == V) {
348 ConstantRange Offset = offsetFromAlloca(UI, Ptr);
349 US.Calls.emplace_back(Callee, A - B, Offset);
350 }
351 }
352
353 break;
354 }
355
356 default:
357 if (Visited.insert(I).second)
358 WorkList.push_back(cast<const Instruction>(I));
359 }
360 }
361 }
362
363 return true;
364}
365
366StackSafetyInfo StackSafetyLocalAnalysis::run() {
367 StackSafetyInfo::FunctionInfo Info(&F);
368 assert(!F.isDeclaration() &&
369 "Can't run StackSafety on a function declaration");
370
371 LLVM_DEBUG(dbgs() << "[StackSafety] " << F.getName() << "\n");
372
373 for (auto &I : instructions(F)) {
374 if (auto AI = dyn_cast<AllocaInst>(&I)) {
375 Info.Allocas.emplace_back(PointerSize, AI,
376 getStaticAllocaAllocationSize(AI));
377 AllocaInfo &AS = Info.Allocas.back();
378 analyzeAllUses(AI, AS.Use);
379 }
380 }
381
382 for (const Argument &A : make_range(F.arg_begin(), F.arg_end())) {
383 Info.Params.emplace_back(PointerSize, &A);
384 ParamInfo &PS = Info.Params.back();
385 analyzeAllUses(&A, PS.Use);
386 }
387
388 LLVM_DEBUG(dbgs() << "[StackSafety] done\n");
389 LLVM_DEBUG(Info.print(dbgs()));
390 return StackSafetyInfo(std::move(Info));
391}
392
Vitaly Buka42b05062018-11-26 23:05:58 +0000393class StackSafetyDataFlowAnalysis {
394 using FunctionMap =
395 std::map<const GlobalValue *, StackSafetyInfo::FunctionInfo>;
396
397 FunctionMap Functions;
398 // Callee-to-Caller multimap.
399 DenseMap<const GlobalValue *, SmallVector<const GlobalValue *, 4>> Callers;
400 SetVector<const GlobalValue *> WorkList;
401
402 unsigned PointerSize = 0;
403 const ConstantRange UnknownRange;
404
405 ConstantRange getArgumentAccessRange(const GlobalValue *Callee,
406 unsigned ParamNo) const;
407 bool updateOneUse(UseInfo &US, bool UpdateToFullSet);
408 void updateOneNode(const GlobalValue *Callee,
409 StackSafetyInfo::FunctionInfo &FS);
410 void updateOneNode(const GlobalValue *Callee) {
411 updateOneNode(Callee, Functions.find(Callee)->second);
412 }
413 void updateAllNodes() {
414 for (auto &F : Functions)
415 updateOneNode(F.first, F.second);
416 }
417 void runDataFlow();
418 void verifyFixedPoint();
419
420public:
421 StackSafetyDataFlowAnalysis(
422 Module &M, std::function<const StackSafetyInfo &(Function &)> FI);
423 StackSafetyGlobalInfo run();
424};
425
426StackSafetyDataFlowAnalysis::StackSafetyDataFlowAnalysis(
427 Module &M, std::function<const StackSafetyInfo &(Function &)> FI)
428 : PointerSize(M.getDataLayout().getPointerSizeInBits()),
429 UnknownRange(PointerSize, true) {
430 // Without ThinLTO, run the local analysis for every function in the TU and
Vitaly Buka44abeb52018-11-27 01:56:44 +0000431 // then run the DFA.
Vitaly Buka42b05062018-11-26 23:05:58 +0000432 for (auto &F : M.functions())
433 if (!F.isDeclaration())
434 Functions.emplace(&F, FI(F));
435 for (auto &A : M.aliases())
436 if (isa<Function>(A.getBaseObject()))
Vitaly Buka769bff12018-11-27 01:56:26 +0000437 Functions.emplace(&A, StackSafetyInfo::FunctionInfo(&A));
Vitaly Buka42b05062018-11-26 23:05:58 +0000438}
439
440ConstantRange
441StackSafetyDataFlowAnalysis::getArgumentAccessRange(const GlobalValue *Callee,
442 unsigned ParamNo) const {
443 auto IT = Functions.find(Callee);
444 // Unknown callee (outside of LTO domain or an indirect call).
445 if (IT == Functions.end())
446 return UnknownRange;
447 const StackSafetyInfo::FunctionInfo &FS = IT->second;
448 // The definition of this symbol may not be the definition in this linkage
449 // unit.
450 if (!FS.IsDSOLocal() || FS.IsInterposable())
451 return UnknownRange;
452 if (ParamNo >= FS.Params.size()) // possibly vararg
453 return UnknownRange;
454 return FS.Params[ParamNo].Use.Range;
455}
456
457bool StackSafetyDataFlowAnalysis::updateOneUse(UseInfo &US,
458 bool UpdateToFullSet) {
459 bool Changed = false;
460 for (auto &CS : US.Calls) {
Vitaly Buka7792f5f2018-11-27 01:56:35 +0000461 assert(!CS.Offset.isEmptySet() &&
462 "Param range can't be empty-set, invalid offset range");
Vitaly Buka42b05062018-11-26 23:05:58 +0000463
464 ConstantRange CalleeRange = getArgumentAccessRange(CS.Callee, CS.ParamNo);
465 CalleeRange = CalleeRange.add(CS.Offset);
466 if (!US.Range.contains(CalleeRange)) {
467 Changed = true;
468 if (UpdateToFullSet)
469 US.Range = UnknownRange;
470 else
471 US.Range = US.Range.unionWith(CalleeRange);
472 }
473 }
474 return Changed;
475}
476
477void StackSafetyDataFlowAnalysis::updateOneNode(
478 const GlobalValue *Callee, StackSafetyInfo::FunctionInfo &FS) {
479 bool UpdateToFullSet = FS.UpdateCount > StackSafetyMaxIterations;
480 bool Changed = false;
481 for (auto &AS : FS.Allocas)
482 Changed |= updateOneUse(AS.Use, UpdateToFullSet);
483 for (auto &PS : FS.Params)
484 Changed |= updateOneUse(PS.Use, UpdateToFullSet);
485
486 if (Changed) {
487 LLVM_DEBUG(dbgs() << "=== update [" << FS.UpdateCount
488 << (UpdateToFullSet ? ", full-set" : "") << "] "
489 << FS.getName() << "\n");
490 // Callers of this function may need updating.
491 for (auto &CallerID : Callers[Callee])
492 WorkList.insert(CallerID);
493
494 ++FS.UpdateCount;
495 }
496}
497
498void StackSafetyDataFlowAnalysis::runDataFlow() {
499 Callers.clear();
500 WorkList.clear();
501
502 SmallVector<const GlobalValue *, 16> Callees;
503 for (auto &F : Functions) {
504 Callees.clear();
505 StackSafetyInfo::FunctionInfo &FS = F.second;
506 for (auto &AS : FS.Allocas)
507 for (auto &CS : AS.Use.Calls)
508 Callees.push_back(CS.Callee);
509 for (auto &PS : FS.Params)
510 for (auto &CS : PS.Use.Calls)
511 Callees.push_back(CS.Callee);
512
513 llvm::sort(Callees);
514 Callees.erase(std::unique(Callees.begin(), Callees.end()), Callees.end());
515
516 for (auto &Callee : Callees)
517 Callers[Callee].push_back(F.first);
518 }
519
520 updateAllNodes();
521
522 while (!WorkList.empty()) {
523 const GlobalValue *Callee = WorkList.back();
524 WorkList.pop_back();
525 updateOneNode(Callee);
526 }
527}
528
529void StackSafetyDataFlowAnalysis::verifyFixedPoint() {
530 WorkList.clear();
531 updateAllNodes();
532 assert(WorkList.empty());
533}
534
535StackSafetyGlobalInfo StackSafetyDataFlowAnalysis::run() {
536 runDataFlow();
537 LLVM_DEBUG(verifyFixedPoint());
538
539 StackSafetyGlobalInfo SSI;
540 for (auto &F : Functions)
541 SSI.emplace(F.first, std::move(F.second));
542 return SSI;
543}
544
Vitaly Bukab8e6fa62018-11-26 23:05:48 +0000545void print(const StackSafetyGlobalInfo &SSI, raw_ostream &O, const Module &M) {
Vitaly Buka42b05062018-11-26 23:05:58 +0000546 size_t Count = 0;
547 for (auto &F : M.functions())
548 if (!F.isDeclaration()) {
549 SSI.find(&F)->second.print(O);
550 O << "\n";
551 ++Count;
552 }
553 for (auto &A : M.aliases()) {
554 SSI.find(&A)->second.print(O);
555 O << "\n";
556 ++Count;
557 }
558 assert(Count == SSI.size() && "Unexpected functions in the result");
Vitaly Bukab8e6fa62018-11-26 23:05:48 +0000559}
560
Vitaly Bukafa98c072018-11-26 21:57:59 +0000561} // end anonymous namespace
562
563StackSafetyInfo::StackSafetyInfo() = default;
564StackSafetyInfo::StackSafetyInfo(StackSafetyInfo &&) = default;
565StackSafetyInfo &StackSafetyInfo::operator=(StackSafetyInfo &&) = default;
566
567StackSafetyInfo::StackSafetyInfo(FunctionInfo &&Info)
568 : Info(new FunctionInfo(std::move(Info))) {}
569
570StackSafetyInfo::~StackSafetyInfo() = default;
571
572void StackSafetyInfo::print(raw_ostream &O) const { Info->print(O); }
573
574AnalysisKey StackSafetyAnalysis::Key;
Vitaly Buka4493fe12018-11-26 21:57:47 +0000575
576StackSafetyInfo StackSafetyAnalysis::run(Function &F,
577 FunctionAnalysisManager &AM) {
Vitaly Bukafa98c072018-11-26 21:57:59 +0000578 StackSafetyLocalAnalysis SSLA(F, AM.getResult<ScalarEvolutionAnalysis>(F));
579 return SSLA.run();
Vitaly Buka4493fe12018-11-26 21:57:47 +0000580}
581
582PreservedAnalyses StackSafetyPrinterPass::run(Function &F,
583 FunctionAnalysisManager &AM) {
584 OS << "'Stack Safety Local Analysis' for function '" << F.getName() << "'\n";
585 AM.getResult<StackSafetyAnalysis>(F).print(OS);
586 return PreservedAnalyses::all();
587}
588
589char StackSafetyInfoWrapperPass::ID = 0;
590
591StackSafetyInfoWrapperPass::StackSafetyInfoWrapperPass() : FunctionPass(ID) {
592 initializeStackSafetyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
593}
594
595void StackSafetyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
596 AU.addRequired<ScalarEvolutionWrapperPass>();
597 AU.setPreservesAll();
598}
599
600void StackSafetyInfoWrapperPass::print(raw_ostream &O, const Module *M) const {
601 SSI.print(O);
602}
603
Vitaly Bukafa98c072018-11-26 21:57:59 +0000604bool StackSafetyInfoWrapperPass::runOnFunction(Function &F) {
605 StackSafetyLocalAnalysis SSLA(
606 F, getAnalysis<ScalarEvolutionWrapperPass>().getSE());
607 SSI = StackSafetyInfo(SSLA.run());
608 return false;
609}
Vitaly Buka4493fe12018-11-26 21:57:47 +0000610
Vitaly Bukab8e6fa62018-11-26 23:05:48 +0000611AnalysisKey StackSafetyGlobalAnalysis::Key;
612
613StackSafetyGlobalInfo
614StackSafetyGlobalAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
Vitaly Buka42b05062018-11-26 23:05:58 +0000615 FunctionAnalysisManager &FAM =
616 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
617
618 StackSafetyDataFlowAnalysis SSDFA(
619 M, [&FAM](Function &F) -> const StackSafetyInfo & {
620 return FAM.getResult<StackSafetyAnalysis>(F);
621 });
622 return SSDFA.run();
Vitaly Bukab8e6fa62018-11-26 23:05:48 +0000623}
624
625PreservedAnalyses StackSafetyGlobalPrinterPass::run(Module &M,
626 ModuleAnalysisManager &AM) {
627 OS << "'Stack Safety Analysis' for module '" << M.getName() << "'\n";
628 print(AM.getResult<StackSafetyGlobalAnalysis>(M), OS, M);
629 return PreservedAnalyses::all();
630}
631
632char StackSafetyGlobalInfoWrapperPass::ID = 0;
633
634StackSafetyGlobalInfoWrapperPass::StackSafetyGlobalInfoWrapperPass()
635 : ModulePass(ID) {
636 initializeStackSafetyGlobalInfoWrapperPassPass(
637 *PassRegistry::getPassRegistry());
638}
639
640void StackSafetyGlobalInfoWrapperPass::print(raw_ostream &O,
641 const Module *M) const {
642 ::print(SSI, O, *M);
643}
644
645void StackSafetyGlobalInfoWrapperPass::getAnalysisUsage(
646 AnalysisUsage &AU) const {
647 AU.addRequired<StackSafetyInfoWrapperPass>();
648}
649
Vitaly Buka42b05062018-11-26 23:05:58 +0000650bool StackSafetyGlobalInfoWrapperPass::runOnModule(Module &M) {
651 StackSafetyDataFlowAnalysis SSDFA(
652 M, [this](Function &F) -> const StackSafetyInfo & {
653 return getAnalysis<StackSafetyInfoWrapperPass>(F).getResult();
654 });
655 SSI = SSDFA.run();
656 return false;
657}
Vitaly Bukab8e6fa62018-11-26 23:05:48 +0000658
Vitaly Buka4493fe12018-11-26 21:57:47 +0000659static const char LocalPassArg[] = "stack-safety-local";
660static const char LocalPassName[] = "Stack Safety Local Analysis";
661INITIALIZE_PASS_BEGIN(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName,
662 false, true)
663INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
664INITIALIZE_PASS_END(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName,
665 false, true)
Vitaly Bukab8e6fa62018-11-26 23:05:48 +0000666
667static const char GlobalPassName[] = "Stack Safety Analysis";
668INITIALIZE_PASS_BEGIN(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE,
669 GlobalPassName, false, false)
670INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass)
671INITIALIZE_PASS_END(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE,
672 GlobalPassName, false, false)