blob: c59ad8b54abf8da2a9edef70d0cb6557f4588fd1 [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
Evan Chenga2c519b2010-07-30 23:33:54 +000039static cl::opt<bool>
40DisableShifterOp("disable-shifter-op", cl::Hidden,
41 cl::desc("Disable isel of shifter-op"),
42 cl::init(false));
43
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000044//===--------------------------------------------------------------------===//
45/// ARMDAGToDAGISel - ARM specific code to select ARM machine
46/// instructions for SelectionDAG operations.
47///
48namespace {
49class ARMDAGToDAGISel : public SelectionDAGISel {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000050 ARMBaseTargetMachine &TM;
Evan Cheng3f7eb8e2008-09-18 07:24:33 +000051
Evan Chenga8e29892007-01-19 07:51:42 +000052 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
53 /// make the right decision when generating code for different targets.
54 const ARMSubtarget *Subtarget;
55
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000056public:
Bob Wilson522ce972009-09-28 14:30:20 +000057 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm,
58 CodeGenOpt::Level OptLevel)
59 : SelectionDAGISel(tm, OptLevel), TM(tm),
Evan Chenga8e29892007-01-19 07:51:42 +000060 Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000061 }
62
Evan Chenga8e29892007-01-19 07:51:42 +000063 virtual const char *getPassName() const {
64 return "ARM Instruction Selection";
Anton Korobeynikov52237112009-06-17 18:13:58 +000065 }
66
Bob Wilsonaf4a8912009-10-08 18:51:31 +000067 /// getI32Imm - Return a target constant of type i32 with the specified
68 /// value.
Anton Korobeynikov52237112009-06-17 18:13:58 +000069 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +000070 return CurDAG->getTargetConstant(Imm, MVT::i32);
Anton Korobeynikov52237112009-06-17 18:13:58 +000071 }
72
Dan Gohmaneeb3a002010-01-05 01:24:18 +000073 SDNode *Select(SDNode *N);
Evan Cheng014bf212010-02-15 19:41:07 +000074
Chris Lattner52a261b2010-09-21 20:31:19 +000075 bool SelectShifterOperandReg(SDValue N, SDValue &A,
Evan Cheng055b0312009-06-29 07:51:04 +000076 SDValue &B, SDValue &C);
Chris Lattner52a261b2010-09-21 20:31:19 +000077 bool SelectAddrMode2(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000078 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000079 bool SelectAddrMode2Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +000080 SDValue &Offset, SDValue &Opc);
Chris Lattner52a261b2010-09-21 20:31:19 +000081 bool SelectAddrMode3(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000082 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000083 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +000084 SDValue &Offset, SDValue &Opc);
Chris Lattner52a261b2010-09-21 20:31:19 +000085 bool SelectAddrMode4(SDValue N, SDValue &Addr, SDValue &Mode);
86 bool SelectAddrMode5(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000087 SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +000088 bool SelectAddrMode6(SDValue N, SDValue &Addr, SDValue &Align);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000089
Chris Lattner52a261b2010-09-21 20:31:19 +000090 bool SelectAddrModePC(SDValue N, SDValue &Offset,
Bob Wilson8b024a52009-07-01 23:16:05 +000091 SDValue &Label);
Evan Chenga8e29892007-01-19 07:51:42 +000092
Chris Lattner52a261b2010-09-21 20:31:19 +000093 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
94 bool SelectThumbAddrModeRI5(SDValue N, unsigned Scale,
Dan Gohman475871a2008-07-27 21:46:04 +000095 SDValue &Base, SDValue &OffImm,
96 SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +000097 bool SelectThumbAddrModeS1(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000098 SDValue &OffImm, SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +000099 bool SelectThumbAddrModeS2(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000100 SDValue &OffImm, SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +0000101 bool SelectThumbAddrModeS4(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000102 SDValue &OffImm, SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +0000103 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000104
Chris Lattner52a261b2010-09-21 20:31:19 +0000105 bool SelectT2ShifterOperandReg(SDValue N,
Evan Cheng9cb9e672009-06-27 02:26:13 +0000106 SDValue &BaseReg, SDValue &Opc);
Chris Lattner52a261b2010-09-21 20:31:19 +0000107 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
108 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
Evan Cheng055b0312009-06-29 07:51:04 +0000109 SDValue &OffImm);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000110 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Chenge88d5ce2009-07-02 07:28:31 +0000111 SDValue &OffImm);
Chris Lattner52a261b2010-09-21 20:31:19 +0000112 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
Evan Cheng055b0312009-06-29 07:51:04 +0000113 SDValue &OffReg, SDValue &ShImm);
114
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +0000115 inline bool Pred_so_imm(SDNode *inN) const {
116 ConstantSDNode *N = cast<ConstantSDNode>(inN);
117 return ARM_AM::getSOImmVal(N->getZExtValue()) != -1;
118 }
119
120 inline bool Pred_t2_so_imm(SDNode *inN) const {
121 ConstantSDNode *N = cast<ConstantSDNode>(inN);
122 return ARM_AM::getT2SOImmVal(N->getZExtValue()) != -1;
123 }
124
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000125 // Include the pieces autogenerated from the target description.
126#include "ARMGenDAGISel.inc"
Bob Wilson224c2442009-05-19 05:53:42 +0000127
128private:
Evan Chenge88d5ce2009-07-02 07:28:31 +0000129 /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
130 /// ARM.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000131 SDNode *SelectARMIndexedLoad(SDNode *N);
132 SDNode *SelectT2IndexedLoad(SDNode *N);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000133
Bob Wilson621f1952010-03-23 05:25:43 +0000134 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
135 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson3e36f132009-10-14 17:28:52 +0000136 /// loads of D registers and even subregs and odd subregs of Q registers.
Bob Wilson621f1952010-03-23 05:25:43 +0000137 /// For NumVecs <= 2, QOpcodes1 is not used.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000138 SDNode *SelectVLD(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson3e36f132009-10-14 17:28:52 +0000139 unsigned *QOpcodes0, unsigned *QOpcodes1);
140
Bob Wilson24f995d2009-10-14 18:32:29 +0000141 /// SelectVST - Select NEON store intrinsics. NumVecs should
Bob Wilson11d98992010-03-23 06:20:33 +0000142 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson24f995d2009-10-14 18:32:29 +0000143 /// stores of D registers and even subregs and odd subregs of Q registers.
Bob Wilson11d98992010-03-23 06:20:33 +0000144 /// For NumVecs <= 2, QOpcodes1 is not used.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000145 SDNode *SelectVST(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson24f995d2009-10-14 18:32:29 +0000146 unsigned *QOpcodes0, unsigned *QOpcodes1);
147
Bob Wilson96493442009-10-14 16:46:45 +0000148 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
Bob Wilsona7c397c2009-10-14 16:19:03 +0000149 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson8466fa12010-09-13 23:01:35 +0000150 /// load/store of D registers and Q registers.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000151 SDNode *SelectVLDSTLane(SDNode *N, bool IsLoad, unsigned NumVecs,
Bob Wilson8466fa12010-09-13 23:01:35 +0000152 unsigned *DOpcodes, unsigned *QOpcodes);
Bob Wilsona7c397c2009-10-14 16:19:03 +0000153
Bob Wilson78dfbc32010-07-07 00:08:54 +0000154 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2,
155 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be
156 /// generated to force the table registers to be consecutive.
157 SDNode *SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
Bob Wilsond491d6e2010-07-06 23:36:25 +0000158
Sandeep Patel4e1ed882009-10-13 20:25:58 +0000159 /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
Jim Grosbach3a1287b2010-04-22 23:24:18 +0000160 SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
Sandeep Patel47eedaa2009-10-13 18:59:48 +0000161
Evan Cheng07ba9062009-11-19 21:45:22 +0000162 /// SelectCMOVOp - Select CMOV instructions for ARM.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000163 SDNode *SelectCMOVOp(SDNode *N);
164 SDNode *SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000165 ARMCC::CondCodes CCVal, SDValue CCR,
166 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000167 SDNode *SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000168 ARMCC::CondCodes CCVal, SDValue CCR,
169 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000170 SDNode *SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000171 ARMCC::CondCodes CCVal, SDValue CCR,
172 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000173 SDNode *SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000174 ARMCC::CondCodes CCVal, SDValue CCR,
175 SDValue InFlag);
Evan Cheng07ba9062009-11-19 21:45:22 +0000176
Evan Chengde8aa4e2010-05-05 18:28:36 +0000177 SDNode *SelectConcatVector(SDNode *N);
178
Evan Chengaf4550f2009-07-02 01:23:32 +0000179 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
180 /// inline asm expressions.
181 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
182 char ConstraintCode,
183 std::vector<SDValue> &OutOps);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000184
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000185 // Form pairs of consecutive S, D, or Q registers.
186 SDNode *PairSRegs(EVT VT, SDValue V0, SDValue V1);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000187 SDNode *PairDRegs(EVT VT, SDValue V0, SDValue V1);
Evan Cheng603afbf2010-05-10 17:34:18 +0000188 SDNode *PairQRegs(EVT VT, SDValue V0, SDValue V1);
189
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000190 // Form sequences of 4 consecutive S, D, or Q registers.
191 SDNode *QuadSRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Evan Cheng603afbf2010-05-10 17:34:18 +0000192 SDNode *QuadDRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Evan Cheng8f6de382010-05-16 03:27:48 +0000193 SDNode *QuadQRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000194};
Evan Chenga8e29892007-01-19 07:51:42 +0000195}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000196
Sandeep Patel47eedaa2009-10-13 18:59:48 +0000197/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
198/// operand. If so Imm will receive the 32-bit value.
199static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
200 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
201 Imm = cast<ConstantSDNode>(N)->getZExtValue();
202 return true;
203 }
204 return false;
205}
206
207// isInt32Immediate - This method tests to see if a constant operand.
208// If so Imm will receive the 32 bit value.
209static bool isInt32Immediate(SDValue N, unsigned &Imm) {
210 return isInt32Immediate(N.getNode(), Imm);
211}
212
213// isOpcWithIntImmediate - This method tests to see if the node is a specific
214// opcode and that it has a immediate integer right operand.
215// If so Imm will receive the 32 bit value.
216static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
217 return N->getOpcode() == Opc &&
218 isInt32Immediate(N->getOperand(1).getNode(), Imm);
219}
220
221
Chris Lattner52a261b2010-09-21 20:31:19 +0000222bool ARMDAGToDAGISel::SelectShifterOperandReg(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000223 SDValue &BaseReg,
224 SDValue &ShReg,
225 SDValue &Opc) {
Evan Chenga2c519b2010-07-30 23:33:54 +0000226 if (DisableShifterOp)
227 return false;
228
Evan Cheng055b0312009-06-29 07:51:04 +0000229 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
230
231 // Don't match base register only case. That is matched to a separate
232 // lower complexity pattern with explicit register operand.
233 if (ShOpcVal == ARM_AM::no_shift) return false;
Jim Grosbach764ab522009-08-11 15:33:49 +0000234
Evan Cheng055b0312009-06-29 07:51:04 +0000235 BaseReg = N.getOperand(0);
236 unsigned ShImmVal = 0;
237 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 ShReg = CurDAG->getRegister(0, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000239 ShImmVal = RHS->getZExtValue() & 31;
240 } else {
241 ShReg = N.getOperand(1);
242 }
243 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000245 return true;
246}
247
Chris Lattner52a261b2010-09-21 20:31:19 +0000248bool ARMDAGToDAGISel::SelectAddrMode2(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000249 SDValue &Base, SDValue &Offset,
250 SDValue &Opc) {
Evan Chenga13fd102007-03-13 21:05:54 +0000251 if (N.getOpcode() == ISD::MUL) {
252 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
253 // X * [3,5,9] -> X + X * [2,4,8] etc.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000254 int RHSC = (int)RHS->getZExtValue();
Evan Chenga13fd102007-03-13 21:05:54 +0000255 if (RHSC & 1) {
256 RHSC = RHSC & ~1;
257 ARM_AM::AddrOpc AddSub = ARM_AM::add;
258 if (RHSC < 0) {
259 AddSub = ARM_AM::sub;
260 RHSC = - RHSC;
261 }
262 if (isPowerOf2_32(RHSC)) {
263 unsigned ShAmt = Log2_32(RHSC);
264 Base = Offset = N.getOperand(0);
265 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
266 ARM_AM::lsl),
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 MVT::i32);
Evan Chenga13fd102007-03-13 21:05:54 +0000268 return true;
269 }
270 }
271 }
272 }
273
Evan Chenga8e29892007-01-19 07:51:42 +0000274 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
275 Base = N;
276 if (N.getOpcode() == ISD::FrameIndex) {
277 int FI = cast<FrameIndexSDNode>(N)->getIndex();
278 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000279 } else if (N.getOpcode() == ARMISD::Wrapper &&
280 !(Subtarget->useMovt() &&
281 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000282 Base = N.getOperand(0);
283 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000285 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
286 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000288 return true;
289 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000290
Evan Chenga8e29892007-01-19 07:51:42 +0000291 // Match simple R +/- imm12 operands.
292 if (N.getOpcode() == ISD::ADD)
293 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000294 int RHSC = (int)RHS->getZExtValue();
Evan Chenge966d642007-01-24 02:45:25 +0000295 if ((RHSC >= 0 && RHSC < 0x1000) ||
296 (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
Evan Chenga8e29892007-01-19 07:51:42 +0000297 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000298 if (Base.getOpcode() == ISD::FrameIndex) {
299 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
300 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
301 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000303
304 ARM_AM::AddrOpc AddSub = ARM_AM::add;
305 if (RHSC < 0) {
306 AddSub = ARM_AM::sub;
307 RHSC = - RHSC;
308 }
309 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
Evan Chenga8e29892007-01-19 07:51:42 +0000310 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000312 return true;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000313 }
Evan Chenga8e29892007-01-19 07:51:42 +0000314 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000315
Johnny Chen6a3b5ee2009-10-27 17:25:15 +0000316 // Otherwise this is R +/- [possibly shifted] R.
Evan Chenga8e29892007-01-19 07:51:42 +0000317 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
318 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
319 unsigned ShAmt = 0;
Jim Grosbach764ab522009-08-11 15:33:49 +0000320
Evan Chenga8e29892007-01-19 07:51:42 +0000321 Base = N.getOperand(0);
322 Offset = N.getOperand(1);
Jim Grosbach764ab522009-08-11 15:33:49 +0000323
Evan Chenga8e29892007-01-19 07:51:42 +0000324 if (ShOpcVal != ARM_AM::no_shift) {
325 // Check to see if the RHS of the shift is a constant, if not, we can't fold
326 // it.
327 if (ConstantSDNode *Sh =
328 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000329 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000330 Offset = N.getOperand(1).getOperand(0);
331 } else {
332 ShOpcVal = ARM_AM::no_shift;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000333 }
334 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000335
Evan Chenga8e29892007-01-19 07:51:42 +0000336 // Try matching (R shl C) + (R).
337 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
338 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
339 if (ShOpcVal != ARM_AM::no_shift) {
340 // Check to see if the RHS of the shift is a constant, if not, we can't
341 // fold it.
342 if (ConstantSDNode *Sh =
343 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000344 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000345 Offset = N.getOperand(0).getOperand(0);
346 Base = N.getOperand(1);
347 } else {
348 ShOpcVal = ARM_AM::no_shift;
349 }
350 }
351 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000352
Evan Chenga8e29892007-01-19 07:51:42 +0000353 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000355 return true;
356}
357
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000358bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000359 SDValue &Offset, SDValue &Opc) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000360 unsigned Opcode = Op->getOpcode();
Evan Chenga8e29892007-01-19 07:51:42 +0000361 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
362 ? cast<LoadSDNode>(Op)->getAddressingMode()
363 : cast<StoreSDNode>(Op)->getAddressingMode();
364 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
365 ? ARM_AM::add : ARM_AM::sub;
366 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000367 int Val = (int)C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000368 if (Val >= 0 && Val < 0x1000) { // 12 bits.
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000370 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
371 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000373 return true;
374 }
375 }
376
377 Offset = N;
378 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
379 unsigned ShAmt = 0;
380 if (ShOpcVal != ARM_AM::no_shift) {
381 // Check to see if the RHS of the shift is a constant, if not, we can't fold
382 // it.
383 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000384 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000385 Offset = N.getOperand(0);
386 } else {
387 ShOpcVal = ARM_AM::no_shift;
388 }
389 }
390
391 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000392 MVT::i32);
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000393 return true;
394}
395
Evan Chenga8e29892007-01-19 07:51:42 +0000396
Chris Lattner52a261b2010-09-21 20:31:19 +0000397bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000398 SDValue &Base, SDValue &Offset,
399 SDValue &Opc) {
Evan Chenga8e29892007-01-19 07:51:42 +0000400 if (N.getOpcode() == ISD::SUB) {
401 // X - C is canonicalize to X + -C, no need to handle it here.
402 Base = N.getOperand(0);
403 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000405 return true;
406 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000407
Evan Chenga8e29892007-01-19 07:51:42 +0000408 if (N.getOpcode() != ISD::ADD) {
409 Base = N;
410 if (N.getOpcode() == ISD::FrameIndex) {
411 int FI = cast<FrameIndexSDNode>(N)->getIndex();
412 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
413 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 Offset = CurDAG->getRegister(0, MVT::i32);
415 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000416 return true;
417 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000418
Evan Chenga8e29892007-01-19 07:51:42 +0000419 // If the RHS is +/- imm8, fold into addr mode.
420 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000421 int RHSC = (int)RHS->getZExtValue();
Evan Chenge966d642007-01-24 02:45:25 +0000422 if ((RHSC >= 0 && RHSC < 256) ||
423 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000424 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000425 if (Base.getOpcode() == ISD::FrameIndex) {
426 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
427 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
428 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000430
431 ARM_AM::AddrOpc AddSub = ARM_AM::add;
432 if (RHSC < 0) {
433 AddSub = ARM_AM::sub;
434 RHSC = - RHSC;
435 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000437 return true;
438 }
439 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000440
Evan Chenga8e29892007-01-19 07:51:42 +0000441 Base = N.getOperand(0);
442 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000444 return true;
445}
446
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000447bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000448 SDValue &Offset, SDValue &Opc) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000449 unsigned Opcode = Op->getOpcode();
Evan Chenga8e29892007-01-19 07:51:42 +0000450 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
451 ? cast<LoadSDNode>(Op)->getAddressingMode()
452 : cast<StoreSDNode>(Op)->getAddressingMode();
453 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
454 ? ARM_AM::add : ARM_AM::sub;
455 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000456 int Val = (int)C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000457 if (Val >= 0 && Val < 256) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 Offset = CurDAG->getRegister(0, MVT::i32);
459 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000460 return true;
461 }
462 }
463
464 Offset = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000466 return true;
467}
468
Chris Lattner52a261b2010-09-21 20:31:19 +0000469bool ARMDAGToDAGISel::SelectAddrMode4(SDValue N, SDValue &Addr, SDValue &Mode) {
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000470 Addr = N;
Bob Wilsonfd7fd942010-08-28 00:20:11 +0000471 Mode = CurDAG->getTargetConstant(ARM_AM::getAM4ModeImm(ARM_AM::ia), MVT::i32);
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000472 return true;
473}
Evan Chenga8e29892007-01-19 07:51:42 +0000474
Chris Lattner52a261b2010-09-21 20:31:19 +0000475bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000476 SDValue &Base, SDValue &Offset) {
Evan Chenga8e29892007-01-19 07:51:42 +0000477 if (N.getOpcode() != ISD::ADD) {
478 Base = N;
479 if (N.getOpcode() == ISD::FrameIndex) {
480 int FI = cast<FrameIndexSDNode>(N)->getIndex();
481 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000482 } else if (N.getOpcode() == ARMISD::Wrapper &&
483 !(Subtarget->useMovt() &&
484 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000485 Base = N.getOperand(0);
486 }
487 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000489 return true;
490 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000491
Evan Chenga8e29892007-01-19 07:51:42 +0000492 // If the RHS is +/- imm8, fold into addr mode.
493 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000494 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000495 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
496 RHSC >>= 2;
Evan Chenge966d642007-01-24 02:45:25 +0000497 if ((RHSC >= 0 && RHSC < 256) ||
498 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000499 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000500 if (Base.getOpcode() == ISD::FrameIndex) {
501 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
502 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
503 }
504
505 ARM_AM::AddrOpc AddSub = ARM_AM::add;
506 if (RHSC < 0) {
507 AddSub = ARM_AM::sub;
508 RHSC = - RHSC;
509 }
510 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
Owen Anderson825b72b2009-08-11 20:47:22 +0000511 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000512 return true;
513 }
514 }
515 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000516
Evan Chenga8e29892007-01-19 07:51:42 +0000517 Base = N;
518 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000520 return true;
521}
522
Chris Lattner52a261b2010-09-21 20:31:19 +0000523bool ARMDAGToDAGISel::SelectAddrMode6(SDValue N, SDValue &Addr, SDValue &Align){
Bob Wilson8b024a52009-07-01 23:16:05 +0000524 Addr = N;
Jim Grosbach8a5ec862009-11-07 21:25:39 +0000525 // Default to no alignment.
526 Align = CurDAG->getTargetConstant(0, MVT::i32);
Bob Wilson8b024a52009-07-01 23:16:05 +0000527 return true;
528}
529
Chris Lattner52a261b2010-09-21 20:31:19 +0000530bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
Evan Chengbba9f5f2009-08-14 19:01:37 +0000531 SDValue &Offset, SDValue &Label) {
Evan Chenga8e29892007-01-19 07:51:42 +0000532 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
533 Offset = N.getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000534 SDValue N1 = N.getOperand(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000535 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000537 return true;
538 }
539 return false;
540}
541
Chris Lattner52a261b2010-09-21 20:31:19 +0000542bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000543 SDValue &Base, SDValue &Offset){
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000544 // FIXME dl should come from the parent load or store, not the address
Evan Chengc38f2bc2007-01-23 22:59:13 +0000545 if (N.getOpcode() != ISD::ADD) {
Evan Cheng2f297df2009-07-11 07:08:13 +0000546 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
Dan Gohmane368b462010-06-18 14:22:04 +0000547 if (!NC || !NC->isNullValue())
Evan Cheng2f297df2009-07-11 07:08:13 +0000548 return false;
549
550 Base = Offset = N;
Evan Chengc38f2bc2007-01-23 22:59:13 +0000551 return true;
552 }
553
Evan Chenga8e29892007-01-19 07:51:42 +0000554 Base = N.getOperand(0);
555 Offset = N.getOperand(1);
556 return true;
557}
558
Evan Cheng79d43262007-01-24 02:21:22 +0000559bool
Chris Lattner52a261b2010-09-21 20:31:19 +0000560ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000561 unsigned Scale, SDValue &Base,
562 SDValue &OffImm, SDValue &Offset) {
Evan Cheng79d43262007-01-24 02:21:22 +0000563 if (Scale == 4) {
Dan Gohman475871a2008-07-27 21:46:04 +0000564 SDValue TmpBase, TmpOffImm;
Chris Lattner52a261b2010-09-21 20:31:19 +0000565 if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
Evan Cheng79d43262007-01-24 02:21:22 +0000566 return false; // We want to select tLDRspi / tSTRspi instead.
Evan Cheng012f2d92007-01-24 08:53:17 +0000567 if (N.getOpcode() == ARMISD::Wrapper &&
568 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
569 return false; // We want to select tLDRpci instead.
Evan Cheng79d43262007-01-24 02:21:22 +0000570 }
571
Evan Chenga8e29892007-01-19 07:51:42 +0000572 if (N.getOpcode() != ISD::ADD) {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000573 if (N.getOpcode() == ARMISD::Wrapper &&
574 !(Subtarget->useMovt() &&
575 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
576 Base = N.getOperand(0);
577 } else
578 Base = N;
579
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 Offset = CurDAG->getRegister(0, MVT::i32);
581 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000582 return true;
583 }
584
Evan Chengad0e4652007-02-06 00:22:06 +0000585 // Thumb does not have [sp, r] address mode.
586 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
587 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
588 if ((LHSR && LHSR->getReg() == ARM::SP) ||
589 (RHSR && RHSR->getReg() == ARM::SP)) {
590 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 Offset = CurDAG->getRegister(0, MVT::i32);
592 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengad0e4652007-02-06 00:22:06 +0000593 return true;
594 }
595
Evan Chenga8e29892007-01-19 07:51:42 +0000596 // If the RHS is + imm5 * scale, fold into addr mode.
597 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000598 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000599 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
600 RHSC /= Scale;
601 if (RHSC >= 0 && RHSC < 32) {
602 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 Offset = CurDAG->getRegister(0, MVT::i32);
604 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000605 return true;
606 }
607 }
608 }
609
Evan Chengc38f2bc2007-01-23 22:59:13 +0000610 Base = N.getOperand(0);
611 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000613 return true;
Evan Chenga8e29892007-01-19 07:51:42 +0000614}
615
Chris Lattner52a261b2010-09-21 20:31:19 +0000616bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000617 SDValue &Base, SDValue &OffImm,
618 SDValue &Offset) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000619 return SelectThumbAddrModeRI5(N, 1, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000620}
621
Chris Lattner52a261b2010-09-21 20:31:19 +0000622bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000623 SDValue &Base, SDValue &OffImm,
624 SDValue &Offset) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000625 return SelectThumbAddrModeRI5(N, 2, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000626}
627
Chris Lattner52a261b2010-09-21 20:31:19 +0000628bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000629 SDValue &Base, SDValue &OffImm,
630 SDValue &Offset) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000631 return SelectThumbAddrModeRI5(N, 4, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000632}
633
Chris Lattner52a261b2010-09-21 20:31:19 +0000634bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
635 SDValue &Base, SDValue &OffImm) {
Evan Chenga8e29892007-01-19 07:51:42 +0000636 if (N.getOpcode() == ISD::FrameIndex) {
637 int FI = cast<FrameIndexSDNode>(N)->getIndex();
638 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000640 return true;
641 }
Evan Cheng79d43262007-01-24 02:21:22 +0000642
Evan Chengad0e4652007-02-06 00:22:06 +0000643 if (N.getOpcode() != ISD::ADD)
644 return false;
645
646 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
Evan Cheng8c1a73a2007-02-06 09:11:20 +0000647 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
648 (LHSR && LHSR->getReg() == ARM::SP)) {
Evan Cheng79d43262007-01-24 02:21:22 +0000649 // If the RHS is + imm8 * scale, fold into addr mode.
650 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000651 int RHSC = (int)RHS->getZExtValue();
Evan Cheng79d43262007-01-24 02:21:22 +0000652 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
653 RHSC >>= 2;
654 if (RHSC >= 0 && RHSC < 256) {
Evan Chengad0e4652007-02-06 00:22:06 +0000655 Base = N.getOperand(0);
Evan Cheng8c1a73a2007-02-06 09:11:20 +0000656 if (Base.getOpcode() == ISD::FrameIndex) {
657 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
658 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
659 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000660 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Cheng79d43262007-01-24 02:21:22 +0000661 return true;
662 }
663 }
664 }
665 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000666
Evan Chenga8e29892007-01-19 07:51:42 +0000667 return false;
668}
669
Chris Lattner52a261b2010-09-21 20:31:19 +0000670bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDValue N, SDValue &BaseReg,
Evan Cheng9cb9e672009-06-27 02:26:13 +0000671 SDValue &Opc) {
Evan Chenga2c519b2010-07-30 23:33:54 +0000672 if (DisableShifterOp)
673 return false;
674
Evan Cheng9cb9e672009-06-27 02:26:13 +0000675 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
676
677 // Don't match base register only case. That is matched to a separate
678 // lower complexity pattern with explicit register operand.
679 if (ShOpcVal == ARM_AM::no_shift) return false;
680
681 BaseReg = N.getOperand(0);
682 unsigned ShImmVal = 0;
683 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
684 ShImmVal = RHS->getZExtValue() & 31;
685 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
686 return true;
687 }
688
689 return false;
690}
691
Chris Lattner52a261b2010-09-21 20:31:19 +0000692bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000693 SDValue &Base, SDValue &OffImm) {
694 // Match simple R + imm12 operands.
David Goodwin31e7eba2009-07-20 15:55:39 +0000695
Evan Cheng3a214252009-08-11 08:52:18 +0000696 // Base only.
697 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
David Goodwin31e7eba2009-07-20 15:55:39 +0000698 if (N.getOpcode() == ISD::FrameIndex) {
Evan Cheng3a214252009-08-11 08:52:18 +0000699 // Match frame index...
David Goodwin31e7eba2009-07-20 15:55:39 +0000700 int FI = cast<FrameIndexSDNode>(N)->getIndex();
701 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
David Goodwin31e7eba2009-07-20 15:55:39 +0000703 return true;
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000704 } else if (N.getOpcode() == ARMISD::Wrapper &&
705 !(Subtarget->useMovt() &&
706 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Cheng3a214252009-08-11 08:52:18 +0000707 Base = N.getOperand(0);
708 if (Base.getOpcode() == ISD::TargetConstantPool)
709 return false; // We want to select t2LDRpci instead.
710 } else
711 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000712 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng3a214252009-08-11 08:52:18 +0000713 return true;
David Goodwin31e7eba2009-07-20 15:55:39 +0000714 }
Evan Cheng055b0312009-06-29 07:51:04 +0000715
716 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000717 if (SelectT2AddrModeImm8(N, Base, OffImm))
Evan Cheng3a214252009-08-11 08:52:18 +0000718 // Let t2LDRi8 handle (R - imm8).
719 return false;
720
Evan Cheng055b0312009-06-29 07:51:04 +0000721 int RHSC = (int)RHS->getZExtValue();
David Goodwind8c95b52009-07-30 18:56:48 +0000722 if (N.getOpcode() == ISD::SUB)
723 RHSC = -RHSC;
724
725 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
Evan Cheng055b0312009-06-29 07:51:04 +0000726 Base = N.getOperand(0);
David Goodwind8c95b52009-07-30 18:56:48 +0000727 if (Base.getOpcode() == ISD::FrameIndex) {
728 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
729 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
730 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000732 return true;
733 }
734 }
735
Evan Cheng3a214252009-08-11 08:52:18 +0000736 // Base only.
737 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000738 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng3a214252009-08-11 08:52:18 +0000739 return true;
Evan Cheng055b0312009-06-29 07:51:04 +0000740}
741
Chris Lattner52a261b2010-09-21 20:31:19 +0000742bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000743 SDValue &Base, SDValue &OffImm) {
David Goodwind8c95b52009-07-30 18:56:48 +0000744 // Match simple R - imm8 operands.
Evan Cheng3a214252009-08-11 08:52:18 +0000745 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::SUB) {
David Goodwin07337c02009-07-30 22:45:52 +0000746 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
747 int RHSC = (int)RHS->getSExtValue();
748 if (N.getOpcode() == ISD::SUB)
749 RHSC = -RHSC;
Jim Grosbach764ab522009-08-11 15:33:49 +0000750
Evan Cheng3a214252009-08-11 08:52:18 +0000751 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
752 Base = N.getOperand(0);
David Goodwin07337c02009-07-30 22:45:52 +0000753 if (Base.getOpcode() == ISD::FrameIndex) {
754 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
755 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
756 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
David Goodwin07337c02009-07-30 22:45:52 +0000758 return true;
Evan Cheng055b0312009-06-29 07:51:04 +0000759 }
Evan Cheng055b0312009-06-29 07:51:04 +0000760 }
761 }
762
763 return false;
764}
765
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000766bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Chenge88d5ce2009-07-02 07:28:31 +0000767 SDValue &OffImm){
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000768 unsigned Opcode = Op->getOpcode();
Evan Chenge88d5ce2009-07-02 07:28:31 +0000769 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
770 ? cast<LoadSDNode>(Op)->getAddressingMode()
771 : cast<StoreSDNode>(Op)->getAddressingMode();
772 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N)) {
773 int RHSC = (int)RHS->getZExtValue();
774 if (RHSC >= 0 && RHSC < 0x100) { // 8 bits.
David Goodwin4cb73522009-07-14 21:29:29 +0000775 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
Owen Anderson825b72b2009-08-11 20:47:22 +0000776 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
777 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000778 return true;
779 }
780 }
781
782 return false;
783}
784
Chris Lattner52a261b2010-09-21 20:31:19 +0000785bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000786 SDValue &Base,
787 SDValue &OffReg, SDValue &ShImm) {
Evan Cheng3a214252009-08-11 08:52:18 +0000788 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
789 if (N.getOpcode() != ISD::ADD)
790 return false;
Evan Cheng055b0312009-06-29 07:51:04 +0000791
Evan Cheng3a214252009-08-11 08:52:18 +0000792 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
793 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
794 int RHSC = (int)RHS->getZExtValue();
795 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
796 return false;
797 else if (RHSC < 0 && RHSC >= -255) // 8 bits
David Goodwind8c95b52009-07-30 18:56:48 +0000798 return false;
799 }
800
Evan Cheng055b0312009-06-29 07:51:04 +0000801 // Look for (R + R) or (R + (R << [1,2,3])).
802 unsigned ShAmt = 0;
803 Base = N.getOperand(0);
804 OffReg = N.getOperand(1);
805
806 // Swap if it is ((R << c) + R).
807 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
808 if (ShOpcVal != ARM_AM::lsl) {
809 ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
810 if (ShOpcVal == ARM_AM::lsl)
811 std::swap(Base, OffReg);
Jim Grosbach764ab522009-08-11 15:33:49 +0000812 }
813
Evan Cheng055b0312009-06-29 07:51:04 +0000814 if (ShOpcVal == ARM_AM::lsl) {
815 // Check to see if the RHS of the shift is a constant, if not, we can't fold
816 // it.
817 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
818 ShAmt = Sh->getZExtValue();
819 if (ShAmt >= 4) {
820 ShAmt = 0;
821 ShOpcVal = ARM_AM::no_shift;
822 } else
823 OffReg = OffReg.getOperand(0);
824 } else {
825 ShOpcVal = ARM_AM::no_shift;
826 }
David Goodwin7ecc8502009-07-15 15:50:19 +0000827 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000828
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000830
831 return true;
832}
833
834//===--------------------------------------------------------------------===//
835
Evan Chengee568cf2007-07-05 07:15:27 +0000836/// getAL - Returns a ARMCC::AL immediate node.
Dan Gohman475871a2008-07-27 21:46:04 +0000837static inline SDValue getAL(SelectionDAG *CurDAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
Evan Cheng44bec522007-05-15 01:29:07 +0000839}
840
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000841SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
842 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengaf4550f2009-07-02 01:23:32 +0000843 ISD::MemIndexedMode AM = LD->getAddressingMode();
844 if (AM == ISD::UNINDEXED)
845 return NULL;
846
Owen Andersone50ed302009-08-10 22:56:29 +0000847 EVT LoadedVT = LD->getMemoryVT();
Evan Chengaf4550f2009-07-02 01:23:32 +0000848 SDValue Offset, AMOpc;
849 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
850 unsigned Opcode = 0;
851 bool Match = false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 if (LoadedVT == MVT::i32 &&
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000853 SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000854 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
855 Match = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 } else if (LoadedVT == MVT::i16 &&
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000857 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000858 Match = true;
859 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
860 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
861 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000863 if (LD->getExtensionType() == ISD::SEXTLOAD) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000864 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000865 Match = true;
866 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
867 }
868 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000869 if (SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000870 Match = true;
871 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
872 }
873 }
874 }
875
876 if (Match) {
877 SDValue Chain = LD->getChain();
878 SDValue Base = LD->getBasePtr();
879 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 CurDAG->getRegister(0, MVT::i32), Chain };
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000881 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +0000882 MVT::Other, Ops, 6);
Evan Chengaf4550f2009-07-02 01:23:32 +0000883 }
884
885 return NULL;
886}
887
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000888SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
889 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000890 ISD::MemIndexedMode AM = LD->getAddressingMode();
891 if (AM == ISD::UNINDEXED)
892 return NULL;
893
Owen Andersone50ed302009-08-10 22:56:29 +0000894 EVT LoadedVT = LD->getMemoryVT();
Evan Cheng4fbb9962009-07-02 23:16:11 +0000895 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000896 SDValue Offset;
897 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
898 unsigned Opcode = 0;
899 bool Match = false;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000900 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 switch (LoadedVT.getSimpleVT().SimpleTy) {
902 case MVT::i32:
Evan Chenge88d5ce2009-07-02 07:28:31 +0000903 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
904 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 case MVT::i16:
Evan Cheng4fbb9962009-07-02 23:16:11 +0000906 if (isSExtLd)
907 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
908 else
909 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000910 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000911 case MVT::i8:
912 case MVT::i1:
Evan Cheng4fbb9962009-07-02 23:16:11 +0000913 if (isSExtLd)
914 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
915 else
916 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000917 break;
918 default:
919 return NULL;
920 }
921 Match = true;
922 }
923
924 if (Match) {
925 SDValue Chain = LD->getChain();
926 SDValue Base = LD->getBasePtr();
927 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 CurDAG->getRegister(0, MVT::i32), Chain };
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000929 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +0000930 MVT::Other, Ops, 5);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000931 }
932
933 return NULL;
934}
935
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000936/// PairSRegs - Form a D register from a pair of S registers.
937///
938SDNode *ARMDAGToDAGISel::PairSRegs(EVT VT, SDValue V0, SDValue V1) {
939 DebugLoc dl = V0.getNode()->getDebugLoc();
940 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
941 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
Bob Wilson07f6e802010-06-16 21:34:01 +0000942 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
943 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000944}
945
Evan Cheng603afbf2010-05-10 17:34:18 +0000946/// PairDRegs - Form a quad register from a pair of D registers.
947///
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000948SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
949 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000950 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
951 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Bob Wilson07f6e802010-06-16 21:34:01 +0000952 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
953 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000954}
955
Evan Cheng7f687192010-05-14 00:21:45 +0000956/// PairQRegs - Form 4 consecutive D registers from a pair of Q registers.
Evan Cheng603afbf2010-05-10 17:34:18 +0000957///
958SDNode *ARMDAGToDAGISel::PairQRegs(EVT VT, SDValue V0, SDValue V1) {
959 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000960 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
961 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
Evan Cheng603afbf2010-05-10 17:34:18 +0000962 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
963 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
964}
965
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000966/// QuadSRegs - Form 4 consecutive S registers.
967///
968SDNode *ARMDAGToDAGISel::QuadSRegs(EVT VT, SDValue V0, SDValue V1,
969 SDValue V2, SDValue V3) {
970 DebugLoc dl = V0.getNode()->getDebugLoc();
971 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
972 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
973 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, MVT::i32);
974 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, MVT::i32);
975 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
976 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
977}
978
Evan Cheng7f687192010-05-14 00:21:45 +0000979/// QuadDRegs - Form 4 consecutive D registers.
Evan Cheng603afbf2010-05-10 17:34:18 +0000980///
981SDNode *ARMDAGToDAGISel::QuadDRegs(EVT VT, SDValue V0, SDValue V1,
982 SDValue V2, SDValue V3) {
983 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000984 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
985 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
986 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
987 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
Evan Cheng603afbf2010-05-10 17:34:18 +0000988 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
989 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
990}
991
Evan Cheng8f6de382010-05-16 03:27:48 +0000992/// QuadQRegs - Form 4 consecutive Q registers.
993///
994SDNode *ARMDAGToDAGISel::QuadQRegs(EVT VT, SDValue V0, SDValue V1,
995 SDValue V2, SDValue V3) {
996 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000997 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
998 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
999 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, MVT::i32);
1000 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, MVT::i32);
Evan Cheng8f6de382010-05-16 03:27:48 +00001001 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1002 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1003}
1004
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001005SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
Bob Wilson3e36f132009-10-14 17:28:52 +00001006 unsigned *DOpcodes, unsigned *QOpcodes0,
1007 unsigned *QOpcodes1) {
Bob Wilson621f1952010-03-23 05:25:43 +00001008 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
Bob Wilson3e36f132009-10-14 17:28:52 +00001009 DebugLoc dl = N->getDebugLoc();
1010
Bob Wilson226036e2010-03-20 22:13:40 +00001011 SDValue MemAddr, Align;
Chris Lattner52a261b2010-09-21 20:31:19 +00001012 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
Bob Wilson3e36f132009-10-14 17:28:52 +00001013 return NULL;
1014
1015 SDValue Chain = N->getOperand(0);
1016 EVT VT = N->getValueType(0);
1017 bool is64BitVector = VT.is64BitVector();
1018
1019 unsigned OpcodeIndex;
1020 switch (VT.getSimpleVT().SimpleTy) {
1021 default: llvm_unreachable("unhandled vld type");
1022 // Double-register operations:
1023 case MVT::v8i8: OpcodeIndex = 0; break;
1024 case MVT::v4i16: OpcodeIndex = 1; break;
1025 case MVT::v2f32:
1026 case MVT::v2i32: OpcodeIndex = 2; break;
1027 case MVT::v1i64: OpcodeIndex = 3; break;
1028 // Quad-register operations:
1029 case MVT::v16i8: OpcodeIndex = 0; break;
1030 case MVT::v8i16: OpcodeIndex = 1; break;
1031 case MVT::v4f32:
1032 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson621f1952010-03-23 05:25:43 +00001033 case MVT::v2i64: OpcodeIndex = 3;
Bob Wilson11d98992010-03-23 06:20:33 +00001034 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
Bob Wilson621f1952010-03-23 05:25:43 +00001035 break;
Bob Wilson3e36f132009-10-14 17:28:52 +00001036 }
1037
Bob Wilsonf5721912010-09-03 18:16:02 +00001038 EVT ResTy;
1039 if (NumVecs == 1)
1040 ResTy = VT;
1041 else {
1042 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1043 if (!is64BitVector)
1044 ResTyElts *= 2;
1045 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1046 }
1047
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001048 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001049 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilsonf5721912010-09-03 18:16:02 +00001050 SDValue SuperReg;
Bob Wilson3e36f132009-10-14 17:28:52 +00001051 if (is64BitVector) {
1052 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001053 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilsonf5721912010-09-03 18:16:02 +00001054 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
Bob Wilsonffde0802010-09-02 16:00:54 +00001055 if (NumVecs == 1)
Evan Chenge9e2ba02010-05-10 21:26:24 +00001056 return VLd;
1057
Bob Wilsonf5721912010-09-03 18:16:02 +00001058 SuperReg = SDValue(VLd, 0);
Jakob Stoklund Olesen7bb31e32010-05-24 17:13:28 +00001059 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
Evan Cheng5c6aba22010-05-14 18:54:59 +00001060 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001061 SDValue D = CurDAG->getTargetExtractSubreg(ARM::dsub_0+Vec,
Bob Wilsonffde0802010-09-02 16:00:54 +00001062 dl, VT, SuperReg);
Evan Cheng5c6aba22010-05-14 18:54:59 +00001063 ReplaceUses(SDValue(N, Vec), D);
Evan Chenge9e2ba02010-05-10 21:26:24 +00001064 }
Bob Wilsonf5721912010-09-03 18:16:02 +00001065 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
Evan Chenge9e2ba02010-05-10 21:26:24 +00001066 return NULL;
Bob Wilson3e36f132009-10-14 17:28:52 +00001067 }
1068
Bob Wilson621f1952010-03-23 05:25:43 +00001069 if (NumVecs <= 2) {
1070 // Quad registers are directly supported for VLD1 and VLD2,
1071 // loading pairs of D regs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001072 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001073 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilsonffde0802010-09-02 16:00:54 +00001074 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
Bob Wilsonffde0802010-09-02 16:00:54 +00001075 if (NumVecs == 1)
1076 return VLd;
1077
Bob Wilsonf5721912010-09-03 18:16:02 +00001078 SuperReg = SDValue(VLd, 0);
Bob Wilsonffde0802010-09-02 16:00:54 +00001079 Chain = SDValue(VLd, 1);
1080
Bob Wilson3e36f132009-10-14 17:28:52 +00001081 } else {
1082 // Otherwise, quad registers are loaded with two separate instructions,
1083 // where one loads the even registers and the other loads the odd registers.
Bob Wilsonf5721912010-09-03 18:16:02 +00001084 EVT AddrTy = MemAddr.getValueType();
Bob Wilson3e36f132009-10-14 17:28:52 +00001085
Bob Wilson24f995d2009-10-14 18:32:29 +00001086 // Load the even subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001087 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilsonf5721912010-09-03 18:16:02 +00001088 SDValue ImplDef =
1089 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1090 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1091 SDNode *VLdA =
1092 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsA, 7);
1093 Chain = SDValue(VLdA, 2);
Bob Wilson3e36f132009-10-14 17:28:52 +00001094
Bob Wilson24f995d2009-10-14 18:32:29 +00001095 // Load the odd subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001096 Opc = QOpcodes1[OpcodeIndex];
Bob Wilsonf5721912010-09-03 18:16:02 +00001097 const SDValue OpsB[] = { SDValue(VLdA, 1), Align, Reg0, SDValue(VLdA, 0),
1098 Pred, Reg0, Chain };
1099 SDNode *VLdB =
1100 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsB, 7);
1101 SuperReg = SDValue(VLdB, 0);
1102 Chain = SDValue(VLdB, 2);
1103 }
Bob Wilson3e36f132009-10-14 17:28:52 +00001104
Bob Wilsonf5721912010-09-03 18:16:02 +00001105 // Extract out the Q registers.
1106 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1107 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1108 SDValue Q = CurDAG->getTargetExtractSubreg(ARM::qsub_0+Vec,
1109 dl, VT, SuperReg);
1110 ReplaceUses(SDValue(N, Vec), Q);
Bob Wilson3e36f132009-10-14 17:28:52 +00001111 }
1112 ReplaceUses(SDValue(N, NumVecs), Chain);
1113 return NULL;
1114}
1115
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001116SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
Bob Wilson24f995d2009-10-14 18:32:29 +00001117 unsigned *DOpcodes, unsigned *QOpcodes0,
1118 unsigned *QOpcodes1) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001119 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
Bob Wilson24f995d2009-10-14 18:32:29 +00001120 DebugLoc dl = N->getDebugLoc();
1121
Bob Wilson226036e2010-03-20 22:13:40 +00001122 SDValue MemAddr, Align;
Chris Lattner52a261b2010-09-21 20:31:19 +00001123 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
Bob Wilson24f995d2009-10-14 18:32:29 +00001124 return NULL;
1125
1126 SDValue Chain = N->getOperand(0);
1127 EVT VT = N->getOperand(3).getValueType();
1128 bool is64BitVector = VT.is64BitVector();
1129
1130 unsigned OpcodeIndex;
1131 switch (VT.getSimpleVT().SimpleTy) {
1132 default: llvm_unreachable("unhandled vst type");
1133 // Double-register operations:
1134 case MVT::v8i8: OpcodeIndex = 0; break;
1135 case MVT::v4i16: OpcodeIndex = 1; break;
1136 case MVT::v2f32:
1137 case MVT::v2i32: OpcodeIndex = 2; break;
1138 case MVT::v1i64: OpcodeIndex = 3; break;
1139 // Quad-register operations:
1140 case MVT::v16i8: OpcodeIndex = 0; break;
1141 case MVT::v8i16: OpcodeIndex = 1; break;
1142 case MVT::v4f32:
1143 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson11d98992010-03-23 06:20:33 +00001144 case MVT::v2i64: OpcodeIndex = 3;
1145 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1146 break;
Bob Wilson24f995d2009-10-14 18:32:29 +00001147 }
1148
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001149 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001150 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001151
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001152 SmallVector<SDValue, 7> Ops;
Bob Wilson24f995d2009-10-14 18:32:29 +00001153 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001154 Ops.push_back(Align);
Bob Wilson24f995d2009-10-14 18:32:29 +00001155
1156 if (is64BitVector) {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001157 if (NumVecs == 1) {
1158 Ops.push_back(N->getOperand(3));
1159 } else {
Evan Cheng0ce537a2010-05-11 01:19:40 +00001160 SDValue RegSeq;
1161 SDValue V0 = N->getOperand(0+3);
1162 SDValue V1 = N->getOperand(1+3);
1163
1164 // Form a REG_SEQUENCE to force register allocation.
1165 if (NumVecs == 2)
1166 RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1167 else {
1168 SDValue V2 = N->getOperand(2+3);
1169 // If it's a vld3, form a quad D-register and leave the last part as
1170 // an undef.
1171 SDValue V3 = (NumVecs == 3)
1172 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1173 : N->getOperand(3+3);
1174 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1175 }
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001176 Ops.push_back(RegSeq);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001177 }
Evan Chengac0869d2009-11-21 06:21:52 +00001178 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001179 Ops.push_back(Reg0); // predicate register
Bob Wilson24f995d2009-10-14 18:32:29 +00001180 Ops.push_back(Chain);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001181 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001182 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
Bob Wilson24f995d2009-10-14 18:32:29 +00001183 }
1184
Bob Wilson11d98992010-03-23 06:20:33 +00001185 if (NumVecs <= 2) {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001186 // Quad registers are directly supported for VST1 and VST2.
Bob Wilson24f995d2009-10-14 18:32:29 +00001187 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001188 if (NumVecs == 1) {
1189 Ops.push_back(N->getOperand(3));
1190 } else {
1191 // Form a QQ register.
Evan Cheng603afbf2010-05-10 17:34:18 +00001192 SDValue Q0 = N->getOperand(3);
1193 SDValue Q1 = N->getOperand(4);
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001194 Ops.push_back(SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0));
Bob Wilson24f995d2009-10-14 18:32:29 +00001195 }
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001196 Ops.push_back(Pred);
1197 Ops.push_back(Reg0); // predicate register
1198 Ops.push_back(Chain);
1199 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
Bob Wilson24f995d2009-10-14 18:32:29 +00001200 }
1201
1202 // Otherwise, quad registers are stored with two separate instructions,
1203 // where one stores the even registers and the other stores the odd registers.
Evan Cheng7189fd02010-05-15 07:53:37 +00001204
Bob Wilson07f6e802010-06-16 21:34:01 +00001205 // Form the QQQQ REG_SEQUENCE.
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001206 SDValue V0 = N->getOperand(0+3);
1207 SDValue V1 = N->getOperand(1+3);
1208 SDValue V2 = N->getOperand(2+3);
1209 SDValue V3 = (NumVecs == 3)
1210 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1211 : N->getOperand(3+3);
1212 SDValue RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilson07f6e802010-06-16 21:34:01 +00001213
1214 // Store the even D registers.
Bob Wilson07f6e802010-06-16 21:34:01 +00001215 Ops.push_back(Reg0); // post-access address offset
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001216 Ops.push_back(RegSeq);
Bob Wilson07f6e802010-06-16 21:34:01 +00001217 Ops.push_back(Pred);
1218 Ops.push_back(Reg0); // predicate register
1219 Ops.push_back(Chain);
1220 unsigned Opc = QOpcodes0[OpcodeIndex];
1221 SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001222 MVT::Other, Ops.data(), 7);
Bob Wilson07f6e802010-06-16 21:34:01 +00001223 Chain = SDValue(VStA, 1);
1224
1225 // Store the odd D registers.
1226 Ops[0] = SDValue(VStA, 0); // MemAddr
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001227 Ops[6] = Chain;
Bob Wilson07f6e802010-06-16 21:34:01 +00001228 Opc = QOpcodes1[OpcodeIndex];
1229 SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001230 MVT::Other, Ops.data(), 7);
Bob Wilson07f6e802010-06-16 21:34:01 +00001231 Chain = SDValue(VStB, 1);
1232 ReplaceUses(SDValue(N, 0), Chain);
1233 return NULL;
Bob Wilson24f995d2009-10-14 18:32:29 +00001234}
1235
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001236SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
Bob Wilson96493442009-10-14 16:46:45 +00001237 unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson8466fa12010-09-13 23:01:35 +00001238 unsigned *QOpcodes) {
Bob Wilson96493442009-10-14 16:46:45 +00001239 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001240 DebugLoc dl = N->getDebugLoc();
1241
Bob Wilson226036e2010-03-20 22:13:40 +00001242 SDValue MemAddr, Align;
Chris Lattner52a261b2010-09-21 20:31:19 +00001243 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
Bob Wilsona7c397c2009-10-14 16:19:03 +00001244 return NULL;
1245
1246 SDValue Chain = N->getOperand(0);
1247 unsigned Lane =
1248 cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
Bob Wilson96493442009-10-14 16:46:45 +00001249 EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
Bob Wilsona7c397c2009-10-14 16:19:03 +00001250 bool is64BitVector = VT.is64BitVector();
1251
Bob Wilsona7c397c2009-10-14 16:19:03 +00001252 unsigned OpcodeIndex;
1253 switch (VT.getSimpleVT().SimpleTy) {
Bob Wilson96493442009-10-14 16:46:45 +00001254 default: llvm_unreachable("unhandled vld/vst lane type");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001255 // Double-register operations:
1256 case MVT::v8i8: OpcodeIndex = 0; break;
1257 case MVT::v4i16: OpcodeIndex = 1; break;
1258 case MVT::v2f32:
1259 case MVT::v2i32: OpcodeIndex = 2; break;
1260 // Quad-register operations:
1261 case MVT::v8i16: OpcodeIndex = 0; break;
1262 case MVT::v4f32:
1263 case MVT::v4i32: OpcodeIndex = 1; break;
1264 }
1265
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001266 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001267 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001268
Bob Wilson8466fa12010-09-13 23:01:35 +00001269 SmallVector<SDValue, 7> Ops;
Bob Wilsona7c397c2009-10-14 16:19:03 +00001270 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001271 Ops.push_back(Align);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001272
Eric Christopher23da0b22010-09-14 08:31:25 +00001273 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1274 QOpcodes[OpcodeIndex]);
Bob Wilson07f6e802010-06-16 21:34:01 +00001275
Bob Wilson8466fa12010-09-13 23:01:35 +00001276 SDValue SuperReg;
1277 SDValue V0 = N->getOperand(0+3);
1278 SDValue V1 = N->getOperand(1+3);
1279 if (NumVecs == 2) {
1280 if (is64BitVector)
1281 SuperReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1282 else
1283 SuperReg = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001284 } else {
Bob Wilson8466fa12010-09-13 23:01:35 +00001285 SDValue V2 = N->getOperand(2+3);
1286 SDValue V3 = (NumVecs == 3)
1287 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1288 : N->getOperand(3+3);
1289 if (is64BitVector)
1290 SuperReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1291 else
1292 SuperReg = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001293 }
Bob Wilson8466fa12010-09-13 23:01:35 +00001294 Ops.push_back(SuperReg);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001295 Ops.push_back(getI32Imm(Lane));
Evan Chengac0869d2009-11-21 06:21:52 +00001296 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001297 Ops.push_back(Reg0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001298 Ops.push_back(Chain);
1299
Bob Wilson96493442009-10-14 16:46:45 +00001300 if (!IsLoad)
Bob Wilson8466fa12010-09-13 23:01:35 +00001301 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 7);
Bob Wilson96493442009-10-14 16:46:45 +00001302
Bob Wilson8466fa12010-09-13 23:01:35 +00001303 EVT ResTy;
1304 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1305 if (!is64BitVector)
1306 ResTyElts *= 2;
1307 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001308
Bob Wilson8466fa12010-09-13 23:01:35 +00001309 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other,
1310 Ops.data(), 7);
1311 SuperReg = SDValue(VLdLn, 0);
1312 Chain = SDValue(VLdLn, 1);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001313
Bob Wilson8466fa12010-09-13 23:01:35 +00001314 // Extract the subregisters.
Bob Wilson07f6e802010-06-16 21:34:01 +00001315 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1316 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1317 unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1318 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1319 ReplaceUses(SDValue(N, Vec),
Bob Wilson8466fa12010-09-13 23:01:35 +00001320 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1321 ReplaceUses(SDValue(N, NumVecs), Chain);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001322 return NULL;
1323}
1324
Bob Wilson78dfbc32010-07-07 00:08:54 +00001325SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1326 unsigned Opc) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001327 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1328 DebugLoc dl = N->getDebugLoc();
1329 EVT VT = N->getValueType(0);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001330 unsigned FirstTblReg = IsExt ? 2 : 1;
Bob Wilsond491d6e2010-07-06 23:36:25 +00001331
1332 // Form a REG_SEQUENCE to force register allocation.
1333 SDValue RegSeq;
Bob Wilson78dfbc32010-07-07 00:08:54 +00001334 SDValue V0 = N->getOperand(FirstTblReg + 0);
1335 SDValue V1 = N->getOperand(FirstTblReg + 1);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001336 if (NumVecs == 2)
1337 RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1338 else {
Bob Wilson78dfbc32010-07-07 00:08:54 +00001339 SDValue V2 = N->getOperand(FirstTblReg + 2);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001340 // If it's a vtbl3, form a quad D-register and leave the last part as
1341 // an undef.
1342 SDValue V3 = (NumVecs == 3)
1343 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
Bob Wilson78dfbc32010-07-07 00:08:54 +00001344 : N->getOperand(FirstTblReg + 3);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001345 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1346 }
1347
Bob Wilson78dfbc32010-07-07 00:08:54 +00001348 SmallVector<SDValue, 6> Ops;
1349 if (IsExt)
1350 Ops.push_back(N->getOperand(1));
Bob Wilsonbd916c52010-09-13 23:55:10 +00001351 Ops.push_back(RegSeq);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001352 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
Bob Wilsond491d6e2010-07-06 23:36:25 +00001353 Ops.push_back(getAL(CurDAG)); // predicate
1354 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
Bob Wilson78dfbc32010-07-07 00:08:54 +00001355 return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
Bob Wilsond491d6e2010-07-06 23:36:25 +00001356}
1357
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001358SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001359 bool isSigned) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001360 if (!Subtarget->hasV6T2Ops())
1361 return NULL;
Bob Wilson96493442009-10-14 16:46:45 +00001362
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001363 unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1364 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1365
1366
1367 // For unsigned extracts, check for a shift right and mask
1368 unsigned And_imm = 0;
1369 if (N->getOpcode() == ISD::AND) {
1370 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1371
1372 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1373 if (And_imm & (And_imm + 1))
1374 return NULL;
1375
1376 unsigned Srl_imm = 0;
1377 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1378 Srl_imm)) {
1379 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1380
1381 unsigned Width = CountTrailingOnes_32(And_imm);
1382 unsigned LSB = Srl_imm;
1383 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1384 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1385 CurDAG->getTargetConstant(LSB, MVT::i32),
1386 CurDAG->getTargetConstant(Width, MVT::i32),
1387 getAL(CurDAG), Reg0 };
1388 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1389 }
1390 }
1391 return NULL;
1392 }
1393
1394 // Otherwise, we're looking for a shift of a shift
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001395 unsigned Shl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001396 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001397 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1398 unsigned Srl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001399 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001400 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1401 unsigned Width = 32 - Srl_imm;
1402 int LSB = Srl_imm - Shl_imm;
Evan Cheng8000c6c2009-10-22 00:40:00 +00001403 if (LSB < 0)
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001404 return NULL;
1405 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001406 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001407 CurDAG->getTargetConstant(LSB, MVT::i32),
1408 CurDAG->getTargetConstant(Width, MVT::i32),
1409 getAL(CurDAG), Reg0 };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001410 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001411 }
1412 }
1413 return NULL;
1414}
1415
Evan Cheng9ef48352009-11-20 00:54:03 +00001416SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001417SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001418 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1419 SDValue CPTmp0;
1420 SDValue CPTmp1;
Chris Lattner52a261b2010-09-21 20:31:19 +00001421 if (SelectT2ShifterOperandReg(TrueVal, CPTmp0, CPTmp1)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001422 unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1423 unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1424 unsigned Opc = 0;
1425 switch (SOShOp) {
1426 case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1427 case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1428 case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1429 case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1430 default:
1431 llvm_unreachable("Unknown so_reg opcode!");
1432 break;
1433 }
1434 SDValue SOShImm =
1435 CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1436 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1437 SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001438 return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
Evan Cheng9ef48352009-11-20 00:54:03 +00001439 }
1440 return 0;
1441}
1442
1443SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001444SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001445 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1446 SDValue CPTmp0;
1447 SDValue CPTmp1;
1448 SDValue CPTmp2;
Chris Lattner52a261b2010-09-21 20:31:19 +00001449 if (SelectShifterOperandReg(TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001450 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1451 SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001452 return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
Evan Cheng9ef48352009-11-20 00:54:03 +00001453 }
1454 return 0;
1455}
1456
1457SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001458SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001459 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1460 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1461 if (!T)
1462 return 0;
1463
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001464 if (Pred_t2_so_imm(TrueVal.getNode())) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001465 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1466 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1467 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001468 return CurDAG->SelectNodeTo(N,
Evan Cheng9ef48352009-11-20 00:54:03 +00001469 ARM::t2MOVCCi, MVT::i32, Ops, 5);
1470 }
1471 return 0;
1472}
1473
1474SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001475SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001476 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1477 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1478 if (!T)
1479 return 0;
1480
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001481 if (Pred_so_imm(TrueVal.getNode())) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001482 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1483 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1484 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001485 return CurDAG->SelectNodeTo(N,
Evan Cheng9ef48352009-11-20 00:54:03 +00001486 ARM::MOVCCi, MVT::i32, Ops, 5);
1487 }
1488 return 0;
1489}
1490
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001491SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
1492 EVT VT = N->getValueType(0);
1493 SDValue FalseVal = N->getOperand(0);
1494 SDValue TrueVal = N->getOperand(1);
1495 SDValue CC = N->getOperand(2);
1496 SDValue CCR = N->getOperand(3);
1497 SDValue InFlag = N->getOperand(4);
Evan Cheng9ef48352009-11-20 00:54:03 +00001498 assert(CC.getOpcode() == ISD::Constant);
1499 assert(CCR.getOpcode() == ISD::Register);
1500 ARMCC::CondCodes CCVal =
1501 (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
Evan Cheng07ba9062009-11-19 21:45:22 +00001502
1503 if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1504 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1505 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1506 // Pattern complexity = 18 cost = 1 size = 0
1507 SDValue CPTmp0;
1508 SDValue CPTmp1;
1509 SDValue CPTmp2;
1510 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001511 SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001512 CCVal, CCR, InFlag);
1513 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001514 Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001515 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1516 if (Res)
1517 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001518 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001519 SDNode *Res = SelectARMCMOVShiftOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001520 CCVal, CCR, InFlag);
1521 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001522 Res = SelectARMCMOVShiftOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001523 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1524 if (Res)
1525 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001526 }
1527
1528 // Pattern: (ARMcmov:i32 GPR:i32:$false,
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001529 // (imm:i32)<<P:Pred_so_imm>>:$true,
Evan Cheng07ba9062009-11-19 21:45:22 +00001530 // (imm:i32):$cc)
1531 // Emits: (MOVCCi:i32 GPR:i32:$false,
1532 // (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1533 // Pattern complexity = 10 cost = 1 size = 0
Evan Cheng9ef48352009-11-20 00:54:03 +00001534 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001535 SDNode *Res = SelectT2CMOVSoImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001536 CCVal, CCR, InFlag);
1537 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001538 Res = SelectT2CMOVSoImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001539 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1540 if (Res)
1541 return Res;
1542 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001543 SDNode *Res = SelectARMCMOVSoImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001544 CCVal, CCR, InFlag);
1545 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001546 Res = SelectARMCMOVSoImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001547 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1548 if (Res)
1549 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001550 }
1551 }
1552
1553 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1554 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1555 // Pattern complexity = 6 cost = 1 size = 0
1556 //
1557 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1558 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1559 // Pattern complexity = 6 cost = 11 size = 0
1560 //
1561 // Also FCPYScc and FCPYDcc.
Evan Cheng9ef48352009-11-20 00:54:03 +00001562 SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1563 SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
Evan Cheng07ba9062009-11-19 21:45:22 +00001564 unsigned Opc = 0;
1565 switch (VT.getSimpleVT().SimpleTy) {
1566 default: assert(false && "Illegal conditional move type!");
1567 break;
1568 case MVT::i32:
1569 Opc = Subtarget->isThumb()
1570 ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1571 : ARM::MOVCCr;
1572 break;
1573 case MVT::f32:
1574 Opc = ARM::VMOVScc;
1575 break;
1576 case MVT::f64:
1577 Opc = ARM::VMOVDcc;
1578 break;
1579 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001580 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Cheng07ba9062009-11-19 21:45:22 +00001581}
1582
Evan Chengde8aa4e2010-05-05 18:28:36 +00001583SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
1584 // The only time a CONCAT_VECTORS operation can have legal types is when
1585 // two 64-bit vectors are concatenated to a 128-bit vector.
1586 EVT VT = N->getValueType(0);
1587 if (!VT.is128BitVector() || N->getNumOperands() != 2)
1588 llvm_unreachable("unexpected CONCAT_VECTORS");
1589 DebugLoc dl = N->getDebugLoc();
1590 SDValue V0 = N->getOperand(0);
1591 SDValue V1 = N->getOperand(1);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001592 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1593 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Evan Chengde8aa4e2010-05-05 18:28:36 +00001594 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1595 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1596}
1597
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001598SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
Dale Johannesened2eee62009-02-06 01:31:28 +00001599 DebugLoc dl = N->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001600
Dan Gohmane8be6c62008-07-17 19:10:17 +00001601 if (N->isMachineOpcode())
Evan Chenga8e29892007-01-19 07:51:42 +00001602 return NULL; // Already selected.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001603
1604 switch (N->getOpcode()) {
Evan Chenga8e29892007-01-19 07:51:42 +00001605 default: break;
1606 case ISD::Constant: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001607 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001608 bool UseCP = true;
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00001609 if (Subtarget->hasThumb2())
1610 // Thumb2-aware targets have the MOVT instruction, so all immediates can
1611 // be done with MOV + MOVT, at worst.
1612 UseCP = 0;
1613 else {
1614 if (Subtarget->isThumb()) {
Bob Wilsone64e3cf2009-06-22 17:29:13 +00001615 UseCP = (Val > 255 && // MOV
1616 ~Val > 255 && // MOV + MVN
1617 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00001618 } else
1619 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
1620 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
1621 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
1622 }
1623
Evan Chenga8e29892007-01-19 07:51:42 +00001624 if (UseCP) {
Dan Gohman475871a2008-07-27 21:46:04 +00001625 SDValue CPIdx =
Owen Anderson1d0be152009-08-13 21:58:54 +00001626 CurDAG->getTargetConstantPool(ConstantInt::get(
1627 Type::getInt32Ty(*CurDAG->getContext()), Val),
Evan Chenga8e29892007-01-19 07:51:42 +00001628 TLI.getPointerTy());
Evan Cheng012f2d92007-01-24 08:53:17 +00001629
1630 SDNode *ResNode;
Evan Cheng446c4282009-07-11 06:43:01 +00001631 if (Subtarget->isThumb1Only()) {
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001632 SDValue Pred = getAL(CurDAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00001633 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
Evan Cheng446c4282009-07-11 06:43:01 +00001634 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
Dan Gohman602b0c82009-09-25 18:54:59 +00001635 ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
1636 Ops, 4);
Evan Cheng446c4282009-07-11 06:43:01 +00001637 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001638 SDValue Ops[] = {
Jim Grosbach764ab522009-08-11 15:33:49 +00001639 CPIdx,
Owen Anderson825b72b2009-08-11 20:47:22 +00001640 CurDAG->getRegister(0, MVT::i32),
1641 CurDAG->getTargetConstant(0, MVT::i32),
Evan Chengee568cf2007-07-05 07:15:27 +00001642 getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +00001643 CurDAG->getRegister(0, MVT::i32),
Evan Cheng012f2d92007-01-24 08:53:17 +00001644 CurDAG->getEntryNode()
1645 };
Dan Gohman602b0c82009-09-25 18:54:59 +00001646 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
1647 Ops, 6);
Evan Cheng012f2d92007-01-24 08:53:17 +00001648 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001649 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
Evan Chenga8e29892007-01-19 07:51:42 +00001650 return NULL;
1651 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001652
Evan Chenga8e29892007-01-19 07:51:42 +00001653 // Other cases are autogenerated.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001654 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001655 }
Rafael Espindolaf819a492006-11-09 13:58:55 +00001656 case ISD::FrameIndex: {
Evan Chenga8e29892007-01-19 07:51:42 +00001657 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindolaf819a492006-11-09 13:58:55 +00001658 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohman475871a2008-07-27 21:46:04 +00001659 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
David Goodwinf1daf7d2009-07-08 23:10:31 +00001660 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001661 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
1662 CurDAG->getTargetConstant(0, MVT::i32));
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001663 } else {
David Goodwin419c6152009-07-14 18:48:51 +00001664 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
1665 ARM::t2ADDri : ARM::ADDri);
Owen Anderson825b72b2009-08-11 20:47:22 +00001666 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
1667 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1668 CurDAG->getRegister(0, MVT::i32) };
1669 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00001670 }
Evan Chenga8e29892007-01-19 07:51:42 +00001671 }
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001672 case ISD::SRL:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001673 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001674 return I;
1675 break;
1676 case ISD::SRA:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001677 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001678 return I;
1679 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001680 case ISD::MUL:
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001681 if (Subtarget->isThumb1Only())
Evan Cheng79d43262007-01-24 02:21:22 +00001682 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001683 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001684 unsigned RHSV = C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001685 if (!RHSV) break;
1686 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00001687 unsigned ShImm = Log2_32(RHSV-1);
1688 if (ShImm >= 32)
1689 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001690 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001691 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00001692 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1693 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00001694 if (Subtarget->isThumb()) {
Evan Chengaf9e7a72009-07-21 00:31:12 +00001695 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001696 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001697 } else {
1698 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001700 }
Evan Chenga8e29892007-01-19 07:51:42 +00001701 }
1702 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00001703 unsigned ShImm = Log2_32(RHSV+1);
1704 if (ShImm >= 32)
1705 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001706 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001707 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1709 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00001710 if (Subtarget->isThumb()) {
Bob Wilson13ef8402010-05-28 00:27:15 +00001711 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1712 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001713 } else {
1714 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001715 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001716 }
Evan Chenga8e29892007-01-19 07:51:42 +00001717 }
1718 }
1719 break;
Evan Cheng20956592009-10-21 08:15:52 +00001720 case ISD::AND: {
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001721 // Check for unsigned bitfield extract
1722 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
1723 return I;
1724
Evan Cheng20956592009-10-21 08:15:52 +00001725 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
1726 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
1727 // are entirely contributed by c2 and lower 16-bits are entirely contributed
1728 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
1729 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001730 EVT VT = N->getValueType(0);
Evan Cheng20956592009-10-21 08:15:52 +00001731 if (VT != MVT::i32)
1732 break;
1733 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
1734 ? ARM::t2MOVTi16
1735 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
1736 if (!Opc)
1737 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001738 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Evan Cheng20956592009-10-21 08:15:52 +00001739 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1740 if (!N1C)
1741 break;
1742 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
1743 SDValue N2 = N0.getOperand(1);
1744 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1745 if (!N2C)
1746 break;
1747 unsigned N1CVal = N1C->getZExtValue();
1748 unsigned N2CVal = N2C->getZExtValue();
1749 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
1750 (N1CVal & 0xffffU) == 0xffffU &&
1751 (N2CVal & 0xffffU) == 0x0U) {
1752 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
1753 MVT::i32);
1754 SDValue Ops[] = { N0.getOperand(0), Imm16,
1755 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1756 return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
1757 }
1758 }
1759 break;
1760 }
Jim Grosbache5165492009-11-09 00:11:35 +00001761 case ARMISD::VMOVRRD:
1762 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001763 N->getOperand(0), getAL(CurDAG),
Dan Gohman602b0c82009-09-25 18:54:59 +00001764 CurDAG->getRegister(0, MVT::i32));
Dan Gohman525178c2007-10-08 18:33:35 +00001765 case ISD::UMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001766 if (Subtarget->isThumb1Only())
1767 break;
1768 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001769 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001770 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1771 CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00001772 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001773 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001774 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001775 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1776 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00001777 return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001778 }
Evan Chengee568cf2007-07-05 07:15:27 +00001779 }
Dan Gohman525178c2007-10-08 18:33:35 +00001780 case ISD::SMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001781 if (Subtarget->isThumb1Only())
1782 break;
1783 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001784 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001785 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00001786 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001787 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001788 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001789 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1790 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00001791 return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001792 }
Evan Chengee568cf2007-07-05 07:15:27 +00001793 }
Evan Chenga8e29892007-01-19 07:51:42 +00001794 case ISD::LOAD: {
Evan Chenge88d5ce2009-07-02 07:28:31 +00001795 SDNode *ResNode = 0;
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001796 if (Subtarget->isThumb() && Subtarget->hasThumb2())
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001797 ResNode = SelectT2IndexedLoad(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +00001798 else
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001799 ResNode = SelectARMIndexedLoad(N);
Evan Chengaf4550f2009-07-02 01:23:32 +00001800 if (ResNode)
1801 return ResNode;
Evan Chenga8e29892007-01-19 07:51:42 +00001802 // Other cases are autogenerated.
Rafael Espindolaf819a492006-11-09 13:58:55 +00001803 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001804 }
Evan Chengee568cf2007-07-05 07:15:27 +00001805 case ARMISD::BRCOND: {
1806 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1807 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1808 // Pattern complexity = 6 cost = 1 size = 0
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001809
Evan Chengee568cf2007-07-05 07:15:27 +00001810 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1811 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
1812 // Pattern complexity = 6 cost = 1 size = 0
1813
David Goodwin5e47a9a2009-06-30 18:04:13 +00001814 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1815 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1816 // Pattern complexity = 6 cost = 1 size = 0
1817
Jim Grosbach764ab522009-08-11 15:33:49 +00001818 unsigned Opc = Subtarget->isThumb() ?
David Goodwin5e47a9a2009-06-30 18:04:13 +00001819 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001820 SDValue Chain = N->getOperand(0);
1821 SDValue N1 = N->getOperand(1);
1822 SDValue N2 = N->getOperand(2);
1823 SDValue N3 = N->getOperand(3);
1824 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00001825 assert(N1.getOpcode() == ISD::BasicBlock);
1826 assert(N2.getOpcode() == ISD::Constant);
1827 assert(N3.getOpcode() == ISD::Register);
1828
Dan Gohman475871a2008-07-27 21:46:04 +00001829 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001830 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00001831 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001832 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
Dan Gohman602b0c82009-09-25 18:54:59 +00001833 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
1834 MVT::Flag, Ops, 5);
Dan Gohman475871a2008-07-27 21:46:04 +00001835 Chain = SDValue(ResNode, 0);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001836 if (N->getNumValues() == 2) {
Dan Gohman475871a2008-07-27 21:46:04 +00001837 InFlag = SDValue(ResNode, 1);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001838 ReplaceUses(SDValue(N, 1), InFlag);
Chris Lattnera47b9bc2008-02-03 03:20:59 +00001839 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001840 ReplaceUses(SDValue(N, 0),
Evan Chenged54de42009-11-19 08:16:50 +00001841 SDValue(Chain.getNode(), Chain.getResNo()));
Evan Chengee568cf2007-07-05 07:15:27 +00001842 return NULL;
1843 }
Evan Cheng07ba9062009-11-19 21:45:22 +00001844 case ARMISD::CMOV:
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001845 return SelectCMOVOp(N);
Evan Chengee568cf2007-07-05 07:15:27 +00001846 case ARMISD::CNEG: {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001847 EVT VT = N->getValueType(0);
1848 SDValue N0 = N->getOperand(0);
1849 SDValue N1 = N->getOperand(1);
1850 SDValue N2 = N->getOperand(2);
1851 SDValue N3 = N->getOperand(3);
1852 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00001853 assert(N2.getOpcode() == ISD::Constant);
1854 assert(N3.getOpcode() == ISD::Register);
1855
Dan Gohman475871a2008-07-27 21:46:04 +00001856 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001857 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00001858 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001859 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
Evan Chengee568cf2007-07-05 07:15:27 +00001860 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001861 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengee568cf2007-07-05 07:15:27 +00001862 default: assert(false && "Illegal conditional move type!");
1863 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001864 case MVT::f32:
Jim Grosbache5165492009-11-09 00:11:35 +00001865 Opc = ARM::VNEGScc;
Evan Chengee568cf2007-07-05 07:15:27 +00001866 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001867 case MVT::f64:
Jim Grosbache5165492009-11-09 00:11:35 +00001868 Opc = ARM::VNEGDcc;
Evan Chenge5ad88e2008-12-10 21:54:21 +00001869 break;
Evan Chengee568cf2007-07-05 07:15:27 +00001870 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001871 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00001872 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00001873
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001874 case ARMISD::VZIP: {
1875 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001876 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001877 switch (VT.getSimpleVT().SimpleTy) {
1878 default: return NULL;
1879 case MVT::v8i8: Opc = ARM::VZIPd8; break;
1880 case MVT::v4i16: Opc = ARM::VZIPd16; break;
1881 case MVT::v2f32:
1882 case MVT::v2i32: Opc = ARM::VZIPd32; break;
1883 case MVT::v16i8: Opc = ARM::VZIPq8; break;
1884 case MVT::v8i16: Opc = ARM::VZIPq16; break;
1885 case MVT::v4f32:
1886 case MVT::v4i32: Opc = ARM::VZIPq32; break;
1887 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001888 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001889 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1890 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1891 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001892 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001893 case ARMISD::VUZP: {
1894 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001895 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001896 switch (VT.getSimpleVT().SimpleTy) {
1897 default: return NULL;
1898 case MVT::v8i8: Opc = ARM::VUZPd8; break;
1899 case MVT::v4i16: Opc = ARM::VUZPd16; break;
1900 case MVT::v2f32:
1901 case MVT::v2i32: Opc = ARM::VUZPd32; break;
1902 case MVT::v16i8: Opc = ARM::VUZPq8; break;
1903 case MVT::v8i16: Opc = ARM::VUZPq16; break;
1904 case MVT::v4f32:
1905 case MVT::v4i32: Opc = ARM::VUZPq32; break;
1906 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001907 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001908 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1909 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1910 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001911 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001912 case ARMISD::VTRN: {
1913 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001914 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001915 switch (VT.getSimpleVT().SimpleTy) {
1916 default: return NULL;
1917 case MVT::v8i8: Opc = ARM::VTRNd8; break;
1918 case MVT::v4i16: Opc = ARM::VTRNd16; break;
1919 case MVT::v2f32:
1920 case MVT::v2i32: Opc = ARM::VTRNd32; break;
1921 case MVT::v16i8: Opc = ARM::VTRNq8; break;
1922 case MVT::v8i16: Opc = ARM::VTRNq16; break;
1923 case MVT::v4f32:
1924 case MVT::v4i32: Opc = ARM::VTRNq32; break;
1925 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001926 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001927 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1928 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1929 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001930 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001931 case ARMISD::BUILD_VECTOR: {
1932 EVT VecVT = N->getValueType(0);
1933 EVT EltVT = VecVT.getVectorElementType();
1934 unsigned NumElts = VecVT.getVectorNumElements();
1935 if (EltVT.getSimpleVT() == MVT::f64) {
1936 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
1937 return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
1938 }
1939 assert(EltVT.getSimpleVT() == MVT::f32 &&
1940 "unexpected type for BUILD_VECTOR");
1941 if (NumElts == 2)
1942 return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
1943 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
1944 return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
1945 N->getOperand(2), N->getOperand(3));
1946 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00001947
1948 case ISD::INTRINSIC_VOID:
1949 case ISD::INTRINSIC_W_CHAIN: {
1950 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Bob Wilson31fb12f2009-08-26 17:39:53 +00001951 switch (IntNo) {
1952 default:
Bob Wilson429009b2010-05-06 16:05:26 +00001953 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00001954
Bob Wilson621f1952010-03-23 05:25:43 +00001955 case Intrinsic::arm_neon_vld1: {
1956 unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
1957 ARM::VLD1d32, ARM::VLD1d64 };
Bob Wilsonffde0802010-09-02 16:00:54 +00001958 unsigned QOpcodes[] = { ARM::VLD1q8Pseudo, ARM::VLD1q16Pseudo,
1959 ARM::VLD1q32Pseudo, ARM::VLD1q64Pseudo };
Bob Wilson621f1952010-03-23 05:25:43 +00001960 return SelectVLD(N, 1, DOpcodes, QOpcodes, 0);
1961 }
1962
Bob Wilson31fb12f2009-08-26 17:39:53 +00001963 case Intrinsic::arm_neon_vld2: {
Bob Wilsonffde0802010-09-02 16:00:54 +00001964 unsigned DOpcodes[] = { ARM::VLD2d8Pseudo, ARM::VLD2d16Pseudo,
1965 ARM::VLD2d32Pseudo, ARM::VLD1q64Pseudo };
1966 unsigned QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
1967 ARM::VLD2q32Pseudo };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001968 return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00001969 }
1970
1971 case Intrinsic::arm_neon_vld3: {
Bob Wilsonf5721912010-09-03 18:16:02 +00001972 unsigned DOpcodes[] = { ARM::VLD3d8Pseudo, ARM::VLD3d16Pseudo,
1973 ARM::VLD3d32Pseudo, ARM::VLD1d64TPseudo };
1974 unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
1975 ARM::VLD3q16Pseudo_UPD,
1976 ARM::VLD3q32Pseudo_UPD };
1977 unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
1978 ARM::VLD3q16oddPseudo_UPD,
1979 ARM::VLD3q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001980 return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00001981 }
1982
1983 case Intrinsic::arm_neon_vld4: {
Bob Wilsonf5721912010-09-03 18:16:02 +00001984 unsigned DOpcodes[] = { ARM::VLD4d8Pseudo, ARM::VLD4d16Pseudo,
1985 ARM::VLD4d32Pseudo, ARM::VLD1d64QPseudo };
1986 unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
1987 ARM::VLD4q16Pseudo_UPD,
1988 ARM::VLD4q32Pseudo_UPD };
1989 unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
1990 ARM::VLD4q16oddPseudo_UPD,
1991 ARM::VLD4q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001992 return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00001993 }
1994
Bob Wilson243fcc52009-09-01 04:26:28 +00001995 case Intrinsic::arm_neon_vld2lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00001996 unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo, ARM::VLD2LNd16Pseudo,
1997 ARM::VLD2LNd32Pseudo };
1998 unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo, ARM::VLD2LNq32Pseudo };
1999 return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002000 }
2001
2002 case Intrinsic::arm_neon_vld3lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002003 unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo, ARM::VLD3LNd16Pseudo,
2004 ARM::VLD3LNd32Pseudo };
2005 unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo, ARM::VLD3LNq32Pseudo };
2006 return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002007 }
2008
2009 case Intrinsic::arm_neon_vld4lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002010 unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo, ARM::VLD4LNd16Pseudo,
2011 ARM::VLD4LNd32Pseudo };
2012 unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo, ARM::VLD4LNq32Pseudo };
2013 return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002014 }
2015
Bob Wilson11d98992010-03-23 06:20:33 +00002016 case Intrinsic::arm_neon_vst1: {
2017 unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2018 ARM::VST1d32, ARM::VST1d64 };
Bob Wilsone5ce4f62010-08-28 05:12:57 +00002019 unsigned QOpcodes[] = { ARM::VST1q8Pseudo, ARM::VST1q16Pseudo,
2020 ARM::VST1q32Pseudo, ARM::VST1q64Pseudo };
Bob Wilson11d98992010-03-23 06:20:33 +00002021 return SelectVST(N, 1, DOpcodes, QOpcodes, 0);
2022 }
2023
Bob Wilson31fb12f2009-08-26 17:39:53 +00002024 case Intrinsic::arm_neon_vst2: {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00002025 unsigned DOpcodes[] = { ARM::VST2d8Pseudo, ARM::VST2d16Pseudo,
2026 ARM::VST2d32Pseudo, ARM::VST1q64Pseudo };
2027 unsigned QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
2028 ARM::VST2q32Pseudo };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002029 return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002030 }
2031
2032 case Intrinsic::arm_neon_vst3: {
Bob Wilson01ba4612010-08-26 18:51:29 +00002033 unsigned DOpcodes[] = { ARM::VST3d8Pseudo, ARM::VST3d16Pseudo,
2034 ARM::VST3d32Pseudo, ARM::VST1d64TPseudo };
2035 unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2036 ARM::VST3q16Pseudo_UPD,
2037 ARM::VST3q32Pseudo_UPD };
2038 unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2039 ARM::VST3q16oddPseudo_UPD,
2040 ARM::VST3q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002041 return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002042 }
2043
2044 case Intrinsic::arm_neon_vst4: {
Bob Wilson709d5922010-08-25 23:27:42 +00002045 unsigned DOpcodes[] = { ARM::VST4d8Pseudo, ARM::VST4d16Pseudo,
Bob Wilson70e48b22010-08-26 05:33:30 +00002046 ARM::VST4d32Pseudo, ARM::VST1d64QPseudo };
Bob Wilson709d5922010-08-25 23:27:42 +00002047 unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2048 ARM::VST4q16Pseudo_UPD,
2049 ARM::VST4q32Pseudo_UPD };
2050 unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2051 ARM::VST4q16oddPseudo_UPD,
2052 ARM::VST4q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002053 return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002054 }
Bob Wilson8a3198b2009-09-01 18:51:56 +00002055
2056 case Intrinsic::arm_neon_vst2lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002057 unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo, ARM::VST2LNd16Pseudo,
2058 ARM::VST2LNd32Pseudo };
2059 unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo, ARM::VST2LNq32Pseudo };
2060 return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002061 }
2062
2063 case Intrinsic::arm_neon_vst3lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002064 unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo, ARM::VST3LNd16Pseudo,
2065 ARM::VST3LNd32Pseudo };
2066 unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo, ARM::VST3LNq32Pseudo };
2067 return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002068 }
2069
2070 case Intrinsic::arm_neon_vst4lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002071 unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo, ARM::VST4LNd16Pseudo,
2072 ARM::VST4LNd32Pseudo };
2073 unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo, ARM::VST4LNq32Pseudo };
2074 return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002075 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00002076 }
Bob Wilson429009b2010-05-06 16:05:26 +00002077 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00002078 }
Evan Chengde8aa4e2010-05-05 18:28:36 +00002079
Bob Wilsond491d6e2010-07-06 23:36:25 +00002080 case ISD::INTRINSIC_WO_CHAIN: {
2081 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2082 switch (IntNo) {
2083 default:
2084 break;
2085
2086 case Intrinsic::arm_neon_vtbl2:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002087 return SelectVTBL(N, false, 2, ARM::VTBL2Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002088 case Intrinsic::arm_neon_vtbl3:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002089 return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002090 case Intrinsic::arm_neon_vtbl4:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002091 return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002092
2093 case Intrinsic::arm_neon_vtbx2:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002094 return SelectVTBL(N, true, 2, ARM::VTBX2Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002095 case Intrinsic::arm_neon_vtbx3:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002096 return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002097 case Intrinsic::arm_neon_vtbx4:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002098 return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002099 }
2100 break;
2101 }
2102
Bob Wilson429009b2010-05-06 16:05:26 +00002103 case ISD::CONCAT_VECTORS:
Evan Chengde8aa4e2010-05-05 18:28:36 +00002104 return SelectConcatVector(N);
2105 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00002106
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002107 return SelectCode(N);
Evan Chenga8e29892007-01-19 07:51:42 +00002108}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002109
Bob Wilson224c2442009-05-19 05:53:42 +00002110bool ARMDAGToDAGISel::
2111SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2112 std::vector<SDValue> &OutOps) {
2113 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
Bob Wilson765cc0b2009-10-13 20:50:28 +00002114 // Require the address to be in a register. That is safe for all ARM
2115 // variants and it is hard to do anything much smarter without knowing
2116 // how the operand is used.
2117 OutOps.push_back(Op);
Bob Wilson224c2442009-05-19 05:53:42 +00002118 return false;
2119}
2120
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002121/// createARMISelDag - This pass converts a legalized DAG into a
2122/// ARM-specific DAG, ready for instruction scheduling.
2123///
Bob Wilson522ce972009-09-28 14:30:20 +00002124FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2125 CodeGenOpt::Level OptLevel) {
2126 return new ARMDAGToDAGISel(TM, OptLevel);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002127}