[llvm] 8f1350e - [IR] Check GEP source type when comparing instructions

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 11 03:32:12 PST 2022


Author: Nikita Popov
Date: 2022-02-11T12:32:04+01:00
New Revision: 8f1350e03aeaf1201ee13878dfa9b7a978262b3d

URL: https://github.com/llvm/llvm-project/commit/8f1350e03aeaf1201ee13878dfa9b7a978262b3d
DIFF: https://github.com/llvm/llvm-project/commit/8f1350e03aeaf1201ee13878dfa9b7a978262b3d.diff

LOG: [IR] Check GEP source type when comparing instructions

Two GEPs with same indices but different source type are not the
same.

Worth noting that FunctionComparator already handles this correctly.

Added: 
    llvm/test/Transforms/SimplifyCFG/opaque-ptr.ll

Modified: 
    llvm/lib/IR/Instruction.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index 36a20679863ba..9f1d5dabf14b5 100644
--- a/llvm/lib/IR/Instruction.cpp
+++ b/llvm/lib/IR/Instruction.cpp
@@ -492,6 +492,9 @@ static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2,
   if (const ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I1))
     return SVI->getShuffleMask() ==
            cast<ShuffleVectorInst>(I2)->getShuffleMask();
+  if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I1))
+    return GEP->getSourceElementType() ==
+           cast<GetElementPtrInst>(I2)->getSourceElementType();
 
   return true;
 }

diff  --git a/llvm/test/Transforms/SimplifyCFG/opaque-ptr.ll b/llvm/test/Transforms/SimplifyCFG/opaque-ptr.ll
new file mode 100644
index 0000000000000..909346f90d99a
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/opaque-ptr.ll
@@ -0,0 +1,46 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -simplifycfg -sink-common-insts -simplifycfg-require-and-preserve-domtree=1 -opaque-pointers -S < %s | FileCheck %s
+
+define ptr @test_sink_gep(i1 %c, ptr %p) {
+; CHECK-LABEL: @test_sink_gep(
+; CHECK-NEXT:  join:
+; CHECK-NEXT:    [[GEP2:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 1
+; CHECK-NEXT:    ret ptr [[GEP2]]
+;
+  br i1 %c, label %if, label %else
+
+if:
+  %gep1 = getelementptr i32, ptr %p, i64 1
+  br label %join
+
+else:
+  %gep2 = getelementptr i32, ptr %p, i64 1
+  br label %join
+
+join:
+  %phi = phi ptr [ %gep1, %if ], [ %gep2, %else]
+  ret ptr %phi
+}
+
+define ptr @test_sink_gep_
diff erent_types(i1 %c, ptr %p) {
+; CHECK-LABEL: @test_sink_gep_
diff erent_types(
+; CHECK-NEXT:  join:
+; CHECK-NEXT:    [[GEP1:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 1
+; CHECK-NEXT:    [[GEP2:%.*]] = getelementptr i64, ptr [[P]], i64 1
+; CHECK-NEXT:    [[PHI:%.*]] = select i1 [[C:%.*]], ptr [[GEP1]], ptr [[GEP2]]
+; CHECK-NEXT:    ret ptr [[PHI]]
+;
+  br i1 %c, label %if, label %else
+
+if:
+  %gep1 = getelementptr i32, ptr %p, i64 1
+  br label %join
+
+else:
+  %gep2 = getelementptr i64, ptr %p, i64 1
+  br label %join
+
+join:
+  %phi = phi ptr [ %gep1, %if ], [ %gep2, %else]
+  ret ptr %phi
+}


        


More information about the llvm-commits mailing list