Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -2615,6 +2615,8 @@ const unsigned PredCount = pred_size(BB); + bool Changed = false; + Instruction *Cond = nullptr; if (BI->isConditional()) Cond = dyn_cast(BI->getCondition()); @@ -2639,16 +2641,17 @@ // Quit if we can't remove this instruction. if (!tryCSEWithPredecessor(Curr, PB)) return false; + Changed = true; } } if (!Cond) - return false; + return Changed; } if (!Cond || (!isa(Cond) && !isa(Cond)) || Cond->getParent() != BB || !Cond->hasOneUse()) - return false; + return Changed; // Make sure the instruction after the condition is the cond branch. BasicBlock::iterator CondIt = ++Cond->getIterator(); @@ -2658,7 +2661,7 @@ ++CondIt; if (&*CondIt != BI) - return false; + return Changed; // Only allow this transformation if computing the condition doesn't involve // too many instructions and these involved instructions can be executed @@ -2672,11 +2675,11 @@ if (isa(I)) continue; if (!I->hasOneUse() || !isSafeToSpeculativelyExecute(&*I)) - return false; + return Changed; // I has only one use and can be executed unconditionally. Instruction *User = dyn_cast(I->user_back()); if (User == nullptr || User->getParent() != BB) - return false; + return Changed; // I is used in the same BB. Since BI uses Cond and doesn't have more slots // to use any other instruction, User must be an instruction between next(I) // and Cond. @@ -2686,23 +2689,23 @@ NumBonusInsts += PredCount; // Early exits once we reach the limit. if (NumBonusInsts > BonusInstThreshold) - return false; + return Changed; } // Cond is known to be a compare or binary operator. Check to make sure that // neither operand is a potentially-trapping constant expression. if (ConstantExpr *CE = dyn_cast(Cond->getOperand(0))) if (CE->canTrap()) - return false; + return Changed; if (ConstantExpr *CE = dyn_cast(Cond->getOperand(1))) if (CE->canTrap()) - return false; + return Changed; // Finally, don't infinitely unroll conditional loops. BasicBlock *TrueDest = BI->getSuccessor(0); BasicBlock *FalseDest = (BI->isConditional()) ? BI->getSuccessor(1) : nullptr; if (TrueDest == BB || FalseDest == BB) - return false; + return Changed; for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { BasicBlock *PredBlock = *PI; @@ -2759,6 +2762,7 @@ PBI->setCondition(NewCond); PBI->swapSuccessors(); } + Changed = true; // If we have bonus instructions, clone them into the predecessor block. // Note that there may be multiple predecessor blocks, so we cannot move @@ -2913,9 +2917,9 @@ } } - return true; + return Changed; } - return false; + return Changed; } // If there is only one store in BB1 and BB2, return it, otherwise return @@ -5948,8 +5952,10 @@ // branches to us and our successor, fold the comparison into the // predecessor and use logical operations to update the incoming value // for PHI nodes in common successor. - if (FoldBranchToCommonDest(BI, nullptr, Options.BonusInstThreshold)) - return requestResimplify(); + if (FoldBranchToCommonDest(BI, nullptr, Options.BonusInstThreshold)) { + requestResimplify(); + return true; + } return false; } Index: llvm/test/Transforms/SimplifyCFG/fold-branch-debuginvariant.ll =================================================================== --- llvm/test/Transforms/SimplifyCFG/fold-branch-debuginvariant.ll +++ llvm/test/Transforms/SimplifyCFG/fold-branch-debuginvariant.ll @@ -6,7 +6,6 @@ ; CHECK-LABEL: bb1: ; CHECK: and i1 false, false -; CHECK-LABEL: bb2: ; CHECK-NOT: and i1 false, false target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"