2017-02-01 10:54:34 +08:00
|
|
|
//===- X86MacroFusion.cpp - X86 Macro Fusion ------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-02-01 10:54:34 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2017-06-19 20:53:31 +08:00
|
|
|
/// \file This file contains the X86 implementation of the DAG scheduling
|
|
|
|
/// mutation to pair instructions back to back.
|
2017-02-01 10:54:34 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-12-14 06:21:02 +08:00
|
|
|
#include "X86MacroFusion.h"
|
2017-02-01 10:54:34 +08:00
|
|
|
#include "X86Subtarget.h"
|
2017-06-19 20:53:31 +08:00
|
|
|
#include "llvm/CodeGen/MacroFusion.h"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-02-22 06:16:13 +08:00
|
|
|
|
2017-02-01 10:54:34 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2019-03-28 22:12:46 +08:00
|
|
|
namespace {
|
2017-02-01 10:54:34 +08:00
|
|
|
|
2019-03-28 22:12:46 +08:00
|
|
|
// The classification for the first instruction.
|
|
|
|
enum class FirstInstrKind { Test, Cmp, And, ALU, IncDec, Invalid };
|
2017-02-01 10:54:34 +08:00
|
|
|
|
2019-03-28 22:12:46 +08:00
|
|
|
// The classification for the second instruction (jump).
|
|
|
|
enum class JumpKind {
|
|
|
|
// JE, JL, JG and variants.
|
|
|
|
ELG,
|
|
|
|
// JA, JB and variants.
|
|
|
|
AB,
|
|
|
|
// JS, JP, JO and variants.
|
|
|
|
SPO,
|
|
|
|
// Not a fusable jump.
|
|
|
|
Invalid,
|
|
|
|
};
|
2017-02-01 10:54:34 +08:00
|
|
|
|
2019-03-28 22:12:46 +08:00
|
|
|
} // namespace
|
2017-02-01 10:54:34 +08:00
|
|
|
|
2019-03-28 22:12:46 +08:00
|
|
|
static FirstInstrKind classifyFirst(const MachineInstr &MI) {
|
|
|
|
switch (MI.getOpcode()) {
|
2017-02-01 10:54:34 +08:00
|
|
|
default:
|
2019-03-28 22:12:46 +08:00
|
|
|
return FirstInstrKind::Invalid;
|
2017-02-01 10:54:34 +08:00
|
|
|
case X86::TEST8rr:
|
|
|
|
case X86::TEST16rr:
|
|
|
|
case X86::TEST32rr:
|
|
|
|
case X86::TEST64rr:
|
|
|
|
case X86::TEST8ri:
|
|
|
|
case X86::TEST16ri:
|
|
|
|
case X86::TEST32ri:
|
|
|
|
case X86::TEST64ri32:
|
2017-10-02 07:53:53 +08:00
|
|
|
case X86::TEST8mr:
|
|
|
|
case X86::TEST16mr:
|
|
|
|
case X86::TEST32mr:
|
|
|
|
case X86::TEST64mr:
|
2019-03-28 22:12:46 +08:00
|
|
|
return FirstInstrKind::Test;
|
2017-02-01 10:54:34 +08:00
|
|
|
case X86::AND16ri:
|
|
|
|
case X86::AND16ri8:
|
|
|
|
case X86::AND16rm:
|
|
|
|
case X86::AND16rr:
|
|
|
|
case X86::AND32ri:
|
|
|
|
case X86::AND32ri8:
|
|
|
|
case X86::AND32rm:
|
|
|
|
case X86::AND32rr:
|
|
|
|
case X86::AND64ri32:
|
|
|
|
case X86::AND64ri8:
|
|
|
|
case X86::AND64rm:
|
|
|
|
case X86::AND64rr:
|
|
|
|
case X86::AND8ri:
|
|
|
|
case X86::AND8rm:
|
|
|
|
case X86::AND8rr:
|
2019-03-28 22:12:46 +08:00
|
|
|
return FirstInstrKind::And;
|
2017-02-01 10:54:34 +08:00
|
|
|
case X86::CMP16ri:
|
|
|
|
case X86::CMP16ri8:
|
|
|
|
case X86::CMP16rm:
|
|
|
|
case X86::CMP16rr:
|
2018-08-11 14:42:50 +08:00
|
|
|
case X86::CMP16mr:
|
2017-02-01 10:54:34 +08:00
|
|
|
case X86::CMP32ri:
|
|
|
|
case X86::CMP32ri8:
|
|
|
|
case X86::CMP32rm:
|
|
|
|
case X86::CMP32rr:
|
2018-08-11 14:42:50 +08:00
|
|
|
case X86::CMP32mr:
|
2017-02-01 10:54:34 +08:00
|
|
|
case X86::CMP64ri32:
|
|
|
|
case X86::CMP64ri8:
|
|
|
|
case X86::CMP64rm:
|
|
|
|
case X86::CMP64rr:
|
2018-08-11 14:42:50 +08:00
|
|
|
case X86::CMP64mr:
|
2017-02-01 10:54:34 +08:00
|
|
|
case X86::CMP8ri:
|
|
|
|
case X86::CMP8rm:
|
|
|
|
case X86::CMP8rr:
|
2018-08-11 14:42:50 +08:00
|
|
|
case X86::CMP8mr:
|
2019-03-28 22:12:46 +08:00
|
|
|
return FirstInstrKind::Cmp;
|
2017-02-01 10:54:34 +08:00
|
|
|
case X86::ADD16ri:
|
|
|
|
case X86::ADD16ri8:
|
|
|
|
case X86::ADD16ri8_DB:
|
|
|
|
case X86::ADD16ri_DB:
|
|
|
|
case X86::ADD16rm:
|
|
|
|
case X86::ADD16rr:
|
|
|
|
case X86::ADD16rr_DB:
|
|
|
|
case X86::ADD32ri:
|
|
|
|
case X86::ADD32ri8:
|
|
|
|
case X86::ADD32ri8_DB:
|
|
|
|
case X86::ADD32ri_DB:
|
|
|
|
case X86::ADD32rm:
|
|
|
|
case X86::ADD32rr:
|
|
|
|
case X86::ADD32rr_DB:
|
|
|
|
case X86::ADD64ri32:
|
|
|
|
case X86::ADD64ri32_DB:
|
|
|
|
case X86::ADD64ri8:
|
|
|
|
case X86::ADD64ri8_DB:
|
|
|
|
case X86::ADD64rm:
|
|
|
|
case X86::ADD64rr:
|
|
|
|
case X86::ADD64rr_DB:
|
|
|
|
case X86::ADD8ri:
|
2019-03-06 02:37:33 +08:00
|
|
|
case X86::ADD8ri_DB:
|
2017-02-01 10:54:34 +08:00
|
|
|
case X86::ADD8rm:
|
|
|
|
case X86::ADD8rr:
|
2019-03-06 02:37:33 +08:00
|
|
|
case X86::ADD8rr_DB:
|
2017-02-01 10:54:34 +08:00
|
|
|
case X86::SUB16ri:
|
|
|
|
case X86::SUB16ri8:
|
|
|
|
case X86::SUB16rm:
|
|
|
|
case X86::SUB16rr:
|
|
|
|
case X86::SUB32ri:
|
|
|
|
case X86::SUB32ri8:
|
|
|
|
case X86::SUB32rm:
|
|
|
|
case X86::SUB32rr:
|
|
|
|
case X86::SUB64ri32:
|
|
|
|
case X86::SUB64ri8:
|
|
|
|
case X86::SUB64rm:
|
|
|
|
case X86::SUB64rr:
|
|
|
|
case X86::SUB8ri:
|
|
|
|
case X86::SUB8rm:
|
|
|
|
case X86::SUB8rr:
|
2019-03-28 22:12:46 +08:00
|
|
|
return FirstInstrKind::ALU;
|
2017-02-01 10:54:34 +08:00
|
|
|
case X86::INC16r:
|
|
|
|
case X86::INC32r:
|
|
|
|
case X86::INC64r:
|
|
|
|
case X86::INC8r:
|
|
|
|
case X86::DEC16r:
|
|
|
|
case X86::DEC32r:
|
|
|
|
case X86::DEC64r:
|
|
|
|
case X86::DEC8r:
|
2019-03-28 22:12:46 +08:00
|
|
|
return FirstInstrKind::IncDec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static JumpKind classifySecond(const MachineInstr &MI) {
|
[X86] Merge the different Jcc instructions for each condition code into single instructions that store the condition code as an operand.
Summary:
This avoids needing an isel pattern for each condition code. And it removes translation switches for converting between Jcc instructions and condition codes.
Now the printer, encoder and disassembler take care of converting the immediate. We use InstAliases to handle the assembly matching. But we print using the asm string in the instruction definition. The instruction itself is marked IsCodeGenOnly=1 to hide it from the assembly parser.
Reviewers: spatel, lebedev.ri, courbet, gchatelet, RKSimon
Reviewed By: RKSimon
Subscribers: MatzeB, qcolombet, eraman, hiraditya, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60228
llvm-svn: 357802
2019-04-06 03:28:09 +08:00
|
|
|
X86::CondCode CC = X86::getCondFromBranch(MI);
|
|
|
|
if (CC == X86::COND_INVALID)
|
|
|
|
return JumpKind::Invalid;
|
|
|
|
|
|
|
|
switch (CC) {
|
2019-03-28 22:12:46 +08:00
|
|
|
default:
|
|
|
|
return JumpKind::Invalid;
|
[X86] Merge the different Jcc instructions for each condition code into single instructions that store the condition code as an operand.
Summary:
This avoids needing an isel pattern for each condition code. And it removes translation switches for converting between Jcc instructions and condition codes.
Now the printer, encoder and disassembler take care of converting the immediate. We use InstAliases to handle the assembly matching. But we print using the asm string in the instruction definition. The instruction itself is marked IsCodeGenOnly=1 to hide it from the assembly parser.
Reviewers: spatel, lebedev.ri, courbet, gchatelet, RKSimon
Reviewed By: RKSimon
Subscribers: MatzeB, qcolombet, eraman, hiraditya, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60228
llvm-svn: 357802
2019-04-06 03:28:09 +08:00
|
|
|
case X86::COND_E:
|
|
|
|
case X86::COND_NE:
|
|
|
|
case X86::COND_L:
|
|
|
|
case X86::COND_LE:
|
|
|
|
case X86::COND_G:
|
|
|
|
case X86::COND_GE:
|
2019-03-28 22:12:46 +08:00
|
|
|
return JumpKind::ELG;
|
[X86] Merge the different Jcc instructions for each condition code into single instructions that store the condition code as an operand.
Summary:
This avoids needing an isel pattern for each condition code. And it removes translation switches for converting between Jcc instructions and condition codes.
Now the printer, encoder and disassembler take care of converting the immediate. We use InstAliases to handle the assembly matching. But we print using the asm string in the instruction definition. The instruction itself is marked IsCodeGenOnly=1 to hide it from the assembly parser.
Reviewers: spatel, lebedev.ri, courbet, gchatelet, RKSimon
Reviewed By: RKSimon
Subscribers: MatzeB, qcolombet, eraman, hiraditya, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60228
llvm-svn: 357802
2019-04-06 03:28:09 +08:00
|
|
|
case X86::COND_B:
|
|
|
|
case X86::COND_BE:
|
|
|
|
case X86::COND_A:
|
|
|
|
case X86::COND_AE:
|
2019-03-28 22:12:46 +08:00
|
|
|
return JumpKind::AB;
|
[X86] Merge the different Jcc instructions for each condition code into single instructions that store the condition code as an operand.
Summary:
This avoids needing an isel pattern for each condition code. And it removes translation switches for converting between Jcc instructions and condition codes.
Now the printer, encoder and disassembler take care of converting the immediate. We use InstAliases to handle the assembly matching. But we print using the asm string in the instruction definition. The instruction itself is marked IsCodeGenOnly=1 to hide it from the assembly parser.
Reviewers: spatel, lebedev.ri, courbet, gchatelet, RKSimon
Reviewed By: RKSimon
Subscribers: MatzeB, qcolombet, eraman, hiraditya, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60228
llvm-svn: 357802
2019-04-06 03:28:09 +08:00
|
|
|
case X86::COND_S:
|
|
|
|
case X86::COND_NS:
|
|
|
|
case X86::COND_P:
|
|
|
|
case X86::COND_NP:
|
|
|
|
case X86::COND_O:
|
|
|
|
case X86::COND_NO:
|
2019-03-28 22:12:46 +08:00
|
|
|
return JumpKind::SPO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if the instr pair, FirstMI and SecondMI, should be fused
|
|
|
|
/// together. Given SecondMI, when FirstMI is unspecified, then check if
|
|
|
|
/// SecondMI may be part of a fused pair at all.
|
|
|
|
static bool shouldScheduleAdjacent(const TargetInstrInfo &TII,
|
|
|
|
const TargetSubtargetInfo &TSI,
|
|
|
|
const MachineInstr *FirstMI,
|
|
|
|
const MachineInstr &SecondMI) {
|
|
|
|
const X86Subtarget &ST = static_cast<const X86Subtarget &>(TSI);
|
|
|
|
|
|
|
|
// Check if this processor supports any kind of fusion.
|
|
|
|
if (!(ST.hasBranchFusion() || ST.hasMacroFusion()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const JumpKind BranchKind = classifySecond(SecondMI);
|
|
|
|
|
|
|
|
if (BranchKind == JumpKind::Invalid)
|
|
|
|
return false; // Second cannot be fused with anything.
|
|
|
|
|
|
|
|
if (FirstMI == nullptr)
|
|
|
|
return true; // We're only checking whether Second can be fused at all.
|
|
|
|
|
|
|
|
const FirstInstrKind TestKind = classifyFirst(*FirstMI);
|
|
|
|
|
|
|
|
if (ST.hasBranchFusion()) {
|
|
|
|
// Branch fusion can merge CMP and TEST with all conditional jumps.
|
|
|
|
return (TestKind == FirstInstrKind::Cmp ||
|
|
|
|
TestKind == FirstInstrKind::Test);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ST.hasMacroFusion()) {
|
|
|
|
// Macro Fusion rules are a bit more complex. See Agner Fog's
|
|
|
|
// Microarchitecture table 9.2 "Instruction Fusion".
|
|
|
|
switch (TestKind) {
|
|
|
|
case FirstInstrKind::Test:
|
|
|
|
case FirstInstrKind::And:
|
|
|
|
return true;
|
|
|
|
case FirstInstrKind::Cmp:
|
|
|
|
case FirstInstrKind::ALU:
|
|
|
|
return BranchKind == JumpKind::ELG || BranchKind == JumpKind::AB;
|
|
|
|
case FirstInstrKind::IncDec:
|
|
|
|
return BranchKind == JumpKind::ELG;
|
|
|
|
case FirstInstrKind::Invalid:
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-01 10:54:34 +08:00
|
|
|
}
|
2019-03-28 22:12:46 +08:00
|
|
|
|
|
|
|
llvm_unreachable("unknown branch fusion type");
|
2017-02-01 10:54:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
std::unique_ptr<ScheduleDAGMutation>
|
|
|
|
createX86MacroFusionDAGMutation () {
|
2017-06-19 20:53:31 +08:00
|
|
|
return createBranchMacroFusionDAGMutation(shouldScheduleAdjacent);
|
2017-02-01 10:54:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace llvm
|