blob: e6df1c185f685b0630c3d69bbeeb1dacd9df3c21 [file] [log] [blame]
Tony Linthicumb4b54152011-12-12 21:14:40 +00001//===-- HexagonISelLowering.cpp - Hexagon DAG Lowering Implementation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Hexagon uses to lower LLVM code
11// into a selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "HexagonISelLowering.h"
16#include "HexagonTargetMachine.h"
17#include "HexagonMachineFunctionInfo.h"
18#include "HexagonTargetObjectFile.h"
19#include "HexagonSubtarget.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/InlineAsm.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/GlobalAlias.h"
25#include "llvm/Intrinsics.h"
26#include "llvm/CallingConv.h"
27#include "llvm/CodeGen/CallingConvLower.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Craig Topper79aa3412012-03-17 18:46:09 +000031#include "llvm/CodeGen/MachineJumpTableInfo.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
33#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/CodeGen/ValueTypes.h"
NAKAMURA Takumid2f16a22012-04-21 15:31:36 +000035#include "llvm/Support/CommandLine.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumi89593932012-04-21 15:31:45 +000038#include "llvm/Support/raw_ostream.h"
NAKAMURA Takumid2f16a22012-04-21 15:31:36 +000039
Craig Topper79aa3412012-03-17 18:46:09 +000040using namespace llvm;
Tony Linthicumb4b54152011-12-12 21:14:40 +000041
42const unsigned Hexagon_MAX_RET_SIZE = 64;
Tony Linthicumb4b54152011-12-12 21:14:40 +000043
44static cl::opt<bool>
45EmitJumpTables("hexagon-emit-jump-tables", cl::init(true), cl::Hidden,
46 cl::desc("Control jump table emission on Hexagon target"));
47
48int NumNamedVarArgParams = -1;
49
50// Implement calling convention for Hexagon.
51static bool
52CC_Hexagon(unsigned ValNo, MVT ValVT,
53 MVT LocVT, CCValAssign::LocInfo LocInfo,
54 ISD::ArgFlagsTy ArgFlags, CCState &State);
55
56static bool
57CC_Hexagon32(unsigned ValNo, MVT ValVT,
58 MVT LocVT, CCValAssign::LocInfo LocInfo,
59 ISD::ArgFlagsTy ArgFlags, CCState &State);
60
61static bool
62CC_Hexagon64(unsigned ValNo, MVT ValVT,
63 MVT LocVT, CCValAssign::LocInfo LocInfo,
64 ISD::ArgFlagsTy ArgFlags, CCState &State);
65
66static bool
67RetCC_Hexagon(unsigned ValNo, MVT ValVT,
68 MVT LocVT, CCValAssign::LocInfo LocInfo,
69 ISD::ArgFlagsTy ArgFlags, CCState &State);
70
71static bool
72RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
73 MVT LocVT, CCValAssign::LocInfo LocInfo,
74 ISD::ArgFlagsTy ArgFlags, CCState &State);
75
76static bool
77RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
78 MVT LocVT, CCValAssign::LocInfo LocInfo,
79 ISD::ArgFlagsTy ArgFlags, CCState &State);
80
81static bool
82CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
83 MVT LocVT, CCValAssign::LocInfo LocInfo,
84 ISD::ArgFlagsTy ArgFlags, CCState &State) {
85
86 // NumNamedVarArgParams can not be zero for a VarArg function.
87 assert ( (NumNamedVarArgParams > 0) &&
88 "NumNamedVarArgParams is not bigger than zero.");
89
90 if ( (int)ValNo < NumNamedVarArgParams ) {
91 // Deal with named arguments.
92 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
93 }
94
95 // Deal with un-named arguments.
96 unsigned ofst;
97 if (ArgFlags.isByVal()) {
98 // If pass-by-value, the size allocated on stack is decided
99 // by ArgFlags.getByValSize(), not by the size of LocVT.
100 assert ((ArgFlags.getByValSize() > 8) &&
101 "ByValSize must be bigger than 8 bytes");
102 ofst = State.AllocateStack(ArgFlags.getByValSize(), 4);
103 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
104 return false;
105 }
Sirish Pande7517bbc2012-05-10 20:20:25 +0000106 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000107 ofst = State.AllocateStack(4, 4);
108 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
109 return false;
110 }
Sirish Pande7517bbc2012-05-10 20:20:25 +0000111 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000112 ofst = State.AllocateStack(8, 8);
113 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
114 return false;
115 }
116 llvm_unreachable(0);
Tony Linthicumb4b54152011-12-12 21:14:40 +0000117}
118
119
120static bool
121CC_Hexagon (unsigned ValNo, MVT ValVT,
122 MVT LocVT, CCValAssign::LocInfo LocInfo,
123 ISD::ArgFlagsTy ArgFlags, CCState &State) {
124
125 if (ArgFlags.isByVal()) {
126 // Passed on stack.
127 assert ((ArgFlags.getByValSize() > 8) &&
128 "ByValSize must be bigger than 8 bytes");
129 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 4);
130 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
131 return false;
132 }
133
134 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
135 LocVT = MVT::i32;
136 ValVT = MVT::i32;
137 if (ArgFlags.isSExt())
138 LocInfo = CCValAssign::SExt;
139 else if (ArgFlags.isZExt())
140 LocInfo = CCValAssign::ZExt;
141 else
142 LocInfo = CCValAssign::AExt;
143 }
144
Sirish Pande7517bbc2012-05-10 20:20:25 +0000145 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000146 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
147 return false;
148 }
149
Sirish Pande7517bbc2012-05-10 20:20:25 +0000150 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000151 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
152 return false;
153 }
154
155 return true; // CC didn't match.
156}
157
158
159static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
160 MVT LocVT, CCValAssign::LocInfo LocInfo,
161 ISD::ArgFlagsTy ArgFlags, CCState &State) {
162
Craig Topperc5eaae42012-03-11 07:57:25 +0000163 static const uint16_t RegList[] = {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000164 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
165 Hexagon::R5
166 };
167 if (unsigned Reg = State.AllocateReg(RegList, 6)) {
168 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
169 return false;
170 }
171
172 unsigned Offset = State.AllocateStack(4, 4);
173 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
174 return false;
175}
176
177static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
178 MVT LocVT, CCValAssign::LocInfo LocInfo,
179 ISD::ArgFlagsTy ArgFlags, CCState &State) {
180
181 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
182 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
183 return false;
184 }
185
Craig Topperc5eaae42012-03-11 07:57:25 +0000186 static const uint16_t RegList1[] = {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000187 Hexagon::D1, Hexagon::D2
188 };
Craig Topperc5eaae42012-03-11 07:57:25 +0000189 static const uint16_t RegList2[] = {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000190 Hexagon::R1, Hexagon::R3
191 };
192 if (unsigned Reg = State.AllocateReg(RegList1, RegList2, 2)) {
193 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
194 return false;
195 }
196
197 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
198 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
199 return false;
200}
201
202static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
203 MVT LocVT, CCValAssign::LocInfo LocInfo,
204 ISD::ArgFlagsTy ArgFlags, CCState &State) {
205
206
207 if (LocVT == MVT::i1 ||
208 LocVT == MVT::i8 ||
209 LocVT == MVT::i16) {
210 LocVT = MVT::i32;
211 ValVT = MVT::i32;
212 if (ArgFlags.isSExt())
213 LocInfo = CCValAssign::SExt;
214 else if (ArgFlags.isZExt())
215 LocInfo = CCValAssign::ZExt;
216 else
217 LocInfo = CCValAssign::AExt;
218 }
219
Sirish Pande7517bbc2012-05-10 20:20:25 +0000220 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000221 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
222 return false;
223 }
224
Sirish Pande7517bbc2012-05-10 20:20:25 +0000225 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000226 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
227 return false;
228 }
229
230 return true; // CC didn't match.
231}
232
233static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
234 MVT LocVT, CCValAssign::LocInfo LocInfo,
235 ISD::ArgFlagsTy ArgFlags, CCState &State) {
236
Sirish Pande7517bbc2012-05-10 20:20:25 +0000237 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000238 if (unsigned Reg = State.AllocateReg(Hexagon::R0)) {
239 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
240 return false;
241 }
242 }
243
244 unsigned Offset = State.AllocateStack(4, 4);
245 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
246 return false;
247}
248
249static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
250 MVT LocVT, CCValAssign::LocInfo LocInfo,
251 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande7517bbc2012-05-10 20:20:25 +0000252 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000253 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
254 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
255 return false;
256 }
257 }
258
259 unsigned Offset = State.AllocateStack(8, 8);
260 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
261 return false;
262}
263
264SDValue
265HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
266const {
267 return SDValue();
268}
269
270/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
271/// by "Src" to address "Dst" of size "Size". Alignment information is
272/// specified by the specific parameter attribute. The copy will be passed as
273/// a byval function parameter. Sometimes what we are copying is the end of a
274/// larger object, the part that does not fit in registers.
275static SDValue
276CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
277 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
278 DebugLoc dl) {
279
280 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
281 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
282 /*isVolatile=*/false, /*AlwaysInline=*/false,
283 MachinePointerInfo(), MachinePointerInfo());
284}
285
286
287// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
288// passed by value, the function prototype is modified to return void and
289// the value is stored in memory pointed by a pointer passed by caller.
290SDValue
291HexagonTargetLowering::LowerReturn(SDValue Chain,
292 CallingConv::ID CallConv, bool isVarArg,
293 const SmallVectorImpl<ISD::OutputArg> &Outs,
294 const SmallVectorImpl<SDValue> &OutVals,
295 DebugLoc dl, SelectionDAG &DAG) const {
296
297 // CCValAssign - represent the assignment of the return value to locations.
298 SmallVector<CCValAssign, 16> RVLocs;
299
300 // CCState - Info about the registers and stack slot.
301 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
302 getTargetMachine(), RVLocs, *DAG.getContext());
303
304 // Analyze return values of ISD::RET
305 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
306
Tony Linthicumb4b54152011-12-12 21:14:40 +0000307 // If this is the first return lowered for this function, add the regs to the
308 // liveout set for the function.
309 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
310 for (unsigned i = 0; i != RVLocs.size(); ++i)
311 if (RVLocs[i].isRegLoc())
312 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
313 }
314
315 SDValue Flag;
316 // Copy the result values into the output registers.
317 for (unsigned i = 0; i != RVLocs.size(); ++i) {
318 CCValAssign &VA = RVLocs[i];
Tony Linthicumb4b54152011-12-12 21:14:40 +0000319
320 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
321
322 // Guarantee that all emitted copies are stuck together with flags.
323 Flag = Chain.getValue(1);
324 }
325
326 if (Flag.getNode())
327 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
328
329 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, Chain);
330}
331
332
333
334
335/// LowerCallResult - Lower the result values of an ISD::CALL into the
336/// appropriate copies out of appropriate physical registers. This assumes that
337/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
338/// being lowered. Returns a SDNode with the same number of values as the
339/// ISD::CALL.
340SDValue
341HexagonTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
342 CallingConv::ID CallConv, bool isVarArg,
343 const
344 SmallVectorImpl<ISD::InputArg> &Ins,
345 DebugLoc dl, SelectionDAG &DAG,
346 SmallVectorImpl<SDValue> &InVals,
347 const SmallVectorImpl<SDValue> &OutVals,
348 SDValue Callee) const {
349
350 // Assign locations to each value returned by this call.
351 SmallVector<CCValAssign, 16> RVLocs;
352
353 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
354 getTargetMachine(), RVLocs, *DAG.getContext());
355
356 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
357
358 // Copy all of the result registers out of their specified physreg.
359 for (unsigned i = 0; i != RVLocs.size(); ++i) {
360 Chain = DAG.getCopyFromReg(Chain, dl,
361 RVLocs[i].getLocReg(),
362 RVLocs[i].getValVT(), InFlag).getValue(1);
363 InFlag = Chain.getValue(2);
364 InVals.push_back(Chain.getValue(0));
365 }
366
367 return Chain;
368}
369
370/// LowerCall - Functions arguments are copied from virtual regs to
371/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
372SDValue
373HexagonTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
374 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +0000375 bool doesNotRet, bool &isTailCall,
Tony Linthicumb4b54152011-12-12 21:14:40 +0000376 const SmallVectorImpl<ISD::OutputArg> &Outs,
377 const SmallVectorImpl<SDValue> &OutVals,
378 const SmallVectorImpl<ISD::InputArg> &Ins,
379 DebugLoc dl, SelectionDAG &DAG,
380 SmallVectorImpl<SDValue> &InVals) const {
381
382 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
383
384 // Analyze operands of the call, assigning locations to each operand.
385 SmallVector<CCValAssign, 16> ArgLocs;
386 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
387 getTargetMachine(), ArgLocs, *DAG.getContext());
388
389 // Check for varargs.
390 NumNamedVarArgParams = -1;
391 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Callee))
392 {
393 const Function* CalleeFn = NULL;
394 Callee = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, MVT::i32);
395 if ((CalleeFn = dyn_cast<Function>(GA->getGlobal())))
396 {
397 // If a function has zero args and is a vararg function, that's
398 // disallowed so it must be an undeclared function. Do not assume
399 // varargs if the callee is undefined.
400 if (CalleeFn->isVarArg() &&
401 CalleeFn->getFunctionType()->getNumParams() != 0) {
402 NumNamedVarArgParams = CalleeFn->getFunctionType()->getNumParams();
403 }
404 }
405 }
406
407 if (NumNamedVarArgParams > 0)
408 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
409 else
410 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
411
412
413 if(isTailCall) {
414 bool StructAttrFlag =
415 DAG.getMachineFunction().getFunction()->hasStructRetAttr();
416 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
417 isVarArg, IsStructRet,
418 StructAttrFlag,
419 Outs, OutVals, Ins, DAG);
420 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i){
421 CCValAssign &VA = ArgLocs[i];
422 if (VA.isMemLoc()) {
423 isTailCall = false;
424 break;
425 }
426 }
427 if (isTailCall) {
428 DEBUG(dbgs () << "Eligible for Tail Call\n");
429 } else {
430 DEBUG(dbgs () <<
431 "Argument must be passed on stack. Not eligible for Tail Call\n");
432 }
433 }
434 // Get a count of how many bytes are to be pushed on the stack.
435 unsigned NumBytes = CCInfo.getNextStackOffset();
436 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
437 SmallVector<SDValue, 8> MemOpChains;
438
439 SDValue StackPtr =
440 DAG.getCopyFromReg(Chain, dl, TM.getRegisterInfo()->getStackRegister(),
441 getPointerTy());
442
443 // Walk the register/memloc assignments, inserting copies/loads.
444 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
445 CCValAssign &VA = ArgLocs[i];
446 SDValue Arg = OutVals[i];
447 ISD::ArgFlagsTy Flags = Outs[i].Flags;
448
449 // Promote the value if needed.
450 switch (VA.getLocInfo()) {
451 default:
452 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Topperbc219812012-02-07 02:50:20 +0000453 llvm_unreachable("Unknown loc info!");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000454 case CCValAssign::Full:
455 break;
456 case CCValAssign::SExt:
457 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
458 break;
459 case CCValAssign::ZExt:
460 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
461 break;
462 case CCValAssign::AExt:
463 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
464 break;
465 }
466
467 if (VA.isMemLoc()) {
468 unsigned LocMemOffset = VA.getLocMemOffset();
469 SDValue PtrOff = DAG.getConstant(LocMemOffset, StackPtr.getValueType());
470 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
471
472 if (Flags.isByVal()) {
473 // The argument is a struct passed by value. According to LLVM, "Arg"
474 // is is pointer.
475 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, PtrOff, Chain,
476 Flags, DAG, dl));
477 } else {
478 // The argument is not passed by value. "Arg" is a buildin type. It is
479 // not a pointer.
480 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
481 MachinePointerInfo(),false, false,
482 0));
483 }
484 continue;
485 }
486
487 // Arguments that can be passed on register must be kept at RegsToPass
488 // vector.
489 if (VA.isRegLoc()) {
490 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
491 }
492 }
493
494 // Transform all store nodes into one single node because all store
495 // nodes are independent of each other.
496 if (!MemOpChains.empty()) {
497 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &MemOpChains[0],
498 MemOpChains.size());
499 }
500
501 if (!isTailCall)
502 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes,
503 getPointerTy(), true));
504
505 // Build a sequence of copy-to-reg nodes chained together with token
506 // chain and flag operands which copy the outgoing args into registers.
507 // The InFlag in necessary since all emited instructions must be
508 // stuck together.
509 SDValue InFlag;
510 if (!isTailCall) {
511 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
512 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
513 RegsToPass[i].second, InFlag);
514 InFlag = Chain.getValue(1);
515 }
516 }
517
518 // For tail calls lower the arguments to the 'real' stack slot.
519 if (isTailCall) {
520 // Force all the incoming stack arguments to be loaded from the stack
521 // before any new outgoing arguments are stored to the stack, because the
522 // outgoing stack slots may alias the incoming argument stack slots, and
523 // the alias isn't otherwise explicit. This is slightly more conservative
524 // than necessary, because it means that each store effectively depends
525 // on every argument instead of just those arguments it would clobber.
526 //
527 // Do not flag preceeding copytoreg stuff together with the following stuff.
528 InFlag = SDValue();
529 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
530 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
531 RegsToPass[i].second, InFlag);
532 InFlag = Chain.getValue(1);
533 }
534 InFlag =SDValue();
535 }
536
537 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
538 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
539 // node so that legalize doesn't hack it.
540 if (flag_aligned_memcpy) {
541 const char *MemcpyName =
542 "__hexagon_memcpy_likely_aligned_min32bytes_mult8bytes";
543 Callee =
544 DAG.getTargetExternalSymbol(MemcpyName, getPointerTy());
545 flag_aligned_memcpy = false;
546 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
547 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy());
548 } else if (ExternalSymbolSDNode *S =
549 dyn_cast<ExternalSymbolSDNode>(Callee)) {
550 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
551 }
552
553 // Returns a chain & a flag for retval copy to use.
554 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
555 SmallVector<SDValue, 8> Ops;
556 Ops.push_back(Chain);
557 Ops.push_back(Callee);
558
559 // Add argument registers to the end of the list so that they are
560 // known live into the call.
561 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
562 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
563 RegsToPass[i].second.getValueType()));
564 }
565
566 if (InFlag.getNode()) {
567 Ops.push_back(InFlag);
568 }
569
570 if (isTailCall)
571 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
572
573 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
574 InFlag = Chain.getValue(1);
575
576 // Create the CALLSEQ_END node.
577 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
578 DAG.getIntPtrConstant(0, true), InFlag);
579 InFlag = Chain.getValue(1);
580
581 // Handle result values, copying them out of physregs into vregs that we
582 // return.
583 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
584 InVals, OutVals, Callee);
585}
586
587static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
588 bool isSEXTLoad, SDValue &Base,
589 SDValue &Offset, bool &isInc,
590 SelectionDAG &DAG) {
591 if (Ptr->getOpcode() != ISD::ADD)
592 return false;
593
594 if (VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
595 isInc = (Ptr->getOpcode() == ISD::ADD);
596 Base = Ptr->getOperand(0);
597 Offset = Ptr->getOperand(1);
598 // Ensure that Offset is a constant.
599 return (isa<ConstantSDNode>(Offset));
600 }
601
602 return false;
603}
604
605// TODO: Put this function along with the other isS* functions in
606// HexagonISelDAGToDAG.cpp into a common file. Or better still, use the
607// functions defined in HexagonImmediates.td.
608static bool Is_PostInc_S4_Offset(SDNode * S, int ShiftAmount) {
609 ConstantSDNode *N = cast<ConstantSDNode>(S);
610
611 // immS4 predicate - True if the immediate fits in a 4-bit sign extended.
612 // field.
613 int64_t v = (int64_t)N->getSExtValue();
614 int64_t m = 0;
615 if (ShiftAmount > 0) {
616 m = v % ShiftAmount;
617 v = v >> ShiftAmount;
618 }
619 return (v <= 7) && (v >= -8) && (m == 0);
620}
621
622/// getPostIndexedAddressParts - returns true by value, base pointer and
623/// offset pointer and addressing mode by reference if this node can be
624/// combined with a load / store to form a post-indexed load / store.
625bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
626 SDValue &Base,
627 SDValue &Offset,
628 ISD::MemIndexedMode &AM,
629 SelectionDAG &DAG) const
630{
631 EVT VT;
632 SDValue Ptr;
633 bool isSEXTLoad = false;
634
635 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
636 VT = LD->getMemoryVT();
637 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
638 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
639 VT = ST->getMemoryVT();
640 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
641 return false;
642 }
643 } else {
644 return false;
645 }
646
Chad Rosier6da0ef92012-01-06 20:11:59 +0000647 bool isInc = false;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000648 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
649 isInc, DAG);
650 // ShiftAmount = number of left-shifted bits in the Hexagon instruction.
651 int ShiftAmount = VT.getSizeInBits() / 16;
652 if (isLegal && Is_PostInc_S4_Offset(Offset.getNode(), ShiftAmount)) {
653 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
654 return true;
655 }
656
657 return false;
658}
659
660SDValue HexagonTargetLowering::LowerINLINEASM(SDValue Op,
661 SelectionDAG &DAG) const {
662 SDNode *Node = Op.getNode();
663 MachineFunction &MF = DAG.getMachineFunction();
664 HexagonMachineFunctionInfo *FuncInfo =
665 MF.getInfo<HexagonMachineFunctionInfo>();
666 switch (Node->getOpcode()) {
667 case ISD::INLINEASM: {
668 unsigned NumOps = Node->getNumOperands();
669 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
670 --NumOps; // Ignore the flag operand.
671
672 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
673 if (FuncInfo->hasClobberLR())
674 break;
675 unsigned Flags =
676 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
677 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
678 ++i; // Skip the ID value.
679
680 switch (InlineAsm::getKind(Flags)) {
681 default: llvm_unreachable("Bad flags!");
682 case InlineAsm::Kind_RegDef:
683 case InlineAsm::Kind_RegUse:
684 case InlineAsm::Kind_Imm:
685 case InlineAsm::Kind_Clobber:
686 case InlineAsm::Kind_Mem: {
687 for (; NumVals; --NumVals, ++i) {}
688 break;
689 }
690 case InlineAsm::Kind_RegDefEarlyClobber: {
691 for (; NumVals; --NumVals, ++i) {
692 unsigned Reg =
693 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
694
695 // Check it to be lr
696 if (Reg == TM.getRegisterInfo()->getRARegister()) {
697 FuncInfo->setHasClobberLR(true);
698 break;
699 }
700 }
701 break;
702 }
703 }
704 }
705 }
706 } // Node->getOpcode
707 return Op;
708}
709
710
711//
712// Taken from the XCore backend.
713//
714SDValue HexagonTargetLowering::
715LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
716{
717 SDValue Chain = Op.getOperand(0);
718 SDValue Table = Op.getOperand(1);
719 SDValue Index = Op.getOperand(2);
720 DebugLoc dl = Op.getDebugLoc();
721 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
722 unsigned JTI = JT->getIndex();
723 MachineFunction &MF = DAG.getMachineFunction();
724 const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
725 SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
726
727 // Mark all jump table targets as address taken.
728 const std::vector<MachineJumpTableEntry> &JTE = MJTI->getJumpTables();
729 const std::vector<MachineBasicBlock*> &JTBBs = JTE[JTI].MBBs;
730 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
731 MachineBasicBlock *MBB = JTBBs[i];
732 MBB->setHasAddressTaken();
733 // This line is needed to set the hasAddressTaken flag on the BasicBlock
734 // object.
735 BlockAddress::get(const_cast<BasicBlock *>(MBB->getBasicBlock()));
736 }
737
738 SDValue JumpTableBase = DAG.getNode(HexagonISD::WrapperJT, dl,
739 getPointerTy(), TargetJT);
740 SDValue ShiftIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
741 DAG.getConstant(2, MVT::i32));
742 SDValue JTAddress = DAG.getNode(ISD::ADD, dl, MVT::i32, JumpTableBase,
743 ShiftIndex);
744 SDValue LoadTarget = DAG.getLoad(MVT::i32, dl, Chain, JTAddress,
745 MachinePointerInfo(), false, false, false,
746 0);
747 return DAG.getNode(HexagonISD::BR_JT, dl, MVT::Other, Chain, LoadTarget);
748}
749
750
751SDValue
752HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
753 SelectionDAG &DAG) const {
754 SDValue Chain = Op.getOperand(0);
755 SDValue Size = Op.getOperand(1);
756 DebugLoc dl = Op.getDebugLoc();
757
758 unsigned SPReg = getStackPointerRegisterToSaveRestore();
759
760 // Get a reference to the stack pointer.
761 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
762
763 // Subtract the dynamic size from the actual stack size to
764 // obtain the new stack size.
765 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
766
767 //
768 // For Hexagon, the outgoing memory arguments area should be on top of the
769 // alloca area on the stack i.e., the outgoing memory arguments should be
770 // at a lower address than the alloca area. Move the alloca area down the
771 // stack by adding back the space reserved for outgoing arguments to SP
772 // here.
773 //
774 // We do not know what the size of the outgoing args is at this point.
775 // So, we add a pseudo instruction ADJDYNALLOC that will adjust the
776 // stack pointer. We patch this instruction with the correct, known
777 // offset in emitPrologue().
778 //
779 // Use a placeholder immediate (zero) for now. This will be patched up
780 // by emitPrologue().
781 SDValue ArgAdjust = DAG.getNode(HexagonISD::ADJDYNALLOC, dl,
782 MVT::i32,
783 Sub,
784 DAG.getConstant(0, MVT::i32));
785
786 // The Sub result contains the new stack start address, so it
787 // must be placed in the stack pointer register.
788 SDValue CopyChain = DAG.getCopyToReg(Chain, dl,
789 TM.getRegisterInfo()->getStackRegister(),
790 Sub);
791
792 SDValue Ops[2] = { ArgAdjust, CopyChain };
793 return DAG.getMergeValues(Ops, 2, dl);
794}
795
796SDValue
797HexagonTargetLowering::LowerFormalArguments(SDValue Chain,
798 CallingConv::ID CallConv,
799 bool isVarArg,
800 const
801 SmallVectorImpl<ISD::InputArg> &Ins,
802 DebugLoc dl, SelectionDAG &DAG,
803 SmallVectorImpl<SDValue> &InVals)
804const {
805
806 MachineFunction &MF = DAG.getMachineFunction();
807 MachineFrameInfo *MFI = MF.getFrameInfo();
808 MachineRegisterInfo &RegInfo = MF.getRegInfo();
809 HexagonMachineFunctionInfo *FuncInfo =
810 MF.getInfo<HexagonMachineFunctionInfo>();
811
812
813 // Assign locations to all of the incoming arguments.
814 SmallVector<CCValAssign, 16> ArgLocs;
815 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
816 getTargetMachine(), ArgLocs, *DAG.getContext());
817
818 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
819
820 // For LLVM, in the case when returning a struct by value (>8byte),
821 // the first argument is a pointer that points to the location on caller's
822 // stack where the return value will be stored. For Hexagon, the location on
823 // caller's stack is passed only when the struct size is smaller than (and
824 // equal to) 8 bytes. If not, no address will be passed into callee and
825 // callee return the result direclty through R0/R1.
826
827 SmallVector<SDValue, 4> MemOps;
828
829 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
830 CCValAssign &VA = ArgLocs[i];
831 ISD::ArgFlagsTy Flags = Ins[i].Flags;
832 unsigned ObjSize;
833 unsigned StackLocation;
834 int FI;
835
836 if ( (VA.isRegLoc() && !Flags.isByVal())
837 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
838 // Arguments passed in registers
839 // 1. int, long long, ptr args that get allocated in register.
840 // 2. Large struct that gets an register to put its address in.
841 EVT RegVT = VA.getLocVT();
Sirish Pande7517bbc2012-05-10 20:20:25 +0000842 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
843 RegVT == MVT::i32 || RegVT == MVT::f32) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000844 unsigned VReg =
Craig Topper420761a2012-04-20 07:30:17 +0000845 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicumb4b54152011-12-12 21:14:40 +0000846 RegInfo.addLiveIn(VA.getLocReg(), VReg);
847 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Chandler Carruth37097622012-04-18 21:31:19 +0000848 } else if (RegVT == MVT::i64) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000849 unsigned VReg =
Craig Topper420761a2012-04-20 07:30:17 +0000850 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicumb4b54152011-12-12 21:14:40 +0000851 RegInfo.addLiveIn(VA.getLocReg(), VReg);
852 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
853 } else {
854 assert (0);
855 }
856 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
857 assert (0 && "ByValSize must be bigger than 8 bytes");
858 } else {
859 // Sanity check.
860 assert(VA.isMemLoc());
861
862 if (Flags.isByVal()) {
863 // If it's a byval parameter, then we need to compute the
864 // "real" size, not the size of the pointer.
865 ObjSize = Flags.getByValSize();
866 } else {
867 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
868 }
869
870 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
871 // Create the frame index object for this incoming parameter...
872 FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
873
874 // Create the SelectionDAG nodes cordl, responding to a load
875 // from this parameter.
876 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
877
878 if (Flags.isByVal()) {
879 // If it's a pass-by-value aggregate, then do not dereference the stack
880 // location. Instead, we should generate a reference to the stack
881 // location.
882 InVals.push_back(FIN);
883 } else {
884 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
885 MachinePointerInfo(), false, false,
886 false, 0));
887 }
888 }
889 }
890
891 if (!MemOps.empty())
892 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &MemOps[0],
893 MemOps.size());
894
895 if (isVarArg) {
896 // This will point to the next argument passed via stack.
897 int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
898 HEXAGON_LRFP_SIZE +
899 CCInfo.getNextStackOffset(),
900 true);
901 FuncInfo->setVarArgsFrameIndex(FrameIndex);
902 }
903
904 return Chain;
905}
906
907SDValue
908HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
909 // VASTART stores the address of the VarArgsFrameIndex slot into the
910 // memory location argument.
911 MachineFunction &MF = DAG.getMachineFunction();
912 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
913 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
914 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
915 return DAG.getStore(Op.getOperand(0), Op.getDebugLoc(), Addr,
916 Op.getOperand(1), MachinePointerInfo(SV), false,
917 false, 0);
918}
919
920SDValue
921HexagonTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Sirish Pande7517bbc2012-05-10 20:20:25 +0000922 SDValue LHS = Op.getOperand(0);
923 SDValue RHS = Op.getOperand(1);
924 SDValue CC = Op.getOperand(4);
925 SDValue TrueVal = Op.getOperand(2);
926 SDValue FalseVal = Op.getOperand(3);
927 DebugLoc dl = Op.getDebugLoc();
Tony Linthicumb4b54152011-12-12 21:14:40 +0000928 SDNode* OpNode = Op.getNode();
Sirish Pande7517bbc2012-05-10 20:20:25 +0000929 EVT SVT = OpNode->getValueType(0);
Tony Linthicumb4b54152011-12-12 21:14:40 +0000930
Sirish Pande7517bbc2012-05-10 20:20:25 +0000931 SDValue Cond = DAG.getNode(ISD::SETCC, dl, MVT::i1, LHS, RHS, CC);
932 return DAG.getNode(ISD::SELECT, dl, SVT, Cond, TrueVal, FalseVal);
933}
934
935SDValue
936HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
937 EVT ValTy = Op.getValueType();
938
939 DebugLoc dl = Op.getDebugLoc();
940 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
941 SDValue Res;
942 if (CP->isMachineConstantPoolEntry())
943 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), ValTy,
944 CP->getAlignment());
945 else
946 Res = DAG.getTargetConstantPool(CP->getConstVal(), ValTy,
947 CP->getAlignment());
948 return DAG.getNode(HexagonISD::CONST32, dl, ValTy, Res);
Tony Linthicumb4b54152011-12-12 21:14:40 +0000949}
950
951SDValue
952HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
953 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
954 MachineFunction &MF = DAG.getMachineFunction();
955 MachineFrameInfo *MFI = MF.getFrameInfo();
956 MFI->setReturnAddressIsTaken(true);
957
958 EVT VT = Op.getValueType();
959 DebugLoc dl = Op.getDebugLoc();
960 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
961 if (Depth) {
962 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
963 SDValue Offset = DAG.getConstant(4, MVT::i32);
964 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
965 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
966 MachinePointerInfo(), false, false, false, 0);
967 }
968
969 // Return LR, which contains the return address. Mark it an implicit live-in.
970 unsigned Reg = MF.addLiveIn(TRI->getRARegister(), getRegClassFor(MVT::i32));
971 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
972}
973
974SDValue
975HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
976 const HexagonRegisterInfo *TRI = TM.getRegisterInfo();
977 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
978 MFI->setFrameAddressIsTaken(true);
979
980 EVT VT = Op.getValueType();
981 DebugLoc dl = Op.getDebugLoc();
982 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
983 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
984 TRI->getFrameRegister(), VT);
985 while (Depth--)
986 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
987 MachinePointerInfo(),
988 false, false, false, 0);
989 return FrameAddr;
990}
991
992
993SDValue HexagonTargetLowering::LowerMEMBARRIER(SDValue Op,
994 SelectionDAG& DAG) const {
995 DebugLoc dl = Op.getDebugLoc();
996 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
997}
998
999
1000SDValue HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1001 SelectionDAG& DAG) const {
1002 DebugLoc dl = Op.getDebugLoc();
1003 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1004}
1005
1006
1007SDValue HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op,
1008 SelectionDAG &DAG) const {
1009 SDValue Result;
1010 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1011 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
1012 DebugLoc dl = Op.getDebugLoc();
1013 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
1014
1015 HexagonTargetObjectFile &TLOF =
1016 (HexagonTargetObjectFile&)getObjFileLowering();
1017 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1018 return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), Result);
1019 }
1020
1021 return DAG.getNode(HexagonISD::CONST32, dl, getPointerTy(), Result);
1022}
1023
1024//===----------------------------------------------------------------------===//
1025// TargetLowering Implementation
1026//===----------------------------------------------------------------------===//
1027
1028HexagonTargetLowering::HexagonTargetLowering(HexagonTargetMachine
1029 &targetmachine)
1030 : TargetLowering(targetmachine, new HexagonTargetObjectFile()),
1031 TM(targetmachine) {
1032
Sirish Pande7517bbc2012-05-10 20:20:25 +00001033 const HexagonRegisterInfo* QRI = TM.getRegisterInfo();
1034
Tony Linthicumb4b54152011-12-12 21:14:40 +00001035 // Set up the register classes.
Craig Topper420761a2012-04-20 07:30:17 +00001036 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1037 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001038
Sirish Pande7517bbc2012-05-10 20:20:25 +00001039 if (QRI->Subtarget.hasV5TOps()) {
1040 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1041 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1042 }
1043
Craig Topper420761a2012-04-20 07:30:17 +00001044 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001045
1046 computeRegisterProperties();
1047
1048 // Align loop entry
1049 setPrefLoopAlignment(4);
1050
1051 // Limits for inline expansion of memcpy/memmove
1052 maxStoresPerMemcpy = 6;
1053 maxStoresPerMemmove = 6;
1054
1055 //
1056 // Library calls for unsupported operations
1057 //
Tony Linthicumb4b54152011-12-12 21:14:40 +00001058
Tony Linthicumb4b54152011-12-12 21:14:40 +00001059 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1060 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
Tony Linthicumb4b54152011-12-12 21:14:40 +00001061
Tony Linthicumb4b54152011-12-12 21:14:40 +00001062 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
Tony Linthicumb4b54152011-12-12 21:14:40 +00001063 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1064
Tony Linthicumb4b54152011-12-12 21:14:40 +00001065 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
Tony Linthicumb4b54152011-12-12 21:14:40 +00001066 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
1067
Tony Linthicumb4b54152011-12-12 21:14:40 +00001068 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1069 setOperationAction(ISD::SDIV, MVT::i32, Expand);
1070 setLibcallName(RTLIB::SREM_I32, "__hexagon_umodsi3");
1071 setOperationAction(ISD::SREM, MVT::i32, Expand);
1072
1073 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1074 setOperationAction(ISD::SDIV, MVT::i64, Expand);
1075 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1076 setOperationAction(ISD::SREM, MVT::i64, Expand);
1077
1078 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1079 setOperationAction(ISD::UDIV, MVT::i32, Expand);
1080
1081 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1082 setOperationAction(ISD::UDIV, MVT::i64, Expand);
1083
1084 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1085 setOperationAction(ISD::UREM, MVT::i32, Expand);
1086
1087 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
1088 setOperationAction(ISD::UREM, MVT::i64, Expand);
1089
1090 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1091 setOperationAction(ISD::FDIV, MVT::f32, Expand);
1092
1093 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1094 setOperationAction(ISD::FDIV, MVT::f64, Expand);
1095
Sirish Pande7517bbc2012-05-10 20:20:25 +00001096 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
1097 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
1098 setOperationAction(ISD::FSIN, MVT::f32, Expand);
1099 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001100
Sirish Pande7517bbc2012-05-10 20:20:25 +00001101 if (QRI->Subtarget.hasV5TOps()) {
1102 // Hexagon V5 Support.
1103 setOperationAction(ISD::FADD, MVT::f32, Legal);
1104 setOperationAction(ISD::FADD, MVT::f64, Legal);
1105 setOperationAction(ISD::FP_EXTEND, MVT::f32, Legal);
1106 setCondCodeAction(ISD::SETOEQ, MVT::f32, Legal);
1107 setCondCodeAction(ISD::SETOEQ, MVT::f64, Legal);
1108 setCondCodeAction(ISD::SETUEQ, MVT::f32, Legal);
1109 setCondCodeAction(ISD::SETUEQ, MVT::f64, Legal);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001110
Sirish Pande7517bbc2012-05-10 20:20:25 +00001111 setCondCodeAction(ISD::SETOGE, MVT::f32, Legal);
1112 setCondCodeAction(ISD::SETOGE, MVT::f64, Legal);
1113 setCondCodeAction(ISD::SETUGE, MVT::f32, Legal);
1114 setCondCodeAction(ISD::SETUGE, MVT::f64, Legal);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001115
Sirish Pande7517bbc2012-05-10 20:20:25 +00001116 setCondCodeAction(ISD::SETOGT, MVT::f32, Legal);
1117 setCondCodeAction(ISD::SETOGT, MVT::f64, Legal);
1118 setCondCodeAction(ISD::SETUGT, MVT::f32, Legal);
1119 setCondCodeAction(ISD::SETUGT, MVT::f64, Legal);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001120
Sirish Pande7517bbc2012-05-10 20:20:25 +00001121 setCondCodeAction(ISD::SETOLE, MVT::f32, Legal);
1122 setCondCodeAction(ISD::SETOLE, MVT::f64, Legal);
1123 setCondCodeAction(ISD::SETOLT, MVT::f32, Legal);
1124 setCondCodeAction(ISD::SETOLT, MVT::f64, Legal);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001125
Sirish Pande7517bbc2012-05-10 20:20:25 +00001126 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
1127 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001128
Sirish Pande7517bbc2012-05-10 20:20:25 +00001129 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1130 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1131 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1132 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001133
Sirish Pande7517bbc2012-05-10 20:20:25 +00001134 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1135 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1136 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1137 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001138
Sirish Pande7517bbc2012-05-10 20:20:25 +00001139 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1140 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1141 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1142 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001143
Sirish Pande7517bbc2012-05-10 20:20:25 +00001144 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Legal);
1145 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Legal);
1146 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Legal);
1147 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Legal);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001148
Sirish Pande7517bbc2012-05-10 20:20:25 +00001149 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Legal);
1150 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Legal);
1151 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Legal);
1152 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Legal);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001153
Sirish Pande7517bbc2012-05-10 20:20:25 +00001154 setOperationAction(ISD::FABS, MVT::f32, Legal);
1155 setOperationAction(ISD::FABS, MVT::f64, Expand);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001156
Sirish Pande7517bbc2012-05-10 20:20:25 +00001157 setOperationAction(ISD::FNEG, MVT::f32, Legal);
1158 setOperationAction(ISD::FNEG, MVT::f64, Expand);
1159 } else {
Tony Linthicumb4b54152011-12-12 21:14:40 +00001160
Sirish Pande7517bbc2012-05-10 20:20:25 +00001161 // Expand fp<->uint.
1162 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Expand);
1163 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001164
Sirish Pande7517bbc2012-05-10 20:20:25 +00001165 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
1166 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001167
Sirish Pande7517bbc2012-05-10 20:20:25 +00001168 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
1169 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
1170
1171 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
1172 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
1173
1174 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
1175 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
1176
1177 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
1178 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
1179
1180 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
1181 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
1182
1183 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
1184 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
1185
1186 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
1187 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
1188
1189 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1190 setOperationAction(ISD::FADD, MVT::f64, Expand);
1191
1192 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
1193 setOperationAction(ISD::FADD, MVT::f32, Expand);
1194
1195 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
1196 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
1197
1198 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
1199 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
1200
1201 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
1202 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
1203
1204 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
1205 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
1206
1207 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
1208 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
1209
1210 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
1211 setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
1212
1213 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
1214 setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
1215
1216 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
1217 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
1218
1219 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
1220 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
1221
1222 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
1223 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
1224
1225 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
1226 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
1227
1228 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
1229 setCondCodeAction(ISD::SETOLT, MVT::f64, Expand);
1230
1231 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
1232 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
1233
1234 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1235 setOperationAction(ISD::FMUL, MVT::f64, Expand);
1236
1237 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
1238 setOperationAction(ISD::MUL, MVT::f32, Expand);
1239
1240 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
1241 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
1242
1243 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
1244
1245 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1246 setOperationAction(ISD::SUB, MVT::f64, Expand);
1247
1248 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
1249 setOperationAction(ISD::SUB, MVT::f32, Expand);
1250
1251 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
1252 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
1253
1254 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
1255 setCondCodeAction(ISD::SETUO, MVT::f64, Expand);
1256
1257 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
1258 setCondCodeAction(ISD::SETO, MVT::f64, Expand);
1259
1260 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
1261 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
1262
1263 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
1264 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
1265
1266 setOperationAction(ISD::FABS, MVT::f32, Expand);
1267 setOperationAction(ISD::FABS, MVT::f64, Expand);
1268 setOperationAction(ISD::FNEG, MVT::f32, Expand);
1269 setOperationAction(ISD::FNEG, MVT::f64, Expand);
1270 }
Tony Linthicumb4b54152011-12-12 21:14:40 +00001271
1272 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1273 setOperationAction(ISD::SREM, MVT::i32, Expand);
1274
Tony Linthicumb4b54152011-12-12 21:14:40 +00001275 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
1276 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
1277 setIndexedLoadAction(ISD::POST_INC, MVT::i32, Legal);
1278 setIndexedLoadAction(ISD::POST_INC, MVT::i64, Legal);
1279
1280 setIndexedStoreAction(ISD::POST_INC, MVT::i8, Legal);
1281 setIndexedStoreAction(ISD::POST_INC, MVT::i16, Legal);
1282 setIndexedStoreAction(ISD::POST_INC, MVT::i32, Legal);
1283 setIndexedStoreAction(ISD::POST_INC, MVT::i64, Legal);
1284
1285 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
1286
1287 // Turn FP extload into load/fextend.
1288 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
1289 // Hexagon has a i1 sign extending load.
1290 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Expand);
1291 // Turn FP truncstore into trunc + store.
1292 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1293
1294 // Custom legalize GlobalAddress nodes into CONST32.
1295 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
1296 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1297 // Truncate action?
1298 setOperationAction(ISD::TRUNCATE, MVT::i64, Expand);
1299
1300 // Hexagon doesn't have sext_inreg, replace them with shl/sra.
1301 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
1302
1303 // Hexagon has no REM or DIVREM operations.
1304 setOperationAction(ISD::UREM, MVT::i32, Expand);
1305 setOperationAction(ISD::SREM, MVT::i32, Expand);
1306 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1307 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
1308 setOperationAction(ISD::SREM, MVT::i64, Expand);
1309 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1310 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1311
1312 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
1313
Tony Linthicumb4b54152011-12-12 21:14:40 +00001314 // Lower SELECT_CC to SETCC and SELECT.
1315 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1316 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Sirish Pande7517bbc2012-05-10 20:20:25 +00001317
1318 if (QRI->Subtarget.hasV5TOps()) {
1319
1320 // We need to make the operation type of SELECT node to be Custom,
1321 // such that we don't go into the infinite loop of
1322 // select -> setcc -> select_cc -> select loop.
1323 setOperationAction(ISD::SELECT, MVT::f32, Custom);
1324 setOperationAction(ISD::SELECT, MVT::f64, Custom);
1325
1326 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
1327 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
1328 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
1329
1330 } else {
1331
1332 // Hexagon has no select or setcc: expand to SELECT_CC.
1333 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1334 setOperationAction(ISD::SELECT, MVT::f64, Expand);
1335
1336 // This is a workaround documented in DAGCombiner.cpp:2892 We don't
1337 // support SELECT_CC on every type.
1338 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
1339
1340 }
Tony Linthicumb4b54152011-12-12 21:14:40 +00001341
1342 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
1343 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1344 if (EmitJumpTables) {
1345 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
1346 } else {
1347 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1348 }
1349
1350 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
1351
1352 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
1353 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
1354
1355 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1356 setOperationAction(ISD::FCOS , MVT::f64, Expand);
1357 setOperationAction(ISD::FREM , MVT::f64, Expand);
1358 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1359 setOperationAction(ISD::FCOS , MVT::f32, Expand);
1360 setOperationAction(ISD::FREM , MVT::f32, Expand);
1361 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1362 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +00001363 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001364 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +00001365 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001366 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1367 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1368 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
1369 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1370 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
1371 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1372 setOperationAction(ISD::FPOW , MVT::f32, Expand);
1373
1374 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1375 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1376 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
1377
1378 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1379 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
1380
1381 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1382 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1383
1384 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
1385 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
1386 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
1387 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
1388
1389 setOperationAction(ISD::EH_RETURN, MVT::Other, Expand);
1390
1391 if (TM.getSubtargetImpl()->isSubtargetV2()) {
1392 setExceptionPointerRegister(Hexagon::R20);
1393 setExceptionSelectorRegister(Hexagon::R21);
1394 } else {
1395 setExceptionPointerRegister(Hexagon::R0);
1396 setExceptionSelectorRegister(Hexagon::R1);
1397 }
1398
1399 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1400 setOperationAction(ISD::VASTART , MVT::Other, Custom);
1401
1402 // Use the default implementation.
1403 setOperationAction(ISD::VAARG , MVT::Other, Expand);
1404 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1405 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1406 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1407 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1408
1409
1410 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
1411 setOperationAction(ISD::INLINEASM , MVT::Other, Custom);
1412
1413 setMinFunctionAlignment(2);
1414
1415 // Needed for DYNAMIC_STACKALLOC expansion.
1416 unsigned StackRegister = TM.getRegisterInfo()->getStackRegister();
1417 setStackPointerRegisterToSaveRestore(StackRegister);
Andrew Trickee498d32012-02-01 22:13:57 +00001418 setSchedulingPreference(Sched::VLIW);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001419}
1420
1421
1422const char*
1423HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
1424 switch (Opcode) {
1425 default: return 0;
Sirish Pande7517bbc2012-05-10 20:20:25 +00001426 case HexagonISD::CONST32: return "HexagonISD::CONST32";
Tony Linthicumb4b54152011-12-12 21:14:40 +00001427 case HexagonISD::ADJDYNALLOC: return "HexagonISD::ADJDYNALLOC";
Sirish Pande7517bbc2012-05-10 20:20:25 +00001428 case HexagonISD::CMPICC: return "HexagonISD::CMPICC";
1429 case HexagonISD::CMPFCC: return "HexagonISD::CMPFCC";
1430 case HexagonISD::BRICC: return "HexagonISD::BRICC";
1431 case HexagonISD::BRFCC: return "HexagonISD::BRFCC";
1432 case HexagonISD::SELECT_ICC: return "HexagonISD::SELECT_ICC";
1433 case HexagonISD::SELECT_FCC: return "HexagonISD::SELECT_FCC";
1434 case HexagonISD::Hi: return "HexagonISD::Hi";
1435 case HexagonISD::Lo: return "HexagonISD::Lo";
1436 case HexagonISD::FTOI: return "HexagonISD::FTOI";
1437 case HexagonISD::ITOF: return "HexagonISD::ITOF";
1438 case HexagonISD::CALL: return "HexagonISD::CALL";
1439 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
1440 case HexagonISD::BR_JT: return "HexagonISD::BR_JT";
1441 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
Tony Linthicumb4b54152011-12-12 21:14:40 +00001442 }
1443}
1444
1445bool
1446HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
1447 EVT MTy1 = EVT::getEVT(Ty1);
1448 EVT MTy2 = EVT::getEVT(Ty2);
1449 if (!MTy1.isSimple() || !MTy2.isSimple()) {
1450 return false;
1451 }
1452 return ((MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32));
1453}
1454
1455bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1456 if (!VT1.isSimple() || !VT2.isSimple()) {
1457 return false;
1458 }
1459 return ((VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32));
1460}
1461
1462SDValue
1463HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1464 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00001465 default: llvm_unreachable("Should not custom lower this!");
Sirish Pande7517bbc2012-05-10 20:20:25 +00001466 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001467 // Frame & Return address. Currently unimplemented.
Sirish Pande7517bbc2012-05-10 20:20:25 +00001468 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
1469 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001470 case ISD::GlobalTLSAddress:
Craig Topperbc219812012-02-07 02:50:20 +00001471 llvm_unreachable("TLS not implemented for Hexagon.");
Tony Linthicumb4b54152011-12-12 21:14:40 +00001472 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
1473 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
1474 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
1475 case ISD::VASTART: return LowerVASTART(Op, DAG);
1476 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1477
1478 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Sirish Pande7517bbc2012-05-10 20:20:25 +00001479 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
1480 case ISD::SELECT: return Op;
Tony Linthicumb4b54152011-12-12 21:14:40 +00001481 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Sirish Pande7517bbc2012-05-10 20:20:25 +00001482 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001483
1484 }
1485}
1486
1487
1488
1489//===----------------------------------------------------------------------===//
1490// Hexagon Scheduler Hooks
1491//===----------------------------------------------------------------------===//
1492MachineBasicBlock *
1493HexagonTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1494 MachineBasicBlock *BB)
1495const {
1496 switch (MI->getOpcode()) {
1497 case Hexagon::ADJDYNALLOC: {
1498 MachineFunction *MF = BB->getParent();
1499 HexagonMachineFunctionInfo *FuncInfo =
1500 MF->getInfo<HexagonMachineFunctionInfo>();
1501 FuncInfo->addAllocaAdjustInst(MI);
1502 return BB;
1503 }
Craig Topperbc219812012-02-07 02:50:20 +00001504 default: llvm_unreachable("Unexpected instr type to insert");
Tony Linthicumb4b54152011-12-12 21:14:40 +00001505 } // switch
Tony Linthicumb4b54152011-12-12 21:14:40 +00001506}
1507
1508//===----------------------------------------------------------------------===//
1509// Inline Assembly Support
1510//===----------------------------------------------------------------------===//
1511
1512std::pair<unsigned, const TargetRegisterClass*>
1513HexagonTargetLowering::getRegForInlineAsmConstraint(const
1514 std::string &Constraint,
1515 EVT VT) const {
1516 if (Constraint.size() == 1) {
1517 switch (Constraint[0]) {
1518 case 'r': // R0-R31
1519 switch (VT.getSimpleVT().SimpleTy) {
1520 default:
Craig Topperbc219812012-02-07 02:50:20 +00001521 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
Tony Linthicumb4b54152011-12-12 21:14:40 +00001522 case MVT::i32:
1523 case MVT::i16:
1524 case MVT::i8:
Sirish Pande7517bbc2012-05-10 20:20:25 +00001525 case MVT::f32:
Craig Topper420761a2012-04-20 07:30:17 +00001526 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001527 case MVT::i64:
Sirish Pande7517bbc2012-05-10 20:20:25 +00001528 case MVT::f64:
Craig Topper420761a2012-04-20 07:30:17 +00001529 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicumb4b54152011-12-12 21:14:40 +00001530 }
1531 default:
Craig Topperbc219812012-02-07 02:50:20 +00001532 llvm_unreachable("Unknown asm register class");
Tony Linthicumb4b54152011-12-12 21:14:40 +00001533 }
1534 }
1535
1536 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1537}
1538
Sirish Pande7517bbc2012-05-10 20:20:25 +00001539/// isFPImmLegal - Returns true if the target can instruction select the
1540/// specified FP immediate natively. If false, the legalizer will
1541/// materialize the FP immediate as a load from a constant pool.
1542bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1543 const HexagonRegisterInfo* QRI = TM.getRegisterInfo();
1544 return QRI->Subtarget.hasV5TOps();
1545}
1546
Tony Linthicumb4b54152011-12-12 21:14:40 +00001547/// isLegalAddressingMode - Return true if the addressing mode represented by
1548/// AM is legal for this target, for a load/store of the specified type.
1549bool HexagonTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1550 Type *Ty) const {
1551 // Allows a signed-extended 11-bit immediate field.
1552 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1) {
1553 return false;
1554 }
1555
1556 // No global is ever allowed as a base.
1557 if (AM.BaseGV) {
1558 return false;
1559 }
1560
1561 int Scale = AM.Scale;
1562 if (Scale < 0) Scale = -Scale;
1563 switch (Scale) {
1564 case 0: // No scale reg, "r+i", "r", or just "i".
1565 break;
1566 default: // No scaled addressing mode.
1567 return false;
1568 }
1569 return true;
1570}
1571
1572/// isLegalICmpImmediate - Return true if the specified immediate is legal
1573/// icmp immediate, that is the target has icmp instructions which can compare
1574/// a register against the immediate without having to materialize the
1575/// immediate into a register.
1576bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
1577 return Imm >= -512 && Imm <= 511;
1578}
1579
1580/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1581/// for tail call optimization. Targets which want to do tail call
1582/// optimization should implement this function.
1583bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
1584 SDValue Callee,
1585 CallingConv::ID CalleeCC,
1586 bool isVarArg,
1587 bool isCalleeStructRet,
1588 bool isCallerStructRet,
1589 const SmallVectorImpl<ISD::OutputArg> &Outs,
1590 const SmallVectorImpl<SDValue> &OutVals,
1591 const SmallVectorImpl<ISD::InputArg> &Ins,
1592 SelectionDAG& DAG) const {
1593 const Function *CallerF = DAG.getMachineFunction().getFunction();
1594 CallingConv::ID CallerCC = CallerF->getCallingConv();
1595 bool CCMatch = CallerCC == CalleeCC;
1596
1597 // ***************************************************************************
1598 // Look for obvious safe cases to perform tail call optimization that do not
1599 // require ABI changes.
1600 // ***************************************************************************
1601
1602 // If this is a tail call via a function pointer, then don't do it!
1603 if (!(dyn_cast<GlobalAddressSDNode>(Callee))
1604 && !(dyn_cast<ExternalSymbolSDNode>(Callee))) {
1605 return false;
1606 }
1607
1608 // Do not optimize if the calling conventions do not match.
1609 if (!CCMatch)
1610 return false;
1611
1612 // Do not tail call optimize vararg calls.
1613 if (isVarArg)
1614 return false;
1615
1616 // Also avoid tail call optimization if either caller or callee uses struct
1617 // return semantics.
1618 if (isCalleeStructRet || isCallerStructRet)
1619 return false;
1620
1621 // In addition to the cases above, we also disable Tail Call Optimization if
1622 // the calling convention code that at least one outgoing argument needs to
1623 // go on the stack. We cannot check that here because at this point that
1624 // information is not available.
1625 return true;
1626}