15
15
// ===----------------------------------------------------------------------===//
16
16
17
17
#include " llvm/ADT/ArrayRef.h"
18
+ #include " llvm/ADT/BitVector.h"
18
19
#include " llvm/ADT/DenseMap.h"
19
20
#include " llvm/ADT/STLExtras.h"
20
21
#include " llvm/ADT/SmallPtrSet.h"
@@ -361,8 +362,7 @@ struct PromoteMem2Reg {
361
362
// /
362
363
// / That map is used to simplify some Phi nodes as we iterate over it, so
363
364
// / 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.
366
366
DenseMap<std::pair<unsigned , unsigned >, PHINode *> NewPhiNodes;
367
367
368
368
// / For each PHI node, keep track of which entry in Allocas it corresponds
@@ -384,14 +384,11 @@ struct PromoteMem2Reg {
384
384
SmallSet<DbgVariableRecord *, 8 > DVRAssignsToDelete;
385
385
386
386
// / The set of basic blocks the renamer has already visited.
387
- SmallPtrSet<BasicBlock *, 16 > Visited;
387
+ BitVector Visited;
388
388
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;
395
392
396
393
// / Whether the function has the no-signed-zeros-fp-math attribute set.
397
394
bool NoSignedZeros = false ;
@@ -414,7 +411,8 @@ struct PromoteMem2Reg {
414
411
}
415
412
416
413
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 ()];
418
416
if (NP == 0 )
419
417
NP = pred_size (BB) + 1 ;
420
418
return NP - 1 ;
@@ -795,13 +793,9 @@ void PromoteMem2Reg::run() {
795
793
continue ;
796
794
}
797
795
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 ());
805
799
806
800
// Remember the dbg.declare intrinsic describing this alloca, if any.
807
801
if (!Info.DbgUsers .empty ())
@@ -831,8 +825,8 @@ void PromoteMem2Reg::run() {
831
825
IDF.setDefiningBlocks (DefBlocks);
832
826
SmallVector<BasicBlock *, 32 > PHIBlocks;
833
827
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 () ;
836
830
});
837
831
838
832
unsigned CurrentVersion = 0 ;
@@ -857,6 +851,9 @@ void PromoteMem2Reg::run() {
857
851
// locations until proven otherwise.
858
852
RenamePassData::LocationVector Locations (Allocas.size ());
859
853
854
+ // The renamer uses the Visited set to avoid infinite loops.
855
+ Visited.resize (F.getMaxBlockNumber ());
856
+
860
857
// Walks all basic blocks in the function performing the SSA rename algorithm
861
858
// and inserting the phi nodes we marked as necessary
862
859
std::vector<RenamePassData> RenamePassWorkList;
@@ -869,9 +866,6 @@ void PromoteMem2Reg::run() {
869
866
RenamePass (RPD.BB , RPD.Pred , RPD.Values , RPD.Locations , RenamePassWorkList);
870
867
} while (!RenamePassWorkList.empty ());
871
868
872
- // The renamer uses the Visited set to avoid infinite loops. Clear it now.
873
- Visited.clear ();
874
-
875
869
// Remove the allocas themselves from the function.
876
870
for (Instruction *A : Allocas) {
877
871
// Remove dbg.assigns linked to the alloca as these are now redundant.
@@ -954,8 +948,8 @@ void PromoteMem2Reg::run() {
954
948
// Ok, now we know that all of the PHI nodes are missing entries for some
955
949
// basic blocks. Start by sorting the incoming predecessors for efficient
956
950
// 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 () ;
959
953
};
960
954
llvm::sort (Preds, CompareBBNumbers);
961
955
@@ -1067,7 +1061,7 @@ void PromoteMem2Reg::ComputeLiveInBlocks(
1067
1061
bool PromoteMem2Reg::QueuePhiNode (BasicBlock *BB, unsigned AllocaNo,
1068
1062
unsigned &Version) {
1069
1063
// 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)];
1071
1065
1072
1066
// If the BB already has a phi node added for the i'th alloca then we're done!
1073
1067
if (PN)
@@ -1165,8 +1159,9 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
1165
1159
}
1166
1160
1167
1161
// Don't revisit blocks.
1168
- if (! Visited.insert (BB). second )
1162
+ if (Visited.test (BB-> getNumber ()) )
1169
1163
return ;
1164
+ Visited.set (BB->getNumber ());
1170
1165
1171
1166
for (BasicBlock::iterator II = BB->begin (); !II->isTerminator ();) {
1172
1167
Instruction *I = &*II++; // get the instruction, increment iterator
0 commit comments