blob: eea794fcd94ac3847867a33d5b213f0f957eeeed [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"
15#include "ARMTargetMachine.h"
Rafael Espindola84b19be2006-07-16 01:02:57 +000016#include "llvm/CallingConv.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Intrinsics.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/Debug.h"
28#include <iostream>
Evan Cheng2ef88a02006-08-07 22:28:20 +000029#include <queue>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000030#include <set>
31using namespace llvm;
32
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000033namespace {
34 class ARMTargetLowering : public TargetLowering {
35 public:
36 ARMTargetLowering(TargetMachine &TM);
37 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Rafael Espindola84b19be2006-07-16 01:02:57 +000038 virtual const char *getTargetNodeName(unsigned Opcode) const;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000039 };
40
41}
42
43ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
44 : TargetLowering(TM) {
Rafael Espindola3717ca92006-08-20 01:49:49 +000045 addRegisterClass(MVT::i32, ARM::IntRegsRegisterClass);
46
47 //LLVM requires that a register class supports MVT::f64!
48 addRegisterClass(MVT::f64, ARM::IntRegsRegisterClass);
49
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000050 setOperationAction(ISD::RET, MVT::Other, Custom);
51 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
52 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000053
Rafael Espindola3c000bf2006-08-21 22:00:32 +000054 setOperationAction(ISD::SETCC, MVT::i32, Expand);
55 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindola687bc492006-08-24 13:45:55 +000056 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000057
Rafael Espindola341b8642006-08-04 12:48:42 +000058 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +000059 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000060}
61
Rafael Espindola84b19be2006-07-16 01:02:57 +000062namespace llvm {
63 namespace ARMISD {
64 enum NodeType {
65 // Start the numbering where the builting ops and target ops leave off.
66 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
67 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000068 CALL,
69
70 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +000071 RET_FLAG,
72
73 CMP,
74
Rafael Espindola687bc492006-08-24 13:45:55 +000075 SELECT,
76
77 BR
Rafael Espindola84b19be2006-07-16 01:02:57 +000078 };
79 }
80}
81
82const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
83 switch (Opcode) {
84 default: return 0;
85 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +000086 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +000087 case ARMISD::SELECT: return "ARMISD::SELECT";
88 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +000089 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola84b19be2006-07-16 01:02:57 +000090 }
91}
92
93// This transforms a ISD::CALL node into a
94// callseq_star <- ARMISD:CALL <- callseq_end
95// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +000096static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +000097 SDOperand Chain = Op.getOperand(0);
98 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
99 assert(CallConv == CallingConv::C && "unknown calling convention");
100 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000101 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
102 assert(isTailCall == false && "tail call not supported");
103 SDOperand Callee = Op.getOperand(4);
104 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000105
Rafael Espindolaec46ea32006-08-16 14:43:33 +0000106 // Count how many bytes are to be pushed on the stack.
107 unsigned NumBytes = 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000108
Rafael Espindola1a009462006-08-08 13:02:29 +0000109 // Add up all the space actually used.
110 for (unsigned i = 4; i < NumOps; ++i)
111 NumBytes += MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000112
Rafael Espindola84b19be2006-07-16 01:02:57 +0000113 // Adjust the stack pointer for the new arguments...
114 // These operations are automatically eliminated by the prolog/epilog pass
115 Chain = DAG.getCALLSEQ_START(Chain,
116 DAG.getConstant(NumBytes, MVT::i32));
117
Rafael Espindola1a009462006-08-08 13:02:29 +0000118 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
119
120 static const unsigned int num_regs = 4;
121 static const unsigned regs[num_regs] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000122 ARM::R0, ARM::R1, ARM::R2, ARM::R3
123 };
124
125 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
Rafael Espindola1a009462006-08-08 13:02:29 +0000126 std::vector<SDOperand> MemOpChains;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000127
128 for (unsigned i = 0; i != NumOps; ++i) {
129 SDOperand Arg = Op.getOperand(5+2*i);
Rafael Espindola1a009462006-08-08 13:02:29 +0000130 assert(Arg.getValueType() == MVT::i32);
131 if (i < num_regs)
132 RegsToPass.push_back(std::make_pair(regs[i], Arg));
133 else {
134 unsigned ArgOffset = (i - num_regs) * 4;
135 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
136 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
137 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
138 Arg, PtrOff, DAG.getSrcValue(NULL)));
139 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000140 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000141 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000142 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
143 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000144
145 // Build a sequence of copy-to-reg nodes chained together with token chain
146 // and flag operands which copy the outgoing args into the appropriate regs.
147 SDOperand InFlag;
148 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
149 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
150 InFlag);
151 InFlag = Chain.getValue(1);
152 }
153
Rafael Espindola84b19be2006-07-16 01:02:57 +0000154 std::vector<MVT::ValueType> NodeTys;
155 NodeTys.push_back(MVT::Other); // Returns a chain
156 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
157
158 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
159 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
160 // node so that legalize doesn't hack it.
161 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
162 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
163
164 // If this is a direct call, pass the chain and the callee.
165 assert (Callee.Val);
166 std::vector<SDOperand> Ops;
167 Ops.push_back(Chain);
168 Ops.push_back(Callee);
169
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000170 // Add argument registers to the end of the list so that they are known live
171 // into the call.
172 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
173 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
174 RegsToPass[i].second.getValueType()));
175
Rafael Espindola84b19be2006-07-16 01:02:57 +0000176 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000177 if (InFlag.Val)
178 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000179 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000180 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000181
Rafael Espindolafac00a92006-07-25 20:17:20 +0000182 std::vector<SDOperand> ResultVals;
183 NodeTys.clear();
184
185 // If the call has results, copy the values out of the ret val registers.
186 switch (Op.Val->getValueType(0)) {
187 default: assert(0 && "Unexpected ret value!");
188 case MVT::Other:
189 break;
190 case MVT::i32:
191 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
192 ResultVals.push_back(Chain.getValue(0));
193 NodeTys.push_back(MVT::i32);
194 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000195
196 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
197 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000198 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000199
Rafael Espindolafac00a92006-07-25 20:17:20 +0000200 if (ResultVals.empty())
201 return Chain;
202
203 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000204 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
205 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000206 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000207}
208
209static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
210 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000211 SDOperand Chain = Op.getOperand(0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000212 switch(Op.getNumOperands()) {
213 default:
214 assert(0 && "Do not know how to return this many arguments!");
215 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000216 case 1: {
217 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000218 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000219 }
Evan Cheng6848be12006-05-26 23:10:12 +0000220 case 3:
Rafael Espindola4b023672006-06-05 22:26:14 +0000221 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op.getOperand(1), SDOperand());
222 if (DAG.getMachineFunction().liveout_empty())
223 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000224 break;
225 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000226
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000227 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
228 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000229}
230
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000231static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
232 unsigned ArgNo) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000233 MachineFunction &MF = DAG.getMachineFunction();
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000234 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
235 assert (ObjectVT == MVT::i32);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000236 SDOperand Root = Op.getOperand(0);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000237 SSARegMap *RegMap = MF.getSSARegMap();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000238
Rafael Espindola4b442b52006-05-23 02:48:20 +0000239 unsigned num_regs = 4;
Rafael Espindola4b442b52006-05-23 02:48:20 +0000240 static const unsigned REGS[] = {
241 ARM::R0, ARM::R1, ARM::R2, ARM::R3
242 };
243
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000244 if(ArgNo < num_regs) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000245 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000246 MF.addLiveIn(REGS[ArgNo], VReg);
247 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
248 } else {
249 // If the argument is actually used, emit a load from the right stack
250 // slot.
251 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000252 unsigned ArgOffset = (ArgNo - num_regs) * 4;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000253
254 MachineFrameInfo *MFI = MF.getFrameInfo();
255 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
256 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
257 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
258 return DAG.getLoad(ObjectVT, Root, FIN,
259 DAG.getSrcValue(NULL));
260 } else {
261 // Don't emit a dead load.
262 return DAG.getNode(ISD::UNDEF, ObjectVT);
263 }
264 }
265}
266
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000267static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
268 MVT::ValueType PtrVT = Op.getValueType();
269 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
270 Constant *C = CP->get();
271 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
272
273 return CPI;
274}
275
276static SDOperand LowerGlobalAddress(SDOperand Op,
277 SelectionDAG &DAG) {
278 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000279 int alignment = 2;
280 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000281 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
282 DAG.getSrcValue(NULL));
283}
284
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000285static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
286 std::vector<SDOperand> ArgValues;
287 SDOperand Root = Op.getOperand(0);
288
289 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) {
290 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, ArgNo);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000291
292 ArgValues.push_back(ArgVal);
293 }
294
295 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
296 assert(!isVarArg);
297
298 ArgValues.push_back(Root);
299
300 // Return the new list of results.
301 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
302 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000303 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000304}
305
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000306static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
307 SDOperand LHS = Op.getOperand(0);
308 SDOperand RHS = Op.getOperand(1);
309 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
310 SDOperand TrueVal = Op.getOperand(2);
311 SDOperand FalseVal = Op.getOperand(3);
312
313 assert(CC == ISD::SETEQ);
314
315 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
316 return DAG.getNode(ARMISD::SELECT, MVT::i32, FalseVal, TrueVal, Cmp);
317}
318
Rafael Espindola687bc492006-08-24 13:45:55 +0000319static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
320 SDOperand Chain = Op.getOperand(0);
321 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
322 SDOperand LHS = Op.getOperand(2);
323 SDOperand RHS = Op.getOperand(3);
324 SDOperand Dest = Op.getOperand(4);
325
326 assert(CC == ISD::SETNE);
327
328 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
329 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, Cmp);
330}
331
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000332SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
333 switch (Op.getOpcode()) {
334 default:
335 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000336 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000337 case ISD::ConstantPool:
338 return LowerConstantPool(Op, DAG);
339 case ISD::GlobalAddress:
340 return LowerGlobalAddress(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000341 case ISD::FORMAL_ARGUMENTS:
342 return LowerFORMAL_ARGUMENTS(Op, DAG);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000343 case ISD::CALL:
344 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000345 case ISD::RET:
346 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000347 case ISD::SELECT_CC:
348 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000349 case ISD::BR_CC:
350 return LowerBR_CC(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000351 }
352}
353
354//===----------------------------------------------------------------------===//
355// Instruction Selector Implementation
356//===----------------------------------------------------------------------===//
357
358//===--------------------------------------------------------------------===//
359/// ARMDAGToDAGISel - ARM specific code to select ARM machine
360/// instructions for SelectionDAG operations.
361///
362namespace {
363class ARMDAGToDAGISel : public SelectionDAGISel {
364 ARMTargetLowering Lowering;
365
366public:
367 ARMDAGToDAGISel(TargetMachine &TM)
368 : SelectionDAGISel(Lowering), Lowering(TM) {
369 }
370
Evan Cheng64a752f2006-08-11 09:08:15 +0000371 SDNode *Select(SDOperand &Result, SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000372 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000373 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000374
375 // Include the pieces autogenerated from the target description.
376#include "ARMGenDAGISel.inc"
377};
378
379void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
380 DEBUG(BB->dump());
381
382 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000383 DAG.RemoveDeadNodes();
384
385 ScheduleAndEmitDAG(DAG);
386}
387
Rafael Espindola61369da2006-08-14 19:01:24 +0000388static bool isInt12Immediate(SDNode *N, short &Imm) {
389 if (N->getOpcode() != ISD::Constant)
390 return false;
391
392 int32_t t = cast<ConstantSDNode>(N)->getValue();
393 int max = 2<<12 - 1;
394 int min = -max;
395 if (t > min && t < max) {
396 Imm = t;
397 return true;
398 }
399 else
400 return false;
401}
402
403static bool isInt12Immediate(SDOperand Op, short &Imm) {
404 return isInt12Immediate(Op.Val, Imm);
405}
406
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000407//register plus/minus 12 bit offset
408bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
409 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000410 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
411 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
412 Offset = CurDAG->getTargetConstant(0, MVT::i32);
413 return true;
414 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000415 if (N.getOpcode() == ISD::ADD) {
416 short imm = 0;
417 if (isInt12Immediate(N.getOperand(1), imm)) {
418 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
419 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
420 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
421 } else {
422 Base = N.getOperand(0);
423 }
424 return true; // [r+i]
425 }
426 }
427
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000428 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000429 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
430 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
431 }
432 else
433 Base = N;
434 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000435}
436
Evan Cheng64a752f2006-08-11 09:08:15 +0000437SDNode *ARMDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000438 SDNode *N = Op.Val;
439
440 switch (N->getOpcode()) {
441 default:
Evan Cheng64a752f2006-08-11 09:08:15 +0000442 return SelectCode(Result, Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000443 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000444 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000445 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000446}
447
448} // end anonymous namespace
449
450/// createARMISelDag - This pass converts a legalized DAG into a
451/// ARM-specific DAG, ready for instruction scheduling.
452///
453FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
454 return new ARMDAGToDAGISel(TM);
455}