2018-11-26 22:57:47 +01:00
|
|
|
//===- StackSafetyAnalysis.cpp - Stack memory safety analysis -------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-11-26 22:57:47 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/StackSafetyAnalysis.h"
|
2020-05-27 01:53:02 +02:00
|
|
|
#include "llvm/ADT/APInt.h"
|
2020-05-29 02:26:15 +02:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2020-06-22 09:45:08 +02:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2020-06-04 01:12:08 +02:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2020-06-02 10:19:57 +02:00
|
|
|
#include "llvm/Analysis/ModuleSummaryAnalysis.h"
|
2018-11-26 22:57:47 +01:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2020-06-22 09:45:08 +02:00
|
|
|
#include "llvm/Analysis/StackLifetime.h"
|
2020-05-27 01:53:02 +02:00
|
|
|
#include "llvm/IR/ConstantRange.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/GlobalValue.h"
|
2018-11-26 22:57:59 +01:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2020-05-27 01:53:02 +02:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2018-11-26 22:57:59 +01:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2020-08-14 21:42:21 +02:00
|
|
|
#include "llvm/IR/ModuleSummaryIndex.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2020-05-24 11:44:31 +02:00
|
|
|
#include "llvm/Support/Casting.h"
|
2019-11-15 00:15:48 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2020-06-01 09:26:29 +02:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2018-11-26 22:57:47 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2020-05-27 01:53:02 +02:00
|
|
|
#include <algorithm>
|
2020-05-24 07:47:21 +02:00
|
|
|
#include <memory>
|
2018-11-26 22:57:47 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "stack-safety"
|
|
|
|
|
2020-06-04 01:12:08 +02:00
|
|
|
STATISTIC(NumAllocaStackSafe, "Number of safe allocas");
|
|
|
|
STATISTIC(NumAllocaTotal, "Number of total allocas");
|
|
|
|
|
2020-08-06 14:19:12 +02:00
|
|
|
STATISTIC(NumCombinedCalleeLookupTotal,
|
|
|
|
"Number of total callee lookups on combined index.");
|
|
|
|
STATISTIC(NumCombinedCalleeLookupFailed,
|
|
|
|
"Number of failed callee lookups on combined index.");
|
|
|
|
STATISTIC(NumModuleCalleeLookupTotal,
|
|
|
|
"Number of total callee lookups on module index.");
|
|
|
|
STATISTIC(NumModuleCalleeLookupFailed,
|
|
|
|
"Number of failed callee lookups on module index.");
|
2020-08-07 22:58:10 +02:00
|
|
|
STATISTIC(NumCombinedParamAccessesBefore,
|
|
|
|
"Number of total param accesses before generateParamAccessSummary.");
|
|
|
|
STATISTIC(NumCombinedParamAccessesAfter,
|
|
|
|
"Number of total param accesses after generateParamAccessSummary.");
|
2020-08-08 23:00:42 +02:00
|
|
|
STATISTIC(NumCombinedDataFlowNodes,
|
|
|
|
"Number of total nodes in combined index for dataflow processing.");
|
2020-08-06 14:28:03 +02:00
|
|
|
STATISTIC(NumIndexCalleeUnhandled, "Number of index callee which are unhandled.");
|
|
|
|
STATISTIC(NumIndexCalleeMultipleWeak, "Number of index callee non-unique weak.");
|
|
|
|
STATISTIC(NumIndexCalleeMultipleExternal, "Number of index callee non-unique external.");
|
|
|
|
|
2020-08-06 14:19:12 +02:00
|
|
|
|
2018-11-27 00:05:58 +01:00
|
|
|
static cl::opt<int> StackSafetyMaxIterations("stack-safety-max-iterations",
|
|
|
|
cl::init(20), cl::Hidden);
|
|
|
|
|
2020-06-04 11:38:58 +02:00
|
|
|
static cl::opt<bool> StackSafetyPrint("stack-safety-print", cl::init(false),
|
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<bool> StackSafetyRun("stack-safety-run", cl::init(false),
|
|
|
|
cl::Hidden);
|
2020-05-29 00:36:17 +02:00
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
namespace {
|
|
|
|
|
2020-08-08 11:05:04 +02:00
|
|
|
// Check if we should bailout for such ranges.
|
|
|
|
bool isUnsafe(const ConstantRange &R) {
|
|
|
|
return R.isEmptySet() || R.isFullSet() || R.isUpperSignWrapped();
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantRange addOverflowNever(const ConstantRange &L, const ConstantRange &R) {
|
|
|
|
assert(!L.isSignWrappedSet());
|
|
|
|
assert(!R.isSignWrappedSet());
|
|
|
|
if (L.signedAddMayOverflow(R) !=
|
|
|
|
ConstantRange::OverflowResult::NeverOverflows)
|
|
|
|
return ConstantRange::getFull(L.getBitWidth());
|
|
|
|
ConstantRange Result = L.add(R);
|
|
|
|
assert(!Result.isSignWrappedSet());
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantRange unionNoWrap(const ConstantRange &L, const ConstantRange &R) {
|
|
|
|
assert(!L.isSignWrappedSet());
|
|
|
|
assert(!R.isSignWrappedSet());
|
|
|
|
auto Result = L.unionWith(R);
|
|
|
|
// Two non-wrapped sets can produce wrapped.
|
|
|
|
if (Result.isSignWrappedSet())
|
|
|
|
Result = ConstantRange::getFull(Result.getBitWidth());
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
/// Describes use of address in as a function call argument.
|
2020-06-02 11:51:35 +02:00
|
|
|
template <typename CalleeTy> struct CallInfo {
|
2018-11-26 22:57:59 +01:00
|
|
|
/// Function being called.
|
2020-06-02 11:51:35 +02:00
|
|
|
const CalleeTy *Callee = nullptr;
|
2018-11-26 22:57:59 +01:00
|
|
|
/// Index of argument which pass address.
|
|
|
|
size_t ParamNo = 0;
|
|
|
|
|
2020-08-14 10:14:02 +02:00
|
|
|
CallInfo(const CalleeTy *Callee, size_t ParamNo)
|
|
|
|
: Callee(Callee), ParamNo(ParamNo) {}
|
|
|
|
|
|
|
|
struct Less {
|
|
|
|
bool operator()(const CallInfo &L, const CallInfo &R) const {
|
2020-08-14 13:23:15 +02:00
|
|
|
return std::tie(L.ParamNo, L.Callee) < std::tie(R.ParamNo, R.Callee);
|
2020-08-14 10:14:02 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2018-11-26 22:57:59 +01:00
|
|
|
|
|
|
|
/// Describe uses of address (alloca or parameter) inside of the function.
|
2020-06-02 11:51:35 +02:00
|
|
|
template <typename CalleeTy> struct UseInfo {
|
2018-11-26 22:57:59 +01:00
|
|
|
// Access range if the address (alloca or parameters).
|
|
|
|
// It is allowed to be empty-set when there are no known accesses.
|
|
|
|
ConstantRange Range;
|
|
|
|
|
|
|
|
// List of calls which pass address as an argument.
|
2020-08-14 10:14:02 +02:00
|
|
|
// Value is offset range of address from base address (alloca or calling
|
|
|
|
// function argument). Range should never set to empty-set, that is an invalid
|
|
|
|
// access range that can cause empty-set to be propagated with
|
|
|
|
// ConstantRange::add
|
2020-09-19 02:08:21 +02:00
|
|
|
using CallsTy = std::map<CallInfo<CalleeTy>, ConstantRange,
|
|
|
|
typename CallInfo<CalleeTy>::Less>;
|
|
|
|
CallsTy Calls;
|
2018-11-26 22:57:59 +01:00
|
|
|
|
2020-05-30 00:10:50 +02:00
|
|
|
UseInfo(unsigned PointerSize) : Range{PointerSize, false} {}
|
2018-11-26 22:57:59 +01:00
|
|
|
|
2020-08-08 11:05:04 +02:00
|
|
|
void updateRange(const ConstantRange &R) { Range = unionNoWrap(Range, R); }
|
2018-11-26 22:57:59 +01:00
|
|
|
};
|
|
|
|
|
2020-06-02 11:51:35 +02:00
|
|
|
template <typename CalleeTy>
|
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const UseInfo<CalleeTy> &U) {
|
2018-11-26 22:57:59 +01:00
|
|
|
OS << U.Range;
|
|
|
|
for (auto &Call : U.Calls)
|
2020-08-14 10:14:02 +02:00
|
|
|
OS << ", "
|
|
|
|
<< "@" << Call.first.Callee->getName() << "(arg" << Call.first.ParamNo
|
|
|
|
<< ", " << Call.second << ")";
|
2018-11-26 22:57:59 +01:00
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:41:17 +02:00
|
|
|
/// Calculate the allocation size of a given alloca. Returns empty range
|
|
|
|
// in case of confution.
|
|
|
|
ConstantRange getStaticAllocaSizeRange(const AllocaInst &AI) {
|
|
|
|
const DataLayout &DL = AI.getModule()->getDataLayout();
|
|
|
|
TypeSize TS = DL.getTypeAllocSize(AI.getAllocatedType());
|
|
|
|
unsigned PointerSize = DL.getMaxPointerSizeInBits();
|
|
|
|
// Fallback to empty range for alloca size.
|
|
|
|
ConstantRange R = ConstantRange::getEmpty(PointerSize);
|
2020-01-25 01:36:13 +01:00
|
|
|
if (TS.isScalable())
|
2020-05-28 02:41:17 +02:00
|
|
|
return R;
|
|
|
|
APInt APSize(PointerSize, TS.getFixedSize(), true);
|
|
|
|
if (APSize.isNonPositive())
|
|
|
|
return R;
|
|
|
|
if (AI.isArrayAllocation()) {
|
2020-05-28 23:25:44 +02:00
|
|
|
const auto *C = dyn_cast<ConstantInt>(AI.getArraySize());
|
2018-11-26 22:57:59 +01:00
|
|
|
if (!C)
|
2020-05-28 02:41:17 +02:00
|
|
|
return R;
|
|
|
|
bool Overflow = false;
|
|
|
|
APInt Mul = C->getValue();
|
|
|
|
if (Mul.isNonPositive())
|
|
|
|
return R;
|
|
|
|
Mul = Mul.sextOrTrunc(PointerSize);
|
|
|
|
APSize = APSize.smul_ov(Mul, Overflow);
|
|
|
|
if (Overflow)
|
|
|
|
return R;
|
2018-11-26 22:57:59 +01:00
|
|
|
}
|
2020-05-28 02:41:17 +02:00
|
|
|
R = ConstantRange(APInt::getNullValue(PointerSize), APSize);
|
|
|
|
assert(!isUnsafe(R));
|
|
|
|
return R;
|
2018-11-26 22:57:59 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 11:51:35 +02:00
|
|
|
template <typename CalleeTy> struct FunctionInfo {
|
|
|
|
std::map<const AllocaInst *, UseInfo<CalleeTy>> Allocas;
|
|
|
|
std::map<uint32_t, UseInfo<CalleeTy>> Params;
|
2018-11-26 22:57:59 +01:00
|
|
|
// TODO: describe return value as depending on one or more of its arguments.
|
|
|
|
|
2018-11-27 00:05:58 +01:00
|
|
|
// StackSafetyDataFlowAnalysis counter stored here for faster access.
|
|
|
|
int UpdateCount = 0;
|
|
|
|
|
2020-05-24 11:44:31 +02:00
|
|
|
void print(raw_ostream &O, StringRef Name, const Function *F) const {
|
2018-11-27 00:05:58 +01:00
|
|
|
// TODO: Consider different printout format after
|
|
|
|
// StackSafetyDataFlowAnalysis. Calls and parameters are irrelevant then.
|
2020-05-28 07:21:39 +02:00
|
|
|
O << " @" << Name << ((F && F->isDSOLocal()) ? "" : " dso_preemptable")
|
|
|
|
<< ((F && F->isInterposable()) ? " interposable" : "") << "\n";
|
2020-05-24 11:44:31 +02:00
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
O << " args uses:\n";
|
2020-06-01 09:26:29 +02:00
|
|
|
for (auto &KV : Params) {
|
|
|
|
O << " ";
|
2020-05-24 11:44:31 +02:00
|
|
|
if (F)
|
2020-06-01 09:26:29 +02:00
|
|
|
O << F->getArg(KV.first)->getName();
|
|
|
|
else
|
|
|
|
O << formatv("arg{0}", KV.first);
|
|
|
|
O << "[]: " << KV.second << "\n";
|
2020-05-24 11:44:31 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
O << " allocas uses:\n";
|
2020-05-24 11:44:31 +02:00
|
|
|
if (F) {
|
|
|
|
for (auto &I : instructions(F)) {
|
2020-05-29 02:26:15 +02:00
|
|
|
if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
|
2020-05-30 00:10:50 +02:00
|
|
|
auto &AS = Allocas.find(AI)->second;
|
2020-05-24 11:44:31 +02:00
|
|
|
O << " " << AI->getName() << "["
|
2020-05-28 02:41:17 +02:00
|
|
|
<< getStaticAllocaSizeRange(*AI).getUpper() << "]: " << AS << "\n";
|
2020-05-24 11:44:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(Allocas.empty());
|
|
|
|
}
|
2020-08-06 14:09:44 +02:00
|
|
|
O << "\n";
|
2018-11-26 22:57:59 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-02 11:51:35 +02:00
|
|
|
using GVToSSI = std::map<const GlobalValue *, FunctionInfo<GlobalValue>>;
|
2020-05-28 10:07:31 +02:00
|
|
|
|
2020-05-24 07:47:21 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
struct StackSafetyInfo::InfoTy {
|
2020-06-02 11:51:35 +02:00
|
|
|
FunctionInfo<GlobalValue> Info;
|
2020-05-24 07:47:21 +02:00
|
|
|
};
|
|
|
|
|
2020-05-28 10:07:31 +02:00
|
|
|
struct StackSafetyGlobalInfo::InfoTy {
|
|
|
|
GVToSSI Info;
|
2020-05-29 02:26:15 +02:00
|
|
|
SmallPtrSet<const AllocaInst *, 8> SafeAllocas;
|
2020-05-28 10:07:31 +02:00
|
|
|
};
|
2020-05-24 07:47:21 +02:00
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class StackSafetyLocalAnalysis {
|
2020-02-28 00:43:53 +01:00
|
|
|
Function &F;
|
2018-11-26 22:57:59 +01:00
|
|
|
const DataLayout &DL;
|
|
|
|
ScalarEvolution &SE;
|
|
|
|
unsigned PointerSize = 0;
|
|
|
|
|
|
|
|
const ConstantRange UnknownRange;
|
|
|
|
|
2020-05-27 01:12:08 +02:00
|
|
|
ConstantRange offsetFrom(Value *Addr, Value *Base);
|
|
|
|
ConstantRange getAccessRange(Value *Addr, Value *Base,
|
2020-07-10 13:07:37 +02:00
|
|
|
const ConstantRange &SizeRange);
|
2020-05-27 01:12:08 +02:00
|
|
|
ConstantRange getAccessRange(Value *Addr, Value *Base, TypeSize Size);
|
2018-11-26 22:57:59 +01:00
|
|
|
ConstantRange getMemIntrinsicAccessRange(const MemIntrinsic *MI, const Use &U,
|
2020-05-27 01:12:08 +02:00
|
|
|
Value *Base);
|
2018-11-26 22:57:47 +01:00
|
|
|
|
2020-06-22 09:45:08 +02:00
|
|
|
bool analyzeAllUses(Value *Ptr, UseInfo<GlobalValue> &AS,
|
|
|
|
const StackLifetime &SL);
|
2018-11-26 22:57:59 +01:00
|
|
|
|
|
|
|
public:
|
2020-02-28 00:43:53 +01:00
|
|
|
StackSafetyLocalAnalysis(Function &F, ScalarEvolution &SE)
|
2018-11-26 22:57:59 +01:00
|
|
|
: F(F), DL(F.getParent()->getDataLayout()), SE(SE),
|
|
|
|
PointerSize(DL.getPointerSizeInBits()),
|
|
|
|
UnknownRange(PointerSize, true) {}
|
|
|
|
|
|
|
|
// Run the transformation on the associated function.
|
2020-06-02 11:51:35 +02:00
|
|
|
FunctionInfo<GlobalValue> run();
|
2018-11-26 22:57:59 +01:00
|
|
|
};
|
|
|
|
|
2020-05-27 01:12:08 +02:00
|
|
|
ConstantRange StackSafetyLocalAnalysis::offsetFrom(Value *Addr, Value *Base) {
|
2020-06-02 06:40:47 +02:00
|
|
|
if (!SE.isSCEVable(Addr->getType()) || !SE.isSCEVable(Base->getType()))
|
2018-11-26 22:57:59 +01:00
|
|
|
return UnknownRange;
|
|
|
|
|
2020-06-02 06:40:47 +02:00
|
|
|
auto *PtrTy = IntegerType::getInt8PtrTy(SE.getContext());
|
|
|
|
const SCEV *AddrExp = SE.getTruncateOrZeroExtend(SE.getSCEV(Addr), PtrTy);
|
|
|
|
const SCEV *BaseExp = SE.getTruncateOrZeroExtend(SE.getSCEV(Base), PtrTy);
|
|
|
|
const SCEV *Diff = SE.getMinusSCEV(AddrExp, BaseExp);
|
2021-07-06 20:25:49 +02:00
|
|
|
if (isa<SCEVCouldNotCompute>(Diff))
|
|
|
|
return UnknownRange;
|
2020-06-02 06:40:47 +02:00
|
|
|
|
|
|
|
ConstantRange Offset = SE.getSignedRange(Diff);
|
2020-05-27 22:22:16 +02:00
|
|
|
if (isUnsafe(Offset))
|
2020-05-27 02:04:09 +02:00
|
|
|
return UnknownRange;
|
2020-05-27 07:05:41 +02:00
|
|
|
return Offset.sextOrTrunc(PointerSize);
|
2018-11-26 22:57:59 +01:00
|
|
|
}
|
|
|
|
|
2020-01-25 01:36:13 +01:00
|
|
|
ConstantRange
|
2020-05-27 01:12:08 +02:00
|
|
|
StackSafetyLocalAnalysis::getAccessRange(Value *Addr, Value *Base,
|
2020-07-10 13:07:37 +02:00
|
|
|
const ConstantRange &SizeRange) {
|
2020-01-25 01:36:24 +01:00
|
|
|
// Zero-size loads and stores do not access memory.
|
|
|
|
if (SizeRange.isEmptySet())
|
|
|
|
return ConstantRange::getEmpty(PointerSize);
|
2020-05-27 22:22:16 +02:00
|
|
|
assert(!isUnsafe(SizeRange));
|
|
|
|
|
2020-05-27 22:32:58 +02:00
|
|
|
ConstantRange Offsets = offsetFrom(Addr, Base);
|
|
|
|
if (isUnsafe(Offsets))
|
2020-05-27 22:22:16 +02:00
|
|
|
return UnknownRange;
|
2020-01-25 01:36:24 +01:00
|
|
|
|
2020-06-13 02:41:28 +02:00
|
|
|
Offsets = addOverflowNever(Offsets, SizeRange);
|
2020-05-27 22:32:58 +02:00
|
|
|
if (isUnsafe(Offsets))
|
2020-05-27 22:22:16 +02:00
|
|
|
return UnknownRange;
|
2020-05-27 22:32:58 +02:00
|
|
|
return Offsets;
|
2018-11-26 22:57:59 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:12:08 +02:00
|
|
|
ConstantRange StackSafetyLocalAnalysis::getAccessRange(Value *Addr, Value *Base,
|
2020-01-25 01:36:13 +01:00
|
|
|
TypeSize Size) {
|
2020-05-27 22:22:16 +02:00
|
|
|
if (Size.isScalable())
|
|
|
|
return UnknownRange;
|
2020-05-28 02:41:17 +02:00
|
|
|
APInt APSize(PointerSize, Size.getFixedSize(), true);
|
|
|
|
if (APSize.isNegative())
|
|
|
|
return UnknownRange;
|
|
|
|
return getAccessRange(
|
|
|
|
Addr, Base, ConstantRange(APInt::getNullValue(PointerSize), APSize));
|
2020-01-25 01:36:13 +01:00
|
|
|
}
|
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
ConstantRange StackSafetyLocalAnalysis::getMemIntrinsicAccessRange(
|
2020-05-27 01:12:08 +02:00
|
|
|
const MemIntrinsic *MI, const Use &U, Value *Base) {
|
2020-05-28 23:25:44 +02:00
|
|
|
if (const auto *MTI = dyn_cast<MemTransferInst>(MI)) {
|
2018-11-26 22:57:59 +01:00
|
|
|
if (MTI->getRawSource() != U && MTI->getRawDest() != U)
|
2020-05-27 10:42:28 +02:00
|
|
|
return ConstantRange::getEmpty(PointerSize);
|
2018-11-26 22:57:59 +01:00
|
|
|
} else {
|
|
|
|
if (MI->getRawDest() != U)
|
2020-05-27 10:42:28 +02:00
|
|
|
return ConstantRange::getEmpty(PointerSize);
|
2018-11-26 22:57:59 +01:00
|
|
|
}
|
2020-05-28 02:41:17 +02:00
|
|
|
|
2020-05-27 08:20:12 +02:00
|
|
|
auto *CalculationTy = IntegerType::getIntNTy(SE.getContext(), PointerSize);
|
|
|
|
if (!SE.isSCEVable(MI->getLength()->getType()))
|
2018-11-26 22:57:59 +01:00
|
|
|
return UnknownRange;
|
2020-05-27 08:20:12 +02:00
|
|
|
|
|
|
|
const SCEV *Expr =
|
|
|
|
SE.getTruncateOrZeroExtend(SE.getSCEV(MI->getLength()), CalculationTy);
|
2020-05-27 22:32:58 +02:00
|
|
|
ConstantRange Sizes = SE.getSignedRange(Expr);
|
2020-05-28 02:41:17 +02:00
|
|
|
if (Sizes.getUpper().isNegative() || isUnsafe(Sizes))
|
2020-05-27 08:20:12 +02:00
|
|
|
return UnknownRange;
|
2020-05-27 22:32:58 +02:00
|
|
|
Sizes = Sizes.sextOrTrunc(PointerSize);
|
2020-05-27 08:20:12 +02:00
|
|
|
ConstantRange SizeRange(APInt::getNullValue(PointerSize),
|
2020-05-27 22:32:58 +02:00
|
|
|
Sizes.getUpper() - 1);
|
2020-05-27 08:20:12 +02:00
|
|
|
return getAccessRange(U, Base, SizeRange);
|
2018-11-26 22:57:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The function analyzes all local uses of Ptr (alloca or argument) and
|
|
|
|
/// calculates local access range and all function calls where it was used.
|
2020-06-02 11:51:35 +02:00
|
|
|
bool StackSafetyLocalAnalysis::analyzeAllUses(Value *Ptr,
|
2020-06-22 09:45:08 +02:00
|
|
|
UseInfo<GlobalValue> &US,
|
|
|
|
const StackLifetime &SL) {
|
2018-11-26 22:57:59 +01:00
|
|
|
SmallPtrSet<const Value *, 16> Visited;
|
|
|
|
SmallVector<const Value *, 8> WorkList;
|
|
|
|
WorkList.push_back(Ptr);
|
2020-06-22 10:28:25 +02:00
|
|
|
const AllocaInst *AI = dyn_cast<AllocaInst>(Ptr);
|
2018-11-26 22:57:59 +01:00
|
|
|
|
|
|
|
// A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc.
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
const Value *V = WorkList.pop_back_val();
|
|
|
|
for (const Use &UI : V->uses()) {
|
2020-06-22 09:45:08 +02:00
|
|
|
const auto *I = cast<Instruction>(UI.getUser());
|
|
|
|
if (!SL.isReachable(I))
|
|
|
|
continue;
|
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
assert(V == UI.get());
|
|
|
|
|
|
|
|
switch (I->getOpcode()) {
|
|
|
|
case Instruction::Load: {
|
2020-06-22 10:28:25 +02:00
|
|
|
if (AI && !SL.isAliveAfter(AI, I)) {
|
|
|
|
US.updateRange(UnknownRange);
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-26 22:57:59 +01:00
|
|
|
US.updateRange(
|
|
|
|
getAccessRange(UI, Ptr, DL.getTypeStoreSize(I->getType())));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Instruction::VAArg:
|
|
|
|
// "va-arg" from a pointer is safe.
|
|
|
|
break;
|
|
|
|
case Instruction::Store: {
|
|
|
|
if (V == I->getOperand(0)) {
|
|
|
|
// Stored the pointer - conservatively assume it may be unsafe.
|
|
|
|
US.updateRange(UnknownRange);
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-22 10:28:25 +02:00
|
|
|
if (AI && !SL.isAliveAfter(AI, I)) {
|
|
|
|
US.updateRange(UnknownRange);
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-26 22:57:59 +01:00
|
|
|
US.updateRange(getAccessRange(
|
|
|
|
UI, Ptr, DL.getTypeStoreSize(I->getOperand(0)->getType())));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Instruction::Ret:
|
|
|
|
// Information leak.
|
|
|
|
// FIXME: Process parameters correctly. This is a leak only if we return
|
|
|
|
// alloca.
|
|
|
|
US.updateRange(UnknownRange);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case Instruction::Call:
|
|
|
|
case Instruction::Invoke: {
|
2018-12-21 22:49:40 +01:00
|
|
|
if (I->isLifetimeStartOrEnd())
|
|
|
|
break;
|
2018-11-26 22:57:59 +01:00
|
|
|
|
2020-06-22 10:28:25 +02:00
|
|
|
if (AI && !SL.isAliveAfter(AI, I)) {
|
|
|
|
US.updateRange(UnknownRange);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
|
|
|
|
US.updateRange(getMemIntrinsicAccessRange(MI, UI, Ptr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-11 22:47:50 +02:00
|
|
|
const auto &CB = cast<CallBase>(*I);
|
|
|
|
if (!CB.isArgOperand(&UI)) {
|
|
|
|
US.updateRange(UnknownRange);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-12 03:56:20 +02:00
|
|
|
unsigned ArgNo = CB.getArgOperandNo(&UI);
|
|
|
|
if (CB.isByValArgument(ArgNo)) {
|
|
|
|
US.updateRange(getAccessRange(
|
|
|
|
UI, Ptr, DL.getTypeStoreSize(CB.getParamByValType(ArgNo))));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
// FIXME: consult devirt?
|
|
|
|
// Do not follow aliases, otherwise we could inadvertently follow
|
|
|
|
// dso_preemptable aliases or aliases with interposable linkage.
|
2019-08-22 21:56:14 +02:00
|
|
|
const GlobalValue *Callee =
|
2020-04-28 05:15:59 +02:00
|
|
|
dyn_cast<GlobalValue>(CB.getCalledOperand()->stripPointerCasts());
|
2018-11-26 22:57:59 +01:00
|
|
|
if (!Callee) {
|
|
|
|
US.updateRange(UnknownRange);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(isa<Function>(Callee) || isa<GlobalAlias>(Callee));
|
2020-08-14 10:14:02 +02:00
|
|
|
ConstantRange Offsets = offsetFrom(UI, Ptr);
|
|
|
|
auto Insert =
|
|
|
|
US.Calls.emplace(CallInfo<GlobalValue>(Callee, ArgNo), Offsets);
|
|
|
|
if (!Insert.second)
|
|
|
|
Insert.first->second = Insert.first->second.unionWith(Offsets);
|
2018-11-26 22:57:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (Visited.insert(I).second)
|
|
|
|
WorkList.push_back(cast<const Instruction>(I));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-02 11:51:35 +02:00
|
|
|
FunctionInfo<GlobalValue> StackSafetyLocalAnalysis::run() {
|
|
|
|
FunctionInfo<GlobalValue> Info;
|
2018-11-26 22:57:59 +01:00
|
|
|
assert(!F.isDeclaration() &&
|
|
|
|
"Can't run StackSafety on a function declaration");
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "[StackSafety] " << F.getName() << "\n");
|
|
|
|
|
2020-06-22 09:45:08 +02:00
|
|
|
SmallVector<AllocaInst *, 64> Allocas;
|
|
|
|
for (auto &I : instructions(F))
|
|
|
|
if (auto *AI = dyn_cast<AllocaInst>(&I))
|
|
|
|
Allocas.push_back(AI);
|
|
|
|
StackLifetime SL(F, Allocas, StackLifetime::LivenessType::Must);
|
|
|
|
SL.run();
|
|
|
|
|
|
|
|
for (auto *AI : Allocas) {
|
|
|
|
auto &UI = Info.Allocas.emplace(AI, PointerSize).first->second;
|
|
|
|
analyzeAllUses(AI, UI, SL);
|
2018-11-26 22:57:59 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 18:24:59 +01:00
|
|
|
for (Argument &A : F.args()) {
|
2020-06-12 03:56:20 +02:00
|
|
|
// Non pointers and bypass arguments are not going to be used in any global
|
|
|
|
// processing.
|
|
|
|
if (A.getType()->isPointerTy() && !A.hasByValAttr()) {
|
2020-06-01 09:26:29 +02:00
|
|
|
auto &UI = Info.Params.emplace(A.getArgNo(), PointerSize).first->second;
|
2020-06-22 09:45:08 +02:00
|
|
|
analyzeAllUses(&A, UI, SL);
|
2020-06-01 09:26:29 +02:00
|
|
|
}
|
2018-11-26 22:57:59 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 11:44:31 +02:00
|
|
|
LLVM_DEBUG(Info.print(dbgs(), F.getName(), &F));
|
2018-11-26 22:57:59 +01:00
|
|
|
LLVM_DEBUG(dbgs() << "[StackSafety] done\n");
|
2020-05-24 07:47:21 +02:00
|
|
|
return Info;
|
2018-11-26 22:57:59 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 11:51:35 +02:00
|
|
|
template <typename CalleeTy> class StackSafetyDataFlowAnalysis {
|
|
|
|
using FunctionMap = std::map<const CalleeTy *, FunctionInfo<CalleeTy>>;
|
2018-11-27 00:05:58 +01:00
|
|
|
|
|
|
|
FunctionMap Functions;
|
2020-05-28 07:21:39 +02:00
|
|
|
const ConstantRange UnknownRange;
|
|
|
|
|
2018-11-27 00:05:58 +01:00
|
|
|
// Callee-to-Caller multimap.
|
2020-06-02 11:51:35 +02:00
|
|
|
DenseMap<const CalleeTy *, SmallVector<const CalleeTy *, 4>> Callers;
|
|
|
|
SetVector<const CalleeTy *> WorkList;
|
2018-11-27 00:05:58 +01:00
|
|
|
|
2020-06-02 11:51:35 +02:00
|
|
|
bool updateOneUse(UseInfo<CalleeTy> &US, bool UpdateToFullSet);
|
|
|
|
void updateOneNode(const CalleeTy *Callee, FunctionInfo<CalleeTy> &FS);
|
|
|
|
void updateOneNode(const CalleeTy *Callee) {
|
2018-11-27 00:05:58 +01:00
|
|
|
updateOneNode(Callee, Functions.find(Callee)->second);
|
|
|
|
}
|
|
|
|
void updateAllNodes() {
|
|
|
|
for (auto &F : Functions)
|
|
|
|
updateOneNode(F.first, F.second);
|
|
|
|
}
|
|
|
|
void runDataFlow();
|
2019-03-01 18:15:21 +01:00
|
|
|
#ifndef NDEBUG
|
2018-11-27 00:05:58 +01:00
|
|
|
void verifyFixedPoint();
|
2019-03-01 18:15:21 +01:00
|
|
|
#endif
|
2018-11-27 00:05:58 +01:00
|
|
|
|
|
|
|
public:
|
2020-05-28 10:07:31 +02:00
|
|
|
StackSafetyDataFlowAnalysis(uint32_t PointerBitWidth, FunctionMap Functions)
|
2020-05-28 07:21:39 +02:00
|
|
|
: Functions(std::move(Functions)),
|
2020-05-28 10:07:31 +02:00
|
|
|
UnknownRange(ConstantRange::getFull(PointerBitWidth)) {}
|
2018-11-27 00:05:58 +01:00
|
|
|
|
2020-05-28 07:21:39 +02:00
|
|
|
const FunctionMap &run();
|
|
|
|
|
2020-06-02 11:51:35 +02:00
|
|
|
ConstantRange getArgumentAccessRange(const CalleeTy *Callee, unsigned ParamNo,
|
2020-05-28 22:01:02 +02:00
|
|
|
const ConstantRange &Offsets) const;
|
2020-05-28 07:21:39 +02:00
|
|
|
};
|
2018-11-27 00:05:58 +01:00
|
|
|
|
2020-06-02 11:51:35 +02:00
|
|
|
template <typename CalleeTy>
|
|
|
|
ConstantRange StackSafetyDataFlowAnalysis<CalleeTy>::getArgumentAccessRange(
|
|
|
|
const CalleeTy *Callee, unsigned ParamNo,
|
2020-05-28 22:01:02 +02:00
|
|
|
const ConstantRange &Offsets) const {
|
2020-06-01 09:26:29 +02:00
|
|
|
auto FnIt = Functions.find(Callee);
|
2018-11-27 00:05:58 +01:00
|
|
|
// Unknown callee (outside of LTO domain or an indirect call).
|
2020-06-01 09:26:29 +02:00
|
|
|
if (FnIt == Functions.end())
|
2018-11-27 00:05:58 +01:00
|
|
|
return UnknownRange;
|
2020-06-01 09:26:29 +02:00
|
|
|
auto &FS = FnIt->second;
|
|
|
|
auto ParamIt = FS.Params.find(ParamNo);
|
|
|
|
if (ParamIt == FS.Params.end())
|
2018-11-27 00:05:58 +01:00
|
|
|
return UnknownRange;
|
2020-06-01 09:26:29 +02:00
|
|
|
auto &Access = ParamIt->second.Range;
|
2020-05-28 22:01:02 +02:00
|
|
|
if (Access.isEmptySet())
|
|
|
|
return Access;
|
2020-06-01 09:26:29 +02:00
|
|
|
if (Access.isFullSet())
|
2020-05-28 22:01:02 +02:00
|
|
|
return UnknownRange;
|
2020-06-13 02:41:28 +02:00
|
|
|
return addOverflowNever(Access, Offsets);
|
2018-11-27 00:05:58 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 11:51:35 +02:00
|
|
|
template <typename CalleeTy>
|
|
|
|
bool StackSafetyDataFlowAnalysis<CalleeTy>::updateOneUse(UseInfo<CalleeTy> &US,
|
|
|
|
bool UpdateToFullSet) {
|
2018-11-27 00:05:58 +01:00
|
|
|
bool Changed = false;
|
2020-08-14 10:14:02 +02:00
|
|
|
for (auto &KV : US.Calls) {
|
|
|
|
assert(!KV.second.isEmptySet() &&
|
2018-11-27 02:56:35 +01:00
|
|
|
"Param range can't be empty-set, invalid offset range");
|
2018-11-27 00:05:58 +01:00
|
|
|
|
2020-05-28 22:01:02 +02:00
|
|
|
ConstantRange CalleeRange =
|
2020-08-14 10:14:02 +02:00
|
|
|
getArgumentAccessRange(KV.first.Callee, KV.first.ParamNo, KV.second);
|
2018-11-27 00:05:58 +01:00
|
|
|
if (!US.Range.contains(CalleeRange)) {
|
|
|
|
Changed = true;
|
|
|
|
if (UpdateToFullSet)
|
|
|
|
US.Range = UnknownRange;
|
|
|
|
else
|
2020-08-08 11:05:04 +02:00
|
|
|
US.updateRange(CalleeRange);
|
2018-11-27 00:05:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2020-06-02 11:51:35 +02:00
|
|
|
template <typename CalleeTy>
|
|
|
|
void StackSafetyDataFlowAnalysis<CalleeTy>::updateOneNode(
|
|
|
|
const CalleeTy *Callee, FunctionInfo<CalleeTy> &FS) {
|
2018-11-27 00:05:58 +01:00
|
|
|
bool UpdateToFullSet = FS.UpdateCount > StackSafetyMaxIterations;
|
|
|
|
bool Changed = false;
|
2020-06-01 09:26:29 +02:00
|
|
|
for (auto &KV : FS.Params)
|
|
|
|
Changed |= updateOneUse(KV.second, UpdateToFullSet);
|
2018-11-27 00:05:58 +01:00
|
|
|
|
|
|
|
if (Changed) {
|
|
|
|
LLVM_DEBUG(dbgs() << "=== update [" << FS.UpdateCount
|
2020-05-24 11:44:31 +02:00
|
|
|
<< (UpdateToFullSet ? ", full-set" : "") << "] " << &FS
|
|
|
|
<< "\n");
|
2018-11-27 00:05:58 +01:00
|
|
|
// Callers of this function may need updating.
|
|
|
|
for (auto &CallerID : Callers[Callee])
|
|
|
|
WorkList.insert(CallerID);
|
|
|
|
|
|
|
|
++FS.UpdateCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 11:51:35 +02:00
|
|
|
template <typename CalleeTy>
|
|
|
|
void StackSafetyDataFlowAnalysis<CalleeTy>::runDataFlow() {
|
|
|
|
SmallVector<const CalleeTy *, 16> Callees;
|
2018-11-27 00:05:58 +01:00
|
|
|
for (auto &F : Functions) {
|
|
|
|
Callees.clear();
|
2020-06-02 11:51:35 +02:00
|
|
|
auto &FS = F.second;
|
|
|
|
for (auto &KV : FS.Params)
|
2020-06-01 09:26:29 +02:00
|
|
|
for (auto &CS : KV.second.Calls)
|
2020-08-14 10:14:02 +02:00
|
|
|
Callees.push_back(CS.first.Callee);
|
2018-11-27 00:05:58 +01:00
|
|
|
|
|
|
|
llvm::sort(Callees);
|
|
|
|
Callees.erase(std::unique(Callees.begin(), Callees.end()), Callees.end());
|
|
|
|
|
|
|
|
for (auto &Callee : Callees)
|
|
|
|
Callers[Callee].push_back(F.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAllNodes();
|
|
|
|
|
|
|
|
while (!WorkList.empty()) {
|
2020-06-02 11:51:35 +02:00
|
|
|
const CalleeTy *Callee = WorkList.back();
|
2018-11-27 00:05:58 +01:00
|
|
|
WorkList.pop_back();
|
|
|
|
updateOneNode(Callee);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 18:15:21 +01:00
|
|
|
#ifndef NDEBUG
|
2020-06-03 11:38:49 +02:00
|
|
|
template <typename CalleeTy>
|
|
|
|
void StackSafetyDataFlowAnalysis<CalleeTy>::verifyFixedPoint() {
|
2018-11-27 00:05:58 +01:00
|
|
|
WorkList.clear();
|
|
|
|
updateAllNodes();
|
|
|
|
assert(WorkList.empty());
|
|
|
|
}
|
2019-03-01 18:15:21 +01:00
|
|
|
#endif
|
2018-11-27 00:05:58 +01:00
|
|
|
|
2020-06-02 11:51:35 +02:00
|
|
|
template <typename CalleeTy>
|
|
|
|
const typename StackSafetyDataFlowAnalysis<CalleeTy>::FunctionMap &
|
|
|
|
StackSafetyDataFlowAnalysis<CalleeTy>::run() {
|
2018-11-27 00:05:58 +01:00
|
|
|
runDataFlow();
|
|
|
|
LLVM_DEBUG(verifyFixedPoint());
|
2020-05-28 07:21:39 +02:00
|
|
|
return Functions;
|
2018-11-27 00:05:58 +01:00
|
|
|
}
|
|
|
|
|
2020-08-06 14:28:03 +02:00
|
|
|
FunctionSummary *findCalleeFunctionSummary(ValueInfo VI, StringRef ModuleId) {
|
|
|
|
if (!VI)
|
|
|
|
return nullptr;
|
|
|
|
auto SummaryList = VI.getSummaryList();
|
|
|
|
GlobalValueSummary* S = nullptr;
|
|
|
|
for (const auto& GVS : SummaryList) {
|
|
|
|
if (!GVS->isLive())
|
|
|
|
continue;
|
|
|
|
if (const AliasSummary *AS = dyn_cast<AliasSummary>(GVS.get()))
|
|
|
|
if (!AS->hasAliasee())
|
|
|
|
continue;
|
|
|
|
if (!isa<FunctionSummary>(GVS->getBaseObject()))
|
|
|
|
continue;
|
|
|
|
if (GlobalValue::isLocalLinkage(GVS->linkage())) {
|
|
|
|
if (GVS->modulePath() == ModuleId) {
|
|
|
|
S = GVS.get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (GlobalValue::isExternalLinkage(GVS->linkage())) {
|
|
|
|
if (S) {
|
|
|
|
++NumIndexCalleeMultipleExternal;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
S = GVS.get();
|
|
|
|
} else if (GlobalValue::isWeakLinkage(GVS->linkage())) {
|
|
|
|
if (S) {
|
|
|
|
++NumIndexCalleeMultipleWeak;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
S = GVS.get();
|
|
|
|
} else if (GlobalValue::isAvailableExternallyLinkage(GVS->linkage()) ||
|
|
|
|
GlobalValue::isLinkOnceLinkage(GVS->linkage())) {
|
|
|
|
if (SummaryList.size() == 1)
|
|
|
|
S = GVS.get();
|
|
|
|
// According thinLTOResolvePrevailingGUID these are unlikely prevailing.
|
|
|
|
} else {
|
|
|
|
++NumIndexCalleeUnhandled;
|
|
|
|
}
|
|
|
|
};
|
2020-06-05 10:11:35 +02:00
|
|
|
while (S) {
|
|
|
|
if (!S->isLive() || !S->isDSOLocal())
|
|
|
|
return nullptr;
|
|
|
|
if (FunctionSummary *FS = dyn_cast<FunctionSummary>(S))
|
|
|
|
return FS;
|
|
|
|
AliasSummary *AS = dyn_cast<AliasSummary>(S);
|
2020-08-08 02:28:05 +02:00
|
|
|
if (!AS || !AS->hasAliasee())
|
2020-06-05 10:11:35 +02:00
|
|
|
return nullptr;
|
|
|
|
S = AS->getBaseObject();
|
|
|
|
if (S == AS)
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-28 23:25:44 +02:00
|
|
|
const Function *findCalleeInModule(const GlobalValue *GV) {
|
2020-05-28 07:21:39 +02:00
|
|
|
while (GV) {
|
2020-06-05 04:44:42 +02:00
|
|
|
if (GV->isDeclaration() || GV->isInterposable() || !GV->isDSOLocal())
|
2020-05-28 07:21:39 +02:00
|
|
|
return nullptr;
|
|
|
|
if (const Function *F = dyn_cast<Function>(GV))
|
|
|
|
return F;
|
|
|
|
const GlobalAlias *A = dyn_cast<GlobalAlias>(GV);
|
|
|
|
if (!A)
|
|
|
|
return nullptr;
|
|
|
|
GV = A->getBaseObject();
|
|
|
|
if (GV == A)
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-05 10:11:35 +02:00
|
|
|
const ConstantRange *findParamAccess(const FunctionSummary &FS,
|
|
|
|
uint32_t ParamNo) {
|
|
|
|
assert(FS.isLive());
|
|
|
|
assert(FS.isDSOLocal());
|
|
|
|
for (auto &PS : FS.paramAccesses())
|
|
|
|
if (ParamNo == PS.ParamNo)
|
|
|
|
return &PS.Use;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resolveAllCalls(UseInfo<GlobalValue> &Use,
|
|
|
|
const ModuleSummaryIndex *Index) {
|
2020-05-28 07:21:39 +02:00
|
|
|
ConstantRange FullSet(Use.Range.getBitWidth(), true);
|
2020-10-03 18:51:40 +02:00
|
|
|
// Move Use.Calls to a temp storage and repopulate - don't use std::move as it
|
|
|
|
// leaves Use.Calls in an undefined state.
|
|
|
|
UseInfo<GlobalValue>::CallsTy TmpCalls;
|
|
|
|
std::swap(TmpCalls, Use.Calls);
|
2020-08-14 10:14:02 +02:00
|
|
|
for (const auto &C : TmpCalls) {
|
|
|
|
const Function *F = findCalleeInModule(C.first.Callee);
|
2020-05-28 07:21:39 +02:00
|
|
|
if (F) {
|
2020-08-14 10:14:02 +02:00
|
|
|
Use.Calls.emplace(CallInfo<GlobalValue>(F, C.first.ParamNo), C.second);
|
2020-05-28 07:21:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-05 10:11:35 +02:00
|
|
|
if (!Index)
|
|
|
|
return Use.updateRange(FullSet);
|
2020-08-06 14:28:03 +02:00
|
|
|
FunctionSummary *FS =
|
|
|
|
findCalleeFunctionSummary(Index->getValueInfo(C.first.Callee->getGUID()),
|
|
|
|
C.first.Callee->getParent()->getModuleIdentifier());
|
2020-08-06 14:19:12 +02:00
|
|
|
++NumModuleCalleeLookupTotal;
|
|
|
|
if (!FS) {
|
|
|
|
++NumModuleCalleeLookupFailed;
|
2020-06-05 10:11:35 +02:00
|
|
|
return Use.updateRange(FullSet);
|
2020-08-06 14:19:12 +02:00
|
|
|
}
|
2020-08-14 10:14:02 +02:00
|
|
|
const ConstantRange *Found = findParamAccess(*FS, C.first.ParamNo);
|
2020-08-08 11:10:10 +02:00
|
|
|
if (!Found || Found->isFullSet())
|
2020-06-05 10:11:35 +02:00
|
|
|
return Use.updateRange(FullSet);
|
|
|
|
ConstantRange Access = Found->sextOrTrunc(Use.Range.getBitWidth());
|
2020-08-08 11:10:10 +02:00
|
|
|
if (!Access.isEmptySet())
|
2020-08-14 10:14:02 +02:00
|
|
|
Use.updateRange(addOverflowNever(Access, C.second));
|
2020-05-28 07:21:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GVToSSI createGlobalStackSafetyInfo(
|
2020-06-02 10:19:57 +02:00
|
|
|
std::map<const GlobalValue *, FunctionInfo<GlobalValue>> Functions,
|
|
|
|
const ModuleSummaryIndex *Index) {
|
2020-05-28 07:21:39 +02:00
|
|
|
GVToSSI SSI;
|
|
|
|
if (Functions.empty())
|
|
|
|
return SSI;
|
|
|
|
|
|
|
|
// FIXME: Simplify printing and remove copying here.
|
|
|
|
auto Copy = Functions;
|
|
|
|
|
2020-06-01 09:26:29 +02:00
|
|
|
for (auto &FnKV : Copy)
|
2020-08-14 10:14:02 +02:00
|
|
|
for (auto &KV : FnKV.second.Params) {
|
2020-06-05 10:11:35 +02:00
|
|
|
resolveAllCalls(KV.second, Index);
|
2020-08-14 10:14:02 +02:00
|
|
|
if (KV.second.Range.isFullSet())
|
|
|
|
KV.second.Calls.clear();
|
|
|
|
}
|
2020-05-28 07:21:39 +02:00
|
|
|
|
2020-05-28 10:07:31 +02:00
|
|
|
uint32_t PointerSize = Copy.begin()
|
|
|
|
->first->getParent()
|
|
|
|
->getDataLayout()
|
|
|
|
.getMaxPointerSizeInBits();
|
2020-06-02 11:51:35 +02:00
|
|
|
StackSafetyDataFlowAnalysis<GlobalValue> SSDFA(PointerSize, std::move(Copy));
|
2020-05-28 07:21:39 +02:00
|
|
|
|
|
|
|
for (auto &F : SSDFA.run()) {
|
|
|
|
auto FI = F.second;
|
2020-05-28 10:07:31 +02:00
|
|
|
auto &SrcF = Functions[F.first];
|
2020-05-30 00:10:50 +02:00
|
|
|
for (auto &KV : FI.Allocas) {
|
|
|
|
auto &A = KV.second;
|
2020-06-05 10:11:35 +02:00
|
|
|
resolveAllCalls(A, Index);
|
2020-05-28 07:21:39 +02:00
|
|
|
for (auto &C : A.Calls) {
|
2020-08-14 10:14:02 +02:00
|
|
|
A.updateRange(SSDFA.getArgumentAccessRange(C.first.Callee,
|
|
|
|
C.first.ParamNo, C.second));
|
2020-05-28 07:21:39 +02:00
|
|
|
}
|
|
|
|
// FIXME: This is needed only to preserve calls in print() results.
|
2020-05-30 00:10:50 +02:00
|
|
|
A.Calls = SrcF.Allocas.find(KV.first)->second.Calls;
|
2020-05-28 07:21:39 +02:00
|
|
|
}
|
2020-06-01 09:26:29 +02:00
|
|
|
for (auto &KV : FI.Params) {
|
|
|
|
auto &P = KV.second;
|
|
|
|
P.Calls = SrcF.Params.find(KV.first)->second.Calls;
|
2020-05-28 07:21:39 +02:00
|
|
|
}
|
2020-05-28 10:07:31 +02:00
|
|
|
SSI[F.first] = std::move(FI);
|
2020-05-28 07:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return SSI;
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2020-05-28 10:07:31 +02:00
|
|
|
StackSafetyInfo::StackSafetyInfo() = default;
|
|
|
|
|
|
|
|
StackSafetyInfo::StackSafetyInfo(Function *F,
|
|
|
|
std::function<ScalarEvolution &()> GetSE)
|
|
|
|
: F(F), GetSE(GetSE) {}
|
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
StackSafetyInfo::StackSafetyInfo(StackSafetyInfo &&) = default;
|
|
|
|
|
2020-05-28 10:07:31 +02:00
|
|
|
StackSafetyInfo &StackSafetyInfo::operator=(StackSafetyInfo &&) = default;
|
2018-11-26 22:57:59 +01:00
|
|
|
|
|
|
|
StackSafetyInfo::~StackSafetyInfo() = default;
|
|
|
|
|
2020-05-28 10:07:31 +02:00
|
|
|
const StackSafetyInfo::InfoTy &StackSafetyInfo::getInfo() const {
|
|
|
|
if (!Info) {
|
|
|
|
StackSafetyLocalAnalysis SSLA(*F, GetSE());
|
|
|
|
Info.reset(new InfoTy{SSLA.run()});
|
|
|
|
}
|
|
|
|
return *Info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyInfo::print(raw_ostream &O) const {
|
|
|
|
getInfo().Info.print(O, F->getName(), dyn_cast<Function>(F));
|
|
|
|
}
|
|
|
|
|
|
|
|
const StackSafetyGlobalInfo::InfoTy &StackSafetyGlobalInfo::getInfo() const {
|
|
|
|
if (!Info) {
|
2020-06-02 11:51:35 +02:00
|
|
|
std::map<const GlobalValue *, FunctionInfo<GlobalValue>> Functions;
|
2020-05-28 10:07:31 +02:00
|
|
|
for (auto &F : M->functions()) {
|
|
|
|
if (!F.isDeclaration()) {
|
|
|
|
auto FI = GetSSI(F).getInfo().Info;
|
|
|
|
Functions.emplace(&F, std::move(FI));
|
|
|
|
}
|
|
|
|
}
|
2020-06-02 10:19:57 +02:00
|
|
|
Info.reset(new InfoTy{
|
|
|
|
createGlobalStackSafetyInfo(std::move(Functions), Index), {}});
|
2020-05-30 00:10:50 +02:00
|
|
|
for (auto &FnKV : Info->Info) {
|
|
|
|
for (auto &KV : FnKV.second.Allocas) {
|
2020-06-04 01:12:08 +02:00
|
|
|
++NumAllocaTotal;
|
2020-05-30 00:10:50 +02:00
|
|
|
const AllocaInst *AI = KV.first;
|
2020-06-04 01:12:08 +02:00
|
|
|
if (getStaticAllocaSizeRange(*AI).contains(KV.second.Range)) {
|
2020-05-30 00:10:50 +02:00
|
|
|
Info->SafeAllocas.insert(AI);
|
2020-06-04 01:12:08 +02:00
|
|
|
++NumAllocaStackSafe;
|
|
|
|
}
|
2020-05-29 02:26:15 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-29 00:36:17 +02:00
|
|
|
if (StackSafetyPrint)
|
|
|
|
print(errs());
|
2020-05-28 10:07:31 +02:00
|
|
|
}
|
|
|
|
return *Info;
|
2020-05-24 11:44:31 +02:00
|
|
|
}
|
|
|
|
|
2020-06-01 08:49:57 +02:00
|
|
|
std::vector<FunctionSummary::ParamAccess>
|
2020-08-14 21:42:21 +02:00
|
|
|
StackSafetyInfo::getParamAccesses(ModuleSummaryIndex &Index) const {
|
2020-07-09 08:57:13 +02:00
|
|
|
// Implementation transforms internal representation of parameter information
|
|
|
|
// into FunctionSummary format.
|
2020-06-01 08:49:57 +02:00
|
|
|
std::vector<FunctionSummary::ParamAccess> ParamAccesses;
|
|
|
|
for (const auto &KV : getInfo().Info.Params) {
|
|
|
|
auto &PS = KV.second;
|
2020-07-09 08:57:13 +02:00
|
|
|
// Parameter accessed by any or unknown offset, represented as FullSet by
|
|
|
|
// StackSafety, is handled as the parameter for which we have no
|
|
|
|
// StackSafety info at all. So drop it to reduce summary size.
|
2020-06-01 08:49:57 +02:00
|
|
|
if (PS.Range.isFullSet())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ParamAccesses.emplace_back(KV.first, PS.Range);
|
|
|
|
FunctionSummary::ParamAccess &Param = ParamAccesses.back();
|
|
|
|
|
|
|
|
Param.Calls.reserve(PS.Calls.size());
|
|
|
|
for (auto &C : PS.Calls) {
|
2020-07-09 08:57:13 +02:00
|
|
|
// Parameter forwarded into another function by any or unknown offset
|
|
|
|
// will make ParamAccess::Range as FullSet anyway. So we can drop the
|
|
|
|
// entire parameter like we did above.
|
|
|
|
// TODO(vitalybuka): Return already filtered parameters from getInfo().
|
2020-08-14 10:14:02 +02:00
|
|
|
if (C.second.isFullSet()) {
|
2020-06-01 08:49:57 +02:00
|
|
|
ParamAccesses.pop_back();
|
|
|
|
break;
|
|
|
|
}
|
2020-08-14 21:42:21 +02:00
|
|
|
Param.Calls.emplace_back(C.first.ParamNo,
|
|
|
|
Index.getOrInsertValueInfo(C.first.Callee),
|
2020-08-14 10:14:02 +02:00
|
|
|
C.second);
|
2020-06-01 08:49:57 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-14 13:17:08 +02:00
|
|
|
for (FunctionSummary::ParamAccess &Param : ParamAccesses) {
|
|
|
|
sort(Param.Calls, [](const FunctionSummary::ParamAccess::Call &L,
|
|
|
|
const FunctionSummary::ParamAccess::Call &R) {
|
|
|
|
return std::tie(L.ParamNo, L.Callee) < std::tie(R.ParamNo, R.Callee);
|
|
|
|
});
|
|
|
|
}
|
2020-06-01 08:49:57 +02:00
|
|
|
return ParamAccesses;
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:07:31 +02:00
|
|
|
StackSafetyGlobalInfo::StackSafetyGlobalInfo() = default;
|
|
|
|
|
|
|
|
StackSafetyGlobalInfo::StackSafetyGlobalInfo(
|
2020-06-02 10:19:57 +02:00
|
|
|
Module *M, std::function<const StackSafetyInfo &(Function &F)> GetSSI,
|
|
|
|
const ModuleSummaryIndex *Index)
|
|
|
|
: M(M), GetSSI(GetSSI), Index(Index) {
|
2020-06-04 11:38:58 +02:00
|
|
|
if (StackSafetyRun)
|
2020-05-29 00:36:17 +02:00
|
|
|
getInfo();
|
|
|
|
}
|
2020-05-28 10:07:31 +02:00
|
|
|
|
|
|
|
StackSafetyGlobalInfo::StackSafetyGlobalInfo(StackSafetyGlobalInfo &&) =
|
|
|
|
default;
|
|
|
|
|
|
|
|
StackSafetyGlobalInfo &
|
|
|
|
StackSafetyGlobalInfo::operator=(StackSafetyGlobalInfo &&) = default;
|
|
|
|
|
|
|
|
StackSafetyGlobalInfo::~StackSafetyGlobalInfo() = default;
|
|
|
|
|
2020-05-29 02:26:15 +02:00
|
|
|
bool StackSafetyGlobalInfo::isSafe(const AllocaInst &AI) const {
|
|
|
|
const auto &Info = getInfo();
|
2020-06-07 22:36:10 +02:00
|
|
|
return Info.SafeAllocas.count(&AI);
|
2020-05-28 04:28:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyGlobalInfo::print(raw_ostream &O) const {
|
2020-05-28 10:07:31 +02:00
|
|
|
auto &SSI = getInfo().Info;
|
|
|
|
if (SSI.empty())
|
2020-05-28 04:28:42 +02:00
|
|
|
return;
|
2020-05-28 10:07:31 +02:00
|
|
|
const Module &M = *SSI.begin()->first->getParent();
|
2020-05-28 04:28:42 +02:00
|
|
|
for (auto &F : M.functions()) {
|
2020-05-24 11:44:31 +02:00
|
|
|
if (!F.isDeclaration()) {
|
2020-05-28 10:07:31 +02:00
|
|
|
SSI.find(&F)->second.print(O, F.getName(), &F);
|
2020-05-24 11:44:31 +02:00
|
|
|
O << "\n";
|
|
|
|
}
|
2020-05-28 04:28:42 +02:00
|
|
|
}
|
2020-05-24 11:44:31 +02:00
|
|
|
}
|
2018-11-26 22:57:59 +01:00
|
|
|
|
2020-05-28 04:28:42 +02:00
|
|
|
LLVM_DUMP_METHOD void StackSafetyGlobalInfo::dump() const { print(dbgs()); }
|
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
AnalysisKey StackSafetyAnalysis::Key;
|
2018-11-26 22:57:47 +01:00
|
|
|
|
|
|
|
StackSafetyInfo StackSafetyAnalysis::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
2020-05-28 10:07:31 +02:00
|
|
|
return StackSafetyInfo(&F, [&AM, &F]() -> ScalarEvolution & {
|
|
|
|
return AM.getResult<ScalarEvolutionAnalysis>(F);
|
|
|
|
});
|
2018-11-26 22:57:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses StackSafetyPrinterPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
|
|
|
OS << "'Stack Safety Local Analysis' for function '" << F.getName() << "'\n";
|
2020-05-28 10:07:31 +02:00
|
|
|
AM.getResult<StackSafetyAnalysis>(F).print(OS);
|
2018-11-26 22:57:47 +01:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
char StackSafetyInfoWrapperPass::ID = 0;
|
|
|
|
|
|
|
|
StackSafetyInfoWrapperPass::StackSafetyInfoWrapperPass() : FunctionPass(ID) {
|
|
|
|
initializeStackSafetyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
2020-05-28 10:07:31 +02:00
|
|
|
AU.addRequiredTransitive<ScalarEvolutionWrapperPass>();
|
2018-11-26 22:57:47 +01:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyInfoWrapperPass::print(raw_ostream &O, const Module *M) const {
|
2020-05-28 10:07:31 +02:00
|
|
|
SSI.print(O);
|
2018-11-26 22:57:47 +01:00
|
|
|
}
|
|
|
|
|
2018-11-26 22:57:59 +01:00
|
|
|
bool StackSafetyInfoWrapperPass::runOnFunction(Function &F) {
|
2020-05-28 10:07:31 +02:00
|
|
|
auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
|
|
|
|
SSI = {&F, [SE]() -> ScalarEvolution & { return *SE; }};
|
2018-11-26 22:57:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-26 22:57:47 +01:00
|
|
|
|
2018-11-27 00:05:48 +01:00
|
|
|
AnalysisKey StackSafetyGlobalAnalysis::Key;
|
|
|
|
|
|
|
|
StackSafetyGlobalInfo
|
|
|
|
StackSafetyGlobalAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
|
2020-06-02 10:19:57 +02:00
|
|
|
// FIXME: Lookup Module Summary.
|
2018-11-27 00:05:58 +01:00
|
|
|
FunctionAnalysisManager &FAM =
|
|
|
|
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
|
2020-06-02 10:19:57 +02:00
|
|
|
return {&M,
|
|
|
|
[&FAM](Function &F) -> const StackSafetyInfo & {
|
2020-05-28 10:07:31 +02:00
|
|
|
return FAM.getResult<StackSafetyAnalysis>(F);
|
2020-06-02 10:19:57 +02:00
|
|
|
},
|
|
|
|
nullptr};
|
2018-11-27 00:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses StackSafetyGlobalPrinterPass::run(Module &M,
|
|
|
|
ModuleAnalysisManager &AM) {
|
|
|
|
OS << "'Stack Safety Analysis' for module '" << M.getName() << "'\n";
|
2020-05-28 04:28:42 +02:00
|
|
|
AM.getResult<StackSafetyGlobalAnalysis>(M).print(OS);
|
2018-11-27 00:05:48 +01:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
char StackSafetyGlobalInfoWrapperPass::ID = 0;
|
|
|
|
|
2020-05-28 05:35:06 +02:00
|
|
|
StackSafetyGlobalInfoWrapperPass::StackSafetyGlobalInfoWrapperPass()
|
|
|
|
: ModulePass(ID) {
|
2018-11-27 00:05:48 +01:00
|
|
|
initializeStackSafetyGlobalInfoWrapperPassPass(
|
|
|
|
*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:07:31 +02:00
|
|
|
StackSafetyGlobalInfoWrapperPass::~StackSafetyGlobalInfoWrapperPass() = default;
|
|
|
|
|
2018-11-27 00:05:48 +01:00
|
|
|
void StackSafetyGlobalInfoWrapperPass::print(raw_ostream &O,
|
|
|
|
const Module *M) const {
|
2020-05-28 04:28:42 +02:00
|
|
|
SSGI.print(O);
|
2018-11-27 00:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StackSafetyGlobalInfoWrapperPass::getAnalysisUsage(
|
|
|
|
AnalysisUsage &AU) const {
|
2020-05-29 02:26:15 +02:00
|
|
|
AU.setPreservesAll();
|
2018-11-27 00:05:48 +01:00
|
|
|
AU.addRequired<StackSafetyInfoWrapperPass>();
|
|
|
|
}
|
|
|
|
|
2018-11-27 00:05:58 +01:00
|
|
|
bool StackSafetyGlobalInfoWrapperPass::runOnModule(Module &M) {
|
2020-06-02 10:19:57 +02:00
|
|
|
const ModuleSummaryIndex *ImportSummary = nullptr;
|
|
|
|
if (auto *IndexWrapperPass =
|
|
|
|
getAnalysisIfAvailable<ImmutableModuleSummaryIndexWrapperPass>())
|
|
|
|
ImportSummary = IndexWrapperPass->getIndex();
|
|
|
|
|
|
|
|
SSGI = {&M,
|
|
|
|
[this](Function &F) -> const StackSafetyInfo & {
|
2020-05-28 10:07:31 +02:00
|
|
|
return getAnalysis<StackSafetyInfoWrapperPass>(F).getResult();
|
2020-06-02 10:19:57 +02:00
|
|
|
},
|
|
|
|
ImportSummary};
|
2020-05-29 02:26:15 +02:00
|
|
|
return false;
|
2018-11-27 00:05:58 +01:00
|
|
|
}
|
2018-11-27 00:05:48 +01:00
|
|
|
|
2020-06-01 08:49:57 +02:00
|
|
|
bool llvm::needsParamAccessSummary(const Module &M) {
|
2020-06-02 10:19:57 +02:00
|
|
|
if (StackSafetyRun)
|
|
|
|
return true;
|
2020-06-01 08:49:57 +02:00
|
|
|
for (auto &F : M.functions())
|
|
|
|
if (F.hasFnAttribute(Attribute::SanitizeMemTag))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-05 10:11:35 +02:00
|
|
|
void llvm::generateParamAccessSummary(ModuleSummaryIndex &Index) {
|
2020-08-07 03:52:35 +02:00
|
|
|
if (!Index.hasParamAccess())
|
|
|
|
return;
|
2020-06-05 10:11:35 +02:00
|
|
|
const ConstantRange FullSet(FunctionSummary::ParamAccess::RangeWidth, true);
|
2020-08-07 22:58:10 +02:00
|
|
|
|
2020-08-08 01:18:10 +02:00
|
|
|
auto CountParamAccesses = [&](auto &Stat) {
|
2020-08-07 22:58:10 +02:00
|
|
|
if (!AreStatisticsEnabled())
|
|
|
|
return;
|
|
|
|
for (auto &GVS : Index)
|
|
|
|
for (auto &GV : GVS.second.SummaryList)
|
|
|
|
if (FunctionSummary *FS = dyn_cast<FunctionSummary>(GV.get()))
|
2020-08-08 01:18:10 +02:00
|
|
|
Stat += FS->paramAccesses().size();
|
2020-08-07 22:58:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
CountParamAccesses(NumCombinedParamAccessesBefore);
|
|
|
|
|
2020-06-05 10:11:35 +02:00
|
|
|
std::map<const FunctionSummary *, FunctionInfo<FunctionSummary>> Functions;
|
|
|
|
|
|
|
|
// Convert the ModuleSummaryIndex to a FunctionMap
|
|
|
|
for (auto &GVS : Index) {
|
|
|
|
for (auto &GV : GVS.second.SummaryList) {
|
|
|
|
FunctionSummary *FS = dyn_cast<FunctionSummary>(GV.get());
|
2020-08-02 22:56:38 +02:00
|
|
|
if (!FS || FS->paramAccesses().empty())
|
2020-06-05 10:11:35 +02:00
|
|
|
continue;
|
|
|
|
if (FS->isLive() && FS->isDSOLocal()) {
|
|
|
|
FunctionInfo<FunctionSummary> FI;
|
|
|
|
for (auto &PS : FS->paramAccesses()) {
|
|
|
|
auto &US =
|
|
|
|
FI.Params
|
|
|
|
.emplace(PS.ParamNo, FunctionSummary::ParamAccess::RangeWidth)
|
|
|
|
.first->second;
|
|
|
|
US.Range = PS.Use;
|
|
|
|
for (auto &Call : PS.Calls) {
|
|
|
|
assert(!Call.Offsets.isFullSet());
|
2020-08-06 14:28:03 +02:00
|
|
|
FunctionSummary *S =
|
|
|
|
findCalleeFunctionSummary(Call.Callee, FS->modulePath());
|
2020-08-06 14:19:12 +02:00
|
|
|
++NumCombinedCalleeLookupTotal;
|
2020-06-05 10:11:35 +02:00
|
|
|
if (!S) {
|
2020-08-06 14:19:12 +02:00
|
|
|
++NumCombinedCalleeLookupFailed;
|
2020-06-05 10:11:35 +02:00
|
|
|
US.Range = FullSet;
|
|
|
|
US.Calls.clear();
|
|
|
|
break;
|
|
|
|
}
|
2020-08-14 10:14:02 +02:00
|
|
|
US.Calls.emplace(CallInfo<FunctionSummary>(S, Call.ParamNo),
|
|
|
|
Call.Offsets);
|
2020-06-05 10:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Functions.emplace(FS, std::move(FI));
|
|
|
|
}
|
|
|
|
// Reset data for all summaries. Alive and DSO local will be set back from
|
|
|
|
// of data flow results below. Anything else will not be accessed
|
|
|
|
// by ThinLTO backend, so we can save on bitcode size.
|
|
|
|
FS->setParamAccesses({});
|
|
|
|
}
|
|
|
|
}
|
2020-08-08 23:00:42 +02:00
|
|
|
NumCombinedDataFlowNodes += Functions.size();
|
2020-06-05 10:11:35 +02:00
|
|
|
StackSafetyDataFlowAnalysis<FunctionSummary> SSDFA(
|
|
|
|
FunctionSummary::ParamAccess::RangeWidth, std::move(Functions));
|
|
|
|
for (auto &KV : SSDFA.run()) {
|
|
|
|
std::vector<FunctionSummary::ParamAccess> NewParams;
|
|
|
|
NewParams.reserve(KV.second.Params.size());
|
2020-06-13 04:52:55 +02:00
|
|
|
for (auto &Param : KV.second.Params) {
|
2020-08-08 02:25:50 +02:00
|
|
|
// It's not needed as FullSet is processed the same as a missing value.
|
|
|
|
if (Param.second.Range.isFullSet())
|
|
|
|
continue;
|
2020-06-05 10:11:35 +02:00
|
|
|
NewParams.emplace_back();
|
|
|
|
FunctionSummary::ParamAccess &New = NewParams.back();
|
2020-06-13 04:52:55 +02:00
|
|
|
New.ParamNo = Param.first;
|
|
|
|
New.Use = Param.second.Range; // Only range is needed.
|
2020-06-05 10:11:35 +02:00
|
|
|
}
|
|
|
|
const_cast<FunctionSummary *>(KV.first)->setParamAccesses(
|
|
|
|
std::move(NewParams));
|
|
|
|
}
|
2020-08-07 22:58:10 +02:00
|
|
|
|
|
|
|
CountParamAccesses(NumCombinedParamAccessesAfter);
|
2020-06-05 10:11:35 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 22:57:47 +01:00
|
|
|
static const char LocalPassArg[] = "stack-safety-local";
|
|
|
|
static const char LocalPassName[] = "Stack Safety Local Analysis";
|
|
|
|
INITIALIZE_PASS_BEGIN(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName,
|
|
|
|
false, true)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName,
|
|
|
|
false, true)
|
2018-11-27 00:05:48 +01:00
|
|
|
|
|
|
|
static const char GlobalPassName[] = "Stack Safety Analysis";
|
|
|
|
INITIALIZE_PASS_BEGIN(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE,
|
2020-05-29 02:26:15 +02:00
|
|
|
GlobalPassName, false, true)
|
2018-11-27 00:05:48 +01:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass)
|
2020-06-02 10:19:57 +02:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(ImmutableModuleSummaryIndexWrapperPass)
|
2018-11-27 00:05:48 +01:00
|
|
|
INITIALIZE_PASS_END(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE,
|
2020-05-29 02:26:15 +02:00
|
|
|
GlobalPassName, false, true)
|