-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[DeadStoreElimination] Optimize tautological assignments #75744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1901,6 +1901,57 @@ struct DSEState { | |
return true; | ||
} | ||
|
||
// Check if there is a dominating condition, that implies that the value | ||
// being stored in a ptr is already present in the ptr. | ||
bool dominatingConditionImpliesValue(MemoryDef *Def) { | ||
auto *StoreI = cast<StoreInst>(Def->getMemoryInst()); | ||
BasicBlock *StoreBB = StoreI->getParent(); | ||
Value *StorePtr = StoreI->getPointerOperand(); | ||
Value *StoreVal = StoreI->getValueOperand(); | ||
|
||
DomTreeNode *IDom = DT.getNode(StoreBB)->getIDom(); | ||
if (!IDom) | ||
return false; | ||
|
||
auto *BI = dyn_cast<BranchInst>(IDom->getBlock()->getTerminator()); | ||
if (!BI || !BI->isConditional()) | ||
return false; | ||
|
||
// In case both blocks are the same, it is not possible to determine | ||
// if optimization is possible. (We would not want to optimize a store | ||
// in the FalseBB if condition is true and vice versa.) | ||
if (BI->getSuccessor(0) == BI->getSuccessor(1)) | ||
return false; | ||
|
||
Instruction *ICmpL; | ||
ICmpInst::Predicate Pred; | ||
if (!match(BI->getCondition(), | ||
m_c_ICmp(Pred, | ||
m_CombineAnd(m_Load(m_Specific(StorePtr)), | ||
m_Instruction(ICmpL)), | ||
m_Specific(StoreVal))) || | ||
!ICmpInst::isEquality(Pred)) | ||
return false; | ||
nikic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// In case the else blocks also branches to the if block or the other way | ||
// around it is not possible to determine if the optimization is possible. | ||
if (Pred == ICmpInst::ICMP_EQ && | ||
!DT.dominates(BasicBlockEdge(BI->getParent(), BI->getSuccessor(0)), | ||
StoreBB)) | ||
return false; | ||
|
||
if (Pred == ICmpInst::ICMP_NE && | ||
!DT.dominates(BasicBlockEdge(BI->getParent(), BI->getSuccessor(1)), | ||
StoreBB)) | ||
return false; | ||
|
||
MemoryAccess *LoadAcc = MSSA.getMemoryAccess(ICmpL); | ||
MemoryAccess *ClobAcc = | ||
MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def, BatchAA); | ||
|
||
return MSSA.dominates(ClobAcc, LoadAcc); | ||
} | ||
|
||
/// \returns true if \p Def is a no-op store, either because it | ||
/// directly stores back a loaded value or stores zero to a calloced object. | ||
bool storeIsNoop(MemoryDef *Def, const Value *DefUO) { | ||
|
@@ -1931,6 +1982,9 @@ struct DSEState { | |
if (!Store) | ||
return false; | ||
|
||
if (dominatingConditionImpliesValue(Def)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should pass the |
||
return true; | ||
|
||
if (auto *LoadI = dyn_cast<LoadInst>(Store->getOperand(0))) { | ||
if (LoadI->getPointerOperand() == Store->getOperand(1)) { | ||
// Get the defining access for the load. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: We don't need StorePtr and StoreVal until later, so we might move these down.