diff --git a/llvm/lib/Transforms/Utils/LoopSimplify.cpp b/llvm/lib/Transforms/Utils/LoopSimplify.cpp --- a/llvm/lib/Transforms/Utils/LoopSimplify.cpp +++ b/llvm/lib/Transforms/Utils/LoopSimplify.cpp @@ -535,6 +535,18 @@ Changed = true; } + BasicBlock *TrueBB, *FalseBB; + using namespace PatternMatch; + if (match(BI, m_Br(m_Value(), TrueBB, FalseBB)) && + !L->contains(TrueBB) && L->contains(FalseBB)) { + if (isa(BI->getCondition())) { + if (dyn_cast(BI->getCondition())->swapOperands()) + BI->swapSuccessors(); + } else if (isa(BI->getCondition())) { + dyn_cast(BI->getCondition())->swapOperands(); + BI->swapSuccessors(); + } + } } // Does the loop already have a preheader? If so, don't insert one.