Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/GlobalISel/InstructionSelector.cpp --------------------===// |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 9 | // |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 10 | /// \file |
| 11 | /// This file implements the InstructionSelector class. |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 12 | // |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/CodeGen/GlobalISel/InstructionSelector.h" |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/GlobalISel/Utils.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstr.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineOperand.h" |
Daniel Sanders | ea8711b | 2017-10-16 03:36:29 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInstrDesc.h" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 26 | #include <cassert> |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 27 | |
| 28 | #define DEBUG_TYPE "instructionselector" |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 32 | InstructionSelector::MatcherState::MatcherState(unsigned MaxRenderers) |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame] | 33 | : Renderers(MaxRenderers), MIs() {} |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 34 | |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 35 | InstructionSelector::InstructionSelector() = default; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 36 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 37 | bool InstructionSelector::constrainOperandRegToRegClass( |
| 38 | MachineInstr &I, unsigned OpIdx, const TargetRegisterClass &RC, |
| 39 | const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, |
| 40 | const RegisterBankInfo &RBI) const { |
| 41 | MachineBasicBlock &MBB = *I.getParent(); |
| 42 | MachineFunction &MF = *MBB.getParent(); |
| 43 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 44 | |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 45 | return |
| 46 | constrainRegToClass(MRI, TII, RBI, I, I.getOperand(OpIdx).getReg(), RC); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Ahmed Bougacha | 7f2d173 | 2017-03-19 16:12:48 +0000 | [diff] [blame] | 49 | bool InstructionSelector::isOperandImmEqual( |
| 50 | const MachineOperand &MO, int64_t Value, |
| 51 | const MachineRegisterInfo &MRI) const { |
Daniel Sanders | 89e9308 | 2017-05-18 10:33:36 +0000 | [diff] [blame] | 52 | if (MO.isReg() && MO.getReg()) |
Ahmed Bougacha | 2d29998f | 2017-03-27 16:35:27 +0000 | [diff] [blame] | 53 | if (auto VRegVal = getConstantVRegVal(MO.getReg(), MRI)) |
| 54 | return *VRegVal == Value; |
Ahmed Bougacha | 7f2d173 | 2017-03-19 16:12:48 +0000 | [diff] [blame] | 55 | return false; |
| 56 | } |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 57 | |
Daniel Sanders | ea8711b | 2017-10-16 03:36:29 +0000 | [diff] [blame] | 58 | bool InstructionSelector::isBaseWithConstantOffset( |
| 59 | const MachineOperand &Root, const MachineRegisterInfo &MRI) const { |
| 60 | if (!Root.isReg()) |
| 61 | return false; |
| 62 | |
| 63 | MachineInstr *RootI = MRI.getVRegDef(Root.getReg()); |
| 64 | if (RootI->getOpcode() != TargetOpcode::G_GEP) |
| 65 | return false; |
| 66 | |
| 67 | MachineOperand &RHS = RootI->getOperand(2); |
| 68 | MachineInstr *RHSI = MRI.getVRegDef(RHS.getReg()); |
| 69 | if (RHSI->getOpcode() != TargetOpcode::G_CONSTANT) |
| 70 | return false; |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
Daniel Sanders | 7e52367 | 2017-11-11 03:23:44 +0000 | [diff] [blame] | 75 | bool InstructionSelector::isObviouslySafeToFold(MachineInstr &MI, |
| 76 | MachineInstr &IntoMI) const { |
| 77 | // Immediate neighbours are already folded. |
| 78 | if (MI.getParent() == IntoMI.getParent() && |
| 79 | std::next(MI.getIterator()) == IntoMI.getIterator()) |
| 80 | return true; |
| 81 | |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 82 | return !MI.mayLoadOrStore() && !MI.hasUnmodeledSideEffects() && |
| 83 | MI.implicit_operands().begin() == MI.implicit_operands().end(); |
| 84 | } |