From ab6876a40d06e7a30a2ab7c7dcb3440672e201ea Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 31 Aug 2022 13:08:20 -0700 Subject: [PATCH] reland: [Local] Allow creating callbr with duplicate successors Since D129288, callbr is allowed to have duplicate successors. This patch removes a limitation which prevents optimizations from actually producing such callbrs. This is probably the riskiest of all the recent callbr changes, because code with incorrect assumptions might be lurking somewhere. I fixed the one case I encountered ahead of time in https://github.com/llvm/llvm-project/commit/8201e3ef5c84561260218bc041209611aac690e3. Reviewed By: nickdesaulniers Differential Revision: https://reviews.llvm.org/D129997 Originally landed as commit 08860f525a23 ("[Local] Allow creating callbr with duplicate successors") Reverted in commit 1cf6b93df168 ("Revert "[Local] Allow creating callbr with duplicate successors"") --- llvm/lib/Transforms/Utils/Local.cpp | 12 ------------ llvm/test/CodeGen/AArch64/callbr-asm-obj-file.ll | 3 +-- llvm/test/Transforms/JumpThreading/callbr-edge-split.ll | 4 +--- llvm/test/Transforms/JumpThreading/pr46857-callbr.ll | 6 +----- llvm/test/Transforms/SimplifyCFG/callbr-destinations.ll | 12 ++---------- llvm/test/Transforms/SimplifyCFG/jump-threading.ll | 4 +--- 6 files changed, 6 insertions(+), 35 deletions(-) diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index c1735f7..1115b0b 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -1090,18 +1090,6 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB, } } - // We cannot fold the block if it's a branch to an already present callbr - // successor because that creates duplicate successors. - for (BasicBlock *PredBB : predecessors(BB)) { - if (auto *CBI = dyn_cast(PredBB->getTerminator())) { - if (Succ == CBI->getDefaultDest()) - return false; - for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i) - if (Succ == CBI->getIndirectDest(i)) - return false; - } - } - LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB); SmallVector Updates; diff --git a/llvm/test/CodeGen/AArch64/callbr-asm-obj-file.ll b/llvm/test/CodeGen/AArch64/callbr-asm-obj-file.ll index 06d9568..803b15d 100644 --- a/llvm/test/CodeGen/AArch64/callbr-asm-obj-file.ll +++ b/llvm/test/CodeGen/AArch64/callbr-asm-obj-file.ll @@ -72,9 +72,8 @@ define hidden i32 @test2() local_unnamed_addr { ; CHECK-LABEL: : ; CHECK-LABEL: <$d.9>: ; CHECK-LABEL: <$x.10>: -; CHECK-NEXT: b {{.*}} <$x.12+0x4> +; CHECK-NEXT: b {{.*}} <$x.12> ; CHECK-LABEL: <$x.12>: -; CHECK-NEXT: mov w0, wzr ; CHECK-NEXT: ldr x30, [sp], #16 ; CHECK-NEXT: ret define internal i1 @test3() { diff --git a/llvm/test/Transforms/JumpThreading/callbr-edge-split.ll b/llvm/test/Transforms/JumpThreading/callbr-edge-split.ll index 7d1cb4c..d96f5cf 100644 --- a/llvm/test/Transforms/JumpThreading/callbr-edge-split.ll +++ b/llvm/test/Transforms/JumpThreading/callbr-edge-split.ll @@ -13,9 +13,7 @@ define i32 @c() { ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_ELSE:%.*]], label [[IF_END:%.*]] ; CHECK: if.else: ; CHECK-NEXT: callbr void asm sideeffect "", "!i"() -; CHECK-NEXT: to label [[NORMAL:%.*]] [label %if.then2] -; CHECK: normal: -; CHECK-NEXT: br label [[IF_THEN2:%.*]] +; CHECK-NEXT: to label [[IF_THEN2:%.*]] [label %if.then2] ; CHECK: if.end: ; CHECK-NEXT: [[CALL:%.*]] = call i32 @b() ; CHECK-NEXT: [[PHITMP:%.*]] = icmp ne i32 [[CALL]], 0 diff --git a/llvm/test/Transforms/JumpThreading/pr46857-callbr.ll b/llvm/test/Transforms/JumpThreading/pr46857-callbr.ll index a5ccde0..f1d0629 100644 --- a/llvm/test/Transforms/JumpThreading/pr46857-callbr.ll +++ b/llvm/test/Transforms/JumpThreading/pr46857-callbr.ll @@ -1,17 +1,13 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -jump-threading -S | FileCheck %s -; CHECK-ALL-LABEL: @func( - define i1 @func(i1 %arg, i32 %arg1, i1 %arg2) { ; CHECK-LABEL: @func( ; CHECK-NEXT: bb: ; CHECK-NEXT: br i1 [[ARG:%.*]], label [[BB7:%.*]], label [[BB4:%.*]] ; CHECK: bb4: ; CHECK-NEXT: callbr void asm sideeffect "", "!i"() -; CHECK-NEXT: to label [[BB5:%.*]] [label %bb7.thr_comm] -; CHECK: bb5: -; CHECK-NEXT: br label [[BB7_THR_COMM:%.*]] +; CHECK-NEXT: to label [[BB7_THR_COMM:%.*]] [label %bb7.thr_comm] ; CHECK: bb7.thr_comm: ; CHECK-NEXT: [[I91:%.*]] = xor i1 [[ARG2:%.*]], [[ARG]] ; CHECK-NEXT: br i1 [[I91]], label [[BB11:%.*]], label [[BB11]] diff --git a/llvm/test/Transforms/SimplifyCFG/callbr-destinations.ll b/llvm/test/Transforms/SimplifyCFG/callbr-destinations.ll index 368bc5e..008aa5c 100644 --- a/llvm/test/Transforms/SimplifyCFG/callbr-destinations.ll +++ b/llvm/test/Transforms/SimplifyCFG/callbr-destinations.ll @@ -17,16 +17,13 @@ bb: ret void } -; TODO: Can fold to a duplicate callbr destination. define void @callbr_can_fold_to_duplicate_dest1() { ; CHECK-LABEL: @callbr_can_fold_to_duplicate_dest1( ; CHECK-NEXT: entry: ; CHECK-NEXT: callbr void asm sideeffect "", "!i"() -; CHECK-NEXT: to label [[BB2:%.*]] [label %common.ret] +; CHECK-NEXT: to label [[COMMON_RET:%.*]] [label %common.ret] ; CHECK: common.ret: ; CHECK-NEXT: ret void -; CHECK: bb2: -; CHECK-NEXT: br label [[COMMON_RET:%.*]] ; entry: callbr void asm sideeffect "", "!i"() @@ -39,18 +36,13 @@ bb2: ret void } -; TODO: Can fold to a duplicate callbr destination. define void @callbr_can_fold_to_duplicate_dest2() { ; CHECK-LABEL: @callbr_can_fold_to_duplicate_dest2( ; CHECK-NEXT: entry: ; CHECK-NEXT: callbr void asm sideeffect "", "!i,!i"() -; CHECK-NEXT: to label [[COMMON_RET:%.*]] [label [[BB2:%.*]], label %bb3] +; CHECK-NEXT: to label [[COMMON_RET:%.*]] [label [[COMMON_RET]], label %common.ret] ; CHECK: common.ret: ; CHECK-NEXT: ret void -; CHECK: bb2: -; CHECK-NEXT: br label [[COMMON_RET]] -; CHECK: bb3: -; CHECK-NEXT: br label [[COMMON_RET]] ; entry: callbr void asm sideeffect "", "!i,!i"() diff --git a/llvm/test/Transforms/SimplifyCFG/jump-threading.ll b/llvm/test/Transforms/SimplifyCFG/jump-threading.ll index e7d7662..96befcc 100644 --- a/llvm/test/Transforms/SimplifyCFG/jump-threading.ll +++ b/llvm/test/Transforms/SimplifyCFG/jump-threading.ll @@ -425,9 +425,7 @@ define void @callbr() { ; CHECK-LABEL: @callbr( ; CHECK-NEXT: entry: ; CHECK-NEXT: callbr void asm sideeffect "", "!i,~{dirflag},~{fpsr},~{flags}"() -; CHECK-NEXT: to label [[IF_END:%.*]] [label %target] -; CHECK: target: -; CHECK-NEXT: br label [[IF_END]] +; CHECK-NEXT: to label [[IF_END:%.*]] [label %if.end] ; CHECK: if.end: ; CHECK-NEXT: ret void ; -- 2.7.4