blob: fca402572b249087f8fbcdf22254ea0051c107f5 [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
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000014#include "Lanai.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000015#include "LanaiCondCode.h"
16#include "LanaiISelLowering.h"
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000017#include "LanaiMachineFunctionInfo.h"
18#include "LanaiSubtarget.h"
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000019#include "LanaiTargetObjectFile.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000020#include "MCTargetDesc/LanaiBaseInfo.h"
21#include "llvm/ADT/APInt.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/StringSwitch.h"
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000026#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000029#include "llvm/CodeGen/MachineMemOperand.h"
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000031#include "llvm/CodeGen/MachineValueType.h"
32#include "llvm/CodeGen/RuntimeLibcalls.h"
33#include "llvm/CodeGen/SelectionDAG.h"
34#include "llvm/CodeGen/SelectionDAGNodes.h"
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000035#include "llvm/CodeGen/ValueTypes.h"
36#include "llvm/IR/CallingConv.h"
37#include "llvm/IR/DerivedTypes.h"
38#include "llvm/IR/Function.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000039#include "llvm/IR/GlobalValue.h"
40#include "llvm/Support/Casting.h"
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000041#include "llvm/Support/CommandLine.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000042#include "llvm/Support/CodeGen.h"
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000043#include "llvm/Support/Debug.h"
44#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000045#include "llvm/Support/MathExtras.h"
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000046#include "llvm/Support/raw_ostream.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000047#include "llvm/Target/TargetCallingConv.h"
48#include "llvm/Target/TargetMachine.h"
49#include <cassert>
50#include <cmath>
51#include <cstdint>
52#include <cstdlib>
53#include <utility>
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000054
55#define DEBUG_TYPE "lanai-lower"
56
57using namespace llvm;
58
59// Limit on number of instructions the lowered multiplication may have before a
60// call to the library function should be generated instead. The threshold is
61// currently set to 14 as this was the smallest threshold that resulted in all
62// constant multiplications being lowered. A threshold of 5 covered all cases
63// except for one multiplication which required 14. mulsi3 requires 16
64// instructions (including the prologue and epilogue but excluding instructions
65// at call site). Until we can inline mulsi3, generating at most 14 instructions
66// will be faster than invoking mulsi3.
67static cl::opt<int> LanaiLowerConstantMulThreshold(
68 "lanai-constant-mul-threshold", cl::Hidden,
69 cl::desc("Maximum number of instruction to generate when lowering constant "
70 "multiplication instead of calling library function [default=14]"),
71 cl::init(14));
72
73LanaiTargetLowering::LanaiTargetLowering(const TargetMachine &TM,
74 const LanaiSubtarget &STI)
75 : TargetLowering(TM) {
76 // Set up the register classes.
77 addRegisterClass(MVT::i32, &Lanai::GPRRegClass);
78
79 // Compute derived properties from the register classes
80 TRI = STI.getRegisterInfo();
81 computeRegisterProperties(TRI);
82
83 setStackPointerRegisterToSaveRestore(Lanai::SP);
84
85 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
86 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
87 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
88 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Jacques Pienaar50d4e982016-04-19 19:15:25 +000089 setOperationAction(ISD::SETCCE, MVT::i32, Custom);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +000090 setOperationAction(ISD::SELECT, MVT::i32, Expand);
91 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
92
93 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
94 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
95 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
96 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
97
98 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
99 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
100 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
101
102 setOperationAction(ISD::VASTART, MVT::Other, Custom);
103 setOperationAction(ISD::VAARG, MVT::Other, Expand);
104 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
105 setOperationAction(ISD::VAEND, MVT::Other, Expand);
106
107 setOperationAction(ISD::SDIV, MVT::i32, Expand);
108 setOperationAction(ISD::UDIV, MVT::i32, Expand);
109 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
110 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
111 setOperationAction(ISD::SREM, MVT::i32, Expand);
112 setOperationAction(ISD::UREM, MVT::i32, Expand);
113
114 setOperationAction(ISD::MUL, MVT::i32, Custom);
115 setOperationAction(ISD::MULHU, MVT::i32, Expand);
116 setOperationAction(ISD::MULHS, MVT::i32, Expand);
117 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
118 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
119
120 setOperationAction(ISD::ROTR, MVT::i32, Expand);
121 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jacques Pienaar3bec3ef2016-12-02 22:01:28 +0000122 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jacques Pienaarad1db352016-04-14 17:59:22 +0000123 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000124 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
125
126 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
127 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
128 setOperationAction(ISD::CTLZ, MVT::i32, Legal);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000129 setOperationAction(ISD::CTTZ, MVT::i32, Legal);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000130
131 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
132 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
133 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
134
135 // Extended load operations for i1 types must be promoted
136 for (MVT VT : MVT::integer_valuetypes()) {
137 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
138 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
139 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
140 }
141
Jacques Pienaar6d3eecc2016-07-07 23:36:04 +0000142 setTargetDAGCombine(ISD::ADD);
143 setTargetDAGCombine(ISD::SUB);
144 setTargetDAGCombine(ISD::AND);
145 setTargetDAGCombine(ISD::OR);
146 setTargetDAGCombine(ISD::XOR);
147
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000148 // Function alignments (log2)
149 setMinFunctionAlignment(2);
150 setPrefFunctionAlignment(2);
151
152 setJumpIsExpensive(true);
153
154 // TODO: Setting the minimum jump table entries needed before a
155 // switch is transformed to a jump table to 100 to avoid creating jump tables
156 // as this was causing bad performance compared to a large group of if
157 // statements. Re-evaluate this on new benchmarks.
158 setMinimumJumpTableEntries(100);
159
160 // Use fast calling convention for library functions.
161 for (int I = 0; I < RTLIB::UNKNOWN_LIBCALL; ++I) {
162 setLibcallCallingConv(static_cast<RTLIB::Libcall>(I), CallingConv::Fast);
163 }
164
165 MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
166 MaxStoresPerMemsetOptSize = 8;
167 MaxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
168 MaxStoresPerMemcpyOptSize = 8;
169 MaxStoresPerMemmove = 16; // For @llvm.memmove -> sequence of stores
170 MaxStoresPerMemmoveOptSize = 8;
Jacques Pienaar250c4be2016-04-19 00:26:42 +0000171
172 // Booleans always contain 0 or 1.
173 setBooleanContents(ZeroOrOneBooleanContent);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000174}
175
176SDValue LanaiTargetLowering::LowerOperation(SDValue Op,
177 SelectionDAG &DAG) const {
178 switch (Op.getOpcode()) {
179 case ISD::MUL:
180 return LowerMUL(Op, DAG);
181 case ISD::BR_CC:
182 return LowerBR_CC(Op, DAG);
183 case ISD::ConstantPool:
184 return LowerConstantPool(Op, DAG);
185 case ISD::GlobalAddress:
186 return LowerGlobalAddress(Op, DAG);
187 case ISD::BlockAddress:
188 return LowerBlockAddress(Op, DAG);
189 case ISD::JumpTable:
190 return LowerJumpTable(Op, DAG);
191 case ISD::SELECT_CC:
192 return LowerSELECT_CC(Op, DAG);
193 case ISD::SETCC:
194 return LowerSETCC(Op, DAG);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000195 case ISD::SETCCE:
196 return LowerSETCCE(Op, DAG);
Jacques Pienaar3bec3ef2016-12-02 22:01:28 +0000197 case ISD::SHL_PARTS:
198 return LowerSHL_PARTS(Op, DAG);
Jacques Pienaarad1db352016-04-14 17:59:22 +0000199 case ISD::SRL_PARTS:
200 return LowerSRL_PARTS(Op, DAG);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000201 case ISD::VASTART:
202 return LowerVASTART(Op, DAG);
203 case ISD::DYNAMIC_STACKALLOC:
204 return LowerDYNAMIC_STACKALLOC(Op, DAG);
205 case ISD::RETURNADDR:
206 return LowerRETURNADDR(Op, DAG);
207 case ISD::FRAMEADDR:
208 return LowerFRAMEADDR(Op, DAG);
209 default:
210 llvm_unreachable("unimplemented operand");
211 }
212}
Eugene Zelenko049b0172017-01-06 00:30:53 +0000213
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000214//===----------------------------------------------------------------------===//
215// Lanai Inline Assembly Support
216//===----------------------------------------------------------------------===//
217
Jacques Pienaare2f06992016-07-15 22:38:32 +0000218unsigned LanaiTargetLowering::getRegisterByName(const char *RegName, EVT /*VT*/,
219 SelectionDAG & /*DAG*/) const {
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000220 // Only unallocatable registers should be matched here.
221 unsigned Reg = StringSwitch<unsigned>(RegName)
222 .Case("pc", Lanai::PC)
223 .Case("sp", Lanai::SP)
224 .Case("fp", Lanai::FP)
225 .Case("rr1", Lanai::RR1)
226 .Case("r10", Lanai::R10)
227 .Case("rr2", Lanai::RR2)
228 .Case("r11", Lanai::R11)
229 .Case("rca", Lanai::RCA)
230 .Default(0);
231
232 if (Reg)
233 return Reg;
234 report_fatal_error("Invalid register name global variable");
235}
236
237std::pair<unsigned, const TargetRegisterClass *>
238LanaiTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
239 StringRef Constraint,
240 MVT VT) const {
241 if (Constraint.size() == 1)
242 // GCC Constraint Letters
243 switch (Constraint[0]) {
244 case 'r': // GENERAL_REGS
245 return std::make_pair(0U, &Lanai::GPRRegClass);
246 default:
247 break;
248 }
249
250 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
251}
252
253// Examine constraint type and operand type and determine a weight value.
254// This object must already have been set up with the operand type
255// and the current alternative constraint selected.
256TargetLowering::ConstraintWeight
257LanaiTargetLowering::getSingleConstraintMatchWeight(
258 AsmOperandInfo &Info, const char *Constraint) const {
259 ConstraintWeight Weight = CW_Invalid;
260 Value *CallOperandVal = Info.CallOperandVal;
261 // If we don't have a value, we can't do a match,
262 // but allow it at the lowest weight.
Eugene Zelenko049b0172017-01-06 00:30:53 +0000263 if (CallOperandVal == nullptr)
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000264 return CW_Default;
265 // Look at the constraint type.
266 switch (*Constraint) {
267 case 'I': // signed 16 bit immediate
268 case 'J': // integer zero
269 case 'K': // unsigned 16 bit immediate
270 case 'L': // immediate in the range 0 to 31
271 case 'M': // signed 32 bit immediate where lower 16 bits are 0
272 case 'N': // signed 26 bit immediate
273 case 'O': // integer zero
274 if (isa<ConstantInt>(CallOperandVal))
275 Weight = CW_Constant;
276 break;
277 default:
278 Weight = TargetLowering::getSingleConstraintMatchWeight(Info, Constraint);
279 break;
280 }
281 return Weight;
282}
283
284// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
285// vector. If it is invalid, don't add anything to Ops.
286void LanaiTargetLowering::LowerAsmOperandForConstraint(
287 SDValue Op, std::string &Constraint, std::vector<SDValue> &Ops,
288 SelectionDAG &DAG) const {
Eugene Zelenko049b0172017-01-06 00:30:53 +0000289 SDValue Result(nullptr, 0);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000290
291 // Only support length 1 constraints for now.
292 if (Constraint.length() > 1)
293 return;
294
295 char ConstraintLetter = Constraint[0];
296 switch (ConstraintLetter) {
297 case 'I': // Signed 16 bit constant
298 // If this fails, the parent routine will give an error
299 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
300 if (isInt<16>(C->getSExtValue())) {
301 Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(C),
302 Op.getValueType());
303 break;
304 }
305 }
306 return;
307 case 'J': // integer zero
308 case 'O':
309 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
310 if (C->getZExtValue() == 0) {
311 Result = DAG.getTargetConstant(0, SDLoc(C), Op.getValueType());
312 break;
313 }
314 }
315 return;
316 case 'K': // unsigned 16 bit immediate
317 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
318 if (isUInt<16>(C->getZExtValue())) {
319 Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(C),
320 Op.getValueType());
321 break;
322 }
323 }
324 return;
325 case 'L': // immediate in the range 0 to 31
326 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
327 if (C->getZExtValue() <= 31) {
328 Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(C),
329 Op.getValueType());
330 break;
331 }
332 }
333 return;
334 case 'M': // signed 32 bit immediate where lower 16 bits are 0
335 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
336 int64_t Val = C->getSExtValue();
337 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)) {
338 Result = DAG.getTargetConstant(Val, SDLoc(C), Op.getValueType());
339 break;
340 }
341 }
342 return;
343 case 'N': // signed 26 bit immediate
344 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
345 int64_t Val = C->getSExtValue();
346 if ((Val >= -33554432) && (Val <= 33554431)) {
347 Result = DAG.getTargetConstant(Val, SDLoc(C), Op.getValueType());
348 break;
349 }
350 }
351 return;
352 default:
353 break; // This will fall through to the generic implementation
354 }
355
356 if (Result.getNode()) {
357 Ops.push_back(Result);
358 return;
359 }
360
361 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
362}
363
364//===----------------------------------------------------------------------===//
365// Calling Convention Implementation
366//===----------------------------------------------------------------------===//
367
368#include "LanaiGenCallingConv.inc"
369
370static unsigned NumFixedArgs;
371static bool CC_Lanai32_VarArg(unsigned ValNo, MVT ValVT, MVT LocVT,
372 CCValAssign::LocInfo LocInfo,
373 ISD::ArgFlagsTy ArgFlags, CCState &State) {
374 // Handle fixed arguments with default CC.
375 // Note: Both the default and fast CC handle VarArg the same and hence the
376 // calling convention of the function is not considered here.
377 if (ValNo < NumFixedArgs) {
378 return CC_Lanai32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
379 }
380
381 // Promote i8/i16 args to i32
382 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
383 LocVT = MVT::i32;
384 if (ArgFlags.isSExt())
385 LocInfo = CCValAssign::SExt;
386 else if (ArgFlags.isZExt())
387 LocInfo = CCValAssign::ZExt;
388 else
389 LocInfo = CCValAssign::AExt;
390 }
391
392 // VarArgs get passed on stack
393 unsigned Offset = State.AllocateStack(4, 4);
394 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
395 return false;
396}
397
398SDValue LanaiTargetLowering::LowerFormalArguments(
399 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000400 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
401 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000402 switch (CallConv) {
403 case CallingConv::C:
404 case CallingConv::Fast:
405 return LowerCCCArguments(Chain, CallConv, IsVarArg, Ins, DL, DAG, InVals);
406 default:
407 llvm_unreachable("Unsupported calling convention");
408 }
409}
410
411SDValue LanaiTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
412 SmallVectorImpl<SDValue> &InVals) const {
413 SelectionDAG &DAG = CLI.DAG;
414 SDLoc &DL = CLI.DL;
415 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
416 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
417 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
418 SDValue Chain = CLI.Chain;
419 SDValue Callee = CLI.Callee;
420 bool &IsTailCall = CLI.IsTailCall;
421 CallingConv::ID CallConv = CLI.CallConv;
422 bool IsVarArg = CLI.IsVarArg;
423
424 // Lanai target does not yet support tail call optimization.
425 IsTailCall = false;
426
427 switch (CallConv) {
428 case CallingConv::Fast:
429 case CallingConv::C:
430 return LowerCCCCallTo(Chain, Callee, CallConv, IsVarArg, IsTailCall, Outs,
431 OutVals, Ins, DL, DAG, InVals);
432 default:
433 llvm_unreachable("Unsupported calling convention");
434 }
435}
436
437// LowerCCCArguments - transform physical registers into virtual registers and
438// generate load operations for arguments places on the stack.
439SDValue LanaiTargetLowering::LowerCCCArguments(
440 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000441 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
442 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000443 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000444 MachineFrameInfo &MFI = MF.getFrameInfo();
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000445 MachineRegisterInfo &RegInfo = MF.getRegInfo();
446 LanaiMachineFunctionInfo *LanaiMFI = MF.getInfo<LanaiMachineFunctionInfo>();
447
448 // Assign locations to all of the incoming arguments.
449 SmallVector<CCValAssign, 16> ArgLocs;
450 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
451 *DAG.getContext());
452 if (CallConv == CallingConv::Fast) {
453 CCInfo.AnalyzeFormalArguments(Ins, CC_Lanai32_Fast);
454 } else {
455 CCInfo.AnalyzeFormalArguments(Ins, CC_Lanai32);
456 }
457
458 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
459 CCValAssign &VA = ArgLocs[i];
460 if (VA.isRegLoc()) {
461 // Arguments passed in registers
462 EVT RegVT = VA.getLocVT();
463 switch (RegVT.getSimpleVT().SimpleTy) {
464 case MVT::i32: {
465 unsigned VReg = RegInfo.createVirtualRegister(&Lanai::GPRRegClass);
466 RegInfo.addLiveIn(VA.getLocReg(), VReg);
467 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, RegVT);
468
469 // If this is an 8/16-bit value, it is really passed promoted to 32
470 // bits. Insert an assert[sz]ext to capture this, then truncate to the
471 // right size.
472 if (VA.getLocInfo() == CCValAssign::SExt)
473 ArgValue = DAG.getNode(ISD::AssertSext, DL, RegVT, ArgValue,
474 DAG.getValueType(VA.getValVT()));
475 else if (VA.getLocInfo() == CCValAssign::ZExt)
476 ArgValue = DAG.getNode(ISD::AssertZext, DL, RegVT, ArgValue,
477 DAG.getValueType(VA.getValVT()));
478
479 if (VA.getLocInfo() != CCValAssign::Full)
480 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), ArgValue);
481
482 InVals.push_back(ArgValue);
483 break;
484 }
485 default:
486 DEBUG(dbgs() << "LowerFormalArguments Unhandled argument type: "
Craig Topperefea6db2016-04-24 16:30:51 +0000487 << RegVT.getEVTString() << "\n");
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000488 llvm_unreachable("unhandled argument type");
489 }
490 } else {
491 // Sanity check
492 assert(VA.isMemLoc());
493 // Load the argument to a virtual register
494 unsigned ObjSize = VA.getLocVT().getSizeInBits() / 8;
495 // Check that the argument fits in stack slot
496 if (ObjSize > 4) {
497 errs() << "LowerFormalArguments Unhandled argument type: "
498 << EVT(VA.getLocVT()).getEVTString() << "\n";
499 }
500 // Create the frame index object for this incoming parameter...
Matthias Braun941a7052016-07-28 18:40:00 +0000501 int FI = MFI.CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000502
503 // Create the SelectionDAG nodes corresponding to a load
504 // from this parameter
505 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
506 InVals.push_back(DAG.getLoad(
507 VA.getLocVT(), DL, Chain, FIN,
Jacques Pienaare6503192016-07-15 22:18:33 +0000508 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)));
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000509 }
510 }
511
512 // The Lanai ABI for returning structs by value requires that we copy
513 // the sret argument into rv for the return. Save the argument into
514 // a virtual register so that we can access it from the return points.
515 if (MF.getFunction()->hasStructRetAttr()) {
516 unsigned Reg = LanaiMFI->getSRetReturnReg();
517 if (!Reg) {
518 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
519 LanaiMFI->setSRetReturnReg(Reg);
520 }
521 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
522 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
523 }
524
525 if (IsVarArg) {
526 // Record the frame index of the first variable argument
527 // which is a value necessary to VASTART.
Matthias Braun941a7052016-07-28 18:40:00 +0000528 int FI = MFI.CreateFixedObject(4, CCInfo.getNextStackOffset(), true);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000529 LanaiMFI->setVarArgsFrameIndex(FI);
530 }
531
532 return Chain;
533}
534
535SDValue
536LanaiTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
537 bool IsVarArg,
538 const SmallVectorImpl<ISD::OutputArg> &Outs,
539 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000540 const SDLoc &DL, SelectionDAG &DAG) const {
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000541 // CCValAssign - represent the assignment of the return value to a location
542 SmallVector<CCValAssign, 16> RVLocs;
543
544 // CCState - Info about the registers and stack slot.
545 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
546 *DAG.getContext());
547
548 // Analize return values.
549 CCInfo.AnalyzeReturn(Outs, RetCC_Lanai32);
550
551 SDValue Flag;
552 SmallVector<SDValue, 4> RetOps(1, Chain);
553
554 // Copy the result values into the output registers.
555 for (unsigned i = 0; i != RVLocs.size(); ++i) {
556 CCValAssign &VA = RVLocs[i];
557 assert(VA.isRegLoc() && "Can only return in registers!");
558
559 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVals[i], Flag);
560
561 // Guarantee that all emitted copies are stuck together with flags.
562 Flag = Chain.getValue(1);
563 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
564 }
565
566 // The Lanai ABI for returning structs by value requires that we copy
567 // the sret argument into rv for the return. We saved the argument into
568 // a virtual register in the entry block, so now we copy the value out
569 // and into rv.
570 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
571 MachineFunction &MF = DAG.getMachineFunction();
572 LanaiMachineFunctionInfo *LanaiMFI = MF.getInfo<LanaiMachineFunctionInfo>();
573 unsigned Reg = LanaiMFI->getSRetReturnReg();
574 assert(Reg &&
575 "SRetReturnReg should have been set in LowerFormalArguments().");
576 SDValue Val =
577 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
578
579 Chain = DAG.getCopyToReg(Chain, DL, Lanai::RV, Val, Flag);
580 Flag = Chain.getValue(1);
581 RetOps.push_back(
582 DAG.getRegister(Lanai::RV, getPointerTy(DAG.getDataLayout())));
583 }
584
585 RetOps[0] = Chain; // Update chain
586
587 unsigned Opc = LanaiISD::RET_FLAG;
588 if (Flag.getNode())
589 RetOps.push_back(Flag);
590
591 // Return Void
592 return DAG.getNode(Opc, DL, MVT::Other,
593 ArrayRef<SDValue>(&RetOps[0], RetOps.size()));
594}
595
596// LowerCCCCallTo - functions arguments are copied from virtual regs to
597// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
598SDValue LanaiTargetLowering::LowerCCCCallTo(
599 SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool IsVarArg,
Jacques Pienaare2f06992016-07-15 22:38:32 +0000600 bool /*IsTailCall*/, const SmallVectorImpl<ISD::OutputArg> &Outs,
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000601 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000602 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
603 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000604 // Analyze operands of the call, assigning locations to each operand.
605 SmallVector<CCValAssign, 16> ArgLocs;
606 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
607 *DAG.getContext());
608 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Matthias Braun941a7052016-07-28 18:40:00 +0000609 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000610
611 NumFixedArgs = 0;
612 if (IsVarArg && G) {
613 const Function *CalleeFn = dyn_cast<Function>(G->getGlobal());
614 if (CalleeFn)
615 NumFixedArgs = CalleeFn->getFunctionType()->getNumParams();
616 }
617 if (NumFixedArgs)
618 CCInfo.AnalyzeCallOperands(Outs, CC_Lanai32_VarArg);
619 else {
620 if (CallConv == CallingConv::Fast)
621 CCInfo.AnalyzeCallOperands(Outs, CC_Lanai32_Fast);
622 else
623 CCInfo.AnalyzeCallOperands(Outs, CC_Lanai32);
624 }
625
626 // Get a count of how many bytes are to be pushed on the stack.
627 unsigned NumBytes = CCInfo.getNextStackOffset();
628
629 // Create local copies for byval args.
630 SmallVector<SDValue, 8> ByValArgs;
631 for (unsigned I = 0, E = Outs.size(); I != E; ++I) {
632 ISD::ArgFlagsTy Flags = Outs[I].Flags;
633 if (!Flags.isByVal())
634 continue;
635
636 SDValue Arg = OutVals[I];
637 unsigned Size = Flags.getByValSize();
638 unsigned Align = Flags.getByValAlign();
639
Matthias Braun941a7052016-07-28 18:40:00 +0000640 int FI = MFI.CreateStackObject(Size, Align, false);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000641 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
642 SDValue SizeNode = DAG.getConstant(Size, DL, MVT::i32);
643
644 Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Align,
645 /*IsVolatile=*/false,
646 /*AlwaysInline=*/false,
Jacques Pienaare2f06992016-07-15 22:38:32 +0000647 /*isTailCall=*/false, MachinePointerInfo(),
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000648 MachinePointerInfo());
649 ByValArgs.push_back(FIPtr);
650 }
651
Serge Pavlovd526b132017-05-09 13:35:13 +0000652 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000653
654 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
655 SmallVector<SDValue, 12> MemOpChains;
656 SDValue StackPtr;
657
658 // Walk the register/memloc assignments, inserting copies/loads.
659 for (unsigned I = 0, J = 0, E = ArgLocs.size(); I != E; ++I) {
660 CCValAssign &VA = ArgLocs[I];
661 SDValue Arg = OutVals[I];
662 ISD::ArgFlagsTy Flags = Outs[I].Flags;
663
664 // Promote the value if needed.
665 switch (VA.getLocInfo()) {
666 case CCValAssign::Full:
667 break;
668 case CCValAssign::SExt:
669 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
670 break;
671 case CCValAssign::ZExt:
672 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
673 break;
674 case CCValAssign::AExt:
675 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
676 break;
677 default:
678 llvm_unreachable("Unknown loc info!");
679 }
680
681 // Use local copy if it is a byval arg.
682 if (Flags.isByVal())
683 Arg = ByValArgs[J++];
684
685 // Arguments that can be passed on register must be kept at RegsToPass
686 // vector
687 if (VA.isRegLoc()) {
688 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
689 } else {
690 assert(VA.isMemLoc());
691
Eugene Zelenko049b0172017-01-06 00:30:53 +0000692 if (StackPtr.getNode() == nullptr)
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000693 StackPtr = DAG.getCopyFromReg(Chain, DL, Lanai::SP,
694 getPointerTy(DAG.getDataLayout()));
695
696 SDValue PtrOff =
697 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
698 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
699
Jacques Pienaare6503192016-07-15 22:18:33 +0000700 MemOpChains.push_back(
701 DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo()));
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000702 }
703 }
704
705 // Transform all store nodes into one single node because all store nodes are
706 // independent of each other.
707 if (!MemOpChains.empty())
708 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
709 ArrayRef<SDValue>(&MemOpChains[0], MemOpChains.size()));
710
711 SDValue InFlag;
712
713 // Build a sequence of copy-to-reg nodes chained together with token chain and
714 // flag operands which copy the outgoing args into registers. The InFlag in
715 // necessary since all emitted instructions must be stuck together.
716 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
717 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
718 RegsToPass[I].second, InFlag);
719 InFlag = Chain.getValue(1);
720 }
721
722 // If the callee is a GlobalAddress node (quite common, every direct call is)
723 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
724 // Likewise ExternalSymbol -> TargetExternalSymbol.
725 uint8_t OpFlag = LanaiII::MO_NO_FLAG;
726 if (G) {
727 Callee = DAG.getTargetGlobalAddress(
728 G->getGlobal(), DL, getPointerTy(DAG.getDataLayout()), 0, OpFlag);
729 } else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
730 Callee = DAG.getTargetExternalSymbol(
731 E->getSymbol(), getPointerTy(DAG.getDataLayout()), OpFlag);
732 }
733
734 // Returns a chain & a flag for retval copy to use.
735 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
736 SmallVector<SDValue, 8> Ops;
737 Ops.push_back(Chain);
738 Ops.push_back(Callee);
739
740 // Add a register mask operand representing the call-preserved registers.
741 // TODO: Should return-twice functions be handled?
742 const uint32_t *Mask =
743 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv);
744 assert(Mask && "Missing call preserved mask for calling convention");
745 Ops.push_back(DAG.getRegisterMask(Mask));
746
747 // Add argument registers to the end of the list so that they are
748 // known live into the call.
749 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
750 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
751 RegsToPass[I].second.getValueType()));
752
753 if (InFlag.getNode())
754 Ops.push_back(InFlag);
755
756 Chain = DAG.getNode(LanaiISD::CALL, DL, NodeTys,
757 ArrayRef<SDValue>(&Ops[0], Ops.size()));
758 InFlag = Chain.getValue(1);
759
760 // Create the CALLSEQ_END node.
761 Chain = DAG.getCALLSEQ_END(
762 Chain,
763 DAG.getConstant(NumBytes, DL, getPointerTy(DAG.getDataLayout()), true),
764 DAG.getConstant(0, DL, getPointerTy(DAG.getDataLayout()), true), InFlag,
765 DL);
766 InFlag = Chain.getValue(1);
767
768 // Handle result values, copying them out of physregs into vregs that we
769 // return.
770 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
771 InVals);
772}
773
774// LowerCallResult - Lower the result values of a call into the
775// appropriate copies out of appropriate physical registers.
776SDValue LanaiTargetLowering::LowerCallResult(
777 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000778 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
779 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000780 // Assign locations to each value returned by this call.
781 SmallVector<CCValAssign, 16> RVLocs;
782 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
783 *DAG.getContext());
784
785 CCInfo.AnalyzeCallResult(Ins, RetCC_Lanai32);
786
787 // Copy all of the result registers out of their specified physreg.
788 for (unsigned I = 0; I != RVLocs.size(); ++I) {
789 Chain = DAG.getCopyFromReg(Chain, DL, RVLocs[I].getLocReg(),
790 RVLocs[I].getValVT(), InFlag)
791 .getValue(1);
792 InFlag = Chain.getValue(2);
793 InVals.push_back(Chain.getValue(0));
794 }
795
796 return Chain;
797}
798
799//===----------------------------------------------------------------------===//
800// Custom Lowerings
801//===----------------------------------------------------------------------===//
802
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000803static LPCC::CondCode IntCondCCodeToICC(SDValue CC, const SDLoc &DL,
Jacques Pienaare2f06992016-07-15 22:38:32 +0000804 SDValue &RHS, SelectionDAG &DAG) {
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000805 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
806
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000807 // For integer, only the SETEQ, SETNE, SETLT, SETLE, SETGT, SETGE, SETULT,
808 // SETULE, SETUGT, and SETUGE opcodes are used (see CodeGen/ISDOpcodes.h)
809 // and Lanai only supports integer comparisons, so only provide definitions
810 // for them.
811 switch (SetCCOpcode) {
812 case ISD::SETEQ:
813 return LPCC::ICC_EQ;
814 case ISD::SETGT:
815 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
816 if (RHSC->getZExtValue() == 0xFFFFFFFF) {
817 // X > -1 -> X >= 0 -> is_plus(X)
818 RHS = DAG.getConstant(0, DL, RHS.getValueType());
819 return LPCC::ICC_PL;
820 }
821 return LPCC::ICC_GT;
822 case ISD::SETUGT:
823 return LPCC::ICC_UGT;
824 case ISD::SETLT:
825 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
826 if (RHSC->getZExtValue() == 0)
827 // X < 0 -> is_minus(X)
828 return LPCC::ICC_MI;
829 return LPCC::ICC_LT;
830 case ISD::SETULT:
831 return LPCC::ICC_ULT;
832 case ISD::SETLE:
833 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
834 if (RHSC->getZExtValue() == 0xFFFFFFFF) {
835 // X <= -1 -> X < 0 -> is_minus(X)
836 RHS = DAG.getConstant(0, DL, RHS.getValueType());
837 return LPCC::ICC_MI;
838 }
839 return LPCC::ICC_LE;
840 case ISD::SETULE:
841 return LPCC::ICC_ULE;
842 case ISD::SETGE:
843 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))
844 if (RHSC->getZExtValue() == 0)
845 // X >= 0 -> is_plus(X)
846 return LPCC::ICC_PL;
847 return LPCC::ICC_GE;
848 case ISD::SETUGE:
849 return LPCC::ICC_UGE;
850 case ISD::SETNE:
851 return LPCC::ICC_NE;
852 case ISD::SETONE:
853 case ISD::SETUNE:
854 case ISD::SETOGE:
855 case ISD::SETOLE:
856 case ISD::SETOLT:
857 case ISD::SETOGT:
858 case ISD::SETOEQ:
859 case ISD::SETUEQ:
860 case ISD::SETO:
861 case ISD::SETUO:
862 llvm_unreachable("Unsupported comparison.");
863 default:
864 llvm_unreachable("Unknown integer condition code!");
865 }
866}
867
868SDValue LanaiTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
869 SDValue Chain = Op.getOperand(0);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000870 SDValue Cond = Op.getOperand(1);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000871 SDValue LHS = Op.getOperand(2);
872 SDValue RHS = Op.getOperand(3);
873 SDValue Dest = Op.getOperand(4);
874 SDLoc DL(Op);
875
Jacques Pienaare2f06992016-07-15 22:38:32 +0000876 LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, RHS, DAG);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000877 SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000878 SDValue Flag =
879 DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);
880
881 return DAG.getNode(LanaiISD::BR_CC, DL, Op.getValueType(), Chain, Dest,
882 TargetCC, Flag);
883}
884
885SDValue LanaiTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
886 EVT VT = Op->getValueType(0);
887 if (VT != MVT::i32)
888 return SDValue();
889
890 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op->getOperand(1));
891 if (!C)
892 return SDValue();
893
894 int64_t MulAmt = C->getSExtValue();
895 int32_t HighestOne = -1;
896 uint32_t NonzeroEntries = 0;
897 int SignedDigit[32] = {0};
898
899 // Convert to non-adjacent form (NAF) signed-digit representation.
900 // NAF is a signed-digit form where no adjacent digits are non-zero. It is the
901 // minimal Hamming weight representation of a number (on average 1/3 of the
902 // digits will be non-zero vs 1/2 for regular binary representation). And as
903 // the non-zero digits will be the only digits contributing to the instruction
904 // count, this is desirable. The next loop converts it to NAF (following the
905 // approach in 'Guide to Elliptic Curve Cryptography' [ISBN: 038795273X]) by
906 // choosing the non-zero coefficients such that the resulting quotient is
907 // divisible by 2 which will cause the next coefficient to be zero.
908 int64_t E = std::abs(MulAmt);
909 int S = (MulAmt < 0 ? -1 : 1);
910 int I = 0;
911 while (E > 0) {
912 int ZI = 0;
913 if (E % 2 == 1) {
914 ZI = 2 - (E % 4);
915 if (ZI != 0)
916 ++NonzeroEntries;
917 }
918 SignedDigit[I] = S * ZI;
919 if (SignedDigit[I] == 1)
920 HighestOne = I;
921 E = (E - ZI) / 2;
922 ++I;
923 }
924
925 // Compute number of instructions required. Due to differences in lowering
926 // between the different processors this count is not exact.
927 // Start by assuming a shift and a add/sub for every non-zero entry (hence
928 // every non-zero entry requires 1 shift and 1 add/sub except for the first
929 // entry).
930 int32_t InstrRequired = 2 * NonzeroEntries - 1;
931 // Correct possible over-adding due to shift by 0 (which is not emitted).
932 if (std::abs(MulAmt) % 2 == 1)
933 --InstrRequired;
934 // Return if the form generated would exceed the instruction threshold.
935 if (InstrRequired > LanaiLowerConstantMulThreshold)
936 return SDValue();
937
938 SDValue Res;
939 SDLoc DL(Op);
940 SDValue V = Op->getOperand(0);
941
942 // Initialize the running sum. Set the running sum to the maximal shifted
943 // positive value (i.e., largest i such that zi == 1 and MulAmt has V<<i as a
944 // term NAF).
945 if (HighestOne == -1)
946 Res = DAG.getConstant(0, DL, MVT::i32);
947 else {
948 Res = DAG.getNode(ISD::SHL, DL, VT, V,
949 DAG.getConstant(HighestOne, DL, MVT::i32));
950 SignedDigit[HighestOne] = 0;
951 }
952
953 // Assemble multiplication from shift, add, sub using NAF form and running
954 // sum.
955 for (unsigned int I = 0; I < sizeof(SignedDigit) / sizeof(SignedDigit[0]);
956 ++I) {
957 if (SignedDigit[I] == 0)
958 continue;
959
960 // Shifted multiplicand (v<<i).
961 SDValue Op =
962 DAG.getNode(ISD::SHL, DL, VT, V, DAG.getConstant(I, DL, MVT::i32));
963 if (SignedDigit[I] == 1)
964 Res = DAG.getNode(ISD::ADD, DL, VT, Res, Op);
965 else if (SignedDigit[I] == -1)
966 Res = DAG.getNode(ISD::SUB, DL, VT, Res, Op);
967 }
968 return Res;
969}
970
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000971SDValue LanaiTargetLowering::LowerSETCCE(SDValue Op, SelectionDAG &DAG) const {
972 SDValue LHS = Op.getOperand(0);
973 SDValue RHS = Op.getOperand(1);
974 SDValue Carry = Op.getOperand(2);
975 SDValue Cond = Op.getOperand(3);
976 SDLoc DL(Op);
977
Jacques Pienaare2f06992016-07-15 22:38:32 +0000978 LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, RHS, DAG);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000979 SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
980 SDValue Flag = DAG.getNode(LanaiISD::SUBBF, DL, MVT::Glue, LHS, RHS, Carry);
981 return DAG.getNode(LanaiISD::SETCC, DL, Op.getValueType(), TargetCC, Flag);
982}
983
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000984SDValue LanaiTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
985 SDValue LHS = Op.getOperand(0);
986 SDValue RHS = Op.getOperand(1);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000987 SDValue Cond = Op.getOperand(2);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000988 SDLoc DL(Op);
989
Jacques Pienaare2f06992016-07-15 22:38:32 +0000990 LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, RHS, DAG);
Jacques Pienaar50d4e982016-04-19 19:15:25 +0000991 SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +0000992 SDValue Flag =
993 DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);
994
995 return DAG.getNode(LanaiISD::SETCC, DL, Op.getValueType(), TargetCC, Flag);
996}
997
998SDValue LanaiTargetLowering::LowerSELECT_CC(SDValue Op,
999 SelectionDAG &DAG) const {
1000 SDValue LHS = Op.getOperand(0);
1001 SDValue RHS = Op.getOperand(1);
1002 SDValue TrueV = Op.getOperand(2);
1003 SDValue FalseV = Op.getOperand(3);
Jacques Pienaar50d4e982016-04-19 19:15:25 +00001004 SDValue Cond = Op.getOperand(4);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001005 SDLoc DL(Op);
1006
Jacques Pienaare2f06992016-07-15 22:38:32 +00001007 LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, RHS, DAG);
Jacques Pienaar50d4e982016-04-19 19:15:25 +00001008 SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001009 SDValue Flag =
1010 DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);
1011
1012 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
1013 return DAG.getNode(LanaiISD::SELECT_CC, DL, VTs, TrueV, FalseV, TargetCC,
1014 Flag);
1015}
1016
1017SDValue LanaiTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1018 MachineFunction &MF = DAG.getMachineFunction();
1019 LanaiMachineFunctionInfo *FuncInfo = MF.getInfo<LanaiMachineFunctionInfo>();
1020
1021 SDLoc DL(Op);
1022 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1023 getPointerTy(DAG.getDataLayout()));
1024
1025 // vastart just stores the address of the VarArgsFrameIndex slot into the
1026 // memory location argument.
1027 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1028 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Jacques Pienaare6503192016-07-15 22:18:33 +00001029 MachinePointerInfo(SV));
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001030}
1031
1032SDValue LanaiTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1033 SelectionDAG &DAG) const {
1034 SDValue Chain = Op.getOperand(0);
1035 SDValue Size = Op.getOperand(1);
1036 SDLoc DL(Op);
1037
1038 unsigned SPReg = getStackPointerRegisterToSaveRestore();
1039
1040 // Get a reference to the stack pointer.
1041 SDValue StackPointer = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i32);
1042
1043 // Subtract the dynamic size from the actual stack size to
1044 // obtain the new stack size.
1045 SDValue Sub = DAG.getNode(ISD::SUB, DL, MVT::i32, StackPointer, Size);
1046
1047 // For Lanai, the outgoing memory arguments area should be on top of the
1048 // alloca area on the stack i.e., the outgoing memory arguments should be
1049 // at a lower address than the alloca area. Move the alloca area down the
1050 // stack by adding back the space reserved for outgoing arguments to SP
1051 // here.
1052 //
1053 // We do not know what the size of the outgoing args is at this point.
1054 // So, we add a pseudo instruction ADJDYNALLOC that will adjust the
1055 // stack pointer. We replace this instruction with on that has the correct,
1056 // known offset in emitPrologue().
1057 SDValue ArgAdjust = DAG.getNode(LanaiISD::ADJDYNALLOC, DL, MVT::i32, Sub);
1058
1059 // The Sub result contains the new stack start address, so it
1060 // must be placed in the stack pointer register.
1061 SDValue CopyChain = DAG.getCopyToReg(Chain, DL, SPReg, Sub);
1062
1063 SDValue Ops[2] = {ArgAdjust, CopyChain};
1064 return DAG.getMergeValues(Ops, DL);
1065}
1066
1067SDValue LanaiTargetLowering::LowerRETURNADDR(SDValue Op,
1068 SelectionDAG &DAG) const {
1069 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001070 MachineFrameInfo &MFI = MF.getFrameInfo();
1071 MFI.setReturnAddressIsTaken(true);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001072
1073 EVT VT = Op.getValueType();
1074 SDLoc DL(Op);
1075 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1076 if (Depth) {
1077 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
1078 const unsigned Offset = -4;
1079 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
1080 DAG.getIntPtrConstant(Offset, DL));
Jacques Pienaare6503192016-07-15 22:18:33 +00001081 return DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001082 }
1083
1084 // Return the link register, which contains the return address.
1085 // Mark it an implicit live-in.
1086 unsigned Reg = MF.addLiveIn(TRI->getRARegister(), getRegClassFor(MVT::i32));
1087 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, VT);
1088}
1089
1090SDValue LanaiTargetLowering::LowerFRAMEADDR(SDValue Op,
1091 SelectionDAG &DAG) const {
Matthias Braun941a7052016-07-28 18:40:00 +00001092 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
1093 MFI.setFrameAddressIsTaken(true);
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001094
1095 EVT VT = Op.getValueType();
1096 SDLoc DL(Op);
1097 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, Lanai::FP, VT);
1098 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1099 while (Depth--) {
1100 const unsigned Offset = -8;
1101 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
1102 DAG.getIntPtrConstant(Offset, DL));
Jacques Pienaare6503192016-07-15 22:18:33 +00001103 FrameAddr =
1104 DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001105 }
1106 return FrameAddr;
1107}
1108
1109const char *LanaiTargetLowering::getTargetNodeName(unsigned Opcode) const {
1110 switch (Opcode) {
1111 case LanaiISD::ADJDYNALLOC:
1112 return "LanaiISD::ADJDYNALLOC";
1113 case LanaiISD::RET_FLAG:
1114 return "LanaiISD::RET_FLAG";
1115 case LanaiISD::CALL:
1116 return "LanaiISD::CALL";
1117 case LanaiISD::SELECT_CC:
1118 return "LanaiISD::SELECT_CC";
1119 case LanaiISD::SETCC:
1120 return "LanaiISD::SETCC";
Jacques Pienaar50d4e982016-04-19 19:15:25 +00001121 case LanaiISD::SUBBF:
1122 return "LanaiISD::SUBBF";
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001123 case LanaiISD::SET_FLAG:
1124 return "LanaiISD::SET_FLAG";
1125 case LanaiISD::BR_CC:
1126 return "LanaiISD::BR_CC";
1127 case LanaiISD::Wrapper:
1128 return "LanaiISD::Wrapper";
1129 case LanaiISD::HI:
1130 return "LanaiISD::HI";
1131 case LanaiISD::LO:
1132 return "LanaiISD::LO";
1133 case LanaiISD::SMALL:
1134 return "LanaiISD::SMALL";
1135 default:
Eugene Zelenko049b0172017-01-06 00:30:53 +00001136 return nullptr;
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001137 }
1138}
1139
1140SDValue LanaiTargetLowering::LowerConstantPool(SDValue Op,
1141 SelectionDAG &DAG) const {
1142 SDLoc DL(Op);
1143 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1144 const Constant *C = N->getConstVal();
1145 const LanaiTargetObjectFile *TLOF =
1146 static_cast<const LanaiTargetObjectFile *>(
1147 getTargetMachine().getObjFileLowering());
1148
1149 // If the code model is small or constant will be placed in the small section,
1150 // then assume address will fit in 21-bits.
1151 if (getTargetMachine().getCodeModel() == CodeModel::Small ||
1152 TLOF->isConstantInSmallSection(DAG.getDataLayout(), C)) {
1153 SDValue Small = DAG.getTargetConstantPool(
1154 C, MVT::i32, N->getAlignment(), N->getOffset(), LanaiII::MO_NO_FLAG);
1155 return DAG.getNode(ISD::OR, DL, MVT::i32,
1156 DAG.getRegister(Lanai::R0, MVT::i32),
1157 DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));
1158 } else {
1159 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1160 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1161
1162 SDValue Hi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1163 N->getOffset(), OpFlagHi);
1164 SDValue Lo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1165 N->getOffset(), OpFlagLo);
1166 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1167 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1168 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1169 return Result;
1170 }
1171}
1172
1173SDValue LanaiTargetLowering::LowerGlobalAddress(SDValue Op,
1174 SelectionDAG &DAG) const {
1175 SDLoc DL(Op);
1176 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1177 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
1178
1179 const LanaiTargetObjectFile *TLOF =
1180 static_cast<const LanaiTargetObjectFile *>(
1181 getTargetMachine().getObjFileLowering());
1182
1183 // If the code model is small or global variable will be placed in the small
1184 // section, then assume address will fit in 21-bits.
Peter Collingbourne67335642016-10-24 19:23:39 +00001185 const GlobalObject *GO = GV->getBaseObject();
Jacques Pienaarccffe382016-12-15 16:56:16 +00001186 if (TLOF->isGlobalInSmallSection(GO, getTargetMachine())) {
Jacques Pienaarfcef3e42016-03-28 13:09:54 +00001187 SDValue Small = DAG.getTargetGlobalAddress(
1188 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, LanaiII::MO_NO_FLAG);
1189 return DAG.getNode(ISD::OR, DL, MVT::i32,
1190 DAG.getRegister(Lanai::R0, MVT::i32),
1191 DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));
1192 } else {
1193 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1194 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1195
1196 // Create the TargetGlobalAddress node, folding in the constant offset.
1197 SDValue Hi = DAG.getTargetGlobalAddress(
1198 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, OpFlagHi);
1199 SDValue Lo = DAG.getTargetGlobalAddress(
1200 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, OpFlagLo);
1201 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1202 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1203 return DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1204 }
1205}
1206
1207SDValue LanaiTargetLowering::LowerBlockAddress(SDValue Op,
1208 SelectionDAG &DAG) const {
1209 SDLoc DL(Op);
1210 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1211
1212 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1213 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1214
1215 SDValue Hi = DAG.getBlockAddress(BA, MVT::i32, true, OpFlagHi);
1216 SDValue Lo = DAG.getBlockAddress(BA, MVT::i32, true, OpFlagLo);
1217 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1218 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1219 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1220 return Result;
1221}
1222
1223SDValue LanaiTargetLowering::LowerJumpTable(SDValue Op,
1224 SelectionDAG &DAG) const {
1225 SDLoc DL(Op);
1226 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
1227
1228 // If the code model is small assume address will fit in 21-bits.
1229 if (getTargetMachine().getCodeModel() == CodeModel::Small) {
1230 SDValue Small = DAG.getTargetJumpTable(
1231 JT->getIndex(), getPointerTy(DAG.getDataLayout()), LanaiII::MO_NO_FLAG);
1232 return DAG.getNode(ISD::OR, DL, MVT::i32,
1233 DAG.getRegister(Lanai::R0, MVT::i32),
1234 DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));
1235 } else {
1236 uint8_t OpFlagHi = LanaiII::MO_ABS_HI;
1237 uint8_t OpFlagLo = LanaiII::MO_ABS_LO;
1238
1239 SDValue Hi = DAG.getTargetJumpTable(
1240 JT->getIndex(), getPointerTy(DAG.getDataLayout()), OpFlagHi);
1241 SDValue Lo = DAG.getTargetJumpTable(
1242 JT->getIndex(), getPointerTy(DAG.getDataLayout()), OpFlagLo);
1243 Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);
1244 Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);
1245 SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);
1246 return Result;
1247 }
1248}
Jacques Pienaarad1db352016-04-14 17:59:22 +00001249
Jacques Pienaar3bec3ef2016-12-02 22:01:28 +00001250SDValue LanaiTargetLowering::LowerSHL_PARTS(SDValue Op,
1251 SelectionDAG &DAG) const {
1252 EVT VT = Op.getValueType();
1253 unsigned VTBits = VT.getSizeInBits();
1254 SDLoc dl(Op);
1255 assert(Op.getNumOperands() == 3 && "Unexpected SHL!");
1256 SDValue ShOpLo = Op.getOperand(0);
1257 SDValue ShOpHi = Op.getOperand(1);
1258 SDValue ShAmt = Op.getOperand(2);
1259
1260 // Performs the following for (ShOpLo + (ShOpHi << 32)) << ShAmt:
1261 // LoBitsForHi = (ShAmt == 0) ? 0 : (ShOpLo >> (32-ShAmt))
1262 // HiBitsForHi = ShOpHi << ShAmt
1263 // Hi = (ShAmt >= 32) ? (ShOpLo << (ShAmt-32)) : (LoBitsForHi | HiBitsForHi)
1264 // Lo = (ShAmt >= 32) ? 0 : (ShOpLo << ShAmt)
1265 // return (Hi << 32) | Lo;
1266
1267 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
1268 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
1269 SDValue LoBitsForHi = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
1270
1271 // If ShAmt == 0, we just calculated "(SRL ShOpLo, 32)" which is "undef". We
1272 // wanted 0, so CSEL it directly.
1273 SDValue Zero = DAG.getConstant(0, dl, MVT::i32);
1274 SDValue SetCC = DAG.getSetCC(dl, MVT::i32, ShAmt, Zero, ISD::SETEQ);
1275 LoBitsForHi = DAG.getSelect(dl, MVT::i32, SetCC, Zero, LoBitsForHi);
1276
1277 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
1278 DAG.getConstant(VTBits, dl, MVT::i32));
1279 SDValue HiBitsForHi = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
1280 SDValue HiForNormalShift =
1281 DAG.getNode(ISD::OR, dl, VT, LoBitsForHi, HiBitsForHi);
1282
1283 SDValue HiForBigShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
1284
1285 SetCC = DAG.getSetCC(dl, MVT::i32, ExtraShAmt, Zero, ISD::SETGE);
1286 SDValue Hi =
1287 DAG.getSelect(dl, MVT::i32, SetCC, HiForBigShift, HiForNormalShift);
1288
1289 // Lanai shifts of larger than register sizes are wrapped rather than
1290 // clamped, so we can't just emit "lo << b" if b is too big.
1291 SDValue LoForNormalShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
1292 SDValue Lo = DAG.getSelect(
1293 dl, MVT::i32, SetCC, DAG.getConstant(0, dl, MVT::i32), LoForNormalShift);
1294
1295 SDValue Ops[2] = {Lo, Hi};
1296 return DAG.getMergeValues(Ops, dl);
1297}
1298
Jacques Pienaarad1db352016-04-14 17:59:22 +00001299SDValue LanaiTargetLowering::LowerSRL_PARTS(SDValue Op,
1300 SelectionDAG &DAG) const {
1301 MVT VT = Op.getSimpleValueType();
1302 unsigned VTBits = VT.getSizeInBits();
1303 SDLoc dl(Op);
1304 SDValue ShOpLo = Op.getOperand(0);
1305 SDValue ShOpHi = Op.getOperand(1);
1306 SDValue ShAmt = Op.getOperand(2);
1307
1308 // Performs the following for a >> b:
1309 // unsigned r_high = a_high >> b;
1310 // r_high = (32 - b <= 0) ? 0 : r_high;
1311 //
1312 // unsigned r_low = a_low >> b;
1313 // r_low = (32 - b <= 0) ? r_high : r_low;
1314 // r_low = (b == 0) ? r_low : r_low | (a_high << (32 - b));
1315 // return (unsigned long long)r_high << 32 | r_low;
1316 // Note: This takes advantage of Lanai's shift behavior to avoid needing to
1317 // mask the shift amount.
1318
1319 SDValue Zero = DAG.getConstant(0, dl, MVT::i32);
1320 SDValue NegatedPlus32 = DAG.getNode(
1321 ISD::SUB, dl, MVT::i32, DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
1322 SDValue SetCC = DAG.getSetCC(dl, MVT::i32, NegatedPlus32, Zero, ISD::SETLE);
1323
1324 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i32, ShOpHi, ShAmt);
1325 Hi = DAG.getSelect(dl, MVT::i32, SetCC, Zero, Hi);
1326
1327 SDValue Lo = DAG.getNode(ISD::SRL, dl, MVT::i32, ShOpLo, ShAmt);
1328 Lo = DAG.getSelect(dl, MVT::i32, SetCC, Hi, Lo);
1329 SDValue CarryBits =
1330 DAG.getNode(ISD::SHL, dl, MVT::i32, ShOpHi, NegatedPlus32);
1331 SDValue ShiftIsZero = DAG.getSetCC(dl, MVT::i32, ShAmt, Zero, ISD::SETEQ);
1332 Lo = DAG.getSelect(dl, MVT::i32, ShiftIsZero, Lo,
1333 DAG.getNode(ISD::OR, dl, MVT::i32, Lo, CarryBits));
1334
1335 SDValue Ops[2] = {Lo, Hi};
1336 return DAG.getMergeValues(Ops, dl);
1337}
Jacques Pienaar6d3eecc2016-07-07 23:36:04 +00001338
1339// Helper function that checks if N is a null or all ones constant.
1340static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
1341 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
1342}
1343
1344// Return true if N is conditionally 0 or all ones.
1345// Detects these expressions where cc is an i1 value:
1346//
1347// (select cc 0, y) [AllOnes=0]
1348// (select cc y, 0) [AllOnes=0]
1349// (zext cc) [AllOnes=0]
1350// (sext cc) [AllOnes=0/1]
1351// (select cc -1, y) [AllOnes=1]
1352// (select cc y, -1) [AllOnes=1]
1353//
1354// * AllOnes determines whether to check for an all zero (AllOnes false) or an
1355// all ones operand (AllOnes true).
1356// * Invert is set when N is the all zero/ones constant when CC is false.
1357// * OtherOp is set to the alternative value of N.
1358//
1359// For example, for (select cc X, Y) and AllOnes = 0 if:
1360// * X = 0, Invert = False and OtherOp = Y
1361// * Y = 0, Invert = True and OtherOp = X
1362static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes, SDValue &CC,
1363 bool &Invert, SDValue &OtherOp,
1364 SelectionDAG &DAG) {
1365 switch (N->getOpcode()) {
1366 default:
1367 return false;
1368 case ISD::SELECT: {
1369 CC = N->getOperand(0);
1370 SDValue N1 = N->getOperand(1);
1371 SDValue N2 = N->getOperand(2);
1372 if (isZeroOrAllOnes(N1, AllOnes)) {
1373 Invert = false;
1374 OtherOp = N2;
1375 return true;
1376 }
1377 if (isZeroOrAllOnes(N2, AllOnes)) {
1378 Invert = true;
1379 OtherOp = N1;
1380 return true;
1381 }
1382 return false;
1383 }
1384 case ISD::ZERO_EXTEND: {
1385 // (zext cc) can never be the all ones value.
1386 if (AllOnes)
1387 return false;
1388 CC = N->getOperand(0);
1389 if (CC.getValueType() != MVT::i1)
1390 return false;
1391 SDLoc dl(N);
1392 EVT VT = N->getValueType(0);
1393 OtherOp = DAG.getConstant(1, dl, VT);
1394 Invert = true;
1395 return true;
1396 }
1397 case ISD::SIGN_EXTEND: {
1398 CC = N->getOperand(0);
1399 if (CC.getValueType() != MVT::i1)
1400 return false;
1401 SDLoc dl(N);
1402 EVT VT = N->getValueType(0);
1403 Invert = !AllOnes;
1404 if (AllOnes)
1405 // When looking for an AllOnes constant, N is an sext, and the 'other'
1406 // value is 0.
1407 OtherOp = DAG.getConstant(0, dl, VT);
1408 else
1409 OtherOp =
1410 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl, VT);
1411 return true;
1412 }
1413 }
1414}
1415
1416// Combine a constant select operand into its use:
1417//
1418// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1419// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1420// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
1421// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
1422// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
1423//
1424// The transform is rejected if the select doesn't have a constant operand that
1425// is null, or all ones when AllOnes is set.
1426//
1427// Also recognize sext/zext from i1:
1428//
1429// (add (zext cc), x) -> (select cc (add x, 1), x)
1430// (add (sext cc), x) -> (select cc (add x, -1), x)
1431//
1432// These transformations eventually create predicated instructions.
1433static SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
1434 TargetLowering::DAGCombinerInfo &DCI,
1435 bool AllOnes) {
1436 SelectionDAG &DAG = DCI.DAG;
1437 EVT VT = N->getValueType(0);
1438 SDValue NonConstantVal;
1439 SDValue CCOp;
1440 bool SwapSelectOps;
1441 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
1442 NonConstantVal, DAG))
1443 return SDValue();
1444
1445 // Slct is now know to be the desired identity constant when CC is true.
1446 SDValue TrueVal = OtherOp;
1447 SDValue FalseVal =
1448 DAG.getNode(N->getOpcode(), SDLoc(N), VT, OtherOp, NonConstantVal);
1449 // Unless SwapSelectOps says CC should be false.
1450 if (SwapSelectOps)
1451 std::swap(TrueVal, FalseVal);
1452
1453 return DAG.getNode(ISD::SELECT, SDLoc(N), VT, CCOp, TrueVal, FalseVal);
1454}
1455
1456// Attempt combineSelectAndUse on each operand of a commutative operator N.
1457static SDValue
1458combineSelectAndUseCommutative(SDNode *N, TargetLowering::DAGCombinerInfo &DCI,
1459 bool AllOnes) {
1460 SDValue N0 = N->getOperand(0);
1461 SDValue N1 = N->getOperand(1);
1462 if (N0.getNode()->hasOneUse())
1463 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes))
1464 return Result;
1465 if (N1.getNode()->hasOneUse())
1466 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes))
1467 return Result;
1468 return SDValue();
1469}
1470
1471// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
1472static SDValue PerformSUBCombine(SDNode *N,
1473 TargetLowering::DAGCombinerInfo &DCI) {
1474 SDValue N0 = N->getOperand(0);
1475 SDValue N1 = N->getOperand(1);
1476
1477 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1478 if (N1.getNode()->hasOneUse())
1479 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, /*AllOnes=*/false))
1480 return Result;
1481
1482 return SDValue();
1483}
1484
1485SDValue LanaiTargetLowering::PerformDAGCombine(SDNode *N,
1486 DAGCombinerInfo &DCI) const {
1487 switch (N->getOpcode()) {
1488 default:
1489 break;
1490 case ISD::ADD:
1491 case ISD::OR:
1492 case ISD::XOR:
1493 return combineSelectAndUseCommutative(N, DCI, /*AllOnes=*/false);
1494 case ISD::AND:
1495 return combineSelectAndUseCommutative(N, DCI, /*AllOnes=*/true);
1496 case ISD::SUB:
1497 return PerformSUBCombine(N, DCI);
1498 }
1499
1500 return SDValue();
1501}