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/MachineBasicBlock.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/CodeGen/MachineOperand.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/TargetRegisterInfo.h"
22 #include "llvm/MC/MCInstrDesc.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
27 #define DEBUG_TYPE "instructionselector"
31 InstructionSelector::MatcherState::MatcherState(unsigned MaxRenderers
)
32 : Renderers(MaxRenderers
), MIs() {}
34 InstructionSelector::InstructionSelector() = default;
36 bool InstructionSelector::isOperandImmEqual(
37 const MachineOperand
&MO
, int64_t Value
,
38 const MachineRegisterInfo
&MRI
) const {
39 if (MO
.isReg() && MO
.getReg())
40 if (auto VRegVal
= getConstantVRegValWithLookThrough(MO
.getReg(), MRI
))
41 return VRegVal
->Value
.getSExtValue() == Value
;
45 bool InstructionSelector::isBaseWithConstantOffset(
46 const MachineOperand
&Root
, const MachineRegisterInfo
&MRI
) const {
50 MachineInstr
*RootI
= MRI
.getVRegDef(Root
.getReg());
51 if (RootI
->getOpcode() != TargetOpcode::G_PTR_ADD
)
54 MachineOperand
&RHS
= RootI
->getOperand(2);
55 MachineInstr
*RHSI
= MRI
.getVRegDef(RHS
.getReg());
56 if (RHSI
->getOpcode() != TargetOpcode::G_CONSTANT
)
62 bool InstructionSelector::isObviouslySafeToFold(MachineInstr
&MI
,
63 MachineInstr
&IntoMI
) const {
64 // Immediate neighbours are already folded.
65 if (MI
.getParent() == IntoMI
.getParent() &&
66 std::next(MI
.getIterator()) == IntoMI
.getIterator())
69 return !MI
.mayLoadOrStore() && !MI
.mayRaiseFPException() &&
70 !MI
.hasUnmodeledSideEffects() && MI
.implicit_operands().empty();