LLVM 19.0.0git
StackSafetyAnalysis.cpp
Go to the documentation of this file.
1//===- StackSafetyAnalysis.cpp - Stack memory safety analysis -------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9//===----------------------------------------------------------------------===//
10
12#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/Statistic.h"
21#include "llvm/IR/GlobalValue.h"
23#include "llvm/IR/Instruction.h"
32#include <algorithm>
33#include <memory>
34#include <tuple>
35
36using namespace llvm;
37
38#define DEBUG_TYPE "stack-safety"
39
40STATISTIC(NumAllocaStackSafe, "Number of safe allocas");
41STATISTIC(NumAllocaTotal, "Number of total allocas");
42
43STATISTIC(NumCombinedCalleeLookupTotal,
44 "Number of total callee lookups on combined index.");
45STATISTIC(NumCombinedCalleeLookupFailed,
46 "Number of failed callee lookups on combined index.");
47STATISTIC(NumModuleCalleeLookupTotal,
48 "Number of total callee lookups on module index.");
49STATISTIC(NumModuleCalleeLookupFailed,
50 "Number of failed callee lookups on module index.");
51STATISTIC(NumCombinedParamAccessesBefore,
52 "Number of total param accesses before generateParamAccessSummary.");
53STATISTIC(NumCombinedParamAccessesAfter,
54 "Number of total param accesses after generateParamAccessSummary.");
55STATISTIC(NumCombinedDataFlowNodes,
56 "Number of total nodes in combined index for dataflow processing.");
57STATISTIC(NumIndexCalleeUnhandled, "Number of index callee which are unhandled.");
58STATISTIC(NumIndexCalleeMultipleWeak, "Number of index callee non-unique weak.");
59STATISTIC(NumIndexCalleeMultipleExternal, "Number of index callee non-unique external.");
60
61
62static cl::opt<int> StackSafetyMaxIterations("stack-safety-max-iterations",
63 cl::init(20), cl::Hidden);
64
65static cl::opt<bool> StackSafetyPrint("stack-safety-print", cl::init(false),
67
68static cl::opt<bool> StackSafetyRun("stack-safety-run", cl::init(false),
70
71namespace {
72
73// Check if we should bailout for such ranges.
74bool isUnsafe(const ConstantRange &R) {
75 return R.isEmptySet() || R.isFullSet() || R.isUpperSignWrapped();
76}
77
78ConstantRange addOverflowNever(const ConstantRange &L, const ConstantRange &R) {
79 assert(!L.isSignWrappedSet());
80 assert(!R.isSignWrappedSet());
81 if (L.signedAddMayOverflow(R) !=
82 ConstantRange::OverflowResult::NeverOverflows)
83 return ConstantRange::getFull(L.getBitWidth());
84 ConstantRange Result = L.add(R);
85 assert(!Result.isSignWrappedSet());
86 return Result;
87}
88
89ConstantRange unionNoWrap(const ConstantRange &L, const ConstantRange &R) {
90 assert(!L.isSignWrappedSet());
91 assert(!R.isSignWrappedSet());
92 auto Result = L.unionWith(R);
93 // Two non-wrapped sets can produce wrapped.
94 if (Result.isSignWrappedSet())
95 Result = ConstantRange::getFull(Result.getBitWidth());
96 return Result;
97}
98
99/// Describes use of address in as a function call argument.
100template <typename CalleeTy> struct CallInfo {
101 /// Function being called.
102 const CalleeTy *Callee = nullptr;
103 /// Index of argument which pass address.
104 size_t ParamNo = 0;
105
106 CallInfo(const CalleeTy *Callee, size_t ParamNo)
107 : Callee(Callee), ParamNo(ParamNo) {}
108
109 struct Less {
110 bool operator()(const CallInfo &L, const CallInfo &R) const {
111 return std::tie(L.ParamNo, L.Callee) < std::tie(R.ParamNo, R.Callee);
112 }
113 };
114};
115
116/// Describe uses of address (alloca or parameter) inside of the function.
117template <typename CalleeTy> struct UseInfo {
118 // Access range if the address (alloca or parameters).
119 // It is allowed to be empty-set when there are no known accesses.
121 std::set<const Instruction *> UnsafeAccesses;
122
123 // List of calls which pass address as an argument.
124 // Value is offset range of address from base address (alloca or calling
125 // function argument). Range should never set to empty-set, that is an invalid
126 // access range that can cause empty-set to be propagated with
127 // ConstantRange::add
128 using CallsTy = std::map<CallInfo<CalleeTy>, ConstantRange,
129 typename CallInfo<CalleeTy>::Less>;
130 CallsTy Calls;
131
132 UseInfo(unsigned PointerSize) : Range{PointerSize, false} {}
133
134 void updateRange(const ConstantRange &R) { Range = unionNoWrap(Range, R); }
135 void addRange(const Instruction *I, const ConstantRange &R, bool IsSafe) {
136 if (!IsSafe)
137 UnsafeAccesses.insert(I);
138 updateRange(R);
139 }
140};
141
142template <typename CalleeTy>
143raw_ostream &operator<<(raw_ostream &OS, const UseInfo<CalleeTy> &U) {
144 OS << U.Range;
145 for (auto &Call : U.Calls)
146 OS << ", "
147 << "@" << Call.first.Callee->getName() << "(arg" << Call.first.ParamNo
148 << ", " << Call.second << ")";
149 return OS;
150}
151
152/// Calculate the allocation size of a given alloca. Returns empty range
153// in case of confution.
154ConstantRange getStaticAllocaSizeRange(const AllocaInst &AI) {
155 const DataLayout &DL = AI.getModule()->getDataLayout();
156 TypeSize TS = DL.getTypeAllocSize(AI.getAllocatedType());
157 unsigned PointerSize = DL.getPointerTypeSizeInBits(AI.getType());
158 // Fallback to empty range for alloca size.
159 ConstantRange R = ConstantRange::getEmpty(PointerSize);
160 if (TS.isScalable())
161 return R;
162 APInt APSize(PointerSize, TS.getFixedValue(), true);
163 if (APSize.isNonPositive())
164 return R;
165 if (AI.isArrayAllocation()) {
166 const auto *C = dyn_cast<ConstantInt>(AI.getArraySize());
167 if (!C)
168 return R;
169 bool Overflow = false;
170 APInt Mul = C->getValue();
171 if (Mul.isNonPositive())
172 return R;
173 Mul = Mul.sextOrTrunc(PointerSize);
174 APSize = APSize.smul_ov(Mul, Overflow);
175 if (Overflow)
176 return R;
177 }
178 R = ConstantRange(APInt::getZero(PointerSize), APSize);
179 assert(!isUnsafe(R));
180 return R;
181}
182
183template <typename CalleeTy> struct FunctionInfo {
184 std::map<const AllocaInst *, UseInfo<CalleeTy>> Allocas;
185 std::map<uint32_t, UseInfo<CalleeTy>> Params;
186 // TODO: describe return value as depending on one or more of its arguments.
187
188 // StackSafetyDataFlowAnalysis counter stored here for faster access.
189 int UpdateCount = 0;
190
191 void print(raw_ostream &O, StringRef Name, const Function *F) const {
192 // TODO: Consider different printout format after
193 // StackSafetyDataFlowAnalysis. Calls and parameters are irrelevant then.
194 O << " @" << Name << ((F && F->isDSOLocal()) ? "" : " dso_preemptable")
195 << ((F && F->isInterposable()) ? " interposable" : "") << "\n";
196
197 O << " args uses:\n";
198 for (auto &KV : Params) {
199 O << " ";
200 if (F)
201 O << F->getArg(KV.first)->getName();
202 else
203 O << formatv("arg{0}", KV.first);
204 O << "[]: " << KV.second << "\n";
205 }
206
207 O << " allocas uses:\n";
208 if (F) {
209 for (const auto &I : instructions(F)) {
210 if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
211 auto &AS = Allocas.find(AI)->second;
212 O << " " << AI->getName() << "["
213 << getStaticAllocaSizeRange(*AI).getUpper() << "]: " << AS << "\n";
214 }
215 }
216 } else {
217 assert(Allocas.empty());
218 }
219 }
220};
221
222using GVToSSI = std::map<const GlobalValue *, FunctionInfo<GlobalValue>>;
223
224} // namespace
225
227 FunctionInfo<GlobalValue> Info;
228};
229
231 GVToSSI Info;
233 std::set<const Instruction *> UnsafeAccesses;
234};
235
236namespace {
237
238class StackSafetyLocalAnalysis {
239 Function &F;
240 const DataLayout &DL;
241 ScalarEvolution &SE;
242 unsigned PointerSize = 0;
243
244 const ConstantRange UnknownRange;
245
246 /// FIXME: This function is a bandaid, it's only needed
247 /// because this pass doesn't handle address spaces of different pointer
248 /// sizes.
249 ///
250 /// \returns \p Val's SCEV as a pointer of AS zero, or nullptr if it can't be
251 /// converted to AS 0.
252 const SCEV *getSCEVAsPointer(Value *Val);
253
254 ConstantRange offsetFrom(Value *Addr, Value *Base);
255 ConstantRange getAccessRange(Value *Addr, Value *Base,
256 const ConstantRange &SizeRange);
257 ConstantRange getAccessRange(Value *Addr, Value *Base, TypeSize Size);
258 ConstantRange getMemIntrinsicAccessRange(const MemIntrinsic *MI, const Use &U,
259 Value *Base);
260
261 void analyzeAllUses(Value *Ptr, UseInfo<GlobalValue> &AS,
262 const StackLifetime &SL);
263
264
265 bool isSafeAccess(const Use &U, AllocaInst *AI, const SCEV *AccessSize);
266 bool isSafeAccess(const Use &U, AllocaInst *AI, Value *V);
267 bool isSafeAccess(const Use &U, AllocaInst *AI, TypeSize AccessSize);
268
269public:
270 StackSafetyLocalAnalysis(Function &F, ScalarEvolution &SE)
271 : F(F), DL(F.getParent()->getDataLayout()), SE(SE),
272 PointerSize(DL.getPointerSizeInBits()),
273 UnknownRange(PointerSize, true) {}
274
275 // Run the transformation on the associated function.
276 FunctionInfo<GlobalValue> run();
277};
278
279const SCEV *StackSafetyLocalAnalysis::getSCEVAsPointer(Value *Val) {
280 Type *ValTy = Val->getType();
281
282 // We don't handle targets with multiple address spaces.
283 if (!ValTy->isPointerTy()) {
284 auto *PtrTy = PointerType::getUnqual(SE.getContext());
285 return SE.getTruncateOrZeroExtend(SE.getSCEV(Val), PtrTy);
286 }
287
288 if (ValTy->getPointerAddressSpace() != 0)
289 return nullptr;
290 return SE.getSCEV(Val);
291}
292
293ConstantRange StackSafetyLocalAnalysis::offsetFrom(Value *Addr, Value *Base) {
294 if (!SE.isSCEVable(Addr->getType()) || !SE.isSCEVable(Base->getType()))
295 return UnknownRange;
296
297 const SCEV *AddrExp = getSCEVAsPointer(Addr);
298 const SCEV *BaseExp = getSCEVAsPointer(Base);
299 if (!AddrExp || !BaseExp)
300 return UnknownRange;
301
302 const SCEV *Diff = SE.getMinusSCEV(AddrExp, BaseExp);
303 if (isa<SCEVCouldNotCompute>(Diff))
304 return UnknownRange;
305
306 ConstantRange Offset = SE.getSignedRange(Diff);
307 if (isUnsafe(Offset))
308 return UnknownRange;
309 return Offset.sextOrTrunc(PointerSize);
310}
311
313StackSafetyLocalAnalysis::getAccessRange(Value *Addr, Value *Base,
314 const ConstantRange &SizeRange) {
315 // Zero-size loads and stores do not access memory.
316 if (SizeRange.isEmptySet())
317 return ConstantRange::getEmpty(PointerSize);
318 assert(!isUnsafe(SizeRange));
319
320 ConstantRange Offsets = offsetFrom(Addr, Base);
321 if (isUnsafe(Offsets))
322 return UnknownRange;
323
324 Offsets = addOverflowNever(Offsets, SizeRange);
325 if (isUnsafe(Offsets))
326 return UnknownRange;
327 return Offsets;
328}
329
330ConstantRange StackSafetyLocalAnalysis::getAccessRange(Value *Addr, Value *Base,
331 TypeSize Size) {
332 if (Size.isScalable())
333 return UnknownRange;
334 APInt APSize(PointerSize, Size.getFixedValue(), true);
335 if (APSize.isNegative())
336 return UnknownRange;
337 return getAccessRange(Addr, Base,
338 ConstantRange(APInt::getZero(PointerSize), APSize));
339}
340
341ConstantRange StackSafetyLocalAnalysis::getMemIntrinsicAccessRange(
342 const MemIntrinsic *MI, const Use &U, Value *Base) {
343 if (const auto *MTI = dyn_cast<MemTransferInst>(MI)) {
344 if (MTI->getRawSource() != U && MTI->getRawDest() != U)
345 return ConstantRange::getEmpty(PointerSize);
346 } else {
347 if (MI->getRawDest() != U)
348 return ConstantRange::getEmpty(PointerSize);
349 }
350
351 auto *CalculationTy = IntegerType::getIntNTy(SE.getContext(), PointerSize);
352 if (!SE.isSCEVable(MI->getLength()->getType()))
353 return UnknownRange;
354
355 const SCEV *Expr =
356 SE.getTruncateOrZeroExtend(SE.getSCEV(MI->getLength()), CalculationTy);
357 ConstantRange Sizes = SE.getSignedRange(Expr);
358 if (!Sizes.getUpper().isStrictlyPositive() || isUnsafe(Sizes))
359 return UnknownRange;
360 Sizes = Sizes.sextOrTrunc(PointerSize);
361 ConstantRange SizeRange(APInt::getZero(PointerSize), Sizes.getUpper() - 1);
362 return getAccessRange(U, Base, SizeRange);
363}
364
365bool StackSafetyLocalAnalysis::isSafeAccess(const Use &U, AllocaInst *AI,
366 Value *V) {
367 return isSafeAccess(U, AI, SE.getSCEV(V));
368}
369
370bool StackSafetyLocalAnalysis::isSafeAccess(const Use &U, AllocaInst *AI,
371 TypeSize TS) {
372 if (TS.isScalable())
373 return false;
374 auto *CalculationTy = IntegerType::getIntNTy(SE.getContext(), PointerSize);
375 const SCEV *SV = SE.getConstant(CalculationTy, TS.getFixedValue());
376 return isSafeAccess(U, AI, SV);
377}
378
379bool StackSafetyLocalAnalysis::isSafeAccess(const Use &U, AllocaInst *AI,
380 const SCEV *AccessSize) {
381
382 if (!AI)
383 return true; // This only judges whether it is a safe *stack* access.
384 if (isa<SCEVCouldNotCompute>(AccessSize))
385 return false;
386
387 const auto *I = cast<Instruction>(U.getUser());
388
389 const SCEV *AddrExp = getSCEVAsPointer(U.get());
390 const SCEV *BaseExp = getSCEVAsPointer(AI);
391 if (!AddrExp || !BaseExp)
392 return false;
393
394 const SCEV *Diff = SE.getMinusSCEV(AddrExp, BaseExp);
395 if (isa<SCEVCouldNotCompute>(Diff))
396 return false;
397
398 auto Size = getStaticAllocaSizeRange(*AI);
399
400 auto *CalculationTy = IntegerType::getIntNTy(SE.getContext(), PointerSize);
401 auto ToDiffTy = [&](const SCEV *V) {
402 return SE.getTruncateOrZeroExtend(V, CalculationTy);
403 };
404 const SCEV *Min = ToDiffTy(SE.getConstant(Size.getLower()));
405 const SCEV *Max = SE.getMinusSCEV(ToDiffTy(SE.getConstant(Size.getUpper())),
406 ToDiffTy(AccessSize));
407 return SE.evaluatePredicateAt(ICmpInst::Predicate::ICMP_SGE, Diff, Min, I)
408 .value_or(false) &&
409 SE.evaluatePredicateAt(ICmpInst::Predicate::ICMP_SLE, Diff, Max, I)
410 .value_or(false);
411}
412
413/// The function analyzes all local uses of Ptr (alloca or argument) and
414/// calculates local access range and all function calls where it was used.
415void StackSafetyLocalAnalysis::analyzeAllUses(Value *Ptr,
416 UseInfo<GlobalValue> &US,
417 const StackLifetime &SL) {
420 WorkList.push_back(Ptr);
421 AllocaInst *AI = dyn_cast<AllocaInst>(Ptr);
422
423 // A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc.
424 while (!WorkList.empty()) {
425 const Value *V = WorkList.pop_back_val();
426 for (const Use &UI : V->uses()) {
427 const auto *I = cast<Instruction>(UI.getUser());
428 if (!SL.isReachable(I))
429 continue;
430
431 assert(V == UI.get());
432
433 auto RecordStore = [&](const Value* StoredVal) {
434 if (V == StoredVal) {
435 // Stored the pointer - conservatively assume it may be unsafe.
436 US.addRange(I, UnknownRange, /*IsSafe=*/false);
437 return;
438 }
439 if (AI && !SL.isAliveAfter(AI, I)) {
440 US.addRange(I, UnknownRange, /*IsSafe=*/false);
441 return;
442 }
443 auto TypeSize = DL.getTypeStoreSize(StoredVal->getType());
444 auto AccessRange = getAccessRange(UI, Ptr, TypeSize);
445 bool Safe = isSafeAccess(UI, AI, TypeSize);
446 US.addRange(I, AccessRange, Safe);
447 return;
448 };
449
450 switch (I->getOpcode()) {
451 case Instruction::Load: {
452 if (AI && !SL.isAliveAfter(AI, I)) {
453 US.addRange(I, UnknownRange, /*IsSafe=*/false);
454 break;
455 }
456 auto TypeSize = DL.getTypeStoreSize(I->getType());
457 auto AccessRange = getAccessRange(UI, Ptr, TypeSize);
458 bool Safe = isSafeAccess(UI, AI, TypeSize);
459 US.addRange(I, AccessRange, Safe);
460 break;
461 }
462
463 case Instruction::VAArg:
464 // "va-arg" from a pointer is safe.
465 break;
466 case Instruction::Store:
467 RecordStore(cast<StoreInst>(I)->getValueOperand());
468 break;
469 case Instruction::AtomicCmpXchg:
470 RecordStore(cast<AtomicCmpXchgInst>(I)->getNewValOperand());
471 break;
472 case Instruction::AtomicRMW:
473 RecordStore(cast<AtomicRMWInst>(I)->getValOperand());
474 break;
475
476 case Instruction::Ret:
477 // Information leak.
478 // FIXME: Process parameters correctly. This is a leak only if we return
479 // alloca.
480 US.addRange(I, UnknownRange, /*IsSafe=*/false);
481 break;
482
483 case Instruction::Call:
484 case Instruction::Invoke: {
485 if (I->isLifetimeStartOrEnd())
486 break;
487
488 if (AI && !SL.isAliveAfter(AI, I)) {
489 US.addRange(I, UnknownRange, /*IsSafe=*/false);
490 break;
491 }
492 if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
493 auto AccessRange = getMemIntrinsicAccessRange(MI, UI, Ptr);
494 bool Safe = false;
495 if (const auto *MTI = dyn_cast<MemTransferInst>(MI)) {
496 if (MTI->getRawSource() != UI && MTI->getRawDest() != UI)
497 Safe = true;
498 } else if (MI->getRawDest() != UI) {
499 Safe = true;
500 }
501 Safe = Safe || isSafeAccess(UI, AI, MI->getLength());
502 US.addRange(I, AccessRange, Safe);
503 break;
504 }
505
506 const auto &CB = cast<CallBase>(*I);
507 if (CB.getReturnedArgOperand() == V) {
508 if (Visited.insert(I).second)
509 WorkList.push_back(cast<const Instruction>(I));
510 }
511
512 if (!CB.isArgOperand(&UI)) {
513 US.addRange(I, UnknownRange, /*IsSafe=*/false);
514 break;
515 }
516
517 unsigned ArgNo = CB.getArgOperandNo(&UI);
518 if (CB.isByValArgument(ArgNo)) {
519 auto TypeSize = DL.getTypeStoreSize(CB.getParamByValType(ArgNo));
520 auto AccessRange = getAccessRange(UI, Ptr, TypeSize);
521 bool Safe = isSafeAccess(UI, AI, TypeSize);
522 US.addRange(I, AccessRange, Safe);
523 break;
524 }
525
526 // FIXME: consult devirt?
527 // Do not follow aliases, otherwise we could inadvertently follow
528 // dso_preemptable aliases or aliases with interposable linkage.
529 const GlobalValue *Callee =
530 dyn_cast<GlobalValue>(CB.getCalledOperand()->stripPointerCasts());
531 if (!Callee) {
532 US.addRange(I, UnknownRange, /*IsSafe=*/false);
533 break;
534 }
535
536 assert(isa<Function>(Callee) || isa<GlobalAlias>(Callee));
537 ConstantRange Offsets = offsetFrom(UI, Ptr);
538 auto Insert =
539 US.Calls.emplace(CallInfo<GlobalValue>(Callee, ArgNo), Offsets);
540 if (!Insert.second)
541 Insert.first->second = Insert.first->second.unionWith(Offsets);
542 break;
543 }
544
545 default:
546 if (Visited.insert(I).second)
547 WorkList.push_back(cast<const Instruction>(I));
548 }
549 }
550 }
551}
552
553FunctionInfo<GlobalValue> StackSafetyLocalAnalysis::run() {
554 FunctionInfo<GlobalValue> Info;
555 assert(!F.isDeclaration() &&
556 "Can't run StackSafety on a function declaration");
557
558 LLVM_DEBUG(dbgs() << "[StackSafety] " << F.getName() << "\n");
559
561 for (auto &I : instructions(F))
562 if (auto *AI = dyn_cast<AllocaInst>(&I))
563 Allocas.push_back(AI);
564 StackLifetime SL(F, Allocas, StackLifetime::LivenessType::Must);
565 SL.run();
566
567 for (auto *AI : Allocas) {
568 auto &UI = Info.Allocas.emplace(AI, PointerSize).first->second;
569 analyzeAllUses(AI, UI, SL);
570 }
571
572 for (Argument &A : F.args()) {
573 // Non pointers and bypass arguments are not going to be used in any global
574 // processing.
575 if (A.getType()->isPointerTy() && !A.hasByValAttr()) {
576 auto &UI = Info.Params.emplace(A.getArgNo(), PointerSize).first->second;
577 analyzeAllUses(&A, UI, SL);
578 }
579 }
580
581 LLVM_DEBUG(Info.print(dbgs(), F.getName(), &F));
582 LLVM_DEBUG(dbgs() << "\n[StackSafety] done\n");
583 return Info;
584}
585
586template <typename CalleeTy> class StackSafetyDataFlowAnalysis {
587 using FunctionMap = std::map<const CalleeTy *, FunctionInfo<CalleeTy>>;
588
589 FunctionMap Functions;
590 const ConstantRange UnknownRange;
591
592 // Callee-to-Caller multimap.
595
596 bool updateOneUse(UseInfo<CalleeTy> &US, bool UpdateToFullSet);
597 void updateOneNode(const CalleeTy *Callee, FunctionInfo<CalleeTy> &FS);
598 void updateOneNode(const CalleeTy *Callee) {
599 updateOneNode(Callee, Functions.find(Callee)->second);
600 }
601 void updateAllNodes() {
602 for (auto &F : Functions)
603 updateOneNode(F.first, F.second);
604 }
605 void runDataFlow();
606#ifndef NDEBUG
607 void verifyFixedPoint();
608#endif
609
610public:
611 StackSafetyDataFlowAnalysis(uint32_t PointerBitWidth, FunctionMap Functions)
612 : Functions(std::move(Functions)),
613 UnknownRange(ConstantRange::getFull(PointerBitWidth)) {}
614
615 const FunctionMap &run();
616
617 ConstantRange getArgumentAccessRange(const CalleeTy *Callee, unsigned ParamNo,
618 const ConstantRange &Offsets) const;
619};
620
621template <typename CalleeTy>
622ConstantRange StackSafetyDataFlowAnalysis<CalleeTy>::getArgumentAccessRange(
623 const CalleeTy *Callee, unsigned ParamNo,
624 const ConstantRange &Offsets) const {
625 auto FnIt = Functions.find(Callee);
626 // Unknown callee (outside of LTO domain or an indirect call).
627 if (FnIt == Functions.end())
628 return UnknownRange;
629 auto &FS = FnIt->second;
630 auto ParamIt = FS.Params.find(ParamNo);
631 if (ParamIt == FS.Params.end())
632 return UnknownRange;
633 auto &Access = ParamIt->second.Range;
634 if (Access.isEmptySet())
635 return Access;
636 if (Access.isFullSet())
637 return UnknownRange;
638 return addOverflowNever(Access, Offsets);
639}
640
641template <typename CalleeTy>
642bool StackSafetyDataFlowAnalysis<CalleeTy>::updateOneUse(UseInfo<CalleeTy> &US,
643 bool UpdateToFullSet) {
644 bool Changed = false;
645 for (auto &KV : US.Calls) {
646 assert(!KV.second.isEmptySet() &&
647 "Param range can't be empty-set, invalid offset range");
648
649 ConstantRange CalleeRange =
650 getArgumentAccessRange(KV.first.Callee, KV.first.ParamNo, KV.second);
651 if (!US.Range.contains(CalleeRange)) {
652 Changed = true;
653 if (UpdateToFullSet)
654 US.Range = UnknownRange;
655 else
656 US.updateRange(CalleeRange);
657 }
658 }
659 return Changed;
660}
661
662template <typename CalleeTy>
663void StackSafetyDataFlowAnalysis<CalleeTy>::updateOneNode(
664 const CalleeTy *Callee, FunctionInfo<CalleeTy> &FS) {
665 bool UpdateToFullSet = FS.UpdateCount > StackSafetyMaxIterations;
666 bool Changed = false;
667 for (auto &KV : FS.Params)
668 Changed |= updateOneUse(KV.second, UpdateToFullSet);
669
670 if (Changed) {
671 LLVM_DEBUG(dbgs() << "=== update [" << FS.UpdateCount
672 << (UpdateToFullSet ? ", full-set" : "") << "] " << &FS
673 << "\n");
674 // Callers of this function may need updating.
675 for (auto &CallerID : Callers[Callee])
676 WorkList.insert(CallerID);
677
678 ++FS.UpdateCount;
679 }
680}
681
682template <typename CalleeTy>
683void StackSafetyDataFlowAnalysis<CalleeTy>::runDataFlow() {
685 for (auto &F : Functions) {
686 Callees.clear();
687 auto &FS = F.second;
688 for (auto &KV : FS.Params)
689 for (auto &CS : KV.second.Calls)
690 Callees.push_back(CS.first.Callee);
691
692 llvm::sort(Callees);
693 Callees.erase(std::unique(Callees.begin(), Callees.end()), Callees.end());
694
695 for (auto &Callee : Callees)
696 Callers[Callee].push_back(F.first);
697 }
698
699 updateAllNodes();
700
701 while (!WorkList.empty()) {
702 const CalleeTy *Callee = WorkList.pop_back_val();
703 updateOneNode(Callee);
704 }
705}
706
707#ifndef NDEBUG
708template <typename CalleeTy>
709void StackSafetyDataFlowAnalysis<CalleeTy>::verifyFixedPoint() {
710 WorkList.clear();
711 updateAllNodes();
712 assert(WorkList.empty());
713}
714#endif
715
716template <typename CalleeTy>
717const typename StackSafetyDataFlowAnalysis<CalleeTy>::FunctionMap &
718StackSafetyDataFlowAnalysis<CalleeTy>::run() {
719 runDataFlow();
720 LLVM_DEBUG(verifyFixedPoint());
721 return Functions;
722}
723
724FunctionSummary *findCalleeFunctionSummary(ValueInfo VI, StringRef ModuleId) {
725 if (!VI)
726 return nullptr;
727 auto SummaryList = VI.getSummaryList();
728 GlobalValueSummary* S = nullptr;
729 for (const auto& GVS : SummaryList) {
730 if (!GVS->isLive())
731 continue;
732 if (const AliasSummary *AS = dyn_cast<AliasSummary>(GVS.get()))
733 if (!AS->hasAliasee())
734 continue;
735 if (!isa<FunctionSummary>(GVS->getBaseObject()))
736 continue;
737 if (GlobalValue::isLocalLinkage(GVS->linkage())) {
738 if (GVS->modulePath() == ModuleId) {
739 S = GVS.get();
740 break;
741 }
742 } else if (GlobalValue::isExternalLinkage(GVS->linkage())) {
743 if (S) {
744 ++NumIndexCalleeMultipleExternal;
745 return nullptr;
746 }
747 S = GVS.get();
748 } else if (GlobalValue::isWeakLinkage(GVS->linkage())) {
749 if (S) {
750 ++NumIndexCalleeMultipleWeak;
751 return nullptr;
752 }
753 S = GVS.get();
754 } else if (GlobalValue::isAvailableExternallyLinkage(GVS->linkage()) ||
755 GlobalValue::isLinkOnceLinkage(GVS->linkage())) {
756 if (SummaryList.size() == 1)
757 S = GVS.get();
758 // According thinLTOResolvePrevailingGUID these are unlikely prevailing.
759 } else {
760 ++NumIndexCalleeUnhandled;
761 }
762 };
763 while (S) {
764 if (!S->isLive() || !S->isDSOLocal())
765 return nullptr;
766 if (FunctionSummary *FS = dyn_cast<FunctionSummary>(S))
767 return FS;
768 AliasSummary *AS = dyn_cast<AliasSummary>(S);
769 if (!AS || !AS->hasAliasee())
770 return nullptr;
771 S = AS->getBaseObject();
772 if (S == AS)
773 return nullptr;
774 }
775 return nullptr;
776}
777
778const Function *findCalleeInModule(const GlobalValue *GV) {
779 while (GV) {
780 if (GV->isDeclaration() || GV->isInterposable() || !GV->isDSOLocal())
781 return nullptr;
782 if (const Function *F = dyn_cast<Function>(GV))
783 return F;
784 const GlobalAlias *A = dyn_cast<GlobalAlias>(GV);
785 if (!A)
786 return nullptr;
787 GV = A->getAliaseeObject();
788 if (GV == A)
789 return nullptr;
790 }
791 return nullptr;
792}
793
794const ConstantRange *findParamAccess(const FunctionSummary &FS,
795 uint32_t ParamNo) {
796 assert(FS.isLive());
797 assert(FS.isDSOLocal());
798 for (const auto &PS : FS.paramAccesses())
799 if (ParamNo == PS.ParamNo)
800 return &PS.Use;
801 return nullptr;
802}
803
804void resolveAllCalls(UseInfo<GlobalValue> &Use,
805 const ModuleSummaryIndex *Index) {
806 ConstantRange FullSet(Use.Range.getBitWidth(), true);
807 // Move Use.Calls to a temp storage and repopulate - don't use std::move as it
808 // leaves Use.Calls in an undefined state.
809 UseInfo<GlobalValue>::CallsTy TmpCalls;
810 std::swap(TmpCalls, Use.Calls);
811 for (const auto &C : TmpCalls) {
812 const Function *F = findCalleeInModule(C.first.Callee);
813 if (F) {
814 Use.Calls.emplace(CallInfo<GlobalValue>(F, C.first.ParamNo), C.second);
815 continue;
816 }
817
818 if (!Index)
819 return Use.updateRange(FullSet);
821 findCalleeFunctionSummary(Index->getValueInfo(C.first.Callee->getGUID()),
822 C.first.Callee->getParent()->getModuleIdentifier());
823 ++NumModuleCalleeLookupTotal;
824 if (!FS) {
825 ++NumModuleCalleeLookupFailed;
826 return Use.updateRange(FullSet);
827 }
828 const ConstantRange *Found = findParamAccess(*FS, C.first.ParamNo);
829 if (!Found || Found->isFullSet())
830 return Use.updateRange(FullSet);
831 ConstantRange Access = Found->sextOrTrunc(Use.Range.getBitWidth());
832 if (!Access.isEmptySet())
833 Use.updateRange(addOverflowNever(Access, C.second));
834 }
835}
836
837GVToSSI createGlobalStackSafetyInfo(
838 std::map<const GlobalValue *, FunctionInfo<GlobalValue>> Functions,
839 const ModuleSummaryIndex *Index) {
840 GVToSSI SSI;
841 if (Functions.empty())
842 return SSI;
843
844 // FIXME: Simplify printing and remove copying here.
845 auto Copy = Functions;
846
847 for (auto &FnKV : Copy)
848 for (auto &KV : FnKV.second.Params) {
849 resolveAllCalls(KV.second, Index);
850 if (KV.second.Range.isFullSet())
851 KV.second.Calls.clear();
852 }
853
855 Copy.begin()->first->getParent()->getDataLayout().getPointerSizeInBits();
856 StackSafetyDataFlowAnalysis<GlobalValue> SSDFA(PointerSize, std::move(Copy));
857
858 for (const auto &F : SSDFA.run()) {
859 auto FI = F.second;
860 auto &SrcF = Functions[F.first];
861 for (auto &KV : FI.Allocas) {
862 auto &A = KV.second;
863 resolveAllCalls(A, Index);
864 for (auto &C : A.Calls) {
865 A.updateRange(SSDFA.getArgumentAccessRange(C.first.Callee,
866 C.first.ParamNo, C.second));
867 }
868 // FIXME: This is needed only to preserve calls in print() results.
869 A.Calls = SrcF.Allocas.find(KV.first)->second.Calls;
870 }
871 for (auto &KV : FI.Params) {
872 auto &P = KV.second;
873 P.Calls = SrcF.Params.find(KV.first)->second.Calls;
874 }
875 SSI[F.first] = std::move(FI);
876 }
877
878 return SSI;
879}
880
881} // end anonymous namespace
882
884
886 std::function<ScalarEvolution &()> GetSE)
887 : F(F), GetSE(GetSE) {}
888
890
892
894
896 if (!Info) {
897 StackSafetyLocalAnalysis SSLA(*F, GetSE());
898 Info.reset(new InfoTy{SSLA.run()});
899 }
900 return *Info;
901}
902
904 getInfo().Info.print(O, F->getName(), dyn_cast<Function>(F));
905 O << "\n";
906}
907
908const StackSafetyGlobalInfo::InfoTy &StackSafetyGlobalInfo::getInfo() const {
909 if (!Info) {
910 std::map<const GlobalValue *, FunctionInfo<GlobalValue>> Functions;
911 for (auto &F : M->functions()) {
912 if (!F.isDeclaration()) {
913 auto FI = GetSSI(F).getInfo().Info;
914 Functions.emplace(&F, std::move(FI));
915 }
916 }
917 Info.reset(new InfoTy{
918 createGlobalStackSafetyInfo(std::move(Functions), Index), {}, {}});
919
920 for (auto &FnKV : Info->Info) {
921 for (auto &KV : FnKV.second.Allocas) {
922 ++NumAllocaTotal;
923 const AllocaInst *AI = KV.first;
924 auto AIRange = getStaticAllocaSizeRange(*AI);
925 if (AIRange.contains(KV.second.Range)) {
926 Info->SafeAllocas.insert(AI);
927 ++NumAllocaStackSafe;
928 }
929 Info->UnsafeAccesses.insert(KV.second.UnsafeAccesses.begin(),
930 KV.second.UnsafeAccesses.end());
931 }
932 }
933
935 print(errs());
936 }
937 return *Info;
938}
939
940std::vector<FunctionSummary::ParamAccess>
942 // Implementation transforms internal representation of parameter information
943 // into FunctionSummary format.
944 std::vector<FunctionSummary::ParamAccess> ParamAccesses;
945 for (const auto &KV : getInfo().Info.Params) {
946 auto &PS = KV.second;
947 // Parameter accessed by any or unknown offset, represented as FullSet by
948 // StackSafety, is handled as the parameter for which we have no
949 // StackSafety info at all. So drop it to reduce summary size.
950 if (PS.Range.isFullSet())
951 continue;
952
953 ParamAccesses.emplace_back(KV.first, PS.Range);
954 FunctionSummary::ParamAccess &Param = ParamAccesses.back();
955
956 Param.Calls.reserve(PS.Calls.size());
957 for (const auto &C : PS.Calls) {
958 // Parameter forwarded into another function by any or unknown offset
959 // will make ParamAccess::Range as FullSet anyway. So we can drop the
960 // entire parameter like we did above.
961 // TODO(vitalybuka): Return already filtered parameters from getInfo().
962 if (C.second.isFullSet()) {
963 ParamAccesses.pop_back();
964 break;
965 }
966 Param.Calls.emplace_back(C.first.ParamNo,
967 Index.getOrInsertValueInfo(C.first.Callee),
968 C.second);
969 }
970 }
971 for (FunctionSummary::ParamAccess &Param : ParamAccesses) {
972 sort(Param.Calls, [](const FunctionSummary::ParamAccess::Call &L,
974 return std::tie(L.ParamNo, L.Callee) < std::tie(R.ParamNo, R.Callee);
975 });
976 }
977 return ParamAccesses;
978}
979
981
983 Module *M, std::function<const StackSafetyInfo &(Function &F)> GetSSI,
985 : M(M), GetSSI(GetSSI), Index(Index) {
986 if (StackSafetyRun)
987 getInfo();
988}
989
991 default;
992
995
997
999 const auto &Info = getInfo();
1000 return Info.SafeAllocas.count(&AI);
1001}
1002
1004 const auto &Info = getInfo();
1005 return Info.UnsafeAccesses.find(&I) == Info.UnsafeAccesses.end();
1006}
1007
1009 auto &SSI = getInfo().Info;
1010 if (SSI.empty())
1011 return;
1012 const Module &M = *SSI.begin()->first->getParent();
1013 for (const auto &F : M.functions()) {
1014 if (!F.isDeclaration()) {
1015 SSI.find(&F)->second.print(O, F.getName(), &F);
1016 O << " safe accesses:"
1017 << "\n";
1018 for (const auto &I : instructions(F)) {
1019 const CallInst *Call = dyn_cast<CallInst>(&I);
1020 if ((isa<StoreInst>(I) || isa<LoadInst>(I) || isa<MemIntrinsic>(I) ||
1021 isa<AtomicCmpXchgInst>(I) || isa<AtomicRMWInst>(I) ||
1022 (Call && Call->hasByValArgument())) &&
1024 O << " " << I << "\n";
1025 }
1026 }
1027 O << "\n";
1028 }
1029 }
1030}
1031
1033
1034AnalysisKey StackSafetyAnalysis::Key;
1035
1038 return StackSafetyInfo(&F, [&AM, &F]() -> ScalarEvolution & {
1040 });
1041}
1042
1045 OS << "'Stack Safety Local Analysis' for function '" << F.getName() << "'\n";
1047 return PreservedAnalyses::all();
1048}
1049
1051
1054}
1055
1058 AU.setPreservesAll();
1059}
1060
1062 SSI.print(O);
1063}
1064
1066 auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
1067 SSI = {&F, [SE]() -> ScalarEvolution & { return *SE; }};
1068 return false;
1069}
1070
1071AnalysisKey StackSafetyGlobalAnalysis::Key;
1072
1075 // FIXME: Lookup Module Summary.
1078 return {&M,
1079 [&FAM](Function &F) -> const StackSafetyInfo & {
1081 },
1082 nullptr};
1083}
1084
1087 OS << "'Stack Safety Analysis' for module '" << M.getName() << "'\n";
1089 return PreservedAnalyses::all();
1090}
1091
1093
1095 : ModulePass(ID) {
1098}
1099
1101
1103 const Module *M) const {
1104 SSGI.print(O);
1105}
1106
1108 AnalysisUsage &AU) const {
1109 AU.setPreservesAll();
1111}
1112
1114 const ModuleSummaryIndex *ImportSummary = nullptr;
1115 if (auto *IndexWrapperPass =
1116 getAnalysisIfAvailable<ImmutableModuleSummaryIndexWrapperPass>())
1117 ImportSummary = IndexWrapperPass->getIndex();
1118
1119 SSGI = {&M,
1120 [this](Function &F) -> const StackSafetyInfo & {
1121 return getAnalysis<StackSafetyInfoWrapperPass>(F).getResult();
1122 },
1123 ImportSummary};
1124 return false;
1125}
1126
1128 if (StackSafetyRun)
1129 return true;
1130 for (const auto &F : M.functions())
1131 if (F.hasFnAttribute(Attribute::SanitizeMemTag))
1132 return true;
1133 return false;
1134}
1135
1137 if (!Index.hasParamAccess())
1138 return;
1140
1141 auto CountParamAccesses = [&](auto &Stat) {
1142 if (!AreStatisticsEnabled())
1143 return;
1144 for (auto &GVS : Index)
1145 for (auto &GV : GVS.second.SummaryList)
1146 if (FunctionSummary *FS = dyn_cast<FunctionSummary>(GV.get()))
1147 Stat += FS->paramAccesses().size();
1148 };
1149
1150 CountParamAccesses(NumCombinedParamAccessesBefore);
1151
1152 std::map<const FunctionSummary *, FunctionInfo<FunctionSummary>> Functions;
1153
1154 // Convert the ModuleSummaryIndex to a FunctionMap
1155 for (auto &GVS : Index) {
1156 for (auto &GV : GVS.second.SummaryList) {
1157 FunctionSummary *FS = dyn_cast<FunctionSummary>(GV.get());
1158 if (!FS || FS->paramAccesses().empty())
1159 continue;
1160 if (FS->isLive() && FS->isDSOLocal()) {
1161 FunctionInfo<FunctionSummary> FI;
1162 for (const auto &PS : FS->paramAccesses()) {
1163 auto &US =
1164 FI.Params
1165 .emplace(PS.ParamNo, FunctionSummary::ParamAccess::RangeWidth)
1166 .first->second;
1167 US.Range = PS.Use;
1168 for (const auto &Call : PS.Calls) {
1169 assert(!Call.Offsets.isFullSet());
1170 FunctionSummary *S =
1171 findCalleeFunctionSummary(Call.Callee, FS->modulePath());
1172 ++NumCombinedCalleeLookupTotal;
1173 if (!S) {
1174 ++NumCombinedCalleeLookupFailed;
1175 US.Range = FullSet;
1176 US.Calls.clear();
1177 break;
1178 }
1179 US.Calls.emplace(CallInfo<FunctionSummary>(S, Call.ParamNo),
1180 Call.Offsets);
1181 }
1182 }
1183 Functions.emplace(FS, std::move(FI));
1184 }
1185 // Reset data for all summaries. Alive and DSO local will be set back from
1186 // of data flow results below. Anything else will not be accessed
1187 // by ThinLTO backend, so we can save on bitcode size.
1188 FS->setParamAccesses({});
1189 }
1190 }
1191 NumCombinedDataFlowNodes += Functions.size();
1192 StackSafetyDataFlowAnalysis<FunctionSummary> SSDFA(
1193 FunctionSummary::ParamAccess::RangeWidth, std::move(Functions));
1194 for (const auto &KV : SSDFA.run()) {
1195 std::vector<FunctionSummary::ParamAccess> NewParams;
1196 NewParams.reserve(KV.second.Params.size());
1197 for (const auto &Param : KV.second.Params) {
1198 // It's not needed as FullSet is processed the same as a missing value.
1199 if (Param.second.Range.isFullSet())
1200 continue;
1201 NewParams.emplace_back();
1202 FunctionSummary::ParamAccess &New = NewParams.back();
1203 New.ParamNo = Param.first;
1204 New.Use = Param.second.Range; // Only range is needed.
1205 }
1206 const_cast<FunctionSummary *>(KV.first)->setParamAccesses(
1207 std::move(NewParams));
1208 }
1209
1210 CountParamAccesses(NumCombinedParamAccessesAfter);
1211}
1212
1213static const char LocalPassArg[] = "stack-safety-local";
1214static const char LocalPassName[] = "Stack Safety Local Analysis";
1216 false, true)
1220
1221static const char GlobalPassName[] = "Stack Safety Analysis";
aarch64 promote const
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements a class to represent arbitrary precision integral constant values and operations...
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
Expand Atomic instructions
basic Basic Alias true
block Block Frequency Analysis
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:529
#define LLVM_DEBUG(X)
Definition: Debug.h:101
uint64_t Addr
std::string Name
uint64_t Size
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static void addRange(SmallVectorImpl< ConstantInt * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:1269
This is the interface to build a ModuleSummaryIndex for a module.
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
#define P(N)
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static const char LocalPassArg[]
static const char LocalPassName[]
static true const char GlobalPassName[]
static cl::opt< int > StackSafetyMaxIterations("stack-safety-max-iterations", cl::init(20), cl::Hidden)
static cl::opt< bool > StackSafetyRun("stack-safety-run", cl::init(false), cl::Hidden)
static cl::opt< bool > StackSafetyPrint("stack-safety-print", cl::init(false), cl::Hidden)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
BinaryOperator * Mul
Class for arbitrary precision integers.
Definition: APInt.h:76
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:178
Alias summary information.
an instruction to allocate memory on the stack
Definition: Instructions.h:59
PointerType * getType() const
Overload to return most specific pointer type.
Definition: Instructions.h:107
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Definition: Instructions.h:125
bool isArrayAllocation() const
Return true if there is an allocation size parameter to the allocation instruction that is not 1.
const Value * getArraySize() const
Get the number of elements allocated.
Definition: Instructions.h:103
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:473
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage & addRequiredTransitive()
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
This class represents a function call, abstracting a target machine's calling convention.
This class represents a range of values.
Definition: ConstantRange.h:47
bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type.
bool isEmptySet() const
Return true if this set contains no members.
ConstantRange sextOrTrunc(uint32_t BitWidth) const
Make this range have the bit width given by BitWidth.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
Function summary information to aid decisions and implementation of importing.
Function and variable summary information to aid decisions and implementation of importing.
GlobalValueSummary * getBaseObject()
If this is an alias summary, returns the summary of the aliased object (a global variable or function...
bool isDSOLocal() const
Definition: GlobalValue.h:305
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:409
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:281
static bool isLinkOnceLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:388
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:379
static bool isExternalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:376
bool isInterposable() const
Return true if this global's definition can be substituted with an arbitrary definition at link time ...
Definition: Globals.cpp:100
static bool isWeakLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:397
Legacy wrapper pass to provide the ModuleSummaryIndex object.
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:631
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:82
This is the common base class for memset/memcpy/memmove.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
iterator_range< iterator > functions()
Definition: Module.h:721
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.h:293
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
This class represents an analyzed expression in the program.
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
A vector that has set insertion semantics.
Definition: SetVector.h:57
void clear()
Completely clear the SetVector.
Definition: SetVector.h:273
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:93
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
value_type pop_back_val()
Definition: SetVector.h:285
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
bool empty() const
Definition: SmallVector.h:94
iterator erase(const_iterator CI)
Definition: SmallVector.h:750
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
Compute live ranges of allocas.
Definition: StackLifetime.h:37
bool isReachable(const Instruction *I) const
Returns true if instruction is reachable from entry.
bool isAliveAfter(const AllocaInst *AI, const Instruction *I) const
Returns true if the alloca is alive after the instruction.
StackSafetyInfo wrapper for the new pass manager.
StackSafetyInfo run(Function &F, FunctionAnalysisManager &AM)
This pass performs the global (interprocedural) stack safety analysis (new pass manager).
Result run(Module &M, ModuleAnalysisManager &AM)
This pass performs the global (interprocedural) stack safety analysis (legacy pass manager).
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void print(raw_ostream &O, const Module *M) const override
print - Print out the internal state of the pass.
void print(raw_ostream &O) const
bool stackAccessIsSafe(const Instruction &I) const
bool isSafe(const AllocaInst &AI) const
StackSafetyGlobalInfo & operator=(StackSafetyGlobalInfo &&)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
StackSafetyInfo wrapper for the legacy pass manager.
void print(raw_ostream &O, const Module *M) const override
print - Print out the internal state of the pass.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Interface to access stack safety analysis results for single function.
void print(raw_ostream &O) const
const InfoTy & getInfo() const
StackSafetyInfo & operator=(StackSafetyInfo &&)
std::vector< FunctionSummary::ParamAccess > getParamAccesses(ModuleSummaryIndex &Index) const
Parameters use for a FunctionSummary.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:255
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:187
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Offsets
Offsets in bytes from the start of the input buffer.
Definition: SIInstrInfo.h:1522
@ FS
Definition: X86.h:206
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
void generateParamAccessSummary(ModuleSummaryIndex &Index)
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
bool needsParamAccessSummary(const Module &M)
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
bool AreStatisticsEnabled()
Check if statistics are enabled.
Definition: Statistic.cpp:139
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void initializeStackSafetyGlobalInfoWrapperPassPass(PassRegistry &)
void initializeStackSafetyInfoWrapperPassPass(PassRegistry &)
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:293
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
std::set< const Instruction * > UnsafeAccesses
SmallPtrSet< const AllocaInst *, 8 > SafeAllocas
FunctionInfo< GlobalValue > Info
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:26
Describes the use of a value in a call instruction, specifying the call's target, the value's paramet...
Describes the uses of a parameter by the function.
static constexpr uint32_t RangeWidth
Struct that holds a reference to a particular GUID in a global value summary.