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