1*fe013be4SDimitry Andric //===- llvm/CodeGen/GlobalISel/GIMatchTableExecutor.cpp -------------------===//
2*fe013be4SDimitry Andric //
3*fe013be4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*fe013be4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*fe013be4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*fe013be4SDimitry Andric //
7*fe013be4SDimitry Andric //===----------------------------------------------------------------------===//
8*fe013be4SDimitry Andric //
9*fe013be4SDimitry Andric /// \file
10*fe013be4SDimitry Andric /// This file implements the GIMatchTableExecutor class.
11*fe013be4SDimitry Andric //
12*fe013be4SDimitry Andric //===----------------------------------------------------------------------===//
13*fe013be4SDimitry Andric 
14*fe013be4SDimitry Andric #include "llvm/CodeGen/GlobalISel/GIMatchTableExecutor.h"
15*fe013be4SDimitry Andric #include "llvm/CodeGen/GlobalISel/Utils.h"
16*fe013be4SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
17*fe013be4SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
18*fe013be4SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
19*fe013be4SDimitry Andric 
20*fe013be4SDimitry Andric #define DEBUG_TYPE "gi-match-table-executor"
21*fe013be4SDimitry Andric 
22*fe013be4SDimitry Andric using namespace llvm;
23*fe013be4SDimitry Andric 
24*fe013be4SDimitry Andric GIMatchTableExecutor::MatcherState::MatcherState(unsigned MaxRenderers)
25*fe013be4SDimitry Andric     : Renderers(MaxRenderers) {}
26*fe013be4SDimitry Andric 
27*fe013be4SDimitry Andric GIMatchTableExecutor::GIMatchTableExecutor() = default;
28*fe013be4SDimitry Andric 
29*fe013be4SDimitry Andric bool GIMatchTableExecutor::isOperandImmEqual(
30*fe013be4SDimitry Andric     const MachineOperand &MO, int64_t Value,
31*fe013be4SDimitry Andric     const MachineRegisterInfo &MRI) const {
32*fe013be4SDimitry Andric   if (MO.isReg() && MO.getReg())
33*fe013be4SDimitry Andric     if (auto VRegVal = getIConstantVRegValWithLookThrough(MO.getReg(), MRI))
34*fe013be4SDimitry Andric       return VRegVal->Value.getSExtValue() == Value;
35*fe013be4SDimitry Andric   return false;
36*fe013be4SDimitry Andric }
37*fe013be4SDimitry Andric 
38*fe013be4SDimitry Andric bool GIMatchTableExecutor::isBaseWithConstantOffset(
39*fe013be4SDimitry Andric     const MachineOperand &Root, const MachineRegisterInfo &MRI) const {
40*fe013be4SDimitry Andric   if (!Root.isReg())
41*fe013be4SDimitry Andric     return false;
42*fe013be4SDimitry Andric 
43*fe013be4SDimitry Andric   MachineInstr *RootI = MRI.getVRegDef(Root.getReg());
44*fe013be4SDimitry Andric   if (RootI->getOpcode() != TargetOpcode::G_PTR_ADD)
45*fe013be4SDimitry Andric     return false;
46*fe013be4SDimitry Andric 
47*fe013be4SDimitry Andric   MachineOperand &RHS = RootI->getOperand(2);
48*fe013be4SDimitry Andric   MachineInstr *RHSI = MRI.getVRegDef(RHS.getReg());
49*fe013be4SDimitry Andric   if (RHSI->getOpcode() != TargetOpcode::G_CONSTANT)
50*fe013be4SDimitry Andric     return false;
51*fe013be4SDimitry Andric 
52*fe013be4SDimitry Andric   return true;
53*fe013be4SDimitry Andric }
54*fe013be4SDimitry Andric 
55*fe013be4SDimitry Andric bool GIMatchTableExecutor::isObviouslySafeToFold(MachineInstr &MI,
56*fe013be4SDimitry Andric                                                  MachineInstr &IntoMI) const {
57*fe013be4SDimitry Andric   // Immediate neighbours are already folded.
58*fe013be4SDimitry Andric   if (MI.getParent() == IntoMI.getParent() &&
59*fe013be4SDimitry Andric       std::next(MI.getIterator()) == IntoMI.getIterator())
60*fe013be4SDimitry Andric     return true;
61*fe013be4SDimitry Andric 
62*fe013be4SDimitry Andric   // Convergent instructions cannot be moved in the CFG.
63*fe013be4SDimitry Andric   if (MI.isConvergent() && MI.getParent() != IntoMI.getParent())
64*fe013be4SDimitry Andric     return false;
65*fe013be4SDimitry Andric 
66*fe013be4SDimitry Andric   return !MI.mayLoadOrStore() && !MI.mayRaiseFPException() &&
67*fe013be4SDimitry Andric          !MI.hasUnmodeledSideEffects() && MI.implicit_operands().empty();
68*fe013be4SDimitry Andric }
69