blob: df3a2514e6710ebd272cab791fcae6d10f3fd9f3 [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,
376 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
377 SmallVectorImpl<SDValue> &InVals) const {
378 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,
417 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
418 SmallVectorImpl<SDValue> &InVals) const {
419 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,
517 SDLoc DL, SelectionDAG &DAG) const {
518 // 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,
579 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
580 SmallVectorImpl<SDValue> &InVals) const {
581 // 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,
758 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
759 SmallVectorImpl<SDValue> &InVals) const {
760 // 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
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000783static LPCC::CondCode IntCondCCodeToICC(SDValue CC, SDLoc DL, SDValue &LHS,
784 SDValue &RHS, SelectionDAG &DAG) {
785 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
786
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000787 // For integer, only the SETEQ, SETNE, SETLT, SETLE, SETGT, SETGE, SETULT,
788 // SETULE, SETUGT, and SETUGE opcodes are used (see CodeGen/ISDOpcodes.h)
789 // and Lanai only supports integer comparisons, so only provide definitions
790 // for them.
791 switch (SetCCOpcode) {
792 case ISD::SETEQ:
793 return LPCC::ICC_EQ;
794 case ISD::SETGT:
795 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
796 if (RHSC->getZExtValue() == 0xFFFFFFFF) {
797 // X > -1 -> X >= 0 -> is_plus(X)
798 RHS = DAG.getConstant(0, DL, RHS.getValueType());
799 return LPCC::ICC_PL;
800 }
801 return LPCC::ICC_GT;
802 case ISD::SETUGT:
803 return LPCC::ICC_UGT;
804 case ISD::SETLT:
805 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
806 if (RHSC->getZExtValue() == 0)
807 // X < 0 -> is_minus(X)
808 return LPCC::ICC_MI;
809 return LPCC::ICC_LT;
810 case ISD::SETULT:
811 return LPCC::ICC_ULT;
812 case ISD::SETLE:
813 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
814 if (RHSC->getZExtValue() == 0xFFFFFFFF) {
815 // X <= -1 -> X < 0 -> is_minus(X)
816 RHS = DAG.getConstant(0, DL, RHS.getValueType());
817 return LPCC::ICC_MI;
818 }
819 return LPCC::ICC_LE;
820 case ISD::SETULE:
821 return LPCC::ICC_ULE;
822 case ISD::SETGE:
823 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
824 if (RHSC->getZExtValue() == 0)
825 // X >= 0 -> is_plus(X)
826 return LPCC::ICC_PL;
827 return LPCC::ICC_GE;
828 case ISD::SETUGE:
829 return LPCC::ICC_UGE;
830 case ISD::SETNE:
831 return LPCC::ICC_NE;
832 case ISD::SETONE:
833 case ISD::SETUNE:
834 case ISD::SETOGE:
835 case ISD::SETOLE:
836 case ISD::SETOLT:
837 case ISD::SETOGT:
838 case ISD::SETOEQ:
839 case ISD::SETUEQ:
840 case ISD::SETO:
841 case ISD::SETUO:
842 llvm_unreachable("Unsupported comparison.");
843 default:
844 llvm_unreachable("Unknown integer condition code!");
845 }
846}
847
848SDValue LanaiTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
849 SDValue Chain = Op.getOperand(0);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000850 SDValue Cond = Op.getOperand(1);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000851 SDValue LHS = Op.getOperand(2);
852 SDValue RHS = Op.getOperand(3);
853 SDValue Dest = Op.getOperand(4);
854 SDLoc DL(Op);
855
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000856 LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, LHS, RHS, DAG);
857 SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000858 SDValue Flag =
859 DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);
860
861 return DAG.getNode(LanaiISD::BR_CC, DL, Op.getValueType(), Chain, Dest,
862 TargetCC, Flag);
863}
864
865SDValue LanaiTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
866 EVT VT = Op->getValueType(0);
867 if (VT != MVT::i32)
868 return SDValue();
869
870 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op->getOperand(1));
871 if (!C)
872 return SDValue();
873
874 int64_t MulAmt = C->getSExtValue();
875 int32_t HighestOne = -1;
876 uint32_t NonzeroEntries = 0;
877 int SignedDigit[32] = {0};
878
879 // Convert to non-adjacent form (NAF) signed-digit representation.
880 // NAF is a signed-digit form where no adjacent digits are non-zero. It is the
881 // minimal Hamming weight representation of a number (on average 1/3 of the
882 // digits will be non-zero vs 1/2 for regular binary representation). And as
883 // the non-zero digits will be the only digits contributing to the instruction
884 // count, this is desirable. The next loop converts it to NAF (following the
885 // approach in 'Guide to Elliptic Curve Cryptography' [ISBN: 038795273X]) by
886 // choosing the non-zero coefficients such that the resulting quotient is
887 // divisible by 2 which will cause the next coefficient to be zero.
888 int64_t E = std::abs(MulAmt);
889 int S = (MulAmt < 0 ? -1 : 1);
890 int I = 0;
891 while (E > 0) {
892 int ZI = 0;
893 if (E % 2 == 1) {
894 ZI = 2 - (E % 4);
895 if (ZI != 0)
896 ++NonzeroEntries;
897 }
898 SignedDigit[I] = S * ZI;
899 if (SignedDigit[I] == 1)
900 HighestOne = I;
901 E = (E - ZI) / 2;
902 ++I;
903 }
904
905 // Compute number of instructions required. Due to differences in lowering
906 // between the different processors this count is not exact.
907 // Start by assuming a shift and a add/sub for every non-zero entry (hence
908 // every non-zero entry requires 1 shift and 1 add/sub except for the first
909 // entry).
910 int32_t InstrRequired = 2 * NonzeroEntries - 1;
911 // Correct possible over-adding due to shift by 0 (which is not emitted).
912 if (std::abs(MulAmt) % 2 == 1)
913 --InstrRequired;
914 // Return if the form generated would exceed the instruction threshold.
915 if (InstrRequired > LanaiLowerConstantMulThreshold)
916 return SDValue();
917
918 SDValue Res;
919 SDLoc DL(Op);
920 SDValue V = Op->getOperand(0);
921
922 // Initialize the running sum. Set the running sum to the maximal shifted
923 // positive value (i.e., largest i such that zi == 1 and MulAmt has V<<i as a
924 // term NAF).
925 if (HighestOne == -1)
926 Res = DAG.getConstant(0, DL, MVT::i32);
927 else {
928 Res = DAG.getNode(ISD::SHL, DL, VT, V,
929 DAG.getConstant(HighestOne, DL, MVT::i32));
930 SignedDigit[HighestOne] = 0;
931 }
932
933 // Assemble multiplication from shift, add, sub using NAF form and running
934 // sum.
935 for (unsigned int I = 0; I < sizeof(SignedDigit) / sizeof(SignedDigit[0]);
936 ++I) {
937 if (SignedDigit[I] == 0)
938 continue;
939
940 // Shifted multiplicand (v<<i).
941 SDValue Op =
942 DAG.getNode(ISD::SHL, DL, VT, V, DAG.getConstant(I, DL, MVT::i32));
943 if (SignedDigit[I] == 1)
944 Res = DAG.getNode(ISD::ADD, DL, VT, Res, Op);
945 else if (SignedDigit[I] == -1)
946 Res = DAG.getNode(ISD::SUB, DL, VT, Res, Op);
947 }
948 return Res;
949}
950
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000951SDValue LanaiTargetLowering::LowerSETCCE(SDValue Op, SelectionDAG &DAG) const {
952 SDValue LHS = Op.getOperand(0);
953 SDValue RHS = Op.getOperand(1);
954 SDValue Carry = Op.getOperand(2);
955 SDValue Cond = Op.getOperand(3);
956 SDLoc DL(Op);
957
958 LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, LHS, RHS, DAG);
959 SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
960 SDValue Flag = DAG.getNode(LanaiISD::SUBBF, DL, MVT::Glue, LHS, RHS, Carry);
961 return DAG.getNode(LanaiISD::SETCC, DL, Op.getValueType(), TargetCC, Flag);
962}
963
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000964SDValue LanaiTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
965 SDValue LHS = Op.getOperand(0);
966 SDValue RHS = Op.getOperand(1);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000967 SDValue Cond = Op.getOperand(2);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000968 SDLoc DL(Op);
969
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000970 LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, LHS, RHS, DAG);
971 SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000972 SDValue Flag =
973 DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);
974
975 return DAG.getNode(LanaiISD::SETCC, DL, Op.getValueType(), TargetCC, Flag);
976}
977
978SDValue LanaiTargetLowering::LowerSELECT_CC(SDValue Op,
979 SelectionDAG &DAG) const {
980 SDValue LHS = Op.getOperand(0);
981 SDValue RHS = Op.getOperand(1);
982 SDValue TrueV = Op.getOperand(2);
983 SDValue FalseV = Op.getOperand(3);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000984 SDValue Cond = Op.getOperand(4);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000985 SDLoc DL(Op);
986
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000987 LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, LHS, RHS, DAG);
988 SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000989 SDValue Flag =
990 DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);
991
992 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
993 return DAG.getNode(LanaiISD::SELECT_CC, DL, VTs, TrueV, FalseV, TargetCC,
994 Flag);
995}
996
997SDValue LanaiTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
998 MachineFunction &MF = DAG.getMachineFunction();
999 LanaiMachineFunctionInfo *FuncInfo = MF.getInfo<LanaiMachineFunctionInfo>();
1000
1001 SDLoc DL(Op);
1002 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1003 getPointerTy(DAG.getDataLayout()));
1004
1005 // vastart just stores the address of the VarArgsFrameIndex slot into the
1006 // memory location argument.
1007 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1008 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
1009 MachinePointerInfo(SV), false, false, 0);
1010}
1011
1012SDValue LanaiTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1013 SelectionDAG &DAG) const {
1014 SDValue Chain = Op.getOperand(0);
1015 SDValue Size = Op.getOperand(1);
1016 SDLoc DL(Op);
1017
1018 unsigned SPReg = getStackPointerRegisterToSaveRestore();
1019
1020 // Get a reference to the stack pointer.
1021 SDValue StackPointer = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i32);
1022
1023 // Subtract the dynamic size from the actual stack size to
1024 // obtain the new stack size.
1025 SDValue Sub = DAG.getNode(ISD::SUB, DL, MVT::i32, StackPointer, Size);
1026
1027 // For Lanai, the outgoing memory arguments area should be on top of the
1028 // alloca area on the stack i.e., the outgoing memory arguments should be
1029 // at a lower address than the alloca area. Move the alloca area down the
1030 // stack by adding back the space reserved for outgoing arguments to SP
1031 // here.
1032 //
1033 // We do not know what the size of the outgoing args is at this point.
1034 // So, we add a pseudo instruction ADJDYNALLOC that will adjust the
1035 // stack pointer. We replace this instruction with on that has the correct,
1036 // known offset in emitPrologue().
1037 SDValue ArgAdjust = DAG.getNode(LanaiISD::ADJDYNALLOC, DL, MVT::i32, Sub);
1038
1039 // The Sub result contains the new stack start address, so it
1040 // must be placed in the stack pointer register.
1041 SDValue CopyChain = DAG.getCopyToReg(Chain, DL, SPReg, Sub);
1042
1043 SDValue Ops[2] = {ArgAdjust, CopyChain};
1044 return DAG.getMergeValues(Ops, DL);
1045}
1046
1047SDValue LanaiTargetLowering::LowerRETURNADDR(SDValue Op,
1048 SelectionDAG &DAG) const {
1049 MachineFunction &MF = DAG.getMachineFunction();
1050 MachineFrameInfo *MFI = MF.getFrameInfo();
1051 MFI->setReturnAddressIsTaken(true);
1052
1053 EVT VT = Op.getValueType();
1054 SDLoc DL(Op);
1055 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1056 if (Depth) {
1057 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
1058 const unsigned Offset = -4;
1059 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
1060 DAG.getIntPtrConstant(Offset, DL));
1061 return DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
1062 false, false, false, 0);
1063 }
1064
1065 // Return the link register, which contains the return address.
1066 // Mark it an implicit live-in.
1067 unsigned Reg = MF.addLiveIn(TRI->getRARegister(), getRegClassFor(MVT::i32));
1068 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, VT);
1069}
1070
1071SDValue LanaiTargetLowering::LowerFRAMEADDR(SDValue Op,
1072 SelectionDAG &DAG) const {
1073 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1074 MFI->setFrameAddressIsTaken(true);
1075
1076 EVT VT = Op.getValueType();
1077 SDLoc DL(Op);
1078 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, Lanai::FP, VT);
1079 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1080 while (Depth--) {
1081 const unsigned Offset = -8;
1082 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
1083 DAG.getIntPtrConstant(Offset, DL));
1084 FrameAddr = DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr,
1085 MachinePointerInfo(), false, false, false, 0);
1086 }
1087 return FrameAddr;
1088}
1089
1090const char *LanaiTargetLowering::getTargetNodeName(unsigned Opcode) const {
1091 switch (Opcode) {
1092 case LanaiISD::ADJDYNALLOC:
1093 return "LanaiISD::ADJDYNALLOC";
1094 case LanaiISD::RET_FLAG:
1095 return "LanaiISD::RET_FLAG";
1096 case LanaiISD::CALL:
1097 return "LanaiISD::CALL";
1098 case LanaiISD::SELECT_CC:
1099 return "LanaiISD::SELECT_CC";
1100 case LanaiISD::SETCC:
1101 return "LanaiISD::SETCC";
Jacques Pienaar50d4e982016-04-19 19:15:25 +00001102 case LanaiISD::SUBBF:
1103 return "LanaiISD::SUBBF";
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001104 case LanaiISD::SET_FLAG:
1105 return "LanaiISD::SET_FLAG";
1106 case LanaiISD::BR_CC:
1107 return "LanaiISD::BR_CC";
1108 case LanaiISD::Wrapper:
1109 return "LanaiISD::Wrapper";
1110 case LanaiISD::HI:
1111 return "LanaiISD::HI";
1112 case LanaiISD::LO:
1113 return "LanaiISD::LO";
1114 case LanaiISD::SMALL:
1115 return "LanaiISD::SMALL";
1116 default:
1117 return NULL;
1118 }
1119}
1120
1121SDValue LanaiTargetLowering::LowerConstantPool(SDValue Op,
1122 SelectionDAG &DAG) const {
1123 SDLoc DL(Op);
1124 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1125 const Constant *C = N->getConstVal();
1126 const LanaiTargetObjectFile *TLOF =
1127 static_cast<const LanaiTargetObjectFile *>(
1128 getTargetMachine().getObjFileLowering());
1129
1130 // If the code model is small or constant will be placed in the small section,
1131 // then assume address will fit in 21-bits.
1132 if (getTargetMachine().getCodeModel() == CodeModel::Small ||
1133 TLOF->isConstantInSmallSection(DAG.getDataLayout(), C)) {
1134 SDValue Small = DAG.getTargetConstantPool(
1135 C, MVT::i32, N->getAlignment(), N->getOffset(), LanaiII::MO_NO_FLAG);
1136 return DAG.getNode(ISD::OR, DL, MVT::i32,
1137 DAG.getRegister(Lanai::R0, MVT::i32),
1138 DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));
1139 } else {
1140 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1141 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1142
1143 SDValue Hi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1144 N->getOffset(), OpFlagHi);
1145 SDValue Lo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1146 N->getOffset(), OpFlagLo);
1147 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1148 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1149 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1150 return Result;
1151 }
1152}
1153
1154SDValue LanaiTargetLowering::LowerGlobalAddress(SDValue Op,
1155 SelectionDAG &DAG) const {
1156 SDLoc DL(Op);
1157 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1158 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
1159
1160 const LanaiTargetObjectFile *TLOF =
1161 static_cast<const LanaiTargetObjectFile *>(
1162 getTargetMachine().getObjFileLowering());
1163
1164 // If the code model is small or global variable will be placed in the small
1165 // section, then assume address will fit in 21-bits.
1166 if (getTargetMachine().getCodeModel() == CodeModel::Small ||
1167 TLOF->isGlobalInSmallSection(GV, getTargetMachine())) {
1168 SDValue Small = DAG.getTargetGlobalAddress(
1169 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, LanaiII::MO_NO_FLAG);
1170 return DAG.getNode(ISD::OR, DL, MVT::i32,
1171 DAG.getRegister(Lanai::R0, MVT::i32),
1172 DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));
1173 } else {
1174 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1175 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1176
1177 // Create the TargetGlobalAddress node, folding in the constant offset.
1178 SDValue Hi = DAG.getTargetGlobalAddress(
1179 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, OpFlagHi);
1180 SDValue Lo = DAG.getTargetGlobalAddress(
1181 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, OpFlagLo);
1182 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1183 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1184 return DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1185 }
1186}
1187
1188SDValue LanaiTargetLowering::LowerBlockAddress(SDValue Op,
1189 SelectionDAG &DAG) const {
1190 SDLoc DL(Op);
1191 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1192
1193 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1194 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1195
1196 SDValue Hi = DAG.getBlockAddress(BA, MVT::i32, true, OpFlagHi);
1197 SDValue Lo = DAG.getBlockAddress(BA, MVT::i32, true, OpFlagLo);
1198 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1199 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1200 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1201 return Result;
1202}
1203
1204SDValue LanaiTargetLowering::LowerJumpTable(SDValue Op,
1205 SelectionDAG &DAG) const {
1206 SDLoc DL(Op);
1207 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
1208
1209 // If the code model is small assume address will fit in 21-bits.
1210 if (getTargetMachine().getCodeModel() == CodeModel::Small) {
1211 SDValue Small = DAG.getTargetJumpTable(
1212 JT->getIndex(), getPointerTy(DAG.getDataLayout()), LanaiII::MO_NO_FLAG);
1213 return DAG.getNode(ISD::OR, DL, MVT::i32,
1214 DAG.getRegister(Lanai::R0, MVT::i32),
1215 DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));
1216 } else {
1217 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1218 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1219
1220 SDValue Hi = DAG.getTargetJumpTable(
1221 JT->getIndex(), getPointerTy(DAG.getDataLayout()), OpFlagHi);
1222 SDValue Lo = DAG.getTargetJumpTable(
1223 JT->getIndex(), getPointerTy(DAG.getDataLayout()), OpFlagLo);
1224 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1225 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1226 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1227 return Result;
1228 }
1229}
Jacques Pienaarad1db352016-04-14 17:59:22 +00001230
1231SDValue LanaiTargetLowering::LowerSRL_PARTS(SDValue Op,
1232 SelectionDAG &DAG) const {
1233 MVT VT = Op.getSimpleValueType();
1234 unsigned VTBits = VT.getSizeInBits();
1235 SDLoc dl(Op);
1236 SDValue ShOpLo = Op.getOperand(0);
1237 SDValue ShOpHi = Op.getOperand(1);
1238 SDValue ShAmt = Op.getOperand(2);
1239
1240 // Performs the following for a >> b:
1241 // unsigned r_high = a_high >> b;
1242 // r_high = (32 - b <= 0) ? 0 : r_high;
1243 //
1244 // unsigned r_low = a_low >> b;
1245 // r_low = (32 - b <= 0) ? r_high : r_low;
1246 // r_low = (b == 0) ? r_low : r_low | (a_high << (32 - b));
1247 // return (unsigned long long)r_high << 32 | r_low;
1248 // Note: This takes advantage of Lanai's shift behavior to avoid needing to
1249 // mask the shift amount.
1250
1251 SDValue Zero = DAG.getConstant(0, dl, MVT::i32);
1252 SDValue NegatedPlus32 = DAG.getNode(
1253 ISD::SUB, dl, MVT::i32, DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
1254 SDValue SetCC = DAG.getSetCC(dl, MVT::i32, NegatedPlus32, Zero, ISD::SETLE);
1255
1256 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i32, ShOpHi, ShAmt);
1257 Hi = DAG.getSelect(dl, MVT::i32, SetCC, Zero, Hi);
1258
1259 SDValue Lo = DAG.getNode(ISD::SRL, dl, MVT::i32, ShOpLo, ShAmt);
1260 Lo = DAG.getSelect(dl, MVT::i32, SetCC, Hi, Lo);
1261 SDValue CarryBits =
1262 DAG.getNode(ISD::SHL, dl, MVT::i32, ShOpHi, NegatedPlus32);
1263 SDValue ShiftIsZero = DAG.getSetCC(dl, MVT::i32, ShAmt, Zero, ISD::SETEQ);
1264 Lo = DAG.getSelect(dl, MVT::i32, ShiftIsZero, Lo,
1265 DAG.getNode(ISD::OR, dl, MVT::i32, Lo, CarryBits));
1266
1267 SDValue Ops[2] = {Lo, Hi};
1268 return DAG.getMergeValues(Ops, dl);
1269}