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" |
David Blaikie | 6265130 | 2017-10-26 23:39:54 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h" |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineConstantPool.h" |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
| 22 | |
| 23 | #define DEBUG_TYPE "arm-isel" |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 27 | namespace { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 28 | |
| 29 | #define GET_GLOBALISEL_PREDICATE_BITSET |
| 30 | #include "ARMGenGlobalISel.inc" |
| 31 | #undef GET_GLOBALISEL_PREDICATE_BITSET |
| 32 | |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 33 | class ARMInstructionSelector : public InstructionSelector { |
| 34 | public: |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 35 | ARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget &STI, |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 36 | const ARMRegisterBankInfo &RBI); |
| 37 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 38 | bool select(MachineInstr &I, CodeGenCoverage &CoverageInfo) const override; |
David Blaikie | 6265130 | 2017-10-26 23:39:54 +0000 | [diff] [blame] | 39 | static const char *getName() { return DEBUG_TYPE; } |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 40 | |
| 41 | private: |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 42 | bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const; |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 43 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 44 | struct CmpConstants; |
| 45 | struct InsertInfo; |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 46 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 47 | bool selectCmp(CmpConstants Helper, MachineInstrBuilder &MIB, |
| 48 | MachineRegisterInfo &MRI) const; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 49 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 50 | // Helper for inserting a comparison sequence that sets \p ResReg to either 1 |
| 51 | // if \p LHSReg and \p RHSReg are in the relationship defined by \p Cond, or |
| 52 | // \p PrevRes otherwise. In essence, it computes PrevRes OR (LHS Cond RHS). |
| 53 | bool insertComparison(CmpConstants Helper, InsertInfo I, unsigned ResReg, |
| 54 | ARMCC::CondCodes Cond, unsigned LHSReg, unsigned RHSReg, |
| 55 | unsigned PrevRes) const; |
| 56 | |
| 57 | // Set \p DestReg to \p Constant. |
| 58 | void putConstant(InsertInfo I, unsigned DestReg, unsigned Constant) const; |
| 59 | |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 60 | bool selectGlobal(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const; |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 61 | bool selectSelect(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const; |
Diana Picus | e393bc7 | 2017-10-06 15:39:16 +0000 | [diff] [blame] | 62 | bool selectShift(unsigned ShiftOpc, MachineInstrBuilder &MIB) const; |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 63 | |
| 64 | // Check if the types match and both operands have the expected size and |
| 65 | // register bank. |
| 66 | bool validOpRegPair(MachineRegisterInfo &MRI, unsigned LHS, unsigned RHS, |
| 67 | unsigned ExpectedSize, unsigned ExpectedRegBankID) const; |
| 68 | |
| 69 | // Check if the register has the expected size and register bank. |
| 70 | bool validReg(MachineRegisterInfo &MRI, unsigned Reg, unsigned ExpectedSize, |
| 71 | unsigned ExpectedRegBankID) const; |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 72 | |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 73 | const ARMBaseInstrInfo &TII; |
| 74 | const ARMBaseRegisterInfo &TRI; |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 75 | const ARMBaseTargetMachine &TM; |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 76 | const ARMRegisterBankInfo &RBI; |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 77 | const ARMSubtarget &STI; |
| 78 | |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 79 | // Store the opcodes that we might need, so we don't have to check what kind |
| 80 | // of subtarget (ARM vs Thumb) we have all the time. |
| 81 | struct OpcodeCache { |
| 82 | unsigned ZEXT16; |
| 83 | unsigned SEXT16; |
| 84 | |
| 85 | unsigned ZEXT8; |
| 86 | unsigned SEXT8; |
| 87 | |
| 88 | // Used for implementing ZEXT/SEXT from i1 |
| 89 | unsigned AND; |
| 90 | unsigned RSB; |
| 91 | |
| 92 | unsigned STORE32; |
| 93 | unsigned LOAD32; |
| 94 | |
| 95 | unsigned STORE16; |
| 96 | unsigned LOAD16; |
| 97 | |
| 98 | unsigned STORE8; |
| 99 | unsigned LOAD8; |
| 100 | |
| 101 | OpcodeCache(const ARMSubtarget &STI); |
| 102 | } const Opcodes; |
| 103 | |
| 104 | // Select the opcode for simple extensions (that translate to a single SXT/UXT |
| 105 | // instruction). Extension operations more complicated than that should not |
| 106 | // invoke this. Returns the original opcode if it doesn't know how to select a |
| 107 | // better one. |
| 108 | unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) const; |
| 109 | |
| 110 | // Select the opcode for simple loads and stores. Returns the original opcode |
| 111 | // if it doesn't know how to select a better one. |
| 112 | unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank, |
| 113 | unsigned Size) const; |
| 114 | |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 115 | #define GET_GLOBALISEL_PREDICATES_DECL |
| 116 | #include "ARMGenGlobalISel.inc" |
| 117 | #undef GET_GLOBALISEL_PREDICATES_DECL |
| 118 | |
| 119 | // We declare the temporaries used by selectImpl() in the class to minimize the |
| 120 | // cost of constructing placeholder values. |
| 121 | #define GET_GLOBALISEL_TEMPORARIES_DECL |
| 122 | #include "ARMGenGlobalISel.inc" |
| 123 | #undef GET_GLOBALISEL_TEMPORARIES_DECL |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 124 | }; |
| 125 | } // end anonymous namespace |
| 126 | |
| 127 | namespace llvm { |
| 128 | InstructionSelector * |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 129 | createARMInstructionSelector(const ARMBaseTargetMachine &TM, |
| 130 | const ARMSubtarget &STI, |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 131 | const ARMRegisterBankInfo &RBI) { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 132 | return new ARMInstructionSelector(TM, STI, RBI); |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 136 | const unsigned zero_reg = 0; |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 137 | |
| 138 | #define GET_GLOBALISEL_IMPL |
| 139 | #include "ARMGenGlobalISel.inc" |
| 140 | #undef GET_GLOBALISEL_IMPL |
| 141 | |
| 142 | ARMInstructionSelector::ARMInstructionSelector(const ARMBaseTargetMachine &TM, |
| 143 | const ARMSubtarget &STI, |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 144 | const ARMRegisterBankInfo &RBI) |
Diana Picus | 895c6aa | 2016-11-15 16:42:10 +0000 | [diff] [blame] | 145 | : InstructionSelector(), TII(*STI.getInstrInfo()), |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 146 | TRI(*STI.getRegisterInfo()), TM(TM), RBI(RBI), STI(STI), Opcodes(STI), |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 147 | #define GET_GLOBALISEL_PREDICATES_INIT |
| 148 | #include "ARMGenGlobalISel.inc" |
| 149 | #undef GET_GLOBALISEL_PREDICATES_INIT |
| 150 | #define GET_GLOBALISEL_TEMPORARIES_INIT |
| 151 | #include "ARMGenGlobalISel.inc" |
| 152 | #undef GET_GLOBALISEL_TEMPORARIES_INIT |
| 153 | { |
| 154 | } |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 155 | |
Diana Picus | 865f7fe | 2018-01-04 13:09:25 +0000 | [diff] [blame] | 156 | static const TargetRegisterClass *guessRegClass(unsigned Reg, |
| 157 | MachineRegisterInfo &MRI, |
| 158 | const TargetRegisterInfo &TRI, |
| 159 | const RegisterBankInfo &RBI) { |
| 160 | const RegisterBank *RegBank = RBI.getRegBank(Reg, MRI, TRI); |
| 161 | assert(RegBank && "Can't get reg bank for virtual register"); |
| 162 | |
| 163 | const unsigned Size = MRI.getType(Reg).getSizeInBits(); |
| 164 | assert((RegBank->getID() == ARM::GPRRegBankID || |
| 165 | RegBank->getID() == ARM::FPRRegBankID) && |
| 166 | "Unsupported reg bank"); |
| 167 | |
| 168 | if (RegBank->getID() == ARM::FPRRegBankID) { |
| 169 | if (Size == 32) |
| 170 | return &ARM::SPRRegClass; |
| 171 | else if (Size == 64) |
| 172 | return &ARM::DPRRegClass; |
Roman Tereshin | e79d656 | 2018-05-23 02:59:31 +0000 | [diff] [blame] | 173 | else if (Size == 128) |
| 174 | return &ARM::QPRRegClass; |
Diana Picus | 865f7fe | 2018-01-04 13:09:25 +0000 | [diff] [blame] | 175 | else |
| 176 | llvm_unreachable("Unsupported destination size"); |
| 177 | } |
| 178 | |
| 179 | return &ARM::GPRRegClass; |
| 180 | } |
| 181 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 182 | static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII, |
| 183 | MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, |
| 184 | const RegisterBankInfo &RBI) { |
| 185 | unsigned DstReg = I.getOperand(0).getReg(); |
| 186 | if (TargetRegisterInfo::isPhysicalRegister(DstReg)) |
| 187 | return true; |
| 188 | |
Diana Picus | 865f7fe | 2018-01-04 13:09:25 +0000 | [diff] [blame] | 189 | const TargetRegisterClass *RC = guessRegClass(DstReg, MRI, TRI, RBI); |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 190 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 191 | // No need to constrain SrcReg. It will get constrained when |
| 192 | // we hit another of its uses or its defs. |
| 193 | // Copies do not have constraints. |
| 194 | if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 195 | LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode()) |
| 196 | << " operand\n"); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 202 | static bool selectMergeValues(MachineInstrBuilder &MIB, |
| 203 | const ARMBaseInstrInfo &TII, |
| 204 | MachineRegisterInfo &MRI, |
| 205 | const TargetRegisterInfo &TRI, |
| 206 | const RegisterBankInfo &RBI) { |
| 207 | assert(TII.getSubtarget().hasVFP2() && "Can't select merge without VFP"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 208 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 209 | // 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] | 210 | // into one DPR. |
| 211 | unsigned VReg0 = MIB->getOperand(0).getReg(); |
| 212 | (void)VReg0; |
| 213 | assert(MRI.getType(VReg0).getSizeInBits() == 64 && |
| 214 | RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID && |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 215 | "Unsupported operand for G_MERGE_VALUES"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 216 | unsigned VReg1 = MIB->getOperand(1).getReg(); |
| 217 | (void)VReg1; |
| 218 | assert(MRI.getType(VReg1).getSizeInBits() == 32 && |
| 219 | RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID && |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 220 | "Unsupported operand for G_MERGE_VALUES"); |
| 221 | unsigned VReg2 = MIB->getOperand(2).getReg(); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 222 | (void)VReg2; |
| 223 | assert(MRI.getType(VReg2).getSizeInBits() == 32 && |
| 224 | RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID && |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 225 | "Unsupported operand for G_MERGE_VALUES"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 226 | |
| 227 | MIB->setDesc(TII.get(ARM::VMOVDRR)); |
| 228 | MIB.add(predOps(ARMCC::AL)); |
| 229 | |
| 230 | return true; |
| 231 | } |
| 232 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 233 | static bool selectUnmergeValues(MachineInstrBuilder &MIB, |
| 234 | const ARMBaseInstrInfo &TII, |
| 235 | MachineRegisterInfo &MRI, |
| 236 | const TargetRegisterInfo &TRI, |
| 237 | const RegisterBankInfo &RBI) { |
| 238 | assert(TII.getSubtarget().hasVFP2() && "Can't select unmerge without VFP"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 239 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 240 | // We only support G_UNMERGE_VALUES as a way to break up one DPR into two |
| 241 | // GPRs. |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 242 | unsigned VReg0 = MIB->getOperand(0).getReg(); |
| 243 | (void)VReg0; |
| 244 | assert(MRI.getType(VReg0).getSizeInBits() == 32 && |
| 245 | RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID && |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 246 | "Unsupported operand for G_UNMERGE_VALUES"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 247 | unsigned VReg1 = MIB->getOperand(1).getReg(); |
| 248 | (void)VReg1; |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 249 | assert(MRI.getType(VReg1).getSizeInBits() == 32 && |
| 250 | RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID && |
| 251 | "Unsupported operand for G_UNMERGE_VALUES"); |
| 252 | unsigned VReg2 = MIB->getOperand(2).getReg(); |
| 253 | (void)VReg2; |
| 254 | assert(MRI.getType(VReg2).getSizeInBits() == 64 && |
| 255 | RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::FPRRegBankID && |
| 256 | "Unsupported operand for G_UNMERGE_VALUES"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 257 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 258 | MIB->setDesc(TII.get(ARM::VMOVRRD)); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 259 | MIB.add(predOps(ARMCC::AL)); |
| 260 | |
| 261 | return true; |
| 262 | } |
| 263 | |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 264 | ARMInstructionSelector::OpcodeCache::OpcodeCache(const ARMSubtarget &STI) { |
| 265 | bool isThumb = STI.isThumb(); |
| 266 | |
| 267 | using namespace TargetOpcode; |
| 268 | |
| 269 | #define STORE_OPCODE(VAR, OPC) VAR = isThumb ? ARM::t2##OPC : ARM::OPC |
| 270 | STORE_OPCODE(SEXT16, SXTH); |
| 271 | STORE_OPCODE(ZEXT16, UXTH); |
| 272 | |
| 273 | STORE_OPCODE(SEXT8, SXTB); |
| 274 | STORE_OPCODE(ZEXT8, UXTB); |
| 275 | |
| 276 | STORE_OPCODE(AND, ANDri); |
| 277 | STORE_OPCODE(RSB, RSBri); |
| 278 | |
| 279 | STORE_OPCODE(STORE32, STRi12); |
| 280 | STORE_OPCODE(LOAD32, LDRi12); |
| 281 | |
| 282 | // LDRH/STRH are special... |
| 283 | STORE16 = isThumb ? ARM::t2STRHi12 : ARM::STRH; |
| 284 | LOAD16 = isThumb ? ARM::t2LDRHi12 : ARM::LDRH; |
| 285 | |
| 286 | STORE_OPCODE(STORE8, STRBi12); |
| 287 | STORE_OPCODE(LOAD8, LDRBi12); |
| 288 | #undef MAP_OPCODE |
| 289 | } |
| 290 | |
| 291 | unsigned ARMInstructionSelector::selectSimpleExtOpc(unsigned Opc, |
| 292 | unsigned Size) const { |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 293 | using namespace TargetOpcode; |
| 294 | |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 295 | if (Size != 8 && Size != 16) |
| 296 | return Opc; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 297 | |
| 298 | if (Opc == G_SEXT) |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 299 | return Size == 8 ? Opcodes.SEXT8 : Opcodes.SEXT16; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 300 | |
| 301 | if (Opc == G_ZEXT) |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 302 | return Size == 8 ? Opcodes.ZEXT8 : Opcodes.ZEXT16; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 303 | |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 304 | return Opc; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 307 | unsigned ARMInstructionSelector::selectLoadStoreOpCode(unsigned Opc, |
| 308 | unsigned RegBank, |
| 309 | unsigned Size) const { |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 310 | bool isStore = Opc == TargetOpcode::G_STORE; |
| 311 | |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 312 | if (RegBank == ARM::GPRRegBankID) { |
| 313 | switch (Size) { |
| 314 | case 1: |
| 315 | case 8: |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 316 | return isStore ? Opcodes.STORE8 : Opcodes.LOAD8; |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 317 | case 16: |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 318 | return isStore ? Opcodes.STORE16 : Opcodes.LOAD16; |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 319 | case 32: |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 320 | return isStore ? Opcodes.STORE32 : Opcodes.LOAD32; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 321 | default: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 322 | return Opc; |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 323 | } |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 326 | if (RegBank == ARM::FPRRegBankID) { |
| 327 | switch (Size) { |
| 328 | case 32: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 329 | return isStore ? ARM::VSTRS : ARM::VLDRS; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 330 | case 64: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 331 | return isStore ? ARM::VSTRD : ARM::VLDRD; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 332 | default: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 333 | return Opc; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 334 | } |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 337 | return Opc; |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 340 | // When lowering comparisons, we sometimes need to perform two compares instead |
| 341 | // of just one. Get the condition codes for both comparisons. If only one is |
| 342 | // needed, the second member of the pair is ARMCC::AL. |
| 343 | static std::pair<ARMCC::CondCodes, ARMCC::CondCodes> |
| 344 | getComparePreds(CmpInst::Predicate Pred) { |
| 345 | std::pair<ARMCC::CondCodes, ARMCC::CondCodes> Preds = {ARMCC::AL, ARMCC::AL}; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 346 | switch (Pred) { |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 347 | case CmpInst::FCMP_ONE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 348 | Preds = {ARMCC::GT, ARMCC::MI}; |
| 349 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 350 | case CmpInst::FCMP_UEQ: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 351 | Preds = {ARMCC::EQ, ARMCC::VS}; |
| 352 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 353 | case CmpInst::ICMP_EQ: |
| 354 | case CmpInst::FCMP_OEQ: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 355 | Preds.first = ARMCC::EQ; |
| 356 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 357 | case CmpInst::ICMP_SGT: |
| 358 | case CmpInst::FCMP_OGT: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 359 | Preds.first = ARMCC::GT; |
| 360 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 361 | case CmpInst::ICMP_SGE: |
| 362 | case CmpInst::FCMP_OGE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 363 | Preds.first = ARMCC::GE; |
| 364 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 365 | case CmpInst::ICMP_UGT: |
| 366 | case CmpInst::FCMP_UGT: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 367 | Preds.first = ARMCC::HI; |
| 368 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 369 | case CmpInst::FCMP_OLT: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 370 | Preds.first = ARMCC::MI; |
| 371 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 372 | case CmpInst::ICMP_ULE: |
| 373 | case CmpInst::FCMP_OLE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 374 | Preds.first = ARMCC::LS; |
| 375 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 376 | case CmpInst::FCMP_ORD: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 377 | Preds.first = ARMCC::VC; |
| 378 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 379 | case CmpInst::FCMP_UNO: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 380 | Preds.first = ARMCC::VS; |
| 381 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 382 | case CmpInst::FCMP_UGE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 383 | Preds.first = ARMCC::PL; |
| 384 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 385 | case CmpInst::ICMP_SLT: |
| 386 | case CmpInst::FCMP_ULT: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 387 | Preds.first = ARMCC::LT; |
| 388 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 389 | case CmpInst::ICMP_SLE: |
| 390 | case CmpInst::FCMP_ULE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 391 | Preds.first = ARMCC::LE; |
| 392 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 393 | case CmpInst::FCMP_UNE: |
| 394 | case CmpInst::ICMP_NE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 395 | Preds.first = ARMCC::NE; |
| 396 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 397 | case CmpInst::ICMP_UGE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 398 | Preds.first = ARMCC::HS; |
| 399 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 400 | case CmpInst::ICMP_ULT: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 401 | Preds.first = ARMCC::LO; |
| 402 | break; |
| 403 | default: |
| 404 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 405 | } |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 406 | assert(Preds.first != ARMCC::AL && "No comparisons needed?"); |
| 407 | return Preds; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 410 | struct ARMInstructionSelector::CmpConstants { |
| 411 | CmpConstants(unsigned CmpOpcode, unsigned FlagsOpcode, unsigned OpRegBank, |
| 412 | unsigned OpSize) |
| 413 | : ComparisonOpcode(CmpOpcode), ReadFlagsOpcode(FlagsOpcode), |
| 414 | OperandRegBankID(OpRegBank), OperandSize(OpSize) {} |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 415 | |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 416 | // The opcode used for performing the comparison. |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 417 | const unsigned ComparisonOpcode; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 418 | |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 419 | // The opcode used for reading the flags set by the comparison. May be |
| 420 | // ARM::INSTRUCTION_LIST_END if we don't need to read the flags. |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 421 | const unsigned ReadFlagsOpcode; |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 422 | |
| 423 | // The assumed register bank ID for the operands. |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 424 | const unsigned OperandRegBankID; |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 425 | |
Diana Picus | 21014df | 2017-07-12 09:01:54 +0000 | [diff] [blame] | 426 | // The assumed size in bits for the operands. |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 427 | const unsigned OperandSize; |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 428 | }; |
| 429 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 430 | struct ARMInstructionSelector::InsertInfo { |
| 431 | InsertInfo(MachineInstrBuilder &MIB) |
| 432 | : MBB(*MIB->getParent()), InsertBefore(std::next(MIB->getIterator())), |
| 433 | DbgLoc(MIB->getDebugLoc()) {} |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 434 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 435 | MachineBasicBlock &MBB; |
| 436 | const MachineBasicBlock::instr_iterator InsertBefore; |
| 437 | const DebugLoc &DbgLoc; |
| 438 | }; |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 439 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 440 | void ARMInstructionSelector::putConstant(InsertInfo I, unsigned DestReg, |
| 441 | unsigned Constant) const { |
| 442 | (void)BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(ARM::MOVi)) |
| 443 | .addDef(DestReg) |
| 444 | .addImm(Constant) |
| 445 | .add(predOps(ARMCC::AL)) |
| 446 | .add(condCodeOp()); |
| 447 | } |
Diana Picus | 21014df | 2017-07-12 09:01:54 +0000 | [diff] [blame] | 448 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 449 | bool ARMInstructionSelector::validOpRegPair(MachineRegisterInfo &MRI, |
| 450 | unsigned LHSReg, unsigned RHSReg, |
| 451 | unsigned ExpectedSize, |
| 452 | unsigned ExpectedRegBankID) const { |
| 453 | return MRI.getType(LHSReg) == MRI.getType(RHSReg) && |
| 454 | validReg(MRI, LHSReg, ExpectedSize, ExpectedRegBankID) && |
| 455 | validReg(MRI, RHSReg, ExpectedSize, ExpectedRegBankID); |
| 456 | } |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 457 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 458 | bool ARMInstructionSelector::validReg(MachineRegisterInfo &MRI, unsigned Reg, |
| 459 | unsigned ExpectedSize, |
| 460 | unsigned ExpectedRegBankID) const { |
| 461 | if (MRI.getType(Reg).getSizeInBits() != ExpectedSize) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 462 | LLVM_DEBUG(dbgs() << "Unexpected size for register"); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 463 | return false; |
| 464 | } |
| 465 | |
| 466 | if (RBI.getRegBank(Reg, MRI, TRI)->getID() != ExpectedRegBankID) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 467 | LLVM_DEBUG(dbgs() << "Unexpected register bank for register"); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 468 | return false; |
| 469 | } |
| 470 | |
| 471 | return true; |
| 472 | } |
| 473 | |
| 474 | bool ARMInstructionSelector::selectCmp(CmpConstants Helper, |
| 475 | MachineInstrBuilder &MIB, |
| 476 | MachineRegisterInfo &MRI) const { |
| 477 | const InsertInfo I(MIB); |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 478 | |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 479 | auto ResReg = MIB->getOperand(0).getReg(); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 480 | if (!validReg(MRI, ResReg, 1, ARM::GPRRegBankID)) |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 481 | return false; |
| 482 | |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 483 | auto Cond = |
| 484 | static_cast<CmpInst::Predicate>(MIB->getOperand(1).getPredicate()); |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 485 | if (Cond == CmpInst::FCMP_TRUE || Cond == CmpInst::FCMP_FALSE) { |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 486 | putConstant(I, ResReg, Cond == CmpInst::FCMP_TRUE ? 1 : 0); |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 487 | MIB->eraseFromParent(); |
| 488 | return true; |
| 489 | } |
| 490 | |
| 491 | auto LHSReg = MIB->getOperand(2).getReg(); |
| 492 | auto RHSReg = MIB->getOperand(3).getReg(); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 493 | if (!validOpRegPair(MRI, LHSReg, RHSReg, Helper.OperandSize, |
| 494 | Helper.OperandRegBankID)) |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 495 | return false; |
| 496 | |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 497 | auto ARMConds = getComparePreds(Cond); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 498 | auto ZeroReg = MRI.createVirtualRegister(&ARM::GPRRegClass); |
| 499 | putConstant(I, ZeroReg, 0); |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 500 | |
| 501 | if (ARMConds.second == ARMCC::AL) { |
| 502 | // Simple case, we only need one comparison and we're done. |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 503 | if (!insertComparison(Helper, I, ResReg, ARMConds.first, LHSReg, RHSReg, |
| 504 | ZeroReg)) |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 505 | return false; |
| 506 | } else { |
| 507 | // Not so simple, we need two successive comparisons. |
| 508 | auto IntermediateRes = MRI.createVirtualRegister(&ARM::GPRRegClass); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 509 | if (!insertComparison(Helper, I, IntermediateRes, ARMConds.first, LHSReg, |
| 510 | RHSReg, ZeroReg)) |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 511 | return false; |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 512 | if (!insertComparison(Helper, I, ResReg, ARMConds.second, LHSReg, RHSReg, |
| 513 | IntermediateRes)) |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 514 | return false; |
| 515 | } |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 516 | |
| 517 | MIB->eraseFromParent(); |
| 518 | return true; |
| 519 | } |
| 520 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 521 | bool ARMInstructionSelector::insertComparison(CmpConstants Helper, InsertInfo I, |
| 522 | unsigned ResReg, |
| 523 | ARMCC::CondCodes Cond, |
| 524 | unsigned LHSReg, unsigned RHSReg, |
| 525 | unsigned PrevRes) const { |
| 526 | // Perform the comparison. |
| 527 | auto CmpI = |
| 528 | BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(Helper.ComparisonOpcode)) |
| 529 | .addUse(LHSReg) |
| 530 | .addUse(RHSReg) |
| 531 | .add(predOps(ARMCC::AL)); |
| 532 | if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI)) |
| 533 | return false; |
| 534 | |
| 535 | // Read the comparison flags (if necessary). |
| 536 | if (Helper.ReadFlagsOpcode != ARM::INSTRUCTION_LIST_END) { |
| 537 | auto ReadI = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, |
| 538 | TII.get(Helper.ReadFlagsOpcode)) |
| 539 | .add(predOps(ARMCC::AL)); |
| 540 | if (!constrainSelectedInstRegOperands(*ReadI, TII, TRI, RBI)) |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | // Select either 1 or the previous result based on the value of the flags. |
| 545 | auto Mov1I = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(ARM::MOVCCi)) |
| 546 | .addDef(ResReg) |
| 547 | .addUse(PrevRes) |
| 548 | .addImm(1) |
| 549 | .add(predOps(Cond, ARM::CPSR)); |
| 550 | if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI)) |
| 551 | return false; |
| 552 | |
| 553 | return true; |
| 554 | } |
| 555 | |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 556 | bool ARMInstructionSelector::selectGlobal(MachineInstrBuilder &MIB, |
| 557 | MachineRegisterInfo &MRI) const { |
Diana Picus | abb0886 | 2017-09-05 07:57:41 +0000 | [diff] [blame] | 558 | if ((STI.isROPI() || STI.isRWPI()) && !STI.isTargetELF()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 559 | LLVM_DEBUG(dbgs() << "ROPI and RWPI only supported for ELF\n"); |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 560 | return false; |
| 561 | } |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 562 | |
| 563 | auto GV = MIB->getOperand(1).getGlobal(); |
| 564 | if (GV->isThreadLocal()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 565 | LLVM_DEBUG(dbgs() << "TLS variables not supported yet\n"); |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 566 | return false; |
| 567 | } |
| 568 | |
| 569 | auto &MBB = *MIB->getParent(); |
| 570 | auto &MF = *MBB.getParent(); |
| 571 | |
Diana Picus | ac15473 | 2017-09-05 08:22:47 +0000 | [diff] [blame] | 572 | bool UseMovt = STI.useMovt(MF); |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 573 | |
Matt Arsenault | 41e5ac4 | 2018-03-14 00:36:23 +0000 | [diff] [blame] | 574 | unsigned Size = TM.getPointerSize(0); |
Diana Picus | c9f29c6 | 2017-08-29 09:47:55 +0000 | [diff] [blame] | 575 | unsigned Alignment = 4; |
Diana Picus | abb0886 | 2017-09-05 07:57:41 +0000 | [diff] [blame] | 576 | |
| 577 | auto addOpsForConstantPoolLoad = [&MF, Alignment, |
| 578 | Size](MachineInstrBuilder &MIB, |
| 579 | const GlobalValue *GV, bool IsSBREL) { |
| 580 | assert(MIB->getOpcode() == ARM::LDRi12 && "Unsupported instruction"); |
| 581 | auto ConstPool = MF.getConstantPool(); |
| 582 | auto CPIndex = |
| 583 | // For SB relative entries we need a target-specific constant pool. |
| 584 | // Otherwise, just use a regular constant pool entry. |
| 585 | IsSBREL |
| 586 | ? ConstPool->getConstantPoolIndex( |
| 587 | ARMConstantPoolConstant::Create(GV, ARMCP::SBREL), Alignment) |
| 588 | : ConstPool->getConstantPoolIndex(GV, Alignment); |
| 589 | MIB.addConstantPoolIndex(CPIndex, /*Offset*/ 0, /*TargetFlags*/ 0) |
| 590 | .addMemOperand( |
| 591 | MF.getMachineMemOperand(MachinePointerInfo::getConstantPool(MF), |
| 592 | MachineMemOperand::MOLoad, Size, Alignment)) |
| 593 | .addImm(0) |
| 594 | .add(predOps(ARMCC::AL)); |
| 595 | }; |
| 596 | |
Diana Picus | c9f29c6 | 2017-08-29 09:47:55 +0000 | [diff] [blame] | 597 | if (TM.isPositionIndependent()) { |
Diana Picus | ac15473 | 2017-09-05 08:22:47 +0000 | [diff] [blame] | 598 | bool Indirect = STI.isGVIndirectSymbol(GV); |
Diana Picus | c9f29c6 | 2017-08-29 09:47:55 +0000 | [diff] [blame] | 599 | // FIXME: Taking advantage of MOVT for ELF is pretty involved, so we don't |
| 600 | // support it yet. See PR28229. |
| 601 | unsigned Opc = |
Diana Picus | ac15473 | 2017-09-05 08:22:47 +0000 | [diff] [blame] | 602 | UseMovt && !STI.isTargetELF() |
Diana Picus | c9f29c6 | 2017-08-29 09:47:55 +0000 | [diff] [blame] | 603 | ? (Indirect ? ARM::MOV_ga_pcrel_ldr : ARM::MOV_ga_pcrel) |
| 604 | : (Indirect ? ARM::LDRLIT_ga_pcrel_ldr : ARM::LDRLIT_ga_pcrel); |
| 605 | MIB->setDesc(TII.get(Opc)); |
| 606 | |
Evgeniy Stepanov | 76d5ac4 | 2017-11-13 20:45:38 +0000 | [diff] [blame] | 607 | int TargetFlags = ARMII::MO_NO_FLAG; |
Diana Picus | ac15473 | 2017-09-05 08:22:47 +0000 | [diff] [blame] | 608 | if (STI.isTargetDarwin()) |
Evgeniy Stepanov | 76d5ac4 | 2017-11-13 20:45:38 +0000 | [diff] [blame] | 609 | TargetFlags |= ARMII::MO_NONLAZY; |
| 610 | if (STI.isGVInGOT(GV)) |
| 611 | TargetFlags |= ARMII::MO_GOT; |
| 612 | MIB->getOperand(1).setTargetFlags(TargetFlags); |
Diana Picus | c9f29c6 | 2017-08-29 09:47:55 +0000 | [diff] [blame] | 613 | |
| 614 | if (Indirect) |
| 615 | MIB.addMemOperand(MF.getMachineMemOperand( |
| 616 | MachinePointerInfo::getGOT(MF), MachineMemOperand::MOLoad, |
Matt Arsenault | 41e5ac4 | 2018-03-14 00:36:23 +0000 | [diff] [blame] | 617 | TM.getProgramPointerSize(), Alignment)); |
Diana Picus | c9f29c6 | 2017-08-29 09:47:55 +0000 | [diff] [blame] | 618 | |
Diana Picus | ac15473 | 2017-09-05 08:22:47 +0000 | [diff] [blame] | 619 | return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); |
Diana Picus | c9f29c6 | 2017-08-29 09:47:55 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Diana Picus | f9597911 | 2017-09-01 11:13:39 +0000 | [diff] [blame] | 622 | bool isReadOnly = STI.getTargetLowering()->isReadOnly(GV); |
| 623 | if (STI.isROPI() && isReadOnly) { |
| 624 | unsigned Opc = UseMovt ? ARM::MOV_ga_pcrel : ARM::LDRLIT_ga_pcrel; |
| 625 | MIB->setDesc(TII.get(Opc)); |
| 626 | return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); |
| 627 | } |
Diana Picus | abb0886 | 2017-09-05 07:57:41 +0000 | [diff] [blame] | 628 | if (STI.isRWPI() && !isReadOnly) { |
| 629 | auto Offset = MRI.createVirtualRegister(&ARM::GPRRegClass); |
| 630 | MachineInstrBuilder OffsetMIB; |
| 631 | if (UseMovt) { |
| 632 | OffsetMIB = BuildMI(MBB, *MIB, MIB->getDebugLoc(), |
| 633 | TII.get(ARM::MOVi32imm), Offset); |
| 634 | OffsetMIB.addGlobalAddress(GV, /*Offset*/ 0, ARMII::MO_SBREL); |
| 635 | } else { |
| 636 | // Load the offset from the constant pool. |
| 637 | OffsetMIB = |
| 638 | BuildMI(MBB, *MIB, MIB->getDebugLoc(), TII.get(ARM::LDRi12), Offset); |
| 639 | addOpsForConstantPoolLoad(OffsetMIB, GV, /*IsSBREL*/ true); |
| 640 | } |
| 641 | if (!constrainSelectedInstRegOperands(*OffsetMIB, TII, TRI, RBI)) |
| 642 | return false; |
| 643 | |
| 644 | // Add the offset to the SB register. |
| 645 | MIB->setDesc(TII.get(ARM::ADDrr)); |
| 646 | MIB->RemoveOperand(1); |
| 647 | MIB.addReg(ARM::R9) // FIXME: don't hardcode R9 |
| 648 | .addReg(Offset) |
| 649 | .add(predOps(ARMCC::AL)) |
| 650 | .add(condCodeOp()); |
| 651 | |
| 652 | return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); |
| 653 | } |
Diana Picus | f9597911 | 2017-09-01 11:13:39 +0000 | [diff] [blame] | 654 | |
Diana Picus | ac15473 | 2017-09-05 08:22:47 +0000 | [diff] [blame] | 655 | if (STI.isTargetELF()) { |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 656 | if (UseMovt) { |
| 657 | MIB->setDesc(TII.get(ARM::MOVi32imm)); |
| 658 | } else { |
| 659 | // Load the global's address from the constant pool. |
| 660 | MIB->setDesc(TII.get(ARM::LDRi12)); |
| 661 | MIB->RemoveOperand(1); |
Diana Picus | abb0886 | 2017-09-05 07:57:41 +0000 | [diff] [blame] | 662 | addOpsForConstantPoolLoad(MIB, GV, /*IsSBREL*/ false); |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 663 | } |
Diana Picus | ac15473 | 2017-09-05 08:22:47 +0000 | [diff] [blame] | 664 | } else if (STI.isTargetMachO()) { |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 665 | if (UseMovt) |
| 666 | MIB->setDesc(TII.get(ARM::MOVi32imm)); |
| 667 | else |
| 668 | MIB->setDesc(TII.get(ARM::LDRLIT_ga_abs)); |
| 669 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 670 | LLVM_DEBUG(dbgs() << "Object format not supported yet\n"); |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 671 | return false; |
| 672 | } |
| 673 | |
| 674 | return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); |
| 675 | } |
| 676 | |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 677 | bool ARMInstructionSelector::selectSelect(MachineInstrBuilder &MIB, |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 678 | MachineRegisterInfo &MRI) const { |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 679 | auto &MBB = *MIB->getParent(); |
| 680 | auto InsertBefore = std::next(MIB->getIterator()); |
Diana Picus | 7736737 | 2017-07-07 08:53:27 +0000 | [diff] [blame] | 681 | auto &DbgLoc = MIB->getDebugLoc(); |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 682 | |
| 683 | // Compare the condition to 0. |
| 684 | auto CondReg = MIB->getOperand(1).getReg(); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 685 | assert(validReg(MRI, CondReg, 1, ARM::GPRRegBankID) && |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 686 | "Unsupported types for select operation"); |
Diana Picus | 7736737 | 2017-07-07 08:53:27 +0000 | [diff] [blame] | 687 | auto CmpI = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(ARM::CMPri)) |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 688 | .addUse(CondReg) |
| 689 | .addImm(0) |
| 690 | .add(predOps(ARMCC::AL)); |
| 691 | if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI)) |
| 692 | return false; |
| 693 | |
| 694 | // Move a value into the result register based on the result of the |
| 695 | // comparison. |
| 696 | auto ResReg = MIB->getOperand(0).getReg(); |
| 697 | auto TrueReg = MIB->getOperand(2).getReg(); |
| 698 | auto FalseReg = MIB->getOperand(3).getReg(); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 699 | assert(validOpRegPair(MRI, ResReg, TrueReg, 32, ARM::GPRRegBankID) && |
| 700 | validOpRegPair(MRI, TrueReg, FalseReg, 32, ARM::GPRRegBankID) && |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 701 | "Unsupported types for select operation"); |
Diana Picus | 7736737 | 2017-07-07 08:53:27 +0000 | [diff] [blame] | 702 | auto Mov1I = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(ARM::MOVCCr)) |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 703 | .addDef(ResReg) |
| 704 | .addUse(TrueReg) |
| 705 | .addUse(FalseReg) |
| 706 | .add(predOps(ARMCC::EQ, ARM::CPSR)); |
| 707 | if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI)) |
| 708 | return false; |
| 709 | |
| 710 | MIB->eraseFromParent(); |
| 711 | return true; |
| 712 | } |
| 713 | |
Diana Picus | e393bc7 | 2017-10-06 15:39:16 +0000 | [diff] [blame] | 714 | bool ARMInstructionSelector::selectShift(unsigned ShiftOpc, |
| 715 | MachineInstrBuilder &MIB) const { |
| 716 | MIB->setDesc(TII.get(ARM::MOVsr)); |
| 717 | MIB.addImm(ShiftOpc); |
| 718 | MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); |
| 719 | return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); |
| 720 | } |
| 721 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 722 | bool ARMInstructionSelector::select(MachineInstr &I, |
| 723 | CodeGenCoverage &CoverageInfo) const { |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 724 | assert(I.getParent() && "Instruction should be in a basic block!"); |
| 725 | assert(I.getParent()->getParent() && "Instruction should be in a function!"); |
| 726 | |
| 727 | auto &MBB = *I.getParent(); |
| 728 | auto &MF = *MBB.getParent(); |
| 729 | auto &MRI = MF.getRegInfo(); |
| 730 | |
| 731 | if (!isPreISelGenericOpcode(I.getOpcode())) { |
| 732 | if (I.isCopy()) |
| 733 | return selectCopy(I, TII, MRI, TRI, RBI); |
| 734 | |
| 735 | return true; |
| 736 | } |
| 737 | |
Diana Picus | 6877385 | 2017-12-22 11:09:18 +0000 | [diff] [blame] | 738 | using namespace TargetOpcode; |
Diana Picus | 6877385 | 2017-12-22 11:09:18 +0000 | [diff] [blame] | 739 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 740 | if (selectImpl(I, CoverageInfo)) |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 741 | return true; |
| 742 | |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 743 | MachineInstrBuilder MIB{MF, I}; |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 744 | bool isSExt = false; |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 745 | |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 746 | switch (I.getOpcode()) { |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 747 | case G_SEXT: |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 748 | isSExt = true; |
| 749 | LLVM_FALLTHROUGH; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 750 | case G_ZEXT: { |
| 751 | LLT DstTy = MRI.getType(I.getOperand(0).getReg()); |
| 752 | // FIXME: Smaller destination sizes coming soon! |
| 753 | if (DstTy.getSizeInBits() != 32) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 754 | LLVM_DEBUG(dbgs() << "Unsupported destination size for extension"); |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 755 | return false; |
| 756 | } |
| 757 | |
| 758 | LLT SrcTy = MRI.getType(I.getOperand(1).getReg()); |
| 759 | unsigned SrcSize = SrcTy.getSizeInBits(); |
| 760 | switch (SrcSize) { |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 761 | case 1: { |
| 762 | // ZExt boils down to & 0x1; for SExt we also subtract that from 0 |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 763 | I.setDesc(TII.get(Opcodes.AND)); |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 764 | MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp()); |
| 765 | |
| 766 | if (isSExt) { |
| 767 | unsigned SExtResult = I.getOperand(0).getReg(); |
| 768 | |
| 769 | // Use a new virtual register for the result of the AND |
| 770 | unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass); |
| 771 | I.getOperand(0).setReg(AndResult); |
| 772 | |
| 773 | auto InsertBefore = std::next(I.getIterator()); |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 774 | auto SubI = |
| 775 | BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(Opcodes.RSB)) |
| 776 | .addDef(SExtResult) |
| 777 | .addUse(AndResult) |
| 778 | .addImm(0) |
| 779 | .add(predOps(ARMCC::AL)) |
| 780 | .add(condCodeOp()); |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 781 | if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI)) |
| 782 | return false; |
| 783 | } |
| 784 | break; |
| 785 | } |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 786 | case 8: |
| 787 | case 16: { |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 788 | unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize); |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 789 | if (NewOpc == I.getOpcode()) |
| 790 | return false; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 791 | I.setDesc(TII.get(NewOpc)); |
| 792 | MIB.addImm(0).add(predOps(ARMCC::AL)); |
| 793 | break; |
| 794 | } |
| 795 | default: |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 796 | LLVM_DEBUG(dbgs() << "Unsupported source size for extension"); |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 797 | return false; |
| 798 | } |
| 799 | break; |
| 800 | } |
Diana Picus | 657bfd3 | 2017-05-11 08:28:31 +0000 | [diff] [blame] | 801 | case G_ANYEXT: |
Diana Picus | 64a3343 | 2017-04-21 13:16:50 +0000 | [diff] [blame] | 802 | case G_TRUNC: { |
| 803 | // The high bits are undefined, so there's nothing special to do, just |
| 804 | // treat it as a copy. |
| 805 | auto SrcReg = I.getOperand(1).getReg(); |
| 806 | auto DstReg = I.getOperand(0).getReg(); |
| 807 | |
| 808 | const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI); |
| 809 | const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI); |
| 810 | |
Diana Picus | 75ce852 | 2017-12-20 11:27:10 +0000 | [diff] [blame] | 811 | if (SrcRegBank.getID() == ARM::FPRRegBankID) { |
| 812 | // This should only happen in the obscure case where we have put a 64-bit |
| 813 | // integer into a D register. Get it out of there and keep only the |
| 814 | // interesting part. |
| 815 | assert(I.getOpcode() == G_TRUNC && "Unsupported operand for G_ANYEXT"); |
| 816 | assert(DstRegBank.getID() == ARM::GPRRegBankID && |
| 817 | "Unsupported combination of register banks"); |
| 818 | assert(MRI.getType(SrcReg).getSizeInBits() == 64 && "Unsupported size"); |
| 819 | assert(MRI.getType(DstReg).getSizeInBits() <= 32 && "Unsupported size"); |
| 820 | |
| 821 | unsigned IgnoredBits = MRI.createVirtualRegister(&ARM::GPRRegClass); |
| 822 | auto InsertBefore = std::next(I.getIterator()); |
| 823 | auto MovI = |
| 824 | BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::VMOVRRD)) |
| 825 | .addDef(DstReg) |
| 826 | .addDef(IgnoredBits) |
| 827 | .addUse(SrcReg) |
| 828 | .add(predOps(ARMCC::AL)); |
| 829 | if (!constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI)) |
| 830 | return false; |
| 831 | |
| 832 | MIB->eraseFromParent(); |
| 833 | return true; |
| 834 | } |
| 835 | |
Diana Picus | 64a3343 | 2017-04-21 13:16:50 +0000 | [diff] [blame] | 836 | if (SrcRegBank.getID() != DstRegBank.getID()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 837 | LLVM_DEBUG( |
| 838 | dbgs() << "G_TRUNC/G_ANYEXT operands on different register banks\n"); |
Diana Picus | 64a3343 | 2017-04-21 13:16:50 +0000 | [diff] [blame] | 839 | return false; |
| 840 | } |
| 841 | |
| 842 | if (SrcRegBank.getID() != ARM::GPRRegBankID) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 843 | LLVM_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] | 844 | return false; |
| 845 | } |
| 846 | |
| 847 | I.setDesc(TII.get(COPY)); |
| 848 | return selectCopy(I, TII, MRI, TRI, RBI); |
| 849 | } |
Diana Picus | 37ae9f6 | 2018-01-04 10:54:57 +0000 | [diff] [blame] | 850 | case G_CONSTANT: { |
| 851 | if (!MRI.getType(I.getOperand(0).getReg()).isPointer()) { |
| 852 | // Non-pointer constants should be handled by TableGen. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 853 | LLVM_DEBUG(dbgs() << "Unsupported constant type\n"); |
Diana Picus | 37ae9f6 | 2018-01-04 10:54:57 +0000 | [diff] [blame] | 854 | return false; |
| 855 | } |
| 856 | |
| 857 | auto &Val = I.getOperand(1); |
| 858 | if (Val.isCImm()) { |
| 859 | if (!Val.getCImm()->isZero()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 860 | LLVM_DEBUG(dbgs() << "Unsupported pointer constant value\n"); |
Diana Picus | 37ae9f6 | 2018-01-04 10:54:57 +0000 | [diff] [blame] | 861 | return false; |
| 862 | } |
| 863 | Val.ChangeToImmediate(0); |
| 864 | } else { |
| 865 | assert(Val.isImm() && "Unexpected operand for G_CONSTANT"); |
| 866 | if (Val.getImm() != 0) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 867 | LLVM_DEBUG(dbgs() << "Unsupported pointer constant value\n"); |
Diana Picus | 37ae9f6 | 2018-01-04 10:54:57 +0000 | [diff] [blame] | 868 | return false; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | I.setDesc(TII.get(ARM::MOVi)); |
| 873 | MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); |
| 874 | break; |
| 875 | } |
Diana Picus | 28a6d0e | 2017-12-22 13:05:51 +0000 | [diff] [blame] | 876 | case G_INTTOPTR: |
| 877 | case G_PTRTOINT: { |
| 878 | auto SrcReg = I.getOperand(1).getReg(); |
| 879 | auto DstReg = I.getOperand(0).getReg(); |
| 880 | |
| 881 | const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI); |
| 882 | const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI); |
| 883 | |
| 884 | if (SrcRegBank.getID() != DstRegBank.getID()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 885 | LLVM_DEBUG( |
| 886 | dbgs() |
| 887 | << "G_INTTOPTR/G_PTRTOINT operands on different register banks\n"); |
Diana Picus | 28a6d0e | 2017-12-22 13:05:51 +0000 | [diff] [blame] | 888 | return false; |
| 889 | } |
| 890 | |
| 891 | if (SrcRegBank.getID() != ARM::GPRRegBankID) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 892 | LLVM_DEBUG( |
| 893 | dbgs() << "G_INTTOPTR/G_PTRTOINT on non-GPR not supported yet\n"); |
Diana Picus | 28a6d0e | 2017-12-22 13:05:51 +0000 | [diff] [blame] | 894 | return false; |
| 895 | } |
| 896 | |
| 897 | I.setDesc(TII.get(COPY)); |
| 898 | return selectCopy(I, TII, MRI, TRI, RBI); |
| 899 | } |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 900 | case G_SELECT: |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 901 | return selectSelect(MIB, MRI); |
| 902 | case G_ICMP: { |
| 903 | CmpConstants Helper(ARM::CMPrr, ARM::INSTRUCTION_LIST_END, |
| 904 | ARM::GPRRegBankID, 32); |
| 905 | return selectCmp(Helper, MIB, MRI); |
| 906 | } |
Diana Picus | 21014df | 2017-07-12 09:01:54 +0000 | [diff] [blame] | 907 | case G_FCMP: { |
Diana Picus | ac15473 | 2017-09-05 08:22:47 +0000 | [diff] [blame] | 908 | assert(STI.hasVFP2() && "Can't select fcmp without VFP"); |
Diana Picus | 21014df | 2017-07-12 09:01:54 +0000 | [diff] [blame] | 909 | |
| 910 | unsigned OpReg = I.getOperand(2).getReg(); |
| 911 | unsigned Size = MRI.getType(OpReg).getSizeInBits(); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 912 | |
Diana Picus | ac15473 | 2017-09-05 08:22:47 +0000 | [diff] [blame] | 913 | if (Size == 64 && STI.isFPOnlySP()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 914 | LLVM_DEBUG(dbgs() << "Subtarget only supports single precision"); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 915 | return false; |
| 916 | } |
| 917 | if (Size != 32 && Size != 64) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 918 | LLVM_DEBUG(dbgs() << "Unsupported size for G_FCMP operand"); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 919 | return false; |
Diana Picus | 21014df | 2017-07-12 09:01:54 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 922 | CmpConstants Helper(Size == 32 ? ARM::VCMPS : ARM::VCMPD, ARM::FMSTAT, |
| 923 | ARM::FPRRegBankID, Size); |
| 924 | return selectCmp(Helper, MIB, MRI); |
Diana Picus | 21014df | 2017-07-12 09:01:54 +0000 | [diff] [blame] | 925 | } |
Diana Picus | e393bc7 | 2017-10-06 15:39:16 +0000 | [diff] [blame] | 926 | case G_LSHR: |
| 927 | return selectShift(ARM_AM::ShiftOpc::lsr, MIB); |
| 928 | case G_ASHR: |
| 929 | return selectShift(ARM_AM::ShiftOpc::asr, MIB); |
| 930 | case G_SHL: { |
| 931 | return selectShift(ARM_AM::ShiftOpc::lsl, MIB); |
| 932 | } |
Diana Picus | 9d07094 | 2017-02-28 10:14:38 +0000 | [diff] [blame] | 933 | case G_GEP: |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 934 | I.setDesc(TII.get(ARM::ADDrr)); |
Diana Picus | 8a73f55 | 2017-01-13 10:18:01 +0000 | [diff] [blame] | 935 | MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 936 | break; |
| 937 | case G_FRAME_INDEX: |
| 938 | // Add 0 to the given frame index and hope it will eventually be folded into |
| 939 | // the user(s). |
| 940 | I.setDesc(TII.get(ARM::ADDri)); |
Diana Picus | 8a73f55 | 2017-01-13 10:18:01 +0000 | [diff] [blame] | 941 | MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 942 | break; |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 943 | case G_GLOBAL_VALUE: |
| 944 | return selectGlobal(MIB, MRI); |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 945 | case G_STORE: |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 946 | case G_LOAD: { |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 947 | const auto &MemOp = **I.memoperands_begin(); |
| 948 | if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 949 | LLVM_DEBUG(dbgs() << "Atomic load/store not supported yet\n"); |
Daniel Sanders | 3c1c4c0 | 2017-12-05 05:52:07 +0000 | [diff] [blame] | 950 | return false; |
| 951 | } |
| 952 | |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 953 | unsigned Reg = I.getOperand(0).getReg(); |
| 954 | unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID(); |
| 955 | |
| 956 | LLT ValTy = MRI.getType(Reg); |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 957 | const auto ValSize = ValTy.getSizeInBits(); |
| 958 | |
Diana Picus | ac15473 | 2017-09-05 08:22:47 +0000 | [diff] [blame] | 959 | assert((ValSize != 64 || STI.hasVFP2()) && |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 960 | "Don't know how to load/store 64-bit value without VFP"); |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 961 | |
Diana Picus | 813af0d | 2018-12-14 12:37:24 +0000 | [diff] [blame] | 962 | const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize); |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 963 | if (NewOpc == G_LOAD || NewOpc == G_STORE) |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 964 | return false; |
| 965 | |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 966 | I.setDesc(TII.get(NewOpc)); |
| 967 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 968 | if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH) |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 969 | // LDRH has a funny addressing mode (there's already a FIXME for it). |
| 970 | MIB.addReg(0); |
Diana Picus | 4f8c3e1 | 2017-01-13 09:37:56 +0000 | [diff] [blame] | 971 | MIB.addImm(0).add(predOps(ARMCC::AL)); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 972 | break; |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 973 | } |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 974 | case G_MERGE_VALUES: { |
| 975 | if (!selectMergeValues(MIB, TII, MRI, TRI, RBI)) |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 976 | return false; |
| 977 | break; |
| 978 | } |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 979 | case G_UNMERGE_VALUES: { |
| 980 | if (!selectUnmergeValues(MIB, TII, MRI, TRI, RBI)) |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 981 | return false; |
| 982 | break; |
| 983 | } |
Diana Picus | 87a7067 | 2017-07-14 09:46:06 +0000 | [diff] [blame] | 984 | case G_BRCOND: { |
| 985 | if (!validReg(MRI, I.getOperand(0).getReg(), 1, ARM::GPRRegBankID)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 986 | LLVM_DEBUG(dbgs() << "Unsupported condition register for G_BRCOND"); |
Diana Picus | 87a7067 | 2017-07-14 09:46:06 +0000 | [diff] [blame] | 987 | return false; |
| 988 | } |
| 989 | |
| 990 | // Set the flags. |
| 991 | auto Test = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(ARM::TSTri)) |
| 992 | .addReg(I.getOperand(0).getReg()) |
| 993 | .addImm(1) |
| 994 | .add(predOps(ARMCC::AL)); |
| 995 | if (!constrainSelectedInstRegOperands(*Test, TII, TRI, RBI)) |
| 996 | return false; |
| 997 | |
| 998 | // Branch conditionally. |
| 999 | auto Branch = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(ARM::Bcc)) |
| 1000 | .add(I.getOperand(1)) |
Diana Picus | 863b5b0 | 2017-11-29 14:20:06 +0000 | [diff] [blame] | 1001 | .add(predOps(ARMCC::NE, ARM::CPSR)); |
Diana Picus | 87a7067 | 2017-07-14 09:46:06 +0000 | [diff] [blame] | 1002 | if (!constrainSelectedInstRegOperands(*Branch, TII, TRI, RBI)) |
| 1003 | return false; |
| 1004 | I.eraseFromParent(); |
| 1005 | return true; |
| 1006 | } |
Diana Picus | 865f7fe | 2018-01-04 13:09:25 +0000 | [diff] [blame] | 1007 | case G_PHI: { |
| 1008 | I.setDesc(TII.get(PHI)); |
| 1009 | |
| 1010 | unsigned DstReg = I.getOperand(0).getReg(); |
| 1011 | const TargetRegisterClass *RC = guessRegClass(DstReg, MRI, TRI, RBI); |
| 1012 | if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) { |
| 1013 | break; |
| 1014 | } |
| 1015 | |
| 1016 | return true; |
| 1017 | } |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 1018 | default: |
| 1019 | return false; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 1022 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 1023 | } |