13
13
// traversing the function in depth-first order to rewrite loads and stores as
14
14
// appropriate.
15
15
//
16
- // The algorithm used here is based on:
17
- //
18
- // Sreedhar and Gao. A linear time algorithm for placing phi-nodes.
19
- // In Proceedings of the 22nd ACM SIGPLAN-SIGACT Symposium on Principles of
20
- // Programming Languages
21
- // POPL '95. ACM, New York, NY, 62-73.
22
- //
23
- // It has been modified to not explicitly use the DJ graph data structure and to
24
- // directly compute pruned SSA using per-variable liveness information.
25
- //
26
16
// ===----------------------------------------------------------------------===//
27
17
28
18
#include " llvm/Transforms/Utils/PromoteMemToReg.h"
34
24
#include " llvm/ADT/Statistic.h"
35
25
#include " llvm/Analysis/AliasSetTracker.h"
36
26
#include " llvm/Analysis/InstructionSimplify.h"
27
+ #include " llvm/Analysis/IteratedDominanceFrontier.h"
37
28
#include " llvm/Analysis/ValueTracking.h"
38
29
#include " llvm/IR/CFG.h"
39
30
#include " llvm/IR/Constants.h"
48
39
#include " llvm/IR/Module.h"
49
40
#include " llvm/Transforms/Utils/Local.h"
50
41
#include < algorithm>
51
- #include < queue>
52
42
using namespace llvm ;
53
43
54
44
#define DEBUG_TYPE " mem2reg"
@@ -275,9 +265,6 @@ struct PromoteMem2Reg {
275
265
// / behavior.
276
266
DenseMap<BasicBlock *, unsigned > BBNumbers;
277
267
278
- // / Maps DomTreeNodes to their level in the dominator tree.
279
- DenseMap<DomTreeNode *, unsigned > DomLevels;
280
-
281
268
// / Lazily compute the number of predecessors a block has.
282
269
DenseMap<const BasicBlock *, unsigned > BBNumPreds;
283
270
@@ -304,8 +291,6 @@ struct PromoteMem2Reg {
304
291
return NP - 1 ;
305
292
}
306
293
307
- void DetermineInsertionPoint (AllocaInst *AI, unsigned AllocaNum,
308
- AllocaInfo &Info);
309
294
void ComputeLiveInBlocks (AllocaInst *AI, AllocaInfo &Info,
310
295
const SmallPtrSetImpl<BasicBlock *> &DefBlocks,
311
296
SmallPtrSetImpl<BasicBlock *> &LiveInBlocks);
@@ -532,6 +517,7 @@ void PromoteMem2Reg::run() {
532
517
533
518
AllocaInfo Info;
534
519
LargeBlockInfo LBI;
520
+ IDFCalculator IDF (DT);
535
521
536
522
for (unsigned AllocaNum = 0 ; AllocaNum != Allocas.size (); ++AllocaNum) {
537
523
AllocaInst *AI = Allocas[AllocaNum];
@@ -579,31 +565,12 @@ void PromoteMem2Reg::run() {
579
565
continue ;
580
566
}
581
567
582
- // If we haven't computed dominator tree levels, do so now.
583
- if (DomLevels.empty ()) {
584
- SmallVector<DomTreeNode *, 32 > Worklist;
585
-
586
- DomTreeNode *Root = DT.getRootNode ();
587
- DomLevels[Root] = 0 ;
588
- Worklist.push_back (Root);
589
-
590
- while (!Worklist.empty ()) {
591
- DomTreeNode *Node = Worklist.pop_back_val ();
592
- unsigned ChildLevel = DomLevels[Node] + 1 ;
593
- for (DomTreeNode::iterator CI = Node->begin (), CE = Node->end ();
594
- CI != CE; ++CI) {
595
- DomLevels[*CI] = ChildLevel;
596
- Worklist.push_back (*CI);
597
- }
598
- }
599
- }
600
-
601
568
// If we haven't computed a numbering for the BB's in the function, do so
602
569
// now.
603
570
if (BBNumbers.empty ()) {
604
571
unsigned ID = 0 ;
605
- for (Function::iterator I = F. begin (), E = F. end (); I != E; ++I )
606
- BBNumbers[I ] = ID++;
572
+ for (auto &BB : F )
573
+ BBNumbers[&BB ] = ID++;
607
574
}
608
575
609
576
// If we have an AST to keep updated, remember some pointer value that is
@@ -622,7 +589,34 @@ void PromoteMem2Reg::run() {
622
589
// the standard SSA construction algorithm. Determine which blocks need PHI
623
590
// nodes and see if we can optimize out some work by avoiding insertion of
624
591
// dead phi nodes.
625
- DetermineInsertionPoint (AI, AllocaNum, Info);
592
+
593
+
594
+ // Unique the set of defining blocks for efficient lookup.
595
+ SmallPtrSet<BasicBlock *, 32 > DefBlocks;
596
+ DefBlocks.insert (Info.DefiningBlocks .begin (), Info.DefiningBlocks .end ());
597
+
598
+ // Determine which blocks the value is live in. These are blocks which lead
599
+ // to uses.
600
+ SmallPtrSet<BasicBlock *, 32 > LiveInBlocks;
601
+ ComputeLiveInBlocks (AI, Info, DefBlocks, LiveInBlocks);
602
+
603
+ // At this point, we're committed to promoting the alloca using IDF's, and
604
+ // the standard SSA construction algorithm. Determine which blocks need phi
605
+ // nodes and see if we can optimize out some work by avoiding insertion of
606
+ // dead phi nodes.
607
+ IDF.setLiveInBlocks (LiveInBlocks);
608
+ IDF.setDefiningBlocks (DefBlocks);
609
+ SmallVector<BasicBlock *, 32 > PHIBlocks;
610
+ IDF.calculate (PHIBlocks);
611
+ if (PHIBlocks.size () > 1 )
612
+ std::sort (PHIBlocks.begin (), PHIBlocks.end (),
613
+ [this ](BasicBlock *A, BasicBlock *B) {
614
+ return BBNumbers.lookup (A) < BBNumbers.lookup (B);
615
+ });
616
+
617
+ unsigned CurrentVersion = 0 ;
618
+ for (unsigned i = 0 , e = PHIBlocks.size (); i != e; ++i)
619
+ QueuePhiNode (PHIBlocks[i], AllocaNum, CurrentVersion);
626
620
}
627
621
628
622
if (Allocas.empty ())
@@ -844,98 +838,6 @@ void PromoteMem2Reg::ComputeLiveInBlocks(
844
838
}
845
839
}
846
840
847
- // / At this point, we're committed to promoting the alloca using IDF's, and the
848
- // / standard SSA construction algorithm. Determine which blocks need phi nodes
849
- // / and see if we can optimize out some work by avoiding insertion of dead phi
850
- // / nodes.
851
- void PromoteMem2Reg::DetermineInsertionPoint (AllocaInst *AI, unsigned AllocaNum,
852
- AllocaInfo &Info) {
853
- // Unique the set of defining blocks for efficient lookup.
854
- SmallPtrSet<BasicBlock *, 32 > DefBlocks;
855
- DefBlocks.insert (Info.DefiningBlocks .begin (), Info.DefiningBlocks .end ());
856
-
857
- // Determine which blocks the value is live in. These are blocks which lead
858
- // to uses.
859
- SmallPtrSet<BasicBlock *, 32 > LiveInBlocks;
860
- ComputeLiveInBlocks (AI, Info, DefBlocks, LiveInBlocks);
861
-
862
- // Use a priority queue keyed on dominator tree level so that inserted nodes
863
- // are handled from the bottom of the dominator tree upwards.
864
- typedef std::pair<DomTreeNode *, unsigned > DomTreeNodePair;
865
- typedef std::priority_queue<DomTreeNodePair, SmallVector<DomTreeNodePair, 32 >,
866
- less_second> IDFPriorityQueue;
867
- IDFPriorityQueue PQ;
868
-
869
- for (BasicBlock *BB : DefBlocks) {
870
- if (DomTreeNode *Node = DT.getNode (BB))
871
- PQ.push (std::make_pair (Node, DomLevels[Node]));
872
- }
873
-
874
- SmallVector<std::pair<unsigned , BasicBlock *>, 32 > DFBlocks;
875
- SmallVector<DomTreeNode *, 32 > Worklist;
876
- SmallPtrSet<DomTreeNode *, 32 > VisitedPQ;
877
- SmallPtrSet<DomTreeNode *, 32 > VisitedWorklist;
878
-
879
- while (!PQ.empty ()) {
880
- DomTreeNodePair RootPair = PQ.top ();
881
- PQ.pop ();
882
- DomTreeNode *Root = RootPair.first ;
883
- unsigned RootLevel = RootPair.second ;
884
-
885
- // Walk all dominator tree children of Root, inspecting their CFG edges with
886
- // targets elsewhere on the dominator tree. Only targets whose level is at
887
- // most Root's level are added to the iterated dominance frontier of the
888
- // definition set.
889
-
890
- Worklist.clear ();
891
- Worklist.push_back (Root);
892
- VisitedWorklist.insert (Root);
893
-
894
- while (!Worklist.empty ()) {
895
- DomTreeNode *Node = Worklist.pop_back_val ();
896
- BasicBlock *BB = Node->getBlock ();
897
-
898
- for (succ_iterator SI = succ_begin (BB), SE = succ_end (BB); SI != SE;
899
- ++SI) {
900
- DomTreeNode *SuccNode = DT.getNode (*SI);
901
-
902
- // Quickly skip all CFG edges that are also dominator tree edges instead
903
- // of catching them below.
904
- if (SuccNode->getIDom () == Node)
905
- continue ;
906
-
907
- unsigned SuccLevel = DomLevels[SuccNode];
908
- if (SuccLevel > RootLevel)
909
- continue ;
910
-
911
- if (!VisitedPQ.insert (SuccNode).second )
912
- continue ;
913
-
914
- BasicBlock *SuccBB = SuccNode->getBlock ();
915
- if (!LiveInBlocks.count (SuccBB))
916
- continue ;
917
-
918
- DFBlocks.push_back (std::make_pair (BBNumbers[SuccBB], SuccBB));
919
- if (!DefBlocks.count (SuccBB))
920
- PQ.push (std::make_pair (SuccNode, SuccLevel));
921
- }
922
-
923
- for (DomTreeNode::iterator CI = Node->begin (), CE = Node->end (); CI != CE;
924
- ++CI) {
925
- if (VisitedWorklist.insert (*CI).second )
926
- Worklist.push_back (*CI);
927
- }
928
- }
929
- }
930
-
931
- if (DFBlocks.size () > 1 )
932
- std::sort (DFBlocks.begin (), DFBlocks.end ());
933
-
934
- unsigned CurrentVersion = 0 ;
935
- for (unsigned i = 0 , e = DFBlocks.size (); i != e; ++i)
936
- QueuePhiNode (DFBlocks[i].second , AllocaNum, CurrentVersion);
937
- }
938
-
939
841
// / \brief Queue a phi-node to be added to a basic-block for a specific Alloca.
940
842
// /
941
843
// / Returns true if there wasn't already a phi-node for that variable
0 commit comments