[PATCH] D137752: [InstCombine] PR58901 - fix bug with swapping GEP of different types

William Junda Huang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 9 16:39:16 PST 2022


huangjd created this revision.
huangjd added reviewers: nikic, spatel, reames.
Herald added a subscriber: hiraditya.
Herald added a project: All.
huangjd requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137752

Files:
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
  llvm/test/Transforms/InstCombine/pr58901.ll


Index: llvm/test/Transforms/InstCombine/pr58901.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/pr58901.ll
@@ -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
+}
Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2009,6 +2009,7 @@
   // 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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137752.474390.patch
Type: text/x-patch
Size: 1817 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221110/048fb6a4/attachment.bin>


More information about the llvm-commits mailing list