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 | //===----------------------------------------------------------------------===// |
| 9 | /// \file |
| 10 | /// This file implements the InstructionSelector class. |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/CodeGen/GlobalISel/InstructionSelector.h" |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/GlobalISel/Utils.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 16 | #include "llvm/CodeGen/MachineFunction.h" |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineInstr.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineOperand.h" |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame^] | 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCInstrDesc.h" |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame^] | 22 | #include "llvm/IR/Constants.h" |
| 23 | #include "llvm/Target/TargetInstrInfo.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetRegisterInfo.h" |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 27 | #include <cassert> |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 28 | |
| 29 | #define DEBUG_TYPE "instructionselector" |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 33 | InstructionSelector::MatcherState::MatcherState(unsigned MaxRenderers) |
| 34 | : Renderers(MaxRenderers, nullptr), MIs() {} |
| 35 | |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 36 | InstructionSelector::InstructionSelector() = default; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 37 | |
Daniel Sanders | d93a35a | 2017-07-05 09:39:33 +0000 | [diff] [blame^] | 38 | void InstructionSelector::executeEmitTable(NewMIVector &OutMIs, |
| 39 | MatcherState &State, |
| 40 | const int64_t *EmitTable, |
| 41 | const TargetInstrInfo &TII, |
| 42 | const TargetRegisterInfo &TRI, |
| 43 | const RegisterBankInfo &RBI) const { |
| 44 | const int64_t *Command = EmitTable; |
| 45 | while (true) { |
| 46 | switch (*Command++) { |
| 47 | case GIR_MutateOpcode: { |
| 48 | int64_t OldInsnID = *Command++; |
| 49 | int64_t NewInsnID = *Command++; |
| 50 | int64_t NewOpcode = *Command++; |
| 51 | assert((size_t)NewInsnID == OutMIs.size() && |
| 52 | "Expected to store MIs in order"); |
| 53 | OutMIs.push_back( |
| 54 | MachineInstrBuilder(*State.MIs[OldInsnID]->getParent()->getParent(), |
| 55 | State.MIs[OldInsnID])); |
| 56 | OutMIs[NewInsnID]->setDesc(TII.get(NewOpcode)); |
| 57 | DEBUG(dbgs() << "GIR_MutateOpcode(OutMIs[" << NewInsnID << "], MIs[" |
| 58 | << OldInsnID << "], " << NewOpcode << ")\n"); |
| 59 | break; |
| 60 | } |
| 61 | case GIR_BuildMI: { |
| 62 | int64_t InsnID = *Command++; |
| 63 | int64_t Opcode = *Command++; |
| 64 | assert((size_t)InsnID == OutMIs.size() && |
| 65 | "Expected to store MIs in order"); |
| 66 | OutMIs.push_back(BuildMI(*State.MIs[0]->getParent(), State.MIs[0], |
| 67 | State.MIs[0]->getDebugLoc(), TII.get(Opcode))); |
| 68 | DEBUG(dbgs() << "GIR_BuildMI(OutMIs[" << InsnID << "], " << Opcode |
| 69 | << ")\n"); |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | case GIR_Copy: { |
| 74 | int64_t NewInsnID = *Command++; |
| 75 | int64_t OldInsnID = *Command++; |
| 76 | int64_t OpIdx = *Command++; |
| 77 | assert(OutMIs[NewInsnID] && "Attempted to add to undefined instruction"); |
| 78 | OutMIs[NewInsnID].add(State.MIs[OldInsnID]->getOperand(OpIdx)); |
| 79 | DEBUG(dbgs() << "GIR_Copy(OutMIs[" << NewInsnID << "], MIs[" << OldInsnID |
| 80 | << "], " << OpIdx << ")\n"); |
| 81 | break; |
| 82 | } |
| 83 | case GIR_CopySubReg: { |
| 84 | int64_t NewInsnID = *Command++; |
| 85 | int64_t OldInsnID = *Command++; |
| 86 | int64_t OpIdx = *Command++; |
| 87 | int64_t SubRegIdx = *Command++; |
| 88 | assert(OutMIs[NewInsnID] && "Attempted to add to undefined instruction"); |
| 89 | OutMIs[NewInsnID].addReg(State.MIs[OldInsnID]->getOperand(OpIdx).getReg(), |
| 90 | 0, SubRegIdx); |
| 91 | DEBUG(dbgs() << "GIR_CopySubReg(OutMIs[" << NewInsnID << "], MIs[" |
| 92 | << OldInsnID << "], " << OpIdx << ", " << SubRegIdx |
| 93 | << ")\n"); |
| 94 | break; |
| 95 | } |
| 96 | case GIR_AddImplicitDef: { |
| 97 | int64_t InsnID = *Command++; |
| 98 | int64_t RegNum = *Command++; |
| 99 | assert(OutMIs[InsnID] && "Attempted to add to undefined instruction"); |
| 100 | OutMIs[InsnID].addDef(RegNum, RegState::Implicit); |
| 101 | DEBUG(dbgs() << "GIR_AddImplicitDef(OutMIs[" << InsnID << "], " << RegNum |
| 102 | << ")\n"); |
| 103 | break; |
| 104 | } |
| 105 | case GIR_AddImplicitUse: { |
| 106 | int64_t InsnID = *Command++; |
| 107 | int64_t RegNum = *Command++; |
| 108 | assert(OutMIs[InsnID] && "Attempted to add to undefined instruction"); |
| 109 | OutMIs[InsnID].addUse(RegNum, RegState::Implicit); |
| 110 | DEBUG(dbgs() << "GIR_AddImplicitUse(OutMIs[" << InsnID << "], " << RegNum |
| 111 | << ")\n"); |
| 112 | break; |
| 113 | } |
| 114 | case GIR_AddRegister: { |
| 115 | int64_t InsnID = *Command++; |
| 116 | int64_t RegNum = *Command++; |
| 117 | assert(OutMIs[InsnID] && "Attempted to add to undefined instruction"); |
| 118 | OutMIs[InsnID].addReg(RegNum); |
| 119 | DEBUG(dbgs() << "GIR_AddRegister(OutMIs[" << InsnID << "], " << RegNum |
| 120 | << ")\n"); |
| 121 | break; |
| 122 | } |
| 123 | case GIR_AddImm: { |
| 124 | int64_t InsnID = *Command++; |
| 125 | int64_t Imm = *Command++; |
| 126 | assert(OutMIs[InsnID] && "Attempted to add to undefined instruction"); |
| 127 | OutMIs[InsnID].addImm(Imm); |
| 128 | DEBUG(dbgs() << "GIR_AddImm(OutMIs[" << InsnID << "], " << Imm << ")\n"); |
| 129 | break; |
| 130 | } |
| 131 | case GIR_ComplexRenderer: { |
| 132 | int64_t InsnID = *Command++; |
| 133 | int64_t RendererID = *Command++; |
| 134 | assert(OutMIs[InsnID] && "Attempted to add to undefined instruction"); |
| 135 | State.Renderers[RendererID](OutMIs[InsnID]); |
| 136 | DEBUG(dbgs() << "GIR_ComplexRenderer(OutMIs[" << InsnID << "], " |
| 137 | << RendererID << ")\n"); |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | case GIR_ConstrainOperandRC: { |
| 142 | int64_t InsnID = *Command++; |
| 143 | int64_t OpIdx = *Command++; |
| 144 | int64_t RCEnum = *Command++; |
| 145 | assert(OutMIs[InsnID] && "Attempted to add to undefined instruction"); |
| 146 | constrainOperandRegToRegClass(*OutMIs[InsnID].getInstr(), OpIdx, |
| 147 | *TRI.getRegClass(RCEnum), TII, TRI, RBI); |
| 148 | DEBUG(dbgs() << "GIR_ConstrainOperandRC(OutMIs[" << InsnID << "], " |
| 149 | << OpIdx << ", " << RCEnum << ")\n"); |
| 150 | break; |
| 151 | } |
| 152 | case GIR_ConstrainSelectedInstOperands: { |
| 153 | int64_t InsnID = *Command++; |
| 154 | assert(OutMIs[InsnID] && "Attempted to add to undefined instruction"); |
| 155 | constrainSelectedInstRegOperands(*OutMIs[InsnID].getInstr(), TII, TRI, |
| 156 | RBI); |
| 157 | DEBUG(dbgs() << "GIR_ConstrainSelectedInstOperands(OutMIs[" << InsnID |
| 158 | << "])\n"); |
| 159 | break; |
| 160 | } |
| 161 | case GIR_MergeMemOperands: { |
| 162 | int64_t InsnID = *Command++; |
| 163 | assert(OutMIs[InsnID] && "Attempted to add to undefined instruction"); |
| 164 | for (const auto *FromMI : State.MIs) |
| 165 | for (const auto &MMO : FromMI->memoperands()) |
| 166 | OutMIs[InsnID].addMemOperand(MMO); |
| 167 | DEBUG(dbgs() << "GIR_MergeMemOperands(OutMIs[" << InsnID << "])\n"); |
| 168 | break; |
| 169 | } |
| 170 | case GIR_EraseFromParent: { |
| 171 | int64_t InsnID = *Command++; |
| 172 | assert(State.MIs[InsnID] && "Attempted to erase an undefined instruction"); |
| 173 | State.MIs[InsnID]->eraseFromParent(); |
| 174 | DEBUG(dbgs() << "GIR_EraseFromParent(MIs[" << InsnID << "])\n"); |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | case GIR_Done: |
| 179 | DEBUG(dbgs() << "GIR_Done"); |
| 180 | return; |
| 181 | default: |
| 182 | llvm_unreachable("Unexpected command"); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 187 | bool InstructionSelector::constrainOperandRegToRegClass( |
| 188 | MachineInstr &I, unsigned OpIdx, const TargetRegisterClass &RC, |
| 189 | const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, |
| 190 | const RegisterBankInfo &RBI) const { |
| 191 | MachineBasicBlock &MBB = *I.getParent(); |
| 192 | MachineFunction &MF = *MBB.getParent(); |
| 193 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 194 | |
Eugene Zelenko | 76bf48d | 2017-06-26 22:44:03 +0000 | [diff] [blame] | 195 | return |
| 196 | constrainRegToClass(MRI, TII, RBI, I, I.getOperand(OpIdx).getReg(), RC); |
Daniel Sanders | a6e2ceb | 2017-06-20 12:36:34 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 199 | bool InstructionSelector::constrainSelectedInstRegOperands( |
| 200 | MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, |
| 201 | const RegisterBankInfo &RBI) const { |
| 202 | MachineBasicBlock &MBB = *I.getParent(); |
| 203 | MachineFunction &MF = *MBB.getParent(); |
| 204 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 205 | |
| 206 | for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) { |
| 207 | MachineOperand &MO = I.getOperand(OpI); |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 208 | |
Tim Northover | bdf1624 | 2016-10-10 21:50:00 +0000 | [diff] [blame] | 209 | // There's nothing to be done on non-register operands. |
| 210 | if (!MO.isReg()) |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 211 | continue; |
| 212 | |
| 213 | DEBUG(dbgs() << "Converting operand: " << MO << '\n'); |
| 214 | assert(MO.isReg() && "Unsupported non-reg operand"); |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 215 | |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 216 | unsigned Reg = MO.getReg(); |
Ahmed Bougacha | e4c03ab | 2016-08-16 14:37:46 +0000 | [diff] [blame] | 217 | // Physical registers don't need to be constrained. |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 218 | if (TRI.isPhysicalRegister(Reg)) |
Ahmed Bougacha | e4c03ab | 2016-08-16 14:37:46 +0000 | [diff] [blame] | 219 | continue; |
| 220 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 221 | // Register operands with a value of 0 (e.g. predicate operands) don't need |
| 222 | // to be constrained. |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 223 | if (Reg == 0) |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 224 | continue; |
| 225 | |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 226 | // If the operand is a vreg, we should constrain its regclass, and only |
| 227 | // insert COPYs if that's impossible. |
Quentin Colombet | b4e7118 | 2016-12-22 21:56:19 +0000 | [diff] [blame] | 228 | // constrainOperandRegClass does that for us. |
| 229 | MO.setReg(constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(), |
| 230 | Reg, OpI)); |
Igor Breger | f7359d8 | 2017-02-22 12:25:09 +0000 | [diff] [blame] | 231 | |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 232 | // Tie uses to defs as indicated in MCInstrDesc if this hasn't already been |
| 233 | // done. |
Igor Breger | f7359d8 | 2017-02-22 12:25:09 +0000 | [diff] [blame] | 234 | if (MO.isUse()) { |
| 235 | int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO); |
Daniel Sanders | e9fdba3 | 2017-04-29 17:30:09 +0000 | [diff] [blame] | 236 | if (DefIdx != -1 && !I.isRegTiedToUseOperand(DefIdx)) |
Igor Breger | f7359d8 | 2017-02-22 12:25:09 +0000 | [diff] [blame] | 237 | I.tieOperands(DefIdx, OpI); |
| 238 | } |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 239 | } |
| 240 | return true; |
| 241 | } |
Ahmed Bougacha | 7f2d173 | 2017-03-19 16:12:48 +0000 | [diff] [blame] | 242 | |
| 243 | bool InstructionSelector::isOperandImmEqual( |
| 244 | const MachineOperand &MO, int64_t Value, |
| 245 | const MachineRegisterInfo &MRI) const { |
Daniel Sanders | 89e9308 | 2017-05-18 10:33:36 +0000 | [diff] [blame] | 246 | if (MO.isReg() && MO.getReg()) |
Ahmed Bougacha | 2d29998f | 2017-03-27 16:35:27 +0000 | [diff] [blame] | 247 | if (auto VRegVal = getConstantVRegVal(MO.getReg(), MRI)) |
| 248 | return *VRegVal == Value; |
Ahmed Bougacha | 7f2d173 | 2017-03-19 16:12:48 +0000 | [diff] [blame] | 249 | return false; |
| 250 | } |
Daniel Sanders | bee5739 | 2017-04-04 13:25:23 +0000 | [diff] [blame] | 251 | |
| 252 | bool InstructionSelector::isObviouslySafeToFold(MachineInstr &MI) const { |
| 253 | return !MI.mayLoadOrStore() && !MI.hasUnmodeledSideEffects() && |
| 254 | MI.implicit_operands().begin() == MI.implicit_operands().end(); |
| 255 | } |