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