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