Index: llvm/lib/Transforms/Scalar/JumpThreading.cpp =================================================================== --- llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -2728,6 +2728,9 @@ auto *BI = BranchInst::Create(NewBB, BB, SI->getCondition(), Pred); BI->applyMergedLocation(PredTerm->getDebugLoc(), SI->getDebugLoc()); BI->copyMetadata(*SI, {LLVMContext::MD_prof}); + // Also preserve the metadata from the branch + BI->copyMetadata(*PredTerm, {LLVMContext::MD_loop, + LLVMContext::MD_annotation}); SIUse->setIncomingValue(Idx, SI->getFalseValue()); SIUse->addIncoming(SI->getTrueValue(), NewBB); Index: llvm/test/Transforms/JumpThreading/preserve-select-unfold.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/JumpThreading/preserve-select-unfold.ll @@ -0,0 +1,30 @@ +; RUN: opt -passes=jump-threading -S < %s | FileCheck %s +; Ensure that we preserve llvm.loop information +target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024" +target triple = "spir64-unknown-unknown-intelfpga" + +define spir_kernel void @test(i1 %Input) { +entry: + br label %A + +; CHECK-LAB: select.unfold: +; CHECK: br label %A, !llvm.loop ![[INFO:.*]] + +; CHECK-LAB: A: +A: + %current_state.0.i.i = phi i32 [ 0, %entry ], [ %X, %M ] + %B = icmp eq i32 %current_state.0.i.i, 0 + br i1 %B, label %M, label %B2 + +B2: + ret void + +M: + %X = select i1 %Input, i32 2, i32 0 + br label %A, !llvm.loop !0 +} + +; CHECK: ![[INFO]] = distinct !{![[INFO]], ![[LINFO:.*]]} +!0 = distinct !{!0, !1} +; CHECK: ![[LINFO]] = !{!"llvm.loop.ii.count", i32 1} +!1 = !{!"llvm.loop.ii.count", i32 1}