[PATCH] D136524: [InstCombine] Handle select inst when eliminating constant memcpy

Anshil Gandhi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 22 03:18:45 PDT 2022


gandhi21299 updated this revision to Diff 469877.
gandhi21299 added a comment.

- removed `#include "llvm/IR/Instruction.h"`
- ran update_test_checks.py on the test


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/replace-alloca-merge.ll


Index: llvm/test/Transforms/InstCombine/replace-alloca-merge.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/replace-alloca-merge.ll
@@ -0,0 +1,47 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=instcombine -S -o - %s | FileCheck %s
+
+target triple="amdgcn-amd-amdhsa"
+
+define i8 @remove_alloca_use_arg([32 x i8] addrspace(4)* noalias readonly align 4 dereferenceable(32) %arg, i1 %cond) {
+; CHECK-LABEL: @remove_alloca_use_arg(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[VAL1:%.*]] = getelementptr [32 x i8], [32 x i8] addrspace(4)* [[ARG:%.*]], i64 0, i64 1
+; CHECK-NEXT:    [[VAL2:%.*]] = getelementptr [32 x i8], [32 x i8] addrspace(4)* [[ARG]], i64 0, i64 0
+; CHECK-NEXT:    [[VAL1_VAL:%.*]] = load i8, i8 addrspace(4)* [[VAL1]], align 1
+; CHECK-NEXT:    [[VAL2_VAL:%.*]] = load i8, i8 addrspace(4)* [[VAL2]], align 4
+; CHECK-NEXT:    [[LOAD:%.*]] = select i1 [[COND:%.*]], i8 [[VAL1_VAL]], i8 [[VAL2_VAL]]
+; CHECK-NEXT:    ret i8 [[LOAD]]
+;
+entry:
+  %alloca = alloca [32 x i8], align 4, addrspace(5)
+  call void @llvm.memcpy.p5i8.p4i8.i64([32 x i8] addrspace(5)* %alloca, [32 x i8] addrspace(4)* %arg, i64 256, i1 false)
+  %val1 = getelementptr inbounds [32 x i8], [32 x i8] addrspace(5)* %alloca, i32 0, i32 1
+  %val2 = getelementptr inbounds [32 x i8], [32 x i8] addrspace(5)* %alloca, i32 0, i32 0
+  %ptr = select i1 %cond, i8 addrspace(5)* %val1, i8 addrspace(5)* %val2
+  %load = load i8, i8 addrspace(5)* %ptr
+  ret i8 %load
+}
+
+define i8 @volatile_load_keep_alloca([32 x i8] addrspace(4)* noalias readonly align 4 dereferenceable(32) %arg, i1 %cond) {
+; CHECK-LABEL: @volatile_load_keep_alloca(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[ALLOCA:%.*]] = alloca [32 x i8], align 4, addrspace(5)
+; CHECK-NEXT:    call void @llvm.memcpy.p5a32i8.p4a32i8.i64([32 x i8] addrspace(5)* noundef nonnull align 4 dereferenceable(256) [[ALLOCA]], [32 x i8] addrspace(4)* noundef align 4 dereferenceable(256) [[ARG:%.*]], i64 256, i1 false)
+; CHECK-NEXT:    [[VAL1:%.*]] = getelementptr inbounds [32 x i8], [32 x i8] addrspace(5)* [[ALLOCA]], i64 0, i64 1
+; CHECK-NEXT:    [[VAL2:%.*]] = getelementptr inbounds [32 x i8], [32 x i8] addrspace(5)* [[ALLOCA]], i64 0, i64 0
+; CHECK-NEXT:    [[PTR:%.*]] = select i1 [[COND:%.*]], i8 addrspace(5)* [[VAL1]], i8 addrspace(5)* [[VAL2]]
+; CHECK-NEXT:    [[LOAD:%.*]] = load volatile i8, i8 addrspace(5)* [[PTR]], align 1
+; CHECK-NEXT:    ret i8 [[LOAD]]
+;
+entry:
+  %alloca = alloca [32 x i8], align 4, addrspace(5)
+  call void @llvm.memcpy.p5i8.p4i8.i64([32 x i8] addrspace(5)* %alloca, [32 x i8] addrspace(4)* %arg, i64 256, i1 false)
+  %val1 = getelementptr inbounds [32 x i8], [32 x i8] addrspace(5)* %alloca, i32 0, i32 1
+  %val2 = getelementptr inbounds [32 x i8], [32 x i8] addrspace(5)* %alloca, i32 0, i32 0
+  %ptr = select i1 %cond, i8 addrspace(5)* %val1, i8 addrspace(5)* %val2
+  %load = load volatile i8, i8 addrspace(5)* %ptr
+  ret i8 %load
+}
+
+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
@@ -60,7 +60,8 @@
         continue;
       }
 
-      if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I)) {
+      if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I) ||
+          isa<SelectInst>(I)) {
         // If uses of the bitcast are ok, we are ok.
         ValuesToInspect.emplace_back(I, IsOffset);
         continue;
@@ -256,6 +257,12 @@
       if (Load->isVolatile())
         return false;
       Worklist.insert(Load);
+    } else if (auto *SI = dyn_cast<SelectInst>(Inst)) {
+      Worklist.insert(cast<Instruction>(SI->getTrueValue()));
+      Worklist.insert(cast<Instruction>(SI->getFalseValue()));
+      Worklist.insert(Inst);
+      if (!collectUsers(*Inst))
+        return false;
     } else if (isa<GetElementPtrInst>(Inst) || isa<BitCastInst>(Inst)) {
       Worklist.insert(Inst);
       if (!collectUsers(*Inst))
@@ -313,6 +320,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()));
+    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.469877.patch
Type: text/x-patch
Size: 4936 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221022/e3375a0c/attachment.bin>


More information about the llvm-commits mailing list