[llvm] e9c0720 - [PHITransAddr] Check GEP source element type
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 11 07:22:57 PST 2022
Author: Nikita Popov
Date: 2022-02-11T16:22:48+01:00
New Revision: e9c0720010c07c75d63605610934e165b8aa4afd
URL: https://github.com/llvm/llvm-project/commit/e9c0720010c07c75d63605610934e165b8aa4afd
DIFF: https://github.com/llvm/llvm-project/commit/e9c0720010c07c75d63605610934e165b8aa4afd.diff
LOG: [PHITransAddr] Check GEP source element type
It's not the same GEP if the source element type is different.
Added:
Modified:
llvm/lib/Analysis/PHITransAddr.cpp
llvm/test/Transforms/GVN/opaque-ptr.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/PHITransAddr.cpp b/llvm/lib/Analysis/PHITransAddr.cpp
index 02d084937ccbd..d2816d8dedd2f 100644
--- a/llvm/lib/Analysis/PHITransAddr.cpp
+++ b/llvm/lib/Analysis/PHITransAddr.cpp
@@ -240,6 +240,7 @@ Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB,
for (User *U : APHIOp->users()) {
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U))
if (GEPI->getType() == GEP->getType() &&
+ GEPI->getSourceElementType() == GEP->getSourceElementType() &&
GEPI->getNumOperands() == GEPOps.size() &&
GEPI->getParent()->getParent() == CurBB->getParent() &&
(!DT || DT->dominates(GEPI->getParent(), PredBB))) {
diff --git a/llvm/test/Transforms/GVN/opaque-ptr.ll b/llvm/test/Transforms/GVN/opaque-ptr.ll
index f382b799b6fb2..617290d333272 100644
--- a/llvm/test/Transforms/GVN/opaque-ptr.ll
+++ b/llvm/test/Transforms/GVN/opaque-ptr.ll
@@ -2,9 +2,10 @@
; RUN: opt -S -gvn -opaque-pointers < %s | FileCheck %s
declare void @use(ptr)
+declare void @use.i32(i32)
-define void @test(ptr %p) {
-; CHECK-LABEL: @test(
+define void @gep_cse(ptr %p) {
+; CHECK-LABEL: @gep_cse(
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 1
; CHECK-NEXT: [[GEP3:%.*]] = getelementptr i64, ptr [[P]], i64 1
; CHECK-NEXT: call void @use(ptr [[GEP1]])
@@ -20,3 +21,85 @@ define void @test(ptr %p) {
call void @use(ptr %gep3)
ret void
}
+
+define i32 @phi_trans(i1 %c, ptr %p1, ptr %p2) {
+; CHECK-LABEL: @phi_trans(
+; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
+; CHECK: if:
+; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i32, ptr [[P1:%.*]], i64 1
+; CHECK-NEXT: [[V1:%.*]] = load i32, ptr [[GEP1]], align 4
+; CHECK-NEXT: call void @use(i32 [[V1]]) #[[ATTR0:[0-9]+]]
+; CHECK-NEXT: br label [[JOIN:%.*]]
+; CHECK: else:
+; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i32, ptr [[P2:%.*]], i64 1
+; CHECK-NEXT: [[V2:%.*]] = load i32, ptr [[GEP2]], align 4
+; CHECK-NEXT: call void @use(i32 [[V2]]) #[[ATTR0]]
+; CHECK-NEXT: br label [[JOIN]]
+; CHECK: join:
+; CHECK-NEXT: [[V:%.*]] = phi i32 [ [[V1]], [[IF]] ], [ [[V2]], [[ELSE]] ]
+; CHECK-NEXT: [[PHI:%.*]] = phi ptr [ [[P1]], [[IF]] ], [ [[P2]], [[ELSE]] ]
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr i32, ptr [[PHI]], i64 1
+; CHECK-NEXT: ret i32 [[V]]
+;
+ br i1 %c, label %if, label %else
+
+if:
+ %gep1 = getelementptr i32, ptr %p1, i64 1
+ %v1 = load i32, ptr %gep1
+ call void @use(i32 %v1) readnone
+ br label %join
+
+else:
+ %gep2 = getelementptr i32, ptr %p2, i64 1
+ %v2 = load i32, ptr %gep2
+ call void @use(i32 %v2) readnone
+ br label %join
+
+join:
+ %phi = phi ptr [ %p1, %if ], [ %p2, %else ]
+ %gep = getelementptr i32, ptr %phi, i64 1
+ %v = load i32, ptr %gep
+ ret i32 %v
+}
+
+define i32 @phi_trans_
diff erent_types(i1 %c, ptr %p1, ptr %p2) {
+; CHECK-LABEL: @phi_trans_
diff erent_types(
+; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
+; CHECK: if:
+; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i32, ptr [[P1:%.*]], i64 1
+; CHECK-NEXT: [[V1:%.*]] = load i32, ptr [[GEP1]], align 4
+; CHECK-NEXT: call void @use.i32(i32 [[V1]]) #[[ATTR0]]
+; CHECK-NEXT: br label [[JOIN:%.*]]
+; CHECK: else:
+; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i64, ptr [[P2:%.*]], i64 1
+; CHECK-NEXT: [[V2:%.*]] = load i32, ptr [[GEP2]], align 4
+; CHECK-NEXT: call void @use.i32(i32 [[V2]]) #[[ATTR0]]
+; CHECK-NEXT: [[GEP_PHI_TRANS_INSERT:%.*]] = getelementptr i32, ptr [[P2]], i64 1
+; CHECK-NEXT: [[V_PRE:%.*]] = load i32, ptr [[GEP_PHI_TRANS_INSERT]], align 4
+; CHECK-NEXT: br label [[JOIN]]
+; CHECK: join:
+; CHECK-NEXT: [[V:%.*]] = phi i32 [ [[V1]], [[IF]] ], [ [[V_PRE]], [[ELSE]] ]
+; CHECK-NEXT: [[PHI:%.*]] = phi ptr [ [[P1]], [[IF]] ], [ [[P2]], [[ELSE]] ]
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr i32, ptr [[PHI]], i64 1
+; CHECK-NEXT: ret i32 [[V]]
+;
+ br i1 %c, label %if, label %else
+
+if:
+ %gep1 = getelementptr i32, ptr %p1, i64 1
+ %v1 = load i32, ptr %gep1
+ call void @use.i32(i32 %v1) readnone
+ br label %join
+
+else:
+ %gep2 = getelementptr i64, ptr %p2, i64 1
+ %v2 = load i32, ptr %gep2
+ call void @use.i32(i32 %v2) readnone
+ br label %join
+
+join:
+ %phi = phi ptr [ %p1, %if ], [ %p2, %else ]
+ %gep = getelementptr i32, ptr %phi, i64 1
+ %v = load i32, ptr %gep
+ ret i32 %v
+}
More information about the llvm-commits
mailing list