Path: blob/main/contrib/llvm-project/llvm/lib/Target/Lanai/LanaiISelLowering.cpp
35271 views
//===-- LanaiISelLowering.cpp - Lanai DAG Lowering Implementation ---------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the LanaiTargetLowering class.9//10//===----------------------------------------------------------------------===//1112#include "LanaiISelLowering.h"13#include "Lanai.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/RuntimeLibcallUtil.h"30#include "llvm/CodeGen/SelectionDAG.h"31#include "llvm/CodeGen/SelectionDAGNodes.h"32#include "llvm/CodeGen/TargetCallingConv.h"33#include "llvm/CodeGen/ValueTypes.h"34#include "llvm/CodeGenTypes/MachineValueType.h"35#include "llvm/IR/CallingConv.h"36#include "llvm/IR/DerivedTypes.h"37#include "llvm/IR/Function.h"38#include "llvm/IR/GlobalValue.h"39#include "llvm/Support/Casting.h"40#include "llvm/Support/CodeGen.h"41#include "llvm/Support/CommandLine.h"42#include "llvm/Support/Debug.h"43#include "llvm/Support/ErrorHandling.h"44#include "llvm/Support/KnownBits.h"45#include "llvm/Support/MathExtras.h"46#include "llvm/Support/raw_ostream.h"47#include "llvm/Target/TargetMachine.h"48#include <cassert>49#include <cmath>50#include <cstdint>51#include <cstdlib>52#include <utility>5354#define DEBUG_TYPE "lanai-lower"5556using namespace llvm;5758// Limit on number of instructions the lowered multiplication may have before a59// call to the library function should be generated instead. The threshold is60// currently set to 14 as this was the smallest threshold that resulted in all61// constant multiplications being lowered. A threshold of 5 covered all cases62// except for one multiplication which required 14. mulsi3 requires 1663// instructions (including the prologue and epilogue but excluding instructions64// at call site). Until we can inline mulsi3, generating at most 14 instructions65// will be faster than invoking mulsi3.66static cl::opt<int> LanaiLowerConstantMulThreshold(67"lanai-constant-mul-threshold", cl::Hidden,68cl::desc("Maximum number of instruction to generate when lowering constant "69"multiplication instead of calling library function [default=14]"),70cl::init(14));7172LanaiTargetLowering::LanaiTargetLowering(const TargetMachine &TM,73const LanaiSubtarget &STI)74: TargetLowering(TM) {75// Set up the register classes.76addRegisterClass(MVT::i32, &Lanai::GPRRegClass);7778// Compute derived properties from the register classes79TRI = STI.getRegisterInfo();80computeRegisterProperties(TRI);8182setStackPointerRegisterToSaveRestore(Lanai::SP);8384setOperationAction(ISD::BR_CC, MVT::i32, Custom);85setOperationAction(ISD::BR_JT, MVT::Other, Expand);86setOperationAction(ISD::BRCOND, MVT::Other, Expand);87setOperationAction(ISD::SETCC, MVT::i32, Custom);88setOperationAction(ISD::SELECT, MVT::i32, Expand);89setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);9091setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);92setOperationAction(ISD::BlockAddress, MVT::i32, Custom);93setOperationAction(ISD::JumpTable, MVT::i32, Custom);94setOperationAction(ISD::ConstantPool, MVT::i32, Custom);9596setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);97setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);98setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);99100setOperationAction(ISD::VASTART, MVT::Other, Custom);101setOperationAction(ISD::VAARG, MVT::Other, Expand);102setOperationAction(ISD::VACOPY, MVT::Other, Expand);103setOperationAction(ISD::VAEND, MVT::Other, Expand);104105setOperationAction(ISD::SDIV, MVT::i32, Expand);106setOperationAction(ISD::UDIV, MVT::i32, Expand);107setOperationAction(ISD::SDIVREM, MVT::i32, Expand);108setOperationAction(ISD::UDIVREM, MVT::i32, Expand);109setOperationAction(ISD::SREM, MVT::i32, Expand);110setOperationAction(ISD::UREM, MVT::i32, Expand);111112setOperationAction(ISD::MUL, MVT::i32, Custom);113setOperationAction(ISD::MULHU, MVT::i32, Expand);114setOperationAction(ISD::MULHS, MVT::i32, Expand);115setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);116setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);117118setOperationAction(ISD::ROTR, MVT::i32, Expand);119setOperationAction(ISD::ROTL, MVT::i32, Expand);120setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);121setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);122setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);123124setOperationAction(ISD::BSWAP, MVT::i32, Expand);125setOperationAction(ISD::CTPOP, MVT::i32, Legal);126setOperationAction(ISD::CTLZ, MVT::i32, Legal);127setOperationAction(ISD::CTTZ, MVT::i32, Legal);128129setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);130setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);131setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);132133// Extended load operations for i1 types must be promoted134for (MVT VT : MVT::integer_valuetypes()) {135setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);136setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);137setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);138}139140setTargetDAGCombine({ISD::ADD, ISD::SUB, ISD::AND, ISD::OR, ISD::XOR});141142// Function alignments143setMinFunctionAlignment(Align(4));144setPrefFunctionAlignment(Align(4));145146setJumpIsExpensive(true);147148// TODO: Setting the minimum jump table entries needed before a149// switch is transformed to a jump table to 100 to avoid creating jump tables150// as this was causing bad performance compared to a large group of if151// statements. Re-evaluate this on new benchmarks.152setMinimumJumpTableEntries(100);153154// Use fast calling convention for library functions.155for (int I = 0; I < RTLIB::UNKNOWN_LIBCALL; ++I) {156setLibcallCallingConv(static_cast<RTLIB::Libcall>(I), CallingConv::Fast);157}158159MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores160MaxStoresPerMemsetOptSize = 8;161MaxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores162MaxStoresPerMemcpyOptSize = 8;163MaxStoresPerMemmove = 16; // For @llvm.memmove -> sequence of stores164MaxStoresPerMemmoveOptSize = 8;165166// Booleans always contain 0 or 1.167setBooleanContents(ZeroOrOneBooleanContent);168169setMaxAtomicSizeInBitsSupported(0);170}171172SDValue LanaiTargetLowering::LowerOperation(SDValue Op,173SelectionDAG &DAG) const {174switch (Op.getOpcode()) {175case ISD::MUL:176return LowerMUL(Op, DAG);177case ISD::BR_CC:178return LowerBR_CC(Op, DAG);179case ISD::ConstantPool:180return LowerConstantPool(Op, DAG);181case ISD::GlobalAddress:182return LowerGlobalAddress(Op, DAG);183case ISD::BlockAddress:184return LowerBlockAddress(Op, DAG);185case ISD::JumpTable:186return LowerJumpTable(Op, DAG);187case ISD::SELECT_CC:188return LowerSELECT_CC(Op, DAG);189case ISD::SETCC:190return LowerSETCC(Op, DAG);191case ISD::SHL_PARTS:192return LowerSHL_PARTS(Op, DAG);193case ISD::SRL_PARTS:194return LowerSRL_PARTS(Op, DAG);195case ISD::VASTART:196return LowerVASTART(Op, DAG);197case ISD::DYNAMIC_STACKALLOC:198return LowerDYNAMIC_STACKALLOC(Op, DAG);199case ISD::RETURNADDR:200return LowerRETURNADDR(Op, DAG);201case ISD::FRAMEADDR:202return LowerFRAMEADDR(Op, DAG);203default:204llvm_unreachable("unimplemented operand");205}206}207208//===----------------------------------------------------------------------===//209// Lanai Inline Assembly Support210//===----------------------------------------------------------------------===//211212Register LanaiTargetLowering::getRegisterByName(213const char *RegName, LLT /*VT*/,214const MachineFunction & /*MF*/) const {215// Only unallocatable registers should be matched here.216Register Reg = StringSwitch<unsigned>(RegName)217.Case("pc", Lanai::PC)218.Case("sp", Lanai::SP)219.Case("fp", Lanai::FP)220.Case("rr1", Lanai::RR1)221.Case("r10", Lanai::R10)222.Case("rr2", Lanai::RR2)223.Case("r11", Lanai::R11)224.Case("rca", Lanai::RCA)225.Default(0);226227if (Reg)228return Reg;229report_fatal_error("Invalid register name global variable");230}231232std::pair<unsigned, const TargetRegisterClass *>233LanaiTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,234StringRef Constraint,235MVT VT) const {236if (Constraint.size() == 1)237// GCC Constraint Letters238switch (Constraint[0]) {239case 'r': // GENERAL_REGS240return std::make_pair(0U, &Lanai::GPRRegClass);241default:242break;243}244245return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);246}247248// Examine constraint type and operand type and determine a weight value.249// This object must already have been set up with the operand type250// and the current alternative constraint selected.251TargetLowering::ConstraintWeight252LanaiTargetLowering::getSingleConstraintMatchWeight(253AsmOperandInfo &Info, const char *Constraint) const {254ConstraintWeight Weight = CW_Invalid;255Value *CallOperandVal = Info.CallOperandVal;256// If we don't have a value, we can't do a match,257// but allow it at the lowest weight.258if (CallOperandVal == nullptr)259return CW_Default;260// Look at the constraint type.261switch (*Constraint) {262case 'I': // signed 16 bit immediate263case 'J': // integer zero264case 'K': // unsigned 16 bit immediate265case 'L': // immediate in the range 0 to 31266case 'M': // signed 32 bit immediate where lower 16 bits are 0267case 'N': // signed 26 bit immediate268case 'O': // integer zero269if (isa<ConstantInt>(CallOperandVal))270Weight = CW_Constant;271break;272default:273Weight = TargetLowering::getSingleConstraintMatchWeight(Info, Constraint);274break;275}276return Weight;277}278279// LowerAsmOperandForConstraint - Lower the specified operand into the Ops280// vector. If it is invalid, don't add anything to Ops.281void LanaiTargetLowering::LowerAsmOperandForConstraint(282SDValue Op, StringRef Constraint, std::vector<SDValue> &Ops,283SelectionDAG &DAG) const {284SDValue Result;285286// Only support length 1 constraints for now.287if (Constraint.size() > 1)288return;289290char ConstraintLetter = Constraint[0];291switch (ConstraintLetter) {292case 'I': // Signed 16 bit constant293// If this fails, the parent routine will give an error294if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {295if (isInt<16>(C->getSExtValue())) {296Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(C),297Op.getValueType());298break;299}300}301return;302case 'J': // integer zero303case 'O':304if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {305if (C->getZExtValue() == 0) {306Result = DAG.getTargetConstant(0, SDLoc(C), Op.getValueType());307break;308}309}310return;311case 'K': // unsigned 16 bit immediate312if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {313if (isUInt<16>(C->getZExtValue())) {314Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(C),315Op.getValueType());316break;317}318}319return;320case 'L': // immediate in the range 0 to 31321if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {322if (C->getZExtValue() <= 31) {323Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(C),324Op.getValueType());325break;326}327}328return;329case 'M': // signed 32 bit immediate where lower 16 bits are 0330if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {331int64_t Val = C->getSExtValue();332if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)) {333Result = DAG.getTargetConstant(Val, SDLoc(C), Op.getValueType());334break;335}336}337return;338case 'N': // signed 26 bit immediate339if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {340int64_t Val = C->getSExtValue();341if ((Val >= -33554432) && (Val <= 33554431)) {342Result = DAG.getTargetConstant(Val, SDLoc(C), Op.getValueType());343break;344}345}346return;347default:348break; // This will fall through to the generic implementation349}350351if (Result.getNode()) {352Ops.push_back(Result);353return;354}355356TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);357}358359//===----------------------------------------------------------------------===//360// Calling Convention Implementation361//===----------------------------------------------------------------------===//362363#include "LanaiGenCallingConv.inc"364365static unsigned NumFixedArgs;366static bool CC_Lanai32_VarArg(unsigned ValNo, MVT ValVT, MVT LocVT,367CCValAssign::LocInfo LocInfo,368ISD::ArgFlagsTy ArgFlags, CCState &State) {369// Handle fixed arguments with default CC.370// Note: Both the default and fast CC handle VarArg the same and hence the371// calling convention of the function is not considered here.372if (ValNo < NumFixedArgs) {373return CC_Lanai32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);374}375376// Promote i8/i16 args to i32377if (LocVT == MVT::i8 || LocVT == MVT::i16) {378LocVT = MVT::i32;379if (ArgFlags.isSExt())380LocInfo = CCValAssign::SExt;381else if (ArgFlags.isZExt())382LocInfo = CCValAssign::ZExt;383else384LocInfo = CCValAssign::AExt;385}386387// VarArgs get passed on stack388unsigned Offset = State.AllocateStack(4, Align(4));389State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));390return false;391}392393SDValue LanaiTargetLowering::LowerFormalArguments(394SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,395const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,396SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {397switch (CallConv) {398case CallingConv::C:399case CallingConv::Fast:400return LowerCCCArguments(Chain, CallConv, IsVarArg, Ins, DL, DAG, InVals);401default:402report_fatal_error("Unsupported calling convention");403}404}405406SDValue LanaiTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,407SmallVectorImpl<SDValue> &InVals) const {408SelectionDAG &DAG = CLI.DAG;409SDLoc &DL = CLI.DL;410SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;411SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;412SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;413SDValue Chain = CLI.Chain;414SDValue Callee = CLI.Callee;415bool &IsTailCall = CLI.IsTailCall;416CallingConv::ID CallConv = CLI.CallConv;417bool IsVarArg = CLI.IsVarArg;418419// Lanai target does not yet support tail call optimization.420IsTailCall = false;421422switch (CallConv) {423case CallingConv::Fast:424case CallingConv::C:425return LowerCCCCallTo(Chain, Callee, CallConv, IsVarArg, IsTailCall, Outs,426OutVals, Ins, DL, DAG, InVals);427default:428report_fatal_error("Unsupported calling convention");429}430}431432// LowerCCCArguments - transform physical registers into virtual registers and433// generate load operations for arguments places on the stack.434SDValue LanaiTargetLowering::LowerCCCArguments(435SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,436const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,437SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {438MachineFunction &MF = DAG.getMachineFunction();439MachineFrameInfo &MFI = MF.getFrameInfo();440MachineRegisterInfo &RegInfo = MF.getRegInfo();441LanaiMachineFunctionInfo *LanaiMFI = MF.getInfo<LanaiMachineFunctionInfo>();442443// Assign locations to all of the incoming arguments.444SmallVector<CCValAssign, 16> ArgLocs;445CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,446*DAG.getContext());447if (CallConv == CallingConv::Fast) {448CCInfo.AnalyzeFormalArguments(Ins, CC_Lanai32_Fast);449} else {450CCInfo.AnalyzeFormalArguments(Ins, CC_Lanai32);451}452453for (const CCValAssign &VA : ArgLocs) {454if (VA.isRegLoc()) {455// Arguments passed in registers456EVT RegVT = VA.getLocVT();457switch (RegVT.getSimpleVT().SimpleTy) {458case MVT::i32: {459Register VReg = RegInfo.createVirtualRegister(&Lanai::GPRRegClass);460RegInfo.addLiveIn(VA.getLocReg(), VReg);461SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, RegVT);462463// If this is an 8/16-bit value, it is really passed promoted to 32464// bits. Insert an assert[sz]ext to capture this, then truncate to the465// right size.466if (VA.getLocInfo() == CCValAssign::SExt)467ArgValue = DAG.getNode(ISD::AssertSext, DL, RegVT, ArgValue,468DAG.getValueType(VA.getValVT()));469else if (VA.getLocInfo() == CCValAssign::ZExt)470ArgValue = DAG.getNode(ISD::AssertZext, DL, RegVT, ArgValue,471DAG.getValueType(VA.getValVT()));472473if (VA.getLocInfo() != CCValAssign::Full)474ArgValue = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), ArgValue);475476InVals.push_back(ArgValue);477break;478}479default:480LLVM_DEBUG(dbgs() << "LowerFormalArguments Unhandled argument type: "481<< RegVT << "\n");482llvm_unreachable("unhandled argument type");483}484} else {485// Only arguments passed on the stack should make it here.486assert(VA.isMemLoc());487// Load the argument to a virtual register488unsigned ObjSize = VA.getLocVT().getSizeInBits() / 8;489// Check that the argument fits in stack slot490if (ObjSize > 4) {491errs() << "LowerFormalArguments Unhandled argument type: "492<< VA.getLocVT() << "\n";493}494// Create the frame index object for this incoming parameter...495int FI = MFI.CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);496497// Create the SelectionDAG nodes corresponding to a load498// from this parameter499SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);500InVals.push_back(DAG.getLoad(501VA.getLocVT(), DL, Chain, FIN,502MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)));503}504}505506// The Lanai ABI for returning structs by value requires that we copy507// the sret argument into rv for the return. Save the argument into508// a virtual register so that we can access it from the return points.509if (MF.getFunction().hasStructRetAttr()) {510Register Reg = LanaiMFI->getSRetReturnReg();511if (!Reg) {512Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));513LanaiMFI->setSRetReturnReg(Reg);514}515SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);516Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);517}518519if (IsVarArg) {520// Record the frame index of the first variable argument521// which is a value necessary to VASTART.522int FI = MFI.CreateFixedObject(4, CCInfo.getStackSize(), true);523LanaiMFI->setVarArgsFrameIndex(FI);524}525526return Chain;527}528529bool LanaiTargetLowering::CanLowerReturn(530CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,531const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {532SmallVector<CCValAssign, 16> RVLocs;533CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);534535return CCInfo.CheckReturn(Outs, RetCC_Lanai32);536}537538SDValue539LanaiTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,540bool IsVarArg,541const SmallVectorImpl<ISD::OutputArg> &Outs,542const SmallVectorImpl<SDValue> &OutVals,543const SDLoc &DL, SelectionDAG &DAG) const {544// CCValAssign - represent the assignment of the return value to a location545SmallVector<CCValAssign, 16> RVLocs;546547// CCState - Info about the registers and stack slot.548CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,549*DAG.getContext());550551// Analize return values.552CCInfo.AnalyzeReturn(Outs, RetCC_Lanai32);553554SDValue Glue;555SmallVector<SDValue, 4> RetOps(1, Chain);556557// Copy the result values into the output registers.558for (unsigned i = 0; i != RVLocs.size(); ++i) {559CCValAssign &VA = RVLocs[i];560assert(VA.isRegLoc() && "Can only return in registers!");561562Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVals[i], Glue);563564// Guarantee that all emitted copies are stuck together with flags.565Glue = Chain.getValue(1);566RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));567}568569// The Lanai ABI for returning structs by value requires that we copy570// the sret argument into rv for the return. We saved the argument into571// a virtual register in the entry block, so now we copy the value out572// and into rv.573if (DAG.getMachineFunction().getFunction().hasStructRetAttr()) {574MachineFunction &MF = DAG.getMachineFunction();575LanaiMachineFunctionInfo *LanaiMFI = MF.getInfo<LanaiMachineFunctionInfo>();576Register Reg = LanaiMFI->getSRetReturnReg();577assert(Reg &&578"SRetReturnReg should have been set in LowerFormalArguments().");579SDValue Val =580DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));581582Chain = DAG.getCopyToReg(Chain, DL, Lanai::RV, Val, Glue);583Glue = Chain.getValue(1);584RetOps.push_back(585DAG.getRegister(Lanai::RV, getPointerTy(DAG.getDataLayout())));586}587588RetOps[0] = Chain; // Update chain589590unsigned Opc = LanaiISD::RET_GLUE;591if (Glue.getNode())592RetOps.push_back(Glue);593594// Return Void595return DAG.getNode(Opc, DL, MVT::Other,596ArrayRef<SDValue>(&RetOps[0], RetOps.size()));597}598599// LowerCCCCallTo - functions arguments are copied from virtual regs to600// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.601SDValue LanaiTargetLowering::LowerCCCCallTo(602SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool IsVarArg,603bool /*IsTailCall*/, const SmallVectorImpl<ISD::OutputArg> &Outs,604const SmallVectorImpl<SDValue> &OutVals,605const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,606SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {607// Analyze operands of the call, assigning locations to each operand.608SmallVector<CCValAssign, 16> ArgLocs;609CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,610*DAG.getContext());611GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);612MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();613614NumFixedArgs = 0;615if (IsVarArg && G) {616const Function *CalleeFn = dyn_cast<Function>(G->getGlobal());617if (CalleeFn)618NumFixedArgs = CalleeFn->getFunctionType()->getNumParams();619}620if (NumFixedArgs)621CCInfo.AnalyzeCallOperands(Outs, CC_Lanai32_VarArg);622else {623if (CallConv == CallingConv::Fast)624CCInfo.AnalyzeCallOperands(Outs, CC_Lanai32_Fast);625else626CCInfo.AnalyzeCallOperands(Outs, CC_Lanai32);627}628629// Get a count of how many bytes are to be pushed on the stack.630unsigned NumBytes = CCInfo.getStackSize();631632// Create local copies for byval args.633SmallVector<SDValue, 8> ByValArgs;634for (unsigned I = 0, E = Outs.size(); I != E; ++I) {635ISD::ArgFlagsTy Flags = Outs[I].Flags;636if (!Flags.isByVal())637continue;638639SDValue Arg = OutVals[I];640unsigned Size = Flags.getByValSize();641Align Alignment = Flags.getNonZeroByValAlign();642643int FI = MFI.CreateStackObject(Size, Alignment, false);644SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));645SDValue SizeNode = DAG.getConstant(Size, DL, MVT::i32);646647Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Alignment,648/*IsVolatile=*/false,649/*AlwaysInline=*/false,650/*CI=*/nullptr, std::nullopt, MachinePointerInfo(),651MachinePointerInfo());652ByValArgs.push_back(FIPtr);653}654655Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);656657SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;658SmallVector<SDValue, 12> MemOpChains;659SDValue StackPtr;660661// Walk the register/memloc assignments, inserting copies/loads.662for (unsigned I = 0, J = 0, E = ArgLocs.size(); I != E; ++I) {663CCValAssign &VA = ArgLocs[I];664SDValue Arg = OutVals[I];665ISD::ArgFlagsTy Flags = Outs[I].Flags;666667// Promote the value if needed.668switch (VA.getLocInfo()) {669case CCValAssign::Full:670break;671case CCValAssign::SExt:672Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);673break;674case CCValAssign::ZExt:675Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);676break;677case CCValAssign::AExt:678Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);679break;680default:681llvm_unreachable("Unknown loc info!");682}683684// Use local copy if it is a byval arg.685if (Flags.isByVal())686Arg = ByValArgs[J++];687688// Arguments that can be passed on register must be kept at RegsToPass689// vector690if (VA.isRegLoc()) {691RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));692} else {693assert(VA.isMemLoc());694695if (StackPtr.getNode() == nullptr)696StackPtr = DAG.getCopyFromReg(Chain, DL, Lanai::SP,697getPointerTy(DAG.getDataLayout()));698699SDValue PtrOff =700DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,701DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));702703MemOpChains.push_back(704DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo()));705}706}707708// Transform all store nodes into one single node because all store nodes are709// independent of each other.710if (!MemOpChains.empty())711Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,712ArrayRef<SDValue>(&MemOpChains[0], MemOpChains.size()));713714SDValue InGlue;715716// Build a sequence of copy-to-reg nodes chained together with token chain and717// flag operands which copy the outgoing args into registers. The InGlue in718// necessary since all emitted instructions must be stuck together.719for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {720Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,721RegsToPass[I].second, InGlue);722InGlue = Chain.getValue(1);723}724725// If the callee is a GlobalAddress node (quite common, every direct call is)726// turn it into a TargetGlobalAddress node so that legalize doesn't hack it.727// Likewise ExternalSymbol -> TargetExternalSymbol.728uint8_t OpFlag = LanaiII::MO_NO_FLAG;729if (G) {730Callee = DAG.getTargetGlobalAddress(731G->getGlobal(), DL, getPointerTy(DAG.getDataLayout()), 0, OpFlag);732} else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {733Callee = DAG.getTargetExternalSymbol(734E->getSymbol(), getPointerTy(DAG.getDataLayout()), OpFlag);735}736737// Returns a chain & a flag for retval copy to use.738SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);739SmallVector<SDValue, 8> Ops;740Ops.push_back(Chain);741Ops.push_back(Callee);742743// Add a register mask operand representing the call-preserved registers.744// TODO: Should return-twice functions be handled?745const uint32_t *Mask =746TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv);747assert(Mask && "Missing call preserved mask for calling convention");748Ops.push_back(DAG.getRegisterMask(Mask));749750// Add argument registers to the end of the list so that they are751// known live into the call.752for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)753Ops.push_back(DAG.getRegister(RegsToPass[I].first,754RegsToPass[I].second.getValueType()));755756if (InGlue.getNode())757Ops.push_back(InGlue);758759Chain = DAG.getNode(LanaiISD::CALL, DL, NodeTys,760ArrayRef<SDValue>(&Ops[0], Ops.size()));761InGlue = Chain.getValue(1);762763// Create the CALLSEQ_END node.764Chain = DAG.getCALLSEQ_END(Chain, NumBytes, 0, InGlue, DL);765InGlue = Chain.getValue(1);766767// Handle result values, copying them out of physregs into vregs that we768// return.769return LowerCallResult(Chain, InGlue, CallConv, IsVarArg, Ins, DL, DAG,770InVals);771}772773// LowerCallResult - Lower the result values of a call into the774// appropriate copies out of appropriate physical registers.775SDValue LanaiTargetLowering::LowerCallResult(776SDValue Chain, SDValue InGlue, CallingConv::ID CallConv, bool IsVarArg,777const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,778SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {779// Assign locations to each value returned by this call.780SmallVector<CCValAssign, 16> RVLocs;781CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,782*DAG.getContext());783784CCInfo.AnalyzeCallResult(Ins, RetCC_Lanai32);785786// Copy all of the result registers out of their specified physreg.787for (unsigned I = 0; I != RVLocs.size(); ++I) {788Chain = DAG.getCopyFromReg(Chain, DL, RVLocs[I].getLocReg(),789RVLocs[I].getValVT(), InGlue)790.getValue(1);791InGlue = Chain.getValue(2);792InVals.push_back(Chain.getValue(0));793}794795return Chain;796}797798//===----------------------------------------------------------------------===//799// Custom Lowerings800//===----------------------------------------------------------------------===//801802static LPCC::CondCode IntCondCCodeToICC(SDValue CC, const SDLoc &DL,803SDValue &RHS, SelectionDAG &DAG) {804ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();805806// For integer, only the SETEQ, SETNE, SETLT, SETLE, SETGT, SETGE, SETULT,807// SETULE, SETUGT, and SETUGE opcodes are used (see CodeGen/ISDOpcodes.h)808// and Lanai only supports integer comparisons, so only provide definitions809// for them.810switch (SetCCOpcode) {811case ISD::SETEQ:812return LPCC::ICC_EQ;813case ISD::SETGT:814if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))815if (RHSC->getZExtValue() == 0xFFFFFFFF) {816// X > -1 -> X >= 0 -> is_plus(X)817RHS = DAG.getConstant(0, DL, RHS.getValueType());818return LPCC::ICC_PL;819}820return LPCC::ICC_GT;821case ISD::SETUGT:822return LPCC::ICC_UGT;823case ISD::SETLT:824if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))825if (RHSC->getZExtValue() == 0)826// X < 0 -> is_minus(X)827return LPCC::ICC_MI;828return LPCC::ICC_LT;829case ISD::SETULT:830return LPCC::ICC_ULT;831case ISD::SETLE:832if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))833if (RHSC->getZExtValue() == 0xFFFFFFFF) {834// X <= -1 -> X < 0 -> is_minus(X)835RHS = DAG.getConstant(0, DL, RHS.getValueType());836return LPCC::ICC_MI;837}838return LPCC::ICC_LE;839case ISD::SETULE:840return LPCC::ICC_ULE;841case ISD::SETGE:842if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS))843if (RHSC->getZExtValue() == 0)844// X >= 0 -> is_plus(X)845return LPCC::ICC_PL;846return LPCC::ICC_GE;847case ISD::SETUGE:848return LPCC::ICC_UGE;849case ISD::SETNE:850return LPCC::ICC_NE;851case ISD::SETONE:852case ISD::SETUNE:853case ISD::SETOGE:854case ISD::SETOLE:855case ISD::SETOLT:856case ISD::SETOGT:857case ISD::SETOEQ:858case ISD::SETUEQ:859case ISD::SETO:860case ISD::SETUO:861llvm_unreachable("Unsupported comparison.");862default:863llvm_unreachable("Unknown integer condition code!");864}865}866867SDValue LanaiTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {868SDValue Chain = Op.getOperand(0);869SDValue Cond = Op.getOperand(1);870SDValue LHS = Op.getOperand(2);871SDValue RHS = Op.getOperand(3);872SDValue Dest = Op.getOperand(4);873SDLoc DL(Op);874875LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, RHS, DAG);876SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);877SDValue Glue =878DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);879880return DAG.getNode(LanaiISD::BR_CC, DL, Op.getValueType(), Chain, Dest,881TargetCC, Glue);882}883884SDValue LanaiTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {885EVT VT = Op->getValueType(0);886if (VT != MVT::i32)887return SDValue();888889ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op->getOperand(1));890if (!C)891return SDValue();892893int64_t MulAmt = C->getSExtValue();894int32_t HighestOne = -1;895uint32_t NonzeroEntries = 0;896int SignedDigit[32] = {0};897898// Convert to non-adjacent form (NAF) signed-digit representation.899// NAF is a signed-digit form where no adjacent digits are non-zero. It is the900// minimal Hamming weight representation of a number (on average 1/3 of the901// digits will be non-zero vs 1/2 for regular binary representation). And as902// the non-zero digits will be the only digits contributing to the instruction903// count, this is desirable. The next loop converts it to NAF (following the904// approach in 'Guide to Elliptic Curve Cryptography' [ISBN: 038795273X]) by905// choosing the non-zero coefficients such that the resulting quotient is906// divisible by 2 which will cause the next coefficient to be zero.907int64_t E = std::abs(MulAmt);908int S = (MulAmt < 0 ? -1 : 1);909int I = 0;910while (E > 0) {911int ZI = 0;912if (E % 2 == 1) {913ZI = 2 - (E % 4);914if (ZI != 0)915++NonzeroEntries;916}917SignedDigit[I] = S * ZI;918if (SignedDigit[I] == 1)919HighestOne = I;920E = (E - ZI) / 2;921++I;922}923924// Compute number of instructions required. Due to differences in lowering925// between the different processors this count is not exact.926// Start by assuming a shift and a add/sub for every non-zero entry (hence927// every non-zero entry requires 1 shift and 1 add/sub except for the first928// entry).929int32_t InstrRequired = 2 * NonzeroEntries - 1;930// Correct possible over-adding due to shift by 0 (which is not emitted).931if (std::abs(MulAmt) % 2 == 1)932--InstrRequired;933// Return if the form generated would exceed the instruction threshold.934if (InstrRequired > LanaiLowerConstantMulThreshold)935return SDValue();936937SDValue Res;938SDLoc DL(Op);939SDValue V = Op->getOperand(0);940941// Initialize the running sum. Set the running sum to the maximal shifted942// positive value (i.e., largest i such that zi == 1 and MulAmt has V<<i as a943// term NAF).944if (HighestOne == -1)945Res = DAG.getConstant(0, DL, MVT::i32);946else {947Res = DAG.getNode(ISD::SHL, DL, VT, V,948DAG.getConstant(HighestOne, DL, MVT::i32));949SignedDigit[HighestOne] = 0;950}951952// Assemble multiplication from shift, add, sub using NAF form and running953// sum.954for (unsigned int I = 0; I < std::size(SignedDigit); ++I) {955if (SignedDigit[I] == 0)956continue;957958// Shifted multiplicand (v<<i).959SDValue Op =960DAG.getNode(ISD::SHL, DL, VT, V, DAG.getConstant(I, DL, MVT::i32));961if (SignedDigit[I] == 1)962Res = DAG.getNode(ISD::ADD, DL, VT, Res, Op);963else if (SignedDigit[I] == -1)964Res = DAG.getNode(ISD::SUB, DL, VT, Res, Op);965}966return Res;967}968969SDValue LanaiTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {970SDValue LHS = Op.getOperand(0);971SDValue RHS = Op.getOperand(1);972SDValue Cond = Op.getOperand(2);973SDLoc DL(Op);974975LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, RHS, DAG);976SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);977SDValue Glue =978DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);979980return DAG.getNode(LanaiISD::SETCC, DL, Op.getValueType(), TargetCC, Glue);981}982983SDValue LanaiTargetLowering::LowerSELECT_CC(SDValue Op,984SelectionDAG &DAG) const {985SDValue LHS = Op.getOperand(0);986SDValue RHS = Op.getOperand(1);987SDValue TrueV = Op.getOperand(2);988SDValue FalseV = Op.getOperand(3);989SDValue Cond = Op.getOperand(4);990SDLoc DL(Op);991992LPCC::CondCode CC = IntCondCCodeToICC(Cond, DL, RHS, DAG);993SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i32);994SDValue Glue =995DAG.getNode(LanaiISD::SET_FLAG, DL, MVT::Glue, LHS, RHS, TargetCC);996997SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);998return DAG.getNode(LanaiISD::SELECT_CC, DL, VTs, TrueV, FalseV, TargetCC,999Glue);1000}10011002SDValue LanaiTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {1003MachineFunction &MF = DAG.getMachineFunction();1004LanaiMachineFunctionInfo *FuncInfo = MF.getInfo<LanaiMachineFunctionInfo>();10051006SDLoc DL(Op);1007SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),1008getPointerTy(DAG.getDataLayout()));10091010// vastart just stores the address of the VarArgsFrameIndex slot into the1011// memory location argument.1012const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();1013return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),1014MachinePointerInfo(SV));1015}10161017SDValue LanaiTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,1018SelectionDAG &DAG) const {1019SDValue Chain = Op.getOperand(0);1020SDValue Size = Op.getOperand(1);1021SDLoc DL(Op);10221023Register SPReg = getStackPointerRegisterToSaveRestore();10241025// Get a reference to the stack pointer.1026SDValue StackPointer = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i32);10271028// Subtract the dynamic size from the actual stack size to1029// obtain the new stack size.1030SDValue Sub = DAG.getNode(ISD::SUB, DL, MVT::i32, StackPointer, Size);10311032// For Lanai, the outgoing memory arguments area should be on top of the1033// alloca area on the stack i.e., the outgoing memory arguments should be1034// at a lower address than the alloca area. Move the alloca area down the1035// stack by adding back the space reserved for outgoing arguments to SP1036// here.1037//1038// We do not know what the size of the outgoing args is at this point.1039// So, we add a pseudo instruction ADJDYNALLOC that will adjust the1040// stack pointer. We replace this instruction with on that has the correct,1041// known offset in emitPrologue().1042SDValue ArgAdjust = DAG.getNode(LanaiISD::ADJDYNALLOC, DL, MVT::i32, Sub);10431044// The Sub result contains the new stack start address, so it1045// must be placed in the stack pointer register.1046SDValue CopyChain = DAG.getCopyToReg(Chain, DL, SPReg, Sub);10471048SDValue Ops[2] = {ArgAdjust, CopyChain};1049return DAG.getMergeValues(Ops, DL);1050}10511052SDValue LanaiTargetLowering::LowerRETURNADDR(SDValue Op,1053SelectionDAG &DAG) const {1054MachineFunction &MF = DAG.getMachineFunction();1055MachineFrameInfo &MFI = MF.getFrameInfo();1056MFI.setReturnAddressIsTaken(true);10571058EVT VT = Op.getValueType();1059SDLoc DL(Op);1060unsigned Depth = Op.getConstantOperandVal(0);1061if (Depth) {1062SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);1063const unsigned Offset = -4;1064SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,1065DAG.getIntPtrConstant(Offset, DL));1066return DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());1067}10681069// Return the link register, which contains the return address.1070// Mark it an implicit live-in.1071Register Reg = MF.addLiveIn(TRI->getRARegister(), getRegClassFor(MVT::i32));1072return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, VT);1073}10741075SDValue LanaiTargetLowering::LowerFRAMEADDR(SDValue Op,1076SelectionDAG &DAG) const {1077MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();1078MFI.setFrameAddressIsTaken(true);10791080EVT VT = Op.getValueType();1081SDLoc DL(Op);1082SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, Lanai::FP, VT);1083unsigned Depth = Op.getConstantOperandVal(0);1084while (Depth--) {1085const unsigned Offset = -8;1086SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,1087DAG.getIntPtrConstant(Offset, DL));1088FrameAddr =1089DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());1090}1091return FrameAddr;1092}10931094const char *LanaiTargetLowering::getTargetNodeName(unsigned Opcode) const {1095switch (Opcode) {1096case LanaiISD::ADJDYNALLOC:1097return "LanaiISD::ADJDYNALLOC";1098case LanaiISD::RET_GLUE:1099return "LanaiISD::RET_GLUE";1100case LanaiISD::CALL:1101return "LanaiISD::CALL";1102case LanaiISD::SELECT_CC:1103return "LanaiISD::SELECT_CC";1104case LanaiISD::SETCC:1105return "LanaiISD::SETCC";1106case LanaiISD::SUBBF:1107return "LanaiISD::SUBBF";1108case LanaiISD::SET_FLAG:1109return "LanaiISD::SET_FLAG";1110case LanaiISD::BR_CC:1111return "LanaiISD::BR_CC";1112case LanaiISD::Wrapper:1113return "LanaiISD::Wrapper";1114case LanaiISD::HI:1115return "LanaiISD::HI";1116case LanaiISD::LO:1117return "LanaiISD::LO";1118case LanaiISD::SMALL:1119return "LanaiISD::SMALL";1120default:1121return nullptr;1122}1123}11241125SDValue LanaiTargetLowering::LowerConstantPool(SDValue Op,1126SelectionDAG &DAG) const {1127SDLoc DL(Op);1128ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);1129const Constant *C = N->getConstVal();1130const LanaiTargetObjectFile *TLOF =1131static_cast<const LanaiTargetObjectFile *>(1132getTargetMachine().getObjFileLowering());11331134// If the code model is small or constant will be placed in the small section,1135// then assume address will fit in 21-bits.1136if (getTargetMachine().getCodeModel() == CodeModel::Small ||1137TLOF->isConstantInSmallSection(DAG.getDataLayout(), C)) {1138SDValue Small = DAG.getTargetConstantPool(1139C, MVT::i32, N->getAlign(), N->getOffset(), LanaiII::MO_NO_FLAG);1140return DAG.getNode(ISD::OR, DL, MVT::i32,1141DAG.getRegister(Lanai::R0, MVT::i32),1142DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));1143} else {1144uint8_t OpFlagHi = LanaiII::MO_ABS_HI;1145uint8_t OpFlagLo = LanaiII::MO_ABS_LO;11461147SDValue Hi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlign(),1148N->getOffset(), OpFlagHi);1149SDValue Lo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlign(),1150N->getOffset(), OpFlagLo);1151Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);1152Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);1153SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);1154return Result;1155}1156}11571158SDValue LanaiTargetLowering::LowerGlobalAddress(SDValue Op,1159SelectionDAG &DAG) const {1160SDLoc DL(Op);1161const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();1162int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();11631164const LanaiTargetObjectFile *TLOF =1165static_cast<const LanaiTargetObjectFile *>(1166getTargetMachine().getObjFileLowering());11671168// If the code model is small or global variable will be placed in the small1169// section, then assume address will fit in 21-bits.1170const GlobalObject *GO = GV->getAliaseeObject();1171if (TLOF->isGlobalInSmallSection(GO, getTargetMachine())) {1172SDValue Small = DAG.getTargetGlobalAddress(1173GV, DL, getPointerTy(DAG.getDataLayout()), Offset, LanaiII::MO_NO_FLAG);1174return DAG.getNode(ISD::OR, DL, MVT::i32,1175DAG.getRegister(Lanai::R0, MVT::i32),1176DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));1177} else {1178uint8_t OpFlagHi = LanaiII::MO_ABS_HI;1179uint8_t OpFlagLo = LanaiII::MO_ABS_LO;11801181// Create the TargetGlobalAddress node, folding in the constant offset.1182SDValue Hi = DAG.getTargetGlobalAddress(1183GV, DL, getPointerTy(DAG.getDataLayout()), Offset, OpFlagHi);1184SDValue Lo = DAG.getTargetGlobalAddress(1185GV, DL, getPointerTy(DAG.getDataLayout()), Offset, OpFlagLo);1186Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);1187Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);1188return DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);1189}1190}11911192SDValue LanaiTargetLowering::LowerBlockAddress(SDValue Op,1193SelectionDAG &DAG) const {1194SDLoc DL(Op);1195const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();11961197uint8_t OpFlagHi = LanaiII::MO_ABS_HI;1198uint8_t OpFlagLo = LanaiII::MO_ABS_LO;11991200SDValue Hi = DAG.getBlockAddress(BA, MVT::i32, true, OpFlagHi);1201SDValue Lo = DAG.getBlockAddress(BA, MVT::i32, true, OpFlagLo);1202Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);1203Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);1204SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);1205return Result;1206}12071208SDValue LanaiTargetLowering::LowerJumpTable(SDValue Op,1209SelectionDAG &DAG) const {1210SDLoc DL(Op);1211JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);12121213// If the code model is small assume address will fit in 21-bits.1214if (getTargetMachine().getCodeModel() == CodeModel::Small) {1215SDValue Small = DAG.getTargetJumpTable(1216JT->getIndex(), getPointerTy(DAG.getDataLayout()), LanaiII::MO_NO_FLAG);1217return DAG.getNode(ISD::OR, DL, MVT::i32,1218DAG.getRegister(Lanai::R0, MVT::i32),1219DAG.getNode(LanaiISD::SMALL, DL, MVT::i32, Small));1220} else {1221uint8_t OpFlagHi = LanaiII::MO_ABS_HI;1222uint8_t OpFlagLo = LanaiII::MO_ABS_LO;12231224SDValue Hi = DAG.getTargetJumpTable(1225JT->getIndex(), getPointerTy(DAG.getDataLayout()), OpFlagHi);1226SDValue Lo = DAG.getTargetJumpTable(1227JT->getIndex(), getPointerTy(DAG.getDataLayout()), OpFlagLo);1228Hi = DAG.getNode(LanaiISD::HI, DL, MVT::i32, Hi);1229Lo = DAG.getNode(LanaiISD::LO, DL, MVT::i32, Lo);1230SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Hi, Lo);1231return Result;1232}1233}12341235SDValue LanaiTargetLowering::LowerSHL_PARTS(SDValue Op,1236SelectionDAG &DAG) const {1237EVT VT = Op.getValueType();1238unsigned VTBits = VT.getSizeInBits();1239SDLoc dl(Op);1240assert(Op.getNumOperands() == 3 && "Unexpected SHL!");1241SDValue ShOpLo = Op.getOperand(0);1242SDValue ShOpHi = Op.getOperand(1);1243SDValue ShAmt = Op.getOperand(2);12441245// Performs the following for (ShOpLo + (ShOpHi << 32)) << ShAmt:1246// LoBitsForHi = (ShAmt == 0) ? 0 : (ShOpLo >> (32-ShAmt))1247// HiBitsForHi = ShOpHi << ShAmt1248// Hi = (ShAmt >= 32) ? (ShOpLo << (ShAmt-32)) : (LoBitsForHi | HiBitsForHi)1249// Lo = (ShAmt >= 32) ? 0 : (ShOpLo << ShAmt)1250// return (Hi << 32) | Lo;12511252SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,1253DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);1254SDValue LoBitsForHi = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);12551256// If ShAmt == 0, we just calculated "(SRL ShOpLo, 32)" which is "undef". We1257// wanted 0, so CSEL it directly.1258SDValue Zero = DAG.getConstant(0, dl, MVT::i32);1259SDValue SetCC = DAG.getSetCC(dl, MVT::i32, ShAmt, Zero, ISD::SETEQ);1260LoBitsForHi = DAG.getSelect(dl, MVT::i32, SetCC, Zero, LoBitsForHi);12611262SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,1263DAG.getConstant(VTBits, dl, MVT::i32));1264SDValue HiBitsForHi = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);1265SDValue HiForNormalShift =1266DAG.getNode(ISD::OR, dl, VT, LoBitsForHi, HiBitsForHi);12671268SDValue HiForBigShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);12691270SetCC = DAG.getSetCC(dl, MVT::i32, ExtraShAmt, Zero, ISD::SETGE);1271SDValue Hi =1272DAG.getSelect(dl, MVT::i32, SetCC, HiForBigShift, HiForNormalShift);12731274// Lanai shifts of larger than register sizes are wrapped rather than1275// clamped, so we can't just emit "lo << b" if b is too big.1276SDValue LoForNormalShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);1277SDValue Lo = DAG.getSelect(1278dl, MVT::i32, SetCC, DAG.getConstant(0, dl, MVT::i32), LoForNormalShift);12791280SDValue Ops[2] = {Lo, Hi};1281return DAG.getMergeValues(Ops, dl);1282}12831284SDValue LanaiTargetLowering::LowerSRL_PARTS(SDValue Op,1285SelectionDAG &DAG) const {1286MVT VT = Op.getSimpleValueType();1287unsigned VTBits = VT.getSizeInBits();1288SDLoc dl(Op);1289SDValue ShOpLo = Op.getOperand(0);1290SDValue ShOpHi = Op.getOperand(1);1291SDValue ShAmt = Op.getOperand(2);12921293// Performs the following for a >> b:1294// unsigned r_high = a_high >> b;1295// r_high = (32 - b <= 0) ? 0 : r_high;1296//1297// unsigned r_low = a_low >> b;1298// r_low = (32 - b <= 0) ? r_high : r_low;1299// r_low = (b == 0) ? r_low : r_low | (a_high << (32 - b));1300// return (unsigned long long)r_high << 32 | r_low;1301// Note: This takes advantage of Lanai's shift behavior to avoid needing to1302// mask the shift amount.13031304SDValue Zero = DAG.getConstant(0, dl, MVT::i32);1305SDValue NegatedPlus32 = DAG.getNode(1306ISD::SUB, dl, MVT::i32, DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);1307SDValue SetCC = DAG.getSetCC(dl, MVT::i32, NegatedPlus32, Zero, ISD::SETLE);13081309SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i32, ShOpHi, ShAmt);1310Hi = DAG.getSelect(dl, MVT::i32, SetCC, Zero, Hi);13111312SDValue Lo = DAG.getNode(ISD::SRL, dl, MVT::i32, ShOpLo, ShAmt);1313Lo = DAG.getSelect(dl, MVT::i32, SetCC, Hi, Lo);1314SDValue CarryBits =1315DAG.getNode(ISD::SHL, dl, MVT::i32, ShOpHi, NegatedPlus32);1316SDValue ShiftIsZero = DAG.getSetCC(dl, MVT::i32, ShAmt, Zero, ISD::SETEQ);1317Lo = DAG.getSelect(dl, MVT::i32, ShiftIsZero, Lo,1318DAG.getNode(ISD::OR, dl, MVT::i32, Lo, CarryBits));13191320SDValue Ops[2] = {Lo, Hi};1321return DAG.getMergeValues(Ops, dl);1322}13231324// Helper function that checks if N is a null or all ones constant.1325static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {1326return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);1327}13281329// Return true if N is conditionally 0 or all ones.1330// Detects these expressions where cc is an i1 value:1331//1332// (select cc 0, y) [AllOnes=0]1333// (select cc y, 0) [AllOnes=0]1334// (zext cc) [AllOnes=0]1335// (sext cc) [AllOnes=0/1]1336// (select cc -1, y) [AllOnes=1]1337// (select cc y, -1) [AllOnes=1]1338//1339// * AllOnes determines whether to check for an all zero (AllOnes false) or an1340// all ones operand (AllOnes true).1341// * Invert is set when N is the all zero/ones constant when CC is false.1342// * OtherOp is set to the alternative value of N.1343//1344// For example, for (select cc X, Y) and AllOnes = 0 if:1345// * X = 0, Invert = False and OtherOp = Y1346// * Y = 0, Invert = True and OtherOp = X1347static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes, SDValue &CC,1348bool &Invert, SDValue &OtherOp,1349SelectionDAG &DAG) {1350switch (N->getOpcode()) {1351default:1352return false;1353case ISD::SELECT: {1354CC = N->getOperand(0);1355SDValue N1 = N->getOperand(1);1356SDValue N2 = N->getOperand(2);1357if (isZeroOrAllOnes(N1, AllOnes)) {1358Invert = false;1359OtherOp = N2;1360return true;1361}1362if (isZeroOrAllOnes(N2, AllOnes)) {1363Invert = true;1364OtherOp = N1;1365return true;1366}1367return false;1368}1369case ISD::ZERO_EXTEND: {1370// (zext cc) can never be the all ones value.1371if (AllOnes)1372return false;1373CC = N->getOperand(0);1374if (CC.getValueType() != MVT::i1)1375return false;1376SDLoc dl(N);1377EVT VT = N->getValueType(0);1378OtherOp = DAG.getConstant(1, dl, VT);1379Invert = true;1380return true;1381}1382case ISD::SIGN_EXTEND: {1383CC = N->getOperand(0);1384if (CC.getValueType() != MVT::i1)1385return false;1386SDLoc dl(N);1387EVT VT = N->getValueType(0);1388Invert = !AllOnes;1389if (AllOnes)1390// When looking for an AllOnes constant, N is an sext, and the 'other'1391// value is 0.1392OtherOp = DAG.getConstant(0, dl, VT);1393else1394OtherOp = DAG.getAllOnesConstant(dl, VT);1395return true;1396}1397}1398}13991400// Combine a constant select operand into its use:1401//1402// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))1403// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))1404// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]1405// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))1406// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))1407//1408// The transform is rejected if the select doesn't have a constant operand that1409// is null, or all ones when AllOnes is set.1410//1411// Also recognize sext/zext from i1:1412//1413// (add (zext cc), x) -> (select cc (add x, 1), x)1414// (add (sext cc), x) -> (select cc (add x, -1), x)1415//1416// These transformations eventually create predicated instructions.1417static SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,1418TargetLowering::DAGCombinerInfo &DCI,1419bool AllOnes) {1420SelectionDAG &DAG = DCI.DAG;1421EVT VT = N->getValueType(0);1422SDValue NonConstantVal;1423SDValue CCOp;1424bool SwapSelectOps;1425if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,1426NonConstantVal, DAG))1427return SDValue();14281429// Slct is now know to be the desired identity constant when CC is true.1430SDValue TrueVal = OtherOp;1431SDValue FalseVal =1432DAG.getNode(N->getOpcode(), SDLoc(N), VT, OtherOp, NonConstantVal);1433// Unless SwapSelectOps says CC should be false.1434if (SwapSelectOps)1435std::swap(TrueVal, FalseVal);14361437return DAG.getNode(ISD::SELECT, SDLoc(N), VT, CCOp, TrueVal, FalseVal);1438}14391440// Attempt combineSelectAndUse on each operand of a commutative operator N.1441static SDValue1442combineSelectAndUseCommutative(SDNode *N, TargetLowering::DAGCombinerInfo &DCI,1443bool AllOnes) {1444SDValue N0 = N->getOperand(0);1445SDValue N1 = N->getOperand(1);1446if (N0.getNode()->hasOneUse())1447if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes))1448return Result;1449if (N1.getNode()->hasOneUse())1450if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes))1451return Result;1452return SDValue();1453}14541455// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.1456static SDValue PerformSUBCombine(SDNode *N,1457TargetLowering::DAGCombinerInfo &DCI) {1458SDValue N0 = N->getOperand(0);1459SDValue N1 = N->getOperand(1);14601461// fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))1462if (N1.getNode()->hasOneUse())1463if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, /*AllOnes=*/false))1464return Result;14651466return SDValue();1467}14681469SDValue LanaiTargetLowering::PerformDAGCombine(SDNode *N,1470DAGCombinerInfo &DCI) const {1471switch (N->getOpcode()) {1472default:1473break;1474case ISD::ADD:1475case ISD::OR:1476case ISD::XOR:1477return combineSelectAndUseCommutative(N, DCI, /*AllOnes=*/false);1478case ISD::AND:1479return combineSelectAndUseCommutative(N, DCI, /*AllOnes=*/true);1480case ISD::SUB:1481return PerformSUBCombine(N, DCI);1482}14831484return SDValue();1485}14861487void LanaiTargetLowering::computeKnownBitsForTargetNode(1488const SDValue Op, KnownBits &Known, const APInt &DemandedElts,1489const SelectionDAG &DAG, unsigned Depth) const {1490unsigned BitWidth = Known.getBitWidth();1491switch (Op.getOpcode()) {1492default:1493break;1494case LanaiISD::SETCC:1495Known = KnownBits(BitWidth);1496Known.Zero.setBits(1, BitWidth);1497break;1498case LanaiISD::SELECT_CC:1499KnownBits Known2;1500Known = DAG.computeKnownBits(Op->getOperand(0), Depth + 1);1501Known2 = DAG.computeKnownBits(Op->getOperand(1), Depth + 1);1502Known = Known.intersectWith(Known2);1503break;1504}1505}150615071508