Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 1 | //===- ARMInstructionSelector.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 ARM. |
| 11 | /// \todo This should be generated by TableGen. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 14 | #include "ARMRegisterBankInfo.h" |
| 15 | #include "ARMSubtarget.h" |
| 16 | #include "ARMTargetMachine.h" |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame^] | 17 | #include "llvm/CodeGen/GlobalISel/InstructionSelector.h" |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
| 20 | |
| 21 | #define DEBUG_TYPE "arm-isel" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | #ifndef LLVM_BUILD_GLOBAL_ISEL |
| 26 | #error "You shouldn't build this" |
| 27 | #endif |
| 28 | |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame^] | 29 | namespace { |
| 30 | class ARMInstructionSelector : public InstructionSelector { |
| 31 | public: |
| 32 | ARMInstructionSelector(const ARMSubtarget &STI, |
| 33 | const ARMRegisterBankInfo &RBI); |
| 34 | |
| 35 | bool select(MachineInstr &I) const override; |
| 36 | |
| 37 | private: |
| 38 | const ARMBaseInstrInfo &TII; |
| 39 | const ARMBaseRegisterInfo &TRI; |
| 40 | const ARMRegisterBankInfo &RBI; |
| 41 | }; |
| 42 | } // end anonymous namespace |
| 43 | |
| 44 | namespace llvm { |
| 45 | InstructionSelector * |
| 46 | createARMInstructionSelector(const ARMSubtarget &STI, |
| 47 | const ARMRegisterBankInfo &RBI) { |
| 48 | return new ARMInstructionSelector(STI, RBI); |
| 49 | } |
| 50 | } |
| 51 | |
Diana Picus | 895c6aa | 2016-11-15 16:42:10 +0000 | [diff] [blame] | 52 | ARMInstructionSelector::ARMInstructionSelector(const ARMSubtarget &STI, |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 53 | const ARMRegisterBankInfo &RBI) |
Diana Picus | 895c6aa | 2016-11-15 16:42:10 +0000 | [diff] [blame] | 54 | : InstructionSelector(), TII(*STI.getInstrInfo()), |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 55 | TRI(*STI.getRegisterInfo()), RBI(RBI) {} |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 56 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 57 | static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII, |
| 58 | MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, |
| 59 | const RegisterBankInfo &RBI) { |
| 60 | unsigned DstReg = I.getOperand(0).getReg(); |
| 61 | if (TargetRegisterInfo::isPhysicalRegister(DstReg)) |
| 62 | return true; |
| 63 | |
| 64 | const RegisterBank *RegBank = RBI.getRegBank(DstReg, MRI, TRI); |
Benjamin Kramer | 24bf868 | 2016-12-16 13:13:03 +0000 | [diff] [blame] | 65 | (void)RegBank; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 66 | assert(RegBank && "Can't get reg bank for virtual register"); |
| 67 | |
Diana Picus | 36aa09f | 2016-12-19 14:07:50 +0000 | [diff] [blame] | 68 | const unsigned DstSize = MRI.getType(DstReg).getSizeInBits(); |
Daniel Jasper | 24218d5 | 2016-12-19 14:24:22 +0000 | [diff] [blame] | 69 | (void)DstSize; |
Diana Picus | 36aa09f | 2016-12-19 14:07:50 +0000 | [diff] [blame] | 70 | unsigned SrcReg = I.getOperand(1).getReg(); |
| 71 | const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI); |
| 72 | (void)SrcSize; |
Diana Picus | 64a3343 | 2017-04-21 13:16:50 +0000 | [diff] [blame] | 73 | // We use copies for trunc, so it's ok for the size of the destination to be |
| 74 | // smaller (the higher bits will just be undefined). |
| 75 | assert(DstSize <= SrcSize && "Copy with different width?!"); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 76 | |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 77 | assert((RegBank->getID() == ARM::GPRRegBankID || |
| 78 | RegBank->getID() == ARM::FPRRegBankID) && |
| 79 | "Unsupported reg bank"); |
| 80 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 81 | const TargetRegisterClass *RC = &ARM::GPRRegClass; |
| 82 | |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 83 | if (RegBank->getID() == ARM::FPRRegBankID) { |
Diana Picus | 6beef3c | 2017-02-16 12:19:52 +0000 | [diff] [blame] | 84 | if (DstSize == 32) |
| 85 | RC = &ARM::SPRRegClass; |
| 86 | else if (DstSize == 64) |
| 87 | RC = &ARM::DPRRegClass; |
| 88 | else |
| 89 | llvm_unreachable("Unsupported destination size"); |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 92 | // No need to constrain SrcReg. It will get constrained when |
| 93 | // we hit another of its uses or its defs. |
| 94 | // Copies do not have constraints. |
| 95 | if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) { |
| 96 | DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode()) |
| 97 | << " operand\n"); |
| 98 | return false; |
| 99 | } |
| 100 | return true; |
| 101 | } |
| 102 | |
Diana Picus | 6beef3c | 2017-02-16 12:19:52 +0000 | [diff] [blame] | 103 | static bool selectFAdd(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII, |
| 104 | MachineRegisterInfo &MRI) { |
| 105 | assert(TII.getSubtarget().hasVFP2() && "Can't select fp add without vfp"); |
| 106 | |
| 107 | LLT Ty = MRI.getType(MIB->getOperand(0).getReg()); |
| 108 | unsigned ValSize = Ty.getSizeInBits(); |
| 109 | |
| 110 | if (ValSize == 32) { |
| 111 | if (TII.getSubtarget().useNEONForSinglePrecisionFP()) |
| 112 | return false; |
| 113 | MIB->setDesc(TII.get(ARM::VADDS)); |
| 114 | } else { |
| 115 | assert(ValSize == 64 && "Unsupported size for floating point value"); |
| 116 | if (TII.getSubtarget().isFPOnlySP()) |
| 117 | return false; |
| 118 | MIB->setDesc(TII.get(ARM::VADDD)); |
| 119 | } |
| 120 | MIB.add(predOps(ARMCC::AL)); |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 125 | static bool selectSequence(MachineInstrBuilder &MIB, |
| 126 | const ARMBaseInstrInfo &TII, |
| 127 | MachineRegisterInfo &MRI, |
| 128 | const TargetRegisterInfo &TRI, |
| 129 | const RegisterBankInfo &RBI) { |
| 130 | assert(TII.getSubtarget().hasVFP2() && "Can't select sequence without VFP"); |
| 131 | |
| 132 | // We only support G_SEQUENCE as a way to stick together two scalar GPRs |
| 133 | // into one DPR. |
| 134 | unsigned VReg0 = MIB->getOperand(0).getReg(); |
| 135 | (void)VReg0; |
| 136 | assert(MRI.getType(VReg0).getSizeInBits() == 64 && |
| 137 | RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID && |
| 138 | "Unsupported operand for G_SEQUENCE"); |
| 139 | unsigned VReg1 = MIB->getOperand(1).getReg(); |
| 140 | (void)VReg1; |
| 141 | assert(MRI.getType(VReg1).getSizeInBits() == 32 && |
| 142 | RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID && |
| 143 | "Unsupported operand for G_SEQUENCE"); |
| 144 | unsigned VReg2 = MIB->getOperand(3).getReg(); |
| 145 | (void)VReg2; |
| 146 | assert(MRI.getType(VReg2).getSizeInBits() == 32 && |
| 147 | RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID && |
| 148 | "Unsupported operand for G_SEQUENCE"); |
| 149 | |
| 150 | // Remove the operands corresponding to the offsets. |
| 151 | MIB->RemoveOperand(4); |
| 152 | MIB->RemoveOperand(2); |
| 153 | |
| 154 | MIB->setDesc(TII.get(ARM::VMOVDRR)); |
| 155 | MIB.add(predOps(ARMCC::AL)); |
| 156 | |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | static bool selectExtract(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII, |
| 161 | MachineRegisterInfo &MRI, |
| 162 | const TargetRegisterInfo &TRI, |
| 163 | const RegisterBankInfo &RBI) { |
| 164 | assert(TII.getSubtarget().hasVFP2() && "Can't select extract without VFP"); |
| 165 | |
| 166 | // We only support G_EXTRACT as a way to break up one DPR into two GPRs. |
| 167 | unsigned VReg0 = MIB->getOperand(0).getReg(); |
| 168 | (void)VReg0; |
| 169 | assert(MRI.getType(VReg0).getSizeInBits() == 32 && |
| 170 | RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID && |
Tim Northover | c2c545b | 2017-03-06 23:50:28 +0000 | [diff] [blame] | 171 | "Unsupported operand for G_EXTRACT"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 172 | unsigned VReg1 = MIB->getOperand(1).getReg(); |
| 173 | (void)VReg1; |
Tim Northover | c2c545b | 2017-03-06 23:50:28 +0000 | [diff] [blame] | 174 | assert(MRI.getType(VReg1).getSizeInBits() == 64 && |
| 175 | RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::FPRRegBankID && |
| 176 | "Unsupported operand for G_EXTRACT"); |
| 177 | assert(MIB->getOperand(2).getImm() % 32 == 0 && |
| 178 | "Unsupported operand for G_EXTRACT"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 179 | |
| 180 | // Remove the operands corresponding to the offsets. |
Tim Northover | c2c545b | 2017-03-06 23:50:28 +0000 | [diff] [blame] | 181 | MIB->getOperand(2).setImm(MIB->getOperand(2).getImm() / 32); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 182 | |
Tim Northover | c2c545b | 2017-03-06 23:50:28 +0000 | [diff] [blame] | 183 | MIB->setDesc(TII.get(ARM::VGETLNi32)); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 184 | MIB.add(predOps(ARMCC::AL)); |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 189 | /// Select the opcode for simple extensions (that translate to a single SXT/UXT |
| 190 | /// instruction). Extension operations more complicated than that should not |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 191 | /// invoke this. Returns the original opcode if it doesn't know how to select a |
| 192 | /// better one. |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 193 | static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) { |
| 194 | using namespace TargetOpcode; |
| 195 | |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 196 | if (Size != 8 && Size != 16) |
| 197 | return Opc; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 198 | |
| 199 | if (Opc == G_SEXT) |
| 200 | return Size == 8 ? ARM::SXTB : ARM::SXTH; |
| 201 | |
| 202 | if (Opc == G_ZEXT) |
| 203 | return Size == 8 ? ARM::UXTB : ARM::UXTH; |
| 204 | |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 205 | return Opc; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 208 | /// Select the opcode for simple loads and stores. For types smaller than 32 |
| 209 | /// bits, the value will be zero extended. Returns the original opcode if it |
| 210 | /// doesn't know how to select a better one. |
| 211 | static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank, |
| 212 | unsigned Size) { |
| 213 | bool isStore = Opc == TargetOpcode::G_STORE; |
| 214 | |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 215 | if (RegBank == ARM::GPRRegBankID) { |
| 216 | switch (Size) { |
| 217 | case 1: |
| 218 | case 8: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 219 | return isStore ? ARM::STRBi12 : ARM::LDRBi12; |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 220 | case 16: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 221 | return isStore ? ARM::STRH : ARM::LDRH; |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 222 | case 32: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 223 | return isStore ? ARM::STRi12 : ARM::LDRi12; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 224 | default: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 225 | return Opc; |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 226 | } |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 229 | if (RegBank == ARM::FPRRegBankID) { |
| 230 | switch (Size) { |
| 231 | case 32: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 232 | return isStore ? ARM::VSTRS : ARM::VLDRS; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 233 | case 64: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 234 | return isStore ? ARM::VSTRD : ARM::VLDRD; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 235 | default: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 236 | return Opc; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 237 | } |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 240 | return Opc; |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 243 | bool ARMInstructionSelector::select(MachineInstr &I) const { |
| 244 | assert(I.getParent() && "Instruction should be in a basic block!"); |
| 245 | assert(I.getParent()->getParent() && "Instruction should be in a function!"); |
| 246 | |
| 247 | auto &MBB = *I.getParent(); |
| 248 | auto &MF = *MBB.getParent(); |
| 249 | auto &MRI = MF.getRegInfo(); |
| 250 | |
| 251 | if (!isPreISelGenericOpcode(I.getOpcode())) { |
| 252 | if (I.isCopy()) |
| 253 | return selectCopy(I, TII, MRI, TRI, RBI); |
| 254 | |
| 255 | return true; |
| 256 | } |
| 257 | |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 258 | MachineInstrBuilder MIB{MF, I}; |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 259 | bool isSExt = false; |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 260 | |
| 261 | using namespace TargetOpcode; |
| 262 | switch (I.getOpcode()) { |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 263 | case G_SEXT: |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 264 | isSExt = true; |
| 265 | LLVM_FALLTHROUGH; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 266 | case G_ZEXT: { |
| 267 | LLT DstTy = MRI.getType(I.getOperand(0).getReg()); |
| 268 | // FIXME: Smaller destination sizes coming soon! |
| 269 | if (DstTy.getSizeInBits() != 32) { |
| 270 | DEBUG(dbgs() << "Unsupported destination size for extension"); |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | LLT SrcTy = MRI.getType(I.getOperand(1).getReg()); |
| 275 | unsigned SrcSize = SrcTy.getSizeInBits(); |
| 276 | switch (SrcSize) { |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 277 | case 1: { |
| 278 | // ZExt boils down to & 0x1; for SExt we also subtract that from 0 |
| 279 | I.setDesc(TII.get(ARM::ANDri)); |
| 280 | MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp()); |
| 281 | |
| 282 | if (isSExt) { |
| 283 | unsigned SExtResult = I.getOperand(0).getReg(); |
| 284 | |
| 285 | // Use a new virtual register for the result of the AND |
| 286 | unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass); |
| 287 | I.getOperand(0).setReg(AndResult); |
| 288 | |
| 289 | auto InsertBefore = std::next(I.getIterator()); |
Martin Bohme | 8396e14 | 2017-01-25 14:28:19 +0000 | [diff] [blame] | 290 | auto SubI = |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 291 | BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri)) |
| 292 | .addDef(SExtResult) |
| 293 | .addUse(AndResult) |
| 294 | .addImm(0) |
| 295 | .add(predOps(ARMCC::AL)) |
| 296 | .add(condCodeOp()); |
| 297 | if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI)) |
| 298 | return false; |
| 299 | } |
| 300 | break; |
| 301 | } |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 302 | case 8: |
| 303 | case 16: { |
| 304 | unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize); |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 305 | if (NewOpc == I.getOpcode()) |
| 306 | return false; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 307 | I.setDesc(TII.get(NewOpc)); |
| 308 | MIB.addImm(0).add(predOps(ARMCC::AL)); |
| 309 | break; |
| 310 | } |
| 311 | default: |
| 312 | DEBUG(dbgs() << "Unsupported source size for extension"); |
| 313 | return false; |
| 314 | } |
| 315 | break; |
| 316 | } |
Diana Picus | 64a3343 | 2017-04-21 13:16:50 +0000 | [diff] [blame] | 317 | case G_TRUNC: { |
| 318 | // The high bits are undefined, so there's nothing special to do, just |
| 319 | // treat it as a copy. |
| 320 | auto SrcReg = I.getOperand(1).getReg(); |
| 321 | auto DstReg = I.getOperand(0).getReg(); |
| 322 | |
| 323 | const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI); |
| 324 | const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI); |
| 325 | |
| 326 | if (SrcRegBank.getID() != DstRegBank.getID()) { |
| 327 | DEBUG(dbgs() << "G_TRUNC operands on different register banks\n"); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | if (SrcRegBank.getID() != ARM::GPRRegBankID) { |
| 332 | DEBUG(dbgs() << "G_TRUNC on non-GPR not supported yet\n"); |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | I.setDesc(TII.get(COPY)); |
| 337 | return selectCopy(I, TII, MRI, TRI, RBI); |
| 338 | } |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 339 | case G_ADD: |
Diana Picus | 9d07094 | 2017-02-28 10:14:38 +0000 | [diff] [blame] | 340 | case G_GEP: |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 341 | I.setDesc(TII.get(ARM::ADDrr)); |
Diana Picus | 8a73f55 | 2017-01-13 10:18:01 +0000 | [diff] [blame] | 342 | MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 343 | break; |
Diana Picus | a3a0ccc | 2017-04-18 12:35:28 +0000 | [diff] [blame] | 344 | case G_SUB: |
| 345 | I.setDesc(TII.get(ARM::SUBrr)); |
| 346 | MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); |
| 347 | break; |
Diana Picus | 49472ff | 2017-04-19 07:29:46 +0000 | [diff] [blame] | 348 | case G_MUL: |
| 349 | if (TII.getSubtarget().hasV6Ops()) { |
| 350 | I.setDesc(TII.get(ARM::MUL)); |
| 351 | } else { |
| 352 | assert(TII.getSubtarget().useMulOps() && "Unsupported target"); |
| 353 | I.setDesc(TII.get(ARM::MULv5)); |
| 354 | MIB->getOperand(0).setIsEarlyClobber(true); |
| 355 | } |
| 356 | MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); |
| 357 | break; |
Diana Picus | b70e88b | 2017-04-24 08:20:05 +0000 | [diff] [blame] | 358 | case G_SDIV: |
| 359 | assert(TII.getSubtarget().hasDivideInARMMode() && "Unsupported operation"); |
| 360 | I.setDesc(TII.get(ARM::SDIV)); |
| 361 | MIB.add(predOps(ARMCC::AL)); |
| 362 | break; |
| 363 | case G_UDIV: |
| 364 | assert(TII.getSubtarget().hasDivideInARMMode() && "Unsupported operation"); |
| 365 | I.setDesc(TII.get(ARM::UDIV)); |
| 366 | MIB.add(predOps(ARMCC::AL)); |
| 367 | break; |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 368 | case G_FADD: |
Diana Picus | 6beef3c | 2017-02-16 12:19:52 +0000 | [diff] [blame] | 369 | if (!selectFAdd(MIB, TII, MRI)) |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 370 | return false; |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 371 | break; |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 372 | case G_FRAME_INDEX: |
| 373 | // Add 0 to the given frame index and hope it will eventually be folded into |
| 374 | // the user(s). |
| 375 | I.setDesc(TII.get(ARM::ADDri)); |
Diana Picus | 8a73f55 | 2017-01-13 10:18:01 +0000 | [diff] [blame] | 376 | MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 377 | break; |
Diana Picus | 5a7203a | 2017-02-28 13:05:42 +0000 | [diff] [blame] | 378 | case G_CONSTANT: { |
| 379 | unsigned Reg = I.getOperand(0).getReg(); |
| 380 | if (MRI.getType(Reg).getSizeInBits() != 32) |
| 381 | return false; |
| 382 | |
| 383 | assert(RBI.getRegBank(Reg, MRI, TRI)->getID() == ARM::GPRRegBankID && |
| 384 | "Expected constant to live in a GPR"); |
| 385 | I.setDesc(TII.get(ARM::MOVi)); |
| 386 | MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 95a8aa9 | 2017-04-24 06:30:56 +0000 | [diff] [blame] | 387 | |
| 388 | auto &Val = I.getOperand(1); |
| 389 | if (Val.isCImm()) { |
| 390 | if (Val.getCImm()->getBitWidth() > 32) |
| 391 | return false; |
| 392 | Val.ChangeToImmediate(Val.getCImm()->getZExtValue()); |
| 393 | } |
| 394 | |
| 395 | if (!Val.isImm()) { |
| 396 | return false; |
| 397 | } |
| 398 | |
Diana Picus | 5a7203a | 2017-02-28 13:05:42 +0000 | [diff] [blame] | 399 | break; |
| 400 | } |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 401 | case G_STORE: |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 402 | case G_LOAD: { |
Diana Picus | 1c33c9f | 2017-02-20 14:45:58 +0000 | [diff] [blame] | 403 | const auto &MemOp = **I.memoperands_begin(); |
| 404 | if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) { |
| 405 | DEBUG(dbgs() << "Atomic load/store not supported yet\n"); |
| 406 | return false; |
| 407 | } |
| 408 | |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 409 | unsigned Reg = I.getOperand(0).getReg(); |
| 410 | unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID(); |
| 411 | |
| 412 | LLT ValTy = MRI.getType(Reg); |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 413 | const auto ValSize = ValTy.getSizeInBits(); |
| 414 | |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 415 | assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) && |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 416 | "Don't know how to load/store 64-bit value without VFP"); |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 417 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 418 | const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize); |
| 419 | if (NewOpc == G_LOAD || NewOpc == G_STORE) |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 420 | return false; |
| 421 | |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 422 | I.setDesc(TII.get(NewOpc)); |
| 423 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 424 | if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH) |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 425 | // LDRH has a funny addressing mode (there's already a FIXME for it). |
| 426 | MIB.addReg(0); |
Diana Picus | 4f8c3e1 | 2017-01-13 09:37:56 +0000 | [diff] [blame] | 427 | MIB.addImm(0).add(predOps(ARMCC::AL)); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 428 | break; |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 429 | } |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 430 | case G_SEQUENCE: { |
| 431 | if (!selectSequence(MIB, TII, MRI, TRI, RBI)) |
| 432 | return false; |
| 433 | break; |
| 434 | } |
| 435 | case G_EXTRACT: { |
| 436 | if (!selectExtract(MIB, TII, MRI, TRI, RBI)) |
| 437 | return false; |
| 438 | break; |
| 439 | } |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 440 | default: |
| 441 | return false; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 444 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 445 | } |