blob: 996bb1f00147c03a4506f16bbc014c1f6e40071e [file] [log] [blame]
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00001//===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARM.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000015#include "ARMBaseInstrInfo.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000016#include "ARMTargetMachine.h"
Evan Chenga20cde32011-07-20 23:34:39 +000017#include "MCTargetDesc/ARMAddressingModes.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
Weiming Zhaoc5987002013-02-14 18:10:21 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/CallingConv.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/IR/DerivedTypes.h"
27#include "llvm/IR/Function.h"
28#include "llvm/IR/Intrinsics.h"
29#include "llvm/IR/LLVMContext.h"
Evan Cheng8e6b40a2010-05-04 20:39:49 +000030#include "llvm/Support/CommandLine.h"
Chris Lattner1770fb82008-02-03 05:43:57 +000031#include "llvm/Support/Compiler.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000032#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000033#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Target/TargetLowering.h"
35#include "llvm/Target/TargetOptions.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000036
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000037using namespace llvm;
38
Chandler Carruth84e68b22014-04-22 02:41:26 +000039#define DEBUG_TYPE "arm-isel"
40
Evan Cheng59069ec2010-07-30 23:33:54 +000041static cl::opt<bool>
42DisableShifterOp("disable-shifter-op", cl::Hidden,
43 cl::desc("Disable isel of shifter-op"),
44 cl::init(false));
45
Evan Cheng62c7b5b2010-12-05 22:04:16 +000046static cl::opt<bool>
47CheckVMLxHazard("check-vmlx-hazard", cl::Hidden,
48 cl::desc("Check fp vmla / vmls hazard at isel time"),
Bob Wilson0858c3a2011-04-19 18:11:57 +000049 cl::init(true));
Evan Cheng62c7b5b2010-12-05 22:04:16 +000050
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000051//===--------------------------------------------------------------------===//
52/// ARMDAGToDAGISel - ARM specific code to select ARM machine
53/// instructions for SelectionDAG operations.
54///
55namespace {
Jim Grosbach08605202010-09-29 19:03:54 +000056
57enum AddrMode2Type {
58 AM2_BASE, // Simple AM2 (+-imm12)
59 AM2_SHOP // Shifter-op AM2
60};
61
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000062class ARMDAGToDAGISel : public SelectionDAGISel {
Evan Cheng10043e22007-01-19 07:51:42 +000063 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
64 /// make the right decision when generating code for different targets.
65 const ARMSubtarget *Subtarget;
66
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000067public:
Eric Christopher2f991c92014-07-03 22:24:49 +000068 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm, CodeGenOpt::Level OptLevel)
69 : SelectionDAGISel(tm, OptLevel) {}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000070
Eric Christopher0e6e7cf2014-05-22 02:00:27 +000071 bool runOnMachineFunction(MachineFunction &MF) override {
72 // Reset the subtarget each time through.
Eric Christopher2f991c92014-07-03 22:24:49 +000073 Subtarget = &MF.getTarget().getSubtarget<ARMSubtarget>();
Eric Christopher0e6e7cf2014-05-22 02:00:27 +000074 SelectionDAGISel::runOnMachineFunction(MF);
75 return true;
76 }
77
Craig Topper6bc27bf2014-03-10 02:09:33 +000078 const char *getPassName() const override {
Evan Cheng10043e22007-01-19 07:51:42 +000079 return "ARM Instruction Selection";
Anton Korobeynikov02bb33c2009-06-17 18:13:58 +000080 }
81
Craig Topper6bc27bf2014-03-10 02:09:33 +000082 void PreprocessISelDAG() override;
Evan Chengeae6d2c2012-12-19 20:16:09 +000083
Bob Wilson4facd962009-10-08 18:51:31 +000084 /// getI32Imm - Return a target constant of type i32 with the specified
85 /// value.
Anton Korobeynikov02bb33c2009-06-17 18:13:58 +000086 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000087 return CurDAG->getTargetConstant(Imm, MVT::i32);
Anton Korobeynikov02bb33c2009-06-17 18:13:58 +000088 }
89
Craig Topper6bc27bf2014-03-10 02:09:33 +000090 SDNode *Select(SDNode *N) override;
Evan Cheng5e73ff22010-02-15 19:41:07 +000091
Evan Cheng62c7b5b2010-12-05 22:04:16 +000092
93 bool hasNoVMLxHazardUse(SDNode *N) const;
Evan Cheng59bbc542010-10-27 23:41:30 +000094 bool isShifterOpProfitable(const SDValue &Shift,
95 ARM_AM::ShiftOpc ShOpcVal, unsigned ShAmt);
Owen Andersonb595ed02011-07-21 18:54:16 +000096 bool SelectRegShifterOperand(SDValue N, SDValue &A,
97 SDValue &B, SDValue &C,
98 bool CheckProfitability = true);
99 bool SelectImmShifterOperand(SDValue N, SDValue &A,
Owen Anderson04912702011-07-21 23:38:37 +0000100 SDValue &B, bool CheckProfitability = true);
101 bool SelectShiftRegShifterOperand(SDValue N, SDValue &A,
Owen Anderson6d557452011-03-18 19:46:58 +0000102 SDValue &B, SDValue &C) {
103 // Don't apply the profitability check
Owen Anderson04912702011-07-21 23:38:37 +0000104 return SelectRegShifterOperand(N, A, B, C, false);
105 }
106 bool SelectShiftImmShifterOperand(SDValue N, SDValue &A,
107 SDValue &B) {
108 // Don't apply the profitability check
109 return SelectImmShifterOperand(N, A, B, false);
Owen Anderson6d557452011-03-18 19:46:58 +0000110 }
111
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000112 bool SelectAddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
113 bool SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset, SDValue &Opc);
114
Jim Grosbach08605202010-09-29 19:03:54 +0000115 AddrMode2Type SelectAddrMode2Worker(SDValue N, SDValue &Base,
116 SDValue &Offset, SDValue &Opc);
117 bool SelectAddrMode2Base(SDValue N, SDValue &Base, SDValue &Offset,
118 SDValue &Opc) {
119 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_BASE;
120 }
121
122 bool SelectAddrMode2ShOp(SDValue N, SDValue &Base, SDValue &Offset,
123 SDValue &Opc) {
124 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_SHOP;
125 }
126
127 bool SelectAddrMode2(SDValue N, SDValue &Base, SDValue &Offset,
128 SDValue &Opc) {
129 SelectAddrMode2Worker(N, Base, Offset, Opc);
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000130// return SelectAddrMode2ShOp(N, Base, Offset, Opc);
Jim Grosbach08605202010-09-29 19:03:54 +0000131 // This always matches one way or another.
132 return true;
133 }
134
Tim Northover42180442013-08-22 09:57:11 +0000135 bool SelectCMOVPred(SDValue N, SDValue &Pred, SDValue &Reg) {
136 const ConstantSDNode *CN = cast<ConstantSDNode>(N);
137 Pred = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
138 Reg = CurDAG->getRegister(ARM::CPSR, MVT::i32);
139 return true;
140 }
141
Owen Anderson2aedba62011-07-26 20:54:26 +0000142 bool SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
143 SDValue &Offset, SDValue &Opc);
144 bool SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000145 SDValue &Offset, SDValue &Opc);
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000146 bool SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
147 SDValue &Offset, SDValue &Opc);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +0000148 bool SelectAddrOffsetNone(SDValue N, SDValue &Base);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000149 bool SelectAddrMode3(SDValue N, SDValue &Base,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000150 SDValue &Offset, SDValue &Opc);
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000151 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000152 SDValue &Offset, SDValue &Opc);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000153 bool SelectAddrMode5(SDValue N, SDValue &Base,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000154 SDValue &Offset);
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000155 bool SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,SDValue &Align);
Bob Wilsone3ecd5f2011-02-25 06:42:42 +0000156 bool SelectAddrMode6Offset(SDNode *Op, SDValue N, SDValue &Offset);
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000157
Evan Chengdfce83c2011-01-17 08:03:18 +0000158 bool SelectAddrModePC(SDValue N, SDValue &Offset, SDValue &Label);
Evan Cheng10043e22007-01-19 07:51:42 +0000159
Bill Wendling092a7bd2010-12-14 03:36:38 +0000160 // Thumb Addressing Modes:
Chris Lattner0e023ea2010-09-21 20:31:19 +0000161 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
Bill Wendling092a7bd2010-12-14 03:36:38 +0000162 bool SelectThumbAddrModeRI(SDValue N, SDValue &Base, SDValue &Offset,
163 unsigned Scale);
164 bool SelectThumbAddrModeRI5S1(SDValue N, SDValue &Base, SDValue &Offset);
165 bool SelectThumbAddrModeRI5S2(SDValue N, SDValue &Base, SDValue &Offset);
166 bool SelectThumbAddrModeRI5S4(SDValue N, SDValue &Base, SDValue &Offset);
167 bool SelectThumbAddrModeImm5S(SDValue N, unsigned Scale, SDValue &Base,
168 SDValue &OffImm);
169 bool SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
170 SDValue &OffImm);
171 bool SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
172 SDValue &OffImm);
173 bool SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
174 SDValue &OffImm);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000175 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
Evan Cheng10043e22007-01-19 07:51:42 +0000176
Bill Wendling092a7bd2010-12-14 03:36:38 +0000177 // Thumb 2 Addressing Modes:
Chris Lattner0e023ea2010-09-21 20:31:19 +0000178 bool SelectT2ShifterOperandReg(SDValue N,
Evan Chengeab9ca72009-06-27 02:26:13 +0000179 SDValue &BaseReg, SDValue &Opc);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000180 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
181 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
Evan Chengb23b50d2009-06-29 07:51:04 +0000182 SDValue &OffImm);
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000183 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Cheng84c6cda2009-07-02 07:28:31 +0000184 SDValue &OffImm);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000185 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
Evan Chengb23b50d2009-06-29 07:51:04 +0000186 SDValue &OffReg, SDValue &ShImm);
Tim Northovera7ecd242013-07-16 09:46:55 +0000187 bool SelectT2AddrModeExclusive(SDValue N, SDValue &Base, SDValue &OffImm);
Evan Chengb23b50d2009-06-29 07:51:04 +0000188
Evan Cheng0fc80842010-11-12 22:42:47 +0000189 inline bool is_so_imm(unsigned Imm) const {
190 return ARM_AM::getSOImmVal(Imm) != -1;
191 }
192
193 inline bool is_so_imm_not(unsigned Imm) const {
194 return ARM_AM::getSOImmVal(~Imm) != -1;
195 }
196
197 inline bool is_t2_so_imm(unsigned Imm) const {
198 return ARM_AM::getT2SOImmVal(Imm) != -1;
199 }
200
201 inline bool is_t2_so_imm_not(unsigned Imm) const {
202 return ARM_AM::getT2SOImmVal(~Imm) != -1;
203 }
204
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000205 // Include the pieces autogenerated from the target description.
206#include "ARMGenDAGISel.inc"
Bob Wilsona2c462b2009-05-19 05:53:42 +0000207
208private:
Evan Cheng84c6cda2009-07-02 07:28:31 +0000209 /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
210 /// ARM.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000211 SDNode *SelectARMIndexedLoad(SDNode *N);
212 SDNode *SelectT2IndexedLoad(SDNode *N);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000213
Bob Wilson340861d2010-03-23 05:25:43 +0000214 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
215 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson12b47992009-10-14 17:28:52 +0000216 /// loads of D registers and even subregs and odd subregs of Q registers.
Bob Wilson340861d2010-03-23 05:25:43 +0000217 /// For NumVecs <= 2, QOpcodes1 is not used.
Bob Wilson06fce872011-02-07 17:43:21 +0000218 SDNode *SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +0000219 const uint16_t *DOpcodes,
220 const uint16_t *QOpcodes0, const uint16_t *QOpcodes1);
Bob Wilson12b47992009-10-14 17:28:52 +0000221
Bob Wilsonc350cdf2009-10-14 18:32:29 +0000222 /// SelectVST - Select NEON store intrinsics. NumVecs should
Bob Wilsoncc0a2a72010-03-23 06:20:33 +0000223 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilsonc350cdf2009-10-14 18:32:29 +0000224 /// stores of D registers and even subregs and odd subregs of Q registers.
Bob Wilsoncc0a2a72010-03-23 06:20:33 +0000225 /// For NumVecs <= 2, QOpcodes1 is not used.
Bob Wilson06fce872011-02-07 17:43:21 +0000226 SDNode *SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +0000227 const uint16_t *DOpcodes,
228 const uint16_t *QOpcodes0, const uint16_t *QOpcodes1);
Bob Wilsonc350cdf2009-10-14 18:32:29 +0000229
Bob Wilson93117bc2009-10-14 16:46:45 +0000230 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
Bob Wilson4145e3a2009-10-14 16:19:03 +0000231 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilsond5c57a52010-09-13 23:01:35 +0000232 /// load/store of D registers and Q registers.
Bob Wilson06fce872011-02-07 17:43:21 +0000233 SDNode *SelectVLDSTLane(SDNode *N, bool IsLoad,
234 bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +0000235 const uint16_t *DOpcodes, const uint16_t *QOpcodes);
Bob Wilson4145e3a2009-10-14 16:19:03 +0000236
Bob Wilson2d790df2010-11-28 06:51:26 +0000237 /// SelectVLDDup - Select NEON load-duplicate intrinsics. NumVecs
238 /// should be 2, 3 or 4. The opcode array specifies the instructions used
239 /// for loading D registers. (Q registers are not supported.)
Bob Wilson06fce872011-02-07 17:43:21 +0000240 SDNode *SelectVLDDup(SDNode *N, bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +0000241 const uint16_t *Opcodes);
Bob Wilson2d790df2010-11-28 06:51:26 +0000242
Bob Wilson5bc8a792010-07-07 00:08:54 +0000243 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2,
244 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be
245 /// generated to force the table registers to be consecutive.
246 SDNode *SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
Bob Wilson3ed511b2010-07-06 23:36:25 +0000247
Sandeep Patel7460e082009-10-13 20:25:58 +0000248 /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
Jim Grosbach825cb292010-04-22 23:24:18 +0000249 SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
Sandeep Patel423e42b2009-10-13 18:59:48 +0000250
Bill Wendlinga7d697e2011-10-10 22:59:55 +0000251 // Select special operations if node forms integer ABS pattern
252 SDNode *SelectABSOp(SDNode *N);
253
Weiming Zhaoc5987002013-02-14 18:10:21 +0000254 SDNode *SelectInlineAsm(SDNode *N);
255
Evan Chengd85631e2010-05-05 18:28:36 +0000256 SDNode *SelectConcatVector(SDNode *N);
257
Evan Chengd9c55362009-07-02 01:23:32 +0000258 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
259 /// inline asm expressions.
Craig Topper6bc27bf2014-03-10 02:09:33 +0000260 bool SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
261 std::vector<SDValue> &OutOps) override;
Bob Wilsone6b778d2009-10-06 22:01:59 +0000262
Weiming Zhao95782222012-11-17 00:23:35 +0000263 // Form pairs of consecutive R, S, D, or Q registers.
Weiming Zhao8f56f882012-11-16 21:55:34 +0000264 SDNode *createGPRPairNode(EVT VT, SDValue V0, SDValue V1);
Weiming Zhao95782222012-11-17 00:23:35 +0000265 SDNode *createSRegPairNode(EVT VT, SDValue V0, SDValue V1);
266 SDNode *createDRegPairNode(EVT VT, SDValue V0, SDValue V1);
267 SDNode *createQRegPairNode(EVT VT, SDValue V0, SDValue V1);
Evan Chengc2ae5f52010-05-10 17:34:18 +0000268
Bob Wilsond8a9a042010-06-04 00:04:02 +0000269 // Form sequences of 4 consecutive S, D, or Q registers.
Weiming Zhao95782222012-11-17 00:23:35 +0000270 SDNode *createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
271 SDNode *createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
272 SDNode *createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000273
274 // Get the alignment operand for a NEON VLD or VST instruction.
275 SDValue GetVLDSTAlign(SDValue Align, unsigned NumVecs, bool is64BitVector);
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000276};
Evan Cheng10043e22007-01-19 07:51:42 +0000277}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000278
Sandeep Patel423e42b2009-10-13 18:59:48 +0000279/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
280/// operand. If so Imm will receive the 32-bit value.
281static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
282 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
283 Imm = cast<ConstantSDNode>(N)->getZExtValue();
284 return true;
285 }
286 return false;
287}
288
289// isInt32Immediate - This method tests to see if a constant operand.
290// If so Imm will receive the 32 bit value.
291static bool isInt32Immediate(SDValue N, unsigned &Imm) {
292 return isInt32Immediate(N.getNode(), Imm);
293}
294
295// isOpcWithIntImmediate - This method tests to see if the node is a specific
296// opcode and that it has a immediate integer right operand.
297// If so Imm will receive the 32 bit value.
298static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
299 return N->getOpcode() == Opc &&
300 isInt32Immediate(N->getOperand(1).getNode(), Imm);
301}
302
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000303/// \brief Check whether a particular node is a constant value representable as
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000304/// (N * Scale) where (N in [\p RangeMin, \p RangeMax).
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000305///
306/// \param ScaledConstant [out] - On success, the pre-scaled constant value.
Jakob Stoklund Olesen2056d152011-09-23 22:10:33 +0000307static bool isScaledConstantInRange(SDValue Node, int Scale,
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000308 int RangeMin, int RangeMax,
309 int &ScaledConstant) {
Jakob Stoklund Olesen2056d152011-09-23 22:10:33 +0000310 assert(Scale > 0 && "Invalid scale!");
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000311
312 // Check that this is a constant.
313 const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Node);
314 if (!C)
315 return false;
316
317 ScaledConstant = (int) C->getZExtValue();
318 if ((ScaledConstant % Scale) != 0)
319 return false;
320
321 ScaledConstant /= Scale;
322 return ScaledConstant >= RangeMin && ScaledConstant < RangeMax;
323}
324
Evan Chengeae6d2c2012-12-19 20:16:09 +0000325void ARMDAGToDAGISel::PreprocessISelDAG() {
326 if (!Subtarget->hasV6T2Ops())
327 return;
328
329 bool isThumb2 = Subtarget->isThumb();
330 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
331 E = CurDAG->allnodes_end(); I != E; ) {
332 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
333
334 if (N->getOpcode() != ISD::ADD)
335 continue;
336
337 // Look for (add X1, (and (srl X2, c1), c2)) where c2 is constant with
338 // leading zeros, followed by consecutive set bits, followed by 1 or 2
339 // trailing zeros, e.g. 1020.
340 // Transform the expression to
341 // (add X1, (shl (and (srl X2, c1), (c2>>tz)), tz)) where tz is the number
342 // of trailing zeros of c2. The left shift would be folded as an shifter
343 // operand of 'add' and the 'and' and 'srl' would become a bits extraction
344 // node (UBFX).
345
346 SDValue N0 = N->getOperand(0);
347 SDValue N1 = N->getOperand(1);
348 unsigned And_imm = 0;
349 if (!isOpcWithIntImmediate(N1.getNode(), ISD::AND, And_imm)) {
350 if (isOpcWithIntImmediate(N0.getNode(), ISD::AND, And_imm))
351 std::swap(N0, N1);
352 }
353 if (!And_imm)
354 continue;
355
356 // Check if the AND mask is an immediate of the form: 000.....1111111100
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000357 unsigned TZ = countTrailingZeros(And_imm);
Evan Chengeae6d2c2012-12-19 20:16:09 +0000358 if (TZ != 1 && TZ != 2)
359 // Be conservative here. Shifter operands aren't always free. e.g. On
360 // Swift, left shifter operand of 1 / 2 for free but others are not.
361 // e.g.
362 // ubfx r3, r1, #16, #8
363 // ldr.w r3, [r0, r3, lsl #2]
364 // vs.
365 // mov.w r9, #1020
366 // and.w r2, r9, r1, lsr #14
367 // ldr r2, [r0, r2]
368 continue;
369 And_imm >>= TZ;
370 if (And_imm & (And_imm + 1))
371 continue;
372
373 // Look for (and (srl X, c1), c2).
374 SDValue Srl = N1.getOperand(0);
375 unsigned Srl_imm = 0;
376 if (!isOpcWithIntImmediate(Srl.getNode(), ISD::SRL, Srl_imm) ||
377 (Srl_imm <= 2))
378 continue;
379
380 // Make sure first operand is not a shifter operand which would prevent
381 // folding of the left shift.
382 SDValue CPTmp0;
383 SDValue CPTmp1;
384 SDValue CPTmp2;
385 if (isThumb2) {
386 if (SelectT2ShifterOperandReg(N0, CPTmp0, CPTmp1))
387 continue;
388 } else {
389 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1) ||
390 SelectRegShifterOperand(N0, CPTmp0, CPTmp1, CPTmp2))
391 continue;
392 }
393
394 // Now make the transformation.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000395 Srl = CurDAG->getNode(ISD::SRL, SDLoc(Srl), MVT::i32,
Evan Chengeae6d2c2012-12-19 20:16:09 +0000396 Srl.getOperand(0),
397 CurDAG->getConstant(Srl_imm+TZ, MVT::i32));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000398 N1 = CurDAG->getNode(ISD::AND, SDLoc(N1), MVT::i32,
Evan Chengeae6d2c2012-12-19 20:16:09 +0000399 Srl, CurDAG->getConstant(And_imm, MVT::i32));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000400 N1 = CurDAG->getNode(ISD::SHL, SDLoc(N1), MVT::i32,
Evan Chengeae6d2c2012-12-19 20:16:09 +0000401 N1, CurDAG->getConstant(TZ, MVT::i32));
402 CurDAG->UpdateNodeOperands(N, N0, N1);
Jim Grosbach1a597112014-04-03 23:43:18 +0000403 }
Evan Chengeae6d2c2012-12-19 20:16:09 +0000404}
405
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000406/// hasNoVMLxHazardUse - Return true if it's desirable to select a FP MLA / MLS
407/// node. VFP / NEON fp VMLA / VMLS instructions have special RAW hazards (at
408/// least on current ARM implementations) which should be avoidded.
409bool ARMDAGToDAGISel::hasNoVMLxHazardUse(SDNode *N) const {
410 if (OptLevel == CodeGenOpt::None)
411 return true;
412
413 if (!CheckVMLxHazard)
414 return true;
Bob Wilsone8a549c2012-09-29 21:43:49 +0000415
Tim Northover0feb91e2014-04-01 14:10:07 +0000416 if (!Subtarget->isCortexA7() && !Subtarget->isCortexA8() &&
417 !Subtarget->isCortexA9() && !Subtarget->isSwift())
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000418 return true;
419
420 if (!N->hasOneUse())
421 return false;
422
423 SDNode *Use = *N->use_begin();
424 if (Use->getOpcode() == ISD::CopyToReg)
425 return true;
426 if (Use->isMachineOpcode()) {
Eric Christopher2f991c92014-07-03 22:24:49 +0000427 const ARMBaseInstrInfo *TII = static_cast<const ARMBaseInstrInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000428 CurDAG->getSubtarget().getInstrInfo());
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000429
Evan Cheng6cc775f2011-06-28 19:10:37 +0000430 const MCInstrDesc &MCID = TII->get(Use->getMachineOpcode());
431 if (MCID.mayStore())
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000432 return true;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000433 unsigned Opcode = MCID.getOpcode();
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000434 if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
435 return true;
436 // vmlx feeding into another vmlx. We actually want to unfold
437 // the use later in the MLxExpansion pass. e.g.
438 // vmla
439 // vmla (stall 8 cycles)
440 //
441 // vmul (5 cycles)
442 // vadd (5 cycles)
443 // vmla
444 // This adds up to about 18 - 19 cycles.
445 //
446 // vmla
447 // vmul (stall 4 cycles)
448 // vadd adds up to about 14 cycles.
449 return TII->isFpMLxInstruction(Opcode);
450 }
451
452 return false;
453}
Sandeep Patel423e42b2009-10-13 18:59:48 +0000454
Evan Cheng59bbc542010-10-27 23:41:30 +0000455bool ARMDAGToDAGISel::isShifterOpProfitable(const SDValue &Shift,
456 ARM_AM::ShiftOpc ShOpcVal,
457 unsigned ShAmt) {
Bob Wilsone8a549c2012-09-29 21:43:49 +0000458 if (!Subtarget->isLikeA9() && !Subtarget->isSwift())
Evan Cheng59bbc542010-10-27 23:41:30 +0000459 return true;
460 if (Shift.hasOneUse())
461 return true;
462 // R << 2 is free.
Bob Wilsone8a549c2012-09-29 21:43:49 +0000463 return ShOpcVal == ARM_AM::lsl &&
464 (ShAmt == 2 || (Subtarget->isSwift() && ShAmt == 1));
Evan Cheng59bbc542010-10-27 23:41:30 +0000465}
466
Owen Andersonb595ed02011-07-21 18:54:16 +0000467bool ARMDAGToDAGISel::SelectImmShifterOperand(SDValue N,
Evan Chengb23b50d2009-06-29 07:51:04 +0000468 SDValue &BaseReg,
Owen Anderson6d557452011-03-18 19:46:58 +0000469 SDValue &Opc,
470 bool CheckProfitability) {
Evan Cheng59069ec2010-07-30 23:33:54 +0000471 if (DisableShifterOp)
472 return false;
473
Evan Chenga20cde32011-07-20 23:34:39 +0000474 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
Evan Chengb23b50d2009-06-29 07:51:04 +0000475
476 // Don't match base register only case. That is matched to a separate
477 // lower complexity pattern with explicit register operand.
478 if (ShOpcVal == ARM_AM::no_shift) return false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000479
Evan Chengb23b50d2009-06-29 07:51:04 +0000480 BaseReg = N.getOperand(0);
481 unsigned ShImmVal = 0;
Owen Andersonb595ed02011-07-21 18:54:16 +0000482 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
483 if (!RHS) return false;
Owen Andersonb595ed02011-07-21 18:54:16 +0000484 ShImmVal = RHS->getZExtValue() & 31;
Evan Cheng59bbc542010-10-27 23:41:30 +0000485 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
486 MVT::i32);
487 return true;
488}
489
Owen Andersonb595ed02011-07-21 18:54:16 +0000490bool ARMDAGToDAGISel::SelectRegShifterOperand(SDValue N,
491 SDValue &BaseReg,
492 SDValue &ShReg,
493 SDValue &Opc,
494 bool CheckProfitability) {
495 if (DisableShifterOp)
496 return false;
497
498 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
499
500 // Don't match base register only case. That is matched to a separate
501 // lower complexity pattern with explicit register operand.
502 if (ShOpcVal == ARM_AM::no_shift) return false;
503
504 BaseReg = N.getOperand(0);
505 unsigned ShImmVal = 0;
506 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
507 if (RHS) return false;
508
509 ShReg = N.getOperand(1);
510 if (CheckProfitability && !isShifterOpProfitable(N, ShOpcVal, ShImmVal))
511 return false;
512 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
513 MVT::i32);
514 return true;
515}
516
517
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000518bool ARMDAGToDAGISel::SelectAddrModeImm12(SDValue N,
519 SDValue &Base,
520 SDValue &OffImm) {
521 // Match simple R + imm12 operands.
522
523 // Base only.
Chris Lattner46c01a32011-02-13 22:25:43 +0000524 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
525 !CurDAG->isBaseWithConstantOffset(N)) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000526 if (N.getOpcode() == ISD::FrameIndex) {
Chris Lattner46c01a32011-02-13 22:25:43 +0000527 // Match frame index.
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000528 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000529 Base = CurDAG->getTargetFrameIndex(FI,
530 getTargetLowering()->getPointerTy());
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000531 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
532 return true;
Chris Lattner46c01a32011-02-13 22:25:43 +0000533 }
Owen Anderson6d557452011-03-18 19:46:58 +0000534
Chris Lattner46c01a32011-02-13 22:25:43 +0000535 if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northover72360d22013-12-02 10:35:41 +0000536 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000537 Base = N.getOperand(0);
538 } else
539 Base = N;
540 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
541 return true;
542 }
543
544 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Renato Golin63e27982014-09-09 09:57:59 +0000545 int RHSC = (int)RHS->getSExtValue();
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000546 if (N.getOpcode() == ISD::SUB)
547 RHSC = -RHSC;
548
Renato Golin63e27982014-09-09 09:57:59 +0000549 if (RHSC > -0x1000 && RHSC < 0x1000) { // 12 bits
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000550 Base = N.getOperand(0);
551 if (Base.getOpcode() == ISD::FrameIndex) {
552 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000553 Base = CurDAG->getTargetFrameIndex(FI,
554 getTargetLowering()->getPointerTy());
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000555 }
556 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
557 return true;
558 }
559 }
560
561 // Base only.
562 Base = N;
563 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
564 return true;
565}
566
567
568
569bool ARMDAGToDAGISel::SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset,
570 SDValue &Opc) {
Evan Cheng59bbc542010-10-27 23:41:30 +0000571 if (N.getOpcode() == ISD::MUL &&
Bob Wilsone8a549c2012-09-29 21:43:49 +0000572 ((!Subtarget->isLikeA9() && !Subtarget->isSwift()) || N.hasOneUse())) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000573 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
574 // X * [3,5,9] -> X + X * [2,4,8] etc.
575 int RHSC = (int)RHS->getZExtValue();
576 if (RHSC & 1) {
577 RHSC = RHSC & ~1;
578 ARM_AM::AddrOpc AddSub = ARM_AM::add;
579 if (RHSC < 0) {
580 AddSub = ARM_AM::sub;
581 RHSC = - RHSC;
582 }
583 if (isPowerOf2_32(RHSC)) {
584 unsigned ShAmt = Log2_32(RHSC);
585 Base = Offset = N.getOperand(0);
586 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
587 ARM_AM::lsl),
588 MVT::i32);
589 return true;
590 }
591 }
592 }
593 }
594
Chris Lattner46c01a32011-02-13 22:25:43 +0000595 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
596 // ISD::OR that is equivalent to an ISD::ADD.
597 !CurDAG->isBaseWithConstantOffset(N))
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000598 return false;
599
600 // Leave simple R +/- imm12 operands for LDRi12
Chris Lattner46c01a32011-02-13 22:25:43 +0000601 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::OR) {
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000602 int RHSC;
603 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
604 -0x1000+1, 0x1000, RHSC)) // 12 bits.
605 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000606 }
607
608 // Otherwise this is R +/- [possibly shifted] R.
Chris Lattner46c01a32011-02-13 22:25:43 +0000609 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::SUB ? ARM_AM::sub:ARM_AM::add;
Evan Chenga20cde32011-07-20 23:34:39 +0000610 ARM_AM::ShiftOpc ShOpcVal =
611 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode());
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000612 unsigned ShAmt = 0;
613
614 Base = N.getOperand(0);
615 Offset = N.getOperand(1);
616
617 if (ShOpcVal != ARM_AM::no_shift) {
618 // Check to see if the RHS of the shift is a constant, if not, we can't fold
619 // it.
620 if (ConstantSDNode *Sh =
621 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
622 ShAmt = Sh->getZExtValue();
Evan Cheng59bbc542010-10-27 23:41:30 +0000623 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
624 Offset = N.getOperand(1).getOperand(0);
625 else {
626 ShAmt = 0;
627 ShOpcVal = ARM_AM::no_shift;
628 }
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000629 } else {
630 ShOpcVal = ARM_AM::no_shift;
631 }
632 }
633
634 // Try matching (R shl C) + (R).
Chris Lattner46c01a32011-02-13 22:25:43 +0000635 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
Bob Wilsone8a549c2012-09-29 21:43:49 +0000636 !(Subtarget->isLikeA9() || Subtarget->isSwift() ||
637 N.getOperand(0).hasOneUse())) {
Evan Chenga20cde32011-07-20 23:34:39 +0000638 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode());
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000639 if (ShOpcVal != ARM_AM::no_shift) {
640 // Check to see if the RHS of the shift is a constant, if not, we can't
641 // fold it.
642 if (ConstantSDNode *Sh =
643 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
644 ShAmt = Sh->getZExtValue();
Cameron Zwarich842f99a2011-10-05 23:39:02 +0000645 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) {
Evan Cheng59bbc542010-10-27 23:41:30 +0000646 Offset = N.getOperand(0).getOperand(0);
647 Base = N.getOperand(1);
648 } else {
649 ShAmt = 0;
650 ShOpcVal = ARM_AM::no_shift;
651 }
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000652 } else {
653 ShOpcVal = ARM_AM::no_shift;
654 }
655 }
656 }
657
658 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
659 MVT::i32);
660 return true;
661}
662
663
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000664//-----
665
Jim Grosbach08605202010-09-29 19:03:54 +0000666AddrMode2Type ARMDAGToDAGISel::SelectAddrMode2Worker(SDValue N,
667 SDValue &Base,
668 SDValue &Offset,
669 SDValue &Opc) {
Evan Cheng59bbc542010-10-27 23:41:30 +0000670 if (N.getOpcode() == ISD::MUL &&
Bob Wilsone8a549c2012-09-29 21:43:49 +0000671 (!(Subtarget->isLikeA9() || Subtarget->isSwift()) || N.hasOneUse())) {
Evan Cheng72a8bcf2007-03-13 21:05:54 +0000672 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
673 // X * [3,5,9] -> X + X * [2,4,8] etc.
Dan Gohmaneffb8942008-09-12 16:56:44 +0000674 int RHSC = (int)RHS->getZExtValue();
Evan Cheng72a8bcf2007-03-13 21:05:54 +0000675 if (RHSC & 1) {
676 RHSC = RHSC & ~1;
677 ARM_AM::AddrOpc AddSub = ARM_AM::add;
678 if (RHSC < 0) {
679 AddSub = ARM_AM::sub;
680 RHSC = - RHSC;
681 }
682 if (isPowerOf2_32(RHSC)) {
683 unsigned ShAmt = Log2_32(RHSC);
684 Base = Offset = N.getOperand(0);
685 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
686 ARM_AM::lsl),
Owen Anderson9f944592009-08-11 20:47:22 +0000687 MVT::i32);
Jim Grosbach08605202010-09-29 19:03:54 +0000688 return AM2_SHOP;
Evan Cheng72a8bcf2007-03-13 21:05:54 +0000689 }
690 }
691 }
692 }
693
Chris Lattner46c01a32011-02-13 22:25:43 +0000694 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
695 // ISD::OR that is equivalent to an ADD.
696 !CurDAG->isBaseWithConstantOffset(N)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000697 Base = N;
698 if (N.getOpcode() == ISD::FrameIndex) {
699 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000700 Base = CurDAG->getTargetFrameIndex(FI,
701 getTargetLowering()->getPointerTy());
Anton Korobeynikov25229082009-11-24 00:44:37 +0000702 } else if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northover72360d22013-12-02 10:35:41 +0000703 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress) {
Evan Cheng10043e22007-01-19 07:51:42 +0000704 Base = N.getOperand(0);
705 }
Owen Anderson9f944592009-08-11 20:47:22 +0000706 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000707 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
708 ARM_AM::no_shift),
Owen Anderson9f944592009-08-11 20:47:22 +0000709 MVT::i32);
Jim Grosbach08605202010-09-29 19:03:54 +0000710 return AM2_BASE;
Rafael Espindola708cb602006-11-08 17:07:32 +0000711 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000712
Evan Cheng10043e22007-01-19 07:51:42 +0000713 // Match simple R +/- imm12 operands.
Chris Lattner46c01a32011-02-13 22:25:43 +0000714 if (N.getOpcode() != ISD::SUB) {
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000715 int RHSC;
716 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
717 -0x1000+1, 0x1000, RHSC)) { // 12 bits.
718 Base = N.getOperand(0);
719 if (Base.getOpcode() == ISD::FrameIndex) {
720 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000721 Base = CurDAG->getTargetFrameIndex(FI,
722 getTargetLowering()->getPointerTy());
Rafael Espindola708cb602006-11-08 17:07:32 +0000723 }
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000724 Offset = CurDAG->getRegister(0, MVT::i32);
725
726 ARM_AM::AddrOpc AddSub = ARM_AM::add;
727 if (RHSC < 0) {
728 AddSub = ARM_AM::sub;
729 RHSC = - RHSC;
730 }
731 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
732 ARM_AM::no_shift),
733 MVT::i32);
734 return AM2_BASE;
Evan Cheng10043e22007-01-19 07:51:42 +0000735 }
Jim Grosbachc7b10f32010-09-29 17:32:29 +0000736 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000737
Bob Wilsone8a549c2012-09-29 21:43:49 +0000738 if ((Subtarget->isLikeA9() || Subtarget->isSwift()) && !N.hasOneUse()) {
Evan Cheng59bbc542010-10-27 23:41:30 +0000739 // Compute R +/- (R << N) and reuse it.
740 Base = N;
741 Offset = CurDAG->getRegister(0, MVT::i32);
742 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
743 ARM_AM::no_shift),
744 MVT::i32);
745 return AM2_BASE;
746 }
747
Johnny Chenb678a562009-10-27 17:25:15 +0000748 // Otherwise this is R +/- [possibly shifted] R.
Chris Lattner46c01a32011-02-13 22:25:43 +0000749 ARM_AM::AddrOpc AddSub = N.getOpcode() != ISD::SUB ? ARM_AM::add:ARM_AM::sub;
Evan Chenga20cde32011-07-20 23:34:39 +0000750 ARM_AM::ShiftOpc ShOpcVal =
751 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +0000752 unsigned ShAmt = 0;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000753
Evan Cheng10043e22007-01-19 07:51:42 +0000754 Base = N.getOperand(0);
755 Offset = N.getOperand(1);
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000756
Evan Cheng10043e22007-01-19 07:51:42 +0000757 if (ShOpcVal != ARM_AM::no_shift) {
758 // Check to see if the RHS of the shift is a constant, if not, we can't fold
759 // it.
760 if (ConstantSDNode *Sh =
761 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000762 ShAmt = Sh->getZExtValue();
Evan Cheng59bbc542010-10-27 23:41:30 +0000763 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
764 Offset = N.getOperand(1).getOperand(0);
765 else {
766 ShAmt = 0;
767 ShOpcVal = ARM_AM::no_shift;
768 }
Evan Cheng10043e22007-01-19 07:51:42 +0000769 } else {
770 ShOpcVal = ARM_AM::no_shift;
Rafael Espindola708cb602006-11-08 17:07:32 +0000771 }
772 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000773
Evan Cheng10043e22007-01-19 07:51:42 +0000774 // Try matching (R shl C) + (R).
Chris Lattner46c01a32011-02-13 22:25:43 +0000775 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
Bob Wilsone8a549c2012-09-29 21:43:49 +0000776 !(Subtarget->isLikeA9() || Subtarget->isSwift() ||
777 N.getOperand(0).hasOneUse())) {
Evan Chenga20cde32011-07-20 23:34:39 +0000778 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +0000779 if (ShOpcVal != ARM_AM::no_shift) {
780 // Check to see if the RHS of the shift is a constant, if not, we can't
781 // fold it.
782 if (ConstantSDNode *Sh =
783 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000784 ShAmt = Sh->getZExtValue();
Cameron Zwarich842f99a2011-10-05 23:39:02 +0000785 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) {
Evan Cheng59bbc542010-10-27 23:41:30 +0000786 Offset = N.getOperand(0).getOperand(0);
787 Base = N.getOperand(1);
788 } else {
789 ShAmt = 0;
790 ShOpcVal = ARM_AM::no_shift;
791 }
Evan Cheng10043e22007-01-19 07:51:42 +0000792 } else {
793 ShOpcVal = ARM_AM::no_shift;
794 }
795 }
796 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000797
Evan Cheng10043e22007-01-19 07:51:42 +0000798 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson9f944592009-08-11 20:47:22 +0000799 MVT::i32);
Jim Grosbach08605202010-09-29 19:03:54 +0000800 return AM2_SHOP;
Rafael Espindola708cb602006-11-08 17:07:32 +0000801}
802
Owen Anderson2aedba62011-07-26 20:54:26 +0000803bool ARMDAGToDAGISel::SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000804 SDValue &Offset, SDValue &Opc) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000805 unsigned Opcode = Op->getOpcode();
Evan Cheng10043e22007-01-19 07:51:42 +0000806 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
807 ? cast<LoadSDNode>(Op)->getAddressingMode()
808 : cast<StoreSDNode>(Op)->getAddressingMode();
809 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
810 ? ARM_AM::add : ARM_AM::sub;
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000811 int Val;
Owen Anderson2aedba62011-07-26 20:54:26 +0000812 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val))
813 return false;
Evan Cheng10043e22007-01-19 07:51:42 +0000814
815 Offset = N;
Evan Chenga20cde32011-07-20 23:34:39 +0000816 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +0000817 unsigned ShAmt = 0;
818 if (ShOpcVal != ARM_AM::no_shift) {
819 // Check to see if the RHS of the shift is a constant, if not, we can't fold
820 // it.
821 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000822 ShAmt = Sh->getZExtValue();
Evan Cheng59bbc542010-10-27 23:41:30 +0000823 if (isShifterOpProfitable(N, ShOpcVal, ShAmt))
824 Offset = N.getOperand(0);
825 else {
826 ShAmt = 0;
827 ShOpcVal = ARM_AM::no_shift;
828 }
Evan Cheng10043e22007-01-19 07:51:42 +0000829 } else {
830 ShOpcVal = ARM_AM::no_shift;
831 }
832 }
833
834 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson9f944592009-08-11 20:47:22 +0000835 MVT::i32);
Rafael Espindola19398ec2006-10-17 18:04:53 +0000836 return true;
837}
838
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000839bool ARMDAGToDAGISel::SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
840 SDValue &Offset, SDValue &Opc) {
Owen Anderson939cd212011-08-31 20:00:11 +0000841 unsigned Opcode = Op->getOpcode();
842 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
843 ? cast<LoadSDNode>(Op)->getAddressingMode()
844 : cast<StoreSDNode>(Op)->getAddressingMode();
845 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
846 ? ARM_AM::add : ARM_AM::sub;
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000847 int Val;
848 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
Owen Anderson939cd212011-08-31 20:00:11 +0000849 if (AddSub == ARM_AM::sub) Val *= -1;
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000850 Offset = CurDAG->getRegister(0, MVT::i32);
851 Opc = CurDAG->getTargetConstant(Val, MVT::i32);
852 return true;
853 }
854
855 return false;
856}
857
858
Owen Anderson2aedba62011-07-26 20:54:26 +0000859bool ARMDAGToDAGISel::SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
860 SDValue &Offset, SDValue &Opc) {
861 unsigned Opcode = Op->getOpcode();
862 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
863 ? cast<LoadSDNode>(Op)->getAddressingMode()
864 : cast<StoreSDNode>(Op)->getAddressingMode();
865 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
866 ? ARM_AM::add : ARM_AM::sub;
867 int Val;
868 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
869 Offset = CurDAG->getRegister(0, MVT::i32);
870 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
871 ARM_AM::no_shift),
872 MVT::i32);
873 return true;
874 }
875
876 return false;
877}
878
Jim Grosbachf0c95ca2011-08-05 20:35:44 +0000879bool ARMDAGToDAGISel::SelectAddrOffsetNone(SDValue N, SDValue &Base) {
880 Base = N;
881 return true;
882}
Evan Cheng10043e22007-01-19 07:51:42 +0000883
Chris Lattner0e023ea2010-09-21 20:31:19 +0000884bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000885 SDValue &Base, SDValue &Offset,
886 SDValue &Opc) {
Evan Cheng10043e22007-01-19 07:51:42 +0000887 if (N.getOpcode() == ISD::SUB) {
888 // X - C is canonicalize to X + -C, no need to handle it here.
889 Base = N.getOperand(0);
890 Offset = N.getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +0000891 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000892 return true;
893 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000894
Chris Lattner46c01a32011-02-13 22:25:43 +0000895 if (!CurDAG->isBaseWithConstantOffset(N)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000896 Base = N;
897 if (N.getOpcode() == ISD::FrameIndex) {
898 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000899 Base = CurDAG->getTargetFrameIndex(FI,
900 getTargetLowering()->getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +0000901 }
Owen Anderson9f944592009-08-11 20:47:22 +0000902 Offset = CurDAG->getRegister(0, MVT::i32);
903 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000904 return true;
905 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000906
Evan Cheng10043e22007-01-19 07:51:42 +0000907 // If the RHS is +/- imm8, fold into addr mode.
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000908 int RHSC;
909 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
910 -256 + 1, 256, RHSC)) { // 8 bits.
911 Base = N.getOperand(0);
912 if (Base.getOpcode() == ISD::FrameIndex) {
913 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000914 Base = CurDAG->getTargetFrameIndex(FI,
915 getTargetLowering()->getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +0000916 }
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000917 Offset = CurDAG->getRegister(0, MVT::i32);
918
919 ARM_AM::AddrOpc AddSub = ARM_AM::add;
920 if (RHSC < 0) {
921 AddSub = ARM_AM::sub;
Chris Lattner46c01a32011-02-13 22:25:43 +0000922 RHSC = -RHSC;
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000923 }
924 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
925 return true;
Evan Cheng10043e22007-01-19 07:51:42 +0000926 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000927
Evan Cheng10043e22007-01-19 07:51:42 +0000928 Base = N.getOperand(0);
929 Offset = N.getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +0000930 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000931 return true;
932}
933
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000934bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000935 SDValue &Offset, SDValue &Opc) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000936 unsigned Opcode = Op->getOpcode();
Evan Cheng10043e22007-01-19 07:51:42 +0000937 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
938 ? cast<LoadSDNode>(Op)->getAddressingMode()
939 : cast<StoreSDNode>(Op)->getAddressingMode();
940 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
941 ? ARM_AM::add : ARM_AM::sub;
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000942 int Val;
943 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 256, Val)) { // 12 bits.
944 Offset = CurDAG->getRegister(0, MVT::i32);
945 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
946 return true;
Evan Cheng10043e22007-01-19 07:51:42 +0000947 }
948
949 Offset = N;
Owen Anderson9f944592009-08-11 20:47:22 +0000950 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000951 return true;
952}
953
Jim Grosbachd37f0712010-10-21 19:38:40 +0000954bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000955 SDValue &Base, SDValue &Offset) {
Chris Lattner46c01a32011-02-13 22:25:43 +0000956 if (!CurDAG->isBaseWithConstantOffset(N)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000957 Base = N;
958 if (N.getOpcode() == ISD::FrameIndex) {
959 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000960 Base = CurDAG->getTargetFrameIndex(FI,
961 getTargetLowering()->getPointerTy());
Anton Korobeynikov25229082009-11-24 00:44:37 +0000962 } else if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northover72360d22013-12-02 10:35:41 +0000963 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress) {
Evan Cheng10043e22007-01-19 07:51:42 +0000964 Base = N.getOperand(0);
965 }
966 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson9f944592009-08-11 20:47:22 +0000967 MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000968 return true;
969 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000970
Evan Cheng10043e22007-01-19 07:51:42 +0000971 // If the RHS is +/- imm8, fold into addr mode.
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000972 int RHSC;
973 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4,
974 -256 + 1, 256, RHSC)) {
975 Base = N.getOperand(0);
976 if (Base.getOpcode() == ISD::FrameIndex) {
977 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000978 Base = CurDAG->getTargetFrameIndex(FI,
979 getTargetLowering()->getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +0000980 }
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000981
982 ARM_AM::AddrOpc AddSub = ARM_AM::add;
983 if (RHSC < 0) {
984 AddSub = ARM_AM::sub;
Chris Lattner46c01a32011-02-13 22:25:43 +0000985 RHSC = -RHSC;
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000986 }
987 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
988 MVT::i32);
989 return true;
Evan Cheng10043e22007-01-19 07:51:42 +0000990 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000991
Evan Cheng10043e22007-01-19 07:51:42 +0000992 Base = N;
993 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson9f944592009-08-11 20:47:22 +0000994 MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000995 return true;
996}
997
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000998bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,
999 SDValue &Align) {
Bob Wilsondeb35af2009-07-01 23:16:05 +00001000 Addr = N;
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001001
1002 unsigned Alignment = 0;
1003 if (LSBaseSDNode *LSN = dyn_cast<LSBaseSDNode>(Parent)) {
1004 // This case occurs only for VLD1-lane/dup and VST1-lane instructions.
1005 // The maximum alignment is equal to the memory size being referenced.
1006 unsigned LSNAlign = LSN->getAlignment();
1007 unsigned MemSize = LSN->getMemoryVT().getSizeInBits() / 8;
Jakob Stoklund Olesene5a6adc2011-10-27 22:39:16 +00001008 if (LSNAlign >= MemSize && MemSize > 1)
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001009 Alignment = MemSize;
1010 } else {
1011 // All other uses of addrmode6 are for intrinsics. For now just record
1012 // the raw alignment value; it will be refined later based on the legal
1013 // alignment operands for the intrinsic.
1014 Alignment = cast<MemIntrinsicSDNode>(Parent)->getAlignment();
1015 }
1016
1017 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
Bob Wilsondeb35af2009-07-01 23:16:05 +00001018 return true;
1019}
1020
Bob Wilsone3ecd5f2011-02-25 06:42:42 +00001021bool ARMDAGToDAGISel::SelectAddrMode6Offset(SDNode *Op, SDValue N,
1022 SDValue &Offset) {
1023 LSBaseSDNode *LdSt = cast<LSBaseSDNode>(Op);
1024 ISD::MemIndexedMode AM = LdSt->getAddressingMode();
1025 if (AM != ISD::POST_INC)
1026 return false;
1027 Offset = N;
1028 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N)) {
1029 if (NC->getZExtValue() * 8 == LdSt->getMemoryVT().getSizeInBits())
1030 Offset = CurDAG->getRegister(0, MVT::i32);
1031 }
1032 return true;
1033}
1034
Chris Lattner0e023ea2010-09-21 20:31:19 +00001035bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
Evan Cheng9a58aff2009-08-14 19:01:37 +00001036 SDValue &Offset, SDValue &Label) {
Evan Cheng10043e22007-01-19 07:51:42 +00001037 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
1038 Offset = N.getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001039 SDValue N1 = N.getOperand(1);
Evan Chengb8b0ad82011-01-20 08:34:58 +00001040 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
1041 MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00001042 return true;
1043 }
Bill Wendling092a7bd2010-12-14 03:36:38 +00001044
Evan Cheng10043e22007-01-19 07:51:42 +00001045 return false;
1046}
1047
Bill Wendling092a7bd2010-12-14 03:36:38 +00001048
1049//===----------------------------------------------------------------------===//
1050// Thumb Addressing Modes
1051//===----------------------------------------------------------------------===//
1052
Chris Lattner0e023ea2010-09-21 20:31:19 +00001053bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001054 SDValue &Base, SDValue &Offset){
Chris Lattner46c01a32011-02-13 22:25:43 +00001055 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N)) {
Evan Cheng0794c6a2009-07-11 07:08:13 +00001056 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
Dan Gohmanf1d83042010-06-18 14:22:04 +00001057 if (!NC || !NC->isNullValue())
Evan Cheng0794c6a2009-07-11 07:08:13 +00001058 return false;
1059
1060 Base = Offset = N;
Evan Chengc0b73662007-01-23 22:59:13 +00001061 return true;
1062 }
1063
Evan Cheng10043e22007-01-19 07:51:42 +00001064 Base = N.getOperand(0);
1065 Offset = N.getOperand(1);
1066 return true;
1067}
1068
Evan Cheng139edae2007-01-24 02:21:22 +00001069bool
Bill Wendling092a7bd2010-12-14 03:36:38 +00001070ARMDAGToDAGISel::SelectThumbAddrModeRI(SDValue N, SDValue &Base,
1071 SDValue &Offset, unsigned Scale) {
Evan Cheng139edae2007-01-24 02:21:22 +00001072 if (Scale == 4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001073 SDValue TmpBase, TmpOffImm;
Chris Lattner0e023ea2010-09-21 20:31:19 +00001074 if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
Evan Cheng139edae2007-01-24 02:21:22 +00001075 return false; // We want to select tLDRspi / tSTRspi instead.
Bill Wendling092a7bd2010-12-14 03:36:38 +00001076
Evan Cheng1526ba52007-01-24 08:53:17 +00001077 if (N.getOpcode() == ARMISD::Wrapper &&
1078 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
1079 return false; // We want to select tLDRpci instead.
Evan Cheng139edae2007-01-24 02:21:22 +00001080 }
1081
Chris Lattner46c01a32011-02-13 22:25:43 +00001082 if (!CurDAG->isBaseWithConstantOffset(N))
Bill Wendling832a5da2010-12-15 01:03:19 +00001083 return false;
Evan Cheng10043e22007-01-19 07:51:42 +00001084
Evan Cheng650d0672007-02-06 00:22:06 +00001085 // Thumb does not have [sp, r] address mode.
1086 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
1087 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
1088 if ((LHSR && LHSR->getReg() == ARM::SP) ||
Bill Wendling832a5da2010-12-15 01:03:19 +00001089 (RHSR && RHSR->getReg() == ARM::SP))
1090 return false;
Bill Wendling092a7bd2010-12-14 03:36:38 +00001091
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001092 // FIXME: Why do we explicitly check for a match here and then return false?
1093 // Presumably to allow something else to match, but shouldn't this be
1094 // documented?
1095 int RHSC;
1096 if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC))
1097 return false;
Bill Wendling092a7bd2010-12-14 03:36:38 +00001098
1099 Base = N.getOperand(0);
1100 Offset = N.getOperand(1);
1101 return true;
1102}
1103
1104bool
1105ARMDAGToDAGISel::SelectThumbAddrModeRI5S1(SDValue N,
1106 SDValue &Base,
1107 SDValue &Offset) {
1108 return SelectThumbAddrModeRI(N, Base, Offset, 1);
1109}
1110
1111bool
1112ARMDAGToDAGISel::SelectThumbAddrModeRI5S2(SDValue N,
1113 SDValue &Base,
1114 SDValue &Offset) {
1115 return SelectThumbAddrModeRI(N, Base, Offset, 2);
1116}
1117
1118bool
1119ARMDAGToDAGISel::SelectThumbAddrModeRI5S4(SDValue N,
1120 SDValue &Base,
1121 SDValue &Offset) {
1122 return SelectThumbAddrModeRI(N, Base, Offset, 4);
1123}
1124
1125bool
1126ARMDAGToDAGISel::SelectThumbAddrModeImm5S(SDValue N, unsigned Scale,
1127 SDValue &Base, SDValue &OffImm) {
1128 if (Scale == 4) {
1129 SDValue TmpBase, TmpOffImm;
1130 if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
1131 return false; // We want to select tLDRspi / tSTRspi instead.
1132
1133 if (N.getOpcode() == ARMISD::Wrapper &&
1134 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
1135 return false; // We want to select tLDRpci instead.
1136 }
1137
Chris Lattner46c01a32011-02-13 22:25:43 +00001138 if (!CurDAG->isBaseWithConstantOffset(N)) {
Bill Wendling092a7bd2010-12-14 03:36:38 +00001139 if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northover72360d22013-12-02 10:35:41 +00001140 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress) {
Bill Wendling092a7bd2010-12-14 03:36:38 +00001141 Base = N.getOperand(0);
1142 } else {
1143 Base = N;
1144 }
1145
Owen Anderson9f944592009-08-11 20:47:22 +00001146 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng650d0672007-02-06 00:22:06 +00001147 return true;
1148 }
1149
Bill Wendling832a5da2010-12-15 01:03:19 +00001150 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
1151 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
1152 if ((LHSR && LHSR->getReg() == ARM::SP) ||
1153 (RHSR && RHSR->getReg() == ARM::SP)) {
1154 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(N.getOperand(0));
1155 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
1156 unsigned LHSC = LHS ? LHS->getZExtValue() : 0;
1157 unsigned RHSC = RHS ? RHS->getZExtValue() : 0;
1158
1159 // Thumb does not have [sp, #imm5] address mode for non-zero imm5.
1160 if (LHSC != 0 || RHSC != 0) return false;
1161
1162 Base = N;
1163 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1164 return true;
1165 }
1166
Evan Cheng10043e22007-01-19 07:51:42 +00001167 // If the RHS is + imm5 * scale, fold into addr mode.
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001168 int RHSC;
1169 if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC)) {
1170 Base = N.getOperand(0);
1171 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1172 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00001173 }
1174
Evan Chengc0b73662007-01-23 22:59:13 +00001175 Base = N.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001176 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengc0b73662007-01-23 22:59:13 +00001177 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00001178}
1179
Bill Wendling092a7bd2010-12-14 03:36:38 +00001180bool
1181ARMDAGToDAGISel::SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
1182 SDValue &OffImm) {
1183 return SelectThumbAddrModeImm5S(N, 4, Base, OffImm);
Evan Cheng10043e22007-01-19 07:51:42 +00001184}
1185
Bill Wendling092a7bd2010-12-14 03:36:38 +00001186bool
1187ARMDAGToDAGISel::SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
1188 SDValue &OffImm) {
1189 return SelectThumbAddrModeImm5S(N, 2, Base, OffImm);
Evan Cheng10043e22007-01-19 07:51:42 +00001190}
1191
Bill Wendling092a7bd2010-12-14 03:36:38 +00001192bool
1193ARMDAGToDAGISel::SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
1194 SDValue &OffImm) {
1195 return SelectThumbAddrModeImm5S(N, 1, Base, OffImm);
Evan Cheng10043e22007-01-19 07:51:42 +00001196}
1197
Chris Lattner0e023ea2010-09-21 20:31:19 +00001198bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
1199 SDValue &Base, SDValue &OffImm) {
Evan Cheng10043e22007-01-19 07:51:42 +00001200 if (N.getOpcode() == ISD::FrameIndex) {
1201 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001202 Base = CurDAG->getTargetFrameIndex(FI,
1203 getTargetLowering()->getPointerTy());
Owen Anderson9f944592009-08-11 20:47:22 +00001204 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00001205 return true;
1206 }
Evan Cheng139edae2007-01-24 02:21:22 +00001207
Chris Lattner46c01a32011-02-13 22:25:43 +00001208 if (!CurDAG->isBaseWithConstantOffset(N))
Evan Cheng650d0672007-02-06 00:22:06 +00001209 return false;
1210
1211 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
Evan Chenga9740312007-02-06 09:11:20 +00001212 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
1213 (LHSR && LHSR->getReg() == ARM::SP)) {
Evan Cheng139edae2007-01-24 02:21:22 +00001214 // If the RHS is + imm8 * scale, fold into addr mode.
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001215 int RHSC;
1216 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4, 0, 256, RHSC)) {
1217 Base = N.getOperand(0);
1218 if (Base.getOpcode() == ISD::FrameIndex) {
1219 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001220 Base = CurDAG->getTargetFrameIndex(FI,
1221 getTargetLowering()->getPointerTy());
Evan Cheng139edae2007-01-24 02:21:22 +00001222 }
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001223 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1224 return true;
Evan Cheng139edae2007-01-24 02:21:22 +00001225 }
1226 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001227
Evan Cheng10043e22007-01-19 07:51:42 +00001228 return false;
1229}
1230
Bill Wendling092a7bd2010-12-14 03:36:38 +00001231
1232//===----------------------------------------------------------------------===//
1233// Thumb 2 Addressing Modes
1234//===----------------------------------------------------------------------===//
1235
1236
Chris Lattner0e023ea2010-09-21 20:31:19 +00001237bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDValue N, SDValue &BaseReg,
Evan Chengeab9ca72009-06-27 02:26:13 +00001238 SDValue &Opc) {
Evan Cheng59069ec2010-07-30 23:33:54 +00001239 if (DisableShifterOp)
1240 return false;
1241
Evan Chenga20cde32011-07-20 23:34:39 +00001242 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
Evan Chengeab9ca72009-06-27 02:26:13 +00001243
1244 // Don't match base register only case. That is matched to a separate
1245 // lower complexity pattern with explicit register operand.
1246 if (ShOpcVal == ARM_AM::no_shift) return false;
1247
1248 BaseReg = N.getOperand(0);
1249 unsigned ShImmVal = 0;
1250 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1251 ShImmVal = RHS->getZExtValue() & 31;
1252 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
1253 return true;
1254 }
1255
1256 return false;
1257}
1258
Chris Lattner0e023ea2010-09-21 20:31:19 +00001259bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
Evan Chengb23b50d2009-06-29 07:51:04 +00001260 SDValue &Base, SDValue &OffImm) {
1261 // Match simple R + imm12 operands.
David Goodwin802a0b52009-07-20 15:55:39 +00001262
Evan Cheng36064672009-08-11 08:52:18 +00001263 // Base only.
Chris Lattner46c01a32011-02-13 22:25:43 +00001264 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1265 !CurDAG->isBaseWithConstantOffset(N)) {
David Goodwin802a0b52009-07-20 15:55:39 +00001266 if (N.getOpcode() == ISD::FrameIndex) {
Chris Lattner46c01a32011-02-13 22:25:43 +00001267 // Match frame index.
David Goodwin802a0b52009-07-20 15:55:39 +00001268 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001269 Base = CurDAG->getTargetFrameIndex(FI,
1270 getTargetLowering()->getPointerTy());
Owen Anderson9f944592009-08-11 20:47:22 +00001271 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
David Goodwin802a0b52009-07-20 15:55:39 +00001272 return true;
Chris Lattner46c01a32011-02-13 22:25:43 +00001273 }
Owen Anderson6d557452011-03-18 19:46:58 +00001274
Chris Lattner46c01a32011-02-13 22:25:43 +00001275 if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northover72360d22013-12-02 10:35:41 +00001276 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress) {
Evan Cheng36064672009-08-11 08:52:18 +00001277 Base = N.getOperand(0);
1278 if (Base.getOpcode() == ISD::TargetConstantPool)
1279 return false; // We want to select t2LDRpci instead.
1280 } else
1281 Base = N;
Owen Anderson9f944592009-08-11 20:47:22 +00001282 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng36064672009-08-11 08:52:18 +00001283 return true;
David Goodwin802a0b52009-07-20 15:55:39 +00001284 }
Evan Chengb23b50d2009-06-29 07:51:04 +00001285
1286 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner0e023ea2010-09-21 20:31:19 +00001287 if (SelectT2AddrModeImm8(N, Base, OffImm))
Evan Cheng36064672009-08-11 08:52:18 +00001288 // Let t2LDRi8 handle (R - imm8).
1289 return false;
1290
Evan Chengb23b50d2009-06-29 07:51:04 +00001291 int RHSC = (int)RHS->getZExtValue();
David Goodwin79c079b2009-07-30 18:56:48 +00001292 if (N.getOpcode() == ISD::SUB)
1293 RHSC = -RHSC;
1294
1295 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
Evan Chengb23b50d2009-06-29 07:51:04 +00001296 Base = N.getOperand(0);
David Goodwin79c079b2009-07-30 18:56:48 +00001297 if (Base.getOpcode() == ISD::FrameIndex) {
1298 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001299 Base = CurDAG->getTargetFrameIndex(FI,
1300 getTargetLowering()->getPointerTy());
David Goodwin79c079b2009-07-30 18:56:48 +00001301 }
Owen Anderson9f944592009-08-11 20:47:22 +00001302 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Chengb23b50d2009-06-29 07:51:04 +00001303 return true;
1304 }
1305 }
1306
Evan Cheng36064672009-08-11 08:52:18 +00001307 // Base only.
1308 Base = N;
Owen Anderson9f944592009-08-11 20:47:22 +00001309 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng36064672009-08-11 08:52:18 +00001310 return true;
Evan Chengb23b50d2009-06-29 07:51:04 +00001311}
1312
Chris Lattner0e023ea2010-09-21 20:31:19 +00001313bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
Evan Chengb23b50d2009-06-29 07:51:04 +00001314 SDValue &Base, SDValue &OffImm) {
David Goodwin79c079b2009-07-30 18:56:48 +00001315 // Match simple R - imm8 operands.
Chris Lattner46c01a32011-02-13 22:25:43 +00001316 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1317 !CurDAG->isBaseWithConstantOffset(N))
1318 return false;
Owen Anderson6d557452011-03-18 19:46:58 +00001319
Chris Lattner46c01a32011-02-13 22:25:43 +00001320 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1321 int RHSC = (int)RHS->getSExtValue();
1322 if (N.getOpcode() == ISD::SUB)
1323 RHSC = -RHSC;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001324
Chris Lattner46c01a32011-02-13 22:25:43 +00001325 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
1326 Base = N.getOperand(0);
1327 if (Base.getOpcode() == ISD::FrameIndex) {
1328 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001329 Base = CurDAG->getTargetFrameIndex(FI,
1330 getTargetLowering()->getPointerTy());
Evan Chengb23b50d2009-06-29 07:51:04 +00001331 }
Chris Lattner46c01a32011-02-13 22:25:43 +00001332 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1333 return true;
Evan Chengb23b50d2009-06-29 07:51:04 +00001334 }
1335 }
1336
1337 return false;
1338}
1339
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001340bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Cheng84c6cda2009-07-02 07:28:31 +00001341 SDValue &OffImm){
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001342 unsigned Opcode = Op->getOpcode();
Evan Cheng84c6cda2009-07-02 07:28:31 +00001343 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
1344 ? cast<LoadSDNode>(Op)->getAddressingMode()
1345 : cast<StoreSDNode>(Op)->getAddressingMode();
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001346 int RHSC;
1347 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x100, RHSC)) { // 8 bits.
1348 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
1349 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
1350 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
1351 return true;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001352 }
1353
1354 return false;
1355}
1356
Chris Lattner0e023ea2010-09-21 20:31:19 +00001357bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
Evan Chengb23b50d2009-06-29 07:51:04 +00001358 SDValue &Base,
1359 SDValue &OffReg, SDValue &ShImm) {
Evan Cheng36064672009-08-11 08:52:18 +00001360 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
Chris Lattner46c01a32011-02-13 22:25:43 +00001361 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N))
Evan Cheng36064672009-08-11 08:52:18 +00001362 return false;
Evan Chengb23b50d2009-06-29 07:51:04 +00001363
Evan Cheng36064672009-08-11 08:52:18 +00001364 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
1365 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1366 int RHSC = (int)RHS->getZExtValue();
1367 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
1368 return false;
1369 else if (RHSC < 0 && RHSC >= -255) // 8 bits
David Goodwin79c079b2009-07-30 18:56:48 +00001370 return false;
1371 }
1372
Evan Chengb23b50d2009-06-29 07:51:04 +00001373 // Look for (R + R) or (R + (R << [1,2,3])).
1374 unsigned ShAmt = 0;
1375 Base = N.getOperand(0);
1376 OffReg = N.getOperand(1);
1377
1378 // Swap if it is ((R << c) + R).
Evan Chenga20cde32011-07-20 23:34:39 +00001379 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg.getOpcode());
Evan Chengb23b50d2009-06-29 07:51:04 +00001380 if (ShOpcVal != ARM_AM::lsl) {
Evan Chenga20cde32011-07-20 23:34:39 +00001381 ShOpcVal = ARM_AM::getShiftOpcForNode(Base.getOpcode());
Evan Chengb23b50d2009-06-29 07:51:04 +00001382 if (ShOpcVal == ARM_AM::lsl)
1383 std::swap(Base, OffReg);
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001384 }
1385
Evan Chengb23b50d2009-06-29 07:51:04 +00001386 if (ShOpcVal == ARM_AM::lsl) {
1387 // Check to see if the RHS of the shift is a constant, if not, we can't fold
1388 // it.
1389 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
1390 ShAmt = Sh->getZExtValue();
Evan Cheng59bbc542010-10-27 23:41:30 +00001391 if (ShAmt < 4 && isShifterOpProfitable(OffReg, ShOpcVal, ShAmt))
1392 OffReg = OffReg.getOperand(0);
1393 else {
Evan Chengb23b50d2009-06-29 07:51:04 +00001394 ShAmt = 0;
1395 ShOpcVal = ARM_AM::no_shift;
Evan Cheng59bbc542010-10-27 23:41:30 +00001396 }
Evan Chengb23b50d2009-06-29 07:51:04 +00001397 } else {
1398 ShOpcVal = ARM_AM::no_shift;
1399 }
David Goodwinf3912052009-07-15 15:50:19 +00001400 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001401
Owen Anderson9f944592009-08-11 20:47:22 +00001402 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
Evan Chengb23b50d2009-06-29 07:51:04 +00001403
1404 return true;
1405}
1406
Tim Northovera7ecd242013-07-16 09:46:55 +00001407bool ARMDAGToDAGISel::SelectT2AddrModeExclusive(SDValue N, SDValue &Base,
1408 SDValue &OffImm) {
Alp Tokercb402912014-01-24 17:20:08 +00001409 // This *must* succeed since it's used for the irreplaceable ldrex and strex
Tim Northovera7ecd242013-07-16 09:46:55 +00001410 // instructions.
1411 Base = N;
1412 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1413
1414 if (N.getOpcode() != ISD::ADD || !CurDAG->isBaseWithConstantOffset(N))
1415 return true;
1416
1417 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
1418 if (!RHS)
1419 return true;
1420
1421 uint32_t RHSC = (int)RHS->getZExtValue();
1422 if (RHSC > 1020 || RHSC % 4 != 0)
1423 return true;
1424
1425 Base = N.getOperand(0);
1426 if (Base.getOpcode() == ISD::FrameIndex) {
1427 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1428 Base = CurDAG->getTargetFrameIndex(FI, getTargetLowering()->getPointerTy());
1429 }
1430
1431 OffImm = CurDAG->getTargetConstant(RHSC / 4, MVT::i32);
1432 return true;
1433}
1434
Evan Chengb23b50d2009-06-29 07:51:04 +00001435//===--------------------------------------------------------------------===//
1436
Evan Cheng7e90b112007-07-05 07:15:27 +00001437/// getAL - Returns a ARMCC::AL immediate node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001438static inline SDValue getAL(SelectionDAG *CurDAG) {
Owen Anderson9f944592009-08-11 20:47:22 +00001439 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
Evan Cheng0f7cbe82007-05-15 01:29:07 +00001440}
1441
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001442SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
1443 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengd9c55362009-07-02 01:23:32 +00001444 ISD::MemIndexedMode AM = LD->getAddressingMode();
1445 if (AM == ISD::UNINDEXED)
Craig Topper062a2ba2014-04-25 05:30:21 +00001446 return nullptr;
Evan Chengd9c55362009-07-02 01:23:32 +00001447
Owen Anderson53aa7a92009-08-10 22:56:29 +00001448 EVT LoadedVT = LD->getMemoryVT();
Evan Chengd9c55362009-07-02 01:23:32 +00001449 SDValue Offset, AMOpc;
1450 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1451 unsigned Opcode = 0;
1452 bool Match = false;
Owen Anderson4d5c8f82011-08-29 20:16:50 +00001453 if (LoadedVT == MVT::i32 && isPre &&
1454 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
1455 Opcode = ARM::LDR_PRE_IMM;
1456 Match = true;
1457 } else if (LoadedVT == MVT::i32 && !isPre &&
Owen Anderson2aedba62011-07-26 20:54:26 +00001458 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
Owen Anderson4d5c8f82011-08-29 20:16:50 +00001459 Opcode = ARM::LDR_POST_IMM;
Evan Chengd9c55362009-07-02 01:23:32 +00001460 Match = true;
Owen Anderson2aedba62011-07-26 20:54:26 +00001461 } else if (LoadedVT == MVT::i32 &&
1462 SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
Owen Anderson16d33f32011-08-26 20:43:14 +00001463 Opcode = isPre ? ARM::LDR_PRE_REG : ARM::LDR_POST_REG;
Owen Anderson2aedba62011-07-26 20:54:26 +00001464 Match = true;
1465
Owen Anderson9f944592009-08-11 20:47:22 +00001466 } else if (LoadedVT == MVT::i16 &&
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001467 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengd9c55362009-07-02 01:23:32 +00001468 Match = true;
1469 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
1470 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
1471 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
Owen Anderson9f944592009-08-11 20:47:22 +00001472 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
Evan Chengd9c55362009-07-02 01:23:32 +00001473 if (LD->getExtensionType() == ISD::SEXTLOAD) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001474 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengd9c55362009-07-02 01:23:32 +00001475 Match = true;
1476 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
1477 }
1478 } else {
Owen Anderson4d5c8f82011-08-29 20:16:50 +00001479 if (isPre &&
1480 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengd9c55362009-07-02 01:23:32 +00001481 Match = true;
Owen Anderson4d5c8f82011-08-29 20:16:50 +00001482 Opcode = ARM::LDRB_PRE_IMM;
1483 } else if (!isPre &&
1484 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
1485 Match = true;
1486 Opcode = ARM::LDRB_POST_IMM;
Owen Anderson2aedba62011-07-26 20:54:26 +00001487 } else if (SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
1488 Match = true;
Owen Anderson16d33f32011-08-26 20:43:14 +00001489 Opcode = isPre ? ARM::LDRB_PRE_REG : ARM::LDRB_POST_REG;
Evan Chengd9c55362009-07-02 01:23:32 +00001490 }
1491 }
1492 }
1493
1494 if (Match) {
Owen Andersonfd60f602011-08-26 21:12:37 +00001495 if (Opcode == ARM::LDR_PRE_IMM || Opcode == ARM::LDRB_PRE_IMM) {
1496 SDValue Chain = LD->getChain();
1497 SDValue Base = LD->getBasePtr();
1498 SDValue Ops[]= { Base, AMOpc, getAL(CurDAG),
1499 CurDAG->getRegister(0, MVT::i32), Chain };
Andrew Trickef9de2a2013-05-25 02:42:55 +00001500 return CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32,
Michael Liaob53d8962013-04-19 22:22:57 +00001501 MVT::i32, MVT::Other, Ops);
Owen Andersonfd60f602011-08-26 21:12:37 +00001502 } else {
1503 SDValue Chain = LD->getChain();
1504 SDValue Base = LD->getBasePtr();
1505 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
1506 CurDAG->getRegister(0, MVT::i32), Chain };
Andrew Trickef9de2a2013-05-25 02:42:55 +00001507 return CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32,
Michael Liaob53d8962013-04-19 22:22:57 +00001508 MVT::i32, MVT::Other, Ops);
Owen Andersonfd60f602011-08-26 21:12:37 +00001509 }
Evan Chengd9c55362009-07-02 01:23:32 +00001510 }
1511
Craig Topper062a2ba2014-04-25 05:30:21 +00001512 return nullptr;
Evan Chengd9c55362009-07-02 01:23:32 +00001513}
1514
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001515SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
1516 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Cheng84c6cda2009-07-02 07:28:31 +00001517 ISD::MemIndexedMode AM = LD->getAddressingMode();
1518 if (AM == ISD::UNINDEXED)
Craig Topper062a2ba2014-04-25 05:30:21 +00001519 return nullptr;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001520
Owen Anderson53aa7a92009-08-10 22:56:29 +00001521 EVT LoadedVT = LD->getMemoryVT();
Evan Cheng8ecd7eb2009-07-02 23:16:11 +00001522 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001523 SDValue Offset;
1524 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1525 unsigned Opcode = 0;
1526 bool Match = false;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001527 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001528 switch (LoadedVT.getSimpleVT().SimpleTy) {
1529 case MVT::i32:
Evan Cheng84c6cda2009-07-02 07:28:31 +00001530 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
1531 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001532 case MVT::i16:
Evan Cheng8ecd7eb2009-07-02 23:16:11 +00001533 if (isSExtLd)
1534 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
1535 else
1536 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001537 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001538 case MVT::i8:
1539 case MVT::i1:
Evan Cheng8ecd7eb2009-07-02 23:16:11 +00001540 if (isSExtLd)
1541 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
1542 else
1543 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001544 break;
1545 default:
Craig Topper062a2ba2014-04-25 05:30:21 +00001546 return nullptr;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001547 }
1548 Match = true;
1549 }
1550
1551 if (Match) {
1552 SDValue Chain = LD->getChain();
1553 SDValue Base = LD->getBasePtr();
1554 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
Owen Anderson9f944592009-08-11 20:47:22 +00001555 CurDAG->getRegister(0, MVT::i32), Chain };
Andrew Trickef9de2a2013-05-25 02:42:55 +00001556 return CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
Michael Liaob53d8962013-04-19 22:22:57 +00001557 MVT::Other, Ops);
Evan Cheng84c6cda2009-07-02 07:28:31 +00001558 }
1559
Craig Topper062a2ba2014-04-25 05:30:21 +00001560 return nullptr;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001561}
1562
Weiming Zhao8f56f882012-11-16 21:55:34 +00001563/// \brief Form a GPRPair pseudo register from a pair of GPR regs.
1564SDNode *ARMDAGToDAGISel::createGPRPairNode(EVT VT, SDValue V0, SDValue V1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001565 SDLoc dl(V0.getNode());
Weiming Zhao8f56f882012-11-16 21:55:34 +00001566 SDValue RegClass =
1567 CurDAG->getTargetConstant(ARM::GPRPairRegClassID, MVT::i32);
1568 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::gsub_0, MVT::i32);
1569 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::gsub_1, MVT::i32);
1570 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
Michael Liaob53d8962013-04-19 22:22:57 +00001571 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Weiming Zhao8f56f882012-11-16 21:55:34 +00001572}
1573
Weiming Zhao95782222012-11-17 00:23:35 +00001574/// \brief Form a D register from a pair of S registers.
1575SDNode *ARMDAGToDAGISel::createSRegPairNode(EVT VT, SDValue V0, SDValue V1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001576 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001577 SDValue RegClass =
1578 CurDAG->getTargetConstant(ARM::DPR_VFP2RegClassID, MVT::i32);
Bob Wilsond8a9a042010-06-04 00:04:02 +00001579 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
1580 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001581 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
Michael Liaob53d8962013-04-19 22:22:57 +00001582 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Bob Wilsond8a9a042010-06-04 00:04:02 +00001583}
1584
Weiming Zhao95782222012-11-17 00:23:35 +00001585/// \brief Form a quad register from a pair of D registers.
1586SDNode *ARMDAGToDAGISel::createDRegPairNode(EVT VT, SDValue V0, SDValue V1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001587 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001588 SDValue RegClass = CurDAG->getTargetConstant(ARM::QPRRegClassID, MVT::i32);
Jakob Stoklund Olesen6c47d642010-05-24 16:54:32 +00001589 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1590 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001591 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
Michael Liaob53d8962013-04-19 22:22:57 +00001592 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Bob Wilsone6b778d2009-10-06 22:01:59 +00001593}
1594
Weiming Zhao95782222012-11-17 00:23:35 +00001595/// \brief Form 4 consecutive D registers from a pair of Q registers.
1596SDNode *ARMDAGToDAGISel::createQRegPairNode(EVT VT, SDValue V0, SDValue V1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001597 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001598 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, MVT::i32);
Jakob Stoklund Olesen6c47d642010-05-24 16:54:32 +00001599 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1600 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001601 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
Michael Liaob53d8962013-04-19 22:22:57 +00001602 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Evan Chengc2ae5f52010-05-10 17:34:18 +00001603}
1604
Weiming Zhao95782222012-11-17 00:23:35 +00001605/// \brief Form 4 consecutive S registers.
1606SDNode *ARMDAGToDAGISel::createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1,
Bob Wilsond8a9a042010-06-04 00:04:02 +00001607 SDValue V2, SDValue V3) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001608 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001609 SDValue RegClass =
1610 CurDAG->getTargetConstant(ARM::QPR_VFP2RegClassID, MVT::i32);
Bob Wilsond8a9a042010-06-04 00:04:02 +00001611 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
1612 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
1613 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, MVT::i32);
1614 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001615 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1616 V2, SubReg2, V3, SubReg3 };
Michael Liaob53d8962013-04-19 22:22:57 +00001617 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Bob Wilsond8a9a042010-06-04 00:04:02 +00001618}
1619
Weiming Zhao95782222012-11-17 00:23:35 +00001620/// \brief Form 4 consecutive D registers.
1621SDNode *ARMDAGToDAGISel::createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1,
Evan Chengc2ae5f52010-05-10 17:34:18 +00001622 SDValue V2, SDValue V3) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001623 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001624 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, MVT::i32);
Jakob Stoklund Olesen6c47d642010-05-24 16:54:32 +00001625 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1626 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
1627 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
1628 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001629 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1630 V2, SubReg2, V3, SubReg3 };
Michael Liaob53d8962013-04-19 22:22:57 +00001631 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Evan Chengc2ae5f52010-05-10 17:34:18 +00001632}
1633
Weiming Zhao95782222012-11-17 00:23:35 +00001634/// \brief Form 4 consecutive Q registers.
1635SDNode *ARMDAGToDAGISel::createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1,
Evan Cheng298e6b82010-05-16 03:27:48 +00001636 SDValue V2, SDValue V3) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001637 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001638 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQQQPRRegClassID, MVT::i32);
Jakob Stoklund Olesen6c47d642010-05-24 16:54:32 +00001639 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1640 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
1641 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, MVT::i32);
1642 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001643 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1644 V2, SubReg2, V3, SubReg3 };
Michael Liaob53d8962013-04-19 22:22:57 +00001645 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Evan Cheng298e6b82010-05-16 03:27:48 +00001646}
1647
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001648/// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1649/// of a NEON VLD or VST instruction. The supported values depend on the
1650/// number of registers being loaded.
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001651SDValue ARMDAGToDAGISel::GetVLDSTAlign(SDValue Align, unsigned NumVecs,
1652 bool is64BitVector) {
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001653 unsigned NumRegs = NumVecs;
1654 if (!is64BitVector && NumVecs < 3)
1655 NumRegs *= 2;
1656
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001657 unsigned Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001658 if (Alignment >= 32 && NumRegs == 4)
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001659 Alignment = 32;
1660 else if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1661 Alignment = 16;
1662 else if (Alignment >= 8)
1663 Alignment = 8;
1664 else
1665 Alignment = 0;
1666
1667 return CurDAG->getTargetConstant(Alignment, MVT::i32);
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001668}
1669
Jiangning Liu4df23632014-01-16 09:16:13 +00001670static bool isVLDfixed(unsigned Opc)
1671{
1672 switch (Opc) {
1673 default: return false;
1674 case ARM::VLD1d8wb_fixed : return true;
1675 case ARM::VLD1d16wb_fixed : return true;
1676 case ARM::VLD1d64Qwb_fixed : return true;
1677 case ARM::VLD1d32wb_fixed : return true;
1678 case ARM::VLD1d64wb_fixed : return true;
1679 case ARM::VLD1d64TPseudoWB_fixed : return true;
1680 case ARM::VLD1d64QPseudoWB_fixed : return true;
1681 case ARM::VLD1q8wb_fixed : return true;
1682 case ARM::VLD1q16wb_fixed : return true;
1683 case ARM::VLD1q32wb_fixed : return true;
1684 case ARM::VLD1q64wb_fixed : return true;
1685 case ARM::VLD2d8wb_fixed : return true;
1686 case ARM::VLD2d16wb_fixed : return true;
1687 case ARM::VLD2d32wb_fixed : return true;
1688 case ARM::VLD2q8PseudoWB_fixed : return true;
1689 case ARM::VLD2q16PseudoWB_fixed : return true;
1690 case ARM::VLD2q32PseudoWB_fixed : return true;
1691 case ARM::VLD2DUPd8wb_fixed : return true;
1692 case ARM::VLD2DUPd16wb_fixed : return true;
1693 case ARM::VLD2DUPd32wb_fixed : return true;
1694 }
1695}
1696
1697static bool isVSTfixed(unsigned Opc)
1698{
1699 switch (Opc) {
1700 default: return false;
1701 case ARM::VST1d8wb_fixed : return true;
1702 case ARM::VST1d16wb_fixed : return true;
1703 case ARM::VST1d32wb_fixed : return true;
1704 case ARM::VST1d64wb_fixed : return true;
Jim Grosbach1a597112014-04-03 23:43:18 +00001705 case ARM::VST1q8wb_fixed : return true;
1706 case ARM::VST1q16wb_fixed : return true;
1707 case ARM::VST1q32wb_fixed : return true;
1708 case ARM::VST1q64wb_fixed : return true;
Jiangning Liu4df23632014-01-16 09:16:13 +00001709 case ARM::VST1d64TPseudoWB_fixed : return true;
1710 case ARM::VST1d64QPseudoWB_fixed : return true;
1711 case ARM::VST2d8wb_fixed : return true;
1712 case ARM::VST2d16wb_fixed : return true;
1713 case ARM::VST2d32wb_fixed : return true;
1714 case ARM::VST2q8PseudoWB_fixed : return true;
1715 case ARM::VST2q16PseudoWB_fixed : return true;
1716 case ARM::VST2q32PseudoWB_fixed : return true;
1717 }
1718}
1719
Jim Grosbach2098cb12011-10-24 21:45:13 +00001720// Get the register stride update opcode of a VLD/VST instruction that
1721// is otherwise equivalent to the given fixed stride updating instruction.
1722static unsigned getVLDSTRegisterUpdateOpcode(unsigned Opc) {
Jiangning Liu4df23632014-01-16 09:16:13 +00001723 assert((isVLDfixed(Opc) || isVSTfixed(Opc))
1724 && "Incorrect fixed stride updating instruction.");
Jim Grosbach2098cb12011-10-24 21:45:13 +00001725 switch (Opc) {
1726 default: break;
1727 case ARM::VLD1d8wb_fixed: return ARM::VLD1d8wb_register;
1728 case ARM::VLD1d16wb_fixed: return ARM::VLD1d16wb_register;
1729 case ARM::VLD1d32wb_fixed: return ARM::VLD1d32wb_register;
1730 case ARM::VLD1d64wb_fixed: return ARM::VLD1d64wb_register;
1731 case ARM::VLD1q8wb_fixed: return ARM::VLD1q8wb_register;
1732 case ARM::VLD1q16wb_fixed: return ARM::VLD1q16wb_register;
1733 case ARM::VLD1q32wb_fixed: return ARM::VLD1q32wb_register;
1734 case ARM::VLD1q64wb_fixed: return ARM::VLD1q64wb_register;
Jiangning Liu4df23632014-01-16 09:16:13 +00001735 case ARM::VLD1d64Twb_fixed: return ARM::VLD1d64Twb_register;
1736 case ARM::VLD1d64Qwb_fixed: return ARM::VLD1d64Qwb_register;
1737 case ARM::VLD1d64TPseudoWB_fixed: return ARM::VLD1d64TPseudoWB_register;
1738 case ARM::VLD1d64QPseudoWB_fixed: return ARM::VLD1d64QPseudoWB_register;
Jim Grosbach05df4602011-10-31 21:50:31 +00001739
1740 case ARM::VST1d8wb_fixed: return ARM::VST1d8wb_register;
1741 case ARM::VST1d16wb_fixed: return ARM::VST1d16wb_register;
1742 case ARM::VST1d32wb_fixed: return ARM::VST1d32wb_register;
1743 case ARM::VST1d64wb_fixed: return ARM::VST1d64wb_register;
1744 case ARM::VST1q8wb_fixed: return ARM::VST1q8wb_register;
1745 case ARM::VST1q16wb_fixed: return ARM::VST1q16wb_register;
1746 case ARM::VST1q32wb_fixed: return ARM::VST1q32wb_register;
1747 case ARM::VST1q64wb_fixed: return ARM::VST1q64wb_register;
Jim Grosbach98d032f2011-11-29 22:38:04 +00001748 case ARM::VST1d64TPseudoWB_fixed: return ARM::VST1d64TPseudoWB_register;
Jim Grosbach5ee209c2011-11-29 22:58:48 +00001749 case ARM::VST1d64QPseudoWB_fixed: return ARM::VST1d64QPseudoWB_register;
Jim Grosbachd146a022011-12-09 21:28:25 +00001750
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001751 case ARM::VLD2d8wb_fixed: return ARM::VLD2d8wb_register;
1752 case ARM::VLD2d16wb_fixed: return ARM::VLD2d16wb_register;
1753 case ARM::VLD2d32wb_fixed: return ARM::VLD2d32wb_register;
Jim Grosbachd146a022011-12-09 21:28:25 +00001754 case ARM::VLD2q8PseudoWB_fixed: return ARM::VLD2q8PseudoWB_register;
1755 case ARM::VLD2q16PseudoWB_fixed: return ARM::VLD2q16PseudoWB_register;
1756 case ARM::VLD2q32PseudoWB_fixed: return ARM::VLD2q32PseudoWB_register;
1757
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001758 case ARM::VST2d8wb_fixed: return ARM::VST2d8wb_register;
1759 case ARM::VST2d16wb_fixed: return ARM::VST2d16wb_register;
1760 case ARM::VST2d32wb_fixed: return ARM::VST2d32wb_register;
Jim Grosbach88ac7612011-12-14 21:32:11 +00001761 case ARM::VST2q8PseudoWB_fixed: return ARM::VST2q8PseudoWB_register;
1762 case ARM::VST2q16PseudoWB_fixed: return ARM::VST2q16PseudoWB_register;
1763 case ARM::VST2q32PseudoWB_fixed: return ARM::VST2q32PseudoWB_register;
Jim Grosbachc80a2642011-12-21 19:40:55 +00001764
Jim Grosbach13a292c2012-03-06 22:01:44 +00001765 case ARM::VLD2DUPd8wb_fixed: return ARM::VLD2DUPd8wb_register;
1766 case ARM::VLD2DUPd16wb_fixed: return ARM::VLD2DUPd16wb_register;
1767 case ARM::VLD2DUPd32wb_fixed: return ARM::VLD2DUPd32wb_register;
Jim Grosbach2098cb12011-10-24 21:45:13 +00001768 }
1769 return Opc; // If not one we handle, return it unchanged.
1770}
1771
Bob Wilson06fce872011-02-07 17:43:21 +00001772SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +00001773 const uint16_t *DOpcodes,
1774 const uint16_t *QOpcodes0,
1775 const uint16_t *QOpcodes1) {
Bob Wilson340861d2010-03-23 05:25:43 +00001776 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00001777 SDLoc dl(N);
Bob Wilson12b47992009-10-14 17:28:52 +00001778
Bob Wilsonae08a732010-03-20 22:13:40 +00001779 SDValue MemAddr, Align;
Bob Wilson06fce872011-02-07 17:43:21 +00001780 unsigned AddrOpIdx = isUpdating ? 1 : 2;
1781 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
Craig Topper062a2ba2014-04-25 05:30:21 +00001782 return nullptr;
Bob Wilson12b47992009-10-14 17:28:52 +00001783
1784 SDValue Chain = N->getOperand(0);
1785 EVT VT = N->getValueType(0);
1786 bool is64BitVector = VT.is64BitVector();
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001787 Align = GetVLDSTAlign(Align, NumVecs, is64BitVector);
Bob Wilson9eeb8902010-09-23 21:43:54 +00001788
Bob Wilson12b47992009-10-14 17:28:52 +00001789 unsigned OpcodeIndex;
1790 switch (VT.getSimpleVT().SimpleTy) {
1791 default: llvm_unreachable("unhandled vld type");
1792 // Double-register operations:
1793 case MVT::v8i8: OpcodeIndex = 0; break;
1794 case MVT::v4i16: OpcodeIndex = 1; break;
1795 case MVT::v2f32:
1796 case MVT::v2i32: OpcodeIndex = 2; break;
1797 case MVT::v1i64: OpcodeIndex = 3; break;
1798 // Quad-register operations:
1799 case MVT::v16i8: OpcodeIndex = 0; break;
1800 case MVT::v8i16: OpcodeIndex = 1; break;
1801 case MVT::v4f32:
1802 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson340861d2010-03-23 05:25:43 +00001803 case MVT::v2i64: OpcodeIndex = 3;
Bob Wilsoncc0a2a72010-03-23 06:20:33 +00001804 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
Bob Wilson340861d2010-03-23 05:25:43 +00001805 break;
Bob Wilson12b47992009-10-14 17:28:52 +00001806 }
1807
Bob Wilson35fafca2010-09-03 18:16:02 +00001808 EVT ResTy;
1809 if (NumVecs == 1)
1810 ResTy = VT;
1811 else {
1812 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1813 if (!is64BitVector)
1814 ResTyElts *= 2;
1815 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1816 }
Bob Wilson06fce872011-02-07 17:43:21 +00001817 std::vector<EVT> ResTys;
1818 ResTys.push_back(ResTy);
1819 if (isUpdating)
1820 ResTys.push_back(MVT::i32);
1821 ResTys.push_back(MVT::Other);
Bob Wilson35fafca2010-09-03 18:16:02 +00001822
Evan Cheng3da64f762010-04-16 05:46:06 +00001823 SDValue Pred = getAL(CurDAG);
Bob Wilsonae08a732010-03-20 22:13:40 +00001824 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilson06fce872011-02-07 17:43:21 +00001825 SDNode *VLd;
1826 SmallVector<SDValue, 7> Ops;
Evan Cheng630063a2010-05-10 21:26:24 +00001827
Bob Wilson06fce872011-02-07 17:43:21 +00001828 // Double registers and VLD1/VLD2 quad registers are directly supported.
1829 if (is64BitVector || NumVecs <= 2) {
1830 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1831 QOpcodes0[OpcodeIndex]);
1832 Ops.push_back(MemAddr);
1833 Ops.push_back(Align);
1834 if (isUpdating) {
1835 SDValue Inc = N->getOperand(AddrOpIdx + 1);
Jim Grosbachd146a022011-12-09 21:28:25 +00001836 // FIXME: VLD1/VLD2 fixed increment doesn't need Reg0. Remove the reg0
Jim Grosbach2098cb12011-10-24 21:45:13 +00001837 // case entirely when the rest are updated to that form, too.
Jiangning Liu4df23632014-01-16 09:16:13 +00001838 if ((NumVecs <= 2) && !isa<ConstantSDNode>(Inc.getNode()))
Jim Grosbach2098cb12011-10-24 21:45:13 +00001839 Opc = getVLDSTRegisterUpdateOpcode(Opc);
Jiangning Liu4df23632014-01-16 09:16:13 +00001840 // FIXME: We use a VLD1 for v1i64 even if the pseudo says vld2/3/4, so
Jim Grosbach05df4602011-10-31 21:50:31 +00001841 // check for that explicitly too. Horribly hacky, but temporary.
Jiangning Liu4df23632014-01-16 09:16:13 +00001842 if ((NumVecs > 2 && !isVLDfixed(Opc)) ||
Jim Grosbach05df4602011-10-31 21:50:31 +00001843 !isa<ConstantSDNode>(Inc.getNode()))
Jim Grosbach2098cb12011-10-24 21:45:13 +00001844 Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc);
Evan Cheng630063a2010-05-10 21:26:24 +00001845 }
Bob Wilson06fce872011-02-07 17:43:21 +00001846 Ops.push_back(Pred);
1847 Ops.push_back(Reg0);
1848 Ops.push_back(Chain);
Michael Liaob53d8962013-04-19 22:22:57 +00001849 VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
Bob Wilson75a64082010-09-02 16:00:54 +00001850
Bob Wilson12b47992009-10-14 17:28:52 +00001851 } else {
1852 // Otherwise, quad registers are loaded with two separate instructions,
1853 // where one loads the even registers and the other loads the odd registers.
Bob Wilson35fafca2010-09-03 18:16:02 +00001854 EVT AddrTy = MemAddr.getValueType();
Bob Wilson12b47992009-10-14 17:28:52 +00001855
Bob Wilson06fce872011-02-07 17:43:21 +00001856 // Load the even subregs. This is always an updating load, so that it
1857 // provides the address to the second load for the odd subregs.
Bob Wilson35fafca2010-09-03 18:16:02 +00001858 SDValue ImplDef =
1859 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1860 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
Bob Wilsona609b892011-02-07 17:43:15 +00001861 SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
Michael Liaob53d8962013-04-19 22:22:57 +00001862 ResTy, AddrTy, MVT::Other, OpsA);
Bob Wilson35fafca2010-09-03 18:16:02 +00001863 Chain = SDValue(VLdA, 2);
Bob Wilson12b47992009-10-14 17:28:52 +00001864
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001865 // Load the odd subregs.
Bob Wilson06fce872011-02-07 17:43:21 +00001866 Ops.push_back(SDValue(VLdA, 1));
1867 Ops.push_back(Align);
1868 if (isUpdating) {
1869 SDValue Inc = N->getOperand(AddrOpIdx + 1);
1870 assert(isa<ConstantSDNode>(Inc.getNode()) &&
1871 "only constant post-increment update allowed for VLD3/4");
1872 (void)Inc;
1873 Ops.push_back(Reg0);
1874 }
1875 Ops.push_back(SDValue(VLdA, 0));
1876 Ops.push_back(Pred);
1877 Ops.push_back(Reg0);
1878 Ops.push_back(Chain);
Michael Liaob53d8962013-04-19 22:22:57 +00001879 VLd = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys, Ops);
Bob Wilson35fafca2010-09-03 18:16:02 +00001880 }
Bob Wilson12b47992009-10-14 17:28:52 +00001881
Evan Cheng40791332011-04-19 00:04:03 +00001882 // Transfer memoperands.
1883 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1884 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1885 cast<MachineSDNode>(VLd)->setMemRefs(MemOp, MemOp + 1);
1886
Bob Wilson06fce872011-02-07 17:43:21 +00001887 if (NumVecs == 1)
1888 return VLd;
1889
1890 // Extract out the subregisters.
1891 SDValue SuperReg = SDValue(VLd, 0);
1892 assert(ARM::dsub_7 == ARM::dsub_0+7 &&
1893 ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1894 unsigned Sub0 = (is64BitVector ? ARM::dsub_0 : ARM::qsub_0);
1895 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1896 ReplaceUses(SDValue(N, Vec),
1897 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
1898 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
1899 if (isUpdating)
1900 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLd, 2));
Craig Topper062a2ba2014-04-25 05:30:21 +00001901 return nullptr;
Bob Wilson12b47992009-10-14 17:28:52 +00001902}
1903
Bob Wilson06fce872011-02-07 17:43:21 +00001904SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +00001905 const uint16_t *DOpcodes,
1906 const uint16_t *QOpcodes0,
1907 const uint16_t *QOpcodes1) {
Bob Wilson3ed511b2010-07-06 23:36:25 +00001908 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00001909 SDLoc dl(N);
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001910
Bob Wilsonae08a732010-03-20 22:13:40 +00001911 SDValue MemAddr, Align;
Bob Wilson06fce872011-02-07 17:43:21 +00001912 unsigned AddrOpIdx = isUpdating ? 1 : 2;
1913 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
1914 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
Craig Topper062a2ba2014-04-25 05:30:21 +00001915 return nullptr;
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001916
Evan Cheng40791332011-04-19 00:04:03 +00001917 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1918 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1919
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001920 SDValue Chain = N->getOperand(0);
Bob Wilson06fce872011-02-07 17:43:21 +00001921 EVT VT = N->getOperand(Vec0Idx).getValueType();
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001922 bool is64BitVector = VT.is64BitVector();
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001923 Align = GetVLDSTAlign(Align, NumVecs, is64BitVector);
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001924
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001925 unsigned OpcodeIndex;
1926 switch (VT.getSimpleVT().SimpleTy) {
1927 default: llvm_unreachable("unhandled vst type");
1928 // Double-register operations:
1929 case MVT::v8i8: OpcodeIndex = 0; break;
1930 case MVT::v4i16: OpcodeIndex = 1; break;
1931 case MVT::v2f32:
1932 case MVT::v2i32: OpcodeIndex = 2; break;
1933 case MVT::v1i64: OpcodeIndex = 3; break;
1934 // Quad-register operations:
1935 case MVT::v16i8: OpcodeIndex = 0; break;
1936 case MVT::v8i16: OpcodeIndex = 1; break;
1937 case MVT::v4f32:
1938 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilsoncc0a2a72010-03-23 06:20:33 +00001939 case MVT::v2i64: OpcodeIndex = 3;
1940 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1941 break;
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001942 }
1943
Bob Wilson06fce872011-02-07 17:43:21 +00001944 std::vector<EVT> ResTys;
1945 if (isUpdating)
1946 ResTys.push_back(MVT::i32);
1947 ResTys.push_back(MVT::Other);
1948
Evan Cheng3da64f762010-04-16 05:46:06 +00001949 SDValue Pred = getAL(CurDAG);
Bob Wilsonae08a732010-03-20 22:13:40 +00001950 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilson06fce872011-02-07 17:43:21 +00001951 SmallVector<SDValue, 7> Ops;
Evan Chenga33fc862009-11-21 06:21:52 +00001952
Bob Wilson06fce872011-02-07 17:43:21 +00001953 // Double registers and VST1/VST2 quad registers are directly supported.
1954 if (is64BitVector || NumVecs <= 2) {
Bob Wilsona609b892011-02-07 17:43:15 +00001955 SDValue SrcReg;
Bob Wilson950882b2010-08-28 05:12:57 +00001956 if (NumVecs == 1) {
Bob Wilson06fce872011-02-07 17:43:21 +00001957 SrcReg = N->getOperand(Vec0Idx);
1958 } else if (is64BitVector) {
Evan Chenge276c182010-05-11 01:19:40 +00001959 // Form a REG_SEQUENCE to force register allocation.
Bob Wilson06fce872011-02-07 17:43:21 +00001960 SDValue V0 = N->getOperand(Vec0Idx + 0);
1961 SDValue V1 = N->getOperand(Vec0Idx + 1);
Evan Chenge276c182010-05-11 01:19:40 +00001962 if (NumVecs == 2)
Weiming Zhao95782222012-11-17 00:23:35 +00001963 SrcReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
Evan Chenge276c182010-05-11 01:19:40 +00001964 else {
Bob Wilson06fce872011-02-07 17:43:21 +00001965 SDValue V2 = N->getOperand(Vec0Idx + 2);
Bob Wilsona609b892011-02-07 17:43:15 +00001966 // If it's a vst3, form a quad D-register and leave the last part as
Evan Chenge276c182010-05-11 01:19:40 +00001967 // an undef.
1968 SDValue V3 = (NumVecs == 3)
1969 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
Bob Wilson06fce872011-02-07 17:43:21 +00001970 : N->getOperand(Vec0Idx + 3);
Weiming Zhao95782222012-11-17 00:23:35 +00001971 SrcReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
Evan Chenge276c182010-05-11 01:19:40 +00001972 }
Bob Wilson950882b2010-08-28 05:12:57 +00001973 } else {
1974 // Form a QQ register.
Bob Wilson06fce872011-02-07 17:43:21 +00001975 SDValue Q0 = N->getOperand(Vec0Idx);
1976 SDValue Q1 = N->getOperand(Vec0Idx + 1);
Weiming Zhao95782222012-11-17 00:23:35 +00001977 SrcReg = SDValue(createQRegPairNode(MVT::v4i64, Q0, Q1), 0);
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001978 }
Bob Wilson06fce872011-02-07 17:43:21 +00001979
1980 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1981 QOpcodes0[OpcodeIndex]);
1982 Ops.push_back(MemAddr);
1983 Ops.push_back(Align);
1984 if (isUpdating) {
1985 SDValue Inc = N->getOperand(AddrOpIdx + 1);
Jim Grosbach88ac7612011-12-14 21:32:11 +00001986 // FIXME: VST1/VST2 fixed increment doesn't need Reg0. Remove the reg0
Jim Grosbach05df4602011-10-31 21:50:31 +00001987 // case entirely when the rest are updated to that form, too.
Jim Grosbach88ac7612011-12-14 21:32:11 +00001988 if (NumVecs <= 2 && !isa<ConstantSDNode>(Inc.getNode()))
Jim Grosbach05df4602011-10-31 21:50:31 +00001989 Opc = getVLDSTRegisterUpdateOpcode(Opc);
Jiangning Liu4df23632014-01-16 09:16:13 +00001990 // FIXME: We use a VST1 for v1i64 even if the pseudo says vld2/3/4, so
Jim Grosbach05df4602011-10-31 21:50:31 +00001991 // check for that explicitly too. Horribly hacky, but temporary.
Jiangning Liu4df23632014-01-16 09:16:13 +00001992 if (!isa<ConstantSDNode>(Inc.getNode()))
1993 Ops.push_back(Inc);
1994 else if (NumVecs > 2 && !isVSTfixed(Opc))
1995 Ops.push_back(Reg0);
Bob Wilson06fce872011-02-07 17:43:21 +00001996 }
1997 Ops.push_back(SrcReg);
1998 Ops.push_back(Pred);
1999 Ops.push_back(Reg0);
2000 Ops.push_back(Chain);
Michael Liaob53d8962013-04-19 22:22:57 +00002001 SDNode *VSt = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
Evan Cheng40791332011-04-19 00:04:03 +00002002
2003 // Transfer memoperands.
2004 cast<MachineSDNode>(VSt)->setMemRefs(MemOp, MemOp + 1);
2005
2006 return VSt;
Bob Wilsonc350cdf2009-10-14 18:32:29 +00002007 }
2008
2009 // Otherwise, quad registers are stored with two separate instructions,
2010 // where one stores the even registers and the other stores the odd registers.
Evan Cheng9e688cb2010-05-15 07:53:37 +00002011
Bob Wilson01ac8f92010-06-16 21:34:01 +00002012 // Form the QQQQ REG_SEQUENCE.
Bob Wilson06fce872011-02-07 17:43:21 +00002013 SDValue V0 = N->getOperand(Vec0Idx + 0);
2014 SDValue V1 = N->getOperand(Vec0Idx + 1);
2015 SDValue V2 = N->getOperand(Vec0Idx + 2);
Bob Wilson950882b2010-08-28 05:12:57 +00002016 SDValue V3 = (NumVecs == 3)
2017 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
Bob Wilson06fce872011-02-07 17:43:21 +00002018 : N->getOperand(Vec0Idx + 3);
Weiming Zhao95782222012-11-17 00:23:35 +00002019 SDValue RegSeq = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilson01ac8f92010-06-16 21:34:01 +00002020
Bob Wilson06fce872011-02-07 17:43:21 +00002021 // Store the even D registers. This is always an updating store, so that it
2022 // provides the address to the second store for the odd subregs.
Bob Wilsona609b892011-02-07 17:43:15 +00002023 const SDValue OpsA[] = { MemAddr, Align, Reg0, RegSeq, Pred, Reg0, Chain };
2024 SDNode *VStA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
2025 MemAddr.getValueType(),
Michael Liaob53d8962013-04-19 22:22:57 +00002026 MVT::Other, OpsA);
Evan Cheng40791332011-04-19 00:04:03 +00002027 cast<MachineSDNode>(VStA)->setMemRefs(MemOp, MemOp + 1);
Bob Wilson01ac8f92010-06-16 21:34:01 +00002028 Chain = SDValue(VStA, 1);
2029
2030 // Store the odd D registers.
Bob Wilson06fce872011-02-07 17:43:21 +00002031 Ops.push_back(SDValue(VStA, 0));
2032 Ops.push_back(Align);
2033 if (isUpdating) {
2034 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2035 assert(isa<ConstantSDNode>(Inc.getNode()) &&
2036 "only constant post-increment update allowed for VST3/4");
2037 (void)Inc;
2038 Ops.push_back(Reg0);
2039 }
2040 Ops.push_back(RegSeq);
2041 Ops.push_back(Pred);
2042 Ops.push_back(Reg0);
2043 Ops.push_back(Chain);
Evan Cheng40791332011-04-19 00:04:03 +00002044 SDNode *VStB = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys,
Michael Liaob53d8962013-04-19 22:22:57 +00002045 Ops);
Evan Cheng40791332011-04-19 00:04:03 +00002046 cast<MachineSDNode>(VStB)->setMemRefs(MemOp, MemOp + 1);
2047 return VStB;
Bob Wilsonc350cdf2009-10-14 18:32:29 +00002048}
2049
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002050SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
Bob Wilson06fce872011-02-07 17:43:21 +00002051 bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +00002052 const uint16_t *DOpcodes,
2053 const uint16_t *QOpcodes) {
Bob Wilson93117bc2009-10-14 16:46:45 +00002054 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00002055 SDLoc dl(N);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002056
Bob Wilsonae08a732010-03-20 22:13:40 +00002057 SDValue MemAddr, Align;
Bob Wilson06fce872011-02-07 17:43:21 +00002058 unsigned AddrOpIdx = isUpdating ? 1 : 2;
2059 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
2060 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
Craig Topper062a2ba2014-04-25 05:30:21 +00002061 return nullptr;
Bob Wilson4145e3a2009-10-14 16:19:03 +00002062
Evan Cheng40791332011-04-19 00:04:03 +00002063 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2064 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2065
Bob Wilson4145e3a2009-10-14 16:19:03 +00002066 SDValue Chain = N->getOperand(0);
2067 unsigned Lane =
Bob Wilson06fce872011-02-07 17:43:21 +00002068 cast<ConstantSDNode>(N->getOperand(Vec0Idx + NumVecs))->getZExtValue();
2069 EVT VT = N->getOperand(Vec0Idx).getValueType();
Bob Wilson4145e3a2009-10-14 16:19:03 +00002070 bool is64BitVector = VT.is64BitVector();
2071
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00002072 unsigned Alignment = 0;
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002073 if (NumVecs != 3) {
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00002074 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002075 unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
2076 if (Alignment > NumBytes)
2077 Alignment = NumBytes;
Bob Wilsond29b38c2010-12-10 19:37:42 +00002078 if (Alignment < 8 && Alignment < NumBytes)
2079 Alignment = 0;
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002080 // Alignment must be a power of two; make sure of that.
2081 Alignment = (Alignment & -Alignment);
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00002082 if (Alignment == 1)
2083 Alignment = 0;
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002084 }
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00002085 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002086
Bob Wilson4145e3a2009-10-14 16:19:03 +00002087 unsigned OpcodeIndex;
2088 switch (VT.getSimpleVT().SimpleTy) {
Bob Wilson93117bc2009-10-14 16:46:45 +00002089 default: llvm_unreachable("unhandled vld/vst lane type");
Bob Wilson4145e3a2009-10-14 16:19:03 +00002090 // Double-register operations:
2091 case MVT::v8i8: OpcodeIndex = 0; break;
2092 case MVT::v4i16: OpcodeIndex = 1; break;
2093 case MVT::v2f32:
2094 case MVT::v2i32: OpcodeIndex = 2; break;
2095 // Quad-register operations:
2096 case MVT::v8i16: OpcodeIndex = 0; break;
2097 case MVT::v4f32:
2098 case MVT::v4i32: OpcodeIndex = 1; break;
2099 }
2100
Bob Wilson06fce872011-02-07 17:43:21 +00002101 std::vector<EVT> ResTys;
2102 if (IsLoad) {
2103 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
2104 if (!is64BitVector)
2105 ResTyElts *= 2;
2106 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(),
2107 MVT::i64, ResTyElts));
2108 }
2109 if (isUpdating)
2110 ResTys.push_back(MVT::i32);
2111 ResTys.push_back(MVT::Other);
2112
Evan Cheng3da64f762010-04-16 05:46:06 +00002113 SDValue Pred = getAL(CurDAG);
Bob Wilsonae08a732010-03-20 22:13:40 +00002114 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chenga33fc862009-11-21 06:21:52 +00002115
Bob Wilson06fce872011-02-07 17:43:21 +00002116 SmallVector<SDValue, 8> Ops;
Bob Wilson4145e3a2009-10-14 16:19:03 +00002117 Ops.push_back(MemAddr);
Jim Grosbachd1d002a2009-11-07 21:25:39 +00002118 Ops.push_back(Align);
Bob Wilson06fce872011-02-07 17:43:21 +00002119 if (isUpdating) {
2120 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2121 Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc);
2122 }
Bob Wilson01ac8f92010-06-16 21:34:01 +00002123
Bob Wilsond5c57a52010-09-13 23:01:35 +00002124 SDValue SuperReg;
Bob Wilson06fce872011-02-07 17:43:21 +00002125 SDValue V0 = N->getOperand(Vec0Idx + 0);
2126 SDValue V1 = N->getOperand(Vec0Idx + 1);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002127 if (NumVecs == 2) {
2128 if (is64BitVector)
Weiming Zhao95782222012-11-17 00:23:35 +00002129 SuperReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002130 else
Weiming Zhao95782222012-11-17 00:23:35 +00002131 SuperReg = SDValue(createQRegPairNode(MVT::v4i64, V0, V1), 0);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002132 } else {
Bob Wilson06fce872011-02-07 17:43:21 +00002133 SDValue V2 = N->getOperand(Vec0Idx + 2);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002134 SDValue V3 = (NumVecs == 3)
Bob Wilson06fce872011-02-07 17:43:21 +00002135 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2136 : N->getOperand(Vec0Idx + 3);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002137 if (is64BitVector)
Weiming Zhao95782222012-11-17 00:23:35 +00002138 SuperReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002139 else
Weiming Zhao95782222012-11-17 00:23:35 +00002140 SuperReg = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002141 }
Bob Wilsond5c57a52010-09-13 23:01:35 +00002142 Ops.push_back(SuperReg);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002143 Ops.push_back(getI32Imm(Lane));
Evan Chenga33fc862009-11-21 06:21:52 +00002144 Ops.push_back(Pred);
Bob Wilsonae08a732010-03-20 22:13:40 +00002145 Ops.push_back(Reg0);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002146 Ops.push_back(Chain);
2147
Bob Wilson06fce872011-02-07 17:43:21 +00002148 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
2149 QOpcodes[OpcodeIndex]);
Michael Liaob53d8962013-04-19 22:22:57 +00002150 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
Evan Cheng40791332011-04-19 00:04:03 +00002151 cast<MachineSDNode>(VLdLn)->setMemRefs(MemOp, MemOp + 1);
Bob Wilson93117bc2009-10-14 16:46:45 +00002152 if (!IsLoad)
Bob Wilson06fce872011-02-07 17:43:21 +00002153 return VLdLn;
Evan Cheng0cbd11d2010-05-15 01:36:29 +00002154
Bob Wilsond5c57a52010-09-13 23:01:35 +00002155 // Extract the subregisters.
Bob Wilson06fce872011-02-07 17:43:21 +00002156 SuperReg = SDValue(VLdLn, 0);
2157 assert(ARM::dsub_7 == ARM::dsub_0+7 &&
2158 ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
2159 unsigned Sub0 = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
Bob Wilson01ac8f92010-06-16 21:34:01 +00002160 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2161 ReplaceUses(SDValue(N, Vec),
Bob Wilson06fce872011-02-07 17:43:21 +00002162 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
2163 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdLn, 1));
2164 if (isUpdating)
2165 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdLn, 2));
Craig Topper062a2ba2014-04-25 05:30:21 +00002166 return nullptr;
Bob Wilson4145e3a2009-10-14 16:19:03 +00002167}
2168
Bob Wilson06fce872011-02-07 17:43:21 +00002169SDNode *ARMDAGToDAGISel::SelectVLDDup(SDNode *N, bool isUpdating,
Craig Topper01736f82012-05-24 05:17:00 +00002170 unsigned NumVecs,
2171 const uint16_t *Opcodes) {
Bob Wilson2d790df2010-11-28 06:51:26 +00002172 assert(NumVecs >=2 && NumVecs <= 4 && "VLDDup NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00002173 SDLoc dl(N);
Bob Wilson2d790df2010-11-28 06:51:26 +00002174
2175 SDValue MemAddr, Align;
2176 if (!SelectAddrMode6(N, N->getOperand(1), MemAddr, Align))
Craig Topper062a2ba2014-04-25 05:30:21 +00002177 return nullptr;
Bob Wilson2d790df2010-11-28 06:51:26 +00002178
Evan Cheng40791332011-04-19 00:04:03 +00002179 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2180 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2181
Bob Wilson2d790df2010-11-28 06:51:26 +00002182 SDValue Chain = N->getOperand(0);
2183 EVT VT = N->getValueType(0);
2184
2185 unsigned Alignment = 0;
2186 if (NumVecs != 3) {
2187 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
2188 unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
2189 if (Alignment > NumBytes)
2190 Alignment = NumBytes;
Bob Wilsond29b38c2010-12-10 19:37:42 +00002191 if (Alignment < 8 && Alignment < NumBytes)
2192 Alignment = 0;
Bob Wilson2d790df2010-11-28 06:51:26 +00002193 // Alignment must be a power of two; make sure of that.
2194 Alignment = (Alignment & -Alignment);
2195 if (Alignment == 1)
2196 Alignment = 0;
2197 }
2198 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
2199
2200 unsigned OpcodeIndex;
2201 switch (VT.getSimpleVT().SimpleTy) {
2202 default: llvm_unreachable("unhandled vld-dup type");
2203 case MVT::v8i8: OpcodeIndex = 0; break;
2204 case MVT::v4i16: OpcodeIndex = 1; break;
2205 case MVT::v2f32:
2206 case MVT::v2i32: OpcodeIndex = 2; break;
2207 }
2208
2209 SDValue Pred = getAL(CurDAG);
2210 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2211 SDValue SuperReg;
2212 unsigned Opc = Opcodes[OpcodeIndex];
Bob Wilson06fce872011-02-07 17:43:21 +00002213 SmallVector<SDValue, 6> Ops;
2214 Ops.push_back(MemAddr);
2215 Ops.push_back(Align);
2216 if (isUpdating) {
Jim Grosbachc80a2642011-12-21 19:40:55 +00002217 // fixed-stride update instructions don't have an explicit writeback
2218 // operand. It's implicit in the opcode itself.
Bob Wilson06fce872011-02-07 17:43:21 +00002219 SDValue Inc = N->getOperand(2);
Jim Grosbachc80a2642011-12-21 19:40:55 +00002220 if (!isa<ConstantSDNode>(Inc.getNode()))
2221 Ops.push_back(Inc);
2222 // FIXME: VLD3 and VLD4 haven't been updated to that form yet.
2223 else if (NumVecs > 2)
2224 Ops.push_back(Reg0);
Bob Wilson06fce872011-02-07 17:43:21 +00002225 }
2226 Ops.push_back(Pred);
2227 Ops.push_back(Reg0);
2228 Ops.push_back(Chain);
Bob Wilson2d790df2010-11-28 06:51:26 +00002229
2230 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
Bob Wilson06fce872011-02-07 17:43:21 +00002231 std::vector<EVT> ResTys;
Evan Cheng40791332011-04-19 00:04:03 +00002232 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(), MVT::i64,ResTyElts));
Bob Wilson06fce872011-02-07 17:43:21 +00002233 if (isUpdating)
2234 ResTys.push_back(MVT::i32);
2235 ResTys.push_back(MVT::Other);
Michael Liaob53d8962013-04-19 22:22:57 +00002236 SDNode *VLdDup = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
Evan Cheng40791332011-04-19 00:04:03 +00002237 cast<MachineSDNode>(VLdDup)->setMemRefs(MemOp, MemOp + 1);
Bob Wilson2d790df2010-11-28 06:51:26 +00002238 SuperReg = SDValue(VLdDup, 0);
Bob Wilson2d790df2010-11-28 06:51:26 +00002239
2240 // Extract the subregisters.
2241 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
2242 unsigned SubIdx = ARM::dsub_0;
2243 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2244 ReplaceUses(SDValue(N, Vec),
2245 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
Bob Wilson06fce872011-02-07 17:43:21 +00002246 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdDup, 1));
2247 if (isUpdating)
2248 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdDup, 2));
Craig Topper062a2ba2014-04-25 05:30:21 +00002249 return nullptr;
Bob Wilson2d790df2010-11-28 06:51:26 +00002250}
2251
Bob Wilson5bc8a792010-07-07 00:08:54 +00002252SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
2253 unsigned Opc) {
Bob Wilson3ed511b2010-07-06 23:36:25 +00002254 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00002255 SDLoc dl(N);
Bob Wilson3ed511b2010-07-06 23:36:25 +00002256 EVT VT = N->getValueType(0);
Bob Wilson5bc8a792010-07-07 00:08:54 +00002257 unsigned FirstTblReg = IsExt ? 2 : 1;
Bob Wilson3ed511b2010-07-06 23:36:25 +00002258
2259 // Form a REG_SEQUENCE to force register allocation.
2260 SDValue RegSeq;
Bob Wilson5bc8a792010-07-07 00:08:54 +00002261 SDValue V0 = N->getOperand(FirstTblReg + 0);
2262 SDValue V1 = N->getOperand(FirstTblReg + 1);
Bob Wilson3ed511b2010-07-06 23:36:25 +00002263 if (NumVecs == 2)
Weiming Zhao95782222012-11-17 00:23:35 +00002264 RegSeq = SDValue(createDRegPairNode(MVT::v16i8, V0, V1), 0);
Bob Wilson3ed511b2010-07-06 23:36:25 +00002265 else {
Bob Wilson5bc8a792010-07-07 00:08:54 +00002266 SDValue V2 = N->getOperand(FirstTblReg + 2);
Jim Grosbachd37f0712010-10-21 19:38:40 +00002267 // If it's a vtbl3, form a quad D-register and leave the last part as
Bob Wilson3ed511b2010-07-06 23:36:25 +00002268 // an undef.
2269 SDValue V3 = (NumVecs == 3)
2270 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
Bob Wilson5bc8a792010-07-07 00:08:54 +00002271 : N->getOperand(FirstTblReg + 3);
Weiming Zhao95782222012-11-17 00:23:35 +00002272 RegSeq = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
Bob Wilson3ed511b2010-07-06 23:36:25 +00002273 }
2274
Bob Wilson5bc8a792010-07-07 00:08:54 +00002275 SmallVector<SDValue, 6> Ops;
2276 if (IsExt)
2277 Ops.push_back(N->getOperand(1));
Bob Wilsonc597fd3b2010-09-13 23:55:10 +00002278 Ops.push_back(RegSeq);
Bob Wilson5bc8a792010-07-07 00:08:54 +00002279 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
Bob Wilson3ed511b2010-07-06 23:36:25 +00002280 Ops.push_back(getAL(CurDAG)); // predicate
2281 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
Michael Liaob53d8962013-04-19 22:22:57 +00002282 return CurDAG->getMachineNode(Opc, dl, VT, Ops);
Bob Wilson3ed511b2010-07-06 23:36:25 +00002283}
2284
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002285SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
Jim Grosbach825cb292010-04-22 23:24:18 +00002286 bool isSigned) {
Sandeep Patel423e42b2009-10-13 18:59:48 +00002287 if (!Subtarget->hasV6T2Ops())
Craig Topper062a2ba2014-04-25 05:30:21 +00002288 return nullptr;
Bob Wilson93117bc2009-10-14 16:46:45 +00002289
Evan Chengeae6d2c2012-12-19 20:16:09 +00002290 unsigned Opc = isSigned
2291 ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
Jim Grosbach825cb292010-04-22 23:24:18 +00002292 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
2293
Jim Grosbach825cb292010-04-22 23:24:18 +00002294 // For unsigned extracts, check for a shift right and mask
2295 unsigned And_imm = 0;
2296 if (N->getOpcode() == ISD::AND) {
2297 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
2298
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002299 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
Jim Grosbach825cb292010-04-22 23:24:18 +00002300 if (And_imm & (And_imm + 1))
Craig Topper062a2ba2014-04-25 05:30:21 +00002301 return nullptr;
Jim Grosbach825cb292010-04-22 23:24:18 +00002302
2303 unsigned Srl_imm = 0;
2304 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
2305 Srl_imm)) {
2306 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2307
Jim Grosbach03f56d92011-07-27 21:09:25 +00002308 // Note: The width operand is encoded as width-1.
2309 unsigned Width = CountTrailingOnes_32(And_imm) - 1;
Jim Grosbach825cb292010-04-22 23:24:18 +00002310 unsigned LSB = Srl_imm;
Evan Chengeae6d2c2012-12-19 20:16:09 +00002311
Jim Grosbach825cb292010-04-22 23:24:18 +00002312 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengeae6d2c2012-12-19 20:16:09 +00002313
2314 if ((LSB + Width + 1) == N->getValueType(0).getSizeInBits()) {
2315 // It's cheaper to use a right shift to extract the top bits.
2316 if (Subtarget->isThumb()) {
2317 Opc = isSigned ? ARM::t2ASRri : ARM::t2LSRri;
2318 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2319 CurDAG->getTargetConstant(LSB, MVT::i32),
2320 getAL(CurDAG), Reg0, Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002321 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
Evan Chengeae6d2c2012-12-19 20:16:09 +00002322 }
2323
2324 // ARM models shift instructions as MOVsi with shifter operand.
2325 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(ISD::SRL);
2326 SDValue ShOpc =
2327 CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, LSB),
2328 MVT::i32);
2329 SDValue Ops[] = { N->getOperand(0).getOperand(0), ShOpc,
2330 getAL(CurDAG), Reg0, Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002331 return CurDAG->SelectNodeTo(N, ARM::MOVsi, MVT::i32, Ops);
Evan Chengeae6d2c2012-12-19 20:16:09 +00002332 }
2333
Jim Grosbach825cb292010-04-22 23:24:18 +00002334 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2335 CurDAG->getTargetConstant(LSB, MVT::i32),
2336 CurDAG->getTargetConstant(Width, MVT::i32),
Craig Topper481fb282014-04-27 19:21:11 +00002337 getAL(CurDAG), Reg0 };
2338 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
Jim Grosbach825cb292010-04-22 23:24:18 +00002339 }
2340 }
Craig Topper062a2ba2014-04-25 05:30:21 +00002341 return nullptr;
Jim Grosbach825cb292010-04-22 23:24:18 +00002342 }
2343
2344 // Otherwise, we're looking for a shift of a shift
Sandeep Patel423e42b2009-10-13 18:59:48 +00002345 unsigned Shl_imm = 0;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002346 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
Sandeep Patel423e42b2009-10-13 18:59:48 +00002347 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
2348 unsigned Srl_imm = 0;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002349 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
Sandeep Patel423e42b2009-10-13 18:59:48 +00002350 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
Jim Grosbach03f56d92011-07-27 21:09:25 +00002351 // Note: The width operand is encoded as width-1.
2352 unsigned Width = 32 - Srl_imm - 1;
Sandeep Patel423e42b2009-10-13 18:59:48 +00002353 int LSB = Srl_imm - Shl_imm;
Evan Cheng0f55e9c2009-10-22 00:40:00 +00002354 if (LSB < 0)
Craig Topper062a2ba2014-04-25 05:30:21 +00002355 return nullptr;
Sandeep Patel423e42b2009-10-13 18:59:48 +00002356 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002357 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sandeep Patel423e42b2009-10-13 18:59:48 +00002358 CurDAG->getTargetConstant(LSB, MVT::i32),
2359 CurDAG->getTargetConstant(Width, MVT::i32),
2360 getAL(CurDAG), Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002361 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
Sandeep Patel423e42b2009-10-13 18:59:48 +00002362 }
2363 }
Tim Northover14ff2df2014-07-23 13:59:12 +00002364
2365 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG) {
2366 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
2367 unsigned LSB = 0;
2368 if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL, LSB) &&
2369 !isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRA, LSB))
2370 return nullptr;
2371
2372 if (LSB + Width > 32)
2373 return nullptr;
2374
2375 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2376 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2377 CurDAG->getTargetConstant(LSB, MVT::i32),
2378 CurDAG->getTargetConstant(Width - 1, MVT::i32),
2379 getAL(CurDAG), Reg0 };
2380 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2381 }
2382
Craig Topper062a2ba2014-04-25 05:30:21 +00002383 return nullptr;
Sandeep Patel423e42b2009-10-13 18:59:48 +00002384}
2385
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002386/// Target-specific DAG combining for ISD::XOR.
2387/// Target-independent combining lowers SELECT_CC nodes of the form
2388/// select_cc setg[ge] X, 0, X, -X
2389/// select_cc setgt X, -1, X, -X
2390/// select_cc setl[te] X, 0, -X, X
2391/// select_cc setlt X, 1, -X, X
2392/// which represent Integer ABS into:
2393/// Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2394/// ARM instruction selection detects the latter and matches it to
2395/// ARM::ABS or ARM::t2ABS machine node.
2396SDNode *ARMDAGToDAGISel::SelectABSOp(SDNode *N){
2397 SDValue XORSrc0 = N->getOperand(0);
2398 SDValue XORSrc1 = N->getOperand(1);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002399 EVT VT = N->getValueType(0);
2400
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002401 if (Subtarget->isThumb1Only())
Craig Topper062a2ba2014-04-25 05:30:21 +00002402 return nullptr;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002403
Jim Grosbachb437a8c2012-08-01 20:33:00 +00002404 if (XORSrc0.getOpcode() != ISD::ADD || XORSrc1.getOpcode() != ISD::SRA)
Craig Topper062a2ba2014-04-25 05:30:21 +00002405 return nullptr;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002406
2407 SDValue ADDSrc0 = XORSrc0.getOperand(0);
2408 SDValue ADDSrc1 = XORSrc0.getOperand(1);
2409 SDValue SRASrc0 = XORSrc1.getOperand(0);
2410 SDValue SRASrc1 = XORSrc1.getOperand(1);
2411 ConstantSDNode *SRAConstant = dyn_cast<ConstantSDNode>(SRASrc1);
2412 EVT XType = SRASrc0.getValueType();
2413 unsigned Size = XType.getSizeInBits() - 1;
2414
Jim Grosbachb437a8c2012-08-01 20:33:00 +00002415 if (ADDSrc1 == XORSrc1 && ADDSrc0 == SRASrc0 &&
Craig Topper062a2ba2014-04-25 05:30:21 +00002416 XType.isInteger() && SRAConstant != nullptr &&
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002417 Size == SRAConstant->getZExtValue()) {
Jim Grosbachb437a8c2012-08-01 20:33:00 +00002418 unsigned Opcode = Subtarget->isThumb2() ? ARM::t2ABS : ARM::ABS;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002419 return CurDAG->SelectNodeTo(N, Opcode, VT, ADDSrc0);
2420 }
2421
Craig Topper062a2ba2014-04-25 05:30:21 +00002422 return nullptr;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002423}
2424
Evan Chengd85631e2010-05-05 18:28:36 +00002425SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
2426 // The only time a CONCAT_VECTORS operation can have legal types is when
2427 // two 64-bit vectors are concatenated to a 128-bit vector.
2428 EVT VT = N->getValueType(0);
2429 if (!VT.is128BitVector() || N->getNumOperands() != 2)
2430 llvm_unreachable("unexpected CONCAT_VECTORS");
Weiming Zhao95782222012-11-17 00:23:35 +00002431 return createDRegPairNode(VT, N->getOperand(0), N->getOperand(1));
Evan Chengd85631e2010-05-05 18:28:36 +00002432}
2433
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002434SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002435 SDLoc dl(N);
Evan Cheng10043e22007-01-19 07:51:42 +00002436
Tim Northover31d093c2013-09-22 08:21:56 +00002437 if (N->isMachineOpcode()) {
2438 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00002439 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00002440 }
Rafael Espindola4e760152006-06-12 12:28:08 +00002441
2442 switch (N->getOpcode()) {
Evan Cheng10043e22007-01-19 07:51:42 +00002443 default: break;
Weiming Zhaoc5987002013-02-14 18:10:21 +00002444 case ISD::INLINEASM: {
2445 SDNode *ResNode = SelectInlineAsm(N);
2446 if (ResNode)
2447 return ResNode;
2448 break;
2449 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002450 case ISD::XOR: {
2451 // Select special operations if XOR node forms integer ABS pattern
2452 SDNode *ResNode = SelectABSOp(N);
2453 if (ResNode)
2454 return ResNode;
2455 // Other cases are autogenerated.
2456 break;
2457 }
Evan Cheng10043e22007-01-19 07:51:42 +00002458 case ISD::Constant: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002459 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00002460 bool UseCP = true;
Eric Christopherc1058df2014-07-04 01:55:26 +00002461 if (Subtarget->useMovt(*MF))
Anton Korobeynikov7c2b1e72009-09-27 23:52:58 +00002462 // Thumb2-aware targets have the MOVT instruction, so all immediates can
2463 // be done with MOV + MOVT, at worst.
Tim Northover55c625f2014-01-23 13:43:47 +00002464 UseCP = false;
Anton Korobeynikov7c2b1e72009-09-27 23:52:58 +00002465 else {
2466 if (Subtarget->isThumb()) {
Tim Northover55c625f2014-01-23 13:43:47 +00002467 UseCP = (Val > 255 && // MOV
2468 ~Val > 255 && // MOV + MVN
2469 !ARM_AM::isThumbImmShiftedVal(Val) && // MOV + LSL
2470 !(Subtarget->hasV6T2Ops() && Val <= 0xffff)); // MOVW
Anton Korobeynikov7c2b1e72009-09-27 23:52:58 +00002471 } else
Tim Northover55c625f2014-01-23 13:43:47 +00002472 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
2473 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
2474 !ARM_AM::isSOImmTwoPartVal(Val) && // two instrs.
2475 !(Subtarget->hasV6T2Ops() && Val <= 0xffff)); // MOVW
Anton Korobeynikov7c2b1e72009-09-27 23:52:58 +00002476 }
2477
Evan Cheng10043e22007-01-19 07:51:42 +00002478 if (UseCP) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002479 SDValue CPIdx =
Owen Anderson55f1c092009-08-13 21:58:54 +00002480 CurDAG->getTargetConstantPool(ConstantInt::get(
2481 Type::getInt32Ty(*CurDAG->getContext()), Val),
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002482 getTargetLowering()->getPointerTy());
Evan Cheng1526ba52007-01-24 08:53:17 +00002483
2484 SDNode *ResNode;
Tim Northover55c625f2014-01-23 13:43:47 +00002485 if (Subtarget->isThumb()) {
Evan Cheng3da64f762010-04-16 05:46:06 +00002486 SDValue Pred = getAL(CurDAG);
Owen Anderson9f944592009-08-11 20:47:22 +00002487 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
Evan Chengcd4cdd12009-07-11 06:43:01 +00002488 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
Jim Grosbachbfef3092010-12-15 23:52:36 +00002489 ResNode = CurDAG->getMachineNode(ARM::tLDRpci, dl, MVT::i32, MVT::Other,
Michael Liaob53d8962013-04-19 22:22:57 +00002490 Ops);
Evan Chengcd4cdd12009-07-11 06:43:01 +00002491 } else {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002492 SDValue Ops[] = {
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002493 CPIdx,
Owen Anderson9f944592009-08-11 20:47:22 +00002494 CurDAG->getTargetConstant(0, MVT::i32),
Evan Cheng7e90b112007-07-05 07:15:27 +00002495 getAL(CurDAG),
Owen Anderson9f944592009-08-11 20:47:22 +00002496 CurDAG->getRegister(0, MVT::i32),
Evan Cheng1526ba52007-01-24 08:53:17 +00002497 CurDAG->getEntryNode()
2498 };
Dan Gohman32f71d72009-09-25 18:54:59 +00002499 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
Michael Liaob53d8962013-04-19 22:22:57 +00002500 Ops);
Evan Cheng1526ba52007-01-24 08:53:17 +00002501 }
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002502 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
Craig Topper062a2ba2014-04-25 05:30:21 +00002503 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00002504 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002505
Evan Cheng10043e22007-01-19 07:51:42 +00002506 // Other cases are autogenerated.
Rafael Espindola4e760152006-06-12 12:28:08 +00002507 break;
Evan Cheng10043e22007-01-19 07:51:42 +00002508 }
Rafael Espindola5f7ab1b2006-11-09 13:58:55 +00002509 case ISD::FrameIndex: {
Evan Cheng10043e22007-01-19 07:51:42 +00002510 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindola5f7ab1b2006-11-09 13:58:55 +00002511 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002512 SDValue TFI = CurDAG->getTargetFrameIndex(FI,
2513 getTargetLowering()->getPointerTy());
David Goodwin22c2fba2009-07-08 23:10:31 +00002514 if (Subtarget->isThumb1Only()) {
Jim Grosbach1b8457a2011-08-24 17:46:13 +00002515 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
2516 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Craig Topper481fb282014-04-27 19:21:11 +00002517 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, Ops);
Jim Grosbachfde21102009-04-07 20:34:09 +00002518 } else {
David Goodwin4ad77972009-07-14 18:48:51 +00002519 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
2520 ARM::t2ADDri : ARM::ADDri);
Owen Anderson9f944592009-08-11 20:47:22 +00002521 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
2522 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2523 CurDAG->getRegister(0, MVT::i32) };
Craig Topper481fb282014-04-27 19:21:11 +00002524 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
Evan Cheng7e90b112007-07-05 07:15:27 +00002525 }
Evan Cheng10043e22007-01-19 07:51:42 +00002526 }
Sandeep Patel423e42b2009-10-13 18:59:48 +00002527 case ISD::SRL:
Jim Grosbach825cb292010-04-22 23:24:18 +00002528 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
Sandeep Patel423e42b2009-10-13 18:59:48 +00002529 return I;
2530 break;
Tim Northover14ff2df2014-07-23 13:59:12 +00002531 case ISD::SIGN_EXTEND_INREG:
Sandeep Patel423e42b2009-10-13 18:59:48 +00002532 case ISD::SRA:
Jim Grosbach825cb292010-04-22 23:24:18 +00002533 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
Sandeep Patel423e42b2009-10-13 18:59:48 +00002534 return I;
2535 break;
Evan Cheng10043e22007-01-19 07:51:42 +00002536 case ISD::MUL:
Evan Chengb24e51e2009-07-07 01:17:28 +00002537 if (Subtarget->isThumb1Only())
Evan Cheng139edae2007-01-24 02:21:22 +00002538 break;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002539 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002540 unsigned RHSV = C->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00002541 if (!RHSV) break;
2542 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002543 unsigned ShImm = Log2_32(RHSV-1);
2544 if (ShImm >= 32)
2545 break;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002546 SDValue V = N->getOperand(0);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002547 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson9f944592009-08-11 20:47:22 +00002548 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
2549 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng1ec43962009-07-22 18:08:05 +00002550 if (Subtarget->isThumb()) {
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002551 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002552 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002553 } else {
2554 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002555 return CurDAG->SelectNodeTo(N, ARM::ADDrsi, MVT::i32, Ops);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002556 }
Evan Cheng10043e22007-01-19 07:51:42 +00002557 }
2558 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002559 unsigned ShImm = Log2_32(RHSV+1);
2560 if (ShImm >= 32)
2561 break;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002562 SDValue V = N->getOperand(0);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002563 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson9f944592009-08-11 20:47:22 +00002564 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
2565 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng1ec43962009-07-22 18:08:05 +00002566 if (Subtarget->isThumb()) {
Bob Wilsonb6112e82010-05-28 00:27:15 +00002567 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002568 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002569 } else {
2570 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002571 return CurDAG->SelectNodeTo(N, ARM::RSBrsi, MVT::i32, Ops);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002572 }
Evan Cheng10043e22007-01-19 07:51:42 +00002573 }
2574 }
2575 break;
Evan Cheng786b15f2009-10-21 08:15:52 +00002576 case ISD::AND: {
Jim Grosbach825cb292010-04-22 23:24:18 +00002577 // Check for unsigned bitfield extract
2578 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
2579 return I;
2580
Evan Cheng786b15f2009-10-21 08:15:52 +00002581 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
2582 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
2583 // are entirely contributed by c2 and lower 16-bits are entirely contributed
2584 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
2585 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002586 EVT VT = N->getValueType(0);
Evan Cheng786b15f2009-10-21 08:15:52 +00002587 if (VT != MVT::i32)
2588 break;
2589 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
2590 ? ARM::t2MOVTi16
2591 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
2592 if (!Opc)
2593 break;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002594 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Evan Cheng786b15f2009-10-21 08:15:52 +00002595 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2596 if (!N1C)
2597 break;
2598 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
2599 SDValue N2 = N0.getOperand(1);
2600 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2601 if (!N2C)
2602 break;
2603 unsigned N1CVal = N1C->getZExtValue();
2604 unsigned N2CVal = N2C->getZExtValue();
2605 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
2606 (N1CVal & 0xffffU) == 0xffffU &&
2607 (N2CVal & 0xffffU) == 0x0U) {
2608 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
2609 MVT::i32);
2610 SDValue Ops[] = { N0.getOperand(0), Imm16,
2611 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Michael Liaob53d8962013-04-19 22:22:57 +00002612 return CurDAG->getMachineNode(Opc, dl, VT, Ops);
Evan Cheng786b15f2009-10-21 08:15:52 +00002613 }
2614 }
2615 break;
2616 }
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002617 case ARMISD::VMOVRRD:
2618 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002619 N->getOperand(0), getAL(CurDAG),
Dan Gohman32f71d72009-09-25 18:54:59 +00002620 CurDAG->getRegister(0, MVT::i32));
Dan Gohmana1603612007-10-08 18:33:35 +00002621 case ISD::UMUL_LOHI: {
Evan Chengb24e51e2009-07-07 01:17:28 +00002622 if (Subtarget->isThumb1Only())
2623 break;
2624 if (Subtarget->isThumb()) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002625 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Michael Liaob53d8962013-04-19 22:22:57 +00002626 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
2627 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32, Ops);
Evan Chengb24e51e2009-07-07 01:17:28 +00002628 } else {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002629 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson9f944592009-08-11 20:47:22 +00002630 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2631 CurDAG->getRegister(0, MVT::i32) };
Anton Korobeynikov62acecd2011-01-01 20:38:38 +00002632 return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2633 ARM::UMULL : ARM::UMULLv5,
Michael Liaob53d8962013-04-19 22:22:57 +00002634 dl, MVT::i32, MVT::i32, Ops);
Evan Chengb24e51e2009-07-07 01:17:28 +00002635 }
Evan Cheng7e90b112007-07-05 07:15:27 +00002636 }
Dan Gohmana1603612007-10-08 18:33:35 +00002637 case ISD::SMUL_LOHI: {
Evan Chengb24e51e2009-07-07 01:17:28 +00002638 if (Subtarget->isThumb1Only())
2639 break;
2640 if (Subtarget->isThumb()) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002641 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson9f944592009-08-11 20:47:22 +00002642 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Michael Liaob53d8962013-04-19 22:22:57 +00002643 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32, Ops);
Evan Chengb24e51e2009-07-07 01:17:28 +00002644 } else {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002645 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson9f944592009-08-11 20:47:22 +00002646 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2647 CurDAG->getRegister(0, MVT::i32) };
Anton Korobeynikov62acecd2011-01-01 20:38:38 +00002648 return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2649 ARM::SMULL : ARM::SMULLv5,
Michael Liaob53d8962013-04-19 22:22:57 +00002650 dl, MVT::i32, MVT::i32, Ops);
Evan Chengb24e51e2009-07-07 01:17:28 +00002651 }
Evan Cheng7e90b112007-07-05 07:15:27 +00002652 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002653 case ARMISD::UMLAL:{
2654 if (Subtarget->isThumb()) {
2655 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2656 N->getOperand(3), getAL(CurDAG),
2657 CurDAG->getRegister(0, MVT::i32)};
Michael Liaob53d8962013-04-19 22:22:57 +00002658 return CurDAG->getMachineNode(ARM::t2UMLAL, dl, MVT::i32, MVT::i32, Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002659 }else{
2660 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2661 N->getOperand(3), getAL(CurDAG),
2662 CurDAG->getRegister(0, MVT::i32),
2663 CurDAG->getRegister(0, MVT::i32) };
2664 return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2665 ARM::UMLAL : ARM::UMLALv5,
Michael Liaob53d8962013-04-19 22:22:57 +00002666 dl, MVT::i32, MVT::i32, Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002667 }
2668 }
2669 case ARMISD::SMLAL:{
2670 if (Subtarget->isThumb()) {
2671 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2672 N->getOperand(3), getAL(CurDAG),
2673 CurDAG->getRegister(0, MVT::i32)};
Michael Liaob53d8962013-04-19 22:22:57 +00002674 return CurDAG->getMachineNode(ARM::t2SMLAL, dl, MVT::i32, MVT::i32, Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002675 }else{
2676 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2677 N->getOperand(3), getAL(CurDAG),
2678 CurDAG->getRegister(0, MVT::i32),
2679 CurDAG->getRegister(0, MVT::i32) };
2680 return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2681 ARM::SMLAL : ARM::SMLALv5,
Michael Liaob53d8962013-04-19 22:22:57 +00002682 dl, MVT::i32, MVT::i32, Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002683 }
2684 }
Evan Cheng10043e22007-01-19 07:51:42 +00002685 case ISD::LOAD: {
Craig Topper062a2ba2014-04-25 05:30:21 +00002686 SDNode *ResNode = nullptr;
Evan Chengb24e51e2009-07-07 01:17:28 +00002687 if (Subtarget->isThumb() && Subtarget->hasThumb2())
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002688 ResNode = SelectT2IndexedLoad(N);
Evan Cheng84c6cda2009-07-02 07:28:31 +00002689 else
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002690 ResNode = SelectARMIndexedLoad(N);
Evan Chengd9c55362009-07-02 01:23:32 +00002691 if (ResNode)
2692 return ResNode;
Evan Cheng10043e22007-01-19 07:51:42 +00002693 // Other cases are autogenerated.
Rafael Espindola5f7ab1b2006-11-09 13:58:55 +00002694 break;
Rafael Espindola4e760152006-06-12 12:28:08 +00002695 }
Evan Cheng7e90b112007-07-05 07:15:27 +00002696 case ARMISD::BRCOND: {
2697 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2698 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2699 // Pattern complexity = 6 cost = 1 size = 0
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00002700
Evan Cheng7e90b112007-07-05 07:15:27 +00002701 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2702 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
2703 // Pattern complexity = 6 cost = 1 size = 0
2704
David Goodwin27303cd2009-06-30 18:04:13 +00002705 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2706 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2707 // Pattern complexity = 6 cost = 1 size = 0
2708
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002709 unsigned Opc = Subtarget->isThumb() ?
David Goodwin27303cd2009-06-30 18:04:13 +00002710 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002711 SDValue Chain = N->getOperand(0);
2712 SDValue N1 = N->getOperand(1);
2713 SDValue N2 = N->getOperand(2);
2714 SDValue N3 = N->getOperand(3);
2715 SDValue InFlag = N->getOperand(4);
Evan Cheng7e90b112007-07-05 07:15:27 +00002716 assert(N1.getOpcode() == ISD::BasicBlock);
2717 assert(N2.getOpcode() == ISD::Constant);
2718 assert(N3.getOpcode() == ISD::Register);
2719
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002720 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmaneffb8942008-09-12 16:56:44 +00002721 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson9f944592009-08-11 20:47:22 +00002722 MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002723 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
Dan Gohman32f71d72009-09-25 18:54:59 +00002724 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
Michael Liaob53d8962013-04-19 22:22:57 +00002725 MVT::Glue, Ops);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002726 Chain = SDValue(ResNode, 0);
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002727 if (N->getNumValues() == 2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002728 InFlag = SDValue(ResNode, 1);
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002729 ReplaceUses(SDValue(N, 1), InFlag);
Chris Lattnere99faac2008-02-03 03:20:59 +00002730 }
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002731 ReplaceUses(SDValue(N, 0),
Evan Cheng82adca82009-11-19 08:16:50 +00002732 SDValue(Chain.getNode(), Chain.getResNo()));
Craig Topper062a2ba2014-04-25 05:30:21 +00002733 return nullptr;
Evan Cheng7e90b112007-07-05 07:15:27 +00002734 }
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002735 case ARMISD::VZIP: {
2736 unsigned Opc = 0;
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002737 EVT VT = N->getValueType(0);
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002738 switch (VT.getSimpleVT().SimpleTy) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002739 default: return nullptr;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002740 case MVT::v8i8: Opc = ARM::VZIPd8; break;
2741 case MVT::v4i16: Opc = ARM::VZIPd16; break;
2742 case MVT::v2f32:
Jim Grosbach4640c812012-04-11 16:53:25 +00002743 // vzip.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
2744 case MVT::v2i32: Opc = ARM::VTRNd32; break;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002745 case MVT::v16i8: Opc = ARM::VZIPq8; break;
2746 case MVT::v8i16: Opc = ARM::VZIPq16; break;
2747 case MVT::v4f32:
2748 case MVT::v4i32: Opc = ARM::VZIPq32; break;
2749 }
Evan Cheng3da64f762010-04-16 05:46:06 +00002750 SDValue Pred = getAL(CurDAG);
Evan Chenga33fc862009-11-21 06:21:52 +00002751 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2752 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
Michael Liaob53d8962013-04-19 22:22:57 +00002753 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops);
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002754 }
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002755 case ARMISD::VUZP: {
2756 unsigned Opc = 0;
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002757 EVT VT = N->getValueType(0);
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002758 switch (VT.getSimpleVT().SimpleTy) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002759 default: return nullptr;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002760 case MVT::v8i8: Opc = ARM::VUZPd8; break;
2761 case MVT::v4i16: Opc = ARM::VUZPd16; break;
2762 case MVT::v2f32:
Jim Grosbach6e536de2012-04-11 17:40:18 +00002763 // vuzp.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
2764 case MVT::v2i32: Opc = ARM::VTRNd32; break;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002765 case MVT::v16i8: Opc = ARM::VUZPq8; break;
2766 case MVT::v8i16: Opc = ARM::VUZPq16; break;
2767 case MVT::v4f32:
2768 case MVT::v4i32: Opc = ARM::VUZPq32; break;
2769 }
Evan Cheng3da64f762010-04-16 05:46:06 +00002770 SDValue Pred = getAL(CurDAG);
Evan Chenga33fc862009-11-21 06:21:52 +00002771 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2772 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
Michael Liaob53d8962013-04-19 22:22:57 +00002773 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops);
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002774 }
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002775 case ARMISD::VTRN: {
2776 unsigned Opc = 0;
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002777 EVT VT = N->getValueType(0);
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002778 switch (VT.getSimpleVT().SimpleTy) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002779 default: return nullptr;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002780 case MVT::v8i8: Opc = ARM::VTRNd8; break;
2781 case MVT::v4i16: Opc = ARM::VTRNd16; break;
2782 case MVT::v2f32:
2783 case MVT::v2i32: Opc = ARM::VTRNd32; break;
2784 case MVT::v16i8: Opc = ARM::VTRNq8; break;
2785 case MVT::v8i16: Opc = ARM::VTRNq16; break;
2786 case MVT::v4f32:
2787 case MVT::v4i32: Opc = ARM::VTRNq32; break;
2788 }
Evan Cheng3da64f762010-04-16 05:46:06 +00002789 SDValue Pred = getAL(CurDAG);
Evan Chenga33fc862009-11-21 06:21:52 +00002790 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2791 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
Michael Liaob53d8962013-04-19 22:22:57 +00002792 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops);
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002793 }
Bob Wilsond8a9a042010-06-04 00:04:02 +00002794 case ARMISD::BUILD_VECTOR: {
2795 EVT VecVT = N->getValueType(0);
2796 EVT EltVT = VecVT.getVectorElementType();
2797 unsigned NumElts = VecVT.getVectorNumElements();
Duncan Sands14627772010-11-03 12:17:33 +00002798 if (EltVT == MVT::f64) {
Bob Wilsond8a9a042010-06-04 00:04:02 +00002799 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
Weiming Zhao95782222012-11-17 00:23:35 +00002800 return createDRegPairNode(VecVT, N->getOperand(0), N->getOperand(1));
Bob Wilsond8a9a042010-06-04 00:04:02 +00002801 }
Duncan Sands14627772010-11-03 12:17:33 +00002802 assert(EltVT == MVT::f32 && "unexpected type for BUILD_VECTOR");
Bob Wilsond8a9a042010-06-04 00:04:02 +00002803 if (NumElts == 2)
Weiming Zhao95782222012-11-17 00:23:35 +00002804 return createSRegPairNode(VecVT, N->getOperand(0), N->getOperand(1));
Bob Wilsond8a9a042010-06-04 00:04:02 +00002805 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
Weiming Zhao95782222012-11-17 00:23:35 +00002806 return createQuadSRegsNode(VecVT, N->getOperand(0), N->getOperand(1),
Bob Wilsond8a9a042010-06-04 00:04:02 +00002807 N->getOperand(2), N->getOperand(3));
2808 }
Bob Wilsone0636a72009-08-26 17:39:53 +00002809
Bob Wilson2d790df2010-11-28 06:51:26 +00002810 case ARMISD::VLD2DUP: {
Craig Topper01736f82012-05-24 05:17:00 +00002811 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8, ARM::VLD2DUPd16,
2812 ARM::VLD2DUPd32 };
Bob Wilson06fce872011-02-07 17:43:21 +00002813 return SelectVLDDup(N, false, 2, Opcodes);
Bob Wilson2d790df2010-11-28 06:51:26 +00002814 }
2815
Bob Wilson77ab1652010-11-29 19:35:29 +00002816 case ARMISD::VLD3DUP: {
Craig Topper01736f82012-05-24 05:17:00 +00002817 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo,
2818 ARM::VLD3DUPd16Pseudo,
2819 ARM::VLD3DUPd32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00002820 return SelectVLDDup(N, false, 3, Opcodes);
Bob Wilson77ab1652010-11-29 19:35:29 +00002821 }
2822
Bob Wilson431ac4ef2010-11-30 00:00:35 +00002823 case ARMISD::VLD4DUP: {
Craig Topper01736f82012-05-24 05:17:00 +00002824 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo,
2825 ARM::VLD4DUPd16Pseudo,
2826 ARM::VLD4DUPd32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00002827 return SelectVLDDup(N, false, 4, Opcodes);
2828 }
2829
2830 case ARMISD::VLD2DUP_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002831 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8wb_fixed,
2832 ARM::VLD2DUPd16wb_fixed,
2833 ARM::VLD2DUPd32wb_fixed };
Bob Wilson06fce872011-02-07 17:43:21 +00002834 return SelectVLDDup(N, true, 2, Opcodes);
2835 }
2836
2837 case ARMISD::VLD3DUP_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002838 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo_UPD,
2839 ARM::VLD3DUPd16Pseudo_UPD,
2840 ARM::VLD3DUPd32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002841 return SelectVLDDup(N, true, 3, Opcodes);
2842 }
2843
2844 case ARMISD::VLD4DUP_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002845 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo_UPD,
2846 ARM::VLD4DUPd16Pseudo_UPD,
2847 ARM::VLD4DUPd32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002848 return SelectVLDDup(N, true, 4, Opcodes);
2849 }
2850
2851 case ARMISD::VLD1_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002852 static const uint16_t DOpcodes[] = { ARM::VLD1d8wb_fixed,
2853 ARM::VLD1d16wb_fixed,
2854 ARM::VLD1d32wb_fixed,
2855 ARM::VLD1d64wb_fixed };
2856 static const uint16_t QOpcodes[] = { ARM::VLD1q8wb_fixed,
2857 ARM::VLD1q16wb_fixed,
2858 ARM::VLD1q32wb_fixed,
2859 ARM::VLD1q64wb_fixed };
Craig Topper062a2ba2014-04-25 05:30:21 +00002860 return SelectVLD(N, true, 1, DOpcodes, QOpcodes, nullptr);
Bob Wilson06fce872011-02-07 17:43:21 +00002861 }
2862
2863 case ARMISD::VLD2_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002864 static const uint16_t DOpcodes[] = { ARM::VLD2d8wb_fixed,
2865 ARM::VLD2d16wb_fixed,
2866 ARM::VLD2d32wb_fixed,
2867 ARM::VLD1q64wb_fixed};
2868 static const uint16_t QOpcodes[] = { ARM::VLD2q8PseudoWB_fixed,
2869 ARM::VLD2q16PseudoWB_fixed,
2870 ARM::VLD2q32PseudoWB_fixed };
Craig Topper062a2ba2014-04-25 05:30:21 +00002871 return SelectVLD(N, true, 2, DOpcodes, QOpcodes, nullptr);
Bob Wilson06fce872011-02-07 17:43:21 +00002872 }
2873
2874 case ARMISD::VLD3_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002875 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo_UPD,
2876 ARM::VLD3d16Pseudo_UPD,
2877 ARM::VLD3d32Pseudo_UPD,
Jiangning Liu4df23632014-01-16 09:16:13 +00002878 ARM::VLD1d64TPseudoWB_fixed};
Craig Topper01736f82012-05-24 05:17:00 +00002879 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
2880 ARM::VLD3q16Pseudo_UPD,
2881 ARM::VLD3q32Pseudo_UPD };
2882 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
2883 ARM::VLD3q16oddPseudo_UPD,
2884 ARM::VLD3q32oddPseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002885 return SelectVLD(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
2886 }
2887
2888 case ARMISD::VLD4_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002889 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo_UPD,
2890 ARM::VLD4d16Pseudo_UPD,
2891 ARM::VLD4d32Pseudo_UPD,
Jiangning Liu4df23632014-01-16 09:16:13 +00002892 ARM::VLD1d64QPseudoWB_fixed};
Craig Topper01736f82012-05-24 05:17:00 +00002893 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
2894 ARM::VLD4q16Pseudo_UPD,
2895 ARM::VLD4q32Pseudo_UPD };
2896 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
2897 ARM::VLD4q16oddPseudo_UPD,
2898 ARM::VLD4q32oddPseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002899 return SelectVLD(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
2900 }
2901
2902 case ARMISD::VLD2LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002903 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo_UPD,
2904 ARM::VLD2LNd16Pseudo_UPD,
2905 ARM::VLD2LNd32Pseudo_UPD };
2906 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo_UPD,
2907 ARM::VLD2LNq32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002908 return SelectVLDSTLane(N, true, true, 2, DOpcodes, QOpcodes);
2909 }
2910
2911 case ARMISD::VLD3LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002912 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo_UPD,
2913 ARM::VLD3LNd16Pseudo_UPD,
2914 ARM::VLD3LNd32Pseudo_UPD };
2915 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo_UPD,
2916 ARM::VLD3LNq32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002917 return SelectVLDSTLane(N, true, true, 3, DOpcodes, QOpcodes);
2918 }
2919
2920 case ARMISD::VLD4LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002921 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo_UPD,
2922 ARM::VLD4LNd16Pseudo_UPD,
2923 ARM::VLD4LNd32Pseudo_UPD };
2924 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo_UPD,
2925 ARM::VLD4LNq32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002926 return SelectVLDSTLane(N, true, true, 4, DOpcodes, QOpcodes);
2927 }
2928
2929 case ARMISD::VST1_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002930 static const uint16_t DOpcodes[] = { ARM::VST1d8wb_fixed,
2931 ARM::VST1d16wb_fixed,
2932 ARM::VST1d32wb_fixed,
2933 ARM::VST1d64wb_fixed };
2934 static const uint16_t QOpcodes[] = { ARM::VST1q8wb_fixed,
2935 ARM::VST1q16wb_fixed,
2936 ARM::VST1q32wb_fixed,
2937 ARM::VST1q64wb_fixed };
Craig Topper062a2ba2014-04-25 05:30:21 +00002938 return SelectVST(N, true, 1, DOpcodes, QOpcodes, nullptr);
Bob Wilson06fce872011-02-07 17:43:21 +00002939 }
2940
2941 case ARMISD::VST2_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002942 static const uint16_t DOpcodes[] = { ARM::VST2d8wb_fixed,
2943 ARM::VST2d16wb_fixed,
2944 ARM::VST2d32wb_fixed,
2945 ARM::VST1q64wb_fixed};
2946 static const uint16_t QOpcodes[] = { ARM::VST2q8PseudoWB_fixed,
2947 ARM::VST2q16PseudoWB_fixed,
2948 ARM::VST2q32PseudoWB_fixed };
Craig Topper062a2ba2014-04-25 05:30:21 +00002949 return SelectVST(N, true, 2, DOpcodes, QOpcodes, nullptr);
Bob Wilson06fce872011-02-07 17:43:21 +00002950 }
2951
2952 case ARMISD::VST3_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002953 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo_UPD,
2954 ARM::VST3d16Pseudo_UPD,
2955 ARM::VST3d32Pseudo_UPD,
2956 ARM::VST1d64TPseudoWB_fixed};
2957 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2958 ARM::VST3q16Pseudo_UPD,
2959 ARM::VST3q32Pseudo_UPD };
2960 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2961 ARM::VST3q16oddPseudo_UPD,
2962 ARM::VST3q32oddPseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002963 return SelectVST(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
2964 }
2965
2966 case ARMISD::VST4_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002967 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo_UPD,
2968 ARM::VST4d16Pseudo_UPD,
2969 ARM::VST4d32Pseudo_UPD,
2970 ARM::VST1d64QPseudoWB_fixed};
2971 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2972 ARM::VST4q16Pseudo_UPD,
2973 ARM::VST4q32Pseudo_UPD };
2974 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2975 ARM::VST4q16oddPseudo_UPD,
2976 ARM::VST4q32oddPseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002977 return SelectVST(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
2978 }
2979
2980 case ARMISD::VST2LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002981 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo_UPD,
2982 ARM::VST2LNd16Pseudo_UPD,
2983 ARM::VST2LNd32Pseudo_UPD };
2984 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo_UPD,
2985 ARM::VST2LNq32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002986 return SelectVLDSTLane(N, false, true, 2, DOpcodes, QOpcodes);
2987 }
2988
2989 case ARMISD::VST3LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002990 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo_UPD,
2991 ARM::VST3LNd16Pseudo_UPD,
2992 ARM::VST3LNd32Pseudo_UPD };
2993 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo_UPD,
2994 ARM::VST3LNq32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002995 return SelectVLDSTLane(N, false, true, 3, DOpcodes, QOpcodes);
2996 }
2997
2998 case ARMISD::VST4LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002999 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo_UPD,
3000 ARM::VST4LNd16Pseudo_UPD,
3001 ARM::VST4LNd32Pseudo_UPD };
3002 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo_UPD,
3003 ARM::VST4LNq32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00003004 return SelectVLDSTLane(N, false, true, 4, DOpcodes, QOpcodes);
Bob Wilson431ac4ef2010-11-30 00:00:35 +00003005 }
3006
Bob Wilsone0636a72009-08-26 17:39:53 +00003007 case ISD::INTRINSIC_VOID:
3008 case ISD::INTRINSIC_W_CHAIN: {
3009 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Bob Wilsone0636a72009-08-26 17:39:53 +00003010 switch (IntNo) {
3011 default:
Bob Wilsonf765e1f2010-05-06 16:05:26 +00003012 break;
Bob Wilsone0636a72009-08-26 17:39:53 +00003013
Tim Northover1ff5f292014-03-26 14:39:31 +00003014 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003015 case Intrinsic::arm_ldrexd: {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003016 SDLoc dl(N);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003017 SDValue Chain = N->getOperand(0);
Tim Northover1ff5f292014-03-26 14:39:31 +00003018 SDValue MemAddr = N->getOperand(2);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003019 bool isThumb = Subtarget->isThumb() && Subtarget->hasThumb2();
Tim Northover1ff5f292014-03-26 14:39:31 +00003020
3021 bool IsAcquire = IntNo == Intrinsic::arm_ldaexd;
3022 unsigned NewOpc = isThumb ? (IsAcquire ? ARM::t2LDAEXD : ARM::t2LDREXD)
3023 : (IsAcquire ? ARM::LDAEXD : ARM::LDREXD);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003024
3025 // arm_ldrexd returns a i64 value in {i32, i32}
3026 std::vector<EVT> ResTys;
Weiming Zhao8f56f882012-11-16 21:55:34 +00003027 if (isThumb) {
3028 ResTys.push_back(MVT::i32);
3029 ResTys.push_back(MVT::i32);
3030 } else
3031 ResTys.push_back(MVT::Untyped);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003032 ResTys.push_back(MVT::Other);
3033
Weiming Zhao8f56f882012-11-16 21:55:34 +00003034 // Place arguments in the right order.
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003035 SmallVector<SDValue, 7> Ops;
3036 Ops.push_back(MemAddr);
3037 Ops.push_back(getAL(CurDAG));
3038 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3039 Ops.push_back(Chain);
Michael Liaob53d8962013-04-19 22:22:57 +00003040 SDNode *Ld = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003041 // Transfer memoperands.
3042 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3043 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3044 cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1);
3045
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003046 // Remap uses.
Lang Hamesbe3d9712013-03-09 22:56:09 +00003047 SDValue OutChain = isThumb ? SDValue(Ld, 2) : SDValue(Ld, 1);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003048 if (!SDValue(N, 0).use_empty()) {
Weiming Zhao8f56f882012-11-16 21:55:34 +00003049 SDValue Result;
3050 if (isThumb)
3051 Result = SDValue(Ld, 0);
3052 else {
3053 SDValue SubRegIdx = CurDAG->getTargetConstant(ARM::gsub_0, MVT::i32);
3054 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Lang Hamesbe3d9712013-03-09 22:56:09 +00003055 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003056 Result = SDValue(ResNode,0);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003057 }
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003058 ReplaceUses(SDValue(N, 0), Result);
3059 }
3060 if (!SDValue(N, 1).use_empty()) {
Weiming Zhao8f56f882012-11-16 21:55:34 +00003061 SDValue Result;
3062 if (isThumb)
3063 Result = SDValue(Ld, 1);
3064 else {
3065 SDValue SubRegIdx = CurDAG->getTargetConstant(ARM::gsub_1, MVT::i32);
3066 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Lang Hamesbe3d9712013-03-09 22:56:09 +00003067 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003068 Result = SDValue(ResNode,0);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003069 }
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003070 ReplaceUses(SDValue(N, 1), Result);
3071 }
Lang Hamesbe3d9712013-03-09 22:56:09 +00003072 ReplaceUses(SDValue(N, 2), OutChain);
Craig Topper062a2ba2014-04-25 05:30:21 +00003073 return nullptr;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003074 }
Tim Northover1ff5f292014-03-26 14:39:31 +00003075 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003076 case Intrinsic::arm_strexd: {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003077 SDLoc dl(N);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003078 SDValue Chain = N->getOperand(0);
3079 SDValue Val0 = N->getOperand(2);
3080 SDValue Val1 = N->getOperand(3);
3081 SDValue MemAddr = N->getOperand(4);
3082
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003083 // Store exclusive double return a i32 value which is the return status
3084 // of the issued store.
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00003085 EVT ResTys[] = { MVT::i32, MVT::Other };
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003086
Weiming Zhao8f56f882012-11-16 21:55:34 +00003087 bool isThumb = Subtarget->isThumb() && Subtarget->hasThumb2();
3088 // Place arguments in the right order.
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003089 SmallVector<SDValue, 7> Ops;
Weiming Zhao8f56f882012-11-16 21:55:34 +00003090 if (isThumb) {
3091 Ops.push_back(Val0);
3092 Ops.push_back(Val1);
3093 } else
3094 // arm_strexd uses GPRPair.
3095 Ops.push_back(SDValue(createGPRPairNode(MVT::Untyped, Val0, Val1), 0));
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003096 Ops.push_back(MemAddr);
3097 Ops.push_back(getAL(CurDAG));
3098 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3099 Ops.push_back(Chain);
3100
Tim Northover1ff5f292014-03-26 14:39:31 +00003101 bool IsRelease = IntNo == Intrinsic::arm_stlexd;
3102 unsigned NewOpc = isThumb ? (IsRelease ? ARM::t2STLEXD : ARM::t2STREXD)
3103 : (IsRelease ? ARM::STLEXD : ARM::STREXD);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003104
Michael Liaob53d8962013-04-19 22:22:57 +00003105 SDNode *St = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003106 // Transfer memoperands.
3107 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3108 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3109 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
3110
3111 return St;
3112 }
3113
Bob Wilson340861d2010-03-23 05:25:43 +00003114 case Intrinsic::arm_neon_vld1: {
Craig Topper01736f82012-05-24 05:17:00 +00003115 static const uint16_t DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
3116 ARM::VLD1d32, ARM::VLD1d64 };
3117 static const uint16_t QOpcodes[] = { ARM::VLD1q8, ARM::VLD1q16,
3118 ARM::VLD1q32, ARM::VLD1q64};
Craig Topper062a2ba2014-04-25 05:30:21 +00003119 return SelectVLD(N, false, 1, DOpcodes, QOpcodes, nullptr);
Bob Wilson340861d2010-03-23 05:25:43 +00003120 }
3121
Bob Wilsone0636a72009-08-26 17:39:53 +00003122 case Intrinsic::arm_neon_vld2: {
Craig Topper01736f82012-05-24 05:17:00 +00003123 static const uint16_t DOpcodes[] = { ARM::VLD2d8, ARM::VLD2d16,
3124 ARM::VLD2d32, ARM::VLD1q64 };
3125 static const uint16_t QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
3126 ARM::VLD2q32Pseudo };
Craig Topper062a2ba2014-04-25 05:30:21 +00003127 return SelectVLD(N, false, 2, DOpcodes, QOpcodes, nullptr);
Bob Wilsone0636a72009-08-26 17:39:53 +00003128 }
3129
3130 case Intrinsic::arm_neon_vld3: {
Craig Topper01736f82012-05-24 05:17:00 +00003131 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo,
3132 ARM::VLD3d16Pseudo,
3133 ARM::VLD3d32Pseudo,
3134 ARM::VLD1d64TPseudo };
3135 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
3136 ARM::VLD3q16Pseudo_UPD,
3137 ARM::VLD3q32Pseudo_UPD };
3138 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo,
3139 ARM::VLD3q16oddPseudo,
3140 ARM::VLD3q32oddPseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003141 return SelectVLD(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilsone0636a72009-08-26 17:39:53 +00003142 }
3143
3144 case Intrinsic::arm_neon_vld4: {
Craig Topper01736f82012-05-24 05:17:00 +00003145 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo,
3146 ARM::VLD4d16Pseudo,
3147 ARM::VLD4d32Pseudo,
3148 ARM::VLD1d64QPseudo };
3149 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
3150 ARM::VLD4q16Pseudo_UPD,
3151 ARM::VLD4q32Pseudo_UPD };
3152 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo,
3153 ARM::VLD4q16oddPseudo,
3154 ARM::VLD4q32oddPseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003155 return SelectVLD(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilsone0636a72009-08-26 17:39:53 +00003156 }
3157
Bob Wilsonda9817c2009-09-01 04:26:28 +00003158 case Intrinsic::arm_neon_vld2lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003159 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo,
3160 ARM::VLD2LNd16Pseudo,
3161 ARM::VLD2LNd32Pseudo };
3162 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo,
3163 ARM::VLD2LNq32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003164 return SelectVLDSTLane(N, true, false, 2, DOpcodes, QOpcodes);
Bob Wilsonda9817c2009-09-01 04:26:28 +00003165 }
3166
3167 case Intrinsic::arm_neon_vld3lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003168 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo,
3169 ARM::VLD3LNd16Pseudo,
3170 ARM::VLD3LNd32Pseudo };
3171 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo,
3172 ARM::VLD3LNq32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003173 return SelectVLDSTLane(N, true, false, 3, DOpcodes, QOpcodes);
Bob Wilsonda9817c2009-09-01 04:26:28 +00003174 }
3175
3176 case Intrinsic::arm_neon_vld4lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003177 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo,
3178 ARM::VLD4LNd16Pseudo,
3179 ARM::VLD4LNd32Pseudo };
3180 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo,
3181 ARM::VLD4LNq32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003182 return SelectVLDSTLane(N, true, false, 4, DOpcodes, QOpcodes);
Bob Wilsonda9817c2009-09-01 04:26:28 +00003183 }
3184
Bob Wilsoncc0a2a72010-03-23 06:20:33 +00003185 case Intrinsic::arm_neon_vst1: {
Craig Topper01736f82012-05-24 05:17:00 +00003186 static const uint16_t DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
3187 ARM::VST1d32, ARM::VST1d64 };
3188 static const uint16_t QOpcodes[] = { ARM::VST1q8, ARM::VST1q16,
3189 ARM::VST1q32, ARM::VST1q64 };
Craig Topper062a2ba2014-04-25 05:30:21 +00003190 return SelectVST(N, false, 1, DOpcodes, QOpcodes, nullptr);
Bob Wilsoncc0a2a72010-03-23 06:20:33 +00003191 }
3192
Bob Wilsone0636a72009-08-26 17:39:53 +00003193 case Intrinsic::arm_neon_vst2: {
Craig Topper01736f82012-05-24 05:17:00 +00003194 static const uint16_t DOpcodes[] = { ARM::VST2d8, ARM::VST2d16,
3195 ARM::VST2d32, ARM::VST1q64 };
3196 static uint16_t QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
3197 ARM::VST2q32Pseudo };
Craig Topper062a2ba2014-04-25 05:30:21 +00003198 return SelectVST(N, false, 2, DOpcodes, QOpcodes, nullptr);
Bob Wilsone0636a72009-08-26 17:39:53 +00003199 }
3200
3201 case Intrinsic::arm_neon_vst3: {
Craig Topper01736f82012-05-24 05:17:00 +00003202 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo,
3203 ARM::VST3d16Pseudo,
3204 ARM::VST3d32Pseudo,
3205 ARM::VST1d64TPseudo };
3206 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
3207 ARM::VST3q16Pseudo_UPD,
3208 ARM::VST3q32Pseudo_UPD };
3209 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo,
3210 ARM::VST3q16oddPseudo,
3211 ARM::VST3q32oddPseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003212 return SelectVST(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilsone0636a72009-08-26 17:39:53 +00003213 }
3214
3215 case Intrinsic::arm_neon_vst4: {
Craig Topper01736f82012-05-24 05:17:00 +00003216 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo,
3217 ARM::VST4d16Pseudo,
3218 ARM::VST4d32Pseudo,
3219 ARM::VST1d64QPseudo };
3220 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
3221 ARM::VST4q16Pseudo_UPD,
3222 ARM::VST4q32Pseudo_UPD };
3223 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo,
3224 ARM::VST4q16oddPseudo,
3225 ARM::VST4q32oddPseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003226 return SelectVST(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilsone0636a72009-08-26 17:39:53 +00003227 }
Bob Wilsond7797752009-09-01 18:51:56 +00003228
3229 case Intrinsic::arm_neon_vst2lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003230 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo,
3231 ARM::VST2LNd16Pseudo,
3232 ARM::VST2LNd32Pseudo };
3233 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo,
3234 ARM::VST2LNq32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003235 return SelectVLDSTLane(N, false, false, 2, DOpcodes, QOpcodes);
Bob Wilsond7797752009-09-01 18:51:56 +00003236 }
3237
3238 case Intrinsic::arm_neon_vst3lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003239 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo,
3240 ARM::VST3LNd16Pseudo,
3241 ARM::VST3LNd32Pseudo };
3242 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo,
3243 ARM::VST3LNq32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003244 return SelectVLDSTLane(N, false, false, 3, DOpcodes, QOpcodes);
Bob Wilsond7797752009-09-01 18:51:56 +00003245 }
3246
3247 case Intrinsic::arm_neon_vst4lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003248 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo,
3249 ARM::VST4LNd16Pseudo,
3250 ARM::VST4LNd32Pseudo };
3251 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo,
3252 ARM::VST4LNq32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003253 return SelectVLDSTLane(N, false, false, 4, DOpcodes, QOpcodes);
Bob Wilsond7797752009-09-01 18:51:56 +00003254 }
Bob Wilsone0636a72009-08-26 17:39:53 +00003255 }
Bob Wilsonf765e1f2010-05-06 16:05:26 +00003256 break;
Bob Wilsone0636a72009-08-26 17:39:53 +00003257 }
Evan Chengd85631e2010-05-05 18:28:36 +00003258
Bob Wilson3ed511b2010-07-06 23:36:25 +00003259 case ISD::INTRINSIC_WO_CHAIN: {
3260 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3261 switch (IntNo) {
3262 default:
3263 break;
3264
3265 case Intrinsic::arm_neon_vtbl2:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003266 return SelectVTBL(N, false, 2, ARM::VTBL2);
Bob Wilson3ed511b2010-07-06 23:36:25 +00003267 case Intrinsic::arm_neon_vtbl3:
Bob Wilsonc597fd3b2010-09-13 23:55:10 +00003268 return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
Bob Wilson3ed511b2010-07-06 23:36:25 +00003269 case Intrinsic::arm_neon_vtbl4:
Bob Wilsonc597fd3b2010-09-13 23:55:10 +00003270 return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
Bob Wilson5bc8a792010-07-07 00:08:54 +00003271
3272 case Intrinsic::arm_neon_vtbx2:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003273 return SelectVTBL(N, true, 2, ARM::VTBX2);
Bob Wilson5bc8a792010-07-07 00:08:54 +00003274 case Intrinsic::arm_neon_vtbx3:
Bob Wilsonc597fd3b2010-09-13 23:55:10 +00003275 return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
Bob Wilson5bc8a792010-07-07 00:08:54 +00003276 case Intrinsic::arm_neon_vtbx4:
Bob Wilsonc597fd3b2010-09-13 23:55:10 +00003277 return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
Bob Wilson3ed511b2010-07-06 23:36:25 +00003278 }
3279 break;
3280 }
3281
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00003282 case ARMISD::VTBL1: {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003283 SDLoc dl(N);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00003284 EVT VT = N->getValueType(0);
3285 SmallVector<SDValue, 6> Ops;
3286
3287 Ops.push_back(N->getOperand(0));
3288 Ops.push_back(N->getOperand(1));
3289 Ops.push_back(getAL(CurDAG)); // Predicate
3290 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // Predicate Register
Michael Liaob53d8962013-04-19 22:22:57 +00003291 return CurDAG->getMachineNode(ARM::VTBL1, dl, VT, Ops);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00003292 }
3293 case ARMISD::VTBL2: {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003294 SDLoc dl(N);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00003295 EVT VT = N->getValueType(0);
3296
3297 // Form a REG_SEQUENCE to force register allocation.
3298 SDValue V0 = N->getOperand(0);
3299 SDValue V1 = N->getOperand(1);
Weiming Zhao95782222012-11-17 00:23:35 +00003300 SDValue RegSeq = SDValue(createDRegPairNode(MVT::v16i8, V0, V1), 0);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00003301
3302 SmallVector<SDValue, 6> Ops;
3303 Ops.push_back(RegSeq);
3304 Ops.push_back(N->getOperand(2));
3305 Ops.push_back(getAL(CurDAG)); // Predicate
3306 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // Predicate Register
Michael Liaob53d8962013-04-19 22:22:57 +00003307 return CurDAG->getMachineNode(ARM::VTBL2, dl, VT, Ops);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00003308 }
3309
Bob Wilsonf765e1f2010-05-06 16:05:26 +00003310 case ISD::CONCAT_VECTORS:
Evan Chengd85631e2010-05-05 18:28:36 +00003311 return SelectConcatVector(N);
3312 }
Evan Chengd5021732008-12-10 21:54:21 +00003313
Dan Gohmanea6f91f2010-01-05 01:24:18 +00003314 return SelectCode(N);
Evan Cheng10043e22007-01-19 07:51:42 +00003315}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00003316
Weiming Zhaoc5987002013-02-14 18:10:21 +00003317SDNode *ARMDAGToDAGISel::SelectInlineAsm(SDNode *N){
3318 std::vector<SDValue> AsmNodeOperands;
3319 unsigned Flag, Kind;
3320 bool Changed = false;
3321 unsigned NumOps = N->getNumOperands();
3322
Weiming Zhaoc5987002013-02-14 18:10:21 +00003323 // Normally, i64 data is bounded to two arbitrary GRPs for "%r" constraint.
3324 // However, some instrstions (e.g. ldrexd/strexd in ARM mode) require
3325 // (even/even+1) GPRs and use %n and %Hn to refer to the individual regs
3326 // respectively. Since there is no constraint to explicitly specify a
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003327 // reg pair, we use GPRPair reg class for "%r" for 64-bit data. For Thumb,
3328 // the 64-bit data may be referred by H, Q, R modifiers, so we still pack
3329 // them into a GPRPair.
Weiming Zhaoc5987002013-02-14 18:10:21 +00003330
Andrew Trickef9de2a2013-05-25 02:42:55 +00003331 SDLoc dl(N);
Craig Topper062a2ba2014-04-25 05:30:21 +00003332 SDValue Glue = N->getGluedNode() ? N->getOperand(NumOps-1)
3333 : SDValue(nullptr,0);
Weiming Zhaoc5987002013-02-14 18:10:21 +00003334
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003335 SmallVector<bool, 8> OpChanged;
Weiming Zhaoc5987002013-02-14 18:10:21 +00003336 // Glue node will be appended late.
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003337 for(unsigned i = 0, e = N->getGluedNode() ? NumOps - 1 : NumOps; i < e; ++i) {
Weiming Zhaoc5987002013-02-14 18:10:21 +00003338 SDValue op = N->getOperand(i);
3339 AsmNodeOperands.push_back(op);
3340
3341 if (i < InlineAsm::Op_FirstOperand)
3342 continue;
3343
3344 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(i))) {
3345 Flag = C->getZExtValue();
3346 Kind = InlineAsm::getKind(Flag);
3347 }
3348 else
3349 continue;
3350
Joey Gouly392cdad2013-07-08 19:52:51 +00003351 // Immediate operands to inline asm in the SelectionDAG are modeled with
3352 // two operands. The first is a constant of value InlineAsm::Kind_Imm, and
3353 // the second is a constant with the value of the immediate. If we get here
3354 // and we have a Kind_Imm, skip the next operand, and continue.
Joey Gouly606f3fb2013-07-05 10:19:40 +00003355 if (Kind == InlineAsm::Kind_Imm) {
3356 SDValue op = N->getOperand(++i);
3357 AsmNodeOperands.push_back(op);
3358 continue;
3359 }
3360
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003361 unsigned NumRegs = InlineAsm::getNumOperandRegisters(Flag);
3362 if (NumRegs)
3363 OpChanged.push_back(false);
3364
3365 unsigned DefIdx = 0;
3366 bool IsTiedToChangedOp = false;
3367 // If it's a use that is tied with a previous def, it has no
3368 // reg class constraint.
3369 if (Changed && InlineAsm::isUseOperandTiedToDef(Flag, DefIdx))
3370 IsTiedToChangedOp = OpChanged[DefIdx];
3371
Weiming Zhaoc5987002013-02-14 18:10:21 +00003372 if (Kind != InlineAsm::Kind_RegUse && Kind != InlineAsm::Kind_RegDef
3373 && Kind != InlineAsm::Kind_RegDefEarlyClobber)
3374 continue;
3375
Weiming Zhaoc5987002013-02-14 18:10:21 +00003376 unsigned RC;
3377 bool HasRC = InlineAsm::hasRegClassConstraint(Flag, RC);
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003378 if ((!IsTiedToChangedOp && (!HasRC || RC != ARM::GPRRegClassID))
3379 || NumRegs != 2)
Weiming Zhaoc5987002013-02-14 18:10:21 +00003380 continue;
3381
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003382 assert((i+2 < NumOps) && "Invalid number of operands in inline asm");
Weiming Zhaoc5987002013-02-14 18:10:21 +00003383 SDValue V0 = N->getOperand(i+1);
3384 SDValue V1 = N->getOperand(i+2);
3385 unsigned Reg0 = cast<RegisterSDNode>(V0)->getReg();
3386 unsigned Reg1 = cast<RegisterSDNode>(V1)->getReg();
3387 SDValue PairedReg;
3388 MachineRegisterInfo &MRI = MF->getRegInfo();
3389
3390 if (Kind == InlineAsm::Kind_RegDef ||
3391 Kind == InlineAsm::Kind_RegDefEarlyClobber) {
3392 // Replace the two GPRs with 1 GPRPair and copy values from GPRPair to
3393 // the original GPRs.
3394
3395 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
3396 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
3397 SDValue Chain = SDValue(N,0);
3398
3399 SDNode *GU = N->getGluedUser();
3400 SDValue RegCopy = CurDAG->getCopyFromReg(Chain, dl, GPVR, MVT::Untyped,
3401 Chain.getValue(1));
3402
3403 // Extract values from a GPRPair reg and copy to the original GPR reg.
3404 SDValue Sub0 = CurDAG->getTargetExtractSubreg(ARM::gsub_0, dl, MVT::i32,
3405 RegCopy);
3406 SDValue Sub1 = CurDAG->getTargetExtractSubreg(ARM::gsub_1, dl, MVT::i32,
3407 RegCopy);
3408 SDValue T0 = CurDAG->getCopyToReg(Sub0, dl, Reg0, Sub0,
3409 RegCopy.getValue(1));
3410 SDValue T1 = CurDAG->getCopyToReg(Sub1, dl, Reg1, Sub1, T0.getValue(1));
3411
3412 // Update the original glue user.
3413 std::vector<SDValue> Ops(GU->op_begin(), GU->op_end()-1);
3414 Ops.push_back(T1.getValue(1));
Craig Topper8c0b4d02014-04-28 05:57:50 +00003415 CurDAG->UpdateNodeOperands(GU, Ops);
Weiming Zhaoc5987002013-02-14 18:10:21 +00003416 GU = T1.getNode();
3417 }
3418 else {
3419 // For Kind == InlineAsm::Kind_RegUse, we first copy two GPRs into a
3420 // GPRPair and then pass the GPRPair to the inline asm.
3421 SDValue Chain = AsmNodeOperands[InlineAsm::Op_InputChain];
3422
3423 // As REG_SEQ doesn't take RegisterSDNode, we copy them first.
3424 SDValue T0 = CurDAG->getCopyFromReg(Chain, dl, Reg0, MVT::i32,
3425 Chain.getValue(1));
3426 SDValue T1 = CurDAG->getCopyFromReg(Chain, dl, Reg1, MVT::i32,
3427 T0.getValue(1));
3428 SDValue Pair = SDValue(createGPRPairNode(MVT::Untyped, T0, T1), 0);
3429
3430 // Copy REG_SEQ into a GPRPair-typed VR and replace the original two
3431 // i32 VRs of inline asm with it.
3432 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
3433 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
3434 Chain = CurDAG->getCopyToReg(T1, dl, GPVR, Pair, T1.getValue(1));
3435
3436 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
3437 Glue = Chain.getValue(1);
3438 }
3439
3440 Changed = true;
3441
3442 if(PairedReg.getNode()) {
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003443 OpChanged[OpChanged.size() -1 ] = true;
Weiming Zhaoc5987002013-02-14 18:10:21 +00003444 Flag = InlineAsm::getFlagWord(Kind, 1 /* RegNum*/);
Tim Northover55349a22013-08-18 18:06:03 +00003445 if (IsTiedToChangedOp)
3446 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, DefIdx);
3447 else
3448 Flag = InlineAsm::getFlagWordForRegClass(Flag, ARM::GPRPairRegClassID);
Weiming Zhaoc5987002013-02-14 18:10:21 +00003449 // Replace the current flag.
3450 AsmNodeOperands[AsmNodeOperands.size() -1] = CurDAG->getTargetConstant(
3451 Flag, MVT::i32);
3452 // Add the new register node and skip the original two GPRs.
3453 AsmNodeOperands.push_back(PairedReg);
3454 // Skip the next two GPRs.
3455 i += 2;
3456 }
3457 }
3458
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003459 if (Glue.getNode())
3460 AsmNodeOperands.push_back(Glue);
Weiming Zhaoc5987002013-02-14 18:10:21 +00003461 if (!Changed)
Craig Topper062a2ba2014-04-25 05:30:21 +00003462 return nullptr;
Weiming Zhaoc5987002013-02-14 18:10:21 +00003463
Andrew Trickef9de2a2013-05-25 02:42:55 +00003464 SDValue New = CurDAG->getNode(ISD::INLINEASM, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00003465 CurDAG->getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
Weiming Zhaoc5987002013-02-14 18:10:21 +00003466 New->setNodeId(-1);
3467 return New.getNode();
3468}
3469
3470
Bob Wilsona2c462b2009-05-19 05:53:42 +00003471bool ARMDAGToDAGISel::
3472SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
3473 std::vector<SDValue> &OutOps) {
3474 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
Bob Wilson3b515602009-10-13 20:50:28 +00003475 // Require the address to be in a register. That is safe for all ARM
3476 // variants and it is hard to do anything much smarter without knowing
3477 // how the operand is used.
3478 OutOps.push_back(Op);
Bob Wilsona2c462b2009-05-19 05:53:42 +00003479 return false;
3480}
3481
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00003482/// createARMISelDag - This pass converts a legalized DAG into a
3483/// ARM-specific DAG, ready for instruction scheduling.
3484///
Bob Wilson2dd957f2009-09-28 14:30:20 +00003485FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
3486 CodeGenOpt::Level OptLevel) {
3487 return new ARMDAGToDAGISel(TM, OptLevel);
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00003488}