blob: 2307fe2bdf27f3db08db7aead2ef5d369ffd683e [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 Cheng79d43262007-01-24 02:21:22 +000074 bool SelectThumbAddrModeRI5(SDOperand Op, SDOperand N, unsigned Scale,
75 SDOperand &Base, SDOperand &Offset,
76 SDOperand &OffImm);
Evan Chengc38f2bc2007-01-23 22:59:13 +000077 bool SelectThumbAddrModeS1(SDOperand Op, SDOperand N, SDOperand &Base,
78 SDOperand &Offset, SDOperand &OffImm);
79 bool SelectThumbAddrModeS2(SDOperand Op, SDOperand N, SDOperand &Base,
80 SDOperand &Offset, SDOperand &OffImm);
81 bool SelectThumbAddrModeS4(SDOperand Op, SDOperand N, SDOperand &Base,
82 SDOperand &Offset, SDOperand &OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +000083 bool SelectThumbAddrModeSP(SDOperand Op, SDOperand N, SDOperand &Base,
Evan Cheng79d43262007-01-24 02:21:22 +000084 SDOperand &OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +000085
86 bool SelectShifterOperandReg(SDOperand Op, SDOperand N, SDOperand &A,
87 SDOperand &B, SDOperand &C);
88
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000089 // Include the pieces autogenerated from the target description.
90#include "ARMGenDAGISel.inc"
91};
Evan Chenga8e29892007-01-19 07:51:42 +000092}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000093
94void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
95 DEBUG(BB->dump());
96
97 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000098 DAG.RemoveDeadNodes();
99
100 ScheduleAndEmitDAG(DAG);
101}
102
Evan Cheng0d538262006-11-08 20:34:28 +0000103bool ARMDAGToDAGISel::SelectAddrMode2(SDOperand Op, SDOperand N,
Evan Chenga8e29892007-01-19 07:51:42 +0000104 SDOperand &Base, SDOperand &Offset,
105 SDOperand &Opc) {
106 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
107 Base = N;
108 if (N.getOpcode() == ISD::FrameIndex) {
109 int FI = cast<FrameIndexSDNode>(N)->getIndex();
110 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
111 } else if (N.getOpcode() == ARMISD::Wrapper) {
112 Base = N.getOperand(0);
113 }
114 Offset = CurDAG->getRegister(0, MVT::i32);
115 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
116 ARM_AM::no_shift),
117 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000118 return true;
119 }
Evan Chenga8e29892007-01-19 07:51:42 +0000120
121 // Match simple R +/- imm12 operands.
122 if (N.getOpcode() == ISD::ADD)
123 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
124 int RHSC = (int)RHS->getValue();
125 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits.
126 Base = N.getOperand(0);
127 Offset = CurDAG->getRegister(0, MVT::i32);
128 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, RHSC,
129 ARM_AM::no_shift),
130 MVT::i32);
131 return true;
132 } else if (RHSC < 0 && RHSC > -0x1000) {
133 Base = N.getOperand(0);
134 Offset = CurDAG->getRegister(0, MVT::i32);
135 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::sub, -RHSC,
136 ARM_AM::no_shift),
137 MVT::i32);
138 return true;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000139 }
Evan Chenga8e29892007-01-19 07:51:42 +0000140 }
141
142 // Otherwise this is R +/- [possibly shifted] R
143 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
144 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
145 unsigned ShAmt = 0;
146
147 Base = N.getOperand(0);
148 Offset = N.getOperand(1);
149
150 if (ShOpcVal != ARM_AM::no_shift) {
151 // Check to see if the RHS of the shift is a constant, if not, we can't fold
152 // it.
153 if (ConstantSDNode *Sh =
154 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
155 ShAmt = Sh->getValue();
156 Offset = N.getOperand(1).getOperand(0);
157 } else {
158 ShOpcVal = ARM_AM::no_shift;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000159 }
160 }
Evan Chenga8e29892007-01-19 07:51:42 +0000161
162 // Try matching (R shl C) + (R).
163 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
164 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
165 if (ShOpcVal != ARM_AM::no_shift) {
166 // Check to see if the RHS of the shift is a constant, if not, we can't
167 // fold it.
168 if (ConstantSDNode *Sh =
169 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
170 ShAmt = Sh->getValue();
171 Offset = N.getOperand(0).getOperand(0);
172 Base = N.getOperand(1);
173 } else {
174 ShOpcVal = ARM_AM::no_shift;
175 }
176 }
177 }
178
179 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
180 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000181 return true;
182}
183
Evan Chenga8e29892007-01-19 07:51:42 +0000184bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDOperand Op, SDOperand N,
185 SDOperand &Offset, SDOperand &Opc) {
186 unsigned Opcode = Op.getOpcode();
187 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
188 ? cast<LoadSDNode>(Op)->getAddressingMode()
189 : cast<StoreSDNode>(Op)->getAddressingMode();
190 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
191 ? ARM_AM::add : ARM_AM::sub;
192 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
193 int Val = (int)C->getValue();
194 if (Val >= 0 && Val < 0x1000) { // 12 bits.
195 Offset = CurDAG->getRegister(0, MVT::i32);
196 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
197 ARM_AM::no_shift),
198 MVT::i32);
199 return true;
200 }
201 }
202
203 Offset = N;
204 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
205 unsigned ShAmt = 0;
206 if (ShOpcVal != ARM_AM::no_shift) {
207 // Check to see if the RHS of the shift is a constant, if not, we can't fold
208 // it.
209 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
210 ShAmt = Sh->getValue();
211 Offset = N.getOperand(0);
212 } else {
213 ShOpcVal = ARM_AM::no_shift;
214 }
215 }
216
217 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
218 MVT::i32);
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000219 return true;
220}
221
Evan Chenga8e29892007-01-19 07:51:42 +0000222
223bool ARMDAGToDAGISel::SelectAddrMode3(SDOperand Op, SDOperand N,
224 SDOperand &Base, SDOperand &Offset,
225 SDOperand &Opc) {
226 if (N.getOpcode() == ISD::SUB) {
227 // X - C is canonicalize to X + -C, no need to handle it here.
228 Base = N.getOperand(0);
229 Offset = N.getOperand(1);
230 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
231 return true;
232 }
233
234 if (N.getOpcode() != ISD::ADD) {
235 Base = N;
236 if (N.getOpcode() == ISD::FrameIndex) {
237 int FI = cast<FrameIndexSDNode>(N)->getIndex();
238 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
239 }
240 Offset = CurDAG->getRegister(0, MVT::i32);
241 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
242 return true;
243 }
244
245 // If the RHS is +/- imm8, fold into addr mode.
246 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
247 int RHSC = (int)RHS->getValue();
248 if (RHSC >= 0 && RHSC < 256) {
249 Base = N.getOperand(0);
250 Offset = CurDAG->getRegister(0, MVT::i32);
251 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, RHSC),
252 MVT::i32);
253 return true;
254 } else if (RHSC < 0 && RHSC > -256) { // note -256 itself isn't allowed.
255 Base = N.getOperand(0);
256 Offset = CurDAG->getRegister(0, MVT::i32);
257 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, -RHSC),
258 MVT::i32);
259 return true;
260 }
261 }
262
263 Base = N.getOperand(0);
264 Offset = N.getOperand(1);
265 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
266 return true;
267}
268
269bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDOperand Op, SDOperand N,
270 SDOperand &Offset, SDOperand &Opc) {
271 unsigned Opcode = Op.getOpcode();
272 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
273 ? cast<LoadSDNode>(Op)->getAddressingMode()
274 : cast<StoreSDNode>(Op)->getAddressingMode();
275 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
276 ? ARM_AM::add : ARM_AM::sub;
277 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
278 int Val = (int)C->getValue();
279 if (Val >= 0 && Val < 256) {
280 Offset = CurDAG->getRegister(0, MVT::i32);
281 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
282 return true;
283 }
284 }
285
286 Offset = N;
287 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
288 return true;
289}
290
291
292bool ARMDAGToDAGISel::SelectAddrMode5(SDOperand Op, SDOperand N,
293 SDOperand &Base, SDOperand &Offset) {
294 if (N.getOpcode() != ISD::ADD) {
295 Base = N;
296 if (N.getOpcode() == ISD::FrameIndex) {
297 int FI = cast<FrameIndexSDNode>(N)->getIndex();
298 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
299 } else if (N.getOpcode() == ARMISD::Wrapper) {
300 Base = N.getOperand(0);
301 }
302 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
303 MVT::i32);
304 return true;
305 }
306
307 // If the RHS is +/- imm8, fold into addr mode.
308 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
309 int RHSC = (int)RHS->getValue();
310 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
311 RHSC >>= 2;
312 if (RHSC >= 0 && RHSC < 256) {
313 Base = N.getOperand(0);
314 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, RHSC),
315 MVT::i32);
316 return true;
317 } else if (RHSC < 0 && RHSC > -256) { // note -256 itself isn't allowed.
318 Base = N.getOperand(0);
319 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::sub,-RHSC),
320 MVT::i32);
321 return true;
322 }
323 }
324 }
325
326 Base = N;
327 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
328 MVT::i32);
329 return true;
330}
331
332bool ARMDAGToDAGISel::SelectAddrModePC(SDOperand Op, SDOperand N,
333 SDOperand &Offset, SDOperand &Label) {
334 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
335 Offset = N.getOperand(0);
336 SDOperand N1 = N.getOperand(1);
337 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getValue(),
338 MVT::i32);
339 return true;
340 }
341 return false;
342}
343
344bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDOperand Op, SDOperand N,
345 SDOperand &Base, SDOperand &Offset){
Evan Chengc38f2bc2007-01-23 22:59:13 +0000346 if (N.getOpcode() != ISD::ADD) {
347 Base = N;
348 // We must materialize a zero in a reg! Returning an constant here won't
349 // work since its node is -1 so it won't get added to the selection queue.
350 // Explicitly issue a tMOVri8 node!
351 Offset = SDOperand(CurDAG->getTargetNode(ARM::tMOVri8, MVT::i32,
352 CurDAG->getTargetConstant(0, MVT::i32)), 0);
353 return true;
354 }
355
Evan Chenga8e29892007-01-19 07:51:42 +0000356 Base = N.getOperand(0);
357 Offset = N.getOperand(1);
358 return true;
359}
360
Evan Cheng79d43262007-01-24 02:21:22 +0000361bool
362ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDOperand Op, SDOperand N,
363 unsigned Scale, SDOperand &Base,
364 SDOperand &Offset, SDOperand &OffImm) {
365 if (Scale == 4) {
366 SDOperand TmpBase, TmpOffImm;
367 if (SelectThumbAddrModeSP(Op, N, TmpBase, TmpOffImm))
368 return false; // We want to select tLDRspi / tSTRspi instead.
369 }
370
Evan Chenga8e29892007-01-19 07:51:42 +0000371 if (N.getOpcode() != ISD::ADD) {
372 Base = (N.getOpcode() == ARMISD::Wrapper) ? N.getOperand(0) : N;
Evan Chengc38f2bc2007-01-23 22:59:13 +0000373 Offset = CurDAG->getRegister(0, MVT::i32);
374 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000375 return true;
376 }
377
378 // If the RHS is + imm5 * scale, fold into addr mode.
379 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
380 int RHSC = (int)RHS->getValue();
381 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
382 RHSC /= Scale;
383 if (RHSC >= 0 && RHSC < 32) {
384 Base = N.getOperand(0);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000385 Offset = CurDAG->getRegister(0, MVT::i32);
386 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000387 return true;
388 }
389 }
390 }
391
Evan Chengc38f2bc2007-01-23 22:59:13 +0000392 Base = N.getOperand(0);
393 Offset = N.getOperand(1);
394 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
395 return true;
Evan Chenga8e29892007-01-19 07:51:42 +0000396}
397
Evan Chengc38f2bc2007-01-23 22:59:13 +0000398bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDOperand Op, SDOperand N,
399 SDOperand &Base, SDOperand &Offset,
400 SDOperand &OffImm) {
Evan Cheng79d43262007-01-24 02:21:22 +0000401 return SelectThumbAddrModeRI5(Op, N, 1, Base, Offset, OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000402}
403
Evan Chengc38f2bc2007-01-23 22:59:13 +0000404bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDOperand Op, SDOperand N,
405 SDOperand &Base, SDOperand &Offset,
406 SDOperand &OffImm) {
Evan Cheng79d43262007-01-24 02:21:22 +0000407 return SelectThumbAddrModeRI5(Op, N, 2, Base, Offset, OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000408}
409
Evan Chengc38f2bc2007-01-23 22:59:13 +0000410bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDOperand Op, SDOperand N,
411 SDOperand &Base, SDOperand &Offset,
412 SDOperand &OffImm) {
Evan Cheng79d43262007-01-24 02:21:22 +0000413 return SelectThumbAddrModeRI5(Op, N, 4, Base, Offset, OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000414}
415
416bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDOperand Op, SDOperand N,
Evan Cheng79d43262007-01-24 02:21:22 +0000417 SDOperand &Base, SDOperand &OffImm) {
Evan Chenga8e29892007-01-19 07:51:42 +0000418 if (N.getOpcode() == ISD::FrameIndex) {
419 int FI = cast<FrameIndexSDNode>(N)->getIndex();
420 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Evan Cheng79d43262007-01-24 02:21:22 +0000421 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000422 return true;
423 }
Evan Cheng79d43262007-01-24 02:21:22 +0000424
425 if (N.getOpcode() == ISD::ADD &&
426 N.getOperand(0).getOpcode() == ISD::FrameIndex) {
427 // If the RHS is + imm8 * scale, fold into addr mode.
428 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
429 int RHSC = (int)RHS->getValue();
430 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
431 RHSC >>= 2;
432 if (RHSC >= 0 && RHSC < 256) {
433 int FI = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
434 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
435 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
436 return true;
437 }
438 }
439 }
440 }
Evan Chenga8e29892007-01-19 07:51:42 +0000441
442 return false;
443}
444
445bool ARMDAGToDAGISel::SelectShifterOperandReg(SDOperand Op,
446 SDOperand N,
447 SDOperand &BaseReg,
448 SDOperand &ShReg,
449 SDOperand &Opc) {
450 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
451
452 // Don't match base register only case. That is matched to a separate
453 // lower complexity pattern with explicit register operand.
454 if (ShOpcVal == ARM_AM::no_shift) return false;
455
456 BaseReg = N.getOperand(0);
457 unsigned ShImmVal = 0;
458 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
459 ShReg = CurDAG->getRegister(0, MVT::i32);
460 ShImmVal = RHS->getValue() & 31;
461 } else {
462 ShReg = N.getOperand(1);
463 }
464 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
465 MVT::i32);
466 return true;
467}
468
469
Evan Cheng9ade2182006-08-26 05:34:46 +0000470SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000471 SDNode *N = Op.Val;
Evan Chenga8e29892007-01-19 07:51:42 +0000472 unsigned Opcode = N->getOpcode();
473
474 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < ARMISD::FIRST_NUMBER)
475 return NULL; // Already selected.
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000476
477 switch (N->getOpcode()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000478 default: break;
479 case ISD::Constant: {
480 unsigned Val = cast<ConstantSDNode>(N)->getValue();
481 bool UseCP = true;
482 if (Subtarget->isThumb())
483 UseCP = (Val > 255 && // MOV
484 ~Val > 255 && // MOV + MVN
485 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
486 else
487 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
488 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
489 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
490 if (UseCP) {
491 SDOperand CPIdx =
492 CurDAG->getTargetConstantPool(ConstantInt::get(Type::Int32Ty, Val),
493 TLI.getPointerTy());
494 SDOperand Ops[] = {
495 CPIdx,
496 CurDAG->getRegister(0, MVT::i32),
497 CurDAG->getTargetConstant(0, MVT::i32),
498 CurDAG->getEntryNode()
499 };
500 SDNode *ResNode =
501 CurDAG->getTargetNode(ARM::LDR, MVT::i32, MVT::Other, Ops, 4);
502 ReplaceUses(Op, SDOperand(ResNode, 0));
503 return NULL;
504 }
505
506 // Other cases are autogenerated.
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000507 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000508 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000509 case ISD::FrameIndex: {
Evan Chenga8e29892007-01-19 07:51:42 +0000510 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindolaf819a492006-11-09 13:58:55 +0000511 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Chenga8e29892007-01-19 07:51:42 +0000512 unsigned Opc = Subtarget->isThumb() ? ARM::tADDrSPi : ARM::ADDri;
513 SDOperand TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
514 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, TFI,
515 CurDAG->getTargetConstant(0, MVT::i32));
516 }
517 case ISD::MUL:
Evan Cheng79d43262007-01-24 02:21:22 +0000518 if (Subtarget->isThumb())
519 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000520 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
521 unsigned RHSV = C->getValue();
522 if (!RHSV) break;
523 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
524 SDOperand V = Op.getOperand(0);
525 AddToISelQueue(V);
526 unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV-1));
527 SDOperand Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
528 CurDAG->getTargetConstant(ShImm, MVT::i32)
529 };
530 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 4);
531 }
532 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
533 SDOperand V = Op.getOperand(0);
534 AddToISelQueue(V);
535 unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV+1));
536 SDOperand Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
537 CurDAG->getTargetConstant(ShImm, MVT::i32)
538 };
539 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 4);
540 }
541 }
542 break;
543 case ARMISD::FMRRD:
544 AddToISelQueue(Op.getOperand(0));
545 return CurDAG->getTargetNode(ARM::FMRRD, MVT::i32, MVT::i32,
546 Op.getOperand(0));
547 case ARMISD::MULHILOU:
548 AddToISelQueue(Op.getOperand(0));
549 AddToISelQueue(Op.getOperand(1));
550 return CurDAG->getTargetNode(ARM::UMULL, MVT::i32, MVT::i32,
551 Op.getOperand(0), Op.getOperand(1));
552 case ARMISD::MULHILOS:
553 AddToISelQueue(Op.getOperand(0));
554 AddToISelQueue(Op.getOperand(1));
555 return CurDAG->getTargetNode(ARM::SMULL, MVT::i32, MVT::i32,
556 Op.getOperand(0), Op.getOperand(1));
557 case ISD::LOAD: {
558 LoadSDNode *LD = cast<LoadSDNode>(Op);
559 ISD::MemIndexedMode AM = LD->getAddressingMode();
560 MVT::ValueType LoadedVT = LD->getLoadedVT();
561 if (AM != ISD::UNINDEXED) {
562 SDOperand Offset, AMOpc;
563 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
564 unsigned Opcode = 0;
565 bool Match = false;
566 if (LoadedVT == MVT::i32 &&
567 SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
568 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
569 Match = true;
570 } else if (LoadedVT == MVT::i16 &&
571 SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
572 Match = true;
573 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
574 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
575 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
576 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
577 if (LD->getExtensionType() == ISD::SEXTLOAD) {
578 if (SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
579 Match = true;
580 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
581 }
582 } else {
583 if (SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
584 Match = true;
585 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
586 }
587 }
588 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000589
Evan Chenga8e29892007-01-19 07:51:42 +0000590 if (Match) {
591 SDOperand Chain = LD->getChain();
592 SDOperand Base = LD->getBasePtr();
593 AddToISelQueue(Chain);
594 AddToISelQueue(Base);
595 AddToISelQueue(Offset);
596 SDOperand Ops[] = { Base, Offset, AMOpc, Chain };
597 return CurDAG->getTargetNode(Opcode, MVT::i32, MVT::i32,
598 MVT::Other, Ops, 4);
599 }
600 }
601 // Other cases are autogenerated.
Rafael Espindolaf819a492006-11-09 13:58:55 +0000602 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000603 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000604 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000605
Evan Chenga8e29892007-01-19 07:51:42 +0000606 return SelectCode(Op);
607}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000608
609/// createARMISelDag - This pass converts a legalized DAG into a
610/// ARM-specific DAG, ready for instruction scheduling.
611///
Evan Chenga8e29892007-01-19 07:51:42 +0000612FunctionPass *llvm::createARMISelDag(ARMTargetMachine &TM) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000613 return new ARMDAGToDAGISel(TM);
614}