From d46d9689f7c3fe16010cd07761858a6392239b3f Mon Sep 17 00:00:00 2001 From: Dmitry Makogon Date: Thu, 8 Jun 2023 18:56:18 +0700 Subject: [PATCH] [BBUtils] Don't add 'then' block to a loop if it's terminated with unreachable SplitBlockAndInsertIfThen utility creates two new blocks, they're called ThenBlock and Tail (true and false destinations of a conditional branch correspondingly). The function has a bool parameter Unreachable, and if it's set, then ThenBlock is terminated with an unreachable. At the end of the function the new blocks are added to the loop of the split block. However, in case ThenBlock is terminated with an unreachable, it cannot belong to any loop. Differential Revision: https://reviews.llvm.org/D152434 --- llvm/lib/Transforms/Utils/BasicBlockUtils.cpp | 4 +++- llvm/test/Transforms/SimpleLoopUnswitch/guards.ll | 3 --- .../SimpleLoopUnswitch/nontrivial-unswitch-skip-selects-in-guards.ll | 3 --- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp index 4b93b62..68ff16f 100644 --- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -1518,7 +1518,9 @@ Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond, if (LI) { if (Loop *L = LI->getLoopFor(Head)) { - L->addBasicBlockToLoop(ThenBlock, *LI); + // unreachable-terminated blocks cannot belong to any loop. + if (!Unreachable) + L->addBasicBlockToLoop(ThenBlock, *LI); L->addBasicBlockToLoop(Tail, *LI); } } diff --git a/llvm/test/Transforms/SimpleLoopUnswitch/guards.ll b/llvm/test/Transforms/SimpleLoopUnswitch/guards.ll index 5258038..0766861 100644 --- a/llvm/test/Transforms/SimpleLoopUnswitch/guards.ll +++ b/llvm/test/Transforms/SimpleLoopUnswitch/guards.ll @@ -2,9 +2,6 @@ ; RUN: opt -passes='simple-loop-unswitch' -simple-loop-unswitch-guards -S < %s | FileCheck %s ; RUN: opt -passes='loop-mssa(simple-loop-unswitch),verify' -simple-loop-unswitch-guards -verify-memoryssa -verify-loop-info -S < %s | FileCheck %s -; XFAIL: * -; REQUIRES: asserts - declare void @llvm.experimental.guard(i1, ...) define void @test_simple_case(i1 %cond, i32 %N) { diff --git a/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch-skip-selects-in-guards.ll b/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch-skip-selects-in-guards.ll index e7bf39d..543ee09 100644 --- a/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch-skip-selects-in-guards.ll +++ b/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch-skip-selects-in-guards.ll @@ -7,9 +7,6 @@ declare ptr @pluto() declare void @llvm.experimental.guard(i1, ...) declare void @widget() -; XFAIL: * -; REQUIRES: asserts - define void @foo(ptr addrspace(1) %arg, i64 %arg1) personality ptr @pluto { ; CHECK-LABEL: @foo( ; CHECK-NEXT: bb: -- 2.7.4