blob: 8577c8af47d54c4dd728036aa2f87e39c3de0440 [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 {
Anton Korobeynikov99152f32009-06-26 21:28:53 +000063 ARMBaseTargetMachine &TM;
Evan Chengbc0d0ec2008-09-18 07:24:33 +000064
Evan Cheng10043e22007-01-19 07:51:42 +000065 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
66 /// make the right decision when generating code for different targets.
67 const ARMSubtarget *Subtarget;
68
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000069public:
Bob Wilson2dd957f2009-09-28 14:30:20 +000070 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm,
71 CodeGenOpt::Level OptLevel)
72 : SelectionDAGISel(tm, OptLevel), TM(tm),
Evan Cheng62c7b5b2010-12-05 22:04:16 +000073 Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000074 }
75
Craig Topper6bc27bf2014-03-10 02:09:33 +000076 const char *getPassName() const override {
Evan Cheng10043e22007-01-19 07:51:42 +000077 return "ARM Instruction Selection";
Anton Korobeynikov02bb33c2009-06-17 18:13:58 +000078 }
79
Craig Topper6bc27bf2014-03-10 02:09:33 +000080 void PreprocessISelDAG() override;
Evan Chengeae6d2c2012-12-19 20:16:09 +000081
Bob Wilson4facd962009-10-08 18:51:31 +000082 /// getI32Imm - Return a target constant of type i32 with the specified
83 /// value.
Anton Korobeynikov02bb33c2009-06-17 18:13:58 +000084 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000085 return CurDAG->getTargetConstant(Imm, MVT::i32);
Anton Korobeynikov02bb33c2009-06-17 18:13:58 +000086 }
87
Craig Topper6bc27bf2014-03-10 02:09:33 +000088 SDNode *Select(SDNode *N) override;
Evan Cheng5e73ff22010-02-15 19:41:07 +000089
Evan Cheng62c7b5b2010-12-05 22:04:16 +000090
91 bool hasNoVMLxHazardUse(SDNode *N) const;
Evan Cheng59bbc542010-10-27 23:41:30 +000092 bool isShifterOpProfitable(const SDValue &Shift,
93 ARM_AM::ShiftOpc ShOpcVal, unsigned ShAmt);
Owen Andersonb595ed02011-07-21 18:54:16 +000094 bool SelectRegShifterOperand(SDValue N, SDValue &A,
95 SDValue &B, SDValue &C,
96 bool CheckProfitability = true);
97 bool SelectImmShifterOperand(SDValue N, SDValue &A,
Owen Anderson04912702011-07-21 23:38:37 +000098 SDValue &B, bool CheckProfitability = true);
99 bool SelectShiftRegShifterOperand(SDValue N, SDValue &A,
Owen Anderson6d557452011-03-18 19:46:58 +0000100 SDValue &B, SDValue &C) {
101 // Don't apply the profitability check
Owen Anderson04912702011-07-21 23:38:37 +0000102 return SelectRegShifterOperand(N, A, B, C, false);
103 }
104 bool SelectShiftImmShifterOperand(SDValue N, SDValue &A,
105 SDValue &B) {
106 // Don't apply the profitability check
107 return SelectImmShifterOperand(N, A, B, false);
Owen Anderson6d557452011-03-18 19:46:58 +0000108 }
109
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000110 bool SelectAddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
111 bool SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset, SDValue &Opc);
112
Jim Grosbach08605202010-09-29 19:03:54 +0000113 AddrMode2Type SelectAddrMode2Worker(SDValue N, SDValue &Base,
114 SDValue &Offset, SDValue &Opc);
115 bool SelectAddrMode2Base(SDValue N, SDValue &Base, SDValue &Offset,
116 SDValue &Opc) {
117 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_BASE;
118 }
119
120 bool SelectAddrMode2ShOp(SDValue N, SDValue &Base, SDValue &Offset,
121 SDValue &Opc) {
122 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_SHOP;
123 }
124
125 bool SelectAddrMode2(SDValue N, SDValue &Base, SDValue &Offset,
126 SDValue &Opc) {
127 SelectAddrMode2Worker(N, Base, Offset, Opc);
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000128// return SelectAddrMode2ShOp(N, Base, Offset, Opc);
Jim Grosbach08605202010-09-29 19:03:54 +0000129 // This always matches one way or another.
130 return true;
131 }
132
Tim Northover42180442013-08-22 09:57:11 +0000133 bool SelectCMOVPred(SDValue N, SDValue &Pred, SDValue &Reg) {
134 const ConstantSDNode *CN = cast<ConstantSDNode>(N);
135 Pred = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
136 Reg = CurDAG->getRegister(ARM::CPSR, MVT::i32);
137 return true;
138 }
139
Owen Anderson2aedba62011-07-26 20:54:26 +0000140 bool SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
141 SDValue &Offset, SDValue &Opc);
142 bool SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000143 SDValue &Offset, SDValue &Opc);
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000144 bool SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
145 SDValue &Offset, SDValue &Opc);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +0000146 bool SelectAddrOffsetNone(SDValue N, SDValue &Base);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000147 bool SelectAddrMode3(SDValue N, SDValue &Base,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000148 SDValue &Offset, SDValue &Opc);
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000149 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000150 SDValue &Offset, SDValue &Opc);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000151 bool SelectAddrMode5(SDValue N, SDValue &Base,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000152 SDValue &Offset);
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000153 bool SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,SDValue &Align);
Bob Wilsone3ecd5f2011-02-25 06:42:42 +0000154 bool SelectAddrMode6Offset(SDNode *Op, SDValue N, SDValue &Offset);
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000155
Evan Chengdfce83c2011-01-17 08:03:18 +0000156 bool SelectAddrModePC(SDValue N, SDValue &Offset, SDValue &Label);
Evan Cheng10043e22007-01-19 07:51:42 +0000157
Bill Wendling092a7bd2010-12-14 03:36:38 +0000158 // Thumb Addressing Modes:
Chris Lattner0e023ea2010-09-21 20:31:19 +0000159 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
Bill Wendling092a7bd2010-12-14 03:36:38 +0000160 bool SelectThumbAddrModeRI(SDValue N, SDValue &Base, SDValue &Offset,
161 unsigned Scale);
162 bool SelectThumbAddrModeRI5S1(SDValue N, SDValue &Base, SDValue &Offset);
163 bool SelectThumbAddrModeRI5S2(SDValue N, SDValue &Base, SDValue &Offset);
164 bool SelectThumbAddrModeRI5S4(SDValue N, SDValue &Base, SDValue &Offset);
165 bool SelectThumbAddrModeImm5S(SDValue N, unsigned Scale, SDValue &Base,
166 SDValue &OffImm);
167 bool SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
168 SDValue &OffImm);
169 bool SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
170 SDValue &OffImm);
171 bool SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
172 SDValue &OffImm);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000173 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
Evan Cheng10043e22007-01-19 07:51:42 +0000174
Bill Wendling092a7bd2010-12-14 03:36:38 +0000175 // Thumb 2 Addressing Modes:
Chris Lattner0e023ea2010-09-21 20:31:19 +0000176 bool SelectT2ShifterOperandReg(SDValue N,
Evan Chengeab9ca72009-06-27 02:26:13 +0000177 SDValue &BaseReg, SDValue &Opc);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000178 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
179 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
Evan Chengb23b50d2009-06-29 07:51:04 +0000180 SDValue &OffImm);
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000181 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Cheng84c6cda2009-07-02 07:28:31 +0000182 SDValue &OffImm);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000183 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
Evan Chengb23b50d2009-06-29 07:51:04 +0000184 SDValue &OffReg, SDValue &ShImm);
Tim Northovera7ecd242013-07-16 09:46:55 +0000185 bool SelectT2AddrModeExclusive(SDValue N, SDValue &Base, SDValue &OffImm);
Evan Chengb23b50d2009-06-29 07:51:04 +0000186
Evan Cheng0fc80842010-11-12 22:42:47 +0000187 inline bool is_so_imm(unsigned Imm) const {
188 return ARM_AM::getSOImmVal(Imm) != -1;
189 }
190
191 inline bool is_so_imm_not(unsigned Imm) const {
192 return ARM_AM::getSOImmVal(~Imm) != -1;
193 }
194
195 inline bool is_t2_so_imm(unsigned Imm) const {
196 return ARM_AM::getT2SOImmVal(Imm) != -1;
197 }
198
199 inline bool is_t2_so_imm_not(unsigned Imm) const {
200 return ARM_AM::getT2SOImmVal(~Imm) != -1;
201 }
202
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000203 // Include the pieces autogenerated from the target description.
204#include "ARMGenDAGISel.inc"
Bob Wilsona2c462b2009-05-19 05:53:42 +0000205
206private:
Evan Cheng84c6cda2009-07-02 07:28:31 +0000207 /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
208 /// ARM.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000209 SDNode *SelectARMIndexedLoad(SDNode *N);
210 SDNode *SelectT2IndexedLoad(SDNode *N);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000211
Bob Wilson340861d2010-03-23 05:25:43 +0000212 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
213 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson12b47992009-10-14 17:28:52 +0000214 /// loads of D registers and even subregs and odd subregs of Q registers.
Bob Wilson340861d2010-03-23 05:25:43 +0000215 /// For NumVecs <= 2, QOpcodes1 is not used.
Bob Wilson06fce872011-02-07 17:43:21 +0000216 SDNode *SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +0000217 const uint16_t *DOpcodes,
218 const uint16_t *QOpcodes0, const uint16_t *QOpcodes1);
Bob Wilson12b47992009-10-14 17:28:52 +0000219
Bob Wilsonc350cdf2009-10-14 18:32:29 +0000220 /// SelectVST - Select NEON store intrinsics. NumVecs should
Bob Wilsoncc0a2a72010-03-23 06:20:33 +0000221 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilsonc350cdf2009-10-14 18:32:29 +0000222 /// stores of D registers and even subregs and odd subregs of Q registers.
Bob Wilsoncc0a2a72010-03-23 06:20:33 +0000223 /// For NumVecs <= 2, QOpcodes1 is not used.
Bob Wilson06fce872011-02-07 17:43:21 +0000224 SDNode *SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +0000225 const uint16_t *DOpcodes,
226 const uint16_t *QOpcodes0, const uint16_t *QOpcodes1);
Bob Wilsonc350cdf2009-10-14 18:32:29 +0000227
Bob Wilson93117bc2009-10-14 16:46:45 +0000228 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
Bob Wilson4145e3a2009-10-14 16:19:03 +0000229 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilsond5c57a52010-09-13 23:01:35 +0000230 /// load/store of D registers and Q registers.
Bob Wilson06fce872011-02-07 17:43:21 +0000231 SDNode *SelectVLDSTLane(SDNode *N, bool IsLoad,
232 bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +0000233 const uint16_t *DOpcodes, const uint16_t *QOpcodes);
Bob Wilson4145e3a2009-10-14 16:19:03 +0000234
Bob Wilson2d790df2010-11-28 06:51:26 +0000235 /// SelectVLDDup - Select NEON load-duplicate intrinsics. NumVecs
236 /// should be 2, 3 or 4. The opcode array specifies the instructions used
237 /// for loading D registers. (Q registers are not supported.)
Bob Wilson06fce872011-02-07 17:43:21 +0000238 SDNode *SelectVLDDup(SDNode *N, bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +0000239 const uint16_t *Opcodes);
Bob Wilson2d790df2010-11-28 06:51:26 +0000240
Bob Wilson5bc8a792010-07-07 00:08:54 +0000241 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2,
242 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be
243 /// generated to force the table registers to be consecutive.
244 SDNode *SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
Bob Wilson3ed511b2010-07-06 23:36:25 +0000245
Sandeep Patel7460e082009-10-13 20:25:58 +0000246 /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
Jim Grosbach825cb292010-04-22 23:24:18 +0000247 SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
Sandeep Patel423e42b2009-10-13 18:59:48 +0000248
Bill Wendlinga7d697e2011-10-10 22:59:55 +0000249 // Select special operations if node forms integer ABS pattern
250 SDNode *SelectABSOp(SDNode *N);
251
Weiming Zhaoc5987002013-02-14 18:10:21 +0000252 SDNode *SelectInlineAsm(SDNode *N);
253
Evan Chengd85631e2010-05-05 18:28:36 +0000254 SDNode *SelectConcatVector(SDNode *N);
255
Evan Chengd9c55362009-07-02 01:23:32 +0000256 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
257 /// inline asm expressions.
Craig Topper6bc27bf2014-03-10 02:09:33 +0000258 bool SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
259 std::vector<SDValue> &OutOps) override;
Bob Wilsone6b778d2009-10-06 22:01:59 +0000260
Weiming Zhao95782222012-11-17 00:23:35 +0000261 // Form pairs of consecutive R, S, D, or Q registers.
Weiming Zhao8f56f882012-11-16 21:55:34 +0000262 SDNode *createGPRPairNode(EVT VT, SDValue V0, SDValue V1);
Weiming Zhao95782222012-11-17 00:23:35 +0000263 SDNode *createSRegPairNode(EVT VT, SDValue V0, SDValue V1);
264 SDNode *createDRegPairNode(EVT VT, SDValue V0, SDValue V1);
265 SDNode *createQRegPairNode(EVT VT, SDValue V0, SDValue V1);
Evan Chengc2ae5f52010-05-10 17:34:18 +0000266
Bob Wilsond8a9a042010-06-04 00:04:02 +0000267 // Form sequences of 4 consecutive S, D, or Q registers.
Weiming Zhao95782222012-11-17 00:23:35 +0000268 SDNode *createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
269 SDNode *createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
270 SDNode *createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000271
272 // Get the alignment operand for a NEON VLD or VST instruction.
273 SDValue GetVLDSTAlign(SDValue Align, unsigned NumVecs, bool is64BitVector);
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000274};
Evan Cheng10043e22007-01-19 07:51:42 +0000275}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000276
Sandeep Patel423e42b2009-10-13 18:59:48 +0000277/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
278/// operand. If so Imm will receive the 32-bit value.
279static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
280 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
281 Imm = cast<ConstantSDNode>(N)->getZExtValue();
282 return true;
283 }
284 return false;
285}
286
287// isInt32Immediate - This method tests to see if a constant operand.
288// If so Imm will receive the 32 bit value.
289static bool isInt32Immediate(SDValue N, unsigned &Imm) {
290 return isInt32Immediate(N.getNode(), Imm);
291}
292
293// isOpcWithIntImmediate - This method tests to see if the node is a specific
294// opcode and that it has a immediate integer right operand.
295// If so Imm will receive the 32 bit value.
296static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
297 return N->getOpcode() == Opc &&
298 isInt32Immediate(N->getOperand(1).getNode(), Imm);
299}
300
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000301/// \brief Check whether a particular node is a constant value representable as
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000302/// (N * Scale) where (N in [\p RangeMin, \p RangeMax).
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000303///
304/// \param ScaledConstant [out] - On success, the pre-scaled constant value.
Jakob Stoklund Olesen2056d152011-09-23 22:10:33 +0000305static bool isScaledConstantInRange(SDValue Node, int Scale,
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000306 int RangeMin, int RangeMax,
307 int &ScaledConstant) {
Jakob Stoklund Olesen2056d152011-09-23 22:10:33 +0000308 assert(Scale > 0 && "Invalid scale!");
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000309
310 // Check that this is a constant.
311 const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Node);
312 if (!C)
313 return false;
314
315 ScaledConstant = (int) C->getZExtValue();
316 if ((ScaledConstant % Scale) != 0)
317 return false;
318
319 ScaledConstant /= Scale;
320 return ScaledConstant >= RangeMin && ScaledConstant < RangeMax;
321}
322
Evan Chengeae6d2c2012-12-19 20:16:09 +0000323void ARMDAGToDAGISel::PreprocessISelDAG() {
324 if (!Subtarget->hasV6T2Ops())
325 return;
326
327 bool isThumb2 = Subtarget->isThumb();
328 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
329 E = CurDAG->allnodes_end(); I != E; ) {
330 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
331
332 if (N->getOpcode() != ISD::ADD)
333 continue;
334
335 // Look for (add X1, (and (srl X2, c1), c2)) where c2 is constant with
336 // leading zeros, followed by consecutive set bits, followed by 1 or 2
337 // trailing zeros, e.g. 1020.
338 // Transform the expression to
339 // (add X1, (shl (and (srl X2, c1), (c2>>tz)), tz)) where tz is the number
340 // of trailing zeros of c2. The left shift would be folded as an shifter
341 // operand of 'add' and the 'and' and 'srl' would become a bits extraction
342 // node (UBFX).
343
344 SDValue N0 = N->getOperand(0);
345 SDValue N1 = N->getOperand(1);
346 unsigned And_imm = 0;
347 if (!isOpcWithIntImmediate(N1.getNode(), ISD::AND, And_imm)) {
348 if (isOpcWithIntImmediate(N0.getNode(), ISD::AND, And_imm))
349 std::swap(N0, N1);
350 }
351 if (!And_imm)
352 continue;
353
354 // Check if the AND mask is an immediate of the form: 000.....1111111100
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000355 unsigned TZ = countTrailingZeros(And_imm);
Evan Chengeae6d2c2012-12-19 20:16:09 +0000356 if (TZ != 1 && TZ != 2)
357 // Be conservative here. Shifter operands aren't always free. e.g. On
358 // Swift, left shifter operand of 1 / 2 for free but others are not.
359 // e.g.
360 // ubfx r3, r1, #16, #8
361 // ldr.w r3, [r0, r3, lsl #2]
362 // vs.
363 // mov.w r9, #1020
364 // and.w r2, r9, r1, lsr #14
365 // ldr r2, [r0, r2]
366 continue;
367 And_imm >>= TZ;
368 if (And_imm & (And_imm + 1))
369 continue;
370
371 // Look for (and (srl X, c1), c2).
372 SDValue Srl = N1.getOperand(0);
373 unsigned Srl_imm = 0;
374 if (!isOpcWithIntImmediate(Srl.getNode(), ISD::SRL, Srl_imm) ||
375 (Srl_imm <= 2))
376 continue;
377
378 // Make sure first operand is not a shifter operand which would prevent
379 // folding of the left shift.
380 SDValue CPTmp0;
381 SDValue CPTmp1;
382 SDValue CPTmp2;
383 if (isThumb2) {
384 if (SelectT2ShifterOperandReg(N0, CPTmp0, CPTmp1))
385 continue;
386 } else {
387 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1) ||
388 SelectRegShifterOperand(N0, CPTmp0, CPTmp1, CPTmp2))
389 continue;
390 }
391
392 // Now make the transformation.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000393 Srl = CurDAG->getNode(ISD::SRL, SDLoc(Srl), MVT::i32,
Evan Chengeae6d2c2012-12-19 20:16:09 +0000394 Srl.getOperand(0),
395 CurDAG->getConstant(Srl_imm+TZ, MVT::i32));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000396 N1 = CurDAG->getNode(ISD::AND, SDLoc(N1), MVT::i32,
Evan Chengeae6d2c2012-12-19 20:16:09 +0000397 Srl, CurDAG->getConstant(And_imm, MVT::i32));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000398 N1 = CurDAG->getNode(ISD::SHL, SDLoc(N1), MVT::i32,
Evan Chengeae6d2c2012-12-19 20:16:09 +0000399 N1, CurDAG->getConstant(TZ, MVT::i32));
400 CurDAG->UpdateNodeOperands(N, N0, N1);
Jim Grosbach1a597112014-04-03 23:43:18 +0000401 }
Evan Chengeae6d2c2012-12-19 20:16:09 +0000402}
403
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000404/// hasNoVMLxHazardUse - Return true if it's desirable to select a FP MLA / MLS
405/// node. VFP / NEON fp VMLA / VMLS instructions have special RAW hazards (at
406/// least on current ARM implementations) which should be avoidded.
407bool ARMDAGToDAGISel::hasNoVMLxHazardUse(SDNode *N) const {
408 if (OptLevel == CodeGenOpt::None)
409 return true;
410
411 if (!CheckVMLxHazard)
412 return true;
Bob Wilsone8a549c2012-09-29 21:43:49 +0000413
Tim Northover0feb91e2014-04-01 14:10:07 +0000414 if (!Subtarget->isCortexA7() && !Subtarget->isCortexA8() &&
415 !Subtarget->isCortexA9() && !Subtarget->isSwift())
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000416 return true;
417
418 if (!N->hasOneUse())
419 return false;
420
421 SDNode *Use = *N->use_begin();
422 if (Use->getOpcode() == ISD::CopyToReg)
423 return true;
424 if (Use->isMachineOpcode()) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000425 const ARMBaseInstrInfo *TII =
426 static_cast<const ARMBaseInstrInfo*>(TM.getInstrInfo());
427
Evan Cheng6cc775f2011-06-28 19:10:37 +0000428 const MCInstrDesc &MCID = TII->get(Use->getMachineOpcode());
429 if (MCID.mayStore())
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000430 return true;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000431 unsigned Opcode = MCID.getOpcode();
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000432 if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
433 return true;
434 // vmlx feeding into another vmlx. We actually want to unfold
435 // the use later in the MLxExpansion pass. e.g.
436 // vmla
437 // vmla (stall 8 cycles)
438 //
439 // vmul (5 cycles)
440 // vadd (5 cycles)
441 // vmla
442 // This adds up to about 18 - 19 cycles.
443 //
444 // vmla
445 // vmul (stall 4 cycles)
446 // vadd adds up to about 14 cycles.
447 return TII->isFpMLxInstruction(Opcode);
448 }
449
450 return false;
451}
Sandeep Patel423e42b2009-10-13 18:59:48 +0000452
Evan Cheng59bbc542010-10-27 23:41:30 +0000453bool ARMDAGToDAGISel::isShifterOpProfitable(const SDValue &Shift,
454 ARM_AM::ShiftOpc ShOpcVal,
455 unsigned ShAmt) {
Bob Wilsone8a549c2012-09-29 21:43:49 +0000456 if (!Subtarget->isLikeA9() && !Subtarget->isSwift())
Evan Cheng59bbc542010-10-27 23:41:30 +0000457 return true;
458 if (Shift.hasOneUse())
459 return true;
460 // R << 2 is free.
Bob Wilsone8a549c2012-09-29 21:43:49 +0000461 return ShOpcVal == ARM_AM::lsl &&
462 (ShAmt == 2 || (Subtarget->isSwift() && ShAmt == 1));
Evan Cheng59bbc542010-10-27 23:41:30 +0000463}
464
Owen Andersonb595ed02011-07-21 18:54:16 +0000465bool ARMDAGToDAGISel::SelectImmShifterOperand(SDValue N,
Evan Chengb23b50d2009-06-29 07:51:04 +0000466 SDValue &BaseReg,
Owen Anderson6d557452011-03-18 19:46:58 +0000467 SDValue &Opc,
468 bool CheckProfitability) {
Evan Cheng59069ec2010-07-30 23:33:54 +0000469 if (DisableShifterOp)
470 return false;
471
Evan Chenga20cde32011-07-20 23:34:39 +0000472 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
Evan Chengb23b50d2009-06-29 07:51:04 +0000473
474 // Don't match base register only case. That is matched to a separate
475 // lower complexity pattern with explicit register operand.
476 if (ShOpcVal == ARM_AM::no_shift) return false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000477
Evan Chengb23b50d2009-06-29 07:51:04 +0000478 BaseReg = N.getOperand(0);
479 unsigned ShImmVal = 0;
Owen Andersonb595ed02011-07-21 18:54:16 +0000480 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
481 if (!RHS) return false;
Owen Andersonb595ed02011-07-21 18:54:16 +0000482 ShImmVal = RHS->getZExtValue() & 31;
Evan Cheng59bbc542010-10-27 23:41:30 +0000483 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
484 MVT::i32);
485 return true;
486}
487
Owen Andersonb595ed02011-07-21 18:54:16 +0000488bool ARMDAGToDAGISel::SelectRegShifterOperand(SDValue N,
489 SDValue &BaseReg,
490 SDValue &ShReg,
491 SDValue &Opc,
492 bool CheckProfitability) {
493 if (DisableShifterOp)
494 return false;
495
496 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
497
498 // Don't match base register only case. That is matched to a separate
499 // lower complexity pattern with explicit register operand.
500 if (ShOpcVal == ARM_AM::no_shift) return false;
501
502 BaseReg = N.getOperand(0);
503 unsigned ShImmVal = 0;
504 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
505 if (RHS) return false;
506
507 ShReg = N.getOperand(1);
508 if (CheckProfitability && !isShifterOpProfitable(N, ShOpcVal, ShImmVal))
509 return false;
510 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
511 MVT::i32);
512 return true;
513}
514
515
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000516bool ARMDAGToDAGISel::SelectAddrModeImm12(SDValue N,
517 SDValue &Base,
518 SDValue &OffImm) {
519 // Match simple R + imm12 operands.
520
521 // Base only.
Chris Lattner46c01a32011-02-13 22:25:43 +0000522 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
523 !CurDAG->isBaseWithConstantOffset(N)) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000524 if (N.getOpcode() == ISD::FrameIndex) {
Chris Lattner46c01a32011-02-13 22:25:43 +0000525 // Match frame index.
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000526 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000527 Base = CurDAG->getTargetFrameIndex(FI,
528 getTargetLowering()->getPointerTy());
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000529 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
530 return true;
Chris Lattner46c01a32011-02-13 22:25:43 +0000531 }
Owen Anderson6d557452011-03-18 19:46:58 +0000532
Chris Lattner46c01a32011-02-13 22:25:43 +0000533 if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northover72360d22013-12-02 10:35:41 +0000534 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000535 Base = N.getOperand(0);
536 } else
537 Base = N;
538 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
539 return true;
540 }
541
542 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
543 int RHSC = (int)RHS->getZExtValue();
544 if (N.getOpcode() == ISD::SUB)
545 RHSC = -RHSC;
546
547 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
548 Base = N.getOperand(0);
549 if (Base.getOpcode() == ISD::FrameIndex) {
550 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000551 Base = CurDAG->getTargetFrameIndex(FI,
552 getTargetLowering()->getPointerTy());
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000553 }
554 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
555 return true;
556 }
557 }
558
559 // Base only.
560 Base = N;
561 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
562 return true;
563}
564
565
566
567bool ARMDAGToDAGISel::SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset,
568 SDValue &Opc) {
Evan Cheng59bbc542010-10-27 23:41:30 +0000569 if (N.getOpcode() == ISD::MUL &&
Bob Wilsone8a549c2012-09-29 21:43:49 +0000570 ((!Subtarget->isLikeA9() && !Subtarget->isSwift()) || N.hasOneUse())) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000571 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
572 // X * [3,5,9] -> X + X * [2,4,8] etc.
573 int RHSC = (int)RHS->getZExtValue();
574 if (RHSC & 1) {
575 RHSC = RHSC & ~1;
576 ARM_AM::AddrOpc AddSub = ARM_AM::add;
577 if (RHSC < 0) {
578 AddSub = ARM_AM::sub;
579 RHSC = - RHSC;
580 }
581 if (isPowerOf2_32(RHSC)) {
582 unsigned ShAmt = Log2_32(RHSC);
583 Base = Offset = N.getOperand(0);
584 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
585 ARM_AM::lsl),
586 MVT::i32);
587 return true;
588 }
589 }
590 }
591 }
592
Chris Lattner46c01a32011-02-13 22:25:43 +0000593 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
594 // ISD::OR that is equivalent to an ISD::ADD.
595 !CurDAG->isBaseWithConstantOffset(N))
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000596 return false;
597
598 // Leave simple R +/- imm12 operands for LDRi12
Chris Lattner46c01a32011-02-13 22:25:43 +0000599 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::OR) {
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000600 int RHSC;
601 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
602 -0x1000+1, 0x1000, RHSC)) // 12 bits.
603 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000604 }
605
606 // Otherwise this is R +/- [possibly shifted] R.
Chris Lattner46c01a32011-02-13 22:25:43 +0000607 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::SUB ? ARM_AM::sub:ARM_AM::add;
Evan Chenga20cde32011-07-20 23:34:39 +0000608 ARM_AM::ShiftOpc ShOpcVal =
609 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode());
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000610 unsigned ShAmt = 0;
611
612 Base = N.getOperand(0);
613 Offset = N.getOperand(1);
614
615 if (ShOpcVal != ARM_AM::no_shift) {
616 // Check to see if the RHS of the shift is a constant, if not, we can't fold
617 // it.
618 if (ConstantSDNode *Sh =
619 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
620 ShAmt = Sh->getZExtValue();
Evan Cheng59bbc542010-10-27 23:41:30 +0000621 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
622 Offset = N.getOperand(1).getOperand(0);
623 else {
624 ShAmt = 0;
625 ShOpcVal = ARM_AM::no_shift;
626 }
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000627 } else {
628 ShOpcVal = ARM_AM::no_shift;
629 }
630 }
631
632 // Try matching (R shl C) + (R).
Chris Lattner46c01a32011-02-13 22:25:43 +0000633 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
Bob Wilsone8a549c2012-09-29 21:43:49 +0000634 !(Subtarget->isLikeA9() || Subtarget->isSwift() ||
635 N.getOperand(0).hasOneUse())) {
Evan Chenga20cde32011-07-20 23:34:39 +0000636 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode());
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000637 if (ShOpcVal != ARM_AM::no_shift) {
638 // Check to see if the RHS of the shift is a constant, if not, we can't
639 // fold it.
640 if (ConstantSDNode *Sh =
641 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
642 ShAmt = Sh->getZExtValue();
Cameron Zwarich842f99a2011-10-05 23:39:02 +0000643 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) {
Evan Cheng59bbc542010-10-27 23:41:30 +0000644 Offset = N.getOperand(0).getOperand(0);
645 Base = N.getOperand(1);
646 } else {
647 ShAmt = 0;
648 ShOpcVal = ARM_AM::no_shift;
649 }
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000650 } else {
651 ShOpcVal = ARM_AM::no_shift;
652 }
653 }
654 }
655
656 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
657 MVT::i32);
658 return true;
659}
660
661
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000662//-----
663
Jim Grosbach08605202010-09-29 19:03:54 +0000664AddrMode2Type ARMDAGToDAGISel::SelectAddrMode2Worker(SDValue N,
665 SDValue &Base,
666 SDValue &Offset,
667 SDValue &Opc) {
Evan Cheng59bbc542010-10-27 23:41:30 +0000668 if (N.getOpcode() == ISD::MUL &&
Bob Wilsone8a549c2012-09-29 21:43:49 +0000669 (!(Subtarget->isLikeA9() || Subtarget->isSwift()) || N.hasOneUse())) {
Evan Cheng72a8bcf2007-03-13 21:05:54 +0000670 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
671 // X * [3,5,9] -> X + X * [2,4,8] etc.
Dan Gohmaneffb8942008-09-12 16:56:44 +0000672 int RHSC = (int)RHS->getZExtValue();
Evan Cheng72a8bcf2007-03-13 21:05:54 +0000673 if (RHSC & 1) {
674 RHSC = RHSC & ~1;
675 ARM_AM::AddrOpc AddSub = ARM_AM::add;
676 if (RHSC < 0) {
677 AddSub = ARM_AM::sub;
678 RHSC = - RHSC;
679 }
680 if (isPowerOf2_32(RHSC)) {
681 unsigned ShAmt = Log2_32(RHSC);
682 Base = Offset = N.getOperand(0);
683 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
684 ARM_AM::lsl),
Owen Anderson9f944592009-08-11 20:47:22 +0000685 MVT::i32);
Jim Grosbach08605202010-09-29 19:03:54 +0000686 return AM2_SHOP;
Evan Cheng72a8bcf2007-03-13 21:05:54 +0000687 }
688 }
689 }
690 }
691
Chris Lattner46c01a32011-02-13 22:25:43 +0000692 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
693 // ISD::OR that is equivalent to an ADD.
694 !CurDAG->isBaseWithConstantOffset(N)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000695 Base = N;
696 if (N.getOpcode() == ISD::FrameIndex) {
697 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000698 Base = CurDAG->getTargetFrameIndex(FI,
699 getTargetLowering()->getPointerTy());
Anton Korobeynikov25229082009-11-24 00:44:37 +0000700 } else if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northover72360d22013-12-02 10:35:41 +0000701 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress) {
Evan Cheng10043e22007-01-19 07:51:42 +0000702 Base = N.getOperand(0);
703 }
Owen Anderson9f944592009-08-11 20:47:22 +0000704 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000705 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
706 ARM_AM::no_shift),
Owen Anderson9f944592009-08-11 20:47:22 +0000707 MVT::i32);
Jim Grosbach08605202010-09-29 19:03:54 +0000708 return AM2_BASE;
Rafael Espindola708cb602006-11-08 17:07:32 +0000709 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000710
Evan Cheng10043e22007-01-19 07:51:42 +0000711 // Match simple R +/- imm12 operands.
Chris Lattner46c01a32011-02-13 22:25:43 +0000712 if (N.getOpcode() != ISD::SUB) {
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000713 int RHSC;
714 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
715 -0x1000+1, 0x1000, RHSC)) { // 12 bits.
716 Base = N.getOperand(0);
717 if (Base.getOpcode() == ISD::FrameIndex) {
718 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000719 Base = CurDAG->getTargetFrameIndex(FI,
720 getTargetLowering()->getPointerTy());
Rafael Espindola708cb602006-11-08 17:07:32 +0000721 }
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000722 Offset = CurDAG->getRegister(0, MVT::i32);
723
724 ARM_AM::AddrOpc AddSub = ARM_AM::add;
725 if (RHSC < 0) {
726 AddSub = ARM_AM::sub;
727 RHSC = - RHSC;
728 }
729 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
730 ARM_AM::no_shift),
731 MVT::i32);
732 return AM2_BASE;
Evan Cheng10043e22007-01-19 07:51:42 +0000733 }
Jim Grosbachc7b10f32010-09-29 17:32:29 +0000734 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000735
Bob Wilsone8a549c2012-09-29 21:43:49 +0000736 if ((Subtarget->isLikeA9() || Subtarget->isSwift()) && !N.hasOneUse()) {
Evan Cheng59bbc542010-10-27 23:41:30 +0000737 // Compute R +/- (R << N) and reuse it.
738 Base = N;
739 Offset = CurDAG->getRegister(0, MVT::i32);
740 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
741 ARM_AM::no_shift),
742 MVT::i32);
743 return AM2_BASE;
744 }
745
Johnny Chenb678a562009-10-27 17:25:15 +0000746 // Otherwise this is R +/- [possibly shifted] R.
Chris Lattner46c01a32011-02-13 22:25:43 +0000747 ARM_AM::AddrOpc AddSub = N.getOpcode() != ISD::SUB ? ARM_AM::add:ARM_AM::sub;
Evan Chenga20cde32011-07-20 23:34:39 +0000748 ARM_AM::ShiftOpc ShOpcVal =
749 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +0000750 unsigned ShAmt = 0;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000751
Evan Cheng10043e22007-01-19 07:51:42 +0000752 Base = N.getOperand(0);
753 Offset = N.getOperand(1);
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000754
Evan Cheng10043e22007-01-19 07:51:42 +0000755 if (ShOpcVal != ARM_AM::no_shift) {
756 // Check to see if the RHS of the shift is a constant, if not, we can't fold
757 // it.
758 if (ConstantSDNode *Sh =
759 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000760 ShAmt = Sh->getZExtValue();
Evan Cheng59bbc542010-10-27 23:41:30 +0000761 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
762 Offset = N.getOperand(1).getOperand(0);
763 else {
764 ShAmt = 0;
765 ShOpcVal = ARM_AM::no_shift;
766 }
Evan Cheng10043e22007-01-19 07:51:42 +0000767 } else {
768 ShOpcVal = ARM_AM::no_shift;
Rafael Espindola708cb602006-11-08 17:07:32 +0000769 }
770 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000771
Evan Cheng10043e22007-01-19 07:51:42 +0000772 // Try matching (R shl C) + (R).
Chris Lattner46c01a32011-02-13 22:25:43 +0000773 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
Bob Wilsone8a549c2012-09-29 21:43:49 +0000774 !(Subtarget->isLikeA9() || Subtarget->isSwift() ||
775 N.getOperand(0).hasOneUse())) {
Evan Chenga20cde32011-07-20 23:34:39 +0000776 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +0000777 if (ShOpcVal != ARM_AM::no_shift) {
778 // Check to see if the RHS of the shift is a constant, if not, we can't
779 // fold it.
780 if (ConstantSDNode *Sh =
781 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000782 ShAmt = Sh->getZExtValue();
Cameron Zwarich842f99a2011-10-05 23:39:02 +0000783 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) {
Evan Cheng59bbc542010-10-27 23:41:30 +0000784 Offset = N.getOperand(0).getOperand(0);
785 Base = N.getOperand(1);
786 } else {
787 ShAmt = 0;
788 ShOpcVal = ARM_AM::no_shift;
789 }
Evan Cheng10043e22007-01-19 07:51:42 +0000790 } else {
791 ShOpcVal = ARM_AM::no_shift;
792 }
793 }
794 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000795
Evan Cheng10043e22007-01-19 07:51:42 +0000796 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson9f944592009-08-11 20:47:22 +0000797 MVT::i32);
Jim Grosbach08605202010-09-29 19:03:54 +0000798 return AM2_SHOP;
Rafael Espindola708cb602006-11-08 17:07:32 +0000799}
800
Owen Anderson2aedba62011-07-26 20:54:26 +0000801bool ARMDAGToDAGISel::SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000802 SDValue &Offset, SDValue &Opc) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000803 unsigned Opcode = Op->getOpcode();
Evan Cheng10043e22007-01-19 07:51:42 +0000804 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
805 ? cast<LoadSDNode>(Op)->getAddressingMode()
806 : cast<StoreSDNode>(Op)->getAddressingMode();
807 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
808 ? ARM_AM::add : ARM_AM::sub;
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000809 int Val;
Owen Anderson2aedba62011-07-26 20:54:26 +0000810 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val))
811 return false;
Evan Cheng10043e22007-01-19 07:51:42 +0000812
813 Offset = N;
Evan Chenga20cde32011-07-20 23:34:39 +0000814 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +0000815 unsigned ShAmt = 0;
816 if (ShOpcVal != ARM_AM::no_shift) {
817 // Check to see if the RHS of the shift is a constant, if not, we can't fold
818 // it.
819 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000820 ShAmt = Sh->getZExtValue();
Evan Cheng59bbc542010-10-27 23:41:30 +0000821 if (isShifterOpProfitable(N, ShOpcVal, ShAmt))
822 Offset = N.getOperand(0);
823 else {
824 ShAmt = 0;
825 ShOpcVal = ARM_AM::no_shift;
826 }
Evan Cheng10043e22007-01-19 07:51:42 +0000827 } else {
828 ShOpcVal = ARM_AM::no_shift;
829 }
830 }
831
832 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson9f944592009-08-11 20:47:22 +0000833 MVT::i32);
Rafael Espindola19398ec2006-10-17 18:04:53 +0000834 return true;
835}
836
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000837bool ARMDAGToDAGISel::SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
838 SDValue &Offset, SDValue &Opc) {
Owen Anderson939cd212011-08-31 20:00:11 +0000839 unsigned Opcode = Op->getOpcode();
840 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
841 ? cast<LoadSDNode>(Op)->getAddressingMode()
842 : cast<StoreSDNode>(Op)->getAddressingMode();
843 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
844 ? ARM_AM::add : ARM_AM::sub;
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000845 int Val;
846 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
Owen Anderson939cd212011-08-31 20:00:11 +0000847 if (AddSub == ARM_AM::sub) Val *= -1;
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000848 Offset = CurDAG->getRegister(0, MVT::i32);
849 Opc = CurDAG->getTargetConstant(Val, MVT::i32);
850 return true;
851 }
852
853 return false;
854}
855
856
Owen Anderson2aedba62011-07-26 20:54:26 +0000857bool ARMDAGToDAGISel::SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
858 SDValue &Offset, SDValue &Opc) {
859 unsigned Opcode = Op->getOpcode();
860 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
861 ? cast<LoadSDNode>(Op)->getAddressingMode()
862 : cast<StoreSDNode>(Op)->getAddressingMode();
863 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
864 ? ARM_AM::add : ARM_AM::sub;
865 int Val;
866 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
867 Offset = CurDAG->getRegister(0, MVT::i32);
868 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
869 ARM_AM::no_shift),
870 MVT::i32);
871 return true;
872 }
873
874 return false;
875}
876
Jim Grosbachf0c95ca2011-08-05 20:35:44 +0000877bool ARMDAGToDAGISel::SelectAddrOffsetNone(SDValue N, SDValue &Base) {
878 Base = N;
879 return true;
880}
Evan Cheng10043e22007-01-19 07:51:42 +0000881
Chris Lattner0e023ea2010-09-21 20:31:19 +0000882bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000883 SDValue &Base, SDValue &Offset,
884 SDValue &Opc) {
Evan Cheng10043e22007-01-19 07:51:42 +0000885 if (N.getOpcode() == ISD::SUB) {
886 // X - C is canonicalize to X + -C, no need to handle it here.
887 Base = N.getOperand(0);
888 Offset = N.getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +0000889 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000890 return true;
891 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000892
Chris Lattner46c01a32011-02-13 22:25:43 +0000893 if (!CurDAG->isBaseWithConstantOffset(N)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000894 Base = N;
895 if (N.getOpcode() == ISD::FrameIndex) {
896 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000897 Base = CurDAG->getTargetFrameIndex(FI,
898 getTargetLowering()->getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +0000899 }
Owen Anderson9f944592009-08-11 20:47:22 +0000900 Offset = CurDAG->getRegister(0, MVT::i32);
901 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000902 return true;
903 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000904
Evan Cheng10043e22007-01-19 07:51:42 +0000905 // If the RHS is +/- imm8, fold into addr mode.
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000906 int RHSC;
907 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
908 -256 + 1, 256, RHSC)) { // 8 bits.
909 Base = N.getOperand(0);
910 if (Base.getOpcode() == ISD::FrameIndex) {
911 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000912 Base = CurDAG->getTargetFrameIndex(FI,
913 getTargetLowering()->getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +0000914 }
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000915 Offset = CurDAG->getRegister(0, MVT::i32);
916
917 ARM_AM::AddrOpc AddSub = ARM_AM::add;
918 if (RHSC < 0) {
919 AddSub = ARM_AM::sub;
Chris Lattner46c01a32011-02-13 22:25:43 +0000920 RHSC = -RHSC;
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000921 }
922 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
923 return true;
Evan Cheng10043e22007-01-19 07:51:42 +0000924 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000925
Evan Cheng10043e22007-01-19 07:51:42 +0000926 Base = N.getOperand(0);
927 Offset = N.getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +0000928 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000929 return true;
930}
931
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000932bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000933 SDValue &Offset, SDValue &Opc) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000934 unsigned Opcode = Op->getOpcode();
Evan Cheng10043e22007-01-19 07:51:42 +0000935 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
936 ? cast<LoadSDNode>(Op)->getAddressingMode()
937 : cast<StoreSDNode>(Op)->getAddressingMode();
938 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
939 ? ARM_AM::add : ARM_AM::sub;
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000940 int Val;
941 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 256, Val)) { // 12 bits.
942 Offset = CurDAG->getRegister(0, MVT::i32);
943 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
944 return true;
Evan Cheng10043e22007-01-19 07:51:42 +0000945 }
946
947 Offset = N;
Owen Anderson9f944592009-08-11 20:47:22 +0000948 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000949 return true;
950}
951
Jim Grosbachd37f0712010-10-21 19:38:40 +0000952bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000953 SDValue &Base, SDValue &Offset) {
Chris Lattner46c01a32011-02-13 22:25:43 +0000954 if (!CurDAG->isBaseWithConstantOffset(N)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000955 Base = N;
956 if (N.getOpcode() == ISD::FrameIndex) {
957 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000958 Base = CurDAG->getTargetFrameIndex(FI,
959 getTargetLowering()->getPointerTy());
Anton Korobeynikov25229082009-11-24 00:44:37 +0000960 } else if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northover72360d22013-12-02 10:35:41 +0000961 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress) {
Evan Cheng10043e22007-01-19 07:51:42 +0000962 Base = N.getOperand(0);
963 }
964 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson9f944592009-08-11 20:47:22 +0000965 MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000966 return true;
967 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000968
Evan Cheng10043e22007-01-19 07:51:42 +0000969 // If the RHS is +/- imm8, fold into addr mode.
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000970 int RHSC;
971 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4,
972 -256 + 1, 256, RHSC)) {
973 Base = N.getOperand(0);
974 if (Base.getOpcode() == ISD::FrameIndex) {
975 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000976 Base = CurDAG->getTargetFrameIndex(FI,
977 getTargetLowering()->getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +0000978 }
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000979
980 ARM_AM::AddrOpc AddSub = ARM_AM::add;
981 if (RHSC < 0) {
982 AddSub = ARM_AM::sub;
Chris Lattner46c01a32011-02-13 22:25:43 +0000983 RHSC = -RHSC;
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000984 }
985 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
986 MVT::i32);
987 return true;
Evan Cheng10043e22007-01-19 07:51:42 +0000988 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000989
Evan Cheng10043e22007-01-19 07:51:42 +0000990 Base = N;
991 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson9f944592009-08-11 20:47:22 +0000992 MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000993 return true;
994}
995
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000996bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,
997 SDValue &Align) {
Bob Wilsondeb35af2009-07-01 23:16:05 +0000998 Addr = N;
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000999
1000 unsigned Alignment = 0;
1001 if (LSBaseSDNode *LSN = dyn_cast<LSBaseSDNode>(Parent)) {
1002 // This case occurs only for VLD1-lane/dup and VST1-lane instructions.
1003 // The maximum alignment is equal to the memory size being referenced.
1004 unsigned LSNAlign = LSN->getAlignment();
1005 unsigned MemSize = LSN->getMemoryVT().getSizeInBits() / 8;
Jakob Stoklund Olesene5a6adc2011-10-27 22:39:16 +00001006 if (LSNAlign >= MemSize && MemSize > 1)
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001007 Alignment = MemSize;
1008 } else {
1009 // All other uses of addrmode6 are for intrinsics. For now just record
1010 // the raw alignment value; it will be refined later based on the legal
1011 // alignment operands for the intrinsic.
1012 Alignment = cast<MemIntrinsicSDNode>(Parent)->getAlignment();
1013 }
1014
1015 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
Bob Wilsondeb35af2009-07-01 23:16:05 +00001016 return true;
1017}
1018
Bob Wilsone3ecd5f2011-02-25 06:42:42 +00001019bool ARMDAGToDAGISel::SelectAddrMode6Offset(SDNode *Op, SDValue N,
1020 SDValue &Offset) {
1021 LSBaseSDNode *LdSt = cast<LSBaseSDNode>(Op);
1022 ISD::MemIndexedMode AM = LdSt->getAddressingMode();
1023 if (AM != ISD::POST_INC)
1024 return false;
1025 Offset = N;
1026 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N)) {
1027 if (NC->getZExtValue() * 8 == LdSt->getMemoryVT().getSizeInBits())
1028 Offset = CurDAG->getRegister(0, MVT::i32);
1029 }
1030 return true;
1031}
1032
Chris Lattner0e023ea2010-09-21 20:31:19 +00001033bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
Evan Cheng9a58aff2009-08-14 19:01:37 +00001034 SDValue &Offset, SDValue &Label) {
Evan Cheng10043e22007-01-19 07:51:42 +00001035 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
1036 Offset = N.getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001037 SDValue N1 = N.getOperand(1);
Evan Chengb8b0ad82011-01-20 08:34:58 +00001038 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
1039 MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00001040 return true;
1041 }
Bill Wendling092a7bd2010-12-14 03:36:38 +00001042
Evan Cheng10043e22007-01-19 07:51:42 +00001043 return false;
1044}
1045
Bill Wendling092a7bd2010-12-14 03:36:38 +00001046
1047//===----------------------------------------------------------------------===//
1048// Thumb Addressing Modes
1049//===----------------------------------------------------------------------===//
1050
Chris Lattner0e023ea2010-09-21 20:31:19 +00001051bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001052 SDValue &Base, SDValue &Offset){
Chris Lattner46c01a32011-02-13 22:25:43 +00001053 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N)) {
Evan Cheng0794c6a2009-07-11 07:08:13 +00001054 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
Dan Gohmanf1d83042010-06-18 14:22:04 +00001055 if (!NC || !NC->isNullValue())
Evan Cheng0794c6a2009-07-11 07:08:13 +00001056 return false;
1057
1058 Base = Offset = N;
Evan Chengc0b73662007-01-23 22:59:13 +00001059 return true;
1060 }
1061
Evan Cheng10043e22007-01-19 07:51:42 +00001062 Base = N.getOperand(0);
1063 Offset = N.getOperand(1);
1064 return true;
1065}
1066
Evan Cheng139edae2007-01-24 02:21:22 +00001067bool
Bill Wendling092a7bd2010-12-14 03:36:38 +00001068ARMDAGToDAGISel::SelectThumbAddrModeRI(SDValue N, SDValue &Base,
1069 SDValue &Offset, unsigned Scale) {
Evan Cheng139edae2007-01-24 02:21:22 +00001070 if (Scale == 4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001071 SDValue TmpBase, TmpOffImm;
Chris Lattner0e023ea2010-09-21 20:31:19 +00001072 if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
Evan Cheng139edae2007-01-24 02:21:22 +00001073 return false; // We want to select tLDRspi / tSTRspi instead.
Bill Wendling092a7bd2010-12-14 03:36:38 +00001074
Evan Cheng1526ba52007-01-24 08:53:17 +00001075 if (N.getOpcode() == ARMISD::Wrapper &&
1076 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
1077 return false; // We want to select tLDRpci instead.
Evan Cheng139edae2007-01-24 02:21:22 +00001078 }
1079
Chris Lattner46c01a32011-02-13 22:25:43 +00001080 if (!CurDAG->isBaseWithConstantOffset(N))
Bill Wendling832a5da2010-12-15 01:03:19 +00001081 return false;
Evan Cheng10043e22007-01-19 07:51:42 +00001082
Evan Cheng650d0672007-02-06 00:22:06 +00001083 // Thumb does not have [sp, r] address mode.
1084 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
1085 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
1086 if ((LHSR && LHSR->getReg() == ARM::SP) ||
Bill Wendling832a5da2010-12-15 01:03:19 +00001087 (RHSR && RHSR->getReg() == ARM::SP))
1088 return false;
Bill Wendling092a7bd2010-12-14 03:36:38 +00001089
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001090 // FIXME: Why do we explicitly check for a match here and then return false?
1091 // Presumably to allow something else to match, but shouldn't this be
1092 // documented?
1093 int RHSC;
1094 if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC))
1095 return false;
Bill Wendling092a7bd2010-12-14 03:36:38 +00001096
1097 Base = N.getOperand(0);
1098 Offset = N.getOperand(1);
1099 return true;
1100}
1101
1102bool
1103ARMDAGToDAGISel::SelectThumbAddrModeRI5S1(SDValue N,
1104 SDValue &Base,
1105 SDValue &Offset) {
1106 return SelectThumbAddrModeRI(N, Base, Offset, 1);
1107}
1108
1109bool
1110ARMDAGToDAGISel::SelectThumbAddrModeRI5S2(SDValue N,
1111 SDValue &Base,
1112 SDValue &Offset) {
1113 return SelectThumbAddrModeRI(N, Base, Offset, 2);
1114}
1115
1116bool
1117ARMDAGToDAGISel::SelectThumbAddrModeRI5S4(SDValue N,
1118 SDValue &Base,
1119 SDValue &Offset) {
1120 return SelectThumbAddrModeRI(N, Base, Offset, 4);
1121}
1122
1123bool
1124ARMDAGToDAGISel::SelectThumbAddrModeImm5S(SDValue N, unsigned Scale,
1125 SDValue &Base, SDValue &OffImm) {
1126 if (Scale == 4) {
1127 SDValue TmpBase, TmpOffImm;
1128 if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
1129 return false; // We want to select tLDRspi / tSTRspi instead.
1130
1131 if (N.getOpcode() == ARMISD::Wrapper &&
1132 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
1133 return false; // We want to select tLDRpci instead.
1134 }
1135
Chris Lattner46c01a32011-02-13 22:25:43 +00001136 if (!CurDAG->isBaseWithConstantOffset(N)) {
Bill Wendling092a7bd2010-12-14 03:36:38 +00001137 if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northover72360d22013-12-02 10:35:41 +00001138 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress) {
Bill Wendling092a7bd2010-12-14 03:36:38 +00001139 Base = N.getOperand(0);
1140 } else {
1141 Base = N;
1142 }
1143
Owen Anderson9f944592009-08-11 20:47:22 +00001144 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng650d0672007-02-06 00:22:06 +00001145 return true;
1146 }
1147
Bill Wendling832a5da2010-12-15 01:03:19 +00001148 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
1149 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
1150 if ((LHSR && LHSR->getReg() == ARM::SP) ||
1151 (RHSR && RHSR->getReg() == ARM::SP)) {
1152 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(N.getOperand(0));
1153 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
1154 unsigned LHSC = LHS ? LHS->getZExtValue() : 0;
1155 unsigned RHSC = RHS ? RHS->getZExtValue() : 0;
1156
1157 // Thumb does not have [sp, #imm5] address mode for non-zero imm5.
1158 if (LHSC != 0 || RHSC != 0) return false;
1159
1160 Base = N;
1161 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1162 return true;
1163 }
1164
Evan Cheng10043e22007-01-19 07:51:42 +00001165 // If the RHS is + imm5 * scale, fold into addr mode.
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001166 int RHSC;
1167 if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC)) {
1168 Base = N.getOperand(0);
1169 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1170 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00001171 }
1172
Evan Chengc0b73662007-01-23 22:59:13 +00001173 Base = N.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001174 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengc0b73662007-01-23 22:59:13 +00001175 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00001176}
1177
Bill Wendling092a7bd2010-12-14 03:36:38 +00001178bool
1179ARMDAGToDAGISel::SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
1180 SDValue &OffImm) {
1181 return SelectThumbAddrModeImm5S(N, 4, Base, OffImm);
Evan Cheng10043e22007-01-19 07:51:42 +00001182}
1183
Bill Wendling092a7bd2010-12-14 03:36:38 +00001184bool
1185ARMDAGToDAGISel::SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
1186 SDValue &OffImm) {
1187 return SelectThumbAddrModeImm5S(N, 2, Base, OffImm);
Evan Cheng10043e22007-01-19 07:51:42 +00001188}
1189
Bill Wendling092a7bd2010-12-14 03:36:38 +00001190bool
1191ARMDAGToDAGISel::SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
1192 SDValue &OffImm) {
1193 return SelectThumbAddrModeImm5S(N, 1, Base, OffImm);
Evan Cheng10043e22007-01-19 07:51:42 +00001194}
1195
Chris Lattner0e023ea2010-09-21 20:31:19 +00001196bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
1197 SDValue &Base, SDValue &OffImm) {
Evan Cheng10043e22007-01-19 07:51:42 +00001198 if (N.getOpcode() == ISD::FrameIndex) {
1199 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001200 Base = CurDAG->getTargetFrameIndex(FI,
1201 getTargetLowering()->getPointerTy());
Owen Anderson9f944592009-08-11 20:47:22 +00001202 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00001203 return true;
1204 }
Evan Cheng139edae2007-01-24 02:21:22 +00001205
Chris Lattner46c01a32011-02-13 22:25:43 +00001206 if (!CurDAG->isBaseWithConstantOffset(N))
Evan Cheng650d0672007-02-06 00:22:06 +00001207 return false;
1208
1209 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
Evan Chenga9740312007-02-06 09:11:20 +00001210 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
1211 (LHSR && LHSR->getReg() == ARM::SP)) {
Evan Cheng139edae2007-01-24 02:21:22 +00001212 // If the RHS is + imm8 * scale, fold into addr mode.
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001213 int RHSC;
1214 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4, 0, 256, RHSC)) {
1215 Base = N.getOperand(0);
1216 if (Base.getOpcode() == ISD::FrameIndex) {
1217 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001218 Base = CurDAG->getTargetFrameIndex(FI,
1219 getTargetLowering()->getPointerTy());
Evan Cheng139edae2007-01-24 02:21:22 +00001220 }
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001221 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1222 return true;
Evan Cheng139edae2007-01-24 02:21:22 +00001223 }
1224 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001225
Evan Cheng10043e22007-01-19 07:51:42 +00001226 return false;
1227}
1228
Bill Wendling092a7bd2010-12-14 03:36:38 +00001229
1230//===----------------------------------------------------------------------===//
1231// Thumb 2 Addressing Modes
1232//===----------------------------------------------------------------------===//
1233
1234
Chris Lattner0e023ea2010-09-21 20:31:19 +00001235bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDValue N, SDValue &BaseReg,
Evan Chengeab9ca72009-06-27 02:26:13 +00001236 SDValue &Opc) {
Evan Cheng59069ec2010-07-30 23:33:54 +00001237 if (DisableShifterOp)
1238 return false;
1239
Evan Chenga20cde32011-07-20 23:34:39 +00001240 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
Evan Chengeab9ca72009-06-27 02:26:13 +00001241
1242 // Don't match base register only case. That is matched to a separate
1243 // lower complexity pattern with explicit register operand.
1244 if (ShOpcVal == ARM_AM::no_shift) return false;
1245
1246 BaseReg = N.getOperand(0);
1247 unsigned ShImmVal = 0;
1248 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1249 ShImmVal = RHS->getZExtValue() & 31;
1250 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
1251 return true;
1252 }
1253
1254 return false;
1255}
1256
Chris Lattner0e023ea2010-09-21 20:31:19 +00001257bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
Evan Chengb23b50d2009-06-29 07:51:04 +00001258 SDValue &Base, SDValue &OffImm) {
1259 // Match simple R + imm12 operands.
David Goodwin802a0b52009-07-20 15:55:39 +00001260
Evan Cheng36064672009-08-11 08:52:18 +00001261 // Base only.
Chris Lattner46c01a32011-02-13 22:25:43 +00001262 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1263 !CurDAG->isBaseWithConstantOffset(N)) {
David Goodwin802a0b52009-07-20 15:55:39 +00001264 if (N.getOpcode() == ISD::FrameIndex) {
Chris Lattner46c01a32011-02-13 22:25:43 +00001265 // Match frame index.
David Goodwin802a0b52009-07-20 15:55:39 +00001266 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001267 Base = CurDAG->getTargetFrameIndex(FI,
1268 getTargetLowering()->getPointerTy());
Owen Anderson9f944592009-08-11 20:47:22 +00001269 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
David Goodwin802a0b52009-07-20 15:55:39 +00001270 return true;
Chris Lattner46c01a32011-02-13 22:25:43 +00001271 }
Owen Anderson6d557452011-03-18 19:46:58 +00001272
Chris Lattner46c01a32011-02-13 22:25:43 +00001273 if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northover72360d22013-12-02 10:35:41 +00001274 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress) {
Evan Cheng36064672009-08-11 08:52:18 +00001275 Base = N.getOperand(0);
1276 if (Base.getOpcode() == ISD::TargetConstantPool)
1277 return false; // We want to select t2LDRpci instead.
1278 } else
1279 Base = N;
Owen Anderson9f944592009-08-11 20:47:22 +00001280 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng36064672009-08-11 08:52:18 +00001281 return true;
David Goodwin802a0b52009-07-20 15:55:39 +00001282 }
Evan Chengb23b50d2009-06-29 07:51:04 +00001283
1284 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner0e023ea2010-09-21 20:31:19 +00001285 if (SelectT2AddrModeImm8(N, Base, OffImm))
Evan Cheng36064672009-08-11 08:52:18 +00001286 // Let t2LDRi8 handle (R - imm8).
1287 return false;
1288
Evan Chengb23b50d2009-06-29 07:51:04 +00001289 int RHSC = (int)RHS->getZExtValue();
David Goodwin79c079b2009-07-30 18:56:48 +00001290 if (N.getOpcode() == ISD::SUB)
1291 RHSC = -RHSC;
1292
1293 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
Evan Chengb23b50d2009-06-29 07:51:04 +00001294 Base = N.getOperand(0);
David Goodwin79c079b2009-07-30 18:56:48 +00001295 if (Base.getOpcode() == ISD::FrameIndex) {
1296 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001297 Base = CurDAG->getTargetFrameIndex(FI,
1298 getTargetLowering()->getPointerTy());
David Goodwin79c079b2009-07-30 18:56:48 +00001299 }
Owen Anderson9f944592009-08-11 20:47:22 +00001300 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Chengb23b50d2009-06-29 07:51:04 +00001301 return true;
1302 }
1303 }
1304
Evan Cheng36064672009-08-11 08:52:18 +00001305 // Base only.
1306 Base = N;
Owen Anderson9f944592009-08-11 20:47:22 +00001307 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng36064672009-08-11 08:52:18 +00001308 return true;
Evan Chengb23b50d2009-06-29 07:51:04 +00001309}
1310
Chris Lattner0e023ea2010-09-21 20:31:19 +00001311bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
Evan Chengb23b50d2009-06-29 07:51:04 +00001312 SDValue &Base, SDValue &OffImm) {
David Goodwin79c079b2009-07-30 18:56:48 +00001313 // Match simple R - imm8 operands.
Chris Lattner46c01a32011-02-13 22:25:43 +00001314 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1315 !CurDAG->isBaseWithConstantOffset(N))
1316 return false;
Owen Anderson6d557452011-03-18 19:46:58 +00001317
Chris Lattner46c01a32011-02-13 22:25:43 +00001318 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1319 int RHSC = (int)RHS->getSExtValue();
1320 if (N.getOpcode() == ISD::SUB)
1321 RHSC = -RHSC;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001322
Chris Lattner46c01a32011-02-13 22:25:43 +00001323 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
1324 Base = N.getOperand(0);
1325 if (Base.getOpcode() == ISD::FrameIndex) {
1326 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001327 Base = CurDAG->getTargetFrameIndex(FI,
1328 getTargetLowering()->getPointerTy());
Evan Chengb23b50d2009-06-29 07:51:04 +00001329 }
Chris Lattner46c01a32011-02-13 22:25:43 +00001330 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1331 return true;
Evan Chengb23b50d2009-06-29 07:51:04 +00001332 }
1333 }
1334
1335 return false;
1336}
1337
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001338bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Cheng84c6cda2009-07-02 07:28:31 +00001339 SDValue &OffImm){
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001340 unsigned Opcode = Op->getOpcode();
Evan Cheng84c6cda2009-07-02 07:28:31 +00001341 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
1342 ? cast<LoadSDNode>(Op)->getAddressingMode()
1343 : cast<StoreSDNode>(Op)->getAddressingMode();
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001344 int RHSC;
1345 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x100, RHSC)) { // 8 bits.
1346 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
1347 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
1348 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
1349 return true;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001350 }
1351
1352 return false;
1353}
1354
Chris Lattner0e023ea2010-09-21 20:31:19 +00001355bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
Evan Chengb23b50d2009-06-29 07:51:04 +00001356 SDValue &Base,
1357 SDValue &OffReg, SDValue &ShImm) {
Evan Cheng36064672009-08-11 08:52:18 +00001358 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
Chris Lattner46c01a32011-02-13 22:25:43 +00001359 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N))
Evan Cheng36064672009-08-11 08:52:18 +00001360 return false;
Evan Chengb23b50d2009-06-29 07:51:04 +00001361
Evan Cheng36064672009-08-11 08:52:18 +00001362 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
1363 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1364 int RHSC = (int)RHS->getZExtValue();
1365 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
1366 return false;
1367 else if (RHSC < 0 && RHSC >= -255) // 8 bits
David Goodwin79c079b2009-07-30 18:56:48 +00001368 return false;
1369 }
1370
Evan Chengb23b50d2009-06-29 07:51:04 +00001371 // Look for (R + R) or (R + (R << [1,2,3])).
1372 unsigned ShAmt = 0;
1373 Base = N.getOperand(0);
1374 OffReg = N.getOperand(1);
1375
1376 // Swap if it is ((R << c) + R).
Evan Chenga20cde32011-07-20 23:34:39 +00001377 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg.getOpcode());
Evan Chengb23b50d2009-06-29 07:51:04 +00001378 if (ShOpcVal != ARM_AM::lsl) {
Evan Chenga20cde32011-07-20 23:34:39 +00001379 ShOpcVal = ARM_AM::getShiftOpcForNode(Base.getOpcode());
Evan Chengb23b50d2009-06-29 07:51:04 +00001380 if (ShOpcVal == ARM_AM::lsl)
1381 std::swap(Base, OffReg);
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001382 }
1383
Evan Chengb23b50d2009-06-29 07:51:04 +00001384 if (ShOpcVal == ARM_AM::lsl) {
1385 // Check to see if the RHS of the shift is a constant, if not, we can't fold
1386 // it.
1387 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
1388 ShAmt = Sh->getZExtValue();
Evan Cheng59bbc542010-10-27 23:41:30 +00001389 if (ShAmt < 4 && isShifterOpProfitable(OffReg, ShOpcVal, ShAmt))
1390 OffReg = OffReg.getOperand(0);
1391 else {
Evan Chengb23b50d2009-06-29 07:51:04 +00001392 ShAmt = 0;
1393 ShOpcVal = ARM_AM::no_shift;
Evan Cheng59bbc542010-10-27 23:41:30 +00001394 }
Evan Chengb23b50d2009-06-29 07:51:04 +00001395 } else {
1396 ShOpcVal = ARM_AM::no_shift;
1397 }
David Goodwinf3912052009-07-15 15:50:19 +00001398 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001399
Owen Anderson9f944592009-08-11 20:47:22 +00001400 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
Evan Chengb23b50d2009-06-29 07:51:04 +00001401
1402 return true;
1403}
1404
Tim Northovera7ecd242013-07-16 09:46:55 +00001405bool ARMDAGToDAGISel::SelectT2AddrModeExclusive(SDValue N, SDValue &Base,
1406 SDValue &OffImm) {
Alp Tokercb402912014-01-24 17:20:08 +00001407 // This *must* succeed since it's used for the irreplaceable ldrex and strex
Tim Northovera7ecd242013-07-16 09:46:55 +00001408 // instructions.
1409 Base = N;
1410 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1411
1412 if (N.getOpcode() != ISD::ADD || !CurDAG->isBaseWithConstantOffset(N))
1413 return true;
1414
1415 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
1416 if (!RHS)
1417 return true;
1418
1419 uint32_t RHSC = (int)RHS->getZExtValue();
1420 if (RHSC > 1020 || RHSC % 4 != 0)
1421 return true;
1422
1423 Base = N.getOperand(0);
1424 if (Base.getOpcode() == ISD::FrameIndex) {
1425 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1426 Base = CurDAG->getTargetFrameIndex(FI, getTargetLowering()->getPointerTy());
1427 }
1428
1429 OffImm = CurDAG->getTargetConstant(RHSC / 4, MVT::i32);
1430 return true;
1431}
1432
Evan Chengb23b50d2009-06-29 07:51:04 +00001433//===--------------------------------------------------------------------===//
1434
Evan Cheng7e90b112007-07-05 07:15:27 +00001435/// getAL - Returns a ARMCC::AL immediate node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001436static inline SDValue getAL(SelectionDAG *CurDAG) {
Owen Anderson9f944592009-08-11 20:47:22 +00001437 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
Evan Cheng0f7cbe82007-05-15 01:29:07 +00001438}
1439
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001440SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
1441 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengd9c55362009-07-02 01:23:32 +00001442 ISD::MemIndexedMode AM = LD->getAddressingMode();
1443 if (AM == ISD::UNINDEXED)
Craig Topper062a2ba2014-04-25 05:30:21 +00001444 return nullptr;
Evan Chengd9c55362009-07-02 01:23:32 +00001445
Owen Anderson53aa7a92009-08-10 22:56:29 +00001446 EVT LoadedVT = LD->getMemoryVT();
Evan Chengd9c55362009-07-02 01:23:32 +00001447 SDValue Offset, AMOpc;
1448 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1449 unsigned Opcode = 0;
1450 bool Match = false;
Owen Anderson4d5c8f82011-08-29 20:16:50 +00001451 if (LoadedVT == MVT::i32 && isPre &&
1452 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
1453 Opcode = ARM::LDR_PRE_IMM;
1454 Match = true;
1455 } else if (LoadedVT == MVT::i32 && !isPre &&
Owen Anderson2aedba62011-07-26 20:54:26 +00001456 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
Owen Anderson4d5c8f82011-08-29 20:16:50 +00001457 Opcode = ARM::LDR_POST_IMM;
Evan Chengd9c55362009-07-02 01:23:32 +00001458 Match = true;
Owen Anderson2aedba62011-07-26 20:54:26 +00001459 } else if (LoadedVT == MVT::i32 &&
1460 SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
Owen Anderson16d33f32011-08-26 20:43:14 +00001461 Opcode = isPre ? ARM::LDR_PRE_REG : ARM::LDR_POST_REG;
Owen Anderson2aedba62011-07-26 20:54:26 +00001462 Match = true;
1463
Owen Anderson9f944592009-08-11 20:47:22 +00001464 } else if (LoadedVT == MVT::i16 &&
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001465 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengd9c55362009-07-02 01:23:32 +00001466 Match = true;
1467 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
1468 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
1469 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
Owen Anderson9f944592009-08-11 20:47:22 +00001470 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
Evan Chengd9c55362009-07-02 01:23:32 +00001471 if (LD->getExtensionType() == ISD::SEXTLOAD) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001472 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengd9c55362009-07-02 01:23:32 +00001473 Match = true;
1474 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
1475 }
1476 } else {
Owen Anderson4d5c8f82011-08-29 20:16:50 +00001477 if (isPre &&
1478 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengd9c55362009-07-02 01:23:32 +00001479 Match = true;
Owen Anderson4d5c8f82011-08-29 20:16:50 +00001480 Opcode = ARM::LDRB_PRE_IMM;
1481 } else if (!isPre &&
1482 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
1483 Match = true;
1484 Opcode = ARM::LDRB_POST_IMM;
Owen Anderson2aedba62011-07-26 20:54:26 +00001485 } else if (SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
1486 Match = true;
Owen Anderson16d33f32011-08-26 20:43:14 +00001487 Opcode = isPre ? ARM::LDRB_PRE_REG : ARM::LDRB_POST_REG;
Evan Chengd9c55362009-07-02 01:23:32 +00001488 }
1489 }
1490 }
1491
1492 if (Match) {
Owen Andersonfd60f602011-08-26 21:12:37 +00001493 if (Opcode == ARM::LDR_PRE_IMM || Opcode == ARM::LDRB_PRE_IMM) {
1494 SDValue Chain = LD->getChain();
1495 SDValue Base = LD->getBasePtr();
1496 SDValue Ops[]= { Base, AMOpc, getAL(CurDAG),
1497 CurDAG->getRegister(0, MVT::i32), Chain };
Andrew Trickef9de2a2013-05-25 02:42:55 +00001498 return CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32,
Michael Liaob53d8962013-04-19 22:22:57 +00001499 MVT::i32, MVT::Other, Ops);
Owen Andersonfd60f602011-08-26 21:12:37 +00001500 } else {
1501 SDValue Chain = LD->getChain();
1502 SDValue Base = LD->getBasePtr();
1503 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
1504 CurDAG->getRegister(0, MVT::i32), Chain };
Andrew Trickef9de2a2013-05-25 02:42:55 +00001505 return CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32,
Michael Liaob53d8962013-04-19 22:22:57 +00001506 MVT::i32, MVT::Other, Ops);
Owen Andersonfd60f602011-08-26 21:12:37 +00001507 }
Evan Chengd9c55362009-07-02 01:23:32 +00001508 }
1509
Craig Topper062a2ba2014-04-25 05:30:21 +00001510 return nullptr;
Evan Chengd9c55362009-07-02 01:23:32 +00001511}
1512
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001513SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
1514 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Cheng84c6cda2009-07-02 07:28:31 +00001515 ISD::MemIndexedMode AM = LD->getAddressingMode();
1516 if (AM == ISD::UNINDEXED)
Craig Topper062a2ba2014-04-25 05:30:21 +00001517 return nullptr;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001518
Owen Anderson53aa7a92009-08-10 22:56:29 +00001519 EVT LoadedVT = LD->getMemoryVT();
Evan Cheng8ecd7eb2009-07-02 23:16:11 +00001520 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001521 SDValue Offset;
1522 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1523 unsigned Opcode = 0;
1524 bool Match = false;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001525 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001526 switch (LoadedVT.getSimpleVT().SimpleTy) {
1527 case MVT::i32:
Evan Cheng84c6cda2009-07-02 07:28:31 +00001528 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
1529 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001530 case MVT::i16:
Evan Cheng8ecd7eb2009-07-02 23:16:11 +00001531 if (isSExtLd)
1532 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
1533 else
1534 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001535 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001536 case MVT::i8:
1537 case MVT::i1:
Evan Cheng8ecd7eb2009-07-02 23:16:11 +00001538 if (isSExtLd)
1539 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
1540 else
1541 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001542 break;
1543 default:
Craig Topper062a2ba2014-04-25 05:30:21 +00001544 return nullptr;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001545 }
1546 Match = true;
1547 }
1548
1549 if (Match) {
1550 SDValue Chain = LD->getChain();
1551 SDValue Base = LD->getBasePtr();
1552 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
Owen Anderson9f944592009-08-11 20:47:22 +00001553 CurDAG->getRegister(0, MVT::i32), Chain };
Andrew Trickef9de2a2013-05-25 02:42:55 +00001554 return CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
Michael Liaob53d8962013-04-19 22:22:57 +00001555 MVT::Other, Ops);
Evan Cheng84c6cda2009-07-02 07:28:31 +00001556 }
1557
Craig Topper062a2ba2014-04-25 05:30:21 +00001558 return nullptr;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001559}
1560
Weiming Zhao8f56f882012-11-16 21:55:34 +00001561/// \brief Form a GPRPair pseudo register from a pair of GPR regs.
1562SDNode *ARMDAGToDAGISel::createGPRPairNode(EVT VT, SDValue V0, SDValue V1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001563 SDLoc dl(V0.getNode());
Weiming Zhao8f56f882012-11-16 21:55:34 +00001564 SDValue RegClass =
1565 CurDAG->getTargetConstant(ARM::GPRPairRegClassID, MVT::i32);
1566 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::gsub_0, MVT::i32);
1567 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::gsub_1, MVT::i32);
1568 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
Michael Liaob53d8962013-04-19 22:22:57 +00001569 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Weiming Zhao8f56f882012-11-16 21:55:34 +00001570}
1571
Weiming Zhao95782222012-11-17 00:23:35 +00001572/// \brief Form a D register from a pair of S registers.
1573SDNode *ARMDAGToDAGISel::createSRegPairNode(EVT VT, SDValue V0, SDValue V1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001574 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001575 SDValue RegClass =
1576 CurDAG->getTargetConstant(ARM::DPR_VFP2RegClassID, MVT::i32);
Bob Wilsond8a9a042010-06-04 00:04:02 +00001577 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
1578 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001579 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
Michael Liaob53d8962013-04-19 22:22:57 +00001580 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Bob Wilsond8a9a042010-06-04 00:04:02 +00001581}
1582
Weiming Zhao95782222012-11-17 00:23:35 +00001583/// \brief Form a quad register from a pair of D registers.
1584SDNode *ARMDAGToDAGISel::createDRegPairNode(EVT VT, SDValue V0, SDValue V1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001585 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001586 SDValue RegClass = CurDAG->getTargetConstant(ARM::QPRRegClassID, MVT::i32);
Jakob Stoklund Olesen6c47d642010-05-24 16:54:32 +00001587 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1588 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001589 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
Michael Liaob53d8962013-04-19 22:22:57 +00001590 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Bob Wilsone6b778d2009-10-06 22:01:59 +00001591}
1592
Weiming Zhao95782222012-11-17 00:23:35 +00001593/// \brief Form 4 consecutive D registers from a pair of Q registers.
1594SDNode *ARMDAGToDAGISel::createQRegPairNode(EVT VT, SDValue V0, SDValue V1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001595 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001596 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, MVT::i32);
Jakob Stoklund Olesen6c47d642010-05-24 16:54:32 +00001597 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1598 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001599 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
Michael Liaob53d8962013-04-19 22:22:57 +00001600 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Evan Chengc2ae5f52010-05-10 17:34:18 +00001601}
1602
Weiming Zhao95782222012-11-17 00:23:35 +00001603/// \brief Form 4 consecutive S registers.
1604SDNode *ARMDAGToDAGISel::createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1,
Bob Wilsond8a9a042010-06-04 00:04:02 +00001605 SDValue V2, SDValue V3) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001606 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001607 SDValue RegClass =
1608 CurDAG->getTargetConstant(ARM::QPR_VFP2RegClassID, MVT::i32);
Bob Wilsond8a9a042010-06-04 00:04:02 +00001609 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
1610 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
1611 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, MVT::i32);
1612 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001613 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1614 V2, SubReg2, V3, SubReg3 };
Michael Liaob53d8962013-04-19 22:22:57 +00001615 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Bob Wilsond8a9a042010-06-04 00:04:02 +00001616}
1617
Weiming Zhao95782222012-11-17 00:23:35 +00001618/// \brief Form 4 consecutive D registers.
1619SDNode *ARMDAGToDAGISel::createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1,
Evan Chengc2ae5f52010-05-10 17:34:18 +00001620 SDValue V2, SDValue V3) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001621 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001622 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, MVT::i32);
Jakob Stoklund Olesen6c47d642010-05-24 16:54:32 +00001623 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1624 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
1625 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
1626 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001627 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1628 V2, SubReg2, V3, SubReg3 };
Michael Liaob53d8962013-04-19 22:22:57 +00001629 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Evan Chengc2ae5f52010-05-10 17:34:18 +00001630}
1631
Weiming Zhao95782222012-11-17 00:23:35 +00001632/// \brief Form 4 consecutive Q registers.
1633SDNode *ARMDAGToDAGISel::createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1,
Evan Cheng298e6b82010-05-16 03:27:48 +00001634 SDValue V2, SDValue V3) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001635 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001636 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQQQPRRegClassID, MVT::i32);
Jakob Stoklund Olesen6c47d642010-05-24 16:54:32 +00001637 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1638 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
1639 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, MVT::i32);
1640 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001641 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1642 V2, SubReg2, V3, SubReg3 };
Michael Liaob53d8962013-04-19 22:22:57 +00001643 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Evan Cheng298e6b82010-05-16 03:27:48 +00001644}
1645
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001646/// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1647/// of a NEON VLD or VST instruction. The supported values depend on the
1648/// number of registers being loaded.
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001649SDValue ARMDAGToDAGISel::GetVLDSTAlign(SDValue Align, unsigned NumVecs,
1650 bool is64BitVector) {
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001651 unsigned NumRegs = NumVecs;
1652 if (!is64BitVector && NumVecs < 3)
1653 NumRegs *= 2;
1654
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001655 unsigned Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001656 if (Alignment >= 32 && NumRegs == 4)
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001657 Alignment = 32;
1658 else if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1659 Alignment = 16;
1660 else if (Alignment >= 8)
1661 Alignment = 8;
1662 else
1663 Alignment = 0;
1664
1665 return CurDAG->getTargetConstant(Alignment, MVT::i32);
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001666}
1667
Jiangning Liu4df23632014-01-16 09:16:13 +00001668static bool isVLDfixed(unsigned Opc)
1669{
1670 switch (Opc) {
1671 default: return false;
1672 case ARM::VLD1d8wb_fixed : return true;
1673 case ARM::VLD1d16wb_fixed : return true;
1674 case ARM::VLD1d64Qwb_fixed : return true;
1675 case ARM::VLD1d32wb_fixed : return true;
1676 case ARM::VLD1d64wb_fixed : return true;
1677 case ARM::VLD1d64TPseudoWB_fixed : return true;
1678 case ARM::VLD1d64QPseudoWB_fixed : return true;
1679 case ARM::VLD1q8wb_fixed : return true;
1680 case ARM::VLD1q16wb_fixed : return true;
1681 case ARM::VLD1q32wb_fixed : return true;
1682 case ARM::VLD1q64wb_fixed : return true;
1683 case ARM::VLD2d8wb_fixed : return true;
1684 case ARM::VLD2d16wb_fixed : return true;
1685 case ARM::VLD2d32wb_fixed : return true;
1686 case ARM::VLD2q8PseudoWB_fixed : return true;
1687 case ARM::VLD2q16PseudoWB_fixed : return true;
1688 case ARM::VLD2q32PseudoWB_fixed : return true;
1689 case ARM::VLD2DUPd8wb_fixed : return true;
1690 case ARM::VLD2DUPd16wb_fixed : return true;
1691 case ARM::VLD2DUPd32wb_fixed : return true;
1692 }
1693}
1694
1695static bool isVSTfixed(unsigned Opc)
1696{
1697 switch (Opc) {
1698 default: return false;
1699 case ARM::VST1d8wb_fixed : return true;
1700 case ARM::VST1d16wb_fixed : return true;
1701 case ARM::VST1d32wb_fixed : return true;
1702 case ARM::VST1d64wb_fixed : return true;
Jim Grosbach1a597112014-04-03 23:43:18 +00001703 case ARM::VST1q8wb_fixed : return true;
1704 case ARM::VST1q16wb_fixed : return true;
1705 case ARM::VST1q32wb_fixed : return true;
1706 case ARM::VST1q64wb_fixed : return true;
Jiangning Liu4df23632014-01-16 09:16:13 +00001707 case ARM::VST1d64TPseudoWB_fixed : return true;
1708 case ARM::VST1d64QPseudoWB_fixed : return true;
1709 case ARM::VST2d8wb_fixed : return true;
1710 case ARM::VST2d16wb_fixed : return true;
1711 case ARM::VST2d32wb_fixed : return true;
1712 case ARM::VST2q8PseudoWB_fixed : return true;
1713 case ARM::VST2q16PseudoWB_fixed : return true;
1714 case ARM::VST2q32PseudoWB_fixed : return true;
1715 }
1716}
1717
Jim Grosbach2098cb12011-10-24 21:45:13 +00001718// Get the register stride update opcode of a VLD/VST instruction that
1719// is otherwise equivalent to the given fixed stride updating instruction.
1720static unsigned getVLDSTRegisterUpdateOpcode(unsigned Opc) {
Jiangning Liu4df23632014-01-16 09:16:13 +00001721 assert((isVLDfixed(Opc) || isVSTfixed(Opc))
1722 && "Incorrect fixed stride updating instruction.");
Jim Grosbach2098cb12011-10-24 21:45:13 +00001723 switch (Opc) {
1724 default: break;
1725 case ARM::VLD1d8wb_fixed: return ARM::VLD1d8wb_register;
1726 case ARM::VLD1d16wb_fixed: return ARM::VLD1d16wb_register;
1727 case ARM::VLD1d32wb_fixed: return ARM::VLD1d32wb_register;
1728 case ARM::VLD1d64wb_fixed: return ARM::VLD1d64wb_register;
1729 case ARM::VLD1q8wb_fixed: return ARM::VLD1q8wb_register;
1730 case ARM::VLD1q16wb_fixed: return ARM::VLD1q16wb_register;
1731 case ARM::VLD1q32wb_fixed: return ARM::VLD1q32wb_register;
1732 case ARM::VLD1q64wb_fixed: return ARM::VLD1q64wb_register;
Jiangning Liu4df23632014-01-16 09:16:13 +00001733 case ARM::VLD1d64Twb_fixed: return ARM::VLD1d64Twb_register;
1734 case ARM::VLD1d64Qwb_fixed: return ARM::VLD1d64Qwb_register;
1735 case ARM::VLD1d64TPseudoWB_fixed: return ARM::VLD1d64TPseudoWB_register;
1736 case ARM::VLD1d64QPseudoWB_fixed: return ARM::VLD1d64QPseudoWB_register;
Jim Grosbach05df4602011-10-31 21:50:31 +00001737
1738 case ARM::VST1d8wb_fixed: return ARM::VST1d8wb_register;
1739 case ARM::VST1d16wb_fixed: return ARM::VST1d16wb_register;
1740 case ARM::VST1d32wb_fixed: return ARM::VST1d32wb_register;
1741 case ARM::VST1d64wb_fixed: return ARM::VST1d64wb_register;
1742 case ARM::VST1q8wb_fixed: return ARM::VST1q8wb_register;
1743 case ARM::VST1q16wb_fixed: return ARM::VST1q16wb_register;
1744 case ARM::VST1q32wb_fixed: return ARM::VST1q32wb_register;
1745 case ARM::VST1q64wb_fixed: return ARM::VST1q64wb_register;
Jim Grosbach98d032f2011-11-29 22:38:04 +00001746 case ARM::VST1d64TPseudoWB_fixed: return ARM::VST1d64TPseudoWB_register;
Jim Grosbach5ee209c2011-11-29 22:58:48 +00001747 case ARM::VST1d64QPseudoWB_fixed: return ARM::VST1d64QPseudoWB_register;
Jim Grosbachd146a022011-12-09 21:28:25 +00001748
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001749 case ARM::VLD2d8wb_fixed: return ARM::VLD2d8wb_register;
1750 case ARM::VLD2d16wb_fixed: return ARM::VLD2d16wb_register;
1751 case ARM::VLD2d32wb_fixed: return ARM::VLD2d32wb_register;
Jim Grosbachd146a022011-12-09 21:28:25 +00001752 case ARM::VLD2q8PseudoWB_fixed: return ARM::VLD2q8PseudoWB_register;
1753 case ARM::VLD2q16PseudoWB_fixed: return ARM::VLD2q16PseudoWB_register;
1754 case ARM::VLD2q32PseudoWB_fixed: return ARM::VLD2q32PseudoWB_register;
1755
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001756 case ARM::VST2d8wb_fixed: return ARM::VST2d8wb_register;
1757 case ARM::VST2d16wb_fixed: return ARM::VST2d16wb_register;
1758 case ARM::VST2d32wb_fixed: return ARM::VST2d32wb_register;
Jim Grosbach88ac7612011-12-14 21:32:11 +00001759 case ARM::VST2q8PseudoWB_fixed: return ARM::VST2q8PseudoWB_register;
1760 case ARM::VST2q16PseudoWB_fixed: return ARM::VST2q16PseudoWB_register;
1761 case ARM::VST2q32PseudoWB_fixed: return ARM::VST2q32PseudoWB_register;
Jim Grosbachc80a2642011-12-21 19:40:55 +00001762
Jim Grosbach13a292c2012-03-06 22:01:44 +00001763 case ARM::VLD2DUPd8wb_fixed: return ARM::VLD2DUPd8wb_register;
1764 case ARM::VLD2DUPd16wb_fixed: return ARM::VLD2DUPd16wb_register;
1765 case ARM::VLD2DUPd32wb_fixed: return ARM::VLD2DUPd32wb_register;
Jim Grosbach2098cb12011-10-24 21:45:13 +00001766 }
1767 return Opc; // If not one we handle, return it unchanged.
1768}
1769
Bob Wilson06fce872011-02-07 17:43:21 +00001770SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +00001771 const uint16_t *DOpcodes,
1772 const uint16_t *QOpcodes0,
1773 const uint16_t *QOpcodes1) {
Bob Wilson340861d2010-03-23 05:25:43 +00001774 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00001775 SDLoc dl(N);
Bob Wilson12b47992009-10-14 17:28:52 +00001776
Bob Wilsonae08a732010-03-20 22:13:40 +00001777 SDValue MemAddr, Align;
Bob Wilson06fce872011-02-07 17:43:21 +00001778 unsigned AddrOpIdx = isUpdating ? 1 : 2;
1779 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
Craig Topper062a2ba2014-04-25 05:30:21 +00001780 return nullptr;
Bob Wilson12b47992009-10-14 17:28:52 +00001781
1782 SDValue Chain = N->getOperand(0);
1783 EVT VT = N->getValueType(0);
1784 bool is64BitVector = VT.is64BitVector();
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001785 Align = GetVLDSTAlign(Align, NumVecs, is64BitVector);
Bob Wilson9eeb8902010-09-23 21:43:54 +00001786
Bob Wilson12b47992009-10-14 17:28:52 +00001787 unsigned OpcodeIndex;
1788 switch (VT.getSimpleVT().SimpleTy) {
1789 default: llvm_unreachable("unhandled vld type");
1790 // Double-register operations:
1791 case MVT::v8i8: OpcodeIndex = 0; break;
1792 case MVT::v4i16: OpcodeIndex = 1; break;
1793 case MVT::v2f32:
1794 case MVT::v2i32: OpcodeIndex = 2; break;
1795 case MVT::v1i64: OpcodeIndex = 3; break;
1796 // Quad-register operations:
1797 case MVT::v16i8: OpcodeIndex = 0; break;
1798 case MVT::v8i16: OpcodeIndex = 1; break;
1799 case MVT::v4f32:
1800 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson340861d2010-03-23 05:25:43 +00001801 case MVT::v2i64: OpcodeIndex = 3;
Bob Wilsoncc0a2a72010-03-23 06:20:33 +00001802 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
Bob Wilson340861d2010-03-23 05:25:43 +00001803 break;
Bob Wilson12b47992009-10-14 17:28:52 +00001804 }
1805
Bob Wilson35fafca2010-09-03 18:16:02 +00001806 EVT ResTy;
1807 if (NumVecs == 1)
1808 ResTy = VT;
1809 else {
1810 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1811 if (!is64BitVector)
1812 ResTyElts *= 2;
1813 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1814 }
Bob Wilson06fce872011-02-07 17:43:21 +00001815 std::vector<EVT> ResTys;
1816 ResTys.push_back(ResTy);
1817 if (isUpdating)
1818 ResTys.push_back(MVT::i32);
1819 ResTys.push_back(MVT::Other);
Bob Wilson35fafca2010-09-03 18:16:02 +00001820
Evan Cheng3da64f762010-04-16 05:46:06 +00001821 SDValue Pred = getAL(CurDAG);
Bob Wilsonae08a732010-03-20 22:13:40 +00001822 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilson06fce872011-02-07 17:43:21 +00001823 SDNode *VLd;
1824 SmallVector<SDValue, 7> Ops;
Evan Cheng630063a2010-05-10 21:26:24 +00001825
Bob Wilson06fce872011-02-07 17:43:21 +00001826 // Double registers and VLD1/VLD2 quad registers are directly supported.
1827 if (is64BitVector || NumVecs <= 2) {
1828 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1829 QOpcodes0[OpcodeIndex]);
1830 Ops.push_back(MemAddr);
1831 Ops.push_back(Align);
1832 if (isUpdating) {
1833 SDValue Inc = N->getOperand(AddrOpIdx + 1);
Jim Grosbachd146a022011-12-09 21:28:25 +00001834 // FIXME: VLD1/VLD2 fixed increment doesn't need Reg0. Remove the reg0
Jim Grosbach2098cb12011-10-24 21:45:13 +00001835 // case entirely when the rest are updated to that form, too.
Jiangning Liu4df23632014-01-16 09:16:13 +00001836 if ((NumVecs <= 2) && !isa<ConstantSDNode>(Inc.getNode()))
Jim Grosbach2098cb12011-10-24 21:45:13 +00001837 Opc = getVLDSTRegisterUpdateOpcode(Opc);
Jiangning Liu4df23632014-01-16 09:16:13 +00001838 // FIXME: We use a VLD1 for v1i64 even if the pseudo says vld2/3/4, so
Jim Grosbach05df4602011-10-31 21:50:31 +00001839 // check for that explicitly too. Horribly hacky, but temporary.
Jiangning Liu4df23632014-01-16 09:16:13 +00001840 if ((NumVecs > 2 && !isVLDfixed(Opc)) ||
Jim Grosbach05df4602011-10-31 21:50:31 +00001841 !isa<ConstantSDNode>(Inc.getNode()))
Jim Grosbach2098cb12011-10-24 21:45:13 +00001842 Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc);
Evan Cheng630063a2010-05-10 21:26:24 +00001843 }
Bob Wilson06fce872011-02-07 17:43:21 +00001844 Ops.push_back(Pred);
1845 Ops.push_back(Reg0);
1846 Ops.push_back(Chain);
Michael Liaob53d8962013-04-19 22:22:57 +00001847 VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
Bob Wilson75a64082010-09-02 16:00:54 +00001848
Bob Wilson12b47992009-10-14 17:28:52 +00001849 } else {
1850 // Otherwise, quad registers are loaded with two separate instructions,
1851 // where one loads the even registers and the other loads the odd registers.
Bob Wilson35fafca2010-09-03 18:16:02 +00001852 EVT AddrTy = MemAddr.getValueType();
Bob Wilson12b47992009-10-14 17:28:52 +00001853
Bob Wilson06fce872011-02-07 17:43:21 +00001854 // Load the even subregs. This is always an updating load, so that it
1855 // provides the address to the second load for the odd subregs.
Bob Wilson35fafca2010-09-03 18:16:02 +00001856 SDValue ImplDef =
1857 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1858 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
Bob Wilsona609b892011-02-07 17:43:15 +00001859 SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
Michael Liaob53d8962013-04-19 22:22:57 +00001860 ResTy, AddrTy, MVT::Other, OpsA);
Bob Wilson35fafca2010-09-03 18:16:02 +00001861 Chain = SDValue(VLdA, 2);
Bob Wilson12b47992009-10-14 17:28:52 +00001862
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001863 // Load the odd subregs.
Bob Wilson06fce872011-02-07 17:43:21 +00001864 Ops.push_back(SDValue(VLdA, 1));
1865 Ops.push_back(Align);
1866 if (isUpdating) {
1867 SDValue Inc = N->getOperand(AddrOpIdx + 1);
1868 assert(isa<ConstantSDNode>(Inc.getNode()) &&
1869 "only constant post-increment update allowed for VLD3/4");
1870 (void)Inc;
1871 Ops.push_back(Reg0);
1872 }
1873 Ops.push_back(SDValue(VLdA, 0));
1874 Ops.push_back(Pred);
1875 Ops.push_back(Reg0);
1876 Ops.push_back(Chain);
Michael Liaob53d8962013-04-19 22:22:57 +00001877 VLd = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys, Ops);
Bob Wilson35fafca2010-09-03 18:16:02 +00001878 }
Bob Wilson12b47992009-10-14 17:28:52 +00001879
Evan Cheng40791332011-04-19 00:04:03 +00001880 // Transfer memoperands.
1881 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1882 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1883 cast<MachineSDNode>(VLd)->setMemRefs(MemOp, MemOp + 1);
1884
Bob Wilson06fce872011-02-07 17:43:21 +00001885 if (NumVecs == 1)
1886 return VLd;
1887
1888 // Extract out the subregisters.
1889 SDValue SuperReg = SDValue(VLd, 0);
1890 assert(ARM::dsub_7 == ARM::dsub_0+7 &&
1891 ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1892 unsigned Sub0 = (is64BitVector ? ARM::dsub_0 : ARM::qsub_0);
1893 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1894 ReplaceUses(SDValue(N, Vec),
1895 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
1896 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
1897 if (isUpdating)
1898 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLd, 2));
Craig Topper062a2ba2014-04-25 05:30:21 +00001899 return nullptr;
Bob Wilson12b47992009-10-14 17:28:52 +00001900}
1901
Bob Wilson06fce872011-02-07 17:43:21 +00001902SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +00001903 const uint16_t *DOpcodes,
1904 const uint16_t *QOpcodes0,
1905 const uint16_t *QOpcodes1) {
Bob Wilson3ed511b2010-07-06 23:36:25 +00001906 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00001907 SDLoc dl(N);
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001908
Bob Wilsonae08a732010-03-20 22:13:40 +00001909 SDValue MemAddr, Align;
Bob Wilson06fce872011-02-07 17:43:21 +00001910 unsigned AddrOpIdx = isUpdating ? 1 : 2;
1911 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
1912 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
Craig Topper062a2ba2014-04-25 05:30:21 +00001913 return nullptr;
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001914
Evan Cheng40791332011-04-19 00:04:03 +00001915 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1916 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1917
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001918 SDValue Chain = N->getOperand(0);
Bob Wilson06fce872011-02-07 17:43:21 +00001919 EVT VT = N->getOperand(Vec0Idx).getValueType();
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001920 bool is64BitVector = VT.is64BitVector();
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001921 Align = GetVLDSTAlign(Align, NumVecs, is64BitVector);
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001922
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001923 unsigned OpcodeIndex;
1924 switch (VT.getSimpleVT().SimpleTy) {
1925 default: llvm_unreachable("unhandled vst type");
1926 // Double-register operations:
1927 case MVT::v8i8: OpcodeIndex = 0; break;
1928 case MVT::v4i16: OpcodeIndex = 1; break;
1929 case MVT::v2f32:
1930 case MVT::v2i32: OpcodeIndex = 2; break;
1931 case MVT::v1i64: OpcodeIndex = 3; break;
1932 // Quad-register operations:
1933 case MVT::v16i8: OpcodeIndex = 0; break;
1934 case MVT::v8i16: OpcodeIndex = 1; break;
1935 case MVT::v4f32:
1936 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilsoncc0a2a72010-03-23 06:20:33 +00001937 case MVT::v2i64: OpcodeIndex = 3;
1938 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1939 break;
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001940 }
1941
Bob Wilson06fce872011-02-07 17:43:21 +00001942 std::vector<EVT> ResTys;
1943 if (isUpdating)
1944 ResTys.push_back(MVT::i32);
1945 ResTys.push_back(MVT::Other);
1946
Evan Cheng3da64f762010-04-16 05:46:06 +00001947 SDValue Pred = getAL(CurDAG);
Bob Wilsonae08a732010-03-20 22:13:40 +00001948 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilson06fce872011-02-07 17:43:21 +00001949 SmallVector<SDValue, 7> Ops;
Evan Chenga33fc862009-11-21 06:21:52 +00001950
Bob Wilson06fce872011-02-07 17:43:21 +00001951 // Double registers and VST1/VST2 quad registers are directly supported.
1952 if (is64BitVector || NumVecs <= 2) {
Bob Wilsona609b892011-02-07 17:43:15 +00001953 SDValue SrcReg;
Bob Wilson950882b2010-08-28 05:12:57 +00001954 if (NumVecs == 1) {
Bob Wilson06fce872011-02-07 17:43:21 +00001955 SrcReg = N->getOperand(Vec0Idx);
1956 } else if (is64BitVector) {
Evan Chenge276c182010-05-11 01:19:40 +00001957 // Form a REG_SEQUENCE to force register allocation.
Bob Wilson06fce872011-02-07 17:43:21 +00001958 SDValue V0 = N->getOperand(Vec0Idx + 0);
1959 SDValue V1 = N->getOperand(Vec0Idx + 1);
Evan Chenge276c182010-05-11 01:19:40 +00001960 if (NumVecs == 2)
Weiming Zhao95782222012-11-17 00:23:35 +00001961 SrcReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
Evan Chenge276c182010-05-11 01:19:40 +00001962 else {
Bob Wilson06fce872011-02-07 17:43:21 +00001963 SDValue V2 = N->getOperand(Vec0Idx + 2);
Bob Wilsona609b892011-02-07 17:43:15 +00001964 // If it's a vst3, form a quad D-register and leave the last part as
Evan Chenge276c182010-05-11 01:19:40 +00001965 // an undef.
1966 SDValue V3 = (NumVecs == 3)
1967 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
Bob Wilson06fce872011-02-07 17:43:21 +00001968 : N->getOperand(Vec0Idx + 3);
Weiming Zhao95782222012-11-17 00:23:35 +00001969 SrcReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
Evan Chenge276c182010-05-11 01:19:40 +00001970 }
Bob Wilson950882b2010-08-28 05:12:57 +00001971 } else {
1972 // Form a QQ register.
Bob Wilson06fce872011-02-07 17:43:21 +00001973 SDValue Q0 = N->getOperand(Vec0Idx);
1974 SDValue Q1 = N->getOperand(Vec0Idx + 1);
Weiming Zhao95782222012-11-17 00:23:35 +00001975 SrcReg = SDValue(createQRegPairNode(MVT::v4i64, Q0, Q1), 0);
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001976 }
Bob Wilson06fce872011-02-07 17:43:21 +00001977
1978 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1979 QOpcodes0[OpcodeIndex]);
1980 Ops.push_back(MemAddr);
1981 Ops.push_back(Align);
1982 if (isUpdating) {
1983 SDValue Inc = N->getOperand(AddrOpIdx + 1);
Jim Grosbach88ac7612011-12-14 21:32:11 +00001984 // FIXME: VST1/VST2 fixed increment doesn't need Reg0. Remove the reg0
Jim Grosbach05df4602011-10-31 21:50:31 +00001985 // case entirely when the rest are updated to that form, too.
Jim Grosbach88ac7612011-12-14 21:32:11 +00001986 if (NumVecs <= 2 && !isa<ConstantSDNode>(Inc.getNode()))
Jim Grosbach05df4602011-10-31 21:50:31 +00001987 Opc = getVLDSTRegisterUpdateOpcode(Opc);
Jiangning Liu4df23632014-01-16 09:16:13 +00001988 // FIXME: We use a VST1 for v1i64 even if the pseudo says vld2/3/4, so
Jim Grosbach05df4602011-10-31 21:50:31 +00001989 // check for that explicitly too. Horribly hacky, but temporary.
Jiangning Liu4df23632014-01-16 09:16:13 +00001990 if (!isa<ConstantSDNode>(Inc.getNode()))
1991 Ops.push_back(Inc);
1992 else if (NumVecs > 2 && !isVSTfixed(Opc))
1993 Ops.push_back(Reg0);
Bob Wilson06fce872011-02-07 17:43:21 +00001994 }
1995 Ops.push_back(SrcReg);
1996 Ops.push_back(Pred);
1997 Ops.push_back(Reg0);
1998 Ops.push_back(Chain);
Michael Liaob53d8962013-04-19 22:22:57 +00001999 SDNode *VSt = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
Evan Cheng40791332011-04-19 00:04:03 +00002000
2001 // Transfer memoperands.
2002 cast<MachineSDNode>(VSt)->setMemRefs(MemOp, MemOp + 1);
2003
2004 return VSt;
Bob Wilsonc350cdf2009-10-14 18:32:29 +00002005 }
2006
2007 // Otherwise, quad registers are stored with two separate instructions,
2008 // where one stores the even registers and the other stores the odd registers.
Evan Cheng9e688cb2010-05-15 07:53:37 +00002009
Bob Wilson01ac8f92010-06-16 21:34:01 +00002010 // Form the QQQQ REG_SEQUENCE.
Bob Wilson06fce872011-02-07 17:43:21 +00002011 SDValue V0 = N->getOperand(Vec0Idx + 0);
2012 SDValue V1 = N->getOperand(Vec0Idx + 1);
2013 SDValue V2 = N->getOperand(Vec0Idx + 2);
Bob Wilson950882b2010-08-28 05:12:57 +00002014 SDValue V3 = (NumVecs == 3)
2015 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
Bob Wilson06fce872011-02-07 17:43:21 +00002016 : N->getOperand(Vec0Idx + 3);
Weiming Zhao95782222012-11-17 00:23:35 +00002017 SDValue RegSeq = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilson01ac8f92010-06-16 21:34:01 +00002018
Bob Wilson06fce872011-02-07 17:43:21 +00002019 // Store the even D registers. This is always an updating store, so that it
2020 // provides the address to the second store for the odd subregs.
Bob Wilsona609b892011-02-07 17:43:15 +00002021 const SDValue OpsA[] = { MemAddr, Align, Reg0, RegSeq, Pred, Reg0, Chain };
2022 SDNode *VStA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
2023 MemAddr.getValueType(),
Michael Liaob53d8962013-04-19 22:22:57 +00002024 MVT::Other, OpsA);
Evan Cheng40791332011-04-19 00:04:03 +00002025 cast<MachineSDNode>(VStA)->setMemRefs(MemOp, MemOp + 1);
Bob Wilson01ac8f92010-06-16 21:34:01 +00002026 Chain = SDValue(VStA, 1);
2027
2028 // Store the odd D registers.
Bob Wilson06fce872011-02-07 17:43:21 +00002029 Ops.push_back(SDValue(VStA, 0));
2030 Ops.push_back(Align);
2031 if (isUpdating) {
2032 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2033 assert(isa<ConstantSDNode>(Inc.getNode()) &&
2034 "only constant post-increment update allowed for VST3/4");
2035 (void)Inc;
2036 Ops.push_back(Reg0);
2037 }
2038 Ops.push_back(RegSeq);
2039 Ops.push_back(Pred);
2040 Ops.push_back(Reg0);
2041 Ops.push_back(Chain);
Evan Cheng40791332011-04-19 00:04:03 +00002042 SDNode *VStB = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys,
Michael Liaob53d8962013-04-19 22:22:57 +00002043 Ops);
Evan Cheng40791332011-04-19 00:04:03 +00002044 cast<MachineSDNode>(VStB)->setMemRefs(MemOp, MemOp + 1);
2045 return VStB;
Bob Wilsonc350cdf2009-10-14 18:32:29 +00002046}
2047
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002048SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
Bob Wilson06fce872011-02-07 17:43:21 +00002049 bool isUpdating, unsigned NumVecs,
Craig Topper01736f82012-05-24 05:17:00 +00002050 const uint16_t *DOpcodes,
2051 const uint16_t *QOpcodes) {
Bob Wilson93117bc2009-10-14 16:46:45 +00002052 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00002053 SDLoc dl(N);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002054
Bob Wilsonae08a732010-03-20 22:13:40 +00002055 SDValue MemAddr, Align;
Bob Wilson06fce872011-02-07 17:43:21 +00002056 unsigned AddrOpIdx = isUpdating ? 1 : 2;
2057 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
2058 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
Craig Topper062a2ba2014-04-25 05:30:21 +00002059 return nullptr;
Bob Wilson4145e3a2009-10-14 16:19:03 +00002060
Evan Cheng40791332011-04-19 00:04:03 +00002061 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2062 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2063
Bob Wilson4145e3a2009-10-14 16:19:03 +00002064 SDValue Chain = N->getOperand(0);
2065 unsigned Lane =
Bob Wilson06fce872011-02-07 17:43:21 +00002066 cast<ConstantSDNode>(N->getOperand(Vec0Idx + NumVecs))->getZExtValue();
2067 EVT VT = N->getOperand(Vec0Idx).getValueType();
Bob Wilson4145e3a2009-10-14 16:19:03 +00002068 bool is64BitVector = VT.is64BitVector();
2069
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00002070 unsigned Alignment = 0;
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002071 if (NumVecs != 3) {
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00002072 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002073 unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
2074 if (Alignment > NumBytes)
2075 Alignment = NumBytes;
Bob Wilsond29b38c2010-12-10 19:37:42 +00002076 if (Alignment < 8 && Alignment < NumBytes)
2077 Alignment = 0;
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002078 // Alignment must be a power of two; make sure of that.
2079 Alignment = (Alignment & -Alignment);
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00002080 if (Alignment == 1)
2081 Alignment = 0;
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002082 }
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00002083 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002084
Bob Wilson4145e3a2009-10-14 16:19:03 +00002085 unsigned OpcodeIndex;
2086 switch (VT.getSimpleVT().SimpleTy) {
Bob Wilson93117bc2009-10-14 16:46:45 +00002087 default: llvm_unreachable("unhandled vld/vst lane type");
Bob Wilson4145e3a2009-10-14 16:19:03 +00002088 // Double-register operations:
2089 case MVT::v8i8: OpcodeIndex = 0; break;
2090 case MVT::v4i16: OpcodeIndex = 1; break;
2091 case MVT::v2f32:
2092 case MVT::v2i32: OpcodeIndex = 2; break;
2093 // Quad-register operations:
2094 case MVT::v8i16: OpcodeIndex = 0; break;
2095 case MVT::v4f32:
2096 case MVT::v4i32: OpcodeIndex = 1; break;
2097 }
2098
Bob Wilson06fce872011-02-07 17:43:21 +00002099 std::vector<EVT> ResTys;
2100 if (IsLoad) {
2101 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
2102 if (!is64BitVector)
2103 ResTyElts *= 2;
2104 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(),
2105 MVT::i64, ResTyElts));
2106 }
2107 if (isUpdating)
2108 ResTys.push_back(MVT::i32);
2109 ResTys.push_back(MVT::Other);
2110
Evan Cheng3da64f762010-04-16 05:46:06 +00002111 SDValue Pred = getAL(CurDAG);
Bob Wilsonae08a732010-03-20 22:13:40 +00002112 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chenga33fc862009-11-21 06:21:52 +00002113
Bob Wilson06fce872011-02-07 17:43:21 +00002114 SmallVector<SDValue, 8> Ops;
Bob Wilson4145e3a2009-10-14 16:19:03 +00002115 Ops.push_back(MemAddr);
Jim Grosbachd1d002a2009-11-07 21:25:39 +00002116 Ops.push_back(Align);
Bob Wilson06fce872011-02-07 17:43:21 +00002117 if (isUpdating) {
2118 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2119 Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc);
2120 }
Bob Wilson01ac8f92010-06-16 21:34:01 +00002121
Bob Wilsond5c57a52010-09-13 23:01:35 +00002122 SDValue SuperReg;
Bob Wilson06fce872011-02-07 17:43:21 +00002123 SDValue V0 = N->getOperand(Vec0Idx + 0);
2124 SDValue V1 = N->getOperand(Vec0Idx + 1);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002125 if (NumVecs == 2) {
2126 if (is64BitVector)
Weiming Zhao95782222012-11-17 00:23:35 +00002127 SuperReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002128 else
Weiming Zhao95782222012-11-17 00:23:35 +00002129 SuperReg = SDValue(createQRegPairNode(MVT::v4i64, V0, V1), 0);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002130 } else {
Bob Wilson06fce872011-02-07 17:43:21 +00002131 SDValue V2 = N->getOperand(Vec0Idx + 2);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002132 SDValue V3 = (NumVecs == 3)
Bob Wilson06fce872011-02-07 17:43:21 +00002133 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2134 : N->getOperand(Vec0Idx + 3);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002135 if (is64BitVector)
Weiming Zhao95782222012-11-17 00:23:35 +00002136 SuperReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002137 else
Weiming Zhao95782222012-11-17 00:23:35 +00002138 SuperReg = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002139 }
Bob Wilsond5c57a52010-09-13 23:01:35 +00002140 Ops.push_back(SuperReg);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002141 Ops.push_back(getI32Imm(Lane));
Evan Chenga33fc862009-11-21 06:21:52 +00002142 Ops.push_back(Pred);
Bob Wilsonae08a732010-03-20 22:13:40 +00002143 Ops.push_back(Reg0);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002144 Ops.push_back(Chain);
2145
Bob Wilson06fce872011-02-07 17:43:21 +00002146 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
2147 QOpcodes[OpcodeIndex]);
Michael Liaob53d8962013-04-19 22:22:57 +00002148 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
Evan Cheng40791332011-04-19 00:04:03 +00002149 cast<MachineSDNode>(VLdLn)->setMemRefs(MemOp, MemOp + 1);
Bob Wilson93117bc2009-10-14 16:46:45 +00002150 if (!IsLoad)
Bob Wilson06fce872011-02-07 17:43:21 +00002151 return VLdLn;
Evan Cheng0cbd11d2010-05-15 01:36:29 +00002152
Bob Wilsond5c57a52010-09-13 23:01:35 +00002153 // Extract the subregisters.
Bob Wilson06fce872011-02-07 17:43:21 +00002154 SuperReg = SDValue(VLdLn, 0);
2155 assert(ARM::dsub_7 == ARM::dsub_0+7 &&
2156 ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
2157 unsigned Sub0 = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
Bob Wilson01ac8f92010-06-16 21:34:01 +00002158 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2159 ReplaceUses(SDValue(N, Vec),
Bob Wilson06fce872011-02-07 17:43:21 +00002160 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
2161 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdLn, 1));
2162 if (isUpdating)
2163 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdLn, 2));
Craig Topper062a2ba2014-04-25 05:30:21 +00002164 return nullptr;
Bob Wilson4145e3a2009-10-14 16:19:03 +00002165}
2166
Bob Wilson06fce872011-02-07 17:43:21 +00002167SDNode *ARMDAGToDAGISel::SelectVLDDup(SDNode *N, bool isUpdating,
Craig Topper01736f82012-05-24 05:17:00 +00002168 unsigned NumVecs,
2169 const uint16_t *Opcodes) {
Bob Wilson2d790df2010-11-28 06:51:26 +00002170 assert(NumVecs >=2 && NumVecs <= 4 && "VLDDup NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00002171 SDLoc dl(N);
Bob Wilson2d790df2010-11-28 06:51:26 +00002172
2173 SDValue MemAddr, Align;
2174 if (!SelectAddrMode6(N, N->getOperand(1), MemAddr, Align))
Craig Topper062a2ba2014-04-25 05:30:21 +00002175 return nullptr;
Bob Wilson2d790df2010-11-28 06:51:26 +00002176
Evan Cheng40791332011-04-19 00:04:03 +00002177 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2178 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2179
Bob Wilson2d790df2010-11-28 06:51:26 +00002180 SDValue Chain = N->getOperand(0);
2181 EVT VT = N->getValueType(0);
2182
2183 unsigned Alignment = 0;
2184 if (NumVecs != 3) {
2185 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
2186 unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
2187 if (Alignment > NumBytes)
2188 Alignment = NumBytes;
Bob Wilsond29b38c2010-12-10 19:37:42 +00002189 if (Alignment < 8 && Alignment < NumBytes)
2190 Alignment = 0;
Bob Wilson2d790df2010-11-28 06:51:26 +00002191 // Alignment must be a power of two; make sure of that.
2192 Alignment = (Alignment & -Alignment);
2193 if (Alignment == 1)
2194 Alignment = 0;
2195 }
2196 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
2197
2198 unsigned OpcodeIndex;
2199 switch (VT.getSimpleVT().SimpleTy) {
2200 default: llvm_unreachable("unhandled vld-dup type");
2201 case MVT::v8i8: OpcodeIndex = 0; break;
2202 case MVT::v4i16: OpcodeIndex = 1; break;
2203 case MVT::v2f32:
2204 case MVT::v2i32: OpcodeIndex = 2; break;
2205 }
2206
2207 SDValue Pred = getAL(CurDAG);
2208 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2209 SDValue SuperReg;
2210 unsigned Opc = Opcodes[OpcodeIndex];
Bob Wilson06fce872011-02-07 17:43:21 +00002211 SmallVector<SDValue, 6> Ops;
2212 Ops.push_back(MemAddr);
2213 Ops.push_back(Align);
2214 if (isUpdating) {
Jim Grosbachc80a2642011-12-21 19:40:55 +00002215 // fixed-stride update instructions don't have an explicit writeback
2216 // operand. It's implicit in the opcode itself.
Bob Wilson06fce872011-02-07 17:43:21 +00002217 SDValue Inc = N->getOperand(2);
Jim Grosbachc80a2642011-12-21 19:40:55 +00002218 if (!isa<ConstantSDNode>(Inc.getNode()))
2219 Ops.push_back(Inc);
2220 // FIXME: VLD3 and VLD4 haven't been updated to that form yet.
2221 else if (NumVecs > 2)
2222 Ops.push_back(Reg0);
Bob Wilson06fce872011-02-07 17:43:21 +00002223 }
2224 Ops.push_back(Pred);
2225 Ops.push_back(Reg0);
2226 Ops.push_back(Chain);
Bob Wilson2d790df2010-11-28 06:51:26 +00002227
2228 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
Bob Wilson06fce872011-02-07 17:43:21 +00002229 std::vector<EVT> ResTys;
Evan Cheng40791332011-04-19 00:04:03 +00002230 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(), MVT::i64,ResTyElts));
Bob Wilson06fce872011-02-07 17:43:21 +00002231 if (isUpdating)
2232 ResTys.push_back(MVT::i32);
2233 ResTys.push_back(MVT::Other);
Michael Liaob53d8962013-04-19 22:22:57 +00002234 SDNode *VLdDup = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
Evan Cheng40791332011-04-19 00:04:03 +00002235 cast<MachineSDNode>(VLdDup)->setMemRefs(MemOp, MemOp + 1);
Bob Wilson2d790df2010-11-28 06:51:26 +00002236 SuperReg = SDValue(VLdDup, 0);
Bob Wilson2d790df2010-11-28 06:51:26 +00002237
2238 // Extract the subregisters.
2239 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
2240 unsigned SubIdx = ARM::dsub_0;
2241 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2242 ReplaceUses(SDValue(N, Vec),
2243 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
Bob Wilson06fce872011-02-07 17:43:21 +00002244 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdDup, 1));
2245 if (isUpdating)
2246 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdDup, 2));
Craig Topper062a2ba2014-04-25 05:30:21 +00002247 return nullptr;
Bob Wilson2d790df2010-11-28 06:51:26 +00002248}
2249
Bob Wilson5bc8a792010-07-07 00:08:54 +00002250SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
2251 unsigned Opc) {
Bob Wilson3ed511b2010-07-06 23:36:25 +00002252 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00002253 SDLoc dl(N);
Bob Wilson3ed511b2010-07-06 23:36:25 +00002254 EVT VT = N->getValueType(0);
Bob Wilson5bc8a792010-07-07 00:08:54 +00002255 unsigned FirstTblReg = IsExt ? 2 : 1;
Bob Wilson3ed511b2010-07-06 23:36:25 +00002256
2257 // Form a REG_SEQUENCE to force register allocation.
2258 SDValue RegSeq;
Bob Wilson5bc8a792010-07-07 00:08:54 +00002259 SDValue V0 = N->getOperand(FirstTblReg + 0);
2260 SDValue V1 = N->getOperand(FirstTblReg + 1);
Bob Wilson3ed511b2010-07-06 23:36:25 +00002261 if (NumVecs == 2)
Weiming Zhao95782222012-11-17 00:23:35 +00002262 RegSeq = SDValue(createDRegPairNode(MVT::v16i8, V0, V1), 0);
Bob Wilson3ed511b2010-07-06 23:36:25 +00002263 else {
Bob Wilson5bc8a792010-07-07 00:08:54 +00002264 SDValue V2 = N->getOperand(FirstTblReg + 2);
Jim Grosbachd37f0712010-10-21 19:38:40 +00002265 // If it's a vtbl3, form a quad D-register and leave the last part as
Bob Wilson3ed511b2010-07-06 23:36:25 +00002266 // an undef.
2267 SDValue V3 = (NumVecs == 3)
2268 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
Bob Wilson5bc8a792010-07-07 00:08:54 +00002269 : N->getOperand(FirstTblReg + 3);
Weiming Zhao95782222012-11-17 00:23:35 +00002270 RegSeq = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
Bob Wilson3ed511b2010-07-06 23:36:25 +00002271 }
2272
Bob Wilson5bc8a792010-07-07 00:08:54 +00002273 SmallVector<SDValue, 6> Ops;
2274 if (IsExt)
2275 Ops.push_back(N->getOperand(1));
Bob Wilsonc597fd3b2010-09-13 23:55:10 +00002276 Ops.push_back(RegSeq);
Bob Wilson5bc8a792010-07-07 00:08:54 +00002277 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
Bob Wilson3ed511b2010-07-06 23:36:25 +00002278 Ops.push_back(getAL(CurDAG)); // predicate
2279 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
Michael Liaob53d8962013-04-19 22:22:57 +00002280 return CurDAG->getMachineNode(Opc, dl, VT, Ops);
Bob Wilson3ed511b2010-07-06 23:36:25 +00002281}
2282
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002283SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
Jim Grosbach825cb292010-04-22 23:24:18 +00002284 bool isSigned) {
Sandeep Patel423e42b2009-10-13 18:59:48 +00002285 if (!Subtarget->hasV6T2Ops())
Craig Topper062a2ba2014-04-25 05:30:21 +00002286 return nullptr;
Bob Wilson93117bc2009-10-14 16:46:45 +00002287
Evan Chengeae6d2c2012-12-19 20:16:09 +00002288 unsigned Opc = isSigned
2289 ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
Jim Grosbach825cb292010-04-22 23:24:18 +00002290 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
2291
Jim Grosbach825cb292010-04-22 23:24:18 +00002292 // For unsigned extracts, check for a shift right and mask
2293 unsigned And_imm = 0;
2294 if (N->getOpcode() == ISD::AND) {
2295 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
2296
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002297 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
Jim Grosbach825cb292010-04-22 23:24:18 +00002298 if (And_imm & (And_imm + 1))
Craig Topper062a2ba2014-04-25 05:30:21 +00002299 return nullptr;
Jim Grosbach825cb292010-04-22 23:24:18 +00002300
2301 unsigned Srl_imm = 0;
2302 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
2303 Srl_imm)) {
2304 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2305
Jim Grosbach03f56d92011-07-27 21:09:25 +00002306 // Note: The width operand is encoded as width-1.
2307 unsigned Width = CountTrailingOnes_32(And_imm) - 1;
Jim Grosbach825cb292010-04-22 23:24:18 +00002308 unsigned LSB = Srl_imm;
Evan Chengeae6d2c2012-12-19 20:16:09 +00002309
Jim Grosbach825cb292010-04-22 23:24:18 +00002310 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengeae6d2c2012-12-19 20:16:09 +00002311
2312 if ((LSB + Width + 1) == N->getValueType(0).getSizeInBits()) {
2313 // It's cheaper to use a right shift to extract the top bits.
2314 if (Subtarget->isThumb()) {
2315 Opc = isSigned ? ARM::t2ASRri : ARM::t2LSRri;
2316 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2317 CurDAG->getTargetConstant(LSB, MVT::i32),
2318 getAL(CurDAG), Reg0, Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002319 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
Evan Chengeae6d2c2012-12-19 20:16:09 +00002320 }
2321
2322 // ARM models shift instructions as MOVsi with shifter operand.
2323 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(ISD::SRL);
2324 SDValue ShOpc =
2325 CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, LSB),
2326 MVT::i32);
2327 SDValue Ops[] = { N->getOperand(0).getOperand(0), ShOpc,
2328 getAL(CurDAG), Reg0, Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002329 return CurDAG->SelectNodeTo(N, ARM::MOVsi, MVT::i32, Ops);
Evan Chengeae6d2c2012-12-19 20:16:09 +00002330 }
2331
Jim Grosbach825cb292010-04-22 23:24:18 +00002332 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2333 CurDAG->getTargetConstant(LSB, MVT::i32),
2334 CurDAG->getTargetConstant(Width, MVT::i32),
Craig Topper481fb282014-04-27 19:21:11 +00002335 getAL(CurDAG), Reg0 };
2336 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
Jim Grosbach825cb292010-04-22 23:24:18 +00002337 }
2338 }
Craig Topper062a2ba2014-04-25 05:30:21 +00002339 return nullptr;
Jim Grosbach825cb292010-04-22 23:24:18 +00002340 }
2341
2342 // Otherwise, we're looking for a shift of a shift
Sandeep Patel423e42b2009-10-13 18:59:48 +00002343 unsigned Shl_imm = 0;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002344 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
Sandeep Patel423e42b2009-10-13 18:59:48 +00002345 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
2346 unsigned Srl_imm = 0;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002347 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
Sandeep Patel423e42b2009-10-13 18:59:48 +00002348 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
Jim Grosbach03f56d92011-07-27 21:09:25 +00002349 // Note: The width operand is encoded as width-1.
2350 unsigned Width = 32 - Srl_imm - 1;
Sandeep Patel423e42b2009-10-13 18:59:48 +00002351 int LSB = Srl_imm - Shl_imm;
Evan Cheng0f55e9c2009-10-22 00:40:00 +00002352 if (LSB < 0)
Craig Topper062a2ba2014-04-25 05:30:21 +00002353 return nullptr;
Sandeep Patel423e42b2009-10-13 18:59:48 +00002354 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002355 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sandeep Patel423e42b2009-10-13 18:59:48 +00002356 CurDAG->getTargetConstant(LSB, MVT::i32),
2357 CurDAG->getTargetConstant(Width, MVT::i32),
2358 getAL(CurDAG), Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002359 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
Sandeep Patel423e42b2009-10-13 18:59:48 +00002360 }
2361 }
Craig Topper062a2ba2014-04-25 05:30:21 +00002362 return nullptr;
Sandeep Patel423e42b2009-10-13 18:59:48 +00002363}
2364
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002365/// Target-specific DAG combining for ISD::XOR.
2366/// Target-independent combining lowers SELECT_CC nodes of the form
2367/// select_cc setg[ge] X, 0, X, -X
2368/// select_cc setgt X, -1, X, -X
2369/// select_cc setl[te] X, 0, -X, X
2370/// select_cc setlt X, 1, -X, X
2371/// which represent Integer ABS into:
2372/// Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2373/// ARM instruction selection detects the latter and matches it to
2374/// ARM::ABS or ARM::t2ABS machine node.
2375SDNode *ARMDAGToDAGISel::SelectABSOp(SDNode *N){
2376 SDValue XORSrc0 = N->getOperand(0);
2377 SDValue XORSrc1 = N->getOperand(1);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002378 EVT VT = N->getValueType(0);
2379
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002380 if (Subtarget->isThumb1Only())
Craig Topper062a2ba2014-04-25 05:30:21 +00002381 return nullptr;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002382
Jim Grosbachb437a8c2012-08-01 20:33:00 +00002383 if (XORSrc0.getOpcode() != ISD::ADD || XORSrc1.getOpcode() != ISD::SRA)
Craig Topper062a2ba2014-04-25 05:30:21 +00002384 return nullptr;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002385
2386 SDValue ADDSrc0 = XORSrc0.getOperand(0);
2387 SDValue ADDSrc1 = XORSrc0.getOperand(1);
2388 SDValue SRASrc0 = XORSrc1.getOperand(0);
2389 SDValue SRASrc1 = XORSrc1.getOperand(1);
2390 ConstantSDNode *SRAConstant = dyn_cast<ConstantSDNode>(SRASrc1);
2391 EVT XType = SRASrc0.getValueType();
2392 unsigned Size = XType.getSizeInBits() - 1;
2393
Jim Grosbachb437a8c2012-08-01 20:33:00 +00002394 if (ADDSrc1 == XORSrc1 && ADDSrc0 == SRASrc0 &&
Craig Topper062a2ba2014-04-25 05:30:21 +00002395 XType.isInteger() && SRAConstant != nullptr &&
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002396 Size == SRAConstant->getZExtValue()) {
Jim Grosbachb437a8c2012-08-01 20:33:00 +00002397 unsigned Opcode = Subtarget->isThumb2() ? ARM::t2ABS : ARM::ABS;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002398 return CurDAG->SelectNodeTo(N, Opcode, VT, ADDSrc0);
2399 }
2400
Craig Topper062a2ba2014-04-25 05:30:21 +00002401 return nullptr;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002402}
2403
Evan Chengd85631e2010-05-05 18:28:36 +00002404SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
2405 // The only time a CONCAT_VECTORS operation can have legal types is when
2406 // two 64-bit vectors are concatenated to a 128-bit vector.
2407 EVT VT = N->getValueType(0);
2408 if (!VT.is128BitVector() || N->getNumOperands() != 2)
2409 llvm_unreachable("unexpected CONCAT_VECTORS");
Weiming Zhao95782222012-11-17 00:23:35 +00002410 return createDRegPairNode(VT, N->getOperand(0), N->getOperand(1));
Evan Chengd85631e2010-05-05 18:28:36 +00002411}
2412
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002413SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002414 SDLoc dl(N);
Evan Cheng10043e22007-01-19 07:51:42 +00002415
Tim Northover31d093c2013-09-22 08:21:56 +00002416 if (N->isMachineOpcode()) {
2417 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00002418 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00002419 }
Rafael Espindola4e760152006-06-12 12:28:08 +00002420
2421 switch (N->getOpcode()) {
Evan Cheng10043e22007-01-19 07:51:42 +00002422 default: break;
Weiming Zhaoc5987002013-02-14 18:10:21 +00002423 case ISD::INLINEASM: {
2424 SDNode *ResNode = SelectInlineAsm(N);
2425 if (ResNode)
2426 return ResNode;
2427 break;
2428 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002429 case ISD::XOR: {
2430 // Select special operations if XOR node forms integer ABS pattern
2431 SDNode *ResNode = SelectABSOp(N);
2432 if (ResNode)
2433 return ResNode;
2434 // Other cases are autogenerated.
2435 break;
2436 }
Evan Cheng10043e22007-01-19 07:51:42 +00002437 case ISD::Constant: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002438 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00002439 bool UseCP = true;
Tim Northover55c625f2014-01-23 13:43:47 +00002440 if (Subtarget->useMovt())
Anton Korobeynikov7c2b1e72009-09-27 23:52:58 +00002441 // Thumb2-aware targets have the MOVT instruction, so all immediates can
2442 // be done with MOV + MOVT, at worst.
Tim Northover55c625f2014-01-23 13:43:47 +00002443 UseCP = false;
Anton Korobeynikov7c2b1e72009-09-27 23:52:58 +00002444 else {
2445 if (Subtarget->isThumb()) {
Tim Northover55c625f2014-01-23 13:43:47 +00002446 UseCP = (Val > 255 && // MOV
2447 ~Val > 255 && // MOV + MVN
2448 !ARM_AM::isThumbImmShiftedVal(Val) && // MOV + LSL
2449 !(Subtarget->hasV6T2Ops() && Val <= 0xffff)); // MOVW
Anton Korobeynikov7c2b1e72009-09-27 23:52:58 +00002450 } else
Tim Northover55c625f2014-01-23 13:43:47 +00002451 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
2452 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
2453 !ARM_AM::isSOImmTwoPartVal(Val) && // two instrs.
2454 !(Subtarget->hasV6T2Ops() && Val <= 0xffff)); // MOVW
Anton Korobeynikov7c2b1e72009-09-27 23:52:58 +00002455 }
2456
Evan Cheng10043e22007-01-19 07:51:42 +00002457 if (UseCP) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002458 SDValue CPIdx =
Owen Anderson55f1c092009-08-13 21:58:54 +00002459 CurDAG->getTargetConstantPool(ConstantInt::get(
2460 Type::getInt32Ty(*CurDAG->getContext()), Val),
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002461 getTargetLowering()->getPointerTy());
Evan Cheng1526ba52007-01-24 08:53:17 +00002462
2463 SDNode *ResNode;
Tim Northover55c625f2014-01-23 13:43:47 +00002464 if (Subtarget->isThumb()) {
Evan Cheng3da64f762010-04-16 05:46:06 +00002465 SDValue Pred = getAL(CurDAG);
Owen Anderson9f944592009-08-11 20:47:22 +00002466 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
Evan Chengcd4cdd12009-07-11 06:43:01 +00002467 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
Jim Grosbachbfef3092010-12-15 23:52:36 +00002468 ResNode = CurDAG->getMachineNode(ARM::tLDRpci, dl, MVT::i32, MVT::Other,
Michael Liaob53d8962013-04-19 22:22:57 +00002469 Ops);
Evan Chengcd4cdd12009-07-11 06:43:01 +00002470 } else {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002471 SDValue Ops[] = {
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002472 CPIdx,
Owen Anderson9f944592009-08-11 20:47:22 +00002473 CurDAG->getTargetConstant(0, MVT::i32),
Evan Cheng7e90b112007-07-05 07:15:27 +00002474 getAL(CurDAG),
Owen Anderson9f944592009-08-11 20:47:22 +00002475 CurDAG->getRegister(0, MVT::i32),
Evan Cheng1526ba52007-01-24 08:53:17 +00002476 CurDAG->getEntryNode()
2477 };
Dan Gohman32f71d72009-09-25 18:54:59 +00002478 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
Michael Liaob53d8962013-04-19 22:22:57 +00002479 Ops);
Evan Cheng1526ba52007-01-24 08:53:17 +00002480 }
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002481 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
Craig Topper062a2ba2014-04-25 05:30:21 +00002482 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00002483 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002484
Evan Cheng10043e22007-01-19 07:51:42 +00002485 // Other cases are autogenerated.
Rafael Espindola4e760152006-06-12 12:28:08 +00002486 break;
Evan Cheng10043e22007-01-19 07:51:42 +00002487 }
Rafael Espindola5f7ab1b2006-11-09 13:58:55 +00002488 case ISD::FrameIndex: {
Evan Cheng10043e22007-01-19 07:51:42 +00002489 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindola5f7ab1b2006-11-09 13:58:55 +00002490 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002491 SDValue TFI = CurDAG->getTargetFrameIndex(FI,
2492 getTargetLowering()->getPointerTy());
David Goodwin22c2fba2009-07-08 23:10:31 +00002493 if (Subtarget->isThumb1Only()) {
Jim Grosbach1b8457a2011-08-24 17:46:13 +00002494 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
2495 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Craig Topper481fb282014-04-27 19:21:11 +00002496 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, Ops);
Jim Grosbachfde21102009-04-07 20:34:09 +00002497 } else {
David Goodwin4ad77972009-07-14 18:48:51 +00002498 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
2499 ARM::t2ADDri : ARM::ADDri);
Owen Anderson9f944592009-08-11 20:47:22 +00002500 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
2501 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2502 CurDAG->getRegister(0, MVT::i32) };
Craig Topper481fb282014-04-27 19:21:11 +00002503 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
Evan Cheng7e90b112007-07-05 07:15:27 +00002504 }
Evan Cheng10043e22007-01-19 07:51:42 +00002505 }
Sandeep Patel423e42b2009-10-13 18:59:48 +00002506 case ISD::SRL:
Jim Grosbach825cb292010-04-22 23:24:18 +00002507 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
Sandeep Patel423e42b2009-10-13 18:59:48 +00002508 return I;
2509 break;
2510 case ISD::SRA:
Jim Grosbach825cb292010-04-22 23:24:18 +00002511 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
Sandeep Patel423e42b2009-10-13 18:59:48 +00002512 return I;
2513 break;
Evan Cheng10043e22007-01-19 07:51:42 +00002514 case ISD::MUL:
Evan Chengb24e51e2009-07-07 01:17:28 +00002515 if (Subtarget->isThumb1Only())
Evan Cheng139edae2007-01-24 02:21:22 +00002516 break;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002517 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002518 unsigned RHSV = C->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00002519 if (!RHSV) break;
2520 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002521 unsigned ShImm = Log2_32(RHSV-1);
2522 if (ShImm >= 32)
2523 break;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002524 SDValue V = N->getOperand(0);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002525 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson9f944592009-08-11 20:47:22 +00002526 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
2527 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng1ec43962009-07-22 18:08:05 +00002528 if (Subtarget->isThumb()) {
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002529 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002530 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002531 } else {
2532 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002533 return CurDAG->SelectNodeTo(N, ARM::ADDrsi, MVT::i32, Ops);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002534 }
Evan Cheng10043e22007-01-19 07:51:42 +00002535 }
2536 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002537 unsigned ShImm = Log2_32(RHSV+1);
2538 if (ShImm >= 32)
2539 break;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002540 SDValue V = N->getOperand(0);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002541 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson9f944592009-08-11 20:47:22 +00002542 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
2543 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng1ec43962009-07-22 18:08:05 +00002544 if (Subtarget->isThumb()) {
Bob Wilsonb6112e82010-05-28 00:27:15 +00002545 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002546 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002547 } else {
2548 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Craig Topper481fb282014-04-27 19:21:11 +00002549 return CurDAG->SelectNodeTo(N, ARM::RSBrsi, MVT::i32, Ops);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002550 }
Evan Cheng10043e22007-01-19 07:51:42 +00002551 }
2552 }
2553 break;
Evan Cheng786b15f2009-10-21 08:15:52 +00002554 case ISD::AND: {
Jim Grosbach825cb292010-04-22 23:24:18 +00002555 // Check for unsigned bitfield extract
2556 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
2557 return I;
2558
Evan Cheng786b15f2009-10-21 08:15:52 +00002559 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
2560 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
2561 // are entirely contributed by c2 and lower 16-bits are entirely contributed
2562 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
2563 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002564 EVT VT = N->getValueType(0);
Evan Cheng786b15f2009-10-21 08:15:52 +00002565 if (VT != MVT::i32)
2566 break;
2567 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
2568 ? ARM::t2MOVTi16
2569 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
2570 if (!Opc)
2571 break;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002572 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Evan Cheng786b15f2009-10-21 08:15:52 +00002573 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2574 if (!N1C)
2575 break;
2576 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
2577 SDValue N2 = N0.getOperand(1);
2578 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2579 if (!N2C)
2580 break;
2581 unsigned N1CVal = N1C->getZExtValue();
2582 unsigned N2CVal = N2C->getZExtValue();
2583 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
2584 (N1CVal & 0xffffU) == 0xffffU &&
2585 (N2CVal & 0xffffU) == 0x0U) {
2586 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
2587 MVT::i32);
2588 SDValue Ops[] = { N0.getOperand(0), Imm16,
2589 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Michael Liaob53d8962013-04-19 22:22:57 +00002590 return CurDAG->getMachineNode(Opc, dl, VT, Ops);
Evan Cheng786b15f2009-10-21 08:15:52 +00002591 }
2592 }
2593 break;
2594 }
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002595 case ARMISD::VMOVRRD:
2596 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002597 N->getOperand(0), getAL(CurDAG),
Dan Gohman32f71d72009-09-25 18:54:59 +00002598 CurDAG->getRegister(0, MVT::i32));
Dan Gohmana1603612007-10-08 18:33:35 +00002599 case ISD::UMUL_LOHI: {
Evan Chengb24e51e2009-07-07 01:17:28 +00002600 if (Subtarget->isThumb1Only())
2601 break;
2602 if (Subtarget->isThumb()) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002603 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Michael Liaob53d8962013-04-19 22:22:57 +00002604 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
2605 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32, Ops);
Evan Chengb24e51e2009-07-07 01:17:28 +00002606 } else {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002607 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson9f944592009-08-11 20:47:22 +00002608 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2609 CurDAG->getRegister(0, MVT::i32) };
Anton Korobeynikov62acecd2011-01-01 20:38:38 +00002610 return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2611 ARM::UMULL : ARM::UMULLv5,
Michael Liaob53d8962013-04-19 22:22:57 +00002612 dl, MVT::i32, MVT::i32, Ops);
Evan Chengb24e51e2009-07-07 01:17:28 +00002613 }
Evan Cheng7e90b112007-07-05 07:15:27 +00002614 }
Dan Gohmana1603612007-10-08 18:33:35 +00002615 case ISD::SMUL_LOHI: {
Evan Chengb24e51e2009-07-07 01:17:28 +00002616 if (Subtarget->isThumb1Only())
2617 break;
2618 if (Subtarget->isThumb()) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002619 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson9f944592009-08-11 20:47:22 +00002620 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Michael Liaob53d8962013-04-19 22:22:57 +00002621 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32, Ops);
Evan Chengb24e51e2009-07-07 01:17:28 +00002622 } else {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002623 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson9f944592009-08-11 20:47:22 +00002624 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2625 CurDAG->getRegister(0, MVT::i32) };
Anton Korobeynikov62acecd2011-01-01 20:38:38 +00002626 return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2627 ARM::SMULL : ARM::SMULLv5,
Michael Liaob53d8962013-04-19 22:22:57 +00002628 dl, MVT::i32, MVT::i32, Ops);
Evan Chengb24e51e2009-07-07 01:17:28 +00002629 }
Evan Cheng7e90b112007-07-05 07:15:27 +00002630 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002631 case ARMISD::UMLAL:{
2632 if (Subtarget->isThumb()) {
2633 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2634 N->getOperand(3), getAL(CurDAG),
2635 CurDAG->getRegister(0, MVT::i32)};
Michael Liaob53d8962013-04-19 22:22:57 +00002636 return CurDAG->getMachineNode(ARM::t2UMLAL, dl, MVT::i32, MVT::i32, Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002637 }else{
2638 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2639 N->getOperand(3), getAL(CurDAG),
2640 CurDAG->getRegister(0, MVT::i32),
2641 CurDAG->getRegister(0, MVT::i32) };
2642 return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2643 ARM::UMLAL : ARM::UMLALv5,
Michael Liaob53d8962013-04-19 22:22:57 +00002644 dl, MVT::i32, MVT::i32, Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002645 }
2646 }
2647 case ARMISD::SMLAL:{
2648 if (Subtarget->isThumb()) {
2649 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2650 N->getOperand(3), getAL(CurDAG),
2651 CurDAG->getRegister(0, MVT::i32)};
Michael Liaob53d8962013-04-19 22:22:57 +00002652 return CurDAG->getMachineNode(ARM::t2SMLAL, dl, MVT::i32, MVT::i32, Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002653 }else{
2654 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2655 N->getOperand(3), getAL(CurDAG),
2656 CurDAG->getRegister(0, MVT::i32),
2657 CurDAG->getRegister(0, MVT::i32) };
2658 return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2659 ARM::SMLAL : ARM::SMLALv5,
Michael Liaob53d8962013-04-19 22:22:57 +00002660 dl, MVT::i32, MVT::i32, Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002661 }
2662 }
Evan Cheng10043e22007-01-19 07:51:42 +00002663 case ISD::LOAD: {
Craig Topper062a2ba2014-04-25 05:30:21 +00002664 SDNode *ResNode = nullptr;
Evan Chengb24e51e2009-07-07 01:17:28 +00002665 if (Subtarget->isThumb() && Subtarget->hasThumb2())
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002666 ResNode = SelectT2IndexedLoad(N);
Evan Cheng84c6cda2009-07-02 07:28:31 +00002667 else
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002668 ResNode = SelectARMIndexedLoad(N);
Evan Chengd9c55362009-07-02 01:23:32 +00002669 if (ResNode)
2670 return ResNode;
Evan Cheng10043e22007-01-19 07:51:42 +00002671 // Other cases are autogenerated.
Rafael Espindola5f7ab1b2006-11-09 13:58:55 +00002672 break;
Rafael Espindola4e760152006-06-12 12:28:08 +00002673 }
Evan Cheng7e90b112007-07-05 07:15:27 +00002674 case ARMISD::BRCOND: {
2675 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2676 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2677 // Pattern complexity = 6 cost = 1 size = 0
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00002678
Evan Cheng7e90b112007-07-05 07:15:27 +00002679 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2680 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
2681 // Pattern complexity = 6 cost = 1 size = 0
2682
David Goodwin27303cd2009-06-30 18:04:13 +00002683 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2684 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2685 // Pattern complexity = 6 cost = 1 size = 0
2686
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002687 unsigned Opc = Subtarget->isThumb() ?
David Goodwin27303cd2009-06-30 18:04:13 +00002688 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002689 SDValue Chain = N->getOperand(0);
2690 SDValue N1 = N->getOperand(1);
2691 SDValue N2 = N->getOperand(2);
2692 SDValue N3 = N->getOperand(3);
2693 SDValue InFlag = N->getOperand(4);
Evan Cheng7e90b112007-07-05 07:15:27 +00002694 assert(N1.getOpcode() == ISD::BasicBlock);
2695 assert(N2.getOpcode() == ISD::Constant);
2696 assert(N3.getOpcode() == ISD::Register);
2697
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002698 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmaneffb8942008-09-12 16:56:44 +00002699 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson9f944592009-08-11 20:47:22 +00002700 MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002701 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
Dan Gohman32f71d72009-09-25 18:54:59 +00002702 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
Michael Liaob53d8962013-04-19 22:22:57 +00002703 MVT::Glue, Ops);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002704 Chain = SDValue(ResNode, 0);
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002705 if (N->getNumValues() == 2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002706 InFlag = SDValue(ResNode, 1);
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002707 ReplaceUses(SDValue(N, 1), InFlag);
Chris Lattnere99faac2008-02-03 03:20:59 +00002708 }
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002709 ReplaceUses(SDValue(N, 0),
Evan Cheng82adca82009-11-19 08:16:50 +00002710 SDValue(Chain.getNode(), Chain.getResNo()));
Craig Topper062a2ba2014-04-25 05:30:21 +00002711 return nullptr;
Evan Cheng7e90b112007-07-05 07:15:27 +00002712 }
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002713 case ARMISD::VZIP: {
2714 unsigned Opc = 0;
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002715 EVT VT = N->getValueType(0);
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002716 switch (VT.getSimpleVT().SimpleTy) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002717 default: return nullptr;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002718 case MVT::v8i8: Opc = ARM::VZIPd8; break;
2719 case MVT::v4i16: Opc = ARM::VZIPd16; break;
2720 case MVT::v2f32:
Jim Grosbach4640c812012-04-11 16:53:25 +00002721 // vzip.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
2722 case MVT::v2i32: Opc = ARM::VTRNd32; break;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002723 case MVT::v16i8: Opc = ARM::VZIPq8; break;
2724 case MVT::v8i16: Opc = ARM::VZIPq16; break;
2725 case MVT::v4f32:
2726 case MVT::v4i32: Opc = ARM::VZIPq32; break;
2727 }
Evan Cheng3da64f762010-04-16 05:46:06 +00002728 SDValue Pred = getAL(CurDAG);
Evan Chenga33fc862009-11-21 06:21:52 +00002729 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2730 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
Michael Liaob53d8962013-04-19 22:22:57 +00002731 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops);
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002732 }
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002733 case ARMISD::VUZP: {
2734 unsigned Opc = 0;
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002735 EVT VT = N->getValueType(0);
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002736 switch (VT.getSimpleVT().SimpleTy) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002737 default: return nullptr;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002738 case MVT::v8i8: Opc = ARM::VUZPd8; break;
2739 case MVT::v4i16: Opc = ARM::VUZPd16; break;
2740 case MVT::v2f32:
Jim Grosbach6e536de2012-04-11 17:40:18 +00002741 // vuzp.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
2742 case MVT::v2i32: Opc = ARM::VTRNd32; break;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002743 case MVT::v16i8: Opc = ARM::VUZPq8; break;
2744 case MVT::v8i16: Opc = ARM::VUZPq16; break;
2745 case MVT::v4f32:
2746 case MVT::v4i32: Opc = ARM::VUZPq32; break;
2747 }
Evan Cheng3da64f762010-04-16 05:46:06 +00002748 SDValue Pred = getAL(CurDAG);
Evan Chenga33fc862009-11-21 06:21:52 +00002749 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2750 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
Michael Liaob53d8962013-04-19 22:22:57 +00002751 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops);
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002752 }
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002753 case ARMISD::VTRN: {
2754 unsigned Opc = 0;
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002755 EVT VT = N->getValueType(0);
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002756 switch (VT.getSimpleVT().SimpleTy) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002757 default: return nullptr;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002758 case MVT::v8i8: Opc = ARM::VTRNd8; break;
2759 case MVT::v4i16: Opc = ARM::VTRNd16; break;
2760 case MVT::v2f32:
2761 case MVT::v2i32: Opc = ARM::VTRNd32; break;
2762 case MVT::v16i8: Opc = ARM::VTRNq8; break;
2763 case MVT::v8i16: Opc = ARM::VTRNq16; break;
2764 case MVT::v4f32:
2765 case MVT::v4i32: Opc = ARM::VTRNq32; break;
2766 }
Evan Cheng3da64f762010-04-16 05:46:06 +00002767 SDValue Pred = getAL(CurDAG);
Evan Chenga33fc862009-11-21 06:21:52 +00002768 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2769 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
Michael Liaob53d8962013-04-19 22:22:57 +00002770 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops);
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002771 }
Bob Wilsond8a9a042010-06-04 00:04:02 +00002772 case ARMISD::BUILD_VECTOR: {
2773 EVT VecVT = N->getValueType(0);
2774 EVT EltVT = VecVT.getVectorElementType();
2775 unsigned NumElts = VecVT.getVectorNumElements();
Duncan Sands14627772010-11-03 12:17:33 +00002776 if (EltVT == MVT::f64) {
Bob Wilsond8a9a042010-06-04 00:04:02 +00002777 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
Weiming Zhao95782222012-11-17 00:23:35 +00002778 return createDRegPairNode(VecVT, N->getOperand(0), N->getOperand(1));
Bob Wilsond8a9a042010-06-04 00:04:02 +00002779 }
Duncan Sands14627772010-11-03 12:17:33 +00002780 assert(EltVT == MVT::f32 && "unexpected type for BUILD_VECTOR");
Bob Wilsond8a9a042010-06-04 00:04:02 +00002781 if (NumElts == 2)
Weiming Zhao95782222012-11-17 00:23:35 +00002782 return createSRegPairNode(VecVT, N->getOperand(0), N->getOperand(1));
Bob Wilsond8a9a042010-06-04 00:04:02 +00002783 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
Weiming Zhao95782222012-11-17 00:23:35 +00002784 return createQuadSRegsNode(VecVT, N->getOperand(0), N->getOperand(1),
Bob Wilsond8a9a042010-06-04 00:04:02 +00002785 N->getOperand(2), N->getOperand(3));
2786 }
Bob Wilsone0636a72009-08-26 17:39:53 +00002787
Bob Wilson2d790df2010-11-28 06:51:26 +00002788 case ARMISD::VLD2DUP: {
Craig Topper01736f82012-05-24 05:17:00 +00002789 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8, ARM::VLD2DUPd16,
2790 ARM::VLD2DUPd32 };
Bob Wilson06fce872011-02-07 17:43:21 +00002791 return SelectVLDDup(N, false, 2, Opcodes);
Bob Wilson2d790df2010-11-28 06:51:26 +00002792 }
2793
Bob Wilson77ab1652010-11-29 19:35:29 +00002794 case ARMISD::VLD3DUP: {
Craig Topper01736f82012-05-24 05:17:00 +00002795 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo,
2796 ARM::VLD3DUPd16Pseudo,
2797 ARM::VLD3DUPd32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00002798 return SelectVLDDup(N, false, 3, Opcodes);
Bob Wilson77ab1652010-11-29 19:35:29 +00002799 }
2800
Bob Wilson431ac4ef2010-11-30 00:00:35 +00002801 case ARMISD::VLD4DUP: {
Craig Topper01736f82012-05-24 05:17:00 +00002802 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo,
2803 ARM::VLD4DUPd16Pseudo,
2804 ARM::VLD4DUPd32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00002805 return SelectVLDDup(N, false, 4, Opcodes);
2806 }
2807
2808 case ARMISD::VLD2DUP_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002809 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8wb_fixed,
2810 ARM::VLD2DUPd16wb_fixed,
2811 ARM::VLD2DUPd32wb_fixed };
Bob Wilson06fce872011-02-07 17:43:21 +00002812 return SelectVLDDup(N, true, 2, Opcodes);
2813 }
2814
2815 case ARMISD::VLD3DUP_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002816 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo_UPD,
2817 ARM::VLD3DUPd16Pseudo_UPD,
2818 ARM::VLD3DUPd32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002819 return SelectVLDDup(N, true, 3, Opcodes);
2820 }
2821
2822 case ARMISD::VLD4DUP_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002823 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo_UPD,
2824 ARM::VLD4DUPd16Pseudo_UPD,
2825 ARM::VLD4DUPd32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002826 return SelectVLDDup(N, true, 4, Opcodes);
2827 }
2828
2829 case ARMISD::VLD1_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002830 static const uint16_t DOpcodes[] = { ARM::VLD1d8wb_fixed,
2831 ARM::VLD1d16wb_fixed,
2832 ARM::VLD1d32wb_fixed,
2833 ARM::VLD1d64wb_fixed };
2834 static const uint16_t QOpcodes[] = { ARM::VLD1q8wb_fixed,
2835 ARM::VLD1q16wb_fixed,
2836 ARM::VLD1q32wb_fixed,
2837 ARM::VLD1q64wb_fixed };
Craig Topper062a2ba2014-04-25 05:30:21 +00002838 return SelectVLD(N, true, 1, DOpcodes, QOpcodes, nullptr);
Bob Wilson06fce872011-02-07 17:43:21 +00002839 }
2840
2841 case ARMISD::VLD2_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002842 static const uint16_t DOpcodes[] = { ARM::VLD2d8wb_fixed,
2843 ARM::VLD2d16wb_fixed,
2844 ARM::VLD2d32wb_fixed,
2845 ARM::VLD1q64wb_fixed};
2846 static const uint16_t QOpcodes[] = { ARM::VLD2q8PseudoWB_fixed,
2847 ARM::VLD2q16PseudoWB_fixed,
2848 ARM::VLD2q32PseudoWB_fixed };
Craig Topper062a2ba2014-04-25 05:30:21 +00002849 return SelectVLD(N, true, 2, DOpcodes, QOpcodes, nullptr);
Bob Wilson06fce872011-02-07 17:43:21 +00002850 }
2851
2852 case ARMISD::VLD3_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002853 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo_UPD,
2854 ARM::VLD3d16Pseudo_UPD,
2855 ARM::VLD3d32Pseudo_UPD,
Jiangning Liu4df23632014-01-16 09:16:13 +00002856 ARM::VLD1d64TPseudoWB_fixed};
Craig Topper01736f82012-05-24 05:17:00 +00002857 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
2858 ARM::VLD3q16Pseudo_UPD,
2859 ARM::VLD3q32Pseudo_UPD };
2860 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
2861 ARM::VLD3q16oddPseudo_UPD,
2862 ARM::VLD3q32oddPseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002863 return SelectVLD(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
2864 }
2865
2866 case ARMISD::VLD4_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002867 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo_UPD,
2868 ARM::VLD4d16Pseudo_UPD,
2869 ARM::VLD4d32Pseudo_UPD,
Jiangning Liu4df23632014-01-16 09:16:13 +00002870 ARM::VLD1d64QPseudoWB_fixed};
Craig Topper01736f82012-05-24 05:17:00 +00002871 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
2872 ARM::VLD4q16Pseudo_UPD,
2873 ARM::VLD4q32Pseudo_UPD };
2874 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
2875 ARM::VLD4q16oddPseudo_UPD,
2876 ARM::VLD4q32oddPseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002877 return SelectVLD(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
2878 }
2879
2880 case ARMISD::VLD2LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002881 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo_UPD,
2882 ARM::VLD2LNd16Pseudo_UPD,
2883 ARM::VLD2LNd32Pseudo_UPD };
2884 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo_UPD,
2885 ARM::VLD2LNq32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002886 return SelectVLDSTLane(N, true, true, 2, DOpcodes, QOpcodes);
2887 }
2888
2889 case ARMISD::VLD3LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002890 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo_UPD,
2891 ARM::VLD3LNd16Pseudo_UPD,
2892 ARM::VLD3LNd32Pseudo_UPD };
2893 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo_UPD,
2894 ARM::VLD3LNq32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002895 return SelectVLDSTLane(N, true, true, 3, DOpcodes, QOpcodes);
2896 }
2897
2898 case ARMISD::VLD4LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002899 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo_UPD,
2900 ARM::VLD4LNd16Pseudo_UPD,
2901 ARM::VLD4LNd32Pseudo_UPD };
2902 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo_UPD,
2903 ARM::VLD4LNq32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002904 return SelectVLDSTLane(N, true, true, 4, DOpcodes, QOpcodes);
2905 }
2906
2907 case ARMISD::VST1_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002908 static const uint16_t DOpcodes[] = { ARM::VST1d8wb_fixed,
2909 ARM::VST1d16wb_fixed,
2910 ARM::VST1d32wb_fixed,
2911 ARM::VST1d64wb_fixed };
2912 static const uint16_t QOpcodes[] = { ARM::VST1q8wb_fixed,
2913 ARM::VST1q16wb_fixed,
2914 ARM::VST1q32wb_fixed,
2915 ARM::VST1q64wb_fixed };
Craig Topper062a2ba2014-04-25 05:30:21 +00002916 return SelectVST(N, true, 1, DOpcodes, QOpcodes, nullptr);
Bob Wilson06fce872011-02-07 17:43:21 +00002917 }
2918
2919 case ARMISD::VST2_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002920 static const uint16_t DOpcodes[] = { ARM::VST2d8wb_fixed,
2921 ARM::VST2d16wb_fixed,
2922 ARM::VST2d32wb_fixed,
2923 ARM::VST1q64wb_fixed};
2924 static const uint16_t QOpcodes[] = { ARM::VST2q8PseudoWB_fixed,
2925 ARM::VST2q16PseudoWB_fixed,
2926 ARM::VST2q32PseudoWB_fixed };
Craig Topper062a2ba2014-04-25 05:30:21 +00002927 return SelectVST(N, true, 2, DOpcodes, QOpcodes, nullptr);
Bob Wilson06fce872011-02-07 17:43:21 +00002928 }
2929
2930 case ARMISD::VST3_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002931 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo_UPD,
2932 ARM::VST3d16Pseudo_UPD,
2933 ARM::VST3d32Pseudo_UPD,
2934 ARM::VST1d64TPseudoWB_fixed};
2935 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2936 ARM::VST3q16Pseudo_UPD,
2937 ARM::VST3q32Pseudo_UPD };
2938 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2939 ARM::VST3q16oddPseudo_UPD,
2940 ARM::VST3q32oddPseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002941 return SelectVST(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
2942 }
2943
2944 case ARMISD::VST4_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002945 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo_UPD,
2946 ARM::VST4d16Pseudo_UPD,
2947 ARM::VST4d32Pseudo_UPD,
2948 ARM::VST1d64QPseudoWB_fixed};
2949 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2950 ARM::VST4q16Pseudo_UPD,
2951 ARM::VST4q32Pseudo_UPD };
2952 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2953 ARM::VST4q16oddPseudo_UPD,
2954 ARM::VST4q32oddPseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002955 return SelectVST(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
2956 }
2957
2958 case ARMISD::VST2LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002959 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo_UPD,
2960 ARM::VST2LNd16Pseudo_UPD,
2961 ARM::VST2LNd32Pseudo_UPD };
2962 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo_UPD,
2963 ARM::VST2LNq32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002964 return SelectVLDSTLane(N, false, true, 2, DOpcodes, QOpcodes);
2965 }
2966
2967 case ARMISD::VST3LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002968 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo_UPD,
2969 ARM::VST3LNd16Pseudo_UPD,
2970 ARM::VST3LNd32Pseudo_UPD };
2971 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo_UPD,
2972 ARM::VST3LNq32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002973 return SelectVLDSTLane(N, false, true, 3, DOpcodes, QOpcodes);
2974 }
2975
2976 case ARMISD::VST4LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00002977 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo_UPD,
2978 ARM::VST4LNd16Pseudo_UPD,
2979 ARM::VST4LNd32Pseudo_UPD };
2980 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo_UPD,
2981 ARM::VST4LNq32Pseudo_UPD };
Bob Wilson06fce872011-02-07 17:43:21 +00002982 return SelectVLDSTLane(N, false, true, 4, DOpcodes, QOpcodes);
Bob Wilson431ac4ef2010-11-30 00:00:35 +00002983 }
2984
Bob Wilsone0636a72009-08-26 17:39:53 +00002985 case ISD::INTRINSIC_VOID:
2986 case ISD::INTRINSIC_W_CHAIN: {
2987 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Bob Wilsone0636a72009-08-26 17:39:53 +00002988 switch (IntNo) {
2989 default:
Bob Wilsonf765e1f2010-05-06 16:05:26 +00002990 break;
Bob Wilsone0636a72009-08-26 17:39:53 +00002991
Tim Northover1ff5f292014-03-26 14:39:31 +00002992 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00002993 case Intrinsic::arm_ldrexd: {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002994 SDLoc dl(N);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00002995 SDValue Chain = N->getOperand(0);
Tim Northover1ff5f292014-03-26 14:39:31 +00002996 SDValue MemAddr = N->getOperand(2);
Weiming Zhao8f56f882012-11-16 21:55:34 +00002997 bool isThumb = Subtarget->isThumb() && Subtarget->hasThumb2();
Tim Northover1ff5f292014-03-26 14:39:31 +00002998
2999 bool IsAcquire = IntNo == Intrinsic::arm_ldaexd;
3000 unsigned NewOpc = isThumb ? (IsAcquire ? ARM::t2LDAEXD : ARM::t2LDREXD)
3001 : (IsAcquire ? ARM::LDAEXD : ARM::LDREXD);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003002
3003 // arm_ldrexd returns a i64 value in {i32, i32}
3004 std::vector<EVT> ResTys;
Weiming Zhao8f56f882012-11-16 21:55:34 +00003005 if (isThumb) {
3006 ResTys.push_back(MVT::i32);
3007 ResTys.push_back(MVT::i32);
3008 } else
3009 ResTys.push_back(MVT::Untyped);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003010 ResTys.push_back(MVT::Other);
3011
Weiming Zhao8f56f882012-11-16 21:55:34 +00003012 // Place arguments in the right order.
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003013 SmallVector<SDValue, 7> Ops;
3014 Ops.push_back(MemAddr);
3015 Ops.push_back(getAL(CurDAG));
3016 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3017 Ops.push_back(Chain);
Michael Liaob53d8962013-04-19 22:22:57 +00003018 SDNode *Ld = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003019 // Transfer memoperands.
3020 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3021 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3022 cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1);
3023
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003024 // Remap uses.
Lang Hamesbe3d9712013-03-09 22:56:09 +00003025 SDValue OutChain = isThumb ? SDValue(Ld, 2) : SDValue(Ld, 1);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003026 if (!SDValue(N, 0).use_empty()) {
Weiming Zhao8f56f882012-11-16 21:55:34 +00003027 SDValue Result;
3028 if (isThumb)
3029 Result = SDValue(Ld, 0);
3030 else {
3031 SDValue SubRegIdx = CurDAG->getTargetConstant(ARM::gsub_0, MVT::i32);
3032 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Lang Hamesbe3d9712013-03-09 22:56:09 +00003033 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003034 Result = SDValue(ResNode,0);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003035 }
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003036 ReplaceUses(SDValue(N, 0), Result);
3037 }
3038 if (!SDValue(N, 1).use_empty()) {
Weiming Zhao8f56f882012-11-16 21:55:34 +00003039 SDValue Result;
3040 if (isThumb)
3041 Result = SDValue(Ld, 1);
3042 else {
3043 SDValue SubRegIdx = CurDAG->getTargetConstant(ARM::gsub_1, MVT::i32);
3044 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Lang Hamesbe3d9712013-03-09 22:56:09 +00003045 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003046 Result = SDValue(ResNode,0);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003047 }
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003048 ReplaceUses(SDValue(N, 1), Result);
3049 }
Lang Hamesbe3d9712013-03-09 22:56:09 +00003050 ReplaceUses(SDValue(N, 2), OutChain);
Craig Topper062a2ba2014-04-25 05:30:21 +00003051 return nullptr;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003052 }
Tim Northover1ff5f292014-03-26 14:39:31 +00003053 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003054 case Intrinsic::arm_strexd: {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003055 SDLoc dl(N);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003056 SDValue Chain = N->getOperand(0);
3057 SDValue Val0 = N->getOperand(2);
3058 SDValue Val1 = N->getOperand(3);
3059 SDValue MemAddr = N->getOperand(4);
3060
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003061 // Store exclusive double return a i32 value which is the return status
3062 // of the issued store.
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00003063 EVT ResTys[] = { MVT::i32, MVT::Other };
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003064
Weiming Zhao8f56f882012-11-16 21:55:34 +00003065 bool isThumb = Subtarget->isThumb() && Subtarget->hasThumb2();
3066 // Place arguments in the right order.
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003067 SmallVector<SDValue, 7> Ops;
Weiming Zhao8f56f882012-11-16 21:55:34 +00003068 if (isThumb) {
3069 Ops.push_back(Val0);
3070 Ops.push_back(Val1);
3071 } else
3072 // arm_strexd uses GPRPair.
3073 Ops.push_back(SDValue(createGPRPairNode(MVT::Untyped, Val0, Val1), 0));
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003074 Ops.push_back(MemAddr);
3075 Ops.push_back(getAL(CurDAG));
3076 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3077 Ops.push_back(Chain);
3078
Tim Northover1ff5f292014-03-26 14:39:31 +00003079 bool IsRelease = IntNo == Intrinsic::arm_stlexd;
3080 unsigned NewOpc = isThumb ? (IsRelease ? ARM::t2STLEXD : ARM::t2STREXD)
3081 : (IsRelease ? ARM::STLEXD : ARM::STREXD);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003082
Michael Liaob53d8962013-04-19 22:22:57 +00003083 SDNode *St = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003084 // Transfer memoperands.
3085 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3086 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3087 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
3088
3089 return St;
3090 }
3091
Bob Wilson340861d2010-03-23 05:25:43 +00003092 case Intrinsic::arm_neon_vld1: {
Craig Topper01736f82012-05-24 05:17:00 +00003093 static const uint16_t DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
3094 ARM::VLD1d32, ARM::VLD1d64 };
3095 static const uint16_t QOpcodes[] = { ARM::VLD1q8, ARM::VLD1q16,
3096 ARM::VLD1q32, ARM::VLD1q64};
Craig Topper062a2ba2014-04-25 05:30:21 +00003097 return SelectVLD(N, false, 1, DOpcodes, QOpcodes, nullptr);
Bob Wilson340861d2010-03-23 05:25:43 +00003098 }
3099
Bob Wilsone0636a72009-08-26 17:39:53 +00003100 case Intrinsic::arm_neon_vld2: {
Craig Topper01736f82012-05-24 05:17:00 +00003101 static const uint16_t DOpcodes[] = { ARM::VLD2d8, ARM::VLD2d16,
3102 ARM::VLD2d32, ARM::VLD1q64 };
3103 static const uint16_t QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
3104 ARM::VLD2q32Pseudo };
Craig Topper062a2ba2014-04-25 05:30:21 +00003105 return SelectVLD(N, false, 2, DOpcodes, QOpcodes, nullptr);
Bob Wilsone0636a72009-08-26 17:39:53 +00003106 }
3107
3108 case Intrinsic::arm_neon_vld3: {
Craig Topper01736f82012-05-24 05:17:00 +00003109 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo,
3110 ARM::VLD3d16Pseudo,
3111 ARM::VLD3d32Pseudo,
3112 ARM::VLD1d64TPseudo };
3113 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
3114 ARM::VLD3q16Pseudo_UPD,
3115 ARM::VLD3q32Pseudo_UPD };
3116 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo,
3117 ARM::VLD3q16oddPseudo,
3118 ARM::VLD3q32oddPseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003119 return SelectVLD(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilsone0636a72009-08-26 17:39:53 +00003120 }
3121
3122 case Intrinsic::arm_neon_vld4: {
Craig Topper01736f82012-05-24 05:17:00 +00003123 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo,
3124 ARM::VLD4d16Pseudo,
3125 ARM::VLD4d32Pseudo,
3126 ARM::VLD1d64QPseudo };
3127 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
3128 ARM::VLD4q16Pseudo_UPD,
3129 ARM::VLD4q32Pseudo_UPD };
3130 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo,
3131 ARM::VLD4q16oddPseudo,
3132 ARM::VLD4q32oddPseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003133 return SelectVLD(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilsone0636a72009-08-26 17:39:53 +00003134 }
3135
Bob Wilsonda9817c2009-09-01 04:26:28 +00003136 case Intrinsic::arm_neon_vld2lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003137 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo,
3138 ARM::VLD2LNd16Pseudo,
3139 ARM::VLD2LNd32Pseudo };
3140 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo,
3141 ARM::VLD2LNq32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003142 return SelectVLDSTLane(N, true, false, 2, DOpcodes, QOpcodes);
Bob Wilsonda9817c2009-09-01 04:26:28 +00003143 }
3144
3145 case Intrinsic::arm_neon_vld3lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003146 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo,
3147 ARM::VLD3LNd16Pseudo,
3148 ARM::VLD3LNd32Pseudo };
3149 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo,
3150 ARM::VLD3LNq32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003151 return SelectVLDSTLane(N, true, false, 3, DOpcodes, QOpcodes);
Bob Wilsonda9817c2009-09-01 04:26:28 +00003152 }
3153
3154 case Intrinsic::arm_neon_vld4lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003155 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo,
3156 ARM::VLD4LNd16Pseudo,
3157 ARM::VLD4LNd32Pseudo };
3158 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo,
3159 ARM::VLD4LNq32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003160 return SelectVLDSTLane(N, true, false, 4, DOpcodes, QOpcodes);
Bob Wilsonda9817c2009-09-01 04:26:28 +00003161 }
3162
Bob Wilsoncc0a2a72010-03-23 06:20:33 +00003163 case Intrinsic::arm_neon_vst1: {
Craig Topper01736f82012-05-24 05:17:00 +00003164 static const uint16_t DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
3165 ARM::VST1d32, ARM::VST1d64 };
3166 static const uint16_t QOpcodes[] = { ARM::VST1q8, ARM::VST1q16,
3167 ARM::VST1q32, ARM::VST1q64 };
Craig Topper062a2ba2014-04-25 05:30:21 +00003168 return SelectVST(N, false, 1, DOpcodes, QOpcodes, nullptr);
Bob Wilsoncc0a2a72010-03-23 06:20:33 +00003169 }
3170
Bob Wilsone0636a72009-08-26 17:39:53 +00003171 case Intrinsic::arm_neon_vst2: {
Craig Topper01736f82012-05-24 05:17:00 +00003172 static const uint16_t DOpcodes[] = { ARM::VST2d8, ARM::VST2d16,
3173 ARM::VST2d32, ARM::VST1q64 };
3174 static uint16_t QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
3175 ARM::VST2q32Pseudo };
Craig Topper062a2ba2014-04-25 05:30:21 +00003176 return SelectVST(N, false, 2, DOpcodes, QOpcodes, nullptr);
Bob Wilsone0636a72009-08-26 17:39:53 +00003177 }
3178
3179 case Intrinsic::arm_neon_vst3: {
Craig Topper01736f82012-05-24 05:17:00 +00003180 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo,
3181 ARM::VST3d16Pseudo,
3182 ARM::VST3d32Pseudo,
3183 ARM::VST1d64TPseudo };
3184 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
3185 ARM::VST3q16Pseudo_UPD,
3186 ARM::VST3q32Pseudo_UPD };
3187 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo,
3188 ARM::VST3q16oddPseudo,
3189 ARM::VST3q32oddPseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003190 return SelectVST(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilsone0636a72009-08-26 17:39:53 +00003191 }
3192
3193 case Intrinsic::arm_neon_vst4: {
Craig Topper01736f82012-05-24 05:17:00 +00003194 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo,
3195 ARM::VST4d16Pseudo,
3196 ARM::VST4d32Pseudo,
3197 ARM::VST1d64QPseudo };
3198 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
3199 ARM::VST4q16Pseudo_UPD,
3200 ARM::VST4q32Pseudo_UPD };
3201 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo,
3202 ARM::VST4q16oddPseudo,
3203 ARM::VST4q32oddPseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003204 return SelectVST(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilsone0636a72009-08-26 17:39:53 +00003205 }
Bob Wilsond7797752009-09-01 18:51:56 +00003206
3207 case Intrinsic::arm_neon_vst2lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003208 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo,
3209 ARM::VST2LNd16Pseudo,
3210 ARM::VST2LNd32Pseudo };
3211 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo,
3212 ARM::VST2LNq32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003213 return SelectVLDSTLane(N, false, false, 2, DOpcodes, QOpcodes);
Bob Wilsond7797752009-09-01 18:51:56 +00003214 }
3215
3216 case Intrinsic::arm_neon_vst3lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003217 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo,
3218 ARM::VST3LNd16Pseudo,
3219 ARM::VST3LNd32Pseudo };
3220 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo,
3221 ARM::VST3LNq32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003222 return SelectVLDSTLane(N, false, false, 3, DOpcodes, QOpcodes);
Bob Wilsond7797752009-09-01 18:51:56 +00003223 }
3224
3225 case Intrinsic::arm_neon_vst4lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003226 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo,
3227 ARM::VST4LNd16Pseudo,
3228 ARM::VST4LNd32Pseudo };
3229 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo,
3230 ARM::VST4LNq32Pseudo };
Bob Wilson06fce872011-02-07 17:43:21 +00003231 return SelectVLDSTLane(N, false, false, 4, DOpcodes, QOpcodes);
Bob Wilsond7797752009-09-01 18:51:56 +00003232 }
Bob Wilsone0636a72009-08-26 17:39:53 +00003233 }
Bob Wilsonf765e1f2010-05-06 16:05:26 +00003234 break;
Bob Wilsone0636a72009-08-26 17:39:53 +00003235 }
Evan Chengd85631e2010-05-05 18:28:36 +00003236
Bob Wilson3ed511b2010-07-06 23:36:25 +00003237 case ISD::INTRINSIC_WO_CHAIN: {
3238 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3239 switch (IntNo) {
3240 default:
3241 break;
3242
3243 case Intrinsic::arm_neon_vtbl2:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003244 return SelectVTBL(N, false, 2, ARM::VTBL2);
Bob Wilson3ed511b2010-07-06 23:36:25 +00003245 case Intrinsic::arm_neon_vtbl3:
Bob Wilsonc597fd3b2010-09-13 23:55:10 +00003246 return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
Bob Wilson3ed511b2010-07-06 23:36:25 +00003247 case Intrinsic::arm_neon_vtbl4:
Bob Wilsonc597fd3b2010-09-13 23:55:10 +00003248 return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
Bob Wilson5bc8a792010-07-07 00:08:54 +00003249
3250 case Intrinsic::arm_neon_vtbx2:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003251 return SelectVTBL(N, true, 2, ARM::VTBX2);
Bob Wilson5bc8a792010-07-07 00:08:54 +00003252 case Intrinsic::arm_neon_vtbx3:
Bob Wilsonc597fd3b2010-09-13 23:55:10 +00003253 return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
Bob Wilson5bc8a792010-07-07 00:08:54 +00003254 case Intrinsic::arm_neon_vtbx4:
Bob Wilsonc597fd3b2010-09-13 23:55:10 +00003255 return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
Bob Wilson3ed511b2010-07-06 23:36:25 +00003256 }
3257 break;
3258 }
3259
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00003260 case ARMISD::VTBL1: {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003261 SDLoc dl(N);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00003262 EVT VT = N->getValueType(0);
3263 SmallVector<SDValue, 6> Ops;
3264
3265 Ops.push_back(N->getOperand(0));
3266 Ops.push_back(N->getOperand(1));
3267 Ops.push_back(getAL(CurDAG)); // Predicate
3268 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // Predicate Register
Michael Liaob53d8962013-04-19 22:22:57 +00003269 return CurDAG->getMachineNode(ARM::VTBL1, dl, VT, Ops);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00003270 }
3271 case ARMISD::VTBL2: {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003272 SDLoc dl(N);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00003273 EVT VT = N->getValueType(0);
3274
3275 // Form a REG_SEQUENCE to force register allocation.
3276 SDValue V0 = N->getOperand(0);
3277 SDValue V1 = N->getOperand(1);
Weiming Zhao95782222012-11-17 00:23:35 +00003278 SDValue RegSeq = SDValue(createDRegPairNode(MVT::v16i8, V0, V1), 0);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00003279
3280 SmallVector<SDValue, 6> Ops;
3281 Ops.push_back(RegSeq);
3282 Ops.push_back(N->getOperand(2));
3283 Ops.push_back(getAL(CurDAG)); // Predicate
3284 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // Predicate Register
Michael Liaob53d8962013-04-19 22:22:57 +00003285 return CurDAG->getMachineNode(ARM::VTBL2, dl, VT, Ops);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00003286 }
3287
Bob Wilsonf765e1f2010-05-06 16:05:26 +00003288 case ISD::CONCAT_VECTORS:
Evan Chengd85631e2010-05-05 18:28:36 +00003289 return SelectConcatVector(N);
3290 }
Evan Chengd5021732008-12-10 21:54:21 +00003291
Dan Gohmanea6f91f2010-01-05 01:24:18 +00003292 return SelectCode(N);
Evan Cheng10043e22007-01-19 07:51:42 +00003293}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00003294
Weiming Zhaoc5987002013-02-14 18:10:21 +00003295SDNode *ARMDAGToDAGISel::SelectInlineAsm(SDNode *N){
3296 std::vector<SDValue> AsmNodeOperands;
3297 unsigned Flag, Kind;
3298 bool Changed = false;
3299 unsigned NumOps = N->getNumOperands();
3300
Weiming Zhaoc5987002013-02-14 18:10:21 +00003301 // Normally, i64 data is bounded to two arbitrary GRPs for "%r" constraint.
3302 // However, some instrstions (e.g. ldrexd/strexd in ARM mode) require
3303 // (even/even+1) GPRs and use %n and %Hn to refer to the individual regs
3304 // respectively. Since there is no constraint to explicitly specify a
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003305 // reg pair, we use GPRPair reg class for "%r" for 64-bit data. For Thumb,
3306 // the 64-bit data may be referred by H, Q, R modifiers, so we still pack
3307 // them into a GPRPair.
Weiming Zhaoc5987002013-02-14 18:10:21 +00003308
Andrew Trickef9de2a2013-05-25 02:42:55 +00003309 SDLoc dl(N);
Craig Topper062a2ba2014-04-25 05:30:21 +00003310 SDValue Glue = N->getGluedNode() ? N->getOperand(NumOps-1)
3311 : SDValue(nullptr,0);
Weiming Zhaoc5987002013-02-14 18:10:21 +00003312
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003313 SmallVector<bool, 8> OpChanged;
Weiming Zhaoc5987002013-02-14 18:10:21 +00003314 // Glue node will be appended late.
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003315 for(unsigned i = 0, e = N->getGluedNode() ? NumOps - 1 : NumOps; i < e; ++i) {
Weiming Zhaoc5987002013-02-14 18:10:21 +00003316 SDValue op = N->getOperand(i);
3317 AsmNodeOperands.push_back(op);
3318
3319 if (i < InlineAsm::Op_FirstOperand)
3320 continue;
3321
3322 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(i))) {
3323 Flag = C->getZExtValue();
3324 Kind = InlineAsm::getKind(Flag);
3325 }
3326 else
3327 continue;
3328
Joey Gouly392cdad2013-07-08 19:52:51 +00003329 // Immediate operands to inline asm in the SelectionDAG are modeled with
3330 // two operands. The first is a constant of value InlineAsm::Kind_Imm, and
3331 // the second is a constant with the value of the immediate. If we get here
3332 // and we have a Kind_Imm, skip the next operand, and continue.
Joey Gouly606f3fb2013-07-05 10:19:40 +00003333 if (Kind == InlineAsm::Kind_Imm) {
3334 SDValue op = N->getOperand(++i);
3335 AsmNodeOperands.push_back(op);
3336 continue;
3337 }
3338
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003339 unsigned NumRegs = InlineAsm::getNumOperandRegisters(Flag);
3340 if (NumRegs)
3341 OpChanged.push_back(false);
3342
3343 unsigned DefIdx = 0;
3344 bool IsTiedToChangedOp = false;
3345 // If it's a use that is tied with a previous def, it has no
3346 // reg class constraint.
3347 if (Changed && InlineAsm::isUseOperandTiedToDef(Flag, DefIdx))
3348 IsTiedToChangedOp = OpChanged[DefIdx];
3349
Weiming Zhaoc5987002013-02-14 18:10:21 +00003350 if (Kind != InlineAsm::Kind_RegUse && Kind != InlineAsm::Kind_RegDef
3351 && Kind != InlineAsm::Kind_RegDefEarlyClobber)
3352 continue;
3353
Weiming Zhaoc5987002013-02-14 18:10:21 +00003354 unsigned RC;
3355 bool HasRC = InlineAsm::hasRegClassConstraint(Flag, RC);
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003356 if ((!IsTiedToChangedOp && (!HasRC || RC != ARM::GPRRegClassID))
3357 || NumRegs != 2)
Weiming Zhaoc5987002013-02-14 18:10:21 +00003358 continue;
3359
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003360 assert((i+2 < NumOps) && "Invalid number of operands in inline asm");
Weiming Zhaoc5987002013-02-14 18:10:21 +00003361 SDValue V0 = N->getOperand(i+1);
3362 SDValue V1 = N->getOperand(i+2);
3363 unsigned Reg0 = cast<RegisterSDNode>(V0)->getReg();
3364 unsigned Reg1 = cast<RegisterSDNode>(V1)->getReg();
3365 SDValue PairedReg;
3366 MachineRegisterInfo &MRI = MF->getRegInfo();
3367
3368 if (Kind == InlineAsm::Kind_RegDef ||
3369 Kind == InlineAsm::Kind_RegDefEarlyClobber) {
3370 // Replace the two GPRs with 1 GPRPair and copy values from GPRPair to
3371 // the original GPRs.
3372
3373 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
3374 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
3375 SDValue Chain = SDValue(N,0);
3376
3377 SDNode *GU = N->getGluedUser();
3378 SDValue RegCopy = CurDAG->getCopyFromReg(Chain, dl, GPVR, MVT::Untyped,
3379 Chain.getValue(1));
3380
3381 // Extract values from a GPRPair reg and copy to the original GPR reg.
3382 SDValue Sub0 = CurDAG->getTargetExtractSubreg(ARM::gsub_0, dl, MVT::i32,
3383 RegCopy);
3384 SDValue Sub1 = CurDAG->getTargetExtractSubreg(ARM::gsub_1, dl, MVT::i32,
3385 RegCopy);
3386 SDValue T0 = CurDAG->getCopyToReg(Sub0, dl, Reg0, Sub0,
3387 RegCopy.getValue(1));
3388 SDValue T1 = CurDAG->getCopyToReg(Sub1, dl, Reg1, Sub1, T0.getValue(1));
3389
3390 // Update the original glue user.
3391 std::vector<SDValue> Ops(GU->op_begin(), GU->op_end()-1);
3392 Ops.push_back(T1.getValue(1));
Craig Topper8c0b4d02014-04-28 05:57:50 +00003393 CurDAG->UpdateNodeOperands(GU, Ops);
Weiming Zhaoc5987002013-02-14 18:10:21 +00003394 GU = T1.getNode();
3395 }
3396 else {
3397 // For Kind == InlineAsm::Kind_RegUse, we first copy two GPRs into a
3398 // GPRPair and then pass the GPRPair to the inline asm.
3399 SDValue Chain = AsmNodeOperands[InlineAsm::Op_InputChain];
3400
3401 // As REG_SEQ doesn't take RegisterSDNode, we copy them first.
3402 SDValue T0 = CurDAG->getCopyFromReg(Chain, dl, Reg0, MVT::i32,
3403 Chain.getValue(1));
3404 SDValue T1 = CurDAG->getCopyFromReg(Chain, dl, Reg1, MVT::i32,
3405 T0.getValue(1));
3406 SDValue Pair = SDValue(createGPRPairNode(MVT::Untyped, T0, T1), 0);
3407
3408 // Copy REG_SEQ into a GPRPair-typed VR and replace the original two
3409 // i32 VRs of inline asm with it.
3410 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
3411 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
3412 Chain = CurDAG->getCopyToReg(T1, dl, GPVR, Pair, T1.getValue(1));
3413
3414 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
3415 Glue = Chain.getValue(1);
3416 }
3417
3418 Changed = true;
3419
3420 if(PairedReg.getNode()) {
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003421 OpChanged[OpChanged.size() -1 ] = true;
Weiming Zhaoc5987002013-02-14 18:10:21 +00003422 Flag = InlineAsm::getFlagWord(Kind, 1 /* RegNum*/);
Tim Northover55349a22013-08-18 18:06:03 +00003423 if (IsTiedToChangedOp)
3424 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, DefIdx);
3425 else
3426 Flag = InlineAsm::getFlagWordForRegClass(Flag, ARM::GPRPairRegClassID);
Weiming Zhaoc5987002013-02-14 18:10:21 +00003427 // Replace the current flag.
3428 AsmNodeOperands[AsmNodeOperands.size() -1] = CurDAG->getTargetConstant(
3429 Flag, MVT::i32);
3430 // Add the new register node and skip the original two GPRs.
3431 AsmNodeOperands.push_back(PairedReg);
3432 // Skip the next two GPRs.
3433 i += 2;
3434 }
3435 }
3436
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003437 if (Glue.getNode())
3438 AsmNodeOperands.push_back(Glue);
Weiming Zhaoc5987002013-02-14 18:10:21 +00003439 if (!Changed)
Craig Topper062a2ba2014-04-25 05:30:21 +00003440 return nullptr;
Weiming Zhaoc5987002013-02-14 18:10:21 +00003441
Andrew Trickef9de2a2013-05-25 02:42:55 +00003442 SDValue New = CurDAG->getNode(ISD::INLINEASM, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00003443 CurDAG->getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
Weiming Zhaoc5987002013-02-14 18:10:21 +00003444 New->setNodeId(-1);
3445 return New.getNode();
3446}
3447
3448
Bob Wilsona2c462b2009-05-19 05:53:42 +00003449bool ARMDAGToDAGISel::
3450SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
3451 std::vector<SDValue> &OutOps) {
3452 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
Bob Wilson3b515602009-10-13 20:50:28 +00003453 // Require the address to be in a register. That is safe for all ARM
3454 // variants and it is hard to do anything much smarter without knowing
3455 // how the operand is used.
3456 OutOps.push_back(Op);
Bob Wilsona2c462b2009-05-19 05:53:42 +00003457 return false;
3458}
3459
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00003460/// createARMISelDag - This pass converts a legalized DAG into a
3461/// ARM-specific DAG, ready for instruction scheduling.
3462///
Bob Wilson2dd957f2009-09-28 14:30:20 +00003463FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
3464 CodeGenOpt::Level OptLevel) {
3465 return new ARMDAGToDAGISel(TM, OptLevel);
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00003466}