blob: 5cfeb290fe27acf6da4653369e962d94990a91b4 [file] [log] [blame]
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001//===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
Dale Johannesen51e28e62010-06-03 21:09:53 +000014#define DEBUG_TYPE "arm-isel"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000015#include "ARM.h"
Evan Chenge5ad88e2008-12-10 21:54:21 +000016#include "ARMAddressingModes.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000017#include "ARMTargetMachine.h"
Rafael Espindola84b19be2006-07-16 01:02:57 +000018#include "llvm/CallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000020#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/Intrinsics.h"
Owen Anderson9adc0ab2009-07-14 23:09:55 +000023#include "llvm/LLVMContext.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/SelectionDAG.h"
28#include "llvm/CodeGen/SelectionDAGISel.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000029#include "llvm/Target/TargetLowering.h"
Chris Lattner72939122007-05-03 00:32:00 +000030#include "llvm/Target/TargetOptions.h"
Evan Cheng94cc6d32010-05-04 20:39:49 +000031#include "llvm/Support/CommandLine.h"
Chris Lattner3d62d782008-02-03 05:43:57 +000032#include "llvm/Support/Compiler.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000033#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
36
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000037using namespace llvm;
38
Evan Chenga2c519b2010-07-30 23:33:54 +000039static cl::opt<bool>
40DisableShifterOp("disable-shifter-op", cl::Hidden,
41 cl::desc("Disable isel of shifter-op"),
42 cl::init(false));
43
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000044//===--------------------------------------------------------------------===//
45/// ARMDAGToDAGISel - ARM specific code to select ARM machine
46/// instructions for SelectionDAG operations.
47///
48namespace {
49class ARMDAGToDAGISel : public SelectionDAGISel {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000050 ARMBaseTargetMachine &TM;
Evan Cheng3f7eb8e2008-09-18 07:24:33 +000051
Evan Chenga8e29892007-01-19 07:51:42 +000052 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
53 /// make the right decision when generating code for different targets.
54 const ARMSubtarget *Subtarget;
55
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000056public:
Bob Wilson522ce972009-09-28 14:30:20 +000057 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm,
58 CodeGenOpt::Level OptLevel)
59 : SelectionDAGISel(tm, OptLevel), TM(tm),
Evan Chenga8e29892007-01-19 07:51:42 +000060 Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000061 }
62
Evan Chenga8e29892007-01-19 07:51:42 +000063 virtual const char *getPassName() const {
64 return "ARM Instruction Selection";
Anton Korobeynikov52237112009-06-17 18:13:58 +000065 }
66
Bob Wilsonaf4a8912009-10-08 18:51:31 +000067 /// getI32Imm - Return a target constant of type i32 with the specified
68 /// value.
Anton Korobeynikov52237112009-06-17 18:13:58 +000069 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +000070 return CurDAG->getTargetConstant(Imm, MVT::i32);
Anton Korobeynikov52237112009-06-17 18:13:58 +000071 }
72
Dan Gohmaneeb3a002010-01-05 01:24:18 +000073 SDNode *Select(SDNode *N);
Evan Cheng014bf212010-02-15 19:41:07 +000074
Dan Gohmaneeb3a002010-01-05 01:24:18 +000075 bool SelectShifterOperandReg(SDNode *Op, SDValue N, SDValue &A,
Evan Cheng055b0312009-06-29 07:51:04 +000076 SDValue &B, SDValue &C);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000077 bool SelectAddrMode2(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000078 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000079 bool SelectAddrMode2Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +000080 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000081 bool SelectAddrMode3(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000082 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000083 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +000084 SDValue &Offset, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000085 bool SelectAddrMode4(SDNode *Op, SDValue N, SDValue &Addr,
Anton Korobeynikovbaf31082009-08-08 13:35:48 +000086 SDValue &Mode);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000087 bool SelectAddrMode5(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000088 SDValue &Offset);
Bob Wilson226036e2010-03-20 22:13:40 +000089 bool SelectAddrMode6(SDNode *Op, SDValue N, SDValue &Addr, SDValue &Align);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000090
Dan Gohmaneeb3a002010-01-05 01:24:18 +000091 bool SelectAddrModePC(SDNode *Op, SDValue N, SDValue &Offset,
Bob Wilson8b024a52009-07-01 23:16:05 +000092 SDValue &Label);
Evan Chenga8e29892007-01-19 07:51:42 +000093
Dan Gohmaneeb3a002010-01-05 01:24:18 +000094 bool SelectThumbAddrModeRR(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000095 SDValue &Offset);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000096 bool SelectThumbAddrModeRI5(SDNode *Op, SDValue N, unsigned Scale,
Dan Gohman475871a2008-07-27 21:46:04 +000097 SDValue &Base, SDValue &OffImm,
98 SDValue &Offset);
Dan Gohmaneeb3a002010-01-05 01:24:18 +000099 bool SelectThumbAddrModeS1(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000100 SDValue &OffImm, SDValue &Offset);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000101 bool SelectThumbAddrModeS2(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000102 SDValue &OffImm, SDValue &Offset);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000103 bool SelectThumbAddrModeS4(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000104 SDValue &OffImm, SDValue &Offset);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000105 bool SelectThumbAddrModeSP(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000106 SDValue &OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000107
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000108 bool SelectT2ShifterOperandReg(SDNode *Op, SDValue N,
Evan Cheng9cb9e672009-06-27 02:26:13 +0000109 SDValue &BaseReg, SDValue &Opc);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000110 bool SelectT2AddrModeImm12(SDNode *Op, SDValue N, SDValue &Base,
Evan Cheng055b0312009-06-29 07:51:04 +0000111 SDValue &OffImm);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000112 bool SelectT2AddrModeImm8(SDNode *Op, SDValue N, SDValue &Base,
Evan Cheng055b0312009-06-29 07:51:04 +0000113 SDValue &OffImm);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000114 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Chenge88d5ce2009-07-02 07:28:31 +0000115 SDValue &OffImm);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000116 bool SelectT2AddrModeSoReg(SDNode *Op, SDValue N, SDValue &Base,
Evan Cheng055b0312009-06-29 07:51:04 +0000117 SDValue &OffReg, SDValue &ShImm);
118
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +0000119 inline bool Pred_so_imm(SDNode *inN) const {
120 ConstantSDNode *N = cast<ConstantSDNode>(inN);
121 return ARM_AM::getSOImmVal(N->getZExtValue()) != -1;
122 }
123
124 inline bool Pred_t2_so_imm(SDNode *inN) const {
125 ConstantSDNode *N = cast<ConstantSDNode>(inN);
126 return ARM_AM::getT2SOImmVal(N->getZExtValue()) != -1;
127 }
128
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000129 // Include the pieces autogenerated from the target description.
130#include "ARMGenDAGISel.inc"
Bob Wilson224c2442009-05-19 05:53:42 +0000131
132private:
Evan Chenge88d5ce2009-07-02 07:28:31 +0000133 /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
134 /// ARM.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000135 SDNode *SelectARMIndexedLoad(SDNode *N);
136 SDNode *SelectT2IndexedLoad(SDNode *N);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000137
Bob Wilson621f1952010-03-23 05:25:43 +0000138 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
139 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson3e36f132009-10-14 17:28:52 +0000140 /// loads of D registers and even subregs and odd subregs of Q registers.
Bob Wilson621f1952010-03-23 05:25:43 +0000141 /// For NumVecs <= 2, QOpcodes1 is not used.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000142 SDNode *SelectVLD(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson3e36f132009-10-14 17:28:52 +0000143 unsigned *QOpcodes0, unsigned *QOpcodes1);
144
Bob Wilson24f995d2009-10-14 18:32:29 +0000145 /// SelectVST - Select NEON store intrinsics. NumVecs should
Bob Wilson11d98992010-03-23 06:20:33 +0000146 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson24f995d2009-10-14 18:32:29 +0000147 /// stores of D registers and even subregs and odd subregs of Q registers.
Bob Wilson11d98992010-03-23 06:20:33 +0000148 /// For NumVecs <= 2, QOpcodes1 is not used.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000149 SDNode *SelectVST(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson24f995d2009-10-14 18:32:29 +0000150 unsigned *QOpcodes0, unsigned *QOpcodes1);
151
Bob Wilson96493442009-10-14 16:46:45 +0000152 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
Bob Wilsona7c397c2009-10-14 16:19:03 +0000153 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson8466fa12010-09-13 23:01:35 +0000154 /// load/store of D registers and Q registers.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000155 SDNode *SelectVLDSTLane(SDNode *N, bool IsLoad, unsigned NumVecs,
Bob Wilson8466fa12010-09-13 23:01:35 +0000156 unsigned *DOpcodes, unsigned *QOpcodes);
Bob Wilsona7c397c2009-10-14 16:19:03 +0000157
Bob Wilson78dfbc32010-07-07 00:08:54 +0000158 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2,
159 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be
160 /// generated to force the table registers to be consecutive.
161 SDNode *SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
Bob Wilsond491d6e2010-07-06 23:36:25 +0000162
Sandeep Patel4e1ed882009-10-13 20:25:58 +0000163 /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
Jim Grosbach3a1287b2010-04-22 23:24:18 +0000164 SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
Sandeep Patel47eedaa2009-10-13 18:59:48 +0000165
Evan Cheng07ba9062009-11-19 21:45:22 +0000166 /// SelectCMOVOp - Select CMOV instructions for ARM.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000167 SDNode *SelectCMOVOp(SDNode *N);
168 SDNode *SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000169 ARMCC::CondCodes CCVal, SDValue CCR,
170 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000171 SDNode *SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000172 ARMCC::CondCodes CCVal, SDValue CCR,
173 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000174 SDNode *SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000175 ARMCC::CondCodes CCVal, SDValue CCR,
176 SDValue InFlag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000177 SDNode *SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +0000178 ARMCC::CondCodes CCVal, SDValue CCR,
179 SDValue InFlag);
Evan Cheng07ba9062009-11-19 21:45:22 +0000180
Evan Chengde8aa4e2010-05-05 18:28:36 +0000181 SDNode *SelectConcatVector(SDNode *N);
182
Evan Chengaf4550f2009-07-02 01:23:32 +0000183 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
184 /// inline asm expressions.
185 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
186 char ConstraintCode,
187 std::vector<SDValue> &OutOps);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000188
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000189 // Form pairs of consecutive S, D, or Q registers.
190 SDNode *PairSRegs(EVT VT, SDValue V0, SDValue V1);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000191 SDNode *PairDRegs(EVT VT, SDValue V0, SDValue V1);
Evan Cheng603afbf2010-05-10 17:34:18 +0000192 SDNode *PairQRegs(EVT VT, SDValue V0, SDValue V1);
193
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000194 // Form sequences of 4 consecutive S, D, or Q registers.
195 SDNode *QuadSRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Evan Cheng603afbf2010-05-10 17:34:18 +0000196 SDNode *QuadDRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Evan Cheng8f6de382010-05-16 03:27:48 +0000197 SDNode *QuadQRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000198};
Evan Chenga8e29892007-01-19 07:51:42 +0000199}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000200
Sandeep Patel47eedaa2009-10-13 18:59:48 +0000201/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
202/// operand. If so Imm will receive the 32-bit value.
203static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
204 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
205 Imm = cast<ConstantSDNode>(N)->getZExtValue();
206 return true;
207 }
208 return false;
209}
210
211// isInt32Immediate - This method tests to see if a constant operand.
212// If so Imm will receive the 32 bit value.
213static bool isInt32Immediate(SDValue N, unsigned &Imm) {
214 return isInt32Immediate(N.getNode(), Imm);
215}
216
217// isOpcWithIntImmediate - This method tests to see if the node is a specific
218// opcode and that it has a immediate integer right operand.
219// If so Imm will receive the 32 bit value.
220static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
221 return N->getOpcode() == Opc &&
222 isInt32Immediate(N->getOperand(1).getNode(), Imm);
223}
224
225
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000226bool ARMDAGToDAGISel::SelectShifterOperandReg(SDNode *Op,
Evan Cheng055b0312009-06-29 07:51:04 +0000227 SDValue N,
228 SDValue &BaseReg,
229 SDValue &ShReg,
230 SDValue &Opc) {
Evan Chenga2c519b2010-07-30 23:33:54 +0000231 if (DisableShifterOp)
232 return false;
233
Evan Cheng055b0312009-06-29 07:51:04 +0000234 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
235
236 // Don't match base register only case. That is matched to a separate
237 // lower complexity pattern with explicit register operand.
238 if (ShOpcVal == ARM_AM::no_shift) return false;
Jim Grosbach764ab522009-08-11 15:33:49 +0000239
Evan Cheng055b0312009-06-29 07:51:04 +0000240 BaseReg = N.getOperand(0);
241 unsigned ShImmVal = 0;
242 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 ShReg = CurDAG->getRegister(0, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000244 ShImmVal = RHS->getZExtValue() & 31;
245 } else {
246 ShReg = N.getOperand(1);
247 }
248 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000250 return true;
251}
252
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000253bool ARMDAGToDAGISel::SelectAddrMode2(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000254 SDValue &Base, SDValue &Offset,
255 SDValue &Opc) {
Evan Chenga13fd102007-03-13 21:05:54 +0000256 if (N.getOpcode() == ISD::MUL) {
257 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
258 // X * [3,5,9] -> X + X * [2,4,8] etc.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000259 int RHSC = (int)RHS->getZExtValue();
Evan Chenga13fd102007-03-13 21:05:54 +0000260 if (RHSC & 1) {
261 RHSC = RHSC & ~1;
262 ARM_AM::AddrOpc AddSub = ARM_AM::add;
263 if (RHSC < 0) {
264 AddSub = ARM_AM::sub;
265 RHSC = - RHSC;
266 }
267 if (isPowerOf2_32(RHSC)) {
268 unsigned ShAmt = Log2_32(RHSC);
269 Base = Offset = N.getOperand(0);
270 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
271 ARM_AM::lsl),
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 MVT::i32);
Evan Chenga13fd102007-03-13 21:05:54 +0000273 return true;
274 }
275 }
276 }
277 }
278
Evan Chenga8e29892007-01-19 07:51:42 +0000279 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
280 Base = N;
281 if (N.getOpcode() == ISD::FrameIndex) {
282 int FI = cast<FrameIndexSDNode>(N)->getIndex();
283 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000284 } else if (N.getOpcode() == ARMISD::Wrapper &&
285 !(Subtarget->useMovt() &&
286 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000287 Base = N.getOperand(0);
288 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000290 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
291 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000293 return true;
294 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000295
Evan Chenga8e29892007-01-19 07:51:42 +0000296 // Match simple R +/- imm12 operands.
297 if (N.getOpcode() == ISD::ADD)
298 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000299 int RHSC = (int)RHS->getZExtValue();
Evan Chenge966d642007-01-24 02:45:25 +0000300 if ((RHSC >= 0 && RHSC < 0x1000) ||
301 (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
Evan Chenga8e29892007-01-19 07:51:42 +0000302 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000303 if (Base.getOpcode() == ISD::FrameIndex) {
304 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
305 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
306 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000308
309 ARM_AM::AddrOpc AddSub = ARM_AM::add;
310 if (RHSC < 0) {
311 AddSub = ARM_AM::sub;
312 RHSC = - RHSC;
313 }
314 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
Evan Chenga8e29892007-01-19 07:51:42 +0000315 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000317 return true;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000318 }
Evan Chenga8e29892007-01-19 07:51:42 +0000319 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000320
Johnny Chen6a3b5ee2009-10-27 17:25:15 +0000321 // Otherwise this is R +/- [possibly shifted] R.
Evan Chenga8e29892007-01-19 07:51:42 +0000322 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
323 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
324 unsigned ShAmt = 0;
Jim Grosbach764ab522009-08-11 15:33:49 +0000325
Evan Chenga8e29892007-01-19 07:51:42 +0000326 Base = N.getOperand(0);
327 Offset = N.getOperand(1);
Jim Grosbach764ab522009-08-11 15:33:49 +0000328
Evan Chenga8e29892007-01-19 07:51:42 +0000329 if (ShOpcVal != ARM_AM::no_shift) {
330 // Check to see if the RHS of the shift is a constant, if not, we can't fold
331 // it.
332 if (ConstantSDNode *Sh =
333 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000334 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000335 Offset = N.getOperand(1).getOperand(0);
336 } else {
337 ShOpcVal = ARM_AM::no_shift;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000338 }
339 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000340
Evan Chenga8e29892007-01-19 07:51:42 +0000341 // Try matching (R shl C) + (R).
342 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
343 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
344 if (ShOpcVal != ARM_AM::no_shift) {
345 // Check to see if the RHS of the shift is a constant, if not, we can't
346 // fold it.
347 if (ConstantSDNode *Sh =
348 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000349 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000350 Offset = N.getOperand(0).getOperand(0);
351 Base = N.getOperand(1);
352 } else {
353 ShOpcVal = ARM_AM::no_shift;
354 }
355 }
356 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000357
Evan Chenga8e29892007-01-19 07:51:42 +0000358 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000360 return true;
361}
362
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000363bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000364 SDValue &Offset, SDValue &Opc) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000365 unsigned Opcode = Op->getOpcode();
Evan Chenga8e29892007-01-19 07:51:42 +0000366 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
367 ? cast<LoadSDNode>(Op)->getAddressingMode()
368 : cast<StoreSDNode>(Op)->getAddressingMode();
369 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
370 ? ARM_AM::add : ARM_AM::sub;
371 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000372 int Val = (int)C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000373 if (Val >= 0 && Val < 0x1000) { // 12 bits.
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000375 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
376 ARM_AM::no_shift),
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000378 return true;
379 }
380 }
381
382 Offset = N;
383 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
384 unsigned ShAmt = 0;
385 if (ShOpcVal != ARM_AM::no_shift) {
386 // Check to see if the RHS of the shift is a constant, if not, we can't fold
387 // it.
388 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000389 ShAmt = Sh->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000390 Offset = N.getOperand(0);
391 } else {
392 ShOpcVal = ARM_AM::no_shift;
393 }
394 }
395
396 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 MVT::i32);
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000398 return true;
399}
400
Evan Chenga8e29892007-01-19 07:51:42 +0000401
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000402bool ARMDAGToDAGISel::SelectAddrMode3(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000403 SDValue &Base, SDValue &Offset,
404 SDValue &Opc) {
Evan Chenga8e29892007-01-19 07:51:42 +0000405 if (N.getOpcode() == ISD::SUB) {
406 // X - C is canonicalize to X + -C, no need to handle it here.
407 Base = N.getOperand(0);
408 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000410 return true;
411 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000412
Evan Chenga8e29892007-01-19 07:51:42 +0000413 if (N.getOpcode() != ISD::ADD) {
414 Base = N;
415 if (N.getOpcode() == ISD::FrameIndex) {
416 int FI = cast<FrameIndexSDNode>(N)->getIndex();
417 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
418 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 Offset = CurDAG->getRegister(0, MVT::i32);
420 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000421 return true;
422 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000423
Evan Chenga8e29892007-01-19 07:51:42 +0000424 // If the RHS is +/- imm8, fold into addr mode.
425 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000426 int RHSC = (int)RHS->getZExtValue();
Evan Chenge966d642007-01-24 02:45:25 +0000427 if ((RHSC >= 0 && RHSC < 256) ||
428 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000429 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000430 if (Base.getOpcode() == ISD::FrameIndex) {
431 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
432 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
433 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000435
436 ARM_AM::AddrOpc AddSub = ARM_AM::add;
437 if (RHSC < 0) {
438 AddSub = ARM_AM::sub;
439 RHSC = - RHSC;
440 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000442 return true;
443 }
444 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000445
Evan Chenga8e29892007-01-19 07:51:42 +0000446 Base = N.getOperand(0);
447 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000449 return true;
450}
451
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000452bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000453 SDValue &Offset, SDValue &Opc) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000454 unsigned Opcode = Op->getOpcode();
Evan Chenga8e29892007-01-19 07:51:42 +0000455 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
456 ? cast<LoadSDNode>(Op)->getAddressingMode()
457 : cast<StoreSDNode>(Op)->getAddressingMode();
458 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
459 ? ARM_AM::add : ARM_AM::sub;
460 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000461 int Val = (int)C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000462 if (Val >= 0 && Val < 256) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 Offset = CurDAG->getRegister(0, MVT::i32);
464 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000465 return true;
466 }
467 }
468
469 Offset = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000471 return true;
472}
473
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000474bool ARMDAGToDAGISel::SelectAddrMode4(SDNode *Op, SDValue N,
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000475 SDValue &Addr, SDValue &Mode) {
476 Addr = N;
Bob Wilsonfd7fd942010-08-28 00:20:11 +0000477 Mode = CurDAG->getTargetConstant(ARM_AM::getAM4ModeImm(ARM_AM::ia), MVT::i32);
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000478 return true;
479}
Evan Chenga8e29892007-01-19 07:51:42 +0000480
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000481bool ARMDAGToDAGISel::SelectAddrMode5(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000482 SDValue &Base, SDValue &Offset) {
Evan Chenga8e29892007-01-19 07:51:42 +0000483 if (N.getOpcode() != ISD::ADD) {
484 Base = N;
485 if (N.getOpcode() == ISD::FrameIndex) {
486 int FI = cast<FrameIndexSDNode>(N)->getIndex();
487 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000488 } else if (N.getOpcode() == ARMISD::Wrapper &&
489 !(Subtarget->useMovt() &&
490 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000491 Base = N.getOperand(0);
492 }
493 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson825b72b2009-08-11 20:47:22 +0000494 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000495 return true;
496 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000497
Evan Chenga8e29892007-01-19 07:51:42 +0000498 // If the RHS is +/- imm8, fold into addr mode.
499 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000500 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000501 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
502 RHSC >>= 2;
Evan Chenge966d642007-01-24 02:45:25 +0000503 if ((RHSC >= 0 && RHSC < 256) ||
504 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000505 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000506 if (Base.getOpcode() == ISD::FrameIndex) {
507 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
508 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
509 }
510
511 ARM_AM::AddrOpc AddSub = ARM_AM::add;
512 if (RHSC < 0) {
513 AddSub = ARM_AM::sub;
514 RHSC = - RHSC;
515 }
516 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000518 return true;
519 }
520 }
521 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000522
Evan Chenga8e29892007-01-19 07:51:42 +0000523 Base = N;
524 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000526 return true;
527}
528
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000529bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Op, SDValue N,
Bob Wilson226036e2010-03-20 22:13:40 +0000530 SDValue &Addr, SDValue &Align) {
Bob Wilson8b024a52009-07-01 23:16:05 +0000531 Addr = N;
Jim Grosbach8a5ec862009-11-07 21:25:39 +0000532 // Default to no alignment.
533 Align = CurDAG->getTargetConstant(0, MVT::i32);
Bob Wilson8b024a52009-07-01 23:16:05 +0000534 return true;
535}
536
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000537bool ARMDAGToDAGISel::SelectAddrModePC(SDNode *Op, SDValue N,
Evan Chengbba9f5f2009-08-14 19:01:37 +0000538 SDValue &Offset, SDValue &Label) {
Evan Chenga8e29892007-01-19 07:51:42 +0000539 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
540 Offset = N.getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000541 SDValue N1 = N.getOperand(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000542 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000544 return true;
545 }
546 return false;
547}
548
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000549bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000550 SDValue &Base, SDValue &Offset){
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000551 // FIXME dl should come from the parent load or store, not the address
Evan Chengc38f2bc2007-01-23 22:59:13 +0000552 if (N.getOpcode() != ISD::ADD) {
Evan Cheng2f297df2009-07-11 07:08:13 +0000553 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
Dan Gohmane368b462010-06-18 14:22:04 +0000554 if (!NC || !NC->isNullValue())
Evan Cheng2f297df2009-07-11 07:08:13 +0000555 return false;
556
557 Base = Offset = N;
Evan Chengc38f2bc2007-01-23 22:59:13 +0000558 return true;
559 }
560
Evan Chenga8e29892007-01-19 07:51:42 +0000561 Base = N.getOperand(0);
562 Offset = N.getOperand(1);
563 return true;
564}
565
Evan Cheng79d43262007-01-24 02:21:22 +0000566bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000567ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000568 unsigned Scale, SDValue &Base,
569 SDValue &OffImm, SDValue &Offset) {
Evan Cheng79d43262007-01-24 02:21:22 +0000570 if (Scale == 4) {
Dan Gohman475871a2008-07-27 21:46:04 +0000571 SDValue TmpBase, TmpOffImm;
Evan Cheng79d43262007-01-24 02:21:22 +0000572 if (SelectThumbAddrModeSP(Op, N, TmpBase, TmpOffImm))
573 return false; // We want to select tLDRspi / tSTRspi instead.
Evan Cheng012f2d92007-01-24 08:53:17 +0000574 if (N.getOpcode() == ARMISD::Wrapper &&
575 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
576 return false; // We want to select tLDRpci instead.
Evan Cheng79d43262007-01-24 02:21:22 +0000577 }
578
Evan Chenga8e29892007-01-19 07:51:42 +0000579 if (N.getOpcode() != ISD::ADD) {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000580 if (N.getOpcode() == ARMISD::Wrapper &&
581 !(Subtarget->useMovt() &&
582 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
583 Base = N.getOperand(0);
584 } else
585 Base = N;
586
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 Offset = CurDAG->getRegister(0, MVT::i32);
588 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000589 return true;
590 }
591
Evan Chengad0e4652007-02-06 00:22:06 +0000592 // Thumb does not have [sp, r] address mode.
593 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
594 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
595 if ((LHSR && LHSR->getReg() == ARM::SP) ||
596 (RHSR && RHSR->getReg() == ARM::SP)) {
597 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 Offset = CurDAG->getRegister(0, MVT::i32);
599 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengad0e4652007-02-06 00:22:06 +0000600 return true;
601 }
602
Evan Chenga8e29892007-01-19 07:51:42 +0000603 // If the RHS is + imm5 * scale, fold into addr mode.
604 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000605 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000606 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
607 RHSC /= Scale;
608 if (RHSC >= 0 && RHSC < 32) {
609 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 Offset = CurDAG->getRegister(0, MVT::i32);
611 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000612 return true;
613 }
614 }
615 }
616
Evan Chengc38f2bc2007-01-23 22:59:13 +0000617 Base = N.getOperand(0);
618 Offset = N.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000620 return true;
Evan Chenga8e29892007-01-19 07:51:42 +0000621}
622
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000623bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000624 SDValue &Base, SDValue &OffImm,
625 SDValue &Offset) {
Evan Chengcea117d2007-01-30 02:35:32 +0000626 return SelectThumbAddrModeRI5(Op, N, 1, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000627}
628
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000629bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000630 SDValue &Base, SDValue &OffImm,
631 SDValue &Offset) {
Evan Chengcea117d2007-01-30 02:35:32 +0000632 return SelectThumbAddrModeRI5(Op, N, 2, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000633}
634
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000635bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000636 SDValue &Base, SDValue &OffImm,
637 SDValue &Offset) {
Evan Chengcea117d2007-01-30 02:35:32 +0000638 return SelectThumbAddrModeRI5(Op, N, 4, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000639}
640
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000641bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000642 SDValue &Base, SDValue &OffImm) {
Evan Chenga8e29892007-01-19 07:51:42 +0000643 if (N.getOpcode() == ISD::FrameIndex) {
644 int FI = cast<FrameIndexSDNode>(N)->getIndex();
645 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000647 return true;
648 }
Evan Cheng79d43262007-01-24 02:21:22 +0000649
Evan Chengad0e4652007-02-06 00:22:06 +0000650 if (N.getOpcode() != ISD::ADD)
651 return false;
652
653 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
Evan Cheng8c1a73a2007-02-06 09:11:20 +0000654 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
655 (LHSR && LHSR->getReg() == ARM::SP)) {
Evan Cheng79d43262007-01-24 02:21:22 +0000656 // If the RHS is + imm8 * scale, fold into addr mode.
657 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000658 int RHSC = (int)RHS->getZExtValue();
Evan Cheng79d43262007-01-24 02:21:22 +0000659 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
660 RHSC >>= 2;
661 if (RHSC >= 0 && RHSC < 256) {
Evan Chengad0e4652007-02-06 00:22:06 +0000662 Base = N.getOperand(0);
Evan Cheng8c1a73a2007-02-06 09:11:20 +0000663 if (Base.getOpcode() == ISD::FrameIndex) {
664 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
665 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
666 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Cheng79d43262007-01-24 02:21:22 +0000668 return true;
669 }
670 }
671 }
672 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000673
Evan Chenga8e29892007-01-19 07:51:42 +0000674 return false;
675}
676
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000677bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDNode *Op, SDValue N,
Evan Cheng9cb9e672009-06-27 02:26:13 +0000678 SDValue &BaseReg,
679 SDValue &Opc) {
Evan Chenga2c519b2010-07-30 23:33:54 +0000680 if (DisableShifterOp)
681 return false;
682
Evan Cheng9cb9e672009-06-27 02:26:13 +0000683 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
684
685 // Don't match base register only case. That is matched to a separate
686 // lower complexity pattern with explicit register operand.
687 if (ShOpcVal == ARM_AM::no_shift) return false;
688
689 BaseReg = N.getOperand(0);
690 unsigned ShImmVal = 0;
691 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
692 ShImmVal = RHS->getZExtValue() & 31;
693 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
694 return true;
695 }
696
697 return false;
698}
699
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000700bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDNode *Op, SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000701 SDValue &Base, SDValue &OffImm) {
702 // Match simple R + imm12 operands.
David Goodwin31e7eba2009-07-20 15:55:39 +0000703
Evan Cheng3a214252009-08-11 08:52:18 +0000704 // Base only.
705 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
David Goodwin31e7eba2009-07-20 15:55:39 +0000706 if (N.getOpcode() == ISD::FrameIndex) {
Evan Cheng3a214252009-08-11 08:52:18 +0000707 // Match frame index...
David Goodwin31e7eba2009-07-20 15:55:39 +0000708 int FI = cast<FrameIndexSDNode>(N)->getIndex();
709 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
David Goodwin31e7eba2009-07-20 15:55:39 +0000711 return true;
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000712 } else if (N.getOpcode() == ARMISD::Wrapper &&
713 !(Subtarget->useMovt() &&
714 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
Evan Cheng3a214252009-08-11 08:52:18 +0000715 Base = N.getOperand(0);
716 if (Base.getOpcode() == ISD::TargetConstantPool)
717 return false; // We want to select t2LDRpci instead.
718 } else
719 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng3a214252009-08-11 08:52:18 +0000721 return true;
David Goodwin31e7eba2009-07-20 15:55:39 +0000722 }
Evan Cheng055b0312009-06-29 07:51:04 +0000723
724 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Evan Cheng3a214252009-08-11 08:52:18 +0000725 if (SelectT2AddrModeImm8(Op, N, Base, OffImm))
726 // Let t2LDRi8 handle (R - imm8).
727 return false;
728
Evan Cheng055b0312009-06-29 07:51:04 +0000729 int RHSC = (int)RHS->getZExtValue();
David Goodwind8c95b52009-07-30 18:56:48 +0000730 if (N.getOpcode() == ISD::SUB)
731 RHSC = -RHSC;
732
733 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
Evan Cheng055b0312009-06-29 07:51:04 +0000734 Base = N.getOperand(0);
David Goodwind8c95b52009-07-30 18:56:48 +0000735 if (Base.getOpcode() == ISD::FrameIndex) {
736 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
737 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
738 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000740 return true;
741 }
742 }
743
Evan Cheng3a214252009-08-11 08:52:18 +0000744 // Base only.
745 Base = N;
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Cheng3a214252009-08-11 08:52:18 +0000747 return true;
Evan Cheng055b0312009-06-29 07:51:04 +0000748}
749
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000750bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDNode *Op, SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000751 SDValue &Base, SDValue &OffImm) {
David Goodwind8c95b52009-07-30 18:56:48 +0000752 // Match simple R - imm8 operands.
Evan Cheng3a214252009-08-11 08:52:18 +0000753 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::SUB) {
David Goodwin07337c02009-07-30 22:45:52 +0000754 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
755 int RHSC = (int)RHS->getSExtValue();
756 if (N.getOpcode() == ISD::SUB)
757 RHSC = -RHSC;
Jim Grosbach764ab522009-08-11 15:33:49 +0000758
Evan Cheng3a214252009-08-11 08:52:18 +0000759 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
760 Base = N.getOperand(0);
David Goodwin07337c02009-07-30 22:45:52 +0000761 if (Base.getOpcode() == ISD::FrameIndex) {
762 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
763 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
764 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
David Goodwin07337c02009-07-30 22:45:52 +0000766 return true;
Evan Cheng055b0312009-06-29 07:51:04 +0000767 }
Evan Cheng055b0312009-06-29 07:51:04 +0000768 }
769 }
770
771 return false;
772}
773
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000774bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Chenge88d5ce2009-07-02 07:28:31 +0000775 SDValue &OffImm){
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000776 unsigned Opcode = Op->getOpcode();
Evan Chenge88d5ce2009-07-02 07:28:31 +0000777 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
778 ? cast<LoadSDNode>(Op)->getAddressingMode()
779 : cast<StoreSDNode>(Op)->getAddressingMode();
780 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N)) {
781 int RHSC = (int)RHS->getZExtValue();
782 if (RHSC >= 0 && RHSC < 0x100) { // 8 bits.
David Goodwin4cb73522009-07-14 21:29:29 +0000783 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
785 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000786 return true;
787 }
788 }
789
790 return false;
791}
792
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000793bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDNode *Op, SDValue N,
Evan Cheng055b0312009-06-29 07:51:04 +0000794 SDValue &Base,
795 SDValue &OffReg, SDValue &ShImm) {
Evan Cheng3a214252009-08-11 08:52:18 +0000796 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
797 if (N.getOpcode() != ISD::ADD)
798 return false;
Evan Cheng055b0312009-06-29 07:51:04 +0000799
Evan Cheng3a214252009-08-11 08:52:18 +0000800 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
801 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
802 int RHSC = (int)RHS->getZExtValue();
803 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
804 return false;
805 else if (RHSC < 0 && RHSC >= -255) // 8 bits
David Goodwind8c95b52009-07-30 18:56:48 +0000806 return false;
807 }
808
Evan Cheng055b0312009-06-29 07:51:04 +0000809 // Look for (R + R) or (R + (R << [1,2,3])).
810 unsigned ShAmt = 0;
811 Base = N.getOperand(0);
812 OffReg = N.getOperand(1);
813
814 // Swap if it is ((R << c) + R).
815 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
816 if (ShOpcVal != ARM_AM::lsl) {
817 ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
818 if (ShOpcVal == ARM_AM::lsl)
819 std::swap(Base, OffReg);
Jim Grosbach764ab522009-08-11 15:33:49 +0000820 }
821
Evan Cheng055b0312009-06-29 07:51:04 +0000822 if (ShOpcVal == ARM_AM::lsl) {
823 // Check to see if the RHS of the shift is a constant, if not, we can't fold
824 // it.
825 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
826 ShAmt = Sh->getZExtValue();
827 if (ShAmt >= 4) {
828 ShAmt = 0;
829 ShOpcVal = ARM_AM::no_shift;
830 } else
831 OffReg = OffReg.getOperand(0);
832 } else {
833 ShOpcVal = ARM_AM::no_shift;
834 }
David Goodwin7ecc8502009-07-15 15:50:19 +0000835 }
Jim Grosbach764ab522009-08-11 15:33:49 +0000836
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
Evan Cheng055b0312009-06-29 07:51:04 +0000838
839 return true;
840}
841
842//===--------------------------------------------------------------------===//
843
Evan Chengee568cf2007-07-05 07:15:27 +0000844/// getAL - Returns a ARMCC::AL immediate node.
Dan Gohman475871a2008-07-27 21:46:04 +0000845static inline SDValue getAL(SelectionDAG *CurDAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
Evan Cheng44bec522007-05-15 01:29:07 +0000847}
848
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000849SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
850 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengaf4550f2009-07-02 01:23:32 +0000851 ISD::MemIndexedMode AM = LD->getAddressingMode();
852 if (AM == ISD::UNINDEXED)
853 return NULL;
854
Owen Andersone50ed302009-08-10 22:56:29 +0000855 EVT LoadedVT = LD->getMemoryVT();
Evan Chengaf4550f2009-07-02 01:23:32 +0000856 SDValue Offset, AMOpc;
857 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
858 unsigned Opcode = 0;
859 bool Match = false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 if (LoadedVT == MVT::i32 &&
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000861 SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000862 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
863 Match = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 } else if (LoadedVT == MVT::i16 &&
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000865 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000866 Match = true;
867 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
868 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
869 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000871 if (LD->getExtensionType() == ISD::SEXTLOAD) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000872 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000873 Match = true;
874 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
875 }
876 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000877 if (SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengaf4550f2009-07-02 01:23:32 +0000878 Match = true;
879 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
880 }
881 }
882 }
883
884 if (Match) {
885 SDValue Chain = LD->getChain();
886 SDValue Base = LD->getBasePtr();
887 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 CurDAG->getRegister(0, MVT::i32), Chain };
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000889 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +0000890 MVT::Other, Ops, 6);
Evan Chengaf4550f2009-07-02 01:23:32 +0000891 }
892
893 return NULL;
894}
895
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000896SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
897 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000898 ISD::MemIndexedMode AM = LD->getAddressingMode();
899 if (AM == ISD::UNINDEXED)
900 return NULL;
901
Owen Andersone50ed302009-08-10 22:56:29 +0000902 EVT LoadedVT = LD->getMemoryVT();
Evan Cheng4fbb9962009-07-02 23:16:11 +0000903 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000904 SDValue Offset;
905 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
906 unsigned Opcode = 0;
907 bool Match = false;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000908 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000909 switch (LoadedVT.getSimpleVT().SimpleTy) {
910 case MVT::i32:
Evan Chenge88d5ce2009-07-02 07:28:31 +0000911 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
912 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 case MVT::i16:
Evan Cheng4fbb9962009-07-02 23:16:11 +0000914 if (isSExtLd)
915 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
916 else
917 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000918 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000919 case MVT::i8:
920 case MVT::i1:
Evan Cheng4fbb9962009-07-02 23:16:11 +0000921 if (isSExtLd)
922 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
923 else
924 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
Evan Chenge88d5ce2009-07-02 07:28:31 +0000925 break;
926 default:
927 return NULL;
928 }
929 Match = true;
930 }
931
932 if (Match) {
933 SDValue Chain = LD->getChain();
934 SDValue Base = LD->getBasePtr();
935 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 CurDAG->getRegister(0, MVT::i32), Chain };
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000937 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +0000938 MVT::Other, Ops, 5);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000939 }
940
941 return NULL;
942}
943
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000944/// PairSRegs - Form a D register from a pair of S registers.
945///
946SDNode *ARMDAGToDAGISel::PairSRegs(EVT VT, SDValue V0, SDValue V1) {
947 DebugLoc dl = V0.getNode()->getDebugLoc();
948 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
949 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
Bob Wilson07f6e802010-06-16 21:34:01 +0000950 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
951 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000952}
953
Evan Cheng603afbf2010-05-10 17:34:18 +0000954/// PairDRegs - Form a quad register from a pair of D registers.
955///
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000956SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
957 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000958 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
959 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Bob Wilson07f6e802010-06-16 21:34:01 +0000960 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
961 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
Bob Wilson3bf12ab2009-10-06 22:01:59 +0000962}
963
Evan Cheng7f687192010-05-14 00:21:45 +0000964/// PairQRegs - Form 4 consecutive D registers from a pair of Q registers.
Evan Cheng603afbf2010-05-10 17:34:18 +0000965///
966SDNode *ARMDAGToDAGISel::PairQRegs(EVT VT, SDValue V0, SDValue V1) {
967 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000968 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
969 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
Evan Cheng603afbf2010-05-10 17:34:18 +0000970 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
971 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
972}
973
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000974/// QuadSRegs - Form 4 consecutive S registers.
975///
976SDNode *ARMDAGToDAGISel::QuadSRegs(EVT VT, SDValue V0, SDValue V1,
977 SDValue V2, SDValue V3) {
978 DebugLoc dl = V0.getNode()->getDebugLoc();
979 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
980 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
981 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, MVT::i32);
982 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, MVT::i32);
983 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
984 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
985}
986
Evan Cheng7f687192010-05-14 00:21:45 +0000987/// QuadDRegs - Form 4 consecutive D registers.
Evan Cheng603afbf2010-05-10 17:34:18 +0000988///
989SDNode *ARMDAGToDAGISel::QuadDRegs(EVT VT, SDValue V0, SDValue V1,
990 SDValue V2, SDValue V3) {
991 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000992 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
993 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
994 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
995 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
Evan Cheng603afbf2010-05-10 17:34:18 +0000996 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
997 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
998}
999
Evan Cheng8f6de382010-05-16 03:27:48 +00001000/// QuadQRegs - Form 4 consecutive Q registers.
1001///
1002SDNode *ARMDAGToDAGISel::QuadQRegs(EVT VT, SDValue V0, SDValue V1,
1003 SDValue V2, SDValue V3) {
1004 DebugLoc dl = V0.getNode()->getDebugLoc();
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001005 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1006 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
1007 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, MVT::i32);
1008 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, MVT::i32);
Evan Cheng8f6de382010-05-16 03:27:48 +00001009 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1010 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1011}
1012
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001013SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
Bob Wilson3e36f132009-10-14 17:28:52 +00001014 unsigned *DOpcodes, unsigned *QOpcodes0,
1015 unsigned *QOpcodes1) {
Bob Wilson621f1952010-03-23 05:25:43 +00001016 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
Bob Wilson3e36f132009-10-14 17:28:52 +00001017 DebugLoc dl = N->getDebugLoc();
1018
Bob Wilson226036e2010-03-20 22:13:40 +00001019 SDValue MemAddr, Align;
1020 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
Bob Wilson3e36f132009-10-14 17:28:52 +00001021 return NULL;
1022
1023 SDValue Chain = N->getOperand(0);
1024 EVT VT = N->getValueType(0);
1025 bool is64BitVector = VT.is64BitVector();
1026
1027 unsigned OpcodeIndex;
1028 switch (VT.getSimpleVT().SimpleTy) {
1029 default: llvm_unreachable("unhandled vld type");
1030 // Double-register operations:
1031 case MVT::v8i8: OpcodeIndex = 0; break;
1032 case MVT::v4i16: OpcodeIndex = 1; break;
1033 case MVT::v2f32:
1034 case MVT::v2i32: OpcodeIndex = 2; break;
1035 case MVT::v1i64: OpcodeIndex = 3; break;
1036 // Quad-register operations:
1037 case MVT::v16i8: OpcodeIndex = 0; break;
1038 case MVT::v8i16: OpcodeIndex = 1; break;
1039 case MVT::v4f32:
1040 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson621f1952010-03-23 05:25:43 +00001041 case MVT::v2i64: OpcodeIndex = 3;
Bob Wilson11d98992010-03-23 06:20:33 +00001042 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
Bob Wilson621f1952010-03-23 05:25:43 +00001043 break;
Bob Wilson3e36f132009-10-14 17:28:52 +00001044 }
1045
Bob Wilsonf5721912010-09-03 18:16:02 +00001046 EVT ResTy;
1047 if (NumVecs == 1)
1048 ResTy = VT;
1049 else {
1050 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1051 if (!is64BitVector)
1052 ResTyElts *= 2;
1053 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1054 }
1055
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001056 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001057 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilsonf5721912010-09-03 18:16:02 +00001058 SDValue SuperReg;
Bob Wilson3e36f132009-10-14 17:28:52 +00001059 if (is64BitVector) {
1060 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001061 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilsonf5721912010-09-03 18:16:02 +00001062 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
Bob Wilsonffde0802010-09-02 16:00:54 +00001063 if (NumVecs == 1)
Evan Chenge9e2ba02010-05-10 21:26:24 +00001064 return VLd;
1065
Bob Wilsonf5721912010-09-03 18:16:02 +00001066 SuperReg = SDValue(VLd, 0);
Jakob Stoklund Olesen7bb31e32010-05-24 17:13:28 +00001067 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
Evan Cheng5c6aba22010-05-14 18:54:59 +00001068 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001069 SDValue D = CurDAG->getTargetExtractSubreg(ARM::dsub_0+Vec,
Bob Wilsonffde0802010-09-02 16:00:54 +00001070 dl, VT, SuperReg);
Evan Cheng5c6aba22010-05-14 18:54:59 +00001071 ReplaceUses(SDValue(N, Vec), D);
Evan Chenge9e2ba02010-05-10 21:26:24 +00001072 }
Bob Wilsonf5721912010-09-03 18:16:02 +00001073 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
Evan Chenge9e2ba02010-05-10 21:26:24 +00001074 return NULL;
Bob Wilson3e36f132009-10-14 17:28:52 +00001075 }
1076
Bob Wilson621f1952010-03-23 05:25:43 +00001077 if (NumVecs <= 2) {
1078 // Quad registers are directly supported for VLD1 and VLD2,
1079 // loading pairs of D regs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001080 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilson226036e2010-03-20 22:13:40 +00001081 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
Bob Wilsonffde0802010-09-02 16:00:54 +00001082 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
Bob Wilsonffde0802010-09-02 16:00:54 +00001083 if (NumVecs == 1)
1084 return VLd;
1085
Bob Wilsonf5721912010-09-03 18:16:02 +00001086 SuperReg = SDValue(VLd, 0);
Bob Wilsonffde0802010-09-02 16:00:54 +00001087 Chain = SDValue(VLd, 1);
1088
Bob Wilson3e36f132009-10-14 17:28:52 +00001089 } else {
1090 // Otherwise, quad registers are loaded with two separate instructions,
1091 // where one loads the even registers and the other loads the odd registers.
Bob Wilsonf5721912010-09-03 18:16:02 +00001092 EVT AddrTy = MemAddr.getValueType();
Bob Wilson3e36f132009-10-14 17:28:52 +00001093
Bob Wilson24f995d2009-10-14 18:32:29 +00001094 // Load the even subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001095 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilsonf5721912010-09-03 18:16:02 +00001096 SDValue ImplDef =
1097 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1098 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1099 SDNode *VLdA =
1100 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsA, 7);
1101 Chain = SDValue(VLdA, 2);
Bob Wilson3e36f132009-10-14 17:28:52 +00001102
Bob Wilson24f995d2009-10-14 18:32:29 +00001103 // Load the odd subregs.
Bob Wilson3e36f132009-10-14 17:28:52 +00001104 Opc = QOpcodes1[OpcodeIndex];
Bob Wilsonf5721912010-09-03 18:16:02 +00001105 const SDValue OpsB[] = { SDValue(VLdA, 1), Align, Reg0, SDValue(VLdA, 0),
1106 Pred, Reg0, Chain };
1107 SDNode *VLdB =
1108 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsB, 7);
1109 SuperReg = SDValue(VLdB, 0);
1110 Chain = SDValue(VLdB, 2);
1111 }
Bob Wilson3e36f132009-10-14 17:28:52 +00001112
Bob Wilsonf5721912010-09-03 18:16:02 +00001113 // Extract out the Q registers.
1114 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1115 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1116 SDValue Q = CurDAG->getTargetExtractSubreg(ARM::qsub_0+Vec,
1117 dl, VT, SuperReg);
1118 ReplaceUses(SDValue(N, Vec), Q);
Bob Wilson3e36f132009-10-14 17:28:52 +00001119 }
1120 ReplaceUses(SDValue(N, NumVecs), Chain);
1121 return NULL;
1122}
1123
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001124SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
Bob Wilson24f995d2009-10-14 18:32:29 +00001125 unsigned *DOpcodes, unsigned *QOpcodes0,
1126 unsigned *QOpcodes1) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001127 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
Bob Wilson24f995d2009-10-14 18:32:29 +00001128 DebugLoc dl = N->getDebugLoc();
1129
Bob Wilson226036e2010-03-20 22:13:40 +00001130 SDValue MemAddr, Align;
1131 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
Bob Wilson24f995d2009-10-14 18:32:29 +00001132 return NULL;
1133
1134 SDValue Chain = N->getOperand(0);
1135 EVT VT = N->getOperand(3).getValueType();
1136 bool is64BitVector = VT.is64BitVector();
1137
1138 unsigned OpcodeIndex;
1139 switch (VT.getSimpleVT().SimpleTy) {
1140 default: llvm_unreachable("unhandled vst type");
1141 // Double-register operations:
1142 case MVT::v8i8: OpcodeIndex = 0; break;
1143 case MVT::v4i16: OpcodeIndex = 1; break;
1144 case MVT::v2f32:
1145 case MVT::v2i32: OpcodeIndex = 2; break;
1146 case MVT::v1i64: OpcodeIndex = 3; break;
1147 // Quad-register operations:
1148 case MVT::v16i8: OpcodeIndex = 0; break;
1149 case MVT::v8i16: OpcodeIndex = 1; break;
1150 case MVT::v4f32:
1151 case MVT::v4i32: OpcodeIndex = 2; break;
Bob Wilson11d98992010-03-23 06:20:33 +00001152 case MVT::v2i64: OpcodeIndex = 3;
1153 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1154 break;
Bob Wilson24f995d2009-10-14 18:32:29 +00001155 }
1156
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001157 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001158 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001159
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001160 SmallVector<SDValue, 7> Ops;
Bob Wilson24f995d2009-10-14 18:32:29 +00001161 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001162 Ops.push_back(Align);
Bob Wilson24f995d2009-10-14 18:32:29 +00001163
1164 if (is64BitVector) {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001165 if (NumVecs == 1) {
1166 Ops.push_back(N->getOperand(3));
1167 } else {
Evan Cheng0ce537a2010-05-11 01:19:40 +00001168 SDValue RegSeq;
1169 SDValue V0 = N->getOperand(0+3);
1170 SDValue V1 = N->getOperand(1+3);
1171
1172 // Form a REG_SEQUENCE to force register allocation.
1173 if (NumVecs == 2)
1174 RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1175 else {
1176 SDValue V2 = N->getOperand(2+3);
1177 // If it's a vld3, form a quad D-register and leave the last part as
1178 // an undef.
1179 SDValue V3 = (NumVecs == 3)
1180 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1181 : N->getOperand(3+3);
1182 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1183 }
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001184 Ops.push_back(RegSeq);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001185 }
Evan Chengac0869d2009-11-21 06:21:52 +00001186 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001187 Ops.push_back(Reg0); // predicate register
Bob Wilson24f995d2009-10-14 18:32:29 +00001188 Ops.push_back(Chain);
Evan Cheng0ce537a2010-05-11 01:19:40 +00001189 unsigned Opc = DOpcodes[OpcodeIndex];
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001190 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
Bob Wilson24f995d2009-10-14 18:32:29 +00001191 }
1192
Bob Wilson11d98992010-03-23 06:20:33 +00001193 if (NumVecs <= 2) {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001194 // Quad registers are directly supported for VST1 and VST2.
Bob Wilson24f995d2009-10-14 18:32:29 +00001195 unsigned Opc = QOpcodes0[OpcodeIndex];
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001196 if (NumVecs == 1) {
1197 Ops.push_back(N->getOperand(3));
1198 } else {
1199 // Form a QQ register.
Evan Cheng603afbf2010-05-10 17:34:18 +00001200 SDValue Q0 = N->getOperand(3);
1201 SDValue Q1 = N->getOperand(4);
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001202 Ops.push_back(SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0));
Bob Wilson24f995d2009-10-14 18:32:29 +00001203 }
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001204 Ops.push_back(Pred);
1205 Ops.push_back(Reg0); // predicate register
1206 Ops.push_back(Chain);
1207 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
Bob Wilson24f995d2009-10-14 18:32:29 +00001208 }
1209
1210 // Otherwise, quad registers are stored with two separate instructions,
1211 // where one stores the even registers and the other stores the odd registers.
Evan Cheng7189fd02010-05-15 07:53:37 +00001212
Bob Wilson07f6e802010-06-16 21:34:01 +00001213 // Form the QQQQ REG_SEQUENCE.
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001214 SDValue V0 = N->getOperand(0+3);
1215 SDValue V1 = N->getOperand(1+3);
1216 SDValue V2 = N->getOperand(2+3);
1217 SDValue V3 = (NumVecs == 3)
1218 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1219 : N->getOperand(3+3);
1220 SDValue RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilson07f6e802010-06-16 21:34:01 +00001221
1222 // Store the even D registers.
Bob Wilson07f6e802010-06-16 21:34:01 +00001223 Ops.push_back(Reg0); // post-access address offset
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001224 Ops.push_back(RegSeq);
Bob Wilson07f6e802010-06-16 21:34:01 +00001225 Ops.push_back(Pred);
1226 Ops.push_back(Reg0); // predicate register
1227 Ops.push_back(Chain);
1228 unsigned Opc = QOpcodes0[OpcodeIndex];
1229 SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001230 MVT::Other, Ops.data(), 7);
Bob Wilson07f6e802010-06-16 21:34:01 +00001231 Chain = SDValue(VStA, 1);
1232
1233 // Store the odd D registers.
1234 Ops[0] = SDValue(VStA, 0); // MemAddr
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001235 Ops[6] = Chain;
Bob Wilson07f6e802010-06-16 21:34:01 +00001236 Opc = QOpcodes1[OpcodeIndex];
1237 SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
Bob Wilsone5ce4f62010-08-28 05:12:57 +00001238 MVT::Other, Ops.data(), 7);
Bob Wilson07f6e802010-06-16 21:34:01 +00001239 Chain = SDValue(VStB, 1);
1240 ReplaceUses(SDValue(N, 0), Chain);
1241 return NULL;
Bob Wilson24f995d2009-10-14 18:32:29 +00001242}
1243
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001244SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
Bob Wilson96493442009-10-14 16:46:45 +00001245 unsigned NumVecs, unsigned *DOpcodes,
Bob Wilson8466fa12010-09-13 23:01:35 +00001246 unsigned *QOpcodes) {
Bob Wilson96493442009-10-14 16:46:45 +00001247 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001248 DebugLoc dl = N->getDebugLoc();
1249
Bob Wilson226036e2010-03-20 22:13:40 +00001250 SDValue MemAddr, Align;
1251 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
Bob Wilsona7c397c2009-10-14 16:19:03 +00001252 return NULL;
1253
1254 SDValue Chain = N->getOperand(0);
1255 unsigned Lane =
1256 cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
Bob Wilson96493442009-10-14 16:46:45 +00001257 EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
Bob Wilsona7c397c2009-10-14 16:19:03 +00001258 bool is64BitVector = VT.is64BitVector();
1259
Bob Wilsona7c397c2009-10-14 16:19:03 +00001260 unsigned OpcodeIndex;
1261 switch (VT.getSimpleVT().SimpleTy) {
Bob Wilson96493442009-10-14 16:46:45 +00001262 default: llvm_unreachable("unhandled vld/vst lane type");
Bob Wilsona7c397c2009-10-14 16:19:03 +00001263 // Double-register operations:
1264 case MVT::v8i8: OpcodeIndex = 0; break;
1265 case MVT::v4i16: OpcodeIndex = 1; break;
1266 case MVT::v2f32:
1267 case MVT::v2i32: OpcodeIndex = 2; break;
1268 // Quad-register operations:
1269 case MVT::v8i16: OpcodeIndex = 0; break;
1270 case MVT::v4f32:
1271 case MVT::v4i32: OpcodeIndex = 1; break;
1272 }
1273
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001274 SDValue Pred = getAL(CurDAG);
Bob Wilson226036e2010-03-20 22:13:40 +00001275 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengac0869d2009-11-21 06:21:52 +00001276
Bob Wilson8466fa12010-09-13 23:01:35 +00001277 SmallVector<SDValue, 7> Ops;
Bob Wilsona7c397c2009-10-14 16:19:03 +00001278 Ops.push_back(MemAddr);
Jim Grosbach8a5ec862009-11-07 21:25:39 +00001279 Ops.push_back(Align);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001280
Bob Wilson8466fa12010-09-13 23:01:35 +00001281 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1282 Opc = QOpcodes[OpcodeIndex]);
Bob Wilson07f6e802010-06-16 21:34:01 +00001283
Bob Wilson8466fa12010-09-13 23:01:35 +00001284 SDValue SuperReg;
1285 SDValue V0 = N->getOperand(0+3);
1286 SDValue V1 = N->getOperand(1+3);
1287 if (NumVecs == 2) {
1288 if (is64BitVector)
1289 SuperReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1290 else
1291 SuperReg = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001292 } else {
Bob Wilson8466fa12010-09-13 23:01:35 +00001293 SDValue V2 = N->getOperand(2+3);
1294 SDValue V3 = (NumVecs == 3)
1295 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1296 : N->getOperand(3+3);
1297 if (is64BitVector)
1298 SuperReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1299 else
1300 SuperReg = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001301 }
Bob Wilson8466fa12010-09-13 23:01:35 +00001302 Ops.push_back(SuperReg);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001303 Ops.push_back(getI32Imm(Lane));
Evan Chengac0869d2009-11-21 06:21:52 +00001304 Ops.push_back(Pred);
Bob Wilson226036e2010-03-20 22:13:40 +00001305 Ops.push_back(Reg0);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001306 Ops.push_back(Chain);
1307
Bob Wilson96493442009-10-14 16:46:45 +00001308 if (!IsLoad)
Bob Wilson8466fa12010-09-13 23:01:35 +00001309 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 7);
Bob Wilson96493442009-10-14 16:46:45 +00001310
Bob Wilson8466fa12010-09-13 23:01:35 +00001311 EVT ResTy;
1312 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1313 if (!is64BitVector)
1314 ResTyElts *= 2;
1315 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001316
Bob Wilson8466fa12010-09-13 23:01:35 +00001317 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other,
1318 Ops.data(), 7);
1319 SuperReg = SDValue(VLdLn, 0);
1320 Chain = SDValue(VLdLn, 1);
Evan Cheng7092c2b2010-05-15 01:36:29 +00001321
Bob Wilson8466fa12010-09-13 23:01:35 +00001322 // Extract the subregisters.
Bob Wilson07f6e802010-06-16 21:34:01 +00001323 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1324 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1325 unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1326 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1327 ReplaceUses(SDValue(N, Vec),
Bob Wilson8466fa12010-09-13 23:01:35 +00001328 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1329 ReplaceUses(SDValue(N, NumVecs), Chain);
Bob Wilsona7c397c2009-10-14 16:19:03 +00001330 return NULL;
1331}
1332
Bob Wilson78dfbc32010-07-07 00:08:54 +00001333SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1334 unsigned Opc) {
Bob Wilsond491d6e2010-07-06 23:36:25 +00001335 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1336 DebugLoc dl = N->getDebugLoc();
1337 EVT VT = N->getValueType(0);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001338 unsigned FirstTblReg = IsExt ? 2 : 1;
Bob Wilsond491d6e2010-07-06 23:36:25 +00001339
1340 // Form a REG_SEQUENCE to force register allocation.
1341 SDValue RegSeq;
Bob Wilson78dfbc32010-07-07 00:08:54 +00001342 SDValue V0 = N->getOperand(FirstTblReg + 0);
1343 SDValue V1 = N->getOperand(FirstTblReg + 1);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001344 if (NumVecs == 2)
1345 RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1346 else {
Bob Wilson78dfbc32010-07-07 00:08:54 +00001347 SDValue V2 = N->getOperand(FirstTblReg + 2);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001348 // If it's a vtbl3, form a quad D-register and leave the last part as
1349 // an undef.
1350 SDValue V3 = (NumVecs == 3)
1351 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
Bob Wilson78dfbc32010-07-07 00:08:54 +00001352 : N->getOperand(FirstTblReg + 3);
Bob Wilsond491d6e2010-07-06 23:36:25 +00001353 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1354 }
1355
Bob Wilson78dfbc32010-07-07 00:08:54 +00001356 SmallVector<SDValue, 6> Ops;
1357 if (IsExt)
1358 Ops.push_back(N->getOperand(1));
Bob Wilsonbd916c52010-09-13 23:55:10 +00001359 Ops.push_back(RegSeq);
Bob Wilson78dfbc32010-07-07 00:08:54 +00001360 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
Bob Wilsond491d6e2010-07-06 23:36:25 +00001361 Ops.push_back(getAL(CurDAG)); // predicate
1362 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
Bob Wilson78dfbc32010-07-07 00:08:54 +00001363 return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
Bob Wilsond491d6e2010-07-06 23:36:25 +00001364}
1365
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001366SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001367 bool isSigned) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001368 if (!Subtarget->hasV6T2Ops())
1369 return NULL;
Bob Wilson96493442009-10-14 16:46:45 +00001370
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001371 unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1372 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1373
1374
1375 // For unsigned extracts, check for a shift right and mask
1376 unsigned And_imm = 0;
1377 if (N->getOpcode() == ISD::AND) {
1378 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1379
1380 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1381 if (And_imm & (And_imm + 1))
1382 return NULL;
1383
1384 unsigned Srl_imm = 0;
1385 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1386 Srl_imm)) {
1387 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1388
1389 unsigned Width = CountTrailingOnes_32(And_imm);
1390 unsigned LSB = Srl_imm;
1391 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1392 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1393 CurDAG->getTargetConstant(LSB, MVT::i32),
1394 CurDAG->getTargetConstant(Width, MVT::i32),
1395 getAL(CurDAG), Reg0 };
1396 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1397 }
1398 }
1399 return NULL;
1400 }
1401
1402 // Otherwise, we're looking for a shift of a shift
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001403 unsigned Shl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001404 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001405 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1406 unsigned Srl_imm = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001407 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001408 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1409 unsigned Width = 32 - Srl_imm;
1410 int LSB = Srl_imm - Shl_imm;
Evan Cheng8000c6c2009-10-22 00:40:00 +00001411 if (LSB < 0)
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001412 return NULL;
1413 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001414 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001415 CurDAG->getTargetConstant(LSB, MVT::i32),
1416 CurDAG->getTargetConstant(Width, MVT::i32),
1417 getAL(CurDAG), Reg0 };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001418 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001419 }
1420 }
1421 return NULL;
1422}
1423
Evan Cheng9ef48352009-11-20 00:54:03 +00001424SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001425SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001426 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1427 SDValue CPTmp0;
1428 SDValue CPTmp1;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001429 if (SelectT2ShifterOperandReg(N, TrueVal, CPTmp0, CPTmp1)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001430 unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1431 unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1432 unsigned Opc = 0;
1433 switch (SOShOp) {
1434 case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1435 case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1436 case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1437 case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1438 default:
1439 llvm_unreachable("Unknown so_reg opcode!");
1440 break;
1441 }
1442 SDValue SOShImm =
1443 CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1444 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1445 SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001446 return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
Evan Cheng9ef48352009-11-20 00:54:03 +00001447 }
1448 return 0;
1449}
1450
1451SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001452SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001453 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1454 SDValue CPTmp0;
1455 SDValue CPTmp1;
1456 SDValue CPTmp2;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001457 if (SelectShifterOperandReg(N, TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001458 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1459 SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001460 return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
Evan Cheng9ef48352009-11-20 00:54:03 +00001461 }
1462 return 0;
1463}
1464
1465SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001466SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001467 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1468 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1469 if (!T)
1470 return 0;
1471
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001472 if (Pred_t2_so_imm(TrueVal.getNode())) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001473 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1474 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1475 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001476 return CurDAG->SelectNodeTo(N,
Evan Cheng9ef48352009-11-20 00:54:03 +00001477 ARM::t2MOVCCi, MVT::i32, Ops, 5);
1478 }
1479 return 0;
1480}
1481
1482SDNode *ARMDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001483SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001484 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1485 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1486 if (!T)
1487 return 0;
1488
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001489 if (Pred_so_imm(TrueVal.getNode())) {
Evan Cheng9ef48352009-11-20 00:54:03 +00001490 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1491 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1492 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001493 return CurDAG->SelectNodeTo(N,
Evan Cheng9ef48352009-11-20 00:54:03 +00001494 ARM::MOVCCi, MVT::i32, Ops, 5);
1495 }
1496 return 0;
1497}
1498
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001499SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
1500 EVT VT = N->getValueType(0);
1501 SDValue FalseVal = N->getOperand(0);
1502 SDValue TrueVal = N->getOperand(1);
1503 SDValue CC = N->getOperand(2);
1504 SDValue CCR = N->getOperand(3);
1505 SDValue InFlag = N->getOperand(4);
Evan Cheng9ef48352009-11-20 00:54:03 +00001506 assert(CC.getOpcode() == ISD::Constant);
1507 assert(CCR.getOpcode() == ISD::Register);
1508 ARMCC::CondCodes CCVal =
1509 (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
Evan Cheng07ba9062009-11-19 21:45:22 +00001510
1511 if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1512 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1513 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1514 // Pattern complexity = 18 cost = 1 size = 0
1515 SDValue CPTmp0;
1516 SDValue CPTmp1;
1517 SDValue CPTmp2;
1518 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001519 SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001520 CCVal, CCR, InFlag);
1521 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001522 Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001523 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1524 if (Res)
1525 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001526 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001527 SDNode *Res = SelectARMCMOVShiftOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001528 CCVal, CCR, InFlag);
1529 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001530 Res = SelectARMCMOVShiftOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001531 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1532 if (Res)
1533 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001534 }
1535
1536 // Pattern: (ARMcmov:i32 GPR:i32:$false,
Jakob Stoklund Olesen00d3dda2010-08-17 20:39:04 +00001537 // (imm:i32)<<P:Pred_so_imm>>:$true,
Evan Cheng07ba9062009-11-19 21:45:22 +00001538 // (imm:i32):$cc)
1539 // Emits: (MOVCCi:i32 GPR:i32:$false,
1540 // (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1541 // Pattern complexity = 10 cost = 1 size = 0
Evan Cheng9ef48352009-11-20 00:54:03 +00001542 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001543 SDNode *Res = SelectT2CMOVSoImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001544 CCVal, CCR, InFlag);
1545 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001546 Res = SelectT2CMOVSoImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001547 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1548 if (Res)
1549 return Res;
1550 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001551 SDNode *Res = SelectARMCMOVSoImmOp(N, FalseVal, TrueVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001552 CCVal, CCR, InFlag);
1553 if (!Res)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001554 Res = SelectARMCMOVSoImmOp(N, TrueVal, FalseVal,
Evan Cheng9ef48352009-11-20 00:54:03 +00001555 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1556 if (Res)
1557 return Res;
Evan Cheng07ba9062009-11-19 21:45:22 +00001558 }
1559 }
1560
1561 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1562 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1563 // Pattern complexity = 6 cost = 1 size = 0
1564 //
1565 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1566 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1567 // Pattern complexity = 6 cost = 11 size = 0
1568 //
1569 // Also FCPYScc and FCPYDcc.
Evan Cheng9ef48352009-11-20 00:54:03 +00001570 SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1571 SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
Evan Cheng07ba9062009-11-19 21:45:22 +00001572 unsigned Opc = 0;
1573 switch (VT.getSimpleVT().SimpleTy) {
1574 default: assert(false && "Illegal conditional move type!");
1575 break;
1576 case MVT::i32:
1577 Opc = Subtarget->isThumb()
1578 ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1579 : ARM::MOVCCr;
1580 break;
1581 case MVT::f32:
1582 Opc = ARM::VMOVScc;
1583 break;
1584 case MVT::f64:
1585 Opc = ARM::VMOVDcc;
1586 break;
1587 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001588 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Cheng07ba9062009-11-19 21:45:22 +00001589}
1590
Evan Chengde8aa4e2010-05-05 18:28:36 +00001591SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
1592 // The only time a CONCAT_VECTORS operation can have legal types is when
1593 // two 64-bit vectors are concatenated to a 128-bit vector.
1594 EVT VT = N->getValueType(0);
1595 if (!VT.is128BitVector() || N->getNumOperands() != 2)
1596 llvm_unreachable("unexpected CONCAT_VECTORS");
1597 DebugLoc dl = N->getDebugLoc();
1598 SDValue V0 = N->getOperand(0);
1599 SDValue V1 = N->getOperand(1);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +00001600 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1601 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
Evan Chengde8aa4e2010-05-05 18:28:36 +00001602 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1603 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1604}
1605
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001606SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
Dale Johannesened2eee62009-02-06 01:31:28 +00001607 DebugLoc dl = N->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001608
Dan Gohmane8be6c62008-07-17 19:10:17 +00001609 if (N->isMachineOpcode())
Evan Chenga8e29892007-01-19 07:51:42 +00001610 return NULL; // Already selected.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001611
1612 switch (N->getOpcode()) {
Evan Chenga8e29892007-01-19 07:51:42 +00001613 default: break;
1614 case ISD::Constant: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001615 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001616 bool UseCP = true;
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00001617 if (Subtarget->hasThumb2())
1618 // Thumb2-aware targets have the MOVT instruction, so all immediates can
1619 // be done with MOV + MOVT, at worst.
1620 UseCP = 0;
1621 else {
1622 if (Subtarget->isThumb()) {
Bob Wilsone64e3cf2009-06-22 17:29:13 +00001623 UseCP = (Val > 255 && // MOV
1624 ~Val > 255 && // MOV + MVN
1625 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
Anton Korobeynikov6a2fa322009-09-27 23:52:58 +00001626 } else
1627 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
1628 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
1629 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
1630 }
1631
Evan Chenga8e29892007-01-19 07:51:42 +00001632 if (UseCP) {
Dan Gohman475871a2008-07-27 21:46:04 +00001633 SDValue CPIdx =
Owen Anderson1d0be152009-08-13 21:58:54 +00001634 CurDAG->getTargetConstantPool(ConstantInt::get(
1635 Type::getInt32Ty(*CurDAG->getContext()), Val),
Evan Chenga8e29892007-01-19 07:51:42 +00001636 TLI.getPointerTy());
Evan Cheng012f2d92007-01-24 08:53:17 +00001637
1638 SDNode *ResNode;
Evan Cheng446c4282009-07-11 06:43:01 +00001639 if (Subtarget->isThumb1Only()) {
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001640 SDValue Pred = getAL(CurDAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00001641 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
Evan Cheng446c4282009-07-11 06:43:01 +00001642 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
Dan Gohman602b0c82009-09-25 18:54:59 +00001643 ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
1644 Ops, 4);
Evan Cheng446c4282009-07-11 06:43:01 +00001645 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001646 SDValue Ops[] = {
Jim Grosbach764ab522009-08-11 15:33:49 +00001647 CPIdx,
Owen Anderson825b72b2009-08-11 20:47:22 +00001648 CurDAG->getRegister(0, MVT::i32),
1649 CurDAG->getTargetConstant(0, MVT::i32),
Evan Chengee568cf2007-07-05 07:15:27 +00001650 getAL(CurDAG),
Owen Anderson825b72b2009-08-11 20:47:22 +00001651 CurDAG->getRegister(0, MVT::i32),
Evan Cheng012f2d92007-01-24 08:53:17 +00001652 CurDAG->getEntryNode()
1653 };
Dan Gohman602b0c82009-09-25 18:54:59 +00001654 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
1655 Ops, 6);
Evan Cheng012f2d92007-01-24 08:53:17 +00001656 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001657 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
Evan Chenga8e29892007-01-19 07:51:42 +00001658 return NULL;
1659 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001660
Evan Chenga8e29892007-01-19 07:51:42 +00001661 // Other cases are autogenerated.
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001662 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001663 }
Rafael Espindolaf819a492006-11-09 13:58:55 +00001664 case ISD::FrameIndex: {
Evan Chenga8e29892007-01-19 07:51:42 +00001665 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindolaf819a492006-11-09 13:58:55 +00001666 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohman475871a2008-07-27 21:46:04 +00001667 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
David Goodwinf1daf7d2009-07-08 23:10:31 +00001668 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001669 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
1670 CurDAG->getTargetConstant(0, MVT::i32));
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001671 } else {
David Goodwin419c6152009-07-14 18:48:51 +00001672 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
1673 ARM::t2ADDri : ARM::ADDri);
Owen Anderson825b72b2009-08-11 20:47:22 +00001674 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
1675 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1676 CurDAG->getRegister(0, MVT::i32) };
1677 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00001678 }
Evan Chenga8e29892007-01-19 07:51:42 +00001679 }
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001680 case ISD::SRL:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001681 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001682 return I;
1683 break;
1684 case ISD::SRA:
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001685 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
Sandeep Patel47eedaa2009-10-13 18:59:48 +00001686 return I;
1687 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001688 case ISD::MUL:
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001689 if (Subtarget->isThumb1Only())
Evan Cheng79d43262007-01-24 02:21:22 +00001690 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001691 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001692 unsigned RHSV = C->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001693 if (!RHSV) break;
1694 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00001695 unsigned ShImm = Log2_32(RHSV-1);
1696 if (ShImm >= 32)
1697 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001698 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001699 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00001700 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1701 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00001702 if (Subtarget->isThumb()) {
Evan Chengaf9e7a72009-07-21 00:31:12 +00001703 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001704 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001705 } else {
1706 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001707 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001708 }
Evan Chenga8e29892007-01-19 07:51:42 +00001709 }
1710 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
Evan Chengaf9e7a72009-07-21 00:31:12 +00001711 unsigned ShImm = Log2_32(RHSV+1);
1712 if (ShImm >= 32)
1713 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001714 SDValue V = N->getOperand(0);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001715 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Owen Anderson825b72b2009-08-11 20:47:22 +00001716 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1717 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng78dd9db2009-07-22 18:08:05 +00001718 if (Subtarget->isThumb()) {
Bob Wilson13ef8402010-05-28 00:27:15 +00001719 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1720 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001721 } else {
1722 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
Owen Anderson825b72b2009-08-11 20:47:22 +00001723 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
Evan Chengaf9e7a72009-07-21 00:31:12 +00001724 }
Evan Chenga8e29892007-01-19 07:51:42 +00001725 }
1726 }
1727 break;
Evan Cheng20956592009-10-21 08:15:52 +00001728 case ISD::AND: {
Jim Grosbach3a1287b2010-04-22 23:24:18 +00001729 // Check for unsigned bitfield extract
1730 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
1731 return I;
1732
Evan Cheng20956592009-10-21 08:15:52 +00001733 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
1734 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
1735 // are entirely contributed by c2 and lower 16-bits are entirely contributed
1736 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
1737 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001738 EVT VT = N->getValueType(0);
Evan Cheng20956592009-10-21 08:15:52 +00001739 if (VT != MVT::i32)
1740 break;
1741 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
1742 ? ARM::t2MOVTi16
1743 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
1744 if (!Opc)
1745 break;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001746 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Evan Cheng20956592009-10-21 08:15:52 +00001747 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1748 if (!N1C)
1749 break;
1750 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
1751 SDValue N2 = N0.getOperand(1);
1752 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1753 if (!N2C)
1754 break;
1755 unsigned N1CVal = N1C->getZExtValue();
1756 unsigned N2CVal = N2C->getZExtValue();
1757 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
1758 (N1CVal & 0xffffU) == 0xffffU &&
1759 (N2CVal & 0xffffU) == 0x0U) {
1760 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
1761 MVT::i32);
1762 SDValue Ops[] = { N0.getOperand(0), Imm16,
1763 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1764 return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
1765 }
1766 }
1767 break;
1768 }
Jim Grosbache5165492009-11-09 00:11:35 +00001769 case ARMISD::VMOVRRD:
1770 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001771 N->getOperand(0), getAL(CurDAG),
Dan Gohman602b0c82009-09-25 18:54:59 +00001772 CurDAG->getRegister(0, MVT::i32));
Dan Gohman525178c2007-10-08 18:33:35 +00001773 case ISD::UMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001774 if (Subtarget->isThumb1Only())
1775 break;
1776 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001777 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001778 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1779 CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00001780 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001781 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001782 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001783 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1784 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00001785 return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001786 }
Evan Chengee568cf2007-07-05 07:15:27 +00001787 }
Dan Gohman525178c2007-10-08 18:33:35 +00001788 case ISD::SMUL_LOHI: {
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001789 if (Subtarget->isThumb1Only())
1790 break;
1791 if (Subtarget->isThumb()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001792 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001793 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
Jim Grosbach18f30e62010-06-02 21:53:11 +00001794 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001795 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001796 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00001797 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1798 CurDAG->getRegister(0, MVT::i32) };
Dan Gohman602b0c82009-09-25 18:54:59 +00001799 return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001800 }
Evan Chengee568cf2007-07-05 07:15:27 +00001801 }
Evan Chenga8e29892007-01-19 07:51:42 +00001802 case ISD::LOAD: {
Evan Chenge88d5ce2009-07-02 07:28:31 +00001803 SDNode *ResNode = 0;
Evan Cheng5b9fcd12009-07-07 01:17:28 +00001804 if (Subtarget->isThumb() && Subtarget->hasThumb2())
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001805 ResNode = SelectT2IndexedLoad(N);
Evan Chenge88d5ce2009-07-02 07:28:31 +00001806 else
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001807 ResNode = SelectARMIndexedLoad(N);
Evan Chengaf4550f2009-07-02 01:23:32 +00001808 if (ResNode)
1809 return ResNode;
Evan Chenga8e29892007-01-19 07:51:42 +00001810 // Other cases are autogenerated.
Rafael Espindolaf819a492006-11-09 13:58:55 +00001811 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001812 }
Evan Chengee568cf2007-07-05 07:15:27 +00001813 case ARMISD::BRCOND: {
1814 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1815 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1816 // Pattern complexity = 6 cost = 1 size = 0
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001817
Evan Chengee568cf2007-07-05 07:15:27 +00001818 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1819 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
1820 // Pattern complexity = 6 cost = 1 size = 0
1821
David Goodwin5e47a9a2009-06-30 18:04:13 +00001822 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1823 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1824 // Pattern complexity = 6 cost = 1 size = 0
1825
Jim Grosbach764ab522009-08-11 15:33:49 +00001826 unsigned Opc = Subtarget->isThumb() ?
David Goodwin5e47a9a2009-06-30 18:04:13 +00001827 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001828 SDValue Chain = N->getOperand(0);
1829 SDValue N1 = N->getOperand(1);
1830 SDValue N2 = N->getOperand(2);
1831 SDValue N3 = N->getOperand(3);
1832 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00001833 assert(N1.getOpcode() == ISD::BasicBlock);
1834 assert(N2.getOpcode() == ISD::Constant);
1835 assert(N3.getOpcode() == ISD::Register);
1836
Dan Gohman475871a2008-07-27 21:46:04 +00001837 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001838 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00001839 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001840 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
Dan Gohman602b0c82009-09-25 18:54:59 +00001841 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
1842 MVT::Flag, Ops, 5);
Dan Gohman475871a2008-07-27 21:46:04 +00001843 Chain = SDValue(ResNode, 0);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001844 if (N->getNumValues() == 2) {
Dan Gohman475871a2008-07-27 21:46:04 +00001845 InFlag = SDValue(ResNode, 1);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001846 ReplaceUses(SDValue(N, 1), InFlag);
Chris Lattnera47b9bc2008-02-03 03:20:59 +00001847 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001848 ReplaceUses(SDValue(N, 0),
Evan Chenged54de42009-11-19 08:16:50 +00001849 SDValue(Chain.getNode(), Chain.getResNo()));
Evan Chengee568cf2007-07-05 07:15:27 +00001850 return NULL;
1851 }
Evan Cheng07ba9062009-11-19 21:45:22 +00001852 case ARMISD::CMOV:
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001853 return SelectCMOVOp(N);
Evan Chengee568cf2007-07-05 07:15:27 +00001854 case ARMISD::CNEG: {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001855 EVT VT = N->getValueType(0);
1856 SDValue N0 = N->getOperand(0);
1857 SDValue N1 = N->getOperand(1);
1858 SDValue N2 = N->getOperand(2);
1859 SDValue N3 = N->getOperand(3);
1860 SDValue InFlag = N->getOperand(4);
Evan Chengee568cf2007-07-05 07:15:27 +00001861 assert(N2.getOpcode() == ISD::Constant);
1862 assert(N3.getOpcode() == ISD::Register);
1863
Dan Gohman475871a2008-07-27 21:46:04 +00001864 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001865 cast<ConstantSDNode>(N2)->getZExtValue()),
Owen Anderson825b72b2009-08-11 20:47:22 +00001866 MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001867 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
Evan Chengee568cf2007-07-05 07:15:27 +00001868 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001869 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengee568cf2007-07-05 07:15:27 +00001870 default: assert(false && "Illegal conditional move type!");
1871 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001872 case MVT::f32:
Jim Grosbache5165492009-11-09 00:11:35 +00001873 Opc = ARM::VNEGScc;
Evan Chengee568cf2007-07-05 07:15:27 +00001874 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001875 case MVT::f64:
Jim Grosbache5165492009-11-09 00:11:35 +00001876 Opc = ARM::VNEGDcc;
Evan Chenge5ad88e2008-12-10 21:54:21 +00001877 break;
Evan Chengee568cf2007-07-05 07:15:27 +00001878 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001879 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
Evan Chengee568cf2007-07-05 07:15:27 +00001880 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00001881
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001882 case ARMISD::VZIP: {
1883 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001884 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001885 switch (VT.getSimpleVT().SimpleTy) {
1886 default: return NULL;
1887 case MVT::v8i8: Opc = ARM::VZIPd8; break;
1888 case MVT::v4i16: Opc = ARM::VZIPd16; break;
1889 case MVT::v2f32:
1890 case MVT::v2i32: Opc = ARM::VZIPd32; break;
1891 case MVT::v16i8: Opc = ARM::VZIPq8; break;
1892 case MVT::v8i16: Opc = ARM::VZIPq16; break;
1893 case MVT::v4f32:
1894 case MVT::v4i32: Opc = ARM::VZIPq32; break;
1895 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001896 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001897 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1898 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1899 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001900 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001901 case ARMISD::VUZP: {
1902 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001903 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001904 switch (VT.getSimpleVT().SimpleTy) {
1905 default: return NULL;
1906 case MVT::v8i8: Opc = ARM::VUZPd8; break;
1907 case MVT::v4i16: Opc = ARM::VUZPd16; break;
1908 case MVT::v2f32:
1909 case MVT::v2i32: Opc = ARM::VUZPd32; break;
1910 case MVT::v16i8: Opc = ARM::VUZPq8; break;
1911 case MVT::v8i16: Opc = ARM::VUZPq16; break;
1912 case MVT::v4f32:
1913 case MVT::v4i32: Opc = ARM::VUZPq32; break;
1914 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001915 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001916 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1917 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1918 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001919 }
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001920 case ARMISD::VTRN: {
1921 unsigned Opc = 0;
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001922 EVT VT = N->getValueType(0);
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001923 switch (VT.getSimpleVT().SimpleTy) {
1924 default: return NULL;
1925 case MVT::v8i8: Opc = ARM::VTRNd8; break;
1926 case MVT::v4i16: Opc = ARM::VTRNd16; break;
1927 case MVT::v2f32:
1928 case MVT::v2i32: Opc = ARM::VTRNd32; break;
1929 case MVT::v16i8: Opc = ARM::VTRNq8; break;
1930 case MVT::v8i16: Opc = ARM::VTRNq16; break;
1931 case MVT::v4f32:
1932 case MVT::v4i32: Opc = ARM::VTRNq32; break;
1933 }
Evan Cheng47b7b9f2010-04-16 05:46:06 +00001934 SDValue Pred = getAL(CurDAG);
Evan Chengac0869d2009-11-21 06:21:52 +00001935 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1936 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1937 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
Anton Korobeynikov62e84f12009-08-21 12:40:50 +00001938 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001939 case ARMISD::BUILD_VECTOR: {
1940 EVT VecVT = N->getValueType(0);
1941 EVT EltVT = VecVT.getVectorElementType();
1942 unsigned NumElts = VecVT.getVectorNumElements();
1943 if (EltVT.getSimpleVT() == MVT::f64) {
1944 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
1945 return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
1946 }
1947 assert(EltVT.getSimpleVT() == MVT::f32 &&
1948 "unexpected type for BUILD_VECTOR");
1949 if (NumElts == 2)
1950 return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
1951 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
1952 return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
1953 N->getOperand(2), N->getOperand(3));
1954 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00001955
1956 case ISD::INTRINSIC_VOID:
1957 case ISD::INTRINSIC_W_CHAIN: {
1958 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Bob Wilson31fb12f2009-08-26 17:39:53 +00001959 switch (IntNo) {
1960 default:
Bob Wilson429009b2010-05-06 16:05:26 +00001961 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00001962
Bob Wilson621f1952010-03-23 05:25:43 +00001963 case Intrinsic::arm_neon_vld1: {
1964 unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
1965 ARM::VLD1d32, ARM::VLD1d64 };
Bob Wilsonffde0802010-09-02 16:00:54 +00001966 unsigned QOpcodes[] = { ARM::VLD1q8Pseudo, ARM::VLD1q16Pseudo,
1967 ARM::VLD1q32Pseudo, ARM::VLD1q64Pseudo };
Bob Wilson621f1952010-03-23 05:25:43 +00001968 return SelectVLD(N, 1, DOpcodes, QOpcodes, 0);
1969 }
1970
Bob Wilson31fb12f2009-08-26 17:39:53 +00001971 case Intrinsic::arm_neon_vld2: {
Bob Wilsonffde0802010-09-02 16:00:54 +00001972 unsigned DOpcodes[] = { ARM::VLD2d8Pseudo, ARM::VLD2d16Pseudo,
1973 ARM::VLD2d32Pseudo, ARM::VLD1q64Pseudo };
1974 unsigned QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
1975 ARM::VLD2q32Pseudo };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001976 return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00001977 }
1978
1979 case Intrinsic::arm_neon_vld3: {
Bob Wilsonf5721912010-09-03 18:16:02 +00001980 unsigned DOpcodes[] = { ARM::VLD3d8Pseudo, ARM::VLD3d16Pseudo,
1981 ARM::VLD3d32Pseudo, ARM::VLD1d64TPseudo };
1982 unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
1983 ARM::VLD3q16Pseudo_UPD,
1984 ARM::VLD3q32Pseudo_UPD };
1985 unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
1986 ARM::VLD3q16oddPseudo_UPD,
1987 ARM::VLD3q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001988 return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00001989 }
1990
1991 case Intrinsic::arm_neon_vld4: {
Bob Wilsonf5721912010-09-03 18:16:02 +00001992 unsigned DOpcodes[] = { ARM::VLD4d8Pseudo, ARM::VLD4d16Pseudo,
1993 ARM::VLD4d32Pseudo, ARM::VLD1d64QPseudo };
1994 unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
1995 ARM::VLD4q16Pseudo_UPD,
1996 ARM::VLD4q32Pseudo_UPD };
1997 unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
1998 ARM::VLD4q16oddPseudo_UPD,
1999 ARM::VLD4q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002000 return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002001 }
2002
Bob Wilson243fcc52009-09-01 04:26:28 +00002003 case Intrinsic::arm_neon_vld2lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002004 unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo, ARM::VLD2LNd16Pseudo,
2005 ARM::VLD2LNd32Pseudo };
2006 unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo, ARM::VLD2LNq32Pseudo };
2007 return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002008 }
2009
2010 case Intrinsic::arm_neon_vld3lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002011 unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo, ARM::VLD3LNd16Pseudo,
2012 ARM::VLD3LNd32Pseudo };
2013 unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo, ARM::VLD3LNq32Pseudo };
2014 return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002015 }
2016
2017 case Intrinsic::arm_neon_vld4lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002018 unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo, ARM::VLD4LNd16Pseudo,
2019 ARM::VLD4LNd32Pseudo };
2020 unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo, ARM::VLD4LNq32Pseudo };
2021 return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes);
Bob Wilson243fcc52009-09-01 04:26:28 +00002022 }
2023
Bob Wilson11d98992010-03-23 06:20:33 +00002024 case Intrinsic::arm_neon_vst1: {
2025 unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2026 ARM::VST1d32, ARM::VST1d64 };
Bob Wilsone5ce4f62010-08-28 05:12:57 +00002027 unsigned QOpcodes[] = { ARM::VST1q8Pseudo, ARM::VST1q16Pseudo,
2028 ARM::VST1q32Pseudo, ARM::VST1q64Pseudo };
Bob Wilson11d98992010-03-23 06:20:33 +00002029 return SelectVST(N, 1, DOpcodes, QOpcodes, 0);
2030 }
2031
Bob Wilson31fb12f2009-08-26 17:39:53 +00002032 case Intrinsic::arm_neon_vst2: {
Bob Wilsone5ce4f62010-08-28 05:12:57 +00002033 unsigned DOpcodes[] = { ARM::VST2d8Pseudo, ARM::VST2d16Pseudo,
2034 ARM::VST2d32Pseudo, ARM::VST1q64Pseudo };
2035 unsigned QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
2036 ARM::VST2q32Pseudo };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002037 return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002038 }
2039
2040 case Intrinsic::arm_neon_vst3: {
Bob Wilson01ba4612010-08-26 18:51:29 +00002041 unsigned DOpcodes[] = { ARM::VST3d8Pseudo, ARM::VST3d16Pseudo,
2042 ARM::VST3d32Pseudo, ARM::VST1d64TPseudo };
2043 unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2044 ARM::VST3q16Pseudo_UPD,
2045 ARM::VST3q32Pseudo_UPD };
2046 unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2047 ARM::VST3q16oddPseudo_UPD,
2048 ARM::VST3q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002049 return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002050 }
2051
2052 case Intrinsic::arm_neon_vst4: {
Bob Wilson709d5922010-08-25 23:27:42 +00002053 unsigned DOpcodes[] = { ARM::VST4d8Pseudo, ARM::VST4d16Pseudo,
Bob Wilson70e48b22010-08-26 05:33:30 +00002054 ARM::VST4d32Pseudo, ARM::VST1d64QPseudo };
Bob Wilson709d5922010-08-25 23:27:42 +00002055 unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2056 ARM::VST4q16Pseudo_UPD,
2057 ARM::VST4q32Pseudo_UPD };
2058 unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2059 ARM::VST4q16oddPseudo_UPD,
2060 ARM::VST4q32oddPseudo_UPD };
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002061 return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
Bob Wilson31fb12f2009-08-26 17:39:53 +00002062 }
Bob Wilson8a3198b2009-09-01 18:51:56 +00002063
2064 case Intrinsic::arm_neon_vst2lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002065 unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo, ARM::VST2LNd16Pseudo,
2066 ARM::VST2LNd32Pseudo };
2067 unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo, ARM::VST2LNq32Pseudo };
2068 return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002069 }
2070
2071 case Intrinsic::arm_neon_vst3lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002072 unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo, ARM::VST3LNd16Pseudo,
2073 ARM::VST3LNd32Pseudo };
2074 unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo, ARM::VST3LNq32Pseudo };
2075 return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002076 }
2077
2078 case Intrinsic::arm_neon_vst4lane: {
Bob Wilson8466fa12010-09-13 23:01:35 +00002079 unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo, ARM::VST4LNd16Pseudo,
2080 ARM::VST4LNd32Pseudo };
2081 unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo, ARM::VST4LNq32Pseudo };
2082 return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes);
Bob Wilson8a3198b2009-09-01 18:51:56 +00002083 }
Bob Wilson31fb12f2009-08-26 17:39:53 +00002084 }
Bob Wilson429009b2010-05-06 16:05:26 +00002085 break;
Bob Wilson31fb12f2009-08-26 17:39:53 +00002086 }
Evan Chengde8aa4e2010-05-05 18:28:36 +00002087
Bob Wilsond491d6e2010-07-06 23:36:25 +00002088 case ISD::INTRINSIC_WO_CHAIN: {
2089 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2090 switch (IntNo) {
2091 default:
2092 break;
2093
2094 case Intrinsic::arm_neon_vtbl2:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002095 return SelectVTBL(N, false, 2, ARM::VTBL2Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002096 case Intrinsic::arm_neon_vtbl3:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002097 return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002098 case Intrinsic::arm_neon_vtbl4:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002099 return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002100
2101 case Intrinsic::arm_neon_vtbx2:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002102 return SelectVTBL(N, true, 2, ARM::VTBX2Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002103 case Intrinsic::arm_neon_vtbx3:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002104 return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
Bob Wilson78dfbc32010-07-07 00:08:54 +00002105 case Intrinsic::arm_neon_vtbx4:
Bob Wilsonbd916c52010-09-13 23:55:10 +00002106 return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
Bob Wilsond491d6e2010-07-06 23:36:25 +00002107 }
2108 break;
2109 }
2110
Bob Wilson429009b2010-05-06 16:05:26 +00002111 case ISD::CONCAT_VECTORS:
Evan Chengde8aa4e2010-05-05 18:28:36 +00002112 return SelectConcatVector(N);
2113 }
Evan Chenge5ad88e2008-12-10 21:54:21 +00002114
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002115 return SelectCode(N);
Evan Chenga8e29892007-01-19 07:51:42 +00002116}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002117
Bob Wilson224c2442009-05-19 05:53:42 +00002118bool ARMDAGToDAGISel::
2119SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2120 std::vector<SDValue> &OutOps) {
2121 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
Bob Wilson765cc0b2009-10-13 20:50:28 +00002122 // Require the address to be in a register. That is safe for all ARM
2123 // variants and it is hard to do anything much smarter without knowing
2124 // how the operand is used.
2125 OutOps.push_back(Op);
Bob Wilson224c2442009-05-19 05:53:42 +00002126 return false;
2127}
2128
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002129/// createARMISelDag - This pass converts a legalized DAG into a
2130/// ARM-specific DAG, ready for instruction scheduling.
2131///
Bob Wilson522ce972009-09-28 14:30:20 +00002132FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2133 CodeGenOpt::Level OptLevel) {
2134 return new ARMDAGToDAGISel(TM, OptLevel);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00002135}