blob: 6692a4d4142052b2d2e4cf754b432a0c39ccbc87 [file] [log] [blame]
Diana Picus22274932016-11-11 08:27:37 +00001//===- 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 Picus22274932016-11-11 08:27:37 +000014#include "ARMRegisterBankInfo.h"
15#include "ARMSubtarget.h"
16#include "ARMTargetMachine.h"
Diana Picus674888d2017-04-28 09:10:38 +000017#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
David Blaikie62651302017-10-26 23:39:54 +000018#include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h"
Diana Picus930e6ec2017-08-03 09:14:59 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Diana Picus812caee2016-12-16 12:54:46 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000021#include "llvm/Support/Debug.h"
22
23#define DEBUG_TYPE "arm-isel"
24
25using namespace llvm;
26
Diana Picus674888d2017-04-28 09:10:38 +000027namespace {
Diana Picus8abcbbb2017-05-02 09:40:49 +000028
29#define GET_GLOBALISEL_PREDICATE_BITSET
30#include "ARMGenGlobalISel.inc"
31#undef GET_GLOBALISEL_PREDICATE_BITSET
32
Diana Picus674888d2017-04-28 09:10:38 +000033class ARMInstructionSelector : public InstructionSelector {
34public:
Diana Picus8abcbbb2017-05-02 09:40:49 +000035 ARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget &STI,
Diana Picus674888d2017-04-28 09:10:38 +000036 const ARMRegisterBankInfo &RBI);
37
Daniel Sandersf76f3152017-11-16 00:46:35 +000038 bool select(MachineInstr &I, CodeGenCoverage &CoverageInfo) const override;
David Blaikie62651302017-10-26 23:39:54 +000039 static const char *getName() { return DEBUG_TYPE; }
Diana Picus674888d2017-04-28 09:10:38 +000040
41private:
Daniel Sandersf76f3152017-11-16 00:46:35 +000042 bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
Diana Picus8abcbbb2017-05-02 09:40:49 +000043
Diana Picus995746d2017-07-12 10:31:16 +000044 struct CmpConstants;
45 struct InsertInfo;
Diana Picus5b916532017-07-07 08:39:04 +000046
Diana Picus995746d2017-07-12 10:31:16 +000047 bool selectCmp(CmpConstants Helper, MachineInstrBuilder &MIB,
48 MachineRegisterInfo &MRI) const;
Diana Picus621894a2017-06-19 09:40:51 +000049
Diana Picus995746d2017-07-12 10:31:16 +000050 // 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 Picus930e6ec2017-08-03 09:14:59 +000060 bool selectGlobal(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const;
Diana Picus995746d2017-07-12 10:31:16 +000061 bool selectSelect(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const;
Diana Picuse393bc72017-10-06 15:39:16 +000062 bool selectShift(unsigned ShiftOpc, MachineInstrBuilder &MIB) const;
Diana Picus995746d2017-07-12 10:31:16 +000063
64 // Check if the types match and both operands have the expected size and
65 // register bank.
66 bool validOpRegPair(MachineRegisterInfo &MRI, unsigned LHS, unsigned RHS,
67 unsigned ExpectedSize, unsigned ExpectedRegBankID) const;
68
69 // Check if the register has the expected size and register bank.
70 bool validReg(MachineRegisterInfo &MRI, unsigned Reg, unsigned ExpectedSize,
71 unsigned ExpectedRegBankID) const;
Diana Picus7145d222017-06-27 09:19:51 +000072
Diana Picus674888d2017-04-28 09:10:38 +000073 const ARMBaseInstrInfo &TII;
74 const ARMBaseRegisterInfo &TRI;
Diana Picus8abcbbb2017-05-02 09:40:49 +000075 const ARMBaseTargetMachine &TM;
Diana Picus674888d2017-04-28 09:10:38 +000076 const ARMRegisterBankInfo &RBI;
Diana Picus8abcbbb2017-05-02 09:40:49 +000077 const ARMSubtarget &STI;
78
79#define GET_GLOBALISEL_PREDICATES_DECL
80#include "ARMGenGlobalISel.inc"
81#undef GET_GLOBALISEL_PREDICATES_DECL
82
83// We declare the temporaries used by selectImpl() in the class to minimize the
84// cost of constructing placeholder values.
85#define GET_GLOBALISEL_TEMPORARIES_DECL
86#include "ARMGenGlobalISel.inc"
87#undef GET_GLOBALISEL_TEMPORARIES_DECL
Diana Picus674888d2017-04-28 09:10:38 +000088};
89} // end anonymous namespace
90
91namespace llvm {
92InstructionSelector *
Diana Picus8abcbbb2017-05-02 09:40:49 +000093createARMInstructionSelector(const ARMBaseTargetMachine &TM,
94 const ARMSubtarget &STI,
Diana Picus674888d2017-04-28 09:10:38 +000095 const ARMRegisterBankInfo &RBI) {
Diana Picus8abcbbb2017-05-02 09:40:49 +000096 return new ARMInstructionSelector(TM, STI, RBI);
Diana Picus674888d2017-04-28 09:10:38 +000097}
98}
99
Daniel Sanders8e82af22017-07-27 11:03:45 +0000100const unsigned zero_reg = 0;
Diana Picus8abcbbb2017-05-02 09:40:49 +0000101
102#define GET_GLOBALISEL_IMPL
103#include "ARMGenGlobalISel.inc"
104#undef GET_GLOBALISEL_IMPL
105
106ARMInstructionSelector::ARMInstructionSelector(const ARMBaseTargetMachine &TM,
107 const ARMSubtarget &STI,
Diana Picus22274932016-11-11 08:27:37 +0000108 const ARMRegisterBankInfo &RBI)
Diana Picus895c6aa2016-11-15 16:42:10 +0000109 : InstructionSelector(), TII(*STI.getInstrInfo()),
Diana Picus8abcbbb2017-05-02 09:40:49 +0000110 TRI(*STI.getRegisterInfo()), TM(TM), RBI(RBI), STI(STI),
111#define GET_GLOBALISEL_PREDICATES_INIT
112#include "ARMGenGlobalISel.inc"
113#undef GET_GLOBALISEL_PREDICATES_INIT
114#define GET_GLOBALISEL_TEMPORARIES_INIT
115#include "ARMGenGlobalISel.inc"
116#undef GET_GLOBALISEL_TEMPORARIES_INIT
117{
118}
Diana Picus22274932016-11-11 08:27:37 +0000119
Diana Picus865f7fe2018-01-04 13:09:25 +0000120static const TargetRegisterClass *guessRegClass(unsigned Reg,
121 MachineRegisterInfo &MRI,
122 const TargetRegisterInfo &TRI,
123 const RegisterBankInfo &RBI) {
124 const RegisterBank *RegBank = RBI.getRegBank(Reg, MRI, TRI);
125 assert(RegBank && "Can't get reg bank for virtual register");
126
127 const unsigned Size = MRI.getType(Reg).getSizeInBits();
128 assert((RegBank->getID() == ARM::GPRRegBankID ||
129 RegBank->getID() == ARM::FPRRegBankID) &&
130 "Unsupported reg bank");
131
132 if (RegBank->getID() == ARM::FPRRegBankID) {
133 if (Size == 32)
134 return &ARM::SPRRegClass;
135 else if (Size == 64)
136 return &ARM::DPRRegClass;
Roman Tereshine79d6562018-05-23 02:59:31 +0000137 else if (Size == 128)
138 return &ARM::QPRRegClass;
Diana Picus865f7fe2018-01-04 13:09:25 +0000139 else
140 llvm_unreachable("Unsupported destination size");
141 }
142
143 return &ARM::GPRRegClass;
144}
145
Diana Picus812caee2016-12-16 12:54:46 +0000146static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
147 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
148 const RegisterBankInfo &RBI) {
149 unsigned DstReg = I.getOperand(0).getReg();
150 if (TargetRegisterInfo::isPhysicalRegister(DstReg))
151 return true;
152
Diana Picus865f7fe2018-01-04 13:09:25 +0000153 const TargetRegisterClass *RC = guessRegClass(DstReg, MRI, TRI, RBI);
Diana Picus4fa83c02017-02-08 13:23:04 +0000154
Diana Picus812caee2016-12-16 12:54:46 +0000155 // No need to constrain SrcReg. It will get constrained when
156 // we hit another of its uses or its defs.
157 // Copies do not have constraints.
158 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000159 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
160 << " operand\n");
Diana Picus812caee2016-12-16 12:54:46 +0000161 return false;
162 }
163 return true;
164}
165
Diana Picus0b4190a2017-06-07 12:35:05 +0000166static bool selectMergeValues(MachineInstrBuilder &MIB,
167 const ARMBaseInstrInfo &TII,
168 MachineRegisterInfo &MRI,
169 const TargetRegisterInfo &TRI,
170 const RegisterBankInfo &RBI) {
171 assert(TII.getSubtarget().hasVFP2() && "Can't select merge without VFP");
Diana Picusb1701e02017-02-16 12:19:57 +0000172
Diana Picus0b4190a2017-06-07 12:35:05 +0000173 // We only support G_MERGE_VALUES as a way to stick together two scalar GPRs
Diana Picusb1701e02017-02-16 12:19:57 +0000174 // into one DPR.
175 unsigned VReg0 = MIB->getOperand(0).getReg();
176 (void)VReg0;
177 assert(MRI.getType(VReg0).getSizeInBits() == 64 &&
178 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID &&
Diana Picus0b4190a2017-06-07 12:35:05 +0000179 "Unsupported operand for G_MERGE_VALUES");
Diana Picusb1701e02017-02-16 12:19:57 +0000180 unsigned VReg1 = MIB->getOperand(1).getReg();
181 (void)VReg1;
182 assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
183 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
Diana Picus0b4190a2017-06-07 12:35:05 +0000184 "Unsupported operand for G_MERGE_VALUES");
185 unsigned VReg2 = MIB->getOperand(2).getReg();
Diana Picusb1701e02017-02-16 12:19:57 +0000186 (void)VReg2;
187 assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
188 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
Diana Picus0b4190a2017-06-07 12:35:05 +0000189 "Unsupported operand for G_MERGE_VALUES");
Diana Picusb1701e02017-02-16 12:19:57 +0000190
191 MIB->setDesc(TII.get(ARM::VMOVDRR));
192 MIB.add(predOps(ARMCC::AL));
193
194 return true;
195}
196
Diana Picus0b4190a2017-06-07 12:35:05 +0000197static bool selectUnmergeValues(MachineInstrBuilder &MIB,
198 const ARMBaseInstrInfo &TII,
199 MachineRegisterInfo &MRI,
200 const TargetRegisterInfo &TRI,
201 const RegisterBankInfo &RBI) {
202 assert(TII.getSubtarget().hasVFP2() && "Can't select unmerge without VFP");
Diana Picusb1701e02017-02-16 12:19:57 +0000203
Diana Picus0b4190a2017-06-07 12:35:05 +0000204 // We only support G_UNMERGE_VALUES as a way to break up one DPR into two
205 // GPRs.
Diana Picusb1701e02017-02-16 12:19:57 +0000206 unsigned VReg0 = MIB->getOperand(0).getReg();
207 (void)VReg0;
208 assert(MRI.getType(VReg0).getSizeInBits() == 32 &&
209 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID &&
Diana Picus0b4190a2017-06-07 12:35:05 +0000210 "Unsupported operand for G_UNMERGE_VALUES");
Diana Picusb1701e02017-02-16 12:19:57 +0000211 unsigned VReg1 = MIB->getOperand(1).getReg();
212 (void)VReg1;
Diana Picus0b4190a2017-06-07 12:35:05 +0000213 assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
214 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
215 "Unsupported operand for G_UNMERGE_VALUES");
216 unsigned VReg2 = MIB->getOperand(2).getReg();
217 (void)VReg2;
218 assert(MRI.getType(VReg2).getSizeInBits() == 64 &&
219 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::FPRRegBankID &&
220 "Unsupported operand for G_UNMERGE_VALUES");
Diana Picusb1701e02017-02-16 12:19:57 +0000221
Diana Picus0b4190a2017-06-07 12:35:05 +0000222 MIB->setDesc(TII.get(ARM::VMOVRRD));
Diana Picusb1701e02017-02-16 12:19:57 +0000223 MIB.add(predOps(ARMCC::AL));
224
225 return true;
226}
227
Diana Picus8b6c6be2017-01-25 08:10:40 +0000228/// Select the opcode for simple extensions (that translate to a single SXT/UXT
229/// instruction). Extension operations more complicated than that should not
Diana Picuse8368782017-02-17 13:44:19 +0000230/// invoke this. Returns the original opcode if it doesn't know how to select a
231/// better one.
Diana Picus8b6c6be2017-01-25 08:10:40 +0000232static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) {
233 using namespace TargetOpcode;
234
Diana Picuse8368782017-02-17 13:44:19 +0000235 if (Size != 8 && Size != 16)
236 return Opc;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000237
238 if (Opc == G_SEXT)
239 return Size == 8 ? ARM::SXTB : ARM::SXTH;
240
241 if (Opc == G_ZEXT)
242 return Size == 8 ? ARM::UXTB : ARM::UXTH;
243
Diana Picuse8368782017-02-17 13:44:19 +0000244 return Opc;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000245}
246
Diana Picus3b99c642017-02-24 14:01:27 +0000247/// Select the opcode for simple loads and stores. For types smaller than 32
248/// bits, the value will be zero extended. Returns the original opcode if it
249/// doesn't know how to select a better one.
250static unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank,
251 unsigned Size) {
252 bool isStore = Opc == TargetOpcode::G_STORE;
253
Diana Picus1540b062017-02-16 14:10:50 +0000254 if (RegBank == ARM::GPRRegBankID) {
255 switch (Size) {
256 case 1:
257 case 8:
Diana Picus3b99c642017-02-24 14:01:27 +0000258 return isStore ? ARM::STRBi12 : ARM::LDRBi12;
Diana Picus1540b062017-02-16 14:10:50 +0000259 case 16:
Diana Picus3b99c642017-02-24 14:01:27 +0000260 return isStore ? ARM::STRH : ARM::LDRH;
Diana Picus1540b062017-02-16 14:10:50 +0000261 case 32:
Diana Picus3b99c642017-02-24 14:01:27 +0000262 return isStore ? ARM::STRi12 : ARM::LDRi12;
Diana Picuse8368782017-02-17 13:44:19 +0000263 default:
Diana Picus3b99c642017-02-24 14:01:27 +0000264 return Opc;
Diana Picus1540b062017-02-16 14:10:50 +0000265 }
Diana Picus1540b062017-02-16 14:10:50 +0000266 }
267
Diana Picuse8368782017-02-17 13:44:19 +0000268 if (RegBank == ARM::FPRRegBankID) {
269 switch (Size) {
270 case 32:
Diana Picus3b99c642017-02-24 14:01:27 +0000271 return isStore ? ARM::VSTRS : ARM::VLDRS;
Diana Picuse8368782017-02-17 13:44:19 +0000272 case 64:
Diana Picus3b99c642017-02-24 14:01:27 +0000273 return isStore ? ARM::VSTRD : ARM::VLDRD;
Diana Picuse8368782017-02-17 13:44:19 +0000274 default:
Diana Picus3b99c642017-02-24 14:01:27 +0000275 return Opc;
Diana Picuse8368782017-02-17 13:44:19 +0000276 }
Diana Picus278c7222017-01-26 09:20:47 +0000277 }
278
Diana Picus3b99c642017-02-24 14:01:27 +0000279 return Opc;
Diana Picus278c7222017-01-26 09:20:47 +0000280}
281
Diana Picus5b916532017-07-07 08:39:04 +0000282// When lowering comparisons, we sometimes need to perform two compares instead
283// of just one. Get the condition codes for both comparisons. If only one is
284// needed, the second member of the pair is ARMCC::AL.
285static std::pair<ARMCC::CondCodes, ARMCC::CondCodes>
286getComparePreds(CmpInst::Predicate Pred) {
287 std::pair<ARMCC::CondCodes, ARMCC::CondCodes> Preds = {ARMCC::AL, ARMCC::AL};
Diana Picus621894a2017-06-19 09:40:51 +0000288 switch (Pred) {
Diana Picus621894a2017-06-19 09:40:51 +0000289 case CmpInst::FCMP_ONE:
Diana Picus5b916532017-07-07 08:39:04 +0000290 Preds = {ARMCC::GT, ARMCC::MI};
291 break;
Diana Picus621894a2017-06-19 09:40:51 +0000292 case CmpInst::FCMP_UEQ:
Diana Picus5b916532017-07-07 08:39:04 +0000293 Preds = {ARMCC::EQ, ARMCC::VS};
294 break;
Diana Picus621894a2017-06-19 09:40:51 +0000295 case CmpInst::ICMP_EQ:
296 case CmpInst::FCMP_OEQ:
Diana Picus5b916532017-07-07 08:39:04 +0000297 Preds.first = ARMCC::EQ;
298 break;
Diana Picus621894a2017-06-19 09:40:51 +0000299 case CmpInst::ICMP_SGT:
300 case CmpInst::FCMP_OGT:
Diana Picus5b916532017-07-07 08:39:04 +0000301 Preds.first = ARMCC::GT;
302 break;
Diana Picus621894a2017-06-19 09:40:51 +0000303 case CmpInst::ICMP_SGE:
304 case CmpInst::FCMP_OGE:
Diana Picus5b916532017-07-07 08:39:04 +0000305 Preds.first = ARMCC::GE;
306 break;
Diana Picus621894a2017-06-19 09:40:51 +0000307 case CmpInst::ICMP_UGT:
308 case CmpInst::FCMP_UGT:
Diana Picus5b916532017-07-07 08:39:04 +0000309 Preds.first = ARMCC::HI;
310 break;
Diana Picus621894a2017-06-19 09:40:51 +0000311 case CmpInst::FCMP_OLT:
Diana Picus5b916532017-07-07 08:39:04 +0000312 Preds.first = ARMCC::MI;
313 break;
Diana Picus621894a2017-06-19 09:40:51 +0000314 case CmpInst::ICMP_ULE:
315 case CmpInst::FCMP_OLE:
Diana Picus5b916532017-07-07 08:39:04 +0000316 Preds.first = ARMCC::LS;
317 break;
Diana Picus621894a2017-06-19 09:40:51 +0000318 case CmpInst::FCMP_ORD:
Diana Picus5b916532017-07-07 08:39:04 +0000319 Preds.first = ARMCC::VC;
320 break;
Diana Picus621894a2017-06-19 09:40:51 +0000321 case CmpInst::FCMP_UNO:
Diana Picus5b916532017-07-07 08:39:04 +0000322 Preds.first = ARMCC::VS;
323 break;
Diana Picus621894a2017-06-19 09:40:51 +0000324 case CmpInst::FCMP_UGE:
Diana Picus5b916532017-07-07 08:39:04 +0000325 Preds.first = ARMCC::PL;
326 break;
Diana Picus621894a2017-06-19 09:40:51 +0000327 case CmpInst::ICMP_SLT:
328 case CmpInst::FCMP_ULT:
Diana Picus5b916532017-07-07 08:39:04 +0000329 Preds.first = ARMCC::LT;
330 break;
Diana Picus621894a2017-06-19 09:40:51 +0000331 case CmpInst::ICMP_SLE:
332 case CmpInst::FCMP_ULE:
Diana Picus5b916532017-07-07 08:39:04 +0000333 Preds.first = ARMCC::LE;
334 break;
Diana Picus621894a2017-06-19 09:40:51 +0000335 case CmpInst::FCMP_UNE:
336 case CmpInst::ICMP_NE:
Diana Picus5b916532017-07-07 08:39:04 +0000337 Preds.first = ARMCC::NE;
338 break;
Diana Picus621894a2017-06-19 09:40:51 +0000339 case CmpInst::ICMP_UGE:
Diana Picus5b916532017-07-07 08:39:04 +0000340 Preds.first = ARMCC::HS;
341 break;
Diana Picus621894a2017-06-19 09:40:51 +0000342 case CmpInst::ICMP_ULT:
Diana Picus5b916532017-07-07 08:39:04 +0000343 Preds.first = ARMCC::LO;
344 break;
345 default:
346 break;
Diana Picus621894a2017-06-19 09:40:51 +0000347 }
Diana Picus5b916532017-07-07 08:39:04 +0000348 assert(Preds.first != ARMCC::AL && "No comparisons needed?");
349 return Preds;
Diana Picus621894a2017-06-19 09:40:51 +0000350}
351
Diana Picus995746d2017-07-12 10:31:16 +0000352struct ARMInstructionSelector::CmpConstants {
353 CmpConstants(unsigned CmpOpcode, unsigned FlagsOpcode, unsigned OpRegBank,
354 unsigned OpSize)
355 : ComparisonOpcode(CmpOpcode), ReadFlagsOpcode(FlagsOpcode),
356 OperandRegBankID(OpRegBank), OperandSize(OpSize) {}
Diana Picus621894a2017-06-19 09:40:51 +0000357
Diana Picus5b916532017-07-07 08:39:04 +0000358 // The opcode used for performing the comparison.
Diana Picus995746d2017-07-12 10:31:16 +0000359 const unsigned ComparisonOpcode;
Diana Picus621894a2017-06-19 09:40:51 +0000360
Diana Picus5b916532017-07-07 08:39:04 +0000361 // The opcode used for reading the flags set by the comparison. May be
362 // ARM::INSTRUCTION_LIST_END if we don't need to read the flags.
Diana Picus995746d2017-07-12 10:31:16 +0000363 const unsigned ReadFlagsOpcode;
Diana Picus5b916532017-07-07 08:39:04 +0000364
365 // The assumed register bank ID for the operands.
Diana Picus995746d2017-07-12 10:31:16 +0000366 const unsigned OperandRegBankID;
Diana Picus5b916532017-07-07 08:39:04 +0000367
Diana Picus21014df2017-07-12 09:01:54 +0000368 // The assumed size in bits for the operands.
Diana Picus995746d2017-07-12 10:31:16 +0000369 const unsigned OperandSize;
Diana Picus5b916532017-07-07 08:39:04 +0000370};
371
Diana Picus995746d2017-07-12 10:31:16 +0000372struct ARMInstructionSelector::InsertInfo {
373 InsertInfo(MachineInstrBuilder &MIB)
374 : MBB(*MIB->getParent()), InsertBefore(std::next(MIB->getIterator())),
375 DbgLoc(MIB->getDebugLoc()) {}
Diana Picus5b916532017-07-07 08:39:04 +0000376
Diana Picus995746d2017-07-12 10:31:16 +0000377 MachineBasicBlock &MBB;
378 const MachineBasicBlock::instr_iterator InsertBefore;
379 const DebugLoc &DbgLoc;
380};
Diana Picus5b916532017-07-07 08:39:04 +0000381
Diana Picus995746d2017-07-12 10:31:16 +0000382void ARMInstructionSelector::putConstant(InsertInfo I, unsigned DestReg,
383 unsigned Constant) const {
384 (void)BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(ARM::MOVi))
385 .addDef(DestReg)
386 .addImm(Constant)
387 .add(predOps(ARMCC::AL))
388 .add(condCodeOp());
389}
Diana Picus21014df2017-07-12 09:01:54 +0000390
Diana Picus995746d2017-07-12 10:31:16 +0000391bool ARMInstructionSelector::validOpRegPair(MachineRegisterInfo &MRI,
392 unsigned LHSReg, unsigned RHSReg,
393 unsigned ExpectedSize,
394 unsigned ExpectedRegBankID) const {
395 return MRI.getType(LHSReg) == MRI.getType(RHSReg) &&
396 validReg(MRI, LHSReg, ExpectedSize, ExpectedRegBankID) &&
397 validReg(MRI, RHSReg, ExpectedSize, ExpectedRegBankID);
398}
Diana Picus5b916532017-07-07 08:39:04 +0000399
Diana Picus995746d2017-07-12 10:31:16 +0000400bool ARMInstructionSelector::validReg(MachineRegisterInfo &MRI, unsigned Reg,
401 unsigned ExpectedSize,
402 unsigned ExpectedRegBankID) const {
403 if (MRI.getType(Reg).getSizeInBits() != ExpectedSize) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000404 LLVM_DEBUG(dbgs() << "Unexpected size for register");
Diana Picus995746d2017-07-12 10:31:16 +0000405 return false;
406 }
407
408 if (RBI.getRegBank(Reg, MRI, TRI)->getID() != ExpectedRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000409 LLVM_DEBUG(dbgs() << "Unexpected register bank for register");
Diana Picus995746d2017-07-12 10:31:16 +0000410 return false;
411 }
412
413 return true;
414}
415
416bool ARMInstructionSelector::selectCmp(CmpConstants Helper,
417 MachineInstrBuilder &MIB,
418 MachineRegisterInfo &MRI) const {
419 const InsertInfo I(MIB);
Diana Picus5b916532017-07-07 08:39:04 +0000420
Diana Picus621894a2017-06-19 09:40:51 +0000421 auto ResReg = MIB->getOperand(0).getReg();
Diana Picus995746d2017-07-12 10:31:16 +0000422 if (!validReg(MRI, ResReg, 1, ARM::GPRRegBankID))
Diana Picus5b916532017-07-07 08:39:04 +0000423 return false;
424
Diana Picus621894a2017-06-19 09:40:51 +0000425 auto Cond =
426 static_cast<CmpInst::Predicate>(MIB->getOperand(1).getPredicate());
Diana Picus5b916532017-07-07 08:39:04 +0000427 if (Cond == CmpInst::FCMP_TRUE || Cond == CmpInst::FCMP_FALSE) {
Diana Picus995746d2017-07-12 10:31:16 +0000428 putConstant(I, ResReg, Cond == CmpInst::FCMP_TRUE ? 1 : 0);
Diana Picus5b916532017-07-07 08:39:04 +0000429 MIB->eraseFromParent();
430 return true;
431 }
432
433 auto LHSReg = MIB->getOperand(2).getReg();
434 auto RHSReg = MIB->getOperand(3).getReg();
Diana Picus995746d2017-07-12 10:31:16 +0000435 if (!validOpRegPair(MRI, LHSReg, RHSReg, Helper.OperandSize,
436 Helper.OperandRegBankID))
Diana Picus621894a2017-06-19 09:40:51 +0000437 return false;
438
Diana Picus5b916532017-07-07 08:39:04 +0000439 auto ARMConds = getComparePreds(Cond);
Diana Picus995746d2017-07-12 10:31:16 +0000440 auto ZeroReg = MRI.createVirtualRegister(&ARM::GPRRegClass);
441 putConstant(I, ZeroReg, 0);
Diana Picus5b916532017-07-07 08:39:04 +0000442
443 if (ARMConds.second == ARMCC::AL) {
444 // Simple case, we only need one comparison and we're done.
Diana Picus995746d2017-07-12 10:31:16 +0000445 if (!insertComparison(Helper, I, ResReg, ARMConds.first, LHSReg, RHSReg,
446 ZeroReg))
Diana Picus5b916532017-07-07 08:39:04 +0000447 return false;
448 } else {
449 // Not so simple, we need two successive comparisons.
450 auto IntermediateRes = MRI.createVirtualRegister(&ARM::GPRRegClass);
Diana Picus995746d2017-07-12 10:31:16 +0000451 if (!insertComparison(Helper, I, IntermediateRes, ARMConds.first, LHSReg,
452 RHSReg, ZeroReg))
Diana Picus5b916532017-07-07 08:39:04 +0000453 return false;
Diana Picus995746d2017-07-12 10:31:16 +0000454 if (!insertComparison(Helper, I, ResReg, ARMConds.second, LHSReg, RHSReg,
455 IntermediateRes))
Diana Picus5b916532017-07-07 08:39:04 +0000456 return false;
457 }
Diana Picus621894a2017-06-19 09:40:51 +0000458
459 MIB->eraseFromParent();
460 return true;
461}
462
Diana Picus995746d2017-07-12 10:31:16 +0000463bool ARMInstructionSelector::insertComparison(CmpConstants Helper, InsertInfo I,
464 unsigned ResReg,
465 ARMCC::CondCodes Cond,
466 unsigned LHSReg, unsigned RHSReg,
467 unsigned PrevRes) const {
468 // Perform the comparison.
469 auto CmpI =
470 BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(Helper.ComparisonOpcode))
471 .addUse(LHSReg)
472 .addUse(RHSReg)
473 .add(predOps(ARMCC::AL));
474 if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI))
475 return false;
476
477 // Read the comparison flags (if necessary).
478 if (Helper.ReadFlagsOpcode != ARM::INSTRUCTION_LIST_END) {
479 auto ReadI = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc,
480 TII.get(Helper.ReadFlagsOpcode))
481 .add(predOps(ARMCC::AL));
482 if (!constrainSelectedInstRegOperands(*ReadI, TII, TRI, RBI))
483 return false;
484 }
485
486 // Select either 1 or the previous result based on the value of the flags.
487 auto Mov1I = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(ARM::MOVCCi))
488 .addDef(ResReg)
489 .addUse(PrevRes)
490 .addImm(1)
491 .add(predOps(Cond, ARM::CPSR));
492 if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI))
493 return false;
494
495 return true;
496}
497
Diana Picus930e6ec2017-08-03 09:14:59 +0000498bool ARMInstructionSelector::selectGlobal(MachineInstrBuilder &MIB,
499 MachineRegisterInfo &MRI) const {
Diana Picusabb08862017-09-05 07:57:41 +0000500 if ((STI.isROPI() || STI.isRWPI()) && !STI.isTargetELF()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000501 LLVM_DEBUG(dbgs() << "ROPI and RWPI only supported for ELF\n");
Diana Picus930e6ec2017-08-03 09:14:59 +0000502 return false;
503 }
Diana Picus930e6ec2017-08-03 09:14:59 +0000504
505 auto GV = MIB->getOperand(1).getGlobal();
506 if (GV->isThreadLocal()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000507 LLVM_DEBUG(dbgs() << "TLS variables not supported yet\n");
Diana Picus930e6ec2017-08-03 09:14:59 +0000508 return false;
509 }
510
511 auto &MBB = *MIB->getParent();
512 auto &MF = *MBB.getParent();
513
Diana Picusac154732017-09-05 08:22:47 +0000514 bool UseMovt = STI.useMovt(MF);
Diana Picus930e6ec2017-08-03 09:14:59 +0000515
Matt Arsenault41e5ac42018-03-14 00:36:23 +0000516 unsigned Size = TM.getPointerSize(0);
Diana Picusc9f29c62017-08-29 09:47:55 +0000517 unsigned Alignment = 4;
Diana Picusabb08862017-09-05 07:57:41 +0000518
519 auto addOpsForConstantPoolLoad = [&MF, Alignment,
520 Size](MachineInstrBuilder &MIB,
521 const GlobalValue *GV, bool IsSBREL) {
522 assert(MIB->getOpcode() == ARM::LDRi12 && "Unsupported instruction");
523 auto ConstPool = MF.getConstantPool();
524 auto CPIndex =
525 // For SB relative entries we need a target-specific constant pool.
526 // Otherwise, just use a regular constant pool entry.
527 IsSBREL
528 ? ConstPool->getConstantPoolIndex(
529 ARMConstantPoolConstant::Create(GV, ARMCP::SBREL), Alignment)
530 : ConstPool->getConstantPoolIndex(GV, Alignment);
531 MIB.addConstantPoolIndex(CPIndex, /*Offset*/ 0, /*TargetFlags*/ 0)
532 .addMemOperand(
533 MF.getMachineMemOperand(MachinePointerInfo::getConstantPool(MF),
534 MachineMemOperand::MOLoad, Size, Alignment))
535 .addImm(0)
536 .add(predOps(ARMCC::AL));
537 };
538
Diana Picusc9f29c62017-08-29 09:47:55 +0000539 if (TM.isPositionIndependent()) {
Diana Picusac154732017-09-05 08:22:47 +0000540 bool Indirect = STI.isGVIndirectSymbol(GV);
Diana Picusc9f29c62017-08-29 09:47:55 +0000541 // FIXME: Taking advantage of MOVT for ELF is pretty involved, so we don't
542 // support it yet. See PR28229.
543 unsigned Opc =
Diana Picusac154732017-09-05 08:22:47 +0000544 UseMovt && !STI.isTargetELF()
Diana Picusc9f29c62017-08-29 09:47:55 +0000545 ? (Indirect ? ARM::MOV_ga_pcrel_ldr : ARM::MOV_ga_pcrel)
546 : (Indirect ? ARM::LDRLIT_ga_pcrel_ldr : ARM::LDRLIT_ga_pcrel);
547 MIB->setDesc(TII.get(Opc));
548
Evgeniy Stepanov76d5ac42017-11-13 20:45:38 +0000549 int TargetFlags = ARMII::MO_NO_FLAG;
Diana Picusac154732017-09-05 08:22:47 +0000550 if (STI.isTargetDarwin())
Evgeniy Stepanov76d5ac42017-11-13 20:45:38 +0000551 TargetFlags |= ARMII::MO_NONLAZY;
552 if (STI.isGVInGOT(GV))
553 TargetFlags |= ARMII::MO_GOT;
554 MIB->getOperand(1).setTargetFlags(TargetFlags);
Diana Picusc9f29c62017-08-29 09:47:55 +0000555
556 if (Indirect)
557 MIB.addMemOperand(MF.getMachineMemOperand(
558 MachinePointerInfo::getGOT(MF), MachineMemOperand::MOLoad,
Matt Arsenault41e5ac42018-03-14 00:36:23 +0000559 TM.getProgramPointerSize(), Alignment));
Diana Picusc9f29c62017-08-29 09:47:55 +0000560
Diana Picusac154732017-09-05 08:22:47 +0000561 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
Diana Picusc9f29c62017-08-29 09:47:55 +0000562 }
563
Diana Picusf95979112017-09-01 11:13:39 +0000564 bool isReadOnly = STI.getTargetLowering()->isReadOnly(GV);
565 if (STI.isROPI() && isReadOnly) {
566 unsigned Opc = UseMovt ? ARM::MOV_ga_pcrel : ARM::LDRLIT_ga_pcrel;
567 MIB->setDesc(TII.get(Opc));
568 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
569 }
Diana Picusabb08862017-09-05 07:57:41 +0000570 if (STI.isRWPI() && !isReadOnly) {
571 auto Offset = MRI.createVirtualRegister(&ARM::GPRRegClass);
572 MachineInstrBuilder OffsetMIB;
573 if (UseMovt) {
574 OffsetMIB = BuildMI(MBB, *MIB, MIB->getDebugLoc(),
575 TII.get(ARM::MOVi32imm), Offset);
576 OffsetMIB.addGlobalAddress(GV, /*Offset*/ 0, ARMII::MO_SBREL);
577 } else {
578 // Load the offset from the constant pool.
579 OffsetMIB =
580 BuildMI(MBB, *MIB, MIB->getDebugLoc(), TII.get(ARM::LDRi12), Offset);
581 addOpsForConstantPoolLoad(OffsetMIB, GV, /*IsSBREL*/ true);
582 }
583 if (!constrainSelectedInstRegOperands(*OffsetMIB, TII, TRI, RBI))
584 return false;
585
586 // Add the offset to the SB register.
587 MIB->setDesc(TII.get(ARM::ADDrr));
588 MIB->RemoveOperand(1);
589 MIB.addReg(ARM::R9) // FIXME: don't hardcode R9
590 .addReg(Offset)
591 .add(predOps(ARMCC::AL))
592 .add(condCodeOp());
593
594 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
595 }
Diana Picusf95979112017-09-01 11:13:39 +0000596
Diana Picusac154732017-09-05 08:22:47 +0000597 if (STI.isTargetELF()) {
Diana Picus930e6ec2017-08-03 09:14:59 +0000598 if (UseMovt) {
599 MIB->setDesc(TII.get(ARM::MOVi32imm));
600 } else {
601 // Load the global's address from the constant pool.
602 MIB->setDesc(TII.get(ARM::LDRi12));
603 MIB->RemoveOperand(1);
Diana Picusabb08862017-09-05 07:57:41 +0000604 addOpsForConstantPoolLoad(MIB, GV, /*IsSBREL*/ false);
Diana Picus930e6ec2017-08-03 09:14:59 +0000605 }
Diana Picusac154732017-09-05 08:22:47 +0000606 } else if (STI.isTargetMachO()) {
Diana Picus930e6ec2017-08-03 09:14:59 +0000607 if (UseMovt)
608 MIB->setDesc(TII.get(ARM::MOVi32imm));
609 else
610 MIB->setDesc(TII.get(ARM::LDRLIT_ga_abs));
611 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000612 LLVM_DEBUG(dbgs() << "Object format not supported yet\n");
Diana Picus930e6ec2017-08-03 09:14:59 +0000613 return false;
614 }
615
616 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
617}
618
Diana Picus7145d222017-06-27 09:19:51 +0000619bool ARMInstructionSelector::selectSelect(MachineInstrBuilder &MIB,
Diana Picus995746d2017-07-12 10:31:16 +0000620 MachineRegisterInfo &MRI) const {
Diana Picus7145d222017-06-27 09:19:51 +0000621 auto &MBB = *MIB->getParent();
622 auto InsertBefore = std::next(MIB->getIterator());
Diana Picus77367372017-07-07 08:53:27 +0000623 auto &DbgLoc = MIB->getDebugLoc();
Diana Picus7145d222017-06-27 09:19:51 +0000624
625 // Compare the condition to 0.
626 auto CondReg = MIB->getOperand(1).getReg();
Diana Picus995746d2017-07-12 10:31:16 +0000627 assert(validReg(MRI, CondReg, 1, ARM::GPRRegBankID) &&
Diana Picus7145d222017-06-27 09:19:51 +0000628 "Unsupported types for select operation");
Diana Picus77367372017-07-07 08:53:27 +0000629 auto CmpI = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(ARM::CMPri))
Diana Picus7145d222017-06-27 09:19:51 +0000630 .addUse(CondReg)
631 .addImm(0)
632 .add(predOps(ARMCC::AL));
633 if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI))
634 return false;
635
636 // Move a value into the result register based on the result of the
637 // comparison.
638 auto ResReg = MIB->getOperand(0).getReg();
639 auto TrueReg = MIB->getOperand(2).getReg();
640 auto FalseReg = MIB->getOperand(3).getReg();
Diana Picus995746d2017-07-12 10:31:16 +0000641 assert(validOpRegPair(MRI, ResReg, TrueReg, 32, ARM::GPRRegBankID) &&
642 validOpRegPair(MRI, TrueReg, FalseReg, 32, ARM::GPRRegBankID) &&
Diana Picus7145d222017-06-27 09:19:51 +0000643 "Unsupported types for select operation");
Diana Picus77367372017-07-07 08:53:27 +0000644 auto Mov1I = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(ARM::MOVCCr))
Diana Picus7145d222017-06-27 09:19:51 +0000645 .addDef(ResReg)
646 .addUse(TrueReg)
647 .addUse(FalseReg)
648 .add(predOps(ARMCC::EQ, ARM::CPSR));
649 if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI))
650 return false;
651
652 MIB->eraseFromParent();
653 return true;
654}
655
Diana Picuse393bc72017-10-06 15:39:16 +0000656bool ARMInstructionSelector::selectShift(unsigned ShiftOpc,
657 MachineInstrBuilder &MIB) const {
658 MIB->setDesc(TII.get(ARM::MOVsr));
659 MIB.addImm(ShiftOpc);
660 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
661 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
662}
663
Daniel Sandersf76f3152017-11-16 00:46:35 +0000664bool ARMInstructionSelector::select(MachineInstr &I,
665 CodeGenCoverage &CoverageInfo) const {
Diana Picus812caee2016-12-16 12:54:46 +0000666 assert(I.getParent() && "Instruction should be in a basic block!");
667 assert(I.getParent()->getParent() && "Instruction should be in a function!");
668
669 auto &MBB = *I.getParent();
670 auto &MF = *MBB.getParent();
671 auto &MRI = MF.getRegInfo();
672
673 if (!isPreISelGenericOpcode(I.getOpcode())) {
674 if (I.isCopy())
675 return selectCopy(I, TII, MRI, TRI, RBI);
676
677 return true;
678 }
679
Diana Picus68773852017-12-22 11:09:18 +0000680 using namespace TargetOpcode;
Diana Picus68773852017-12-22 11:09:18 +0000681
Daniel Sandersf76f3152017-11-16 00:46:35 +0000682 if (selectImpl(I, CoverageInfo))
Diana Picus8abcbbb2017-05-02 09:40:49 +0000683 return true;
684
Diana Picus519807f2016-12-19 11:26:31 +0000685 MachineInstrBuilder MIB{MF, I};
Diana Picusd83df5d2017-01-25 08:47:40 +0000686 bool isSExt = false;
Diana Picus519807f2016-12-19 11:26:31 +0000687
Diana Picus519807f2016-12-19 11:26:31 +0000688 switch (I.getOpcode()) {
Diana Picus8b6c6be2017-01-25 08:10:40 +0000689 case G_SEXT:
Diana Picusd83df5d2017-01-25 08:47:40 +0000690 isSExt = true;
691 LLVM_FALLTHROUGH;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000692 case G_ZEXT: {
693 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
694 // FIXME: Smaller destination sizes coming soon!
695 if (DstTy.getSizeInBits() != 32) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000696 LLVM_DEBUG(dbgs() << "Unsupported destination size for extension");
Diana Picus8b6c6be2017-01-25 08:10:40 +0000697 return false;
698 }
699
700 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
701 unsigned SrcSize = SrcTy.getSizeInBits();
702 switch (SrcSize) {
Diana Picusd83df5d2017-01-25 08:47:40 +0000703 case 1: {
704 // ZExt boils down to & 0x1; for SExt we also subtract that from 0
705 I.setDesc(TII.get(ARM::ANDri));
706 MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp());
707
708 if (isSExt) {
709 unsigned SExtResult = I.getOperand(0).getReg();
710
711 // Use a new virtual register for the result of the AND
712 unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass);
713 I.getOperand(0).setReg(AndResult);
714
715 auto InsertBefore = std::next(I.getIterator());
Martin Bohme8396e142017-01-25 14:28:19 +0000716 auto SubI =
Diana Picusd83df5d2017-01-25 08:47:40 +0000717 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri))
718 .addDef(SExtResult)
719 .addUse(AndResult)
720 .addImm(0)
721 .add(predOps(ARMCC::AL))
722 .add(condCodeOp());
723 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
724 return false;
725 }
726 break;
727 }
Diana Picus8b6c6be2017-01-25 08:10:40 +0000728 case 8:
729 case 16: {
730 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
Diana Picuse8368782017-02-17 13:44:19 +0000731 if (NewOpc == I.getOpcode())
732 return false;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000733 I.setDesc(TII.get(NewOpc));
734 MIB.addImm(0).add(predOps(ARMCC::AL));
735 break;
736 }
737 default:
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000738 LLVM_DEBUG(dbgs() << "Unsupported source size for extension");
Diana Picus8b6c6be2017-01-25 08:10:40 +0000739 return false;
740 }
741 break;
742 }
Diana Picus657bfd32017-05-11 08:28:31 +0000743 case G_ANYEXT:
Diana Picus64a33432017-04-21 13:16:50 +0000744 case G_TRUNC: {
745 // The high bits are undefined, so there's nothing special to do, just
746 // treat it as a copy.
747 auto SrcReg = I.getOperand(1).getReg();
748 auto DstReg = I.getOperand(0).getReg();
749
750 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
751 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
752
Diana Picus75ce8522017-12-20 11:27:10 +0000753 if (SrcRegBank.getID() == ARM::FPRRegBankID) {
754 // This should only happen in the obscure case where we have put a 64-bit
755 // integer into a D register. Get it out of there and keep only the
756 // interesting part.
757 assert(I.getOpcode() == G_TRUNC && "Unsupported operand for G_ANYEXT");
758 assert(DstRegBank.getID() == ARM::GPRRegBankID &&
759 "Unsupported combination of register banks");
760 assert(MRI.getType(SrcReg).getSizeInBits() == 64 && "Unsupported size");
761 assert(MRI.getType(DstReg).getSizeInBits() <= 32 && "Unsupported size");
762
763 unsigned IgnoredBits = MRI.createVirtualRegister(&ARM::GPRRegClass);
764 auto InsertBefore = std::next(I.getIterator());
765 auto MovI =
766 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::VMOVRRD))
767 .addDef(DstReg)
768 .addDef(IgnoredBits)
769 .addUse(SrcReg)
770 .add(predOps(ARMCC::AL));
771 if (!constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI))
772 return false;
773
774 MIB->eraseFromParent();
775 return true;
776 }
777
Diana Picus64a33432017-04-21 13:16:50 +0000778 if (SrcRegBank.getID() != DstRegBank.getID()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000779 LLVM_DEBUG(
780 dbgs() << "G_TRUNC/G_ANYEXT operands on different register banks\n");
Diana Picus64a33432017-04-21 13:16:50 +0000781 return false;
782 }
783
784 if (SrcRegBank.getID() != ARM::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000785 LLVM_DEBUG(dbgs() << "G_TRUNC/G_ANYEXT on non-GPR not supported yet\n");
Diana Picus64a33432017-04-21 13:16:50 +0000786 return false;
787 }
788
789 I.setDesc(TII.get(COPY));
790 return selectCopy(I, TII, MRI, TRI, RBI);
791 }
Diana Picus37ae9f62018-01-04 10:54:57 +0000792 case G_CONSTANT: {
793 if (!MRI.getType(I.getOperand(0).getReg()).isPointer()) {
794 // Non-pointer constants should be handled by TableGen.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000795 LLVM_DEBUG(dbgs() << "Unsupported constant type\n");
Diana Picus37ae9f62018-01-04 10:54:57 +0000796 return false;
797 }
798
799 auto &Val = I.getOperand(1);
800 if (Val.isCImm()) {
801 if (!Val.getCImm()->isZero()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000802 LLVM_DEBUG(dbgs() << "Unsupported pointer constant value\n");
Diana Picus37ae9f62018-01-04 10:54:57 +0000803 return false;
804 }
805 Val.ChangeToImmediate(0);
806 } else {
807 assert(Val.isImm() && "Unexpected operand for G_CONSTANT");
808 if (Val.getImm() != 0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000809 LLVM_DEBUG(dbgs() << "Unsupported pointer constant value\n");
Diana Picus37ae9f62018-01-04 10:54:57 +0000810 return false;
811 }
812 }
813
814 I.setDesc(TII.get(ARM::MOVi));
815 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
816 break;
817 }
Diana Picus28a6d0e2017-12-22 13:05:51 +0000818 case G_INTTOPTR:
819 case G_PTRTOINT: {
820 auto SrcReg = I.getOperand(1).getReg();
821 auto DstReg = I.getOperand(0).getReg();
822
823 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
824 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
825
826 if (SrcRegBank.getID() != DstRegBank.getID()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000827 LLVM_DEBUG(
828 dbgs()
829 << "G_INTTOPTR/G_PTRTOINT operands on different register banks\n");
Diana Picus28a6d0e2017-12-22 13:05:51 +0000830 return false;
831 }
832
833 if (SrcRegBank.getID() != ARM::GPRRegBankID) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000834 LLVM_DEBUG(
835 dbgs() << "G_INTTOPTR/G_PTRTOINT on non-GPR not supported yet\n");
Diana Picus28a6d0e2017-12-22 13:05:51 +0000836 return false;
837 }
838
839 I.setDesc(TII.get(COPY));
840 return selectCopy(I, TII, MRI, TRI, RBI);
841 }
Diana Picus7145d222017-06-27 09:19:51 +0000842 case G_SELECT:
Diana Picus995746d2017-07-12 10:31:16 +0000843 return selectSelect(MIB, MRI);
844 case G_ICMP: {
845 CmpConstants Helper(ARM::CMPrr, ARM::INSTRUCTION_LIST_END,
846 ARM::GPRRegBankID, 32);
847 return selectCmp(Helper, MIB, MRI);
848 }
Diana Picus21014df2017-07-12 09:01:54 +0000849 case G_FCMP: {
Diana Picusac154732017-09-05 08:22:47 +0000850 assert(STI.hasVFP2() && "Can't select fcmp without VFP");
Diana Picus21014df2017-07-12 09:01:54 +0000851
852 unsigned OpReg = I.getOperand(2).getReg();
853 unsigned Size = MRI.getType(OpReg).getSizeInBits();
Diana Picus995746d2017-07-12 10:31:16 +0000854
Diana Picusac154732017-09-05 08:22:47 +0000855 if (Size == 64 && STI.isFPOnlySP()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000856 LLVM_DEBUG(dbgs() << "Subtarget only supports single precision");
Diana Picus995746d2017-07-12 10:31:16 +0000857 return false;
858 }
859 if (Size != 32 && Size != 64) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000860 LLVM_DEBUG(dbgs() << "Unsupported size for G_FCMP operand");
Diana Picus995746d2017-07-12 10:31:16 +0000861 return false;
Diana Picus21014df2017-07-12 09:01:54 +0000862 }
863
Diana Picus995746d2017-07-12 10:31:16 +0000864 CmpConstants Helper(Size == 32 ? ARM::VCMPS : ARM::VCMPD, ARM::FMSTAT,
865 ARM::FPRRegBankID, Size);
866 return selectCmp(Helper, MIB, MRI);
Diana Picus21014df2017-07-12 09:01:54 +0000867 }
Diana Picuse393bc72017-10-06 15:39:16 +0000868 case G_LSHR:
869 return selectShift(ARM_AM::ShiftOpc::lsr, MIB);
870 case G_ASHR:
871 return selectShift(ARM_AM::ShiftOpc::asr, MIB);
872 case G_SHL: {
873 return selectShift(ARM_AM::ShiftOpc::lsl, MIB);
874 }
Diana Picus9d070942017-02-28 10:14:38 +0000875 case G_GEP:
Diana Picus812caee2016-12-16 12:54:46 +0000876 I.setDesc(TII.get(ARM::ADDrr));
Diana Picus8a73f552017-01-13 10:18:01 +0000877 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
Diana Picus519807f2016-12-19 11:26:31 +0000878 break;
879 case G_FRAME_INDEX:
880 // Add 0 to the given frame index and hope it will eventually be folded into
881 // the user(s).
882 I.setDesc(TII.get(ARM::ADDri));
Diana Picus8a73f552017-01-13 10:18:01 +0000883 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
Diana Picus519807f2016-12-19 11:26:31 +0000884 break;
Diana Picus930e6ec2017-08-03 09:14:59 +0000885 case G_GLOBAL_VALUE:
886 return selectGlobal(MIB, MRI);
Diana Picus3b99c642017-02-24 14:01:27 +0000887 case G_STORE:
Diana Picus278c7222017-01-26 09:20:47 +0000888 case G_LOAD: {
Daniel Sanders3c1c4c02017-12-05 05:52:07 +0000889 const auto &MemOp = **I.memoperands_begin();
890 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000891 LLVM_DEBUG(dbgs() << "Atomic load/store not supported yet\n");
Daniel Sanders3c1c4c02017-12-05 05:52:07 +0000892 return false;
893 }
894
Diana Picus1540b062017-02-16 14:10:50 +0000895 unsigned Reg = I.getOperand(0).getReg();
896 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID();
897
898 LLT ValTy = MRI.getType(Reg);
Diana Picus278c7222017-01-26 09:20:47 +0000899 const auto ValSize = ValTy.getSizeInBits();
900
Diana Picusac154732017-09-05 08:22:47 +0000901 assert((ValSize != 64 || STI.hasVFP2()) &&
Diana Picus3b99c642017-02-24 14:01:27 +0000902 "Don't know how to load/store 64-bit value without VFP");
Diana Picus1540b062017-02-16 14:10:50 +0000903
Diana Picus3b99c642017-02-24 14:01:27 +0000904 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize);
905 if (NewOpc == G_LOAD || NewOpc == G_STORE)
Diana Picuse8368782017-02-17 13:44:19 +0000906 return false;
907
Diana Picus278c7222017-01-26 09:20:47 +0000908 I.setDesc(TII.get(NewOpc));
909
Diana Picus3b99c642017-02-24 14:01:27 +0000910 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH)
Diana Picus278c7222017-01-26 09:20:47 +0000911 // LDRH has a funny addressing mode (there's already a FIXME for it).
912 MIB.addReg(0);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000913 MIB.addImm(0).add(predOps(ARMCC::AL));
Diana Picus519807f2016-12-19 11:26:31 +0000914 break;
Diana Picus278c7222017-01-26 09:20:47 +0000915 }
Diana Picus0b4190a2017-06-07 12:35:05 +0000916 case G_MERGE_VALUES: {
917 if (!selectMergeValues(MIB, TII, MRI, TRI, RBI))
Diana Picusb1701e02017-02-16 12:19:57 +0000918 return false;
919 break;
920 }
Diana Picus0b4190a2017-06-07 12:35:05 +0000921 case G_UNMERGE_VALUES: {
922 if (!selectUnmergeValues(MIB, TII, MRI, TRI, RBI))
Diana Picusb1701e02017-02-16 12:19:57 +0000923 return false;
924 break;
925 }
Diana Picus87a70672017-07-14 09:46:06 +0000926 case G_BRCOND: {
927 if (!validReg(MRI, I.getOperand(0).getReg(), 1, ARM::GPRRegBankID)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000928 LLVM_DEBUG(dbgs() << "Unsupported condition register for G_BRCOND");
Diana Picus87a70672017-07-14 09:46:06 +0000929 return false;
930 }
931
932 // Set the flags.
933 auto Test = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(ARM::TSTri))
934 .addReg(I.getOperand(0).getReg())
935 .addImm(1)
936 .add(predOps(ARMCC::AL));
937 if (!constrainSelectedInstRegOperands(*Test, TII, TRI, RBI))
938 return false;
939
940 // Branch conditionally.
941 auto Branch = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(ARM::Bcc))
942 .add(I.getOperand(1))
Diana Picus863b5b02017-11-29 14:20:06 +0000943 .add(predOps(ARMCC::NE, ARM::CPSR));
Diana Picus87a70672017-07-14 09:46:06 +0000944 if (!constrainSelectedInstRegOperands(*Branch, TII, TRI, RBI))
945 return false;
946 I.eraseFromParent();
947 return true;
948 }
Diana Picus865f7fe2018-01-04 13:09:25 +0000949 case G_PHI: {
950 I.setDesc(TII.get(PHI));
951
952 unsigned DstReg = I.getOperand(0).getReg();
953 const TargetRegisterClass *RC = guessRegClass(DstReg, MRI, TRI, RBI);
954 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
955 break;
956 }
957
958 return true;
959 }
Diana Picus519807f2016-12-19 11:26:31 +0000960 default:
961 return false;
Diana Picus812caee2016-12-16 12:54:46 +0000962 }
963
Diana Picus519807f2016-12-19 11:26:31 +0000964 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Diana Picus22274932016-11-11 08:27:37 +0000965}