1 //===- llvm/CodeGen/GlobalISel/InstructionSelector.cpp --------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
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
;
38 bool InstructionSelector::isBaseWithConstantOffset(
39 const MachineOperand
&Root
, const MachineRegisterInfo
&MRI
) const {
43 MachineInstr
*RootI
= MRI
.getVRegDef(Root
.getReg());
44 if (RootI
->getOpcode() != TargetOpcode::G_PTR_ADD
)
47 MachineOperand
&RHS
= RootI
->getOperand(2);
48 MachineInstr
*RHSI
= MRI
.getVRegDef(RHS
.getReg());
49 if (RHSI
->getOpcode() != TargetOpcode::G_CONSTANT
)
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())
62 // Convergent instructions cannot be moved in the CFG.
63 if (MI
.isConvergent() && MI
.getParent() != IntoMI
.getParent())
66 return !MI
.mayLoadOrStore() && !MI
.mayRaiseFPException() &&
67 !MI
.hasUnmodeledSideEffects() && MI
.implicit_operands().empty();