From 481358974fb0f732e33d503c224492a543f4d7bd Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 15 Sep 2023 11:42:59 +0200 Subject: [PATCH] [GVN] Also remove phi nodes from VN table (PR65447) Followup to D158849: We also need to remove the phi node from the VN table, which is not handled by removeInstruction(). Fixes https://github.com/llvm/llvm-project/issues/65447. (cherry picked from commit 18e77760ce5e42d9057f69c3e64a8300d01a48ac) --- llvm/lib/Transforms/Scalar/GVN.cpp | 4 ++- llvm/test/Transforms/GVN/pr65447.ll | 71 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 llvm/test/Transforms/GVN/pr65447.ll diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp index b074c7c..4cfc0ba 100644 --- a/llvm/lib/Transforms/Scalar/GVN.cpp +++ b/llvm/lib/Transforms/Scalar/GVN.cpp @@ -2780,8 +2780,10 @@ bool GVNPass::processBlock(BasicBlock *BB) { // identical phis, and the second or later passes can eliminate them. SmallPtrSet PHINodesToRemove; ChangedFunction |= EliminateDuplicatePHINodes(BB, PHINodesToRemove); - for (PHINode *PN : PHINodesToRemove) + for (PHINode *PN : PHINodesToRemove) { + VN.erase(PN); removeInstruction(PN); + } for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { diff --git a/llvm/test/Transforms/GVN/pr65447.ll b/llvm/test/Transforms/GVN/pr65447.ll new file mode 100644 index 0000000..1b951e9 --- /dev/null +++ b/llvm/test/Transforms/GVN/pr65447.ll @@ -0,0 +1,71 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3 +; RUN: opt -S -passes=gvn < %s | FileCheck %s + +; Make sure deduplicated phi nodes are removed from the VN map. +define i64 @f() { +; CHECK-LABEL: define i64 @f() { +; CHECK-NEXT: BB: +; CHECK-NEXT: store i1 false, ptr null, align 1 +; CHECK-NEXT: br label [[BB2D:%.*]] +; CHECK: BB2a: +; CHECK-NEXT: br label [[BB2B:%.*]] +; CHECK: BB2b: +; CHECK-NEXT: [[L93_PRE_PRE:%.*]] = load i1, ptr inttoptr (i64 -1 to ptr), align 1 +; CHECK-NEXT: br label [[BB2C:%.*]] +; CHECK: BB2c: +; CHECK-NEXT: [[L93_PRE:%.*]] = phi i1 [ [[L93_PRE_PRE]], [[BB2B]] ], [ true, [[BB2D]] ] +; CHECK-NEXT: [[AZ2:%.*]] = phi i1 [ true, [[BB2B]] ], [ [[AZ:%.*]], [[BB2D]] ] +; CHECK-NEXT: [[DOTPHI_TRANS_INSERT:%.*]] = sext i1 [[AZ2]] to i64 +; CHECK-NEXT: [[GEP2_PHI_TRANS_INSERT:%.*]] = getelementptr i1, ptr null, i64 [[DOTPHI_TRANS_INSERT]] +; CHECK-NEXT: br label [[BB2D]] +; CHECK: BB2d: +; CHECK-NEXT: [[L93_PRE5:%.*]] = phi i1 [ [[L93_PRE]], [[BB2C]] ], [ false, [[BB:%.*]] ] +; CHECK-NEXT: [[AZ]] = phi i1 [ [[AZ2]], [[BB2C]] ], [ false, [[BB]] ] +; CHECK-NEXT: [[TMP0:%.*]] = sext i1 [[AZ]] to i64 +; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i1, ptr null, i64 [[TMP0]] +; CHECK-NEXT: store i1 [[AZ]], ptr null, align 2 +; CHECK-NEXT: br i1 [[L93_PRE5]], label [[BB2C]], label [[BB1E:%.*]] +; CHECK: BB1e: +; CHECK-NEXT: br i1 [[AZ]], label [[BB2F:%.*]], label [[BB4:%.*]] +; CHECK: BB2f: +; CHECK-NEXT: store i1 true, ptr null, align 2 +; CHECK-NEXT: br label [[BB2B]] +; CHECK: BB4: +; CHECK-NEXT: br label [[BB4]] +; +BB: + store i1 false, ptr null, align 1 + br label %BB2d + +BB2a: ; No predecessors! + br label %BB2b + +BB2b: ; preds = %BB2f, %BB2a + br label %BB2c + +BB2c: ; preds = %BB2d, %BB2b + %0 = phi i1 [ true, %BB2b ], [ %1, %BB2d ] + br label %BB2d + +BB2d: ; preds = %BB2c, %BB + %1 = phi i1 [ %0, %BB2c ], [ false, %BB ] + %2 = sext i1 %1 to i64 + %gep2 = getelementptr i1, ptr null, i64 %2 + %L93 = load i1, ptr %gep2, align 1 + %Az = load i1, ptr null, align 2 + store i1 %1, ptr null, align 2 + br i1 %L93, label %BB2c, label %BB1e + +BB1e: ; preds = %BB2d + br i1 %Az, label %BB2f, label %BB4 + +BB2f: ; preds = %BB1e + store i1 true, ptr null, align 2 + br label %BB2b + +BB4: ; preds = %BB1e, %BB4 + br label %BB4 + +; uselistorder directives + uselistorder label %BB4, { 1, 0 } +} -- 2.7.4