1 //===- AMDGPUGlobalISelUtils.cpp ---------------------------------*- C++ -*-==//
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 #include "AMDGPUGlobalISelUtils.h"
10 #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h"
11 #include "llvm/IR/Constants.h"
12
13 using namespace llvm;
14 using namespace MIPatternMatch;
15
16 std::tuple<Register, unsigned, MachineInstr *>
getBaseWithConstantOffset(MachineRegisterInfo & MRI,Register Reg)17 AMDGPU::getBaseWithConstantOffset(MachineRegisterInfo &MRI, Register Reg) {
18 MachineInstr *Def = getDefIgnoringCopies(Reg, MRI);
19 if (!Def)
20 return std::make_tuple(Reg, 0, nullptr);
21
22 if (Def->getOpcode() == TargetOpcode::G_CONSTANT) {
23 unsigned Offset;
24 const MachineOperand &Op = Def->getOperand(1);
25 if (Op.isImm())
26 Offset = Op.getImm();
27 else
28 Offset = Op.getCImm()->getZExtValue();
29
30 return std::make_tuple(Register(), Offset, Def);
31 }
32
33 int64_t Offset;
34 if (Def->getOpcode() == TargetOpcode::G_ADD) {
35 // TODO: Handle G_OR used for add case
36 if (mi_match(Def->getOperand(2).getReg(), MRI, m_ICst(Offset)))
37 return std::make_tuple(Def->getOperand(1).getReg(), Offset, Def);
38
39 // FIXME: matcher should ignore copies
40 if (mi_match(Def->getOperand(2).getReg(), MRI, m_Copy(m_ICst(Offset))))
41 return std::make_tuple(Def->getOperand(1).getReg(), Offset, Def);
42 }
43
44 return std::make_tuple(Reg, 0, Def);
45 }
46