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" |
| 21 | #include "llvm/MC/MCInstrDesc.h" |
| 22 | #include "llvm/Support/Debug.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetRegisterInfo.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 25 | #include <cassert> |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 26 | |
| 27 | #define DEBUG_TYPE "instructionselector" |
| 28 | |
| 29 | using namespace llvm; |
| 30 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 31 | InstructionSelector::MatcherState::MatcherState(unsigned MaxRenderers) |
Daniel Sanders | df39cba | 2017-10-15 18:22:54 +0000 | [diff] [blame^] | 32 | : Renderers(MaxRenderers), MIs() {} |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 33 | |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 34 | InstructionSelector::InstructionSelector() = default; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 35 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 36 | bool InstructionSelector::constrainOperandRegToRegClass( |
| 37 | MachineInstr &I, unsigned OpIdx, const TargetRegisterClass &RC, |
| 38 | const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, |
| 39 | const RegisterBankInfo &RBI) const { |
| 40 | MachineBasicBlock &MBB = *I.getParent(); |
| 41 | MachineFunction &MF = *MBB.getParent(); |
| 42 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 43 | |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 44 | return |
| 45 | constrainRegToClass(MRI, TII, RBI, I, I.getOperand(OpIdx).getReg(), RC); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 48 | bool InstructionSelector::constrainSelectedInstRegOperands( |
| 49 | MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, |
| 50 | const RegisterBankInfo &RBI) const { |
| 51 | MachineBasicBlock &MBB = *I.getParent(); |
| 52 | MachineFunction &MF = *MBB.getParent(); |
| 53 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 54 | |
| 55 | for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) { |
| 56 | MachineOperand &MO = I.getOperand(OpI); |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 57 | |
Tim Northover | bdf1624 | 2016-10-10 21:50:00 +0000 | [diff] [blame] | 58 | // There's nothing to be done on non-register operands. |
| 59 | if (!MO.isReg()) |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 60 | continue; |
| 61 | |
| 62 | DEBUG(dbgs() << "Converting operand: " << MO << '\n'); |
| 63 | assert(MO.isReg() && "Unsupported non-reg operand"); |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 64 | |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 65 | unsigned Reg = MO.getReg(); |
Ahmed Bougacha | e4c03ab | 2016-08-16 14:37:46 +0000 | [diff] [blame] | 66 | // Physical registers don't need to be constrained. |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 67 | if (TRI.isPhysicalRegister(Reg)) |
Ahmed Bougacha | e4c03ab | 2016-08-16 14:37:46 +0000 | [diff] [blame] | 68 | continue; |
| 69 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 70 | // Register operands with a value of 0 (e.g. predicate operands) don't need |
| 71 | // to be constrained. |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 72 | if (Reg == 0) |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 73 | continue; |
| 74 | |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 75 | // If the operand is a vreg, we should constrain its regclass, and only |
| 76 | // insert COPYs if that's impossible. |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 77 | // constrainOperandRegClass does that for us. |
| 78 | MO.setReg(constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(), |
| 79 | Reg, OpI)); |
Igor Breger | f7359d8 | 2017-02-22 12:25:09 +0000 | [diff] [blame] | 80 | |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 81 | // Tie uses to defs as indicated in MCInstrDesc if this hasn't already been |
| 82 | // done. |
Igor Breger | f7359d8 | 2017-02-22 12:25:09 +0000 | [diff] [blame] | 83 | if (MO.isUse()) { |
| 84 | int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO); |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 85 | if (DefIdx != -1 && !I.isRegTiedToUseOperand(DefIdx)) |
Igor Breger | f7359d8 | 2017-02-22 12:25:09 +0000 | [diff] [blame] | 86 | I.tieOperands(DefIdx, OpI); |
| 87 | } |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 88 | } |
| 89 | return true; |
| 90 | } |
Ahmed Bougacha | 7f2d173 | 2017-03-19 16:12:48 +0000 | [diff] [blame] | 91 | |
| 92 | bool InstructionSelector::isOperandImmEqual( |
| 93 | const MachineOperand &MO, int64_t Value, |
| 94 | const MachineRegisterInfo &MRI) const { |
Daniel Sanders | 89e9308 | 2017-05-18 10:33:36 +0000 | [diff] [blame] | 95 | if (MO.isReg() && MO.getReg()) |
Ahmed Bougacha | 2d29998f | 2017-03-27 16:35:27 +0000 | [diff] [blame] | 96 | if (auto VRegVal = getConstantVRegVal(MO.getReg(), MRI)) |
| 97 | return *VRegVal == Value; |
Ahmed Bougacha | 7f2d173 | 2017-03-19 16:12:48 +0000 | [diff] [blame] | 98 | return false; |
| 99 | } |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 100 | |
| 101 | bool InstructionSelector::isObviouslySafeToFold(MachineInstr &MI) const { |
| 102 | return !MI.mayLoadOrStore() && !MI.hasUnmodeledSideEffects() && |
| 103 | MI.implicit_operands().begin() == MI.implicit_operands().end(); |
| 104 | } |