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