1 //===-- PPCISelLowering.cpp - PPC 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 implements the PPCISelLowering class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "PPCISelLowering.h" 15 #include "MCTargetDesc/PPCPredicates.h" 16 #include "PPCCallingConv.h" 17 #include "PPCMachineFunctionInfo.h" 18 #include "PPCPerfectShuffle.h" 19 #include "PPCTargetMachine.h" 20 #include "PPCTargetObjectFile.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/ADT/StringSwitch.h" 23 #include "llvm/ADT/Triple.h" 24 #include "llvm/CodeGen/CallingConvLower.h" 25 #include "llvm/CodeGen/MachineFrameInfo.h" 26 #include "llvm/CodeGen/MachineFunction.h" 27 #include "llvm/CodeGen/MachineInstrBuilder.h" 28 #include "llvm/CodeGen/MachineLoopInfo.h" 29 #include "llvm/CodeGen/MachineRegisterInfo.h" 30 #include "llvm/CodeGen/SelectionDAG.h" 31 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 32 #include "llvm/IR/CallingConv.h" 33 #include "llvm/IR/Constants.h" 34 #include "llvm/IR/DerivedTypes.h" 35 #include "llvm/IR/Function.h" 36 #include "llvm/IR/Intrinsics.h" 37 #include "llvm/Support/CommandLine.h" 38 #include "llvm/Support/ErrorHandling.h" 39 #include "llvm/Support/MathExtras.h" 40 #include "llvm/Support/raw_ostream.h" 41 #include "llvm/Target/TargetOptions.h" 42 43 using namespace llvm; 44 45 // FIXME: Remove this once soft-float is supported. 46 static cl::opt<bool> DisablePPCFloatInVariadic("disable-ppc-float-in-variadic", 47 cl::desc("disable saving float registers for va_start on PPC"), cl::Hidden); 48 49 static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc", 50 cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden); 51 52 static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref", 53 cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden); 54 55 static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned", 56 cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden); 57 58 // FIXME: Remove this once the bug has been fixed! 59 extern cl::opt<bool> ANDIGlueBug; 60 61 PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM, 62 const PPCSubtarget &STI) 63 : TargetLowering(TM), Subtarget(STI) { 64 // Use _setjmp/_longjmp instead of setjmp/longjmp. 65 setUseUnderscoreSetJmp(true); 66 setUseUnderscoreLongJmp(true); 67 68 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all 69 // arguments are at least 4/8 bytes aligned. 70 bool isPPC64 = Subtarget.isPPC64(); 71 setMinStackArgumentAlignment(isPPC64 ? 8:4); 72 73 // Set up the register classes. 74 addRegisterClass(MVT::i32, &PPC::GPRCRegClass); 75 addRegisterClass(MVT::f32, &PPC::F4RCRegClass); 76 addRegisterClass(MVT::f64, &PPC::F8RCRegClass); 77 78 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD 79 for (MVT VT : MVT::integer_valuetypes()) { 80 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 81 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand); 82 } 83 84 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 85 86 // PowerPC has pre-inc load and store's. 87 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal); 88 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal); 89 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal); 90 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal); 91 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal); 92 setIndexedLoadAction(ISD::PRE_INC, MVT::f32, Legal); 93 setIndexedLoadAction(ISD::PRE_INC, MVT::f64, Legal); 94 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal); 95 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal); 96 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal); 97 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal); 98 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal); 99 setIndexedStoreAction(ISD::PRE_INC, MVT::f32, Legal); 100 setIndexedStoreAction(ISD::PRE_INC, MVT::f64, Legal); 101 102 if (Subtarget.useCRBits()) { 103 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 104 105 if (isPPC64 || Subtarget.hasFPCVT()) { 106 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote); 107 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1, 108 isPPC64 ? MVT::i64 : MVT::i32); 109 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote); 110 AddPromotedToType (ISD::UINT_TO_FP, MVT::i1, 111 isPPC64 ? MVT::i64 : MVT::i32); 112 } else { 113 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom); 114 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom); 115 } 116 117 // PowerPC does not support direct load / store of condition registers 118 setOperationAction(ISD::LOAD, MVT::i1, Custom); 119 setOperationAction(ISD::STORE, MVT::i1, Custom); 120 121 // FIXME: Remove this once the ANDI glue bug is fixed: 122 if (ANDIGlueBug) 123 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom); 124 125 for (MVT VT : MVT::integer_valuetypes()) { 126 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 127 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); 128 setTruncStoreAction(VT, MVT::i1, Expand); 129 } 130 131 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass); 132 } 133 134 // This is used in the ppcf128->int sequence. Note it has different semantics 135 // from FP_ROUND: that rounds to nearest, this rounds to zero. 136 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom); 137 138 // We do not currently implement these libm ops for PowerPC. 139 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand); 140 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand); 141 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand); 142 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand); 143 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand); 144 setOperationAction(ISD::FREM, MVT::ppcf128, Expand); 145 146 // PowerPC has no SREM/UREM instructions 147 setOperationAction(ISD::SREM, MVT::i32, Expand); 148 setOperationAction(ISD::UREM, MVT::i32, Expand); 149 setOperationAction(ISD::SREM, MVT::i64, Expand); 150 setOperationAction(ISD::UREM, MVT::i64, Expand); 151 152 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM. 153 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 154 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 155 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand); 156 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand); 157 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 158 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 159 setOperationAction(ISD::UDIVREM, MVT::i64, Expand); 160 setOperationAction(ISD::SDIVREM, MVT::i64, Expand); 161 162 // We don't support sin/cos/sqrt/fmod/pow 163 setOperationAction(ISD::FSIN , MVT::f64, Expand); 164 setOperationAction(ISD::FCOS , MVT::f64, Expand); 165 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 166 setOperationAction(ISD::FREM , MVT::f64, Expand); 167 setOperationAction(ISD::FPOW , MVT::f64, Expand); 168 setOperationAction(ISD::FMA , MVT::f64, Legal); 169 setOperationAction(ISD::FSIN , MVT::f32, Expand); 170 setOperationAction(ISD::FCOS , MVT::f32, Expand); 171 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 172 setOperationAction(ISD::FREM , MVT::f32, Expand); 173 setOperationAction(ISD::FPOW , MVT::f32, Expand); 174 setOperationAction(ISD::FMA , MVT::f32, Legal); 175 176 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); 177 178 // If we're enabling GP optimizations, use hardware square root 179 if (!Subtarget.hasFSQRT() && 180 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTE() && 181 Subtarget.hasFRE())) 182 setOperationAction(ISD::FSQRT, MVT::f64, Expand); 183 184 if (!Subtarget.hasFSQRT() && 185 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTES() && 186 Subtarget.hasFRES())) 187 setOperationAction(ISD::FSQRT, MVT::f32, Expand); 188 189 if (Subtarget.hasFCPSGN()) { 190 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal); 191 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal); 192 } else { 193 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 194 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); 195 } 196 197 if (Subtarget.hasFPRND()) { 198 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 199 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 200 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 201 setOperationAction(ISD::FROUND, MVT::f64, Legal); 202 203 setOperationAction(ISD::FFLOOR, MVT::f32, Legal); 204 setOperationAction(ISD::FCEIL, MVT::f32, Legal); 205 setOperationAction(ISD::FTRUNC, MVT::f32, Legal); 206 setOperationAction(ISD::FROUND, MVT::f32, Legal); 207 } 208 209 // PowerPC does not have BSWAP, CTPOP or CTTZ 210 setOperationAction(ISD::BSWAP, MVT::i32 , Expand); 211 setOperationAction(ISD::CTTZ , MVT::i32 , Expand); 212 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand); 213 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand); 214 setOperationAction(ISD::BSWAP, MVT::i64 , Expand); 215 setOperationAction(ISD::CTTZ , MVT::i64 , Expand); 216 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand); 217 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand); 218 219 if (Subtarget.hasPOPCNTD()) { 220 setOperationAction(ISD::CTPOP, MVT::i32 , Legal); 221 setOperationAction(ISD::CTPOP, MVT::i64 , Legal); 222 } else { 223 setOperationAction(ISD::CTPOP, MVT::i32 , Expand); 224 setOperationAction(ISD::CTPOP, MVT::i64 , Expand); 225 } 226 227 // PowerPC does not have ROTR 228 setOperationAction(ISD::ROTR, MVT::i32 , Expand); 229 setOperationAction(ISD::ROTR, MVT::i64 , Expand); 230 231 if (!Subtarget.useCRBits()) { 232 // PowerPC does not have Select 233 setOperationAction(ISD::SELECT, MVT::i32, Expand); 234 setOperationAction(ISD::SELECT, MVT::i64, Expand); 235 setOperationAction(ISD::SELECT, MVT::f32, Expand); 236 setOperationAction(ISD::SELECT, MVT::f64, Expand); 237 } 238 239 // PowerPC wants to turn select_cc of FP into fsel when possible. 240 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 241 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 242 243 // PowerPC wants to optimize integer setcc a bit 244 if (!Subtarget.useCRBits()) 245 setOperationAction(ISD::SETCC, MVT::i32, Custom); 246 247 // PowerPC does not have BRCOND which requires SetCC 248 if (!Subtarget.useCRBits()) 249 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 250 251 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 252 253 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores. 254 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 255 256 // PowerPC does not have [U|S]INT_TO_FP 257 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand); 258 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); 259 260 setOperationAction(ISD::BITCAST, MVT::f32, Expand); 261 setOperationAction(ISD::BITCAST, MVT::i32, Expand); 262 setOperationAction(ISD::BITCAST, MVT::i64, Expand); 263 setOperationAction(ISD::BITCAST, MVT::f64, Expand); 264 265 // We cannot sextinreg(i1). Expand to shifts. 266 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 267 268 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support 269 // SjLj exception handling but a light-weight setjmp/longjmp replacement to 270 // support continuation, user-level threading, and etc.. As a result, no 271 // other SjLj exception interfaces are implemented and please don't build 272 // your own exception handling based on them. 273 // LLVM/Clang supports zero-cost DWARF exception handling. 274 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); 275 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); 276 277 // We want to legalize GlobalAddress and ConstantPool nodes into the 278 // appropriate instructions to materialize the address. 279 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 280 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 281 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 282 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 283 setOperationAction(ISD::JumpTable, MVT::i32, Custom); 284 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); 285 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); 286 setOperationAction(ISD::BlockAddress, MVT::i64, Custom); 287 setOperationAction(ISD::ConstantPool, MVT::i64, Custom); 288 setOperationAction(ISD::JumpTable, MVT::i64, Custom); 289 290 // TRAP is legal. 291 setOperationAction(ISD::TRAP, MVT::Other, Legal); 292 293 // TRAMPOLINE is custom lowered. 294 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom); 295 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom); 296 297 // VASTART needs to be custom lowered to use the VarArgsFrameIndex 298 setOperationAction(ISD::VASTART , MVT::Other, Custom); 299 300 if (Subtarget.isSVR4ABI()) { 301 if (isPPC64) { 302 // VAARG always uses double-word chunks, so promote anything smaller. 303 setOperationAction(ISD::VAARG, MVT::i1, Promote); 304 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64); 305 setOperationAction(ISD::VAARG, MVT::i8, Promote); 306 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64); 307 setOperationAction(ISD::VAARG, MVT::i16, Promote); 308 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64); 309 setOperationAction(ISD::VAARG, MVT::i32, Promote); 310 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64); 311 setOperationAction(ISD::VAARG, MVT::Other, Expand); 312 } else { 313 // VAARG is custom lowered with the 32-bit SVR4 ABI. 314 setOperationAction(ISD::VAARG, MVT::Other, Custom); 315 setOperationAction(ISD::VAARG, MVT::i64, Custom); 316 } 317 } else 318 setOperationAction(ISD::VAARG, MVT::Other, Expand); 319 320 if (Subtarget.isSVR4ABI() && !isPPC64) 321 // VACOPY is custom lowered with the 32-bit SVR4 ABI. 322 setOperationAction(ISD::VACOPY , MVT::Other, Custom); 323 else 324 setOperationAction(ISD::VACOPY , MVT::Other, Expand); 325 326 // Use the default implementation. 327 setOperationAction(ISD::VAEND , MVT::Other, Expand); 328 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand); 329 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom); 330 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom); 331 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom); 332 333 // We want to custom lower some of our intrinsics. 334 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 335 336 // To handle counter-based loop conditions. 337 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom); 338 339 // Comparisons that require checking two conditions. 340 setCondCodeAction(ISD::SETULT, MVT::f32, Expand); 341 setCondCodeAction(ISD::SETULT, MVT::f64, Expand); 342 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand); 343 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand); 344 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand); 345 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand); 346 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand); 347 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand); 348 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand); 349 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand); 350 setCondCodeAction(ISD::SETONE, MVT::f32, Expand); 351 setCondCodeAction(ISD::SETONE, MVT::f64, Expand); 352 353 if (Subtarget.has64BitSupport()) { 354 // They also have instructions for converting between i64 and fp. 355 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 356 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand); 357 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); 358 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); 359 // This is just the low 32 bits of a (signed) fp->i64 conversion. 360 // We cannot do this with Promote because i64 is not a legal type. 361 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 362 363 if (Subtarget.hasLFIWAX() || Subtarget.isPPC64()) 364 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 365 } else { 366 // PowerPC does not have FP_TO_UINT on 32-bit implementations. 367 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); 368 } 369 370 // With the instructions enabled under FPCVT, we can do everything. 371 if (Subtarget.hasFPCVT()) { 372 if (Subtarget.has64BitSupport()) { 373 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 374 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom); 375 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); 376 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom); 377 } 378 379 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 380 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 381 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 382 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 383 } 384 385 if (Subtarget.use64BitRegs()) { 386 // 64-bit PowerPC implementations can support i64 types directly 387 addRegisterClass(MVT::i64, &PPC::G8RCRegClass); 388 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or 389 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand); 390 // 64-bit PowerPC wants to expand i128 shifts itself. 391 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom); 392 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom); 393 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom); 394 } else { 395 // 32-bit PowerPC wants to expand i64 shifts itself. 396 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 397 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 398 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 399 } 400 401 if (Subtarget.hasAltivec()) { 402 // First set operation action for all vector types to expand. Then we 403 // will selectively turn on ones that can be effectively codegen'd. 404 for (MVT VT : MVT::vector_valuetypes()) { 405 // add/sub are legal for all supported vector VT's. 406 setOperationAction(ISD::ADD , VT, Legal); 407 setOperationAction(ISD::SUB , VT, Legal); 408 409 // Vector instructions introduced in P8 410 if (Subtarget.hasP8Altivec() && (VT.SimpleTy != MVT::v1i128)) { 411 setOperationAction(ISD::CTPOP, VT, Legal); 412 setOperationAction(ISD::CTLZ, VT, Legal); 413 } 414 else { 415 setOperationAction(ISD::CTPOP, VT, Expand); 416 setOperationAction(ISD::CTLZ, VT, Expand); 417 } 418 419 // We promote all shuffles to v16i8. 420 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote); 421 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8); 422 423 // We promote all non-typed operations to v4i32. 424 setOperationAction(ISD::AND , VT, Promote); 425 AddPromotedToType (ISD::AND , VT, MVT::v4i32); 426 setOperationAction(ISD::OR , VT, Promote); 427 AddPromotedToType (ISD::OR , VT, MVT::v4i32); 428 setOperationAction(ISD::XOR , VT, Promote); 429 AddPromotedToType (ISD::XOR , VT, MVT::v4i32); 430 setOperationAction(ISD::LOAD , VT, Promote); 431 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32); 432 setOperationAction(ISD::SELECT, VT, Promote); 433 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32); 434 setOperationAction(ISD::STORE, VT, Promote); 435 AddPromotedToType (ISD::STORE, VT, MVT::v4i32); 436 437 // No other operations are legal. 438 setOperationAction(ISD::MUL , VT, Expand); 439 setOperationAction(ISD::SDIV, VT, Expand); 440 setOperationAction(ISD::SREM, VT, Expand); 441 setOperationAction(ISD::UDIV, VT, Expand); 442 setOperationAction(ISD::UREM, VT, Expand); 443 setOperationAction(ISD::FDIV, VT, Expand); 444 setOperationAction(ISD::FREM, VT, Expand); 445 setOperationAction(ISD::FNEG, VT, Expand); 446 setOperationAction(ISD::FSQRT, VT, Expand); 447 setOperationAction(ISD::FLOG, VT, Expand); 448 setOperationAction(ISD::FLOG10, VT, Expand); 449 setOperationAction(ISD::FLOG2, VT, Expand); 450 setOperationAction(ISD::FEXP, VT, Expand); 451 setOperationAction(ISD::FEXP2, VT, Expand); 452 setOperationAction(ISD::FSIN, VT, Expand); 453 setOperationAction(ISD::FCOS, VT, Expand); 454 setOperationAction(ISD::FABS, VT, Expand); 455 setOperationAction(ISD::FPOWI, VT, Expand); 456 setOperationAction(ISD::FFLOOR, VT, Expand); 457 setOperationAction(ISD::FCEIL, VT, Expand); 458 setOperationAction(ISD::FTRUNC, VT, Expand); 459 setOperationAction(ISD::FRINT, VT, Expand); 460 setOperationAction(ISD::FNEARBYINT, VT, Expand); 461 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand); 462 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand); 463 setOperationAction(ISD::BUILD_VECTOR, VT, Expand); 464 setOperationAction(ISD::MULHU, VT, Expand); 465 setOperationAction(ISD::MULHS, VT, Expand); 466 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 467 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 468 setOperationAction(ISD::UDIVREM, VT, Expand); 469 setOperationAction(ISD::SDIVREM, VT, Expand); 470 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand); 471 setOperationAction(ISD::FPOW, VT, Expand); 472 setOperationAction(ISD::BSWAP, VT, Expand); 473 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand); 474 setOperationAction(ISD::CTTZ, VT, Expand); 475 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand); 476 setOperationAction(ISD::VSELECT, VT, Expand); 477 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); 478 479 for (MVT InnerVT : MVT::vector_valuetypes()) { 480 setTruncStoreAction(VT, InnerVT, Expand); 481 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand); 482 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand); 483 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand); 484 } 485 } 486 487 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle 488 // with merges, splats, etc. 489 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom); 490 491 setOperationAction(ISD::AND , MVT::v4i32, Legal); 492 setOperationAction(ISD::OR , MVT::v4i32, Legal); 493 setOperationAction(ISD::XOR , MVT::v4i32, Legal); 494 setOperationAction(ISD::LOAD , MVT::v4i32, Legal); 495 setOperationAction(ISD::SELECT, MVT::v4i32, 496 Subtarget.useCRBits() ? Legal : Expand); 497 setOperationAction(ISD::STORE , MVT::v4i32, Legal); 498 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal); 499 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal); 500 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal); 501 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal); 502 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal); 503 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal); 504 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal); 505 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal); 506 507 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass); 508 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass); 509 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass); 510 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass); 511 512 setOperationAction(ISD::MUL, MVT::v4f32, Legal); 513 setOperationAction(ISD::FMA, MVT::v4f32, Legal); 514 515 if (TM.Options.UnsafeFPMath || Subtarget.hasVSX()) { 516 setOperationAction(ISD::FDIV, MVT::v4f32, Legal); 517 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); 518 } 519 520 521 if (Subtarget.hasP8Altivec()) 522 setOperationAction(ISD::MUL, MVT::v4i32, Legal); 523 else 524 setOperationAction(ISD::MUL, MVT::v4i32, Custom); 525 526 setOperationAction(ISD::MUL, MVT::v8i16, Custom); 527 setOperationAction(ISD::MUL, MVT::v16i8, Custom); 528 529 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom); 530 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom); 531 532 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom); 533 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom); 534 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom); 535 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); 536 537 // Altivec does not contain unordered floating-point compare instructions 538 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand); 539 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand); 540 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand); 541 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand); 542 543 if (Subtarget.hasVSX()) { 544 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal); 545 if (Subtarget.hasP8Vector()) 546 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal); 547 if (Subtarget.hasDirectMove()) { 548 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Legal); 549 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Legal); 550 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Legal); 551 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Legal); 552 } 553 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal); 554 555 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal); 556 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal); 557 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal); 558 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal); 559 setOperationAction(ISD::FROUND, MVT::v2f64, Legal); 560 561 setOperationAction(ISD::FROUND, MVT::v4f32, Legal); 562 563 setOperationAction(ISD::MUL, MVT::v2f64, Legal); 564 setOperationAction(ISD::FMA, MVT::v2f64, Legal); 565 566 setOperationAction(ISD::FDIV, MVT::v2f64, Legal); 567 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); 568 569 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal); 570 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal); 571 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal); 572 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal); 573 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal); 574 575 // Share the Altivec comparison restrictions. 576 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand); 577 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand); 578 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand); 579 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand); 580 581 setOperationAction(ISD::LOAD, MVT::v2f64, Legal); 582 setOperationAction(ISD::STORE, MVT::v2f64, Legal); 583 584 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal); 585 586 if (Subtarget.hasP8Vector()) 587 addRegisterClass(MVT::f32, &PPC::VSSRCRegClass); 588 589 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass); 590 591 addRegisterClass(MVT::v4i32, &PPC::VSRCRegClass); 592 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass); 593 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass); 594 595 if (Subtarget.hasP8Altivec()) { 596 setOperationAction(ISD::SHL, MVT::v2i64, Legal); 597 setOperationAction(ISD::SRA, MVT::v2i64, Legal); 598 setOperationAction(ISD::SRL, MVT::v2i64, Legal); 599 600 setOperationAction(ISD::SETCC, MVT::v2i64, Legal); 601 } 602 else { 603 setOperationAction(ISD::SHL, MVT::v2i64, Expand); 604 setOperationAction(ISD::SRA, MVT::v2i64, Expand); 605 setOperationAction(ISD::SRL, MVT::v2i64, Expand); 606 607 setOperationAction(ISD::SETCC, MVT::v2i64, Custom); 608 609 // VSX v2i64 only supports non-arithmetic operations. 610 setOperationAction(ISD::ADD, MVT::v2i64, Expand); 611 setOperationAction(ISD::SUB, MVT::v2i64, Expand); 612 } 613 614 setOperationAction(ISD::LOAD, MVT::v2i64, Promote); 615 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64); 616 setOperationAction(ISD::STORE, MVT::v2i64, Promote); 617 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64); 618 619 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal); 620 621 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal); 622 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal); 623 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal); 624 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal); 625 626 // Vector operation legalization checks the result type of 627 // SIGN_EXTEND_INREG, overall legalization checks the inner type. 628 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal); 629 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal); 630 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom); 631 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom); 632 633 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass); 634 } 635 636 if (Subtarget.hasP8Altivec()) { 637 addRegisterClass(MVT::v2i64, &PPC::VRRCRegClass); 638 addRegisterClass(MVT::v1i128, &PPC::VRRCRegClass); 639 } 640 } 641 642 if (Subtarget.hasQPX()) { 643 setOperationAction(ISD::FADD, MVT::v4f64, Legal); 644 setOperationAction(ISD::FSUB, MVT::v4f64, Legal); 645 setOperationAction(ISD::FMUL, MVT::v4f64, Legal); 646 setOperationAction(ISD::FREM, MVT::v4f64, Expand); 647 648 setOperationAction(ISD::FCOPYSIGN, MVT::v4f64, Legal); 649 setOperationAction(ISD::FGETSIGN, MVT::v4f64, Expand); 650 651 setOperationAction(ISD::LOAD , MVT::v4f64, Custom); 652 setOperationAction(ISD::STORE , MVT::v4f64, Custom); 653 654 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Custom); 655 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Custom); 656 657 if (!Subtarget.useCRBits()) 658 setOperationAction(ISD::SELECT, MVT::v4f64, Expand); 659 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal); 660 661 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f64, Legal); 662 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f64, Expand); 663 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f64, Expand); 664 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f64, Expand); 665 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f64, Custom); 666 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f64, Legal); 667 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom); 668 669 setOperationAction(ISD::FP_TO_SINT , MVT::v4f64, Legal); 670 setOperationAction(ISD::FP_TO_UINT , MVT::v4f64, Expand); 671 672 setOperationAction(ISD::FP_ROUND , MVT::v4f32, Legal); 673 setOperationAction(ISD::FP_ROUND_INREG , MVT::v4f32, Expand); 674 setOperationAction(ISD::FP_EXTEND, MVT::v4f64, Legal); 675 676 setOperationAction(ISD::FNEG , MVT::v4f64, Legal); 677 setOperationAction(ISD::FABS , MVT::v4f64, Legal); 678 setOperationAction(ISD::FSIN , MVT::v4f64, Expand); 679 setOperationAction(ISD::FCOS , MVT::v4f64, Expand); 680 setOperationAction(ISD::FPOWI , MVT::v4f64, Expand); 681 setOperationAction(ISD::FPOW , MVT::v4f64, Expand); 682 setOperationAction(ISD::FLOG , MVT::v4f64, Expand); 683 setOperationAction(ISD::FLOG2 , MVT::v4f64, Expand); 684 setOperationAction(ISD::FLOG10 , MVT::v4f64, Expand); 685 setOperationAction(ISD::FEXP , MVT::v4f64, Expand); 686 setOperationAction(ISD::FEXP2 , MVT::v4f64, Expand); 687 688 setOperationAction(ISD::FMINNUM, MVT::v4f64, Legal); 689 setOperationAction(ISD::FMAXNUM, MVT::v4f64, Legal); 690 691 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f64, Legal); 692 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f64, Legal); 693 694 addRegisterClass(MVT::v4f64, &PPC::QFRCRegClass); 695 696 setOperationAction(ISD::FADD, MVT::v4f32, Legal); 697 setOperationAction(ISD::FSUB, MVT::v4f32, Legal); 698 setOperationAction(ISD::FMUL, MVT::v4f32, Legal); 699 setOperationAction(ISD::FREM, MVT::v4f32, Expand); 700 701 setOperationAction(ISD::FCOPYSIGN, MVT::v4f32, Legal); 702 setOperationAction(ISD::FGETSIGN, MVT::v4f32, Expand); 703 704 setOperationAction(ISD::LOAD , MVT::v4f32, Custom); 705 setOperationAction(ISD::STORE , MVT::v4f32, Custom); 706 707 if (!Subtarget.useCRBits()) 708 setOperationAction(ISD::SELECT, MVT::v4f32, Expand); 709 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal); 710 711 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f32, Legal); 712 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f32, Expand); 713 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f32, Expand); 714 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f32, Expand); 715 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f32, Custom); 716 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal); 717 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); 718 719 setOperationAction(ISD::FP_TO_SINT , MVT::v4f32, Legal); 720 setOperationAction(ISD::FP_TO_UINT , MVT::v4f32, Expand); 721 722 setOperationAction(ISD::FNEG , MVT::v4f32, Legal); 723 setOperationAction(ISD::FABS , MVT::v4f32, Legal); 724 setOperationAction(ISD::FSIN , MVT::v4f32, Expand); 725 setOperationAction(ISD::FCOS , MVT::v4f32, Expand); 726 setOperationAction(ISD::FPOWI , MVT::v4f32, Expand); 727 setOperationAction(ISD::FPOW , MVT::v4f32, Expand); 728 setOperationAction(ISD::FLOG , MVT::v4f32, Expand); 729 setOperationAction(ISD::FLOG2 , MVT::v4f32, Expand); 730 setOperationAction(ISD::FLOG10 , MVT::v4f32, Expand); 731 setOperationAction(ISD::FEXP , MVT::v4f32, Expand); 732 setOperationAction(ISD::FEXP2 , MVT::v4f32, Expand); 733 734 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal); 735 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal); 736 737 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f32, Legal); 738 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f32, Legal); 739 740 addRegisterClass(MVT::v4f32, &PPC::QSRCRegClass); 741 742 setOperationAction(ISD::AND , MVT::v4i1, Legal); 743 setOperationAction(ISD::OR , MVT::v4i1, Legal); 744 setOperationAction(ISD::XOR , MVT::v4i1, Legal); 745 746 if (!Subtarget.useCRBits()) 747 setOperationAction(ISD::SELECT, MVT::v4i1, Expand); 748 setOperationAction(ISD::VSELECT, MVT::v4i1, Legal); 749 750 setOperationAction(ISD::LOAD , MVT::v4i1, Custom); 751 setOperationAction(ISD::STORE , MVT::v4i1, Custom); 752 753 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4i1, Custom); 754 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4i1, Expand); 755 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4i1, Expand); 756 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4i1, Expand); 757 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4i1, Custom); 758 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i1, Expand); 759 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i1, Custom); 760 761 setOperationAction(ISD::SINT_TO_FP, MVT::v4i1, Custom); 762 setOperationAction(ISD::UINT_TO_FP, MVT::v4i1, Custom); 763 764 addRegisterClass(MVT::v4i1, &PPC::QBRCRegClass); 765 766 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal); 767 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal); 768 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal); 769 setOperationAction(ISD::FROUND, MVT::v4f64, Legal); 770 771 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal); 772 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal); 773 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal); 774 setOperationAction(ISD::FROUND, MVT::v4f32, Legal); 775 776 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Expand); 777 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand); 778 779 // These need to set FE_INEXACT, and so cannot be vectorized here. 780 setOperationAction(ISD::FRINT, MVT::v4f64, Expand); 781 setOperationAction(ISD::FRINT, MVT::v4f32, Expand); 782 783 if (TM.Options.UnsafeFPMath) { 784 setOperationAction(ISD::FDIV, MVT::v4f64, Legal); 785 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal); 786 787 setOperationAction(ISD::FDIV, MVT::v4f32, Legal); 788 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); 789 } else { 790 setOperationAction(ISD::FDIV, MVT::v4f64, Expand); 791 setOperationAction(ISD::FSQRT, MVT::v4f64, Expand); 792 793 setOperationAction(ISD::FDIV, MVT::v4f32, Expand); 794 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); 795 } 796 } 797 798 if (Subtarget.has64BitSupport()) 799 setOperationAction(ISD::PREFETCH, MVT::Other, Legal); 800 801 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, isPPC64 ? Legal : Custom); 802 803 if (!isPPC64) { 804 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand); 805 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand); 806 } 807 808 setBooleanContents(ZeroOrOneBooleanContent); 809 810 if (Subtarget.hasAltivec()) { 811 // Altivec instructions set fields to all zeros or all ones. 812 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 813 } 814 815 if (!isPPC64) { 816 // These libcalls are not available in 32-bit. 817 setLibcallName(RTLIB::SHL_I128, nullptr); 818 setLibcallName(RTLIB::SRL_I128, nullptr); 819 setLibcallName(RTLIB::SRA_I128, nullptr); 820 } 821 822 if (isPPC64) { 823 setStackPointerRegisterToSaveRestore(PPC::X1); 824 setExceptionPointerRegister(PPC::X3); 825 setExceptionSelectorRegister(PPC::X4); 826 } else { 827 setStackPointerRegisterToSaveRestore(PPC::R1); 828 setExceptionPointerRegister(PPC::R3); 829 setExceptionSelectorRegister(PPC::R4); 830 } 831 832 // We have target-specific dag combine patterns for the following nodes: 833 setTargetDAGCombine(ISD::SINT_TO_FP); 834 if (Subtarget.hasFPCVT()) 835 setTargetDAGCombine(ISD::UINT_TO_FP); 836 setTargetDAGCombine(ISD::LOAD); 837 setTargetDAGCombine(ISD::STORE); 838 setTargetDAGCombine(ISD::BR_CC); 839 if (Subtarget.useCRBits()) 840 setTargetDAGCombine(ISD::BRCOND); 841 setTargetDAGCombine(ISD::BSWAP); 842 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 843 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 844 setTargetDAGCombine(ISD::INTRINSIC_VOID); 845 846 setTargetDAGCombine(ISD::SIGN_EXTEND); 847 setTargetDAGCombine(ISD::ZERO_EXTEND); 848 setTargetDAGCombine(ISD::ANY_EXTEND); 849 850 if (Subtarget.useCRBits()) { 851 setTargetDAGCombine(ISD::TRUNCATE); 852 setTargetDAGCombine(ISD::SETCC); 853 setTargetDAGCombine(ISD::SELECT_CC); 854 } 855 856 // Use reciprocal estimates. 857 if (TM.Options.UnsafeFPMath) { 858 setTargetDAGCombine(ISD::FDIV); 859 setTargetDAGCombine(ISD::FSQRT); 860 } 861 862 // Darwin long double math library functions have $LDBL128 appended. 863 if (Subtarget.isDarwin()) { 864 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128"); 865 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128"); 866 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128"); 867 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128"); 868 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128"); 869 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128"); 870 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128"); 871 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128"); 872 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128"); 873 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128"); 874 } 875 876 // With 32 condition bits, we don't need to sink (and duplicate) compares 877 // aggressively in CodeGenPrep. 878 if (Subtarget.useCRBits()) { 879 setHasMultipleConditionRegisters(); 880 setJumpIsExpensive(); 881 } 882 883 setMinFunctionAlignment(2); 884 if (Subtarget.isDarwin()) 885 setPrefFunctionAlignment(4); 886 887 switch (Subtarget.getDarwinDirective()) { 888 default: break; 889 case PPC::DIR_970: 890 case PPC::DIR_A2: 891 case PPC::DIR_E500mc: 892 case PPC::DIR_E5500: 893 case PPC::DIR_PWR4: 894 case PPC::DIR_PWR5: 895 case PPC::DIR_PWR5X: 896 case PPC::DIR_PWR6: 897 case PPC::DIR_PWR6X: 898 case PPC::DIR_PWR7: 899 case PPC::DIR_PWR8: 900 setPrefFunctionAlignment(4); 901 setPrefLoopAlignment(4); 902 break; 903 } 904 905 setInsertFencesForAtomic(true); 906 907 if (Subtarget.enableMachineScheduler()) 908 setSchedulingPreference(Sched::Source); 909 else 910 setSchedulingPreference(Sched::Hybrid); 911 912 computeRegisterProperties(STI.getRegisterInfo()); 913 914 // The Freescale cores do better with aggressive inlining of memcpy and 915 // friends. GCC uses same threshold of 128 bytes (= 32 word stores). 916 if (Subtarget.getDarwinDirective() == PPC::DIR_E500mc || 917 Subtarget.getDarwinDirective() == PPC::DIR_E5500) { 918 MaxStoresPerMemset = 32; 919 MaxStoresPerMemsetOptSize = 16; 920 MaxStoresPerMemcpy = 32; 921 MaxStoresPerMemcpyOptSize = 8; 922 MaxStoresPerMemmove = 32; 923 MaxStoresPerMemmoveOptSize = 8; 924 } else if (Subtarget.getDarwinDirective() == PPC::DIR_A2) { 925 // The A2 also benefits from (very) aggressive inlining of memcpy and 926 // friends. The overhead of a the function call, even when warm, can be 927 // over one hundred cycles. 928 MaxStoresPerMemset = 128; 929 MaxStoresPerMemcpy = 128; 930 MaxStoresPerMemmove = 128; 931 } 932 } 933 934 /// getMaxByValAlign - Helper for getByValTypeAlignment to determine 935 /// the desired ByVal argument alignment. 936 static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign, 937 unsigned MaxMaxAlign) { 938 if (MaxAlign == MaxMaxAlign) 939 return; 940 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) { 941 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256) 942 MaxAlign = 32; 943 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16) 944 MaxAlign = 16; 945 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 946 unsigned EltAlign = 0; 947 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign); 948 if (EltAlign > MaxAlign) 949 MaxAlign = EltAlign; 950 } else if (StructType *STy = dyn_cast<StructType>(Ty)) { 951 for (auto *EltTy : STy->elements()) { 952 unsigned EltAlign = 0; 953 getMaxByValAlign(EltTy, EltAlign, MaxMaxAlign); 954 if (EltAlign > MaxAlign) 955 MaxAlign = EltAlign; 956 if (MaxAlign == MaxMaxAlign) 957 break; 958 } 959 } 960 } 961 962 /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate 963 /// function arguments in the caller parameter area. 964 unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty, 965 const DataLayout &DL) const { 966 // Darwin passes everything on 4 byte boundary. 967 if (Subtarget.isDarwin()) 968 return 4; 969 970 // 16byte and wider vectors are passed on 16byte boundary. 971 // The rest is 8 on PPC64 and 4 on PPC32 boundary. 972 unsigned Align = Subtarget.isPPC64() ? 8 : 4; 973 if (Subtarget.hasAltivec() || Subtarget.hasQPX()) 974 getMaxByValAlign(Ty, Align, Subtarget.hasQPX() ? 32 : 16); 975 return Align; 976 } 977 978 const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const { 979 switch ((PPCISD::NodeType)Opcode) { 980 case PPCISD::FIRST_NUMBER: break; 981 case PPCISD::FSEL: return "PPCISD::FSEL"; 982 case PPCISD::FCFID: return "PPCISD::FCFID"; 983 case PPCISD::FCFIDU: return "PPCISD::FCFIDU"; 984 case PPCISD::FCFIDS: return "PPCISD::FCFIDS"; 985 case PPCISD::FCFIDUS: return "PPCISD::FCFIDUS"; 986 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ"; 987 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ"; 988 case PPCISD::FCTIDUZ: return "PPCISD::FCTIDUZ"; 989 case PPCISD::FCTIWUZ: return "PPCISD::FCTIWUZ"; 990 case PPCISD::FRE: return "PPCISD::FRE"; 991 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE"; 992 case PPCISD::STFIWX: return "PPCISD::STFIWX"; 993 case PPCISD::VMADDFP: return "PPCISD::VMADDFP"; 994 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP"; 995 case PPCISD::VPERM: return "PPCISD::VPERM"; 996 case PPCISD::CMPB: return "PPCISD::CMPB"; 997 case PPCISD::Hi: return "PPCISD::Hi"; 998 case PPCISD::Lo: return "PPCISD::Lo"; 999 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY"; 1000 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC"; 1001 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg"; 1002 case PPCISD::SRL: return "PPCISD::SRL"; 1003 case PPCISD::SRA: return "PPCISD::SRA"; 1004 case PPCISD::SHL: return "PPCISD::SHL"; 1005 case PPCISD::SRA_ADDZE: return "PPCISD::SRA_ADDZE"; 1006 case PPCISD::CALL: return "PPCISD::CALL"; 1007 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP"; 1008 case PPCISD::MTCTR: return "PPCISD::MTCTR"; 1009 case PPCISD::BCTRL: return "PPCISD::BCTRL"; 1010 case PPCISD::BCTRL_LOAD_TOC: return "PPCISD::BCTRL_LOAD_TOC"; 1011 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG"; 1012 case PPCISD::READ_TIME_BASE: return "PPCISD::READ_TIME_BASE"; 1013 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP"; 1014 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP"; 1015 case PPCISD::MFOCRF: return "PPCISD::MFOCRF"; 1016 case PPCISD::MFVSR: return "PPCISD::MFVSR"; 1017 case PPCISD::MTVSRA: return "PPCISD::MTVSRA"; 1018 case PPCISD::MTVSRZ: return "PPCISD::MTVSRZ"; 1019 case PPCISD::ANDIo_1_EQ_BIT: return "PPCISD::ANDIo_1_EQ_BIT"; 1020 case PPCISD::ANDIo_1_GT_BIT: return "PPCISD::ANDIo_1_GT_BIT"; 1021 case PPCISD::VCMP: return "PPCISD::VCMP"; 1022 case PPCISD::VCMPo: return "PPCISD::VCMPo"; 1023 case PPCISD::LBRX: return "PPCISD::LBRX"; 1024 case PPCISD::STBRX: return "PPCISD::STBRX"; 1025 case PPCISD::LFIWAX: return "PPCISD::LFIWAX"; 1026 case PPCISD::LFIWZX: return "PPCISD::LFIWZX"; 1027 case PPCISD::LXVD2X: return "PPCISD::LXVD2X"; 1028 case PPCISD::STXVD2X: return "PPCISD::STXVD2X"; 1029 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH"; 1030 case PPCISD::BDNZ: return "PPCISD::BDNZ"; 1031 case PPCISD::BDZ: return "PPCISD::BDZ"; 1032 case PPCISD::MFFS: return "PPCISD::MFFS"; 1033 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ"; 1034 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN"; 1035 case PPCISD::CR6SET: return "PPCISD::CR6SET"; 1036 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET"; 1037 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT"; 1038 case PPCISD::PPC32_PICGOT: return "PPCISD::PPC32_PICGOT"; 1039 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA"; 1040 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L"; 1041 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS"; 1042 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA"; 1043 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L"; 1044 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR"; 1045 case PPCISD::ADDI_TLSGD_L_ADDR: return "PPCISD::ADDI_TLSGD_L_ADDR"; 1046 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA"; 1047 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L"; 1048 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR"; 1049 case PPCISD::ADDI_TLSLD_L_ADDR: return "PPCISD::ADDI_TLSLD_L_ADDR"; 1050 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA"; 1051 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L"; 1052 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT"; 1053 case PPCISD::SC: return "PPCISD::SC"; 1054 case PPCISD::CLRBHRB: return "PPCISD::CLRBHRB"; 1055 case PPCISD::MFBHRBE: return "PPCISD::MFBHRBE"; 1056 case PPCISD::RFEBB: return "PPCISD::RFEBB"; 1057 case PPCISD::XXSWAPD: return "PPCISD::XXSWAPD"; 1058 case PPCISD::QVFPERM: return "PPCISD::QVFPERM"; 1059 case PPCISD::QVGPCI: return "PPCISD::QVGPCI"; 1060 case PPCISD::QVALIGNI: return "PPCISD::QVALIGNI"; 1061 case PPCISD::QVESPLATI: return "PPCISD::QVESPLATI"; 1062 case PPCISD::QBFLT: return "PPCISD::QBFLT"; 1063 case PPCISD::QVLFSb: return "PPCISD::QVLFSb"; 1064 } 1065 return nullptr; 1066 } 1067 1068 EVT PPCTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &C, 1069 EVT VT) const { 1070 if (!VT.isVector()) 1071 return Subtarget.useCRBits() ? MVT::i1 : MVT::i32; 1072 1073 if (Subtarget.hasQPX()) 1074 return EVT::getVectorVT(C, MVT::i1, VT.getVectorNumElements()); 1075 1076 return VT.changeVectorElementTypeToInteger(); 1077 } 1078 1079 bool PPCTargetLowering::enableAggressiveFMAFusion(EVT VT) const { 1080 assert(VT.isFloatingPoint() && "Non-floating-point FMA?"); 1081 return true; 1082 } 1083 1084 //===----------------------------------------------------------------------===// 1085 // Node matching predicates, for use by the tblgen matching code. 1086 //===----------------------------------------------------------------------===// 1087 1088 /// isFloatingPointZero - Return true if this is 0.0 or -0.0. 1089 static bool isFloatingPointZero(SDValue Op) { 1090 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) 1091 return CFP->getValueAPF().isZero(); 1092 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { 1093 // Maybe this has already been legalized into the constant pool? 1094 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1))) 1095 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 1096 return CFP->getValueAPF().isZero(); 1097 } 1098 return false; 1099 } 1100 1101 /// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return 1102 /// true if Op is undef or if it matches the specified value. 1103 static bool isConstantOrUndef(int Op, int Val) { 1104 return Op < 0 || Op == Val; 1105 } 1106 1107 /// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a 1108 /// VPKUHUM instruction. 1109 /// The ShuffleKind distinguishes between big-endian operations with 1110 /// two different inputs (0), either-endian operations with two identical 1111 /// inputs (1), and little-endian operations with two different inputs (2). 1112 /// For the latter, the input operands are swapped (see PPCInstrAltivec.td). 1113 bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind, 1114 SelectionDAG &DAG) { 1115 bool IsLE = DAG.getDataLayout().isLittleEndian(); 1116 if (ShuffleKind == 0) { 1117 if (IsLE) 1118 return false; 1119 for (unsigned i = 0; i != 16; ++i) 1120 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1)) 1121 return false; 1122 } else if (ShuffleKind == 2) { 1123 if (!IsLE) 1124 return false; 1125 for (unsigned i = 0; i != 16; ++i) 1126 if (!isConstantOrUndef(N->getMaskElt(i), i*2)) 1127 return false; 1128 } else if (ShuffleKind == 1) { 1129 unsigned j = IsLE ? 0 : 1; 1130 for (unsigned i = 0; i != 8; ++i) 1131 if (!isConstantOrUndef(N->getMaskElt(i), i*2+j) || 1132 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j)) 1133 return false; 1134 } 1135 return true; 1136 } 1137 1138 /// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a 1139 /// VPKUWUM instruction. 1140 /// The ShuffleKind distinguishes between big-endian operations with 1141 /// two different inputs (0), either-endian operations with two identical 1142 /// inputs (1), and little-endian operations with two different inputs (2). 1143 /// For the latter, the input operands are swapped (see PPCInstrAltivec.td). 1144 bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind, 1145 SelectionDAG &DAG) { 1146 bool IsLE = DAG.getDataLayout().isLittleEndian(); 1147 if (ShuffleKind == 0) { 1148 if (IsLE) 1149 return false; 1150 for (unsigned i = 0; i != 16; i += 2) 1151 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) || 1152 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3)) 1153 return false; 1154 } else if (ShuffleKind == 2) { 1155 if (!IsLE) 1156 return false; 1157 for (unsigned i = 0; i != 16; i += 2) 1158 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) || 1159 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1)) 1160 return false; 1161 } else if (ShuffleKind == 1) { 1162 unsigned j = IsLE ? 0 : 2; 1163 for (unsigned i = 0; i != 8; i += 2) 1164 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) || 1165 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) || 1166 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) || 1167 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1)) 1168 return false; 1169 } 1170 return true; 1171 } 1172 1173 /// isVPKUDUMShuffleMask - Return true if this is the shuffle mask for a 1174 /// VPKUDUM instruction, AND the VPKUDUM instruction exists for the 1175 /// current subtarget. 1176 /// 1177 /// The ShuffleKind distinguishes between big-endian operations with 1178 /// two different inputs (0), either-endian operations with two identical 1179 /// inputs (1), and little-endian operations with two different inputs (2). 1180 /// For the latter, the input operands are swapped (see PPCInstrAltivec.td). 1181 bool PPC::isVPKUDUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind, 1182 SelectionDAG &DAG) { 1183 const PPCSubtarget& Subtarget = 1184 static_cast<const PPCSubtarget&>(DAG.getSubtarget()); 1185 if (!Subtarget.hasP8Vector()) 1186 return false; 1187 1188 bool IsLE = DAG.getDataLayout().isLittleEndian(); 1189 if (ShuffleKind == 0) { 1190 if (IsLE) 1191 return false; 1192 for (unsigned i = 0; i != 16; i += 4) 1193 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+4) || 1194 !isConstantOrUndef(N->getMaskElt(i+1), i*2+5) || 1195 !isConstantOrUndef(N->getMaskElt(i+2), i*2+6) || 1196 !isConstantOrUndef(N->getMaskElt(i+3), i*2+7)) 1197 return false; 1198 } else if (ShuffleKind == 2) { 1199 if (!IsLE) 1200 return false; 1201 for (unsigned i = 0; i != 16; i += 4) 1202 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) || 1203 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1) || 1204 !isConstantOrUndef(N->getMaskElt(i+2), i*2+2) || 1205 !isConstantOrUndef(N->getMaskElt(i+3), i*2+3)) 1206 return false; 1207 } else if (ShuffleKind == 1) { 1208 unsigned j = IsLE ? 0 : 4; 1209 for (unsigned i = 0; i != 8; i += 4) 1210 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) || 1211 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) || 1212 !isConstantOrUndef(N->getMaskElt(i+2), i*2+j+2) || 1213 !isConstantOrUndef(N->getMaskElt(i+3), i*2+j+3) || 1214 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) || 1215 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1) || 1216 !isConstantOrUndef(N->getMaskElt(i+10), i*2+j+2) || 1217 !isConstantOrUndef(N->getMaskElt(i+11), i*2+j+3)) 1218 return false; 1219 } 1220 return true; 1221 } 1222 1223 /// isVMerge - Common function, used to match vmrg* shuffles. 1224 /// 1225 static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize, 1226 unsigned LHSStart, unsigned RHSStart) { 1227 if (N->getValueType(0) != MVT::v16i8) 1228 return false; 1229 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) && 1230 "Unsupported merge size!"); 1231 1232 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units 1233 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit 1234 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j), 1235 LHSStart+j+i*UnitSize) || 1236 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j), 1237 RHSStart+j+i*UnitSize)) 1238 return false; 1239 } 1240 return true; 1241 } 1242 1243 /// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for 1244 /// a VMRGL* instruction with the specified unit size (1,2 or 4 bytes). 1245 /// The ShuffleKind distinguishes between big-endian merges with two 1246 /// different inputs (0), either-endian merges with two identical inputs (1), 1247 /// and little-endian merges with two different inputs (2). For the latter, 1248 /// the input operands are swapped (see PPCInstrAltivec.td). 1249 bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize, 1250 unsigned ShuffleKind, SelectionDAG &DAG) { 1251 if (DAG.getDataLayout().isLittleEndian()) { 1252 if (ShuffleKind == 1) // unary 1253 return isVMerge(N, UnitSize, 0, 0); 1254 else if (ShuffleKind == 2) // swapped 1255 return isVMerge(N, UnitSize, 0, 16); 1256 else 1257 return false; 1258 } else { 1259 if (ShuffleKind == 1) // unary 1260 return isVMerge(N, UnitSize, 8, 8); 1261 else if (ShuffleKind == 0) // normal 1262 return isVMerge(N, UnitSize, 8, 24); 1263 else 1264 return false; 1265 } 1266 } 1267 1268 /// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for 1269 /// a VMRGH* instruction with the specified unit size (1,2 or 4 bytes). 1270 /// The ShuffleKind distinguishes between big-endian merges with two 1271 /// different inputs (0), either-endian merges with two identical inputs (1), 1272 /// and little-endian merges with two different inputs (2). For the latter, 1273 /// the input operands are swapped (see PPCInstrAltivec.td). 1274 bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize, 1275 unsigned ShuffleKind, SelectionDAG &DAG) { 1276 if (DAG.getDataLayout().isLittleEndian()) { 1277 if (ShuffleKind == 1) // unary 1278 return isVMerge(N, UnitSize, 8, 8); 1279 else if (ShuffleKind == 2) // swapped 1280 return isVMerge(N, UnitSize, 8, 24); 1281 else 1282 return false; 1283 } else { 1284 if (ShuffleKind == 1) // unary 1285 return isVMerge(N, UnitSize, 0, 0); 1286 else if (ShuffleKind == 0) // normal 1287 return isVMerge(N, UnitSize, 0, 16); 1288 else 1289 return false; 1290 } 1291 } 1292 1293 /** 1294 * \brief Common function used to match vmrgew and vmrgow shuffles 1295 * 1296 * The indexOffset determines whether to look for even or odd words in 1297 * the shuffle mask. This is based on the of the endianness of the target 1298 * machine. 1299 * - Little Endian: 1300 * - Use offset of 0 to check for odd elements 1301 * - Use offset of 4 to check for even elements 1302 * - Big Endian: 1303 * - Use offset of 0 to check for even elements 1304 * - Use offset of 4 to check for odd elements 1305 * A detailed description of the vector element ordering for little endian and 1306 * big endian can be found at 1307 * http://www.ibm.com/developerworks/library/l-ibm-xl-c-cpp-compiler/index.html 1308 * Targeting your applications - what little endian and big endian IBM XL C/C++ 1309 * compiler differences mean to you 1310 * 1311 * The mask to the shuffle vector instruction specifies the indices of the 1312 * elements from the two input vectors to place in the result. The elements are 1313 * numbered in array-access order, starting with the first vector. These vectors 1314 * are always of type v16i8, thus each vector will contain 16 elements of size 1315 * 8. More info on the shuffle vector can be found in the 1316 * http://llvm.org/docs/LangRef.html#shufflevector-instruction 1317 * Language Reference. 1318 * 1319 * The RHSStartValue indicates whether the same input vectors are used (unary) 1320 * or two different input vectors are used, based on the following: 1321 * - If the instruction uses the same vector for both inputs, the range of the 1322 * indices will be 0 to 15. In this case, the RHSStart value passed should 1323 * be 0. 1324 * - If the instruction has two different vectors then the range of the 1325 * indices will be 0 to 31. In this case, the RHSStart value passed should 1326 * be 16 (indices 0-15 specify elements in the first vector while indices 16 1327 * to 31 specify elements in the second vector). 1328 * 1329 * \param[in] N The shuffle vector SD Node to analyze 1330 * \param[in] IndexOffset Specifies whether to look for even or odd elements 1331 * \param[in] RHSStartValue Specifies the starting index for the righthand input 1332 * vector to the shuffle_vector instruction 1333 * \return true iff this shuffle vector represents an even or odd word merge 1334 */ 1335 static bool isVMerge(ShuffleVectorSDNode *N, unsigned IndexOffset, 1336 unsigned RHSStartValue) { 1337 if (N->getValueType(0) != MVT::v16i8) 1338 return false; 1339 1340 for (unsigned i = 0; i < 2; ++i) 1341 for (unsigned j = 0; j < 4; ++j) 1342 if (!isConstantOrUndef(N->getMaskElt(i*4+j), 1343 i*RHSStartValue+j+IndexOffset) || 1344 !isConstantOrUndef(N->getMaskElt(i*4+j+8), 1345 i*RHSStartValue+j+IndexOffset+8)) 1346 return false; 1347 return true; 1348 } 1349 1350 /** 1351 * \brief Determine if the specified shuffle mask is suitable for the vmrgew or 1352 * vmrgow instructions. 1353 * 1354 * \param[in] N The shuffle vector SD Node to analyze 1355 * \param[in] CheckEven Check for an even merge (true) or an odd merge (false) 1356 * \param[in] ShuffleKind Identify the type of merge: 1357 * - 0 = big-endian merge with two different inputs; 1358 * - 1 = either-endian merge with two identical inputs; 1359 * - 2 = little-endian merge with two different inputs (inputs are swapped for 1360 * little-endian merges). 1361 * \param[in] DAG The current SelectionDAG 1362 * \return true iff this shuffle mask 1363 */ 1364 bool PPC::isVMRGEOShuffleMask(ShuffleVectorSDNode *N, bool CheckEven, 1365 unsigned ShuffleKind, SelectionDAG &DAG) { 1366 if (DAG.getDataLayout().isLittleEndian()) { 1367 unsigned indexOffset = CheckEven ? 4 : 0; 1368 if (ShuffleKind == 1) // Unary 1369 return isVMerge(N, indexOffset, 0); 1370 else if (ShuffleKind == 2) // swapped 1371 return isVMerge(N, indexOffset, 16); 1372 else 1373 return false; 1374 } 1375 else { 1376 unsigned indexOffset = CheckEven ? 0 : 4; 1377 if (ShuffleKind == 1) // Unary 1378 return isVMerge(N, indexOffset, 0); 1379 else if (ShuffleKind == 0) // Normal 1380 return isVMerge(N, indexOffset, 16); 1381 else 1382 return false; 1383 } 1384 return false; 1385 } 1386 1387 /// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift 1388 /// amount, otherwise return -1. 1389 /// The ShuffleKind distinguishes between big-endian operations with two 1390 /// different inputs (0), either-endian operations with two identical inputs 1391 /// (1), and little-endian operations with two different inputs (2). For the 1392 /// latter, the input operands are swapped (see PPCInstrAltivec.td). 1393 int PPC::isVSLDOIShuffleMask(SDNode *N, unsigned ShuffleKind, 1394 SelectionDAG &DAG) { 1395 if (N->getValueType(0) != MVT::v16i8) 1396 return -1; 1397 1398 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); 1399 1400 // Find the first non-undef value in the shuffle mask. 1401 unsigned i; 1402 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i) 1403 /*search*/; 1404 1405 if (i == 16) return -1; // all undef. 1406 1407 // Otherwise, check to see if the rest of the elements are consecutively 1408 // numbered from this value. 1409 unsigned ShiftAmt = SVOp->getMaskElt(i); 1410 if (ShiftAmt < i) return -1; 1411 1412 ShiftAmt -= i; 1413 bool isLE = DAG.getDataLayout().isLittleEndian(); 1414 1415 if ((ShuffleKind == 0 && !isLE) || (ShuffleKind == 2 && isLE)) { 1416 // Check the rest of the elements to see if they are consecutive. 1417 for (++i; i != 16; ++i) 1418 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i)) 1419 return -1; 1420 } else if (ShuffleKind == 1) { 1421 // Check the rest of the elements to see if they are consecutive. 1422 for (++i; i != 16; ++i) 1423 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15)) 1424 return -1; 1425 } else 1426 return -1; 1427 1428 if (isLE) 1429 ShiftAmt = 16 - ShiftAmt; 1430 1431 return ShiftAmt; 1432 } 1433 1434 /// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand 1435 /// specifies a splat of a single element that is suitable for input to 1436 /// VSPLTB/VSPLTH/VSPLTW. 1437 bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) { 1438 assert(N->getValueType(0) == MVT::v16i8 && 1439 (EltSize == 1 || EltSize == 2 || EltSize == 4)); 1440 1441 // The consecutive indices need to specify an element, not part of two 1442 // different elements. So abandon ship early if this isn't the case. 1443 if (N->getMaskElt(0) % EltSize != 0) 1444 return false; 1445 1446 // This is a splat operation if each element of the permute is the same, and 1447 // if the value doesn't reference the second vector. 1448 unsigned ElementBase = N->getMaskElt(0); 1449 1450 // FIXME: Handle UNDEF elements too! 1451 if (ElementBase >= 16) 1452 return false; 1453 1454 // Check that the indices are consecutive, in the case of a multi-byte element 1455 // splatted with a v16i8 mask. 1456 for (unsigned i = 1; i != EltSize; ++i) 1457 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase)) 1458 return false; 1459 1460 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) { 1461 if (N->getMaskElt(i) < 0) continue; 1462 for (unsigned j = 0; j != EltSize; ++j) 1463 if (N->getMaskElt(i+j) != N->getMaskElt(j)) 1464 return false; 1465 } 1466 return true; 1467 } 1468 1469 /// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the 1470 /// specified isSplatShuffleMask VECTOR_SHUFFLE mask. 1471 unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize, 1472 SelectionDAG &DAG) { 1473 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); 1474 assert(isSplatShuffleMask(SVOp, EltSize)); 1475 if (DAG.getDataLayout().isLittleEndian()) 1476 return (16 / EltSize) - 1 - (SVOp->getMaskElt(0) / EltSize); 1477 else 1478 return SVOp->getMaskElt(0) / EltSize; 1479 } 1480 1481 /// get_VSPLTI_elt - If this is a build_vector of constants which can be formed 1482 /// by using a vspltis[bhw] instruction of the specified element size, return 1483 /// the constant being splatted. The ByteSize field indicates the number of 1484 /// bytes of each element [124] -> [bhw]. 1485 SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) { 1486 SDValue OpVal(nullptr, 0); 1487 1488 // If ByteSize of the splat is bigger than the element size of the 1489 // build_vector, then we have a case where we are checking for a splat where 1490 // multiple elements of the buildvector are folded together into a single 1491 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8). 1492 unsigned EltSize = 16/N->getNumOperands(); 1493 if (EltSize < ByteSize) { 1494 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval. 1495 SDValue UniquedVals[4]; 1496 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?"); 1497 1498 // See if all of the elements in the buildvector agree across. 1499 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 1500 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; 1501 // If the element isn't a constant, bail fully out. 1502 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue(); 1503 1504 1505 if (!UniquedVals[i&(Multiple-1)].getNode()) 1506 UniquedVals[i&(Multiple-1)] = N->getOperand(i); 1507 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i)) 1508 return SDValue(); // no match. 1509 } 1510 1511 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains 1512 // either constant or undef values that are identical for each chunk. See 1513 // if these chunks can form into a larger vspltis*. 1514 1515 // Check to see if all of the leading entries are either 0 or -1. If 1516 // neither, then this won't fit into the immediate field. 1517 bool LeadingZero = true; 1518 bool LeadingOnes = true; 1519 for (unsigned i = 0; i != Multiple-1; ++i) { 1520 if (!UniquedVals[i].getNode()) continue; // Must have been undefs. 1521 1522 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue(); 1523 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue(); 1524 } 1525 // Finally, check the least significant entry. 1526 if (LeadingZero) { 1527 if (!UniquedVals[Multiple-1].getNode()) 1528 return DAG.getTargetConstant(0, SDLoc(N), MVT::i32); // 0,0,0,undef 1529 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue(); 1530 if (Val < 16) // 0,0,0,4 -> vspltisw(4) 1531 return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32); 1532 } 1533 if (LeadingOnes) { 1534 if (!UniquedVals[Multiple-1].getNode()) 1535 return DAG.getTargetConstant(~0U, SDLoc(N), MVT::i32); // -1,-1,-1,undef 1536 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue(); 1537 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2) 1538 return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32); 1539 } 1540 1541 return SDValue(); 1542 } 1543 1544 // Check to see if this buildvec has a single non-undef value in its elements. 1545 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 1546 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; 1547 if (!OpVal.getNode()) 1548 OpVal = N->getOperand(i); 1549 else if (OpVal != N->getOperand(i)) 1550 return SDValue(); 1551 } 1552 1553 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def. 1554 1555 unsigned ValSizeInBytes = EltSize; 1556 uint64_t Value = 0; 1557 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) { 1558 Value = CN->getZExtValue(); 1559 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) { 1560 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!"); 1561 Value = FloatToBits(CN->getValueAPF().convertToFloat()); 1562 } 1563 1564 // If the splat value is larger than the element value, then we can never do 1565 // this splat. The only case that we could fit the replicated bits into our 1566 // immediate field for would be zero, and we prefer to use vxor for it. 1567 if (ValSizeInBytes < ByteSize) return SDValue(); 1568 1569 // If the element value is larger than the splat value, check if it consists 1570 // of a repeated bit pattern of size ByteSize. 1571 if (!APInt(ValSizeInBytes * 8, Value).isSplat(ByteSize * 8)) 1572 return SDValue(); 1573 1574 // Properly sign extend the value. 1575 int MaskVal = SignExtend32(Value, ByteSize * 8); 1576 1577 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros. 1578 if (MaskVal == 0) return SDValue(); 1579 1580 // Finally, if this value fits in a 5 bit sext field, return it 1581 if (SignExtend32<5>(MaskVal) == MaskVal) 1582 return DAG.getTargetConstant(MaskVal, SDLoc(N), MVT::i32); 1583 return SDValue(); 1584 } 1585 1586 /// isQVALIGNIShuffleMask - If this is a qvaligni shuffle mask, return the shift 1587 /// amount, otherwise return -1. 1588 int PPC::isQVALIGNIShuffleMask(SDNode *N) { 1589 EVT VT = N->getValueType(0); 1590 if (VT != MVT::v4f64 && VT != MVT::v4f32 && VT != MVT::v4i1) 1591 return -1; 1592 1593 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); 1594 1595 // Find the first non-undef value in the shuffle mask. 1596 unsigned i; 1597 for (i = 0; i != 4 && SVOp->getMaskElt(i) < 0; ++i) 1598 /*search*/; 1599 1600 if (i == 4) return -1; // all undef. 1601 1602 // Otherwise, check to see if the rest of the elements are consecutively 1603 // numbered from this value. 1604 unsigned ShiftAmt = SVOp->getMaskElt(i); 1605 if (ShiftAmt < i) return -1; 1606 ShiftAmt -= i; 1607 1608 // Check the rest of the elements to see if they are consecutive. 1609 for (++i; i != 4; ++i) 1610 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i)) 1611 return -1; 1612 1613 return ShiftAmt; 1614 } 1615 1616 //===----------------------------------------------------------------------===// 1617 // Addressing Mode Selection 1618 //===----------------------------------------------------------------------===// 1619 1620 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit 1621 /// or 64-bit immediate, and if the value can be accurately represented as a 1622 /// sign extension from a 16-bit value. If so, this returns true and the 1623 /// immediate. 1624 static bool isIntS16Immediate(SDNode *N, short &Imm) { 1625 if (!isa<ConstantSDNode>(N)) 1626 return false; 1627 1628 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue(); 1629 if (N->getValueType(0) == MVT::i32) 1630 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue(); 1631 else 1632 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue(); 1633 } 1634 static bool isIntS16Immediate(SDValue Op, short &Imm) { 1635 return isIntS16Immediate(Op.getNode(), Imm); 1636 } 1637 1638 1639 /// SelectAddressRegReg - Given the specified addressed, check to see if it 1640 /// can be represented as an indexed [r+r] operation. Returns false if it 1641 /// can be more efficiently represented with [r+imm]. 1642 bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base, 1643 SDValue &Index, 1644 SelectionDAG &DAG) const { 1645 short imm = 0; 1646 if (N.getOpcode() == ISD::ADD) { 1647 if (isIntS16Immediate(N.getOperand(1), imm)) 1648 return false; // r+i 1649 if (N.getOperand(1).getOpcode() == PPCISD::Lo) 1650 return false; // r+i 1651 1652 Base = N.getOperand(0); 1653 Index = N.getOperand(1); 1654 return true; 1655 } else if (N.getOpcode() == ISD::OR) { 1656 if (isIntS16Immediate(N.getOperand(1), imm)) 1657 return false; // r+i can fold it if we can. 1658 1659 // If this is an or of disjoint bitfields, we can codegen this as an add 1660 // (for better address arithmetic) if the LHS and RHS of the OR are provably 1661 // disjoint. 1662 APInt LHSKnownZero, LHSKnownOne; 1663 APInt RHSKnownZero, RHSKnownOne; 1664 DAG.computeKnownBits(N.getOperand(0), 1665 LHSKnownZero, LHSKnownOne); 1666 1667 if (LHSKnownZero.getBoolValue()) { 1668 DAG.computeKnownBits(N.getOperand(1), 1669 RHSKnownZero, RHSKnownOne); 1670 // If all of the bits are known zero on the LHS or RHS, the add won't 1671 // carry. 1672 if (~(LHSKnownZero | RHSKnownZero) == 0) { 1673 Base = N.getOperand(0); 1674 Index = N.getOperand(1); 1675 return true; 1676 } 1677 } 1678 } 1679 1680 return false; 1681 } 1682 1683 // If we happen to be doing an i64 load or store into a stack slot that has 1684 // less than a 4-byte alignment, then the frame-index elimination may need to 1685 // use an indexed load or store instruction (because the offset may not be a 1686 // multiple of 4). The extra register needed to hold the offset comes from the 1687 // register scavenger, and it is possible that the scavenger will need to use 1688 // an emergency spill slot. As a result, we need to make sure that a spill slot 1689 // is allocated when doing an i64 load/store into a less-than-4-byte-aligned 1690 // stack slot. 1691 static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) { 1692 // FIXME: This does not handle the LWA case. 1693 if (VT != MVT::i64) 1694 return; 1695 1696 // NOTE: We'll exclude negative FIs here, which come from argument 1697 // lowering, because there are no known test cases triggering this problem 1698 // using packed structures (or similar). We can remove this exclusion if 1699 // we find such a test case. The reason why this is so test-case driven is 1700 // because this entire 'fixup' is only to prevent crashes (from the 1701 // register scavenger) on not-really-valid inputs. For example, if we have: 1702 // %a = alloca i1 1703 // %b = bitcast i1* %a to i64* 1704 // store i64* a, i64 b 1705 // then the store should really be marked as 'align 1', but is not. If it 1706 // were marked as 'align 1' then the indexed form would have been 1707 // instruction-selected initially, and the problem this 'fixup' is preventing 1708 // won't happen regardless. 1709 if (FrameIdx < 0) 1710 return; 1711 1712 MachineFunction &MF = DAG.getMachineFunction(); 1713 MachineFrameInfo *MFI = MF.getFrameInfo(); 1714 1715 unsigned Align = MFI->getObjectAlignment(FrameIdx); 1716 if (Align >= 4) 1717 return; 1718 1719 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 1720 FuncInfo->setHasNonRISpills(); 1721 } 1722 1723 /// Returns true if the address N can be represented by a base register plus 1724 /// a signed 16-bit displacement [r+imm], and if it is not better 1725 /// represented as reg+reg. If Aligned is true, only accept displacements 1726 /// suitable for STD and friends, i.e. multiples of 4. 1727 bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp, 1728 SDValue &Base, 1729 SelectionDAG &DAG, 1730 bool Aligned) const { 1731 // FIXME dl should come from parent load or store, not from address 1732 SDLoc dl(N); 1733 // If this can be more profitably realized as r+r, fail. 1734 if (SelectAddressRegReg(N, Disp, Base, DAG)) 1735 return false; 1736 1737 if (N.getOpcode() == ISD::ADD) { 1738 short imm = 0; 1739 if (isIntS16Immediate(N.getOperand(1), imm) && 1740 (!Aligned || (imm & 3) == 0)) { 1741 Disp = DAG.getTargetConstant(imm, dl, N.getValueType()); 1742 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) { 1743 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); 1744 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType()); 1745 } else { 1746 Base = N.getOperand(0); 1747 } 1748 return true; // [r+i] 1749 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) { 1750 // Match LOAD (ADD (X, Lo(G))). 1751 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue() 1752 && "Cannot handle constant offsets yet!"); 1753 Disp = N.getOperand(1).getOperand(0); // The global address. 1754 assert(Disp.getOpcode() == ISD::TargetGlobalAddress || 1755 Disp.getOpcode() == ISD::TargetGlobalTLSAddress || 1756 Disp.getOpcode() == ISD::TargetConstantPool || 1757 Disp.getOpcode() == ISD::TargetJumpTable); 1758 Base = N.getOperand(0); 1759 return true; // [&g+r] 1760 } 1761 } else if (N.getOpcode() == ISD::OR) { 1762 short imm = 0; 1763 if (isIntS16Immediate(N.getOperand(1), imm) && 1764 (!Aligned || (imm & 3) == 0)) { 1765 // If this is an or of disjoint bitfields, we can codegen this as an add 1766 // (for better address arithmetic) if the LHS and RHS of the OR are 1767 // provably disjoint. 1768 APInt LHSKnownZero, LHSKnownOne; 1769 DAG.computeKnownBits(N.getOperand(0), LHSKnownZero, LHSKnownOne); 1770 1771 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) { 1772 // If all of the bits are known zero on the LHS or RHS, the add won't 1773 // carry. 1774 if (FrameIndexSDNode *FI = 1775 dyn_cast<FrameIndexSDNode>(N.getOperand(0))) { 1776 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); 1777 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType()); 1778 } else { 1779 Base = N.getOperand(0); 1780 } 1781 Disp = DAG.getTargetConstant(imm, dl, N.getValueType()); 1782 return true; 1783 } 1784 } 1785 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { 1786 // Loading from a constant address. 1787 1788 // If this address fits entirely in a 16-bit sext immediate field, codegen 1789 // this as "d, 0" 1790 short Imm; 1791 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) { 1792 Disp = DAG.getTargetConstant(Imm, dl, CN->getValueType(0)); 1793 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO, 1794 CN->getValueType(0)); 1795 return true; 1796 } 1797 1798 // Handle 32-bit sext immediates with LIS + addr mode. 1799 if ((CN->getValueType(0) == MVT::i32 || 1800 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) && 1801 (!Aligned || (CN->getZExtValue() & 3) == 0)) { 1802 int Addr = (int)CN->getZExtValue(); 1803 1804 // Otherwise, break this down into an LIS + disp. 1805 Disp = DAG.getTargetConstant((short)Addr, dl, MVT::i32); 1806 1807 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, dl, 1808 MVT::i32); 1809 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8; 1810 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0); 1811 return true; 1812 } 1813 } 1814 1815 Disp = DAG.getTargetConstant(0, dl, getPointerTy(DAG.getDataLayout())); 1816 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) { 1817 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); 1818 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType()); 1819 } else 1820 Base = N; 1821 return true; // [r+0] 1822 } 1823 1824 /// SelectAddressRegRegOnly - Given the specified addressed, force it to be 1825 /// represented as an indexed [r+r] operation. 1826 bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base, 1827 SDValue &Index, 1828 SelectionDAG &DAG) const { 1829 // Check to see if we can easily represent this as an [r+r] address. This 1830 // will fail if it thinks that the address is more profitably represented as 1831 // reg+imm, e.g. where imm = 0. 1832 if (SelectAddressRegReg(N, Base, Index, DAG)) 1833 return true; 1834 1835 // If the operand is an addition, always emit this as [r+r], since this is 1836 // better (for code size, and execution, as the memop does the add for free) 1837 // than emitting an explicit add. 1838 if (N.getOpcode() == ISD::ADD) { 1839 Base = N.getOperand(0); 1840 Index = N.getOperand(1); 1841 return true; 1842 } 1843 1844 // Otherwise, do it the hard way, using R0 as the base register. 1845 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO, 1846 N.getValueType()); 1847 Index = N; 1848 return true; 1849 } 1850 1851 /// getPreIndexedAddressParts - returns true by value, base pointer and 1852 /// offset pointer and addressing mode by reference if the node's address 1853 /// can be legally represented as pre-indexed load / store address. 1854 bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 1855 SDValue &Offset, 1856 ISD::MemIndexedMode &AM, 1857 SelectionDAG &DAG) const { 1858 if (DisablePPCPreinc) return false; 1859 1860 bool isLoad = true; 1861 SDValue Ptr; 1862 EVT VT; 1863 unsigned Alignment; 1864 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 1865 Ptr = LD->getBasePtr(); 1866 VT = LD->getMemoryVT(); 1867 Alignment = LD->getAlignment(); 1868 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 1869 Ptr = ST->getBasePtr(); 1870 VT = ST->getMemoryVT(); 1871 Alignment = ST->getAlignment(); 1872 isLoad = false; 1873 } else 1874 return false; 1875 1876 // PowerPC doesn't have preinc load/store instructions for vectors (except 1877 // for QPX, which does have preinc r+r forms). 1878 if (VT.isVector()) { 1879 if (!Subtarget.hasQPX() || (VT != MVT::v4f64 && VT != MVT::v4f32)) { 1880 return false; 1881 } else if (SelectAddressRegRegOnly(Ptr, Offset, Base, DAG)) { 1882 AM = ISD::PRE_INC; 1883 return true; 1884 } 1885 } 1886 1887 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) { 1888 1889 // Common code will reject creating a pre-inc form if the base pointer 1890 // is a frame index, or if N is a store and the base pointer is either 1891 // the same as or a predecessor of the value being stored. Check for 1892 // those situations here, and try with swapped Base/Offset instead. 1893 bool Swap = false; 1894 1895 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base)) 1896 Swap = true; 1897 else if (!isLoad) { 1898 SDValue Val = cast<StoreSDNode>(N)->getValue(); 1899 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode())) 1900 Swap = true; 1901 } 1902 1903 if (Swap) 1904 std::swap(Base, Offset); 1905 1906 AM = ISD::PRE_INC; 1907 return true; 1908 } 1909 1910 // LDU/STU can only handle immediates that are a multiple of 4. 1911 if (VT != MVT::i64) { 1912 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false)) 1913 return false; 1914 } else { 1915 // LDU/STU need an address with at least 4-byte alignment. 1916 if (Alignment < 4) 1917 return false; 1918 1919 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true)) 1920 return false; 1921 } 1922 1923 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 1924 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of 1925 // sext i32 to i64 when addr mode is r+i. 1926 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 && 1927 LD->getExtensionType() == ISD::SEXTLOAD && 1928 isa<ConstantSDNode>(Offset)) 1929 return false; 1930 } 1931 1932 AM = ISD::PRE_INC; 1933 return true; 1934 } 1935 1936 //===----------------------------------------------------------------------===// 1937 // LowerOperation implementation 1938 //===----------------------------------------------------------------------===// 1939 1940 /// GetLabelAccessInfo - Return true if we should reference labels using a 1941 /// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags. 1942 static bool GetLabelAccessInfo(const TargetMachine &TM, 1943 const PPCSubtarget &Subtarget, 1944 unsigned &HiOpFlags, unsigned &LoOpFlags, 1945 const GlobalValue *GV = nullptr) { 1946 HiOpFlags = PPCII::MO_HA; 1947 LoOpFlags = PPCII::MO_LO; 1948 1949 // Don't use the pic base if not in PIC relocation model. 1950 bool isPIC = TM.getRelocationModel() == Reloc::PIC_; 1951 1952 if (isPIC) { 1953 HiOpFlags |= PPCII::MO_PIC_FLAG; 1954 LoOpFlags |= PPCII::MO_PIC_FLAG; 1955 } 1956 1957 // If this is a reference to a global value that requires a non-lazy-ptr, make 1958 // sure that instruction lowering adds it. 1959 if (GV && Subtarget.hasLazyResolverStub(GV)) { 1960 HiOpFlags |= PPCII::MO_NLP_FLAG; 1961 LoOpFlags |= PPCII::MO_NLP_FLAG; 1962 1963 if (GV->hasHiddenVisibility()) { 1964 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG; 1965 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG; 1966 } 1967 } 1968 1969 return isPIC; 1970 } 1971 1972 static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC, 1973 SelectionDAG &DAG) { 1974 SDLoc DL(HiPart); 1975 EVT PtrVT = HiPart.getValueType(); 1976 SDValue Zero = DAG.getConstant(0, DL, PtrVT); 1977 1978 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero); 1979 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero); 1980 1981 // With PIC, the first instruction is actually "GR+hi(&G)". 1982 if (isPIC) 1983 Hi = DAG.getNode(ISD::ADD, DL, PtrVT, 1984 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi); 1985 1986 // Generate non-pic code that has direct accesses to the constant pool. 1987 // The address of the global is just (hi(&g)+lo(&g)). 1988 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo); 1989 } 1990 1991 static void setUsesTOCBasePtr(MachineFunction &MF) { 1992 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 1993 FuncInfo->setUsesTOCBasePtr(); 1994 } 1995 1996 static void setUsesTOCBasePtr(SelectionDAG &DAG) { 1997 setUsesTOCBasePtr(DAG.getMachineFunction()); 1998 } 1999 2000 static SDValue getTOCEntry(SelectionDAG &DAG, SDLoc dl, bool Is64Bit, 2001 SDValue GA) { 2002 EVT VT = Is64Bit ? MVT::i64 : MVT::i32; 2003 SDValue Reg = Is64Bit ? DAG.getRegister(PPC::X2, VT) : 2004 DAG.getNode(PPCISD::GlobalBaseReg, dl, VT); 2005 2006 SDValue Ops[] = { GA, Reg }; 2007 return DAG.getMemIntrinsicNode( 2008 PPCISD::TOC_ENTRY, dl, DAG.getVTList(VT, MVT::Other), Ops, VT, 2009 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 0, false, true, 2010 false, 0); 2011 } 2012 2013 SDValue PPCTargetLowering::LowerConstantPool(SDValue Op, 2014 SelectionDAG &DAG) const { 2015 EVT PtrVT = Op.getValueType(); 2016 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 2017 const Constant *C = CP->getConstVal(); 2018 2019 // 64-bit SVR4 ABI code is always position-independent. 2020 // The actual address of the GlobalValue is stored in the TOC. 2021 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) { 2022 setUsesTOCBasePtr(DAG); 2023 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0); 2024 return getTOCEntry(DAG, SDLoc(CP), true, GA); 2025 } 2026 2027 unsigned MOHiFlag, MOLoFlag; 2028 bool isPIC = 2029 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag); 2030 2031 if (isPIC && Subtarget.isSVR4ABI()) { 2032 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 2033 PPCII::MO_PIC_FLAG); 2034 return getTOCEntry(DAG, SDLoc(CP), false, GA); 2035 } 2036 2037 SDValue CPIHi = 2038 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag); 2039 SDValue CPILo = 2040 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag); 2041 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG); 2042 } 2043 2044 SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const { 2045 EVT PtrVT = Op.getValueType(); 2046 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 2047 2048 // 64-bit SVR4 ABI code is always position-independent. 2049 // The actual address of the GlobalValue is stored in the TOC. 2050 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) { 2051 setUsesTOCBasePtr(DAG); 2052 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); 2053 return getTOCEntry(DAG, SDLoc(JT), true, GA); 2054 } 2055 2056 unsigned MOHiFlag, MOLoFlag; 2057 bool isPIC = 2058 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag); 2059 2060 if (isPIC && Subtarget.isSVR4ABI()) { 2061 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, 2062 PPCII::MO_PIC_FLAG); 2063 return getTOCEntry(DAG, SDLoc(GA), false, GA); 2064 } 2065 2066 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag); 2067 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag); 2068 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG); 2069 } 2070 2071 SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op, 2072 SelectionDAG &DAG) const { 2073 EVT PtrVT = Op.getValueType(); 2074 BlockAddressSDNode *BASDN = cast<BlockAddressSDNode>(Op); 2075 const BlockAddress *BA = BASDN->getBlockAddress(); 2076 2077 // 64-bit SVR4 ABI code is always position-independent. 2078 // The actual BlockAddress is stored in the TOC. 2079 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) { 2080 setUsesTOCBasePtr(DAG); 2081 SDValue GA = DAG.getTargetBlockAddress(BA, PtrVT, BASDN->getOffset()); 2082 return getTOCEntry(DAG, SDLoc(BASDN), true, GA); 2083 } 2084 2085 unsigned MOHiFlag, MOLoFlag; 2086 bool isPIC = 2087 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag); 2088 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag); 2089 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag); 2090 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG); 2091 } 2092 2093 SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op, 2094 SelectionDAG &DAG) const { 2095 2096 // FIXME: TLS addresses currently use medium model code sequences, 2097 // which is the most useful form. Eventually support for small and 2098 // large models could be added if users need it, at the cost of 2099 // additional complexity. 2100 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 2101 if (DAG.getTarget().Options.EmulatedTLS) 2102 return LowerToTLSEmulatedModel(GA, DAG); 2103 2104 SDLoc dl(GA); 2105 const GlobalValue *GV = GA->getGlobal(); 2106 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2107 bool is64bit = Subtarget.isPPC64(); 2108 const Module *M = DAG.getMachineFunction().getFunction()->getParent(); 2109 PICLevel::Level picLevel = M->getPICLevel(); 2110 2111 TLSModel::Model Model = getTargetMachine().getTLSModel(GV); 2112 2113 if (Model == TLSModel::LocalExec) { 2114 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 2115 PPCII::MO_TPREL_HA); 2116 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 2117 PPCII::MO_TPREL_LO); 2118 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2, 2119 is64bit ? MVT::i64 : MVT::i32); 2120 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg); 2121 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi); 2122 } 2123 2124 if (Model == TLSModel::InitialExec) { 2125 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0); 2126 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 2127 PPCII::MO_TLS); 2128 SDValue GOTPtr; 2129 if (is64bit) { 2130 setUsesTOCBasePtr(DAG); 2131 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64); 2132 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl, 2133 PtrVT, GOTReg, TGA); 2134 } else 2135 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT); 2136 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl, 2137 PtrVT, TGA, GOTPtr); 2138 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS); 2139 } 2140 2141 if (Model == TLSModel::GeneralDynamic) { 2142 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0); 2143 SDValue GOTPtr; 2144 if (is64bit) { 2145 setUsesTOCBasePtr(DAG); 2146 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64); 2147 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT, 2148 GOTReg, TGA); 2149 } else { 2150 if (picLevel == PICLevel::Small) 2151 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT); 2152 else 2153 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT); 2154 } 2155 return DAG.getNode(PPCISD::ADDI_TLSGD_L_ADDR, dl, PtrVT, 2156 GOTPtr, TGA, TGA); 2157 } 2158 2159 if (Model == TLSModel::LocalDynamic) { 2160 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0); 2161 SDValue GOTPtr; 2162 if (is64bit) { 2163 setUsesTOCBasePtr(DAG); 2164 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64); 2165 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT, 2166 GOTReg, TGA); 2167 } else { 2168 if (picLevel == PICLevel::Small) 2169 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT); 2170 else 2171 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT); 2172 } 2173 SDValue TLSAddr = DAG.getNode(PPCISD::ADDI_TLSLD_L_ADDR, dl, 2174 PtrVT, GOTPtr, TGA, TGA); 2175 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, 2176 PtrVT, TLSAddr, TGA); 2177 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA); 2178 } 2179 2180 llvm_unreachable("Unknown TLS model!"); 2181 } 2182 2183 SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op, 2184 SelectionDAG &DAG) const { 2185 EVT PtrVT = Op.getValueType(); 2186 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op); 2187 SDLoc DL(GSDN); 2188 const GlobalValue *GV = GSDN->getGlobal(); 2189 2190 // 64-bit SVR4 ABI code is always position-independent. 2191 // The actual address of the GlobalValue is stored in the TOC. 2192 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) { 2193 setUsesTOCBasePtr(DAG); 2194 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset()); 2195 return getTOCEntry(DAG, DL, true, GA); 2196 } 2197 2198 unsigned MOHiFlag, MOLoFlag; 2199 bool isPIC = 2200 GetLabelAccessInfo(DAG.getTarget(), Subtarget, MOHiFlag, MOLoFlag, GV); 2201 2202 if (isPIC && Subtarget.isSVR4ABI()) { 2203 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 2204 GSDN->getOffset(), 2205 PPCII::MO_PIC_FLAG); 2206 return getTOCEntry(DAG, DL, false, GA); 2207 } 2208 2209 SDValue GAHi = 2210 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag); 2211 SDValue GALo = 2212 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag); 2213 2214 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG); 2215 2216 // If the global reference is actually to a non-lazy-pointer, we have to do an 2217 // extra load to get the address of the global. 2218 if (MOHiFlag & PPCII::MO_NLP_FLAG) 2219 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(), 2220 false, false, false, 0); 2221 return Ptr; 2222 } 2223 2224 SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { 2225 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 2226 SDLoc dl(Op); 2227 2228 if (Op.getValueType() == MVT::v2i64) { 2229 // When the operands themselves are v2i64 values, we need to do something 2230 // special because VSX has no underlying comparison operations for these. 2231 if (Op.getOperand(0).getValueType() == MVT::v2i64) { 2232 // Equality can be handled by casting to the legal type for Altivec 2233 // comparisons, everything else needs to be expanded. 2234 if (CC == ISD::SETEQ || CC == ISD::SETNE) { 2235 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, 2236 DAG.getSetCC(dl, MVT::v4i32, 2237 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)), 2238 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)), 2239 CC)); 2240 } 2241 2242 return SDValue(); 2243 } 2244 2245 // We handle most of these in the usual way. 2246 return Op; 2247 } 2248 2249 // If we're comparing for equality to zero, expose the fact that this is 2250 // implented as a ctlz/srl pair on ppc, so that the dag combiner can 2251 // fold the new nodes. 2252 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2253 if (C->isNullValue() && CC == ISD::SETEQ) { 2254 EVT VT = Op.getOperand(0).getValueType(); 2255 SDValue Zext = Op.getOperand(0); 2256 if (VT.bitsLT(MVT::i32)) { 2257 VT = MVT::i32; 2258 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0)); 2259 } 2260 unsigned Log2b = Log2_32(VT.getSizeInBits()); 2261 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext); 2262 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz, 2263 DAG.getConstant(Log2b, dl, MVT::i32)); 2264 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc); 2265 } 2266 // Leave comparisons against 0 and -1 alone for now, since they're usually 2267 // optimized. FIXME: revisit this when we can custom lower all setcc 2268 // optimizations. 2269 if (C->isAllOnesValue() || C->isNullValue()) 2270 return SDValue(); 2271 } 2272 2273 // If we have an integer seteq/setne, turn it into a compare against zero 2274 // by xor'ing the rhs with the lhs, which is faster than setting a 2275 // condition register, reading it back out, and masking the correct bit. The 2276 // normal approach here uses sub to do this instead of xor. Using xor exposes 2277 // the result to other bit-twiddling opportunities. 2278 EVT LHSVT = Op.getOperand(0).getValueType(); 2279 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) { 2280 EVT VT = Op.getValueType(); 2281 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0), 2282 Op.getOperand(1)); 2283 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, dl, LHSVT), CC); 2284 } 2285 return SDValue(); 2286 } 2287 2288 SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG, 2289 const PPCSubtarget &Subtarget) const { 2290 SDNode *Node = Op.getNode(); 2291 EVT VT = Node->getValueType(0); 2292 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 2293 SDValue InChain = Node->getOperand(0); 2294 SDValue VAListPtr = Node->getOperand(1); 2295 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); 2296 SDLoc dl(Node); 2297 2298 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only"); 2299 2300 // gpr_index 2301 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain, 2302 VAListPtr, MachinePointerInfo(SV), MVT::i8, 2303 false, false, false, 0); 2304 InChain = GprIndex.getValue(1); 2305 2306 if (VT == MVT::i64) { 2307 // Check if GprIndex is even 2308 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex, 2309 DAG.getConstant(1, dl, MVT::i32)); 2310 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd, 2311 DAG.getConstant(0, dl, MVT::i32), ISD::SETNE); 2312 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex, 2313 DAG.getConstant(1, dl, MVT::i32)); 2314 // Align GprIndex to be even if it isn't 2315 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne, 2316 GprIndex); 2317 } 2318 2319 // fpr index is 1 byte after gpr 2320 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr, 2321 DAG.getConstant(1, dl, MVT::i32)); 2322 2323 // fpr 2324 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain, 2325 FprPtr, MachinePointerInfo(SV), MVT::i8, 2326 false, false, false, 0); 2327 InChain = FprIndex.getValue(1); 2328 2329 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr, 2330 DAG.getConstant(8, dl, MVT::i32)); 2331 2332 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr, 2333 DAG.getConstant(4, dl, MVT::i32)); 2334 2335 // areas 2336 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr, 2337 MachinePointerInfo(), false, false, 2338 false, 0); 2339 InChain = OverflowArea.getValue(1); 2340 2341 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr, 2342 MachinePointerInfo(), false, false, 2343 false, 0); 2344 InChain = RegSaveArea.getValue(1); 2345 2346 // select overflow_area if index > 8 2347 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex, 2348 DAG.getConstant(8, dl, MVT::i32), ISD::SETLT); 2349 2350 // adjustment constant gpr_index * 4/8 2351 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32, 2352 VT.isInteger() ? GprIndex : FprIndex, 2353 DAG.getConstant(VT.isInteger() ? 4 : 8, dl, 2354 MVT::i32)); 2355 2356 // OurReg = RegSaveArea + RegConstant 2357 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea, 2358 RegConstant); 2359 2360 // Floating types are 32 bytes into RegSaveArea 2361 if (VT.isFloatingPoint()) 2362 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg, 2363 DAG.getConstant(32, dl, MVT::i32)); 2364 2365 // increase {f,g}pr_index by 1 (or 2 if VT is i64) 2366 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32, 2367 VT.isInteger() ? GprIndex : FprIndex, 2368 DAG.getConstant(VT == MVT::i64 ? 2 : 1, dl, 2369 MVT::i32)); 2370 2371 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1, 2372 VT.isInteger() ? VAListPtr : FprPtr, 2373 MachinePointerInfo(SV), 2374 MVT::i8, false, false, 0); 2375 2376 // determine if we should load from reg_save_area or overflow_area 2377 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea); 2378 2379 // increase overflow_area by 4/8 if gpr/fpr > 8 2380 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea, 2381 DAG.getConstant(VT.isInteger() ? 4 : 8, 2382 dl, MVT::i32)); 2383 2384 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea, 2385 OverflowAreaPlusN); 2386 2387 InChain = DAG.getTruncStore(InChain, dl, OverflowArea, 2388 OverflowAreaPtr, 2389 MachinePointerInfo(), 2390 MVT::i32, false, false, 0); 2391 2392 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(), 2393 false, false, false, 0); 2394 } 2395 2396 SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG, 2397 const PPCSubtarget &Subtarget) const { 2398 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only"); 2399 2400 // We have to copy the entire va_list struct: 2401 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte 2402 return DAG.getMemcpy(Op.getOperand(0), Op, 2403 Op.getOperand(1), Op.getOperand(2), 2404 DAG.getConstant(12, SDLoc(Op), MVT::i32), 8, false, true, 2405 false, MachinePointerInfo(), MachinePointerInfo()); 2406 } 2407 2408 SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op, 2409 SelectionDAG &DAG) const { 2410 return Op.getOperand(0); 2411 } 2412 2413 SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op, 2414 SelectionDAG &DAG) const { 2415 SDValue Chain = Op.getOperand(0); 2416 SDValue Trmp = Op.getOperand(1); // trampoline 2417 SDValue FPtr = Op.getOperand(2); // nested function 2418 SDValue Nest = Op.getOperand(3); // 'nest' parameter value 2419 SDLoc dl(Op); 2420 2421 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 2422 bool isPPC64 = (PtrVT == MVT::i64); 2423 Type *IntPtrTy = DAG.getDataLayout().getIntPtrType(*DAG.getContext()); 2424 2425 TargetLowering::ArgListTy Args; 2426 TargetLowering::ArgListEntry Entry; 2427 2428 Entry.Ty = IntPtrTy; 2429 Entry.Node = Trmp; Args.push_back(Entry); 2430 2431 // TrampSize == (isPPC64 ? 48 : 40); 2432 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40, dl, 2433 isPPC64 ? MVT::i64 : MVT::i32); 2434 Args.push_back(Entry); 2435 2436 Entry.Node = FPtr; Args.push_back(Entry); 2437 Entry.Node = Nest; Args.push_back(Entry); 2438 2439 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg) 2440 TargetLowering::CallLoweringInfo CLI(DAG); 2441 CLI.setDebugLoc(dl).setChain(Chain) 2442 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), 2443 DAG.getExternalSymbol("__trampoline_setup", PtrVT), 2444 std::move(Args), 0); 2445 2446 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 2447 return CallResult.second; 2448 } 2449 2450 SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG, 2451 const PPCSubtarget &Subtarget) const { 2452 MachineFunction &MF = DAG.getMachineFunction(); 2453 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 2454 2455 SDLoc dl(Op); 2456 2457 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) { 2458 // vastart just stores the address of the VarArgsFrameIndex slot into the 2459 // memory location argument. 2460 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout()); 2461 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 2462 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 2463 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), 2464 MachinePointerInfo(SV), 2465 false, false, 0); 2466 } 2467 2468 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct. 2469 // We suppose the given va_list is already allocated. 2470 // 2471 // typedef struct { 2472 // char gpr; /* index into the array of 8 GPRs 2473 // * stored in the register save area 2474 // * gpr=0 corresponds to r3, 2475 // * gpr=1 to r4, etc. 2476 // */ 2477 // char fpr; /* index into the array of 8 FPRs 2478 // * stored in the register save area 2479 // * fpr=0 corresponds to f1, 2480 // * fpr=1 to f2, etc. 2481 // */ 2482 // char *overflow_arg_area; 2483 // /* location on stack that holds 2484 // * the next overflow argument 2485 // */ 2486 // char *reg_save_area; 2487 // /* where r3:r10 and f1:f8 (if saved) 2488 // * are stored 2489 // */ 2490 // } va_list[1]; 2491 2492 2493 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), dl, MVT::i32); 2494 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), dl, MVT::i32); 2495 2496 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout()); 2497 2498 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(), 2499 PtrVT); 2500 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), 2501 PtrVT); 2502 2503 uint64_t FrameOffset = PtrVT.getSizeInBits()/8; 2504 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, dl, PtrVT); 2505 2506 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1; 2507 SDValue ConstStackOffset = DAG.getConstant(StackOffset, dl, PtrVT); 2508 2509 uint64_t FPROffset = 1; 2510 SDValue ConstFPROffset = DAG.getConstant(FPROffset, dl, PtrVT); 2511 2512 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 2513 2514 // Store first byte : number of int regs 2515 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR, 2516 Op.getOperand(1), 2517 MachinePointerInfo(SV), 2518 MVT::i8, false, false, 0); 2519 uint64_t nextOffset = FPROffset; 2520 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1), 2521 ConstFPROffset); 2522 2523 // Store second byte : number of float regs 2524 SDValue secondStore = 2525 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr, 2526 MachinePointerInfo(SV, nextOffset), MVT::i8, 2527 false, false, 0); 2528 nextOffset += StackOffset; 2529 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset); 2530 2531 // Store second word : arguments given on stack 2532 SDValue thirdStore = 2533 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr, 2534 MachinePointerInfo(SV, nextOffset), 2535 false, false, 0); 2536 nextOffset += FrameOffset; 2537 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset); 2538 2539 // Store third word : arguments given in registers 2540 return DAG.getStore(thirdStore, dl, FR, nextPtr, 2541 MachinePointerInfo(SV, nextOffset), 2542 false, false, 0); 2543 2544 } 2545 2546 #include "PPCGenCallingConv.inc" 2547 2548 // Function whose sole purpose is to kill compiler warnings 2549 // stemming from unused functions included from PPCGenCallingConv.inc. 2550 CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const { 2551 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS; 2552 } 2553 2554 bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT, 2555 CCValAssign::LocInfo &LocInfo, 2556 ISD::ArgFlagsTy &ArgFlags, 2557 CCState &State) { 2558 return true; 2559 } 2560 2561 bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT, 2562 MVT &LocVT, 2563 CCValAssign::LocInfo &LocInfo, 2564 ISD::ArgFlagsTy &ArgFlags, 2565 CCState &State) { 2566 static const MCPhysReg ArgRegs[] = { 2567 PPC::R3, PPC::R4, PPC::R5, PPC::R6, 2568 PPC::R7, PPC::R8, PPC::R9, PPC::R10, 2569 }; 2570 const unsigned NumArgRegs = array_lengthof(ArgRegs); 2571 2572 unsigned RegNum = State.getFirstUnallocated(ArgRegs); 2573 2574 // Skip one register if the first unallocated register has an even register 2575 // number and there are still argument registers available which have not been 2576 // allocated yet. RegNum is actually an index into ArgRegs, which means we 2577 // need to skip a register if RegNum is odd. 2578 if (RegNum != NumArgRegs && RegNum % 2 == 1) { 2579 State.AllocateReg(ArgRegs[RegNum]); 2580 } 2581 2582 // Always return false here, as this function only makes sure that the first 2583 // unallocated register has an odd register number and does not actually 2584 // allocate a register for the current argument. 2585 return false; 2586 } 2587 2588 bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT, 2589 MVT &LocVT, 2590 CCValAssign::LocInfo &LocInfo, 2591 ISD::ArgFlagsTy &ArgFlags, 2592 CCState &State) { 2593 static const MCPhysReg ArgRegs[] = { 2594 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, 2595 PPC::F8 2596 }; 2597 2598 const unsigned NumArgRegs = array_lengthof(ArgRegs); 2599 2600 unsigned RegNum = State.getFirstUnallocated(ArgRegs); 2601 2602 // If there is only one Floating-point register left we need to put both f64 2603 // values of a split ppc_fp128 value on the stack. 2604 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) { 2605 State.AllocateReg(ArgRegs[RegNum]); 2606 } 2607 2608 // Always return false here, as this function only makes sure that the two f64 2609 // values a ppc_fp128 value is split into are both passed in registers or both 2610 // passed on the stack and does not actually allocate a register for the 2611 // current argument. 2612 return false; 2613 } 2614 2615 /// FPR - The set of FP registers that should be allocated for arguments, 2616 /// on Darwin. 2617 static const MCPhysReg FPR[] = {PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, 2618 PPC::F6, PPC::F7, PPC::F8, PPC::F9, PPC::F10, 2619 PPC::F11, PPC::F12, PPC::F13}; 2620 2621 /// QFPR - The set of QPX registers that should be allocated for arguments. 2622 static const MCPhysReg QFPR[] = { 2623 PPC::QF1, PPC::QF2, PPC::QF3, PPC::QF4, PPC::QF5, PPC::QF6, PPC::QF7, 2624 PPC::QF8, PPC::QF9, PPC::QF10, PPC::QF11, PPC::QF12, PPC::QF13}; 2625 2626 /// CalculateStackSlotSize - Calculates the size reserved for this argument on 2627 /// the stack. 2628 static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags, 2629 unsigned PtrByteSize) { 2630 unsigned ArgSize = ArgVT.getStoreSize(); 2631 if (Flags.isByVal()) 2632 ArgSize = Flags.getByValSize(); 2633 2634 // Round up to multiples of the pointer size, except for array members, 2635 // which are always packed. 2636 if (!Flags.isInConsecutiveRegs()) 2637 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 2638 2639 return ArgSize; 2640 } 2641 2642 /// CalculateStackSlotAlignment - Calculates the alignment of this argument 2643 /// on the stack. 2644 static unsigned CalculateStackSlotAlignment(EVT ArgVT, EVT OrigVT, 2645 ISD::ArgFlagsTy Flags, 2646 unsigned PtrByteSize) { 2647 unsigned Align = PtrByteSize; 2648 2649 // Altivec parameters are padded to a 16 byte boundary. 2650 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 || 2651 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 || 2652 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 || 2653 ArgVT == MVT::v1i128) 2654 Align = 16; 2655 // QPX vector types stored in double-precision are padded to a 32 byte 2656 // boundary. 2657 else if (ArgVT == MVT::v4f64 || ArgVT == MVT::v4i1) 2658 Align = 32; 2659 2660 // ByVal parameters are aligned as requested. 2661 if (Flags.isByVal()) { 2662 unsigned BVAlign = Flags.getByValAlign(); 2663 if (BVAlign > PtrByteSize) { 2664 if (BVAlign % PtrByteSize != 0) 2665 llvm_unreachable( 2666 "ByVal alignment is not a multiple of the pointer size"); 2667 2668 Align = BVAlign; 2669 } 2670 } 2671 2672 // Array members are always packed to their original alignment. 2673 if (Flags.isInConsecutiveRegs()) { 2674 // If the array member was split into multiple registers, the first 2675 // needs to be aligned to the size of the full type. (Except for 2676 // ppcf128, which is only aligned as its f64 components.) 2677 if (Flags.isSplit() && OrigVT != MVT::ppcf128) 2678 Align = OrigVT.getStoreSize(); 2679 else 2680 Align = ArgVT.getStoreSize(); 2681 } 2682 2683 return Align; 2684 } 2685 2686 /// CalculateStackSlotUsed - Return whether this argument will use its 2687 /// stack slot (instead of being passed in registers). ArgOffset, 2688 /// AvailableFPRs, and AvailableVRs must hold the current argument 2689 /// position, and will be updated to account for this argument. 2690 static bool CalculateStackSlotUsed(EVT ArgVT, EVT OrigVT, 2691 ISD::ArgFlagsTy Flags, 2692 unsigned PtrByteSize, 2693 unsigned LinkageSize, 2694 unsigned ParamAreaSize, 2695 unsigned &ArgOffset, 2696 unsigned &AvailableFPRs, 2697 unsigned &AvailableVRs, bool HasQPX) { 2698 bool UseMemory = false; 2699 2700 // Respect alignment of argument on the stack. 2701 unsigned Align = 2702 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize); 2703 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align; 2704 // If there's no space left in the argument save area, we must 2705 // use memory (this check also catches zero-sized arguments). 2706 if (ArgOffset >= LinkageSize + ParamAreaSize) 2707 UseMemory = true; 2708 2709 // Allocate argument on the stack. 2710 ArgOffset += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize); 2711 if (Flags.isInConsecutiveRegsLast()) 2712 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 2713 // If we overran the argument save area, we must use memory 2714 // (this check catches arguments passed partially in memory) 2715 if (ArgOffset > LinkageSize + ParamAreaSize) 2716 UseMemory = true; 2717 2718 // However, if the argument is actually passed in an FPR or a VR, 2719 // we don't use memory after all. 2720 if (!Flags.isByVal()) { 2721 if (ArgVT == MVT::f32 || ArgVT == MVT::f64 || 2722 // QPX registers overlap with the scalar FP registers. 2723 (HasQPX && (ArgVT == MVT::v4f32 || 2724 ArgVT == MVT::v4f64 || 2725 ArgVT == MVT::v4i1))) 2726 if (AvailableFPRs > 0) { 2727 --AvailableFPRs; 2728 return false; 2729 } 2730 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 || 2731 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 || 2732 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 || 2733 ArgVT == MVT::v1i128) 2734 if (AvailableVRs > 0) { 2735 --AvailableVRs; 2736 return false; 2737 } 2738 } 2739 2740 return UseMemory; 2741 } 2742 2743 /// EnsureStackAlignment - Round stack frame size up from NumBytes to 2744 /// ensure minimum alignment required for target. 2745 static unsigned EnsureStackAlignment(const PPCFrameLowering *Lowering, 2746 unsigned NumBytes) { 2747 unsigned TargetAlign = Lowering->getStackAlignment(); 2748 unsigned AlignMask = TargetAlign - 1; 2749 NumBytes = (NumBytes + AlignMask) & ~AlignMask; 2750 return NumBytes; 2751 } 2752 2753 SDValue 2754 PPCTargetLowering::LowerFormalArguments(SDValue Chain, 2755 CallingConv::ID CallConv, bool isVarArg, 2756 const SmallVectorImpl<ISD::InputArg> 2757 &Ins, 2758 SDLoc dl, SelectionDAG &DAG, 2759 SmallVectorImpl<SDValue> &InVals) 2760 const { 2761 if (Subtarget.isSVR4ABI()) { 2762 if (Subtarget.isPPC64()) 2763 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins, 2764 dl, DAG, InVals); 2765 else 2766 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins, 2767 dl, DAG, InVals); 2768 } else { 2769 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins, 2770 dl, DAG, InVals); 2771 } 2772 } 2773 2774 SDValue 2775 PPCTargetLowering::LowerFormalArguments_32SVR4( 2776 SDValue Chain, 2777 CallingConv::ID CallConv, bool isVarArg, 2778 const SmallVectorImpl<ISD::InputArg> 2779 &Ins, 2780 SDLoc dl, SelectionDAG &DAG, 2781 SmallVectorImpl<SDValue> &InVals) const { 2782 2783 // 32-bit SVR4 ABI Stack Frame Layout: 2784 // +-----------------------------------+ 2785 // +--> | Back chain | 2786 // | +-----------------------------------+ 2787 // | | Floating-point register save area | 2788 // | +-----------------------------------+ 2789 // | | General register save area | 2790 // | +-----------------------------------+ 2791 // | | CR save word | 2792 // | +-----------------------------------+ 2793 // | | VRSAVE save word | 2794 // | +-----------------------------------+ 2795 // | | Alignment padding | 2796 // | +-----------------------------------+ 2797 // | | Vector register save area | 2798 // | +-----------------------------------+ 2799 // | | Local variable space | 2800 // | +-----------------------------------+ 2801 // | | Parameter list area | 2802 // | +-----------------------------------+ 2803 // | | LR save word | 2804 // | +-----------------------------------+ 2805 // SP--> +--- | Back chain | 2806 // +-----------------------------------+ 2807 // 2808 // Specifications: 2809 // System V Application Binary Interface PowerPC Processor Supplement 2810 // AltiVec Technology Programming Interface Manual 2811 2812 MachineFunction &MF = DAG.getMachineFunction(); 2813 MachineFrameInfo *MFI = MF.getFrameInfo(); 2814 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 2815 2816 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout()); 2817 // Potential tail calls could cause overwriting of argument stack slots. 2818 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt && 2819 (CallConv == CallingConv::Fast)); 2820 unsigned PtrByteSize = 4; 2821 2822 // Assign locations to all of the incoming arguments. 2823 SmallVector<CCValAssign, 16> ArgLocs; 2824 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 2825 *DAG.getContext()); 2826 2827 // Reserve space for the linkage area on the stack. 2828 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); 2829 CCInfo.AllocateStack(LinkageSize, PtrByteSize); 2830 2831 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4); 2832 2833 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 2834 CCValAssign &VA = ArgLocs[i]; 2835 2836 // Arguments stored in registers. 2837 if (VA.isRegLoc()) { 2838 const TargetRegisterClass *RC; 2839 EVT ValVT = VA.getValVT(); 2840 2841 switch (ValVT.getSimpleVT().SimpleTy) { 2842 default: 2843 llvm_unreachable("ValVT not supported by formal arguments Lowering"); 2844 case MVT::i1: 2845 case MVT::i32: 2846 RC = &PPC::GPRCRegClass; 2847 break; 2848 case MVT::f32: 2849 if (Subtarget.hasP8Vector()) 2850 RC = &PPC::VSSRCRegClass; 2851 else 2852 RC = &PPC::F4RCRegClass; 2853 break; 2854 case MVT::f64: 2855 if (Subtarget.hasVSX()) 2856 RC = &PPC::VSFRCRegClass; 2857 else 2858 RC = &PPC::F8RCRegClass; 2859 break; 2860 case MVT::v16i8: 2861 case MVT::v8i16: 2862 case MVT::v4i32: 2863 RC = &PPC::VRRCRegClass; 2864 break; 2865 case MVT::v4f32: 2866 RC = Subtarget.hasQPX() ? &PPC::QSRCRegClass : &PPC::VRRCRegClass; 2867 break; 2868 case MVT::v2f64: 2869 case MVT::v2i64: 2870 RC = &PPC::VSHRCRegClass; 2871 break; 2872 case MVT::v4f64: 2873 RC = &PPC::QFRCRegClass; 2874 break; 2875 case MVT::v4i1: 2876 RC = &PPC::QBRCRegClass; 2877 break; 2878 } 2879 2880 // Transform the arguments stored in physical registers into virtual ones. 2881 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 2882 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, 2883 ValVT == MVT::i1 ? MVT::i32 : ValVT); 2884 2885 if (ValVT == MVT::i1) 2886 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue); 2887 2888 InVals.push_back(ArgValue); 2889 } else { 2890 // Argument stored in memory. 2891 assert(VA.isMemLoc()); 2892 2893 unsigned ArgSize = VA.getLocVT().getStoreSize(); 2894 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), 2895 isImmutable); 2896 2897 // Create load nodes to retrieve arguments from the stack. 2898 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 2899 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, 2900 MachinePointerInfo(), 2901 false, false, false, 0)); 2902 } 2903 } 2904 2905 // Assign locations to all of the incoming aggregate by value arguments. 2906 // Aggregates passed by value are stored in the local variable space of the 2907 // caller's stack frame, right above the parameter list area. 2908 SmallVector<CCValAssign, 16> ByValArgLocs; 2909 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(), 2910 ByValArgLocs, *DAG.getContext()); 2911 2912 // Reserve stack space for the allocations in CCInfo. 2913 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize); 2914 2915 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal); 2916 2917 // Area that is at least reserved in the caller of this function. 2918 unsigned MinReservedArea = CCByValInfo.getNextStackOffset(); 2919 MinReservedArea = std::max(MinReservedArea, LinkageSize); 2920 2921 // Set the size that is at least reserved in caller of this function. Tail 2922 // call optimized function's reserved stack space needs to be aligned so that 2923 // taking the difference between two stack areas will result in an aligned 2924 // stack. 2925 MinReservedArea = 2926 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea); 2927 FuncInfo->setMinReservedArea(MinReservedArea); 2928 2929 SmallVector<SDValue, 8> MemOps; 2930 2931 // If the function takes variable number of arguments, make a frame index for 2932 // the start of the first vararg value... for expansion of llvm.va_start. 2933 if (isVarArg) { 2934 static const MCPhysReg GPArgRegs[] = { 2935 PPC::R3, PPC::R4, PPC::R5, PPC::R6, 2936 PPC::R7, PPC::R8, PPC::R9, PPC::R10, 2937 }; 2938 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs); 2939 2940 static const MCPhysReg FPArgRegs[] = { 2941 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, 2942 PPC::F8 2943 }; 2944 unsigned NumFPArgRegs = array_lengthof(FPArgRegs); 2945 if (DisablePPCFloatInVariadic) 2946 NumFPArgRegs = 0; 2947 2948 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs)); 2949 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs)); 2950 2951 // Make room for NumGPArgRegs and NumFPArgRegs. 2952 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 + 2953 NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8; 2954 2955 FuncInfo->setVarArgsStackOffset( 2956 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8, 2957 CCInfo.getNextStackOffset(), true)); 2958 2959 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false)); 2960 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 2961 2962 // The fixed integer arguments of a variadic function are stored to the 2963 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing 2964 // the result of va_next. 2965 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) { 2966 // Get an existing live-in vreg, or add a new one. 2967 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]); 2968 if (!VReg) 2969 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass); 2970 2971 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 2972 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, 2973 MachinePointerInfo(), false, false, 0); 2974 MemOps.push_back(Store); 2975 // Increment the address by four for the next argument to store 2976 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT); 2977 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); 2978 } 2979 2980 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6 2981 // is set. 2982 // The double arguments are stored to the VarArgsFrameIndex 2983 // on the stack. 2984 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) { 2985 // Get an existing live-in vreg, or add a new one. 2986 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]); 2987 if (!VReg) 2988 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass); 2989 2990 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64); 2991 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, 2992 MachinePointerInfo(), false, false, 0); 2993 MemOps.push_back(Store); 2994 // Increment the address by eight for the next argument to store 2995 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8, dl, 2996 PtrVT); 2997 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); 2998 } 2999 } 3000 3001 if (!MemOps.empty()) 3002 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 3003 3004 return Chain; 3005 } 3006 3007 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote 3008 // value to MVT::i64 and then truncate to the correct register size. 3009 SDValue 3010 PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT, 3011 SelectionDAG &DAG, SDValue ArgVal, 3012 SDLoc dl) const { 3013 if (Flags.isSExt()) 3014 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal, 3015 DAG.getValueType(ObjectVT)); 3016 else if (Flags.isZExt()) 3017 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal, 3018 DAG.getValueType(ObjectVT)); 3019 3020 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal); 3021 } 3022 3023 SDValue 3024 PPCTargetLowering::LowerFormalArguments_64SVR4( 3025 SDValue Chain, 3026 CallingConv::ID CallConv, bool isVarArg, 3027 const SmallVectorImpl<ISD::InputArg> 3028 &Ins, 3029 SDLoc dl, SelectionDAG &DAG, 3030 SmallVectorImpl<SDValue> &InVals) const { 3031 // TODO: add description of PPC stack frame format, or at least some docs. 3032 // 3033 bool isELFv2ABI = Subtarget.isELFv2ABI(); 3034 bool isLittleEndian = Subtarget.isLittleEndian(); 3035 MachineFunction &MF = DAG.getMachineFunction(); 3036 MachineFrameInfo *MFI = MF.getFrameInfo(); 3037 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 3038 3039 assert(!(CallConv == CallingConv::Fast && isVarArg) && 3040 "fastcc not supported on varargs functions"); 3041 3042 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout()); 3043 // Potential tail calls could cause overwriting of argument stack slots. 3044 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt && 3045 (CallConv == CallingConv::Fast)); 3046 unsigned PtrByteSize = 8; 3047 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); 3048 3049 static const MCPhysReg GPR[] = { 3050 PPC::X3, PPC::X4, PPC::X5, PPC::X6, 3051 PPC::X7, PPC::X8, PPC::X9, PPC::X10, 3052 }; 3053 static const MCPhysReg VR[] = { 3054 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, 3055 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 3056 }; 3057 static const MCPhysReg VSRH[] = { 3058 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8, 3059 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13 3060 }; 3061 3062 const unsigned Num_GPR_Regs = array_lengthof(GPR); 3063 const unsigned Num_FPR_Regs = 13; 3064 const unsigned Num_VR_Regs = array_lengthof(VR); 3065 const unsigned Num_QFPR_Regs = Num_FPR_Regs; 3066 3067 // Do a first pass over the arguments to determine whether the ABI 3068 // guarantees that our caller has allocated the parameter save area 3069 // on its stack frame. In the ELFv1 ABI, this is always the case; 3070 // in the ELFv2 ABI, it is true if this is a vararg function or if 3071 // any parameter is located in a stack slot. 3072 3073 bool HasParameterArea = !isELFv2ABI || isVarArg; 3074 unsigned ParamAreaSize = Num_GPR_Regs * PtrByteSize; 3075 unsigned NumBytes = LinkageSize; 3076 unsigned AvailableFPRs = Num_FPR_Regs; 3077 unsigned AvailableVRs = Num_VR_Regs; 3078 for (unsigned i = 0, e = Ins.size(); i != e; ++i) { 3079 if (Ins[i].Flags.isNest()) 3080 continue; 3081 3082 if (CalculateStackSlotUsed(Ins[i].VT, Ins[i].ArgVT, Ins[i].Flags, 3083 PtrByteSize, LinkageSize, ParamAreaSize, 3084 NumBytes, AvailableFPRs, AvailableVRs, 3085 Subtarget.hasQPX())) 3086 HasParameterArea = true; 3087 } 3088 3089 // Add DAG nodes to load the arguments or copy them out of registers. On 3090 // entry to a function on PPC, the arguments start after the linkage area, 3091 // although the first ones are often in registers. 3092 3093 unsigned ArgOffset = LinkageSize; 3094 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; 3095 unsigned &QFPR_idx = FPR_idx; 3096 SmallVector<SDValue, 8> MemOps; 3097 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin(); 3098 unsigned CurArgIdx = 0; 3099 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) { 3100 SDValue ArgVal; 3101 bool needsLoad = false; 3102 EVT ObjectVT = Ins[ArgNo].VT; 3103 EVT OrigVT = Ins[ArgNo].ArgVT; 3104 unsigned ObjSize = ObjectVT.getStoreSize(); 3105 unsigned ArgSize = ObjSize; 3106 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags; 3107 if (Ins[ArgNo].isOrigArg()) { 3108 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx); 3109 CurArgIdx = Ins[ArgNo].getOrigArgIndex(); 3110 } 3111 // We re-align the argument offset for each argument, except when using the 3112 // fast calling convention, when we need to make sure we do that only when 3113 // we'll actually use a stack slot. 3114 unsigned CurArgOffset, Align; 3115 auto ComputeArgOffset = [&]() { 3116 /* Respect alignment of argument on the stack. */ 3117 Align = CalculateStackSlotAlignment(ObjectVT, OrigVT, Flags, PtrByteSize); 3118 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align; 3119 CurArgOffset = ArgOffset; 3120 }; 3121 3122 if (CallConv != CallingConv::Fast) { 3123 ComputeArgOffset(); 3124 3125 /* Compute GPR index associated with argument offset. */ 3126 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize; 3127 GPR_idx = std::min(GPR_idx, Num_GPR_Regs); 3128 } 3129 3130 // FIXME the codegen can be much improved in some cases. 3131 // We do not have to keep everything in memory. 3132 if (Flags.isByVal()) { 3133 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit"); 3134 3135 if (CallConv == CallingConv::Fast) 3136 ComputeArgOffset(); 3137 3138 // ObjSize is the true size, ArgSize rounded up to multiple of registers. 3139 ObjSize = Flags.getByValSize(); 3140 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 3141 // Empty aggregate parameters do not take up registers. Examples: 3142 // struct { } a; 3143 // union { } b; 3144 // int c[0]; 3145 // etc. However, we have to provide a place-holder in InVals, so 3146 // pretend we have an 8-byte item at the current address for that 3147 // purpose. 3148 if (!ObjSize) { 3149 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true); 3150 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3151 InVals.push_back(FIN); 3152 continue; 3153 } 3154 3155 // Create a stack object covering all stack doublewords occupied 3156 // by the argument. If the argument is (fully or partially) on 3157 // the stack, or if the argument is fully in registers but the 3158 // caller has allocated the parameter save anyway, we can refer 3159 // directly to the caller's stack frame. Otherwise, create a 3160 // local copy in our own frame. 3161 int FI; 3162 if (HasParameterArea || 3163 ArgSize + ArgOffset > LinkageSize + Num_GPR_Regs * PtrByteSize) 3164 FI = MFI->CreateFixedObject(ArgSize, ArgOffset, false, true); 3165 else 3166 FI = MFI->CreateStackObject(ArgSize, Align, false); 3167 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3168 3169 // Handle aggregates smaller than 8 bytes. 3170 if (ObjSize < PtrByteSize) { 3171 // The value of the object is its address, which differs from the 3172 // address of the enclosing doubleword on big-endian systems. 3173 SDValue Arg = FIN; 3174 if (!isLittleEndian) { 3175 SDValue ArgOff = DAG.getConstant(PtrByteSize - ObjSize, dl, PtrVT); 3176 Arg = DAG.getNode(ISD::ADD, dl, ArgOff.getValueType(), Arg, ArgOff); 3177 } 3178 InVals.push_back(Arg); 3179 3180 if (GPR_idx != Num_GPR_Regs) { 3181 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass); 3182 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 3183 SDValue Store; 3184 3185 if (ObjSize==1 || ObjSize==2 || ObjSize==4) { 3186 EVT ObjType = (ObjSize == 1 ? MVT::i8 : 3187 (ObjSize == 2 ? MVT::i16 : MVT::i32)); 3188 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, Arg, 3189 MachinePointerInfo(FuncArg), 3190 ObjType, false, false, 0); 3191 } else { 3192 // For sizes that don't fit a truncating store (3, 5, 6, 7), 3193 // store the whole register as-is to the parameter save area 3194 // slot. 3195 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, 3196 MachinePointerInfo(FuncArg), 3197 false, false, 0); 3198 } 3199 3200 MemOps.push_back(Store); 3201 } 3202 // Whether we copied from a register or not, advance the offset 3203 // into the parameter save area by a full doubleword. 3204 ArgOffset += PtrByteSize; 3205 continue; 3206 } 3207 3208 // The value of the object is its address, which is the address of 3209 // its first stack doubleword. 3210 InVals.push_back(FIN); 3211 3212 // Store whatever pieces of the object are in registers to memory. 3213 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) { 3214 if (GPR_idx == Num_GPR_Regs) 3215 break; 3216 3217 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); 3218 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 3219 SDValue Addr = FIN; 3220 if (j) { 3221 SDValue Off = DAG.getConstant(j, dl, PtrVT); 3222 Addr = DAG.getNode(ISD::ADD, dl, Off.getValueType(), Addr, Off); 3223 } 3224 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, Addr, 3225 MachinePointerInfo(FuncArg, j), 3226 false, false, 0); 3227 MemOps.push_back(Store); 3228 ++GPR_idx; 3229 } 3230 ArgOffset += ArgSize; 3231 continue; 3232 } 3233 3234 switch (ObjectVT.getSimpleVT().SimpleTy) { 3235 default: llvm_unreachable("Unhandled argument type!"); 3236 case MVT::i1: 3237 case MVT::i32: 3238 case MVT::i64: 3239 if (Flags.isNest()) { 3240 // The 'nest' parameter, if any, is passed in R11. 3241 unsigned VReg = MF.addLiveIn(PPC::X11, &PPC::G8RCRegClass); 3242 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); 3243 3244 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1) 3245 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl); 3246 3247 break; 3248 } 3249 3250 // These can be scalar arguments or elements of an integer array type 3251 // passed directly. Clang may use those instead of "byval" aggregate 3252 // types to avoid forcing arguments to memory unnecessarily. 3253 if (GPR_idx != Num_GPR_Regs) { 3254 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass); 3255 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); 3256 3257 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1) 3258 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote 3259 // value to MVT::i64 and then truncate to the correct register size. 3260 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl); 3261 } else { 3262 if (CallConv == CallingConv::Fast) 3263 ComputeArgOffset(); 3264 3265 needsLoad = true; 3266 ArgSize = PtrByteSize; 3267 } 3268 if (CallConv != CallingConv::Fast || needsLoad) 3269 ArgOffset += 8; 3270 break; 3271 3272 case MVT::f32: 3273 case MVT::f64: 3274 // These can be scalar arguments or elements of a float array type 3275 // passed directly. The latter are used to implement ELFv2 homogenous 3276 // float aggregates. 3277 if (FPR_idx != Num_FPR_Regs) { 3278 unsigned VReg; 3279 3280 if (ObjectVT == MVT::f32) 3281 VReg = MF.addLiveIn(FPR[FPR_idx], 3282 Subtarget.hasP8Vector() 3283 ? &PPC::VSSRCRegClass 3284 : &PPC::F4RCRegClass); 3285 else 3286 VReg = MF.addLiveIn(FPR[FPR_idx], Subtarget.hasVSX() 3287 ? &PPC::VSFRCRegClass 3288 : &PPC::F8RCRegClass); 3289 3290 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); 3291 ++FPR_idx; 3292 } else if (GPR_idx != Num_GPR_Regs && CallConv != CallingConv::Fast) { 3293 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8 3294 // once we support fp <-> gpr moves. 3295 3296 // This can only ever happen in the presence of f32 array types, 3297 // since otherwise we never run out of FPRs before running out 3298 // of GPRs. 3299 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass); 3300 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); 3301 3302 if (ObjectVT == MVT::f32) { 3303 if ((ArgOffset % PtrByteSize) == (isLittleEndian ? 4 : 0)) 3304 ArgVal = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgVal, 3305 DAG.getConstant(32, dl, MVT::i32)); 3306 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal); 3307 } 3308 3309 ArgVal = DAG.getNode(ISD::BITCAST, dl, ObjectVT, ArgVal); 3310 } else { 3311 if (CallConv == CallingConv::Fast) 3312 ComputeArgOffset(); 3313 3314 needsLoad = true; 3315 } 3316 3317 // When passing an array of floats, the array occupies consecutive 3318 // space in the argument area; only round up to the next doubleword 3319 // at the end of the array. Otherwise, each float takes 8 bytes. 3320 if (CallConv != CallingConv::Fast || needsLoad) { 3321 ArgSize = Flags.isInConsecutiveRegs() ? ObjSize : PtrByteSize; 3322 ArgOffset += ArgSize; 3323 if (Flags.isInConsecutiveRegsLast()) 3324 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 3325 } 3326 break; 3327 case MVT::v4f32: 3328 case MVT::v4i32: 3329 case MVT::v8i16: 3330 case MVT::v16i8: 3331 case MVT::v2f64: 3332 case MVT::v2i64: 3333 case MVT::v1i128: 3334 if (!Subtarget.hasQPX()) { 3335 // These can be scalar arguments or elements of a vector array type 3336 // passed directly. The latter are used to implement ELFv2 homogenous 3337 // vector aggregates. 3338 if (VR_idx != Num_VR_Regs) { 3339 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ? 3340 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) : 3341 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass); 3342 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); 3343 ++VR_idx; 3344 } else { 3345 if (CallConv == CallingConv::Fast) 3346 ComputeArgOffset(); 3347 3348 needsLoad = true; 3349 } 3350 if (CallConv != CallingConv::Fast || needsLoad) 3351 ArgOffset += 16; 3352 break; 3353 } // not QPX 3354 3355 assert(ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 && 3356 "Invalid QPX parameter type"); 3357 /* fall through */ 3358 3359 case MVT::v4f64: 3360 case MVT::v4i1: 3361 // QPX vectors are treated like their scalar floating-point subregisters 3362 // (except that they're larger). 3363 unsigned Sz = ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 ? 16 : 32; 3364 if (QFPR_idx != Num_QFPR_Regs) { 3365 const TargetRegisterClass *RC; 3366 switch (ObjectVT.getSimpleVT().SimpleTy) { 3367 case MVT::v4f64: RC = &PPC::QFRCRegClass; break; 3368 case MVT::v4f32: RC = &PPC::QSRCRegClass; break; 3369 default: RC = &PPC::QBRCRegClass; break; 3370 } 3371 3372 unsigned VReg = MF.addLiveIn(QFPR[QFPR_idx], RC); 3373 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); 3374 ++QFPR_idx; 3375 } else { 3376 if (CallConv == CallingConv::Fast) 3377 ComputeArgOffset(); 3378 needsLoad = true; 3379 } 3380 if (CallConv != CallingConv::Fast || needsLoad) 3381 ArgOffset += Sz; 3382 break; 3383 } 3384 3385 // We need to load the argument to a virtual register if we determined 3386 // above that we ran out of physical registers of the appropriate type. 3387 if (needsLoad) { 3388 if (ObjSize < ArgSize && !isLittleEndian) 3389 CurArgOffset += ArgSize - ObjSize; 3390 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, isImmutable); 3391 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3392 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(), 3393 false, false, false, 0); 3394 } 3395 3396 InVals.push_back(ArgVal); 3397 } 3398 3399 // Area that is at least reserved in the caller of this function. 3400 unsigned MinReservedArea; 3401 if (HasParameterArea) 3402 MinReservedArea = std::max(ArgOffset, LinkageSize + 8 * PtrByteSize); 3403 else 3404 MinReservedArea = LinkageSize; 3405 3406 // Set the size that is at least reserved in caller of this function. Tail 3407 // call optimized functions' reserved stack space needs to be aligned so that 3408 // taking the difference between two stack areas will result in an aligned 3409 // stack. 3410 MinReservedArea = 3411 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea); 3412 FuncInfo->setMinReservedArea(MinReservedArea); 3413 3414 // If the function takes variable number of arguments, make a frame index for 3415 // the start of the first vararg value... for expansion of llvm.va_start. 3416 if (isVarArg) { 3417 int Depth = ArgOffset; 3418 3419 FuncInfo->setVarArgsFrameIndex( 3420 MFI->CreateFixedObject(PtrByteSize, Depth, true)); 3421 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 3422 3423 // If this function is vararg, store any remaining integer argument regs 3424 // to their spots on the stack so that they may be loaded by deferencing the 3425 // result of va_next. 3426 for (GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize; 3427 GPR_idx < Num_GPR_Regs; ++GPR_idx) { 3428 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); 3429 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 3430 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, 3431 MachinePointerInfo(), false, false, 0); 3432 MemOps.push_back(Store); 3433 // Increment the address by four for the next argument to store 3434 SDValue PtrOff = DAG.getConstant(PtrByteSize, dl, PtrVT); 3435 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); 3436 } 3437 } 3438 3439 if (!MemOps.empty()) 3440 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 3441 3442 return Chain; 3443 } 3444 3445 SDValue 3446 PPCTargetLowering::LowerFormalArguments_Darwin( 3447 SDValue Chain, 3448 CallingConv::ID CallConv, bool isVarArg, 3449 const SmallVectorImpl<ISD::InputArg> 3450 &Ins, 3451 SDLoc dl, SelectionDAG &DAG, 3452 SmallVectorImpl<SDValue> &InVals) const { 3453 // TODO: add description of PPC stack frame format, or at least some docs. 3454 // 3455 MachineFunction &MF = DAG.getMachineFunction(); 3456 MachineFrameInfo *MFI = MF.getFrameInfo(); 3457 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 3458 3459 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout()); 3460 bool isPPC64 = PtrVT == MVT::i64; 3461 // Potential tail calls could cause overwriting of argument stack slots. 3462 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt && 3463 (CallConv == CallingConv::Fast)); 3464 unsigned PtrByteSize = isPPC64 ? 8 : 4; 3465 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); 3466 unsigned ArgOffset = LinkageSize; 3467 // Area that is at least reserved in caller of this function. 3468 unsigned MinReservedArea = ArgOffset; 3469 3470 static const MCPhysReg GPR_32[] = { // 32-bit registers. 3471 PPC::R3, PPC::R4, PPC::R5, PPC::R6, 3472 PPC::R7, PPC::R8, PPC::R9, PPC::R10, 3473 }; 3474 static const MCPhysReg GPR_64[] = { // 64-bit registers. 3475 PPC::X3, PPC::X4, PPC::X5, PPC::X6, 3476 PPC::X7, PPC::X8, PPC::X9, PPC::X10, 3477 }; 3478 static const MCPhysReg VR[] = { 3479 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, 3480 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 3481 }; 3482 3483 const unsigned Num_GPR_Regs = array_lengthof(GPR_32); 3484 const unsigned Num_FPR_Regs = 13; 3485 const unsigned Num_VR_Regs = array_lengthof( VR); 3486 3487 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; 3488 3489 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32; 3490 3491 // In 32-bit non-varargs functions, the stack space for vectors is after the 3492 // stack space for non-vectors. We do not use this space unless we have 3493 // too many vectors to fit in registers, something that only occurs in 3494 // constructed examples:), but we have to walk the arglist to figure 3495 // that out...for the pathological case, compute VecArgOffset as the 3496 // start of the vector parameter area. Computing VecArgOffset is the 3497 // entire point of the following loop. 3498 unsigned VecArgOffset = ArgOffset; 3499 if (!isVarArg && !isPPC64) { 3500 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; 3501 ++ArgNo) { 3502 EVT ObjectVT = Ins[ArgNo].VT; 3503 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags; 3504 3505 if (Flags.isByVal()) { 3506 // ObjSize is the true size, ArgSize rounded up to multiple of regs. 3507 unsigned ObjSize = Flags.getByValSize(); 3508 unsigned ArgSize = 3509 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 3510 VecArgOffset += ArgSize; 3511 continue; 3512 } 3513 3514 switch(ObjectVT.getSimpleVT().SimpleTy) { 3515 default: llvm_unreachable("Unhandled argument type!"); 3516 case MVT::i1: 3517 case MVT::i32: 3518 case MVT::f32: 3519 VecArgOffset += 4; 3520 break; 3521 case MVT::i64: // PPC64 3522 case MVT::f64: 3523 // FIXME: We are guaranteed to be !isPPC64 at this point. 3524 // Does MVT::i64 apply? 3525 VecArgOffset += 8; 3526 break; 3527 case MVT::v4f32: 3528 case MVT::v4i32: 3529 case MVT::v8i16: 3530 case MVT::v16i8: 3531 // Nothing to do, we're only looking at Nonvector args here. 3532 break; 3533 } 3534 } 3535 } 3536 // We've found where the vector parameter area in memory is. Skip the 3537 // first 12 parameters; these don't use that memory. 3538 VecArgOffset = ((VecArgOffset+15)/16)*16; 3539 VecArgOffset += 12*16; 3540 3541 // Add DAG nodes to load the arguments or copy them out of registers. On 3542 // entry to a function on PPC, the arguments start after the linkage area, 3543 // although the first ones are often in registers. 3544 3545 SmallVector<SDValue, 8> MemOps; 3546 unsigned nAltivecParamsAtEnd = 0; 3547 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin(); 3548 unsigned CurArgIdx = 0; 3549 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) { 3550 SDValue ArgVal; 3551 bool needsLoad = false; 3552 EVT ObjectVT = Ins[ArgNo].VT; 3553 unsigned ObjSize = ObjectVT.getSizeInBits()/8; 3554 unsigned ArgSize = ObjSize; 3555 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags; 3556 if (Ins[ArgNo].isOrigArg()) { 3557 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx); 3558 CurArgIdx = Ins[ArgNo].getOrigArgIndex(); 3559 } 3560 unsigned CurArgOffset = ArgOffset; 3561 3562 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary. 3563 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 || 3564 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) { 3565 if (isVarArg || isPPC64) { 3566 MinReservedArea = ((MinReservedArea+15)/16)*16; 3567 MinReservedArea += CalculateStackSlotSize(ObjectVT, 3568 Flags, 3569 PtrByteSize); 3570 } else nAltivecParamsAtEnd++; 3571 } else 3572 // Calculate min reserved area. 3573 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT, 3574 Flags, 3575 PtrByteSize); 3576 3577 // FIXME the codegen can be much improved in some cases. 3578 // We do not have to keep everything in memory. 3579 if (Flags.isByVal()) { 3580 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit"); 3581 3582 // ObjSize is the true size, ArgSize rounded up to multiple of registers. 3583 ObjSize = Flags.getByValSize(); 3584 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 3585 // Objects of size 1 and 2 are right justified, everything else is 3586 // left justified. This means the memory address is adjusted forwards. 3587 if (ObjSize==1 || ObjSize==2) { 3588 CurArgOffset = CurArgOffset + (4 - ObjSize); 3589 } 3590 // The value of the object is its address. 3591 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, false, true); 3592 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3593 InVals.push_back(FIN); 3594 if (ObjSize==1 || ObjSize==2) { 3595 if (GPR_idx != Num_GPR_Regs) { 3596 unsigned VReg; 3597 if (isPPC64) 3598 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); 3599 else 3600 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); 3601 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 3602 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16; 3603 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN, 3604 MachinePointerInfo(FuncArg), 3605 ObjType, false, false, 0); 3606 MemOps.push_back(Store); 3607 ++GPR_idx; 3608 } 3609 3610 ArgOffset += PtrByteSize; 3611 3612 continue; 3613 } 3614 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) { 3615 // Store whatever pieces of the object are in registers 3616 // to memory. ArgOffset will be the address of the beginning 3617 // of the object. 3618 if (GPR_idx != Num_GPR_Regs) { 3619 unsigned VReg; 3620 if (isPPC64) 3621 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); 3622 else 3623 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); 3624 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true); 3625 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3626 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 3627 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, 3628 MachinePointerInfo(FuncArg, j), 3629 false, false, 0); 3630 MemOps.push_back(Store); 3631 ++GPR_idx; 3632 ArgOffset += PtrByteSize; 3633 } else { 3634 ArgOffset += ArgSize - (ArgOffset-CurArgOffset); 3635 break; 3636 } 3637 } 3638 continue; 3639 } 3640 3641 switch (ObjectVT.getSimpleVT().SimpleTy) { 3642 default: llvm_unreachable("Unhandled argument type!"); 3643 case MVT::i1: 3644 case MVT::i32: 3645 if (!isPPC64) { 3646 if (GPR_idx != Num_GPR_Regs) { 3647 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); 3648 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 3649 3650 if (ObjectVT == MVT::i1) 3651 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal); 3652 3653 ++GPR_idx; 3654 } else { 3655 needsLoad = true; 3656 ArgSize = PtrByteSize; 3657 } 3658 // All int arguments reserve stack space in the Darwin ABI. 3659 ArgOffset += PtrByteSize; 3660 break; 3661 } 3662 // FALLTHROUGH 3663 case MVT::i64: // PPC64 3664 if (GPR_idx != Num_GPR_Regs) { 3665 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); 3666 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); 3667 3668 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1) 3669 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote 3670 // value to MVT::i64 and then truncate to the correct register size. 3671 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl); 3672 3673 ++GPR_idx; 3674 } else { 3675 needsLoad = true; 3676 ArgSize = PtrByteSize; 3677 } 3678 // All int arguments reserve stack space in the Darwin ABI. 3679 ArgOffset += 8; 3680 break; 3681 3682 case MVT::f32: 3683 case MVT::f64: 3684 // Every 4 bytes of argument space consumes one of the GPRs available for 3685 // argument passing. 3686 if (GPR_idx != Num_GPR_Regs) { 3687 ++GPR_idx; 3688 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64) 3689 ++GPR_idx; 3690 } 3691 if (FPR_idx != Num_FPR_Regs) { 3692 unsigned VReg; 3693 3694 if (ObjectVT == MVT::f32) 3695 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass); 3696 else 3697 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass); 3698 3699 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); 3700 ++FPR_idx; 3701 } else { 3702 needsLoad = true; 3703 } 3704 3705 // All FP arguments reserve stack space in the Darwin ABI. 3706 ArgOffset += isPPC64 ? 8 : ObjSize; 3707 break; 3708 case MVT::v4f32: 3709 case MVT::v4i32: 3710 case MVT::v8i16: 3711 case MVT::v16i8: 3712 // Note that vector arguments in registers don't reserve stack space, 3713 // except in varargs functions. 3714 if (VR_idx != Num_VR_Regs) { 3715 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass); 3716 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); 3717 if (isVarArg) { 3718 while ((ArgOffset % 16) != 0) { 3719 ArgOffset += PtrByteSize; 3720 if (GPR_idx != Num_GPR_Regs) 3721 GPR_idx++; 3722 } 3723 ArgOffset += 16; 3724 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64? 3725 } 3726 ++VR_idx; 3727 } else { 3728 if (!isVarArg && !isPPC64) { 3729 // Vectors go after all the nonvectors. 3730 CurArgOffset = VecArgOffset; 3731 VecArgOffset += 16; 3732 } else { 3733 // Vectors are aligned. 3734 ArgOffset = ((ArgOffset+15)/16)*16; 3735 CurArgOffset = ArgOffset; 3736 ArgOffset += 16; 3737 } 3738 needsLoad = true; 3739 } 3740 break; 3741 } 3742 3743 // We need to load the argument to a virtual register if we determined above 3744 // that we ran out of physical registers of the appropriate type. 3745 if (needsLoad) { 3746 int FI = MFI->CreateFixedObject(ObjSize, 3747 CurArgOffset + (ArgSize - ObjSize), 3748 isImmutable); 3749 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3750 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(), 3751 false, false, false, 0); 3752 } 3753 3754 InVals.push_back(ArgVal); 3755 } 3756 3757 // Allow for Altivec parameters at the end, if needed. 3758 if (nAltivecParamsAtEnd) { 3759 MinReservedArea = ((MinReservedArea+15)/16)*16; 3760 MinReservedArea += 16*nAltivecParamsAtEnd; 3761 } 3762 3763 // Area that is at least reserved in the caller of this function. 3764 MinReservedArea = std::max(MinReservedArea, LinkageSize + 8 * PtrByteSize); 3765 3766 // Set the size that is at least reserved in caller of this function. Tail 3767 // call optimized functions' reserved stack space needs to be aligned so that 3768 // taking the difference between two stack areas will result in an aligned 3769 // stack. 3770 MinReservedArea = 3771 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea); 3772 FuncInfo->setMinReservedArea(MinReservedArea); 3773 3774 // If the function takes variable number of arguments, make a frame index for 3775 // the start of the first vararg value... for expansion of llvm.va_start. 3776 if (isVarArg) { 3777 int Depth = ArgOffset; 3778 3779 FuncInfo->setVarArgsFrameIndex( 3780 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8, 3781 Depth, true)); 3782 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 3783 3784 // If this function is vararg, store any remaining integer argument regs 3785 // to their spots on the stack so that they may be loaded by deferencing the 3786 // result of va_next. 3787 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) { 3788 unsigned VReg; 3789 3790 if (isPPC64) 3791 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); 3792 else 3793 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); 3794 3795 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 3796 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, 3797 MachinePointerInfo(), false, false, 0); 3798 MemOps.push_back(Store); 3799 // Increment the address by four for the next argument to store 3800 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT); 3801 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); 3802 } 3803 } 3804 3805 if (!MemOps.empty()) 3806 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 3807 3808 return Chain; 3809 } 3810 3811 /// CalculateTailCallSPDiff - Get the amount the stack pointer has to be 3812 /// adjusted to accommodate the arguments for the tailcall. 3813 static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall, 3814 unsigned ParamSize) { 3815 3816 if (!isTailCall) return 0; 3817 3818 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>(); 3819 unsigned CallerMinReservedArea = FI->getMinReservedArea(); 3820 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize; 3821 // Remember only if the new adjustement is bigger. 3822 if (SPDiff < FI->getTailCallSPDelta()) 3823 FI->setTailCallSPDelta(SPDiff); 3824 3825 return SPDiff; 3826 } 3827 3828 /// IsEligibleForTailCallOptimization - Check whether the call is eligible 3829 /// for tail call optimization. Targets which want to do tail call 3830 /// optimization should implement this function. 3831 bool 3832 PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, 3833 CallingConv::ID CalleeCC, 3834 bool isVarArg, 3835 const SmallVectorImpl<ISD::InputArg> &Ins, 3836 SelectionDAG& DAG) const { 3837 if (!getTargetMachine().Options.GuaranteedTailCallOpt) 3838 return false; 3839 3840 // Variable argument functions are not supported. 3841 if (isVarArg) 3842 return false; 3843 3844 MachineFunction &MF = DAG.getMachineFunction(); 3845 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv(); 3846 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) { 3847 // Functions containing by val parameters are not supported. 3848 for (unsigned i = 0; i != Ins.size(); i++) { 3849 ISD::ArgFlagsTy Flags = Ins[i].Flags; 3850 if (Flags.isByVal()) return false; 3851 } 3852 3853 // Non-PIC/GOT tail calls are supported. 3854 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) 3855 return true; 3856 3857 // At the moment we can only do local tail calls (in same module, hidden 3858 // or protected) if we are generating PIC. 3859 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 3860 return G->getGlobal()->hasHiddenVisibility() 3861 || G->getGlobal()->hasProtectedVisibility(); 3862 } 3863 3864 return false; 3865 } 3866 3867 /// isCallCompatibleAddress - Return the immediate to use if the specified 3868 /// 32-bit value is representable in the immediate field of a BxA instruction. 3869 static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) { 3870 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 3871 if (!C) return nullptr; 3872 3873 int Addr = C->getZExtValue(); 3874 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero. 3875 SignExtend32<26>(Addr) != Addr) 3876 return nullptr; // Top 6 bits have to be sext of immediate. 3877 3878 return DAG.getConstant((int)C->getZExtValue() >> 2, SDLoc(Op), 3879 DAG.getTargetLoweringInfo().getPointerTy( 3880 DAG.getDataLayout())).getNode(); 3881 } 3882 3883 namespace { 3884 3885 struct TailCallArgumentInfo { 3886 SDValue Arg; 3887 SDValue FrameIdxOp; 3888 int FrameIdx; 3889 3890 TailCallArgumentInfo() : FrameIdx(0) {} 3891 }; 3892 3893 } 3894 3895 /// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot. 3896 static void 3897 StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG, 3898 SDValue Chain, 3899 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs, 3900 SmallVectorImpl<SDValue> &MemOpChains, 3901 SDLoc dl) { 3902 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) { 3903 SDValue Arg = TailCallArgs[i].Arg; 3904 SDValue FIN = TailCallArgs[i].FrameIdxOp; 3905 int FI = TailCallArgs[i].FrameIdx; 3906 // Store relative to framepointer. 3907 MemOpChains.push_back(DAG.getStore( 3908 Chain, dl, Arg, FIN, 3909 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false, 3910 false, 0)); 3911 } 3912 } 3913 3914 /// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to 3915 /// the appropriate stack slot for the tail call optimized function call. 3916 static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG, 3917 MachineFunction &MF, 3918 SDValue Chain, 3919 SDValue OldRetAddr, 3920 SDValue OldFP, 3921 int SPDiff, 3922 bool isPPC64, 3923 bool isDarwinABI, 3924 SDLoc dl) { 3925 if (SPDiff) { 3926 // Calculate the new stack slot for the return address. 3927 int SlotSize = isPPC64 ? 8 : 4; 3928 const PPCFrameLowering *FL = 3929 MF.getSubtarget<PPCSubtarget>().getFrameLowering(); 3930 int NewRetAddrLoc = SPDiff + FL->getReturnSaveOffset(); 3931 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize, 3932 NewRetAddrLoc, true); 3933 EVT VT = isPPC64 ? MVT::i64 : MVT::i32; 3934 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT); 3935 Chain = DAG.getStore( 3936 Chain, dl, OldRetAddr, NewRetAddrFrIdx, 3937 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), NewRetAddr), 3938 false, false, 0); 3939 3940 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack 3941 // slot as the FP is never overwritten. 3942 if (isDarwinABI) { 3943 int NewFPLoc = SPDiff + FL->getFramePointerSaveOffset(); 3944 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc, 3945 true); 3946 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT); 3947 Chain = DAG.getStore( 3948 Chain, dl, OldFP, NewFramePtrIdx, 3949 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), NewFPIdx), 3950 false, false, 0); 3951 } 3952 } 3953 return Chain; 3954 } 3955 3956 /// CalculateTailCallArgDest - Remember Argument for later processing. Calculate 3957 /// the position of the argument. 3958 static void 3959 CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64, 3960 SDValue Arg, int SPDiff, unsigned ArgOffset, 3961 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) { 3962 int Offset = ArgOffset + SPDiff; 3963 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8; 3964 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true); 3965 EVT VT = isPPC64 ? MVT::i64 : MVT::i32; 3966 SDValue FIN = DAG.getFrameIndex(FI, VT); 3967 TailCallArgumentInfo Info; 3968 Info.Arg = Arg; 3969 Info.FrameIdxOp = FIN; 3970 Info.FrameIdx = FI; 3971 TailCallArguments.push_back(Info); 3972 } 3973 3974 /// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address 3975 /// stack slot. Returns the chain as result and the loaded frame pointers in 3976 /// LROpOut/FPOpout. Used when tail calling. 3977 SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG, 3978 int SPDiff, 3979 SDValue Chain, 3980 SDValue &LROpOut, 3981 SDValue &FPOpOut, 3982 bool isDarwinABI, 3983 SDLoc dl) const { 3984 if (SPDiff) { 3985 // Load the LR and FP stack slot for later adjusting. 3986 EVT VT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32; 3987 LROpOut = getReturnAddrFrameIndex(DAG); 3988 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(), 3989 false, false, false, 0); 3990 Chain = SDValue(LROpOut.getNode(), 1); 3991 3992 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack 3993 // slot as the FP is never overwritten. 3994 if (isDarwinABI) { 3995 FPOpOut = getFramePointerFrameIndex(DAG); 3996 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(), 3997 false, false, false, 0); 3998 Chain = SDValue(FPOpOut.getNode(), 1); 3999 } 4000 } 4001 return Chain; 4002 } 4003 4004 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified 4005 /// by "Src" to address "Dst" of size "Size". Alignment information is 4006 /// specified by the specific parameter attribute. The copy will be passed as 4007 /// a byval function parameter. 4008 /// Sometimes what we are copying is the end of a larger object, the part that 4009 /// does not fit in registers. 4010 static SDValue 4011 CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain, 4012 ISD::ArgFlagsTy Flags, SelectionDAG &DAG, 4013 SDLoc dl) { 4014 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32); 4015 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(), 4016 false, false, false, MachinePointerInfo(), 4017 MachinePointerInfo()); 4018 } 4019 4020 /// LowerMemOpCallTo - Store the argument to the stack or remember it in case of 4021 /// tail calls. 4022 static void 4023 LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain, 4024 SDValue Arg, SDValue PtrOff, int SPDiff, 4025 unsigned ArgOffset, bool isPPC64, bool isTailCall, 4026 bool isVector, SmallVectorImpl<SDValue> &MemOpChains, 4027 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments, 4028 SDLoc dl) { 4029 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 4030 if (!isTailCall) { 4031 if (isVector) { 4032 SDValue StackPtr; 4033 if (isPPC64) 4034 StackPtr = DAG.getRegister(PPC::X1, MVT::i64); 4035 else 4036 StackPtr = DAG.getRegister(PPC::R1, MVT::i32); 4037 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, 4038 DAG.getConstant(ArgOffset, dl, PtrVT)); 4039 } 4040 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, 4041 MachinePointerInfo(), false, false, 0)); 4042 // Calculate and remember argument location. 4043 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset, 4044 TailCallArguments); 4045 } 4046 4047 static 4048 void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain, 4049 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes, 4050 SDValue LROp, SDValue FPOp, bool isDarwinABI, 4051 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) { 4052 MachineFunction &MF = DAG.getMachineFunction(); 4053 4054 // Emit a sequence of copyto/copyfrom virtual registers for arguments that 4055 // might overwrite each other in case of tail call optimization. 4056 SmallVector<SDValue, 8> MemOpChains2; 4057 // Do not flag preceding copytoreg stuff together with the following stuff. 4058 InFlag = SDValue(); 4059 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments, 4060 MemOpChains2, dl); 4061 if (!MemOpChains2.empty()) 4062 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2); 4063 4064 // Store the return address to the appropriate stack slot. 4065 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff, 4066 isPPC64, isDarwinABI, dl); 4067 4068 // Emit callseq_end just before tailcall node. 4069 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), 4070 DAG.getIntPtrConstant(0, dl, true), InFlag, dl); 4071 InFlag = Chain.getValue(1); 4072 } 4073 4074 // Is this global address that of a function that can be called by name? (as 4075 // opposed to something that must hold a descriptor for an indirect call). 4076 static bool isFunctionGlobalAddress(SDValue Callee) { 4077 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 4078 if (Callee.getOpcode() == ISD::GlobalTLSAddress || 4079 Callee.getOpcode() == ISD::TargetGlobalTLSAddress) 4080 return false; 4081 4082 return G->getGlobal()->getType()->getElementType()->isFunctionTy(); 4083 } 4084 4085 return false; 4086 } 4087 4088 static 4089 unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag, 4090 SDValue &Chain, SDValue CallSeqStart, SDLoc dl, int SPDiff, 4091 bool isTailCall, bool IsPatchPoint, bool hasNest, 4092 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass, 4093 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys, 4094 ImmutableCallSite *CS, const PPCSubtarget &Subtarget) { 4095 4096 bool isPPC64 = Subtarget.isPPC64(); 4097 bool isSVR4ABI = Subtarget.isSVR4ABI(); 4098 bool isELFv2ABI = Subtarget.isELFv2ABI(); 4099 4100 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 4101 NodeTys.push_back(MVT::Other); // Returns a chain 4102 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use. 4103 4104 unsigned CallOpc = PPCISD::CALL; 4105 4106 bool needIndirectCall = true; 4107 if (!isSVR4ABI || !isPPC64) 4108 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) { 4109 // If this is an absolute destination address, use the munged value. 4110 Callee = SDValue(Dest, 0); 4111 needIndirectCall = false; 4112 } 4113 4114 if (isFunctionGlobalAddress(Callee)) { 4115 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Callee); 4116 // A call to a TLS address is actually an indirect call to a 4117 // thread-specific pointer. 4118 unsigned OpFlags = 0; 4119 if ((DAG.getTarget().getRelocationModel() != Reloc::Static && 4120 (Subtarget.getTargetTriple().isMacOSX() && 4121 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5)) && 4122 !G->getGlobal()->isStrongDefinitionForLinker()) || 4123 (Subtarget.isTargetELF() && !isPPC64 && 4124 !G->getGlobal()->hasLocalLinkage() && 4125 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) { 4126 // PC-relative references to external symbols should go through $stub, 4127 // unless we're building with the leopard linker or later, which 4128 // automatically synthesizes these stubs. 4129 OpFlags = PPCII::MO_PLT_OR_STUB; 4130 } 4131 4132 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, 4133 // every direct call is) turn it into a TargetGlobalAddress / 4134 // TargetExternalSymbol node so that legalize doesn't hack it. 4135 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, 4136 Callee.getValueType(), 0, OpFlags); 4137 needIndirectCall = false; 4138 } 4139 4140 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 4141 unsigned char OpFlags = 0; 4142 4143 if ((DAG.getTarget().getRelocationModel() != Reloc::Static && 4144 (Subtarget.getTargetTriple().isMacOSX() && 4145 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5))) || 4146 (Subtarget.isTargetELF() && !isPPC64 && 4147 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) { 4148 // PC-relative references to external symbols should go through $stub, 4149 // unless we're building with the leopard linker or later, which 4150 // automatically synthesizes these stubs. 4151 OpFlags = PPCII::MO_PLT_OR_STUB; 4152 } 4153 4154 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(), 4155 OpFlags); 4156 needIndirectCall = false; 4157 } 4158 4159 if (IsPatchPoint) { 4160 // We'll form an invalid direct call when lowering a patchpoint; the full 4161 // sequence for an indirect call is complicated, and many of the 4162 // instructions introduced might have side effects (and, thus, can't be 4163 // removed later). The call itself will be removed as soon as the 4164 // argument/return lowering is complete, so the fact that it has the wrong 4165 // kind of operands should not really matter. 4166 needIndirectCall = false; 4167 } 4168 4169 if (needIndirectCall) { 4170 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair 4171 // to do the call, we can't use PPCISD::CALL. 4172 SDValue MTCTROps[] = {Chain, Callee, InFlag}; 4173 4174 if (isSVR4ABI && isPPC64 && !isELFv2ABI) { 4175 // Function pointers in the 64-bit SVR4 ABI do not point to the function 4176 // entry point, but to the function descriptor (the function entry point 4177 // address is part of the function descriptor though). 4178 // The function descriptor is a three doubleword structure with the 4179 // following fields: function entry point, TOC base address and 4180 // environment pointer. 4181 // Thus for a call through a function pointer, the following actions need 4182 // to be performed: 4183 // 1. Save the TOC of the caller in the TOC save area of its stack 4184 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()). 4185 // 2. Load the address of the function entry point from the function 4186 // descriptor. 4187 // 3. Load the TOC of the callee from the function descriptor into r2. 4188 // 4. Load the environment pointer from the function descriptor into 4189 // r11. 4190 // 5. Branch to the function entry point address. 4191 // 6. On return of the callee, the TOC of the caller needs to be 4192 // restored (this is done in FinishCall()). 4193 // 4194 // The loads are scheduled at the beginning of the call sequence, and the 4195 // register copies are flagged together to ensure that no other 4196 // operations can be scheduled in between. E.g. without flagging the 4197 // copies together, a TOC access in the caller could be scheduled between 4198 // the assignment of the callee TOC and the branch to the callee, which 4199 // results in the TOC access going through the TOC of the callee instead 4200 // of going through the TOC of the caller, which leads to incorrect code. 4201 4202 // Load the address of the function entry point from the function 4203 // descriptor. 4204 SDValue LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-1); 4205 if (LDChain.getValueType() == MVT::Glue) 4206 LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-2); 4207 4208 bool LoadsInv = Subtarget.hasInvariantFunctionDescriptors(); 4209 4210 MachinePointerInfo MPI(CS ? CS->getCalledValue() : nullptr); 4211 SDValue LoadFuncPtr = DAG.getLoad(MVT::i64, dl, LDChain, Callee, MPI, 4212 false, false, LoadsInv, 8); 4213 4214 // Load environment pointer into r11. 4215 SDValue PtrOff = DAG.getIntPtrConstant(16, dl); 4216 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff); 4217 SDValue LoadEnvPtr = DAG.getLoad(MVT::i64, dl, LDChain, AddPtr, 4218 MPI.getWithOffset(16), false, false, 4219 LoadsInv, 8); 4220 4221 SDValue TOCOff = DAG.getIntPtrConstant(8, dl); 4222 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, TOCOff); 4223 SDValue TOCPtr = DAG.getLoad(MVT::i64, dl, LDChain, AddTOC, 4224 MPI.getWithOffset(8), false, false, 4225 LoadsInv, 8); 4226 4227 setUsesTOCBasePtr(DAG); 4228 SDValue TOCVal = DAG.getCopyToReg(Chain, dl, PPC::X2, TOCPtr, 4229 InFlag); 4230 Chain = TOCVal.getValue(0); 4231 InFlag = TOCVal.getValue(1); 4232 4233 // If the function call has an explicit 'nest' parameter, it takes the 4234 // place of the environment pointer. 4235 if (!hasNest) { 4236 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr, 4237 InFlag); 4238 4239 Chain = EnvVal.getValue(0); 4240 InFlag = EnvVal.getValue(1); 4241 } 4242 4243 MTCTROps[0] = Chain; 4244 MTCTROps[1] = LoadFuncPtr; 4245 MTCTROps[2] = InFlag; 4246 } 4247 4248 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, 4249 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2)); 4250 InFlag = Chain.getValue(1); 4251 4252 NodeTys.clear(); 4253 NodeTys.push_back(MVT::Other); 4254 NodeTys.push_back(MVT::Glue); 4255 Ops.push_back(Chain); 4256 CallOpc = PPCISD::BCTRL; 4257 Callee.setNode(nullptr); 4258 // Add use of X11 (holding environment pointer) 4259 if (isSVR4ABI && isPPC64 && !isELFv2ABI && !hasNest) 4260 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT)); 4261 // Add CTR register as callee so a bctr can be emitted later. 4262 if (isTailCall) 4263 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT)); 4264 } 4265 4266 // If this is a direct call, pass the chain and the callee. 4267 if (Callee.getNode()) { 4268 Ops.push_back(Chain); 4269 Ops.push_back(Callee); 4270 } 4271 // If this is a tail call add stack pointer delta. 4272 if (isTailCall) 4273 Ops.push_back(DAG.getConstant(SPDiff, dl, MVT::i32)); 4274 4275 // Add argument registers to the end of the list so that they are known live 4276 // into the call. 4277 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 4278 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 4279 RegsToPass[i].second.getValueType())); 4280 4281 // All calls, in both the ELF V1 and V2 ABIs, need the TOC register live 4282 // into the call. 4283 if (isSVR4ABI && isPPC64 && !IsPatchPoint) { 4284 setUsesTOCBasePtr(DAG); 4285 Ops.push_back(DAG.getRegister(PPC::X2, PtrVT)); 4286 } 4287 4288 return CallOpc; 4289 } 4290 4291 static 4292 bool isLocalCall(const SDValue &Callee) 4293 { 4294 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 4295 return G->getGlobal()->isStrongDefinitionForLinker(); 4296 return false; 4297 } 4298 4299 SDValue 4300 PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 4301 CallingConv::ID CallConv, bool isVarArg, 4302 const SmallVectorImpl<ISD::InputArg> &Ins, 4303 SDLoc dl, SelectionDAG &DAG, 4304 SmallVectorImpl<SDValue> &InVals) const { 4305 4306 SmallVector<CCValAssign, 16> RVLocs; 4307 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 4308 *DAG.getContext()); 4309 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC); 4310 4311 // Copy all of the result registers out of their specified physreg. 4312 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { 4313 CCValAssign &VA = RVLocs[i]; 4314 assert(VA.isRegLoc() && "Can only return in registers!"); 4315 4316 SDValue Val = DAG.getCopyFromReg(Chain, dl, 4317 VA.getLocReg(), VA.getLocVT(), InFlag); 4318 Chain = Val.getValue(1); 4319 InFlag = Val.getValue(2); 4320 4321 switch (VA.getLocInfo()) { 4322 default: llvm_unreachable("Unknown loc info!"); 4323 case CCValAssign::Full: break; 4324 case CCValAssign::AExt: 4325 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); 4326 break; 4327 case CCValAssign::ZExt: 4328 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val, 4329 DAG.getValueType(VA.getValVT())); 4330 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); 4331 break; 4332 case CCValAssign::SExt: 4333 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val, 4334 DAG.getValueType(VA.getValVT())); 4335 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); 4336 break; 4337 } 4338 4339 InVals.push_back(Val); 4340 } 4341 4342 return Chain; 4343 } 4344 4345 SDValue 4346 PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl, 4347 bool isTailCall, bool isVarArg, bool IsPatchPoint, 4348 bool hasNest, SelectionDAG &DAG, 4349 SmallVector<std::pair<unsigned, SDValue>, 8> 4350 &RegsToPass, 4351 SDValue InFlag, SDValue Chain, 4352 SDValue CallSeqStart, SDValue &Callee, 4353 int SPDiff, unsigned NumBytes, 4354 const SmallVectorImpl<ISD::InputArg> &Ins, 4355 SmallVectorImpl<SDValue> &InVals, 4356 ImmutableCallSite *CS) const { 4357 4358 std::vector<EVT> NodeTys; 4359 SmallVector<SDValue, 8> Ops; 4360 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, CallSeqStart, dl, 4361 SPDiff, isTailCall, IsPatchPoint, hasNest, 4362 RegsToPass, Ops, NodeTys, CS, Subtarget); 4363 4364 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls 4365 if (isVarArg && Subtarget.isSVR4ABI() && !Subtarget.isPPC64()) 4366 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32)); 4367 4368 // When performing tail call optimization the callee pops its arguments off 4369 // the stack. Account for this here so these bytes can be pushed back on in 4370 // PPCFrameLowering::eliminateCallFramePseudoInstr. 4371 int BytesCalleePops = 4372 (CallConv == CallingConv::Fast && 4373 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0; 4374 4375 // Add a register mask operand representing the call-preserved registers. 4376 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); 4377 const uint32_t *Mask = 4378 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv); 4379 assert(Mask && "Missing call preserved mask for calling convention"); 4380 Ops.push_back(DAG.getRegisterMask(Mask)); 4381 4382 if (InFlag.getNode()) 4383 Ops.push_back(InFlag); 4384 4385 // Emit tail call. 4386 if (isTailCall) { 4387 assert(((Callee.getOpcode() == ISD::Register && 4388 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) || 4389 Callee.getOpcode() == ISD::TargetExternalSymbol || 4390 Callee.getOpcode() == ISD::TargetGlobalAddress || 4391 isa<ConstantSDNode>(Callee)) && 4392 "Expecting an global address, external symbol, absolute value or register"); 4393 4394 DAG.getMachineFunction().getFrameInfo()->setHasTailCall(); 4395 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops); 4396 } 4397 4398 // Add a NOP immediately after the branch instruction when using the 64-bit 4399 // SVR4 ABI. At link time, if caller and callee are in a different module and 4400 // thus have a different TOC, the call will be replaced with a call to a stub 4401 // function which saves the current TOC, loads the TOC of the callee and 4402 // branches to the callee. The NOP will be replaced with a load instruction 4403 // which restores the TOC of the caller from the TOC save slot of the current 4404 // stack frame. If caller and callee belong to the same module (and have the 4405 // same TOC), the NOP will remain unchanged. 4406 4407 if (!isTailCall && Subtarget.isSVR4ABI()&& Subtarget.isPPC64() && 4408 !IsPatchPoint) { 4409 if (CallOpc == PPCISD::BCTRL) { 4410 // This is a call through a function pointer. 4411 // Restore the caller TOC from the save area into R2. 4412 // See PrepareCall() for more information about calls through function 4413 // pointers in the 64-bit SVR4 ABI. 4414 // We are using a target-specific load with r2 hard coded, because the 4415 // result of a target-independent load would never go directly into r2, 4416 // since r2 is a reserved register (which prevents the register allocator 4417 // from allocating it), resulting in an additional register being 4418 // allocated and an unnecessary move instruction being generated. 4419 CallOpc = PPCISD::BCTRL_LOAD_TOC; 4420 4421 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 4422 SDValue StackPtr = DAG.getRegister(PPC::X1, PtrVT); 4423 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset(); 4424 SDValue TOCOff = DAG.getIntPtrConstant(TOCSaveOffset, dl); 4425 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, StackPtr, TOCOff); 4426 4427 // The address needs to go after the chain input but before the flag (or 4428 // any other variadic arguments). 4429 Ops.insert(std::next(Ops.begin()), AddTOC); 4430 } else if ((CallOpc == PPCISD::CALL) && 4431 (!isLocalCall(Callee) || 4432 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) 4433 // Otherwise insert NOP for non-local calls. 4434 CallOpc = PPCISD::CALL_NOP; 4435 } 4436 4437 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops); 4438 InFlag = Chain.getValue(1); 4439 4440 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), 4441 DAG.getIntPtrConstant(BytesCalleePops, dl, true), 4442 InFlag, dl); 4443 if (!Ins.empty()) 4444 InFlag = Chain.getValue(1); 4445 4446 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, 4447 Ins, dl, DAG, InVals); 4448 } 4449 4450 SDValue 4451 PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 4452 SmallVectorImpl<SDValue> &InVals) const { 4453 SelectionDAG &DAG = CLI.DAG; 4454 SDLoc &dl = CLI.DL; 4455 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 4456 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 4457 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 4458 SDValue Chain = CLI.Chain; 4459 SDValue Callee = CLI.Callee; 4460 bool &isTailCall = CLI.IsTailCall; 4461 CallingConv::ID CallConv = CLI.CallConv; 4462 bool isVarArg = CLI.IsVarArg; 4463 bool IsPatchPoint = CLI.IsPatchPoint; 4464 ImmutableCallSite *CS = CLI.CS; 4465 4466 if (isTailCall) 4467 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, 4468 Ins, DAG); 4469 4470 if (!isTailCall && CS && CS->isMustTailCall()) 4471 report_fatal_error("failed to perform tail call elimination on a call " 4472 "site marked musttail"); 4473 4474 if (Subtarget.isSVR4ABI()) { 4475 if (Subtarget.isPPC64()) 4476 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg, 4477 isTailCall, IsPatchPoint, Outs, OutVals, Ins, 4478 dl, DAG, InVals, CS); 4479 else 4480 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg, 4481 isTailCall, IsPatchPoint, Outs, OutVals, Ins, 4482 dl, DAG, InVals, CS); 4483 } 4484 4485 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg, 4486 isTailCall, IsPatchPoint, Outs, OutVals, Ins, 4487 dl, DAG, InVals, CS); 4488 } 4489 4490 SDValue 4491 PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee, 4492 CallingConv::ID CallConv, bool isVarArg, 4493 bool isTailCall, bool IsPatchPoint, 4494 const SmallVectorImpl<ISD::OutputArg> &Outs, 4495 const SmallVectorImpl<SDValue> &OutVals, 4496 const SmallVectorImpl<ISD::InputArg> &Ins, 4497 SDLoc dl, SelectionDAG &DAG, 4498 SmallVectorImpl<SDValue> &InVals, 4499 ImmutableCallSite *CS) const { 4500 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description 4501 // of the 32-bit SVR4 ABI stack frame layout. 4502 4503 assert((CallConv == CallingConv::C || 4504 CallConv == CallingConv::Fast) && "Unknown calling convention!"); 4505 4506 unsigned PtrByteSize = 4; 4507 4508 MachineFunction &MF = DAG.getMachineFunction(); 4509 4510 // Mark this function as potentially containing a function that contains a 4511 // tail call. As a consequence the frame pointer will be used for dynamicalloc 4512 // and restoring the callers stack pointer in this functions epilog. This is 4513 // done because by tail calling the called function might overwrite the value 4514 // in this function's (MF) stack pointer stack slot 0(SP). 4515 if (getTargetMachine().Options.GuaranteedTailCallOpt && 4516 CallConv == CallingConv::Fast) 4517 MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); 4518 4519 // Count how many bytes are to be pushed on the stack, including the linkage 4520 // area, parameter list area and the part of the local variable space which 4521 // contains copies of aggregates which are passed by value. 4522 4523 // Assign locations to all of the outgoing arguments. 4524 SmallVector<CCValAssign, 16> ArgLocs; 4525 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 4526 *DAG.getContext()); 4527 4528 // Reserve space for the linkage area on the stack. 4529 CCInfo.AllocateStack(Subtarget.getFrameLowering()->getLinkageSize(), 4530 PtrByteSize); 4531 4532 if (isVarArg) { 4533 // Handle fixed and variable vector arguments differently. 4534 // Fixed vector arguments go into registers as long as registers are 4535 // available. Variable vector arguments always go into memory. 4536 unsigned NumArgs = Outs.size(); 4537 4538 for (unsigned i = 0; i != NumArgs; ++i) { 4539 MVT ArgVT = Outs[i].VT; 4540 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; 4541 bool Result; 4542 4543 if (Outs[i].IsFixed) { 4544 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, 4545 CCInfo); 4546 } else { 4547 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, 4548 ArgFlags, CCInfo); 4549 } 4550 4551 if (Result) { 4552 #ifndef NDEBUG 4553 errs() << "Call operand #" << i << " has unhandled type " 4554 << EVT(ArgVT).getEVTString() << "\n"; 4555 #endif 4556 llvm_unreachable(nullptr); 4557 } 4558 } 4559 } else { 4560 // All arguments are treated the same. 4561 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4); 4562 } 4563 4564 // Assign locations to all of the outgoing aggregate by value arguments. 4565 SmallVector<CCValAssign, 16> ByValArgLocs; 4566 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(), 4567 ByValArgLocs, *DAG.getContext()); 4568 4569 // Reserve stack space for the allocations in CCInfo. 4570 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize); 4571 4572 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal); 4573 4574 // Size of the linkage area, parameter list area and the part of the local 4575 // space variable where copies of aggregates which are passed by value are 4576 // stored. 4577 unsigned NumBytes = CCByValInfo.getNextStackOffset(); 4578 4579 // Calculate by how many bytes the stack has to be adjusted in case of tail 4580 // call optimization. 4581 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); 4582 4583 // Adjust the stack pointer for the new arguments... 4584 // These operations are automatically eliminated by the prolog/epilog pass 4585 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), 4586 dl); 4587 SDValue CallSeqStart = Chain; 4588 4589 // Load the return address and frame pointer so it can be moved somewhere else 4590 // later. 4591 SDValue LROp, FPOp; 4592 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false, 4593 dl); 4594 4595 // Set up a copy of the stack pointer for use loading and storing any 4596 // arguments that may not fit in the registers available for argument 4597 // passing. 4598 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32); 4599 4600 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 4601 SmallVector<TailCallArgumentInfo, 8> TailCallArguments; 4602 SmallVector<SDValue, 8> MemOpChains; 4603 4604 bool seenFloatArg = false; 4605 // Walk the register/memloc assignments, inserting copies/loads. 4606 for (unsigned i = 0, j = 0, e = ArgLocs.size(); 4607 i != e; 4608 ++i) { 4609 CCValAssign &VA = ArgLocs[i]; 4610 SDValue Arg = OutVals[i]; 4611 ISD::ArgFlagsTy Flags = Outs[i].Flags; 4612 4613 if (Flags.isByVal()) { 4614 // Argument is an aggregate which is passed by value, thus we need to 4615 // create a copy of it in the local variable space of the current stack 4616 // frame (which is the stack frame of the caller) and pass the address of 4617 // this copy to the callee. 4618 assert((j < ByValArgLocs.size()) && "Index out of bounds!"); 4619 CCValAssign &ByValVA = ByValArgLocs[j++]; 4620 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!"); 4621 4622 // Memory reserved in the local variable space of the callers stack frame. 4623 unsigned LocMemOffset = ByValVA.getLocMemOffset(); 4624 4625 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 4626 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()), 4627 StackPtr, PtrOff); 4628 4629 // Create a copy of the argument in the local area of the current 4630 // stack frame. 4631 SDValue MemcpyCall = 4632 CreateCopyOfByValArgument(Arg, PtrOff, 4633 CallSeqStart.getNode()->getOperand(0), 4634 Flags, DAG, dl); 4635 4636 // This must go outside the CALLSEQ_START..END. 4637 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, 4638 CallSeqStart.getNode()->getOperand(1), 4639 SDLoc(MemcpyCall)); 4640 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), 4641 NewCallSeqStart.getNode()); 4642 Chain = CallSeqStart = NewCallSeqStart; 4643 4644 // Pass the address of the aggregate copy on the stack either in a 4645 // physical register or in the parameter list area of the current stack 4646 // frame to the callee. 4647 Arg = PtrOff; 4648 } 4649 4650 if (VA.isRegLoc()) { 4651 if (Arg.getValueType() == MVT::i1) 4652 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg); 4653 4654 seenFloatArg |= VA.getLocVT().isFloatingPoint(); 4655 // Put argument in a physical register. 4656 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 4657 } else { 4658 // Put argument in the parameter list area of the current stack frame. 4659 assert(VA.isMemLoc()); 4660 unsigned LocMemOffset = VA.getLocMemOffset(); 4661 4662 if (!isTailCall) { 4663 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 4664 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()), 4665 StackPtr, PtrOff); 4666 4667 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, 4668 MachinePointerInfo(), 4669 false, false, 0)); 4670 } else { 4671 // Calculate and remember argument location. 4672 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset, 4673 TailCallArguments); 4674 } 4675 } 4676 } 4677 4678 if (!MemOpChains.empty()) 4679 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 4680 4681 // Build a sequence of copy-to-reg nodes chained together with token chain 4682 // and flag operands which copy the outgoing args into the appropriate regs. 4683 SDValue InFlag; 4684 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 4685 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 4686 RegsToPass[i].second, InFlag); 4687 InFlag = Chain.getValue(1); 4688 } 4689 4690 // Set CR bit 6 to true if this is a vararg call with floating args passed in 4691 // registers. 4692 if (isVarArg) { 4693 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); 4694 SDValue Ops[] = { Chain, InFlag }; 4695 4696 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET, 4697 dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1)); 4698 4699 InFlag = Chain.getValue(1); 4700 } 4701 4702 if (isTailCall) 4703 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp, 4704 false, TailCallArguments); 4705 4706 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint, 4707 /* unused except on PPC64 ELFv1 */ false, DAG, 4708 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff, 4709 NumBytes, Ins, InVals, CS); 4710 } 4711 4712 // Copy an argument into memory, being careful to do this outside the 4713 // call sequence for the call to which the argument belongs. 4714 SDValue 4715 PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff, 4716 SDValue CallSeqStart, 4717 ISD::ArgFlagsTy Flags, 4718 SelectionDAG &DAG, 4719 SDLoc dl) const { 4720 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff, 4721 CallSeqStart.getNode()->getOperand(0), 4722 Flags, DAG, dl); 4723 // The MEMCPY must go outside the CALLSEQ_START..END. 4724 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, 4725 CallSeqStart.getNode()->getOperand(1), 4726 SDLoc(MemcpyCall)); 4727 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), 4728 NewCallSeqStart.getNode()); 4729 return NewCallSeqStart; 4730 } 4731 4732 SDValue 4733 PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee, 4734 CallingConv::ID CallConv, bool isVarArg, 4735 bool isTailCall, bool IsPatchPoint, 4736 const SmallVectorImpl<ISD::OutputArg> &Outs, 4737 const SmallVectorImpl<SDValue> &OutVals, 4738 const SmallVectorImpl<ISD::InputArg> &Ins, 4739 SDLoc dl, SelectionDAG &DAG, 4740 SmallVectorImpl<SDValue> &InVals, 4741 ImmutableCallSite *CS) const { 4742 4743 bool isELFv2ABI = Subtarget.isELFv2ABI(); 4744 bool isLittleEndian = Subtarget.isLittleEndian(); 4745 unsigned NumOps = Outs.size(); 4746 bool hasNest = false; 4747 4748 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 4749 unsigned PtrByteSize = 8; 4750 4751 MachineFunction &MF = DAG.getMachineFunction(); 4752 4753 // Mark this function as potentially containing a function that contains a 4754 // tail call. As a consequence the frame pointer will be used for dynamicalloc 4755 // and restoring the callers stack pointer in this functions epilog. This is 4756 // done because by tail calling the called function might overwrite the value 4757 // in this function's (MF) stack pointer stack slot 0(SP). 4758 if (getTargetMachine().Options.GuaranteedTailCallOpt && 4759 CallConv == CallingConv::Fast) 4760 MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); 4761 4762 assert(!(CallConv == CallingConv::Fast && isVarArg) && 4763 "fastcc not supported on varargs functions"); 4764 4765 // Count how many bytes are to be pushed on the stack, including the linkage 4766 // area, and parameter passing area. On ELFv1, the linkage area is 48 bytes 4767 // reserved space for [SP][CR][LR][2 x unused][TOC]; on ELFv2, the linkage 4768 // area is 32 bytes reserved space for [SP][CR][LR][TOC]. 4769 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); 4770 unsigned NumBytes = LinkageSize; 4771 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; 4772 unsigned &QFPR_idx = FPR_idx; 4773 4774 static const MCPhysReg GPR[] = { 4775 PPC::X3, PPC::X4, PPC::X5, PPC::X6, 4776 PPC::X7, PPC::X8, PPC::X9, PPC::X10, 4777 }; 4778 static const MCPhysReg VR[] = { 4779 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, 4780 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 4781 }; 4782 static const MCPhysReg VSRH[] = { 4783 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8, 4784 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13 4785 }; 4786 4787 const unsigned NumGPRs = array_lengthof(GPR); 4788 const unsigned NumFPRs = 13; 4789 const unsigned NumVRs = array_lengthof(VR); 4790 const unsigned NumQFPRs = NumFPRs; 4791 4792 // When using the fast calling convention, we don't provide backing for 4793 // arguments that will be in registers. 4794 unsigned NumGPRsUsed = 0, NumFPRsUsed = 0, NumVRsUsed = 0; 4795 4796 // Add up all the space actually used. 4797 for (unsigned i = 0; i != NumOps; ++i) { 4798 ISD::ArgFlagsTy Flags = Outs[i].Flags; 4799 EVT ArgVT = Outs[i].VT; 4800 EVT OrigVT = Outs[i].ArgVT; 4801 4802 if (Flags.isNest()) 4803 continue; 4804 4805 if (CallConv == CallingConv::Fast) { 4806 if (Flags.isByVal()) 4807 NumGPRsUsed += (Flags.getByValSize()+7)/8; 4808 else 4809 switch (ArgVT.getSimpleVT().SimpleTy) { 4810 default: llvm_unreachable("Unexpected ValueType for argument!"); 4811 case MVT::i1: 4812 case MVT::i32: 4813 case MVT::i64: 4814 if (++NumGPRsUsed <= NumGPRs) 4815 continue; 4816 break; 4817 case MVT::v4i32: 4818 case MVT::v8i16: 4819 case MVT::v16i8: 4820 case MVT::v2f64: 4821 case MVT::v2i64: 4822 case MVT::v1i128: 4823 if (++NumVRsUsed <= NumVRs) 4824 continue; 4825 break; 4826 case MVT::v4f32: 4827 // When using QPX, this is handled like a FP register, otherwise, it 4828 // is an Altivec register. 4829 if (Subtarget.hasQPX()) { 4830 if (++NumFPRsUsed <= NumFPRs) 4831 continue; 4832 } else { 4833 if (++NumVRsUsed <= NumVRs) 4834 continue; 4835 } 4836 break; 4837 case MVT::f32: 4838 case MVT::f64: 4839 case MVT::v4f64: // QPX 4840 case MVT::v4i1: // QPX 4841 if (++NumFPRsUsed <= NumFPRs) 4842 continue; 4843 break; 4844 } 4845 } 4846 4847 /* Respect alignment of argument on the stack. */ 4848 unsigned Align = 4849 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize); 4850 NumBytes = ((NumBytes + Align - 1) / Align) * Align; 4851 4852 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize); 4853 if (Flags.isInConsecutiveRegsLast()) 4854 NumBytes = ((NumBytes + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 4855 } 4856 4857 unsigned NumBytesActuallyUsed = NumBytes; 4858 4859 // The prolog code of the callee may store up to 8 GPR argument registers to 4860 // the stack, allowing va_start to index over them in memory if its varargs. 4861 // Because we cannot tell if this is needed on the caller side, we have to 4862 // conservatively assume that it is needed. As such, make sure we have at 4863 // least enough stack space for the caller to store the 8 GPRs. 4864 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area. 4865 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize); 4866 4867 // Tail call needs the stack to be aligned. 4868 if (getTargetMachine().Options.GuaranteedTailCallOpt && 4869 CallConv == CallingConv::Fast) 4870 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes); 4871 4872 // Calculate by how many bytes the stack has to be adjusted in case of tail 4873 // call optimization. 4874 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); 4875 4876 // To protect arguments on the stack from being clobbered in a tail call, 4877 // force all the loads to happen before doing any other lowering. 4878 if (isTailCall) 4879 Chain = DAG.getStackArgumentTokenFactor(Chain); 4880 4881 // Adjust the stack pointer for the new arguments... 4882 // These operations are automatically eliminated by the prolog/epilog pass 4883 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), 4884 dl); 4885 SDValue CallSeqStart = Chain; 4886 4887 // Load the return address and frame pointer so it can be move somewhere else 4888 // later. 4889 SDValue LROp, FPOp; 4890 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true, 4891 dl); 4892 4893 // Set up a copy of the stack pointer for use loading and storing any 4894 // arguments that may not fit in the registers available for argument 4895 // passing. 4896 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64); 4897 4898 // Figure out which arguments are going to go in registers, and which in 4899 // memory. Also, if this is a vararg function, floating point operations 4900 // must be stored to our stack, and loaded into integer regs as well, if 4901 // any integer regs are available for argument passing. 4902 unsigned ArgOffset = LinkageSize; 4903 4904 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 4905 SmallVector<TailCallArgumentInfo, 8> TailCallArguments; 4906 4907 SmallVector<SDValue, 8> MemOpChains; 4908 for (unsigned i = 0; i != NumOps; ++i) { 4909 SDValue Arg = OutVals[i]; 4910 ISD::ArgFlagsTy Flags = Outs[i].Flags; 4911 EVT ArgVT = Outs[i].VT; 4912 EVT OrigVT = Outs[i].ArgVT; 4913 4914 // PtrOff will be used to store the current argument to the stack if a 4915 // register cannot be found for it. 4916 SDValue PtrOff; 4917 4918 // We re-align the argument offset for each argument, except when using the 4919 // fast calling convention, when we need to make sure we do that only when 4920 // we'll actually use a stack slot. 4921 auto ComputePtrOff = [&]() { 4922 /* Respect alignment of argument on the stack. */ 4923 unsigned Align = 4924 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize); 4925 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align; 4926 4927 PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType()); 4928 4929 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); 4930 }; 4931 4932 if (CallConv != CallingConv::Fast) { 4933 ComputePtrOff(); 4934 4935 /* Compute GPR index associated with argument offset. */ 4936 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize; 4937 GPR_idx = std::min(GPR_idx, NumGPRs); 4938 } 4939 4940 // Promote integers to 64-bit values. 4941 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) { 4942 // FIXME: Should this use ANY_EXTEND if neither sext nor zext? 4943 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 4944 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg); 4945 } 4946 4947 // FIXME memcpy is used way more than necessary. Correctness first. 4948 // Note: "by value" is code for passing a structure by value, not 4949 // basic types. 4950 if (Flags.isByVal()) { 4951 // Note: Size includes alignment padding, so 4952 // struct x { short a; char b; } 4953 // will have Size = 4. With #pragma pack(1), it will have Size = 3. 4954 // These are the proper values we need for right-justifying the 4955 // aggregate in a parameter register. 4956 unsigned Size = Flags.getByValSize(); 4957 4958 // An empty aggregate parameter takes up no storage and no 4959 // registers. 4960 if (Size == 0) 4961 continue; 4962 4963 if (CallConv == CallingConv::Fast) 4964 ComputePtrOff(); 4965 4966 // All aggregates smaller than 8 bytes must be passed right-justified. 4967 if (Size==1 || Size==2 || Size==4) { 4968 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32); 4969 if (GPR_idx != NumGPRs) { 4970 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg, 4971 MachinePointerInfo(), VT, 4972 false, false, false, 0); 4973 MemOpChains.push_back(Load.getValue(1)); 4974 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 4975 4976 ArgOffset += PtrByteSize; 4977 continue; 4978 } 4979 } 4980 4981 if (GPR_idx == NumGPRs && Size < 8) { 4982 SDValue AddPtr = PtrOff; 4983 if (!isLittleEndian) { 4984 SDValue Const = DAG.getConstant(PtrByteSize - Size, dl, 4985 PtrOff.getValueType()); 4986 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); 4987 } 4988 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr, 4989 CallSeqStart, 4990 Flags, DAG, dl); 4991 ArgOffset += PtrByteSize; 4992 continue; 4993 } 4994 // Copy entire object into memory. There are cases where gcc-generated 4995 // code assumes it is there, even if it could be put entirely into 4996 // registers. (This is not what the doc says.) 4997 4998 // FIXME: The above statement is likely due to a misunderstanding of the 4999 // documents. All arguments must be copied into the parameter area BY 5000 // THE CALLEE in the event that the callee takes the address of any 5001 // formal argument. That has not yet been implemented. However, it is 5002 // reasonable to use the stack area as a staging area for the register 5003 // load. 5004 5005 // Skip this for small aggregates, as we will use the same slot for a 5006 // right-justified copy, below. 5007 if (Size >= 8) 5008 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff, 5009 CallSeqStart, 5010 Flags, DAG, dl); 5011 5012 // When a register is available, pass a small aggregate right-justified. 5013 if (Size < 8 && GPR_idx != NumGPRs) { 5014 // The easiest way to get this right-justified in a register 5015 // is to copy the structure into the rightmost portion of a 5016 // local variable slot, then load the whole slot into the 5017 // register. 5018 // FIXME: The memcpy seems to produce pretty awful code for 5019 // small aggregates, particularly for packed ones. 5020 // FIXME: It would be preferable to use the slot in the 5021 // parameter save area instead of a new local variable. 5022 SDValue AddPtr = PtrOff; 5023 if (!isLittleEndian) { 5024 SDValue Const = DAG.getConstant(8 - Size, dl, PtrOff.getValueType()); 5025 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); 5026 } 5027 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr, 5028 CallSeqStart, 5029 Flags, DAG, dl); 5030 5031 // Load the slot into the register. 5032 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff, 5033 MachinePointerInfo(), 5034 false, false, false, 0); 5035 MemOpChains.push_back(Load.getValue(1)); 5036 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5037 5038 // Done with this argument. 5039 ArgOffset += PtrByteSize; 5040 continue; 5041 } 5042 5043 // For aggregates larger than PtrByteSize, copy the pieces of the 5044 // object that fit into registers from the parameter save area. 5045 for (unsigned j=0; j<Size; j+=PtrByteSize) { 5046 SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType()); 5047 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 5048 if (GPR_idx != NumGPRs) { 5049 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, 5050 MachinePointerInfo(), 5051 false, false, false, 0); 5052 MemOpChains.push_back(Load.getValue(1)); 5053 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5054 ArgOffset += PtrByteSize; 5055 } else { 5056 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize; 5057 break; 5058 } 5059 } 5060 continue; 5061 } 5062 5063 switch (Arg.getSimpleValueType().SimpleTy) { 5064 default: llvm_unreachable("Unexpected ValueType for argument!"); 5065 case MVT::i1: 5066 case MVT::i32: 5067 case MVT::i64: 5068 if (Flags.isNest()) { 5069 // The 'nest' parameter, if any, is passed in R11. 5070 RegsToPass.push_back(std::make_pair(PPC::X11, Arg)); 5071 hasNest = true; 5072 break; 5073 } 5074 5075 // These can be scalar arguments or elements of an integer array type 5076 // passed directly. Clang may use those instead of "byval" aggregate 5077 // types to avoid forcing arguments to memory unnecessarily. 5078 if (GPR_idx != NumGPRs) { 5079 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg)); 5080 } else { 5081 if (CallConv == CallingConv::Fast) 5082 ComputePtrOff(); 5083 5084 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 5085 true, isTailCall, false, MemOpChains, 5086 TailCallArguments, dl); 5087 if (CallConv == CallingConv::Fast) 5088 ArgOffset += PtrByteSize; 5089 } 5090 if (CallConv != CallingConv::Fast) 5091 ArgOffset += PtrByteSize; 5092 break; 5093 case MVT::f32: 5094 case MVT::f64: { 5095 // These can be scalar arguments or elements of a float array type 5096 // passed directly. The latter are used to implement ELFv2 homogenous 5097 // float aggregates. 5098 5099 // Named arguments go into FPRs first, and once they overflow, the 5100 // remaining arguments go into GPRs and then the parameter save area. 5101 // Unnamed arguments for vararg functions always go to GPRs and 5102 // then the parameter save area. For now, put all arguments to vararg 5103 // routines always in both locations (FPR *and* GPR or stack slot). 5104 bool NeedGPROrStack = isVarArg || FPR_idx == NumFPRs; 5105 bool NeededLoad = false; 5106 5107 // First load the argument into the next available FPR. 5108 if (FPR_idx != NumFPRs) 5109 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg)); 5110 5111 // Next, load the argument into GPR or stack slot if needed. 5112 if (!NeedGPROrStack) 5113 ; 5114 else if (GPR_idx != NumGPRs && CallConv != CallingConv::Fast) { 5115 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8 5116 // once we support fp <-> gpr moves. 5117 5118 // In the non-vararg case, this can only ever happen in the 5119 // presence of f32 array types, since otherwise we never run 5120 // out of FPRs before running out of GPRs. 5121 SDValue ArgVal; 5122 5123 // Double values are always passed in a single GPR. 5124 if (Arg.getValueType() != MVT::f32) { 5125 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg); 5126 5127 // Non-array float values are extended and passed in a GPR. 5128 } else if (!Flags.isInConsecutiveRegs()) { 5129 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg); 5130 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal); 5131 5132 // If we have an array of floats, we collect every odd element 5133 // together with its predecessor into one GPR. 5134 } else if (ArgOffset % PtrByteSize != 0) { 5135 SDValue Lo, Hi; 5136 Lo = DAG.getNode(ISD::BITCAST, dl, MVT::i32, OutVals[i - 1]); 5137 Hi = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg); 5138 if (!isLittleEndian) 5139 std::swap(Lo, Hi); 5140 ArgVal = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 5141 5142 // The final element, if even, goes into the first half of a GPR. 5143 } else if (Flags.isInConsecutiveRegsLast()) { 5144 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg); 5145 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal); 5146 if (!isLittleEndian) 5147 ArgVal = DAG.getNode(ISD::SHL, dl, MVT::i64, ArgVal, 5148 DAG.getConstant(32, dl, MVT::i32)); 5149 5150 // Non-final even elements are skipped; they will be handled 5151 // together the with subsequent argument on the next go-around. 5152 } else 5153 ArgVal = SDValue(); 5154 5155 if (ArgVal.getNode()) 5156 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], ArgVal)); 5157 } else { 5158 if (CallConv == CallingConv::Fast) 5159 ComputePtrOff(); 5160 5161 // Single-precision floating-point values are mapped to the 5162 // second (rightmost) word of the stack doubleword. 5163 if (Arg.getValueType() == MVT::f32 && 5164 !isLittleEndian && !Flags.isInConsecutiveRegs()) { 5165 SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType()); 5166 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour); 5167 } 5168 5169 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 5170 true, isTailCall, false, MemOpChains, 5171 TailCallArguments, dl); 5172 5173 NeededLoad = true; 5174 } 5175 // When passing an array of floats, the array occupies consecutive 5176 // space in the argument area; only round up to the next doubleword 5177 // at the end of the array. Otherwise, each float takes 8 bytes. 5178 if (CallConv != CallingConv::Fast || NeededLoad) { 5179 ArgOffset += (Arg.getValueType() == MVT::f32 && 5180 Flags.isInConsecutiveRegs()) ? 4 : 8; 5181 if (Flags.isInConsecutiveRegsLast()) 5182 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 5183 } 5184 break; 5185 } 5186 case MVT::v4f32: 5187 case MVT::v4i32: 5188 case MVT::v8i16: 5189 case MVT::v16i8: 5190 case MVT::v2f64: 5191 case MVT::v2i64: 5192 case MVT::v1i128: 5193 if (!Subtarget.hasQPX()) { 5194 // These can be scalar arguments or elements of a vector array type 5195 // passed directly. The latter are used to implement ELFv2 homogenous 5196 // vector aggregates. 5197 5198 // For a varargs call, named arguments go into VRs or on the stack as 5199 // usual; unnamed arguments always go to the stack or the corresponding 5200 // GPRs when within range. For now, we always put the value in both 5201 // locations (or even all three). 5202 if (isVarArg) { 5203 // We could elide this store in the case where the object fits 5204 // entirely in R registers. Maybe later. 5205 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, 5206 MachinePointerInfo(), false, false, 0); 5207 MemOpChains.push_back(Store); 5208 if (VR_idx != NumVRs) { 5209 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, 5210 MachinePointerInfo(), 5211 false, false, false, 0); 5212 MemOpChains.push_back(Load.getValue(1)); 5213 5214 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 || 5215 Arg.getSimpleValueType() == MVT::v2i64) ? 5216 VSRH[VR_idx] : VR[VR_idx]; 5217 ++VR_idx; 5218 5219 RegsToPass.push_back(std::make_pair(VReg, Load)); 5220 } 5221 ArgOffset += 16; 5222 for (unsigned i=0; i<16; i+=PtrByteSize) { 5223 if (GPR_idx == NumGPRs) 5224 break; 5225 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, 5226 DAG.getConstant(i, dl, PtrVT)); 5227 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(), 5228 false, false, false, 0); 5229 MemOpChains.push_back(Load.getValue(1)); 5230 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5231 } 5232 break; 5233 } 5234 5235 // Non-varargs Altivec params go into VRs or on the stack. 5236 if (VR_idx != NumVRs) { 5237 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 || 5238 Arg.getSimpleValueType() == MVT::v2i64) ? 5239 VSRH[VR_idx] : VR[VR_idx]; 5240 ++VR_idx; 5241 5242 RegsToPass.push_back(std::make_pair(VReg, Arg)); 5243 } else { 5244 if (CallConv == CallingConv::Fast) 5245 ComputePtrOff(); 5246 5247 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 5248 true, isTailCall, true, MemOpChains, 5249 TailCallArguments, dl); 5250 if (CallConv == CallingConv::Fast) 5251 ArgOffset += 16; 5252 } 5253 5254 if (CallConv != CallingConv::Fast) 5255 ArgOffset += 16; 5256 break; 5257 } // not QPX 5258 5259 assert(Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32 && 5260 "Invalid QPX parameter type"); 5261 5262 /* fall through */ 5263 case MVT::v4f64: 5264 case MVT::v4i1: { 5265 bool IsF32 = Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32; 5266 if (isVarArg) { 5267 // We could elide this store in the case where the object fits 5268 // entirely in R registers. Maybe later. 5269 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, 5270 MachinePointerInfo(), false, false, 0); 5271 MemOpChains.push_back(Store); 5272 if (QFPR_idx != NumQFPRs) { 5273 SDValue Load = DAG.getLoad(IsF32 ? MVT::v4f32 : MVT::v4f64, dl, 5274 Store, PtrOff, MachinePointerInfo(), 5275 false, false, false, 0); 5276 MemOpChains.push_back(Load.getValue(1)); 5277 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Load)); 5278 } 5279 ArgOffset += (IsF32 ? 16 : 32); 5280 for (unsigned i = 0; i < (IsF32 ? 16U : 32U); i += PtrByteSize) { 5281 if (GPR_idx == NumGPRs) 5282 break; 5283 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, 5284 DAG.getConstant(i, dl, PtrVT)); 5285 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(), 5286 false, false, false, 0); 5287 MemOpChains.push_back(Load.getValue(1)); 5288 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5289 } 5290 break; 5291 } 5292 5293 // Non-varargs QPX params go into registers or on the stack. 5294 if (QFPR_idx != NumQFPRs) { 5295 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Arg)); 5296 } else { 5297 if (CallConv == CallingConv::Fast) 5298 ComputePtrOff(); 5299 5300 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 5301 true, isTailCall, true, MemOpChains, 5302 TailCallArguments, dl); 5303 if (CallConv == CallingConv::Fast) 5304 ArgOffset += (IsF32 ? 16 : 32); 5305 } 5306 5307 if (CallConv != CallingConv::Fast) 5308 ArgOffset += (IsF32 ? 16 : 32); 5309 break; 5310 } 5311 } 5312 } 5313 5314 assert(NumBytesActuallyUsed == ArgOffset); 5315 (void)NumBytesActuallyUsed; 5316 5317 if (!MemOpChains.empty()) 5318 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 5319 5320 // Check if this is an indirect call (MTCTR/BCTRL). 5321 // See PrepareCall() for more information about calls through function 5322 // pointers in the 64-bit SVR4 ABI. 5323 if (!isTailCall && !IsPatchPoint && 5324 !isFunctionGlobalAddress(Callee) && 5325 !isa<ExternalSymbolSDNode>(Callee)) { 5326 // Load r2 into a virtual register and store it to the TOC save area. 5327 setUsesTOCBasePtr(DAG); 5328 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64); 5329 // TOC save area offset. 5330 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset(); 5331 SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset, dl); 5332 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); 5333 Chain = DAG.getStore( 5334 Val.getValue(1), dl, Val, AddPtr, 5335 MachinePointerInfo::getStack(DAG.getMachineFunction(), TOCSaveOffset), 5336 false, false, 0); 5337 // In the ELFv2 ABI, R12 must contain the address of an indirect callee. 5338 // This does not mean the MTCTR instruction must use R12; it's easier 5339 // to model this as an extra parameter, so do that. 5340 if (isELFv2ABI && !IsPatchPoint) 5341 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee)); 5342 } 5343 5344 // Build a sequence of copy-to-reg nodes chained together with token chain 5345 // and flag operands which copy the outgoing args into the appropriate regs. 5346 SDValue InFlag; 5347 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 5348 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 5349 RegsToPass[i].second, InFlag); 5350 InFlag = Chain.getValue(1); 5351 } 5352 5353 if (isTailCall) 5354 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp, 5355 FPOp, true, TailCallArguments); 5356 5357 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint, 5358 hasNest, DAG, RegsToPass, InFlag, Chain, CallSeqStart, 5359 Callee, SPDiff, NumBytes, Ins, InVals, CS); 5360 } 5361 5362 SDValue 5363 PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee, 5364 CallingConv::ID CallConv, bool isVarArg, 5365 bool isTailCall, bool IsPatchPoint, 5366 const SmallVectorImpl<ISD::OutputArg> &Outs, 5367 const SmallVectorImpl<SDValue> &OutVals, 5368 const SmallVectorImpl<ISD::InputArg> &Ins, 5369 SDLoc dl, SelectionDAG &DAG, 5370 SmallVectorImpl<SDValue> &InVals, 5371 ImmutableCallSite *CS) const { 5372 5373 unsigned NumOps = Outs.size(); 5374 5375 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 5376 bool isPPC64 = PtrVT == MVT::i64; 5377 unsigned PtrByteSize = isPPC64 ? 8 : 4; 5378 5379 MachineFunction &MF = DAG.getMachineFunction(); 5380 5381 // Mark this function as potentially containing a function that contains a 5382 // tail call. As a consequence the frame pointer will be used for dynamicalloc 5383 // and restoring the callers stack pointer in this functions epilog. This is 5384 // done because by tail calling the called function might overwrite the value 5385 // in this function's (MF) stack pointer stack slot 0(SP). 5386 if (getTargetMachine().Options.GuaranteedTailCallOpt && 5387 CallConv == CallingConv::Fast) 5388 MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); 5389 5390 // Count how many bytes are to be pushed on the stack, including the linkage 5391 // area, and parameter passing area. We start with 24/48 bytes, which is 5392 // prereserved space for [SP][CR][LR][3 x unused]. 5393 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); 5394 unsigned NumBytes = LinkageSize; 5395 5396 // Add up all the space actually used. 5397 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually 5398 // they all go in registers, but we must reserve stack space for them for 5399 // possible use by the caller. In varargs or 64-bit calls, parameters are 5400 // assigned stack space in order, with padding so Altivec parameters are 5401 // 16-byte aligned. 5402 unsigned nAltivecParamsAtEnd = 0; 5403 for (unsigned i = 0; i != NumOps; ++i) { 5404 ISD::ArgFlagsTy Flags = Outs[i].Flags; 5405 EVT ArgVT = Outs[i].VT; 5406 // Varargs Altivec parameters are padded to a 16 byte boundary. 5407 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 || 5408 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 || 5409 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64) { 5410 if (!isVarArg && !isPPC64) { 5411 // Non-varargs Altivec parameters go after all the non-Altivec 5412 // parameters; handle those later so we know how much padding we need. 5413 nAltivecParamsAtEnd++; 5414 continue; 5415 } 5416 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary. 5417 NumBytes = ((NumBytes+15)/16)*16; 5418 } 5419 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize); 5420 } 5421 5422 // Allow for Altivec parameters at the end, if needed. 5423 if (nAltivecParamsAtEnd) { 5424 NumBytes = ((NumBytes+15)/16)*16; 5425 NumBytes += 16*nAltivecParamsAtEnd; 5426 } 5427 5428 // The prolog code of the callee may store up to 8 GPR argument registers to 5429 // the stack, allowing va_start to index over them in memory if its varargs. 5430 // Because we cannot tell if this is needed on the caller side, we have to 5431 // conservatively assume that it is needed. As such, make sure we have at 5432 // least enough stack space for the caller to store the 8 GPRs. 5433 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize); 5434 5435 // Tail call needs the stack to be aligned. 5436 if (getTargetMachine().Options.GuaranteedTailCallOpt && 5437 CallConv == CallingConv::Fast) 5438 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes); 5439 5440 // Calculate by how many bytes the stack has to be adjusted in case of tail 5441 // call optimization. 5442 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); 5443 5444 // To protect arguments on the stack from being clobbered in a tail call, 5445 // force all the loads to happen before doing any other lowering. 5446 if (isTailCall) 5447 Chain = DAG.getStackArgumentTokenFactor(Chain); 5448 5449 // Adjust the stack pointer for the new arguments... 5450 // These operations are automatically eliminated by the prolog/epilog pass 5451 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), 5452 dl); 5453 SDValue CallSeqStart = Chain; 5454 5455 // Load the return address and frame pointer so it can be move somewhere else 5456 // later. 5457 SDValue LROp, FPOp; 5458 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true, 5459 dl); 5460 5461 // Set up a copy of the stack pointer for use loading and storing any 5462 // arguments that may not fit in the registers available for argument 5463 // passing. 5464 SDValue StackPtr; 5465 if (isPPC64) 5466 StackPtr = DAG.getRegister(PPC::X1, MVT::i64); 5467 else 5468 StackPtr = DAG.getRegister(PPC::R1, MVT::i32); 5469 5470 // Figure out which arguments are going to go in registers, and which in 5471 // memory. Also, if this is a vararg function, floating point operations 5472 // must be stored to our stack, and loaded into integer regs as well, if 5473 // any integer regs are available for argument passing. 5474 unsigned ArgOffset = LinkageSize; 5475 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; 5476 5477 static const MCPhysReg GPR_32[] = { // 32-bit registers. 5478 PPC::R3, PPC::R4, PPC::R5, PPC::R6, 5479 PPC::R7, PPC::R8, PPC::R9, PPC::R10, 5480 }; 5481 static const MCPhysReg GPR_64[] = { // 64-bit registers. 5482 PPC::X3, PPC::X4, PPC::X5, PPC::X6, 5483 PPC::X7, PPC::X8, PPC::X9, PPC::X10, 5484 }; 5485 static const MCPhysReg VR[] = { 5486 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, 5487 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 5488 }; 5489 const unsigned NumGPRs = array_lengthof(GPR_32); 5490 const unsigned NumFPRs = 13; 5491 const unsigned NumVRs = array_lengthof(VR); 5492 5493 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32; 5494 5495 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 5496 SmallVector<TailCallArgumentInfo, 8> TailCallArguments; 5497 5498 SmallVector<SDValue, 8> MemOpChains; 5499 for (unsigned i = 0; i != NumOps; ++i) { 5500 SDValue Arg = OutVals[i]; 5501 ISD::ArgFlagsTy Flags = Outs[i].Flags; 5502 5503 // PtrOff will be used to store the current argument to the stack if a 5504 // register cannot be found for it. 5505 SDValue PtrOff; 5506 5507 PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType()); 5508 5509 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); 5510 5511 // On PPC64, promote integers to 64-bit values. 5512 if (isPPC64 && Arg.getValueType() == MVT::i32) { 5513 // FIXME: Should this use ANY_EXTEND if neither sext nor zext? 5514 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 5515 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg); 5516 } 5517 5518 // FIXME memcpy is used way more than necessary. Correctness first. 5519 // Note: "by value" is code for passing a structure by value, not 5520 // basic types. 5521 if (Flags.isByVal()) { 5522 unsigned Size = Flags.getByValSize(); 5523 // Very small objects are passed right-justified. Everything else is 5524 // passed left-justified. 5525 if (Size==1 || Size==2) { 5526 EVT VT = (Size==1) ? MVT::i8 : MVT::i16; 5527 if (GPR_idx != NumGPRs) { 5528 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg, 5529 MachinePointerInfo(), VT, 5530 false, false, false, 0); 5531 MemOpChains.push_back(Load.getValue(1)); 5532 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5533 5534 ArgOffset += PtrByteSize; 5535 } else { 5536 SDValue Const = DAG.getConstant(PtrByteSize - Size, dl, 5537 PtrOff.getValueType()); 5538 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); 5539 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr, 5540 CallSeqStart, 5541 Flags, DAG, dl); 5542 ArgOffset += PtrByteSize; 5543 } 5544 continue; 5545 } 5546 // Copy entire object into memory. There are cases where gcc-generated 5547 // code assumes it is there, even if it could be put entirely into 5548 // registers. (This is not what the doc says.) 5549 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff, 5550 CallSeqStart, 5551 Flags, DAG, dl); 5552 5553 // For small aggregates (Darwin only) and aggregates >= PtrByteSize, 5554 // copy the pieces of the object that fit into registers from the 5555 // parameter save area. 5556 for (unsigned j=0; j<Size; j+=PtrByteSize) { 5557 SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType()); 5558 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 5559 if (GPR_idx != NumGPRs) { 5560 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, 5561 MachinePointerInfo(), 5562 false, false, false, 0); 5563 MemOpChains.push_back(Load.getValue(1)); 5564 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5565 ArgOffset += PtrByteSize; 5566 } else { 5567 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize; 5568 break; 5569 } 5570 } 5571 continue; 5572 } 5573 5574 switch (Arg.getSimpleValueType().SimpleTy) { 5575 default: llvm_unreachable("Unexpected ValueType for argument!"); 5576 case MVT::i1: 5577 case MVT::i32: 5578 case MVT::i64: 5579 if (GPR_idx != NumGPRs) { 5580 if (Arg.getValueType() == MVT::i1) 5581 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg); 5582 5583 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg)); 5584 } else { 5585 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 5586 isPPC64, isTailCall, false, MemOpChains, 5587 TailCallArguments, dl); 5588 } 5589 ArgOffset += PtrByteSize; 5590 break; 5591 case MVT::f32: 5592 case MVT::f64: 5593 if (FPR_idx != NumFPRs) { 5594 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg)); 5595 5596 if (isVarArg) { 5597 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, 5598 MachinePointerInfo(), false, false, 0); 5599 MemOpChains.push_back(Store); 5600 5601 // Float varargs are always shadowed in available integer registers 5602 if (GPR_idx != NumGPRs) { 5603 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, 5604 MachinePointerInfo(), false, false, 5605 false, 0); 5606 MemOpChains.push_back(Load.getValue(1)); 5607 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5608 } 5609 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){ 5610 SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType()); 5611 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour); 5612 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, 5613 MachinePointerInfo(), 5614 false, false, false, 0); 5615 MemOpChains.push_back(Load.getValue(1)); 5616 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5617 } 5618 } else { 5619 // If we have any FPRs remaining, we may also have GPRs remaining. 5620 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available 5621 // GPRs. 5622 if (GPR_idx != NumGPRs) 5623 ++GPR_idx; 5624 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && 5625 !isPPC64) // PPC64 has 64-bit GPR's obviously :) 5626 ++GPR_idx; 5627 } 5628 } else 5629 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 5630 isPPC64, isTailCall, false, MemOpChains, 5631 TailCallArguments, dl); 5632 if (isPPC64) 5633 ArgOffset += 8; 5634 else 5635 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8; 5636 break; 5637 case MVT::v4f32: 5638 case MVT::v4i32: 5639 case MVT::v8i16: 5640 case MVT::v16i8: 5641 if (isVarArg) { 5642 // These go aligned on the stack, or in the corresponding R registers 5643 // when within range. The Darwin PPC ABI doc claims they also go in 5644 // V registers; in fact gcc does this only for arguments that are 5645 // prototyped, not for those that match the ... We do it for all 5646 // arguments, seems to work. 5647 while (ArgOffset % 16 !=0) { 5648 ArgOffset += PtrByteSize; 5649 if (GPR_idx != NumGPRs) 5650 GPR_idx++; 5651 } 5652 // We could elide this store in the case where the object fits 5653 // entirely in R registers. Maybe later. 5654 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, 5655 DAG.getConstant(ArgOffset, dl, PtrVT)); 5656 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, 5657 MachinePointerInfo(), false, false, 0); 5658 MemOpChains.push_back(Store); 5659 if (VR_idx != NumVRs) { 5660 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, 5661 MachinePointerInfo(), 5662 false, false, false, 0); 5663 MemOpChains.push_back(Load.getValue(1)); 5664 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load)); 5665 } 5666 ArgOffset += 16; 5667 for (unsigned i=0; i<16; i+=PtrByteSize) { 5668 if (GPR_idx == NumGPRs) 5669 break; 5670 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, 5671 DAG.getConstant(i, dl, PtrVT)); 5672 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(), 5673 false, false, false, 0); 5674 MemOpChains.push_back(Load.getValue(1)); 5675 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5676 } 5677 break; 5678 } 5679 5680 // Non-varargs Altivec params generally go in registers, but have 5681 // stack space allocated at the end. 5682 if (VR_idx != NumVRs) { 5683 // Doesn't have GPR space allocated. 5684 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg)); 5685 } else if (nAltivecParamsAtEnd==0) { 5686 // We are emitting Altivec params in order. 5687 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 5688 isPPC64, isTailCall, true, MemOpChains, 5689 TailCallArguments, dl); 5690 ArgOffset += 16; 5691 } 5692 break; 5693 } 5694 } 5695 // If all Altivec parameters fit in registers, as they usually do, 5696 // they get stack space following the non-Altivec parameters. We 5697 // don't track this here because nobody below needs it. 5698 // If there are more Altivec parameters than fit in registers emit 5699 // the stores here. 5700 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) { 5701 unsigned j = 0; 5702 // Offset is aligned; skip 1st 12 params which go in V registers. 5703 ArgOffset = ((ArgOffset+15)/16)*16; 5704 ArgOffset += 12*16; 5705 for (unsigned i = 0; i != NumOps; ++i) { 5706 SDValue Arg = OutVals[i]; 5707 EVT ArgType = Outs[i].VT; 5708 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 || 5709 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) { 5710 if (++j > NumVRs) { 5711 SDValue PtrOff; 5712 // We are emitting Altivec params in order. 5713 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 5714 isPPC64, isTailCall, true, MemOpChains, 5715 TailCallArguments, dl); 5716 ArgOffset += 16; 5717 } 5718 } 5719 } 5720 } 5721 5722 if (!MemOpChains.empty()) 5723 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 5724 5725 // On Darwin, R12 must contain the address of an indirect callee. This does 5726 // not mean the MTCTR instruction must use R12; it's easier to model this as 5727 // an extra parameter, so do that. 5728 if (!isTailCall && 5729 !isFunctionGlobalAddress(Callee) && 5730 !isa<ExternalSymbolSDNode>(Callee) && 5731 !isBLACompatibleAddress(Callee, DAG)) 5732 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 : 5733 PPC::R12), Callee)); 5734 5735 // Build a sequence of copy-to-reg nodes chained together with token chain 5736 // and flag operands which copy the outgoing args into the appropriate regs. 5737 SDValue InFlag; 5738 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 5739 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 5740 RegsToPass[i].second, InFlag); 5741 InFlag = Chain.getValue(1); 5742 } 5743 5744 if (isTailCall) 5745 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp, 5746 FPOp, true, TailCallArguments); 5747 5748 return FinishCall(CallConv, dl, isTailCall, isVarArg, IsPatchPoint, 5749 /* unused except on PPC64 ELFv1 */ false, DAG, 5750 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff, 5751 NumBytes, Ins, InVals, CS); 5752 } 5753 5754 bool 5755 PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 5756 MachineFunction &MF, bool isVarArg, 5757 const SmallVectorImpl<ISD::OutputArg> &Outs, 5758 LLVMContext &Context) const { 5759 SmallVector<CCValAssign, 16> RVLocs; 5760 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context); 5761 return CCInfo.CheckReturn(Outs, RetCC_PPC); 5762 } 5763 5764 SDValue 5765 PPCTargetLowering::LowerReturn(SDValue Chain, 5766 CallingConv::ID CallConv, bool isVarArg, 5767 const SmallVectorImpl<ISD::OutputArg> &Outs, 5768 const SmallVectorImpl<SDValue> &OutVals, 5769 SDLoc dl, SelectionDAG &DAG) const { 5770 5771 SmallVector<CCValAssign, 16> RVLocs; 5772 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 5773 *DAG.getContext()); 5774 CCInfo.AnalyzeReturn(Outs, RetCC_PPC); 5775 5776 SDValue Flag; 5777 SmallVector<SDValue, 4> RetOps(1, Chain); 5778 5779 // Copy the result values into the output registers. 5780 for (unsigned i = 0; i != RVLocs.size(); ++i) { 5781 CCValAssign &VA = RVLocs[i]; 5782 assert(VA.isRegLoc() && "Can only return in registers!"); 5783 5784 SDValue Arg = OutVals[i]; 5785 5786 switch (VA.getLocInfo()) { 5787 default: llvm_unreachable("Unknown loc info!"); 5788 case CCValAssign::Full: break; 5789 case CCValAssign::AExt: 5790 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 5791 break; 5792 case CCValAssign::ZExt: 5793 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 5794 break; 5795 case CCValAssign::SExt: 5796 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 5797 break; 5798 } 5799 5800 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); 5801 Flag = Chain.getValue(1); 5802 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 5803 } 5804 5805 RetOps[0] = Chain; // Update chain. 5806 5807 // Add the flag if we have it. 5808 if (Flag.getNode()) 5809 RetOps.push_back(Flag); 5810 5811 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps); 5812 } 5813 5814 SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG, 5815 const PPCSubtarget &Subtarget) const { 5816 // When we pop the dynamic allocation we need to restore the SP link. 5817 SDLoc dl(Op); 5818 5819 // Get the corect type for pointers. 5820 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 5821 5822 // Construct the stack pointer operand. 5823 bool isPPC64 = Subtarget.isPPC64(); 5824 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1; 5825 SDValue StackPtr = DAG.getRegister(SP, PtrVT); 5826 5827 // Get the operands for the STACKRESTORE. 5828 SDValue Chain = Op.getOperand(0); 5829 SDValue SaveSP = Op.getOperand(1); 5830 5831 // Load the old link SP. 5832 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr, 5833 MachinePointerInfo(), 5834 false, false, false, 0); 5835 5836 // Restore the stack pointer. 5837 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP); 5838 5839 // Store the old link SP. 5840 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(), 5841 false, false, 0); 5842 } 5843 5844 5845 5846 SDValue 5847 PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const { 5848 MachineFunction &MF = DAG.getMachineFunction(); 5849 bool isPPC64 = Subtarget.isPPC64(); 5850 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout()); 5851 5852 // Get current frame pointer save index. The users of this index will be 5853 // primarily DYNALLOC instructions. 5854 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 5855 int RASI = FI->getReturnAddrSaveIndex(); 5856 5857 // If the frame pointer save index hasn't been defined yet. 5858 if (!RASI) { 5859 // Find out what the fix offset of the frame pointer save area. 5860 int LROffset = Subtarget.getFrameLowering()->getReturnSaveOffset(); 5861 // Allocate the frame index for frame pointer save area. 5862 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, false); 5863 // Save the result. 5864 FI->setReturnAddrSaveIndex(RASI); 5865 } 5866 return DAG.getFrameIndex(RASI, PtrVT); 5867 } 5868 5869 SDValue 5870 PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const { 5871 MachineFunction &MF = DAG.getMachineFunction(); 5872 bool isPPC64 = Subtarget.isPPC64(); 5873 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout()); 5874 5875 // Get current frame pointer save index. The users of this index will be 5876 // primarily DYNALLOC instructions. 5877 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 5878 int FPSI = FI->getFramePointerSaveIndex(); 5879 5880 // If the frame pointer save index hasn't been defined yet. 5881 if (!FPSI) { 5882 // Find out what the fix offset of the frame pointer save area. 5883 int FPOffset = Subtarget.getFrameLowering()->getFramePointerSaveOffset(); 5884 // Allocate the frame index for frame pointer save area. 5885 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true); 5886 // Save the result. 5887 FI->setFramePointerSaveIndex(FPSI); 5888 } 5889 return DAG.getFrameIndex(FPSI, PtrVT); 5890 } 5891 5892 SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, 5893 SelectionDAG &DAG, 5894 const PPCSubtarget &Subtarget) const { 5895 // Get the inputs. 5896 SDValue Chain = Op.getOperand(0); 5897 SDValue Size = Op.getOperand(1); 5898 SDLoc dl(Op); 5899 5900 // Get the corect type for pointers. 5901 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 5902 // Negate the size. 5903 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT, 5904 DAG.getConstant(0, dl, PtrVT), Size); 5905 // Construct a node for the frame pointer save index. 5906 SDValue FPSIdx = getFramePointerFrameIndex(DAG); 5907 // Build a DYNALLOC node. 5908 SDValue Ops[3] = { Chain, NegSize, FPSIdx }; 5909 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other); 5910 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops); 5911 } 5912 5913 SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op, 5914 SelectionDAG &DAG) const { 5915 SDLoc DL(Op); 5916 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL, 5917 DAG.getVTList(MVT::i32, MVT::Other), 5918 Op.getOperand(0), Op.getOperand(1)); 5919 } 5920 5921 SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op, 5922 SelectionDAG &DAG) const { 5923 SDLoc DL(Op); 5924 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other, 5925 Op.getOperand(0), Op.getOperand(1)); 5926 } 5927 5928 SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { 5929 if (Op.getValueType().isVector()) 5930 return LowerVectorLoad(Op, DAG); 5931 5932 assert(Op.getValueType() == MVT::i1 && 5933 "Custom lowering only for i1 loads"); 5934 5935 // First, load 8 bits into 32 bits, then truncate to 1 bit. 5936 5937 SDLoc dl(Op); 5938 LoadSDNode *LD = cast<LoadSDNode>(Op); 5939 5940 SDValue Chain = LD->getChain(); 5941 SDValue BasePtr = LD->getBasePtr(); 5942 MachineMemOperand *MMO = LD->getMemOperand(); 5943 5944 SDValue NewLD = 5945 DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(DAG.getDataLayout()), Chain, 5946 BasePtr, MVT::i8, MMO); 5947 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD); 5948 5949 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) }; 5950 return DAG.getMergeValues(Ops, dl); 5951 } 5952 5953 SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { 5954 if (Op.getOperand(1).getValueType().isVector()) 5955 return LowerVectorStore(Op, DAG); 5956 5957 assert(Op.getOperand(1).getValueType() == MVT::i1 && 5958 "Custom lowering only for i1 stores"); 5959 5960 // First, zero extend to 32 bits, then use a truncating store to 8 bits. 5961 5962 SDLoc dl(Op); 5963 StoreSDNode *ST = cast<StoreSDNode>(Op); 5964 5965 SDValue Chain = ST->getChain(); 5966 SDValue BasePtr = ST->getBasePtr(); 5967 SDValue Value = ST->getValue(); 5968 MachineMemOperand *MMO = ST->getMemOperand(); 5969 5970 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(DAG.getDataLayout()), 5971 Value); 5972 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO); 5973 } 5974 5975 // FIXME: Remove this once the ANDI glue bug is fixed: 5976 SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const { 5977 assert(Op.getValueType() == MVT::i1 && 5978 "Custom lowering only for i1 results"); 5979 5980 SDLoc DL(Op); 5981 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1, 5982 Op.getOperand(0)); 5983 } 5984 5985 /// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when 5986 /// possible. 5987 SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 5988 // Not FP? Not a fsel. 5989 if (!Op.getOperand(0).getValueType().isFloatingPoint() || 5990 !Op.getOperand(2).getValueType().isFloatingPoint()) 5991 return Op; 5992 5993 // We might be able to do better than this under some circumstances, but in 5994 // general, fsel-based lowering of select is a finite-math-only optimization. 5995 // For more information, see section F.3 of the 2.06 ISA specification. 5996 if (!DAG.getTarget().Options.NoInfsFPMath || 5997 !DAG.getTarget().Options.NoNaNsFPMath) 5998 return Op; 5999 6000 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 6001 6002 EVT ResVT = Op.getValueType(); 6003 EVT CmpVT = Op.getOperand(0).getValueType(); 6004 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); 6005 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3); 6006 SDLoc dl(Op); 6007 6008 // If the RHS of the comparison is a 0.0, we don't need to do the 6009 // subtraction at all. 6010 SDValue Sel1; 6011 if (isFloatingPointZero(RHS)) 6012 switch (CC) { 6013 default: break; // SETUO etc aren't handled by fsel. 6014 case ISD::SETNE: 6015 std::swap(TV, FV); 6016 case ISD::SETEQ: 6017 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits 6018 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); 6019 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV); 6020 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits 6021 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1); 6022 return DAG.getNode(PPCISD::FSEL, dl, ResVT, 6023 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV); 6024 case ISD::SETULT: 6025 case ISD::SETLT: 6026 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt 6027 case ISD::SETOGE: 6028 case ISD::SETGE: 6029 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits 6030 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); 6031 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV); 6032 case ISD::SETUGT: 6033 case ISD::SETGT: 6034 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt 6035 case ISD::SETOLE: 6036 case ISD::SETLE: 6037 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits 6038 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); 6039 return DAG.getNode(PPCISD::FSEL, dl, ResVT, 6040 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV); 6041 } 6042 6043 SDValue Cmp; 6044 switch (CC) { 6045 default: break; // SETUO etc aren't handled by fsel. 6046 case ISD::SETNE: 6047 std::swap(TV, FV); 6048 case ISD::SETEQ: 6049 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS); 6050 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits 6051 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); 6052 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); 6053 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits 6054 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1); 6055 return DAG.getNode(PPCISD::FSEL, dl, ResVT, 6056 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV); 6057 case ISD::SETULT: 6058 case ISD::SETLT: 6059 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS); 6060 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits 6061 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); 6062 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV); 6063 case ISD::SETOGE: 6064 case ISD::SETGE: 6065 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS); 6066 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits 6067 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); 6068 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); 6069 case ISD::SETUGT: 6070 case ISD::SETGT: 6071 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS); 6072 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits 6073 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); 6074 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV); 6075 case ISD::SETOLE: 6076 case ISD::SETLE: 6077 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS); 6078 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits 6079 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); 6080 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); 6081 } 6082 return Op; 6083 } 6084 6085 void PPCTargetLowering::LowerFP_TO_INTForReuse(SDValue Op, ReuseLoadInfo &RLI, 6086 SelectionDAG &DAG, 6087 SDLoc dl) const { 6088 assert(Op.getOperand(0).getValueType().isFloatingPoint()); 6089 SDValue Src = Op.getOperand(0); 6090 if (Src.getValueType() == MVT::f32) 6091 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src); 6092 6093 SDValue Tmp; 6094 switch (Op.getSimpleValueType().SimpleTy) { 6095 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!"); 6096 case MVT::i32: 6097 Tmp = DAG.getNode( 6098 Op.getOpcode() == ISD::FP_TO_SINT 6099 ? PPCISD::FCTIWZ 6100 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ), 6101 dl, MVT::f64, Src); 6102 break; 6103 case MVT::i64: 6104 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) && 6105 "i64 FP_TO_UINT is supported only with FPCVT"); 6106 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ : 6107 PPCISD::FCTIDUZ, 6108 dl, MVT::f64, Src); 6109 break; 6110 } 6111 6112 // Convert the FP value to an int value through memory. 6113 bool i32Stack = Op.getValueType() == MVT::i32 && Subtarget.hasSTFIWX() && 6114 (Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()); 6115 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64); 6116 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex(); 6117 MachinePointerInfo MPI = 6118 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 6119 6120 // Emit a store to the stack slot. 6121 SDValue Chain; 6122 if (i32Stack) { 6123 MachineFunction &MF = DAG.getMachineFunction(); 6124 MachineMemOperand *MMO = 6125 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4); 6126 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr }; 6127 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl, 6128 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO); 6129 } else 6130 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr, 6131 MPI, false, false, 0); 6132 6133 // Result is a load from the stack slot. If loading 4 bytes, make sure to 6134 // add in a bias. 6135 if (Op.getValueType() == MVT::i32 && !i32Stack) { 6136 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, 6137 DAG.getConstant(4, dl, FIPtr.getValueType())); 6138 MPI = MPI.getWithOffset(4); 6139 } 6140 6141 RLI.Chain = Chain; 6142 RLI.Ptr = FIPtr; 6143 RLI.MPI = MPI; 6144 } 6145 6146 /// \brief Custom lowers floating point to integer conversions to use 6147 /// the direct move instructions available in ISA 2.07 to avoid the 6148 /// need for load/store combinations. 6149 SDValue PPCTargetLowering::LowerFP_TO_INTDirectMove(SDValue Op, 6150 SelectionDAG &DAG, 6151 SDLoc dl) const { 6152 assert(Op.getOperand(0).getValueType().isFloatingPoint()); 6153 SDValue Src = Op.getOperand(0); 6154 6155 if (Src.getValueType() == MVT::f32) 6156 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src); 6157 6158 SDValue Tmp; 6159 switch (Op.getSimpleValueType().SimpleTy) { 6160 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!"); 6161 case MVT::i32: 6162 Tmp = DAG.getNode( 6163 Op.getOpcode() == ISD::FP_TO_SINT 6164 ? PPCISD::FCTIWZ 6165 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ), 6166 dl, MVT::f64, Src); 6167 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i32, Tmp); 6168 break; 6169 case MVT::i64: 6170 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) && 6171 "i64 FP_TO_UINT is supported only with FPCVT"); 6172 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ : 6173 PPCISD::FCTIDUZ, 6174 dl, MVT::f64, Src); 6175 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i64, Tmp); 6176 break; 6177 } 6178 return Tmp; 6179 } 6180 6181 SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG, 6182 SDLoc dl) const { 6183 if (Subtarget.hasDirectMove() && Subtarget.isPPC64()) 6184 return LowerFP_TO_INTDirectMove(Op, DAG, dl); 6185 6186 ReuseLoadInfo RLI; 6187 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl); 6188 6189 return DAG.getLoad(Op.getValueType(), dl, RLI.Chain, RLI.Ptr, RLI.MPI, false, 6190 false, RLI.IsInvariant, RLI.Alignment, RLI.AAInfo, 6191 RLI.Ranges); 6192 } 6193 6194 // We're trying to insert a regular store, S, and then a load, L. If the 6195 // incoming value, O, is a load, we might just be able to have our load use the 6196 // address used by O. However, we don't know if anything else will store to 6197 // that address before we can load from it. To prevent this situation, we need 6198 // to insert our load, L, into the chain as a peer of O. To do this, we give L 6199 // the same chain operand as O, we create a token factor from the chain results 6200 // of O and L, and we replace all uses of O's chain result with that token 6201 // factor (see spliceIntoChain below for this last part). 6202 bool PPCTargetLowering::canReuseLoadAddress(SDValue Op, EVT MemVT, 6203 ReuseLoadInfo &RLI, 6204 SelectionDAG &DAG, 6205 ISD::LoadExtType ET) const { 6206 SDLoc dl(Op); 6207 if (ET == ISD::NON_EXTLOAD && 6208 (Op.getOpcode() == ISD::FP_TO_UINT || 6209 Op.getOpcode() == ISD::FP_TO_SINT) && 6210 isOperationLegalOrCustom(Op.getOpcode(), 6211 Op.getOperand(0).getValueType())) { 6212 6213 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl); 6214 return true; 6215 } 6216 6217 LoadSDNode *LD = dyn_cast<LoadSDNode>(Op); 6218 if (!LD || LD->getExtensionType() != ET || LD->isVolatile() || 6219 LD->isNonTemporal()) 6220 return false; 6221 if (LD->getMemoryVT() != MemVT) 6222 return false; 6223 6224 RLI.Ptr = LD->getBasePtr(); 6225 if (LD->isIndexed() && LD->getOffset().getOpcode() != ISD::UNDEF) { 6226 assert(LD->getAddressingMode() == ISD::PRE_INC && 6227 "Non-pre-inc AM on PPC?"); 6228 RLI.Ptr = DAG.getNode(ISD::ADD, dl, RLI.Ptr.getValueType(), RLI.Ptr, 6229 LD->getOffset()); 6230 } 6231 6232 RLI.Chain = LD->getChain(); 6233 RLI.MPI = LD->getPointerInfo(); 6234 RLI.IsInvariant = LD->isInvariant(); 6235 RLI.Alignment = LD->getAlignment(); 6236 RLI.AAInfo = LD->getAAInfo(); 6237 RLI.Ranges = LD->getRanges(); 6238 6239 RLI.ResChain = SDValue(LD, LD->isIndexed() ? 2 : 1); 6240 return true; 6241 } 6242 6243 // Given the head of the old chain, ResChain, insert a token factor containing 6244 // it and NewResChain, and make users of ResChain now be users of that token 6245 // factor. 6246 void PPCTargetLowering::spliceIntoChain(SDValue ResChain, 6247 SDValue NewResChain, 6248 SelectionDAG &DAG) const { 6249 if (!ResChain) 6250 return; 6251 6252 SDLoc dl(NewResChain); 6253 6254 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 6255 NewResChain, DAG.getUNDEF(MVT::Other)); 6256 assert(TF.getNode() != NewResChain.getNode() && 6257 "A new TF really is required here"); 6258 6259 DAG.ReplaceAllUsesOfValueWith(ResChain, TF); 6260 DAG.UpdateNodeOperands(TF.getNode(), ResChain, NewResChain); 6261 } 6262 6263 /// \brief Custom lowers integer to floating point conversions to use 6264 /// the direct move instructions available in ISA 2.07 to avoid the 6265 /// need for load/store combinations. 6266 SDValue PPCTargetLowering::LowerINT_TO_FPDirectMove(SDValue Op, 6267 SelectionDAG &DAG, 6268 SDLoc dl) const { 6269 assert((Op.getValueType() == MVT::f32 || 6270 Op.getValueType() == MVT::f64) && 6271 "Invalid floating point type as target of conversion"); 6272 assert(Subtarget.hasFPCVT() && 6273 "Int to FP conversions with direct moves require FPCVT"); 6274 SDValue FP; 6275 SDValue Src = Op.getOperand(0); 6276 bool SinglePrec = Op.getValueType() == MVT::f32; 6277 bool WordInt = Src.getSimpleValueType().SimpleTy == MVT::i32; 6278 bool Signed = Op.getOpcode() == ISD::SINT_TO_FP; 6279 unsigned ConvOp = Signed ? (SinglePrec ? PPCISD::FCFIDS : PPCISD::FCFID) : 6280 (SinglePrec ? PPCISD::FCFIDUS : PPCISD::FCFIDU); 6281 6282 if (WordInt) { 6283 FP = DAG.getNode(Signed ? PPCISD::MTVSRA : PPCISD::MTVSRZ, 6284 dl, MVT::f64, Src); 6285 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP); 6286 } 6287 else { 6288 FP = DAG.getNode(PPCISD::MTVSRA, dl, MVT::f64, Src); 6289 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP); 6290 } 6291 6292 return FP; 6293 } 6294 6295 SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op, 6296 SelectionDAG &DAG) const { 6297 SDLoc dl(Op); 6298 6299 if (Subtarget.hasQPX() && Op.getOperand(0).getValueType() == MVT::v4i1) { 6300 if (Op.getValueType() != MVT::v4f32 && Op.getValueType() != MVT::v4f64) 6301 return SDValue(); 6302 6303 SDValue Value = Op.getOperand(0); 6304 // The values are now known to be -1 (false) or 1 (true). To convert this 6305 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5). 6306 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5 6307 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value); 6308 6309 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::f64); 6310 FPHalfs = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f64, 6311 FPHalfs, FPHalfs, FPHalfs, FPHalfs); 6312 6313 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs); 6314 6315 if (Op.getValueType() != MVT::v4f64) 6316 Value = DAG.getNode(ISD::FP_ROUND, dl, 6317 Op.getValueType(), Value, 6318 DAG.getIntPtrConstant(1, dl)); 6319 return Value; 6320 } 6321 6322 // Don't handle ppc_fp128 here; let it be lowered to a libcall. 6323 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64) 6324 return SDValue(); 6325 6326 if (Op.getOperand(0).getValueType() == MVT::i1) 6327 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0), 6328 DAG.getConstantFP(1.0, dl, Op.getValueType()), 6329 DAG.getConstantFP(0.0, dl, Op.getValueType())); 6330 6331 // If we have direct moves, we can do all the conversion, skip the store/load 6332 // however, without FPCVT we can't do most conversions. 6333 if (Subtarget.hasDirectMove() && Subtarget.isPPC64() && Subtarget.hasFPCVT()) 6334 return LowerINT_TO_FPDirectMove(Op, DAG, dl); 6335 6336 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) && 6337 "UINT_TO_FP is supported only with FPCVT"); 6338 6339 // If we have FCFIDS, then use it when converting to single-precision. 6340 // Otherwise, convert to double-precision and then round. 6341 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) 6342 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS 6343 : PPCISD::FCFIDS) 6344 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU 6345 : PPCISD::FCFID); 6346 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) 6347 ? MVT::f32 6348 : MVT::f64; 6349 6350 if (Op.getOperand(0).getValueType() == MVT::i64) { 6351 SDValue SINT = Op.getOperand(0); 6352 // When converting to single-precision, we actually need to convert 6353 // to double-precision first and then round to single-precision. 6354 // To avoid double-rounding effects during that operation, we have 6355 // to prepare the input operand. Bits that might be truncated when 6356 // converting to double-precision are replaced by a bit that won't 6357 // be lost at this stage, but is below the single-precision rounding 6358 // position. 6359 // 6360 // However, if -enable-unsafe-fp-math is in effect, accept double 6361 // rounding to avoid the extra overhead. 6362 if (Op.getValueType() == MVT::f32 && 6363 !Subtarget.hasFPCVT() && 6364 !DAG.getTarget().Options.UnsafeFPMath) { 6365 6366 // Twiddle input to make sure the low 11 bits are zero. (If this 6367 // is the case, we are guaranteed the value will fit into the 53 bit 6368 // mantissa of an IEEE double-precision value without rounding.) 6369 // If any of those low 11 bits were not zero originally, make sure 6370 // bit 12 (value 2048) is set instead, so that the final rounding 6371 // to single-precision gets the correct result. 6372 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64, 6373 SINT, DAG.getConstant(2047, dl, MVT::i64)); 6374 Round = DAG.getNode(ISD::ADD, dl, MVT::i64, 6375 Round, DAG.getConstant(2047, dl, MVT::i64)); 6376 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT); 6377 Round = DAG.getNode(ISD::AND, dl, MVT::i64, 6378 Round, DAG.getConstant(-2048, dl, MVT::i64)); 6379 6380 // However, we cannot use that value unconditionally: if the magnitude 6381 // of the input value is small, the bit-twiddling we did above might 6382 // end up visibly changing the output. Fortunately, in that case, we 6383 // don't need to twiddle bits since the original input will convert 6384 // exactly to double-precision floating-point already. Therefore, 6385 // construct a conditional to use the original value if the top 11 6386 // bits are all sign-bit copies, and use the rounded value computed 6387 // above otherwise. 6388 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64, 6389 SINT, DAG.getConstant(53, dl, MVT::i32)); 6390 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64, 6391 Cond, DAG.getConstant(1, dl, MVT::i64)); 6392 Cond = DAG.getSetCC(dl, MVT::i32, 6393 Cond, DAG.getConstant(1, dl, MVT::i64), ISD::SETUGT); 6394 6395 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT); 6396 } 6397 6398 ReuseLoadInfo RLI; 6399 SDValue Bits; 6400 6401 MachineFunction &MF = DAG.getMachineFunction(); 6402 if (canReuseLoadAddress(SINT, MVT::i64, RLI, DAG)) { 6403 Bits = DAG.getLoad(MVT::f64, dl, RLI.Chain, RLI.Ptr, RLI.MPI, false, 6404 false, RLI.IsInvariant, RLI.Alignment, RLI.AAInfo, 6405 RLI.Ranges); 6406 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG); 6407 } else if (Subtarget.hasLFIWAX() && 6408 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::SEXTLOAD)) { 6409 MachineMemOperand *MMO = 6410 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4, 6411 RLI.Alignment, RLI.AAInfo, RLI.Ranges); 6412 SDValue Ops[] = { RLI.Chain, RLI.Ptr }; 6413 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWAX, dl, 6414 DAG.getVTList(MVT::f64, MVT::Other), 6415 Ops, MVT::i32, MMO); 6416 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG); 6417 } else if (Subtarget.hasFPCVT() && 6418 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::ZEXTLOAD)) { 6419 MachineMemOperand *MMO = 6420 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4, 6421 RLI.Alignment, RLI.AAInfo, RLI.Ranges); 6422 SDValue Ops[] = { RLI.Chain, RLI.Ptr }; 6423 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWZX, dl, 6424 DAG.getVTList(MVT::f64, MVT::Other), 6425 Ops, MVT::i32, MMO); 6426 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG); 6427 } else if (((Subtarget.hasLFIWAX() && 6428 SINT.getOpcode() == ISD::SIGN_EXTEND) || 6429 (Subtarget.hasFPCVT() && 6430 SINT.getOpcode() == ISD::ZERO_EXTEND)) && 6431 SINT.getOperand(0).getValueType() == MVT::i32) { 6432 MachineFrameInfo *FrameInfo = MF.getFrameInfo(); 6433 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 6434 6435 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false); 6436 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 6437 6438 SDValue Store = DAG.getStore( 6439 DAG.getEntryNode(), dl, SINT.getOperand(0), FIdx, 6440 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx), 6441 false, false, 0); 6442 6443 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 && 6444 "Expected an i32 store"); 6445 6446 RLI.Ptr = FIdx; 6447 RLI.Chain = Store; 6448 RLI.MPI = 6449 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); 6450 RLI.Alignment = 4; 6451 6452 MachineMemOperand *MMO = 6453 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4, 6454 RLI.Alignment, RLI.AAInfo, RLI.Ranges); 6455 SDValue Ops[] = { RLI.Chain, RLI.Ptr }; 6456 Bits = DAG.getMemIntrinsicNode(SINT.getOpcode() == ISD::ZERO_EXTEND ? 6457 PPCISD::LFIWZX : PPCISD::LFIWAX, 6458 dl, DAG.getVTList(MVT::f64, MVT::Other), 6459 Ops, MVT::i32, MMO); 6460 } else 6461 Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT); 6462 6463 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits); 6464 6465 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) 6466 FP = DAG.getNode(ISD::FP_ROUND, dl, 6467 MVT::f32, FP, DAG.getIntPtrConstant(0, dl)); 6468 return FP; 6469 } 6470 6471 assert(Op.getOperand(0).getValueType() == MVT::i32 && 6472 "Unhandled INT_TO_FP type in custom expander!"); 6473 // Since we only generate this in 64-bit mode, we can take advantage of 6474 // 64-bit registers. In particular, sign extend the input value into the 6475 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack 6476 // then lfd it and fcfid it. 6477 MachineFunction &MF = DAG.getMachineFunction(); 6478 MachineFrameInfo *FrameInfo = MF.getFrameInfo(); 6479 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout()); 6480 6481 SDValue Ld; 6482 if (Subtarget.hasLFIWAX() || Subtarget.hasFPCVT()) { 6483 ReuseLoadInfo RLI; 6484 bool ReusingLoad; 6485 if (!(ReusingLoad = canReuseLoadAddress(Op.getOperand(0), MVT::i32, RLI, 6486 DAG))) { 6487 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false); 6488 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 6489 6490 SDValue Store = DAG.getStore( 6491 DAG.getEntryNode(), dl, Op.getOperand(0), FIdx, 6492 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx), 6493 false, false, 0); 6494 6495 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 && 6496 "Expected an i32 store"); 6497 6498 RLI.Ptr = FIdx; 6499 RLI.Chain = Store; 6500 RLI.MPI = 6501 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); 6502 RLI.Alignment = 4; 6503 } 6504 6505 MachineMemOperand *MMO = 6506 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4, 6507 RLI.Alignment, RLI.AAInfo, RLI.Ranges); 6508 SDValue Ops[] = { RLI.Chain, RLI.Ptr }; 6509 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ? 6510 PPCISD::LFIWZX : PPCISD::LFIWAX, 6511 dl, DAG.getVTList(MVT::f64, MVT::Other), 6512 Ops, MVT::i32, MMO); 6513 if (ReusingLoad) 6514 spliceIntoChain(RLI.ResChain, Ld.getValue(1), DAG); 6515 } else { 6516 assert(Subtarget.isPPC64() && 6517 "i32->FP without LFIWAX supported only on PPC64"); 6518 6519 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false); 6520 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 6521 6522 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64, 6523 Op.getOperand(0)); 6524 6525 // STD the extended value into the stack slot. 6526 SDValue Store = DAG.getStore( 6527 DAG.getEntryNode(), dl, Ext64, FIdx, 6528 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx), 6529 false, false, 0); 6530 6531 // Load the value as a double. 6532 Ld = DAG.getLoad( 6533 MVT::f64, dl, Store, FIdx, 6534 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx), 6535 false, false, false, 0); 6536 } 6537 6538 // FCFID it and return it. 6539 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld); 6540 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) 6541 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, 6542 DAG.getIntPtrConstant(0, dl)); 6543 return FP; 6544 } 6545 6546 SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op, 6547 SelectionDAG &DAG) const { 6548 SDLoc dl(Op); 6549 /* 6550 The rounding mode is in bits 30:31 of FPSR, and has the following 6551 settings: 6552 00 Round to nearest 6553 01 Round to 0 6554 10 Round to +inf 6555 11 Round to -inf 6556 6557 FLT_ROUNDS, on the other hand, expects the following: 6558 -1 Undefined 6559 0 Round to 0 6560 1 Round to nearest 6561 2 Round to +inf 6562 3 Round to -inf 6563 6564 To perform the conversion, we do: 6565 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1)) 6566 */ 6567 6568 MachineFunction &MF = DAG.getMachineFunction(); 6569 EVT VT = Op.getValueType(); 6570 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout()); 6571 6572 // Save FP Control Word to register 6573 EVT NodeTys[] = { 6574 MVT::f64, // return register 6575 MVT::Glue // unused in this context 6576 }; 6577 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None); 6578 6579 // Save FP register to stack slot 6580 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false); 6581 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT); 6582 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain, 6583 StackSlot, MachinePointerInfo(), false, false,0); 6584 6585 // Load FP Control Word from low 32 bits of stack slot. 6586 SDValue Four = DAG.getConstant(4, dl, PtrVT); 6587 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four); 6588 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(), 6589 false, false, false, 0); 6590 6591 // Transform as necessary 6592 SDValue CWD1 = 6593 DAG.getNode(ISD::AND, dl, MVT::i32, 6594 CWD, DAG.getConstant(3, dl, MVT::i32)); 6595 SDValue CWD2 = 6596 DAG.getNode(ISD::SRL, dl, MVT::i32, 6597 DAG.getNode(ISD::AND, dl, MVT::i32, 6598 DAG.getNode(ISD::XOR, dl, MVT::i32, 6599 CWD, DAG.getConstant(3, dl, MVT::i32)), 6600 DAG.getConstant(3, dl, MVT::i32)), 6601 DAG.getConstant(1, dl, MVT::i32)); 6602 6603 SDValue RetVal = 6604 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2); 6605 6606 return DAG.getNode((VT.getSizeInBits() < 16 ? 6607 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal); 6608 } 6609 6610 SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const { 6611 EVT VT = Op.getValueType(); 6612 unsigned BitWidth = VT.getSizeInBits(); 6613 SDLoc dl(Op); 6614 assert(Op.getNumOperands() == 3 && 6615 VT == Op.getOperand(1).getValueType() && 6616 "Unexpected SHL!"); 6617 6618 // Expand into a bunch of logical ops. Note that these ops 6619 // depend on the PPC behavior for oversized shift amounts. 6620 SDValue Lo = Op.getOperand(0); 6621 SDValue Hi = Op.getOperand(1); 6622 SDValue Amt = Op.getOperand(2); 6623 EVT AmtVT = Amt.getValueType(); 6624 6625 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, 6626 DAG.getConstant(BitWidth, dl, AmtVT), Amt); 6627 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt); 6628 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1); 6629 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3); 6630 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, 6631 DAG.getConstant(-BitWidth, dl, AmtVT)); 6632 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5); 6633 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6); 6634 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt); 6635 SDValue OutOps[] = { OutLo, OutHi }; 6636 return DAG.getMergeValues(OutOps, dl); 6637 } 6638 6639 SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const { 6640 EVT VT = Op.getValueType(); 6641 SDLoc dl(Op); 6642 unsigned BitWidth = VT.getSizeInBits(); 6643 assert(Op.getNumOperands() == 3 && 6644 VT == Op.getOperand(1).getValueType() && 6645 "Unexpected SRL!"); 6646 6647 // Expand into a bunch of logical ops. Note that these ops 6648 // depend on the PPC behavior for oversized shift amounts. 6649 SDValue Lo = Op.getOperand(0); 6650 SDValue Hi = Op.getOperand(1); 6651 SDValue Amt = Op.getOperand(2); 6652 EVT AmtVT = Amt.getValueType(); 6653 6654 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, 6655 DAG.getConstant(BitWidth, dl, AmtVT), Amt); 6656 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt); 6657 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1); 6658 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); 6659 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, 6660 DAG.getConstant(-BitWidth, dl, AmtVT)); 6661 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5); 6662 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6); 6663 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt); 6664 SDValue OutOps[] = { OutLo, OutHi }; 6665 return DAG.getMergeValues(OutOps, dl); 6666 } 6667 6668 SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const { 6669 SDLoc dl(Op); 6670 EVT VT = Op.getValueType(); 6671 unsigned BitWidth = VT.getSizeInBits(); 6672 assert(Op.getNumOperands() == 3 && 6673 VT == Op.getOperand(1).getValueType() && 6674 "Unexpected SRA!"); 6675 6676 // Expand into a bunch of logical ops, followed by a select_cc. 6677 SDValue Lo = Op.getOperand(0); 6678 SDValue Hi = Op.getOperand(1); 6679 SDValue Amt = Op.getOperand(2); 6680 EVT AmtVT = Amt.getValueType(); 6681 6682 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, 6683 DAG.getConstant(BitWidth, dl, AmtVT), Amt); 6684 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt); 6685 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1); 6686 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); 6687 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, 6688 DAG.getConstant(-BitWidth, dl, AmtVT)); 6689 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5); 6690 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt); 6691 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, dl, AmtVT), 6692 Tmp4, Tmp6, ISD::SETLE); 6693 SDValue OutOps[] = { OutLo, OutHi }; 6694 return DAG.getMergeValues(OutOps, dl); 6695 } 6696 6697 //===----------------------------------------------------------------------===// 6698 // Vector related lowering. 6699 // 6700 6701 /// BuildSplatI - Build a canonical splati of Val with an element size of 6702 /// SplatSize. Cast the result to VT. 6703 static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT, 6704 SelectionDAG &DAG, SDLoc dl) { 6705 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!"); 6706 6707 static const MVT VTys[] = { // canonical VT to use for each size. 6708 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32 6709 }; 6710 6711 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1]; 6712 6713 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize. 6714 if (Val == -1) 6715 SplatSize = 1; 6716 6717 EVT CanonicalVT = VTys[SplatSize-1]; 6718 6719 // Build a canonical splat for this value. 6720 SDValue Elt = DAG.getConstant(Val, dl, MVT::i32); 6721 SmallVector<SDValue, 8> Ops; 6722 Ops.assign(CanonicalVT.getVectorNumElements(), Elt); 6723 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, Ops); 6724 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res); 6725 } 6726 6727 /// BuildIntrinsicOp - Return a unary operator intrinsic node with the 6728 /// specified intrinsic ID. 6729 static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op, 6730 SelectionDAG &DAG, SDLoc dl, 6731 EVT DestVT = MVT::Other) { 6732 if (DestVT == MVT::Other) DestVT = Op.getValueType(); 6733 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, 6734 DAG.getConstant(IID, dl, MVT::i32), Op); 6735 } 6736 6737 /// BuildIntrinsicOp - Return a binary operator intrinsic node with the 6738 /// specified intrinsic ID. 6739 static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS, 6740 SelectionDAG &DAG, SDLoc dl, 6741 EVT DestVT = MVT::Other) { 6742 if (DestVT == MVT::Other) DestVT = LHS.getValueType(); 6743 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, 6744 DAG.getConstant(IID, dl, MVT::i32), LHS, RHS); 6745 } 6746 6747 /// BuildIntrinsicOp - Return a ternary operator intrinsic node with the 6748 /// specified intrinsic ID. 6749 static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1, 6750 SDValue Op2, SelectionDAG &DAG, 6751 SDLoc dl, EVT DestVT = MVT::Other) { 6752 if (DestVT == MVT::Other) DestVT = Op0.getValueType(); 6753 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, 6754 DAG.getConstant(IID, dl, MVT::i32), Op0, Op1, Op2); 6755 } 6756 6757 6758 /// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified 6759 /// amount. The result has the specified value type. 6760 static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt, 6761 EVT VT, SelectionDAG &DAG, SDLoc dl) { 6762 // Force LHS/RHS to be the right type. 6763 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS); 6764 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS); 6765 6766 int Ops[16]; 6767 for (unsigned i = 0; i != 16; ++i) 6768 Ops[i] = i + Amt; 6769 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops); 6770 return DAG.getNode(ISD::BITCAST, dl, VT, T); 6771 } 6772 6773 // If this is a case we can't handle, return null and let the default 6774 // expansion code take care of it. If we CAN select this case, and if it 6775 // selects to a single instruction, return Op. Otherwise, if we can codegen 6776 // this case more efficiently than a constant pool load, lower it to the 6777 // sequence of ops that should be used. 6778 SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op, 6779 SelectionDAG &DAG) const { 6780 SDLoc dl(Op); 6781 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 6782 assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR"); 6783 6784 if (Subtarget.hasQPX() && Op.getValueType() == MVT::v4i1) { 6785 // We first build an i32 vector, load it into a QPX register, 6786 // then convert it to a floating-point vector and compare it 6787 // to a zero vector to get the boolean result. 6788 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); 6789 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false); 6790 MachinePointerInfo PtrInfo = 6791 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); 6792 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 6793 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 6794 6795 assert(BVN->getNumOperands() == 4 && 6796 "BUILD_VECTOR for v4i1 does not have 4 operands"); 6797 6798 bool IsConst = true; 6799 for (unsigned i = 0; i < 4; ++i) { 6800 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF) continue; 6801 if (!isa<ConstantSDNode>(BVN->getOperand(i))) { 6802 IsConst = false; 6803 break; 6804 } 6805 } 6806 6807 if (IsConst) { 6808 Constant *One = 6809 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), 1.0); 6810 Constant *NegOne = 6811 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), -1.0); 6812 6813 SmallVector<Constant*, 4> CV(4, NegOne); 6814 for (unsigned i = 0; i < 4; ++i) { 6815 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF) 6816 CV[i] = UndefValue::get(Type::getFloatTy(*DAG.getContext())); 6817 else if (cast<ConstantSDNode>(BVN->getOperand(i))-> 6818 getConstantIntValue()->isZero()) 6819 continue; 6820 else 6821 CV[i] = One; 6822 } 6823 6824 Constant *CP = ConstantVector::get(CV); 6825 SDValue CPIdx = DAG.getConstantPool(CP, getPointerTy(DAG.getDataLayout()), 6826 16 /* alignment */); 6827 6828 SmallVector<SDValue, 2> Ops; 6829 Ops.push_back(DAG.getEntryNode()); 6830 Ops.push_back(CPIdx); 6831 6832 SmallVector<EVT, 2> ValueVTs; 6833 ValueVTs.push_back(MVT::v4i1); 6834 ValueVTs.push_back(MVT::Other); // chain 6835 SDVTList VTs = DAG.getVTList(ValueVTs); 6836 6837 return DAG.getMemIntrinsicNode( 6838 PPCISD::QVLFSb, dl, VTs, Ops, MVT::v4f32, 6839 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 6840 } 6841 6842 SmallVector<SDValue, 4> Stores; 6843 for (unsigned i = 0; i < 4; ++i) { 6844 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF) continue; 6845 6846 unsigned Offset = 4*i; 6847 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType()); 6848 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx); 6849 6850 unsigned StoreSize = BVN->getOperand(i).getValueType().getStoreSize(); 6851 if (StoreSize > 4) { 6852 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl, 6853 BVN->getOperand(i), Idx, 6854 PtrInfo.getWithOffset(Offset), 6855 MVT::i32, false, false, 0)); 6856 } else { 6857 SDValue StoreValue = BVN->getOperand(i); 6858 if (StoreSize < 4) 6859 StoreValue = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, StoreValue); 6860 6861 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, 6862 StoreValue, Idx, 6863 PtrInfo.getWithOffset(Offset), 6864 false, false, 0)); 6865 } 6866 } 6867 6868 SDValue StoreChain; 6869 if (!Stores.empty()) 6870 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 6871 else 6872 StoreChain = DAG.getEntryNode(); 6873 6874 // Now load from v4i32 into the QPX register; this will extend it to 6875 // v4i64 but not yet convert it to a floating point. Nevertheless, this 6876 // is typed as v4f64 because the QPX register integer states are not 6877 // explicitly represented. 6878 6879 SmallVector<SDValue, 2> Ops; 6880 Ops.push_back(StoreChain); 6881 Ops.push_back(DAG.getConstant(Intrinsic::ppc_qpx_qvlfiwz, dl, MVT::i32)); 6882 Ops.push_back(FIdx); 6883 6884 SmallVector<EVT, 2> ValueVTs; 6885 ValueVTs.push_back(MVT::v4f64); 6886 ValueVTs.push_back(MVT::Other); // chain 6887 SDVTList VTs = DAG.getVTList(ValueVTs); 6888 6889 SDValue LoadedVect = DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, 6890 dl, VTs, Ops, MVT::v4i32, PtrInfo); 6891 LoadedVect = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64, 6892 DAG.getConstant(Intrinsic::ppc_qpx_qvfcfidu, dl, MVT::i32), 6893 LoadedVect); 6894 6895 SDValue FPZeros = DAG.getConstantFP(0.0, dl, MVT::f64); 6896 FPZeros = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f64, 6897 FPZeros, FPZeros, FPZeros, FPZeros); 6898 6899 return DAG.getSetCC(dl, MVT::v4i1, LoadedVect, FPZeros, ISD::SETEQ); 6900 } 6901 6902 // All other QPX vectors are handled by generic code. 6903 if (Subtarget.hasQPX()) 6904 return SDValue(); 6905 6906 // Check if this is a splat of a constant value. 6907 APInt APSplatBits, APSplatUndef; 6908 unsigned SplatBitSize; 6909 bool HasAnyUndefs; 6910 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize, 6911 HasAnyUndefs, 0, !Subtarget.isLittleEndian()) || 6912 SplatBitSize > 32) 6913 return SDValue(); 6914 6915 unsigned SplatBits = APSplatBits.getZExtValue(); 6916 unsigned SplatUndef = APSplatUndef.getZExtValue(); 6917 unsigned SplatSize = SplatBitSize / 8; 6918 6919 // First, handle single instruction cases. 6920 6921 // All zeros? 6922 if (SplatBits == 0) { 6923 // Canonicalize all zero vectors to be v4i32. 6924 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) { 6925 SDValue Z = DAG.getConstant(0, dl, MVT::i32); 6926 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z); 6927 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z); 6928 } 6929 return Op; 6930 } 6931 6932 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw]. 6933 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >> 6934 (32-SplatBitSize)); 6935 if (SextVal >= -16 && SextVal <= 15) 6936 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl); 6937 6938 6939 // Two instruction sequences. 6940 6941 // If this value is in the range [-32,30] and is even, use: 6942 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2) 6943 // If this value is in the range [17,31] and is odd, use: 6944 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16) 6945 // If this value is in the range [-31,-17] and is odd, use: 6946 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16) 6947 // Note the last two are three-instruction sequences. 6948 if (SextVal >= -32 && SextVal <= 31) { 6949 // To avoid having these optimizations undone by constant folding, 6950 // we convert to a pseudo that will be expanded later into one of 6951 // the above forms. 6952 SDValue Elt = DAG.getConstant(SextVal, dl, MVT::i32); 6953 EVT VT = (SplatSize == 1 ? MVT::v16i8 : 6954 (SplatSize == 2 ? MVT::v8i16 : MVT::v4i32)); 6955 SDValue EltSize = DAG.getConstant(SplatSize, dl, MVT::i32); 6956 SDValue RetVal = DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize); 6957 if (VT == Op.getValueType()) 6958 return RetVal; 6959 else 6960 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), RetVal); 6961 } 6962 6963 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is 6964 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important 6965 // for fneg/fabs. 6966 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) { 6967 // Make -1 and vspltisw -1: 6968 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl); 6969 6970 // Make the VSLW intrinsic, computing 0x8000_0000. 6971 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV, 6972 OnesV, DAG, dl); 6973 6974 // xor by OnesV to invert it. 6975 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV); 6976 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); 6977 } 6978 6979 // Check to see if this is a wide variety of vsplti*, binop self cases. 6980 static const signed char SplatCsts[] = { 6981 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, 6982 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16 6983 }; 6984 6985 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) { 6986 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for 6987 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1' 6988 int i = SplatCsts[idx]; 6989 6990 // Figure out what shift amount will be used by altivec if shifted by i in 6991 // this splat size. 6992 unsigned TypeShiftAmt = i & (SplatBitSize-1); 6993 6994 // vsplti + shl self. 6995 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) { 6996 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); 6997 static const unsigned IIDs[] = { // Intrinsic to use for each size. 6998 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0, 6999 Intrinsic::ppc_altivec_vslw 7000 }; 7001 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); 7002 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); 7003 } 7004 7005 // vsplti + srl self. 7006 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) { 7007 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); 7008 static const unsigned IIDs[] = { // Intrinsic to use for each size. 7009 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0, 7010 Intrinsic::ppc_altivec_vsrw 7011 }; 7012 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); 7013 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); 7014 } 7015 7016 // vsplti + sra self. 7017 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) { 7018 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); 7019 static const unsigned IIDs[] = { // Intrinsic to use for each size. 7020 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0, 7021 Intrinsic::ppc_altivec_vsraw 7022 }; 7023 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); 7024 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); 7025 } 7026 7027 // vsplti + rol self. 7028 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) | 7029 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) { 7030 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); 7031 static const unsigned IIDs[] = { // Intrinsic to use for each size. 7032 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0, 7033 Intrinsic::ppc_altivec_vrlw 7034 }; 7035 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); 7036 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); 7037 } 7038 7039 // t = vsplti c, result = vsldoi t, t, 1 7040 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) { 7041 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); 7042 unsigned Amt = Subtarget.isLittleEndian() ? 15 : 1; 7043 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl); 7044 } 7045 // t = vsplti c, result = vsldoi t, t, 2 7046 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) { 7047 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); 7048 unsigned Amt = Subtarget.isLittleEndian() ? 14 : 2; 7049 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl); 7050 } 7051 // t = vsplti c, result = vsldoi t, t, 3 7052 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) { 7053 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); 7054 unsigned Amt = Subtarget.isLittleEndian() ? 13 : 3; 7055 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl); 7056 } 7057 } 7058 7059 return SDValue(); 7060 } 7061 7062 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit 7063 /// the specified operations to build the shuffle. 7064 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, 7065 SDValue RHS, SelectionDAG &DAG, 7066 SDLoc dl) { 7067 unsigned OpNum = (PFEntry >> 26) & 0x0F; 7068 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); 7069 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); 7070 7071 enum { 7072 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> 7073 OP_VMRGHW, 7074 OP_VMRGLW, 7075 OP_VSPLTISW0, 7076 OP_VSPLTISW1, 7077 OP_VSPLTISW2, 7078 OP_VSPLTISW3, 7079 OP_VSLDOI4, 7080 OP_VSLDOI8, 7081 OP_VSLDOI12 7082 }; 7083 7084 if (OpNum == OP_COPY) { 7085 if (LHSID == (1*9+2)*9+3) return LHS; 7086 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); 7087 return RHS; 7088 } 7089 7090 SDValue OpLHS, OpRHS; 7091 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); 7092 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); 7093 7094 int ShufIdxs[16]; 7095 switch (OpNum) { 7096 default: llvm_unreachable("Unknown i32 permute!"); 7097 case OP_VMRGHW: 7098 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3; 7099 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19; 7100 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7; 7101 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23; 7102 break; 7103 case OP_VMRGLW: 7104 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11; 7105 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27; 7106 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15; 7107 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31; 7108 break; 7109 case OP_VSPLTISW0: 7110 for (unsigned i = 0; i != 16; ++i) 7111 ShufIdxs[i] = (i&3)+0; 7112 break; 7113 case OP_VSPLTISW1: 7114 for (unsigned i = 0; i != 16; ++i) 7115 ShufIdxs[i] = (i&3)+4; 7116 break; 7117 case OP_VSPLTISW2: 7118 for (unsigned i = 0; i != 16; ++i) 7119 ShufIdxs[i] = (i&3)+8; 7120 break; 7121 case OP_VSPLTISW3: 7122 for (unsigned i = 0; i != 16; ++i) 7123 ShufIdxs[i] = (i&3)+12; 7124 break; 7125 case OP_VSLDOI4: 7126 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl); 7127 case OP_VSLDOI8: 7128 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl); 7129 case OP_VSLDOI12: 7130 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl); 7131 } 7132 EVT VT = OpLHS.getValueType(); 7133 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS); 7134 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS); 7135 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs); 7136 return DAG.getNode(ISD::BITCAST, dl, VT, T); 7137 } 7138 7139 /// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this 7140 /// is a shuffle we can handle in a single instruction, return it. Otherwise, 7141 /// return the code it can be lowered into. Worst case, it can always be 7142 /// lowered into a vperm. 7143 SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, 7144 SelectionDAG &DAG) const { 7145 SDLoc dl(Op); 7146 SDValue V1 = Op.getOperand(0); 7147 SDValue V2 = Op.getOperand(1); 7148 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); 7149 EVT VT = Op.getValueType(); 7150 bool isLittleEndian = Subtarget.isLittleEndian(); 7151 7152 if (Subtarget.hasQPX()) { 7153 if (VT.getVectorNumElements() != 4) 7154 return SDValue(); 7155 7156 if (V2.getOpcode() == ISD::UNDEF) V2 = V1; 7157 7158 int AlignIdx = PPC::isQVALIGNIShuffleMask(SVOp); 7159 if (AlignIdx != -1) { 7160 return DAG.getNode(PPCISD::QVALIGNI, dl, VT, V1, V2, 7161 DAG.getConstant(AlignIdx, dl, MVT::i32)); 7162 } else if (SVOp->isSplat()) { 7163 int SplatIdx = SVOp->getSplatIndex(); 7164 if (SplatIdx >= 4) { 7165 std::swap(V1, V2); 7166 SplatIdx -= 4; 7167 } 7168 7169 // FIXME: If SplatIdx == 0 and the input came from a load, then there is 7170 // nothing to do. 7171 7172 return DAG.getNode(PPCISD::QVESPLATI, dl, VT, V1, 7173 DAG.getConstant(SplatIdx, dl, MVT::i32)); 7174 } 7175 7176 // Lower this into a qvgpci/qvfperm pair. 7177 7178 // Compute the qvgpci literal 7179 unsigned idx = 0; 7180 for (unsigned i = 0; i < 4; ++i) { 7181 int m = SVOp->getMaskElt(i); 7182 unsigned mm = m >= 0 ? (unsigned) m : i; 7183 idx |= mm << (3-i)*3; 7184 } 7185 7186 SDValue V3 = DAG.getNode(PPCISD::QVGPCI, dl, MVT::v4f64, 7187 DAG.getConstant(idx, dl, MVT::i32)); 7188 return DAG.getNode(PPCISD::QVFPERM, dl, VT, V1, V2, V3); 7189 } 7190 7191 // Cases that are handled by instructions that take permute immediates 7192 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be 7193 // selected by the instruction selector. 7194 if (V2.getOpcode() == ISD::UNDEF) { 7195 if (PPC::isSplatShuffleMask(SVOp, 1) || 7196 PPC::isSplatShuffleMask(SVOp, 2) || 7197 PPC::isSplatShuffleMask(SVOp, 4) || 7198 PPC::isVPKUWUMShuffleMask(SVOp, 1, DAG) || 7199 PPC::isVPKUHUMShuffleMask(SVOp, 1, DAG) || 7200 PPC::isVPKUDUMShuffleMask(SVOp, 1, DAG) || 7201 PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) != -1 || 7202 PPC::isVMRGLShuffleMask(SVOp, 1, 1, DAG) || 7203 PPC::isVMRGLShuffleMask(SVOp, 2, 1, DAG) || 7204 PPC::isVMRGLShuffleMask(SVOp, 4, 1, DAG) || 7205 PPC::isVMRGHShuffleMask(SVOp, 1, 1, DAG) || 7206 PPC::isVMRGHShuffleMask(SVOp, 2, 1, DAG) || 7207 PPC::isVMRGHShuffleMask(SVOp, 4, 1, DAG) || 7208 PPC::isVMRGEOShuffleMask(SVOp, true, 1, DAG) || 7209 PPC::isVMRGEOShuffleMask(SVOp, false, 1, DAG)) { 7210 return Op; 7211 } 7212 } 7213 7214 // Altivec has a variety of "shuffle immediates" that take two vector inputs 7215 // and produce a fixed permutation. If any of these match, do not lower to 7216 // VPERM. 7217 unsigned int ShuffleKind = isLittleEndian ? 2 : 0; 7218 if (PPC::isVPKUWUMShuffleMask(SVOp, ShuffleKind, DAG) || 7219 PPC::isVPKUHUMShuffleMask(SVOp, ShuffleKind, DAG) || 7220 PPC::isVPKUDUMShuffleMask(SVOp, ShuffleKind, DAG) || 7221 PPC::isVSLDOIShuffleMask(SVOp, ShuffleKind, DAG) != -1 || 7222 PPC::isVMRGLShuffleMask(SVOp, 1, ShuffleKind, DAG) || 7223 PPC::isVMRGLShuffleMask(SVOp, 2, ShuffleKind, DAG) || 7224 PPC::isVMRGLShuffleMask(SVOp, 4, ShuffleKind, DAG) || 7225 PPC::isVMRGHShuffleMask(SVOp, 1, ShuffleKind, DAG) || 7226 PPC::isVMRGHShuffleMask(SVOp, 2, ShuffleKind, DAG) || 7227 PPC::isVMRGHShuffleMask(SVOp, 4, ShuffleKind, DAG) || 7228 PPC::isVMRGEOShuffleMask(SVOp, true, ShuffleKind, DAG) || 7229 PPC::isVMRGEOShuffleMask(SVOp, false, ShuffleKind, DAG)) 7230 return Op; 7231 7232 // Check to see if this is a shuffle of 4-byte values. If so, we can use our 7233 // perfect shuffle table to emit an optimal matching sequence. 7234 ArrayRef<int> PermMask = SVOp->getMask(); 7235 7236 unsigned PFIndexes[4]; 7237 bool isFourElementShuffle = true; 7238 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number 7239 unsigned EltNo = 8; // Start out undef. 7240 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte. 7241 if (PermMask[i*4+j] < 0) 7242 continue; // Undef, ignore it. 7243 7244 unsigned ByteSource = PermMask[i*4+j]; 7245 if ((ByteSource & 3) != j) { 7246 isFourElementShuffle = false; 7247 break; 7248 } 7249 7250 if (EltNo == 8) { 7251 EltNo = ByteSource/4; 7252 } else if (EltNo != ByteSource/4) { 7253 isFourElementShuffle = false; 7254 break; 7255 } 7256 } 7257 PFIndexes[i] = EltNo; 7258 } 7259 7260 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the 7261 // perfect shuffle vector to determine if it is cost effective to do this as 7262 // discrete instructions, or whether we should use a vperm. 7263 // For now, we skip this for little endian until such time as we have a 7264 // little-endian perfect shuffle table. 7265 if (isFourElementShuffle && !isLittleEndian) { 7266 // Compute the index in the perfect shuffle table. 7267 unsigned PFTableIndex = 7268 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 7269 7270 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 7271 unsigned Cost = (PFEntry >> 30); 7272 7273 // Determining when to avoid vperm is tricky. Many things affect the cost 7274 // of vperm, particularly how many times the perm mask needs to be computed. 7275 // For example, if the perm mask can be hoisted out of a loop or is already 7276 // used (perhaps because there are multiple permutes with the same shuffle 7277 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of 7278 // the loop requires an extra register. 7279 // 7280 // As a compromise, we only emit discrete instructions if the shuffle can be 7281 // generated in 3 or fewer operations. When we have loop information 7282 // available, if this block is within a loop, we should avoid using vperm 7283 // for 3-operation perms and use a constant pool load instead. 7284 if (Cost < 3) 7285 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 7286 } 7287 7288 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant 7289 // vector that will get spilled to the constant pool. 7290 if (V2.getOpcode() == ISD::UNDEF) V2 = V1; 7291 7292 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except 7293 // that it is in input element units, not in bytes. Convert now. 7294 7295 // For little endian, the order of the input vectors is reversed, and 7296 // the permutation mask is complemented with respect to 31. This is 7297 // necessary to produce proper semantics with the big-endian-biased vperm 7298 // instruction. 7299 EVT EltVT = V1.getValueType().getVectorElementType(); 7300 unsigned BytesPerElement = EltVT.getSizeInBits()/8; 7301 7302 SmallVector<SDValue, 16> ResultMask; 7303 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) { 7304 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i]; 7305 7306 for (unsigned j = 0; j != BytesPerElement; ++j) 7307 if (isLittleEndian) 7308 ResultMask.push_back(DAG.getConstant(31 - (SrcElt*BytesPerElement + j), 7309 dl, MVT::i32)); 7310 else 7311 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement + j, dl, 7312 MVT::i32)); 7313 } 7314 7315 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8, 7316 ResultMask); 7317 if (isLittleEndian) 7318 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), 7319 V2, V1, VPermMask); 7320 else 7321 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), 7322 V1, V2, VPermMask); 7323 } 7324 7325 /// getAltivecCompareInfo - Given an intrinsic, return false if it is not an 7326 /// altivec comparison. If it is, return true and fill in Opc/isDot with 7327 /// information about the intrinsic. 7328 static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc, 7329 bool &isDot, const PPCSubtarget &Subtarget) { 7330 unsigned IntrinsicID = 7331 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue(); 7332 CompareOpc = -1; 7333 isDot = false; 7334 switch (IntrinsicID) { 7335 default: return false; 7336 // Comparison predicates. 7337 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break; 7338 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break; 7339 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break; 7340 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break; 7341 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break; 7342 case Intrinsic::ppc_altivec_vcmpequd_p: 7343 if (Subtarget.hasP8Altivec()) { 7344 CompareOpc = 199; 7345 isDot = 1; 7346 } 7347 else 7348 return false; 7349 7350 break; 7351 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break; 7352 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break; 7353 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break; 7354 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break; 7355 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break; 7356 case Intrinsic::ppc_altivec_vcmpgtsd_p: 7357 if (Subtarget.hasP8Altivec()) { 7358 CompareOpc = 967; 7359 isDot = 1; 7360 } 7361 else 7362 return false; 7363 7364 break; 7365 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break; 7366 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break; 7367 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break; 7368 case Intrinsic::ppc_altivec_vcmpgtud_p: 7369 if (Subtarget.hasP8Altivec()) { 7370 CompareOpc = 711; 7371 isDot = 1; 7372 } 7373 else 7374 return false; 7375 7376 break; 7377 7378 // Normal Comparisons. 7379 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break; 7380 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break; 7381 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break; 7382 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break; 7383 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break; 7384 case Intrinsic::ppc_altivec_vcmpequd: 7385 if (Subtarget.hasP8Altivec()) { 7386 CompareOpc = 199; 7387 isDot = 0; 7388 } 7389 else 7390 return false; 7391 7392 break; 7393 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break; 7394 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break; 7395 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break; 7396 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break; 7397 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break; 7398 case Intrinsic::ppc_altivec_vcmpgtsd: 7399 if (Subtarget.hasP8Altivec()) { 7400 CompareOpc = 967; 7401 isDot = 0; 7402 } 7403 else 7404 return false; 7405 7406 break; 7407 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break; 7408 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break; 7409 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break; 7410 case Intrinsic::ppc_altivec_vcmpgtud: 7411 if (Subtarget.hasP8Altivec()) { 7412 CompareOpc = 711; 7413 isDot = 0; 7414 } 7415 else 7416 return false; 7417 7418 break; 7419 } 7420 return true; 7421 } 7422 7423 /// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom 7424 /// lower, do it, otherwise return null. 7425 SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, 7426 SelectionDAG &DAG) const { 7427 // If this is a lowered altivec predicate compare, CompareOpc is set to the 7428 // opcode number of the comparison. 7429 SDLoc dl(Op); 7430 int CompareOpc; 7431 bool isDot; 7432 if (!getAltivecCompareInfo(Op, CompareOpc, isDot, Subtarget)) 7433 return SDValue(); // Don't custom lower most intrinsics. 7434 7435 // If this is a non-dot comparison, make the VCMP node and we are done. 7436 if (!isDot) { 7437 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(), 7438 Op.getOperand(1), Op.getOperand(2), 7439 DAG.getConstant(CompareOpc, dl, MVT::i32)); 7440 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp); 7441 } 7442 7443 // Create the PPCISD altivec 'dot' comparison node. 7444 SDValue Ops[] = { 7445 Op.getOperand(2), // LHS 7446 Op.getOperand(3), // RHS 7447 DAG.getConstant(CompareOpc, dl, MVT::i32) 7448 }; 7449 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue }; 7450 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops); 7451 7452 // Now that we have the comparison, emit a copy from the CR to a GPR. 7453 // This is flagged to the above dot comparison. 7454 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32, 7455 DAG.getRegister(PPC::CR6, MVT::i32), 7456 CompNode.getValue(1)); 7457 7458 // Unpack the result based on how the target uses it. 7459 unsigned BitNo; // Bit # of CR6. 7460 bool InvertBit; // Invert result? 7461 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) { 7462 default: // Can't happen, don't crash on invalid number though. 7463 case 0: // Return the value of the EQ bit of CR6. 7464 BitNo = 0; InvertBit = false; 7465 break; 7466 case 1: // Return the inverted value of the EQ bit of CR6. 7467 BitNo = 0; InvertBit = true; 7468 break; 7469 case 2: // Return the value of the LT bit of CR6. 7470 BitNo = 2; InvertBit = false; 7471 break; 7472 case 3: // Return the inverted value of the LT bit of CR6. 7473 BitNo = 2; InvertBit = true; 7474 break; 7475 } 7476 7477 // Shift the bit into the low position. 7478 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags, 7479 DAG.getConstant(8 - (3 - BitNo), dl, MVT::i32)); 7480 // Isolate the bit. 7481 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags, 7482 DAG.getConstant(1, dl, MVT::i32)); 7483 7484 // If we are supposed to, toggle the bit. 7485 if (InvertBit) 7486 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags, 7487 DAG.getConstant(1, dl, MVT::i32)); 7488 return Flags; 7489 } 7490 7491 SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, 7492 SelectionDAG &DAG) const { 7493 SDLoc dl(Op); 7494 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int 7495 // instructions), but for smaller types, we need to first extend up to v2i32 7496 // before doing going farther. 7497 if (Op.getValueType() == MVT::v2i64) { 7498 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 7499 if (ExtVT != MVT::v2i32) { 7500 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)); 7501 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op, 7502 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(), 7503 ExtVT.getVectorElementType(), 4))); 7504 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op); 7505 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op, 7506 DAG.getValueType(MVT::v2i32)); 7507 } 7508 7509 return Op; 7510 } 7511 7512 return SDValue(); 7513 } 7514 7515 SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, 7516 SelectionDAG &DAG) const { 7517 SDLoc dl(Op); 7518 // Create a stack slot that is 16-byte aligned. 7519 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); 7520 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false); 7521 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 7522 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 7523 7524 // Store the input value into Value#0 of the stack slot. 7525 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, 7526 Op.getOperand(0), FIdx, MachinePointerInfo(), 7527 false, false, 0); 7528 // Load it out. 7529 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(), 7530 false, false, false, 0); 7531 } 7532 7533 SDValue PPCTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, 7534 SelectionDAG &DAG) const { 7535 SDLoc dl(Op); 7536 SDNode *N = Op.getNode(); 7537 7538 assert(N->getOperand(0).getValueType() == MVT::v4i1 && 7539 "Unknown extract_vector_elt type"); 7540 7541 SDValue Value = N->getOperand(0); 7542 7543 // The first part of this is like the store lowering except that we don't 7544 // need to track the chain. 7545 7546 // The values are now known to be -1 (false) or 1 (true). To convert this 7547 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5). 7548 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5 7549 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value); 7550 7551 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to 7552 // understand how to form the extending load. 7553 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::f64); 7554 FPHalfs = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f64, 7555 FPHalfs, FPHalfs, FPHalfs, FPHalfs); 7556 7557 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs); 7558 7559 // Now convert to an integer and store. 7560 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64, 7561 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32), 7562 Value); 7563 7564 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); 7565 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false); 7566 MachinePointerInfo PtrInfo = 7567 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); 7568 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 7569 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 7570 7571 SDValue StoreChain = DAG.getEntryNode(); 7572 SmallVector<SDValue, 2> Ops; 7573 Ops.push_back(StoreChain); 7574 Ops.push_back(DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32)); 7575 Ops.push_back(Value); 7576 Ops.push_back(FIdx); 7577 7578 SmallVector<EVT, 2> ValueVTs; 7579 ValueVTs.push_back(MVT::Other); // chain 7580 SDVTList VTs = DAG.getVTList(ValueVTs); 7581 7582 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID, 7583 dl, VTs, Ops, MVT::v4i32, PtrInfo); 7584 7585 // Extract the value requested. 7586 unsigned Offset = 4*cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 7587 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType()); 7588 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx); 7589 7590 SDValue IntVal = DAG.getLoad(MVT::i32, dl, StoreChain, Idx, 7591 PtrInfo.getWithOffset(Offset), 7592 false, false, false, 0); 7593 7594 if (!Subtarget.useCRBits()) 7595 return IntVal; 7596 7597 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, IntVal); 7598 } 7599 7600 /// Lowering for QPX v4i1 loads 7601 SDValue PPCTargetLowering::LowerVectorLoad(SDValue Op, 7602 SelectionDAG &DAG) const { 7603 SDLoc dl(Op); 7604 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode()); 7605 SDValue LoadChain = LN->getChain(); 7606 SDValue BasePtr = LN->getBasePtr(); 7607 7608 if (Op.getValueType() == MVT::v4f64 || 7609 Op.getValueType() == MVT::v4f32) { 7610 EVT MemVT = LN->getMemoryVT(); 7611 unsigned Alignment = LN->getAlignment(); 7612 7613 // If this load is properly aligned, then it is legal. 7614 if (Alignment >= MemVT.getStoreSize()) 7615 return Op; 7616 7617 EVT ScalarVT = Op.getValueType().getScalarType(), 7618 ScalarMemVT = MemVT.getScalarType(); 7619 unsigned Stride = ScalarMemVT.getStoreSize(); 7620 7621 SmallVector<SDValue, 8> Vals, LoadChains; 7622 for (unsigned Idx = 0; Idx < 4; ++Idx) { 7623 SDValue Load; 7624 if (ScalarVT != ScalarMemVT) 7625 Load = 7626 DAG.getExtLoad(LN->getExtensionType(), dl, ScalarVT, LoadChain, 7627 BasePtr, 7628 LN->getPointerInfo().getWithOffset(Idx*Stride), 7629 ScalarMemVT, LN->isVolatile(), LN->isNonTemporal(), 7630 LN->isInvariant(), MinAlign(Alignment, Idx*Stride), 7631 LN->getAAInfo()); 7632 else 7633 Load = 7634 DAG.getLoad(ScalarVT, dl, LoadChain, BasePtr, 7635 LN->getPointerInfo().getWithOffset(Idx*Stride), 7636 LN->isVolatile(), LN->isNonTemporal(), 7637 LN->isInvariant(), MinAlign(Alignment, Idx*Stride), 7638 LN->getAAInfo()); 7639 7640 if (Idx == 0 && LN->isIndexed()) { 7641 assert(LN->getAddressingMode() == ISD::PRE_INC && 7642 "Unknown addressing mode on vector load"); 7643 Load = DAG.getIndexedLoad(Load, dl, BasePtr, LN->getOffset(), 7644 LN->getAddressingMode()); 7645 } 7646 7647 Vals.push_back(Load); 7648 LoadChains.push_back(Load.getValue(1)); 7649 7650 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, 7651 DAG.getConstant(Stride, dl, 7652 BasePtr.getValueType())); 7653 } 7654 7655 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains); 7656 SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl, 7657 Op.getValueType(), Vals); 7658 7659 if (LN->isIndexed()) { 7660 SDValue RetOps[] = { Value, Vals[0].getValue(1), TF }; 7661 return DAG.getMergeValues(RetOps, dl); 7662 } 7663 7664 SDValue RetOps[] = { Value, TF }; 7665 return DAG.getMergeValues(RetOps, dl); 7666 } 7667 7668 assert(Op.getValueType() == MVT::v4i1 && "Unknown load to lower"); 7669 assert(LN->isUnindexed() && "Indexed v4i1 loads are not supported"); 7670 7671 // To lower v4i1 from a byte array, we load the byte elements of the 7672 // vector and then reuse the BUILD_VECTOR logic. 7673 7674 SmallVector<SDValue, 4> VectElmts, VectElmtChains; 7675 for (unsigned i = 0; i < 4; ++i) { 7676 SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType()); 7677 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx); 7678 7679 VectElmts.push_back(DAG.getExtLoad(ISD::EXTLOAD, 7680 dl, MVT::i32, LoadChain, Idx, 7681 LN->getPointerInfo().getWithOffset(i), 7682 MVT::i8 /* memory type */, 7683 LN->isVolatile(), LN->isNonTemporal(), 7684 LN->isInvariant(), 7685 1 /* alignment */, LN->getAAInfo())); 7686 VectElmtChains.push_back(VectElmts[i].getValue(1)); 7687 } 7688 7689 LoadChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, VectElmtChains); 7690 SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i1, VectElmts); 7691 7692 SDValue RVals[] = { Value, LoadChain }; 7693 return DAG.getMergeValues(RVals, dl); 7694 } 7695 7696 /// Lowering for QPX v4i1 stores 7697 SDValue PPCTargetLowering::LowerVectorStore(SDValue Op, 7698 SelectionDAG &DAG) const { 7699 SDLoc dl(Op); 7700 StoreSDNode *SN = cast<StoreSDNode>(Op.getNode()); 7701 SDValue StoreChain = SN->getChain(); 7702 SDValue BasePtr = SN->getBasePtr(); 7703 SDValue Value = SN->getValue(); 7704 7705 if (Value.getValueType() == MVT::v4f64 || 7706 Value.getValueType() == MVT::v4f32) { 7707 EVT MemVT = SN->getMemoryVT(); 7708 unsigned Alignment = SN->getAlignment(); 7709 7710 // If this store is properly aligned, then it is legal. 7711 if (Alignment >= MemVT.getStoreSize()) 7712 return Op; 7713 7714 EVT ScalarVT = Value.getValueType().getScalarType(), 7715 ScalarMemVT = MemVT.getScalarType(); 7716 unsigned Stride = ScalarMemVT.getStoreSize(); 7717 7718 SmallVector<SDValue, 8> Stores; 7719 for (unsigned Idx = 0; Idx < 4; ++Idx) { 7720 SDValue Ex = DAG.getNode( 7721 ISD::EXTRACT_VECTOR_ELT, dl, ScalarVT, Value, 7722 DAG.getConstant(Idx, dl, getVectorIdxTy(DAG.getDataLayout()))); 7723 SDValue Store; 7724 if (ScalarVT != ScalarMemVT) 7725 Store = 7726 DAG.getTruncStore(StoreChain, dl, Ex, BasePtr, 7727 SN->getPointerInfo().getWithOffset(Idx*Stride), 7728 ScalarMemVT, SN->isVolatile(), SN->isNonTemporal(), 7729 MinAlign(Alignment, Idx*Stride), SN->getAAInfo()); 7730 else 7731 Store = 7732 DAG.getStore(StoreChain, dl, Ex, BasePtr, 7733 SN->getPointerInfo().getWithOffset(Idx*Stride), 7734 SN->isVolatile(), SN->isNonTemporal(), 7735 MinAlign(Alignment, Idx*Stride), SN->getAAInfo()); 7736 7737 if (Idx == 0 && SN->isIndexed()) { 7738 assert(SN->getAddressingMode() == ISD::PRE_INC && 7739 "Unknown addressing mode on vector store"); 7740 Store = DAG.getIndexedStore(Store, dl, BasePtr, SN->getOffset(), 7741 SN->getAddressingMode()); 7742 } 7743 7744 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, 7745 DAG.getConstant(Stride, dl, 7746 BasePtr.getValueType())); 7747 Stores.push_back(Store); 7748 } 7749 7750 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 7751 7752 if (SN->isIndexed()) { 7753 SDValue RetOps[] = { TF, Stores[0].getValue(1) }; 7754 return DAG.getMergeValues(RetOps, dl); 7755 } 7756 7757 return TF; 7758 } 7759 7760 assert(SN->isUnindexed() && "Indexed v4i1 stores are not supported"); 7761 assert(Value.getValueType() == MVT::v4i1 && "Unknown store to lower"); 7762 7763 // The values are now known to be -1 (false) or 1 (true). To convert this 7764 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5). 7765 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5 7766 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value); 7767 7768 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to 7769 // understand how to form the extending load. 7770 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::f64); 7771 FPHalfs = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f64, 7772 FPHalfs, FPHalfs, FPHalfs, FPHalfs); 7773 7774 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs); 7775 7776 // Now convert to an integer and store. 7777 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64, 7778 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32), 7779 Value); 7780 7781 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); 7782 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false); 7783 MachinePointerInfo PtrInfo = 7784 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); 7785 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 7786 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 7787 7788 SmallVector<SDValue, 2> Ops; 7789 Ops.push_back(StoreChain); 7790 Ops.push_back(DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32)); 7791 Ops.push_back(Value); 7792 Ops.push_back(FIdx); 7793 7794 SmallVector<EVT, 2> ValueVTs; 7795 ValueVTs.push_back(MVT::Other); // chain 7796 SDVTList VTs = DAG.getVTList(ValueVTs); 7797 7798 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID, 7799 dl, VTs, Ops, MVT::v4i32, PtrInfo); 7800 7801 // Move data into the byte array. 7802 SmallVector<SDValue, 4> Loads, LoadChains; 7803 for (unsigned i = 0; i < 4; ++i) { 7804 unsigned Offset = 4*i; 7805 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType()); 7806 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx); 7807 7808 Loads.push_back(DAG.getLoad(MVT::i32, dl, StoreChain, Idx, 7809 PtrInfo.getWithOffset(Offset), 7810 false, false, false, 0)); 7811 LoadChains.push_back(Loads[i].getValue(1)); 7812 } 7813 7814 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains); 7815 7816 SmallVector<SDValue, 4> Stores; 7817 for (unsigned i = 0; i < 4; ++i) { 7818 SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType()); 7819 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx); 7820 7821 Stores.push_back(DAG.getTruncStore(StoreChain, dl, Loads[i], Idx, 7822 SN->getPointerInfo().getWithOffset(i), 7823 MVT::i8 /* memory type */, 7824 SN->isNonTemporal(), SN->isVolatile(), 7825 1 /* alignment */, SN->getAAInfo())); 7826 } 7827 7828 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 7829 7830 return StoreChain; 7831 } 7832 7833 SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const { 7834 SDLoc dl(Op); 7835 if (Op.getValueType() == MVT::v4i32) { 7836 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); 7837 7838 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl); 7839 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt. 7840 7841 SDValue RHSSwap = // = vrlw RHS, 16 7842 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl); 7843 7844 // Shrinkify inputs to v8i16. 7845 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS); 7846 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS); 7847 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap); 7848 7849 // Low parts multiplied together, generating 32-bit results (we ignore the 7850 // top parts). 7851 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh, 7852 LHS, RHS, DAG, dl, MVT::v4i32); 7853 7854 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm, 7855 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32); 7856 // Shift the high parts up 16 bits. 7857 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd, 7858 Neg16, DAG, dl); 7859 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd); 7860 } else if (Op.getValueType() == MVT::v8i16) { 7861 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); 7862 7863 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl); 7864 7865 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm, 7866 LHS, RHS, Zero, DAG, dl); 7867 } else if (Op.getValueType() == MVT::v16i8) { 7868 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); 7869 bool isLittleEndian = Subtarget.isLittleEndian(); 7870 7871 // Multiply the even 8-bit parts, producing 16-bit sums. 7872 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub, 7873 LHS, RHS, DAG, dl, MVT::v8i16); 7874 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts); 7875 7876 // Multiply the odd 8-bit parts, producing 16-bit sums. 7877 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub, 7878 LHS, RHS, DAG, dl, MVT::v8i16); 7879 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts); 7880 7881 // Merge the results together. Because vmuleub and vmuloub are 7882 // instructions with a big-endian bias, we must reverse the 7883 // element numbering and reverse the meaning of "odd" and "even" 7884 // when generating little endian code. 7885 int Ops[16]; 7886 for (unsigned i = 0; i != 8; ++i) { 7887 if (isLittleEndian) { 7888 Ops[i*2 ] = 2*i; 7889 Ops[i*2+1] = 2*i+16; 7890 } else { 7891 Ops[i*2 ] = 2*i+1; 7892 Ops[i*2+1] = 2*i+1+16; 7893 } 7894 } 7895 if (isLittleEndian) 7896 return DAG.getVectorShuffle(MVT::v16i8, dl, OddParts, EvenParts, Ops); 7897 else 7898 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops); 7899 } else { 7900 llvm_unreachable("Unknown mul to lower!"); 7901 } 7902 } 7903 7904 /// LowerOperation - Provide custom lowering hooks for some operations. 7905 /// 7906 SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 7907 switch (Op.getOpcode()) { 7908 default: llvm_unreachable("Wasn't expecting to be able to lower this!"); 7909 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 7910 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 7911 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); 7912 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 7913 case ISD::JumpTable: return LowerJumpTable(Op, DAG); 7914 case ISD::SETCC: return LowerSETCC(Op, DAG); 7915 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG); 7916 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG); 7917 case ISD::VASTART: 7918 return LowerVASTART(Op, DAG, Subtarget); 7919 7920 case ISD::VAARG: 7921 return LowerVAARG(Op, DAG, Subtarget); 7922 7923 case ISD::VACOPY: 7924 return LowerVACOPY(Op, DAG, Subtarget); 7925 7926 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, Subtarget); 7927 case ISD::DYNAMIC_STACKALLOC: 7928 return LowerDYNAMIC_STACKALLOC(Op, DAG, Subtarget); 7929 7930 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG); 7931 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG); 7932 7933 case ISD::LOAD: return LowerLOAD(Op, DAG); 7934 case ISD::STORE: return LowerSTORE(Op, DAG); 7935 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG); 7936 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 7937 case ISD::FP_TO_UINT: 7938 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG, 7939 SDLoc(Op)); 7940 case ISD::UINT_TO_FP: 7941 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG); 7942 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); 7943 7944 // Lower 64-bit shifts. 7945 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG); 7946 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG); 7947 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG); 7948 7949 // Vector-related lowering. 7950 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); 7951 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 7952 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); 7953 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); 7954 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG); 7955 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 7956 case ISD::MUL: return LowerMUL(Op, DAG); 7957 7958 // For counter-based loop handling. 7959 case ISD::INTRINSIC_W_CHAIN: return SDValue(); 7960 7961 // Frame & Return address. 7962 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 7963 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 7964 } 7965 } 7966 7967 void PPCTargetLowering::ReplaceNodeResults(SDNode *N, 7968 SmallVectorImpl<SDValue>&Results, 7969 SelectionDAG &DAG) const { 7970 SDLoc dl(N); 7971 switch (N->getOpcode()) { 7972 default: 7973 llvm_unreachable("Do not know how to custom type legalize this operation!"); 7974 case ISD::READCYCLECOUNTER: { 7975 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other); 7976 SDValue RTB = DAG.getNode(PPCISD::READ_TIME_BASE, dl, VTs, N->getOperand(0)); 7977 7978 Results.push_back(RTB); 7979 Results.push_back(RTB.getValue(1)); 7980 Results.push_back(RTB.getValue(2)); 7981 break; 7982 } 7983 case ISD::INTRINSIC_W_CHAIN: { 7984 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 7985 Intrinsic::ppc_is_decremented_ctr_nonzero) 7986 break; 7987 7988 assert(N->getValueType(0) == MVT::i1 && 7989 "Unexpected result type for CTR decrement intrinsic"); 7990 EVT SVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), 7991 N->getValueType(0)); 7992 SDVTList VTs = DAG.getVTList(SVT, MVT::Other); 7993 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0), 7994 N->getOperand(1)); 7995 7996 Results.push_back(NewInt); 7997 Results.push_back(NewInt.getValue(1)); 7998 break; 7999 } 8000 case ISD::VAARG: { 8001 if (!Subtarget.isSVR4ABI() || Subtarget.isPPC64()) 8002 return; 8003 8004 EVT VT = N->getValueType(0); 8005 8006 if (VT == MVT::i64) { 8007 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, Subtarget); 8008 8009 Results.push_back(NewNode); 8010 Results.push_back(NewNode.getValue(1)); 8011 } 8012 return; 8013 } 8014 case ISD::FP_ROUND_INREG: { 8015 assert(N->getValueType(0) == MVT::ppcf128); 8016 assert(N->getOperand(0).getValueType() == MVT::ppcf128); 8017 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, 8018 MVT::f64, N->getOperand(0), 8019 DAG.getIntPtrConstant(0, dl)); 8020 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, 8021 MVT::f64, N->getOperand(0), 8022 DAG.getIntPtrConstant(1, dl)); 8023 8024 // Add the two halves of the long double in round-to-zero mode. 8025 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi); 8026 8027 // We know the low half is about to be thrown away, so just use something 8028 // convenient. 8029 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128, 8030 FPreg, FPreg)); 8031 return; 8032 } 8033 case ISD::FP_TO_SINT: 8034 case ISD::FP_TO_UINT: 8035 // LowerFP_TO_INT() can only handle f32 and f64. 8036 if (N->getOperand(0).getValueType() == MVT::ppcf128) 8037 return; 8038 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl)); 8039 return; 8040 } 8041 } 8042 8043 8044 //===----------------------------------------------------------------------===// 8045 // Other Lowering Code 8046 //===----------------------------------------------------------------------===// 8047 8048 static Instruction* callIntrinsic(IRBuilder<> &Builder, Intrinsic::ID Id) { 8049 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 8050 Function *Func = Intrinsic::getDeclaration(M, Id); 8051 return Builder.CreateCall(Func, {}); 8052 } 8053 8054 // The mappings for emitLeading/TrailingFence is taken from 8055 // http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html 8056 Instruction* PPCTargetLowering::emitLeadingFence(IRBuilder<> &Builder, 8057 AtomicOrdering Ord, bool IsStore, 8058 bool IsLoad) const { 8059 if (Ord == SequentiallyConsistent) 8060 return callIntrinsic(Builder, Intrinsic::ppc_sync); 8061 if (isAtLeastRelease(Ord)) 8062 return callIntrinsic(Builder, Intrinsic::ppc_lwsync); 8063 return nullptr; 8064 } 8065 8066 Instruction* PPCTargetLowering::emitTrailingFence(IRBuilder<> &Builder, 8067 AtomicOrdering Ord, bool IsStore, 8068 bool IsLoad) const { 8069 if (IsLoad && isAtLeastAcquire(Ord)) 8070 return callIntrinsic(Builder, Intrinsic::ppc_lwsync); 8071 // FIXME: this is too conservative, a dependent branch + isync is enough. 8072 // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html and 8073 // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2011.03.04a.html 8074 // and http://www.cl.cam.ac.uk/~pes20/cppppc/ for justification. 8075 return nullptr; 8076 } 8077 8078 MachineBasicBlock * 8079 PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB, 8080 unsigned AtomicSize, 8081 unsigned BinOpcode) const { 8082 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. 8083 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 8084 8085 auto LoadMnemonic = PPC::LDARX; 8086 auto StoreMnemonic = PPC::STDCX; 8087 switch (AtomicSize) { 8088 default: 8089 llvm_unreachable("Unexpected size of atomic entity"); 8090 case 1: 8091 LoadMnemonic = PPC::LBARX; 8092 StoreMnemonic = PPC::STBCX; 8093 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4"); 8094 break; 8095 case 2: 8096 LoadMnemonic = PPC::LHARX; 8097 StoreMnemonic = PPC::STHCX; 8098 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4"); 8099 break; 8100 case 4: 8101 LoadMnemonic = PPC::LWARX; 8102 StoreMnemonic = PPC::STWCX; 8103 break; 8104 case 8: 8105 LoadMnemonic = PPC::LDARX; 8106 StoreMnemonic = PPC::STDCX; 8107 break; 8108 } 8109 8110 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 8111 MachineFunction *F = BB->getParent(); 8112 MachineFunction::iterator It = BB; 8113 ++It; 8114 8115 unsigned dest = MI->getOperand(0).getReg(); 8116 unsigned ptrA = MI->getOperand(1).getReg(); 8117 unsigned ptrB = MI->getOperand(2).getReg(); 8118 unsigned incr = MI->getOperand(3).getReg(); 8119 DebugLoc dl = MI->getDebugLoc(); 8120 8121 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB); 8122 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); 8123 F->insert(It, loopMBB); 8124 F->insert(It, exitMBB); 8125 exitMBB->splice(exitMBB->begin(), BB, 8126 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 8127 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 8128 8129 MachineRegisterInfo &RegInfo = F->getRegInfo(); 8130 unsigned TmpReg = (!BinOpcode) ? incr : 8131 RegInfo.createVirtualRegister( AtomicSize == 8 ? &PPC::G8RCRegClass 8132 : &PPC::GPRCRegClass); 8133 8134 // thisMBB: 8135 // ... 8136 // fallthrough --> loopMBB 8137 BB->addSuccessor(loopMBB); 8138 8139 // loopMBB: 8140 // l[wd]arx dest, ptr 8141 // add r0, dest, incr 8142 // st[wd]cx. r0, ptr 8143 // bne- loopMBB 8144 // fallthrough --> exitMBB 8145 BB = loopMBB; 8146 BuildMI(BB, dl, TII->get(LoadMnemonic), dest) 8147 .addReg(ptrA).addReg(ptrB); 8148 if (BinOpcode) 8149 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest); 8150 BuildMI(BB, dl, TII->get(StoreMnemonic)) 8151 .addReg(TmpReg).addReg(ptrA).addReg(ptrB); 8152 BuildMI(BB, dl, TII->get(PPC::BCC)) 8153 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB); 8154 BB->addSuccessor(loopMBB); 8155 BB->addSuccessor(exitMBB); 8156 8157 // exitMBB: 8158 // ... 8159 BB = exitMBB; 8160 return BB; 8161 } 8162 8163 MachineBasicBlock * 8164 PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI, 8165 MachineBasicBlock *BB, 8166 bool is8bit, // operation 8167 unsigned BinOpcode) const { 8168 // If we support part-word atomic mnemonics, just use them 8169 if (Subtarget.hasPartwordAtomics()) 8170 return EmitAtomicBinary(MI, BB, is8bit ? 1 : 2, BinOpcode); 8171 8172 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. 8173 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 8174 // In 64 bit mode we have to use 64 bits for addresses, even though the 8175 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address 8176 // registers without caring whether they're 32 or 64, but here we're 8177 // doing actual arithmetic on the addresses. 8178 bool is64bit = Subtarget.isPPC64(); 8179 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO; 8180 8181 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 8182 MachineFunction *F = BB->getParent(); 8183 MachineFunction::iterator It = BB; 8184 ++It; 8185 8186 unsigned dest = MI->getOperand(0).getReg(); 8187 unsigned ptrA = MI->getOperand(1).getReg(); 8188 unsigned ptrB = MI->getOperand(2).getReg(); 8189 unsigned incr = MI->getOperand(3).getReg(); 8190 DebugLoc dl = MI->getDebugLoc(); 8191 8192 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB); 8193 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); 8194 F->insert(It, loopMBB); 8195 F->insert(It, exitMBB); 8196 exitMBB->splice(exitMBB->begin(), BB, 8197 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 8198 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 8199 8200 MachineRegisterInfo &RegInfo = F->getRegInfo(); 8201 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass 8202 : &PPC::GPRCRegClass; 8203 unsigned PtrReg = RegInfo.createVirtualRegister(RC); 8204 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC); 8205 unsigned ShiftReg = RegInfo.createVirtualRegister(RC); 8206 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC); 8207 unsigned MaskReg = RegInfo.createVirtualRegister(RC); 8208 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC); 8209 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC); 8210 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC); 8211 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC); 8212 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC); 8213 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC); 8214 unsigned Ptr1Reg; 8215 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC); 8216 8217 // thisMBB: 8218 // ... 8219 // fallthrough --> loopMBB 8220 BB->addSuccessor(loopMBB); 8221 8222 // The 4-byte load must be aligned, while a char or short may be 8223 // anywhere in the word. Hence all this nasty bookkeeping code. 8224 // add ptr1, ptrA, ptrB [copy if ptrA==0] 8225 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27] 8226 // xori shift, shift1, 24 [16] 8227 // rlwinm ptr, ptr1, 0, 0, 29 8228 // slw incr2, incr, shift 8229 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535] 8230 // slw mask, mask2, shift 8231 // loopMBB: 8232 // lwarx tmpDest, ptr 8233 // add tmp, tmpDest, incr2 8234 // andc tmp2, tmpDest, mask 8235 // and tmp3, tmp, mask 8236 // or tmp4, tmp3, tmp2 8237 // stwcx. tmp4, ptr 8238 // bne- loopMBB 8239 // fallthrough --> exitMBB 8240 // srw dest, tmpDest, shift 8241 if (ptrA != ZeroReg) { 8242 Ptr1Reg = RegInfo.createVirtualRegister(RC); 8243 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg) 8244 .addReg(ptrA).addReg(ptrB); 8245 } else { 8246 Ptr1Reg = ptrB; 8247 } 8248 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg) 8249 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27); 8250 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg) 8251 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16); 8252 if (is64bit) 8253 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg) 8254 .addReg(Ptr1Reg).addImm(0).addImm(61); 8255 else 8256 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg) 8257 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29); 8258 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg) 8259 .addReg(incr).addReg(ShiftReg); 8260 if (is8bit) 8261 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255); 8262 else { 8263 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0); 8264 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535); 8265 } 8266 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg) 8267 .addReg(Mask2Reg).addReg(ShiftReg); 8268 8269 BB = loopMBB; 8270 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg) 8271 .addReg(ZeroReg).addReg(PtrReg); 8272 if (BinOpcode) 8273 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg) 8274 .addReg(Incr2Reg).addReg(TmpDestReg); 8275 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg) 8276 .addReg(TmpDestReg).addReg(MaskReg); 8277 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg) 8278 .addReg(TmpReg).addReg(MaskReg); 8279 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg) 8280 .addReg(Tmp3Reg).addReg(Tmp2Reg); 8281 BuildMI(BB, dl, TII->get(PPC::STWCX)) 8282 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg); 8283 BuildMI(BB, dl, TII->get(PPC::BCC)) 8284 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB); 8285 BB->addSuccessor(loopMBB); 8286 BB->addSuccessor(exitMBB); 8287 8288 // exitMBB: 8289 // ... 8290 BB = exitMBB; 8291 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg) 8292 .addReg(ShiftReg); 8293 return BB; 8294 } 8295 8296 llvm::MachineBasicBlock* 8297 PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI, 8298 MachineBasicBlock *MBB) const { 8299 DebugLoc DL = MI->getDebugLoc(); 8300 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 8301 8302 MachineFunction *MF = MBB->getParent(); 8303 MachineRegisterInfo &MRI = MF->getRegInfo(); 8304 8305 const BasicBlock *BB = MBB->getBasicBlock(); 8306 MachineFunction::iterator I = MBB; 8307 ++I; 8308 8309 // Memory Reference 8310 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin(); 8311 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end(); 8312 8313 unsigned DstReg = MI->getOperand(0).getReg(); 8314 const TargetRegisterClass *RC = MRI.getRegClass(DstReg); 8315 assert(RC->hasType(MVT::i32) && "Invalid destination!"); 8316 unsigned mainDstReg = MRI.createVirtualRegister(RC); 8317 unsigned restoreDstReg = MRI.createVirtualRegister(RC); 8318 8319 MVT PVT = getPointerTy(MF->getDataLayout()); 8320 assert((PVT == MVT::i64 || PVT == MVT::i32) && 8321 "Invalid Pointer Size!"); 8322 // For v = setjmp(buf), we generate 8323 // 8324 // thisMBB: 8325 // SjLjSetup mainMBB 8326 // bl mainMBB 8327 // v_restore = 1 8328 // b sinkMBB 8329 // 8330 // mainMBB: 8331 // buf[LabelOffset] = LR 8332 // v_main = 0 8333 // 8334 // sinkMBB: 8335 // v = phi(main, restore) 8336 // 8337 8338 MachineBasicBlock *thisMBB = MBB; 8339 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB); 8340 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB); 8341 MF->insert(I, mainMBB); 8342 MF->insert(I, sinkMBB); 8343 8344 MachineInstrBuilder MIB; 8345 8346 // Transfer the remainder of BB and its successor edges to sinkMBB. 8347 sinkMBB->splice(sinkMBB->begin(), MBB, 8348 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 8349 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB); 8350 8351 // Note that the structure of the jmp_buf used here is not compatible 8352 // with that used by libc, and is not designed to be. Specifically, it 8353 // stores only those 'reserved' registers that LLVM does not otherwise 8354 // understand how to spill. Also, by convention, by the time this 8355 // intrinsic is called, Clang has already stored the frame address in the 8356 // first slot of the buffer and stack address in the third. Following the 8357 // X86 target code, we'll store the jump address in the second slot. We also 8358 // need to save the TOC pointer (R2) to handle jumps between shared 8359 // libraries, and that will be stored in the fourth slot. The thread 8360 // identifier (R13) is not affected. 8361 8362 // thisMBB: 8363 const int64_t LabelOffset = 1 * PVT.getStoreSize(); 8364 const int64_t TOCOffset = 3 * PVT.getStoreSize(); 8365 const int64_t BPOffset = 4 * PVT.getStoreSize(); 8366 8367 // Prepare IP either in reg. 8368 const TargetRegisterClass *PtrRC = getRegClassFor(PVT); 8369 unsigned LabelReg = MRI.createVirtualRegister(PtrRC); 8370 unsigned BufReg = MI->getOperand(1).getReg(); 8371 8372 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI()) { 8373 setUsesTOCBasePtr(*MBB->getParent()); 8374 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD)) 8375 .addReg(PPC::X2) 8376 .addImm(TOCOffset) 8377 .addReg(BufReg); 8378 MIB.setMemRefs(MMOBegin, MMOEnd); 8379 } 8380 8381 // Naked functions never have a base pointer, and so we use r1. For all 8382 // other functions, this decision must be delayed until during PEI. 8383 unsigned BaseReg; 8384 if (MF->getFunction()->hasFnAttribute(Attribute::Naked)) 8385 BaseReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1; 8386 else 8387 BaseReg = Subtarget.isPPC64() ? PPC::BP8 : PPC::BP; 8388 8389 MIB = BuildMI(*thisMBB, MI, DL, 8390 TII->get(Subtarget.isPPC64() ? PPC::STD : PPC::STW)) 8391 .addReg(BaseReg) 8392 .addImm(BPOffset) 8393 .addReg(BufReg); 8394 MIB.setMemRefs(MMOBegin, MMOEnd); 8395 8396 // Setup 8397 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB); 8398 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo(); 8399 MIB.addRegMask(TRI->getNoPreservedMask()); 8400 8401 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1); 8402 8403 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup)) 8404 .addMBB(mainMBB); 8405 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB); 8406 8407 thisMBB->addSuccessor(mainMBB, /* weight */ 0); 8408 thisMBB->addSuccessor(sinkMBB, /* weight */ 1); 8409 8410 // mainMBB: 8411 // mainDstReg = 0 8412 MIB = 8413 BuildMI(mainMBB, DL, 8414 TII->get(Subtarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg); 8415 8416 // Store IP 8417 if (Subtarget.isPPC64()) { 8418 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD)) 8419 .addReg(LabelReg) 8420 .addImm(LabelOffset) 8421 .addReg(BufReg); 8422 } else { 8423 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW)) 8424 .addReg(LabelReg) 8425 .addImm(LabelOffset) 8426 .addReg(BufReg); 8427 } 8428 8429 MIB.setMemRefs(MMOBegin, MMOEnd); 8430 8431 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0); 8432 mainMBB->addSuccessor(sinkMBB); 8433 8434 // sinkMBB: 8435 BuildMI(*sinkMBB, sinkMBB->begin(), DL, 8436 TII->get(PPC::PHI), DstReg) 8437 .addReg(mainDstReg).addMBB(mainMBB) 8438 .addReg(restoreDstReg).addMBB(thisMBB); 8439 8440 MI->eraseFromParent(); 8441 return sinkMBB; 8442 } 8443 8444 MachineBasicBlock * 8445 PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI, 8446 MachineBasicBlock *MBB) const { 8447 DebugLoc DL = MI->getDebugLoc(); 8448 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 8449 8450 MachineFunction *MF = MBB->getParent(); 8451 MachineRegisterInfo &MRI = MF->getRegInfo(); 8452 8453 // Memory Reference 8454 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin(); 8455 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end(); 8456 8457 MVT PVT = getPointerTy(MF->getDataLayout()); 8458 assert((PVT == MVT::i64 || PVT == MVT::i32) && 8459 "Invalid Pointer Size!"); 8460 8461 const TargetRegisterClass *RC = 8462 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass; 8463 unsigned Tmp = MRI.createVirtualRegister(RC); 8464 // Since FP is only updated here but NOT referenced, it's treated as GPR. 8465 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31; 8466 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1; 8467 unsigned BP = 8468 (PVT == MVT::i64) 8469 ? PPC::X30 8470 : (Subtarget.isSVR4ABI() && 8471 MF->getTarget().getRelocationModel() == Reloc::PIC_ 8472 ? PPC::R29 8473 : PPC::R30); 8474 8475 MachineInstrBuilder MIB; 8476 8477 const int64_t LabelOffset = 1 * PVT.getStoreSize(); 8478 const int64_t SPOffset = 2 * PVT.getStoreSize(); 8479 const int64_t TOCOffset = 3 * PVT.getStoreSize(); 8480 const int64_t BPOffset = 4 * PVT.getStoreSize(); 8481 8482 unsigned BufReg = MI->getOperand(0).getReg(); 8483 8484 // Reload FP (the jumped-to function may not have had a 8485 // frame pointer, and if so, then its r31 will be restored 8486 // as necessary). 8487 if (PVT == MVT::i64) { 8488 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP) 8489 .addImm(0) 8490 .addReg(BufReg); 8491 } else { 8492 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP) 8493 .addImm(0) 8494 .addReg(BufReg); 8495 } 8496 MIB.setMemRefs(MMOBegin, MMOEnd); 8497 8498 // Reload IP 8499 if (PVT == MVT::i64) { 8500 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp) 8501 .addImm(LabelOffset) 8502 .addReg(BufReg); 8503 } else { 8504 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp) 8505 .addImm(LabelOffset) 8506 .addReg(BufReg); 8507 } 8508 MIB.setMemRefs(MMOBegin, MMOEnd); 8509 8510 // Reload SP 8511 if (PVT == MVT::i64) { 8512 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP) 8513 .addImm(SPOffset) 8514 .addReg(BufReg); 8515 } else { 8516 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP) 8517 .addImm(SPOffset) 8518 .addReg(BufReg); 8519 } 8520 MIB.setMemRefs(MMOBegin, MMOEnd); 8521 8522 // Reload BP 8523 if (PVT == MVT::i64) { 8524 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP) 8525 .addImm(BPOffset) 8526 .addReg(BufReg); 8527 } else { 8528 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP) 8529 .addImm(BPOffset) 8530 .addReg(BufReg); 8531 } 8532 MIB.setMemRefs(MMOBegin, MMOEnd); 8533 8534 // Reload TOC 8535 if (PVT == MVT::i64 && Subtarget.isSVR4ABI()) { 8536 setUsesTOCBasePtr(*MBB->getParent()); 8537 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2) 8538 .addImm(TOCOffset) 8539 .addReg(BufReg); 8540 8541 MIB.setMemRefs(MMOBegin, MMOEnd); 8542 } 8543 8544 // Jump 8545 BuildMI(*MBB, MI, DL, 8546 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp); 8547 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR)); 8548 8549 MI->eraseFromParent(); 8550 return MBB; 8551 } 8552 8553 MachineBasicBlock * 8554 PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 8555 MachineBasicBlock *BB) const { 8556 if (MI->getOpcode() == TargetOpcode::STACKMAP || 8557 MI->getOpcode() == TargetOpcode::PATCHPOINT) { 8558 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI() && 8559 MI->getOpcode() == TargetOpcode::PATCHPOINT) { 8560 // Call lowering should have added an r2 operand to indicate a dependence 8561 // on the TOC base pointer value. It can't however, because there is no 8562 // way to mark the dependence as implicit there, and so the stackmap code 8563 // will confuse it with a regular operand. Instead, add the dependence 8564 // here. 8565 setUsesTOCBasePtr(*BB->getParent()); 8566 MI->addOperand(MachineOperand::CreateReg(PPC::X2, false, true)); 8567 } 8568 8569 return emitPatchPoint(MI, BB); 8570 } 8571 8572 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 || 8573 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) { 8574 return emitEHSjLjSetJmp(MI, BB); 8575 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 || 8576 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) { 8577 return emitEHSjLjLongJmp(MI, BB); 8578 } 8579 8580 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 8581 8582 // To "insert" these instructions we actually have to insert their 8583 // control-flow patterns. 8584 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 8585 MachineFunction::iterator It = BB; 8586 ++It; 8587 8588 MachineFunction *F = BB->getParent(); 8589 8590 if (Subtarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 || 8591 MI->getOpcode() == PPC::SELECT_CC_I8 || 8592 MI->getOpcode() == PPC::SELECT_I4 || 8593 MI->getOpcode() == PPC::SELECT_I8)) { 8594 SmallVector<MachineOperand, 2> Cond; 8595 if (MI->getOpcode() == PPC::SELECT_CC_I4 || 8596 MI->getOpcode() == PPC::SELECT_CC_I8) 8597 Cond.push_back(MI->getOperand(4)); 8598 else 8599 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); 8600 Cond.push_back(MI->getOperand(1)); 8601 8602 DebugLoc dl = MI->getDebugLoc(); 8603 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(), 8604 Cond, MI->getOperand(2).getReg(), 8605 MI->getOperand(3).getReg()); 8606 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 || 8607 MI->getOpcode() == PPC::SELECT_CC_I8 || 8608 MI->getOpcode() == PPC::SELECT_CC_F4 || 8609 MI->getOpcode() == PPC::SELECT_CC_F8 || 8610 MI->getOpcode() == PPC::SELECT_CC_QFRC || 8611 MI->getOpcode() == PPC::SELECT_CC_QSRC || 8612 MI->getOpcode() == PPC::SELECT_CC_QBRC || 8613 MI->getOpcode() == PPC::SELECT_CC_VRRC || 8614 MI->getOpcode() == PPC::SELECT_CC_VSFRC || 8615 MI->getOpcode() == PPC::SELECT_CC_VSSRC || 8616 MI->getOpcode() == PPC::SELECT_CC_VSRC || 8617 MI->getOpcode() == PPC::SELECT_I4 || 8618 MI->getOpcode() == PPC::SELECT_I8 || 8619 MI->getOpcode() == PPC::SELECT_F4 || 8620 MI->getOpcode() == PPC::SELECT_F8 || 8621 MI->getOpcode() == PPC::SELECT_QFRC || 8622 MI->getOpcode() == PPC::SELECT_QSRC || 8623 MI->getOpcode() == PPC::SELECT_QBRC || 8624 MI->getOpcode() == PPC::SELECT_VRRC || 8625 MI->getOpcode() == PPC::SELECT_VSFRC || 8626 MI->getOpcode() == PPC::SELECT_VSSRC || 8627 MI->getOpcode() == PPC::SELECT_VSRC) { 8628 // The incoming instruction knows the destination vreg to set, the 8629 // condition code register to branch on, the true/false values to 8630 // select between, and a branch opcode to use. 8631 8632 // thisMBB: 8633 // ... 8634 // TrueVal = ... 8635 // cmpTY ccX, r1, r2 8636 // bCC copy1MBB 8637 // fallthrough --> copy0MBB 8638 MachineBasicBlock *thisMBB = BB; 8639 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 8640 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 8641 DebugLoc dl = MI->getDebugLoc(); 8642 F->insert(It, copy0MBB); 8643 F->insert(It, sinkMBB); 8644 8645 // Transfer the remainder of BB and its successor edges to sinkMBB. 8646 sinkMBB->splice(sinkMBB->begin(), BB, 8647 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 8648 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 8649 8650 // Next, add the true and fallthrough blocks as its successors. 8651 BB->addSuccessor(copy0MBB); 8652 BB->addSuccessor(sinkMBB); 8653 8654 if (MI->getOpcode() == PPC::SELECT_I4 || 8655 MI->getOpcode() == PPC::SELECT_I8 || 8656 MI->getOpcode() == PPC::SELECT_F4 || 8657 MI->getOpcode() == PPC::SELECT_F8 || 8658 MI->getOpcode() == PPC::SELECT_QFRC || 8659 MI->getOpcode() == PPC::SELECT_QSRC || 8660 MI->getOpcode() == PPC::SELECT_QBRC || 8661 MI->getOpcode() == PPC::SELECT_VRRC || 8662 MI->getOpcode() == PPC::SELECT_VSFRC || 8663 MI->getOpcode() == PPC::SELECT_VSSRC || 8664 MI->getOpcode() == PPC::SELECT_VSRC) { 8665 BuildMI(BB, dl, TII->get(PPC::BC)) 8666 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB); 8667 } else { 8668 unsigned SelectPred = MI->getOperand(4).getImm(); 8669 BuildMI(BB, dl, TII->get(PPC::BCC)) 8670 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB); 8671 } 8672 8673 // copy0MBB: 8674 // %FalseValue = ... 8675 // # fallthrough to sinkMBB 8676 BB = copy0MBB; 8677 8678 // Update machine-CFG edges 8679 BB->addSuccessor(sinkMBB); 8680 8681 // sinkMBB: 8682 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 8683 // ... 8684 BB = sinkMBB; 8685 BuildMI(*BB, BB->begin(), dl, 8686 TII->get(PPC::PHI), MI->getOperand(0).getReg()) 8687 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB) 8688 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); 8689 } else if (MI->getOpcode() == PPC::ReadTB) { 8690 // To read the 64-bit time-base register on a 32-bit target, we read the 8691 // two halves. Should the counter have wrapped while it was being read, we 8692 // need to try again. 8693 // ... 8694 // readLoop: 8695 // mfspr Rx,TBU # load from TBU 8696 // mfspr Ry,TB # load from TB 8697 // mfspr Rz,TBU # load from TBU 8698 // cmpw crX,Rx,Rz # check if ‘old’=’new’ 8699 // bne readLoop # branch if they're not equal 8700 // ... 8701 8702 MachineBasicBlock *readMBB = F->CreateMachineBasicBlock(LLVM_BB); 8703 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 8704 DebugLoc dl = MI->getDebugLoc(); 8705 F->insert(It, readMBB); 8706 F->insert(It, sinkMBB); 8707 8708 // Transfer the remainder of BB and its successor edges to sinkMBB. 8709 sinkMBB->splice(sinkMBB->begin(), BB, 8710 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 8711 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 8712 8713 BB->addSuccessor(readMBB); 8714 BB = readMBB; 8715 8716 MachineRegisterInfo &RegInfo = F->getRegInfo(); 8717 unsigned ReadAgainReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass); 8718 unsigned LoReg = MI->getOperand(0).getReg(); 8719 unsigned HiReg = MI->getOperand(1).getReg(); 8720 8721 BuildMI(BB, dl, TII->get(PPC::MFSPR), HiReg).addImm(269); 8722 BuildMI(BB, dl, TII->get(PPC::MFSPR), LoReg).addImm(268); 8723 BuildMI(BB, dl, TII->get(PPC::MFSPR), ReadAgainReg).addImm(269); 8724 8725 unsigned CmpReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass); 8726 8727 BuildMI(BB, dl, TII->get(PPC::CMPW), CmpReg) 8728 .addReg(HiReg).addReg(ReadAgainReg); 8729 BuildMI(BB, dl, TII->get(PPC::BCC)) 8730 .addImm(PPC::PRED_NE).addReg(CmpReg).addMBB(readMBB); 8731 8732 BB->addSuccessor(readMBB); 8733 BB->addSuccessor(sinkMBB); 8734 } 8735 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8) 8736 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4); 8737 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16) 8738 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4); 8739 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32) 8740 BB = EmitAtomicBinary(MI, BB, 4, PPC::ADD4); 8741 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64) 8742 BB = EmitAtomicBinary(MI, BB, 8, PPC::ADD8); 8743 8744 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8) 8745 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND); 8746 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16) 8747 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND); 8748 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32) 8749 BB = EmitAtomicBinary(MI, BB, 4, PPC::AND); 8750 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64) 8751 BB = EmitAtomicBinary(MI, BB, 8, PPC::AND8); 8752 8753 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8) 8754 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR); 8755 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16) 8756 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR); 8757 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32) 8758 BB = EmitAtomicBinary(MI, BB, 4, PPC::OR); 8759 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64) 8760 BB = EmitAtomicBinary(MI, BB, 8, PPC::OR8); 8761 8762 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8) 8763 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR); 8764 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16) 8765 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR); 8766 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32) 8767 BB = EmitAtomicBinary(MI, BB, 4, PPC::XOR); 8768 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64) 8769 BB = EmitAtomicBinary(MI, BB, 8, PPC::XOR8); 8770 8771 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8) 8772 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::NAND); 8773 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16) 8774 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::NAND); 8775 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32) 8776 BB = EmitAtomicBinary(MI, BB, 4, PPC::NAND); 8777 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64) 8778 BB = EmitAtomicBinary(MI, BB, 8, PPC::NAND8); 8779 8780 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8) 8781 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF); 8782 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16) 8783 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF); 8784 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32) 8785 BB = EmitAtomicBinary(MI, BB, 4, PPC::SUBF); 8786 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64) 8787 BB = EmitAtomicBinary(MI, BB, 8, PPC::SUBF8); 8788 8789 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8) 8790 BB = EmitPartwordAtomicBinary(MI, BB, true, 0); 8791 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16) 8792 BB = EmitPartwordAtomicBinary(MI, BB, false, 0); 8793 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32) 8794 BB = EmitAtomicBinary(MI, BB, 4, 0); 8795 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64) 8796 BB = EmitAtomicBinary(MI, BB, 8, 0); 8797 8798 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 || 8799 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64 || 8800 (Subtarget.hasPartwordAtomics() && 8801 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8) || 8802 (Subtarget.hasPartwordAtomics() && 8803 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16)) { 8804 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64; 8805 8806 auto LoadMnemonic = PPC::LDARX; 8807 auto StoreMnemonic = PPC::STDCX; 8808 switch(MI->getOpcode()) { 8809 default: 8810 llvm_unreachable("Compare and swap of unknown size"); 8811 case PPC::ATOMIC_CMP_SWAP_I8: 8812 LoadMnemonic = PPC::LBARX; 8813 StoreMnemonic = PPC::STBCX; 8814 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics."); 8815 break; 8816 case PPC::ATOMIC_CMP_SWAP_I16: 8817 LoadMnemonic = PPC::LHARX; 8818 StoreMnemonic = PPC::STHCX; 8819 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics."); 8820 break; 8821 case PPC::ATOMIC_CMP_SWAP_I32: 8822 LoadMnemonic = PPC::LWARX; 8823 StoreMnemonic = PPC::STWCX; 8824 break; 8825 case PPC::ATOMIC_CMP_SWAP_I64: 8826 LoadMnemonic = PPC::LDARX; 8827 StoreMnemonic = PPC::STDCX; 8828 break; 8829 } 8830 unsigned dest = MI->getOperand(0).getReg(); 8831 unsigned ptrA = MI->getOperand(1).getReg(); 8832 unsigned ptrB = MI->getOperand(2).getReg(); 8833 unsigned oldval = MI->getOperand(3).getReg(); 8834 unsigned newval = MI->getOperand(4).getReg(); 8835 DebugLoc dl = MI->getDebugLoc(); 8836 8837 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB); 8838 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB); 8839 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB); 8840 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); 8841 F->insert(It, loop1MBB); 8842 F->insert(It, loop2MBB); 8843 F->insert(It, midMBB); 8844 F->insert(It, exitMBB); 8845 exitMBB->splice(exitMBB->begin(), BB, 8846 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 8847 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 8848 8849 // thisMBB: 8850 // ... 8851 // fallthrough --> loopMBB 8852 BB->addSuccessor(loop1MBB); 8853 8854 // loop1MBB: 8855 // l[bhwd]arx dest, ptr 8856 // cmp[wd] dest, oldval 8857 // bne- midMBB 8858 // loop2MBB: 8859 // st[bhwd]cx. newval, ptr 8860 // bne- loopMBB 8861 // b exitBB 8862 // midMBB: 8863 // st[bhwd]cx. dest, ptr 8864 // exitBB: 8865 BB = loop1MBB; 8866 BuildMI(BB, dl, TII->get(LoadMnemonic), dest) 8867 .addReg(ptrA).addReg(ptrB); 8868 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0) 8869 .addReg(oldval).addReg(dest); 8870 BuildMI(BB, dl, TII->get(PPC::BCC)) 8871 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB); 8872 BB->addSuccessor(loop2MBB); 8873 BB->addSuccessor(midMBB); 8874 8875 BB = loop2MBB; 8876 BuildMI(BB, dl, TII->get(StoreMnemonic)) 8877 .addReg(newval).addReg(ptrA).addReg(ptrB); 8878 BuildMI(BB, dl, TII->get(PPC::BCC)) 8879 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB); 8880 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB); 8881 BB->addSuccessor(loop1MBB); 8882 BB->addSuccessor(exitMBB); 8883 8884 BB = midMBB; 8885 BuildMI(BB, dl, TII->get(StoreMnemonic)) 8886 .addReg(dest).addReg(ptrA).addReg(ptrB); 8887 BB->addSuccessor(exitMBB); 8888 8889 // exitMBB: 8890 // ... 8891 BB = exitMBB; 8892 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 || 8893 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) { 8894 // We must use 64-bit registers for addresses when targeting 64-bit, 8895 // since we're actually doing arithmetic on them. Other registers 8896 // can be 32-bit. 8897 bool is64bit = Subtarget.isPPC64(); 8898 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8; 8899 8900 unsigned dest = MI->getOperand(0).getReg(); 8901 unsigned ptrA = MI->getOperand(1).getReg(); 8902 unsigned ptrB = MI->getOperand(2).getReg(); 8903 unsigned oldval = MI->getOperand(3).getReg(); 8904 unsigned newval = MI->getOperand(4).getReg(); 8905 DebugLoc dl = MI->getDebugLoc(); 8906 8907 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB); 8908 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB); 8909 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB); 8910 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); 8911 F->insert(It, loop1MBB); 8912 F->insert(It, loop2MBB); 8913 F->insert(It, midMBB); 8914 F->insert(It, exitMBB); 8915 exitMBB->splice(exitMBB->begin(), BB, 8916 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 8917 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 8918 8919 MachineRegisterInfo &RegInfo = F->getRegInfo(); 8920 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass 8921 : &PPC::GPRCRegClass; 8922 unsigned PtrReg = RegInfo.createVirtualRegister(RC); 8923 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC); 8924 unsigned ShiftReg = RegInfo.createVirtualRegister(RC); 8925 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC); 8926 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC); 8927 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC); 8928 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC); 8929 unsigned MaskReg = RegInfo.createVirtualRegister(RC); 8930 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC); 8931 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC); 8932 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC); 8933 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC); 8934 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC); 8935 unsigned Ptr1Reg; 8936 unsigned TmpReg = RegInfo.createVirtualRegister(RC); 8937 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO; 8938 // thisMBB: 8939 // ... 8940 // fallthrough --> loopMBB 8941 BB->addSuccessor(loop1MBB); 8942 8943 // The 4-byte load must be aligned, while a char or short may be 8944 // anywhere in the word. Hence all this nasty bookkeeping code. 8945 // add ptr1, ptrA, ptrB [copy if ptrA==0] 8946 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27] 8947 // xori shift, shift1, 24 [16] 8948 // rlwinm ptr, ptr1, 0, 0, 29 8949 // slw newval2, newval, shift 8950 // slw oldval2, oldval,shift 8951 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535] 8952 // slw mask, mask2, shift 8953 // and newval3, newval2, mask 8954 // and oldval3, oldval2, mask 8955 // loop1MBB: 8956 // lwarx tmpDest, ptr 8957 // and tmp, tmpDest, mask 8958 // cmpw tmp, oldval3 8959 // bne- midMBB 8960 // loop2MBB: 8961 // andc tmp2, tmpDest, mask 8962 // or tmp4, tmp2, newval3 8963 // stwcx. tmp4, ptr 8964 // bne- loop1MBB 8965 // b exitBB 8966 // midMBB: 8967 // stwcx. tmpDest, ptr 8968 // exitBB: 8969 // srw dest, tmpDest, shift 8970 if (ptrA != ZeroReg) { 8971 Ptr1Reg = RegInfo.createVirtualRegister(RC); 8972 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg) 8973 .addReg(ptrA).addReg(ptrB); 8974 } else { 8975 Ptr1Reg = ptrB; 8976 } 8977 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg) 8978 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27); 8979 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg) 8980 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16); 8981 if (is64bit) 8982 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg) 8983 .addReg(Ptr1Reg).addImm(0).addImm(61); 8984 else 8985 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg) 8986 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29); 8987 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg) 8988 .addReg(newval).addReg(ShiftReg); 8989 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg) 8990 .addReg(oldval).addReg(ShiftReg); 8991 if (is8bit) 8992 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255); 8993 else { 8994 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0); 8995 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg) 8996 .addReg(Mask3Reg).addImm(65535); 8997 } 8998 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg) 8999 .addReg(Mask2Reg).addReg(ShiftReg); 9000 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg) 9001 .addReg(NewVal2Reg).addReg(MaskReg); 9002 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg) 9003 .addReg(OldVal2Reg).addReg(MaskReg); 9004 9005 BB = loop1MBB; 9006 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg) 9007 .addReg(ZeroReg).addReg(PtrReg); 9008 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg) 9009 .addReg(TmpDestReg).addReg(MaskReg); 9010 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0) 9011 .addReg(TmpReg).addReg(OldVal3Reg); 9012 BuildMI(BB, dl, TII->get(PPC::BCC)) 9013 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB); 9014 BB->addSuccessor(loop2MBB); 9015 BB->addSuccessor(midMBB); 9016 9017 BB = loop2MBB; 9018 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg) 9019 .addReg(TmpDestReg).addReg(MaskReg); 9020 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg) 9021 .addReg(Tmp2Reg).addReg(NewVal3Reg); 9022 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg) 9023 .addReg(ZeroReg).addReg(PtrReg); 9024 BuildMI(BB, dl, TII->get(PPC::BCC)) 9025 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB); 9026 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB); 9027 BB->addSuccessor(loop1MBB); 9028 BB->addSuccessor(exitMBB); 9029 9030 BB = midMBB; 9031 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg) 9032 .addReg(ZeroReg).addReg(PtrReg); 9033 BB->addSuccessor(exitMBB); 9034 9035 // exitMBB: 9036 // ... 9037 BB = exitMBB; 9038 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg) 9039 .addReg(ShiftReg); 9040 } else if (MI->getOpcode() == PPC::FADDrtz) { 9041 // This pseudo performs an FADD with rounding mode temporarily forced 9042 // to round-to-zero. We emit this via custom inserter since the FPSCR 9043 // is not modeled at the SelectionDAG level. 9044 unsigned Dest = MI->getOperand(0).getReg(); 9045 unsigned Src1 = MI->getOperand(1).getReg(); 9046 unsigned Src2 = MI->getOperand(2).getReg(); 9047 DebugLoc dl = MI->getDebugLoc(); 9048 9049 MachineRegisterInfo &RegInfo = F->getRegInfo(); 9050 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass); 9051 9052 // Save FPSCR value. 9053 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg); 9054 9055 // Set rounding mode to round-to-zero. 9056 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31); 9057 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30); 9058 9059 // Perform addition. 9060 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2); 9061 9062 // Restore FPSCR value. 9063 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSFb)).addImm(1).addReg(MFFSReg); 9064 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT || 9065 MI->getOpcode() == PPC::ANDIo_1_GT_BIT || 9066 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 || 9067 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) { 9068 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 || 9069 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ? 9070 PPC::ANDIo8 : PPC::ANDIo; 9071 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT || 9072 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8); 9073 9074 MachineRegisterInfo &RegInfo = F->getRegInfo(); 9075 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ? 9076 &PPC::GPRCRegClass : 9077 &PPC::G8RCRegClass); 9078 9079 DebugLoc dl = MI->getDebugLoc(); 9080 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest) 9081 .addReg(MI->getOperand(1).getReg()).addImm(1); 9082 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), 9083 MI->getOperand(0).getReg()) 9084 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT); 9085 } else if (MI->getOpcode() == PPC::TCHECK_RET) { 9086 DebugLoc Dl = MI->getDebugLoc(); 9087 MachineRegisterInfo &RegInfo = F->getRegInfo(); 9088 unsigned CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass); 9089 BuildMI(*BB, MI, Dl, TII->get(PPC::TCHECK), CRReg); 9090 return BB; 9091 } else { 9092 llvm_unreachable("Unexpected instr type to insert"); 9093 } 9094 9095 MI->eraseFromParent(); // The pseudo instruction is gone now. 9096 return BB; 9097 } 9098 9099 //===----------------------------------------------------------------------===// 9100 // Target Optimization Hooks 9101 //===----------------------------------------------------------------------===// 9102 9103 static std::string getRecipOp(const char *Base, EVT VT) { 9104 std::string RecipOp(Base); 9105 if (VT.getScalarType() == MVT::f64) 9106 RecipOp += "d"; 9107 else 9108 RecipOp += "f"; 9109 9110 if (VT.isVector()) 9111 RecipOp = "vec-" + RecipOp; 9112 9113 return RecipOp; 9114 } 9115 9116 SDValue PPCTargetLowering::getRsqrtEstimate(SDValue Operand, 9117 DAGCombinerInfo &DCI, 9118 unsigned &RefinementSteps, 9119 bool &UseOneConstNR) const { 9120 EVT VT = Operand.getValueType(); 9121 if ((VT == MVT::f32 && Subtarget.hasFRSQRTES()) || 9122 (VT == MVT::f64 && Subtarget.hasFRSQRTE()) || 9123 (VT == MVT::v4f32 && Subtarget.hasAltivec()) || 9124 (VT == MVT::v2f64 && Subtarget.hasVSX()) || 9125 (VT == MVT::v4f32 && Subtarget.hasQPX()) || 9126 (VT == MVT::v4f64 && Subtarget.hasQPX())) { 9127 TargetRecip Recips = DCI.DAG.getTarget().Options.Reciprocals; 9128 std::string RecipOp = getRecipOp("sqrt", VT); 9129 if (!Recips.isEnabled(RecipOp)) 9130 return SDValue(); 9131 9132 RefinementSteps = Recips.getRefinementSteps(RecipOp); 9133 UseOneConstNR = true; 9134 return DCI.DAG.getNode(PPCISD::FRSQRTE, SDLoc(Operand), VT, Operand); 9135 } 9136 return SDValue(); 9137 } 9138 9139 SDValue PPCTargetLowering::getRecipEstimate(SDValue Operand, 9140 DAGCombinerInfo &DCI, 9141 unsigned &RefinementSteps) const { 9142 EVT VT = Operand.getValueType(); 9143 if ((VT == MVT::f32 && Subtarget.hasFRES()) || 9144 (VT == MVT::f64 && Subtarget.hasFRE()) || 9145 (VT == MVT::v4f32 && Subtarget.hasAltivec()) || 9146 (VT == MVT::v2f64 && Subtarget.hasVSX()) || 9147 (VT == MVT::v4f32 && Subtarget.hasQPX()) || 9148 (VT == MVT::v4f64 && Subtarget.hasQPX())) { 9149 TargetRecip Recips = DCI.DAG.getTarget().Options.Reciprocals; 9150 std::string RecipOp = getRecipOp("div", VT); 9151 if (!Recips.isEnabled(RecipOp)) 9152 return SDValue(); 9153 9154 RefinementSteps = Recips.getRefinementSteps(RecipOp); 9155 return DCI.DAG.getNode(PPCISD::FRE, SDLoc(Operand), VT, Operand); 9156 } 9157 return SDValue(); 9158 } 9159 9160 unsigned PPCTargetLowering::combineRepeatedFPDivisors() const { 9161 // Note: This functionality is used only when unsafe-fp-math is enabled, and 9162 // on cores with reciprocal estimates (which are used when unsafe-fp-math is 9163 // enabled for division), this functionality is redundant with the default 9164 // combiner logic (once the division -> reciprocal/multiply transformation 9165 // has taken place). As a result, this matters more for older cores than for 9166 // newer ones. 9167 9168 // Combine multiple FDIVs with the same divisor into multiple FMULs by the 9169 // reciprocal if there are two or more FDIVs (for embedded cores with only 9170 // one FP pipeline) for three or more FDIVs (for generic OOO cores). 9171 switch (Subtarget.getDarwinDirective()) { 9172 default: 9173 return 3; 9174 case PPC::DIR_440: 9175 case PPC::DIR_A2: 9176 case PPC::DIR_E500mc: 9177 case PPC::DIR_E5500: 9178 return 2; 9179 } 9180 } 9181 9182 static bool isConsecutiveLSLoc(SDValue Loc, EVT VT, LSBaseSDNode *Base, 9183 unsigned Bytes, int Dist, 9184 SelectionDAG &DAG) { 9185 if (VT.getSizeInBits() / 8 != Bytes) 9186 return false; 9187 9188 SDValue BaseLoc = Base->getBasePtr(); 9189 if (Loc.getOpcode() == ISD::FrameIndex) { 9190 if (BaseLoc.getOpcode() != ISD::FrameIndex) 9191 return false; 9192 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 9193 int FI = cast<FrameIndexSDNode>(Loc)->getIndex(); 9194 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex(); 9195 int FS = MFI->getObjectSize(FI); 9196 int BFS = MFI->getObjectSize(BFI); 9197 if (FS != BFS || FS != (int)Bytes) return false; 9198 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes); 9199 } 9200 9201 // Handle X+C 9202 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc && 9203 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes) 9204 return true; 9205 9206 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9207 const GlobalValue *GV1 = nullptr; 9208 const GlobalValue *GV2 = nullptr; 9209 int64_t Offset1 = 0; 9210 int64_t Offset2 = 0; 9211 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1); 9212 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2); 9213 if (isGA1 && isGA2 && GV1 == GV2) 9214 return Offset1 == (Offset2 + Dist*Bytes); 9215 return false; 9216 } 9217 9218 // Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does 9219 // not enforce equality of the chain operands. 9220 static bool isConsecutiveLS(SDNode *N, LSBaseSDNode *Base, 9221 unsigned Bytes, int Dist, 9222 SelectionDAG &DAG) { 9223 if (LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(N)) { 9224 EVT VT = LS->getMemoryVT(); 9225 SDValue Loc = LS->getBasePtr(); 9226 return isConsecutiveLSLoc(Loc, VT, Base, Bytes, Dist, DAG); 9227 } 9228 9229 if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN) { 9230 EVT VT; 9231 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 9232 default: return false; 9233 case Intrinsic::ppc_qpx_qvlfd: 9234 case Intrinsic::ppc_qpx_qvlfda: 9235 VT = MVT::v4f64; 9236 break; 9237 case Intrinsic::ppc_qpx_qvlfs: 9238 case Intrinsic::ppc_qpx_qvlfsa: 9239 VT = MVT::v4f32; 9240 break; 9241 case Intrinsic::ppc_qpx_qvlfcd: 9242 case Intrinsic::ppc_qpx_qvlfcda: 9243 VT = MVT::v2f64; 9244 break; 9245 case Intrinsic::ppc_qpx_qvlfcs: 9246 case Intrinsic::ppc_qpx_qvlfcsa: 9247 VT = MVT::v2f32; 9248 break; 9249 case Intrinsic::ppc_qpx_qvlfiwa: 9250 case Intrinsic::ppc_qpx_qvlfiwz: 9251 case Intrinsic::ppc_altivec_lvx: 9252 case Intrinsic::ppc_altivec_lvxl: 9253 case Intrinsic::ppc_vsx_lxvw4x: 9254 VT = MVT::v4i32; 9255 break; 9256 case Intrinsic::ppc_vsx_lxvd2x: 9257 VT = MVT::v2f64; 9258 break; 9259 case Intrinsic::ppc_altivec_lvebx: 9260 VT = MVT::i8; 9261 break; 9262 case Intrinsic::ppc_altivec_lvehx: 9263 VT = MVT::i16; 9264 break; 9265 case Intrinsic::ppc_altivec_lvewx: 9266 VT = MVT::i32; 9267 break; 9268 } 9269 9270 return isConsecutiveLSLoc(N->getOperand(2), VT, Base, Bytes, Dist, DAG); 9271 } 9272 9273 if (N->getOpcode() == ISD::INTRINSIC_VOID) { 9274 EVT VT; 9275 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 9276 default: return false; 9277 case Intrinsic::ppc_qpx_qvstfd: 9278 case Intrinsic::ppc_qpx_qvstfda: 9279 VT = MVT::v4f64; 9280 break; 9281 case Intrinsic::ppc_qpx_qvstfs: 9282 case Intrinsic::ppc_qpx_qvstfsa: 9283 VT = MVT::v4f32; 9284 break; 9285 case Intrinsic::ppc_qpx_qvstfcd: 9286 case Intrinsic::ppc_qpx_qvstfcda: 9287 VT = MVT::v2f64; 9288 break; 9289 case Intrinsic::ppc_qpx_qvstfcs: 9290 case Intrinsic::ppc_qpx_qvstfcsa: 9291 VT = MVT::v2f32; 9292 break; 9293 case Intrinsic::ppc_qpx_qvstfiw: 9294 case Intrinsic::ppc_qpx_qvstfiwa: 9295 case Intrinsic::ppc_altivec_stvx: 9296 case Intrinsic::ppc_altivec_stvxl: 9297 case Intrinsic::ppc_vsx_stxvw4x: 9298 VT = MVT::v4i32; 9299 break; 9300 case Intrinsic::ppc_vsx_stxvd2x: 9301 VT = MVT::v2f64; 9302 break; 9303 case Intrinsic::ppc_altivec_stvebx: 9304 VT = MVT::i8; 9305 break; 9306 case Intrinsic::ppc_altivec_stvehx: 9307 VT = MVT::i16; 9308 break; 9309 case Intrinsic::ppc_altivec_stvewx: 9310 VT = MVT::i32; 9311 break; 9312 } 9313 9314 return isConsecutiveLSLoc(N->getOperand(3), VT, Base, Bytes, Dist, DAG); 9315 } 9316 9317 return false; 9318 } 9319 9320 // Return true is there is a nearyby consecutive load to the one provided 9321 // (regardless of alignment). We search up and down the chain, looking though 9322 // token factors and other loads (but nothing else). As a result, a true result 9323 // indicates that it is safe to create a new consecutive load adjacent to the 9324 // load provided. 9325 static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) { 9326 SDValue Chain = LD->getChain(); 9327 EVT VT = LD->getMemoryVT(); 9328 9329 SmallSet<SDNode *, 16> LoadRoots; 9330 SmallVector<SDNode *, 8> Queue(1, Chain.getNode()); 9331 SmallSet<SDNode *, 16> Visited; 9332 9333 // First, search up the chain, branching to follow all token-factor operands. 9334 // If we find a consecutive load, then we're done, otherwise, record all 9335 // nodes just above the top-level loads and token factors. 9336 while (!Queue.empty()) { 9337 SDNode *ChainNext = Queue.pop_back_val(); 9338 if (!Visited.insert(ChainNext).second) 9339 continue; 9340 9341 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(ChainNext)) { 9342 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG)) 9343 return true; 9344 9345 if (!Visited.count(ChainLD->getChain().getNode())) 9346 Queue.push_back(ChainLD->getChain().getNode()); 9347 } else if (ChainNext->getOpcode() == ISD::TokenFactor) { 9348 for (const SDUse &O : ChainNext->ops()) 9349 if (!Visited.count(O.getNode())) 9350 Queue.push_back(O.getNode()); 9351 } else 9352 LoadRoots.insert(ChainNext); 9353 } 9354 9355 // Second, search down the chain, starting from the top-level nodes recorded 9356 // in the first phase. These top-level nodes are the nodes just above all 9357 // loads and token factors. Starting with their uses, recursively look though 9358 // all loads (just the chain uses) and token factors to find a consecutive 9359 // load. 9360 Visited.clear(); 9361 Queue.clear(); 9362 9363 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(), 9364 IE = LoadRoots.end(); I != IE; ++I) { 9365 Queue.push_back(*I); 9366 9367 while (!Queue.empty()) { 9368 SDNode *LoadRoot = Queue.pop_back_val(); 9369 if (!Visited.insert(LoadRoot).second) 9370 continue; 9371 9372 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(LoadRoot)) 9373 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG)) 9374 return true; 9375 9376 for (SDNode::use_iterator UI = LoadRoot->use_begin(), 9377 UE = LoadRoot->use_end(); UI != UE; ++UI) 9378 if (((isa<MemSDNode>(*UI) && 9379 cast<MemSDNode>(*UI)->getChain().getNode() == LoadRoot) || 9380 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI)) 9381 Queue.push_back(*UI); 9382 } 9383 } 9384 9385 return false; 9386 } 9387 9388 SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N, 9389 DAGCombinerInfo &DCI) const { 9390 SelectionDAG &DAG = DCI.DAG; 9391 SDLoc dl(N); 9392 9393 assert(Subtarget.useCRBits() && "Expecting to be tracking CR bits"); 9394 // If we're tracking CR bits, we need to be careful that we don't have: 9395 // trunc(binary-ops(zext(x), zext(y))) 9396 // or 9397 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...) 9398 // such that we're unnecessarily moving things into GPRs when it would be 9399 // better to keep them in CR bits. 9400 9401 // Note that trunc here can be an actual i1 trunc, or can be the effective 9402 // truncation that comes from a setcc or select_cc. 9403 if (N->getOpcode() == ISD::TRUNCATE && 9404 N->getValueType(0) != MVT::i1) 9405 return SDValue(); 9406 9407 if (N->getOperand(0).getValueType() != MVT::i32 && 9408 N->getOperand(0).getValueType() != MVT::i64) 9409 return SDValue(); 9410 9411 if (N->getOpcode() == ISD::SETCC || 9412 N->getOpcode() == ISD::SELECT_CC) { 9413 // If we're looking at a comparison, then we need to make sure that the 9414 // high bits (all except for the first) don't matter the result. 9415 ISD::CondCode CC = 9416 cast<CondCodeSDNode>(N->getOperand( 9417 N->getOpcode() == ISD::SETCC ? 2 : 4))->get(); 9418 unsigned OpBits = N->getOperand(0).getValueSizeInBits(); 9419 9420 if (ISD::isSignedIntSetCC(CC)) { 9421 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits || 9422 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits) 9423 return SDValue(); 9424 } else if (ISD::isUnsignedIntSetCC(CC)) { 9425 if (!DAG.MaskedValueIsZero(N->getOperand(0), 9426 APInt::getHighBitsSet(OpBits, OpBits-1)) || 9427 !DAG.MaskedValueIsZero(N->getOperand(1), 9428 APInt::getHighBitsSet(OpBits, OpBits-1))) 9429 return SDValue(); 9430 } else { 9431 // This is neither a signed nor an unsigned comparison, just make sure 9432 // that the high bits are equal. 9433 APInt Op1Zero, Op1One; 9434 APInt Op2Zero, Op2One; 9435 DAG.computeKnownBits(N->getOperand(0), Op1Zero, Op1One); 9436 DAG.computeKnownBits(N->getOperand(1), Op2Zero, Op2One); 9437 9438 // We don't really care about what is known about the first bit (if 9439 // anything), so clear it in all masks prior to comparing them. 9440 Op1Zero.clearBit(0); Op1One.clearBit(0); 9441 Op2Zero.clearBit(0); Op2One.clearBit(0); 9442 9443 if (Op1Zero != Op2Zero || Op1One != Op2One) 9444 return SDValue(); 9445 } 9446 } 9447 9448 // We now know that the higher-order bits are irrelevant, we just need to 9449 // make sure that all of the intermediate operations are bit operations, and 9450 // all inputs are extensions. 9451 if (N->getOperand(0).getOpcode() != ISD::AND && 9452 N->getOperand(0).getOpcode() != ISD::OR && 9453 N->getOperand(0).getOpcode() != ISD::XOR && 9454 N->getOperand(0).getOpcode() != ISD::SELECT && 9455 N->getOperand(0).getOpcode() != ISD::SELECT_CC && 9456 N->getOperand(0).getOpcode() != ISD::TRUNCATE && 9457 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND && 9458 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND && 9459 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND) 9460 return SDValue(); 9461 9462 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) && 9463 N->getOperand(1).getOpcode() != ISD::AND && 9464 N->getOperand(1).getOpcode() != ISD::OR && 9465 N->getOperand(1).getOpcode() != ISD::XOR && 9466 N->getOperand(1).getOpcode() != ISD::SELECT && 9467 N->getOperand(1).getOpcode() != ISD::SELECT_CC && 9468 N->getOperand(1).getOpcode() != ISD::TRUNCATE && 9469 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND && 9470 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND && 9471 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND) 9472 return SDValue(); 9473 9474 SmallVector<SDValue, 4> Inputs; 9475 SmallVector<SDValue, 8> BinOps, PromOps; 9476 SmallPtrSet<SDNode *, 16> Visited; 9477 9478 for (unsigned i = 0; i < 2; ++i) { 9479 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND || 9480 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND || 9481 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) && 9482 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) || 9483 isa<ConstantSDNode>(N->getOperand(i))) 9484 Inputs.push_back(N->getOperand(i)); 9485 else 9486 BinOps.push_back(N->getOperand(i)); 9487 9488 if (N->getOpcode() == ISD::TRUNCATE) 9489 break; 9490 } 9491 9492 // Visit all inputs, collect all binary operations (and, or, xor and 9493 // select) that are all fed by extensions. 9494 while (!BinOps.empty()) { 9495 SDValue BinOp = BinOps.back(); 9496 BinOps.pop_back(); 9497 9498 if (!Visited.insert(BinOp.getNode()).second) 9499 continue; 9500 9501 PromOps.push_back(BinOp); 9502 9503 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) { 9504 // The condition of the select is not promoted. 9505 if (BinOp.getOpcode() == ISD::SELECT && i == 0) 9506 continue; 9507 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3) 9508 continue; 9509 9510 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND || 9511 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND || 9512 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) && 9513 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) || 9514 isa<ConstantSDNode>(BinOp.getOperand(i))) { 9515 Inputs.push_back(BinOp.getOperand(i)); 9516 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND || 9517 BinOp.getOperand(i).getOpcode() == ISD::OR || 9518 BinOp.getOperand(i).getOpcode() == ISD::XOR || 9519 BinOp.getOperand(i).getOpcode() == ISD::SELECT || 9520 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC || 9521 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE || 9522 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND || 9523 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND || 9524 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) { 9525 BinOps.push_back(BinOp.getOperand(i)); 9526 } else { 9527 // We have an input that is not an extension or another binary 9528 // operation; we'll abort this transformation. 9529 return SDValue(); 9530 } 9531 } 9532 } 9533 9534 // Make sure that this is a self-contained cluster of operations (which 9535 // is not quite the same thing as saying that everything has only one 9536 // use). 9537 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { 9538 if (isa<ConstantSDNode>(Inputs[i])) 9539 continue; 9540 9541 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(), 9542 UE = Inputs[i].getNode()->use_end(); 9543 UI != UE; ++UI) { 9544 SDNode *User = *UI; 9545 if (User != N && !Visited.count(User)) 9546 return SDValue(); 9547 9548 // Make sure that we're not going to promote the non-output-value 9549 // operand(s) or SELECT or SELECT_CC. 9550 // FIXME: Although we could sometimes handle this, and it does occur in 9551 // practice that one of the condition inputs to the select is also one of 9552 // the outputs, we currently can't deal with this. 9553 if (User->getOpcode() == ISD::SELECT) { 9554 if (User->getOperand(0) == Inputs[i]) 9555 return SDValue(); 9556 } else if (User->getOpcode() == ISD::SELECT_CC) { 9557 if (User->getOperand(0) == Inputs[i] || 9558 User->getOperand(1) == Inputs[i]) 9559 return SDValue(); 9560 } 9561 } 9562 } 9563 9564 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) { 9565 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(), 9566 UE = PromOps[i].getNode()->use_end(); 9567 UI != UE; ++UI) { 9568 SDNode *User = *UI; 9569 if (User != N && !Visited.count(User)) 9570 return SDValue(); 9571 9572 // Make sure that we're not going to promote the non-output-value 9573 // operand(s) or SELECT or SELECT_CC. 9574 // FIXME: Although we could sometimes handle this, and it does occur in 9575 // practice that one of the condition inputs to the select is also one of 9576 // the outputs, we currently can't deal with this. 9577 if (User->getOpcode() == ISD::SELECT) { 9578 if (User->getOperand(0) == PromOps[i]) 9579 return SDValue(); 9580 } else if (User->getOpcode() == ISD::SELECT_CC) { 9581 if (User->getOperand(0) == PromOps[i] || 9582 User->getOperand(1) == PromOps[i]) 9583 return SDValue(); 9584 } 9585 } 9586 } 9587 9588 // Replace all inputs with the extension operand. 9589 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { 9590 // Constants may have users outside the cluster of to-be-promoted nodes, 9591 // and so we need to replace those as we do the promotions. 9592 if (isa<ConstantSDNode>(Inputs[i])) 9593 continue; 9594 else 9595 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0)); 9596 } 9597 9598 // Replace all operations (these are all the same, but have a different 9599 // (i1) return type). DAG.getNode will validate that the types of 9600 // a binary operator match, so go through the list in reverse so that 9601 // we've likely promoted both operands first. Any intermediate truncations or 9602 // extensions disappear. 9603 while (!PromOps.empty()) { 9604 SDValue PromOp = PromOps.back(); 9605 PromOps.pop_back(); 9606 9607 if (PromOp.getOpcode() == ISD::TRUNCATE || 9608 PromOp.getOpcode() == ISD::SIGN_EXTEND || 9609 PromOp.getOpcode() == ISD::ZERO_EXTEND || 9610 PromOp.getOpcode() == ISD::ANY_EXTEND) { 9611 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) && 9612 PromOp.getOperand(0).getValueType() != MVT::i1) { 9613 // The operand is not yet ready (see comment below). 9614 PromOps.insert(PromOps.begin(), PromOp); 9615 continue; 9616 } 9617 9618 SDValue RepValue = PromOp.getOperand(0); 9619 if (isa<ConstantSDNode>(RepValue)) 9620 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue); 9621 9622 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue); 9623 continue; 9624 } 9625 9626 unsigned C; 9627 switch (PromOp.getOpcode()) { 9628 default: C = 0; break; 9629 case ISD::SELECT: C = 1; break; 9630 case ISD::SELECT_CC: C = 2; break; 9631 } 9632 9633 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) && 9634 PromOp.getOperand(C).getValueType() != MVT::i1) || 9635 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) && 9636 PromOp.getOperand(C+1).getValueType() != MVT::i1)) { 9637 // The to-be-promoted operands of this node have not yet been 9638 // promoted (this should be rare because we're going through the 9639 // list backward, but if one of the operands has several users in 9640 // this cluster of to-be-promoted nodes, it is possible). 9641 PromOps.insert(PromOps.begin(), PromOp); 9642 continue; 9643 } 9644 9645 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(), 9646 PromOp.getNode()->op_end()); 9647 9648 // If there are any constant inputs, make sure they're replaced now. 9649 for (unsigned i = 0; i < 2; ++i) 9650 if (isa<ConstantSDNode>(Ops[C+i])) 9651 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]); 9652 9653 DAG.ReplaceAllUsesOfValueWith(PromOp, 9654 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops)); 9655 } 9656 9657 // Now we're left with the initial truncation itself. 9658 if (N->getOpcode() == ISD::TRUNCATE) 9659 return N->getOperand(0); 9660 9661 // Otherwise, this is a comparison. The operands to be compared have just 9662 // changed type (to i1), but everything else is the same. 9663 return SDValue(N, 0); 9664 } 9665 9666 SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N, 9667 DAGCombinerInfo &DCI) const { 9668 SelectionDAG &DAG = DCI.DAG; 9669 SDLoc dl(N); 9670 9671 // If we're tracking CR bits, we need to be careful that we don't have: 9672 // zext(binary-ops(trunc(x), trunc(y))) 9673 // or 9674 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...) 9675 // such that we're unnecessarily moving things into CR bits that can more 9676 // efficiently stay in GPRs. Note that if we're not certain that the high 9677 // bits are set as required by the final extension, we still may need to do 9678 // some masking to get the proper behavior. 9679 9680 // This same functionality is important on PPC64 when dealing with 9681 // 32-to-64-bit extensions; these occur often when 32-bit values are used as 9682 // the return values of functions. Because it is so similar, it is handled 9683 // here as well. 9684 9685 if (N->getValueType(0) != MVT::i32 && 9686 N->getValueType(0) != MVT::i64) 9687 return SDValue(); 9688 9689 if (!((N->getOperand(0).getValueType() == MVT::i1 && Subtarget.useCRBits()) || 9690 (N->getOperand(0).getValueType() == MVT::i32 && Subtarget.isPPC64()))) 9691 return SDValue(); 9692 9693 if (N->getOperand(0).getOpcode() != ISD::AND && 9694 N->getOperand(0).getOpcode() != ISD::OR && 9695 N->getOperand(0).getOpcode() != ISD::XOR && 9696 N->getOperand(0).getOpcode() != ISD::SELECT && 9697 N->getOperand(0).getOpcode() != ISD::SELECT_CC) 9698 return SDValue(); 9699 9700 SmallVector<SDValue, 4> Inputs; 9701 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps; 9702 SmallPtrSet<SDNode *, 16> Visited; 9703 9704 // Visit all inputs, collect all binary operations (and, or, xor and 9705 // select) that are all fed by truncations. 9706 while (!BinOps.empty()) { 9707 SDValue BinOp = BinOps.back(); 9708 BinOps.pop_back(); 9709 9710 if (!Visited.insert(BinOp.getNode()).second) 9711 continue; 9712 9713 PromOps.push_back(BinOp); 9714 9715 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) { 9716 // The condition of the select is not promoted. 9717 if (BinOp.getOpcode() == ISD::SELECT && i == 0) 9718 continue; 9719 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3) 9720 continue; 9721 9722 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE || 9723 isa<ConstantSDNode>(BinOp.getOperand(i))) { 9724 Inputs.push_back(BinOp.getOperand(i)); 9725 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND || 9726 BinOp.getOperand(i).getOpcode() == ISD::OR || 9727 BinOp.getOperand(i).getOpcode() == ISD::XOR || 9728 BinOp.getOperand(i).getOpcode() == ISD::SELECT || 9729 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) { 9730 BinOps.push_back(BinOp.getOperand(i)); 9731 } else { 9732 // We have an input that is not a truncation or another binary 9733 // operation; we'll abort this transformation. 9734 return SDValue(); 9735 } 9736 } 9737 } 9738 9739 // The operands of a select that must be truncated when the select is 9740 // promoted because the operand is actually part of the to-be-promoted set. 9741 DenseMap<SDNode *, EVT> SelectTruncOp[2]; 9742 9743 // Make sure that this is a self-contained cluster of operations (which 9744 // is not quite the same thing as saying that everything has only one 9745 // use). 9746 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { 9747 if (isa<ConstantSDNode>(Inputs[i])) 9748 continue; 9749 9750 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(), 9751 UE = Inputs[i].getNode()->use_end(); 9752 UI != UE; ++UI) { 9753 SDNode *User = *UI; 9754 if (User != N && !Visited.count(User)) 9755 return SDValue(); 9756 9757 // If we're going to promote the non-output-value operand(s) or SELECT or 9758 // SELECT_CC, record them for truncation. 9759 if (User->getOpcode() == ISD::SELECT) { 9760 if (User->getOperand(0) == Inputs[i]) 9761 SelectTruncOp[0].insert(std::make_pair(User, 9762 User->getOperand(0).getValueType())); 9763 } else if (User->getOpcode() == ISD::SELECT_CC) { 9764 if (User->getOperand(0) == Inputs[i]) 9765 SelectTruncOp[0].insert(std::make_pair(User, 9766 User->getOperand(0).getValueType())); 9767 if (User->getOperand(1) == Inputs[i]) 9768 SelectTruncOp[1].insert(std::make_pair(User, 9769 User->getOperand(1).getValueType())); 9770 } 9771 } 9772 } 9773 9774 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) { 9775 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(), 9776 UE = PromOps[i].getNode()->use_end(); 9777 UI != UE; ++UI) { 9778 SDNode *User = *UI; 9779 if (User != N && !Visited.count(User)) 9780 return SDValue(); 9781 9782 // If we're going to promote the non-output-value operand(s) or SELECT or 9783 // SELECT_CC, record them for truncation. 9784 if (User->getOpcode() == ISD::SELECT) { 9785 if (User->getOperand(0) == PromOps[i]) 9786 SelectTruncOp[0].insert(std::make_pair(User, 9787 User->getOperand(0).getValueType())); 9788 } else if (User->getOpcode() == ISD::SELECT_CC) { 9789 if (User->getOperand(0) == PromOps[i]) 9790 SelectTruncOp[0].insert(std::make_pair(User, 9791 User->getOperand(0).getValueType())); 9792 if (User->getOperand(1) == PromOps[i]) 9793 SelectTruncOp[1].insert(std::make_pair(User, 9794 User->getOperand(1).getValueType())); 9795 } 9796 } 9797 } 9798 9799 unsigned PromBits = N->getOperand(0).getValueSizeInBits(); 9800 bool ReallyNeedsExt = false; 9801 if (N->getOpcode() != ISD::ANY_EXTEND) { 9802 // If all of the inputs are not already sign/zero extended, then 9803 // we'll still need to do that at the end. 9804 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { 9805 if (isa<ConstantSDNode>(Inputs[i])) 9806 continue; 9807 9808 unsigned OpBits = 9809 Inputs[i].getOperand(0).getValueSizeInBits(); 9810 assert(PromBits < OpBits && "Truncation not to a smaller bit count?"); 9811 9812 if ((N->getOpcode() == ISD::ZERO_EXTEND && 9813 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0), 9814 APInt::getHighBitsSet(OpBits, 9815 OpBits-PromBits))) || 9816 (N->getOpcode() == ISD::SIGN_EXTEND && 9817 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) < 9818 (OpBits-(PromBits-1)))) { 9819 ReallyNeedsExt = true; 9820 break; 9821 } 9822 } 9823 } 9824 9825 // Replace all inputs, either with the truncation operand, or a 9826 // truncation or extension to the final output type. 9827 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { 9828 // Constant inputs need to be replaced with the to-be-promoted nodes that 9829 // use them because they might have users outside of the cluster of 9830 // promoted nodes. 9831 if (isa<ConstantSDNode>(Inputs[i])) 9832 continue; 9833 9834 SDValue InSrc = Inputs[i].getOperand(0); 9835 if (Inputs[i].getValueType() == N->getValueType(0)) 9836 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc); 9837 else if (N->getOpcode() == ISD::SIGN_EXTEND) 9838 DAG.ReplaceAllUsesOfValueWith(Inputs[i], 9839 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0))); 9840 else if (N->getOpcode() == ISD::ZERO_EXTEND) 9841 DAG.ReplaceAllUsesOfValueWith(Inputs[i], 9842 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0))); 9843 else 9844 DAG.ReplaceAllUsesOfValueWith(Inputs[i], 9845 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0))); 9846 } 9847 9848 // Replace all operations (these are all the same, but have a different 9849 // (promoted) return type). DAG.getNode will validate that the types of 9850 // a binary operator match, so go through the list in reverse so that 9851 // we've likely promoted both operands first. 9852 while (!PromOps.empty()) { 9853 SDValue PromOp = PromOps.back(); 9854 PromOps.pop_back(); 9855 9856 unsigned C; 9857 switch (PromOp.getOpcode()) { 9858 default: C = 0; break; 9859 case ISD::SELECT: C = 1; break; 9860 case ISD::SELECT_CC: C = 2; break; 9861 } 9862 9863 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) && 9864 PromOp.getOperand(C).getValueType() != N->getValueType(0)) || 9865 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) && 9866 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) { 9867 // The to-be-promoted operands of this node have not yet been 9868 // promoted (this should be rare because we're going through the 9869 // list backward, but if one of the operands has several users in 9870 // this cluster of to-be-promoted nodes, it is possible). 9871 PromOps.insert(PromOps.begin(), PromOp); 9872 continue; 9873 } 9874 9875 // For SELECT and SELECT_CC nodes, we do a similar check for any 9876 // to-be-promoted comparison inputs. 9877 if (PromOp.getOpcode() == ISD::SELECT || 9878 PromOp.getOpcode() == ISD::SELECT_CC) { 9879 if ((SelectTruncOp[0].count(PromOp.getNode()) && 9880 PromOp.getOperand(0).getValueType() != N->getValueType(0)) || 9881 (SelectTruncOp[1].count(PromOp.getNode()) && 9882 PromOp.getOperand(1).getValueType() != N->getValueType(0))) { 9883 PromOps.insert(PromOps.begin(), PromOp); 9884 continue; 9885 } 9886 } 9887 9888 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(), 9889 PromOp.getNode()->op_end()); 9890 9891 // If this node has constant inputs, then they'll need to be promoted here. 9892 for (unsigned i = 0; i < 2; ++i) { 9893 if (!isa<ConstantSDNode>(Ops[C+i])) 9894 continue; 9895 if (Ops[C+i].getValueType() == N->getValueType(0)) 9896 continue; 9897 9898 if (N->getOpcode() == ISD::SIGN_EXTEND) 9899 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0)); 9900 else if (N->getOpcode() == ISD::ZERO_EXTEND) 9901 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0)); 9902 else 9903 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0)); 9904 } 9905 9906 // If we've promoted the comparison inputs of a SELECT or SELECT_CC, 9907 // truncate them again to the original value type. 9908 if (PromOp.getOpcode() == ISD::SELECT || 9909 PromOp.getOpcode() == ISD::SELECT_CC) { 9910 auto SI0 = SelectTruncOp[0].find(PromOp.getNode()); 9911 if (SI0 != SelectTruncOp[0].end()) 9912 Ops[0] = DAG.getNode(ISD::TRUNCATE, dl, SI0->second, Ops[0]); 9913 auto SI1 = SelectTruncOp[1].find(PromOp.getNode()); 9914 if (SI1 != SelectTruncOp[1].end()) 9915 Ops[1] = DAG.getNode(ISD::TRUNCATE, dl, SI1->second, Ops[1]); 9916 } 9917 9918 DAG.ReplaceAllUsesOfValueWith(PromOp, 9919 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops)); 9920 } 9921 9922 // Now we're left with the initial extension itself. 9923 if (!ReallyNeedsExt) 9924 return N->getOperand(0); 9925 9926 // To zero extend, just mask off everything except for the first bit (in the 9927 // i1 case). 9928 if (N->getOpcode() == ISD::ZERO_EXTEND) 9929 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0), 9930 DAG.getConstant(APInt::getLowBitsSet( 9931 N->getValueSizeInBits(0), PromBits), 9932 dl, N->getValueType(0))); 9933 9934 assert(N->getOpcode() == ISD::SIGN_EXTEND && 9935 "Invalid extension type"); 9936 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0), DAG.getDataLayout()); 9937 SDValue ShiftCst = 9938 DAG.getConstant(N->getValueSizeInBits(0) - PromBits, dl, ShiftAmountTy); 9939 return DAG.getNode(ISD::SRA, dl, N->getValueType(0), 9940 DAG.getNode(ISD::SHL, dl, N->getValueType(0), 9941 N->getOperand(0), ShiftCst), ShiftCst); 9942 } 9943 9944 SDValue PPCTargetLowering::combineFPToIntToFP(SDNode *N, 9945 DAGCombinerInfo &DCI) const { 9946 assert((N->getOpcode() == ISD::SINT_TO_FP || 9947 N->getOpcode() == ISD::UINT_TO_FP) && 9948 "Need an int -> FP conversion node here"); 9949 9950 if (!Subtarget.has64BitSupport()) 9951 return SDValue(); 9952 9953 SelectionDAG &DAG = DCI.DAG; 9954 SDLoc dl(N); 9955 SDValue Op(N, 0); 9956 9957 // Don't handle ppc_fp128 here or i1 conversions. 9958 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64) 9959 return SDValue(); 9960 if (Op.getOperand(0).getValueType() == MVT::i1) 9961 return SDValue(); 9962 9963 // For i32 intermediate values, unfortunately, the conversion functions 9964 // leave the upper 32 bits of the value are undefined. Within the set of 9965 // scalar instructions, we have no method for zero- or sign-extending the 9966 // value. Thus, we cannot handle i32 intermediate values here. 9967 if (Op.getOperand(0).getValueType() == MVT::i32) 9968 return SDValue(); 9969 9970 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) && 9971 "UINT_TO_FP is supported only with FPCVT"); 9972 9973 // If we have FCFIDS, then use it when converting to single-precision. 9974 // Otherwise, convert to double-precision and then round. 9975 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) 9976 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS 9977 : PPCISD::FCFIDS) 9978 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU 9979 : PPCISD::FCFID); 9980 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) 9981 ? MVT::f32 9982 : MVT::f64; 9983 9984 // If we're converting from a float, to an int, and back to a float again, 9985 // then we don't need the store/load pair at all. 9986 if ((Op.getOperand(0).getOpcode() == ISD::FP_TO_UINT && 9987 Subtarget.hasFPCVT()) || 9988 (Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT)) { 9989 SDValue Src = Op.getOperand(0).getOperand(0); 9990 if (Src.getValueType() == MVT::f32) { 9991 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src); 9992 DCI.AddToWorklist(Src.getNode()); 9993 } 9994 9995 unsigned FCTOp = 9996 Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT ? PPCISD::FCTIDZ : 9997 PPCISD::FCTIDUZ; 9998 9999 SDValue Tmp = DAG.getNode(FCTOp, dl, MVT::f64, Src); 10000 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Tmp); 10001 10002 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) { 10003 FP = DAG.getNode(ISD::FP_ROUND, dl, 10004 MVT::f32, FP, DAG.getIntPtrConstant(0, dl)); 10005 DCI.AddToWorklist(FP.getNode()); 10006 } 10007 10008 return FP; 10009 } 10010 10011 return SDValue(); 10012 } 10013 10014 // expandVSXLoadForLE - Convert VSX loads (which may be intrinsics for 10015 // builtins) into loads with swaps. 10016 SDValue PPCTargetLowering::expandVSXLoadForLE(SDNode *N, 10017 DAGCombinerInfo &DCI) const { 10018 SelectionDAG &DAG = DCI.DAG; 10019 SDLoc dl(N); 10020 SDValue Chain; 10021 SDValue Base; 10022 MachineMemOperand *MMO; 10023 10024 switch (N->getOpcode()) { 10025 default: 10026 llvm_unreachable("Unexpected opcode for little endian VSX load"); 10027 case ISD::LOAD: { 10028 LoadSDNode *LD = cast<LoadSDNode>(N); 10029 Chain = LD->getChain(); 10030 Base = LD->getBasePtr(); 10031 MMO = LD->getMemOperand(); 10032 // If the MMO suggests this isn't a load of a full vector, leave 10033 // things alone. For a built-in, we have to make the change for 10034 // correctness, so if there is a size problem that will be a bug. 10035 if (MMO->getSize() < 16) 10036 return SDValue(); 10037 break; 10038 } 10039 case ISD::INTRINSIC_W_CHAIN: { 10040 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N); 10041 Chain = Intrin->getChain(); 10042 // Similarly to the store case below, Intrin->getBasePtr() doesn't get 10043 // us what we want. Get operand 2 instead. 10044 Base = Intrin->getOperand(2); 10045 MMO = Intrin->getMemOperand(); 10046 break; 10047 } 10048 } 10049 10050 MVT VecTy = N->getValueType(0).getSimpleVT(); 10051 SDValue LoadOps[] = { Chain, Base }; 10052 SDValue Load = DAG.getMemIntrinsicNode(PPCISD::LXVD2X, dl, 10053 DAG.getVTList(VecTy, MVT::Other), 10054 LoadOps, VecTy, MMO); 10055 DCI.AddToWorklist(Load.getNode()); 10056 Chain = Load.getValue(1); 10057 SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl, 10058 DAG.getVTList(VecTy, MVT::Other), Chain, Load); 10059 DCI.AddToWorklist(Swap.getNode()); 10060 return Swap; 10061 } 10062 10063 // expandVSXStoreForLE - Convert VSX stores (which may be intrinsics for 10064 // builtins) into stores with swaps. 10065 SDValue PPCTargetLowering::expandVSXStoreForLE(SDNode *N, 10066 DAGCombinerInfo &DCI) const { 10067 SelectionDAG &DAG = DCI.DAG; 10068 SDLoc dl(N); 10069 SDValue Chain; 10070 SDValue Base; 10071 unsigned SrcOpnd; 10072 MachineMemOperand *MMO; 10073 10074 switch (N->getOpcode()) { 10075 default: 10076 llvm_unreachable("Unexpected opcode for little endian VSX store"); 10077 case ISD::STORE: { 10078 StoreSDNode *ST = cast<StoreSDNode>(N); 10079 Chain = ST->getChain(); 10080 Base = ST->getBasePtr(); 10081 MMO = ST->getMemOperand(); 10082 SrcOpnd = 1; 10083 // If the MMO suggests this isn't a store of a full vector, leave 10084 // things alone. For a built-in, we have to make the change for 10085 // correctness, so if there is a size problem that will be a bug. 10086 if (MMO->getSize() < 16) 10087 return SDValue(); 10088 break; 10089 } 10090 case ISD::INTRINSIC_VOID: { 10091 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N); 10092 Chain = Intrin->getChain(); 10093 // Intrin->getBasePtr() oddly does not get what we want. 10094 Base = Intrin->getOperand(3); 10095 MMO = Intrin->getMemOperand(); 10096 SrcOpnd = 2; 10097 break; 10098 } 10099 } 10100 10101 SDValue Src = N->getOperand(SrcOpnd); 10102 MVT VecTy = Src.getValueType().getSimpleVT(); 10103 SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl, 10104 DAG.getVTList(VecTy, MVT::Other), Chain, Src); 10105 DCI.AddToWorklist(Swap.getNode()); 10106 Chain = Swap.getValue(1); 10107 SDValue StoreOps[] = { Chain, Swap, Base }; 10108 SDValue Store = DAG.getMemIntrinsicNode(PPCISD::STXVD2X, dl, 10109 DAG.getVTList(MVT::Other), 10110 StoreOps, VecTy, MMO); 10111 DCI.AddToWorklist(Store.getNode()); 10112 return Store; 10113 } 10114 10115 SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N, 10116 DAGCombinerInfo &DCI) const { 10117 SelectionDAG &DAG = DCI.DAG; 10118 SDLoc dl(N); 10119 switch (N->getOpcode()) { 10120 default: break; 10121 case PPCISD::SHL: 10122 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) { 10123 if (C->isNullValue()) // 0 << V -> 0. 10124 return N->getOperand(0); 10125 } 10126 break; 10127 case PPCISD::SRL: 10128 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) { 10129 if (C->isNullValue()) // 0 >>u V -> 0. 10130 return N->getOperand(0); 10131 } 10132 break; 10133 case PPCISD::SRA: 10134 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) { 10135 if (C->isNullValue() || // 0 >>s V -> 0. 10136 C->isAllOnesValue()) // -1 >>s V -> -1. 10137 return N->getOperand(0); 10138 } 10139 break; 10140 case ISD::SIGN_EXTEND: 10141 case ISD::ZERO_EXTEND: 10142 case ISD::ANY_EXTEND: 10143 return DAGCombineExtBoolTrunc(N, DCI); 10144 case ISD::TRUNCATE: 10145 case ISD::SETCC: 10146 case ISD::SELECT_CC: 10147 return DAGCombineTruncBoolExt(N, DCI); 10148 case ISD::SINT_TO_FP: 10149 case ISD::UINT_TO_FP: 10150 return combineFPToIntToFP(N, DCI); 10151 case ISD::STORE: { 10152 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)). 10153 if (Subtarget.hasSTFIWX() && !cast<StoreSDNode>(N)->isTruncatingStore() && 10154 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT && 10155 N->getOperand(1).getValueType() == MVT::i32 && 10156 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) { 10157 SDValue Val = N->getOperand(1).getOperand(0); 10158 if (Val.getValueType() == MVT::f32) { 10159 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val); 10160 DCI.AddToWorklist(Val.getNode()); 10161 } 10162 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val); 10163 DCI.AddToWorklist(Val.getNode()); 10164 10165 SDValue Ops[] = { 10166 N->getOperand(0), Val, N->getOperand(2), 10167 DAG.getValueType(N->getOperand(1).getValueType()) 10168 }; 10169 10170 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl, 10171 DAG.getVTList(MVT::Other), Ops, 10172 cast<StoreSDNode>(N)->getMemoryVT(), 10173 cast<StoreSDNode>(N)->getMemOperand()); 10174 DCI.AddToWorklist(Val.getNode()); 10175 return Val; 10176 } 10177 10178 // Turn STORE (BSWAP) -> sthbrx/stwbrx. 10179 if (cast<StoreSDNode>(N)->isUnindexed() && 10180 N->getOperand(1).getOpcode() == ISD::BSWAP && 10181 N->getOperand(1).getNode()->hasOneUse() && 10182 (N->getOperand(1).getValueType() == MVT::i32 || 10183 N->getOperand(1).getValueType() == MVT::i16 || 10184 (Subtarget.hasLDBRX() && Subtarget.isPPC64() && 10185 N->getOperand(1).getValueType() == MVT::i64))) { 10186 SDValue BSwapOp = N->getOperand(1).getOperand(0); 10187 // Do an any-extend to 32-bits if this is a half-word input. 10188 if (BSwapOp.getValueType() == MVT::i16) 10189 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp); 10190 10191 SDValue Ops[] = { 10192 N->getOperand(0), BSwapOp, N->getOperand(2), 10193 DAG.getValueType(N->getOperand(1).getValueType()) 10194 }; 10195 return 10196 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other), 10197 Ops, cast<StoreSDNode>(N)->getMemoryVT(), 10198 cast<StoreSDNode>(N)->getMemOperand()); 10199 } 10200 10201 // For little endian, VSX stores require generating xxswapd/lxvd2x. 10202 EVT VT = N->getOperand(1).getValueType(); 10203 if (VT.isSimple()) { 10204 MVT StoreVT = VT.getSimpleVT(); 10205 if (Subtarget.hasVSX() && Subtarget.isLittleEndian() && 10206 (StoreVT == MVT::v2f64 || StoreVT == MVT::v2i64 || 10207 StoreVT == MVT::v4f32 || StoreVT == MVT::v4i32)) 10208 return expandVSXStoreForLE(N, DCI); 10209 } 10210 break; 10211 } 10212 case ISD::LOAD: { 10213 LoadSDNode *LD = cast<LoadSDNode>(N); 10214 EVT VT = LD->getValueType(0); 10215 10216 // For little endian, VSX loads require generating lxvd2x/xxswapd. 10217 if (VT.isSimple()) { 10218 MVT LoadVT = VT.getSimpleVT(); 10219 if (Subtarget.hasVSX() && Subtarget.isLittleEndian() && 10220 (LoadVT == MVT::v2f64 || LoadVT == MVT::v2i64 || 10221 LoadVT == MVT::v4f32 || LoadVT == MVT::v4i32)) 10222 return expandVSXLoadForLE(N, DCI); 10223 } 10224 10225 EVT MemVT = LD->getMemoryVT(); 10226 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext()); 10227 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty); 10228 Type *STy = MemVT.getScalarType().getTypeForEVT(*DAG.getContext()); 10229 unsigned ScalarABIAlignment = DAG.getDataLayout().getABITypeAlignment(STy); 10230 if (LD->isUnindexed() && VT.isVector() && 10231 ((Subtarget.hasAltivec() && ISD::isNON_EXTLoad(N) && 10232 // P8 and later hardware should just use LOAD. 10233 !Subtarget.hasP8Vector() && (VT == MVT::v16i8 || VT == MVT::v8i16 || 10234 VT == MVT::v4i32 || VT == MVT::v4f32)) || 10235 (Subtarget.hasQPX() && (VT == MVT::v4f64 || VT == MVT::v4f32) && 10236 LD->getAlignment() >= ScalarABIAlignment)) && 10237 LD->getAlignment() < ABIAlignment) { 10238 // This is a type-legal unaligned Altivec or QPX load. 10239 SDValue Chain = LD->getChain(); 10240 SDValue Ptr = LD->getBasePtr(); 10241 bool isLittleEndian = Subtarget.isLittleEndian(); 10242 10243 // This implements the loading of unaligned vectors as described in 10244 // the venerable Apple Velocity Engine overview. Specifically: 10245 // https://developer.apple.com/hardwaredrivers/ve/alignment.html 10246 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html 10247 // 10248 // The general idea is to expand a sequence of one or more unaligned 10249 // loads into an alignment-based permutation-control instruction (lvsl 10250 // or lvsr), a series of regular vector loads (which always truncate 10251 // their input address to an aligned address), and a series of 10252 // permutations. The results of these permutations are the requested 10253 // loaded values. The trick is that the last "extra" load is not taken 10254 // from the address you might suspect (sizeof(vector) bytes after the 10255 // last requested load), but rather sizeof(vector) - 1 bytes after the 10256 // last requested vector. The point of this is to avoid a page fault if 10257 // the base address happened to be aligned. This works because if the 10258 // base address is aligned, then adding less than a full vector length 10259 // will cause the last vector in the sequence to be (re)loaded. 10260 // Otherwise, the next vector will be fetched as you might suspect was 10261 // necessary. 10262 10263 // We might be able to reuse the permutation generation from 10264 // a different base address offset from this one by an aligned amount. 10265 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this 10266 // optimization later. 10267 Intrinsic::ID Intr, IntrLD, IntrPerm; 10268 MVT PermCntlTy, PermTy, LDTy; 10269 if (Subtarget.hasAltivec()) { 10270 Intr = isLittleEndian ? Intrinsic::ppc_altivec_lvsr : 10271 Intrinsic::ppc_altivec_lvsl; 10272 IntrLD = Intrinsic::ppc_altivec_lvx; 10273 IntrPerm = Intrinsic::ppc_altivec_vperm; 10274 PermCntlTy = MVT::v16i8; 10275 PermTy = MVT::v4i32; 10276 LDTy = MVT::v4i32; 10277 } else { 10278 Intr = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlpcld : 10279 Intrinsic::ppc_qpx_qvlpcls; 10280 IntrLD = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlfd : 10281 Intrinsic::ppc_qpx_qvlfs; 10282 IntrPerm = Intrinsic::ppc_qpx_qvfperm; 10283 PermCntlTy = MVT::v4f64; 10284 PermTy = MVT::v4f64; 10285 LDTy = MemVT.getSimpleVT(); 10286 } 10287 10288 SDValue PermCntl = BuildIntrinsicOp(Intr, Ptr, DAG, dl, PermCntlTy); 10289 10290 // Create the new MMO for the new base load. It is like the original MMO, 10291 // but represents an area in memory almost twice the vector size centered 10292 // on the original address. If the address is unaligned, we might start 10293 // reading up to (sizeof(vector)-1) bytes below the address of the 10294 // original unaligned load. 10295 MachineFunction &MF = DAG.getMachineFunction(); 10296 MachineMemOperand *BaseMMO = 10297 MF.getMachineMemOperand(LD->getMemOperand(), -MemVT.getStoreSize()+1, 10298 2*MemVT.getStoreSize()-1); 10299 10300 // Create the new base load. 10301 SDValue LDXIntID = 10302 DAG.getTargetConstant(IntrLD, dl, getPointerTy(MF.getDataLayout())); 10303 SDValue BaseLoadOps[] = { Chain, LDXIntID, Ptr }; 10304 SDValue BaseLoad = 10305 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl, 10306 DAG.getVTList(PermTy, MVT::Other), 10307 BaseLoadOps, LDTy, BaseMMO); 10308 10309 // Note that the value of IncOffset (which is provided to the next 10310 // load's pointer info offset value, and thus used to calculate the 10311 // alignment), and the value of IncValue (which is actually used to 10312 // increment the pointer value) are different! This is because we 10313 // require the next load to appear to be aligned, even though it 10314 // is actually offset from the base pointer by a lesser amount. 10315 int IncOffset = VT.getSizeInBits() / 8; 10316 int IncValue = IncOffset; 10317 10318 // Walk (both up and down) the chain looking for another load at the real 10319 // (aligned) offset (the alignment of the other load does not matter in 10320 // this case). If found, then do not use the offset reduction trick, as 10321 // that will prevent the loads from being later combined (as they would 10322 // otherwise be duplicates). 10323 if (!findConsecutiveLoad(LD, DAG)) 10324 --IncValue; 10325 10326 SDValue Increment = 10327 DAG.getConstant(IncValue, dl, getPointerTy(MF.getDataLayout())); 10328 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); 10329 10330 MachineMemOperand *ExtraMMO = 10331 MF.getMachineMemOperand(LD->getMemOperand(), 10332 1, 2*MemVT.getStoreSize()-1); 10333 SDValue ExtraLoadOps[] = { Chain, LDXIntID, Ptr }; 10334 SDValue ExtraLoad = 10335 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl, 10336 DAG.getVTList(PermTy, MVT::Other), 10337 ExtraLoadOps, LDTy, ExtraMMO); 10338 10339 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 10340 BaseLoad.getValue(1), ExtraLoad.getValue(1)); 10341 10342 // Because vperm has a big-endian bias, we must reverse the order 10343 // of the input vectors and complement the permute control vector 10344 // when generating little endian code. We have already handled the 10345 // latter by using lvsr instead of lvsl, so just reverse BaseLoad 10346 // and ExtraLoad here. 10347 SDValue Perm; 10348 if (isLittleEndian) 10349 Perm = BuildIntrinsicOp(IntrPerm, 10350 ExtraLoad, BaseLoad, PermCntl, DAG, dl); 10351 else 10352 Perm = BuildIntrinsicOp(IntrPerm, 10353 BaseLoad, ExtraLoad, PermCntl, DAG, dl); 10354 10355 if (VT != PermTy) 10356 Perm = Subtarget.hasAltivec() ? 10357 DAG.getNode(ISD::BITCAST, dl, VT, Perm) : 10358 DAG.getNode(ISD::FP_ROUND, dl, VT, Perm, // QPX 10359 DAG.getTargetConstant(1, dl, MVT::i64)); 10360 // second argument is 1 because this rounding 10361 // is always exact. 10362 10363 // The output of the permutation is our loaded result, the TokenFactor is 10364 // our new chain. 10365 DCI.CombineTo(N, Perm, TF); 10366 return SDValue(N, 0); 10367 } 10368 } 10369 break; 10370 case ISD::INTRINSIC_WO_CHAIN: { 10371 bool isLittleEndian = Subtarget.isLittleEndian(); 10372 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 10373 Intrinsic::ID Intr = (isLittleEndian ? Intrinsic::ppc_altivec_lvsr 10374 : Intrinsic::ppc_altivec_lvsl); 10375 if ((IID == Intr || 10376 IID == Intrinsic::ppc_qpx_qvlpcld || 10377 IID == Intrinsic::ppc_qpx_qvlpcls) && 10378 N->getOperand(1)->getOpcode() == ISD::ADD) { 10379 SDValue Add = N->getOperand(1); 10380 10381 int Bits = IID == Intrinsic::ppc_qpx_qvlpcld ? 10382 5 /* 32 byte alignment */ : 4 /* 16 byte alignment */; 10383 10384 if (DAG.MaskedValueIsZero( 10385 Add->getOperand(1), 10386 APInt::getAllOnesValue(Bits /* alignment */) 10387 .zext( 10388 Add.getValueType().getScalarType().getSizeInBits()))) { 10389 SDNode *BasePtr = Add->getOperand(0).getNode(); 10390 for (SDNode::use_iterator UI = BasePtr->use_begin(), 10391 UE = BasePtr->use_end(); 10392 UI != UE; ++UI) { 10393 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN && 10394 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() == IID) { 10395 // We've found another LVSL/LVSR, and this address is an aligned 10396 // multiple of that one. The results will be the same, so use the 10397 // one we've just found instead. 10398 10399 return SDValue(*UI, 0); 10400 } 10401 } 10402 } 10403 10404 if (isa<ConstantSDNode>(Add->getOperand(1))) { 10405 SDNode *BasePtr = Add->getOperand(0).getNode(); 10406 for (SDNode::use_iterator UI = BasePtr->use_begin(), 10407 UE = BasePtr->use_end(); UI != UE; ++UI) { 10408 if (UI->getOpcode() == ISD::ADD && 10409 isa<ConstantSDNode>(UI->getOperand(1)) && 10410 (cast<ConstantSDNode>(Add->getOperand(1))->getZExtValue() - 10411 cast<ConstantSDNode>(UI->getOperand(1))->getZExtValue()) % 10412 (1ULL << Bits) == 0) { 10413 SDNode *OtherAdd = *UI; 10414 for (SDNode::use_iterator VI = OtherAdd->use_begin(), 10415 VE = OtherAdd->use_end(); VI != VE; ++VI) { 10416 if (VI->getOpcode() == ISD::INTRINSIC_WO_CHAIN && 10417 cast<ConstantSDNode>(VI->getOperand(0))->getZExtValue() == IID) { 10418 return SDValue(*VI, 0); 10419 } 10420 } 10421 } 10422 } 10423 } 10424 } 10425 } 10426 10427 break; 10428 case ISD::INTRINSIC_W_CHAIN: { 10429 // For little endian, VSX loads require generating lxvd2x/xxswapd. 10430 if (Subtarget.hasVSX() && Subtarget.isLittleEndian()) { 10431 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 10432 default: 10433 break; 10434 case Intrinsic::ppc_vsx_lxvw4x: 10435 case Intrinsic::ppc_vsx_lxvd2x: 10436 return expandVSXLoadForLE(N, DCI); 10437 } 10438 } 10439 break; 10440 } 10441 case ISD::INTRINSIC_VOID: { 10442 // For little endian, VSX stores require generating xxswapd/stxvd2x. 10443 if (Subtarget.hasVSX() && Subtarget.isLittleEndian()) { 10444 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 10445 default: 10446 break; 10447 case Intrinsic::ppc_vsx_stxvw4x: 10448 case Intrinsic::ppc_vsx_stxvd2x: 10449 return expandVSXStoreForLE(N, DCI); 10450 } 10451 } 10452 break; 10453 } 10454 case ISD::BSWAP: 10455 // Turn BSWAP (LOAD) -> lhbrx/lwbrx. 10456 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) && 10457 N->getOperand(0).hasOneUse() && 10458 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 || 10459 (Subtarget.hasLDBRX() && Subtarget.isPPC64() && 10460 N->getValueType(0) == MVT::i64))) { 10461 SDValue Load = N->getOperand(0); 10462 LoadSDNode *LD = cast<LoadSDNode>(Load); 10463 // Create the byte-swapping load. 10464 SDValue Ops[] = { 10465 LD->getChain(), // Chain 10466 LD->getBasePtr(), // Ptr 10467 DAG.getValueType(N->getValueType(0)) // VT 10468 }; 10469 SDValue BSLoad = 10470 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl, 10471 DAG.getVTList(N->getValueType(0) == MVT::i64 ? 10472 MVT::i64 : MVT::i32, MVT::Other), 10473 Ops, LD->getMemoryVT(), LD->getMemOperand()); 10474 10475 // If this is an i16 load, insert the truncate. 10476 SDValue ResVal = BSLoad; 10477 if (N->getValueType(0) == MVT::i16) 10478 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad); 10479 10480 // First, combine the bswap away. This makes the value produced by the 10481 // load dead. 10482 DCI.CombineTo(N, ResVal); 10483 10484 // Next, combine the load away, we give it a bogus result value but a real 10485 // chain result. The result value is dead because the bswap is dead. 10486 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1)); 10487 10488 // Return N so it doesn't get rechecked! 10489 return SDValue(N, 0); 10490 } 10491 10492 break; 10493 case PPCISD::VCMP: { 10494 // If a VCMPo node already exists with exactly the same operands as this 10495 // node, use its result instead of this node (VCMPo computes both a CR6 and 10496 // a normal output). 10497 // 10498 if (!N->getOperand(0).hasOneUse() && 10499 !N->getOperand(1).hasOneUse() && 10500 !N->getOperand(2).hasOneUse()) { 10501 10502 // Scan all of the users of the LHS, looking for VCMPo's that match. 10503 SDNode *VCMPoNode = nullptr; 10504 10505 SDNode *LHSN = N->getOperand(0).getNode(); 10506 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end(); 10507 UI != E; ++UI) 10508 if (UI->getOpcode() == PPCISD::VCMPo && 10509 UI->getOperand(1) == N->getOperand(1) && 10510 UI->getOperand(2) == N->getOperand(2) && 10511 UI->getOperand(0) == N->getOperand(0)) { 10512 VCMPoNode = *UI; 10513 break; 10514 } 10515 10516 // If there is no VCMPo node, or if the flag value has a single use, don't 10517 // transform this. 10518 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1)) 10519 break; 10520 10521 // Look at the (necessarily single) use of the flag value. If it has a 10522 // chain, this transformation is more complex. Note that multiple things 10523 // could use the value result, which we should ignore. 10524 SDNode *FlagUser = nullptr; 10525 for (SDNode::use_iterator UI = VCMPoNode->use_begin(); 10526 FlagUser == nullptr; ++UI) { 10527 assert(UI != VCMPoNode->use_end() && "Didn't find user!"); 10528 SDNode *User = *UI; 10529 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { 10530 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) { 10531 FlagUser = User; 10532 break; 10533 } 10534 } 10535 } 10536 10537 // If the user is a MFOCRF instruction, we know this is safe. 10538 // Otherwise we give up for right now. 10539 if (FlagUser->getOpcode() == PPCISD::MFOCRF) 10540 return SDValue(VCMPoNode, 0); 10541 } 10542 break; 10543 } 10544 case ISD::BRCOND: { 10545 SDValue Cond = N->getOperand(1); 10546 SDValue Target = N->getOperand(2); 10547 10548 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN && 10549 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() == 10550 Intrinsic::ppc_is_decremented_ctr_nonzero) { 10551 10552 // We now need to make the intrinsic dead (it cannot be instruction 10553 // selected). 10554 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0)); 10555 assert(Cond.getNode()->hasOneUse() && 10556 "Counter decrement has more than one use"); 10557 10558 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other, 10559 N->getOperand(0), Target); 10560 } 10561 } 10562 break; 10563 case ISD::BR_CC: { 10564 // If this is a branch on an altivec predicate comparison, lower this so 10565 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This 10566 // lowering is done pre-legalize, because the legalizer lowers the predicate 10567 // compare down to code that is difficult to reassemble. 10568 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); 10569 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3); 10570 10571 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero 10572 // value. If so, pass-through the AND to get to the intrinsic. 10573 if (LHS.getOpcode() == ISD::AND && 10574 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN && 10575 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() == 10576 Intrinsic::ppc_is_decremented_ctr_nonzero && 10577 isa<ConstantSDNode>(LHS.getOperand(1)) && 10578 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()-> 10579 isZero()) 10580 LHS = LHS.getOperand(0); 10581 10582 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN && 10583 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 10584 Intrinsic::ppc_is_decremented_ctr_nonzero && 10585 isa<ConstantSDNode>(RHS)) { 10586 assert((CC == ISD::SETEQ || CC == ISD::SETNE) && 10587 "Counter decrement comparison is not EQ or NE"); 10588 10589 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue(); 10590 bool isBDNZ = (CC == ISD::SETEQ && Val) || 10591 (CC == ISD::SETNE && !Val); 10592 10593 // We now need to make the intrinsic dead (it cannot be instruction 10594 // selected). 10595 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0)); 10596 assert(LHS.getNode()->hasOneUse() && 10597 "Counter decrement has more than one use"); 10598 10599 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other, 10600 N->getOperand(0), N->getOperand(4)); 10601 } 10602 10603 int CompareOpc; 10604 bool isDot; 10605 10606 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN && 10607 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) && 10608 getAltivecCompareInfo(LHS, CompareOpc, isDot, Subtarget)) { 10609 assert(isDot && "Can't compare against a vector result!"); 10610 10611 // If this is a comparison against something other than 0/1, then we know 10612 // that the condition is never/always true. 10613 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue(); 10614 if (Val != 0 && Val != 1) { 10615 if (CC == ISD::SETEQ) // Cond never true, remove branch. 10616 return N->getOperand(0); 10617 // Always !=, turn it into an unconditional branch. 10618 return DAG.getNode(ISD::BR, dl, MVT::Other, 10619 N->getOperand(0), N->getOperand(4)); 10620 } 10621 10622 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0); 10623 10624 // Create the PPCISD altivec 'dot' comparison node. 10625 SDValue Ops[] = { 10626 LHS.getOperand(2), // LHS of compare 10627 LHS.getOperand(3), // RHS of compare 10628 DAG.getConstant(CompareOpc, dl, MVT::i32) 10629 }; 10630 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue }; 10631 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops); 10632 10633 // Unpack the result based on how the target uses it. 10634 PPC::Predicate CompOpc; 10635 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) { 10636 default: // Can't happen, don't crash on invalid number though. 10637 case 0: // Branch on the value of the EQ bit of CR6. 10638 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE; 10639 break; 10640 case 1: // Branch on the inverted value of the EQ bit of CR6. 10641 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ; 10642 break; 10643 case 2: // Branch on the value of the LT bit of CR6. 10644 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE; 10645 break; 10646 case 3: // Branch on the inverted value of the LT bit of CR6. 10647 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT; 10648 break; 10649 } 10650 10651 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0), 10652 DAG.getConstant(CompOpc, dl, MVT::i32), 10653 DAG.getRegister(PPC::CR6, MVT::i32), 10654 N->getOperand(4), CompNode.getValue(1)); 10655 } 10656 break; 10657 } 10658 } 10659 10660 return SDValue(); 10661 } 10662 10663 SDValue 10664 PPCTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor, 10665 SelectionDAG &DAG, 10666 std::vector<SDNode *> *Created) const { 10667 // fold (sdiv X, pow2) 10668 EVT VT = N->getValueType(0); 10669 if (VT == MVT::i64 && !Subtarget.isPPC64()) 10670 return SDValue(); 10671 if ((VT != MVT::i32 && VT != MVT::i64) || 10672 !(Divisor.isPowerOf2() || (-Divisor).isPowerOf2())) 10673 return SDValue(); 10674 10675 SDLoc DL(N); 10676 SDValue N0 = N->getOperand(0); 10677 10678 bool IsNegPow2 = (-Divisor).isPowerOf2(); 10679 unsigned Lg2 = (IsNegPow2 ? -Divisor : Divisor).countTrailingZeros(); 10680 SDValue ShiftAmt = DAG.getConstant(Lg2, DL, VT); 10681 10682 SDValue Op = DAG.getNode(PPCISD::SRA_ADDZE, DL, VT, N0, ShiftAmt); 10683 if (Created) 10684 Created->push_back(Op.getNode()); 10685 10686 if (IsNegPow2) { 10687 Op = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Op); 10688 if (Created) 10689 Created->push_back(Op.getNode()); 10690 } 10691 10692 return Op; 10693 } 10694 10695 //===----------------------------------------------------------------------===// 10696 // Inline Assembly Support 10697 //===----------------------------------------------------------------------===// 10698 10699 void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, 10700 APInt &KnownZero, 10701 APInt &KnownOne, 10702 const SelectionDAG &DAG, 10703 unsigned Depth) const { 10704 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0); 10705 switch (Op.getOpcode()) { 10706 default: break; 10707 case PPCISD::LBRX: { 10708 // lhbrx is known to have the top bits cleared out. 10709 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16) 10710 KnownZero = 0xFFFF0000; 10711 break; 10712 } 10713 case ISD::INTRINSIC_WO_CHAIN: { 10714 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) { 10715 default: break; 10716 case Intrinsic::ppc_altivec_vcmpbfp_p: 10717 case Intrinsic::ppc_altivec_vcmpeqfp_p: 10718 case Intrinsic::ppc_altivec_vcmpequb_p: 10719 case Intrinsic::ppc_altivec_vcmpequh_p: 10720 case Intrinsic::ppc_altivec_vcmpequw_p: 10721 case Intrinsic::ppc_altivec_vcmpequd_p: 10722 case Intrinsic::ppc_altivec_vcmpgefp_p: 10723 case Intrinsic::ppc_altivec_vcmpgtfp_p: 10724 case Intrinsic::ppc_altivec_vcmpgtsb_p: 10725 case Intrinsic::ppc_altivec_vcmpgtsh_p: 10726 case Intrinsic::ppc_altivec_vcmpgtsw_p: 10727 case Intrinsic::ppc_altivec_vcmpgtsd_p: 10728 case Intrinsic::ppc_altivec_vcmpgtub_p: 10729 case Intrinsic::ppc_altivec_vcmpgtuh_p: 10730 case Intrinsic::ppc_altivec_vcmpgtuw_p: 10731 case Intrinsic::ppc_altivec_vcmpgtud_p: 10732 KnownZero = ~1U; // All bits but the low one are known to be zero. 10733 break; 10734 } 10735 } 10736 } 10737 } 10738 10739 unsigned PPCTargetLowering::getPrefLoopAlignment(MachineLoop *ML) const { 10740 switch (Subtarget.getDarwinDirective()) { 10741 default: break; 10742 case PPC::DIR_970: 10743 case PPC::DIR_PWR4: 10744 case PPC::DIR_PWR5: 10745 case PPC::DIR_PWR5X: 10746 case PPC::DIR_PWR6: 10747 case PPC::DIR_PWR6X: 10748 case PPC::DIR_PWR7: 10749 case PPC::DIR_PWR8: { 10750 if (!ML) 10751 break; 10752 10753 const PPCInstrInfo *TII = Subtarget.getInstrInfo(); 10754 10755 // For small loops (between 5 and 8 instructions), align to a 32-byte 10756 // boundary so that the entire loop fits in one instruction-cache line. 10757 uint64_t LoopSize = 0; 10758 for (auto I = ML->block_begin(), IE = ML->block_end(); I != IE; ++I) 10759 for (auto J = (*I)->begin(), JE = (*I)->end(); J != JE; ++J) 10760 LoopSize += TII->GetInstSizeInBytes(J); 10761 10762 if (LoopSize > 16 && LoopSize <= 32) 10763 return 5; 10764 10765 break; 10766 } 10767 } 10768 10769 return TargetLowering::getPrefLoopAlignment(ML); 10770 } 10771 10772 /// getConstraintType - Given a constraint, return the type of 10773 /// constraint it is for this target. 10774 PPCTargetLowering::ConstraintType 10775 PPCTargetLowering::getConstraintType(StringRef Constraint) const { 10776 if (Constraint.size() == 1) { 10777 switch (Constraint[0]) { 10778 default: break; 10779 case 'b': 10780 case 'r': 10781 case 'f': 10782 case 'v': 10783 case 'y': 10784 return C_RegisterClass; 10785 case 'Z': 10786 // FIXME: While Z does indicate a memory constraint, it specifically 10787 // indicates an r+r address (used in conjunction with the 'y' modifier 10788 // in the replacement string). Currently, we're forcing the base 10789 // register to be r0 in the asm printer (which is interpreted as zero) 10790 // and forming the complete address in the second register. This is 10791 // suboptimal. 10792 return C_Memory; 10793 } 10794 } else if (Constraint == "wc") { // individual CR bits. 10795 return C_RegisterClass; 10796 } else if (Constraint == "wa" || Constraint == "wd" || 10797 Constraint == "wf" || Constraint == "ws") { 10798 return C_RegisterClass; // VSX registers. 10799 } 10800 return TargetLowering::getConstraintType(Constraint); 10801 } 10802 10803 /// Examine constraint type and operand type and determine a weight value. 10804 /// This object must already have been set up with the operand type 10805 /// and the current alternative constraint selected. 10806 TargetLowering::ConstraintWeight 10807 PPCTargetLowering::getSingleConstraintMatchWeight( 10808 AsmOperandInfo &info, const char *constraint) const { 10809 ConstraintWeight weight = CW_Invalid; 10810 Value *CallOperandVal = info.CallOperandVal; 10811 // If we don't have a value, we can't do a match, 10812 // but allow it at the lowest weight. 10813 if (!CallOperandVal) 10814 return CW_Default; 10815 Type *type = CallOperandVal->getType(); 10816 10817 // Look at the constraint type. 10818 if (StringRef(constraint) == "wc" && type->isIntegerTy(1)) 10819 return CW_Register; // an individual CR bit. 10820 else if ((StringRef(constraint) == "wa" || 10821 StringRef(constraint) == "wd" || 10822 StringRef(constraint) == "wf") && 10823 type->isVectorTy()) 10824 return CW_Register; 10825 else if (StringRef(constraint) == "ws" && type->isDoubleTy()) 10826 return CW_Register; 10827 10828 switch (*constraint) { 10829 default: 10830 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 10831 break; 10832 case 'b': 10833 if (type->isIntegerTy()) 10834 weight = CW_Register; 10835 break; 10836 case 'f': 10837 if (type->isFloatTy()) 10838 weight = CW_Register; 10839 break; 10840 case 'd': 10841 if (type->isDoubleTy()) 10842 weight = CW_Register; 10843 break; 10844 case 'v': 10845 if (type->isVectorTy()) 10846 weight = CW_Register; 10847 break; 10848 case 'y': 10849 weight = CW_Register; 10850 break; 10851 case 'Z': 10852 weight = CW_Memory; 10853 break; 10854 } 10855 return weight; 10856 } 10857 10858 std::pair<unsigned, const TargetRegisterClass *> 10859 PPCTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 10860 StringRef Constraint, 10861 MVT VT) const { 10862 if (Constraint.size() == 1) { 10863 // GCC RS6000 Constraint Letters 10864 switch (Constraint[0]) { 10865 case 'b': // R1-R31 10866 if (VT == MVT::i64 && Subtarget.isPPC64()) 10867 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass); 10868 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass); 10869 case 'r': // R0-R31 10870 if (VT == MVT::i64 && Subtarget.isPPC64()) 10871 return std::make_pair(0U, &PPC::G8RCRegClass); 10872 return std::make_pair(0U, &PPC::GPRCRegClass); 10873 case 'f': 10874 if (VT == MVT::f32 || VT == MVT::i32) 10875 return std::make_pair(0U, &PPC::F4RCRegClass); 10876 if (VT == MVT::f64 || VT == MVT::i64) 10877 return std::make_pair(0U, &PPC::F8RCRegClass); 10878 if (VT == MVT::v4f64 && Subtarget.hasQPX()) 10879 return std::make_pair(0U, &PPC::QFRCRegClass); 10880 if (VT == MVT::v4f32 && Subtarget.hasQPX()) 10881 return std::make_pair(0U, &PPC::QSRCRegClass); 10882 break; 10883 case 'v': 10884 if (VT == MVT::v4f64 && Subtarget.hasQPX()) 10885 return std::make_pair(0U, &PPC::QFRCRegClass); 10886 if (VT == MVT::v4f32 && Subtarget.hasQPX()) 10887 return std::make_pair(0U, &PPC::QSRCRegClass); 10888 return std::make_pair(0U, &PPC::VRRCRegClass); 10889 case 'y': // crrc 10890 return std::make_pair(0U, &PPC::CRRCRegClass); 10891 } 10892 } else if (Constraint == "wc") { // an individual CR bit. 10893 return std::make_pair(0U, &PPC::CRBITRCRegClass); 10894 } else if (Constraint == "wa" || Constraint == "wd" || 10895 Constraint == "wf") { 10896 return std::make_pair(0U, &PPC::VSRCRegClass); 10897 } else if (Constraint == "ws") { 10898 if (VT == MVT::f32) 10899 return std::make_pair(0U, &PPC::VSSRCRegClass); 10900 else 10901 return std::make_pair(0U, &PPC::VSFRCRegClass); 10902 } 10903 10904 std::pair<unsigned, const TargetRegisterClass *> R = 10905 TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 10906 10907 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers 10908 // (which we call X[0-9]+). If a 64-bit value has been requested, and a 10909 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent 10910 // register. 10911 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use 10912 // the AsmName field from *RegisterInfo.td, then this would not be necessary. 10913 if (R.first && VT == MVT::i64 && Subtarget.isPPC64() && 10914 PPC::GPRCRegClass.contains(R.first)) 10915 return std::make_pair(TRI->getMatchingSuperReg(R.first, 10916 PPC::sub_32, &PPC::G8RCRegClass), 10917 &PPC::G8RCRegClass); 10918 10919 // GCC accepts 'cc' as an alias for 'cr0', and we need to do the same. 10920 if (!R.second && StringRef("{cc}").equals_lower(Constraint)) { 10921 R.first = PPC::CR0; 10922 R.second = &PPC::CRRCRegClass; 10923 } 10924 10925 return R; 10926 } 10927 10928 10929 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 10930 /// vector. If it is invalid, don't add anything to Ops. 10931 void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 10932 std::string &Constraint, 10933 std::vector<SDValue>&Ops, 10934 SelectionDAG &DAG) const { 10935 SDValue Result; 10936 10937 // Only support length 1 constraints. 10938 if (Constraint.length() > 1) return; 10939 10940 char Letter = Constraint[0]; 10941 switch (Letter) { 10942 default: break; 10943 case 'I': 10944 case 'J': 10945 case 'K': 10946 case 'L': 10947 case 'M': 10948 case 'N': 10949 case 'O': 10950 case 'P': { 10951 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op); 10952 if (!CST) return; // Must be an immediate to match. 10953 SDLoc dl(Op); 10954 int64_t Value = CST->getSExtValue(); 10955 EVT TCVT = MVT::i64; // All constants taken to be 64 bits so that negative 10956 // numbers are printed as such. 10957 switch (Letter) { 10958 default: llvm_unreachable("Unknown constraint letter!"); 10959 case 'I': // "I" is a signed 16-bit constant. 10960 if (isInt<16>(Value)) 10961 Result = DAG.getTargetConstant(Value, dl, TCVT); 10962 break; 10963 case 'J': // "J" is a constant with only the high-order 16 bits nonzero. 10964 if (isShiftedUInt<16, 16>(Value)) 10965 Result = DAG.getTargetConstant(Value, dl, TCVT); 10966 break; 10967 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits. 10968 if (isShiftedInt<16, 16>(Value)) 10969 Result = DAG.getTargetConstant(Value, dl, TCVT); 10970 break; 10971 case 'K': // "K" is a constant with only the low-order 16 bits nonzero. 10972 if (isUInt<16>(Value)) 10973 Result = DAG.getTargetConstant(Value, dl, TCVT); 10974 break; 10975 case 'M': // "M" is a constant that is greater than 31. 10976 if (Value > 31) 10977 Result = DAG.getTargetConstant(Value, dl, TCVT); 10978 break; 10979 case 'N': // "N" is a positive constant that is an exact power of two. 10980 if (Value > 0 && isPowerOf2_64(Value)) 10981 Result = DAG.getTargetConstant(Value, dl, TCVT); 10982 break; 10983 case 'O': // "O" is the constant zero. 10984 if (Value == 0) 10985 Result = DAG.getTargetConstant(Value, dl, TCVT); 10986 break; 10987 case 'P': // "P" is a constant whose negation is a signed 16-bit constant. 10988 if (isInt<16>(-Value)) 10989 Result = DAG.getTargetConstant(Value, dl, TCVT); 10990 break; 10991 } 10992 break; 10993 } 10994 } 10995 10996 if (Result.getNode()) { 10997 Ops.push_back(Result); 10998 return; 10999 } 11000 11001 // Handle standard constraint letters. 11002 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 11003 } 11004 11005 // isLegalAddressingMode - Return true if the addressing mode represented 11006 // by AM is legal for this target, for a load/store of the specified type. 11007 bool PPCTargetLowering::isLegalAddressingMode(const DataLayout &DL, 11008 const AddrMode &AM, Type *Ty, 11009 unsigned AS) const { 11010 // PPC does not allow r+i addressing modes for vectors! 11011 if (Ty->isVectorTy() && AM.BaseOffs != 0) 11012 return false; 11013 11014 // PPC allows a sign-extended 16-bit immediate field. 11015 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1) 11016 return false; 11017 11018 // No global is ever allowed as a base. 11019 if (AM.BaseGV) 11020 return false; 11021 11022 // PPC only support r+r, 11023 switch (AM.Scale) { 11024 case 0: // "r+i" or just "i", depending on HasBaseReg. 11025 break; 11026 case 1: 11027 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed. 11028 return false; 11029 // Otherwise we have r+r or r+i. 11030 break; 11031 case 2: 11032 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed. 11033 return false; 11034 // Allow 2*r as r+r. 11035 break; 11036 default: 11037 // No other scales are supported. 11038 return false; 11039 } 11040 11041 return true; 11042 } 11043 11044 SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op, 11045 SelectionDAG &DAG) const { 11046 MachineFunction &MF = DAG.getMachineFunction(); 11047 MachineFrameInfo *MFI = MF.getFrameInfo(); 11048 MFI->setReturnAddressIsTaken(true); 11049 11050 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 11051 return SDValue(); 11052 11053 SDLoc dl(Op); 11054 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 11055 11056 // Make sure the function does not optimize away the store of the RA to 11057 // the stack. 11058 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 11059 FuncInfo->setLRStoreRequired(); 11060 bool isPPC64 = Subtarget.isPPC64(); 11061 auto PtrVT = getPointerTy(MF.getDataLayout()); 11062 11063 if (Depth > 0) { 11064 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 11065 SDValue Offset = 11066 DAG.getConstant(Subtarget.getFrameLowering()->getReturnSaveOffset(), dl, 11067 isPPC64 ? MVT::i64 : MVT::i32); 11068 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), 11069 DAG.getNode(ISD::ADD, dl, PtrVT, FrameAddr, Offset), 11070 MachinePointerInfo(), false, false, false, 0); 11071 } 11072 11073 // Just load the return address off the stack. 11074 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG); 11075 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), RetAddrFI, 11076 MachinePointerInfo(), false, false, false, 0); 11077 } 11078 11079 SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op, 11080 SelectionDAG &DAG) const { 11081 SDLoc dl(Op); 11082 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 11083 11084 MachineFunction &MF = DAG.getMachineFunction(); 11085 MachineFrameInfo *MFI = MF.getFrameInfo(); 11086 MFI->setFrameAddressIsTaken(true); 11087 11088 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(MF.getDataLayout()); 11089 bool isPPC64 = PtrVT == MVT::i64; 11090 11091 // Naked functions never have a frame pointer, and so we use r1. For all 11092 // other functions, this decision must be delayed until during PEI. 11093 unsigned FrameReg; 11094 if (MF.getFunction()->hasFnAttribute(Attribute::Naked)) 11095 FrameReg = isPPC64 ? PPC::X1 : PPC::R1; 11096 else 11097 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP; 11098 11099 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, 11100 PtrVT); 11101 while (Depth--) 11102 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(), 11103 FrameAddr, MachinePointerInfo(), false, false, 11104 false, 0); 11105 return FrameAddr; 11106 } 11107 11108 // FIXME? Maybe this could be a TableGen attribute on some registers and 11109 // this table could be generated automatically from RegInfo. 11110 unsigned PPCTargetLowering::getRegisterByName(const char* RegName, EVT VT, 11111 SelectionDAG &DAG) const { 11112 bool isPPC64 = Subtarget.isPPC64(); 11113 bool isDarwinABI = Subtarget.isDarwinABI(); 11114 11115 if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) || 11116 (!isPPC64 && VT != MVT::i32)) 11117 report_fatal_error("Invalid register global variable type"); 11118 11119 bool is64Bit = isPPC64 && VT == MVT::i64; 11120 unsigned Reg = StringSwitch<unsigned>(RegName) 11121 .Case("r1", is64Bit ? PPC::X1 : PPC::R1) 11122 .Case("r2", (isDarwinABI || isPPC64) ? 0 : PPC::R2) 11123 .Case("r13", (!isPPC64 && isDarwinABI) ? 0 : 11124 (is64Bit ? PPC::X13 : PPC::R13)) 11125 .Default(0); 11126 11127 if (Reg) 11128 return Reg; 11129 report_fatal_error("Invalid register name global variable"); 11130 } 11131 11132 bool 11133 PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 11134 // The PowerPC target isn't yet aware of offsets. 11135 return false; 11136 } 11137 11138 bool PPCTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 11139 const CallInst &I, 11140 unsigned Intrinsic) const { 11141 11142 switch (Intrinsic) { 11143 case Intrinsic::ppc_qpx_qvlfd: 11144 case Intrinsic::ppc_qpx_qvlfs: 11145 case Intrinsic::ppc_qpx_qvlfcd: 11146 case Intrinsic::ppc_qpx_qvlfcs: 11147 case Intrinsic::ppc_qpx_qvlfiwa: 11148 case Intrinsic::ppc_qpx_qvlfiwz: 11149 case Intrinsic::ppc_altivec_lvx: 11150 case Intrinsic::ppc_altivec_lvxl: 11151 case Intrinsic::ppc_altivec_lvebx: 11152 case Intrinsic::ppc_altivec_lvehx: 11153 case Intrinsic::ppc_altivec_lvewx: 11154 case Intrinsic::ppc_vsx_lxvd2x: 11155 case Intrinsic::ppc_vsx_lxvw4x: { 11156 EVT VT; 11157 switch (Intrinsic) { 11158 case Intrinsic::ppc_altivec_lvebx: 11159 VT = MVT::i8; 11160 break; 11161 case Intrinsic::ppc_altivec_lvehx: 11162 VT = MVT::i16; 11163 break; 11164 case Intrinsic::ppc_altivec_lvewx: 11165 VT = MVT::i32; 11166 break; 11167 case Intrinsic::ppc_vsx_lxvd2x: 11168 VT = MVT::v2f64; 11169 break; 11170 case Intrinsic::ppc_qpx_qvlfd: 11171 VT = MVT::v4f64; 11172 break; 11173 case Intrinsic::ppc_qpx_qvlfs: 11174 VT = MVT::v4f32; 11175 break; 11176 case Intrinsic::ppc_qpx_qvlfcd: 11177 VT = MVT::v2f64; 11178 break; 11179 case Intrinsic::ppc_qpx_qvlfcs: 11180 VT = MVT::v2f32; 11181 break; 11182 default: 11183 VT = MVT::v4i32; 11184 break; 11185 } 11186 11187 Info.opc = ISD::INTRINSIC_W_CHAIN; 11188 Info.memVT = VT; 11189 Info.ptrVal = I.getArgOperand(0); 11190 Info.offset = -VT.getStoreSize()+1; 11191 Info.size = 2*VT.getStoreSize()-1; 11192 Info.align = 1; 11193 Info.vol = false; 11194 Info.readMem = true; 11195 Info.writeMem = false; 11196 return true; 11197 } 11198 case Intrinsic::ppc_qpx_qvlfda: 11199 case Intrinsic::ppc_qpx_qvlfsa: 11200 case Intrinsic::ppc_qpx_qvlfcda: 11201 case Intrinsic::ppc_qpx_qvlfcsa: 11202 case Intrinsic::ppc_qpx_qvlfiwaa: 11203 case Intrinsic::ppc_qpx_qvlfiwza: { 11204 EVT VT; 11205 switch (Intrinsic) { 11206 case Intrinsic::ppc_qpx_qvlfda: 11207 VT = MVT::v4f64; 11208 break; 11209 case Intrinsic::ppc_qpx_qvlfsa: 11210 VT = MVT::v4f32; 11211 break; 11212 case Intrinsic::ppc_qpx_qvlfcda: 11213 VT = MVT::v2f64; 11214 break; 11215 case Intrinsic::ppc_qpx_qvlfcsa: 11216 VT = MVT::v2f32; 11217 break; 11218 default: 11219 VT = MVT::v4i32; 11220 break; 11221 } 11222 11223 Info.opc = ISD::INTRINSIC_W_CHAIN; 11224 Info.memVT = VT; 11225 Info.ptrVal = I.getArgOperand(0); 11226 Info.offset = 0; 11227 Info.size = VT.getStoreSize(); 11228 Info.align = 1; 11229 Info.vol = false; 11230 Info.readMem = true; 11231 Info.writeMem = false; 11232 return true; 11233 } 11234 case Intrinsic::ppc_qpx_qvstfd: 11235 case Intrinsic::ppc_qpx_qvstfs: 11236 case Intrinsic::ppc_qpx_qvstfcd: 11237 case Intrinsic::ppc_qpx_qvstfcs: 11238 case Intrinsic::ppc_qpx_qvstfiw: 11239 case Intrinsic::ppc_altivec_stvx: 11240 case Intrinsic::ppc_altivec_stvxl: 11241 case Intrinsic::ppc_altivec_stvebx: 11242 case Intrinsic::ppc_altivec_stvehx: 11243 case Intrinsic::ppc_altivec_stvewx: 11244 case Intrinsic::ppc_vsx_stxvd2x: 11245 case Intrinsic::ppc_vsx_stxvw4x: { 11246 EVT VT; 11247 switch (Intrinsic) { 11248 case Intrinsic::ppc_altivec_stvebx: 11249 VT = MVT::i8; 11250 break; 11251 case Intrinsic::ppc_altivec_stvehx: 11252 VT = MVT::i16; 11253 break; 11254 case Intrinsic::ppc_altivec_stvewx: 11255 VT = MVT::i32; 11256 break; 11257 case Intrinsic::ppc_vsx_stxvd2x: 11258 VT = MVT::v2f64; 11259 break; 11260 case Intrinsic::ppc_qpx_qvstfd: 11261 VT = MVT::v4f64; 11262 break; 11263 case Intrinsic::ppc_qpx_qvstfs: 11264 VT = MVT::v4f32; 11265 break; 11266 case Intrinsic::ppc_qpx_qvstfcd: 11267 VT = MVT::v2f64; 11268 break; 11269 case Intrinsic::ppc_qpx_qvstfcs: 11270 VT = MVT::v2f32; 11271 break; 11272 default: 11273 VT = MVT::v4i32; 11274 break; 11275 } 11276 11277 Info.opc = ISD::INTRINSIC_VOID; 11278 Info.memVT = VT; 11279 Info.ptrVal = I.getArgOperand(1); 11280 Info.offset = -VT.getStoreSize()+1; 11281 Info.size = 2*VT.getStoreSize()-1; 11282 Info.align = 1; 11283 Info.vol = false; 11284 Info.readMem = false; 11285 Info.writeMem = true; 11286 return true; 11287 } 11288 case Intrinsic::ppc_qpx_qvstfda: 11289 case Intrinsic::ppc_qpx_qvstfsa: 11290 case Intrinsic::ppc_qpx_qvstfcda: 11291 case Intrinsic::ppc_qpx_qvstfcsa: 11292 case Intrinsic::ppc_qpx_qvstfiwa: { 11293 EVT VT; 11294 switch (Intrinsic) { 11295 case Intrinsic::ppc_qpx_qvstfda: 11296 VT = MVT::v4f64; 11297 break; 11298 case Intrinsic::ppc_qpx_qvstfsa: 11299 VT = MVT::v4f32; 11300 break; 11301 case Intrinsic::ppc_qpx_qvstfcda: 11302 VT = MVT::v2f64; 11303 break; 11304 case Intrinsic::ppc_qpx_qvstfcsa: 11305 VT = MVT::v2f32; 11306 break; 11307 default: 11308 VT = MVT::v4i32; 11309 break; 11310 } 11311 11312 Info.opc = ISD::INTRINSIC_VOID; 11313 Info.memVT = VT; 11314 Info.ptrVal = I.getArgOperand(1); 11315 Info.offset = 0; 11316 Info.size = VT.getStoreSize(); 11317 Info.align = 1; 11318 Info.vol = false; 11319 Info.readMem = false; 11320 Info.writeMem = true; 11321 return true; 11322 } 11323 default: 11324 break; 11325 } 11326 11327 return false; 11328 } 11329 11330 /// getOptimalMemOpType - Returns the target specific optimal type for load 11331 /// and store operations as a result of memset, memcpy, and memmove 11332 /// lowering. If DstAlign is zero that means it's safe to destination 11333 /// alignment can satisfy any constraint. Similarly if SrcAlign is zero it 11334 /// means there isn't a need to check it against alignment requirement, 11335 /// probably because the source does not need to be loaded. If 'IsMemset' is 11336 /// true, that means it's expanding a memset. If 'ZeroMemset' is true, that 11337 /// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy 11338 /// source is constant so it does not need to be loaded. 11339 /// It returns EVT::Other if the type should be determined using generic 11340 /// target-independent logic. 11341 EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size, 11342 unsigned DstAlign, unsigned SrcAlign, 11343 bool IsMemset, bool ZeroMemset, 11344 bool MemcpyStrSrc, 11345 MachineFunction &MF) const { 11346 if (getTargetMachine().getOptLevel() != CodeGenOpt::None) { 11347 const Function *F = MF.getFunction(); 11348 // When expanding a memset, require at least two QPX instructions to cover 11349 // the cost of loading the value to be stored from the constant pool. 11350 if (Subtarget.hasQPX() && Size >= 32 && (!IsMemset || Size >= 64) && 11351 (!SrcAlign || SrcAlign >= 32) && (!DstAlign || DstAlign >= 32) && 11352 !F->hasFnAttribute(Attribute::NoImplicitFloat)) { 11353 return MVT::v4f64; 11354 } 11355 11356 // We should use Altivec/VSX loads and stores when available. For unaligned 11357 // addresses, unaligned VSX loads are only fast starting with the P8. 11358 if (Subtarget.hasAltivec() && Size >= 16 && 11359 (((!SrcAlign || SrcAlign >= 16) && (!DstAlign || DstAlign >= 16)) || 11360 ((IsMemset && Subtarget.hasVSX()) || Subtarget.hasP8Vector()))) 11361 return MVT::v4i32; 11362 } 11363 11364 if (Subtarget.isPPC64()) { 11365 return MVT::i64; 11366 } 11367 11368 return MVT::i32; 11369 } 11370 11371 /// \brief Returns true if it is beneficial to convert a load of a constant 11372 /// to just the constant itself. 11373 bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 11374 Type *Ty) const { 11375 assert(Ty->isIntegerTy()); 11376 11377 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 11378 if (BitSize == 0 || BitSize > 64) 11379 return false; 11380 return true; 11381 } 11382 11383 bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const { 11384 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 11385 return false; 11386 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits(); 11387 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits(); 11388 return NumBits1 == 64 && NumBits2 == 32; 11389 } 11390 11391 bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const { 11392 if (!VT1.isInteger() || !VT2.isInteger()) 11393 return false; 11394 unsigned NumBits1 = VT1.getSizeInBits(); 11395 unsigned NumBits2 = VT2.getSizeInBits(); 11396 return NumBits1 == 64 && NumBits2 == 32; 11397 } 11398 11399 bool PPCTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 11400 // Generally speaking, zexts are not free, but they are free when they can be 11401 // folded with other operations. 11402 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Val)) { 11403 EVT MemVT = LD->getMemoryVT(); 11404 if ((MemVT == MVT::i1 || MemVT == MVT::i8 || MemVT == MVT::i16 || 11405 (Subtarget.isPPC64() && MemVT == MVT::i32)) && 11406 (LD->getExtensionType() == ISD::NON_EXTLOAD || 11407 LD->getExtensionType() == ISD::ZEXTLOAD)) 11408 return true; 11409 } 11410 11411 // FIXME: Add other cases... 11412 // - 32-bit shifts with a zext to i64 11413 // - zext after ctlz, bswap, etc. 11414 // - zext after and by a constant mask 11415 11416 return TargetLowering::isZExtFree(Val, VT2); 11417 } 11418 11419 bool PPCTargetLowering::isFPExtFree(EVT VT) const { 11420 assert(VT.isFloatingPoint()); 11421 return true; 11422 } 11423 11424 bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 11425 return isInt<16>(Imm) || isUInt<16>(Imm); 11426 } 11427 11428 bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const { 11429 return isInt<16>(Imm) || isUInt<16>(Imm); 11430 } 11431 11432 bool PPCTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 11433 unsigned, 11434 unsigned, 11435 bool *Fast) const { 11436 if (DisablePPCUnaligned) 11437 return false; 11438 11439 // PowerPC supports unaligned memory access for simple non-vector types. 11440 // Although accessing unaligned addresses is not as efficient as accessing 11441 // aligned addresses, it is generally more efficient than manual expansion, 11442 // and generally only traps for software emulation when crossing page 11443 // boundaries. 11444 11445 if (!VT.isSimple()) 11446 return false; 11447 11448 if (VT.getSimpleVT().isVector()) { 11449 if (Subtarget.hasVSX()) { 11450 if (VT != MVT::v2f64 && VT != MVT::v2i64 && 11451 VT != MVT::v4f32 && VT != MVT::v4i32) 11452 return false; 11453 } else { 11454 return false; 11455 } 11456 } 11457 11458 if (VT == MVT::ppcf128) 11459 return false; 11460 11461 if (Fast) 11462 *Fast = true; 11463 11464 return true; 11465 } 11466 11467 bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { 11468 VT = VT.getScalarType(); 11469 11470 if (!VT.isSimple()) 11471 return false; 11472 11473 switch (VT.getSimpleVT().SimpleTy) { 11474 case MVT::f32: 11475 case MVT::f64: 11476 return true; 11477 default: 11478 break; 11479 } 11480 11481 return false; 11482 } 11483 11484 const MCPhysReg * 11485 PPCTargetLowering::getScratchRegisters(CallingConv::ID) const { 11486 // LR is a callee-save register, but we must treat it as clobbered by any call 11487 // site. Hence we include LR in the scratch registers, which are in turn added 11488 // as implicit-defs for stackmaps and patchpoints. The same reasoning applies 11489 // to CTR, which is used by any indirect call. 11490 static const MCPhysReg ScratchRegs[] = { 11491 PPC::X12, PPC::LR8, PPC::CTR8, 0 11492 }; 11493 11494 return ScratchRegs; 11495 } 11496 11497 bool 11498 PPCTargetLowering::shouldExpandBuildVectorWithShuffles( 11499 EVT VT , unsigned DefinedValues) const { 11500 if (VT == MVT::v2i64) 11501 return Subtarget.hasDirectMove(); // Don't need stack ops with direct moves 11502 11503 if (Subtarget.hasQPX()) { 11504 if (VT == MVT::v4f32 || VT == MVT::v4f64 || VT == MVT::v4i1) 11505 return true; 11506 } 11507 11508 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues); 11509 } 11510 11511 Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const { 11512 if (DisableILPPref || Subtarget.enableMachineScheduler()) 11513 return TargetLowering::getSchedulingPreference(N); 11514 11515 return Sched::ILP; 11516 } 11517 11518 // Create a fast isel object. 11519 FastISel * 11520 PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo, 11521 const TargetLibraryInfo *LibInfo) const { 11522 return PPC::createFastISel(FuncInfo, LibInfo); 11523 } 11524