[PATCH] D136524: [InstCombine] Handle select inst when eliminating constant memcpy
Anshil Gandhi via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 20 15:09:52 PST 2023
gandhi21299 updated this revision to Diff 490985.
gandhi21299 added a comment.
Run update_test_checks on ptr-replace-alloca.ll
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D136524/new/
https://reviews.llvm.org/D136524
Files:
llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
llvm/test/Transforms/InstCombine/ptr-replace-alloca.ll
Index: llvm/test/Transforms/InstCombine/ptr-replace-alloca.ll
===================================================================
--- llvm/test/Transforms/InstCombine/ptr-replace-alloca.ll
+++ llvm/test/Transforms/InstCombine/ptr-replace-alloca.ll
@@ -340,9 +340,7 @@
define i8 @select_same_addrspace_remove_alloca(i1 %cond, ptr %p) {
; CHECK-LABEL: @select_same_addrspace_remove_alloca(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [32 x i8], align 1
-; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) [[ALLOCA]], ptr noundef nonnull align 16 dereferenceable(32) @g1, i64 32, i1 false)
-; CHECK-NEXT: [[PTR:%.*]] = select i1 [[COND:%.*]], ptr [[ALLOCA]], ptr [[P:%.*]]
+; CHECK-NEXT: [[PTR:%.*]] = select i1 [[COND:%.*]], ptr @g1, ptr [[P:%.*]]
; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr [[PTR]], align 1
; CHECK-NEXT: ret i8 [[LOAD]]
;
Index: llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -72,14 +72,14 @@
continue;
}
- if (isa<PHINode>(I)) {
+ if (isa<PHINode, SelectInst>(I)) {
// We set IsOffset=true, to forbid the memcpy from occurring after the
// phi: If one of the phi operands is not based on the alloca, we
// would incorrectly omit a write.
Worklist.emplace_back(I, true);
continue;
}
- if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I)) {
+ if (isa<BitCastInst, AddrSpaceCastInst>(I)) {
// If uses of the bitcast are ok, we are ok.
Worklist.emplace_back(I, IsOffset);
continue;
@@ -315,6 +315,19 @@
Worklist.insert(PHI);
if (!collectUsersRecursive(*PHI))
return false;
+ } else if (auto *SI = dyn_cast<SelectInst>(Inst)) {
+ if (!isa<Instruction>(SI->getTrueValue()) ||
+ !isa<Instruction>(SI->getFalseValue()))
+ return false;
+
+ if (!Worklist.contains(cast<Instruction>(SI->getTrueValue())) ||
+ !Worklist.contains(cast<Instruction>(SI->getFalseValue()))) {
+ ValuesToRevisit.insert(Inst);
+ continue;
+ }
+ Worklist.insert(SI);
+ if (!collectUsersRecursive(*SI))
+ return false;
} else if (isa<GetElementPtrInst, BitCastInst>(Inst)) {
Worklist.insert(Inst);
if (!collectUsersRecursive(*Inst))
@@ -380,6 +393,13 @@
IC.InsertNewInstWith(NewI, *BC);
NewI->takeName(BC);
WorkMap[BC] = NewI;
+ } else if (auto *SI = dyn_cast<SelectInst>(I)) {
+ auto *NewSI = SelectInst::Create(
+ SI->getCondition(), getReplacement(SI->getTrueValue()),
+ getReplacement(SI->getFalseValue()), SI->getName(), nullptr, SI);
+ IC.InsertNewInstWith(NewSI, *SI);
+ NewSI->takeName(SI);
+ WorkMap[SI] = NewSI;
} else if (auto *MemCpy = dyn_cast<MemTransferInst>(I)) {
auto *SrcV = getReplacement(MemCpy->getRawSource());
// The pointer may appear in the destination of a copy, but we don't want to
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136524.490985.patch
Type: text/x-patch
Size: 3185 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230120/4bf70b10/attachment.bin>
More information about the llvm-commits
mailing list