Index: llvm/include/llvm/CodeGen/TargetInstrInfo.h =================================================================== --- llvm/include/llvm/CodeGen/TargetInstrInfo.h +++ llvm/include/llvm/CodeGen/TargetInstrInfo.h @@ -612,6 +612,13 @@ llvm_unreachable("target did not implement"); } + /// Analyze a basic block \p Block and its instructions and tell if it + /// is profitable to move it up in the final layout of a function. Some + /// hardware loop instructions can work better with backward branches. + virtual bool isProfitableToMoveUp(const MachineBasicBlock *Block) const { + return true; + } + /// Analyze the branching code at the end of MBB, returning /// true if it cannot be understood (e.g. it's a switch dispatch or isn't /// implemented for a target). Upon success, this returns false and returns Index: llvm/lib/CodeGen/MachineBlockPlacement.cpp =================================================================== --- llvm/lib/CodeGen/MachineBlockPlacement.cpp +++ llvm/lib/CodeGen/MachineBlockPlacement.cpp @@ -1907,6 +1907,10 @@ MachineBlockPlacement::canMoveBottomBlockToTop( const MachineBasicBlock *BottomBlock, const MachineBasicBlock *OldTop) { + + if (!TII->isProfitableToMoveUp(BottomBlock)) + return false; + if (BottomBlock->pred_size() != 1) return true; MachineBasicBlock *Pred = *BottomBlock->pred_begin();