blob: 42fbcfd670f45bc44a26e4bf1c49a45db6c57721 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARM.h"
Evan Chenga8e29892007-01-19 07:51:42 +000015#include "ARMISelLowering.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000016#include "ARMTargetMachine.h"
Evan Chenga8e29892007-01-19 07:51:42 +000017#include "ARMAddressingModes.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"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/SelectionDAG.h"
27#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/CodeGen/SSARegMap.h"
29#include "llvm/Target/TargetLowering.h"
30#include "llvm/Support/Debug.h"
Evan Chenga8e29892007-01-19 07:51:42 +000031#include <iostream>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000032using namespace llvm;
33
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000034//===--------------------------------------------------------------------===//
35/// ARMDAGToDAGISel - ARM specific code to select ARM machine
36/// instructions for SelectionDAG operations.
37///
38namespace {
39class ARMDAGToDAGISel : public SelectionDAGISel {
40 ARMTargetLowering Lowering;
41
Evan Chenga8e29892007-01-19 07:51:42 +000042 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
43 /// make the right decision when generating code for different targets.
44 const ARMSubtarget *Subtarget;
45
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000046public:
Evan Chenga8e29892007-01-19 07:51:42 +000047 ARMDAGToDAGISel(ARMTargetMachine &TM)
48 : SelectionDAGISel(Lowering), Lowering(TM),
49 Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000050 }
51
Evan Chenga8e29892007-01-19 07:51:42 +000052 virtual const char *getPassName() const {
53 return "ARM Instruction Selection";
54 }
55
Evan Cheng9ade2182006-08-26 05:34:46 +000056 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000057 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Evan Chenga8e29892007-01-19 07:51:42 +000058 bool SelectAddrMode2(SDOperand Op, SDOperand N, SDOperand &Base,
59 SDOperand &Offset, SDOperand &Opc);
60 bool SelectAddrMode2Offset(SDOperand Op, SDOperand N,
61 SDOperand &Offset, SDOperand &Opc);
62 bool SelectAddrMode3(SDOperand Op, SDOperand N, SDOperand &Base,
63 SDOperand &Offset, SDOperand &Opc);
64 bool SelectAddrMode3Offset(SDOperand Op, SDOperand N,
65 SDOperand &Offset, SDOperand &Opc);
66 bool SelectAddrMode5(SDOperand Op, SDOperand N, SDOperand &Base,
Evan Cheng0d538262006-11-08 20:34:28 +000067 SDOperand &Offset);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000068
Evan Chenga8e29892007-01-19 07:51:42 +000069 bool SelectAddrModePC(SDOperand Op, SDOperand N, SDOperand &Offset,
70 SDOperand &Label);
71
72 bool SelectThumbAddrModeRR(SDOperand Op, SDOperand N, SDOperand &Base,
73 SDOperand &Offset);
Evan Chengc38f2bc2007-01-23 22:59:13 +000074 bool SelectThumbAddrModeS1(SDOperand Op, SDOperand N, SDOperand &Base,
75 SDOperand &Offset, SDOperand &OffImm);
76 bool SelectThumbAddrModeS2(SDOperand Op, SDOperand N, SDOperand &Base,
77 SDOperand &Offset, SDOperand &OffImm);
78 bool SelectThumbAddrModeS4(SDOperand Op, SDOperand N, SDOperand &Base,
79 SDOperand &Offset, SDOperand &OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +000080 bool SelectThumbAddrModeSP(SDOperand Op, SDOperand N, SDOperand &Base,
81 SDOperand &Offset);
82
83 bool SelectShifterOperandReg(SDOperand Op, SDOperand N, SDOperand &A,
84 SDOperand &B, SDOperand &C);
85
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000086 // Include the pieces autogenerated from the target description.
87#include "ARMGenDAGISel.inc"
88};
Evan Chenga8e29892007-01-19 07:51:42 +000089}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000090
91void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
92 DEBUG(BB->dump());
93
94 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000095 DAG.RemoveDeadNodes();
96
97 ScheduleAndEmitDAG(DAG);
98}
99
Evan Cheng0d538262006-11-08 20:34:28 +0000100bool ARMDAGToDAGISel::SelectAddrMode2(SDOperand Op, SDOperand N,
Evan Chenga8e29892007-01-19 07:51:42 +0000101 SDOperand &Base, SDOperand &Offset,
102 SDOperand &Opc) {
103 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
104 Base = N;
105 if (N.getOpcode() == ISD::FrameIndex) {
106 int FI = cast<FrameIndexSDNode>(N)->getIndex();
107 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
108 } else if (N.getOpcode() == ARMISD::Wrapper) {
109 Base = N.getOperand(0);
110 }
111 Offset = CurDAG->getRegister(0, MVT::i32);
112 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
113 ARM_AM::no_shift),
114 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000115 return true;
116 }
Evan Chenga8e29892007-01-19 07:51:42 +0000117
118 // Match simple R +/- imm12 operands.
119 if (N.getOpcode() == ISD::ADD)
120 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
121 int RHSC = (int)RHS->getValue();
122 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits.
123 Base = N.getOperand(0);
124 Offset = CurDAG->getRegister(0, MVT::i32);
125 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, RHSC,
126 ARM_AM::no_shift),
127 MVT::i32);
128 return true;
129 } else if (RHSC < 0 && RHSC > -0x1000) {
130 Base = N.getOperand(0);
131 Offset = CurDAG->getRegister(0, MVT::i32);
132 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::sub, -RHSC,
133 ARM_AM::no_shift),
134 MVT::i32);
135 return true;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000136 }
Evan Chenga8e29892007-01-19 07:51:42 +0000137 }
138
139 // Otherwise this is R +/- [possibly shifted] R
140 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
141 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
142 unsigned ShAmt = 0;
143
144 Base = N.getOperand(0);
145 Offset = N.getOperand(1);
146
147 if (ShOpcVal != ARM_AM::no_shift) {
148 // Check to see if the RHS of the shift is a constant, if not, we can't fold
149 // it.
150 if (ConstantSDNode *Sh =
151 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
152 ShAmt = Sh->getValue();
153 Offset = N.getOperand(1).getOperand(0);
154 } else {
155 ShOpcVal = ARM_AM::no_shift;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000156 }
157 }
Evan Chenga8e29892007-01-19 07:51:42 +0000158
159 // Try matching (R shl C) + (R).
160 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
161 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
162 if (ShOpcVal != ARM_AM::no_shift) {
163 // Check to see if the RHS of the shift is a constant, if not, we can't
164 // fold it.
165 if (ConstantSDNode *Sh =
166 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
167 ShAmt = Sh->getValue();
168 Offset = N.getOperand(0).getOperand(0);
169 Base = N.getOperand(1);
170 } else {
171 ShOpcVal = ARM_AM::no_shift;
172 }
173 }
174 }
175
176 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
177 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000178 return true;
179}
180
Evan Chenga8e29892007-01-19 07:51:42 +0000181bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDOperand Op, SDOperand N,
182 SDOperand &Offset, SDOperand &Opc) {
183 unsigned Opcode = Op.getOpcode();
184 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
185 ? cast<LoadSDNode>(Op)->getAddressingMode()
186 : cast<StoreSDNode>(Op)->getAddressingMode();
187 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
188 ? ARM_AM::add : ARM_AM::sub;
189 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
190 int Val = (int)C->getValue();
191 if (Val >= 0 && Val < 0x1000) { // 12 bits.
192 Offset = CurDAG->getRegister(0, MVT::i32);
193 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
194 ARM_AM::no_shift),
195 MVT::i32);
196 return true;
197 }
198 }
199
200 Offset = N;
201 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
202 unsigned ShAmt = 0;
203 if (ShOpcVal != ARM_AM::no_shift) {
204 // Check to see if the RHS of the shift is a constant, if not, we can't fold
205 // it.
206 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
207 ShAmt = Sh->getValue();
208 Offset = N.getOperand(0);
209 } else {
210 ShOpcVal = ARM_AM::no_shift;
211 }
212 }
213
214 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
215 MVT::i32);
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000216 return true;
217}
218
Evan Chenga8e29892007-01-19 07:51:42 +0000219
220bool ARMDAGToDAGISel::SelectAddrMode3(SDOperand Op, SDOperand N,
221 SDOperand &Base, SDOperand &Offset,
222 SDOperand &Opc) {
223 if (N.getOpcode() == ISD::SUB) {
224 // X - C is canonicalize to X + -C, no need to handle it here.
225 Base = N.getOperand(0);
226 Offset = N.getOperand(1);
227 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
228 return true;
229 }
230
231 if (N.getOpcode() != ISD::ADD) {
232 Base = N;
233 if (N.getOpcode() == ISD::FrameIndex) {
234 int FI = cast<FrameIndexSDNode>(N)->getIndex();
235 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
236 }
237 Offset = CurDAG->getRegister(0, MVT::i32);
238 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
239 return true;
240 }
241
242 // If the RHS is +/- imm8, fold into addr mode.
243 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
244 int RHSC = (int)RHS->getValue();
245 if (RHSC >= 0 && RHSC < 256) {
246 Base = N.getOperand(0);
247 Offset = CurDAG->getRegister(0, MVT::i32);
248 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, RHSC),
249 MVT::i32);
250 return true;
251 } else if (RHSC < 0 && RHSC > -256) { // note -256 itself isn't allowed.
252 Base = N.getOperand(0);
253 Offset = CurDAG->getRegister(0, MVT::i32);
254 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, -RHSC),
255 MVT::i32);
256 return true;
257 }
258 }
259
260 Base = N.getOperand(0);
261 Offset = N.getOperand(1);
262 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
263 return true;
264}
265
266bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDOperand Op, SDOperand N,
267 SDOperand &Offset, SDOperand &Opc) {
268 unsigned Opcode = Op.getOpcode();
269 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
270 ? cast<LoadSDNode>(Op)->getAddressingMode()
271 : cast<StoreSDNode>(Op)->getAddressingMode();
272 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
273 ? ARM_AM::add : ARM_AM::sub;
274 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
275 int Val = (int)C->getValue();
276 if (Val >= 0 && Val < 256) {
277 Offset = CurDAG->getRegister(0, MVT::i32);
278 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
279 return true;
280 }
281 }
282
283 Offset = N;
284 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
285 return true;
286}
287
288
289bool ARMDAGToDAGISel::SelectAddrMode5(SDOperand Op, SDOperand N,
290 SDOperand &Base, SDOperand &Offset) {
291 if (N.getOpcode() != ISD::ADD) {
292 Base = N;
293 if (N.getOpcode() == ISD::FrameIndex) {
294 int FI = cast<FrameIndexSDNode>(N)->getIndex();
295 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
296 } else if (N.getOpcode() == ARMISD::Wrapper) {
297 Base = N.getOperand(0);
298 }
299 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
300 MVT::i32);
301 return true;
302 }
303
304 // If the RHS is +/- imm8, fold into addr mode.
305 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
306 int RHSC = (int)RHS->getValue();
307 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
308 RHSC >>= 2;
309 if (RHSC >= 0 && RHSC < 256) {
310 Base = N.getOperand(0);
311 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, RHSC),
312 MVT::i32);
313 return true;
314 } else if (RHSC < 0 && RHSC > -256) { // note -256 itself isn't allowed.
315 Base = N.getOperand(0);
316 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::sub,-RHSC),
317 MVT::i32);
318 return true;
319 }
320 }
321 }
322
323 Base = N;
324 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
325 MVT::i32);
326 return true;
327}
328
329bool ARMDAGToDAGISel::SelectAddrModePC(SDOperand Op, SDOperand N,
330 SDOperand &Offset, SDOperand &Label) {
331 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
332 Offset = N.getOperand(0);
333 SDOperand N1 = N.getOperand(1);
334 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getValue(),
335 MVT::i32);
336 return true;
337 }
338 return false;
339}
340
341bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDOperand Op, SDOperand N,
342 SDOperand &Base, SDOperand &Offset){
Evan Chengc38f2bc2007-01-23 22:59:13 +0000343 if (N.getOpcode() != ISD::ADD) {
344 Base = N;
345 // We must materialize a zero in a reg! Returning an constant here won't
346 // work since its node is -1 so it won't get added to the selection queue.
347 // Explicitly issue a tMOVri8 node!
348 Offset = SDOperand(CurDAG->getTargetNode(ARM::tMOVri8, MVT::i32,
349 CurDAG->getTargetConstant(0, MVT::i32)), 0);
350 return true;
351 }
352
Evan Chenga8e29892007-01-19 07:51:42 +0000353 Base = N.getOperand(0);
354 Offset = N.getOperand(1);
355 return true;
356}
357
358static bool SelectThumbAddrModeRI5(SDOperand N, unsigned Scale,
359 TargetLowering &TLI, SelectionDAG *CurDAG,
Evan Chengc38f2bc2007-01-23 22:59:13 +0000360 SDOperand &Base, SDOperand &Offset,
361 SDOperand &OffImm) {
Evan Chenga8e29892007-01-19 07:51:42 +0000362 if (N.getOpcode() == ISD::FrameIndex)
363 return false;
364
365 if (N.getOpcode() != ISD::ADD) {
366 Base = (N.getOpcode() == ARMISD::Wrapper) ? N.getOperand(0) : N;
Evan Chengc38f2bc2007-01-23 22:59:13 +0000367 Offset = CurDAG->getRegister(0, MVT::i32);
368 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000369 return true;
370 }
371
372 // If the RHS is + imm5 * scale, fold into addr mode.
373 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
374 int RHSC = (int)RHS->getValue();
375 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
376 RHSC /= Scale;
377 if (RHSC >= 0 && RHSC < 32) {
378 Base = N.getOperand(0);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000379 Offset = CurDAG->getRegister(0, MVT::i32);
380 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000381 return true;
382 }
383 }
384 }
385
Evan Chengc38f2bc2007-01-23 22:59:13 +0000386 Base = N.getOperand(0);
387 Offset = N.getOperand(1);
388 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
389 return true;
Evan Chenga8e29892007-01-19 07:51:42 +0000390}
391
Evan Chengc38f2bc2007-01-23 22:59:13 +0000392bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDOperand Op, SDOperand N,
393 SDOperand &Base, SDOperand &Offset,
394 SDOperand &OffImm) {
395 return SelectThumbAddrModeRI5(N, 1, TLI, CurDAG, Base, Offset, OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000396}
397
Evan Chengc38f2bc2007-01-23 22:59:13 +0000398bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDOperand Op, SDOperand N,
399 SDOperand &Base, SDOperand &Offset,
400 SDOperand &OffImm) {
401 return SelectThumbAddrModeRI5(N, 2, TLI, CurDAG, Base, Offset, OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000402}
403
Evan Chengc38f2bc2007-01-23 22:59:13 +0000404bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDOperand Op, SDOperand N,
405 SDOperand &Base, SDOperand &Offset,
406 SDOperand &OffImm) {
407 return SelectThumbAddrModeRI5(N, 4, TLI, CurDAG, Base, Offset, OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000408}
409
410bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDOperand Op, SDOperand N,
411 SDOperand &Base, SDOperand &Offset) {
412 if (N.getOpcode() == ISD::FrameIndex) {
413 int FI = cast<FrameIndexSDNode>(N)->getIndex();
414 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
415 Offset = CurDAG->getTargetConstant(0, MVT::i32);
416 return true;
417 }
418
419 return false;
420}
421
422bool ARMDAGToDAGISel::SelectShifterOperandReg(SDOperand Op,
423 SDOperand N,
424 SDOperand &BaseReg,
425 SDOperand &ShReg,
426 SDOperand &Opc) {
427 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
428
429 // Don't match base register only case. That is matched to a separate
430 // lower complexity pattern with explicit register operand.
431 if (ShOpcVal == ARM_AM::no_shift) return false;
432
433 BaseReg = N.getOperand(0);
434 unsigned ShImmVal = 0;
435 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
436 ShReg = CurDAG->getRegister(0, MVT::i32);
437 ShImmVal = RHS->getValue() & 31;
438 } else {
439 ShReg = N.getOperand(1);
440 }
441 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
442 MVT::i32);
443 return true;
444}
445
446
Evan Cheng9ade2182006-08-26 05:34:46 +0000447SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000448 SDNode *N = Op.Val;
Evan Chenga8e29892007-01-19 07:51:42 +0000449 unsigned Opcode = N->getOpcode();
450
451 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < ARMISD::FIRST_NUMBER)
452 return NULL; // Already selected.
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000453
454 switch (N->getOpcode()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000455 default: break;
456 case ISD::Constant: {
457 unsigned Val = cast<ConstantSDNode>(N)->getValue();
458 bool UseCP = true;
459 if (Subtarget->isThumb())
460 UseCP = (Val > 255 && // MOV
461 ~Val > 255 && // MOV + MVN
462 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
463 else
464 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
465 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
466 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
467 if (UseCP) {
468 SDOperand CPIdx =
469 CurDAG->getTargetConstantPool(ConstantInt::get(Type::Int32Ty, Val),
470 TLI.getPointerTy());
471 SDOperand Ops[] = {
472 CPIdx,
473 CurDAG->getRegister(0, MVT::i32),
474 CurDAG->getTargetConstant(0, MVT::i32),
475 CurDAG->getEntryNode()
476 };
477 SDNode *ResNode =
478 CurDAG->getTargetNode(ARM::LDR, MVT::i32, MVT::Other, Ops, 4);
479 ReplaceUses(Op, SDOperand(ResNode, 0));
480 return NULL;
481 }
482
483 // Other cases are autogenerated.
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000484 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000485 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000486 case ISD::FrameIndex: {
Evan Chenga8e29892007-01-19 07:51:42 +0000487 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindolaf819a492006-11-09 13:58:55 +0000488 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Chenga8e29892007-01-19 07:51:42 +0000489 unsigned Opc = Subtarget->isThumb() ? ARM::tADDrSPi : ARM::ADDri;
490 SDOperand TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
491 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, TFI,
492 CurDAG->getTargetConstant(0, MVT::i32));
493 }
494 case ISD::MUL:
495 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
496 unsigned RHSV = C->getValue();
497 if (!RHSV) break;
498 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
499 SDOperand V = Op.getOperand(0);
500 AddToISelQueue(V);
501 unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV-1));
502 SDOperand Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
503 CurDAG->getTargetConstant(ShImm, MVT::i32)
504 };
505 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 4);
506 }
507 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
508 SDOperand V = Op.getOperand(0);
509 AddToISelQueue(V);
510 unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV+1));
511 SDOperand Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
512 CurDAG->getTargetConstant(ShImm, MVT::i32)
513 };
514 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 4);
515 }
516 }
517 break;
518 case ARMISD::FMRRD:
519 AddToISelQueue(Op.getOperand(0));
520 return CurDAG->getTargetNode(ARM::FMRRD, MVT::i32, MVT::i32,
521 Op.getOperand(0));
522 case ARMISD::MULHILOU:
523 AddToISelQueue(Op.getOperand(0));
524 AddToISelQueue(Op.getOperand(1));
525 return CurDAG->getTargetNode(ARM::UMULL, MVT::i32, MVT::i32,
526 Op.getOperand(0), Op.getOperand(1));
527 case ARMISD::MULHILOS:
528 AddToISelQueue(Op.getOperand(0));
529 AddToISelQueue(Op.getOperand(1));
530 return CurDAG->getTargetNode(ARM::SMULL, MVT::i32, MVT::i32,
531 Op.getOperand(0), Op.getOperand(1));
532 case ISD::LOAD: {
533 LoadSDNode *LD = cast<LoadSDNode>(Op);
534 ISD::MemIndexedMode AM = LD->getAddressingMode();
535 MVT::ValueType LoadedVT = LD->getLoadedVT();
536 if (AM != ISD::UNINDEXED) {
537 SDOperand Offset, AMOpc;
538 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
539 unsigned Opcode = 0;
540 bool Match = false;
541 if (LoadedVT == MVT::i32 &&
542 SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
543 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
544 Match = true;
545 } else if (LoadedVT == MVT::i16 &&
546 SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
547 Match = true;
548 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
549 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
550 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
551 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
552 if (LD->getExtensionType() == ISD::SEXTLOAD) {
553 if (SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
554 Match = true;
555 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
556 }
557 } else {
558 if (SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
559 Match = true;
560 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
561 }
562 }
563 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000564
Evan Chenga8e29892007-01-19 07:51:42 +0000565 if (Match) {
566 SDOperand Chain = LD->getChain();
567 SDOperand Base = LD->getBasePtr();
568 AddToISelQueue(Chain);
569 AddToISelQueue(Base);
570 AddToISelQueue(Offset);
571 SDOperand Ops[] = { Base, Offset, AMOpc, Chain };
572 return CurDAG->getTargetNode(Opcode, MVT::i32, MVT::i32,
573 MVT::Other, Ops, 4);
574 }
575 }
576 // Other cases are autogenerated.
Rafael Espindolaf819a492006-11-09 13:58:55 +0000577 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000578 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000579 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000580
Evan Chenga8e29892007-01-19 07:51:42 +0000581 return SelectCode(Op);
582}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000583
584/// createARMISelDag - This pass converts a legalized DAG into a
585/// ARM-specific DAG, ready for instruction scheduling.
586///
Evan Chenga8e29892007-01-19 07:51:42 +0000587FunctionPass *llvm::createARMISelDag(ARMTargetMachine &TM) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000588 return new ARMDAGToDAGISel(TM);
589}