14 #ifndef LLVM_TRANSFORMS_UTILS_SSAUPDATERIMPL_H 15 #define LLVM_TRANSFORMS_UTILS_SSAUPDATERIMPL_H 23 #define DEBUG_TYPE "ssaupdater" 27 template<
typename T>
class SSAUpdaterTraits;
29 template<
typename UpdaterT>
35 using BlkT =
typename Traits::BlkT;
36 using ValT =
typename Traits::ValT;
37 using PhiT =
typename Traits::PhiT;
57 BBInfo *IDom =
nullptr;
60 unsigned NumPreds = 0;
63 BBInfo **Preds =
nullptr;
66 PhiT *PHITag =
nullptr;
68 BBInfo(BlkT *ThisBB, ValT V)
69 : BB(ThisBB), AvailableVal(V), DefBB(V ?
this :
nullptr) {}
87 Updater(U), AvailableVals(A), InsertedPHIs(Ins) {}
98 if (BlockList.
size() == 0) {
99 ValT V = Traits::GetUndefVal(BB, Updater);
100 (*AvailableVals)[BB] = V;
108 return BBMap[BB]->DefBB->AvailableVal;
119 BBInfo *
Info =
new (Allocator) BBInfo(BB, 0);
127 while (!WorkList.
empty()) {
130 Traits::FindPredecessorBlocks(Info->BB, &Preds);
131 Info->NumPreds = Preds.
size();
132 if (Info->NumPreds == 0)
133 Info->Preds =
nullptr;
135 Info->Preds =
static_cast<BBInfo **
>(Allocator.
Allocate(
136 Info->NumPreds *
sizeof(BBInfo *),
alignof(BBInfo *)));
138 for (
unsigned p = 0; p != Info->NumPreds; ++p) {
139 BlkT *Pred = Preds[p];
143 if (BBMapBucket.second) {
144 Info->Preds[p] = BBMapBucket.second;
149 ValT PredVal = AvailableVals->
lookup(Pred);
150 BBInfo *PredInfo =
new (Allocator) BBInfo(Pred, PredVal);
151 BBMapBucket.second = PredInfo;
152 Info->Preds[p] = PredInfo;
154 if (PredInfo->AvailableVal) {
165 BBInfo *PseudoEntry =
new (Allocator) BBInfo(
nullptr, 0);
169 while (!RootList.
empty()) {
171 Info->IDom = PseudoEntry;
176 while (!WorkList.
empty()) {
177 Info = WorkList.
back();
179 if (Info->BlkNum == -2) {
181 Info->BlkNum = BlkNum++;
183 if (!Info->AvailableVal)
196 for (
typename Traits::BlkSucc_iterator
SI =
197 Traits::BlkSucc_begin(Info->BB),
198 E = Traits::BlkSucc_end(Info->BB);
SI !=
E; ++
SI) {
199 BBInfo *SuccInfo = BBMap[*
SI];
200 if (!SuccInfo || SuccInfo->BlkNum)
202 SuccInfo->BlkNum = -1;
206 PseudoEntry->BlkNum = BlkNum;
215 while (Blk1 != Blk2) {
216 while (Blk1->BlkNum < Blk2->BlkNum) {
221 while (Blk2->BlkNum < Blk1->BlkNum) {
246 E = BlockList->
rend();
I !=
E; ++
I) {
248 BBInfo *NewIDom =
nullptr;
251 for (
unsigned p = 0; p != Info->NumPreds; ++p) {
252 BBInfo *Pred = Info->Preds[p];
255 if (Pred->BlkNum == 0) {
256 Pred->AvailableVal = Traits::GetUndefVal(Pred->BB, Updater);
257 (*AvailableVals)[Pred->BB] = Pred->AvailableVal;
259 Pred->BlkNum = PseudoEntry->BlkNum;
260 PseudoEntry->BlkNum++;
270 if (NewIDom && NewIDom != Info->IDom) {
271 Info->IDom = NewIDom;
283 for (; Pred != IDom; Pred = Pred->IDom) {
284 if (Pred->DefBB == Pred)
300 E = BlockList->
rend();
I !=
E; ++
I) {
304 if (Info->DefBB == Info)
308 BBInfo *NewDefBB = Info->IDom->DefBB;
309 for (
unsigned p = 0; p != Info->NumPreds; ++p) {
318 if (NewDefBB != Info->DefBB) {
319 Info->DefBB = NewDefBB;
336 E = BlockList->
end();
I !=
E; ++
I) {
339 if (Info->DefBB != Info)
344 if (Info->AvailableVal)
347 ValT PHI = Traits::CreateEmptyPHI(Info->BB, Info->NumPreds, Updater);
348 Info->AvailableVal = PHI;
349 (*AvailableVals)[Info->BB] = PHI;
355 E = BlockList->
rend();
I !=
E; ++
I) {
358 if (Info->DefBB != Info) {
361 (*AvailableVals)[Info->BB] = Info->DefBB->AvailableVal;
366 PhiT *PHI = Traits::ValueIsNewPHI(Info->AvailableVal, Updater);
371 for (
unsigned p = 0; p != Info->NumPreds; ++p) {
372 BBInfo *PredInfo = Info->Preds[p];
373 BlkT *Pred = PredInfo->BB;
375 if (PredInfo->DefBB != PredInfo)
376 PredInfo = PredInfo->DefBB;
377 Traits::AddPHIOperand(PHI, PredInfo->AvailableVal, Pred);
383 if (InsertedPHIs) InsertedPHIs->
push_back(PHI);
390 for (
auto &SomePHI : BB->phis()) {
397 E = BlockList->
end();
I !=
E; ++
I)
398 (*I)->PHITag =
nullptr;
409 BBMap[PHI->getParent()]->PHITag = PHI;
411 while (!WorkList.
empty()) {
415 for (
typename Traits::PHI_iterator
I = Traits::PHI_begin(PHI),
416 E = Traits::PHI_end(PHI);
I !=
E; ++
I) {
417 ValT IncomingVal =
I.getIncomingValue();
418 BBInfo *PredInfo = BBMap[
I.getIncomingBlock()];
420 if (PredInfo->DefBB != PredInfo)
421 PredInfo = PredInfo->DefBB;
424 if (PredInfo->AvailableVal) {
425 if (IncomingVal == PredInfo->AvailableVal)
431 PhiT *IncomingPHIVal = Traits::ValueIsPHI(IncomingVal, Updater);
432 if (!IncomingPHIVal || IncomingPHIVal->getParent() != PredInfo->BB)
436 if (PredInfo->PHITag) {
437 if (IncomingPHIVal == PredInfo->PHITag)
441 PredInfo->PHITag = IncomingPHIVal;
453 E = BlockList->
end();
I !=
E; ++
I)
454 if (PhiT *PHI = (*I)->PHITag) {
455 BlkT *BB = PHI->getParent();
456 ValT PHIVal = Traits::GetPHIValue(PHI);
457 (*AvailableVals)[BB] = PHIVal;
458 BBMap[BB]->AvailableVal = PHIVal;
465 #undef DEBUG_TYPE // "ssaupdater" 467 #endif // LLVM_TRANSFORMS_UTILS_SSAUPDATERIMPL_H
SSAUpdaterImpl(UpdaterT *U, AvailableValsTy *A, SmallVectorImpl< PhiT *> *Ins)
This class represents lattice values for constants.
void push_back(const T &Elt)
ValT GetValue(BlkT *BB)
GetValue - Check to see if AvailableVals has an entry for the specified BB and if so...
void FindPHIPlacement(BlockListTy *BlockList)
FindPHIPlacement - PHIs are needed in the iterated dominance frontiers of the known definitions...
void FindExistingPHI(BlkT *BB, BlockListTy *BlockList)
FindExistingPHI - Look through the PHI nodes in a block to see if any of them match what is needed...
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
bool CheckIfPHIMatches(PhiT *PHI)
CheckIfPHIMatches - Check if a PHI node matches the placement and values in the BBMap.
Analysis containing CSE Info
value_type & FindAndConstruct(const KeyT &Key)
Allocate memory in an ever growing pool, as if by bump-pointer.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void FindDominators(BlockListTy *BlockList, BBInfo *PseudoEntry)
FindDominators - Calculate the dominator tree for the subset of the CFG corresponding to the basic bl...
std::reverse_iterator< iterator > reverse_iterator
bool IsDefInDomFrontier(const BBInfo *Pred, const BBInfo *IDom)
IsDefInDomFrontier - Search up the dominator tree from Pred to IDom for any blocks containing definit...
void RecordMatchingPHIs(BlockListTy *BlockList)
RecordMatchingPHIs - For each PHI node that matches, record it in both the BBMap and the AvailableVal...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD T pop_back_val()
void FindAvailableVals(BlockListTy *BlockList)
FindAvailableVal - If this block requires a PHI, first check if an existing PHI matches the PHI place...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
typename SuperClass::iterator iterator
BBInfo * IntersectDominators(BBInfo *Blk1, BBInfo *Blk2)
IntersectDominators - This is the dataflow lattice "meet" operation for finding dominators.
LLVM_NODISCARD bool empty() const
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
reverse_iterator rbegin()
BBInfo * BuildBlockList(BlkT *BB, BlockListTy *BlockList)
BuildBlockList - Starting from the specified basic block, traverse back through its predecessors unti...