/src/llvm-project/llvm/lib/Target/X86/X86MacroFusion.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- X86MacroFusion.cpp - X86 Macro Fusion ------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | /// \file This file contains the X86 implementation of the DAG scheduling |
10 | | /// mutation to pair instructions back to back. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "X86MacroFusion.h" |
15 | | #include "MCTargetDesc/X86BaseInfo.h" |
16 | | #include "X86Subtarget.h" |
17 | | #include "llvm/CodeGen/MacroFusion.h" |
18 | | #include "llvm/CodeGen/ScheduleDAGMutation.h" |
19 | | #include "llvm/CodeGen/TargetInstrInfo.h" |
20 | | |
21 | | using namespace llvm; |
22 | | |
23 | 100 | static X86::FirstMacroFusionInstKind classifyFirst(const MachineInstr &MI) { |
24 | 100 | return X86::classifyFirstOpcodeInMacroFusion(MI.getOpcode()); |
25 | 100 | } |
26 | | |
27 | 399 | static X86::SecondMacroFusionInstKind classifySecond(const MachineInstr &MI) { |
28 | 399 | X86::CondCode CC = X86::getCondFromBranch(MI); |
29 | 399 | return X86::classifySecondCondCodeInMacroFusion(CC); |
30 | 399 | } |
31 | | |
32 | | /// Check if the instr pair, FirstMI and SecondMI, should be fused |
33 | | /// together. Given SecondMI, when FirstMI is unspecified, then check if |
34 | | /// SecondMI may be part of a fused pair at all. |
35 | | static bool shouldScheduleAdjacent(const TargetInstrInfo &TII, |
36 | | const TargetSubtargetInfo &TSI, |
37 | | const MachineInstr *FirstMI, |
38 | 22.6k | const MachineInstr &SecondMI) { |
39 | 22.6k | const X86Subtarget &ST = static_cast<const X86Subtarget &>(TSI); |
40 | | |
41 | | // Check if this processor supports any kind of fusion. |
42 | 22.6k | if (!(ST.hasBranchFusion() || ST.hasMacroFusion())) |
43 | 22.2k | return false; |
44 | | |
45 | 399 | const X86::SecondMacroFusionInstKind BranchKind = classifySecond(SecondMI); |
46 | | |
47 | 399 | if (BranchKind == X86::SecondMacroFusionInstKind::Invalid) |
48 | 203 | return false; // Second cannot be fused with anything. |
49 | | |
50 | 196 | if (FirstMI == nullptr) |
51 | 96 | return true; // We're only checking whether Second can be fused at all. |
52 | | |
53 | 100 | const X86::FirstMacroFusionInstKind TestKind = classifyFirst(*FirstMI); |
54 | | |
55 | 100 | if (ST.hasBranchFusion()) { |
56 | | // Branch fusion can merge CMP and TEST with all conditional jumps. |
57 | 0 | return (TestKind == X86::FirstMacroFusionInstKind::Cmp || |
58 | 0 | TestKind == X86::FirstMacroFusionInstKind::Test); |
59 | 0 | } |
60 | | |
61 | 100 | if (ST.hasMacroFusion()) { |
62 | 100 | return X86::isMacroFused(TestKind, BranchKind); |
63 | 100 | } |
64 | | |
65 | 0 | llvm_unreachable("unknown fusion type"); |
66 | 0 | } |
67 | | |
68 | | namespace llvm { |
69 | | |
70 | 22.2k | std::unique_ptr<ScheduleDAGMutation> createX86MacroFusionDAGMutation() { |
71 | 22.2k | return createMacroFusionDAGMutation(shouldScheduleAdjacent, |
72 | 22.2k | /*BranchOnly=*/true); |
73 | 22.2k | } |
74 | | |
75 | | } // end namespace llvm |