1 //===-- CSKYISelLowering.cpp - CSKY 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 defines the interfaces that CSKY uses to lower LLVM code into a 10 // selection DAG. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_LIB_TARGET_CSKY_CSKYISELLOWERING_H 15 #define LLVM_LIB_TARGET_CSKY_CSKYISELLOWERING_H 16 17 #include "MCTargetDesc/CSKYBaseInfo.h" 18 #include "llvm/CodeGen/CallingConvLower.h" 19 #include "llvm/CodeGen/TargetLowering.h" 20 21 namespace llvm { 22 class CSKYSubtarget; 23 24 namespace CSKYISD { 25 enum NodeType : unsigned { 26 FIRST_NUMBER = ISD::BUILTIN_OP_END, 27 NIE, 28 NIR, 29 RET, 30 CALL, 31 CALLReg, 32 TAIL, 33 TAILReg, 34 LOAD_ADDR, 35 // i32, i32 <-- f64 36 BITCAST_TO_LOHI, 37 // f64 < -- i32, i32 38 BITCAST_FROM_LOHI, 39 }; 40 } 41 42 class CSKYTargetLowering : public TargetLowering { 43 const CSKYSubtarget &Subtarget; 44 45 public: 46 explicit CSKYTargetLowering(const TargetMachine &TM, 47 const CSKYSubtarget &STI); 48 49 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override; 50 51 EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, 52 EVT VT) const override; 53 54 private: 55 SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, 56 bool IsVarArg, 57 const SmallVectorImpl<ISD::InputArg> &Ins, 58 const SDLoc &DL, SelectionDAG &DAG, 59 SmallVectorImpl<SDValue> &InVals) const override; 60 61 bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF, 62 bool IsVarArg, 63 const SmallVectorImpl<ISD::OutputArg> &Outs, 64 LLVMContext &Context) const override; 65 66 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 67 const SmallVectorImpl<ISD::OutputArg> &Outs, 68 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL, 69 SelectionDAG &DAG) const override; 70 71 SDValue LowerCall(TargetLowering::CallLoweringInfo &CLI, 72 SmallVectorImpl<SDValue> &InVals) const override; 73 74 const char *getTargetNodeName(unsigned Opcode) const override; 75 76 /// If a physical register, this returns the register that receives the 77 /// exception address on entry to an EH pad. 78 Register 79 getExceptionPointerRegister(const Constant *PersonalityFn) const override; 80 81 /// If a physical register, this returns the register that receives the 82 /// exception typeid on entry to a landing pad. 83 Register 84 getExceptionSelectorRegister(const Constant *PersonalityFn) const override; 85 86 bool isSelectSupported(SelectSupportKind Kind) const override { 87 // CSKY does not support scalar condition selects on vectors. 88 return (Kind != ScalarCondVectorVal); 89 } 90 91 ConstraintType getConstraintType(StringRef Constraint) const override; 92 93 std::pair<unsigned, const TargetRegisterClass *> 94 getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 95 StringRef Constraint, MVT VT) const override; 96 97 MachineBasicBlock * 98 EmitInstrWithCustomInserter(MachineInstr &MI, 99 MachineBasicBlock *BB) const override; 100 101 SDValue getTargetNode(GlobalAddressSDNode *N, SDLoc DL, EVT Ty, 102 SelectionDAG &DAG, unsigned Flags) const; 103 104 SDValue getTargetNode(ExternalSymbolSDNode *N, SDLoc DL, EVT Ty, 105 SelectionDAG &DAG, unsigned Flags) const; 106 107 SDValue getTargetNode(JumpTableSDNode *N, SDLoc DL, EVT Ty, SelectionDAG &DAG, 108 unsigned Flags) const; 109 110 SDValue getTargetNode(BlockAddressSDNode *N, SDLoc DL, EVT Ty, 111 SelectionDAG &DAG, unsigned Flags) const; 112 113 SDValue getTargetConstantPoolValue(GlobalAddressSDNode *N, EVT Ty, 114 SelectionDAG &DAG, unsigned Flags) const; 115 116 SDValue getTargetConstantPoolValue(ExternalSymbolSDNode *N, EVT Ty, 117 SelectionDAG &DAG, unsigned Flags) const; 118 119 SDValue getTargetConstantPoolValue(JumpTableSDNode *N, EVT Ty, 120 SelectionDAG &DAG, unsigned Flags) const; 121 122 SDValue getTargetConstantPoolValue(BlockAddressSDNode *N, EVT Ty, 123 SelectionDAG &DAG, unsigned Flags) const; 124 125 template <class NodeTy, bool IsCall = false> 126 SDValue getAddr(NodeTy *N, SelectionDAG &DAG, bool IsLocal = true) const { 127 SDLoc DL(N); 128 EVT Ty = getPointerTy(DAG.getDataLayout()); 129 130 unsigned Flag = CSKYII::MO_None; 131 bool IsPIC = isPositionIndependent(); 132 133 if (IsPIC) 134 Flag = IsLocal ? CSKYII::MO_GOTOFF 135 : IsCall ? CSKYII::MO_PLT32 136 : CSKYII::MO_GOT32; 137 138 SDValue TCPV = getTargetConstantPoolValue(N, Ty, DAG, Flag); 139 SDValue TV = getTargetNode(N, DL, Ty, DAG, Flag); 140 SDValue Addr = DAG.getNode(CSKYISD::LOAD_ADDR, DL, Ty, {TV, TCPV}); 141 142 if (!IsPIC) 143 return Addr; 144 145 SDValue Result = 146 DAG.getNode(ISD::ADD, DL, Ty, {DAG.getGLOBAL_OFFSET_TABLE(Ty), Addr}); 147 if (IsLocal) 148 return Result; 149 150 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Result, 151 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 152 } 153 154 SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const; 155 SDValue LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const; 156 SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const; 157 SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const; 158 SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const; 159 SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) const; 160 SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const; 161 SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const; 162 163 SDValue getStaticTLSAddr(GlobalAddressSDNode *N, SelectionDAG &DAG, 164 bool UseGOT) const; 165 SDValue getDynamicTLSAddr(GlobalAddressSDNode *N, SelectionDAG &DAG) const; 166 167 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg) const; 168 CCAssignFn *CCAssignFnForReturn(CallingConv::ID CC, bool IsVarArg) const; 169 }; 170 171 } // namespace llvm 172 173 #endif // LLVM_LIB_TARGET_CSKY_CSKYISELLOWERING_H 174