blob: f40d9585c02cb1bee940e4496a64ac9b05578117 [file] [log] [blame]
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +00001//===- BlackfinISelLowering.cpp - Blackfin 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 Blackfin uses to lower LLVM code
11// into a selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "BlackfinISelLowering.h"
16#include "BlackfinTargetMachine.h"
17#include "llvm/Function.h"
18#include "llvm/CodeGen/CallingConvLower.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/PseudoSourceValue.h"
24#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/Target/TargetLoweringObjectFile.h"
26#include "llvm/ADT/VectorExtras.h"
27#include "llvm/Support/Debug.h"
28
29using namespace llvm;
30
31//===----------------------------------------------------------------------===//
32// Calling Convention Implementation
33//===----------------------------------------------------------------------===//
34
35#include "BlackfinGenCallingConv.inc"
36
37//===----------------------------------------------------------------------===//
38// TargetLowering Implementation
39//===----------------------------------------------------------------------===//
40
41BlackfinTargetLowering::BlackfinTargetLowering(TargetMachine &TM)
42 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Owen Andersonac9de032009-08-10 22:56:29 +000043 setShiftAmountType(EVT::i16);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +000044 setBooleanContents(ZeroOrOneBooleanContent);
45 setStackPointerRegisterToSaveRestore(BF::SP);
46 setIntDivIsCheap(false);
47
48 // Set up the legal register classes.
Owen Andersonac9de032009-08-10 22:56:29 +000049 addRegisterClass(EVT::i32, BF::DRegisterClass);
50 addRegisterClass(EVT::i16, BF::D16RegisterClass);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +000051
52 computeRegisterProperties();
53
54 // Blackfin doesn't have i1 loads or stores
Owen Andersonac9de032009-08-10 22:56:29 +000055 setLoadExtAction(ISD::EXTLOAD, EVT::i1, Promote);
56 setLoadExtAction(ISD::ZEXTLOAD, EVT::i1, Promote);
57 setLoadExtAction(ISD::SEXTLOAD, EVT::i1, Promote);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +000058
Owen Andersonac9de032009-08-10 22:56:29 +000059 setOperationAction(ISD::GlobalAddress, EVT::i32, Custom);
60 setOperationAction(ISD::JumpTable, EVT::i32, Custom);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +000061
Owen Andersonac9de032009-08-10 22:56:29 +000062 setOperationAction(ISD::SELECT_CC, EVT::Other, Expand);
63 setOperationAction(ISD::BR_JT, EVT::Other, Expand);
64 setOperationAction(ISD::BR_CC, EVT::Other, Expand);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +000065
66 // i16 registers don't do much
Owen Andersonac9de032009-08-10 22:56:29 +000067 setOperationAction(ISD::AND, EVT::i16, Promote);
68 setOperationAction(ISD::OR, EVT::i16, Promote);
69 setOperationAction(ISD::XOR, EVT::i16, Promote);
70 setOperationAction(ISD::CTPOP, EVT::i16, Promote);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +000071 // The expansion of CTLZ/CTTZ uses AND/OR, so we might as well promote
72 // immediately.
Owen Andersonac9de032009-08-10 22:56:29 +000073 setOperationAction(ISD::CTLZ, EVT::i16, Promote);
74 setOperationAction(ISD::CTTZ, EVT::i16, Promote);
75 setOperationAction(ISD::SETCC, EVT::i16, Promote);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +000076
77 // Blackfin has no division
Owen Andersonac9de032009-08-10 22:56:29 +000078 setOperationAction(ISD::SDIV, EVT::i16, Expand);
79 setOperationAction(ISD::SDIV, EVT::i32, Expand);
80 setOperationAction(ISD::SDIVREM, EVT::i16, Expand);
81 setOperationAction(ISD::SDIVREM, EVT::i32, Expand);
82 setOperationAction(ISD::SREM, EVT::i16, Expand);
83 setOperationAction(ISD::SREM, EVT::i32, Expand);
84 setOperationAction(ISD::UDIV, EVT::i16, Expand);
85 setOperationAction(ISD::UDIV, EVT::i32, Expand);
86 setOperationAction(ISD::UDIVREM, EVT::i16, Expand);
87 setOperationAction(ISD::UDIVREM, EVT::i32, Expand);
88 setOperationAction(ISD::UREM, EVT::i16, Expand);
89 setOperationAction(ISD::UREM, EVT::i32, Expand);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +000090
Owen Andersonac9de032009-08-10 22:56:29 +000091 setOperationAction(ISD::SMUL_LOHI, EVT::i32, Expand);
92 setOperationAction(ISD::UMUL_LOHI, EVT::i32, Expand);
93 setOperationAction(ISD::MULHU, EVT::i32, Expand);
94 setOperationAction(ISD::MULHS, EVT::i32, Expand);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +000095
96 // No carry-in operations.
Owen Andersonac9de032009-08-10 22:56:29 +000097 setOperationAction(ISD::ADDE, EVT::i32, Custom);
98 setOperationAction(ISD::SUBE, EVT::i32, Custom);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +000099
100 // Blackfin has no intrinsics for these particular operations.
Owen Andersonac9de032009-08-10 22:56:29 +0000101 setOperationAction(ISD::MEMBARRIER, EVT::Other, Expand);
102 setOperationAction(ISD::BSWAP, EVT::i32, Expand);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000103
Owen Andersonac9de032009-08-10 22:56:29 +0000104 setOperationAction(ISD::SHL_PARTS, EVT::i32, Expand);
105 setOperationAction(ISD::SRA_PARTS, EVT::i32, Expand);
106 setOperationAction(ISD::SRL_PARTS, EVT::i32, Expand);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000107
Owen Andersonac9de032009-08-10 22:56:29 +0000108 setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i1, Expand);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000109
110 // i32 has native CTPOP, but not CTLZ/CTTZ
Owen Andersonac9de032009-08-10 22:56:29 +0000111 setOperationAction(ISD::CTLZ, EVT::i32, Expand);
112 setOperationAction(ISD::CTTZ, EVT::i32, Expand);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000113
Jakob Stoklund Olesen69f1aec2009-08-08 21:42:22 +0000114 // READCYCLECOUNTER needs special type legalization.
Owen Andersonac9de032009-08-10 22:56:29 +0000115 setOperationAction(ISD::READCYCLECOUNTER, EVT::i64, Custom);
Jakob Stoklund Olesen69f1aec2009-08-08 21:42:22 +0000116
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000117 // We don't have line number support yet.
Owen Andersonac9de032009-08-10 22:56:29 +0000118 setOperationAction(ISD::DBG_STOPPOINT, EVT::Other, Expand);
119 setOperationAction(ISD::DEBUG_LOC, EVT::Other, Expand);
120 setOperationAction(ISD::DBG_LABEL, EVT::Other, Expand);
121 setOperationAction(ISD::EH_LABEL, EVT::Other, Expand);
122 setOperationAction(ISD::DECLARE, EVT::Other, Expand);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000123
124 // Use the default implementation.
Owen Andersonac9de032009-08-10 22:56:29 +0000125 setOperationAction(ISD::VACOPY, EVT::Other, Expand);
126 setOperationAction(ISD::VAEND, EVT::Other, Expand);
127 setOperationAction(ISD::STACKSAVE, EVT::Other, Expand);
128 setOperationAction(ISD::STACKRESTORE, EVT::Other, Expand);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000129}
130
131const char *BlackfinTargetLowering::getTargetNodeName(unsigned Opcode) const {
132 switch (Opcode) {
133 default: return 0;
134 case BFISD::CALL: return "BFISD::CALL";
135 case BFISD::RET_FLAG: return "BFISD::RET_FLAG";
136 case BFISD::Wrapper: return "BFISD::Wrapper";
137 }
138}
139
Owen Andersonac9de032009-08-10 22:56:29 +0000140EVT::SimpleValueType BlackfinTargetLowering::getSetCCResultType(EVT VT) const {
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000141 // SETCC always sets the CC register. Technically that is an i1 register, but
142 // that type is not legal, so we treat it as an i32 register.
Owen Andersonac9de032009-08-10 22:56:29 +0000143 return EVT::i32;
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000144}
145
146SDValue BlackfinTargetLowering::LowerGlobalAddress(SDValue Op,
147 SelectionDAG &DAG) {
148 DebugLoc DL = Op.getDebugLoc();
149 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
150
Owen Andersonac9de032009-08-10 22:56:29 +0000151 Op = DAG.getTargetGlobalAddress(GV, EVT::i32);
152 return DAG.getNode(BFISD::Wrapper, DL, EVT::i32, Op);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000153}
154
155SDValue BlackfinTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
156 DebugLoc DL = Op.getDebugLoc();
157 int JTI = cast<JumpTableSDNode>(Op)->getIndex();
158
Owen Andersonac9de032009-08-10 22:56:29 +0000159 Op = DAG.getTargetJumpTable(JTI, EVT::i32);
160 return DAG.getNode(BFISD::Wrapper, DL, EVT::i32, Op);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000161}
162
Dan Gohman9178de12009-08-05 01:29:28 +0000163SDValue
164BlackfinTargetLowering::LowerFormalArguments(SDValue Chain,
165 unsigned CallConv, bool isVarArg,
166 const SmallVectorImpl<ISD::InputArg>
167 &Ins,
168 DebugLoc dl, SelectionDAG &DAG,
169 SmallVectorImpl<SDValue> &InVals) {
170
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000171 MachineFunction &MF = DAG.getMachineFunction();
172 MachineFrameInfo *MFI = MF.getFrameInfo();
173
174 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman9178de12009-08-05 01:29:28 +0000175 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
176 ArgLocs, *DAG.getContext());
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000177 CCInfo.AllocateStack(12, 4); // ABI requires 12 bytes stack space
Dan Gohman9178de12009-08-05 01:29:28 +0000178 CCInfo.AnalyzeFormalArguments(Ins, CC_Blackfin);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000179
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000180 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
181 CCValAssign &VA = ArgLocs[i];
182
183 if (VA.isRegLoc()) {
Owen Andersonac9de032009-08-10 22:56:29 +0000184 EVT RegVT = VA.getLocVT();
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000185 TargetRegisterClass *RC = VA.getLocReg() == BF::P0 ?
186 BF::PRegisterClass : BF::DRegisterClass;
Jakob Stoklund Olesen28891ad2009-08-03 19:32:30 +0000187 assert(RC->contains(VA.getLocReg()) && "Unexpected regclass in CCState");
188 assert(RC->hasType(RegVT) && "Unexpected regclass in CCState");
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000189
190 unsigned Reg = MF.getRegInfo().createVirtualRegister(RC);
191 MF.getRegInfo().addLiveIn(VA.getLocReg(), Reg);
Dan Gohman9178de12009-08-05 01:29:28 +0000192 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000193
194 // If this is an 8 or 16-bit value, it is really passed promoted to 32
195 // bits. Insert an assert[sz]ext to capture this, then truncate to the
196 // right size.
197 if (VA.getLocInfo() == CCValAssign::SExt)
198 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
199 DAG.getValueType(VA.getValVT()));
200 else if (VA.getLocInfo() == CCValAssign::ZExt)
201 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
202 DAG.getValueType(VA.getValVT()));
203
204 if (VA.getLocInfo() != CCValAssign::Full)
205 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
206
Dan Gohman9178de12009-08-05 01:29:28 +0000207 InVals.push_back(ArgValue);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000208 } else {
Jakob Stoklund Olesen28891ad2009-08-03 19:32:30 +0000209 assert(VA.isMemLoc() && "CCValAssign must be RegLoc or MemLoc");
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000210 unsigned ObjSize = VA.getLocVT().getStoreSizeInBits()/8;
211 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
Owen Andersonac9de032009-08-10 22:56:29 +0000212 SDValue FIN = DAG.getFrameIndex(FI, EVT::i32);
Dan Gohman9178de12009-08-05 01:29:28 +0000213 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000214 }
215 }
216
Dan Gohman9178de12009-08-05 01:29:28 +0000217 return Chain;
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000218}
219
Dan Gohman9178de12009-08-05 01:29:28 +0000220SDValue
221BlackfinTargetLowering::LowerReturn(SDValue Chain,
222 unsigned CallConv, bool isVarArg,
223 const SmallVectorImpl<ISD::OutputArg> &Outs,
224 DebugLoc dl, SelectionDAG &DAG) {
225
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000226 // CCValAssign - represent the assignment of the return value to locations.
227 SmallVector<CCValAssign, 16> RVLocs;
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000228
229 // CCState - Info about the registers and stack slot.
Dan Gohman9178de12009-08-05 01:29:28 +0000230 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(),
231 RVLocs, *DAG.getContext());
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000232
Dan Gohman9178de12009-08-05 01:29:28 +0000233 // Analize return values.
234 CCInfo.AnalyzeReturn(Outs, RetCC_Blackfin);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000235
236 // If this is the first return lowered for this function, add the regs to the
237 // liveout set for the function.
238 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
239 for (unsigned i = 0; i != RVLocs.size(); ++i)
240 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
241 }
242
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000243 SDValue Flag;
244
245 // Copy the result values into the output registers.
246 for (unsigned i = 0; i != RVLocs.size(); ++i) {
247 CCValAssign &VA = RVLocs[i];
248 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman9178de12009-08-05 01:29:28 +0000249 SDValue Opi = Outs[i].Val;
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000250
251 // Expand to i32 if necessary
252 switch (VA.getLocInfo()) {
253 default: llvm_unreachable("Unknown loc info!");
254 case CCValAssign::Full: break;
255 case CCValAssign::SExt:
256 Opi = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Opi);
257 break;
258 case CCValAssign::ZExt:
259 Opi = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Opi);
260 break;
261 case CCValAssign::AExt:
262 Opi = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Opi);
263 break;
264 }
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000265 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Opi, SDValue());
266 // Guarantee that all emitted copies are stuck together with flags.
267 Flag = Chain.getValue(1);
268 }
269
270 if (Flag.getNode()) {
Owen Andersonac9de032009-08-10 22:56:29 +0000271 return DAG.getNode(BFISD::RET_FLAG, dl, EVT::Other, Chain, Flag);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000272 } else {
Owen Andersonac9de032009-08-10 22:56:29 +0000273 return DAG.getNode(BFISD::RET_FLAG, dl, EVT::Other, Chain);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000274 }
275}
276
Dan Gohman9178de12009-08-05 01:29:28 +0000277SDValue
278BlackfinTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
279 unsigned CallConv, bool isVarArg,
280 bool isTailCall,
281 const SmallVectorImpl<ISD::OutputArg> &Outs,
282 const SmallVectorImpl<ISD::InputArg> &Ins,
283 DebugLoc dl, SelectionDAG &DAG,
284 SmallVectorImpl<SDValue> &InVals) {
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000285
286 // Analyze operands of the call, assigning locations to each operand.
287 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman9178de12009-08-05 01:29:28 +0000288 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs,
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000289 *DAG.getContext());
290 CCInfo.AllocateStack(12, 4); // ABI requires 12 bytes stack space
Dan Gohman9178de12009-08-05 01:29:28 +0000291 CCInfo.AnalyzeCallOperands(Outs, CC_Blackfin);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000292
293 // Get the size of the outgoing arguments stack space requirement.
294 unsigned ArgsSize = CCInfo.getNextStackOffset();
295
296 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
297 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
298 SmallVector<SDValue, 8> MemOpChains;
299
300 // Walk the register/memloc assignments, inserting copies/loads.
301 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
302 CCValAssign &VA = ArgLocs[i];
Dan Gohman9178de12009-08-05 01:29:28 +0000303 SDValue Arg = Outs[i].Val;
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000304
305 // Promote the value if needed.
306 switch (VA.getLocInfo()) {
307 default: llvm_unreachable("Unknown loc info!");
308 case CCValAssign::Full: break;
309 case CCValAssign::SExt:
310 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
311 break;
312 case CCValAssign::ZExt:
313 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
314 break;
315 case CCValAssign::AExt:
316 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
317 break;
318 }
319
320 // Arguments that can be passed on register must be kept at
321 // RegsToPass vector
322 if (VA.isRegLoc()) {
323 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
324 } else {
Jakob Stoklund Olesen28891ad2009-08-03 19:32:30 +0000325 assert(VA.isMemLoc() && "CCValAssign must be RegLoc or MemLoc");
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000326 int Offset = VA.getLocMemOffset();
Jakob Stoklund Olesen28891ad2009-08-03 19:32:30 +0000327 assert(Offset%4 == 0 && "Unaligned LocMemOffset");
Owen Andersonac9de032009-08-10 22:56:29 +0000328 assert(VA.getLocVT()==EVT::i32 && "Illegal CCValAssign type");
329 SDValue SPN = DAG.getCopyFromReg(Chain, dl, BF::SP, EVT::i32);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000330 SDValue OffsetN = DAG.getIntPtrConstant(Offset);
Owen Andersonac9de032009-08-10 22:56:29 +0000331 OffsetN = DAG.getNode(ISD::ADD, dl, EVT::i32, SPN, OffsetN);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000332 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, OffsetN,
333 PseudoSourceValue::getStack(),
334 Offset));
335 }
336 }
337
338 // Transform all store nodes into one single node because
339 // all store nodes are independent of each other.
340 if (!MemOpChains.empty())
Owen Andersonac9de032009-08-10 22:56:29 +0000341 Chain = DAG.getNode(ISD::TokenFactor, dl, EVT::Other,
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000342 &MemOpChains[0], MemOpChains.size());
343
344 // Build a sequence of copy-to-reg nodes chained together with token
345 // chain and flag operands which copy the outgoing args into registers.
346 // The InFlag in necessary since all emited instructions must be
347 // stuck together.
348 SDValue InFlag;
349 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
350 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
351 RegsToPass[i].second, InFlag);
352 InFlag = Chain.getValue(1);
353 }
354
355 // If the callee is a GlobalAddress node (quite common, every direct call is)
356 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
357 // Likewise ExternalSymbol -> TargetExternalSymbol.
358 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Owen Andersonac9de032009-08-10 22:56:29 +0000359 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), EVT::i32);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000360 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Andersonac9de032009-08-10 22:56:29 +0000361 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), EVT::i32);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000362
Owen Andersonac9de032009-08-10 22:56:29 +0000363 std::vector<EVT> NodeTys;
364 NodeTys.push_back(EVT::Other); // Returns a chain
365 NodeTys.push_back(EVT::Flag); // Returns a flag for retval copy to use.
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000366 SDValue Ops[] = { Chain, Callee, InFlag };
367 Chain = DAG.getNode(BFISD::CALL, dl, NodeTys, Ops,
368 InFlag.getNode() ? 3 : 2);
369 InFlag = Chain.getValue(1);
370
371 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
372 DAG.getIntPtrConstant(0, true), InFlag);
373 InFlag = Chain.getValue(1);
374
375 // Assign locations to each value returned by this call.
376 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman9178de12009-08-05 01:29:28 +0000377 CCState RVInfo(CallConv, isVarArg, DAG.getTarget(), RVLocs,
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000378 *DAG.getContext());
379
Dan Gohman9178de12009-08-05 01:29:28 +0000380 RVInfo.AnalyzeCallResult(Ins, RetCC_Blackfin);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000381
382 // Copy all of the result registers out of their specified physreg.
383 for (unsigned i = 0; i != RVLocs.size(); ++i) {
384 CCValAssign &RV = RVLocs[i];
385 unsigned Reg = RV.getLocReg();
386
387 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
388 RVLocs[i].getLocVT(), InFlag);
389 SDValue Val = Chain.getValue(0);
390 InFlag = Chain.getValue(2);
391 Chain = Chain.getValue(1);
392
393 // Callee is responsible for extending any i16 return values.
394 switch (RV.getLocInfo()) {
395 case CCValAssign::SExt:
396 Val = DAG.getNode(ISD::AssertSext, dl, RV.getLocVT(), Val,
397 DAG.getValueType(RV.getValVT()));
398 break;
399 case CCValAssign::ZExt:
400 Val = DAG.getNode(ISD::AssertZext, dl, RV.getLocVT(), Val,
401 DAG.getValueType(RV.getValVT()));
402 break;
403 default:
404 break;
405 }
406
407 // Truncate to valtype
408 if (RV.getLocInfo() != CCValAssign::Full)
409 Val = DAG.getNode(ISD::TRUNCATE, dl, RV.getValVT(), Val);
Dan Gohman9178de12009-08-05 01:29:28 +0000410 InVals.push_back(Val);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000411 }
412
Dan Gohman9178de12009-08-05 01:29:28 +0000413 return Chain;
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000414}
415
416// Expansion of ADDE / SUBE. This is a bit involved since blackfin doesn't have
417// add-with-carry instructions.
418SDValue BlackfinTargetLowering::LowerADDE(SDValue Op, SelectionDAG &DAG) {
419 // Operands: lhs, rhs, carry-in (AC0 flag)
420 // Results: sum, carry-out (AC0 flag)
421 DebugLoc dl = Op.getDebugLoc();
422
423 unsigned Opcode = Op.getOpcode()==ISD::ADDE ? BF::ADD : BF::SUB;
424
425 // zext incoming carry flag in AC0 to 32 bits
Owen Andersonac9de032009-08-10 22:56:29 +0000426 SDNode* CarryIn = DAG.getTargetNode(BF::MOVE_cc_ac0, dl, EVT::i32,
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000427 /* flag= */ Op.getOperand(2));
Owen Andersonac9de032009-08-10 22:56:29 +0000428 CarryIn = DAG.getTargetNode(BF::MOVECC_zext, dl, EVT::i32,
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000429 SDValue(CarryIn, 0));
430
431 // Add operands, produce sum and carry flag
Owen Andersonac9de032009-08-10 22:56:29 +0000432 SDNode *Sum = DAG.getTargetNode(Opcode, dl, EVT::i32, EVT::Flag,
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000433 Op.getOperand(0), Op.getOperand(1));
434
435 // Store intermediate carry from Sum
Owen Andersonac9de032009-08-10 22:56:29 +0000436 SDNode* Carry1 = DAG.getTargetNode(BF::MOVE_cc_ac0, dl, EVT::i32,
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000437 /* flag= */ SDValue(Sum, 1));
438
439 // Add incoming carry, again producing an output flag
Owen Andersonac9de032009-08-10 22:56:29 +0000440 Sum = DAG.getTargetNode(Opcode, dl, EVT::i32, EVT::Flag,
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000441 SDValue(Sum, 0), SDValue(CarryIn, 0));
442
443 // Update AC0 with the intermediate carry, producing a flag.
Owen Andersonac9de032009-08-10 22:56:29 +0000444 SDNode *CarryOut = DAG.getTargetNode(BF::OR_ac0_cc, dl, EVT::Flag,
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000445 SDValue(Carry1, 0));
446
447 // Compose (i32, flag) pair
448 SDValue ops[2] = { SDValue(Sum, 0), SDValue(CarryOut, 0) };
449 return DAG.getMergeValues(ops, 2, dl);
450}
451
452SDValue BlackfinTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
453 switch (Op.getOpcode()) {
454 default:
455 Op.getNode()->dump();
456 llvm_unreachable("Should not custom lower this!");
457 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
458 case ISD::GlobalTLSAddress:
459 llvm_unreachable("TLS not implemented for Blackfin.");
460 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
461 // Frame & Return address. Currently unimplemented
462 case ISD::FRAMEADDR: return SDValue();
463 case ISD::RETURNADDR: return SDValue();
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000464 case ISD::ADDE:
465 case ISD::SUBE: return LowerADDE(Op, DAG);
466 }
467}
468
Jakob Stoklund Olesen69f1aec2009-08-08 21:42:22 +0000469void
470BlackfinTargetLowering::ReplaceNodeResults(SDNode *N,
471 SmallVectorImpl<SDValue> &Results,
472 SelectionDAG &DAG) {
473 DebugLoc dl = N->getDebugLoc();
474 switch (N->getOpcode()) {
475 default:
476 llvm_unreachable("Do not know how to custom type legalize this operation!");
477 return;
478 case ISD::READCYCLECOUNTER: {
479 // The low part of the cycle counter is in CYCLES, the high part in
480 // CYCLES2. Reading CYCLES will latch the value of CYCLES2, so we must read
481 // CYCLES2 last.
482 SDValue TheChain = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +0000483 SDValue lo = DAG.getCopyFromReg(TheChain, dl, BF::CYCLES, EVT::i32);
484 SDValue hi = DAG.getCopyFromReg(lo.getValue(1), dl, BF::CYCLES2, EVT::i32);
Jakob Stoklund Olesen69f1aec2009-08-08 21:42:22 +0000485 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Owen Andersonac9de032009-08-10 22:56:29 +0000486 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, EVT::i64, lo, hi));
Jakob Stoklund Olesen69f1aec2009-08-08 21:42:22 +0000487 // Outgoing chain. If we were to use the chain from lo instead, it would be
488 // possible to entirely eliminate the CYCLES2 read in (i32 (trunc
489 // readcyclecounter)). Unfortunately this could possibly delay the CYCLES2
490 // read beyond the next CYCLES read, leading to invalid results.
491 Results.push_back(hi.getValue(1));
492 return;
493 }
494 }
495}
496
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000497/// getFunctionAlignment - Return the Log2 alignment of this function.
498unsigned BlackfinTargetLowering::getFunctionAlignment(const Function *F) const {
499 return 2;
500}
501
502//===----------------------------------------------------------------------===//
503// Blackfin Inline Assembly Support
504//===----------------------------------------------------------------------===//
505
506/// getConstraintType - Given a constraint letter, return the type of
507/// constraint it is for this target.
508BlackfinTargetLowering::ConstraintType
509BlackfinTargetLowering::getConstraintType(const std::string &Constraint) const {
Jakob Stoklund Olesen3d9f0ba2009-08-02 17:39:17 +0000510 if (Constraint.size() != 1)
511 return TargetLowering::getConstraintType(Constraint);
512
513 switch (Constraint[0]) {
514 // Standard constraints
515 case 'r':
516 return C_RegisterClass;
517
518 // Blackfin-specific constraints
519 case 'a':
520 case 'd':
521 case 'z':
522 case 'D':
523 case 'W':
524 case 'e':
525 case 'b':
526 case 'v':
527 case 'f':
528 case 'c':
529 case 't':
530 case 'u':
531 case 'k':
532 case 'x':
533 case 'y':
534 case 'w':
535 return C_RegisterClass;
536 case 'A':
537 case 'B':
538 case 'C':
539 case 'Z':
540 case 'Y':
541 return C_Register;
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000542 }
543
Jakob Stoklund Olesen3d9f0ba2009-08-02 17:39:17 +0000544 // Not implemented: q0-q7, qA. Use {R2} etc instead
545
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000546 return TargetLowering::getConstraintType(Constraint);
547}
548
Jakob Stoklund Olesen3d9f0ba2009-08-02 17:39:17 +0000549/// getRegForInlineAsmConstraint - Return register no and class for a C_Register
550/// constraint.
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000551std::pair<unsigned, const TargetRegisterClass*> BlackfinTargetLowering::
Owen Andersonac9de032009-08-10 22:56:29 +0000552getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const {
Jakob Stoklund Olesen3d9f0ba2009-08-02 17:39:17 +0000553 typedef std::pair<unsigned, const TargetRegisterClass*> Pair;
554 using namespace BF;
555
556 if (Constraint.size() != 1)
557 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
558
559 switch (Constraint[0]) {
560 // Standard constraints
561 case 'r':
Owen Andersonac9de032009-08-10 22:56:29 +0000562 return Pair(0U, VT == EVT::i16 ? D16RegisterClass : DPRegisterClass);
Jakob Stoklund Olesen3d9f0ba2009-08-02 17:39:17 +0000563
564 // Blackfin-specific constraints
565 case 'a': return Pair(0U, PRegisterClass);
566 case 'd': return Pair(0U, DRegisterClass);
567 case 'e': return Pair(0U, AccuRegisterClass);
568 case 'A': return Pair(A0, AccuRegisterClass);
569 case 'B': return Pair(A1, AccuRegisterClass);
570 case 'b': return Pair(0U, IRegisterClass);
571 case 'v': return Pair(0U, BRegisterClass);
572 case 'f': return Pair(0U, MRegisterClass);
573 case 'C': return Pair(CC, JustCCRegisterClass);
574 case 'x': return Pair(0U, GRRegisterClass);
575 case 'w': return Pair(0U, ALLRegisterClass);
576 case 'Z': return Pair(P3, PRegisterClass);
577 case 'Y': return Pair(P1, PRegisterClass);
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000578 }
579
Jakob Stoklund Olesen3d9f0ba2009-08-02 17:39:17 +0000580 // Not implemented: q0-q7, qA. Use {R2} etc instead.
581 // Constraints z, D, W, c, t, u, k, and y use non-existing classes, defer to
582 // getRegClassForInlineAsmConstraint()
583
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000584 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
585}
586
587std::vector<unsigned> BlackfinTargetLowering::
Owen Andersonac9de032009-08-10 22:56:29 +0000588getRegClassForInlineAsmConstraint(const std::string &Constraint, EVT VT) const {
Jakob Stoklund Olesen3d9f0ba2009-08-02 17:39:17 +0000589 using namespace BF;
590
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000591 if (Constraint.size() != 1)
592 return std::vector<unsigned>();
593
Jakob Stoklund Olesen3d9f0ba2009-08-02 17:39:17 +0000594 switch (Constraint[0]) {
595 case 'z': return make_vector<unsigned>(P0, P1, P2, 0);
596 case 'D': return make_vector<unsigned>(R0, R2, R4, R6, 0);
597 case 'W': return make_vector<unsigned>(R1, R3, R5, R7, 0);
598 case 'c': return make_vector<unsigned>(I0, I1, I2, I3,
599 B0, B1, B2, B3,
600 L0, L1, L2, L3, 0);
601 case 't': return make_vector<unsigned>(LT0, LT1, 0);
602 case 'u': return make_vector<unsigned>(LB0, LB1, 0);
603 case 'k': return make_vector<unsigned>(LC0, LC1, 0);
604 case 'y': return make_vector<unsigned>(RETS, RETN, RETI, RETX, RETE,
605 ASTAT, SEQSTAT, USP, 0);
606 }
607
Jakob Stoklund Olesen2cfddbb2009-08-02 17:32:10 +0000608 return std::vector<unsigned>();
609}
610
611bool BlackfinTargetLowering::
612isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
613 // The Blackfin target isn't yet aware of offsets.
614 return false;
615}