6262#include " swift/SIL/InstructionUtils.h"
6363#include " swift/SIL/Projection.h"
6464#include " swift/SIL/LoopInfo.h"
65+ #include " swift/SIL/SILBitfield.h"
6566#include " swift/SIL/SILCloner.h"
6667#include " llvm/ADT/SmallSet.h"
6768#include " llvm/Support/CommandLine.h"
@@ -86,18 +87,20 @@ class ArrayPropertiesAnalysis {
8687 llvm::DenseMap<SILFunction *, uint32_t > InstCountCache;
8788 llvm::SmallSet<SILValue, 16 > HoistableArray;
8889
89- SmallPtrSet<SILBasicBlock *, 16 > ReachingBlocks;
90- SmallPtrSet <SILBasicBlock *, 16 > CachedExitingBlocks;
90+ BasicBlockSet ReachingBlocks;
91+ SmallVector <SILBasicBlock *, 16 > CachedExitingBlocks;
9192
9293 // This controls the max instructions the analysis can scan before giving up
9394 const uint32_t AnalysisThreshold = 5000 ;
9495 // This controls the max threshold for instruction count in the loop
9596 const uint32_t LoopInstCountThreshold = 500 ;
9697
98+ bool reachingBlocksComputed = false ;
99+
97100public:
98101 ArrayPropertiesAnalysis (SILLoop *L, DominanceAnalysis *DA)
99102 : Fun(L->getHeader ()->getParent()), Loop(L), Preheader(nullptr ),
100- DomTree(DA->get (Fun)) {}
103+ DomTree(DA->get (Fun)), ReachingBlocks(Fun) {}
101104
102105 // / Check if it is profitable to specialize a loop when you see an apply
103106 // / instruction. We consider it is not profitable to specialize the loop when:
@@ -239,18 +242,19 @@ class ArrayPropertiesAnalysis {
239242 return V;
240243 }
241244
242- SmallPtrSetImpl<SILBasicBlock *> &getReachingBlocks () {
243- if (ReachingBlocks. empty () ) {
245+ BasicBlockSet &getReachingBlocks () {
246+ if (!reachingBlocksComputed ) {
244247 SmallVector<SILBasicBlock *, 8 > Worklist;
245248 ReachingBlocks.insert (Preheader);
246249 Worklist.push_back (Preheader);
247250 while (!Worklist.empty ()) {
248251 SILBasicBlock *BB = Worklist.pop_back_val ();
249252 for (auto PI = BB->pred_begin (), PE = BB->pred_end (); PI != PE; ++PI) {
250- if (ReachingBlocks.insert (*PI). second )
253+ if (ReachingBlocks.insert (*PI))
251254 Worklist.push_back (*PI);
252255 }
253256 }
257+ reachingBlocksComputed = true ;
254258 }
255259 return ReachingBlocks;
256260 }
@@ -279,7 +283,7 @@ class ArrayPropertiesAnalysis {
279283
280284 // Check if this escape can reach the current loop.
281285 if (!Loop->contains (UseInst->getParent ()) &&
282- !getReachingBlocks ().count (UseInst->getParent ())) {
286+ !getReachingBlocks ().contains (UseInst->getParent ())) {
283287 continue ;
284288 }
285289 LLVM_DEBUG (llvm::dbgs ()
@@ -356,12 +360,10 @@ class ArrayPropertiesAnalysis {
356360 return false ;
357361 }
358362
359- SmallPtrSetImpl <SILBasicBlock *> &getLoopExitingBlocks () {
363+ SmallVectorImpl <SILBasicBlock *> &getLoopExitingBlocks () {
360364 if (!CachedExitingBlocks.empty ())
361365 return CachedExitingBlocks;
362- SmallVector<SILBasicBlock *, 16 > ExitingBlocks;
363- Loop->getExitingBlocks (ExitingBlocks);
364- CachedExitingBlocks.insert (ExitingBlocks.begin (), ExitingBlocks.end ());
366+ Loop->getExitingBlocks (CachedExitingBlocks);
365367 return CachedExitingBlocks;
366368 }
367369
0 commit comments