blob: 2adf4073ba02134c039c1a1773e622b018f3a892 [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 Espindolacd71da52006-10-03 17:27:58 +000064 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
65 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
66
Rafael Espindola341b8642006-08-04 12:48:42 +000067 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +000068 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000069}
70
Rafael Espindola84b19be2006-07-16 01:02:57 +000071namespace llvm {
72 namespace ARMISD {
73 enum NodeType {
74 // Start the numbering where the builting ops and target ops leave off.
75 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
76 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000077 CALL,
78
79 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +000080 RET_FLAG,
81
82 CMP,
83
Rafael Espindola687bc492006-08-24 13:45:55 +000084 SELECT,
85
Rafael Espindola27185192006-09-29 21:20:16 +000086 BR,
87
Rafael Espindola9e071f02006-10-02 19:30:56 +000088 FSITOS,
89
90 FSITOD,
91
92 FMRRD
Rafael Espindola84b19be2006-07-16 01:02:57 +000093 };
94 }
95}
96
Rafael Espindola6f602de2006-08-24 16:13:15 +000097/// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
98static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
99 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000100 default:
101 std::cerr << "CC = " << CC << "\n";
102 assert(0 && "Unknown condition code!");
103 case ISD::SETUGT: return ARMCC::HI;
104 case ISD::SETULE: return ARMCC::LS;
105 case ISD::SETLE: return ARMCC::LE;
106 case ISD::SETLT: return ARMCC::LT;
107 case ISD::SETGT: return ARMCC::GT;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000108 case ISD::SETNE: return ARMCC::NE;
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000109 case ISD::SETEQ: return ARMCC::EQ;
Rafael Espindola5f450d22006-09-02 20:24:25 +0000110 case ISD::SETGE: return ARMCC::GE;
111 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000112 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000113 }
114}
115
Rafael Espindola84b19be2006-07-16 01:02:57 +0000116const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
117 switch (Opcode) {
118 default: return 0;
119 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000120 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000121 case ARMISD::SELECT: return "ARMISD::SELECT";
122 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000123 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000124 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000125 case ARMISD::FSITOD: return "ARMISD::FSITOD";
126 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000127 }
128}
129
130// This transforms a ISD::CALL node into a
131// callseq_star <- ARMISD:CALL <- callseq_end
132// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000133static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000134 SDOperand Chain = Op.getOperand(0);
135 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
136 assert(CallConv == CallingConv::C && "unknown calling convention");
137 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000138 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
139 assert(isTailCall == false && "tail call not supported");
140 SDOperand Callee = Op.getOperand(4);
141 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000142
Rafael Espindolaec46ea32006-08-16 14:43:33 +0000143 // Count how many bytes are to be pushed on the stack.
144 unsigned NumBytes = 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000145
Rafael Espindola1a009462006-08-08 13:02:29 +0000146 // Add up all the space actually used.
147 for (unsigned i = 4; i < NumOps; ++i)
148 NumBytes += MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000149
Rafael Espindola84b19be2006-07-16 01:02:57 +0000150 // Adjust the stack pointer for the new arguments...
151 // These operations are automatically eliminated by the prolog/epilog pass
152 Chain = DAG.getCALLSEQ_START(Chain,
153 DAG.getConstant(NumBytes, MVT::i32));
154
Rafael Espindola1a009462006-08-08 13:02:29 +0000155 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
156
157 static const unsigned int num_regs = 4;
158 static const unsigned regs[num_regs] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000159 ARM::R0, ARM::R1, ARM::R2, ARM::R3
160 };
161
162 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
Rafael Espindola1a009462006-08-08 13:02:29 +0000163 std::vector<SDOperand> MemOpChains;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000164
165 for (unsigned i = 0; i != NumOps; ++i) {
166 SDOperand Arg = Op.getOperand(5+2*i);
Rafael Espindola1a009462006-08-08 13:02:29 +0000167 assert(Arg.getValueType() == MVT::i32);
168 if (i < num_regs)
169 RegsToPass.push_back(std::make_pair(regs[i], Arg));
170 else {
171 unsigned ArgOffset = (i - num_regs) * 4;
172 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
173 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
174 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
175 Arg, PtrOff, DAG.getSrcValue(NULL)));
176 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000177 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000178 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000179 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
180 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000181
182 // Build a sequence of copy-to-reg nodes chained together with token chain
183 // and flag operands which copy the outgoing args into the appropriate regs.
184 SDOperand InFlag;
185 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
186 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
187 InFlag);
188 InFlag = Chain.getValue(1);
189 }
190
Rafael Espindola84b19be2006-07-16 01:02:57 +0000191 std::vector<MVT::ValueType> NodeTys;
192 NodeTys.push_back(MVT::Other); // Returns a chain
193 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
194
195 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
196 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
197 // node so that legalize doesn't hack it.
198 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
199 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
200
201 // If this is a direct call, pass the chain and the callee.
202 assert (Callee.Val);
203 std::vector<SDOperand> Ops;
204 Ops.push_back(Chain);
205 Ops.push_back(Callee);
206
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000207 // Add argument registers to the end of the list so that they are known live
208 // into the call.
209 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
210 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
211 RegsToPass[i].second.getValueType()));
212
Rafael Espindola84b19be2006-07-16 01:02:57 +0000213 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000214 if (InFlag.Val)
215 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000216 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000217 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000218
Rafael Espindolafac00a92006-07-25 20:17:20 +0000219 std::vector<SDOperand> ResultVals;
220 NodeTys.clear();
221
222 // If the call has results, copy the values out of the ret val registers.
223 switch (Op.Val->getValueType(0)) {
224 default: assert(0 && "Unexpected ret value!");
225 case MVT::Other:
226 break;
227 case MVT::i32:
228 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
229 ResultVals.push_back(Chain.getValue(0));
230 NodeTys.push_back(MVT::i32);
231 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000232
233 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
234 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000235 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000236
Rafael Espindolafac00a92006-07-25 20:17:20 +0000237 if (ResultVals.empty())
238 return Chain;
239
240 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000241 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
242 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000243 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000244}
245
246static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
247 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000248 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000249 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
250 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
251
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000252 switch(Op.getNumOperands()) {
253 default:
254 assert(0 && "Do not know how to return this many arguments!");
255 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000256 case 1: {
257 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000258 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000259 }
Rafael Espindola27185192006-09-29 21:20:16 +0000260 case 3: {
261 SDOperand Val = Op.getOperand(1);
262 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000263 Val.getValueType() == MVT::f32 ||
264 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000265
Rafael Espindola9e071f02006-10-02 19:30:56 +0000266 if (Val.getValueType() == MVT::f64) {
267 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
268 SDOperand Ops[] = {Chain, R0, R1, Val};
269 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
270 } else {
271 if (Val.getValueType() == MVT::f32)
272 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
273 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
274 }
275
276 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000277 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000278 if (Val.getValueType() == MVT::f64)
279 DAG.getMachineFunction().addLiveOut(ARM::R1);
280 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000281 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000282 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000283 case 5:
284 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
285 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
286 // If we haven't noted the R0+R1 are live out, do so now.
287 if (DAG.getMachineFunction().liveout_empty()) {
288 DAG.getMachineFunction().addLiveOut(ARM::R0);
289 DAG.getMachineFunction().addLiveOut(ARM::R1);
290 }
291 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000292 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000293
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000294 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
295 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000296}
297
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000298static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
Rafael Espindola755be9b2006-08-25 17:55:16 +0000299 unsigned *vRegs,
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000300 unsigned ArgNo) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000301 MachineFunction &MF = DAG.getMachineFunction();
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000302 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
303 assert (ObjectVT == MVT::i32);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000304 SDOperand Root = Op.getOperand(0);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000305 SSARegMap *RegMap = MF.getSSARegMap();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000306
Rafael Espindola4b442b52006-05-23 02:48:20 +0000307 unsigned num_regs = 4;
Rafael Espindola4b442b52006-05-23 02:48:20 +0000308 static const unsigned REGS[] = {
309 ARM::R0, ARM::R1, ARM::R2, ARM::R3
310 };
311
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000312 if(ArgNo < num_regs) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000313 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000314 MF.addLiveIn(REGS[ArgNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000315 vRegs[ArgNo] = VReg;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000316 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
317 } else {
318 // If the argument is actually used, emit a load from the right stack
319 // slot.
320 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000321 unsigned ArgOffset = (ArgNo - num_regs) * 4;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000322
323 MachineFrameInfo *MFI = MF.getFrameInfo();
324 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
325 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
326 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
327 return DAG.getLoad(ObjectVT, Root, FIN,
328 DAG.getSrcValue(NULL));
329 } else {
330 // Don't emit a dead load.
331 return DAG.getNode(ISD::UNDEF, ObjectVT);
332 }
333 }
334}
335
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000336static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
337 MVT::ValueType PtrVT = Op.getValueType();
338 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000339 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000340 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
341
342 return CPI;
343}
344
345static SDOperand LowerGlobalAddress(SDOperand Op,
346 SelectionDAG &DAG) {
347 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000348 int alignment = 2;
349 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000350 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
351 DAG.getSrcValue(NULL));
352}
353
Rafael Espindola755be9b2006-08-25 17:55:16 +0000354static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
355 unsigned VarArgsFrameIndex) {
356 // vastart just stores the address of the VarArgsFrameIndex slot into the
357 // memory location argument.
358 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
359 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
360 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
361 Op.getOperand(1), Op.getOperand(2));
362}
363
364static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
365 int &VarArgsFrameIndex) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000366 std::vector<SDOperand> ArgValues;
367 SDOperand Root = Op.getOperand(0);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000368 unsigned VRegs[4];
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000369
Rafael Espindola755be9b2006-08-25 17:55:16 +0000370 unsigned NumArgs = Op.Val->getNumValues()-1;
371 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
372 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000373
374 ArgValues.push_back(ArgVal);
375 }
376
377 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000378 if (isVarArg) {
379 MachineFunction &MF = DAG.getMachineFunction();
380 SSARegMap *RegMap = MF.getSSARegMap();
381 MachineFrameInfo *MFI = MF.getFrameInfo();
382 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
383 -16 + NumArgs * 4);
384
385
386 static const unsigned REGS[] = {
387 ARM::R0, ARM::R1, ARM::R2, ARM::R3
388 };
389 // If this function is vararg, store r0-r3 to their spots on the stack
390 // so that they may be loaded by deferencing the result of va_next.
391 SmallVector<SDOperand, 4> MemOps;
392 for (unsigned ArgNo = 0; ArgNo < 4; ++ArgNo) {
393 int ArgOffset = - (4 - ArgNo) * 4;
394 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
395 ArgOffset);
396 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
397
398 unsigned VReg;
399 if (ArgNo < NumArgs)
400 VReg = VRegs[ArgNo];
401 else
402 VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
403 if (ArgNo >= NumArgs)
404 MF.addLiveIn(REGS[ArgNo], VReg);
405
406 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
407 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
408 Val, FIN, DAG.getSrcValue(NULL));
409 MemOps.push_back(Store);
410 }
411 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
412 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000413
414 ArgValues.push_back(Root);
415
416 // Return the new list of results.
417 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
418 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000419 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000420}
421
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000422static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
423 SDOperand LHS = Op.getOperand(0);
424 SDOperand RHS = Op.getOperand(1);
425 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
426 SDOperand TrueVal = Op.getOperand(2);
427 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000428 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000429
430 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000431 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000432}
433
Rafael Espindola687bc492006-08-24 13:45:55 +0000434static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
435 SDOperand Chain = Op.getOperand(0);
436 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
437 SDOperand LHS = Op.getOperand(2);
438 SDOperand RHS = Op.getOperand(3);
439 SDOperand Dest = Op.getOperand(4);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000440 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola687bc492006-08-24 13:45:55 +0000441
442 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000443 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000444}
445
Rafael Espindola27185192006-09-29 21:20:16 +0000446static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000447 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000448 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000449 MVT::ValueType vt = Op.getValueType();
450 assert(vt == MVT::f32 ||
451 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000452
453 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000454 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
455 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000456}
457
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000458SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
459 switch (Op.getOpcode()) {
460 default:
461 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000462 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000463 case ISD::ConstantPool:
464 return LowerConstantPool(Op, DAG);
465 case ISD::GlobalAddress:
466 return LowerGlobalAddress(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000467 case ISD::SINT_TO_FP:
468 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000469 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000470 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000471 case ISD::CALL:
472 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000473 case ISD::RET:
474 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000475 case ISD::SELECT_CC:
476 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000477 case ISD::BR_CC:
478 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000479 case ISD::VASTART:
480 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000481 }
482}
483
484//===----------------------------------------------------------------------===//
485// Instruction Selector Implementation
486//===----------------------------------------------------------------------===//
487
488//===--------------------------------------------------------------------===//
489/// ARMDAGToDAGISel - ARM specific code to select ARM machine
490/// instructions for SelectionDAG operations.
491///
492namespace {
493class ARMDAGToDAGISel : public SelectionDAGISel {
494 ARMTargetLowering Lowering;
495
496public:
497 ARMDAGToDAGISel(TargetMachine &TM)
498 : SelectionDAGISel(Lowering), Lowering(TM) {
499 }
500
Evan Cheng9ade2182006-08-26 05:34:46 +0000501 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000502 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000503 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000504 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
505 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000506
507 // Include the pieces autogenerated from the target description.
508#include "ARMGenDAGISel.inc"
509};
510
511void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
512 DEBUG(BB->dump());
513
514 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000515 DAG.RemoveDeadNodes();
516
517 ScheduleAndEmitDAG(DAG);
518}
519
Rafael Espindola61369da2006-08-14 19:01:24 +0000520static bool isInt12Immediate(SDNode *N, short &Imm) {
521 if (N->getOpcode() != ISD::Constant)
522 return false;
523
524 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000525 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000526 int min = -max;
527 if (t > min && t < max) {
528 Imm = t;
529 return true;
530 }
531 else
532 return false;
533}
534
535static bool isInt12Immediate(SDOperand Op, short &Imm) {
536 return isInt12Immediate(Op.Val, Imm);
537}
538
Rafael Espindola7246d332006-09-21 11:29:52 +0000539static uint32_t rotateL(uint32_t x) {
540 uint32_t bit31 = (x & (1 << 31)) >> 31;
541 uint32_t t = x << 1;
542 return t | bit31;
543}
544
545static bool isUInt8Immediate(uint32_t x) {
546 return x < (1 << 8);
547}
548
549static bool isRotInt8Immediate(uint32_t x) {
550 int r;
551 for (r = 0; r < 16; r++) {
552 if (isUInt8Immediate(x))
553 return true;
554 x = rotateL(rotateL(x));
555 }
556 return false;
557}
558
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000559bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000560 SDOperand &Arg,
561 SDOperand &Shift,
562 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000563 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000564 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000565 uint32_t val = cast<ConstantSDNode>(N)->getValue();
566 if(!isRotInt8Immediate(val)) {
567 const Type *t = MVT::getTypeForValueType(MVT::i32);
568 Constant *C = ConstantUInt::get(t, val);
569 int alignment = 2;
570 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
571 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
572 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
573 Arg = SDOperand(n, 0);
574 } else
575 Arg = CurDAG->getTargetConstant(val, MVT::i32);
576
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000577 Shift = CurDAG->getTargetConstant(0, MVT::i32);
578 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000579 return true;
580 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000581 case ISD::SRA:
582 Arg = N.getOperand(0);
583 Shift = N.getOperand(1);
584 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
585 return true;
586 case ISD::SRL:
587 Arg = N.getOperand(0);
588 Shift = N.getOperand(1);
589 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
590 return true;
591 case ISD::SHL:
592 Arg = N.getOperand(0);
593 Shift = N.getOperand(1);
594 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
595 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000596 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000597
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000598 Arg = N;
599 Shift = CurDAG->getTargetConstant(0, MVT::i32);
600 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000601 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000602}
603
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000604//register plus/minus 12 bit offset
605bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
606 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000607 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
608 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
609 Offset = CurDAG->getTargetConstant(0, MVT::i32);
610 return true;
611 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000612 if (N.getOpcode() == ISD::ADD) {
613 short imm = 0;
614 if (isInt12Immediate(N.getOperand(1), imm)) {
615 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
616 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
617 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
618 } else {
619 Base = N.getOperand(0);
620 }
621 return true; // [r+i]
622 }
623 }
624
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000625 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000626 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
627 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
628 }
629 else
630 Base = N;
631 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000632}
633
Evan Cheng9ade2182006-08-26 05:34:46 +0000634SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000635 SDNode *N = Op.Val;
636
637 switch (N->getOpcode()) {
638 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000639 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000640 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000641 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000642 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000643}
644
645} // end anonymous namespace
646
647/// createARMISelDag - This pass converts a legalized DAG into a
648/// ARM-specific DAG, ready for instruction scheduling.
649///
650FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
651 return new ARMDAGToDAGISel(TM);
652}