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 = dyn_cast<const UseFact *>(F))
64 return UF->getUseExpr()->getExprLoc();
65 if (
const auto *OEF = dyn_cast<const OriginEscapesFact *>(F)) {
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());
151 Lattice
transfer(Lattice In,
const UseFact &UF) {
153 for (
const OriginList *Cur = UF.getUsedOrigins(); Cur;
154 Cur = Cur->peelOuterOrigin())
155 Out = addLive(Out, Cur->getOuterOriginID(),
162 Lattice
transfer(Lattice In,
const OriginEscapesFact &OEF) {
163 return addLive(In, OEF.getEscapedOriginID(),
168 Lattice
transfer(Lattice In,
const IssueFact &IF) {
169 return removeLive(In, IF.getOriginID());
174 Lattice
transfer(Lattice In,
const OriginFlowFact &OF) {
176 OriginID Dest = OF.getDestOriginID();
180 if (
const LivenessInfo *DestInfo = lookupLive(In, Dest))
181 Out = addLive(Out, OF.getSrcOriginID(), *DestInfo);
182 if (OF.getKillDest())
183 Out = removeLive(Out, Dest);
187 Lattice
transfer(Lattice In,
const KillOriginFact &F) {
188 return removeLive(In, F.getKilledOrigin());
191 Lattice
transfer(Lattice In,
const ExpireFact &F) {
192 if (
auto OID = F.getOriginID())
193 return removeLive(In, *OID);
198 Lattice L = getState(P);
199 return LiveOriginSet{L.Persistent, L.BlockLocal};
203 void dump(llvm::raw_ostream &OS,
204 const llvm::StringMap<ProgramPoint> &TestPoints)
const {
205 llvm::dbgs() <<
"==========================================\n";
206 llvm::dbgs() << getAnalysisName() <<
" results:\n";
207 llvm::dbgs() <<
"==========================================\n";
208 for (
const auto &Entry : TestPoints) {
209 OS <<
"TestPoint: " << Entry.getKey() <<
"\n";
210 getState(Entry.getValue()).dump(OS, FactMgr.getOriginMgr());
216 bool isPersistent(
OriginID OID)
const {
217 return PersistentOrigins.test(OID.Value);
220 Lattice addLive(Lattice L,
OriginID OID, LivenessInfo Info) {
221 if (isPersistent(OID))
222 return Lattice(Factory.add(L.Persistent, OID, Info), L.BlockLocal);
223 return Lattice(L.Persistent, Factory.add(L.BlockLocal, OID, Info));
226 Lattice removeLive(Lattice L,
OriginID OID) {
227 if (isPersistent(OID))
228 return Lattice(Factory.remove(L.Persistent, OID), L.BlockLocal);
229 return Lattice(L.Persistent, Factory.remove(L.BlockLocal, OID));
232 const LivenessInfo *lookupLive(
const Lattice &L,
OriginID OID)
const {
233 return isPersistent(OID) ? L.Persistent.lookup(OID)
234 : L.BlockLocal.lookup(OID);
237 FactManager &FactMgr;
238 LivenessMap::Factory &Factory;
241 const llvm::BitVector &PersistentOrigins;
247 using AnalysisImpl::AnalysisImpl;
252 LivenessMap::Factory &SF)
253 : PImpl(
std::make_unique<
Impl>(
C, AC, F, SF)) {
260 return PImpl->getLiveOriginsAt(P);
264 llvm::raw_ostream &OS,
265 const llvm::StringMap<ProgramPoint> &TestPoints)
const {
266 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
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.