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 { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 30 | |
| 31 | #define GET_GLOBALISEL_PREDICATE_BITSET |
| 32 | #include "ARMGenGlobalISel.inc" |
| 33 | #undef GET_GLOBALISEL_PREDICATE_BITSET |
| 34 | |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 35 | class ARMInstructionSelector : public InstructionSelector { |
| 36 | public: |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 37 | ARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget &STI, |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 38 | const ARMRegisterBankInfo &RBI); |
| 39 | |
| 40 | bool select(MachineInstr &I) const override; |
| 41 | |
| 42 | private: |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 43 | bool selectImpl(MachineInstr &I) const; |
| 44 | |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame^] | 45 | bool selectICmp(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII, |
| 46 | MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, |
| 47 | const RegisterBankInfo &RBI) const; |
| 48 | |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 49 | const ARMBaseInstrInfo &TII; |
| 50 | const ARMBaseRegisterInfo &TRI; |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 51 | const ARMBaseTargetMachine &TM; |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 52 | const ARMRegisterBankInfo &RBI; |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 53 | const ARMSubtarget &STI; |
| 54 | |
| 55 | #define GET_GLOBALISEL_PREDICATES_DECL |
| 56 | #include "ARMGenGlobalISel.inc" |
| 57 | #undef GET_GLOBALISEL_PREDICATES_DECL |
| 58 | |
| 59 | // We declare the temporaries used by selectImpl() in the class to minimize the |
| 60 | // cost of constructing placeholder values. |
| 61 | #define GET_GLOBALISEL_TEMPORARIES_DECL |
| 62 | #include "ARMGenGlobalISel.inc" |
| 63 | #undef GET_GLOBALISEL_TEMPORARIES_DECL |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 64 | }; |
| 65 | } // end anonymous namespace |
| 66 | |
| 67 | namespace llvm { |
| 68 | InstructionSelector * |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 69 | createARMInstructionSelector(const ARMBaseTargetMachine &TM, |
| 70 | const ARMSubtarget &STI, |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 71 | const ARMRegisterBankInfo &RBI) { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 72 | return new ARMInstructionSelector(TM, STI, RBI); |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 76 | unsigned zero_reg = 0; |
| 77 | |
| 78 | #define GET_GLOBALISEL_IMPL |
| 79 | #include "ARMGenGlobalISel.inc" |
| 80 | #undef GET_GLOBALISEL_IMPL |
| 81 | |
| 82 | ARMInstructionSelector::ARMInstructionSelector(const ARMBaseTargetMachine &TM, |
| 83 | const ARMSubtarget &STI, |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 84 | const ARMRegisterBankInfo &RBI) |
Diana Picus | 895c6aa | 2016-11-15 16:42:10 +0000 | [diff] [blame] | 85 | : InstructionSelector(), TII(*STI.getInstrInfo()), |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 86 | TRI(*STI.getRegisterInfo()), TM(TM), RBI(RBI), STI(STI), |
| 87 | #define GET_GLOBALISEL_PREDICATES_INIT |
| 88 | #include "ARMGenGlobalISel.inc" |
| 89 | #undef GET_GLOBALISEL_PREDICATES_INIT |
| 90 | #define GET_GLOBALISEL_TEMPORARIES_INIT |
| 91 | #include "ARMGenGlobalISel.inc" |
| 92 | #undef GET_GLOBALISEL_TEMPORARIES_INIT |
| 93 | { |
| 94 | } |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 95 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 96 | static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII, |
| 97 | MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, |
| 98 | const RegisterBankInfo &RBI) { |
| 99 | unsigned DstReg = I.getOperand(0).getReg(); |
| 100 | if (TargetRegisterInfo::isPhysicalRegister(DstReg)) |
| 101 | return true; |
| 102 | |
| 103 | const RegisterBank *RegBank = RBI.getRegBank(DstReg, MRI, TRI); |
Benjamin Kramer | 24bf868 | 2016-12-16 13:13:03 +0000 | [diff] [blame] | 104 | (void)RegBank; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 105 | assert(RegBank && "Can't get reg bank for virtual register"); |
| 106 | |
Diana Picus | 36aa09f | 2016-12-19 14:07:50 +0000 | [diff] [blame] | 107 | const unsigned DstSize = MRI.getType(DstReg).getSizeInBits(); |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 108 | assert((RegBank->getID() == ARM::GPRRegBankID || |
| 109 | RegBank->getID() == ARM::FPRRegBankID) && |
| 110 | "Unsupported reg bank"); |
| 111 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 112 | const TargetRegisterClass *RC = &ARM::GPRRegClass; |
| 113 | |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 114 | if (RegBank->getID() == ARM::FPRRegBankID) { |
Diana Picus | 6beef3c | 2017-02-16 12:19:52 +0000 | [diff] [blame] | 115 | if (DstSize == 32) |
| 116 | RC = &ARM::SPRRegClass; |
| 117 | else if (DstSize == 64) |
| 118 | RC = &ARM::DPRRegClass; |
| 119 | else |
| 120 | llvm_unreachable("Unsupported destination size"); |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 123 | // No need to constrain SrcReg. It will get constrained when |
| 124 | // we hit another of its uses or its defs. |
| 125 | // Copies do not have constraints. |
| 126 | if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) { |
| 127 | DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode()) |
| 128 | << " operand\n"); |
| 129 | return false; |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 134 | static bool selectMergeValues(MachineInstrBuilder &MIB, |
| 135 | const ARMBaseInstrInfo &TII, |
| 136 | MachineRegisterInfo &MRI, |
| 137 | const TargetRegisterInfo &TRI, |
| 138 | const RegisterBankInfo &RBI) { |
| 139 | assert(TII.getSubtarget().hasVFP2() && "Can't select merge without VFP"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 140 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 141 | // We only support G_MERGE_VALUES as a way to stick together two scalar GPRs |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 142 | // into one DPR. |
| 143 | unsigned VReg0 = MIB->getOperand(0).getReg(); |
| 144 | (void)VReg0; |
| 145 | assert(MRI.getType(VReg0).getSizeInBits() == 64 && |
| 146 | RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID && |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 147 | "Unsupported operand for G_MERGE_VALUES"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 148 | unsigned VReg1 = MIB->getOperand(1).getReg(); |
| 149 | (void)VReg1; |
| 150 | assert(MRI.getType(VReg1).getSizeInBits() == 32 && |
| 151 | RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID && |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 152 | "Unsupported operand for G_MERGE_VALUES"); |
| 153 | unsigned VReg2 = MIB->getOperand(2).getReg(); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 154 | (void)VReg2; |
| 155 | assert(MRI.getType(VReg2).getSizeInBits() == 32 && |
| 156 | RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID && |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 157 | "Unsupported operand for G_MERGE_VALUES"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 158 | |
| 159 | MIB->setDesc(TII.get(ARM::VMOVDRR)); |
| 160 | MIB.add(predOps(ARMCC::AL)); |
| 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 165 | static bool selectUnmergeValues(MachineInstrBuilder &MIB, |
| 166 | const ARMBaseInstrInfo &TII, |
| 167 | MachineRegisterInfo &MRI, |
| 168 | const TargetRegisterInfo &TRI, |
| 169 | const RegisterBankInfo &RBI) { |
| 170 | assert(TII.getSubtarget().hasVFP2() && "Can't select unmerge without VFP"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 171 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 172 | // We only support G_UNMERGE_VALUES as a way to break up one DPR into two |
| 173 | // GPRs. |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 174 | unsigned VReg0 = MIB->getOperand(0).getReg(); |
| 175 | (void)VReg0; |
| 176 | assert(MRI.getType(VReg0).getSizeInBits() == 32 && |
| 177 | RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID && |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 178 | "Unsupported operand for G_UNMERGE_VALUES"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 179 | unsigned VReg1 = MIB->getOperand(1).getReg(); |
| 180 | (void)VReg1; |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 181 | assert(MRI.getType(VReg1).getSizeInBits() == 32 && |
| 182 | RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID && |
| 183 | "Unsupported operand for G_UNMERGE_VALUES"); |
| 184 | unsigned VReg2 = MIB->getOperand(2).getReg(); |
| 185 | (void)VReg2; |
| 186 | assert(MRI.getType(VReg2).getSizeInBits() == 64 && |
| 187 | RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::FPRRegBankID && |
| 188 | "Unsupported operand for G_UNMERGE_VALUES"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 189 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 190 | MIB->setDesc(TII.get(ARM::VMOVRRD)); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 191 | MIB.add(predOps(ARMCC::AL)); |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 196 | /// Select the opcode for simple extensions (that translate to a single SXT/UXT |
| 197 | /// instruction). Extension operations more complicated than that should not |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 198 | /// invoke this. Returns the original opcode if it doesn't know how to select a |
| 199 | /// better one. |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 200 | static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) { |
| 201 | using namespace TargetOpcode; |
| 202 | |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 203 | if (Size != 8 && Size != 16) |
| 204 | return Opc; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 205 | |
| 206 | if (Opc == G_SEXT) |
| 207 | return Size == 8 ? ARM::SXTB : ARM::SXTH; |
| 208 | |
| 209 | if (Opc == G_ZEXT) |
| 210 | return Size == 8 ? ARM::UXTB : ARM::UXTH; |
| 211 | |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 212 | return Opc; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 215 | /// Select the opcode for simple loads and stores. For types smaller than 32 |
| 216 | /// bits, the value will be zero extended. Returns the original opcode if it |
| 217 | /// doesn't know how to select a better one. |
| 218 | static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank, |
| 219 | unsigned Size) { |
| 220 | bool isStore = Opc == TargetOpcode::G_STORE; |
| 221 | |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 222 | if (RegBank == ARM::GPRRegBankID) { |
| 223 | switch (Size) { |
| 224 | case 1: |
| 225 | case 8: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 226 | return isStore ? ARM::STRBi12 : ARM::LDRBi12; |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 227 | case 16: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 228 | return isStore ? ARM::STRH : ARM::LDRH; |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 229 | case 32: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 230 | return isStore ? ARM::STRi12 : ARM::LDRi12; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 231 | default: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 232 | return Opc; |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 233 | } |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 236 | if (RegBank == ARM::FPRRegBankID) { |
| 237 | switch (Size) { |
| 238 | case 32: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 239 | return isStore ? ARM::VSTRS : ARM::VLDRS; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 240 | case 64: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 241 | return isStore ? ARM::VSTRD : ARM::VLDRD; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 242 | default: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 243 | return Opc; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 244 | } |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 247 | return Opc; |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame^] | 250 | static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) { |
| 251 | switch (Pred) { |
| 252 | // Needs two compares... |
| 253 | case CmpInst::FCMP_ONE: |
| 254 | case CmpInst::FCMP_UEQ: |
| 255 | default: |
| 256 | // AL is our "false" for now. The other two need more compares. |
| 257 | return ARMCC::AL; |
| 258 | case CmpInst::ICMP_EQ: |
| 259 | case CmpInst::FCMP_OEQ: |
| 260 | return ARMCC::EQ; |
| 261 | case CmpInst::ICMP_SGT: |
| 262 | case CmpInst::FCMP_OGT: |
| 263 | return ARMCC::GT; |
| 264 | case CmpInst::ICMP_SGE: |
| 265 | case CmpInst::FCMP_OGE: |
| 266 | return ARMCC::GE; |
| 267 | case CmpInst::ICMP_UGT: |
| 268 | case CmpInst::FCMP_UGT: |
| 269 | return ARMCC::HI; |
| 270 | case CmpInst::FCMP_OLT: |
| 271 | return ARMCC::MI; |
| 272 | case CmpInst::ICMP_ULE: |
| 273 | case CmpInst::FCMP_OLE: |
| 274 | return ARMCC::LS; |
| 275 | case CmpInst::FCMP_ORD: |
| 276 | return ARMCC::VC; |
| 277 | case CmpInst::FCMP_UNO: |
| 278 | return ARMCC::VS; |
| 279 | case CmpInst::FCMP_UGE: |
| 280 | return ARMCC::PL; |
| 281 | case CmpInst::ICMP_SLT: |
| 282 | case CmpInst::FCMP_ULT: |
| 283 | return ARMCC::LT; |
| 284 | case CmpInst::ICMP_SLE: |
| 285 | case CmpInst::FCMP_ULE: |
| 286 | return ARMCC::LE; |
| 287 | case CmpInst::FCMP_UNE: |
| 288 | case CmpInst::ICMP_NE: |
| 289 | return ARMCC::NE; |
| 290 | case CmpInst::ICMP_UGE: |
| 291 | return ARMCC::HS; |
| 292 | case CmpInst::ICMP_ULT: |
| 293 | return ARMCC::LO; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | bool ARMInstructionSelector::selectICmp(MachineInstrBuilder &MIB, |
| 298 | const ARMBaseInstrInfo &TII, |
| 299 | MachineRegisterInfo &MRI, |
| 300 | const TargetRegisterInfo &TRI, |
| 301 | const RegisterBankInfo &RBI) const { |
| 302 | auto &MBB = *MIB->getParent(); |
| 303 | auto InsertBefore = std::next(MIB->getIterator()); |
| 304 | auto &DebugLoc = MIB->getDebugLoc(); |
| 305 | |
| 306 | // Move 0 into the result register. |
| 307 | auto Mov0I = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::MOVi)) |
| 308 | .addDef(MRI.createVirtualRegister(&ARM::GPRRegClass)) |
| 309 | .addImm(0) |
| 310 | .add(predOps(ARMCC::AL)) |
| 311 | .add(condCodeOp()); |
| 312 | if (!constrainSelectedInstRegOperands(*Mov0I, TII, TRI, RBI)) |
| 313 | return false; |
| 314 | |
| 315 | // Perform the comparison. |
| 316 | auto LHSReg = MIB->getOperand(2).getReg(); |
| 317 | auto RHSReg = MIB->getOperand(3).getReg(); |
| 318 | assert(MRI.getType(LHSReg) == MRI.getType(RHSReg) && |
| 319 | MRI.getType(LHSReg).getSizeInBits() == 32 && |
| 320 | MRI.getType(RHSReg).getSizeInBits() == 32 && |
| 321 | "Unsupported types for comparison operation"); |
| 322 | auto CmpI = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::CMPrr)) |
| 323 | .addUse(LHSReg) |
| 324 | .addUse(RHSReg) |
| 325 | .add(predOps(ARMCC::AL)); |
| 326 | if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI)) |
| 327 | return false; |
| 328 | |
| 329 | // Move 1 into the result register if the flags say so. |
| 330 | auto ResReg = MIB->getOperand(0).getReg(); |
| 331 | auto Cond = |
| 332 | static_cast<CmpInst::Predicate>(MIB->getOperand(1).getPredicate()); |
| 333 | auto ARMCond = getComparePred(Cond); |
| 334 | if (ARMCond == ARMCC::AL) |
| 335 | return false; |
| 336 | |
| 337 | auto Mov1I = BuildMI(MBB, InsertBefore, DebugLoc, TII.get(ARM::MOVCCi)) |
| 338 | .addDef(ResReg) |
| 339 | .addUse(Mov0I->getOperand(0).getReg()) |
| 340 | .addImm(1) |
| 341 | .add(predOps(ARMCond, ARM::CPSR)); |
| 342 | if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI)) |
| 343 | return false; |
| 344 | |
| 345 | MIB->eraseFromParent(); |
| 346 | return true; |
| 347 | } |
| 348 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 349 | bool ARMInstructionSelector::select(MachineInstr &I) const { |
| 350 | assert(I.getParent() && "Instruction should be in a basic block!"); |
| 351 | assert(I.getParent()->getParent() && "Instruction should be in a function!"); |
| 352 | |
| 353 | auto &MBB = *I.getParent(); |
| 354 | auto &MF = *MBB.getParent(); |
| 355 | auto &MRI = MF.getRegInfo(); |
| 356 | |
| 357 | if (!isPreISelGenericOpcode(I.getOpcode())) { |
| 358 | if (I.isCopy()) |
| 359 | return selectCopy(I, TII, MRI, TRI, RBI); |
| 360 | |
| 361 | return true; |
| 362 | } |
| 363 | |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 364 | if (selectImpl(I)) |
| 365 | return true; |
| 366 | |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 367 | MachineInstrBuilder MIB{MF, I}; |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 368 | bool isSExt = false; |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 369 | |
| 370 | using namespace TargetOpcode; |
| 371 | switch (I.getOpcode()) { |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 372 | case G_SEXT: |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 373 | isSExt = true; |
| 374 | LLVM_FALLTHROUGH; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 375 | case G_ZEXT: { |
| 376 | LLT DstTy = MRI.getType(I.getOperand(0).getReg()); |
| 377 | // FIXME: Smaller destination sizes coming soon! |
| 378 | if (DstTy.getSizeInBits() != 32) { |
| 379 | DEBUG(dbgs() << "Unsupported destination size for extension"); |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | LLT SrcTy = MRI.getType(I.getOperand(1).getReg()); |
| 384 | unsigned SrcSize = SrcTy.getSizeInBits(); |
| 385 | switch (SrcSize) { |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 386 | case 1: { |
| 387 | // ZExt boils down to & 0x1; for SExt we also subtract that from 0 |
| 388 | I.setDesc(TII.get(ARM::ANDri)); |
| 389 | MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp()); |
| 390 | |
| 391 | if (isSExt) { |
| 392 | unsigned SExtResult = I.getOperand(0).getReg(); |
| 393 | |
| 394 | // Use a new virtual register for the result of the AND |
| 395 | unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass); |
| 396 | I.getOperand(0).setReg(AndResult); |
| 397 | |
| 398 | auto InsertBefore = std::next(I.getIterator()); |
Martin Bohme | 8396e14 | 2017-01-25 14:28:19 +0000 | [diff] [blame] | 399 | auto SubI = |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 400 | BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri)) |
| 401 | .addDef(SExtResult) |
| 402 | .addUse(AndResult) |
| 403 | .addImm(0) |
| 404 | .add(predOps(ARMCC::AL)) |
| 405 | .add(condCodeOp()); |
| 406 | if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI)) |
| 407 | return false; |
| 408 | } |
| 409 | break; |
| 410 | } |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 411 | case 8: |
| 412 | case 16: { |
| 413 | unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize); |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 414 | if (NewOpc == I.getOpcode()) |
| 415 | return false; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 416 | I.setDesc(TII.get(NewOpc)); |
| 417 | MIB.addImm(0).add(predOps(ARMCC::AL)); |
| 418 | break; |
| 419 | } |
| 420 | default: |
| 421 | DEBUG(dbgs() << "Unsupported source size for extension"); |
| 422 | return false; |
| 423 | } |
| 424 | break; |
| 425 | } |
Diana Picus | 657bfd3 | 2017-05-11 08:28:31 +0000 | [diff] [blame] | 426 | case G_ANYEXT: |
Diana Picus | 64a3343 | 2017-04-21 13:16:50 +0000 | [diff] [blame] | 427 | case G_TRUNC: { |
| 428 | // The high bits are undefined, so there's nothing special to do, just |
| 429 | // treat it as a copy. |
| 430 | auto SrcReg = I.getOperand(1).getReg(); |
| 431 | auto DstReg = I.getOperand(0).getReg(); |
| 432 | |
| 433 | const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI); |
| 434 | const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI); |
| 435 | |
| 436 | if (SrcRegBank.getID() != DstRegBank.getID()) { |
Diana Picus | 657bfd3 | 2017-05-11 08:28:31 +0000 | [diff] [blame] | 437 | DEBUG(dbgs() << "G_TRUNC/G_ANYEXT operands on different register banks\n"); |
Diana Picus | 64a3343 | 2017-04-21 13:16:50 +0000 | [diff] [blame] | 438 | return false; |
| 439 | } |
| 440 | |
| 441 | if (SrcRegBank.getID() != ARM::GPRRegBankID) { |
Diana Picus | 657bfd3 | 2017-05-11 08:28:31 +0000 | [diff] [blame] | 442 | DEBUG(dbgs() << "G_TRUNC/G_ANYEXT on non-GPR not supported yet\n"); |
Diana Picus | 64a3343 | 2017-04-21 13:16:50 +0000 | [diff] [blame] | 443 | return false; |
| 444 | } |
| 445 | |
| 446 | I.setDesc(TII.get(COPY)); |
| 447 | return selectCopy(I, TII, MRI, TRI, RBI); |
| 448 | } |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame^] | 449 | case G_ICMP: |
| 450 | return selectICmp(MIB, TII, MRI, TRI, RBI); |
Diana Picus | 9d07094 | 2017-02-28 10:14:38 +0000 | [diff] [blame] | 451 | case G_GEP: |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 452 | I.setDesc(TII.get(ARM::ADDrr)); |
Diana Picus | 8a73f55 | 2017-01-13 10:18:01 +0000 | [diff] [blame] | 453 | MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 454 | break; |
| 455 | case G_FRAME_INDEX: |
| 456 | // Add 0 to the given frame index and hope it will eventually be folded into |
| 457 | // the user(s). |
| 458 | I.setDesc(TII.get(ARM::ADDri)); |
Diana Picus | 8a73f55 | 2017-01-13 10:18:01 +0000 | [diff] [blame] | 459 | MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 460 | break; |
Diana Picus | 5a7203a | 2017-02-28 13:05:42 +0000 | [diff] [blame] | 461 | case G_CONSTANT: { |
| 462 | unsigned Reg = I.getOperand(0).getReg(); |
| 463 | if (MRI.getType(Reg).getSizeInBits() != 32) |
| 464 | return false; |
| 465 | |
| 466 | assert(RBI.getRegBank(Reg, MRI, TRI)->getID() == ARM::GPRRegBankID && |
| 467 | "Expected constant to live in a GPR"); |
| 468 | I.setDesc(TII.get(ARM::MOVi)); |
| 469 | MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 95a8aa9 | 2017-04-24 06:30:56 +0000 | [diff] [blame] | 470 | |
| 471 | auto &Val = I.getOperand(1); |
| 472 | if (Val.isCImm()) { |
| 473 | if (Val.getCImm()->getBitWidth() > 32) |
| 474 | return false; |
| 475 | Val.ChangeToImmediate(Val.getCImm()->getZExtValue()); |
| 476 | } |
| 477 | |
| 478 | if (!Val.isImm()) { |
| 479 | return false; |
| 480 | } |
| 481 | |
Diana Picus | 5a7203a | 2017-02-28 13:05:42 +0000 | [diff] [blame] | 482 | break; |
| 483 | } |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 484 | case G_STORE: |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 485 | case G_LOAD: { |
Diana Picus | 1c33c9f | 2017-02-20 14:45:58 +0000 | [diff] [blame] | 486 | const auto &MemOp = **I.memoperands_begin(); |
| 487 | if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) { |
| 488 | DEBUG(dbgs() << "Atomic load/store not supported yet\n"); |
| 489 | return false; |
| 490 | } |
| 491 | |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 492 | unsigned Reg = I.getOperand(0).getReg(); |
| 493 | unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID(); |
| 494 | |
| 495 | LLT ValTy = MRI.getType(Reg); |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 496 | const auto ValSize = ValTy.getSizeInBits(); |
| 497 | |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 498 | assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) && |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 499 | "Don't know how to load/store 64-bit value without VFP"); |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 500 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 501 | const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize); |
| 502 | if (NewOpc == G_LOAD || NewOpc == G_STORE) |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 503 | return false; |
| 504 | |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 505 | I.setDesc(TII.get(NewOpc)); |
| 506 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 507 | if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH) |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 508 | // LDRH has a funny addressing mode (there's already a FIXME for it). |
| 509 | MIB.addReg(0); |
Diana Picus | 4f8c3e1 | 2017-01-13 09:37:56 +0000 | [diff] [blame] | 510 | MIB.addImm(0).add(predOps(ARMCC::AL)); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 511 | break; |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 512 | } |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 513 | case G_MERGE_VALUES: { |
| 514 | if (!selectMergeValues(MIB, TII, MRI, TRI, RBI)) |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 515 | return false; |
| 516 | break; |
| 517 | } |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 518 | case G_UNMERGE_VALUES: { |
| 519 | if (!selectUnmergeValues(MIB, TII, MRI, TRI, RBI)) |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 520 | return false; |
| 521 | break; |
| 522 | } |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 523 | default: |
| 524 | return false; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 527 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 528 | } |