diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp --- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -2767,7 +2767,8 @@ if (CollectGuards) for (auto &I : *BB) if (isGuard(&I)) { - auto *Cond = cast(&I)->getArgOperand(0); + auto *Cond = + skipTrivialSelect(cast(&I)->getArgOperand(0)); // TODO: Support AND, OR conditions and partial unswitching. if (!isa(Cond) && L.isLoopInvariant(Cond)) UnswitchCandidates.push_back({&I, {Cond}}); 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 --- a/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch-skip-selects-in-guards.ll +++ b/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch-skip-selects-in-guards.ll @@ -1,3 +1,4 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt -passes='loop(simple-loop-unswitch),verify' -S < %s | FileCheck %s ; RUN: opt -passes='loop-mssa(simple-loop-unswitch),verify' -S < %s | FileCheck %s ; RUN: opt -simple-loop-unswitch -enable-nontrivial-unswitch -verify-memoryssa -S < %s | FileCheck %s @@ -7,9 +8,40 @@ declare void @widget() ; REQUIRES: asserts -; XFAIL: * define void @foo(ptr addrspace(1) %arg, i64 %arg1) personality ptr @pluto { +; CHECK-LABEL: @foo( +; CHECK-NEXT: bb: +; CHECK-NEXT: [[TMP:%.*]] = icmp slt i32 poison, 570 +; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP]], i1 true, i1 false +; CHECK-NEXT: br i1 [[TMP]], label [[BB_SPLIT_US:%.*]], label [[BB_SPLIT:%.*]] +; CHECK: bb.split.us: +; CHECK-NEXT: br label [[BB3_US:%.*]] +; CHECK: bb3.us: +; CHECK-NEXT: br label [[GUARDED_US:%.*]] +; CHECK: bb4.us: +; CHECK-NEXT: invoke void @widget() +; CHECK-NEXT: to label [[BB6_US:%.*]] unwind label [[BB7_SPLIT_US:%.*]] +; CHECK: bb6.us: +; CHECK-NEXT: invoke void @widget() +; CHECK-NEXT: to label [[BB3_US]] unwind label [[BB7_SPLIT_US]] +; CHECK: guarded.us: +; CHECK-NEXT: invoke void @widget() +; CHECK-NEXT: to label [[BB4_US:%.*]] unwind label [[BB7_SPLIT_US]] +; CHECK: bb7.split.us: +; CHECK-NEXT: [[TMP8_US:%.*]] = landingpad { ptr, i32 } +; CHECK-NEXT: cleanup +; CHECK-NEXT: br label [[BB7:%.*]] +; CHECK: bb.split: +; CHECK-NEXT: br label [[BB3:%.*]] +; CHECK: bb3: +; CHECK-NEXT: br label [[DEOPT:%.*]] +; CHECK: deopt: +; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 false, i32 7) [ "deopt"() ] +; CHECK-NEXT: unreachable +; CHECK: bb7: +; CHECK-NEXT: ret void +; bb: %tmp = icmp slt i32 poison, 570 %tmp2 = select i1 %tmp, i1 true, i1 false @@ -18,19 +50,19 @@ bb3: ; preds = %bb6, %bb call void (i1, ...) @llvm.experimental.guard(i1 %tmp2, i32 7) [ "deopt"() ] invoke void @widget() - to label %bb4 unwind label %bb7 + to label %bb4 unwind label %bb7 bb4: ; preds = %bb3 invoke void @widget() - to label %bb6 unwind label %bb7 + to label %bb6 unwind label %bb7 bb6: ; preds = %bb4 invoke void @widget() - to label %bb3 unwind label %bb7 + to label %bb3 unwind label %bb7 bb7: ; preds = %bb6, %bb4, %bb3 %tmp8 = landingpad { ptr, i32 } - cleanup + cleanup ret void }