summaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
diff options
context:
space:
mode:
authorYingwei Zheng <dtcxzyw2333@gmail.com>2025-02-20 23:53:45 +0800
committerGitHub <noreply@github.com>2025-02-20 23:53:45 +0800
commit1b78ff6972789d189fe0b403b403626328bf6a65 (patch)
tree973191d4f473754698346a91ea9c3110d12b090d /llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
parent43f2968a028fbc6b4fac5ba6d18ad9d27eb4ef76 (diff)
[InstCombine] Simplify the pointer operand of store if writing to null is UB (#127979)
Proof: https://alive2.llvm.org/ce/z/mzVj-u I will add some follow-up patches to avoid duplicate code, support more memory instructions, and bypass gep instructions.
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index 4b42e86e2516..d5534c15cca7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -1437,6 +1437,20 @@ Instruction *InstCombinerImpl::visitStoreInst(StoreInst &SI) {
if (isa<UndefValue>(Val))
return eraseInstFromFunction(SI);
+ // TODO: Add a helper to simplify the pointer operand for all memory
+ // instructions.
+ // store val, (select (cond, null, P)) -> store val, P
+ // store val, (select (cond, P, null)) -> store val, P
+ if (!NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace())) {
+ if (SelectInst *Sel = dyn_cast<SelectInst>(Ptr)) {
+ if (isa<ConstantPointerNull>(Sel->getOperand(1)))
+ return replaceOperand(SI, 1, Sel->getOperand(2));
+
+ if (isa<ConstantPointerNull>(Sel->getOperand(2)))
+ return replaceOperand(SI, 1, Sel->getOperand(1));
+ }
+ }
+
return nullptr;
}