Index: llvm/lib/CodeGen/MachineScheduler.cpp =================================================================== --- llvm/lib/CodeGen/MachineScheduler.cpp +++ llvm/lib/CodeGen/MachineScheduler.cpp @@ -2969,6 +2969,33 @@ return; if (SameBoundary) { + // Choose in priority instructions with lower latencies. + if (Zone->isTop()) { + // Top-down scheduling. + unsigned TryDepth = TryCand.SU->getDepth(); + unsigned Depth = Cand.SU->getDepth(); + + // Prefer lowest depth. + if (TryDepth > Depth) + return; + + // For equal depth, prefer higher height. + if (TryDepth == Depth && TryCand.SU->getHeight() < Cand.SU->getHeight()) + return; + } else { + // Bottom-up scheduling. + unsigned TryHeight = TryCand.SU->getHeight(); + unsigned Height = Cand.SU->getHeight(); + + // Prefer lowest height. + if (TryHeight > Height) + return; + + // For equal height, prefer higher depth. + if (TryHeight == Height && TryCand.SU->getDepth() < Cand.SU->getDepth()) + return; + } + // Avoid critical resource consumption and balance the schedule. TryCand.initResourceDelta(DAG, SchedModel); if (tryLess(TryCand.ResDelta.CritResources, Cand.ResDelta.CritResources, @@ -3273,6 +3300,10 @@ TryCand, Cand, Cluster)) return; + // Choose in priority instructions with lower latencies. + if (TryCand.SU->getDepth() > Cand.SU->getDepth()) + return; + // Avoid critical resource consumption and balance the schedule. if (tryLess(TryCand.ResDelta.CritResources, Cand.ResDelta.CritResources, TryCand, Cand, ResourceReduce))