Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 1 | //===- ARMInstructionSelector.cpp ----------------------------*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// \file |
| 10 | /// This file implements the targeting of the InstructionSelector class for ARM. |
| 11 | /// \todo This should be generated by TableGen. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 14 | #include "ARMRegisterBankInfo.h" |
| 15 | #include "ARMSubtarget.h" |
| 16 | #include "ARMTargetMachine.h" |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/GlobalISel/InstructionSelector.h" |
Diana Picus | 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 | |
Daniel Sanders | 6ab0daa | 2017-07-04 14:35:06 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h" |
| 25 | |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 28 | namespace { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 29 | |
| 30 | #define GET_GLOBALISEL_PREDICATE_BITSET |
| 31 | #include "ARMGenGlobalISel.inc" |
| 32 | #undef GET_GLOBALISEL_PREDICATE_BITSET |
| 33 | |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 34 | class ARMInstructionSelector : public InstructionSelector { |
| 35 | public: |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 36 | ARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget &STI, |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 37 | const ARMRegisterBankInfo &RBI); |
| 38 | |
| 39 | bool select(MachineInstr &I) const override; |
| 40 | |
| 41 | private: |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 42 | bool selectImpl(MachineInstr &I) const; |
| 43 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 44 | struct CmpConstants; |
| 45 | struct InsertInfo; |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 46 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 47 | bool selectCmp(CmpConstants Helper, MachineInstrBuilder &MIB, |
| 48 | MachineRegisterInfo &MRI) const; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 49 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 50 | // Helper for inserting a comparison sequence that sets \p ResReg to either 1 |
| 51 | // if \p LHSReg and \p RHSReg are in the relationship defined by \p Cond, or |
| 52 | // \p PrevRes otherwise. In essence, it computes PrevRes OR (LHS Cond RHS). |
| 53 | bool insertComparison(CmpConstants Helper, InsertInfo I, unsigned ResReg, |
| 54 | ARMCC::CondCodes Cond, unsigned LHSReg, unsigned RHSReg, |
| 55 | unsigned PrevRes) const; |
| 56 | |
| 57 | // Set \p DestReg to \p Constant. |
| 58 | void putConstant(InsertInfo I, unsigned DestReg, unsigned Constant) const; |
| 59 | |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 60 | bool selectGlobal(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const; |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 61 | bool selectSelect(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const; |
| 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 | |
| 78 | #define GET_GLOBALISEL_PREDICATES_DECL |
| 79 | #include "ARMGenGlobalISel.inc" |
| 80 | #undef GET_GLOBALISEL_PREDICATES_DECL |
| 81 | |
| 82 | // We declare the temporaries used by selectImpl() in the class to minimize the |
| 83 | // cost of constructing placeholder values. |
| 84 | #define GET_GLOBALISEL_TEMPORARIES_DECL |
| 85 | #include "ARMGenGlobalISel.inc" |
| 86 | #undef GET_GLOBALISEL_TEMPORARIES_DECL |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 87 | }; |
| 88 | } // end anonymous namespace |
| 89 | |
| 90 | namespace llvm { |
| 91 | InstructionSelector * |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 92 | createARMInstructionSelector(const ARMBaseTargetMachine &TM, |
| 93 | const ARMSubtarget &STI, |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 94 | const ARMRegisterBankInfo &RBI) { |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 95 | return new ARMInstructionSelector(TM, STI, RBI); |
Diana Picus | 674888d | 2017-04-28 09:10:38 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Daniel Sanders | 8e82af2 | 2017-07-27 11:03:45 +0000 | [diff] [blame] | 99 | const unsigned zero_reg = 0; |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 100 | |
| 101 | #define GET_GLOBALISEL_IMPL |
| 102 | #include "ARMGenGlobalISel.inc" |
| 103 | #undef GET_GLOBALISEL_IMPL |
| 104 | |
| 105 | ARMInstructionSelector::ARMInstructionSelector(const ARMBaseTargetMachine &TM, |
| 106 | const ARMSubtarget &STI, |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 107 | const ARMRegisterBankInfo &RBI) |
Diana Picus | 895c6aa | 2016-11-15 16:42:10 +0000 | [diff] [blame] | 108 | : InstructionSelector(), TII(*STI.getInstrInfo()), |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 109 | TRI(*STI.getRegisterInfo()), TM(TM), RBI(RBI), STI(STI), |
| 110 | #define GET_GLOBALISEL_PREDICATES_INIT |
| 111 | #include "ARMGenGlobalISel.inc" |
| 112 | #undef GET_GLOBALISEL_PREDICATES_INIT |
| 113 | #define GET_GLOBALISEL_TEMPORARIES_INIT |
| 114 | #include "ARMGenGlobalISel.inc" |
| 115 | #undef GET_GLOBALISEL_TEMPORARIES_INIT |
| 116 | { |
| 117 | } |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 118 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 119 | static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII, |
| 120 | MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, |
| 121 | const RegisterBankInfo &RBI) { |
| 122 | unsigned DstReg = I.getOperand(0).getReg(); |
| 123 | if (TargetRegisterInfo::isPhysicalRegister(DstReg)) |
| 124 | return true; |
| 125 | |
| 126 | const RegisterBank *RegBank = RBI.getRegBank(DstReg, MRI, TRI); |
Benjamin Kramer | 24bf868 | 2016-12-16 13:13:03 +0000 | [diff] [blame] | 127 | (void)RegBank; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 128 | assert(RegBank && "Can't get reg bank for virtual register"); |
| 129 | |
Diana Picus | 36aa09f | 2016-12-19 14:07:50 +0000 | [diff] [blame] | 130 | const unsigned DstSize = MRI.getType(DstReg).getSizeInBits(); |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 131 | assert((RegBank->getID() == ARM::GPRRegBankID || |
| 132 | RegBank->getID() == ARM::FPRRegBankID) && |
| 133 | "Unsupported reg bank"); |
| 134 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 135 | const TargetRegisterClass *RC = &ARM::GPRRegClass; |
| 136 | |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 137 | if (RegBank->getID() == ARM::FPRRegBankID) { |
Diana Picus | 6beef3c | 2017-02-16 12:19:52 +0000 | [diff] [blame] | 138 | if (DstSize == 32) |
| 139 | RC = &ARM::SPRRegClass; |
| 140 | else if (DstSize == 64) |
| 141 | RC = &ARM::DPRRegClass; |
| 142 | else |
| 143 | llvm_unreachable("Unsupported destination size"); |
Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 146 | // No need to constrain SrcReg. It will get constrained when |
| 147 | // we hit another of its uses or its defs. |
| 148 | // Copies do not have constraints. |
| 149 | if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) { |
| 150 | DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode()) |
| 151 | << " operand\n"); |
| 152 | return false; |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 157 | static bool selectMergeValues(MachineInstrBuilder &MIB, |
| 158 | const ARMBaseInstrInfo &TII, |
| 159 | MachineRegisterInfo &MRI, |
| 160 | const TargetRegisterInfo &TRI, |
| 161 | const RegisterBankInfo &RBI) { |
| 162 | assert(TII.getSubtarget().hasVFP2() && "Can't select merge without VFP"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 163 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 164 | // 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] | 165 | // into one DPR. |
| 166 | unsigned VReg0 = MIB->getOperand(0).getReg(); |
| 167 | (void)VReg0; |
| 168 | assert(MRI.getType(VReg0).getSizeInBits() == 64 && |
| 169 | RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID && |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 170 | "Unsupported operand for G_MERGE_VALUES"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 171 | unsigned VReg1 = MIB->getOperand(1).getReg(); |
| 172 | (void)VReg1; |
| 173 | assert(MRI.getType(VReg1).getSizeInBits() == 32 && |
| 174 | RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID && |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 175 | "Unsupported operand for G_MERGE_VALUES"); |
| 176 | unsigned VReg2 = MIB->getOperand(2).getReg(); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 177 | (void)VReg2; |
| 178 | assert(MRI.getType(VReg2).getSizeInBits() == 32 && |
| 179 | RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID && |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 180 | "Unsupported operand for G_MERGE_VALUES"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 181 | |
| 182 | MIB->setDesc(TII.get(ARM::VMOVDRR)); |
| 183 | MIB.add(predOps(ARMCC::AL)); |
| 184 | |
| 185 | return true; |
| 186 | } |
| 187 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 188 | static bool selectUnmergeValues(MachineInstrBuilder &MIB, |
| 189 | const ARMBaseInstrInfo &TII, |
| 190 | MachineRegisterInfo &MRI, |
| 191 | const TargetRegisterInfo &TRI, |
| 192 | const RegisterBankInfo &RBI) { |
| 193 | assert(TII.getSubtarget().hasVFP2() && "Can't select unmerge without VFP"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 194 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 195 | // We only support G_UNMERGE_VALUES as a way to break up one DPR into two |
| 196 | // GPRs. |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 197 | unsigned VReg0 = MIB->getOperand(0).getReg(); |
| 198 | (void)VReg0; |
| 199 | assert(MRI.getType(VReg0).getSizeInBits() == 32 && |
| 200 | RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID && |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 201 | "Unsupported operand for G_UNMERGE_VALUES"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 202 | unsigned VReg1 = MIB->getOperand(1).getReg(); |
| 203 | (void)VReg1; |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 204 | assert(MRI.getType(VReg1).getSizeInBits() == 32 && |
| 205 | RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID && |
| 206 | "Unsupported operand for G_UNMERGE_VALUES"); |
| 207 | unsigned VReg2 = MIB->getOperand(2).getReg(); |
| 208 | (void)VReg2; |
| 209 | assert(MRI.getType(VReg2).getSizeInBits() == 64 && |
| 210 | RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::FPRRegBankID && |
| 211 | "Unsupported operand for G_UNMERGE_VALUES"); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 212 | |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 213 | MIB->setDesc(TII.get(ARM::VMOVRRD)); |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 214 | MIB.add(predOps(ARMCC::AL)); |
| 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 219 | /// Select the opcode for simple extensions (that translate to a single SXT/UXT |
| 220 | /// instruction). Extension operations more complicated than that should not |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 221 | /// invoke this. Returns the original opcode if it doesn't know how to select a |
| 222 | /// better one. |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 223 | static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) { |
| 224 | using namespace TargetOpcode; |
| 225 | |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 226 | if (Size != 8 && Size != 16) |
| 227 | return Opc; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 228 | |
| 229 | if (Opc == G_SEXT) |
| 230 | return Size == 8 ? ARM::SXTB : ARM::SXTH; |
| 231 | |
| 232 | if (Opc == G_ZEXT) |
| 233 | return Size == 8 ? ARM::UXTB : ARM::UXTH; |
| 234 | |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 235 | return Opc; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 238 | /// Select the opcode for simple loads and stores. For types smaller than 32 |
| 239 | /// bits, the value will be zero extended. Returns the original opcode if it |
| 240 | /// doesn't know how to select a better one. |
| 241 | static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank, |
| 242 | unsigned Size) { |
| 243 | bool isStore = Opc == TargetOpcode::G_STORE; |
| 244 | |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 245 | if (RegBank == ARM::GPRRegBankID) { |
| 246 | switch (Size) { |
| 247 | case 1: |
| 248 | case 8: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 249 | return isStore ? ARM::STRBi12 : ARM::LDRBi12; |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 250 | case 16: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 251 | return isStore ? ARM::STRH : ARM::LDRH; |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 252 | case 32: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 253 | return isStore ? ARM::STRi12 : ARM::LDRi12; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 254 | default: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 255 | return Opc; |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 256 | } |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 259 | if (RegBank == ARM::FPRRegBankID) { |
| 260 | switch (Size) { |
| 261 | case 32: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 262 | return isStore ? ARM::VSTRS : ARM::VLDRS; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 263 | case 64: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 264 | return isStore ? ARM::VSTRD : ARM::VLDRD; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 265 | default: |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 266 | return Opc; |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 267 | } |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 270 | return Opc; |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 273 | // When lowering comparisons, we sometimes need to perform two compares instead |
| 274 | // of just one. Get the condition codes for both comparisons. If only one is |
| 275 | // needed, the second member of the pair is ARMCC::AL. |
| 276 | static std::pair<ARMCC::CondCodes, ARMCC::CondCodes> |
| 277 | getComparePreds(CmpInst::Predicate Pred) { |
| 278 | std::pair<ARMCC::CondCodes, ARMCC::CondCodes> Preds = {ARMCC::AL, ARMCC::AL}; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 279 | switch (Pred) { |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 280 | case CmpInst::FCMP_ONE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 281 | Preds = {ARMCC::GT, ARMCC::MI}; |
| 282 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 283 | case CmpInst::FCMP_UEQ: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 284 | Preds = {ARMCC::EQ, ARMCC::VS}; |
| 285 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 286 | case CmpInst::ICMP_EQ: |
| 287 | case CmpInst::FCMP_OEQ: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 288 | Preds.first = ARMCC::EQ; |
| 289 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 290 | case CmpInst::ICMP_SGT: |
| 291 | case CmpInst::FCMP_OGT: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 292 | Preds.first = ARMCC::GT; |
| 293 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 294 | case CmpInst::ICMP_SGE: |
| 295 | case CmpInst::FCMP_OGE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 296 | Preds.first = ARMCC::GE; |
| 297 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 298 | case CmpInst::ICMP_UGT: |
| 299 | case CmpInst::FCMP_UGT: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 300 | Preds.first = ARMCC::HI; |
| 301 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 302 | case CmpInst::FCMP_OLT: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 303 | Preds.first = ARMCC::MI; |
| 304 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 305 | case CmpInst::ICMP_ULE: |
| 306 | case CmpInst::FCMP_OLE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 307 | Preds.first = ARMCC::LS; |
| 308 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 309 | case CmpInst::FCMP_ORD: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 310 | Preds.first = ARMCC::VC; |
| 311 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 312 | case CmpInst::FCMP_UNO: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 313 | Preds.first = ARMCC::VS; |
| 314 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 315 | case CmpInst::FCMP_UGE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 316 | Preds.first = ARMCC::PL; |
| 317 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 318 | case CmpInst::ICMP_SLT: |
| 319 | case CmpInst::FCMP_ULT: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 320 | Preds.first = ARMCC::LT; |
| 321 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 322 | case CmpInst::ICMP_SLE: |
| 323 | case CmpInst::FCMP_ULE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 324 | Preds.first = ARMCC::LE; |
| 325 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 326 | case CmpInst::FCMP_UNE: |
| 327 | case CmpInst::ICMP_NE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 328 | Preds.first = ARMCC::NE; |
| 329 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 330 | case CmpInst::ICMP_UGE: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 331 | Preds.first = ARMCC::HS; |
| 332 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 333 | case CmpInst::ICMP_ULT: |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 334 | Preds.first = ARMCC::LO; |
| 335 | break; |
| 336 | default: |
| 337 | break; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 338 | } |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 339 | assert(Preds.first != ARMCC::AL && "No comparisons needed?"); |
| 340 | return Preds; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 343 | struct ARMInstructionSelector::CmpConstants { |
| 344 | CmpConstants(unsigned CmpOpcode, unsigned FlagsOpcode, unsigned OpRegBank, |
| 345 | unsigned OpSize) |
| 346 | : ComparisonOpcode(CmpOpcode), ReadFlagsOpcode(FlagsOpcode), |
| 347 | OperandRegBankID(OpRegBank), OperandSize(OpSize) {} |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 348 | |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 349 | // The opcode used for performing the comparison. |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 350 | const unsigned ComparisonOpcode; |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 351 | |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 352 | // The opcode used for reading the flags set by the comparison. May be |
| 353 | // 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] | 354 | const unsigned ReadFlagsOpcode; |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 355 | |
| 356 | // The assumed register bank ID for the operands. |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 357 | const unsigned OperandRegBankID; |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 358 | |
Diana Picus | 21014df | 2017-07-12 09:01:54 +0000 | [diff] [blame] | 359 | // The assumed size in bits for the operands. |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 360 | const unsigned OperandSize; |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 361 | }; |
| 362 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 363 | struct ARMInstructionSelector::InsertInfo { |
| 364 | InsertInfo(MachineInstrBuilder &MIB) |
| 365 | : MBB(*MIB->getParent()), InsertBefore(std::next(MIB->getIterator())), |
| 366 | DbgLoc(MIB->getDebugLoc()) {} |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 367 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 368 | MachineBasicBlock &MBB; |
| 369 | const MachineBasicBlock::instr_iterator InsertBefore; |
| 370 | const DebugLoc &DbgLoc; |
| 371 | }; |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 372 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 373 | void ARMInstructionSelector::putConstant(InsertInfo I, unsigned DestReg, |
| 374 | unsigned Constant) const { |
| 375 | (void)BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(ARM::MOVi)) |
| 376 | .addDef(DestReg) |
| 377 | .addImm(Constant) |
| 378 | .add(predOps(ARMCC::AL)) |
| 379 | .add(condCodeOp()); |
| 380 | } |
Diana Picus | 21014df | 2017-07-12 09:01:54 +0000 | [diff] [blame] | 381 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 382 | bool ARMInstructionSelector::validOpRegPair(MachineRegisterInfo &MRI, |
| 383 | unsigned LHSReg, unsigned RHSReg, |
| 384 | unsigned ExpectedSize, |
| 385 | unsigned ExpectedRegBankID) const { |
| 386 | return MRI.getType(LHSReg) == MRI.getType(RHSReg) && |
| 387 | validReg(MRI, LHSReg, ExpectedSize, ExpectedRegBankID) && |
| 388 | validReg(MRI, RHSReg, ExpectedSize, ExpectedRegBankID); |
| 389 | } |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 390 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 391 | bool ARMInstructionSelector::validReg(MachineRegisterInfo &MRI, unsigned Reg, |
| 392 | unsigned ExpectedSize, |
| 393 | unsigned ExpectedRegBankID) const { |
| 394 | if (MRI.getType(Reg).getSizeInBits() != ExpectedSize) { |
| 395 | DEBUG(dbgs() << "Unexpected size for register"); |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | if (RBI.getRegBank(Reg, MRI, TRI)->getID() != ExpectedRegBankID) { |
| 400 | DEBUG(dbgs() << "Unexpected register bank for register"); |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | return true; |
| 405 | } |
| 406 | |
| 407 | bool ARMInstructionSelector::selectCmp(CmpConstants Helper, |
| 408 | MachineInstrBuilder &MIB, |
| 409 | MachineRegisterInfo &MRI) const { |
| 410 | const InsertInfo I(MIB); |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 411 | |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 412 | auto ResReg = MIB->getOperand(0).getReg(); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 413 | if (!validReg(MRI, ResReg, 1, ARM::GPRRegBankID)) |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 414 | return false; |
| 415 | |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 416 | auto Cond = |
| 417 | static_cast<CmpInst::Predicate>(MIB->getOperand(1).getPredicate()); |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 418 | if (Cond == CmpInst::FCMP_TRUE || Cond == CmpInst::FCMP_FALSE) { |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 419 | putConstant(I, ResReg, Cond == CmpInst::FCMP_TRUE ? 1 : 0); |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 420 | MIB->eraseFromParent(); |
| 421 | return true; |
| 422 | } |
| 423 | |
| 424 | auto LHSReg = MIB->getOperand(2).getReg(); |
| 425 | auto RHSReg = MIB->getOperand(3).getReg(); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 426 | if (!validOpRegPair(MRI, LHSReg, RHSReg, Helper.OperandSize, |
| 427 | Helper.OperandRegBankID)) |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 428 | return false; |
| 429 | |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 430 | auto ARMConds = getComparePreds(Cond); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 431 | auto ZeroReg = MRI.createVirtualRegister(&ARM::GPRRegClass); |
| 432 | putConstant(I, ZeroReg, 0); |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 433 | |
| 434 | if (ARMConds.second == ARMCC::AL) { |
| 435 | // Simple case, we only need one comparison and we're done. |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 436 | if (!insertComparison(Helper, I, ResReg, ARMConds.first, LHSReg, RHSReg, |
| 437 | ZeroReg)) |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 438 | return false; |
| 439 | } else { |
| 440 | // Not so simple, we need two successive comparisons. |
| 441 | auto IntermediateRes = MRI.createVirtualRegister(&ARM::GPRRegClass); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 442 | if (!insertComparison(Helper, I, IntermediateRes, ARMConds.first, LHSReg, |
| 443 | RHSReg, ZeroReg)) |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 444 | return false; |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 445 | if (!insertComparison(Helper, I, ResReg, ARMConds.second, LHSReg, RHSReg, |
| 446 | IntermediateRes)) |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 447 | return false; |
| 448 | } |
Diana Picus | 621894a | 2017-06-19 09:40:51 +0000 | [diff] [blame] | 449 | |
| 450 | MIB->eraseFromParent(); |
| 451 | return true; |
| 452 | } |
| 453 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 454 | bool ARMInstructionSelector::insertComparison(CmpConstants Helper, InsertInfo I, |
| 455 | unsigned ResReg, |
| 456 | ARMCC::CondCodes Cond, |
| 457 | unsigned LHSReg, unsigned RHSReg, |
| 458 | unsigned PrevRes) const { |
| 459 | // Perform the comparison. |
| 460 | auto CmpI = |
| 461 | BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(Helper.ComparisonOpcode)) |
| 462 | .addUse(LHSReg) |
| 463 | .addUse(RHSReg) |
| 464 | .add(predOps(ARMCC::AL)); |
| 465 | if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI)) |
| 466 | return false; |
| 467 | |
| 468 | // Read the comparison flags (if necessary). |
| 469 | if (Helper.ReadFlagsOpcode != ARM::INSTRUCTION_LIST_END) { |
| 470 | auto ReadI = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, |
| 471 | TII.get(Helper.ReadFlagsOpcode)) |
| 472 | .add(predOps(ARMCC::AL)); |
| 473 | if (!constrainSelectedInstRegOperands(*ReadI, TII, TRI, RBI)) |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | // Select either 1 or the previous result based on the value of the flags. |
| 478 | auto Mov1I = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(ARM::MOVCCi)) |
| 479 | .addDef(ResReg) |
| 480 | .addUse(PrevRes) |
| 481 | .addImm(1) |
| 482 | .add(predOps(Cond, ARM::CPSR)); |
| 483 | if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI)) |
| 484 | return false; |
| 485 | |
| 486 | return true; |
| 487 | } |
| 488 | |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 489 | bool ARMInstructionSelector::selectGlobal(MachineInstrBuilder &MIB, |
| 490 | MachineRegisterInfo &MRI) const { |
Diana Picus | f9597911 | 2017-09-01 11:13:39 +0000 | [diff] [blame^] | 491 | if (TII.getSubtarget().isRWPI()) { |
| 492 | DEBUG(dbgs() << "RWPI not supported yet\n"); |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | if (STI.isROPI() && !STI.isTargetELF()) { |
| 497 | DEBUG(dbgs() << "ROPI only supported for ELF\n"); |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 498 | return false; |
| 499 | } |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 500 | |
| 501 | auto GV = MIB->getOperand(1).getGlobal(); |
| 502 | if (GV->isThreadLocal()) { |
| 503 | DEBUG(dbgs() << "TLS variables not supported yet\n"); |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | auto &MBB = *MIB->getParent(); |
| 508 | auto &MF = *MBB.getParent(); |
| 509 | |
| 510 | auto ObjectFormat = TII.getSubtarget().getTargetTriple().getObjectFormat(); |
| 511 | bool UseMovt = TII.getSubtarget().useMovt(MF); |
| 512 | |
Diana Picus | c9f29c6 | 2017-08-29 09:47:55 +0000 | [diff] [blame] | 513 | unsigned Alignment = 4; |
| 514 | if (TM.isPositionIndependent()) { |
| 515 | bool Indirect = TII.getSubtarget().isGVIndirectSymbol(GV); |
| 516 | // FIXME: Taking advantage of MOVT for ELF is pretty involved, so we don't |
| 517 | // support it yet. See PR28229. |
| 518 | unsigned Opc = |
| 519 | UseMovt && !TII.getSubtarget().isTargetELF() |
| 520 | ? (Indirect ? ARM::MOV_ga_pcrel_ldr : ARM::MOV_ga_pcrel) |
| 521 | : (Indirect ? ARM::LDRLIT_ga_pcrel_ldr : ARM::LDRLIT_ga_pcrel); |
| 522 | MIB->setDesc(TII.get(Opc)); |
| 523 | |
| 524 | if (TII.getSubtarget().isTargetDarwin()) |
| 525 | MIB->getOperand(1).setTargetFlags(ARMII::MO_NONLAZY); |
| 526 | |
| 527 | if (Indirect) |
| 528 | MIB.addMemOperand(MF.getMachineMemOperand( |
| 529 | MachinePointerInfo::getGOT(MF), MachineMemOperand::MOLoad, |
| 530 | TM.getPointerSize(), Alignment)); |
| 531 | |
| 532 | return true; |
| 533 | } |
| 534 | |
Diana Picus | f9597911 | 2017-09-01 11:13:39 +0000 | [diff] [blame^] | 535 | bool isReadOnly = STI.getTargetLowering()->isReadOnly(GV); |
| 536 | if (STI.isROPI() && isReadOnly) { |
| 537 | unsigned Opc = UseMovt ? ARM::MOV_ga_pcrel : ARM::LDRLIT_ga_pcrel; |
| 538 | MIB->setDesc(TII.get(Opc)); |
| 539 | return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); |
| 540 | } |
| 541 | |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 542 | if (ObjectFormat == Triple::ELF) { |
| 543 | if (UseMovt) { |
| 544 | MIB->setDesc(TII.get(ARM::MOVi32imm)); |
| 545 | } else { |
| 546 | // Load the global's address from the constant pool. |
| 547 | MIB->setDesc(TII.get(ARM::LDRi12)); |
| 548 | MIB->RemoveOperand(1); |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 549 | MIB.addConstantPoolIndex( |
| 550 | MF.getConstantPool()->getConstantPoolIndex(GV, Alignment), |
| 551 | /* Offset */ 0, /* TargetFlags */ 0) |
| 552 | .addMemOperand(MF.getMachineMemOperand( |
| 553 | MachinePointerInfo::getConstantPool(MF), |
| 554 | MachineMemOperand::MOLoad, TM.getPointerSize(), Alignment)) |
| 555 | .addImm(0) |
| 556 | .add(predOps(ARMCC::AL)); |
| 557 | } |
| 558 | } else if (ObjectFormat == Triple::MachO) { |
| 559 | if (UseMovt) |
| 560 | MIB->setDesc(TII.get(ARM::MOVi32imm)); |
| 561 | else |
| 562 | MIB->setDesc(TII.get(ARM::LDRLIT_ga_abs)); |
| 563 | } else { |
| 564 | DEBUG(dbgs() << "Object format not supported yet\n"); |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI); |
| 569 | } |
| 570 | |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 571 | bool ARMInstructionSelector::selectSelect(MachineInstrBuilder &MIB, |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 572 | MachineRegisterInfo &MRI) const { |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 573 | auto &MBB = *MIB->getParent(); |
| 574 | auto InsertBefore = std::next(MIB->getIterator()); |
Diana Picus | 7736737 | 2017-07-07 08:53:27 +0000 | [diff] [blame] | 575 | auto &DbgLoc = MIB->getDebugLoc(); |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 576 | |
| 577 | // Compare the condition to 0. |
| 578 | auto CondReg = MIB->getOperand(1).getReg(); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 579 | assert(validReg(MRI, CondReg, 1, ARM::GPRRegBankID) && |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 580 | "Unsupported types for select operation"); |
Diana Picus | 7736737 | 2017-07-07 08:53:27 +0000 | [diff] [blame] | 581 | auto CmpI = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(ARM::CMPri)) |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 582 | .addUse(CondReg) |
| 583 | .addImm(0) |
| 584 | .add(predOps(ARMCC::AL)); |
| 585 | if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI)) |
| 586 | return false; |
| 587 | |
| 588 | // Move a value into the result register based on the result of the |
| 589 | // comparison. |
| 590 | auto ResReg = MIB->getOperand(0).getReg(); |
| 591 | auto TrueReg = MIB->getOperand(2).getReg(); |
| 592 | auto FalseReg = MIB->getOperand(3).getReg(); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 593 | assert(validOpRegPair(MRI, ResReg, TrueReg, 32, ARM::GPRRegBankID) && |
| 594 | validOpRegPair(MRI, TrueReg, FalseReg, 32, ARM::GPRRegBankID) && |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 595 | "Unsupported types for select operation"); |
Diana Picus | 7736737 | 2017-07-07 08:53:27 +0000 | [diff] [blame] | 596 | auto Mov1I = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(ARM::MOVCCr)) |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 597 | .addDef(ResReg) |
| 598 | .addUse(TrueReg) |
| 599 | .addUse(FalseReg) |
| 600 | .add(predOps(ARMCC::EQ, ARM::CPSR)); |
| 601 | if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI)) |
| 602 | return false; |
| 603 | |
| 604 | MIB->eraseFromParent(); |
| 605 | return true; |
| 606 | } |
| 607 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 608 | bool ARMInstructionSelector::select(MachineInstr &I) const { |
| 609 | assert(I.getParent() && "Instruction should be in a basic block!"); |
| 610 | assert(I.getParent()->getParent() && "Instruction should be in a function!"); |
| 611 | |
| 612 | auto &MBB = *I.getParent(); |
| 613 | auto &MF = *MBB.getParent(); |
| 614 | auto &MRI = MF.getRegInfo(); |
| 615 | |
| 616 | if (!isPreISelGenericOpcode(I.getOpcode())) { |
| 617 | if (I.isCopy()) |
| 618 | return selectCopy(I, TII, MRI, TRI, RBI); |
| 619 | |
| 620 | return true; |
| 621 | } |
| 622 | |
Diana Picus | 8abcbbb | 2017-05-02 09:40:49 +0000 | [diff] [blame] | 623 | if (selectImpl(I)) |
| 624 | return true; |
| 625 | |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 626 | MachineInstrBuilder MIB{MF, I}; |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 627 | bool isSExt = false; |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 628 | |
| 629 | using namespace TargetOpcode; |
| 630 | switch (I.getOpcode()) { |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 631 | case G_SEXT: |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 632 | isSExt = true; |
| 633 | LLVM_FALLTHROUGH; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 634 | case G_ZEXT: { |
| 635 | LLT DstTy = MRI.getType(I.getOperand(0).getReg()); |
| 636 | // FIXME: Smaller destination sizes coming soon! |
| 637 | if (DstTy.getSizeInBits() != 32) { |
| 638 | DEBUG(dbgs() << "Unsupported destination size for extension"); |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | LLT SrcTy = MRI.getType(I.getOperand(1).getReg()); |
| 643 | unsigned SrcSize = SrcTy.getSizeInBits(); |
| 644 | switch (SrcSize) { |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 645 | case 1: { |
| 646 | // ZExt boils down to & 0x1; for SExt we also subtract that from 0 |
| 647 | I.setDesc(TII.get(ARM::ANDri)); |
| 648 | MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp()); |
| 649 | |
| 650 | if (isSExt) { |
| 651 | unsigned SExtResult = I.getOperand(0).getReg(); |
| 652 | |
| 653 | // Use a new virtual register for the result of the AND |
| 654 | unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass); |
| 655 | I.getOperand(0).setReg(AndResult); |
| 656 | |
| 657 | auto InsertBefore = std::next(I.getIterator()); |
Martin Bohme | 8396e14 | 2017-01-25 14:28:19 +0000 | [diff] [blame] | 658 | auto SubI = |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 659 | BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri)) |
| 660 | .addDef(SExtResult) |
| 661 | .addUse(AndResult) |
| 662 | .addImm(0) |
| 663 | .add(predOps(ARMCC::AL)) |
| 664 | .add(condCodeOp()); |
| 665 | if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI)) |
| 666 | return false; |
| 667 | } |
| 668 | break; |
| 669 | } |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 670 | case 8: |
| 671 | case 16: { |
| 672 | unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize); |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 673 | if (NewOpc == I.getOpcode()) |
| 674 | return false; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 675 | I.setDesc(TII.get(NewOpc)); |
| 676 | MIB.addImm(0).add(predOps(ARMCC::AL)); |
| 677 | break; |
| 678 | } |
| 679 | default: |
| 680 | DEBUG(dbgs() << "Unsupported source size for extension"); |
| 681 | return false; |
| 682 | } |
| 683 | break; |
| 684 | } |
Diana Picus | 657bfd3 | 2017-05-11 08:28:31 +0000 | [diff] [blame] | 685 | case G_ANYEXT: |
Diana Picus | 64a3343 | 2017-04-21 13:16:50 +0000 | [diff] [blame] | 686 | case G_TRUNC: { |
| 687 | // The high bits are undefined, so there's nothing special to do, just |
| 688 | // treat it as a copy. |
| 689 | auto SrcReg = I.getOperand(1).getReg(); |
| 690 | auto DstReg = I.getOperand(0).getReg(); |
| 691 | |
| 692 | const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI); |
| 693 | const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI); |
| 694 | |
| 695 | if (SrcRegBank.getID() != DstRegBank.getID()) { |
Diana Picus | 657bfd3 | 2017-05-11 08:28:31 +0000 | [diff] [blame] | 696 | DEBUG(dbgs() << "G_TRUNC/G_ANYEXT operands on different register banks\n"); |
Diana Picus | 64a3343 | 2017-04-21 13:16:50 +0000 | [diff] [blame] | 697 | return false; |
| 698 | } |
| 699 | |
| 700 | if (SrcRegBank.getID() != ARM::GPRRegBankID) { |
Diana Picus | 657bfd3 | 2017-05-11 08:28:31 +0000 | [diff] [blame] | 701 | 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] | 702 | return false; |
| 703 | } |
| 704 | |
| 705 | I.setDesc(TII.get(COPY)); |
| 706 | return selectCopy(I, TII, MRI, TRI, RBI); |
| 707 | } |
Diana Picus | 7145d22 | 2017-06-27 09:19:51 +0000 | [diff] [blame] | 708 | case G_SELECT: |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 709 | return selectSelect(MIB, MRI); |
| 710 | case G_ICMP: { |
| 711 | CmpConstants Helper(ARM::CMPrr, ARM::INSTRUCTION_LIST_END, |
| 712 | ARM::GPRRegBankID, 32); |
| 713 | return selectCmp(Helper, MIB, MRI); |
| 714 | } |
Diana Picus | 21014df | 2017-07-12 09:01:54 +0000 | [diff] [blame] | 715 | case G_FCMP: { |
Diana Picus | 5b91653 | 2017-07-07 08:39:04 +0000 | [diff] [blame] | 716 | assert(TII.getSubtarget().hasVFP2() && "Can't select fcmp without VFP"); |
Diana Picus | 21014df | 2017-07-12 09:01:54 +0000 | [diff] [blame] | 717 | |
| 718 | unsigned OpReg = I.getOperand(2).getReg(); |
| 719 | unsigned Size = MRI.getType(OpReg).getSizeInBits(); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 720 | |
| 721 | if (Size == 64 && TII.getSubtarget().isFPOnlySP()) { |
| 722 | DEBUG(dbgs() << "Subtarget only supports single precision"); |
| 723 | return false; |
| 724 | } |
| 725 | if (Size != 32 && Size != 64) { |
| 726 | DEBUG(dbgs() << "Unsupported size for G_FCMP operand"); |
| 727 | return false; |
Diana Picus | 21014df | 2017-07-12 09:01:54 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 730 | CmpConstants Helper(Size == 32 ? ARM::VCMPS : ARM::VCMPD, ARM::FMSTAT, |
| 731 | ARM::FPRRegBankID, Size); |
| 732 | return selectCmp(Helper, MIB, MRI); |
Diana Picus | 21014df | 2017-07-12 09:01:54 +0000 | [diff] [blame] | 733 | } |
Diana Picus | 9d07094 | 2017-02-28 10:14:38 +0000 | [diff] [blame] | 734 | case G_GEP: |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 735 | I.setDesc(TII.get(ARM::ADDrr)); |
Diana Picus | 8a73f55 | 2017-01-13 10:18:01 +0000 | [diff] [blame] | 736 | MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 737 | break; |
| 738 | case G_FRAME_INDEX: |
| 739 | // Add 0 to the given frame index and hope it will eventually be folded into |
| 740 | // the user(s). |
| 741 | I.setDesc(TII.get(ARM::ADDri)); |
Diana Picus | 8a73f55 | 2017-01-13 10:18:01 +0000 | [diff] [blame] | 742 | MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 743 | break; |
Diana Picus | 5a7203a | 2017-02-28 13:05:42 +0000 | [diff] [blame] | 744 | case G_CONSTANT: { |
| 745 | unsigned Reg = I.getOperand(0).getReg(); |
Diana Picus | 995746d | 2017-07-12 10:31:16 +0000 | [diff] [blame] | 746 | |
| 747 | if (!validReg(MRI, Reg, 32, ARM::GPRRegBankID)) |
Diana Picus | 5a7203a | 2017-02-28 13:05:42 +0000 | [diff] [blame] | 748 | return false; |
| 749 | |
Diana Picus | 5a7203a | 2017-02-28 13:05:42 +0000 | [diff] [blame] | 750 | I.setDesc(TII.get(ARM::MOVi)); |
| 751 | MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 95a8aa9 | 2017-04-24 06:30:56 +0000 | [diff] [blame] | 752 | |
| 753 | auto &Val = I.getOperand(1); |
| 754 | if (Val.isCImm()) { |
| 755 | if (Val.getCImm()->getBitWidth() > 32) |
| 756 | return false; |
| 757 | Val.ChangeToImmediate(Val.getCImm()->getZExtValue()); |
| 758 | } |
| 759 | |
| 760 | if (!Val.isImm()) { |
| 761 | return false; |
| 762 | } |
| 763 | |
Diana Picus | 5a7203a | 2017-02-28 13:05:42 +0000 | [diff] [blame] | 764 | break; |
| 765 | } |
Diana Picus | 930e6ec | 2017-08-03 09:14:59 +0000 | [diff] [blame] | 766 | case G_GLOBAL_VALUE: |
| 767 | return selectGlobal(MIB, MRI); |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 768 | case G_STORE: |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 769 | case G_LOAD: { |
Diana Picus | 1c33c9f | 2017-02-20 14:45:58 +0000 | [diff] [blame] | 770 | const auto &MemOp = **I.memoperands_begin(); |
| 771 | if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) { |
| 772 | DEBUG(dbgs() << "Atomic load/store not supported yet\n"); |
| 773 | return false; |
| 774 | } |
| 775 | |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 776 | unsigned Reg = I.getOperand(0).getReg(); |
| 777 | unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID(); |
| 778 | |
| 779 | LLT ValTy = MRI.getType(Reg); |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 780 | const auto ValSize = ValTy.getSizeInBits(); |
| 781 | |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 782 | assert((ValSize != 64 || TII.getSubtarget().hasVFP2()) && |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 783 | "Don't know how to load/store 64-bit value without VFP"); |
Diana Picus | 1540b06 | 2017-02-16 14:10:50 +0000 | [diff] [blame] | 784 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 785 | const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize); |
| 786 | if (NewOpc == G_LOAD || NewOpc == G_STORE) |
Diana Picus | e836878 | 2017-02-17 13:44:19 +0000 | [diff] [blame] | 787 | return false; |
| 788 | |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 789 | I.setDesc(TII.get(NewOpc)); |
| 790 | |
Diana Picus | 3b99c64 | 2017-02-24 14:01:27 +0000 | [diff] [blame] | 791 | if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH) |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 792 | // LDRH has a funny addressing mode (there's already a FIXME for it). |
| 793 | MIB.addReg(0); |
Diana Picus | 4f8c3e1 | 2017-01-13 09:37:56 +0000 | [diff] [blame] | 794 | MIB.addImm(0).add(predOps(ARMCC::AL)); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 795 | break; |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 796 | } |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 797 | case G_MERGE_VALUES: { |
| 798 | if (!selectMergeValues(MIB, TII, MRI, TRI, RBI)) |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 799 | return false; |
| 800 | break; |
| 801 | } |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 802 | case G_UNMERGE_VALUES: { |
| 803 | if (!selectUnmergeValues(MIB, TII, MRI, TRI, RBI)) |
Diana Picus | b1701e0 | 2017-02-16 12:19:57 +0000 | [diff] [blame] | 804 | return false; |
| 805 | break; |
| 806 | } |
Diana Picus | 87a7067 | 2017-07-14 09:46:06 +0000 | [diff] [blame] | 807 | case G_BRCOND: { |
| 808 | if (!validReg(MRI, I.getOperand(0).getReg(), 1, ARM::GPRRegBankID)) { |
| 809 | DEBUG(dbgs() << "Unsupported condition register for G_BRCOND"); |
| 810 | return false; |
| 811 | } |
| 812 | |
| 813 | // Set the flags. |
| 814 | auto Test = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(ARM::TSTri)) |
| 815 | .addReg(I.getOperand(0).getReg()) |
| 816 | .addImm(1) |
| 817 | .add(predOps(ARMCC::AL)); |
| 818 | if (!constrainSelectedInstRegOperands(*Test, TII, TRI, RBI)) |
| 819 | return false; |
| 820 | |
| 821 | // Branch conditionally. |
| 822 | auto Branch = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(ARM::Bcc)) |
| 823 | .add(I.getOperand(1)) |
| 824 | .add(predOps(ARMCC::EQ, ARM::CPSR)); |
| 825 | if (!constrainSelectedInstRegOperands(*Branch, TII, TRI, RBI)) |
| 826 | return false; |
| 827 | I.eraseFromParent(); |
| 828 | return true; |
| 829 | } |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 830 | default: |
| 831 | return false; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 834 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 835 | } |