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