24
24
#include " llvm/ADT/Twine.h"
25
25
#include " llvm/ADT/iterator_range.h"
26
26
#include " llvm/IR/BasicBlock.h"
27
+ #include " llvm/IR/ValueHandle.h"
27
28
#include " llvm/Support/BlockFrequency.h"
28
29
#include " llvm/Support/BranchProbability.h"
29
30
#include " llvm/Support/CommandLine.h"
@@ -547,19 +548,24 @@ namespace bfi_detail {
547
548
template <class BlockT > struct TypeMap {};
548
549
template <> struct TypeMap <BasicBlock> {
549
550
using BlockT = BasicBlock;
551
+ using BlockKeyT = AssertingVH<const BasicBlock>;
550
552
using FunctionT = Function;
551
553
using BranchProbabilityInfoT = BranchProbabilityInfo;
552
554
using LoopT = Loop;
553
555
using LoopInfoT = LoopInfo;
554
556
};
555
557
template <> struct TypeMap <MachineBasicBlock> {
556
558
using BlockT = MachineBasicBlock;
559
+ using BlockKeyT = const MachineBasicBlock *;
557
560
using FunctionT = MachineFunction;
558
561
using BranchProbabilityInfoT = MachineBranchProbabilityInfo;
559
562
using LoopT = MachineLoop;
560
563
using LoopInfoT = MachineLoopInfo;
561
564
};
562
565
566
+ template <class BlockT , class BFIImplT >
567
+ class BFICallbackVH ;
568
+
563
569
// / Get the name of a MachineBasicBlock.
564
570
// /
565
571
// / Get the name of a MachineBasicBlock. It's templated so that including from
@@ -845,21 +851,24 @@ template <class BT> class BlockFrequencyInfoImpl : BlockFrequencyInfoImplBase {
845
851
friend struct bfi_detail ::BlockEdgesAdder<BT>;
846
852
847
853
using BlockT = typename bfi_detail::TypeMap<BT>::BlockT;
854
+ using BlockKeyT = typename bfi_detail::TypeMap<BT>::BlockKeyT;
848
855
using FunctionT = typename bfi_detail::TypeMap<BT>::FunctionT;
849
856
using BranchProbabilityInfoT =
850
857
typename bfi_detail::TypeMap<BT>::BranchProbabilityInfoT;
851
858
using LoopT = typename bfi_detail::TypeMap<BT>::LoopT;
852
859
using LoopInfoT = typename bfi_detail::TypeMap<BT>::LoopInfoT;
853
860
using Successor = GraphTraits<const BlockT *>;
854
861
using Predecessor = GraphTraits<Inverse<const BlockT *>>;
862
+ using BFICallbackVH =
863
+ bfi_detail::BFICallbackVH<BlockT, BlockFrequencyInfoImpl>;
855
864
856
865
const BranchProbabilityInfoT *BPI = nullptr ;
857
866
const LoopInfoT *LI = nullptr ;
858
867
const FunctionT *F = nullptr ;
859
868
860
869
// All blocks in reverse postorder.
861
870
std::vector<const BlockT *> RPOT;
862
- DenseMap<const BlockT *, BlockNode> Nodes;
871
+ DenseMap<BlockKeyT, std::pair< BlockNode, BFICallbackVH> > Nodes;
863
872
864
873
using rpot_iterator = typename std::vector<const BlockT *>::const_iterator;
865
874
@@ -871,7 +880,8 @@ template <class BT> class BlockFrequencyInfoImpl : BlockFrequencyInfoImplBase {
871
880
BlockNode getNode (const rpot_iterator &I) const {
872
881
return BlockNode (getIndex (I));
873
882
}
874
- BlockNode getNode (const BlockT *BB) const { return Nodes.lookup (BB); }
883
+
884
+ BlockNode getNode (const BlockT *BB) const { return Nodes.lookup (BB).first ; }
875
885
876
886
const BlockT *getBlock (const BlockNode &Node) const {
877
887
assert (Node.Index < RPOT.size ());
@@ -992,6 +1002,13 @@ template <class BT> class BlockFrequencyInfoImpl : BlockFrequencyInfoImplBase {
992
1002
993
1003
void setBlockFreq (const BlockT *BB, uint64_t Freq);
994
1004
1005
+ void forgetBlock (const BlockT *BB) {
1006
+ // We don't erase corresponding items from `Freqs`, `RPOT` and other to
1007
+ // avoid invalidating indices. Doing so would have saved some memory, but
1008
+ // it's not worth it.
1009
+ Nodes.erase (BB);
1010
+ }
1011
+
995
1012
Scaled64 getFloatingBlockFreq (const BlockT *BB) const {
996
1013
return BlockFrequencyInfoImplBase::getFloatingBlockFreq (getNode (BB));
997
1014
}
@@ -1019,6 +1036,36 @@ template <class BT> class BlockFrequencyInfoImpl : BlockFrequencyInfoImplBase {
1019
1036
}
1020
1037
};
1021
1038
1039
+ namespace bfi_detail {
1040
+
1041
+ template <class BFIImplT >
1042
+ class BFICallbackVH <BasicBlock, BFIImplT> : public CallbackVH {
1043
+ BFIImplT *BFIImpl;
1044
+
1045
+ public:
1046
+ BFICallbackVH () = default ;
1047
+
1048
+ BFICallbackVH (const BasicBlock *BB, BFIImplT *BFIImpl)
1049
+ : CallbackVH(BB), BFIImpl(BFIImpl) {}
1050
+
1051
+ virtual ~BFICallbackVH () = default ;
1052
+
1053
+ void deleted () override {
1054
+ BFIImpl->forgetBlock (cast<BasicBlock>(getValPtr ()));
1055
+ }
1056
+ };
1057
+
1058
+ // / Dummy implementation since MachineBasicBlocks aren't Values, so ValueHandles
1059
+ // / don't apply to them.
1060
+ template <class BFIImplT >
1061
+ class BFICallbackVH <MachineBasicBlock, BFIImplT> {
1062
+ public:
1063
+ BFICallbackVH () = default ;
1064
+ BFICallbackVH (const MachineBasicBlock *, BFIImplT *) {}
1065
+ };
1066
+
1067
+ } // end namespace bfi_detail
1068
+
1022
1069
template <class BT >
1023
1070
void BlockFrequencyInfoImpl<BT>::calculate(const FunctionT &F,
1024
1071
const BranchProbabilityInfoT &BPI,
@@ -1066,7 +1113,7 @@ void BlockFrequencyInfoImpl<BT>::setBlockFreq(const BlockT *BB, uint64_t Freq) {
1066
1113
// BlockNode for it assigned with a new index. The index can be determined
1067
1114
// by the size of Freqs.
1068
1115
BlockNode NewNode (Freqs.size ());
1069
- Nodes[BB] = NewNode;
1116
+ Nodes[BB] = { NewNode, BFICallbackVH (BB, this )} ;
1070
1117
Freqs.emplace_back ();
1071
1118
BlockFrequencyInfoImplBase::setBlockFreq (NewNode, Freq);
1072
1119
}
@@ -1086,7 +1133,7 @@ template <class BT> void BlockFrequencyInfoImpl<BT>::initializeRPOT() {
1086
1133
BlockNode Node = getNode (I);
1087
1134
LLVM_DEBUG (dbgs () << " - " << getIndex (I) << " : " << getBlockName (Node)
1088
1135
<< " \n " );
1089
- Nodes[*I] = Node;
1136
+ Nodes[*I] = { Node, BFICallbackVH (*I, this )} ;
1090
1137
}
1091
1138
1092
1139
Working.reserve (RPOT.size ());
0 commit comments