blob: c81f950c012ace2371ddeb48263e9ea0079712b1 [file] [log] [blame]
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001//===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
Dale Johannesen51e28e62010-06-03 21:09:53 +000014#define DEBUG_TYPE "arm-isel"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000015#include "ARM.h"
Evan Chenge5ad88e2008-12-10 21:54:21 +000016#include "ARMAddressingModes.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000017#include "ARMTargetMachine.h"
Rafael Espindola84b19be2006-07-16 01:02:57 +000018#include "llvm/CallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000020#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/Intrinsics.h"
Owen Anderson9adc0ab2009-07-14 23:09:55 +000023#include "llvm/LLVMContext.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/SelectionDAG.h"
28#include "llvm/CodeGen/SelectionDAGISel.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000029#include "llvm/Target/TargetLowering.h"
Chris Lattner72939122007-05-03 00:32:00 +000030#include "llvm/Target/TargetOptions.h"
Evan Cheng94cc6d32010-05-04 20:39:49 +000031#include "llvm/Support/CommandLine.h"
Chris Lattner3d62d782008-02-03 05:43:57 +000032#include "llvm/Support/Compiler.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000033#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
36
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000037using namespace llvm;
38
Evan Chenga2c519b2010-07-30 23:33:54 +000039static cl::opt<bool>
40DisableShifterOp("disable-shifter-op", cl::Hidden,
41 cl::desc("Disable isel of shifter-op"),
42 cl::init(false));
43
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000044//===--------------------------------------------------------------------===//
45/// ARMDAGToDAGISel - ARM specific code to select ARM machine
46/// instructions for SelectionDAG operations.
47///
48namespace {
49class ARMDAGToDAGISel : public SelectionDAGISel {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000050 ARMBaseTargetMachine &TM;
Evan Cheng3f7eb8e2008-09-18 07:24:33 +000051
Evan Chenga8e29892007-01-19 07:51:42 +000052 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
53 /// make the right decision when generating code for different targets.
54 const ARMSubtarget *Subtarget;
55
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000056public:
Bob Wilson522ce972009-09-28 14:30:20 +000057 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm,
58 CodeGenOpt::Level OptLevel)
59 : SelectionDAGISel(tm, OptLevel), TM(tm),
Evan Chenga8e29892007-01-19 07:51:42 +000060 Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000061 }
62
Evan Chenga8e29892007-01-19 07:51:42 +000063 virtual const char *getPassName() const {
64 return "ARM Instruction Selection";
Anton Korobeynikov52237112009-06-17 18:13:58 +000065 }
66
Bob Wilsonaf4a8912009-10-08 18:51:31 +000067 /// getI32Imm - Return a target constant of type i32 with the specified
68 /// value.
Anton Korobeynikov52237112009-06-17 18:13:58 +000069 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +000070 return CurDAG->getTargetConstant(Imm, MVT::i32);
Anton Korobeynikov52237112009-06-17 18:13:58 +000071 }
72
Dan Gohmaneeb3a002010-01-05 01:24:18 +000073 SDNode *Select(SDNode *N);
Evan Cheng014bf212010-02-15 19:41:07 +000074
Chris Lattner52a261b2010-09-21 20:31:19 +000075 bool SelectShifterOperandReg(SDValue N, SDValue &A,
Evan Cheng055b0312009-06-29 07:51:04 +000076 SDValue &B, SDValue &C);
Chris Lattner52a261b2010-09-21 20:31:19 +000077 bool SelectAddrMode2(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000078 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000079 bool SelectAddrMode2Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +000080 SDValue &Offset, SDValue &Opc);
Chris Lattner52a261b2010-09-21 20:31:19 +000081 bool SelectAddrMode3(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000082 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000083 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +000084 SDValue &Offset, SDValue &Opc);
Chris Lattner52a261b2010-09-21 20:31:19 +000085 bool SelectAddrMode4(SDValue N, SDValue &Addr, SDValue &Mode);
86 bool SelectAddrMode5(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000087 SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +000088 bool SelectAddrMode6(SDValue N, SDValue &Addr, SDValue &Align);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000089
Chris Lattner52a261b2010-09-21 20:31:19 +000090 bool SelectAddrModePC(SDValue N, SDValue &Offset,
Bob Wilson8b024a52009-07-01 23:16:05 +000091 SDValue &Label);
Evan Chenga8e29892007-01-19 07:51:42 +000092
Chris Lattner52a261b2010-09-21 20:31:19 +000093 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
94 bool SelectThumbAddrModeRI5(SDValue N, unsigned Scale,
Dan Gohman475871a2008-07-27 21:46:04 +000095 SDValue &Base, SDValue &OffImm,
96 SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +000097 bool SelectThumbAddrModeS1(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000098 SDValue &OffImm, SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +000099 bool SelectThumbAddrModeS2(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000100 SDValue &OffImm, SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +0000101 bool SelectThumbAddrModeS4(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000102 SDValue &OffImm, SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +0000103 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000104
Chris Lattner52a261b2010-09-21 20:31:19 +0000105 bool SelectT2ShifterOperandReg(SDValue N,
Evan Cheng9cb9e672009-06-27 02:26:13 +0000106 SDValue &BaseReg, SDValue &Opc);
Chris Lattner52a261b2010-09-21 20:31:19 +0000107 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
108 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
Evan Cheng055b0312009-06-29 07:51:04 +0000109 SDValue &OffImm);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000110 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Chenge88d5ce2009-07-02 07:28:31 +0000111 SDValue &OffImm);
Chris Lattner52a261b2010-09-21 20:31:19 +0000112 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
Evan Cheng055b0312009-06-29 07:51:04 +0000113 SDValue &OffReg, SDValue &ShImm);
114
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +0000115 inline bool Pred_so_imm(SDNode *inN) const {
116 ConstantSDNode *N = cast<ConstantSDNode>(inN);
117 return ARM_AM::getSOImmVal(N->getZExtValue()) != -1;
118 }
119
120 inline bool Pred_t2_so_imm(SDNode *inN) const {
121 ConstantSDNode *N = cast<ConstantSDNode>(inN);
122 return ARM_AM::getT2SOImmVal(N->getZExtValue()) != -1;
123 }
124
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000125 // Include the pieces autogenerated from the target description.
126#include "ARMGenDAGISel.inc"
Bob Wilson224c2442009-05-19 05:53:42 +0000127
128private:
Evan Chenge88d5ce2009-07-02 07:28:31 +0000129 /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
130 /// ARM.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000131 SDNode *SelectARMIndexedLoad(SDNode *N);
132 SDNode *SelectT2IndexedLoad(SDNode *N);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000133
Bob Wilson621f1952010-03-23 05:25:43 +0000134 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
135 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson3e36f132009-10-14 17:28:52 +0000136 /// loads of D registers and even subregs and odd subregs of Q registers.
Bob Wilson621f1952010-03-23 05:25:43 +0000137 /// For NumVecs <= 2, QOpcodes1 is not used.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000138 SDNode *SelectVLD(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson3e36f132009-10-14 17:28:52 +0000139 unsigned *QOpcodes0, unsigned *QOpcodes1);
140
Bob Wilson24f995d2009-10-14 18:32:29 +0000141 /// SelectVST - Select NEON store intrinsics. NumVecs should
Bob Wilson11d98992010-03-23 06:20:33 +0000142 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson24f995d2009-10-14 18:32:29 +0000143 /// stores of D registers and even subregs and odd subregs of Q registers.
Bob Wilson11d98992010-03-23 06:20:33 +0000144 /// For NumVecs <= 2, QOpcodes1 is not used.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000145 SDNode *SelectVST(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson24f995d2009-10-14 18:32:29 +0000146 unsigned *QOpcodes0, unsigned *QOpcodes1);
147
Bob Wilson96493442009-10-14 16:46:45 +0000148 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
Bob Wilsona7c397c2009-10-14 16:19:03 +0000149 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson8466fa12010-09-13 23:01:35 +0000150 /// load/store of D registers and Q registers.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000151 SDNode *SelectVLDSTLane(SDNode *N, bool IsLoad, unsigned NumVecs,
Bob Wilson8466fa12010-09-13 23:01:35 +0000152 unsigned *DOpcodes, unsigned *QOpcodes);
Bob Wilsona7c397c2009-10-14 16:19:03 +0000153
Bob Wilson78dfbc32010-07-07 00:08:54 +0000154 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2,
155 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be
156 /// generated to force the table registers to be consecutive.
157 SDNode *SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
Bob Wilsond491d6e2010-07-06 23:36:25 +0000158
Sandeep Patel4e1ed882009-10-13 20:25:58 +0000159 /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
Jim Grosbach3a1287b2010-04-22 23:24:18 +0000160 SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
Sandeep Patel47eedaa2009-10-13 18:59:48 +0000161
Evan Cheng07ba9062009-11-19 21:45:22 +0000162 /// SelectCMOVOp - Select CMOV instructions for ARM.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000163 SDNode *SelectCMOVOp(SDNode *N);
164 SDNode *SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000165 ARMCC::CondCodes CCVal, SDValue CCR,
166 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000167 SDNode *SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000168 ARMCC::CondCodes CCVal, SDValue CCR,
169 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000170 SDNode *SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000171 ARMCC::CondCodes CCVal, SDValue CCR,
172 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000173 SDNode *SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000174 ARMCC::CondCodes CCVal, SDValue CCR,
175 SDValue InFlag);
Evan Cheng07ba9062009-11-19 21:45:22 +0000176
Evan Chengde8aa4e2010-05-05 18:28:36 +0000177 SDNode *SelectConcatVector(SDNode *N);
178
Evan Chengaf4550f2009-07-02 01:23:32 +0000179 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
180 /// inline asm expressions.
181 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
182 char ConstraintCode,
183 std::vector<SDValue> &OutOps);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000184
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000185 // Form pairs of consecutive S, D, or Q registers.
186 SDNode *PairSRegs(EVT VT, SDValue V0, SDValue V1);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000187 SDNode *PairDRegs(EVT VT, SDValue V0, SDValue V1);
Evan Cheng603afbf2010-05-10 17:34:18 +0000188 SDNode *PairQRegs(EVT VT, SDValue V0, SDValue V1);
189
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000190 // Form sequences of 4 consecutive S, D, or Q registers.
191 SDNode *QuadSRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Evan Cheng603afbf2010-05-10 17:34:18 +0000192 SDNode *QuadDRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Evan Cheng8f6de382010-05-16 03:27:48 +0000193 SDNode *QuadQRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000194};
Evan Chenga8e29892007-01-19 07:51:42 +0000195}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000196
Sandeep Patel47eedaa2009-10-13 18:59:48 +0000197/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
198/// operand. If so Imm will receive the 32-bit value.
199static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
200 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
201 Imm = cast<ConstantSDNode>(N)->getZExtValue();
202 return true;
203 }
204 return false;
205}
206
207// isInt32Immediate - This method tests to see if a constant operand.
208// If so Imm will receive the 32 bit value.
209static bool isInt32Immediate(SDValue N, unsigned &Imm) {
210 return isInt32Immediate(N.getNode(), Imm);
211}
212
213// isOpcWithIntImmediate - This method tests to see if the node is a specific
214// opcode and that it has a immediate integer right operand.
215// If so Imm will receive the 32 bit value.
216static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
217 return N->getOpcode() == Opc &&
218 isInt32Immediate(N->getOperand(1).getNode(), Imm);
219}
220
221
Chris Lattner52a261b2010-09-21 20:31:19 +0000222bool ARMDAGToDAGISel::SelectShifterOperandReg(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000223 SDValue &BaseReg,
224 SDValue &ShReg,
225 SDValue &Opc) {
Evan Chenga2c519b2010-07-30 23:33:54 +0000226 if (DisableShifterOp)
227 return false;
228
Evan Cheng055b0312009-06-29 07:51:04 +0000229 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
230
231 // Don't match base register only case. That is matched to a separate
232 // lower complexity pattern with explicit register operand.
233 if (ShOpcVal == ARM_AM::no_shift) return false;
Jim Grosbach764ab522009-08-11 15:33:49 +0000234
Evan Cheng055b0312009-06-29 07:51:04 +0000235 BaseReg = N.getOperand(0);
236 unsigned ShImmVal = 0;
237 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 ShReg = CurDAG->getRegister(0, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000239 ShImmVal = RHS->getZExtValue() & 31;
240 } else {
241 ShReg = N.getOperand(1);
242 }
243 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000245 return true;
246}
247
Chris Lattner52a261b2010-09-21 20:31:19 +0000248bool ARMDAGToDAGISel::SelectAddrMode2(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000249 SDValue &Base, SDValue &Offset,
250 SDValue &Opc) {
Evan Chenga13fd102007-03-13 21:05:54 +0000251 if (N.getOpcode() == ISD::MUL) {
252 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
253 // X * [3,5,9] -> X + X * [2,4,8] etc.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000254 int RHSC = (int)RHS->getZExtValue();
Evan Chenga13fd102007-03-13 21:05:54 +0000255 if (RHSC & 1) {
256 RHSC = RHSC & ~1;
257 ARM_AM::AddrOpc AddSub = ARM_AM::add;
258 if (RHSC < 0) {
259 AddSub = ARM_AM::sub;
260 RHSC = - RHSC;
261 }
262 if (isPowerOf2_32(RHSC)) {
263 unsigned ShAmt = Log2_32(RHSC);
264 Base = Offset = N.getOperand(0);
265 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
266 ARM_AM::lsl),
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 MVT::i32);
Evan Chenga13fd102007-03-13 21:05:54 +0000268 return true;
269 }
270 }
271 }
272 }
273
Evan Chenga8e29892007-01-19 07:51:42 +0000274 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
275 Base = N;
276 if (N.getOpcode() == ISD::FrameIndex) {
277 int FI = cast<FrameIndexSDNode>(N)->getIndex();
278 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000279 } else if (N.getOpcode() == ARMISD::Wrapper &&
280 !(Subtarget->useMovt() &&
281 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000282 Base = N.getOperand(0);
283 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000285 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
286 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000288 return true;
289 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000290
Evan Chenga8e29892007-01-19 07:51:42 +0000291 // Match simple R +/- imm12 operands.
292 if (N.getOpcode() == ISD::ADD)
293 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000294 int RHSC = (int)RHS->getZExtValue();
Evan Chenge966d642007-01-24 02:45:25 +0000295 if ((RHSC >= 0 && RHSC < 0x1000) ||
296 (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
Evan Chenga8e29892007-01-19 07:51:42 +0000297 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000298 if (Base.getOpcode() == ISD::FrameIndex) {
299 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
300 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
301 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000303
304 ARM_AM::AddrOpc AddSub = ARM_AM::add;
305 if (RHSC < 0) {
306 AddSub = ARM_AM::sub;
307 RHSC = - RHSC;
308 }
309 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
Evan Chenga8e29892007-01-19 07:51:42 +0000310 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000312 return true;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000313 }
Evan Chenga8e29892007-01-19 07:51:42 +0000314 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000315
Johnny Chen6a3b5ee2009-10-27 17:25:15 +0000316 // Otherwise this is R +/- [possibly shifted] R.
Evan Chenga8e29892007-01-19 07:51:42 +0000317 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
318 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
319 unsigned ShAmt = 0;
Jim Grosbach764ab522009-08-11 15:33:49 +0000320
Evan Chenga8e29892007-01-19 07:51:42 +0000321 Base = N.getOperand(0);
322 Offset = N.getOperand(1);
Jim Grosbach764ab522009-08-11 15:33:49 +0000323
Evan Chenga8e29892007-01-19 07:51:42 +0000324 if (ShOpcVal != ARM_AM::no_shift) {
325 // Check to see if the RHS of the shift is a constant, if not, we can't fold
326 // it.
327 if (ConstantSDNode *Sh =
328 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000329 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000330 Offset = N.getOperand(1).getOperand(0);
331 } else {
332 ShOpcVal = ARM_AM::no_shift;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000333 }
334 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000335
Evan Chenga8e29892007-01-19 07:51:42 +0000336 // Try matching (R shl C) + (R).
337 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
338 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
339 if (ShOpcVal != ARM_AM::no_shift) {
340 // Check to see if the RHS of the shift is a constant, if not, we can't
341 // fold it.
342 if (ConstantSDNode *Sh =
343 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000344 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000345 Offset = N.getOperand(0).getOperand(0);
346 Base = N.getOperand(1);
347 } else {
348 ShOpcVal = ARM_AM::no_shift;
349 }
350 }
351 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000352
Evan Chenga8e29892007-01-19 07:51:42 +0000353 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000355 return true;
356}
357
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000358bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000359 SDValue &Offset, SDValue &Opc) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000360 unsigned Opcode = Op->getOpcode();
Evan Chenga8e29892007-01-19 07:51:42 +0000361 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
362 ? cast<LoadSDNode>(Op)->getAddressingMode()
363 : cast<StoreSDNode>(Op)->getAddressingMode();
364 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
365 ? ARM_AM::add : ARM_AM::sub;
366 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000367 int Val = (int)C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000368 if (Val >= 0 && Val < 0x1000) { // 12 bits.
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000370 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
371 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000373 return true;
374 }
375 }
376
377 Offset = N;
378 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
379 unsigned ShAmt = 0;
380 if (ShOpcVal != ARM_AM::no_shift) {
381 // Check to see if the RHS of the shift is a constant, if not, we can't fold
382 // it.
383 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000384 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000385 Offset = N.getOperand(0);
386 } else {
387 ShOpcVal = ARM_AM::no_shift;
388 }
389 }
390
391 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000392 MVT::i32);
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000393 return true;
394}
395
Evan Chenga8e29892007-01-19 07:51:42 +0000396
Chris Lattner52a261b2010-09-21 20:31:19 +0000397bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000398 SDValue &Base, SDValue &Offset,
399 SDValue &Opc) {
Evan Chenga8e29892007-01-19 07:51:42 +0000400 if (N.getOpcode() == ISD::SUB) {
401 // X - C is canonicalize to X + -C, no need to handle it here.
402 Base = N.getOperand(0);
403 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000405 return true;
406 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000407
Evan Chenga8e29892007-01-19 07:51:42 +0000408 if (N.getOpcode() != ISD::ADD) {
409 Base = N;
410 if (N.getOpcode() == ISD::FrameIndex) {
411 int FI = cast<FrameIndexSDNode>(N)->getIndex();
412 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
413 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 Offset = CurDAG->getRegister(0, MVT::i32);
415 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000416 return true;
417 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000418
Evan Chenga8e29892007-01-19 07:51:42 +0000419 // If the RHS is +/- imm8, fold into addr mode.
420 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000421 int RHSC = (int)RHS->getZExtValue();
Evan Chenge966d642007-01-24 02:45:25 +0000422 if ((RHSC >= 0 && RHSC < 256) ||
423 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000424 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000425 if (Base.getOpcode() == ISD::FrameIndex) {
426 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
427 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
428 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000430
431 ARM_AM::AddrOpc AddSub = ARM_AM::add;
432 if (RHSC < 0) {
433 AddSub = ARM_AM::sub;
434 RHSC = - RHSC;
435 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000437 return true;
438 }
439 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000440
Evan Chenga8e29892007-01-19 07:51:42 +0000441 Base = N.getOperand(0);
442 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000444 return true;
445}
446
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000447bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000448 SDValue &Offset, SDValue &Opc) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000449 unsigned Opcode = Op->getOpcode();
Evan Chenga8e29892007-01-19 07:51:42 +0000450 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
451 ? cast<LoadSDNode>(Op)->getAddressingMode()
452 : cast<StoreSDNode>(Op)->getAddressingMode();
453 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
454 ? ARM_AM::add : ARM_AM::sub;
455 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000456 int Val = (int)C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000457 if (Val >= 0 && Val < 256) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 Offset = CurDAG->getRegister(0, MVT::i32);
459 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000460 return true;
461 }
462 }
463
464 Offset = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000466 return true;
467}
468
Chris Lattner52a261b2010-09-21 20:31:19 +0000469bool ARMDAGToDAGISel::SelectAddrMode4(SDValue N, SDValue &Addr, SDValue &Mode) {
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000470 Addr = N;
Bob Wilsonfd7fd942010-08-28 00:20:11 +0000471 Mode = CurDAG->getTargetConstant(ARM_AM::getAM4ModeImm(ARM_AM::ia), MVT::i32);
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000472 return true;
473}
Evan Chenga8e29892007-01-19 07:51:42 +0000474
Chris Lattner52a261b2010-09-21 20:31:19 +0000475bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000476 SDValue &Base, SDValue &Offset) {
Evan Chenga8e29892007-01-19 07:51:42 +0000477 if (N.getOpcode() != ISD::ADD) {
478 Base = N;
479 if (N.getOpcode() == ISD::FrameIndex) {
480 int FI = cast<FrameIndexSDNode>(N)->getIndex();
481 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000482 } else if (N.getOpcode() == ARMISD::Wrapper &&
483 !(Subtarget->useMovt() &&
484 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000485 Base = N.getOperand(0);
486 }
487 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000489 return true;
490 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000491
Evan Chenga8e29892007-01-19 07:51:42 +0000492 // If the RHS is +/- imm8, fold into addr mode.
493 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000494 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000495 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
496 RHSC >>= 2;
Evan Chenge966d642007-01-24 02:45:25 +0000497 if ((RHSC >= 0 && RHSC < 256) ||
498 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000499 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000500 if (Base.getOpcode() == ISD::FrameIndex) {
501 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
502 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
503 }
504
505 ARM_AM::AddrOpc AddSub = ARM_AM::add;
506 if (RHSC < 0) {
507 AddSub = ARM_AM::sub;
508 RHSC = - RHSC;
509 }
510 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
Owen Anderson825b72b2009-08-11 20:47:22 +0000511 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000512 return true;
513 }
514 }
515 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000516
Evan Chenga8e29892007-01-19 07:51:42 +0000517 Base = N;
518 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000520 return true;
521}
522
Chris Lattner52a261b2010-09-21 20:31:19 +0000523bool ARMDAGToDAGISel::SelectAddrMode6(SDValue N, SDValue &Addr, SDValue &Align){
Bob Wilson8b024a52009-07-01 23:16:05 +0000524 Addr = N;
Jim Grosbach8a5ec862009-11-07 21:25:39 +0000525 // Default to no alignment.
526 Align = CurDAG->getTargetConstant(0, MVT::i32);
Bob Wilson8b024a52009-07-01 23:16:05 +0000527 return true;
528}
529
Chris Lattner52a261b2010-09-21 20:31:19 +0000530bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
Evan Chengbba9f5f2009-08-14 19:01:37 +0000531 SDValue &Offset, SDValue &Label) {
Evan Chenga8e29892007-01-19 07:51:42 +0000532 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
533 Offset = N.getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000534 SDValue N1 = N.getOperand(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000535 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000537 return true;
538 }
539 return false;
540}
541
Chris Lattner52a261b2010-09-21 20:31:19 +0000542bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000543 SDValue &Base, SDValue &Offset){
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000544 // FIXME dl should come from the parent load or store, not the address
Evan Chengc38f2bc2007-01-23 22:59:13 +0000545 if (N.getOpcode() != ISD::ADD) {
Evan Cheng2f297df2009-07-11 07:08:13 +0000546 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
Dan Gohmane368b462010-06-18 14:22:04 +0000547 if (!NC || !NC->isNullValue())
Evan Cheng2f297df2009-07-11 07:08:13 +0000548 return false;
549
550 Base = Offset = N;
Evan Chengc38f2bc2007-01-23 22:59:13 +0000551 return true;
552 }
553
Evan Chenga8e29892007-01-19 07:51:42 +0000554 Base = N.getOperand(0);
555 Offset = N.getOperand(1);
556 return true;
557}
558
Evan Cheng79d43262007-01-24 02:21:22 +0000559bool
Chris Lattner52a261b2010-09-21 20:31:19 +0000560ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000561 unsigned Scale, SDValue &Base,
562 SDValue &OffImm, SDValue &Offset) {
Evan Cheng79d43262007-01-24 02:21:22 +0000563 if (Scale == 4) {
Dan Gohman475871a2008-07-27 21:46:04 +0000564 SDValue TmpBase, TmpOffImm;
Chris Lattner52a261b2010-09-21 20:31:19 +0000565 if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
Evan Cheng79d43262007-01-24 02:21:22 +0000566 return false; // We want to select tLDRspi / tSTRspi instead.
Evan Cheng012f2d92007-01-24 08:53:17 +0000567 if (N.getOpcode() == ARMISD::Wrapper &&
568 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
569 return false; // We want to select tLDRpci instead.
Evan Cheng79d43262007-01-24 02:21:22 +0000570 }
571
Evan Chenga8e29892007-01-19 07:51:42 +0000572 if (N.getOpcode() != ISD::ADD) {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000573 if (N.getOpcode() == ARMISD::Wrapper &&
574 !(Subtarget->useMovt() &&
575 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
576 Base = N.getOperand(0);
577 } else
578 Base = N;
579
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 Offset = CurDAG->getRegister(0, MVT::i32);
581 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000582 return true;
583 }
584
Evan Chengad0e4652007-02-06 00:22:06 +0000585 // Thumb does not have [sp, r] address mode.
586 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
587 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
588 if ((LHSR && LHSR->getReg() == ARM::SP) ||
589 (RHSR && RHSR->getReg() == ARM::SP)) {
590 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 Offset = CurDAG->getRegister(0, MVT::i32);
592 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengad0e4652007-02-06 00:22:06 +0000593 return true;
594 }
595
Evan Chenga8e29892007-01-19 07:51:42 +0000596 // If the RHS is + imm5 * scale, fold into addr mode.
597 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000598 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000599 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
600 RHSC /= Scale;
601 if (RHSC >= 0 && RHSC < 32) {
602 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 Offset = CurDAG->getRegister(0, MVT::i32);
604 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000605 return true;
606 }
607 }
608 }
609
Evan Chengc38f2bc2007-01-23 22:59:13 +0000610 Base = N.getOperand(0);
611 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000613 return true;
Evan Chenga8e29892007-01-19 07:51:42 +0000614}
615
Chris Lattner52a261b2010-09-21 20:31:19 +0000616bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000617 SDValue &Base, SDValue &OffImm,
618 SDValue &Offset) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000619 return SelectThumbAddrModeRI5(N, 1, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000620}
621
Chris Lattner52a261b2010-09-21 20:31:19 +0000622bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000623 SDValue &Base, SDValue &OffImm,
624 SDValue &Offset) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000625 return SelectThumbAddrModeRI5(N, 2, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000626}
627
Chris Lattner52a261b2010-09-21 20:31:19 +0000628bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000629 SDValue &Base, SDValue &OffImm,
630 SDValue &Offset) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000631 return SelectThumbAddrModeRI5(N, 4, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000632}
633
Chris Lattner52a261b2010-09-21 20:31:19 +0000634bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
635 SDValue &Base, SDValue &OffImm) {
Evan Chenga8e29892007-01-19 07:51:42 +0000636 if (N.getOpcode() == ISD::FrameIndex) {
637 int FI = cast<FrameIndexSDNode>(N)->getIndex();
638 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000640 return true;
641 }
Evan Cheng79d43262007-01-24 02:21:22 +0000642
Evan Chengad0e4652007-02-06 00:22:06 +0000643 if (N.getOpcode() != ISD::ADD)
644 return false;
645
646 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
Evan Cheng8c1a73a2007-02-06 09:11:20 +0000647 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
648 (LHSR && LHSR->getReg() == ARM::SP)) {
Evan Cheng79d43262007-01-24 02:21:22 +0000649 // If the RHS is + imm8 * scale, fold into addr mode.
650 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000651 int RHSC = (int)RHS->getZExtValue();
Evan Cheng79d43262007-01-24 02:21:22 +0000652 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
653 RHSC >>= 2;
654 if (RHSC >= 0 && RHSC < 256) {
Evan Chengad0e4652007-02-06 00:22:06 +0000655 Base = N.getOperand(0);
Evan Cheng8c1a73a2007-02-06 09:11:20 +0000656 if (Base.getOpcode() == ISD::FrameIndex) {
657 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
658 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
659 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000660 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Cheng79d43262007-01-24 02:21:22 +0000661 return true;
662 }
663 }
664 }
665 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000666
Evan Chenga8e29892007-01-19 07:51:42 +0000667 return false;
668}
669
Chris Lattner52a261b2010-09-21 20:31:19 +0000670bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDValue N, SDValue &BaseReg,
Evan Cheng9cb9e672009-06-27 02:26:13 +0000671 SDValue &Opc) {
Evan Chenga2c519b2010-07-30 23:33:54 +0000672 if (DisableShifterOp)
673 return false;
674
Evan Cheng9cb9e672009-06-27 02:26:13 +0000675 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
676
677 // Don't match base register only case. That is matched to a separate
678 // lower complexity pattern with explicit register operand.
679 if (ShOpcVal == ARM_AM::no_shift) return false;
680
681 BaseReg = N.getOperand(0);
682 unsigned ShImmVal = 0;
683 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
684 ShImmVal = RHS->getZExtValue() & 31;
685 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
686 return true;
687 }
688
689 return false;
690}
691
Chris Lattner52a261b2010-09-21 20:31:19 +0000692bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000693 SDValue &Base, SDValue &OffImm) {
694 // Match simple R + imm12 operands.
David Goodwin31e7eba2009-07-20 15:55:39 +0000695
Evan Cheng3a214252009-08-11 08:52:18 +0000696 // Base only.
697 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
David Goodwin31e7eba2009-07-20 15:55:39 +0000698 if (N.getOpcode() == ISD::FrameIndex) {
Evan Cheng3a214252009-08-11 08:52:18 +0000699 // Match frame index...
David Goodwin31e7eba2009-07-20 15:55:39 +0000700 int FI = cast<FrameIndexSDNode>(N)->getIndex();
701 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
David Goodwin31e7eba2009-07-20 15:55:39 +0000703 return true;
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000704 } else if (N.getOpcode() == ARMISD::Wrapper &&
705 !(Subtarget->useMovt() &&
706 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Cheng3a214252009-08-11 08:52:18 +0000707 Base = N.getOperand(0);
708 if (Base.getOpcode() == ISD::TargetConstantPool)
709 return false; // We want to select t2LDRpci instead.
710 } else
711 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000712 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng3a214252009-08-11 08:52:18 +0000713 return true;
David Goodwin31e7eba2009-07-20 15:55:39 +0000714 }
Evan Cheng055b0312009-06-29 07:51:04 +0000715
716 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000717 if (SelectT2AddrModeImm8(N, Base, OffImm))
Evan Cheng3a214252009-08-11 08:52:18 +0000718 // Let t2LDRi8 handle (R - imm8).
719 return false;
720
Evan Cheng055b0312009-06-29 07:51:04 +0000721 int RHSC = (int)RHS->getZExtValue();
David Goodwind8c95b52009-07-30 18:56:48 +0000722 if (N.getOpcode() == ISD::SUB)
723 RHSC = -RHSC;
724
725 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
Evan Cheng055b0312009-06-29 07:51:04 +0000726 Base = N.getOperand(0);
David Goodwind8c95b52009-07-30 18:56:48 +0000727 if (Base.getOpcode() == ISD::FrameIndex) {
728 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
729 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
730 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000732 return true;
733 }
734 }
735
Evan Cheng3a214252009-08-11 08:52:18 +0000736 // Base only.
737 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000738 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng3a214252009-08-11 08:52:18 +0000739 return true;
Evan Cheng055b0312009-06-29 07:51:04 +0000740}
741
Chris Lattner52a261b2010-09-21 20:31:19 +0000742bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000743 SDValue &Base, SDValue &OffImm) {
David Goodwind8c95b52009-07-30 18:56:48 +0000744 // Match simple R - imm8 operands.
Evan Cheng3a214252009-08-11 08:52:18 +0000745 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::SUB) {
David Goodwin07337c02009-07-30 22:45:52 +0000746 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
747 int RHSC = (int)RHS->getSExtValue();
748 if (N.getOpcode() == ISD::SUB)
749 RHSC = -RHSC;
Jim Grosbach764ab522009-08-11 15:33:49 +0000750
Evan Cheng3a214252009-08-11 08:52:18 +0000751 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
752 Base = N.getOperand(0);
David Goodwin07337c02009-07-30 22:45:52 +0000753 if (Base.getOpcode() == ISD::FrameIndex) {
754 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
755 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
756 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
David Goodwin07337c02009-07-30 22:45:52 +0000758 return true;
Evan Cheng055b0312009-06-29 07:51:04 +0000759 }
Evan Cheng055b0312009-06-29 07:51:04 +0000760 }
761 }
762
763 return false;
764}
765
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000766bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Chenge88d5ce2009-07-02 07:28:31 +0000767 SDValue &OffImm){
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000768 unsigned Opcode = Op->getOpcode();
Evan Chenge88d5ce2009-07-02 07:28:31 +0000769 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
770 ? cast<LoadSDNode>(Op)->getAddressingMode()
771 : cast<StoreSDNode>(Op)->getAddressingMode();
772 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N)) {
773 int RHSC = (int)RHS->getZExtValue();
774 if (RHSC >= 0 && RHSC < 0x100) { // 8 bits.
David Goodwin4cb73522009-07-14 21:29:29 +0000775 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
Owen Anderson825b72b2009-08-11 20:47:22 +0000776 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
777 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000778 return true;
779 }
780 }
781
782 return false;
783}
784
Chris Lattner52a261b2010-09-21 20:31:19 +0000785bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000786 SDValue &Base,
787 SDValue &OffReg, SDValue &ShImm) {
Evan Cheng3a214252009-08-11 08:52:18 +0000788 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
789 if (N.getOpcode() != ISD::ADD)
790 return false;
Evan Cheng055b0312009-06-29 07:51:04 +0000791
Evan Cheng3a214252009-08-11 08:52:18 +0000792 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
793 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
794 int RHSC = (int)RHS->getZExtValue();
795 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
796 return false;
797 else if (RHSC < 0 && RHSC >= -255) // 8 bits
David Goodwind8c95b52009-07-30 18:56:48 +0000798 return false;
799 }
800
Evan Cheng055b0312009-06-29 07:51:04 +0000801 // Look for (R + R) or (R + (R << [1,2,3])).
802 unsigned ShAmt = 0;
803 Base = N.getOperand(0);
804 OffReg = N.getOperand(1);
805
806 // Swap if it is ((R << c) + R).
807 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
808 if (ShOpcVal != ARM_AM::lsl) {
809 ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
810 if (ShOpcVal == ARM_AM::lsl)
811 std::swap(Base, OffReg);
Jim Grosbach764ab522009-08-11 15:33:49 +0000812 }
813
Evan Cheng055b0312009-06-29 07:51:04 +0000814 if (ShOpcVal == ARM_AM::lsl) {
815 // Check to see if the RHS of the shift is a constant, if not, we can't fold
816 // it.
817 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
818 ShAmt = Sh->getZExtValue();
819 if (ShAmt >= 4) {
820 ShAmt = 0;
821 ShOpcVal = ARM_AM::no_shift;
822 } else
823 OffReg = OffReg.getOperand(0);
824 } else {
825 ShOpcVal = ARM_AM::no_shift;
826 }
David Goodwin7ecc8502009-07-15 15:50:19 +0000827 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000828
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000830
831 return true;
832}
833
834//===--------------------------------------------------------------------===//
835
Evan Chengee568cf2007-07-05 07:15:27 +0000836/// getAL - Returns a ARMCC::AL immediate node.
Dan Gohman475871a2008-07-27 21:46:04 +0000837static inline SDValue getAL(SelectionDAG *CurDAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
Evan Cheng44bec522007-05-15 01:29:07 +0000839}
840
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000841SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
842 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengaf4550f2009-07-02 01:23:32 +0000843 ISD::MemIndexedMode AM = LD->getAddressingMode();
844 if (AM == ISD::UNINDEXED)
845 return NULL;
846
Owen Andersone50ed302009-08-10 22:56:29 +0000847 EVT LoadedVT = LD->getMemoryVT();
Evan Chengaf4550f2009-07-02 01:23:32 +0000848 SDValue Offset, AMOpc;
849 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
850 unsigned Opcode = 0;
851 bool Match = false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 if (LoadedVT == MVT::i32 &&
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000853 SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000854 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
855 Match = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 } else if (LoadedVT == MVT::i16 &&
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000857 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000858 Match = true;
859 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
860 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
861 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000863 if (LD->getExtensionType() == ISD::SEXTLOAD) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000864 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000865 Match = true;
866 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
867 }
868 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000869 if (SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000870 Match = true;
871 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
872 }
873 }
874 }
875
876 if (Match) {
877 SDValue Chain = LD->getChain();
878 SDValue Base = LD->getBasePtr();
879 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 CurDAG->getRegister(0, MVT::i32), Chain };
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000881 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +0000882 MVT::Other, Ops, 6);
Evan Chengaf4550f2009-07-02 01:23:32 +0000883 }
884
885 return NULL;
886}
887
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000888SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
889 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000890 ISD::MemIndexedMode AM = LD->getAddressingMode();
891 if (AM == ISD::UNINDEXED)
892 return NULL;
893
Owen Andersone50ed302009-08-10 22:56:29 +0000894 EVT LoadedVT = LD->getMemoryVT();
Evan Cheng4fbb9962009-07-02 23:16:11 +0000895 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000896 SDValue Offset;
897 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
898 unsigned Opcode = 0;
899 bool Match = false;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000900 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 switch (LoadedVT.getSimpleVT().SimpleTy) {
902 case MVT::i32:
Evan Chenge88d5ce2009-07-02 07:28:31 +0000903 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
904 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 case MVT::i16:
Evan Cheng4fbb9962009-07-02 23:16:11 +0000906 if (isSExtLd)
907 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
908 else
909 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000910 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000911 case MVT::i8:
912 case MVT::i1:
Evan Cheng4fbb9962009-07-02 23:16:11 +0000913 if (isSExtLd)
914 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
915 else
916 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000917 break;
918 default:
919 return NULL;
920 }
921 Match = true;
922 }
923
924 if (Match) {
925 SDValue Chain = LD->getChain();
926 SDValue Base = LD->getBasePtr();
927 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 CurDAG->getRegister(0, MVT::i32), Chain };
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000929 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +0000930 MVT::Other, Ops, 5);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000931 }
932
933 return NULL;
934}
935
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000936/// PairSRegs - Form a D register from a pair of S registers.
937///
938SDNode *ARMDAGToDAGISel::PairSRegs(EVT VT, SDValue V0, SDValue V1) {
939 DebugLoc dl = V0.getNode()->getDebugLoc();
940 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
941 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
Bob Wilson07f6e802010-06-16 21:34:01 +0000942 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
943 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000944}
945
Evan Cheng603afbf2010-05-10 17:34:18 +0000946/// PairDRegs - Form a quad register from a pair of D registers.
947///
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000948SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
949 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000950 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
951 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Bob Wilson07f6e802010-06-16 21:34:01 +0000952 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
953 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000954}
955
Evan Cheng7f687192010-05-14 00:21:45 +0000956/// PairQRegs - Form 4 consecutive D registers from a pair of Q registers.
Evan Cheng603afbf2010-05-10 17:34:18 +0000957///
958SDNode *ARMDAGToDAGISel::PairQRegs(EVT VT, SDValue V0, SDValue V1) {
959 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000960 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
961 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
Evan Cheng603afbf2010-05-10 17:34:18 +0000962 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
963 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
964}
965
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000966/// QuadSRegs - Form 4 consecutive S registers.
967///
968SDNode *ARMDAGToDAGISel::QuadSRegs(EVT VT, SDValue V0, SDValue V1,
969 SDValue V2, SDValue V3) {
970 DebugLoc dl = V0.getNode()->getDebugLoc();
971 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
972 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
973 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, MVT::i32);
974 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, MVT::i32);
975 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
976 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
977}
978
Evan Cheng7f687192010-05-14 00:21:45 +0000979/// QuadDRegs - Form 4 consecutive D registers.
Evan Cheng603afbf2010-05-10 17:34:18 +0000980///
981SDNode *ARMDAGToDAGISel::QuadDRegs(EVT VT, SDValue V0, SDValue V1,
982 SDValue V2, SDValue V3) {
983 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000984 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
985 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
986 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
987 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
Evan Cheng603afbf2010-05-10 17:34:18 +0000988 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
989 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
990}
991
Evan Cheng8f6de382010-05-16 03:27:48 +0000992/// QuadQRegs - Form 4 consecutive Q registers.
993///
994SDNode *ARMDAGToDAGISel::QuadQRegs(EVT VT, SDValue V0, SDValue V1,
995 SDValue V2, SDValue V3) {
996 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000997 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
998 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
999 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, MVT::i32);
1000 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, MVT::i32);
Evan Cheng8f6de382010-05-16 03:27:48 +00001001 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1002 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1003}
1004
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001005SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
Bob Wilson3e36f132009-10-14 17:28:52 +00001006 unsigned *DOpcodes, unsigned *QOpcodes0,
1007 unsigned *QOpcodes1) {
Bob Wilson621f1952010-03-23 05:25:43 +00001008 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
Bob Wilson3e36f132009-10-14 17:28:52 +00001009 DebugLoc dl = N->getDebugLoc();
1010
Bob Wilson226036e2010-03-20 22:13:40 +00001011 SDValue MemAddr, Align;
Chris Lattner52a261b2010-09-21 20:31:19 +00001012 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
Bob Wilson3e36f132009-10-14 17:28:52 +00001013 return NULL;
1014
1015 SDValue Chain = N->getOperand(0);
1016 EVT VT = N->getValueType(0);
1017 bool is64BitVector = VT.is64BitVector();
1018
Bob Wilson40ff01a2010-09-23 21:43:54 +00001019 // Set the alignment. The supported values depend on the number of
1020 // registers being loaded.
1021 unsigned NumRegs = NumVecs;
1022 if (!is64BitVector && NumVecs < 3)
1023 NumRegs *= 2;
1024 unsigned Alignment = cast<MemIntrinsicSDNode>(N)->getAlignment();
1025 if (Alignment >= 32 && NumRegs == 4)
1026 Alignment = 32;
1027 else if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1028 Alignment = 16;
1029 else if (Alignment >= 8)
1030 Alignment = 8;
1031 else
1032 Alignment = 0;
1033 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1034
Bob Wilson3e36f132009-10-14 17:28:52 +00001035 unsigned OpcodeIndex;
1036 switch (VT.getSimpleVT().SimpleTy) {
1037 default: llvm_unreachable("unhandled vld type");
1038 // Double-register operations:
1039 case MVT::v8i8: OpcodeIndex = 0; break;
1040 case MVT::v4i16: OpcodeIndex = 1; break;
1041 case MVT::v2f32:
1042 case MVT::v2i32: OpcodeIndex = 2; break;
1043 case MVT::v1i64: OpcodeIndex = 3; break;
1044 // Quad-register operations:
1045 case MVT::v16i8: OpcodeIndex = 0; break;
1046 case MVT::v8i16: OpcodeIndex = 1; break;
1047 case MVT::v4f32:
1048 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson621f1952010-03-23 05:25:43 +00001049 case MVT::v2i64: OpcodeIndex = 3;
Bob Wilson11d98992010-03-23 06:20:33 +00001050 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
Bob Wilson621f1952010-03-23 05:25:43 +00001051 break;
Bob Wilson3e36f132009-10-14 17:28:52 +00001052 }
1053
Bob Wilsonf5721912010-09-03 18:16:02 +00001054 EVT ResTy;
1055 if (NumVecs == 1)
1056 ResTy = VT;
1057 else {
1058 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1059 if (!is64BitVector)
1060 ResTyElts *= 2;
1061 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1062 }
1063
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001064 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001065 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilsonf5721912010-09-03 18:16:02 +00001066 SDValue SuperReg;
Bob Wilson3e36f132009-10-14 17:28:52 +00001067 if (is64BitVector) {
1068 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001069 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilsonf5721912010-09-03 18:16:02 +00001070 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
Bob Wilsonffde0802010-09-02 16:00:54 +00001071 if (NumVecs == 1)
Evan Chenge9e2ba02010-05-10 21:26:24 +00001072 return VLd;
1073
Bob Wilsonf5721912010-09-03 18:16:02 +00001074 SuperReg = SDValue(VLd, 0);
Jakob Stoklund Olesen7bb31e32010-05-24 17:13:28 +00001075 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
Evan Cheng5c6aba22010-05-14 18:54:59 +00001076 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001077 SDValue D = CurDAG->getTargetExtractSubreg(ARM::dsub_0+Vec,
Bob Wilsonffde0802010-09-02 16:00:54 +00001078 dl, VT, SuperReg);
Evan Cheng5c6aba22010-05-14 18:54:59 +00001079 ReplaceUses(SDValue(N, Vec), D);
Evan Chenge9e2ba02010-05-10 21:26:24 +00001080 }
Bob Wilsonf5721912010-09-03 18:16:02 +00001081 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
Evan Chenge9e2ba02010-05-10 21:26:24 +00001082 return NULL;
Bob Wilson3e36f132009-10-14 17:28:52 +00001083 }
1084
Bob Wilson621f1952010-03-23 05:25:43 +00001085 if (NumVecs <= 2) {
1086 // Quad registers are directly supported for VLD1 and VLD2,
1087 // loading pairs of D regs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001088 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001089 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilsonffde0802010-09-02 16:00:54 +00001090 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
Bob Wilsonffde0802010-09-02 16:00:54 +00001091 if (NumVecs == 1)
1092 return VLd;
1093
Bob Wilsonf5721912010-09-03 18:16:02 +00001094 SuperReg = SDValue(VLd, 0);
Bob Wilsonffde0802010-09-02 16:00:54 +00001095 Chain = SDValue(VLd, 1);
1096
Bob Wilson3e36f132009-10-14 17:28:52 +00001097 } else {
1098 // Otherwise, quad registers are loaded with two separate instructions,
1099 // where one loads the even registers and the other loads the odd registers.
Bob Wilsonf5721912010-09-03 18:16:02 +00001100 EVT AddrTy = MemAddr.getValueType();
Bob Wilson3e36f132009-10-14 17:28:52 +00001101
Bob Wilson24f995d2009-10-14 18:32:29 +00001102 // Load the even subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001103 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilsonf5721912010-09-03 18:16:02 +00001104 SDValue ImplDef =
1105 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1106 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1107 SDNode *VLdA =
1108 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsA, 7);
1109 Chain = SDValue(VLdA, 2);
Bob Wilson3e36f132009-10-14 17:28:52 +00001110
Bob Wilson24f995d2009-10-14 18:32:29 +00001111 // Load the odd subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001112 Opc = QOpcodes1[OpcodeIndex];
Bob Wilsonf5721912010-09-03 18:16:02 +00001113 const SDValue OpsB[] = { SDValue(VLdA, 1), Align, Reg0, SDValue(VLdA, 0),
1114 Pred, Reg0, Chain };
1115 SDNode *VLdB =
1116 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsB, 7);
1117 SuperReg = SDValue(VLdB, 0);
1118 Chain = SDValue(VLdB, 2);
1119 }
Bob Wilson3e36f132009-10-14 17:28:52 +00001120
Bob Wilsonf5721912010-09-03 18:16:02 +00001121 // Extract out the Q registers.
1122 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1123 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1124 SDValue Q = CurDAG->getTargetExtractSubreg(ARM::qsub_0+Vec,
1125 dl, VT, SuperReg);
1126 ReplaceUses(SDValue(N, Vec), Q);
Bob Wilson3e36f132009-10-14 17:28:52 +00001127 }
1128 ReplaceUses(SDValue(N, NumVecs), Chain);
1129 return NULL;
1130}
1131
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001132SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
Bob Wilson24f995d2009-10-14 18:32:29 +00001133 unsigned *DOpcodes, unsigned *QOpcodes0,
1134 unsigned *QOpcodes1) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001135 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
Bob Wilson24f995d2009-10-14 18:32:29 +00001136 DebugLoc dl = N->getDebugLoc();
1137
Bob Wilson226036e2010-03-20 22:13:40 +00001138 SDValue MemAddr, Align;
Chris Lattner52a261b2010-09-21 20:31:19 +00001139 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
Bob Wilson24f995d2009-10-14 18:32:29 +00001140 return NULL;
1141
1142 SDValue Chain = N->getOperand(0);
1143 EVT VT = N->getOperand(3).getValueType();
1144 bool is64BitVector = VT.is64BitVector();
1145
1146 unsigned OpcodeIndex;
1147 switch (VT.getSimpleVT().SimpleTy) {
1148 default: llvm_unreachable("unhandled vst type");
1149 // Double-register operations:
1150 case MVT::v8i8: OpcodeIndex = 0; break;
1151 case MVT::v4i16: OpcodeIndex = 1; break;
1152 case MVT::v2f32:
1153 case MVT::v2i32: OpcodeIndex = 2; break;
1154 case MVT::v1i64: OpcodeIndex = 3; break;
1155 // Quad-register operations:
1156 case MVT::v16i8: OpcodeIndex = 0; break;
1157 case MVT::v8i16: OpcodeIndex = 1; break;
1158 case MVT::v4f32:
1159 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson11d98992010-03-23 06:20:33 +00001160 case MVT::v2i64: OpcodeIndex = 3;
1161 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1162 break;
Bob Wilson24f995d2009-10-14 18:32:29 +00001163 }
1164
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001165 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001166 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001167
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001168 SmallVector<SDValue, 7> Ops;
Bob Wilson24f995d2009-10-14 18:32:29 +00001169 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001170 Ops.push_back(Align);
Bob Wilson24f995d2009-10-14 18:32:29 +00001171
1172 if (is64BitVector) {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001173 if (NumVecs == 1) {
1174 Ops.push_back(N->getOperand(3));
1175 } else {
Evan Cheng0ce537a2010-05-11 01:19:40 +00001176 SDValue RegSeq;
1177 SDValue V0 = N->getOperand(0+3);
1178 SDValue V1 = N->getOperand(1+3);
1179
1180 // Form a REG_SEQUENCE to force register allocation.
1181 if (NumVecs == 2)
1182 RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1183 else {
1184 SDValue V2 = N->getOperand(2+3);
1185 // If it's a vld3, form a quad D-register and leave the last part as
1186 // an undef.
1187 SDValue V3 = (NumVecs == 3)
1188 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1189 : N->getOperand(3+3);
1190 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1191 }
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001192 Ops.push_back(RegSeq);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001193 }
Evan Chengac0869d2009-11-21 06:21:52 +00001194 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001195 Ops.push_back(Reg0); // predicate register
Bob Wilson24f995d2009-10-14 18:32:29 +00001196 Ops.push_back(Chain);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001197 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001198 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
Bob Wilson24f995d2009-10-14 18:32:29 +00001199 }
1200
Bob Wilson11d98992010-03-23 06:20:33 +00001201 if (NumVecs <= 2) {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001202 // Quad registers are directly supported for VST1 and VST2.
Bob Wilson24f995d2009-10-14 18:32:29 +00001203 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001204 if (NumVecs == 1) {
1205 Ops.push_back(N->getOperand(3));
1206 } else {
1207 // Form a QQ register.
Evan Cheng603afbf2010-05-10 17:34:18 +00001208 SDValue Q0 = N->getOperand(3);
1209 SDValue Q1 = N->getOperand(4);
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001210 Ops.push_back(SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0));
Bob Wilson24f995d2009-10-14 18:32:29 +00001211 }
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001212 Ops.push_back(Pred);
1213 Ops.push_back(Reg0); // predicate register
1214 Ops.push_back(Chain);
1215 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
Bob Wilson24f995d2009-10-14 18:32:29 +00001216 }
1217
1218 // Otherwise, quad registers are stored with two separate instructions,
1219 // where one stores the even registers and the other stores the odd registers.
Evan Cheng7189fd02010-05-15 07:53:37 +00001220
Bob Wilson07f6e802010-06-16 21:34:01 +00001221 // Form the QQQQ REG_SEQUENCE.
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001222 SDValue V0 = N->getOperand(0+3);
1223 SDValue V1 = N->getOperand(1+3);
1224 SDValue V2 = N->getOperand(2+3);
1225 SDValue V3 = (NumVecs == 3)
1226 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1227 : N->getOperand(3+3);
1228 SDValue RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilson07f6e802010-06-16 21:34:01 +00001229
1230 // Store the even D registers.
Bob Wilson07f6e802010-06-16 21:34:01 +00001231 Ops.push_back(Reg0); // post-access address offset
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001232 Ops.push_back(RegSeq);
Bob Wilson07f6e802010-06-16 21:34:01 +00001233 Ops.push_back(Pred);
1234 Ops.push_back(Reg0); // predicate register
1235 Ops.push_back(Chain);
1236 unsigned Opc = QOpcodes0[OpcodeIndex];
1237 SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001238 MVT::Other, Ops.data(), 7);
Bob Wilson07f6e802010-06-16 21:34:01 +00001239 Chain = SDValue(VStA, 1);
1240
1241 // Store the odd D registers.
1242 Ops[0] = SDValue(VStA, 0); // MemAddr
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001243 Ops[6] = Chain;
Bob Wilson07f6e802010-06-16 21:34:01 +00001244 Opc = QOpcodes1[OpcodeIndex];
1245 SDNode *VStB = 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(VStB, 1);
1248 ReplaceUses(SDValue(N, 0), Chain);
1249 return NULL;
Bob Wilson24f995d2009-10-14 18:32:29 +00001250}
1251
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001252SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
Bob Wilson96493442009-10-14 16:46:45 +00001253 unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson8466fa12010-09-13 23:01:35 +00001254 unsigned *QOpcodes) {
Bob Wilson96493442009-10-14 16:46:45 +00001255 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001256 DebugLoc dl = N->getDebugLoc();
1257
Bob Wilson226036e2010-03-20 22:13:40 +00001258 SDValue MemAddr, Align;
Chris Lattner52a261b2010-09-21 20:31:19 +00001259 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
Bob Wilsona7c397c2009-10-14 16:19:03 +00001260 return NULL;
1261
1262 SDValue Chain = N->getOperand(0);
1263 unsigned Lane =
1264 cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
Bob Wilson96493442009-10-14 16:46:45 +00001265 EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
Bob Wilsona7c397c2009-10-14 16:19:03 +00001266 bool is64BitVector = VT.is64BitVector();
1267
Bob Wilsona7c397c2009-10-14 16:19:03 +00001268 unsigned OpcodeIndex;
1269 switch (VT.getSimpleVT().SimpleTy) {
Bob Wilson96493442009-10-14 16:46:45 +00001270 default: llvm_unreachable("unhandled vld/vst lane type");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001271 // Double-register operations:
1272 case MVT::v8i8: OpcodeIndex = 0; break;
1273 case MVT::v4i16: OpcodeIndex = 1; break;
1274 case MVT::v2f32:
1275 case MVT::v2i32: OpcodeIndex = 2; break;
1276 // Quad-register operations:
1277 case MVT::v8i16: OpcodeIndex = 0; break;
1278 case MVT::v4f32:
1279 case MVT::v4i32: OpcodeIndex = 1; break;
1280 }
1281
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001282 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001283 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001284
Bob Wilson8466fa12010-09-13 23:01:35 +00001285 SmallVector<SDValue, 7> Ops;
Bob Wilsona7c397c2009-10-14 16:19:03 +00001286 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001287 Ops.push_back(Align);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001288
Eric Christopher23da0b22010-09-14 08:31:25 +00001289 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1290 QOpcodes[OpcodeIndex]);
Bob Wilson07f6e802010-06-16 21:34:01 +00001291
Bob Wilson8466fa12010-09-13 23:01:35 +00001292 SDValue SuperReg;
1293 SDValue V0 = N->getOperand(0+3);
1294 SDValue V1 = N->getOperand(1+3);
1295 if (NumVecs == 2) {
1296 if (is64BitVector)
1297 SuperReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1298 else
1299 SuperReg = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001300 } else {
Bob Wilson8466fa12010-09-13 23:01:35 +00001301 SDValue V2 = N->getOperand(2+3);
1302 SDValue V3 = (NumVecs == 3)
1303 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1304 : N->getOperand(3+3);
1305 if (is64BitVector)
1306 SuperReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1307 else
1308 SuperReg = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001309 }
Bob Wilson8466fa12010-09-13 23:01:35 +00001310 Ops.push_back(SuperReg);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001311 Ops.push_back(getI32Imm(Lane));
Evan Chengac0869d2009-11-21 06:21:52 +00001312 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001313 Ops.push_back(Reg0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001314 Ops.push_back(Chain);
1315
Bob Wilson96493442009-10-14 16:46:45 +00001316 if (!IsLoad)
Bob Wilson8466fa12010-09-13 23:01:35 +00001317 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 7);
Bob Wilson96493442009-10-14 16:46:45 +00001318
Bob Wilson8466fa12010-09-13 23:01:35 +00001319 EVT ResTy;
1320 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1321 if (!is64BitVector)
1322 ResTyElts *= 2;
1323 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001324
Bob Wilson8466fa12010-09-13 23:01:35 +00001325 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other,
1326 Ops.data(), 7);
1327 SuperReg = SDValue(VLdLn, 0);
1328 Chain = SDValue(VLdLn, 1);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001329
Bob Wilson8466fa12010-09-13 23:01:35 +00001330 // Extract the subregisters.
Bob Wilson07f6e802010-06-16 21:34:01 +00001331 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1332 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1333 unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1334 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1335 ReplaceUses(SDValue(N, Vec),
Bob Wilson8466fa12010-09-13 23:01:35 +00001336 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1337 ReplaceUses(SDValue(N, NumVecs), Chain);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001338 return NULL;
1339}
1340
Bob Wilson78dfbc32010-07-07 00:08:54 +00001341SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1342 unsigned Opc) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001343 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1344 DebugLoc dl = N->getDebugLoc();
1345 EVT VT = N->getValueType(0);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001346 unsigned FirstTblReg = IsExt ? 2 : 1;
Bob Wilsond491d6e2010-07-06 23:36:25 +00001347
1348 // Form a REG_SEQUENCE to force register allocation.
1349 SDValue RegSeq;
Bob Wilson78dfbc32010-07-07 00:08:54 +00001350 SDValue V0 = N->getOperand(FirstTblReg + 0);
1351 SDValue V1 = N->getOperand(FirstTblReg + 1);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001352 if (NumVecs == 2)
1353 RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1354 else {
Bob Wilson78dfbc32010-07-07 00:08:54 +00001355 SDValue V2 = N->getOperand(FirstTblReg + 2);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001356 // If it's a vtbl3, form a quad D-register and leave the last part as
1357 // an undef.
1358 SDValue V3 = (NumVecs == 3)
1359 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
Bob Wilson78dfbc32010-07-07 00:08:54 +00001360 : N->getOperand(FirstTblReg + 3);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001361 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1362 }
1363
Bob Wilson78dfbc32010-07-07 00:08:54 +00001364 SmallVector<SDValue, 6> Ops;
1365 if (IsExt)
1366 Ops.push_back(N->getOperand(1));
Bob Wilsonbd916c52010-09-13 23:55:10 +00001367 Ops.push_back(RegSeq);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001368 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
Bob Wilsond491d6e2010-07-06 23:36:25 +00001369 Ops.push_back(getAL(CurDAG)); // predicate
1370 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
Bob Wilson78dfbc32010-07-07 00:08:54 +00001371 return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
Bob Wilsond491d6e2010-07-06 23:36:25 +00001372}
1373
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001374SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001375 bool isSigned) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001376 if (!Subtarget->hasV6T2Ops())
1377 return NULL;
Bob Wilson96493442009-10-14 16:46:45 +00001378
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001379 unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1380 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1381
1382
1383 // For unsigned extracts, check for a shift right and mask
1384 unsigned And_imm = 0;
1385 if (N->getOpcode() == ISD::AND) {
1386 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1387
1388 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1389 if (And_imm & (And_imm + 1))
1390 return NULL;
1391
1392 unsigned Srl_imm = 0;
1393 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1394 Srl_imm)) {
1395 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1396
1397 unsigned Width = CountTrailingOnes_32(And_imm);
1398 unsigned LSB = Srl_imm;
1399 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1400 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1401 CurDAG->getTargetConstant(LSB, MVT::i32),
1402 CurDAG->getTargetConstant(Width, MVT::i32),
1403 getAL(CurDAG), Reg0 };
1404 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1405 }
1406 }
1407 return NULL;
1408 }
1409
1410 // Otherwise, we're looking for a shift of a shift
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001411 unsigned Shl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001412 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001413 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1414 unsigned Srl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001415 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001416 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1417 unsigned Width = 32 - Srl_imm;
1418 int LSB = Srl_imm - Shl_imm;
Evan Cheng8000c6c2009-10-22 00:40:00 +00001419 if (LSB < 0)
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001420 return NULL;
1421 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001422 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001423 CurDAG->getTargetConstant(LSB, MVT::i32),
1424 CurDAG->getTargetConstant(Width, MVT::i32),
1425 getAL(CurDAG), Reg0 };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001426 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001427 }
1428 }
1429 return NULL;
1430}
1431
Evan Cheng9ef48352009-11-20 00:54:03 +00001432SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001433SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001434 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1435 SDValue CPTmp0;
1436 SDValue CPTmp1;
Chris Lattner52a261b2010-09-21 20:31:19 +00001437 if (SelectT2ShifterOperandReg(TrueVal, CPTmp0, CPTmp1)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001438 unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1439 unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1440 unsigned Opc = 0;
1441 switch (SOShOp) {
1442 case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1443 case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1444 case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1445 case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1446 default:
1447 llvm_unreachable("Unknown so_reg opcode!");
1448 break;
1449 }
1450 SDValue SOShImm =
1451 CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1452 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1453 SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001454 return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
Evan Cheng9ef48352009-11-20 00:54:03 +00001455 }
1456 return 0;
1457}
1458
1459SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001460SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001461 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1462 SDValue CPTmp0;
1463 SDValue CPTmp1;
1464 SDValue CPTmp2;
Chris Lattner52a261b2010-09-21 20:31:19 +00001465 if (SelectShifterOperandReg(TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001466 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1467 SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001468 return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
Evan Cheng9ef48352009-11-20 00:54:03 +00001469 }
1470 return 0;
1471}
1472
1473SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001474SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001475 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1476 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1477 if (!T)
1478 return 0;
1479
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001480 if (Pred_t2_so_imm(TrueVal.getNode())) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001481 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1482 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1483 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001484 return CurDAG->SelectNodeTo(N,
Evan Cheng9ef48352009-11-20 00:54:03 +00001485 ARM::t2MOVCCi, MVT::i32, Ops, 5);
1486 }
1487 return 0;
1488}
1489
1490SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001491SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001492 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1493 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1494 if (!T)
1495 return 0;
1496
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001497 if (Pred_so_imm(TrueVal.getNode())) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001498 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1499 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1500 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001501 return CurDAG->SelectNodeTo(N,
Evan Cheng9ef48352009-11-20 00:54:03 +00001502 ARM::MOVCCi, MVT::i32, Ops, 5);
1503 }
1504 return 0;
1505}
1506
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001507SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
1508 EVT VT = N->getValueType(0);
1509 SDValue FalseVal = N->getOperand(0);
1510 SDValue TrueVal = N->getOperand(1);
1511 SDValue CC = N->getOperand(2);
1512 SDValue CCR = N->getOperand(3);
1513 SDValue InFlag = N->getOperand(4);
Evan Cheng9ef48352009-11-20 00:54:03 +00001514 assert(CC.getOpcode() == ISD::Constant);
1515 assert(CCR.getOpcode() == ISD::Register);
1516 ARMCC::CondCodes CCVal =
1517 (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
Evan Cheng07ba9062009-11-19 21:45:22 +00001518
1519 if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1520 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1521 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1522 // Pattern complexity = 18 cost = 1 size = 0
1523 SDValue CPTmp0;
1524 SDValue CPTmp1;
1525 SDValue CPTmp2;
1526 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001527 SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001528 CCVal, CCR, InFlag);
1529 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001530 Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001531 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1532 if (Res)
1533 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001534 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001535 SDNode *Res = SelectARMCMOVShiftOp(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 = SelectARMCMOVShiftOp(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 }
1543
1544 // Pattern: (ARMcmov:i32 GPR:i32:$false,
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001545 // (imm:i32)<<P:Pred_so_imm>>:$true,
Evan Cheng07ba9062009-11-19 21:45:22 +00001546 // (imm:i32):$cc)
1547 // Emits: (MOVCCi:i32 GPR:i32:$false,
1548 // (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1549 // Pattern complexity = 10 cost = 1 size = 0
Evan Cheng9ef48352009-11-20 00:54:03 +00001550 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001551 SDNode *Res = SelectT2CMOVSoImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001552 CCVal, CCR, InFlag);
1553 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001554 Res = SelectT2CMOVSoImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001555 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1556 if (Res)
1557 return Res;
1558 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001559 SDNode *Res = SelectARMCMOVSoImmOp(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 = SelectARMCMOVSoImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001563 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1564 if (Res)
1565 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001566 }
1567 }
1568
1569 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1570 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1571 // Pattern complexity = 6 cost = 1 size = 0
1572 //
1573 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1574 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1575 // Pattern complexity = 6 cost = 11 size = 0
1576 //
1577 // Also FCPYScc and FCPYDcc.
Evan Cheng9ef48352009-11-20 00:54:03 +00001578 SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1579 SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
Evan Cheng07ba9062009-11-19 21:45:22 +00001580 unsigned Opc = 0;
1581 switch (VT.getSimpleVT().SimpleTy) {
1582 default: assert(false && "Illegal conditional move type!");
1583 break;
1584 case MVT::i32:
1585 Opc = Subtarget->isThumb()
1586 ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1587 : ARM::MOVCCr;
1588 break;
1589 case MVT::f32:
1590 Opc = ARM::VMOVScc;
1591 break;
1592 case MVT::f64:
1593 Opc = ARM::VMOVDcc;
1594 break;
1595 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001596 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Cheng07ba9062009-11-19 21:45:22 +00001597}
1598
Evan Chengde8aa4e2010-05-05 18:28:36 +00001599SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
1600 // The only time a CONCAT_VECTORS operation can have legal types is when
1601 // two 64-bit vectors are concatenated to a 128-bit vector.
1602 EVT VT = N->getValueType(0);
1603 if (!VT.is128BitVector() || N->getNumOperands() != 2)
1604 llvm_unreachable("unexpected CONCAT_VECTORS");
1605 DebugLoc dl = N->getDebugLoc();
1606 SDValue V0 = N->getOperand(0);
1607 SDValue V1 = N->getOperand(1);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001608 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1609 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Evan Chengde8aa4e2010-05-05 18:28:36 +00001610 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1611 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1612}
1613
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001614SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
Dale Johannesened2eee62009-02-06 01:31:28 +00001615 DebugLoc dl = N->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001616
Dan Gohmane8be6c62008-07-17 19:10:17 +00001617 if (N->isMachineOpcode())
Evan Chenga8e29892007-01-19 07:51:42 +00001618 return NULL; // Already selected.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001619
1620 switch (N->getOpcode()) {
Evan Chenga8e29892007-01-19 07:51:42 +00001621 default: break;
1622 case ISD::Constant: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001623 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001624 bool UseCP = true;
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00001625 if (Subtarget->hasThumb2())
1626 // Thumb2-aware targets have the MOVT instruction, so all immediates can
1627 // be done with MOV + MOVT, at worst.
1628 UseCP = 0;
1629 else {
1630 if (Subtarget->isThumb()) {
Bob Wilsone64e3cf2009-06-22 17:29:13 +00001631 UseCP = (Val > 255 && // MOV
1632 ~Val > 255 && // MOV + MVN
1633 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00001634 } else
1635 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
1636 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
1637 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
1638 }
1639
Evan Chenga8e29892007-01-19 07:51:42 +00001640 if (UseCP) {
Dan Gohman475871a2008-07-27 21:46:04 +00001641 SDValue CPIdx =
Owen Anderson1d0be152009-08-13 21:58:54 +00001642 CurDAG->getTargetConstantPool(ConstantInt::get(
1643 Type::getInt32Ty(*CurDAG->getContext()), Val),
Evan Chenga8e29892007-01-19 07:51:42 +00001644 TLI.getPointerTy());
Evan Cheng012f2d92007-01-24 08:53:17 +00001645
1646 SDNode *ResNode;
Evan Cheng446c4282009-07-11 06:43:01 +00001647 if (Subtarget->isThumb1Only()) {
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001648 SDValue Pred = getAL(CurDAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00001649 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
Evan Cheng446c4282009-07-11 06:43:01 +00001650 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
Dan Gohman602b0c82009-09-25 18:54:59 +00001651 ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
1652 Ops, 4);
Evan Cheng446c4282009-07-11 06:43:01 +00001653 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001654 SDValue Ops[] = {
Jim Grosbach764ab522009-08-11 15:33:49 +00001655 CPIdx,
Owen Anderson825b72b2009-08-11 20:47:22 +00001656 CurDAG->getRegister(0, MVT::i32),
1657 CurDAG->getTargetConstant(0, MVT::i32),
Evan Chengee568cf2007-07-05 07:15:27 +00001658 getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +00001659 CurDAG->getRegister(0, MVT::i32),
Evan Cheng012f2d92007-01-24 08:53:17 +00001660 CurDAG->getEntryNode()
1661 };
Dan Gohman602b0c82009-09-25 18:54:59 +00001662 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
1663 Ops, 6);
Evan Cheng012f2d92007-01-24 08:53:17 +00001664 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001665 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
Evan Chenga8e29892007-01-19 07:51:42 +00001666 return NULL;
1667 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001668
Evan Chenga8e29892007-01-19 07:51:42 +00001669 // Other cases are autogenerated.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001670 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001671 }
Rafael Espindolaf819a492006-11-09 13:58:55 +00001672 case ISD::FrameIndex: {
Evan Chenga8e29892007-01-19 07:51:42 +00001673 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindolaf819a492006-11-09 13:58:55 +00001674 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohman475871a2008-07-27 21:46:04 +00001675 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
David Goodwinf1daf7d2009-07-08 23:10:31 +00001676 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001677 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
1678 CurDAG->getTargetConstant(0, MVT::i32));
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001679 } else {
David Goodwin419c6152009-07-14 18:48:51 +00001680 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
1681 ARM::t2ADDri : ARM::ADDri);
Owen Anderson825b72b2009-08-11 20:47:22 +00001682 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
1683 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1684 CurDAG->getRegister(0, MVT::i32) };
1685 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00001686 }
Evan Chenga8e29892007-01-19 07:51:42 +00001687 }
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001688 case ISD::SRL:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001689 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001690 return I;
1691 break;
1692 case ISD::SRA:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001693 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001694 return I;
1695 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001696 case ISD::MUL:
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001697 if (Subtarget->isThumb1Only())
Evan Cheng79d43262007-01-24 02:21:22 +00001698 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001699 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001700 unsigned RHSV = C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001701 if (!RHSV) break;
1702 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00001703 unsigned ShImm = Log2_32(RHSV-1);
1704 if (ShImm >= 32)
1705 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001706 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001707 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1709 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00001710 if (Subtarget->isThumb()) {
Evan Chengaf9e7a72009-07-21 00:31:12 +00001711 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001713 } else {
1714 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001715 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001716 }
Evan Chenga8e29892007-01-19 07:51:42 +00001717 }
1718 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00001719 unsigned ShImm = Log2_32(RHSV+1);
1720 if (ShImm >= 32)
1721 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001722 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001723 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00001724 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1725 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00001726 if (Subtarget->isThumb()) {
Bob Wilson13ef8402010-05-28 00:27:15 +00001727 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1728 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001729 } else {
1730 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001731 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001732 }
Evan Chenga8e29892007-01-19 07:51:42 +00001733 }
1734 }
1735 break;
Evan Cheng20956592009-10-21 08:15:52 +00001736 case ISD::AND: {
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001737 // Check for unsigned bitfield extract
1738 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
1739 return I;
1740
Evan Cheng20956592009-10-21 08:15:52 +00001741 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
1742 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
1743 // are entirely contributed by c2 and lower 16-bits are entirely contributed
1744 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
1745 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001746 EVT VT = N->getValueType(0);
Evan Cheng20956592009-10-21 08:15:52 +00001747 if (VT != MVT::i32)
1748 break;
1749 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
1750 ? ARM::t2MOVTi16
1751 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
1752 if (!Opc)
1753 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001754 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Evan Cheng20956592009-10-21 08:15:52 +00001755 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1756 if (!N1C)
1757 break;
1758 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
1759 SDValue N2 = N0.getOperand(1);
1760 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1761 if (!N2C)
1762 break;
1763 unsigned N1CVal = N1C->getZExtValue();
1764 unsigned N2CVal = N2C->getZExtValue();
1765 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
1766 (N1CVal & 0xffffU) == 0xffffU &&
1767 (N2CVal & 0xffffU) == 0x0U) {
1768 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
1769 MVT::i32);
1770 SDValue Ops[] = { N0.getOperand(0), Imm16,
1771 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1772 return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
1773 }
1774 }
1775 break;
1776 }
Jim Grosbache5165492009-11-09 00:11:35 +00001777 case ARMISD::VMOVRRD:
1778 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001779 N->getOperand(0), getAL(CurDAG),
Dan Gohman602b0c82009-09-25 18:54:59 +00001780 CurDAG->getRegister(0, MVT::i32));
Dan Gohman525178c2007-10-08 18:33:35 +00001781 case ISD::UMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001782 if (Subtarget->isThumb1Only())
1783 break;
1784 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001785 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001786 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1787 CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00001788 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001789 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001790 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001791 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1792 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00001793 return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001794 }
Evan Chengee568cf2007-07-05 07:15:27 +00001795 }
Dan Gohman525178c2007-10-08 18:33:35 +00001796 case ISD::SMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001797 if (Subtarget->isThumb1Only())
1798 break;
1799 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001800 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001801 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00001802 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001803 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001804 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001805 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1806 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00001807 return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001808 }
Evan Chengee568cf2007-07-05 07:15:27 +00001809 }
Evan Chenga8e29892007-01-19 07:51:42 +00001810 case ISD::LOAD: {
Evan Chenge88d5ce2009-07-02 07:28:31 +00001811 SDNode *ResNode = 0;
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001812 if (Subtarget->isThumb() && Subtarget->hasThumb2())
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001813 ResNode = SelectT2IndexedLoad(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +00001814 else
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001815 ResNode = SelectARMIndexedLoad(N);
Evan Chengaf4550f2009-07-02 01:23:32 +00001816 if (ResNode)
1817 return ResNode;
Evan Chenga8e29892007-01-19 07:51:42 +00001818 // Other cases are autogenerated.
Rafael Espindolaf819a492006-11-09 13:58:55 +00001819 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001820 }
Evan Chengee568cf2007-07-05 07:15:27 +00001821 case ARMISD::BRCOND: {
1822 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1823 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1824 // Pattern complexity = 6 cost = 1 size = 0
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001825
Evan Chengee568cf2007-07-05 07:15:27 +00001826 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1827 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
1828 // Pattern complexity = 6 cost = 1 size = 0
1829
David Goodwin5e47a9a2009-06-30 18:04:13 +00001830 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1831 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1832 // Pattern complexity = 6 cost = 1 size = 0
1833
Jim Grosbach764ab522009-08-11 15:33:49 +00001834 unsigned Opc = Subtarget->isThumb() ?
David Goodwin5e47a9a2009-06-30 18:04:13 +00001835 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001836 SDValue Chain = N->getOperand(0);
1837 SDValue N1 = N->getOperand(1);
1838 SDValue N2 = N->getOperand(2);
1839 SDValue N3 = N->getOperand(3);
1840 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00001841 assert(N1.getOpcode() == ISD::BasicBlock);
1842 assert(N2.getOpcode() == ISD::Constant);
1843 assert(N3.getOpcode() == ISD::Register);
1844
Dan Gohman475871a2008-07-27 21:46:04 +00001845 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001846 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00001847 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001848 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
Dan Gohman602b0c82009-09-25 18:54:59 +00001849 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
1850 MVT::Flag, Ops, 5);
Dan Gohman475871a2008-07-27 21:46:04 +00001851 Chain = SDValue(ResNode, 0);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001852 if (N->getNumValues() == 2) {
Dan Gohman475871a2008-07-27 21:46:04 +00001853 InFlag = SDValue(ResNode, 1);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001854 ReplaceUses(SDValue(N, 1), InFlag);
Chris Lattnera47b9bc2008-02-03 03:20:59 +00001855 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001856 ReplaceUses(SDValue(N, 0),
Evan Chenged54de42009-11-19 08:16:50 +00001857 SDValue(Chain.getNode(), Chain.getResNo()));
Evan Chengee568cf2007-07-05 07:15:27 +00001858 return NULL;
1859 }
Evan Cheng07ba9062009-11-19 21:45:22 +00001860 case ARMISD::CMOV:
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001861 return SelectCMOVOp(N);
Evan Chengee568cf2007-07-05 07:15:27 +00001862 case ARMISD::CNEG: {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001863 EVT VT = N->getValueType(0);
1864 SDValue N0 = N->getOperand(0);
1865 SDValue N1 = N->getOperand(1);
1866 SDValue N2 = N->getOperand(2);
1867 SDValue N3 = N->getOperand(3);
1868 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00001869 assert(N2.getOpcode() == ISD::Constant);
1870 assert(N3.getOpcode() == ISD::Register);
1871
Dan Gohman475871a2008-07-27 21:46:04 +00001872 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001873 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00001874 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001875 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
Evan Chengee568cf2007-07-05 07:15:27 +00001876 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001877 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengee568cf2007-07-05 07:15:27 +00001878 default: assert(false && "Illegal conditional move type!");
1879 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001880 case MVT::f32:
Jim Grosbache5165492009-11-09 00:11:35 +00001881 Opc = ARM::VNEGScc;
Evan Chengee568cf2007-07-05 07:15:27 +00001882 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001883 case MVT::f64:
Jim Grosbache5165492009-11-09 00:11:35 +00001884 Opc = ARM::VNEGDcc;
Evan Chenge5ad88e2008-12-10 21:54:21 +00001885 break;
Evan Chengee568cf2007-07-05 07:15:27 +00001886 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001887 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00001888 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00001889
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001890 case ARMISD::VZIP: {
1891 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001892 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001893 switch (VT.getSimpleVT().SimpleTy) {
1894 default: return NULL;
1895 case MVT::v8i8: Opc = ARM::VZIPd8; break;
1896 case MVT::v4i16: Opc = ARM::VZIPd16; break;
1897 case MVT::v2f32:
1898 case MVT::v2i32: Opc = ARM::VZIPd32; break;
1899 case MVT::v16i8: Opc = ARM::VZIPq8; break;
1900 case MVT::v8i16: Opc = ARM::VZIPq16; break;
1901 case MVT::v4f32:
1902 case MVT::v4i32: Opc = ARM::VZIPq32; break;
1903 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001904 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001905 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1906 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1907 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001908 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001909 case ARMISD::VUZP: {
1910 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001911 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001912 switch (VT.getSimpleVT().SimpleTy) {
1913 default: return NULL;
1914 case MVT::v8i8: Opc = ARM::VUZPd8; break;
1915 case MVT::v4i16: Opc = ARM::VUZPd16; break;
1916 case MVT::v2f32:
1917 case MVT::v2i32: Opc = ARM::VUZPd32; break;
1918 case MVT::v16i8: Opc = ARM::VUZPq8; break;
1919 case MVT::v8i16: Opc = ARM::VUZPq16; break;
1920 case MVT::v4f32:
1921 case MVT::v4i32: Opc = ARM::VUZPq32; break;
1922 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001923 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001924 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1925 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1926 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001927 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001928 case ARMISD::VTRN: {
1929 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001930 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001931 switch (VT.getSimpleVT().SimpleTy) {
1932 default: return NULL;
1933 case MVT::v8i8: Opc = ARM::VTRNd8; break;
1934 case MVT::v4i16: Opc = ARM::VTRNd16; break;
1935 case MVT::v2f32:
1936 case MVT::v2i32: Opc = ARM::VTRNd32; break;
1937 case MVT::v16i8: Opc = ARM::VTRNq8; break;
1938 case MVT::v8i16: Opc = ARM::VTRNq16; break;
1939 case MVT::v4f32:
1940 case MVT::v4i32: Opc = ARM::VTRNq32; break;
1941 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001942 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001943 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1944 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1945 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001946 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001947 case ARMISD::BUILD_VECTOR: {
1948 EVT VecVT = N->getValueType(0);
1949 EVT EltVT = VecVT.getVectorElementType();
1950 unsigned NumElts = VecVT.getVectorNumElements();
1951 if (EltVT.getSimpleVT() == MVT::f64) {
1952 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
1953 return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
1954 }
1955 assert(EltVT.getSimpleVT() == MVT::f32 &&
1956 "unexpected type for BUILD_VECTOR");
1957 if (NumElts == 2)
1958 return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
1959 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
1960 return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
1961 N->getOperand(2), N->getOperand(3));
1962 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00001963
1964 case ISD::INTRINSIC_VOID:
1965 case ISD::INTRINSIC_W_CHAIN: {
1966 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Bob Wilson31fb12f2009-08-26 17:39:53 +00001967 switch (IntNo) {
1968 default:
Bob Wilson429009b2010-05-06 16:05:26 +00001969 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00001970
Bob Wilson621f1952010-03-23 05:25:43 +00001971 case Intrinsic::arm_neon_vld1: {
1972 unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
1973 ARM::VLD1d32, ARM::VLD1d64 };
Bob Wilsonffde0802010-09-02 16:00:54 +00001974 unsigned QOpcodes[] = { ARM::VLD1q8Pseudo, ARM::VLD1q16Pseudo,
1975 ARM::VLD1q32Pseudo, ARM::VLD1q64Pseudo };
Bob Wilson621f1952010-03-23 05:25:43 +00001976 return SelectVLD(N, 1, DOpcodes, QOpcodes, 0);
1977 }
1978
Bob Wilson31fb12f2009-08-26 17:39:53 +00001979 case Intrinsic::arm_neon_vld2: {
Bob Wilsonffde0802010-09-02 16:00:54 +00001980 unsigned DOpcodes[] = { ARM::VLD2d8Pseudo, ARM::VLD2d16Pseudo,
1981 ARM::VLD2d32Pseudo, ARM::VLD1q64Pseudo };
1982 unsigned QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
1983 ARM::VLD2q32Pseudo };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001984 return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00001985 }
1986
1987 case Intrinsic::arm_neon_vld3: {
Bob Wilsonf5721912010-09-03 18:16:02 +00001988 unsigned DOpcodes[] = { ARM::VLD3d8Pseudo, ARM::VLD3d16Pseudo,
1989 ARM::VLD3d32Pseudo, ARM::VLD1d64TPseudo };
1990 unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
1991 ARM::VLD3q16Pseudo_UPD,
1992 ARM::VLD3q32Pseudo_UPD };
1993 unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
1994 ARM::VLD3q16oddPseudo_UPD,
1995 ARM::VLD3q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001996 return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00001997 }
1998
1999 case Intrinsic::arm_neon_vld4: {
Bob Wilsonf5721912010-09-03 18:16:02 +00002000 unsigned DOpcodes[] = { ARM::VLD4d8Pseudo, ARM::VLD4d16Pseudo,
2001 ARM::VLD4d32Pseudo, ARM::VLD1d64QPseudo };
2002 unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
2003 ARM::VLD4q16Pseudo_UPD,
2004 ARM::VLD4q32Pseudo_UPD };
2005 unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
2006 ARM::VLD4q16oddPseudo_UPD,
2007 ARM::VLD4q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002008 return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002009 }
2010
Bob Wilson243fcc52009-09-01 04:26:28 +00002011 case Intrinsic::arm_neon_vld2lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002012 unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo, ARM::VLD2LNd16Pseudo,
2013 ARM::VLD2LNd32Pseudo };
2014 unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo, ARM::VLD2LNq32Pseudo };
2015 return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002016 }
2017
2018 case Intrinsic::arm_neon_vld3lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002019 unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo, ARM::VLD3LNd16Pseudo,
2020 ARM::VLD3LNd32Pseudo };
2021 unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo, ARM::VLD3LNq32Pseudo };
2022 return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002023 }
2024
2025 case Intrinsic::arm_neon_vld4lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002026 unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo, ARM::VLD4LNd16Pseudo,
2027 ARM::VLD4LNd32Pseudo };
2028 unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo, ARM::VLD4LNq32Pseudo };
2029 return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002030 }
2031
Bob Wilson11d98992010-03-23 06:20:33 +00002032 case Intrinsic::arm_neon_vst1: {
2033 unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2034 ARM::VST1d32, ARM::VST1d64 };
Bob Wilsone5ce4f62010-08-28 05:12:57 +00002035 unsigned QOpcodes[] = { ARM::VST1q8Pseudo, ARM::VST1q16Pseudo,
2036 ARM::VST1q32Pseudo, ARM::VST1q64Pseudo };
Bob Wilson11d98992010-03-23 06:20:33 +00002037 return SelectVST(N, 1, DOpcodes, QOpcodes, 0);
2038 }
2039
Bob Wilson31fb12f2009-08-26 17:39:53 +00002040 case Intrinsic::arm_neon_vst2: {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00002041 unsigned DOpcodes[] = { ARM::VST2d8Pseudo, ARM::VST2d16Pseudo,
2042 ARM::VST2d32Pseudo, ARM::VST1q64Pseudo };
2043 unsigned QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
2044 ARM::VST2q32Pseudo };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002045 return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002046 }
2047
2048 case Intrinsic::arm_neon_vst3: {
Bob Wilson01ba4612010-08-26 18:51:29 +00002049 unsigned DOpcodes[] = { ARM::VST3d8Pseudo, ARM::VST3d16Pseudo,
2050 ARM::VST3d32Pseudo, ARM::VST1d64TPseudo };
2051 unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2052 ARM::VST3q16Pseudo_UPD,
2053 ARM::VST3q32Pseudo_UPD };
2054 unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2055 ARM::VST3q16oddPseudo_UPD,
2056 ARM::VST3q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002057 return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002058 }
2059
2060 case Intrinsic::arm_neon_vst4: {
Bob Wilson709d5922010-08-25 23:27:42 +00002061 unsigned DOpcodes[] = { ARM::VST4d8Pseudo, ARM::VST4d16Pseudo,
Bob Wilson70e48b22010-08-26 05:33:30 +00002062 ARM::VST4d32Pseudo, ARM::VST1d64QPseudo };
Bob Wilson709d5922010-08-25 23:27:42 +00002063 unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2064 ARM::VST4q16Pseudo_UPD,
2065 ARM::VST4q32Pseudo_UPD };
2066 unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2067 ARM::VST4q16oddPseudo_UPD,
2068 ARM::VST4q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002069 return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002070 }
Bob Wilson8a3198b2009-09-01 18:51:56 +00002071
2072 case Intrinsic::arm_neon_vst2lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002073 unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo, ARM::VST2LNd16Pseudo,
2074 ARM::VST2LNd32Pseudo };
2075 unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo, ARM::VST2LNq32Pseudo };
2076 return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002077 }
2078
2079 case Intrinsic::arm_neon_vst3lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002080 unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo, ARM::VST3LNd16Pseudo,
2081 ARM::VST3LNd32Pseudo };
2082 unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo, ARM::VST3LNq32Pseudo };
2083 return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002084 }
2085
2086 case Intrinsic::arm_neon_vst4lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002087 unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo, ARM::VST4LNd16Pseudo,
2088 ARM::VST4LNd32Pseudo };
2089 unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo, ARM::VST4LNq32Pseudo };
2090 return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002091 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00002092 }
Bob Wilson429009b2010-05-06 16:05:26 +00002093 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00002094 }
Evan Chengde8aa4e2010-05-05 18:28:36 +00002095
Bob Wilsond491d6e2010-07-06 23:36:25 +00002096 case ISD::INTRINSIC_WO_CHAIN: {
2097 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2098 switch (IntNo) {
2099 default:
2100 break;
2101
2102 case Intrinsic::arm_neon_vtbl2:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002103 return SelectVTBL(N, false, 2, ARM::VTBL2Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002104 case Intrinsic::arm_neon_vtbl3:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002105 return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002106 case Intrinsic::arm_neon_vtbl4:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002107 return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002108
2109 case Intrinsic::arm_neon_vtbx2:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002110 return SelectVTBL(N, true, 2, ARM::VTBX2Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002111 case Intrinsic::arm_neon_vtbx3:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002112 return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002113 case Intrinsic::arm_neon_vtbx4:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002114 return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002115 }
2116 break;
2117 }
2118
Bob Wilson429009b2010-05-06 16:05:26 +00002119 case ISD::CONCAT_VECTORS:
Evan Chengde8aa4e2010-05-05 18:28:36 +00002120 return SelectConcatVector(N);
2121 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00002122
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002123 return SelectCode(N);
Evan Chenga8e29892007-01-19 07:51:42 +00002124}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002125
Bob Wilson224c2442009-05-19 05:53:42 +00002126bool ARMDAGToDAGISel::
2127SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2128 std::vector<SDValue> &OutOps) {
2129 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
Bob Wilson765cc0b2009-10-13 20:50:28 +00002130 // Require the address to be in a register. That is safe for all ARM
2131 // variants and it is hard to do anything much smarter without knowing
2132 // how the operand is used.
2133 OutOps.push_back(Op);
Bob Wilson224c2442009-05-19 05:53:42 +00002134 return false;
2135}
2136
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002137/// createARMISelDag - This pass converts a legalized DAG into a
2138/// ARM-specific DAG, ready for instruction scheduling.
2139///
Bob Wilson522ce972009-09-28 14:30:20 +00002140FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2141 CodeGenOpt::Level OptLevel) {
2142 return new ARMDAGToDAGISel(TM, OptLevel);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002143}