[InstCombine] PR58901 - fix bug with swapping GEP of different types
authorWilliam Huang <williamjhuang@google.com>
Thu, 10 Nov 2022 00:34:07 +0000 (00:34 +0000)
committerWilliam Huang <williamjhuang@google.com>
Thu, 10 Nov 2022 20:24:41 +0000 (20:24 +0000)
Fix https://github.com/llvm/llvm-project/issues/58901 by adding stricter check whether non-opaque GEP can be swapped. This will not affect GEP swapping optimization in the future since we are switching to opaque GEP

Reviewed By: clin1

Differential Revision: https://reviews.llvm.org/D137752

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/pr58901.ll [new file with mode: 0644]

index ba51ee5..61bac17 100644 (file)
@@ -2009,6 +2009,7 @@ Instruction *InstCombinerImpl::visitGEPOfGEP(GetElementPtrInst &GEP,
   // optimizations below.
   if (ShouldCanonicalizeSwap && Src->hasOneUse() &&
       Src->getPointerOperandType() == GEP.getPointerOperandType() &&
+      Src->getPointerOperandType() == GEP.getType() &&
       Src->getType()->isVectorTy() == GEP.getType()->isVectorTy() &&
       !isa<GlobalValue>(Src->getPointerOperand())) {
     // When swapping, GEP with all constant indices are more prioritized than
diff --git a/llvm/test/Transforms/InstCombine/pr58901.ll b/llvm/test/Transforms/InstCombine/pr58901.ll
new file mode 100644 (file)
index 0000000..cab7201
--- /dev/null
@@ -0,0 +1,23 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i32* @f1([6 x i32]* %arg, i64 %arg1) {
+; CHECK-LABEL: @f1(
+; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr [6 x i32], [6 x i32]* [[ARG:%.*]], i64 3, i64 [[ARG1:%.*]]
+; CHECK-NEXT:    ret i32* [[TMP1]]
+;
+  %1 = getelementptr [6 x i32], [6 x i32]* %arg, i64 3
+  %2 = getelementptr [6 x i32], [6 x i32]* %1, i64 0, i64 %arg1
+  ret i32* %2
+}
+
+define i32* @f2([6 x i32]* %arg, i64 %arg1) {
+; CHECK-LABEL: @f2(
+; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr [6 x i32], [6 x i32]* [[ARG:%.*]], i64 3
+; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr [6 x i32], [6 x i32]* [[TMP1]], i64 [[ARG1:%.*]], i64 [[ARG1]]
+; CHECK-NEXT:    ret i32* [[TMP2]]
+;
+  %1 = getelementptr [6 x i32], [6 x i32]* %arg, i64 3
+  %2 = getelementptr [6 x i32], [6 x i32]* %1, i64 %arg1, i64 %arg1
+  ret i32* %2
+}