blob: 262c856dd104d3def01f776473439e96f669bdfc [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,
Evan Chengcea117d2007-01-30 02:35:32 +000075 SDOperand &Base, SDOperand &OffImm,
76 SDOperand &Offset);
Evan Chengc38f2bc2007-01-23 22:59:13 +000077 bool SelectThumbAddrModeS1(SDOperand Op, SDOperand N, SDOperand &Base,
Evan Chengcea117d2007-01-30 02:35:32 +000078 SDOperand &OffImm, SDOperand &Offset);
Evan Chengc38f2bc2007-01-23 22:59:13 +000079 bool SelectThumbAddrModeS2(SDOperand Op, SDOperand N, SDOperand &Base,
Evan Chengcea117d2007-01-30 02:35:32 +000080 SDOperand &OffImm, SDOperand &Offset);
Evan Chengc38f2bc2007-01-23 22:59:13 +000081 bool SelectThumbAddrModeS4(SDOperand Op, SDOperand N, SDOperand &Base,
Evan Chengcea117d2007-01-30 02:35:32 +000082 SDOperand &OffImm, SDOperand &Offset);
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,
Evan Chengcea117d2007-01-30 02:35:32 +0000378 SDOperand &OffImm, SDOperand &Offset) {
Evan Cheng79d43262007-01-24 02:21:22 +0000379 if (Scale == 4) {
380 SDOperand TmpBase, TmpOffImm;
381 if (SelectThumbAddrModeSP(Op, N, TmpBase, TmpOffImm))
382 return false; // We want to select tLDRspi / tSTRspi instead.
Evan Cheng012f2d92007-01-24 08:53:17 +0000383 if (N.getOpcode() == ARMISD::Wrapper &&
384 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
385 return false; // We want to select tLDRpci instead.
Evan Cheng79d43262007-01-24 02:21:22 +0000386 }
387
Evan Chenga8e29892007-01-19 07:51:42 +0000388 if (N.getOpcode() != ISD::ADD) {
389 Base = (N.getOpcode() == ARMISD::Wrapper) ? N.getOperand(0) : N;
Evan Chengc38f2bc2007-01-23 22:59:13 +0000390 Offset = CurDAG->getRegister(0, MVT::i32);
391 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000392 return true;
393 }
394
395 // If the RHS is + imm5 * scale, fold into addr mode.
396 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
397 int RHSC = (int)RHS->getValue();
398 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
399 RHSC /= Scale;
400 if (RHSC >= 0 && RHSC < 32) {
401 Base = N.getOperand(0);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000402 Offset = CurDAG->getRegister(0, MVT::i32);
403 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000404 return true;
405 }
406 }
407 }
408
Evan Chengc38f2bc2007-01-23 22:59:13 +0000409 Base = N.getOperand(0);
410 Offset = N.getOperand(1);
411 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
412 return true;
Evan Chenga8e29892007-01-19 07:51:42 +0000413}
414
Evan Chengc38f2bc2007-01-23 22:59:13 +0000415bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDOperand Op, SDOperand N,
Evan Chengcea117d2007-01-30 02:35:32 +0000416 SDOperand &Base, SDOperand &OffImm,
417 SDOperand &Offset) {
418 return SelectThumbAddrModeRI5(Op, N, 1, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000419}
420
Evan Chengc38f2bc2007-01-23 22:59:13 +0000421bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDOperand Op, SDOperand N,
Evan Chengcea117d2007-01-30 02:35:32 +0000422 SDOperand &Base, SDOperand &OffImm,
423 SDOperand &Offset) {
424 return SelectThumbAddrModeRI5(Op, N, 2, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000425}
426
Evan Chengc38f2bc2007-01-23 22:59:13 +0000427bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDOperand Op, SDOperand N,
Evan Chengcea117d2007-01-30 02:35:32 +0000428 SDOperand &Base, SDOperand &OffImm,
429 SDOperand &Offset) {
430 return SelectThumbAddrModeRI5(Op, N, 4, Base, OffImm, Offset);
Evan Chenga8e29892007-01-19 07:51:42 +0000431}
432
433bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDOperand Op, SDOperand N,
Evan Cheng79d43262007-01-24 02:21:22 +0000434 SDOperand &Base, SDOperand &OffImm) {
Evan Chenga8e29892007-01-19 07:51:42 +0000435 if (N.getOpcode() == ISD::FrameIndex) {
436 int FI = cast<FrameIndexSDNode>(N)->getIndex();
437 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Evan Cheng79d43262007-01-24 02:21:22 +0000438 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000439 return true;
440 }
Evan Cheng79d43262007-01-24 02:21:22 +0000441
442 if (N.getOpcode() == ISD::ADD &&
443 N.getOperand(0).getOpcode() == ISD::FrameIndex) {
444 // If the RHS is + imm8 * scale, fold into addr mode.
445 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
446 int RHSC = (int)RHS->getValue();
447 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
448 RHSC >>= 2;
449 if (RHSC >= 0 && RHSC < 256) {
450 int FI = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
451 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
452 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
453 return true;
454 }
455 }
456 }
457 }
Evan Chenga8e29892007-01-19 07:51:42 +0000458
459 return false;
460}
461
462bool ARMDAGToDAGISel::SelectShifterOperandReg(SDOperand Op,
463 SDOperand N,
464 SDOperand &BaseReg,
465 SDOperand &ShReg,
466 SDOperand &Opc) {
467 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
468
469 // Don't match base register only case. That is matched to a separate
470 // lower complexity pattern with explicit register operand.
471 if (ShOpcVal == ARM_AM::no_shift) return false;
472
473 BaseReg = N.getOperand(0);
474 unsigned ShImmVal = 0;
475 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
476 ShReg = CurDAG->getRegister(0, MVT::i32);
477 ShImmVal = RHS->getValue() & 31;
478 } else {
479 ShReg = N.getOperand(1);
480 }
481 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
482 MVT::i32);
483 return true;
484}
485
486
Evan Cheng9ade2182006-08-26 05:34:46 +0000487SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000488 SDNode *N = Op.Val;
Evan Chenga8e29892007-01-19 07:51:42 +0000489 unsigned Opcode = N->getOpcode();
490
491 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < ARMISD::FIRST_NUMBER)
492 return NULL; // Already selected.
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000493
494 switch (N->getOpcode()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000495 default: break;
496 case ISD::Constant: {
497 unsigned Val = cast<ConstantSDNode>(N)->getValue();
498 bool UseCP = true;
499 if (Subtarget->isThumb())
500 UseCP = (Val > 255 && // MOV
501 ~Val > 255 && // MOV + MVN
502 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
503 else
504 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
505 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
506 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
507 if (UseCP) {
508 SDOperand CPIdx =
509 CurDAG->getTargetConstantPool(ConstantInt::get(Type::Int32Ty, Val),
510 TLI.getPointerTy());
Evan Cheng012f2d92007-01-24 08:53:17 +0000511
512 SDNode *ResNode;
513 if (Subtarget->isThumb())
514 ResNode = CurDAG->getTargetNode(ARM::tLDRpci, MVT::i32, MVT::Other,
515 CPIdx, CurDAG->getEntryNode());
516 else {
517 SDOperand Ops[] = {
518 CPIdx,
519 CurDAG->getRegister(0, MVT::i32),
520 CurDAG->getTargetConstant(0, MVT::i32),
521 CurDAG->getEntryNode()
522 };
523 ResNode = CurDAG->getTargetNode(ARM::LDR, MVT::i32, MVT::Other, Ops, 4);
524 }
Evan Chenga8e29892007-01-19 07:51:42 +0000525 ReplaceUses(Op, SDOperand(ResNode, 0));
526 return NULL;
527 }
528
529 // Other cases are autogenerated.
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000530 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000531 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000532 case ISD::FrameIndex: {
Evan Chenga8e29892007-01-19 07:51:42 +0000533 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindolaf819a492006-11-09 13:58:55 +0000534 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Chenga8e29892007-01-19 07:51:42 +0000535 unsigned Opc = Subtarget->isThumb() ? ARM::tADDrSPi : ARM::ADDri;
536 SDOperand TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
537 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, TFI,
538 CurDAG->getTargetConstant(0, MVT::i32));
539 }
540 case ISD::MUL:
Evan Cheng79d43262007-01-24 02:21:22 +0000541 if (Subtarget->isThumb())
542 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000543 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
544 unsigned RHSV = C->getValue();
545 if (!RHSV) break;
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::ADDrs, MVT::i32, Ops, 4);
554 }
555 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
556 SDOperand V = Op.getOperand(0);
557 AddToISelQueue(V);
558 unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV+1));
559 SDOperand Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
560 CurDAG->getTargetConstant(ShImm, MVT::i32)
561 };
562 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 4);
563 }
564 }
565 break;
566 case ARMISD::FMRRD:
567 AddToISelQueue(Op.getOperand(0));
568 return CurDAG->getTargetNode(ARM::FMRRD, MVT::i32, MVT::i32,
569 Op.getOperand(0));
570 case ARMISD::MULHILOU:
571 AddToISelQueue(Op.getOperand(0));
572 AddToISelQueue(Op.getOperand(1));
573 return CurDAG->getTargetNode(ARM::UMULL, MVT::i32, MVT::i32,
574 Op.getOperand(0), Op.getOperand(1));
575 case ARMISD::MULHILOS:
576 AddToISelQueue(Op.getOperand(0));
577 AddToISelQueue(Op.getOperand(1));
578 return CurDAG->getTargetNode(ARM::SMULL, MVT::i32, MVT::i32,
579 Op.getOperand(0), Op.getOperand(1));
580 case ISD::LOAD: {
581 LoadSDNode *LD = cast<LoadSDNode>(Op);
582 ISD::MemIndexedMode AM = LD->getAddressingMode();
583 MVT::ValueType LoadedVT = LD->getLoadedVT();
584 if (AM != ISD::UNINDEXED) {
585 SDOperand Offset, AMOpc;
586 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
587 unsigned Opcode = 0;
588 bool Match = false;
589 if (LoadedVT == MVT::i32 &&
590 SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
591 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
592 Match = true;
593 } else if (LoadedVT == MVT::i16 &&
594 SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
595 Match = true;
596 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
597 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
598 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
599 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
600 if (LD->getExtensionType() == ISD::SEXTLOAD) {
601 if (SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
602 Match = true;
603 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
604 }
605 } else {
606 if (SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
607 Match = true;
608 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
609 }
610 }
611 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000612
Evan Chenga8e29892007-01-19 07:51:42 +0000613 if (Match) {
614 SDOperand Chain = LD->getChain();
615 SDOperand Base = LD->getBasePtr();
616 AddToISelQueue(Chain);
617 AddToISelQueue(Base);
618 AddToISelQueue(Offset);
619 SDOperand Ops[] = { Base, Offset, AMOpc, Chain };
620 return CurDAG->getTargetNode(Opcode, MVT::i32, MVT::i32,
621 MVT::Other, Ops, 4);
622 }
623 }
624 // Other cases are autogenerated.
Rafael Espindolaf819a492006-11-09 13:58:55 +0000625 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000626 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000627 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000628
Evan Chenga8e29892007-01-19 07:51:42 +0000629 return SelectCode(Op);
630}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000631
632/// createARMISelDag - This pass converts a legalized DAG into a
633/// ARM-specific DAG, ready for instruction scheduling.
634///
Evan Chenga8e29892007-01-19 07:51:42 +0000635FunctionPass *llvm::createARMISelDag(ARMTargetMachine &TM) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000636 return new ARMDAGToDAGISel(TM);
637}