blob: b299f81c01b911406a4f23f8e4486386862a2693 [file] [log] [blame]
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001//===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARM.h"
15#include "ARMTargetMachine.h"
Rafael Espindola84b19be2006-07-16 01:02:57 +000016#include "llvm/CallingConv.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Intrinsics.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/Debug.h"
28#include <iostream>
29#include <set>
30using namespace llvm;
31
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000032namespace {
33 class ARMTargetLowering : public TargetLowering {
34 public:
35 ARMTargetLowering(TargetMachine &TM);
36 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Rafael Espindola84b19be2006-07-16 01:02:57 +000037 virtual const char *getTargetNodeName(unsigned Opcode) const;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000038 };
39
40}
41
42ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
43 : TargetLowering(TM) {
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000044 setOperationAction(ISD::RET, MVT::Other, Custom);
45 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
46 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000047}
48
Rafael Espindola84b19be2006-07-16 01:02:57 +000049namespace llvm {
50 namespace ARMISD {
51 enum NodeType {
52 // Start the numbering where the builting ops and target ops leave off.
53 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
54 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000055 CALL,
56
57 /// Return with a flag operand.
58 RET_FLAG
Rafael Espindola84b19be2006-07-16 01:02:57 +000059 };
60 }
61}
62
63const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
64 switch (Opcode) {
65 default: return 0;
66 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +000067 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola84b19be2006-07-16 01:02:57 +000068 }
69}
70
71// This transforms a ISD::CALL node into a
72// callseq_star <- ARMISD:CALL <- callseq_end
73// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +000074static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +000075 SDOperand Chain = Op.getOperand(0);
76 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
77 assert(CallConv == CallingConv::C && "unknown calling convention");
78 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
79 assert(isVarArg == false && "VarArg not supported");
80 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
81 assert(isTailCall == false && "tail call not supported");
82 SDOperand Callee = Op.getOperand(4);
83 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola84b19be2006-07-16 01:02:57 +000084
85 // Count how many bytes are to be pushed on the stack. Initially
86 // only the link register.
87 unsigned NumBytes = 4;
88
Rafael Espindolafac00a92006-07-25 20:17:20 +000089 assert(NumOps <= 4); //no args on the stack
90
Rafael Espindola84b19be2006-07-16 01:02:57 +000091 // Adjust the stack pointer for the new arguments...
92 // These operations are automatically eliminated by the prolog/epilog pass
93 Chain = DAG.getCALLSEQ_START(Chain,
94 DAG.getConstant(NumBytes, MVT::i32));
95
Rafael Espindolafac00a92006-07-25 20:17:20 +000096 static const unsigned regs[] = {
97 ARM::R0, ARM::R1, ARM::R2, ARM::R3
98 };
99
100 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
101
102 for (unsigned i = 0; i != NumOps; ++i) {
103 SDOperand Arg = Op.getOperand(5+2*i);
104 RegsToPass.push_back(std::make_pair(regs[i], Arg));
105 }
106
107 // Build a sequence of copy-to-reg nodes chained together with token chain
108 // and flag operands which copy the outgoing args into the appropriate regs.
109 SDOperand InFlag;
110 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
111 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
112 InFlag);
113 InFlag = Chain.getValue(1);
114 }
115
Rafael Espindola84b19be2006-07-16 01:02:57 +0000116 std::vector<MVT::ValueType> NodeTys;
117 NodeTys.push_back(MVT::Other); // Returns a chain
118 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
119
120 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
121 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
122 // node so that legalize doesn't hack it.
123 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
124 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
125
126 // If this is a direct call, pass the chain and the callee.
127 assert (Callee.Val);
128 std::vector<SDOperand> Ops;
129 Ops.push_back(Chain);
130 Ops.push_back(Callee);
131
132 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000133 if (InFlag.Val)
134 Ops.push_back(InFlag);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000135 Chain = DAG.getNode(CallOpc, NodeTys, Ops);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000136 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000137
Rafael Espindolafac00a92006-07-25 20:17:20 +0000138 std::vector<SDOperand> ResultVals;
139 NodeTys.clear();
140
141 // If the call has results, copy the values out of the ret val registers.
142 switch (Op.Val->getValueType(0)) {
143 default: assert(0 && "Unexpected ret value!");
144 case MVT::Other:
145 break;
146 case MVT::i32:
147 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
148 ResultVals.push_back(Chain.getValue(0));
149 NodeTys.push_back(MVT::i32);
150 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000151
152 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
153 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000154 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000155
Rafael Espindolafac00a92006-07-25 20:17:20 +0000156 if (ResultVals.empty())
157 return Chain;
158
159 ResultVals.push_back(Chain);
160 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
161 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000162}
163
164static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
165 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000166 SDOperand Chain = Op.getOperand(0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000167 switch(Op.getNumOperands()) {
168 default:
169 assert(0 && "Do not know how to return this many arguments!");
170 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000171 case 1: {
172 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
173 return DAG.getNode(ISD::BRIND, MVT::Other, Chain, LR);
174 }
Evan Cheng6848be12006-05-26 23:10:12 +0000175 case 3:
Rafael Espindola4b023672006-06-05 22:26:14 +0000176 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op.getOperand(1), SDOperand());
177 if (DAG.getMachineFunction().liveout_empty())
178 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000179 break;
180 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000181
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000182 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
183 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000184}
185
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000186static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
187 unsigned ArgNo) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000188 MachineFunction &MF = DAG.getMachineFunction();
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000189 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
190 assert (ObjectVT == MVT::i32);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000191 SDOperand Root = Op.getOperand(0);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000192 SSARegMap *RegMap = MF.getSSARegMap();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000193
Rafael Espindola4b442b52006-05-23 02:48:20 +0000194 unsigned num_regs = 4;
Rafael Espindola4b442b52006-05-23 02:48:20 +0000195 static const unsigned REGS[] = {
196 ARM::R0, ARM::R1, ARM::R2, ARM::R3
197 };
198
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000199 if(ArgNo < num_regs) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000200 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000201 MF.addLiveIn(REGS[ArgNo], VReg);
202 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
203 } else {
204 // If the argument is actually used, emit a load from the right stack
205 // slot.
206 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000207 unsigned ArgOffset = (ArgNo - num_regs) * 4;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000208
209 MachineFrameInfo *MFI = MF.getFrameInfo();
210 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
211 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
212 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
213 return DAG.getLoad(ObjectVT, Root, FIN,
214 DAG.getSrcValue(NULL));
215 } else {
216 // Don't emit a dead load.
217 return DAG.getNode(ISD::UNDEF, ObjectVT);
218 }
219 }
220}
221
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000222static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
223 MVT::ValueType PtrVT = Op.getValueType();
224 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
225 Constant *C = CP->get();
226 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
227
228 return CPI;
229}
230
231static SDOperand LowerGlobalAddress(SDOperand Op,
232 SelectionDAG &DAG) {
233 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
234 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, 2);
235 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
236 DAG.getSrcValue(NULL));
237}
238
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000239static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
240 std::vector<SDOperand> ArgValues;
241 SDOperand Root = Op.getOperand(0);
242
243 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) {
244 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, ArgNo);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000245
246 ArgValues.push_back(ArgVal);
247 }
248
249 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
250 assert(!isVarArg);
251
252 ArgValues.push_back(Root);
253
254 // Return the new list of results.
255 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
256 Op.Val->value_end());
257 return DAG.getNode(ISD::MERGE_VALUES, RetVT, ArgValues);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000258}
259
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000260SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
261 switch (Op.getOpcode()) {
262 default:
263 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000264 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000265 case ISD::ConstantPool:
266 return LowerConstantPool(Op, DAG);
267 case ISD::GlobalAddress:
268 return LowerGlobalAddress(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000269 case ISD::FORMAL_ARGUMENTS:
270 return LowerFORMAL_ARGUMENTS(Op, DAG);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000271 case ISD::CALL:
272 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000273 case ISD::RET:
274 return LowerRET(Op, DAG);
275 }
276}
277
278//===----------------------------------------------------------------------===//
279// Instruction Selector Implementation
280//===----------------------------------------------------------------------===//
281
282//===--------------------------------------------------------------------===//
283/// ARMDAGToDAGISel - ARM specific code to select ARM machine
284/// instructions for SelectionDAG operations.
285///
286namespace {
287class ARMDAGToDAGISel : public SelectionDAGISel {
288 ARMTargetLowering Lowering;
289
290public:
291 ARMDAGToDAGISel(TargetMachine &TM)
292 : SelectionDAGISel(Lowering), Lowering(TM) {
293 }
294
295 void Select(SDOperand &Result, SDOperand Op);
296 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000297 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000298
299 // Include the pieces autogenerated from the target description.
300#include "ARMGenDAGISel.inc"
301};
302
303void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
304 DEBUG(BB->dump());
305
306 DAG.setRoot(SelectRoot(DAG.getRoot()));
307 CodeGenMap.clear();
Evan Chengafe358e2006-05-24 20:46:25 +0000308 HandleMap.clear();
309 ReplaceMap.clear();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000310 DAG.RemoveDeadNodes();
311
312 ScheduleAndEmitDAG(DAG);
313}
314
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000315//register plus/minus 12 bit offset
316bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
317 SDOperand &Base) {
318 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000319 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
320 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
321 }
322 else
323 Base = N;
324 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000325}
326
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000327void ARMDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000328 SDNode *N = Op.Val;
329
330 switch (N->getOpcode()) {
331 default:
332 SelectCode(Result, Op);
333 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000334 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000335}
336
337} // end anonymous namespace
338
339/// createARMISelDag - This pass converts a legalized DAG into a
340/// ARM-specific DAG, ready for instruction scheduling.
341///
342FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
343 return new ARMDAGToDAGISel(TM);
344}