Index: lib/CodeGen/CodeGenPrepare.cpp =================================================================== --- lib/CodeGen/CodeGenPrepare.cpp +++ lib/CodeGen/CodeGenPrepare.cpp @@ -292,7 +292,7 @@ ValueToSExts ValToSExtendedUses; /// True if CFG is modified in any way. - bool ModifiedDT; + bool DTModified; /// True if optimizing for size. bool OptSize; @@ -354,7 +354,7 @@ bool optimizeExt(Instruction *&I); bool optimizeExtUses(Instruction *I); bool optimizeLoadExt(LoadInst *Load); - bool optimizeSelectInst(SelectInst *SI); + bool optimizeSelectInst(SelectInst *SI, bool &ModifiedDT); bool optimizeShuffleVectorInst(ShuffleVectorInst *SVI); bool optimizeSwitchInst(SwitchInst *SI); bool optimizeExtractElementInst(Instruction *Inst); @@ -402,7 +402,7 @@ InsertedInsts.clear(); PromotedInsts.clear(); - ModifiedDT = false; + DTModified = false; if (auto *TPC = getAnalysisIfAvailable()) { TM = &TPC->getTM(); SubtargetInfo = TM->getSubtargetImpl(F); @@ -2042,7 +2042,7 @@ // Duplicate the return into CallBB. (void)FoldReturnIntoUncondBranch(RetI, BB, CallBB); - ModifiedDT = Changed = true; + DTModified = Changed = true; ++NumRetsDup; } @@ -5863,7 +5863,7 @@ /// If we have a SelectInst that will likely profit from branch prediction, /// turn it into a branch. -bool CodeGenPrepare::optimizeSelectInst(SelectInst *SI) { +bool CodeGenPrepare::optimizeSelectInst(SelectInst *SI, bool &ModifiedDT) { // If branch conversion isn't desirable, exit early. if (DisableSelectToBranch || OptSize || !TLI) return false; @@ -5905,6 +5905,7 @@ return false; ModifiedDT = true; + DTModified = true; // Transform a sequence like this: // start: @@ -6957,7 +6958,7 @@ return optimizeCallInst(CI, ModifiedDT); if (SelectInst *SI = dyn_cast(I)) - return optimizeSelectInst(SI); + return optimizeSelectInst(SI, ModifiedDT); if (ShuffleVectorInst *SVI = dyn_cast(I)) return optimizeShuffleVectorInst(SVI); @@ -7250,7 +7251,7 @@ // Note: No point in getting fancy here, since the DT info is never // available to CodeGenPrepare. - ModifiedDT = true; + DTModified = true; MadeChange = true; Index: test/CodeGen/X86/codegen-prepare-DT.ll =================================================================== --- test/CodeGen/X86/codegen-prepare-DT.ll +++ test/CodeGen/X86/codegen-prepare-DT.ll @@ -0,0 +1,51 @@ +; RUN: llc -o - -verify-machineinstrs %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define void @foo(i64 %i0, i64 %i1, i1 %i2, i64 %i3) !prof !1 { +entry: + switch i64 %i0, label %def1 [ + i64 -8, label %sw1 + i64 -16, label %sw1 + i64 0, label %sw1 + ] + +def1: + unreachable + +sw1: + switch i64 %i1, label %def2 [ + i64 -8, label %sw2 + i64 -16, label %sw2 + i64 0, label %sw2 + ] + +def2: + unreachable + +sw2: + %v1 = select i1 %i2 , i64 %i3, i64 100, !prof !3 + %v2 = add nsw i64 %v1, -1 + %0 = icmp ult i64 %v2, 9223372036854775804 + br label %if1 + +if1: + br i1 %0, label %then1, label %else1 + +then1: + unreachable + +else1: + %cmp = icmp eq i64 %v1, 0 + %spec = select i1 %cmp, i64 0, i64 undef + ret void +} + +attributes #0 = { "use-soft-float"="false" } + +!1 = !{!"function_entry_count", i64 10562721} +!2 = !{!"branch_weights", i32 10562721, i32 0, i32 0, i32 0} +!3 = !{!"branch_weights", i32 15714, i32 4352144} +!4 = !{!"branch_weights", i32 0, i32 24913} +!5 = !{!"branch_weights", i32 0, i32 19483}