Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 1 | //===- AArch64InstructionSelector.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 targeting of the InstructionSelector class for |
| 11 | /// AArch64. |
| 12 | /// \todo This should be generated by TableGen. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "AArch64InstructionSelector.h" |
| 16 | #include "AArch64InstrInfo.h" |
| 17 | #include "AArch64RegisterBankInfo.h" |
| 18 | #include "AArch64RegisterInfo.h" |
| 19 | #include "AArch64Subtarget.h" |
| 20 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineInstr.h" |
| 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 25 | #include "llvm/IR/Type.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | |
| 29 | #define DEBUG_TYPE "aarch64-isel" |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
| 33 | #ifndef LLVM_BUILD_GLOBAL_ISEL |
| 34 | #error "You shouldn't build this" |
| 35 | #endif |
| 36 | |
| 37 | AArch64InstructionSelector::AArch64InstructionSelector( |
| 38 | const AArch64Subtarget &STI, const AArch64RegisterBankInfo &RBI) |
| 39 | : InstructionSelector(), TII(*STI.getInstrInfo()), |
| 40 | TRI(*STI.getRegisterInfo()), RBI(RBI) {} |
| 41 | |
Ahmed Bougacha | 59e160a | 2016-08-16 14:37:40 +0000 | [diff] [blame] | 42 | /// Check whether \p I is a currently unsupported binary operation: |
| 43 | /// - it has an unsized type |
| 44 | /// - an operand is not a vreg |
| 45 | /// - all operands are not in the same bank |
| 46 | /// These are checks that should someday live in the verifier, but right now, |
| 47 | /// these are mostly limitations of the aarch64 selector. |
| 48 | static bool unsupportedBinOp(const MachineInstr &I, |
| 49 | const AArch64RegisterBankInfo &RBI, |
| 50 | const MachineRegisterInfo &MRI, |
| 51 | const AArch64RegisterInfo &TRI) { |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 52 | LLT Ty = MRI.getType(I.getOperand(0).getReg()); |
Tim Northover | 32a078a | 2016-09-15 10:09:59 +0000 | [diff] [blame^] | 53 | if (!Ty.isValid()) { |
| 54 | DEBUG(dbgs() << "Generic binop register should be typed\n"); |
Ahmed Bougacha | 59e160a | 2016-08-16 14:37:40 +0000 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
| 58 | const RegisterBank *PrevOpBank = nullptr; |
| 59 | for (auto &MO : I.operands()) { |
| 60 | // FIXME: Support non-register operands. |
| 61 | if (!MO.isReg()) { |
| 62 | DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n"); |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | // FIXME: Can generic operations have physical registers operands? If |
| 67 | // so, this will need to be taught about that, and we'll need to get the |
| 68 | // bank out of the minimal class for the register. |
| 69 | // Either way, this needs to be documented (and possibly verified). |
| 70 | if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 71 | DEBUG(dbgs() << "Generic inst has physical register operand\n"); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI); |
| 76 | if (!OpBank) { |
| 77 | DEBUG(dbgs() << "Generic register has no bank or class\n"); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | if (PrevOpBank && OpBank != PrevOpBank) { |
| 82 | DEBUG(dbgs() << "Generic inst operands have different banks\n"); |
| 83 | return true; |
| 84 | } |
| 85 | PrevOpBank = OpBank; |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 90 | /// Select the AArch64 opcode for the basic binary operation \p GenericOpc |
| 91 | /// (such as G_OR or G_ADD), appropriate for the register bank \p RegBankID |
| 92 | /// and of size \p OpSize. |
| 93 | /// \returns \p GenericOpc if the combination is unsupported. |
| 94 | static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID, |
| 95 | unsigned OpSize) { |
| 96 | switch (RegBankID) { |
| 97 | case AArch64::GPRRegBankID: |
| 98 | switch (OpSize) { |
| 99 | case 32: |
| 100 | switch (GenericOpc) { |
| 101 | case TargetOpcode::G_OR: |
| 102 | return AArch64::ORRWrr; |
Ahmed Bougacha | 6db3cfe | 2016-07-29 16:56:25 +0000 | [diff] [blame] | 103 | case TargetOpcode::G_XOR: |
| 104 | return AArch64::EORWrr; |
Ahmed Bougacha | 61a7928 | 2016-07-28 16:58:31 +0000 | [diff] [blame] | 105 | case TargetOpcode::G_AND: |
| 106 | return AArch64::ANDWrr; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 107 | case TargetOpcode::G_ADD: |
| 108 | return AArch64::ADDWrr; |
Ahmed Bougacha | d7748d6 | 2016-07-28 16:58:35 +0000 | [diff] [blame] | 109 | case TargetOpcode::G_SUB: |
| 110 | return AArch64::SUBWrr; |
Ahmed Bougacha | 2ac5bf9 | 2016-08-16 14:02:47 +0000 | [diff] [blame] | 111 | case TargetOpcode::G_SHL: |
| 112 | return AArch64::LSLVWr; |
| 113 | case TargetOpcode::G_LSHR: |
| 114 | return AArch64::LSRVWr; |
| 115 | case TargetOpcode::G_ASHR: |
| 116 | return AArch64::ASRVWr; |
Ahmed Bougacha | 1d0560b | 2016-08-18 15:17:13 +0000 | [diff] [blame] | 117 | case TargetOpcode::G_SDIV: |
| 118 | return AArch64::SDIVWr; |
| 119 | case TargetOpcode::G_UDIV: |
| 120 | return AArch64::UDIVWr; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 121 | default: |
| 122 | return GenericOpc; |
| 123 | } |
| 124 | case 64: |
| 125 | switch (GenericOpc) { |
| 126 | case TargetOpcode::G_OR: |
| 127 | return AArch64::ORRXrr; |
Ahmed Bougacha | 6db3cfe | 2016-07-29 16:56:25 +0000 | [diff] [blame] | 128 | case TargetOpcode::G_XOR: |
| 129 | return AArch64::EORXrr; |
Ahmed Bougacha | 61a7928 | 2016-07-28 16:58:31 +0000 | [diff] [blame] | 130 | case TargetOpcode::G_AND: |
| 131 | return AArch64::ANDXrr; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 132 | case TargetOpcode::G_ADD: |
| 133 | return AArch64::ADDXrr; |
Ahmed Bougacha | d7748d6 | 2016-07-28 16:58:35 +0000 | [diff] [blame] | 134 | case TargetOpcode::G_SUB: |
| 135 | return AArch64::SUBXrr; |
Ahmed Bougacha | 2ac5bf9 | 2016-08-16 14:02:47 +0000 | [diff] [blame] | 136 | case TargetOpcode::G_SHL: |
| 137 | return AArch64::LSLVXr; |
| 138 | case TargetOpcode::G_LSHR: |
| 139 | return AArch64::LSRVXr; |
| 140 | case TargetOpcode::G_ASHR: |
| 141 | return AArch64::ASRVXr; |
Ahmed Bougacha | 1d0560b | 2016-08-18 15:17:13 +0000 | [diff] [blame] | 142 | case TargetOpcode::G_SDIV: |
| 143 | return AArch64::SDIVXr; |
| 144 | case TargetOpcode::G_UDIV: |
| 145 | return AArch64::UDIVXr; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 146 | default: |
| 147 | return GenericOpc; |
| 148 | } |
| 149 | } |
Ahmed Bougacha | 33e19fe | 2016-08-18 16:05:11 +0000 | [diff] [blame] | 150 | case AArch64::FPRRegBankID: |
| 151 | switch (OpSize) { |
| 152 | case 32: |
| 153 | switch (GenericOpc) { |
| 154 | case TargetOpcode::G_FADD: |
| 155 | return AArch64::FADDSrr; |
| 156 | case TargetOpcode::G_FSUB: |
| 157 | return AArch64::FSUBSrr; |
| 158 | case TargetOpcode::G_FMUL: |
| 159 | return AArch64::FMULSrr; |
| 160 | case TargetOpcode::G_FDIV: |
| 161 | return AArch64::FDIVSrr; |
| 162 | default: |
| 163 | return GenericOpc; |
| 164 | } |
| 165 | case 64: |
| 166 | switch (GenericOpc) { |
| 167 | case TargetOpcode::G_FADD: |
| 168 | return AArch64::FADDDrr; |
| 169 | case TargetOpcode::G_FSUB: |
| 170 | return AArch64::FSUBDrr; |
| 171 | case TargetOpcode::G_FMUL: |
| 172 | return AArch64::FMULDrr; |
| 173 | case TargetOpcode::G_FDIV: |
| 174 | return AArch64::FDIVDrr; |
| 175 | default: |
| 176 | return GenericOpc; |
| 177 | } |
| 178 | } |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 179 | }; |
| 180 | return GenericOpc; |
| 181 | } |
| 182 | |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 183 | /// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc, |
| 184 | /// appropriate for the (value) register bank \p RegBankID and of memory access |
| 185 | /// size \p OpSize. This returns the variant with the base+unsigned-immediate |
| 186 | /// addressing mode (e.g., LDRXui). |
| 187 | /// \returns \p GenericOpc if the combination is unsupported. |
| 188 | static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID, |
| 189 | unsigned OpSize) { |
| 190 | const bool isStore = GenericOpc == TargetOpcode::G_STORE; |
| 191 | switch (RegBankID) { |
| 192 | case AArch64::GPRRegBankID: |
| 193 | switch (OpSize) { |
| 194 | case 32: |
| 195 | return isStore ? AArch64::STRWui : AArch64::LDRWui; |
| 196 | case 64: |
| 197 | return isStore ? AArch64::STRXui : AArch64::LDRXui; |
| 198 | } |
| 199 | }; |
| 200 | return GenericOpc; |
| 201 | } |
| 202 | |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 203 | bool AArch64InstructionSelector::select(MachineInstr &I) const { |
| 204 | assert(I.getParent() && "Instruction should be in a basic block!"); |
| 205 | assert(I.getParent()->getParent() && "Instruction should be in a function!"); |
| 206 | |
| 207 | MachineBasicBlock &MBB = *I.getParent(); |
| 208 | MachineFunction &MF = *MBB.getParent(); |
| 209 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 210 | |
| 211 | // FIXME: Is there *really* nothing to be done here? This assumes that |
| 212 | // no upstream pass introduces things like generic vreg on copies or |
| 213 | // target-specific instructions. |
| 214 | // We should document (and verify) that assumption. |
| 215 | if (!isPreISelGenericOpcode(I.getOpcode())) |
| 216 | return true; |
| 217 | |
| 218 | if (I.getNumOperands() != I.getNumExplicitOperands()) { |
| 219 | DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n"); |
| 220 | return false; |
| 221 | } |
| 222 | |
Tim Northover | 32a078a | 2016-09-15 10:09:59 +0000 | [diff] [blame^] | 223 | LLT Ty = |
| 224 | I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{}; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 225 | |
Ahmed Bougacha | 8550509 | 2016-07-28 17:15:15 +0000 | [diff] [blame] | 226 | switch (I.getOpcode()) { |
| 227 | case TargetOpcode::G_BR: { |
| 228 | I.setDesc(TII.get(AArch64::B)); |
Ahmed Bougacha | 8550509 | 2016-07-28 17:15:15 +0000 | [diff] [blame] | 229 | return true; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Ahmed Bougacha | 0306b5e | 2016-08-16 14:02:42 +0000 | [diff] [blame] | 232 | case TargetOpcode::G_FRAME_INDEX: { |
| 233 | // allocas and G_FRAME_INDEX are only supported in addrspace(0). |
Tim Northover | 5ae8350 | 2016-09-15 09:20:34 +0000 | [diff] [blame] | 234 | if (Ty != LLT::pointer(0, 64)) { |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 235 | DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty |
Tim Northover | 5ae8350 | 2016-09-15 09:20:34 +0000 | [diff] [blame] | 236 | << ", expected: " << LLT::pointer(0, 64) << '\n'); |
Ahmed Bougacha | 0306b5e | 2016-08-16 14:02:42 +0000 | [diff] [blame] | 237 | return false; |
| 238 | } |
| 239 | |
| 240 | I.setDesc(TII.get(AArch64::ADDXri)); |
Ahmed Bougacha | 0306b5e | 2016-08-16 14:02:42 +0000 | [diff] [blame] | 241 | |
| 242 | // MOs for a #0 shifted immediate. |
| 243 | I.addOperand(MachineOperand::CreateImm(0)); |
| 244 | I.addOperand(MachineOperand::CreateImm(0)); |
| 245 | |
| 246 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
| 247 | } |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 248 | case TargetOpcode::G_LOAD: |
| 249 | case TargetOpcode::G_STORE: { |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 250 | LLT MemTy = Ty; |
| 251 | LLT PtrTy = MRI.getType(I.getOperand(1).getReg()); |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 252 | |
Tim Northover | 5ae8350 | 2016-09-15 09:20:34 +0000 | [diff] [blame] | 253 | if (PtrTy != LLT::pointer(0, 64)) { |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 254 | DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy |
Tim Northover | 5ae8350 | 2016-09-15 09:20:34 +0000 | [diff] [blame] | 255 | << ", expected: " << LLT::pointer(0, 64) << '\n'); |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 256 | return false; |
| 257 | } |
| 258 | |
| 259 | #ifndef NDEBUG |
| 260 | // Sanity-check the pointer register. |
| 261 | const unsigned PtrReg = I.getOperand(1).getReg(); |
| 262 | const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI); |
| 263 | assert(PtrRB.getID() == AArch64::GPRRegBankID && |
| 264 | "Load/Store pointer operand isn't a GPR"); |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 265 | assert(MRI.getType(PtrReg).isPointer() && |
| 266 | "Load/Store pointer operand isn't a pointer"); |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 267 | #endif |
| 268 | |
| 269 | const unsigned ValReg = I.getOperand(0).getReg(); |
| 270 | const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI); |
| 271 | |
| 272 | const unsigned NewOpc = |
| 273 | selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits()); |
| 274 | if (NewOpc == I.getOpcode()) |
| 275 | return false; |
| 276 | |
| 277 | I.setDesc(TII.get(NewOpc)); |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 278 | |
| 279 | I.addOperand(MachineOperand::CreateImm(0)); |
| 280 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
| 281 | } |
| 282 | |
Ahmed Bougacha | e4c03ab | 2016-08-16 14:37:46 +0000 | [diff] [blame] | 283 | case TargetOpcode::G_MUL: { |
| 284 | // Reject the various things we don't support yet. |
| 285 | if (unsupportedBinOp(I, RBI, MRI, TRI)) |
| 286 | return false; |
| 287 | |
| 288 | const unsigned DefReg = I.getOperand(0).getReg(); |
| 289 | const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI); |
| 290 | |
| 291 | if (RB.getID() != AArch64::GPRRegBankID) { |
| 292 | DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n"); |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | unsigned ZeroReg; |
| 297 | unsigned NewOpc; |
| 298 | if (Ty == LLT::scalar(32)) { |
| 299 | NewOpc = AArch64::MADDWrrr; |
| 300 | ZeroReg = AArch64::WZR; |
| 301 | } else if (Ty == LLT::scalar(64)) { |
| 302 | NewOpc = AArch64::MADDXrrr; |
| 303 | ZeroReg = AArch64::XZR; |
| 304 | } else { |
| 305 | DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: " |
| 306 | << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n'); |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | I.setDesc(TII.get(NewOpc)); |
Ahmed Bougacha | e4c03ab | 2016-08-16 14:37:46 +0000 | [diff] [blame] | 311 | |
| 312 | I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false)); |
| 313 | |
| 314 | // Now that we selected an opcode, we need to constrain the register |
| 315 | // operands to use appropriate classes. |
| 316 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
| 317 | } |
| 318 | |
Ahmed Bougacha | 33e19fe | 2016-08-18 16:05:11 +0000 | [diff] [blame] | 319 | case TargetOpcode::G_FADD: |
| 320 | case TargetOpcode::G_FSUB: |
| 321 | case TargetOpcode::G_FMUL: |
| 322 | case TargetOpcode::G_FDIV: |
| 323 | |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 324 | case TargetOpcode::G_OR: |
Ahmed Bougacha | 6db3cfe | 2016-07-29 16:56:25 +0000 | [diff] [blame] | 325 | case TargetOpcode::G_XOR: |
Ahmed Bougacha | 61a7928 | 2016-07-28 16:58:31 +0000 | [diff] [blame] | 326 | case TargetOpcode::G_AND: |
Ahmed Bougacha | 2ac5bf9 | 2016-08-16 14:02:47 +0000 | [diff] [blame] | 327 | case TargetOpcode::G_SHL: |
| 328 | case TargetOpcode::G_LSHR: |
| 329 | case TargetOpcode::G_ASHR: |
Ahmed Bougacha | 1d0560b | 2016-08-18 15:17:13 +0000 | [diff] [blame] | 330 | case TargetOpcode::G_SDIV: |
| 331 | case TargetOpcode::G_UDIV: |
Ahmed Bougacha | d7748d6 | 2016-07-28 16:58:35 +0000 | [diff] [blame] | 332 | case TargetOpcode::G_ADD: |
| 333 | case TargetOpcode::G_SUB: { |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 334 | // Reject the various things we don't support yet. |
Ahmed Bougacha | 59e160a | 2016-08-16 14:37:40 +0000 | [diff] [blame] | 335 | if (unsupportedBinOp(I, RBI, MRI, TRI)) |
| 336 | return false; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 337 | |
Ahmed Bougacha | 59e160a | 2016-08-16 14:37:40 +0000 | [diff] [blame] | 338 | const unsigned OpSize = Ty.getSizeInBits(); |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 339 | |
| 340 | const unsigned DefReg = I.getOperand(0).getReg(); |
| 341 | const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI); |
| 342 | |
| 343 | const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize); |
| 344 | if (NewOpc == I.getOpcode()) |
| 345 | return false; |
| 346 | |
| 347 | I.setDesc(TII.get(NewOpc)); |
| 348 | // FIXME: Should the type be always reset in setDesc? |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 349 | |
| 350 | // Now that we selected an opcode, we need to constrain the register |
| 351 | // operands to use appropriate classes. |
| 352 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return false; |
| 357 | } |