blob: b80c45c7aea6ef46937cfa4a0d2efbd5ad74d9da [file] [log] [blame]
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001//===-- LanaiISelLowering.cpp - Lanai 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 LanaiTargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "LanaiISelLowering.h"
15
16#include "Lanai.h"
17#include "LanaiMachineFunctionInfo.h"
18#include "LanaiSubtarget.h"
19#include "LanaiTargetMachine.h"
20#include "LanaiTargetObjectFile.h"
21#include "llvm/CodeGen/CallingConvLower.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
28#include "llvm/CodeGen/ValueTypes.h"
29#include "llvm/IR/CallingConv.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalAlias.h"
33#include "llvm/IR/GlobalVariable.h"
34#include "llvm/IR/Intrinsics.h"
35#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/raw_ostream.h"
39
40#define DEBUG_TYPE "lanai-lower"
41
42using namespace llvm;
43
44// Limit on number of instructions the lowered multiplication may have before a
45// call to the library function should be generated instead. The threshold is
46// currently set to 14 as this was the smallest threshold that resulted in all
47// constant multiplications being lowered. A threshold of 5 covered all cases
48// except for one multiplication which required 14. mulsi3 requires 16
49// instructions (including the prologue and epilogue but excluding instructions
50// at call site). Until we can inline mulsi3, generating at most 14 instructions
51// will be faster than invoking mulsi3.
52static cl::opt<int> LanaiLowerConstantMulThreshold(
53 "lanai-constant-mul-threshold", cl::Hidden,
54 cl::desc("Maximum number of instruction to generate when lowering constant "
55 "multiplication instead of calling library function [default=14]"),
56 cl::init(14));
57
58LanaiTargetLowering::LanaiTargetLowering(const TargetMachine &TM,
59 const LanaiSubtarget &STI)
60 : TargetLowering(TM) {
61 // Set up the register classes.
62 addRegisterClass(MVT::i32, &Lanai::GPRRegClass);
63
64 // Compute derived properties from the register classes
65 TRI = STI.getRegisterInfo();
66 computeRegisterProperties(TRI);
67
68 setStackPointerRegisterToSaveRestore(Lanai::SP);
69
70 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
71 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
72 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
73 setOperationAction(ISD::SETCC, MVT::i32, Custom);
74 setOperationAction(ISD::SELECT, MVT::i32, Expand);
75 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
76
77 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
78 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
79 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
80 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
81
82 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
83 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
84 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
85
86 setOperationAction(ISD::VASTART, MVT::Other, Custom);
87 setOperationAction(ISD::VAARG, MVT::Other, Expand);
88 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
89 setOperationAction(ISD::VAEND, MVT::Other, Expand);
90
91 setOperationAction(ISD::SDIV, MVT::i32, Expand);
92 setOperationAction(ISD::UDIV, MVT::i32, Expand);
93 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
94 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
95 setOperationAction(ISD::SREM, MVT::i32, Expand);
96 setOperationAction(ISD::UREM, MVT::i32, Expand);
97
98 setOperationAction(ISD::MUL, MVT::i32, Custom);
99 setOperationAction(ISD::MULHU, MVT::i32, Expand);
100 setOperationAction(ISD::MULHS, MVT::i32, Expand);
101 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
102 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
103
104 setOperationAction(ISD::ROTR, MVT::i32, Expand);
105 setOperationAction(ISD::ROTL, MVT::i32, Expand);
106 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
Jacques Pienaarad1db352016-04-14 17:59:22 +0000107 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000108 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
109
110 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
111 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
112 setOperationAction(ISD::CTLZ, MVT::i32, Legal);
113 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Legal);
114 setOperationAction(ISD::CTTZ, MVT::i32, Legal);
115 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Legal);
116
117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
120
121 // Extended load operations for i1 types must be promoted
122 for (MVT VT : MVT::integer_valuetypes()) {
123 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
124 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
125 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
126 }
127
128 // Function alignments (log2)
129 setMinFunctionAlignment(2);
130 setPrefFunctionAlignment(2);
131
132 setJumpIsExpensive(true);
133
134 // TODO: Setting the minimum jump table entries needed before a
135 // switch is transformed to a jump table to 100 to avoid creating jump tables
136 // as this was causing bad performance compared to a large group of if
137 // statements. Re-evaluate this on new benchmarks.
138 setMinimumJumpTableEntries(100);
139
140 // Use fast calling convention for library functions.
141 for (int I = 0; I < RTLIB::UNKNOWN_LIBCALL; ++I) {
142 setLibcallCallingConv(static_cast<RTLIB::Libcall>(I), CallingConv::Fast);
143 }
144
145 MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
146 MaxStoresPerMemsetOptSize = 8;
147 MaxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
148 MaxStoresPerMemcpyOptSize = 8;
149 MaxStoresPerMemmove = 16; // For @llvm.memmove -> sequence of stores
150 MaxStoresPerMemmoveOptSize = 8;
151}
152
153SDValue LanaiTargetLowering::LowerOperation(SDValue Op,
154 SelectionDAG &DAG) const {
155 switch (Op.getOpcode()) {
156 case ISD::MUL:
157 return LowerMUL(Op, DAG);
158 case ISD::BR_CC:
159 return LowerBR_CC(Op, DAG);
160 case ISD::ConstantPool:
161 return LowerConstantPool(Op, DAG);
162 case ISD::GlobalAddress:
163 return LowerGlobalAddress(Op, DAG);
164 case ISD::BlockAddress:
165 return LowerBlockAddress(Op, DAG);
166 case ISD::JumpTable:
167 return LowerJumpTable(Op, DAG);
168 case ISD::SELECT_CC:
169 return LowerSELECT_CC(Op, DAG);
170 case ISD::SETCC:
171 return LowerSETCC(Op, DAG);
Jacques Pienaarad1db352016-04-14 17:59:22 +0000172 case ISD::SRL_PARTS:
173 return LowerSRL_PARTS(Op, DAG);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000174 case ISD::VASTART:
175 return LowerVASTART(Op, DAG);
176 case ISD::DYNAMIC_STACKALLOC:
177 return LowerDYNAMIC_STACKALLOC(Op, DAG);
178 case ISD::RETURNADDR:
179 return LowerRETURNADDR(Op, DAG);
180 case ISD::FRAMEADDR:
181 return LowerFRAMEADDR(Op, DAG);
182 default:
183 llvm_unreachable("unimplemented operand");
184 }
185}
186//===----------------------------------------------------------------------===//
187// Lanai Inline Assembly Support
188//===----------------------------------------------------------------------===//
189
190unsigned LanaiTargetLowering::getRegisterByName(const char *RegName, EVT VT,
191 SelectionDAG &DAG) const {
192 // Only unallocatable registers should be matched here.
193 unsigned Reg = StringSwitch<unsigned>(RegName)
194 .Case("pc", Lanai::PC)
195 .Case("sp", Lanai::SP)
196 .Case("fp", Lanai::FP)
197 .Case("rr1", Lanai::RR1)
198 .Case("r10", Lanai::R10)
199 .Case("rr2", Lanai::RR2)
200 .Case("r11", Lanai::R11)
201 .Case("rca", Lanai::RCA)
202 .Default(0);
203
204 if (Reg)
205 return Reg;
206 report_fatal_error("Invalid register name global variable");
207}
208
209std::pair<unsigned, const TargetRegisterClass *>
210LanaiTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
211 StringRef Constraint,
212 MVT VT) const {
213 if (Constraint.size() == 1)
214 // GCC Constraint Letters
215 switch (Constraint[0]) {
216 case 'r': // GENERAL_REGS
217 return std::make_pair(0U, &Lanai::GPRRegClass);
218 default:
219 break;
220 }
221
222 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
223}
224
225// Examine constraint type and operand type and determine a weight value.
226// This object must already have been set up with the operand type
227// and the current alternative constraint selected.
228TargetLowering::ConstraintWeight
229LanaiTargetLowering::getSingleConstraintMatchWeight(
230 AsmOperandInfo &Info, const char *Constraint) const {
231 ConstraintWeight Weight = CW_Invalid;
232 Value *CallOperandVal = Info.CallOperandVal;
233 // If we don't have a value, we can't do a match,
234 // but allow it at the lowest weight.
235 if (CallOperandVal == NULL)
236 return CW_Default;
237 // Look at the constraint type.
238 switch (*Constraint) {
239 case 'I': // signed 16 bit immediate
240 case 'J': // integer zero
241 case 'K': // unsigned 16 bit immediate
242 case 'L': // immediate in the range 0 to 31
243 case 'M': // signed 32 bit immediate where lower 16 bits are 0
244 case 'N': // signed 26 bit immediate
245 case 'O': // integer zero
246 if (isa<ConstantInt>(CallOperandVal))
247 Weight = CW_Constant;
248 break;
249 default:
250 Weight = TargetLowering::getSingleConstraintMatchWeight(Info, Constraint);
251 break;
252 }
253 return Weight;
254}
255
256// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
257// vector. If it is invalid, don't add anything to Ops.
258void LanaiTargetLowering::LowerAsmOperandForConstraint(
259 SDValue Op, std::string &Constraint, std::vector<SDValue> &Ops,
260 SelectionDAG &DAG) const {
261 SDValue Result(0, 0);
262
263 // Only support length 1 constraints for now.
264 if (Constraint.length() > 1)
265 return;
266
267 char ConstraintLetter = Constraint[0];
268 switch (ConstraintLetter) {
269 case 'I': // Signed 16 bit constant
270 // If this fails, the parent routine will give an error
271 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
272 if (isInt<16>(C->getSExtValue())) {
273 Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(C),
274 Op.getValueType());
275 break;
276 }
277 }
278 return;
279 case 'J': // integer zero
280 case 'O':
281 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
282 if (C->getZExtValue() == 0) {
283 Result = DAG.getTargetConstant(0, SDLoc(C), Op.getValueType());
284 break;
285 }
286 }
287 return;
288 case 'K': // unsigned 16 bit immediate
289 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
290 if (isUInt<16>(C->getZExtValue())) {
291 Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(C),
292 Op.getValueType());
293 break;
294 }
295 }
296 return;
297 case 'L': // immediate in the range 0 to 31
298 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
299 if (C->getZExtValue() <= 31) {
300 Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(C),
301 Op.getValueType());
302 break;
303 }
304 }
305 return;
306 case 'M': // signed 32 bit immediate where lower 16 bits are 0
307 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
308 int64_t Val = C->getSExtValue();
309 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)) {
310 Result = DAG.getTargetConstant(Val, SDLoc(C), Op.getValueType());
311 break;
312 }
313 }
314 return;
315 case 'N': // signed 26 bit immediate
316 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
317 int64_t Val = C->getSExtValue();
318 if ((Val >= -33554432) && (Val <= 33554431)) {
319 Result = DAG.getTargetConstant(Val, SDLoc(C), Op.getValueType());
320 break;
321 }
322 }
323 return;
324 default:
325 break; // This will fall through to the generic implementation
326 }
327
328 if (Result.getNode()) {
329 Ops.push_back(Result);
330 return;
331 }
332
333 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
334}
335
336//===----------------------------------------------------------------------===//
337// Calling Convention Implementation
338//===----------------------------------------------------------------------===//
339
340#include "LanaiGenCallingConv.inc"
341
342static unsigned NumFixedArgs;
343static bool CC_Lanai32_VarArg(unsigned ValNo, MVT ValVT, MVT LocVT,
344 CCValAssign::LocInfo LocInfo,
345 ISD::ArgFlagsTy ArgFlags, CCState &State) {
346 // Handle fixed arguments with default CC.
347 // Note: Both the default and fast CC handle VarArg the same and hence the
348 // calling convention of the function is not considered here.
349 if (ValNo < NumFixedArgs) {
350 return CC_Lanai32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
351 }
352
353 // Promote i8/i16 args to i32
354 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
355 LocVT = MVT::i32;
356 if (ArgFlags.isSExt())
357 LocInfo = CCValAssign::SExt;
358 else if (ArgFlags.isZExt())
359 LocInfo = CCValAssign::ZExt;
360 else
361 LocInfo = CCValAssign::AExt;
362 }
363
364 // VarArgs get passed on stack
365 unsigned Offset = State.AllocateStack(4, 4);
366 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
367 return false;
368}
369
370SDValue LanaiTargetLowering::LowerFormalArguments(
371 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
372 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
373 SmallVectorImpl<SDValue> &InVals) const {
374 switch (CallConv) {
375 case CallingConv::C:
376 case CallingConv::Fast:
377 return LowerCCCArguments(Chain, CallConv, IsVarArg, Ins, DL, DAG, InVals);
378 default:
379 llvm_unreachable("Unsupported calling convention");
380 }
381}
382
383SDValue LanaiTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
384 SmallVectorImpl<SDValue> &InVals) const {
385 SelectionDAG &DAG = CLI.DAG;
386 SDLoc &DL = CLI.DL;
387 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
388 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
389 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
390 SDValue Chain = CLI.Chain;
391 SDValue Callee = CLI.Callee;
392 bool &IsTailCall = CLI.IsTailCall;
393 CallingConv::ID CallConv = CLI.CallConv;
394 bool IsVarArg = CLI.IsVarArg;
395
396 // Lanai target does not yet support tail call optimization.
397 IsTailCall = false;
398
399 switch (CallConv) {
400 case CallingConv::Fast:
401 case CallingConv::C:
402 return LowerCCCCallTo(Chain, Callee, CallConv, IsVarArg, IsTailCall, Outs,
403 OutVals, Ins, DL, DAG, InVals);
404 default:
405 llvm_unreachable("Unsupported calling convention");
406 }
407}
408
409// LowerCCCArguments - transform physical registers into virtual registers and
410// generate load operations for arguments places on the stack.
411SDValue LanaiTargetLowering::LowerCCCArguments(
412 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
413 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
414 SmallVectorImpl<SDValue> &InVals) const {
415 MachineFunction &MF = DAG.getMachineFunction();
416 MachineFrameInfo *MFI = MF.getFrameInfo();
417 MachineRegisterInfo &RegInfo = MF.getRegInfo();
418 LanaiMachineFunctionInfo *LanaiMFI = MF.getInfo<LanaiMachineFunctionInfo>();
419
420 // Assign locations to all of the incoming arguments.
421 SmallVector<CCValAssign, 16> ArgLocs;
422 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
423 *DAG.getContext());
424 if (CallConv == CallingConv::Fast) {
425 CCInfo.AnalyzeFormalArguments(Ins, CC_Lanai32_Fast);
426 } else {
427 CCInfo.AnalyzeFormalArguments(Ins, CC_Lanai32);
428 }
429
430 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
431 CCValAssign &VA = ArgLocs[i];
432 if (VA.isRegLoc()) {
433 // Arguments passed in registers
434 EVT RegVT = VA.getLocVT();
435 switch (RegVT.getSimpleVT().SimpleTy) {
436 case MVT::i32: {
437 unsigned VReg = RegInfo.createVirtualRegister(&Lanai::GPRRegClass);
438 RegInfo.addLiveIn(VA.getLocReg(), VReg);
439 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, RegVT);
440
441 // If this is an 8/16-bit value, it is really passed promoted to 32
442 // bits. Insert an assert[sz]ext to capture this, then truncate to the
443 // right size.
444 if (VA.getLocInfo() == CCValAssign::SExt)
445 ArgValue = DAG.getNode(ISD::AssertSext, DL, RegVT, ArgValue,
446 DAG.getValueType(VA.getValVT()));
447 else if (VA.getLocInfo() == CCValAssign::ZExt)
448 ArgValue = DAG.getNode(ISD::AssertZext, DL, RegVT, ArgValue,
449 DAG.getValueType(VA.getValVT()));
450
451 if (VA.getLocInfo() != CCValAssign::Full)
452 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), ArgValue);
453
454 InVals.push_back(ArgValue);
455 break;
456 }
457 default:
458 DEBUG(dbgs() << "LowerFormalArguments Unhandled argument type: "
JF Bastien246e7962016-04-18 16:33:41 +0000459 << (int)RegVT.getSimpleVT().SimpleTy << "\n");
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000460 llvm_unreachable("unhandled argument type");
461 }
462 } else {
463 // Sanity check
464 assert(VA.isMemLoc());
465 // Load the argument to a virtual register
466 unsigned ObjSize = VA.getLocVT().getSizeInBits() / 8;
467 // Check that the argument fits in stack slot
468 if (ObjSize > 4) {
469 errs() << "LowerFormalArguments Unhandled argument type: "
470 << EVT(VA.getLocVT()).getEVTString() << "\n";
471 }
472 // Create the frame index object for this incoming parameter...
473 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);
474
475 // Create the SelectionDAG nodes corresponding to a load
476 // from this parameter
477 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
478 InVals.push_back(DAG.getLoad(
479 VA.getLocVT(), DL, Chain, FIN,
480 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
481 false, false, false, 0));
482 }
483 }
484
485 // The Lanai ABI for returning structs by value requires that we copy
486 // the sret argument into rv for the return. Save the argument into
487 // a virtual register so that we can access it from the return points.
488 if (MF.getFunction()->hasStructRetAttr()) {
489 unsigned Reg = LanaiMFI->getSRetReturnReg();
490 if (!Reg) {
491 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
492 LanaiMFI->setSRetReturnReg(Reg);
493 }
494 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
495 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
496 }
497
498 if (IsVarArg) {
499 // Record the frame index of the first variable argument
500 // which is a value necessary to VASTART.
501 int FI = MFI->CreateFixedObject(4, CCInfo.getNextStackOffset(), true);
502 LanaiMFI->setVarArgsFrameIndex(FI);
503 }
504
505 return Chain;
506}
507
508SDValue
509LanaiTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
510 bool IsVarArg,
511 const SmallVectorImpl<ISD::OutputArg> &Outs,
512 const SmallVectorImpl<SDValue> &OutVals,
513 SDLoc DL, SelectionDAG &DAG) const {
514 // CCValAssign - represent the assignment of the return value to a location
515 SmallVector<CCValAssign, 16> RVLocs;
516
517 // CCState - Info about the registers and stack slot.
518 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
519 *DAG.getContext());
520
521 // Analize return values.
522 CCInfo.AnalyzeReturn(Outs, RetCC_Lanai32);
523
524 SDValue Flag;
525 SmallVector<SDValue, 4> RetOps(1, Chain);
526
527 // Copy the result values into the output registers.
528 for (unsigned i = 0; i != RVLocs.size(); ++i) {
529 CCValAssign &VA = RVLocs[i];
530 assert(VA.isRegLoc() && "Can only return in registers!");
531
532 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVals[i], Flag);
533
534 // Guarantee that all emitted copies are stuck together with flags.
535 Flag = Chain.getValue(1);
536 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
537 }
538
539 // The Lanai ABI for returning structs by value requires that we copy
540 // the sret argument into rv for the return. We saved the argument into
541 // a virtual register in the entry block, so now we copy the value out
542 // and into rv.
543 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
544 MachineFunction &MF = DAG.getMachineFunction();
545 LanaiMachineFunctionInfo *LanaiMFI = MF.getInfo<LanaiMachineFunctionInfo>();
546 unsigned Reg = LanaiMFI->getSRetReturnReg();
547 assert(Reg &&
548 "SRetReturnReg should have been set in LowerFormalArguments().");
549 SDValue Val =
550 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
551
552 Chain = DAG.getCopyToReg(Chain, DL, Lanai::RV, Val, Flag);
553 Flag = Chain.getValue(1);
554 RetOps.push_back(
555 DAG.getRegister(Lanai::RV, getPointerTy(DAG.getDataLayout())));
556 }
557
558 RetOps[0] = Chain; // Update chain
559
560 unsigned Opc = LanaiISD::RET_FLAG;
561 if (Flag.getNode())
562 RetOps.push_back(Flag);
563
564 // Return Void
565 return DAG.getNode(Opc, DL, MVT::Other,
566 ArrayRef<SDValue>(&RetOps[0], RetOps.size()));
567}
568
569// LowerCCCCallTo - functions arguments are copied from virtual regs to
570// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
571SDValue LanaiTargetLowering::LowerCCCCallTo(
572 SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool IsVarArg,
573 bool IsTailCall, const SmallVectorImpl<ISD::OutputArg> &Outs,
574 const SmallVectorImpl<SDValue> &OutVals,
575 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
576 SmallVectorImpl<SDValue> &InVals) const {
577 // Analyze operands of the call, assigning locations to each operand.
578 SmallVector<CCValAssign, 16> ArgLocs;
579 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
580 *DAG.getContext());
581 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
582 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
583
584 NumFixedArgs = 0;
585 if (IsVarArg && G) {
586 const Function *CalleeFn = dyn_cast<Function>(G->getGlobal());
587 if (CalleeFn)
588 NumFixedArgs = CalleeFn->getFunctionType()->getNumParams();
589 }
590 if (NumFixedArgs)
591 CCInfo.AnalyzeCallOperands(Outs, CC_Lanai32_VarArg);
592 else {
593 if (CallConv == CallingConv::Fast)
594 CCInfo.AnalyzeCallOperands(Outs, CC_Lanai32_Fast);
595 else
596 CCInfo.AnalyzeCallOperands(Outs, CC_Lanai32);
597 }
598
599 // Get a count of how many bytes are to be pushed on the stack.
600 unsigned NumBytes = CCInfo.getNextStackOffset();
601
602 // Create local copies for byval args.
603 SmallVector<SDValue, 8> ByValArgs;
604 for (unsigned I = 0, E = Outs.size(); I != E; ++I) {
605 ISD::ArgFlagsTy Flags = Outs[I].Flags;
606 if (!Flags.isByVal())
607 continue;
608
609 SDValue Arg = OutVals[I];
610 unsigned Size = Flags.getByValSize();
611 unsigned Align = Flags.getByValAlign();
612
613 int FI = MFI->CreateStackObject(Size, Align, false);
614 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
615 SDValue SizeNode = DAG.getConstant(Size, DL, MVT::i32);
616
617 Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Align,
618 /*IsVolatile=*/false,
619 /*AlwaysInline=*/false,
620 /*IsTailCall=*/false, MachinePointerInfo(),
621 MachinePointerInfo());
622 ByValArgs.push_back(FIPtr);
623 }
624
625 Chain = DAG.getCALLSEQ_START(
626 Chain,
627 DAG.getConstant(NumBytes, DL, getPointerTy(DAG.getDataLayout()), true),
628 DL);
629
630 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
631 SmallVector<SDValue, 12> MemOpChains;
632 SDValue StackPtr;
633
634 // Walk the register/memloc assignments, inserting copies/loads.
635 for (unsigned I = 0, J = 0, E = ArgLocs.size(); I != E; ++I) {
636 CCValAssign &VA = ArgLocs[I];
637 SDValue Arg = OutVals[I];
638 ISD::ArgFlagsTy Flags = Outs[I].Flags;
639
640 // Promote the value if needed.
641 switch (VA.getLocInfo()) {
642 case CCValAssign::Full:
643 break;
644 case CCValAssign::SExt:
645 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
646 break;
647 case CCValAssign::ZExt:
648 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
649 break;
650 case CCValAssign::AExt:
651 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
652 break;
653 default:
654 llvm_unreachable("Unknown loc info!");
655 }
656
657 // Use local copy if it is a byval arg.
658 if (Flags.isByVal())
659 Arg = ByValArgs[J++];
660
661 // Arguments that can be passed on register must be kept at RegsToPass
662 // vector
663 if (VA.isRegLoc()) {
664 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
665 } else {
666 assert(VA.isMemLoc());
667
668 if (StackPtr.getNode() == 0)
669 StackPtr = DAG.getCopyFromReg(Chain, DL, Lanai::SP,
670 getPointerTy(DAG.getDataLayout()));
671
672 SDValue PtrOff =
673 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
674 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
675
676 MemOpChains.push_back(DAG.getStore(
677 Chain, DL, Arg, PtrOff, MachinePointerInfo(), false, false, 0));
678 }
679 }
680
681 // Transform all store nodes into one single node because all store nodes are
682 // independent of each other.
683 if (!MemOpChains.empty())
684 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
685 ArrayRef<SDValue>(&MemOpChains[0], MemOpChains.size()));
686
687 SDValue InFlag;
688
689 // Build a sequence of copy-to-reg nodes chained together with token chain and
690 // flag operands which copy the outgoing args into registers. The InFlag in
691 // necessary since all emitted instructions must be stuck together.
692 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
693 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
694 RegsToPass[I].second, InFlag);
695 InFlag = Chain.getValue(1);
696 }
697
698 // If the callee is a GlobalAddress node (quite common, every direct call is)
699 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
700 // Likewise ExternalSymbol -> TargetExternalSymbol.
701 uint8_t OpFlag = LanaiII::MO_NO_FLAG;
702 if (G) {
703 Callee = DAG.getTargetGlobalAddress(
704 G->getGlobal(), DL, getPointerTy(DAG.getDataLayout()), 0, OpFlag);
705 } else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
706 Callee = DAG.getTargetExternalSymbol(
707 E->getSymbol(), getPointerTy(DAG.getDataLayout()), OpFlag);
708 }
709
710 // Returns a chain & a flag for retval copy to use.
711 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
712 SmallVector<SDValue, 8> Ops;
713 Ops.push_back(Chain);
714 Ops.push_back(Callee);
715
716 // Add a register mask operand representing the call-preserved registers.
717 // TODO: Should return-twice functions be handled?
718 const uint32_t *Mask =
719 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv);
720 assert(Mask && "Missing call preserved mask for calling convention");
721 Ops.push_back(DAG.getRegisterMask(Mask));
722
723 // Add argument registers to the end of the list so that they are
724 // known live into the call.
725 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
726 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
727 RegsToPass[I].second.getValueType()));
728
729 if (InFlag.getNode())
730 Ops.push_back(InFlag);
731
732 Chain = DAG.getNode(LanaiISD::CALL, DL, NodeTys,
733 ArrayRef<SDValue>(&Ops[0], Ops.size()));
734 InFlag = Chain.getValue(1);
735
736 // Create the CALLSEQ_END node.
737 Chain = DAG.getCALLSEQ_END(
738 Chain,
739 DAG.getConstant(NumBytes, DL, getPointerTy(DAG.getDataLayout()), true),
740 DAG.getConstant(0, DL, getPointerTy(DAG.getDataLayout()), true), InFlag,
741 DL);
742 InFlag = Chain.getValue(1);
743
744 // Handle result values, copying them out of physregs into vregs that we
745 // return.
746 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
747 InVals);
748}
749
750// LowerCallResult - Lower the result values of a call into the
751// appropriate copies out of appropriate physical registers.
752SDValue LanaiTargetLowering::LowerCallResult(
753 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
754 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
755 SmallVectorImpl<SDValue> &InVals) const {
756 // Assign locations to each value returned by this call.
757 SmallVector<CCValAssign, 16> RVLocs;
758 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
759 *DAG.getContext());
760
761 CCInfo.AnalyzeCallResult(Ins, RetCC_Lanai32);
762
763 // Copy all of the result registers out of their specified physreg.
764 for (unsigned I = 0; I != RVLocs.size(); ++I) {
765 Chain = DAG.getCopyFromReg(Chain, DL, RVLocs[I].getLocReg(),
766 RVLocs[I].getValVT(), InFlag)
767 .getValue(1);
768 InFlag = Chain.getValue(2);
769 InVals.push_back(Chain.getValue(0));
770 }
771
772 return Chain;
773}
774
775//===----------------------------------------------------------------------===//
776// Custom Lowerings
777//===----------------------------------------------------------------------===//
778
779static LPCC::CondCode IntCondCCodeToICC(ISD::CondCode SetCCOpcode, SDLoc DL,
780 SDValue &LHS, SDValue &RHS,
781 SelectionDAG &DAG) {
782 // For integer, only the SETEQ, SETNE, SETLT, SETLE, SETGT, SETGE, SETULT,
783 // SETULE, SETUGT, and SETUGE opcodes are used (see CodeGen/ISDOpcodes.h)
784 // and Lanai only supports integer comparisons, so only provide definitions
785 // for them.
786 switch (SetCCOpcode) {
787 case ISD::SETEQ:
788 return LPCC::ICC_EQ;
789 case ISD::SETGT:
790 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
791 if (RHSC->getZExtValue() == 0xFFFFFFFF) {
792 // X > -1 -> X >= 0 -> is_plus(X)
793 RHS = DAG.getConstant(0, DL, RHS.getValueType());
794 return LPCC::ICC_PL;
795 }
796 return LPCC::ICC_GT;
797 case ISD::SETUGT:
798 return LPCC::ICC_UGT;
799 case ISD::SETLT:
800 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
801 if (RHSC->getZExtValue() == 0)
802 // X < 0 -> is_minus(X)
803 return LPCC::ICC_MI;
804 return LPCC::ICC_LT;
805 case ISD::SETULT:
806 return LPCC::ICC_ULT;
807 case ISD::SETLE:
808 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
809 if (RHSC->getZExtValue() == 0xFFFFFFFF) {
810 // X <= -1 -> X < 0 -> is_minus(X)
811 RHS = DAG.getConstant(0, DL, RHS.getValueType());
812 return LPCC::ICC_MI;
813 }
814 return LPCC::ICC_LE;
815 case ISD::SETULE:
816 return LPCC::ICC_ULE;
817 case ISD::SETGE:
818 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
819 if (RHSC->getZExtValue() == 0)
820 // X >= 0 -> is_plus(X)
821 return LPCC::ICC_PL;
822 return LPCC::ICC_GE;
823 case ISD::SETUGE:
824 return LPCC::ICC_UGE;
825 case ISD::SETNE:
826 return LPCC::ICC_NE;
827 case ISD::SETONE:
828 case ISD::SETUNE:
829 case ISD::SETOGE:
830 case ISD::SETOLE:
831 case ISD::SETOLT:
832 case ISD::SETOGT:
833 case ISD::SETOEQ:
834 case ISD::SETUEQ:
835 case ISD::SETO:
836 case ISD::SETUO:
837 llvm_unreachable("Unsupported comparison.");
838 default:
839 llvm_unreachable("Unknown integer condition code!");
840 }
841}
842
843SDValue LanaiTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
844 SDValue Chain = Op.getOperand(0);
845 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
846 SDValue LHS = Op.getOperand(2);
847 SDValue RHS = Op.getOperand(3);
848 SDValue Dest = Op.getOperand(4);
849 SDLoc DL(Op);
850
851 SDValue TargetCC =
852 DAG.getConstant(IntCondCCodeToICC(CC, DL, LHS, RHS, DAG), DL, MVT::i32);
853 SDValue Flag =
854 DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);
855
856 return DAG.getNode(LanaiISD::BR_CC, DL, Op.getValueType(), Chain, Dest,
857 TargetCC, Flag);
858}
859
860SDValue LanaiTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
861 EVT VT = Op->getValueType(0);
862 if (VT != MVT::i32)
863 return SDValue();
864
865 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op->getOperand(1));
866 if (!C)
867 return SDValue();
868
869 int64_t MulAmt = C->getSExtValue();
870 int32_t HighestOne = -1;
871 uint32_t NonzeroEntries = 0;
872 int SignedDigit[32] = {0};
873
874 // Convert to non-adjacent form (NAF) signed-digit representation.
875 // NAF is a signed-digit form where no adjacent digits are non-zero. It is the
876 // minimal Hamming weight representation of a number (on average 1/3 of the
877 // digits will be non-zero vs 1/2 for regular binary representation). And as
878 // the non-zero digits will be the only digits contributing to the instruction
879 // count, this is desirable. The next loop converts it to NAF (following the
880 // approach in 'Guide to Elliptic Curve Cryptography' [ISBN: 038795273X]) by
881 // choosing the non-zero coefficients such that the resulting quotient is
882 // divisible by 2 which will cause the next coefficient to be zero.
883 int64_t E = std::abs(MulAmt);
884 int S = (MulAmt < 0 ? -1 : 1);
885 int I = 0;
886 while (E > 0) {
887 int ZI = 0;
888 if (E % 2 == 1) {
889 ZI = 2 - (E % 4);
890 if (ZI != 0)
891 ++NonzeroEntries;
892 }
893 SignedDigit[I] = S * ZI;
894 if (SignedDigit[I] == 1)
895 HighestOne = I;
896 E = (E - ZI) / 2;
897 ++I;
898 }
899
900 // Compute number of instructions required. Due to differences in lowering
901 // between the different processors this count is not exact.
902 // Start by assuming a shift and a add/sub for every non-zero entry (hence
903 // every non-zero entry requires 1 shift and 1 add/sub except for the first
904 // entry).
905 int32_t InstrRequired = 2 * NonzeroEntries - 1;
906 // Correct possible over-adding due to shift by 0 (which is not emitted).
907 if (std::abs(MulAmt) % 2 == 1)
908 --InstrRequired;
909 // Return if the form generated would exceed the instruction threshold.
910 if (InstrRequired > LanaiLowerConstantMulThreshold)
911 return SDValue();
912
913 SDValue Res;
914 SDLoc DL(Op);
915 SDValue V = Op->getOperand(0);
916
917 // Initialize the running sum. Set the running sum to the maximal shifted
918 // positive value (i.e., largest i such that zi == 1 and MulAmt has V<<i as a
919 // term NAF).
920 if (HighestOne == -1)
921 Res = DAG.getConstant(0, DL, MVT::i32);
922 else {
923 Res = DAG.getNode(ISD::SHL, DL, VT, V,
924 DAG.getConstant(HighestOne, DL, MVT::i32));
925 SignedDigit[HighestOne] = 0;
926 }
927
928 // Assemble multiplication from shift, add, sub using NAF form and running
929 // sum.
930 for (unsigned int I = 0; I < sizeof(SignedDigit) / sizeof(SignedDigit[0]);
931 ++I) {
932 if (SignedDigit[I] == 0)
933 continue;
934
935 // Shifted multiplicand (v<<i).
936 SDValue Op =
937 DAG.getNode(ISD::SHL, DL, VT, V, DAG.getConstant(I, DL, MVT::i32));
938 if (SignedDigit[I] == 1)
939 Res = DAG.getNode(ISD::ADD, DL, VT, Res, Op);
940 else if (SignedDigit[I] == -1)
941 Res = DAG.getNode(ISD::SUB, DL, VT, Res, Op);
942 }
943 return Res;
944}
945
946SDValue LanaiTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
947 SDValue LHS = Op.getOperand(0);
948 SDValue RHS = Op.getOperand(1);
949 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
950 SDLoc DL(Op);
951
952 SDValue TargetCC =
953 DAG.getConstant(IntCondCCodeToICC(CC, DL, LHS, RHS, DAG), DL, MVT::i32);
954 SDValue Flag =
955 DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);
956
957 return DAG.getNode(LanaiISD::SETCC, DL, Op.getValueType(), TargetCC, Flag);
958}
959
960SDValue LanaiTargetLowering::LowerSELECT_CC(SDValue Op,
961 SelectionDAG &DAG) const {
962 SDValue LHS = Op.getOperand(0);
963 SDValue RHS = Op.getOperand(1);
964 SDValue TrueV = Op.getOperand(2);
965 SDValue FalseV = Op.getOperand(3);
966 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
967 SDLoc DL(Op);
968
969 SDValue TargetCC =
970 DAG.getConstant(IntCondCCodeToICC(CC, DL, LHS, RHS, DAG), DL, MVT::i32);
971 SDValue Flag =
972 DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);
973
974 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
975 return DAG.getNode(LanaiISD::SELECT_CC, DL, VTs, TrueV, FalseV, TargetCC,
976 Flag);
977}
978
979SDValue LanaiTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
980 MachineFunction &MF = DAG.getMachineFunction();
981 LanaiMachineFunctionInfo *FuncInfo = MF.getInfo<LanaiMachineFunctionInfo>();
982
983 SDLoc DL(Op);
984 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
985 getPointerTy(DAG.getDataLayout()));
986
987 // vastart just stores the address of the VarArgsFrameIndex slot into the
988 // memory location argument.
989 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
990 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
991 MachinePointerInfo(SV), false, false, 0);
992}
993
994SDValue LanaiTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
995 SelectionDAG &DAG) const {
996 SDValue Chain = Op.getOperand(0);
997 SDValue Size = Op.getOperand(1);
998 SDLoc DL(Op);
999
1000 unsigned SPReg = getStackPointerRegisterToSaveRestore();
1001
1002 // Get a reference to the stack pointer.
1003 SDValue StackPointer = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i32);
1004
1005 // Subtract the dynamic size from the actual stack size to
1006 // obtain the new stack size.
1007 SDValue Sub = DAG.getNode(ISD::SUB, DL, MVT::i32, StackPointer, Size);
1008
1009 // For Lanai, the outgoing memory arguments area should be on top of the
1010 // alloca area on the stack i.e., the outgoing memory arguments should be
1011 // at a lower address than the alloca area. Move the alloca area down the
1012 // stack by adding back the space reserved for outgoing arguments to SP
1013 // here.
1014 //
1015 // We do not know what the size of the outgoing args is at this point.
1016 // So, we add a pseudo instruction ADJDYNALLOC that will adjust the
1017 // stack pointer. We replace this instruction with on that has the correct,
1018 // known offset in emitPrologue().
1019 SDValue ArgAdjust = DAG.getNode(LanaiISD::ADJDYNALLOC, DL, MVT::i32, Sub);
1020
1021 // The Sub result contains the new stack start address, so it
1022 // must be placed in the stack pointer register.
1023 SDValue CopyChain = DAG.getCopyToReg(Chain, DL, SPReg, Sub);
1024
1025 SDValue Ops[2] = {ArgAdjust, CopyChain};
1026 return DAG.getMergeValues(Ops, DL);
1027}
1028
1029SDValue LanaiTargetLowering::LowerRETURNADDR(SDValue Op,
1030 SelectionDAG &DAG) const {
1031 MachineFunction &MF = DAG.getMachineFunction();
1032 MachineFrameInfo *MFI = MF.getFrameInfo();
1033 MFI->setReturnAddressIsTaken(true);
1034
1035 EVT VT = Op.getValueType();
1036 SDLoc DL(Op);
1037 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1038 if (Depth) {
1039 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
1040 const unsigned Offset = -4;
1041 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
1042 DAG.getIntPtrConstant(Offset, DL));
1043 return DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
1044 false, false, false, 0);
1045 }
1046
1047 // Return the link register, which contains the return address.
1048 // Mark it an implicit live-in.
1049 unsigned Reg = MF.addLiveIn(TRI->getRARegister(), getRegClassFor(MVT::i32));
1050 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, VT);
1051}
1052
1053SDValue LanaiTargetLowering::LowerFRAMEADDR(SDValue Op,
1054 SelectionDAG &DAG) const {
1055 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1056 MFI->setFrameAddressIsTaken(true);
1057
1058 EVT VT = Op.getValueType();
1059 SDLoc DL(Op);
1060 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, Lanai::FP, VT);
1061 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1062 while (Depth--) {
1063 const unsigned Offset = -8;
1064 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
1065 DAG.getIntPtrConstant(Offset, DL));
1066 FrameAddr = DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr,
1067 MachinePointerInfo(), false, false, false, 0);
1068 }
1069 return FrameAddr;
1070}
1071
1072const char *LanaiTargetLowering::getTargetNodeName(unsigned Opcode) const {
1073 switch (Opcode) {
1074 case LanaiISD::ADJDYNALLOC:
1075 return "LanaiISD::ADJDYNALLOC";
1076 case LanaiISD::RET_FLAG:
1077 return "LanaiISD::RET_FLAG";
1078 case LanaiISD::CALL:
1079 return "LanaiISD::CALL";
1080 case LanaiISD::SELECT_CC:
1081 return "LanaiISD::SELECT_CC";
1082 case LanaiISD::SETCC:
1083 return "LanaiISD::SETCC";
1084 case LanaiISD::SET_FLAG:
1085 return "LanaiISD::SET_FLAG";
1086 case LanaiISD::BR_CC:
1087 return "LanaiISD::BR_CC";
1088 case LanaiISD::Wrapper:
1089 return "LanaiISD::Wrapper";
1090 case LanaiISD::HI:
1091 return "LanaiISD::HI";
1092 case LanaiISD::LO:
1093 return "LanaiISD::LO";
1094 case LanaiISD::SMALL:
1095 return "LanaiISD::SMALL";
1096 default:
1097 return NULL;
1098 }
1099}
1100
1101SDValue LanaiTargetLowering::LowerConstantPool(SDValue Op,
1102 SelectionDAG &DAG) const {
1103 SDLoc DL(Op);
1104 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1105 const Constant *C = N->getConstVal();
1106 const LanaiTargetObjectFile *TLOF =
1107 static_cast<const LanaiTargetObjectFile *>(
1108 getTargetMachine().getObjFileLowering());
1109
1110 // If the code model is small or constant will be placed in the small section,
1111 // then assume address will fit in 21-bits.
1112 if (getTargetMachine().getCodeModel() == CodeModel::Small ||
1113 TLOF->isConstantInSmallSection(DAG.getDataLayout(), C)) {
1114 SDValue Small = DAG.getTargetConstantPool(
1115 C, MVT::i32, N->getAlignment(), N->getOffset(), LanaiII::MO_NO_FLAG);
1116 return DAG.getNode(ISD::OR, DL, MVT::i32,
1117 DAG.getRegister(Lanai::R0, MVT::i32),
1118 DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));
1119 } else {
1120 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1121 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1122
1123 SDValue Hi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1124 N->getOffset(), OpFlagHi);
1125 SDValue Lo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1126 N->getOffset(), OpFlagLo);
1127 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1128 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1129 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1130 return Result;
1131 }
1132}
1133
1134SDValue LanaiTargetLowering::LowerGlobalAddress(SDValue Op,
1135 SelectionDAG &DAG) const {
1136 SDLoc DL(Op);
1137 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1138 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
1139
1140 const LanaiTargetObjectFile *TLOF =
1141 static_cast<const LanaiTargetObjectFile *>(
1142 getTargetMachine().getObjFileLowering());
1143
1144 // If the code model is small or global variable will be placed in the small
1145 // section, then assume address will fit in 21-bits.
1146 if (getTargetMachine().getCodeModel() == CodeModel::Small ||
1147 TLOF->isGlobalInSmallSection(GV, getTargetMachine())) {
1148 SDValue Small = DAG.getTargetGlobalAddress(
1149 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, LanaiII::MO_NO_FLAG);
1150 return DAG.getNode(ISD::OR, DL, MVT::i32,
1151 DAG.getRegister(Lanai::R0, MVT::i32),
1152 DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));
1153 } else {
1154 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1155 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1156
1157 // Create the TargetGlobalAddress node, folding in the constant offset.
1158 SDValue Hi = DAG.getTargetGlobalAddress(
1159 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, OpFlagHi);
1160 SDValue Lo = DAG.getTargetGlobalAddress(
1161 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, OpFlagLo);
1162 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1163 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1164 return DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1165 }
1166}
1167
1168SDValue LanaiTargetLowering::LowerBlockAddress(SDValue Op,
1169 SelectionDAG &DAG) const {
1170 SDLoc DL(Op);
1171 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1172
1173 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1174 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1175
1176 SDValue Hi = DAG.getBlockAddress(BA, MVT::i32, true, OpFlagHi);
1177 SDValue Lo = DAG.getBlockAddress(BA, MVT::i32, true, OpFlagLo);
1178 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1179 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1180 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1181 return Result;
1182}
1183
1184SDValue LanaiTargetLowering::LowerJumpTable(SDValue Op,
1185 SelectionDAG &DAG) const {
1186 SDLoc DL(Op);
1187 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
1188
1189 // If the code model is small assume address will fit in 21-bits.
1190 if (getTargetMachine().getCodeModel() == CodeModel::Small) {
1191 SDValue Small = DAG.getTargetJumpTable(
1192 JT->getIndex(), getPointerTy(DAG.getDataLayout()), LanaiII::MO_NO_FLAG);
1193 return DAG.getNode(ISD::OR, DL, MVT::i32,
1194 DAG.getRegister(Lanai::R0, MVT::i32),
1195 DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));
1196 } else {
1197 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1198 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1199
1200 SDValue Hi = DAG.getTargetJumpTable(
1201 JT->getIndex(), getPointerTy(DAG.getDataLayout()), OpFlagHi);
1202 SDValue Lo = DAG.getTargetJumpTable(
1203 JT->getIndex(), getPointerTy(DAG.getDataLayout()), OpFlagLo);
1204 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1205 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1206 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1207 return Result;
1208 }
1209}
Jacques Pienaarad1db352016-04-14 17:59:22 +00001210
1211SDValue LanaiTargetLowering::LowerSRL_PARTS(SDValue Op,
1212 SelectionDAG &DAG) const {
1213 MVT VT = Op.getSimpleValueType();
1214 unsigned VTBits = VT.getSizeInBits();
1215 SDLoc dl(Op);
1216 SDValue ShOpLo = Op.getOperand(0);
1217 SDValue ShOpHi = Op.getOperand(1);
1218 SDValue ShAmt = Op.getOperand(2);
1219
1220 // Performs the following for a >> b:
1221 // unsigned r_high = a_high >> b;
1222 // r_high = (32 - b <= 0) ? 0 : r_high;
1223 //
1224 // unsigned r_low = a_low >> b;
1225 // r_low = (32 - b <= 0) ? r_high : r_low;
1226 // r_low = (b == 0) ? r_low : r_low | (a_high << (32 - b));
1227 // return (unsigned long long)r_high << 32 | r_low;
1228 // Note: This takes advantage of Lanai's shift behavior to avoid needing to
1229 // mask the shift amount.
1230
1231 SDValue Zero = DAG.getConstant(0, dl, MVT::i32);
1232 SDValue NegatedPlus32 = DAG.getNode(
1233 ISD::SUB, dl, MVT::i32, DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
1234 SDValue SetCC = DAG.getSetCC(dl, MVT::i32, NegatedPlus32, Zero, ISD::SETLE);
1235
1236 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i32, ShOpHi, ShAmt);
1237 Hi = DAG.getSelect(dl, MVT::i32, SetCC, Zero, Hi);
1238
1239 SDValue Lo = DAG.getNode(ISD::SRL, dl, MVT::i32, ShOpLo, ShAmt);
1240 Lo = DAG.getSelect(dl, MVT::i32, SetCC, Hi, Lo);
1241 SDValue CarryBits =
1242 DAG.getNode(ISD::SHL, dl, MVT::i32, ShOpHi, NegatedPlus32);
1243 SDValue ShiftIsZero = DAG.getSetCC(dl, MVT::i32, ShAmt, Zero, ISD::SETEQ);
1244 Lo = DAG.getSelect(dl, MVT::i32, ShiftIsZero, Lo,
1245 DAG.getNode(ISD::OR, dl, MVT::i32, Lo, CarryBits));
1246
1247 SDValue Ops[2] = {Lo, Hi};
1248 return DAG.getMergeValues(Ops, dl);
1249}