blob: a3b86cb9dbd8b7416a06b6fa2ea12709b3e9d494 [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 {
Jim Grosbach82891622010-09-29 19:03:54 +000049
50enum AddrMode2Type {
51 AM2_BASE, // Simple AM2 (+-imm12)
52 AM2_SHOP // Shifter-op AM2
53};
54
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000055class ARMDAGToDAGISel : public SelectionDAGISel {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000056 ARMBaseTargetMachine &TM;
Evan Cheng3f7eb8e2008-09-18 07:24:33 +000057
Evan Chenga8e29892007-01-19 07:51:42 +000058 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
59 /// make the right decision when generating code for different targets.
60 const ARMSubtarget *Subtarget;
61
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000062public:
Bob Wilson522ce972009-09-28 14:30:20 +000063 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm,
64 CodeGenOpt::Level OptLevel)
65 : SelectionDAGISel(tm, OptLevel), TM(tm),
Evan Chenga8e29892007-01-19 07:51:42 +000066 Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000067 }
68
Evan Chenga8e29892007-01-19 07:51:42 +000069 virtual const char *getPassName() const {
70 return "ARM Instruction Selection";
Anton Korobeynikov52237112009-06-17 18:13:58 +000071 }
72
Bob Wilsonaf4a8912009-10-08 18:51:31 +000073 /// getI32Imm - Return a target constant of type i32 with the specified
74 /// value.
Anton Korobeynikov52237112009-06-17 18:13:58 +000075 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +000076 return CurDAG->getTargetConstant(Imm, MVT::i32);
Anton Korobeynikov52237112009-06-17 18:13:58 +000077 }
78
Dan Gohmaneeb3a002010-01-05 01:24:18 +000079 SDNode *Select(SDNode *N);
Evan Cheng014bf212010-02-15 19:41:07 +000080
Evan Chengf40deed2010-10-27 23:41:30 +000081 bool isShifterOpProfitable(const SDValue &Shift,
82 ARM_AM::ShiftOpc ShOpcVal, unsigned ShAmt);
Chris Lattner52a261b2010-09-21 20:31:19 +000083 bool SelectShifterOperandReg(SDValue N, SDValue &A,
Evan Cheng055b0312009-06-29 07:51:04 +000084 SDValue &B, SDValue &C);
Evan Chengf40deed2010-10-27 23:41:30 +000085 bool SelectShiftShifterOperandReg(SDValue N, SDValue &A,
86 SDValue &B, SDValue &C);
Jim Grosbach3e556122010-10-26 22:37:02 +000087 bool SelectAddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
88 bool SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset, SDValue &Opc);
89
Jim Grosbach82891622010-09-29 19:03:54 +000090 AddrMode2Type SelectAddrMode2Worker(SDValue N, SDValue &Base,
91 SDValue &Offset, SDValue &Opc);
92 bool SelectAddrMode2Base(SDValue N, SDValue &Base, SDValue &Offset,
93 SDValue &Opc) {
94 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_BASE;
95 }
96
97 bool SelectAddrMode2ShOp(SDValue N, SDValue &Base, SDValue &Offset,
98 SDValue &Opc) {
99 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_SHOP;
100 }
101
102 bool SelectAddrMode2(SDValue N, SDValue &Base, SDValue &Offset,
103 SDValue &Opc) {
104 SelectAddrMode2Worker(N, Base, Offset, Opc);
Jim Grosbach3e556122010-10-26 22:37:02 +0000105// return SelectAddrMode2ShOp(N, Base, Offset, Opc);
Jim Grosbach82891622010-09-29 19:03:54 +0000106 // This always matches one way or another.
107 return true;
108 }
109
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000110 bool SelectAddrMode2Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000111 SDValue &Offset, SDValue &Opc);
Chris Lattner52a261b2010-09-21 20:31:19 +0000112 bool SelectAddrMode3(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000113 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000114 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000115 SDValue &Offset, SDValue &Opc);
Chris Lattner52a261b2010-09-21 20:31:19 +0000116 bool SelectAddrMode5(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000117 SDValue &Offset);
Bob Wilson665814b2010-11-01 23:40:51 +0000118 bool SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,SDValue &Align);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000119
Chris Lattner52a261b2010-09-21 20:31:19 +0000120 bool SelectAddrModePC(SDValue N, SDValue &Offset,
Bob Wilson8b024a52009-07-01 23:16:05 +0000121 SDValue &Label);
Evan Chenga8e29892007-01-19 07:51:42 +0000122
Chris Lattner52a261b2010-09-21 20:31:19 +0000123 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
124 bool SelectThumbAddrModeRI5(SDValue N, unsigned Scale,
Dan Gohman475871a2008-07-27 21:46:04 +0000125 SDValue &Base, SDValue &OffImm,
126 SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +0000127 bool SelectThumbAddrModeS1(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000128 SDValue &OffImm, SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +0000129 bool SelectThumbAddrModeS2(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000130 SDValue &OffImm, SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +0000131 bool SelectThumbAddrModeS4(SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000132 SDValue &OffImm, SDValue &Offset);
Chris Lattner52a261b2010-09-21 20:31:19 +0000133 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000134
Chris Lattner52a261b2010-09-21 20:31:19 +0000135 bool SelectT2ShifterOperandReg(SDValue N,
Evan Cheng9cb9e672009-06-27 02:26:13 +0000136 SDValue &BaseReg, SDValue &Opc);
Chris Lattner52a261b2010-09-21 20:31:19 +0000137 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
138 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
Evan Cheng055b0312009-06-29 07:51:04 +0000139 SDValue &OffImm);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000140 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Chenge88d5ce2009-07-02 07:28:31 +0000141 SDValue &OffImm);
Chris Lattner52a261b2010-09-21 20:31:19 +0000142 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
Evan Cheng055b0312009-06-29 07:51:04 +0000143 SDValue &OffReg, SDValue &ShImm);
144
Evan Cheng875a6ac2010-11-12 22:42:47 +0000145 inline bool is_so_imm(unsigned Imm) const {
146 return ARM_AM::getSOImmVal(Imm) != -1;
147 }
148
149 inline bool is_so_imm_not(unsigned Imm) const {
150 return ARM_AM::getSOImmVal(~Imm) != -1;
151 }
152
153 inline bool is_t2_so_imm(unsigned Imm) const {
154 return ARM_AM::getT2SOImmVal(Imm) != -1;
155 }
156
157 inline bool is_t2_so_imm_not(unsigned Imm) const {
158 return ARM_AM::getT2SOImmVal(~Imm) != -1;
159 }
160
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +0000161 inline bool Pred_so_imm(SDNode *inN) const {
162 ConstantSDNode *N = cast<ConstantSDNode>(inN);
Evan Cheng875a6ac2010-11-12 22:42:47 +0000163 return is_so_imm(N->getZExtValue());
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +0000164 }
165
166 inline bool Pred_t2_so_imm(SDNode *inN) const {
167 ConstantSDNode *N = cast<ConstantSDNode>(inN);
Evan Cheng875a6ac2010-11-12 22:42:47 +0000168 return is_t2_so_imm(N->getZExtValue());
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +0000169 }
170
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000171 // Include the pieces autogenerated from the target description.
172#include "ARMGenDAGISel.inc"
Bob Wilson224c2442009-05-19 05:53:42 +0000173
174private:
Evan Chenge88d5ce2009-07-02 07:28:31 +0000175 /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
176 /// ARM.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000177 SDNode *SelectARMIndexedLoad(SDNode *N);
178 SDNode *SelectT2IndexedLoad(SDNode *N);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000179
Bob Wilson621f1952010-03-23 05:25:43 +0000180 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
181 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson3e36f132009-10-14 17:28:52 +0000182 /// loads of D registers and even subregs and odd subregs of Q registers.
Bob Wilson621f1952010-03-23 05:25:43 +0000183 /// For NumVecs <= 2, QOpcodes1 is not used.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000184 SDNode *SelectVLD(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson3e36f132009-10-14 17:28:52 +0000185 unsigned *QOpcodes0, unsigned *QOpcodes1);
186
Bob Wilson24f995d2009-10-14 18:32:29 +0000187 /// SelectVST - Select NEON store intrinsics. NumVecs should
Bob Wilson11d98992010-03-23 06:20:33 +0000188 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson24f995d2009-10-14 18:32:29 +0000189 /// stores of D registers and even subregs and odd subregs of Q registers.
Bob Wilson11d98992010-03-23 06:20:33 +0000190 /// For NumVecs <= 2, QOpcodes1 is not used.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000191 SDNode *SelectVST(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson24f995d2009-10-14 18:32:29 +0000192 unsigned *QOpcodes0, unsigned *QOpcodes1);
193
Bob Wilson96493442009-10-14 16:46:45 +0000194 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
Bob Wilsona7c397c2009-10-14 16:19:03 +0000195 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson8466fa12010-09-13 23:01:35 +0000196 /// load/store of D registers and Q registers.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000197 SDNode *SelectVLDSTLane(SDNode *N, bool IsLoad, unsigned NumVecs,
Bob Wilson8466fa12010-09-13 23:01:35 +0000198 unsigned *DOpcodes, unsigned *QOpcodes);
Bob Wilsona7c397c2009-10-14 16:19:03 +0000199
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000200 /// SelectVLDDup - Select NEON load-duplicate intrinsics. NumVecs
201 /// should be 2, 3 or 4. The opcode array specifies the instructions used
202 /// for loading D registers. (Q registers are not supported.)
203 SDNode *SelectVLDDup(SDNode *N, unsigned NumVecs, unsigned *Opcodes);
204
Bob Wilson78dfbc32010-07-07 00:08:54 +0000205 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2,
206 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be
207 /// generated to force the table registers to be consecutive.
208 SDNode *SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
Bob Wilsond491d6e2010-07-06 23:36:25 +0000209
Sandeep Patel4e1ed882009-10-13 20:25:58 +0000210 /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
Jim Grosbach3a1287b2010-04-22 23:24:18 +0000211 SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
Sandeep Patel47eedaa2009-10-13 18:59:48 +0000212
Evan Cheng07ba9062009-11-19 21:45:22 +0000213 /// SelectCMOVOp - Select CMOV instructions for ARM.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000214 SDNode *SelectCMOVOp(SDNode *N);
215 SDNode *SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000216 ARMCC::CondCodes CCVal, SDValue CCR,
217 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000218 SDNode *SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000219 ARMCC::CondCodes CCVal, SDValue CCR,
220 SDValue InFlag);
Jim Grosbacha4257162010-10-07 00:53:56 +0000221 SDNode *SelectT2CMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000222 ARMCC::CondCodes CCVal, SDValue CCR,
223 SDValue InFlag);
Jim Grosbach3bbdcea2010-10-07 00:42:42 +0000224 SDNode *SelectARMCMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000225 ARMCC::CondCodes CCVal, SDValue CCR,
226 SDValue InFlag);
Evan Cheng07ba9062009-11-19 21:45:22 +0000227
Evan Chengde8aa4e2010-05-05 18:28:36 +0000228 SDNode *SelectConcatVector(SDNode *N);
229
Evan Chengaf4550f2009-07-02 01:23:32 +0000230 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
231 /// inline asm expressions.
232 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
233 char ConstraintCode,
234 std::vector<SDValue> &OutOps);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000235
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000236 // Form pairs of consecutive S, D, or Q registers.
237 SDNode *PairSRegs(EVT VT, SDValue V0, SDValue V1);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000238 SDNode *PairDRegs(EVT VT, SDValue V0, SDValue V1);
Evan Cheng603afbf2010-05-10 17:34:18 +0000239 SDNode *PairQRegs(EVT VT, SDValue V0, SDValue V1);
240
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000241 // Form sequences of 4 consecutive S, D, or Q registers.
242 SDNode *QuadSRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Evan Cheng603afbf2010-05-10 17:34:18 +0000243 SDNode *QuadDRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Evan Cheng8f6de382010-05-16 03:27:48 +0000244 SDNode *QuadQRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Bob Wilson665814b2010-11-01 23:40:51 +0000245
246 // Get the alignment operand for a NEON VLD or VST instruction.
247 SDValue GetVLDSTAlign(SDValue Align, unsigned NumVecs, bool is64BitVector);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000248};
Evan Chenga8e29892007-01-19 07:51:42 +0000249}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000250
Sandeep Patel47eedaa2009-10-13 18:59:48 +0000251/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
252/// operand. If so Imm will receive the 32-bit value.
253static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
254 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
255 Imm = cast<ConstantSDNode>(N)->getZExtValue();
256 return true;
257 }
258 return false;
259}
260
261// isInt32Immediate - This method tests to see if a constant operand.
262// If so Imm will receive the 32 bit value.
263static bool isInt32Immediate(SDValue N, unsigned &Imm) {
264 return isInt32Immediate(N.getNode(), Imm);
265}
266
267// isOpcWithIntImmediate - This method tests to see if the node is a specific
268// opcode and that it has a immediate integer right operand.
269// If so Imm will receive the 32 bit value.
270static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
271 return N->getOpcode() == Opc &&
272 isInt32Immediate(N->getOperand(1).getNode(), Imm);
273}
274
275
Evan Chengf40deed2010-10-27 23:41:30 +0000276bool ARMDAGToDAGISel::isShifterOpProfitable(const SDValue &Shift,
277 ARM_AM::ShiftOpc ShOpcVal,
278 unsigned ShAmt) {
279 if (!Subtarget->isCortexA9())
280 return true;
281 if (Shift.hasOneUse())
282 return true;
283 // R << 2 is free.
284 return ShOpcVal == ARM_AM::lsl && ShAmt == 2;
285}
286
Chris Lattner52a261b2010-09-21 20:31:19 +0000287bool ARMDAGToDAGISel::SelectShifterOperandReg(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000288 SDValue &BaseReg,
289 SDValue &ShReg,
290 SDValue &Opc) {
Evan Chenga2c519b2010-07-30 23:33:54 +0000291 if (DisableShifterOp)
292 return false;
293
Evan Cheng055b0312009-06-29 07:51:04 +0000294 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
295
296 // Don't match base register only case. That is matched to a separate
297 // lower complexity pattern with explicit register operand.
298 if (ShOpcVal == ARM_AM::no_shift) return false;
Jim Grosbach764ab522009-08-11 15:33:49 +0000299
Evan Cheng055b0312009-06-29 07:51:04 +0000300 BaseReg = N.getOperand(0);
301 unsigned ShImmVal = 0;
302 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 ShReg = CurDAG->getRegister(0, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000304 ShImmVal = RHS->getZExtValue() & 31;
305 } else {
306 ShReg = N.getOperand(1);
Evan Chengf40deed2010-10-27 23:41:30 +0000307 if (!isShifterOpProfitable(N, ShOpcVal, ShImmVal))
308 return false;
309 }
310 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
311 MVT::i32);
312 return true;
313}
314
315bool ARMDAGToDAGISel::SelectShiftShifterOperandReg(SDValue N,
316 SDValue &BaseReg,
317 SDValue &ShReg,
318 SDValue &Opc) {
319 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
320
321 // Don't match base register only case. That is matched to a separate
322 // lower complexity pattern with explicit register operand.
323 if (ShOpcVal == ARM_AM::no_shift) return false;
324
325 BaseReg = N.getOperand(0);
326 unsigned ShImmVal = 0;
327 // Do not check isShifterOpProfitable. This must return true.
328 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
329 ShReg = CurDAG->getRegister(0, MVT::i32);
330 ShImmVal = RHS->getZExtValue() & 31;
331 } else {
332 ShReg = N.getOperand(1);
Evan Cheng055b0312009-06-29 07:51:04 +0000333 }
334 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000336 return true;
337}
338
Jim Grosbach3e556122010-10-26 22:37:02 +0000339bool ARMDAGToDAGISel::SelectAddrModeImm12(SDValue N,
340 SDValue &Base,
341 SDValue &OffImm) {
342 // Match simple R + imm12 operands.
343
344 // Base only.
345 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
346 if (N.getOpcode() == ISD::FrameIndex) {
347 // Match frame index...
348 int FI = cast<FrameIndexSDNode>(N)->getIndex();
349 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
350 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
351 return true;
352 } else if (N.getOpcode() == ARMISD::Wrapper &&
353 !(Subtarget->useMovt() &&
354 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
355 Base = N.getOperand(0);
356 } else
357 Base = N;
358 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
359 return true;
360 }
361
362 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
363 int RHSC = (int)RHS->getZExtValue();
364 if (N.getOpcode() == ISD::SUB)
365 RHSC = -RHSC;
366
367 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
368 Base = N.getOperand(0);
369 if (Base.getOpcode() == ISD::FrameIndex) {
370 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
371 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
372 }
373 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
374 return true;
375 }
376 }
377
378 // Base only.
379 Base = N;
380 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
381 return true;
382}
383
384
385
386bool ARMDAGToDAGISel::SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset,
387 SDValue &Opc) {
Evan Chengf40deed2010-10-27 23:41:30 +0000388 if (N.getOpcode() == ISD::MUL &&
389 (!Subtarget->isCortexA9() || N.hasOneUse())) {
Jim Grosbach3e556122010-10-26 22:37:02 +0000390 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
391 // X * [3,5,9] -> X + X * [2,4,8] etc.
392 int RHSC = (int)RHS->getZExtValue();
393 if (RHSC & 1) {
394 RHSC = RHSC & ~1;
395 ARM_AM::AddrOpc AddSub = ARM_AM::add;
396 if (RHSC < 0) {
397 AddSub = ARM_AM::sub;
398 RHSC = - RHSC;
399 }
400 if (isPowerOf2_32(RHSC)) {
401 unsigned ShAmt = Log2_32(RHSC);
402 Base = Offset = N.getOperand(0);
403 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
404 ARM_AM::lsl),
405 MVT::i32);
406 return true;
407 }
408 }
409 }
410 }
411
412 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB)
413 return false;
414
415 // Leave simple R +/- imm12 operands for LDRi12
416 if (N.getOpcode() == ISD::ADD) {
417 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
418 int RHSC = (int)RHS->getZExtValue();
419 if ((RHSC >= 0 && RHSC < 0x1000) ||
420 (RHSC < 0 && RHSC > -0x1000)) // 12 bits.
421 return false;
422 }
423 }
424
Evan Chengf40deed2010-10-27 23:41:30 +0000425 if (Subtarget->isCortexA9() && !N.hasOneUse())
426 // Compute R +/- (R << N) and reuse it.
427 return false;
428
Jim Grosbach3e556122010-10-26 22:37:02 +0000429 // Otherwise this is R +/- [possibly shifted] R.
430 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
431 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
432 unsigned ShAmt = 0;
433
434 Base = N.getOperand(0);
435 Offset = N.getOperand(1);
436
437 if (ShOpcVal != ARM_AM::no_shift) {
438 // Check to see if the RHS of the shift is a constant, if not, we can't fold
439 // it.
440 if (ConstantSDNode *Sh =
441 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
442 ShAmt = Sh->getZExtValue();
Evan Chengf40deed2010-10-27 23:41:30 +0000443 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
444 Offset = N.getOperand(1).getOperand(0);
445 else {
446 ShAmt = 0;
447 ShOpcVal = ARM_AM::no_shift;
448 }
Jim Grosbach3e556122010-10-26 22:37:02 +0000449 } else {
450 ShOpcVal = ARM_AM::no_shift;
451 }
452 }
453
454 // Try matching (R shl C) + (R).
Evan Chengf40deed2010-10-27 23:41:30 +0000455 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift &&
456 !(Subtarget->isCortexA9() || N.getOperand(0).hasOneUse())) {
Jim Grosbach3e556122010-10-26 22:37:02 +0000457 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
458 if (ShOpcVal != ARM_AM::no_shift) {
459 // Check to see if the RHS of the shift is a constant, if not, we can't
460 // fold it.
461 if (ConstantSDNode *Sh =
462 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
463 ShAmt = Sh->getZExtValue();
Evan Chengf40deed2010-10-27 23:41:30 +0000464 if (!Subtarget->isCortexA9() ||
465 (N.hasOneUse() &&
466 isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt))) {
467 Offset = N.getOperand(0).getOperand(0);
468 Base = N.getOperand(1);
469 } else {
470 ShAmt = 0;
471 ShOpcVal = ARM_AM::no_shift;
472 }
Jim Grosbach3e556122010-10-26 22:37:02 +0000473 } else {
474 ShOpcVal = ARM_AM::no_shift;
475 }
476 }
477 }
478
479 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
480 MVT::i32);
481 return true;
482}
483
484
485
486
487//-----
488
Jim Grosbach82891622010-09-29 19:03:54 +0000489AddrMode2Type ARMDAGToDAGISel::SelectAddrMode2Worker(SDValue N,
490 SDValue &Base,
491 SDValue &Offset,
492 SDValue &Opc) {
Evan Chengf40deed2010-10-27 23:41:30 +0000493 if (N.getOpcode() == ISD::MUL &&
494 (!Subtarget->isCortexA9() || N.hasOneUse())) {
Evan Chenga13fd102007-03-13 21:05:54 +0000495 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
496 // X * [3,5,9] -> X + X * [2,4,8] etc.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000497 int RHSC = (int)RHS->getZExtValue();
Evan Chenga13fd102007-03-13 21:05:54 +0000498 if (RHSC & 1) {
499 RHSC = RHSC & ~1;
500 ARM_AM::AddrOpc AddSub = ARM_AM::add;
501 if (RHSC < 0) {
502 AddSub = ARM_AM::sub;
503 RHSC = - RHSC;
504 }
505 if (isPowerOf2_32(RHSC)) {
506 unsigned ShAmt = Log2_32(RHSC);
507 Base = Offset = N.getOperand(0);
508 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
509 ARM_AM::lsl),
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 MVT::i32);
Jim Grosbach82891622010-09-29 19:03:54 +0000511 return AM2_SHOP;
Evan Chenga13fd102007-03-13 21:05:54 +0000512 }
513 }
514 }
515 }
516
Evan Chenga8e29892007-01-19 07:51:42 +0000517 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
518 Base = N;
519 if (N.getOpcode() == ISD::FrameIndex) {
520 int FI = cast<FrameIndexSDNode>(N)->getIndex();
521 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000522 } else if (N.getOpcode() == ARMISD::Wrapper &&
523 !(Subtarget->useMovt() &&
524 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000525 Base = N.getOperand(0);
526 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000528 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
529 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 MVT::i32);
Jim Grosbach82891622010-09-29 19:03:54 +0000531 return AM2_BASE;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000532 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000533
Evan Chenga8e29892007-01-19 07:51:42 +0000534 // Match simple R +/- imm12 operands.
Jim Grosbachbe912322010-09-29 17:32:29 +0000535 if (N.getOpcode() == ISD::ADD) {
Evan Chenga8e29892007-01-19 07:51:42 +0000536 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000537 int RHSC = (int)RHS->getZExtValue();
Evan Chenge966d642007-01-24 02:45:25 +0000538 if ((RHSC >= 0 && RHSC < 0x1000) ||
539 (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
Evan Chenga8e29892007-01-19 07:51:42 +0000540 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000541 if (Base.getOpcode() == ISD::FrameIndex) {
542 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
543 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
544 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000546
547 ARM_AM::AddrOpc AddSub = ARM_AM::add;
548 if (RHSC < 0) {
549 AddSub = ARM_AM::sub;
550 RHSC = - RHSC;
551 }
552 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
Evan Chenga8e29892007-01-19 07:51:42 +0000553 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 MVT::i32);
Jim Grosbach82891622010-09-29 19:03:54 +0000555 return AM2_BASE;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000556 }
Evan Chenga8e29892007-01-19 07:51:42 +0000557 }
Jim Grosbachbe912322010-09-29 17:32:29 +0000558 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000559
Evan Chengf40deed2010-10-27 23:41:30 +0000560 if (Subtarget->isCortexA9() && !N.hasOneUse()) {
561 // Compute R +/- (R << N) and reuse it.
562 Base = N;
563 Offset = CurDAG->getRegister(0, MVT::i32);
564 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
565 ARM_AM::no_shift),
566 MVT::i32);
567 return AM2_BASE;
568 }
569
Johnny Chen6a3b5ee2009-10-27 17:25:15 +0000570 // Otherwise this is R +/- [possibly shifted] R.
Evan Chenga8e29892007-01-19 07:51:42 +0000571 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
572 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
573 unsigned ShAmt = 0;
Jim Grosbach764ab522009-08-11 15:33:49 +0000574
Evan Chenga8e29892007-01-19 07:51:42 +0000575 Base = N.getOperand(0);
576 Offset = N.getOperand(1);
Jim Grosbach764ab522009-08-11 15:33:49 +0000577
Evan Chenga8e29892007-01-19 07:51:42 +0000578 if (ShOpcVal != ARM_AM::no_shift) {
579 // Check to see if the RHS of the shift is a constant, if not, we can't fold
580 // it.
581 if (ConstantSDNode *Sh =
582 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000583 ShAmt = Sh->getZExtValue();
Evan Chengf40deed2010-10-27 23:41:30 +0000584 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
585 Offset = N.getOperand(1).getOperand(0);
586 else {
587 ShAmt = 0;
588 ShOpcVal = ARM_AM::no_shift;
589 }
Evan Chenga8e29892007-01-19 07:51:42 +0000590 } else {
591 ShOpcVal = ARM_AM::no_shift;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000592 }
593 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000594
Evan Chenga8e29892007-01-19 07:51:42 +0000595 // Try matching (R shl C) + (R).
Evan Chengf40deed2010-10-27 23:41:30 +0000596 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift &&
597 !(Subtarget->isCortexA9() || N.getOperand(0).hasOneUse())) {
Evan Chenga8e29892007-01-19 07:51:42 +0000598 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
599 if (ShOpcVal != ARM_AM::no_shift) {
600 // Check to see if the RHS of the shift is a constant, if not, we can't
601 // fold it.
602 if (ConstantSDNode *Sh =
603 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000604 ShAmt = Sh->getZExtValue();
Evan Chengf40deed2010-10-27 23:41:30 +0000605 if (!Subtarget->isCortexA9() ||
606 (N.hasOneUse() &&
607 isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt))) {
608 Offset = N.getOperand(0).getOperand(0);
609 Base = N.getOperand(1);
610 } else {
611 ShAmt = 0;
612 ShOpcVal = ARM_AM::no_shift;
613 }
Evan Chenga8e29892007-01-19 07:51:42 +0000614 } else {
615 ShOpcVal = ARM_AM::no_shift;
616 }
617 }
618 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000619
Evan Chenga8e29892007-01-19 07:51:42 +0000620 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 MVT::i32);
Jim Grosbach82891622010-09-29 19:03:54 +0000622 return AM2_SHOP;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000623}
624
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000625bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000626 SDValue &Offset, SDValue &Opc) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000627 unsigned Opcode = Op->getOpcode();
Evan Chenga8e29892007-01-19 07:51:42 +0000628 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
629 ? cast<LoadSDNode>(Op)->getAddressingMode()
630 : cast<StoreSDNode>(Op)->getAddressingMode();
631 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
632 ? ARM_AM::add : ARM_AM::sub;
633 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000634 int Val = (int)C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000635 if (Val >= 0 && Val < 0x1000) { // 12 bits.
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000637 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
638 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000640 return true;
641 }
642 }
643
644 Offset = N;
645 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
646 unsigned ShAmt = 0;
647 if (ShOpcVal != ARM_AM::no_shift) {
648 // Check to see if the RHS of the shift is a constant, if not, we can't fold
649 // it.
650 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000651 ShAmt = Sh->getZExtValue();
Evan Chengf40deed2010-10-27 23:41:30 +0000652 if (isShifterOpProfitable(N, ShOpcVal, ShAmt))
653 Offset = N.getOperand(0);
654 else {
655 ShAmt = 0;
656 ShOpcVal = ARM_AM::no_shift;
657 }
Evan Chenga8e29892007-01-19 07:51:42 +0000658 } else {
659 ShOpcVal = ARM_AM::no_shift;
660 }
661 }
662
663 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 MVT::i32);
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000665 return true;
666}
667
Evan Chenga8e29892007-01-19 07:51:42 +0000668
Chris Lattner52a261b2010-09-21 20:31:19 +0000669bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000670 SDValue &Base, SDValue &Offset,
671 SDValue &Opc) {
Evan Chenga8e29892007-01-19 07:51:42 +0000672 if (N.getOpcode() == ISD::SUB) {
673 // X - C is canonicalize to X + -C, no need to handle it here.
674 Base = N.getOperand(0);
675 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000677 return true;
678 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000679
Evan Chenga8e29892007-01-19 07:51:42 +0000680 if (N.getOpcode() != ISD::ADD) {
681 Base = N;
682 if (N.getOpcode() == ISD::FrameIndex) {
683 int FI = cast<FrameIndexSDNode>(N)->getIndex();
684 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
685 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000686 Offset = CurDAG->getRegister(0, MVT::i32);
687 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000688 return true;
689 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000690
Evan Chenga8e29892007-01-19 07:51:42 +0000691 // If the RHS is +/- imm8, fold into addr mode.
692 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000693 int RHSC = (int)RHS->getZExtValue();
Evan Chenge966d642007-01-24 02:45:25 +0000694 if ((RHSC >= 0 && RHSC < 256) ||
695 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000696 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000697 if (Base.getOpcode() == ISD::FrameIndex) {
698 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
699 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
700 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000702
703 ARM_AM::AddrOpc AddSub = ARM_AM::add;
704 if (RHSC < 0) {
705 AddSub = ARM_AM::sub;
706 RHSC = - RHSC;
707 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000708 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000709 return true;
710 }
711 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000712
Evan Chenga8e29892007-01-19 07:51:42 +0000713 Base = N.getOperand(0);
714 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000716 return true;
717}
718
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000719bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000720 SDValue &Offset, SDValue &Opc) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000721 unsigned Opcode = Op->getOpcode();
Evan Chenga8e29892007-01-19 07:51:42 +0000722 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
723 ? cast<LoadSDNode>(Op)->getAddressingMode()
724 : cast<StoreSDNode>(Op)->getAddressingMode();
725 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
726 ? ARM_AM::add : ARM_AM::sub;
727 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000728 int Val = (int)C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000729 if (Val >= 0 && Val < 256) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 Offset = CurDAG->getRegister(0, MVT::i32);
731 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000732 return true;
733 }
734 }
735
736 Offset = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000737 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000738 return true;
739}
740
Jim Grosbach3ab56582010-10-21 19:38:40 +0000741bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000742 SDValue &Base, SDValue &Offset) {
Evan Chenga8e29892007-01-19 07:51:42 +0000743 if (N.getOpcode() != ISD::ADD) {
744 Base = N;
745 if (N.getOpcode() == ISD::FrameIndex) {
746 int FI = cast<FrameIndexSDNode>(N)->getIndex();
747 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000748 } else if (N.getOpcode() == ARMISD::Wrapper &&
749 !(Subtarget->useMovt() &&
750 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000751 Base = N.getOperand(0);
752 }
753 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000755 return true;
756 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000757
Evan Chenga8e29892007-01-19 07:51:42 +0000758 // If the RHS is +/- imm8, fold into addr mode.
759 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000760 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000761 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
762 RHSC >>= 2;
Evan Chenge966d642007-01-24 02:45:25 +0000763 if ((RHSC >= 0 && RHSC < 256) ||
764 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000765 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000766 if (Base.getOpcode() == ISD::FrameIndex) {
767 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
768 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
769 }
770
771 ARM_AM::AddrOpc AddSub = ARM_AM::add;
772 if (RHSC < 0) {
773 AddSub = ARM_AM::sub;
774 RHSC = - RHSC;
775 }
776 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
Owen Anderson825b72b2009-08-11 20:47:22 +0000777 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000778 return true;
779 }
780 }
781 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000782
Evan Chenga8e29892007-01-19 07:51:42 +0000783 Base = N;
784 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000786 return true;
787}
788
Bob Wilson665814b2010-11-01 23:40:51 +0000789bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,
790 SDValue &Align) {
Bob Wilson8b024a52009-07-01 23:16:05 +0000791 Addr = N;
Bob Wilson665814b2010-11-01 23:40:51 +0000792
793 unsigned Alignment = 0;
794 if (LSBaseSDNode *LSN = dyn_cast<LSBaseSDNode>(Parent)) {
795 // This case occurs only for VLD1-lane/dup and VST1-lane instructions.
796 // The maximum alignment is equal to the memory size being referenced.
797 unsigned LSNAlign = LSN->getAlignment();
798 unsigned MemSize = LSN->getMemoryVT().getSizeInBits() / 8;
799 if (LSNAlign > MemSize && MemSize > 1)
800 Alignment = MemSize;
801 } else {
802 // All other uses of addrmode6 are for intrinsics. For now just record
803 // the raw alignment value; it will be refined later based on the legal
804 // alignment operands for the intrinsic.
805 Alignment = cast<MemIntrinsicSDNode>(Parent)->getAlignment();
806 }
807
808 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
Bob Wilson8b024a52009-07-01 23:16:05 +0000809 return true;
810}
811
Chris Lattner52a261b2010-09-21 20:31:19 +0000812bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
Evan Chengbba9f5f2009-08-14 19:01:37 +0000813 SDValue &Offset, SDValue &Label) {
Evan Chenga8e29892007-01-19 07:51:42 +0000814 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
815 Offset = N.getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000816 SDValue N1 = N.getOperand(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000817 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000818 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000819 return true;
820 }
821 return false;
822}
823
Chris Lattner52a261b2010-09-21 20:31:19 +0000824bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000825 SDValue &Base, SDValue &Offset){
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000826 // FIXME dl should come from the parent load or store, not the address
Evan Chengc38f2bc2007-01-23 22:59:13 +0000827 if (N.getOpcode() != ISD::ADD) {
Evan Cheng2f297df2009-07-11 07:08:13 +0000828 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
Dan Gohmane368b462010-06-18 14:22:04 +0000829 if (!NC || !NC->isNullValue())
Evan Cheng2f297df2009-07-11 07:08:13 +0000830 return false;
831
832 Base = Offset = N;
Evan Chengc38f2bc2007-01-23 22:59:13 +0000833 return true;
834 }
835
Evan Chenga8e29892007-01-19 07:51:42 +0000836 Base = N.getOperand(0);
837 Offset = N.getOperand(1);
838 return true;
839}
840
Evan Cheng79d43262007-01-24 02:21:22 +0000841bool
Chris Lattner52a261b2010-09-21 20:31:19 +0000842ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000843 unsigned Scale, SDValue &Base,
844 SDValue &OffImm, SDValue &Offset) {
Evan Cheng79d43262007-01-24 02:21:22 +0000845 if (Scale == 4) {
Dan Gohman475871a2008-07-27 21:46:04 +0000846 SDValue TmpBase, TmpOffImm;
Chris Lattner52a261b2010-09-21 20:31:19 +0000847 if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
Evan Cheng79d43262007-01-24 02:21:22 +0000848 return false; // We want to select tLDRspi / tSTRspi instead.
Evan Cheng012f2d92007-01-24 08:53:17 +0000849 if (N.getOpcode() == ARMISD::Wrapper &&
850 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
851 return false; // We want to select tLDRpci instead.
Evan Cheng79d43262007-01-24 02:21:22 +0000852 }
853
Evan Chenga8e29892007-01-19 07:51:42 +0000854 if (N.getOpcode() != ISD::ADD) {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000855 if (N.getOpcode() == ARMISD::Wrapper &&
856 !(Subtarget->useMovt() &&
857 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
858 Base = N.getOperand(0);
859 } else
860 Base = N;
861
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 Offset = CurDAG->getRegister(0, MVT::i32);
863 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000864 return true;
865 }
866
Evan Chengad0e4652007-02-06 00:22:06 +0000867 // Thumb does not have [sp, r] address mode.
868 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
869 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
870 if ((LHSR && LHSR->getReg() == ARM::SP) ||
871 (RHSR && RHSR->getReg() == ARM::SP)) {
872 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 Offset = CurDAG->getRegister(0, MVT::i32);
874 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengad0e4652007-02-06 00:22:06 +0000875 return true;
876 }
877
Evan Chenga8e29892007-01-19 07:51:42 +0000878 // If the RHS is + imm5 * scale, fold into addr mode.
879 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000880 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000881 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
882 RHSC /= Scale;
883 if (RHSC >= 0 && RHSC < 32) {
884 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 Offset = CurDAG->getRegister(0, MVT::i32);
886 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000887 return true;
888 }
889 }
890 }
891
Evan Chengc38f2bc2007-01-23 22:59:13 +0000892 Base = N.getOperand(0);
893 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000895 return true;
Evan Chenga8e29892007-01-19 07:51:42 +0000896}
897
Chris Lattner52a261b2010-09-21 20:31:19 +0000898bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000899 SDValue &Base, SDValue &OffImm,
900 SDValue &Offset) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000901 return SelectThumbAddrModeRI5(N, 1, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000902}
903
Chris Lattner52a261b2010-09-21 20:31:19 +0000904bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000905 SDValue &Base, SDValue &OffImm,
906 SDValue &Offset) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000907 return SelectThumbAddrModeRI5(N, 2, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000908}
909
Chris Lattner52a261b2010-09-21 20:31:19 +0000910bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000911 SDValue &Base, SDValue &OffImm,
912 SDValue &Offset) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000913 return SelectThumbAddrModeRI5(N, 4, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000914}
915
Chris Lattner52a261b2010-09-21 20:31:19 +0000916bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
917 SDValue &Base, SDValue &OffImm) {
Evan Chenga8e29892007-01-19 07:51:42 +0000918 if (N.getOpcode() == ISD::FrameIndex) {
919 int FI = cast<FrameIndexSDNode>(N)->getIndex();
920 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000922 return true;
923 }
Evan Cheng79d43262007-01-24 02:21:22 +0000924
Evan Chengad0e4652007-02-06 00:22:06 +0000925 if (N.getOpcode() != ISD::ADD)
926 return false;
927
928 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
Evan Cheng8c1a73a2007-02-06 09:11:20 +0000929 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
930 (LHSR && LHSR->getReg() == ARM::SP)) {
Evan Cheng79d43262007-01-24 02:21:22 +0000931 // If the RHS is + imm8 * scale, fold into addr mode.
932 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000933 int RHSC = (int)RHS->getZExtValue();
Evan Cheng79d43262007-01-24 02:21:22 +0000934 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
935 RHSC >>= 2;
936 if (RHSC >= 0 && RHSC < 256) {
Evan Chengad0e4652007-02-06 00:22:06 +0000937 Base = N.getOperand(0);
Evan Cheng8c1a73a2007-02-06 09:11:20 +0000938 if (Base.getOpcode() == ISD::FrameIndex) {
939 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
940 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
941 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000942 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Cheng79d43262007-01-24 02:21:22 +0000943 return true;
944 }
945 }
946 }
947 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000948
Evan Chenga8e29892007-01-19 07:51:42 +0000949 return false;
950}
951
Chris Lattner52a261b2010-09-21 20:31:19 +0000952bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDValue N, SDValue &BaseReg,
Evan Cheng9cb9e672009-06-27 02:26:13 +0000953 SDValue &Opc) {
Evan Chenga2c519b2010-07-30 23:33:54 +0000954 if (DisableShifterOp)
955 return false;
956
Evan Cheng9cb9e672009-06-27 02:26:13 +0000957 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
958
959 // Don't match base register only case. That is matched to a separate
960 // lower complexity pattern with explicit register operand.
961 if (ShOpcVal == ARM_AM::no_shift) return false;
962
963 BaseReg = N.getOperand(0);
964 unsigned ShImmVal = 0;
965 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
966 ShImmVal = RHS->getZExtValue() & 31;
967 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
968 return true;
969 }
970
971 return false;
972}
973
Chris Lattner52a261b2010-09-21 20:31:19 +0000974bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000975 SDValue &Base, SDValue &OffImm) {
976 // Match simple R + imm12 operands.
David Goodwin31e7eba2009-07-20 15:55:39 +0000977
Evan Cheng3a214252009-08-11 08:52:18 +0000978 // Base only.
979 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
David Goodwin31e7eba2009-07-20 15:55:39 +0000980 if (N.getOpcode() == ISD::FrameIndex) {
Evan Cheng3a214252009-08-11 08:52:18 +0000981 // Match frame index...
David Goodwin31e7eba2009-07-20 15:55:39 +0000982 int FI = cast<FrameIndexSDNode>(N)->getIndex();
983 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
David Goodwin31e7eba2009-07-20 15:55:39 +0000985 return true;
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000986 } else if (N.getOpcode() == ARMISD::Wrapper &&
987 !(Subtarget->useMovt() &&
988 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Cheng3a214252009-08-11 08:52:18 +0000989 Base = N.getOperand(0);
990 if (Base.getOpcode() == ISD::TargetConstantPool)
991 return false; // We want to select t2LDRpci instead.
992 } else
993 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000994 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng3a214252009-08-11 08:52:18 +0000995 return true;
David Goodwin31e7eba2009-07-20 15:55:39 +0000996 }
Evan Cheng055b0312009-06-29 07:51:04 +0000997
998 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner52a261b2010-09-21 20:31:19 +0000999 if (SelectT2AddrModeImm8(N, Base, OffImm))
Evan Cheng3a214252009-08-11 08:52:18 +00001000 // Let t2LDRi8 handle (R - imm8).
1001 return false;
1002
Evan Cheng055b0312009-06-29 07:51:04 +00001003 int RHSC = (int)RHS->getZExtValue();
David Goodwind8c95b52009-07-30 18:56:48 +00001004 if (N.getOpcode() == ISD::SUB)
1005 RHSC = -RHSC;
1006
1007 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
Evan Cheng055b0312009-06-29 07:51:04 +00001008 Base = N.getOperand(0);
David Goodwind8c95b52009-07-30 18:56:48 +00001009 if (Base.getOpcode() == ISD::FrameIndex) {
1010 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1011 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1012 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001013 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +00001014 return true;
1015 }
1016 }
1017
Evan Cheng3a214252009-08-11 08:52:18 +00001018 // Base only.
1019 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng3a214252009-08-11 08:52:18 +00001021 return true;
Evan Cheng055b0312009-06-29 07:51:04 +00001022}
1023
Chris Lattner52a261b2010-09-21 20:31:19 +00001024bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +00001025 SDValue &Base, SDValue &OffImm) {
David Goodwind8c95b52009-07-30 18:56:48 +00001026 // Match simple R - imm8 operands.
Evan Cheng3a214252009-08-11 08:52:18 +00001027 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::SUB) {
David Goodwin07337c02009-07-30 22:45:52 +00001028 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1029 int RHSC = (int)RHS->getSExtValue();
1030 if (N.getOpcode() == ISD::SUB)
1031 RHSC = -RHSC;
Jim Grosbach764ab522009-08-11 15:33:49 +00001032
Evan Cheng3a214252009-08-11 08:52:18 +00001033 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
1034 Base = N.getOperand(0);
David Goodwin07337c02009-07-30 22:45:52 +00001035 if (Base.getOpcode() == ISD::FrameIndex) {
1036 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1037 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1038 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001039 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
David Goodwin07337c02009-07-30 22:45:52 +00001040 return true;
Evan Cheng055b0312009-06-29 07:51:04 +00001041 }
Evan Cheng055b0312009-06-29 07:51:04 +00001042 }
1043 }
1044
1045 return false;
1046}
1047
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001048bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Chenge88d5ce2009-07-02 07:28:31 +00001049 SDValue &OffImm){
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001050 unsigned Opcode = Op->getOpcode();
Evan Chenge88d5ce2009-07-02 07:28:31 +00001051 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
1052 ? cast<LoadSDNode>(Op)->getAddressingMode()
1053 : cast<StoreSDNode>(Op)->getAddressingMode();
1054 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N)) {
1055 int RHSC = (int)RHS->getZExtValue();
1056 if (RHSC >= 0 && RHSC < 0x100) { // 8 bits.
David Goodwin4cb73522009-07-14 21:29:29 +00001057 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
Owen Anderson825b72b2009-08-11 20:47:22 +00001058 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
1059 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
Evan Chenge88d5ce2009-07-02 07:28:31 +00001060 return true;
1061 }
1062 }
1063
1064 return false;
1065}
1066
Chris Lattner52a261b2010-09-21 20:31:19 +00001067bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +00001068 SDValue &Base,
1069 SDValue &OffReg, SDValue &ShImm) {
Evan Cheng3a214252009-08-11 08:52:18 +00001070 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
1071 if (N.getOpcode() != ISD::ADD)
1072 return false;
Evan Cheng055b0312009-06-29 07:51:04 +00001073
Evan Cheng3a214252009-08-11 08:52:18 +00001074 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
1075 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1076 int RHSC = (int)RHS->getZExtValue();
1077 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
1078 return false;
1079 else if (RHSC < 0 && RHSC >= -255) // 8 bits
David Goodwind8c95b52009-07-30 18:56:48 +00001080 return false;
1081 }
1082
Evan Chengf40deed2010-10-27 23:41:30 +00001083 if (Subtarget->isCortexA9() && !N.hasOneUse()) {
1084 // Compute R + (R << [1,2,3]) and reuse it.
1085 Base = N;
1086 return false;
1087 }
1088
Evan Cheng055b0312009-06-29 07:51:04 +00001089 // Look for (R + R) or (R + (R << [1,2,3])).
1090 unsigned ShAmt = 0;
1091 Base = N.getOperand(0);
1092 OffReg = N.getOperand(1);
1093
1094 // Swap if it is ((R << c) + R).
1095 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
1096 if (ShOpcVal != ARM_AM::lsl) {
1097 ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
1098 if (ShOpcVal == ARM_AM::lsl)
1099 std::swap(Base, OffReg);
Jim Grosbach764ab522009-08-11 15:33:49 +00001100 }
1101
Evan Cheng055b0312009-06-29 07:51:04 +00001102 if (ShOpcVal == ARM_AM::lsl) {
1103 // Check to see if the RHS of the shift is a constant, if not, we can't fold
1104 // it.
1105 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
1106 ShAmt = Sh->getZExtValue();
Evan Chengf40deed2010-10-27 23:41:30 +00001107 if (ShAmt < 4 && isShifterOpProfitable(OffReg, ShOpcVal, ShAmt))
1108 OffReg = OffReg.getOperand(0);
1109 else {
Evan Cheng055b0312009-06-29 07:51:04 +00001110 ShAmt = 0;
1111 ShOpcVal = ARM_AM::no_shift;
Evan Chengf40deed2010-10-27 23:41:30 +00001112 }
Evan Cheng055b0312009-06-29 07:51:04 +00001113 } else {
1114 ShOpcVal = ARM_AM::no_shift;
1115 }
David Goodwin7ecc8502009-07-15 15:50:19 +00001116 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001117
Owen Anderson825b72b2009-08-11 20:47:22 +00001118 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +00001119
1120 return true;
1121}
1122
1123//===--------------------------------------------------------------------===//
1124
Evan Chengee568cf2007-07-05 07:15:27 +00001125/// getAL - Returns a ARMCC::AL immediate node.
Dan Gohman475871a2008-07-27 21:46:04 +00001126static inline SDValue getAL(SelectionDAG *CurDAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001127 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
Evan Cheng44bec522007-05-15 01:29:07 +00001128}
1129
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001130SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
1131 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengaf4550f2009-07-02 01:23:32 +00001132 ISD::MemIndexedMode AM = LD->getAddressingMode();
1133 if (AM == ISD::UNINDEXED)
1134 return NULL;
1135
Owen Andersone50ed302009-08-10 22:56:29 +00001136 EVT LoadedVT = LD->getMemoryVT();
Evan Chengaf4550f2009-07-02 01:23:32 +00001137 SDValue Offset, AMOpc;
1138 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1139 unsigned Opcode = 0;
1140 bool Match = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001141 if (LoadedVT == MVT::i32 &&
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001142 SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +00001143 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
1144 Match = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00001145 } else if (LoadedVT == MVT::i16 &&
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001146 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +00001147 Match = true;
1148 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
1149 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
1150 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
Owen Anderson825b72b2009-08-11 20:47:22 +00001151 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
Evan Chengaf4550f2009-07-02 01:23:32 +00001152 if (LD->getExtensionType() == ISD::SEXTLOAD) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001153 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +00001154 Match = true;
1155 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
1156 }
1157 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001158 if (SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +00001159 Match = true;
1160 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
1161 }
1162 }
1163 }
1164
1165 if (Match) {
1166 SDValue Chain = LD->getChain();
1167 SDValue Base = LD->getBasePtr();
1168 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +00001169 CurDAG->getRegister(0, MVT::i32), Chain };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001170 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +00001171 MVT::Other, Ops, 6);
Evan Chengaf4550f2009-07-02 01:23:32 +00001172 }
1173
1174 return NULL;
1175}
1176
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001177SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
1178 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +00001179 ISD::MemIndexedMode AM = LD->getAddressingMode();
1180 if (AM == ISD::UNINDEXED)
1181 return NULL;
1182
Owen Andersone50ed302009-08-10 22:56:29 +00001183 EVT LoadedVT = LD->getMemoryVT();
Evan Cheng4fbb9962009-07-02 23:16:11 +00001184 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
Evan Chenge88d5ce2009-07-02 07:28:31 +00001185 SDValue Offset;
1186 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1187 unsigned Opcode = 0;
1188 bool Match = false;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001189 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001190 switch (LoadedVT.getSimpleVT().SimpleTy) {
1191 case MVT::i32:
Evan Chenge88d5ce2009-07-02 07:28:31 +00001192 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
1193 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001194 case MVT::i16:
Evan Cheng4fbb9962009-07-02 23:16:11 +00001195 if (isSExtLd)
1196 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
1197 else
1198 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
Evan Chenge88d5ce2009-07-02 07:28:31 +00001199 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001200 case MVT::i8:
1201 case MVT::i1:
Evan Cheng4fbb9962009-07-02 23:16:11 +00001202 if (isSExtLd)
1203 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
1204 else
1205 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
Evan Chenge88d5ce2009-07-02 07:28:31 +00001206 break;
1207 default:
1208 return NULL;
1209 }
1210 Match = true;
1211 }
1212
1213 if (Match) {
1214 SDValue Chain = LD->getChain();
1215 SDValue Base = LD->getBasePtr();
1216 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 CurDAG->getRegister(0, MVT::i32), Chain };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001218 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +00001219 MVT::Other, Ops, 5);
Evan Chenge88d5ce2009-07-02 07:28:31 +00001220 }
1221
1222 return NULL;
1223}
1224
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001225/// PairSRegs - Form a D register from a pair of S registers.
1226///
1227SDNode *ARMDAGToDAGISel::PairSRegs(EVT VT, SDValue V0, SDValue V1) {
1228 DebugLoc dl = V0.getNode()->getDebugLoc();
1229 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
1230 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
Bob Wilson07f6e802010-06-16 21:34:01 +00001231 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1232 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001233}
1234
Evan Cheng603afbf2010-05-10 17:34:18 +00001235/// PairDRegs - Form a quad register from a pair of D registers.
1236///
Bob Wilson3bf12ab2009-10-06 22:01:59 +00001237SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
1238 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001239 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1240 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Bob Wilson07f6e802010-06-16 21:34:01 +00001241 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1242 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
Bob Wilson3bf12ab2009-10-06 22:01:59 +00001243}
1244
Evan Cheng7f687192010-05-14 00:21:45 +00001245/// PairQRegs - Form 4 consecutive D registers from a pair of Q registers.
Evan Cheng603afbf2010-05-10 17:34:18 +00001246///
1247SDNode *ARMDAGToDAGISel::PairQRegs(EVT VT, SDValue V0, SDValue V1) {
1248 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001249 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1250 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
Evan Cheng603afbf2010-05-10 17:34:18 +00001251 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1252 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1253}
1254
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001255/// QuadSRegs - Form 4 consecutive S registers.
1256///
1257SDNode *ARMDAGToDAGISel::QuadSRegs(EVT VT, SDValue V0, SDValue V1,
1258 SDValue V2, SDValue V3) {
1259 DebugLoc dl = V0.getNode()->getDebugLoc();
1260 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
1261 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
1262 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, MVT::i32);
1263 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, MVT::i32);
1264 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1265 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1266}
1267
Evan Cheng7f687192010-05-14 00:21:45 +00001268/// QuadDRegs - Form 4 consecutive D registers.
Evan Cheng603afbf2010-05-10 17:34:18 +00001269///
1270SDNode *ARMDAGToDAGISel::QuadDRegs(EVT VT, SDValue V0, SDValue V1,
1271 SDValue V2, SDValue V3) {
1272 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001273 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1274 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
1275 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
1276 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
Evan Cheng603afbf2010-05-10 17:34:18 +00001277 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1278 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1279}
1280
Evan Cheng8f6de382010-05-16 03:27:48 +00001281/// QuadQRegs - Form 4 consecutive Q registers.
1282///
1283SDNode *ARMDAGToDAGISel::QuadQRegs(EVT VT, SDValue V0, SDValue V1,
1284 SDValue V2, SDValue V3) {
1285 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001286 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1287 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
1288 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, MVT::i32);
1289 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, MVT::i32);
Evan Cheng8f6de382010-05-16 03:27:48 +00001290 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1291 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1292}
1293
Bob Wilson2a6e6162010-09-23 23:42:37 +00001294/// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1295/// of a NEON VLD or VST instruction. The supported values depend on the
1296/// number of registers being loaded.
Bob Wilson665814b2010-11-01 23:40:51 +00001297SDValue ARMDAGToDAGISel::GetVLDSTAlign(SDValue Align, unsigned NumVecs,
1298 bool is64BitVector) {
Bob Wilson2a6e6162010-09-23 23:42:37 +00001299 unsigned NumRegs = NumVecs;
1300 if (!is64BitVector && NumVecs < 3)
1301 NumRegs *= 2;
1302
Bob Wilson665814b2010-11-01 23:40:51 +00001303 unsigned Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
Bob Wilson2a6e6162010-09-23 23:42:37 +00001304 if (Alignment >= 32 && NumRegs == 4)
Bob Wilson665814b2010-11-01 23:40:51 +00001305 Alignment = 32;
1306 else if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1307 Alignment = 16;
1308 else if (Alignment >= 8)
1309 Alignment = 8;
1310 else
1311 Alignment = 0;
1312
1313 return CurDAG->getTargetConstant(Alignment, MVT::i32);
Bob Wilson2a6e6162010-09-23 23:42:37 +00001314}
1315
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001316SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
Bob Wilson3e36f132009-10-14 17:28:52 +00001317 unsigned *DOpcodes, unsigned *QOpcodes0,
1318 unsigned *QOpcodes1) {
Bob Wilson621f1952010-03-23 05:25:43 +00001319 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
Bob Wilson3e36f132009-10-14 17:28:52 +00001320 DebugLoc dl = N->getDebugLoc();
1321
Bob Wilson226036e2010-03-20 22:13:40 +00001322 SDValue MemAddr, Align;
Bob Wilson665814b2010-11-01 23:40:51 +00001323 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
Bob Wilson3e36f132009-10-14 17:28:52 +00001324 return NULL;
1325
1326 SDValue Chain = N->getOperand(0);
1327 EVT VT = N->getValueType(0);
1328 bool is64BitVector = VT.is64BitVector();
Bob Wilson665814b2010-11-01 23:40:51 +00001329 Align = GetVLDSTAlign(Align, NumVecs, is64BitVector);
Bob Wilson40ff01a2010-09-23 21:43:54 +00001330
Bob Wilson3e36f132009-10-14 17:28:52 +00001331 unsigned OpcodeIndex;
1332 switch (VT.getSimpleVT().SimpleTy) {
1333 default: llvm_unreachable("unhandled vld type");
1334 // Double-register operations:
1335 case MVT::v8i8: OpcodeIndex = 0; break;
1336 case MVT::v4i16: OpcodeIndex = 1; break;
1337 case MVT::v2f32:
1338 case MVT::v2i32: OpcodeIndex = 2; break;
1339 case MVT::v1i64: OpcodeIndex = 3; break;
1340 // Quad-register operations:
1341 case MVT::v16i8: OpcodeIndex = 0; break;
1342 case MVT::v8i16: OpcodeIndex = 1; break;
1343 case MVT::v4f32:
1344 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson621f1952010-03-23 05:25:43 +00001345 case MVT::v2i64: OpcodeIndex = 3;
Bob Wilson11d98992010-03-23 06:20:33 +00001346 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
Bob Wilson621f1952010-03-23 05:25:43 +00001347 break;
Bob Wilson3e36f132009-10-14 17:28:52 +00001348 }
1349
Bob Wilsonf5721912010-09-03 18:16:02 +00001350 EVT ResTy;
1351 if (NumVecs == 1)
1352 ResTy = VT;
1353 else {
1354 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1355 if (!is64BitVector)
1356 ResTyElts *= 2;
1357 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1358 }
1359
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001360 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001361 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilsonf5721912010-09-03 18:16:02 +00001362 SDValue SuperReg;
Bob Wilson3e36f132009-10-14 17:28:52 +00001363 if (is64BitVector) {
1364 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001365 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilsonf5721912010-09-03 18:16:02 +00001366 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
Bob Wilsonffde0802010-09-02 16:00:54 +00001367 if (NumVecs == 1)
Evan Chenge9e2ba02010-05-10 21:26:24 +00001368 return VLd;
1369
Bob Wilsonf5721912010-09-03 18:16:02 +00001370 SuperReg = SDValue(VLd, 0);
Jakob Stoklund Olesen7bb31e32010-05-24 17:13:28 +00001371 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
Evan Cheng5c6aba22010-05-14 18:54:59 +00001372 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001373 SDValue D = CurDAG->getTargetExtractSubreg(ARM::dsub_0+Vec,
Bob Wilsonffde0802010-09-02 16:00:54 +00001374 dl, VT, SuperReg);
Evan Cheng5c6aba22010-05-14 18:54:59 +00001375 ReplaceUses(SDValue(N, Vec), D);
Evan Chenge9e2ba02010-05-10 21:26:24 +00001376 }
Bob Wilsonf5721912010-09-03 18:16:02 +00001377 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
Evan Chenge9e2ba02010-05-10 21:26:24 +00001378 return NULL;
Bob Wilson3e36f132009-10-14 17:28:52 +00001379 }
1380
Bob Wilson621f1952010-03-23 05:25:43 +00001381 if (NumVecs <= 2) {
1382 // Quad registers are directly supported for VLD1 and VLD2,
1383 // loading pairs of D regs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001384 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001385 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilsonffde0802010-09-02 16:00:54 +00001386 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
Bob Wilsonffde0802010-09-02 16:00:54 +00001387 if (NumVecs == 1)
1388 return VLd;
1389
Bob Wilsonf5721912010-09-03 18:16:02 +00001390 SuperReg = SDValue(VLd, 0);
Bob Wilsonffde0802010-09-02 16:00:54 +00001391 Chain = SDValue(VLd, 1);
1392
Bob Wilson3e36f132009-10-14 17:28:52 +00001393 } else {
1394 // Otherwise, quad registers are loaded with two separate instructions,
1395 // where one loads the even registers and the other loads the odd registers.
Bob Wilsonf5721912010-09-03 18:16:02 +00001396 EVT AddrTy = MemAddr.getValueType();
Bob Wilson3e36f132009-10-14 17:28:52 +00001397
Bob Wilson24f995d2009-10-14 18:32:29 +00001398 // Load the even subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001399 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilsonf5721912010-09-03 18:16:02 +00001400 SDValue ImplDef =
1401 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1402 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1403 SDNode *VLdA =
1404 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsA, 7);
1405 Chain = SDValue(VLdA, 2);
Bob Wilson3e36f132009-10-14 17:28:52 +00001406
Bob Wilson24f995d2009-10-14 18:32:29 +00001407 // Load the odd subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001408 Opc = QOpcodes1[OpcodeIndex];
Bob Wilsonf5721912010-09-03 18:16:02 +00001409 const SDValue OpsB[] = { SDValue(VLdA, 1), Align, Reg0, SDValue(VLdA, 0),
1410 Pred, Reg0, Chain };
1411 SDNode *VLdB =
1412 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsB, 7);
1413 SuperReg = SDValue(VLdB, 0);
1414 Chain = SDValue(VLdB, 2);
1415 }
Bob Wilson3e36f132009-10-14 17:28:52 +00001416
Bob Wilsonf5721912010-09-03 18:16:02 +00001417 // Extract out the Q registers.
1418 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1419 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1420 SDValue Q = CurDAG->getTargetExtractSubreg(ARM::qsub_0+Vec,
1421 dl, VT, SuperReg);
1422 ReplaceUses(SDValue(N, Vec), Q);
Bob Wilson3e36f132009-10-14 17:28:52 +00001423 }
1424 ReplaceUses(SDValue(N, NumVecs), Chain);
1425 return NULL;
1426}
1427
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001428SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
Bob Wilson24f995d2009-10-14 18:32:29 +00001429 unsigned *DOpcodes, unsigned *QOpcodes0,
1430 unsigned *QOpcodes1) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001431 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
Bob Wilson24f995d2009-10-14 18:32:29 +00001432 DebugLoc dl = N->getDebugLoc();
1433
Bob Wilson226036e2010-03-20 22:13:40 +00001434 SDValue MemAddr, Align;
Bob Wilson665814b2010-11-01 23:40:51 +00001435 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
Bob Wilson24f995d2009-10-14 18:32:29 +00001436 return NULL;
1437
1438 SDValue Chain = N->getOperand(0);
1439 EVT VT = N->getOperand(3).getValueType();
1440 bool is64BitVector = VT.is64BitVector();
Bob Wilson665814b2010-11-01 23:40:51 +00001441 Align = GetVLDSTAlign(Align, NumVecs, is64BitVector);
Bob Wilson2a6e6162010-09-23 23:42:37 +00001442
Bob Wilson24f995d2009-10-14 18:32:29 +00001443 unsigned OpcodeIndex;
1444 switch (VT.getSimpleVT().SimpleTy) {
1445 default: llvm_unreachable("unhandled vst type");
1446 // Double-register operations:
1447 case MVT::v8i8: OpcodeIndex = 0; break;
1448 case MVT::v4i16: OpcodeIndex = 1; break;
1449 case MVT::v2f32:
1450 case MVT::v2i32: OpcodeIndex = 2; break;
1451 case MVT::v1i64: OpcodeIndex = 3; break;
1452 // Quad-register operations:
1453 case MVT::v16i8: OpcodeIndex = 0; break;
1454 case MVT::v8i16: OpcodeIndex = 1; break;
1455 case MVT::v4f32:
1456 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson11d98992010-03-23 06:20:33 +00001457 case MVT::v2i64: OpcodeIndex = 3;
1458 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1459 break;
Bob Wilson24f995d2009-10-14 18:32:29 +00001460 }
1461
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001462 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001463 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001464
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001465 SmallVector<SDValue, 7> Ops;
Bob Wilson24f995d2009-10-14 18:32:29 +00001466 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001467 Ops.push_back(Align);
Bob Wilson24f995d2009-10-14 18:32:29 +00001468
1469 if (is64BitVector) {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001470 if (NumVecs == 1) {
1471 Ops.push_back(N->getOperand(3));
1472 } else {
Evan Cheng0ce537a2010-05-11 01:19:40 +00001473 SDValue RegSeq;
1474 SDValue V0 = N->getOperand(0+3);
1475 SDValue V1 = N->getOperand(1+3);
1476
1477 // Form a REG_SEQUENCE to force register allocation.
1478 if (NumVecs == 2)
1479 RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1480 else {
1481 SDValue V2 = N->getOperand(2+3);
Jim Grosbach3ab56582010-10-21 19:38:40 +00001482 // If it's a vld3, form a quad D-register and leave the last part as
Evan Cheng0ce537a2010-05-11 01:19:40 +00001483 // an undef.
1484 SDValue V3 = (NumVecs == 3)
1485 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1486 : N->getOperand(3+3);
1487 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1488 }
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001489 Ops.push_back(RegSeq);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001490 }
Evan Chengac0869d2009-11-21 06:21:52 +00001491 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001492 Ops.push_back(Reg0); // predicate register
Bob Wilson24f995d2009-10-14 18:32:29 +00001493 Ops.push_back(Chain);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001494 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001495 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
Bob Wilson24f995d2009-10-14 18:32:29 +00001496 }
1497
Bob Wilson11d98992010-03-23 06:20:33 +00001498 if (NumVecs <= 2) {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001499 // Quad registers are directly supported for VST1 and VST2.
Bob Wilson24f995d2009-10-14 18:32:29 +00001500 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001501 if (NumVecs == 1) {
1502 Ops.push_back(N->getOperand(3));
1503 } else {
1504 // Form a QQ register.
Evan Cheng603afbf2010-05-10 17:34:18 +00001505 SDValue Q0 = N->getOperand(3);
1506 SDValue Q1 = N->getOperand(4);
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001507 Ops.push_back(SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0));
Bob Wilson24f995d2009-10-14 18:32:29 +00001508 }
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001509 Ops.push_back(Pred);
1510 Ops.push_back(Reg0); // predicate register
1511 Ops.push_back(Chain);
1512 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
Bob Wilson24f995d2009-10-14 18:32:29 +00001513 }
1514
1515 // Otherwise, quad registers are stored with two separate instructions,
1516 // where one stores the even registers and the other stores the odd registers.
Evan Cheng7189fd02010-05-15 07:53:37 +00001517
Bob Wilson07f6e802010-06-16 21:34:01 +00001518 // Form the QQQQ REG_SEQUENCE.
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001519 SDValue V0 = N->getOperand(0+3);
1520 SDValue V1 = N->getOperand(1+3);
1521 SDValue V2 = N->getOperand(2+3);
1522 SDValue V3 = (NumVecs == 3)
1523 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1524 : N->getOperand(3+3);
1525 SDValue RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilson07f6e802010-06-16 21:34:01 +00001526
1527 // Store the even D registers.
Bob Wilson07f6e802010-06-16 21:34:01 +00001528 Ops.push_back(Reg0); // post-access address offset
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001529 Ops.push_back(RegSeq);
Bob Wilson07f6e802010-06-16 21:34:01 +00001530 Ops.push_back(Pred);
1531 Ops.push_back(Reg0); // predicate register
1532 Ops.push_back(Chain);
1533 unsigned Opc = QOpcodes0[OpcodeIndex];
1534 SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001535 MVT::Other, Ops.data(), 7);
Bob Wilson07f6e802010-06-16 21:34:01 +00001536 Chain = SDValue(VStA, 1);
1537
1538 // Store the odd D registers.
1539 Ops[0] = SDValue(VStA, 0); // MemAddr
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001540 Ops[6] = Chain;
Bob Wilson07f6e802010-06-16 21:34:01 +00001541 Opc = QOpcodes1[OpcodeIndex];
1542 SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001543 MVT::Other, Ops.data(), 7);
Bob Wilson07f6e802010-06-16 21:34:01 +00001544 Chain = SDValue(VStB, 1);
1545 ReplaceUses(SDValue(N, 0), Chain);
1546 return NULL;
Bob Wilson24f995d2009-10-14 18:32:29 +00001547}
1548
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001549SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
Bob Wilson96493442009-10-14 16:46:45 +00001550 unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson8466fa12010-09-13 23:01:35 +00001551 unsigned *QOpcodes) {
Bob Wilson96493442009-10-14 16:46:45 +00001552 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001553 DebugLoc dl = N->getDebugLoc();
1554
Bob Wilson226036e2010-03-20 22:13:40 +00001555 SDValue MemAddr, Align;
Bob Wilson665814b2010-11-01 23:40:51 +00001556 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
Bob Wilsona7c397c2009-10-14 16:19:03 +00001557 return NULL;
1558
1559 SDValue Chain = N->getOperand(0);
1560 unsigned Lane =
1561 cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
Bob Wilson96493442009-10-14 16:46:45 +00001562 EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
Bob Wilsona7c397c2009-10-14 16:19:03 +00001563 bool is64BitVector = VT.is64BitVector();
1564
Bob Wilson665814b2010-11-01 23:40:51 +00001565 unsigned Alignment = 0;
Bob Wilson3454ed92010-10-19 00:16:32 +00001566 if (NumVecs != 3) {
Bob Wilson665814b2010-11-01 23:40:51 +00001567 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
Bob Wilson3454ed92010-10-19 00:16:32 +00001568 unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
1569 if (Alignment > NumBytes)
1570 Alignment = NumBytes;
1571 // Alignment must be a power of two; make sure of that.
1572 Alignment = (Alignment & -Alignment);
Bob Wilson665814b2010-11-01 23:40:51 +00001573 if (Alignment == 1)
1574 Alignment = 0;
Bob Wilson3454ed92010-10-19 00:16:32 +00001575 }
Bob Wilson665814b2010-11-01 23:40:51 +00001576 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
Bob Wilson3454ed92010-10-19 00:16:32 +00001577
Bob Wilsona7c397c2009-10-14 16:19:03 +00001578 unsigned OpcodeIndex;
1579 switch (VT.getSimpleVT().SimpleTy) {
Bob Wilson96493442009-10-14 16:46:45 +00001580 default: llvm_unreachable("unhandled vld/vst lane type");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001581 // Double-register operations:
1582 case MVT::v8i8: OpcodeIndex = 0; break;
1583 case MVT::v4i16: OpcodeIndex = 1; break;
1584 case MVT::v2f32:
1585 case MVT::v2i32: OpcodeIndex = 2; break;
1586 // Quad-register operations:
1587 case MVT::v8i16: OpcodeIndex = 0; break;
1588 case MVT::v4f32:
1589 case MVT::v4i32: OpcodeIndex = 1; break;
1590 }
1591
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001592 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001593 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001594
Bob Wilson8466fa12010-09-13 23:01:35 +00001595 SmallVector<SDValue, 7> Ops;
Bob Wilsona7c397c2009-10-14 16:19:03 +00001596 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001597 Ops.push_back(Align);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001598
Jim Grosbach3ab56582010-10-21 19:38:40 +00001599 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
Eric Christopher23da0b22010-09-14 08:31:25 +00001600 QOpcodes[OpcodeIndex]);
Bob Wilson07f6e802010-06-16 21:34:01 +00001601
Bob Wilson8466fa12010-09-13 23:01:35 +00001602 SDValue SuperReg;
1603 SDValue V0 = N->getOperand(0+3);
1604 SDValue V1 = N->getOperand(1+3);
1605 if (NumVecs == 2) {
1606 if (is64BitVector)
1607 SuperReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1608 else
1609 SuperReg = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001610 } else {
Bob Wilson8466fa12010-09-13 23:01:35 +00001611 SDValue V2 = N->getOperand(2+3);
1612 SDValue V3 = (NumVecs == 3)
1613 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1614 : N->getOperand(3+3);
1615 if (is64BitVector)
1616 SuperReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1617 else
1618 SuperReg = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001619 }
Bob Wilson8466fa12010-09-13 23:01:35 +00001620 Ops.push_back(SuperReg);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001621 Ops.push_back(getI32Imm(Lane));
Evan Chengac0869d2009-11-21 06:21:52 +00001622 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001623 Ops.push_back(Reg0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001624 Ops.push_back(Chain);
1625
Bob Wilson96493442009-10-14 16:46:45 +00001626 if (!IsLoad)
Bob Wilson8466fa12010-09-13 23:01:35 +00001627 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 7);
Bob Wilson96493442009-10-14 16:46:45 +00001628
Bob Wilson8466fa12010-09-13 23:01:35 +00001629 EVT ResTy;
1630 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1631 if (!is64BitVector)
1632 ResTyElts *= 2;
1633 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001634
Bob Wilson8466fa12010-09-13 23:01:35 +00001635 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other,
1636 Ops.data(), 7);
1637 SuperReg = SDValue(VLdLn, 0);
1638 Chain = SDValue(VLdLn, 1);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001639
Bob Wilson8466fa12010-09-13 23:01:35 +00001640 // Extract the subregisters.
Bob Wilson07f6e802010-06-16 21:34:01 +00001641 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1642 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1643 unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1644 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1645 ReplaceUses(SDValue(N, Vec),
Bob Wilson8466fa12010-09-13 23:01:35 +00001646 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1647 ReplaceUses(SDValue(N, NumVecs), Chain);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001648 return NULL;
1649}
1650
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00001651SDNode *ARMDAGToDAGISel::SelectVLDDup(SDNode *N, unsigned NumVecs,
1652 unsigned *Opcodes) {
1653 assert(NumVecs >=2 && NumVecs <= 4 && "VLDDup NumVecs out-of-range");
1654 DebugLoc dl = N->getDebugLoc();
1655
1656 SDValue MemAddr, Align;
1657 if (!SelectAddrMode6(N, N->getOperand(1), MemAddr, Align))
1658 return NULL;
1659
1660 SDValue Chain = N->getOperand(0);
1661 EVT VT = N->getValueType(0);
1662
1663 unsigned Alignment = 0;
1664 if (NumVecs != 3) {
1665 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
1666 unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
1667 if (Alignment > NumBytes)
1668 Alignment = NumBytes;
1669 // Alignment must be a power of two; make sure of that.
1670 Alignment = (Alignment & -Alignment);
1671 if (Alignment == 1)
1672 Alignment = 0;
1673 }
1674 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1675
1676 unsigned OpcodeIndex;
1677 switch (VT.getSimpleVT().SimpleTy) {
1678 default: llvm_unreachable("unhandled vld-dup type");
1679 case MVT::v8i8: OpcodeIndex = 0; break;
1680 case MVT::v4i16: OpcodeIndex = 1; break;
1681 case MVT::v2f32:
1682 case MVT::v2i32: OpcodeIndex = 2; break;
1683 }
1684
1685 SDValue Pred = getAL(CurDAG);
1686 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1687 SDValue SuperReg;
1688 unsigned Opc = Opcodes[OpcodeIndex];
1689 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1690
1691 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1692 EVT ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1693 SDNode *VLdDup = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
1694 SuperReg = SDValue(VLdDup, 0);
1695 Chain = SDValue(VLdDup, 1);
1696
1697 // Extract the subregisters.
1698 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1699 unsigned SubIdx = ARM::dsub_0;
1700 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1701 ReplaceUses(SDValue(N, Vec),
1702 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1703 ReplaceUses(SDValue(N, NumVecs), Chain);
1704 return NULL;
1705}
1706
Bob Wilson78dfbc32010-07-07 00:08:54 +00001707SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1708 unsigned Opc) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001709 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1710 DebugLoc dl = N->getDebugLoc();
1711 EVT VT = N->getValueType(0);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001712 unsigned FirstTblReg = IsExt ? 2 : 1;
Bob Wilsond491d6e2010-07-06 23:36:25 +00001713
1714 // Form a REG_SEQUENCE to force register allocation.
1715 SDValue RegSeq;
Bob Wilson78dfbc32010-07-07 00:08:54 +00001716 SDValue V0 = N->getOperand(FirstTblReg + 0);
1717 SDValue V1 = N->getOperand(FirstTblReg + 1);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001718 if (NumVecs == 2)
1719 RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1720 else {
Bob Wilson78dfbc32010-07-07 00:08:54 +00001721 SDValue V2 = N->getOperand(FirstTblReg + 2);
Jim Grosbach3ab56582010-10-21 19:38:40 +00001722 // If it's a vtbl3, form a quad D-register and leave the last part as
Bob Wilsond491d6e2010-07-06 23:36:25 +00001723 // an undef.
1724 SDValue V3 = (NumVecs == 3)
1725 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
Bob Wilson78dfbc32010-07-07 00:08:54 +00001726 : N->getOperand(FirstTblReg + 3);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001727 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1728 }
1729
Bob Wilson78dfbc32010-07-07 00:08:54 +00001730 SmallVector<SDValue, 6> Ops;
1731 if (IsExt)
1732 Ops.push_back(N->getOperand(1));
Bob Wilsonbd916c52010-09-13 23:55:10 +00001733 Ops.push_back(RegSeq);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001734 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
Bob Wilsond491d6e2010-07-06 23:36:25 +00001735 Ops.push_back(getAL(CurDAG)); // predicate
1736 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
Bob Wilson78dfbc32010-07-07 00:08:54 +00001737 return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
Bob Wilsond491d6e2010-07-06 23:36:25 +00001738}
1739
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001740SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001741 bool isSigned) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001742 if (!Subtarget->hasV6T2Ops())
1743 return NULL;
Bob Wilson96493442009-10-14 16:46:45 +00001744
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001745 unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1746 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1747
1748
1749 // For unsigned extracts, check for a shift right and mask
1750 unsigned And_imm = 0;
1751 if (N->getOpcode() == ISD::AND) {
1752 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1753
1754 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1755 if (And_imm & (And_imm + 1))
1756 return NULL;
1757
1758 unsigned Srl_imm = 0;
1759 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1760 Srl_imm)) {
1761 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1762
1763 unsigned Width = CountTrailingOnes_32(And_imm);
1764 unsigned LSB = Srl_imm;
1765 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1766 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1767 CurDAG->getTargetConstant(LSB, MVT::i32),
1768 CurDAG->getTargetConstant(Width, MVT::i32),
1769 getAL(CurDAG), Reg0 };
1770 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1771 }
1772 }
1773 return NULL;
1774 }
1775
1776 // Otherwise, we're looking for a shift of a shift
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001777 unsigned Shl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001778 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001779 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1780 unsigned Srl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001781 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001782 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1783 unsigned Width = 32 - Srl_imm;
1784 int LSB = Srl_imm - Shl_imm;
Evan Cheng8000c6c2009-10-22 00:40:00 +00001785 if (LSB < 0)
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001786 return NULL;
1787 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001788 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001789 CurDAG->getTargetConstant(LSB, MVT::i32),
1790 CurDAG->getTargetConstant(Width, MVT::i32),
1791 getAL(CurDAG), Reg0 };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001792 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001793 }
1794 }
1795 return NULL;
1796}
1797
Evan Cheng9ef48352009-11-20 00:54:03 +00001798SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001799SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001800 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1801 SDValue CPTmp0;
1802 SDValue CPTmp1;
Chris Lattner52a261b2010-09-21 20:31:19 +00001803 if (SelectT2ShifterOperandReg(TrueVal, CPTmp0, CPTmp1)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001804 unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1805 unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1806 unsigned Opc = 0;
1807 switch (SOShOp) {
1808 case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1809 case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1810 case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1811 case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1812 default:
1813 llvm_unreachable("Unknown so_reg opcode!");
1814 break;
1815 }
1816 SDValue SOShImm =
1817 CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1818 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1819 SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001820 return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
Evan Cheng9ef48352009-11-20 00:54:03 +00001821 }
1822 return 0;
1823}
1824
1825SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001826SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001827 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1828 SDValue CPTmp0;
1829 SDValue CPTmp1;
1830 SDValue CPTmp2;
Chris Lattner52a261b2010-09-21 20:31:19 +00001831 if (SelectShifterOperandReg(TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001832 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1833 SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001834 return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
Evan Cheng9ef48352009-11-20 00:54:03 +00001835 }
1836 return 0;
1837}
1838
1839SDNode *ARMDAGToDAGISel::
Jim Grosbacha4257162010-10-07 00:53:56 +00001840SelectT2CMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng6b194912010-11-17 20:56:30 +00001841 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001842 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
Evan Chengff96b632010-11-19 23:01:16 +00001843 if (!T)
Evan Cheng9ef48352009-11-20 00:54:03 +00001844 return 0;
1845
Evan Cheng63f35442010-11-13 02:25:14 +00001846 unsigned Opc = 0;
Jim Grosbacha4257162010-10-07 00:53:56 +00001847 unsigned TrueImm = T->getZExtValue();
Evan Cheng6b194912010-11-17 20:56:30 +00001848 if (is_t2_so_imm(TrueImm)) {
1849 Opc = ARM::t2MOVCCi;
1850 } else if (TrueImm <= 0xffff) {
1851 Opc = ARM::t2MOVCCi16;
Evan Cheng63f35442010-11-13 02:25:14 +00001852 } else if (is_t2_so_imm_not(TrueImm)) {
1853 TrueImm = ~TrueImm;
1854 Opc = ARM::t2MVNCCi;
Evan Cheng6b194912010-11-17 20:56:30 +00001855 } else if (TrueVal.getNode()->hasOneUse() && Subtarget->hasV6T2Ops()) {
Evan Cheng63f35442010-11-13 02:25:14 +00001856 // Large immediate.
1857 Opc = ARM::t2MOVCCi32imm;
1858 }
1859
1860 if (Opc) {
Evan Cheng875a6ac2010-11-12 22:42:47 +00001861 SDValue True = CurDAG->getTargetConstant(TrueImm, MVT::i32);
Evan Cheng9ef48352009-11-20 00:54:03 +00001862 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1863 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Evan Cheng63f35442010-11-13 02:25:14 +00001864 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Evan Cheng9ef48352009-11-20 00:54:03 +00001865 }
Evan Cheng63f35442010-11-13 02:25:14 +00001866
Evan Cheng9ef48352009-11-20 00:54:03 +00001867 return 0;
1868}
1869
1870SDNode *ARMDAGToDAGISel::
Jim Grosbach3bbdcea2010-10-07 00:42:42 +00001871SelectARMCMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng6b194912010-11-17 20:56:30 +00001872 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001873 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1874 if (!T)
1875 return 0;
1876
Evan Cheng63f35442010-11-13 02:25:14 +00001877 unsigned Opc = 0;
Jim Grosbach3bbdcea2010-10-07 00:42:42 +00001878 unsigned TrueImm = T->getZExtValue();
Evan Cheng875a6ac2010-11-12 22:42:47 +00001879 bool isSoImm = is_so_imm(TrueImm);
Evan Cheng6b194912010-11-17 20:56:30 +00001880 if (isSoImm) {
1881 Opc = ARM::MOVCCi;
1882 } else if (Subtarget->hasV6T2Ops() && TrueImm <= 0xffff) {
1883 Opc = ARM::MOVCCi16;
Evan Cheng63f35442010-11-13 02:25:14 +00001884 } else if (is_so_imm_not(TrueImm)) {
1885 TrueImm = ~TrueImm;
1886 Opc = ARM::MVNCCi;
Evan Cheng6b194912010-11-17 20:56:30 +00001887 } else if (TrueVal.getNode()->hasOneUse() &&
1888 (Subtarget->hasV6T2Ops() || ARM_AM::isSOImmTwoPartVal(TrueImm))) {
Evan Cheng63f35442010-11-13 02:25:14 +00001889 // Large immediate.
1890 Opc = ARM::MOVCCi32imm;
1891 }
1892
1893 if (Opc) {
Jim Grosbach3bbdcea2010-10-07 00:42:42 +00001894 SDValue True = CurDAG->getTargetConstant(TrueImm, MVT::i32);
Evan Cheng9ef48352009-11-20 00:54:03 +00001895 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1896 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Evan Cheng63f35442010-11-13 02:25:14 +00001897 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Evan Cheng9ef48352009-11-20 00:54:03 +00001898 }
Evan Cheng63f35442010-11-13 02:25:14 +00001899
Evan Cheng9ef48352009-11-20 00:54:03 +00001900 return 0;
1901}
1902
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001903SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
1904 EVT VT = N->getValueType(0);
1905 SDValue FalseVal = N->getOperand(0);
1906 SDValue TrueVal = N->getOperand(1);
1907 SDValue CC = N->getOperand(2);
1908 SDValue CCR = N->getOperand(3);
1909 SDValue InFlag = N->getOperand(4);
Evan Cheng9ef48352009-11-20 00:54:03 +00001910 assert(CC.getOpcode() == ISD::Constant);
1911 assert(CCR.getOpcode() == ISD::Register);
1912 ARMCC::CondCodes CCVal =
1913 (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
Evan Cheng07ba9062009-11-19 21:45:22 +00001914
1915 if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1916 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1917 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1918 // Pattern complexity = 18 cost = 1 size = 0
1919 SDValue CPTmp0;
1920 SDValue CPTmp1;
1921 SDValue CPTmp2;
1922 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001923 SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001924 CCVal, CCR, InFlag);
1925 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001926 Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001927 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1928 if (Res)
1929 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001930 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001931 SDNode *Res = SelectARMCMOVShiftOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001932 CCVal, CCR, InFlag);
1933 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001934 Res = SelectARMCMOVShiftOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001935 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1936 if (Res)
1937 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001938 }
1939
1940 // Pattern: (ARMcmov:i32 GPR:i32:$false,
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001941 // (imm:i32)<<P:Pred_so_imm>>:$true,
Evan Cheng07ba9062009-11-19 21:45:22 +00001942 // (imm:i32):$cc)
1943 // Emits: (MOVCCi:i32 GPR:i32:$false,
1944 // (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1945 // Pattern complexity = 10 cost = 1 size = 0
Evan Cheng9ef48352009-11-20 00:54:03 +00001946 if (Subtarget->isThumb()) {
Jim Grosbacha4257162010-10-07 00:53:56 +00001947 SDNode *Res = SelectT2CMOVImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001948 CCVal, CCR, InFlag);
1949 if (!Res)
Jim Grosbacha4257162010-10-07 00:53:56 +00001950 Res = SelectT2CMOVImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001951 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1952 if (Res)
1953 return Res;
1954 } else {
Jim Grosbach3bbdcea2010-10-07 00:42:42 +00001955 SDNode *Res = SelectARMCMOVImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001956 CCVal, CCR, InFlag);
1957 if (!Res)
Jim Grosbach3bbdcea2010-10-07 00:42:42 +00001958 Res = SelectARMCMOVImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001959 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1960 if (Res)
1961 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001962 }
1963 }
1964
1965 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1966 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1967 // Pattern complexity = 6 cost = 1 size = 0
1968 //
1969 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1970 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1971 // Pattern complexity = 6 cost = 11 size = 0
1972 //
1973 // Also FCPYScc and FCPYDcc.
Evan Cheng9ef48352009-11-20 00:54:03 +00001974 SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1975 SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
Evan Cheng07ba9062009-11-19 21:45:22 +00001976 unsigned Opc = 0;
1977 switch (VT.getSimpleVT().SimpleTy) {
1978 default: assert(false && "Illegal conditional move type!");
1979 break;
1980 case MVT::i32:
1981 Opc = Subtarget->isThumb()
1982 ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1983 : ARM::MOVCCr;
1984 break;
1985 case MVT::f32:
1986 Opc = ARM::VMOVScc;
1987 break;
1988 case MVT::f64:
1989 Opc = ARM::VMOVDcc;
1990 break;
1991 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001992 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Cheng07ba9062009-11-19 21:45:22 +00001993}
1994
Evan Chengde8aa4e2010-05-05 18:28:36 +00001995SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
1996 // The only time a CONCAT_VECTORS operation can have legal types is when
1997 // two 64-bit vectors are concatenated to a 128-bit vector.
1998 EVT VT = N->getValueType(0);
1999 if (!VT.is128BitVector() || N->getNumOperands() != 2)
2000 llvm_unreachable("unexpected CONCAT_VECTORS");
2001 DebugLoc dl = N->getDebugLoc();
2002 SDValue V0 = N->getOperand(0);
2003 SDValue V1 = N->getOperand(1);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00002004 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
2005 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Evan Chengde8aa4e2010-05-05 18:28:36 +00002006 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
2007 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
2008}
2009
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002010SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
Dale Johannesened2eee62009-02-06 01:31:28 +00002011 DebugLoc dl = N->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002012
Dan Gohmane8be6c62008-07-17 19:10:17 +00002013 if (N->isMachineOpcode())
Evan Chenga8e29892007-01-19 07:51:42 +00002014 return NULL; // Already selected.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00002015
2016 switch (N->getOpcode()) {
Evan Chenga8e29892007-01-19 07:51:42 +00002017 default: break;
2018 case ISD::Constant: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002019 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002020 bool UseCP = true;
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00002021 if (Subtarget->hasThumb2())
2022 // Thumb2-aware targets have the MOVT instruction, so all immediates can
2023 // be done with MOV + MOVT, at worst.
2024 UseCP = 0;
2025 else {
2026 if (Subtarget->isThumb()) {
Bob Wilsone64e3cf2009-06-22 17:29:13 +00002027 UseCP = (Val > 255 && // MOV
2028 ~Val > 255 && // MOV + MVN
2029 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00002030 } else
2031 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
2032 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
2033 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
2034 }
2035
Evan Chenga8e29892007-01-19 07:51:42 +00002036 if (UseCP) {
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue CPIdx =
Owen Anderson1d0be152009-08-13 21:58:54 +00002038 CurDAG->getTargetConstantPool(ConstantInt::get(
2039 Type::getInt32Ty(*CurDAG->getContext()), Val),
Evan Chenga8e29892007-01-19 07:51:42 +00002040 TLI.getPointerTy());
Evan Cheng012f2d92007-01-24 08:53:17 +00002041
2042 SDNode *ResNode;
Evan Cheng446c4282009-07-11 06:43:01 +00002043 if (Subtarget->isThumb1Only()) {
Evan Cheng47b7b9f2010-04-16 05:46:06 +00002044 SDValue Pred = getAL(CurDAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
Evan Cheng446c4282009-07-11 06:43:01 +00002046 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
Dan Gohman602b0c82009-09-25 18:54:59 +00002047 ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
2048 Ops, 4);
Evan Cheng446c4282009-07-11 06:43:01 +00002049 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00002050 SDValue Ops[] = {
Jim Grosbach764ab522009-08-11 15:33:49 +00002051 CPIdx,
Owen Anderson825b72b2009-08-11 20:47:22 +00002052 CurDAG->getTargetConstant(0, MVT::i32),
Evan Chengee568cf2007-07-05 07:15:27 +00002053 getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +00002054 CurDAG->getRegister(0, MVT::i32),
Evan Cheng012f2d92007-01-24 08:53:17 +00002055 CurDAG->getEntryNode()
2056 };
Dan Gohman602b0c82009-09-25 18:54:59 +00002057 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
Jim Grosbach3e556122010-10-26 22:37:02 +00002058 Ops, 5);
Evan Cheng012f2d92007-01-24 08:53:17 +00002059 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002060 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
Evan Chenga8e29892007-01-19 07:51:42 +00002061 return NULL;
2062 }
Jim Grosbach764ab522009-08-11 15:33:49 +00002063
Evan Chenga8e29892007-01-19 07:51:42 +00002064 // Other cases are autogenerated.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00002065 break;
Evan Chenga8e29892007-01-19 07:51:42 +00002066 }
Rafael Espindolaf819a492006-11-09 13:58:55 +00002067 case ISD::FrameIndex: {
Evan Chenga8e29892007-01-19 07:51:42 +00002068 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindolaf819a492006-11-09 13:58:55 +00002069 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohman475871a2008-07-27 21:46:04 +00002070 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
David Goodwinf1daf7d2009-07-08 23:10:31 +00002071 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002072 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
2073 CurDAG->getTargetConstant(0, MVT::i32));
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002074 } else {
David Goodwin419c6152009-07-14 18:48:51 +00002075 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
2076 ARM::t2ADDri : ARM::ADDri);
Owen Anderson825b72b2009-08-11 20:47:22 +00002077 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
2078 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2079 CurDAG->getRegister(0, MVT::i32) };
2080 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00002081 }
Evan Chenga8e29892007-01-19 07:51:42 +00002082 }
Sandeep Patel47eedaa2009-10-13 18:59:48 +00002083 case ISD::SRL:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00002084 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00002085 return I;
2086 break;
2087 case ISD::SRA:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00002088 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00002089 return I;
2090 break;
Evan Chenga8e29892007-01-19 07:51:42 +00002091 case ISD::MUL:
Evan Cheng5b9fcd12009-07-07 01:17:28 +00002092 if (Subtarget->isThumb1Only())
Evan Cheng79d43262007-01-24 02:21:22 +00002093 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002094 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002095 unsigned RHSV = C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002096 if (!RHSV) break;
2097 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00002098 unsigned ShImm = Log2_32(RHSV-1);
2099 if (ShImm >= 32)
2100 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002101 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00002102 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
2104 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00002105 if (Subtarget->isThumb()) {
Evan Chengaf9e7a72009-07-21 00:31:12 +00002106 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00002107 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00002108 } else {
2109 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00002110 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00002111 }
Evan Chenga8e29892007-01-19 07:51:42 +00002112 }
2113 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00002114 unsigned ShImm = Log2_32(RHSV+1);
2115 if (ShImm >= 32)
2116 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002117 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00002118 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00002119 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
2120 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00002121 if (Subtarget->isThumb()) {
Bob Wilson13ef8402010-05-28 00:27:15 +00002122 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
2123 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00002124 } else {
2125 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00002126 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00002127 }
Evan Chenga8e29892007-01-19 07:51:42 +00002128 }
2129 }
2130 break;
Evan Cheng20956592009-10-21 08:15:52 +00002131 case ISD::AND: {
Jim Grosbach3a1287b2010-04-22 23:24:18 +00002132 // Check for unsigned bitfield extract
2133 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
2134 return I;
2135
Evan Cheng20956592009-10-21 08:15:52 +00002136 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
2137 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
2138 // are entirely contributed by c2 and lower 16-bits are entirely contributed
2139 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
2140 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002141 EVT VT = N->getValueType(0);
Evan Cheng20956592009-10-21 08:15:52 +00002142 if (VT != MVT::i32)
2143 break;
2144 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
2145 ? ARM::t2MOVTi16
2146 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
2147 if (!Opc)
2148 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002149 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Evan Cheng20956592009-10-21 08:15:52 +00002150 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2151 if (!N1C)
2152 break;
2153 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
2154 SDValue N2 = N0.getOperand(1);
2155 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2156 if (!N2C)
2157 break;
2158 unsigned N1CVal = N1C->getZExtValue();
2159 unsigned N2CVal = N2C->getZExtValue();
2160 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
2161 (N1CVal & 0xffffU) == 0xffffU &&
2162 (N2CVal & 0xffffU) == 0x0U) {
2163 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
2164 MVT::i32);
2165 SDValue Ops[] = { N0.getOperand(0), Imm16,
2166 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
2167 return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
2168 }
2169 }
2170 break;
2171 }
Jim Grosbache5165492009-11-09 00:11:35 +00002172 case ARMISD::VMOVRRD:
2173 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002174 N->getOperand(0), getAL(CurDAG),
Dan Gohman602b0c82009-09-25 18:54:59 +00002175 CurDAG->getRegister(0, MVT::i32));
Dan Gohman525178c2007-10-08 18:33:35 +00002176 case ISD::UMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00002177 if (Subtarget->isThumb1Only())
2178 break;
2179 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002180 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00002181 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2182 CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00002183 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00002184 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002185 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00002186 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2187 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00002188 return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00002189 }
Evan Chengee568cf2007-07-05 07:15:27 +00002190 }
Dan Gohman525178c2007-10-08 18:33:35 +00002191 case ISD::SMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00002192 if (Subtarget->isThumb1Only())
2193 break;
2194 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002195 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00002196 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00002197 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00002198 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002199 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00002200 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2201 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00002202 return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00002203 }
Evan Chengee568cf2007-07-05 07:15:27 +00002204 }
Evan Chenga8e29892007-01-19 07:51:42 +00002205 case ISD::LOAD: {
Evan Chenge88d5ce2009-07-02 07:28:31 +00002206 SDNode *ResNode = 0;
Evan Cheng5b9fcd12009-07-07 01:17:28 +00002207 if (Subtarget->isThumb() && Subtarget->hasThumb2())
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002208 ResNode = SelectT2IndexedLoad(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +00002209 else
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002210 ResNode = SelectARMIndexedLoad(N);
Evan Chengaf4550f2009-07-02 01:23:32 +00002211 if (ResNode)
2212 return ResNode;
Evan Chenga8e29892007-01-19 07:51:42 +00002213 // Other cases are autogenerated.
Rafael Espindolaf819a492006-11-09 13:58:55 +00002214 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +00002215 }
Evan Chengee568cf2007-07-05 07:15:27 +00002216 case ARMISD::BRCOND: {
2217 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2218 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2219 // Pattern complexity = 6 cost = 1 size = 0
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002220
Evan Chengee568cf2007-07-05 07:15:27 +00002221 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2222 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
2223 // Pattern complexity = 6 cost = 1 size = 0
2224
David Goodwin5e47a9a2009-06-30 18:04:13 +00002225 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2226 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2227 // Pattern complexity = 6 cost = 1 size = 0
2228
Jim Grosbach764ab522009-08-11 15:33:49 +00002229 unsigned Opc = Subtarget->isThumb() ?
David Goodwin5e47a9a2009-06-30 18:04:13 +00002230 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002231 SDValue Chain = N->getOperand(0);
2232 SDValue N1 = N->getOperand(1);
2233 SDValue N2 = N->getOperand(2);
2234 SDValue N3 = N->getOperand(3);
2235 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00002236 assert(N1.getOpcode() == ISD::BasicBlock);
2237 assert(N2.getOpcode() == ISD::Constant);
2238 assert(N3.getOpcode() == ISD::Register);
2239
Dan Gohman475871a2008-07-27 21:46:04 +00002240 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002241 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00002242 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002243 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
Dan Gohman602b0c82009-09-25 18:54:59 +00002244 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
2245 MVT::Flag, Ops, 5);
Dan Gohman475871a2008-07-27 21:46:04 +00002246 Chain = SDValue(ResNode, 0);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002247 if (N->getNumValues() == 2) {
Dan Gohman475871a2008-07-27 21:46:04 +00002248 InFlag = SDValue(ResNode, 1);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002249 ReplaceUses(SDValue(N, 1), InFlag);
Chris Lattnera47b9bc2008-02-03 03:20:59 +00002250 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002251 ReplaceUses(SDValue(N, 0),
Evan Chenged54de42009-11-19 08:16:50 +00002252 SDValue(Chain.getNode(), Chain.getResNo()));
Evan Chengee568cf2007-07-05 07:15:27 +00002253 return NULL;
2254 }
Evan Cheng07ba9062009-11-19 21:45:22 +00002255 case ARMISD::CMOV:
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002256 return SelectCMOVOp(N);
Evan Chengee568cf2007-07-05 07:15:27 +00002257 case ARMISD::CNEG: {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002258 EVT VT = N->getValueType(0);
2259 SDValue N0 = N->getOperand(0);
2260 SDValue N1 = N->getOperand(1);
2261 SDValue N2 = N->getOperand(2);
2262 SDValue N3 = N->getOperand(3);
2263 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00002264 assert(N2.getOpcode() == ISD::Constant);
2265 assert(N3.getOpcode() == ISD::Register);
2266
Dan Gohman475871a2008-07-27 21:46:04 +00002267 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002268 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00002269 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002270 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
Evan Chengee568cf2007-07-05 07:15:27 +00002271 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00002272 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengee568cf2007-07-05 07:15:27 +00002273 default: assert(false && "Illegal conditional move type!");
2274 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002275 case MVT::f32:
Jim Grosbache5165492009-11-09 00:11:35 +00002276 Opc = ARM::VNEGScc;
Evan Chengee568cf2007-07-05 07:15:27 +00002277 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002278 case MVT::f64:
Jim Grosbache5165492009-11-09 00:11:35 +00002279 Opc = ARM::VNEGDcc;
Evan Chenge5ad88e2008-12-10 21:54:21 +00002280 break;
Evan Chengee568cf2007-07-05 07:15:27 +00002281 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002282 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00002283 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00002284
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002285 case ARMISD::VZIP: {
2286 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00002287 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002288 switch (VT.getSimpleVT().SimpleTy) {
2289 default: return NULL;
2290 case MVT::v8i8: Opc = ARM::VZIPd8; break;
2291 case MVT::v4i16: Opc = ARM::VZIPd16; break;
2292 case MVT::v2f32:
2293 case MVT::v2i32: Opc = ARM::VZIPd32; break;
2294 case MVT::v16i8: Opc = ARM::VZIPq8; break;
2295 case MVT::v8i16: Opc = ARM::VZIPq16; break;
2296 case MVT::v4f32:
2297 case MVT::v4i32: Opc = ARM::VZIPq32; break;
2298 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00002299 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00002300 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2301 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2302 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00002303 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002304 case ARMISD::VUZP: {
2305 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00002306 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002307 switch (VT.getSimpleVT().SimpleTy) {
2308 default: return NULL;
2309 case MVT::v8i8: Opc = ARM::VUZPd8; break;
2310 case MVT::v4i16: Opc = ARM::VUZPd16; break;
2311 case MVT::v2f32:
2312 case MVT::v2i32: Opc = ARM::VUZPd32; break;
2313 case MVT::v16i8: Opc = ARM::VUZPq8; break;
2314 case MVT::v8i16: Opc = ARM::VUZPq16; break;
2315 case MVT::v4f32:
2316 case MVT::v4i32: Opc = ARM::VUZPq32; break;
2317 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00002318 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00002319 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2320 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2321 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00002322 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002323 case ARMISD::VTRN: {
2324 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00002325 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002326 switch (VT.getSimpleVT().SimpleTy) {
2327 default: return NULL;
2328 case MVT::v8i8: Opc = ARM::VTRNd8; break;
2329 case MVT::v4i16: Opc = ARM::VTRNd16; break;
2330 case MVT::v2f32:
2331 case MVT::v2i32: Opc = ARM::VTRNd32; break;
2332 case MVT::v16i8: Opc = ARM::VTRNq8; break;
2333 case MVT::v8i16: Opc = ARM::VTRNq16; break;
2334 case MVT::v4f32:
2335 case MVT::v4i32: Opc = ARM::VTRNq32; break;
2336 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00002337 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00002338 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2339 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2340 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00002341 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00002342 case ARMISD::BUILD_VECTOR: {
2343 EVT VecVT = N->getValueType(0);
2344 EVT EltVT = VecVT.getVectorElementType();
2345 unsigned NumElts = VecVT.getVectorNumElements();
Duncan Sandscdfad362010-11-03 12:17:33 +00002346 if (EltVT == MVT::f64) {
Bob Wilson40cbe7d2010-06-04 00:04:02 +00002347 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
2348 return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
2349 }
Duncan Sandscdfad362010-11-03 12:17:33 +00002350 assert(EltVT == MVT::f32 && "unexpected type for BUILD_VECTOR");
Bob Wilson40cbe7d2010-06-04 00:04:02 +00002351 if (NumElts == 2)
2352 return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
2353 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
2354 return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
2355 N->getOperand(2), N->getOperand(3));
2356 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00002357
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00002358 case ARMISD::VLD2DUP: {
2359 unsigned Opcodes[] = { ARM::VLD2DUPd8Pseudo, ARM::VLD2DUPd16Pseudo,
2360 ARM::VLD2DUPd32Pseudo };
2361 return SelectVLDDup(N, 2, Opcodes);
2362 }
2363
Bob Wilson86c6d802010-11-29 19:35:29 +00002364 case ARMISD::VLD3DUP: {
2365 unsigned Opcodes[] = { ARM::VLD3DUPd8Pseudo, ARM::VLD3DUPd16Pseudo,
2366 ARM::VLD3DUPd32Pseudo };
2367 return SelectVLDDup(N, 3, Opcodes);
2368 }
2369
Bob Wilson31fb12f2009-08-26 17:39:53 +00002370 case ISD::INTRINSIC_VOID:
2371 case ISD::INTRINSIC_W_CHAIN: {
2372 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Bob Wilson31fb12f2009-08-26 17:39:53 +00002373 switch (IntNo) {
2374 default:
Bob Wilson429009b2010-05-06 16:05:26 +00002375 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00002376
Bob Wilson621f1952010-03-23 05:25:43 +00002377 case Intrinsic::arm_neon_vld1: {
2378 unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
2379 ARM::VLD1d32, ARM::VLD1d64 };
Bob Wilsonffde0802010-09-02 16:00:54 +00002380 unsigned QOpcodes[] = { ARM::VLD1q8Pseudo, ARM::VLD1q16Pseudo,
2381 ARM::VLD1q32Pseudo, ARM::VLD1q64Pseudo };
Bob Wilson621f1952010-03-23 05:25:43 +00002382 return SelectVLD(N, 1, DOpcodes, QOpcodes, 0);
2383 }
2384
Bob Wilson31fb12f2009-08-26 17:39:53 +00002385 case Intrinsic::arm_neon_vld2: {
Bob Wilsonffde0802010-09-02 16:00:54 +00002386 unsigned DOpcodes[] = { ARM::VLD2d8Pseudo, ARM::VLD2d16Pseudo,
2387 ARM::VLD2d32Pseudo, ARM::VLD1q64Pseudo };
2388 unsigned QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
2389 ARM::VLD2q32Pseudo };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002390 return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002391 }
2392
2393 case Intrinsic::arm_neon_vld3: {
Bob Wilsonf5721912010-09-03 18:16:02 +00002394 unsigned DOpcodes[] = { ARM::VLD3d8Pseudo, ARM::VLD3d16Pseudo,
2395 ARM::VLD3d32Pseudo, ARM::VLD1d64TPseudo };
2396 unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
2397 ARM::VLD3q16Pseudo_UPD,
2398 ARM::VLD3q32Pseudo_UPD };
2399 unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
2400 ARM::VLD3q16oddPseudo_UPD,
2401 ARM::VLD3q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002402 return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002403 }
2404
2405 case Intrinsic::arm_neon_vld4: {
Bob Wilsonf5721912010-09-03 18:16:02 +00002406 unsigned DOpcodes[] = { ARM::VLD4d8Pseudo, ARM::VLD4d16Pseudo,
2407 ARM::VLD4d32Pseudo, ARM::VLD1d64QPseudo };
2408 unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
2409 ARM::VLD4q16Pseudo_UPD,
2410 ARM::VLD4q32Pseudo_UPD };
2411 unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
2412 ARM::VLD4q16oddPseudo_UPD,
2413 ARM::VLD4q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002414 return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002415 }
2416
Bob Wilson243fcc52009-09-01 04:26:28 +00002417 case Intrinsic::arm_neon_vld2lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002418 unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo, ARM::VLD2LNd16Pseudo,
2419 ARM::VLD2LNd32Pseudo };
2420 unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo, ARM::VLD2LNq32Pseudo };
2421 return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002422 }
2423
2424 case Intrinsic::arm_neon_vld3lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002425 unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo, ARM::VLD3LNd16Pseudo,
2426 ARM::VLD3LNd32Pseudo };
2427 unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo, ARM::VLD3LNq32Pseudo };
2428 return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002429 }
2430
2431 case Intrinsic::arm_neon_vld4lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002432 unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo, ARM::VLD4LNd16Pseudo,
2433 ARM::VLD4LNd32Pseudo };
2434 unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo, ARM::VLD4LNq32Pseudo };
2435 return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002436 }
2437
Bob Wilson11d98992010-03-23 06:20:33 +00002438 case Intrinsic::arm_neon_vst1: {
2439 unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2440 ARM::VST1d32, ARM::VST1d64 };
Bob Wilsone5ce4f62010-08-28 05:12:57 +00002441 unsigned QOpcodes[] = { ARM::VST1q8Pseudo, ARM::VST1q16Pseudo,
2442 ARM::VST1q32Pseudo, ARM::VST1q64Pseudo };
Bob Wilson11d98992010-03-23 06:20:33 +00002443 return SelectVST(N, 1, DOpcodes, QOpcodes, 0);
2444 }
2445
Bob Wilson31fb12f2009-08-26 17:39:53 +00002446 case Intrinsic::arm_neon_vst2: {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00002447 unsigned DOpcodes[] = { ARM::VST2d8Pseudo, ARM::VST2d16Pseudo,
2448 ARM::VST2d32Pseudo, ARM::VST1q64Pseudo };
2449 unsigned QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
2450 ARM::VST2q32Pseudo };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002451 return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002452 }
2453
2454 case Intrinsic::arm_neon_vst3: {
Bob Wilson01ba4612010-08-26 18:51:29 +00002455 unsigned DOpcodes[] = { ARM::VST3d8Pseudo, ARM::VST3d16Pseudo,
2456 ARM::VST3d32Pseudo, ARM::VST1d64TPseudo };
2457 unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2458 ARM::VST3q16Pseudo_UPD,
2459 ARM::VST3q32Pseudo_UPD };
2460 unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2461 ARM::VST3q16oddPseudo_UPD,
2462 ARM::VST3q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002463 return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002464 }
2465
2466 case Intrinsic::arm_neon_vst4: {
Bob Wilson709d5922010-08-25 23:27:42 +00002467 unsigned DOpcodes[] = { ARM::VST4d8Pseudo, ARM::VST4d16Pseudo,
Bob Wilson70e48b22010-08-26 05:33:30 +00002468 ARM::VST4d32Pseudo, ARM::VST1d64QPseudo };
Bob Wilson709d5922010-08-25 23:27:42 +00002469 unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2470 ARM::VST4q16Pseudo_UPD,
2471 ARM::VST4q32Pseudo_UPD };
2472 unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2473 ARM::VST4q16oddPseudo_UPD,
2474 ARM::VST4q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002475 return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002476 }
Bob Wilson8a3198b2009-09-01 18:51:56 +00002477
2478 case Intrinsic::arm_neon_vst2lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002479 unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo, ARM::VST2LNd16Pseudo,
2480 ARM::VST2LNd32Pseudo };
2481 unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo, ARM::VST2LNq32Pseudo };
2482 return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002483 }
2484
2485 case Intrinsic::arm_neon_vst3lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002486 unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo, ARM::VST3LNd16Pseudo,
2487 ARM::VST3LNd32Pseudo };
2488 unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo, ARM::VST3LNq32Pseudo };
2489 return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002490 }
2491
2492 case Intrinsic::arm_neon_vst4lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002493 unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo, ARM::VST4LNd16Pseudo,
2494 ARM::VST4LNd32Pseudo };
2495 unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo, ARM::VST4LNq32Pseudo };
2496 return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002497 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00002498 }
Bob Wilson429009b2010-05-06 16:05:26 +00002499 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00002500 }
Evan Chengde8aa4e2010-05-05 18:28:36 +00002501
Bob Wilsond491d6e2010-07-06 23:36:25 +00002502 case ISD::INTRINSIC_WO_CHAIN: {
2503 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2504 switch (IntNo) {
2505 default:
2506 break;
2507
2508 case Intrinsic::arm_neon_vtbl2:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002509 return SelectVTBL(N, false, 2, ARM::VTBL2Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002510 case Intrinsic::arm_neon_vtbl3:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002511 return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002512 case Intrinsic::arm_neon_vtbl4:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002513 return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002514
2515 case Intrinsic::arm_neon_vtbx2:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002516 return SelectVTBL(N, true, 2, ARM::VTBX2Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002517 case Intrinsic::arm_neon_vtbx3:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002518 return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002519 case Intrinsic::arm_neon_vtbx4:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002520 return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002521 }
2522 break;
2523 }
2524
Bob Wilson429009b2010-05-06 16:05:26 +00002525 case ISD::CONCAT_VECTORS:
Evan Chengde8aa4e2010-05-05 18:28:36 +00002526 return SelectConcatVector(N);
2527 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00002528
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002529 return SelectCode(N);
Evan Chenga8e29892007-01-19 07:51:42 +00002530}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002531
Bob Wilson224c2442009-05-19 05:53:42 +00002532bool ARMDAGToDAGISel::
2533SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2534 std::vector<SDValue> &OutOps) {
2535 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
Bob Wilson765cc0b2009-10-13 20:50:28 +00002536 // Require the address to be in a register. That is safe for all ARM
2537 // variants and it is hard to do anything much smarter without knowing
2538 // how the operand is used.
2539 OutOps.push_back(Op);
Bob Wilson224c2442009-05-19 05:53:42 +00002540 return false;
2541}
2542
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002543/// createARMISelDag - This pass converts a legalized DAG into a
2544/// ARM-specific DAG, ready for instruction scheduling.
2545///
Bob Wilson522ce972009-09-28 14:30:20 +00002546FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2547 CodeGenOpt::Level OptLevel) {
2548 return new ARMDAGToDAGISel(TM, OptLevel);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002549}