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