blob: c84d3ff813248d7a21e09f5ed5a7d5531112246d [file] [log] [blame]
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001//===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Espindola7bc59bc2006-05-14 22:18:28 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
Dale Johannesen51e28e62010-06-03 21:09:53 +000014#define DEBUG_TYPE "arm-isel"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000015#include "ARM.h"
Evan Chenge5ad88e2008-12-10 21:54:21 +000016#include "ARMAddressingModes.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000017#include "ARMTargetMachine.h"
Rafael Espindola84b19be2006-07-16 01:02:57 +000018#include "llvm/CallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000020#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/Intrinsics.h"
Owen Anderson9adc0ab2009-07-14 23:09:55 +000023#include "llvm/LLVMContext.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/SelectionDAG.h"
28#include "llvm/CodeGen/SelectionDAGISel.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000029#include "llvm/Target/TargetLowering.h"
Chris Lattner72939122007-05-03 00:32:00 +000030#include "llvm/Target/TargetOptions.h"
Evan Cheng94cc6d32010-05-04 20:39:49 +000031#include "llvm/Support/CommandLine.h"
Chris Lattner3d62d782008-02-03 05:43:57 +000032#include "llvm/Support/Compiler.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000033#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
36
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000037using namespace llvm;
38
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000039//===--------------------------------------------------------------------===//
40/// ARMDAGToDAGISel - ARM specific code to select ARM machine
41/// instructions for SelectionDAG operations.
42///
43namespace {
44class ARMDAGToDAGISel : public SelectionDAGISel {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000045 ARMBaseTargetMachine &TM;
Evan Cheng3f7eb8e2008-09-18 07:24:33 +000046
Evan Chenga8e29892007-01-19 07:51:42 +000047 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
48 /// make the right decision when generating code for different targets.
49 const ARMSubtarget *Subtarget;
50
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000051public:
Bob Wilson522ce972009-09-28 14:30:20 +000052 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm,
53 CodeGenOpt::Level OptLevel)
54 : SelectionDAGISel(tm, OptLevel), TM(tm),
Evan Chenga8e29892007-01-19 07:51:42 +000055 Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000056 }
57
Evan Chenga8e29892007-01-19 07:51:42 +000058 virtual const char *getPassName() const {
59 return "ARM Instruction Selection";
Anton Korobeynikov52237112009-06-17 18:13:58 +000060 }
61
Bob Wilsonaf4a8912009-10-08 18:51:31 +000062 /// getI32Imm - Return a target constant of type i32 with the specified
63 /// value.
Anton Korobeynikov52237112009-06-17 18:13:58 +000064 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +000065 return CurDAG->getTargetConstant(Imm, MVT::i32);
Anton Korobeynikov52237112009-06-17 18:13:58 +000066 }
67
Dan Gohmaneeb3a002010-01-05 01:24:18 +000068 SDNode *Select(SDNode *N);
Evan Cheng014bf212010-02-15 19:41:07 +000069
Dan Gohmaneeb3a002010-01-05 01:24:18 +000070 bool SelectShifterOperandReg(SDNode *Op, SDValue N, SDValue &A,
Evan Cheng055b0312009-06-29 07:51:04 +000071 SDValue &B, SDValue &C);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000072 bool SelectAddrMode2(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000073 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000074 bool SelectAddrMode2Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +000075 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000076 bool SelectAddrMode3(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000077 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000078 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +000079 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000080 bool SelectAddrMode4(SDNode *Op, SDValue N, SDValue &Addr,
Anton Korobeynikovbaf31082009-08-08 13:35:48 +000081 SDValue &Mode);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000082 bool SelectAddrMode5(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000083 SDValue &Offset);
Bob Wilson226036e2010-03-20 22:13:40 +000084 bool SelectAddrMode6(SDNode *Op, SDValue N, SDValue &Addr, SDValue &Align);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000085
Dan Gohmaneeb3a002010-01-05 01:24:18 +000086 bool SelectAddrModePC(SDNode *Op, SDValue N, SDValue &Offset,
Bob Wilson8b024a52009-07-01 23:16:05 +000087 SDValue &Label);
Evan Chenga8e29892007-01-19 07:51:42 +000088
Dan Gohmaneeb3a002010-01-05 01:24:18 +000089 bool SelectThumbAddrModeRR(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000090 SDValue &Offset);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000091 bool SelectThumbAddrModeRI5(SDNode *Op, SDValue N, unsigned Scale,
Dan Gohman475871a2008-07-27 21:46:04 +000092 SDValue &Base, SDValue &OffImm,
93 SDValue &Offset);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000094 bool SelectThumbAddrModeS1(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000095 SDValue &OffImm, SDValue &Offset);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000096 bool SelectThumbAddrModeS2(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000097 SDValue &OffImm, SDValue &Offset);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000098 bool SelectThumbAddrModeS4(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000099 SDValue &OffImm, SDValue &Offset);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000100 bool SelectThumbAddrModeSP(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000101 SDValue &OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000102
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000103 bool SelectT2ShifterOperandReg(SDNode *Op, SDValue N,
Evan Cheng9cb9e672009-06-27 02:26:13 +0000104 SDValue &BaseReg, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000105 bool SelectT2AddrModeImm12(SDNode *Op, SDValue N, SDValue &Base,
Evan Cheng055b0312009-06-29 07:51:04 +0000106 SDValue &OffImm);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000107 bool SelectT2AddrModeImm8(SDNode *Op, SDValue N, SDValue &Base,
Evan Cheng055b0312009-06-29 07:51:04 +0000108 SDValue &OffImm);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000109 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Chenge88d5ce2009-07-02 07:28:31 +0000110 SDValue &OffImm);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000111 bool SelectT2AddrModeImm8s4(SDNode *Op, SDValue N, SDValue &Base,
David Goodwin6647cea2009-06-30 22:50:01 +0000112 SDValue &OffImm);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000113 bool SelectT2AddrModeSoReg(SDNode *Op, SDValue N, SDValue &Base,
Evan Cheng055b0312009-06-29 07:51:04 +0000114 SDValue &OffReg, SDValue &ShImm);
115
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000116 // Include the pieces autogenerated from the target description.
117#include "ARMGenDAGISel.inc"
Bob Wilson224c2442009-05-19 05:53:42 +0000118
119private:
Evan Chenge88d5ce2009-07-02 07:28:31 +0000120 /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
121 /// ARM.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000122 SDNode *SelectARMIndexedLoad(SDNode *N);
123 SDNode *SelectT2IndexedLoad(SDNode *N);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000124
Bob Wilson621f1952010-03-23 05:25:43 +0000125 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
126 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson3e36f132009-10-14 17:28:52 +0000127 /// loads of D registers and even subregs and odd subregs of Q registers.
Bob Wilson621f1952010-03-23 05:25:43 +0000128 /// For NumVecs <= 2, QOpcodes1 is not used.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000129 SDNode *SelectVLD(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson3e36f132009-10-14 17:28:52 +0000130 unsigned *QOpcodes0, unsigned *QOpcodes1);
131
Bob Wilson24f995d2009-10-14 18:32:29 +0000132 /// SelectVST - Select NEON store intrinsics. NumVecs should
Bob Wilson11d98992010-03-23 06:20:33 +0000133 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson24f995d2009-10-14 18:32:29 +0000134 /// stores of D registers and even subregs and odd subregs of Q registers.
Bob Wilson11d98992010-03-23 06:20:33 +0000135 /// For NumVecs <= 2, QOpcodes1 is not used.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000136 SDNode *SelectVST(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson24f995d2009-10-14 18:32:29 +0000137 unsigned *QOpcodes0, unsigned *QOpcodes1);
138
Bob Wilson96493442009-10-14 16:46:45 +0000139 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
Bob Wilsona7c397c2009-10-14 16:19:03 +0000140 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson96493442009-10-14 16:46:45 +0000141 /// load/store of D registers and even subregs and odd subregs of Q registers.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000142 SDNode *SelectVLDSTLane(SDNode *N, bool IsLoad, unsigned NumVecs,
Bob Wilson96493442009-10-14 16:46:45 +0000143 unsigned *DOpcodes, unsigned *QOpcodes0,
144 unsigned *QOpcodes1);
Bob Wilsona7c397c2009-10-14 16:19:03 +0000145
Bob Wilson78dfbc32010-07-07 00:08:54 +0000146 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2,
147 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be
148 /// generated to force the table registers to be consecutive.
149 SDNode *SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
Bob Wilsond491d6e2010-07-06 23:36:25 +0000150
Sandeep Patel4e1ed882009-10-13 20:25:58 +0000151 /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
Jim Grosbach3a1287b2010-04-22 23:24:18 +0000152 SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
Sandeep Patel47eedaa2009-10-13 18:59:48 +0000153
Evan Cheng07ba9062009-11-19 21:45:22 +0000154 /// SelectCMOVOp - Select CMOV instructions for ARM.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000155 SDNode *SelectCMOVOp(SDNode *N);
156 SDNode *SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000157 ARMCC::CondCodes CCVal, SDValue CCR,
158 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000159 SDNode *SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000160 ARMCC::CondCodes CCVal, SDValue CCR,
161 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000162 SDNode *SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000163 ARMCC::CondCodes CCVal, SDValue CCR,
164 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000165 SDNode *SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000166 ARMCC::CondCodes CCVal, SDValue CCR,
167 SDValue InFlag);
Evan Cheng07ba9062009-11-19 21:45:22 +0000168
Evan Chengde8aa4e2010-05-05 18:28:36 +0000169 SDNode *SelectConcatVector(SDNode *N);
170
Evan Chengaf4550f2009-07-02 01:23:32 +0000171 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
172 /// inline asm expressions.
173 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
174 char ConstraintCode,
175 std::vector<SDValue> &OutOps);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000176
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000177 // Form pairs of consecutive S, D, or Q registers.
178 SDNode *PairSRegs(EVT VT, SDValue V0, SDValue V1);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000179 SDNode *PairDRegs(EVT VT, SDValue V0, SDValue V1);
Evan Cheng603afbf2010-05-10 17:34:18 +0000180 SDNode *PairQRegs(EVT VT, SDValue V0, SDValue V1);
181
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000182 // Form sequences of 4 consecutive S, D, or Q registers.
183 SDNode *QuadSRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Evan Cheng603afbf2010-05-10 17:34:18 +0000184 SDNode *QuadDRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Evan Cheng8f6de382010-05-16 03:27:48 +0000185 SDNode *QuadQRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
186
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000187 // Form sequences of 8 consecutive D registers.
Evan Cheng5c6aba22010-05-14 18:54:59 +0000188 SDNode *OctoDRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3,
189 SDValue V4, SDValue V5, SDValue V6, SDValue V7);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000190};
Evan Chenga8e29892007-01-19 07:51:42 +0000191}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000192
Sandeep Patel47eedaa2009-10-13 18:59:48 +0000193/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
194/// operand. If so Imm will receive the 32-bit value.
195static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
196 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
197 Imm = cast<ConstantSDNode>(N)->getZExtValue();
198 return true;
199 }
200 return false;
201}
202
203// isInt32Immediate - This method tests to see if a constant operand.
204// If so Imm will receive the 32 bit value.
205static bool isInt32Immediate(SDValue N, unsigned &Imm) {
206 return isInt32Immediate(N.getNode(), Imm);
207}
208
209// isOpcWithIntImmediate - This method tests to see if the node is a specific
210// opcode and that it has a immediate integer right operand.
211// If so Imm will receive the 32 bit value.
212static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
213 return N->getOpcode() == Opc &&
214 isInt32Immediate(N->getOperand(1).getNode(), Imm);
215}
216
217
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000218bool ARMDAGToDAGISel::SelectShifterOperandReg(SDNode *Op,
Evan Cheng055b0312009-06-29 07:51:04 +0000219 SDValue N,
220 SDValue &BaseReg,
221 SDValue &ShReg,
222 SDValue &Opc) {
223 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
224
225 // Don't match base register only case. That is matched to a separate
226 // lower complexity pattern with explicit register operand.
227 if (ShOpcVal == ARM_AM::no_shift) return false;
Jim Grosbach764ab522009-08-11 15:33:49 +0000228
Evan Cheng055b0312009-06-29 07:51:04 +0000229 BaseReg = N.getOperand(0);
230 unsigned ShImmVal = 0;
231 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 ShReg = CurDAG->getRegister(0, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000233 ShImmVal = RHS->getZExtValue() & 31;
234 } else {
235 ShReg = N.getOperand(1);
236 }
237 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000239 return true;
240}
241
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000242bool ARMDAGToDAGISel::SelectAddrMode2(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000243 SDValue &Base, SDValue &Offset,
244 SDValue &Opc) {
Evan Chenga13fd102007-03-13 21:05:54 +0000245 if (N.getOpcode() == ISD::MUL) {
246 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
247 // X * [3,5,9] -> X + X * [2,4,8] etc.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000248 int RHSC = (int)RHS->getZExtValue();
Evan Chenga13fd102007-03-13 21:05:54 +0000249 if (RHSC & 1) {
250 RHSC = RHSC & ~1;
251 ARM_AM::AddrOpc AddSub = ARM_AM::add;
252 if (RHSC < 0) {
253 AddSub = ARM_AM::sub;
254 RHSC = - RHSC;
255 }
256 if (isPowerOf2_32(RHSC)) {
257 unsigned ShAmt = Log2_32(RHSC);
258 Base = Offset = N.getOperand(0);
259 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
260 ARM_AM::lsl),
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 MVT::i32);
Evan Chenga13fd102007-03-13 21:05:54 +0000262 return true;
263 }
264 }
265 }
266 }
267
Evan Chenga8e29892007-01-19 07:51:42 +0000268 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
269 Base = N;
270 if (N.getOpcode() == ISD::FrameIndex) {
271 int FI = cast<FrameIndexSDNode>(N)->getIndex();
272 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000273 } else if (N.getOpcode() == ARMISD::Wrapper &&
274 !(Subtarget->useMovt() &&
275 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000276 Base = N.getOperand(0);
277 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000279 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
280 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000282 return true;
283 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000284
Evan Chenga8e29892007-01-19 07:51:42 +0000285 // Match simple R +/- imm12 operands.
286 if (N.getOpcode() == ISD::ADD)
287 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000288 int RHSC = (int)RHS->getZExtValue();
Evan Chenge966d642007-01-24 02:45:25 +0000289 if ((RHSC >= 0 && RHSC < 0x1000) ||
290 (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
Evan Chenga8e29892007-01-19 07:51:42 +0000291 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000292 if (Base.getOpcode() == ISD::FrameIndex) {
293 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
294 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
295 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000297
298 ARM_AM::AddrOpc AddSub = ARM_AM::add;
299 if (RHSC < 0) {
300 AddSub = ARM_AM::sub;
301 RHSC = - RHSC;
302 }
303 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
Evan Chenga8e29892007-01-19 07:51:42 +0000304 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000305 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000306 return true;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000307 }
Evan Chenga8e29892007-01-19 07:51:42 +0000308 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000309
Johnny Chen6a3b5ee2009-10-27 17:25:15 +0000310 // Otherwise this is R +/- [possibly shifted] R.
Evan Chenga8e29892007-01-19 07:51:42 +0000311 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
312 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
313 unsigned ShAmt = 0;
Jim Grosbach764ab522009-08-11 15:33:49 +0000314
Evan Chenga8e29892007-01-19 07:51:42 +0000315 Base = N.getOperand(0);
316 Offset = N.getOperand(1);
Jim Grosbach764ab522009-08-11 15:33:49 +0000317
Evan Chenga8e29892007-01-19 07:51:42 +0000318 if (ShOpcVal != ARM_AM::no_shift) {
319 // Check to see if the RHS of the shift is a constant, if not, we can't fold
320 // it.
321 if (ConstantSDNode *Sh =
322 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000323 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000324 Offset = N.getOperand(1).getOperand(0);
325 } else {
326 ShOpcVal = ARM_AM::no_shift;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000327 }
328 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000329
Evan Chenga8e29892007-01-19 07:51:42 +0000330 // Try matching (R shl C) + (R).
331 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
332 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
333 if (ShOpcVal != ARM_AM::no_shift) {
334 // Check to see if the RHS of the shift is a constant, if not, we can't
335 // fold it.
336 if (ConstantSDNode *Sh =
337 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000338 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000339 Offset = N.getOperand(0).getOperand(0);
340 Base = N.getOperand(1);
341 } else {
342 ShOpcVal = ARM_AM::no_shift;
343 }
344 }
345 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000346
Evan Chenga8e29892007-01-19 07:51:42 +0000347 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000349 return true;
350}
351
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000352bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000353 SDValue &Offset, SDValue &Opc) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000354 unsigned Opcode = Op->getOpcode();
Evan Chenga8e29892007-01-19 07:51:42 +0000355 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
356 ? cast<LoadSDNode>(Op)->getAddressingMode()
357 : cast<StoreSDNode>(Op)->getAddressingMode();
358 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
359 ? ARM_AM::add : ARM_AM::sub;
360 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000361 int Val = (int)C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000362 if (Val >= 0 && Val < 0x1000) { // 12 bits.
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000364 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
365 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000367 return true;
368 }
369 }
370
371 Offset = N;
372 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
373 unsigned ShAmt = 0;
374 if (ShOpcVal != ARM_AM::no_shift) {
375 // Check to see if the RHS of the shift is a constant, if not, we can't fold
376 // it.
377 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000378 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000379 Offset = N.getOperand(0);
380 } else {
381 ShOpcVal = ARM_AM::no_shift;
382 }
383 }
384
385 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 MVT::i32);
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000387 return true;
388}
389
Evan Chenga8e29892007-01-19 07:51:42 +0000390
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000391bool ARMDAGToDAGISel::SelectAddrMode3(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000392 SDValue &Base, SDValue &Offset,
393 SDValue &Opc) {
Evan Chenga8e29892007-01-19 07:51:42 +0000394 if (N.getOpcode() == ISD::SUB) {
395 // X - C is canonicalize to X + -C, no need to handle it here.
396 Base = N.getOperand(0);
397 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000399 return true;
400 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000401
Evan Chenga8e29892007-01-19 07:51:42 +0000402 if (N.getOpcode() != ISD::ADD) {
403 Base = N;
404 if (N.getOpcode() == ISD::FrameIndex) {
405 int FI = cast<FrameIndexSDNode>(N)->getIndex();
406 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
407 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 Offset = CurDAG->getRegister(0, MVT::i32);
409 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000410 return true;
411 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000412
Evan Chenga8e29892007-01-19 07:51:42 +0000413 // If the RHS is +/- imm8, fold into addr mode.
414 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000415 int RHSC = (int)RHS->getZExtValue();
Evan Chenge966d642007-01-24 02:45:25 +0000416 if ((RHSC >= 0 && RHSC < 256) ||
417 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000418 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000419 if (Base.getOpcode() == ISD::FrameIndex) {
420 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
421 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
422 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000424
425 ARM_AM::AddrOpc AddSub = ARM_AM::add;
426 if (RHSC < 0) {
427 AddSub = ARM_AM::sub;
428 RHSC = - RHSC;
429 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000431 return true;
432 }
433 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000434
Evan Chenga8e29892007-01-19 07:51:42 +0000435 Base = N.getOperand(0);
436 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000438 return true;
439}
440
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000441bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000442 SDValue &Offset, SDValue &Opc) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000443 unsigned Opcode = Op->getOpcode();
Evan Chenga8e29892007-01-19 07:51:42 +0000444 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
445 ? cast<LoadSDNode>(Op)->getAddressingMode()
446 : cast<StoreSDNode>(Op)->getAddressingMode();
447 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
448 ? ARM_AM::add : ARM_AM::sub;
449 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000450 int Val = (int)C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000451 if (Val >= 0 && Val < 256) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 Offset = CurDAG->getRegister(0, MVT::i32);
453 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000454 return true;
455 }
456 }
457
458 Offset = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000460 return true;
461}
462
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000463bool ARMDAGToDAGISel::SelectAddrMode4(SDNode *Op, SDValue N,
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000464 SDValue &Addr, SDValue &Mode) {
465 Addr = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 Mode = CurDAG->getTargetConstant(0, MVT::i32);
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000467 return true;
468}
Evan Chenga8e29892007-01-19 07:51:42 +0000469
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000470bool ARMDAGToDAGISel::SelectAddrMode5(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000471 SDValue &Base, SDValue &Offset) {
Evan Chenga8e29892007-01-19 07:51:42 +0000472 if (N.getOpcode() != ISD::ADD) {
473 Base = N;
474 if (N.getOpcode() == ISD::FrameIndex) {
475 int FI = cast<FrameIndexSDNode>(N)->getIndex();
476 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000477 } else if (N.getOpcode() == ARMISD::Wrapper &&
478 !(Subtarget->useMovt() &&
479 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000480 Base = N.getOperand(0);
481 }
482 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000484 return true;
485 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000486
Evan Chenga8e29892007-01-19 07:51:42 +0000487 // If the RHS is +/- imm8, fold into addr mode.
488 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000489 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000490 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
491 RHSC >>= 2;
Evan Chenge966d642007-01-24 02:45:25 +0000492 if ((RHSC >= 0 && RHSC < 256) ||
493 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000494 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000495 if (Base.getOpcode() == ISD::FrameIndex) {
496 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
497 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
498 }
499
500 ARM_AM::AddrOpc AddSub = ARM_AM::add;
501 if (RHSC < 0) {
502 AddSub = ARM_AM::sub;
503 RHSC = - RHSC;
504 }
505 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000507 return true;
508 }
509 }
510 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000511
Evan Chenga8e29892007-01-19 07:51:42 +0000512 Base = N;
513 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson825b72b2009-08-11 20:47:22 +0000514 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000515 return true;
516}
517
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000518bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Op, SDValue N,
Bob Wilson226036e2010-03-20 22:13:40 +0000519 SDValue &Addr, SDValue &Align) {
Bob Wilson8b024a52009-07-01 23:16:05 +0000520 Addr = N;
Jim Grosbach8a5ec862009-11-07 21:25:39 +0000521 // Default to no alignment.
522 Align = CurDAG->getTargetConstant(0, MVT::i32);
Bob Wilson8b024a52009-07-01 23:16:05 +0000523 return true;
524}
525
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000526bool ARMDAGToDAGISel::SelectAddrModePC(SDNode *Op, SDValue N,
Evan Chengbba9f5f2009-08-14 19:01:37 +0000527 SDValue &Offset, SDValue &Label) {
Evan Chenga8e29892007-01-19 07:51:42 +0000528 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
529 Offset = N.getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000530 SDValue N1 = N.getOperand(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000531 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000533 return true;
534 }
535 return false;
536}
537
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000538bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000539 SDValue &Base, SDValue &Offset){
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000540 // FIXME dl should come from the parent load or store, not the address
Evan Chengc38f2bc2007-01-23 22:59:13 +0000541 if (N.getOpcode() != ISD::ADD) {
Evan Cheng2f297df2009-07-11 07:08:13 +0000542 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
Dan Gohmane368b462010-06-18 14:22:04 +0000543 if (!NC || !NC->isNullValue())
Evan Cheng2f297df2009-07-11 07:08:13 +0000544 return false;
545
546 Base = Offset = N;
Evan Chengc38f2bc2007-01-23 22:59:13 +0000547 return true;
548 }
549
Evan Chenga8e29892007-01-19 07:51:42 +0000550 Base = N.getOperand(0);
551 Offset = N.getOperand(1);
552 return true;
553}
554
Evan Cheng79d43262007-01-24 02:21:22 +0000555bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000556ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000557 unsigned Scale, SDValue &Base,
558 SDValue &OffImm, SDValue &Offset) {
Evan Cheng79d43262007-01-24 02:21:22 +0000559 if (Scale == 4) {
Dan Gohman475871a2008-07-27 21:46:04 +0000560 SDValue TmpBase, TmpOffImm;
Evan Cheng79d43262007-01-24 02:21:22 +0000561 if (SelectThumbAddrModeSP(Op, N, TmpBase, TmpOffImm))
562 return false; // We want to select tLDRspi / tSTRspi instead.
Evan Cheng012f2d92007-01-24 08:53:17 +0000563 if (N.getOpcode() == ARMISD::Wrapper &&
564 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
565 return false; // We want to select tLDRpci instead.
Evan Cheng79d43262007-01-24 02:21:22 +0000566 }
567
Evan Chenga8e29892007-01-19 07:51:42 +0000568 if (N.getOpcode() != ISD::ADD) {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000569 if (N.getOpcode() == ARMISD::Wrapper &&
570 !(Subtarget->useMovt() &&
571 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
572 Base = N.getOperand(0);
573 } else
574 Base = N;
575
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 Offset = CurDAG->getRegister(0, MVT::i32);
577 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000578 return true;
579 }
580
Evan Chengad0e4652007-02-06 00:22:06 +0000581 // Thumb does not have [sp, r] address mode.
582 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
583 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
584 if ((LHSR && LHSR->getReg() == ARM::SP) ||
585 (RHSR && RHSR->getReg() == ARM::SP)) {
586 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 Offset = CurDAG->getRegister(0, MVT::i32);
588 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengad0e4652007-02-06 00:22:06 +0000589 return true;
590 }
591
Evan Chenga8e29892007-01-19 07:51:42 +0000592 // If the RHS is + imm5 * scale, fold into addr mode.
593 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000594 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000595 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
596 RHSC /= Scale;
597 if (RHSC >= 0 && RHSC < 32) {
598 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 Offset = CurDAG->getRegister(0, MVT::i32);
600 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000601 return true;
602 }
603 }
604 }
605
Evan Chengc38f2bc2007-01-23 22:59:13 +0000606 Base = N.getOperand(0);
607 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000609 return true;
Evan Chenga8e29892007-01-19 07:51:42 +0000610}
611
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000612bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000613 SDValue &Base, SDValue &OffImm,
614 SDValue &Offset) {
Evan Chengcea117d2007-01-30 02:35:32 +0000615 return SelectThumbAddrModeRI5(Op, N, 1, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000616}
617
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000618bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000619 SDValue &Base, SDValue &OffImm,
620 SDValue &Offset) {
Evan Chengcea117d2007-01-30 02:35:32 +0000621 return SelectThumbAddrModeRI5(Op, N, 2, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000622}
623
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000624bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000625 SDValue &Base, SDValue &OffImm,
626 SDValue &Offset) {
Evan Chengcea117d2007-01-30 02:35:32 +0000627 return SelectThumbAddrModeRI5(Op, N, 4, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000628}
629
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000630bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000631 SDValue &Base, SDValue &OffImm) {
Evan Chenga8e29892007-01-19 07:51:42 +0000632 if (N.getOpcode() == ISD::FrameIndex) {
633 int FI = cast<FrameIndexSDNode>(N)->getIndex();
634 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000636 return true;
637 }
Evan Cheng79d43262007-01-24 02:21:22 +0000638
Evan Chengad0e4652007-02-06 00:22:06 +0000639 if (N.getOpcode() != ISD::ADD)
640 return false;
641
642 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
Evan Cheng8c1a73a2007-02-06 09:11:20 +0000643 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
644 (LHSR && LHSR->getReg() == ARM::SP)) {
Evan Cheng79d43262007-01-24 02:21:22 +0000645 // If the RHS is + imm8 * scale, fold into addr mode.
646 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000647 int RHSC = (int)RHS->getZExtValue();
Evan Cheng79d43262007-01-24 02:21:22 +0000648 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
649 RHSC >>= 2;
650 if (RHSC >= 0 && RHSC < 256) {
Evan Chengad0e4652007-02-06 00:22:06 +0000651 Base = N.getOperand(0);
Evan Cheng8c1a73a2007-02-06 09:11:20 +0000652 if (Base.getOpcode() == ISD::FrameIndex) {
653 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
654 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
655 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Cheng79d43262007-01-24 02:21:22 +0000657 return true;
658 }
659 }
660 }
661 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000662
Evan Chenga8e29892007-01-19 07:51:42 +0000663 return false;
664}
665
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000666bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDNode *Op, SDValue N,
Evan Cheng9cb9e672009-06-27 02:26:13 +0000667 SDValue &BaseReg,
668 SDValue &Opc) {
669 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
670
671 // Don't match base register only case. That is matched to a separate
672 // lower complexity pattern with explicit register operand.
673 if (ShOpcVal == ARM_AM::no_shift) return false;
674
675 BaseReg = N.getOperand(0);
676 unsigned ShImmVal = 0;
677 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
678 ShImmVal = RHS->getZExtValue() & 31;
679 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
680 return true;
681 }
682
683 return false;
684}
685
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000686bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDNode *Op, SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000687 SDValue &Base, SDValue &OffImm) {
688 // Match simple R + imm12 operands.
David Goodwin31e7eba2009-07-20 15:55:39 +0000689
Evan Cheng3a214252009-08-11 08:52:18 +0000690 // Base only.
691 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
David Goodwin31e7eba2009-07-20 15:55:39 +0000692 if (N.getOpcode() == ISD::FrameIndex) {
Evan Cheng3a214252009-08-11 08:52:18 +0000693 // Match frame index...
David Goodwin31e7eba2009-07-20 15:55:39 +0000694 int FI = cast<FrameIndexSDNode>(N)->getIndex();
695 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000696 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
David Goodwin31e7eba2009-07-20 15:55:39 +0000697 return true;
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000698 } else if (N.getOpcode() == ARMISD::Wrapper &&
699 !(Subtarget->useMovt() &&
700 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Cheng3a214252009-08-11 08:52:18 +0000701 Base = N.getOperand(0);
702 if (Base.getOpcode() == ISD::TargetConstantPool)
703 return false; // We want to select t2LDRpci instead.
704 } else
705 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng3a214252009-08-11 08:52:18 +0000707 return true;
David Goodwin31e7eba2009-07-20 15:55:39 +0000708 }
Evan Cheng055b0312009-06-29 07:51:04 +0000709
710 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Evan Cheng3a214252009-08-11 08:52:18 +0000711 if (SelectT2AddrModeImm8(Op, N, Base, OffImm))
712 // Let t2LDRi8 handle (R - imm8).
713 return false;
714
Evan Cheng055b0312009-06-29 07:51:04 +0000715 int RHSC = (int)RHS->getZExtValue();
David Goodwind8c95b52009-07-30 18:56:48 +0000716 if (N.getOpcode() == ISD::SUB)
717 RHSC = -RHSC;
718
719 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
Evan Cheng055b0312009-06-29 07:51:04 +0000720 Base = N.getOperand(0);
David Goodwind8c95b52009-07-30 18:56:48 +0000721 if (Base.getOpcode() == ISD::FrameIndex) {
722 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
723 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
724 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000725 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000726 return true;
727 }
728 }
729
Evan Cheng3a214252009-08-11 08:52:18 +0000730 // Base only.
731 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000732 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng3a214252009-08-11 08:52:18 +0000733 return true;
Evan Cheng055b0312009-06-29 07:51:04 +0000734}
735
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000736bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDNode *Op, SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000737 SDValue &Base, SDValue &OffImm) {
David Goodwind8c95b52009-07-30 18:56:48 +0000738 // Match simple R - imm8 operands.
Evan Cheng3a214252009-08-11 08:52:18 +0000739 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::SUB) {
David Goodwin07337c02009-07-30 22:45:52 +0000740 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
741 int RHSC = (int)RHS->getSExtValue();
742 if (N.getOpcode() == ISD::SUB)
743 RHSC = -RHSC;
Jim Grosbach764ab522009-08-11 15:33:49 +0000744
Evan Cheng3a214252009-08-11 08:52:18 +0000745 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
746 Base = N.getOperand(0);
David Goodwin07337c02009-07-30 22:45:52 +0000747 if (Base.getOpcode() == ISD::FrameIndex) {
748 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
749 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
750 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000751 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
David Goodwin07337c02009-07-30 22:45:52 +0000752 return true;
Evan Cheng055b0312009-06-29 07:51:04 +0000753 }
Evan Cheng055b0312009-06-29 07:51:04 +0000754 }
755 }
756
757 return false;
758}
759
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000760bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Chenge88d5ce2009-07-02 07:28:31 +0000761 SDValue &OffImm){
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000762 unsigned Opcode = Op->getOpcode();
Evan Chenge88d5ce2009-07-02 07:28:31 +0000763 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
764 ? cast<LoadSDNode>(Op)->getAddressingMode()
765 : cast<StoreSDNode>(Op)->getAddressingMode();
766 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N)) {
767 int RHSC = (int)RHS->getZExtValue();
768 if (RHSC >= 0 && RHSC < 0x100) { // 8 bits.
David Goodwin4cb73522009-07-14 21:29:29 +0000769 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
771 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000772 return true;
773 }
774 }
775
776 return false;
777}
778
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000779bool ARMDAGToDAGISel::SelectT2AddrModeImm8s4(SDNode *Op, SDValue N,
David Goodwin6647cea2009-06-30 22:50:01 +0000780 SDValue &Base, SDValue &OffImm) {
781 if (N.getOpcode() == ISD::ADD) {
782 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
783 int RHSC = (int)RHS->getZExtValue();
Jim Grosbach18f30e62010-06-02 21:53:11 +0000784 // 8 bits.
Evan Cheng5c874172009-07-09 22:21:59 +0000785 if (((RHSC & 0x3) == 0) &&
Jim Grosbach18f30e62010-06-02 21:53:11 +0000786 ((RHSC >= 0 && RHSC < 0x400) || (RHSC < 0 && RHSC > -0x400))) {
David Goodwin6647cea2009-06-30 22:50:01 +0000787 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
David Goodwin6647cea2009-06-30 22:50:01 +0000789 return true;
790 }
791 }
792 } else if (N.getOpcode() == ISD::SUB) {
793 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
794 int RHSC = (int)RHS->getZExtValue();
Jim Grosbach18f30e62010-06-02 21:53:11 +0000795 // 8 bits.
796 if (((RHSC & 0x3) == 0) && (RHSC >= 0 && RHSC < 0x400)) {
David Goodwin6647cea2009-06-30 22:50:01 +0000797 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 OffImm = CurDAG->getTargetConstant(-RHSC, MVT::i32);
David Goodwin6647cea2009-06-30 22:50:01 +0000799 return true;
800 }
801 }
802 }
803
804 return false;
805}
806
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000807bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDNode *Op, SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000808 SDValue &Base,
809 SDValue &OffReg, SDValue &ShImm) {
Evan Cheng3a214252009-08-11 08:52:18 +0000810 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
811 if (N.getOpcode() != ISD::ADD)
812 return false;
Evan Cheng055b0312009-06-29 07:51:04 +0000813
Evan Cheng3a214252009-08-11 08:52:18 +0000814 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
815 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
816 int RHSC = (int)RHS->getZExtValue();
817 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
818 return false;
819 else if (RHSC < 0 && RHSC >= -255) // 8 bits
David Goodwind8c95b52009-07-30 18:56:48 +0000820 return false;
821 }
822
Evan Cheng055b0312009-06-29 07:51:04 +0000823 // Look for (R + R) or (R + (R << [1,2,3])).
824 unsigned ShAmt = 0;
825 Base = N.getOperand(0);
826 OffReg = N.getOperand(1);
827
828 // Swap if it is ((R << c) + R).
829 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
830 if (ShOpcVal != ARM_AM::lsl) {
831 ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
832 if (ShOpcVal == ARM_AM::lsl)
833 std::swap(Base, OffReg);
Jim Grosbach764ab522009-08-11 15:33:49 +0000834 }
835
Evan Cheng055b0312009-06-29 07:51:04 +0000836 if (ShOpcVal == ARM_AM::lsl) {
837 // Check to see if the RHS of the shift is a constant, if not, we can't fold
838 // it.
839 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
840 ShAmt = Sh->getZExtValue();
841 if (ShAmt >= 4) {
842 ShAmt = 0;
843 ShOpcVal = ARM_AM::no_shift;
844 } else
845 OffReg = OffReg.getOperand(0);
846 } else {
847 ShOpcVal = ARM_AM::no_shift;
848 }
David Goodwin7ecc8502009-07-15 15:50:19 +0000849 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000850
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000852
853 return true;
854}
855
856//===--------------------------------------------------------------------===//
857
Evan Chengee568cf2007-07-05 07:15:27 +0000858/// getAL - Returns a ARMCC::AL immediate node.
Dan Gohman475871a2008-07-27 21:46:04 +0000859static inline SDValue getAL(SelectionDAG *CurDAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
Evan Cheng44bec522007-05-15 01:29:07 +0000861}
862
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000863SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
864 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengaf4550f2009-07-02 01:23:32 +0000865 ISD::MemIndexedMode AM = LD->getAddressingMode();
866 if (AM == ISD::UNINDEXED)
867 return NULL;
868
Owen Andersone50ed302009-08-10 22:56:29 +0000869 EVT LoadedVT = LD->getMemoryVT();
Evan Chengaf4550f2009-07-02 01:23:32 +0000870 SDValue Offset, AMOpc;
871 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
872 unsigned Opcode = 0;
873 bool Match = false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 if (LoadedVT == MVT::i32 &&
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000875 SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000876 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
877 Match = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 } else if (LoadedVT == MVT::i16 &&
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000879 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000880 Match = true;
881 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
882 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
883 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000885 if (LD->getExtensionType() == ISD::SEXTLOAD) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000886 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000887 Match = true;
888 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
889 }
890 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000891 if (SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000892 Match = true;
893 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
894 }
895 }
896 }
897
898 if (Match) {
899 SDValue Chain = LD->getChain();
900 SDValue Base = LD->getBasePtr();
901 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 CurDAG->getRegister(0, MVT::i32), Chain };
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000903 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +0000904 MVT::Other, Ops, 6);
Evan Chengaf4550f2009-07-02 01:23:32 +0000905 }
906
907 return NULL;
908}
909
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000910SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
911 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000912 ISD::MemIndexedMode AM = LD->getAddressingMode();
913 if (AM == ISD::UNINDEXED)
914 return NULL;
915
Owen Andersone50ed302009-08-10 22:56:29 +0000916 EVT LoadedVT = LD->getMemoryVT();
Evan Cheng4fbb9962009-07-02 23:16:11 +0000917 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000918 SDValue Offset;
919 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
920 unsigned Opcode = 0;
921 bool Match = false;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000922 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000923 switch (LoadedVT.getSimpleVT().SimpleTy) {
924 case MVT::i32:
Evan Chenge88d5ce2009-07-02 07:28:31 +0000925 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
926 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000927 case MVT::i16:
Evan Cheng4fbb9962009-07-02 23:16:11 +0000928 if (isSExtLd)
929 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
930 else
931 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000932 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 case MVT::i8:
934 case MVT::i1:
Evan Cheng4fbb9962009-07-02 23:16:11 +0000935 if (isSExtLd)
936 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
937 else
938 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000939 break;
940 default:
941 return NULL;
942 }
943 Match = true;
944 }
945
946 if (Match) {
947 SDValue Chain = LD->getChain();
948 SDValue Base = LD->getBasePtr();
949 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 CurDAG->getRegister(0, MVT::i32), Chain };
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000951 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +0000952 MVT::Other, Ops, 5);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000953 }
954
955 return NULL;
956}
957
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000958/// PairSRegs - Form a D register from a pair of S registers.
959///
960SDNode *ARMDAGToDAGISel::PairSRegs(EVT VT, SDValue V0, SDValue V1) {
961 DebugLoc dl = V0.getNode()->getDebugLoc();
962 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
963 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
Bob Wilson07f6e802010-06-16 21:34:01 +0000964 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
965 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000966}
967
Evan Cheng603afbf2010-05-10 17:34:18 +0000968/// PairDRegs - Form a quad register from a pair of D registers.
969///
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000970SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
971 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000972 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
973 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Bob Wilson07f6e802010-06-16 21:34:01 +0000974 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
975 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000976}
977
Evan Cheng7f687192010-05-14 00:21:45 +0000978/// PairQRegs - Form 4 consecutive D registers from a pair of Q registers.
Evan Cheng603afbf2010-05-10 17:34:18 +0000979///
980SDNode *ARMDAGToDAGISel::PairQRegs(EVT VT, SDValue V0, SDValue V1) {
981 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000982 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
983 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
Evan Cheng603afbf2010-05-10 17:34:18 +0000984 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
985 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
986}
987
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000988/// QuadSRegs - Form 4 consecutive S registers.
989///
990SDNode *ARMDAGToDAGISel::QuadSRegs(EVT VT, SDValue V0, SDValue V1,
991 SDValue V2, SDValue V3) {
992 DebugLoc dl = V0.getNode()->getDebugLoc();
993 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
994 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
995 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, MVT::i32);
996 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, MVT::i32);
997 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
998 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
999}
1000
Evan Cheng7f687192010-05-14 00:21:45 +00001001/// QuadDRegs - Form 4 consecutive D registers.
Evan Cheng603afbf2010-05-10 17:34:18 +00001002///
1003SDNode *ARMDAGToDAGISel::QuadDRegs(EVT VT, SDValue V0, SDValue V1,
1004 SDValue V2, SDValue V3) {
1005 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001006 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1007 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
1008 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
1009 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
Evan Cheng603afbf2010-05-10 17:34:18 +00001010 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1011 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1012}
1013
Evan Cheng8f6de382010-05-16 03:27:48 +00001014/// QuadQRegs - Form 4 consecutive Q registers.
1015///
1016SDNode *ARMDAGToDAGISel::QuadQRegs(EVT VT, SDValue V0, SDValue V1,
1017 SDValue V2, SDValue V3) {
1018 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001019 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1020 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
1021 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, MVT::i32);
1022 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, MVT::i32);
Evan Cheng8f6de382010-05-16 03:27:48 +00001023 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1024 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1025}
1026
Evan Cheng5c6aba22010-05-14 18:54:59 +00001027/// OctoDRegs - Form 8 consecutive D registers.
1028///
1029SDNode *ARMDAGToDAGISel::OctoDRegs(EVT VT, SDValue V0, SDValue V1,
1030 SDValue V2, SDValue V3,
1031 SDValue V4, SDValue V5,
1032 SDValue V6, SDValue V7) {
1033 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001034 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1035 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
1036 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
1037 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
1038 SDValue SubReg4 = CurDAG->getTargetConstant(ARM::dsub_4, MVT::i32);
1039 SDValue SubReg5 = CurDAG->getTargetConstant(ARM::dsub_5, MVT::i32);
1040 SDValue SubReg6 = CurDAG->getTargetConstant(ARM::dsub_6, MVT::i32);
1041 SDValue SubReg7 = CurDAG->getTargetConstant(ARM::dsub_7, MVT::i32);
Evan Cheng5c6aba22010-05-14 18:54:59 +00001042 const SDValue Ops[] ={ V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3,
1043 V4, SubReg4, V5, SubReg5, V6, SubReg6, V7, SubReg7 };
1044 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 16);
1045}
1046
Bob Wilsona7c397c2009-10-14 16:19:03 +00001047/// GetNEONSubregVT - Given a type for a 128-bit NEON vector, return the type
1048/// for a 64-bit subregister of the vector.
1049static EVT GetNEONSubregVT(EVT VT) {
1050 switch (VT.getSimpleVT().SimpleTy) {
1051 default: llvm_unreachable("unhandled NEON type");
1052 case MVT::v16i8: return MVT::v8i8;
1053 case MVT::v8i16: return MVT::v4i16;
1054 case MVT::v4f32: return MVT::v2f32;
1055 case MVT::v4i32: return MVT::v2i32;
1056 case MVT::v2i64: return MVT::v1i64;
1057 }
1058}
1059
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001060SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
Bob Wilson3e36f132009-10-14 17:28:52 +00001061 unsigned *DOpcodes, unsigned *QOpcodes0,
1062 unsigned *QOpcodes1) {
Bob Wilson621f1952010-03-23 05:25:43 +00001063 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
Bob Wilson3e36f132009-10-14 17:28:52 +00001064 DebugLoc dl = N->getDebugLoc();
1065
Bob Wilson226036e2010-03-20 22:13:40 +00001066 SDValue MemAddr, Align;
1067 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
Bob Wilson3e36f132009-10-14 17:28:52 +00001068 return NULL;
1069
1070 SDValue Chain = N->getOperand(0);
1071 EVT VT = N->getValueType(0);
1072 bool is64BitVector = VT.is64BitVector();
1073
1074 unsigned OpcodeIndex;
1075 switch (VT.getSimpleVT().SimpleTy) {
1076 default: llvm_unreachable("unhandled vld type");
1077 // Double-register operations:
1078 case MVT::v8i8: OpcodeIndex = 0; break;
1079 case MVT::v4i16: OpcodeIndex = 1; break;
1080 case MVT::v2f32:
1081 case MVT::v2i32: OpcodeIndex = 2; break;
1082 case MVT::v1i64: OpcodeIndex = 3; break;
1083 // Quad-register operations:
1084 case MVT::v16i8: OpcodeIndex = 0; break;
1085 case MVT::v8i16: OpcodeIndex = 1; break;
1086 case MVT::v4f32:
1087 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson621f1952010-03-23 05:25:43 +00001088 case MVT::v2i64: OpcodeIndex = 3;
Bob Wilson11d98992010-03-23 06:20:33 +00001089 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
Bob Wilson621f1952010-03-23 05:25:43 +00001090 break;
Bob Wilson3e36f132009-10-14 17:28:52 +00001091 }
1092
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001093 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001094 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilson3e36f132009-10-14 17:28:52 +00001095 if (is64BitVector) {
1096 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001097 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilson3e36f132009-10-14 17:28:52 +00001098 std::vector<EVT> ResTys(NumVecs, VT);
1099 ResTys.push_back(MVT::Other);
Evan Chenge9e2ba02010-05-10 21:26:24 +00001100 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops, 5);
Bob Wilson07f6e802010-06-16 21:34:01 +00001101 if (NumVecs < 2)
Evan Chenge9e2ba02010-05-10 21:26:24 +00001102 return VLd;
1103
Evan Cheng0ce537a2010-05-11 01:19:40 +00001104 SDValue RegSeq;
Evan Chenge9e2ba02010-05-10 21:26:24 +00001105 SDValue V0 = SDValue(VLd, 0);
1106 SDValue V1 = SDValue(VLd, 1);
Evan Chenge9e2ba02010-05-10 21:26:24 +00001107
Evan Cheng0ce537a2010-05-11 01:19:40 +00001108 // Form a REG_SEQUENCE to force register allocation.
Evan Chenge9e2ba02010-05-10 21:26:24 +00001109 if (NumVecs == 2)
1110 RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1111 else {
1112 SDValue V2 = SDValue(VLd, 2);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001113 // If it's a vld3, form a quad D-register but discard the last part.
Evan Chenge9e2ba02010-05-10 21:26:24 +00001114 SDValue V3 = (NumVecs == 3)
1115 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1116 : SDValue(VLd, 3);
1117 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1118 }
1119
Jakob Stoklund Olesen7bb31e32010-05-24 17:13:28 +00001120 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
Evan Cheng5c6aba22010-05-14 18:54:59 +00001121 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001122 SDValue D = CurDAG->getTargetExtractSubreg(ARM::dsub_0+Vec,
Evan Cheng5c6aba22010-05-14 18:54:59 +00001123 dl, VT, RegSeq);
1124 ReplaceUses(SDValue(N, Vec), D);
Evan Chenge9e2ba02010-05-10 21:26:24 +00001125 }
1126 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, NumVecs));
1127 return NULL;
Bob Wilson3e36f132009-10-14 17:28:52 +00001128 }
1129
1130 EVT RegVT = GetNEONSubregVT(VT);
Bob Wilson621f1952010-03-23 05:25:43 +00001131 if (NumVecs <= 2) {
1132 // Quad registers are directly supported for VLD1 and VLD2,
1133 // loading pairs of D regs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001134 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001135 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilson621f1952010-03-23 05:25:43 +00001136 std::vector<EVT> ResTys(2 * NumVecs, RegVT);
Bob Wilson3e36f132009-10-14 17:28:52 +00001137 ResTys.push_back(MVT::Other);
Bob Wilson226036e2010-03-20 22:13:40 +00001138 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops, 5);
Bob Wilson621f1952010-03-23 05:25:43 +00001139 Chain = SDValue(VLd, 2 * NumVecs);
Bob Wilson3e36f132009-10-14 17:28:52 +00001140
1141 // Combine the even and odd subregs to produce the result.
Bob Wilson07f6e802010-06-16 21:34:01 +00001142 if (NumVecs == 1) {
1143 SDNode *Q = PairDRegs(VT, SDValue(VLd, 0), SDValue(VLd, 1));
1144 ReplaceUses(SDValue(N, 0), SDValue(Q, 0));
Evan Cheng603afbf2010-05-10 17:34:18 +00001145 } else {
Bob Wilson07f6e802010-06-16 21:34:01 +00001146 SDValue QQ = SDValue(QuadDRegs(MVT::v4i64,
1147 SDValue(VLd, 0), SDValue(VLd, 1),
1148 SDValue(VLd, 2), SDValue(VLd, 3)), 0);
1149 SDValue Q0 = CurDAG->getTargetExtractSubreg(ARM::qsub_0, dl, VT, QQ);
1150 SDValue Q1 = CurDAG->getTargetExtractSubreg(ARM::qsub_1, dl, VT, QQ);
1151 ReplaceUses(SDValue(N, 0), Q0);
1152 ReplaceUses(SDValue(N, 1), Q1);
Bob Wilson3e36f132009-10-14 17:28:52 +00001153 }
1154 } else {
1155 // Otherwise, quad registers are loaded with two separate instructions,
1156 // where one loads the even registers and the other loads the odd registers.
1157
Bob Wilson3e36f132009-10-14 17:28:52 +00001158 std::vector<EVT> ResTys(NumVecs, RegVT);
1159 ResTys.push_back(MemAddr.getValueType());
1160 ResTys.push_back(MVT::Other);
1161
Bob Wilson24f995d2009-10-14 18:32:29 +00001162 // Load the even subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001163 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001164 const SDValue OpsA[] = { MemAddr, Align, Reg0, Pred, Reg0, Chain };
1165 SDNode *VLdA = CurDAG->getMachineNode(Opc, dl, ResTys, OpsA, 6);
Bob Wilson3e36f132009-10-14 17:28:52 +00001166 Chain = SDValue(VLdA, NumVecs+1);
1167
Bob Wilson24f995d2009-10-14 18:32:29 +00001168 // Load the odd subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001169 Opc = QOpcodes1[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001170 const SDValue OpsB[] = { SDValue(VLdA, NumVecs),
1171 Align, Reg0, Pred, Reg0, Chain };
1172 SDNode *VLdB = CurDAG->getMachineNode(Opc, dl, ResTys, OpsB, 6);
Bob Wilson3e36f132009-10-14 17:28:52 +00001173 Chain = SDValue(VLdB, NumVecs+1);
1174
Bob Wilson07f6e802010-06-16 21:34:01 +00001175 SDValue V0 = SDValue(VLdA, 0);
1176 SDValue V1 = SDValue(VLdB, 0);
1177 SDValue V2 = SDValue(VLdA, 1);
1178 SDValue V3 = SDValue(VLdB, 1);
1179 SDValue V4 = SDValue(VLdA, 2);
1180 SDValue V5 = SDValue(VLdB, 2);
1181 SDValue V6 = (NumVecs == 3)
1182 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,RegVT), 0)
1183 : SDValue(VLdA, 3);
1184 SDValue V7 = (NumVecs == 3)
1185 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,RegVT), 0)
1186 : SDValue(VLdB, 3);
1187 SDValue RegSeq = SDValue(OctoDRegs(MVT::v8i64, V0, V1, V2, V3,
1188 V4, V5, V6, V7), 0);
Evan Cheng5c6aba22010-05-14 18:54:59 +00001189
Bob Wilson07f6e802010-06-16 21:34:01 +00001190 // Extract out the 3 / 4 Q registers.
1191 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1192 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1193 SDValue Q = CurDAG->getTargetExtractSubreg(ARM::qsub_0+Vec,
1194 dl, VT, RegSeq);
1195 ReplaceUses(SDValue(N, Vec), Q);
Bob Wilson3e36f132009-10-14 17:28:52 +00001196 }
1197 }
1198 ReplaceUses(SDValue(N, NumVecs), Chain);
1199 return NULL;
1200}
1201
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001202SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
Bob Wilson24f995d2009-10-14 18:32:29 +00001203 unsigned *DOpcodes, unsigned *QOpcodes0,
1204 unsigned *QOpcodes1) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001205 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
Bob Wilson24f995d2009-10-14 18:32:29 +00001206 DebugLoc dl = N->getDebugLoc();
1207
Bob Wilson226036e2010-03-20 22:13:40 +00001208 SDValue MemAddr, Align;
1209 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
Bob Wilson24f995d2009-10-14 18:32:29 +00001210 return NULL;
1211
1212 SDValue Chain = N->getOperand(0);
1213 EVT VT = N->getOperand(3).getValueType();
1214 bool is64BitVector = VT.is64BitVector();
1215
1216 unsigned OpcodeIndex;
1217 switch (VT.getSimpleVT().SimpleTy) {
1218 default: llvm_unreachable("unhandled vst type");
1219 // Double-register operations:
1220 case MVT::v8i8: OpcodeIndex = 0; break;
1221 case MVT::v4i16: OpcodeIndex = 1; break;
1222 case MVT::v2f32:
1223 case MVT::v2i32: OpcodeIndex = 2; break;
1224 case MVT::v1i64: OpcodeIndex = 3; break;
1225 // Quad-register operations:
1226 case MVT::v16i8: OpcodeIndex = 0; break;
1227 case MVT::v8i16: OpcodeIndex = 1; break;
1228 case MVT::v4f32:
1229 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson11d98992010-03-23 06:20:33 +00001230 case MVT::v2i64: OpcodeIndex = 3;
1231 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1232 break;
Bob Wilson24f995d2009-10-14 18:32:29 +00001233 }
1234
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001235 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001236 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001237
Bob Wilson226036e2010-03-20 22:13:40 +00001238 SmallVector<SDValue, 10> Ops;
Bob Wilson24f995d2009-10-14 18:32:29 +00001239 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001240 Ops.push_back(Align);
Bob Wilson24f995d2009-10-14 18:32:29 +00001241
1242 if (is64BitVector) {
Bob Wilson07f6e802010-06-16 21:34:01 +00001243 if (NumVecs >= 2) {
Evan Cheng0ce537a2010-05-11 01:19:40 +00001244 SDValue RegSeq;
1245 SDValue V0 = N->getOperand(0+3);
1246 SDValue V1 = N->getOperand(1+3);
1247
1248 // Form a REG_SEQUENCE to force register allocation.
1249 if (NumVecs == 2)
1250 RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1251 else {
1252 SDValue V2 = N->getOperand(2+3);
1253 // If it's a vld3, form a quad D-register and leave the last part as
1254 // an undef.
1255 SDValue V3 = (NumVecs == 3)
1256 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1257 : N->getOperand(3+3);
1258 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1259 }
1260
1261 // Now extract the D registers back out.
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001262 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_0, dl, VT,
Evan Cheng0ce537a2010-05-11 01:19:40 +00001263 RegSeq));
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001264 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_1, dl, VT,
Evan Cheng0ce537a2010-05-11 01:19:40 +00001265 RegSeq));
1266 if (NumVecs > 2)
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001267 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_2, dl, VT,
Evan Cheng0ce537a2010-05-11 01:19:40 +00001268 RegSeq));
1269 if (NumVecs > 3)
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001270 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_3, dl, VT,
Evan Cheng0ce537a2010-05-11 01:19:40 +00001271 RegSeq));
1272 } else {
1273 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1274 Ops.push_back(N->getOperand(Vec+3));
1275 }
Evan Chengac0869d2009-11-21 06:21:52 +00001276 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001277 Ops.push_back(Reg0); // predicate register
Bob Wilson24f995d2009-10-14 18:32:29 +00001278 Ops.push_back(Chain);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001279 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001280 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), NumVecs+5);
Bob Wilson24f995d2009-10-14 18:32:29 +00001281 }
1282
1283 EVT RegVT = GetNEONSubregVT(VT);
Bob Wilson11d98992010-03-23 06:20:33 +00001284 if (NumVecs <= 2) {
1285 // Quad registers are directly supported for VST1 and VST2,
1286 // storing pairs of D regs.
Bob Wilson24f995d2009-10-14 18:32:29 +00001287 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilson07f6e802010-06-16 21:34:01 +00001288 if (NumVecs == 2) {
Evan Cheng0ce537a2010-05-11 01:19:40 +00001289 // First extract the pair of Q registers.
Evan Cheng603afbf2010-05-10 17:34:18 +00001290 SDValue Q0 = N->getOperand(3);
1291 SDValue Q1 = N->getOperand(4);
1292
1293 // Form a QQ register.
1294 SDValue QQ = SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0);
1295
1296 // Now extract the D registers back out.
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001297 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_0, dl, RegVT,
Evan Cheng603afbf2010-05-10 17:34:18 +00001298 QQ));
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001299 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_1, dl, RegVT,
Evan Cheng603afbf2010-05-10 17:34:18 +00001300 QQ));
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001301 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_2, dl, RegVT,
Evan Cheng603afbf2010-05-10 17:34:18 +00001302 QQ));
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001303 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_3, dl, RegVT,
Evan Cheng603afbf2010-05-10 17:34:18 +00001304 QQ));
1305 Ops.push_back(Pred);
1306 Ops.push_back(Reg0); // predicate register
1307 Ops.push_back(Chain);
1308 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 5 + 4);
1309 } else {
1310 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001311 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_0, dl, RegVT,
Evan Cheng603afbf2010-05-10 17:34:18 +00001312 N->getOperand(Vec+3)));
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001313 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_1, dl, RegVT,
Evan Cheng603afbf2010-05-10 17:34:18 +00001314 N->getOperand(Vec+3)));
1315 }
1316 Ops.push_back(Pred);
1317 Ops.push_back(Reg0); // predicate register
1318 Ops.push_back(Chain);
1319 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(),
1320 5 + 2 * NumVecs);
Bob Wilson24f995d2009-10-14 18:32:29 +00001321 }
Bob Wilson24f995d2009-10-14 18:32:29 +00001322 }
1323
1324 // Otherwise, quad registers are stored with two separate instructions,
1325 // where one stores the even registers and the other stores the odd registers.
Evan Cheng7189fd02010-05-15 07:53:37 +00001326
Bob Wilson07f6e802010-06-16 21:34:01 +00001327 // Form the QQQQ REG_SEQUENCE.
1328 SDValue V[8];
1329 for (unsigned Vec = 0, i = 0; Vec < NumVecs; ++Vec, i+=2) {
1330 V[i] = CurDAG->getTargetExtractSubreg(ARM::dsub_0, dl, RegVT,
1331 N->getOperand(Vec+3));
1332 V[i+1] = CurDAG->getTargetExtractSubreg(ARM::dsub_1, dl, RegVT,
1333 N->getOperand(Vec+3));
Evan Cheng12c24692010-05-14 22:54:52 +00001334 }
Bob Wilson07f6e802010-06-16 21:34:01 +00001335 if (NumVecs == 3)
1336 V[6] = V[7] = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
1337 dl, RegVT), 0);
1338
1339 SDValue RegSeq = SDValue(OctoDRegs(MVT::v8i64, V[0], V[1], V[2], V[3],
1340 V[4], V[5], V[6], V[7]), 0);
1341
1342 // Store the even D registers.
1343 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1344 Ops.push_back(Reg0); // post-access address offset
1345 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1346 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_0+Vec*2, dl,
1347 RegVT, RegSeq));
1348 Ops.push_back(Pred);
1349 Ops.push_back(Reg0); // predicate register
1350 Ops.push_back(Chain);
1351 unsigned Opc = QOpcodes0[OpcodeIndex];
1352 SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1353 MVT::Other, Ops.data(), NumVecs+6);
1354 Chain = SDValue(VStA, 1);
1355
1356 // Store the odd D registers.
1357 Ops[0] = SDValue(VStA, 0); // MemAddr
1358 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1359 Ops[Vec+3] = CurDAG->getTargetExtractSubreg(ARM::dsub_1+Vec*2, dl,
1360 RegVT, RegSeq);
1361 Ops[NumVecs+5] = Chain;
1362 Opc = QOpcodes1[OpcodeIndex];
1363 SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1364 MVT::Other, Ops.data(), NumVecs+6);
1365 Chain = SDValue(VStB, 1);
1366 ReplaceUses(SDValue(N, 0), Chain);
1367 return NULL;
Bob Wilson24f995d2009-10-14 18:32:29 +00001368}
1369
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001370SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
Bob Wilson96493442009-10-14 16:46:45 +00001371 unsigned NumVecs, unsigned *DOpcodes,
1372 unsigned *QOpcodes0,
1373 unsigned *QOpcodes1) {
1374 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001375 DebugLoc dl = N->getDebugLoc();
1376
Bob Wilson226036e2010-03-20 22:13:40 +00001377 SDValue MemAddr, Align;
1378 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
Bob Wilsona7c397c2009-10-14 16:19:03 +00001379 return NULL;
1380
1381 SDValue Chain = N->getOperand(0);
1382 unsigned Lane =
1383 cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
Bob Wilson96493442009-10-14 16:46:45 +00001384 EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
Bob Wilsona7c397c2009-10-14 16:19:03 +00001385 bool is64BitVector = VT.is64BitVector();
1386
Bob Wilson96493442009-10-14 16:46:45 +00001387 // Quad registers are handled by load/store of subregs. Find the subreg info.
Bob Wilsona7c397c2009-10-14 16:19:03 +00001388 unsigned NumElts = 0;
Evan Cheng8f6de382010-05-16 03:27:48 +00001389 bool Even = false;
Bob Wilsona7c397c2009-10-14 16:19:03 +00001390 EVT RegVT = VT;
1391 if (!is64BitVector) {
1392 RegVT = GetNEONSubregVT(VT);
1393 NumElts = RegVT.getVectorNumElements();
Evan Cheng8f6de382010-05-16 03:27:48 +00001394 Even = Lane < NumElts;
Bob Wilsona7c397c2009-10-14 16:19:03 +00001395 }
1396
1397 unsigned OpcodeIndex;
1398 switch (VT.getSimpleVT().SimpleTy) {
Bob Wilson96493442009-10-14 16:46:45 +00001399 default: llvm_unreachable("unhandled vld/vst lane type");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001400 // Double-register operations:
1401 case MVT::v8i8: OpcodeIndex = 0; break;
1402 case MVT::v4i16: OpcodeIndex = 1; break;
1403 case MVT::v2f32:
1404 case MVT::v2i32: OpcodeIndex = 2; break;
1405 // Quad-register operations:
1406 case MVT::v8i16: OpcodeIndex = 0; break;
1407 case MVT::v4f32:
1408 case MVT::v4i32: OpcodeIndex = 1; break;
1409 }
1410
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001411 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001412 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001413
Bob Wilson226036e2010-03-20 22:13:40 +00001414 SmallVector<SDValue, 10> Ops;
Bob Wilsona7c397c2009-10-14 16:19:03 +00001415 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001416 Ops.push_back(Align);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001417
1418 unsigned Opc = 0;
1419 if (is64BitVector) {
1420 Opc = DOpcodes[OpcodeIndex];
Bob Wilson07f6e802010-06-16 21:34:01 +00001421 SDValue RegSeq;
1422 SDValue V0 = N->getOperand(0+3);
1423 SDValue V1 = N->getOperand(1+3);
1424 if (NumVecs == 2) {
1425 RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
Evan Cheng8f6de382010-05-16 03:27:48 +00001426 } else {
Bob Wilson07f6e802010-06-16 21:34:01 +00001427 SDValue V2 = N->getOperand(2+3);
1428 SDValue V3 = (NumVecs == 3)
1429 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1430 : N->getOperand(3+3);
1431 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
Evan Cheng8f6de382010-05-16 03:27:48 +00001432 }
Bob Wilson07f6e802010-06-16 21:34:01 +00001433
1434 // Now extract the D registers back out.
1435 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_0, dl, VT, RegSeq));
1436 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_1, dl, VT, RegSeq));
1437 if (NumVecs > 2)
1438 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_2, dl, VT,RegSeq));
1439 if (NumVecs > 3)
1440 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_3, dl, VT,RegSeq));
Bob Wilsona7c397c2009-10-14 16:19:03 +00001441 } else {
1442 // Check if this is loading the even or odd subreg of a Q register.
1443 if (Lane < NumElts) {
1444 Opc = QOpcodes0[OpcodeIndex];
1445 } else {
1446 Lane -= NumElts;
1447 Opc = QOpcodes1[OpcodeIndex];
1448 }
Evan Cheng8f6de382010-05-16 03:27:48 +00001449
Bob Wilson07f6e802010-06-16 21:34:01 +00001450 SDValue RegSeq;
1451 SDValue V0 = N->getOperand(0+3);
1452 SDValue V1 = N->getOperand(1+3);
1453 if (NumVecs == 2) {
1454 RegSeq = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
Evan Cheng8f6de382010-05-16 03:27:48 +00001455 } else {
Bob Wilson07f6e802010-06-16 21:34:01 +00001456 SDValue V2 = N->getOperand(2+3);
1457 SDValue V3 = (NumVecs == 3)
1458 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1459 : N->getOperand(3+3);
1460 RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Evan Cheng8f6de382010-05-16 03:27:48 +00001461 }
Bob Wilson07f6e802010-06-16 21:34:01 +00001462
1463 // Extract the subregs of the input vector.
1464 unsigned SubIdx = Even ? ARM::dsub_0 : ARM::dsub_1;
1465 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1466 Ops.push_back(CurDAG->getTargetExtractSubreg(SubIdx+Vec*2, dl, RegVT,
1467 RegSeq));
Bob Wilsona7c397c2009-10-14 16:19:03 +00001468 }
1469 Ops.push_back(getI32Imm(Lane));
Evan Chengac0869d2009-11-21 06:21:52 +00001470 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001471 Ops.push_back(Reg0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001472 Ops.push_back(Chain);
1473
Bob Wilson96493442009-10-14 16:46:45 +00001474 if (!IsLoad)
Bob Wilson226036e2010-03-20 22:13:40 +00001475 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), NumVecs+6);
Bob Wilson96493442009-10-14 16:46:45 +00001476
Bob Wilsona7c397c2009-10-14 16:19:03 +00001477 std::vector<EVT> ResTys(NumVecs, RegVT);
1478 ResTys.push_back(MVT::Other);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001479 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTys, Ops.data(),NumVecs+6);
1480
Bob Wilson07f6e802010-06-16 21:34:01 +00001481 // Form a REG_SEQUENCE to force register allocation.
1482 SDValue RegSeq;
1483 if (is64BitVector) {
1484 SDValue V0 = SDValue(VLdLn, 0);
1485 SDValue V1 = SDValue(VLdLn, 1);
1486 if (NumVecs == 2) {
1487 RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
Evan Cheng7189fd02010-05-15 07:53:37 +00001488 } else {
Bob Wilson07f6e802010-06-16 21:34:01 +00001489 SDValue V2 = SDValue(VLdLn, 2);
1490 // If it's a vld3, form a quad D-register but discard the last part.
1491 SDValue V3 = (NumVecs == 3)
1492 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1493 : SDValue(VLdLn, 3);
1494 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001495 }
Bob Wilson07f6e802010-06-16 21:34:01 +00001496 } else {
1497 // For 128-bit vectors, take the 64-bit results of the load and insert
1498 // them as subregs into the result.
1499 SDValue V[8];
1500 for (unsigned Vec = 0, i = 0; Vec < NumVecs; ++Vec, i+=2) {
1501 if (Even) {
1502 V[i] = SDValue(VLdLn, Vec);
1503 V[i+1] = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
1504 dl, RegVT), 0);
1505 } else {
1506 V[i] = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
1507 dl, RegVT), 0);
1508 V[i+1] = SDValue(VLdLn, Vec);
1509 }
1510 }
1511 if (NumVecs == 3)
1512 V[6] = V[7] = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
1513 dl, RegVT), 0);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001514
Bob Wilson07f6e802010-06-16 21:34:01 +00001515 if (NumVecs == 2)
1516 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V[0], V[1], V[2], V[3]), 0);
1517 else
1518 RegSeq = SDValue(OctoDRegs(MVT::v8i64, V[0], V[1], V[2], V[3],
1519 V[4], V[5], V[6], V[7]), 0);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001520 }
1521
Bob Wilson07f6e802010-06-16 21:34:01 +00001522 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1523 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1524 unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1525 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1526 ReplaceUses(SDValue(N, Vec),
1527 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, RegSeq));
1528 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdLn, NumVecs));
Bob Wilsona7c397c2009-10-14 16:19:03 +00001529 return NULL;
1530}
1531
Bob Wilson78dfbc32010-07-07 00:08:54 +00001532SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1533 unsigned Opc) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001534 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1535 DebugLoc dl = N->getDebugLoc();
1536 EVT VT = N->getValueType(0);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001537 unsigned FirstTblReg = IsExt ? 2 : 1;
Bob Wilsond491d6e2010-07-06 23:36:25 +00001538
1539 // Form a REG_SEQUENCE to force register allocation.
1540 SDValue RegSeq;
Bob Wilson78dfbc32010-07-07 00:08:54 +00001541 SDValue V0 = N->getOperand(FirstTblReg + 0);
1542 SDValue V1 = N->getOperand(FirstTblReg + 1);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001543 if (NumVecs == 2)
1544 RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1545 else {
Bob Wilson78dfbc32010-07-07 00:08:54 +00001546 SDValue V2 = N->getOperand(FirstTblReg + 2);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001547 // If it's a vtbl3, form a quad D-register and leave the last part as
1548 // an undef.
1549 SDValue V3 = (NumVecs == 3)
1550 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
Bob Wilson78dfbc32010-07-07 00:08:54 +00001551 : N->getOperand(FirstTblReg + 3);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001552 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1553 }
1554
1555 // Now extract the D registers back out.
Bob Wilson78dfbc32010-07-07 00:08:54 +00001556 SmallVector<SDValue, 6> Ops;
1557 if (IsExt)
1558 Ops.push_back(N->getOperand(1));
Bob Wilsond491d6e2010-07-06 23:36:25 +00001559 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_0, dl, VT, RegSeq));
1560 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_1, dl, VT, RegSeq));
1561 if (NumVecs > 2)
1562 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_2, dl, VT, RegSeq));
1563 if (NumVecs > 3)
1564 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::dsub_3, dl, VT, RegSeq));
1565
Bob Wilson78dfbc32010-07-07 00:08:54 +00001566 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
Bob Wilsond491d6e2010-07-06 23:36:25 +00001567 Ops.push_back(getAL(CurDAG)); // predicate
1568 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
Bob Wilson78dfbc32010-07-07 00:08:54 +00001569 return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
Bob Wilsond491d6e2010-07-06 23:36:25 +00001570}
1571
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001572SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001573 bool isSigned) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001574 if (!Subtarget->hasV6T2Ops())
1575 return NULL;
Bob Wilson96493442009-10-14 16:46:45 +00001576
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001577 unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1578 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1579
1580
1581 // For unsigned extracts, check for a shift right and mask
1582 unsigned And_imm = 0;
1583 if (N->getOpcode() == ISD::AND) {
1584 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1585
1586 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1587 if (And_imm & (And_imm + 1))
1588 return NULL;
1589
1590 unsigned Srl_imm = 0;
1591 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1592 Srl_imm)) {
1593 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1594
1595 unsigned Width = CountTrailingOnes_32(And_imm);
1596 unsigned LSB = Srl_imm;
1597 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1598 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1599 CurDAG->getTargetConstant(LSB, MVT::i32),
1600 CurDAG->getTargetConstant(Width, MVT::i32),
1601 getAL(CurDAG), Reg0 };
1602 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1603 }
1604 }
1605 return NULL;
1606 }
1607
1608 // Otherwise, we're looking for a shift of a shift
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001609 unsigned Shl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001610 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001611 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1612 unsigned Srl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001613 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001614 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1615 unsigned Width = 32 - Srl_imm;
1616 int LSB = Srl_imm - Shl_imm;
Evan Cheng8000c6c2009-10-22 00:40:00 +00001617 if (LSB < 0)
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001618 return NULL;
1619 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001620 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001621 CurDAG->getTargetConstant(LSB, MVT::i32),
1622 CurDAG->getTargetConstant(Width, MVT::i32),
1623 getAL(CurDAG), Reg0 };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001624 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001625 }
1626 }
1627 return NULL;
1628}
1629
Evan Cheng9ef48352009-11-20 00:54:03 +00001630SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001631SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001632 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1633 SDValue CPTmp0;
1634 SDValue CPTmp1;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001635 if (SelectT2ShifterOperandReg(N, TrueVal, CPTmp0, CPTmp1)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001636 unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1637 unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1638 unsigned Opc = 0;
1639 switch (SOShOp) {
1640 case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1641 case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1642 case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1643 case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1644 default:
1645 llvm_unreachable("Unknown so_reg opcode!");
1646 break;
1647 }
1648 SDValue SOShImm =
1649 CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1650 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1651 SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001652 return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
Evan Cheng9ef48352009-11-20 00:54:03 +00001653 }
1654 return 0;
1655}
1656
1657SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001658SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001659 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1660 SDValue CPTmp0;
1661 SDValue CPTmp1;
1662 SDValue CPTmp2;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001663 if (SelectShifterOperandReg(N, TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001664 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1665 SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001666 return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
Evan Cheng9ef48352009-11-20 00:54:03 +00001667 }
1668 return 0;
1669}
1670
1671SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001672SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001673 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1674 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1675 if (!T)
1676 return 0;
1677
1678 if (Predicate_t2_so_imm(TrueVal.getNode())) {
1679 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1680 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1681 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001682 return CurDAG->SelectNodeTo(N,
Evan Cheng9ef48352009-11-20 00:54:03 +00001683 ARM::t2MOVCCi, MVT::i32, Ops, 5);
1684 }
1685 return 0;
1686}
1687
1688SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001689SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001690 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1691 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1692 if (!T)
1693 return 0;
1694
1695 if (Predicate_so_imm(TrueVal.getNode())) {
1696 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1697 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1698 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001699 return CurDAG->SelectNodeTo(N,
Evan Cheng9ef48352009-11-20 00:54:03 +00001700 ARM::MOVCCi, MVT::i32, Ops, 5);
1701 }
1702 return 0;
1703}
1704
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001705SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
1706 EVT VT = N->getValueType(0);
1707 SDValue FalseVal = N->getOperand(0);
1708 SDValue TrueVal = N->getOperand(1);
1709 SDValue CC = N->getOperand(2);
1710 SDValue CCR = N->getOperand(3);
1711 SDValue InFlag = N->getOperand(4);
Evan Cheng9ef48352009-11-20 00:54:03 +00001712 assert(CC.getOpcode() == ISD::Constant);
1713 assert(CCR.getOpcode() == ISD::Register);
1714 ARMCC::CondCodes CCVal =
1715 (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
Evan Cheng07ba9062009-11-19 21:45:22 +00001716
1717 if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1718 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1719 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1720 // Pattern complexity = 18 cost = 1 size = 0
1721 SDValue CPTmp0;
1722 SDValue CPTmp1;
1723 SDValue CPTmp2;
1724 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001725 SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001726 CCVal, CCR, InFlag);
1727 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001728 Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001729 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1730 if (Res)
1731 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001732 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001733 SDNode *Res = SelectARMCMOVShiftOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001734 CCVal, CCR, InFlag);
1735 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001736 Res = SelectARMCMOVShiftOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001737 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1738 if (Res)
1739 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001740 }
1741
1742 // Pattern: (ARMcmov:i32 GPR:i32:$false,
1743 // (imm:i32)<<P:Predicate_so_imm>>:$true,
1744 // (imm:i32):$cc)
1745 // Emits: (MOVCCi:i32 GPR:i32:$false,
1746 // (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1747 // Pattern complexity = 10 cost = 1 size = 0
Evan Cheng9ef48352009-11-20 00:54:03 +00001748 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001749 SDNode *Res = SelectT2CMOVSoImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001750 CCVal, CCR, InFlag);
1751 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001752 Res = SelectT2CMOVSoImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001753 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1754 if (Res)
1755 return Res;
1756 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001757 SDNode *Res = SelectARMCMOVSoImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001758 CCVal, CCR, InFlag);
1759 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001760 Res = SelectARMCMOVSoImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001761 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1762 if (Res)
1763 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001764 }
1765 }
1766
1767 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1768 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1769 // Pattern complexity = 6 cost = 1 size = 0
1770 //
1771 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1772 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1773 // Pattern complexity = 6 cost = 11 size = 0
1774 //
1775 // Also FCPYScc and FCPYDcc.
Evan Cheng9ef48352009-11-20 00:54:03 +00001776 SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1777 SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
Evan Cheng07ba9062009-11-19 21:45:22 +00001778 unsigned Opc = 0;
1779 switch (VT.getSimpleVT().SimpleTy) {
1780 default: assert(false && "Illegal conditional move type!");
1781 break;
1782 case MVT::i32:
1783 Opc = Subtarget->isThumb()
1784 ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1785 : ARM::MOVCCr;
1786 break;
1787 case MVT::f32:
1788 Opc = ARM::VMOVScc;
1789 break;
1790 case MVT::f64:
1791 Opc = ARM::VMOVDcc;
1792 break;
1793 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001794 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Cheng07ba9062009-11-19 21:45:22 +00001795}
1796
Evan Chengde8aa4e2010-05-05 18:28:36 +00001797SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
1798 // The only time a CONCAT_VECTORS operation can have legal types is when
1799 // two 64-bit vectors are concatenated to a 128-bit vector.
1800 EVT VT = N->getValueType(0);
1801 if (!VT.is128BitVector() || N->getNumOperands() != 2)
1802 llvm_unreachable("unexpected CONCAT_VECTORS");
1803 DebugLoc dl = N->getDebugLoc();
1804 SDValue V0 = N->getOperand(0);
1805 SDValue V1 = N->getOperand(1);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001806 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1807 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Evan Chengde8aa4e2010-05-05 18:28:36 +00001808 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1809 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1810}
1811
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001812SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
Dale Johannesened2eee62009-02-06 01:31:28 +00001813 DebugLoc dl = N->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001814
Dan Gohmane8be6c62008-07-17 19:10:17 +00001815 if (N->isMachineOpcode())
Evan Chenga8e29892007-01-19 07:51:42 +00001816 return NULL; // Already selected.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001817
1818 switch (N->getOpcode()) {
Evan Chenga8e29892007-01-19 07:51:42 +00001819 default: break;
1820 case ISD::Constant: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001821 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001822 bool UseCP = true;
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00001823 if (Subtarget->hasThumb2())
1824 // Thumb2-aware targets have the MOVT instruction, so all immediates can
1825 // be done with MOV + MOVT, at worst.
1826 UseCP = 0;
1827 else {
1828 if (Subtarget->isThumb()) {
Bob Wilsone64e3cf2009-06-22 17:29:13 +00001829 UseCP = (Val > 255 && // MOV
1830 ~Val > 255 && // MOV + MVN
1831 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00001832 } else
1833 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
1834 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
1835 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
1836 }
1837
Evan Chenga8e29892007-01-19 07:51:42 +00001838 if (UseCP) {
Dan Gohman475871a2008-07-27 21:46:04 +00001839 SDValue CPIdx =
Owen Anderson1d0be152009-08-13 21:58:54 +00001840 CurDAG->getTargetConstantPool(ConstantInt::get(
1841 Type::getInt32Ty(*CurDAG->getContext()), Val),
Evan Chenga8e29892007-01-19 07:51:42 +00001842 TLI.getPointerTy());
Evan Cheng012f2d92007-01-24 08:53:17 +00001843
1844 SDNode *ResNode;
Evan Cheng446c4282009-07-11 06:43:01 +00001845 if (Subtarget->isThumb1Only()) {
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001846 SDValue Pred = getAL(CurDAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00001847 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
Evan Cheng446c4282009-07-11 06:43:01 +00001848 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
Dan Gohman602b0c82009-09-25 18:54:59 +00001849 ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
1850 Ops, 4);
Evan Cheng446c4282009-07-11 06:43:01 +00001851 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001852 SDValue Ops[] = {
Jim Grosbach764ab522009-08-11 15:33:49 +00001853 CPIdx,
Owen Anderson825b72b2009-08-11 20:47:22 +00001854 CurDAG->getRegister(0, MVT::i32),
1855 CurDAG->getTargetConstant(0, MVT::i32),
Evan Chengee568cf2007-07-05 07:15:27 +00001856 getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +00001857 CurDAG->getRegister(0, MVT::i32),
Evan Cheng012f2d92007-01-24 08:53:17 +00001858 CurDAG->getEntryNode()
1859 };
Dan Gohman602b0c82009-09-25 18:54:59 +00001860 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
1861 Ops, 6);
Evan Cheng012f2d92007-01-24 08:53:17 +00001862 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001863 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
Evan Chenga8e29892007-01-19 07:51:42 +00001864 return NULL;
1865 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001866
Evan Chenga8e29892007-01-19 07:51:42 +00001867 // Other cases are autogenerated.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001868 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001869 }
Rafael Espindolaf819a492006-11-09 13:58:55 +00001870 case ISD::FrameIndex: {
Evan Chenga8e29892007-01-19 07:51:42 +00001871 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindolaf819a492006-11-09 13:58:55 +00001872 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohman475871a2008-07-27 21:46:04 +00001873 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
David Goodwinf1daf7d2009-07-08 23:10:31 +00001874 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001875 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
1876 CurDAG->getTargetConstant(0, MVT::i32));
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001877 } else {
David Goodwin419c6152009-07-14 18:48:51 +00001878 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
1879 ARM::t2ADDri : ARM::ADDri);
Owen Anderson825b72b2009-08-11 20:47:22 +00001880 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
1881 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1882 CurDAG->getRegister(0, MVT::i32) };
1883 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00001884 }
Evan Chenga8e29892007-01-19 07:51:42 +00001885 }
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001886 case ISD::SRL:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001887 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001888 return I;
1889 break;
1890 case ISD::SRA:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001891 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001892 return I;
1893 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001894 case ISD::MUL:
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001895 if (Subtarget->isThumb1Only())
Evan Cheng79d43262007-01-24 02:21:22 +00001896 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001897 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001898 unsigned RHSV = C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001899 if (!RHSV) break;
1900 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00001901 unsigned ShImm = Log2_32(RHSV-1);
1902 if (ShImm >= 32)
1903 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001904 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001905 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00001906 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1907 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00001908 if (Subtarget->isThumb()) {
Evan Chengaf9e7a72009-07-21 00:31:12 +00001909 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001910 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001911 } else {
1912 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001913 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001914 }
Evan Chenga8e29892007-01-19 07:51:42 +00001915 }
1916 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00001917 unsigned ShImm = Log2_32(RHSV+1);
1918 if (ShImm >= 32)
1919 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001920 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001921 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00001922 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1923 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00001924 if (Subtarget->isThumb()) {
Bob Wilson13ef8402010-05-28 00:27:15 +00001925 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1926 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001927 } else {
1928 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001929 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001930 }
Evan Chenga8e29892007-01-19 07:51:42 +00001931 }
1932 }
1933 break;
Evan Cheng20956592009-10-21 08:15:52 +00001934 case ISD::AND: {
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001935 // Check for unsigned bitfield extract
1936 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
1937 return I;
1938
Evan Cheng20956592009-10-21 08:15:52 +00001939 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
1940 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
1941 // are entirely contributed by c2 and lower 16-bits are entirely contributed
1942 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
1943 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001944 EVT VT = N->getValueType(0);
Evan Cheng20956592009-10-21 08:15:52 +00001945 if (VT != MVT::i32)
1946 break;
1947 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
1948 ? ARM::t2MOVTi16
1949 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
1950 if (!Opc)
1951 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001952 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Evan Cheng20956592009-10-21 08:15:52 +00001953 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1954 if (!N1C)
1955 break;
1956 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
1957 SDValue N2 = N0.getOperand(1);
1958 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1959 if (!N2C)
1960 break;
1961 unsigned N1CVal = N1C->getZExtValue();
1962 unsigned N2CVal = N2C->getZExtValue();
1963 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
1964 (N1CVal & 0xffffU) == 0xffffU &&
1965 (N2CVal & 0xffffU) == 0x0U) {
1966 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
1967 MVT::i32);
1968 SDValue Ops[] = { N0.getOperand(0), Imm16,
1969 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1970 return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
1971 }
1972 }
1973 break;
1974 }
Jim Grosbache5165492009-11-09 00:11:35 +00001975 case ARMISD::VMOVRRD:
1976 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001977 N->getOperand(0), getAL(CurDAG),
Dan Gohman602b0c82009-09-25 18:54:59 +00001978 CurDAG->getRegister(0, MVT::i32));
Dan Gohman525178c2007-10-08 18:33:35 +00001979 case ISD::UMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001980 if (Subtarget->isThumb1Only())
1981 break;
1982 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001983 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001984 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1985 CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00001986 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001987 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001988 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001989 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1990 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00001991 return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001992 }
Evan Chengee568cf2007-07-05 07:15:27 +00001993 }
Dan Gohman525178c2007-10-08 18:33:35 +00001994 case ISD::SMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001995 if (Subtarget->isThumb1Only())
1996 break;
1997 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001998 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001999 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00002000 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00002001 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002002 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00002003 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2004 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00002005 return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00002006 }
Evan Chengee568cf2007-07-05 07:15:27 +00002007 }
Evan Chenga8e29892007-01-19 07:51:42 +00002008 case ISD::LOAD: {
Evan Chenge88d5ce2009-07-02 07:28:31 +00002009 SDNode *ResNode = 0;
Evan Cheng5b9fcd12009-07-07 01:17:28 +00002010 if (Subtarget->isThumb() && Subtarget->hasThumb2())
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002011 ResNode = SelectT2IndexedLoad(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +00002012 else
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002013 ResNode = SelectARMIndexedLoad(N);
Evan Chengaf4550f2009-07-02 01:23:32 +00002014 if (ResNode)
2015 return ResNode;
Bob Wilsondf9a4f02010-03-23 18:54:46 +00002016
2017 // VLDMQ must be custom-selected for "v2f64 load" to set the AM5Opc value.
2018 if (Subtarget->hasVFP2() &&
2019 N->getValueType(0).getSimpleVT().SimpleTy == MVT::v2f64) {
2020 SDValue Chain = N->getOperand(0);
2021 SDValue AM5Opc =
2022 CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::ia, 4), MVT::i32);
Evan Cheng47b7b9f2010-04-16 05:46:06 +00002023 SDValue Pred = getAL(CurDAG);
Bob Wilsondf9a4f02010-03-23 18:54:46 +00002024 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2025 SDValue Ops[] = { N->getOperand(1), AM5Opc, Pred, PredReg, Chain };
Evan Cheng3c3195c2010-05-19 06:06:09 +00002026 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2027 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2028 SDNode *Ret = CurDAG->getMachineNode(ARM::VLDMQ, dl,
2029 MVT::v2f64, MVT::Other, Ops, 5);
2030 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
2031 return Ret;
Bob Wilsondf9a4f02010-03-23 18:54:46 +00002032 }
2033 // Other cases are autogenerated.
2034 break;
2035 }
2036 case ISD::STORE: {
2037 // VSTMQ must be custom-selected for "v2f64 store" to set the AM5Opc value.
2038 if (Subtarget->hasVFP2() &&
2039 N->getOperand(1).getValueType().getSimpleVT().SimpleTy == MVT::v2f64) {
2040 SDValue Chain = N->getOperand(0);
2041 SDValue AM5Opc =
2042 CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::ia, 4), MVT::i32);
Evan Cheng47b7b9f2010-04-16 05:46:06 +00002043 SDValue Pred = getAL(CurDAG);
Bob Wilsondf9a4f02010-03-23 18:54:46 +00002044 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2045 SDValue Ops[] = { N->getOperand(1), N->getOperand(2),
2046 AM5Opc, Pred, PredReg, Chain };
Evan Cheng3c3195c2010-05-19 06:06:09 +00002047 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2048 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2049 SDNode *Ret = CurDAG->getMachineNode(ARM::VSTMQ, dl, MVT::Other, Ops, 6);
2050 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
2051 return Ret;
Bob Wilsondf9a4f02010-03-23 18:54:46 +00002052 }
Evan Chenga8e29892007-01-19 07:51:42 +00002053 // Other cases are autogenerated.
Rafael Espindolaf819a492006-11-09 13:58:55 +00002054 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +00002055 }
Evan Chengee568cf2007-07-05 07:15:27 +00002056 case ARMISD::BRCOND: {
2057 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2058 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2059 // Pattern complexity = 6 cost = 1 size = 0
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002060
Evan Chengee568cf2007-07-05 07:15:27 +00002061 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2062 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
2063 // Pattern complexity = 6 cost = 1 size = 0
2064
David Goodwin5e47a9a2009-06-30 18:04:13 +00002065 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2066 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2067 // Pattern complexity = 6 cost = 1 size = 0
2068
Jim Grosbach764ab522009-08-11 15:33:49 +00002069 unsigned Opc = Subtarget->isThumb() ?
David Goodwin5e47a9a2009-06-30 18:04:13 +00002070 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002071 SDValue Chain = N->getOperand(0);
2072 SDValue N1 = N->getOperand(1);
2073 SDValue N2 = N->getOperand(2);
2074 SDValue N3 = N->getOperand(3);
2075 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00002076 assert(N1.getOpcode() == ISD::BasicBlock);
2077 assert(N2.getOpcode() == ISD::Constant);
2078 assert(N3.getOpcode() == ISD::Register);
2079
Dan Gohman475871a2008-07-27 21:46:04 +00002080 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002081 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00002082 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002083 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
Dan Gohman602b0c82009-09-25 18:54:59 +00002084 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
2085 MVT::Flag, Ops, 5);
Dan Gohman475871a2008-07-27 21:46:04 +00002086 Chain = SDValue(ResNode, 0);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002087 if (N->getNumValues() == 2) {
Dan Gohman475871a2008-07-27 21:46:04 +00002088 InFlag = SDValue(ResNode, 1);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002089 ReplaceUses(SDValue(N, 1), InFlag);
Chris Lattnera47b9bc2008-02-03 03:20:59 +00002090 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002091 ReplaceUses(SDValue(N, 0),
Evan Chenged54de42009-11-19 08:16:50 +00002092 SDValue(Chain.getNode(), Chain.getResNo()));
Evan Chengee568cf2007-07-05 07:15:27 +00002093 return NULL;
2094 }
Evan Cheng07ba9062009-11-19 21:45:22 +00002095 case ARMISD::CMOV:
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002096 return SelectCMOVOp(N);
Evan Chengee568cf2007-07-05 07:15:27 +00002097 case ARMISD::CNEG: {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002098 EVT VT = N->getValueType(0);
2099 SDValue N0 = N->getOperand(0);
2100 SDValue N1 = N->getOperand(1);
2101 SDValue N2 = N->getOperand(2);
2102 SDValue N3 = N->getOperand(3);
2103 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00002104 assert(N2.getOpcode() == ISD::Constant);
2105 assert(N3.getOpcode() == ISD::Register);
2106
Dan Gohman475871a2008-07-27 21:46:04 +00002107 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002108 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00002109 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002110 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
Evan Chengee568cf2007-07-05 07:15:27 +00002111 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00002112 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengee568cf2007-07-05 07:15:27 +00002113 default: assert(false && "Illegal conditional move type!");
2114 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002115 case MVT::f32:
Jim Grosbache5165492009-11-09 00:11:35 +00002116 Opc = ARM::VNEGScc;
Evan Chengee568cf2007-07-05 07:15:27 +00002117 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002118 case MVT::f64:
Jim Grosbache5165492009-11-09 00:11:35 +00002119 Opc = ARM::VNEGDcc;
Evan Chenge5ad88e2008-12-10 21:54:21 +00002120 break;
Evan Chengee568cf2007-07-05 07:15:27 +00002121 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002122 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00002123 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00002124
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002125 case ARMISD::VZIP: {
2126 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00002127 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002128 switch (VT.getSimpleVT().SimpleTy) {
2129 default: return NULL;
2130 case MVT::v8i8: Opc = ARM::VZIPd8; break;
2131 case MVT::v4i16: Opc = ARM::VZIPd16; break;
2132 case MVT::v2f32:
2133 case MVT::v2i32: Opc = ARM::VZIPd32; break;
2134 case MVT::v16i8: Opc = ARM::VZIPq8; break;
2135 case MVT::v8i16: Opc = ARM::VZIPq16; break;
2136 case MVT::v4f32:
2137 case MVT::v4i32: Opc = ARM::VZIPq32; break;
2138 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00002139 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00002140 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2141 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2142 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00002143 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002144 case ARMISD::VUZP: {
2145 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00002146 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002147 switch (VT.getSimpleVT().SimpleTy) {
2148 default: return NULL;
2149 case MVT::v8i8: Opc = ARM::VUZPd8; break;
2150 case MVT::v4i16: Opc = ARM::VUZPd16; break;
2151 case MVT::v2f32:
2152 case MVT::v2i32: Opc = ARM::VUZPd32; break;
2153 case MVT::v16i8: Opc = ARM::VUZPq8; break;
2154 case MVT::v8i16: Opc = ARM::VUZPq16; break;
2155 case MVT::v4f32:
2156 case MVT::v4i32: Opc = ARM::VUZPq32; break;
2157 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00002158 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00002159 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2160 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2161 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00002162 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002163 case ARMISD::VTRN: {
2164 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00002165 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002166 switch (VT.getSimpleVT().SimpleTy) {
2167 default: return NULL;
2168 case MVT::v8i8: Opc = ARM::VTRNd8; break;
2169 case MVT::v4i16: Opc = ARM::VTRNd16; break;
2170 case MVT::v2f32:
2171 case MVT::v2i32: Opc = ARM::VTRNd32; break;
2172 case MVT::v16i8: Opc = ARM::VTRNq8; break;
2173 case MVT::v8i16: Opc = ARM::VTRNq16; break;
2174 case MVT::v4f32:
2175 case MVT::v4i32: Opc = ARM::VTRNq32; break;
2176 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00002177 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00002178 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2179 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2180 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00002181 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00002182 case ARMISD::BUILD_VECTOR: {
2183 EVT VecVT = N->getValueType(0);
2184 EVT EltVT = VecVT.getVectorElementType();
2185 unsigned NumElts = VecVT.getVectorNumElements();
2186 if (EltVT.getSimpleVT() == MVT::f64) {
2187 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
2188 return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
2189 }
2190 assert(EltVT.getSimpleVT() == MVT::f32 &&
2191 "unexpected type for BUILD_VECTOR");
2192 if (NumElts == 2)
2193 return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
2194 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
2195 return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
2196 N->getOperand(2), N->getOperand(3));
2197 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00002198
2199 case ISD::INTRINSIC_VOID:
2200 case ISD::INTRINSIC_W_CHAIN: {
2201 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Bob Wilson31fb12f2009-08-26 17:39:53 +00002202 switch (IntNo) {
2203 default:
Bob Wilson429009b2010-05-06 16:05:26 +00002204 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00002205
Bob Wilson621f1952010-03-23 05:25:43 +00002206 case Intrinsic::arm_neon_vld1: {
2207 unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
2208 ARM::VLD1d32, ARM::VLD1d64 };
2209 unsigned QOpcodes[] = { ARM::VLD1q8, ARM::VLD1q16,
2210 ARM::VLD1q32, ARM::VLD1q64 };
2211 return SelectVLD(N, 1, DOpcodes, QOpcodes, 0);
2212 }
2213
Bob Wilson31fb12f2009-08-26 17:39:53 +00002214 case Intrinsic::arm_neon_vld2: {
Bob Wilson3e36f132009-10-14 17:28:52 +00002215 unsigned DOpcodes[] = { ARM::VLD2d8, ARM::VLD2d16,
Bob Wilson621f1952010-03-23 05:25:43 +00002216 ARM::VLD2d32, ARM::VLD1q64 };
Bob Wilson3e36f132009-10-14 17:28:52 +00002217 unsigned QOpcodes[] = { ARM::VLD2q8, ARM::VLD2q16, ARM::VLD2q32 };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002218 return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002219 }
2220
2221 case Intrinsic::arm_neon_vld3: {
Bob Wilson3e36f132009-10-14 17:28:52 +00002222 unsigned DOpcodes[] = { ARM::VLD3d8, ARM::VLD3d16,
Bob Wilsona6979752010-03-22 18:13:18 +00002223 ARM::VLD3d32, ARM::VLD1d64T };
Bob Wilson95ffecd2010-03-20 18:35:24 +00002224 unsigned QOpcodes0[] = { ARM::VLD3q8_UPD,
2225 ARM::VLD3q16_UPD,
2226 ARM::VLD3q32_UPD };
2227 unsigned QOpcodes1[] = { ARM::VLD3q8odd_UPD,
2228 ARM::VLD3q16odd_UPD,
2229 ARM::VLD3q32odd_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002230 return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002231 }
2232
2233 case Intrinsic::arm_neon_vld4: {
Bob Wilson3e36f132009-10-14 17:28:52 +00002234 unsigned DOpcodes[] = { ARM::VLD4d8, ARM::VLD4d16,
Bob Wilsona6979752010-03-22 18:13:18 +00002235 ARM::VLD4d32, ARM::VLD1d64Q };
Bob Wilson95ffecd2010-03-20 18:35:24 +00002236 unsigned QOpcodes0[] = { ARM::VLD4q8_UPD,
2237 ARM::VLD4q16_UPD,
2238 ARM::VLD4q32_UPD };
2239 unsigned QOpcodes1[] = { ARM::VLD4q8odd_UPD,
2240 ARM::VLD4q16odd_UPD,
2241 ARM::VLD4q32odd_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002242 return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002243 }
2244
Bob Wilson243fcc52009-09-01 04:26:28 +00002245 case Intrinsic::arm_neon_vld2lane: {
Bob Wilsona7c397c2009-10-14 16:19:03 +00002246 unsigned DOpcodes[] = { ARM::VLD2LNd8, ARM::VLD2LNd16, ARM::VLD2LNd32 };
Bob Wilson95ffecd2010-03-20 18:35:24 +00002247 unsigned QOpcodes0[] = { ARM::VLD2LNq16, ARM::VLD2LNq32 };
2248 unsigned QOpcodes1[] = { ARM::VLD2LNq16odd, ARM::VLD2LNq32odd };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002249 return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson243fcc52009-09-01 04:26:28 +00002250 }
2251
2252 case Intrinsic::arm_neon_vld3lane: {
Bob Wilsona7c397c2009-10-14 16:19:03 +00002253 unsigned DOpcodes[] = { ARM::VLD3LNd8, ARM::VLD3LNd16, ARM::VLD3LNd32 };
Bob Wilson95ffecd2010-03-20 18:35:24 +00002254 unsigned QOpcodes0[] = { ARM::VLD3LNq16, ARM::VLD3LNq32 };
2255 unsigned QOpcodes1[] = { ARM::VLD3LNq16odd, ARM::VLD3LNq32odd };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002256 return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson243fcc52009-09-01 04:26:28 +00002257 }
2258
2259 case Intrinsic::arm_neon_vld4lane: {
Bob Wilsona7c397c2009-10-14 16:19:03 +00002260 unsigned DOpcodes[] = { ARM::VLD4LNd8, ARM::VLD4LNd16, ARM::VLD4LNd32 };
Bob Wilson95ffecd2010-03-20 18:35:24 +00002261 unsigned QOpcodes0[] = { ARM::VLD4LNq16, ARM::VLD4LNq32 };
2262 unsigned QOpcodes1[] = { ARM::VLD4LNq16odd, ARM::VLD4LNq32odd };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002263 return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson243fcc52009-09-01 04:26:28 +00002264 }
2265
Bob Wilson11d98992010-03-23 06:20:33 +00002266 case Intrinsic::arm_neon_vst1: {
2267 unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2268 ARM::VST1d32, ARM::VST1d64 };
2269 unsigned QOpcodes[] = { ARM::VST1q8, ARM::VST1q16,
2270 ARM::VST1q32, ARM::VST1q64 };
2271 return SelectVST(N, 1, DOpcodes, QOpcodes, 0);
2272 }
2273
Bob Wilson31fb12f2009-08-26 17:39:53 +00002274 case Intrinsic::arm_neon_vst2: {
Bob Wilson24f995d2009-10-14 18:32:29 +00002275 unsigned DOpcodes[] = { ARM::VST2d8, ARM::VST2d16,
Bob Wilson11d98992010-03-23 06:20:33 +00002276 ARM::VST2d32, ARM::VST1q64 };
Bob Wilson24f995d2009-10-14 18:32:29 +00002277 unsigned QOpcodes[] = { ARM::VST2q8, ARM::VST2q16, ARM::VST2q32 };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002278 return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002279 }
2280
2281 case Intrinsic::arm_neon_vst3: {
Bob Wilson24f995d2009-10-14 18:32:29 +00002282 unsigned DOpcodes[] = { ARM::VST3d8, ARM::VST3d16,
Bob Wilsona6979752010-03-22 18:13:18 +00002283 ARM::VST3d32, ARM::VST1d64T };
Bob Wilson95ffecd2010-03-20 18:35:24 +00002284 unsigned QOpcodes0[] = { ARM::VST3q8_UPD,
2285 ARM::VST3q16_UPD,
2286 ARM::VST3q32_UPD };
2287 unsigned QOpcodes1[] = { ARM::VST3q8odd_UPD,
2288 ARM::VST3q16odd_UPD,
2289 ARM::VST3q32odd_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002290 return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002291 }
2292
2293 case Intrinsic::arm_neon_vst4: {
Bob Wilson24f995d2009-10-14 18:32:29 +00002294 unsigned DOpcodes[] = { ARM::VST4d8, ARM::VST4d16,
Bob Wilsona6979752010-03-22 18:13:18 +00002295 ARM::VST4d32, ARM::VST1d64Q };
Bob Wilson95ffecd2010-03-20 18:35:24 +00002296 unsigned QOpcodes0[] = { ARM::VST4q8_UPD,
2297 ARM::VST4q16_UPD,
2298 ARM::VST4q32_UPD };
2299 unsigned QOpcodes1[] = { ARM::VST4q8odd_UPD,
2300 ARM::VST4q16odd_UPD,
2301 ARM::VST4q32odd_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002302 return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002303 }
Bob Wilson8a3198b2009-09-01 18:51:56 +00002304
2305 case Intrinsic::arm_neon_vst2lane: {
Bob Wilson96493442009-10-14 16:46:45 +00002306 unsigned DOpcodes[] = { ARM::VST2LNd8, ARM::VST2LNd16, ARM::VST2LNd32 };
Bob Wilson95ffecd2010-03-20 18:35:24 +00002307 unsigned QOpcodes0[] = { ARM::VST2LNq16, ARM::VST2LNq32 };
2308 unsigned QOpcodes1[] = { ARM::VST2LNq16odd, ARM::VST2LNq32odd };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002309 return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002310 }
2311
2312 case Intrinsic::arm_neon_vst3lane: {
Bob Wilson96493442009-10-14 16:46:45 +00002313 unsigned DOpcodes[] = { ARM::VST3LNd8, ARM::VST3LNd16, ARM::VST3LNd32 };
Bob Wilson95ffecd2010-03-20 18:35:24 +00002314 unsigned QOpcodes0[] = { ARM::VST3LNq16, ARM::VST3LNq32 };
2315 unsigned QOpcodes1[] = { ARM::VST3LNq16odd, ARM::VST3LNq32odd };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002316 return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002317 }
2318
2319 case Intrinsic::arm_neon_vst4lane: {
Bob Wilson96493442009-10-14 16:46:45 +00002320 unsigned DOpcodes[] = { ARM::VST4LNd8, ARM::VST4LNd16, ARM::VST4LNd32 };
Bob Wilson95ffecd2010-03-20 18:35:24 +00002321 unsigned QOpcodes0[] = { ARM::VST4LNq16, ARM::VST4LNq32 };
2322 unsigned QOpcodes1[] = { ARM::VST4LNq16odd, ARM::VST4LNq32odd };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002323 return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002324 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00002325 }
Bob Wilson429009b2010-05-06 16:05:26 +00002326 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00002327 }
Evan Chengde8aa4e2010-05-05 18:28:36 +00002328
Bob Wilsond491d6e2010-07-06 23:36:25 +00002329 case ISD::INTRINSIC_WO_CHAIN: {
2330 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2331 switch (IntNo) {
2332 default:
2333 break;
2334
2335 case Intrinsic::arm_neon_vtbl2:
Bob Wilson78dfbc32010-07-07 00:08:54 +00002336 return SelectVTBL(N, false, 2, ARM::VTBL2);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002337 case Intrinsic::arm_neon_vtbl3:
Bob Wilson78dfbc32010-07-07 00:08:54 +00002338 return SelectVTBL(N, false, 3, ARM::VTBL3);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002339 case Intrinsic::arm_neon_vtbl4:
Bob Wilson78dfbc32010-07-07 00:08:54 +00002340 return SelectVTBL(N, false, 4, ARM::VTBL4);
2341
2342 case Intrinsic::arm_neon_vtbx2:
2343 return SelectVTBL(N, true, 2, ARM::VTBX2);
2344 case Intrinsic::arm_neon_vtbx3:
2345 return SelectVTBL(N, true, 3, ARM::VTBX3);
2346 case Intrinsic::arm_neon_vtbx4:
2347 return SelectVTBL(N, true, 4, ARM::VTBX4);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002348 }
2349 break;
2350 }
2351
Bob Wilson429009b2010-05-06 16:05:26 +00002352 case ISD::CONCAT_VECTORS:
Evan Chengde8aa4e2010-05-05 18:28:36 +00002353 return SelectConcatVector(N);
2354 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00002355
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002356 return SelectCode(N);
Evan Chenga8e29892007-01-19 07:51:42 +00002357}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002358
Bob Wilson224c2442009-05-19 05:53:42 +00002359bool ARMDAGToDAGISel::
2360SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2361 std::vector<SDValue> &OutOps) {
2362 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
Bob Wilson765cc0b2009-10-13 20:50:28 +00002363 // Require the address to be in a register. That is safe for all ARM
2364 // variants and it is hard to do anything much smarter without knowing
2365 // how the operand is used.
2366 OutOps.push_back(Op);
Bob Wilson224c2442009-05-19 05:53:42 +00002367 return false;
2368}
2369
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002370/// createARMISelDag - This pass converts a legalized DAG into a
2371/// ARM-specific DAG, ready for instruction scheduling.
2372///
Bob Wilson522ce972009-09-28 14:30:20 +00002373FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2374 CodeGenOpt::Level OptLevel) {
2375 return new ARMDAGToDAGISel(TM, OptLevel);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002376}