diff options
author | Peter Rong <PeterRong96@gmail.com> | 2023-03-28 05:14:23 -0700 |
---|---|---|
committer | Peter Rong <PeterRong96@gmail.com> | 2023-03-30 00:35:56 -0700 |
commit | 670c92a415d0c646dc9f6b08a0f0c817d17ebb46 (patch) | |
tree | 4c1ebdfc1334c06b223ebda36113eebb5e69309b /llvm/lib/CodeGen/CodeGenPrepare.cpp | |
parent | 8c124e3c416f51701732e9bae5e072f56357217a (diff) | |
download | llvm-670c92a415d0c646dc9f6b08a0f0c817d17ebb46.zip llvm-670c92a415d0c646dc9f6b08a0f0c817d17ebb46.tar.gz llvm-670c92a415d0c646dc9f6b08a0f0c817d17ebb46.tar.bz2 |
[CodeGen] Remove redundent instructions generated by combineAddrModes.
CodeGenPare may optimize memory access modes.
During such optimization, it might create a new instruction representing combined value.
Later, If the optimization failed, the generated value is not removed and remains a dead instruction.
Normally this won't be a problem as dead code will be eliminated later.
However, in this case (Issue 58538), the generated instruction may trigger an infinite loop.
The infinite loop involves `sinkCmpExpression`, where it tries to optimize the placeholder generated by us.
(See the test case detailed in the issue)
To fix this, we remove the unnecessary placeholder immediately when we abort the optimization.
`AddressingModeCombiner` will keep track of the placeholder, and remove it if it is an inserted placeholder and has no uses.
This patch fixes https://github.com/llvm/llvm-project/issues/58538, a test is also included.
Reviewed By: skatkov
Differential Revision: https://reviews.llvm.org/D147041
Diffstat (limited to 'llvm/lib/CodeGen/CodeGenPrepare.cpp')
-rw-r--r-- | llvm/lib/CodeGen/CodeGenPrepare.cpp | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp index 16196b0..ddbba80 100644 --- a/llvm/lib/CodeGen/CodeGenPrepare.cpp +++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -3559,10 +3559,15 @@ private: /// Original Address. Value *Original; + /// Common value among addresses + Value *CommonValue = nullptr; + public: AddressingModeCombiner(const SimplifyQuery &_SQ, Value *OriginalValue) : SQ(_SQ), Original(OriginalValue) {} + ~AddressingModeCombiner() { eraseCommonValueIfDead(); } + /// Get the combined AddrMode const ExtAddrMode &getAddrMode() const { return AddrModes[0]; } @@ -3647,13 +3652,21 @@ public: if (!initializeMap(Map)) return false; - Value *CommonValue = findCommon(Map); + CommonValue = findCommon(Map); if (CommonValue) AddrModes[0].SetCombinedField(DifferentField, CommonValue, AddrModes); return CommonValue != nullptr; } private: + /// `CommonValue` may be a placeholder inserted by us. + /// If the placeholder is not used, we should remove this dead instruction. + void eraseCommonValueIfDead() { + if (CommonValue && CommonValue->getNumUses() == 0) + if (Instruction *CommonInst = dyn_cast<Instruction>(CommonValue)) + CommonInst->eraseFromParent(); + } + /// Initialize Map with anchor values. For address seen /// we set the value of different field saw in this address. /// At the same time we find a common type for different field we will |