10b57cec5SDimitry Andric //===- llvm/CodeGen/GlobalISel/InstructionSelector.cpp --------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This file implements the InstructionSelector class.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/Utils.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
220b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
230b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
240b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
250b57cec5SDimitry Andric #include <cassert>
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric #define DEBUG_TYPE "instructionselector"
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric using namespace llvm;
300b57cec5SDimitry Andric 
MatcherState(unsigned MaxRenderers)310b57cec5SDimitry Andric InstructionSelector::MatcherState::MatcherState(unsigned MaxRenderers)
320b57cec5SDimitry Andric     : Renderers(MaxRenderers), MIs() {}
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric InstructionSelector::InstructionSelector() = default;
350b57cec5SDimitry Andric 
isOperandImmEqual(const MachineOperand & MO,int64_t Value,const MachineRegisterInfo & MRI) const360b57cec5SDimitry Andric bool InstructionSelector::isOperandImmEqual(
370b57cec5SDimitry Andric     const MachineOperand &MO, int64_t Value,
380b57cec5SDimitry Andric     const MachineRegisterInfo &MRI) const {
390b57cec5SDimitry Andric   if (MO.isReg() && MO.getReg())
400b57cec5SDimitry Andric     if (auto VRegVal = getConstantVRegValWithLookThrough(MO.getReg(), MRI))
41*af732203SDimitry Andric       return VRegVal->Value.getSExtValue() == Value;
420b57cec5SDimitry Andric   return false;
430b57cec5SDimitry Andric }
440b57cec5SDimitry Andric 
isBaseWithConstantOffset(const MachineOperand & Root,const MachineRegisterInfo & MRI) const450b57cec5SDimitry Andric bool InstructionSelector::isBaseWithConstantOffset(
460b57cec5SDimitry Andric     const MachineOperand &Root, const MachineRegisterInfo &MRI) const {
470b57cec5SDimitry Andric   if (!Root.isReg())
480b57cec5SDimitry Andric     return false;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   MachineInstr *RootI = MRI.getVRegDef(Root.getReg());
51480093f4SDimitry Andric   if (RootI->getOpcode() != TargetOpcode::G_PTR_ADD)
520b57cec5SDimitry Andric     return false;
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   MachineOperand &RHS = RootI->getOperand(2);
550b57cec5SDimitry Andric   MachineInstr *RHSI = MRI.getVRegDef(RHS.getReg());
560b57cec5SDimitry Andric   if (RHSI->getOpcode() != TargetOpcode::G_CONSTANT)
570b57cec5SDimitry Andric     return false;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   return true;
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric 
isObviouslySafeToFold(MachineInstr & MI,MachineInstr & IntoMI) const620b57cec5SDimitry Andric bool InstructionSelector::isObviouslySafeToFold(MachineInstr &MI,
630b57cec5SDimitry Andric                                                 MachineInstr &IntoMI) const {
640b57cec5SDimitry Andric   // Immediate neighbours are already folded.
650b57cec5SDimitry Andric   if (MI.getParent() == IntoMI.getParent() &&
660b57cec5SDimitry Andric       std::next(MI.getIterator()) == IntoMI.getIterator())
670b57cec5SDimitry Andric     return true;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   return !MI.mayLoadOrStore() && !MI.mayRaiseFPException() &&
708bcb0991SDimitry Andric          !MI.hasUnmodeledSideEffects() && MI.implicit_operands().empty();
710b57cec5SDimitry Andric }
72