Index: llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp =================================================================== --- llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -487,6 +487,9 @@ "Must have an `and` of `i1`s for the condition!"); buildPartialUnswitchConditionalBranch(*OldPH, Invariants, ExitDirection, *UnswitchedBB, *NewPH); + // If there is any prof branch_weights metadata then discard them because + // they are no longer valid for the split condition. + BI.setMetadata(LLVMContext::MD_prof, nullptr); } // Update the dominator tree with the added edge. @@ -2004,6 +2007,12 @@ // Keep track of the dominator tree updates needed. SmallVector DTUpdates; + // Before cloning for partial unswitch if there is any prof branch_weights + // metadata then discard them because they are no longer valid for the split + // condition. + if (!FullUnswitch) + BI->setMetadata(LLVMContext::MD_prof, nullptr); + // Clone the loop for each unswitched successor. SmallVector, 4> VMaps; VMaps.reserve(UnswitchedSuccBBs.size()); Index: llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll =================================================================== --- llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll +++ llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll @@ -2708,7 +2708,7 @@ loop_begin: %v1 = load i1, i1* %ptr %cond_or = or i1 %v1, %cond - br i1 %cond_or, label %loop_a, label %loop_b + br i1 %cond_or, label %loop_a, label %loop_b, !prof !{!"branch_weights", i32 1, i32 2} loop_a: call i32 @a() @@ -2729,7 +2729,7 @@ ; ; CHECK: latch.us: ; CHECK-NEXT: %[[V2_US:.*]] = load i1, i1* %ptr -; CHECK-NEXT: br i1 %[[V2_US]], label %loop_begin.us, label %loop_exit.split.us +; CHECK-NEXT: br i1 %[[V2_US]], label %loop_begin.us, label %loop_exit.split.us, !prof ![[MD0:[0-9]+]] ; ; CHECK: loop_exit.split.us: ; CHECK-NEXT: br label %loop_exit @@ -2745,7 +2745,7 @@ ; CHECK: loop_begin: ; CHECK-NEXT: %[[V1:.*]] = load i1, i1* %ptr ; CHECK-NEXT: %[[OR:.*]] = or i1 %[[V1]], false -; CHECK-NEXT: br i1 %[[OR]], label %loop_a, label %loop_b +; CHECK-NEXT: br i1 %[[OR]], label %loop_a, label %loop_b{{$}} ; ; CHECK: loop_a: ; CHECK-NEXT: call i32 @a() @@ -2757,10 +2757,10 @@ latch: %v2 = load i1, i1* %ptr - br i1 %v2, label %loop_begin, label %loop_exit + br i1 %v2, label %loop_begin, label %loop_exit, !prof !{!"branch_weights", i32 3, i32 4} ; CHECK: latch: ; CHECK-NEXT: %[[V2:.*]] = load i1, i1* %ptr -; CHECK-NEXT: br i1 %[[V2]], label %loop_begin, label %loop_exit.split +; CHECK-NEXT: br i1 %[[V2]], label %loop_begin, label %loop_exit.split, !prof ![[MD0:[0-9]+]] loop_exit: ret i32 0 @@ -4214,3 +4214,5 @@ ; CHECK: exit: ; CHECK-NEXT: ret void } + +; CHECK: ![[MD0]] = !{!"branch_weights", i32 3, i32 4} Index: llvm/test/Transforms/SimpleLoopUnswitch/trivial-unswitch.ll =================================================================== --- llvm/test/Transforms/SimpleLoopUnswitch/trivial-unswitch.ll +++ llvm/test/Transforms/SimpleLoopUnswitch/trivial-unswitch.ll @@ -450,16 +450,16 @@ entry: br label %loop_begin ; CHECK-NEXT: entry: -; CHECK-NEXT: br i1 %cond1, label %entry.split, label %loop_exit.split +; CHECK-NEXT: br i1 %cond1, label %entry.split, label %loop_exit.split, !prof ![[MD0:[0-9]+]] ; ; CHECK: entry.split: -; CHECK-NEXT: br i1 %cond2, label %entry.split.split, label %loop_exit +; CHECK-NEXT: br i1 %cond2, label %entry.split.split, label %loop_exit.split1{{$}} ; ; CHECK: entry.split.split: ; CHECK-NEXT: br label %loop_begin loop_begin: - br i1 %cond1, label %continue, label %loop_exit + br i1 %cond1, label %continue, label %loop_exit, !prof !{!"branch_weights", i32 1, i32 2} ; CHECK: loop_begin: ; CHECK-NEXT: br label %continue @@ -467,12 +467,12 @@ %var_val = load i32, i32* %var %var_cond = trunc i32 %var_val to i1 %cond_and = and i1 %var_cond, %cond2 - br i1 %cond_and, label %do_something, label %loop_exit + br i1 %cond_and, label %do_something, label %loop_exit, !prof !{!"branch_weights", i32 3, i32 4} ; CHECK: continue: ; CHECK-NEXT: %[[VAR:.*]] = load i32 ; CHECK-NEXT: %[[VAR_COND:.*]] = trunc i32 %[[VAR]] to i1 ; CHECK-NEXT: %[[COND_AND:.*]] = and i1 %[[VAR_COND]], true -; CHECK-NEXT: br i1 %[[COND_AND]], label %do_something, label %loop_exit +; CHECK-NEXT: br i1 %[[COND_AND]], label %do_something, label %loop_exit{{$}} do_something: call void @some_func() noreturn nounwind @@ -1243,3 +1243,5 @@ ; CHECK: loopexit: ; CHECK-NEXT: ret } + +; CHECK: ![[MD0]] = !{!"branch_weights", i32 1, i32 2}