blob: 7bc7b4ae01bd4255814eba1158590a3d293408c8 [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.
Jim Grosbachbe912322010-09-29 17:32:29 +0000292 if (N.getOpcode() == ISD::ADD) {
Evan Chenga8e29892007-01-19 07:51:42 +0000293 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 Grosbachbe912322010-09-29 17:32:29 +0000315 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000316
Johnny Chen6a3b5ee2009-10-27 17:25:15 +0000317 // Otherwise this is R +/- [possibly shifted] R.
Evan Chenga8e29892007-01-19 07:51:42 +0000318 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
319 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
320 unsigned ShAmt = 0;
Jim Grosbach764ab522009-08-11 15:33:49 +0000321
Evan Chenga8e29892007-01-19 07:51:42 +0000322 Base = N.getOperand(0);
323 Offset = N.getOperand(1);
Jim Grosbach764ab522009-08-11 15:33:49 +0000324
Evan Chenga8e29892007-01-19 07:51:42 +0000325 if (ShOpcVal != ARM_AM::no_shift) {
326 // Check to see if the RHS of the shift is a constant, if not, we can't fold
327 // it.
328 if (ConstantSDNode *Sh =
329 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000330 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000331 Offset = N.getOperand(1).getOperand(0);
332 } else {
333 ShOpcVal = ARM_AM::no_shift;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000334 }
335 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000336
Evan Chenga8e29892007-01-19 07:51:42 +0000337 // Try matching (R shl C) + (R).
338 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
339 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
340 if (ShOpcVal != ARM_AM::no_shift) {
341 // Check to see if the RHS of the shift is a constant, if not, we can't
342 // fold it.
343 if (ConstantSDNode *Sh =
344 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000345 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000346 Offset = N.getOperand(0).getOperand(0);
347 Base = N.getOperand(1);
348 } else {
349 ShOpcVal = ARM_AM::no_shift;
350 }
351 }
352 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000353
Evan Chenga8e29892007-01-19 07:51:42 +0000354 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000356 return true;
357}
358
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000359bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000360 SDValue &Offset, SDValue &Opc) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000361 unsigned Opcode = Op->getOpcode();
Evan Chenga8e29892007-01-19 07:51:42 +0000362 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
363 ? cast<LoadSDNode>(Op)->getAddressingMode()
364 : cast<StoreSDNode>(Op)->getAddressingMode();
365 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
366 ? ARM_AM::add : ARM_AM::sub;
367 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000368 int Val = (int)C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000369 if (Val >= 0 && Val < 0x1000) { // 12 bits.
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000371 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
372 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000374 return true;
375 }
376 }
377
378 Offset = N;
379 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
380 unsigned ShAmt = 0;
381 if (ShOpcVal != ARM_AM::no_shift) {
382 // Check to see if the RHS of the shift is a constant, if not, we can't fold
383 // it.
384 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000385 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000386 Offset = N.getOperand(0);
387 } else {
388 ShOpcVal = ARM_AM::no_shift;
389 }
390 }
391
392 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 MVT::i32);
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000394 return true;
395}
396
Evan Chenga8e29892007-01-19 07:51:42 +0000397
Chris Lattner52a261b2010-09-21 20:31:19 +0000398bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000399 SDValue &Base, SDValue &Offset,
400 SDValue &Opc) {
Evan Chenga8e29892007-01-19 07:51:42 +0000401 if (N.getOpcode() == ISD::SUB) {
402 // X - C is canonicalize to X + -C, no need to handle it here.
403 Base = N.getOperand(0);
404 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000406 return true;
407 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000408
Evan Chenga8e29892007-01-19 07:51:42 +0000409 if (N.getOpcode() != ISD::ADD) {
410 Base = N;
411 if (N.getOpcode() == ISD::FrameIndex) {
412 int FI = cast<FrameIndexSDNode>(N)->getIndex();
413 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
414 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 Offset = CurDAG->getRegister(0, MVT::i32);
416 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000417 return true;
418 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000419
Evan Chenga8e29892007-01-19 07:51:42 +0000420 // If the RHS is +/- imm8, fold into addr mode.
421 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000422 int RHSC = (int)RHS->getZExtValue();
Evan Chenge966d642007-01-24 02:45:25 +0000423 if ((RHSC >= 0 && RHSC < 256) ||
424 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000425 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000426 if (Base.getOpcode() == ISD::FrameIndex) {
427 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
428 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
429 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000431
432 ARM_AM::AddrOpc AddSub = ARM_AM::add;
433 if (RHSC < 0) {
434 AddSub = ARM_AM::sub;
435 RHSC = - RHSC;
436 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000438 return true;
439 }
440 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000441
Evan Chenga8e29892007-01-19 07:51:42 +0000442 Base = N.getOperand(0);
443 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000445 return true;
446}
447
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000448bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000449 SDValue &Offset, SDValue &Opc) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000450 unsigned Opcode = Op->getOpcode();
Evan Chenga8e29892007-01-19 07:51:42 +0000451 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
452 ? cast<LoadSDNode>(Op)->getAddressingMode()
453 : cast<StoreSDNode>(Op)->getAddressingMode();
454 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
455 ? ARM_AM::add : ARM_AM::sub;
456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000457 int Val = (int)C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000458 if (Val >= 0 && Val < 256) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 Offset = CurDAG->getRegister(0, MVT::i32);
460 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000461 return true;
462 }
463 }
464
465 Offset = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000467 return true;
468}
469
Chris Lattner52a261b2010-09-21 20:31:19 +0000470bool ARMDAGToDAGISel::SelectAddrMode4(SDValue N, SDValue &Addr, SDValue &Mode) {
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000471 Addr = N;
Bob Wilsonfd7fd942010-08-28 00:20:11 +0000472 Mode = CurDAG->getTargetConstant(ARM_AM::getAM4ModeImm(ARM_AM::ia), MVT::i32);
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000473 return true;
474}
Evan Chenga8e29892007-01-19 07:51:42 +0000475
Chris Lattner52a261b2010-09-21 20:31:19 +0000476bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000477 SDValue &Base, SDValue &Offset) {
Evan Chenga8e29892007-01-19 07:51:42 +0000478 if (N.getOpcode() != ISD::ADD) {
479 Base = N;
480 if (N.getOpcode() == ISD::FrameIndex) {
481 int FI = cast<FrameIndexSDNode>(N)->getIndex();
482 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000483 } else if (N.getOpcode() == ARMISD::Wrapper &&
484 !(Subtarget->useMovt() &&
485 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000486 Base = N.getOperand(0);
487 }
488 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000490 return true;
491 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000492
Evan Chenga8e29892007-01-19 07:51:42 +0000493 // If the RHS is +/- imm8, fold into addr mode.
494 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000495 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000496 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
497 RHSC >>= 2;
Evan Chenge966d642007-01-24 02:45:25 +0000498 if ((RHSC >= 0 && RHSC < 256) ||
499 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000500 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000501 if (Base.getOpcode() == ISD::FrameIndex) {
502 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
503 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
504 }
505
506 ARM_AM::AddrOpc AddSub = ARM_AM::add;
507 if (RHSC < 0) {
508 AddSub = ARM_AM::sub;
509 RHSC = - RHSC;
510 }
511 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000513 return true;
514 }
515 }
516 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000517
Evan Chenga8e29892007-01-19 07:51:42 +0000518 Base = N;
519 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000521 return true;
522}
523
Chris Lattner52a261b2010-09-21 20:31:19 +0000524bool ARMDAGToDAGISel::SelectAddrMode6(SDValue N, SDValue &Addr, SDValue &Align){
Bob Wilson8b024a52009-07-01 23:16:05 +0000525 Addr = N;
Jim Grosbach8a5ec862009-11-07 21:25:39 +0000526 // Default to no alignment.
527 Align = CurDAG->getTargetConstant(0, MVT::i32);
Bob Wilson8b024a52009-07-01 23:16:05 +0000528 return true;
529}
530
Chris Lattner52a261b2010-09-21 20:31:19 +0000531bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
Evan Chengbba9f5f2009-08-14 19:01:37 +0000532 SDValue &Offset, SDValue &Label) {
Evan Chenga8e29892007-01-19 07:51:42 +0000533 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
534 Offset = N.getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000535 SDValue N1 = N.getOperand(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000536 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000538 return true;
539 }
540 return false;
541}
542
Chris Lattner52a261b2010-09-21 20:31:19 +0000543bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000544 SDValue &Base, SDValue &Offset){
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000545 // FIXME dl should come from the parent load or store, not the address
Evan Chengc38f2bc2007-01-23 22:59:13 +0000546 if (N.getOpcode() != ISD::ADD) {
Evan Cheng2f297df2009-07-11 07:08:13 +0000547 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
Dan Gohmane368b462010-06-18 14:22:04 +0000548 if (!NC || !NC->isNullValue())
Evan Cheng2f297df2009-07-11 07:08:13 +0000549 return false;
550
551 Base = Offset = N;
Evan Chengc38f2bc2007-01-23 22:59:13 +0000552 return true;
553 }
554
Evan Chenga8e29892007-01-19 07:51:42 +0000555 Base = N.getOperand(0);
556 Offset = N.getOperand(1);
557 return true;
558}
559
Evan Cheng79d43262007-01-24 02:21:22 +0000560bool
Chris Lattner52a261b2010-09-21 20:31:19 +0000561ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000562 unsigned Scale, SDValue &Base,
563 SDValue &OffImm, SDValue &Offset) {
Evan Cheng79d43262007-01-24 02:21:22 +0000564 if (Scale == 4) {
Dan Gohman475871a2008-07-27 21:46:04 +0000565 SDValue TmpBase, TmpOffImm;
Chris Lattner52a261b2010-09-21 20:31:19 +0000566 if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
Evan Cheng79d43262007-01-24 02:21:22 +0000567 return false; // We want to select tLDRspi / tSTRspi instead.
Evan Cheng012f2d92007-01-24 08:53:17 +0000568 if (N.getOpcode() == ARMISD::Wrapper &&
569 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
570 return false; // We want to select tLDRpci instead.
Evan Cheng79d43262007-01-24 02:21:22 +0000571 }
572
Evan Chenga8e29892007-01-19 07:51:42 +0000573 if (N.getOpcode() != ISD::ADD) {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000574 if (N.getOpcode() == ARMISD::Wrapper &&
575 !(Subtarget->useMovt() &&
576 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
577 Base = N.getOperand(0);
578 } else
579 Base = N;
580
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 Offset = CurDAG->getRegister(0, MVT::i32);
582 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000583 return true;
584 }
585
Evan Chengad0e4652007-02-06 00:22:06 +0000586 // Thumb does not have [sp, r] address mode.
587 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
588 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
589 if ((LHSR && LHSR->getReg() == ARM::SP) ||
590 (RHSR && RHSR->getReg() == ARM::SP)) {
591 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 Offset = CurDAG->getRegister(0, MVT::i32);
593 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengad0e4652007-02-06 00:22:06 +0000594 return true;
595 }
596
Evan Chenga8e29892007-01-19 07:51:42 +0000597 // If the RHS is + imm5 * scale, fold into addr mode.
598 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000599 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000600 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
601 RHSC /= Scale;
602 if (RHSC >= 0 && RHSC < 32) {
603 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 Offset = CurDAG->getRegister(0, MVT::i32);
605 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000606 return true;
607 }
608 }
609 }
610
Evan Chengc38f2bc2007-01-23 22:59:13 +0000611 Base = N.getOperand(0);
612 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000614 return true;
Evan Chenga8e29892007-01-19 07:51:42 +0000615}
616
Chris Lattner52a261b2010-09-21 20:31:19 +0000617bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000618 SDValue &Base, SDValue &OffImm,
619 SDValue &Offset) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000620 return SelectThumbAddrModeRI5(N, 1, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000621}
622
Chris Lattner52a261b2010-09-21 20:31:19 +0000623bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000624 SDValue &Base, SDValue &OffImm,
625 SDValue &Offset) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000626 return SelectThumbAddrModeRI5(N, 2, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000627}
628
Chris Lattner52a261b2010-09-21 20:31:19 +0000629bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000630 SDValue &Base, SDValue &OffImm,
631 SDValue &Offset) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000632 return SelectThumbAddrModeRI5(N, 4, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000633}
634
Chris Lattner52a261b2010-09-21 20:31:19 +0000635bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
636 SDValue &Base, SDValue &OffImm) {
Evan Chenga8e29892007-01-19 07:51:42 +0000637 if (N.getOpcode() == ISD::FrameIndex) {
638 int FI = cast<FrameIndexSDNode>(N)->getIndex();
639 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000641 return true;
642 }
Evan Cheng79d43262007-01-24 02:21:22 +0000643
Evan Chengad0e4652007-02-06 00:22:06 +0000644 if (N.getOpcode() != ISD::ADD)
645 return false;
646
647 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
Evan Cheng8c1a73a2007-02-06 09:11:20 +0000648 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
649 (LHSR && LHSR->getReg() == ARM::SP)) {
Evan Cheng79d43262007-01-24 02:21:22 +0000650 // If the RHS is + imm8 * scale, fold into addr mode.
651 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000652 int RHSC = (int)RHS->getZExtValue();
Evan Cheng79d43262007-01-24 02:21:22 +0000653 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
654 RHSC >>= 2;
655 if (RHSC >= 0 && RHSC < 256) {
Evan Chengad0e4652007-02-06 00:22:06 +0000656 Base = N.getOperand(0);
Evan Cheng8c1a73a2007-02-06 09:11:20 +0000657 if (Base.getOpcode() == ISD::FrameIndex) {
658 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
659 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
660 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Cheng79d43262007-01-24 02:21:22 +0000662 return true;
663 }
664 }
665 }
666 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000667
Evan Chenga8e29892007-01-19 07:51:42 +0000668 return false;
669}
670
Chris Lattner52a261b2010-09-21 20:31:19 +0000671bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDValue N, SDValue &BaseReg,
Evan Cheng9cb9e672009-06-27 02:26:13 +0000672 SDValue &Opc) {
Evan Chenga2c519b2010-07-30 23:33:54 +0000673 if (DisableShifterOp)
674 return false;
675
Evan Cheng9cb9e672009-06-27 02:26:13 +0000676 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
677
678 // Don't match base register only case. That is matched to a separate
679 // lower complexity pattern with explicit register operand.
680 if (ShOpcVal == ARM_AM::no_shift) return false;
681
682 BaseReg = N.getOperand(0);
683 unsigned ShImmVal = 0;
684 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
685 ShImmVal = RHS->getZExtValue() & 31;
686 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
687 return true;
688 }
689
690 return false;
691}
692
Chris Lattner52a261b2010-09-21 20:31:19 +0000693bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000694 SDValue &Base, SDValue &OffImm) {
695 // Match simple R + imm12 operands.
David Goodwin31e7eba2009-07-20 15:55:39 +0000696
Evan Cheng3a214252009-08-11 08:52:18 +0000697 // Base only.
698 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
David Goodwin31e7eba2009-07-20 15:55:39 +0000699 if (N.getOpcode() == ISD::FrameIndex) {
Evan Cheng3a214252009-08-11 08:52:18 +0000700 // Match frame index...
David Goodwin31e7eba2009-07-20 15:55:39 +0000701 int FI = cast<FrameIndexSDNode>(N)->getIndex();
702 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
David Goodwin31e7eba2009-07-20 15:55:39 +0000704 return true;
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000705 } else if (N.getOpcode() == ARMISD::Wrapper &&
706 !(Subtarget->useMovt() &&
707 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Cheng3a214252009-08-11 08:52:18 +0000708 Base = N.getOperand(0);
709 if (Base.getOpcode() == ISD::TargetConstantPool)
710 return false; // We want to select t2LDRpci instead.
711 } else
712 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000713 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng3a214252009-08-11 08:52:18 +0000714 return true;
David Goodwin31e7eba2009-07-20 15:55:39 +0000715 }
Evan Cheng055b0312009-06-29 07:51:04 +0000716
717 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000718 if (SelectT2AddrModeImm8(N, Base, OffImm))
Evan Cheng3a214252009-08-11 08:52:18 +0000719 // Let t2LDRi8 handle (R - imm8).
720 return false;
721
Evan Cheng055b0312009-06-29 07:51:04 +0000722 int RHSC = (int)RHS->getZExtValue();
David Goodwind8c95b52009-07-30 18:56:48 +0000723 if (N.getOpcode() == ISD::SUB)
724 RHSC = -RHSC;
725
726 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
Evan Cheng055b0312009-06-29 07:51:04 +0000727 Base = N.getOperand(0);
David Goodwind8c95b52009-07-30 18:56:48 +0000728 if (Base.getOpcode() == ISD::FrameIndex) {
729 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
730 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
731 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000732 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000733 return true;
734 }
735 }
736
Evan Cheng3a214252009-08-11 08:52:18 +0000737 // Base only.
738 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng3a214252009-08-11 08:52:18 +0000740 return true;
Evan Cheng055b0312009-06-29 07:51:04 +0000741}
742
Chris Lattner52a261b2010-09-21 20:31:19 +0000743bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000744 SDValue &Base, SDValue &OffImm) {
David Goodwind8c95b52009-07-30 18:56:48 +0000745 // Match simple R - imm8 operands.
Evan Cheng3a214252009-08-11 08:52:18 +0000746 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::SUB) {
David Goodwin07337c02009-07-30 22:45:52 +0000747 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
748 int RHSC = (int)RHS->getSExtValue();
749 if (N.getOpcode() == ISD::SUB)
750 RHSC = -RHSC;
Jim Grosbach764ab522009-08-11 15:33:49 +0000751
Evan Cheng3a214252009-08-11 08:52:18 +0000752 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
753 Base = N.getOperand(0);
David Goodwin07337c02009-07-30 22:45:52 +0000754 if (Base.getOpcode() == ISD::FrameIndex) {
755 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
756 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
757 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
David Goodwin07337c02009-07-30 22:45:52 +0000759 return true;
Evan Cheng055b0312009-06-29 07:51:04 +0000760 }
Evan Cheng055b0312009-06-29 07:51:04 +0000761 }
762 }
763
764 return false;
765}
766
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000767bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Chenge88d5ce2009-07-02 07:28:31 +0000768 SDValue &OffImm){
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000769 unsigned Opcode = Op->getOpcode();
Evan Chenge88d5ce2009-07-02 07:28:31 +0000770 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
771 ? cast<LoadSDNode>(Op)->getAddressingMode()
772 : cast<StoreSDNode>(Op)->getAddressingMode();
773 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N)) {
774 int RHSC = (int)RHS->getZExtValue();
775 if (RHSC >= 0 && RHSC < 0x100) { // 8 bits.
David Goodwin4cb73522009-07-14 21:29:29 +0000776 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
Owen Anderson825b72b2009-08-11 20:47:22 +0000777 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
778 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000779 return true;
780 }
781 }
782
783 return false;
784}
785
Chris Lattner52a261b2010-09-21 20:31:19 +0000786bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000787 SDValue &Base,
788 SDValue &OffReg, SDValue &ShImm) {
Evan Cheng3a214252009-08-11 08:52:18 +0000789 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
790 if (N.getOpcode() != ISD::ADD)
791 return false;
Evan Cheng055b0312009-06-29 07:51:04 +0000792
Evan Cheng3a214252009-08-11 08:52:18 +0000793 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
794 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
795 int RHSC = (int)RHS->getZExtValue();
796 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
797 return false;
798 else if (RHSC < 0 && RHSC >= -255) // 8 bits
David Goodwind8c95b52009-07-30 18:56:48 +0000799 return false;
800 }
801
Evan Cheng055b0312009-06-29 07:51:04 +0000802 // Look for (R + R) or (R + (R << [1,2,3])).
803 unsigned ShAmt = 0;
804 Base = N.getOperand(0);
805 OffReg = N.getOperand(1);
806
807 // Swap if it is ((R << c) + R).
808 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
809 if (ShOpcVal != ARM_AM::lsl) {
810 ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
811 if (ShOpcVal == ARM_AM::lsl)
812 std::swap(Base, OffReg);
Jim Grosbach764ab522009-08-11 15:33:49 +0000813 }
814
Evan Cheng055b0312009-06-29 07:51:04 +0000815 if (ShOpcVal == ARM_AM::lsl) {
816 // Check to see if the RHS of the shift is a constant, if not, we can't fold
817 // it.
818 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
819 ShAmt = Sh->getZExtValue();
820 if (ShAmt >= 4) {
821 ShAmt = 0;
822 ShOpcVal = ARM_AM::no_shift;
823 } else
824 OffReg = OffReg.getOperand(0);
825 } else {
826 ShOpcVal = ARM_AM::no_shift;
827 }
David Goodwin7ecc8502009-07-15 15:50:19 +0000828 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000829
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000831
832 return true;
833}
834
835//===--------------------------------------------------------------------===//
836
Evan Chengee568cf2007-07-05 07:15:27 +0000837/// getAL - Returns a ARMCC::AL immediate node.
Dan Gohman475871a2008-07-27 21:46:04 +0000838static inline SDValue getAL(SelectionDAG *CurDAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000839 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
Evan Cheng44bec522007-05-15 01:29:07 +0000840}
841
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000842SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
843 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengaf4550f2009-07-02 01:23:32 +0000844 ISD::MemIndexedMode AM = LD->getAddressingMode();
845 if (AM == ISD::UNINDEXED)
846 return NULL;
847
Owen Andersone50ed302009-08-10 22:56:29 +0000848 EVT LoadedVT = LD->getMemoryVT();
Evan Chengaf4550f2009-07-02 01:23:32 +0000849 SDValue Offset, AMOpc;
850 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
851 unsigned Opcode = 0;
852 bool Match = false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000853 if (LoadedVT == MVT::i32 &&
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000854 SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000855 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
856 Match = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 } else if (LoadedVT == MVT::i16 &&
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000858 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000859 Match = true;
860 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
861 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
862 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000864 if (LD->getExtensionType() == ISD::SEXTLOAD) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000865 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000866 Match = true;
867 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
868 }
869 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000870 if (SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000871 Match = true;
872 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
873 }
874 }
875 }
876
877 if (Match) {
878 SDValue Chain = LD->getChain();
879 SDValue Base = LD->getBasePtr();
880 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +0000881 CurDAG->getRegister(0, MVT::i32), Chain };
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000882 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +0000883 MVT::Other, Ops, 6);
Evan Chengaf4550f2009-07-02 01:23:32 +0000884 }
885
886 return NULL;
887}
888
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000889SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
890 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000891 ISD::MemIndexedMode AM = LD->getAddressingMode();
892 if (AM == ISD::UNINDEXED)
893 return NULL;
894
Owen Andersone50ed302009-08-10 22:56:29 +0000895 EVT LoadedVT = LD->getMemoryVT();
Evan Cheng4fbb9962009-07-02 23:16:11 +0000896 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000897 SDValue Offset;
898 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
899 unsigned Opcode = 0;
900 bool Match = false;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000901 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 switch (LoadedVT.getSimpleVT().SimpleTy) {
903 case MVT::i32:
Evan Chenge88d5ce2009-07-02 07:28:31 +0000904 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
905 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000906 case MVT::i16:
Evan Cheng4fbb9962009-07-02 23:16:11 +0000907 if (isSExtLd)
908 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
909 else
910 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000911 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 case MVT::i8:
913 case MVT::i1:
Evan Cheng4fbb9962009-07-02 23:16:11 +0000914 if (isSExtLd)
915 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
916 else
917 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000918 break;
919 default:
920 return NULL;
921 }
922 Match = true;
923 }
924
925 if (Match) {
926 SDValue Chain = LD->getChain();
927 SDValue Base = LD->getBasePtr();
928 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 CurDAG->getRegister(0, MVT::i32), Chain };
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000930 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +0000931 MVT::Other, Ops, 5);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000932 }
933
934 return NULL;
935}
936
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000937/// PairSRegs - Form a D register from a pair of S registers.
938///
939SDNode *ARMDAGToDAGISel::PairSRegs(EVT VT, SDValue V0, SDValue V1) {
940 DebugLoc dl = V0.getNode()->getDebugLoc();
941 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
942 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
Bob Wilson07f6e802010-06-16 21:34:01 +0000943 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
944 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000945}
946
Evan Cheng603afbf2010-05-10 17:34:18 +0000947/// PairDRegs - Form a quad register from a pair of D registers.
948///
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000949SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
950 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000951 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
952 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Bob Wilson07f6e802010-06-16 21:34:01 +0000953 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
954 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000955}
956
Evan Cheng7f687192010-05-14 00:21:45 +0000957/// PairQRegs - Form 4 consecutive D registers from a pair of Q registers.
Evan Cheng603afbf2010-05-10 17:34:18 +0000958///
959SDNode *ARMDAGToDAGISel::PairQRegs(EVT VT, SDValue V0, SDValue V1) {
960 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000961 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
962 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
Evan Cheng603afbf2010-05-10 17:34:18 +0000963 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
964 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
965}
966
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000967/// QuadSRegs - Form 4 consecutive S registers.
968///
969SDNode *ARMDAGToDAGISel::QuadSRegs(EVT VT, SDValue V0, SDValue V1,
970 SDValue V2, SDValue V3) {
971 DebugLoc dl = V0.getNode()->getDebugLoc();
972 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
973 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
974 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, MVT::i32);
975 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, MVT::i32);
976 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
977 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
978}
979
Evan Cheng7f687192010-05-14 00:21:45 +0000980/// QuadDRegs - Form 4 consecutive D registers.
Evan Cheng603afbf2010-05-10 17:34:18 +0000981///
982SDNode *ARMDAGToDAGISel::QuadDRegs(EVT VT, SDValue V0, SDValue V1,
983 SDValue V2, SDValue V3) {
984 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000985 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
986 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
987 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
988 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
Evan Cheng603afbf2010-05-10 17:34:18 +0000989 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
990 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
991}
992
Evan Cheng8f6de382010-05-16 03:27:48 +0000993/// QuadQRegs - Form 4 consecutive Q registers.
994///
995SDNode *ARMDAGToDAGISel::QuadQRegs(EVT VT, SDValue V0, SDValue V1,
996 SDValue V2, SDValue V3) {
997 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000998 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
999 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
1000 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, MVT::i32);
1001 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, MVT::i32);
Evan Cheng8f6de382010-05-16 03:27:48 +00001002 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1003 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1004}
1005
Bob Wilson2a6e6162010-09-23 23:42:37 +00001006/// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1007/// of a NEON VLD or VST instruction. The supported values depend on the
1008/// number of registers being loaded.
1009static unsigned GetVLDSTAlign(SDNode *N, unsigned NumVecs, bool is64BitVector) {
1010 unsigned NumRegs = NumVecs;
1011 if (!is64BitVector && NumVecs < 3)
1012 NumRegs *= 2;
1013
1014 unsigned Alignment = cast<MemIntrinsicSDNode>(N)->getAlignment();
1015 if (Alignment >= 32 && NumRegs == 4)
1016 return 32;
1017 if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1018 return 16;
1019 if (Alignment >= 8)
1020 return 8;
1021 return 0;
1022}
1023
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001024SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
Bob Wilson3e36f132009-10-14 17:28:52 +00001025 unsigned *DOpcodes, unsigned *QOpcodes0,
1026 unsigned *QOpcodes1) {
Bob Wilson621f1952010-03-23 05:25:43 +00001027 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
Bob Wilson3e36f132009-10-14 17:28:52 +00001028 DebugLoc dl = N->getDebugLoc();
1029
Bob Wilson226036e2010-03-20 22:13:40 +00001030 SDValue MemAddr, Align;
Chris Lattner52a261b2010-09-21 20:31:19 +00001031 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
Bob Wilson3e36f132009-10-14 17:28:52 +00001032 return NULL;
1033
1034 SDValue Chain = N->getOperand(0);
1035 EVT VT = N->getValueType(0);
1036 bool is64BitVector = VT.is64BitVector();
1037
Bob Wilson2a6e6162010-09-23 23:42:37 +00001038 unsigned Alignment = GetVLDSTAlign(N, NumVecs, is64BitVector);
Bob Wilson40ff01a2010-09-23 21:43:54 +00001039 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1040
Bob Wilson3e36f132009-10-14 17:28:52 +00001041 unsigned OpcodeIndex;
1042 switch (VT.getSimpleVT().SimpleTy) {
1043 default: llvm_unreachable("unhandled vld type");
1044 // Double-register operations:
1045 case MVT::v8i8: OpcodeIndex = 0; break;
1046 case MVT::v4i16: OpcodeIndex = 1; break;
1047 case MVT::v2f32:
1048 case MVT::v2i32: OpcodeIndex = 2; break;
1049 case MVT::v1i64: OpcodeIndex = 3; break;
1050 // Quad-register operations:
1051 case MVT::v16i8: OpcodeIndex = 0; break;
1052 case MVT::v8i16: OpcodeIndex = 1; break;
1053 case MVT::v4f32:
1054 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson621f1952010-03-23 05:25:43 +00001055 case MVT::v2i64: OpcodeIndex = 3;
Bob Wilson11d98992010-03-23 06:20:33 +00001056 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
Bob Wilson621f1952010-03-23 05:25:43 +00001057 break;
Bob Wilson3e36f132009-10-14 17:28:52 +00001058 }
1059
Bob Wilsonf5721912010-09-03 18:16:02 +00001060 EVT ResTy;
1061 if (NumVecs == 1)
1062 ResTy = VT;
1063 else {
1064 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1065 if (!is64BitVector)
1066 ResTyElts *= 2;
1067 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1068 }
1069
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001070 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001071 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilsonf5721912010-09-03 18:16:02 +00001072 SDValue SuperReg;
Bob Wilson3e36f132009-10-14 17:28:52 +00001073 if (is64BitVector) {
1074 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001075 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilsonf5721912010-09-03 18:16:02 +00001076 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
Bob Wilsonffde0802010-09-02 16:00:54 +00001077 if (NumVecs == 1)
Evan Chenge9e2ba02010-05-10 21:26:24 +00001078 return VLd;
1079
Bob Wilsonf5721912010-09-03 18:16:02 +00001080 SuperReg = SDValue(VLd, 0);
Jakob Stoklund Olesen7bb31e32010-05-24 17:13:28 +00001081 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
Evan Cheng5c6aba22010-05-14 18:54:59 +00001082 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001083 SDValue D = CurDAG->getTargetExtractSubreg(ARM::dsub_0+Vec,
Bob Wilsonffde0802010-09-02 16:00:54 +00001084 dl, VT, SuperReg);
Evan Cheng5c6aba22010-05-14 18:54:59 +00001085 ReplaceUses(SDValue(N, Vec), D);
Evan Chenge9e2ba02010-05-10 21:26:24 +00001086 }
Bob Wilsonf5721912010-09-03 18:16:02 +00001087 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
Evan Chenge9e2ba02010-05-10 21:26:24 +00001088 return NULL;
Bob Wilson3e36f132009-10-14 17:28:52 +00001089 }
1090
Bob Wilson621f1952010-03-23 05:25:43 +00001091 if (NumVecs <= 2) {
1092 // Quad registers are directly supported for VLD1 and VLD2,
1093 // loading pairs of D regs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001094 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001095 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilsonffde0802010-09-02 16:00:54 +00001096 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
Bob Wilsonffde0802010-09-02 16:00:54 +00001097 if (NumVecs == 1)
1098 return VLd;
1099
Bob Wilsonf5721912010-09-03 18:16:02 +00001100 SuperReg = SDValue(VLd, 0);
Bob Wilsonffde0802010-09-02 16:00:54 +00001101 Chain = SDValue(VLd, 1);
1102
Bob Wilson3e36f132009-10-14 17:28:52 +00001103 } else {
1104 // Otherwise, quad registers are loaded with two separate instructions,
1105 // where one loads the even registers and the other loads the odd registers.
Bob Wilsonf5721912010-09-03 18:16:02 +00001106 EVT AddrTy = MemAddr.getValueType();
Bob Wilson3e36f132009-10-14 17:28:52 +00001107
Bob Wilson24f995d2009-10-14 18:32:29 +00001108 // Load the even subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001109 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilsonf5721912010-09-03 18:16:02 +00001110 SDValue ImplDef =
1111 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1112 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1113 SDNode *VLdA =
1114 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsA, 7);
1115 Chain = SDValue(VLdA, 2);
Bob Wilson3e36f132009-10-14 17:28:52 +00001116
Bob Wilson24f995d2009-10-14 18:32:29 +00001117 // Load the odd subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001118 Opc = QOpcodes1[OpcodeIndex];
Bob Wilsonf5721912010-09-03 18:16:02 +00001119 const SDValue OpsB[] = { SDValue(VLdA, 1), Align, Reg0, SDValue(VLdA, 0),
1120 Pred, Reg0, Chain };
1121 SDNode *VLdB =
1122 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsB, 7);
1123 SuperReg = SDValue(VLdB, 0);
1124 Chain = SDValue(VLdB, 2);
1125 }
Bob Wilson3e36f132009-10-14 17:28:52 +00001126
Bob Wilsonf5721912010-09-03 18:16:02 +00001127 // Extract out the Q registers.
1128 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1129 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1130 SDValue Q = CurDAG->getTargetExtractSubreg(ARM::qsub_0+Vec,
1131 dl, VT, SuperReg);
1132 ReplaceUses(SDValue(N, Vec), Q);
Bob Wilson3e36f132009-10-14 17:28:52 +00001133 }
1134 ReplaceUses(SDValue(N, NumVecs), Chain);
1135 return NULL;
1136}
1137
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001138SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
Bob Wilson24f995d2009-10-14 18:32:29 +00001139 unsigned *DOpcodes, unsigned *QOpcodes0,
1140 unsigned *QOpcodes1) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001141 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
Bob Wilson24f995d2009-10-14 18:32:29 +00001142 DebugLoc dl = N->getDebugLoc();
1143
Bob Wilson226036e2010-03-20 22:13:40 +00001144 SDValue MemAddr, Align;
Chris Lattner52a261b2010-09-21 20:31:19 +00001145 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
Bob Wilson24f995d2009-10-14 18:32:29 +00001146 return NULL;
1147
1148 SDValue Chain = N->getOperand(0);
1149 EVT VT = N->getOperand(3).getValueType();
1150 bool is64BitVector = VT.is64BitVector();
1151
Bob Wilson2a6e6162010-09-23 23:42:37 +00001152 unsigned Alignment = GetVLDSTAlign(N, NumVecs, is64BitVector);
1153 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1154
Bob Wilson24f995d2009-10-14 18:32:29 +00001155 unsigned OpcodeIndex;
1156 switch (VT.getSimpleVT().SimpleTy) {
1157 default: llvm_unreachable("unhandled vst type");
1158 // Double-register operations:
1159 case MVT::v8i8: OpcodeIndex = 0; break;
1160 case MVT::v4i16: OpcodeIndex = 1; break;
1161 case MVT::v2f32:
1162 case MVT::v2i32: OpcodeIndex = 2; break;
1163 case MVT::v1i64: OpcodeIndex = 3; break;
1164 // Quad-register operations:
1165 case MVT::v16i8: OpcodeIndex = 0; break;
1166 case MVT::v8i16: OpcodeIndex = 1; break;
1167 case MVT::v4f32:
1168 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson11d98992010-03-23 06:20:33 +00001169 case MVT::v2i64: OpcodeIndex = 3;
1170 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1171 break;
Bob Wilson24f995d2009-10-14 18:32:29 +00001172 }
1173
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001174 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001175 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001176
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001177 SmallVector<SDValue, 7> Ops;
Bob Wilson24f995d2009-10-14 18:32:29 +00001178 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001179 Ops.push_back(Align);
Bob Wilson24f995d2009-10-14 18:32:29 +00001180
1181 if (is64BitVector) {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001182 if (NumVecs == 1) {
1183 Ops.push_back(N->getOperand(3));
1184 } else {
Evan Cheng0ce537a2010-05-11 01:19:40 +00001185 SDValue RegSeq;
1186 SDValue V0 = N->getOperand(0+3);
1187 SDValue V1 = N->getOperand(1+3);
1188
1189 // Form a REG_SEQUENCE to force register allocation.
1190 if (NumVecs == 2)
1191 RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1192 else {
1193 SDValue V2 = N->getOperand(2+3);
1194 // If it's a vld3, form a quad D-register and leave the last part as
1195 // an undef.
1196 SDValue V3 = (NumVecs == 3)
1197 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1198 : N->getOperand(3+3);
1199 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1200 }
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001201 Ops.push_back(RegSeq);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001202 }
Evan Chengac0869d2009-11-21 06:21:52 +00001203 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001204 Ops.push_back(Reg0); // predicate register
Bob Wilson24f995d2009-10-14 18:32:29 +00001205 Ops.push_back(Chain);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001206 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001207 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
Bob Wilson24f995d2009-10-14 18:32:29 +00001208 }
1209
Bob Wilson11d98992010-03-23 06:20:33 +00001210 if (NumVecs <= 2) {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001211 // Quad registers are directly supported for VST1 and VST2.
Bob Wilson24f995d2009-10-14 18:32:29 +00001212 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001213 if (NumVecs == 1) {
1214 Ops.push_back(N->getOperand(3));
1215 } else {
1216 // Form a QQ register.
Evan Cheng603afbf2010-05-10 17:34:18 +00001217 SDValue Q0 = N->getOperand(3);
1218 SDValue Q1 = N->getOperand(4);
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001219 Ops.push_back(SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0));
Bob Wilson24f995d2009-10-14 18:32:29 +00001220 }
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001221 Ops.push_back(Pred);
1222 Ops.push_back(Reg0); // predicate register
1223 Ops.push_back(Chain);
1224 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
Bob Wilson24f995d2009-10-14 18:32:29 +00001225 }
1226
1227 // Otherwise, quad registers are stored with two separate instructions,
1228 // where one stores the even registers and the other stores the odd registers.
Evan Cheng7189fd02010-05-15 07:53:37 +00001229
Bob Wilson07f6e802010-06-16 21:34:01 +00001230 // Form the QQQQ REG_SEQUENCE.
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001231 SDValue V0 = N->getOperand(0+3);
1232 SDValue V1 = N->getOperand(1+3);
1233 SDValue V2 = N->getOperand(2+3);
1234 SDValue V3 = (NumVecs == 3)
1235 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1236 : N->getOperand(3+3);
1237 SDValue RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilson07f6e802010-06-16 21:34:01 +00001238
1239 // Store the even D registers.
Bob Wilson07f6e802010-06-16 21:34:01 +00001240 Ops.push_back(Reg0); // post-access address offset
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001241 Ops.push_back(RegSeq);
Bob Wilson07f6e802010-06-16 21:34:01 +00001242 Ops.push_back(Pred);
1243 Ops.push_back(Reg0); // predicate register
1244 Ops.push_back(Chain);
1245 unsigned Opc = QOpcodes0[OpcodeIndex];
1246 SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001247 MVT::Other, Ops.data(), 7);
Bob Wilson07f6e802010-06-16 21:34:01 +00001248 Chain = SDValue(VStA, 1);
1249
1250 // Store the odd D registers.
1251 Ops[0] = SDValue(VStA, 0); // MemAddr
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001252 Ops[6] = Chain;
Bob Wilson07f6e802010-06-16 21:34:01 +00001253 Opc = QOpcodes1[OpcodeIndex];
1254 SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001255 MVT::Other, Ops.data(), 7);
Bob Wilson07f6e802010-06-16 21:34:01 +00001256 Chain = SDValue(VStB, 1);
1257 ReplaceUses(SDValue(N, 0), Chain);
1258 return NULL;
Bob Wilson24f995d2009-10-14 18:32:29 +00001259}
1260
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001261SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
Bob Wilson96493442009-10-14 16:46:45 +00001262 unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson8466fa12010-09-13 23:01:35 +00001263 unsigned *QOpcodes) {
Bob Wilson96493442009-10-14 16:46:45 +00001264 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001265 DebugLoc dl = N->getDebugLoc();
1266
Bob Wilson226036e2010-03-20 22:13:40 +00001267 SDValue MemAddr, Align;
Chris Lattner52a261b2010-09-21 20:31:19 +00001268 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
Bob Wilsona7c397c2009-10-14 16:19:03 +00001269 return NULL;
1270
1271 SDValue Chain = N->getOperand(0);
1272 unsigned Lane =
1273 cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
Bob Wilson96493442009-10-14 16:46:45 +00001274 EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
Bob Wilsona7c397c2009-10-14 16:19:03 +00001275 bool is64BitVector = VT.is64BitVector();
1276
Bob Wilsona7c397c2009-10-14 16:19:03 +00001277 unsigned OpcodeIndex;
1278 switch (VT.getSimpleVT().SimpleTy) {
Bob Wilson96493442009-10-14 16:46:45 +00001279 default: llvm_unreachable("unhandled vld/vst lane type");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001280 // Double-register operations:
1281 case MVT::v8i8: OpcodeIndex = 0; break;
1282 case MVT::v4i16: OpcodeIndex = 1; break;
1283 case MVT::v2f32:
1284 case MVT::v2i32: OpcodeIndex = 2; break;
1285 // Quad-register operations:
1286 case MVT::v8i16: OpcodeIndex = 0; break;
1287 case MVT::v4f32:
1288 case MVT::v4i32: OpcodeIndex = 1; break;
1289 }
1290
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001291 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001292 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001293
Bob Wilson8466fa12010-09-13 23:01:35 +00001294 SmallVector<SDValue, 7> Ops;
Bob Wilsona7c397c2009-10-14 16:19:03 +00001295 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001296 Ops.push_back(Align);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001297
Eric Christopher23da0b22010-09-14 08:31:25 +00001298 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1299 QOpcodes[OpcodeIndex]);
Bob Wilson07f6e802010-06-16 21:34:01 +00001300
Bob Wilson8466fa12010-09-13 23:01:35 +00001301 SDValue SuperReg;
1302 SDValue V0 = N->getOperand(0+3);
1303 SDValue V1 = N->getOperand(1+3);
1304 if (NumVecs == 2) {
1305 if (is64BitVector)
1306 SuperReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1307 else
1308 SuperReg = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001309 } else {
Bob Wilson8466fa12010-09-13 23:01:35 +00001310 SDValue V2 = N->getOperand(2+3);
1311 SDValue V3 = (NumVecs == 3)
1312 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1313 : N->getOperand(3+3);
1314 if (is64BitVector)
1315 SuperReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1316 else
1317 SuperReg = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001318 }
Bob Wilson8466fa12010-09-13 23:01:35 +00001319 Ops.push_back(SuperReg);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001320 Ops.push_back(getI32Imm(Lane));
Evan Chengac0869d2009-11-21 06:21:52 +00001321 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001322 Ops.push_back(Reg0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001323 Ops.push_back(Chain);
1324
Bob Wilson96493442009-10-14 16:46:45 +00001325 if (!IsLoad)
Bob Wilson8466fa12010-09-13 23:01:35 +00001326 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 7);
Bob Wilson96493442009-10-14 16:46:45 +00001327
Bob Wilson8466fa12010-09-13 23:01:35 +00001328 EVT ResTy;
1329 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1330 if (!is64BitVector)
1331 ResTyElts *= 2;
1332 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001333
Bob Wilson8466fa12010-09-13 23:01:35 +00001334 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other,
1335 Ops.data(), 7);
1336 SuperReg = SDValue(VLdLn, 0);
1337 Chain = SDValue(VLdLn, 1);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001338
Bob Wilson8466fa12010-09-13 23:01:35 +00001339 // Extract the subregisters.
Bob Wilson07f6e802010-06-16 21:34:01 +00001340 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1341 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1342 unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1343 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1344 ReplaceUses(SDValue(N, Vec),
Bob Wilson8466fa12010-09-13 23:01:35 +00001345 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1346 ReplaceUses(SDValue(N, NumVecs), Chain);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001347 return NULL;
1348}
1349
Bob Wilson78dfbc32010-07-07 00:08:54 +00001350SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1351 unsigned Opc) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001352 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1353 DebugLoc dl = N->getDebugLoc();
1354 EVT VT = N->getValueType(0);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001355 unsigned FirstTblReg = IsExt ? 2 : 1;
Bob Wilsond491d6e2010-07-06 23:36:25 +00001356
1357 // Form a REG_SEQUENCE to force register allocation.
1358 SDValue RegSeq;
Bob Wilson78dfbc32010-07-07 00:08:54 +00001359 SDValue V0 = N->getOperand(FirstTblReg + 0);
1360 SDValue V1 = N->getOperand(FirstTblReg + 1);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001361 if (NumVecs == 2)
1362 RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1363 else {
Bob Wilson78dfbc32010-07-07 00:08:54 +00001364 SDValue V2 = N->getOperand(FirstTblReg + 2);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001365 // If it's a vtbl3, form a quad D-register and leave the last part as
1366 // an undef.
1367 SDValue V3 = (NumVecs == 3)
1368 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
Bob Wilson78dfbc32010-07-07 00:08:54 +00001369 : N->getOperand(FirstTblReg + 3);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001370 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1371 }
1372
Bob Wilson78dfbc32010-07-07 00:08:54 +00001373 SmallVector<SDValue, 6> Ops;
1374 if (IsExt)
1375 Ops.push_back(N->getOperand(1));
Bob Wilsonbd916c52010-09-13 23:55:10 +00001376 Ops.push_back(RegSeq);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001377 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
Bob Wilsond491d6e2010-07-06 23:36:25 +00001378 Ops.push_back(getAL(CurDAG)); // predicate
1379 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
Bob Wilson78dfbc32010-07-07 00:08:54 +00001380 return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
Bob Wilsond491d6e2010-07-06 23:36:25 +00001381}
1382
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001383SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001384 bool isSigned) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001385 if (!Subtarget->hasV6T2Ops())
1386 return NULL;
Bob Wilson96493442009-10-14 16:46:45 +00001387
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001388 unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1389 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1390
1391
1392 // For unsigned extracts, check for a shift right and mask
1393 unsigned And_imm = 0;
1394 if (N->getOpcode() == ISD::AND) {
1395 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1396
1397 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1398 if (And_imm & (And_imm + 1))
1399 return NULL;
1400
1401 unsigned Srl_imm = 0;
1402 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1403 Srl_imm)) {
1404 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1405
1406 unsigned Width = CountTrailingOnes_32(And_imm);
1407 unsigned LSB = Srl_imm;
1408 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1409 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1410 CurDAG->getTargetConstant(LSB, MVT::i32),
1411 CurDAG->getTargetConstant(Width, MVT::i32),
1412 getAL(CurDAG), Reg0 };
1413 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1414 }
1415 }
1416 return NULL;
1417 }
1418
1419 // Otherwise, we're looking for a shift of a shift
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001420 unsigned Shl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001421 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001422 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1423 unsigned Srl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001424 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001425 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1426 unsigned Width = 32 - Srl_imm;
1427 int LSB = Srl_imm - Shl_imm;
Evan Cheng8000c6c2009-10-22 00:40:00 +00001428 if (LSB < 0)
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001429 return NULL;
1430 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001431 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001432 CurDAG->getTargetConstant(LSB, MVT::i32),
1433 CurDAG->getTargetConstant(Width, MVT::i32),
1434 getAL(CurDAG), Reg0 };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001435 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001436 }
1437 }
1438 return NULL;
1439}
1440
Evan Cheng9ef48352009-11-20 00:54:03 +00001441SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001442SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001443 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1444 SDValue CPTmp0;
1445 SDValue CPTmp1;
Chris Lattner52a261b2010-09-21 20:31:19 +00001446 if (SelectT2ShifterOperandReg(TrueVal, CPTmp0, CPTmp1)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001447 unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1448 unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1449 unsigned Opc = 0;
1450 switch (SOShOp) {
1451 case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1452 case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1453 case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1454 case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1455 default:
1456 llvm_unreachable("Unknown so_reg opcode!");
1457 break;
1458 }
1459 SDValue SOShImm =
1460 CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1461 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1462 SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001463 return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
Evan Cheng9ef48352009-11-20 00:54:03 +00001464 }
1465 return 0;
1466}
1467
1468SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001469SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001470 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1471 SDValue CPTmp0;
1472 SDValue CPTmp1;
1473 SDValue CPTmp2;
Chris Lattner52a261b2010-09-21 20:31:19 +00001474 if (SelectShifterOperandReg(TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001475 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1476 SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001477 return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
Evan Cheng9ef48352009-11-20 00:54:03 +00001478 }
1479 return 0;
1480}
1481
1482SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001483SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001484 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1485 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1486 if (!T)
1487 return 0;
1488
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001489 if (Pred_t2_so_imm(TrueVal.getNode())) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001490 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1491 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1492 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001493 return CurDAG->SelectNodeTo(N,
Evan Cheng9ef48352009-11-20 00:54:03 +00001494 ARM::t2MOVCCi, MVT::i32, Ops, 5);
1495 }
1496 return 0;
1497}
1498
1499SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001500SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001501 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1502 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1503 if (!T)
1504 return 0;
1505
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001506 if (Pred_so_imm(TrueVal.getNode())) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001507 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1508 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1509 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001510 return CurDAG->SelectNodeTo(N,
Evan Cheng9ef48352009-11-20 00:54:03 +00001511 ARM::MOVCCi, MVT::i32, Ops, 5);
1512 }
1513 return 0;
1514}
1515
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001516SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
1517 EVT VT = N->getValueType(0);
1518 SDValue FalseVal = N->getOperand(0);
1519 SDValue TrueVal = N->getOperand(1);
1520 SDValue CC = N->getOperand(2);
1521 SDValue CCR = N->getOperand(3);
1522 SDValue InFlag = N->getOperand(4);
Evan Cheng9ef48352009-11-20 00:54:03 +00001523 assert(CC.getOpcode() == ISD::Constant);
1524 assert(CCR.getOpcode() == ISD::Register);
1525 ARMCC::CondCodes CCVal =
1526 (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
Evan Cheng07ba9062009-11-19 21:45:22 +00001527
1528 if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1529 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1530 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1531 // Pattern complexity = 18 cost = 1 size = 0
1532 SDValue CPTmp0;
1533 SDValue CPTmp1;
1534 SDValue CPTmp2;
1535 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001536 SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001537 CCVal, CCR, InFlag);
1538 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001539 Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001540 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1541 if (Res)
1542 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001543 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001544 SDNode *Res = SelectARMCMOVShiftOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001545 CCVal, CCR, InFlag);
1546 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001547 Res = SelectARMCMOVShiftOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001548 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1549 if (Res)
1550 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001551 }
1552
1553 // Pattern: (ARMcmov:i32 GPR:i32:$false,
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001554 // (imm:i32)<<P:Pred_so_imm>>:$true,
Evan Cheng07ba9062009-11-19 21:45:22 +00001555 // (imm:i32):$cc)
1556 // Emits: (MOVCCi:i32 GPR:i32:$false,
1557 // (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1558 // Pattern complexity = 10 cost = 1 size = 0
Evan Cheng9ef48352009-11-20 00:54:03 +00001559 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001560 SDNode *Res = SelectT2CMOVSoImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001561 CCVal, CCR, InFlag);
1562 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001563 Res = SelectT2CMOVSoImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001564 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1565 if (Res)
1566 return Res;
1567 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001568 SDNode *Res = SelectARMCMOVSoImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001569 CCVal, CCR, InFlag);
1570 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001571 Res = SelectARMCMOVSoImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001572 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1573 if (Res)
1574 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001575 }
1576 }
1577
1578 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1579 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1580 // Pattern complexity = 6 cost = 1 size = 0
1581 //
1582 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1583 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1584 // Pattern complexity = 6 cost = 11 size = 0
1585 //
1586 // Also FCPYScc and FCPYDcc.
Evan Cheng9ef48352009-11-20 00:54:03 +00001587 SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1588 SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
Evan Cheng07ba9062009-11-19 21:45:22 +00001589 unsigned Opc = 0;
1590 switch (VT.getSimpleVT().SimpleTy) {
1591 default: assert(false && "Illegal conditional move type!");
1592 break;
1593 case MVT::i32:
1594 Opc = Subtarget->isThumb()
1595 ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1596 : ARM::MOVCCr;
1597 break;
1598 case MVT::f32:
1599 Opc = ARM::VMOVScc;
1600 break;
1601 case MVT::f64:
1602 Opc = ARM::VMOVDcc;
1603 break;
1604 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001605 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Cheng07ba9062009-11-19 21:45:22 +00001606}
1607
Evan Chengde8aa4e2010-05-05 18:28:36 +00001608SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
1609 // The only time a CONCAT_VECTORS operation can have legal types is when
1610 // two 64-bit vectors are concatenated to a 128-bit vector.
1611 EVT VT = N->getValueType(0);
1612 if (!VT.is128BitVector() || N->getNumOperands() != 2)
1613 llvm_unreachable("unexpected CONCAT_VECTORS");
1614 DebugLoc dl = N->getDebugLoc();
1615 SDValue V0 = N->getOperand(0);
1616 SDValue V1 = N->getOperand(1);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001617 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1618 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Evan Chengde8aa4e2010-05-05 18:28:36 +00001619 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1620 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1621}
1622
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001623SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
Dale Johannesened2eee62009-02-06 01:31:28 +00001624 DebugLoc dl = N->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001625
Dan Gohmane8be6c62008-07-17 19:10:17 +00001626 if (N->isMachineOpcode())
Evan Chenga8e29892007-01-19 07:51:42 +00001627 return NULL; // Already selected.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001628
1629 switch (N->getOpcode()) {
Evan Chenga8e29892007-01-19 07:51:42 +00001630 default: break;
1631 case ISD::Constant: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001632 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001633 bool UseCP = true;
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00001634 if (Subtarget->hasThumb2())
1635 // Thumb2-aware targets have the MOVT instruction, so all immediates can
1636 // be done with MOV + MOVT, at worst.
1637 UseCP = 0;
1638 else {
1639 if (Subtarget->isThumb()) {
Bob Wilsone64e3cf2009-06-22 17:29:13 +00001640 UseCP = (Val > 255 && // MOV
1641 ~Val > 255 && // MOV + MVN
1642 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00001643 } else
1644 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
1645 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
1646 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
1647 }
1648
Evan Chenga8e29892007-01-19 07:51:42 +00001649 if (UseCP) {
Dan Gohman475871a2008-07-27 21:46:04 +00001650 SDValue CPIdx =
Owen Anderson1d0be152009-08-13 21:58:54 +00001651 CurDAG->getTargetConstantPool(ConstantInt::get(
1652 Type::getInt32Ty(*CurDAG->getContext()), Val),
Evan Chenga8e29892007-01-19 07:51:42 +00001653 TLI.getPointerTy());
Evan Cheng012f2d92007-01-24 08:53:17 +00001654
1655 SDNode *ResNode;
Evan Cheng446c4282009-07-11 06:43:01 +00001656 if (Subtarget->isThumb1Only()) {
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001657 SDValue Pred = getAL(CurDAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00001658 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
Evan Cheng446c4282009-07-11 06:43:01 +00001659 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
Dan Gohman602b0c82009-09-25 18:54:59 +00001660 ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
1661 Ops, 4);
Evan Cheng446c4282009-07-11 06:43:01 +00001662 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001663 SDValue Ops[] = {
Jim Grosbach764ab522009-08-11 15:33:49 +00001664 CPIdx,
Owen Anderson825b72b2009-08-11 20:47:22 +00001665 CurDAG->getRegister(0, MVT::i32),
1666 CurDAG->getTargetConstant(0, MVT::i32),
Evan Chengee568cf2007-07-05 07:15:27 +00001667 getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +00001668 CurDAG->getRegister(0, MVT::i32),
Evan Cheng012f2d92007-01-24 08:53:17 +00001669 CurDAG->getEntryNode()
1670 };
Dan Gohman602b0c82009-09-25 18:54:59 +00001671 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
1672 Ops, 6);
Evan Cheng012f2d92007-01-24 08:53:17 +00001673 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001674 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
Evan Chenga8e29892007-01-19 07:51:42 +00001675 return NULL;
1676 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001677
Evan Chenga8e29892007-01-19 07:51:42 +00001678 // Other cases are autogenerated.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001679 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001680 }
Rafael Espindolaf819a492006-11-09 13:58:55 +00001681 case ISD::FrameIndex: {
Evan Chenga8e29892007-01-19 07:51:42 +00001682 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindolaf819a492006-11-09 13:58:55 +00001683 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohman475871a2008-07-27 21:46:04 +00001684 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
David Goodwinf1daf7d2009-07-08 23:10:31 +00001685 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001686 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
1687 CurDAG->getTargetConstant(0, MVT::i32));
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001688 } else {
David Goodwin419c6152009-07-14 18:48:51 +00001689 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
1690 ARM::t2ADDri : ARM::ADDri);
Owen Anderson825b72b2009-08-11 20:47:22 +00001691 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
1692 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1693 CurDAG->getRegister(0, MVT::i32) };
1694 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00001695 }
Evan Chenga8e29892007-01-19 07:51:42 +00001696 }
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001697 case ISD::SRL:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001698 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001699 return I;
1700 break;
1701 case ISD::SRA:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001702 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001703 return I;
1704 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001705 case ISD::MUL:
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001706 if (Subtarget->isThumb1Only())
Evan Cheng79d43262007-01-24 02:21:22 +00001707 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001708 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001709 unsigned RHSV = C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001710 if (!RHSV) break;
1711 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00001712 unsigned ShImm = Log2_32(RHSV-1);
1713 if (ShImm >= 32)
1714 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001715 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001716 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00001717 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1718 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00001719 if (Subtarget->isThumb()) {
Evan Chengaf9e7a72009-07-21 00:31:12 +00001720 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001721 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001722 } else {
1723 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001724 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001725 }
Evan Chenga8e29892007-01-19 07:51:42 +00001726 }
1727 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00001728 unsigned ShImm = Log2_32(RHSV+1);
1729 if (ShImm >= 32)
1730 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001731 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001732 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00001733 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1734 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00001735 if (Subtarget->isThumb()) {
Bob Wilson13ef8402010-05-28 00:27:15 +00001736 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1737 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001738 } else {
1739 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001740 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001741 }
Evan Chenga8e29892007-01-19 07:51:42 +00001742 }
1743 }
1744 break;
Evan Cheng20956592009-10-21 08:15:52 +00001745 case ISD::AND: {
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001746 // Check for unsigned bitfield extract
1747 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
1748 return I;
1749
Evan Cheng20956592009-10-21 08:15:52 +00001750 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
1751 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
1752 // are entirely contributed by c2 and lower 16-bits are entirely contributed
1753 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
1754 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001755 EVT VT = N->getValueType(0);
Evan Cheng20956592009-10-21 08:15:52 +00001756 if (VT != MVT::i32)
1757 break;
1758 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
1759 ? ARM::t2MOVTi16
1760 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
1761 if (!Opc)
1762 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001763 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Evan Cheng20956592009-10-21 08:15:52 +00001764 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1765 if (!N1C)
1766 break;
1767 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
1768 SDValue N2 = N0.getOperand(1);
1769 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1770 if (!N2C)
1771 break;
1772 unsigned N1CVal = N1C->getZExtValue();
1773 unsigned N2CVal = N2C->getZExtValue();
1774 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
1775 (N1CVal & 0xffffU) == 0xffffU &&
1776 (N2CVal & 0xffffU) == 0x0U) {
1777 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
1778 MVT::i32);
1779 SDValue Ops[] = { N0.getOperand(0), Imm16,
1780 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1781 return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
1782 }
1783 }
1784 break;
1785 }
Jim Grosbache5165492009-11-09 00:11:35 +00001786 case ARMISD::VMOVRRD:
1787 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001788 N->getOperand(0), getAL(CurDAG),
Dan Gohman602b0c82009-09-25 18:54:59 +00001789 CurDAG->getRegister(0, MVT::i32));
Dan Gohman525178c2007-10-08 18:33:35 +00001790 case ISD::UMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001791 if (Subtarget->isThumb1Only())
1792 break;
1793 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001794 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001795 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1796 CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00001797 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001798 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001799 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001800 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1801 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00001802 return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001803 }
Evan Chengee568cf2007-07-05 07:15:27 +00001804 }
Dan Gohman525178c2007-10-08 18:33:35 +00001805 case ISD::SMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001806 if (Subtarget->isThumb1Only())
1807 break;
1808 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001809 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001810 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00001811 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001812 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001813 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001814 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1815 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00001816 return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001817 }
Evan Chengee568cf2007-07-05 07:15:27 +00001818 }
Evan Chenga8e29892007-01-19 07:51:42 +00001819 case ISD::LOAD: {
Evan Chenge88d5ce2009-07-02 07:28:31 +00001820 SDNode *ResNode = 0;
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001821 if (Subtarget->isThumb() && Subtarget->hasThumb2())
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001822 ResNode = SelectT2IndexedLoad(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +00001823 else
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001824 ResNode = SelectARMIndexedLoad(N);
Evan Chengaf4550f2009-07-02 01:23:32 +00001825 if (ResNode)
1826 return ResNode;
Evan Chenga8e29892007-01-19 07:51:42 +00001827 // Other cases are autogenerated.
Rafael Espindolaf819a492006-11-09 13:58:55 +00001828 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001829 }
Evan Chengee568cf2007-07-05 07:15:27 +00001830 case ARMISD::BRCOND: {
1831 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1832 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1833 // Pattern complexity = 6 cost = 1 size = 0
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001834
Evan Chengee568cf2007-07-05 07:15:27 +00001835 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1836 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
1837 // Pattern complexity = 6 cost = 1 size = 0
1838
David Goodwin5e47a9a2009-06-30 18:04:13 +00001839 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1840 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1841 // Pattern complexity = 6 cost = 1 size = 0
1842
Jim Grosbach764ab522009-08-11 15:33:49 +00001843 unsigned Opc = Subtarget->isThumb() ?
David Goodwin5e47a9a2009-06-30 18:04:13 +00001844 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001845 SDValue Chain = N->getOperand(0);
1846 SDValue N1 = N->getOperand(1);
1847 SDValue N2 = N->getOperand(2);
1848 SDValue N3 = N->getOperand(3);
1849 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00001850 assert(N1.getOpcode() == ISD::BasicBlock);
1851 assert(N2.getOpcode() == ISD::Constant);
1852 assert(N3.getOpcode() == ISD::Register);
1853
Dan Gohman475871a2008-07-27 21:46:04 +00001854 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001855 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001857 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
Dan Gohman602b0c82009-09-25 18:54:59 +00001858 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
1859 MVT::Flag, Ops, 5);
Dan Gohman475871a2008-07-27 21:46:04 +00001860 Chain = SDValue(ResNode, 0);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001861 if (N->getNumValues() == 2) {
Dan Gohman475871a2008-07-27 21:46:04 +00001862 InFlag = SDValue(ResNode, 1);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001863 ReplaceUses(SDValue(N, 1), InFlag);
Chris Lattnera47b9bc2008-02-03 03:20:59 +00001864 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001865 ReplaceUses(SDValue(N, 0),
Evan Chenged54de42009-11-19 08:16:50 +00001866 SDValue(Chain.getNode(), Chain.getResNo()));
Evan Chengee568cf2007-07-05 07:15:27 +00001867 return NULL;
1868 }
Evan Cheng07ba9062009-11-19 21:45:22 +00001869 case ARMISD::CMOV:
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001870 return SelectCMOVOp(N);
Evan Chengee568cf2007-07-05 07:15:27 +00001871 case ARMISD::CNEG: {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001872 EVT VT = N->getValueType(0);
1873 SDValue N0 = N->getOperand(0);
1874 SDValue N1 = N->getOperand(1);
1875 SDValue N2 = N->getOperand(2);
1876 SDValue N3 = N->getOperand(3);
1877 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00001878 assert(N2.getOpcode() == ISD::Constant);
1879 assert(N3.getOpcode() == ISD::Register);
1880
Dan Gohman475871a2008-07-27 21:46:04 +00001881 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001882 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00001883 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001884 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
Evan Chengee568cf2007-07-05 07:15:27 +00001885 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001886 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengee568cf2007-07-05 07:15:27 +00001887 default: assert(false && "Illegal conditional move type!");
1888 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001889 case MVT::f32:
Jim Grosbache5165492009-11-09 00:11:35 +00001890 Opc = ARM::VNEGScc;
Evan Chengee568cf2007-07-05 07:15:27 +00001891 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001892 case MVT::f64:
Jim Grosbache5165492009-11-09 00:11:35 +00001893 Opc = ARM::VNEGDcc;
Evan Chenge5ad88e2008-12-10 21:54:21 +00001894 break;
Evan Chengee568cf2007-07-05 07:15:27 +00001895 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001896 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00001897 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00001898
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001899 case ARMISD::VZIP: {
1900 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001901 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001902 switch (VT.getSimpleVT().SimpleTy) {
1903 default: return NULL;
1904 case MVT::v8i8: Opc = ARM::VZIPd8; break;
1905 case MVT::v4i16: Opc = ARM::VZIPd16; break;
1906 case MVT::v2f32:
1907 case MVT::v2i32: Opc = ARM::VZIPd32; break;
1908 case MVT::v16i8: Opc = ARM::VZIPq8; break;
1909 case MVT::v8i16: Opc = ARM::VZIPq16; break;
1910 case MVT::v4f32:
1911 case MVT::v4i32: Opc = ARM::VZIPq32; break;
1912 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001913 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001914 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1915 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1916 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001917 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001918 case ARMISD::VUZP: {
1919 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001920 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001921 switch (VT.getSimpleVT().SimpleTy) {
1922 default: return NULL;
1923 case MVT::v8i8: Opc = ARM::VUZPd8; break;
1924 case MVT::v4i16: Opc = ARM::VUZPd16; break;
1925 case MVT::v2f32:
1926 case MVT::v2i32: Opc = ARM::VUZPd32; break;
1927 case MVT::v16i8: Opc = ARM::VUZPq8; break;
1928 case MVT::v8i16: Opc = ARM::VUZPq16; break;
1929 case MVT::v4f32:
1930 case MVT::v4i32: Opc = ARM::VUZPq32; break;
1931 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001932 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001933 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1934 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1935 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001936 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001937 case ARMISD::VTRN: {
1938 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001939 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001940 switch (VT.getSimpleVT().SimpleTy) {
1941 default: return NULL;
1942 case MVT::v8i8: Opc = ARM::VTRNd8; break;
1943 case MVT::v4i16: Opc = ARM::VTRNd16; break;
1944 case MVT::v2f32:
1945 case MVT::v2i32: Opc = ARM::VTRNd32; break;
1946 case MVT::v16i8: Opc = ARM::VTRNq8; break;
1947 case MVT::v8i16: Opc = ARM::VTRNq16; break;
1948 case MVT::v4f32:
1949 case MVT::v4i32: Opc = ARM::VTRNq32; break;
1950 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001951 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001952 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1953 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1954 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001955 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001956 case ARMISD::BUILD_VECTOR: {
1957 EVT VecVT = N->getValueType(0);
1958 EVT EltVT = VecVT.getVectorElementType();
1959 unsigned NumElts = VecVT.getVectorNumElements();
1960 if (EltVT.getSimpleVT() == MVT::f64) {
1961 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
1962 return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
1963 }
1964 assert(EltVT.getSimpleVT() == MVT::f32 &&
1965 "unexpected type for BUILD_VECTOR");
1966 if (NumElts == 2)
1967 return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
1968 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
1969 return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
1970 N->getOperand(2), N->getOperand(3));
1971 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00001972
1973 case ISD::INTRINSIC_VOID:
1974 case ISD::INTRINSIC_W_CHAIN: {
1975 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Bob Wilson31fb12f2009-08-26 17:39:53 +00001976 switch (IntNo) {
1977 default:
Bob Wilson429009b2010-05-06 16:05:26 +00001978 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00001979
Bob Wilson621f1952010-03-23 05:25:43 +00001980 case Intrinsic::arm_neon_vld1: {
1981 unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
1982 ARM::VLD1d32, ARM::VLD1d64 };
Bob Wilsonffde0802010-09-02 16:00:54 +00001983 unsigned QOpcodes[] = { ARM::VLD1q8Pseudo, ARM::VLD1q16Pseudo,
1984 ARM::VLD1q32Pseudo, ARM::VLD1q64Pseudo };
Bob Wilson621f1952010-03-23 05:25:43 +00001985 return SelectVLD(N, 1, DOpcodes, QOpcodes, 0);
1986 }
1987
Bob Wilson31fb12f2009-08-26 17:39:53 +00001988 case Intrinsic::arm_neon_vld2: {
Bob Wilsonffde0802010-09-02 16:00:54 +00001989 unsigned DOpcodes[] = { ARM::VLD2d8Pseudo, ARM::VLD2d16Pseudo,
1990 ARM::VLD2d32Pseudo, ARM::VLD1q64Pseudo };
1991 unsigned QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
1992 ARM::VLD2q32Pseudo };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001993 return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00001994 }
1995
1996 case Intrinsic::arm_neon_vld3: {
Bob Wilsonf5721912010-09-03 18:16:02 +00001997 unsigned DOpcodes[] = { ARM::VLD3d8Pseudo, ARM::VLD3d16Pseudo,
1998 ARM::VLD3d32Pseudo, ARM::VLD1d64TPseudo };
1999 unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
2000 ARM::VLD3q16Pseudo_UPD,
2001 ARM::VLD3q32Pseudo_UPD };
2002 unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
2003 ARM::VLD3q16oddPseudo_UPD,
2004 ARM::VLD3q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002005 return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002006 }
2007
2008 case Intrinsic::arm_neon_vld4: {
Bob Wilsonf5721912010-09-03 18:16:02 +00002009 unsigned DOpcodes[] = { ARM::VLD4d8Pseudo, ARM::VLD4d16Pseudo,
2010 ARM::VLD4d32Pseudo, ARM::VLD1d64QPseudo };
2011 unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
2012 ARM::VLD4q16Pseudo_UPD,
2013 ARM::VLD4q32Pseudo_UPD };
2014 unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
2015 ARM::VLD4q16oddPseudo_UPD,
2016 ARM::VLD4q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002017 return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002018 }
2019
Bob Wilson243fcc52009-09-01 04:26:28 +00002020 case Intrinsic::arm_neon_vld2lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002021 unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo, ARM::VLD2LNd16Pseudo,
2022 ARM::VLD2LNd32Pseudo };
2023 unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo, ARM::VLD2LNq32Pseudo };
2024 return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002025 }
2026
2027 case Intrinsic::arm_neon_vld3lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002028 unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo, ARM::VLD3LNd16Pseudo,
2029 ARM::VLD3LNd32Pseudo };
2030 unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo, ARM::VLD3LNq32Pseudo };
2031 return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002032 }
2033
2034 case Intrinsic::arm_neon_vld4lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002035 unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo, ARM::VLD4LNd16Pseudo,
2036 ARM::VLD4LNd32Pseudo };
2037 unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo, ARM::VLD4LNq32Pseudo };
2038 return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002039 }
2040
Bob Wilson11d98992010-03-23 06:20:33 +00002041 case Intrinsic::arm_neon_vst1: {
2042 unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2043 ARM::VST1d32, ARM::VST1d64 };
Bob Wilsone5ce4f62010-08-28 05:12:57 +00002044 unsigned QOpcodes[] = { ARM::VST1q8Pseudo, ARM::VST1q16Pseudo,
2045 ARM::VST1q32Pseudo, ARM::VST1q64Pseudo };
Bob Wilson11d98992010-03-23 06:20:33 +00002046 return SelectVST(N, 1, DOpcodes, QOpcodes, 0);
2047 }
2048
Bob Wilson31fb12f2009-08-26 17:39:53 +00002049 case Intrinsic::arm_neon_vst2: {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00002050 unsigned DOpcodes[] = { ARM::VST2d8Pseudo, ARM::VST2d16Pseudo,
2051 ARM::VST2d32Pseudo, ARM::VST1q64Pseudo };
2052 unsigned QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
2053 ARM::VST2q32Pseudo };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002054 return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002055 }
2056
2057 case Intrinsic::arm_neon_vst3: {
Bob Wilson01ba4612010-08-26 18:51:29 +00002058 unsigned DOpcodes[] = { ARM::VST3d8Pseudo, ARM::VST3d16Pseudo,
2059 ARM::VST3d32Pseudo, ARM::VST1d64TPseudo };
2060 unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2061 ARM::VST3q16Pseudo_UPD,
2062 ARM::VST3q32Pseudo_UPD };
2063 unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2064 ARM::VST3q16oddPseudo_UPD,
2065 ARM::VST3q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002066 return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002067 }
2068
2069 case Intrinsic::arm_neon_vst4: {
Bob Wilson709d5922010-08-25 23:27:42 +00002070 unsigned DOpcodes[] = { ARM::VST4d8Pseudo, ARM::VST4d16Pseudo,
Bob Wilson70e48b22010-08-26 05:33:30 +00002071 ARM::VST4d32Pseudo, ARM::VST1d64QPseudo };
Bob Wilson709d5922010-08-25 23:27:42 +00002072 unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2073 ARM::VST4q16Pseudo_UPD,
2074 ARM::VST4q32Pseudo_UPD };
2075 unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2076 ARM::VST4q16oddPseudo_UPD,
2077 ARM::VST4q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002078 return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002079 }
Bob Wilson8a3198b2009-09-01 18:51:56 +00002080
2081 case Intrinsic::arm_neon_vst2lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002082 unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo, ARM::VST2LNd16Pseudo,
2083 ARM::VST2LNd32Pseudo };
2084 unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo, ARM::VST2LNq32Pseudo };
2085 return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002086 }
2087
2088 case Intrinsic::arm_neon_vst3lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002089 unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo, ARM::VST3LNd16Pseudo,
2090 ARM::VST3LNd32Pseudo };
2091 unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo, ARM::VST3LNq32Pseudo };
2092 return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002093 }
2094
2095 case Intrinsic::arm_neon_vst4lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002096 unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo, ARM::VST4LNd16Pseudo,
2097 ARM::VST4LNd32Pseudo };
2098 unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo, ARM::VST4LNq32Pseudo };
2099 return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002100 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00002101 }
Bob Wilson429009b2010-05-06 16:05:26 +00002102 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00002103 }
Evan Chengde8aa4e2010-05-05 18:28:36 +00002104
Bob Wilsond491d6e2010-07-06 23:36:25 +00002105 case ISD::INTRINSIC_WO_CHAIN: {
2106 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2107 switch (IntNo) {
2108 default:
2109 break;
2110
2111 case Intrinsic::arm_neon_vtbl2:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002112 return SelectVTBL(N, false, 2, ARM::VTBL2Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002113 case Intrinsic::arm_neon_vtbl3:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002114 return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002115 case Intrinsic::arm_neon_vtbl4:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002116 return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002117
2118 case Intrinsic::arm_neon_vtbx2:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002119 return SelectVTBL(N, true, 2, ARM::VTBX2Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002120 case Intrinsic::arm_neon_vtbx3:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002121 return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002122 case Intrinsic::arm_neon_vtbx4:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002123 return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002124 }
2125 break;
2126 }
2127
Bob Wilson429009b2010-05-06 16:05:26 +00002128 case ISD::CONCAT_VECTORS:
Evan Chengde8aa4e2010-05-05 18:28:36 +00002129 return SelectConcatVector(N);
2130 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00002131
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002132 return SelectCode(N);
Evan Chenga8e29892007-01-19 07:51:42 +00002133}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002134
Bob Wilson224c2442009-05-19 05:53:42 +00002135bool ARMDAGToDAGISel::
2136SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2137 std::vector<SDValue> &OutOps) {
2138 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
Bob Wilson765cc0b2009-10-13 20:50:28 +00002139 // Require the address to be in a register. That is safe for all ARM
2140 // variants and it is hard to do anything much smarter without knowing
2141 // how the operand is used.
2142 OutOps.push_back(Op);
Bob Wilson224c2442009-05-19 05:53:42 +00002143 return false;
2144}
2145
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002146/// createARMISelDag - This pass converts a legalized DAG into a
2147/// ARM-specific DAG, ready for instruction scheduling.
2148///
Bob Wilson522ce972009-09-28 14:30:20 +00002149FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2150 CodeGenOpt::Level OptLevel) {
2151 return new ARMDAGToDAGISel(TM, OptLevel);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002152}