[PATCH] D136524: [InstCombine] Handle select inst when eliminating constant memcpy
Anshil Gandhi via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 18 14:45:12 PST 2023
gandhi21299 updated this revision to Diff 490302.
gandhi21299 added a comment.
Herald added a subscriber: StephenFan.
- Rebased against the latest revision.
- Check if the true and false values of SelectInst are already in the Worklist before inserting the SelectInst itself into the Worklist.
- Define a new SelectInst while preserving the attached MetaData.
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
llvm/test/Transforms/InstCombine/replace-alloca-phi.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
@@ -337,6 +337,39 @@
ret i32 %v
}
+define i8 @select_same_addrspace_remove_alloca(i1 %cond, ptr %p) {
+; CHECK-LABEL: @select_same_addrspace_remove_alloca(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[PTR:%.*]] = select i1 %cond, ptr [[G1:@.*]], ptr [[P:%.*]]
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr [[PTR]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+entry:
+ %alloca = alloca [32 x i8]
+ call void @llvm.memcpy.p0.p0.i64(ptr %alloca, ptr @g1, i64 256, i1 false)
+ %ptr = select i1 %cond, ptr %alloca, ptr %p
+ %load = load i8, ptr %ptr
+ ret i8 %load
+}
+
+define i8 @select_diff_addrspace_keep_alloca(i1 %cond, ptr addrspace(1) %p) {
+; CHECK-LABEL: @select_diff_addrspace_keep_alloca(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: %alloca = alloca [32 x i8], align 1, addrspace(1)
+; CHECK-NEXT: call void @llvm.memcpy.p1.p0.i64(ptr addrspace(1) noundef align 1 dereferenceable(256) %alloca, ptr noundef nonnull align 16 dereferenceable(256) @g1, i64 256, i1 false)
+; CHECK-NEXT: %ptr = select i1 %cond, ptr addrspace(1) %alloca, ptr addrspace(1) %p
+; CHECK-NEXT: %load = load i8, ptr addrspace(1) %ptr, align 1
+; CHECK-NEXT: ret i8 %load
+;
+entry:
+ %alloca = alloca [32 x i8], addrspace(1)
+ call void @llvm.memcpy.p1.p0.i64(ptr addrspace(1) %alloca, ptr @g1, i64 256, i1 false)
+ %ptr = select i1 %cond, ptr addrspace(1) %alloca, ptr addrspace(1) %p
+ %load = load i8, ptr addrspace(1) %ptr
+ ret i8 %load
+}
+
declare void @llvm.memcpy.p1.p0.i64(ptr addrspace(1), ptr, i64, i1)
declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1)
declare void @llvm.memcpy.p0.p1.i64(ptr, ptr addrspace(1), i64, i1)
+declare void @llvm.memcpy.p5i8.p4i8.i64([32 x i8] addrspace(5)*, [32 x i8] addrspace(4)*, i64, i1)
Index: llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -79,7 +79,7 @@
Worklist.emplace_back(I, true);
continue;
}
- if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I)) {
+ if (isa<BitCastInst, AddrSpaceCastInst, SelectInst>(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.490302.patch
Type: text/x-patch
Size: 3966 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230118/bef9f35d/attachment.bin>
More information about the llvm-commits
mailing list