[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / lib / CodeGen / GlobalISel / InstructionSelector.cpp
blob8959d215ecd1a32fde85f388b3563a6fb7790ad7
1 //===- llvm/CodeGen/GlobalISel/InstructionSelector.cpp --------------------===//
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 /// \file
10 /// This file implements the InstructionSelector class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
15 #include "llvm/CodeGen/GlobalISel/Utils.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/MachineOperand.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #define DEBUG_TYPE "instructionselector"
22 using namespace llvm;
24 InstructionSelector::MatcherState::MatcherState(unsigned MaxRenderers)
25 : Renderers(MaxRenderers) {}
27 InstructionSelector::InstructionSelector() = default;
29 bool InstructionSelector::isOperandImmEqual(
30 const MachineOperand &MO, int64_t Value,
31 const MachineRegisterInfo &MRI) const {
32 if (MO.isReg() && MO.getReg())
33 if (auto VRegVal = getIConstantVRegValWithLookThrough(MO.getReg(), MRI))
34 return VRegVal->Value.getSExtValue() == Value;
35 return false;
38 bool InstructionSelector::isBaseWithConstantOffset(
39 const MachineOperand &Root, const MachineRegisterInfo &MRI) const {
40 if (!Root.isReg())
41 return false;
43 MachineInstr *RootI = MRI.getVRegDef(Root.getReg());
44 if (RootI->getOpcode() != TargetOpcode::G_PTR_ADD)
45 return false;
47 MachineOperand &RHS = RootI->getOperand(2);
48 MachineInstr *RHSI = MRI.getVRegDef(RHS.getReg());
49 if (RHSI->getOpcode() != TargetOpcode::G_CONSTANT)
50 return false;
52 return true;
55 bool InstructionSelector::isObviouslySafeToFold(MachineInstr &MI,
56 MachineInstr &IntoMI) const {
57 // Immediate neighbours are already folded.
58 if (MI.getParent() == IntoMI.getParent() &&
59 std::next(MI.getIterator()) == IntoMI.getIterator())
60 return true;
62 // Convergent instructions cannot be moved in the CFG.
63 if (MI.isConvergent() && MI.getParent() != IntoMI.getParent())
64 return false;
66 return !MI.mayLoadOrStore() && !MI.mayRaiseFPException() &&
67 !MI.hasUnmodeledSideEffects() && MI.implicit_operands().empty();