blob: 66416b38e812a76b242ed262081e45246194e232 [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
Jacques Pienaare2f06992016-07-15 22:38:32 +0000200unsigned LanaiTargetLowering::getRegisterByName(const char *RegName, EVT /*VT*/,
201 SelectionDAG & /*DAG*/) const {
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000202 // 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,
Jacques Pienaare2f06992016-07-15 22:38:32 +0000582 bool /*IsTailCall*/, const SmallVectorImpl<ISD::OutputArg> &Outs,
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000583 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,
Jacques Pienaare2f06992016-07-15 22:38:32 +0000629 /*isTailCall=*/false, MachinePointerInfo(),
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000630 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,
Jacques Pienaare2f06992016-07-15 22:38:32 +0000789 SDValue &RHS, SelectionDAG &DAG) {
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000790 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
791
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000792 // For integer, only the SETEQ, SETNE, SETLT, SETLE, SETGT, SETGE, SETULT,
793 // SETULE, SETUGT, and SETUGE opcodes are used (see CodeGen/ISDOpcodes.h)
794 // and Lanai only supports integer comparisons, so only provide definitions
795 // for them.
796 switch (SetCCOpcode) {
797 case ISD::SETEQ:
798 return LPCC::ICC_EQ;
799 case ISD::SETGT:
800 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
801 if (RHSC->getZExtValue() == 0xFFFFFFFF) {
802 // X > -1 -> X >= 0 -> is_plus(X)
803 RHS = DAG.getConstant(0, DL, RHS.getValueType());
804 return LPCC::ICC_PL;
805 }
806 return LPCC::ICC_GT;
807 case ISD::SETUGT:
808 return LPCC::ICC_UGT;
809 case ISD::SETLT:
810 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
811 if (RHSC->getZExtValue() == 0)
812 // X < 0 -> is_minus(X)
813 return LPCC::ICC_MI;
814 return LPCC::ICC_LT;
815 case ISD::SETULT:
816 return LPCC::ICC_ULT;
817 case ISD::SETLE:
818 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
819 if (RHSC->getZExtValue() == 0xFFFFFFFF) {
820 // X <= -1 -> X < 0 -> is_minus(X)
821 RHS = DAG.getConstant(0, DL, RHS.getValueType());
822 return LPCC::ICC_MI;
823 }
824 return LPCC::ICC_LE;
825 case ISD::SETULE:
826 return LPCC::ICC_ULE;
827 case ISD::SETGE:
828 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
829 if (RHSC->getZExtValue() == 0)
830 // X >= 0 -> is_plus(X)
831 return LPCC::ICC_PL;
832 return LPCC::ICC_GE;
833 case ISD::SETUGE:
834 return LPCC::ICC_UGE;
835 case ISD::SETNE:
836 return LPCC::ICC_NE;
837 case ISD::SETONE:
838 case ISD::SETUNE:
839 case ISD::SETOGE:
840 case ISD::SETOLE:
841 case ISD::SETOLT:
842 case ISD::SETOGT:
843 case ISD::SETOEQ:
844 case ISD::SETUEQ:
845 case ISD::SETO:
846 case ISD::SETUO:
847 llvm_unreachable("Unsupported comparison.");
848 default:
849 llvm_unreachable("Unknown integer condition code!");
850 }
851}
852
853SDValue LanaiTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
854 SDValue Chain = Op.getOperand(0);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000855 SDValue Cond = Op.getOperand(1);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000856 SDValue LHS = Op.getOperand(2);
857 SDValue RHS = Op.getOperand(3);
858 SDValue Dest = Op.getOperand(4);
859 SDLoc DL(Op);
860
Jacques Pienaare2f06992016-07-15 22:38:32 +0000861 LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, RHS, DAG);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000862 SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000863 SDValue Flag =
864 DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);
865
866 return DAG.getNode(LanaiISD::BR_CC, DL, Op.getValueType(), Chain, Dest,
867 TargetCC, Flag);
868}
869
870SDValue LanaiTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
871 EVT VT = Op->getValueType(0);
872 if (VT != MVT::i32)
873 return SDValue();
874
875 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op->getOperand(1));
876 if (!C)
877 return SDValue();
878
879 int64_t MulAmt = C->getSExtValue();
880 int32_t HighestOne = -1;
881 uint32_t NonzeroEntries = 0;
882 int SignedDigit[32] = {0};
883
884 // Convert to non-adjacent form (NAF) signed-digit representation.
885 // NAF is a signed-digit form where no adjacent digits are non-zero. It is the
886 // minimal Hamming weight representation of a number (on average 1/3 of the
887 // digits will be non-zero vs 1/2 for regular binary representation). And as
888 // the non-zero digits will be the only digits contributing to the instruction
889 // count, this is desirable. The next loop converts it to NAF (following the
890 // approach in 'Guide to Elliptic Curve Cryptography' [ISBN: 038795273X]) by
891 // choosing the non-zero coefficients such that the resulting quotient is
892 // divisible by 2 which will cause the next coefficient to be zero.
893 int64_t E = std::abs(MulAmt);
894 int S = (MulAmt < 0 ? -1 : 1);
895 int I = 0;
896 while (E > 0) {
897 int ZI = 0;
898 if (E % 2 == 1) {
899 ZI = 2 - (E % 4);
900 if (ZI != 0)
901 ++NonzeroEntries;
902 }
903 SignedDigit[I] = S * ZI;
904 if (SignedDigit[I] == 1)
905 HighestOne = I;
906 E = (E - ZI) / 2;
907 ++I;
908 }
909
910 // Compute number of instructions required. Due to differences in lowering
911 // between the different processors this count is not exact.
912 // Start by assuming a shift and a add/sub for every non-zero entry (hence
913 // every non-zero entry requires 1 shift and 1 add/sub except for the first
914 // entry).
915 int32_t InstrRequired = 2 * NonzeroEntries - 1;
916 // Correct possible over-adding due to shift by 0 (which is not emitted).
917 if (std::abs(MulAmt) % 2 == 1)
918 --InstrRequired;
919 // Return if the form generated would exceed the instruction threshold.
920 if (InstrRequired > LanaiLowerConstantMulThreshold)
921 return SDValue();
922
923 SDValue Res;
924 SDLoc DL(Op);
925 SDValue V = Op->getOperand(0);
926
927 // Initialize the running sum. Set the running sum to the maximal shifted
928 // positive value (i.e., largest i such that zi == 1 and MulAmt has V<<i as a
929 // term NAF).
930 if (HighestOne == -1)
931 Res = DAG.getConstant(0, DL, MVT::i32);
932 else {
933 Res = DAG.getNode(ISD::SHL, DL, VT, V,
934 DAG.getConstant(HighestOne, DL, MVT::i32));
935 SignedDigit[HighestOne] = 0;
936 }
937
938 // Assemble multiplication from shift, add, sub using NAF form and running
939 // sum.
940 for (unsigned int I = 0; I < sizeof(SignedDigit) / sizeof(SignedDigit[0]);
941 ++I) {
942 if (SignedDigit[I] == 0)
943 continue;
944
945 // Shifted multiplicand (v<<i).
946 SDValue Op =
947 DAG.getNode(ISD::SHL, DL, VT, V, DAG.getConstant(I, DL, MVT::i32));
948 if (SignedDigit[I] == 1)
949 Res = DAG.getNode(ISD::ADD, DL, VT, Res, Op);
950 else if (SignedDigit[I] == -1)
951 Res = DAG.getNode(ISD::SUB, DL, VT, Res, Op);
952 }
953 return Res;
954}
955
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000956SDValue LanaiTargetLowering::LowerSETCCE(SDValue Op, SelectionDAG &DAG) const {
957 SDValue LHS = Op.getOperand(0);
958 SDValue RHS = Op.getOperand(1);
959 SDValue Carry = Op.getOperand(2);
960 SDValue Cond = Op.getOperand(3);
961 SDLoc DL(Op);
962
Jacques Pienaare2f06992016-07-15 22:38:32 +0000963 LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, RHS, DAG);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000964 SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
965 SDValue Flag = DAG.getNode(LanaiISD::SUBBF, DL, MVT::Glue, LHS, RHS, Carry);
966 return DAG.getNode(LanaiISD::SETCC, DL, Op.getValueType(), TargetCC, Flag);
967}
968
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000969SDValue LanaiTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
970 SDValue LHS = Op.getOperand(0);
971 SDValue RHS = Op.getOperand(1);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000972 SDValue Cond = Op.getOperand(2);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000973 SDLoc DL(Op);
974
Jacques Pienaare2f06992016-07-15 22:38:32 +0000975 LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, RHS, DAG);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000976 SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000977 SDValue Flag =
978 DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);
979
980 return DAG.getNode(LanaiISD::SETCC, DL, Op.getValueType(), TargetCC, Flag);
981}
982
983SDValue LanaiTargetLowering::LowerSELECT_CC(SDValue Op,
984 SelectionDAG &DAG) const {
985 SDValue LHS = Op.getOperand(0);
986 SDValue RHS = Op.getOperand(1);
987 SDValue TrueV = Op.getOperand(2);
988 SDValue FalseV = Op.getOperand(3);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000989 SDValue Cond = Op.getOperand(4);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000990 SDLoc DL(Op);
991
Jacques Pienaare2f06992016-07-15 22:38:32 +0000992 LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, RHS, DAG);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000993 SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000994 SDValue Flag =
995 DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);
996
997 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
998 return DAG.getNode(LanaiISD::SELECT_CC, DL, VTs, TrueV, FalseV, TargetCC,
999 Flag);
1000}
1001
1002SDValue LanaiTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1003 MachineFunction &MF = DAG.getMachineFunction();
1004 LanaiMachineFunctionInfo *FuncInfo = MF.getInfo<LanaiMachineFunctionInfo>();
1005
1006 SDLoc DL(Op);
1007 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1008 getPointerTy(DAG.getDataLayout()));
1009
1010 // vastart just stores the address of the VarArgsFrameIndex slot into the
1011 // memory location argument.
1012 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1013 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Jacques Pienaare6503192016-07-15 22:18:33 +00001014 MachinePointerInfo(SV));
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001015}
1016
1017SDValue LanaiTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1018 SelectionDAG &DAG) const {
1019 SDValue Chain = Op.getOperand(0);
1020 SDValue Size = Op.getOperand(1);
1021 SDLoc DL(Op);
1022
1023 unsigned SPReg = getStackPointerRegisterToSaveRestore();
1024
1025 // Get a reference to the stack pointer.
1026 SDValue StackPointer = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i32);
1027
1028 // Subtract the dynamic size from the actual stack size to
1029 // obtain the new stack size.
1030 SDValue Sub = DAG.getNode(ISD::SUB, DL, MVT::i32, StackPointer, Size);
1031
1032 // For Lanai, the outgoing memory arguments area should be on top of the
1033 // alloca area on the stack i.e., the outgoing memory arguments should be
1034 // at a lower address than the alloca area. Move the alloca area down the
1035 // stack by adding back the space reserved for outgoing arguments to SP
1036 // here.
1037 //
1038 // We do not know what the size of the outgoing args is at this point.
1039 // So, we add a pseudo instruction ADJDYNALLOC that will adjust the
1040 // stack pointer. We replace this instruction with on that has the correct,
1041 // known offset in emitPrologue().
1042 SDValue ArgAdjust = DAG.getNode(LanaiISD::ADJDYNALLOC, DL, MVT::i32, Sub);
1043
1044 // The Sub result contains the new stack start address, so it
1045 // must be placed in the stack pointer register.
1046 SDValue CopyChain = DAG.getCopyToReg(Chain, DL, SPReg, Sub);
1047
1048 SDValue Ops[2] = {ArgAdjust, CopyChain};
1049 return DAG.getMergeValues(Ops, DL);
1050}
1051
1052SDValue LanaiTargetLowering::LowerRETURNADDR(SDValue Op,
1053 SelectionDAG &DAG) const {
1054 MachineFunction &MF = DAG.getMachineFunction();
1055 MachineFrameInfo *MFI = MF.getFrameInfo();
1056 MFI->setReturnAddressIsTaken(true);
1057
1058 EVT VT = Op.getValueType();
1059 SDLoc DL(Op);
1060 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1061 if (Depth) {
1062 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
1063 const unsigned Offset = -4;
1064 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
1065 DAG.getIntPtrConstant(Offset, DL));
Jacques Pienaare6503192016-07-15 22:18:33 +00001066 return DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001067 }
1068
1069 // Return the link register, which contains the return address.
1070 // Mark it an implicit live-in.
1071 unsigned Reg = MF.addLiveIn(TRI->getRARegister(), getRegClassFor(MVT::i32));
1072 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, VT);
1073}
1074
1075SDValue LanaiTargetLowering::LowerFRAMEADDR(SDValue Op,
1076 SelectionDAG &DAG) const {
1077 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1078 MFI->setFrameAddressIsTaken(true);
1079
1080 EVT VT = Op.getValueType();
1081 SDLoc DL(Op);
1082 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, Lanai::FP, VT);
1083 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1084 while (Depth--) {
1085 const unsigned Offset = -8;
1086 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
1087 DAG.getIntPtrConstant(Offset, DL));
Jacques Pienaare6503192016-07-15 22:18:33 +00001088 FrameAddr =
1089 DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001090 }
1091 return FrameAddr;
1092}
1093
1094const char *LanaiTargetLowering::getTargetNodeName(unsigned Opcode) const {
1095 switch (Opcode) {
1096 case LanaiISD::ADJDYNALLOC:
1097 return "LanaiISD::ADJDYNALLOC";
1098 case LanaiISD::RET_FLAG:
1099 return "LanaiISD::RET_FLAG";
1100 case LanaiISD::CALL:
1101 return "LanaiISD::CALL";
1102 case LanaiISD::SELECT_CC:
1103 return "LanaiISD::SELECT_CC";
1104 case LanaiISD::SETCC:
1105 return "LanaiISD::SETCC";
Jacques Pienaar50d4e982016-04-19 19:15:25 +00001106 case LanaiISD::SUBBF:
1107 return "LanaiISD::SUBBF";
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001108 case LanaiISD::SET_FLAG:
1109 return "LanaiISD::SET_FLAG";
1110 case LanaiISD::BR_CC:
1111 return "LanaiISD::BR_CC";
1112 case LanaiISD::Wrapper:
1113 return "LanaiISD::Wrapper";
1114 case LanaiISD::HI:
1115 return "LanaiISD::HI";
1116 case LanaiISD::LO:
1117 return "LanaiISD::LO";
1118 case LanaiISD::SMALL:
1119 return "LanaiISD::SMALL";
1120 default:
1121 return NULL;
1122 }
1123}
1124
1125SDValue LanaiTargetLowering::LowerConstantPool(SDValue Op,
1126 SelectionDAG &DAG) const {
1127 SDLoc DL(Op);
1128 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1129 const Constant *C = N->getConstVal();
1130 const LanaiTargetObjectFile *TLOF =
1131 static_cast<const LanaiTargetObjectFile *>(
1132 getTargetMachine().getObjFileLowering());
1133
1134 // If the code model is small or constant will be placed in the small section,
1135 // then assume address will fit in 21-bits.
1136 if (getTargetMachine().getCodeModel() == CodeModel::Small ||
1137 TLOF->isConstantInSmallSection(DAG.getDataLayout(), C)) {
1138 SDValue Small = DAG.getTargetConstantPool(
1139 C, MVT::i32, N->getAlignment(), N->getOffset(), LanaiII::MO_NO_FLAG);
1140 return DAG.getNode(ISD::OR, DL, MVT::i32,
1141 DAG.getRegister(Lanai::R0, MVT::i32),
1142 DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));
1143 } else {
1144 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1145 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1146
1147 SDValue Hi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1148 N->getOffset(), OpFlagHi);
1149 SDValue Lo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1150 N->getOffset(), OpFlagLo);
1151 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1152 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1153 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1154 return Result;
1155 }
1156}
1157
1158SDValue LanaiTargetLowering::LowerGlobalAddress(SDValue Op,
1159 SelectionDAG &DAG) const {
1160 SDLoc DL(Op);
1161 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1162 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
1163
1164 const LanaiTargetObjectFile *TLOF =
1165 static_cast<const LanaiTargetObjectFile *>(
1166 getTargetMachine().getObjFileLowering());
1167
1168 // If the code model is small or global variable will be placed in the small
1169 // section, then assume address will fit in 21-bits.
1170 if (getTargetMachine().getCodeModel() == CodeModel::Small ||
1171 TLOF->isGlobalInSmallSection(GV, getTargetMachine())) {
1172 SDValue Small = DAG.getTargetGlobalAddress(
1173 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, LanaiII::MO_NO_FLAG);
1174 return DAG.getNode(ISD::OR, DL, MVT::i32,
1175 DAG.getRegister(Lanai::R0, MVT::i32),
1176 DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));
1177 } else {
1178 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1179 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1180
1181 // Create the TargetGlobalAddress node, folding in the constant offset.
1182 SDValue Hi = DAG.getTargetGlobalAddress(
1183 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, OpFlagHi);
1184 SDValue Lo = DAG.getTargetGlobalAddress(
1185 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, OpFlagLo);
1186 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1187 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1188 return DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1189 }
1190}
1191
1192SDValue LanaiTargetLowering::LowerBlockAddress(SDValue Op,
1193 SelectionDAG &DAG) const {
1194 SDLoc DL(Op);
1195 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1196
1197 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1198 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1199
1200 SDValue Hi = DAG.getBlockAddress(BA, MVT::i32, true, OpFlagHi);
1201 SDValue Lo = DAG.getBlockAddress(BA, MVT::i32, true, OpFlagLo);
1202 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1203 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1204 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1205 return Result;
1206}
1207
1208SDValue LanaiTargetLowering::LowerJumpTable(SDValue Op,
1209 SelectionDAG &DAG) const {
1210 SDLoc DL(Op);
1211 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
1212
1213 // If the code model is small assume address will fit in 21-bits.
1214 if (getTargetMachine().getCodeModel() == CodeModel::Small) {
1215 SDValue Small = DAG.getTargetJumpTable(
1216 JT->getIndex(), getPointerTy(DAG.getDataLayout()), LanaiII::MO_NO_FLAG);
1217 return DAG.getNode(ISD::OR, DL, MVT::i32,
1218 DAG.getRegister(Lanai::R0, MVT::i32),
1219 DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));
1220 } else {
1221 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1222 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1223
1224 SDValue Hi = DAG.getTargetJumpTable(
1225 JT->getIndex(), getPointerTy(DAG.getDataLayout()), OpFlagHi);
1226 SDValue Lo = DAG.getTargetJumpTable(
1227 JT->getIndex(), getPointerTy(DAG.getDataLayout()), OpFlagLo);
1228 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1229 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1230 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1231 return Result;
1232 }
1233}
Jacques Pienaarad1db352016-04-14 17:59:22 +00001234
1235SDValue LanaiTargetLowering::LowerSRL_PARTS(SDValue Op,
1236 SelectionDAG &DAG) const {
1237 MVT VT = Op.getSimpleValueType();
1238 unsigned VTBits = VT.getSizeInBits();
1239 SDLoc dl(Op);
1240 SDValue ShOpLo = Op.getOperand(0);
1241 SDValue ShOpHi = Op.getOperand(1);
1242 SDValue ShAmt = Op.getOperand(2);
1243
1244 // Performs the following for a >> b:
1245 // unsigned r_high = a_high >> b;
1246 // r_high = (32 - b <= 0) ? 0 : r_high;
1247 //
1248 // unsigned r_low = a_low >> b;
1249 // r_low = (32 - b <= 0) ? r_high : r_low;
1250 // r_low = (b == 0) ? r_low : r_low | (a_high << (32 - b));
1251 // return (unsigned long long)r_high << 32 | r_low;
1252 // Note: This takes advantage of Lanai's shift behavior to avoid needing to
1253 // mask the shift amount.
1254
1255 SDValue Zero = DAG.getConstant(0, dl, MVT::i32);
1256 SDValue NegatedPlus32 = DAG.getNode(
1257 ISD::SUB, dl, MVT::i32, DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
1258 SDValue SetCC = DAG.getSetCC(dl, MVT::i32, NegatedPlus32, Zero, ISD::SETLE);
1259
1260 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i32, ShOpHi, ShAmt);
1261 Hi = DAG.getSelect(dl, MVT::i32, SetCC, Zero, Hi);
1262
1263 SDValue Lo = DAG.getNode(ISD::SRL, dl, MVT::i32, ShOpLo, ShAmt);
1264 Lo = DAG.getSelect(dl, MVT::i32, SetCC, Hi, Lo);
1265 SDValue CarryBits =
1266 DAG.getNode(ISD::SHL, dl, MVT::i32, ShOpHi, NegatedPlus32);
1267 SDValue ShiftIsZero = DAG.getSetCC(dl, MVT::i32, ShAmt, Zero, ISD::SETEQ);
1268 Lo = DAG.getSelect(dl, MVT::i32, ShiftIsZero, Lo,
1269 DAG.getNode(ISD::OR, dl, MVT::i32, Lo, CarryBits));
1270
1271 SDValue Ops[2] = {Lo, Hi};
1272 return DAG.getMergeValues(Ops, dl);
1273}
Jacques Pienaar6d3eecc2016-07-07 23:36:04 +00001274
1275// Helper function that checks if N is a null or all ones constant.
1276static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
1277 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
1278}
1279
1280// Return true if N is conditionally 0 or all ones.
1281// Detects these expressions where cc is an i1 value:
1282//
1283// (select cc 0, y) [AllOnes=0]
1284// (select cc y, 0) [AllOnes=0]
1285// (zext cc) [AllOnes=0]
1286// (sext cc) [AllOnes=0/1]
1287// (select cc -1, y) [AllOnes=1]
1288// (select cc y, -1) [AllOnes=1]
1289//
1290// * AllOnes determines whether to check for an all zero (AllOnes false) or an
1291// all ones operand (AllOnes true).
1292// * Invert is set when N is the all zero/ones constant when CC is false.
1293// * OtherOp is set to the alternative value of N.
1294//
1295// For example, for (select cc X, Y) and AllOnes = 0 if:
1296// * X = 0, Invert = False and OtherOp = Y
1297// * Y = 0, Invert = True and OtherOp = X
1298static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes, SDValue &CC,
1299 bool &Invert, SDValue &OtherOp,
1300 SelectionDAG &DAG) {
1301 switch (N->getOpcode()) {
1302 default:
1303 return false;
1304 case ISD::SELECT: {
1305 CC = N->getOperand(0);
1306 SDValue N1 = N->getOperand(1);
1307 SDValue N2 = N->getOperand(2);
1308 if (isZeroOrAllOnes(N1, AllOnes)) {
1309 Invert = false;
1310 OtherOp = N2;
1311 return true;
1312 }
1313 if (isZeroOrAllOnes(N2, AllOnes)) {
1314 Invert = true;
1315 OtherOp = N1;
1316 return true;
1317 }
1318 return false;
1319 }
1320 case ISD::ZERO_EXTEND: {
1321 // (zext cc) can never be the all ones value.
1322 if (AllOnes)
1323 return false;
1324 CC = N->getOperand(0);
1325 if (CC.getValueType() != MVT::i1)
1326 return false;
1327 SDLoc dl(N);
1328 EVT VT = N->getValueType(0);
1329 OtherOp = DAG.getConstant(1, dl, VT);
1330 Invert = true;
1331 return true;
1332 }
1333 case ISD::SIGN_EXTEND: {
1334 CC = N->getOperand(0);
1335 if (CC.getValueType() != MVT::i1)
1336 return false;
1337 SDLoc dl(N);
1338 EVT VT = N->getValueType(0);
1339 Invert = !AllOnes;
1340 if (AllOnes)
1341 // When looking for an AllOnes constant, N is an sext, and the 'other'
1342 // value is 0.
1343 OtherOp = DAG.getConstant(0, dl, VT);
1344 else
1345 OtherOp =
1346 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl, VT);
1347 return true;
1348 }
1349 }
1350}
1351
1352// Combine a constant select operand into its use:
1353//
1354// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1355// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1356// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
1357// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
1358// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
1359//
1360// The transform is rejected if the select doesn't have a constant operand that
1361// is null, or all ones when AllOnes is set.
1362//
1363// Also recognize sext/zext from i1:
1364//
1365// (add (zext cc), x) -> (select cc (add x, 1), x)
1366// (add (sext cc), x) -> (select cc (add x, -1), x)
1367//
1368// These transformations eventually create predicated instructions.
1369static SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
1370 TargetLowering::DAGCombinerInfo &DCI,
1371 bool AllOnes) {
1372 SelectionDAG &DAG = DCI.DAG;
1373 EVT VT = N->getValueType(0);
1374 SDValue NonConstantVal;
1375 SDValue CCOp;
1376 bool SwapSelectOps;
1377 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
1378 NonConstantVal, DAG))
1379 return SDValue();
1380
1381 // Slct is now know to be the desired identity constant when CC is true.
1382 SDValue TrueVal = OtherOp;
1383 SDValue FalseVal =
1384 DAG.getNode(N->getOpcode(), SDLoc(N), VT, OtherOp, NonConstantVal);
1385 // Unless SwapSelectOps says CC should be false.
1386 if (SwapSelectOps)
1387 std::swap(TrueVal, FalseVal);
1388
1389 return DAG.getNode(ISD::SELECT, SDLoc(N), VT, CCOp, TrueVal, FalseVal);
1390}
1391
1392// Attempt combineSelectAndUse on each operand of a commutative operator N.
1393static SDValue
1394combineSelectAndUseCommutative(SDNode *N, TargetLowering::DAGCombinerInfo &DCI,
1395 bool AllOnes) {
1396 SDValue N0 = N->getOperand(0);
1397 SDValue N1 = N->getOperand(1);
1398 if (N0.getNode()->hasOneUse())
1399 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes))
1400 return Result;
1401 if (N1.getNode()->hasOneUse())
1402 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes))
1403 return Result;
1404 return SDValue();
1405}
1406
1407// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
1408static SDValue PerformSUBCombine(SDNode *N,
1409 TargetLowering::DAGCombinerInfo &DCI) {
1410 SDValue N0 = N->getOperand(0);
1411 SDValue N1 = N->getOperand(1);
1412
1413 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1414 if (N1.getNode()->hasOneUse())
1415 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, /*AllOnes=*/false))
1416 return Result;
1417
1418 return SDValue();
1419}
1420
1421SDValue LanaiTargetLowering::PerformDAGCombine(SDNode *N,
1422 DAGCombinerInfo &DCI) const {
1423 switch (N->getOpcode()) {
1424 default:
1425 break;
1426 case ISD::ADD:
1427 case ISD::OR:
1428 case ISD::XOR:
1429 return combineSelectAndUseCommutative(N, DCI, /*AllOnes=*/false);
1430 case ISD::AND:
1431 return combineSelectAndUseCommutative(N, DCI, /*AllOnes=*/true);
1432 case ISD::SUB:
1433 return PerformSUBCombine(N, DCI);
1434 }
1435
1436 return SDValue();
1437}