diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/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; @@ -2742,6 +2745,8 @@ } LLVM_DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB); + Changed = true; + IRBuilder<> Builder(PBI); // If we need to invert the condition in the pred block to match, do so now. @@ -2913,9 +2918,9 @@ } } - return true; + return Changed; } - return false; + return Changed; } // If there is only one store in BB1 and BB2, return it, otherwise return diff --git a/llvm/test/Transforms/SimplifyCFG/fold-branch-debuginvariant.ll b/llvm/test/Transforms/SimplifyCFG/fold-branch-debuginvariant.ll --- a/llvm/test/Transforms/SimplifyCFG/fold-branch-debuginvariant.ll +++ b/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"