blob: a98064cc2f701cae63ea7d058d364c8d23491749 [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"
Rafael Espindola7246d332006-09-21 11:29:52 +000019#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000020#include "llvm/Intrinsics.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
26#include "llvm/CodeGen/SSARegMap.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/Debug.h"
29#include <iostream>
Evan Cheng2ef88a02006-08-07 22:28:20 +000030#include <queue>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000031#include <set>
32using namespace llvm;
33
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000034namespace {
35 class ARMTargetLowering : public TargetLowering {
Rafael Espindola755be9b2006-08-25 17:55:16 +000036 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000037 public:
38 ARMTargetLowering(TargetMachine &TM);
39 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Rafael Espindola84b19be2006-07-16 01:02:57 +000040 virtual const char *getTargetNodeName(unsigned Opcode) const;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000041 };
42
43}
44
45ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
46 : TargetLowering(TM) {
Rafael Espindola3717ca92006-08-20 01:49:49 +000047 addRegisterClass(MVT::i32, ARM::IntRegsRegisterClass);
Rafael Espindola27185192006-09-29 21:20:16 +000048 addRegisterClass(MVT::f32, ARM::FPRegsRegisterClass);
49 addRegisterClass(MVT::f64, ARM::DFPRegsRegisterClass);
Rafael Espindola3717ca92006-08-20 01:49:49 +000050
Rafael Espindola27185192006-09-29 21:20:16 +000051 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Rafael Espindola3717ca92006-08-20 01:49:49 +000052
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000053 setOperationAction(ISD::RET, MVT::Other, Custom);
54 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
55 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000056
Rafael Espindola3c000bf2006-08-21 22:00:32 +000057 setOperationAction(ISD::SETCC, MVT::i32, Expand);
58 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindola687bc492006-08-24 13:45:55 +000059 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000060
Rafael Espindola755be9b2006-08-25 17:55:16 +000061 setOperationAction(ISD::VASTART, MVT::Other, Custom);
62 setOperationAction(ISD::VAEND, MVT::Other, Expand);
63
Rafael Espindola341b8642006-08-04 12:48:42 +000064 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +000065 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000066}
67
Rafael Espindola84b19be2006-07-16 01:02:57 +000068namespace llvm {
69 namespace ARMISD {
70 enum NodeType {
71 // Start the numbering where the builting ops and target ops leave off.
72 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
73 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000074 CALL,
75
76 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +000077 RET_FLAG,
78
79 CMP,
80
Rafael Espindola687bc492006-08-24 13:45:55 +000081 SELECT,
82
Rafael Espindola27185192006-09-29 21:20:16 +000083 BR,
84
85 FSITOS
Rafael Espindola84b19be2006-07-16 01:02:57 +000086 };
87 }
88}
89
Rafael Espindola6f602de2006-08-24 16:13:15 +000090/// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
91static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
92 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +000093 default:
94 std::cerr << "CC = " << CC << "\n";
95 assert(0 && "Unknown condition code!");
96 case ISD::SETUGT: return ARMCC::HI;
97 case ISD::SETULE: return ARMCC::LS;
98 case ISD::SETLE: return ARMCC::LE;
99 case ISD::SETLT: return ARMCC::LT;
100 case ISD::SETGT: return ARMCC::GT;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000101 case ISD::SETNE: return ARMCC::NE;
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000102 case ISD::SETEQ: return ARMCC::EQ;
Rafael Espindola5f450d22006-09-02 20:24:25 +0000103 case ISD::SETGE: return ARMCC::GE;
104 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000105 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000106 }
107}
108
Rafael Espindola84b19be2006-07-16 01:02:57 +0000109const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
110 switch (Opcode) {
111 default: return 0;
112 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000113 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000114 case ARMISD::SELECT: return "ARMISD::SELECT";
115 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000116 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000117 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000118 }
119}
120
121// This transforms a ISD::CALL node into a
122// callseq_star <- ARMISD:CALL <- callseq_end
123// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000124static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000125 SDOperand Chain = Op.getOperand(0);
126 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
127 assert(CallConv == CallingConv::C && "unknown calling convention");
128 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000129 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
130 assert(isTailCall == false && "tail call not supported");
131 SDOperand Callee = Op.getOperand(4);
132 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000133
Rafael Espindolaec46ea32006-08-16 14:43:33 +0000134 // Count how many bytes are to be pushed on the stack.
135 unsigned NumBytes = 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000136
Rafael Espindola1a009462006-08-08 13:02:29 +0000137 // Add up all the space actually used.
138 for (unsigned i = 4; i < NumOps; ++i)
139 NumBytes += MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000140
Rafael Espindola84b19be2006-07-16 01:02:57 +0000141 // Adjust the stack pointer for the new arguments...
142 // These operations are automatically eliminated by the prolog/epilog pass
143 Chain = DAG.getCALLSEQ_START(Chain,
144 DAG.getConstant(NumBytes, MVT::i32));
145
Rafael Espindola1a009462006-08-08 13:02:29 +0000146 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
147
148 static const unsigned int num_regs = 4;
149 static const unsigned regs[num_regs] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000150 ARM::R0, ARM::R1, ARM::R2, ARM::R3
151 };
152
153 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
Rafael Espindola1a009462006-08-08 13:02:29 +0000154 std::vector<SDOperand> MemOpChains;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000155
156 for (unsigned i = 0; i != NumOps; ++i) {
157 SDOperand Arg = Op.getOperand(5+2*i);
Rafael Espindola1a009462006-08-08 13:02:29 +0000158 assert(Arg.getValueType() == MVT::i32);
159 if (i < num_regs)
160 RegsToPass.push_back(std::make_pair(regs[i], Arg));
161 else {
162 unsigned ArgOffset = (i - num_regs) * 4;
163 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
164 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
165 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
166 Arg, PtrOff, DAG.getSrcValue(NULL)));
167 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000168 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000169 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000170 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
171 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000172
173 // Build a sequence of copy-to-reg nodes chained together with token chain
174 // and flag operands which copy the outgoing args into the appropriate regs.
175 SDOperand InFlag;
176 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
177 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
178 InFlag);
179 InFlag = Chain.getValue(1);
180 }
181
Rafael Espindola84b19be2006-07-16 01:02:57 +0000182 std::vector<MVT::ValueType> NodeTys;
183 NodeTys.push_back(MVT::Other); // Returns a chain
184 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
185
186 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
187 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
188 // node so that legalize doesn't hack it.
189 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
190 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
191
192 // If this is a direct call, pass the chain and the callee.
193 assert (Callee.Val);
194 std::vector<SDOperand> Ops;
195 Ops.push_back(Chain);
196 Ops.push_back(Callee);
197
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000198 // Add argument registers to the end of the list so that they are known live
199 // into the call.
200 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
201 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
202 RegsToPass[i].second.getValueType()));
203
Rafael Espindola84b19be2006-07-16 01:02:57 +0000204 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000205 if (InFlag.Val)
206 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000207 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000208 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000209
Rafael Espindolafac00a92006-07-25 20:17:20 +0000210 std::vector<SDOperand> ResultVals;
211 NodeTys.clear();
212
213 // If the call has results, copy the values out of the ret val registers.
214 switch (Op.Val->getValueType(0)) {
215 default: assert(0 && "Unexpected ret value!");
216 case MVT::Other:
217 break;
218 case MVT::i32:
219 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
220 ResultVals.push_back(Chain.getValue(0));
221 NodeTys.push_back(MVT::i32);
222 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000223
224 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
225 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000226 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000227
Rafael Espindolafac00a92006-07-25 20:17:20 +0000228 if (ResultVals.empty())
229 return Chain;
230
231 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000232 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
233 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000234 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000235}
236
237static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
238 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000239 SDOperand Chain = Op.getOperand(0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000240 switch(Op.getNumOperands()) {
241 default:
242 assert(0 && "Do not know how to return this many arguments!");
243 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000244 case 1: {
245 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000246 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000247 }
Rafael Espindola27185192006-09-29 21:20:16 +0000248 case 3: {
249 SDOperand Val = Op.getOperand(1);
250 assert(Val.getValueType() == MVT::i32 ||
251 Val.getValueType() == MVT::f32);
252
253 if (Val.getValueType() == MVT::f32)
254 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
255 Copy = DAG.getCopyToReg(Chain, ARM::R0, Val, SDOperand());
Rafael Espindola4b023672006-06-05 22:26:14 +0000256 if (DAG.getMachineFunction().liveout_empty())
257 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000258 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000259 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000260 case 5:
261 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
262 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
263 // If we haven't noted the R0+R1 are live out, do so now.
264 if (DAG.getMachineFunction().liveout_empty()) {
265 DAG.getMachineFunction().addLiveOut(ARM::R0);
266 DAG.getMachineFunction().addLiveOut(ARM::R1);
267 }
268 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000269 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000270
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000271 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
272 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000273}
274
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000275static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
Rafael Espindola755be9b2006-08-25 17:55:16 +0000276 unsigned *vRegs,
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000277 unsigned ArgNo) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000278 MachineFunction &MF = DAG.getMachineFunction();
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000279 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
280 assert (ObjectVT == MVT::i32);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000281 SDOperand Root = Op.getOperand(0);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000282 SSARegMap *RegMap = MF.getSSARegMap();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000283
Rafael Espindola4b442b52006-05-23 02:48:20 +0000284 unsigned num_regs = 4;
Rafael Espindola4b442b52006-05-23 02:48:20 +0000285 static const unsigned REGS[] = {
286 ARM::R0, ARM::R1, ARM::R2, ARM::R3
287 };
288
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000289 if(ArgNo < num_regs) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000290 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000291 MF.addLiveIn(REGS[ArgNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000292 vRegs[ArgNo] = VReg;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000293 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
294 } else {
295 // If the argument is actually used, emit a load from the right stack
296 // slot.
297 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000298 unsigned ArgOffset = (ArgNo - num_regs) * 4;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000299
300 MachineFrameInfo *MFI = MF.getFrameInfo();
301 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
302 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
303 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
304 return DAG.getLoad(ObjectVT, Root, FIN,
305 DAG.getSrcValue(NULL));
306 } else {
307 // Don't emit a dead load.
308 return DAG.getNode(ISD::UNDEF, ObjectVT);
309 }
310 }
311}
312
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000313static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
314 MVT::ValueType PtrVT = Op.getValueType();
315 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000316 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000317 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
318
319 return CPI;
320}
321
322static SDOperand LowerGlobalAddress(SDOperand Op,
323 SelectionDAG &DAG) {
324 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000325 int alignment = 2;
326 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000327 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
328 DAG.getSrcValue(NULL));
329}
330
Rafael Espindola755be9b2006-08-25 17:55:16 +0000331static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
332 unsigned VarArgsFrameIndex) {
333 // vastart just stores the address of the VarArgsFrameIndex slot into the
334 // memory location argument.
335 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
336 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
337 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
338 Op.getOperand(1), Op.getOperand(2));
339}
340
341static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
342 int &VarArgsFrameIndex) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000343 std::vector<SDOperand> ArgValues;
344 SDOperand Root = Op.getOperand(0);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000345 unsigned VRegs[4];
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000346
Rafael Espindola755be9b2006-08-25 17:55:16 +0000347 unsigned NumArgs = Op.Val->getNumValues()-1;
348 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
349 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000350
351 ArgValues.push_back(ArgVal);
352 }
353
354 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000355 if (isVarArg) {
356 MachineFunction &MF = DAG.getMachineFunction();
357 SSARegMap *RegMap = MF.getSSARegMap();
358 MachineFrameInfo *MFI = MF.getFrameInfo();
359 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
360 -16 + NumArgs * 4);
361
362
363 static const unsigned REGS[] = {
364 ARM::R0, ARM::R1, ARM::R2, ARM::R3
365 };
366 // If this function is vararg, store r0-r3 to their spots on the stack
367 // so that they may be loaded by deferencing the result of va_next.
368 SmallVector<SDOperand, 4> MemOps;
369 for (unsigned ArgNo = 0; ArgNo < 4; ++ArgNo) {
370 int ArgOffset = - (4 - ArgNo) * 4;
371 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
372 ArgOffset);
373 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
374
375 unsigned VReg;
376 if (ArgNo < NumArgs)
377 VReg = VRegs[ArgNo];
378 else
379 VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
380 if (ArgNo >= NumArgs)
381 MF.addLiveIn(REGS[ArgNo], VReg);
382
383 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
384 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
385 Val, FIN, DAG.getSrcValue(NULL));
386 MemOps.push_back(Store);
387 }
388 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
389 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000390
391 ArgValues.push_back(Root);
392
393 // Return the new list of results.
394 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
395 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000396 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000397}
398
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000399static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
400 SDOperand LHS = Op.getOperand(0);
401 SDOperand RHS = Op.getOperand(1);
402 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
403 SDOperand TrueVal = Op.getOperand(2);
404 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000405 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000406
407 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000408 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000409}
410
Rafael Espindola687bc492006-08-24 13:45:55 +0000411static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
412 SDOperand Chain = Op.getOperand(0);
413 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
414 SDOperand LHS = Op.getOperand(2);
415 SDOperand RHS = Op.getOperand(3);
416 SDOperand Dest = Op.getOperand(4);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000417 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola687bc492006-08-24 13:45:55 +0000418
419 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000420 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000421}
422
Rafael Espindola27185192006-09-29 21:20:16 +0000423static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
424 SDOperand IntVal = Op.getOperand(0);
425 assert(IntVal.getValueType() == MVT::i32);
426 assert(Op.getValueType() == MVT::f32);
427
428 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
429 return DAG.getNode(ARMISD::FSITOS, MVT::f32, Tmp);
430}
431
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000432SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
433 switch (Op.getOpcode()) {
434 default:
435 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000436 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000437 case ISD::ConstantPool:
438 return LowerConstantPool(Op, DAG);
439 case ISD::GlobalAddress:
440 return LowerGlobalAddress(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000441 case ISD::SINT_TO_FP:
442 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000443 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000444 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000445 case ISD::CALL:
446 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000447 case ISD::RET:
448 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000449 case ISD::SELECT_CC:
450 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000451 case ISD::BR_CC:
452 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000453 case ISD::VASTART:
454 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000455 }
456}
457
458//===----------------------------------------------------------------------===//
459// Instruction Selector Implementation
460//===----------------------------------------------------------------------===//
461
462//===--------------------------------------------------------------------===//
463/// ARMDAGToDAGISel - ARM specific code to select ARM machine
464/// instructions for SelectionDAG operations.
465///
466namespace {
467class ARMDAGToDAGISel : public SelectionDAGISel {
468 ARMTargetLowering Lowering;
469
470public:
471 ARMDAGToDAGISel(TargetMachine &TM)
472 : SelectionDAGISel(Lowering), Lowering(TM) {
473 }
474
Evan Cheng9ade2182006-08-26 05:34:46 +0000475 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000476 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000477 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000478 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
479 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000480
481 // Include the pieces autogenerated from the target description.
482#include "ARMGenDAGISel.inc"
483};
484
485void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
486 DEBUG(BB->dump());
487
488 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000489 DAG.RemoveDeadNodes();
490
491 ScheduleAndEmitDAG(DAG);
492}
493
Rafael Espindola61369da2006-08-14 19:01:24 +0000494static bool isInt12Immediate(SDNode *N, short &Imm) {
495 if (N->getOpcode() != ISD::Constant)
496 return false;
497
498 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000499 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000500 int min = -max;
501 if (t > min && t < max) {
502 Imm = t;
503 return true;
504 }
505 else
506 return false;
507}
508
509static bool isInt12Immediate(SDOperand Op, short &Imm) {
510 return isInt12Immediate(Op.Val, Imm);
511}
512
Rafael Espindola7246d332006-09-21 11:29:52 +0000513static uint32_t rotateL(uint32_t x) {
514 uint32_t bit31 = (x & (1 << 31)) >> 31;
515 uint32_t t = x << 1;
516 return t | bit31;
517}
518
519static bool isUInt8Immediate(uint32_t x) {
520 return x < (1 << 8);
521}
522
523static bool isRotInt8Immediate(uint32_t x) {
524 int r;
525 for (r = 0; r < 16; r++) {
526 if (isUInt8Immediate(x))
527 return true;
528 x = rotateL(rotateL(x));
529 }
530 return false;
531}
532
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000533bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000534 SDOperand &Arg,
535 SDOperand &Shift,
536 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000537 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000538 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000539 uint32_t val = cast<ConstantSDNode>(N)->getValue();
540 if(!isRotInt8Immediate(val)) {
541 const Type *t = MVT::getTypeForValueType(MVT::i32);
542 Constant *C = ConstantUInt::get(t, val);
543 int alignment = 2;
544 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
545 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
546 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
547 Arg = SDOperand(n, 0);
548 } else
549 Arg = CurDAG->getTargetConstant(val, MVT::i32);
550
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000551 Shift = CurDAG->getTargetConstant(0, MVT::i32);
552 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000553 return true;
554 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000555 case ISD::SRA:
556 Arg = N.getOperand(0);
557 Shift = N.getOperand(1);
558 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
559 return true;
560 case ISD::SRL:
561 Arg = N.getOperand(0);
562 Shift = N.getOperand(1);
563 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
564 return true;
565 case ISD::SHL:
566 Arg = N.getOperand(0);
567 Shift = N.getOperand(1);
568 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
569 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000570 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000571
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000572 Arg = N;
573 Shift = CurDAG->getTargetConstant(0, MVT::i32);
574 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000575 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000576}
577
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000578//register plus/minus 12 bit offset
579bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
580 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000581 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
582 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
583 Offset = CurDAG->getTargetConstant(0, MVT::i32);
584 return true;
585 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000586 if (N.getOpcode() == ISD::ADD) {
587 short imm = 0;
588 if (isInt12Immediate(N.getOperand(1), imm)) {
589 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
590 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
591 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
592 } else {
593 Base = N.getOperand(0);
594 }
595 return true; // [r+i]
596 }
597 }
598
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000599 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000600 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
601 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
602 }
603 else
604 Base = N;
605 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000606}
607
Evan Cheng9ade2182006-08-26 05:34:46 +0000608SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000609 SDNode *N = Op.Val;
610
611 switch (N->getOpcode()) {
612 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000613 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000614 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000615 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000616 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000617}
618
619} // end anonymous namespace
620
621/// createARMISelDag - This pass converts a legalized DAG into a
622/// ARM-specific DAG, ready for instruction scheduling.
623///
624FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
625 return new ARMDAGToDAGISel(TM);
626}