Index: lib/CodeGen/CodeGenPrepare.cpp =================================================================== --- lib/CodeGen/CodeGenPrepare.cpp +++ lib/CodeGen/CodeGenPrepare.cpp @@ -291,9 +291,6 @@ /// Keep track of SExt promoted. ValueToSExts ValToSExtendedUses; - /// True if CFG is modified in any way. - bool ModifiedDT; - /// True if optimizing for size. bool OptSize; @@ -354,7 +351,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); @@ -373,7 +370,7 @@ bool AllowPromotionWithoutCommonHeader, bool HasPromoted, TypePromotionTransaction &TPT, SmallVectorImpl &SpeculativelyMovedExts); - bool splitBranchCondition(Function &F); + bool splitBranchCondition(Function &F, bool &ModifiedDT); bool simplifyOffsetableRelocate(Instruction &I); bool tryToSinkFreeOperands(Instruction *I); @@ -402,7 +399,6 @@ InsertedInsts.clear(); PromotedInsts.clear(); - ModifiedDT = false; if (auto *TPC = getAnalysisIfAvailable()) { TM = &TPC->getTM(); SubtargetInfo = TM->getSubtargetImpl(F); @@ -445,8 +441,9 @@ // unconditional branch. EverMadeChange |= eliminateMostlyEmptyBlocks(F); + bool ModifiedDT = false; if (!DisableBranchOpts) - EverMadeChange |= splitBranchCondition(F); + EverMadeChange |= splitBranchCondition(F, ModifiedDT); // Split some critical edges where one of the sources is an indirect branch, // to help generate sane code for PHIs involving such edges. @@ -2042,7 +2039,7 @@ // Duplicate the return into CallBB. (void)FoldReturnIntoUncondBranch(RetI, BB, CallBB); - ModifiedDT = Changed = true; + Changed = true; ++NumRetsDup; } @@ -5863,7 +5860,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; @@ -6957,7 +6954,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); @@ -7091,7 +7088,7 @@ /// /// FIXME: Remove the (equivalent?) implementation in SelectionDAG. /// -bool CodeGenPrepare::splitBranchCondition(Function &F) { +bool CodeGenPrepare::splitBranchCondition(Function &F, bool &ModifiedDT) { if (!TM || !TM->Options.EnableFastISel || !TLI || TLI->isJumpExpensive()) return false; @@ -7248,10 +7245,7 @@ } } - // Note: No point in getting fancy here, since the DT info is never - // available to CodeGenPrepare. ModifiedDT = true; - MadeChange = true; LLVM_DEBUG(dbgs() << "After branch condition splitting\n"; BB.dump(); 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,18 @@ +; 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 i1 @PR41004(i32 %x, i32 %y, i32 %t1) { +entry: + %rem = srem i32 %x, 2 + %t0 = icmp eq i32 %y, 1 + %mul = select i1 %t0, i32 %rem, i32 0 + %neg = add i32 %t1, -1 + %add = add i32 %neg, %mul + br label %if + +if: + %tobool = icmp eq i32 %t1, 0 + ret i1 %tobool +}