73
73
#define LLVM_TRANSFORMS_UTILS_MEMORYSSA_H
74
74
75
75
#include " llvm/ADT/DenseMap.h"
76
+ #include " llvm/ADT/DenseSet.h"
76
77
#include " llvm/ADT/GraphTraits.h"
77
- #include " llvm/ADT/iterator_range.h"
78
78
#include " llvm/ADT/SmallPtrSet.h"
79
79
#include " llvm/ADT/SmallVector.h"
80
80
#include " llvm/ADT/ilist.h"
81
81
#include " llvm/ADT/ilist_node.h"
82
82
#include " llvm/ADT/iterator.h"
83
+ #include " llvm/ADT/iterator_range.h"
83
84
#include " llvm/Analysis/AliasAnalysis.h"
84
85
#include " llvm/Analysis/MemoryLocation.h"
85
86
#include " llvm/Analysis/PHITransAddr.h"
@@ -108,6 +109,11 @@ class Instruction;
108
109
class MemoryAccess ;
109
110
class LLVMContext ;
110
111
class raw_ostream ;
112
+ namespace MSSAHelpers {
113
+
114
+ struct AllAccessTag {};
115
+ struct DefsOnlyTag {};
116
+ }
111
117
112
118
enum {
113
119
// Used to signify what the default invalid ID is for MemoryAccess's
@@ -122,8 +128,16 @@ using const_memoryaccess_def_iterator =
122
128
123
129
// \brief The base for all memory accesses. All memory accesses in a block are
124
130
// linked together using an intrusive list.
125
- class MemoryAccess : public User , public ilist_node <MemoryAccess> {
131
+ class MemoryAccess
132
+ : public User,
133
+ public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>,
134
+ public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>> {
126
135
public:
136
+ using AllAccessType =
137
+ ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>;
138
+ using DefsOnlyType =
139
+ ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>>;
140
+
127
141
// Methods for support type inquiry through isa, cast, and
128
142
// dyn_cast
129
143
static inline bool classof (const MemoryAccess *) { return true ; }
@@ -156,6 +170,33 @@ class MemoryAccess : public User, public ilist_node<MemoryAccess> {
156
170
memoryaccess_def_iterator defs_end ();
157
171
const_memoryaccess_def_iterator defs_end () const ;
158
172
173
+ // / \brief Get the iterators for the all access list and the defs only list
174
+ // / We default to the all access list.
175
+ AllAccessType::self_iterator getIterator () {
176
+ return this ->AllAccessType ::getIterator ();
177
+ }
178
+ AllAccessType::const_self_iterator getIterator () const {
179
+ return this ->AllAccessType ::getIterator ();
180
+ }
181
+ AllAccessType::reverse_self_iterator getReverseIterator () {
182
+ return this ->AllAccessType ::getReverseIterator ();
183
+ }
184
+ AllAccessType::const_reverse_self_iterator getReverseIterator () const {
185
+ return this ->AllAccessType ::getReverseIterator ();
186
+ }
187
+ DefsOnlyType::self_iterator getDefsIterator () {
188
+ return this ->DefsOnlyType ::getIterator ();
189
+ }
190
+ DefsOnlyType::const_self_iterator getDefsIterator () const {
191
+ return this ->DefsOnlyType ::getIterator ();
192
+ }
193
+ DefsOnlyType::reverse_self_iterator getReverseDefsIterator () {
194
+ return this ->DefsOnlyType ::getReverseIterator ();
195
+ }
196
+ DefsOnlyType::const_reverse_self_iterator getReverseDefsIterator () const {
197
+ return this ->DefsOnlyType ::getReverseIterator ();
198
+ }
199
+
159
200
protected:
160
201
friend class MemorySSA ;
161
202
friend class MemoryUseOrDef ;
@@ -531,7 +572,14 @@ class MemorySSA {
531
572
return LiveOnEntryDef.get ();
532
573
}
533
574
534
- using AccessList = iplist<MemoryAccess>;
575
+ // Sadly, iplists, by default, owns and deletes pointers added to the
576
+ // list. It's not currently possible to have two iplists for the same type,
577
+ // where one owns the pointers, and one does not. This is because the traits
578
+ // are per-type, not per-tag. If this ever changes, we should make the
579
+ // DefList an iplist.
580
+ using AccessList = iplist<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>;
581
+ using DefsList =
582
+ simple_ilist<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>>;
535
583
536
584
// / \brief Return the list of MemoryAccess's for a given basic block.
537
585
// /
@@ -540,6 +588,14 @@ class MemorySSA {
540
588
return getWritableBlockAccesses (BB);
541
589
}
542
590
591
+ // / \brief Return the list of MemoryDef's and MemoryPhi's for a given basic
592
+ // / block.
593
+ // /
594
+ // / This list is not modifiable by the user.
595
+ const DefsList *getBlockDefs (const BasicBlock *BB) const {
596
+ return getWritableBlockDefs (BB);
597
+ }
598
+
543
599
// / \brief Create an empty MemoryPhi in MemorySSA for a given basic block.
544
600
// / Only one MemoryPhi for a block exists at a time, so this function will
545
601
// / assert if you try to create one where it already exists.
@@ -623,12 +679,18 @@ class MemorySSA {
623
679
void verifyDomination (Function &F) const ;
624
680
void verifyOrdering (Function &F) const ;
625
681
626
- // This is used by the use optimizer class
682
+ // This is used by the use optimizer and updater.
627
683
AccessList *getWritableBlockAccesses (const BasicBlock *BB) const {
628
684
auto It = PerBlockAccesses.find (BB);
629
685
return It == PerBlockAccesses.end () ? nullptr : It->second .get ();
630
686
}
631
687
688
+ // This is used by the use optimizer and updater.
689
+ DefsList *getWritableBlockDefs (const BasicBlock *BB) const {
690
+ auto It = PerBlockDefs.find (BB);
691
+ return It == PerBlockDefs.end () ? nullptr : It->second .get ();
692
+ }
693
+
632
694
private:
633
695
class CachingWalker ;
634
696
class OptimizeUses ;
@@ -639,6 +701,7 @@ class MemorySSA {
639
701
640
702
void verifyUseInDefs (MemoryAccess *, MemoryAccess *) const ;
641
703
using AccessMap = DenseMap<const BasicBlock *, std::unique_ptr<AccessList>>;
704
+ using DefsMap = DenseMap<const BasicBlock *, std::unique_ptr<DefsList>>;
642
705
643
706
void
644
707
determineInsertionPoint (const SmallPtrSetImpl<BasicBlock *> &DefiningBlocks);
@@ -656,15 +719,26 @@ class MemorySSA {
656
719
void renamePass (DomTreeNode *, MemoryAccess *IncomingVal,
657
720
SmallPtrSet<BasicBlock *, 16 > &Visited);
658
721
AccessList *getOrCreateAccessList (const BasicBlock *);
722
+ DefsList *getOrCreateDefsList (const BasicBlock *);
659
723
void renumberBlock (const BasicBlock *) const ;
660
-
724
+ void insertIntoListsForBlock (MemoryAccess *, const BasicBlock *,
725
+ InsertionPlace);
726
+ void insertIntoListsBefore (MemoryAccess *, const BasicBlock *,
727
+ AccessList::iterator);
661
728
AliasAnalysis *AA;
662
729
DominatorTree *DT;
663
730
Function &F;
664
731
665
732
// Memory SSA mappings
666
733
DenseMap<const Value *, MemoryAccess *> ValueToMemoryAccess;
734
+ // These two mappings contain the main block to access/def mappings for
735
+ // MemorySSA. The list contained in PerBlockAccesses really owns all the
736
+ // MemoryAccesses.
737
+ // Both maps maintain the invariant that if a block is found in them, the
738
+ // corresponding list is not empty, and if a block is not found in them, the
739
+ // corresponding list is empty.
667
740
AccessMap PerBlockAccesses;
741
+ DefsMap PerBlockDefs;
668
742
std::unique_ptr<MemoryAccess> LiveOnEntryDef;
669
743
670
744
// Domination mappings
@@ -957,9 +1031,7 @@ class upward_defs_iterator
957
1031
fillInCurrentPair ();
958
1032
}
959
1033
960
- upward_defs_iterator () {
961
- CurrentPair.first = nullptr ;
962
- }
1034
+ upward_defs_iterator () { CurrentPair.first = nullptr ; }
963
1035
964
1036
bool operator ==(const upward_defs_iterator &Other) const {
965
1037
return DefIterator == Other.DefIterator ;
0 commit comments