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