diff options
Diffstat (limited to 'llvm/lib/Transforms/Utils')
| -rw-r--r-- | llvm/lib/Transforms/Utils/Local.cpp | 31 | ||||
| -rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 12 |
2 files changed, 32 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index f153ace5d3fc..eeb0446c1197 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -1247,7 +1247,9 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB, return true; } -static bool EliminateDuplicatePHINodesNaiveImpl(BasicBlock *BB) { +static bool +EliminateDuplicatePHINodesNaiveImpl(BasicBlock *BB, + SmallPtrSetImpl<PHINode *> &ToRemove) { // This implementation doesn't currently consider undef operands // specially. Theoretically, two phis which are identical except for // one having an undef where the other doesn't could be collapsed. @@ -1263,12 +1265,14 @@ static bool EliminateDuplicatePHINodesNaiveImpl(BasicBlock *BB) { // Note that we only look in the upper square's triangle, // we already checked that the lower triangle PHI's aren't identical. for (auto J = I; PHINode *DuplicatePN = dyn_cast<PHINode>(J); ++J) { + if (ToRemove.contains(DuplicatePN)) + continue; if (!DuplicatePN->isIdenticalToWhenDefined(PN)) continue; // A duplicate. Replace this PHI with the base PHI. ++NumPHICSEs; DuplicatePN->replaceAllUsesWith(PN); - DuplicatePN->eraseFromParent(); + ToRemove.insert(DuplicatePN); Changed = true; // The RAUW can change PHIs that we already visited. @@ -1279,7 +1283,9 @@ static bool EliminateDuplicatePHINodesNaiveImpl(BasicBlock *BB) { return Changed; } -static bool EliminateDuplicatePHINodesSetBasedImpl(BasicBlock *BB) { +static bool +EliminateDuplicatePHINodesSetBasedImpl(BasicBlock *BB, + SmallPtrSetImpl<PHINode *> &ToRemove) { // This implementation doesn't currently consider undef operands // specially. Theoretically, two phis which are identical except for // one having an undef where the other doesn't could be collapsed. @@ -1343,12 +1349,14 @@ static bool EliminateDuplicatePHINodesSetBasedImpl(BasicBlock *BB) { // Examine each PHI. bool Changed = false; for (auto I = BB->begin(); PHINode *PN = dyn_cast<PHINode>(I++);) { + if (ToRemove.contains(PN)) + continue; auto Inserted = PHISet.insert(PN); if (!Inserted.second) { // A duplicate. Replace this PHI with its duplicate. ++NumPHICSEs; PN->replaceAllUsesWith(*Inserted.first); - PN->eraseFromParent(); + ToRemove.insert(PN); Changed = true; // The RAUW can change PHIs that we already visited. Start over from the @@ -1361,14 +1369,23 @@ static bool EliminateDuplicatePHINodesSetBasedImpl(BasicBlock *BB) { return Changed; } -bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) { +bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB, + SmallPtrSetImpl<PHINode *> &ToRemove) { if ( #ifndef NDEBUG !PHICSEDebugHash && #endif hasNItemsOrLess(BB->phis(), PHICSENumPHISmallSize)) - return EliminateDuplicatePHINodesNaiveImpl(BB); - return EliminateDuplicatePHINodesSetBasedImpl(BB); + return EliminateDuplicatePHINodesNaiveImpl(BB, ToRemove); + return EliminateDuplicatePHINodesSetBasedImpl(BB, ToRemove); +} + +bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) { + SmallPtrSet<PHINode *, 8> ToRemove; + bool Changed = EliminateDuplicatePHINodes(BB, ToRemove); + for (PHINode *PN : ToRemove) + PN->eraseFromParent(); + return Changed; } /// If the specified pointer points to an object that we control, try to modify diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index d3a9a41aef15..bd7ab7c98781 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -6051,8 +6051,9 @@ SwitchLookupTable::SwitchLookupTable( bool LinearMappingPossible = true; APInt PrevVal; APInt DistToPrev; - // When linear map is monotonic, we can attach nsw. - bool Wrapped = false; + // When linear map is monotonic and signed overflow doesn't happen on + // maximum index, we can attach nsw on Add and Mul. + bool NonMonotonic = false; assert(TableSize >= 2 && "Should be a SingleValue table."); // Check if there is the same distance between two consecutive values. for (uint64_t I = 0; I < TableSize; ++I) { @@ -6072,7 +6073,7 @@ SwitchLookupTable::SwitchLookupTable( LinearMappingPossible = false; break; } - Wrapped |= + NonMonotonic |= Dist.isStrictlyPositive() ? Val.sle(PrevVal) : Val.sgt(PrevVal); } PrevVal = Val; @@ -6080,7 +6081,10 @@ SwitchLookupTable::SwitchLookupTable( if (LinearMappingPossible) { LinearOffset = cast<ConstantInt>(TableContents[0]); LinearMultiplier = ConstantInt::get(M.getContext(), DistToPrev); - LinearMapValWrapped = Wrapped; + bool MayWrap = false; + APInt M = LinearMultiplier->getValue(); + (void)M.smul_ov(APInt(M.getBitWidth(), TableSize - 1), MayWrap); + LinearMapValWrapped = NonMonotonic || MayWrap; Kind = LinearMapKind; ++NumLinearMaps; return; |
