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