[PATCH] D110225: Don't fold (select C, (gep Ptr, Idx), Ptr) if C is vector but Idx is scalar

Yi Kong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 22 01:06:08 PDT 2021


kongyi created this revision.
kongyi added a reviewer: RKSimon.
Herald added a subscriber: hiraditya.
kongyi requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The folding rule `(select C, (gep Ptr, Idx), Ptr) -> (gep Ptr, (select C, Idx, 0))` creates a malformed `select` if `C` is a vector while `Idx` is scalar.

  SELECT VecC, ScalarIdx, 0

We could splat `Idx` to a vector but it defeats the purpose of optimisation. Don't apply the folding rule in this case.

This fixes a regression from commit d561b6fbdbe6d1da05fd92003a4ac1e37bf4b8bc <https://reviews.llvm.org/rGd561b6fbdbe6d1da05fd92003a4ac1e37bf4b8bc>.


https://reviews.llvm.org/D110225

Files:
  llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
  llvm/test/Transforms/InstCombine/select-gep.ll


Index: llvm/test/Transforms/InstCombine/select-gep.ll
===================================================================
--- llvm/test/Transforms/InstCombine/select-gep.ll
+++ llvm/test/Transforms/InstCombine/select-gep.ll
@@ -244,3 +244,16 @@
   ret i32* %sel
 }
 declare void @use_i32p(i32*)
+
+; We cannot create a select-with-idx with a vector condition but scalar idx.
+
+define <2 x i64*> @test7(<2 x i64*> %p1, i64 %idx, <2 x i1> %cc) {
+; CHECK-LABEL: @test7(
+; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i64, <2 x i64*> [[P1:%.*]], i64 [[IDX]]
+; CHECK-NEXT:    [[SELECT:%.*]] = select <2 x i1> [[CC:%.*]], <2 x i64*> [[P1:%.*]], <2 x i64*> [[GEP]]
+; CHECK-NEXT:    ret <2 x i64*> [[SELECT]]
+;
+  %gep = getelementptr i64, <2 x i64*> %p1, i64 %idx
+  %select = select <2 x i1> %cc, <2 x i64*> %p1, <2 x i64*> %gep
+  ret <2 x i64*> %select
+}
\ No newline at end of file
Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -3003,8 +3003,10 @@
     if (Gep->getNumOperands() != 2 || Gep->getPointerOperand() != Base ||
         !Gep->hasOneUse())
       return nullptr;
-    Type *ElementType = Gep->getResultElementType();
     Value *Idx = Gep->getOperand(1);
+    if (isa<VectorType>(CondVal->getType()) && !isa<VectorType>(Idx->getType()))
+      return nullptr;
+    Type *ElementType = Gep->getResultElementType();
     Value *NewT = Idx;
     Value *NewF = Constant::getNullValue(Idx->getType());
     if (Swap)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110225.374135.patch
Type: text/x-patch
Size: 1627 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210922/45811d6a/attachment.bin>


More information about the llvm-commits mailing list