blob: 75253b92cd0c6165067fe6666e4965c6e6db9d1f [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
Rafael Espindola6f602de2006-08-24 16:13:15 +000082/// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
83static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
84 switch (CC) {
85 default: assert(0 && "Unknown condition code!");
86 case ISD::SETNE: return ARMCC::NE;
Rafael Espindolacdda88c2006-08-24 17:19:08 +000087 case ISD::SETEQ: return ARMCC::EQ;
Rafael Espindola6f602de2006-08-24 16:13:15 +000088 }
89}
90
Rafael Espindola84b19be2006-07-16 01:02:57 +000091const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
92 switch (Opcode) {
93 default: return 0;
94 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +000095 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +000096 case ARMISD::SELECT: return "ARMISD::SELECT";
97 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +000098 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola84b19be2006-07-16 01:02:57 +000099 }
100}
101
102// This transforms a ISD::CALL node into a
103// callseq_star <- ARMISD:CALL <- callseq_end
104// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000105static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000106 SDOperand Chain = Op.getOperand(0);
107 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
108 assert(CallConv == CallingConv::C && "unknown calling convention");
109 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000110 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
111 assert(isTailCall == false && "tail call not supported");
112 SDOperand Callee = Op.getOperand(4);
113 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000114
Rafael Espindolaec46ea32006-08-16 14:43:33 +0000115 // Count how many bytes are to be pushed on the stack.
116 unsigned NumBytes = 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000117
Rafael Espindola1a009462006-08-08 13:02:29 +0000118 // Add up all the space actually used.
119 for (unsigned i = 4; i < NumOps; ++i)
120 NumBytes += MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000121
Rafael Espindola84b19be2006-07-16 01:02:57 +0000122 // Adjust the stack pointer for the new arguments...
123 // These operations are automatically eliminated by the prolog/epilog pass
124 Chain = DAG.getCALLSEQ_START(Chain,
125 DAG.getConstant(NumBytes, MVT::i32));
126
Rafael Espindola1a009462006-08-08 13:02:29 +0000127 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
128
129 static const unsigned int num_regs = 4;
130 static const unsigned regs[num_regs] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000131 ARM::R0, ARM::R1, ARM::R2, ARM::R3
132 };
133
134 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
Rafael Espindola1a009462006-08-08 13:02:29 +0000135 std::vector<SDOperand> MemOpChains;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000136
137 for (unsigned i = 0; i != NumOps; ++i) {
138 SDOperand Arg = Op.getOperand(5+2*i);
Rafael Espindola1a009462006-08-08 13:02:29 +0000139 assert(Arg.getValueType() == MVT::i32);
140 if (i < num_regs)
141 RegsToPass.push_back(std::make_pair(regs[i], Arg));
142 else {
143 unsigned ArgOffset = (i - num_regs) * 4;
144 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
145 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
146 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
147 Arg, PtrOff, DAG.getSrcValue(NULL)));
148 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000149 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000150 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000151 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
152 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000153
154 // Build a sequence of copy-to-reg nodes chained together with token chain
155 // and flag operands which copy the outgoing args into the appropriate regs.
156 SDOperand InFlag;
157 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
158 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
159 InFlag);
160 InFlag = Chain.getValue(1);
161 }
162
Rafael Espindola84b19be2006-07-16 01:02:57 +0000163 std::vector<MVT::ValueType> NodeTys;
164 NodeTys.push_back(MVT::Other); // Returns a chain
165 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
166
167 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
168 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
169 // node so that legalize doesn't hack it.
170 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
171 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
172
173 // If this is a direct call, pass the chain and the callee.
174 assert (Callee.Val);
175 std::vector<SDOperand> Ops;
176 Ops.push_back(Chain);
177 Ops.push_back(Callee);
178
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000179 // Add argument registers to the end of the list so that they are known live
180 // into the call.
181 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
182 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
183 RegsToPass[i].second.getValueType()));
184
Rafael Espindola84b19be2006-07-16 01:02:57 +0000185 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000186 if (InFlag.Val)
187 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000188 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000189 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000190
Rafael Espindolafac00a92006-07-25 20:17:20 +0000191 std::vector<SDOperand> ResultVals;
192 NodeTys.clear();
193
194 // If the call has results, copy the values out of the ret val registers.
195 switch (Op.Val->getValueType(0)) {
196 default: assert(0 && "Unexpected ret value!");
197 case MVT::Other:
198 break;
199 case MVT::i32:
200 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
201 ResultVals.push_back(Chain.getValue(0));
202 NodeTys.push_back(MVT::i32);
203 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000204
205 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
206 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000207 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000208
Rafael Espindolafac00a92006-07-25 20:17:20 +0000209 if (ResultVals.empty())
210 return Chain;
211
212 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000213 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
214 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000215 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000216}
217
218static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
219 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000220 SDOperand Chain = Op.getOperand(0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000221 switch(Op.getNumOperands()) {
222 default:
223 assert(0 && "Do not know how to return this many arguments!");
224 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000225 case 1: {
226 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000227 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000228 }
Evan Cheng6848be12006-05-26 23:10:12 +0000229 case 3:
Rafael Espindola4b023672006-06-05 22:26:14 +0000230 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op.getOperand(1), SDOperand());
231 if (DAG.getMachineFunction().liveout_empty())
232 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000233 break;
234 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000235
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000236 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
237 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000238}
239
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000240static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
241 unsigned ArgNo) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000242 MachineFunction &MF = DAG.getMachineFunction();
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000243 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
244 assert (ObjectVT == MVT::i32);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000245 SDOperand Root = Op.getOperand(0);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000246 SSARegMap *RegMap = MF.getSSARegMap();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000247
Rafael Espindola4b442b52006-05-23 02:48:20 +0000248 unsigned num_regs = 4;
Rafael Espindola4b442b52006-05-23 02:48:20 +0000249 static const unsigned REGS[] = {
250 ARM::R0, ARM::R1, ARM::R2, ARM::R3
251 };
252
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000253 if(ArgNo < num_regs) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000254 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000255 MF.addLiveIn(REGS[ArgNo], VReg);
256 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
257 } else {
258 // If the argument is actually used, emit a load from the right stack
259 // slot.
260 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000261 unsigned ArgOffset = (ArgNo - num_regs) * 4;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000262
263 MachineFrameInfo *MFI = MF.getFrameInfo();
264 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
265 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
266 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
267 return DAG.getLoad(ObjectVT, Root, FIN,
268 DAG.getSrcValue(NULL));
269 } else {
270 // Don't emit a dead load.
271 return DAG.getNode(ISD::UNDEF, ObjectVT);
272 }
273 }
274}
275
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000276static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
277 MVT::ValueType PtrVT = Op.getValueType();
278 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
279 Constant *C = CP->get();
280 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
281
282 return CPI;
283}
284
285static SDOperand LowerGlobalAddress(SDOperand Op,
286 SelectionDAG &DAG) {
287 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000288 int alignment = 2;
289 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000290 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
291 DAG.getSrcValue(NULL));
292}
293
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000294static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
295 std::vector<SDOperand> ArgValues;
296 SDOperand Root = Op.getOperand(0);
297
298 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) {
299 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, ArgNo);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000300
301 ArgValues.push_back(ArgVal);
302 }
303
304 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
305 assert(!isVarArg);
306
307 ArgValues.push_back(Root);
308
309 // Return the new list of results.
310 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
311 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000312 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000313}
314
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000315static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
316 SDOperand LHS = Op.getOperand(0);
317 SDOperand RHS = Op.getOperand(1);
318 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
319 SDOperand TrueVal = Op.getOperand(2);
320 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000321 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000322
323 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000324 return DAG.getNode(ARMISD::SELECT, MVT::i32, FalseVal, TrueVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000325}
326
Rafael Espindola687bc492006-08-24 13:45:55 +0000327static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
328 SDOperand Chain = Op.getOperand(0);
329 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
330 SDOperand LHS = Op.getOperand(2);
331 SDOperand RHS = Op.getOperand(3);
332 SDOperand Dest = Op.getOperand(4);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000333 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola687bc492006-08-24 13:45:55 +0000334
335 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000336 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000337}
338
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000339SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
340 switch (Op.getOpcode()) {
341 default:
342 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000343 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000344 case ISD::ConstantPool:
345 return LowerConstantPool(Op, DAG);
346 case ISD::GlobalAddress:
347 return LowerGlobalAddress(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000348 case ISD::FORMAL_ARGUMENTS:
349 return LowerFORMAL_ARGUMENTS(Op, DAG);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000350 case ISD::CALL:
351 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000352 case ISD::RET:
353 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000354 case ISD::SELECT_CC:
355 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000356 case ISD::BR_CC:
357 return LowerBR_CC(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000358 }
359}
360
361//===----------------------------------------------------------------------===//
362// Instruction Selector Implementation
363//===----------------------------------------------------------------------===//
364
365//===--------------------------------------------------------------------===//
366/// ARMDAGToDAGISel - ARM specific code to select ARM machine
367/// instructions for SelectionDAG operations.
368///
369namespace {
370class ARMDAGToDAGISel : public SelectionDAGISel {
371 ARMTargetLowering Lowering;
372
373public:
374 ARMDAGToDAGISel(TargetMachine &TM)
375 : SelectionDAGISel(Lowering), Lowering(TM) {
376 }
377
Evan Cheng64a752f2006-08-11 09:08:15 +0000378 SDNode *Select(SDOperand &Result, SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000379 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000380 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000381
382 // Include the pieces autogenerated from the target description.
383#include "ARMGenDAGISel.inc"
384};
385
386void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
387 DEBUG(BB->dump());
388
389 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000390 DAG.RemoveDeadNodes();
391
392 ScheduleAndEmitDAG(DAG);
393}
394
Rafael Espindola61369da2006-08-14 19:01:24 +0000395static bool isInt12Immediate(SDNode *N, short &Imm) {
396 if (N->getOpcode() != ISD::Constant)
397 return false;
398
399 int32_t t = cast<ConstantSDNode>(N)->getValue();
400 int max = 2<<12 - 1;
401 int min = -max;
402 if (t > min && t < max) {
403 Imm = t;
404 return true;
405 }
406 else
407 return false;
408}
409
410static bool isInt12Immediate(SDOperand Op, short &Imm) {
411 return isInt12Immediate(Op.Val, Imm);
412}
413
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000414//register plus/minus 12 bit offset
415bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
416 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000417 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
418 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
419 Offset = CurDAG->getTargetConstant(0, MVT::i32);
420 return true;
421 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000422 if (N.getOpcode() == ISD::ADD) {
423 short imm = 0;
424 if (isInt12Immediate(N.getOperand(1), imm)) {
425 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
426 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
427 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
428 } else {
429 Base = N.getOperand(0);
430 }
431 return true; // [r+i]
432 }
433 }
434
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000435 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000436 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
437 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
438 }
439 else
440 Base = N;
441 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000442}
443
Evan Cheng64a752f2006-08-11 09:08:15 +0000444SDNode *ARMDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000445 SDNode *N = Op.Val;
446
447 switch (N->getOpcode()) {
448 default:
Evan Cheng64a752f2006-08-11 09:08:15 +0000449 return SelectCode(Result, Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000450 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000451 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000452 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000453}
454
455} // end anonymous namespace
456
457/// createARMISelDag - This pass converts a legalized DAG into a
458/// ARM-specific DAG, ready for instruction scheduling.
459///
460FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
461 return new ARMDAGToDAGISel(TM);
462}