[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.
This commit is contained in:
Nikita Popov 2022-02-11 12:25:36 +01:00
parent 87c32be023
commit 8f1350e03a
2 changed files with 49 additions and 0 deletions

View File

@ -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;
}

View File

@ -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_different_types(i1 %c, ptr %p) {
; CHECK-LABEL: @test_sink_gep_different_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
}