Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/GlobalISel/InstructionSelector.cpp -----------*- C++ -*-==// |
| 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 | //===----------------------------------------------------------------------===// |
| 9 | /// \file |
| 10 | /// This file implements the InstructionSelector class. |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/CodeGen/GlobalISel/InstructionSelector.h" |
| 14 | #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h" |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/GlobalISel/Utils.h" |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineInstr.h" |
Ahmed Bougacha | 7f2d173 | 2017-03-19 16:12:48 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 18 | #include "llvm/IR/Constants.h" |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetInstrInfo.h" |
| 20 | #include "llvm/Target/TargetRegisterInfo.h" |
| 21 | |
| 22 | #define DEBUG_TYPE "instructionselector" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | InstructionSelector::InstructionSelector() {} |
| 27 | |
| 28 | bool InstructionSelector::constrainSelectedInstRegOperands( |
| 29 | MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, |
| 30 | const RegisterBankInfo &RBI) const { |
| 31 | MachineBasicBlock &MBB = *I.getParent(); |
| 32 | MachineFunction &MF = *MBB.getParent(); |
| 33 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 34 | |
| 35 | for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) { |
| 36 | MachineOperand &MO = I.getOperand(OpI); |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 37 | |
Tim Northover | bdf1624 | 2016-10-10 21:50:00 +0000 | [diff] [blame] | 38 | // There's nothing to be done on non-register operands. |
| 39 | if (!MO.isReg()) |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 40 | continue; |
| 41 | |
| 42 | DEBUG(dbgs() << "Converting operand: " << MO << '\n'); |
| 43 | assert(MO.isReg() && "Unsupported non-reg operand"); |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 44 | |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 45 | unsigned Reg = MO.getReg(); |
Ahmed Bougacha | e4c03ab | 2016-08-16 14:37:46 +0000 | [diff] [blame] | 46 | // Physical registers don't need to be constrained. |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 47 | if (TRI.isPhysicalRegister(Reg)) |
Ahmed Bougacha | e4c03ab | 2016-08-16 14:37:46 +0000 | [diff] [blame] | 48 | continue; |
| 49 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 50 | // Register operands with a value of 0 (e.g. predicate operands) don't need |
| 51 | // to be constrained. |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 52 | if (Reg == 0) |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 53 | continue; |
| 54 | |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 55 | // If the operand is a vreg, we should constrain its regclass, and only |
| 56 | // insert COPYs if that's impossible. |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 57 | // constrainOperandRegClass does that for us. |
| 58 | MO.setReg(constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(), |
| 59 | Reg, OpI)); |
Igor Breger | f7359d8 | 2017-02-22 12:25:09 +0000 | [diff] [blame] | 60 | |
| 61 | // Tie uses to defs as indicated in MCInstrDesc. |
| 62 | if (MO.isUse()) { |
| 63 | int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO); |
| 64 | if (DefIdx != -1) |
| 65 | I.tieOperands(DefIdx, OpI); |
| 66 | } |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 67 | } |
| 68 | return true; |
| 69 | } |
Ahmed Bougacha | 7f2d173 | 2017-03-19 16:12:48 +0000 | [diff] [blame] | 70 | |
Ahmed Bougacha | 2d29998f | 2017-03-27 16:35:27 +0000 | [diff] [blame^] | 71 | Optional<int64_t> |
| 72 | InstructionSelector::getConstantVRegVal(unsigned VReg, |
| 73 | const MachineRegisterInfo &MRI) const { |
| 74 | MachineInstr *MI = MRI.getVRegDef(VReg); |
| 75 | if (MI->getOpcode() != TargetOpcode::G_CONSTANT) |
| 76 | return None; |
| 77 | |
| 78 | if (MI->getOperand(1).isImm()) |
| 79 | return MI->getOperand(1).getImm(); |
| 80 | |
| 81 | if (MI->getOperand(1).isCImm() && |
| 82 | MI->getOperand(1).getCImm()->getBitWidth() <= 64) |
| 83 | return MI->getOperand(1).getCImm()->getSExtValue(); |
| 84 | |
| 85 | return None; |
| 86 | } |
| 87 | |
Ahmed Bougacha | 7f2d173 | 2017-03-19 16:12:48 +0000 | [diff] [blame] | 88 | bool InstructionSelector::isOperandImmEqual( |
| 89 | const MachineOperand &MO, int64_t Value, |
| 90 | const MachineRegisterInfo &MRI) const { |
Ahmed Bougacha | 7f2d173 | 2017-03-19 16:12:48 +0000 | [diff] [blame] | 91 | |
Ahmed Bougacha | 2d29998f | 2017-03-27 16:35:27 +0000 | [diff] [blame^] | 92 | if (MO.getReg()) |
| 93 | if (auto VRegVal = getConstantVRegVal(MO.getReg(), MRI)) |
| 94 | return *VRegVal == Value; |
Ahmed Bougacha | 7f2d173 | 2017-03-19 16:12:48 +0000 | [diff] [blame] | 95 | return false; |
| 96 | } |