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