Index: llvm/lib/Transforms/Scalar/LoopFuse.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LoopFuse.cpp +++ llvm/lib/Transforms/Scalar/LoopFuse.cpp @@ -415,9 +415,29 @@ return true; } - // If LHS does not dominate RHS and RHS does not dominate LHS then there is - // no dominance relationship between the two FusionCandidates. Thus, they - // should not be in the same set together. + // If two FusionCandidates are in the same level of dominator tree, + // they will not dominate each other, but may still be control flow + // equivalent. To sort those FusionCandidates, nonStrictlyPostDominate() + // function is needed. + bool wrongOrder = + nonStrictlyPostDominate(LHSEntryBlock, RHSEntryBlock, DT, LHS.PDT); + bool rightOrder = + nonStrictlyPostDominate(RHSEntryBlock, LHSEntryBlock, DT, LHS.PDT); + if (wrongOrder && rightOrder) { + // If common predecessor of LHS and RHS post dominates both + // FusionCandidates then, Order of FusionCandidate can be + // identified by its level in post dominator tree. + DomTreeNode *LNode = LHS.PDT->getNode(LHSEntryBlock); + DomTreeNode *RNode = LHS.PDT->getNode(RHSEntryBlock); + return (LNode->getLevel() > RNode->getLevel()); + } else if (wrongOrder) + return false; + else if (rightOrder) + return true; + + // If LHS does not non-strict Postdominate RHS and RHS does not non-strict + // Postdominate LHS then, there is no dominance relationship between the + // two FusionCandidates. Thus, they should not be in the same set together. llvm_unreachable( "No dominance relationship between these fusion candidates!"); } Index: llvm/test/Transforms/LoopFusion/undominated_loops.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/LoopFusion/undominated_loops.ll @@ -0,0 +1,26 @@ +; RUN: opt -S -passes=loop-simplify,newgvn,loop-fusion -pass-remarks-analysis=loop-fusion -disable-output < %s 2>&1 | FileCheck %s + +; CHECK: remark: :0:0: [function_0]: Loop is not a candidate for fusion: Loop has unknown trip count + +define void @function_0() { +entry_1: + br i1 false, label %bb_2, label %bb_3 + +bb_2: ; preds = %bb_7, %bb_5, %bb_2, %entry_1 + br i1 false, label %bb_2, label %bb_5 + +bb_3: ; preds = %bb_3, %entry_1 + br i1 false, label %bb_3, label %bb_4 + +bb_4: ; preds = %bb_3 + br label %bb_6 + +bb_5: ; preds = %bb_2 + br i1 undef, label %bb_2, label %bb_7 + +bb_6: ; preds = %bb_7, %bb_6, %bb_4 + br i1 undef, label %bb_7, label %bb_6 + +bb_7: ; preds = %bb_6, %bb_5 + br i1 undef, label %bb_2, label %bb_6 +}