blob: bcaf6623393cd310fcb920c0aa4a481cefad5830 [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
Bob Wilson2a6e6162010-09-23 23:42:37 +00001005/// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1006/// of a NEON VLD or VST instruction. The supported values depend on the
1007/// number of registers being loaded.
1008static unsigned GetVLDSTAlign(SDNode *N, unsigned NumVecs, bool is64BitVector) {
1009 unsigned NumRegs = NumVecs;
1010 if (!is64BitVector && NumVecs < 3)
1011 NumRegs *= 2;
1012
1013 unsigned Alignment = cast<MemIntrinsicSDNode>(N)->getAlignment();
1014 if (Alignment >= 32 && NumRegs == 4)
1015 return 32;
1016 if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1017 return 16;
1018 if (Alignment >= 8)
1019 return 8;
1020 return 0;
1021}
1022
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001023SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
Bob Wilson3e36f132009-10-14 17:28:52 +00001024 unsigned *DOpcodes, unsigned *QOpcodes0,
1025 unsigned *QOpcodes1) {
Bob Wilson621f1952010-03-23 05:25:43 +00001026 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
Bob Wilson3e36f132009-10-14 17:28:52 +00001027 DebugLoc dl = N->getDebugLoc();
1028
Bob Wilson226036e2010-03-20 22:13:40 +00001029 SDValue MemAddr, Align;
Chris Lattner52a261b2010-09-21 20:31:19 +00001030 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
Bob Wilson3e36f132009-10-14 17:28:52 +00001031 return NULL;
1032
1033 SDValue Chain = N->getOperand(0);
1034 EVT VT = N->getValueType(0);
1035 bool is64BitVector = VT.is64BitVector();
1036
Bob Wilson2a6e6162010-09-23 23:42:37 +00001037 unsigned Alignment = GetVLDSTAlign(N, NumVecs, is64BitVector);
Bob Wilson40ff01a2010-09-23 21:43:54 +00001038 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1039
Bob Wilson3e36f132009-10-14 17:28:52 +00001040 unsigned OpcodeIndex;
1041 switch (VT.getSimpleVT().SimpleTy) {
1042 default: llvm_unreachable("unhandled vld type");
1043 // Double-register operations:
1044 case MVT::v8i8: OpcodeIndex = 0; break;
1045 case MVT::v4i16: OpcodeIndex = 1; break;
1046 case MVT::v2f32:
1047 case MVT::v2i32: OpcodeIndex = 2; break;
1048 case MVT::v1i64: OpcodeIndex = 3; break;
1049 // Quad-register operations:
1050 case MVT::v16i8: OpcodeIndex = 0; break;
1051 case MVT::v8i16: OpcodeIndex = 1; break;
1052 case MVT::v4f32:
1053 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson621f1952010-03-23 05:25:43 +00001054 case MVT::v2i64: OpcodeIndex = 3;
Bob Wilson11d98992010-03-23 06:20:33 +00001055 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
Bob Wilson621f1952010-03-23 05:25:43 +00001056 break;
Bob Wilson3e36f132009-10-14 17:28:52 +00001057 }
1058
Bob Wilsonf5721912010-09-03 18:16:02 +00001059 EVT ResTy;
1060 if (NumVecs == 1)
1061 ResTy = VT;
1062 else {
1063 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1064 if (!is64BitVector)
1065 ResTyElts *= 2;
1066 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1067 }
1068
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001069 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001070 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilsonf5721912010-09-03 18:16:02 +00001071 SDValue SuperReg;
Bob Wilson3e36f132009-10-14 17:28:52 +00001072 if (is64BitVector) {
1073 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001074 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilsonf5721912010-09-03 18:16:02 +00001075 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
Bob Wilsonffde0802010-09-02 16:00:54 +00001076 if (NumVecs == 1)
Evan Chenge9e2ba02010-05-10 21:26:24 +00001077 return VLd;
1078
Bob Wilsonf5721912010-09-03 18:16:02 +00001079 SuperReg = SDValue(VLd, 0);
Jakob Stoklund Olesen7bb31e32010-05-24 17:13:28 +00001080 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
Evan Cheng5c6aba22010-05-14 18:54:59 +00001081 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001082 SDValue D = CurDAG->getTargetExtractSubreg(ARM::dsub_0+Vec,
Bob Wilsonffde0802010-09-02 16:00:54 +00001083 dl, VT, SuperReg);
Evan Cheng5c6aba22010-05-14 18:54:59 +00001084 ReplaceUses(SDValue(N, Vec), D);
Evan Chenge9e2ba02010-05-10 21:26:24 +00001085 }
Bob Wilsonf5721912010-09-03 18:16:02 +00001086 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
Evan Chenge9e2ba02010-05-10 21:26:24 +00001087 return NULL;
Bob Wilson3e36f132009-10-14 17:28:52 +00001088 }
1089
Bob Wilson621f1952010-03-23 05:25:43 +00001090 if (NumVecs <= 2) {
1091 // Quad registers are directly supported for VLD1 and VLD2,
1092 // loading pairs of D regs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001093 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001094 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilsonffde0802010-09-02 16:00:54 +00001095 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
Bob Wilsonffde0802010-09-02 16:00:54 +00001096 if (NumVecs == 1)
1097 return VLd;
1098
Bob Wilsonf5721912010-09-03 18:16:02 +00001099 SuperReg = SDValue(VLd, 0);
Bob Wilsonffde0802010-09-02 16:00:54 +00001100 Chain = SDValue(VLd, 1);
1101
Bob Wilson3e36f132009-10-14 17:28:52 +00001102 } else {
1103 // Otherwise, quad registers are loaded with two separate instructions,
1104 // where one loads the even registers and the other loads the odd registers.
Bob Wilsonf5721912010-09-03 18:16:02 +00001105 EVT AddrTy = MemAddr.getValueType();
Bob Wilson3e36f132009-10-14 17:28:52 +00001106
Bob Wilson24f995d2009-10-14 18:32:29 +00001107 // Load the even subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001108 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilsonf5721912010-09-03 18:16:02 +00001109 SDValue ImplDef =
1110 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1111 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1112 SDNode *VLdA =
1113 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsA, 7);
1114 Chain = SDValue(VLdA, 2);
Bob Wilson3e36f132009-10-14 17:28:52 +00001115
Bob Wilson24f995d2009-10-14 18:32:29 +00001116 // Load the odd subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001117 Opc = QOpcodes1[OpcodeIndex];
Bob Wilsonf5721912010-09-03 18:16:02 +00001118 const SDValue OpsB[] = { SDValue(VLdA, 1), Align, Reg0, SDValue(VLdA, 0),
1119 Pred, Reg0, Chain };
1120 SDNode *VLdB =
1121 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsB, 7);
1122 SuperReg = SDValue(VLdB, 0);
1123 Chain = SDValue(VLdB, 2);
1124 }
Bob Wilson3e36f132009-10-14 17:28:52 +00001125
Bob Wilsonf5721912010-09-03 18:16:02 +00001126 // Extract out the Q registers.
1127 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1128 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1129 SDValue Q = CurDAG->getTargetExtractSubreg(ARM::qsub_0+Vec,
1130 dl, VT, SuperReg);
1131 ReplaceUses(SDValue(N, Vec), Q);
Bob Wilson3e36f132009-10-14 17:28:52 +00001132 }
1133 ReplaceUses(SDValue(N, NumVecs), Chain);
1134 return NULL;
1135}
1136
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001137SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
Bob Wilson24f995d2009-10-14 18:32:29 +00001138 unsigned *DOpcodes, unsigned *QOpcodes0,
1139 unsigned *QOpcodes1) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001140 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
Bob Wilson24f995d2009-10-14 18:32:29 +00001141 DebugLoc dl = N->getDebugLoc();
1142
Bob Wilson226036e2010-03-20 22:13:40 +00001143 SDValue MemAddr, Align;
Chris Lattner52a261b2010-09-21 20:31:19 +00001144 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
Bob Wilson24f995d2009-10-14 18:32:29 +00001145 return NULL;
1146
1147 SDValue Chain = N->getOperand(0);
1148 EVT VT = N->getOperand(3).getValueType();
1149 bool is64BitVector = VT.is64BitVector();
1150
Bob Wilson2a6e6162010-09-23 23:42:37 +00001151 unsigned Alignment = GetVLDSTAlign(N, NumVecs, is64BitVector);
1152 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1153
Bob Wilson24f995d2009-10-14 18:32:29 +00001154 unsigned OpcodeIndex;
1155 switch (VT.getSimpleVT().SimpleTy) {
1156 default: llvm_unreachable("unhandled vst type");
1157 // Double-register operations:
1158 case MVT::v8i8: OpcodeIndex = 0; break;
1159 case MVT::v4i16: OpcodeIndex = 1; break;
1160 case MVT::v2f32:
1161 case MVT::v2i32: OpcodeIndex = 2; break;
1162 case MVT::v1i64: OpcodeIndex = 3; break;
1163 // Quad-register operations:
1164 case MVT::v16i8: OpcodeIndex = 0; break;
1165 case MVT::v8i16: OpcodeIndex = 1; break;
1166 case MVT::v4f32:
1167 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson11d98992010-03-23 06:20:33 +00001168 case MVT::v2i64: OpcodeIndex = 3;
1169 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1170 break;
Bob Wilson24f995d2009-10-14 18:32:29 +00001171 }
1172
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001173 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001174 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001175
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001176 SmallVector<SDValue, 7> Ops;
Bob Wilson24f995d2009-10-14 18:32:29 +00001177 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001178 Ops.push_back(Align);
Bob Wilson24f995d2009-10-14 18:32:29 +00001179
1180 if (is64BitVector) {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001181 if (NumVecs == 1) {
1182 Ops.push_back(N->getOperand(3));
1183 } else {
Evan Cheng0ce537a2010-05-11 01:19:40 +00001184 SDValue RegSeq;
1185 SDValue V0 = N->getOperand(0+3);
1186 SDValue V1 = N->getOperand(1+3);
1187
1188 // Form a REG_SEQUENCE to force register allocation.
1189 if (NumVecs == 2)
1190 RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1191 else {
1192 SDValue V2 = N->getOperand(2+3);
1193 // If it's a vld3, form a quad D-register and leave the last part as
1194 // an undef.
1195 SDValue V3 = (NumVecs == 3)
1196 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1197 : N->getOperand(3+3);
1198 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1199 }
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001200 Ops.push_back(RegSeq);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001201 }
Evan Chengac0869d2009-11-21 06:21:52 +00001202 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001203 Ops.push_back(Reg0); // predicate register
Bob Wilson24f995d2009-10-14 18:32:29 +00001204 Ops.push_back(Chain);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001205 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001206 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
Bob Wilson24f995d2009-10-14 18:32:29 +00001207 }
1208
Bob Wilson11d98992010-03-23 06:20:33 +00001209 if (NumVecs <= 2) {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001210 // Quad registers are directly supported for VST1 and VST2.
Bob Wilson24f995d2009-10-14 18:32:29 +00001211 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001212 if (NumVecs == 1) {
1213 Ops.push_back(N->getOperand(3));
1214 } else {
1215 // Form a QQ register.
Evan Cheng603afbf2010-05-10 17:34:18 +00001216 SDValue Q0 = N->getOperand(3);
1217 SDValue Q1 = N->getOperand(4);
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001218 Ops.push_back(SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0));
Bob Wilson24f995d2009-10-14 18:32:29 +00001219 }
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001220 Ops.push_back(Pred);
1221 Ops.push_back(Reg0); // predicate register
1222 Ops.push_back(Chain);
1223 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
Bob Wilson24f995d2009-10-14 18:32:29 +00001224 }
1225
1226 // Otherwise, quad registers are stored with two separate instructions,
1227 // where one stores the even registers and the other stores the odd registers.
Evan Cheng7189fd02010-05-15 07:53:37 +00001228
Bob Wilson07f6e802010-06-16 21:34:01 +00001229 // Form the QQQQ REG_SEQUENCE.
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001230 SDValue V0 = N->getOperand(0+3);
1231 SDValue V1 = N->getOperand(1+3);
1232 SDValue V2 = N->getOperand(2+3);
1233 SDValue V3 = (NumVecs == 3)
1234 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1235 : N->getOperand(3+3);
1236 SDValue RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilson07f6e802010-06-16 21:34:01 +00001237
1238 // Store the even D registers.
Bob Wilson07f6e802010-06-16 21:34:01 +00001239 Ops.push_back(Reg0); // post-access address offset
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001240 Ops.push_back(RegSeq);
Bob Wilson07f6e802010-06-16 21:34:01 +00001241 Ops.push_back(Pred);
1242 Ops.push_back(Reg0); // predicate register
1243 Ops.push_back(Chain);
1244 unsigned Opc = QOpcodes0[OpcodeIndex];
1245 SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001246 MVT::Other, Ops.data(), 7);
Bob Wilson07f6e802010-06-16 21:34:01 +00001247 Chain = SDValue(VStA, 1);
1248
1249 // Store the odd D registers.
1250 Ops[0] = SDValue(VStA, 0); // MemAddr
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001251 Ops[6] = Chain;
Bob Wilson07f6e802010-06-16 21:34:01 +00001252 Opc = QOpcodes1[OpcodeIndex];
1253 SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001254 MVT::Other, Ops.data(), 7);
Bob Wilson07f6e802010-06-16 21:34:01 +00001255 Chain = SDValue(VStB, 1);
1256 ReplaceUses(SDValue(N, 0), Chain);
1257 return NULL;
Bob Wilson24f995d2009-10-14 18:32:29 +00001258}
1259
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001260SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
Bob Wilson96493442009-10-14 16:46:45 +00001261 unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson8466fa12010-09-13 23:01:35 +00001262 unsigned *QOpcodes) {
Bob Wilson96493442009-10-14 16:46:45 +00001263 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001264 DebugLoc dl = N->getDebugLoc();
1265
Bob Wilson226036e2010-03-20 22:13:40 +00001266 SDValue MemAddr, Align;
Chris Lattner52a261b2010-09-21 20:31:19 +00001267 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
Bob Wilsona7c397c2009-10-14 16:19:03 +00001268 return NULL;
1269
1270 SDValue Chain = N->getOperand(0);
1271 unsigned Lane =
1272 cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
Bob Wilson96493442009-10-14 16:46:45 +00001273 EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
Bob Wilsona7c397c2009-10-14 16:19:03 +00001274 bool is64BitVector = VT.is64BitVector();
1275
Bob Wilsona7c397c2009-10-14 16:19:03 +00001276 unsigned OpcodeIndex;
1277 switch (VT.getSimpleVT().SimpleTy) {
Bob Wilson96493442009-10-14 16:46:45 +00001278 default: llvm_unreachable("unhandled vld/vst lane type");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001279 // Double-register operations:
1280 case MVT::v8i8: OpcodeIndex = 0; break;
1281 case MVT::v4i16: OpcodeIndex = 1; break;
1282 case MVT::v2f32:
1283 case MVT::v2i32: OpcodeIndex = 2; break;
1284 // Quad-register operations:
1285 case MVT::v8i16: OpcodeIndex = 0; break;
1286 case MVT::v4f32:
1287 case MVT::v4i32: OpcodeIndex = 1; break;
1288 }
1289
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001290 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001291 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001292
Bob Wilson8466fa12010-09-13 23:01:35 +00001293 SmallVector<SDValue, 7> Ops;
Bob Wilsona7c397c2009-10-14 16:19:03 +00001294 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001295 Ops.push_back(Align);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001296
Eric Christopher23da0b22010-09-14 08:31:25 +00001297 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1298 QOpcodes[OpcodeIndex]);
Bob Wilson07f6e802010-06-16 21:34:01 +00001299
Bob Wilson8466fa12010-09-13 23:01:35 +00001300 SDValue SuperReg;
1301 SDValue V0 = N->getOperand(0+3);
1302 SDValue V1 = N->getOperand(1+3);
1303 if (NumVecs == 2) {
1304 if (is64BitVector)
1305 SuperReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1306 else
1307 SuperReg = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001308 } else {
Bob Wilson8466fa12010-09-13 23:01:35 +00001309 SDValue V2 = N->getOperand(2+3);
1310 SDValue V3 = (NumVecs == 3)
1311 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1312 : N->getOperand(3+3);
1313 if (is64BitVector)
1314 SuperReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1315 else
1316 SuperReg = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001317 }
Bob Wilson8466fa12010-09-13 23:01:35 +00001318 Ops.push_back(SuperReg);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001319 Ops.push_back(getI32Imm(Lane));
Evan Chengac0869d2009-11-21 06:21:52 +00001320 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001321 Ops.push_back(Reg0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001322 Ops.push_back(Chain);
1323
Bob Wilson96493442009-10-14 16:46:45 +00001324 if (!IsLoad)
Bob Wilson8466fa12010-09-13 23:01:35 +00001325 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 7);
Bob Wilson96493442009-10-14 16:46:45 +00001326
Bob Wilson8466fa12010-09-13 23:01:35 +00001327 EVT ResTy;
1328 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1329 if (!is64BitVector)
1330 ResTyElts *= 2;
1331 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001332
Bob Wilson8466fa12010-09-13 23:01:35 +00001333 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other,
1334 Ops.data(), 7);
1335 SuperReg = SDValue(VLdLn, 0);
1336 Chain = SDValue(VLdLn, 1);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001337
Bob Wilson8466fa12010-09-13 23:01:35 +00001338 // Extract the subregisters.
Bob Wilson07f6e802010-06-16 21:34:01 +00001339 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1340 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1341 unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1342 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1343 ReplaceUses(SDValue(N, Vec),
Bob Wilson8466fa12010-09-13 23:01:35 +00001344 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1345 ReplaceUses(SDValue(N, NumVecs), Chain);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001346 return NULL;
1347}
1348
Bob Wilson78dfbc32010-07-07 00:08:54 +00001349SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1350 unsigned Opc) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001351 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1352 DebugLoc dl = N->getDebugLoc();
1353 EVT VT = N->getValueType(0);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001354 unsigned FirstTblReg = IsExt ? 2 : 1;
Bob Wilsond491d6e2010-07-06 23:36:25 +00001355
1356 // Form a REG_SEQUENCE to force register allocation.
1357 SDValue RegSeq;
Bob Wilson78dfbc32010-07-07 00:08:54 +00001358 SDValue V0 = N->getOperand(FirstTblReg + 0);
1359 SDValue V1 = N->getOperand(FirstTblReg + 1);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001360 if (NumVecs == 2)
1361 RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1362 else {
Bob Wilson78dfbc32010-07-07 00:08:54 +00001363 SDValue V2 = N->getOperand(FirstTblReg + 2);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001364 // If it's a vtbl3, form a quad D-register and leave the last part as
1365 // an undef.
1366 SDValue V3 = (NumVecs == 3)
1367 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
Bob Wilson78dfbc32010-07-07 00:08:54 +00001368 : N->getOperand(FirstTblReg + 3);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001369 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1370 }
1371
Bob Wilson78dfbc32010-07-07 00:08:54 +00001372 SmallVector<SDValue, 6> Ops;
1373 if (IsExt)
1374 Ops.push_back(N->getOperand(1));
Bob Wilsonbd916c52010-09-13 23:55:10 +00001375 Ops.push_back(RegSeq);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001376 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
Bob Wilsond491d6e2010-07-06 23:36:25 +00001377 Ops.push_back(getAL(CurDAG)); // predicate
1378 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
Bob Wilson78dfbc32010-07-07 00:08:54 +00001379 return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
Bob Wilsond491d6e2010-07-06 23:36:25 +00001380}
1381
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001382SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001383 bool isSigned) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001384 if (!Subtarget->hasV6T2Ops())
1385 return NULL;
Bob Wilson96493442009-10-14 16:46:45 +00001386
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001387 unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1388 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1389
1390
1391 // For unsigned extracts, check for a shift right and mask
1392 unsigned And_imm = 0;
1393 if (N->getOpcode() == ISD::AND) {
1394 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1395
1396 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1397 if (And_imm & (And_imm + 1))
1398 return NULL;
1399
1400 unsigned Srl_imm = 0;
1401 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1402 Srl_imm)) {
1403 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1404
1405 unsigned Width = CountTrailingOnes_32(And_imm);
1406 unsigned LSB = Srl_imm;
1407 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1408 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1409 CurDAG->getTargetConstant(LSB, MVT::i32),
1410 CurDAG->getTargetConstant(Width, MVT::i32),
1411 getAL(CurDAG), Reg0 };
1412 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1413 }
1414 }
1415 return NULL;
1416 }
1417
1418 // Otherwise, we're looking for a shift of a shift
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001419 unsigned Shl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001420 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001421 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1422 unsigned Srl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001423 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001424 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1425 unsigned Width = 32 - Srl_imm;
1426 int LSB = Srl_imm - Shl_imm;
Evan Cheng8000c6c2009-10-22 00:40:00 +00001427 if (LSB < 0)
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001428 return NULL;
1429 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001430 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001431 CurDAG->getTargetConstant(LSB, MVT::i32),
1432 CurDAG->getTargetConstant(Width, MVT::i32),
1433 getAL(CurDAG), Reg0 };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001434 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001435 }
1436 }
1437 return NULL;
1438}
1439
Evan Cheng9ef48352009-11-20 00:54:03 +00001440SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001441SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001442 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1443 SDValue CPTmp0;
1444 SDValue CPTmp1;
Chris Lattner52a261b2010-09-21 20:31:19 +00001445 if (SelectT2ShifterOperandReg(TrueVal, CPTmp0, CPTmp1)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001446 unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1447 unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1448 unsigned Opc = 0;
1449 switch (SOShOp) {
1450 case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1451 case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1452 case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1453 case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1454 default:
1455 llvm_unreachable("Unknown so_reg opcode!");
1456 break;
1457 }
1458 SDValue SOShImm =
1459 CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1460 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1461 SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001462 return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
Evan Cheng9ef48352009-11-20 00:54:03 +00001463 }
1464 return 0;
1465}
1466
1467SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001468SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001469 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1470 SDValue CPTmp0;
1471 SDValue CPTmp1;
1472 SDValue CPTmp2;
Chris Lattner52a261b2010-09-21 20:31:19 +00001473 if (SelectShifterOperandReg(TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001474 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1475 SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001476 return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
Evan Cheng9ef48352009-11-20 00:54:03 +00001477 }
1478 return 0;
1479}
1480
1481SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001482SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001483 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1484 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1485 if (!T)
1486 return 0;
1487
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001488 if (Pred_t2_so_imm(TrueVal.getNode())) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001489 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1490 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1491 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001492 return CurDAG->SelectNodeTo(N,
Evan Cheng9ef48352009-11-20 00:54:03 +00001493 ARM::t2MOVCCi, MVT::i32, Ops, 5);
1494 }
1495 return 0;
1496}
1497
1498SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001499SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001500 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1501 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1502 if (!T)
1503 return 0;
1504
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001505 if (Pred_so_imm(TrueVal.getNode())) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001506 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1507 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1508 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001509 return CurDAG->SelectNodeTo(N,
Evan Cheng9ef48352009-11-20 00:54:03 +00001510 ARM::MOVCCi, MVT::i32, Ops, 5);
1511 }
1512 return 0;
1513}
1514
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001515SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
1516 EVT VT = N->getValueType(0);
1517 SDValue FalseVal = N->getOperand(0);
1518 SDValue TrueVal = N->getOperand(1);
1519 SDValue CC = N->getOperand(2);
1520 SDValue CCR = N->getOperand(3);
1521 SDValue InFlag = N->getOperand(4);
Evan Cheng9ef48352009-11-20 00:54:03 +00001522 assert(CC.getOpcode() == ISD::Constant);
1523 assert(CCR.getOpcode() == ISD::Register);
1524 ARMCC::CondCodes CCVal =
1525 (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
Evan Cheng07ba9062009-11-19 21:45:22 +00001526
1527 if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1528 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1529 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1530 // Pattern complexity = 18 cost = 1 size = 0
1531 SDValue CPTmp0;
1532 SDValue CPTmp1;
1533 SDValue CPTmp2;
1534 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001535 SDNode *Res = SelectT2CMOVShiftOp(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 = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001539 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1540 if (Res)
1541 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001542 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001543 SDNode *Res = SelectARMCMOVShiftOp(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 = SelectARMCMOVShiftOp(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 // Pattern: (ARMcmov:i32 GPR:i32:$false,
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001553 // (imm:i32)<<P:Pred_so_imm>>:$true,
Evan Cheng07ba9062009-11-19 21:45:22 +00001554 // (imm:i32):$cc)
1555 // Emits: (MOVCCi:i32 GPR:i32:$false,
1556 // (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1557 // Pattern complexity = 10 cost = 1 size = 0
Evan Cheng9ef48352009-11-20 00:54:03 +00001558 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001559 SDNode *Res = SelectT2CMOVSoImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001560 CCVal, CCR, InFlag);
1561 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001562 Res = SelectT2CMOVSoImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001563 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1564 if (Res)
1565 return Res;
1566 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001567 SDNode *Res = SelectARMCMOVSoImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001568 CCVal, CCR, InFlag);
1569 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001570 Res = SelectARMCMOVSoImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001571 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1572 if (Res)
1573 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001574 }
1575 }
1576
1577 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1578 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1579 // Pattern complexity = 6 cost = 1 size = 0
1580 //
1581 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1582 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1583 // Pattern complexity = 6 cost = 11 size = 0
1584 //
1585 // Also FCPYScc and FCPYDcc.
Evan Cheng9ef48352009-11-20 00:54:03 +00001586 SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1587 SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
Evan Cheng07ba9062009-11-19 21:45:22 +00001588 unsigned Opc = 0;
1589 switch (VT.getSimpleVT().SimpleTy) {
1590 default: assert(false && "Illegal conditional move type!");
1591 break;
1592 case MVT::i32:
1593 Opc = Subtarget->isThumb()
1594 ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1595 : ARM::MOVCCr;
1596 break;
1597 case MVT::f32:
1598 Opc = ARM::VMOVScc;
1599 break;
1600 case MVT::f64:
1601 Opc = ARM::VMOVDcc;
1602 break;
1603 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001604 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Cheng07ba9062009-11-19 21:45:22 +00001605}
1606
Evan Chengde8aa4e2010-05-05 18:28:36 +00001607SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
1608 // The only time a CONCAT_VECTORS operation can have legal types is when
1609 // two 64-bit vectors are concatenated to a 128-bit vector.
1610 EVT VT = N->getValueType(0);
1611 if (!VT.is128BitVector() || N->getNumOperands() != 2)
1612 llvm_unreachable("unexpected CONCAT_VECTORS");
1613 DebugLoc dl = N->getDebugLoc();
1614 SDValue V0 = N->getOperand(0);
1615 SDValue V1 = N->getOperand(1);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001616 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1617 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Evan Chengde8aa4e2010-05-05 18:28:36 +00001618 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1619 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1620}
1621
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001622SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
Dale Johannesened2eee62009-02-06 01:31:28 +00001623 DebugLoc dl = N->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001624
Dan Gohmane8be6c62008-07-17 19:10:17 +00001625 if (N->isMachineOpcode())
Evan Chenga8e29892007-01-19 07:51:42 +00001626 return NULL; // Already selected.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001627
1628 switch (N->getOpcode()) {
Evan Chenga8e29892007-01-19 07:51:42 +00001629 default: break;
1630 case ISD::Constant: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001631 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001632 bool UseCP = true;
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00001633 if (Subtarget->hasThumb2())
1634 // Thumb2-aware targets have the MOVT instruction, so all immediates can
1635 // be done with MOV + MOVT, at worst.
1636 UseCP = 0;
1637 else {
1638 if (Subtarget->isThumb()) {
Bob Wilsone64e3cf2009-06-22 17:29:13 +00001639 UseCP = (Val > 255 && // MOV
1640 ~Val > 255 && // MOV + MVN
1641 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00001642 } else
1643 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
1644 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
1645 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
1646 }
1647
Evan Chenga8e29892007-01-19 07:51:42 +00001648 if (UseCP) {
Dan Gohman475871a2008-07-27 21:46:04 +00001649 SDValue CPIdx =
Owen Anderson1d0be152009-08-13 21:58:54 +00001650 CurDAG->getTargetConstantPool(ConstantInt::get(
1651 Type::getInt32Ty(*CurDAG->getContext()), Val),
Evan Chenga8e29892007-01-19 07:51:42 +00001652 TLI.getPointerTy());
Evan Cheng012f2d92007-01-24 08:53:17 +00001653
1654 SDNode *ResNode;
Evan Cheng446c4282009-07-11 06:43:01 +00001655 if (Subtarget->isThumb1Only()) {
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001656 SDValue Pred = getAL(CurDAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00001657 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
Evan Cheng446c4282009-07-11 06:43:01 +00001658 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
Dan Gohman602b0c82009-09-25 18:54:59 +00001659 ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
1660 Ops, 4);
Evan Cheng446c4282009-07-11 06:43:01 +00001661 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001662 SDValue Ops[] = {
Jim Grosbach764ab522009-08-11 15:33:49 +00001663 CPIdx,
Owen Anderson825b72b2009-08-11 20:47:22 +00001664 CurDAG->getRegister(0, MVT::i32),
1665 CurDAG->getTargetConstant(0, MVT::i32),
Evan Chengee568cf2007-07-05 07:15:27 +00001666 getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +00001667 CurDAG->getRegister(0, MVT::i32),
Evan Cheng012f2d92007-01-24 08:53:17 +00001668 CurDAG->getEntryNode()
1669 };
Dan Gohman602b0c82009-09-25 18:54:59 +00001670 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
1671 Ops, 6);
Evan Cheng012f2d92007-01-24 08:53:17 +00001672 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001673 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
Evan Chenga8e29892007-01-19 07:51:42 +00001674 return NULL;
1675 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001676
Evan Chenga8e29892007-01-19 07:51:42 +00001677 // Other cases are autogenerated.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001678 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001679 }
Rafael Espindolaf819a492006-11-09 13:58:55 +00001680 case ISD::FrameIndex: {
Evan Chenga8e29892007-01-19 07:51:42 +00001681 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindolaf819a492006-11-09 13:58:55 +00001682 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohman475871a2008-07-27 21:46:04 +00001683 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
David Goodwinf1daf7d2009-07-08 23:10:31 +00001684 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001685 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
1686 CurDAG->getTargetConstant(0, MVT::i32));
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001687 } else {
David Goodwin419c6152009-07-14 18:48:51 +00001688 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
1689 ARM::t2ADDri : ARM::ADDri);
Owen Anderson825b72b2009-08-11 20:47:22 +00001690 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
1691 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1692 CurDAG->getRegister(0, MVT::i32) };
1693 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00001694 }
Evan Chenga8e29892007-01-19 07:51:42 +00001695 }
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001696 case ISD::SRL:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001697 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001698 return I;
1699 break;
1700 case ISD::SRA:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001701 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001702 return I;
1703 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001704 case ISD::MUL:
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001705 if (Subtarget->isThumb1Only())
Evan Cheng79d43262007-01-24 02:21:22 +00001706 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001707 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001708 unsigned RHSV = C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001709 if (!RHSV) break;
1710 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00001711 unsigned ShImm = Log2_32(RHSV-1);
1712 if (ShImm >= 32)
1713 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001714 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001715 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00001716 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1717 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00001718 if (Subtarget->isThumb()) {
Evan Chengaf9e7a72009-07-21 00:31:12 +00001719 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001720 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001721 } else {
1722 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001723 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001724 }
Evan Chenga8e29892007-01-19 07:51:42 +00001725 }
1726 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00001727 unsigned ShImm = Log2_32(RHSV+1);
1728 if (ShImm >= 32)
1729 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001730 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001731 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00001732 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1733 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00001734 if (Subtarget->isThumb()) {
Bob Wilson13ef8402010-05-28 00:27:15 +00001735 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1736 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001737 } else {
1738 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001739 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001740 }
Evan Chenga8e29892007-01-19 07:51:42 +00001741 }
1742 }
1743 break;
Evan Cheng20956592009-10-21 08:15:52 +00001744 case ISD::AND: {
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001745 // Check for unsigned bitfield extract
1746 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
1747 return I;
1748
Evan Cheng20956592009-10-21 08:15:52 +00001749 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
1750 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
1751 // are entirely contributed by c2 and lower 16-bits are entirely contributed
1752 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
1753 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001754 EVT VT = N->getValueType(0);
Evan Cheng20956592009-10-21 08:15:52 +00001755 if (VT != MVT::i32)
1756 break;
1757 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
1758 ? ARM::t2MOVTi16
1759 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
1760 if (!Opc)
1761 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001762 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Evan Cheng20956592009-10-21 08:15:52 +00001763 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1764 if (!N1C)
1765 break;
1766 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
1767 SDValue N2 = N0.getOperand(1);
1768 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1769 if (!N2C)
1770 break;
1771 unsigned N1CVal = N1C->getZExtValue();
1772 unsigned N2CVal = N2C->getZExtValue();
1773 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
1774 (N1CVal & 0xffffU) == 0xffffU &&
1775 (N2CVal & 0xffffU) == 0x0U) {
1776 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
1777 MVT::i32);
1778 SDValue Ops[] = { N0.getOperand(0), Imm16,
1779 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1780 return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
1781 }
1782 }
1783 break;
1784 }
Jim Grosbache5165492009-11-09 00:11:35 +00001785 case ARMISD::VMOVRRD:
1786 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001787 N->getOperand(0), getAL(CurDAG),
Dan Gohman602b0c82009-09-25 18:54:59 +00001788 CurDAG->getRegister(0, MVT::i32));
Dan Gohman525178c2007-10-08 18:33:35 +00001789 case ISD::UMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001790 if (Subtarget->isThumb1Only())
1791 break;
1792 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001793 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001794 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1795 CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00001796 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001797 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001798 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001799 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1800 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00001801 return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001802 }
Evan Chengee568cf2007-07-05 07:15:27 +00001803 }
Dan Gohman525178c2007-10-08 18:33:35 +00001804 case ISD::SMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001805 if (Subtarget->isThumb1Only())
1806 break;
1807 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001808 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00001810 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001811 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001812 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001813 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1814 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00001815 return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001816 }
Evan Chengee568cf2007-07-05 07:15:27 +00001817 }
Evan Chenga8e29892007-01-19 07:51:42 +00001818 case ISD::LOAD: {
Evan Chenge88d5ce2009-07-02 07:28:31 +00001819 SDNode *ResNode = 0;
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001820 if (Subtarget->isThumb() && Subtarget->hasThumb2())
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001821 ResNode = SelectT2IndexedLoad(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +00001822 else
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001823 ResNode = SelectARMIndexedLoad(N);
Evan Chengaf4550f2009-07-02 01:23:32 +00001824 if (ResNode)
1825 return ResNode;
Evan Chenga8e29892007-01-19 07:51:42 +00001826 // Other cases are autogenerated.
Rafael Espindolaf819a492006-11-09 13:58:55 +00001827 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001828 }
Evan Chengee568cf2007-07-05 07:15:27 +00001829 case ARMISD::BRCOND: {
1830 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1831 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1832 // Pattern complexity = 6 cost = 1 size = 0
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001833
Evan Chengee568cf2007-07-05 07:15:27 +00001834 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1835 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
1836 // Pattern complexity = 6 cost = 1 size = 0
1837
David Goodwin5e47a9a2009-06-30 18:04:13 +00001838 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1839 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1840 // Pattern complexity = 6 cost = 1 size = 0
1841
Jim Grosbach764ab522009-08-11 15:33:49 +00001842 unsigned Opc = Subtarget->isThumb() ?
David Goodwin5e47a9a2009-06-30 18:04:13 +00001843 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001844 SDValue Chain = N->getOperand(0);
1845 SDValue N1 = N->getOperand(1);
1846 SDValue N2 = N->getOperand(2);
1847 SDValue N3 = N->getOperand(3);
1848 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00001849 assert(N1.getOpcode() == ISD::BasicBlock);
1850 assert(N2.getOpcode() == ISD::Constant);
1851 assert(N3.getOpcode() == ISD::Register);
1852
Dan Gohman475871a2008-07-27 21:46:04 +00001853 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001854 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00001855 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001856 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
Dan Gohman602b0c82009-09-25 18:54:59 +00001857 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
1858 MVT::Flag, Ops, 5);
Dan Gohman475871a2008-07-27 21:46:04 +00001859 Chain = SDValue(ResNode, 0);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001860 if (N->getNumValues() == 2) {
Dan Gohman475871a2008-07-27 21:46:04 +00001861 InFlag = SDValue(ResNode, 1);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001862 ReplaceUses(SDValue(N, 1), InFlag);
Chris Lattnera47b9bc2008-02-03 03:20:59 +00001863 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001864 ReplaceUses(SDValue(N, 0),
Evan Chenged54de42009-11-19 08:16:50 +00001865 SDValue(Chain.getNode(), Chain.getResNo()));
Evan Chengee568cf2007-07-05 07:15:27 +00001866 return NULL;
1867 }
Evan Cheng07ba9062009-11-19 21:45:22 +00001868 case ARMISD::CMOV:
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001869 return SelectCMOVOp(N);
Evan Chengee568cf2007-07-05 07:15:27 +00001870 case ARMISD::CNEG: {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001871 EVT VT = N->getValueType(0);
1872 SDValue N0 = N->getOperand(0);
1873 SDValue N1 = N->getOperand(1);
1874 SDValue N2 = N->getOperand(2);
1875 SDValue N3 = N->getOperand(3);
1876 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00001877 assert(N2.getOpcode() == ISD::Constant);
1878 assert(N3.getOpcode() == ISD::Register);
1879
Dan Gohman475871a2008-07-27 21:46:04 +00001880 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001881 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00001882 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001883 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
Evan Chengee568cf2007-07-05 07:15:27 +00001884 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001885 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengee568cf2007-07-05 07:15:27 +00001886 default: assert(false && "Illegal conditional move type!");
1887 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001888 case MVT::f32:
Jim Grosbache5165492009-11-09 00:11:35 +00001889 Opc = ARM::VNEGScc;
Evan Chengee568cf2007-07-05 07:15:27 +00001890 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001891 case MVT::f64:
Jim Grosbache5165492009-11-09 00:11:35 +00001892 Opc = ARM::VNEGDcc;
Evan Chenge5ad88e2008-12-10 21:54:21 +00001893 break;
Evan Chengee568cf2007-07-05 07:15:27 +00001894 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001895 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00001896 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00001897
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001898 case ARMISD::VZIP: {
1899 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001900 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001901 switch (VT.getSimpleVT().SimpleTy) {
1902 default: return NULL;
1903 case MVT::v8i8: Opc = ARM::VZIPd8; break;
1904 case MVT::v4i16: Opc = ARM::VZIPd16; break;
1905 case MVT::v2f32:
1906 case MVT::v2i32: Opc = ARM::VZIPd32; break;
1907 case MVT::v16i8: Opc = ARM::VZIPq8; break;
1908 case MVT::v8i16: Opc = ARM::VZIPq16; break;
1909 case MVT::v4f32:
1910 case MVT::v4i32: Opc = ARM::VZIPq32; break;
1911 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001912 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001913 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1914 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1915 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001916 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001917 case ARMISD::VUZP: {
1918 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001919 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001920 switch (VT.getSimpleVT().SimpleTy) {
1921 default: return NULL;
1922 case MVT::v8i8: Opc = ARM::VUZPd8; break;
1923 case MVT::v4i16: Opc = ARM::VUZPd16; break;
1924 case MVT::v2f32:
1925 case MVT::v2i32: Opc = ARM::VUZPd32; break;
1926 case MVT::v16i8: Opc = ARM::VUZPq8; break;
1927 case MVT::v8i16: Opc = ARM::VUZPq16; break;
1928 case MVT::v4f32:
1929 case MVT::v4i32: Opc = ARM::VUZPq32; break;
1930 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001931 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001932 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1933 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1934 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001935 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001936 case ARMISD::VTRN: {
1937 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001938 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001939 switch (VT.getSimpleVT().SimpleTy) {
1940 default: return NULL;
1941 case MVT::v8i8: Opc = ARM::VTRNd8; break;
1942 case MVT::v4i16: Opc = ARM::VTRNd16; break;
1943 case MVT::v2f32:
1944 case MVT::v2i32: Opc = ARM::VTRNd32; break;
1945 case MVT::v16i8: Opc = ARM::VTRNq8; break;
1946 case MVT::v8i16: Opc = ARM::VTRNq16; break;
1947 case MVT::v4f32:
1948 case MVT::v4i32: Opc = ARM::VTRNq32; break;
1949 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001950 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001951 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1952 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1953 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001954 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001955 case ARMISD::BUILD_VECTOR: {
1956 EVT VecVT = N->getValueType(0);
1957 EVT EltVT = VecVT.getVectorElementType();
1958 unsigned NumElts = VecVT.getVectorNumElements();
1959 if (EltVT.getSimpleVT() == MVT::f64) {
1960 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
1961 return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
1962 }
1963 assert(EltVT.getSimpleVT() == MVT::f32 &&
1964 "unexpected type for BUILD_VECTOR");
1965 if (NumElts == 2)
1966 return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
1967 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
1968 return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
1969 N->getOperand(2), N->getOperand(3));
1970 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00001971
1972 case ISD::INTRINSIC_VOID:
1973 case ISD::INTRINSIC_W_CHAIN: {
1974 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Bob Wilson31fb12f2009-08-26 17:39:53 +00001975 switch (IntNo) {
1976 default:
Bob Wilson429009b2010-05-06 16:05:26 +00001977 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00001978
Bob Wilson621f1952010-03-23 05:25:43 +00001979 case Intrinsic::arm_neon_vld1: {
1980 unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
1981 ARM::VLD1d32, ARM::VLD1d64 };
Bob Wilsonffde0802010-09-02 16:00:54 +00001982 unsigned QOpcodes[] = { ARM::VLD1q8Pseudo, ARM::VLD1q16Pseudo,
1983 ARM::VLD1q32Pseudo, ARM::VLD1q64Pseudo };
Bob Wilson621f1952010-03-23 05:25:43 +00001984 return SelectVLD(N, 1, DOpcodes, QOpcodes, 0);
1985 }
1986
Bob Wilson31fb12f2009-08-26 17:39:53 +00001987 case Intrinsic::arm_neon_vld2: {
Bob Wilsonffde0802010-09-02 16:00:54 +00001988 unsigned DOpcodes[] = { ARM::VLD2d8Pseudo, ARM::VLD2d16Pseudo,
1989 ARM::VLD2d32Pseudo, ARM::VLD1q64Pseudo };
1990 unsigned QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
1991 ARM::VLD2q32Pseudo };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001992 return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00001993 }
1994
1995 case Intrinsic::arm_neon_vld3: {
Bob Wilsonf5721912010-09-03 18:16:02 +00001996 unsigned DOpcodes[] = { ARM::VLD3d8Pseudo, ARM::VLD3d16Pseudo,
1997 ARM::VLD3d32Pseudo, ARM::VLD1d64TPseudo };
1998 unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
1999 ARM::VLD3q16Pseudo_UPD,
2000 ARM::VLD3q32Pseudo_UPD };
2001 unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
2002 ARM::VLD3q16oddPseudo_UPD,
2003 ARM::VLD3q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002004 return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002005 }
2006
2007 case Intrinsic::arm_neon_vld4: {
Bob Wilsonf5721912010-09-03 18:16:02 +00002008 unsigned DOpcodes[] = { ARM::VLD4d8Pseudo, ARM::VLD4d16Pseudo,
2009 ARM::VLD4d32Pseudo, ARM::VLD1d64QPseudo };
2010 unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
2011 ARM::VLD4q16Pseudo_UPD,
2012 ARM::VLD4q32Pseudo_UPD };
2013 unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
2014 ARM::VLD4q16oddPseudo_UPD,
2015 ARM::VLD4q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002016 return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002017 }
2018
Bob Wilson243fcc52009-09-01 04:26:28 +00002019 case Intrinsic::arm_neon_vld2lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002020 unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo, ARM::VLD2LNd16Pseudo,
2021 ARM::VLD2LNd32Pseudo };
2022 unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo, ARM::VLD2LNq32Pseudo };
2023 return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002024 }
2025
2026 case Intrinsic::arm_neon_vld3lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002027 unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo, ARM::VLD3LNd16Pseudo,
2028 ARM::VLD3LNd32Pseudo };
2029 unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo, ARM::VLD3LNq32Pseudo };
2030 return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002031 }
2032
2033 case Intrinsic::arm_neon_vld4lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002034 unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo, ARM::VLD4LNd16Pseudo,
2035 ARM::VLD4LNd32Pseudo };
2036 unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo, ARM::VLD4LNq32Pseudo };
2037 return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002038 }
2039
Bob Wilson11d98992010-03-23 06:20:33 +00002040 case Intrinsic::arm_neon_vst1: {
2041 unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2042 ARM::VST1d32, ARM::VST1d64 };
Bob Wilsone5ce4f62010-08-28 05:12:57 +00002043 unsigned QOpcodes[] = { ARM::VST1q8Pseudo, ARM::VST1q16Pseudo,
2044 ARM::VST1q32Pseudo, ARM::VST1q64Pseudo };
Bob Wilson11d98992010-03-23 06:20:33 +00002045 return SelectVST(N, 1, DOpcodes, QOpcodes, 0);
2046 }
2047
Bob Wilson31fb12f2009-08-26 17:39:53 +00002048 case Intrinsic::arm_neon_vst2: {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00002049 unsigned DOpcodes[] = { ARM::VST2d8Pseudo, ARM::VST2d16Pseudo,
2050 ARM::VST2d32Pseudo, ARM::VST1q64Pseudo };
2051 unsigned QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
2052 ARM::VST2q32Pseudo };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002053 return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002054 }
2055
2056 case Intrinsic::arm_neon_vst3: {
Bob Wilson01ba4612010-08-26 18:51:29 +00002057 unsigned DOpcodes[] = { ARM::VST3d8Pseudo, ARM::VST3d16Pseudo,
2058 ARM::VST3d32Pseudo, ARM::VST1d64TPseudo };
2059 unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2060 ARM::VST3q16Pseudo_UPD,
2061 ARM::VST3q32Pseudo_UPD };
2062 unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2063 ARM::VST3q16oddPseudo_UPD,
2064 ARM::VST3q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002065 return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002066 }
2067
2068 case Intrinsic::arm_neon_vst4: {
Bob Wilson709d5922010-08-25 23:27:42 +00002069 unsigned DOpcodes[] = { ARM::VST4d8Pseudo, ARM::VST4d16Pseudo,
Bob Wilson70e48b22010-08-26 05:33:30 +00002070 ARM::VST4d32Pseudo, ARM::VST1d64QPseudo };
Bob Wilson709d5922010-08-25 23:27:42 +00002071 unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2072 ARM::VST4q16Pseudo_UPD,
2073 ARM::VST4q32Pseudo_UPD };
2074 unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2075 ARM::VST4q16oddPseudo_UPD,
2076 ARM::VST4q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002077 return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002078 }
Bob Wilson8a3198b2009-09-01 18:51:56 +00002079
2080 case Intrinsic::arm_neon_vst2lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002081 unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo, ARM::VST2LNd16Pseudo,
2082 ARM::VST2LNd32Pseudo };
2083 unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo, ARM::VST2LNq32Pseudo };
2084 return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002085 }
2086
2087 case Intrinsic::arm_neon_vst3lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002088 unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo, ARM::VST3LNd16Pseudo,
2089 ARM::VST3LNd32Pseudo };
2090 unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo, ARM::VST3LNq32Pseudo };
2091 return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002092 }
2093
2094 case Intrinsic::arm_neon_vst4lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002095 unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo, ARM::VST4LNd16Pseudo,
2096 ARM::VST4LNd32Pseudo };
2097 unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo, ARM::VST4LNq32Pseudo };
2098 return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002099 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00002100 }
Bob Wilson429009b2010-05-06 16:05:26 +00002101 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00002102 }
Evan Chengde8aa4e2010-05-05 18:28:36 +00002103
Bob Wilsond491d6e2010-07-06 23:36:25 +00002104 case ISD::INTRINSIC_WO_CHAIN: {
2105 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2106 switch (IntNo) {
2107 default:
2108 break;
2109
2110 case Intrinsic::arm_neon_vtbl2:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002111 return SelectVTBL(N, false, 2, ARM::VTBL2Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002112 case Intrinsic::arm_neon_vtbl3:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002113 return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002114 case Intrinsic::arm_neon_vtbl4:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002115 return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002116
2117 case Intrinsic::arm_neon_vtbx2:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002118 return SelectVTBL(N, true, 2, ARM::VTBX2Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002119 case Intrinsic::arm_neon_vtbx3:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002120 return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002121 case Intrinsic::arm_neon_vtbx4:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002122 return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002123 }
2124 break;
2125 }
2126
Bob Wilson429009b2010-05-06 16:05:26 +00002127 case ISD::CONCAT_VECTORS:
Evan Chengde8aa4e2010-05-05 18:28:36 +00002128 return SelectConcatVector(N);
2129 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00002130
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002131 return SelectCode(N);
Evan Chenga8e29892007-01-19 07:51:42 +00002132}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002133
Bob Wilson224c2442009-05-19 05:53:42 +00002134bool ARMDAGToDAGISel::
2135SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2136 std::vector<SDValue> &OutOps) {
2137 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
Bob Wilson765cc0b2009-10-13 20:50:28 +00002138 // Require the address to be in a register. That is safe for all ARM
2139 // variants and it is hard to do anything much smarter without knowing
2140 // how the operand is used.
2141 OutOps.push_back(Op);
Bob Wilson224c2442009-05-19 05:53:42 +00002142 return false;
2143}
2144
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002145/// createARMISelDag - This pass converts a legalized DAG into a
2146/// ARM-specific DAG, ready for instruction scheduling.
2147///
Bob Wilson522ce972009-09-28 14:30:20 +00002148FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2149 CodeGenOpt::Level OptLevel) {
2150 return new ARMDAGToDAGISel(TM, OptLevel);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002151}