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