blob: e4b591d675230e0af04e5e100b5bdfb91c5bff03 [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
Rafael Espindola9e071f02006-10-02 19:30:56 +000085 FSITOS,
86
87 FSITOD,
88
89 FMRRD
Rafael Espindola84b19be2006-07-16 01:02:57 +000090 };
91 }
92}
93
Rafael Espindola6f602de2006-08-24 16:13:15 +000094/// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
95static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
96 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +000097 default:
98 std::cerr << "CC = " << CC << "\n";
99 assert(0 && "Unknown condition code!");
100 case ISD::SETUGT: return ARMCC::HI;
101 case ISD::SETULE: return ARMCC::LS;
102 case ISD::SETLE: return ARMCC::LE;
103 case ISD::SETLT: return ARMCC::LT;
104 case ISD::SETGT: return ARMCC::GT;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000105 case ISD::SETNE: return ARMCC::NE;
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000106 case ISD::SETEQ: return ARMCC::EQ;
Rafael Espindola5f450d22006-09-02 20:24:25 +0000107 case ISD::SETGE: return ARMCC::GE;
108 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000109 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000110 }
111}
112
Rafael Espindola84b19be2006-07-16 01:02:57 +0000113const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
114 switch (Opcode) {
115 default: return 0;
116 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000117 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000118 case ARMISD::SELECT: return "ARMISD::SELECT";
119 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000120 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000121 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000122 case ARMISD::FSITOD: return "ARMISD::FSITOD";
123 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000124 }
125}
126
127// This transforms a ISD::CALL node into a
128// callseq_star <- ARMISD:CALL <- callseq_end
129// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000130static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000131 SDOperand Chain = Op.getOperand(0);
132 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
133 assert(CallConv == CallingConv::C && "unknown calling convention");
134 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000135 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
136 assert(isTailCall == false && "tail call not supported");
137 SDOperand Callee = Op.getOperand(4);
138 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000139
Rafael Espindolaec46ea32006-08-16 14:43:33 +0000140 // Count how many bytes are to be pushed on the stack.
141 unsigned NumBytes = 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000142
Rafael Espindola1a009462006-08-08 13:02:29 +0000143 // Add up all the space actually used.
144 for (unsigned i = 4; i < NumOps; ++i)
145 NumBytes += MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000146
Rafael Espindola84b19be2006-07-16 01:02:57 +0000147 // Adjust the stack pointer for the new arguments...
148 // These operations are automatically eliminated by the prolog/epilog pass
149 Chain = DAG.getCALLSEQ_START(Chain,
150 DAG.getConstant(NumBytes, MVT::i32));
151
Rafael Espindola1a009462006-08-08 13:02:29 +0000152 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
153
154 static const unsigned int num_regs = 4;
155 static const unsigned regs[num_regs] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000156 ARM::R0, ARM::R1, ARM::R2, ARM::R3
157 };
158
159 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
Rafael Espindola1a009462006-08-08 13:02:29 +0000160 std::vector<SDOperand> MemOpChains;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000161
162 for (unsigned i = 0; i != NumOps; ++i) {
163 SDOperand Arg = Op.getOperand(5+2*i);
Rafael Espindola1a009462006-08-08 13:02:29 +0000164 assert(Arg.getValueType() == MVT::i32);
165 if (i < num_regs)
166 RegsToPass.push_back(std::make_pair(regs[i], Arg));
167 else {
168 unsigned ArgOffset = (i - num_regs) * 4;
169 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
170 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
171 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
172 Arg, PtrOff, DAG.getSrcValue(NULL)));
173 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000174 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000175 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000176 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
177 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000178
179 // Build a sequence of copy-to-reg nodes chained together with token chain
180 // and flag operands which copy the outgoing args into the appropriate regs.
181 SDOperand InFlag;
182 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
183 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
184 InFlag);
185 InFlag = Chain.getValue(1);
186 }
187
Rafael Espindola84b19be2006-07-16 01:02:57 +0000188 std::vector<MVT::ValueType> NodeTys;
189 NodeTys.push_back(MVT::Other); // Returns a chain
190 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
191
192 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
193 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
194 // node so that legalize doesn't hack it.
195 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
196 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
197
198 // If this is a direct call, pass the chain and the callee.
199 assert (Callee.Val);
200 std::vector<SDOperand> Ops;
201 Ops.push_back(Chain);
202 Ops.push_back(Callee);
203
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000204 // Add argument registers to the end of the list so that they are known live
205 // into the call.
206 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
207 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
208 RegsToPass[i].second.getValueType()));
209
Rafael Espindola84b19be2006-07-16 01:02:57 +0000210 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000211 if (InFlag.Val)
212 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000213 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000214 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000215
Rafael Espindolafac00a92006-07-25 20:17:20 +0000216 std::vector<SDOperand> ResultVals;
217 NodeTys.clear();
218
219 // If the call has results, copy the values out of the ret val registers.
220 switch (Op.Val->getValueType(0)) {
221 default: assert(0 && "Unexpected ret value!");
222 case MVT::Other:
223 break;
224 case MVT::i32:
225 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
226 ResultVals.push_back(Chain.getValue(0));
227 NodeTys.push_back(MVT::i32);
228 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000229
230 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
231 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000232 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000233
Rafael Espindolafac00a92006-07-25 20:17:20 +0000234 if (ResultVals.empty())
235 return Chain;
236
237 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000238 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
239 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000240 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000241}
242
243static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
244 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000245 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000246 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
247 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
248
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000249 switch(Op.getNumOperands()) {
250 default:
251 assert(0 && "Do not know how to return this many arguments!");
252 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000253 case 1: {
254 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000255 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000256 }
Rafael Espindola27185192006-09-29 21:20:16 +0000257 case 3: {
258 SDOperand Val = Op.getOperand(1);
259 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000260 Val.getValueType() == MVT::f32 ||
261 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000262
Rafael Espindola9e071f02006-10-02 19:30:56 +0000263 if (Val.getValueType() == MVT::f64) {
264 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
265 SDOperand Ops[] = {Chain, R0, R1, Val};
266 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
267 } else {
268 if (Val.getValueType() == MVT::f32)
269 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
270 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
271 }
272
273 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000274 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000275 if (Val.getValueType() == MVT::f64)
276 DAG.getMachineFunction().addLiveOut(ARM::R1);
277 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000278 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000279 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000280 case 5:
281 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
282 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
283 // If we haven't noted the R0+R1 are live out, do so now.
284 if (DAG.getMachineFunction().liveout_empty()) {
285 DAG.getMachineFunction().addLiveOut(ARM::R0);
286 DAG.getMachineFunction().addLiveOut(ARM::R1);
287 }
288 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000289 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000290
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000291 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
292 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000293}
294
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000295static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
Rafael Espindola755be9b2006-08-25 17:55:16 +0000296 unsigned *vRegs,
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000297 unsigned ArgNo) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000298 MachineFunction &MF = DAG.getMachineFunction();
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000299 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
300 assert (ObjectVT == MVT::i32);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000301 SDOperand Root = Op.getOperand(0);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000302 SSARegMap *RegMap = MF.getSSARegMap();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000303
Rafael Espindola4b442b52006-05-23 02:48:20 +0000304 unsigned num_regs = 4;
Rafael Espindola4b442b52006-05-23 02:48:20 +0000305 static const unsigned REGS[] = {
306 ARM::R0, ARM::R1, ARM::R2, ARM::R3
307 };
308
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000309 if(ArgNo < num_regs) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000310 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000311 MF.addLiveIn(REGS[ArgNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000312 vRegs[ArgNo] = VReg;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000313 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
314 } else {
315 // If the argument is actually used, emit a load from the right stack
316 // slot.
317 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000318 unsigned ArgOffset = (ArgNo - num_regs) * 4;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000319
320 MachineFrameInfo *MFI = MF.getFrameInfo();
321 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
322 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
323 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
324 return DAG.getLoad(ObjectVT, Root, FIN,
325 DAG.getSrcValue(NULL));
326 } else {
327 // Don't emit a dead load.
328 return DAG.getNode(ISD::UNDEF, ObjectVT);
329 }
330 }
331}
332
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000333static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
334 MVT::ValueType PtrVT = Op.getValueType();
335 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000336 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000337 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
338
339 return CPI;
340}
341
342static SDOperand LowerGlobalAddress(SDOperand Op,
343 SelectionDAG &DAG) {
344 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000345 int alignment = 2;
346 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000347 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
348 DAG.getSrcValue(NULL));
349}
350
Rafael Espindola755be9b2006-08-25 17:55:16 +0000351static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
352 unsigned VarArgsFrameIndex) {
353 // vastart just stores the address of the VarArgsFrameIndex slot into the
354 // memory location argument.
355 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
356 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
357 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
358 Op.getOperand(1), Op.getOperand(2));
359}
360
361static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
362 int &VarArgsFrameIndex) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000363 std::vector<SDOperand> ArgValues;
364 SDOperand Root = Op.getOperand(0);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000365 unsigned VRegs[4];
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000366
Rafael Espindola755be9b2006-08-25 17:55:16 +0000367 unsigned NumArgs = Op.Val->getNumValues()-1;
368 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
369 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000370
371 ArgValues.push_back(ArgVal);
372 }
373
374 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000375 if (isVarArg) {
376 MachineFunction &MF = DAG.getMachineFunction();
377 SSARegMap *RegMap = MF.getSSARegMap();
378 MachineFrameInfo *MFI = MF.getFrameInfo();
379 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
380 -16 + NumArgs * 4);
381
382
383 static const unsigned REGS[] = {
384 ARM::R0, ARM::R1, ARM::R2, ARM::R3
385 };
386 // If this function is vararg, store r0-r3 to their spots on the stack
387 // so that they may be loaded by deferencing the result of va_next.
388 SmallVector<SDOperand, 4> MemOps;
389 for (unsigned ArgNo = 0; ArgNo < 4; ++ArgNo) {
390 int ArgOffset = - (4 - ArgNo) * 4;
391 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
392 ArgOffset);
393 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
394
395 unsigned VReg;
396 if (ArgNo < NumArgs)
397 VReg = VRegs[ArgNo];
398 else
399 VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
400 if (ArgNo >= NumArgs)
401 MF.addLiveIn(REGS[ArgNo], VReg);
402
403 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
404 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
405 Val, FIN, DAG.getSrcValue(NULL));
406 MemOps.push_back(Store);
407 }
408 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
409 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000410
411 ArgValues.push_back(Root);
412
413 // Return the new list of results.
414 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
415 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000416 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000417}
418
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000419static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
420 SDOperand LHS = Op.getOperand(0);
421 SDOperand RHS = Op.getOperand(1);
422 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
423 SDOperand TrueVal = Op.getOperand(2);
424 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000425 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000426
427 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000428 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000429}
430
Rafael Espindola687bc492006-08-24 13:45:55 +0000431static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
432 SDOperand Chain = Op.getOperand(0);
433 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
434 SDOperand LHS = Op.getOperand(2);
435 SDOperand RHS = Op.getOperand(3);
436 SDOperand Dest = Op.getOperand(4);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000437 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola687bc492006-08-24 13:45:55 +0000438
439 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000440 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000441}
442
Rafael Espindola27185192006-09-29 21:20:16 +0000443static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000444 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000445 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000446 MVT::ValueType vt = Op.getValueType();
447 assert(vt == MVT::f32 ||
448 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000449
450 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000451 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
452 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000453}
454
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000455SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
456 switch (Op.getOpcode()) {
457 default:
458 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000459 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000460 case ISD::ConstantPool:
461 return LowerConstantPool(Op, DAG);
462 case ISD::GlobalAddress:
463 return LowerGlobalAddress(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000464 case ISD::SINT_TO_FP:
465 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000466 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000467 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000468 case ISD::CALL:
469 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000470 case ISD::RET:
471 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000472 case ISD::SELECT_CC:
473 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000474 case ISD::BR_CC:
475 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000476 case ISD::VASTART:
477 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000478 }
479}
480
481//===----------------------------------------------------------------------===//
482// Instruction Selector Implementation
483//===----------------------------------------------------------------------===//
484
485//===--------------------------------------------------------------------===//
486/// ARMDAGToDAGISel - ARM specific code to select ARM machine
487/// instructions for SelectionDAG operations.
488///
489namespace {
490class ARMDAGToDAGISel : public SelectionDAGISel {
491 ARMTargetLowering Lowering;
492
493public:
494 ARMDAGToDAGISel(TargetMachine &TM)
495 : SelectionDAGISel(Lowering), Lowering(TM) {
496 }
497
Evan Cheng9ade2182006-08-26 05:34:46 +0000498 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000499 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000500 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000501 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
502 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000503
504 // Include the pieces autogenerated from the target description.
505#include "ARMGenDAGISel.inc"
506};
507
508void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
509 DEBUG(BB->dump());
510
511 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000512 DAG.RemoveDeadNodes();
513
514 ScheduleAndEmitDAG(DAG);
515}
516
Rafael Espindola61369da2006-08-14 19:01:24 +0000517static bool isInt12Immediate(SDNode *N, short &Imm) {
518 if (N->getOpcode() != ISD::Constant)
519 return false;
520
521 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000522 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000523 int min = -max;
524 if (t > min && t < max) {
525 Imm = t;
526 return true;
527 }
528 else
529 return false;
530}
531
532static bool isInt12Immediate(SDOperand Op, short &Imm) {
533 return isInt12Immediate(Op.Val, Imm);
534}
535
Rafael Espindola7246d332006-09-21 11:29:52 +0000536static uint32_t rotateL(uint32_t x) {
537 uint32_t bit31 = (x & (1 << 31)) >> 31;
538 uint32_t t = x << 1;
539 return t | bit31;
540}
541
542static bool isUInt8Immediate(uint32_t x) {
543 return x < (1 << 8);
544}
545
546static bool isRotInt8Immediate(uint32_t x) {
547 int r;
548 for (r = 0; r < 16; r++) {
549 if (isUInt8Immediate(x))
550 return true;
551 x = rotateL(rotateL(x));
552 }
553 return false;
554}
555
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000556bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000557 SDOperand &Arg,
558 SDOperand &Shift,
559 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000560 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000561 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000562 uint32_t val = cast<ConstantSDNode>(N)->getValue();
563 if(!isRotInt8Immediate(val)) {
564 const Type *t = MVT::getTypeForValueType(MVT::i32);
565 Constant *C = ConstantUInt::get(t, val);
566 int alignment = 2;
567 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
568 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
569 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
570 Arg = SDOperand(n, 0);
571 } else
572 Arg = CurDAG->getTargetConstant(val, MVT::i32);
573
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000574 Shift = CurDAG->getTargetConstant(0, MVT::i32);
575 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000576 return true;
577 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000578 case ISD::SRA:
579 Arg = N.getOperand(0);
580 Shift = N.getOperand(1);
581 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
582 return true;
583 case ISD::SRL:
584 Arg = N.getOperand(0);
585 Shift = N.getOperand(1);
586 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
587 return true;
588 case ISD::SHL:
589 Arg = N.getOperand(0);
590 Shift = N.getOperand(1);
591 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
592 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000593 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000594
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000595 Arg = N;
596 Shift = CurDAG->getTargetConstant(0, MVT::i32);
597 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000598 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000599}
600
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000601//register plus/minus 12 bit offset
602bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
603 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000604 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
605 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
606 Offset = CurDAG->getTargetConstant(0, MVT::i32);
607 return true;
608 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000609 if (N.getOpcode() == ISD::ADD) {
610 short imm = 0;
611 if (isInt12Immediate(N.getOperand(1), imm)) {
612 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
613 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
614 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
615 } else {
616 Base = N.getOperand(0);
617 }
618 return true; // [r+i]
619 }
620 }
621
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000622 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000623 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
624 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
625 }
626 else
627 Base = N;
628 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000629}
630
Evan Cheng9ade2182006-08-26 05:34:46 +0000631SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000632 SDNode *N = Op.Val;
633
634 switch (N->getOpcode()) {
635 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000636 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000637 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000638 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000639 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000640}
641
642} // end anonymous namespace
643
644/// createARMISelDag - This pass converts a legalized DAG into a
645/// ARM-specific DAG, ready for instruction scheduling.
646///
647FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
648 return new ARMDAGToDAGISel(TM);
649}