12#include "llvm/Support/ErrorHandling.h"
29 : Persistent(Persistent), BlockLocal(BlockLocal) {}
32 return Persistent ==
Other.Persistent && BlockLocal ==
Other.BlockLocal;
37 void dump(llvm::raw_ostream &OS,
const OriginManager &OM)
const {
38 if (Persistent.isEmpty() && BlockLocal.isEmpty())
40 for (
const LivenessMap &Live : {Persistent, BlockLocal})
41 for (
const auto &Entry : Live) {
43 const LivenessInfo &Info = Entry.second;
55 llvm_unreachable(
"liveness kind of live origins should not be dead.");
57 OS <<
" live at this point\n";
63 if (
const auto *UF = F.dyn_cast<
const UseFact *>())
64 return UF->getUseExpr()->getExprLoc();
66 if (
auto *ReturnEsc = dyn_cast<ReturnEscapeFact>(OEF))
67 return ReturnEsc->getReturnExpr()->getExprLoc();
68 if (
auto *FieldEsc = dyn_cast<FieldEscapeFact>(OEF))
69 return FieldEsc->getFieldDecl()->getLocation();
70 if (
auto *GlobalEsc = dyn_cast<GlobalEscapeFact>(OEF))
71 return GlobalEsc->getGlobal()->getLocation();
73 llvm_unreachable(
"unhandled causing fact in PointerUnion");
83 AnalysisImpl(
const CFG &
C, AnalysisDeclContext &AC, FactManager &F,
84 LivenessMap::Factory &SF)
85 : DataflowAnalysis(
C, AC, F), FactMgr(F), Factory(SF),
86 PersistentOrigins(F.getPersistentOrigins()) {}
89 StringRef getAnalysisName()
const {
return "LiveOrigins"; }
91 Lattice getInitialState() {
92 return Lattice(Factory.getEmptyMap(), Factory.getEmptyMap());
103 Lattice transferAtBlockExit(Lattice L) {
104 return Lattice(L.Persistent, Factory.getEmptyMap());
110 Lattice
join(Lattice L1, Lattice L2)
const {
111 assert(L1.BlockLocal.isEmpty() && L2.BlockLocal.isEmpty() &&
112 "block-local origins must not reach a block boundary");
120 return GetFactLoc(A) < GetFactLoc(B) ? A : B;
131 auto CombineLivenessInfo = [&](
const LivenessInfo *L1,
132 const LivenessInfo *L2) -> LivenessInfo {
133 assert((L1 || L2) &&
"unexpectedly merging 2 empty sets");
138 return LivenessInfo(CombineCausingFact(L1->CausingFact, L2->CausingFact),
139 CombineLivenessKind(L1->Kind, L2->Kind));
144 utils::join(L1.Persistent, L2.Persistent, Factory, CombineLivenessInfo,
146 return Lattice(Joined, Factory.getEmptyMap());
152 Lattice
transfer(Lattice In,
const UseFact &UF) {
154 for (
const OriginList *Cur = UF.getUsedOrigins(); Cur;
155 Cur = Cur->peelOuterOrigin()) {
156 OriginID OID = Cur->getOuterOriginID();
159 Out = removeLive(Out, OID);
170 Lattice
transfer(Lattice In,
const OriginEscapesFact &OEF) {
171 return addLive(In, OEF.getEscapedOriginID(),
176 Lattice
transfer(Lattice In,
const IssueFact &IF) {
177 return removeLive(In, IF.getOriginID());
182 Lattice
transfer(Lattice In,
const OriginFlowFact &OF) {
184 OriginID Dest = OF.getDestOriginID();
188 if (
const LivenessInfo *DestInfo = lookupLive(In, Dest))
189 Out = addLive(Out, OF.getSrcOriginID(), *DestInfo);
190 if (OF.getKillDest())
191 Out = removeLive(Out, Dest);
195 Lattice
transfer(Lattice In,
const KillOriginFact &F) {
196 return removeLive(In, F.getKilledOrigin());
199 Lattice
transfer(Lattice In,
const ExpireFact &F) {
200 if (
auto OID = F.getOriginID())
201 return removeLive(In, *OID);
206 Lattice L = getState(P);
207 return LiveOriginSet{L.Persistent, L.BlockLocal};
211 void dump(llvm::raw_ostream &OS,
212 const llvm::StringMap<ProgramPoint> &TestPoints)
const {
213 llvm::dbgs() <<
"==========================================\n";
214 llvm::dbgs() << getAnalysisName() <<
" results:\n";
215 llvm::dbgs() <<
"==========================================\n";
216 for (
const auto &Entry : TestPoints) {
217 OS <<
"TestPoint: " << Entry.getKey() <<
"\n";
218 getState(Entry.getValue()).dump(OS, FactMgr.getOriginMgr());
224 bool isPersistent(
OriginID OID)
const {
225 return PersistentOrigins.test(OID.Value);
228 Lattice addLive(Lattice L,
OriginID OID, LivenessInfo Info) {
229 if (isPersistent(OID))
230 return Lattice(Factory.add(L.Persistent, OID, Info), L.BlockLocal);
231 return Lattice(L.Persistent, Factory.add(L.BlockLocal, OID, Info));
234 Lattice removeLive(Lattice L,
OriginID OID) {
235 if (isPersistent(OID))
236 return Lattice(Factory.remove(L.Persistent, OID), L.BlockLocal);
237 return Lattice(L.Persistent, Factory.remove(L.BlockLocal, OID));
240 const LivenessInfo *lookupLive(
const Lattice &L,
OriginID OID)
const {
241 return isPersistent(OID) ? L.Persistent.lookup(OID)
242 : L.BlockLocal.lookup(OID);
245 FactManager &FactMgr;
246 LivenessMap::Factory &Factory;
249 const llvm::BitVector &PersistentOrigins;
255 using AnalysisImpl::AnalysisImpl;
260 LivenessMap::Factory &SF)
261 : PImpl(
std::make_unique<
Impl>(
C, AC, F, SF)) {
268 return PImpl->getLiveOriginsAt(P);
272 llvm::raw_ostream &OS,
273 const llvm::StringMap<ProgramPoint> &TestPoints)
const {
274 PImpl->dump(OS, TestPoints);
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
A generic, policy-based driver for dataflow analyses.
LiveOriginSet getLiveOriginsAt(ProgramPoint P) const
Returns the set of origins that are live at a specific program point, along with the the details of t...
LiveOriginsAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F, LivenessMap::Factory &SF)
void dump(llvm::raw_ostream &OS, const llvm::StringMap< ProgramPoint > &TestPoints) const
Represents that an origin escapes the current scope through various means.
void transfer(const StmtToEnvMap &StmtToEnv, const Stmt &S, Environment &Env, Environment::ValueModel &Model)
Evaluates S and updates Env accordingly.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
@ Symmetric
A symmetric join applies the JoinValues operation to keys unique to either map, ensuring that values ...
SetTy< T > join(SetTy< T > A, SetTy< T > B, typename SetTy< T >::Factory &F)
Computes the union of two ImmutableSets.
const Fact * ProgramPoint
A ProgramPoint identifies a location in the CFG by pointing to a specific Fact.
utils::ID< struct OriginTag > OriginID
::llvm::PointerUnion< const UseFact *, const OriginEscapesFact * > CausingFactType
utils::MapTy< OriginID, LivenessInfo > LivenessMap
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
bool operator!=(CanQual< T > x, CanQual< U > y)
@ Other
Other implicit parameter.
The origins that are live at a program point.