From 35df6d09ce355e2e4db5d00bcc08c7b3776923d7 Mon Sep 17 00:00:00 2001 From: Yingwei Zheng Date: Sat, 14 Oct 2023 02:28:56 +0800 Subject: [PATCH 1/3] [SimplifyCFG] Add pre-commit tests for PR67342. NFC. --- llvm/test/Transforms/SimplifyCFG/pr67342.ll | 52 +++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 llvm/test/Transforms/SimplifyCFG/pr67342.ll diff --git a/llvm/test/Transforms/SimplifyCFG/pr67342.ll b/llvm/test/Transforms/SimplifyCFG/pr67342.ll new file mode 100644 index 0000000000000..e278c626acf17 --- /dev/null +++ b/llvm/test/Transforms/SimplifyCFG/pr67342.ll @@ -0,0 +1,52 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3 +; RUN: opt -S -passes=simplifycfg < %s | FileCheck %s + +; Tests from PR67342 +define i16 @test1(i32 %err) { +; CHECK-LABEL: define i16 @test1( +; CHECK-SAME: i32 [[ERR:%.*]]) { +; CHECK-NEXT: bb3: +; CHECK-NEXT: [[_3:%.*]] = icmp slt i32 [[ERR]], 0 +; CHECK-NEXT: [[OK:%.*]] = trunc i32 [[ERR]] to i16 +; CHECK-NEXT: [[R_SROA_3_0:%.*]] = select i1 [[_3]], i16 undef, i16 [[OK]] +; CHECK-NEXT: ret i16 [[R_SROA_3_0]] +; + %_3 = icmp slt i32 %err, 0 + br i1 %_3, label %bb1, label %bb2 + +bb2: + %ok = trunc i32 %err to i16 + br label %bb3 + +bb1: + br label %bb3 + +bb3: + %r.sroa.3.0 = phi i16 [ undef, %bb1 ], [ %ok, %bb2 ] + ret i16 %r.sroa.3.0 +} + +; commuted test +define i16 @test2(i32 %err) { +; CHECK-LABEL: define i16 @test2( +; CHECK-SAME: i32 [[ERR:%.*]]) { +; CHECK-NEXT: bb3: +; CHECK-NEXT: [[_3:%.*]] = icmp slt i32 [[ERR]], 0 +; CHECK-NEXT: [[OK:%.*]] = trunc i32 [[ERR]] to i16 +; CHECK-NEXT: [[R_SROA_3_0:%.*]] = select i1 [[_3]], i16 [[OK]], i16 undef +; CHECK-NEXT: ret i16 [[R_SROA_3_0]] +; + %_3 = icmp slt i32 %err, 0 + br i1 %_3, label %bb1, label %bb2 + +bb2: + br label %bb3 + +bb1: + %ok = trunc i32 %err to i16 + br label %bb3 + +bb3: + %r.sroa.3.0 = phi i16 [ %ok, %bb1 ], [ undef, %bb2 ] + ret i16 %r.sroa.3.0 +} From e3aa1dcc937e11b5f073224cbfbcbc0c677bd928 Mon Sep 17 00:00:00 2001 From: Yingwei Zheng Date: Sat, 14 Oct 2023 02:32:08 +0800 Subject: [PATCH 2/3] [SimplifyCFG] Improve FoldTwoEntryPHINode when one of phi values is undef --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 19 ++++++++++++++++--- llvm/test/Transforms/SimplifyCFG/pr67342.ll | 6 ++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 35fead111aa96..bab5363e63a85 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -3543,9 +3543,22 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, Value *TrueVal = PN->getIncomingValueForBlock(IfTrue); Value *FalseVal = PN->getIncomingValueForBlock(IfFalse); - Value *Sel = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", DomBI); - PN->replaceAllUsesWith(Sel); - Sel->takeName(PN); + Value *ReplaceVal = nullptr; + if (isa(TrueVal) && + isGuaranteedNotToBePoison(FalseVal, /*AC*/ nullptr, + IfFalse->getTerminator(), + DTU ? &DTU->getDomTree() : nullptr, 0)) { + ReplaceVal = FalseVal; + } else if (isa(FalseVal) && + isGuaranteedNotToBePoison( + TrueVal, /*AC*/ nullptr, IfFalse->getTerminator(), + DTU ? &DTU->getDomTree() : nullptr, 0)) { + ReplaceVal = TrueVal; + } else { + ReplaceVal = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", DomBI); + ReplaceVal->takeName(PN); + } + PN->replaceAllUsesWith(ReplaceVal); PN->eraseFromParent(); } diff --git a/llvm/test/Transforms/SimplifyCFG/pr67342.ll b/llvm/test/Transforms/SimplifyCFG/pr67342.ll index e278c626acf17..c2c9f460d8e5f 100644 --- a/llvm/test/Transforms/SimplifyCFG/pr67342.ll +++ b/llvm/test/Transforms/SimplifyCFG/pr67342.ll @@ -8,8 +8,7 @@ define i16 @test1(i32 %err) { ; CHECK-NEXT: bb3: ; CHECK-NEXT: [[_3:%.*]] = icmp slt i32 [[ERR]], 0 ; CHECK-NEXT: [[OK:%.*]] = trunc i32 [[ERR]] to i16 -; CHECK-NEXT: [[R_SROA_3_0:%.*]] = select i1 [[_3]], i16 undef, i16 [[OK]] -; CHECK-NEXT: ret i16 [[R_SROA_3_0]] +; CHECK-NEXT: ret i16 [[OK]] ; %_3 = icmp slt i32 %err, 0 br i1 %_3, label %bb1, label %bb2 @@ -33,8 +32,7 @@ define i16 @test2(i32 %err) { ; CHECK-NEXT: bb3: ; CHECK-NEXT: [[_3:%.*]] = icmp slt i32 [[ERR]], 0 ; CHECK-NEXT: [[OK:%.*]] = trunc i32 [[ERR]] to i16 -; CHECK-NEXT: [[R_SROA_3_0:%.*]] = select i1 [[_3]], i16 [[OK]], i16 undef -; CHECK-NEXT: ret i16 [[R_SROA_3_0]] +; CHECK-NEXT: ret i16 [[OK]] ; %_3 = icmp slt i32 %err, 0 br i1 %_3, label %bb1, label %bb2 From f64cfee5666472a67e8b3c733aa2e75f96273b46 Mon Sep 17 00:00:00 2001 From: Yingwei Zheng Date: Sat, 14 Oct 2023 19:05:12 +0800 Subject: [PATCH 3/3] fixup! [SimplifyCFG] Improve FoldTwoEntryPHINode when one of phi values is undef --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index bab5363e63a85..a8c4b1cc34c6c 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -3376,7 +3376,8 @@ static bool FoldCondBranchOnValueKnownInPredecessor(BranchInst *BI, /// Given a BB that starts with the specified two-entry PHI node, /// see if we can eliminate it. static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, - DomTreeUpdater *DTU, const DataLayout &DL) { + DomTreeUpdater *DTU, const DataLayout &DL, + AssumptionCache *AC) { // Ok, this is a two entry PHI node. Check to see if this is a simple "if // statement", which has a very simple dominance structure. Basically, we // are trying to find the condition that is being branched on, which @@ -3545,14 +3546,12 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, Value *ReplaceVal = nullptr; if (isa(TrueVal) && - isGuaranteedNotToBePoison(FalseVal, /*AC*/ nullptr, - IfFalse->getTerminator(), - DTU ? &DTU->getDomTree() : nullptr, 0)) { + isGuaranteedNotToBePoison(FalseVal, AC, IfFalse->getTerminator(), + /*DT*/ nullptr, /*Depth*/ 0)) { ReplaceVal = FalseVal; } else if (isa(FalseVal) && - isGuaranteedNotToBePoison( - TrueVal, /*AC*/ nullptr, IfFalse->getTerminator(), - DTU ? &DTU->getDomTree() : nullptr, 0)) { + isGuaranteedNotToBePoison(TrueVal, AC, IfTrue->getTerminator(), + /*DT*/ nullptr, /*Depth*/ 0)) { ReplaceVal = TrueVal; } else { ReplaceVal = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", DomBI); @@ -7419,7 +7418,7 @@ bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) { // eliminate it, do so now. if (auto *PN = dyn_cast(BB->begin())) if (PN->getNumIncomingValues() == 2) - if (FoldTwoEntryPHINode(PN, TTI, DTU, DL)) + if (FoldTwoEntryPHINode(PN, TTI, DTU, DL, Options.AC)) return true; }