1 //===- MipsISelLowering.cpp - Mips 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 Mips uses to lower LLVM code into a 10 // selection DAG. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MipsISelLowering.h" 15 #include "MCTargetDesc/MipsBaseInfo.h" 16 #include "MCTargetDesc/MipsInstPrinter.h" 17 #include "MCTargetDesc/MipsMCTargetDesc.h" 18 #include "MipsCCState.h" 19 #include "MipsInstrInfo.h" 20 #include "MipsMachineFunction.h" 21 #include "MipsRegisterInfo.h" 22 #include "MipsSubtarget.h" 23 #include "MipsTargetMachine.h" 24 #include "MipsTargetObjectFile.h" 25 #include "llvm/ADT/APFloat.h" 26 #include "llvm/ADT/ArrayRef.h" 27 #include "llvm/ADT/SmallVector.h" 28 #include "llvm/ADT/Statistic.h" 29 #include "llvm/ADT/StringRef.h" 30 #include "llvm/ADT/StringSwitch.h" 31 #include "llvm/CodeGen/CallingConvLower.h" 32 #include "llvm/CodeGen/FunctionLoweringInfo.h" 33 #include "llvm/CodeGen/ISDOpcodes.h" 34 #include "llvm/CodeGen/MachineBasicBlock.h" 35 #include "llvm/CodeGen/MachineFrameInfo.h" 36 #include "llvm/CodeGen/MachineFunction.h" 37 #include "llvm/CodeGen/MachineInstr.h" 38 #include "llvm/CodeGen/MachineInstrBuilder.h" 39 #include "llvm/CodeGen/MachineJumpTableInfo.h" 40 #include "llvm/CodeGen/MachineMemOperand.h" 41 #include "llvm/CodeGen/MachineOperand.h" 42 #include "llvm/CodeGen/MachineRegisterInfo.h" 43 #include "llvm/CodeGen/RuntimeLibcalls.h" 44 #include "llvm/CodeGen/SelectionDAG.h" 45 #include "llvm/CodeGen/SelectionDAGNodes.h" 46 #include "llvm/CodeGen/TargetFrameLowering.h" 47 #include "llvm/CodeGen/TargetInstrInfo.h" 48 #include "llvm/CodeGen/TargetRegisterInfo.h" 49 #include "llvm/CodeGen/ValueTypes.h" 50 #include "llvm/IR/CallingConv.h" 51 #include "llvm/IR/Constants.h" 52 #include "llvm/IR/DataLayout.h" 53 #include "llvm/IR/DebugLoc.h" 54 #include "llvm/IR/DerivedTypes.h" 55 #include "llvm/IR/Function.h" 56 #include "llvm/IR/GlobalValue.h" 57 #include "llvm/IR/Type.h" 58 #include "llvm/IR/Value.h" 59 #include "llvm/MC/MCContext.h" 60 #include "llvm/MC/MCRegisterInfo.h" 61 #include "llvm/Support/Casting.h" 62 #include "llvm/Support/CodeGen.h" 63 #include "llvm/Support/CommandLine.h" 64 #include "llvm/Support/Compiler.h" 65 #include "llvm/Support/ErrorHandling.h" 66 #include "llvm/Support/MachineValueType.h" 67 #include "llvm/Support/MathExtras.h" 68 #include "llvm/Target/TargetMachine.h" 69 #include "llvm/Target/TargetOptions.h" 70 #include <algorithm> 71 #include <cassert> 72 #include <cctype> 73 #include <cstdint> 74 #include <deque> 75 #include <iterator> 76 #include <utility> 77 #include <vector> 78 79 using namespace llvm; 80 81 #define DEBUG_TYPE "mips-lower" 82 83 STATISTIC(NumTailCalls, "Number of tail calls"); 84 85 static cl::opt<bool> 86 NoZeroDivCheck("mno-check-zero-division", cl::Hidden, 87 cl::desc("MIPS: Don't trap on integer division by zero."), 88 cl::init(false)); 89 90 extern cl::opt<bool> EmitJalrReloc; 91 92 static const MCPhysReg Mips64DPRegs[8] = { 93 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64, 94 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64 95 }; 96 97 // If I is a shifted mask, set the size (Size) and the first bit of the 98 // mask (Pos), and return true. 99 // For example, if I is 0x003ff800, (Pos, Size) = (11, 11). 100 static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) { 101 if (!isShiftedMask_64(I)) 102 return false; 103 104 Size = countPopulation(I); 105 Pos = countTrailingZeros(I); 106 return true; 107 } 108 109 // The MIPS MSA ABI passes vector arguments in the integer register set. 110 // The number of integer registers used is dependant on the ABI used. 111 MVT MipsTargetLowering::getRegisterTypeForCallingConv(LLVMContext &Context, 112 CallingConv::ID CC, 113 EVT VT) const { 114 if (!VT.isVector()) 115 return getRegisterType(Context, VT); 116 117 return Subtarget.isABI_O32() || VT.getSizeInBits() == 32 ? MVT::i32 118 : MVT::i64; 119 } 120 121 unsigned MipsTargetLowering::getNumRegistersForCallingConv(LLVMContext &Context, 122 CallingConv::ID CC, 123 EVT VT) const { 124 if (VT.isVector()) 125 return divideCeil(VT.getSizeInBits(), Subtarget.isABI_O32() ? 32 : 64); 126 return MipsTargetLowering::getNumRegisters(Context, VT); 127 } 128 129 unsigned MipsTargetLowering::getVectorTypeBreakdownForCallingConv( 130 LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, 131 unsigned &NumIntermediates, MVT &RegisterVT) const { 132 // Break down vector types to either 2 i64s or 4 i32s. 133 RegisterVT = getRegisterTypeForCallingConv(Context, CC, VT); 134 IntermediateVT = RegisterVT; 135 NumIntermediates = 136 VT.getFixedSizeInBits() < RegisterVT.getFixedSizeInBits() 137 ? VT.getVectorNumElements() 138 : divideCeil(VT.getSizeInBits(), RegisterVT.getSizeInBits()); 139 return NumIntermediates; 140 } 141 142 SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const { 143 MachineFunction &MF = DAG.getMachineFunction(); 144 MipsFunctionInfo *FI = MF.getInfo<MipsFunctionInfo>(); 145 return DAG.getRegister(FI->getGlobalBaseReg(MF), Ty); 146 } 147 148 SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty, 149 SelectionDAG &DAG, 150 unsigned Flag) const { 151 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag); 152 } 153 154 SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty, 155 SelectionDAG &DAG, 156 unsigned Flag) const { 157 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag); 158 } 159 160 SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty, 161 SelectionDAG &DAG, 162 unsigned Flag) const { 163 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag); 164 } 165 166 SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty, 167 SelectionDAG &DAG, 168 unsigned Flag) const { 169 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag); 170 } 171 172 SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty, 173 SelectionDAG &DAG, 174 unsigned Flag) const { 175 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlign(), 176 N->getOffset(), Flag); 177 } 178 179 const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const { 180 switch ((MipsISD::NodeType)Opcode) { 181 case MipsISD::FIRST_NUMBER: break; 182 case MipsISD::JmpLink: return "MipsISD::JmpLink"; 183 case MipsISD::TailCall: return "MipsISD::TailCall"; 184 case MipsISD::Highest: return "MipsISD::Highest"; 185 case MipsISD::Higher: return "MipsISD::Higher"; 186 case MipsISD::Hi: return "MipsISD::Hi"; 187 case MipsISD::Lo: return "MipsISD::Lo"; 188 case MipsISD::GotHi: return "MipsISD::GotHi"; 189 case MipsISD::TlsHi: return "MipsISD::TlsHi"; 190 case MipsISD::GPRel: return "MipsISD::GPRel"; 191 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer"; 192 case MipsISD::Ret: return "MipsISD::Ret"; 193 case MipsISD::ERet: return "MipsISD::ERet"; 194 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN"; 195 case MipsISD::FMS: return "MipsISD::FMS"; 196 case MipsISD::FPBrcond: return "MipsISD::FPBrcond"; 197 case MipsISD::FPCmp: return "MipsISD::FPCmp"; 198 case MipsISD::FSELECT: return "MipsISD::FSELECT"; 199 case MipsISD::MTC1_D64: return "MipsISD::MTC1_D64"; 200 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T"; 201 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F"; 202 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP"; 203 case MipsISD::MFHI: return "MipsISD::MFHI"; 204 case MipsISD::MFLO: return "MipsISD::MFLO"; 205 case MipsISD::MTLOHI: return "MipsISD::MTLOHI"; 206 case MipsISD::Mult: return "MipsISD::Mult"; 207 case MipsISD::Multu: return "MipsISD::Multu"; 208 case MipsISD::MAdd: return "MipsISD::MAdd"; 209 case MipsISD::MAddu: return "MipsISD::MAddu"; 210 case MipsISD::MSub: return "MipsISD::MSub"; 211 case MipsISD::MSubu: return "MipsISD::MSubu"; 212 case MipsISD::DivRem: return "MipsISD::DivRem"; 213 case MipsISD::DivRemU: return "MipsISD::DivRemU"; 214 case MipsISD::DivRem16: return "MipsISD::DivRem16"; 215 case MipsISD::DivRemU16: return "MipsISD::DivRemU16"; 216 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64"; 217 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64"; 218 case MipsISD::Wrapper: return "MipsISD::Wrapper"; 219 case MipsISD::DynAlloc: return "MipsISD::DynAlloc"; 220 case MipsISD::Sync: return "MipsISD::Sync"; 221 case MipsISD::Ext: return "MipsISD::Ext"; 222 case MipsISD::Ins: return "MipsISD::Ins"; 223 case MipsISD::CIns: return "MipsISD::CIns"; 224 case MipsISD::LWL: return "MipsISD::LWL"; 225 case MipsISD::LWR: return "MipsISD::LWR"; 226 case MipsISD::SWL: return "MipsISD::SWL"; 227 case MipsISD::SWR: return "MipsISD::SWR"; 228 case MipsISD::LDL: return "MipsISD::LDL"; 229 case MipsISD::LDR: return "MipsISD::LDR"; 230 case MipsISD::SDL: return "MipsISD::SDL"; 231 case MipsISD::SDR: return "MipsISD::SDR"; 232 case MipsISD::EXTP: return "MipsISD::EXTP"; 233 case MipsISD::EXTPDP: return "MipsISD::EXTPDP"; 234 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H"; 235 case MipsISD::EXTR_W: return "MipsISD::EXTR_W"; 236 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W"; 237 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W"; 238 case MipsISD::SHILO: return "MipsISD::SHILO"; 239 case MipsISD::MTHLIP: return "MipsISD::MTHLIP"; 240 case MipsISD::MULSAQ_S_W_PH: return "MipsISD::MULSAQ_S_W_PH"; 241 case MipsISD::MAQ_S_W_PHL: return "MipsISD::MAQ_S_W_PHL"; 242 case MipsISD::MAQ_S_W_PHR: return "MipsISD::MAQ_S_W_PHR"; 243 case MipsISD::MAQ_SA_W_PHL: return "MipsISD::MAQ_SA_W_PHL"; 244 case MipsISD::MAQ_SA_W_PHR: return "MipsISD::MAQ_SA_W_PHR"; 245 case MipsISD::DPAU_H_QBL: return "MipsISD::DPAU_H_QBL"; 246 case MipsISD::DPAU_H_QBR: return "MipsISD::DPAU_H_QBR"; 247 case MipsISD::DPSU_H_QBL: return "MipsISD::DPSU_H_QBL"; 248 case MipsISD::DPSU_H_QBR: return "MipsISD::DPSU_H_QBR"; 249 case MipsISD::DPAQ_S_W_PH: return "MipsISD::DPAQ_S_W_PH"; 250 case MipsISD::DPSQ_S_W_PH: return "MipsISD::DPSQ_S_W_PH"; 251 case MipsISD::DPAQ_SA_L_W: return "MipsISD::DPAQ_SA_L_W"; 252 case MipsISD::DPSQ_SA_L_W: return "MipsISD::DPSQ_SA_L_W"; 253 case MipsISD::DPA_W_PH: return "MipsISD::DPA_W_PH"; 254 case MipsISD::DPS_W_PH: return "MipsISD::DPS_W_PH"; 255 case MipsISD::DPAQX_S_W_PH: return "MipsISD::DPAQX_S_W_PH"; 256 case MipsISD::DPAQX_SA_W_PH: return "MipsISD::DPAQX_SA_W_PH"; 257 case MipsISD::DPAX_W_PH: return "MipsISD::DPAX_W_PH"; 258 case MipsISD::DPSX_W_PH: return "MipsISD::DPSX_W_PH"; 259 case MipsISD::DPSQX_S_W_PH: return "MipsISD::DPSQX_S_W_PH"; 260 case MipsISD::DPSQX_SA_W_PH: return "MipsISD::DPSQX_SA_W_PH"; 261 case MipsISD::MULSA_W_PH: return "MipsISD::MULSA_W_PH"; 262 case MipsISD::MULT: return "MipsISD::MULT"; 263 case MipsISD::MULTU: return "MipsISD::MULTU"; 264 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP"; 265 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP"; 266 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP"; 267 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP"; 268 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP"; 269 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP"; 270 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP"; 271 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP"; 272 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP"; 273 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO"; 274 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO"; 275 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO"; 276 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO"; 277 case MipsISD::VCEQ: return "MipsISD::VCEQ"; 278 case MipsISD::VCLE_S: return "MipsISD::VCLE_S"; 279 case MipsISD::VCLE_U: return "MipsISD::VCLE_U"; 280 case MipsISD::VCLT_S: return "MipsISD::VCLT_S"; 281 case MipsISD::VCLT_U: return "MipsISD::VCLT_U"; 282 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT"; 283 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT"; 284 case MipsISD::VNOR: return "MipsISD::VNOR"; 285 case MipsISD::VSHF: return "MipsISD::VSHF"; 286 case MipsISD::SHF: return "MipsISD::SHF"; 287 case MipsISD::ILVEV: return "MipsISD::ILVEV"; 288 case MipsISD::ILVOD: return "MipsISD::ILVOD"; 289 case MipsISD::ILVL: return "MipsISD::ILVL"; 290 case MipsISD::ILVR: return "MipsISD::ILVR"; 291 case MipsISD::PCKEV: return "MipsISD::PCKEV"; 292 case MipsISD::PCKOD: return "MipsISD::PCKOD"; 293 case MipsISD::INSVE: return "MipsISD::INSVE"; 294 } 295 return nullptr; 296 } 297 298 MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM, 299 const MipsSubtarget &STI) 300 : TargetLowering(TM), Subtarget(STI), ABI(TM.getABI()) { 301 // Mips does not have i1 type, so use i32 for 302 // setcc operations results (slt, sgt, ...). 303 setBooleanContents(ZeroOrOneBooleanContent); 304 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 305 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA 306 // does. Integer booleans still use 0 and 1. 307 if (Subtarget.hasMips32r6()) 308 setBooleanContents(ZeroOrOneBooleanContent, 309 ZeroOrNegativeOneBooleanContent); 310 311 // Load extented operations for i1 types must be promoted 312 for (MVT VT : MVT::integer_valuetypes()) { 313 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote); 314 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); 315 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 316 } 317 318 // MIPS doesn't have extending float->double load/store. Set LoadExtAction 319 // for f32, f16 320 for (MVT VT : MVT::fp_valuetypes()) { 321 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand); 322 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand); 323 } 324 325 // Set LoadExtAction for f16 vectors to Expand 326 for (MVT VT : MVT::fp_fixedlen_vector_valuetypes()) { 327 MVT F16VT = MVT::getVectorVT(MVT::f16, VT.getVectorNumElements()); 328 if (F16VT.isValid()) 329 setLoadExtAction(ISD::EXTLOAD, VT, F16VT, Expand); 330 } 331 332 setTruncStoreAction(MVT::f32, MVT::f16, Expand); 333 setTruncStoreAction(MVT::f64, MVT::f16, Expand); 334 335 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 336 337 // Used by legalize types to correctly generate the setcc result. 338 // Without this, every float setcc comes with a AND/OR with the result, 339 // we don't want this, since the fpcmp result goes to a flag register, 340 // which is used implicitly by brcond and select operations. 341 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32); 342 343 // Mips Custom Operations 344 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 345 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 346 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 347 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 348 setOperationAction(ISD::JumpTable, MVT::i32, Custom); 349 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 350 setOperationAction(ISD::SELECT, MVT::f32, Custom); 351 setOperationAction(ISD::SELECT, MVT::f64, Custom); 352 setOperationAction(ISD::SELECT, MVT::i32, Custom); 353 setOperationAction(ISD::SETCC, MVT::f32, Custom); 354 setOperationAction(ISD::SETCC, MVT::f64, Custom); 355 setOperationAction(ISD::BRCOND, MVT::Other, Custom); 356 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 357 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 358 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 359 360 if (!(TM.Options.NoNaNsFPMath || Subtarget.inAbs2008Mode())) { 361 setOperationAction(ISD::FABS, MVT::f32, Custom); 362 setOperationAction(ISD::FABS, MVT::f64, Custom); 363 } 364 365 if (Subtarget.isGP64bit()) { 366 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); 367 setOperationAction(ISD::BlockAddress, MVT::i64, Custom); 368 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); 369 setOperationAction(ISD::JumpTable, MVT::i64, Custom); 370 setOperationAction(ISD::ConstantPool, MVT::i64, Custom); 371 setOperationAction(ISD::SELECT, MVT::i64, Custom); 372 setOperationAction(ISD::LOAD, MVT::i64, Custom); 373 setOperationAction(ISD::STORE, MVT::i64, Custom); 374 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 375 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom); 376 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom); 377 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom); 378 } 379 380 if (!Subtarget.isGP64bit()) { 381 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 382 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 383 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 384 } 385 386 setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom); 387 if (Subtarget.isGP64bit()) 388 setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom); 389 390 setOperationAction(ISD::SDIV, MVT::i32, Expand); 391 setOperationAction(ISD::SREM, MVT::i32, Expand); 392 setOperationAction(ISD::UDIV, MVT::i32, Expand); 393 setOperationAction(ISD::UREM, MVT::i32, Expand); 394 setOperationAction(ISD::SDIV, MVT::i64, Expand); 395 setOperationAction(ISD::SREM, MVT::i64, Expand); 396 setOperationAction(ISD::UDIV, MVT::i64, Expand); 397 setOperationAction(ISD::UREM, MVT::i64, Expand); 398 399 // Operations not directly supported by Mips. 400 setOperationAction(ISD::BR_CC, MVT::f32, Expand); 401 setOperationAction(ISD::BR_CC, MVT::f64, Expand); 402 setOperationAction(ISD::BR_CC, MVT::i32, Expand); 403 setOperationAction(ISD::BR_CC, MVT::i64, Expand); 404 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand); 405 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); 406 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); 407 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); 408 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); 409 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); 410 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); 411 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand); 412 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 413 if (Subtarget.hasCnMips()) { 414 setOperationAction(ISD::CTPOP, MVT::i32, Legal); 415 setOperationAction(ISD::CTPOP, MVT::i64, Legal); 416 } else { 417 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 418 setOperationAction(ISD::CTPOP, MVT::i64, Expand); 419 } 420 setOperationAction(ISD::CTTZ, MVT::i32, Expand); 421 setOperationAction(ISD::CTTZ, MVT::i64, Expand); 422 setOperationAction(ISD::ROTL, MVT::i32, Expand); 423 setOperationAction(ISD::ROTL, MVT::i64, Expand); 424 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 425 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); 426 427 if (!Subtarget.hasMips32r2()) 428 setOperationAction(ISD::ROTR, MVT::i32, Expand); 429 430 if (!Subtarget.hasMips64r2()) 431 setOperationAction(ISD::ROTR, MVT::i64, Expand); 432 433 setOperationAction(ISD::FSIN, MVT::f32, Expand); 434 setOperationAction(ISD::FSIN, MVT::f64, Expand); 435 setOperationAction(ISD::FCOS, MVT::f32, Expand); 436 setOperationAction(ISD::FCOS, MVT::f64, Expand); 437 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 438 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 439 setOperationAction(ISD::FPOW, MVT::f32, Expand); 440 setOperationAction(ISD::FPOW, MVT::f64, Expand); 441 setOperationAction(ISD::FLOG, MVT::f32, Expand); 442 setOperationAction(ISD::FLOG2, MVT::f32, Expand); 443 setOperationAction(ISD::FLOG10, MVT::f32, Expand); 444 setOperationAction(ISD::FEXP, MVT::f32, Expand); 445 setOperationAction(ISD::FMA, MVT::f32, Expand); 446 setOperationAction(ISD::FMA, MVT::f64, Expand); 447 setOperationAction(ISD::FREM, MVT::f32, Expand); 448 setOperationAction(ISD::FREM, MVT::f64, Expand); 449 450 // Lower f16 conversion operations into library calls 451 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand); 452 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand); 453 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand); 454 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand); 455 456 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom); 457 458 setOperationAction(ISD::VASTART, MVT::Other, Custom); 459 setOperationAction(ISD::VAARG, MVT::Other, Custom); 460 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 461 setOperationAction(ISD::VAEND, MVT::Other, Expand); 462 463 // Use the default for now 464 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 465 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 466 467 if (!Subtarget.isGP64bit()) { 468 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand); 469 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand); 470 } 471 472 if (!Subtarget.hasMips32r2()) { 473 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); 474 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 475 } 476 477 // MIPS16 lacks MIPS32's clz and clo instructions. 478 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode()) 479 setOperationAction(ISD::CTLZ, MVT::i32, Expand); 480 if (!Subtarget.hasMips64()) 481 setOperationAction(ISD::CTLZ, MVT::i64, Expand); 482 483 if (!Subtarget.hasMips32r2()) 484 setOperationAction(ISD::BSWAP, MVT::i32, Expand); 485 if (!Subtarget.hasMips64r2()) 486 setOperationAction(ISD::BSWAP, MVT::i64, Expand); 487 488 if (Subtarget.isGP64bit()) { 489 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom); 490 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom); 491 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom); 492 setTruncStoreAction(MVT::i64, MVT::i32, Custom); 493 } 494 495 setOperationAction(ISD::TRAP, MVT::Other, Legal); 496 497 setTargetDAGCombine(ISD::SDIVREM); 498 setTargetDAGCombine(ISD::UDIVREM); 499 setTargetDAGCombine(ISD::SELECT); 500 setTargetDAGCombine(ISD::AND); 501 setTargetDAGCombine(ISD::OR); 502 setTargetDAGCombine(ISD::ADD); 503 setTargetDAGCombine(ISD::SUB); 504 setTargetDAGCombine(ISD::AssertZext); 505 setTargetDAGCombine(ISD::SHL); 506 507 if (ABI.IsO32()) { 508 // These libcalls are not available in 32-bit. 509 setLibcallName(RTLIB::SHL_I128, nullptr); 510 setLibcallName(RTLIB::SRL_I128, nullptr); 511 setLibcallName(RTLIB::SRA_I128, nullptr); 512 setLibcallName(RTLIB::MUL_I128, nullptr); 513 setLibcallName(RTLIB::MULO_I64, nullptr); 514 setLibcallName(RTLIB::MULO_I128, nullptr); 515 } 516 517 setMinFunctionAlignment(Subtarget.isGP64bit() ? Align(8) : Align(4)); 518 519 // The arguments on the stack are defined in terms of 4-byte slots on O32 520 // and 8-byte slots on N32/N64. 521 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? Align(8) 522 : Align(4)); 523 524 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP); 525 526 MaxStoresPerMemcpy = 16; 527 528 isMicroMips = Subtarget.inMicroMipsMode(); 529 } 530 531 const MipsTargetLowering * 532 MipsTargetLowering::create(const MipsTargetMachine &TM, 533 const MipsSubtarget &STI) { 534 if (STI.inMips16Mode()) 535 return createMips16TargetLowering(TM, STI); 536 537 return createMipsSETargetLowering(TM, STI); 538 } 539 540 // Create a fast isel object. 541 FastISel * 542 MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo, 543 const TargetLibraryInfo *libInfo) const { 544 const MipsTargetMachine &TM = 545 static_cast<const MipsTargetMachine &>(funcInfo.MF->getTarget()); 546 547 // We support only the standard encoding [MIPS32,MIPS32R5] ISAs. 548 bool UseFastISel = TM.Options.EnableFastISel && Subtarget.hasMips32() && 549 !Subtarget.hasMips32r6() && !Subtarget.inMips16Mode() && 550 !Subtarget.inMicroMipsMode(); 551 552 // Disable if either of the following is true: 553 // We do not generate PIC, the ABI is not O32, XGOT is being used. 554 if (!TM.isPositionIndependent() || !TM.getABI().IsO32() || 555 Subtarget.useXGOT()) 556 UseFastISel = false; 557 558 return UseFastISel ? Mips::createFastISel(funcInfo, libInfo) : nullptr; 559 } 560 561 EVT MipsTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &, 562 EVT VT) const { 563 if (!VT.isVector()) 564 return MVT::i32; 565 return VT.changeVectorElementTypeToInteger(); 566 } 567 568 static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG, 569 TargetLowering::DAGCombinerInfo &DCI, 570 const MipsSubtarget &Subtarget) { 571 if (DCI.isBeforeLegalizeOps()) 572 return SDValue(); 573 574 EVT Ty = N->getValueType(0); 575 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64; 576 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64; 577 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 : 578 MipsISD::DivRemU16; 579 SDLoc DL(N); 580 581 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue, 582 N->getOperand(0), N->getOperand(1)); 583 SDValue InChain = DAG.getEntryNode(); 584 SDValue InGlue = DivRem; 585 586 // insert MFLO 587 if (N->hasAnyUseOfValue(0)) { 588 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty, 589 InGlue); 590 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo); 591 InChain = CopyFromLo.getValue(1); 592 InGlue = CopyFromLo.getValue(2); 593 } 594 595 // insert MFHI 596 if (N->hasAnyUseOfValue(1)) { 597 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL, 598 HI, Ty, InGlue); 599 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi); 600 } 601 602 return SDValue(); 603 } 604 605 static Mips::CondCode condCodeToFCC(ISD::CondCode CC) { 606 switch (CC) { 607 default: llvm_unreachable("Unknown fp condition code!"); 608 case ISD::SETEQ: 609 case ISD::SETOEQ: return Mips::FCOND_OEQ; 610 case ISD::SETUNE: return Mips::FCOND_UNE; 611 case ISD::SETLT: 612 case ISD::SETOLT: return Mips::FCOND_OLT; 613 case ISD::SETGT: 614 case ISD::SETOGT: return Mips::FCOND_OGT; 615 case ISD::SETLE: 616 case ISD::SETOLE: return Mips::FCOND_OLE; 617 case ISD::SETGE: 618 case ISD::SETOGE: return Mips::FCOND_OGE; 619 case ISD::SETULT: return Mips::FCOND_ULT; 620 case ISD::SETULE: return Mips::FCOND_ULE; 621 case ISD::SETUGT: return Mips::FCOND_UGT; 622 case ISD::SETUGE: return Mips::FCOND_UGE; 623 case ISD::SETUO: return Mips::FCOND_UN; 624 case ISD::SETO: return Mips::FCOND_OR; 625 case ISD::SETNE: 626 case ISD::SETONE: return Mips::FCOND_ONE; 627 case ISD::SETUEQ: return Mips::FCOND_UEQ; 628 } 629 } 630 631 /// This function returns true if the floating point conditional branches and 632 /// conditional moves which use condition code CC should be inverted. 633 static bool invertFPCondCodeUser(Mips::CondCode CC) { 634 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT) 635 return false; 636 637 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) && 638 "Illegal Condition Code"); 639 640 return true; 641 } 642 643 // Creates and returns an FPCmp node from a setcc node. 644 // Returns Op if setcc is not a floating point comparison. 645 static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) { 646 // must be a SETCC node 647 if (Op.getOpcode() != ISD::SETCC) 648 return Op; 649 650 SDValue LHS = Op.getOperand(0); 651 652 if (!LHS.getValueType().isFloatingPoint()) 653 return Op; 654 655 SDValue RHS = Op.getOperand(1); 656 SDLoc DL(Op); 657 658 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of 659 // node if necessary. 660 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 661 662 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS, 663 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32)); 664 } 665 666 // Creates and returns a CMovFPT/F node. 667 static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True, 668 SDValue False, const SDLoc &DL) { 669 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2)); 670 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue()); 671 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32); 672 673 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL, 674 True.getValueType(), True, FCC0, False, Cond); 675 } 676 677 static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG, 678 TargetLowering::DAGCombinerInfo &DCI, 679 const MipsSubtarget &Subtarget) { 680 if (DCI.isBeforeLegalizeOps()) 681 return SDValue(); 682 683 SDValue SetCC = N->getOperand(0); 684 685 if ((SetCC.getOpcode() != ISD::SETCC) || 686 !SetCC.getOperand(0).getValueType().isInteger()) 687 return SDValue(); 688 689 SDValue False = N->getOperand(2); 690 EVT FalseTy = False.getValueType(); 691 692 if (!FalseTy.isInteger()) 693 return SDValue(); 694 695 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False); 696 697 // If the RHS (False) is 0, we swap the order of the operands 698 // of ISD::SELECT (obviously also inverting the condition) so that we can 699 // take advantage of conditional moves using the $0 register. 700 // Example: 701 // return (a != 0) ? x : 0; 702 // load $reg, x 703 // movz $reg, $0, a 704 if (!FalseC) 705 return SDValue(); 706 707 const SDLoc DL(N); 708 709 if (!FalseC->getZExtValue()) { 710 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get(); 711 SDValue True = N->getOperand(1); 712 713 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0), 714 SetCC.getOperand(1), 715 ISD::getSetCCInverse(CC, SetCC.getValueType())); 716 717 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True); 718 } 719 720 // If both operands are integer constants there's a possibility that we 721 // can do some interesting optimizations. 722 SDValue True = N->getOperand(1); 723 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True); 724 725 if (!TrueC || !True.getValueType().isInteger()) 726 return SDValue(); 727 728 // We'll also ignore MVT::i64 operands as this optimizations proves 729 // to be ineffective because of the required sign extensions as the result 730 // of a SETCC operator is always MVT::i32 for non-vector types. 731 if (True.getValueType() == MVT::i64) 732 return SDValue(); 733 734 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue(); 735 736 // 1) (a < x) ? y : y-1 737 // slti $reg1, a, x 738 // addiu $reg2, $reg1, y-1 739 if (Diff == 1) 740 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False); 741 742 // 2) (a < x) ? y-1 : y 743 // slti $reg1, a, x 744 // xor $reg1, $reg1, 1 745 // addiu $reg2, $reg1, y-1 746 if (Diff == -1) { 747 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get(); 748 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0), 749 SetCC.getOperand(1), 750 ISD::getSetCCInverse(CC, SetCC.getValueType())); 751 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True); 752 } 753 754 // Could not optimize. 755 return SDValue(); 756 } 757 758 static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG, 759 TargetLowering::DAGCombinerInfo &DCI, 760 const MipsSubtarget &Subtarget) { 761 if (DCI.isBeforeLegalizeOps()) 762 return SDValue(); 763 764 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2); 765 766 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse); 767 if (!FalseC || FalseC->getZExtValue()) 768 return SDValue(); 769 770 // Since RHS (False) is 0, we swap the order of the True/False operands 771 // (obviously also inverting the condition) so that we can 772 // take advantage of conditional moves using the $0 register. 773 // Example: 774 // return (a != 0) ? x : 0; 775 // load $reg, x 776 // movz $reg, $0, a 777 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F : 778 MipsISD::CMovFP_T; 779 780 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3); 781 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(), 782 ValueIfFalse, FCC, ValueIfTrue, Glue); 783 } 784 785 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG, 786 TargetLowering::DAGCombinerInfo &DCI, 787 const MipsSubtarget &Subtarget) { 788 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert()) 789 return SDValue(); 790 791 SDValue FirstOperand = N->getOperand(0); 792 unsigned FirstOperandOpc = FirstOperand.getOpcode(); 793 SDValue Mask = N->getOperand(1); 794 EVT ValTy = N->getValueType(0); 795 SDLoc DL(N); 796 797 uint64_t Pos = 0, SMPos, SMSize; 798 ConstantSDNode *CN; 799 SDValue NewOperand; 800 unsigned Opc; 801 802 // Op's second operand must be a shifted mask. 803 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) || 804 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize)) 805 return SDValue(); 806 807 if (FirstOperandOpc == ISD::SRA || FirstOperandOpc == ISD::SRL) { 808 // Pattern match EXT. 809 // $dst = and ((sra or srl) $src , pos), (2**size - 1) 810 // => ext $dst, $src, pos, size 811 812 // The second operand of the shift must be an immediate. 813 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1)))) 814 return SDValue(); 815 816 Pos = CN->getZExtValue(); 817 818 // Return if the shifted mask does not start at bit 0 or the sum of its size 819 // and Pos exceeds the word's size. 820 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits()) 821 return SDValue(); 822 823 Opc = MipsISD::Ext; 824 NewOperand = FirstOperand.getOperand(0); 825 } else if (FirstOperandOpc == ISD::SHL && Subtarget.hasCnMips()) { 826 // Pattern match CINS. 827 // $dst = and (shl $src , pos), mask 828 // => cins $dst, $src, pos, size 829 // mask is a shifted mask with consecutive 1's, pos = shift amount, 830 // size = population count. 831 832 // The second operand of the shift must be an immediate. 833 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1)))) 834 return SDValue(); 835 836 Pos = CN->getZExtValue(); 837 838 if (SMPos != Pos || Pos >= ValTy.getSizeInBits() || SMSize >= 32 || 839 Pos + SMSize > ValTy.getSizeInBits()) 840 return SDValue(); 841 842 NewOperand = FirstOperand.getOperand(0); 843 // SMSize is 'location' (position) in this case, not size. 844 SMSize--; 845 Opc = MipsISD::CIns; 846 } else { 847 // Pattern match EXT. 848 // $dst = and $src, (2**size - 1) , if size > 16 849 // => ext $dst, $src, pos, size , pos = 0 850 851 // If the mask is <= 0xffff, andi can be used instead. 852 if (CN->getZExtValue() <= 0xffff) 853 return SDValue(); 854 855 // Return if the mask doesn't start at position 0. 856 if (SMPos) 857 return SDValue(); 858 859 Opc = MipsISD::Ext; 860 NewOperand = FirstOperand; 861 } 862 return DAG.getNode(Opc, DL, ValTy, NewOperand, 863 DAG.getConstant(Pos, DL, MVT::i32), 864 DAG.getConstant(SMSize, DL, MVT::i32)); 865 } 866 867 static SDValue performORCombine(SDNode *N, SelectionDAG &DAG, 868 TargetLowering::DAGCombinerInfo &DCI, 869 const MipsSubtarget &Subtarget) { 870 // Pattern match INS. 871 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1), 872 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1 873 // => ins $dst, $src, size, pos, $src1 874 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert()) 875 return SDValue(); 876 877 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1); 878 uint64_t SMPos0, SMSize0, SMPos1, SMSize1; 879 ConstantSDNode *CN, *CN1; 880 881 // See if Op's first operand matches (and $src1 , mask0). 882 if (And0.getOpcode() != ISD::AND) 883 return SDValue(); 884 885 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) || 886 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0)) 887 return SDValue(); 888 889 // See if Op's second operand matches (and (shl $src, pos), mask1). 890 if (And1.getOpcode() == ISD::AND && 891 And1.getOperand(0).getOpcode() == ISD::SHL) { 892 893 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) || 894 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1)) 895 return SDValue(); 896 897 // The shift masks must have the same position and size. 898 if (SMPos0 != SMPos1 || SMSize0 != SMSize1) 899 return SDValue(); 900 901 SDValue Shl = And1.getOperand(0); 902 903 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1)))) 904 return SDValue(); 905 906 unsigned Shamt = CN->getZExtValue(); 907 908 // Return if the shift amount and the first bit position of mask are not the 909 // same. 910 EVT ValTy = N->getValueType(0); 911 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits())) 912 return SDValue(); 913 914 SDLoc DL(N); 915 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0), 916 DAG.getConstant(SMPos0, DL, MVT::i32), 917 DAG.getConstant(SMSize0, DL, MVT::i32), 918 And0.getOperand(0)); 919 } else { 920 // Pattern match DINS. 921 // $dst = or (and $src, mask0), mask1 922 // where mask0 = ((1 << SMSize0) -1) << SMPos0 923 // => dins $dst, $src, pos, size 924 if (~CN->getSExtValue() == ((((int64_t)1 << SMSize0) - 1) << SMPos0) && 925 ((SMSize0 + SMPos0 <= 64 && Subtarget.hasMips64r2()) || 926 (SMSize0 + SMPos0 <= 32))) { 927 // Check if AND instruction has constant as argument 928 bool isConstCase = And1.getOpcode() != ISD::AND; 929 if (And1.getOpcode() == ISD::AND) { 930 if (!(CN1 = dyn_cast<ConstantSDNode>(And1->getOperand(1)))) 931 return SDValue(); 932 } else { 933 if (!(CN1 = dyn_cast<ConstantSDNode>(N->getOperand(1)))) 934 return SDValue(); 935 } 936 // Don't generate INS if constant OR operand doesn't fit into bits 937 // cleared by constant AND operand. 938 if (CN->getSExtValue() & CN1->getSExtValue()) 939 return SDValue(); 940 941 SDLoc DL(N); 942 EVT ValTy = N->getOperand(0)->getValueType(0); 943 SDValue Const1; 944 SDValue SrlX; 945 if (!isConstCase) { 946 Const1 = DAG.getConstant(SMPos0, DL, MVT::i32); 947 SrlX = DAG.getNode(ISD::SRL, DL, And1->getValueType(0), And1, Const1); 948 } 949 return DAG.getNode( 950 MipsISD::Ins, DL, N->getValueType(0), 951 isConstCase 952 ? DAG.getConstant(CN1->getSExtValue() >> SMPos0, DL, ValTy) 953 : SrlX, 954 DAG.getConstant(SMPos0, DL, MVT::i32), 955 DAG.getConstant(ValTy.getSizeInBits() / 8 < 8 ? SMSize0 & 31 956 : SMSize0, 957 DL, MVT::i32), 958 And0->getOperand(0)); 959 960 } 961 return SDValue(); 962 } 963 } 964 965 static SDValue performMADD_MSUBCombine(SDNode *ROOTNode, SelectionDAG &CurDAG, 966 const MipsSubtarget &Subtarget) { 967 // ROOTNode must have a multiplication as an operand for the match to be 968 // successful. 969 if (ROOTNode->getOperand(0).getOpcode() != ISD::MUL && 970 ROOTNode->getOperand(1).getOpcode() != ISD::MUL) 971 return SDValue(); 972 973 // We don't handle vector types here. 974 if (ROOTNode->getValueType(0).isVector()) 975 return SDValue(); 976 977 // For MIPS64, madd / msub instructions are inefficent to use with 64 bit 978 // arithmetic. E.g. 979 // (add (mul a b) c) => 980 // let res = (madd (mthi (drotr c 32))x(mtlo c) a b) in 981 // MIPS64: (or (dsll (mfhi res) 32) (dsrl (dsll (mflo res) 32) 32) 982 // or 983 // MIPS64R2: (dins (mflo res) (mfhi res) 32 32) 984 // 985 // The overhead of setting up the Hi/Lo registers and reassembling the 986 // result makes this a dubious optimzation for MIPS64. The core of the 987 // problem is that Hi/Lo contain the upper and lower 32 bits of the 988 // operand and result. 989 // 990 // It requires a chain of 4 add/mul for MIPS64R2 to get better code 991 // density than doing it naively, 5 for MIPS64. Additionally, using 992 // madd/msub on MIPS64 requires the operands actually be 32 bit sign 993 // extended operands, not true 64 bit values. 994 // 995 // FIXME: For the moment, disable this completely for MIPS64. 996 if (Subtarget.hasMips64()) 997 return SDValue(); 998 999 SDValue Mult = ROOTNode->getOperand(0).getOpcode() == ISD::MUL 1000 ? ROOTNode->getOperand(0) 1001 : ROOTNode->getOperand(1); 1002 1003 SDValue AddOperand = ROOTNode->getOperand(0).getOpcode() == ISD::MUL 1004 ? ROOTNode->getOperand(1) 1005 : ROOTNode->getOperand(0); 1006 1007 // Transform this to a MADD only if the user of this node is the add. 1008 // If there are other users of the mul, this function returns here. 1009 if (!Mult.hasOneUse()) 1010 return SDValue(); 1011 1012 // maddu and madd are unusual instructions in that on MIPS64 bits 63..31 1013 // must be in canonical form, i.e. sign extended. For MIPS32, the operands 1014 // of the multiply must have 32 or more sign bits, otherwise we cannot 1015 // perform this optimization. We have to check this here as we're performing 1016 // this optimization pre-legalization. 1017 SDValue MultLHS = Mult->getOperand(0); 1018 SDValue MultRHS = Mult->getOperand(1); 1019 1020 bool IsSigned = MultLHS->getOpcode() == ISD::SIGN_EXTEND && 1021 MultRHS->getOpcode() == ISD::SIGN_EXTEND; 1022 bool IsUnsigned = MultLHS->getOpcode() == ISD::ZERO_EXTEND && 1023 MultRHS->getOpcode() == ISD::ZERO_EXTEND; 1024 1025 if (!IsSigned && !IsUnsigned) 1026 return SDValue(); 1027 1028 // Initialize accumulator. 1029 SDLoc DL(ROOTNode); 1030 SDValue TopHalf; 1031 SDValue BottomHalf; 1032 BottomHalf = CurDAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, AddOperand, 1033 CurDAG.getIntPtrConstant(0, DL)); 1034 1035 TopHalf = CurDAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, AddOperand, 1036 CurDAG.getIntPtrConstant(1, DL)); 1037 SDValue ACCIn = CurDAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, 1038 BottomHalf, 1039 TopHalf); 1040 1041 // Create MipsMAdd(u) / MipsMSub(u) node. 1042 bool IsAdd = ROOTNode->getOpcode() == ISD::ADD; 1043 unsigned Opcode = IsAdd ? (IsUnsigned ? MipsISD::MAddu : MipsISD::MAdd) 1044 : (IsUnsigned ? MipsISD::MSubu : MipsISD::MSub); 1045 SDValue MAddOps[3] = { 1046 CurDAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mult->getOperand(0)), 1047 CurDAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mult->getOperand(1)), ACCIn}; 1048 EVT VTs[2] = {MVT::i32, MVT::i32}; 1049 SDValue MAdd = CurDAG.getNode(Opcode, DL, VTs, MAddOps); 1050 1051 SDValue ResLo = CurDAG.getNode(MipsISD::MFLO, DL, MVT::i32, MAdd); 1052 SDValue ResHi = CurDAG.getNode(MipsISD::MFHI, DL, MVT::i32, MAdd); 1053 SDValue Combined = 1054 CurDAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, ResLo, ResHi); 1055 return Combined; 1056 } 1057 1058 static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG, 1059 TargetLowering::DAGCombinerInfo &DCI, 1060 const MipsSubtarget &Subtarget) { 1061 // (sub v0 (mul v1, v2)) => (msub v1, v2, v0) 1062 if (DCI.isBeforeLegalizeOps()) { 1063 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() && 1064 !Subtarget.inMips16Mode() && N->getValueType(0) == MVT::i64) 1065 return performMADD_MSUBCombine(N, DAG, Subtarget); 1066 1067 return SDValue(); 1068 } 1069 1070 return SDValue(); 1071 } 1072 1073 static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG, 1074 TargetLowering::DAGCombinerInfo &DCI, 1075 const MipsSubtarget &Subtarget) { 1076 // (add v0 (mul v1, v2)) => (madd v1, v2, v0) 1077 if (DCI.isBeforeLegalizeOps()) { 1078 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() && 1079 !Subtarget.inMips16Mode() && N->getValueType(0) == MVT::i64) 1080 return performMADD_MSUBCombine(N, DAG, Subtarget); 1081 1082 return SDValue(); 1083 } 1084 1085 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt)) 1086 SDValue Add = N->getOperand(1); 1087 1088 if (Add.getOpcode() != ISD::ADD) 1089 return SDValue(); 1090 1091 SDValue Lo = Add.getOperand(1); 1092 1093 if ((Lo.getOpcode() != MipsISD::Lo) || 1094 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable)) 1095 return SDValue(); 1096 1097 EVT ValTy = N->getValueType(0); 1098 SDLoc DL(N); 1099 1100 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0), 1101 Add.getOperand(0)); 1102 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo); 1103 } 1104 1105 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG, 1106 TargetLowering::DAGCombinerInfo &DCI, 1107 const MipsSubtarget &Subtarget) { 1108 // Pattern match CINS. 1109 // $dst = shl (and $src , imm), pos 1110 // => cins $dst, $src, pos, size 1111 1112 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasCnMips()) 1113 return SDValue(); 1114 1115 SDValue FirstOperand = N->getOperand(0); 1116 unsigned FirstOperandOpc = FirstOperand.getOpcode(); 1117 SDValue SecondOperand = N->getOperand(1); 1118 EVT ValTy = N->getValueType(0); 1119 SDLoc DL(N); 1120 1121 uint64_t Pos = 0, SMPos, SMSize; 1122 ConstantSDNode *CN; 1123 SDValue NewOperand; 1124 1125 // The second operand of the shift must be an immediate. 1126 if (!(CN = dyn_cast<ConstantSDNode>(SecondOperand))) 1127 return SDValue(); 1128 1129 Pos = CN->getZExtValue(); 1130 1131 if (Pos >= ValTy.getSizeInBits()) 1132 return SDValue(); 1133 1134 if (FirstOperandOpc != ISD::AND) 1135 return SDValue(); 1136 1137 // AND's second operand must be a shifted mask. 1138 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))) || 1139 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize)) 1140 return SDValue(); 1141 1142 // Return if the shifted mask does not start at bit 0 or the sum of its size 1143 // and Pos exceeds the word's size. 1144 if (SMPos != 0 || SMSize > 32 || Pos + SMSize > ValTy.getSizeInBits()) 1145 return SDValue(); 1146 1147 NewOperand = FirstOperand.getOperand(0); 1148 // SMSize is 'location' (position) in this case, not size. 1149 SMSize--; 1150 1151 return DAG.getNode(MipsISD::CIns, DL, ValTy, NewOperand, 1152 DAG.getConstant(Pos, DL, MVT::i32), 1153 DAG.getConstant(SMSize, DL, MVT::i32)); 1154 } 1155 1156 SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) 1157 const { 1158 SelectionDAG &DAG = DCI.DAG; 1159 unsigned Opc = N->getOpcode(); 1160 1161 switch (Opc) { 1162 default: break; 1163 case ISD::SDIVREM: 1164 case ISD::UDIVREM: 1165 return performDivRemCombine(N, DAG, DCI, Subtarget); 1166 case ISD::SELECT: 1167 return performSELECTCombine(N, DAG, DCI, Subtarget); 1168 case MipsISD::CMovFP_F: 1169 case MipsISD::CMovFP_T: 1170 return performCMovFPCombine(N, DAG, DCI, Subtarget); 1171 case ISD::AND: 1172 return performANDCombine(N, DAG, DCI, Subtarget); 1173 case ISD::OR: 1174 return performORCombine(N, DAG, DCI, Subtarget); 1175 case ISD::ADD: 1176 return performADDCombine(N, DAG, DCI, Subtarget); 1177 case ISD::SHL: 1178 return performSHLCombine(N, DAG, DCI, Subtarget); 1179 case ISD::SUB: 1180 return performSUBCombine(N, DAG, DCI, Subtarget); 1181 } 1182 1183 return SDValue(); 1184 } 1185 1186 bool MipsTargetLowering::isCheapToSpeculateCttz() const { 1187 return Subtarget.hasMips32(); 1188 } 1189 1190 bool MipsTargetLowering::isCheapToSpeculateCtlz() const { 1191 return Subtarget.hasMips32(); 1192 } 1193 1194 bool MipsTargetLowering::shouldFoldConstantShiftPairToMask( 1195 const SDNode *N, CombineLevel Level) const { 1196 if (N->getOperand(0).getValueType().isVector()) 1197 return false; 1198 return true; 1199 } 1200 1201 void 1202 MipsTargetLowering::ReplaceNodeResults(SDNode *N, 1203 SmallVectorImpl<SDValue> &Results, 1204 SelectionDAG &DAG) const { 1205 return LowerOperationWrapper(N, Results, DAG); 1206 } 1207 1208 SDValue MipsTargetLowering:: 1209 LowerOperation(SDValue Op, SelectionDAG &DAG) const 1210 { 1211 switch (Op.getOpcode()) 1212 { 1213 case ISD::BRCOND: return lowerBRCOND(Op, DAG); 1214 case ISD::ConstantPool: return lowerConstantPool(Op, DAG); 1215 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG); 1216 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG); 1217 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG); 1218 case ISD::JumpTable: return lowerJumpTable(Op, DAG); 1219 case ISD::SELECT: return lowerSELECT(Op, DAG); 1220 case ISD::SETCC: return lowerSETCC(Op, DAG); 1221 case ISD::VASTART: return lowerVASTART(Op, DAG); 1222 case ISD::VAARG: return lowerVAARG(Op, DAG); 1223 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG); 1224 case ISD::FABS: return lowerFABS(Op, DAG); 1225 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG); 1226 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG); 1227 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG); 1228 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG); 1229 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG); 1230 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true); 1231 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false); 1232 case ISD::LOAD: return lowerLOAD(Op, DAG); 1233 case ISD::STORE: return lowerSTORE(Op, DAG); 1234 case ISD::EH_DWARF_CFA: return lowerEH_DWARF_CFA(Op, DAG); 1235 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG); 1236 } 1237 return SDValue(); 1238 } 1239 1240 //===----------------------------------------------------------------------===// 1241 // Lower helper functions 1242 //===----------------------------------------------------------------------===// 1243 1244 // addLiveIn - This helper function adds the specified physical register to the 1245 // MachineFunction as a live in value. It also creates a corresponding 1246 // virtual register for it. 1247 static unsigned 1248 addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC) 1249 { 1250 Register VReg = MF.getRegInfo().createVirtualRegister(RC); 1251 MF.getRegInfo().addLiveIn(PReg, VReg); 1252 return VReg; 1253 } 1254 1255 static MachineBasicBlock *insertDivByZeroTrap(MachineInstr &MI, 1256 MachineBasicBlock &MBB, 1257 const TargetInstrInfo &TII, 1258 bool Is64Bit, bool IsMicroMips) { 1259 if (NoZeroDivCheck) 1260 return &MBB; 1261 1262 // Insert instruction "teq $divisor_reg, $zero, 7". 1263 MachineBasicBlock::iterator I(MI); 1264 MachineInstrBuilder MIB; 1265 MachineOperand &Divisor = MI.getOperand(2); 1266 MIB = BuildMI(MBB, std::next(I), MI.getDebugLoc(), 1267 TII.get(IsMicroMips ? Mips::TEQ_MM : Mips::TEQ)) 1268 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill())) 1269 .addReg(Mips::ZERO) 1270 .addImm(7); 1271 1272 // Use the 32-bit sub-register if this is a 64-bit division. 1273 if (Is64Bit) 1274 MIB->getOperand(0).setSubReg(Mips::sub_32); 1275 1276 // Clear Divisor's kill flag. 1277 Divisor.setIsKill(false); 1278 1279 // We would normally delete the original instruction here but in this case 1280 // we only needed to inject an additional instruction rather than replace it. 1281 1282 return &MBB; 1283 } 1284 1285 MachineBasicBlock * 1286 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, 1287 MachineBasicBlock *BB) const { 1288 switch (MI.getOpcode()) { 1289 default: 1290 llvm_unreachable("Unexpected instr type to insert"); 1291 case Mips::ATOMIC_LOAD_ADD_I8: 1292 return emitAtomicBinaryPartword(MI, BB, 1); 1293 case Mips::ATOMIC_LOAD_ADD_I16: 1294 return emitAtomicBinaryPartword(MI, BB, 2); 1295 case Mips::ATOMIC_LOAD_ADD_I32: 1296 return emitAtomicBinary(MI, BB); 1297 case Mips::ATOMIC_LOAD_ADD_I64: 1298 return emitAtomicBinary(MI, BB); 1299 1300 case Mips::ATOMIC_LOAD_AND_I8: 1301 return emitAtomicBinaryPartword(MI, BB, 1); 1302 case Mips::ATOMIC_LOAD_AND_I16: 1303 return emitAtomicBinaryPartword(MI, BB, 2); 1304 case Mips::ATOMIC_LOAD_AND_I32: 1305 return emitAtomicBinary(MI, BB); 1306 case Mips::ATOMIC_LOAD_AND_I64: 1307 return emitAtomicBinary(MI, BB); 1308 1309 case Mips::ATOMIC_LOAD_OR_I8: 1310 return emitAtomicBinaryPartword(MI, BB, 1); 1311 case Mips::ATOMIC_LOAD_OR_I16: 1312 return emitAtomicBinaryPartword(MI, BB, 2); 1313 case Mips::ATOMIC_LOAD_OR_I32: 1314 return emitAtomicBinary(MI, BB); 1315 case Mips::ATOMIC_LOAD_OR_I64: 1316 return emitAtomicBinary(MI, BB); 1317 1318 case Mips::ATOMIC_LOAD_XOR_I8: 1319 return emitAtomicBinaryPartword(MI, BB, 1); 1320 case Mips::ATOMIC_LOAD_XOR_I16: 1321 return emitAtomicBinaryPartword(MI, BB, 2); 1322 case Mips::ATOMIC_LOAD_XOR_I32: 1323 return emitAtomicBinary(MI, BB); 1324 case Mips::ATOMIC_LOAD_XOR_I64: 1325 return emitAtomicBinary(MI, BB); 1326 1327 case Mips::ATOMIC_LOAD_NAND_I8: 1328 return emitAtomicBinaryPartword(MI, BB, 1); 1329 case Mips::ATOMIC_LOAD_NAND_I16: 1330 return emitAtomicBinaryPartword(MI, BB, 2); 1331 case Mips::ATOMIC_LOAD_NAND_I32: 1332 return emitAtomicBinary(MI, BB); 1333 case Mips::ATOMIC_LOAD_NAND_I64: 1334 return emitAtomicBinary(MI, BB); 1335 1336 case Mips::ATOMIC_LOAD_SUB_I8: 1337 return emitAtomicBinaryPartword(MI, BB, 1); 1338 case Mips::ATOMIC_LOAD_SUB_I16: 1339 return emitAtomicBinaryPartword(MI, BB, 2); 1340 case Mips::ATOMIC_LOAD_SUB_I32: 1341 return emitAtomicBinary(MI, BB); 1342 case Mips::ATOMIC_LOAD_SUB_I64: 1343 return emitAtomicBinary(MI, BB); 1344 1345 case Mips::ATOMIC_SWAP_I8: 1346 return emitAtomicBinaryPartword(MI, BB, 1); 1347 case Mips::ATOMIC_SWAP_I16: 1348 return emitAtomicBinaryPartword(MI, BB, 2); 1349 case Mips::ATOMIC_SWAP_I32: 1350 return emitAtomicBinary(MI, BB); 1351 case Mips::ATOMIC_SWAP_I64: 1352 return emitAtomicBinary(MI, BB); 1353 1354 case Mips::ATOMIC_CMP_SWAP_I8: 1355 return emitAtomicCmpSwapPartword(MI, BB, 1); 1356 case Mips::ATOMIC_CMP_SWAP_I16: 1357 return emitAtomicCmpSwapPartword(MI, BB, 2); 1358 case Mips::ATOMIC_CMP_SWAP_I32: 1359 return emitAtomicCmpSwap(MI, BB); 1360 case Mips::ATOMIC_CMP_SWAP_I64: 1361 return emitAtomicCmpSwap(MI, BB); 1362 1363 case Mips::ATOMIC_LOAD_MIN_I8: 1364 return emitAtomicBinaryPartword(MI, BB, 1); 1365 case Mips::ATOMIC_LOAD_MIN_I16: 1366 return emitAtomicBinaryPartword(MI, BB, 2); 1367 case Mips::ATOMIC_LOAD_MIN_I32: 1368 return emitAtomicBinary(MI, BB); 1369 case Mips::ATOMIC_LOAD_MIN_I64: 1370 return emitAtomicBinary(MI, BB); 1371 1372 case Mips::ATOMIC_LOAD_MAX_I8: 1373 return emitAtomicBinaryPartword(MI, BB, 1); 1374 case Mips::ATOMIC_LOAD_MAX_I16: 1375 return emitAtomicBinaryPartword(MI, BB, 2); 1376 case Mips::ATOMIC_LOAD_MAX_I32: 1377 return emitAtomicBinary(MI, BB); 1378 case Mips::ATOMIC_LOAD_MAX_I64: 1379 return emitAtomicBinary(MI, BB); 1380 1381 case Mips::ATOMIC_LOAD_UMIN_I8: 1382 return emitAtomicBinaryPartword(MI, BB, 1); 1383 case Mips::ATOMIC_LOAD_UMIN_I16: 1384 return emitAtomicBinaryPartword(MI, BB, 2); 1385 case Mips::ATOMIC_LOAD_UMIN_I32: 1386 return emitAtomicBinary(MI, BB); 1387 case Mips::ATOMIC_LOAD_UMIN_I64: 1388 return emitAtomicBinary(MI, BB); 1389 1390 case Mips::ATOMIC_LOAD_UMAX_I8: 1391 return emitAtomicBinaryPartword(MI, BB, 1); 1392 case Mips::ATOMIC_LOAD_UMAX_I16: 1393 return emitAtomicBinaryPartword(MI, BB, 2); 1394 case Mips::ATOMIC_LOAD_UMAX_I32: 1395 return emitAtomicBinary(MI, BB); 1396 case Mips::ATOMIC_LOAD_UMAX_I64: 1397 return emitAtomicBinary(MI, BB); 1398 1399 case Mips::PseudoSDIV: 1400 case Mips::PseudoUDIV: 1401 case Mips::DIV: 1402 case Mips::DIVU: 1403 case Mips::MOD: 1404 case Mips::MODU: 1405 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false, 1406 false); 1407 case Mips::SDIV_MM_Pseudo: 1408 case Mips::UDIV_MM_Pseudo: 1409 case Mips::SDIV_MM: 1410 case Mips::UDIV_MM: 1411 case Mips::DIV_MMR6: 1412 case Mips::DIVU_MMR6: 1413 case Mips::MOD_MMR6: 1414 case Mips::MODU_MMR6: 1415 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false, true); 1416 case Mips::PseudoDSDIV: 1417 case Mips::PseudoDUDIV: 1418 case Mips::DDIV: 1419 case Mips::DDIVU: 1420 case Mips::DMOD: 1421 case Mips::DMODU: 1422 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, false); 1423 1424 case Mips::PseudoSELECT_I: 1425 case Mips::PseudoSELECT_I64: 1426 case Mips::PseudoSELECT_S: 1427 case Mips::PseudoSELECT_D32: 1428 case Mips::PseudoSELECT_D64: 1429 return emitPseudoSELECT(MI, BB, false, Mips::BNE); 1430 case Mips::PseudoSELECTFP_F_I: 1431 case Mips::PseudoSELECTFP_F_I64: 1432 case Mips::PseudoSELECTFP_F_S: 1433 case Mips::PseudoSELECTFP_F_D32: 1434 case Mips::PseudoSELECTFP_F_D64: 1435 return emitPseudoSELECT(MI, BB, true, Mips::BC1F); 1436 case Mips::PseudoSELECTFP_T_I: 1437 case Mips::PseudoSELECTFP_T_I64: 1438 case Mips::PseudoSELECTFP_T_S: 1439 case Mips::PseudoSELECTFP_T_D32: 1440 case Mips::PseudoSELECTFP_T_D64: 1441 return emitPseudoSELECT(MI, BB, true, Mips::BC1T); 1442 case Mips::PseudoD_SELECT_I: 1443 case Mips::PseudoD_SELECT_I64: 1444 return emitPseudoD_SELECT(MI, BB); 1445 case Mips::LDR_W: 1446 return emitLDR_W(MI, BB); 1447 case Mips::LDR_D: 1448 return emitLDR_D(MI, BB); 1449 case Mips::STR_W: 1450 return emitSTR_W(MI, BB); 1451 case Mips::STR_D: 1452 return emitSTR_D(MI, BB); 1453 } 1454 } 1455 1456 // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and 1457 // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true) 1458 MachineBasicBlock * 1459 MipsTargetLowering::emitAtomicBinary(MachineInstr &MI, 1460 MachineBasicBlock *BB) const { 1461 1462 MachineFunction *MF = BB->getParent(); 1463 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 1464 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 1465 DebugLoc DL = MI.getDebugLoc(); 1466 1467 unsigned AtomicOp; 1468 bool NeedsAdditionalReg = false; 1469 switch (MI.getOpcode()) { 1470 case Mips::ATOMIC_LOAD_ADD_I32: 1471 AtomicOp = Mips::ATOMIC_LOAD_ADD_I32_POSTRA; 1472 break; 1473 case Mips::ATOMIC_LOAD_SUB_I32: 1474 AtomicOp = Mips::ATOMIC_LOAD_SUB_I32_POSTRA; 1475 break; 1476 case Mips::ATOMIC_LOAD_AND_I32: 1477 AtomicOp = Mips::ATOMIC_LOAD_AND_I32_POSTRA; 1478 break; 1479 case Mips::ATOMIC_LOAD_OR_I32: 1480 AtomicOp = Mips::ATOMIC_LOAD_OR_I32_POSTRA; 1481 break; 1482 case Mips::ATOMIC_LOAD_XOR_I32: 1483 AtomicOp = Mips::ATOMIC_LOAD_XOR_I32_POSTRA; 1484 break; 1485 case Mips::ATOMIC_LOAD_NAND_I32: 1486 AtomicOp = Mips::ATOMIC_LOAD_NAND_I32_POSTRA; 1487 break; 1488 case Mips::ATOMIC_SWAP_I32: 1489 AtomicOp = Mips::ATOMIC_SWAP_I32_POSTRA; 1490 break; 1491 case Mips::ATOMIC_LOAD_ADD_I64: 1492 AtomicOp = Mips::ATOMIC_LOAD_ADD_I64_POSTRA; 1493 break; 1494 case Mips::ATOMIC_LOAD_SUB_I64: 1495 AtomicOp = Mips::ATOMIC_LOAD_SUB_I64_POSTRA; 1496 break; 1497 case Mips::ATOMIC_LOAD_AND_I64: 1498 AtomicOp = Mips::ATOMIC_LOAD_AND_I64_POSTRA; 1499 break; 1500 case Mips::ATOMIC_LOAD_OR_I64: 1501 AtomicOp = Mips::ATOMIC_LOAD_OR_I64_POSTRA; 1502 break; 1503 case Mips::ATOMIC_LOAD_XOR_I64: 1504 AtomicOp = Mips::ATOMIC_LOAD_XOR_I64_POSTRA; 1505 break; 1506 case Mips::ATOMIC_LOAD_NAND_I64: 1507 AtomicOp = Mips::ATOMIC_LOAD_NAND_I64_POSTRA; 1508 break; 1509 case Mips::ATOMIC_SWAP_I64: 1510 AtomicOp = Mips::ATOMIC_SWAP_I64_POSTRA; 1511 break; 1512 case Mips::ATOMIC_LOAD_MIN_I32: 1513 AtomicOp = Mips::ATOMIC_LOAD_MIN_I32_POSTRA; 1514 NeedsAdditionalReg = true; 1515 break; 1516 case Mips::ATOMIC_LOAD_MAX_I32: 1517 AtomicOp = Mips::ATOMIC_LOAD_MAX_I32_POSTRA; 1518 NeedsAdditionalReg = true; 1519 break; 1520 case Mips::ATOMIC_LOAD_UMIN_I32: 1521 AtomicOp = Mips::ATOMIC_LOAD_UMIN_I32_POSTRA; 1522 NeedsAdditionalReg = true; 1523 break; 1524 case Mips::ATOMIC_LOAD_UMAX_I32: 1525 AtomicOp = Mips::ATOMIC_LOAD_UMAX_I32_POSTRA; 1526 NeedsAdditionalReg = true; 1527 break; 1528 case Mips::ATOMIC_LOAD_MIN_I64: 1529 AtomicOp = Mips::ATOMIC_LOAD_MIN_I64_POSTRA; 1530 NeedsAdditionalReg = true; 1531 break; 1532 case Mips::ATOMIC_LOAD_MAX_I64: 1533 AtomicOp = Mips::ATOMIC_LOAD_MAX_I64_POSTRA; 1534 NeedsAdditionalReg = true; 1535 break; 1536 case Mips::ATOMIC_LOAD_UMIN_I64: 1537 AtomicOp = Mips::ATOMIC_LOAD_UMIN_I64_POSTRA; 1538 NeedsAdditionalReg = true; 1539 break; 1540 case Mips::ATOMIC_LOAD_UMAX_I64: 1541 AtomicOp = Mips::ATOMIC_LOAD_UMAX_I64_POSTRA; 1542 NeedsAdditionalReg = true; 1543 break; 1544 default: 1545 llvm_unreachable("Unknown pseudo atomic for replacement!"); 1546 } 1547 1548 Register OldVal = MI.getOperand(0).getReg(); 1549 Register Ptr = MI.getOperand(1).getReg(); 1550 Register Incr = MI.getOperand(2).getReg(); 1551 Register Scratch = RegInfo.createVirtualRegister(RegInfo.getRegClass(OldVal)); 1552 1553 MachineBasicBlock::iterator II(MI); 1554 1555 // The scratch registers here with the EarlyClobber | Define | Implicit 1556 // flags is used to persuade the register allocator and the machine 1557 // verifier to accept the usage of this register. This has to be a real 1558 // register which has an UNDEF value but is dead after the instruction which 1559 // is unique among the registers chosen for the instruction. 1560 1561 // The EarlyClobber flag has the semantic properties that the operand it is 1562 // attached to is clobbered before the rest of the inputs are read. Hence it 1563 // must be unique among the operands to the instruction. 1564 // The Define flag is needed to coerce the machine verifier that an Undef 1565 // value isn't a problem. 1566 // The Dead flag is needed as the value in scratch isn't used by any other 1567 // instruction. Kill isn't used as Dead is more precise. 1568 // The implicit flag is here due to the interaction between the other flags 1569 // and the machine verifier. 1570 1571 // For correctness purpose, a new pseudo is introduced here. We need this 1572 // new pseudo, so that FastRegisterAllocator does not see an ll/sc sequence 1573 // that is spread over >1 basic blocks. A register allocator which 1574 // introduces (or any codegen infact) a store, can violate the expectations 1575 // of the hardware. 1576 // 1577 // An atomic read-modify-write sequence starts with a linked load 1578 // instruction and ends with a store conditional instruction. The atomic 1579 // read-modify-write sequence fails if any of the following conditions 1580 // occur between the execution of ll and sc: 1581 // * A coherent store is completed by another process or coherent I/O 1582 // module into the block of synchronizable physical memory containing 1583 // the word. The size and alignment of the block is 1584 // implementation-dependent. 1585 // * A coherent store is executed between an LL and SC sequence on the 1586 // same processor to the block of synchornizable physical memory 1587 // containing the word. 1588 // 1589 1590 Register PtrCopy = RegInfo.createVirtualRegister(RegInfo.getRegClass(Ptr)); 1591 Register IncrCopy = RegInfo.createVirtualRegister(RegInfo.getRegClass(Incr)); 1592 1593 BuildMI(*BB, II, DL, TII->get(Mips::COPY), IncrCopy).addReg(Incr); 1594 BuildMI(*BB, II, DL, TII->get(Mips::COPY), PtrCopy).addReg(Ptr); 1595 1596 MachineInstrBuilder MIB = 1597 BuildMI(*BB, II, DL, TII->get(AtomicOp)) 1598 .addReg(OldVal, RegState::Define | RegState::EarlyClobber) 1599 .addReg(PtrCopy) 1600 .addReg(IncrCopy) 1601 .addReg(Scratch, RegState::Define | RegState::EarlyClobber | 1602 RegState::Implicit | RegState::Dead); 1603 if (NeedsAdditionalReg) { 1604 Register Scratch2 = 1605 RegInfo.createVirtualRegister(RegInfo.getRegClass(OldVal)); 1606 MIB.addReg(Scratch2, RegState::Define | RegState::EarlyClobber | 1607 RegState::Implicit | RegState::Dead); 1608 } 1609 1610 MI.eraseFromParent(); 1611 1612 return BB; 1613 } 1614 1615 MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg( 1616 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg, 1617 unsigned SrcReg) const { 1618 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 1619 const DebugLoc &DL = MI.getDebugLoc(); 1620 1621 if (Subtarget.hasMips32r2() && Size == 1) { 1622 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg); 1623 return BB; 1624 } 1625 1626 if (Subtarget.hasMips32r2() && Size == 2) { 1627 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg); 1628 return BB; 1629 } 1630 1631 MachineFunction *MF = BB->getParent(); 1632 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 1633 const TargetRegisterClass *RC = getRegClassFor(MVT::i32); 1634 Register ScrReg = RegInfo.createVirtualRegister(RC); 1635 1636 assert(Size < 32); 1637 int64_t ShiftImm = 32 - (Size * 8); 1638 1639 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm); 1640 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm); 1641 1642 return BB; 1643 } 1644 1645 MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword( 1646 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const { 1647 assert((Size == 1 || Size == 2) && 1648 "Unsupported size for EmitAtomicBinaryPartial."); 1649 1650 MachineFunction *MF = BB->getParent(); 1651 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 1652 const TargetRegisterClass *RC = getRegClassFor(MVT::i32); 1653 const bool ArePtrs64bit = ABI.ArePtrs64bit(); 1654 const TargetRegisterClass *RCp = 1655 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32); 1656 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 1657 DebugLoc DL = MI.getDebugLoc(); 1658 1659 Register Dest = MI.getOperand(0).getReg(); 1660 Register Ptr = MI.getOperand(1).getReg(); 1661 Register Incr = MI.getOperand(2).getReg(); 1662 1663 Register AlignedAddr = RegInfo.createVirtualRegister(RCp); 1664 Register ShiftAmt = RegInfo.createVirtualRegister(RC); 1665 Register Mask = RegInfo.createVirtualRegister(RC); 1666 Register Mask2 = RegInfo.createVirtualRegister(RC); 1667 Register Incr2 = RegInfo.createVirtualRegister(RC); 1668 Register MaskLSB2 = RegInfo.createVirtualRegister(RCp); 1669 Register PtrLSB2 = RegInfo.createVirtualRegister(RC); 1670 Register MaskUpper = RegInfo.createVirtualRegister(RC); 1671 Register Scratch = RegInfo.createVirtualRegister(RC); 1672 Register Scratch2 = RegInfo.createVirtualRegister(RC); 1673 Register Scratch3 = RegInfo.createVirtualRegister(RC); 1674 1675 unsigned AtomicOp = 0; 1676 bool NeedsAdditionalReg = false; 1677 switch (MI.getOpcode()) { 1678 case Mips::ATOMIC_LOAD_NAND_I8: 1679 AtomicOp = Mips::ATOMIC_LOAD_NAND_I8_POSTRA; 1680 break; 1681 case Mips::ATOMIC_LOAD_NAND_I16: 1682 AtomicOp = Mips::ATOMIC_LOAD_NAND_I16_POSTRA; 1683 break; 1684 case Mips::ATOMIC_SWAP_I8: 1685 AtomicOp = Mips::ATOMIC_SWAP_I8_POSTRA; 1686 break; 1687 case Mips::ATOMIC_SWAP_I16: 1688 AtomicOp = Mips::ATOMIC_SWAP_I16_POSTRA; 1689 break; 1690 case Mips::ATOMIC_LOAD_ADD_I8: 1691 AtomicOp = Mips::ATOMIC_LOAD_ADD_I8_POSTRA; 1692 break; 1693 case Mips::ATOMIC_LOAD_ADD_I16: 1694 AtomicOp = Mips::ATOMIC_LOAD_ADD_I16_POSTRA; 1695 break; 1696 case Mips::ATOMIC_LOAD_SUB_I8: 1697 AtomicOp = Mips::ATOMIC_LOAD_SUB_I8_POSTRA; 1698 break; 1699 case Mips::ATOMIC_LOAD_SUB_I16: 1700 AtomicOp = Mips::ATOMIC_LOAD_SUB_I16_POSTRA; 1701 break; 1702 case Mips::ATOMIC_LOAD_AND_I8: 1703 AtomicOp = Mips::ATOMIC_LOAD_AND_I8_POSTRA; 1704 break; 1705 case Mips::ATOMIC_LOAD_AND_I16: 1706 AtomicOp = Mips::ATOMIC_LOAD_AND_I16_POSTRA; 1707 break; 1708 case Mips::ATOMIC_LOAD_OR_I8: 1709 AtomicOp = Mips::ATOMIC_LOAD_OR_I8_POSTRA; 1710 break; 1711 case Mips::ATOMIC_LOAD_OR_I16: 1712 AtomicOp = Mips::ATOMIC_LOAD_OR_I16_POSTRA; 1713 break; 1714 case Mips::ATOMIC_LOAD_XOR_I8: 1715 AtomicOp = Mips::ATOMIC_LOAD_XOR_I8_POSTRA; 1716 break; 1717 case Mips::ATOMIC_LOAD_XOR_I16: 1718 AtomicOp = Mips::ATOMIC_LOAD_XOR_I16_POSTRA; 1719 break; 1720 case Mips::ATOMIC_LOAD_MIN_I8: 1721 AtomicOp = Mips::ATOMIC_LOAD_MIN_I8_POSTRA; 1722 NeedsAdditionalReg = true; 1723 break; 1724 case Mips::ATOMIC_LOAD_MIN_I16: 1725 AtomicOp = Mips::ATOMIC_LOAD_MIN_I16_POSTRA; 1726 NeedsAdditionalReg = true; 1727 break; 1728 case Mips::ATOMIC_LOAD_MAX_I8: 1729 AtomicOp = Mips::ATOMIC_LOAD_MAX_I8_POSTRA; 1730 NeedsAdditionalReg = true; 1731 break; 1732 case Mips::ATOMIC_LOAD_MAX_I16: 1733 AtomicOp = Mips::ATOMIC_LOAD_MAX_I16_POSTRA; 1734 NeedsAdditionalReg = true; 1735 break; 1736 case Mips::ATOMIC_LOAD_UMIN_I8: 1737 AtomicOp = Mips::ATOMIC_LOAD_UMIN_I8_POSTRA; 1738 NeedsAdditionalReg = true; 1739 break; 1740 case Mips::ATOMIC_LOAD_UMIN_I16: 1741 AtomicOp = Mips::ATOMIC_LOAD_UMIN_I16_POSTRA; 1742 NeedsAdditionalReg = true; 1743 break; 1744 case Mips::ATOMIC_LOAD_UMAX_I8: 1745 AtomicOp = Mips::ATOMIC_LOAD_UMAX_I8_POSTRA; 1746 NeedsAdditionalReg = true; 1747 break; 1748 case Mips::ATOMIC_LOAD_UMAX_I16: 1749 AtomicOp = Mips::ATOMIC_LOAD_UMAX_I16_POSTRA; 1750 NeedsAdditionalReg = true; 1751 break; 1752 default: 1753 llvm_unreachable("Unknown subword atomic pseudo for expansion!"); 1754 } 1755 1756 // insert new blocks after the current block 1757 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 1758 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 1759 MachineFunction::iterator It = ++BB->getIterator(); 1760 MF->insert(It, exitMBB); 1761 1762 // Transfer the remainder of BB and its successor edges to exitMBB. 1763 exitMBB->splice(exitMBB->begin(), BB, 1764 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 1765 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 1766 1767 BB->addSuccessor(exitMBB, BranchProbability::getOne()); 1768 1769 // thisMBB: 1770 // addiu masklsb2,$0,-4 # 0xfffffffc 1771 // and alignedaddr,ptr,masklsb2 1772 // andi ptrlsb2,ptr,3 1773 // sll shiftamt,ptrlsb2,3 1774 // ori maskupper,$0,255 # 0xff 1775 // sll mask,maskupper,shiftamt 1776 // nor mask2,$0,mask 1777 // sll incr2,incr,shiftamt 1778 1779 int64_t MaskImm = (Size == 1) ? 255 : 65535; 1780 BuildMI(BB, DL, TII->get(ABI.GetPtrAddiuOp()), MaskLSB2) 1781 .addReg(ABI.GetNullPtr()).addImm(-4); 1782 BuildMI(BB, DL, TII->get(ABI.GetPtrAndOp()), AlignedAddr) 1783 .addReg(Ptr).addReg(MaskLSB2); 1784 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2) 1785 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3); 1786 if (Subtarget.isLittle()) { 1787 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3); 1788 } else { 1789 Register Off = RegInfo.createVirtualRegister(RC); 1790 BuildMI(BB, DL, TII->get(Mips::XORi), Off) 1791 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2); 1792 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3); 1793 } 1794 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper) 1795 .addReg(Mips::ZERO).addImm(MaskImm); 1796 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask) 1797 .addReg(MaskUpper).addReg(ShiftAmt); 1798 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask); 1799 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt); 1800 1801 1802 // The purposes of the flags on the scratch registers is explained in 1803 // emitAtomicBinary. In summary, we need a scratch register which is going to 1804 // be undef, that is unique among registers chosen for the instruction. 1805 1806 MachineInstrBuilder MIB = 1807 BuildMI(BB, DL, TII->get(AtomicOp)) 1808 .addReg(Dest, RegState::Define | RegState::EarlyClobber) 1809 .addReg(AlignedAddr) 1810 .addReg(Incr2) 1811 .addReg(Mask) 1812 .addReg(Mask2) 1813 .addReg(ShiftAmt) 1814 .addReg(Scratch, RegState::EarlyClobber | RegState::Define | 1815 RegState::Dead | RegState::Implicit) 1816 .addReg(Scratch2, RegState::EarlyClobber | RegState::Define | 1817 RegState::Dead | RegState::Implicit) 1818 .addReg(Scratch3, RegState::EarlyClobber | RegState::Define | 1819 RegState::Dead | RegState::Implicit); 1820 if (NeedsAdditionalReg) { 1821 Register Scratch4 = RegInfo.createVirtualRegister(RC); 1822 MIB.addReg(Scratch4, RegState::EarlyClobber | RegState::Define | 1823 RegState::Dead | RegState::Implicit); 1824 } 1825 1826 MI.eraseFromParent(); // The instruction is gone now. 1827 1828 return exitMBB; 1829 } 1830 1831 // Lower atomic compare and swap to a pseudo instruction, taking care to 1832 // define a scratch register for the pseudo instruction's expansion. The 1833 // instruction is expanded after the register allocator as to prevent 1834 // the insertion of stores between the linked load and the store conditional. 1835 1836 MachineBasicBlock * 1837 MipsTargetLowering::emitAtomicCmpSwap(MachineInstr &MI, 1838 MachineBasicBlock *BB) const { 1839 1840 assert((MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 || 1841 MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I64) && 1842 "Unsupported atomic pseudo for EmitAtomicCmpSwap."); 1843 1844 const unsigned Size = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 ? 4 : 8; 1845 1846 MachineFunction *MF = BB->getParent(); 1847 MachineRegisterInfo &MRI = MF->getRegInfo(); 1848 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8)); 1849 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 1850 DebugLoc DL = MI.getDebugLoc(); 1851 1852 unsigned AtomicOp = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 1853 ? Mips::ATOMIC_CMP_SWAP_I32_POSTRA 1854 : Mips::ATOMIC_CMP_SWAP_I64_POSTRA; 1855 Register Dest = MI.getOperand(0).getReg(); 1856 Register Ptr = MI.getOperand(1).getReg(); 1857 Register OldVal = MI.getOperand(2).getReg(); 1858 Register NewVal = MI.getOperand(3).getReg(); 1859 1860 Register Scratch = MRI.createVirtualRegister(RC); 1861 MachineBasicBlock::iterator II(MI); 1862 1863 // We need to create copies of the various registers and kill them at the 1864 // atomic pseudo. If the copies are not made, when the atomic is expanded 1865 // after fast register allocation, the spills will end up outside of the 1866 // blocks that their values are defined in, causing livein errors. 1867 1868 Register PtrCopy = MRI.createVirtualRegister(MRI.getRegClass(Ptr)); 1869 Register OldValCopy = MRI.createVirtualRegister(MRI.getRegClass(OldVal)); 1870 Register NewValCopy = MRI.createVirtualRegister(MRI.getRegClass(NewVal)); 1871 1872 BuildMI(*BB, II, DL, TII->get(Mips::COPY), PtrCopy).addReg(Ptr); 1873 BuildMI(*BB, II, DL, TII->get(Mips::COPY), OldValCopy).addReg(OldVal); 1874 BuildMI(*BB, II, DL, TII->get(Mips::COPY), NewValCopy).addReg(NewVal); 1875 1876 // The purposes of the flags on the scratch registers is explained in 1877 // emitAtomicBinary. In summary, we need a scratch register which is going to 1878 // be undef, that is unique among registers chosen for the instruction. 1879 1880 BuildMI(*BB, II, DL, TII->get(AtomicOp)) 1881 .addReg(Dest, RegState::Define | RegState::EarlyClobber) 1882 .addReg(PtrCopy, RegState::Kill) 1883 .addReg(OldValCopy, RegState::Kill) 1884 .addReg(NewValCopy, RegState::Kill) 1885 .addReg(Scratch, RegState::EarlyClobber | RegState::Define | 1886 RegState::Dead | RegState::Implicit); 1887 1888 MI.eraseFromParent(); // The instruction is gone now. 1889 1890 return BB; 1891 } 1892 1893 MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwapPartword( 1894 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const { 1895 assert((Size == 1 || Size == 2) && 1896 "Unsupported size for EmitAtomicCmpSwapPartial."); 1897 1898 MachineFunction *MF = BB->getParent(); 1899 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 1900 const TargetRegisterClass *RC = getRegClassFor(MVT::i32); 1901 const bool ArePtrs64bit = ABI.ArePtrs64bit(); 1902 const TargetRegisterClass *RCp = 1903 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32); 1904 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 1905 DebugLoc DL = MI.getDebugLoc(); 1906 1907 Register Dest = MI.getOperand(0).getReg(); 1908 Register Ptr = MI.getOperand(1).getReg(); 1909 Register CmpVal = MI.getOperand(2).getReg(); 1910 Register NewVal = MI.getOperand(3).getReg(); 1911 1912 Register AlignedAddr = RegInfo.createVirtualRegister(RCp); 1913 Register ShiftAmt = RegInfo.createVirtualRegister(RC); 1914 Register Mask = RegInfo.createVirtualRegister(RC); 1915 Register Mask2 = RegInfo.createVirtualRegister(RC); 1916 Register ShiftedCmpVal = RegInfo.createVirtualRegister(RC); 1917 Register ShiftedNewVal = RegInfo.createVirtualRegister(RC); 1918 Register MaskLSB2 = RegInfo.createVirtualRegister(RCp); 1919 Register PtrLSB2 = RegInfo.createVirtualRegister(RC); 1920 Register MaskUpper = RegInfo.createVirtualRegister(RC); 1921 Register MaskedCmpVal = RegInfo.createVirtualRegister(RC); 1922 Register MaskedNewVal = RegInfo.createVirtualRegister(RC); 1923 unsigned AtomicOp = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I8 1924 ? Mips::ATOMIC_CMP_SWAP_I8_POSTRA 1925 : Mips::ATOMIC_CMP_SWAP_I16_POSTRA; 1926 1927 // The scratch registers here with the EarlyClobber | Define | Dead | Implicit 1928 // flags are used to coerce the register allocator and the machine verifier to 1929 // accept the usage of these registers. 1930 // The EarlyClobber flag has the semantic properties that the operand it is 1931 // attached to is clobbered before the rest of the inputs are read. Hence it 1932 // must be unique among the operands to the instruction. 1933 // The Define flag is needed to coerce the machine verifier that an Undef 1934 // value isn't a problem. 1935 // The Dead flag is needed as the value in scratch isn't used by any other 1936 // instruction. Kill isn't used as Dead is more precise. 1937 Register Scratch = RegInfo.createVirtualRegister(RC); 1938 Register Scratch2 = RegInfo.createVirtualRegister(RC); 1939 1940 // insert new blocks after the current block 1941 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 1942 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 1943 MachineFunction::iterator It = ++BB->getIterator(); 1944 MF->insert(It, exitMBB); 1945 1946 // Transfer the remainder of BB and its successor edges to exitMBB. 1947 exitMBB->splice(exitMBB->begin(), BB, 1948 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 1949 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 1950 1951 BB->addSuccessor(exitMBB, BranchProbability::getOne()); 1952 1953 // thisMBB: 1954 // addiu masklsb2,$0,-4 # 0xfffffffc 1955 // and alignedaddr,ptr,masklsb2 1956 // andi ptrlsb2,ptr,3 1957 // xori ptrlsb2,ptrlsb2,3 # Only for BE 1958 // sll shiftamt,ptrlsb2,3 1959 // ori maskupper,$0,255 # 0xff 1960 // sll mask,maskupper,shiftamt 1961 // nor mask2,$0,mask 1962 // andi maskedcmpval,cmpval,255 1963 // sll shiftedcmpval,maskedcmpval,shiftamt 1964 // andi maskednewval,newval,255 1965 // sll shiftednewval,maskednewval,shiftamt 1966 int64_t MaskImm = (Size == 1) ? 255 : 65535; 1967 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::DADDiu : Mips::ADDiu), MaskLSB2) 1968 .addReg(ABI.GetNullPtr()).addImm(-4); 1969 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::AND64 : Mips::AND), AlignedAddr) 1970 .addReg(Ptr).addReg(MaskLSB2); 1971 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2) 1972 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3); 1973 if (Subtarget.isLittle()) { 1974 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3); 1975 } else { 1976 Register Off = RegInfo.createVirtualRegister(RC); 1977 BuildMI(BB, DL, TII->get(Mips::XORi), Off) 1978 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2); 1979 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3); 1980 } 1981 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper) 1982 .addReg(Mips::ZERO).addImm(MaskImm); 1983 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask) 1984 .addReg(MaskUpper).addReg(ShiftAmt); 1985 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask); 1986 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal) 1987 .addReg(CmpVal).addImm(MaskImm); 1988 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal) 1989 .addReg(MaskedCmpVal).addReg(ShiftAmt); 1990 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal) 1991 .addReg(NewVal).addImm(MaskImm); 1992 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal) 1993 .addReg(MaskedNewVal).addReg(ShiftAmt); 1994 1995 // The purposes of the flags on the scratch registers are explained in 1996 // emitAtomicBinary. In summary, we need a scratch register which is going to 1997 // be undef, that is unique among the register chosen for the instruction. 1998 1999 BuildMI(BB, DL, TII->get(AtomicOp)) 2000 .addReg(Dest, RegState::Define | RegState::EarlyClobber) 2001 .addReg(AlignedAddr) 2002 .addReg(Mask) 2003 .addReg(ShiftedCmpVal) 2004 .addReg(Mask2) 2005 .addReg(ShiftedNewVal) 2006 .addReg(ShiftAmt) 2007 .addReg(Scratch, RegState::EarlyClobber | RegState::Define | 2008 RegState::Dead | RegState::Implicit) 2009 .addReg(Scratch2, RegState::EarlyClobber | RegState::Define | 2010 RegState::Dead | RegState::Implicit); 2011 2012 MI.eraseFromParent(); // The instruction is gone now. 2013 2014 return exitMBB; 2015 } 2016 2017 SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const { 2018 // The first operand is the chain, the second is the condition, the third is 2019 // the block to branch to if the condition is true. 2020 SDValue Chain = Op.getOperand(0); 2021 SDValue Dest = Op.getOperand(2); 2022 SDLoc DL(Op); 2023 2024 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6()); 2025 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1)); 2026 2027 // Return if flag is not set by a floating point comparison. 2028 if (CondRes.getOpcode() != MipsISD::FPCmp) 2029 return Op; 2030 2031 SDValue CCNode = CondRes.getOperand(2); 2032 Mips::CondCode CC = 2033 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue(); 2034 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T; 2035 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32); 2036 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32); 2037 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode, 2038 FCC0, Dest, CondRes); 2039 } 2040 2041 SDValue MipsTargetLowering:: 2042 lowerSELECT(SDValue Op, SelectionDAG &DAG) const 2043 { 2044 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6()); 2045 SDValue Cond = createFPCmp(DAG, Op.getOperand(0)); 2046 2047 // Return if flag is not set by a floating point comparison. 2048 if (Cond.getOpcode() != MipsISD::FPCmp) 2049 return Op; 2050 2051 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2), 2052 SDLoc(Op)); 2053 } 2054 2055 SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const { 2056 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6()); 2057 SDValue Cond = createFPCmp(DAG, Op); 2058 2059 assert(Cond.getOpcode() == MipsISD::FPCmp && 2060 "Floating point operand expected."); 2061 2062 SDLoc DL(Op); 2063 SDValue True = DAG.getConstant(1, DL, MVT::i32); 2064 SDValue False = DAG.getConstant(0, DL, MVT::i32); 2065 2066 return createCMovFP(DAG, Cond, True, False, DL); 2067 } 2068 2069 SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op, 2070 SelectionDAG &DAG) const { 2071 EVT Ty = Op.getValueType(); 2072 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op); 2073 const GlobalValue *GV = N->getGlobal(); 2074 2075 if (!isPositionIndependent()) { 2076 const MipsTargetObjectFile *TLOF = 2077 static_cast<const MipsTargetObjectFile *>( 2078 getTargetMachine().getObjFileLowering()); 2079 const GlobalObject *GO = GV->getAliaseeObject(); 2080 if (GO && TLOF->IsGlobalInSmallSection(GO, getTargetMachine())) 2081 // %gp_rel relocation 2082 return getAddrGPRel(N, SDLoc(N), Ty, DAG, ABI.IsN64()); 2083 2084 // %hi/%lo relocation 2085 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG) 2086 // %highest/%higher/%hi/%lo relocation 2087 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG); 2088 } 2089 2090 // Every other architecture would use shouldAssumeDSOLocal in here, but 2091 // mips is special. 2092 // * In PIC code mips requires got loads even for local statics! 2093 // * To save on got entries, for local statics the got entry contains the 2094 // page and an additional add instruction takes care of the low bits. 2095 // * It is legal to access a hidden symbol with a non hidden undefined, 2096 // so one cannot guarantee that all access to a hidden symbol will know 2097 // it is hidden. 2098 // * Mips linkers don't support creating a page and a full got entry for 2099 // the same symbol. 2100 // * Given all that, we have to use a full got entry for hidden symbols :-( 2101 if (GV->hasLocalLinkage()) 2102 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64()); 2103 2104 if (Subtarget.useXGOT()) 2105 return getAddrGlobalLargeGOT( 2106 N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16, MipsII::MO_GOT_LO16, 2107 DAG.getEntryNode(), 2108 MachinePointerInfo::getGOT(DAG.getMachineFunction())); 2109 2110 return getAddrGlobal( 2111 N, SDLoc(N), Ty, DAG, 2112 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP : MipsII::MO_GOT, 2113 DAG.getEntryNode(), MachinePointerInfo::getGOT(DAG.getMachineFunction())); 2114 } 2115 2116 SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op, 2117 SelectionDAG &DAG) const { 2118 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op); 2119 EVT Ty = Op.getValueType(); 2120 2121 if (!isPositionIndependent()) 2122 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG) 2123 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG); 2124 2125 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64()); 2126 } 2127 2128 SDValue MipsTargetLowering:: 2129 lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const 2130 { 2131 // If the relocation model is PIC, use the General Dynamic TLS Model or 2132 // Local Dynamic TLS model, otherwise use the Initial Exec or 2133 // Local Exec TLS Model. 2134 2135 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 2136 if (DAG.getTarget().useEmulatedTLS()) 2137 return LowerToTLSEmulatedModel(GA, DAG); 2138 2139 SDLoc DL(GA); 2140 const GlobalValue *GV = GA->getGlobal(); 2141 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2142 2143 TLSModel::Model model = getTargetMachine().getTLSModel(GV); 2144 2145 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) { 2146 // General Dynamic and Local Dynamic TLS Model. 2147 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM 2148 : MipsII::MO_TLSGD; 2149 2150 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag); 2151 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, 2152 getGlobalReg(DAG, PtrVT), TGA); 2153 unsigned PtrSize = PtrVT.getSizeInBits(); 2154 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize); 2155 2156 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT); 2157 2158 ArgListTy Args; 2159 ArgListEntry Entry; 2160 Entry.Node = Argument; 2161 Entry.Ty = PtrTy; 2162 Args.push_back(Entry); 2163 2164 TargetLowering::CallLoweringInfo CLI(DAG); 2165 CLI.setDebugLoc(DL) 2166 .setChain(DAG.getEntryNode()) 2167 .setLibCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args)); 2168 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 2169 2170 SDValue Ret = CallResult.first; 2171 2172 if (model != TLSModel::LocalDynamic) 2173 return Ret; 2174 2175 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 2176 MipsII::MO_DTPREL_HI); 2177 SDValue Hi = DAG.getNode(MipsISD::TlsHi, DL, PtrVT, TGAHi); 2178 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 2179 MipsII::MO_DTPREL_LO); 2180 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo); 2181 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret); 2182 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo); 2183 } 2184 2185 SDValue Offset; 2186 if (model == TLSModel::InitialExec) { 2187 // Initial Exec TLS Model 2188 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 2189 MipsII::MO_GOTTPREL); 2190 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT), 2191 TGA); 2192 Offset = 2193 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), TGA, MachinePointerInfo()); 2194 } else { 2195 // Local Exec TLS Model 2196 assert(model == TLSModel::LocalExec); 2197 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 2198 MipsII::MO_TPREL_HI); 2199 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 2200 MipsII::MO_TPREL_LO); 2201 SDValue Hi = DAG.getNode(MipsISD::TlsHi, DL, PtrVT, TGAHi); 2202 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo); 2203 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo); 2204 } 2205 2206 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT); 2207 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset); 2208 } 2209 2210 SDValue MipsTargetLowering:: 2211 lowerJumpTable(SDValue Op, SelectionDAG &DAG) const 2212 { 2213 JumpTableSDNode *N = cast<JumpTableSDNode>(Op); 2214 EVT Ty = Op.getValueType(); 2215 2216 if (!isPositionIndependent()) 2217 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG) 2218 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG); 2219 2220 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64()); 2221 } 2222 2223 SDValue MipsTargetLowering:: 2224 lowerConstantPool(SDValue Op, SelectionDAG &DAG) const 2225 { 2226 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op); 2227 EVT Ty = Op.getValueType(); 2228 2229 if (!isPositionIndependent()) { 2230 const MipsTargetObjectFile *TLOF = 2231 static_cast<const MipsTargetObjectFile *>( 2232 getTargetMachine().getObjFileLowering()); 2233 2234 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(), 2235 getTargetMachine())) 2236 // %gp_rel relocation 2237 return getAddrGPRel(N, SDLoc(N), Ty, DAG, ABI.IsN64()); 2238 2239 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG) 2240 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG); 2241 } 2242 2243 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64()); 2244 } 2245 2246 SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const { 2247 MachineFunction &MF = DAG.getMachineFunction(); 2248 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>(); 2249 2250 SDLoc DL(Op); 2251 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), 2252 getPointerTy(MF.getDataLayout())); 2253 2254 // vastart just stores the address of the VarArgsFrameIndex slot into the 2255 // memory location argument. 2256 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 2257 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1), 2258 MachinePointerInfo(SV)); 2259 } 2260 2261 SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const { 2262 SDNode *Node = Op.getNode(); 2263 EVT VT = Node->getValueType(0); 2264 SDValue Chain = Node->getOperand(0); 2265 SDValue VAListPtr = Node->getOperand(1); 2266 const Align Align = 2267 llvm::MaybeAlign(Node->getConstantOperandVal(3)).valueOrOne(); 2268 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); 2269 SDLoc DL(Node); 2270 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4; 2271 2272 SDValue VAListLoad = DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain, 2273 VAListPtr, MachinePointerInfo(SV)); 2274 SDValue VAList = VAListLoad; 2275 2276 // Re-align the pointer if necessary. 2277 // It should only ever be necessary for 64-bit types on O32 since the minimum 2278 // argument alignment is the same as the maximum type alignment for N32/N64. 2279 // 2280 // FIXME: We currently align too often. The code generator doesn't notice 2281 // when the pointer is still aligned from the last va_arg (or pair of 2282 // va_args for the i64 on O32 case). 2283 if (Align > getMinStackArgumentAlignment()) { 2284 VAList = DAG.getNode( 2285 ISD::ADD, DL, VAList.getValueType(), VAList, 2286 DAG.getConstant(Align.value() - 1, DL, VAList.getValueType())); 2287 2288 VAList = DAG.getNode( 2289 ISD::AND, DL, VAList.getValueType(), VAList, 2290 DAG.getConstant(-(int64_t)Align.value(), DL, VAList.getValueType())); 2291 } 2292 2293 // Increment the pointer, VAList, to the next vaarg. 2294 auto &TD = DAG.getDataLayout(); 2295 unsigned ArgSizeInBytes = 2296 TD.getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())); 2297 SDValue Tmp3 = 2298 DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList, 2299 DAG.getConstant(alignTo(ArgSizeInBytes, ArgSlotSizeInBytes), 2300 DL, VAList.getValueType())); 2301 // Store the incremented VAList to the legalized pointer 2302 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr, 2303 MachinePointerInfo(SV)); 2304 2305 // In big-endian mode we must adjust the pointer when the load size is smaller 2306 // than the argument slot size. We must also reduce the known alignment to 2307 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get 2308 // the correct half of the slot, and reduce the alignment from 8 (slot 2309 // alignment) down to 4 (type alignment). 2310 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) { 2311 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes; 2312 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList, 2313 DAG.getIntPtrConstant(Adjustment, DL)); 2314 } 2315 // Load the actual argument out of the pointer VAList 2316 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo()); 2317 } 2318 2319 static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, 2320 bool HasExtractInsert) { 2321 EVT TyX = Op.getOperand(0).getValueType(); 2322 EVT TyY = Op.getOperand(1).getValueType(); 2323 SDLoc DL(Op); 2324 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32); 2325 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32); 2326 SDValue Res; 2327 2328 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it 2329 // to i32. 2330 SDValue X = (TyX == MVT::f32) ? 2331 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) : 2332 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0), 2333 Const1); 2334 SDValue Y = (TyY == MVT::f32) ? 2335 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) : 2336 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1), 2337 Const1); 2338 2339 if (HasExtractInsert) { 2340 // ext E, Y, 31, 1 ; extract bit31 of Y 2341 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X 2342 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1); 2343 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X); 2344 } else { 2345 // sll SllX, X, 1 2346 // srl SrlX, SllX, 1 2347 // srl SrlY, Y, 31 2348 // sll SllY, SrlX, 31 2349 // or Or, SrlX, SllY 2350 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1); 2351 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1); 2352 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31); 2353 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31); 2354 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY); 2355 } 2356 2357 if (TyX == MVT::f32) 2358 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res); 2359 2360 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, 2361 Op.getOperand(0), 2362 DAG.getConstant(0, DL, MVT::i32)); 2363 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res); 2364 } 2365 2366 static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, 2367 bool HasExtractInsert) { 2368 unsigned WidthX = Op.getOperand(0).getValueSizeInBits(); 2369 unsigned WidthY = Op.getOperand(1).getValueSizeInBits(); 2370 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY); 2371 SDLoc DL(Op); 2372 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32); 2373 2374 // Bitcast to integer nodes. 2375 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0)); 2376 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1)); 2377 2378 if (HasExtractInsert) { 2379 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y 2380 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X 2381 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y, 2382 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1); 2383 2384 if (WidthX > WidthY) 2385 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E); 2386 else if (WidthY > WidthX) 2387 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E); 2388 2389 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E, 2390 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1, 2391 X); 2392 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I); 2393 } 2394 2395 // (d)sll SllX, X, 1 2396 // (d)srl SrlX, SllX, 1 2397 // (d)srl SrlY, Y, width(Y)-1 2398 // (d)sll SllY, SrlX, width(Y)-1 2399 // or Or, SrlX, SllY 2400 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1); 2401 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1); 2402 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y, 2403 DAG.getConstant(WidthY - 1, DL, MVT::i32)); 2404 2405 if (WidthX > WidthY) 2406 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY); 2407 else if (WidthY > WidthX) 2408 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY); 2409 2410 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY, 2411 DAG.getConstant(WidthX - 1, DL, MVT::i32)); 2412 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY); 2413 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or); 2414 } 2415 2416 SDValue 2417 MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { 2418 if (Subtarget.isGP64bit()) 2419 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert()); 2420 2421 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert()); 2422 } 2423 2424 static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, 2425 bool HasExtractInsert) { 2426 SDLoc DL(Op); 2427 SDValue Res, Const1 = DAG.getConstant(1, DL, MVT::i32); 2428 2429 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it 2430 // to i32. 2431 SDValue X = (Op.getValueType() == MVT::f32) 2432 ? DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) 2433 : DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, 2434 Op.getOperand(0), Const1); 2435 2436 // Clear MSB. 2437 if (HasExtractInsert) 2438 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, 2439 DAG.getRegister(Mips::ZERO, MVT::i32), 2440 DAG.getConstant(31, DL, MVT::i32), Const1, X); 2441 else { 2442 // TODO: Provide DAG patterns which transform (and x, cst) 2443 // back to a (shl (srl x (clz cst)) (clz cst)) sequence. 2444 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1); 2445 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1); 2446 } 2447 2448 if (Op.getValueType() == MVT::f32) 2449 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res); 2450 2451 // FIXME: For mips32r2, the sequence of (BuildPairF64 (ins (ExtractElementF64 2452 // Op 1), $zero, 31 1) (ExtractElementF64 Op 0)) and the Op has one use, we 2453 // should be able to drop the usage of mfc1/mtc1 and rewrite the register in 2454 // place. 2455 SDValue LowX = 2456 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0), 2457 DAG.getConstant(0, DL, MVT::i32)); 2458 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res); 2459 } 2460 2461 static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, 2462 bool HasExtractInsert) { 2463 SDLoc DL(Op); 2464 SDValue Res, Const1 = DAG.getConstant(1, DL, MVT::i32); 2465 2466 // Bitcast to integer node. 2467 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0)); 2468 2469 // Clear MSB. 2470 if (HasExtractInsert) 2471 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64, 2472 DAG.getRegister(Mips::ZERO_64, MVT::i64), 2473 DAG.getConstant(63, DL, MVT::i32), Const1, X); 2474 else { 2475 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1); 2476 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1); 2477 } 2478 2479 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res); 2480 } 2481 2482 SDValue MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const { 2483 if ((ABI.IsN32() || ABI.IsN64()) && (Op.getValueType() == MVT::f64)) 2484 return lowerFABS64(Op, DAG, Subtarget.hasExtractInsert()); 2485 2486 return lowerFABS32(Op, DAG, Subtarget.hasExtractInsert()); 2487 } 2488 2489 SDValue MipsTargetLowering:: 2490 lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { 2491 // check the depth 2492 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() != 0) { 2493 DAG.getContext()->emitError( 2494 "return address can be determined only for current frame"); 2495 return SDValue(); 2496 } 2497 2498 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 2499 MFI.setFrameAddressIsTaken(true); 2500 EVT VT = Op.getValueType(); 2501 SDLoc DL(Op); 2502 SDValue FrameAddr = DAG.getCopyFromReg( 2503 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT); 2504 return FrameAddr; 2505 } 2506 2507 SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op, 2508 SelectionDAG &DAG) const { 2509 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 2510 return SDValue(); 2511 2512 // check the depth 2513 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() != 0) { 2514 DAG.getContext()->emitError( 2515 "return address can be determined only for current frame"); 2516 return SDValue(); 2517 } 2518 2519 MachineFunction &MF = DAG.getMachineFunction(); 2520 MachineFrameInfo &MFI = MF.getFrameInfo(); 2521 MVT VT = Op.getSimpleValueType(); 2522 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA; 2523 MFI.setReturnAddressIsTaken(true); 2524 2525 // Return RA, which contains the return address. Mark it an implicit live-in. 2526 Register Reg = MF.addLiveIn(RA, getRegClassFor(VT)); 2527 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT); 2528 } 2529 2530 // An EH_RETURN is the result of lowering llvm.eh.return which in turn is 2531 // generated from __builtin_eh_return (offset, handler) 2532 // The effect of this is to adjust the stack pointer by "offset" 2533 // and then branch to "handler". 2534 SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG) 2535 const { 2536 MachineFunction &MF = DAG.getMachineFunction(); 2537 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 2538 2539 MipsFI->setCallsEhReturn(); 2540 SDValue Chain = Op.getOperand(0); 2541 SDValue Offset = Op.getOperand(1); 2542 SDValue Handler = Op.getOperand(2); 2543 SDLoc DL(Op); 2544 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32; 2545 2546 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and 2547 // EH_RETURN nodes, so that instructions are emitted back-to-back. 2548 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1; 2549 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0; 2550 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue()); 2551 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1)); 2552 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain, 2553 DAG.getRegister(OffsetReg, Ty), 2554 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())), 2555 Chain.getValue(1)); 2556 } 2557 2558 SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op, 2559 SelectionDAG &DAG) const { 2560 // FIXME: Need pseudo-fence for 'singlethread' fences 2561 // FIXME: Set SType for weaker fences where supported/appropriate. 2562 unsigned SType = 0; 2563 SDLoc DL(Op); 2564 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0), 2565 DAG.getConstant(SType, DL, MVT::i32)); 2566 } 2567 2568 SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op, 2569 SelectionDAG &DAG) const { 2570 SDLoc DL(Op); 2571 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32; 2572 2573 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1); 2574 SDValue Shamt = Op.getOperand(2); 2575 // if shamt < (VT.bits): 2576 // lo = (shl lo, shamt) 2577 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt)) 2578 // else: 2579 // lo = 0 2580 // hi = (shl lo, shamt[4:0]) 2581 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt, 2582 DAG.getConstant(-1, DL, MVT::i32)); 2583 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo, 2584 DAG.getConstant(1, DL, VT)); 2585 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not); 2586 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt); 2587 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo); 2588 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt); 2589 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt, 2590 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32)); 2591 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, 2592 DAG.getConstant(0, DL, VT), ShiftLeftLo); 2593 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or); 2594 2595 SDValue Ops[2] = {Lo, Hi}; 2596 return DAG.getMergeValues(Ops, DL); 2597 } 2598 2599 SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG, 2600 bool IsSRA) const { 2601 SDLoc DL(Op); 2602 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1); 2603 SDValue Shamt = Op.getOperand(2); 2604 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32; 2605 2606 // if shamt < (VT.bits): 2607 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt)) 2608 // if isSRA: 2609 // hi = (sra hi, shamt) 2610 // else: 2611 // hi = (srl hi, shamt) 2612 // else: 2613 // if isSRA: 2614 // lo = (sra hi, shamt[4:0]) 2615 // hi = (sra hi, 31) 2616 // else: 2617 // lo = (srl hi, shamt[4:0]) 2618 // hi = 0 2619 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt, 2620 DAG.getConstant(-1, DL, MVT::i32)); 2621 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, 2622 DAG.getConstant(1, DL, VT)); 2623 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not); 2624 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt); 2625 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo); 2626 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, 2627 DL, VT, Hi, Shamt); 2628 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt, 2629 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32)); 2630 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi, 2631 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT)); 2632 2633 if (!(Subtarget.hasMips4() || Subtarget.hasMips32())) { 2634 SDVTList VTList = DAG.getVTList(VT, VT); 2635 return DAG.getNode(Subtarget.isGP64bit() ? Mips::PseudoD_SELECT_I64 2636 : Mips::PseudoD_SELECT_I, 2637 DL, VTList, Cond, ShiftRightHi, 2638 IsSRA ? Ext : DAG.getConstant(0, DL, VT), Or, 2639 ShiftRightHi); 2640 } 2641 2642 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or); 2643 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, 2644 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi); 2645 2646 SDValue Ops[2] = {Lo, Hi}; 2647 return DAG.getMergeValues(Ops, DL); 2648 } 2649 2650 static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD, 2651 SDValue Chain, SDValue Src, unsigned Offset) { 2652 SDValue Ptr = LD->getBasePtr(); 2653 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT(); 2654 EVT BasePtrVT = Ptr.getValueType(); 2655 SDLoc DL(LD); 2656 SDVTList VTList = DAG.getVTList(VT, MVT::Other); 2657 2658 if (Offset) 2659 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr, 2660 DAG.getConstant(Offset, DL, BasePtrVT)); 2661 2662 SDValue Ops[] = { Chain, Ptr, Src }; 2663 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT, 2664 LD->getMemOperand()); 2665 } 2666 2667 // Expand an unaligned 32 or 64-bit integer load node. 2668 SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const { 2669 LoadSDNode *LD = cast<LoadSDNode>(Op); 2670 EVT MemVT = LD->getMemoryVT(); 2671 2672 if (Subtarget.systemSupportsUnalignedAccess()) 2673 return Op; 2674 2675 // Return if load is aligned or if MemVT is neither i32 nor i64. 2676 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) || 2677 ((MemVT != MVT::i32) && (MemVT != MVT::i64))) 2678 return SDValue(); 2679 2680 bool IsLittle = Subtarget.isLittle(); 2681 EVT VT = Op.getValueType(); 2682 ISD::LoadExtType ExtType = LD->getExtensionType(); 2683 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT); 2684 2685 assert((VT == MVT::i32) || (VT == MVT::i64)); 2686 2687 // Expand 2688 // (set dst, (i64 (load baseptr))) 2689 // to 2690 // (set tmp, (ldl (add baseptr, 7), undef)) 2691 // (set dst, (ldr baseptr, tmp)) 2692 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) { 2693 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef, 2694 IsLittle ? 7 : 0); 2695 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL, 2696 IsLittle ? 0 : 7); 2697 } 2698 2699 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef, 2700 IsLittle ? 3 : 0); 2701 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL, 2702 IsLittle ? 0 : 3); 2703 2704 // Expand 2705 // (set dst, (i32 (load baseptr))) or 2706 // (set dst, (i64 (sextload baseptr))) or 2707 // (set dst, (i64 (extload baseptr))) 2708 // to 2709 // (set tmp, (lwl (add baseptr, 3), undef)) 2710 // (set dst, (lwr baseptr, tmp)) 2711 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) || 2712 (ExtType == ISD::EXTLOAD)) 2713 return LWR; 2714 2715 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD)); 2716 2717 // Expand 2718 // (set dst, (i64 (zextload baseptr))) 2719 // to 2720 // (set tmp0, (lwl (add baseptr, 3), undef)) 2721 // (set tmp1, (lwr baseptr, tmp0)) 2722 // (set tmp2, (shl tmp1, 32)) 2723 // (set dst, (srl tmp2, 32)) 2724 SDLoc DL(LD); 2725 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32); 2726 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32); 2727 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32); 2728 SDValue Ops[] = { SRL, LWR.getValue(1) }; 2729 return DAG.getMergeValues(Ops, DL); 2730 } 2731 2732 static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD, 2733 SDValue Chain, unsigned Offset) { 2734 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue(); 2735 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType(); 2736 SDLoc DL(SD); 2737 SDVTList VTList = DAG.getVTList(MVT::Other); 2738 2739 if (Offset) 2740 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr, 2741 DAG.getConstant(Offset, DL, BasePtrVT)); 2742 2743 SDValue Ops[] = { Chain, Value, Ptr }; 2744 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT, 2745 SD->getMemOperand()); 2746 } 2747 2748 // Expand an unaligned 32 or 64-bit integer store node. 2749 static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG, 2750 bool IsLittle) { 2751 SDValue Value = SD->getValue(), Chain = SD->getChain(); 2752 EVT VT = Value.getValueType(); 2753 2754 // Expand 2755 // (store val, baseptr) or 2756 // (truncstore val, baseptr) 2757 // to 2758 // (swl val, (add baseptr, 3)) 2759 // (swr val, baseptr) 2760 if ((VT == MVT::i32) || SD->isTruncatingStore()) { 2761 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain, 2762 IsLittle ? 3 : 0); 2763 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3); 2764 } 2765 2766 assert(VT == MVT::i64); 2767 2768 // Expand 2769 // (store val, baseptr) 2770 // to 2771 // (sdl val, (add baseptr, 7)) 2772 // (sdr val, baseptr) 2773 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0); 2774 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7); 2775 } 2776 2777 // Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr). 2778 static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG, 2779 bool SingleFloat) { 2780 SDValue Val = SD->getValue(); 2781 2782 if (Val.getOpcode() != ISD::FP_TO_SINT || 2783 (Val.getValueSizeInBits() > 32 && SingleFloat)) 2784 return SDValue(); 2785 2786 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits()); 2787 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy, 2788 Val.getOperand(0)); 2789 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(), 2790 SD->getPointerInfo(), SD->getAlignment(), 2791 SD->getMemOperand()->getFlags()); 2792 } 2793 2794 SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const { 2795 StoreSDNode *SD = cast<StoreSDNode>(Op); 2796 EVT MemVT = SD->getMemoryVT(); 2797 2798 // Lower unaligned integer stores. 2799 if (!Subtarget.systemSupportsUnalignedAccess() && 2800 (SD->getAlignment() < MemVT.getSizeInBits() / 8) && 2801 ((MemVT == MVT::i32) || (MemVT == MVT::i64))) 2802 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle()); 2803 2804 return lowerFP_TO_SINT_STORE(SD, DAG, Subtarget.isSingleFloat()); 2805 } 2806 2807 SDValue MipsTargetLowering::lowerEH_DWARF_CFA(SDValue Op, 2808 SelectionDAG &DAG) const { 2809 2810 // Return a fixed StackObject with offset 0 which points to the old stack 2811 // pointer. 2812 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 2813 EVT ValTy = Op->getValueType(0); 2814 int FI = MFI.CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false); 2815 return DAG.getFrameIndex(FI, ValTy); 2816 } 2817 2818 SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op, 2819 SelectionDAG &DAG) const { 2820 if (Op.getValueSizeInBits() > 32 && Subtarget.isSingleFloat()) 2821 return SDValue(); 2822 2823 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits()); 2824 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy, 2825 Op.getOperand(0)); 2826 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc); 2827 } 2828 2829 //===----------------------------------------------------------------------===// 2830 // Calling Convention Implementation 2831 //===----------------------------------------------------------------------===// 2832 2833 //===----------------------------------------------------------------------===// 2834 // TODO: Implement a generic logic using tblgen that can support this. 2835 // Mips O32 ABI rules: 2836 // --- 2837 // i32 - Passed in A0, A1, A2, A3 and stack 2838 // f32 - Only passed in f32 registers if no int reg has been used yet to hold 2839 // an argument. Otherwise, passed in A1, A2, A3 and stack. 2840 // f64 - Only passed in two aliased f32 registers if no int reg has been used 2841 // yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is 2842 // not used, it must be shadowed. If only A3 is available, shadow it and 2843 // go to stack. 2844 // vXiX - Received as scalarized i32s, passed in A0 - A3 and the stack. 2845 // vXf32 - Passed in either a pair of registers {A0, A1}, {A2, A3} or {A0 - A3} 2846 // with the remainder spilled to the stack. 2847 // vXf64 - Passed in either {A0, A1, A2, A3} or {A2, A3} and in both cases 2848 // spilling the remainder to the stack. 2849 // 2850 // For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack. 2851 //===----------------------------------------------------------------------===// 2852 2853 static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT, 2854 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, 2855 CCState &State, ArrayRef<MCPhysReg> F64Regs) { 2856 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>( 2857 State.getMachineFunction().getSubtarget()); 2858 2859 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 }; 2860 2861 const MipsCCState * MipsState = static_cast<MipsCCState *>(&State); 2862 2863 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 }; 2864 2865 static const MCPhysReg FloatVectorIntRegs[] = { Mips::A0, Mips::A2 }; 2866 2867 // Do not process byval args here. 2868 if (ArgFlags.isByVal()) 2869 return true; 2870 2871 // Promote i8 and i16 2872 if (ArgFlags.isInReg() && !Subtarget.isLittle()) { 2873 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) { 2874 LocVT = MVT::i32; 2875 if (ArgFlags.isSExt()) 2876 LocInfo = CCValAssign::SExtUpper; 2877 else if (ArgFlags.isZExt()) 2878 LocInfo = CCValAssign::ZExtUpper; 2879 else 2880 LocInfo = CCValAssign::AExtUpper; 2881 } 2882 } 2883 2884 // Promote i8 and i16 2885 if (LocVT == MVT::i8 || LocVT == MVT::i16) { 2886 LocVT = MVT::i32; 2887 if (ArgFlags.isSExt()) 2888 LocInfo = CCValAssign::SExt; 2889 else if (ArgFlags.isZExt()) 2890 LocInfo = CCValAssign::ZExt; 2891 else 2892 LocInfo = CCValAssign::AExt; 2893 } 2894 2895 unsigned Reg; 2896 2897 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following 2898 // is true: function is vararg, argument is 3rd or higher, there is previous 2899 // argument which is not f32 or f64. 2900 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 || 2901 State.getFirstUnallocated(F32Regs) != ValNo; 2902 Align OrigAlign = ArgFlags.getNonZeroOrigAlign(); 2903 bool isI64 = (ValVT == MVT::i32 && OrigAlign == Align(8)); 2904 bool isVectorFloat = MipsState->WasOriginalArgVectorFloat(ValNo); 2905 2906 // The MIPS vector ABI for floats passes them in a pair of registers 2907 if (ValVT == MVT::i32 && isVectorFloat) { 2908 // This is the start of an vector that was scalarized into an unknown number 2909 // of components. It doesn't matter how many there are. Allocate one of the 2910 // notional 8 byte aligned registers which map onto the argument stack, and 2911 // shadow the register lost to alignment requirements. 2912 if (ArgFlags.isSplit()) { 2913 Reg = State.AllocateReg(FloatVectorIntRegs); 2914 if (Reg == Mips::A2) 2915 State.AllocateReg(Mips::A1); 2916 else if (Reg == 0) 2917 State.AllocateReg(Mips::A3); 2918 } else { 2919 // If we're an intermediate component of the split, we can just attempt to 2920 // allocate a register directly. 2921 Reg = State.AllocateReg(IntRegs); 2922 } 2923 } else if (ValVT == MVT::i32 || 2924 (ValVT == MVT::f32 && AllocateFloatsInIntReg)) { 2925 Reg = State.AllocateReg(IntRegs); 2926 // If this is the first part of an i64 arg, 2927 // the allocated register must be either A0 or A2. 2928 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3)) 2929 Reg = State.AllocateReg(IntRegs); 2930 LocVT = MVT::i32; 2931 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) { 2932 LocVT = MVT::i32; 2933 2934 // Allocate int register and shadow next int register. If first 2935 // available register is Mips::A1 or Mips::A3, shadow it too. 2936 Reg = State.AllocateReg(IntRegs); 2937 if (Reg == Mips::A1 || Reg == Mips::A3) 2938 Reg = State.AllocateReg(IntRegs); 2939 2940 if (Reg) { 2941 State.addLoc( 2942 CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 2943 MCRegister HiReg = State.AllocateReg(IntRegs); 2944 assert(HiReg); 2945 State.addLoc( 2946 CCValAssign::getCustomReg(ValNo, ValVT, HiReg, LocVT, LocInfo)); 2947 return false; 2948 } 2949 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) { 2950 // we are guaranteed to find an available float register 2951 if (ValVT == MVT::f32) { 2952 Reg = State.AllocateReg(F32Regs); 2953 // Shadow int register 2954 State.AllocateReg(IntRegs); 2955 } else { 2956 Reg = State.AllocateReg(F64Regs); 2957 // Shadow int registers 2958 unsigned Reg2 = State.AllocateReg(IntRegs); 2959 if (Reg2 == Mips::A1 || Reg2 == Mips::A3) 2960 State.AllocateReg(IntRegs); 2961 State.AllocateReg(IntRegs); 2962 } 2963 } else 2964 llvm_unreachable("Cannot handle this ValVT."); 2965 2966 if (!Reg) { 2967 unsigned Offset = State.AllocateStack(ValVT.getStoreSize(), OrigAlign); 2968 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo)); 2969 } else 2970 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 2971 2972 return false; 2973 } 2974 2975 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, 2976 MVT LocVT, CCValAssign::LocInfo LocInfo, 2977 ISD::ArgFlagsTy ArgFlags, CCState &State) { 2978 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 }; 2979 2980 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs); 2981 } 2982 2983 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, 2984 MVT LocVT, CCValAssign::LocInfo LocInfo, 2985 ISD::ArgFlagsTy ArgFlags, CCState &State) { 2986 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 }; 2987 2988 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs); 2989 } 2990 2991 static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT, 2992 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, 2993 CCState &State) LLVM_ATTRIBUTE_UNUSED; 2994 2995 #include "MipsGenCallingConv.inc" 2996 2997 CCAssignFn *MipsTargetLowering::CCAssignFnForCall() const{ 2998 return CC_Mips_FixedArg; 2999 } 3000 3001 CCAssignFn *MipsTargetLowering::CCAssignFnForReturn() const{ 3002 return RetCC_Mips; 3003 } 3004 //===----------------------------------------------------------------------===// 3005 // Call Calling Convention Implementation 3006 //===----------------------------------------------------------------------===// 3007 3008 SDValue MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset, 3009 SDValue Chain, SDValue Arg, 3010 const SDLoc &DL, bool IsTailCall, 3011 SelectionDAG &DAG) const { 3012 if (!IsTailCall) { 3013 SDValue PtrOff = 3014 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr, 3015 DAG.getIntPtrConstant(Offset, DL)); 3016 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo()); 3017 } 3018 3019 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 3020 int FI = MFI.CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false); 3021 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 3022 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(), MaybeAlign(), 3023 MachineMemOperand::MOVolatile); 3024 } 3025 3026 void MipsTargetLowering:: 3027 getOpndList(SmallVectorImpl<SDValue> &Ops, 3028 std::deque<std::pair<unsigned, SDValue>> &RegsToPass, 3029 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage, 3030 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee, 3031 SDValue Chain) const { 3032 // Insert node "GP copy globalreg" before call to function. 3033 // 3034 // R_MIPS_CALL* operators (emitted when non-internal functions are called 3035 // in PIC mode) allow symbols to be resolved via lazy binding. 3036 // The lazy binding stub requires GP to point to the GOT. 3037 // Note that we don't need GP to point to the GOT for indirect calls 3038 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates 3039 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs 3040 // used for the function (that is, Mips linker doesn't generate lazy binding 3041 // stub for a function whose address is taken in the program). 3042 if (IsPICCall && !InternalLinkage && IsCallReloc) { 3043 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP; 3044 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32; 3045 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty))); 3046 } 3047 3048 // Build a sequence of copy-to-reg nodes chained together with token 3049 // chain and flag operands which copy the outgoing args into registers. 3050 // The InFlag in necessary since all emitted instructions must be 3051 // stuck together. 3052 SDValue InFlag; 3053 3054 for (auto &R : RegsToPass) { 3055 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, R.first, R.second, InFlag); 3056 InFlag = Chain.getValue(1); 3057 } 3058 3059 // Add argument registers to the end of the list so that they are 3060 // known live into the call. 3061 for (auto &R : RegsToPass) 3062 Ops.push_back(CLI.DAG.getRegister(R.first, R.second.getValueType())); 3063 3064 // Add a register mask operand representing the call-preserved registers. 3065 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); 3066 const uint32_t *Mask = 3067 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv); 3068 assert(Mask && "Missing call preserved mask for calling convention"); 3069 if (Subtarget.inMips16HardFloat()) { 3070 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) { 3071 StringRef Sym = G->getGlobal()->getName(); 3072 Function *F = G->getGlobal()->getParent()->getFunction(Sym); 3073 if (F && F->hasFnAttribute("__Mips16RetHelper")) { 3074 Mask = MipsRegisterInfo::getMips16RetHelperMask(); 3075 } 3076 } 3077 } 3078 Ops.push_back(CLI.DAG.getRegisterMask(Mask)); 3079 3080 if (InFlag.getNode()) 3081 Ops.push_back(InFlag); 3082 } 3083 3084 void MipsTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, 3085 SDNode *Node) const { 3086 switch (MI.getOpcode()) { 3087 default: 3088 return; 3089 case Mips::JALR: 3090 case Mips::JALRPseudo: 3091 case Mips::JALR64: 3092 case Mips::JALR64Pseudo: 3093 case Mips::JALR16_MM: 3094 case Mips::JALRC16_MMR6: 3095 case Mips::TAILCALLREG: 3096 case Mips::TAILCALLREG64: 3097 case Mips::TAILCALLR6REG: 3098 case Mips::TAILCALL64R6REG: 3099 case Mips::TAILCALLREG_MM: 3100 case Mips::TAILCALLREG_MMR6: { 3101 if (!EmitJalrReloc || 3102 Subtarget.inMips16Mode() || 3103 !isPositionIndependent() || 3104 Node->getNumOperands() < 1 || 3105 Node->getOperand(0).getNumOperands() < 2) { 3106 return; 3107 } 3108 // We are after the callee address, set by LowerCall(). 3109 // If added to MI, asm printer will emit .reloc R_MIPS_JALR for the 3110 // symbol. 3111 const SDValue TargetAddr = Node->getOperand(0).getOperand(1); 3112 StringRef Sym; 3113 if (const GlobalAddressSDNode *G = 3114 dyn_cast_or_null<const GlobalAddressSDNode>(TargetAddr)) { 3115 // We must not emit the R_MIPS_JALR relocation against data symbols 3116 // since this will cause run-time crashes if the linker replaces the 3117 // call instruction with a relative branch to the data symbol. 3118 if (!isa<Function>(G->getGlobal())) { 3119 LLVM_DEBUG(dbgs() << "Not adding R_MIPS_JALR against data symbol " 3120 << G->getGlobal()->getName() << "\n"); 3121 return; 3122 } 3123 Sym = G->getGlobal()->getName(); 3124 } 3125 else if (const ExternalSymbolSDNode *ES = 3126 dyn_cast_or_null<const ExternalSymbolSDNode>(TargetAddr)) { 3127 Sym = ES->getSymbol(); 3128 } 3129 3130 if (Sym.empty()) 3131 return; 3132 3133 MachineFunction *MF = MI.getParent()->getParent(); 3134 MCSymbol *S = MF->getContext().getOrCreateSymbol(Sym); 3135 LLVM_DEBUG(dbgs() << "Adding R_MIPS_JALR against " << Sym << "\n"); 3136 MI.addOperand(MachineOperand::CreateMCSymbol(S, MipsII::MO_JALR)); 3137 } 3138 } 3139 } 3140 3141 /// LowerCall - functions arguments are copied from virtual regs to 3142 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted. 3143 SDValue 3144 MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 3145 SmallVectorImpl<SDValue> &InVals) const { 3146 SelectionDAG &DAG = CLI.DAG; 3147 SDLoc DL = CLI.DL; 3148 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 3149 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 3150 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 3151 SDValue Chain = CLI.Chain; 3152 SDValue Callee = CLI.Callee; 3153 bool &IsTailCall = CLI.IsTailCall; 3154 CallingConv::ID CallConv = CLI.CallConv; 3155 bool IsVarArg = CLI.IsVarArg; 3156 3157 MachineFunction &MF = DAG.getMachineFunction(); 3158 MachineFrameInfo &MFI = MF.getFrameInfo(); 3159 const TargetFrameLowering *TFL = Subtarget.getFrameLowering(); 3160 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>(); 3161 bool IsPIC = isPositionIndependent(); 3162 3163 // Analyze operands of the call, assigning locations to each operand. 3164 SmallVector<CCValAssign, 16> ArgLocs; 3165 MipsCCState CCInfo( 3166 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(), 3167 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget)); 3168 3169 const ExternalSymbolSDNode *ES = 3170 dyn_cast_or_null<const ExternalSymbolSDNode>(Callee.getNode()); 3171 3172 // There is one case where CALLSEQ_START..CALLSEQ_END can be nested, which 3173 // is during the lowering of a call with a byval argument which produces 3174 // a call to memcpy. For the O32 case, this causes the caller to allocate 3175 // stack space for the reserved argument area for the callee, then recursively 3176 // again for the memcpy call. In the NEWABI case, this doesn't occur as those 3177 // ABIs mandate that the callee allocates the reserved argument area. We do 3178 // still produce nested CALLSEQ_START..CALLSEQ_END with zero space though. 3179 // 3180 // If the callee has a byval argument and memcpy is used, we are mandated 3181 // to already have produced a reserved argument area for the callee for O32. 3182 // Therefore, the reserved argument area can be reused for both calls. 3183 // 3184 // Other cases of calling memcpy cannot have a chain with a CALLSEQ_START 3185 // present, as we have yet to hook that node onto the chain. 3186 // 3187 // Hence, the CALLSEQ_START and CALLSEQ_END nodes can be eliminated in this 3188 // case. GCC does a similar trick, in that wherever possible, it calculates 3189 // the maximum out going argument area (including the reserved area), and 3190 // preallocates the stack space on entrance to the caller. 3191 // 3192 // FIXME: We should do the same for efficiency and space. 3193 3194 // Note: The check on the calling convention below must match 3195 // MipsABIInfo::GetCalleeAllocdArgSizeInBytes(). 3196 bool MemcpyInByVal = ES && 3197 StringRef(ES->getSymbol()) == StringRef("memcpy") && 3198 CallConv != CallingConv::Fast && 3199 Chain.getOpcode() == ISD::CALLSEQ_START; 3200 3201 // Allocate the reserved argument area. It seems strange to do this from the 3202 // caller side but removing it breaks the frame size calculation. 3203 unsigned ReservedArgArea = 3204 MemcpyInByVal ? 0 : ABI.GetCalleeAllocdArgSizeInBytes(CallConv); 3205 CCInfo.AllocateStack(ReservedArgArea, Align(1)); 3206 3207 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), 3208 ES ? ES->getSymbol() : nullptr); 3209 3210 // Get a count of how many bytes are to be pushed on the stack. 3211 unsigned NextStackOffset = CCInfo.getNextStackOffset(); 3212 3213 // Call site info for function parameters tracking. 3214 MachineFunction::CallSiteInfo CSInfo; 3215 3216 // Check if it's really possible to do a tail call. Restrict it to functions 3217 // that are part of this compilation unit. 3218 bool InternalLinkage = false; 3219 if (IsTailCall) { 3220 IsTailCall = isEligibleForTailCallOptimization( 3221 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>()); 3222 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 3223 InternalLinkage = G->getGlobal()->hasInternalLinkage(); 3224 IsTailCall &= (InternalLinkage || G->getGlobal()->hasLocalLinkage() || 3225 G->getGlobal()->hasPrivateLinkage() || 3226 G->getGlobal()->hasHiddenVisibility() || 3227 G->getGlobal()->hasProtectedVisibility()); 3228 } 3229 } 3230 if (!IsTailCall && CLI.CB && CLI.CB->isMustTailCall()) 3231 report_fatal_error("failed to perform tail call elimination on a call " 3232 "site marked musttail"); 3233 3234 if (IsTailCall) 3235 ++NumTailCalls; 3236 3237 // Chain is the output chain of the last Load/Store or CopyToReg node. 3238 // ByValChain is the output chain of the last Memcpy node created for copying 3239 // byval arguments to the stack. 3240 unsigned StackAlignment = TFL->getStackAlignment(); 3241 NextStackOffset = alignTo(NextStackOffset, StackAlignment); 3242 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true); 3243 3244 if (!(IsTailCall || MemcpyInByVal)) 3245 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffset, 0, DL); 3246 3247 SDValue StackPtr = 3248 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP, 3249 getPointerTy(DAG.getDataLayout())); 3250 3251 std::deque<std::pair<unsigned, SDValue>> RegsToPass; 3252 SmallVector<SDValue, 8> MemOpChains; 3253 3254 CCInfo.rewindByValRegsInfo(); 3255 3256 // Walk the register/memloc assignments, inserting copies/loads. 3257 for (unsigned i = 0, e = ArgLocs.size(), OutIdx = 0; i != e; ++i, ++OutIdx) { 3258 SDValue Arg = OutVals[OutIdx]; 3259 CCValAssign &VA = ArgLocs[i]; 3260 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT(); 3261 ISD::ArgFlagsTy Flags = Outs[OutIdx].Flags; 3262 bool UseUpperBits = false; 3263 3264 // ByVal Arg. 3265 if (Flags.isByVal()) { 3266 unsigned FirstByValReg, LastByValReg; 3267 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed(); 3268 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg); 3269 3270 assert(Flags.getByValSize() && 3271 "ByVal args of size 0 should have been ignored by front-end."); 3272 assert(ByValIdx < CCInfo.getInRegsParamsCount()); 3273 assert(!IsTailCall && 3274 "Do not tail-call optimize if there is a byval argument."); 3275 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg, 3276 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(), 3277 VA); 3278 CCInfo.nextInRegsParam(); 3279 continue; 3280 } 3281 3282 // Promote the value if needed. 3283 switch (VA.getLocInfo()) { 3284 default: 3285 llvm_unreachable("Unknown loc info!"); 3286 case CCValAssign::Full: 3287 if (VA.isRegLoc()) { 3288 if ((ValVT == MVT::f32 && LocVT == MVT::i32) || 3289 (ValVT == MVT::f64 && LocVT == MVT::i64) || 3290 (ValVT == MVT::i64 && LocVT == MVT::f64)) 3291 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg); 3292 else if (ValVT == MVT::f64 && LocVT == MVT::i32) { 3293 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, 3294 Arg, DAG.getConstant(0, DL, MVT::i32)); 3295 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, 3296 Arg, DAG.getConstant(1, DL, MVT::i32)); 3297 if (!Subtarget.isLittle()) 3298 std::swap(Lo, Hi); 3299 3300 assert(VA.needsCustom()); 3301 3302 Register LocRegLo = VA.getLocReg(); 3303 Register LocRegHigh = ArgLocs[++i].getLocReg(); 3304 RegsToPass.push_back(std::make_pair(LocRegLo, Lo)); 3305 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi)); 3306 continue; 3307 } 3308 } 3309 break; 3310 case CCValAssign::BCvt: 3311 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg); 3312 break; 3313 case CCValAssign::SExtUpper: 3314 UseUpperBits = true; 3315 LLVM_FALLTHROUGH; 3316 case CCValAssign::SExt: 3317 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg); 3318 break; 3319 case CCValAssign::ZExtUpper: 3320 UseUpperBits = true; 3321 LLVM_FALLTHROUGH; 3322 case CCValAssign::ZExt: 3323 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg); 3324 break; 3325 case CCValAssign::AExtUpper: 3326 UseUpperBits = true; 3327 LLVM_FALLTHROUGH; 3328 case CCValAssign::AExt: 3329 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg); 3330 break; 3331 } 3332 3333 if (UseUpperBits) { 3334 unsigned ValSizeInBits = Outs[OutIdx].ArgVT.getSizeInBits(); 3335 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits(); 3336 Arg = DAG.getNode( 3337 ISD::SHL, DL, VA.getLocVT(), Arg, 3338 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT())); 3339 } 3340 3341 // Arguments that can be passed on register must be kept at 3342 // RegsToPass vector 3343 if (VA.isRegLoc()) { 3344 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 3345 3346 // If the parameter is passed through reg $D, which splits into 3347 // two physical registers, avoid creating call site info. 3348 if (Mips::AFGR64RegClass.contains(VA.getLocReg())) 3349 continue; 3350 3351 // Collect CSInfo about which register passes which parameter. 3352 const TargetOptions &Options = DAG.getTarget().Options; 3353 if (Options.SupportsDebugEntryValues) 3354 CSInfo.emplace_back(VA.getLocReg(), i); 3355 3356 continue; 3357 } 3358 3359 // Register can't get to this point... 3360 assert(VA.isMemLoc()); 3361 3362 // emit ISD::STORE whichs stores the 3363 // parameter value to a stack Location 3364 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(), 3365 Chain, Arg, DL, IsTailCall, DAG)); 3366 } 3367 3368 // Transform all store nodes into one single node because all store 3369 // nodes are independent of each other. 3370 if (!MemOpChains.empty()) 3371 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains); 3372 3373 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 3374 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 3375 // node so that legalize doesn't hack it. 3376 3377 EVT Ty = Callee.getValueType(); 3378 bool GlobalOrExternal = false, IsCallReloc = false; 3379 3380 // The long-calls feature is ignored in case of PIC. 3381 // While we do not support -mshared / -mno-shared properly, 3382 // ignore long-calls in case of -mabicalls too. 3383 if (!Subtarget.isABICalls() && !IsPIC) { 3384 // If the function should be called using "long call", 3385 // get its address into a register to prevent using 3386 // of the `jal` instruction for the direct call. 3387 if (auto *N = dyn_cast<ExternalSymbolSDNode>(Callee)) { 3388 if (Subtarget.useLongCalls()) 3389 Callee = Subtarget.hasSym32() 3390 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG) 3391 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG); 3392 } else if (auto *N = dyn_cast<GlobalAddressSDNode>(Callee)) { 3393 bool UseLongCalls = Subtarget.useLongCalls(); 3394 // If the function has long-call/far/near attribute 3395 // it overrides command line switch pased to the backend. 3396 if (auto *F = dyn_cast<Function>(N->getGlobal())) { 3397 if (F->hasFnAttribute("long-call")) 3398 UseLongCalls = true; 3399 else if (F->hasFnAttribute("short-call")) 3400 UseLongCalls = false; 3401 } 3402 if (UseLongCalls) 3403 Callee = Subtarget.hasSym32() 3404 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG) 3405 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG); 3406 } 3407 } 3408 3409 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 3410 if (IsPIC) { 3411 const GlobalValue *Val = G->getGlobal(); 3412 InternalLinkage = Val->hasInternalLinkage(); 3413 3414 if (InternalLinkage) 3415 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64()); 3416 else if (Subtarget.useXGOT()) { 3417 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16, 3418 MipsII::MO_CALL_LO16, Chain, 3419 FuncInfo->callPtrInfo(MF, Val)); 3420 IsCallReloc = true; 3421 } else { 3422 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain, 3423 FuncInfo->callPtrInfo(MF, Val)); 3424 IsCallReloc = true; 3425 } 3426 } else 3427 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, 3428 getPointerTy(DAG.getDataLayout()), 0, 3429 MipsII::MO_NO_FLAG); 3430 GlobalOrExternal = true; 3431 } 3432 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 3433 const char *Sym = S->getSymbol(); 3434 3435 if (!IsPIC) // static 3436 Callee = DAG.getTargetExternalSymbol( 3437 Sym, getPointerTy(DAG.getDataLayout()), MipsII::MO_NO_FLAG); 3438 else if (Subtarget.useXGOT()) { 3439 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16, 3440 MipsII::MO_CALL_LO16, Chain, 3441 FuncInfo->callPtrInfo(MF, Sym)); 3442 IsCallReloc = true; 3443 } else { // PIC 3444 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain, 3445 FuncInfo->callPtrInfo(MF, Sym)); 3446 IsCallReloc = true; 3447 } 3448 3449 GlobalOrExternal = true; 3450 } 3451 3452 SmallVector<SDValue, 8> Ops(1, Chain); 3453 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 3454 3455 getOpndList(Ops, RegsToPass, IsPIC, GlobalOrExternal, InternalLinkage, 3456 IsCallReloc, CLI, Callee, Chain); 3457 3458 if (IsTailCall) { 3459 MF.getFrameInfo().setHasTailCall(); 3460 SDValue Ret = DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops); 3461 DAG.addCallSiteInfo(Ret.getNode(), std::move(CSInfo)); 3462 return Ret; 3463 } 3464 3465 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops); 3466 SDValue InFlag = Chain.getValue(1); 3467 3468 DAG.addCallSiteInfo(Chain.getNode(), std::move(CSInfo)); 3469 3470 // Create the CALLSEQ_END node in the case of where it is not a call to 3471 // memcpy. 3472 if (!(MemcpyInByVal)) { 3473 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal, 3474 DAG.getIntPtrConstant(0, DL, true), InFlag, DL); 3475 InFlag = Chain.getValue(1); 3476 } 3477 3478 // Handle result values, copying them out of physregs into vregs that we 3479 // return. 3480 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG, 3481 InVals, CLI); 3482 } 3483 3484 /// LowerCallResult - Lower the result values of a call into the 3485 /// appropriate copies out of appropriate physical registers. 3486 SDValue MipsTargetLowering::LowerCallResult( 3487 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg, 3488 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 3489 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, 3490 TargetLowering::CallLoweringInfo &CLI) const { 3491 // Assign locations to each value returned by this call. 3492 SmallVector<CCValAssign, 16> RVLocs; 3493 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs, 3494 *DAG.getContext()); 3495 3496 const ExternalSymbolSDNode *ES = 3497 dyn_cast_or_null<const ExternalSymbolSDNode>(CLI.Callee.getNode()); 3498 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI.RetTy, 3499 ES ? ES->getSymbol() : nullptr); 3500 3501 // Copy all of the result registers out of their specified physreg. 3502 for (unsigned i = 0; i != RVLocs.size(); ++i) { 3503 CCValAssign &VA = RVLocs[i]; 3504 assert(VA.isRegLoc() && "Can only return in registers!"); 3505 3506 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(), 3507 RVLocs[i].getLocVT(), InFlag); 3508 Chain = Val.getValue(1); 3509 InFlag = Val.getValue(2); 3510 3511 if (VA.isUpperBitsInLoc()) { 3512 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits(); 3513 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits(); 3514 unsigned Shift = 3515 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA; 3516 Val = DAG.getNode( 3517 Shift, DL, VA.getLocVT(), Val, 3518 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT())); 3519 } 3520 3521 switch (VA.getLocInfo()) { 3522 default: 3523 llvm_unreachable("Unknown loc info!"); 3524 case CCValAssign::Full: 3525 break; 3526 case CCValAssign::BCvt: 3527 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val); 3528 break; 3529 case CCValAssign::AExt: 3530 case CCValAssign::AExtUpper: 3531 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val); 3532 break; 3533 case CCValAssign::ZExt: 3534 case CCValAssign::ZExtUpper: 3535 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val, 3536 DAG.getValueType(VA.getValVT())); 3537 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val); 3538 break; 3539 case CCValAssign::SExt: 3540 case CCValAssign::SExtUpper: 3541 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val, 3542 DAG.getValueType(VA.getValVT())); 3543 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val); 3544 break; 3545 } 3546 3547 InVals.push_back(Val); 3548 } 3549 3550 return Chain; 3551 } 3552 3553 static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA, 3554 EVT ArgVT, const SDLoc &DL, 3555 SelectionDAG &DAG) { 3556 MVT LocVT = VA.getLocVT(); 3557 EVT ValVT = VA.getValVT(); 3558 3559 // Shift into the upper bits if necessary. 3560 switch (VA.getLocInfo()) { 3561 default: 3562 break; 3563 case CCValAssign::AExtUpper: 3564 case CCValAssign::SExtUpper: 3565 case CCValAssign::ZExtUpper: { 3566 unsigned ValSizeInBits = ArgVT.getSizeInBits(); 3567 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits(); 3568 unsigned Opcode = 3569 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA; 3570 Val = DAG.getNode( 3571 Opcode, DL, VA.getLocVT(), Val, 3572 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT())); 3573 break; 3574 } 3575 } 3576 3577 // If this is an value smaller than the argument slot size (32-bit for O32, 3578 // 64-bit for N32/N64), it has been promoted in some way to the argument slot 3579 // size. Extract the value and insert any appropriate assertions regarding 3580 // sign/zero extension. 3581 switch (VA.getLocInfo()) { 3582 default: 3583 llvm_unreachable("Unknown loc info!"); 3584 case CCValAssign::Full: 3585 break; 3586 case CCValAssign::AExtUpper: 3587 case CCValAssign::AExt: 3588 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val); 3589 break; 3590 case CCValAssign::SExtUpper: 3591 case CCValAssign::SExt: 3592 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT)); 3593 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val); 3594 break; 3595 case CCValAssign::ZExtUpper: 3596 case CCValAssign::ZExt: 3597 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT)); 3598 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val); 3599 break; 3600 case CCValAssign::BCvt: 3601 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val); 3602 break; 3603 } 3604 3605 return Val; 3606 } 3607 3608 //===----------------------------------------------------------------------===// 3609 // Formal Arguments Calling Convention Implementation 3610 //===----------------------------------------------------------------------===// 3611 /// LowerFormalArguments - transform physical registers into virtual registers 3612 /// and generate load operations for arguments places on the stack. 3613 SDValue MipsTargetLowering::LowerFormalArguments( 3614 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 3615 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 3616 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 3617 MachineFunction &MF = DAG.getMachineFunction(); 3618 MachineFrameInfo &MFI = MF.getFrameInfo(); 3619 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 3620 3621 MipsFI->setVarArgsFrameIndex(0); 3622 3623 // Used with vargs to acumulate store chains. 3624 std::vector<SDValue> OutChains; 3625 3626 // Assign locations to all of the incoming arguments. 3627 SmallVector<CCValAssign, 16> ArgLocs; 3628 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, 3629 *DAG.getContext()); 3630 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), Align(1)); 3631 const Function &Func = DAG.getMachineFunction().getFunction(); 3632 Function::const_arg_iterator FuncArg = Func.arg_begin(); 3633 3634 if (Func.hasFnAttribute("interrupt") && !Func.arg_empty()) 3635 report_fatal_error( 3636 "Functions with the interrupt attribute cannot have arguments!"); 3637 3638 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg); 3639 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(), 3640 CCInfo.getInRegsParamsCount() > 0); 3641 3642 unsigned CurArgIdx = 0; 3643 CCInfo.rewindByValRegsInfo(); 3644 3645 for (unsigned i = 0, e = ArgLocs.size(), InsIdx = 0; i != e; ++i, ++InsIdx) { 3646 CCValAssign &VA = ArgLocs[i]; 3647 if (Ins[InsIdx].isOrigArg()) { 3648 std::advance(FuncArg, Ins[InsIdx].getOrigArgIndex() - CurArgIdx); 3649 CurArgIdx = Ins[InsIdx].getOrigArgIndex(); 3650 } 3651 EVT ValVT = VA.getValVT(); 3652 ISD::ArgFlagsTy Flags = Ins[InsIdx].Flags; 3653 bool IsRegLoc = VA.isRegLoc(); 3654 3655 if (Flags.isByVal()) { 3656 assert(Ins[InsIdx].isOrigArg() && "Byval arguments cannot be implicit"); 3657 unsigned FirstByValReg, LastByValReg; 3658 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed(); 3659 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg); 3660 3661 assert(Flags.getByValSize() && 3662 "ByVal args of size 0 should have been ignored by front-end."); 3663 assert(ByValIdx < CCInfo.getInRegsParamsCount()); 3664 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg, 3665 FirstByValReg, LastByValReg, VA, CCInfo); 3666 CCInfo.nextInRegsParam(); 3667 continue; 3668 } 3669 3670 // Arguments stored on registers 3671 if (IsRegLoc) { 3672 MVT RegVT = VA.getLocVT(); 3673 Register ArgReg = VA.getLocReg(); 3674 const TargetRegisterClass *RC = getRegClassFor(RegVT); 3675 3676 // Transform the arguments stored on 3677 // physical registers into virtual ones 3678 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC); 3679 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT); 3680 3681 ArgValue = 3682 UnpackFromArgumentSlot(ArgValue, VA, Ins[InsIdx].ArgVT, DL, DAG); 3683 3684 // Handle floating point arguments passed in integer registers and 3685 // long double arguments passed in floating point registers. 3686 if ((RegVT == MVT::i32 && ValVT == MVT::f32) || 3687 (RegVT == MVT::i64 && ValVT == MVT::f64) || 3688 (RegVT == MVT::f64 && ValVT == MVT::i64)) 3689 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue); 3690 else if (ABI.IsO32() && RegVT == MVT::i32 && 3691 ValVT == MVT::f64) { 3692 assert(VA.needsCustom() && "Expected custom argument for f64 split"); 3693 CCValAssign &NextVA = ArgLocs[++i]; 3694 unsigned Reg2 = 3695 addLiveIn(DAG.getMachineFunction(), NextVA.getLocReg(), RC); 3696 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT); 3697 if (!Subtarget.isLittle()) 3698 std::swap(ArgValue, ArgValue2); 3699 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, 3700 ArgValue, ArgValue2); 3701 } 3702 3703 InVals.push_back(ArgValue); 3704 } else { // VA.isRegLoc() 3705 MVT LocVT = VA.getLocVT(); 3706 3707 assert(!VA.needsCustom() && "unexpected custom memory argument"); 3708 3709 if (ABI.IsO32()) { 3710 // We ought to be able to use LocVT directly but O32 sets it to i32 3711 // when allocating floating point values to integer registers. 3712 // This shouldn't influence how we load the value into registers unless 3713 // we are targeting softfloat. 3714 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat()) 3715 LocVT = VA.getValVT(); 3716 } 3717 3718 // Only arguments pased on the stack should make it here. 3719 assert(VA.isMemLoc()); 3720 3721 // The stack pointer offset is relative to the caller stack frame. 3722 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8, 3723 VA.getLocMemOffset(), true); 3724 3725 // Create load nodes to retrieve arguments from the stack 3726 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 3727 SDValue ArgValue = DAG.getLoad( 3728 LocVT, DL, Chain, FIN, 3729 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)); 3730 OutChains.push_back(ArgValue.getValue(1)); 3731 3732 ArgValue = 3733 UnpackFromArgumentSlot(ArgValue, VA, Ins[InsIdx].ArgVT, DL, DAG); 3734 3735 InVals.push_back(ArgValue); 3736 } 3737 } 3738 3739 for (unsigned i = 0, e = ArgLocs.size(), InsIdx = 0; i != e; ++i, ++InsIdx) { 3740 3741 if (ArgLocs[i].needsCustom()) { 3742 ++i; 3743 continue; 3744 } 3745 3746 // The mips ABIs for returning structs by value requires that we copy 3747 // the sret argument into $v0 for the return. Save the argument into 3748 // a virtual register so that we can access it from the return points. 3749 if (Ins[InsIdx].Flags.isSRet()) { 3750 unsigned Reg = MipsFI->getSRetReturnReg(); 3751 if (!Reg) { 3752 Reg = MF.getRegInfo().createVirtualRegister( 3753 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32)); 3754 MipsFI->setSRetReturnReg(Reg); 3755 } 3756 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]); 3757 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain); 3758 break; 3759 } 3760 } 3761 3762 if (IsVarArg) 3763 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo); 3764 3765 // All stores are grouped in one node to allow the matching between 3766 // the size of Ins and InVals. This only happens when on varg functions 3767 if (!OutChains.empty()) { 3768 OutChains.push_back(Chain); 3769 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains); 3770 } 3771 3772 return Chain; 3773 } 3774 3775 //===----------------------------------------------------------------------===// 3776 // Return Value Calling Convention Implementation 3777 //===----------------------------------------------------------------------===// 3778 3779 bool 3780 MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 3781 MachineFunction &MF, bool IsVarArg, 3782 const SmallVectorImpl<ISD::OutputArg> &Outs, 3783 LLVMContext &Context) const { 3784 SmallVector<CCValAssign, 16> RVLocs; 3785 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context); 3786 return CCInfo.CheckReturn(Outs, RetCC_Mips); 3787 } 3788 3789 bool MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, 3790 bool IsSigned) const { 3791 if ((ABI.IsN32() || ABI.IsN64()) && Type == MVT::i32) 3792 return true; 3793 3794 return IsSigned; 3795 } 3796 3797 SDValue 3798 MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps, 3799 const SDLoc &DL, 3800 SelectionDAG &DAG) const { 3801 MachineFunction &MF = DAG.getMachineFunction(); 3802 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 3803 3804 MipsFI->setISR(); 3805 3806 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps); 3807 } 3808 3809 SDValue 3810 MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 3811 bool IsVarArg, 3812 const SmallVectorImpl<ISD::OutputArg> &Outs, 3813 const SmallVectorImpl<SDValue> &OutVals, 3814 const SDLoc &DL, SelectionDAG &DAG) const { 3815 // CCValAssign - represent the assignment of 3816 // the return value to a location 3817 SmallVector<CCValAssign, 16> RVLocs; 3818 MachineFunction &MF = DAG.getMachineFunction(); 3819 3820 // CCState - Info about the registers and stack slot. 3821 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext()); 3822 3823 // Analyze return values. 3824 CCInfo.AnalyzeReturn(Outs, RetCC_Mips); 3825 3826 SDValue Flag; 3827 SmallVector<SDValue, 4> RetOps(1, Chain); 3828 3829 // Copy the result values into the output registers. 3830 for (unsigned i = 0; i != RVLocs.size(); ++i) { 3831 SDValue Val = OutVals[i]; 3832 CCValAssign &VA = RVLocs[i]; 3833 assert(VA.isRegLoc() && "Can only return in registers!"); 3834 bool UseUpperBits = false; 3835 3836 switch (VA.getLocInfo()) { 3837 default: 3838 llvm_unreachable("Unknown loc info!"); 3839 case CCValAssign::Full: 3840 break; 3841 case CCValAssign::BCvt: 3842 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val); 3843 break; 3844 case CCValAssign::AExtUpper: 3845 UseUpperBits = true; 3846 LLVM_FALLTHROUGH; 3847 case CCValAssign::AExt: 3848 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val); 3849 break; 3850 case CCValAssign::ZExtUpper: 3851 UseUpperBits = true; 3852 LLVM_FALLTHROUGH; 3853 case CCValAssign::ZExt: 3854 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val); 3855 break; 3856 case CCValAssign::SExtUpper: 3857 UseUpperBits = true; 3858 LLVM_FALLTHROUGH; 3859 case CCValAssign::SExt: 3860 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val); 3861 break; 3862 } 3863 3864 if (UseUpperBits) { 3865 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits(); 3866 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits(); 3867 Val = DAG.getNode( 3868 ISD::SHL, DL, VA.getLocVT(), Val, 3869 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT())); 3870 } 3871 3872 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag); 3873 3874 // Guarantee that all emitted copies are stuck together with flags. 3875 Flag = Chain.getValue(1); 3876 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 3877 } 3878 3879 // The mips ABIs for returning structs by value requires that we copy 3880 // the sret argument into $v0 for the return. We saved the argument into 3881 // a virtual register in the entry block, so now we copy the value out 3882 // and into $v0. 3883 if (MF.getFunction().hasStructRetAttr()) { 3884 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 3885 unsigned Reg = MipsFI->getSRetReturnReg(); 3886 3887 if (!Reg) 3888 llvm_unreachable("sret virtual register not created in the entry block"); 3889 SDValue Val = 3890 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout())); 3891 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0; 3892 3893 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag); 3894 Flag = Chain.getValue(1); 3895 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout()))); 3896 } 3897 3898 RetOps[0] = Chain; // Update chain. 3899 3900 // Add the flag if we have it. 3901 if (Flag.getNode()) 3902 RetOps.push_back(Flag); 3903 3904 // ISRs must use "eret". 3905 if (DAG.getMachineFunction().getFunction().hasFnAttribute("interrupt")) 3906 return LowerInterruptReturn(RetOps, DL, DAG); 3907 3908 // Standard return on Mips is a "jr $ra" 3909 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps); 3910 } 3911 3912 //===----------------------------------------------------------------------===// 3913 // Mips Inline Assembly Support 3914 //===----------------------------------------------------------------------===// 3915 3916 /// getConstraintType - Given a constraint letter, return the type of 3917 /// constraint it is for this target. 3918 MipsTargetLowering::ConstraintType 3919 MipsTargetLowering::getConstraintType(StringRef Constraint) const { 3920 // Mips specific constraints 3921 // GCC config/mips/constraints.md 3922 // 3923 // 'd' : An address register. Equivalent to r 3924 // unless generating MIPS16 code. 3925 // 'y' : Equivalent to r; retained for 3926 // backwards compatibility. 3927 // 'c' : A register suitable for use in an indirect 3928 // jump. This will always be $25 for -mabicalls. 3929 // 'l' : The lo register. 1 word storage. 3930 // 'x' : The hilo register pair. Double word storage. 3931 if (Constraint.size() == 1) { 3932 switch (Constraint[0]) { 3933 default : break; 3934 case 'd': 3935 case 'y': 3936 case 'f': 3937 case 'c': 3938 case 'l': 3939 case 'x': 3940 return C_RegisterClass; 3941 case 'R': 3942 return C_Memory; 3943 } 3944 } 3945 3946 if (Constraint == "ZC") 3947 return C_Memory; 3948 3949 return TargetLowering::getConstraintType(Constraint); 3950 } 3951 3952 /// Examine constraint type and operand type and determine a weight value. 3953 /// This object must already have been set up with the operand type 3954 /// and the current alternative constraint selected. 3955 TargetLowering::ConstraintWeight 3956 MipsTargetLowering::getSingleConstraintMatchWeight( 3957 AsmOperandInfo &info, const char *constraint) const { 3958 ConstraintWeight weight = CW_Invalid; 3959 Value *CallOperandVal = info.CallOperandVal; 3960 // If we don't have a value, we can't do a match, 3961 // but allow it at the lowest weight. 3962 if (!CallOperandVal) 3963 return CW_Default; 3964 Type *type = CallOperandVal->getType(); 3965 // Look at the constraint type. 3966 switch (*constraint) { 3967 default: 3968 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 3969 break; 3970 case 'd': 3971 case 'y': 3972 if (type->isIntegerTy()) 3973 weight = CW_Register; 3974 break; 3975 case 'f': // FPU or MSA register 3976 if (Subtarget.hasMSA() && type->isVectorTy() && 3977 type->getPrimitiveSizeInBits().getFixedSize() == 128) 3978 weight = CW_Register; 3979 else if (type->isFloatTy()) 3980 weight = CW_Register; 3981 break; 3982 case 'c': // $25 for indirect jumps 3983 case 'l': // lo register 3984 case 'x': // hilo register pair 3985 if (type->isIntegerTy()) 3986 weight = CW_SpecificReg; 3987 break; 3988 case 'I': // signed 16 bit immediate 3989 case 'J': // integer zero 3990 case 'K': // unsigned 16 bit immediate 3991 case 'L': // signed 32 bit immediate where lower 16 bits are 0 3992 case 'N': // immediate in the range of -65535 to -1 (inclusive) 3993 case 'O': // signed 15 bit immediate (+- 16383) 3994 case 'P': // immediate in the range of 65535 to 1 (inclusive) 3995 if (isa<ConstantInt>(CallOperandVal)) 3996 weight = CW_Constant; 3997 break; 3998 case 'R': 3999 weight = CW_Memory; 4000 break; 4001 } 4002 return weight; 4003 } 4004 4005 /// This is a helper function to parse a physical register string and split it 4006 /// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag 4007 /// that is returned indicates whether parsing was successful. The second flag 4008 /// is true if the numeric part exists. 4009 static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix, 4010 unsigned long long &Reg) { 4011 if (C.front() != '{' || C.back() != '}') 4012 return std::make_pair(false, false); 4013 4014 // Search for the first numeric character. 4015 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1; 4016 I = std::find_if(B, E, isdigit); 4017 4018 Prefix = StringRef(B, I - B); 4019 4020 // The second flag is set to false if no numeric characters were found. 4021 if (I == E) 4022 return std::make_pair(true, false); 4023 4024 // Parse the numeric characters. 4025 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg), 4026 true); 4027 } 4028 4029 EVT MipsTargetLowering::getTypeForExtReturn(LLVMContext &Context, EVT VT, 4030 ISD::NodeType) const { 4031 bool Cond = !Subtarget.isABI_O32() && VT.getSizeInBits() == 32; 4032 EVT MinVT = getRegisterType(Context, Cond ? MVT::i64 : MVT::i32); 4033 return VT.bitsLT(MinVT) ? MinVT : VT; 4034 } 4035 4036 std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering:: 4037 parseRegForInlineAsmConstraint(StringRef C, MVT VT) const { 4038 const TargetRegisterInfo *TRI = 4039 Subtarget.getRegisterInfo(); 4040 const TargetRegisterClass *RC; 4041 StringRef Prefix; 4042 unsigned long long Reg; 4043 4044 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg); 4045 4046 if (!R.first) 4047 return std::make_pair(0U, nullptr); 4048 4049 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo. 4050 // No numeric characters follow "hi" or "lo". 4051 if (R.second) 4052 return std::make_pair(0U, nullptr); 4053 4054 RC = TRI->getRegClass(Prefix == "hi" ? 4055 Mips::HI32RegClassID : Mips::LO32RegClassID); 4056 return std::make_pair(*(RC->begin()), RC); 4057 } else if (Prefix.startswith("$msa")) { 4058 // Parse $msa(ir|csr|access|save|modify|request|map|unmap) 4059 4060 // No numeric characters follow the name. 4061 if (R.second) 4062 return std::make_pair(0U, nullptr); 4063 4064 Reg = StringSwitch<unsigned long long>(Prefix) 4065 .Case("$msair", Mips::MSAIR) 4066 .Case("$msacsr", Mips::MSACSR) 4067 .Case("$msaaccess", Mips::MSAAccess) 4068 .Case("$msasave", Mips::MSASave) 4069 .Case("$msamodify", Mips::MSAModify) 4070 .Case("$msarequest", Mips::MSARequest) 4071 .Case("$msamap", Mips::MSAMap) 4072 .Case("$msaunmap", Mips::MSAUnmap) 4073 .Default(0); 4074 4075 if (!Reg) 4076 return std::make_pair(0U, nullptr); 4077 4078 RC = TRI->getRegClass(Mips::MSACtrlRegClassID); 4079 return std::make_pair(Reg, RC); 4080 } 4081 4082 if (!R.second) 4083 return std::make_pair(0U, nullptr); 4084 4085 if (Prefix == "$f") { // Parse $f0-$f31. 4086 // If the size of FP registers is 64-bit or Reg is an even number, select 4087 // the 64-bit register class. Otherwise, select the 32-bit register class. 4088 if (VT == MVT::Other) 4089 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32; 4090 4091 RC = getRegClassFor(VT); 4092 4093 if (RC == &Mips::AFGR64RegClass) { 4094 assert(Reg % 2 == 0); 4095 Reg >>= 1; 4096 } 4097 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7. 4098 RC = TRI->getRegClass(Mips::FCCRegClassID); 4099 else if (Prefix == "$w") { // Parse $w0-$w31. 4100 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT); 4101 } else { // Parse $0-$31. 4102 assert(Prefix == "$"); 4103 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT); 4104 } 4105 4106 assert(Reg < RC->getNumRegs()); 4107 return std::make_pair(*(RC->begin() + Reg), RC); 4108 } 4109 4110 /// Given a register class constraint, like 'r', if this corresponds directly 4111 /// to an LLVM register class, return a register of 0 and the register class 4112 /// pointer. 4113 std::pair<unsigned, const TargetRegisterClass *> 4114 MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 4115 StringRef Constraint, 4116 MVT VT) const { 4117 if (Constraint.size() == 1) { 4118 switch (Constraint[0]) { 4119 case 'd': // Address register. Same as 'r' unless generating MIPS16 code. 4120 case 'y': // Same as 'r'. Exists for compatibility. 4121 case 'r': 4122 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8 || VT == MVT::i1) { 4123 if (Subtarget.inMips16Mode()) 4124 return std::make_pair(0U, &Mips::CPU16RegsRegClass); 4125 return std::make_pair(0U, &Mips::GPR32RegClass); 4126 } 4127 if (VT == MVT::i64 && !Subtarget.isGP64bit()) 4128 return std::make_pair(0U, &Mips::GPR32RegClass); 4129 if (VT == MVT::i64 && Subtarget.isGP64bit()) 4130 return std::make_pair(0U, &Mips::GPR64RegClass); 4131 // This will generate an error message 4132 return std::make_pair(0U, nullptr); 4133 case 'f': // FPU or MSA register 4134 if (VT == MVT::v16i8) 4135 return std::make_pair(0U, &Mips::MSA128BRegClass); 4136 else if (VT == MVT::v8i16 || VT == MVT::v8f16) 4137 return std::make_pair(0U, &Mips::MSA128HRegClass); 4138 else if (VT == MVT::v4i32 || VT == MVT::v4f32) 4139 return std::make_pair(0U, &Mips::MSA128WRegClass); 4140 else if (VT == MVT::v2i64 || VT == MVT::v2f64) 4141 return std::make_pair(0U, &Mips::MSA128DRegClass); 4142 else if (VT == MVT::f32) 4143 return std::make_pair(0U, &Mips::FGR32RegClass); 4144 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) { 4145 if (Subtarget.isFP64bit()) 4146 return std::make_pair(0U, &Mips::FGR64RegClass); 4147 return std::make_pair(0U, &Mips::AFGR64RegClass); 4148 } 4149 break; 4150 case 'c': // register suitable for indirect jump 4151 if (VT == MVT::i32) 4152 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass); 4153 if (VT == MVT::i64) 4154 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass); 4155 // This will generate an error message 4156 return std::make_pair(0U, nullptr); 4157 case 'l': // use the `lo` register to store values 4158 // that are no bigger than a word 4159 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) 4160 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass); 4161 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass); 4162 case 'x': // use the concatenated `hi` and `lo` registers 4163 // to store doubleword values 4164 // Fixme: Not triggering the use of both hi and low 4165 // This will generate an error message 4166 return std::make_pair(0U, nullptr); 4167 } 4168 } 4169 4170 if (!Constraint.empty()) { 4171 std::pair<unsigned, const TargetRegisterClass *> R; 4172 R = parseRegForInlineAsmConstraint(Constraint, VT); 4173 4174 if (R.second) 4175 return R; 4176 } 4177 4178 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 4179 } 4180 4181 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 4182 /// vector. If it is invalid, don't add anything to Ops. 4183 void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 4184 std::string &Constraint, 4185 std::vector<SDValue>&Ops, 4186 SelectionDAG &DAG) const { 4187 SDLoc DL(Op); 4188 SDValue Result; 4189 4190 // Only support length 1 constraints for now. 4191 if (Constraint.length() > 1) return; 4192 4193 char ConstraintLetter = Constraint[0]; 4194 switch (ConstraintLetter) { 4195 default: break; // This will fall through to the generic implementation 4196 case 'I': // Signed 16 bit constant 4197 // If this fails, the parent routine will give an error 4198 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4199 EVT Type = Op.getValueType(); 4200 int64_t Val = C->getSExtValue(); 4201 if (isInt<16>(Val)) { 4202 Result = DAG.getTargetConstant(Val, DL, Type); 4203 break; 4204 } 4205 } 4206 return; 4207 case 'J': // integer zero 4208 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4209 EVT Type = Op.getValueType(); 4210 int64_t Val = C->getZExtValue(); 4211 if (Val == 0) { 4212 Result = DAG.getTargetConstant(0, DL, Type); 4213 break; 4214 } 4215 } 4216 return; 4217 case 'K': // unsigned 16 bit immediate 4218 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4219 EVT Type = Op.getValueType(); 4220 uint64_t Val = (uint64_t)C->getZExtValue(); 4221 if (isUInt<16>(Val)) { 4222 Result = DAG.getTargetConstant(Val, DL, Type); 4223 break; 4224 } 4225 } 4226 return; 4227 case 'L': // signed 32 bit immediate where lower 16 bits are 0 4228 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4229 EVT Type = Op.getValueType(); 4230 int64_t Val = C->getSExtValue(); 4231 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){ 4232 Result = DAG.getTargetConstant(Val, DL, Type); 4233 break; 4234 } 4235 } 4236 return; 4237 case 'N': // immediate in the range of -65535 to -1 (inclusive) 4238 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4239 EVT Type = Op.getValueType(); 4240 int64_t Val = C->getSExtValue(); 4241 if ((Val >= -65535) && (Val <= -1)) { 4242 Result = DAG.getTargetConstant(Val, DL, Type); 4243 break; 4244 } 4245 } 4246 return; 4247 case 'O': // signed 15 bit immediate 4248 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4249 EVT Type = Op.getValueType(); 4250 int64_t Val = C->getSExtValue(); 4251 if ((isInt<15>(Val))) { 4252 Result = DAG.getTargetConstant(Val, DL, Type); 4253 break; 4254 } 4255 } 4256 return; 4257 case 'P': // immediate in the range of 1 to 65535 (inclusive) 4258 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4259 EVT Type = Op.getValueType(); 4260 int64_t Val = C->getSExtValue(); 4261 if ((Val <= 65535) && (Val >= 1)) { 4262 Result = DAG.getTargetConstant(Val, DL, Type); 4263 break; 4264 } 4265 } 4266 return; 4267 } 4268 4269 if (Result.getNode()) { 4270 Ops.push_back(Result); 4271 return; 4272 } 4273 4274 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 4275 } 4276 4277 bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL, 4278 const AddrMode &AM, Type *Ty, 4279 unsigned AS, 4280 Instruction *I) const { 4281 // No global is ever allowed as a base. 4282 if (AM.BaseGV) 4283 return false; 4284 4285 switch (AM.Scale) { 4286 case 0: // "r+i" or just "i", depending on HasBaseReg. 4287 break; 4288 case 1: 4289 if (!AM.HasBaseReg) // allow "r+i". 4290 break; 4291 return false; // disallow "r+r" or "r+r+i". 4292 default: 4293 return false; 4294 } 4295 4296 return true; 4297 } 4298 4299 bool 4300 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 4301 // The Mips target isn't yet aware of offsets. 4302 return false; 4303 } 4304 4305 EVT MipsTargetLowering::getOptimalMemOpType( 4306 const MemOp &Op, const AttributeList &FuncAttributes) const { 4307 if (Subtarget.hasMips64()) 4308 return MVT::i64; 4309 4310 return MVT::i32; 4311 } 4312 4313 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT, 4314 bool ForCodeSize) const { 4315 if (VT != MVT::f32 && VT != MVT::f64) 4316 return false; 4317 if (Imm.isNegZero()) 4318 return false; 4319 return Imm.isZero(); 4320 } 4321 4322 unsigned MipsTargetLowering::getJumpTableEncoding() const { 4323 4324 // FIXME: For space reasons this should be: EK_GPRel32BlockAddress. 4325 if (ABI.IsN64() && isPositionIndependent()) 4326 return MachineJumpTableInfo::EK_GPRel64BlockAddress; 4327 4328 return TargetLowering::getJumpTableEncoding(); 4329 } 4330 4331 bool MipsTargetLowering::useSoftFloat() const { 4332 return Subtarget.useSoftFloat(); 4333 } 4334 4335 void MipsTargetLowering::copyByValRegs( 4336 SDValue Chain, const SDLoc &DL, std::vector<SDValue> &OutChains, 4337 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags, 4338 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg, 4339 unsigned FirstReg, unsigned LastReg, const CCValAssign &VA, 4340 MipsCCState &State) const { 4341 MachineFunction &MF = DAG.getMachineFunction(); 4342 MachineFrameInfo &MFI = MF.getFrameInfo(); 4343 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes(); 4344 unsigned NumRegs = LastReg - FirstReg; 4345 unsigned RegAreaSize = NumRegs * GPRSizeInBytes; 4346 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize); 4347 int FrameObjOffset; 4348 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs(); 4349 4350 if (RegAreaSize) 4351 FrameObjOffset = 4352 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) - 4353 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes); 4354 else 4355 FrameObjOffset = VA.getLocMemOffset(); 4356 4357 // Create frame object. 4358 EVT PtrTy = getPointerTy(DAG.getDataLayout()); 4359 // Make the fixed object stored to mutable so that the load instructions 4360 // referencing it have their memory dependencies added. 4361 // Set the frame object as isAliased which clears the underlying objects 4362 // vector in ScheduleDAGInstrs::buildSchedGraph() resulting in addition of all 4363 // stores as dependencies for loads referencing this fixed object. 4364 int FI = MFI.CreateFixedObject(FrameObjSize, FrameObjOffset, false, true); 4365 SDValue FIN = DAG.getFrameIndex(FI, PtrTy); 4366 InVals.push_back(FIN); 4367 4368 if (!NumRegs) 4369 return; 4370 4371 // Copy arg registers. 4372 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8); 4373 const TargetRegisterClass *RC = getRegClassFor(RegTy); 4374 4375 for (unsigned I = 0; I < NumRegs; ++I) { 4376 unsigned ArgReg = ByValArgRegs[FirstReg + I]; 4377 unsigned VReg = addLiveIn(MF, ArgReg, RC); 4378 unsigned Offset = I * GPRSizeInBytes; 4379 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN, 4380 DAG.getConstant(Offset, DL, PtrTy)); 4381 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy), 4382 StorePtr, MachinePointerInfo(FuncArg, Offset)); 4383 OutChains.push_back(Store); 4384 } 4385 } 4386 4387 // Copy byVal arg to registers and stack. 4388 void MipsTargetLowering::passByValArg( 4389 SDValue Chain, const SDLoc &DL, 4390 std::deque<std::pair<unsigned, SDValue>> &RegsToPass, 4391 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr, 4392 MachineFrameInfo &MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg, 4393 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle, 4394 const CCValAssign &VA) const { 4395 unsigned ByValSizeInBytes = Flags.getByValSize(); 4396 unsigned OffsetInBytes = 0; // From beginning of struct 4397 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes(); 4398 Align Alignment = 4399 std::min(Flags.getNonZeroByValAlign(), Align(RegSizeInBytes)); 4400 EVT PtrTy = getPointerTy(DAG.getDataLayout()), 4401 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8); 4402 unsigned NumRegs = LastReg - FirstReg; 4403 4404 if (NumRegs) { 4405 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs(); 4406 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes); 4407 unsigned I = 0; 4408 4409 // Copy words to registers. 4410 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) { 4411 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg, 4412 DAG.getConstant(OffsetInBytes, DL, PtrTy)); 4413 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr, 4414 MachinePointerInfo(), Alignment); 4415 MemOpChains.push_back(LoadVal.getValue(1)); 4416 unsigned ArgReg = ArgRegs[FirstReg + I]; 4417 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal)); 4418 } 4419 4420 // Return if the struct has been fully copied. 4421 if (ByValSizeInBytes == OffsetInBytes) 4422 return; 4423 4424 // Copy the remainder of the byval argument with sub-word loads and shifts. 4425 if (LeftoverBytes) { 4426 SDValue Val; 4427 4428 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0; 4429 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) { 4430 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes; 4431 4432 if (RemainingSizeInBytes < LoadSizeInBytes) 4433 continue; 4434 4435 // Load subword. 4436 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg, 4437 DAG.getConstant(OffsetInBytes, DL, 4438 PtrTy)); 4439 SDValue LoadVal = DAG.getExtLoad( 4440 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(), 4441 MVT::getIntegerVT(LoadSizeInBytes * 8), Alignment); 4442 MemOpChains.push_back(LoadVal.getValue(1)); 4443 4444 // Shift the loaded value. 4445 unsigned Shamt; 4446 4447 if (isLittle) 4448 Shamt = TotalBytesLoaded * 8; 4449 else 4450 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8; 4451 4452 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal, 4453 DAG.getConstant(Shamt, DL, MVT::i32)); 4454 4455 if (Val.getNode()) 4456 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift); 4457 else 4458 Val = Shift; 4459 4460 OffsetInBytes += LoadSizeInBytes; 4461 TotalBytesLoaded += LoadSizeInBytes; 4462 Alignment = std::min(Alignment, Align(LoadSizeInBytes)); 4463 } 4464 4465 unsigned ArgReg = ArgRegs[FirstReg + I]; 4466 RegsToPass.push_back(std::make_pair(ArgReg, Val)); 4467 return; 4468 } 4469 } 4470 4471 // Copy remainder of byval arg to it with memcpy. 4472 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes; 4473 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg, 4474 DAG.getConstant(OffsetInBytes, DL, PtrTy)); 4475 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr, 4476 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL)); 4477 Chain = DAG.getMemcpy( 4478 Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, DL, PtrTy), 4479 Align(Alignment), /*isVolatile=*/false, /*AlwaysInline=*/false, 4480 /*isTailCall=*/false, MachinePointerInfo(), MachinePointerInfo()); 4481 MemOpChains.push_back(Chain); 4482 } 4483 4484 void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains, 4485 SDValue Chain, const SDLoc &DL, 4486 SelectionDAG &DAG, 4487 CCState &State) const { 4488 ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs(); 4489 unsigned Idx = State.getFirstUnallocated(ArgRegs); 4490 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes(); 4491 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8); 4492 const TargetRegisterClass *RC = getRegClassFor(RegTy); 4493 MachineFunction &MF = DAG.getMachineFunction(); 4494 MachineFrameInfo &MFI = MF.getFrameInfo(); 4495 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 4496 4497 // Offset of the first variable argument from stack pointer. 4498 int VaArgOffset; 4499 4500 if (ArgRegs.size() == Idx) 4501 VaArgOffset = alignTo(State.getNextStackOffset(), RegSizeInBytes); 4502 else { 4503 VaArgOffset = 4504 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) - 4505 (int)(RegSizeInBytes * (ArgRegs.size() - Idx)); 4506 } 4507 4508 // Record the frame index of the first variable argument 4509 // which is a value necessary to VASTART. 4510 int FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true); 4511 MipsFI->setVarArgsFrameIndex(FI); 4512 4513 // Copy the integer registers that have not been used for argument passing 4514 // to the argument register save area. For O32, the save area is allocated 4515 // in the caller's stack frame, while for N32/64, it is allocated in the 4516 // callee's stack frame. 4517 for (unsigned I = Idx; I < ArgRegs.size(); 4518 ++I, VaArgOffset += RegSizeInBytes) { 4519 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC); 4520 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy); 4521 FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true); 4522 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 4523 SDValue Store = 4524 DAG.getStore(Chain, DL, ArgValue, PtrOff, MachinePointerInfo()); 4525 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue( 4526 (Value *)nullptr); 4527 OutChains.push_back(Store); 4528 } 4529 } 4530 4531 void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size, 4532 Align Alignment) const { 4533 const TargetFrameLowering *TFL = Subtarget.getFrameLowering(); 4534 4535 assert(Size && "Byval argument's size shouldn't be 0."); 4536 4537 Alignment = std::min(Alignment, TFL->getStackAlign()); 4538 4539 unsigned FirstReg = 0; 4540 unsigned NumRegs = 0; 4541 4542 if (State->getCallingConv() != CallingConv::Fast) { 4543 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes(); 4544 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs(); 4545 // FIXME: The O32 case actually describes no shadow registers. 4546 const MCPhysReg *ShadowRegs = 4547 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs; 4548 4549 // We used to check the size as well but we can't do that anymore since 4550 // CCState::HandleByVal() rounds up the size after calling this function. 4551 assert( 4552 Alignment >= Align(RegSizeInBytes) && 4553 "Byval argument's alignment should be a multiple of RegSizeInBytes."); 4554 4555 FirstReg = State->getFirstUnallocated(IntArgRegs); 4556 4557 // If Alignment > RegSizeInBytes, the first arg register must be even. 4558 // FIXME: This condition happens to do the right thing but it's not the 4559 // right way to test it. We want to check that the stack frame offset 4560 // of the register is aligned. 4561 if ((Alignment > RegSizeInBytes) && (FirstReg % 2)) { 4562 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]); 4563 ++FirstReg; 4564 } 4565 4566 // Mark the registers allocated. 4567 Size = alignTo(Size, RegSizeInBytes); 4568 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size()); 4569 Size -= RegSizeInBytes, ++I, ++NumRegs) 4570 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]); 4571 } 4572 4573 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs); 4574 } 4575 4576 MachineBasicBlock *MipsTargetLowering::emitPseudoSELECT(MachineInstr &MI, 4577 MachineBasicBlock *BB, 4578 bool isFPCmp, 4579 unsigned Opc) const { 4580 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) && 4581 "Subtarget already supports SELECT nodes with the use of" 4582 "conditional-move instructions."); 4583 4584 const TargetInstrInfo *TII = 4585 Subtarget.getInstrInfo(); 4586 DebugLoc DL = MI.getDebugLoc(); 4587 4588 // To "insert" a SELECT instruction, we actually have to insert the 4589 // diamond control-flow pattern. The incoming instruction knows the 4590 // destination vreg to set, the condition code register to branch on, the 4591 // true/false values to select between, and a branch opcode to use. 4592 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 4593 MachineFunction::iterator It = ++BB->getIterator(); 4594 4595 // thisMBB: 4596 // ... 4597 // TrueVal = ... 4598 // setcc r1, r2, r3 4599 // bNE r1, r0, copy1MBB 4600 // fallthrough --> copy0MBB 4601 MachineBasicBlock *thisMBB = BB; 4602 MachineFunction *F = BB->getParent(); 4603 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 4604 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 4605 F->insert(It, copy0MBB); 4606 F->insert(It, sinkMBB); 4607 4608 // Transfer the remainder of BB and its successor edges to sinkMBB. 4609 sinkMBB->splice(sinkMBB->begin(), BB, 4610 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 4611 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 4612 4613 // Next, add the true and fallthrough blocks as its successors. 4614 BB->addSuccessor(copy0MBB); 4615 BB->addSuccessor(sinkMBB); 4616 4617 if (isFPCmp) { 4618 // bc1[tf] cc, sinkMBB 4619 BuildMI(BB, DL, TII->get(Opc)) 4620 .addReg(MI.getOperand(1).getReg()) 4621 .addMBB(sinkMBB); 4622 } else { 4623 // bne rs, $0, sinkMBB 4624 BuildMI(BB, DL, TII->get(Opc)) 4625 .addReg(MI.getOperand(1).getReg()) 4626 .addReg(Mips::ZERO) 4627 .addMBB(sinkMBB); 4628 } 4629 4630 // copy0MBB: 4631 // %FalseValue = ... 4632 // # fallthrough to sinkMBB 4633 BB = copy0MBB; 4634 4635 // Update machine-CFG edges 4636 BB->addSuccessor(sinkMBB); 4637 4638 // sinkMBB: 4639 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ] 4640 // ... 4641 BB = sinkMBB; 4642 4643 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg()) 4644 .addReg(MI.getOperand(2).getReg()) 4645 .addMBB(thisMBB) 4646 .addReg(MI.getOperand(3).getReg()) 4647 .addMBB(copy0MBB); 4648 4649 MI.eraseFromParent(); // The pseudo instruction is gone now. 4650 4651 return BB; 4652 } 4653 4654 MachineBasicBlock * 4655 MipsTargetLowering::emitPseudoD_SELECT(MachineInstr &MI, 4656 MachineBasicBlock *BB) const { 4657 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) && 4658 "Subtarget already supports SELECT nodes with the use of" 4659 "conditional-move instructions."); 4660 4661 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 4662 DebugLoc DL = MI.getDebugLoc(); 4663 4664 // D_SELECT substitutes two SELECT nodes that goes one after another and 4665 // have the same condition operand. On machines which don't have 4666 // conditional-move instruction, it reduces unnecessary branch instructions 4667 // which are result of using two diamond patterns that are result of two 4668 // SELECT pseudo instructions. 4669 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 4670 MachineFunction::iterator It = ++BB->getIterator(); 4671 4672 // thisMBB: 4673 // ... 4674 // TrueVal = ... 4675 // setcc r1, r2, r3 4676 // bNE r1, r0, copy1MBB 4677 // fallthrough --> copy0MBB 4678 MachineBasicBlock *thisMBB = BB; 4679 MachineFunction *F = BB->getParent(); 4680 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 4681 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 4682 F->insert(It, copy0MBB); 4683 F->insert(It, sinkMBB); 4684 4685 // Transfer the remainder of BB and its successor edges to sinkMBB. 4686 sinkMBB->splice(sinkMBB->begin(), BB, 4687 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 4688 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 4689 4690 // Next, add the true and fallthrough blocks as its successors. 4691 BB->addSuccessor(copy0MBB); 4692 BB->addSuccessor(sinkMBB); 4693 4694 // bne rs, $0, sinkMBB 4695 BuildMI(BB, DL, TII->get(Mips::BNE)) 4696 .addReg(MI.getOperand(2).getReg()) 4697 .addReg(Mips::ZERO) 4698 .addMBB(sinkMBB); 4699 4700 // copy0MBB: 4701 // %FalseValue = ... 4702 // # fallthrough to sinkMBB 4703 BB = copy0MBB; 4704 4705 // Update machine-CFG edges 4706 BB->addSuccessor(sinkMBB); 4707 4708 // sinkMBB: 4709 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ] 4710 // ... 4711 BB = sinkMBB; 4712 4713 // Use two PHI nodes to select two reults 4714 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg()) 4715 .addReg(MI.getOperand(3).getReg()) 4716 .addMBB(thisMBB) 4717 .addReg(MI.getOperand(5).getReg()) 4718 .addMBB(copy0MBB); 4719 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(1).getReg()) 4720 .addReg(MI.getOperand(4).getReg()) 4721 .addMBB(thisMBB) 4722 .addReg(MI.getOperand(6).getReg()) 4723 .addMBB(copy0MBB); 4724 4725 MI.eraseFromParent(); // The pseudo instruction is gone now. 4726 4727 return BB; 4728 } 4729 4730 // FIXME? Maybe this could be a TableGen attribute on some registers and 4731 // this table could be generated automatically from RegInfo. 4732 Register 4733 MipsTargetLowering::getRegisterByName(const char *RegName, LLT VT, 4734 const MachineFunction &MF) const { 4735 // Named registers is expected to be fairly rare. For now, just support $28 4736 // since the linux kernel uses it. 4737 if (Subtarget.isGP64bit()) { 4738 Register Reg = StringSwitch<Register>(RegName) 4739 .Case("$28", Mips::GP_64) 4740 .Default(Register()); 4741 if (Reg) 4742 return Reg; 4743 } else { 4744 Register Reg = StringSwitch<Register>(RegName) 4745 .Case("$28", Mips::GP) 4746 .Default(Register()); 4747 if (Reg) 4748 return Reg; 4749 } 4750 report_fatal_error("Invalid register name global variable"); 4751 } 4752 4753 MachineBasicBlock *MipsTargetLowering::emitLDR_W(MachineInstr &MI, 4754 MachineBasicBlock *BB) const { 4755 MachineFunction *MF = BB->getParent(); 4756 MachineRegisterInfo &MRI = MF->getRegInfo(); 4757 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 4758 const bool IsLittle = Subtarget.isLittle(); 4759 DebugLoc DL = MI.getDebugLoc(); 4760 4761 Register Dest = MI.getOperand(0).getReg(); 4762 Register Address = MI.getOperand(1).getReg(); 4763 unsigned Imm = MI.getOperand(2).getImm(); 4764 4765 MachineBasicBlock::iterator I(MI); 4766 4767 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) { 4768 // Mips release 6 can load from adress that is not naturally-aligned. 4769 Register Temp = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4770 BuildMI(*BB, I, DL, TII->get(Mips::LW)) 4771 .addDef(Temp) 4772 .addUse(Address) 4773 .addImm(Imm); 4774 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Dest).addUse(Temp); 4775 } else { 4776 // Mips release 5 needs to use instructions that can load from an unaligned 4777 // memory address. 4778 Register LoadHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4779 Register LoadFull = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4780 Register Undef = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4781 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(Undef); 4782 BuildMI(*BB, I, DL, TII->get(Mips::LWR)) 4783 .addDef(LoadHalf) 4784 .addUse(Address) 4785 .addImm(Imm + (IsLittle ? 0 : 3)) 4786 .addUse(Undef); 4787 BuildMI(*BB, I, DL, TII->get(Mips::LWL)) 4788 .addDef(LoadFull) 4789 .addUse(Address) 4790 .addImm(Imm + (IsLittle ? 3 : 0)) 4791 .addUse(LoadHalf); 4792 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Dest).addUse(LoadFull); 4793 } 4794 4795 MI.eraseFromParent(); 4796 return BB; 4797 } 4798 4799 MachineBasicBlock *MipsTargetLowering::emitLDR_D(MachineInstr &MI, 4800 MachineBasicBlock *BB) const { 4801 MachineFunction *MF = BB->getParent(); 4802 MachineRegisterInfo &MRI = MF->getRegInfo(); 4803 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 4804 const bool IsLittle = Subtarget.isLittle(); 4805 DebugLoc DL = MI.getDebugLoc(); 4806 4807 Register Dest = MI.getOperand(0).getReg(); 4808 Register Address = MI.getOperand(1).getReg(); 4809 unsigned Imm = MI.getOperand(2).getImm(); 4810 4811 MachineBasicBlock::iterator I(MI); 4812 4813 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) { 4814 // Mips release 6 can load from adress that is not naturally-aligned. 4815 if (Subtarget.isGP64bit()) { 4816 Register Temp = MRI.createVirtualRegister(&Mips::GPR64RegClass); 4817 BuildMI(*BB, I, DL, TII->get(Mips::LD)) 4818 .addDef(Temp) 4819 .addUse(Address) 4820 .addImm(Imm); 4821 BuildMI(*BB, I, DL, TII->get(Mips::FILL_D)).addDef(Dest).addUse(Temp); 4822 } else { 4823 Register Wtemp = MRI.createVirtualRegister(&Mips::MSA128WRegClass); 4824 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4825 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4826 BuildMI(*BB, I, DL, TII->get(Mips::LW)) 4827 .addDef(Lo) 4828 .addUse(Address) 4829 .addImm(Imm + (IsLittle ? 0 : 4)); 4830 BuildMI(*BB, I, DL, TII->get(Mips::LW)) 4831 .addDef(Hi) 4832 .addUse(Address) 4833 .addImm(Imm + (IsLittle ? 4 : 0)); 4834 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Wtemp).addUse(Lo); 4835 BuildMI(*BB, I, DL, TII->get(Mips::INSERT_W), Dest) 4836 .addUse(Wtemp) 4837 .addUse(Hi) 4838 .addImm(1); 4839 } 4840 } else { 4841 // Mips release 5 needs to use instructions that can load from an unaligned 4842 // memory address. 4843 Register LoHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4844 Register LoFull = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4845 Register LoUndef = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4846 Register HiHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4847 Register HiFull = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4848 Register HiUndef = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4849 Register Wtemp = MRI.createVirtualRegister(&Mips::MSA128WRegClass); 4850 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(LoUndef); 4851 BuildMI(*BB, I, DL, TII->get(Mips::LWR)) 4852 .addDef(LoHalf) 4853 .addUse(Address) 4854 .addImm(Imm + (IsLittle ? 0 : 7)) 4855 .addUse(LoUndef); 4856 BuildMI(*BB, I, DL, TII->get(Mips::LWL)) 4857 .addDef(LoFull) 4858 .addUse(Address) 4859 .addImm(Imm + (IsLittle ? 3 : 4)) 4860 .addUse(LoHalf); 4861 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(HiUndef); 4862 BuildMI(*BB, I, DL, TII->get(Mips::LWR)) 4863 .addDef(HiHalf) 4864 .addUse(Address) 4865 .addImm(Imm + (IsLittle ? 4 : 3)) 4866 .addUse(HiUndef); 4867 BuildMI(*BB, I, DL, TII->get(Mips::LWL)) 4868 .addDef(HiFull) 4869 .addUse(Address) 4870 .addImm(Imm + (IsLittle ? 7 : 0)) 4871 .addUse(HiHalf); 4872 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Wtemp).addUse(LoFull); 4873 BuildMI(*BB, I, DL, TII->get(Mips::INSERT_W), Dest) 4874 .addUse(Wtemp) 4875 .addUse(HiFull) 4876 .addImm(1); 4877 } 4878 4879 MI.eraseFromParent(); 4880 return BB; 4881 } 4882 4883 MachineBasicBlock *MipsTargetLowering::emitSTR_W(MachineInstr &MI, 4884 MachineBasicBlock *BB) const { 4885 MachineFunction *MF = BB->getParent(); 4886 MachineRegisterInfo &MRI = MF->getRegInfo(); 4887 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 4888 const bool IsLittle = Subtarget.isLittle(); 4889 DebugLoc DL = MI.getDebugLoc(); 4890 4891 Register StoreVal = MI.getOperand(0).getReg(); 4892 Register Address = MI.getOperand(1).getReg(); 4893 unsigned Imm = MI.getOperand(2).getImm(); 4894 4895 MachineBasicBlock::iterator I(MI); 4896 4897 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) { 4898 // Mips release 6 can store to adress that is not naturally-aligned. 4899 Register BitcastW = MRI.createVirtualRegister(&Mips::MSA128WRegClass); 4900 Register Tmp = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4901 BuildMI(*BB, I, DL, TII->get(Mips::COPY)).addDef(BitcastW).addUse(StoreVal); 4902 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W)) 4903 .addDef(Tmp) 4904 .addUse(BitcastW) 4905 .addImm(0); 4906 BuildMI(*BB, I, DL, TII->get(Mips::SW)) 4907 .addUse(Tmp) 4908 .addUse(Address) 4909 .addImm(Imm); 4910 } else { 4911 // Mips release 5 needs to use instructions that can store to an unaligned 4912 // memory address. 4913 Register Tmp = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4914 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W)) 4915 .addDef(Tmp) 4916 .addUse(StoreVal) 4917 .addImm(0); 4918 BuildMI(*BB, I, DL, TII->get(Mips::SWR)) 4919 .addUse(Tmp) 4920 .addUse(Address) 4921 .addImm(Imm + (IsLittle ? 0 : 3)); 4922 BuildMI(*BB, I, DL, TII->get(Mips::SWL)) 4923 .addUse(Tmp) 4924 .addUse(Address) 4925 .addImm(Imm + (IsLittle ? 3 : 0)); 4926 } 4927 4928 MI.eraseFromParent(); 4929 4930 return BB; 4931 } 4932 4933 MachineBasicBlock *MipsTargetLowering::emitSTR_D(MachineInstr &MI, 4934 MachineBasicBlock *BB) const { 4935 MachineFunction *MF = BB->getParent(); 4936 MachineRegisterInfo &MRI = MF->getRegInfo(); 4937 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 4938 const bool IsLittle = Subtarget.isLittle(); 4939 DebugLoc DL = MI.getDebugLoc(); 4940 4941 Register StoreVal = MI.getOperand(0).getReg(); 4942 Register Address = MI.getOperand(1).getReg(); 4943 unsigned Imm = MI.getOperand(2).getImm(); 4944 4945 MachineBasicBlock::iterator I(MI); 4946 4947 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) { 4948 // Mips release 6 can store to adress that is not naturally-aligned. 4949 if (Subtarget.isGP64bit()) { 4950 Register BitcastD = MRI.createVirtualRegister(&Mips::MSA128DRegClass); 4951 Register Lo = MRI.createVirtualRegister(&Mips::GPR64RegClass); 4952 BuildMI(*BB, I, DL, TII->get(Mips::COPY)) 4953 .addDef(BitcastD) 4954 .addUse(StoreVal); 4955 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_D)) 4956 .addDef(Lo) 4957 .addUse(BitcastD) 4958 .addImm(0); 4959 BuildMI(*BB, I, DL, TII->get(Mips::SD)) 4960 .addUse(Lo) 4961 .addUse(Address) 4962 .addImm(Imm); 4963 } else { 4964 Register BitcastW = MRI.createVirtualRegister(&Mips::MSA128WRegClass); 4965 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4966 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4967 BuildMI(*BB, I, DL, TII->get(Mips::COPY)) 4968 .addDef(BitcastW) 4969 .addUse(StoreVal); 4970 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W)) 4971 .addDef(Lo) 4972 .addUse(BitcastW) 4973 .addImm(0); 4974 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W)) 4975 .addDef(Hi) 4976 .addUse(BitcastW) 4977 .addImm(1); 4978 BuildMI(*BB, I, DL, TII->get(Mips::SW)) 4979 .addUse(Lo) 4980 .addUse(Address) 4981 .addImm(Imm + (IsLittle ? 0 : 4)); 4982 BuildMI(*BB, I, DL, TII->get(Mips::SW)) 4983 .addUse(Hi) 4984 .addUse(Address) 4985 .addImm(Imm + (IsLittle ? 4 : 0)); 4986 } 4987 } else { 4988 // Mips release 5 needs to use instructions that can store to an unaligned 4989 // memory address. 4990 Register Bitcast = MRI.createVirtualRegister(&Mips::MSA128WRegClass); 4991 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4992 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4993 BuildMI(*BB, I, DL, TII->get(Mips::COPY)).addDef(Bitcast).addUse(StoreVal); 4994 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W)) 4995 .addDef(Lo) 4996 .addUse(Bitcast) 4997 .addImm(0); 4998 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W)) 4999 .addDef(Hi) 5000 .addUse(Bitcast) 5001 .addImm(1); 5002 BuildMI(*BB, I, DL, TII->get(Mips::SWR)) 5003 .addUse(Lo) 5004 .addUse(Address) 5005 .addImm(Imm + (IsLittle ? 0 : 3)); 5006 BuildMI(*BB, I, DL, TII->get(Mips::SWL)) 5007 .addUse(Lo) 5008 .addUse(Address) 5009 .addImm(Imm + (IsLittle ? 3 : 0)); 5010 BuildMI(*BB, I, DL, TII->get(Mips::SWR)) 5011 .addUse(Hi) 5012 .addUse(Address) 5013 .addImm(Imm + (IsLittle ? 4 : 7)); 5014 BuildMI(*BB, I, DL, TII->get(Mips::SWL)) 5015 .addUse(Hi) 5016 .addUse(Address) 5017 .addImm(Imm + (IsLittle ? 7 : 4)); 5018 } 5019 5020 MI.eraseFromParent(); 5021 return BB; 5022 } 5023