blob: 9b06adda9c1a46d50ffb2409fa12246fa50766a7 [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();
Evan Chenge966d642007-01-24 02:45:25 +0000125 if ((RHSC >= 0 && RHSC < 0x1000) ||
126 (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
Evan Chenga8e29892007-01-19 07:51:42 +0000127 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000128 if (Base.getOpcode() == ISD::FrameIndex) {
129 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
130 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
131 }
Evan Chenga8e29892007-01-19 07:51:42 +0000132 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000133
134 ARM_AM::AddrOpc AddSub = ARM_AM::add;
135 if (RHSC < 0) {
136 AddSub = ARM_AM::sub;
137 RHSC = - RHSC;
138 }
139 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
Evan Chenga8e29892007-01-19 07:51:42 +0000140 ARM_AM::no_shift),
141 MVT::i32);
142 return true;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000143 }
Evan Chenga8e29892007-01-19 07:51:42 +0000144 }
145
146 // Otherwise this is R +/- [possibly shifted] R
147 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
148 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
149 unsigned ShAmt = 0;
150
151 Base = N.getOperand(0);
152 Offset = N.getOperand(1);
153
154 if (ShOpcVal != ARM_AM::no_shift) {
155 // Check to see if the RHS of the shift is a constant, if not, we can't fold
156 // it.
157 if (ConstantSDNode *Sh =
158 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
159 ShAmt = Sh->getValue();
160 Offset = N.getOperand(1).getOperand(0);
161 } else {
162 ShOpcVal = ARM_AM::no_shift;
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000163 }
164 }
Evan Chenga8e29892007-01-19 07:51:42 +0000165
166 // Try matching (R shl C) + (R).
167 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
168 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
169 if (ShOpcVal != ARM_AM::no_shift) {
170 // Check to see if the RHS of the shift is a constant, if not, we can't
171 // fold it.
172 if (ConstantSDNode *Sh =
173 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
174 ShAmt = Sh->getValue();
175 Offset = N.getOperand(0).getOperand(0);
176 Base = N.getOperand(1);
177 } else {
178 ShOpcVal = ARM_AM::no_shift;
179 }
180 }
181 }
182
183 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
184 MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000185 return true;
186}
187
Evan Chenga8e29892007-01-19 07:51:42 +0000188bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDOperand Op, SDOperand N,
189 SDOperand &Offset, SDOperand &Opc) {
190 unsigned Opcode = Op.getOpcode();
191 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
192 ? cast<LoadSDNode>(Op)->getAddressingMode()
193 : cast<StoreSDNode>(Op)->getAddressingMode();
194 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
195 ? ARM_AM::add : ARM_AM::sub;
196 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
197 int Val = (int)C->getValue();
198 if (Val >= 0 && Val < 0x1000) { // 12 bits.
199 Offset = CurDAG->getRegister(0, MVT::i32);
200 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
201 ARM_AM::no_shift),
202 MVT::i32);
203 return true;
204 }
205 }
206
207 Offset = N;
208 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
209 unsigned ShAmt = 0;
210 if (ShOpcVal != ARM_AM::no_shift) {
211 // Check to see if the RHS of the shift is a constant, if not, we can't fold
212 // it.
213 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
214 ShAmt = Sh->getValue();
215 Offset = N.getOperand(0);
216 } else {
217 ShOpcVal = ARM_AM::no_shift;
218 }
219 }
220
221 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
222 MVT::i32);
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000223 return true;
224}
225
Evan Chenga8e29892007-01-19 07:51:42 +0000226
227bool ARMDAGToDAGISel::SelectAddrMode3(SDOperand Op, SDOperand N,
228 SDOperand &Base, SDOperand &Offset,
229 SDOperand &Opc) {
230 if (N.getOpcode() == ISD::SUB) {
231 // X - C is canonicalize to X + -C, no need to handle it here.
232 Base = N.getOperand(0);
233 Offset = N.getOperand(1);
234 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
235 return true;
236 }
237
238 if (N.getOpcode() != ISD::ADD) {
239 Base = N;
240 if (N.getOpcode() == ISD::FrameIndex) {
241 int FI = cast<FrameIndexSDNode>(N)->getIndex();
242 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
243 }
244 Offset = CurDAG->getRegister(0, MVT::i32);
245 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
246 return true;
247 }
248
249 // If the RHS is +/- imm8, fold into addr mode.
250 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
251 int RHSC = (int)RHS->getValue();
Evan Chenge966d642007-01-24 02:45:25 +0000252 if ((RHSC >= 0 && RHSC < 256) ||
253 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000254 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000255 if (Base.getOpcode() == ISD::FrameIndex) {
256 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
257 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
258 }
Evan Chenga8e29892007-01-19 07:51:42 +0000259 Offset = CurDAG->getRegister(0, MVT::i32);
Evan Chenge966d642007-01-24 02:45:25 +0000260
261 ARM_AM::AddrOpc AddSub = ARM_AM::add;
262 if (RHSC < 0) {
263 AddSub = ARM_AM::sub;
264 RHSC = - RHSC;
265 }
266 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000267 return true;
268 }
269 }
270
271 Base = N.getOperand(0);
272 Offset = N.getOperand(1);
273 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
274 return true;
275}
276
277bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDOperand Op, SDOperand N,
278 SDOperand &Offset, SDOperand &Opc) {
279 unsigned Opcode = Op.getOpcode();
280 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
281 ? cast<LoadSDNode>(Op)->getAddressingMode()
282 : cast<StoreSDNode>(Op)->getAddressingMode();
283 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
284 ? ARM_AM::add : ARM_AM::sub;
285 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
286 int Val = (int)C->getValue();
287 if (Val >= 0 && Val < 256) {
288 Offset = CurDAG->getRegister(0, MVT::i32);
289 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
290 return true;
291 }
292 }
293
294 Offset = N;
295 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
296 return true;
297}
298
299
300bool ARMDAGToDAGISel::SelectAddrMode5(SDOperand Op, SDOperand N,
301 SDOperand &Base, SDOperand &Offset) {
302 if (N.getOpcode() != ISD::ADD) {
303 Base = N;
304 if (N.getOpcode() == ISD::FrameIndex) {
305 int FI = cast<FrameIndexSDNode>(N)->getIndex();
306 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
307 } else if (N.getOpcode() == ARMISD::Wrapper) {
308 Base = N.getOperand(0);
309 }
310 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
311 MVT::i32);
312 return true;
313 }
314
315 // If the RHS is +/- imm8, fold into addr mode.
316 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
317 int RHSC = (int)RHS->getValue();
318 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
319 RHSC >>= 2;
Evan Chenge966d642007-01-24 02:45:25 +0000320 if ((RHSC >= 0 && RHSC < 256) ||
321 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
Evan Chenga8e29892007-01-19 07:51:42 +0000322 Base = N.getOperand(0);
Evan Chenge966d642007-01-24 02:45:25 +0000323 if (Base.getOpcode() == ISD::FrameIndex) {
324 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
325 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
326 }
327
328 ARM_AM::AddrOpc AddSub = ARM_AM::add;
329 if (RHSC < 0) {
330 AddSub = ARM_AM::sub;
331 RHSC = - RHSC;
332 }
333 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
Evan Chenga8e29892007-01-19 07:51:42 +0000334 MVT::i32);
335 return true;
336 }
337 }
338 }
339
340 Base = N;
341 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
342 MVT::i32);
343 return true;
344}
345
346bool ARMDAGToDAGISel::SelectAddrModePC(SDOperand Op, SDOperand N,
347 SDOperand &Offset, SDOperand &Label) {
348 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
349 Offset = N.getOperand(0);
350 SDOperand N1 = N.getOperand(1);
351 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getValue(),
352 MVT::i32);
353 return true;
354 }
355 return false;
356}
357
358bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDOperand Op, SDOperand N,
359 SDOperand &Base, SDOperand &Offset){
Evan Chengc38f2bc2007-01-23 22:59:13 +0000360 if (N.getOpcode() != ISD::ADD) {
361 Base = N;
362 // We must materialize a zero in a reg! Returning an constant here won't
363 // work since its node is -1 so it won't get added to the selection queue.
364 // Explicitly issue a tMOVri8 node!
365 Offset = SDOperand(CurDAG->getTargetNode(ARM::tMOVri8, MVT::i32,
366 CurDAG->getTargetConstant(0, MVT::i32)), 0);
367 return true;
368 }
369
Evan Chenga8e29892007-01-19 07:51:42 +0000370 Base = N.getOperand(0);
371 Offset = N.getOperand(1);
372 return true;
373}
374
Evan Cheng79d43262007-01-24 02:21:22 +0000375bool
376ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDOperand Op, SDOperand N,
377 unsigned Scale, SDOperand &Base,
378 SDOperand &Offset, SDOperand &OffImm) {
379 if (Scale == 4) {
380 SDOperand TmpBase, TmpOffImm;
381 if (SelectThumbAddrModeSP(Op, N, TmpBase, TmpOffImm))
382 return false; // We want to select tLDRspi / tSTRspi instead.
383 }
384
Evan Chenga8e29892007-01-19 07:51:42 +0000385 if (N.getOpcode() != ISD::ADD) {
386 Base = (N.getOpcode() == ARMISD::Wrapper) ? N.getOperand(0) : N;
Evan Chengc38f2bc2007-01-23 22:59:13 +0000387 Offset = CurDAG->getRegister(0, MVT::i32);
388 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000389 return true;
390 }
391
392 // If the RHS is + imm5 * scale, fold into addr mode.
393 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
394 int RHSC = (int)RHS->getValue();
395 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
396 RHSC /= Scale;
397 if (RHSC >= 0 && RHSC < 32) {
398 Base = N.getOperand(0);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000399 Offset = CurDAG->getRegister(0, MVT::i32);
400 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000401 return true;
402 }
403 }
404 }
405
Evan Chengc38f2bc2007-01-23 22:59:13 +0000406 Base = N.getOperand(0);
407 Offset = N.getOperand(1);
408 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
409 return true;
Evan Chenga8e29892007-01-19 07:51:42 +0000410}
411
Evan Chengc38f2bc2007-01-23 22:59:13 +0000412bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDOperand Op, SDOperand N,
413 SDOperand &Base, SDOperand &Offset,
414 SDOperand &OffImm) {
Evan Cheng79d43262007-01-24 02:21:22 +0000415 return SelectThumbAddrModeRI5(Op, N, 1, Base, Offset, OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000416}
417
Evan Chengc38f2bc2007-01-23 22:59:13 +0000418bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDOperand Op, SDOperand N,
419 SDOperand &Base, SDOperand &Offset,
420 SDOperand &OffImm) {
Evan Cheng79d43262007-01-24 02:21:22 +0000421 return SelectThumbAddrModeRI5(Op, N, 2, Base, Offset, OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000422}
423
Evan Chengc38f2bc2007-01-23 22:59:13 +0000424bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDOperand Op, SDOperand N,
425 SDOperand &Base, SDOperand &Offset,
426 SDOperand &OffImm) {
Evan Cheng79d43262007-01-24 02:21:22 +0000427 return SelectThumbAddrModeRI5(Op, N, 4, Base, Offset, OffImm);
Evan Chenga8e29892007-01-19 07:51:42 +0000428}
429
430bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDOperand Op, SDOperand N,
Evan Cheng79d43262007-01-24 02:21:22 +0000431 SDOperand &Base, SDOperand &OffImm) {
Evan Chenga8e29892007-01-19 07:51:42 +0000432 if (N.getOpcode() == ISD::FrameIndex) {
433 int FI = cast<FrameIndexSDNode>(N)->getIndex();
434 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Evan Cheng79d43262007-01-24 02:21:22 +0000435 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000436 return true;
437 }
Evan Cheng79d43262007-01-24 02:21:22 +0000438
439 if (N.getOpcode() == ISD::ADD &&
440 N.getOperand(0).getOpcode() == ISD::FrameIndex) {
441 // If the RHS is + imm8 * scale, fold into addr mode.
442 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
443 int RHSC = (int)RHS->getValue();
444 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
445 RHSC >>= 2;
446 if (RHSC >= 0 && RHSC < 256) {
447 int FI = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
448 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
449 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
450 return true;
451 }
452 }
453 }
454 }
Evan Chenga8e29892007-01-19 07:51:42 +0000455
456 return false;
457}
458
459bool ARMDAGToDAGISel::SelectShifterOperandReg(SDOperand Op,
460 SDOperand N,
461 SDOperand &BaseReg,
462 SDOperand &ShReg,
463 SDOperand &Opc) {
464 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
465
466 // Don't match base register only case. That is matched to a separate
467 // lower complexity pattern with explicit register operand.
468 if (ShOpcVal == ARM_AM::no_shift) return false;
469
470 BaseReg = N.getOperand(0);
471 unsigned ShImmVal = 0;
472 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
473 ShReg = CurDAG->getRegister(0, MVT::i32);
474 ShImmVal = RHS->getValue() & 31;
475 } else {
476 ShReg = N.getOperand(1);
477 }
478 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
479 MVT::i32);
480 return true;
481}
482
483
Evan Cheng9ade2182006-08-26 05:34:46 +0000484SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000485 SDNode *N = Op.Val;
Evan Chenga8e29892007-01-19 07:51:42 +0000486 unsigned Opcode = N->getOpcode();
487
488 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < ARMISD::FIRST_NUMBER)
489 return NULL; // Already selected.
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000490
491 switch (N->getOpcode()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000492 default: break;
493 case ISD::Constant: {
494 unsigned Val = cast<ConstantSDNode>(N)->getValue();
495 bool UseCP = true;
496 if (Subtarget->isThumb())
497 UseCP = (Val > 255 && // MOV
498 ~Val > 255 && // MOV + MVN
499 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
500 else
501 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
502 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
503 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
504 if (UseCP) {
505 SDOperand CPIdx =
506 CurDAG->getTargetConstantPool(ConstantInt::get(Type::Int32Ty, Val),
507 TLI.getPointerTy());
508 SDOperand Ops[] = {
509 CPIdx,
510 CurDAG->getRegister(0, MVT::i32),
511 CurDAG->getTargetConstant(0, MVT::i32),
512 CurDAG->getEntryNode()
513 };
514 SDNode *ResNode =
515 CurDAG->getTargetNode(ARM::LDR, MVT::i32, MVT::Other, Ops, 4);
516 ReplaceUses(Op, SDOperand(ResNode, 0));
517 return NULL;
518 }
519
520 // Other cases are autogenerated.
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000521 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000522 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000523 case ISD::FrameIndex: {
Evan Chenga8e29892007-01-19 07:51:42 +0000524 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindolaf819a492006-11-09 13:58:55 +0000525 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Chenga8e29892007-01-19 07:51:42 +0000526 unsigned Opc = Subtarget->isThumb() ? ARM::tADDrSPi : ARM::ADDri;
527 SDOperand TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
528 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, TFI,
529 CurDAG->getTargetConstant(0, MVT::i32));
530 }
531 case ISD::MUL:
Evan Cheng79d43262007-01-24 02:21:22 +0000532 if (Subtarget->isThumb())
533 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000534 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
535 unsigned RHSV = C->getValue();
536 if (!RHSV) break;
537 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
538 SDOperand V = Op.getOperand(0);
539 AddToISelQueue(V);
540 unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV-1));
541 SDOperand Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
542 CurDAG->getTargetConstant(ShImm, MVT::i32)
543 };
544 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 4);
545 }
546 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
547 SDOperand V = Op.getOperand(0);
548 AddToISelQueue(V);
549 unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV+1));
550 SDOperand Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
551 CurDAG->getTargetConstant(ShImm, MVT::i32)
552 };
553 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 4);
554 }
555 }
556 break;
557 case ARMISD::FMRRD:
558 AddToISelQueue(Op.getOperand(0));
559 return CurDAG->getTargetNode(ARM::FMRRD, MVT::i32, MVT::i32,
560 Op.getOperand(0));
561 case ARMISD::MULHILOU:
562 AddToISelQueue(Op.getOperand(0));
563 AddToISelQueue(Op.getOperand(1));
564 return CurDAG->getTargetNode(ARM::UMULL, MVT::i32, MVT::i32,
565 Op.getOperand(0), Op.getOperand(1));
566 case ARMISD::MULHILOS:
567 AddToISelQueue(Op.getOperand(0));
568 AddToISelQueue(Op.getOperand(1));
569 return CurDAG->getTargetNode(ARM::SMULL, MVT::i32, MVT::i32,
570 Op.getOperand(0), Op.getOperand(1));
571 case ISD::LOAD: {
572 LoadSDNode *LD = cast<LoadSDNode>(Op);
573 ISD::MemIndexedMode AM = LD->getAddressingMode();
574 MVT::ValueType LoadedVT = LD->getLoadedVT();
575 if (AM != ISD::UNINDEXED) {
576 SDOperand Offset, AMOpc;
577 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
578 unsigned Opcode = 0;
579 bool Match = false;
580 if (LoadedVT == MVT::i32 &&
581 SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
582 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
583 Match = true;
584 } else if (LoadedVT == MVT::i16 &&
585 SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
586 Match = true;
587 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
588 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
589 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
590 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
591 if (LD->getExtensionType() == ISD::SEXTLOAD) {
592 if (SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
593 Match = true;
594 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
595 }
596 } else {
597 if (SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
598 Match = true;
599 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
600 }
601 }
602 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000603
Evan Chenga8e29892007-01-19 07:51:42 +0000604 if (Match) {
605 SDOperand Chain = LD->getChain();
606 SDOperand Base = LD->getBasePtr();
607 AddToISelQueue(Chain);
608 AddToISelQueue(Base);
609 AddToISelQueue(Offset);
610 SDOperand Ops[] = { Base, Offset, AMOpc, Chain };
611 return CurDAG->getTargetNode(Opcode, MVT::i32, MVT::i32,
612 MVT::Other, Ops, 4);
613 }
614 }
615 // Other cases are autogenerated.
Rafael Espindolaf819a492006-11-09 13:58:55 +0000616 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000617 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000618 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000619
Evan Chenga8e29892007-01-19 07:51:42 +0000620 return SelectCode(Op);
621}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000622
623/// createARMISelDag - This pass converts a legalized DAG into a
624/// ARM-specific DAG, ready for instruction scheduling.
625///
Evan Chenga8e29892007-01-19 07:51:42 +0000626FunctionPass *llvm::createARMISelDag(ARMTargetMachine &TM) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000627 return new ARMDAGToDAGISel(TM);
628}