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