1515// ===----------------------------------------------------------------------===//
1616
1717#include " llvm/ADT/ArrayRef.h"
18+ #include " llvm/ADT/BitVector.h"
1819#include " llvm/ADT/DenseMap.h"
1920#include " llvm/ADT/STLExtras.h"
2021#include " llvm/ADT/SmallPtrSet.h"
@@ -361,8 +362,7 @@ struct PromoteMem2Reg {
361362 // /
362363 // / That map is used to simplify some Phi nodes as we iterate over it, so
363364 // / it should have deterministic iterators. We could use a MapVector, but
364- // / since we already maintain a map from BasicBlock* to a stable numbering
365- // / (BBNumbers), the DenseMap is more efficient (also supports removal).
365+ // / since basic blocks have numbers, using these are more efficient.
366366 DenseMap<std::pair<unsigned , unsigned >, PHINode *> NewPhiNodes;
367367
368368 // / For each PHI node, keep track of which entry in Allocas it corresponds
@@ -384,14 +384,11 @@ struct PromoteMem2Reg {
384384 SmallSet<DbgVariableRecord *, 8 > DVRAssignsToDelete;
385385
386386 // / The set of basic blocks the renamer has already visited.
387- SmallPtrSet<BasicBlock *, 16 > Visited;
387+ BitVector Visited;
388388
389- // / Contains a stable numbering of basic blocks to avoid non-determinstic
390- // / behavior.
391- DenseMap<BasicBlock *, unsigned > BBNumbers;
392-
393- // / Lazily compute the number of predecessors a block has.
394- DenseMap<const BasicBlock *, unsigned > BBNumPreds;
389+ // / Lazily compute the number of predecessors a block has, indexed by block
390+ // / number.
391+ SmallVector<unsigned > BBNumPreds;
395392
396393 // / Whether the function has the no-signed-zeros-fp-math attribute set.
397394 bool NoSignedZeros = false ;
@@ -414,7 +411,8 @@ struct PromoteMem2Reg {
414411 }
415412
416413 unsigned getNumPreds (const BasicBlock *BB) {
417- unsigned &NP = BBNumPreds[BB];
414+ // BBNumPreds is resized to getMaxBlockNumber() at the beginning.
415+ unsigned &NP = BBNumPreds[BB->getNumber ()];
418416 if (NP == 0 )
419417 NP = pred_size (BB) + 1 ;
420418 return NP - 1 ;
@@ -795,13 +793,9 @@ void PromoteMem2Reg::run() {
795793 continue ;
796794 }
797795
798- // If we haven't computed a numbering for the BB's in the function, do so
799- // now.
800- if (BBNumbers.empty ()) {
801- unsigned ID = 0 ;
802- for (auto &BB : F)
803- BBNumbers[&BB] = ID++;
804- }
796+ // Initialize BBNumPreds lazily
797+ if (BBNumPreds.empty ())
798+ BBNumPreds.resize (F.getMaxBlockNumber ());
805799
806800 // Remember the dbg.declare intrinsic describing this alloca, if any.
807801 if (!Info.DbgUsers .empty ())
@@ -831,8 +825,8 @@ void PromoteMem2Reg::run() {
831825 IDF.setDefiningBlocks (DefBlocks);
832826 SmallVector<BasicBlock *, 32 > PHIBlocks;
833827 IDF.calculate (PHIBlocks);
834- llvm::sort (PHIBlocks, [this ](BasicBlock *A, BasicBlock *B) {
835- return BBNumbers. find (A)-> second < BBNumbers. find (B)-> second ;
828+ llvm::sort (PHIBlocks, [](BasicBlock *A, BasicBlock *B) {
829+ return A-> getNumber () < B-> getNumber () ;
836830 });
837831
838832 unsigned CurrentVersion = 0 ;
@@ -857,6 +851,9 @@ void PromoteMem2Reg::run() {
857851 // locations until proven otherwise.
858852 RenamePassData::LocationVector Locations (Allocas.size ());
859853
854+ // The renamer uses the Visited set to avoid infinite loops.
855+ Visited.resize (F.getMaxBlockNumber ());
856+
860857 // Walks all basic blocks in the function performing the SSA rename algorithm
861858 // and inserting the phi nodes we marked as necessary
862859 std::vector<RenamePassData> RenamePassWorkList;
@@ -869,9 +866,6 @@ void PromoteMem2Reg::run() {
869866 RenamePass (RPD.BB , RPD.Pred , RPD.Values , RPD.Locations , RenamePassWorkList);
870867 } while (!RenamePassWorkList.empty ());
871868
872- // The renamer uses the Visited set to avoid infinite loops. Clear it now.
873- Visited.clear ();
874-
875869 // Remove the allocas themselves from the function.
876870 for (Instruction *A : Allocas) {
877871 // Remove dbg.assigns linked to the alloca as these are now redundant.
@@ -954,8 +948,8 @@ void PromoteMem2Reg::run() {
954948 // Ok, now we know that all of the PHI nodes are missing entries for some
955949 // basic blocks. Start by sorting the incoming predecessors for efficient
956950 // access.
957- auto CompareBBNumbers = [this ](BasicBlock *A, BasicBlock *B) {
958- return BBNumbers. find (A)-> second < BBNumbers. find (B)-> second ;
951+ auto CompareBBNumbers = [](BasicBlock *A, BasicBlock *B) {
952+ return A-> getNumber () < B-> getNumber () ;
959953 };
960954 llvm::sort (Preds, CompareBBNumbers);
961955
@@ -1067,7 +1061,7 @@ void PromoteMem2Reg::ComputeLiveInBlocks(
10671061bool PromoteMem2Reg::QueuePhiNode (BasicBlock *BB, unsigned AllocaNo,
10681062 unsigned &Version) {
10691063 // Look up the basic-block in question.
1070- PHINode *&PN = NewPhiNodes[std::make_pair (BBNumbers[BB] , AllocaNo)];
1064+ PHINode *&PN = NewPhiNodes[std::make_pair (BB-> getNumber () , AllocaNo)];
10711065
10721066 // If the BB already has a phi node added for the i'th alloca then we're done!
10731067 if (PN)
@@ -1165,8 +1159,9 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
11651159 }
11661160
11671161 // Don't revisit blocks.
1168- if (! Visited.insert (BB). second )
1162+ if (Visited.test (BB-> getNumber ()) )
11691163 return ;
1164+ Visited.set (BB->getNumber ());
11701165
11711166 for (BasicBlock::iterator II = BB->begin (); !II->isTerminator ();) {
11721167 Instruction *I = &*II++; // get the instruction, increment iterator
0 commit comments