1 //===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the interfaces that Mips uses to lower LLVM code into a 11 // selection DAG. 12 // 13 //===----------------------------------------------------------------------===// 14 #include "MipsISelLowering.h" 15 #include "InstPrinter/MipsInstPrinter.h" 16 #include "MCTargetDesc/MipsBaseInfo.h" 17 #include "MipsMachineFunction.h" 18 #include "MipsSubtarget.h" 19 #include "MipsTargetMachine.h" 20 #include "MipsTargetObjectFile.h" 21 #include "llvm/ADT/Statistic.h" 22 #include "llvm/ADT/StringSwitch.h" 23 #include "llvm/CodeGen/CallingConvLower.h" 24 #include "llvm/CodeGen/MachineFrameInfo.h" 25 #include "llvm/CodeGen/MachineFunction.h" 26 #include "llvm/CodeGen/MachineInstrBuilder.h" 27 #include "llvm/CodeGen/MachineRegisterInfo.h" 28 #include "llvm/CodeGen/SelectionDAGISel.h" 29 #include "llvm/CodeGen/ValueTypes.h" 30 #include "llvm/IR/CallingConv.h" 31 #include "llvm/IR/DerivedTypes.h" 32 #include "llvm/IR/GlobalVariable.h" 33 #include "llvm/Support/CommandLine.h" 34 #include "llvm/Support/Debug.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/raw_ostream.h" 37 #include <cctype> 38 39 using namespace llvm; 40 41 #define DEBUG_TYPE "mips-lower" 42 43 STATISTIC(NumTailCalls, "Number of tail calls"); 44 45 static cl::opt<bool> 46 LargeGOT("mxgot", cl::Hidden, 47 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false)); 48 49 static cl::opt<bool> 50 NoZeroDivCheck("mno-check-zero-division", cl::Hidden, 51 cl::desc("MIPS: Don't trap on integer division by zero."), 52 cl::init(false)); 53 54 cl::opt<bool> 55 EnableMipsFastISel("mips-fast-isel", cl::Hidden, 56 cl::desc("Allow mips-fast-isel to be used"), 57 cl::init(false)); 58 59 static const MCPhysReg O32IntRegs[4] = { 60 Mips::A0, Mips::A1, Mips::A2, Mips::A3 61 }; 62 63 static const MCPhysReg Mips64IntRegs[8] = { 64 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64, 65 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64 66 }; 67 68 static const MCPhysReg Mips64DPRegs[8] = { 69 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64, 70 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64 71 }; 72 73 // If I is a shifted mask, set the size (Size) and the first bit of the 74 // mask (Pos), and return true. 75 // For example, if I is 0x003ff800, (Pos, Size) = (11, 11). 76 static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) { 77 if (!isShiftedMask_64(I)) 78 return false; 79 80 Size = CountPopulation_64(I); 81 Pos = countTrailingZeros(I); 82 return true; 83 } 84 85 SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const { 86 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>(); 87 return DAG.getRegister(FI->getGlobalBaseReg(), Ty); 88 } 89 90 SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty, 91 SelectionDAG &DAG, 92 unsigned Flag) const { 93 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag); 94 } 95 96 SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty, 97 SelectionDAG &DAG, 98 unsigned Flag) const { 99 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag); 100 } 101 102 SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty, 103 SelectionDAG &DAG, 104 unsigned Flag) const { 105 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag); 106 } 107 108 SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty, 109 SelectionDAG &DAG, 110 unsigned Flag) const { 111 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag); 112 } 113 114 SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty, 115 SelectionDAG &DAG, 116 unsigned Flag) const { 117 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(), 118 N->getOffset(), Flag); 119 } 120 121 const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const { 122 switch (Opcode) { 123 case MipsISD::JmpLink: return "MipsISD::JmpLink"; 124 case MipsISD::TailCall: return "MipsISD::TailCall"; 125 case MipsISD::Hi: return "MipsISD::Hi"; 126 case MipsISD::Lo: return "MipsISD::Lo"; 127 case MipsISD::GPRel: return "MipsISD::GPRel"; 128 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer"; 129 case MipsISD::Ret: return "MipsISD::Ret"; 130 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN"; 131 case MipsISD::FPBrcond: return "MipsISD::FPBrcond"; 132 case MipsISD::FPCmp: return "MipsISD::FPCmp"; 133 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T"; 134 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F"; 135 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP"; 136 case MipsISD::MFHI: return "MipsISD::MFHI"; 137 case MipsISD::MFLO: return "MipsISD::MFLO"; 138 case MipsISD::MTLOHI: return "MipsISD::MTLOHI"; 139 case MipsISD::Mult: return "MipsISD::Mult"; 140 case MipsISD::Multu: return "MipsISD::Multu"; 141 case MipsISD::MAdd: return "MipsISD::MAdd"; 142 case MipsISD::MAddu: return "MipsISD::MAddu"; 143 case MipsISD::MSub: return "MipsISD::MSub"; 144 case MipsISD::MSubu: return "MipsISD::MSubu"; 145 case MipsISD::DivRem: return "MipsISD::DivRem"; 146 case MipsISD::DivRemU: return "MipsISD::DivRemU"; 147 case MipsISD::DivRem16: return "MipsISD::DivRem16"; 148 case MipsISD::DivRemU16: return "MipsISD::DivRemU16"; 149 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64"; 150 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64"; 151 case MipsISD::Wrapper: return "MipsISD::Wrapper"; 152 case MipsISD::Sync: return "MipsISD::Sync"; 153 case MipsISD::Ext: return "MipsISD::Ext"; 154 case MipsISD::Ins: return "MipsISD::Ins"; 155 case MipsISD::LWL: return "MipsISD::LWL"; 156 case MipsISD::LWR: return "MipsISD::LWR"; 157 case MipsISD::SWL: return "MipsISD::SWL"; 158 case MipsISD::SWR: return "MipsISD::SWR"; 159 case MipsISD::LDL: return "MipsISD::LDL"; 160 case MipsISD::LDR: return "MipsISD::LDR"; 161 case MipsISD::SDL: return "MipsISD::SDL"; 162 case MipsISD::SDR: return "MipsISD::SDR"; 163 case MipsISD::EXTP: return "MipsISD::EXTP"; 164 case MipsISD::EXTPDP: return "MipsISD::EXTPDP"; 165 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H"; 166 case MipsISD::EXTR_W: return "MipsISD::EXTR_W"; 167 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W"; 168 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W"; 169 case MipsISD::SHILO: return "MipsISD::SHILO"; 170 case MipsISD::MTHLIP: return "MipsISD::MTHLIP"; 171 case MipsISD::MULT: return "MipsISD::MULT"; 172 case MipsISD::MULTU: return "MipsISD::MULTU"; 173 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP"; 174 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP"; 175 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP"; 176 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP"; 177 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP"; 178 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP"; 179 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP"; 180 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP"; 181 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP"; 182 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO"; 183 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO"; 184 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO"; 185 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO"; 186 case MipsISD::VCEQ: return "MipsISD::VCEQ"; 187 case MipsISD::VCLE_S: return "MipsISD::VCLE_S"; 188 case MipsISD::VCLE_U: return "MipsISD::VCLE_U"; 189 case MipsISD::VCLT_S: return "MipsISD::VCLT_S"; 190 case MipsISD::VCLT_U: return "MipsISD::VCLT_U"; 191 case MipsISD::VSMAX: return "MipsISD::VSMAX"; 192 case MipsISD::VSMIN: return "MipsISD::VSMIN"; 193 case MipsISD::VUMAX: return "MipsISD::VUMAX"; 194 case MipsISD::VUMIN: return "MipsISD::VUMIN"; 195 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT"; 196 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT"; 197 case MipsISD::VNOR: return "MipsISD::VNOR"; 198 case MipsISD::VSHF: return "MipsISD::VSHF"; 199 case MipsISD::SHF: return "MipsISD::SHF"; 200 case MipsISD::ILVEV: return "MipsISD::ILVEV"; 201 case MipsISD::ILVOD: return "MipsISD::ILVOD"; 202 case MipsISD::ILVL: return "MipsISD::ILVL"; 203 case MipsISD::ILVR: return "MipsISD::ILVR"; 204 case MipsISD::PCKEV: return "MipsISD::PCKEV"; 205 case MipsISD::PCKOD: return "MipsISD::PCKOD"; 206 case MipsISD::INSVE: return "MipsISD::INSVE"; 207 default: return nullptr; 208 } 209 } 210 211 MipsTargetLowering::MipsTargetLowering(MipsTargetMachine &TM) 212 : TargetLowering(TM, new MipsTargetObjectFile()), 213 Subtarget(&TM.getSubtarget<MipsSubtarget>()) { 214 // Mips does not have i1 type, so use i32 for 215 // setcc operations results (slt, sgt, ...). 216 setBooleanContents(ZeroOrOneBooleanContent); 217 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 218 219 // Load extented operations for i1 types must be promoted 220 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote); 221 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote); 222 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); 223 224 // MIPS doesn't have extending float->double load/store 225 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); 226 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 227 228 // Used by legalize types to correctly generate the setcc result. 229 // Without this, every float setcc comes with a AND/OR with the result, 230 // we don't want this, since the fpcmp result goes to a flag register, 231 // which is used implicitly by brcond and select operations. 232 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32); 233 234 // Mips Custom Operations 235 setOperationAction(ISD::BR_JT, MVT::Other, Custom); 236 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 237 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 238 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 239 setOperationAction(ISD::JumpTable, MVT::i32, Custom); 240 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 241 setOperationAction(ISD::SELECT, MVT::f32, Custom); 242 setOperationAction(ISD::SELECT, MVT::f64, Custom); 243 setOperationAction(ISD::SELECT, MVT::i32, Custom); 244 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 245 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 246 setOperationAction(ISD::SETCC, MVT::f32, Custom); 247 setOperationAction(ISD::SETCC, MVT::f64, Custom); 248 setOperationAction(ISD::BRCOND, MVT::Other, Custom); 249 setOperationAction(ISD::VASTART, MVT::Other, Custom); 250 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 251 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 252 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 253 254 if (isGP64bit()) { 255 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); 256 setOperationAction(ISD::BlockAddress, MVT::i64, Custom); 257 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); 258 setOperationAction(ISD::JumpTable, MVT::i64, Custom); 259 setOperationAction(ISD::ConstantPool, MVT::i64, Custom); 260 setOperationAction(ISD::SELECT, MVT::i64, Custom); 261 setOperationAction(ISD::LOAD, MVT::i64, Custom); 262 setOperationAction(ISD::STORE, MVT::i64, Custom); 263 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 264 } 265 266 if (!isGP64bit()) { 267 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 268 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 269 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 270 } 271 272 setOperationAction(ISD::ADD, MVT::i32, Custom); 273 if (isGP64bit()) 274 setOperationAction(ISD::ADD, MVT::i64, Custom); 275 276 setOperationAction(ISD::SDIV, MVT::i32, Expand); 277 setOperationAction(ISD::SREM, MVT::i32, Expand); 278 setOperationAction(ISD::UDIV, MVT::i32, Expand); 279 setOperationAction(ISD::UREM, MVT::i32, Expand); 280 setOperationAction(ISD::SDIV, MVT::i64, Expand); 281 setOperationAction(ISD::SREM, MVT::i64, Expand); 282 setOperationAction(ISD::UDIV, MVT::i64, Expand); 283 setOperationAction(ISD::UREM, MVT::i64, Expand); 284 285 // Operations not directly supported by Mips. 286 setOperationAction(ISD::BR_CC, MVT::f32, Expand); 287 setOperationAction(ISD::BR_CC, MVT::f64, Expand); 288 setOperationAction(ISD::BR_CC, MVT::i32, Expand); 289 setOperationAction(ISD::BR_CC, MVT::i64, Expand); 290 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand); 291 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); 292 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); 293 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); 294 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); 295 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand); 296 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 297 if (Subtarget->hasCnMips()) { 298 setOperationAction(ISD::CTPOP, MVT::i32, Legal); 299 setOperationAction(ISD::CTPOP, MVT::i64, Legal); 300 } else { 301 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 302 setOperationAction(ISD::CTPOP, MVT::i64, Expand); 303 } 304 setOperationAction(ISD::CTTZ, MVT::i32, Expand); 305 setOperationAction(ISD::CTTZ, MVT::i64, Expand); 306 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand); 307 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand); 308 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand); 309 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand); 310 setOperationAction(ISD::ROTL, MVT::i32, Expand); 311 setOperationAction(ISD::ROTL, MVT::i64, Expand); 312 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 313 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); 314 315 if (!Subtarget->hasMips32r2()) 316 setOperationAction(ISD::ROTR, MVT::i32, Expand); 317 318 if (!Subtarget->hasMips64r2()) 319 setOperationAction(ISD::ROTR, MVT::i64, Expand); 320 321 setOperationAction(ISD::FSIN, MVT::f32, Expand); 322 setOperationAction(ISD::FSIN, MVT::f64, Expand); 323 setOperationAction(ISD::FCOS, MVT::f32, Expand); 324 setOperationAction(ISD::FCOS, MVT::f64, Expand); 325 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 326 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 327 setOperationAction(ISD::FPOWI, MVT::f32, Expand); 328 setOperationAction(ISD::FPOW, MVT::f32, Expand); 329 setOperationAction(ISD::FPOW, MVT::f64, Expand); 330 setOperationAction(ISD::FLOG, MVT::f32, Expand); 331 setOperationAction(ISD::FLOG2, MVT::f32, Expand); 332 setOperationAction(ISD::FLOG10, MVT::f32, Expand); 333 setOperationAction(ISD::FEXP, MVT::f32, Expand); 334 setOperationAction(ISD::FMA, MVT::f32, Expand); 335 setOperationAction(ISD::FMA, MVT::f64, Expand); 336 setOperationAction(ISD::FREM, MVT::f32, Expand); 337 setOperationAction(ISD::FREM, MVT::f64, Expand); 338 339 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom); 340 341 setOperationAction(ISD::VAARG, MVT::Other, Expand); 342 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 343 setOperationAction(ISD::VAEND, MVT::Other, Expand); 344 345 // Use the default for now 346 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 347 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 348 349 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand); 350 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand); 351 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand); 352 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand); 353 354 setInsertFencesForAtomic(true); 355 356 if (!Subtarget->hasMips32r2()) { 357 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); 358 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 359 } 360 361 // MIPS16 lacks MIPS32's clz and clo instructions. 362 if (!Subtarget->hasMips32() || Subtarget->inMips16Mode()) 363 setOperationAction(ISD::CTLZ, MVT::i32, Expand); 364 if (!Subtarget->hasMips64()) 365 setOperationAction(ISD::CTLZ, MVT::i64, Expand); 366 367 if (!Subtarget->hasMips32r2()) 368 setOperationAction(ISD::BSWAP, MVT::i32, Expand); 369 if (!Subtarget->hasMips64r2()) 370 setOperationAction(ISD::BSWAP, MVT::i64, Expand); 371 372 if (isGP64bit()) { 373 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom); 374 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom); 375 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom); 376 setTruncStoreAction(MVT::i64, MVT::i32, Custom); 377 } 378 379 setOperationAction(ISD::TRAP, MVT::Other, Legal); 380 381 setTargetDAGCombine(ISD::SDIVREM); 382 setTargetDAGCombine(ISD::UDIVREM); 383 setTargetDAGCombine(ISD::SELECT); 384 setTargetDAGCombine(ISD::AND); 385 setTargetDAGCombine(ISD::OR); 386 setTargetDAGCombine(ISD::ADD); 387 388 setMinFunctionAlignment(isGP64bit() ? 3 : 2); 389 390 setStackPointerRegisterToSaveRestore(isN64() ? Mips::SP_64 : Mips::SP); 391 392 setExceptionPointerRegister(isN64() ? Mips::A0_64 : Mips::A0); 393 setExceptionSelectorRegister(isN64() ? Mips::A1_64 : Mips::A1); 394 395 MaxStoresPerMemcpy = 16; 396 397 isMicroMips = Subtarget->inMicroMipsMode(); 398 } 399 400 const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) { 401 if (TM.getSubtargetImpl()->inMips16Mode()) 402 return llvm::createMips16TargetLowering(TM); 403 404 return llvm::createMipsSETargetLowering(TM); 405 } 406 407 // Create a fast isel object. 408 FastISel * 409 MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo, 410 const TargetLibraryInfo *libInfo) const { 411 if (!EnableMipsFastISel) 412 return TargetLowering::createFastISel(funcInfo, libInfo); 413 return Mips::createFastISel(funcInfo, libInfo); 414 } 415 416 EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { 417 if (!VT.isVector()) 418 return MVT::i32; 419 return VT.changeVectorElementTypeToInteger(); 420 } 421 422 static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG, 423 TargetLowering::DAGCombinerInfo &DCI, 424 const MipsSubtarget *Subtarget) { 425 if (DCI.isBeforeLegalizeOps()) 426 return SDValue(); 427 428 EVT Ty = N->getValueType(0); 429 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64; 430 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64; 431 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 : 432 MipsISD::DivRemU16; 433 SDLoc DL(N); 434 435 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue, 436 N->getOperand(0), N->getOperand(1)); 437 SDValue InChain = DAG.getEntryNode(); 438 SDValue InGlue = DivRem; 439 440 // insert MFLO 441 if (N->hasAnyUseOfValue(0)) { 442 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty, 443 InGlue); 444 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo); 445 InChain = CopyFromLo.getValue(1); 446 InGlue = CopyFromLo.getValue(2); 447 } 448 449 // insert MFHI 450 if (N->hasAnyUseOfValue(1)) { 451 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL, 452 HI, Ty, InGlue); 453 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi); 454 } 455 456 return SDValue(); 457 } 458 459 static Mips::CondCode condCodeToFCC(ISD::CondCode CC) { 460 switch (CC) { 461 default: llvm_unreachable("Unknown fp condition code!"); 462 case ISD::SETEQ: 463 case ISD::SETOEQ: return Mips::FCOND_OEQ; 464 case ISD::SETUNE: return Mips::FCOND_UNE; 465 case ISD::SETLT: 466 case ISD::SETOLT: return Mips::FCOND_OLT; 467 case ISD::SETGT: 468 case ISD::SETOGT: return Mips::FCOND_OGT; 469 case ISD::SETLE: 470 case ISD::SETOLE: return Mips::FCOND_OLE; 471 case ISD::SETGE: 472 case ISD::SETOGE: return Mips::FCOND_OGE; 473 case ISD::SETULT: return Mips::FCOND_ULT; 474 case ISD::SETULE: return Mips::FCOND_ULE; 475 case ISD::SETUGT: return Mips::FCOND_UGT; 476 case ISD::SETUGE: return Mips::FCOND_UGE; 477 case ISD::SETUO: return Mips::FCOND_UN; 478 case ISD::SETO: return Mips::FCOND_OR; 479 case ISD::SETNE: 480 case ISD::SETONE: return Mips::FCOND_ONE; 481 case ISD::SETUEQ: return Mips::FCOND_UEQ; 482 } 483 } 484 485 486 /// This function returns true if the floating point conditional branches and 487 /// conditional moves which use condition code CC should be inverted. 488 static bool invertFPCondCodeUser(Mips::CondCode CC) { 489 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT) 490 return false; 491 492 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) && 493 "Illegal Condition Code"); 494 495 return true; 496 } 497 498 // Creates and returns an FPCmp node from a setcc node. 499 // Returns Op if setcc is not a floating point comparison. 500 static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) { 501 // must be a SETCC node 502 if (Op.getOpcode() != ISD::SETCC) 503 return Op; 504 505 SDValue LHS = Op.getOperand(0); 506 507 if (!LHS.getValueType().isFloatingPoint()) 508 return Op; 509 510 SDValue RHS = Op.getOperand(1); 511 SDLoc DL(Op); 512 513 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of 514 // node if necessary. 515 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 516 517 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS, 518 DAG.getConstant(condCodeToFCC(CC), MVT::i32)); 519 } 520 521 // Creates and returns a CMovFPT/F node. 522 static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True, 523 SDValue False, SDLoc DL) { 524 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2)); 525 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue()); 526 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32); 527 528 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL, 529 True.getValueType(), True, FCC0, False, Cond); 530 } 531 532 static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG, 533 TargetLowering::DAGCombinerInfo &DCI, 534 const MipsSubtarget *Subtarget) { 535 if (DCI.isBeforeLegalizeOps()) 536 return SDValue(); 537 538 SDValue SetCC = N->getOperand(0); 539 540 if ((SetCC.getOpcode() != ISD::SETCC) || 541 !SetCC.getOperand(0).getValueType().isInteger()) 542 return SDValue(); 543 544 SDValue False = N->getOperand(2); 545 EVT FalseTy = False.getValueType(); 546 547 if (!FalseTy.isInteger()) 548 return SDValue(); 549 550 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False); 551 552 // If the RHS (False) is 0, we swap the order of the operands 553 // of ISD::SELECT (obviously also inverting the condition) so that we can 554 // take advantage of conditional moves using the $0 register. 555 // Example: 556 // return (a != 0) ? x : 0; 557 // load $reg, x 558 // movz $reg, $0, a 559 if (!FalseC) 560 return SDValue(); 561 562 const SDLoc DL(N); 563 564 if (!FalseC->getZExtValue()) { 565 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get(); 566 SDValue True = N->getOperand(1); 567 568 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0), 569 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true)); 570 571 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True); 572 } 573 574 // If both operands are integer constants there's a possibility that we 575 // can do some interesting optimizations. 576 SDValue True = N->getOperand(1); 577 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True); 578 579 if (!TrueC || !True.getValueType().isInteger()) 580 return SDValue(); 581 582 // We'll also ignore MVT::i64 operands as this optimizations proves 583 // to be ineffective because of the required sign extensions as the result 584 // of a SETCC operator is always MVT::i32 for non-vector types. 585 if (True.getValueType() == MVT::i64) 586 return SDValue(); 587 588 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue(); 589 590 // 1) (a < x) ? y : y-1 591 // slti $reg1, a, x 592 // addiu $reg2, $reg1, y-1 593 if (Diff == 1) 594 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False); 595 596 // 2) (a < x) ? y-1 : y 597 // slti $reg1, a, x 598 // xor $reg1, $reg1, 1 599 // addiu $reg2, $reg1, y-1 600 if (Diff == -1) { 601 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get(); 602 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0), 603 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true)); 604 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True); 605 } 606 607 // Couldn't optimize. 608 return SDValue(); 609 } 610 611 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG, 612 TargetLowering::DAGCombinerInfo &DCI, 613 const MipsSubtarget *Subtarget) { 614 // Pattern match EXT. 615 // $dst = and ((sra or srl) $src , pos), (2**size - 1) 616 // => ext $dst, $src, size, pos 617 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert()) 618 return SDValue(); 619 620 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1); 621 unsigned ShiftRightOpc = ShiftRight.getOpcode(); 622 623 // Op's first operand must be a shift right. 624 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL) 625 return SDValue(); 626 627 // The second operand of the shift must be an immediate. 628 ConstantSDNode *CN; 629 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1)))) 630 return SDValue(); 631 632 uint64_t Pos = CN->getZExtValue(); 633 uint64_t SMPos, SMSize; 634 635 // Op's second operand must be a shifted mask. 636 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) || 637 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize)) 638 return SDValue(); 639 640 // Return if the shifted mask does not start at bit 0 or the sum of its size 641 // and Pos exceeds the word's size. 642 EVT ValTy = N->getValueType(0); 643 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits()) 644 return SDValue(); 645 646 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy, 647 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32), 648 DAG.getConstant(SMSize, MVT::i32)); 649 } 650 651 static SDValue performORCombine(SDNode *N, SelectionDAG &DAG, 652 TargetLowering::DAGCombinerInfo &DCI, 653 const MipsSubtarget *Subtarget) { 654 // Pattern match INS. 655 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1), 656 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1 657 // => ins $dst, $src, size, pos, $src1 658 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert()) 659 return SDValue(); 660 661 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1); 662 uint64_t SMPos0, SMSize0, SMPos1, SMSize1; 663 ConstantSDNode *CN; 664 665 // See if Op's first operand matches (and $src1 , mask0). 666 if (And0.getOpcode() != ISD::AND) 667 return SDValue(); 668 669 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) || 670 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0)) 671 return SDValue(); 672 673 // See if Op's second operand matches (and (shl $src, pos), mask1). 674 if (And1.getOpcode() != ISD::AND) 675 return SDValue(); 676 677 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) || 678 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1)) 679 return SDValue(); 680 681 // The shift masks must have the same position and size. 682 if (SMPos0 != SMPos1 || SMSize0 != SMSize1) 683 return SDValue(); 684 685 SDValue Shl = And1.getOperand(0); 686 if (Shl.getOpcode() != ISD::SHL) 687 return SDValue(); 688 689 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1)))) 690 return SDValue(); 691 692 unsigned Shamt = CN->getZExtValue(); 693 694 // Return if the shift amount and the first bit position of mask are not the 695 // same. 696 EVT ValTy = N->getValueType(0); 697 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits())) 698 return SDValue(); 699 700 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0), 701 DAG.getConstant(SMPos0, MVT::i32), 702 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0)); 703 } 704 705 static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG, 706 TargetLowering::DAGCombinerInfo &DCI, 707 const MipsSubtarget *Subtarget) { 708 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt)) 709 710 if (DCI.isBeforeLegalizeOps()) 711 return SDValue(); 712 713 SDValue Add = N->getOperand(1); 714 715 if (Add.getOpcode() != ISD::ADD) 716 return SDValue(); 717 718 SDValue Lo = Add.getOperand(1); 719 720 if ((Lo.getOpcode() != MipsISD::Lo) || 721 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable)) 722 return SDValue(); 723 724 EVT ValTy = N->getValueType(0); 725 SDLoc DL(N); 726 727 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0), 728 Add.getOperand(0)); 729 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo); 730 } 731 732 SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) 733 const { 734 SelectionDAG &DAG = DCI.DAG; 735 unsigned Opc = N->getOpcode(); 736 737 switch (Opc) { 738 default: break; 739 case ISD::SDIVREM: 740 case ISD::UDIVREM: 741 return performDivRemCombine(N, DAG, DCI, Subtarget); 742 case ISD::SELECT: 743 return performSELECTCombine(N, DAG, DCI, Subtarget); 744 case ISD::AND: 745 return performANDCombine(N, DAG, DCI, Subtarget); 746 case ISD::OR: 747 return performORCombine(N, DAG, DCI, Subtarget); 748 case ISD::ADD: 749 return performADDCombine(N, DAG, DCI, Subtarget); 750 } 751 752 return SDValue(); 753 } 754 755 void 756 MipsTargetLowering::LowerOperationWrapper(SDNode *N, 757 SmallVectorImpl<SDValue> &Results, 758 SelectionDAG &DAG) const { 759 SDValue Res = LowerOperation(SDValue(N, 0), DAG); 760 761 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I) 762 Results.push_back(Res.getValue(I)); 763 } 764 765 void 766 MipsTargetLowering::ReplaceNodeResults(SDNode *N, 767 SmallVectorImpl<SDValue> &Results, 768 SelectionDAG &DAG) const { 769 return LowerOperationWrapper(N, Results, DAG); 770 } 771 772 SDValue MipsTargetLowering:: 773 LowerOperation(SDValue Op, SelectionDAG &DAG) const 774 { 775 switch (Op.getOpcode()) 776 { 777 case ISD::BR_JT: return lowerBR_JT(Op, DAG); 778 case ISD::BRCOND: return lowerBRCOND(Op, DAG); 779 case ISD::ConstantPool: return lowerConstantPool(Op, DAG); 780 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG); 781 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG); 782 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG); 783 case ISD::JumpTable: return lowerJumpTable(Op, DAG); 784 case ISD::SELECT: return lowerSELECT(Op, DAG); 785 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG); 786 case ISD::SETCC: return lowerSETCC(Op, DAG); 787 case ISD::VASTART: return lowerVASTART(Op, DAG); 788 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG); 789 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG); 790 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG); 791 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG); 792 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG); 793 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG); 794 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true); 795 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false); 796 case ISD::LOAD: return lowerLOAD(Op, DAG); 797 case ISD::STORE: return lowerSTORE(Op, DAG); 798 case ISD::ADD: return lowerADD(Op, DAG); 799 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG); 800 } 801 return SDValue(); 802 } 803 804 //===----------------------------------------------------------------------===// 805 // Lower helper functions 806 //===----------------------------------------------------------------------===// 807 808 // addLiveIn - This helper function adds the specified physical register to the 809 // MachineFunction as a live in value. It also creates a corresponding 810 // virtual register for it. 811 static unsigned 812 addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC) 813 { 814 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC); 815 MF.getRegInfo().addLiveIn(PReg, VReg); 816 return VReg; 817 } 818 819 static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI, 820 MachineBasicBlock &MBB, 821 const TargetInstrInfo &TII, 822 bool Is64Bit) { 823 if (NoZeroDivCheck) 824 return &MBB; 825 826 // Insert instruction "teq $divisor_reg, $zero, 7". 827 MachineBasicBlock::iterator I(MI); 828 MachineInstrBuilder MIB; 829 MachineOperand &Divisor = MI->getOperand(2); 830 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ)) 831 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill())) 832 .addReg(Mips::ZERO).addImm(7); 833 834 // Use the 32-bit sub-register if this is a 64-bit division. 835 if (Is64Bit) 836 MIB->getOperand(0).setSubReg(Mips::sub_32); 837 838 // Clear Divisor's kill flag. 839 Divisor.setIsKill(false); 840 841 // We would normally delete the original instruction here but in this case 842 // we only needed to inject an additional instruction rather than replace it. 843 844 return &MBB; 845 } 846 847 MachineBasicBlock * 848 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 849 MachineBasicBlock *BB) const { 850 switch (MI->getOpcode()) { 851 default: 852 llvm_unreachable("Unexpected instr type to insert"); 853 case Mips::ATOMIC_LOAD_ADD_I8: 854 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu); 855 case Mips::ATOMIC_LOAD_ADD_I16: 856 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu); 857 case Mips::ATOMIC_LOAD_ADD_I32: 858 return emitAtomicBinary(MI, BB, 4, Mips::ADDu); 859 case Mips::ATOMIC_LOAD_ADD_I64: 860 return emitAtomicBinary(MI, BB, 8, Mips::DADDu); 861 862 case Mips::ATOMIC_LOAD_AND_I8: 863 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND); 864 case Mips::ATOMIC_LOAD_AND_I16: 865 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND); 866 case Mips::ATOMIC_LOAD_AND_I32: 867 return emitAtomicBinary(MI, BB, 4, Mips::AND); 868 case Mips::ATOMIC_LOAD_AND_I64: 869 return emitAtomicBinary(MI, BB, 8, Mips::AND64); 870 871 case Mips::ATOMIC_LOAD_OR_I8: 872 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR); 873 case Mips::ATOMIC_LOAD_OR_I16: 874 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR); 875 case Mips::ATOMIC_LOAD_OR_I32: 876 return emitAtomicBinary(MI, BB, 4, Mips::OR); 877 case Mips::ATOMIC_LOAD_OR_I64: 878 return emitAtomicBinary(MI, BB, 8, Mips::OR64); 879 880 case Mips::ATOMIC_LOAD_XOR_I8: 881 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR); 882 case Mips::ATOMIC_LOAD_XOR_I16: 883 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR); 884 case Mips::ATOMIC_LOAD_XOR_I32: 885 return emitAtomicBinary(MI, BB, 4, Mips::XOR); 886 case Mips::ATOMIC_LOAD_XOR_I64: 887 return emitAtomicBinary(MI, BB, 8, Mips::XOR64); 888 889 case Mips::ATOMIC_LOAD_NAND_I8: 890 return emitAtomicBinaryPartword(MI, BB, 1, 0, true); 891 case Mips::ATOMIC_LOAD_NAND_I16: 892 return emitAtomicBinaryPartword(MI, BB, 2, 0, true); 893 case Mips::ATOMIC_LOAD_NAND_I32: 894 return emitAtomicBinary(MI, BB, 4, 0, true); 895 case Mips::ATOMIC_LOAD_NAND_I64: 896 return emitAtomicBinary(MI, BB, 8, 0, true); 897 898 case Mips::ATOMIC_LOAD_SUB_I8: 899 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu); 900 case Mips::ATOMIC_LOAD_SUB_I16: 901 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu); 902 case Mips::ATOMIC_LOAD_SUB_I32: 903 return emitAtomicBinary(MI, BB, 4, Mips::SUBu); 904 case Mips::ATOMIC_LOAD_SUB_I64: 905 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu); 906 907 case Mips::ATOMIC_SWAP_I8: 908 return emitAtomicBinaryPartword(MI, BB, 1, 0); 909 case Mips::ATOMIC_SWAP_I16: 910 return emitAtomicBinaryPartword(MI, BB, 2, 0); 911 case Mips::ATOMIC_SWAP_I32: 912 return emitAtomicBinary(MI, BB, 4, 0); 913 case Mips::ATOMIC_SWAP_I64: 914 return emitAtomicBinary(MI, BB, 8, 0); 915 916 case Mips::ATOMIC_CMP_SWAP_I8: 917 return emitAtomicCmpSwapPartword(MI, BB, 1); 918 case Mips::ATOMIC_CMP_SWAP_I16: 919 return emitAtomicCmpSwapPartword(MI, BB, 2); 920 case Mips::ATOMIC_CMP_SWAP_I32: 921 return emitAtomicCmpSwap(MI, BB, 4); 922 case Mips::ATOMIC_CMP_SWAP_I64: 923 return emitAtomicCmpSwap(MI, BB, 8); 924 case Mips::PseudoSDIV: 925 case Mips::PseudoUDIV: 926 case Mips::DIV: 927 case Mips::DIVU: 928 case Mips::MOD: 929 case Mips::MODU: 930 return insertDivByZeroTrap(MI, *BB, *getTargetMachine().getInstrInfo(), 931 false); 932 case Mips::PseudoDSDIV: 933 case Mips::PseudoDUDIV: 934 case Mips::DDIV: 935 case Mips::DDIVU: 936 case Mips::DMOD: 937 case Mips::DMODU: 938 return insertDivByZeroTrap(MI, *BB, *getTargetMachine().getInstrInfo(), 939 true); 940 case Mips::SEL_D: 941 return emitSEL_D(MI, BB); 942 } 943 } 944 945 // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and 946 // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true) 947 MachineBasicBlock * 948 MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB, 949 unsigned Size, unsigned BinOpcode, 950 bool Nand) const { 951 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary."); 952 953 MachineFunction *MF = BB->getParent(); 954 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 955 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8)); 956 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 957 DebugLoc DL = MI->getDebugLoc(); 958 unsigned LL, SC, AND, NOR, ZERO, BEQ; 959 960 if (Size == 4) { 961 if (isMicroMips) { 962 LL = Mips::LL_MM; 963 SC = Mips::SC_MM; 964 } else { 965 LL = Subtarget->hasMips32r6() ? Mips::LL : Mips::LL_R6; 966 SC = Subtarget->hasMips32r6() ? Mips::SC : Mips::SC_R6; 967 } 968 AND = Mips::AND; 969 NOR = Mips::NOR; 970 ZERO = Mips::ZERO; 971 BEQ = Mips::BEQ; 972 } else { 973 LL = Subtarget->hasMips64r6() ? Mips::LLD : Mips::LLD_R6; 974 SC = Subtarget->hasMips64r6() ? Mips::SCD : Mips::SCD_R6; 975 AND = Mips::AND64; 976 NOR = Mips::NOR64; 977 ZERO = Mips::ZERO_64; 978 BEQ = Mips::BEQ64; 979 } 980 981 unsigned OldVal = MI->getOperand(0).getReg(); 982 unsigned Ptr = MI->getOperand(1).getReg(); 983 unsigned Incr = MI->getOperand(2).getReg(); 984 985 unsigned StoreVal = RegInfo.createVirtualRegister(RC); 986 unsigned AndRes = RegInfo.createVirtualRegister(RC); 987 unsigned Success = RegInfo.createVirtualRegister(RC); 988 989 // insert new blocks after the current block 990 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 991 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 992 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 993 MachineFunction::iterator It = BB; 994 ++It; 995 MF->insert(It, loopMBB); 996 MF->insert(It, exitMBB); 997 998 // Transfer the remainder of BB and its successor edges to exitMBB. 999 exitMBB->splice(exitMBB->begin(), BB, 1000 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 1001 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 1002 1003 // thisMBB: 1004 // ... 1005 // fallthrough --> loopMBB 1006 BB->addSuccessor(loopMBB); 1007 loopMBB->addSuccessor(loopMBB); 1008 loopMBB->addSuccessor(exitMBB); 1009 1010 // loopMBB: 1011 // ll oldval, 0(ptr) 1012 // <binop> storeval, oldval, incr 1013 // sc success, storeval, 0(ptr) 1014 // beq success, $0, loopMBB 1015 BB = loopMBB; 1016 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0); 1017 if (Nand) { 1018 // and andres, oldval, incr 1019 // nor storeval, $0, andres 1020 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr); 1021 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes); 1022 } else if (BinOpcode) { 1023 // <binop> storeval, oldval, incr 1024 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr); 1025 } else { 1026 StoreVal = Incr; 1027 } 1028 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0); 1029 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB); 1030 1031 MI->eraseFromParent(); // The instruction is gone now. 1032 1033 return exitMBB; 1034 } 1035 1036 MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg( 1037 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg, 1038 unsigned SrcReg) const { 1039 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 1040 DebugLoc DL = MI->getDebugLoc(); 1041 1042 if (Subtarget->hasMips32r2() && Size == 1) { 1043 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg); 1044 return BB; 1045 } 1046 1047 if (Subtarget->hasMips32r2() && Size == 2) { 1048 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg); 1049 return BB; 1050 } 1051 1052 MachineFunction *MF = BB->getParent(); 1053 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 1054 const TargetRegisterClass *RC = getRegClassFor(MVT::i32); 1055 unsigned ScrReg = RegInfo.createVirtualRegister(RC); 1056 1057 assert(Size < 32); 1058 int64_t ShiftImm = 32 - (Size * 8); 1059 1060 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm); 1061 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm); 1062 1063 return BB; 1064 } 1065 1066 MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword( 1067 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode, 1068 bool Nand) const { 1069 assert((Size == 1 || Size == 2) && 1070 "Unsupported size for EmitAtomicBinaryPartial."); 1071 1072 MachineFunction *MF = BB->getParent(); 1073 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 1074 const TargetRegisterClass *RC = getRegClassFor(MVT::i32); 1075 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 1076 DebugLoc DL = MI->getDebugLoc(); 1077 1078 unsigned Dest = MI->getOperand(0).getReg(); 1079 unsigned Ptr = MI->getOperand(1).getReg(); 1080 unsigned Incr = MI->getOperand(2).getReg(); 1081 1082 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC); 1083 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC); 1084 unsigned Mask = RegInfo.createVirtualRegister(RC); 1085 unsigned Mask2 = RegInfo.createVirtualRegister(RC); 1086 unsigned NewVal = RegInfo.createVirtualRegister(RC); 1087 unsigned OldVal = RegInfo.createVirtualRegister(RC); 1088 unsigned Incr2 = RegInfo.createVirtualRegister(RC); 1089 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC); 1090 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC); 1091 unsigned MaskUpper = RegInfo.createVirtualRegister(RC); 1092 unsigned AndRes = RegInfo.createVirtualRegister(RC); 1093 unsigned BinOpRes = RegInfo.createVirtualRegister(RC); 1094 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC); 1095 unsigned StoreVal = RegInfo.createVirtualRegister(RC); 1096 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC); 1097 unsigned SrlRes = RegInfo.createVirtualRegister(RC); 1098 unsigned Success = RegInfo.createVirtualRegister(RC); 1099 1100 // insert new blocks after the current block 1101 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 1102 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 1103 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB); 1104 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 1105 MachineFunction::iterator It = BB; 1106 ++It; 1107 MF->insert(It, loopMBB); 1108 MF->insert(It, sinkMBB); 1109 MF->insert(It, exitMBB); 1110 1111 // Transfer the remainder of BB and its successor edges to exitMBB. 1112 exitMBB->splice(exitMBB->begin(), BB, 1113 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 1114 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 1115 1116 BB->addSuccessor(loopMBB); 1117 loopMBB->addSuccessor(loopMBB); 1118 loopMBB->addSuccessor(sinkMBB); 1119 sinkMBB->addSuccessor(exitMBB); 1120 1121 // thisMBB: 1122 // addiu masklsb2,$0,-4 # 0xfffffffc 1123 // and alignedaddr,ptr,masklsb2 1124 // andi ptrlsb2,ptr,3 1125 // sll shiftamt,ptrlsb2,3 1126 // ori maskupper,$0,255 # 0xff 1127 // sll mask,maskupper,shiftamt 1128 // nor mask2,$0,mask 1129 // sll incr2,incr,shiftamt 1130 1131 int64_t MaskImm = (Size == 1) ? 255 : 65535; 1132 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2) 1133 .addReg(Mips::ZERO).addImm(-4); 1134 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr) 1135 .addReg(Ptr).addReg(MaskLSB2); 1136 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3); 1137 if (Subtarget->isLittle()) { 1138 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3); 1139 } else { 1140 unsigned Off = RegInfo.createVirtualRegister(RC); 1141 BuildMI(BB, DL, TII->get(Mips::XORi), Off) 1142 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2); 1143 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3); 1144 } 1145 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper) 1146 .addReg(Mips::ZERO).addImm(MaskImm); 1147 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask) 1148 .addReg(MaskUpper).addReg(ShiftAmt); 1149 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask); 1150 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt); 1151 1152 // atomic.load.binop 1153 // loopMBB: 1154 // ll oldval,0(alignedaddr) 1155 // binop binopres,oldval,incr2 1156 // and newval,binopres,mask 1157 // and maskedoldval0,oldval,mask2 1158 // or storeval,maskedoldval0,newval 1159 // sc success,storeval,0(alignedaddr) 1160 // beq success,$0,loopMBB 1161 1162 // atomic.swap 1163 // loopMBB: 1164 // ll oldval,0(alignedaddr) 1165 // and newval,incr2,mask 1166 // and maskedoldval0,oldval,mask2 1167 // or storeval,maskedoldval0,newval 1168 // sc success,storeval,0(alignedaddr) 1169 // beq success,$0,loopMBB 1170 1171 BB = loopMBB; 1172 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0); 1173 if (Nand) { 1174 // and andres, oldval, incr2 1175 // nor binopres, $0, andres 1176 // and newval, binopres, mask 1177 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2); 1178 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes) 1179 .addReg(Mips::ZERO).addReg(AndRes); 1180 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask); 1181 } else if (BinOpcode) { 1182 // <binop> binopres, oldval, incr2 1183 // and newval, binopres, mask 1184 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2); 1185 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask); 1186 } else { // atomic.swap 1187 // and newval, incr2, mask 1188 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask); 1189 } 1190 1191 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0) 1192 .addReg(OldVal).addReg(Mask2); 1193 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal) 1194 .addReg(MaskedOldVal0).addReg(NewVal); 1195 BuildMI(BB, DL, TII->get(Mips::SC), Success) 1196 .addReg(StoreVal).addReg(AlignedAddr).addImm(0); 1197 BuildMI(BB, DL, TII->get(Mips::BEQ)) 1198 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB); 1199 1200 // sinkMBB: 1201 // and maskedoldval1,oldval,mask 1202 // srl srlres,maskedoldval1,shiftamt 1203 // sign_extend dest,srlres 1204 BB = sinkMBB; 1205 1206 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1) 1207 .addReg(OldVal).addReg(Mask); 1208 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes) 1209 .addReg(MaskedOldVal1).addReg(ShiftAmt); 1210 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes); 1211 1212 MI->eraseFromParent(); // The instruction is gone now. 1213 1214 return exitMBB; 1215 } 1216 1217 MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI, 1218 MachineBasicBlock *BB, 1219 unsigned Size) const { 1220 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap."); 1221 1222 MachineFunction *MF = BB->getParent(); 1223 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 1224 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8)); 1225 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 1226 DebugLoc DL = MI->getDebugLoc(); 1227 unsigned LL, SC, ZERO, BNE, BEQ; 1228 1229 if (Size == 4) { 1230 LL = isMicroMips ? Mips::LL_MM : Mips::LL; 1231 SC = isMicroMips ? Mips::SC_MM : Mips::SC; 1232 ZERO = Mips::ZERO; 1233 BNE = Mips::BNE; 1234 BEQ = Mips::BEQ; 1235 } else { 1236 LL = Mips::LLD; 1237 SC = Mips::SCD; 1238 ZERO = Mips::ZERO_64; 1239 BNE = Mips::BNE64; 1240 BEQ = Mips::BEQ64; 1241 } 1242 1243 unsigned Dest = MI->getOperand(0).getReg(); 1244 unsigned Ptr = MI->getOperand(1).getReg(); 1245 unsigned OldVal = MI->getOperand(2).getReg(); 1246 unsigned NewVal = MI->getOperand(3).getReg(); 1247 1248 unsigned Success = RegInfo.createVirtualRegister(RC); 1249 1250 // insert new blocks after the current block 1251 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 1252 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB); 1253 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB); 1254 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 1255 MachineFunction::iterator It = BB; 1256 ++It; 1257 MF->insert(It, loop1MBB); 1258 MF->insert(It, loop2MBB); 1259 MF->insert(It, exitMBB); 1260 1261 // Transfer the remainder of BB and its successor edges to exitMBB. 1262 exitMBB->splice(exitMBB->begin(), BB, 1263 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 1264 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 1265 1266 // thisMBB: 1267 // ... 1268 // fallthrough --> loop1MBB 1269 BB->addSuccessor(loop1MBB); 1270 loop1MBB->addSuccessor(exitMBB); 1271 loop1MBB->addSuccessor(loop2MBB); 1272 loop2MBB->addSuccessor(loop1MBB); 1273 loop2MBB->addSuccessor(exitMBB); 1274 1275 // loop1MBB: 1276 // ll dest, 0(ptr) 1277 // bne dest, oldval, exitMBB 1278 BB = loop1MBB; 1279 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0); 1280 BuildMI(BB, DL, TII->get(BNE)) 1281 .addReg(Dest).addReg(OldVal).addMBB(exitMBB); 1282 1283 // loop2MBB: 1284 // sc success, newval, 0(ptr) 1285 // beq success, $0, loop1MBB 1286 BB = loop2MBB; 1287 BuildMI(BB, DL, TII->get(SC), Success) 1288 .addReg(NewVal).addReg(Ptr).addImm(0); 1289 BuildMI(BB, DL, TII->get(BEQ)) 1290 .addReg(Success).addReg(ZERO).addMBB(loop1MBB); 1291 1292 MI->eraseFromParent(); // The instruction is gone now. 1293 1294 return exitMBB; 1295 } 1296 1297 MachineBasicBlock * 1298 MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI, 1299 MachineBasicBlock *BB, 1300 unsigned Size) const { 1301 assert((Size == 1 || Size == 2) && 1302 "Unsupported size for EmitAtomicCmpSwapPartial."); 1303 1304 MachineFunction *MF = BB->getParent(); 1305 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 1306 const TargetRegisterClass *RC = getRegClassFor(MVT::i32); 1307 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 1308 DebugLoc DL = MI->getDebugLoc(); 1309 1310 unsigned Dest = MI->getOperand(0).getReg(); 1311 unsigned Ptr = MI->getOperand(1).getReg(); 1312 unsigned CmpVal = MI->getOperand(2).getReg(); 1313 unsigned NewVal = MI->getOperand(3).getReg(); 1314 1315 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC); 1316 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC); 1317 unsigned Mask = RegInfo.createVirtualRegister(RC); 1318 unsigned Mask2 = RegInfo.createVirtualRegister(RC); 1319 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC); 1320 unsigned OldVal = RegInfo.createVirtualRegister(RC); 1321 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC); 1322 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC); 1323 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC); 1324 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC); 1325 unsigned MaskUpper = RegInfo.createVirtualRegister(RC); 1326 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC); 1327 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC); 1328 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC); 1329 unsigned StoreVal = RegInfo.createVirtualRegister(RC); 1330 unsigned SrlRes = RegInfo.createVirtualRegister(RC); 1331 unsigned Success = RegInfo.createVirtualRegister(RC); 1332 1333 // insert new blocks after the current block 1334 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 1335 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB); 1336 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB); 1337 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB); 1338 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 1339 MachineFunction::iterator It = BB; 1340 ++It; 1341 MF->insert(It, loop1MBB); 1342 MF->insert(It, loop2MBB); 1343 MF->insert(It, sinkMBB); 1344 MF->insert(It, exitMBB); 1345 1346 // Transfer the remainder of BB and its successor edges to exitMBB. 1347 exitMBB->splice(exitMBB->begin(), BB, 1348 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 1349 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 1350 1351 BB->addSuccessor(loop1MBB); 1352 loop1MBB->addSuccessor(sinkMBB); 1353 loop1MBB->addSuccessor(loop2MBB); 1354 loop2MBB->addSuccessor(loop1MBB); 1355 loop2MBB->addSuccessor(sinkMBB); 1356 sinkMBB->addSuccessor(exitMBB); 1357 1358 // FIXME: computation of newval2 can be moved to loop2MBB. 1359 // thisMBB: 1360 // addiu masklsb2,$0,-4 # 0xfffffffc 1361 // and alignedaddr,ptr,masklsb2 1362 // andi ptrlsb2,ptr,3 1363 // sll shiftamt,ptrlsb2,3 1364 // ori maskupper,$0,255 # 0xff 1365 // sll mask,maskupper,shiftamt 1366 // nor mask2,$0,mask 1367 // andi maskedcmpval,cmpval,255 1368 // sll shiftedcmpval,maskedcmpval,shiftamt 1369 // andi maskednewval,newval,255 1370 // sll shiftednewval,maskednewval,shiftamt 1371 int64_t MaskImm = (Size == 1) ? 255 : 65535; 1372 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2) 1373 .addReg(Mips::ZERO).addImm(-4); 1374 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr) 1375 .addReg(Ptr).addReg(MaskLSB2); 1376 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3); 1377 if (Subtarget->isLittle()) { 1378 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3); 1379 } else { 1380 unsigned Off = RegInfo.createVirtualRegister(RC); 1381 BuildMI(BB, DL, TII->get(Mips::XORi), Off) 1382 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2); 1383 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3); 1384 } 1385 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper) 1386 .addReg(Mips::ZERO).addImm(MaskImm); 1387 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask) 1388 .addReg(MaskUpper).addReg(ShiftAmt); 1389 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask); 1390 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal) 1391 .addReg(CmpVal).addImm(MaskImm); 1392 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal) 1393 .addReg(MaskedCmpVal).addReg(ShiftAmt); 1394 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal) 1395 .addReg(NewVal).addImm(MaskImm); 1396 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal) 1397 .addReg(MaskedNewVal).addReg(ShiftAmt); 1398 1399 // loop1MBB: 1400 // ll oldval,0(alginedaddr) 1401 // and maskedoldval0,oldval,mask 1402 // bne maskedoldval0,shiftedcmpval,sinkMBB 1403 BB = loop1MBB; 1404 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0); 1405 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0) 1406 .addReg(OldVal).addReg(Mask); 1407 BuildMI(BB, DL, TII->get(Mips::BNE)) 1408 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB); 1409 1410 // loop2MBB: 1411 // and maskedoldval1,oldval,mask2 1412 // or storeval,maskedoldval1,shiftednewval 1413 // sc success,storeval,0(alignedaddr) 1414 // beq success,$0,loop1MBB 1415 BB = loop2MBB; 1416 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1) 1417 .addReg(OldVal).addReg(Mask2); 1418 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal) 1419 .addReg(MaskedOldVal1).addReg(ShiftedNewVal); 1420 BuildMI(BB, DL, TII->get(Mips::SC), Success) 1421 .addReg(StoreVal).addReg(AlignedAddr).addImm(0); 1422 BuildMI(BB, DL, TII->get(Mips::BEQ)) 1423 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB); 1424 1425 // sinkMBB: 1426 // srl srlres,maskedoldval0,shiftamt 1427 // sign_extend dest,srlres 1428 BB = sinkMBB; 1429 1430 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes) 1431 .addReg(MaskedOldVal0).addReg(ShiftAmt); 1432 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes); 1433 1434 MI->eraseFromParent(); // The instruction is gone now. 1435 1436 return exitMBB; 1437 } 1438 1439 MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI, 1440 MachineBasicBlock *BB) const { 1441 MachineFunction *MF = BB->getParent(); 1442 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); 1443 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); 1444 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 1445 DebugLoc DL = MI->getDebugLoc(); 1446 MachineBasicBlock::iterator II(MI); 1447 1448 unsigned Fc = MI->getOperand(1).getReg(); 1449 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID); 1450 1451 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass); 1452 1453 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2) 1454 .addImm(0) 1455 .addReg(Fc) 1456 .addImm(Mips::sub_lo); 1457 1458 // We don't erase the original instruction, we just replace the condition 1459 // register with the 64-bit super-register. 1460 MI->getOperand(1).setReg(Fc2); 1461 1462 return BB; 1463 } 1464 1465 //===----------------------------------------------------------------------===// 1466 // Misc Lower Operation implementation 1467 //===----------------------------------------------------------------------===// 1468 SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const { 1469 SDValue Chain = Op.getOperand(0); 1470 SDValue Table = Op.getOperand(1); 1471 SDValue Index = Op.getOperand(2); 1472 SDLoc DL(Op); 1473 EVT PTy = getPointerTy(); 1474 unsigned EntrySize = 1475 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout()); 1476 1477 Index = DAG.getNode(ISD::MUL, DL, PTy, Index, 1478 DAG.getConstant(EntrySize, PTy)); 1479 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table); 1480 1481 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8); 1482 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr, 1483 MachinePointerInfo::getJumpTable(), MemVT, false, false, 1484 0); 1485 Chain = Addr.getValue(1); 1486 1487 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || isN64()) { 1488 // For PIC, the sequence is: 1489 // BRIND(load(Jumptable + index) + RelocBase) 1490 // RelocBase can be JumpTable, GOT or some sort of global base. 1491 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr, 1492 getPICJumpTableRelocBase(Table, DAG)); 1493 } 1494 1495 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr); 1496 } 1497 1498 SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const { 1499 // The first operand is the chain, the second is the condition, the third is 1500 // the block to branch to if the condition is true. 1501 SDValue Chain = Op.getOperand(0); 1502 SDValue Dest = Op.getOperand(2); 1503 SDLoc DL(Op); 1504 1505 assert(!Subtarget->hasMips32r6() && !Subtarget->hasMips64r6()); 1506 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1)); 1507 1508 // Return if flag is not set by a floating point comparison. 1509 if (CondRes.getOpcode() != MipsISD::FPCmp) 1510 return Op; 1511 1512 SDValue CCNode = CondRes.getOperand(2); 1513 Mips::CondCode CC = 1514 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue(); 1515 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T; 1516 SDValue BrCode = DAG.getConstant(Opc, MVT::i32); 1517 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32); 1518 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode, 1519 FCC0, Dest, CondRes); 1520 } 1521 1522 SDValue MipsTargetLowering:: 1523 lowerSELECT(SDValue Op, SelectionDAG &DAG) const 1524 { 1525 assert(!Subtarget->hasMips32r6() && !Subtarget->hasMips64r6()); 1526 SDValue Cond = createFPCmp(DAG, Op.getOperand(0)); 1527 1528 // Return if flag is not set by a floating point comparison. 1529 if (Cond.getOpcode() != MipsISD::FPCmp) 1530 return Op; 1531 1532 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2), 1533 SDLoc(Op)); 1534 } 1535 1536 SDValue MipsTargetLowering:: 1537 lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const 1538 { 1539 SDLoc DL(Op); 1540 EVT Ty = Op.getOperand(0).getValueType(); 1541 SDValue Cond = DAG.getNode(ISD::SETCC, DL, 1542 getSetCCResultType(*DAG.getContext(), Ty), 1543 Op.getOperand(0), Op.getOperand(1), 1544 Op.getOperand(4)); 1545 1546 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2), 1547 Op.getOperand(3)); 1548 } 1549 1550 SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const { 1551 assert(!Subtarget->hasMips32r6() && !Subtarget->hasMips64r6()); 1552 SDValue Cond = createFPCmp(DAG, Op); 1553 1554 assert(Cond.getOpcode() == MipsISD::FPCmp && 1555 "Floating point operand expected."); 1556 1557 SDValue True = DAG.getConstant(1, MVT::i32); 1558 SDValue False = DAG.getConstant(0, MVT::i32); 1559 1560 return createCMovFP(DAG, Cond, True, False, SDLoc(Op)); 1561 } 1562 1563 SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op, 1564 SelectionDAG &DAG) const { 1565 // FIXME there isn't actually debug info here 1566 SDLoc DL(Op); 1567 EVT Ty = Op.getValueType(); 1568 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op); 1569 const GlobalValue *GV = N->getGlobal(); 1570 1571 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !isN64()) { 1572 const MipsTargetObjectFile &TLOF = 1573 (const MipsTargetObjectFile&)getObjFileLowering(); 1574 1575 // %gp_rel relocation 1576 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) { 1577 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0, 1578 MipsII::MO_GPREL); 1579 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL, 1580 DAG.getVTList(MVT::i32), GA); 1581 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32); 1582 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode); 1583 } 1584 1585 // %hi/%lo relocation 1586 return getAddrNonPIC(N, Ty, DAG); 1587 } 1588 1589 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV))) 1590 return getAddrLocal(N, Ty, DAG, isN32() || isN64()); 1591 1592 if (LargeGOT) 1593 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16, 1594 MipsII::MO_GOT_LO16, DAG.getEntryNode(), 1595 MachinePointerInfo::getGOT()); 1596 1597 return getAddrGlobal(N, Ty, DAG, (isN32() || isN64()) ? MipsII::MO_GOT_DISP 1598 : MipsII::MO_GOT16, 1599 DAG.getEntryNode(), MachinePointerInfo::getGOT()); 1600 } 1601 1602 SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op, 1603 SelectionDAG &DAG) const { 1604 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op); 1605 EVT Ty = Op.getValueType(); 1606 1607 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !isN64()) 1608 return getAddrNonPIC(N, Ty, DAG); 1609 1610 return getAddrLocal(N, Ty, DAG, isN32() || isN64()); 1611 } 1612 1613 SDValue MipsTargetLowering:: 1614 lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const 1615 { 1616 // If the relocation model is PIC, use the General Dynamic TLS Model or 1617 // Local Dynamic TLS model, otherwise use the Initial Exec or 1618 // Local Exec TLS Model. 1619 1620 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 1621 SDLoc DL(GA); 1622 const GlobalValue *GV = GA->getGlobal(); 1623 EVT PtrVT = getPointerTy(); 1624 1625 TLSModel::Model model = getTargetMachine().getTLSModel(GV); 1626 1627 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) { 1628 // General Dynamic and Local Dynamic TLS Model. 1629 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM 1630 : MipsII::MO_TLSGD; 1631 1632 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag); 1633 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, 1634 getGlobalReg(DAG, PtrVT), TGA); 1635 unsigned PtrSize = PtrVT.getSizeInBits(); 1636 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize); 1637 1638 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT); 1639 1640 ArgListTy Args; 1641 ArgListEntry Entry; 1642 Entry.Node = Argument; 1643 Entry.Ty = PtrTy; 1644 Args.push_back(Entry); 1645 1646 TargetLowering::CallLoweringInfo CLI(DAG); 1647 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode()) 1648 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, &Args, 0); 1649 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 1650 1651 SDValue Ret = CallResult.first; 1652 1653 if (model != TLSModel::LocalDynamic) 1654 return Ret; 1655 1656 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 1657 MipsII::MO_DTPREL_HI); 1658 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi); 1659 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 1660 MipsII::MO_DTPREL_LO); 1661 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo); 1662 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret); 1663 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo); 1664 } 1665 1666 SDValue Offset; 1667 if (model == TLSModel::InitialExec) { 1668 // Initial Exec TLS Model 1669 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 1670 MipsII::MO_GOTTPREL); 1671 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT), 1672 TGA); 1673 Offset = DAG.getLoad(PtrVT, DL, 1674 DAG.getEntryNode(), TGA, MachinePointerInfo(), 1675 false, false, false, 0); 1676 } else { 1677 // Local Exec TLS Model 1678 assert(model == TLSModel::LocalExec); 1679 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 1680 MipsII::MO_TPREL_HI); 1681 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 1682 MipsII::MO_TPREL_LO); 1683 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi); 1684 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo); 1685 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo); 1686 } 1687 1688 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT); 1689 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset); 1690 } 1691 1692 SDValue MipsTargetLowering:: 1693 lowerJumpTable(SDValue Op, SelectionDAG &DAG) const 1694 { 1695 JumpTableSDNode *N = cast<JumpTableSDNode>(Op); 1696 EVT Ty = Op.getValueType(); 1697 1698 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !isN64()) 1699 return getAddrNonPIC(N, Ty, DAG); 1700 1701 return getAddrLocal(N, Ty, DAG, isN32() || isN64()); 1702 } 1703 1704 SDValue MipsTargetLowering:: 1705 lowerConstantPool(SDValue Op, SelectionDAG &DAG) const 1706 { 1707 // gp_rel relocation 1708 // FIXME: we should reference the constant pool using small data sections, 1709 // but the asm printer currently doesn't support this feature without 1710 // hacking it. This feature should come soon so we can uncomment the 1711 // stuff below. 1712 //if (IsInSmallSection(C->getType())) { 1713 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP); 1714 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32); 1715 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode); 1716 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op); 1717 EVT Ty = Op.getValueType(); 1718 1719 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !isN64()) 1720 return getAddrNonPIC(N, Ty, DAG); 1721 1722 return getAddrLocal(N, Ty, DAG, isN32() || isN64()); 1723 } 1724 1725 SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const { 1726 MachineFunction &MF = DAG.getMachineFunction(); 1727 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>(); 1728 1729 SDLoc DL(Op); 1730 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), 1731 getPointerTy()); 1732 1733 // vastart just stores the address of the VarArgsFrameIndex slot into the 1734 // memory location argument. 1735 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 1736 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1), 1737 MachinePointerInfo(SV), false, false, 0); 1738 } 1739 1740 static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, 1741 bool HasExtractInsert) { 1742 EVT TyX = Op.getOperand(0).getValueType(); 1743 EVT TyY = Op.getOperand(1).getValueType(); 1744 SDValue Const1 = DAG.getConstant(1, MVT::i32); 1745 SDValue Const31 = DAG.getConstant(31, MVT::i32); 1746 SDLoc DL(Op); 1747 SDValue Res; 1748 1749 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it 1750 // to i32. 1751 SDValue X = (TyX == MVT::f32) ? 1752 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) : 1753 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0), 1754 Const1); 1755 SDValue Y = (TyY == MVT::f32) ? 1756 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) : 1757 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1), 1758 Const1); 1759 1760 if (HasExtractInsert) { 1761 // ext E, Y, 31, 1 ; extract bit31 of Y 1762 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X 1763 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1); 1764 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X); 1765 } else { 1766 // sll SllX, X, 1 1767 // srl SrlX, SllX, 1 1768 // srl SrlY, Y, 31 1769 // sll SllY, SrlX, 31 1770 // or Or, SrlX, SllY 1771 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1); 1772 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1); 1773 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31); 1774 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31); 1775 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY); 1776 } 1777 1778 if (TyX == MVT::f32) 1779 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res); 1780 1781 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, 1782 Op.getOperand(0), DAG.getConstant(0, MVT::i32)); 1783 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res); 1784 } 1785 1786 static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, 1787 bool HasExtractInsert) { 1788 unsigned WidthX = Op.getOperand(0).getValueSizeInBits(); 1789 unsigned WidthY = Op.getOperand(1).getValueSizeInBits(); 1790 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY); 1791 SDValue Const1 = DAG.getConstant(1, MVT::i32); 1792 SDLoc DL(Op); 1793 1794 // Bitcast to integer nodes. 1795 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0)); 1796 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1)); 1797 1798 if (HasExtractInsert) { 1799 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y 1800 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X 1801 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y, 1802 DAG.getConstant(WidthY - 1, MVT::i32), Const1); 1803 1804 if (WidthX > WidthY) 1805 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E); 1806 else if (WidthY > WidthX) 1807 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E); 1808 1809 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E, 1810 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X); 1811 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I); 1812 } 1813 1814 // (d)sll SllX, X, 1 1815 // (d)srl SrlX, SllX, 1 1816 // (d)srl SrlY, Y, width(Y)-1 1817 // (d)sll SllY, SrlX, width(Y)-1 1818 // or Or, SrlX, SllY 1819 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1); 1820 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1); 1821 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y, 1822 DAG.getConstant(WidthY - 1, MVT::i32)); 1823 1824 if (WidthX > WidthY) 1825 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY); 1826 else if (WidthY > WidthX) 1827 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY); 1828 1829 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY, 1830 DAG.getConstant(WidthX - 1, MVT::i32)); 1831 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY); 1832 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or); 1833 } 1834 1835 SDValue 1836 MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { 1837 if (Subtarget->isGP64bit()) 1838 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasExtractInsert()); 1839 1840 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasExtractInsert()); 1841 } 1842 1843 SDValue MipsTargetLowering:: 1844 lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { 1845 // check the depth 1846 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) && 1847 "Frame address can only be determined for current frame."); 1848 1849 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 1850 MFI->setFrameAddressIsTaken(true); 1851 EVT VT = Op.getValueType(); 1852 SDLoc DL(Op); 1853 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, 1854 isN64() ? Mips::FP_64 : Mips::FP, VT); 1855 return FrameAddr; 1856 } 1857 1858 SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op, 1859 SelectionDAG &DAG) const { 1860 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 1861 return SDValue(); 1862 1863 // check the depth 1864 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) && 1865 "Return address can be determined only for current frame."); 1866 1867 MachineFunction &MF = DAG.getMachineFunction(); 1868 MachineFrameInfo *MFI = MF.getFrameInfo(); 1869 MVT VT = Op.getSimpleValueType(); 1870 unsigned RA = isN64() ? Mips::RA_64 : Mips::RA; 1871 MFI->setReturnAddressIsTaken(true); 1872 1873 // Return RA, which contains the return address. Mark it an implicit live-in. 1874 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT)); 1875 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT); 1876 } 1877 1878 // An EH_RETURN is the result of lowering llvm.eh.return which in turn is 1879 // generated from __builtin_eh_return (offset, handler) 1880 // The effect of this is to adjust the stack pointer by "offset" 1881 // and then branch to "handler". 1882 SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG) 1883 const { 1884 MachineFunction &MF = DAG.getMachineFunction(); 1885 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 1886 1887 MipsFI->setCallsEhReturn(); 1888 SDValue Chain = Op.getOperand(0); 1889 SDValue Offset = Op.getOperand(1); 1890 SDValue Handler = Op.getOperand(2); 1891 SDLoc DL(Op); 1892 EVT Ty = isN64() ? MVT::i64 : MVT::i32; 1893 1894 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and 1895 // EH_RETURN nodes, so that instructions are emitted back-to-back. 1896 unsigned OffsetReg = isN64() ? Mips::V1_64 : Mips::V1; 1897 unsigned AddrReg = isN64() ? Mips::V0_64 : Mips::V0; 1898 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue()); 1899 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1)); 1900 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain, 1901 DAG.getRegister(OffsetReg, Ty), 1902 DAG.getRegister(AddrReg, getPointerTy()), 1903 Chain.getValue(1)); 1904 } 1905 1906 SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op, 1907 SelectionDAG &DAG) const { 1908 // FIXME: Need pseudo-fence for 'singlethread' fences 1909 // FIXME: Set SType for weaker fences where supported/appropriate. 1910 unsigned SType = 0; 1911 SDLoc DL(Op); 1912 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0), 1913 DAG.getConstant(SType, MVT::i32)); 1914 } 1915 1916 SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op, 1917 SelectionDAG &DAG) const { 1918 SDLoc DL(Op); 1919 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1); 1920 SDValue Shamt = Op.getOperand(2); 1921 1922 // if shamt < 32: 1923 // lo = (shl lo, shamt) 1924 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt)) 1925 // else: 1926 // lo = 0 1927 // hi = (shl lo, shamt[4:0]) 1928 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt, 1929 DAG.getConstant(-1, MVT::i32)); 1930 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, 1931 DAG.getConstant(1, MVT::i32)); 1932 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo, 1933 Not); 1934 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt); 1935 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo); 1936 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt); 1937 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt, 1938 DAG.getConstant(0x20, MVT::i32)); 1939 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, 1940 DAG.getConstant(0, MVT::i32), ShiftLeftLo); 1941 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or); 1942 1943 SDValue Ops[2] = {Lo, Hi}; 1944 return DAG.getMergeValues(Ops, DL); 1945 } 1946 1947 SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG, 1948 bool IsSRA) const { 1949 SDLoc DL(Op); 1950 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1); 1951 SDValue Shamt = Op.getOperand(2); 1952 1953 // if shamt < 32: 1954 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt)) 1955 // if isSRA: 1956 // hi = (sra hi, shamt) 1957 // else: 1958 // hi = (srl hi, shamt) 1959 // else: 1960 // if isSRA: 1961 // lo = (sra hi, shamt[4:0]) 1962 // hi = (sra hi, 31) 1963 // else: 1964 // lo = (srl hi, shamt[4:0]) 1965 // hi = 0 1966 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt, 1967 DAG.getConstant(-1, MVT::i32)); 1968 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, 1969 DAG.getConstant(1, MVT::i32)); 1970 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not); 1971 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt); 1972 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo); 1973 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32, 1974 Hi, Shamt); 1975 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt, 1976 DAG.getConstant(0x20, MVT::i32)); 1977 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi, 1978 DAG.getConstant(31, MVT::i32)); 1979 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or); 1980 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, 1981 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32), 1982 ShiftRightHi); 1983 1984 SDValue Ops[2] = {Lo, Hi}; 1985 return DAG.getMergeValues(Ops, DL); 1986 } 1987 1988 static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD, 1989 SDValue Chain, SDValue Src, unsigned Offset) { 1990 SDValue Ptr = LD->getBasePtr(); 1991 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT(); 1992 EVT BasePtrVT = Ptr.getValueType(); 1993 SDLoc DL(LD); 1994 SDVTList VTList = DAG.getVTList(VT, MVT::Other); 1995 1996 if (Offset) 1997 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr, 1998 DAG.getConstant(Offset, BasePtrVT)); 1999 2000 SDValue Ops[] = { Chain, Ptr, Src }; 2001 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT, 2002 LD->getMemOperand()); 2003 } 2004 2005 // Expand an unaligned 32 or 64-bit integer load node. 2006 SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const { 2007 LoadSDNode *LD = cast<LoadSDNode>(Op); 2008 EVT MemVT = LD->getMemoryVT(); 2009 2010 if (Subtarget->systemSupportsUnalignedAccess()) 2011 return Op; 2012 2013 // Return if load is aligned or if MemVT is neither i32 nor i64. 2014 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) || 2015 ((MemVT != MVT::i32) && (MemVT != MVT::i64))) 2016 return SDValue(); 2017 2018 bool IsLittle = Subtarget->isLittle(); 2019 EVT VT = Op.getValueType(); 2020 ISD::LoadExtType ExtType = LD->getExtensionType(); 2021 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT); 2022 2023 assert((VT == MVT::i32) || (VT == MVT::i64)); 2024 2025 // Expand 2026 // (set dst, (i64 (load baseptr))) 2027 // to 2028 // (set tmp, (ldl (add baseptr, 7), undef)) 2029 // (set dst, (ldr baseptr, tmp)) 2030 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) { 2031 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef, 2032 IsLittle ? 7 : 0); 2033 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL, 2034 IsLittle ? 0 : 7); 2035 } 2036 2037 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef, 2038 IsLittle ? 3 : 0); 2039 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL, 2040 IsLittle ? 0 : 3); 2041 2042 // Expand 2043 // (set dst, (i32 (load baseptr))) or 2044 // (set dst, (i64 (sextload baseptr))) or 2045 // (set dst, (i64 (extload baseptr))) 2046 // to 2047 // (set tmp, (lwl (add baseptr, 3), undef)) 2048 // (set dst, (lwr baseptr, tmp)) 2049 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) || 2050 (ExtType == ISD::EXTLOAD)) 2051 return LWR; 2052 2053 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD)); 2054 2055 // Expand 2056 // (set dst, (i64 (zextload baseptr))) 2057 // to 2058 // (set tmp0, (lwl (add baseptr, 3), undef)) 2059 // (set tmp1, (lwr baseptr, tmp0)) 2060 // (set tmp2, (shl tmp1, 32)) 2061 // (set dst, (srl tmp2, 32)) 2062 SDLoc DL(LD); 2063 SDValue Const32 = DAG.getConstant(32, MVT::i32); 2064 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32); 2065 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32); 2066 SDValue Ops[] = { SRL, LWR.getValue(1) }; 2067 return DAG.getMergeValues(Ops, DL); 2068 } 2069 2070 static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD, 2071 SDValue Chain, unsigned Offset) { 2072 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue(); 2073 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType(); 2074 SDLoc DL(SD); 2075 SDVTList VTList = DAG.getVTList(MVT::Other); 2076 2077 if (Offset) 2078 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr, 2079 DAG.getConstant(Offset, BasePtrVT)); 2080 2081 SDValue Ops[] = { Chain, Value, Ptr }; 2082 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT, 2083 SD->getMemOperand()); 2084 } 2085 2086 // Expand an unaligned 32 or 64-bit integer store node. 2087 static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG, 2088 bool IsLittle) { 2089 SDValue Value = SD->getValue(), Chain = SD->getChain(); 2090 EVT VT = Value.getValueType(); 2091 2092 // Expand 2093 // (store val, baseptr) or 2094 // (truncstore val, baseptr) 2095 // to 2096 // (swl val, (add baseptr, 3)) 2097 // (swr val, baseptr) 2098 if ((VT == MVT::i32) || SD->isTruncatingStore()) { 2099 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain, 2100 IsLittle ? 3 : 0); 2101 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3); 2102 } 2103 2104 assert(VT == MVT::i64); 2105 2106 // Expand 2107 // (store val, baseptr) 2108 // to 2109 // (sdl val, (add baseptr, 7)) 2110 // (sdr val, baseptr) 2111 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0); 2112 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7); 2113 } 2114 2115 // Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr). 2116 static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) { 2117 SDValue Val = SD->getValue(); 2118 2119 if (Val.getOpcode() != ISD::FP_TO_SINT) 2120 return SDValue(); 2121 2122 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits()); 2123 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy, 2124 Val.getOperand(0)); 2125 2126 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(), 2127 SD->getPointerInfo(), SD->isVolatile(), 2128 SD->isNonTemporal(), SD->getAlignment()); 2129 } 2130 2131 SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const { 2132 StoreSDNode *SD = cast<StoreSDNode>(Op); 2133 EVT MemVT = SD->getMemoryVT(); 2134 2135 // Lower unaligned integer stores. 2136 if (!Subtarget->systemSupportsUnalignedAccess() && 2137 (SD->getAlignment() < MemVT.getSizeInBits() / 8) && 2138 ((MemVT == MVT::i32) || (MemVT == MVT::i64))) 2139 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle()); 2140 2141 return lowerFP_TO_SINT_STORE(SD, DAG); 2142 } 2143 2144 SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const { 2145 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR 2146 || cast<ConstantSDNode> 2147 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0 2148 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET) 2149 return SDValue(); 2150 2151 // The pattern 2152 // (add (frameaddr 0), (frame_to_args_offset)) 2153 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to 2154 // (add FrameObject, 0) 2155 // where FrameObject is a fixed StackObject with offset 0 which points to 2156 // the old stack pointer. 2157 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 2158 EVT ValTy = Op->getValueType(0); 2159 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false); 2160 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy); 2161 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr, 2162 DAG.getConstant(0, ValTy)); 2163 } 2164 2165 SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op, 2166 SelectionDAG &DAG) const { 2167 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits()); 2168 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy, 2169 Op.getOperand(0)); 2170 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc); 2171 } 2172 2173 //===----------------------------------------------------------------------===// 2174 // Calling Convention Implementation 2175 //===----------------------------------------------------------------------===// 2176 2177 //===----------------------------------------------------------------------===// 2178 // TODO: Implement a generic logic using tblgen that can support this. 2179 // Mips O32 ABI rules: 2180 // --- 2181 // i32 - Passed in A0, A1, A2, A3 and stack 2182 // f32 - Only passed in f32 registers if no int reg has been used yet to hold 2183 // an argument. Otherwise, passed in A1, A2, A3 and stack. 2184 // f64 - Only passed in two aliased f32 registers if no int reg has been used 2185 // yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is 2186 // not used, it must be shadowed. If only A3 is avaiable, shadow it and 2187 // go to stack. 2188 // 2189 // For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack. 2190 //===----------------------------------------------------------------------===// 2191 2192 static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT, 2193 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, 2194 CCState &State, const MCPhysReg *F64Regs) { 2195 2196 static const unsigned IntRegsSize = 4, FloatRegsSize = 2; 2197 2198 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 }; 2199 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 }; 2200 2201 // Do not process byval args here. 2202 if (ArgFlags.isByVal()) 2203 return true; 2204 2205 // Promote i8 and i16 2206 if (LocVT == MVT::i8 || LocVT == MVT::i16) { 2207 LocVT = MVT::i32; 2208 if (ArgFlags.isSExt()) 2209 LocInfo = CCValAssign::SExt; 2210 else if (ArgFlags.isZExt()) 2211 LocInfo = CCValAssign::ZExt; 2212 else 2213 LocInfo = CCValAssign::AExt; 2214 } 2215 2216 unsigned Reg; 2217 2218 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following 2219 // is true: function is vararg, argument is 3rd or higher, there is previous 2220 // argument which is not f32 or f64. 2221 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 2222 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo; 2223 unsigned OrigAlign = ArgFlags.getOrigAlign(); 2224 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8); 2225 2226 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) { 2227 Reg = State.AllocateReg(IntRegs, IntRegsSize); 2228 // If this is the first part of an i64 arg, 2229 // the allocated register must be either A0 or A2. 2230 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3)) 2231 Reg = State.AllocateReg(IntRegs, IntRegsSize); 2232 LocVT = MVT::i32; 2233 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) { 2234 // Allocate int register and shadow next int register. If first 2235 // available register is Mips::A1 or Mips::A3, shadow it too. 2236 Reg = State.AllocateReg(IntRegs, IntRegsSize); 2237 if (Reg == Mips::A1 || Reg == Mips::A3) 2238 Reg = State.AllocateReg(IntRegs, IntRegsSize); 2239 State.AllocateReg(IntRegs, IntRegsSize); 2240 LocVT = MVT::i32; 2241 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) { 2242 // we are guaranteed to find an available float register 2243 if (ValVT == MVT::f32) { 2244 Reg = State.AllocateReg(F32Regs, FloatRegsSize); 2245 // Shadow int register 2246 State.AllocateReg(IntRegs, IntRegsSize); 2247 } else { 2248 Reg = State.AllocateReg(F64Regs, FloatRegsSize); 2249 // Shadow int registers 2250 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize); 2251 if (Reg2 == Mips::A1 || Reg2 == Mips::A3) 2252 State.AllocateReg(IntRegs, IntRegsSize); 2253 State.AllocateReg(IntRegs, IntRegsSize); 2254 } 2255 } else 2256 llvm_unreachable("Cannot handle this ValVT."); 2257 2258 if (!Reg) { 2259 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3, 2260 OrigAlign); 2261 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo)); 2262 } else 2263 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 2264 2265 return false; 2266 } 2267 2268 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, 2269 MVT LocVT, CCValAssign::LocInfo LocInfo, 2270 ISD::ArgFlagsTy ArgFlags, CCState &State) { 2271 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 }; 2272 2273 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs); 2274 } 2275 2276 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, 2277 MVT LocVT, CCValAssign::LocInfo LocInfo, 2278 ISD::ArgFlagsTy ArgFlags, CCState &State) { 2279 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 }; 2280 2281 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs); 2282 } 2283 2284 #include "MipsGenCallingConv.inc" 2285 2286 //===----------------------------------------------------------------------===// 2287 // Call Calling Convention Implementation 2288 //===----------------------------------------------------------------------===// 2289 2290 // Return next O32 integer argument register. 2291 static unsigned getNextIntArgReg(unsigned Reg) { 2292 assert((Reg == Mips::A0) || (Reg == Mips::A2)); 2293 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3; 2294 } 2295 2296 SDValue 2297 MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset, 2298 SDValue Chain, SDValue Arg, SDLoc DL, 2299 bool IsTailCall, SelectionDAG &DAG) const { 2300 if (!IsTailCall) { 2301 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr, 2302 DAG.getIntPtrConstant(Offset)); 2303 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false, 2304 false, 0); 2305 } 2306 2307 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 2308 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false); 2309 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 2310 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(), 2311 /*isVolatile=*/ true, false, 0); 2312 } 2313 2314 void MipsTargetLowering:: 2315 getOpndList(SmallVectorImpl<SDValue> &Ops, 2316 std::deque< std::pair<unsigned, SDValue> > &RegsToPass, 2317 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage, 2318 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const { 2319 // Insert node "GP copy globalreg" before call to function. 2320 // 2321 // R_MIPS_CALL* operators (emitted when non-internal functions are called 2322 // in PIC mode) allow symbols to be resolved via lazy binding. 2323 // The lazy binding stub requires GP to point to the GOT. 2324 if (IsPICCall && !InternalLinkage) { 2325 unsigned GPReg = isN64() ? Mips::GP_64 : Mips::GP; 2326 EVT Ty = isN64() ? MVT::i64 : MVT::i32; 2327 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty))); 2328 } 2329 2330 // Build a sequence of copy-to-reg nodes chained together with token 2331 // chain and flag operands which copy the outgoing args into registers. 2332 // The InFlag in necessary since all emitted instructions must be 2333 // stuck together. 2334 SDValue InFlag; 2335 2336 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 2337 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first, 2338 RegsToPass[i].second, InFlag); 2339 InFlag = Chain.getValue(1); 2340 } 2341 2342 // Add argument registers to the end of the list so that they are 2343 // known live into the call. 2344 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 2345 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first, 2346 RegsToPass[i].second.getValueType())); 2347 2348 // Add a register mask operand representing the call-preserved registers. 2349 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); 2350 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv); 2351 assert(Mask && "Missing call preserved mask for calling convention"); 2352 if (Subtarget->inMips16HardFloat()) { 2353 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) { 2354 llvm::StringRef Sym = G->getGlobal()->getName(); 2355 Function *F = G->getGlobal()->getParent()->getFunction(Sym); 2356 if (F && F->hasFnAttribute("__Mips16RetHelper")) { 2357 Mask = MipsRegisterInfo::getMips16RetHelperMask(); 2358 } 2359 } 2360 } 2361 Ops.push_back(CLI.DAG.getRegisterMask(Mask)); 2362 2363 if (InFlag.getNode()) 2364 Ops.push_back(InFlag); 2365 } 2366 2367 /// LowerCall - functions arguments are copied from virtual regs to 2368 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted. 2369 SDValue 2370 MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 2371 SmallVectorImpl<SDValue> &InVals) const { 2372 SelectionDAG &DAG = CLI.DAG; 2373 SDLoc DL = CLI.DL; 2374 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 2375 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 2376 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 2377 SDValue Chain = CLI.Chain; 2378 SDValue Callee = CLI.Callee; 2379 bool &IsTailCall = CLI.IsTailCall; 2380 CallingConv::ID CallConv = CLI.CallConv; 2381 bool IsVarArg = CLI.IsVarArg; 2382 2383 MachineFunction &MF = DAG.getMachineFunction(); 2384 MachineFrameInfo *MFI = MF.getFrameInfo(); 2385 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering(); 2386 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>(); 2387 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_; 2388 2389 // Analyze operands of the call, assigning locations to each operand. 2390 SmallVector<CCValAssign, 16> ArgLocs; 2391 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), 2392 getTargetMachine(), ArgLocs, *DAG.getContext()); 2393 MipsCC::SpecialCallingConvType SpecialCallingConv = 2394 getSpecialCallingConv(Callee); 2395 MipsCC MipsCCInfo(CallConv, isO32(), Subtarget->isFP64bit(), CCInfo, 2396 SpecialCallingConv); 2397 2398 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg, 2399 Subtarget->mipsSEUsesSoftFloat(), 2400 Callee.getNode(), CLI.getArgs()); 2401 2402 // Get a count of how many bytes are to be pushed on the stack. 2403 unsigned NextStackOffset = CCInfo.getNextStackOffset(); 2404 2405 // Check if it's really possible to do a tail call. 2406 if (IsTailCall) 2407 IsTailCall = 2408 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset, 2409 *MF.getInfo<MipsFunctionInfo>()); 2410 2411 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall()) 2412 report_fatal_error("failed to perform tail call elimination on a call " 2413 "site marked musttail"); 2414 2415 if (IsTailCall) 2416 ++NumTailCalls; 2417 2418 // Chain is the output chain of the last Load/Store or CopyToReg node. 2419 // ByValChain is the output chain of the last Memcpy node created for copying 2420 // byval arguments to the stack. 2421 unsigned StackAlignment = TFL->getStackAlignment(); 2422 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment); 2423 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true); 2424 2425 if (!IsTailCall) 2426 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL); 2427 2428 SDValue StackPtr = DAG.getCopyFromReg( 2429 Chain, DL, isN64() ? Mips::SP_64 : Mips::SP, getPointerTy()); 2430 2431 // With EABI is it possible to have 16 args on registers. 2432 std::deque< std::pair<unsigned, SDValue> > RegsToPass; 2433 SmallVector<SDValue, 8> MemOpChains; 2434 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin(); 2435 2436 // Walk the register/memloc assignments, inserting copies/loads. 2437 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 2438 SDValue Arg = OutVals[i]; 2439 CCValAssign &VA = ArgLocs[i]; 2440 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT(); 2441 ISD::ArgFlagsTy Flags = Outs[i].Flags; 2442 2443 // ByVal Arg. 2444 if (Flags.isByVal()) { 2445 assert(Flags.getByValSize() && 2446 "ByVal args of size 0 should have been ignored by front-end."); 2447 assert(ByValArg != MipsCCInfo.byval_end()); 2448 assert(!IsTailCall && 2449 "Do not tail-call optimize if there is a byval argument."); 2450 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg, 2451 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle()); 2452 ++ByValArg; 2453 continue; 2454 } 2455 2456 // Promote the value if needed. 2457 switch (VA.getLocInfo()) { 2458 default: llvm_unreachable("Unknown loc info!"); 2459 case CCValAssign::Full: 2460 if (VA.isRegLoc()) { 2461 if ((ValVT == MVT::f32 && LocVT == MVT::i32) || 2462 (ValVT == MVT::f64 && LocVT == MVT::i64) || 2463 (ValVT == MVT::i64 && LocVT == MVT::f64)) 2464 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg); 2465 else if (ValVT == MVT::f64 && LocVT == MVT::i32) { 2466 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, 2467 Arg, DAG.getConstant(0, MVT::i32)); 2468 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, 2469 Arg, DAG.getConstant(1, MVT::i32)); 2470 if (!Subtarget->isLittle()) 2471 std::swap(Lo, Hi); 2472 unsigned LocRegLo = VA.getLocReg(); 2473 unsigned LocRegHigh = getNextIntArgReg(LocRegLo); 2474 RegsToPass.push_back(std::make_pair(LocRegLo, Lo)); 2475 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi)); 2476 continue; 2477 } 2478 } 2479 break; 2480 case CCValAssign::SExt: 2481 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg); 2482 break; 2483 case CCValAssign::ZExt: 2484 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg); 2485 break; 2486 case CCValAssign::AExt: 2487 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg); 2488 break; 2489 } 2490 2491 // Arguments that can be passed on register must be kept at 2492 // RegsToPass vector 2493 if (VA.isRegLoc()) { 2494 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 2495 continue; 2496 } 2497 2498 // Register can't get to this point... 2499 assert(VA.isMemLoc()); 2500 2501 // emit ISD::STORE whichs stores the 2502 // parameter value to a stack Location 2503 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(), 2504 Chain, Arg, DL, IsTailCall, DAG)); 2505 } 2506 2507 // Transform all store nodes into one single node because all store 2508 // nodes are independent of each other. 2509 if (!MemOpChains.empty()) 2510 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains); 2511 2512 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 2513 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 2514 // node so that legalize doesn't hack it. 2515 bool IsPICCall = (isN64() || IsPIC); // true if calls are translated to 2516 // jalr $25 2517 bool GlobalOrExternal = false, InternalLinkage = false; 2518 SDValue CalleeLo; 2519 EVT Ty = Callee.getValueType(); 2520 2521 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 2522 if (IsPICCall) { 2523 const GlobalValue *Val = G->getGlobal(); 2524 InternalLinkage = Val->hasInternalLinkage(); 2525 2526 if (InternalLinkage) 2527 Callee = getAddrLocal(G, Ty, DAG, isN32() || isN64()); 2528 else if (LargeGOT) 2529 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16, 2530 MipsII::MO_CALL_LO16, Chain, 2531 FuncInfo->callPtrInfo(Val)); 2532 else 2533 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain, 2534 FuncInfo->callPtrInfo(Val)); 2535 } else 2536 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0, 2537 MipsII::MO_NO_FLAG); 2538 GlobalOrExternal = true; 2539 } 2540 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 2541 const char *Sym = S->getSymbol(); 2542 2543 if (!isN64() && !IsPIC) // !N64 && static 2544 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), 2545 MipsII::MO_NO_FLAG); 2546 else if (LargeGOT) 2547 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16, 2548 MipsII::MO_CALL_LO16, Chain, 2549 FuncInfo->callPtrInfo(Sym)); 2550 else // N64 || PIC 2551 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain, 2552 FuncInfo->callPtrInfo(Sym)); 2553 2554 GlobalOrExternal = true; 2555 } 2556 2557 SmallVector<SDValue, 8> Ops(1, Chain); 2558 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 2559 2560 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage, 2561 CLI, Callee, Chain); 2562 2563 if (IsTailCall) 2564 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops); 2565 2566 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops); 2567 SDValue InFlag = Chain.getValue(1); 2568 2569 // Create the CALLSEQ_END node. 2570 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal, 2571 DAG.getIntPtrConstant(0, true), InFlag, DL); 2572 InFlag = Chain.getValue(1); 2573 2574 // Handle result values, copying them out of physregs into vregs that we 2575 // return. 2576 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, 2577 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy); 2578 } 2579 2580 /// LowerCallResult - Lower the result values of a call into the 2581 /// appropriate copies out of appropriate physical registers. 2582 SDValue 2583 MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 2584 CallingConv::ID CallConv, bool IsVarArg, 2585 const SmallVectorImpl<ISD::InputArg> &Ins, 2586 SDLoc DL, SelectionDAG &DAG, 2587 SmallVectorImpl<SDValue> &InVals, 2588 const SDNode *CallNode, 2589 const Type *RetTy) const { 2590 // Assign locations to each value returned by this call. 2591 SmallVector<CCValAssign, 16> RVLocs; 2592 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), 2593 getTargetMachine(), RVLocs, *DAG.getContext()); 2594 MipsCC MipsCCInfo(CallConv, isO32(), Subtarget->isFP64bit(), CCInfo); 2595 2596 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(), 2597 CallNode, RetTy); 2598 2599 // Copy all of the result registers out of their specified physreg. 2600 for (unsigned i = 0; i != RVLocs.size(); ++i) { 2601 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(), 2602 RVLocs[i].getLocVT(), InFlag); 2603 Chain = Val.getValue(1); 2604 InFlag = Val.getValue(2); 2605 2606 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT()) 2607 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val); 2608 2609 InVals.push_back(Val); 2610 } 2611 2612 return Chain; 2613 } 2614 2615 //===----------------------------------------------------------------------===// 2616 // Formal Arguments Calling Convention Implementation 2617 //===----------------------------------------------------------------------===// 2618 /// LowerFormalArguments - transform physical registers into virtual registers 2619 /// and generate load operations for arguments places on the stack. 2620 SDValue 2621 MipsTargetLowering::LowerFormalArguments(SDValue Chain, 2622 CallingConv::ID CallConv, 2623 bool IsVarArg, 2624 const SmallVectorImpl<ISD::InputArg> &Ins, 2625 SDLoc DL, SelectionDAG &DAG, 2626 SmallVectorImpl<SDValue> &InVals) 2627 const { 2628 MachineFunction &MF = DAG.getMachineFunction(); 2629 MachineFrameInfo *MFI = MF.getFrameInfo(); 2630 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 2631 2632 MipsFI->setVarArgsFrameIndex(0); 2633 2634 // Used with vargs to acumulate store chains. 2635 std::vector<SDValue> OutChains; 2636 2637 // Assign locations to all of the incoming arguments. 2638 SmallVector<CCValAssign, 16> ArgLocs; 2639 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), 2640 getTargetMachine(), ArgLocs, *DAG.getContext()); 2641 MipsCC MipsCCInfo(CallConv, isO32(), Subtarget->isFP64bit(), CCInfo); 2642 Function::const_arg_iterator FuncArg = 2643 DAG.getMachineFunction().getFunction()->arg_begin(); 2644 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat(); 2645 2646 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg); 2647 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(), 2648 MipsCCInfo.hasByValArg()); 2649 2650 unsigned CurArgIdx = 0; 2651 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin(); 2652 2653 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 2654 CCValAssign &VA = ArgLocs[i]; 2655 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx); 2656 CurArgIdx = Ins[i].OrigArgIndex; 2657 EVT ValVT = VA.getValVT(); 2658 ISD::ArgFlagsTy Flags = Ins[i].Flags; 2659 bool IsRegLoc = VA.isRegLoc(); 2660 2661 if (Flags.isByVal()) { 2662 assert(Flags.getByValSize() && 2663 "ByVal args of size 0 should have been ignored by front-end."); 2664 assert(ByValArg != MipsCCInfo.byval_end()); 2665 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg, 2666 MipsCCInfo, *ByValArg); 2667 ++ByValArg; 2668 continue; 2669 } 2670 2671 // Arguments stored on registers 2672 if (IsRegLoc) { 2673 MVT RegVT = VA.getLocVT(); 2674 unsigned ArgReg = VA.getLocReg(); 2675 const TargetRegisterClass *RC = getRegClassFor(RegVT); 2676 2677 // Transform the arguments stored on 2678 // physical registers into virtual ones 2679 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC); 2680 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT); 2681 2682 // If this is an 8 or 16-bit value, it has been passed promoted 2683 // to 32 bits. Insert an assert[sz]ext to capture this, then 2684 // truncate to the right size. 2685 if (VA.getLocInfo() != CCValAssign::Full) { 2686 unsigned Opcode = 0; 2687 if (VA.getLocInfo() == CCValAssign::SExt) 2688 Opcode = ISD::AssertSext; 2689 else if (VA.getLocInfo() == CCValAssign::ZExt) 2690 Opcode = ISD::AssertZext; 2691 if (Opcode) 2692 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue, 2693 DAG.getValueType(ValVT)); 2694 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue); 2695 } 2696 2697 // Handle floating point arguments passed in integer registers and 2698 // long double arguments passed in floating point registers. 2699 if ((RegVT == MVT::i32 && ValVT == MVT::f32) || 2700 (RegVT == MVT::i64 && ValVT == MVT::f64) || 2701 (RegVT == MVT::f64 && ValVT == MVT::i64)) 2702 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue); 2703 else if (isO32() && RegVT == MVT::i32 && ValVT == MVT::f64) { 2704 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(), 2705 getNextIntArgReg(ArgReg), RC); 2706 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT); 2707 if (!Subtarget->isLittle()) 2708 std::swap(ArgValue, ArgValue2); 2709 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, 2710 ArgValue, ArgValue2); 2711 } 2712 2713 InVals.push_back(ArgValue); 2714 } else { // VA.isRegLoc() 2715 2716 // sanity check 2717 assert(VA.isMemLoc()); 2718 2719 // The stack pointer offset is relative to the caller stack frame. 2720 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8, 2721 VA.getLocMemOffset(), true); 2722 2723 // Create load nodes to retrieve arguments from the stack 2724 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 2725 SDValue Load = DAG.getLoad(ValVT, DL, Chain, FIN, 2726 MachinePointerInfo::getFixedStack(FI), 2727 false, false, false, 0); 2728 InVals.push_back(Load); 2729 OutChains.push_back(Load.getValue(1)); 2730 } 2731 } 2732 2733 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 2734 // The mips ABIs for returning structs by value requires that we copy 2735 // the sret argument into $v0 for the return. Save the argument into 2736 // a virtual register so that we can access it from the return points. 2737 if (Ins[i].Flags.isSRet()) { 2738 unsigned Reg = MipsFI->getSRetReturnReg(); 2739 if (!Reg) { 2740 Reg = MF.getRegInfo().createVirtualRegister( 2741 getRegClassFor(isN64() ? MVT::i64 : MVT::i32)); 2742 MipsFI->setSRetReturnReg(Reg); 2743 } 2744 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]); 2745 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain); 2746 break; 2747 } 2748 } 2749 2750 if (IsVarArg) 2751 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG); 2752 2753 // All stores are grouped in one node to allow the matching between 2754 // the size of Ins and InVals. This only happens when on varg functions 2755 if (!OutChains.empty()) { 2756 OutChains.push_back(Chain); 2757 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains); 2758 } 2759 2760 return Chain; 2761 } 2762 2763 //===----------------------------------------------------------------------===// 2764 // Return Value Calling Convention Implementation 2765 //===----------------------------------------------------------------------===// 2766 2767 bool 2768 MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 2769 MachineFunction &MF, bool IsVarArg, 2770 const SmallVectorImpl<ISD::OutputArg> &Outs, 2771 LLVMContext &Context) const { 2772 SmallVector<CCValAssign, 16> RVLocs; 2773 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), 2774 RVLocs, Context); 2775 return CCInfo.CheckReturn(Outs, RetCC_Mips); 2776 } 2777 2778 SDValue 2779 MipsTargetLowering::LowerReturn(SDValue Chain, 2780 CallingConv::ID CallConv, bool IsVarArg, 2781 const SmallVectorImpl<ISD::OutputArg> &Outs, 2782 const SmallVectorImpl<SDValue> &OutVals, 2783 SDLoc DL, SelectionDAG &DAG) const { 2784 // CCValAssign - represent the assignment of 2785 // the return value to a location 2786 SmallVector<CCValAssign, 16> RVLocs; 2787 MachineFunction &MF = DAG.getMachineFunction(); 2788 2789 // CCState - Info about the registers and stack slot. 2790 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs, 2791 *DAG.getContext()); 2792 MipsCC MipsCCInfo(CallConv, isO32(), Subtarget->isFP64bit(), CCInfo); 2793 2794 // Analyze return values. 2795 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(), 2796 MF.getFunction()->getReturnType()); 2797 2798 SDValue Flag; 2799 SmallVector<SDValue, 4> RetOps(1, Chain); 2800 2801 // Copy the result values into the output registers. 2802 for (unsigned i = 0; i != RVLocs.size(); ++i) { 2803 SDValue Val = OutVals[i]; 2804 CCValAssign &VA = RVLocs[i]; 2805 assert(VA.isRegLoc() && "Can only return in registers!"); 2806 2807 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT()) 2808 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val); 2809 2810 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag); 2811 2812 // Guarantee that all emitted copies are stuck together with flags. 2813 Flag = Chain.getValue(1); 2814 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2815 } 2816 2817 // The mips ABIs for returning structs by value requires that we copy 2818 // the sret argument into $v0 for the return. We saved the argument into 2819 // a virtual register in the entry block, so now we copy the value out 2820 // and into $v0. 2821 if (MF.getFunction()->hasStructRetAttr()) { 2822 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 2823 unsigned Reg = MipsFI->getSRetReturnReg(); 2824 2825 if (!Reg) 2826 llvm_unreachable("sret virtual register not created in the entry block"); 2827 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy()); 2828 unsigned V0 = isN64() ? Mips::V0_64 : Mips::V0; 2829 2830 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag); 2831 Flag = Chain.getValue(1); 2832 RetOps.push_back(DAG.getRegister(V0, getPointerTy())); 2833 } 2834 2835 RetOps[0] = Chain; // Update chain. 2836 2837 // Add the flag if we have it. 2838 if (Flag.getNode()) 2839 RetOps.push_back(Flag); 2840 2841 // Return on Mips is always a "jr $ra" 2842 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps); 2843 } 2844 2845 //===----------------------------------------------------------------------===// 2846 // Mips Inline Assembly Support 2847 //===----------------------------------------------------------------------===// 2848 2849 /// getConstraintType - Given a constraint letter, return the type of 2850 /// constraint it is for this target. 2851 MipsTargetLowering::ConstraintType MipsTargetLowering:: 2852 getConstraintType(const std::string &Constraint) const 2853 { 2854 // Mips specific constraints 2855 // GCC config/mips/constraints.md 2856 // 2857 // 'd' : An address register. Equivalent to r 2858 // unless generating MIPS16 code. 2859 // 'y' : Equivalent to r; retained for 2860 // backwards compatibility. 2861 // 'c' : A register suitable for use in an indirect 2862 // jump. This will always be $25 for -mabicalls. 2863 // 'l' : The lo register. 1 word storage. 2864 // 'x' : The hilo register pair. Double word storage. 2865 if (Constraint.size() == 1) { 2866 switch (Constraint[0]) { 2867 default : break; 2868 case 'd': 2869 case 'y': 2870 case 'f': 2871 case 'c': 2872 case 'l': 2873 case 'x': 2874 return C_RegisterClass; 2875 case 'R': 2876 return C_Memory; 2877 } 2878 } 2879 return TargetLowering::getConstraintType(Constraint); 2880 } 2881 2882 /// Examine constraint type and operand type and determine a weight value. 2883 /// This object must already have been set up with the operand type 2884 /// and the current alternative constraint selected. 2885 TargetLowering::ConstraintWeight 2886 MipsTargetLowering::getSingleConstraintMatchWeight( 2887 AsmOperandInfo &info, const char *constraint) const { 2888 ConstraintWeight weight = CW_Invalid; 2889 Value *CallOperandVal = info.CallOperandVal; 2890 // If we don't have a value, we can't do a match, 2891 // but allow it at the lowest weight. 2892 if (!CallOperandVal) 2893 return CW_Default; 2894 Type *type = CallOperandVal->getType(); 2895 // Look at the constraint type. 2896 switch (*constraint) { 2897 default: 2898 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 2899 break; 2900 case 'd': 2901 case 'y': 2902 if (type->isIntegerTy()) 2903 weight = CW_Register; 2904 break; 2905 case 'f': // FPU or MSA register 2906 if (Subtarget->hasMSA() && type->isVectorTy() && 2907 cast<VectorType>(type)->getBitWidth() == 128) 2908 weight = CW_Register; 2909 else if (type->isFloatTy()) 2910 weight = CW_Register; 2911 break; 2912 case 'c': // $25 for indirect jumps 2913 case 'l': // lo register 2914 case 'x': // hilo register pair 2915 if (type->isIntegerTy()) 2916 weight = CW_SpecificReg; 2917 break; 2918 case 'I': // signed 16 bit immediate 2919 case 'J': // integer zero 2920 case 'K': // unsigned 16 bit immediate 2921 case 'L': // signed 32 bit immediate where lower 16 bits are 0 2922 case 'N': // immediate in the range of -65535 to -1 (inclusive) 2923 case 'O': // signed 15 bit immediate (+- 16383) 2924 case 'P': // immediate in the range of 65535 to 1 (inclusive) 2925 if (isa<ConstantInt>(CallOperandVal)) 2926 weight = CW_Constant; 2927 break; 2928 case 'R': 2929 weight = CW_Memory; 2930 break; 2931 } 2932 return weight; 2933 } 2934 2935 /// This is a helper function to parse a physical register string and split it 2936 /// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag 2937 /// that is returned indicates whether parsing was successful. The second flag 2938 /// is true if the numeric part exists. 2939 static std::pair<bool, bool> 2940 parsePhysicalReg(const StringRef &C, std::string &Prefix, 2941 unsigned long long &Reg) { 2942 if (C.front() != '{' || C.back() != '}') 2943 return std::make_pair(false, false); 2944 2945 // Search for the first numeric character. 2946 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1; 2947 I = std::find_if(B, E, std::ptr_fun(isdigit)); 2948 2949 Prefix.assign(B, I - B); 2950 2951 // The second flag is set to false if no numeric characters were found. 2952 if (I == E) 2953 return std::make_pair(true, false); 2954 2955 // Parse the numeric characters. 2956 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg), 2957 true); 2958 } 2959 2960 std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering:: 2961 parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const { 2962 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); 2963 const TargetRegisterClass *RC; 2964 std::string Prefix; 2965 unsigned long long Reg; 2966 2967 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg); 2968 2969 if (!R.first) 2970 return std::make_pair(0U, nullptr); 2971 2972 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo. 2973 // No numeric characters follow "hi" or "lo". 2974 if (R.second) 2975 return std::make_pair(0U, nullptr); 2976 2977 RC = TRI->getRegClass(Prefix == "hi" ? 2978 Mips::HI32RegClassID : Mips::LO32RegClassID); 2979 return std::make_pair(*(RC->begin()), RC); 2980 } else if (Prefix.compare(0, 4, "$msa") == 0) { 2981 // Parse $msa(ir|csr|access|save|modify|request|map|unmap) 2982 2983 // No numeric characters follow the name. 2984 if (R.second) 2985 return std::make_pair(0U, nullptr); 2986 2987 Reg = StringSwitch<unsigned long long>(Prefix) 2988 .Case("$msair", Mips::MSAIR) 2989 .Case("$msacsr", Mips::MSACSR) 2990 .Case("$msaaccess", Mips::MSAAccess) 2991 .Case("$msasave", Mips::MSASave) 2992 .Case("$msamodify", Mips::MSAModify) 2993 .Case("$msarequest", Mips::MSARequest) 2994 .Case("$msamap", Mips::MSAMap) 2995 .Case("$msaunmap", Mips::MSAUnmap) 2996 .Default(0); 2997 2998 if (!Reg) 2999 return std::make_pair(0U, nullptr); 3000 3001 RC = TRI->getRegClass(Mips::MSACtrlRegClassID); 3002 return std::make_pair(Reg, RC); 3003 } 3004 3005 if (!R.second) 3006 return std::make_pair(0U, nullptr); 3007 3008 if (Prefix == "$f") { // Parse $f0-$f31. 3009 // If the size of FP registers is 64-bit or Reg is an even number, select 3010 // the 64-bit register class. Otherwise, select the 32-bit register class. 3011 if (VT == MVT::Other) 3012 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32; 3013 3014 RC = getRegClassFor(VT); 3015 3016 if (RC == &Mips::AFGR64RegClass) { 3017 assert(Reg % 2 == 0); 3018 Reg >>= 1; 3019 } 3020 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7. 3021 RC = TRI->getRegClass(Mips::FCCRegClassID); 3022 else if (Prefix == "$w") { // Parse $w0-$w31. 3023 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT); 3024 } else { // Parse $0-$31. 3025 assert(Prefix == "$"); 3026 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT); 3027 } 3028 3029 assert(Reg < RC->getNumRegs()); 3030 return std::make_pair(*(RC->begin() + Reg), RC); 3031 } 3032 3033 /// Given a register class constraint, like 'r', if this corresponds directly 3034 /// to an LLVM register class, return a register of 0 and the register class 3035 /// pointer. 3036 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering:: 3037 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const 3038 { 3039 if (Constraint.size() == 1) { 3040 switch (Constraint[0]) { 3041 case 'd': // Address register. Same as 'r' unless generating MIPS16 code. 3042 case 'y': // Same as 'r'. Exists for compatibility. 3043 case 'r': 3044 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) { 3045 if (Subtarget->inMips16Mode()) 3046 return std::make_pair(0U, &Mips::CPU16RegsRegClass); 3047 return std::make_pair(0U, &Mips::GPR32RegClass); 3048 } 3049 if (VT == MVT::i64 && !isGP64bit()) 3050 return std::make_pair(0U, &Mips::GPR32RegClass); 3051 if (VT == MVT::i64 && isGP64bit()) 3052 return std::make_pair(0U, &Mips::GPR64RegClass); 3053 // This will generate an error message 3054 return std::make_pair(0U, nullptr); 3055 case 'f': // FPU or MSA register 3056 if (VT == MVT::v16i8) 3057 return std::make_pair(0U, &Mips::MSA128BRegClass); 3058 else if (VT == MVT::v8i16 || VT == MVT::v8f16) 3059 return std::make_pair(0U, &Mips::MSA128HRegClass); 3060 else if (VT == MVT::v4i32 || VT == MVT::v4f32) 3061 return std::make_pair(0U, &Mips::MSA128WRegClass); 3062 else if (VT == MVT::v2i64 || VT == MVT::v2f64) 3063 return std::make_pair(0U, &Mips::MSA128DRegClass); 3064 else if (VT == MVT::f32) 3065 return std::make_pair(0U, &Mips::FGR32RegClass); 3066 else if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) { 3067 if (Subtarget->isFP64bit()) 3068 return std::make_pair(0U, &Mips::FGR64RegClass); 3069 return std::make_pair(0U, &Mips::AFGR64RegClass); 3070 } 3071 break; 3072 case 'c': // register suitable for indirect jump 3073 if (VT == MVT::i32) 3074 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass); 3075 assert(VT == MVT::i64 && "Unexpected type."); 3076 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass); 3077 case 'l': // register suitable for indirect jump 3078 if (VT == MVT::i32) 3079 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass); 3080 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass); 3081 case 'x': // register suitable for indirect jump 3082 // Fixme: Not triggering the use of both hi and low 3083 // This will generate an error message 3084 return std::make_pair(0U, nullptr); 3085 } 3086 } 3087 3088 std::pair<unsigned, const TargetRegisterClass *> R; 3089 R = parseRegForInlineAsmConstraint(Constraint, VT); 3090 3091 if (R.second) 3092 return R; 3093 3094 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); 3095 } 3096 3097 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 3098 /// vector. If it is invalid, don't add anything to Ops. 3099 void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 3100 std::string &Constraint, 3101 std::vector<SDValue>&Ops, 3102 SelectionDAG &DAG) const { 3103 SDValue Result; 3104 3105 // Only support length 1 constraints for now. 3106 if (Constraint.length() > 1) return; 3107 3108 char ConstraintLetter = Constraint[0]; 3109 switch (ConstraintLetter) { 3110 default: break; // This will fall through to the generic implementation 3111 case 'I': // Signed 16 bit constant 3112 // If this fails, the parent routine will give an error 3113 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 3114 EVT Type = Op.getValueType(); 3115 int64_t Val = C->getSExtValue(); 3116 if (isInt<16>(Val)) { 3117 Result = DAG.getTargetConstant(Val, Type); 3118 break; 3119 } 3120 } 3121 return; 3122 case 'J': // integer zero 3123 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 3124 EVT Type = Op.getValueType(); 3125 int64_t Val = C->getZExtValue(); 3126 if (Val == 0) { 3127 Result = DAG.getTargetConstant(0, Type); 3128 break; 3129 } 3130 } 3131 return; 3132 case 'K': // unsigned 16 bit immediate 3133 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 3134 EVT Type = Op.getValueType(); 3135 uint64_t Val = (uint64_t)C->getZExtValue(); 3136 if (isUInt<16>(Val)) { 3137 Result = DAG.getTargetConstant(Val, Type); 3138 break; 3139 } 3140 } 3141 return; 3142 case 'L': // signed 32 bit immediate where lower 16 bits are 0 3143 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 3144 EVT Type = Op.getValueType(); 3145 int64_t Val = C->getSExtValue(); 3146 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){ 3147 Result = DAG.getTargetConstant(Val, Type); 3148 break; 3149 } 3150 } 3151 return; 3152 case 'N': // immediate in the range of -65535 to -1 (inclusive) 3153 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 3154 EVT Type = Op.getValueType(); 3155 int64_t Val = C->getSExtValue(); 3156 if ((Val >= -65535) && (Val <= -1)) { 3157 Result = DAG.getTargetConstant(Val, Type); 3158 break; 3159 } 3160 } 3161 return; 3162 case 'O': // signed 15 bit immediate 3163 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 3164 EVT Type = Op.getValueType(); 3165 int64_t Val = C->getSExtValue(); 3166 if ((isInt<15>(Val))) { 3167 Result = DAG.getTargetConstant(Val, Type); 3168 break; 3169 } 3170 } 3171 return; 3172 case 'P': // immediate in the range of 1 to 65535 (inclusive) 3173 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 3174 EVT Type = Op.getValueType(); 3175 int64_t Val = C->getSExtValue(); 3176 if ((Val <= 65535) && (Val >= 1)) { 3177 Result = DAG.getTargetConstant(Val, Type); 3178 break; 3179 } 3180 } 3181 return; 3182 } 3183 3184 if (Result.getNode()) { 3185 Ops.push_back(Result); 3186 return; 3187 } 3188 3189 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 3190 } 3191 3192 bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, 3193 Type *Ty) const { 3194 // No global is ever allowed as a base. 3195 if (AM.BaseGV) 3196 return false; 3197 3198 switch (AM.Scale) { 3199 case 0: // "r+i" or just "i", depending on HasBaseReg. 3200 break; 3201 case 1: 3202 if (!AM.HasBaseReg) // allow "r+i". 3203 break; 3204 return false; // disallow "r+r" or "r+r+i". 3205 default: 3206 return false; 3207 } 3208 3209 return true; 3210 } 3211 3212 bool 3213 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 3214 // The Mips target isn't yet aware of offsets. 3215 return false; 3216 } 3217 3218 EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign, 3219 unsigned SrcAlign, 3220 bool IsMemset, bool ZeroMemset, 3221 bool MemcpyStrSrc, 3222 MachineFunction &MF) const { 3223 if (Subtarget->hasMips64()) 3224 return MVT::i64; 3225 3226 return MVT::i32; 3227 } 3228 3229 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 3230 if (VT != MVT::f32 && VT != MVT::f64) 3231 return false; 3232 if (Imm.isNegZero()) 3233 return false; 3234 return Imm.isZero(); 3235 } 3236 3237 unsigned MipsTargetLowering::getJumpTableEncoding() const { 3238 if (isN64()) 3239 return MachineJumpTableInfo::EK_GPRel64BlockAddress; 3240 3241 return TargetLowering::getJumpTableEncoding(); 3242 } 3243 3244 /// This function returns true if CallSym is a long double emulation routine. 3245 static bool isF128SoftLibCall(const char *CallSym) { 3246 const char *const LibCalls[] = 3247 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2", 3248 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi", 3249 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf", 3250 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2", 3251 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3", 3252 "__trunctfdf2", "__trunctfsf2", "__unordtf2", 3253 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl", 3254 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl", 3255 "truncl"}; 3256 3257 const char *const *End = LibCalls + array_lengthof(LibCalls); 3258 3259 // Check that LibCalls is sorted alphabetically. 3260 MipsTargetLowering::LTStr Comp; 3261 3262 #ifndef NDEBUG 3263 for (const char *const *I = LibCalls; I < End - 1; ++I) 3264 assert(Comp(*I, *(I + 1))); 3265 #endif 3266 3267 return std::binary_search(LibCalls, End, CallSym, Comp); 3268 } 3269 3270 /// This function returns true if Ty is fp128 or i128 which was originally a 3271 /// fp128. 3272 static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) { 3273 if (Ty->isFP128Ty()) 3274 return true; 3275 3276 const ExternalSymbolSDNode *ES = 3277 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode); 3278 3279 // If the Ty is i128 and the function being called is a long double emulation 3280 // routine, then the original type is f128. 3281 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol())); 3282 } 3283 3284 MipsTargetLowering::MipsCC::SpecialCallingConvType 3285 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const { 3286 MipsCC::SpecialCallingConvType SpecialCallingConv = 3287 MipsCC::NoSpecialCallingConv; 3288 if (Subtarget->inMips16HardFloat()) { 3289 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 3290 llvm::StringRef Sym = G->getGlobal()->getName(); 3291 Function *F = G->getGlobal()->getParent()->getFunction(Sym); 3292 if (F && F->hasFnAttribute("__Mips16RetHelper")) { 3293 SpecialCallingConv = MipsCC::Mips16RetHelperConv; 3294 } 3295 } 3296 } 3297 return SpecialCallingConv; 3298 } 3299 3300 MipsTargetLowering::MipsCC::MipsCC( 3301 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info, 3302 MipsCC::SpecialCallingConvType SpecialCallingConv_) 3303 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_), 3304 SpecialCallingConv(SpecialCallingConv_){ 3305 // Pre-allocate reserved argument area. 3306 CCInfo.AllocateStack(reservedArgArea(), 1); 3307 } 3308 3309 3310 void MipsTargetLowering::MipsCC:: 3311 analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args, 3312 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode, 3313 std::vector<ArgListEntry> &FuncArgs) { 3314 assert((CallConv != CallingConv::Fast || !IsVarArg) && 3315 "CallingConv::Fast shouldn't be used for vararg functions."); 3316 3317 unsigned NumOpnds = Args.size(); 3318 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn(); 3319 3320 for (unsigned I = 0; I != NumOpnds; ++I) { 3321 MVT ArgVT = Args[I].VT; 3322 ISD::ArgFlagsTy ArgFlags = Args[I].Flags; 3323 bool R; 3324 3325 if (ArgFlags.isByVal()) { 3326 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags); 3327 continue; 3328 } 3329 3330 if (IsVarArg && !Args[I].IsFixed) 3331 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo); 3332 else { 3333 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode, 3334 IsSoftFloat); 3335 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo); 3336 } 3337 3338 if (R) { 3339 #ifndef NDEBUG 3340 dbgs() << "Call operand #" << I << " has unhandled type " 3341 << EVT(ArgVT).getEVTString(); 3342 #endif 3343 llvm_unreachable(nullptr); 3344 } 3345 } 3346 } 3347 3348 void MipsTargetLowering::MipsCC:: 3349 analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args, 3350 bool IsSoftFloat, Function::const_arg_iterator FuncArg) { 3351 unsigned NumArgs = Args.size(); 3352 llvm::CCAssignFn *FixedFn = fixedArgFn(); 3353 unsigned CurArgIdx = 0; 3354 3355 for (unsigned I = 0; I != NumArgs; ++I) { 3356 MVT ArgVT = Args[I].VT; 3357 ISD::ArgFlagsTy ArgFlags = Args[I].Flags; 3358 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx); 3359 CurArgIdx = Args[I].OrigArgIndex; 3360 3361 if (ArgFlags.isByVal()) { 3362 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags); 3363 continue; 3364 } 3365 3366 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), nullptr, IsSoftFloat); 3367 3368 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo)) 3369 continue; 3370 3371 #ifndef NDEBUG 3372 dbgs() << "Formal Arg #" << I << " has unhandled type " 3373 << EVT(ArgVT).getEVTString(); 3374 #endif 3375 llvm_unreachable(nullptr); 3376 } 3377 } 3378 3379 template<typename Ty> 3380 void MipsTargetLowering::MipsCC:: 3381 analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat, 3382 const SDNode *CallNode, const Type *RetTy) const { 3383 CCAssignFn *Fn; 3384 3385 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode)) 3386 Fn = RetCC_F128Soft; 3387 else 3388 Fn = RetCC_Mips; 3389 3390 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) { 3391 MVT VT = RetVals[I].VT; 3392 ISD::ArgFlagsTy Flags = RetVals[I].Flags; 3393 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat); 3394 3395 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) { 3396 #ifndef NDEBUG 3397 dbgs() << "Call result #" << I << " has unhandled type " 3398 << EVT(VT).getEVTString() << '\n'; 3399 #endif 3400 llvm_unreachable(nullptr); 3401 } 3402 } 3403 } 3404 3405 void MipsTargetLowering::MipsCC:: 3406 analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat, 3407 const SDNode *CallNode, const Type *RetTy) const { 3408 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy); 3409 } 3410 3411 void MipsTargetLowering::MipsCC:: 3412 analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat, 3413 const Type *RetTy) const { 3414 analyzeReturn(Outs, IsSoftFloat, nullptr, RetTy); 3415 } 3416 3417 void MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT, 3418 MVT LocVT, 3419 CCValAssign::LocInfo LocInfo, 3420 ISD::ArgFlagsTy ArgFlags) { 3421 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0."); 3422 3423 struct ByValArgInfo ByVal; 3424 unsigned RegSize = regSize(); 3425 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize); 3426 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize), 3427 RegSize * 2); 3428 3429 if (useRegsForByval()) 3430 allocateRegs(ByVal, ByValSize, Align); 3431 3432 // Allocate space on caller's stack. 3433 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs, 3434 Align); 3435 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT, 3436 LocInfo)); 3437 ByValArgs.push_back(ByVal); 3438 } 3439 3440 unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const { 3441 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs); 3442 } 3443 3444 unsigned MipsTargetLowering::MipsCC::reservedArgArea() const { 3445 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0; 3446 } 3447 3448 const MCPhysReg *MipsTargetLowering::MipsCC::intArgRegs() const { 3449 return IsO32 ? O32IntRegs : Mips64IntRegs; 3450 } 3451 3452 llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const { 3453 if (CallConv == CallingConv::Fast) 3454 return CC_Mips_FastCC; 3455 3456 if (SpecialCallingConv == Mips16RetHelperConv) 3457 return CC_Mips16RetHelper; 3458 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN; 3459 } 3460 3461 llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const { 3462 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg; 3463 } 3464 3465 const MCPhysReg *MipsTargetLowering::MipsCC::shadowRegs() const { 3466 return IsO32 ? O32IntRegs : Mips64DPRegs; 3467 } 3468 3469 void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal, 3470 unsigned ByValSize, 3471 unsigned Align) { 3472 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs(); 3473 const MCPhysReg *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs(); 3474 assert(!(ByValSize % RegSize) && !(Align % RegSize) && 3475 "Byval argument's size and alignment should be a multiple of" 3476 "RegSize."); 3477 3478 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs); 3479 3480 // If Align > RegSize, the first arg register must be even. 3481 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) { 3482 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]); 3483 ++ByVal.FirstIdx; 3484 } 3485 3486 // Mark the registers allocated. 3487 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs); 3488 ByValSize -= RegSize, ++I, ++ByVal.NumRegs) 3489 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]); 3490 } 3491 3492 MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy, 3493 const SDNode *CallNode, 3494 bool IsSoftFloat) const { 3495 if (IsSoftFloat || IsO32) 3496 return VT; 3497 3498 // Check if the original type was fp128. 3499 if (originalTypeIsF128(OrigTy, CallNode)) { 3500 assert(VT == MVT::i64); 3501 return MVT::f64; 3502 } 3503 3504 return VT; 3505 } 3506 3507 void MipsTargetLowering:: 3508 copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains, 3509 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags, 3510 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg, 3511 const MipsCC &CC, const ByValArgInfo &ByVal) const { 3512 MachineFunction &MF = DAG.getMachineFunction(); 3513 MachineFrameInfo *MFI = MF.getFrameInfo(); 3514 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize(); 3515 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize); 3516 int FrameObjOffset; 3517 3518 if (RegAreaSize) 3519 FrameObjOffset = (int)CC.reservedArgArea() - 3520 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize()); 3521 else 3522 FrameObjOffset = ByVal.Address; 3523 3524 // Create frame object. 3525 EVT PtrTy = getPointerTy(); 3526 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true); 3527 SDValue FIN = DAG.getFrameIndex(FI, PtrTy); 3528 InVals.push_back(FIN); 3529 3530 if (!ByVal.NumRegs) 3531 return; 3532 3533 // Copy arg registers. 3534 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8); 3535 const TargetRegisterClass *RC = getRegClassFor(RegTy); 3536 3537 for (unsigned I = 0; I < ByVal.NumRegs; ++I) { 3538 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I]; 3539 unsigned VReg = addLiveIn(MF, ArgReg, RC); 3540 unsigned Offset = I * CC.regSize(); 3541 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN, 3542 DAG.getConstant(Offset, PtrTy)); 3543 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy), 3544 StorePtr, MachinePointerInfo(FuncArg, Offset), 3545 false, false, 0); 3546 OutChains.push_back(Store); 3547 } 3548 } 3549 3550 // Copy byVal arg to registers and stack. 3551 void MipsTargetLowering:: 3552 passByValArg(SDValue Chain, SDLoc DL, 3553 std::deque< std::pair<unsigned, SDValue> > &RegsToPass, 3554 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr, 3555 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, 3556 const MipsCC &CC, const ByValArgInfo &ByVal, 3557 const ISD::ArgFlagsTy &Flags, bool isLittle) const { 3558 unsigned ByValSizeInBytes = Flags.getByValSize(); 3559 unsigned OffsetInBytes = 0; // From beginning of struct 3560 unsigned RegSizeInBytes = CC.regSize(); 3561 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes); 3562 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSizeInBytes * 8); 3563 3564 if (ByVal.NumRegs) { 3565 const MCPhysReg *ArgRegs = CC.intArgRegs(); 3566 bool LeftoverBytes = (ByVal.NumRegs * RegSizeInBytes > ByValSizeInBytes); 3567 unsigned I = 0; 3568 3569 // Copy words to registers. 3570 for (; I < ByVal.NumRegs - LeftoverBytes; 3571 ++I, OffsetInBytes += RegSizeInBytes) { 3572 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg, 3573 DAG.getConstant(OffsetInBytes, PtrTy)); 3574 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr, 3575 MachinePointerInfo(), false, false, false, 3576 Alignment); 3577 MemOpChains.push_back(LoadVal.getValue(1)); 3578 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I]; 3579 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal)); 3580 } 3581 3582 // Return if the struct has been fully copied. 3583 if (ByValSizeInBytes == OffsetInBytes) 3584 return; 3585 3586 // Copy the remainder of the byval argument with sub-word loads and shifts. 3587 if (LeftoverBytes) { 3588 assert((ByValSizeInBytes > OffsetInBytes) && 3589 (ByValSizeInBytes < OffsetInBytes + RegSizeInBytes) && 3590 "Size of the remainder should be smaller than RegSizeInBytes."); 3591 SDValue Val; 3592 3593 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0; 3594 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) { 3595 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes; 3596 3597 if (RemainingSizeInBytes < LoadSizeInBytes) 3598 continue; 3599 3600 // Load subword. 3601 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg, 3602 DAG.getConstant(OffsetInBytes, PtrTy)); 3603 SDValue LoadVal = DAG.getExtLoad( 3604 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(), 3605 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, Alignment); 3606 MemOpChains.push_back(LoadVal.getValue(1)); 3607 3608 // Shift the loaded value. 3609 unsigned Shamt; 3610 3611 if (isLittle) 3612 Shamt = TotalBytesLoaded * 8; 3613 else 3614 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8; 3615 3616 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal, 3617 DAG.getConstant(Shamt, MVT::i32)); 3618 3619 if (Val.getNode()) 3620 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift); 3621 else 3622 Val = Shift; 3623 3624 OffsetInBytes += LoadSizeInBytes; 3625 TotalBytesLoaded += LoadSizeInBytes; 3626 Alignment = std::min(Alignment, LoadSizeInBytes); 3627 } 3628 3629 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I]; 3630 RegsToPass.push_back(std::make_pair(ArgReg, Val)); 3631 return; 3632 } 3633 } 3634 3635 // Copy remainder of byval arg to it with memcpy. 3636 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes; 3637 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg, 3638 DAG.getConstant(OffsetInBytes, PtrTy)); 3639 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr, 3640 DAG.getIntPtrConstant(ByVal.Address)); 3641 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy), 3642 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false, 3643 MachinePointerInfo(), MachinePointerInfo()); 3644 MemOpChains.push_back(Chain); 3645 } 3646 3647 void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains, 3648 const MipsCC &CC, SDValue Chain, 3649 SDLoc DL, SelectionDAG &DAG) const { 3650 unsigned NumRegs = CC.numIntArgRegs(); 3651 const MCPhysReg *ArgRegs = CC.intArgRegs(); 3652 const CCState &CCInfo = CC.getCCInfo(); 3653 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs); 3654 unsigned RegSize = CC.regSize(); 3655 MVT RegTy = MVT::getIntegerVT(RegSize * 8); 3656 const TargetRegisterClass *RC = getRegClassFor(RegTy); 3657 MachineFunction &MF = DAG.getMachineFunction(); 3658 MachineFrameInfo *MFI = MF.getFrameInfo(); 3659 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 3660 3661 // Offset of the first variable argument from stack pointer. 3662 int VaArgOffset; 3663 3664 if (NumRegs == Idx) 3665 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize); 3666 else 3667 VaArgOffset = (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx)); 3668 3669 // Record the frame index of the first variable argument 3670 // which is a value necessary to VASTART. 3671 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true); 3672 MipsFI->setVarArgsFrameIndex(FI); 3673 3674 // Copy the integer registers that have not been used for argument passing 3675 // to the argument register save area. For O32, the save area is allocated 3676 // in the caller's stack frame, while for N32/64, it is allocated in the 3677 // callee's stack frame. 3678 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) { 3679 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC); 3680 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy); 3681 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true); 3682 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy()); 3683 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff, 3684 MachinePointerInfo(), false, false, 0); 3685 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue((Value*)nullptr); 3686 OutChains.push_back(Store); 3687 } 3688 } 3689