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" |
Tim Northover | bdf1624 | 2016-10-10 21:50:00 +0000 | [diff] [blame] | 20 | #include "AArch64TargetMachine.h" |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 22 | #include "llvm/CodeGen/MachineFunction.h" |
| 23 | #include "llvm/CodeGen/MachineInstr.h" |
| 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 25 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 26 | #include "llvm/IR/Type.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | |
| 30 | #define DEBUG_TYPE "aarch64-isel" |
| 31 | |
| 32 | using namespace llvm; |
| 33 | |
| 34 | #ifndef LLVM_BUILD_GLOBAL_ISEL |
| 35 | #error "You shouldn't build this" |
| 36 | #endif |
| 37 | |
| 38 | AArch64InstructionSelector::AArch64InstructionSelector( |
Tim Northover | bdf1624 | 2016-10-10 21:50:00 +0000 | [diff] [blame] | 39 | const AArch64TargetMachine &TM, const AArch64Subtarget &STI, |
| 40 | const AArch64RegisterBankInfo &RBI) |
| 41 | : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()), |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 42 | TRI(*STI.getRegisterInfo()), RBI(RBI) {} |
| 43 | |
Ahmed Bougacha | 59e160a | 2016-08-16 14:37:40 +0000 | [diff] [blame] | 44 | /// Check whether \p I is a currently unsupported binary operation: |
| 45 | /// - it has an unsized type |
| 46 | /// - an operand is not a vreg |
| 47 | /// - all operands are not in the same bank |
| 48 | /// These are checks that should someday live in the verifier, but right now, |
| 49 | /// these are mostly limitations of the aarch64 selector. |
| 50 | static bool unsupportedBinOp(const MachineInstr &I, |
| 51 | const AArch64RegisterBankInfo &RBI, |
| 52 | const MachineRegisterInfo &MRI, |
| 53 | const AArch64RegisterInfo &TRI) { |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 54 | LLT Ty = MRI.getType(I.getOperand(0).getReg()); |
Tim Northover | 32a078a | 2016-09-15 10:09:59 +0000 | [diff] [blame] | 55 | if (!Ty.isValid()) { |
| 56 | DEBUG(dbgs() << "Generic binop register should be typed\n"); |
Ahmed Bougacha | 59e160a | 2016-08-16 14:37:40 +0000 | [diff] [blame] | 57 | return true; |
| 58 | } |
| 59 | |
| 60 | const RegisterBank *PrevOpBank = nullptr; |
| 61 | for (auto &MO : I.operands()) { |
| 62 | // FIXME: Support non-register operands. |
| 63 | if (!MO.isReg()) { |
| 64 | DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n"); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | // FIXME: Can generic operations have physical registers operands? If |
| 69 | // so, this will need to be taught about that, and we'll need to get the |
| 70 | // bank out of the minimal class for the register. |
| 71 | // Either way, this needs to be documented (and possibly verified). |
| 72 | if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 73 | DEBUG(dbgs() << "Generic inst has physical register operand\n"); |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI); |
| 78 | if (!OpBank) { |
| 79 | DEBUG(dbgs() << "Generic register has no bank or class\n"); |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | if (PrevOpBank && OpBank != PrevOpBank) { |
| 84 | DEBUG(dbgs() << "Generic inst operands have different banks\n"); |
| 85 | return true; |
| 86 | } |
| 87 | PrevOpBank = OpBank; |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 92 | /// Select the AArch64 opcode for the basic binary operation \p GenericOpc |
| 93 | /// (such as G_OR or G_ADD), appropriate for the register bank \p RegBankID |
| 94 | /// and of size \p OpSize. |
| 95 | /// \returns \p GenericOpc if the combination is unsupported. |
| 96 | static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID, |
| 97 | unsigned OpSize) { |
| 98 | switch (RegBankID) { |
| 99 | case AArch64::GPRRegBankID: |
| 100 | switch (OpSize) { |
| 101 | case 32: |
| 102 | switch (GenericOpc) { |
| 103 | case TargetOpcode::G_OR: |
| 104 | return AArch64::ORRWrr; |
Ahmed Bougacha | 6db3cfe | 2016-07-29 16:56:25 +0000 | [diff] [blame] | 105 | case TargetOpcode::G_XOR: |
| 106 | return AArch64::EORWrr; |
Ahmed Bougacha | 61a7928 | 2016-07-28 16:58:31 +0000 | [diff] [blame] | 107 | case TargetOpcode::G_AND: |
| 108 | return AArch64::ANDWrr; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 109 | case TargetOpcode::G_ADD: |
| 110 | return AArch64::ADDWrr; |
Ahmed Bougacha | d7748d6 | 2016-07-28 16:58:35 +0000 | [diff] [blame] | 111 | case TargetOpcode::G_SUB: |
| 112 | return AArch64::SUBWrr; |
Ahmed Bougacha | 2ac5bf9 | 2016-08-16 14:02:47 +0000 | [diff] [blame] | 113 | case TargetOpcode::G_SHL: |
| 114 | return AArch64::LSLVWr; |
| 115 | case TargetOpcode::G_LSHR: |
| 116 | return AArch64::LSRVWr; |
| 117 | case TargetOpcode::G_ASHR: |
| 118 | return AArch64::ASRVWr; |
Ahmed Bougacha | 1d0560b | 2016-08-18 15:17:13 +0000 | [diff] [blame] | 119 | case TargetOpcode::G_SDIV: |
| 120 | return AArch64::SDIVWr; |
| 121 | case TargetOpcode::G_UDIV: |
| 122 | return AArch64::UDIVWr; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 123 | default: |
| 124 | return GenericOpc; |
| 125 | } |
| 126 | case 64: |
| 127 | switch (GenericOpc) { |
| 128 | case TargetOpcode::G_OR: |
| 129 | return AArch64::ORRXrr; |
Ahmed Bougacha | 6db3cfe | 2016-07-29 16:56:25 +0000 | [diff] [blame] | 130 | case TargetOpcode::G_XOR: |
| 131 | return AArch64::EORXrr; |
Ahmed Bougacha | 61a7928 | 2016-07-28 16:58:31 +0000 | [diff] [blame] | 132 | case TargetOpcode::G_AND: |
| 133 | return AArch64::ANDXrr; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 134 | case TargetOpcode::G_ADD: |
Tim Northover | 2fda4b0 | 2016-10-10 21:49:49 +0000 | [diff] [blame] | 135 | case TargetOpcode::G_GEP: |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 136 | return AArch64::ADDXrr; |
Ahmed Bougacha | d7748d6 | 2016-07-28 16:58:35 +0000 | [diff] [blame] | 137 | case TargetOpcode::G_SUB: |
| 138 | return AArch64::SUBXrr; |
Ahmed Bougacha | 2ac5bf9 | 2016-08-16 14:02:47 +0000 | [diff] [blame] | 139 | case TargetOpcode::G_SHL: |
| 140 | return AArch64::LSLVXr; |
| 141 | case TargetOpcode::G_LSHR: |
| 142 | return AArch64::LSRVXr; |
| 143 | case TargetOpcode::G_ASHR: |
| 144 | return AArch64::ASRVXr; |
Ahmed Bougacha | 1d0560b | 2016-08-18 15:17:13 +0000 | [diff] [blame] | 145 | case TargetOpcode::G_SDIV: |
| 146 | return AArch64::SDIVXr; |
| 147 | case TargetOpcode::G_UDIV: |
| 148 | return AArch64::UDIVXr; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 149 | default: |
| 150 | return GenericOpc; |
| 151 | } |
| 152 | } |
Ahmed Bougacha | 33e19fe | 2016-08-18 16:05:11 +0000 | [diff] [blame] | 153 | case AArch64::FPRRegBankID: |
| 154 | switch (OpSize) { |
| 155 | case 32: |
| 156 | switch (GenericOpc) { |
| 157 | case TargetOpcode::G_FADD: |
| 158 | return AArch64::FADDSrr; |
| 159 | case TargetOpcode::G_FSUB: |
| 160 | return AArch64::FSUBSrr; |
| 161 | case TargetOpcode::G_FMUL: |
| 162 | return AArch64::FMULSrr; |
| 163 | case TargetOpcode::G_FDIV: |
| 164 | return AArch64::FDIVSrr; |
| 165 | default: |
| 166 | return GenericOpc; |
| 167 | } |
| 168 | case 64: |
| 169 | switch (GenericOpc) { |
| 170 | case TargetOpcode::G_FADD: |
| 171 | return AArch64::FADDDrr; |
| 172 | case TargetOpcode::G_FSUB: |
| 173 | return AArch64::FSUBDrr; |
| 174 | case TargetOpcode::G_FMUL: |
| 175 | return AArch64::FMULDrr; |
| 176 | case TargetOpcode::G_FDIV: |
| 177 | return AArch64::FDIVDrr; |
Quentin Colombet | 0e53127 | 2016-10-11 00:21:11 +0000 | [diff] [blame] | 178 | case TargetOpcode::G_OR: |
| 179 | return AArch64::ORRv8i8; |
Ahmed Bougacha | 33e19fe | 2016-08-18 16:05:11 +0000 | [diff] [blame] | 180 | default: |
| 181 | return GenericOpc; |
| 182 | } |
| 183 | } |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 184 | }; |
| 185 | return GenericOpc; |
| 186 | } |
| 187 | |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 188 | /// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc, |
| 189 | /// appropriate for the (value) register bank \p RegBankID and of memory access |
| 190 | /// size \p OpSize. This returns the variant with the base+unsigned-immediate |
| 191 | /// addressing mode (e.g., LDRXui). |
| 192 | /// \returns \p GenericOpc if the combination is unsupported. |
| 193 | static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID, |
| 194 | unsigned OpSize) { |
| 195 | const bool isStore = GenericOpc == TargetOpcode::G_STORE; |
| 196 | switch (RegBankID) { |
| 197 | case AArch64::GPRRegBankID: |
| 198 | switch (OpSize) { |
| 199 | case 32: |
| 200 | return isStore ? AArch64::STRWui : AArch64::LDRWui; |
| 201 | case 64: |
| 202 | return isStore ? AArch64::STRXui : AArch64::LDRXui; |
| 203 | } |
Quentin Colombet | d2623f8e | 2016-10-11 00:21:14 +0000 | [diff] [blame] | 204 | case AArch64::FPRRegBankID: |
| 205 | switch (OpSize) { |
| 206 | case 32: |
| 207 | return isStore ? AArch64::STRSui : AArch64::LDRSui; |
| 208 | case 64: |
| 209 | return isStore ? AArch64::STRDui : AArch64::LDRDui; |
| 210 | } |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 211 | }; |
| 212 | return GenericOpc; |
| 213 | } |
| 214 | |
Quentin Colombet | cb629a8 | 2016-10-12 03:57:49 +0000 | [diff] [blame] | 215 | static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII, |
| 216 | MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, |
| 217 | const RegisterBankInfo &RBI) { |
| 218 | |
| 219 | unsigned DstReg = I.getOperand(0).getReg(); |
| 220 | if (TargetRegisterInfo::isPhysicalRegister(DstReg)) { |
| 221 | assert(I.isCopy() && "Generic operators do not allow physical registers"); |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI); |
| 226 | const unsigned DstSize = MRI.getType(DstReg).getSizeInBits(); |
| 227 | unsigned SrcReg = I.getOperand(1).getReg(); |
| 228 | const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI); |
| 229 | (void)SrcSize; |
| 230 | assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) && |
| 231 | "No phys reg on generic operators"); |
| 232 | assert( |
| 233 | (DstSize == SrcSize || |
| 234 | // Copies are a mean to setup initial types, the number of |
| 235 | // bits may not exactly match. |
| 236 | (TargetRegisterInfo::isPhysicalRegister(SrcReg) && |
| 237 | DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) || |
| 238 | // Copies are a mean to copy bits around, as long as we are |
| 239 | // on the same register class, that's fine. Otherwise, that |
| 240 | // means we need some SUBREG_TO_REG or AND & co. |
| 241 | (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) && |
| 242 | "Copy with different width?!"); |
| 243 | assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) && |
| 244 | "GPRs cannot get more than 64-bit width values"); |
| 245 | const TargetRegisterClass *RC = nullptr; |
| 246 | |
| 247 | if (RegBank.getID() == AArch64::FPRRegBankID) { |
| 248 | if (DstSize <= 32) |
| 249 | RC = &AArch64::FPR32RegClass; |
| 250 | else if (DstSize <= 64) |
| 251 | RC = &AArch64::FPR64RegClass; |
| 252 | else if (DstSize <= 128) |
| 253 | RC = &AArch64::FPR128RegClass; |
| 254 | else { |
| 255 | DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n'); |
| 256 | return false; |
| 257 | } |
| 258 | } else { |
| 259 | assert(RegBank.getID() == AArch64::GPRRegBankID && |
| 260 | "Bitcast for the flags?"); |
| 261 | RC = |
| 262 | DstSize <= 32 ? &AArch64::GPR32allRegClass : &AArch64::GPR64allRegClass; |
| 263 | } |
| 264 | |
| 265 | // No need to constrain SrcReg. It will get constrained when |
| 266 | // we hit another of its use or its defs. |
| 267 | // Copies do not have constraints. |
| 268 | if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) { |
| 269 | DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode()) |
| 270 | << " operand\n"); |
| 271 | return false; |
| 272 | } |
| 273 | I.setDesc(TII.get(AArch64::COPY)); |
| 274 | return true; |
| 275 | } |
| 276 | |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 277 | bool AArch64InstructionSelector::select(MachineInstr &I) const { |
| 278 | assert(I.getParent() && "Instruction should be in a basic block!"); |
| 279 | assert(I.getParent()->getParent() && "Instruction should be in a function!"); |
| 280 | |
| 281 | MachineBasicBlock &MBB = *I.getParent(); |
| 282 | MachineFunction &MF = *MBB.getParent(); |
| 283 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 284 | |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 285 | if (!isPreISelGenericOpcode(I.getOpcode())) |
Quentin Colombet | cb629a8 | 2016-10-12 03:57:49 +0000 | [diff] [blame] | 286 | return !I.isCopy() || selectCopy(I, TII, MRI, TRI, RBI); |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 287 | |
| 288 | if (I.getNumOperands() != I.getNumExplicitOperands()) { |
| 289 | DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n"); |
| 290 | return false; |
| 291 | } |
| 292 | |
Tim Northover | 32a078a | 2016-09-15 10:09:59 +0000 | [diff] [blame] | 293 | LLT Ty = |
| 294 | I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{}; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 295 | |
Ahmed Bougacha | 8550509 | 2016-07-28 17:15:15 +0000 | [diff] [blame] | 296 | switch (I.getOpcode()) { |
| 297 | case TargetOpcode::G_BR: { |
| 298 | I.setDesc(TII.get(AArch64::B)); |
Ahmed Bougacha | 8550509 | 2016-07-28 17:15:15 +0000 | [diff] [blame] | 299 | return true; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Tim Northover | 5e3dbf3 | 2016-10-12 22:49:01 +0000 | [diff] [blame^] | 302 | case TargetOpcode::G_BRCOND: { |
| 303 | if (Ty.getSizeInBits() > 32) { |
| 304 | // We shouldn't need this on AArch64, but it would be implemented as an |
| 305 | // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the |
| 306 | // bit being tested is < 32. |
| 307 | DEBUG(dbgs() << "G_BRCOND has type: " << Ty |
| 308 | << ", expected at most 32-bits"); |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | const unsigned CondReg = I.getOperand(0).getReg(); |
| 313 | MachineBasicBlock *DestMBB = I.getOperand(1).getMBB(); |
| 314 | |
| 315 | auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW)) |
| 316 | .addUse(CondReg) |
| 317 | .addImm(/*bit offset=*/0) |
| 318 | .addMBB(DestMBB); |
| 319 | |
| 320 | I.eraseFromParent(); |
| 321 | return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI); |
| 322 | } |
| 323 | |
Tim Northover | 4edc60d | 2016-10-10 21:49:42 +0000 | [diff] [blame] | 324 | case TargetOpcode::G_CONSTANT: { |
| 325 | if (Ty.getSizeInBits() <= 32) |
| 326 | I.setDesc(TII.get(AArch64::MOVi32imm)); |
| 327 | else if (Ty.getSizeInBits() <= 64) |
| 328 | I.setDesc(TII.get(AArch64::MOVi64imm)); |
| 329 | else |
| 330 | return false; |
| 331 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
| 332 | } |
| 333 | |
Ahmed Bougacha | 0306b5e | 2016-08-16 14:02:42 +0000 | [diff] [blame] | 334 | case TargetOpcode::G_FRAME_INDEX: { |
| 335 | // allocas and G_FRAME_INDEX are only supported in addrspace(0). |
Tim Northover | 5ae8350 | 2016-09-15 09:20:34 +0000 | [diff] [blame] | 336 | if (Ty != LLT::pointer(0, 64)) { |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 337 | DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty |
Tim Northover | 5ae8350 | 2016-09-15 09:20:34 +0000 | [diff] [blame] | 338 | << ", expected: " << LLT::pointer(0, 64) << '\n'); |
Ahmed Bougacha | 0306b5e | 2016-08-16 14:02:42 +0000 | [diff] [blame] | 339 | return false; |
| 340 | } |
| 341 | |
| 342 | I.setDesc(TII.get(AArch64::ADDXri)); |
Ahmed Bougacha | 0306b5e | 2016-08-16 14:02:42 +0000 | [diff] [blame] | 343 | |
| 344 | // MOs for a #0 shifted immediate. |
| 345 | I.addOperand(MachineOperand::CreateImm(0)); |
| 346 | I.addOperand(MachineOperand::CreateImm(0)); |
| 347 | |
| 348 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
| 349 | } |
Tim Northover | bdf1624 | 2016-10-10 21:50:00 +0000 | [diff] [blame] | 350 | |
| 351 | case TargetOpcode::G_GLOBAL_VALUE: { |
| 352 | auto GV = I.getOperand(1).getGlobal(); |
| 353 | if (GV->isThreadLocal()) { |
| 354 | // FIXME: we don't support TLS yet. |
| 355 | return false; |
| 356 | } |
| 357 | unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM); |
| 358 | if (OpFlags & AArch64II::MO_GOT) |
| 359 | I.setDesc(TII.get(AArch64::LOADgot)); |
| 360 | else { |
| 361 | I.setDesc(TII.get(AArch64::MOVaddr)); |
| 362 | I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE); |
| 363 | MachineInstrBuilder MIB(MF, I); |
| 364 | MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(), |
| 365 | OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC); |
| 366 | } |
| 367 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
| 368 | } |
| 369 | |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 370 | case TargetOpcode::G_LOAD: |
| 371 | case TargetOpcode::G_STORE: { |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 372 | LLT MemTy = Ty; |
| 373 | LLT PtrTy = MRI.getType(I.getOperand(1).getReg()); |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 374 | |
Tim Northover | 5ae8350 | 2016-09-15 09:20:34 +0000 | [diff] [blame] | 375 | if (PtrTy != LLT::pointer(0, 64)) { |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 376 | DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy |
Tim Northover | 5ae8350 | 2016-09-15 09:20:34 +0000 | [diff] [blame] | 377 | << ", expected: " << LLT::pointer(0, 64) << '\n'); |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 378 | return false; |
| 379 | } |
| 380 | |
| 381 | #ifndef NDEBUG |
| 382 | // Sanity-check the pointer register. |
| 383 | const unsigned PtrReg = I.getOperand(1).getReg(); |
| 384 | const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI); |
| 385 | assert(PtrRB.getID() == AArch64::GPRRegBankID && |
| 386 | "Load/Store pointer operand isn't a GPR"); |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 387 | assert(MRI.getType(PtrReg).isPointer() && |
| 388 | "Load/Store pointer operand isn't a pointer"); |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 389 | #endif |
| 390 | |
| 391 | const unsigned ValReg = I.getOperand(0).getReg(); |
| 392 | const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI); |
| 393 | |
| 394 | const unsigned NewOpc = |
| 395 | selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemTy.getSizeInBits()); |
| 396 | if (NewOpc == I.getOpcode()) |
| 397 | return false; |
| 398 | |
| 399 | I.setDesc(TII.get(NewOpc)); |
Ahmed Bougacha | 7adfac5 | 2016-07-29 16:56:16 +0000 | [diff] [blame] | 400 | |
| 401 | I.addOperand(MachineOperand::CreateImm(0)); |
| 402 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
| 403 | } |
| 404 | |
Ahmed Bougacha | e4c03ab | 2016-08-16 14:37:46 +0000 | [diff] [blame] | 405 | case TargetOpcode::G_MUL: { |
| 406 | // Reject the various things we don't support yet. |
| 407 | if (unsupportedBinOp(I, RBI, MRI, TRI)) |
| 408 | return false; |
| 409 | |
| 410 | const unsigned DefReg = I.getOperand(0).getReg(); |
| 411 | const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI); |
| 412 | |
| 413 | if (RB.getID() != AArch64::GPRRegBankID) { |
| 414 | DEBUG(dbgs() << "G_MUL on bank: " << RB << ", expected: GPR\n"); |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | unsigned ZeroReg; |
| 419 | unsigned NewOpc; |
| 420 | if (Ty == LLT::scalar(32)) { |
| 421 | NewOpc = AArch64::MADDWrrr; |
| 422 | ZeroReg = AArch64::WZR; |
| 423 | } else if (Ty == LLT::scalar(64)) { |
| 424 | NewOpc = AArch64::MADDXrrr; |
| 425 | ZeroReg = AArch64::XZR; |
| 426 | } else { |
| 427 | DEBUG(dbgs() << "G_MUL has type: " << Ty << ", expected: " |
| 428 | << LLT::scalar(32) << " or " << LLT::scalar(64) << '\n'); |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | I.setDesc(TII.get(NewOpc)); |
Ahmed Bougacha | e4c03ab | 2016-08-16 14:37:46 +0000 | [diff] [blame] | 433 | |
| 434 | I.addOperand(MachineOperand::CreateReg(ZeroReg, /*isDef=*/false)); |
| 435 | |
| 436 | // Now that we selected an opcode, we need to constrain the register |
| 437 | // operands to use appropriate classes. |
| 438 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
| 439 | } |
| 440 | |
Ahmed Bougacha | 33e19fe | 2016-08-18 16:05:11 +0000 | [diff] [blame] | 441 | case TargetOpcode::G_FADD: |
| 442 | case TargetOpcode::G_FSUB: |
| 443 | case TargetOpcode::G_FMUL: |
| 444 | case TargetOpcode::G_FDIV: |
| 445 | |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 446 | case TargetOpcode::G_OR: |
Ahmed Bougacha | 6db3cfe | 2016-07-29 16:56:25 +0000 | [diff] [blame] | 447 | case TargetOpcode::G_XOR: |
Ahmed Bougacha | 61a7928 | 2016-07-28 16:58:31 +0000 | [diff] [blame] | 448 | case TargetOpcode::G_AND: |
Ahmed Bougacha | 2ac5bf9 | 2016-08-16 14:02:47 +0000 | [diff] [blame] | 449 | case TargetOpcode::G_SHL: |
| 450 | case TargetOpcode::G_LSHR: |
| 451 | case TargetOpcode::G_ASHR: |
Ahmed Bougacha | 1d0560b | 2016-08-18 15:17:13 +0000 | [diff] [blame] | 452 | case TargetOpcode::G_SDIV: |
| 453 | case TargetOpcode::G_UDIV: |
Ahmed Bougacha | d7748d6 | 2016-07-28 16:58:35 +0000 | [diff] [blame] | 454 | case TargetOpcode::G_ADD: |
Tim Northover | 2fda4b0 | 2016-10-10 21:49:49 +0000 | [diff] [blame] | 455 | case TargetOpcode::G_SUB: |
| 456 | case TargetOpcode::G_GEP: { |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 457 | // Reject the various things we don't support yet. |
Ahmed Bougacha | 59e160a | 2016-08-16 14:37:40 +0000 | [diff] [blame] | 458 | if (unsupportedBinOp(I, RBI, MRI, TRI)) |
| 459 | return false; |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 460 | |
Ahmed Bougacha | 59e160a | 2016-08-16 14:37:40 +0000 | [diff] [blame] | 461 | const unsigned OpSize = Ty.getSizeInBits(); |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 462 | |
| 463 | const unsigned DefReg = I.getOperand(0).getReg(); |
| 464 | const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI); |
| 465 | |
| 466 | const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize); |
| 467 | if (NewOpc == I.getOpcode()) |
| 468 | return false; |
| 469 | |
| 470 | I.setDesc(TII.get(NewOpc)); |
| 471 | // FIXME: Should the type be always reset in setDesc? |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 472 | |
| 473 | // Now that we selected an opcode, we need to constrain the register |
| 474 | // operands to use appropriate classes. |
| 475 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
| 476 | } |
Tim Northover | 3d38b3a | 2016-10-11 20:50:21 +0000 | [diff] [blame] | 477 | |
| 478 | case TargetOpcode::G_ANYEXT: { |
| 479 | const unsigned DstReg = I.getOperand(0).getReg(); |
| 480 | const unsigned SrcReg = I.getOperand(1).getReg(); |
| 481 | |
Quentin Colombet | cb629a8 | 2016-10-12 03:57:49 +0000 | [diff] [blame] | 482 | const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI); |
| 483 | if (RBDst.getID() != AArch64::GPRRegBankID) { |
| 484 | DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst << ", expected: GPR\n"); |
| 485 | return false; |
| 486 | } |
Tim Northover | 3d38b3a | 2016-10-11 20:50:21 +0000 | [diff] [blame] | 487 | |
Quentin Colombet | cb629a8 | 2016-10-12 03:57:49 +0000 | [diff] [blame] | 488 | const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI); |
| 489 | if (RBSrc.getID() != AArch64::GPRRegBankID) { |
| 490 | DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc << ", expected: GPR\n"); |
Tim Northover | 3d38b3a | 2016-10-11 20:50:21 +0000 | [diff] [blame] | 491 | return false; |
| 492 | } |
| 493 | |
| 494 | const unsigned DstSize = MRI.getType(DstReg).getSizeInBits(); |
| 495 | |
| 496 | if (DstSize == 0) { |
| 497 | DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n"); |
| 498 | return false; |
| 499 | } |
| 500 | |
Quentin Colombet | cb629a8 | 2016-10-12 03:57:49 +0000 | [diff] [blame] | 501 | if (DstSize != 64 && DstSize > 32) { |
Tim Northover | 3d38b3a | 2016-10-11 20:50:21 +0000 | [diff] [blame] | 502 | DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize |
| 503 | << ", expected: 32 or 64\n"); |
| 504 | return false; |
| 505 | } |
Quentin Colombet | cb629a8 | 2016-10-12 03:57:49 +0000 | [diff] [blame] | 506 | // At this point G_ANYEXT is just like a plain COPY, but we need |
| 507 | // to explicitly form the 64-bit value if any. |
| 508 | if (DstSize > 32) { |
| 509 | unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass); |
| 510 | BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG)) |
| 511 | .addDef(ExtSrc) |
| 512 | .addImm(0) |
| 513 | .addUse(SrcReg) |
| 514 | .addImm(AArch64::sub_32); |
| 515 | I.getOperand(1).setReg(ExtSrc); |
Tim Northover | 3d38b3a | 2016-10-11 20:50:21 +0000 | [diff] [blame] | 516 | } |
Quentin Colombet | cb629a8 | 2016-10-12 03:57:49 +0000 | [diff] [blame] | 517 | return selectCopy(I, TII, MRI, TRI, RBI); |
Tim Northover | 3d38b3a | 2016-10-11 20:50:21 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | case TargetOpcode::G_ZEXT: |
| 521 | case TargetOpcode::G_SEXT: { |
| 522 | unsigned Opcode = I.getOpcode(); |
| 523 | const LLT DstTy = MRI.getType(I.getOperand(0).getReg()), |
| 524 | SrcTy = MRI.getType(I.getOperand(1).getReg()); |
| 525 | const bool isSigned = Opcode == TargetOpcode::G_SEXT; |
| 526 | const unsigned DefReg = I.getOperand(0).getReg(); |
| 527 | const unsigned SrcReg = I.getOperand(1).getReg(); |
| 528 | const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI); |
| 529 | |
| 530 | if (RB.getID() != AArch64::GPRRegBankID) { |
| 531 | DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB |
| 532 | << ", expected: GPR\n"); |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | MachineInstr *ExtI; |
| 537 | if (DstTy == LLT::scalar(64)) { |
| 538 | // FIXME: Can we avoid manually doing this? |
| 539 | if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) { |
| 540 | DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode) |
| 541 | << " operand\n"); |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | const unsigned SrcXReg = |
| 546 | MRI.createVirtualRegister(&AArch64::GPR64RegClass); |
| 547 | BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG)) |
| 548 | .addDef(SrcXReg) |
| 549 | .addImm(0) |
| 550 | .addUse(SrcReg) |
| 551 | .addImm(AArch64::sub_32); |
| 552 | |
| 553 | const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri; |
| 554 | ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc)) |
| 555 | .addDef(DefReg) |
| 556 | .addUse(SrcXReg) |
| 557 | .addImm(0) |
| 558 | .addImm(SrcTy.getSizeInBits() - 1); |
| 559 | } else if (DstTy == LLT::scalar(32)) { |
| 560 | const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri; |
| 561 | ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc)) |
| 562 | .addDef(DefReg) |
| 563 | .addUse(SrcReg) |
| 564 | .addImm(0) |
| 565 | .addImm(SrcTy.getSizeInBits() - 1); |
| 566 | } else { |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI); |
| 571 | |
| 572 | I.eraseFromParent(); |
| 573 | return true; |
| 574 | } |
Tim Northover | c1d8c2b | 2016-10-11 22:29:23 +0000 | [diff] [blame] | 575 | |
| 576 | case TargetOpcode::G_INTTOPTR: |
| 577 | case TargetOpcode::G_PTRTOINT: |
Quentin Colombet | 9de30fa | 2016-10-12 03:57:52 +0000 | [diff] [blame] | 578 | case TargetOpcode::G_BITCAST: |
| 579 | return selectCopy(I, TII, MRI, TRI, RBI); |
Ahmed Bougacha | 6756a2c | 2016-07-27 14:31:55 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | return false; |
| 583 | } |