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 = 2967 State.AllocateStack(ValVT.getStoreSize(), Align(OrigAlign)); 2968 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo)); 2969 } else 2970 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 2971 2972 return false; 2973 } 2974 2975 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, 2976 MVT LocVT, CCValAssign::LocInfo LocInfo, 2977 ISD::ArgFlagsTy ArgFlags, CCState &State) { 2978 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 }; 2979 2980 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs); 2981 } 2982 2983 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, 2984 MVT LocVT, CCValAssign::LocInfo LocInfo, 2985 ISD::ArgFlagsTy ArgFlags, CCState &State) { 2986 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 }; 2987 2988 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs); 2989 } 2990 2991 static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT, 2992 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, 2993 CCState &State) LLVM_ATTRIBUTE_UNUSED; 2994 2995 #include "MipsGenCallingConv.inc" 2996 2997 CCAssignFn *MipsTargetLowering::CCAssignFnForCall() const{ 2998 return CC_Mips_FixedArg; 2999 } 3000 3001 CCAssignFn *MipsTargetLowering::CCAssignFnForReturn() const{ 3002 return RetCC_Mips; 3003 } 3004 //===----------------------------------------------------------------------===// 3005 // Call Calling Convention Implementation 3006 //===----------------------------------------------------------------------===// 3007 3008 // Return next O32 integer argument register. 3009 static unsigned getNextIntArgReg(unsigned Reg) { 3010 assert((Reg == Mips::A0) || (Reg == Mips::A2)); 3011 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3; 3012 } 3013 3014 SDValue MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset, 3015 SDValue Chain, SDValue Arg, 3016 const SDLoc &DL, bool IsTailCall, 3017 SelectionDAG &DAG) const { 3018 if (!IsTailCall) { 3019 SDValue PtrOff = 3020 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr, 3021 DAG.getIntPtrConstant(Offset, DL)); 3022 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo()); 3023 } 3024 3025 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 3026 int FI = MFI.CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false); 3027 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 3028 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(), 3029 /* Alignment = */ 0, MachineMemOperand::MOVolatile); 3030 } 3031 3032 void MipsTargetLowering:: 3033 getOpndList(SmallVectorImpl<SDValue> &Ops, 3034 std::deque<std::pair<unsigned, SDValue>> &RegsToPass, 3035 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage, 3036 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee, 3037 SDValue Chain) const { 3038 // Insert node "GP copy globalreg" before call to function. 3039 // 3040 // R_MIPS_CALL* operators (emitted when non-internal functions are called 3041 // in PIC mode) allow symbols to be resolved via lazy binding. 3042 // The lazy binding stub requires GP to point to the GOT. 3043 // Note that we don't need GP to point to the GOT for indirect calls 3044 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates 3045 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs 3046 // used for the function (that is, Mips linker doesn't generate lazy binding 3047 // stub for a function whose address is taken in the program). 3048 if (IsPICCall && !InternalLinkage && IsCallReloc) { 3049 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP; 3050 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32; 3051 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty))); 3052 } 3053 3054 // Build a sequence of copy-to-reg nodes chained together with token 3055 // chain and flag operands which copy the outgoing args into registers. 3056 // The InFlag in necessary since all emitted instructions must be 3057 // stuck together. 3058 SDValue InFlag; 3059 3060 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 3061 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first, 3062 RegsToPass[i].second, InFlag); 3063 InFlag = Chain.getValue(1); 3064 } 3065 3066 // Add argument registers to the end of the list so that they are 3067 // known live into the call. 3068 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 3069 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first, 3070 RegsToPass[i].second.getValueType())); 3071 3072 // Add a register mask operand representing the call-preserved registers. 3073 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); 3074 const uint32_t *Mask = 3075 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv); 3076 assert(Mask && "Missing call preserved mask for calling convention"); 3077 if (Subtarget.inMips16HardFloat()) { 3078 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) { 3079 StringRef Sym = G->getGlobal()->getName(); 3080 Function *F = G->getGlobal()->getParent()->getFunction(Sym); 3081 if (F && F->hasFnAttribute("__Mips16RetHelper")) { 3082 Mask = MipsRegisterInfo::getMips16RetHelperMask(); 3083 } 3084 } 3085 } 3086 Ops.push_back(CLI.DAG.getRegisterMask(Mask)); 3087 3088 if (InFlag.getNode()) 3089 Ops.push_back(InFlag); 3090 } 3091 3092 void MipsTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, 3093 SDNode *Node) const { 3094 switch (MI.getOpcode()) { 3095 default: 3096 return; 3097 case Mips::JALR: 3098 case Mips::JALRPseudo: 3099 case Mips::JALR64: 3100 case Mips::JALR64Pseudo: 3101 case Mips::JALR16_MM: 3102 case Mips::JALRC16_MMR6: 3103 case Mips::TAILCALLREG: 3104 case Mips::TAILCALLREG64: 3105 case Mips::TAILCALLR6REG: 3106 case Mips::TAILCALL64R6REG: 3107 case Mips::TAILCALLREG_MM: 3108 case Mips::TAILCALLREG_MMR6: { 3109 if (!EmitJalrReloc || 3110 Subtarget.inMips16Mode() || 3111 !isPositionIndependent() || 3112 Node->getNumOperands() < 1 || 3113 Node->getOperand(0).getNumOperands() < 2) { 3114 return; 3115 } 3116 // We are after the callee address, set by LowerCall(). 3117 // If added to MI, asm printer will emit .reloc R_MIPS_JALR for the 3118 // symbol. 3119 const SDValue TargetAddr = Node->getOperand(0).getOperand(1); 3120 StringRef Sym; 3121 if (const GlobalAddressSDNode *G = 3122 dyn_cast_or_null<const GlobalAddressSDNode>(TargetAddr)) { 3123 // We must not emit the R_MIPS_JALR relocation against data symbols 3124 // since this will cause run-time crashes if the linker replaces the 3125 // call instruction with a relative branch to the data symbol. 3126 if (!isa<Function>(G->getGlobal())) { 3127 LLVM_DEBUG(dbgs() << "Not adding R_MIPS_JALR against data symbol " 3128 << G->getGlobal()->getName() << "\n"); 3129 return; 3130 } 3131 Sym = G->getGlobal()->getName(); 3132 } 3133 else if (const ExternalSymbolSDNode *ES = 3134 dyn_cast_or_null<const ExternalSymbolSDNode>(TargetAddr)) { 3135 Sym = ES->getSymbol(); 3136 } 3137 3138 if (Sym.empty()) 3139 return; 3140 3141 MachineFunction *MF = MI.getParent()->getParent(); 3142 MCSymbol *S = MF->getContext().getOrCreateSymbol(Sym); 3143 LLVM_DEBUG(dbgs() << "Adding R_MIPS_JALR against " << Sym << "\n"); 3144 MI.addOperand(MachineOperand::CreateMCSymbol(S, MipsII::MO_JALR)); 3145 } 3146 } 3147 } 3148 3149 /// LowerCall - functions arguments are copied from virtual regs to 3150 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted. 3151 SDValue 3152 MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 3153 SmallVectorImpl<SDValue> &InVals) const { 3154 SelectionDAG &DAG = CLI.DAG; 3155 SDLoc DL = CLI.DL; 3156 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 3157 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 3158 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 3159 SDValue Chain = CLI.Chain; 3160 SDValue Callee = CLI.Callee; 3161 bool &IsTailCall = CLI.IsTailCall; 3162 CallingConv::ID CallConv = CLI.CallConv; 3163 bool IsVarArg = CLI.IsVarArg; 3164 3165 MachineFunction &MF = DAG.getMachineFunction(); 3166 MachineFrameInfo &MFI = MF.getFrameInfo(); 3167 const TargetFrameLowering *TFL = Subtarget.getFrameLowering(); 3168 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>(); 3169 bool IsPIC = isPositionIndependent(); 3170 3171 // Analyze operands of the call, assigning locations to each operand. 3172 SmallVector<CCValAssign, 16> ArgLocs; 3173 MipsCCState CCInfo( 3174 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(), 3175 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget)); 3176 3177 const ExternalSymbolSDNode *ES = 3178 dyn_cast_or_null<const ExternalSymbolSDNode>(Callee.getNode()); 3179 3180 // There is one case where CALLSEQ_START..CALLSEQ_END can be nested, which 3181 // is during the lowering of a call with a byval argument which produces 3182 // a call to memcpy. For the O32 case, this causes the caller to allocate 3183 // stack space for the reserved argument area for the callee, then recursively 3184 // again for the memcpy call. In the NEWABI case, this doesn't occur as those 3185 // ABIs mandate that the callee allocates the reserved argument area. We do 3186 // still produce nested CALLSEQ_START..CALLSEQ_END with zero space though. 3187 // 3188 // If the callee has a byval argument and memcpy is used, we are mandated 3189 // to already have produced a reserved argument area for the callee for O32. 3190 // Therefore, the reserved argument area can be reused for both calls. 3191 // 3192 // Other cases of calling memcpy cannot have a chain with a CALLSEQ_START 3193 // present, as we have yet to hook that node onto the chain. 3194 // 3195 // Hence, the CALLSEQ_START and CALLSEQ_END nodes can be eliminated in this 3196 // case. GCC does a similar trick, in that wherever possible, it calculates 3197 // the maximum out going argument area (including the reserved area), and 3198 // preallocates the stack space on entrance to the caller. 3199 // 3200 // FIXME: We should do the same for efficiency and space. 3201 3202 // Note: The check on the calling convention below must match 3203 // MipsABIInfo::GetCalleeAllocdArgSizeInBytes(). 3204 bool MemcpyInByVal = ES && 3205 StringRef(ES->getSymbol()) == StringRef("memcpy") && 3206 CallConv != CallingConv::Fast && 3207 Chain.getOpcode() == ISD::CALLSEQ_START; 3208 3209 // Allocate the reserved argument area. It seems strange to do this from the 3210 // caller side but removing it breaks the frame size calculation. 3211 unsigned ReservedArgArea = 3212 MemcpyInByVal ? 0 : ABI.GetCalleeAllocdArgSizeInBytes(CallConv); 3213 CCInfo.AllocateStack(ReservedArgArea, Align(1)); 3214 3215 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), 3216 ES ? ES->getSymbol() : nullptr); 3217 3218 // Get a count of how many bytes are to be pushed on the stack. 3219 unsigned NextStackOffset = CCInfo.getNextStackOffset(); 3220 3221 // Call site info for function parameters tracking. 3222 MachineFunction::CallSiteInfo CSInfo; 3223 3224 // Check if it's really possible to do a tail call. Restrict it to functions 3225 // that are part of this compilation unit. 3226 bool InternalLinkage = false; 3227 if (IsTailCall) { 3228 IsTailCall = isEligibleForTailCallOptimization( 3229 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>()); 3230 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 3231 InternalLinkage = G->getGlobal()->hasInternalLinkage(); 3232 IsTailCall &= (InternalLinkage || G->getGlobal()->hasLocalLinkage() || 3233 G->getGlobal()->hasPrivateLinkage() || 3234 G->getGlobal()->hasHiddenVisibility() || 3235 G->getGlobal()->hasProtectedVisibility()); 3236 } 3237 } 3238 if (!IsTailCall && CLI.CB && CLI.CB->isMustTailCall()) 3239 report_fatal_error("failed to perform tail call elimination on a call " 3240 "site marked musttail"); 3241 3242 if (IsTailCall) 3243 ++NumTailCalls; 3244 3245 // Chain is the output chain of the last Load/Store or CopyToReg node. 3246 // ByValChain is the output chain of the last Memcpy node created for copying 3247 // byval arguments to the stack. 3248 unsigned StackAlignment = TFL->getStackAlignment(); 3249 NextStackOffset = alignTo(NextStackOffset, StackAlignment); 3250 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true); 3251 3252 if (!(IsTailCall || MemcpyInByVal)) 3253 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffset, 0, DL); 3254 3255 SDValue StackPtr = 3256 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP, 3257 getPointerTy(DAG.getDataLayout())); 3258 3259 std::deque<std::pair<unsigned, SDValue>> RegsToPass; 3260 SmallVector<SDValue, 8> MemOpChains; 3261 3262 CCInfo.rewindByValRegsInfo(); 3263 3264 // Walk the register/memloc assignments, inserting copies/loads. 3265 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3266 SDValue Arg = OutVals[i]; 3267 CCValAssign &VA = ArgLocs[i]; 3268 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT(); 3269 ISD::ArgFlagsTy Flags = Outs[i].Flags; 3270 bool UseUpperBits = false; 3271 3272 // ByVal Arg. 3273 if (Flags.isByVal()) { 3274 unsigned FirstByValReg, LastByValReg; 3275 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed(); 3276 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg); 3277 3278 assert(Flags.getByValSize() && 3279 "ByVal args of size 0 should have been ignored by front-end."); 3280 assert(ByValIdx < CCInfo.getInRegsParamsCount()); 3281 assert(!IsTailCall && 3282 "Do not tail-call optimize if there is a byval argument."); 3283 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg, 3284 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(), 3285 VA); 3286 CCInfo.nextInRegsParam(); 3287 continue; 3288 } 3289 3290 // Promote the value if needed. 3291 switch (VA.getLocInfo()) { 3292 default: 3293 llvm_unreachable("Unknown loc info!"); 3294 case CCValAssign::Full: 3295 if (VA.isRegLoc()) { 3296 if ((ValVT == MVT::f32 && LocVT == MVT::i32) || 3297 (ValVT == MVT::f64 && LocVT == MVT::i64) || 3298 (ValVT == MVT::i64 && LocVT == MVT::f64)) 3299 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg); 3300 else if (ValVT == MVT::f64 && LocVT == MVT::i32) { 3301 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, 3302 Arg, DAG.getConstant(0, DL, MVT::i32)); 3303 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, 3304 Arg, DAG.getConstant(1, DL, MVT::i32)); 3305 if (!Subtarget.isLittle()) 3306 std::swap(Lo, Hi); 3307 Register LocRegLo = VA.getLocReg(); 3308 unsigned LocRegHigh = getNextIntArgReg(LocRegLo); 3309 RegsToPass.push_back(std::make_pair(LocRegLo, Lo)); 3310 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi)); 3311 continue; 3312 } 3313 } 3314 break; 3315 case CCValAssign::BCvt: 3316 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg); 3317 break; 3318 case CCValAssign::SExtUpper: 3319 UseUpperBits = true; 3320 LLVM_FALLTHROUGH; 3321 case CCValAssign::SExt: 3322 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg); 3323 break; 3324 case CCValAssign::ZExtUpper: 3325 UseUpperBits = true; 3326 LLVM_FALLTHROUGH; 3327 case CCValAssign::ZExt: 3328 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg); 3329 break; 3330 case CCValAssign::AExtUpper: 3331 UseUpperBits = true; 3332 LLVM_FALLTHROUGH; 3333 case CCValAssign::AExt: 3334 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg); 3335 break; 3336 } 3337 3338 if (UseUpperBits) { 3339 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits(); 3340 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits(); 3341 Arg = DAG.getNode( 3342 ISD::SHL, DL, VA.getLocVT(), Arg, 3343 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT())); 3344 } 3345 3346 // Arguments that can be passed on register must be kept at 3347 // RegsToPass vector 3348 if (VA.isRegLoc()) { 3349 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 3350 3351 // If the parameter is passed through reg $D, which splits into 3352 // two physical registers, avoid creating call site info. 3353 if (Mips::AFGR64RegClass.contains(VA.getLocReg())) 3354 continue; 3355 3356 // Collect CSInfo about which register passes which parameter. 3357 const TargetOptions &Options = DAG.getTarget().Options; 3358 if (Options.SupportsDebugEntryValues) 3359 CSInfo.emplace_back(VA.getLocReg(), i); 3360 3361 continue; 3362 } 3363 3364 // Register can't get to this point... 3365 assert(VA.isMemLoc()); 3366 3367 // emit ISD::STORE whichs stores the 3368 // parameter value to a stack Location 3369 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(), 3370 Chain, Arg, DL, IsTailCall, DAG)); 3371 } 3372 3373 // Transform all store nodes into one single node because all store 3374 // nodes are independent of each other. 3375 if (!MemOpChains.empty()) 3376 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains); 3377 3378 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 3379 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 3380 // node so that legalize doesn't hack it. 3381 3382 EVT Ty = Callee.getValueType(); 3383 bool GlobalOrExternal = false, IsCallReloc = false; 3384 3385 // The long-calls feature is ignored in case of PIC. 3386 // While we do not support -mshared / -mno-shared properly, 3387 // ignore long-calls in case of -mabicalls too. 3388 if (!Subtarget.isABICalls() && !IsPIC) { 3389 // If the function should be called using "long call", 3390 // get its address into a register to prevent using 3391 // of the `jal` instruction for the direct call. 3392 if (auto *N = dyn_cast<ExternalSymbolSDNode>(Callee)) { 3393 if (Subtarget.useLongCalls()) 3394 Callee = Subtarget.hasSym32() 3395 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG) 3396 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG); 3397 } else if (auto *N = dyn_cast<GlobalAddressSDNode>(Callee)) { 3398 bool UseLongCalls = Subtarget.useLongCalls(); 3399 // If the function has long-call/far/near attribute 3400 // it overrides command line switch pased to the backend. 3401 if (auto *F = dyn_cast<Function>(N->getGlobal())) { 3402 if (F->hasFnAttribute("long-call")) 3403 UseLongCalls = true; 3404 else if (F->hasFnAttribute("short-call")) 3405 UseLongCalls = false; 3406 } 3407 if (UseLongCalls) 3408 Callee = Subtarget.hasSym32() 3409 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG) 3410 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG); 3411 } 3412 } 3413 3414 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 3415 if (IsPIC) { 3416 const GlobalValue *Val = G->getGlobal(); 3417 InternalLinkage = Val->hasInternalLinkage(); 3418 3419 if (InternalLinkage) 3420 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64()); 3421 else if (Subtarget.useXGOT()) { 3422 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16, 3423 MipsII::MO_CALL_LO16, Chain, 3424 FuncInfo->callPtrInfo(Val)); 3425 IsCallReloc = true; 3426 } else { 3427 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain, 3428 FuncInfo->callPtrInfo(Val)); 3429 IsCallReloc = true; 3430 } 3431 } else 3432 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, 3433 getPointerTy(DAG.getDataLayout()), 0, 3434 MipsII::MO_NO_FLAG); 3435 GlobalOrExternal = true; 3436 } 3437 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 3438 const char *Sym = S->getSymbol(); 3439 3440 if (!IsPIC) // static 3441 Callee = DAG.getTargetExternalSymbol( 3442 Sym, getPointerTy(DAG.getDataLayout()), MipsII::MO_NO_FLAG); 3443 else if (Subtarget.useXGOT()) { 3444 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16, 3445 MipsII::MO_CALL_LO16, Chain, 3446 FuncInfo->callPtrInfo(Sym)); 3447 IsCallReloc = true; 3448 } else { // PIC 3449 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain, 3450 FuncInfo->callPtrInfo(Sym)); 3451 IsCallReloc = true; 3452 } 3453 3454 GlobalOrExternal = true; 3455 } 3456 3457 SmallVector<SDValue, 8> Ops(1, Chain); 3458 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 3459 3460 getOpndList(Ops, RegsToPass, IsPIC, GlobalOrExternal, InternalLinkage, 3461 IsCallReloc, CLI, Callee, Chain); 3462 3463 if (IsTailCall) { 3464 MF.getFrameInfo().setHasTailCall(); 3465 SDValue Ret = DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops); 3466 DAG.addCallSiteInfo(Ret.getNode(), std::move(CSInfo)); 3467 return Ret; 3468 } 3469 3470 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops); 3471 SDValue InFlag = Chain.getValue(1); 3472 3473 DAG.addCallSiteInfo(Chain.getNode(), std::move(CSInfo)); 3474 3475 // Create the CALLSEQ_END node in the case of where it is not a call to 3476 // memcpy. 3477 if (!(MemcpyInByVal)) { 3478 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal, 3479 DAG.getIntPtrConstant(0, DL, true), InFlag, DL); 3480 InFlag = Chain.getValue(1); 3481 } 3482 3483 // Handle result values, copying them out of physregs into vregs that we 3484 // return. 3485 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG, 3486 InVals, CLI); 3487 } 3488 3489 /// LowerCallResult - Lower the result values of a call into the 3490 /// appropriate copies out of appropriate physical registers. 3491 SDValue MipsTargetLowering::LowerCallResult( 3492 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg, 3493 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 3494 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, 3495 TargetLowering::CallLoweringInfo &CLI) const { 3496 // Assign locations to each value returned by this call. 3497 SmallVector<CCValAssign, 16> RVLocs; 3498 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs, 3499 *DAG.getContext()); 3500 3501 const ExternalSymbolSDNode *ES = 3502 dyn_cast_or_null<const ExternalSymbolSDNode>(CLI.Callee.getNode()); 3503 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI.RetTy, 3504 ES ? ES->getSymbol() : nullptr); 3505 3506 // Copy all of the result registers out of their specified physreg. 3507 for (unsigned i = 0; i != RVLocs.size(); ++i) { 3508 CCValAssign &VA = RVLocs[i]; 3509 assert(VA.isRegLoc() && "Can only return in registers!"); 3510 3511 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(), 3512 RVLocs[i].getLocVT(), InFlag); 3513 Chain = Val.getValue(1); 3514 InFlag = Val.getValue(2); 3515 3516 if (VA.isUpperBitsInLoc()) { 3517 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits(); 3518 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits(); 3519 unsigned Shift = 3520 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA; 3521 Val = DAG.getNode( 3522 Shift, DL, VA.getLocVT(), Val, 3523 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT())); 3524 } 3525 3526 switch (VA.getLocInfo()) { 3527 default: 3528 llvm_unreachable("Unknown loc info!"); 3529 case CCValAssign::Full: 3530 break; 3531 case CCValAssign::BCvt: 3532 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val); 3533 break; 3534 case CCValAssign::AExt: 3535 case CCValAssign::AExtUpper: 3536 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val); 3537 break; 3538 case CCValAssign::ZExt: 3539 case CCValAssign::ZExtUpper: 3540 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val, 3541 DAG.getValueType(VA.getValVT())); 3542 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val); 3543 break; 3544 case CCValAssign::SExt: 3545 case CCValAssign::SExtUpper: 3546 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val, 3547 DAG.getValueType(VA.getValVT())); 3548 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val); 3549 break; 3550 } 3551 3552 InVals.push_back(Val); 3553 } 3554 3555 return Chain; 3556 } 3557 3558 static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA, 3559 EVT ArgVT, const SDLoc &DL, 3560 SelectionDAG &DAG) { 3561 MVT LocVT = VA.getLocVT(); 3562 EVT ValVT = VA.getValVT(); 3563 3564 // Shift into the upper bits if necessary. 3565 switch (VA.getLocInfo()) { 3566 default: 3567 break; 3568 case CCValAssign::AExtUpper: 3569 case CCValAssign::SExtUpper: 3570 case CCValAssign::ZExtUpper: { 3571 unsigned ValSizeInBits = ArgVT.getSizeInBits(); 3572 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits(); 3573 unsigned Opcode = 3574 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA; 3575 Val = DAG.getNode( 3576 Opcode, DL, VA.getLocVT(), Val, 3577 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT())); 3578 break; 3579 } 3580 } 3581 3582 // If this is an value smaller than the argument slot size (32-bit for O32, 3583 // 64-bit for N32/N64), it has been promoted in some way to the argument slot 3584 // size. Extract the value and insert any appropriate assertions regarding 3585 // sign/zero extension. 3586 switch (VA.getLocInfo()) { 3587 default: 3588 llvm_unreachable("Unknown loc info!"); 3589 case CCValAssign::Full: 3590 break; 3591 case CCValAssign::AExtUpper: 3592 case CCValAssign::AExt: 3593 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val); 3594 break; 3595 case CCValAssign::SExtUpper: 3596 case CCValAssign::SExt: 3597 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT)); 3598 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val); 3599 break; 3600 case CCValAssign::ZExtUpper: 3601 case CCValAssign::ZExt: 3602 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT)); 3603 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val); 3604 break; 3605 case CCValAssign::BCvt: 3606 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val); 3607 break; 3608 } 3609 3610 return Val; 3611 } 3612 3613 //===----------------------------------------------------------------------===// 3614 // Formal Arguments Calling Convention Implementation 3615 //===----------------------------------------------------------------------===// 3616 /// LowerFormalArguments - transform physical registers into virtual registers 3617 /// and generate load operations for arguments places on the stack. 3618 SDValue MipsTargetLowering::LowerFormalArguments( 3619 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 3620 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 3621 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 3622 MachineFunction &MF = DAG.getMachineFunction(); 3623 MachineFrameInfo &MFI = MF.getFrameInfo(); 3624 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 3625 3626 MipsFI->setVarArgsFrameIndex(0); 3627 3628 // Used with vargs to acumulate store chains. 3629 std::vector<SDValue> OutChains; 3630 3631 // Assign locations to all of the incoming arguments. 3632 SmallVector<CCValAssign, 16> ArgLocs; 3633 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, 3634 *DAG.getContext()); 3635 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), Align(1)); 3636 const Function &Func = DAG.getMachineFunction().getFunction(); 3637 Function::const_arg_iterator FuncArg = Func.arg_begin(); 3638 3639 if (Func.hasFnAttribute("interrupt") && !Func.arg_empty()) 3640 report_fatal_error( 3641 "Functions with the interrupt attribute cannot have arguments!"); 3642 3643 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg); 3644 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(), 3645 CCInfo.getInRegsParamsCount() > 0); 3646 3647 unsigned CurArgIdx = 0; 3648 CCInfo.rewindByValRegsInfo(); 3649 3650 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3651 CCValAssign &VA = ArgLocs[i]; 3652 if (Ins[i].isOrigArg()) { 3653 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx); 3654 CurArgIdx = Ins[i].getOrigArgIndex(); 3655 } 3656 EVT ValVT = VA.getValVT(); 3657 ISD::ArgFlagsTy Flags = Ins[i].Flags; 3658 bool IsRegLoc = VA.isRegLoc(); 3659 3660 if (Flags.isByVal()) { 3661 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit"); 3662 unsigned FirstByValReg, LastByValReg; 3663 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed(); 3664 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg); 3665 3666 assert(Flags.getByValSize() && 3667 "ByVal args of size 0 should have been ignored by front-end."); 3668 assert(ByValIdx < CCInfo.getInRegsParamsCount()); 3669 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg, 3670 FirstByValReg, LastByValReg, VA, CCInfo); 3671 CCInfo.nextInRegsParam(); 3672 continue; 3673 } 3674 3675 // Arguments stored on registers 3676 if (IsRegLoc) { 3677 MVT RegVT = VA.getLocVT(); 3678 Register ArgReg = VA.getLocReg(); 3679 const TargetRegisterClass *RC = getRegClassFor(RegVT); 3680 3681 // Transform the arguments stored on 3682 // physical registers into virtual ones 3683 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC); 3684 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT); 3685 3686 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG); 3687 3688 // Handle floating point arguments passed in integer registers and 3689 // long double arguments passed in floating point registers. 3690 if ((RegVT == MVT::i32 && ValVT == MVT::f32) || 3691 (RegVT == MVT::i64 && ValVT == MVT::f64) || 3692 (RegVT == MVT::f64 && ValVT == MVT::i64)) 3693 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue); 3694 else if (ABI.IsO32() && RegVT == MVT::i32 && 3695 ValVT == MVT::f64) { 3696 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(), 3697 getNextIntArgReg(ArgReg), RC); 3698 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT); 3699 if (!Subtarget.isLittle()) 3700 std::swap(ArgValue, ArgValue2); 3701 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, 3702 ArgValue, ArgValue2); 3703 } 3704 3705 InVals.push_back(ArgValue); 3706 } else { // VA.isRegLoc() 3707 MVT LocVT = VA.getLocVT(); 3708 3709 if (ABI.IsO32()) { 3710 // We ought to be able to use LocVT directly but O32 sets it to i32 3711 // when allocating floating point values to integer registers. 3712 // This shouldn't influence how we load the value into registers unless 3713 // we are targeting softfloat. 3714 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat()) 3715 LocVT = VA.getValVT(); 3716 } 3717 3718 // sanity check 3719 assert(VA.isMemLoc()); 3720 3721 // The stack pointer offset is relative to the caller stack frame. 3722 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8, 3723 VA.getLocMemOffset(), true); 3724 3725 // Create load nodes to retrieve arguments from the stack 3726 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 3727 SDValue ArgValue = DAG.getLoad( 3728 LocVT, DL, Chain, FIN, 3729 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)); 3730 OutChains.push_back(ArgValue.getValue(1)); 3731 3732 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG); 3733 3734 InVals.push_back(ArgValue); 3735 } 3736 } 3737 3738 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3739 // The mips ABIs for returning structs by value requires that we copy 3740 // the sret argument into $v0 for the return. Save the argument into 3741 // a virtual register so that we can access it from the return points. 3742 if (Ins[i].Flags.isSRet()) { 3743 unsigned Reg = MipsFI->getSRetReturnReg(); 3744 if (!Reg) { 3745 Reg = MF.getRegInfo().createVirtualRegister( 3746 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32)); 3747 MipsFI->setSRetReturnReg(Reg); 3748 } 3749 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]); 3750 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain); 3751 break; 3752 } 3753 } 3754 3755 if (IsVarArg) 3756 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo); 3757 3758 // All stores are grouped in one node to allow the matching between 3759 // the size of Ins and InVals. This only happens when on varg functions 3760 if (!OutChains.empty()) { 3761 OutChains.push_back(Chain); 3762 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains); 3763 } 3764 3765 return Chain; 3766 } 3767 3768 //===----------------------------------------------------------------------===// 3769 // Return Value Calling Convention Implementation 3770 //===----------------------------------------------------------------------===// 3771 3772 bool 3773 MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 3774 MachineFunction &MF, bool IsVarArg, 3775 const SmallVectorImpl<ISD::OutputArg> &Outs, 3776 LLVMContext &Context) const { 3777 SmallVector<CCValAssign, 16> RVLocs; 3778 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context); 3779 return CCInfo.CheckReturn(Outs, RetCC_Mips); 3780 } 3781 3782 bool MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, 3783 bool IsSigned) const { 3784 if ((ABI.IsN32() || ABI.IsN64()) && Type == MVT::i32) 3785 return true; 3786 3787 return IsSigned; 3788 } 3789 3790 SDValue 3791 MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps, 3792 const SDLoc &DL, 3793 SelectionDAG &DAG) const { 3794 MachineFunction &MF = DAG.getMachineFunction(); 3795 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 3796 3797 MipsFI->setISR(); 3798 3799 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps); 3800 } 3801 3802 SDValue 3803 MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 3804 bool IsVarArg, 3805 const SmallVectorImpl<ISD::OutputArg> &Outs, 3806 const SmallVectorImpl<SDValue> &OutVals, 3807 const SDLoc &DL, SelectionDAG &DAG) const { 3808 // CCValAssign - represent the assignment of 3809 // the return value to a location 3810 SmallVector<CCValAssign, 16> RVLocs; 3811 MachineFunction &MF = DAG.getMachineFunction(); 3812 3813 // CCState - Info about the registers and stack slot. 3814 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext()); 3815 3816 // Analyze return values. 3817 CCInfo.AnalyzeReturn(Outs, RetCC_Mips); 3818 3819 SDValue Flag; 3820 SmallVector<SDValue, 4> RetOps(1, Chain); 3821 3822 // Copy the result values into the output registers. 3823 for (unsigned i = 0; i != RVLocs.size(); ++i) { 3824 SDValue Val = OutVals[i]; 3825 CCValAssign &VA = RVLocs[i]; 3826 assert(VA.isRegLoc() && "Can only return in registers!"); 3827 bool UseUpperBits = false; 3828 3829 switch (VA.getLocInfo()) { 3830 default: 3831 llvm_unreachable("Unknown loc info!"); 3832 case CCValAssign::Full: 3833 break; 3834 case CCValAssign::BCvt: 3835 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val); 3836 break; 3837 case CCValAssign::AExtUpper: 3838 UseUpperBits = true; 3839 LLVM_FALLTHROUGH; 3840 case CCValAssign::AExt: 3841 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val); 3842 break; 3843 case CCValAssign::ZExtUpper: 3844 UseUpperBits = true; 3845 LLVM_FALLTHROUGH; 3846 case CCValAssign::ZExt: 3847 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val); 3848 break; 3849 case CCValAssign::SExtUpper: 3850 UseUpperBits = true; 3851 LLVM_FALLTHROUGH; 3852 case CCValAssign::SExt: 3853 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val); 3854 break; 3855 } 3856 3857 if (UseUpperBits) { 3858 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits(); 3859 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits(); 3860 Val = DAG.getNode( 3861 ISD::SHL, DL, VA.getLocVT(), Val, 3862 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT())); 3863 } 3864 3865 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag); 3866 3867 // Guarantee that all emitted copies are stuck together with flags. 3868 Flag = Chain.getValue(1); 3869 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 3870 } 3871 3872 // The mips ABIs for returning structs by value requires that we copy 3873 // the sret argument into $v0 for the return. We saved the argument into 3874 // a virtual register in the entry block, so now we copy the value out 3875 // and into $v0. 3876 if (MF.getFunction().hasStructRetAttr()) { 3877 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 3878 unsigned Reg = MipsFI->getSRetReturnReg(); 3879 3880 if (!Reg) 3881 llvm_unreachable("sret virtual register not created in the entry block"); 3882 SDValue Val = 3883 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout())); 3884 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0; 3885 3886 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag); 3887 Flag = Chain.getValue(1); 3888 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout()))); 3889 } 3890 3891 RetOps[0] = Chain; // Update chain. 3892 3893 // Add the flag if we have it. 3894 if (Flag.getNode()) 3895 RetOps.push_back(Flag); 3896 3897 // ISRs must use "eret". 3898 if (DAG.getMachineFunction().getFunction().hasFnAttribute("interrupt")) 3899 return LowerInterruptReturn(RetOps, DL, DAG); 3900 3901 // Standard return on Mips is a "jr $ra" 3902 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps); 3903 } 3904 3905 //===----------------------------------------------------------------------===// 3906 // Mips Inline Assembly Support 3907 //===----------------------------------------------------------------------===// 3908 3909 /// getConstraintType - Given a constraint letter, return the type of 3910 /// constraint it is for this target. 3911 MipsTargetLowering::ConstraintType 3912 MipsTargetLowering::getConstraintType(StringRef Constraint) const { 3913 // Mips specific constraints 3914 // GCC config/mips/constraints.md 3915 // 3916 // 'd' : An address register. Equivalent to r 3917 // unless generating MIPS16 code. 3918 // 'y' : Equivalent to r; retained for 3919 // backwards compatibility. 3920 // 'c' : A register suitable for use in an indirect 3921 // jump. This will always be $25 for -mabicalls. 3922 // 'l' : The lo register. 1 word storage. 3923 // 'x' : The hilo register pair. Double word storage. 3924 if (Constraint.size() == 1) { 3925 switch (Constraint[0]) { 3926 default : break; 3927 case 'd': 3928 case 'y': 3929 case 'f': 3930 case 'c': 3931 case 'l': 3932 case 'x': 3933 return C_RegisterClass; 3934 case 'R': 3935 return C_Memory; 3936 } 3937 } 3938 3939 if (Constraint == "ZC") 3940 return C_Memory; 3941 3942 return TargetLowering::getConstraintType(Constraint); 3943 } 3944 3945 /// Examine constraint type and operand type and determine a weight value. 3946 /// This object must already have been set up with the operand type 3947 /// and the current alternative constraint selected. 3948 TargetLowering::ConstraintWeight 3949 MipsTargetLowering::getSingleConstraintMatchWeight( 3950 AsmOperandInfo &info, const char *constraint) const { 3951 ConstraintWeight weight = CW_Invalid; 3952 Value *CallOperandVal = info.CallOperandVal; 3953 // If we don't have a value, we can't do a match, 3954 // but allow it at the lowest weight. 3955 if (!CallOperandVal) 3956 return CW_Default; 3957 Type *type = CallOperandVal->getType(); 3958 // Look at the constraint type. 3959 switch (*constraint) { 3960 default: 3961 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 3962 break; 3963 case 'd': 3964 case 'y': 3965 if (type->isIntegerTy()) 3966 weight = CW_Register; 3967 break; 3968 case 'f': // FPU or MSA register 3969 if (Subtarget.hasMSA() && type->isVectorTy() && 3970 type->getPrimitiveSizeInBits().getFixedSize() == 128) 3971 weight = CW_Register; 3972 else if (type->isFloatTy()) 3973 weight = CW_Register; 3974 break; 3975 case 'c': // $25 for indirect jumps 3976 case 'l': // lo register 3977 case 'x': // hilo register pair 3978 if (type->isIntegerTy()) 3979 weight = CW_SpecificReg; 3980 break; 3981 case 'I': // signed 16 bit immediate 3982 case 'J': // integer zero 3983 case 'K': // unsigned 16 bit immediate 3984 case 'L': // signed 32 bit immediate where lower 16 bits are 0 3985 case 'N': // immediate in the range of -65535 to -1 (inclusive) 3986 case 'O': // signed 15 bit immediate (+- 16383) 3987 case 'P': // immediate in the range of 65535 to 1 (inclusive) 3988 if (isa<ConstantInt>(CallOperandVal)) 3989 weight = CW_Constant; 3990 break; 3991 case 'R': 3992 weight = CW_Memory; 3993 break; 3994 } 3995 return weight; 3996 } 3997 3998 /// This is a helper function to parse a physical register string and split it 3999 /// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag 4000 /// that is returned indicates whether parsing was successful. The second flag 4001 /// is true if the numeric part exists. 4002 static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix, 4003 unsigned long long &Reg) { 4004 if (C.front() != '{' || C.back() != '}') 4005 return std::make_pair(false, false); 4006 4007 // Search for the first numeric character. 4008 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1; 4009 I = std::find_if(B, E, isdigit); 4010 4011 Prefix = StringRef(B, I - B); 4012 4013 // The second flag is set to false if no numeric characters were found. 4014 if (I == E) 4015 return std::make_pair(true, false); 4016 4017 // Parse the numeric characters. 4018 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg), 4019 true); 4020 } 4021 4022 EVT MipsTargetLowering::getTypeForExtReturn(LLVMContext &Context, EVT VT, 4023 ISD::NodeType) const { 4024 bool Cond = !Subtarget.isABI_O32() && VT.getSizeInBits() == 32; 4025 EVT MinVT = getRegisterType(Context, Cond ? MVT::i64 : MVT::i32); 4026 return VT.bitsLT(MinVT) ? MinVT : VT; 4027 } 4028 4029 std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering:: 4030 parseRegForInlineAsmConstraint(StringRef C, MVT VT) const { 4031 const TargetRegisterInfo *TRI = 4032 Subtarget.getRegisterInfo(); 4033 const TargetRegisterClass *RC; 4034 StringRef Prefix; 4035 unsigned long long Reg; 4036 4037 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg); 4038 4039 if (!R.first) 4040 return std::make_pair(0U, nullptr); 4041 4042 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo. 4043 // No numeric characters follow "hi" or "lo". 4044 if (R.second) 4045 return std::make_pair(0U, nullptr); 4046 4047 RC = TRI->getRegClass(Prefix == "hi" ? 4048 Mips::HI32RegClassID : Mips::LO32RegClassID); 4049 return std::make_pair(*(RC->begin()), RC); 4050 } else if (Prefix.startswith("$msa")) { 4051 // Parse $msa(ir|csr|access|save|modify|request|map|unmap) 4052 4053 // No numeric characters follow the name. 4054 if (R.second) 4055 return std::make_pair(0U, nullptr); 4056 4057 Reg = StringSwitch<unsigned long long>(Prefix) 4058 .Case("$msair", Mips::MSAIR) 4059 .Case("$msacsr", Mips::MSACSR) 4060 .Case("$msaaccess", Mips::MSAAccess) 4061 .Case("$msasave", Mips::MSASave) 4062 .Case("$msamodify", Mips::MSAModify) 4063 .Case("$msarequest", Mips::MSARequest) 4064 .Case("$msamap", Mips::MSAMap) 4065 .Case("$msaunmap", Mips::MSAUnmap) 4066 .Default(0); 4067 4068 if (!Reg) 4069 return std::make_pair(0U, nullptr); 4070 4071 RC = TRI->getRegClass(Mips::MSACtrlRegClassID); 4072 return std::make_pair(Reg, RC); 4073 } 4074 4075 if (!R.second) 4076 return std::make_pair(0U, nullptr); 4077 4078 if (Prefix == "$f") { // Parse $f0-$f31. 4079 // If the size of FP registers is 64-bit or Reg is an even number, select 4080 // the 64-bit register class. Otherwise, select the 32-bit register class. 4081 if (VT == MVT::Other) 4082 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32; 4083 4084 RC = getRegClassFor(VT); 4085 4086 if (RC == &Mips::AFGR64RegClass) { 4087 assert(Reg % 2 == 0); 4088 Reg >>= 1; 4089 } 4090 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7. 4091 RC = TRI->getRegClass(Mips::FCCRegClassID); 4092 else if (Prefix == "$w") { // Parse $w0-$w31. 4093 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT); 4094 } else { // Parse $0-$31. 4095 assert(Prefix == "$"); 4096 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT); 4097 } 4098 4099 assert(Reg < RC->getNumRegs()); 4100 return std::make_pair(*(RC->begin() + Reg), RC); 4101 } 4102 4103 /// Given a register class constraint, like 'r', if this corresponds directly 4104 /// to an LLVM register class, return a register of 0 and the register class 4105 /// pointer. 4106 std::pair<unsigned, const TargetRegisterClass *> 4107 MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 4108 StringRef Constraint, 4109 MVT VT) const { 4110 if (Constraint.size() == 1) { 4111 switch (Constraint[0]) { 4112 case 'd': // Address register. Same as 'r' unless generating MIPS16 code. 4113 case 'y': // Same as 'r'. Exists for compatibility. 4114 case 'r': 4115 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) { 4116 if (Subtarget.inMips16Mode()) 4117 return std::make_pair(0U, &Mips::CPU16RegsRegClass); 4118 return std::make_pair(0U, &Mips::GPR32RegClass); 4119 } 4120 if (VT == MVT::i64 && !Subtarget.isGP64bit()) 4121 return std::make_pair(0U, &Mips::GPR32RegClass); 4122 if (VT == MVT::i64 && Subtarget.isGP64bit()) 4123 return std::make_pair(0U, &Mips::GPR64RegClass); 4124 // This will generate an error message 4125 return std::make_pair(0U, nullptr); 4126 case 'f': // FPU or MSA register 4127 if (VT == MVT::v16i8) 4128 return std::make_pair(0U, &Mips::MSA128BRegClass); 4129 else if (VT == MVT::v8i16 || VT == MVT::v8f16) 4130 return std::make_pair(0U, &Mips::MSA128HRegClass); 4131 else if (VT == MVT::v4i32 || VT == MVT::v4f32) 4132 return std::make_pair(0U, &Mips::MSA128WRegClass); 4133 else if (VT == MVT::v2i64 || VT == MVT::v2f64) 4134 return std::make_pair(0U, &Mips::MSA128DRegClass); 4135 else if (VT == MVT::f32) 4136 return std::make_pair(0U, &Mips::FGR32RegClass); 4137 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) { 4138 if (Subtarget.isFP64bit()) 4139 return std::make_pair(0U, &Mips::FGR64RegClass); 4140 return std::make_pair(0U, &Mips::AFGR64RegClass); 4141 } 4142 break; 4143 case 'c': // register suitable for indirect jump 4144 if (VT == MVT::i32) 4145 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass); 4146 if (VT == MVT::i64) 4147 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass); 4148 // This will generate an error message 4149 return std::make_pair(0U, nullptr); 4150 case 'l': // use the `lo` register to store values 4151 // that are no bigger than a word 4152 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) 4153 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass); 4154 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass); 4155 case 'x': // use the concatenated `hi` and `lo` registers 4156 // to store doubleword values 4157 // Fixme: Not triggering the use of both hi and low 4158 // This will generate an error message 4159 return std::make_pair(0U, nullptr); 4160 } 4161 } 4162 4163 if (!Constraint.empty()) { 4164 std::pair<unsigned, const TargetRegisterClass *> R; 4165 R = parseRegForInlineAsmConstraint(Constraint, VT); 4166 4167 if (R.second) 4168 return R; 4169 } 4170 4171 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 4172 } 4173 4174 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 4175 /// vector. If it is invalid, don't add anything to Ops. 4176 void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 4177 std::string &Constraint, 4178 std::vector<SDValue>&Ops, 4179 SelectionDAG &DAG) const { 4180 SDLoc DL(Op); 4181 SDValue Result; 4182 4183 // Only support length 1 constraints for now. 4184 if (Constraint.length() > 1) return; 4185 4186 char ConstraintLetter = Constraint[0]; 4187 switch (ConstraintLetter) { 4188 default: break; // This will fall through to the generic implementation 4189 case 'I': // Signed 16 bit constant 4190 // If this fails, the parent routine will give an error 4191 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4192 EVT Type = Op.getValueType(); 4193 int64_t Val = C->getSExtValue(); 4194 if (isInt<16>(Val)) { 4195 Result = DAG.getTargetConstant(Val, DL, Type); 4196 break; 4197 } 4198 } 4199 return; 4200 case 'J': // integer zero 4201 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4202 EVT Type = Op.getValueType(); 4203 int64_t Val = C->getZExtValue(); 4204 if (Val == 0) { 4205 Result = DAG.getTargetConstant(0, DL, Type); 4206 break; 4207 } 4208 } 4209 return; 4210 case 'K': // unsigned 16 bit immediate 4211 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4212 EVT Type = Op.getValueType(); 4213 uint64_t Val = (uint64_t)C->getZExtValue(); 4214 if (isUInt<16>(Val)) { 4215 Result = DAG.getTargetConstant(Val, DL, Type); 4216 break; 4217 } 4218 } 4219 return; 4220 case 'L': // signed 32 bit immediate where lower 16 bits are 0 4221 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4222 EVT Type = Op.getValueType(); 4223 int64_t Val = C->getSExtValue(); 4224 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){ 4225 Result = DAG.getTargetConstant(Val, DL, Type); 4226 break; 4227 } 4228 } 4229 return; 4230 case 'N': // immediate in the range of -65535 to -1 (inclusive) 4231 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4232 EVT Type = Op.getValueType(); 4233 int64_t Val = C->getSExtValue(); 4234 if ((Val >= -65535) && (Val <= -1)) { 4235 Result = DAG.getTargetConstant(Val, DL, Type); 4236 break; 4237 } 4238 } 4239 return; 4240 case 'O': // signed 15 bit immediate 4241 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4242 EVT Type = Op.getValueType(); 4243 int64_t Val = C->getSExtValue(); 4244 if ((isInt<15>(Val))) { 4245 Result = DAG.getTargetConstant(Val, DL, Type); 4246 break; 4247 } 4248 } 4249 return; 4250 case 'P': // immediate in the range of 1 to 65535 (inclusive) 4251 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 4252 EVT Type = Op.getValueType(); 4253 int64_t Val = C->getSExtValue(); 4254 if ((Val <= 65535) && (Val >= 1)) { 4255 Result = DAG.getTargetConstant(Val, DL, Type); 4256 break; 4257 } 4258 } 4259 return; 4260 } 4261 4262 if (Result.getNode()) { 4263 Ops.push_back(Result); 4264 return; 4265 } 4266 4267 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 4268 } 4269 4270 bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL, 4271 const AddrMode &AM, Type *Ty, 4272 unsigned AS, 4273 Instruction *I) const { 4274 // No global is ever allowed as a base. 4275 if (AM.BaseGV) 4276 return false; 4277 4278 switch (AM.Scale) { 4279 case 0: // "r+i" or just "i", depending on HasBaseReg. 4280 break; 4281 case 1: 4282 if (!AM.HasBaseReg) // allow "r+i". 4283 break; 4284 return false; // disallow "r+r" or "r+r+i". 4285 default: 4286 return false; 4287 } 4288 4289 return true; 4290 } 4291 4292 bool 4293 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 4294 // The Mips target isn't yet aware of offsets. 4295 return false; 4296 } 4297 4298 EVT MipsTargetLowering::getOptimalMemOpType( 4299 const MemOp &Op, const AttributeList &FuncAttributes) const { 4300 if (Subtarget.hasMips64()) 4301 return MVT::i64; 4302 4303 return MVT::i32; 4304 } 4305 4306 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT, 4307 bool ForCodeSize) const { 4308 if (VT != MVT::f32 && VT != MVT::f64) 4309 return false; 4310 if (Imm.isNegZero()) 4311 return false; 4312 return Imm.isZero(); 4313 } 4314 4315 unsigned MipsTargetLowering::getJumpTableEncoding() const { 4316 4317 // FIXME: For space reasons this should be: EK_GPRel32BlockAddress. 4318 if (ABI.IsN64() && isPositionIndependent()) 4319 return MachineJumpTableInfo::EK_GPRel64BlockAddress; 4320 4321 return TargetLowering::getJumpTableEncoding(); 4322 } 4323 4324 bool MipsTargetLowering::useSoftFloat() const { 4325 return Subtarget.useSoftFloat(); 4326 } 4327 4328 void MipsTargetLowering::copyByValRegs( 4329 SDValue Chain, const SDLoc &DL, std::vector<SDValue> &OutChains, 4330 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags, 4331 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg, 4332 unsigned FirstReg, unsigned LastReg, const CCValAssign &VA, 4333 MipsCCState &State) const { 4334 MachineFunction &MF = DAG.getMachineFunction(); 4335 MachineFrameInfo &MFI = MF.getFrameInfo(); 4336 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes(); 4337 unsigned NumRegs = LastReg - FirstReg; 4338 unsigned RegAreaSize = NumRegs * GPRSizeInBytes; 4339 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize); 4340 int FrameObjOffset; 4341 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs(); 4342 4343 if (RegAreaSize) 4344 FrameObjOffset = 4345 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) - 4346 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes); 4347 else 4348 FrameObjOffset = VA.getLocMemOffset(); 4349 4350 // Create frame object. 4351 EVT PtrTy = getPointerTy(DAG.getDataLayout()); 4352 // Make the fixed object stored to mutable so that the load instructions 4353 // referencing it have their memory dependencies added. 4354 // Set the frame object as isAliased which clears the underlying objects 4355 // vector in ScheduleDAGInstrs::buildSchedGraph() resulting in addition of all 4356 // stores as dependencies for loads referencing this fixed object. 4357 int FI = MFI.CreateFixedObject(FrameObjSize, FrameObjOffset, false, true); 4358 SDValue FIN = DAG.getFrameIndex(FI, PtrTy); 4359 InVals.push_back(FIN); 4360 4361 if (!NumRegs) 4362 return; 4363 4364 // Copy arg registers. 4365 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8); 4366 const TargetRegisterClass *RC = getRegClassFor(RegTy); 4367 4368 for (unsigned I = 0; I < NumRegs; ++I) { 4369 unsigned ArgReg = ByValArgRegs[FirstReg + I]; 4370 unsigned VReg = addLiveIn(MF, ArgReg, RC); 4371 unsigned Offset = I * GPRSizeInBytes; 4372 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN, 4373 DAG.getConstant(Offset, DL, PtrTy)); 4374 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy), 4375 StorePtr, MachinePointerInfo(FuncArg, Offset)); 4376 OutChains.push_back(Store); 4377 } 4378 } 4379 4380 // Copy byVal arg to registers and stack. 4381 void MipsTargetLowering::passByValArg( 4382 SDValue Chain, const SDLoc &DL, 4383 std::deque<std::pair<unsigned, SDValue>> &RegsToPass, 4384 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr, 4385 MachineFrameInfo &MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg, 4386 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle, 4387 const CCValAssign &VA) const { 4388 unsigned ByValSizeInBytes = Flags.getByValSize(); 4389 unsigned OffsetInBytes = 0; // From beginning of struct 4390 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes(); 4391 Align Alignment = 4392 std::min(Flags.getNonZeroByValAlign(), Align(RegSizeInBytes)); 4393 EVT PtrTy = getPointerTy(DAG.getDataLayout()), 4394 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8); 4395 unsigned NumRegs = LastReg - FirstReg; 4396 4397 if (NumRegs) { 4398 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs(); 4399 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes); 4400 unsigned I = 0; 4401 4402 // Copy words to registers. 4403 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) { 4404 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg, 4405 DAG.getConstant(OffsetInBytes, DL, PtrTy)); 4406 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr, 4407 MachinePointerInfo(), Alignment.value()); 4408 MemOpChains.push_back(LoadVal.getValue(1)); 4409 unsigned ArgReg = ArgRegs[FirstReg + I]; 4410 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal)); 4411 } 4412 4413 // Return if the struct has been fully copied. 4414 if (ByValSizeInBytes == OffsetInBytes) 4415 return; 4416 4417 // Copy the remainder of the byval argument with sub-word loads and shifts. 4418 if (LeftoverBytes) { 4419 SDValue Val; 4420 4421 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0; 4422 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) { 4423 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes; 4424 4425 if (RemainingSizeInBytes < LoadSizeInBytes) 4426 continue; 4427 4428 // Load subword. 4429 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg, 4430 DAG.getConstant(OffsetInBytes, DL, 4431 PtrTy)); 4432 SDValue LoadVal = DAG.getExtLoad( 4433 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(), 4434 MVT::getIntegerVT(LoadSizeInBytes * 8), Alignment.value()); 4435 MemOpChains.push_back(LoadVal.getValue(1)); 4436 4437 // Shift the loaded value. 4438 unsigned Shamt; 4439 4440 if (isLittle) 4441 Shamt = TotalBytesLoaded * 8; 4442 else 4443 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8; 4444 4445 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal, 4446 DAG.getConstant(Shamt, DL, MVT::i32)); 4447 4448 if (Val.getNode()) 4449 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift); 4450 else 4451 Val = Shift; 4452 4453 OffsetInBytes += LoadSizeInBytes; 4454 TotalBytesLoaded += LoadSizeInBytes; 4455 Alignment = std::min(Alignment, Align(LoadSizeInBytes)); 4456 } 4457 4458 unsigned ArgReg = ArgRegs[FirstReg + I]; 4459 RegsToPass.push_back(std::make_pair(ArgReg, Val)); 4460 return; 4461 } 4462 } 4463 4464 // Copy remainder of byval arg to it with memcpy. 4465 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes; 4466 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg, 4467 DAG.getConstant(OffsetInBytes, DL, PtrTy)); 4468 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr, 4469 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL)); 4470 Chain = DAG.getMemcpy( 4471 Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, DL, PtrTy), 4472 Align(Alignment), /*isVolatile=*/false, /*AlwaysInline=*/false, 4473 /*isTailCall=*/false, MachinePointerInfo(), MachinePointerInfo()); 4474 MemOpChains.push_back(Chain); 4475 } 4476 4477 void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains, 4478 SDValue Chain, const SDLoc &DL, 4479 SelectionDAG &DAG, 4480 CCState &State) const { 4481 ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs(); 4482 unsigned Idx = State.getFirstUnallocated(ArgRegs); 4483 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes(); 4484 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8); 4485 const TargetRegisterClass *RC = getRegClassFor(RegTy); 4486 MachineFunction &MF = DAG.getMachineFunction(); 4487 MachineFrameInfo &MFI = MF.getFrameInfo(); 4488 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 4489 4490 // Offset of the first variable argument from stack pointer. 4491 int VaArgOffset; 4492 4493 if (ArgRegs.size() == Idx) 4494 VaArgOffset = alignTo(State.getNextStackOffset(), RegSizeInBytes); 4495 else { 4496 VaArgOffset = 4497 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) - 4498 (int)(RegSizeInBytes * (ArgRegs.size() - Idx)); 4499 } 4500 4501 // Record the frame index of the first variable argument 4502 // which is a value necessary to VASTART. 4503 int FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true); 4504 MipsFI->setVarArgsFrameIndex(FI); 4505 4506 // Copy the integer registers that have not been used for argument passing 4507 // to the argument register save area. For O32, the save area is allocated 4508 // in the caller's stack frame, while for N32/64, it is allocated in the 4509 // callee's stack frame. 4510 for (unsigned I = Idx; I < ArgRegs.size(); 4511 ++I, VaArgOffset += RegSizeInBytes) { 4512 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC); 4513 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy); 4514 FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true); 4515 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 4516 SDValue Store = 4517 DAG.getStore(Chain, DL, ArgValue, PtrOff, MachinePointerInfo()); 4518 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue( 4519 (Value *)nullptr); 4520 OutChains.push_back(Store); 4521 } 4522 } 4523 4524 void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size, 4525 Align Alignment) const { 4526 const TargetFrameLowering *TFL = Subtarget.getFrameLowering(); 4527 4528 assert(Size && "Byval argument's size shouldn't be 0."); 4529 4530 Alignment = std::min(Alignment, TFL->getStackAlign()); 4531 4532 unsigned FirstReg = 0; 4533 unsigned NumRegs = 0; 4534 4535 if (State->getCallingConv() != CallingConv::Fast) { 4536 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes(); 4537 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs(); 4538 // FIXME: The O32 case actually describes no shadow registers. 4539 const MCPhysReg *ShadowRegs = 4540 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs; 4541 4542 // We used to check the size as well but we can't do that anymore since 4543 // CCState::HandleByVal() rounds up the size after calling this function. 4544 assert( 4545 Alignment >= Align(RegSizeInBytes) && 4546 "Byval argument's alignment should be a multiple of RegSizeInBytes."); 4547 4548 FirstReg = State->getFirstUnallocated(IntArgRegs); 4549 4550 // If Alignment > RegSizeInBytes, the first arg register must be even. 4551 // FIXME: This condition happens to do the right thing but it's not the 4552 // right way to test it. We want to check that the stack frame offset 4553 // of the register is aligned. 4554 if ((Alignment > RegSizeInBytes) && (FirstReg % 2)) { 4555 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]); 4556 ++FirstReg; 4557 } 4558 4559 // Mark the registers allocated. 4560 Size = alignTo(Size, RegSizeInBytes); 4561 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size()); 4562 Size -= RegSizeInBytes, ++I, ++NumRegs) 4563 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]); 4564 } 4565 4566 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs); 4567 } 4568 4569 MachineBasicBlock *MipsTargetLowering::emitPseudoSELECT(MachineInstr &MI, 4570 MachineBasicBlock *BB, 4571 bool isFPCmp, 4572 unsigned Opc) const { 4573 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) && 4574 "Subtarget already supports SELECT nodes with the use of" 4575 "conditional-move instructions."); 4576 4577 const TargetInstrInfo *TII = 4578 Subtarget.getInstrInfo(); 4579 DebugLoc DL = MI.getDebugLoc(); 4580 4581 // To "insert" a SELECT instruction, we actually have to insert the 4582 // diamond control-flow pattern. The incoming instruction knows the 4583 // destination vreg to set, the condition code register to branch on, the 4584 // true/false values to select between, and a branch opcode to use. 4585 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 4586 MachineFunction::iterator It = ++BB->getIterator(); 4587 4588 // thisMBB: 4589 // ... 4590 // TrueVal = ... 4591 // setcc r1, r2, r3 4592 // bNE r1, r0, copy1MBB 4593 // fallthrough --> copy0MBB 4594 MachineBasicBlock *thisMBB = BB; 4595 MachineFunction *F = BB->getParent(); 4596 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 4597 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 4598 F->insert(It, copy0MBB); 4599 F->insert(It, sinkMBB); 4600 4601 // Transfer the remainder of BB and its successor edges to sinkMBB. 4602 sinkMBB->splice(sinkMBB->begin(), BB, 4603 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 4604 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 4605 4606 // Next, add the true and fallthrough blocks as its successors. 4607 BB->addSuccessor(copy0MBB); 4608 BB->addSuccessor(sinkMBB); 4609 4610 if (isFPCmp) { 4611 // bc1[tf] cc, sinkMBB 4612 BuildMI(BB, DL, TII->get(Opc)) 4613 .addReg(MI.getOperand(1).getReg()) 4614 .addMBB(sinkMBB); 4615 } else { 4616 // bne rs, $0, sinkMBB 4617 BuildMI(BB, DL, TII->get(Opc)) 4618 .addReg(MI.getOperand(1).getReg()) 4619 .addReg(Mips::ZERO) 4620 .addMBB(sinkMBB); 4621 } 4622 4623 // copy0MBB: 4624 // %FalseValue = ... 4625 // # fallthrough to sinkMBB 4626 BB = copy0MBB; 4627 4628 // Update machine-CFG edges 4629 BB->addSuccessor(sinkMBB); 4630 4631 // sinkMBB: 4632 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ] 4633 // ... 4634 BB = sinkMBB; 4635 4636 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg()) 4637 .addReg(MI.getOperand(2).getReg()) 4638 .addMBB(thisMBB) 4639 .addReg(MI.getOperand(3).getReg()) 4640 .addMBB(copy0MBB); 4641 4642 MI.eraseFromParent(); // The pseudo instruction is gone now. 4643 4644 return BB; 4645 } 4646 4647 MachineBasicBlock * 4648 MipsTargetLowering::emitPseudoD_SELECT(MachineInstr &MI, 4649 MachineBasicBlock *BB) const { 4650 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) && 4651 "Subtarget already supports SELECT nodes with the use of" 4652 "conditional-move instructions."); 4653 4654 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 4655 DebugLoc DL = MI.getDebugLoc(); 4656 4657 // D_SELECT substitutes two SELECT nodes that goes one after another and 4658 // have the same condition operand. On machines which don't have 4659 // conditional-move instruction, it reduces unnecessary branch instructions 4660 // which are result of using two diamond patterns that are result of two 4661 // SELECT pseudo instructions. 4662 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 4663 MachineFunction::iterator It = ++BB->getIterator(); 4664 4665 // thisMBB: 4666 // ... 4667 // TrueVal = ... 4668 // setcc r1, r2, r3 4669 // bNE r1, r0, copy1MBB 4670 // fallthrough --> copy0MBB 4671 MachineBasicBlock *thisMBB = BB; 4672 MachineFunction *F = BB->getParent(); 4673 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 4674 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 4675 F->insert(It, copy0MBB); 4676 F->insert(It, sinkMBB); 4677 4678 // Transfer the remainder of BB and its successor edges to sinkMBB. 4679 sinkMBB->splice(sinkMBB->begin(), BB, 4680 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 4681 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 4682 4683 // Next, add the true and fallthrough blocks as its successors. 4684 BB->addSuccessor(copy0MBB); 4685 BB->addSuccessor(sinkMBB); 4686 4687 // bne rs, $0, sinkMBB 4688 BuildMI(BB, DL, TII->get(Mips::BNE)) 4689 .addReg(MI.getOperand(2).getReg()) 4690 .addReg(Mips::ZERO) 4691 .addMBB(sinkMBB); 4692 4693 // copy0MBB: 4694 // %FalseValue = ... 4695 // # fallthrough to sinkMBB 4696 BB = copy0MBB; 4697 4698 // Update machine-CFG edges 4699 BB->addSuccessor(sinkMBB); 4700 4701 // sinkMBB: 4702 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ] 4703 // ... 4704 BB = sinkMBB; 4705 4706 // Use two PHI nodes to select two reults 4707 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg()) 4708 .addReg(MI.getOperand(3).getReg()) 4709 .addMBB(thisMBB) 4710 .addReg(MI.getOperand(5).getReg()) 4711 .addMBB(copy0MBB); 4712 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(1).getReg()) 4713 .addReg(MI.getOperand(4).getReg()) 4714 .addMBB(thisMBB) 4715 .addReg(MI.getOperand(6).getReg()) 4716 .addMBB(copy0MBB); 4717 4718 MI.eraseFromParent(); // The pseudo instruction is gone now. 4719 4720 return BB; 4721 } 4722 4723 // FIXME? Maybe this could be a TableGen attribute on some registers and 4724 // this table could be generated automatically from RegInfo. 4725 Register 4726 MipsTargetLowering::getRegisterByName(const char *RegName, LLT VT, 4727 const MachineFunction &MF) const { 4728 // Named registers is expected to be fairly rare. For now, just support $28 4729 // since the linux kernel uses it. 4730 if (Subtarget.isGP64bit()) { 4731 Register Reg = StringSwitch<Register>(RegName) 4732 .Case("$28", Mips::GP_64) 4733 .Default(Register()); 4734 if (Reg) 4735 return Reg; 4736 } else { 4737 Register Reg = StringSwitch<Register>(RegName) 4738 .Case("$28", Mips::GP) 4739 .Default(Register()); 4740 if (Reg) 4741 return Reg; 4742 } 4743 report_fatal_error("Invalid register name global variable"); 4744 } 4745 4746 MachineBasicBlock *MipsTargetLowering::emitLDR_W(MachineInstr &MI, 4747 MachineBasicBlock *BB) const { 4748 MachineFunction *MF = BB->getParent(); 4749 MachineRegisterInfo &MRI = MF->getRegInfo(); 4750 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 4751 const bool IsLittle = Subtarget.isLittle(); 4752 DebugLoc DL = MI.getDebugLoc(); 4753 4754 Register Dest = MI.getOperand(0).getReg(); 4755 Register Address = MI.getOperand(1).getReg(); 4756 unsigned Imm = MI.getOperand(2).getImm(); 4757 4758 MachineBasicBlock::iterator I(MI); 4759 4760 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) { 4761 // Mips release 6 can load from adress that is not naturally-aligned. 4762 Register Temp = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4763 BuildMI(*BB, I, DL, TII->get(Mips::LW)) 4764 .addDef(Temp) 4765 .addUse(Address) 4766 .addImm(Imm); 4767 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Dest).addUse(Temp); 4768 } else { 4769 // Mips release 5 needs to use instructions that can load from an unaligned 4770 // memory address. 4771 Register LoadHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4772 Register LoadFull = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4773 Register Undef = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4774 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(Undef); 4775 BuildMI(*BB, I, DL, TII->get(Mips::LWR)) 4776 .addDef(LoadHalf) 4777 .addUse(Address) 4778 .addImm(Imm + (IsLittle ? 0 : 3)) 4779 .addUse(Undef); 4780 BuildMI(*BB, I, DL, TII->get(Mips::LWL)) 4781 .addDef(LoadFull) 4782 .addUse(Address) 4783 .addImm(Imm + (IsLittle ? 3 : 0)) 4784 .addUse(LoadHalf); 4785 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Dest).addUse(LoadFull); 4786 } 4787 4788 MI.eraseFromParent(); 4789 return BB; 4790 } 4791 4792 MachineBasicBlock *MipsTargetLowering::emitLDR_D(MachineInstr &MI, 4793 MachineBasicBlock *BB) const { 4794 MachineFunction *MF = BB->getParent(); 4795 MachineRegisterInfo &MRI = MF->getRegInfo(); 4796 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 4797 const bool IsLittle = Subtarget.isLittle(); 4798 DebugLoc DL = MI.getDebugLoc(); 4799 4800 Register Dest = MI.getOperand(0).getReg(); 4801 Register Address = MI.getOperand(1).getReg(); 4802 unsigned Imm = MI.getOperand(2).getImm(); 4803 4804 MachineBasicBlock::iterator I(MI); 4805 4806 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) { 4807 // Mips release 6 can load from adress that is not naturally-aligned. 4808 if (Subtarget.isGP64bit()) { 4809 Register Temp = MRI.createVirtualRegister(&Mips::GPR64RegClass); 4810 BuildMI(*BB, I, DL, TII->get(Mips::LD)) 4811 .addDef(Temp) 4812 .addUse(Address) 4813 .addImm(Imm); 4814 BuildMI(*BB, I, DL, TII->get(Mips::FILL_D)).addDef(Dest).addUse(Temp); 4815 } else { 4816 Register Wtemp = MRI.createVirtualRegister(&Mips::MSA128WRegClass); 4817 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4818 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4819 BuildMI(*BB, I, DL, TII->get(Mips::LW)) 4820 .addDef(Lo) 4821 .addUse(Address) 4822 .addImm(Imm + (IsLittle ? 0 : 4)); 4823 BuildMI(*BB, I, DL, TII->get(Mips::LW)) 4824 .addDef(Hi) 4825 .addUse(Address) 4826 .addImm(Imm + (IsLittle ? 4 : 0)); 4827 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Wtemp).addUse(Lo); 4828 BuildMI(*BB, I, DL, TII->get(Mips::INSERT_W), Dest) 4829 .addUse(Wtemp) 4830 .addUse(Hi) 4831 .addImm(1); 4832 } 4833 } else { 4834 // Mips release 5 needs to use instructions that can load from an unaligned 4835 // memory address. 4836 Register LoHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4837 Register LoFull = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4838 Register LoUndef = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4839 Register HiHalf = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4840 Register HiFull = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4841 Register HiUndef = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4842 Register Wtemp = MRI.createVirtualRegister(&Mips::MSA128WRegClass); 4843 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(LoUndef); 4844 BuildMI(*BB, I, DL, TII->get(Mips::LWR)) 4845 .addDef(LoHalf) 4846 .addUse(Address) 4847 .addImm(Imm + (IsLittle ? 0 : 7)) 4848 .addUse(LoUndef); 4849 BuildMI(*BB, I, DL, TII->get(Mips::LWL)) 4850 .addDef(LoFull) 4851 .addUse(Address) 4852 .addImm(Imm + (IsLittle ? 3 : 4)) 4853 .addUse(LoHalf); 4854 BuildMI(*BB, I, DL, TII->get(Mips::IMPLICIT_DEF)).addDef(HiUndef); 4855 BuildMI(*BB, I, DL, TII->get(Mips::LWR)) 4856 .addDef(HiHalf) 4857 .addUse(Address) 4858 .addImm(Imm + (IsLittle ? 4 : 3)) 4859 .addUse(HiUndef); 4860 BuildMI(*BB, I, DL, TII->get(Mips::LWL)) 4861 .addDef(HiFull) 4862 .addUse(Address) 4863 .addImm(Imm + (IsLittle ? 7 : 0)) 4864 .addUse(HiHalf); 4865 BuildMI(*BB, I, DL, TII->get(Mips::FILL_W)).addDef(Wtemp).addUse(LoFull); 4866 BuildMI(*BB, I, DL, TII->get(Mips::INSERT_W), Dest) 4867 .addUse(Wtemp) 4868 .addUse(HiFull) 4869 .addImm(1); 4870 } 4871 4872 MI.eraseFromParent(); 4873 return BB; 4874 } 4875 4876 MachineBasicBlock *MipsTargetLowering::emitSTR_W(MachineInstr &MI, 4877 MachineBasicBlock *BB) const { 4878 MachineFunction *MF = BB->getParent(); 4879 MachineRegisterInfo &MRI = MF->getRegInfo(); 4880 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 4881 const bool IsLittle = Subtarget.isLittle(); 4882 DebugLoc DL = MI.getDebugLoc(); 4883 4884 Register StoreVal = MI.getOperand(0).getReg(); 4885 Register Address = MI.getOperand(1).getReg(); 4886 unsigned Imm = MI.getOperand(2).getImm(); 4887 4888 MachineBasicBlock::iterator I(MI); 4889 4890 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) { 4891 // Mips release 6 can store to adress that is not naturally-aligned. 4892 Register BitcastW = MRI.createVirtualRegister(&Mips::MSA128WRegClass); 4893 Register Tmp = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4894 BuildMI(*BB, I, DL, TII->get(Mips::COPY)).addDef(BitcastW).addUse(StoreVal); 4895 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W)) 4896 .addDef(Tmp) 4897 .addUse(BitcastW) 4898 .addImm(0); 4899 BuildMI(*BB, I, DL, TII->get(Mips::SW)) 4900 .addUse(Tmp) 4901 .addUse(Address) 4902 .addImm(Imm); 4903 } else { 4904 // Mips release 5 needs to use instructions that can store to an unaligned 4905 // memory address. 4906 Register Tmp = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4907 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W)) 4908 .addDef(Tmp) 4909 .addUse(StoreVal) 4910 .addImm(0); 4911 BuildMI(*BB, I, DL, TII->get(Mips::SWR)) 4912 .addUse(Tmp) 4913 .addUse(Address) 4914 .addImm(Imm + (IsLittle ? 0 : 3)); 4915 BuildMI(*BB, I, DL, TII->get(Mips::SWL)) 4916 .addUse(Tmp) 4917 .addUse(Address) 4918 .addImm(Imm + (IsLittle ? 3 : 0)); 4919 } 4920 4921 MI.eraseFromParent(); 4922 4923 return BB; 4924 } 4925 4926 MachineBasicBlock *MipsTargetLowering::emitSTR_D(MachineInstr &MI, 4927 MachineBasicBlock *BB) const { 4928 MachineFunction *MF = BB->getParent(); 4929 MachineRegisterInfo &MRI = MF->getRegInfo(); 4930 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 4931 const bool IsLittle = Subtarget.isLittle(); 4932 DebugLoc DL = MI.getDebugLoc(); 4933 4934 Register StoreVal = MI.getOperand(0).getReg(); 4935 Register Address = MI.getOperand(1).getReg(); 4936 unsigned Imm = MI.getOperand(2).getImm(); 4937 4938 MachineBasicBlock::iterator I(MI); 4939 4940 if (Subtarget.hasMips32r6() || Subtarget.hasMips64r6()) { 4941 // Mips release 6 can store to adress that is not naturally-aligned. 4942 if (Subtarget.isGP64bit()) { 4943 Register BitcastD = MRI.createVirtualRegister(&Mips::MSA128DRegClass); 4944 Register Lo = MRI.createVirtualRegister(&Mips::GPR64RegClass); 4945 BuildMI(*BB, I, DL, TII->get(Mips::COPY)) 4946 .addDef(BitcastD) 4947 .addUse(StoreVal); 4948 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_D)) 4949 .addDef(Lo) 4950 .addUse(BitcastD) 4951 .addImm(0); 4952 BuildMI(*BB, I, DL, TII->get(Mips::SD)) 4953 .addUse(Lo) 4954 .addUse(Address) 4955 .addImm(Imm); 4956 } else { 4957 Register BitcastW = MRI.createVirtualRegister(&Mips::MSA128WRegClass); 4958 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4959 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4960 BuildMI(*BB, I, DL, TII->get(Mips::COPY)) 4961 .addDef(BitcastW) 4962 .addUse(StoreVal); 4963 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W)) 4964 .addDef(Lo) 4965 .addUse(BitcastW) 4966 .addImm(0); 4967 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W)) 4968 .addDef(Hi) 4969 .addUse(BitcastW) 4970 .addImm(1); 4971 BuildMI(*BB, I, DL, TII->get(Mips::SW)) 4972 .addUse(Lo) 4973 .addUse(Address) 4974 .addImm(Imm + (IsLittle ? 0 : 4)); 4975 BuildMI(*BB, I, DL, TII->get(Mips::SW)) 4976 .addUse(Hi) 4977 .addUse(Address) 4978 .addImm(Imm + (IsLittle ? 4 : 0)); 4979 } 4980 } else { 4981 // Mips release 5 needs to use instructions that can store to an unaligned 4982 // memory address. 4983 Register Bitcast = MRI.createVirtualRegister(&Mips::MSA128WRegClass); 4984 Register Lo = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4985 Register Hi = MRI.createVirtualRegister(&Mips::GPR32RegClass); 4986 BuildMI(*BB, I, DL, TII->get(Mips::COPY)).addDef(Bitcast).addUse(StoreVal); 4987 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W)) 4988 .addDef(Lo) 4989 .addUse(Bitcast) 4990 .addImm(0); 4991 BuildMI(*BB, I, DL, TII->get(Mips::COPY_S_W)) 4992 .addDef(Hi) 4993 .addUse(Bitcast) 4994 .addImm(1); 4995 BuildMI(*BB, I, DL, TII->get(Mips::SWR)) 4996 .addUse(Lo) 4997 .addUse(Address) 4998 .addImm(Imm + (IsLittle ? 0 : 3)); 4999 BuildMI(*BB, I, DL, TII->get(Mips::SWL)) 5000 .addUse(Lo) 5001 .addUse(Address) 5002 .addImm(Imm + (IsLittle ? 3 : 0)); 5003 BuildMI(*BB, I, DL, TII->get(Mips::SWR)) 5004 .addUse(Hi) 5005 .addUse(Address) 5006 .addImm(Imm + (IsLittle ? 4 : 7)); 5007 BuildMI(*BB, I, DL, TII->get(Mips::SWL)) 5008 .addUse(Hi) 5009 .addUse(Address) 5010 .addImm(Imm + (IsLittle ? 7 : 4)); 5011 } 5012 5013 MI.eraseFromParent(); 5014 return BB; 5015 } 5016