xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/X86/X86MacroFusion.cpp (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
10b57cec5SDimitry Andric //===- X86MacroFusion.cpp - X86 Macro Fusion ------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric /// \file This file contains the X86 implementation of the DAG scheduling
100b57cec5SDimitry Andric /// mutation to pair instructions back to back.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "X86MacroFusion.h"
155ffd83dbSDimitry Andric #include "MCTargetDesc/X86BaseInfo.h"
160b57cec5SDimitry Andric #include "X86Subtarget.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MacroFusion.h"
1881ad6265SDimitry Andric #include "llvm/CodeGen/ScheduleDAGMutation.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric 
classifyFirst(const MachineInstr & MI)23480093f4SDimitry Andric static X86::FirstMacroFusionInstKind classifyFirst(const MachineInstr &MI) {
24480093f4SDimitry Andric   return X86::classifyFirstOpcodeInMacroFusion(MI.getOpcode());
250b57cec5SDimitry Andric }
260b57cec5SDimitry Andric 
classifySecond(const MachineInstr & MI)27480093f4SDimitry Andric static X86::SecondMacroFusionInstKind classifySecond(const MachineInstr &MI) {
280b57cec5SDimitry Andric   X86::CondCode CC = X86::getCondFromBranch(MI);
29480093f4SDimitry Andric   return X86::classifySecondCondCodeInMacroFusion(CC);
300b57cec5SDimitry Andric }
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric /// Check if the instr pair, FirstMI and SecondMI, should be fused
330b57cec5SDimitry Andric /// together. Given SecondMI, when FirstMI is unspecified, then check if
340b57cec5SDimitry Andric /// SecondMI may be part of a fused pair at all.
shouldScheduleAdjacent(const TargetInstrInfo & TII,const TargetSubtargetInfo & TSI,const MachineInstr * FirstMI,const MachineInstr & SecondMI)350b57cec5SDimitry Andric static bool shouldScheduleAdjacent(const TargetInstrInfo &TII,
360b57cec5SDimitry Andric                                    const TargetSubtargetInfo &TSI,
370b57cec5SDimitry Andric                                    const MachineInstr *FirstMI,
380b57cec5SDimitry Andric                                    const MachineInstr &SecondMI) {
390b57cec5SDimitry Andric   const X86Subtarget &ST = static_cast<const X86Subtarget &>(TSI);
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   // Check if this processor supports any kind of fusion.
420b57cec5SDimitry Andric   if (!(ST.hasBranchFusion() || ST.hasMacroFusion()))
430b57cec5SDimitry Andric     return false;
440b57cec5SDimitry Andric 
45480093f4SDimitry Andric   const X86::SecondMacroFusionInstKind BranchKind = classifySecond(SecondMI);
460b57cec5SDimitry Andric 
47480093f4SDimitry Andric   if (BranchKind == X86::SecondMacroFusionInstKind::Invalid)
480b57cec5SDimitry Andric     return false; // Second cannot be fused with anything.
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   if (FirstMI == nullptr)
510b57cec5SDimitry Andric     return true; // We're only checking whether Second can be fused at all.
520b57cec5SDimitry Andric 
53480093f4SDimitry Andric   const X86::FirstMacroFusionInstKind TestKind = classifyFirst(*FirstMI);
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   if (ST.hasBranchFusion()) {
560b57cec5SDimitry Andric     // Branch fusion can merge CMP and TEST with all conditional jumps.
57480093f4SDimitry Andric     return (TestKind == X86::FirstMacroFusionInstKind::Cmp ||
58480093f4SDimitry Andric             TestKind == X86::FirstMacroFusionInstKind::Test);
590b57cec5SDimitry Andric   }
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   if (ST.hasMacroFusion()) {
62480093f4SDimitry Andric     return X86::isMacroFused(TestKind, BranchKind);
630b57cec5SDimitry Andric   }
640b57cec5SDimitry Andric 
65480093f4SDimitry Andric   llvm_unreachable("unknown fusion type");
660b57cec5SDimitry Andric }
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric namespace llvm {
690b57cec5SDimitry Andric 
createX86MacroFusionDAGMutation()705f757f3fSDimitry Andric std::unique_ptr<ScheduleDAGMutation> createX86MacroFusionDAGMutation() {
71*cb14a3feSDimitry Andric   return createMacroFusionDAGMutation(shouldScheduleAdjacent,
72*cb14a3feSDimitry Andric                                       /*BranchOnly=*/true);
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric } // end namespace llvm
76