blob: 74bfdc3da249d9b163fc6efb2679b4e3b03e9a58 [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);
48
49 //LLVM requires that a register class supports MVT::f64!
50 addRegisterClass(MVT::f64, ARM::IntRegsRegisterClass);
51
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000052 setOperationAction(ISD::RET, MVT::Other, Custom);
53 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
54 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000055
Rafael Espindola3c000bf2006-08-21 22:00:32 +000056 setOperationAction(ISD::SETCC, MVT::i32, Expand);
57 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindola687bc492006-08-24 13:45:55 +000058 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000059
Rafael Espindola755be9b2006-08-25 17:55:16 +000060 setOperationAction(ISD::VASTART, MVT::Other, Custom);
61 setOperationAction(ISD::VAEND, MVT::Other, Expand);
62
Rafael Espindola341b8642006-08-04 12:48:42 +000063 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +000064 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000065}
66
Rafael Espindola84b19be2006-07-16 01:02:57 +000067namespace llvm {
68 namespace ARMISD {
69 enum NodeType {
70 // Start the numbering where the builting ops and target ops leave off.
71 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
72 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000073 CALL,
74
75 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +000076 RET_FLAG,
77
78 CMP,
79
Rafael Espindola687bc492006-08-24 13:45:55 +000080 SELECT,
81
82 BR
Rafael Espindola84b19be2006-07-16 01:02:57 +000083 };
84 }
85}
86
Rafael Espindola6f602de2006-08-24 16:13:15 +000087/// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
88static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
89 switch (CC) {
90 default: assert(0 && "Unknown condition code!");
91 case ISD::SETNE: return ARMCC::NE;
Rafael Espindolacdda88c2006-08-24 17:19:08 +000092 case ISD::SETEQ: return ARMCC::EQ;
Rafael Espindola5f450d22006-09-02 20:24:25 +000093 case ISD::SETGE: return ARMCC::GE;
94 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindolabc4cec92006-09-03 13:19:16 +000095 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola6f602de2006-08-24 16:13:15 +000096 }
97}
98
Rafael Espindola84b19be2006-07-16 01:02:57 +000099const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
100 switch (Opcode) {
101 default: return 0;
102 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000103 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000104 case ARMISD::SELECT: return "ARMISD::SELECT";
105 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000106 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000107 }
108}
109
110// This transforms a ISD::CALL node into a
111// callseq_star <- ARMISD:CALL <- callseq_end
112// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000113static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000114 SDOperand Chain = Op.getOperand(0);
115 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
116 assert(CallConv == CallingConv::C && "unknown calling convention");
117 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000118 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
119 assert(isTailCall == false && "tail call not supported");
120 SDOperand Callee = Op.getOperand(4);
121 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000122
Rafael Espindolaec46ea32006-08-16 14:43:33 +0000123 // Count how many bytes are to be pushed on the stack.
124 unsigned NumBytes = 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000125
Rafael Espindola1a009462006-08-08 13:02:29 +0000126 // Add up all the space actually used.
127 for (unsigned i = 4; i < NumOps; ++i)
128 NumBytes += MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000129
Rafael Espindola84b19be2006-07-16 01:02:57 +0000130 // Adjust the stack pointer for the new arguments...
131 // These operations are automatically eliminated by the prolog/epilog pass
132 Chain = DAG.getCALLSEQ_START(Chain,
133 DAG.getConstant(NumBytes, MVT::i32));
134
Rafael Espindola1a009462006-08-08 13:02:29 +0000135 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
136
137 static const unsigned int num_regs = 4;
138 static const unsigned regs[num_regs] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000139 ARM::R0, ARM::R1, ARM::R2, ARM::R3
140 };
141
142 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
Rafael Espindola1a009462006-08-08 13:02:29 +0000143 std::vector<SDOperand> MemOpChains;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000144
145 for (unsigned i = 0; i != NumOps; ++i) {
146 SDOperand Arg = Op.getOperand(5+2*i);
Rafael Espindola1a009462006-08-08 13:02:29 +0000147 assert(Arg.getValueType() == MVT::i32);
148 if (i < num_regs)
149 RegsToPass.push_back(std::make_pair(regs[i], Arg));
150 else {
151 unsigned ArgOffset = (i - num_regs) * 4;
152 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
153 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
154 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
155 Arg, PtrOff, DAG.getSrcValue(NULL)));
156 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000157 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000158 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000159 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
160 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000161
162 // Build a sequence of copy-to-reg nodes chained together with token chain
163 // and flag operands which copy the outgoing args into the appropriate regs.
164 SDOperand InFlag;
165 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
166 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
167 InFlag);
168 InFlag = Chain.getValue(1);
169 }
170
Rafael Espindola84b19be2006-07-16 01:02:57 +0000171 std::vector<MVT::ValueType> NodeTys;
172 NodeTys.push_back(MVT::Other); // Returns a chain
173 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
174
175 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
176 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
177 // node so that legalize doesn't hack it.
178 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
179 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
180
181 // If this is a direct call, pass the chain and the callee.
182 assert (Callee.Val);
183 std::vector<SDOperand> Ops;
184 Ops.push_back(Chain);
185 Ops.push_back(Callee);
186
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000187 // Add argument registers to the end of the list so that they are known live
188 // into the call.
189 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
190 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
191 RegsToPass[i].second.getValueType()));
192
Rafael Espindola84b19be2006-07-16 01:02:57 +0000193 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000194 if (InFlag.Val)
195 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000196 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000197 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000198
Rafael Espindolafac00a92006-07-25 20:17:20 +0000199 std::vector<SDOperand> ResultVals;
200 NodeTys.clear();
201
202 // If the call has results, copy the values out of the ret val registers.
203 switch (Op.Val->getValueType(0)) {
204 default: assert(0 && "Unexpected ret value!");
205 case MVT::Other:
206 break;
207 case MVT::i32:
208 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
209 ResultVals.push_back(Chain.getValue(0));
210 NodeTys.push_back(MVT::i32);
211 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000212
213 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
214 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000215 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000216
Rafael Espindolafac00a92006-07-25 20:17:20 +0000217 if (ResultVals.empty())
218 return Chain;
219
220 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000221 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
222 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000223 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000224}
225
226static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
227 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000228 SDOperand Chain = Op.getOperand(0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000229 switch(Op.getNumOperands()) {
230 default:
231 assert(0 && "Do not know how to return this many arguments!");
232 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000233 case 1: {
234 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000235 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000236 }
Evan Cheng6848be12006-05-26 23:10:12 +0000237 case 3:
Rafael Espindola4b023672006-06-05 22:26:14 +0000238 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op.getOperand(1), SDOperand());
239 if (DAG.getMachineFunction().liveout_empty())
240 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000241 break;
Rafael Espindola3a02f022006-09-04 19:05:01 +0000242 case 5:
243 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
244 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
245 // If we haven't noted the R0+R1 are live out, do so now.
246 if (DAG.getMachineFunction().liveout_empty()) {
247 DAG.getMachineFunction().addLiveOut(ARM::R0);
248 DAG.getMachineFunction().addLiveOut(ARM::R1);
249 }
250 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000251 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000252
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000253 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
254 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000255}
256
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000257static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
Rafael Espindola755be9b2006-08-25 17:55:16 +0000258 unsigned *vRegs,
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000259 unsigned ArgNo) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000260 MachineFunction &MF = DAG.getMachineFunction();
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000261 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
262 assert (ObjectVT == MVT::i32);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000263 SDOperand Root = Op.getOperand(0);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000264 SSARegMap *RegMap = MF.getSSARegMap();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000265
Rafael Espindola4b442b52006-05-23 02:48:20 +0000266 unsigned num_regs = 4;
Rafael Espindola4b442b52006-05-23 02:48:20 +0000267 static const unsigned REGS[] = {
268 ARM::R0, ARM::R1, ARM::R2, ARM::R3
269 };
270
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000271 if(ArgNo < num_regs) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000272 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000273 MF.addLiveIn(REGS[ArgNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000274 vRegs[ArgNo] = VReg;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000275 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
276 } else {
277 // If the argument is actually used, emit a load from the right stack
278 // slot.
279 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000280 unsigned ArgOffset = (ArgNo - num_regs) * 4;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000281
282 MachineFrameInfo *MFI = MF.getFrameInfo();
283 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
284 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
285 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
286 return DAG.getLoad(ObjectVT, Root, FIN,
287 DAG.getSrcValue(NULL));
288 } else {
289 // Don't emit a dead load.
290 return DAG.getNode(ISD::UNDEF, ObjectVT);
291 }
292 }
293}
294
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000295static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
296 MVT::ValueType PtrVT = Op.getValueType();
297 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000298 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000299 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
300
301 return CPI;
302}
303
304static SDOperand LowerGlobalAddress(SDOperand Op,
305 SelectionDAG &DAG) {
306 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000307 int alignment = 2;
308 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000309 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
310 DAG.getSrcValue(NULL));
311}
312
Rafael Espindola755be9b2006-08-25 17:55:16 +0000313static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
314 unsigned VarArgsFrameIndex) {
315 // vastart just stores the address of the VarArgsFrameIndex slot into the
316 // memory location argument.
317 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
318 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
319 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
320 Op.getOperand(1), Op.getOperand(2));
321}
322
323static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
324 int &VarArgsFrameIndex) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000325 std::vector<SDOperand> ArgValues;
326 SDOperand Root = Op.getOperand(0);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000327 unsigned VRegs[4];
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000328
Rafael Espindola755be9b2006-08-25 17:55:16 +0000329 unsigned NumArgs = Op.Val->getNumValues()-1;
330 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
331 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000332
333 ArgValues.push_back(ArgVal);
334 }
335
336 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000337 if (isVarArg) {
338 MachineFunction &MF = DAG.getMachineFunction();
339 SSARegMap *RegMap = MF.getSSARegMap();
340 MachineFrameInfo *MFI = MF.getFrameInfo();
341 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
342 -16 + NumArgs * 4);
343
344
345 static const unsigned REGS[] = {
346 ARM::R0, ARM::R1, ARM::R2, ARM::R3
347 };
348 // If this function is vararg, store r0-r3 to their spots on the stack
349 // so that they may be loaded by deferencing the result of va_next.
350 SmallVector<SDOperand, 4> MemOps;
351 for (unsigned ArgNo = 0; ArgNo < 4; ++ArgNo) {
352 int ArgOffset = - (4 - ArgNo) * 4;
353 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
354 ArgOffset);
355 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
356
357 unsigned VReg;
358 if (ArgNo < NumArgs)
359 VReg = VRegs[ArgNo];
360 else
361 VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
362 if (ArgNo >= NumArgs)
363 MF.addLiveIn(REGS[ArgNo], VReg);
364
365 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
366 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
367 Val, FIN, DAG.getSrcValue(NULL));
368 MemOps.push_back(Store);
369 }
370 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
371 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000372
373 ArgValues.push_back(Root);
374
375 // Return the new list of results.
376 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
377 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000378 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000379}
380
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000381static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
382 SDOperand LHS = Op.getOperand(0);
383 SDOperand RHS = Op.getOperand(1);
384 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
385 SDOperand TrueVal = Op.getOperand(2);
386 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000387 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000388
389 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000390 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000391}
392
Rafael Espindola687bc492006-08-24 13:45:55 +0000393static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
394 SDOperand Chain = Op.getOperand(0);
395 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
396 SDOperand LHS = Op.getOperand(2);
397 SDOperand RHS = Op.getOperand(3);
398 SDOperand Dest = Op.getOperand(4);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000399 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola687bc492006-08-24 13:45:55 +0000400
401 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000402 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000403}
404
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000405SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
406 switch (Op.getOpcode()) {
407 default:
408 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000409 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000410 case ISD::ConstantPool:
411 return LowerConstantPool(Op, DAG);
412 case ISD::GlobalAddress:
413 return LowerGlobalAddress(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000414 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000415 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000416 case ISD::CALL:
417 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000418 case ISD::RET:
419 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000420 case ISD::SELECT_CC:
421 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000422 case ISD::BR_CC:
423 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000424 case ISD::VASTART:
425 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000426 }
427}
428
429//===----------------------------------------------------------------------===//
430// Instruction Selector Implementation
431//===----------------------------------------------------------------------===//
432
433//===--------------------------------------------------------------------===//
434/// ARMDAGToDAGISel - ARM specific code to select ARM machine
435/// instructions for SelectionDAG operations.
436///
437namespace {
438class ARMDAGToDAGISel : public SelectionDAGISel {
439 ARMTargetLowering Lowering;
440
441public:
442 ARMDAGToDAGISel(TargetMachine &TM)
443 : SelectionDAGISel(Lowering), Lowering(TM) {
444 }
445
Evan Cheng9ade2182006-08-26 05:34:46 +0000446 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000447 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000448 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000449 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
450 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000451
452 // Include the pieces autogenerated from the target description.
453#include "ARMGenDAGISel.inc"
454};
455
456void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
457 DEBUG(BB->dump());
458
459 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000460 DAG.RemoveDeadNodes();
461
462 ScheduleAndEmitDAG(DAG);
463}
464
Rafael Espindola61369da2006-08-14 19:01:24 +0000465static bool isInt12Immediate(SDNode *N, short &Imm) {
466 if (N->getOpcode() != ISD::Constant)
467 return false;
468
469 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000470 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000471 int min = -max;
472 if (t > min && t < max) {
473 Imm = t;
474 return true;
475 }
476 else
477 return false;
478}
479
480static bool isInt12Immediate(SDOperand Op, short &Imm) {
481 return isInt12Immediate(Op.Val, Imm);
482}
483
Rafael Espindola7246d332006-09-21 11:29:52 +0000484static uint32_t rotateL(uint32_t x) {
485 uint32_t bit31 = (x & (1 << 31)) >> 31;
486 uint32_t t = x << 1;
487 return t | bit31;
488}
489
490static bool isUInt8Immediate(uint32_t x) {
491 return x < (1 << 8);
492}
493
494static bool isRotInt8Immediate(uint32_t x) {
495 int r;
496 for (r = 0; r < 16; r++) {
497 if (isUInt8Immediate(x))
498 return true;
499 x = rotateL(rotateL(x));
500 }
501 return false;
502}
503
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000504bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000505 SDOperand &Arg,
506 SDOperand &Shift,
507 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000508 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000509 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000510 uint32_t val = cast<ConstantSDNode>(N)->getValue();
511 if(!isRotInt8Immediate(val)) {
512 const Type *t = MVT::getTypeForValueType(MVT::i32);
513 Constant *C = ConstantUInt::get(t, val);
514 int alignment = 2;
515 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
516 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
517 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
518 Arg = SDOperand(n, 0);
519 } else
520 Arg = CurDAG->getTargetConstant(val, MVT::i32);
521
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000522 Shift = CurDAG->getTargetConstant(0, MVT::i32);
523 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000524 return true;
525 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000526 case ISD::SRA:
527 Arg = N.getOperand(0);
528 Shift = N.getOperand(1);
529 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
530 return true;
531 case ISD::SRL:
532 Arg = N.getOperand(0);
533 Shift = N.getOperand(1);
534 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
535 return true;
536 case ISD::SHL:
537 Arg = N.getOperand(0);
538 Shift = N.getOperand(1);
539 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
540 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000541 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000542
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000543 Arg = N;
544 Shift = CurDAG->getTargetConstant(0, MVT::i32);
545 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000546 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000547}
548
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000549//register plus/minus 12 bit offset
550bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
551 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000552 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
553 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
554 Offset = CurDAG->getTargetConstant(0, MVT::i32);
555 return true;
556 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000557 if (N.getOpcode() == ISD::ADD) {
558 short imm = 0;
559 if (isInt12Immediate(N.getOperand(1), imm)) {
560 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
561 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
562 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
563 } else {
564 Base = N.getOperand(0);
565 }
566 return true; // [r+i]
567 }
568 }
569
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000570 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000571 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
572 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
573 }
574 else
575 Base = N;
576 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000577}
578
Evan Cheng9ade2182006-08-26 05:34:46 +0000579SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000580 SDNode *N = Op.Val;
581
582 switch (N->getOpcode()) {
583 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000584 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000585 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000586 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000587 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000588}
589
590} // end anonymous namespace
591
592/// createARMISelDag - This pass converts a legalized DAG into a
593/// ARM-specific DAG, ready for instruction scheduling.
594///
595FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
596 return new ARMDAGToDAGISel(TM);
597}