@@ -675,12 +675,14 @@ join(llvm::ImmutableMap<K, V> A, llvm::ImmutableMap<K, V> B,
675675// TODO(opt): Consider using a bitset to represent the set of loans.
676676using LoanSet = llvm::ImmutableSet<LoanID>;
677677using OriginLoanMap = llvm::ImmutableMap<OriginID, LoanSet>;
678+ using OriginSet = llvm::ImmutableSet<OriginID>;
678679
679680// / An object to hold the factories for immutable collections, ensuring
680681// / that all created states share the same underlying memory management.
681682struct LifetimeFactory {
682683 OriginLoanMap::Factory OriginMapFactory;
683684 LoanSet::Factory LoanSetFactory;
685+ OriginSet::Factory OriginSetFactory;
684686
685687 // / Creates a singleton set containing only the given loan ID.
686688 LoanSet createLoanSet (LoanID LID) {
@@ -777,6 +779,78 @@ class LoanPropagationAnalysis
777779 }
778780};
779781
782+ // ========================================================================= //
783+ // Live Origins Analysis
784+ // ========================================================================= //
785+
786+ // / The dataflow lattice for origin liveness analysis.
787+ // / It tracks the set of origins that are live at a given program point.
788+ struct LivenessLattice {
789+ OriginSet LiveOrigins;
790+
791+ LivenessLattice () : LiveOrigins(nullptr ) {};
792+ explicit LivenessLattice (OriginSet S) : LiveOrigins(S) {}
793+
794+ bool operator ==(const LivenessLattice &Other) const {
795+ return LiveOrigins == Other.LiveOrigins ;
796+ }
797+ bool operator !=(const LivenessLattice &Other) const {
798+ return !(*this == Other);
799+ }
800+
801+ void dump (llvm::raw_ostream &OS) const {
802+ OS << " LivenessLattice State:\n " ;
803+ if (LiveOrigins.isEmpty ())
804+ OS << " <empty>\n " ;
805+ for (const OriginID &OID : LiveOrigins)
806+ OS << " Origin " << OID << " is live\n " ;
807+ }
808+ };
809+
810+ // / The analysis that tracks which origins are live. This is a backward
811+ // / analysis.
812+ class LiveOriginAnalysis
813+ : public DataflowAnalysis<LiveOriginAnalysis, LivenessLattice,
814+ Direction::Backward> {
815+
816+ OriginSet::Factory &SetFactory;
817+
818+ public:
819+ LiveOriginAnalysis (const CFG &C, AnalysisDeclContext &AC, FactManager &F,
820+ OriginSet::Factory &SF)
821+ : DataflowAnalysis(C, AC, F), SetFactory(SF) {}
822+
823+ using DataflowAnalysis<LiveOriginAnalysis, Lattice,
824+ Direction::Backward>::transfer;
825+
826+ StringRef getAnalysisName () const { return " LiveOrigins" ; }
827+
828+ Lattice getInitialState () { return Lattice (SetFactory.getEmptySet ()); }
829+
830+ // / Merges two lattices by taking the union of the live origin sets.
831+ Lattice join (Lattice L1, Lattice L2) const {
832+ return Lattice (utils::join (L1.LiveOrigins , L2.LiveOrigins , SetFactory));
833+ }
834+
835+ // / An assignment `p = q` kills the liveness of `p` and generates liveness
836+ // / for `q`.
837+ Lattice transfer (Lattice In, const AssignOriginFact &F) {
838+ OriginSet S = SetFactory.remove (In.LiveOrigins , F.getDestOriginID ());
839+ S = SetFactory.add (S, F.getSrcOriginID ());
840+ return Lattice (S);
841+ }
842+
843+ // / Issuing a new loan to an origin kills its liveness.
844+ Lattice transfer (Lattice In, const IssueFact &F) {
845+ return Lattice (SetFactory.remove (In.LiveOrigins , F.getOriginID ()));
846+ }
847+
848+ // / A return statement generates liveness for the returned origin.
849+ Lattice transfer (Lattice In, const ReturnOfOriginFact &F) {
850+ return Lattice (SetFactory.add (In.LiveOrigins , F.getReturnedOriginID ()));
851+ }
852+ };
853+
780854// ========================================================================= //
781855// Expired Loans Analysis
782856// ========================================================================= //
@@ -872,5 +946,9 @@ void runLifetimeSafetyAnalysis(const DeclContext &DC, const CFG &Cfg,
872946 ExpiredLoansAnalysis ExpiredLoans (Cfg, AC, FactMgr, Factory);
873947 ExpiredLoans.run ();
874948 DEBUG_WITH_TYPE (" LifetimeExpiredLoans" , ExpiredLoans.dump ());
949+
950+ LiveOriginAnalysis Liveness (Cfg, AC, FactMgr, Factory.OriginSetFactory );
951+ Liveness.run ();
952+ DEBUG_WITH_TYPE (" LifetimeLiveOrigins" , Liveness.dump ());
875953}
876954} // namespace clang
0 commit comments