55// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66//
77// ===----------------------------------------------------------------------===//
8- #include " clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h"
8+ #include < cassert>
9+ #include < memory>
10+
911#include " Dataflow.h"
12+ #include " clang/Analysis/Analyses/LifetimeSafety/Facts.h"
13+ #include " clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h"
14+ #include " clang/Analysis/Analyses/LifetimeSafety/Loans.h"
15+ #include " clang/Analysis/Analyses/LifetimeSafety/Origins.h"
16+ #include " clang/Analysis/Analyses/LifetimeSafety/Utils.h"
17+ #include " clang/Analysis/AnalysisDeclContext.h"
18+ #include " clang/Analysis/CFG.h"
19+ #include " clang/Basic/LLVM.h"
1020#include " llvm/ADT/BitVector.h"
21+ #include " llvm/ADT/SmallVector.h"
1122#include " llvm/Support/TimeProfiler.h"
12- #include < memory >
23+ #include " llvm/Support/raw_ostream.h "
1324
1425namespace clang ::lifetimes::internal {
1526
16- // Pre-pass to find persistent origins. An origin is persistent if it is
27+ // Prepass to find persistent origins. An origin is persistent if it is
1728// referenced in more than one basic block.
18- static llvm::BitVector computePersistentOrigins (FactManager &FactMgr,
29+ static llvm::BitVector computePersistentOrigins (const FactManager &FactMgr,
1930 const CFG &C) {
2031 llvm::TimeTraceScope (" ComputePersistentOrigins" );
21- llvm::BitVector PersistentOrigins ( FactMgr.getOriginMgr ().getOrigins (). size () +
22- 1 );
32+ unsigned NumOrigins = FactMgr.getOriginMgr ().getNumOrigins ();
33+ llvm::BitVector PersistentOrigins (NumOrigins );
2334
24- llvm::DenseMap<OriginID, const CFGBlock *> OriginToBlock;
35+ llvm::SmallVector<const CFGBlock *> OriginToFirstSeenBlock (NumOrigins,
36+ nullptr );
2537 for (const CFGBlock *B : C) {
2638 for (const Fact *F : FactMgr.getFacts (B)) {
2739 auto CheckOrigin = [&](OriginID OID) {
40+ // llvm::errs() << "OID: " << OID.Value << "/"
41+ // << FactMgr.getOriginMgr().getNumOrigins()
42+ // << ", original num origins: " << NumOrigins << "\n";
2843 if (PersistentOrigins.test (OID.Value ))
2944 return ;
30- auto It = OriginToBlock. find ( OID) ;
31- if (It == OriginToBlock. end () )
32- OriginToBlock[OID] = B;
33- else if (It-> second != B) {
45+ auto &FirstSeenBlock = OriginToFirstSeenBlock[ OID. Value ] ;
46+ if (FirstSeenBlock == nullptr )
47+ FirstSeenBlock = B;
48+ if (FirstSeenBlock != B) {
3449 // We saw this origin in more than one block.
3550 PersistentOrigins.set (OID.Value );
3651 }
@@ -50,7 +65,7 @@ static llvm::BitVector computePersistentOrigins(FactManager &FactMgr,
5065 CheckOrigin (F->getAs <ReturnOfOriginFact>()->getReturnedOriginID ());
5166 break ;
5267 case Fact::Kind::Use:
53- CheckOrigin (F->getAs <UseFact>()->getUsedOrigin (FactMgr. getOriginMgr () ));
68+ CheckOrigin (F->getAs <UseFact>()->getUsedOrigin ());
5469 break ;
5570 case Fact::Kind::Expire:
5671 case Fact::Kind::TestPoint:
@@ -195,9 +210,9 @@ class AnalysisImpl
195210
196211 OriginLoanMap::Factory &OriginLoanMapFactory;
197212 LoanSet::Factory &LoanSetFactory;
198- // / Origins that appear in multiple basic blocks and must participate in join
199- // / operations. Origins not in this set are block-local and can be discarded
200- // / at block boundaries.
213+ // / Boolean vector indexed by origin ID. If true, the origin appears in
214+ // / multiple basic blocks and must participate in join operations. If false,
215+ // / the origin is block-local and can be discarded at block boundaries.
201216 llvm::BitVector PersistentOrigins;
202217};
203218} // namespace
0 commit comments