Index: llvm/trunk/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp =================================================================== --- llvm/trunk/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ llvm/trunk/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -302,10 +302,11 @@ formLCSSA(*OldContainingL, DT, &LI, nullptr); // We shouldn't need to form dedicated exits because the exit introduced - // here is the (just split by unswitching) preheader. As such, it is - // necessarily dedicated. - assert(OldContainingL->hasDedicatedExits() && - "Unexpected predecessor of hoisted loop preheader!"); + // here is the (just split by unswitching) preheader. However, after trivial + // unswitching it is possible to get new non-dedicated exits out of parent + // loop so let's conservatively form dedicated exit blocks and figure out + // if we can optimize later. + formDedicatedExitBlocks(OldContainingL, &DT, &LI, /*PreserveLCSSA*/ true); } } @@ -482,6 +483,7 @@ if (FullUnswitch) hoistLoopToNewParent(L, *NewPH, DT, LI); + LLVM_DEBUG(dbgs() << " done: unswitching trivial branch...\n"); ++NumTrivial; ++NumBranches; return true; @@ -539,7 +541,7 @@ else if (ExitCaseIndices.empty()) return false; - LLVM_DEBUG(dbgs() << " unswitching trivial cases...\n"); + LLVM_DEBUG(dbgs() << " unswitching trivial switch...\n"); // We may need to invalidate SCEVs for the outermost loop reached by any of // the exits. @@ -739,6 +741,7 @@ ++NumTrivial; ++NumSwitches; + LLVM_DEBUG(dbgs() << " done: unswitching trivial switch...\n"); return true; } Index: llvm/trunk/test/Transforms/SimpleLoopUnswitch/formDedicatedAfterTrivial1.ll =================================================================== --- llvm/trunk/test/Transforms/SimpleLoopUnswitch/formDedicatedAfterTrivial1.ll +++ llvm/trunk/test/Transforms/SimpleLoopUnswitch/formDedicatedAfterTrivial1.ll @@ -0,0 +1,25 @@ +; RUN: opt < %s -simple-loop-unswitch -disable-output + +; PR38283 +; PR38737 +define void @f1() { +for.cond1thread-pre-split.lr.ph.lr.ph: + %tobool4 = icmp eq i16 undef, 0 + br label %for.cond1thread-pre-split + +for.cond1thread-pre-split: ; preds = %if.end, %for.cond1thread-pre-split.lr.ph.lr.ph + %tobool3 = icmp eq i16 undef, 0 + br label %for.body2 + +for.body2: ; preds = %if.end6, %for.cond1thread-pre-split + br i1 %tobool3, label %if.end, label %for.end + +if.end: ; preds = %for.body2 + br i1 %tobool4, label %if.end6, label %for.cond1thread-pre-split + +if.end6: ; preds = %if.end + br i1 undef, label %for.body2, label %for.end + +for.end: ; preds = %if.end6, %for.body2 + ret void +} Index: llvm/trunk/test/Transforms/SimpleLoopUnswitch/formDedicatedAfterTrivial2.ll =================================================================== --- llvm/trunk/test/Transforms/SimpleLoopUnswitch/formDedicatedAfterTrivial2.ll +++ llvm/trunk/test/Transforms/SimpleLoopUnswitch/formDedicatedAfterTrivial2.ll @@ -0,0 +1,22 @@ +; RUN: opt < %s -simple-loop-unswitch -disable-output + +; PR38283 +; PR38737 +define void @Test(i32) { +entry: + %trunc = trunc i32 %0 to i3 + br label %outer +outer: + br label %inner +inner: + switch i3 %trunc, label %crit_edge [ + i3 2, label %break + i3 1, label %loopexit + ] +crit_edge: + br i1 true, label %loopexit, label %inner +loopexit: + ret void +break: + br label %outer +} Index: llvm/trunk/test/Transforms/SimpleLoopUnswitch/formDedicatedAfterTrivial3.ll =================================================================== --- llvm/trunk/test/Transforms/SimpleLoopUnswitch/formDedicatedAfterTrivial3.ll +++ llvm/trunk/test/Transforms/SimpleLoopUnswitch/formDedicatedAfterTrivial3.ll @@ -0,0 +1,37 @@ +; RUN: opt < %s -simple-loop-unswitch -disable-output + +; PR38283 +; PR38737 +declare void @func_1() + +define void @func_9(i32 signext %arg) { +bb: + br label %bb5 +bb5: ; preds = %bb24, %bb + %tmp3.0 = phi i32 [ undef, %bb ], [ %tmp29, %bb24 ] + %tmp11 = icmp eq i32 %arg, 0 + %tmp15 = icmp eq i32 %tmp3.0, 0 + %spec.select = select i1 %tmp15, i32 0, i32 49 + %tmp1.2 = select i1 %tmp11, i32 %spec.select, i32 9 + %trunc = trunc i32 %tmp1.2 to i6 + br label %bb9 + +bb9: ; preds = %bb5, %bb19 + %tmp2.03 = phi i32 [ 0, %bb5 ], [ %tmp21, %bb19 ] + switch i6 %trunc, label %bb24 [ + i6 0, label %bb19 + i6 -15, label %bb22 + ] + +bb19: ; preds = %bb9 + %tmp21 = add nuw nsw i32 %tmp2.03, 1 + %tmp8 = icmp eq i32 %tmp21, 25 + br i1 %tmp8, label %bb22, label %bb9 + +bb22: ; preds = %bb19, %bb9 + unreachable + +bb24: ; preds = %bb9 + %tmp29 = or i32 %tmp3.0, 1 + br label %bb5 +}