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