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) {
2840 if (PersistentOrigins.test (OID.Value ))
2941 return ;
30- auto It = OriginToBlock. find ( OID) ;
31- if (It == OriginToBlock. end () )
32- OriginToBlock[OID] = B;
33- else if (It-> second != B) {
42+ auto &FirstSeenBlock = OriginToFirstSeenBlock[ OID. Value ] ;
43+ if (FirstSeenBlock == nullptr )
44+ FirstSeenBlock = B;
45+ if (FirstSeenBlock != B) {
3446 // We saw this origin in more than one block.
3547 PersistentOrigins.set (OID.Value );
3648 }
@@ -50,7 +62,7 @@ static llvm::BitVector computePersistentOrigins(FactManager &FactMgr,
5062 CheckOrigin (F->getAs <ReturnOfOriginFact>()->getReturnedOriginID ());
5163 break ;
5264 case Fact::Kind::Use:
53- CheckOrigin (F->getAs <UseFact>()->getUsedOrigin (FactMgr. getOriginMgr () ));
65+ CheckOrigin (F->getAs <UseFact>()->getUsedOrigin ());
5466 break ;
5567 case Fact::Kind::Expire:
5668 case Fact::Kind::TestPoint:
@@ -195,9 +207,9 @@ class AnalysisImpl
195207
196208 OriginLoanMap::Factory &OriginLoanMapFactory;
197209 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.
210+ // / Boolean vector indexed by origin ID. If true, the origin appears in
211+ // / multiple basic blocks and must participate in join operations. If false,
212+ // / the origin is block-local and can be discarded at block boundaries.
201213 llvm::BitVector PersistentOrigins;
202214};
203215} // namespace
0 commit comments