xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUGlobalISelUtils.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
1480093f4SDimitry Andric //===- AMDGPUGlobalISelUtils.cpp ---------------------------------*- C++ -*-==//
2480093f4SDimitry Andric //
3480093f4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4480093f4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5480093f4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6480093f4SDimitry Andric //
7480093f4SDimitry Andric //===----------------------------------------------------------------------===//
8480093f4SDimitry Andric 
9480093f4SDimitry Andric #include "AMDGPUGlobalISelUtils.h"
10480093f4SDimitry Andric #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h"
11480093f4SDimitry Andric #include "llvm/IR/Constants.h"
12480093f4SDimitry Andric 
13480093f4SDimitry Andric using namespace llvm;
14480093f4SDimitry Andric using namespace MIPatternMatch;
15480093f4SDimitry Andric 
16480093f4SDimitry Andric std::tuple<Register, unsigned, MachineInstr *>
17480093f4SDimitry Andric AMDGPU::getBaseWithConstantOffset(MachineRegisterInfo &MRI, Register Reg) {
18480093f4SDimitry Andric   MachineInstr *Def = getDefIgnoringCopies(Reg, MRI);
19480093f4SDimitry Andric   if (!Def)
20480093f4SDimitry Andric     return std::make_tuple(Reg, 0, nullptr);
21480093f4SDimitry Andric 
22480093f4SDimitry Andric   if (Def->getOpcode() == TargetOpcode::G_CONSTANT) {
23480093f4SDimitry Andric     unsigned Offset;
24480093f4SDimitry Andric     const MachineOperand &Op = Def->getOperand(1);
25480093f4SDimitry Andric     if (Op.isImm())
26480093f4SDimitry Andric       Offset = Op.getImm();
27480093f4SDimitry Andric     else
28480093f4SDimitry Andric       Offset = Op.getCImm()->getZExtValue();
29480093f4SDimitry Andric 
30480093f4SDimitry Andric     return std::make_tuple(Register(), Offset, Def);
31480093f4SDimitry Andric   }
32480093f4SDimitry Andric 
33480093f4SDimitry Andric   int64_t Offset;
34480093f4SDimitry Andric   if (Def->getOpcode() == TargetOpcode::G_ADD) {
35480093f4SDimitry Andric     // TODO: Handle G_OR used for add case
36480093f4SDimitry Andric     if (mi_match(Def->getOperand(2).getReg(), MRI, m_ICst(Offset)))
37480093f4SDimitry Andric       return std::make_tuple(Def->getOperand(1).getReg(), Offset, Def);
38480093f4SDimitry Andric 
39480093f4SDimitry Andric     // FIXME: matcher should ignore copies
40480093f4SDimitry Andric     if (mi_match(Def->getOperand(2).getReg(), MRI, m_Copy(m_ICst(Offset))))
41480093f4SDimitry Andric       return std::make_tuple(Def->getOperand(1).getReg(), Offset, Def);
42480093f4SDimitry Andric   }
43480093f4SDimitry Andric 
44480093f4SDimitry Andric   return std::make_tuple(Reg, 0, Def);
45480093f4SDimitry Andric }
46*5ffd83dbSDimitry Andric 
47*5ffd83dbSDimitry Andric bool AMDGPU::isLegalVOP3PShuffleMask(ArrayRef<int> Mask) {
48*5ffd83dbSDimitry Andric   assert(Mask.size() == 2);
49*5ffd83dbSDimitry Andric 
50*5ffd83dbSDimitry Andric   // If one half is undef, the other is trivially in the same reg.
51*5ffd83dbSDimitry Andric   if (Mask[0] == -1 || Mask[1] == -1)
52*5ffd83dbSDimitry Andric     return true;
53*5ffd83dbSDimitry Andric   return (Mask[0] & 2) == (Mask[1] & 2);
54*5ffd83dbSDimitry Andric }
55