blob: 537f6faf788a1b41797976247c92f60089e76652 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARM.h"
Evan Chenge4428082008-12-10 21:54:21 +000015#include "ARMAddressingModes.h"
16#include "ARMConstantPoolValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "ARMISelLowering.h"
18#include "ARMTargetMachine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/CallingConv.h"
20#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
23#include "llvm/Intrinsics.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/SelectionDAG.h"
28#include "llvm/CodeGen/SelectionDAGISel.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Target/TargetLowering.h"
30#include "llvm/Target/TargetOptions.h"
Chris Lattner93c741a2008-02-03 05:43:57 +000031#include "llvm/Support/Compiler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Support/Debug.h"
33using namespace llvm;
34
35//===--------------------------------------------------------------------===//
36/// ARMDAGToDAGISel - ARM specific code to select ARM machine
37/// instructions for SelectionDAG operations.
38///
39namespace {
40class ARMDAGToDAGISel : public SelectionDAGISel {
Evan Cheng0edb69d2008-09-18 07:24:33 +000041 ARMTargetMachine &TM;
42
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
44 /// make the right decision when generating code for different targets.
45 const ARMSubtarget *Subtarget;
46
47public:
Evan Cheng0edb69d2008-09-18 07:24:33 +000048 explicit ARMDAGToDAGISel(ARMTargetMachine &tm)
Dan Gohmanf2b29572008-10-03 16:55:19 +000049 : SelectionDAGISel(*tm.getTargetLowering()), TM(tm),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
51 }
52
53 virtual const char *getPassName() const {
54 return "ARM Instruction Selection";
55 }
56
Dan Gohman8181bd12008-07-27 21:46:04 +000057 SDNode *Select(SDValue Op);
Dan Gohman14a66442008-08-23 02:25:05 +000058 virtual void InstructionSelect();
Dan Gohman8181bd12008-07-27 21:46:04 +000059 bool SelectAddrMode2(SDValue Op, SDValue N, SDValue &Base,
60 SDValue &Offset, SDValue &Opc);
61 bool SelectAddrMode2Offset(SDValue Op, SDValue N,
62 SDValue &Offset, SDValue &Opc);
63 bool SelectAddrMode3(SDValue Op, SDValue N, SDValue &Base,
64 SDValue &Offset, SDValue &Opc);
65 bool SelectAddrMode3Offset(SDValue Op, SDValue N,
66 SDValue &Offset, SDValue &Opc);
67 bool SelectAddrMode5(SDValue Op, SDValue N, SDValue &Base,
68 SDValue &Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069
Dan Gohman8181bd12008-07-27 21:46:04 +000070 bool SelectAddrModePC(SDValue Op, SDValue N, SDValue &Offset,
71 SDValue &Label);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072
Dan Gohman8181bd12008-07-27 21:46:04 +000073 bool SelectThumbAddrModeRR(SDValue Op, SDValue N, SDValue &Base,
74 SDValue &Offset);
75 bool SelectThumbAddrModeRI5(SDValue Op, SDValue N, unsigned Scale,
76 SDValue &Base, SDValue &OffImm,
77 SDValue &Offset);
78 bool SelectThumbAddrModeS1(SDValue Op, SDValue N, SDValue &Base,
79 SDValue &OffImm, SDValue &Offset);
80 bool SelectThumbAddrModeS2(SDValue Op, SDValue N, SDValue &Base,
81 SDValue &OffImm, SDValue &Offset);
82 bool SelectThumbAddrModeS4(SDValue Op, SDValue N, SDValue &Base,
83 SDValue &OffImm, SDValue &Offset);
84 bool SelectThumbAddrModeSP(SDValue Op, SDValue N, SDValue &Base,
85 SDValue &OffImm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086
Dan Gohman8181bd12008-07-27 21:46:04 +000087 bool SelectShifterOperandReg(SDValue Op, SDValue N, SDValue &A,
88 SDValue &B, SDValue &C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089
90 // Include the pieces autogenerated from the target description.
91#include "ARMGenDAGISel.inc"
92};
93}
94
Dan Gohman14a66442008-08-23 02:25:05 +000095void ARMDAGToDAGISel::InstructionSelect() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 DEBUG(BB->dump());
97
David Greene932618b2008-10-27 21:56:29 +000098 SelectRoot(*CurDAG);
Dan Gohman14a66442008-08-23 02:25:05 +000099 CurDAG->RemoveDeadNodes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100}
101
Dan Gohman8181bd12008-07-27 21:46:04 +0000102bool ARMDAGToDAGISel::SelectAddrMode2(SDValue Op, SDValue N,
103 SDValue &Base, SDValue &Offset,
104 SDValue &Opc) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 if (N.getOpcode() == ISD::MUL) {
106 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
107 // X * [3,5,9] -> X + X * [2,4,8] etc.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000108 int RHSC = (int)RHS->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 if (RHSC & 1) {
110 RHSC = RHSC & ~1;
111 ARM_AM::AddrOpc AddSub = ARM_AM::add;
112 if (RHSC < 0) {
113 AddSub = ARM_AM::sub;
114 RHSC = - RHSC;
115 }
116 if (isPowerOf2_32(RHSC)) {
117 unsigned ShAmt = Log2_32(RHSC);
118 Base = Offset = N.getOperand(0);
119 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
120 ARM_AM::lsl),
121 MVT::i32);
122 return true;
123 }
124 }
125 }
126 }
127
128 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
129 Base = N;
130 if (N.getOpcode() == ISD::FrameIndex) {
131 int FI = cast<FrameIndexSDNode>(N)->getIndex();
132 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
133 } else if (N.getOpcode() == ARMISD::Wrapper) {
134 Base = N.getOperand(0);
135 }
136 Offset = CurDAG->getRegister(0, MVT::i32);
137 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
138 ARM_AM::no_shift),
139 MVT::i32);
140 return true;
141 }
142
143 // Match simple R +/- imm12 operands.
144 if (N.getOpcode() == ISD::ADD)
145 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000146 int RHSC = (int)RHS->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 if ((RHSC >= 0 && RHSC < 0x1000) ||
148 (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
149 Base = N.getOperand(0);
150 if (Base.getOpcode() == ISD::FrameIndex) {
151 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
152 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
153 }
154 Offset = CurDAG->getRegister(0, MVT::i32);
155
156 ARM_AM::AddrOpc AddSub = ARM_AM::add;
157 if (RHSC < 0) {
158 AddSub = ARM_AM::sub;
159 RHSC = - RHSC;
160 }
161 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
162 ARM_AM::no_shift),
163 MVT::i32);
164 return true;
165 }
166 }
167
168 // Otherwise this is R +/- [possibly shifted] R
169 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
170 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
171 unsigned ShAmt = 0;
172
173 Base = N.getOperand(0);
174 Offset = N.getOperand(1);
175
176 if (ShOpcVal != ARM_AM::no_shift) {
177 // Check to see if the RHS of the shift is a constant, if not, we can't fold
178 // it.
179 if (ConstantSDNode *Sh =
180 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000181 ShAmt = Sh->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 Offset = N.getOperand(1).getOperand(0);
183 } else {
184 ShOpcVal = ARM_AM::no_shift;
185 }
186 }
187
188 // Try matching (R shl C) + (R).
189 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
190 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
191 if (ShOpcVal != ARM_AM::no_shift) {
192 // Check to see if the RHS of the shift is a constant, if not, we can't
193 // fold it.
194 if (ConstantSDNode *Sh =
195 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000196 ShAmt = Sh->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 Offset = N.getOperand(0).getOperand(0);
198 Base = N.getOperand(1);
199 } else {
200 ShOpcVal = ARM_AM::no_shift;
201 }
202 }
203 }
204
205 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
206 MVT::i32);
207 return true;
208}
209
Dan Gohman8181bd12008-07-27 21:46:04 +0000210bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDValue Op, SDValue N,
211 SDValue &Offset, SDValue &Opc) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 unsigned Opcode = Op.getOpcode();
213 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
214 ? cast<LoadSDNode>(Op)->getAddressingMode()
215 : cast<StoreSDNode>(Op)->getAddressingMode();
216 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
217 ? ARM_AM::add : ARM_AM::sub;
218 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000219 int Val = (int)C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 if (Val >= 0 && Val < 0x1000) { // 12 bits.
221 Offset = CurDAG->getRegister(0, MVT::i32);
222 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
223 ARM_AM::no_shift),
224 MVT::i32);
225 return true;
226 }
227 }
228
229 Offset = N;
230 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
231 unsigned ShAmt = 0;
232 if (ShOpcVal != ARM_AM::no_shift) {
233 // Check to see if the RHS of the shift is a constant, if not, we can't fold
234 // it.
235 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000236 ShAmt = Sh->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 Offset = N.getOperand(0);
238 } else {
239 ShOpcVal = ARM_AM::no_shift;
240 }
241 }
242
243 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
244 MVT::i32);
245 return true;
246}
247
248
Dan Gohman8181bd12008-07-27 21:46:04 +0000249bool ARMDAGToDAGISel::SelectAddrMode3(SDValue Op, SDValue N,
250 SDValue &Base, SDValue &Offset,
251 SDValue &Opc) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 if (N.getOpcode() == ISD::SUB) {
253 // X - C is canonicalize to X + -C, no need to handle it here.
254 Base = N.getOperand(0);
255 Offset = N.getOperand(1);
256 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
257 return true;
258 }
259
260 if (N.getOpcode() != ISD::ADD) {
261 Base = N;
262 if (N.getOpcode() == ISD::FrameIndex) {
263 int FI = cast<FrameIndexSDNode>(N)->getIndex();
264 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
265 }
266 Offset = CurDAG->getRegister(0, MVT::i32);
267 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
268 return true;
269 }
270
271 // If the RHS is +/- imm8, fold into addr mode.
272 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000273 int RHSC = (int)RHS->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 if ((RHSC >= 0 && RHSC < 256) ||
275 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
276 Base = N.getOperand(0);
277 if (Base.getOpcode() == ISD::FrameIndex) {
278 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
279 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
280 }
281 Offset = CurDAG->getRegister(0, MVT::i32);
282
283 ARM_AM::AddrOpc AddSub = ARM_AM::add;
284 if (RHSC < 0) {
285 AddSub = ARM_AM::sub;
286 RHSC = - RHSC;
287 }
288 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
289 return true;
290 }
291 }
292
293 Base = N.getOperand(0);
294 Offset = N.getOperand(1);
295 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
296 return true;
297}
298
Dan Gohman8181bd12008-07-27 21:46:04 +0000299bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDValue Op, SDValue N,
300 SDValue &Offset, SDValue &Opc) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 unsigned Opcode = Op.getOpcode();
302 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
303 ? cast<LoadSDNode>(Op)->getAddressingMode()
304 : cast<StoreSDNode>(Op)->getAddressingMode();
305 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
306 ? ARM_AM::add : ARM_AM::sub;
307 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000308 int Val = (int)C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 if (Val >= 0 && Val < 256) {
310 Offset = CurDAG->getRegister(0, MVT::i32);
311 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
312 return true;
313 }
314 }
315
316 Offset = N;
317 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
318 return true;
319}
320
321
Dan Gohman8181bd12008-07-27 21:46:04 +0000322bool ARMDAGToDAGISel::SelectAddrMode5(SDValue Op, SDValue N,
323 SDValue &Base, SDValue &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 if (N.getOpcode() != ISD::ADD) {
325 Base = N;
326 if (N.getOpcode() == ISD::FrameIndex) {
327 int FI = cast<FrameIndexSDNode>(N)->getIndex();
328 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
329 } else if (N.getOpcode() == ARMISD::Wrapper) {
330 Base = N.getOperand(0);
331 }
332 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
333 MVT::i32);
334 return true;
335 }
336
337 // If the RHS is +/- imm8, fold into addr mode.
338 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000339 int RHSC = (int)RHS->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
341 RHSC >>= 2;
342 if ((RHSC >= 0 && RHSC < 256) ||
343 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
344 Base = N.getOperand(0);
345 if (Base.getOpcode() == ISD::FrameIndex) {
346 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
347 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
348 }
349
350 ARM_AM::AddrOpc AddSub = ARM_AM::add;
351 if (RHSC < 0) {
352 AddSub = ARM_AM::sub;
353 RHSC = - RHSC;
354 }
355 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
356 MVT::i32);
357 return true;
358 }
359 }
360 }
361
362 Base = N;
363 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
364 MVT::i32);
365 return true;
366}
367
Dan Gohman8181bd12008-07-27 21:46:04 +0000368bool ARMDAGToDAGISel::SelectAddrModePC(SDValue Op, SDValue N,
369 SDValue &Offset, SDValue &Label) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
371 Offset = N.getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000372 SDValue N1 = N.getOperand(1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000373 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 MVT::i32);
375 return true;
376 }
377 return false;
378}
379
Dan Gohman8181bd12008-07-27 21:46:04 +0000380bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue Op, SDValue N,
381 SDValue &Base, SDValue &Offset){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382 if (N.getOpcode() != ISD::ADD) {
383 Base = N;
Dan Gohman5c49fde2008-12-03 17:10:41 +0000384 // We must materialize a zero in a reg! Returning a constant here
385 // wouldn't work without additional code to position the node within
386 // ISel's topological ordering in a place where ISel will process it
387 // normally. Instead, just explicitly issue a tMOVri8 node!
Dan Gohman8181bd12008-07-27 21:46:04 +0000388 Offset = SDValue(CurDAG->getTargetNode(ARM::tMOVi8, MVT::i32,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 CurDAG->getTargetConstant(0, MVT::i32)), 0);
390 return true;
391 }
392
393 Base = N.getOperand(0);
394 Offset = N.getOperand(1);
395 return true;
396}
397
398bool
Dan Gohman8181bd12008-07-27 21:46:04 +0000399ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDValue Op, SDValue N,
400 unsigned Scale, SDValue &Base,
401 SDValue &OffImm, SDValue &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 if (Scale == 4) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000403 SDValue TmpBase, TmpOffImm;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 if (SelectThumbAddrModeSP(Op, N, TmpBase, TmpOffImm))
405 return false; // We want to select tLDRspi / tSTRspi instead.
406 if (N.getOpcode() == ARMISD::Wrapper &&
407 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
408 return false; // We want to select tLDRpci instead.
409 }
410
411 if (N.getOpcode() != ISD::ADD) {
412 Base = (N.getOpcode() == ARMISD::Wrapper) ? N.getOperand(0) : N;
413 Offset = CurDAG->getRegister(0, MVT::i32);
414 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
415 return true;
416 }
417
418 // Thumb does not have [sp, r] address mode.
419 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
420 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
421 if ((LHSR && LHSR->getReg() == ARM::SP) ||
422 (RHSR && RHSR->getReg() == ARM::SP)) {
423 Base = N;
424 Offset = CurDAG->getRegister(0, MVT::i32);
425 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
426 return true;
427 }
428
429 // If the RHS is + imm5 * scale, fold into addr mode.
430 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000431 int RHSC = (int)RHS->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
433 RHSC /= Scale;
434 if (RHSC >= 0 && RHSC < 32) {
435 Base = N.getOperand(0);
436 Offset = CurDAG->getRegister(0, MVT::i32);
437 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
438 return true;
439 }
440 }
441 }
442
443 Base = N.getOperand(0);
444 Offset = N.getOperand(1);
445 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
446 return true;
447}
448
Dan Gohman8181bd12008-07-27 21:46:04 +0000449bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDValue Op, SDValue N,
450 SDValue &Base, SDValue &OffImm,
451 SDValue &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 return SelectThumbAddrModeRI5(Op, N, 1, Base, OffImm, Offset);
453}
454
Dan Gohman8181bd12008-07-27 21:46:04 +0000455bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDValue Op, SDValue N,
456 SDValue &Base, SDValue &OffImm,
457 SDValue &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 return SelectThumbAddrModeRI5(Op, N, 2, Base, OffImm, Offset);
459}
460
Dan Gohman8181bd12008-07-27 21:46:04 +0000461bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDValue Op, SDValue N,
462 SDValue &Base, SDValue &OffImm,
463 SDValue &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 return SelectThumbAddrModeRI5(Op, N, 4, Base, OffImm, Offset);
465}
466
Dan Gohman8181bd12008-07-27 21:46:04 +0000467bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue Op, SDValue N,
468 SDValue &Base, SDValue &OffImm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 if (N.getOpcode() == ISD::FrameIndex) {
470 int FI = cast<FrameIndexSDNode>(N)->getIndex();
471 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
472 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
473 return true;
474 }
475
476 if (N.getOpcode() != ISD::ADD)
477 return false;
478
479 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
480 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
481 (LHSR && LHSR->getReg() == ARM::SP)) {
482 // If the RHS is + imm8 * scale, fold into addr mode.
483 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000484 int RHSC = (int)RHS->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
486 RHSC >>= 2;
487 if (RHSC >= 0 && RHSC < 256) {
488 Base = N.getOperand(0);
489 if (Base.getOpcode() == ISD::FrameIndex) {
490 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
491 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
492 }
493 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
494 return true;
495 }
496 }
497 }
498 }
499
500 return false;
501}
502
Dan Gohman8181bd12008-07-27 21:46:04 +0000503bool ARMDAGToDAGISel::SelectShifterOperandReg(SDValue Op,
504 SDValue N,
505 SDValue &BaseReg,
506 SDValue &ShReg,
507 SDValue &Opc) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
509
510 // Don't match base register only case. That is matched to a separate
511 // lower complexity pattern with explicit register operand.
512 if (ShOpcVal == ARM_AM::no_shift) return false;
513
514 BaseReg = N.getOperand(0);
515 unsigned ShImmVal = 0;
516 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
517 ShReg = CurDAG->getRegister(0, MVT::i32);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000518 ShImmVal = RHS->getZExtValue() & 31;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 } else {
520 ShReg = N.getOperand(1);
521 }
522 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
523 MVT::i32);
524 return true;
525}
526
527/// getAL - Returns a ARMCC::AL immediate node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000528static inline SDValue getAL(SelectionDAG *CurDAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
530}
531
532
Dan Gohman8181bd12008-07-27 21:46:04 +0000533SDNode *ARMDAGToDAGISel::Select(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000534 SDNode *N = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535
Dan Gohmanbd68c792008-07-17 19:10:17 +0000536 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 return NULL; // Already selected.
538
539 switch (N->getOpcode()) {
540 default: break;
541 case ISD::Constant: {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000542 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 bool UseCP = true;
544 if (Subtarget->isThumb())
545 UseCP = (Val > 255 && // MOV
546 ~Val > 255 && // MOV + MVN
547 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
548 else
549 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
550 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
551 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
552 if (UseCP) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000553 SDValue CPIdx =
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 CurDAG->getTargetConstantPool(ConstantInt::get(Type::Int32Ty, Val),
555 TLI.getPointerTy());
556
557 SDNode *ResNode;
558 if (Subtarget->isThumb())
559 ResNode = CurDAG->getTargetNode(ARM::tLDRcp, MVT::i32, MVT::Other,
560 CPIdx, CurDAG->getEntryNode());
561 else {
Dan Gohman8181bd12008-07-27 21:46:04 +0000562 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 CPIdx,
564 CurDAG->getRegister(0, MVT::i32),
565 CurDAG->getTargetConstant(0, MVT::i32),
566 getAL(CurDAG),
567 CurDAG->getRegister(0, MVT::i32),
568 CurDAG->getEntryNode()
569 };
570 ResNode=CurDAG->getTargetNode(ARM::LDRcp, MVT::i32, MVT::Other, Ops, 6);
571 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000572 ReplaceUses(Op, SDValue(ResNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 return NULL;
574 }
575
576 // Other cases are autogenerated.
577 break;
578 }
579 case ISD::FrameIndex: {
580 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
581 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohman8181bd12008-07-27 21:46:04 +0000582 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 if (Subtarget->isThumb())
584 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
585 CurDAG->getTargetConstant(0, MVT::i32));
586 else {
Dan Gohman8181bd12008-07-27 21:46:04 +0000587 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
589 CurDAG->getRegister(0, MVT::i32) };
590 return CurDAG->SelectNodeTo(N, ARM::ADDri, MVT::i32, Ops, 5);
591 }
592 }
593 case ISD::ADD: {
594 // Select add sp, c to tADDhirr.
Dan Gohman8181bd12008-07-27 21:46:04 +0000595 SDValue N0 = Op.getOperand(0);
596 SDValue N1 = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(Op.getOperand(0));
598 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(Op.getOperand(1));
599 if (LHSR && LHSR->getReg() == ARM::SP) {
600 std::swap(N0, N1);
601 std::swap(LHSR, RHSR);
602 }
603 if (RHSR && RHSR->getReg() == ARM::SP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 return CurDAG->SelectNodeTo(N, ARM::tADDhirr, Op.getValueType(), N0, N1);
605 }
606 break;
607 }
608 case ISD::MUL:
609 if (Subtarget->isThumb())
610 break;
611 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000612 unsigned RHSV = C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613 if (!RHSV) break;
614 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
Dan Gohman8181bd12008-07-27 21:46:04 +0000615 SDValue V = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV-1));
Dan Gohman8181bd12008-07-27 21:46:04 +0000617 SDValue Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 CurDAG->getTargetConstant(ShImm, MVT::i32),
619 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
620 CurDAG->getRegister(0, MVT::i32) };
621 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
622 }
623 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
Dan Gohman8181bd12008-07-27 21:46:04 +0000624 SDValue V = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV+1));
Dan Gohman8181bd12008-07-27 21:46:04 +0000626 SDValue Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 CurDAG->getTargetConstant(ShImm, MVT::i32),
628 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
629 CurDAG->getRegister(0, MVT::i32) };
630 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
631 }
632 }
633 break;
634 case ARMISD::FMRRD:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 return CurDAG->getTargetNode(ARM::FMRRD, MVT::i32, MVT::i32,
636 Op.getOperand(0), getAL(CurDAG),
637 CurDAG->getRegister(0, MVT::i32));
Dan Gohman5a199552007-10-08 18:33:35 +0000638 case ISD::UMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000639 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
641 CurDAG->getRegister(0, MVT::i32) };
642 return CurDAG->getTargetNode(ARM::UMULL, MVT::i32, MVT::i32, Ops, 5);
643 }
Dan Gohman5a199552007-10-08 18:33:35 +0000644 case ISD::SMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000645 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
647 CurDAG->getRegister(0, MVT::i32) };
648 return CurDAG->getTargetNode(ARM::SMULL, MVT::i32, MVT::i32, Ops, 5);
649 }
650 case ISD::LOAD: {
651 LoadSDNode *LD = cast<LoadSDNode>(Op);
652 ISD::MemIndexedMode AM = LD->getAddressingMode();
Duncan Sands92c43912008-06-06 12:08:01 +0000653 MVT LoadedVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 if (AM != ISD::UNINDEXED) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000655 SDValue Offset, AMOpc;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
657 unsigned Opcode = 0;
658 bool Match = false;
659 if (LoadedVT == MVT::i32 &&
660 SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
661 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
662 Match = true;
663 } else if (LoadedVT == MVT::i16 &&
664 SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
665 Match = true;
666 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
667 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
668 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
669 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
670 if (LD->getExtensionType() == ISD::SEXTLOAD) {
671 if (SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
672 Match = true;
673 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
674 }
675 } else {
676 if (SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
677 Match = true;
678 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
679 }
680 }
681 }
682
683 if (Match) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000684 SDValue Chain = LD->getChain();
685 SDValue Base = LD->getBasePtr();
Dan Gohman8181bd12008-07-27 21:46:04 +0000686 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 CurDAG->getRegister(0, MVT::i32), Chain };
688 return CurDAG->getTargetNode(Opcode, MVT::i32, MVT::i32,
689 MVT::Other, Ops, 6);
690 }
691 }
692 // Other cases are autogenerated.
693 break;
694 }
695 case ARMISD::BRCOND: {
696 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
697 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
698 // Pattern complexity = 6 cost = 1 size = 0
699
700 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
701 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
702 // Pattern complexity = 6 cost = 1 size = 0
703
704 unsigned Opc = Subtarget->isThumb() ? ARM::tBcc : ARM::Bcc;
Dan Gohman8181bd12008-07-27 21:46:04 +0000705 SDValue Chain = Op.getOperand(0);
706 SDValue N1 = Op.getOperand(1);
707 SDValue N2 = Op.getOperand(2);
708 SDValue N3 = Op.getOperand(3);
709 SDValue InFlag = Op.getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 assert(N1.getOpcode() == ISD::BasicBlock);
711 assert(N2.getOpcode() == ISD::Constant);
712 assert(N3.getOpcode() == ISD::Register);
713
Dan Gohman8181bd12008-07-27 21:46:04 +0000714 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000715 cast<ConstantSDNode>(N2)->getZExtValue()),
716 MVT::i32);
Dan Gohman8181bd12008-07-27 21:46:04 +0000717 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 SDNode *ResNode = CurDAG->getTargetNode(Opc, MVT::Other, MVT::Flag, Ops, 5);
Dan Gohman8181bd12008-07-27 21:46:04 +0000719 Chain = SDValue(ResNode, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000720 if (Op.getNode()->getNumValues() == 2) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000721 InFlag = SDValue(ResNode, 1);
Gabor Greif1c80d112008-08-28 21:40:38 +0000722 ReplaceUses(SDValue(Op.getNode(), 1), InFlag);
Chris Lattnerb9843c72008-02-03 03:20:59 +0000723 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000724 ReplaceUses(SDValue(Op.getNode(), 0), SDValue(Chain.getNode(), Chain.getResNo()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 return NULL;
726 }
727 case ARMISD::CMOV: {
728 bool isThumb = Subtarget->isThumb();
Duncan Sands92c43912008-06-06 12:08:01 +0000729 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +0000730 SDValue N0 = Op.getOperand(0);
731 SDValue N1 = Op.getOperand(1);
732 SDValue N2 = Op.getOperand(2);
733 SDValue N3 = Op.getOperand(3);
734 SDValue InFlag = Op.getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735 assert(N2.getOpcode() == ISD::Constant);
736 assert(N3.getOpcode() == ISD::Register);
737
738 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
739 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
740 // Pattern complexity = 18 cost = 1 size = 0
Dan Gohman8181bd12008-07-27 21:46:04 +0000741 SDValue CPTmp0;
742 SDValue CPTmp1;
743 SDValue CPTmp2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 if (!isThumb && VT == MVT::i32 &&
745 SelectShifterOperandReg(Op, N1, CPTmp0, CPTmp1, CPTmp2)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000746 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000747 cast<ConstantSDNode>(N2)->getZExtValue()),
748 MVT::i32);
Dan Gohman8181bd12008-07-27 21:46:04 +0000749 SDValue Ops[] = { N0, CPTmp0, CPTmp1, CPTmp2, Tmp2, N3, InFlag };
Gabor Greif1c80d112008-08-28 21:40:38 +0000750 return CurDAG->SelectNodeTo(Op.getNode(), ARM::MOVCCs, MVT::i32, Ops, 7);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751 }
752
753 // Pattern: (ARMcmov:i32 GPR:i32:$false,
754 // (imm:i32)<<P:Predicate_so_imm>><<X:so_imm_XFORM>>:$true,
755 // (imm:i32):$cc)
756 // Emits: (MOVCCi:i32 GPR:i32:$false,
757 // (so_imm_XFORM:i32 (imm:i32):$true), (imm:i32):$cc)
758 // Pattern complexity = 10 cost = 1 size = 0
759 if (VT == MVT::i32 &&
760 N3.getOpcode() == ISD::Constant &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000761 Predicate_so_imm(N3.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000762 SDValue Tmp1 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000763 cast<ConstantSDNode>(N1)->getZExtValue()),
764 MVT::i32);
Gabor Greif1c80d112008-08-28 21:40:38 +0000765 Tmp1 = Transform_so_imm_XFORM(Tmp1.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +0000766 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000767 cast<ConstantSDNode>(N2)->getZExtValue()),
768 MVT::i32);
Dan Gohman8181bd12008-07-27 21:46:04 +0000769 SDValue Ops[] = { N0, Tmp1, Tmp2, N3, InFlag };
Gabor Greif1c80d112008-08-28 21:40:38 +0000770 return CurDAG->SelectNodeTo(Op.getNode(), ARM::MOVCCi, MVT::i32, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771 }
772
773 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
774 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
775 // Pattern complexity = 6 cost = 1 size = 0
776 //
777 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
778 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
779 // Pattern complexity = 6 cost = 11 size = 0
780 //
781 // Also FCPYScc and FCPYDcc.
Dan Gohman8181bd12008-07-27 21:46:04 +0000782 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000783 cast<ConstantSDNode>(N2)->getZExtValue()),
784 MVT::i32);
Dan Gohman8181bd12008-07-27 21:46:04 +0000785 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786 unsigned Opc = 0;
Duncan Sands92c43912008-06-06 12:08:01 +0000787 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788 default: assert(false && "Illegal conditional move type!");
789 break;
790 case MVT::i32:
791 Opc = isThumb ? ARM::tMOVCCr : ARM::MOVCCr;
792 break;
793 case MVT::f32:
794 Opc = ARM::FCPYScc;
795 break;
796 case MVT::f64:
797 Opc = ARM::FCPYDcc;
798 break;
799 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000800 return CurDAG->SelectNodeTo(Op.getNode(), Opc, VT, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 }
802 case ARMISD::CNEG: {
Duncan Sands92c43912008-06-06 12:08:01 +0000803 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +0000804 SDValue N0 = Op.getOperand(0);
805 SDValue N1 = Op.getOperand(1);
806 SDValue N2 = Op.getOperand(2);
807 SDValue N3 = Op.getOperand(3);
808 SDValue InFlag = Op.getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 assert(N2.getOpcode() == ISD::Constant);
810 assert(N3.getOpcode() == ISD::Register);
811
Dan Gohman8181bd12008-07-27 21:46:04 +0000812 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000813 cast<ConstantSDNode>(N2)->getZExtValue()),
814 MVT::i32);
Dan Gohman8181bd12008-07-27 21:46:04 +0000815 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 unsigned Opc = 0;
Duncan Sands92c43912008-06-06 12:08:01 +0000817 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 default: assert(false && "Illegal conditional move type!");
819 break;
820 case MVT::f32:
821 Opc = ARM::FNEGScc;
822 break;
823 case MVT::f64:
824 Opc = ARM::FNEGDcc;
Evan Chenge4428082008-12-10 21:54:21 +0000825 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000827 return CurDAG->SelectNodeTo(Op.getNode(), Opc, VT, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 }
Evan Chenge4428082008-12-10 21:54:21 +0000829
830 case ISD::DECLARE: {
831 SDValue Chain = Op.getOperand(0);
832 SDValue N1 = Op.getOperand(1);
833 SDValue N2 = Op.getOperand(2);
834 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
835 if (!FINode)
836 break;
837 if (N2.getOpcode() == ARMISD::PIC_ADD && isa<LoadSDNode>(N2.getOperand(0)))
838 N2 = N2.getOperand(0);
839 LoadSDNode *Ld = dyn_cast<LoadSDNode>(N2);
840 if (!Ld)
841 break;
842 SDValue BasePtr = Ld->getBasePtr();
843 assert(BasePtr.getOpcode() == ARMISD::Wrapper &&
844 isa<ConstantPoolSDNode>(BasePtr.getOperand(0)) &&
845 "llvm.dbg.variable should be a constantpool node");
846 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(BasePtr.getOperand(0));
847 GlobalValue *GV = 0;
848 if (CP->isMachineConstantPoolEntry()) {
849 ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)CP->getMachineCPVal();
850 GV = ACPV->getGV();
851 } else
852 GV = dyn_cast<GlobalValue>(CP->getConstVal());
853 if (GV) {
854 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
855 TLI.getPointerTy());
856 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
857 SDValue Ops[] = { Tmp1, Tmp2, Chain };
858 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
859 MVT::Other, Ops, 3);
860 }
861 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 }
Evan Chenge4428082008-12-10 21:54:21 +0000863 }
864
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865 return SelectCode(Op);
866}
867
868/// createARMISelDag - This pass converts a legalized DAG into a
869/// ARM-specific DAG, ready for instruction scheduling.
870///
871FunctionPass *llvm::createARMISelDag(ARMTargetMachine &TM) {
872 return new ARMDAGToDAGISel(TM);
873}