1//===--- AMDGPUMacroFusion.cpp - AMDGPU 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 AMDGPU implementation of the DAG scheduling
10/// mutation to pair instructions back to back.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AMDGPUMacroFusion.h"
15#include "SIInstrInfo.h"
16#include "llvm/CodeGen/MacroFusion.h"
17
18using namespace llvm;
19
20namespace {
21
22/// Check if the instr pair, FirstMI and SecondMI, should be fused
23/// together. Given SecondMI, when FirstMI is unspecified, then check if
24/// SecondMI may be part of a fused pair at all.
25static bool shouldScheduleAdjacent(const TargetInstrInfo &TII_,
26 const TargetSubtargetInfo &TSI,
27 const MachineInstr *FirstMI,
28 const MachineInstr &SecondMI,
29 const SDep *Dep) {
30 if (isNonDataDep(Dep))
31 return false;
32 const SIInstrInfo &TII = static_cast<const SIInstrInfo&>(TII_);
33
34 switch (SecondMI.getOpcode()) {
35 case AMDGPU::V_ADDC_U32_e64:
36 case AMDGPU::V_SUBB_U32_e64:
37 case AMDGPU::V_SUBBREV_U32_e64:
38 case AMDGPU::V_CNDMASK_B32_e64: {
39 // Try to cluster defs of condition registers to their uses. This improves
40 // the chance VCC will be available which will allow shrinking to VOP2
41 // encodings.
42 if (!FirstMI)
43 return true;
44
45 const MachineBasicBlock &MBB = *FirstMI->getParent();
46 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
47 const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
48 const MachineOperand *Src2 = TII.getNamedOperand(MI: SecondMI,
49 OperandName: AMDGPU::OpName::src2);
50 return FirstMI->definesRegister(Reg: Src2->getReg(), TRI);
51 }
52 default:
53 return false;
54 }
55
56 return false;
57}
58
59} // end namespace
60
61
62namespace llvm {
63
64std::unique_ptr<ScheduleDAGMutation> createAMDGPUMacroFusionDAGMutation() {
65 return createMacroFusionDAGMutation(Predicates: shouldScheduleAdjacent);
66}
67
68} // end namespace llvm
69