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 "MCTargetDesc/PPCPredicates.h" 15 #include "PPC.h" 16 #include "PPCCallingConv.h" 17 #include "PPCCCState.h" 18 #include "PPCFrameLowering.h" 19 #include "PPCInstrInfo.h" 20 #include "PPCISelLowering.h" 21 #include "PPCMachineFunctionInfo.h" 22 #include "PPCPerfectShuffle.h" 23 #include "PPCRegisterInfo.h" 24 #include "PPCSubtarget.h" 25 #include "PPCTargetMachine.h" 26 #include "llvm/ADT/APFloat.h" 27 #include "llvm/ADT/APInt.h" 28 #include "llvm/ADT/ArrayRef.h" 29 #include "llvm/ADT/DenseMap.h" 30 #include "llvm/ADT/None.h" 31 #include "llvm/ADT/SmallPtrSet.h" 32 #include "llvm/ADT/SmallSet.h" 33 #include "llvm/ADT/SmallVector.h" 34 #include "llvm/ADT/Statistic.h" 35 #include "llvm/ADT/STLExtras.h" 36 #include "llvm/ADT/StringRef.h" 37 #include "llvm/ADT/StringSwitch.h" 38 #include "llvm/CodeGen/CallingConvLower.h" 39 #include "llvm/CodeGen/ISDOpcodes.h" 40 #include "llvm/CodeGen/MachineBasicBlock.h" 41 #include "llvm/CodeGen/MachineFrameInfo.h" 42 #include "llvm/CodeGen/MachineFunction.h" 43 #include "llvm/CodeGen/MachineInstr.h" 44 #include "llvm/CodeGen/MachineInstrBuilder.h" 45 #include "llvm/CodeGen/MachineJumpTableInfo.h" 46 #include "llvm/CodeGen/MachineLoopInfo.h" 47 #include "llvm/CodeGen/MachineMemOperand.h" 48 #include "llvm/CodeGen/MachineOperand.h" 49 #include "llvm/CodeGen/MachineRegisterInfo.h" 50 #include "llvm/CodeGen/MachineValueType.h" 51 #include "llvm/CodeGen/RuntimeLibcalls.h" 52 #include "llvm/CodeGen/SelectionDAG.h" 53 #include "llvm/CodeGen/SelectionDAGNodes.h" 54 #include "llvm/CodeGen/ValueTypes.h" 55 #include "llvm/IR/CallingConv.h" 56 #include "llvm/IR/CallSite.h" 57 #include "llvm/IR/Constant.h" 58 #include "llvm/IR/Constants.h" 59 #include "llvm/IR/DataLayout.h" 60 #include "llvm/IR/DebugLoc.h" 61 #include "llvm/IR/DerivedTypes.h" 62 #include "llvm/IR/Function.h" 63 #include "llvm/IR/GlobalValue.h" 64 #include "llvm/IR/Instructions.h" 65 #include "llvm/IR/Intrinsics.h" 66 #include "llvm/IR/IRBuilder.h" 67 #include "llvm/IR/Module.h" 68 #include "llvm/IR/Type.h" 69 #include "llvm/IR/Use.h" 70 #include "llvm/IR/Value.h" 71 #include "llvm/MC/MCExpr.h" 72 #include "llvm/MC/MCRegisterInfo.h" 73 #include "llvm/Support/AtomicOrdering.h" 74 #include "llvm/Support/BranchProbability.h" 75 #include "llvm/Support/Casting.h" 76 #include "llvm/Support/CodeGen.h" 77 #include "llvm/Support/CommandLine.h" 78 #include "llvm/Support/Compiler.h" 79 #include "llvm/Support/Debug.h" 80 #include "llvm/Support/ErrorHandling.h" 81 #include "llvm/Support/Format.h" 82 #include "llvm/Support/KnownBits.h" 83 #include "llvm/Support/MathExtras.h" 84 #include "llvm/Support/raw_ostream.h" 85 #include "llvm/Target/TargetInstrInfo.h" 86 #include "llvm/Target/TargetLowering.h" 87 #include "llvm/Target/TargetMachine.h" 88 #include "llvm/Target/TargetOptions.h" 89 #include "llvm/Target/TargetRegisterInfo.h" 90 #include <algorithm> 91 #include <cassert> 92 #include <cstdint> 93 #include <iterator> 94 #include <list> 95 #include <utility> 96 #include <vector> 97 98 using namespace llvm; 99 100 #define DEBUG_TYPE "ppc-lowering" 101 102 static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc", 103 cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden); 104 105 static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref", 106 cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden); 107 108 static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned", 109 cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden); 110 111 static cl::opt<bool> DisableSCO("disable-ppc-sco", 112 cl::desc("disable sibling call optimization on ppc"), cl::Hidden); 113 114 STATISTIC(NumTailCalls, "Number of tail calls"); 115 STATISTIC(NumSiblingCalls, "Number of sibling calls"); 116 117 // FIXME: Remove this once the bug has been fixed! 118 extern cl::opt<bool> ANDIGlueBug; 119 120 PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM, 121 const PPCSubtarget &STI) 122 : TargetLowering(TM), Subtarget(STI) { 123 // Use _setjmp/_longjmp instead of setjmp/longjmp. 124 setUseUnderscoreSetJmp(true); 125 setUseUnderscoreLongJmp(true); 126 127 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all 128 // arguments are at least 4/8 bytes aligned. 129 bool isPPC64 = Subtarget.isPPC64(); 130 setMinStackArgumentAlignment(isPPC64 ? 8:4); 131 132 // Set up the register classes. 133 addRegisterClass(MVT::i32, &PPC::GPRCRegClass); 134 if (!useSoftFloat()) { 135 addRegisterClass(MVT::f32, &PPC::F4RCRegClass); 136 addRegisterClass(MVT::f64, &PPC::F8RCRegClass); 137 } 138 139 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD 140 for (MVT VT : MVT::integer_valuetypes()) { 141 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 142 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand); 143 } 144 145 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 146 147 // PowerPC has pre-inc load and store's. 148 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal); 149 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal); 150 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal); 151 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal); 152 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal); 153 setIndexedLoadAction(ISD::PRE_INC, MVT::f32, Legal); 154 setIndexedLoadAction(ISD::PRE_INC, MVT::f64, Legal); 155 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal); 156 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal); 157 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal); 158 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal); 159 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal); 160 setIndexedStoreAction(ISD::PRE_INC, MVT::f32, Legal); 161 setIndexedStoreAction(ISD::PRE_INC, MVT::f64, Legal); 162 163 if (Subtarget.useCRBits()) { 164 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 165 166 if (isPPC64 || Subtarget.hasFPCVT()) { 167 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote); 168 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1, 169 isPPC64 ? MVT::i64 : MVT::i32); 170 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote); 171 AddPromotedToType(ISD::UINT_TO_FP, MVT::i1, 172 isPPC64 ? MVT::i64 : MVT::i32); 173 } else { 174 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom); 175 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom); 176 } 177 178 // PowerPC does not support direct load / store of condition registers 179 setOperationAction(ISD::LOAD, MVT::i1, Custom); 180 setOperationAction(ISD::STORE, MVT::i1, Custom); 181 182 // FIXME: Remove this once the ANDI glue bug is fixed: 183 if (ANDIGlueBug) 184 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom); 185 186 for (MVT VT : MVT::integer_valuetypes()) { 187 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 188 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); 189 setTruncStoreAction(VT, MVT::i1, Expand); 190 } 191 192 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass); 193 } 194 195 // This is used in the ppcf128->int sequence. Note it has different semantics 196 // from FP_ROUND: that rounds to nearest, this rounds to zero. 197 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom); 198 199 // We do not currently implement these libm ops for PowerPC. 200 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand); 201 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand); 202 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand); 203 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand); 204 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand); 205 setOperationAction(ISD::FREM, MVT::ppcf128, Expand); 206 207 // PowerPC has no SREM/UREM instructions 208 setOperationAction(ISD::SREM, MVT::i32, Expand); 209 setOperationAction(ISD::UREM, MVT::i32, Expand); 210 setOperationAction(ISD::SREM, MVT::i64, Expand); 211 setOperationAction(ISD::UREM, MVT::i64, Expand); 212 213 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM. 214 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 215 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 216 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand); 217 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand); 218 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 219 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 220 setOperationAction(ISD::UDIVREM, MVT::i64, Expand); 221 setOperationAction(ISD::SDIVREM, MVT::i64, Expand); 222 223 // We don't support sin/cos/sqrt/fmod/pow 224 setOperationAction(ISD::FSIN , MVT::f64, Expand); 225 setOperationAction(ISD::FCOS , MVT::f64, Expand); 226 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 227 setOperationAction(ISD::FREM , MVT::f64, Expand); 228 setOperationAction(ISD::FPOW , MVT::f64, Expand); 229 setOperationAction(ISD::FMA , MVT::f64, Legal); 230 setOperationAction(ISD::FSIN , MVT::f32, Expand); 231 setOperationAction(ISD::FCOS , MVT::f32, Expand); 232 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 233 setOperationAction(ISD::FREM , MVT::f32, Expand); 234 setOperationAction(ISD::FPOW , MVT::f32, Expand); 235 setOperationAction(ISD::FMA , MVT::f32, Legal); 236 237 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); 238 239 // If we're enabling GP optimizations, use hardware square root 240 if (!Subtarget.hasFSQRT() && 241 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTE() && 242 Subtarget.hasFRE())) 243 setOperationAction(ISD::FSQRT, MVT::f64, Expand); 244 245 if (!Subtarget.hasFSQRT() && 246 !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTES() && 247 Subtarget.hasFRES())) 248 setOperationAction(ISD::FSQRT, MVT::f32, Expand); 249 250 if (Subtarget.hasFCPSGN()) { 251 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal); 252 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal); 253 } else { 254 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 255 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); 256 } 257 258 if (Subtarget.hasFPRND()) { 259 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 260 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 261 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 262 setOperationAction(ISD::FROUND, MVT::f64, Legal); 263 264 setOperationAction(ISD::FFLOOR, MVT::f32, Legal); 265 setOperationAction(ISD::FCEIL, MVT::f32, Legal); 266 setOperationAction(ISD::FTRUNC, MVT::f32, Legal); 267 setOperationAction(ISD::FROUND, MVT::f32, Legal); 268 } 269 270 // PowerPC does not have BSWAP 271 // CTPOP or CTTZ were introduced in P8/P9 respectivelly 272 setOperationAction(ISD::BSWAP, MVT::i32 , Expand); 273 setOperationAction(ISD::BSWAP, MVT::i64 , Expand); 274 if (Subtarget.isISA3_0()) { 275 setOperationAction(ISD::CTTZ , MVT::i32 , Legal); 276 setOperationAction(ISD::CTTZ , MVT::i64 , Legal); 277 } else { 278 setOperationAction(ISD::CTTZ , MVT::i32 , Expand); 279 setOperationAction(ISD::CTTZ , MVT::i64 , Expand); 280 } 281 282 if (Subtarget.hasPOPCNTD() == PPCSubtarget::POPCNTD_Fast) { 283 setOperationAction(ISD::CTPOP, MVT::i32 , Legal); 284 setOperationAction(ISD::CTPOP, MVT::i64 , Legal); 285 } else { 286 setOperationAction(ISD::CTPOP, MVT::i32 , Expand); 287 setOperationAction(ISD::CTPOP, MVT::i64 , Expand); 288 } 289 290 // PowerPC does not have ROTR 291 setOperationAction(ISD::ROTR, MVT::i32 , Expand); 292 setOperationAction(ISD::ROTR, MVT::i64 , Expand); 293 294 if (!Subtarget.useCRBits()) { 295 // PowerPC does not have Select 296 setOperationAction(ISD::SELECT, MVT::i32, Expand); 297 setOperationAction(ISD::SELECT, MVT::i64, Expand); 298 setOperationAction(ISD::SELECT, MVT::f32, Expand); 299 setOperationAction(ISD::SELECT, MVT::f64, Expand); 300 } 301 302 // PowerPC wants to turn select_cc of FP into fsel when possible. 303 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 304 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 305 306 // PowerPC wants to optimize integer setcc a bit 307 if (!Subtarget.useCRBits()) 308 setOperationAction(ISD::SETCC, MVT::i32, Custom); 309 310 // PowerPC does not have BRCOND which requires SetCC 311 if (!Subtarget.useCRBits()) 312 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 313 314 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 315 316 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores. 317 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 318 319 // PowerPC does not have [U|S]INT_TO_FP 320 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand); 321 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); 322 323 if (Subtarget.hasDirectMove() && isPPC64) { 324 setOperationAction(ISD::BITCAST, MVT::f32, Legal); 325 setOperationAction(ISD::BITCAST, MVT::i32, Legal); 326 setOperationAction(ISD::BITCAST, MVT::i64, Legal); 327 setOperationAction(ISD::BITCAST, MVT::f64, Legal); 328 } else { 329 setOperationAction(ISD::BITCAST, MVT::f32, Expand); 330 setOperationAction(ISD::BITCAST, MVT::i32, Expand); 331 setOperationAction(ISD::BITCAST, MVT::i64, Expand); 332 setOperationAction(ISD::BITCAST, MVT::f64, Expand); 333 } 334 335 // We cannot sextinreg(i1). Expand to shifts. 336 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 337 338 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support 339 // SjLj exception handling but a light-weight setjmp/longjmp replacement to 340 // support continuation, user-level threading, and etc.. As a result, no 341 // other SjLj exception interfaces are implemented and please don't build 342 // your own exception handling based on them. 343 // LLVM/Clang supports zero-cost DWARF exception handling. 344 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); 345 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); 346 347 // We want to legalize GlobalAddress and ConstantPool nodes into the 348 // appropriate instructions to materialize the address. 349 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 350 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 351 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 352 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 353 setOperationAction(ISD::JumpTable, MVT::i32, Custom); 354 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); 355 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); 356 setOperationAction(ISD::BlockAddress, MVT::i64, Custom); 357 setOperationAction(ISD::ConstantPool, MVT::i64, Custom); 358 setOperationAction(ISD::JumpTable, MVT::i64, Custom); 359 360 // TRAP is legal. 361 setOperationAction(ISD::TRAP, MVT::Other, Legal); 362 363 // TRAMPOLINE is custom lowered. 364 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom); 365 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom); 366 367 // VASTART needs to be custom lowered to use the VarArgsFrameIndex 368 setOperationAction(ISD::VASTART , MVT::Other, Custom); 369 370 if (Subtarget.isSVR4ABI()) { 371 if (isPPC64) { 372 // VAARG always uses double-word chunks, so promote anything smaller. 373 setOperationAction(ISD::VAARG, MVT::i1, Promote); 374 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64); 375 setOperationAction(ISD::VAARG, MVT::i8, Promote); 376 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64); 377 setOperationAction(ISD::VAARG, MVT::i16, Promote); 378 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64); 379 setOperationAction(ISD::VAARG, MVT::i32, Promote); 380 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64); 381 setOperationAction(ISD::VAARG, MVT::Other, Expand); 382 } else { 383 // VAARG is custom lowered with the 32-bit SVR4 ABI. 384 setOperationAction(ISD::VAARG, MVT::Other, Custom); 385 setOperationAction(ISD::VAARG, MVT::i64, Custom); 386 } 387 } else 388 setOperationAction(ISD::VAARG, MVT::Other, Expand); 389 390 if (Subtarget.isSVR4ABI() && !isPPC64) 391 // VACOPY is custom lowered with the 32-bit SVR4 ABI. 392 setOperationAction(ISD::VACOPY , MVT::Other, Custom); 393 else 394 setOperationAction(ISD::VACOPY , MVT::Other, Expand); 395 396 // Use the default implementation. 397 setOperationAction(ISD::VAEND , MVT::Other, Expand); 398 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand); 399 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom); 400 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom); 401 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom); 402 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i32, Custom); 403 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i64, Custom); 404 setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom); 405 setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom); 406 407 // We want to custom lower some of our intrinsics. 408 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 409 410 // To handle counter-based loop conditions. 411 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom); 412 413 setOperationAction(ISD::INTRINSIC_VOID, MVT::i8, Custom); 414 setOperationAction(ISD::INTRINSIC_VOID, MVT::i16, Custom); 415 setOperationAction(ISD::INTRINSIC_VOID, MVT::i32, Custom); 416 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); 417 418 // Comparisons that require checking two conditions. 419 setCondCodeAction(ISD::SETULT, MVT::f32, Expand); 420 setCondCodeAction(ISD::SETULT, MVT::f64, Expand); 421 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand); 422 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand); 423 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand); 424 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand); 425 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand); 426 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand); 427 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand); 428 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand); 429 setCondCodeAction(ISD::SETONE, MVT::f32, Expand); 430 setCondCodeAction(ISD::SETONE, MVT::f64, Expand); 431 432 if (Subtarget.has64BitSupport()) { 433 // They also have instructions for converting between i64 and fp. 434 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 435 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand); 436 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); 437 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); 438 // This is just the low 32 bits of a (signed) fp->i64 conversion. 439 // We cannot do this with Promote because i64 is not a legal type. 440 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 441 442 if (Subtarget.hasLFIWAX() || Subtarget.isPPC64()) 443 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 444 } else { 445 // PowerPC does not have FP_TO_UINT on 32-bit implementations. 446 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); 447 } 448 449 // With the instructions enabled under FPCVT, we can do everything. 450 if (Subtarget.hasFPCVT()) { 451 if (Subtarget.has64BitSupport()) { 452 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); 453 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom); 454 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); 455 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom); 456 } 457 458 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 459 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 460 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 461 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 462 } 463 464 if (Subtarget.use64BitRegs()) { 465 // 64-bit PowerPC implementations can support i64 types directly 466 addRegisterClass(MVT::i64, &PPC::G8RCRegClass); 467 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or 468 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand); 469 // 64-bit PowerPC wants to expand i128 shifts itself. 470 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom); 471 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom); 472 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom); 473 } else { 474 // 32-bit PowerPC wants to expand i64 shifts itself. 475 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 476 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 477 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 478 } 479 480 if (Subtarget.hasAltivec()) { 481 // First set operation action for all vector types to expand. Then we 482 // will selectively turn on ones that can be effectively codegen'd. 483 for (MVT VT : MVT::vector_valuetypes()) { 484 // add/sub are legal for all supported vector VT's. 485 setOperationAction(ISD::ADD, VT, Legal); 486 setOperationAction(ISD::SUB, VT, Legal); 487 488 // Vector instructions introduced in P8 489 if (Subtarget.hasP8Altivec() && (VT.SimpleTy != MVT::v1i128)) { 490 setOperationAction(ISD::CTPOP, VT, Legal); 491 setOperationAction(ISD::CTLZ, VT, Legal); 492 } 493 else { 494 setOperationAction(ISD::CTPOP, VT, Expand); 495 setOperationAction(ISD::CTLZ, VT, Expand); 496 } 497 498 // Vector instructions introduced in P9 499 if (Subtarget.hasP9Altivec() && (VT.SimpleTy != MVT::v1i128)) 500 setOperationAction(ISD::CTTZ, VT, Legal); 501 else 502 setOperationAction(ISD::CTTZ, VT, Expand); 503 504 // We promote all shuffles to v16i8. 505 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote); 506 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8); 507 508 // We promote all non-typed operations to v4i32. 509 setOperationAction(ISD::AND , VT, Promote); 510 AddPromotedToType (ISD::AND , VT, MVT::v4i32); 511 setOperationAction(ISD::OR , VT, Promote); 512 AddPromotedToType (ISD::OR , VT, MVT::v4i32); 513 setOperationAction(ISD::XOR , VT, Promote); 514 AddPromotedToType (ISD::XOR , VT, MVT::v4i32); 515 setOperationAction(ISD::LOAD , VT, Promote); 516 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32); 517 setOperationAction(ISD::SELECT, VT, Promote); 518 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32); 519 setOperationAction(ISD::SELECT_CC, VT, Promote); 520 AddPromotedToType (ISD::SELECT_CC, VT, MVT::v4i32); 521 setOperationAction(ISD::STORE, VT, Promote); 522 AddPromotedToType (ISD::STORE, VT, MVT::v4i32); 523 524 // No other operations are legal. 525 setOperationAction(ISD::MUL , VT, Expand); 526 setOperationAction(ISD::SDIV, VT, Expand); 527 setOperationAction(ISD::SREM, VT, Expand); 528 setOperationAction(ISD::UDIV, VT, Expand); 529 setOperationAction(ISD::UREM, VT, Expand); 530 setOperationAction(ISD::FDIV, VT, Expand); 531 setOperationAction(ISD::FREM, VT, Expand); 532 setOperationAction(ISD::FNEG, VT, Expand); 533 setOperationAction(ISD::FSQRT, VT, Expand); 534 setOperationAction(ISD::FLOG, VT, Expand); 535 setOperationAction(ISD::FLOG10, VT, Expand); 536 setOperationAction(ISD::FLOG2, VT, Expand); 537 setOperationAction(ISD::FEXP, VT, Expand); 538 setOperationAction(ISD::FEXP2, VT, Expand); 539 setOperationAction(ISD::FSIN, VT, Expand); 540 setOperationAction(ISD::FCOS, VT, Expand); 541 setOperationAction(ISD::FABS, VT, Expand); 542 setOperationAction(ISD::FPOWI, VT, Expand); 543 setOperationAction(ISD::FFLOOR, VT, Expand); 544 setOperationAction(ISD::FCEIL, VT, Expand); 545 setOperationAction(ISD::FTRUNC, VT, Expand); 546 setOperationAction(ISD::FRINT, VT, Expand); 547 setOperationAction(ISD::FNEARBYINT, VT, Expand); 548 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand); 549 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand); 550 setOperationAction(ISD::BUILD_VECTOR, VT, Expand); 551 setOperationAction(ISD::MULHU, VT, Expand); 552 setOperationAction(ISD::MULHS, VT, Expand); 553 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 554 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 555 setOperationAction(ISD::UDIVREM, VT, Expand); 556 setOperationAction(ISD::SDIVREM, VT, Expand); 557 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand); 558 setOperationAction(ISD::FPOW, VT, Expand); 559 setOperationAction(ISD::BSWAP, VT, Expand); 560 setOperationAction(ISD::VSELECT, VT, Expand); 561 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); 562 setOperationAction(ISD::ROTL, VT, Expand); 563 setOperationAction(ISD::ROTR, VT, Expand); 564 565 for (MVT InnerVT : MVT::vector_valuetypes()) { 566 setTruncStoreAction(VT, InnerVT, Expand); 567 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand); 568 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand); 569 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand); 570 } 571 } 572 573 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle 574 // with merges, splats, etc. 575 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom); 576 577 setOperationAction(ISD::AND , MVT::v4i32, Legal); 578 setOperationAction(ISD::OR , MVT::v4i32, Legal); 579 setOperationAction(ISD::XOR , MVT::v4i32, Legal); 580 setOperationAction(ISD::LOAD , MVT::v4i32, Legal); 581 setOperationAction(ISD::SELECT, MVT::v4i32, 582 Subtarget.useCRBits() ? Legal : Expand); 583 setOperationAction(ISD::STORE , MVT::v4i32, Legal); 584 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal); 585 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal); 586 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal); 587 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal); 588 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal); 589 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal); 590 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal); 591 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal); 592 593 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass); 594 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass); 595 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass); 596 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass); 597 598 setOperationAction(ISD::MUL, MVT::v4f32, Legal); 599 setOperationAction(ISD::FMA, MVT::v4f32, Legal); 600 601 if (TM.Options.UnsafeFPMath || Subtarget.hasVSX()) { 602 setOperationAction(ISD::FDIV, MVT::v4f32, Legal); 603 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); 604 } 605 606 if (Subtarget.hasP8Altivec()) 607 setOperationAction(ISD::MUL, MVT::v4i32, Legal); 608 else 609 setOperationAction(ISD::MUL, MVT::v4i32, Custom); 610 611 setOperationAction(ISD::MUL, MVT::v8i16, Custom); 612 setOperationAction(ISD::MUL, MVT::v16i8, Custom); 613 614 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom); 615 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom); 616 617 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom); 618 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom); 619 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom); 620 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); 621 622 // Altivec does not contain unordered floating-point compare instructions 623 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand); 624 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand); 625 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand); 626 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand); 627 628 if (Subtarget.hasVSX()) { 629 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal); 630 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal); 631 if (Subtarget.hasP8Vector()) { 632 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal); 633 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal); 634 } 635 if (Subtarget.hasDirectMove() && isPPC64) { 636 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Legal); 637 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Legal); 638 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Legal); 639 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Legal); 640 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Legal); 641 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Legal); 642 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal); 643 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal); 644 } 645 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal); 646 647 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal); 648 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal); 649 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal); 650 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal); 651 setOperationAction(ISD::FROUND, MVT::v2f64, Legal); 652 653 setOperationAction(ISD::FROUND, MVT::v4f32, Legal); 654 655 setOperationAction(ISD::MUL, MVT::v2f64, Legal); 656 setOperationAction(ISD::FMA, MVT::v2f64, Legal); 657 658 setOperationAction(ISD::FDIV, MVT::v2f64, Legal); 659 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); 660 661 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal); 662 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal); 663 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal); 664 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal); 665 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal); 666 667 // Share the Altivec comparison restrictions. 668 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand); 669 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand); 670 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand); 671 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand); 672 673 setOperationAction(ISD::LOAD, MVT::v2f64, Legal); 674 setOperationAction(ISD::STORE, MVT::v2f64, Legal); 675 676 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal); 677 678 if (Subtarget.hasP8Vector()) 679 addRegisterClass(MVT::f32, &PPC::VSSRCRegClass); 680 681 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass); 682 683 addRegisterClass(MVT::v4i32, &PPC::VSRCRegClass); 684 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass); 685 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass); 686 687 if (Subtarget.hasP8Altivec()) { 688 setOperationAction(ISD::SHL, MVT::v2i64, Legal); 689 setOperationAction(ISD::SRA, MVT::v2i64, Legal); 690 setOperationAction(ISD::SRL, MVT::v2i64, Legal); 691 692 // 128 bit shifts can be accomplished via 3 instructions for SHL and 693 // SRL, but not for SRA because of the instructions available: 694 // VS{RL} and VS{RL}O. However due to direct move costs, it's not worth 695 // doing 696 setOperationAction(ISD::SHL, MVT::v1i128, Expand); 697 setOperationAction(ISD::SRL, MVT::v1i128, Expand); 698 setOperationAction(ISD::SRA, MVT::v1i128, Expand); 699 700 setOperationAction(ISD::SETCC, MVT::v2i64, Legal); 701 } 702 else { 703 setOperationAction(ISD::SHL, MVT::v2i64, Expand); 704 setOperationAction(ISD::SRA, MVT::v2i64, Expand); 705 setOperationAction(ISD::SRL, MVT::v2i64, Expand); 706 707 setOperationAction(ISD::SETCC, MVT::v2i64, Custom); 708 709 // VSX v2i64 only supports non-arithmetic operations. 710 setOperationAction(ISD::ADD, MVT::v2i64, Expand); 711 setOperationAction(ISD::SUB, MVT::v2i64, Expand); 712 } 713 714 setOperationAction(ISD::LOAD, MVT::v2i64, Promote); 715 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64); 716 setOperationAction(ISD::STORE, MVT::v2i64, Promote); 717 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64); 718 719 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal); 720 721 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal); 722 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal); 723 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal); 724 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal); 725 726 // Vector operation legalization checks the result type of 727 // SIGN_EXTEND_INREG, overall legalization checks the inner type. 728 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal); 729 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal); 730 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom); 731 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom); 732 733 setOperationAction(ISD::FNEG, MVT::v4f32, Legal); 734 setOperationAction(ISD::FNEG, MVT::v2f64, Legal); 735 setOperationAction(ISD::FABS, MVT::v4f32, Legal); 736 setOperationAction(ISD::FABS, MVT::v2f64, Legal); 737 738 if (Subtarget.hasDirectMove()) 739 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom); 740 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom); 741 742 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass); 743 } 744 745 if (Subtarget.hasP8Altivec()) { 746 addRegisterClass(MVT::v2i64, &PPC::VRRCRegClass); 747 addRegisterClass(MVT::v1i128, &PPC::VRRCRegClass); 748 } 749 750 if (Subtarget.hasP9Vector()) { 751 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); 752 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); 753 754 // 128 bit shifts can be accomplished via 3 instructions for SHL and 755 // SRL, but not for SRA because of the instructions available: 756 // VS{RL} and VS{RL}O. 757 setOperationAction(ISD::SHL, MVT::v1i128, Legal); 758 setOperationAction(ISD::SRL, MVT::v1i128, Legal); 759 setOperationAction(ISD::SRA, MVT::v1i128, Expand); 760 } 761 } 762 763 if (Subtarget.hasQPX()) { 764 setOperationAction(ISD::FADD, MVT::v4f64, Legal); 765 setOperationAction(ISD::FSUB, MVT::v4f64, Legal); 766 setOperationAction(ISD::FMUL, MVT::v4f64, Legal); 767 setOperationAction(ISD::FREM, MVT::v4f64, Expand); 768 769 setOperationAction(ISD::FCOPYSIGN, MVT::v4f64, Legal); 770 setOperationAction(ISD::FGETSIGN, MVT::v4f64, Expand); 771 772 setOperationAction(ISD::LOAD , MVT::v4f64, Custom); 773 setOperationAction(ISD::STORE , MVT::v4f64, Custom); 774 775 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Custom); 776 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Custom); 777 778 if (!Subtarget.useCRBits()) 779 setOperationAction(ISD::SELECT, MVT::v4f64, Expand); 780 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal); 781 782 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f64, Legal); 783 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f64, Expand); 784 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f64, Expand); 785 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f64, Expand); 786 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f64, Custom); 787 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f64, Legal); 788 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom); 789 790 setOperationAction(ISD::FP_TO_SINT , MVT::v4f64, Legal); 791 setOperationAction(ISD::FP_TO_UINT , MVT::v4f64, Expand); 792 793 setOperationAction(ISD::FP_ROUND , MVT::v4f32, Legal); 794 setOperationAction(ISD::FP_ROUND_INREG , MVT::v4f32, Expand); 795 setOperationAction(ISD::FP_EXTEND, MVT::v4f64, Legal); 796 797 setOperationAction(ISD::FNEG , MVT::v4f64, Legal); 798 setOperationAction(ISD::FABS , MVT::v4f64, Legal); 799 setOperationAction(ISD::FSIN , MVT::v4f64, Expand); 800 setOperationAction(ISD::FCOS , MVT::v4f64, Expand); 801 setOperationAction(ISD::FPOWI , MVT::v4f64, Expand); 802 setOperationAction(ISD::FPOW , MVT::v4f64, Expand); 803 setOperationAction(ISD::FLOG , MVT::v4f64, Expand); 804 setOperationAction(ISD::FLOG2 , MVT::v4f64, Expand); 805 setOperationAction(ISD::FLOG10 , MVT::v4f64, Expand); 806 setOperationAction(ISD::FEXP , MVT::v4f64, Expand); 807 setOperationAction(ISD::FEXP2 , MVT::v4f64, Expand); 808 809 setOperationAction(ISD::FMINNUM, MVT::v4f64, Legal); 810 setOperationAction(ISD::FMAXNUM, MVT::v4f64, Legal); 811 812 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f64, Legal); 813 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f64, Legal); 814 815 addRegisterClass(MVT::v4f64, &PPC::QFRCRegClass); 816 817 setOperationAction(ISD::FADD, MVT::v4f32, Legal); 818 setOperationAction(ISD::FSUB, MVT::v4f32, Legal); 819 setOperationAction(ISD::FMUL, MVT::v4f32, Legal); 820 setOperationAction(ISD::FREM, MVT::v4f32, Expand); 821 822 setOperationAction(ISD::FCOPYSIGN, MVT::v4f32, Legal); 823 setOperationAction(ISD::FGETSIGN, MVT::v4f32, Expand); 824 825 setOperationAction(ISD::LOAD , MVT::v4f32, Custom); 826 setOperationAction(ISD::STORE , MVT::v4f32, Custom); 827 828 if (!Subtarget.useCRBits()) 829 setOperationAction(ISD::SELECT, MVT::v4f32, Expand); 830 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal); 831 832 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f32, Legal); 833 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f32, Expand); 834 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f32, Expand); 835 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f32, Expand); 836 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f32, Custom); 837 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal); 838 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); 839 840 setOperationAction(ISD::FP_TO_SINT , MVT::v4f32, Legal); 841 setOperationAction(ISD::FP_TO_UINT , MVT::v4f32, Expand); 842 843 setOperationAction(ISD::FNEG , MVT::v4f32, Legal); 844 setOperationAction(ISD::FABS , MVT::v4f32, Legal); 845 setOperationAction(ISD::FSIN , MVT::v4f32, Expand); 846 setOperationAction(ISD::FCOS , MVT::v4f32, Expand); 847 setOperationAction(ISD::FPOWI , MVT::v4f32, Expand); 848 setOperationAction(ISD::FPOW , MVT::v4f32, Expand); 849 setOperationAction(ISD::FLOG , MVT::v4f32, Expand); 850 setOperationAction(ISD::FLOG2 , MVT::v4f32, Expand); 851 setOperationAction(ISD::FLOG10 , MVT::v4f32, Expand); 852 setOperationAction(ISD::FEXP , MVT::v4f32, Expand); 853 setOperationAction(ISD::FEXP2 , MVT::v4f32, Expand); 854 855 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal); 856 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal); 857 858 setIndexedLoadAction(ISD::PRE_INC, MVT::v4f32, Legal); 859 setIndexedStoreAction(ISD::PRE_INC, MVT::v4f32, Legal); 860 861 addRegisterClass(MVT::v4f32, &PPC::QSRCRegClass); 862 863 setOperationAction(ISD::AND , MVT::v4i1, Legal); 864 setOperationAction(ISD::OR , MVT::v4i1, Legal); 865 setOperationAction(ISD::XOR , MVT::v4i1, Legal); 866 867 if (!Subtarget.useCRBits()) 868 setOperationAction(ISD::SELECT, MVT::v4i1, Expand); 869 setOperationAction(ISD::VSELECT, MVT::v4i1, Legal); 870 871 setOperationAction(ISD::LOAD , MVT::v4i1, Custom); 872 setOperationAction(ISD::STORE , MVT::v4i1, Custom); 873 874 setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4i1, Custom); 875 setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4i1, Expand); 876 setOperationAction(ISD::CONCAT_VECTORS , MVT::v4i1, Expand); 877 setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4i1, Expand); 878 setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4i1, Custom); 879 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i1, Expand); 880 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i1, Custom); 881 882 setOperationAction(ISD::SINT_TO_FP, MVT::v4i1, Custom); 883 setOperationAction(ISD::UINT_TO_FP, MVT::v4i1, Custom); 884 885 addRegisterClass(MVT::v4i1, &PPC::QBRCRegClass); 886 887 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal); 888 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal); 889 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal); 890 setOperationAction(ISD::FROUND, MVT::v4f64, Legal); 891 892 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal); 893 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal); 894 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal); 895 setOperationAction(ISD::FROUND, MVT::v4f32, Legal); 896 897 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Expand); 898 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand); 899 900 // These need to set FE_INEXACT, and so cannot be vectorized here. 901 setOperationAction(ISD::FRINT, MVT::v4f64, Expand); 902 setOperationAction(ISD::FRINT, MVT::v4f32, Expand); 903 904 if (TM.Options.UnsafeFPMath) { 905 setOperationAction(ISD::FDIV, MVT::v4f64, Legal); 906 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal); 907 908 setOperationAction(ISD::FDIV, MVT::v4f32, Legal); 909 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); 910 } else { 911 setOperationAction(ISD::FDIV, MVT::v4f64, Expand); 912 setOperationAction(ISD::FSQRT, MVT::v4f64, Expand); 913 914 setOperationAction(ISD::FDIV, MVT::v4f32, Expand); 915 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); 916 } 917 } 918 919 if (Subtarget.has64BitSupport()) 920 setOperationAction(ISD::PREFETCH, MVT::Other, Legal); 921 922 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, isPPC64 ? Legal : Custom); 923 924 if (!isPPC64) { 925 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand); 926 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand); 927 } 928 929 setBooleanContents(ZeroOrOneBooleanContent); 930 931 if (Subtarget.hasAltivec()) { 932 // Altivec instructions set fields to all zeros or all ones. 933 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 934 } 935 936 if (!isPPC64) { 937 // These libcalls are not available in 32-bit. 938 setLibcallName(RTLIB::SHL_I128, nullptr); 939 setLibcallName(RTLIB::SRL_I128, nullptr); 940 setLibcallName(RTLIB::SRA_I128, nullptr); 941 } 942 943 setStackPointerRegisterToSaveRestore(isPPC64 ? PPC::X1 : PPC::R1); 944 945 // We have target-specific dag combine patterns for the following nodes: 946 setTargetDAGCombine(ISD::SHL); 947 setTargetDAGCombine(ISD::SRA); 948 setTargetDAGCombine(ISD::SRL); 949 setTargetDAGCombine(ISD::SINT_TO_FP); 950 setTargetDAGCombine(ISD::BUILD_VECTOR); 951 if (Subtarget.hasFPCVT()) 952 setTargetDAGCombine(ISD::UINT_TO_FP); 953 setTargetDAGCombine(ISD::LOAD); 954 setTargetDAGCombine(ISD::STORE); 955 setTargetDAGCombine(ISD::BR_CC); 956 if (Subtarget.useCRBits()) 957 setTargetDAGCombine(ISD::BRCOND); 958 setTargetDAGCombine(ISD::BSWAP); 959 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 960 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 961 setTargetDAGCombine(ISD::INTRINSIC_VOID); 962 963 setTargetDAGCombine(ISD::SIGN_EXTEND); 964 setTargetDAGCombine(ISD::ZERO_EXTEND); 965 setTargetDAGCombine(ISD::ANY_EXTEND); 966 967 if (Subtarget.useCRBits()) { 968 setTargetDAGCombine(ISD::TRUNCATE); 969 setTargetDAGCombine(ISD::SETCC); 970 setTargetDAGCombine(ISD::SELECT_CC); 971 } 972 973 // Use reciprocal estimates. 974 if (TM.Options.UnsafeFPMath) { 975 setTargetDAGCombine(ISD::FDIV); 976 setTargetDAGCombine(ISD::FSQRT); 977 } 978 979 // Darwin long double math library functions have $LDBL128 appended. 980 if (Subtarget.isDarwin()) { 981 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128"); 982 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128"); 983 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128"); 984 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128"); 985 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128"); 986 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128"); 987 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128"); 988 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128"); 989 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128"); 990 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128"); 991 } 992 993 // With 32 condition bits, we don't need to sink (and duplicate) compares 994 // aggressively in CodeGenPrep. 995 if (Subtarget.useCRBits()) { 996 setHasMultipleConditionRegisters(); 997 setJumpIsExpensive(); 998 } 999 1000 setMinFunctionAlignment(2); 1001 if (Subtarget.isDarwin()) 1002 setPrefFunctionAlignment(4); 1003 1004 switch (Subtarget.getDarwinDirective()) { 1005 default: break; 1006 case PPC::DIR_970: 1007 case PPC::DIR_A2: 1008 case PPC::DIR_E500mc: 1009 case PPC::DIR_E5500: 1010 case PPC::DIR_PWR4: 1011 case PPC::DIR_PWR5: 1012 case PPC::DIR_PWR5X: 1013 case PPC::DIR_PWR6: 1014 case PPC::DIR_PWR6X: 1015 case PPC::DIR_PWR7: 1016 case PPC::DIR_PWR8: 1017 case PPC::DIR_PWR9: 1018 setPrefFunctionAlignment(4); 1019 setPrefLoopAlignment(4); 1020 break; 1021 } 1022 1023 if (Subtarget.enableMachineScheduler()) 1024 setSchedulingPreference(Sched::Source); 1025 else 1026 setSchedulingPreference(Sched::Hybrid); 1027 1028 computeRegisterProperties(STI.getRegisterInfo()); 1029 1030 // The Freescale cores do better with aggressive inlining of memcpy and 1031 // friends. GCC uses same threshold of 128 bytes (= 32 word stores). 1032 if (Subtarget.getDarwinDirective() == PPC::DIR_E500mc || 1033 Subtarget.getDarwinDirective() == PPC::DIR_E5500) { 1034 MaxStoresPerMemset = 32; 1035 MaxStoresPerMemsetOptSize = 16; 1036 MaxStoresPerMemcpy = 32; 1037 MaxStoresPerMemcpyOptSize = 8; 1038 MaxStoresPerMemmove = 32; 1039 MaxStoresPerMemmoveOptSize = 8; 1040 } else if (Subtarget.getDarwinDirective() == PPC::DIR_A2) { 1041 // The A2 also benefits from (very) aggressive inlining of memcpy and 1042 // friends. The overhead of a the function call, even when warm, can be 1043 // over one hundred cycles. 1044 MaxStoresPerMemset = 128; 1045 MaxStoresPerMemcpy = 128; 1046 MaxStoresPerMemmove = 128; 1047 } 1048 } 1049 1050 /// getMaxByValAlign - Helper for getByValTypeAlignment to determine 1051 /// the desired ByVal argument alignment. 1052 static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign, 1053 unsigned MaxMaxAlign) { 1054 if (MaxAlign == MaxMaxAlign) 1055 return; 1056 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) { 1057 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256) 1058 MaxAlign = 32; 1059 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16) 1060 MaxAlign = 16; 1061 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 1062 unsigned EltAlign = 0; 1063 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign); 1064 if (EltAlign > MaxAlign) 1065 MaxAlign = EltAlign; 1066 } else if (StructType *STy = dyn_cast<StructType>(Ty)) { 1067 for (auto *EltTy : STy->elements()) { 1068 unsigned EltAlign = 0; 1069 getMaxByValAlign(EltTy, EltAlign, MaxMaxAlign); 1070 if (EltAlign > MaxAlign) 1071 MaxAlign = EltAlign; 1072 if (MaxAlign == MaxMaxAlign) 1073 break; 1074 } 1075 } 1076 } 1077 1078 /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate 1079 /// function arguments in the caller parameter area. 1080 unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty, 1081 const DataLayout &DL) const { 1082 // Darwin passes everything on 4 byte boundary. 1083 if (Subtarget.isDarwin()) 1084 return 4; 1085 1086 // 16byte and wider vectors are passed on 16byte boundary. 1087 // The rest is 8 on PPC64 and 4 on PPC32 boundary. 1088 unsigned Align = Subtarget.isPPC64() ? 8 : 4; 1089 if (Subtarget.hasAltivec() || Subtarget.hasQPX()) 1090 getMaxByValAlign(Ty, Align, Subtarget.hasQPX() ? 32 : 16); 1091 return Align; 1092 } 1093 1094 bool PPCTargetLowering::useSoftFloat() const { 1095 return Subtarget.useSoftFloat(); 1096 } 1097 1098 const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const { 1099 switch ((PPCISD::NodeType)Opcode) { 1100 case PPCISD::FIRST_NUMBER: break; 1101 case PPCISD::FSEL: return "PPCISD::FSEL"; 1102 case PPCISD::FCFID: return "PPCISD::FCFID"; 1103 case PPCISD::FCFIDU: return "PPCISD::FCFIDU"; 1104 case PPCISD::FCFIDS: return "PPCISD::FCFIDS"; 1105 case PPCISD::FCFIDUS: return "PPCISD::FCFIDUS"; 1106 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ"; 1107 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ"; 1108 case PPCISD::FCTIDUZ: return "PPCISD::FCTIDUZ"; 1109 case PPCISD::FCTIWUZ: return "PPCISD::FCTIWUZ"; 1110 case PPCISD::FRE: return "PPCISD::FRE"; 1111 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE"; 1112 case PPCISD::STFIWX: return "PPCISD::STFIWX"; 1113 case PPCISD::VMADDFP: return "PPCISD::VMADDFP"; 1114 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP"; 1115 case PPCISD::VPERM: return "PPCISD::VPERM"; 1116 case PPCISD::XXSPLT: return "PPCISD::XXSPLT"; 1117 case PPCISD::XXINSERT: return "PPCISD::XXINSERT"; 1118 case PPCISD::VECSHL: return "PPCISD::VECSHL"; 1119 case PPCISD::CMPB: return "PPCISD::CMPB"; 1120 case PPCISD::Hi: return "PPCISD::Hi"; 1121 case PPCISD::Lo: return "PPCISD::Lo"; 1122 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY"; 1123 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC"; 1124 case PPCISD::DYNAREAOFFSET: return "PPCISD::DYNAREAOFFSET"; 1125 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg"; 1126 case PPCISD::SRL: return "PPCISD::SRL"; 1127 case PPCISD::SRA: return "PPCISD::SRA"; 1128 case PPCISD::SHL: return "PPCISD::SHL"; 1129 case PPCISD::SRA_ADDZE: return "PPCISD::SRA_ADDZE"; 1130 case PPCISD::CALL: return "PPCISD::CALL"; 1131 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP"; 1132 case PPCISD::MTCTR: return "PPCISD::MTCTR"; 1133 case PPCISD::BCTRL: return "PPCISD::BCTRL"; 1134 case PPCISD::BCTRL_LOAD_TOC: return "PPCISD::BCTRL_LOAD_TOC"; 1135 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG"; 1136 case PPCISD::READ_TIME_BASE: return "PPCISD::READ_TIME_BASE"; 1137 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP"; 1138 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP"; 1139 case PPCISD::MFOCRF: return "PPCISD::MFOCRF"; 1140 case PPCISD::MFVSR: return "PPCISD::MFVSR"; 1141 case PPCISD::MTVSRA: return "PPCISD::MTVSRA"; 1142 case PPCISD::MTVSRZ: return "PPCISD::MTVSRZ"; 1143 case PPCISD::SINT_VEC_TO_FP: return "PPCISD::SINT_VEC_TO_FP"; 1144 case PPCISD::UINT_VEC_TO_FP: return "PPCISD::UINT_VEC_TO_FP"; 1145 case PPCISD::ANDIo_1_EQ_BIT: return "PPCISD::ANDIo_1_EQ_BIT"; 1146 case PPCISD::ANDIo_1_GT_BIT: return "PPCISD::ANDIo_1_GT_BIT"; 1147 case PPCISD::VCMP: return "PPCISD::VCMP"; 1148 case PPCISD::VCMPo: return "PPCISD::VCMPo"; 1149 case PPCISD::LBRX: return "PPCISD::LBRX"; 1150 case PPCISD::STBRX: return "PPCISD::STBRX"; 1151 case PPCISD::LFIWAX: return "PPCISD::LFIWAX"; 1152 case PPCISD::LFIWZX: return "PPCISD::LFIWZX"; 1153 case PPCISD::LXSIZX: return "PPCISD::LXSIZX"; 1154 case PPCISD::STXSIX: return "PPCISD::STXSIX"; 1155 case PPCISD::VEXTS: return "PPCISD::VEXTS"; 1156 case PPCISD::LXVD2X: return "PPCISD::LXVD2X"; 1157 case PPCISD::STXVD2X: return "PPCISD::STXVD2X"; 1158 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH"; 1159 case PPCISD::BDNZ: return "PPCISD::BDNZ"; 1160 case PPCISD::BDZ: return "PPCISD::BDZ"; 1161 case PPCISD::MFFS: return "PPCISD::MFFS"; 1162 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ"; 1163 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN"; 1164 case PPCISD::CR6SET: return "PPCISD::CR6SET"; 1165 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET"; 1166 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT"; 1167 case PPCISD::PPC32_PICGOT: return "PPCISD::PPC32_PICGOT"; 1168 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA"; 1169 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L"; 1170 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS"; 1171 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA"; 1172 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L"; 1173 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR"; 1174 case PPCISD::ADDI_TLSGD_L_ADDR: return "PPCISD::ADDI_TLSGD_L_ADDR"; 1175 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA"; 1176 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L"; 1177 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR"; 1178 case PPCISD::ADDI_TLSLD_L_ADDR: return "PPCISD::ADDI_TLSLD_L_ADDR"; 1179 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA"; 1180 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L"; 1181 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT"; 1182 case PPCISD::SC: return "PPCISD::SC"; 1183 case PPCISD::CLRBHRB: return "PPCISD::CLRBHRB"; 1184 case PPCISD::MFBHRBE: return "PPCISD::MFBHRBE"; 1185 case PPCISD::RFEBB: return "PPCISD::RFEBB"; 1186 case PPCISD::XXSWAPD: return "PPCISD::XXSWAPD"; 1187 case PPCISD::SWAP_NO_CHAIN: return "PPCISD::SWAP_NO_CHAIN"; 1188 case PPCISD::QVFPERM: return "PPCISD::QVFPERM"; 1189 case PPCISD::QVGPCI: return "PPCISD::QVGPCI"; 1190 case PPCISD::QVALIGNI: return "PPCISD::QVALIGNI"; 1191 case PPCISD::QVESPLATI: return "PPCISD::QVESPLATI"; 1192 case PPCISD::QBFLT: return "PPCISD::QBFLT"; 1193 case PPCISD::QVLFSb: return "PPCISD::QVLFSb"; 1194 } 1195 return nullptr; 1196 } 1197 1198 EVT PPCTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &C, 1199 EVT VT) const { 1200 if (!VT.isVector()) 1201 return Subtarget.useCRBits() ? MVT::i1 : MVT::i32; 1202 1203 if (Subtarget.hasQPX()) 1204 return EVT::getVectorVT(C, MVT::i1, VT.getVectorNumElements()); 1205 1206 return VT.changeVectorElementTypeToInteger(); 1207 } 1208 1209 bool PPCTargetLowering::enableAggressiveFMAFusion(EVT VT) const { 1210 assert(VT.isFloatingPoint() && "Non-floating-point FMA?"); 1211 return true; 1212 } 1213 1214 //===----------------------------------------------------------------------===// 1215 // Node matching predicates, for use by the tblgen matching code. 1216 //===----------------------------------------------------------------------===// 1217 1218 /// isFloatingPointZero - Return true if this is 0.0 or -0.0. 1219 static bool isFloatingPointZero(SDValue Op) { 1220 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) 1221 return CFP->getValueAPF().isZero(); 1222 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { 1223 // Maybe this has already been legalized into the constant pool? 1224 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1))) 1225 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 1226 return CFP->getValueAPF().isZero(); 1227 } 1228 return false; 1229 } 1230 1231 /// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return 1232 /// true if Op is undef or if it matches the specified value. 1233 static bool isConstantOrUndef(int Op, int Val) { 1234 return Op < 0 || Op == Val; 1235 } 1236 1237 /// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a 1238 /// VPKUHUM instruction. 1239 /// The ShuffleKind distinguishes between big-endian operations with 1240 /// two different inputs (0), either-endian operations with two identical 1241 /// inputs (1), and little-endian operations with two different inputs (2). 1242 /// For the latter, the input operands are swapped (see PPCInstrAltivec.td). 1243 bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind, 1244 SelectionDAG &DAG) { 1245 bool IsLE = DAG.getDataLayout().isLittleEndian(); 1246 if (ShuffleKind == 0) { 1247 if (IsLE) 1248 return false; 1249 for (unsigned i = 0; i != 16; ++i) 1250 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1)) 1251 return false; 1252 } else if (ShuffleKind == 2) { 1253 if (!IsLE) 1254 return false; 1255 for (unsigned i = 0; i != 16; ++i) 1256 if (!isConstantOrUndef(N->getMaskElt(i), i*2)) 1257 return false; 1258 } else if (ShuffleKind == 1) { 1259 unsigned j = IsLE ? 0 : 1; 1260 for (unsigned i = 0; i != 8; ++i) 1261 if (!isConstantOrUndef(N->getMaskElt(i), i*2+j) || 1262 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j)) 1263 return false; 1264 } 1265 return true; 1266 } 1267 1268 /// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a 1269 /// VPKUWUM instruction. 1270 /// The ShuffleKind distinguishes between big-endian operations with 1271 /// two different inputs (0), either-endian operations with two identical 1272 /// inputs (1), and little-endian operations with two different inputs (2). 1273 /// For the latter, the input operands are swapped (see PPCInstrAltivec.td). 1274 bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind, 1275 SelectionDAG &DAG) { 1276 bool IsLE = DAG.getDataLayout().isLittleEndian(); 1277 if (ShuffleKind == 0) { 1278 if (IsLE) 1279 return false; 1280 for (unsigned i = 0; i != 16; i += 2) 1281 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) || 1282 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3)) 1283 return false; 1284 } else if (ShuffleKind == 2) { 1285 if (!IsLE) 1286 return false; 1287 for (unsigned i = 0; i != 16; i += 2) 1288 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) || 1289 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1)) 1290 return false; 1291 } else if (ShuffleKind == 1) { 1292 unsigned j = IsLE ? 0 : 2; 1293 for (unsigned i = 0; i != 8; i += 2) 1294 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) || 1295 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) || 1296 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) || 1297 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1)) 1298 return false; 1299 } 1300 return true; 1301 } 1302 1303 /// isVPKUDUMShuffleMask - Return true if this is the shuffle mask for a 1304 /// VPKUDUM instruction, AND the VPKUDUM instruction exists for the 1305 /// current subtarget. 1306 /// 1307 /// The ShuffleKind distinguishes between big-endian operations with 1308 /// two different inputs (0), either-endian operations with two identical 1309 /// inputs (1), and little-endian operations with two different inputs (2). 1310 /// For the latter, the input operands are swapped (see PPCInstrAltivec.td). 1311 bool PPC::isVPKUDUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind, 1312 SelectionDAG &DAG) { 1313 const PPCSubtarget& Subtarget = 1314 static_cast<const PPCSubtarget&>(DAG.getSubtarget()); 1315 if (!Subtarget.hasP8Vector()) 1316 return false; 1317 1318 bool IsLE = DAG.getDataLayout().isLittleEndian(); 1319 if (ShuffleKind == 0) { 1320 if (IsLE) 1321 return false; 1322 for (unsigned i = 0; i != 16; i += 4) 1323 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+4) || 1324 !isConstantOrUndef(N->getMaskElt(i+1), i*2+5) || 1325 !isConstantOrUndef(N->getMaskElt(i+2), i*2+6) || 1326 !isConstantOrUndef(N->getMaskElt(i+3), i*2+7)) 1327 return false; 1328 } else if (ShuffleKind == 2) { 1329 if (!IsLE) 1330 return false; 1331 for (unsigned i = 0; i != 16; i += 4) 1332 if (!isConstantOrUndef(N->getMaskElt(i ), i*2) || 1333 !isConstantOrUndef(N->getMaskElt(i+1), i*2+1) || 1334 !isConstantOrUndef(N->getMaskElt(i+2), i*2+2) || 1335 !isConstantOrUndef(N->getMaskElt(i+3), i*2+3)) 1336 return false; 1337 } else if (ShuffleKind == 1) { 1338 unsigned j = IsLE ? 0 : 4; 1339 for (unsigned i = 0; i != 8; i += 4) 1340 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) || 1341 !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) || 1342 !isConstantOrUndef(N->getMaskElt(i+2), i*2+j+2) || 1343 !isConstantOrUndef(N->getMaskElt(i+3), i*2+j+3) || 1344 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) || 1345 !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1) || 1346 !isConstantOrUndef(N->getMaskElt(i+10), i*2+j+2) || 1347 !isConstantOrUndef(N->getMaskElt(i+11), i*2+j+3)) 1348 return false; 1349 } 1350 return true; 1351 } 1352 1353 /// isVMerge - Common function, used to match vmrg* shuffles. 1354 /// 1355 static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize, 1356 unsigned LHSStart, unsigned RHSStart) { 1357 if (N->getValueType(0) != MVT::v16i8) 1358 return false; 1359 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) && 1360 "Unsupported merge size!"); 1361 1362 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units 1363 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit 1364 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j), 1365 LHSStart+j+i*UnitSize) || 1366 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j), 1367 RHSStart+j+i*UnitSize)) 1368 return false; 1369 } 1370 return true; 1371 } 1372 1373 /// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for 1374 /// a VMRGL* instruction with the specified unit size (1,2 or 4 bytes). 1375 /// The ShuffleKind distinguishes between big-endian merges with two 1376 /// different inputs (0), either-endian merges with two identical inputs (1), 1377 /// and little-endian merges with two different inputs (2). For the latter, 1378 /// the input operands are swapped (see PPCInstrAltivec.td). 1379 bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize, 1380 unsigned ShuffleKind, SelectionDAG &DAG) { 1381 if (DAG.getDataLayout().isLittleEndian()) { 1382 if (ShuffleKind == 1) // unary 1383 return isVMerge(N, UnitSize, 0, 0); 1384 else if (ShuffleKind == 2) // swapped 1385 return isVMerge(N, UnitSize, 0, 16); 1386 else 1387 return false; 1388 } else { 1389 if (ShuffleKind == 1) // unary 1390 return isVMerge(N, UnitSize, 8, 8); 1391 else if (ShuffleKind == 0) // normal 1392 return isVMerge(N, UnitSize, 8, 24); 1393 else 1394 return false; 1395 } 1396 } 1397 1398 /// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for 1399 /// a VMRGH* instruction with the specified unit size (1,2 or 4 bytes). 1400 /// The ShuffleKind distinguishes between big-endian merges with two 1401 /// different inputs (0), either-endian merges with two identical inputs (1), 1402 /// and little-endian merges with two different inputs (2). For the latter, 1403 /// the input operands are swapped (see PPCInstrAltivec.td). 1404 bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize, 1405 unsigned ShuffleKind, SelectionDAG &DAG) { 1406 if (DAG.getDataLayout().isLittleEndian()) { 1407 if (ShuffleKind == 1) // unary 1408 return isVMerge(N, UnitSize, 8, 8); 1409 else if (ShuffleKind == 2) // swapped 1410 return isVMerge(N, UnitSize, 8, 24); 1411 else 1412 return false; 1413 } else { 1414 if (ShuffleKind == 1) // unary 1415 return isVMerge(N, UnitSize, 0, 0); 1416 else if (ShuffleKind == 0) // normal 1417 return isVMerge(N, UnitSize, 0, 16); 1418 else 1419 return false; 1420 } 1421 } 1422 1423 /** 1424 * \brief Common function used to match vmrgew and vmrgow shuffles 1425 * 1426 * The indexOffset determines whether to look for even or odd words in 1427 * the shuffle mask. This is based on the of the endianness of the target 1428 * machine. 1429 * - Little Endian: 1430 * - Use offset of 0 to check for odd elements 1431 * - Use offset of 4 to check for even elements 1432 * - Big Endian: 1433 * - Use offset of 0 to check for even elements 1434 * - Use offset of 4 to check for odd elements 1435 * A detailed description of the vector element ordering for little endian and 1436 * big endian can be found at 1437 * http://www.ibm.com/developerworks/library/l-ibm-xl-c-cpp-compiler/index.html 1438 * Targeting your applications - what little endian and big endian IBM XL C/C++ 1439 * compiler differences mean to you 1440 * 1441 * The mask to the shuffle vector instruction specifies the indices of the 1442 * elements from the two input vectors to place in the result. The elements are 1443 * numbered in array-access order, starting with the first vector. These vectors 1444 * are always of type v16i8, thus each vector will contain 16 elements of size 1445 * 8. More info on the shuffle vector can be found in the 1446 * http://llvm.org/docs/LangRef.html#shufflevector-instruction 1447 * Language Reference. 1448 * 1449 * The RHSStartValue indicates whether the same input vectors are used (unary) 1450 * or two different input vectors are used, based on the following: 1451 * - If the instruction uses the same vector for both inputs, the range of the 1452 * indices will be 0 to 15. In this case, the RHSStart value passed should 1453 * be 0. 1454 * - If the instruction has two different vectors then the range of the 1455 * indices will be 0 to 31. In this case, the RHSStart value passed should 1456 * be 16 (indices 0-15 specify elements in the first vector while indices 16 1457 * to 31 specify elements in the second vector). 1458 * 1459 * \param[in] N The shuffle vector SD Node to analyze 1460 * \param[in] IndexOffset Specifies whether to look for even or odd elements 1461 * \param[in] RHSStartValue Specifies the starting index for the righthand input 1462 * vector to the shuffle_vector instruction 1463 * \return true iff this shuffle vector represents an even or odd word merge 1464 */ 1465 static bool isVMerge(ShuffleVectorSDNode *N, unsigned IndexOffset, 1466 unsigned RHSStartValue) { 1467 if (N->getValueType(0) != MVT::v16i8) 1468 return false; 1469 1470 for (unsigned i = 0; i < 2; ++i) 1471 for (unsigned j = 0; j < 4; ++j) 1472 if (!isConstantOrUndef(N->getMaskElt(i*4+j), 1473 i*RHSStartValue+j+IndexOffset) || 1474 !isConstantOrUndef(N->getMaskElt(i*4+j+8), 1475 i*RHSStartValue+j+IndexOffset+8)) 1476 return false; 1477 return true; 1478 } 1479 1480 /** 1481 * \brief Determine if the specified shuffle mask is suitable for the vmrgew or 1482 * vmrgow instructions. 1483 * 1484 * \param[in] N The shuffle vector SD Node to analyze 1485 * \param[in] CheckEven Check for an even merge (true) or an odd merge (false) 1486 * \param[in] ShuffleKind Identify the type of merge: 1487 * - 0 = big-endian merge with two different inputs; 1488 * - 1 = either-endian merge with two identical inputs; 1489 * - 2 = little-endian merge with two different inputs (inputs are swapped for 1490 * little-endian merges). 1491 * \param[in] DAG The current SelectionDAG 1492 * \return true iff this shuffle mask 1493 */ 1494 bool PPC::isVMRGEOShuffleMask(ShuffleVectorSDNode *N, bool CheckEven, 1495 unsigned ShuffleKind, SelectionDAG &DAG) { 1496 if (DAG.getDataLayout().isLittleEndian()) { 1497 unsigned indexOffset = CheckEven ? 4 : 0; 1498 if (ShuffleKind == 1) // Unary 1499 return isVMerge(N, indexOffset, 0); 1500 else if (ShuffleKind == 2) // swapped 1501 return isVMerge(N, indexOffset, 16); 1502 else 1503 return false; 1504 } 1505 else { 1506 unsigned indexOffset = CheckEven ? 0 : 4; 1507 if (ShuffleKind == 1) // Unary 1508 return isVMerge(N, indexOffset, 0); 1509 else if (ShuffleKind == 0) // Normal 1510 return isVMerge(N, indexOffset, 16); 1511 else 1512 return false; 1513 } 1514 return false; 1515 } 1516 1517 /// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift 1518 /// amount, otherwise return -1. 1519 /// The ShuffleKind distinguishes between big-endian operations with two 1520 /// different inputs (0), either-endian operations with two identical inputs 1521 /// (1), and little-endian operations with two different inputs (2). For the 1522 /// latter, the input operands are swapped (see PPCInstrAltivec.td). 1523 int PPC::isVSLDOIShuffleMask(SDNode *N, unsigned ShuffleKind, 1524 SelectionDAG &DAG) { 1525 if (N->getValueType(0) != MVT::v16i8) 1526 return -1; 1527 1528 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); 1529 1530 // Find the first non-undef value in the shuffle mask. 1531 unsigned i; 1532 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i) 1533 /*search*/; 1534 1535 if (i == 16) return -1; // all undef. 1536 1537 // Otherwise, check to see if the rest of the elements are consecutively 1538 // numbered from this value. 1539 unsigned ShiftAmt = SVOp->getMaskElt(i); 1540 if (ShiftAmt < i) return -1; 1541 1542 ShiftAmt -= i; 1543 bool isLE = DAG.getDataLayout().isLittleEndian(); 1544 1545 if ((ShuffleKind == 0 && !isLE) || (ShuffleKind == 2 && isLE)) { 1546 // Check the rest of the elements to see if they are consecutive. 1547 for (++i; i != 16; ++i) 1548 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i)) 1549 return -1; 1550 } else if (ShuffleKind == 1) { 1551 // Check the rest of the elements to see if they are consecutive. 1552 for (++i; i != 16; ++i) 1553 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15)) 1554 return -1; 1555 } else 1556 return -1; 1557 1558 if (isLE) 1559 ShiftAmt = 16 - ShiftAmt; 1560 1561 return ShiftAmt; 1562 } 1563 1564 /// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand 1565 /// specifies a splat of a single element that is suitable for input to 1566 /// VSPLTB/VSPLTH/VSPLTW. 1567 bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) { 1568 assert(N->getValueType(0) == MVT::v16i8 && 1569 (EltSize == 1 || EltSize == 2 || EltSize == 4)); 1570 1571 // The consecutive indices need to specify an element, not part of two 1572 // different elements. So abandon ship early if this isn't the case. 1573 if (N->getMaskElt(0) % EltSize != 0) 1574 return false; 1575 1576 // This is a splat operation if each element of the permute is the same, and 1577 // if the value doesn't reference the second vector. 1578 unsigned ElementBase = N->getMaskElt(0); 1579 1580 // FIXME: Handle UNDEF elements too! 1581 if (ElementBase >= 16) 1582 return false; 1583 1584 // Check that the indices are consecutive, in the case of a multi-byte element 1585 // splatted with a v16i8 mask. 1586 for (unsigned i = 1; i != EltSize; ++i) 1587 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase)) 1588 return false; 1589 1590 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) { 1591 if (N->getMaskElt(i) < 0) continue; 1592 for (unsigned j = 0; j != EltSize; ++j) 1593 if (N->getMaskElt(i+j) != N->getMaskElt(j)) 1594 return false; 1595 } 1596 return true; 1597 } 1598 1599 // Check that the mask is shuffling words 1600 static bool isWordShuffleMask(ShuffleVectorSDNode *N) { 1601 for (unsigned i = 0; i < 4; ++i) { 1602 unsigned B0 = N->getMaskElt(i*4); 1603 unsigned B1 = N->getMaskElt(i*4+1); 1604 unsigned B2 = N->getMaskElt(i*4+2); 1605 unsigned B3 = N->getMaskElt(i*4+3); 1606 if (B0 % 4) 1607 return false; 1608 if (B1 != B0+1 || B2 != B1+1 || B3 != B2+1) 1609 return false; 1610 } 1611 1612 return true; 1613 } 1614 1615 bool PPC::isXXINSERTWMask(ShuffleVectorSDNode *N, unsigned &ShiftElts, 1616 unsigned &InsertAtByte, bool &Swap, bool IsLE) { 1617 if (!isWordShuffleMask(N)) 1618 return false; 1619 1620 // Now we look at mask elements 0,4,8,12 1621 unsigned M0 = N->getMaskElt(0) / 4; 1622 unsigned M1 = N->getMaskElt(4) / 4; 1623 unsigned M2 = N->getMaskElt(8) / 4; 1624 unsigned M3 = N->getMaskElt(12) / 4; 1625 unsigned LittleEndianShifts[] = { 2, 1, 0, 3 }; 1626 unsigned BigEndianShifts[] = { 3, 0, 1, 2 }; 1627 1628 // Below, let H and L be arbitrary elements of the shuffle mask 1629 // where H is in the range [4,7] and L is in the range [0,3]. 1630 // H, 1, 2, 3 or L, 5, 6, 7 1631 if ((M0 > 3 && M1 == 1 && M2 == 2 && M3 == 3) || 1632 (M0 < 4 && M1 == 5 && M2 == 6 && M3 == 7)) { 1633 ShiftElts = IsLE ? LittleEndianShifts[M0 & 0x3] : BigEndianShifts[M0 & 0x3]; 1634 InsertAtByte = IsLE ? 12 : 0; 1635 Swap = M0 < 4; 1636 return true; 1637 } 1638 // 0, H, 2, 3 or 4, L, 6, 7 1639 if ((M1 > 3 && M0 == 0 && M2 == 2 && M3 == 3) || 1640 (M1 < 4 && M0 == 4 && M2 == 6 && M3 == 7)) { 1641 ShiftElts = IsLE ? LittleEndianShifts[M1 & 0x3] : BigEndianShifts[M1 & 0x3]; 1642 InsertAtByte = IsLE ? 8 : 4; 1643 Swap = M1 < 4; 1644 return true; 1645 } 1646 // 0, 1, H, 3 or 4, 5, L, 7 1647 if ((M2 > 3 && M0 == 0 && M1 == 1 && M3 == 3) || 1648 (M2 < 4 && M0 == 4 && M1 == 5 && M3 == 7)) { 1649 ShiftElts = IsLE ? LittleEndianShifts[M2 & 0x3] : BigEndianShifts[M2 & 0x3]; 1650 InsertAtByte = IsLE ? 4 : 8; 1651 Swap = M2 < 4; 1652 return true; 1653 } 1654 // 0, 1, 2, H or 4, 5, 6, L 1655 if ((M3 > 3 && M0 == 0 && M1 == 1 && M2 == 2) || 1656 (M3 < 4 && M0 == 4 && M1 == 5 && M2 == 6)) { 1657 ShiftElts = IsLE ? LittleEndianShifts[M3 & 0x3] : BigEndianShifts[M3 & 0x3]; 1658 InsertAtByte = IsLE ? 0 : 12; 1659 Swap = M3 < 4; 1660 return true; 1661 } 1662 1663 // If both vector operands for the shuffle are the same vector, the mask will 1664 // contain only elements from the first one and the second one will be undef. 1665 if (N->getOperand(1).isUndef()) { 1666 ShiftElts = 0; 1667 Swap = true; 1668 unsigned XXINSERTWSrcElem = IsLE ? 2 : 1; 1669 if (M0 == XXINSERTWSrcElem && M1 == 1 && M2 == 2 && M3 == 3) { 1670 InsertAtByte = IsLE ? 12 : 0; 1671 return true; 1672 } 1673 if (M0 == 0 && M1 == XXINSERTWSrcElem && M2 == 2 && M3 == 3) { 1674 InsertAtByte = IsLE ? 8 : 4; 1675 return true; 1676 } 1677 if (M0 == 0 && M1 == 1 && M2 == XXINSERTWSrcElem && M3 == 3) { 1678 InsertAtByte = IsLE ? 4 : 8; 1679 return true; 1680 } 1681 if (M0 == 0 && M1 == 1 && M2 == 2 && M3 == XXINSERTWSrcElem) { 1682 InsertAtByte = IsLE ? 0 : 12; 1683 return true; 1684 } 1685 } 1686 1687 return false; 1688 } 1689 1690 bool PPC::isXXSLDWIShuffleMask(ShuffleVectorSDNode *N, unsigned &ShiftElts, 1691 bool &Swap, bool IsLE) { 1692 assert(N->getValueType(0) == MVT::v16i8 && "Shuffle vector expects v16i8"); 1693 // Ensure each byte index of the word is consecutive. 1694 if (!isWordShuffleMask(N)) 1695 return false; 1696 1697 // Now we look at mask elements 0,4,8,12, which are the beginning of words. 1698 unsigned M0 = N->getMaskElt(0) / 4; 1699 unsigned M1 = N->getMaskElt(4) / 4; 1700 unsigned M2 = N->getMaskElt(8) / 4; 1701 unsigned M3 = N->getMaskElt(12) / 4; 1702 1703 // If both vector operands for the shuffle are the same vector, the mask will 1704 // contain only elements from the first one and the second one will be undef. 1705 if (N->getOperand(1).isUndef()) { 1706 assert(M0 < 4 && "Indexing into an undef vector?"); 1707 if (M1 != (M0 + 1) % 4 || M2 != (M1 + 1) % 4 || M3 != (M2 + 1) % 4) 1708 return false; 1709 1710 ShiftElts = IsLE ? (4 - M0) % 4 : M0; 1711 Swap = false; 1712 return true; 1713 } 1714 1715 // Ensure each word index of the ShuffleVector Mask is consecutive. 1716 if (M1 != (M0 + 1) % 8 || M2 != (M1 + 1) % 8 || M3 != (M2 + 1) % 8) 1717 return false; 1718 1719 if (IsLE) { 1720 if (M0 == 0 || M0 == 7 || M0 == 6 || M0 == 5) { 1721 // Input vectors don't need to be swapped if the leading element 1722 // of the result is one of the 3 left elements of the second vector 1723 // (or if there is no shift to be done at all). 1724 Swap = false; 1725 ShiftElts = (8 - M0) % 8; 1726 } else if (M0 == 4 || M0 == 3 || M0 == 2 || M0 == 1) { 1727 // Input vectors need to be swapped if the leading element 1728 // of the result is one of the 3 left elements of the first vector 1729 // (or if we're shifting by 4 - thereby simply swapping the vectors). 1730 Swap = true; 1731 ShiftElts = (4 - M0) % 4; 1732 } 1733 1734 return true; 1735 } else { // BE 1736 if (M0 == 0 || M0 == 1 || M0 == 2 || M0 == 3) { 1737 // Input vectors don't need to be swapped if the leading element 1738 // of the result is one of the 4 elements of the first vector. 1739 Swap = false; 1740 ShiftElts = M0; 1741 } else if (M0 == 4 || M0 == 5 || M0 == 6 || M0 == 7) { 1742 // Input vectors need to be swapped if the leading element 1743 // of the result is one of the 4 elements of the right vector. 1744 Swap = true; 1745 ShiftElts = M0 - 4; 1746 } 1747 1748 return true; 1749 } 1750 } 1751 1752 1753 /// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the 1754 /// specified isSplatShuffleMask VECTOR_SHUFFLE mask. 1755 unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize, 1756 SelectionDAG &DAG) { 1757 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); 1758 assert(isSplatShuffleMask(SVOp, EltSize)); 1759 if (DAG.getDataLayout().isLittleEndian()) 1760 return (16 / EltSize) - 1 - (SVOp->getMaskElt(0) / EltSize); 1761 else 1762 return SVOp->getMaskElt(0) / EltSize; 1763 } 1764 1765 /// get_VSPLTI_elt - If this is a build_vector of constants which can be formed 1766 /// by using a vspltis[bhw] instruction of the specified element size, return 1767 /// the constant being splatted. The ByteSize field indicates the number of 1768 /// bytes of each element [124] -> [bhw]. 1769 SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) { 1770 SDValue OpVal(nullptr, 0); 1771 1772 // If ByteSize of the splat is bigger than the element size of the 1773 // build_vector, then we have a case where we are checking for a splat where 1774 // multiple elements of the buildvector are folded together into a single 1775 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8). 1776 unsigned EltSize = 16/N->getNumOperands(); 1777 if (EltSize < ByteSize) { 1778 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval. 1779 SDValue UniquedVals[4]; 1780 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?"); 1781 1782 // See if all of the elements in the buildvector agree across. 1783 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 1784 if (N->getOperand(i).isUndef()) continue; 1785 // If the element isn't a constant, bail fully out. 1786 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue(); 1787 1788 if (!UniquedVals[i&(Multiple-1)].getNode()) 1789 UniquedVals[i&(Multiple-1)] = N->getOperand(i); 1790 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i)) 1791 return SDValue(); // no match. 1792 } 1793 1794 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains 1795 // either constant or undef values that are identical for each chunk. See 1796 // if these chunks can form into a larger vspltis*. 1797 1798 // Check to see if all of the leading entries are either 0 or -1. If 1799 // neither, then this won't fit into the immediate field. 1800 bool LeadingZero = true; 1801 bool LeadingOnes = true; 1802 for (unsigned i = 0; i != Multiple-1; ++i) { 1803 if (!UniquedVals[i].getNode()) continue; // Must have been undefs. 1804 1805 LeadingZero &= isNullConstant(UniquedVals[i]); 1806 LeadingOnes &= isAllOnesConstant(UniquedVals[i]); 1807 } 1808 // Finally, check the least significant entry. 1809 if (LeadingZero) { 1810 if (!UniquedVals[Multiple-1].getNode()) 1811 return DAG.getTargetConstant(0, SDLoc(N), MVT::i32); // 0,0,0,undef 1812 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue(); 1813 if (Val < 16) // 0,0,0,4 -> vspltisw(4) 1814 return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32); 1815 } 1816 if (LeadingOnes) { 1817 if (!UniquedVals[Multiple-1].getNode()) 1818 return DAG.getTargetConstant(~0U, SDLoc(N), MVT::i32); // -1,-1,-1,undef 1819 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue(); 1820 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2) 1821 return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32); 1822 } 1823 1824 return SDValue(); 1825 } 1826 1827 // Check to see if this buildvec has a single non-undef value in its elements. 1828 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 1829 if (N->getOperand(i).isUndef()) continue; 1830 if (!OpVal.getNode()) 1831 OpVal = N->getOperand(i); 1832 else if (OpVal != N->getOperand(i)) 1833 return SDValue(); 1834 } 1835 1836 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def. 1837 1838 unsigned ValSizeInBytes = EltSize; 1839 uint64_t Value = 0; 1840 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) { 1841 Value = CN->getZExtValue(); 1842 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) { 1843 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!"); 1844 Value = FloatToBits(CN->getValueAPF().convertToFloat()); 1845 } 1846 1847 // If the splat value is larger than the element value, then we can never do 1848 // this splat. The only case that we could fit the replicated bits into our 1849 // immediate field for would be zero, and we prefer to use vxor for it. 1850 if (ValSizeInBytes < ByteSize) return SDValue(); 1851 1852 // If the element value is larger than the splat value, check if it consists 1853 // of a repeated bit pattern of size ByteSize. 1854 if (!APInt(ValSizeInBytes * 8, Value).isSplat(ByteSize * 8)) 1855 return SDValue(); 1856 1857 // Properly sign extend the value. 1858 int MaskVal = SignExtend32(Value, ByteSize * 8); 1859 1860 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros. 1861 if (MaskVal == 0) return SDValue(); 1862 1863 // Finally, if this value fits in a 5 bit sext field, return it 1864 if (SignExtend32<5>(MaskVal) == MaskVal) 1865 return DAG.getTargetConstant(MaskVal, SDLoc(N), MVT::i32); 1866 return SDValue(); 1867 } 1868 1869 /// isQVALIGNIShuffleMask - If this is a qvaligni shuffle mask, return the shift 1870 /// amount, otherwise return -1. 1871 int PPC::isQVALIGNIShuffleMask(SDNode *N) { 1872 EVT VT = N->getValueType(0); 1873 if (VT != MVT::v4f64 && VT != MVT::v4f32 && VT != MVT::v4i1) 1874 return -1; 1875 1876 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); 1877 1878 // Find the first non-undef value in the shuffle mask. 1879 unsigned i; 1880 for (i = 0; i != 4 && SVOp->getMaskElt(i) < 0; ++i) 1881 /*search*/; 1882 1883 if (i == 4) return -1; // all undef. 1884 1885 // Otherwise, check to see if the rest of the elements are consecutively 1886 // numbered from this value. 1887 unsigned ShiftAmt = SVOp->getMaskElt(i); 1888 if (ShiftAmt < i) return -1; 1889 ShiftAmt -= i; 1890 1891 // Check the rest of the elements to see if they are consecutive. 1892 for (++i; i != 4; ++i) 1893 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i)) 1894 return -1; 1895 1896 return ShiftAmt; 1897 } 1898 1899 //===----------------------------------------------------------------------===// 1900 // Addressing Mode Selection 1901 //===----------------------------------------------------------------------===// 1902 1903 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit 1904 /// or 64-bit immediate, and if the value can be accurately represented as a 1905 /// sign extension from a 16-bit value. If so, this returns true and the 1906 /// immediate. 1907 static bool isIntS16Immediate(SDNode *N, short &Imm) { 1908 if (!isa<ConstantSDNode>(N)) 1909 return false; 1910 1911 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue(); 1912 if (N->getValueType(0) == MVT::i32) 1913 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue(); 1914 else 1915 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue(); 1916 } 1917 static bool isIntS16Immediate(SDValue Op, short &Imm) { 1918 return isIntS16Immediate(Op.getNode(), Imm); 1919 } 1920 1921 /// SelectAddressRegReg - Given the specified addressed, check to see if it 1922 /// can be represented as an indexed [r+r] operation. Returns false if it 1923 /// can be more efficiently represented with [r+imm]. 1924 bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base, 1925 SDValue &Index, 1926 SelectionDAG &DAG) const { 1927 short imm = 0; 1928 if (N.getOpcode() == ISD::ADD) { 1929 if (isIntS16Immediate(N.getOperand(1), imm)) 1930 return false; // r+i 1931 if (N.getOperand(1).getOpcode() == PPCISD::Lo) 1932 return false; // r+i 1933 1934 Base = N.getOperand(0); 1935 Index = N.getOperand(1); 1936 return true; 1937 } else if (N.getOpcode() == ISD::OR) { 1938 if (isIntS16Immediate(N.getOperand(1), imm)) 1939 return false; // r+i can fold it if we can. 1940 1941 // If this is an or of disjoint bitfields, we can codegen this as an add 1942 // (for better address arithmetic) if the LHS and RHS of the OR are provably 1943 // disjoint. 1944 KnownBits LHSKnown, RHSKnown; 1945 DAG.computeKnownBits(N.getOperand(0), LHSKnown); 1946 1947 if (LHSKnown.Zero.getBoolValue()) { 1948 DAG.computeKnownBits(N.getOperand(1), RHSKnown); 1949 // If all of the bits are known zero on the LHS or RHS, the add won't 1950 // carry. 1951 if (~(LHSKnown.Zero | RHSKnown.Zero) == 0) { 1952 Base = N.getOperand(0); 1953 Index = N.getOperand(1); 1954 return true; 1955 } 1956 } 1957 } 1958 1959 return false; 1960 } 1961 1962 // If we happen to be doing an i64 load or store into a stack slot that has 1963 // less than a 4-byte alignment, then the frame-index elimination may need to 1964 // use an indexed load or store instruction (because the offset may not be a 1965 // multiple of 4). The extra register needed to hold the offset comes from the 1966 // register scavenger, and it is possible that the scavenger will need to use 1967 // an emergency spill slot. As a result, we need to make sure that a spill slot 1968 // is allocated when doing an i64 load/store into a less-than-4-byte-aligned 1969 // stack slot. 1970 static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) { 1971 // FIXME: This does not handle the LWA case. 1972 if (VT != MVT::i64) 1973 return; 1974 1975 // NOTE: We'll exclude negative FIs here, which come from argument 1976 // lowering, because there are no known test cases triggering this problem 1977 // using packed structures (or similar). We can remove this exclusion if 1978 // we find such a test case. The reason why this is so test-case driven is 1979 // because this entire 'fixup' is only to prevent crashes (from the 1980 // register scavenger) on not-really-valid inputs. For example, if we have: 1981 // %a = alloca i1 1982 // %b = bitcast i1* %a to i64* 1983 // store i64* a, i64 b 1984 // then the store should really be marked as 'align 1', but is not. If it 1985 // were marked as 'align 1' then the indexed form would have been 1986 // instruction-selected initially, and the problem this 'fixup' is preventing 1987 // won't happen regardless. 1988 if (FrameIdx < 0) 1989 return; 1990 1991 MachineFunction &MF = DAG.getMachineFunction(); 1992 MachineFrameInfo &MFI = MF.getFrameInfo(); 1993 1994 unsigned Align = MFI.getObjectAlignment(FrameIdx); 1995 if (Align >= 4) 1996 return; 1997 1998 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 1999 FuncInfo->setHasNonRISpills(); 2000 } 2001 2002 /// Returns true if the address N can be represented by a base register plus 2003 /// a signed 16-bit displacement [r+imm], and if it is not better 2004 /// represented as reg+reg. If Aligned is true, only accept displacements 2005 /// suitable for STD and friends, i.e. multiples of 4. 2006 bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp, 2007 SDValue &Base, 2008 SelectionDAG &DAG, 2009 bool Aligned) const { 2010 // FIXME dl should come from parent load or store, not from address 2011 SDLoc dl(N); 2012 // If this can be more profitably realized as r+r, fail. 2013 if (SelectAddressRegReg(N, Disp, Base, DAG)) 2014 return false; 2015 2016 if (N.getOpcode() == ISD::ADD) { 2017 short imm = 0; 2018 if (isIntS16Immediate(N.getOperand(1), imm) && 2019 (!Aligned || (imm & 3) == 0)) { 2020 Disp = DAG.getTargetConstant(imm, dl, N.getValueType()); 2021 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) { 2022 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); 2023 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType()); 2024 } else { 2025 Base = N.getOperand(0); 2026 } 2027 return true; // [r+i] 2028 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) { 2029 // Match LOAD (ADD (X, Lo(G))). 2030 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue() 2031 && "Cannot handle constant offsets yet!"); 2032 Disp = N.getOperand(1).getOperand(0); // The global address. 2033 assert(Disp.getOpcode() == ISD::TargetGlobalAddress || 2034 Disp.getOpcode() == ISD::TargetGlobalTLSAddress || 2035 Disp.getOpcode() == ISD::TargetConstantPool || 2036 Disp.getOpcode() == ISD::TargetJumpTable); 2037 Base = N.getOperand(0); 2038 return true; // [&g+r] 2039 } 2040 } else if (N.getOpcode() == ISD::OR) { 2041 short imm = 0; 2042 if (isIntS16Immediate(N.getOperand(1), imm) && 2043 (!Aligned || (imm & 3) == 0)) { 2044 // If this is an or of disjoint bitfields, we can codegen this as an add 2045 // (for better address arithmetic) if the LHS and RHS of the OR are 2046 // provably disjoint. 2047 KnownBits LHSKnown; 2048 DAG.computeKnownBits(N.getOperand(0), LHSKnown); 2049 2050 if ((LHSKnown.Zero.getZExtValue()|~(uint64_t)imm) == ~0ULL) { 2051 // If all of the bits are known zero on the LHS or RHS, the add won't 2052 // carry. 2053 if (FrameIndexSDNode *FI = 2054 dyn_cast<FrameIndexSDNode>(N.getOperand(0))) { 2055 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); 2056 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType()); 2057 } else { 2058 Base = N.getOperand(0); 2059 } 2060 Disp = DAG.getTargetConstant(imm, dl, N.getValueType()); 2061 return true; 2062 } 2063 } 2064 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { 2065 // Loading from a constant address. 2066 2067 // If this address fits entirely in a 16-bit sext immediate field, codegen 2068 // this as "d, 0" 2069 short Imm; 2070 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) { 2071 Disp = DAG.getTargetConstant(Imm, dl, CN->getValueType(0)); 2072 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO, 2073 CN->getValueType(0)); 2074 return true; 2075 } 2076 2077 // Handle 32-bit sext immediates with LIS + addr mode. 2078 if ((CN->getValueType(0) == MVT::i32 || 2079 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) && 2080 (!Aligned || (CN->getZExtValue() & 3) == 0)) { 2081 int Addr = (int)CN->getZExtValue(); 2082 2083 // Otherwise, break this down into an LIS + disp. 2084 Disp = DAG.getTargetConstant((short)Addr, dl, MVT::i32); 2085 2086 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, dl, 2087 MVT::i32); 2088 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8; 2089 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0); 2090 return true; 2091 } 2092 } 2093 2094 Disp = DAG.getTargetConstant(0, dl, getPointerTy(DAG.getDataLayout())); 2095 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) { 2096 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); 2097 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType()); 2098 } else 2099 Base = N; 2100 return true; // [r+0] 2101 } 2102 2103 /// SelectAddressRegRegOnly - Given the specified addressed, force it to be 2104 /// represented as an indexed [r+r] operation. 2105 bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base, 2106 SDValue &Index, 2107 SelectionDAG &DAG) const { 2108 // Check to see if we can easily represent this as an [r+r] address. This 2109 // will fail if it thinks that the address is more profitably represented as 2110 // reg+imm, e.g. where imm = 0. 2111 if (SelectAddressRegReg(N, Base, Index, DAG)) 2112 return true; 2113 2114 // If the operand is an addition, always emit this as [r+r], since this is 2115 // better (for code size, and execution, as the memop does the add for free) 2116 // than emitting an explicit add. 2117 if (N.getOpcode() == ISD::ADD) { 2118 Base = N.getOperand(0); 2119 Index = N.getOperand(1); 2120 return true; 2121 } 2122 2123 // Otherwise, do it the hard way, using R0 as the base register. 2124 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO, 2125 N.getValueType()); 2126 Index = N; 2127 return true; 2128 } 2129 2130 /// getPreIndexedAddressParts - returns true by value, base pointer and 2131 /// offset pointer and addressing mode by reference if the node's address 2132 /// can be legally represented as pre-indexed load / store address. 2133 bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 2134 SDValue &Offset, 2135 ISD::MemIndexedMode &AM, 2136 SelectionDAG &DAG) const { 2137 if (DisablePPCPreinc) return false; 2138 2139 bool isLoad = true; 2140 SDValue Ptr; 2141 EVT VT; 2142 unsigned Alignment; 2143 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 2144 Ptr = LD->getBasePtr(); 2145 VT = LD->getMemoryVT(); 2146 Alignment = LD->getAlignment(); 2147 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 2148 Ptr = ST->getBasePtr(); 2149 VT = ST->getMemoryVT(); 2150 Alignment = ST->getAlignment(); 2151 isLoad = false; 2152 } else 2153 return false; 2154 2155 // PowerPC doesn't have preinc load/store instructions for vectors (except 2156 // for QPX, which does have preinc r+r forms). 2157 if (VT.isVector()) { 2158 if (!Subtarget.hasQPX() || (VT != MVT::v4f64 && VT != MVT::v4f32)) { 2159 return false; 2160 } else if (SelectAddressRegRegOnly(Ptr, Offset, Base, DAG)) { 2161 AM = ISD::PRE_INC; 2162 return true; 2163 } 2164 } 2165 2166 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) { 2167 // Common code will reject creating a pre-inc form if the base pointer 2168 // is a frame index, or if N is a store and the base pointer is either 2169 // the same as or a predecessor of the value being stored. Check for 2170 // those situations here, and try with swapped Base/Offset instead. 2171 bool Swap = false; 2172 2173 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base)) 2174 Swap = true; 2175 else if (!isLoad) { 2176 SDValue Val = cast<StoreSDNode>(N)->getValue(); 2177 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode())) 2178 Swap = true; 2179 } 2180 2181 if (Swap) 2182 std::swap(Base, Offset); 2183 2184 AM = ISD::PRE_INC; 2185 return true; 2186 } 2187 2188 // LDU/STU can only handle immediates that are a multiple of 4. 2189 if (VT != MVT::i64) { 2190 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false)) 2191 return false; 2192 } else { 2193 // LDU/STU need an address with at least 4-byte alignment. 2194 if (Alignment < 4) 2195 return false; 2196 2197 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true)) 2198 return false; 2199 } 2200 2201 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 2202 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of 2203 // sext i32 to i64 when addr mode is r+i. 2204 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 && 2205 LD->getExtensionType() == ISD::SEXTLOAD && 2206 isa<ConstantSDNode>(Offset)) 2207 return false; 2208 } 2209 2210 AM = ISD::PRE_INC; 2211 return true; 2212 } 2213 2214 //===----------------------------------------------------------------------===// 2215 // LowerOperation implementation 2216 //===----------------------------------------------------------------------===// 2217 2218 /// Return true if we should reference labels using a PICBase, set the HiOpFlags 2219 /// and LoOpFlags to the target MO flags. 2220 static void getLabelAccessInfo(bool IsPIC, const PPCSubtarget &Subtarget, 2221 unsigned &HiOpFlags, unsigned &LoOpFlags, 2222 const GlobalValue *GV = nullptr) { 2223 HiOpFlags = PPCII::MO_HA; 2224 LoOpFlags = PPCII::MO_LO; 2225 2226 // Don't use the pic base if not in PIC relocation model. 2227 if (IsPIC) { 2228 HiOpFlags |= PPCII::MO_PIC_FLAG; 2229 LoOpFlags |= PPCII::MO_PIC_FLAG; 2230 } 2231 2232 // If this is a reference to a global value that requires a non-lazy-ptr, make 2233 // sure that instruction lowering adds it. 2234 if (GV && Subtarget.hasLazyResolverStub(GV)) { 2235 HiOpFlags |= PPCII::MO_NLP_FLAG; 2236 LoOpFlags |= PPCII::MO_NLP_FLAG; 2237 2238 if (GV->hasHiddenVisibility()) { 2239 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG; 2240 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG; 2241 } 2242 } 2243 } 2244 2245 static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC, 2246 SelectionDAG &DAG) { 2247 SDLoc DL(HiPart); 2248 EVT PtrVT = HiPart.getValueType(); 2249 SDValue Zero = DAG.getConstant(0, DL, PtrVT); 2250 2251 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero); 2252 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero); 2253 2254 // With PIC, the first instruction is actually "GR+hi(&G)". 2255 if (isPIC) 2256 Hi = DAG.getNode(ISD::ADD, DL, PtrVT, 2257 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi); 2258 2259 // Generate non-pic code that has direct accesses to the constant pool. 2260 // The address of the global is just (hi(&g)+lo(&g)). 2261 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo); 2262 } 2263 2264 static void setUsesTOCBasePtr(MachineFunction &MF) { 2265 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 2266 FuncInfo->setUsesTOCBasePtr(); 2267 } 2268 2269 static void setUsesTOCBasePtr(SelectionDAG &DAG) { 2270 setUsesTOCBasePtr(DAG.getMachineFunction()); 2271 } 2272 2273 static SDValue getTOCEntry(SelectionDAG &DAG, const SDLoc &dl, bool Is64Bit, 2274 SDValue GA) { 2275 EVT VT = Is64Bit ? MVT::i64 : MVT::i32; 2276 SDValue Reg = Is64Bit ? DAG.getRegister(PPC::X2, VT) : 2277 DAG.getNode(PPCISD::GlobalBaseReg, dl, VT); 2278 2279 SDValue Ops[] = { GA, Reg }; 2280 return DAG.getMemIntrinsicNode( 2281 PPCISD::TOC_ENTRY, dl, DAG.getVTList(VT, MVT::Other), Ops, VT, 2282 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 0, false, true, 2283 false, 0); 2284 } 2285 2286 SDValue PPCTargetLowering::LowerConstantPool(SDValue Op, 2287 SelectionDAG &DAG) const { 2288 EVT PtrVT = Op.getValueType(); 2289 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 2290 const Constant *C = CP->getConstVal(); 2291 2292 // 64-bit SVR4 ABI code is always position-independent. 2293 // The actual address of the GlobalValue is stored in the TOC. 2294 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) { 2295 setUsesTOCBasePtr(DAG); 2296 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0); 2297 return getTOCEntry(DAG, SDLoc(CP), true, GA); 2298 } 2299 2300 unsigned MOHiFlag, MOLoFlag; 2301 bool IsPIC = isPositionIndependent(); 2302 getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag); 2303 2304 if (IsPIC && Subtarget.isSVR4ABI()) { 2305 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 2306 PPCII::MO_PIC_FLAG); 2307 return getTOCEntry(DAG, SDLoc(CP), false, GA); 2308 } 2309 2310 SDValue CPIHi = 2311 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag); 2312 SDValue CPILo = 2313 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag); 2314 return LowerLabelRef(CPIHi, CPILo, IsPIC, DAG); 2315 } 2316 2317 // For 64-bit PowerPC, prefer the more compact relative encodings. 2318 // This trades 32 bits per jump table entry for one or two instructions 2319 // on the jump site. 2320 unsigned PPCTargetLowering::getJumpTableEncoding() const { 2321 if (isJumpTableRelative()) 2322 return MachineJumpTableInfo::EK_LabelDifference32; 2323 2324 return TargetLowering::getJumpTableEncoding(); 2325 } 2326 2327 bool PPCTargetLowering::isJumpTableRelative() const { 2328 if (Subtarget.isPPC64()) 2329 return true; 2330 return TargetLowering::isJumpTableRelative(); 2331 } 2332 2333 SDValue PPCTargetLowering::getPICJumpTableRelocBase(SDValue Table, 2334 SelectionDAG &DAG) const { 2335 if (!Subtarget.isPPC64()) 2336 return TargetLowering::getPICJumpTableRelocBase(Table, DAG); 2337 2338 switch (getTargetMachine().getCodeModel()) { 2339 case CodeModel::Default: 2340 case CodeModel::Small: 2341 case CodeModel::Medium: 2342 return TargetLowering::getPICJumpTableRelocBase(Table, DAG); 2343 default: 2344 return DAG.getNode(PPCISD::GlobalBaseReg, SDLoc(), 2345 getPointerTy(DAG.getDataLayout())); 2346 } 2347 } 2348 2349 const MCExpr * 2350 PPCTargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF, 2351 unsigned JTI, 2352 MCContext &Ctx) const { 2353 if (!Subtarget.isPPC64()) 2354 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx); 2355 2356 switch (getTargetMachine().getCodeModel()) { 2357 case CodeModel::Default: 2358 case CodeModel::Small: 2359 case CodeModel::Medium: 2360 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx); 2361 default: 2362 return MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx); 2363 } 2364 } 2365 2366 SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const { 2367 EVT PtrVT = Op.getValueType(); 2368 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); 2369 2370 // 64-bit SVR4 ABI code is always position-independent. 2371 // The actual address of the GlobalValue is stored in the TOC. 2372 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) { 2373 setUsesTOCBasePtr(DAG); 2374 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); 2375 return getTOCEntry(DAG, SDLoc(JT), true, GA); 2376 } 2377 2378 unsigned MOHiFlag, MOLoFlag; 2379 bool IsPIC = isPositionIndependent(); 2380 getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag); 2381 2382 if (IsPIC && Subtarget.isSVR4ABI()) { 2383 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, 2384 PPCII::MO_PIC_FLAG); 2385 return getTOCEntry(DAG, SDLoc(GA), false, GA); 2386 } 2387 2388 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag); 2389 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag); 2390 return LowerLabelRef(JTIHi, JTILo, IsPIC, DAG); 2391 } 2392 2393 SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op, 2394 SelectionDAG &DAG) const { 2395 EVT PtrVT = Op.getValueType(); 2396 BlockAddressSDNode *BASDN = cast<BlockAddressSDNode>(Op); 2397 const BlockAddress *BA = BASDN->getBlockAddress(); 2398 2399 // 64-bit SVR4 ABI code is always position-independent. 2400 // The actual BlockAddress is stored in the TOC. 2401 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) { 2402 setUsesTOCBasePtr(DAG); 2403 SDValue GA = DAG.getTargetBlockAddress(BA, PtrVT, BASDN->getOffset()); 2404 return getTOCEntry(DAG, SDLoc(BASDN), true, GA); 2405 } 2406 2407 unsigned MOHiFlag, MOLoFlag; 2408 bool IsPIC = isPositionIndependent(); 2409 getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag); 2410 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag); 2411 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag); 2412 return LowerLabelRef(TgtBAHi, TgtBALo, IsPIC, DAG); 2413 } 2414 2415 SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op, 2416 SelectionDAG &DAG) const { 2417 // FIXME: TLS addresses currently use medium model code sequences, 2418 // which is the most useful form. Eventually support for small and 2419 // large models could be added if users need it, at the cost of 2420 // additional complexity. 2421 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 2422 if (DAG.getTarget().Options.EmulatedTLS) 2423 return LowerToTLSEmulatedModel(GA, DAG); 2424 2425 SDLoc dl(GA); 2426 const GlobalValue *GV = GA->getGlobal(); 2427 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2428 bool is64bit = Subtarget.isPPC64(); 2429 const Module *M = DAG.getMachineFunction().getFunction()->getParent(); 2430 PICLevel::Level picLevel = M->getPICLevel(); 2431 2432 TLSModel::Model Model = getTargetMachine().getTLSModel(GV); 2433 2434 if (Model == TLSModel::LocalExec) { 2435 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 2436 PPCII::MO_TPREL_HA); 2437 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 2438 PPCII::MO_TPREL_LO); 2439 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2, 2440 is64bit ? MVT::i64 : MVT::i32); 2441 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg); 2442 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi); 2443 } 2444 2445 if (Model == TLSModel::InitialExec) { 2446 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0); 2447 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 2448 PPCII::MO_TLS); 2449 SDValue GOTPtr; 2450 if (is64bit) { 2451 setUsesTOCBasePtr(DAG); 2452 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64); 2453 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl, 2454 PtrVT, GOTReg, TGA); 2455 } else 2456 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT); 2457 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl, 2458 PtrVT, TGA, GOTPtr); 2459 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS); 2460 } 2461 2462 if (Model == TLSModel::GeneralDynamic) { 2463 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0); 2464 SDValue GOTPtr; 2465 if (is64bit) { 2466 setUsesTOCBasePtr(DAG); 2467 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64); 2468 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT, 2469 GOTReg, TGA); 2470 } else { 2471 if (picLevel == PICLevel::SmallPIC) 2472 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT); 2473 else 2474 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT); 2475 } 2476 return DAG.getNode(PPCISD::ADDI_TLSGD_L_ADDR, dl, PtrVT, 2477 GOTPtr, TGA, TGA); 2478 } 2479 2480 if (Model == TLSModel::LocalDynamic) { 2481 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0); 2482 SDValue GOTPtr; 2483 if (is64bit) { 2484 setUsesTOCBasePtr(DAG); 2485 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64); 2486 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT, 2487 GOTReg, TGA); 2488 } else { 2489 if (picLevel == PICLevel::SmallPIC) 2490 GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT); 2491 else 2492 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT); 2493 } 2494 SDValue TLSAddr = DAG.getNode(PPCISD::ADDI_TLSLD_L_ADDR, dl, 2495 PtrVT, GOTPtr, TGA, TGA); 2496 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, 2497 PtrVT, TLSAddr, TGA); 2498 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA); 2499 } 2500 2501 llvm_unreachable("Unknown TLS model!"); 2502 } 2503 2504 SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op, 2505 SelectionDAG &DAG) const { 2506 EVT PtrVT = Op.getValueType(); 2507 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op); 2508 SDLoc DL(GSDN); 2509 const GlobalValue *GV = GSDN->getGlobal(); 2510 2511 // 64-bit SVR4 ABI code is always position-independent. 2512 // The actual address of the GlobalValue is stored in the TOC. 2513 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) { 2514 setUsesTOCBasePtr(DAG); 2515 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset()); 2516 return getTOCEntry(DAG, DL, true, GA); 2517 } 2518 2519 unsigned MOHiFlag, MOLoFlag; 2520 bool IsPIC = isPositionIndependent(); 2521 getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag, GV); 2522 2523 if (IsPIC && Subtarget.isSVR4ABI()) { 2524 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 2525 GSDN->getOffset(), 2526 PPCII::MO_PIC_FLAG); 2527 return getTOCEntry(DAG, DL, false, GA); 2528 } 2529 2530 SDValue GAHi = 2531 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag); 2532 SDValue GALo = 2533 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag); 2534 2535 SDValue Ptr = LowerLabelRef(GAHi, GALo, IsPIC, DAG); 2536 2537 // If the global reference is actually to a non-lazy-pointer, we have to do an 2538 // extra load to get the address of the global. 2539 if (MOHiFlag & PPCII::MO_NLP_FLAG) 2540 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo()); 2541 return Ptr; 2542 } 2543 2544 SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { 2545 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 2546 SDLoc dl(Op); 2547 2548 if (Op.getValueType() == MVT::v2i64) { 2549 // When the operands themselves are v2i64 values, we need to do something 2550 // special because VSX has no underlying comparison operations for these. 2551 if (Op.getOperand(0).getValueType() == MVT::v2i64) { 2552 // Equality can be handled by casting to the legal type for Altivec 2553 // comparisons, everything else needs to be expanded. 2554 if (CC == ISD::SETEQ || CC == ISD::SETNE) { 2555 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, 2556 DAG.getSetCC(dl, MVT::v4i32, 2557 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)), 2558 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)), 2559 CC)); 2560 } 2561 2562 return SDValue(); 2563 } 2564 2565 // We handle most of these in the usual way. 2566 return Op; 2567 } 2568 2569 // If we're comparing for equality to zero, expose the fact that this is 2570 // implemented as a ctlz/srl pair on ppc, so that the dag combiner can 2571 // fold the new nodes. 2572 if (SDValue V = lowerCmpEqZeroToCtlzSrl(Op, DAG)) 2573 return V; 2574 2575 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 2576 // Leave comparisons against 0 and -1 alone for now, since they're usually 2577 // optimized. FIXME: revisit this when we can custom lower all setcc 2578 // optimizations. 2579 if (C->isAllOnesValue() || C->isNullValue()) 2580 return SDValue(); 2581 } 2582 2583 // If we have an integer seteq/setne, turn it into a compare against zero 2584 // by xor'ing the rhs with the lhs, which is faster than setting a 2585 // condition register, reading it back out, and masking the correct bit. The 2586 // normal approach here uses sub to do this instead of xor. Using xor exposes 2587 // the result to other bit-twiddling opportunities. 2588 EVT LHSVT = Op.getOperand(0).getValueType(); 2589 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) { 2590 EVT VT = Op.getValueType(); 2591 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0), 2592 Op.getOperand(1)); 2593 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, dl, LHSVT), CC); 2594 } 2595 return SDValue(); 2596 } 2597 2598 SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const { 2599 SDNode *Node = Op.getNode(); 2600 EVT VT = Node->getValueType(0); 2601 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2602 SDValue InChain = Node->getOperand(0); 2603 SDValue VAListPtr = Node->getOperand(1); 2604 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); 2605 SDLoc dl(Node); 2606 2607 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only"); 2608 2609 // gpr_index 2610 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain, 2611 VAListPtr, MachinePointerInfo(SV), MVT::i8); 2612 InChain = GprIndex.getValue(1); 2613 2614 if (VT == MVT::i64) { 2615 // Check if GprIndex is even 2616 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex, 2617 DAG.getConstant(1, dl, MVT::i32)); 2618 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd, 2619 DAG.getConstant(0, dl, MVT::i32), ISD::SETNE); 2620 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex, 2621 DAG.getConstant(1, dl, MVT::i32)); 2622 // Align GprIndex to be even if it isn't 2623 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne, 2624 GprIndex); 2625 } 2626 2627 // fpr index is 1 byte after gpr 2628 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr, 2629 DAG.getConstant(1, dl, MVT::i32)); 2630 2631 // fpr 2632 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain, 2633 FprPtr, MachinePointerInfo(SV), MVT::i8); 2634 InChain = FprIndex.getValue(1); 2635 2636 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr, 2637 DAG.getConstant(8, dl, MVT::i32)); 2638 2639 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr, 2640 DAG.getConstant(4, dl, MVT::i32)); 2641 2642 // areas 2643 SDValue OverflowArea = 2644 DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr, MachinePointerInfo()); 2645 InChain = OverflowArea.getValue(1); 2646 2647 SDValue RegSaveArea = 2648 DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr, MachinePointerInfo()); 2649 InChain = RegSaveArea.getValue(1); 2650 2651 // select overflow_area if index > 8 2652 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex, 2653 DAG.getConstant(8, dl, MVT::i32), ISD::SETLT); 2654 2655 // adjustment constant gpr_index * 4/8 2656 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32, 2657 VT.isInteger() ? GprIndex : FprIndex, 2658 DAG.getConstant(VT.isInteger() ? 4 : 8, dl, 2659 MVT::i32)); 2660 2661 // OurReg = RegSaveArea + RegConstant 2662 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea, 2663 RegConstant); 2664 2665 // Floating types are 32 bytes into RegSaveArea 2666 if (VT.isFloatingPoint()) 2667 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg, 2668 DAG.getConstant(32, dl, MVT::i32)); 2669 2670 // increase {f,g}pr_index by 1 (or 2 if VT is i64) 2671 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32, 2672 VT.isInteger() ? GprIndex : FprIndex, 2673 DAG.getConstant(VT == MVT::i64 ? 2 : 1, dl, 2674 MVT::i32)); 2675 2676 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1, 2677 VT.isInteger() ? VAListPtr : FprPtr, 2678 MachinePointerInfo(SV), MVT::i8); 2679 2680 // determine if we should load from reg_save_area or overflow_area 2681 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea); 2682 2683 // increase overflow_area by 4/8 if gpr/fpr > 8 2684 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea, 2685 DAG.getConstant(VT.isInteger() ? 4 : 8, 2686 dl, MVT::i32)); 2687 2688 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea, 2689 OverflowAreaPlusN); 2690 2691 InChain = DAG.getTruncStore(InChain, dl, OverflowArea, OverflowAreaPtr, 2692 MachinePointerInfo(), MVT::i32); 2693 2694 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo()); 2695 } 2696 2697 SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const { 2698 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only"); 2699 2700 // We have to copy the entire va_list struct: 2701 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte 2702 return DAG.getMemcpy(Op.getOperand(0), Op, 2703 Op.getOperand(1), Op.getOperand(2), 2704 DAG.getConstant(12, SDLoc(Op), MVT::i32), 8, false, true, 2705 false, MachinePointerInfo(), MachinePointerInfo()); 2706 } 2707 2708 SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op, 2709 SelectionDAG &DAG) const { 2710 return Op.getOperand(0); 2711 } 2712 2713 SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op, 2714 SelectionDAG &DAG) const { 2715 SDValue Chain = Op.getOperand(0); 2716 SDValue Trmp = Op.getOperand(1); // trampoline 2717 SDValue FPtr = Op.getOperand(2); // nested function 2718 SDValue Nest = Op.getOperand(3); // 'nest' parameter value 2719 SDLoc dl(Op); 2720 2721 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2722 bool isPPC64 = (PtrVT == MVT::i64); 2723 Type *IntPtrTy = DAG.getDataLayout().getIntPtrType(*DAG.getContext()); 2724 2725 TargetLowering::ArgListTy Args; 2726 TargetLowering::ArgListEntry Entry; 2727 2728 Entry.Ty = IntPtrTy; 2729 Entry.Node = Trmp; Args.push_back(Entry); 2730 2731 // TrampSize == (isPPC64 ? 48 : 40); 2732 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40, dl, 2733 isPPC64 ? MVT::i64 : MVT::i32); 2734 Args.push_back(Entry); 2735 2736 Entry.Node = FPtr; Args.push_back(Entry); 2737 Entry.Node = Nest; Args.push_back(Entry); 2738 2739 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg) 2740 TargetLowering::CallLoweringInfo CLI(DAG); 2741 CLI.setDebugLoc(dl).setChain(Chain).setLibCallee( 2742 CallingConv::C, Type::getVoidTy(*DAG.getContext()), 2743 DAG.getExternalSymbol("__trampoline_setup", PtrVT), std::move(Args)); 2744 2745 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 2746 return CallResult.second; 2747 } 2748 2749 SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const { 2750 MachineFunction &MF = DAG.getMachineFunction(); 2751 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 2752 EVT PtrVT = getPointerTy(MF.getDataLayout()); 2753 2754 SDLoc dl(Op); 2755 2756 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) { 2757 // vastart just stores the address of the VarArgsFrameIndex slot into the 2758 // memory location argument. 2759 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 2760 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 2761 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), 2762 MachinePointerInfo(SV)); 2763 } 2764 2765 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct. 2766 // We suppose the given va_list is already allocated. 2767 // 2768 // typedef struct { 2769 // char gpr; /* index into the array of 8 GPRs 2770 // * stored in the register save area 2771 // * gpr=0 corresponds to r3, 2772 // * gpr=1 to r4, etc. 2773 // */ 2774 // char fpr; /* index into the array of 8 FPRs 2775 // * stored in the register save area 2776 // * fpr=0 corresponds to f1, 2777 // * fpr=1 to f2, etc. 2778 // */ 2779 // char *overflow_arg_area; 2780 // /* location on stack that holds 2781 // * the next overflow argument 2782 // */ 2783 // char *reg_save_area; 2784 // /* where r3:r10 and f1:f8 (if saved) 2785 // * are stored 2786 // */ 2787 // } va_list[1]; 2788 2789 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), dl, MVT::i32); 2790 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), dl, MVT::i32); 2791 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(), 2792 PtrVT); 2793 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), 2794 PtrVT); 2795 2796 uint64_t FrameOffset = PtrVT.getSizeInBits()/8; 2797 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, dl, PtrVT); 2798 2799 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1; 2800 SDValue ConstStackOffset = DAG.getConstant(StackOffset, dl, PtrVT); 2801 2802 uint64_t FPROffset = 1; 2803 SDValue ConstFPROffset = DAG.getConstant(FPROffset, dl, PtrVT); 2804 2805 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 2806 2807 // Store first byte : number of int regs 2808 SDValue firstStore = 2809 DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR, Op.getOperand(1), 2810 MachinePointerInfo(SV), MVT::i8); 2811 uint64_t nextOffset = FPROffset; 2812 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1), 2813 ConstFPROffset); 2814 2815 // Store second byte : number of float regs 2816 SDValue secondStore = 2817 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr, 2818 MachinePointerInfo(SV, nextOffset), MVT::i8); 2819 nextOffset += StackOffset; 2820 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset); 2821 2822 // Store second word : arguments given on stack 2823 SDValue thirdStore = DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr, 2824 MachinePointerInfo(SV, nextOffset)); 2825 nextOffset += FrameOffset; 2826 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset); 2827 2828 // Store third word : arguments given in registers 2829 return DAG.getStore(thirdStore, dl, FR, nextPtr, 2830 MachinePointerInfo(SV, nextOffset)); 2831 } 2832 2833 #include "PPCGenCallingConv.inc" 2834 2835 // Function whose sole purpose is to kill compiler warnings 2836 // stemming from unused functions included from PPCGenCallingConv.inc. 2837 CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const { 2838 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS; 2839 } 2840 2841 bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT, 2842 CCValAssign::LocInfo &LocInfo, 2843 ISD::ArgFlagsTy &ArgFlags, 2844 CCState &State) { 2845 return true; 2846 } 2847 2848 bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT, 2849 MVT &LocVT, 2850 CCValAssign::LocInfo &LocInfo, 2851 ISD::ArgFlagsTy &ArgFlags, 2852 CCState &State) { 2853 static const MCPhysReg ArgRegs[] = { 2854 PPC::R3, PPC::R4, PPC::R5, PPC::R6, 2855 PPC::R7, PPC::R8, PPC::R9, PPC::R10, 2856 }; 2857 const unsigned NumArgRegs = array_lengthof(ArgRegs); 2858 2859 unsigned RegNum = State.getFirstUnallocated(ArgRegs); 2860 2861 // Skip one register if the first unallocated register has an even register 2862 // number and there are still argument registers available which have not been 2863 // allocated yet. RegNum is actually an index into ArgRegs, which means we 2864 // need to skip a register if RegNum is odd. 2865 if (RegNum != NumArgRegs && RegNum % 2 == 1) { 2866 State.AllocateReg(ArgRegs[RegNum]); 2867 } 2868 2869 // Always return false here, as this function only makes sure that the first 2870 // unallocated register has an odd register number and does not actually 2871 // allocate a register for the current argument. 2872 return false; 2873 } 2874 2875 bool 2876 llvm::CC_PPC32_SVR4_Custom_SkipLastArgRegsPPCF128(unsigned &ValNo, MVT &ValVT, 2877 MVT &LocVT, 2878 CCValAssign::LocInfo &LocInfo, 2879 ISD::ArgFlagsTy &ArgFlags, 2880 CCState &State) { 2881 static const MCPhysReg ArgRegs[] = { 2882 PPC::R3, PPC::R4, PPC::R5, PPC::R6, 2883 PPC::R7, PPC::R8, PPC::R9, PPC::R10, 2884 }; 2885 const unsigned NumArgRegs = array_lengthof(ArgRegs); 2886 2887 unsigned RegNum = State.getFirstUnallocated(ArgRegs); 2888 int RegsLeft = NumArgRegs - RegNum; 2889 2890 // Skip if there is not enough registers left for long double type (4 gpr regs 2891 // in soft float mode) and put long double argument on the stack. 2892 if (RegNum != NumArgRegs && RegsLeft < 4) { 2893 for (int i = 0; i < RegsLeft; i++) { 2894 State.AllocateReg(ArgRegs[RegNum + i]); 2895 } 2896 } 2897 2898 return false; 2899 } 2900 2901 bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT, 2902 MVT &LocVT, 2903 CCValAssign::LocInfo &LocInfo, 2904 ISD::ArgFlagsTy &ArgFlags, 2905 CCState &State) { 2906 static const MCPhysReg ArgRegs[] = { 2907 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, 2908 PPC::F8 2909 }; 2910 2911 const unsigned NumArgRegs = array_lengthof(ArgRegs); 2912 2913 unsigned RegNum = State.getFirstUnallocated(ArgRegs); 2914 2915 // If there is only one Floating-point register left we need to put both f64 2916 // values of a split ppc_fp128 value on the stack. 2917 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) { 2918 State.AllocateReg(ArgRegs[RegNum]); 2919 } 2920 2921 // Always return false here, as this function only makes sure that the two f64 2922 // values a ppc_fp128 value is split into are both passed in registers or both 2923 // passed on the stack and does not actually allocate a register for the 2924 // current argument. 2925 return false; 2926 } 2927 2928 /// FPR - The set of FP registers that should be allocated for arguments, 2929 /// on Darwin. 2930 static const MCPhysReg FPR[] = {PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, 2931 PPC::F6, PPC::F7, PPC::F8, PPC::F9, PPC::F10, 2932 PPC::F11, PPC::F12, PPC::F13}; 2933 2934 /// QFPR - The set of QPX registers that should be allocated for arguments. 2935 static const MCPhysReg QFPR[] = { 2936 PPC::QF1, PPC::QF2, PPC::QF3, PPC::QF4, PPC::QF5, PPC::QF6, PPC::QF7, 2937 PPC::QF8, PPC::QF9, PPC::QF10, PPC::QF11, PPC::QF12, PPC::QF13}; 2938 2939 /// CalculateStackSlotSize - Calculates the size reserved for this argument on 2940 /// the stack. 2941 static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags, 2942 unsigned PtrByteSize) { 2943 unsigned ArgSize = ArgVT.getStoreSize(); 2944 if (Flags.isByVal()) 2945 ArgSize = Flags.getByValSize(); 2946 2947 // Round up to multiples of the pointer size, except for array members, 2948 // which are always packed. 2949 if (!Flags.isInConsecutiveRegs()) 2950 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 2951 2952 return ArgSize; 2953 } 2954 2955 /// CalculateStackSlotAlignment - Calculates the alignment of this argument 2956 /// on the stack. 2957 static unsigned CalculateStackSlotAlignment(EVT ArgVT, EVT OrigVT, 2958 ISD::ArgFlagsTy Flags, 2959 unsigned PtrByteSize) { 2960 unsigned Align = PtrByteSize; 2961 2962 // Altivec parameters are padded to a 16 byte boundary. 2963 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 || 2964 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 || 2965 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 || 2966 ArgVT == MVT::v1i128) 2967 Align = 16; 2968 // QPX vector types stored in double-precision are padded to a 32 byte 2969 // boundary. 2970 else if (ArgVT == MVT::v4f64 || ArgVT == MVT::v4i1) 2971 Align = 32; 2972 2973 // ByVal parameters are aligned as requested. 2974 if (Flags.isByVal()) { 2975 unsigned BVAlign = Flags.getByValAlign(); 2976 if (BVAlign > PtrByteSize) { 2977 if (BVAlign % PtrByteSize != 0) 2978 llvm_unreachable( 2979 "ByVal alignment is not a multiple of the pointer size"); 2980 2981 Align = BVAlign; 2982 } 2983 } 2984 2985 // Array members are always packed to their original alignment. 2986 if (Flags.isInConsecutiveRegs()) { 2987 // If the array member was split into multiple registers, the first 2988 // needs to be aligned to the size of the full type. (Except for 2989 // ppcf128, which is only aligned as its f64 components.) 2990 if (Flags.isSplit() && OrigVT != MVT::ppcf128) 2991 Align = OrigVT.getStoreSize(); 2992 else 2993 Align = ArgVT.getStoreSize(); 2994 } 2995 2996 return Align; 2997 } 2998 2999 /// CalculateStackSlotUsed - Return whether this argument will use its 3000 /// stack slot (instead of being passed in registers). ArgOffset, 3001 /// AvailableFPRs, and AvailableVRs must hold the current argument 3002 /// position, and will be updated to account for this argument. 3003 static bool CalculateStackSlotUsed(EVT ArgVT, EVT OrigVT, 3004 ISD::ArgFlagsTy Flags, 3005 unsigned PtrByteSize, 3006 unsigned LinkageSize, 3007 unsigned ParamAreaSize, 3008 unsigned &ArgOffset, 3009 unsigned &AvailableFPRs, 3010 unsigned &AvailableVRs, bool HasQPX) { 3011 bool UseMemory = false; 3012 3013 // Respect alignment of argument on the stack. 3014 unsigned Align = 3015 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize); 3016 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align; 3017 // If there's no space left in the argument save area, we must 3018 // use memory (this check also catches zero-sized arguments). 3019 if (ArgOffset >= LinkageSize + ParamAreaSize) 3020 UseMemory = true; 3021 3022 // Allocate argument on the stack. 3023 ArgOffset += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize); 3024 if (Flags.isInConsecutiveRegsLast()) 3025 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 3026 // If we overran the argument save area, we must use memory 3027 // (this check catches arguments passed partially in memory) 3028 if (ArgOffset > LinkageSize + ParamAreaSize) 3029 UseMemory = true; 3030 3031 // However, if the argument is actually passed in an FPR or a VR, 3032 // we don't use memory after all. 3033 if (!Flags.isByVal()) { 3034 if (ArgVT == MVT::f32 || ArgVT == MVT::f64 || 3035 // QPX registers overlap with the scalar FP registers. 3036 (HasQPX && (ArgVT == MVT::v4f32 || 3037 ArgVT == MVT::v4f64 || 3038 ArgVT == MVT::v4i1))) 3039 if (AvailableFPRs > 0) { 3040 --AvailableFPRs; 3041 return false; 3042 } 3043 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 || 3044 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 || 3045 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 || 3046 ArgVT == MVT::v1i128) 3047 if (AvailableVRs > 0) { 3048 --AvailableVRs; 3049 return false; 3050 } 3051 } 3052 3053 return UseMemory; 3054 } 3055 3056 /// EnsureStackAlignment - Round stack frame size up from NumBytes to 3057 /// ensure minimum alignment required for target. 3058 static unsigned EnsureStackAlignment(const PPCFrameLowering *Lowering, 3059 unsigned NumBytes) { 3060 unsigned TargetAlign = Lowering->getStackAlignment(); 3061 unsigned AlignMask = TargetAlign - 1; 3062 NumBytes = (NumBytes + AlignMask) & ~AlignMask; 3063 return NumBytes; 3064 } 3065 3066 SDValue PPCTargetLowering::LowerFormalArguments( 3067 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 3068 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 3069 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 3070 if (Subtarget.isSVR4ABI()) { 3071 if (Subtarget.isPPC64()) 3072 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins, 3073 dl, DAG, InVals); 3074 else 3075 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins, 3076 dl, DAG, InVals); 3077 } else { 3078 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins, 3079 dl, DAG, InVals); 3080 } 3081 } 3082 3083 SDValue PPCTargetLowering::LowerFormalArguments_32SVR4( 3084 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 3085 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 3086 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 3087 3088 // 32-bit SVR4 ABI Stack Frame Layout: 3089 // +-----------------------------------+ 3090 // +--> | Back chain | 3091 // | +-----------------------------------+ 3092 // | | Floating-point register save area | 3093 // | +-----------------------------------+ 3094 // | | General register save area | 3095 // | +-----------------------------------+ 3096 // | | CR save word | 3097 // | +-----------------------------------+ 3098 // | | VRSAVE save word | 3099 // | +-----------------------------------+ 3100 // | | Alignment padding | 3101 // | +-----------------------------------+ 3102 // | | Vector register save area | 3103 // | +-----------------------------------+ 3104 // | | Local variable space | 3105 // | +-----------------------------------+ 3106 // | | Parameter list area | 3107 // | +-----------------------------------+ 3108 // | | LR save word | 3109 // | +-----------------------------------+ 3110 // SP--> +--- | Back chain | 3111 // +-----------------------------------+ 3112 // 3113 // Specifications: 3114 // System V Application Binary Interface PowerPC Processor Supplement 3115 // AltiVec Technology Programming Interface Manual 3116 3117 MachineFunction &MF = DAG.getMachineFunction(); 3118 MachineFrameInfo &MFI = MF.getFrameInfo(); 3119 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 3120 3121 EVT PtrVT = getPointerTy(MF.getDataLayout()); 3122 // Potential tail calls could cause overwriting of argument stack slots. 3123 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt && 3124 (CallConv == CallingConv::Fast)); 3125 unsigned PtrByteSize = 4; 3126 3127 // Assign locations to all of the incoming arguments. 3128 SmallVector<CCValAssign, 16> ArgLocs; 3129 PPCCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 3130 *DAG.getContext()); 3131 3132 // Reserve space for the linkage area on the stack. 3133 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); 3134 CCInfo.AllocateStack(LinkageSize, PtrByteSize); 3135 if (useSoftFloat()) 3136 CCInfo.PreAnalyzeFormalArguments(Ins); 3137 3138 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4); 3139 CCInfo.clearWasPPCF128(); 3140 3141 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3142 CCValAssign &VA = ArgLocs[i]; 3143 3144 // Arguments stored in registers. 3145 if (VA.isRegLoc()) { 3146 const TargetRegisterClass *RC; 3147 EVT ValVT = VA.getValVT(); 3148 3149 switch (ValVT.getSimpleVT().SimpleTy) { 3150 default: 3151 llvm_unreachable("ValVT not supported by formal arguments Lowering"); 3152 case MVT::i1: 3153 case MVT::i32: 3154 RC = &PPC::GPRCRegClass; 3155 break; 3156 case MVT::f32: 3157 if (Subtarget.hasP8Vector()) 3158 RC = &PPC::VSSRCRegClass; 3159 else 3160 RC = &PPC::F4RCRegClass; 3161 break; 3162 case MVT::f64: 3163 if (Subtarget.hasVSX()) 3164 RC = &PPC::VSFRCRegClass; 3165 else 3166 RC = &PPC::F8RCRegClass; 3167 break; 3168 case MVT::v16i8: 3169 case MVT::v8i16: 3170 case MVT::v4i32: 3171 RC = &PPC::VRRCRegClass; 3172 break; 3173 case MVT::v4f32: 3174 RC = Subtarget.hasQPX() ? &PPC::QSRCRegClass : &PPC::VRRCRegClass; 3175 break; 3176 case MVT::v2f64: 3177 case MVT::v2i64: 3178 RC = &PPC::VRRCRegClass; 3179 break; 3180 case MVT::v4f64: 3181 RC = &PPC::QFRCRegClass; 3182 break; 3183 case MVT::v4i1: 3184 RC = &PPC::QBRCRegClass; 3185 break; 3186 } 3187 3188 // Transform the arguments stored in physical registers into virtual ones. 3189 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 3190 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, 3191 ValVT == MVT::i1 ? MVT::i32 : ValVT); 3192 3193 if (ValVT == MVT::i1) 3194 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue); 3195 3196 InVals.push_back(ArgValue); 3197 } else { 3198 // Argument stored in memory. 3199 assert(VA.isMemLoc()); 3200 3201 unsigned ArgSize = VA.getLocVT().getStoreSize(); 3202 int FI = MFI.CreateFixedObject(ArgSize, VA.getLocMemOffset(), 3203 isImmutable); 3204 3205 // Create load nodes to retrieve arguments from the stack. 3206 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3207 InVals.push_back( 3208 DAG.getLoad(VA.getValVT(), dl, Chain, FIN, MachinePointerInfo())); 3209 } 3210 } 3211 3212 // Assign locations to all of the incoming aggregate by value arguments. 3213 // Aggregates passed by value are stored in the local variable space of the 3214 // caller's stack frame, right above the parameter list area. 3215 SmallVector<CCValAssign, 16> ByValArgLocs; 3216 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(), 3217 ByValArgLocs, *DAG.getContext()); 3218 3219 // Reserve stack space for the allocations in CCInfo. 3220 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize); 3221 3222 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal); 3223 3224 // Area that is at least reserved in the caller of this function. 3225 unsigned MinReservedArea = CCByValInfo.getNextStackOffset(); 3226 MinReservedArea = std::max(MinReservedArea, LinkageSize); 3227 3228 // Set the size that is at least reserved in caller of this function. Tail 3229 // call optimized function's reserved stack space needs to be aligned so that 3230 // taking the difference between two stack areas will result in an aligned 3231 // stack. 3232 MinReservedArea = 3233 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea); 3234 FuncInfo->setMinReservedArea(MinReservedArea); 3235 3236 SmallVector<SDValue, 8> MemOps; 3237 3238 // If the function takes variable number of arguments, make a frame index for 3239 // the start of the first vararg value... for expansion of llvm.va_start. 3240 if (isVarArg) { 3241 static const MCPhysReg GPArgRegs[] = { 3242 PPC::R3, PPC::R4, PPC::R5, PPC::R6, 3243 PPC::R7, PPC::R8, PPC::R9, PPC::R10, 3244 }; 3245 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs); 3246 3247 static const MCPhysReg FPArgRegs[] = { 3248 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, 3249 PPC::F8 3250 }; 3251 unsigned NumFPArgRegs = array_lengthof(FPArgRegs); 3252 3253 if (useSoftFloat()) 3254 NumFPArgRegs = 0; 3255 3256 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs)); 3257 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs)); 3258 3259 // Make room for NumGPArgRegs and NumFPArgRegs. 3260 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 + 3261 NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8; 3262 3263 FuncInfo->setVarArgsStackOffset( 3264 MFI.CreateFixedObject(PtrVT.getSizeInBits()/8, 3265 CCInfo.getNextStackOffset(), true)); 3266 3267 FuncInfo->setVarArgsFrameIndex(MFI.CreateStackObject(Depth, 8, false)); 3268 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 3269 3270 // The fixed integer arguments of a variadic function are stored to the 3271 // VarArgsFrameIndex on the stack so that they may be loaded by 3272 // dereferencing the result of va_next. 3273 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) { 3274 // Get an existing live-in vreg, or add a new one. 3275 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]); 3276 if (!VReg) 3277 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass); 3278 3279 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 3280 SDValue Store = 3281 DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo()); 3282 MemOps.push_back(Store); 3283 // Increment the address by four for the next argument to store 3284 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT); 3285 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); 3286 } 3287 3288 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6 3289 // is set. 3290 // The double arguments are stored to the VarArgsFrameIndex 3291 // on the stack. 3292 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) { 3293 // Get an existing live-in vreg, or add a new one. 3294 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]); 3295 if (!VReg) 3296 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass); 3297 3298 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64); 3299 SDValue Store = 3300 DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo()); 3301 MemOps.push_back(Store); 3302 // Increment the address by eight for the next argument to store 3303 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8, dl, 3304 PtrVT); 3305 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); 3306 } 3307 } 3308 3309 if (!MemOps.empty()) 3310 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 3311 3312 return Chain; 3313 } 3314 3315 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote 3316 // value to MVT::i64 and then truncate to the correct register size. 3317 SDValue PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, 3318 EVT ObjectVT, SelectionDAG &DAG, 3319 SDValue ArgVal, 3320 const SDLoc &dl) const { 3321 if (Flags.isSExt()) 3322 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal, 3323 DAG.getValueType(ObjectVT)); 3324 else if (Flags.isZExt()) 3325 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal, 3326 DAG.getValueType(ObjectVT)); 3327 3328 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal); 3329 } 3330 3331 SDValue PPCTargetLowering::LowerFormalArguments_64SVR4( 3332 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 3333 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 3334 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 3335 // TODO: add description of PPC stack frame format, or at least some docs. 3336 // 3337 bool isELFv2ABI = Subtarget.isELFv2ABI(); 3338 bool isLittleEndian = Subtarget.isLittleEndian(); 3339 MachineFunction &MF = DAG.getMachineFunction(); 3340 MachineFrameInfo &MFI = MF.getFrameInfo(); 3341 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 3342 3343 assert(!(CallConv == CallingConv::Fast && isVarArg) && 3344 "fastcc not supported on varargs functions"); 3345 3346 EVT PtrVT = getPointerTy(MF.getDataLayout()); 3347 // Potential tail calls could cause overwriting of argument stack slots. 3348 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt && 3349 (CallConv == CallingConv::Fast)); 3350 unsigned PtrByteSize = 8; 3351 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); 3352 3353 static const MCPhysReg GPR[] = { 3354 PPC::X3, PPC::X4, PPC::X5, PPC::X6, 3355 PPC::X7, PPC::X8, PPC::X9, PPC::X10, 3356 }; 3357 static const MCPhysReg VR[] = { 3358 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, 3359 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 3360 }; 3361 3362 const unsigned Num_GPR_Regs = array_lengthof(GPR); 3363 const unsigned Num_FPR_Regs = useSoftFloat() ? 0 : 13; 3364 const unsigned Num_VR_Regs = array_lengthof(VR); 3365 const unsigned Num_QFPR_Regs = Num_FPR_Regs; 3366 3367 // Do a first pass over the arguments to determine whether the ABI 3368 // guarantees that our caller has allocated the parameter save area 3369 // on its stack frame. In the ELFv1 ABI, this is always the case; 3370 // in the ELFv2 ABI, it is true if this is a vararg function or if 3371 // any parameter is located in a stack slot. 3372 3373 bool HasParameterArea = !isELFv2ABI || isVarArg; 3374 unsigned ParamAreaSize = Num_GPR_Regs * PtrByteSize; 3375 unsigned NumBytes = LinkageSize; 3376 unsigned AvailableFPRs = Num_FPR_Regs; 3377 unsigned AvailableVRs = Num_VR_Regs; 3378 for (unsigned i = 0, e = Ins.size(); i != e; ++i) { 3379 if (Ins[i].Flags.isNest()) 3380 continue; 3381 3382 if (CalculateStackSlotUsed(Ins[i].VT, Ins[i].ArgVT, Ins[i].Flags, 3383 PtrByteSize, LinkageSize, ParamAreaSize, 3384 NumBytes, AvailableFPRs, AvailableVRs, 3385 Subtarget.hasQPX())) 3386 HasParameterArea = true; 3387 } 3388 3389 // Add DAG nodes to load the arguments or copy them out of registers. On 3390 // entry to a function on PPC, the arguments start after the linkage area, 3391 // although the first ones are often in registers. 3392 3393 unsigned ArgOffset = LinkageSize; 3394 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; 3395 unsigned &QFPR_idx = FPR_idx; 3396 SmallVector<SDValue, 8> MemOps; 3397 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin(); 3398 unsigned CurArgIdx = 0; 3399 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) { 3400 SDValue ArgVal; 3401 bool needsLoad = false; 3402 EVT ObjectVT = Ins[ArgNo].VT; 3403 EVT OrigVT = Ins[ArgNo].ArgVT; 3404 unsigned ObjSize = ObjectVT.getStoreSize(); 3405 unsigned ArgSize = ObjSize; 3406 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags; 3407 if (Ins[ArgNo].isOrigArg()) { 3408 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx); 3409 CurArgIdx = Ins[ArgNo].getOrigArgIndex(); 3410 } 3411 // We re-align the argument offset for each argument, except when using the 3412 // fast calling convention, when we need to make sure we do that only when 3413 // we'll actually use a stack slot. 3414 unsigned CurArgOffset, Align; 3415 auto ComputeArgOffset = [&]() { 3416 /* Respect alignment of argument on the stack. */ 3417 Align = CalculateStackSlotAlignment(ObjectVT, OrigVT, Flags, PtrByteSize); 3418 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align; 3419 CurArgOffset = ArgOffset; 3420 }; 3421 3422 if (CallConv != CallingConv::Fast) { 3423 ComputeArgOffset(); 3424 3425 /* Compute GPR index associated with argument offset. */ 3426 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize; 3427 GPR_idx = std::min(GPR_idx, Num_GPR_Regs); 3428 } 3429 3430 // FIXME the codegen can be much improved in some cases. 3431 // We do not have to keep everything in memory. 3432 if (Flags.isByVal()) { 3433 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit"); 3434 3435 if (CallConv == CallingConv::Fast) 3436 ComputeArgOffset(); 3437 3438 // ObjSize is the true size, ArgSize rounded up to multiple of registers. 3439 ObjSize = Flags.getByValSize(); 3440 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 3441 // Empty aggregate parameters do not take up registers. Examples: 3442 // struct { } a; 3443 // union { } b; 3444 // int c[0]; 3445 // etc. However, we have to provide a place-holder in InVals, so 3446 // pretend we have an 8-byte item at the current address for that 3447 // purpose. 3448 if (!ObjSize) { 3449 int FI = MFI.CreateFixedObject(PtrByteSize, ArgOffset, true); 3450 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3451 InVals.push_back(FIN); 3452 continue; 3453 } 3454 3455 // Create a stack object covering all stack doublewords occupied 3456 // by the argument. If the argument is (fully or partially) on 3457 // the stack, or if the argument is fully in registers but the 3458 // caller has allocated the parameter save anyway, we can refer 3459 // directly to the caller's stack frame. Otherwise, create a 3460 // local copy in our own frame. 3461 int FI; 3462 if (HasParameterArea || 3463 ArgSize + ArgOffset > LinkageSize + Num_GPR_Regs * PtrByteSize) 3464 FI = MFI.CreateFixedObject(ArgSize, ArgOffset, false, true); 3465 else 3466 FI = MFI.CreateStackObject(ArgSize, Align, false); 3467 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3468 3469 // Handle aggregates smaller than 8 bytes. 3470 if (ObjSize < PtrByteSize) { 3471 // The value of the object is its address, which differs from the 3472 // address of the enclosing doubleword on big-endian systems. 3473 SDValue Arg = FIN; 3474 if (!isLittleEndian) { 3475 SDValue ArgOff = DAG.getConstant(PtrByteSize - ObjSize, dl, PtrVT); 3476 Arg = DAG.getNode(ISD::ADD, dl, ArgOff.getValueType(), Arg, ArgOff); 3477 } 3478 InVals.push_back(Arg); 3479 3480 if (GPR_idx != Num_GPR_Regs) { 3481 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass); 3482 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 3483 SDValue Store; 3484 3485 if (ObjSize==1 || ObjSize==2 || ObjSize==4) { 3486 EVT ObjType = (ObjSize == 1 ? MVT::i8 : 3487 (ObjSize == 2 ? MVT::i16 : MVT::i32)); 3488 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, Arg, 3489 MachinePointerInfo(&*FuncArg), ObjType); 3490 } else { 3491 // For sizes that don't fit a truncating store (3, 5, 6, 7), 3492 // store the whole register as-is to the parameter save area 3493 // slot. 3494 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, 3495 MachinePointerInfo(&*FuncArg)); 3496 } 3497 3498 MemOps.push_back(Store); 3499 } 3500 // Whether we copied from a register or not, advance the offset 3501 // into the parameter save area by a full doubleword. 3502 ArgOffset += PtrByteSize; 3503 continue; 3504 } 3505 3506 // The value of the object is its address, which is the address of 3507 // its first stack doubleword. 3508 InVals.push_back(FIN); 3509 3510 // Store whatever pieces of the object are in registers to memory. 3511 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) { 3512 if (GPR_idx == Num_GPR_Regs) 3513 break; 3514 3515 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); 3516 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 3517 SDValue Addr = FIN; 3518 if (j) { 3519 SDValue Off = DAG.getConstant(j, dl, PtrVT); 3520 Addr = DAG.getNode(ISD::ADD, dl, Off.getValueType(), Addr, Off); 3521 } 3522 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, Addr, 3523 MachinePointerInfo(&*FuncArg, j)); 3524 MemOps.push_back(Store); 3525 ++GPR_idx; 3526 } 3527 ArgOffset += ArgSize; 3528 continue; 3529 } 3530 3531 switch (ObjectVT.getSimpleVT().SimpleTy) { 3532 default: llvm_unreachable("Unhandled argument type!"); 3533 case MVT::i1: 3534 case MVT::i32: 3535 case MVT::i64: 3536 if (Flags.isNest()) { 3537 // The 'nest' parameter, if any, is passed in R11. 3538 unsigned VReg = MF.addLiveIn(PPC::X11, &PPC::G8RCRegClass); 3539 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); 3540 3541 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1) 3542 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl); 3543 3544 break; 3545 } 3546 3547 // These can be scalar arguments or elements of an integer array type 3548 // passed directly. Clang may use those instead of "byval" aggregate 3549 // types to avoid forcing arguments to memory unnecessarily. 3550 if (GPR_idx != Num_GPR_Regs) { 3551 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass); 3552 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); 3553 3554 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1) 3555 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote 3556 // value to MVT::i64 and then truncate to the correct register size. 3557 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl); 3558 } else { 3559 if (CallConv == CallingConv::Fast) 3560 ComputeArgOffset(); 3561 3562 needsLoad = true; 3563 ArgSize = PtrByteSize; 3564 } 3565 if (CallConv != CallingConv::Fast || needsLoad) 3566 ArgOffset += 8; 3567 break; 3568 3569 case MVT::f32: 3570 case MVT::f64: 3571 // These can be scalar arguments or elements of a float array type 3572 // passed directly. The latter are used to implement ELFv2 homogenous 3573 // float aggregates. 3574 if (FPR_idx != Num_FPR_Regs) { 3575 unsigned VReg; 3576 3577 if (ObjectVT == MVT::f32) 3578 VReg = MF.addLiveIn(FPR[FPR_idx], 3579 Subtarget.hasP8Vector() 3580 ? &PPC::VSSRCRegClass 3581 : &PPC::F4RCRegClass); 3582 else 3583 VReg = MF.addLiveIn(FPR[FPR_idx], Subtarget.hasVSX() 3584 ? &PPC::VSFRCRegClass 3585 : &PPC::F8RCRegClass); 3586 3587 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); 3588 ++FPR_idx; 3589 } else if (GPR_idx != Num_GPR_Regs && CallConv != CallingConv::Fast) { 3590 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8 3591 // once we support fp <-> gpr moves. 3592 3593 // This can only ever happen in the presence of f32 array types, 3594 // since otherwise we never run out of FPRs before running out 3595 // of GPRs. 3596 unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass); 3597 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); 3598 3599 if (ObjectVT == MVT::f32) { 3600 if ((ArgOffset % PtrByteSize) == (isLittleEndian ? 4 : 0)) 3601 ArgVal = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgVal, 3602 DAG.getConstant(32, dl, MVT::i32)); 3603 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal); 3604 } 3605 3606 ArgVal = DAG.getNode(ISD::BITCAST, dl, ObjectVT, ArgVal); 3607 } else { 3608 if (CallConv == CallingConv::Fast) 3609 ComputeArgOffset(); 3610 3611 needsLoad = true; 3612 } 3613 3614 // When passing an array of floats, the array occupies consecutive 3615 // space in the argument area; only round up to the next doubleword 3616 // at the end of the array. Otherwise, each float takes 8 bytes. 3617 if (CallConv != CallingConv::Fast || needsLoad) { 3618 ArgSize = Flags.isInConsecutiveRegs() ? ObjSize : PtrByteSize; 3619 ArgOffset += ArgSize; 3620 if (Flags.isInConsecutiveRegsLast()) 3621 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 3622 } 3623 break; 3624 case MVT::v4f32: 3625 case MVT::v4i32: 3626 case MVT::v8i16: 3627 case MVT::v16i8: 3628 case MVT::v2f64: 3629 case MVT::v2i64: 3630 case MVT::v1i128: 3631 if (!Subtarget.hasQPX()) { 3632 // These can be scalar arguments or elements of a vector array type 3633 // passed directly. The latter are used to implement ELFv2 homogenous 3634 // vector aggregates. 3635 if (VR_idx != Num_VR_Regs) { 3636 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass); 3637 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); 3638 ++VR_idx; 3639 } else { 3640 if (CallConv == CallingConv::Fast) 3641 ComputeArgOffset(); 3642 3643 needsLoad = true; 3644 } 3645 if (CallConv != CallingConv::Fast || needsLoad) 3646 ArgOffset += 16; 3647 break; 3648 } // not QPX 3649 3650 assert(ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 && 3651 "Invalid QPX parameter type"); 3652 /* fall through */ 3653 3654 case MVT::v4f64: 3655 case MVT::v4i1: 3656 // QPX vectors are treated like their scalar floating-point subregisters 3657 // (except that they're larger). 3658 unsigned Sz = ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 ? 16 : 32; 3659 if (QFPR_idx != Num_QFPR_Regs) { 3660 const TargetRegisterClass *RC; 3661 switch (ObjectVT.getSimpleVT().SimpleTy) { 3662 case MVT::v4f64: RC = &PPC::QFRCRegClass; break; 3663 case MVT::v4f32: RC = &PPC::QSRCRegClass; break; 3664 default: RC = &PPC::QBRCRegClass; break; 3665 } 3666 3667 unsigned VReg = MF.addLiveIn(QFPR[QFPR_idx], RC); 3668 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); 3669 ++QFPR_idx; 3670 } else { 3671 if (CallConv == CallingConv::Fast) 3672 ComputeArgOffset(); 3673 needsLoad = true; 3674 } 3675 if (CallConv != CallingConv::Fast || needsLoad) 3676 ArgOffset += Sz; 3677 break; 3678 } 3679 3680 // We need to load the argument to a virtual register if we determined 3681 // above that we ran out of physical registers of the appropriate type. 3682 if (needsLoad) { 3683 if (ObjSize < ArgSize && !isLittleEndian) 3684 CurArgOffset += ArgSize - ObjSize; 3685 int FI = MFI.CreateFixedObject(ObjSize, CurArgOffset, isImmutable); 3686 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3687 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo()); 3688 } 3689 3690 InVals.push_back(ArgVal); 3691 } 3692 3693 // Area that is at least reserved in the caller of this function. 3694 unsigned MinReservedArea; 3695 if (HasParameterArea) 3696 MinReservedArea = std::max(ArgOffset, LinkageSize + 8 * PtrByteSize); 3697 else 3698 MinReservedArea = LinkageSize; 3699 3700 // Set the size that is at least reserved in caller of this function. Tail 3701 // call optimized functions' reserved stack space needs to be aligned so that 3702 // taking the difference between two stack areas will result in an aligned 3703 // stack. 3704 MinReservedArea = 3705 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea); 3706 FuncInfo->setMinReservedArea(MinReservedArea); 3707 3708 // If the function takes variable number of arguments, make a frame index for 3709 // the start of the first vararg value... for expansion of llvm.va_start. 3710 if (isVarArg) { 3711 int Depth = ArgOffset; 3712 3713 FuncInfo->setVarArgsFrameIndex( 3714 MFI.CreateFixedObject(PtrByteSize, Depth, true)); 3715 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 3716 3717 // If this function is vararg, store any remaining integer argument regs 3718 // to their spots on the stack so that they may be loaded by dereferencing 3719 // the result of va_next. 3720 for (GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize; 3721 GPR_idx < Num_GPR_Regs; ++GPR_idx) { 3722 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); 3723 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 3724 SDValue Store = 3725 DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo()); 3726 MemOps.push_back(Store); 3727 // Increment the address by four for the next argument to store 3728 SDValue PtrOff = DAG.getConstant(PtrByteSize, dl, PtrVT); 3729 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); 3730 } 3731 } 3732 3733 if (!MemOps.empty()) 3734 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 3735 3736 return Chain; 3737 } 3738 3739 SDValue PPCTargetLowering::LowerFormalArguments_Darwin( 3740 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 3741 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 3742 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 3743 // TODO: add description of PPC stack frame format, or at least some docs. 3744 // 3745 MachineFunction &MF = DAG.getMachineFunction(); 3746 MachineFrameInfo &MFI = MF.getFrameInfo(); 3747 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 3748 3749 EVT PtrVT = getPointerTy(MF.getDataLayout()); 3750 bool isPPC64 = PtrVT == MVT::i64; 3751 // Potential tail calls could cause overwriting of argument stack slots. 3752 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt && 3753 (CallConv == CallingConv::Fast)); 3754 unsigned PtrByteSize = isPPC64 ? 8 : 4; 3755 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); 3756 unsigned ArgOffset = LinkageSize; 3757 // Area that is at least reserved in caller of this function. 3758 unsigned MinReservedArea = ArgOffset; 3759 3760 static const MCPhysReg GPR_32[] = { // 32-bit registers. 3761 PPC::R3, PPC::R4, PPC::R5, PPC::R6, 3762 PPC::R7, PPC::R8, PPC::R9, PPC::R10, 3763 }; 3764 static const MCPhysReg GPR_64[] = { // 64-bit registers. 3765 PPC::X3, PPC::X4, PPC::X5, PPC::X6, 3766 PPC::X7, PPC::X8, PPC::X9, PPC::X10, 3767 }; 3768 static const MCPhysReg VR[] = { 3769 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, 3770 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 3771 }; 3772 3773 const unsigned Num_GPR_Regs = array_lengthof(GPR_32); 3774 const unsigned Num_FPR_Regs = useSoftFloat() ? 0 : 13; 3775 const unsigned Num_VR_Regs = array_lengthof( VR); 3776 3777 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; 3778 3779 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32; 3780 3781 // In 32-bit non-varargs functions, the stack space for vectors is after the 3782 // stack space for non-vectors. We do not use this space unless we have 3783 // too many vectors to fit in registers, something that only occurs in 3784 // constructed examples:), but we have to walk the arglist to figure 3785 // that out...for the pathological case, compute VecArgOffset as the 3786 // start of the vector parameter area. Computing VecArgOffset is the 3787 // entire point of the following loop. 3788 unsigned VecArgOffset = ArgOffset; 3789 if (!isVarArg && !isPPC64) { 3790 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; 3791 ++ArgNo) { 3792 EVT ObjectVT = Ins[ArgNo].VT; 3793 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags; 3794 3795 if (Flags.isByVal()) { 3796 // ObjSize is the true size, ArgSize rounded up to multiple of regs. 3797 unsigned ObjSize = Flags.getByValSize(); 3798 unsigned ArgSize = 3799 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 3800 VecArgOffset += ArgSize; 3801 continue; 3802 } 3803 3804 switch(ObjectVT.getSimpleVT().SimpleTy) { 3805 default: llvm_unreachable("Unhandled argument type!"); 3806 case MVT::i1: 3807 case MVT::i32: 3808 case MVT::f32: 3809 VecArgOffset += 4; 3810 break; 3811 case MVT::i64: // PPC64 3812 case MVT::f64: 3813 // FIXME: We are guaranteed to be !isPPC64 at this point. 3814 // Does MVT::i64 apply? 3815 VecArgOffset += 8; 3816 break; 3817 case MVT::v4f32: 3818 case MVT::v4i32: 3819 case MVT::v8i16: 3820 case MVT::v16i8: 3821 // Nothing to do, we're only looking at Nonvector args here. 3822 break; 3823 } 3824 } 3825 } 3826 // We've found where the vector parameter area in memory is. Skip the 3827 // first 12 parameters; these don't use that memory. 3828 VecArgOffset = ((VecArgOffset+15)/16)*16; 3829 VecArgOffset += 12*16; 3830 3831 // Add DAG nodes to load the arguments or copy them out of registers. On 3832 // entry to a function on PPC, the arguments start after the linkage area, 3833 // although the first ones are often in registers. 3834 3835 SmallVector<SDValue, 8> MemOps; 3836 unsigned nAltivecParamsAtEnd = 0; 3837 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin(); 3838 unsigned CurArgIdx = 0; 3839 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) { 3840 SDValue ArgVal; 3841 bool needsLoad = false; 3842 EVT ObjectVT = Ins[ArgNo].VT; 3843 unsigned ObjSize = ObjectVT.getSizeInBits()/8; 3844 unsigned ArgSize = ObjSize; 3845 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags; 3846 if (Ins[ArgNo].isOrigArg()) { 3847 std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx); 3848 CurArgIdx = Ins[ArgNo].getOrigArgIndex(); 3849 } 3850 unsigned CurArgOffset = ArgOffset; 3851 3852 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary. 3853 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 || 3854 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) { 3855 if (isVarArg || isPPC64) { 3856 MinReservedArea = ((MinReservedArea+15)/16)*16; 3857 MinReservedArea += CalculateStackSlotSize(ObjectVT, 3858 Flags, 3859 PtrByteSize); 3860 } else nAltivecParamsAtEnd++; 3861 } else 3862 // Calculate min reserved area. 3863 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT, 3864 Flags, 3865 PtrByteSize); 3866 3867 // FIXME the codegen can be much improved in some cases. 3868 // We do not have to keep everything in memory. 3869 if (Flags.isByVal()) { 3870 assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit"); 3871 3872 // ObjSize is the true size, ArgSize rounded up to multiple of registers. 3873 ObjSize = Flags.getByValSize(); 3874 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 3875 // Objects of size 1 and 2 are right justified, everything else is 3876 // left justified. This means the memory address is adjusted forwards. 3877 if (ObjSize==1 || ObjSize==2) { 3878 CurArgOffset = CurArgOffset + (4 - ObjSize); 3879 } 3880 // The value of the object is its address. 3881 int FI = MFI.CreateFixedObject(ObjSize, CurArgOffset, false, true); 3882 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3883 InVals.push_back(FIN); 3884 if (ObjSize==1 || ObjSize==2) { 3885 if (GPR_idx != Num_GPR_Regs) { 3886 unsigned VReg; 3887 if (isPPC64) 3888 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); 3889 else 3890 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); 3891 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 3892 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16; 3893 SDValue Store = 3894 DAG.getTruncStore(Val.getValue(1), dl, Val, FIN, 3895 MachinePointerInfo(&*FuncArg), ObjType); 3896 MemOps.push_back(Store); 3897 ++GPR_idx; 3898 } 3899 3900 ArgOffset += PtrByteSize; 3901 3902 continue; 3903 } 3904 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) { 3905 // Store whatever pieces of the object are in registers 3906 // to memory. ArgOffset will be the address of the beginning 3907 // of the object. 3908 if (GPR_idx != Num_GPR_Regs) { 3909 unsigned VReg; 3910 if (isPPC64) 3911 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); 3912 else 3913 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); 3914 int FI = MFI.CreateFixedObject(PtrByteSize, ArgOffset, true); 3915 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3916 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 3917 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, 3918 MachinePointerInfo(&*FuncArg, j)); 3919 MemOps.push_back(Store); 3920 ++GPR_idx; 3921 ArgOffset += PtrByteSize; 3922 } else { 3923 ArgOffset += ArgSize - (ArgOffset-CurArgOffset); 3924 break; 3925 } 3926 } 3927 continue; 3928 } 3929 3930 switch (ObjectVT.getSimpleVT().SimpleTy) { 3931 default: llvm_unreachable("Unhandled argument type!"); 3932 case MVT::i1: 3933 case MVT::i32: 3934 if (!isPPC64) { 3935 if (GPR_idx != Num_GPR_Regs) { 3936 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); 3937 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 3938 3939 if (ObjectVT == MVT::i1) 3940 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal); 3941 3942 ++GPR_idx; 3943 } else { 3944 needsLoad = true; 3945 ArgSize = PtrByteSize; 3946 } 3947 // All int arguments reserve stack space in the Darwin ABI. 3948 ArgOffset += PtrByteSize; 3949 break; 3950 } 3951 LLVM_FALLTHROUGH; 3952 case MVT::i64: // PPC64 3953 if (GPR_idx != Num_GPR_Regs) { 3954 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); 3955 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); 3956 3957 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1) 3958 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote 3959 // value to MVT::i64 and then truncate to the correct register size. 3960 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl); 3961 3962 ++GPR_idx; 3963 } else { 3964 needsLoad = true; 3965 ArgSize = PtrByteSize; 3966 } 3967 // All int arguments reserve stack space in the Darwin ABI. 3968 ArgOffset += 8; 3969 break; 3970 3971 case MVT::f32: 3972 case MVT::f64: 3973 // Every 4 bytes of argument space consumes one of the GPRs available for 3974 // argument passing. 3975 if (GPR_idx != Num_GPR_Regs) { 3976 ++GPR_idx; 3977 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64) 3978 ++GPR_idx; 3979 } 3980 if (FPR_idx != Num_FPR_Regs) { 3981 unsigned VReg; 3982 3983 if (ObjectVT == MVT::f32) 3984 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass); 3985 else 3986 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass); 3987 3988 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); 3989 ++FPR_idx; 3990 } else { 3991 needsLoad = true; 3992 } 3993 3994 // All FP arguments reserve stack space in the Darwin ABI. 3995 ArgOffset += isPPC64 ? 8 : ObjSize; 3996 break; 3997 case MVT::v4f32: 3998 case MVT::v4i32: 3999 case MVT::v8i16: 4000 case MVT::v16i8: 4001 // Note that vector arguments in registers don't reserve stack space, 4002 // except in varargs functions. 4003 if (VR_idx != Num_VR_Regs) { 4004 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass); 4005 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); 4006 if (isVarArg) { 4007 while ((ArgOffset % 16) != 0) { 4008 ArgOffset += PtrByteSize; 4009 if (GPR_idx != Num_GPR_Regs) 4010 GPR_idx++; 4011 } 4012 ArgOffset += 16; 4013 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64? 4014 } 4015 ++VR_idx; 4016 } else { 4017 if (!isVarArg && !isPPC64) { 4018 // Vectors go after all the nonvectors. 4019 CurArgOffset = VecArgOffset; 4020 VecArgOffset += 16; 4021 } else { 4022 // Vectors are aligned. 4023 ArgOffset = ((ArgOffset+15)/16)*16; 4024 CurArgOffset = ArgOffset; 4025 ArgOffset += 16; 4026 } 4027 needsLoad = true; 4028 } 4029 break; 4030 } 4031 4032 // We need to load the argument to a virtual register if we determined above 4033 // that we ran out of physical registers of the appropriate type. 4034 if (needsLoad) { 4035 int FI = MFI.CreateFixedObject(ObjSize, 4036 CurArgOffset + (ArgSize - ObjSize), 4037 isImmutable); 4038 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 4039 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo()); 4040 } 4041 4042 InVals.push_back(ArgVal); 4043 } 4044 4045 // Allow for Altivec parameters at the end, if needed. 4046 if (nAltivecParamsAtEnd) { 4047 MinReservedArea = ((MinReservedArea+15)/16)*16; 4048 MinReservedArea += 16*nAltivecParamsAtEnd; 4049 } 4050 4051 // Area that is at least reserved in the caller of this function. 4052 MinReservedArea = std::max(MinReservedArea, LinkageSize + 8 * PtrByteSize); 4053 4054 // Set the size that is at least reserved in caller of this function. Tail 4055 // call optimized functions' reserved stack space needs to be aligned so that 4056 // taking the difference between two stack areas will result in an aligned 4057 // stack. 4058 MinReservedArea = 4059 EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea); 4060 FuncInfo->setMinReservedArea(MinReservedArea); 4061 4062 // If the function takes variable number of arguments, make a frame index for 4063 // the start of the first vararg value... for expansion of llvm.va_start. 4064 if (isVarArg) { 4065 int Depth = ArgOffset; 4066 4067 FuncInfo->setVarArgsFrameIndex( 4068 MFI.CreateFixedObject(PtrVT.getSizeInBits()/8, 4069 Depth, true)); 4070 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 4071 4072 // If this function is vararg, store any remaining integer argument regs 4073 // to their spots on the stack so that they may be loaded by dereferencing 4074 // the result of va_next. 4075 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) { 4076 unsigned VReg; 4077 4078 if (isPPC64) 4079 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); 4080 else 4081 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); 4082 4083 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); 4084 SDValue Store = 4085 DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo()); 4086 MemOps.push_back(Store); 4087 // Increment the address by four for the next argument to store 4088 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT); 4089 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); 4090 } 4091 } 4092 4093 if (!MemOps.empty()) 4094 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 4095 4096 return Chain; 4097 } 4098 4099 /// CalculateTailCallSPDiff - Get the amount the stack pointer has to be 4100 /// adjusted to accommodate the arguments for the tailcall. 4101 static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall, 4102 unsigned ParamSize) { 4103 4104 if (!isTailCall) return 0; 4105 4106 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>(); 4107 unsigned CallerMinReservedArea = FI->getMinReservedArea(); 4108 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize; 4109 // Remember only if the new adjustement is bigger. 4110 if (SPDiff < FI->getTailCallSPDelta()) 4111 FI->setTailCallSPDelta(SPDiff); 4112 4113 return SPDiff; 4114 } 4115 4116 static bool isFunctionGlobalAddress(SDValue Callee); 4117 4118 static bool 4119 resideInSameSection(const Function *Caller, SDValue Callee, 4120 const TargetMachine &TM) { 4121 // If !G, Callee can be an external symbol. 4122 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee); 4123 if (!G) 4124 return false; 4125 4126 const GlobalValue *GV = G->getGlobal(); 4127 if (!GV->isStrongDefinitionForLinker()) 4128 return false; 4129 4130 // Any explicitly-specified sections and section prefixes must also match. 4131 // Also, if we're using -ffunction-sections, then each function is always in 4132 // a different section (the same is true for COMDAT functions). 4133 if (TM.getFunctionSections() || GV->hasComdat() || Caller->hasComdat() || 4134 GV->getSection() != Caller->getSection()) 4135 return false; 4136 if (const auto *F = dyn_cast<Function>(GV)) { 4137 if (F->getSectionPrefix() != Caller->getSectionPrefix()) 4138 return false; 4139 } 4140 4141 // If the callee might be interposed, then we can't assume the ultimate call 4142 // target will be in the same section. Even in cases where we can assume that 4143 // interposition won't happen, in any case where the linker might insert a 4144 // stub to allow for interposition, we must generate code as though 4145 // interposition might occur. To understand why this matters, consider a 4146 // situation where: a -> b -> c where the arrows indicate calls. b and c are 4147 // in the same section, but a is in a different module (i.e. has a different 4148 // TOC base pointer). If the linker allows for interposition between b and c, 4149 // then it will generate a stub for the call edge between b and c which will 4150 // save the TOC pointer into the designated stack slot allocated by b. If we 4151 // return true here, and therefore allow a tail call between b and c, that 4152 // stack slot won't exist and the b -> c stub will end up saving b'c TOC base 4153 // pointer into the stack slot allocated by a (where the a -> b stub saved 4154 // a's TOC base pointer). If we're not considering a tail call, but rather, 4155 // whether a nop is needed after the call instruction in b, because the linker 4156 // will insert a stub, it might complain about a missing nop if we omit it 4157 // (although many don't complain in this case). 4158 if (!TM.shouldAssumeDSOLocal(*Caller->getParent(), GV)) 4159 return false; 4160 4161 return true; 4162 } 4163 4164 static bool 4165 needStackSlotPassParameters(const PPCSubtarget &Subtarget, 4166 const SmallVectorImpl<ISD::OutputArg> &Outs) { 4167 assert(Subtarget.isSVR4ABI() && Subtarget.isPPC64()); 4168 4169 const unsigned PtrByteSize = 8; 4170 const unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); 4171 4172 static const MCPhysReg GPR[] = { 4173 PPC::X3, PPC::X4, PPC::X5, PPC::X6, 4174 PPC::X7, PPC::X8, PPC::X9, PPC::X10, 4175 }; 4176 static const MCPhysReg VR[] = { 4177 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, 4178 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 4179 }; 4180 4181 const unsigned NumGPRs = array_lengthof(GPR); 4182 const unsigned NumFPRs = 13; 4183 const unsigned NumVRs = array_lengthof(VR); 4184 const unsigned ParamAreaSize = NumGPRs * PtrByteSize; 4185 4186 unsigned NumBytes = LinkageSize; 4187 unsigned AvailableFPRs = NumFPRs; 4188 unsigned AvailableVRs = NumVRs; 4189 4190 for (const ISD::OutputArg& Param : Outs) { 4191 if (Param.Flags.isNest()) continue; 4192 4193 if (CalculateStackSlotUsed(Param.VT, Param.ArgVT, Param.Flags, 4194 PtrByteSize, LinkageSize, ParamAreaSize, 4195 NumBytes, AvailableFPRs, AvailableVRs, 4196 Subtarget.hasQPX())) 4197 return true; 4198 } 4199 return false; 4200 } 4201 4202 static bool 4203 hasSameArgumentList(const Function *CallerFn, ImmutableCallSite *CS) { 4204 if (CS->arg_size() != CallerFn->arg_size()) 4205 return false; 4206 4207 ImmutableCallSite::arg_iterator CalleeArgIter = CS->arg_begin(); 4208 ImmutableCallSite::arg_iterator CalleeArgEnd = CS->arg_end(); 4209 Function::const_arg_iterator CallerArgIter = CallerFn->arg_begin(); 4210 4211 for (; CalleeArgIter != CalleeArgEnd; ++CalleeArgIter, ++CallerArgIter) { 4212 const Value* CalleeArg = *CalleeArgIter; 4213 const Value* CallerArg = &(*CallerArgIter); 4214 if (CalleeArg == CallerArg) 4215 continue; 4216 4217 // e.g. @caller([4 x i64] %a, [4 x i64] %b) { 4218 // tail call @callee([4 x i64] undef, [4 x i64] %b) 4219 // } 4220 // 1st argument of callee is undef and has the same type as caller. 4221 if (CalleeArg->getType() == CallerArg->getType() && 4222 isa<UndefValue>(CalleeArg)) 4223 continue; 4224 4225 return false; 4226 } 4227 4228 return true; 4229 } 4230 4231 bool 4232 PPCTargetLowering::IsEligibleForTailCallOptimization_64SVR4( 4233 SDValue Callee, 4234 CallingConv::ID CalleeCC, 4235 ImmutableCallSite *CS, 4236 bool isVarArg, 4237 const SmallVectorImpl<ISD::OutputArg> &Outs, 4238 const SmallVectorImpl<ISD::InputArg> &Ins, 4239 SelectionDAG& DAG) const { 4240 bool TailCallOpt = getTargetMachine().Options.GuaranteedTailCallOpt; 4241 4242 if (DisableSCO && !TailCallOpt) return false; 4243 4244 // Variadic argument functions are not supported. 4245 if (isVarArg) return false; 4246 4247 MachineFunction &MF = DAG.getMachineFunction(); 4248 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv(); 4249 4250 // Tail or Sibling call optimization (TCO/SCO) needs callee and caller has 4251 // the same calling convention 4252 if (CallerCC != CalleeCC) return false; 4253 4254 // SCO support C calling convention 4255 if (CalleeCC != CallingConv::Fast && CalleeCC != CallingConv::C) 4256 return false; 4257 4258 // Caller contains any byval parameter is not supported. 4259 if (any_of(Ins, [](const ISD::InputArg &IA) { return IA.Flags.isByVal(); })) 4260 return false; 4261 4262 // Callee contains any byval parameter is not supported, too. 4263 // Note: This is a quick work around, because in some cases, e.g. 4264 // caller's stack size > callee's stack size, we are still able to apply 4265 // sibling call optimization. See: https://reviews.llvm.org/D23441#513574 4266 if (any_of(Outs, [](const ISD::OutputArg& OA) { return OA.Flags.isByVal(); })) 4267 return false; 4268 4269 // No TCO/SCO on indirect call because Caller have to restore its TOC 4270 if (!isFunctionGlobalAddress(Callee) && 4271 !isa<ExternalSymbolSDNode>(Callee)) 4272 return false; 4273 4274 // Check if Callee resides in the same section, because for now, PPC64 SVR4 4275 // ABI (ELFv1/ELFv2) doesn't allow tail calls to a symbol resides in another 4276 // section. 4277 // ref: https://bugzilla.mozilla.org/show_bug.cgi?id=973977 4278 if (!resideInSameSection(MF.getFunction(), Callee, getTargetMachine())) 4279 return false; 4280 4281 // TCO allows altering callee ABI, so we don't have to check further. 4282 if (CalleeCC == CallingConv::Fast && TailCallOpt) 4283 return true; 4284 4285 if (DisableSCO) return false; 4286 4287 // If callee use the same argument list that caller is using, then we can 4288 // apply SCO on this case. If it is not, then we need to check if callee needs 4289 // stack for passing arguments. 4290 if (!hasSameArgumentList(MF.getFunction(), CS) && 4291 needStackSlotPassParameters(Subtarget, Outs)) { 4292 return false; 4293 } 4294 4295 return true; 4296 } 4297 4298 /// IsEligibleForTailCallOptimization - Check whether the call is eligible 4299 /// for tail call optimization. Targets which want to do tail call 4300 /// optimization should implement this function. 4301 bool 4302 PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, 4303 CallingConv::ID CalleeCC, 4304 bool isVarArg, 4305 const SmallVectorImpl<ISD::InputArg> &Ins, 4306 SelectionDAG& DAG) const { 4307 if (!getTargetMachine().Options.GuaranteedTailCallOpt) 4308 return false; 4309 4310 // Variable argument functions are not supported. 4311 if (isVarArg) 4312 return false; 4313 4314 MachineFunction &MF = DAG.getMachineFunction(); 4315 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv(); 4316 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) { 4317 // Functions containing by val parameters are not supported. 4318 for (unsigned i = 0; i != Ins.size(); i++) { 4319 ISD::ArgFlagsTy Flags = Ins[i].Flags; 4320 if (Flags.isByVal()) return false; 4321 } 4322 4323 // Non-PIC/GOT tail calls are supported. 4324 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) 4325 return true; 4326 4327 // At the moment we can only do local tail calls (in same module, hidden 4328 // or protected) if we are generating PIC. 4329 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 4330 return G->getGlobal()->hasHiddenVisibility() 4331 || G->getGlobal()->hasProtectedVisibility(); 4332 } 4333 4334 return false; 4335 } 4336 4337 /// isCallCompatibleAddress - Return the immediate to use if the specified 4338 /// 32-bit value is representable in the immediate field of a BxA instruction. 4339 static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) { 4340 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 4341 if (!C) return nullptr; 4342 4343 int Addr = C->getZExtValue(); 4344 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero. 4345 SignExtend32<26>(Addr) != Addr) 4346 return nullptr; // Top 6 bits have to be sext of immediate. 4347 4348 return DAG 4349 .getConstant( 4350 (int)C->getZExtValue() >> 2, SDLoc(Op), 4351 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout())) 4352 .getNode(); 4353 } 4354 4355 namespace { 4356 4357 struct TailCallArgumentInfo { 4358 SDValue Arg; 4359 SDValue FrameIdxOp; 4360 int FrameIdx = 0; 4361 4362 TailCallArgumentInfo() = default; 4363 }; 4364 4365 } // end anonymous namespace 4366 4367 /// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot. 4368 static void StoreTailCallArgumentsToStackSlot( 4369 SelectionDAG &DAG, SDValue Chain, 4370 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs, 4371 SmallVectorImpl<SDValue> &MemOpChains, const SDLoc &dl) { 4372 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) { 4373 SDValue Arg = TailCallArgs[i].Arg; 4374 SDValue FIN = TailCallArgs[i].FrameIdxOp; 4375 int FI = TailCallArgs[i].FrameIdx; 4376 // Store relative to framepointer. 4377 MemOpChains.push_back(DAG.getStore( 4378 Chain, dl, Arg, FIN, 4379 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI))); 4380 } 4381 } 4382 4383 /// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to 4384 /// the appropriate stack slot for the tail call optimized function call. 4385 static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG, SDValue Chain, 4386 SDValue OldRetAddr, SDValue OldFP, 4387 int SPDiff, const SDLoc &dl) { 4388 if (SPDiff) { 4389 // Calculate the new stack slot for the return address. 4390 MachineFunction &MF = DAG.getMachineFunction(); 4391 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 4392 const PPCFrameLowering *FL = Subtarget.getFrameLowering(); 4393 bool isPPC64 = Subtarget.isPPC64(); 4394 int SlotSize = isPPC64 ? 8 : 4; 4395 int NewRetAddrLoc = SPDiff + FL->getReturnSaveOffset(); 4396 int NewRetAddr = MF.getFrameInfo().CreateFixedObject(SlotSize, 4397 NewRetAddrLoc, true); 4398 EVT VT = isPPC64 ? MVT::i64 : MVT::i32; 4399 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT); 4400 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx, 4401 MachinePointerInfo::getFixedStack(MF, NewRetAddr)); 4402 4403 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack 4404 // slot as the FP is never overwritten. 4405 if (Subtarget.isDarwinABI()) { 4406 int NewFPLoc = SPDiff + FL->getFramePointerSaveOffset(); 4407 int NewFPIdx = MF.getFrameInfo().CreateFixedObject(SlotSize, NewFPLoc, 4408 true); 4409 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT); 4410 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx, 4411 MachinePointerInfo::getFixedStack( 4412 DAG.getMachineFunction(), NewFPIdx)); 4413 } 4414 } 4415 return Chain; 4416 } 4417 4418 /// CalculateTailCallArgDest - Remember Argument for later processing. Calculate 4419 /// the position of the argument. 4420 static void 4421 CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64, 4422 SDValue Arg, int SPDiff, unsigned ArgOffset, 4423 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) { 4424 int Offset = ArgOffset + SPDiff; 4425 uint32_t OpSize = (Arg.getValueSizeInBits() + 7) / 8; 4426 int FI = MF.getFrameInfo().CreateFixedObject(OpSize, Offset, true); 4427 EVT VT = isPPC64 ? MVT::i64 : MVT::i32; 4428 SDValue FIN = DAG.getFrameIndex(FI, VT); 4429 TailCallArgumentInfo Info; 4430 Info.Arg = Arg; 4431 Info.FrameIdxOp = FIN; 4432 Info.FrameIdx = FI; 4433 TailCallArguments.push_back(Info); 4434 } 4435 4436 /// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address 4437 /// stack slot. Returns the chain as result and the loaded frame pointers in 4438 /// LROpOut/FPOpout. Used when tail calling. 4439 SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr( 4440 SelectionDAG &DAG, int SPDiff, SDValue Chain, SDValue &LROpOut, 4441 SDValue &FPOpOut, const SDLoc &dl) const { 4442 if (SPDiff) { 4443 // Load the LR and FP stack slot for later adjusting. 4444 EVT VT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32; 4445 LROpOut = getReturnAddrFrameIndex(DAG); 4446 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo()); 4447 Chain = SDValue(LROpOut.getNode(), 1); 4448 4449 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack 4450 // slot as the FP is never overwritten. 4451 if (Subtarget.isDarwinABI()) { 4452 FPOpOut = getFramePointerFrameIndex(DAG); 4453 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo()); 4454 Chain = SDValue(FPOpOut.getNode(), 1); 4455 } 4456 } 4457 return Chain; 4458 } 4459 4460 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified 4461 /// by "Src" to address "Dst" of size "Size". Alignment information is 4462 /// specified by the specific parameter attribute. The copy will be passed as 4463 /// a byval function parameter. 4464 /// Sometimes what we are copying is the end of a larger object, the part that 4465 /// does not fit in registers. 4466 static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst, 4467 SDValue Chain, ISD::ArgFlagsTy Flags, 4468 SelectionDAG &DAG, const SDLoc &dl) { 4469 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32); 4470 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(), 4471 false, false, false, MachinePointerInfo(), 4472 MachinePointerInfo()); 4473 } 4474 4475 /// LowerMemOpCallTo - Store the argument to the stack or remember it in case of 4476 /// tail calls. 4477 static void LowerMemOpCallTo( 4478 SelectionDAG &DAG, MachineFunction &MF, SDValue Chain, SDValue Arg, 4479 SDValue PtrOff, int SPDiff, unsigned ArgOffset, bool isPPC64, 4480 bool isTailCall, bool isVector, SmallVectorImpl<SDValue> &MemOpChains, 4481 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments, const SDLoc &dl) { 4482 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 4483 if (!isTailCall) { 4484 if (isVector) { 4485 SDValue StackPtr; 4486 if (isPPC64) 4487 StackPtr = DAG.getRegister(PPC::X1, MVT::i64); 4488 else 4489 StackPtr = DAG.getRegister(PPC::R1, MVT::i32); 4490 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, 4491 DAG.getConstant(ArgOffset, dl, PtrVT)); 4492 } 4493 MemOpChains.push_back( 4494 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo())); 4495 // Calculate and remember argument location. 4496 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset, 4497 TailCallArguments); 4498 } 4499 4500 static void 4501 PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain, 4502 const SDLoc &dl, int SPDiff, unsigned NumBytes, SDValue LROp, 4503 SDValue FPOp, 4504 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) { 4505 // Emit a sequence of copyto/copyfrom virtual registers for arguments that 4506 // might overwrite each other in case of tail call optimization. 4507 SmallVector<SDValue, 8> MemOpChains2; 4508 // Do not flag preceding copytoreg stuff together with the following stuff. 4509 InFlag = SDValue(); 4510 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments, 4511 MemOpChains2, dl); 4512 if (!MemOpChains2.empty()) 4513 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2); 4514 4515 // Store the return address to the appropriate stack slot. 4516 Chain = EmitTailCallStoreFPAndRetAddr(DAG, Chain, LROp, FPOp, SPDiff, dl); 4517 4518 // Emit callseq_end just before tailcall node. 4519 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), 4520 DAG.getIntPtrConstant(0, dl, true), InFlag, dl); 4521 InFlag = Chain.getValue(1); 4522 } 4523 4524 // Is this global address that of a function that can be called by name? (as 4525 // opposed to something that must hold a descriptor for an indirect call). 4526 static bool isFunctionGlobalAddress(SDValue Callee) { 4527 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 4528 if (Callee.getOpcode() == ISD::GlobalTLSAddress || 4529 Callee.getOpcode() == ISD::TargetGlobalTLSAddress) 4530 return false; 4531 4532 return G->getGlobal()->getValueType()->isFunctionTy(); 4533 } 4534 4535 return false; 4536 } 4537 4538 static unsigned 4539 PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag, SDValue &Chain, 4540 SDValue CallSeqStart, const SDLoc &dl, int SPDiff, bool isTailCall, 4541 bool isPatchPoint, bool hasNest, 4542 SmallVectorImpl<std::pair<unsigned, SDValue>> &RegsToPass, 4543 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys, 4544 ImmutableCallSite *CS, const PPCSubtarget &Subtarget) { 4545 bool isPPC64 = Subtarget.isPPC64(); 4546 bool isSVR4ABI = Subtarget.isSVR4ABI(); 4547 bool isELFv2ABI = Subtarget.isELFv2ABI(); 4548 4549 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 4550 NodeTys.push_back(MVT::Other); // Returns a chain 4551 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use. 4552 4553 unsigned CallOpc = PPCISD::CALL; 4554 4555 bool needIndirectCall = true; 4556 if (!isSVR4ABI || !isPPC64) 4557 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) { 4558 // If this is an absolute destination address, use the munged value. 4559 Callee = SDValue(Dest, 0); 4560 needIndirectCall = false; 4561 } 4562 4563 // PC-relative references to external symbols should go through $stub, unless 4564 // we're building with the leopard linker or later, which automatically 4565 // synthesizes these stubs. 4566 const TargetMachine &TM = DAG.getTarget(); 4567 const Module *Mod = DAG.getMachineFunction().getFunction()->getParent(); 4568 const GlobalValue *GV = nullptr; 4569 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) 4570 GV = G->getGlobal(); 4571 bool Local = TM.shouldAssumeDSOLocal(*Mod, GV); 4572 bool UsePlt = !Local && Subtarget.isTargetELF() && !isPPC64; 4573 4574 if (isFunctionGlobalAddress(Callee)) { 4575 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Callee); 4576 // A call to a TLS address is actually an indirect call to a 4577 // thread-specific pointer. 4578 unsigned OpFlags = 0; 4579 if (UsePlt) 4580 OpFlags = PPCII::MO_PLT; 4581 4582 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, 4583 // every direct call is) turn it into a TargetGlobalAddress / 4584 // TargetExternalSymbol node so that legalize doesn't hack it. 4585 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, 4586 Callee.getValueType(), 0, OpFlags); 4587 needIndirectCall = false; 4588 } 4589 4590 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 4591 unsigned char OpFlags = 0; 4592 4593 if (UsePlt) 4594 OpFlags = PPCII::MO_PLT; 4595 4596 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(), 4597 OpFlags); 4598 needIndirectCall = false; 4599 } 4600 4601 if (isPatchPoint) { 4602 // We'll form an invalid direct call when lowering a patchpoint; the full 4603 // sequence for an indirect call is complicated, and many of the 4604 // instructions introduced might have side effects (and, thus, can't be 4605 // removed later). The call itself will be removed as soon as the 4606 // argument/return lowering is complete, so the fact that it has the wrong 4607 // kind of operands should not really matter. 4608 needIndirectCall = false; 4609 } 4610 4611 if (needIndirectCall) { 4612 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair 4613 // to do the call, we can't use PPCISD::CALL. 4614 SDValue MTCTROps[] = {Chain, Callee, InFlag}; 4615 4616 if (isSVR4ABI && isPPC64 && !isELFv2ABI) { 4617 // Function pointers in the 64-bit SVR4 ABI do not point to the function 4618 // entry point, but to the function descriptor (the function entry point 4619 // address is part of the function descriptor though). 4620 // The function descriptor is a three doubleword structure with the 4621 // following fields: function entry point, TOC base address and 4622 // environment pointer. 4623 // Thus for a call through a function pointer, the following actions need 4624 // to be performed: 4625 // 1. Save the TOC of the caller in the TOC save area of its stack 4626 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()). 4627 // 2. Load the address of the function entry point from the function 4628 // descriptor. 4629 // 3. Load the TOC of the callee from the function descriptor into r2. 4630 // 4. Load the environment pointer from the function descriptor into 4631 // r11. 4632 // 5. Branch to the function entry point address. 4633 // 6. On return of the callee, the TOC of the caller needs to be 4634 // restored (this is done in FinishCall()). 4635 // 4636 // The loads are scheduled at the beginning of the call sequence, and the 4637 // register copies are flagged together to ensure that no other 4638 // operations can be scheduled in between. E.g. without flagging the 4639 // copies together, a TOC access in the caller could be scheduled between 4640 // the assignment of the callee TOC and the branch to the callee, which 4641 // results in the TOC access going through the TOC of the callee instead 4642 // of going through the TOC of the caller, which leads to incorrect code. 4643 4644 // Load the address of the function entry point from the function 4645 // descriptor. 4646 SDValue LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-1); 4647 if (LDChain.getValueType() == MVT::Glue) 4648 LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-2); 4649 4650 auto MMOFlags = Subtarget.hasInvariantFunctionDescriptors() 4651 ? (MachineMemOperand::MODereferenceable | 4652 MachineMemOperand::MOInvariant) 4653 : MachineMemOperand::MONone; 4654 4655 MachinePointerInfo MPI(CS ? CS->getCalledValue() : nullptr); 4656 SDValue LoadFuncPtr = DAG.getLoad(MVT::i64, dl, LDChain, Callee, MPI, 4657 /* Alignment = */ 8, MMOFlags); 4658 4659 // Load environment pointer into r11. 4660 SDValue PtrOff = DAG.getIntPtrConstant(16, dl); 4661 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff); 4662 SDValue LoadEnvPtr = 4663 DAG.getLoad(MVT::i64, dl, LDChain, AddPtr, MPI.getWithOffset(16), 4664 /* Alignment = */ 8, MMOFlags); 4665 4666 SDValue TOCOff = DAG.getIntPtrConstant(8, dl); 4667 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, TOCOff); 4668 SDValue TOCPtr = 4669 DAG.getLoad(MVT::i64, dl, LDChain, AddTOC, MPI.getWithOffset(8), 4670 /* Alignment = */ 8, MMOFlags); 4671 4672 setUsesTOCBasePtr(DAG); 4673 SDValue TOCVal = DAG.getCopyToReg(Chain, dl, PPC::X2, TOCPtr, 4674 InFlag); 4675 Chain = TOCVal.getValue(0); 4676 InFlag = TOCVal.getValue(1); 4677 4678 // If the function call has an explicit 'nest' parameter, it takes the 4679 // place of the environment pointer. 4680 if (!hasNest) { 4681 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr, 4682 InFlag); 4683 4684 Chain = EnvVal.getValue(0); 4685 InFlag = EnvVal.getValue(1); 4686 } 4687 4688 MTCTROps[0] = Chain; 4689 MTCTROps[1] = LoadFuncPtr; 4690 MTCTROps[2] = InFlag; 4691 } 4692 4693 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, 4694 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2)); 4695 InFlag = Chain.getValue(1); 4696 4697 NodeTys.clear(); 4698 NodeTys.push_back(MVT::Other); 4699 NodeTys.push_back(MVT::Glue); 4700 Ops.push_back(Chain); 4701 CallOpc = PPCISD::BCTRL; 4702 Callee.setNode(nullptr); 4703 // Add use of X11 (holding environment pointer) 4704 if (isSVR4ABI && isPPC64 && !isELFv2ABI && !hasNest) 4705 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT)); 4706 // Add CTR register as callee so a bctr can be emitted later. 4707 if (isTailCall) 4708 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT)); 4709 } 4710 4711 // If this is a direct call, pass the chain and the callee. 4712 if (Callee.getNode()) { 4713 Ops.push_back(Chain); 4714 Ops.push_back(Callee); 4715 } 4716 // If this is a tail call add stack pointer delta. 4717 if (isTailCall) 4718 Ops.push_back(DAG.getConstant(SPDiff, dl, MVT::i32)); 4719 4720 // Add argument registers to the end of the list so that they are known live 4721 // into the call. 4722 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 4723 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 4724 RegsToPass[i].second.getValueType())); 4725 4726 // All calls, in both the ELF V1 and V2 ABIs, need the TOC register live 4727 // into the call. 4728 if (isSVR4ABI && isPPC64 && !isPatchPoint) { 4729 setUsesTOCBasePtr(DAG); 4730 Ops.push_back(DAG.getRegister(PPC::X2, PtrVT)); 4731 } 4732 4733 return CallOpc; 4734 } 4735 4736 SDValue PPCTargetLowering::LowerCallResult( 4737 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg, 4738 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 4739 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 4740 SmallVector<CCValAssign, 16> RVLocs; 4741 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 4742 *DAG.getContext()); 4743 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC); 4744 4745 // Copy all of the result registers out of their specified physreg. 4746 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { 4747 CCValAssign &VA = RVLocs[i]; 4748 assert(VA.isRegLoc() && "Can only return in registers!"); 4749 4750 SDValue Val = DAG.getCopyFromReg(Chain, dl, 4751 VA.getLocReg(), VA.getLocVT(), InFlag); 4752 Chain = Val.getValue(1); 4753 InFlag = Val.getValue(2); 4754 4755 switch (VA.getLocInfo()) { 4756 default: llvm_unreachable("Unknown loc info!"); 4757 case CCValAssign::Full: break; 4758 case CCValAssign::AExt: 4759 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); 4760 break; 4761 case CCValAssign::ZExt: 4762 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val, 4763 DAG.getValueType(VA.getValVT())); 4764 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); 4765 break; 4766 case CCValAssign::SExt: 4767 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val, 4768 DAG.getValueType(VA.getValVT())); 4769 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); 4770 break; 4771 } 4772 4773 InVals.push_back(Val); 4774 } 4775 4776 return Chain; 4777 } 4778 4779 SDValue PPCTargetLowering::FinishCall( 4780 CallingConv::ID CallConv, const SDLoc &dl, bool isTailCall, bool isVarArg, 4781 bool isPatchPoint, bool hasNest, SelectionDAG &DAG, 4782 SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass, SDValue InFlag, 4783 SDValue Chain, SDValue CallSeqStart, SDValue &Callee, int SPDiff, 4784 unsigned NumBytes, const SmallVectorImpl<ISD::InputArg> &Ins, 4785 SmallVectorImpl<SDValue> &InVals, ImmutableCallSite *CS) const { 4786 std::vector<EVT> NodeTys; 4787 SmallVector<SDValue, 8> Ops; 4788 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, CallSeqStart, dl, 4789 SPDiff, isTailCall, isPatchPoint, hasNest, 4790 RegsToPass, Ops, NodeTys, CS, Subtarget); 4791 4792 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls 4793 if (isVarArg && Subtarget.isSVR4ABI() && !Subtarget.isPPC64()) 4794 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32)); 4795 4796 // When performing tail call optimization the callee pops its arguments off 4797 // the stack. Account for this here so these bytes can be pushed back on in 4798 // PPCFrameLowering::eliminateCallFramePseudoInstr. 4799 int BytesCalleePops = 4800 (CallConv == CallingConv::Fast && 4801 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0; 4802 4803 // Add a register mask operand representing the call-preserved registers. 4804 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); 4805 const uint32_t *Mask = 4806 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv); 4807 assert(Mask && "Missing call preserved mask for calling convention"); 4808 Ops.push_back(DAG.getRegisterMask(Mask)); 4809 4810 if (InFlag.getNode()) 4811 Ops.push_back(InFlag); 4812 4813 // Emit tail call. 4814 if (isTailCall) { 4815 assert(((Callee.getOpcode() == ISD::Register && 4816 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) || 4817 Callee.getOpcode() == ISD::TargetExternalSymbol || 4818 Callee.getOpcode() == ISD::TargetGlobalAddress || 4819 isa<ConstantSDNode>(Callee)) && 4820 "Expecting an global address, external symbol, absolute value or register"); 4821 4822 DAG.getMachineFunction().getFrameInfo().setHasTailCall(); 4823 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops); 4824 } 4825 4826 // Add a NOP immediately after the branch instruction when using the 64-bit 4827 // SVR4 ABI. At link time, if caller and callee are in a different module and 4828 // thus have a different TOC, the call will be replaced with a call to a stub 4829 // function which saves the current TOC, loads the TOC of the callee and 4830 // branches to the callee. The NOP will be replaced with a load instruction 4831 // which restores the TOC of the caller from the TOC save slot of the current 4832 // stack frame. If caller and callee belong to the same module (and have the 4833 // same TOC), the NOP will remain unchanged. 4834 4835 MachineFunction &MF = DAG.getMachineFunction(); 4836 if (!isTailCall && Subtarget.isSVR4ABI()&& Subtarget.isPPC64() && 4837 !isPatchPoint) { 4838 if (CallOpc == PPCISD::BCTRL) { 4839 // This is a call through a function pointer. 4840 // Restore the caller TOC from the save area into R2. 4841 // See PrepareCall() for more information about calls through function 4842 // pointers in the 64-bit SVR4 ABI. 4843 // We are using a target-specific load with r2 hard coded, because the 4844 // result of a target-independent load would never go directly into r2, 4845 // since r2 is a reserved register (which prevents the register allocator 4846 // from allocating it), resulting in an additional register being 4847 // allocated and an unnecessary move instruction being generated. 4848 CallOpc = PPCISD::BCTRL_LOAD_TOC; 4849 4850 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 4851 SDValue StackPtr = DAG.getRegister(PPC::X1, PtrVT); 4852 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset(); 4853 SDValue TOCOff = DAG.getIntPtrConstant(TOCSaveOffset, dl); 4854 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, StackPtr, TOCOff); 4855 4856 // The address needs to go after the chain input but before the flag (or 4857 // any other variadic arguments). 4858 Ops.insert(std::next(Ops.begin()), AddTOC); 4859 } else if (CallOpc == PPCISD::CALL && 4860 !resideInSameSection(MF.getFunction(), Callee, DAG.getTarget())) { 4861 // Otherwise insert NOP for non-local calls. 4862 CallOpc = PPCISD::CALL_NOP; 4863 } 4864 } 4865 4866 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops); 4867 InFlag = Chain.getValue(1); 4868 4869 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), 4870 DAG.getIntPtrConstant(BytesCalleePops, dl, true), 4871 InFlag, dl); 4872 if (!Ins.empty()) 4873 InFlag = Chain.getValue(1); 4874 4875 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, 4876 Ins, dl, DAG, InVals); 4877 } 4878 4879 SDValue 4880 PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 4881 SmallVectorImpl<SDValue> &InVals) const { 4882 SelectionDAG &DAG = CLI.DAG; 4883 SDLoc &dl = CLI.DL; 4884 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 4885 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 4886 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 4887 SDValue Chain = CLI.Chain; 4888 SDValue Callee = CLI.Callee; 4889 bool &isTailCall = CLI.IsTailCall; 4890 CallingConv::ID CallConv = CLI.CallConv; 4891 bool isVarArg = CLI.IsVarArg; 4892 bool isPatchPoint = CLI.IsPatchPoint; 4893 ImmutableCallSite *CS = CLI.CS; 4894 4895 if (isTailCall) { 4896 if (Subtarget.useLongCalls() && !(CS && CS->isMustTailCall())) 4897 isTailCall = false; 4898 else if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) 4899 isTailCall = 4900 IsEligibleForTailCallOptimization_64SVR4(Callee, CallConv, CS, 4901 isVarArg, Outs, Ins, DAG); 4902 else 4903 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, 4904 Ins, DAG); 4905 if (isTailCall) { 4906 ++NumTailCalls; 4907 if (!getTargetMachine().Options.GuaranteedTailCallOpt) 4908 ++NumSiblingCalls; 4909 4910 assert(isa<GlobalAddressSDNode>(Callee) && 4911 "Callee should be an llvm::Function object."); 4912 DEBUG( 4913 const GlobalValue *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal(); 4914 const unsigned Width = 80 - strlen("TCO caller: ") 4915 - strlen(", callee linkage: 0, 0"); 4916 dbgs() << "TCO caller: " 4917 << left_justify(DAG.getMachineFunction().getName(), Width) 4918 << ", callee linkage: " 4919 << GV->getVisibility() << ", " << GV->getLinkage() << "\n" 4920 ); 4921 } 4922 } 4923 4924 if (!isTailCall && CS && CS->isMustTailCall()) 4925 report_fatal_error("failed to perform tail call elimination on a call " 4926 "site marked musttail"); 4927 4928 // When long calls (i.e. indirect calls) are always used, calls are always 4929 // made via function pointer. If we have a function name, first translate it 4930 // into a pointer. 4931 if (Subtarget.useLongCalls() && isa<GlobalAddressSDNode>(Callee) && 4932 !isTailCall) 4933 Callee = LowerGlobalAddress(Callee, DAG); 4934 4935 if (Subtarget.isSVR4ABI()) { 4936 if (Subtarget.isPPC64()) 4937 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg, 4938 isTailCall, isPatchPoint, Outs, OutVals, Ins, 4939 dl, DAG, InVals, CS); 4940 else 4941 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg, 4942 isTailCall, isPatchPoint, Outs, OutVals, Ins, 4943 dl, DAG, InVals, CS); 4944 } 4945 4946 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg, 4947 isTailCall, isPatchPoint, Outs, OutVals, Ins, 4948 dl, DAG, InVals, CS); 4949 } 4950 4951 SDValue PPCTargetLowering::LowerCall_32SVR4( 4952 SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg, 4953 bool isTailCall, bool isPatchPoint, 4954 const SmallVectorImpl<ISD::OutputArg> &Outs, 4955 const SmallVectorImpl<SDValue> &OutVals, 4956 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 4957 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, 4958 ImmutableCallSite *CS) const { 4959 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description 4960 // of the 32-bit SVR4 ABI stack frame layout. 4961 4962 assert((CallConv == CallingConv::C || 4963 CallConv == CallingConv::Fast) && "Unknown calling convention!"); 4964 4965 unsigned PtrByteSize = 4; 4966 4967 MachineFunction &MF = DAG.getMachineFunction(); 4968 4969 // Mark this function as potentially containing a function that contains a 4970 // tail call. As a consequence the frame pointer will be used for dynamicalloc 4971 // and restoring the callers stack pointer in this functions epilog. This is 4972 // done because by tail calling the called function might overwrite the value 4973 // in this function's (MF) stack pointer stack slot 0(SP). 4974 if (getTargetMachine().Options.GuaranteedTailCallOpt && 4975 CallConv == CallingConv::Fast) 4976 MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); 4977 4978 // Count how many bytes are to be pushed on the stack, including the linkage 4979 // area, parameter list area and the part of the local variable space which 4980 // contains copies of aggregates which are passed by value. 4981 4982 // Assign locations to all of the outgoing arguments. 4983 SmallVector<CCValAssign, 16> ArgLocs; 4984 PPCCCState CCInfo(CallConv, isVarArg, MF, ArgLocs, *DAG.getContext()); 4985 4986 // Reserve space for the linkage area on the stack. 4987 CCInfo.AllocateStack(Subtarget.getFrameLowering()->getLinkageSize(), 4988 PtrByteSize); 4989 if (useSoftFloat()) 4990 CCInfo.PreAnalyzeCallOperands(Outs); 4991 4992 if (isVarArg) { 4993 // Handle fixed and variable vector arguments differently. 4994 // Fixed vector arguments go into registers as long as registers are 4995 // available. Variable vector arguments always go into memory. 4996 unsigned NumArgs = Outs.size(); 4997 4998 for (unsigned i = 0; i != NumArgs; ++i) { 4999 MVT ArgVT = Outs[i].VT; 5000 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; 5001 bool Result; 5002 5003 if (Outs[i].IsFixed) { 5004 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, 5005 CCInfo); 5006 } else { 5007 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, 5008 ArgFlags, CCInfo); 5009 } 5010 5011 if (Result) { 5012 #ifndef NDEBUG 5013 errs() << "Call operand #" << i << " has unhandled type " 5014 << EVT(ArgVT).getEVTString() << "\n"; 5015 #endif 5016 llvm_unreachable(nullptr); 5017 } 5018 } 5019 } else { 5020 // All arguments are treated the same. 5021 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4); 5022 } 5023 CCInfo.clearWasPPCF128(); 5024 5025 // Assign locations to all of the outgoing aggregate by value arguments. 5026 SmallVector<CCValAssign, 16> ByValArgLocs; 5027 CCState CCByValInfo(CallConv, isVarArg, MF, ByValArgLocs, *DAG.getContext()); 5028 5029 // Reserve stack space for the allocations in CCInfo. 5030 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize); 5031 5032 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal); 5033 5034 // Size of the linkage area, parameter list area and the part of the local 5035 // space variable where copies of aggregates which are passed by value are 5036 // stored. 5037 unsigned NumBytes = CCByValInfo.getNextStackOffset(); 5038 5039 // Calculate by how many bytes the stack has to be adjusted in case of tail 5040 // call optimization. 5041 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); 5042 5043 // Adjust the stack pointer for the new arguments... 5044 // These operations are automatically eliminated by the prolog/epilog pass 5045 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl); 5046 SDValue CallSeqStart = Chain; 5047 5048 // Load the return address and frame pointer so it can be moved somewhere else 5049 // later. 5050 SDValue LROp, FPOp; 5051 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, dl); 5052 5053 // Set up a copy of the stack pointer for use loading and storing any 5054 // arguments that may not fit in the registers available for argument 5055 // passing. 5056 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32); 5057 5058 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 5059 SmallVector<TailCallArgumentInfo, 8> TailCallArguments; 5060 SmallVector<SDValue, 8> MemOpChains; 5061 5062 bool seenFloatArg = false; 5063 // Walk the register/memloc assignments, inserting copies/loads. 5064 for (unsigned i = 0, j = 0, e = ArgLocs.size(); 5065 i != e; 5066 ++i) { 5067 CCValAssign &VA = ArgLocs[i]; 5068 SDValue Arg = OutVals[i]; 5069 ISD::ArgFlagsTy Flags = Outs[i].Flags; 5070 5071 if (Flags.isByVal()) { 5072 // Argument is an aggregate which is passed by value, thus we need to 5073 // create a copy of it in the local variable space of the current stack 5074 // frame (which is the stack frame of the caller) and pass the address of 5075 // this copy to the callee. 5076 assert((j < ByValArgLocs.size()) && "Index out of bounds!"); 5077 CCValAssign &ByValVA = ByValArgLocs[j++]; 5078 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!"); 5079 5080 // Memory reserved in the local variable space of the callers stack frame. 5081 unsigned LocMemOffset = ByValVA.getLocMemOffset(); 5082 5083 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 5084 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()), 5085 StackPtr, PtrOff); 5086 5087 // Create a copy of the argument in the local area of the current 5088 // stack frame. 5089 SDValue MemcpyCall = 5090 CreateCopyOfByValArgument(Arg, PtrOff, 5091 CallSeqStart.getNode()->getOperand(0), 5092 Flags, DAG, dl); 5093 5094 // This must go outside the CALLSEQ_START..END. 5095 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, NumBytes, 0, 5096 SDLoc(MemcpyCall)); 5097 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), 5098 NewCallSeqStart.getNode()); 5099 Chain = CallSeqStart = NewCallSeqStart; 5100 5101 // Pass the address of the aggregate copy on the stack either in a 5102 // physical register or in the parameter list area of the current stack 5103 // frame to the callee. 5104 Arg = PtrOff; 5105 } 5106 5107 if (VA.isRegLoc()) { 5108 if (Arg.getValueType() == MVT::i1) 5109 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg); 5110 5111 seenFloatArg |= VA.getLocVT().isFloatingPoint(); 5112 // Put argument in a physical register. 5113 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 5114 } else { 5115 // Put argument in the parameter list area of the current stack frame. 5116 assert(VA.isMemLoc()); 5117 unsigned LocMemOffset = VA.getLocMemOffset(); 5118 5119 if (!isTailCall) { 5120 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 5121 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()), 5122 StackPtr, PtrOff); 5123 5124 MemOpChains.push_back( 5125 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo())); 5126 } else { 5127 // Calculate and remember argument location. 5128 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset, 5129 TailCallArguments); 5130 } 5131 } 5132 } 5133 5134 if (!MemOpChains.empty()) 5135 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 5136 5137 // Build a sequence of copy-to-reg nodes chained together with token chain 5138 // and flag operands which copy the outgoing args into the appropriate regs. 5139 SDValue InFlag; 5140 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 5141 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 5142 RegsToPass[i].second, InFlag); 5143 InFlag = Chain.getValue(1); 5144 } 5145 5146 // Set CR bit 6 to true if this is a vararg call with floating args passed in 5147 // registers. 5148 if (isVarArg) { 5149 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); 5150 SDValue Ops[] = { Chain, InFlag }; 5151 5152 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET, 5153 dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1)); 5154 5155 InFlag = Chain.getValue(1); 5156 } 5157 5158 if (isTailCall) 5159 PrepareTailCall(DAG, InFlag, Chain, dl, SPDiff, NumBytes, LROp, FPOp, 5160 TailCallArguments); 5161 5162 return FinishCall(CallConv, dl, isTailCall, isVarArg, isPatchPoint, 5163 /* unused except on PPC64 ELFv1 */ false, DAG, 5164 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff, 5165 NumBytes, Ins, InVals, CS); 5166 } 5167 5168 // Copy an argument into memory, being careful to do this outside the 5169 // call sequence for the call to which the argument belongs. 5170 SDValue PPCTargetLowering::createMemcpyOutsideCallSeq( 5171 SDValue Arg, SDValue PtrOff, SDValue CallSeqStart, ISD::ArgFlagsTy Flags, 5172 SelectionDAG &DAG, const SDLoc &dl) const { 5173 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff, 5174 CallSeqStart.getNode()->getOperand(0), 5175 Flags, DAG, dl); 5176 // The MEMCPY must go outside the CALLSEQ_START..END. 5177 int64_t FrameSize = CallSeqStart.getConstantOperandVal(1); 5178 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, FrameSize, 0, 5179 SDLoc(MemcpyCall)); 5180 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), 5181 NewCallSeqStart.getNode()); 5182 return NewCallSeqStart; 5183 } 5184 5185 SDValue PPCTargetLowering::LowerCall_64SVR4( 5186 SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg, 5187 bool isTailCall, bool isPatchPoint, 5188 const SmallVectorImpl<ISD::OutputArg> &Outs, 5189 const SmallVectorImpl<SDValue> &OutVals, 5190 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 5191 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, 5192 ImmutableCallSite *CS) const { 5193 bool isELFv2ABI = Subtarget.isELFv2ABI(); 5194 bool isLittleEndian = Subtarget.isLittleEndian(); 5195 unsigned NumOps = Outs.size(); 5196 bool hasNest = false; 5197 bool IsSibCall = false; 5198 5199 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 5200 unsigned PtrByteSize = 8; 5201 5202 MachineFunction &MF = DAG.getMachineFunction(); 5203 5204 if (isTailCall && !getTargetMachine().Options.GuaranteedTailCallOpt) 5205 IsSibCall = true; 5206 5207 // Mark this function as potentially containing a function that contains a 5208 // tail call. As a consequence the frame pointer will be used for dynamicalloc 5209 // and restoring the callers stack pointer in this functions epilog. This is 5210 // done because by tail calling the called function might overwrite the value 5211 // in this function's (MF) stack pointer stack slot 0(SP). 5212 if (getTargetMachine().Options.GuaranteedTailCallOpt && 5213 CallConv == CallingConv::Fast) 5214 MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); 5215 5216 assert(!(CallConv == CallingConv::Fast && isVarArg) && 5217 "fastcc not supported on varargs functions"); 5218 5219 // Count how many bytes are to be pushed on the stack, including the linkage 5220 // area, and parameter passing area. On ELFv1, the linkage area is 48 bytes 5221 // reserved space for [SP][CR][LR][2 x unused][TOC]; on ELFv2, the linkage 5222 // area is 32 bytes reserved space for [SP][CR][LR][TOC]. 5223 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); 5224 unsigned NumBytes = LinkageSize; 5225 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; 5226 unsigned &QFPR_idx = FPR_idx; 5227 5228 static const MCPhysReg GPR[] = { 5229 PPC::X3, PPC::X4, PPC::X5, PPC::X6, 5230 PPC::X7, PPC::X8, PPC::X9, PPC::X10, 5231 }; 5232 static const MCPhysReg VR[] = { 5233 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, 5234 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 5235 }; 5236 5237 const unsigned NumGPRs = array_lengthof(GPR); 5238 const unsigned NumFPRs = useSoftFloat() ? 0 : 13; 5239 const unsigned NumVRs = array_lengthof(VR); 5240 const unsigned NumQFPRs = NumFPRs; 5241 5242 // On ELFv2, we can avoid allocating the parameter area if all the arguments 5243 // can be passed to the callee in registers. 5244 // For the fast calling convention, there is another check below. 5245 // Note: We should keep consistent with LowerFormalArguments_64SVR4() 5246 bool HasParameterArea = !isELFv2ABI || isVarArg || CallConv == CallingConv::Fast; 5247 if (!HasParameterArea) { 5248 unsigned ParamAreaSize = NumGPRs * PtrByteSize; 5249 unsigned AvailableFPRs = NumFPRs; 5250 unsigned AvailableVRs = NumVRs; 5251 unsigned NumBytesTmp = NumBytes; 5252 for (unsigned i = 0; i != NumOps; ++i) { 5253 if (Outs[i].Flags.isNest()) continue; 5254 if (CalculateStackSlotUsed(Outs[i].VT, Outs[i].ArgVT, Outs[i].Flags, 5255 PtrByteSize, LinkageSize, ParamAreaSize, 5256 NumBytesTmp, AvailableFPRs, AvailableVRs, 5257 Subtarget.hasQPX())) 5258 HasParameterArea = true; 5259 } 5260 } 5261 5262 // When using the fast calling convention, we don't provide backing for 5263 // arguments that will be in registers. 5264 unsigned NumGPRsUsed = 0, NumFPRsUsed = 0, NumVRsUsed = 0; 5265 5266 // Add up all the space actually used. 5267 for (unsigned i = 0; i != NumOps; ++i) { 5268 ISD::ArgFlagsTy Flags = Outs[i].Flags; 5269 EVT ArgVT = Outs[i].VT; 5270 EVT OrigVT = Outs[i].ArgVT; 5271 5272 if (Flags.isNest()) 5273 continue; 5274 5275 if (CallConv == CallingConv::Fast) { 5276 if (Flags.isByVal()) 5277 NumGPRsUsed += (Flags.getByValSize()+7)/8; 5278 else 5279 switch (ArgVT.getSimpleVT().SimpleTy) { 5280 default: llvm_unreachable("Unexpected ValueType for argument!"); 5281 case MVT::i1: 5282 case MVT::i32: 5283 case MVT::i64: 5284 if (++NumGPRsUsed <= NumGPRs) 5285 continue; 5286 break; 5287 case MVT::v4i32: 5288 case MVT::v8i16: 5289 case MVT::v16i8: 5290 case MVT::v2f64: 5291 case MVT::v2i64: 5292 case MVT::v1i128: 5293 if (++NumVRsUsed <= NumVRs) 5294 continue; 5295 break; 5296 case MVT::v4f32: 5297 // When using QPX, this is handled like a FP register, otherwise, it 5298 // is an Altivec register. 5299 if (Subtarget.hasQPX()) { 5300 if (++NumFPRsUsed <= NumFPRs) 5301 continue; 5302 } else { 5303 if (++NumVRsUsed <= NumVRs) 5304 continue; 5305 } 5306 break; 5307 case MVT::f32: 5308 case MVT::f64: 5309 case MVT::v4f64: // QPX 5310 case MVT::v4i1: // QPX 5311 if (++NumFPRsUsed <= NumFPRs) 5312 continue; 5313 break; 5314 } 5315 } 5316 5317 /* Respect alignment of argument on the stack. */ 5318 unsigned Align = 5319 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize); 5320 NumBytes = ((NumBytes + Align - 1) / Align) * Align; 5321 5322 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize); 5323 if (Flags.isInConsecutiveRegsLast()) 5324 NumBytes = ((NumBytes + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 5325 } 5326 5327 unsigned NumBytesActuallyUsed = NumBytes; 5328 5329 // In the old ELFv1 ABI, 5330 // the prolog code of the callee may store up to 8 GPR argument registers to 5331 // the stack, allowing va_start to index over them in memory if its varargs. 5332 // Because we cannot tell if this is needed on the caller side, we have to 5333 // conservatively assume that it is needed. As such, make sure we have at 5334 // least enough stack space for the caller to store the 8 GPRs. 5335 // In the ELFv2 ABI, we allocate the parameter area iff a callee 5336 // really requires memory operands, e.g. a vararg function. 5337 if (HasParameterArea) 5338 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize); 5339 else 5340 NumBytes = LinkageSize; 5341 5342 // Tail call needs the stack to be aligned. 5343 if (getTargetMachine().Options.GuaranteedTailCallOpt && 5344 CallConv == CallingConv::Fast) 5345 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes); 5346 5347 int SPDiff = 0; 5348 5349 // Calculate by how many bytes the stack has to be adjusted in case of tail 5350 // call optimization. 5351 if (!IsSibCall) 5352 SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); 5353 5354 // To protect arguments on the stack from being clobbered in a tail call, 5355 // force all the loads to happen before doing any other lowering. 5356 if (isTailCall) 5357 Chain = DAG.getStackArgumentTokenFactor(Chain); 5358 5359 // Adjust the stack pointer for the new arguments... 5360 // These operations are automatically eliminated by the prolog/epilog pass 5361 if (!IsSibCall) 5362 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl); 5363 SDValue CallSeqStart = Chain; 5364 5365 // Load the return address and frame pointer so it can be move somewhere else 5366 // later. 5367 SDValue LROp, FPOp; 5368 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, dl); 5369 5370 // Set up a copy of the stack pointer for use loading and storing any 5371 // arguments that may not fit in the registers available for argument 5372 // passing. 5373 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64); 5374 5375 // Figure out which arguments are going to go in registers, and which in 5376 // memory. Also, if this is a vararg function, floating point operations 5377 // must be stored to our stack, and loaded into integer regs as well, if 5378 // any integer regs are available for argument passing. 5379 unsigned ArgOffset = LinkageSize; 5380 5381 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 5382 SmallVector<TailCallArgumentInfo, 8> TailCallArguments; 5383 5384 SmallVector<SDValue, 8> MemOpChains; 5385 for (unsigned i = 0; i != NumOps; ++i) { 5386 SDValue Arg = OutVals[i]; 5387 ISD::ArgFlagsTy Flags = Outs[i].Flags; 5388 EVT ArgVT = Outs[i].VT; 5389 EVT OrigVT = Outs[i].ArgVT; 5390 5391 // PtrOff will be used to store the current argument to the stack if a 5392 // register cannot be found for it. 5393 SDValue PtrOff; 5394 5395 // We re-align the argument offset for each argument, except when using the 5396 // fast calling convention, when we need to make sure we do that only when 5397 // we'll actually use a stack slot. 5398 auto ComputePtrOff = [&]() { 5399 /* Respect alignment of argument on the stack. */ 5400 unsigned Align = 5401 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize); 5402 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align; 5403 5404 PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType()); 5405 5406 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); 5407 }; 5408 5409 if (CallConv != CallingConv::Fast) { 5410 ComputePtrOff(); 5411 5412 /* Compute GPR index associated with argument offset. */ 5413 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize; 5414 GPR_idx = std::min(GPR_idx, NumGPRs); 5415 } 5416 5417 // Promote integers to 64-bit values. 5418 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) { 5419 // FIXME: Should this use ANY_EXTEND if neither sext nor zext? 5420 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 5421 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg); 5422 } 5423 5424 // FIXME memcpy is used way more than necessary. Correctness first. 5425 // Note: "by value" is code for passing a structure by value, not 5426 // basic types. 5427 if (Flags.isByVal()) { 5428 // Note: Size includes alignment padding, so 5429 // struct x { short a; char b; } 5430 // will have Size = 4. With #pragma pack(1), it will have Size = 3. 5431 // These are the proper values we need for right-justifying the 5432 // aggregate in a parameter register. 5433 unsigned Size = Flags.getByValSize(); 5434 5435 // An empty aggregate parameter takes up no storage and no 5436 // registers. 5437 if (Size == 0) 5438 continue; 5439 5440 if (CallConv == CallingConv::Fast) 5441 ComputePtrOff(); 5442 5443 // All aggregates smaller than 8 bytes must be passed right-justified. 5444 if (Size==1 || Size==2 || Size==4) { 5445 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32); 5446 if (GPR_idx != NumGPRs) { 5447 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg, 5448 MachinePointerInfo(), VT); 5449 MemOpChains.push_back(Load.getValue(1)); 5450 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5451 5452 ArgOffset += PtrByteSize; 5453 continue; 5454 } 5455 } 5456 5457 if (GPR_idx == NumGPRs && Size < 8) { 5458 SDValue AddPtr = PtrOff; 5459 if (!isLittleEndian) { 5460 SDValue Const = DAG.getConstant(PtrByteSize - Size, dl, 5461 PtrOff.getValueType()); 5462 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); 5463 } 5464 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr, 5465 CallSeqStart, 5466 Flags, DAG, dl); 5467 ArgOffset += PtrByteSize; 5468 continue; 5469 } 5470 // Copy entire object into memory. There are cases where gcc-generated 5471 // code assumes it is there, even if it could be put entirely into 5472 // registers. (This is not what the doc says.) 5473 5474 // FIXME: The above statement is likely due to a misunderstanding of the 5475 // documents. All arguments must be copied into the parameter area BY 5476 // THE CALLEE in the event that the callee takes the address of any 5477 // formal argument. That has not yet been implemented. However, it is 5478 // reasonable to use the stack area as a staging area for the register 5479 // load. 5480 5481 // Skip this for small aggregates, as we will use the same slot for a 5482 // right-justified copy, below. 5483 if (Size >= 8) 5484 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff, 5485 CallSeqStart, 5486 Flags, DAG, dl); 5487 5488 // When a register is available, pass a small aggregate right-justified. 5489 if (Size < 8 && GPR_idx != NumGPRs) { 5490 // The easiest way to get this right-justified in a register 5491 // is to copy the structure into the rightmost portion of a 5492 // local variable slot, then load the whole slot into the 5493 // register. 5494 // FIXME: The memcpy seems to produce pretty awful code for 5495 // small aggregates, particularly for packed ones. 5496 // FIXME: It would be preferable to use the slot in the 5497 // parameter save area instead of a new local variable. 5498 SDValue AddPtr = PtrOff; 5499 if (!isLittleEndian) { 5500 SDValue Const = DAG.getConstant(8 - Size, dl, PtrOff.getValueType()); 5501 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); 5502 } 5503 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr, 5504 CallSeqStart, 5505 Flags, DAG, dl); 5506 5507 // Load the slot into the register. 5508 SDValue Load = 5509 DAG.getLoad(PtrVT, dl, Chain, PtrOff, MachinePointerInfo()); 5510 MemOpChains.push_back(Load.getValue(1)); 5511 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5512 5513 // Done with this argument. 5514 ArgOffset += PtrByteSize; 5515 continue; 5516 } 5517 5518 // For aggregates larger than PtrByteSize, copy the pieces of the 5519 // object that fit into registers from the parameter save area. 5520 for (unsigned j=0; j<Size; j+=PtrByteSize) { 5521 SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType()); 5522 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 5523 if (GPR_idx != NumGPRs) { 5524 SDValue Load = 5525 DAG.getLoad(PtrVT, dl, Chain, AddArg, MachinePointerInfo()); 5526 MemOpChains.push_back(Load.getValue(1)); 5527 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5528 ArgOffset += PtrByteSize; 5529 } else { 5530 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize; 5531 break; 5532 } 5533 } 5534 continue; 5535 } 5536 5537 switch (Arg.getSimpleValueType().SimpleTy) { 5538 default: llvm_unreachable("Unexpected ValueType for argument!"); 5539 case MVT::i1: 5540 case MVT::i32: 5541 case MVT::i64: 5542 if (Flags.isNest()) { 5543 // The 'nest' parameter, if any, is passed in R11. 5544 RegsToPass.push_back(std::make_pair(PPC::X11, Arg)); 5545 hasNest = true; 5546 break; 5547 } 5548 5549 // These can be scalar arguments or elements of an integer array type 5550 // passed directly. Clang may use those instead of "byval" aggregate 5551 // types to avoid forcing arguments to memory unnecessarily. 5552 if (GPR_idx != NumGPRs) { 5553 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg)); 5554 } else { 5555 if (CallConv == CallingConv::Fast) 5556 ComputePtrOff(); 5557 5558 assert(HasParameterArea && 5559 "Parameter area must exist to pass an argument in memory."); 5560 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 5561 true, isTailCall, false, MemOpChains, 5562 TailCallArguments, dl); 5563 if (CallConv == CallingConv::Fast) 5564 ArgOffset += PtrByteSize; 5565 } 5566 if (CallConv != CallingConv::Fast) 5567 ArgOffset += PtrByteSize; 5568 break; 5569 case MVT::f32: 5570 case MVT::f64: { 5571 // These can be scalar arguments or elements of a float array type 5572 // passed directly. The latter are used to implement ELFv2 homogenous 5573 // float aggregates. 5574 5575 // Named arguments go into FPRs first, and once they overflow, the 5576 // remaining arguments go into GPRs and then the parameter save area. 5577 // Unnamed arguments for vararg functions always go to GPRs and 5578 // then the parameter save area. For now, put all arguments to vararg 5579 // routines always in both locations (FPR *and* GPR or stack slot). 5580 bool NeedGPROrStack = isVarArg || FPR_idx == NumFPRs; 5581 bool NeededLoad = false; 5582 5583 // First load the argument into the next available FPR. 5584 if (FPR_idx != NumFPRs) 5585 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg)); 5586 5587 // Next, load the argument into GPR or stack slot if needed. 5588 if (!NeedGPROrStack) 5589 ; 5590 else if (GPR_idx != NumGPRs && CallConv != CallingConv::Fast) { 5591 // FIXME: We may want to re-enable this for CallingConv::Fast on the P8 5592 // once we support fp <-> gpr moves. 5593 5594 // In the non-vararg case, this can only ever happen in the 5595 // presence of f32 array types, since otherwise we never run 5596 // out of FPRs before running out of GPRs. 5597 SDValue ArgVal; 5598 5599 // Double values are always passed in a single GPR. 5600 if (Arg.getValueType() != MVT::f32) { 5601 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg); 5602 5603 // Non-array float values are extended and passed in a GPR. 5604 } else if (!Flags.isInConsecutiveRegs()) { 5605 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg); 5606 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal); 5607 5608 // If we have an array of floats, we collect every odd element 5609 // together with its predecessor into one GPR. 5610 } else if (ArgOffset % PtrByteSize != 0) { 5611 SDValue Lo, Hi; 5612 Lo = DAG.getNode(ISD::BITCAST, dl, MVT::i32, OutVals[i - 1]); 5613 Hi = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg); 5614 if (!isLittleEndian) 5615 std::swap(Lo, Hi); 5616 ArgVal = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 5617 5618 // The final element, if even, goes into the first half of a GPR. 5619 } else if (Flags.isInConsecutiveRegsLast()) { 5620 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg); 5621 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal); 5622 if (!isLittleEndian) 5623 ArgVal = DAG.getNode(ISD::SHL, dl, MVT::i64, ArgVal, 5624 DAG.getConstant(32, dl, MVT::i32)); 5625 5626 // Non-final even elements are skipped; they will be handled 5627 // together the with subsequent argument on the next go-around. 5628 } else 5629 ArgVal = SDValue(); 5630 5631 if (ArgVal.getNode()) 5632 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], ArgVal)); 5633 } else { 5634 if (CallConv == CallingConv::Fast) 5635 ComputePtrOff(); 5636 5637 // Single-precision floating-point values are mapped to the 5638 // second (rightmost) word of the stack doubleword. 5639 if (Arg.getValueType() == MVT::f32 && 5640 !isLittleEndian && !Flags.isInConsecutiveRegs()) { 5641 SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType()); 5642 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour); 5643 } 5644 5645 assert(HasParameterArea && 5646 "Parameter area must exist to pass an argument in memory."); 5647 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 5648 true, isTailCall, false, MemOpChains, 5649 TailCallArguments, dl); 5650 5651 NeededLoad = true; 5652 } 5653 // When passing an array of floats, the array occupies consecutive 5654 // space in the argument area; only round up to the next doubleword 5655 // at the end of the array. Otherwise, each float takes 8 bytes. 5656 if (CallConv != CallingConv::Fast || NeededLoad) { 5657 ArgOffset += (Arg.getValueType() == MVT::f32 && 5658 Flags.isInConsecutiveRegs()) ? 4 : 8; 5659 if (Flags.isInConsecutiveRegsLast()) 5660 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; 5661 } 5662 break; 5663 } 5664 case MVT::v4f32: 5665 case MVT::v4i32: 5666 case MVT::v8i16: 5667 case MVT::v16i8: 5668 case MVT::v2f64: 5669 case MVT::v2i64: 5670 case MVT::v1i128: 5671 if (!Subtarget.hasQPX()) { 5672 // These can be scalar arguments or elements of a vector array type 5673 // passed directly. The latter are used to implement ELFv2 homogenous 5674 // vector aggregates. 5675 5676 // For a varargs call, named arguments go into VRs or on the stack as 5677 // usual; unnamed arguments always go to the stack or the corresponding 5678 // GPRs when within range. For now, we always put the value in both 5679 // locations (or even all three). 5680 if (isVarArg) { 5681 assert(HasParameterArea && 5682 "Parameter area must exist if we have a varargs call."); 5683 // We could elide this store in the case where the object fits 5684 // entirely in R registers. Maybe later. 5685 SDValue Store = 5686 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()); 5687 MemOpChains.push_back(Store); 5688 if (VR_idx != NumVRs) { 5689 SDValue Load = 5690 DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, MachinePointerInfo()); 5691 MemOpChains.push_back(Load.getValue(1)); 5692 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load)); 5693 } 5694 ArgOffset += 16; 5695 for (unsigned i=0; i<16; i+=PtrByteSize) { 5696 if (GPR_idx == NumGPRs) 5697 break; 5698 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, 5699 DAG.getConstant(i, dl, PtrVT)); 5700 SDValue Load = 5701 DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo()); 5702 MemOpChains.push_back(Load.getValue(1)); 5703 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5704 } 5705 break; 5706 } 5707 5708 // Non-varargs Altivec params go into VRs or on the stack. 5709 if (VR_idx != NumVRs) { 5710 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg)); 5711 } else { 5712 if (CallConv == CallingConv::Fast) 5713 ComputePtrOff(); 5714 5715 assert(HasParameterArea && 5716 "Parameter area must exist to pass an argument in memory."); 5717 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 5718 true, isTailCall, true, MemOpChains, 5719 TailCallArguments, dl); 5720 if (CallConv == CallingConv::Fast) 5721 ArgOffset += 16; 5722 } 5723 5724 if (CallConv != CallingConv::Fast) 5725 ArgOffset += 16; 5726 break; 5727 } // not QPX 5728 5729 assert(Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32 && 5730 "Invalid QPX parameter type"); 5731 5732 /* fall through */ 5733 case MVT::v4f64: 5734 case MVT::v4i1: { 5735 bool IsF32 = Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32; 5736 if (isVarArg) { 5737 assert(HasParameterArea && 5738 "Parameter area must exist if we have a varargs call."); 5739 // We could elide this store in the case where the object fits 5740 // entirely in R registers. Maybe later. 5741 SDValue Store = 5742 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()); 5743 MemOpChains.push_back(Store); 5744 if (QFPR_idx != NumQFPRs) { 5745 SDValue Load = DAG.getLoad(IsF32 ? MVT::v4f32 : MVT::v4f64, dl, Store, 5746 PtrOff, MachinePointerInfo()); 5747 MemOpChains.push_back(Load.getValue(1)); 5748 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Load)); 5749 } 5750 ArgOffset += (IsF32 ? 16 : 32); 5751 for (unsigned i = 0; i < (IsF32 ? 16U : 32U); i += PtrByteSize) { 5752 if (GPR_idx == NumGPRs) 5753 break; 5754 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, 5755 DAG.getConstant(i, dl, PtrVT)); 5756 SDValue Load = 5757 DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo()); 5758 MemOpChains.push_back(Load.getValue(1)); 5759 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 5760 } 5761 break; 5762 } 5763 5764 // Non-varargs QPX params go into registers or on the stack. 5765 if (QFPR_idx != NumQFPRs) { 5766 RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Arg)); 5767 } else { 5768 if (CallConv == CallingConv::Fast) 5769 ComputePtrOff(); 5770 5771 assert(HasParameterArea && 5772 "Parameter area must exist to pass an argument in memory."); 5773 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 5774 true, isTailCall, true, MemOpChains, 5775 TailCallArguments, dl); 5776 if (CallConv == CallingConv::Fast) 5777 ArgOffset += (IsF32 ? 16 : 32); 5778 } 5779 5780 if (CallConv != CallingConv::Fast) 5781 ArgOffset += (IsF32 ? 16 : 32); 5782 break; 5783 } 5784 } 5785 } 5786 5787 assert((!HasParameterArea || NumBytesActuallyUsed == ArgOffset) && 5788 "mismatch in size of parameter area"); 5789 (void)NumBytesActuallyUsed; 5790 5791 if (!MemOpChains.empty()) 5792 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 5793 5794 // Check if this is an indirect call (MTCTR/BCTRL). 5795 // See PrepareCall() for more information about calls through function 5796 // pointers in the 64-bit SVR4 ABI. 5797 if (!isTailCall && !isPatchPoint && 5798 !isFunctionGlobalAddress(Callee) && 5799 !isa<ExternalSymbolSDNode>(Callee)) { 5800 // Load r2 into a virtual register and store it to the TOC save area. 5801 setUsesTOCBasePtr(DAG); 5802 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64); 5803 // TOC save area offset. 5804 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset(); 5805 SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset, dl); 5806 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); 5807 Chain = DAG.getStore( 5808 Val.getValue(1), dl, Val, AddPtr, 5809 MachinePointerInfo::getStack(DAG.getMachineFunction(), TOCSaveOffset)); 5810 // In the ELFv2 ABI, R12 must contain the address of an indirect callee. 5811 // This does not mean the MTCTR instruction must use R12; it's easier 5812 // to model this as an extra parameter, so do that. 5813 if (isELFv2ABI && !isPatchPoint) 5814 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee)); 5815 } 5816 5817 // Build a sequence of copy-to-reg nodes chained together with token chain 5818 // and flag operands which copy the outgoing args into the appropriate regs. 5819 SDValue InFlag; 5820 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 5821 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 5822 RegsToPass[i].second, InFlag); 5823 InFlag = Chain.getValue(1); 5824 } 5825 5826 if (isTailCall && !IsSibCall) 5827 PrepareTailCall(DAG, InFlag, Chain, dl, SPDiff, NumBytes, LROp, FPOp, 5828 TailCallArguments); 5829 5830 return FinishCall(CallConv, dl, isTailCall, isVarArg, isPatchPoint, hasNest, 5831 DAG, RegsToPass, InFlag, Chain, CallSeqStart, Callee, 5832 SPDiff, NumBytes, Ins, InVals, CS); 5833 } 5834 5835 SDValue PPCTargetLowering::LowerCall_Darwin( 5836 SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg, 5837 bool isTailCall, bool isPatchPoint, 5838 const SmallVectorImpl<ISD::OutputArg> &Outs, 5839 const SmallVectorImpl<SDValue> &OutVals, 5840 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 5841 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, 5842 ImmutableCallSite *CS) const { 5843 unsigned NumOps = Outs.size(); 5844 5845 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 5846 bool isPPC64 = PtrVT == MVT::i64; 5847 unsigned PtrByteSize = isPPC64 ? 8 : 4; 5848 5849 MachineFunction &MF = DAG.getMachineFunction(); 5850 5851 // Mark this function as potentially containing a function that contains a 5852 // tail call. As a consequence the frame pointer will be used for dynamicalloc 5853 // and restoring the callers stack pointer in this functions epilog. This is 5854 // done because by tail calling the called function might overwrite the value 5855 // in this function's (MF) stack pointer stack slot 0(SP). 5856 if (getTargetMachine().Options.GuaranteedTailCallOpt && 5857 CallConv == CallingConv::Fast) 5858 MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); 5859 5860 // Count how many bytes are to be pushed on the stack, including the linkage 5861 // area, and parameter passing area. We start with 24/48 bytes, which is 5862 // prereserved space for [SP][CR][LR][3 x unused]. 5863 unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); 5864 unsigned NumBytes = LinkageSize; 5865 5866 // Add up all the space actually used. 5867 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually 5868 // they all go in registers, but we must reserve stack space for them for 5869 // possible use by the caller. In varargs or 64-bit calls, parameters are 5870 // assigned stack space in order, with padding so Altivec parameters are 5871 // 16-byte aligned. 5872 unsigned nAltivecParamsAtEnd = 0; 5873 for (unsigned i = 0; i != NumOps; ++i) { 5874 ISD::ArgFlagsTy Flags = Outs[i].Flags; 5875 EVT ArgVT = Outs[i].VT; 5876 // Varargs Altivec parameters are padded to a 16 byte boundary. 5877 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 || 5878 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 || 5879 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64) { 5880 if (!isVarArg && !isPPC64) { 5881 // Non-varargs Altivec parameters go after all the non-Altivec 5882 // parameters; handle those later so we know how much padding we need. 5883 nAltivecParamsAtEnd++; 5884 continue; 5885 } 5886 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary. 5887 NumBytes = ((NumBytes+15)/16)*16; 5888 } 5889 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize); 5890 } 5891 5892 // Allow for Altivec parameters at the end, if needed. 5893 if (nAltivecParamsAtEnd) { 5894 NumBytes = ((NumBytes+15)/16)*16; 5895 NumBytes += 16*nAltivecParamsAtEnd; 5896 } 5897 5898 // The prolog code of the callee may store up to 8 GPR argument registers to 5899 // the stack, allowing va_start to index over them in memory if its varargs. 5900 // Because we cannot tell if this is needed on the caller side, we have to 5901 // conservatively assume that it is needed. As such, make sure we have at 5902 // least enough stack space for the caller to store the 8 GPRs. 5903 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize); 5904 5905 // Tail call needs the stack to be aligned. 5906 if (getTargetMachine().Options.GuaranteedTailCallOpt && 5907 CallConv == CallingConv::Fast) 5908 NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes); 5909 5910 // Calculate by how many bytes the stack has to be adjusted in case of tail 5911 // call optimization. 5912 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); 5913 5914 // To protect arguments on the stack from being clobbered in a tail call, 5915 // force all the loads to happen before doing any other lowering. 5916 if (isTailCall) 5917 Chain = DAG.getStackArgumentTokenFactor(Chain); 5918 5919 // Adjust the stack pointer for the new arguments... 5920 // These operations are automatically eliminated by the prolog/epilog pass 5921 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl); 5922 SDValue CallSeqStart = Chain; 5923 5924 // Load the return address and frame pointer so it can be move somewhere else 5925 // later. 5926 SDValue LROp, FPOp; 5927 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, dl); 5928 5929 // Set up a copy of the stack pointer for use loading and storing any 5930 // arguments that may not fit in the registers available for argument 5931 // passing. 5932 SDValue StackPtr; 5933 if (isPPC64) 5934 StackPtr = DAG.getRegister(PPC::X1, MVT::i64); 5935 else 5936 StackPtr = DAG.getRegister(PPC::R1, MVT::i32); 5937 5938 // Figure out which arguments are going to go in registers, and which in 5939 // memory. Also, if this is a vararg function, floating point operations 5940 // must be stored to our stack, and loaded into integer regs as well, if 5941 // any integer regs are available for argument passing. 5942 unsigned ArgOffset = LinkageSize; 5943 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; 5944 5945 static const MCPhysReg GPR_32[] = { // 32-bit registers. 5946 PPC::R3, PPC::R4, PPC::R5, PPC::R6, 5947 PPC::R7, PPC::R8, PPC::R9, PPC::R10, 5948 }; 5949 static const MCPhysReg GPR_64[] = { // 64-bit registers. 5950 PPC::X3, PPC::X4, PPC::X5, PPC::X6, 5951 PPC::X7, PPC::X8, PPC::X9, PPC::X10, 5952 }; 5953 static const MCPhysReg VR[] = { 5954 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, 5955 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 5956 }; 5957 const unsigned NumGPRs = array_lengthof(GPR_32); 5958 const unsigned NumFPRs = 13; 5959 const unsigned NumVRs = array_lengthof(VR); 5960 5961 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32; 5962 5963 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 5964 SmallVector<TailCallArgumentInfo, 8> TailCallArguments; 5965 5966 SmallVector<SDValue, 8> MemOpChains; 5967 for (unsigned i = 0; i != NumOps; ++i) { 5968 SDValue Arg = OutVals[i]; 5969 ISD::ArgFlagsTy Flags = Outs[i].Flags; 5970 5971 // PtrOff will be used to store the current argument to the stack if a 5972 // register cannot be found for it. 5973 SDValue PtrOff; 5974 5975 PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType()); 5976 5977 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); 5978 5979 // On PPC64, promote integers to 64-bit values. 5980 if (isPPC64 && Arg.getValueType() == MVT::i32) { 5981 // FIXME: Should this use ANY_EXTEND if neither sext nor zext? 5982 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 5983 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg); 5984 } 5985 5986 // FIXME memcpy is used way more than necessary. Correctness first. 5987 // Note: "by value" is code for passing a structure by value, not 5988 // basic types. 5989 if (Flags.isByVal()) { 5990 unsigned Size = Flags.getByValSize(); 5991 // Very small objects are passed right-justified. Everything else is 5992 // passed left-justified. 5993 if (Size==1 || Size==2) { 5994 EVT VT = (Size==1) ? MVT::i8 : MVT::i16; 5995 if (GPR_idx != NumGPRs) { 5996 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg, 5997 MachinePointerInfo(), VT); 5998 MemOpChains.push_back(Load.getValue(1)); 5999 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 6000 6001 ArgOffset += PtrByteSize; 6002 } else { 6003 SDValue Const = DAG.getConstant(PtrByteSize - Size, dl, 6004 PtrOff.getValueType()); 6005 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); 6006 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr, 6007 CallSeqStart, 6008 Flags, DAG, dl); 6009 ArgOffset += PtrByteSize; 6010 } 6011 continue; 6012 } 6013 // Copy entire object into memory. There are cases where gcc-generated 6014 // code assumes it is there, even if it could be put entirely into 6015 // registers. (This is not what the doc says.) 6016 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff, 6017 CallSeqStart, 6018 Flags, DAG, dl); 6019 6020 // For small aggregates (Darwin only) and aggregates >= PtrByteSize, 6021 // copy the pieces of the object that fit into registers from the 6022 // parameter save area. 6023 for (unsigned j=0; j<Size; j+=PtrByteSize) { 6024 SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType()); 6025 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 6026 if (GPR_idx != NumGPRs) { 6027 SDValue Load = 6028 DAG.getLoad(PtrVT, dl, Chain, AddArg, MachinePointerInfo()); 6029 MemOpChains.push_back(Load.getValue(1)); 6030 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 6031 ArgOffset += PtrByteSize; 6032 } else { 6033 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize; 6034 break; 6035 } 6036 } 6037 continue; 6038 } 6039 6040 switch (Arg.getSimpleValueType().SimpleTy) { 6041 default: llvm_unreachable("Unexpected ValueType for argument!"); 6042 case MVT::i1: 6043 case MVT::i32: 6044 case MVT::i64: 6045 if (GPR_idx != NumGPRs) { 6046 if (Arg.getValueType() == MVT::i1) 6047 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg); 6048 6049 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg)); 6050 } else { 6051 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 6052 isPPC64, isTailCall, false, MemOpChains, 6053 TailCallArguments, dl); 6054 } 6055 ArgOffset += PtrByteSize; 6056 break; 6057 case MVT::f32: 6058 case MVT::f64: 6059 if (FPR_idx != NumFPRs) { 6060 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg)); 6061 6062 if (isVarArg) { 6063 SDValue Store = 6064 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()); 6065 MemOpChains.push_back(Store); 6066 6067 // Float varargs are always shadowed in available integer registers 6068 if (GPR_idx != NumGPRs) { 6069 SDValue Load = 6070 DAG.getLoad(PtrVT, dl, Store, PtrOff, MachinePointerInfo()); 6071 MemOpChains.push_back(Load.getValue(1)); 6072 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 6073 } 6074 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){ 6075 SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType()); 6076 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour); 6077 SDValue Load = 6078 DAG.getLoad(PtrVT, dl, Store, PtrOff, MachinePointerInfo()); 6079 MemOpChains.push_back(Load.getValue(1)); 6080 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 6081 } 6082 } else { 6083 // If we have any FPRs remaining, we may also have GPRs remaining. 6084 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available 6085 // GPRs. 6086 if (GPR_idx != NumGPRs) 6087 ++GPR_idx; 6088 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && 6089 !isPPC64) // PPC64 has 64-bit GPR's obviously :) 6090 ++GPR_idx; 6091 } 6092 } else 6093 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 6094 isPPC64, isTailCall, false, MemOpChains, 6095 TailCallArguments, dl); 6096 if (isPPC64) 6097 ArgOffset += 8; 6098 else 6099 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8; 6100 break; 6101 case MVT::v4f32: 6102 case MVT::v4i32: 6103 case MVT::v8i16: 6104 case MVT::v16i8: 6105 if (isVarArg) { 6106 // These go aligned on the stack, or in the corresponding R registers 6107 // when within range. The Darwin PPC ABI doc claims they also go in 6108 // V registers; in fact gcc does this only for arguments that are 6109 // prototyped, not for those that match the ... We do it for all 6110 // arguments, seems to work. 6111 while (ArgOffset % 16 !=0) { 6112 ArgOffset += PtrByteSize; 6113 if (GPR_idx != NumGPRs) 6114 GPR_idx++; 6115 } 6116 // We could elide this store in the case where the object fits 6117 // entirely in R registers. Maybe later. 6118 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, 6119 DAG.getConstant(ArgOffset, dl, PtrVT)); 6120 SDValue Store = 6121 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()); 6122 MemOpChains.push_back(Store); 6123 if (VR_idx != NumVRs) { 6124 SDValue Load = 6125 DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, MachinePointerInfo()); 6126 MemOpChains.push_back(Load.getValue(1)); 6127 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load)); 6128 } 6129 ArgOffset += 16; 6130 for (unsigned i=0; i<16; i+=PtrByteSize) { 6131 if (GPR_idx == NumGPRs) 6132 break; 6133 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, 6134 DAG.getConstant(i, dl, PtrVT)); 6135 SDValue Load = 6136 DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo()); 6137 MemOpChains.push_back(Load.getValue(1)); 6138 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); 6139 } 6140 break; 6141 } 6142 6143 // Non-varargs Altivec params generally go in registers, but have 6144 // stack space allocated at the end. 6145 if (VR_idx != NumVRs) { 6146 // Doesn't have GPR space allocated. 6147 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg)); 6148 } else if (nAltivecParamsAtEnd==0) { 6149 // We are emitting Altivec params in order. 6150 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 6151 isPPC64, isTailCall, true, MemOpChains, 6152 TailCallArguments, dl); 6153 ArgOffset += 16; 6154 } 6155 break; 6156 } 6157 } 6158 // If all Altivec parameters fit in registers, as they usually do, 6159 // they get stack space following the non-Altivec parameters. We 6160 // don't track this here because nobody below needs it. 6161 // If there are more Altivec parameters than fit in registers emit 6162 // the stores here. 6163 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) { 6164 unsigned j = 0; 6165 // Offset is aligned; skip 1st 12 params which go in V registers. 6166 ArgOffset = ((ArgOffset+15)/16)*16; 6167 ArgOffset += 12*16; 6168 for (unsigned i = 0; i != NumOps; ++i) { 6169 SDValue Arg = OutVals[i]; 6170 EVT ArgType = Outs[i].VT; 6171 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 || 6172 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) { 6173 if (++j > NumVRs) { 6174 SDValue PtrOff; 6175 // We are emitting Altivec params in order. 6176 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, 6177 isPPC64, isTailCall, true, MemOpChains, 6178 TailCallArguments, dl); 6179 ArgOffset += 16; 6180 } 6181 } 6182 } 6183 } 6184 6185 if (!MemOpChains.empty()) 6186 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 6187 6188 // On Darwin, R12 must contain the address of an indirect callee. This does 6189 // not mean the MTCTR instruction must use R12; it's easier to model this as 6190 // an extra parameter, so do that. 6191 if (!isTailCall && 6192 !isFunctionGlobalAddress(Callee) && 6193 !isa<ExternalSymbolSDNode>(Callee) && 6194 !isBLACompatibleAddress(Callee, DAG)) 6195 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 : 6196 PPC::R12), Callee)); 6197 6198 // Build a sequence of copy-to-reg nodes chained together with token chain 6199 // and flag operands which copy the outgoing args into the appropriate regs. 6200 SDValue InFlag; 6201 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 6202 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 6203 RegsToPass[i].second, InFlag); 6204 InFlag = Chain.getValue(1); 6205 } 6206 6207 if (isTailCall) 6208 PrepareTailCall(DAG, InFlag, Chain, dl, SPDiff, NumBytes, LROp, FPOp, 6209 TailCallArguments); 6210 6211 return FinishCall(CallConv, dl, isTailCall, isVarArg, isPatchPoint, 6212 /* unused except on PPC64 ELFv1 */ false, DAG, 6213 RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff, 6214 NumBytes, Ins, InVals, CS); 6215 } 6216 6217 bool 6218 PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 6219 MachineFunction &MF, bool isVarArg, 6220 const SmallVectorImpl<ISD::OutputArg> &Outs, 6221 LLVMContext &Context) const { 6222 SmallVector<CCValAssign, 16> RVLocs; 6223 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context); 6224 return CCInfo.CheckReturn(Outs, RetCC_PPC); 6225 } 6226 6227 SDValue 6228 PPCTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 6229 bool isVarArg, 6230 const SmallVectorImpl<ISD::OutputArg> &Outs, 6231 const SmallVectorImpl<SDValue> &OutVals, 6232 const SDLoc &dl, SelectionDAG &DAG) const { 6233 SmallVector<CCValAssign, 16> RVLocs; 6234 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 6235 *DAG.getContext()); 6236 CCInfo.AnalyzeReturn(Outs, RetCC_PPC); 6237 6238 SDValue Flag; 6239 SmallVector<SDValue, 4> RetOps(1, Chain); 6240 6241 // Copy the result values into the output registers. 6242 for (unsigned i = 0; i != RVLocs.size(); ++i) { 6243 CCValAssign &VA = RVLocs[i]; 6244 assert(VA.isRegLoc() && "Can only return in registers!"); 6245 6246 SDValue Arg = OutVals[i]; 6247 6248 switch (VA.getLocInfo()) { 6249 default: llvm_unreachable("Unknown loc info!"); 6250 case CCValAssign::Full: break; 6251 case CCValAssign::AExt: 6252 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 6253 break; 6254 case CCValAssign::ZExt: 6255 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 6256 break; 6257 case CCValAssign::SExt: 6258 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 6259 break; 6260 } 6261 6262 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); 6263 Flag = Chain.getValue(1); 6264 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 6265 } 6266 6267 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo(); 6268 const MCPhysReg *I = 6269 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction()); 6270 if (I) { 6271 for (; *I; ++I) { 6272 6273 if (PPC::G8RCRegClass.contains(*I)) 6274 RetOps.push_back(DAG.getRegister(*I, MVT::i64)); 6275 else if (PPC::F8RCRegClass.contains(*I)) 6276 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64))); 6277 else if (PPC::CRRCRegClass.contains(*I)) 6278 RetOps.push_back(DAG.getRegister(*I, MVT::i1)); 6279 else if (PPC::VRRCRegClass.contains(*I)) 6280 RetOps.push_back(DAG.getRegister(*I, MVT::Other)); 6281 else 6282 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 6283 } 6284 } 6285 6286 RetOps[0] = Chain; // Update chain. 6287 6288 // Add the flag if we have it. 6289 if (Flag.getNode()) 6290 RetOps.push_back(Flag); 6291 6292 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps); 6293 } 6294 6295 SDValue 6296 PPCTargetLowering::LowerGET_DYNAMIC_AREA_OFFSET(SDValue Op, 6297 SelectionDAG &DAG) const { 6298 SDLoc dl(Op); 6299 6300 // Get the corect type for integers. 6301 EVT IntVT = Op.getValueType(); 6302 6303 // Get the inputs. 6304 SDValue Chain = Op.getOperand(0); 6305 SDValue FPSIdx = getFramePointerFrameIndex(DAG); 6306 // Build a DYNAREAOFFSET node. 6307 SDValue Ops[2] = {Chain, FPSIdx}; 6308 SDVTList VTs = DAG.getVTList(IntVT); 6309 return DAG.getNode(PPCISD::DYNAREAOFFSET, dl, VTs, Ops); 6310 } 6311 6312 SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, 6313 SelectionDAG &DAG) const { 6314 // When we pop the dynamic allocation we need to restore the SP link. 6315 SDLoc dl(Op); 6316 6317 // Get the corect type for pointers. 6318 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 6319 6320 // Construct the stack pointer operand. 6321 bool isPPC64 = Subtarget.isPPC64(); 6322 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1; 6323 SDValue StackPtr = DAG.getRegister(SP, PtrVT); 6324 6325 // Get the operands for the STACKRESTORE. 6326 SDValue Chain = Op.getOperand(0); 6327 SDValue SaveSP = Op.getOperand(1); 6328 6329 // Load the old link SP. 6330 SDValue LoadLinkSP = 6331 DAG.getLoad(PtrVT, dl, Chain, StackPtr, MachinePointerInfo()); 6332 6333 // Restore the stack pointer. 6334 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP); 6335 6336 // Store the old link SP. 6337 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo()); 6338 } 6339 6340 SDValue PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG &DAG) const { 6341 MachineFunction &MF = DAG.getMachineFunction(); 6342 bool isPPC64 = Subtarget.isPPC64(); 6343 EVT PtrVT = getPointerTy(MF.getDataLayout()); 6344 6345 // Get current frame pointer save index. The users of this index will be 6346 // primarily DYNALLOC instructions. 6347 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 6348 int RASI = FI->getReturnAddrSaveIndex(); 6349 6350 // If the frame pointer save index hasn't been defined yet. 6351 if (!RASI) { 6352 // Find out what the fix offset of the frame pointer save area. 6353 int LROffset = Subtarget.getFrameLowering()->getReturnSaveOffset(); 6354 // Allocate the frame index for frame pointer save area. 6355 RASI = MF.getFrameInfo().CreateFixedObject(isPPC64? 8 : 4, LROffset, false); 6356 // Save the result. 6357 FI->setReturnAddrSaveIndex(RASI); 6358 } 6359 return DAG.getFrameIndex(RASI, PtrVT); 6360 } 6361 6362 SDValue 6363 PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const { 6364 MachineFunction &MF = DAG.getMachineFunction(); 6365 bool isPPC64 = Subtarget.isPPC64(); 6366 EVT PtrVT = getPointerTy(MF.getDataLayout()); 6367 6368 // Get current frame pointer save index. The users of this index will be 6369 // primarily DYNALLOC instructions. 6370 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 6371 int FPSI = FI->getFramePointerSaveIndex(); 6372 6373 // If the frame pointer save index hasn't been defined yet. 6374 if (!FPSI) { 6375 // Find out what the fix offset of the frame pointer save area. 6376 int FPOffset = Subtarget.getFrameLowering()->getFramePointerSaveOffset(); 6377 // Allocate the frame index for frame pointer save area. 6378 FPSI = MF.getFrameInfo().CreateFixedObject(isPPC64? 8 : 4, FPOffset, true); 6379 // Save the result. 6380 FI->setFramePointerSaveIndex(FPSI); 6381 } 6382 return DAG.getFrameIndex(FPSI, PtrVT); 6383 } 6384 6385 SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, 6386 SelectionDAG &DAG) const { 6387 // Get the inputs. 6388 SDValue Chain = Op.getOperand(0); 6389 SDValue Size = Op.getOperand(1); 6390 SDLoc dl(Op); 6391 6392 // Get the corect type for pointers. 6393 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 6394 // Negate the size. 6395 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT, 6396 DAG.getConstant(0, dl, PtrVT), Size); 6397 // Construct a node for the frame pointer save index. 6398 SDValue FPSIdx = getFramePointerFrameIndex(DAG); 6399 // Build a DYNALLOC node. 6400 SDValue Ops[3] = { Chain, NegSize, FPSIdx }; 6401 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other); 6402 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops); 6403 } 6404 6405 SDValue PPCTargetLowering::LowerEH_DWARF_CFA(SDValue Op, 6406 SelectionDAG &DAG) const { 6407 MachineFunction &MF = DAG.getMachineFunction(); 6408 6409 bool isPPC64 = Subtarget.isPPC64(); 6410 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 6411 6412 int FI = MF.getFrameInfo().CreateFixedObject(isPPC64 ? 8 : 4, 0, false); 6413 return DAG.getFrameIndex(FI, PtrVT); 6414 } 6415 6416 SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op, 6417 SelectionDAG &DAG) const { 6418 SDLoc DL(Op); 6419 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL, 6420 DAG.getVTList(MVT::i32, MVT::Other), 6421 Op.getOperand(0), Op.getOperand(1)); 6422 } 6423 6424 SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op, 6425 SelectionDAG &DAG) const { 6426 SDLoc DL(Op); 6427 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other, 6428 Op.getOperand(0), Op.getOperand(1)); 6429 } 6430 6431 SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { 6432 if (Op.getValueType().isVector()) 6433 return LowerVectorLoad(Op, DAG); 6434 6435 assert(Op.getValueType() == MVT::i1 && 6436 "Custom lowering only for i1 loads"); 6437 6438 // First, load 8 bits into 32 bits, then truncate to 1 bit. 6439 6440 SDLoc dl(Op); 6441 LoadSDNode *LD = cast<LoadSDNode>(Op); 6442 6443 SDValue Chain = LD->getChain(); 6444 SDValue BasePtr = LD->getBasePtr(); 6445 MachineMemOperand *MMO = LD->getMemOperand(); 6446 6447 SDValue NewLD = 6448 DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(DAG.getDataLayout()), Chain, 6449 BasePtr, MVT::i8, MMO); 6450 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD); 6451 6452 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) }; 6453 return DAG.getMergeValues(Ops, dl); 6454 } 6455 6456 SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { 6457 if (Op.getOperand(1).getValueType().isVector()) 6458 return LowerVectorStore(Op, DAG); 6459 6460 assert(Op.getOperand(1).getValueType() == MVT::i1 && 6461 "Custom lowering only for i1 stores"); 6462 6463 // First, zero extend to 32 bits, then use a truncating store to 8 bits. 6464 6465 SDLoc dl(Op); 6466 StoreSDNode *ST = cast<StoreSDNode>(Op); 6467 6468 SDValue Chain = ST->getChain(); 6469 SDValue BasePtr = ST->getBasePtr(); 6470 SDValue Value = ST->getValue(); 6471 MachineMemOperand *MMO = ST->getMemOperand(); 6472 6473 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(DAG.getDataLayout()), 6474 Value); 6475 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO); 6476 } 6477 6478 // FIXME: Remove this once the ANDI glue bug is fixed: 6479 SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const { 6480 assert(Op.getValueType() == MVT::i1 && 6481 "Custom lowering only for i1 results"); 6482 6483 SDLoc DL(Op); 6484 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1, 6485 Op.getOperand(0)); 6486 } 6487 6488 /// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when 6489 /// possible. 6490 SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 6491 // Not FP? Not a fsel. 6492 if (!Op.getOperand(0).getValueType().isFloatingPoint() || 6493 !Op.getOperand(2).getValueType().isFloatingPoint()) 6494 return Op; 6495 6496 // We might be able to do better than this under some circumstances, but in 6497 // general, fsel-based lowering of select is a finite-math-only optimization. 6498 // For more information, see section F.3 of the 2.06 ISA specification. 6499 if (!DAG.getTarget().Options.NoInfsFPMath || 6500 !DAG.getTarget().Options.NoNaNsFPMath) 6501 return Op; 6502 // TODO: Propagate flags from the select rather than global settings. 6503 SDNodeFlags Flags; 6504 Flags.setNoInfs(true); 6505 Flags.setNoNaNs(true); 6506 6507 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 6508 6509 EVT ResVT = Op.getValueType(); 6510 EVT CmpVT = Op.getOperand(0).getValueType(); 6511 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); 6512 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3); 6513 SDLoc dl(Op); 6514 6515 // If the RHS of the comparison is a 0.0, we don't need to do the 6516 // subtraction at all. 6517 SDValue Sel1; 6518 if (isFloatingPointZero(RHS)) 6519 switch (CC) { 6520 default: break; // SETUO etc aren't handled by fsel. 6521 case ISD::SETNE: 6522 std::swap(TV, FV); 6523 case ISD::SETEQ: 6524 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits 6525 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); 6526 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV); 6527 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits 6528 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1); 6529 return DAG.getNode(PPCISD::FSEL, dl, ResVT, 6530 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV); 6531 case ISD::SETULT: 6532 case ISD::SETLT: 6533 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt 6534 case ISD::SETOGE: 6535 case ISD::SETGE: 6536 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits 6537 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); 6538 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV); 6539 case ISD::SETUGT: 6540 case ISD::SETGT: 6541 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt 6542 case ISD::SETOLE: 6543 case ISD::SETLE: 6544 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits 6545 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); 6546 return DAG.getNode(PPCISD::FSEL, dl, ResVT, 6547 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV); 6548 } 6549 6550 SDValue Cmp; 6551 switch (CC) { 6552 default: break; // SETUO etc aren't handled by fsel. 6553 case ISD::SETNE: 6554 std::swap(TV, FV); 6555 case ISD::SETEQ: 6556 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, Flags); 6557 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits 6558 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); 6559 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); 6560 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits 6561 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1); 6562 return DAG.getNode(PPCISD::FSEL, dl, ResVT, 6563 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV); 6564 case ISD::SETULT: 6565 case ISD::SETLT: 6566 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, Flags); 6567 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits 6568 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); 6569 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV); 6570 case ISD::SETOGE: 6571 case ISD::SETGE: 6572 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, Flags); 6573 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits 6574 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); 6575 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); 6576 case ISD::SETUGT: 6577 case ISD::SETGT: 6578 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS, Flags); 6579 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits 6580 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); 6581 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV); 6582 case ISD::SETOLE: 6583 case ISD::SETLE: 6584 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS, Flags); 6585 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits 6586 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); 6587 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); 6588 } 6589 return Op; 6590 } 6591 6592 void PPCTargetLowering::LowerFP_TO_INTForReuse(SDValue Op, ReuseLoadInfo &RLI, 6593 SelectionDAG &DAG, 6594 const SDLoc &dl) const { 6595 assert(Op.getOperand(0).getValueType().isFloatingPoint()); 6596 SDValue Src = Op.getOperand(0); 6597 if (Src.getValueType() == MVT::f32) 6598 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src); 6599 6600 SDValue Tmp; 6601 switch (Op.getSimpleValueType().SimpleTy) { 6602 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!"); 6603 case MVT::i32: 6604 Tmp = DAG.getNode( 6605 Op.getOpcode() == ISD::FP_TO_SINT 6606 ? PPCISD::FCTIWZ 6607 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ), 6608 dl, MVT::f64, Src); 6609 break; 6610 case MVT::i64: 6611 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) && 6612 "i64 FP_TO_UINT is supported only with FPCVT"); 6613 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ : 6614 PPCISD::FCTIDUZ, 6615 dl, MVT::f64, Src); 6616 break; 6617 } 6618 6619 // Convert the FP value to an int value through memory. 6620 bool i32Stack = Op.getValueType() == MVT::i32 && Subtarget.hasSTFIWX() && 6621 (Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()); 6622 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64); 6623 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex(); 6624 MachinePointerInfo MPI = 6625 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 6626 6627 // Emit a store to the stack slot. 6628 SDValue Chain; 6629 if (i32Stack) { 6630 MachineFunction &MF = DAG.getMachineFunction(); 6631 MachineMemOperand *MMO = 6632 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4); 6633 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr }; 6634 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl, 6635 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO); 6636 } else 6637 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr, MPI); 6638 6639 // Result is a load from the stack slot. If loading 4 bytes, make sure to 6640 // add in a bias on big endian. 6641 if (Op.getValueType() == MVT::i32 && !i32Stack) { 6642 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, 6643 DAG.getConstant(4, dl, FIPtr.getValueType())); 6644 MPI = MPI.getWithOffset(Subtarget.isLittleEndian() ? 0 : 4); 6645 } 6646 6647 RLI.Chain = Chain; 6648 RLI.Ptr = FIPtr; 6649 RLI.MPI = MPI; 6650 } 6651 6652 /// \brief Custom lowers floating point to integer conversions to use 6653 /// the direct move instructions available in ISA 2.07 to avoid the 6654 /// need for load/store combinations. 6655 SDValue PPCTargetLowering::LowerFP_TO_INTDirectMove(SDValue Op, 6656 SelectionDAG &DAG, 6657 const SDLoc &dl) const { 6658 assert(Op.getOperand(0).getValueType().isFloatingPoint()); 6659 SDValue Src = Op.getOperand(0); 6660 6661 if (Src.getValueType() == MVT::f32) 6662 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src); 6663 6664 SDValue Tmp; 6665 switch (Op.getSimpleValueType().SimpleTy) { 6666 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!"); 6667 case MVT::i32: 6668 Tmp = DAG.getNode( 6669 Op.getOpcode() == ISD::FP_TO_SINT 6670 ? PPCISD::FCTIWZ 6671 : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ), 6672 dl, MVT::f64, Src); 6673 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i32, Tmp); 6674 break; 6675 case MVT::i64: 6676 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) && 6677 "i64 FP_TO_UINT is supported only with FPCVT"); 6678 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ : 6679 PPCISD::FCTIDUZ, 6680 dl, MVT::f64, Src); 6681 Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i64, Tmp); 6682 break; 6683 } 6684 return Tmp; 6685 } 6686 6687 SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG, 6688 const SDLoc &dl) const { 6689 if (Subtarget.hasDirectMove() && Subtarget.isPPC64()) 6690 return LowerFP_TO_INTDirectMove(Op, DAG, dl); 6691 6692 ReuseLoadInfo RLI; 6693 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl); 6694 6695 return DAG.getLoad(Op.getValueType(), dl, RLI.Chain, RLI.Ptr, RLI.MPI, 6696 RLI.Alignment, RLI.MMOFlags(), RLI.AAInfo, RLI.Ranges); 6697 } 6698 6699 // We're trying to insert a regular store, S, and then a load, L. If the 6700 // incoming value, O, is a load, we might just be able to have our load use the 6701 // address used by O. However, we don't know if anything else will store to 6702 // that address before we can load from it. To prevent this situation, we need 6703 // to insert our load, L, into the chain as a peer of O. To do this, we give L 6704 // the same chain operand as O, we create a token factor from the chain results 6705 // of O and L, and we replace all uses of O's chain result with that token 6706 // factor (see spliceIntoChain below for this last part). 6707 bool PPCTargetLowering::canReuseLoadAddress(SDValue Op, EVT MemVT, 6708 ReuseLoadInfo &RLI, 6709 SelectionDAG &DAG, 6710 ISD::LoadExtType ET) const { 6711 SDLoc dl(Op); 6712 if (ET == ISD::NON_EXTLOAD && 6713 (Op.getOpcode() == ISD::FP_TO_UINT || 6714 Op.getOpcode() == ISD::FP_TO_SINT) && 6715 isOperationLegalOrCustom(Op.getOpcode(), 6716 Op.getOperand(0).getValueType())) { 6717 6718 LowerFP_TO_INTForReuse(Op, RLI, DAG, dl); 6719 return true; 6720 } 6721 6722 LoadSDNode *LD = dyn_cast<LoadSDNode>(Op); 6723 if (!LD || LD->getExtensionType() != ET || LD->isVolatile() || 6724 LD->isNonTemporal()) 6725 return false; 6726 if (LD->getMemoryVT() != MemVT) 6727 return false; 6728 6729 RLI.Ptr = LD->getBasePtr(); 6730 if (LD->isIndexed() && !LD->getOffset().isUndef()) { 6731 assert(LD->getAddressingMode() == ISD::PRE_INC && 6732 "Non-pre-inc AM on PPC?"); 6733 RLI.Ptr = DAG.getNode(ISD::ADD, dl, RLI.Ptr.getValueType(), RLI.Ptr, 6734 LD->getOffset()); 6735 } 6736 6737 RLI.Chain = LD->getChain(); 6738 RLI.MPI = LD->getPointerInfo(); 6739 RLI.IsDereferenceable = LD->isDereferenceable(); 6740 RLI.IsInvariant = LD->isInvariant(); 6741 RLI.Alignment = LD->getAlignment(); 6742 RLI.AAInfo = LD->getAAInfo(); 6743 RLI.Ranges = LD->getRanges(); 6744 6745 RLI.ResChain = SDValue(LD, LD->isIndexed() ? 2 : 1); 6746 return true; 6747 } 6748 6749 // Given the head of the old chain, ResChain, insert a token factor containing 6750 // it and NewResChain, and make users of ResChain now be users of that token 6751 // factor. 6752 void PPCTargetLowering::spliceIntoChain(SDValue ResChain, 6753 SDValue NewResChain, 6754 SelectionDAG &DAG) const { 6755 if (!ResChain) 6756 return; 6757 6758 SDLoc dl(NewResChain); 6759 6760 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 6761 NewResChain, DAG.getUNDEF(MVT::Other)); 6762 assert(TF.getNode() != NewResChain.getNode() && 6763 "A new TF really is required here"); 6764 6765 DAG.ReplaceAllUsesOfValueWith(ResChain, TF); 6766 DAG.UpdateNodeOperands(TF.getNode(), ResChain, NewResChain); 6767 } 6768 6769 /// \brief Analyze profitability of direct move 6770 /// prefer float load to int load plus direct move 6771 /// when there is no integer use of int load 6772 bool PPCTargetLowering::directMoveIsProfitable(const SDValue &Op) const { 6773 SDNode *Origin = Op.getOperand(0).getNode(); 6774 if (Origin->getOpcode() != ISD::LOAD) 6775 return true; 6776 6777 // If there is no LXSIBZX/LXSIHZX, like Power8, 6778 // prefer direct move if the memory size is 1 or 2 bytes. 6779 MachineMemOperand *MMO = cast<LoadSDNode>(Origin)->getMemOperand(); 6780 if (!Subtarget.hasP9Vector() && MMO->getSize() <= 2) 6781 return true; 6782 6783 for (SDNode::use_iterator UI = Origin->use_begin(), 6784 UE = Origin->use_end(); 6785 UI != UE; ++UI) { 6786 6787 // Only look at the users of the loaded value. 6788 if (UI.getUse().get().getResNo() != 0) 6789 continue; 6790 6791 if (UI->getOpcode() != ISD::SINT_TO_FP && 6792 UI->getOpcode() != ISD::UINT_TO_FP) 6793 return true; 6794 } 6795 6796 return false; 6797 } 6798 6799 /// \brief Custom lowers integer to floating point conversions to use 6800 /// the direct move instructions available in ISA 2.07 to avoid the 6801 /// need for load/store combinations. 6802 SDValue PPCTargetLowering::LowerINT_TO_FPDirectMove(SDValue Op, 6803 SelectionDAG &DAG, 6804 const SDLoc &dl) const { 6805 assert((Op.getValueType() == MVT::f32 || 6806 Op.getValueType() == MVT::f64) && 6807 "Invalid floating point type as target of conversion"); 6808 assert(Subtarget.hasFPCVT() && 6809 "Int to FP conversions with direct moves require FPCVT"); 6810 SDValue FP; 6811 SDValue Src = Op.getOperand(0); 6812 bool SinglePrec = Op.getValueType() == MVT::f32; 6813 bool WordInt = Src.getSimpleValueType().SimpleTy == MVT::i32; 6814 bool Signed = Op.getOpcode() == ISD::SINT_TO_FP; 6815 unsigned ConvOp = Signed ? (SinglePrec ? PPCISD::FCFIDS : PPCISD::FCFID) : 6816 (SinglePrec ? PPCISD::FCFIDUS : PPCISD::FCFIDU); 6817 6818 if (WordInt) { 6819 FP = DAG.getNode(Signed ? PPCISD::MTVSRA : PPCISD::MTVSRZ, 6820 dl, MVT::f64, Src); 6821 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP); 6822 } 6823 else { 6824 FP = DAG.getNode(PPCISD::MTVSRA, dl, MVT::f64, Src); 6825 FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP); 6826 } 6827 6828 return FP; 6829 } 6830 6831 SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op, 6832 SelectionDAG &DAG) const { 6833 SDLoc dl(Op); 6834 6835 if (Subtarget.hasQPX() && Op.getOperand(0).getValueType() == MVT::v4i1) { 6836 if (Op.getValueType() != MVT::v4f32 && Op.getValueType() != MVT::v4f64) 6837 return SDValue(); 6838 6839 SDValue Value = Op.getOperand(0); 6840 // The values are now known to be -1 (false) or 1 (true). To convert this 6841 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5). 6842 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5 6843 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value); 6844 6845 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64); 6846 6847 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs); 6848 6849 if (Op.getValueType() != MVT::v4f64) 6850 Value = DAG.getNode(ISD::FP_ROUND, dl, 6851 Op.getValueType(), Value, 6852 DAG.getIntPtrConstant(1, dl)); 6853 return Value; 6854 } 6855 6856 // Don't handle ppc_fp128 here; let it be lowered to a libcall. 6857 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64) 6858 return SDValue(); 6859 6860 if (Op.getOperand(0).getValueType() == MVT::i1) 6861 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0), 6862 DAG.getConstantFP(1.0, dl, Op.getValueType()), 6863 DAG.getConstantFP(0.0, dl, Op.getValueType())); 6864 6865 // If we have direct moves, we can do all the conversion, skip the store/load 6866 // however, without FPCVT we can't do most conversions. 6867 if (Subtarget.hasDirectMove() && directMoveIsProfitable(Op) && 6868 Subtarget.isPPC64() && Subtarget.hasFPCVT()) 6869 return LowerINT_TO_FPDirectMove(Op, DAG, dl); 6870 6871 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) && 6872 "UINT_TO_FP is supported only with FPCVT"); 6873 6874 // If we have FCFIDS, then use it when converting to single-precision. 6875 // Otherwise, convert to double-precision and then round. 6876 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) 6877 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS 6878 : PPCISD::FCFIDS) 6879 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU 6880 : PPCISD::FCFID); 6881 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) 6882 ? MVT::f32 6883 : MVT::f64; 6884 6885 if (Op.getOperand(0).getValueType() == MVT::i64) { 6886 SDValue SINT = Op.getOperand(0); 6887 // When converting to single-precision, we actually need to convert 6888 // to double-precision first and then round to single-precision. 6889 // To avoid double-rounding effects during that operation, we have 6890 // to prepare the input operand. Bits that might be truncated when 6891 // converting to double-precision are replaced by a bit that won't 6892 // be lost at this stage, but is below the single-precision rounding 6893 // position. 6894 // 6895 // However, if -enable-unsafe-fp-math is in effect, accept double 6896 // rounding to avoid the extra overhead. 6897 if (Op.getValueType() == MVT::f32 && 6898 !Subtarget.hasFPCVT() && 6899 !DAG.getTarget().Options.UnsafeFPMath) { 6900 6901 // Twiddle input to make sure the low 11 bits are zero. (If this 6902 // is the case, we are guaranteed the value will fit into the 53 bit 6903 // mantissa of an IEEE double-precision value without rounding.) 6904 // If any of those low 11 bits were not zero originally, make sure 6905 // bit 12 (value 2048) is set instead, so that the final rounding 6906 // to single-precision gets the correct result. 6907 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64, 6908 SINT, DAG.getConstant(2047, dl, MVT::i64)); 6909 Round = DAG.getNode(ISD::ADD, dl, MVT::i64, 6910 Round, DAG.getConstant(2047, dl, MVT::i64)); 6911 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT); 6912 Round = DAG.getNode(ISD::AND, dl, MVT::i64, 6913 Round, DAG.getConstant(-2048, dl, MVT::i64)); 6914 6915 // However, we cannot use that value unconditionally: if the magnitude 6916 // of the input value is small, the bit-twiddling we did above might 6917 // end up visibly changing the output. Fortunately, in that case, we 6918 // don't need to twiddle bits since the original input will convert 6919 // exactly to double-precision floating-point already. Therefore, 6920 // construct a conditional to use the original value if the top 11 6921 // bits are all sign-bit copies, and use the rounded value computed 6922 // above otherwise. 6923 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64, 6924 SINT, DAG.getConstant(53, dl, MVT::i32)); 6925 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64, 6926 Cond, DAG.getConstant(1, dl, MVT::i64)); 6927 Cond = DAG.getSetCC(dl, MVT::i32, 6928 Cond, DAG.getConstant(1, dl, MVT::i64), ISD::SETUGT); 6929 6930 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT); 6931 } 6932 6933 ReuseLoadInfo RLI; 6934 SDValue Bits; 6935 6936 MachineFunction &MF = DAG.getMachineFunction(); 6937 if (canReuseLoadAddress(SINT, MVT::i64, RLI, DAG)) { 6938 Bits = DAG.getLoad(MVT::f64, dl, RLI.Chain, RLI.Ptr, RLI.MPI, 6939 RLI.Alignment, RLI.MMOFlags(), RLI.AAInfo, RLI.Ranges); 6940 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG); 6941 } else if (Subtarget.hasLFIWAX() && 6942 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::SEXTLOAD)) { 6943 MachineMemOperand *MMO = 6944 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4, 6945 RLI.Alignment, RLI.AAInfo, RLI.Ranges); 6946 SDValue Ops[] = { RLI.Chain, RLI.Ptr }; 6947 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWAX, dl, 6948 DAG.getVTList(MVT::f64, MVT::Other), 6949 Ops, MVT::i32, MMO); 6950 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG); 6951 } else if (Subtarget.hasFPCVT() && 6952 canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::ZEXTLOAD)) { 6953 MachineMemOperand *MMO = 6954 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4, 6955 RLI.Alignment, RLI.AAInfo, RLI.Ranges); 6956 SDValue Ops[] = { RLI.Chain, RLI.Ptr }; 6957 Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWZX, dl, 6958 DAG.getVTList(MVT::f64, MVT::Other), 6959 Ops, MVT::i32, MMO); 6960 spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG); 6961 } else if (((Subtarget.hasLFIWAX() && 6962 SINT.getOpcode() == ISD::SIGN_EXTEND) || 6963 (Subtarget.hasFPCVT() && 6964 SINT.getOpcode() == ISD::ZERO_EXTEND)) && 6965 SINT.getOperand(0).getValueType() == MVT::i32) { 6966 MachineFrameInfo &MFI = MF.getFrameInfo(); 6967 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 6968 6969 int FrameIdx = MFI.CreateStackObject(4, 4, false); 6970 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 6971 6972 SDValue Store = 6973 DAG.getStore(DAG.getEntryNode(), dl, SINT.getOperand(0), FIdx, 6974 MachinePointerInfo::getFixedStack( 6975 DAG.getMachineFunction(), FrameIdx)); 6976 6977 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 && 6978 "Expected an i32 store"); 6979 6980 RLI.Ptr = FIdx; 6981 RLI.Chain = Store; 6982 RLI.MPI = 6983 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); 6984 RLI.Alignment = 4; 6985 6986 MachineMemOperand *MMO = 6987 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4, 6988 RLI.Alignment, RLI.AAInfo, RLI.Ranges); 6989 SDValue Ops[] = { RLI.Chain, RLI.Ptr }; 6990 Bits = DAG.getMemIntrinsicNode(SINT.getOpcode() == ISD::ZERO_EXTEND ? 6991 PPCISD::LFIWZX : PPCISD::LFIWAX, 6992 dl, DAG.getVTList(MVT::f64, MVT::Other), 6993 Ops, MVT::i32, MMO); 6994 } else 6995 Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT); 6996 6997 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits); 6998 6999 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) 7000 FP = DAG.getNode(ISD::FP_ROUND, dl, 7001 MVT::f32, FP, DAG.getIntPtrConstant(0, dl)); 7002 return FP; 7003 } 7004 7005 assert(Op.getOperand(0).getValueType() == MVT::i32 && 7006 "Unhandled INT_TO_FP type in custom expander!"); 7007 // Since we only generate this in 64-bit mode, we can take advantage of 7008 // 64-bit registers. In particular, sign extend the input value into the 7009 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack 7010 // then lfd it and fcfid it. 7011 MachineFunction &MF = DAG.getMachineFunction(); 7012 MachineFrameInfo &MFI = MF.getFrameInfo(); 7013 EVT PtrVT = getPointerTy(MF.getDataLayout()); 7014 7015 SDValue Ld; 7016 if (Subtarget.hasLFIWAX() || Subtarget.hasFPCVT()) { 7017 ReuseLoadInfo RLI; 7018 bool ReusingLoad; 7019 if (!(ReusingLoad = canReuseLoadAddress(Op.getOperand(0), MVT::i32, RLI, 7020 DAG))) { 7021 int FrameIdx = MFI.CreateStackObject(4, 4, false); 7022 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 7023 7024 SDValue Store = 7025 DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx, 7026 MachinePointerInfo::getFixedStack( 7027 DAG.getMachineFunction(), FrameIdx)); 7028 7029 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 && 7030 "Expected an i32 store"); 7031 7032 RLI.Ptr = FIdx; 7033 RLI.Chain = Store; 7034 RLI.MPI = 7035 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); 7036 RLI.Alignment = 4; 7037 } 7038 7039 MachineMemOperand *MMO = 7040 MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4, 7041 RLI.Alignment, RLI.AAInfo, RLI.Ranges); 7042 SDValue Ops[] = { RLI.Chain, RLI.Ptr }; 7043 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ? 7044 PPCISD::LFIWZX : PPCISD::LFIWAX, 7045 dl, DAG.getVTList(MVT::f64, MVT::Other), 7046 Ops, MVT::i32, MMO); 7047 if (ReusingLoad) 7048 spliceIntoChain(RLI.ResChain, Ld.getValue(1), DAG); 7049 } else { 7050 assert(Subtarget.isPPC64() && 7051 "i32->FP without LFIWAX supported only on PPC64"); 7052 7053 int FrameIdx = MFI.CreateStackObject(8, 8, false); 7054 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 7055 7056 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64, 7057 Op.getOperand(0)); 7058 7059 // STD the extended value into the stack slot. 7060 SDValue Store = DAG.getStore( 7061 DAG.getEntryNode(), dl, Ext64, FIdx, 7062 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx)); 7063 7064 // Load the value as a double. 7065 Ld = DAG.getLoad( 7066 MVT::f64, dl, Store, FIdx, 7067 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx)); 7068 } 7069 7070 // FCFID it and return it. 7071 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld); 7072 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) 7073 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, 7074 DAG.getIntPtrConstant(0, dl)); 7075 return FP; 7076 } 7077 7078 SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op, 7079 SelectionDAG &DAG) const { 7080 SDLoc dl(Op); 7081 /* 7082 The rounding mode is in bits 30:31 of FPSR, and has the following 7083 settings: 7084 00 Round to nearest 7085 01 Round to 0 7086 10 Round to +inf 7087 11 Round to -inf 7088 7089 FLT_ROUNDS, on the other hand, expects the following: 7090 -1 Undefined 7091 0 Round to 0 7092 1 Round to nearest 7093 2 Round to +inf 7094 3 Round to -inf 7095 7096 To perform the conversion, we do: 7097 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1)) 7098 */ 7099 7100 MachineFunction &MF = DAG.getMachineFunction(); 7101 EVT VT = Op.getValueType(); 7102 EVT PtrVT = getPointerTy(MF.getDataLayout()); 7103 7104 // Save FP Control Word to register 7105 EVT NodeTys[] = { 7106 MVT::f64, // return register 7107 MVT::Glue // unused in this context 7108 }; 7109 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None); 7110 7111 // Save FP register to stack slot 7112 int SSFI = MF.getFrameInfo().CreateStackObject(8, 8, false); 7113 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT); 7114 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain, StackSlot, 7115 MachinePointerInfo()); 7116 7117 // Load FP Control Word from low 32 bits of stack slot. 7118 SDValue Four = DAG.getConstant(4, dl, PtrVT); 7119 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four); 7120 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo()); 7121 7122 // Transform as necessary 7123 SDValue CWD1 = 7124 DAG.getNode(ISD::AND, dl, MVT::i32, 7125 CWD, DAG.getConstant(3, dl, MVT::i32)); 7126 SDValue CWD2 = 7127 DAG.getNode(ISD::SRL, dl, MVT::i32, 7128 DAG.getNode(ISD::AND, dl, MVT::i32, 7129 DAG.getNode(ISD::XOR, dl, MVT::i32, 7130 CWD, DAG.getConstant(3, dl, MVT::i32)), 7131 DAG.getConstant(3, dl, MVT::i32)), 7132 DAG.getConstant(1, dl, MVT::i32)); 7133 7134 SDValue RetVal = 7135 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2); 7136 7137 return DAG.getNode((VT.getSizeInBits() < 16 ? 7138 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal); 7139 } 7140 7141 SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const { 7142 EVT VT = Op.getValueType(); 7143 unsigned BitWidth = VT.getSizeInBits(); 7144 SDLoc dl(Op); 7145 assert(Op.getNumOperands() == 3 && 7146 VT == Op.getOperand(1).getValueType() && 7147 "Unexpected SHL!"); 7148 7149 // Expand into a bunch of logical ops. Note that these ops 7150 // depend on the PPC behavior for oversized shift amounts. 7151 SDValue Lo = Op.getOperand(0); 7152 SDValue Hi = Op.getOperand(1); 7153 SDValue Amt = Op.getOperand(2); 7154 EVT AmtVT = Amt.getValueType(); 7155 7156 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, 7157 DAG.getConstant(BitWidth, dl, AmtVT), Amt); 7158 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt); 7159 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1); 7160 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3); 7161 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, 7162 DAG.getConstant(-BitWidth, dl, AmtVT)); 7163 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5); 7164 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6); 7165 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt); 7166 SDValue OutOps[] = { OutLo, OutHi }; 7167 return DAG.getMergeValues(OutOps, dl); 7168 } 7169 7170 SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const { 7171 EVT VT = Op.getValueType(); 7172 SDLoc dl(Op); 7173 unsigned BitWidth = VT.getSizeInBits(); 7174 assert(Op.getNumOperands() == 3 && 7175 VT == Op.getOperand(1).getValueType() && 7176 "Unexpected SRL!"); 7177 7178 // Expand into a bunch of logical ops. Note that these ops 7179 // depend on the PPC behavior for oversized shift amounts. 7180 SDValue Lo = Op.getOperand(0); 7181 SDValue Hi = Op.getOperand(1); 7182 SDValue Amt = Op.getOperand(2); 7183 EVT AmtVT = Amt.getValueType(); 7184 7185 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, 7186 DAG.getConstant(BitWidth, dl, AmtVT), Amt); 7187 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt); 7188 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1); 7189 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); 7190 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, 7191 DAG.getConstant(-BitWidth, dl, AmtVT)); 7192 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5); 7193 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6); 7194 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt); 7195 SDValue OutOps[] = { OutLo, OutHi }; 7196 return DAG.getMergeValues(OutOps, dl); 7197 } 7198 7199 SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const { 7200 SDLoc dl(Op); 7201 EVT VT = Op.getValueType(); 7202 unsigned BitWidth = VT.getSizeInBits(); 7203 assert(Op.getNumOperands() == 3 && 7204 VT == Op.getOperand(1).getValueType() && 7205 "Unexpected SRA!"); 7206 7207 // Expand into a bunch of logical ops, followed by a select_cc. 7208 SDValue Lo = Op.getOperand(0); 7209 SDValue Hi = Op.getOperand(1); 7210 SDValue Amt = Op.getOperand(2); 7211 EVT AmtVT = Amt.getValueType(); 7212 7213 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, 7214 DAG.getConstant(BitWidth, dl, AmtVT), Amt); 7215 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt); 7216 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1); 7217 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); 7218 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, 7219 DAG.getConstant(-BitWidth, dl, AmtVT)); 7220 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5); 7221 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt); 7222 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, dl, AmtVT), 7223 Tmp4, Tmp6, ISD::SETLE); 7224 SDValue OutOps[] = { OutLo, OutHi }; 7225 return DAG.getMergeValues(OutOps, dl); 7226 } 7227 7228 //===----------------------------------------------------------------------===// 7229 // Vector related lowering. 7230 // 7231 7232 /// BuildSplatI - Build a canonical splati of Val with an element size of 7233 /// SplatSize. Cast the result to VT. 7234 static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT, 7235 SelectionDAG &DAG, const SDLoc &dl) { 7236 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!"); 7237 7238 static const MVT VTys[] = { // canonical VT to use for each size. 7239 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32 7240 }; 7241 7242 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1]; 7243 7244 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize. 7245 if (Val == -1) 7246 SplatSize = 1; 7247 7248 EVT CanonicalVT = VTys[SplatSize-1]; 7249 7250 // Build a canonical splat for this value. 7251 return DAG.getBitcast(ReqVT, DAG.getConstant(Val, dl, CanonicalVT)); 7252 } 7253 7254 /// BuildIntrinsicOp - Return a unary operator intrinsic node with the 7255 /// specified intrinsic ID. 7256 static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op, SelectionDAG &DAG, 7257 const SDLoc &dl, EVT DestVT = MVT::Other) { 7258 if (DestVT == MVT::Other) DestVT = Op.getValueType(); 7259 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, 7260 DAG.getConstant(IID, dl, MVT::i32), Op); 7261 } 7262 7263 /// BuildIntrinsicOp - Return a binary operator intrinsic node with the 7264 /// specified intrinsic ID. 7265 static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS, 7266 SelectionDAG &DAG, const SDLoc &dl, 7267 EVT DestVT = MVT::Other) { 7268 if (DestVT == MVT::Other) DestVT = LHS.getValueType(); 7269 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, 7270 DAG.getConstant(IID, dl, MVT::i32), LHS, RHS); 7271 } 7272 7273 /// BuildIntrinsicOp - Return a ternary operator intrinsic node with the 7274 /// specified intrinsic ID. 7275 static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1, 7276 SDValue Op2, SelectionDAG &DAG, const SDLoc &dl, 7277 EVT DestVT = MVT::Other) { 7278 if (DestVT == MVT::Other) DestVT = Op0.getValueType(); 7279 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, 7280 DAG.getConstant(IID, dl, MVT::i32), Op0, Op1, Op2); 7281 } 7282 7283 /// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified 7284 /// amount. The result has the specified value type. 7285 static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt, EVT VT, 7286 SelectionDAG &DAG, const SDLoc &dl) { 7287 // Force LHS/RHS to be the right type. 7288 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS); 7289 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS); 7290 7291 int Ops[16]; 7292 for (unsigned i = 0; i != 16; ++i) 7293 Ops[i] = i + Amt; 7294 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops); 7295 return DAG.getNode(ISD::BITCAST, dl, VT, T); 7296 } 7297 7298 /// Do we have an efficient pattern in a .td file for this node? 7299 /// 7300 /// \param V - pointer to the BuildVectorSDNode being matched 7301 /// \param HasDirectMove - does this subtarget have VSR <-> GPR direct moves? 7302 /// 7303 /// There are some patterns where it is beneficial to keep a BUILD_VECTOR 7304 /// node as a BUILD_VECTOR node rather than expanding it. The patterns where 7305 /// the opposite is true (expansion is beneficial) are: 7306 /// - The node builds a vector out of integers that are not 32 or 64-bits 7307 /// - The node builds a vector out of constants 7308 /// - The node is a "load-and-splat" 7309 /// In all other cases, we will choose to keep the BUILD_VECTOR. 7310 static bool haveEfficientBuildVectorPattern(BuildVectorSDNode *V, 7311 bool HasDirectMove) { 7312 EVT VecVT = V->getValueType(0); 7313 bool RightType = VecVT == MVT::v2f64 || VecVT == MVT::v4f32 || 7314 (HasDirectMove && (VecVT == MVT::v2i64 || VecVT == MVT::v4i32)); 7315 if (!RightType) 7316 return false; 7317 7318 bool IsSplat = true; 7319 bool IsLoad = false; 7320 SDValue Op0 = V->getOperand(0); 7321 7322 // This function is called in a block that confirms the node is not a constant 7323 // splat. So a constant BUILD_VECTOR here means the vector is built out of 7324 // different constants. 7325 if (V->isConstant()) 7326 return false; 7327 for (int i = 0, e = V->getNumOperands(); i < e; ++i) { 7328 if (V->getOperand(i).isUndef()) 7329 return false; 7330 // We want to expand nodes that represent load-and-splat even if the 7331 // loaded value is a floating point truncation or conversion to int. 7332 if (V->getOperand(i).getOpcode() == ISD::LOAD || 7333 (V->getOperand(i).getOpcode() == ISD::FP_ROUND && 7334 V->getOperand(i).getOperand(0).getOpcode() == ISD::LOAD) || 7335 (V->getOperand(i).getOpcode() == ISD::FP_TO_SINT && 7336 V->getOperand(i).getOperand(0).getOpcode() == ISD::LOAD) || 7337 (V->getOperand(i).getOpcode() == ISD::FP_TO_UINT && 7338 V->getOperand(i).getOperand(0).getOpcode() == ISD::LOAD)) 7339 IsLoad = true; 7340 // If the operands are different or the input is not a load and has more 7341 // uses than just this BV node, then it isn't a splat. 7342 if (V->getOperand(i) != Op0 || 7343 (!IsLoad && !V->isOnlyUserOf(V->getOperand(i).getNode()))) 7344 IsSplat = false; 7345 } 7346 return !(IsSplat && IsLoad); 7347 } 7348 7349 // If this is a case we can't handle, return null and let the default 7350 // expansion code take care of it. If we CAN select this case, and if it 7351 // selects to a single instruction, return Op. Otherwise, if we can codegen 7352 // this case more efficiently than a constant pool load, lower it to the 7353 // sequence of ops that should be used. 7354 SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op, 7355 SelectionDAG &DAG) const { 7356 SDLoc dl(Op); 7357 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 7358 assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR"); 7359 7360 if (Subtarget.hasQPX() && Op.getValueType() == MVT::v4i1) { 7361 // We first build an i32 vector, load it into a QPX register, 7362 // then convert it to a floating-point vector and compare it 7363 // to a zero vector to get the boolean result. 7364 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 7365 int FrameIdx = MFI.CreateStackObject(16, 16, false); 7366 MachinePointerInfo PtrInfo = 7367 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); 7368 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 7369 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 7370 7371 assert(BVN->getNumOperands() == 4 && 7372 "BUILD_VECTOR for v4i1 does not have 4 operands"); 7373 7374 bool IsConst = true; 7375 for (unsigned i = 0; i < 4; ++i) { 7376 if (BVN->getOperand(i).isUndef()) continue; 7377 if (!isa<ConstantSDNode>(BVN->getOperand(i))) { 7378 IsConst = false; 7379 break; 7380 } 7381 } 7382 7383 if (IsConst) { 7384 Constant *One = 7385 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), 1.0); 7386 Constant *NegOne = 7387 ConstantFP::get(Type::getFloatTy(*DAG.getContext()), -1.0); 7388 7389 Constant *CV[4]; 7390 for (unsigned i = 0; i < 4; ++i) { 7391 if (BVN->getOperand(i).isUndef()) 7392 CV[i] = UndefValue::get(Type::getFloatTy(*DAG.getContext())); 7393 else if (isNullConstant(BVN->getOperand(i))) 7394 CV[i] = NegOne; 7395 else 7396 CV[i] = One; 7397 } 7398 7399 Constant *CP = ConstantVector::get(CV); 7400 SDValue CPIdx = DAG.getConstantPool(CP, getPointerTy(DAG.getDataLayout()), 7401 16 /* alignment */); 7402 7403 SDValue Ops[] = {DAG.getEntryNode(), CPIdx}; 7404 SDVTList VTs = DAG.getVTList({MVT::v4i1, /*chain*/ MVT::Other}); 7405 return DAG.getMemIntrinsicNode( 7406 PPCISD::QVLFSb, dl, VTs, Ops, MVT::v4f32, 7407 MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); 7408 } 7409 7410 SmallVector<SDValue, 4> Stores; 7411 for (unsigned i = 0; i < 4; ++i) { 7412 if (BVN->getOperand(i).isUndef()) continue; 7413 7414 unsigned Offset = 4*i; 7415 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType()); 7416 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx); 7417 7418 unsigned StoreSize = BVN->getOperand(i).getValueType().getStoreSize(); 7419 if (StoreSize > 4) { 7420 Stores.push_back( 7421 DAG.getTruncStore(DAG.getEntryNode(), dl, BVN->getOperand(i), Idx, 7422 PtrInfo.getWithOffset(Offset), MVT::i32)); 7423 } else { 7424 SDValue StoreValue = BVN->getOperand(i); 7425 if (StoreSize < 4) 7426 StoreValue = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, StoreValue); 7427 7428 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, StoreValue, Idx, 7429 PtrInfo.getWithOffset(Offset))); 7430 } 7431 } 7432 7433 SDValue StoreChain; 7434 if (!Stores.empty()) 7435 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 7436 else 7437 StoreChain = DAG.getEntryNode(); 7438 7439 // Now load from v4i32 into the QPX register; this will extend it to 7440 // v4i64 but not yet convert it to a floating point. Nevertheless, this 7441 // is typed as v4f64 because the QPX register integer states are not 7442 // explicitly represented. 7443 7444 SDValue Ops[] = {StoreChain, 7445 DAG.getConstant(Intrinsic::ppc_qpx_qvlfiwz, dl, MVT::i32), 7446 FIdx}; 7447 SDVTList VTs = DAG.getVTList({MVT::v4f64, /*chain*/ MVT::Other}); 7448 7449 SDValue LoadedVect = DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, 7450 dl, VTs, Ops, MVT::v4i32, PtrInfo); 7451 LoadedVect = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64, 7452 DAG.getConstant(Intrinsic::ppc_qpx_qvfcfidu, dl, MVT::i32), 7453 LoadedVect); 7454 7455 SDValue FPZeros = DAG.getConstantFP(0.0, dl, MVT::v4f64); 7456 7457 return DAG.getSetCC(dl, MVT::v4i1, LoadedVect, FPZeros, ISD::SETEQ); 7458 } 7459 7460 // All other QPX vectors are handled by generic code. 7461 if (Subtarget.hasQPX()) 7462 return SDValue(); 7463 7464 // Check if this is a splat of a constant value. 7465 APInt APSplatBits, APSplatUndef; 7466 unsigned SplatBitSize; 7467 bool HasAnyUndefs; 7468 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize, 7469 HasAnyUndefs, 0, !Subtarget.isLittleEndian()) || 7470 SplatBitSize > 32) { 7471 // BUILD_VECTOR nodes that are not constant splats of up to 32-bits can be 7472 // lowered to VSX instructions under certain conditions. 7473 // Without VSX, there is no pattern more efficient than expanding the node. 7474 if (Subtarget.hasVSX() && 7475 haveEfficientBuildVectorPattern(BVN, Subtarget.hasDirectMove())) 7476 return Op; 7477 return SDValue(); 7478 } 7479 7480 unsigned SplatBits = APSplatBits.getZExtValue(); 7481 unsigned SplatUndef = APSplatUndef.getZExtValue(); 7482 unsigned SplatSize = SplatBitSize / 8; 7483 7484 // First, handle single instruction cases. 7485 7486 // All zeros? 7487 if (SplatBits == 0) { 7488 // Canonicalize all zero vectors to be v4i32. 7489 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) { 7490 SDValue Z = DAG.getConstant(0, dl, MVT::v4i32); 7491 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z); 7492 } 7493 return Op; 7494 } 7495 7496 // We have XXSPLTIB for constant splats one byte wide 7497 if (Subtarget.hasP9Vector() && SplatSize == 1) { 7498 // This is a splat of 1-byte elements with some elements potentially undef. 7499 // Rather than trying to match undef in the SDAG patterns, ensure that all 7500 // elements are the same constant. 7501 if (HasAnyUndefs || ISD::isBuildVectorAllOnes(BVN)) { 7502 SmallVector<SDValue, 16> Ops(16, DAG.getConstant(SplatBits, 7503 dl, MVT::i32)); 7504 SDValue NewBV = DAG.getBuildVector(MVT::v16i8, dl, Ops); 7505 if (Op.getValueType() != MVT::v16i8) 7506 return DAG.getBitcast(Op.getValueType(), NewBV); 7507 return NewBV; 7508 } 7509 return Op; 7510 } 7511 7512 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw]. 7513 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >> 7514 (32-SplatBitSize)); 7515 if (SextVal >= -16 && SextVal <= 15) 7516 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl); 7517 7518 // Two instruction sequences. 7519 7520 // If this value is in the range [-32,30] and is even, use: 7521 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2) 7522 // If this value is in the range [17,31] and is odd, use: 7523 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16) 7524 // If this value is in the range [-31,-17] and is odd, use: 7525 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16) 7526 // Note the last two are three-instruction sequences. 7527 if (SextVal >= -32 && SextVal <= 31) { 7528 // To avoid having these optimizations undone by constant folding, 7529 // we convert to a pseudo that will be expanded later into one of 7530 // the above forms. 7531 SDValue Elt = DAG.getConstant(SextVal, dl, MVT::i32); 7532 EVT VT = (SplatSize == 1 ? MVT::v16i8 : 7533 (SplatSize == 2 ? MVT::v8i16 : MVT::v4i32)); 7534 SDValue EltSize = DAG.getConstant(SplatSize, dl, MVT::i32); 7535 SDValue RetVal = DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize); 7536 if (VT == Op.getValueType()) 7537 return RetVal; 7538 else 7539 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), RetVal); 7540 } 7541 7542 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is 7543 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important 7544 // for fneg/fabs. 7545 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) { 7546 // Make -1 and vspltisw -1: 7547 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl); 7548 7549 // Make the VSLW intrinsic, computing 0x8000_0000. 7550 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV, 7551 OnesV, DAG, dl); 7552 7553 // xor by OnesV to invert it. 7554 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV); 7555 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); 7556 } 7557 7558 // Check to see if this is a wide variety of vsplti*, binop self cases. 7559 static const signed char SplatCsts[] = { 7560 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, 7561 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16 7562 }; 7563 7564 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) { 7565 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for 7566 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1' 7567 int i = SplatCsts[idx]; 7568 7569 // Figure out what shift amount will be used by altivec if shifted by i in 7570 // this splat size. 7571 unsigned TypeShiftAmt = i & (SplatBitSize-1); 7572 7573 // vsplti + shl self. 7574 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) { 7575 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); 7576 static const unsigned IIDs[] = { // Intrinsic to use for each size. 7577 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0, 7578 Intrinsic::ppc_altivec_vslw 7579 }; 7580 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); 7581 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); 7582 } 7583 7584 // vsplti + srl self. 7585 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) { 7586 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); 7587 static const unsigned IIDs[] = { // Intrinsic to use for each size. 7588 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0, 7589 Intrinsic::ppc_altivec_vsrw 7590 }; 7591 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); 7592 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); 7593 } 7594 7595 // vsplti + sra self. 7596 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) { 7597 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); 7598 static const unsigned IIDs[] = { // Intrinsic to use for each size. 7599 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0, 7600 Intrinsic::ppc_altivec_vsraw 7601 }; 7602 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); 7603 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); 7604 } 7605 7606 // vsplti + rol self. 7607 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) | 7608 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) { 7609 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); 7610 static const unsigned IIDs[] = { // Intrinsic to use for each size. 7611 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0, 7612 Intrinsic::ppc_altivec_vrlw 7613 }; 7614 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); 7615 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); 7616 } 7617 7618 // t = vsplti c, result = vsldoi t, t, 1 7619 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) { 7620 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); 7621 unsigned Amt = Subtarget.isLittleEndian() ? 15 : 1; 7622 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl); 7623 } 7624 // t = vsplti c, result = vsldoi t, t, 2 7625 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) { 7626 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); 7627 unsigned Amt = Subtarget.isLittleEndian() ? 14 : 2; 7628 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl); 7629 } 7630 // t = vsplti c, result = vsldoi t, t, 3 7631 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) { 7632 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); 7633 unsigned Amt = Subtarget.isLittleEndian() ? 13 : 3; 7634 return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl); 7635 } 7636 } 7637 7638 return SDValue(); 7639 } 7640 7641 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit 7642 /// the specified operations to build the shuffle. 7643 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, 7644 SDValue RHS, SelectionDAG &DAG, 7645 const SDLoc &dl) { 7646 unsigned OpNum = (PFEntry >> 26) & 0x0F; 7647 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); 7648 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); 7649 7650 enum { 7651 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> 7652 OP_VMRGHW, 7653 OP_VMRGLW, 7654 OP_VSPLTISW0, 7655 OP_VSPLTISW1, 7656 OP_VSPLTISW2, 7657 OP_VSPLTISW3, 7658 OP_VSLDOI4, 7659 OP_VSLDOI8, 7660 OP_VSLDOI12 7661 }; 7662 7663 if (OpNum == OP_COPY) { 7664 if (LHSID == (1*9+2)*9+3) return LHS; 7665 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); 7666 return RHS; 7667 } 7668 7669 SDValue OpLHS, OpRHS; 7670 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); 7671 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); 7672 7673 int ShufIdxs[16]; 7674 switch (OpNum) { 7675 default: llvm_unreachable("Unknown i32 permute!"); 7676 case OP_VMRGHW: 7677 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3; 7678 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19; 7679 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7; 7680 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23; 7681 break; 7682 case OP_VMRGLW: 7683 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11; 7684 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27; 7685 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15; 7686 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31; 7687 break; 7688 case OP_VSPLTISW0: 7689 for (unsigned i = 0; i != 16; ++i) 7690 ShufIdxs[i] = (i&3)+0; 7691 break; 7692 case OP_VSPLTISW1: 7693 for (unsigned i = 0; i != 16; ++i) 7694 ShufIdxs[i] = (i&3)+4; 7695 break; 7696 case OP_VSPLTISW2: 7697 for (unsigned i = 0; i != 16; ++i) 7698 ShufIdxs[i] = (i&3)+8; 7699 break; 7700 case OP_VSPLTISW3: 7701 for (unsigned i = 0; i != 16; ++i) 7702 ShufIdxs[i] = (i&3)+12; 7703 break; 7704 case OP_VSLDOI4: 7705 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl); 7706 case OP_VSLDOI8: 7707 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl); 7708 case OP_VSLDOI12: 7709 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl); 7710 } 7711 EVT VT = OpLHS.getValueType(); 7712 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS); 7713 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS); 7714 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs); 7715 return DAG.getNode(ISD::BITCAST, dl, VT, T); 7716 } 7717 7718 /// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this 7719 /// is a shuffle we can handle in a single instruction, return it. Otherwise, 7720 /// return the code it can be lowered into. Worst case, it can always be 7721 /// lowered into a vperm. 7722 SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, 7723 SelectionDAG &DAG) const { 7724 SDLoc dl(Op); 7725 SDValue V1 = Op.getOperand(0); 7726 SDValue V2 = Op.getOperand(1); 7727 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); 7728 EVT VT = Op.getValueType(); 7729 bool isLittleEndian = Subtarget.isLittleEndian(); 7730 7731 unsigned ShiftElts, InsertAtByte; 7732 bool Swap; 7733 if (Subtarget.hasP9Vector() && 7734 PPC::isXXINSERTWMask(SVOp, ShiftElts, InsertAtByte, Swap, 7735 isLittleEndian)) { 7736 if (Swap) 7737 std::swap(V1, V2); 7738 SDValue Conv1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1); 7739 SDValue Conv2 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V2); 7740 if (ShiftElts) { 7741 SDValue Shl = DAG.getNode(PPCISD::VECSHL, dl, MVT::v4i32, Conv2, Conv2, 7742 DAG.getConstant(ShiftElts, dl, MVT::i32)); 7743 SDValue Ins = DAG.getNode(PPCISD::XXINSERT, dl, MVT::v4i32, Conv1, Shl, 7744 DAG.getConstant(InsertAtByte, dl, MVT::i32)); 7745 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Ins); 7746 } 7747 SDValue Ins = DAG.getNode(PPCISD::XXINSERT, dl, MVT::v4i32, Conv1, Conv2, 7748 DAG.getConstant(InsertAtByte, dl, MVT::i32)); 7749 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Ins); 7750 } 7751 7752 7753 if (Subtarget.hasVSX() && 7754 PPC::isXXSLDWIShuffleMask(SVOp, ShiftElts, Swap, isLittleEndian)) { 7755 if (Swap) 7756 std::swap(V1, V2); 7757 SDValue Conv1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1); 7758 SDValue Conv2 = 7759 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V2.isUndef() ? V1 : V2); 7760 7761 SDValue Shl = DAG.getNode(PPCISD::VECSHL, dl, MVT::v4i32, Conv1, Conv2, 7762 DAG.getConstant(ShiftElts, dl, MVT::i32)); 7763 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Shl); 7764 } 7765 7766 if (Subtarget.hasVSX()) { 7767 if (V2.isUndef() && PPC::isSplatShuffleMask(SVOp, 4)) { 7768 int SplatIdx = PPC::getVSPLTImmediate(SVOp, 4, DAG); 7769 7770 // If the source for the shuffle is a scalar_to_vector that came from a 7771 // 32-bit load, it will have used LXVWSX so we don't need to splat again. 7772 if (Subtarget.hasP9Vector() && 7773 ((isLittleEndian && SplatIdx == 3) || 7774 (!isLittleEndian && SplatIdx == 0))) { 7775 SDValue Src = V1.getOperand(0); 7776 if (Src.getOpcode() == ISD::SCALAR_TO_VECTOR && 7777 Src.getOperand(0).getOpcode() == ISD::LOAD && 7778 Src.getOperand(0).hasOneUse()) 7779 return V1; 7780 } 7781 SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1); 7782 SDValue Splat = DAG.getNode(PPCISD::XXSPLT, dl, MVT::v4i32, Conv, 7783 DAG.getConstant(SplatIdx, dl, MVT::i32)); 7784 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Splat); 7785 } 7786 7787 // Left shifts of 8 bytes are actually swaps. Convert accordingly. 7788 if (V2.isUndef() && PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) == 8) { 7789 SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1); 7790 SDValue Swap = DAG.getNode(PPCISD::SWAP_NO_CHAIN, dl, MVT::v2f64, Conv); 7791 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Swap); 7792 } 7793 } 7794 7795 if (Subtarget.hasQPX()) { 7796 if (VT.getVectorNumElements() != 4) 7797 return SDValue(); 7798 7799 if (V2.isUndef()) V2 = V1; 7800 7801 int AlignIdx = PPC::isQVALIGNIShuffleMask(SVOp); 7802 if (AlignIdx != -1) { 7803 return DAG.getNode(PPCISD::QVALIGNI, dl, VT, V1, V2, 7804 DAG.getConstant(AlignIdx, dl, MVT::i32)); 7805 } else if (SVOp->isSplat()) { 7806 int SplatIdx = SVOp->getSplatIndex(); 7807 if (SplatIdx >= 4) { 7808 std::swap(V1, V2); 7809 SplatIdx -= 4; 7810 } 7811 7812 return DAG.getNode(PPCISD::QVESPLATI, dl, VT, V1, 7813 DAG.getConstant(SplatIdx, dl, MVT::i32)); 7814 } 7815 7816 // Lower this into a qvgpci/qvfperm pair. 7817 7818 // Compute the qvgpci literal 7819 unsigned idx = 0; 7820 for (unsigned i = 0; i < 4; ++i) { 7821 int m = SVOp->getMaskElt(i); 7822 unsigned mm = m >= 0 ? (unsigned) m : i; 7823 idx |= mm << (3-i)*3; 7824 } 7825 7826 SDValue V3 = DAG.getNode(PPCISD::QVGPCI, dl, MVT::v4f64, 7827 DAG.getConstant(idx, dl, MVT::i32)); 7828 return DAG.getNode(PPCISD::QVFPERM, dl, VT, V1, V2, V3); 7829 } 7830 7831 // Cases that are handled by instructions that take permute immediates 7832 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be 7833 // selected by the instruction selector. 7834 if (V2.isUndef()) { 7835 if (PPC::isSplatShuffleMask(SVOp, 1) || 7836 PPC::isSplatShuffleMask(SVOp, 2) || 7837 PPC::isSplatShuffleMask(SVOp, 4) || 7838 PPC::isVPKUWUMShuffleMask(SVOp, 1, DAG) || 7839 PPC::isVPKUHUMShuffleMask(SVOp, 1, DAG) || 7840 PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) != -1 || 7841 PPC::isVMRGLShuffleMask(SVOp, 1, 1, DAG) || 7842 PPC::isVMRGLShuffleMask(SVOp, 2, 1, DAG) || 7843 PPC::isVMRGLShuffleMask(SVOp, 4, 1, DAG) || 7844 PPC::isVMRGHShuffleMask(SVOp, 1, 1, DAG) || 7845 PPC::isVMRGHShuffleMask(SVOp, 2, 1, DAG) || 7846 PPC::isVMRGHShuffleMask(SVOp, 4, 1, DAG) || 7847 (Subtarget.hasP8Altivec() && ( 7848 PPC::isVPKUDUMShuffleMask(SVOp, 1, DAG) || 7849 PPC::isVMRGEOShuffleMask(SVOp, true, 1, DAG) || 7850 PPC::isVMRGEOShuffleMask(SVOp, false, 1, DAG)))) { 7851 return Op; 7852 } 7853 } 7854 7855 // Altivec has a variety of "shuffle immediates" that take two vector inputs 7856 // and produce a fixed permutation. If any of these match, do not lower to 7857 // VPERM. 7858 unsigned int ShuffleKind = isLittleEndian ? 2 : 0; 7859 if (PPC::isVPKUWUMShuffleMask(SVOp, ShuffleKind, DAG) || 7860 PPC::isVPKUHUMShuffleMask(SVOp, ShuffleKind, DAG) || 7861 PPC::isVSLDOIShuffleMask(SVOp, ShuffleKind, DAG) != -1 || 7862 PPC::isVMRGLShuffleMask(SVOp, 1, ShuffleKind, DAG) || 7863 PPC::isVMRGLShuffleMask(SVOp, 2, ShuffleKind, DAG) || 7864 PPC::isVMRGLShuffleMask(SVOp, 4, ShuffleKind, DAG) || 7865 PPC::isVMRGHShuffleMask(SVOp, 1, ShuffleKind, DAG) || 7866 PPC::isVMRGHShuffleMask(SVOp, 2, ShuffleKind, DAG) || 7867 PPC::isVMRGHShuffleMask(SVOp, 4, ShuffleKind, DAG) || 7868 (Subtarget.hasP8Altivec() && ( 7869 PPC::isVPKUDUMShuffleMask(SVOp, ShuffleKind, DAG) || 7870 PPC::isVMRGEOShuffleMask(SVOp, true, ShuffleKind, DAG) || 7871 PPC::isVMRGEOShuffleMask(SVOp, false, ShuffleKind, DAG)))) 7872 return Op; 7873 7874 // Check to see if this is a shuffle of 4-byte values. If so, we can use our 7875 // perfect shuffle table to emit an optimal matching sequence. 7876 ArrayRef<int> PermMask = SVOp->getMask(); 7877 7878 unsigned PFIndexes[4]; 7879 bool isFourElementShuffle = true; 7880 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number 7881 unsigned EltNo = 8; // Start out undef. 7882 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte. 7883 if (PermMask[i*4+j] < 0) 7884 continue; // Undef, ignore it. 7885 7886 unsigned ByteSource = PermMask[i*4+j]; 7887 if ((ByteSource & 3) != j) { 7888 isFourElementShuffle = false; 7889 break; 7890 } 7891 7892 if (EltNo == 8) { 7893 EltNo = ByteSource/4; 7894 } else if (EltNo != ByteSource/4) { 7895 isFourElementShuffle = false; 7896 break; 7897 } 7898 } 7899 PFIndexes[i] = EltNo; 7900 } 7901 7902 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the 7903 // perfect shuffle vector to determine if it is cost effective to do this as 7904 // discrete instructions, or whether we should use a vperm. 7905 // For now, we skip this for little endian until such time as we have a 7906 // little-endian perfect shuffle table. 7907 if (isFourElementShuffle && !isLittleEndian) { 7908 // Compute the index in the perfect shuffle table. 7909 unsigned PFTableIndex = 7910 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 7911 7912 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 7913 unsigned Cost = (PFEntry >> 30); 7914 7915 // Determining when to avoid vperm is tricky. Many things affect the cost 7916 // of vperm, particularly how many times the perm mask needs to be computed. 7917 // For example, if the perm mask can be hoisted out of a loop or is already 7918 // used (perhaps because there are multiple permutes with the same shuffle 7919 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of 7920 // the loop requires an extra register. 7921 // 7922 // As a compromise, we only emit discrete instructions if the shuffle can be 7923 // generated in 3 or fewer operations. When we have loop information 7924 // available, if this block is within a loop, we should avoid using vperm 7925 // for 3-operation perms and use a constant pool load instead. 7926 if (Cost < 3) 7927 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 7928 } 7929 7930 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant 7931 // vector that will get spilled to the constant pool. 7932 if (V2.isUndef()) V2 = V1; 7933 7934 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except 7935 // that it is in input element units, not in bytes. Convert now. 7936 7937 // For little endian, the order of the input vectors is reversed, and 7938 // the permutation mask is complemented with respect to 31. This is 7939 // necessary to produce proper semantics with the big-endian-biased vperm 7940 // instruction. 7941 EVT EltVT = V1.getValueType().getVectorElementType(); 7942 unsigned BytesPerElement = EltVT.getSizeInBits()/8; 7943 7944 SmallVector<SDValue, 16> ResultMask; 7945 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) { 7946 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i]; 7947 7948 for (unsigned j = 0; j != BytesPerElement; ++j) 7949 if (isLittleEndian) 7950 ResultMask.push_back(DAG.getConstant(31 - (SrcElt*BytesPerElement + j), 7951 dl, MVT::i32)); 7952 else 7953 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement + j, dl, 7954 MVT::i32)); 7955 } 7956 7957 SDValue VPermMask = DAG.getBuildVector(MVT::v16i8, dl, ResultMask); 7958 if (isLittleEndian) 7959 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), 7960 V2, V1, VPermMask); 7961 else 7962 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), 7963 V1, V2, VPermMask); 7964 } 7965 7966 /// getVectorCompareInfo - Given an intrinsic, return false if it is not a 7967 /// vector comparison. If it is, return true and fill in Opc/isDot with 7968 /// information about the intrinsic. 7969 static bool getVectorCompareInfo(SDValue Intrin, int &CompareOpc, 7970 bool &isDot, const PPCSubtarget &Subtarget) { 7971 unsigned IntrinsicID = 7972 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue(); 7973 CompareOpc = -1; 7974 isDot = false; 7975 switch (IntrinsicID) { 7976 default: 7977 return false; 7978 // Comparison predicates. 7979 case Intrinsic::ppc_altivec_vcmpbfp_p: 7980 CompareOpc = 966; 7981 isDot = true; 7982 break; 7983 case Intrinsic::ppc_altivec_vcmpeqfp_p: 7984 CompareOpc = 198; 7985 isDot = true; 7986 break; 7987 case Intrinsic::ppc_altivec_vcmpequb_p: 7988 CompareOpc = 6; 7989 isDot = true; 7990 break; 7991 case Intrinsic::ppc_altivec_vcmpequh_p: 7992 CompareOpc = 70; 7993 isDot = true; 7994 break; 7995 case Intrinsic::ppc_altivec_vcmpequw_p: 7996 CompareOpc = 134; 7997 isDot = true; 7998 break; 7999 case Intrinsic::ppc_altivec_vcmpequd_p: 8000 if (Subtarget.hasP8Altivec()) { 8001 CompareOpc = 199; 8002 isDot = true; 8003 } else 8004 return false; 8005 break; 8006 case Intrinsic::ppc_altivec_vcmpneb_p: 8007 case Intrinsic::ppc_altivec_vcmpneh_p: 8008 case Intrinsic::ppc_altivec_vcmpnew_p: 8009 case Intrinsic::ppc_altivec_vcmpnezb_p: 8010 case Intrinsic::ppc_altivec_vcmpnezh_p: 8011 case Intrinsic::ppc_altivec_vcmpnezw_p: 8012 if (Subtarget.hasP9Altivec()) { 8013 switch (IntrinsicID) { 8014 default: 8015 llvm_unreachable("Unknown comparison intrinsic."); 8016 case Intrinsic::ppc_altivec_vcmpneb_p: 8017 CompareOpc = 7; 8018 break; 8019 case Intrinsic::ppc_altivec_vcmpneh_p: 8020 CompareOpc = 71; 8021 break; 8022 case Intrinsic::ppc_altivec_vcmpnew_p: 8023 CompareOpc = 135; 8024 break; 8025 case Intrinsic::ppc_altivec_vcmpnezb_p: 8026 CompareOpc = 263; 8027 break; 8028 case Intrinsic::ppc_altivec_vcmpnezh_p: 8029 CompareOpc = 327; 8030 break; 8031 case Intrinsic::ppc_altivec_vcmpnezw_p: 8032 CompareOpc = 391; 8033 break; 8034 } 8035 isDot = true; 8036 } else 8037 return false; 8038 break; 8039 case Intrinsic::ppc_altivec_vcmpgefp_p: 8040 CompareOpc = 454; 8041 isDot = true; 8042 break; 8043 case Intrinsic::ppc_altivec_vcmpgtfp_p: 8044 CompareOpc = 710; 8045 isDot = true; 8046 break; 8047 case Intrinsic::ppc_altivec_vcmpgtsb_p: 8048 CompareOpc = 774; 8049 isDot = true; 8050 break; 8051 case Intrinsic::ppc_altivec_vcmpgtsh_p: 8052 CompareOpc = 838; 8053 isDot = true; 8054 break; 8055 case Intrinsic::ppc_altivec_vcmpgtsw_p: 8056 CompareOpc = 902; 8057 isDot = true; 8058 break; 8059 case Intrinsic::ppc_altivec_vcmpgtsd_p: 8060 if (Subtarget.hasP8Altivec()) { 8061 CompareOpc = 967; 8062 isDot = true; 8063 } else 8064 return false; 8065 break; 8066 case Intrinsic::ppc_altivec_vcmpgtub_p: 8067 CompareOpc = 518; 8068 isDot = true; 8069 break; 8070 case Intrinsic::ppc_altivec_vcmpgtuh_p: 8071 CompareOpc = 582; 8072 isDot = true; 8073 break; 8074 case Intrinsic::ppc_altivec_vcmpgtuw_p: 8075 CompareOpc = 646; 8076 isDot = true; 8077 break; 8078 case Intrinsic::ppc_altivec_vcmpgtud_p: 8079 if (Subtarget.hasP8Altivec()) { 8080 CompareOpc = 711; 8081 isDot = true; 8082 } else 8083 return false; 8084 break; 8085 8086 // VSX predicate comparisons use the same infrastructure 8087 case Intrinsic::ppc_vsx_xvcmpeqdp_p: 8088 case Intrinsic::ppc_vsx_xvcmpgedp_p: 8089 case Intrinsic::ppc_vsx_xvcmpgtdp_p: 8090 case Intrinsic::ppc_vsx_xvcmpeqsp_p: 8091 case Intrinsic::ppc_vsx_xvcmpgesp_p: 8092 case Intrinsic::ppc_vsx_xvcmpgtsp_p: 8093 if (Subtarget.hasVSX()) { 8094 switch (IntrinsicID) { 8095 case Intrinsic::ppc_vsx_xvcmpeqdp_p: 8096 CompareOpc = 99; 8097 break; 8098 case Intrinsic::ppc_vsx_xvcmpgedp_p: 8099 CompareOpc = 115; 8100 break; 8101 case Intrinsic::ppc_vsx_xvcmpgtdp_p: 8102 CompareOpc = 107; 8103 break; 8104 case Intrinsic::ppc_vsx_xvcmpeqsp_p: 8105 CompareOpc = 67; 8106 break; 8107 case Intrinsic::ppc_vsx_xvcmpgesp_p: 8108 CompareOpc = 83; 8109 break; 8110 case Intrinsic::ppc_vsx_xvcmpgtsp_p: 8111 CompareOpc = 75; 8112 break; 8113 } 8114 isDot = true; 8115 } else 8116 return false; 8117 break; 8118 8119 // Normal Comparisons. 8120 case Intrinsic::ppc_altivec_vcmpbfp: 8121 CompareOpc = 966; 8122 break; 8123 case Intrinsic::ppc_altivec_vcmpeqfp: 8124 CompareOpc = 198; 8125 break; 8126 case Intrinsic::ppc_altivec_vcmpequb: 8127 CompareOpc = 6; 8128 break; 8129 case Intrinsic::ppc_altivec_vcmpequh: 8130 CompareOpc = 70; 8131 break; 8132 case Intrinsic::ppc_altivec_vcmpequw: 8133 CompareOpc = 134; 8134 break; 8135 case Intrinsic::ppc_altivec_vcmpequd: 8136 if (Subtarget.hasP8Altivec()) 8137 CompareOpc = 199; 8138 else 8139 return false; 8140 break; 8141 case Intrinsic::ppc_altivec_vcmpneb: 8142 case Intrinsic::ppc_altivec_vcmpneh: 8143 case Intrinsic::ppc_altivec_vcmpnew: 8144 case Intrinsic::ppc_altivec_vcmpnezb: 8145 case Intrinsic::ppc_altivec_vcmpnezh: 8146 case Intrinsic::ppc_altivec_vcmpnezw: 8147 if (Subtarget.hasP9Altivec()) 8148 switch (IntrinsicID) { 8149 default: 8150 llvm_unreachable("Unknown comparison intrinsic."); 8151 case Intrinsic::ppc_altivec_vcmpneb: 8152 CompareOpc = 7; 8153 break; 8154 case Intrinsic::ppc_altivec_vcmpneh: 8155 CompareOpc = 71; 8156 break; 8157 case Intrinsic::ppc_altivec_vcmpnew: 8158 CompareOpc = 135; 8159 break; 8160 case Intrinsic::ppc_altivec_vcmpnezb: 8161 CompareOpc = 263; 8162 break; 8163 case Intrinsic::ppc_altivec_vcmpnezh: 8164 CompareOpc = 327; 8165 break; 8166 case Intrinsic::ppc_altivec_vcmpnezw: 8167 CompareOpc = 391; 8168 break; 8169 } 8170 else 8171 return false; 8172 break; 8173 case Intrinsic::ppc_altivec_vcmpgefp: 8174 CompareOpc = 454; 8175 break; 8176 case Intrinsic::ppc_altivec_vcmpgtfp: 8177 CompareOpc = 710; 8178 break; 8179 case Intrinsic::ppc_altivec_vcmpgtsb: 8180 CompareOpc = 774; 8181 break; 8182 case Intrinsic::ppc_altivec_vcmpgtsh: 8183 CompareOpc = 838; 8184 break; 8185 case Intrinsic::ppc_altivec_vcmpgtsw: 8186 CompareOpc = 902; 8187 break; 8188 case Intrinsic::ppc_altivec_vcmpgtsd: 8189 if (Subtarget.hasP8Altivec()) 8190 CompareOpc = 967; 8191 else 8192 return false; 8193 break; 8194 case Intrinsic::ppc_altivec_vcmpgtub: 8195 CompareOpc = 518; 8196 break; 8197 case Intrinsic::ppc_altivec_vcmpgtuh: 8198 CompareOpc = 582; 8199 break; 8200 case Intrinsic::ppc_altivec_vcmpgtuw: 8201 CompareOpc = 646; 8202 break; 8203 case Intrinsic::ppc_altivec_vcmpgtud: 8204 if (Subtarget.hasP8Altivec()) 8205 CompareOpc = 711; 8206 else 8207 return false; 8208 break; 8209 } 8210 return true; 8211 } 8212 8213 /// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom 8214 /// lower, do it, otherwise return null. 8215 SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, 8216 SelectionDAG &DAG) const { 8217 unsigned IntrinsicID = 8218 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 8219 8220 if (IntrinsicID == Intrinsic::thread_pointer) { 8221 // Reads the thread pointer register, used for __builtin_thread_pointer. 8222 bool is64bit = Subtarget.isPPC64(); 8223 return DAG.getRegister(is64bit ? PPC::X13 : PPC::R2, 8224 is64bit ? MVT::i64 : MVT::i32); 8225 } 8226 8227 // If this is a lowered altivec predicate compare, CompareOpc is set to the 8228 // opcode number of the comparison. 8229 SDLoc dl(Op); 8230 int CompareOpc; 8231 bool isDot; 8232 if (!getVectorCompareInfo(Op, CompareOpc, isDot, Subtarget)) 8233 return SDValue(); // Don't custom lower most intrinsics. 8234 8235 // If this is a non-dot comparison, make the VCMP node and we are done. 8236 if (!isDot) { 8237 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(), 8238 Op.getOperand(1), Op.getOperand(2), 8239 DAG.getConstant(CompareOpc, dl, MVT::i32)); 8240 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp); 8241 } 8242 8243 // Create the PPCISD altivec 'dot' comparison node. 8244 SDValue Ops[] = { 8245 Op.getOperand(2), // LHS 8246 Op.getOperand(3), // RHS 8247 DAG.getConstant(CompareOpc, dl, MVT::i32) 8248 }; 8249 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue }; 8250 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops); 8251 8252 // Now that we have the comparison, emit a copy from the CR to a GPR. 8253 // This is flagged to the above dot comparison. 8254 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32, 8255 DAG.getRegister(PPC::CR6, MVT::i32), 8256 CompNode.getValue(1)); 8257 8258 // Unpack the result based on how the target uses it. 8259 unsigned BitNo; // Bit # of CR6. 8260 bool InvertBit; // Invert result? 8261 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) { 8262 default: // Can't happen, don't crash on invalid number though. 8263 case 0: // Return the value of the EQ bit of CR6. 8264 BitNo = 0; InvertBit = false; 8265 break; 8266 case 1: // Return the inverted value of the EQ bit of CR6. 8267 BitNo = 0; InvertBit = true; 8268 break; 8269 case 2: // Return the value of the LT bit of CR6. 8270 BitNo = 2; InvertBit = false; 8271 break; 8272 case 3: // Return the inverted value of the LT bit of CR6. 8273 BitNo = 2; InvertBit = true; 8274 break; 8275 } 8276 8277 // Shift the bit into the low position. 8278 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags, 8279 DAG.getConstant(8 - (3 - BitNo), dl, MVT::i32)); 8280 // Isolate the bit. 8281 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags, 8282 DAG.getConstant(1, dl, MVT::i32)); 8283 8284 // If we are supposed to, toggle the bit. 8285 if (InvertBit) 8286 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags, 8287 DAG.getConstant(1, dl, MVT::i32)); 8288 return Flags; 8289 } 8290 8291 SDValue PPCTargetLowering::LowerINTRINSIC_VOID(SDValue Op, 8292 SelectionDAG &DAG) const { 8293 // SelectionDAGBuilder::visitTargetIntrinsic may insert one extra chain to 8294 // the beginning of the argument list. 8295 int ArgStart = isa<ConstantSDNode>(Op.getOperand(0)) ? 0 : 1; 8296 SDLoc DL(Op); 8297 switch (cast<ConstantSDNode>(Op.getOperand(ArgStart))->getZExtValue()) { 8298 case Intrinsic::ppc_cfence: { 8299 assert(Subtarget.isPPC64() && "Only 64-bit is supported for now."); 8300 return SDValue(DAG.getMachineNode(PPC::CFENCE8, DL, MVT::Other, 8301 DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, 8302 Op.getOperand(ArgStart + 1))), 8303 0); 8304 } 8305 default: 8306 break; 8307 } 8308 return SDValue(); 8309 } 8310 8311 SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, 8312 SelectionDAG &DAG) const { 8313 SDLoc dl(Op); 8314 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int 8315 // instructions), but for smaller types, we need to first extend up to v2i32 8316 // before doing going farther. 8317 if (Op.getValueType() == MVT::v2i64) { 8318 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 8319 if (ExtVT != MVT::v2i32) { 8320 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)); 8321 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op, 8322 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(), 8323 ExtVT.getVectorElementType(), 4))); 8324 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op); 8325 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op, 8326 DAG.getValueType(MVT::v2i32)); 8327 } 8328 8329 return Op; 8330 } 8331 8332 return SDValue(); 8333 } 8334 8335 SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, 8336 SelectionDAG &DAG) const { 8337 SDLoc dl(Op); 8338 // Create a stack slot that is 16-byte aligned. 8339 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 8340 int FrameIdx = MFI.CreateStackObject(16, 16, false); 8341 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 8342 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 8343 8344 // Store the input value into Value#0 of the stack slot. 8345 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx, 8346 MachinePointerInfo()); 8347 // Load it out. 8348 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo()); 8349 } 8350 8351 SDValue PPCTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, 8352 SelectionDAG &DAG) const { 8353 assert(Op.getOpcode() == ISD::INSERT_VECTOR_ELT && 8354 "Should only be called for ISD::INSERT_VECTOR_ELT"); 8355 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 8356 // We have legal lowering for constant indices but not for variable ones. 8357 if (C) 8358 return Op; 8359 return SDValue(); 8360 } 8361 8362 SDValue PPCTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, 8363 SelectionDAG &DAG) const { 8364 SDLoc dl(Op); 8365 SDNode *N = Op.getNode(); 8366 8367 assert(N->getOperand(0).getValueType() == MVT::v4i1 && 8368 "Unknown extract_vector_elt type"); 8369 8370 SDValue Value = N->getOperand(0); 8371 8372 // The first part of this is like the store lowering except that we don't 8373 // need to track the chain. 8374 8375 // The values are now known to be -1 (false) or 1 (true). To convert this 8376 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5). 8377 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5 8378 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value); 8379 8380 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to 8381 // understand how to form the extending load. 8382 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64); 8383 8384 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs); 8385 8386 // Now convert to an integer and store. 8387 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64, 8388 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32), 8389 Value); 8390 8391 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 8392 int FrameIdx = MFI.CreateStackObject(16, 16, false); 8393 MachinePointerInfo PtrInfo = 8394 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); 8395 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 8396 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 8397 8398 SDValue StoreChain = DAG.getEntryNode(); 8399 SDValue Ops[] = {StoreChain, 8400 DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32), 8401 Value, FIdx}; 8402 SDVTList VTs = DAG.getVTList(/*chain*/ MVT::Other); 8403 8404 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID, 8405 dl, VTs, Ops, MVT::v4i32, PtrInfo); 8406 8407 // Extract the value requested. 8408 unsigned Offset = 4*cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 8409 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType()); 8410 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx); 8411 8412 SDValue IntVal = 8413 DAG.getLoad(MVT::i32, dl, StoreChain, Idx, PtrInfo.getWithOffset(Offset)); 8414 8415 if (!Subtarget.useCRBits()) 8416 return IntVal; 8417 8418 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, IntVal); 8419 } 8420 8421 /// Lowering for QPX v4i1 loads 8422 SDValue PPCTargetLowering::LowerVectorLoad(SDValue Op, 8423 SelectionDAG &DAG) const { 8424 SDLoc dl(Op); 8425 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode()); 8426 SDValue LoadChain = LN->getChain(); 8427 SDValue BasePtr = LN->getBasePtr(); 8428 8429 if (Op.getValueType() == MVT::v4f64 || 8430 Op.getValueType() == MVT::v4f32) { 8431 EVT MemVT = LN->getMemoryVT(); 8432 unsigned Alignment = LN->getAlignment(); 8433 8434 // If this load is properly aligned, then it is legal. 8435 if (Alignment >= MemVT.getStoreSize()) 8436 return Op; 8437 8438 EVT ScalarVT = Op.getValueType().getScalarType(), 8439 ScalarMemVT = MemVT.getScalarType(); 8440 unsigned Stride = ScalarMemVT.getStoreSize(); 8441 8442 SDValue Vals[4], LoadChains[4]; 8443 for (unsigned Idx = 0; Idx < 4; ++Idx) { 8444 SDValue Load; 8445 if (ScalarVT != ScalarMemVT) 8446 Load = DAG.getExtLoad(LN->getExtensionType(), dl, ScalarVT, LoadChain, 8447 BasePtr, 8448 LN->getPointerInfo().getWithOffset(Idx * Stride), 8449 ScalarMemVT, MinAlign(Alignment, Idx * Stride), 8450 LN->getMemOperand()->getFlags(), LN->getAAInfo()); 8451 else 8452 Load = DAG.getLoad(ScalarVT, dl, LoadChain, BasePtr, 8453 LN->getPointerInfo().getWithOffset(Idx * Stride), 8454 MinAlign(Alignment, Idx * Stride), 8455 LN->getMemOperand()->getFlags(), LN->getAAInfo()); 8456 8457 if (Idx == 0 && LN->isIndexed()) { 8458 assert(LN->getAddressingMode() == ISD::PRE_INC && 8459 "Unknown addressing mode on vector load"); 8460 Load = DAG.getIndexedLoad(Load, dl, BasePtr, LN->getOffset(), 8461 LN->getAddressingMode()); 8462 } 8463 8464 Vals[Idx] = Load; 8465 LoadChains[Idx] = Load.getValue(1); 8466 8467 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, 8468 DAG.getConstant(Stride, dl, 8469 BasePtr.getValueType())); 8470 } 8471 8472 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains); 8473 SDValue Value = DAG.getBuildVector(Op.getValueType(), dl, Vals); 8474 8475 if (LN->isIndexed()) { 8476 SDValue RetOps[] = { Value, Vals[0].getValue(1), TF }; 8477 return DAG.getMergeValues(RetOps, dl); 8478 } 8479 8480 SDValue RetOps[] = { Value, TF }; 8481 return DAG.getMergeValues(RetOps, dl); 8482 } 8483 8484 assert(Op.getValueType() == MVT::v4i1 && "Unknown load to lower"); 8485 assert(LN->isUnindexed() && "Indexed v4i1 loads are not supported"); 8486 8487 // To lower v4i1 from a byte array, we load the byte elements of the 8488 // vector and then reuse the BUILD_VECTOR logic. 8489 8490 SDValue VectElmts[4], VectElmtChains[4]; 8491 for (unsigned i = 0; i < 4; ++i) { 8492 SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType()); 8493 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx); 8494 8495 VectElmts[i] = DAG.getExtLoad( 8496 ISD::EXTLOAD, dl, MVT::i32, LoadChain, Idx, 8497 LN->getPointerInfo().getWithOffset(i), MVT::i8, 8498 /* Alignment = */ 1, LN->getMemOperand()->getFlags(), LN->getAAInfo()); 8499 VectElmtChains[i] = VectElmts[i].getValue(1); 8500 } 8501 8502 LoadChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, VectElmtChains); 8503 SDValue Value = DAG.getBuildVector(MVT::v4i1, dl, VectElmts); 8504 8505 SDValue RVals[] = { Value, LoadChain }; 8506 return DAG.getMergeValues(RVals, dl); 8507 } 8508 8509 /// Lowering for QPX v4i1 stores 8510 SDValue PPCTargetLowering::LowerVectorStore(SDValue Op, 8511 SelectionDAG &DAG) const { 8512 SDLoc dl(Op); 8513 StoreSDNode *SN = cast<StoreSDNode>(Op.getNode()); 8514 SDValue StoreChain = SN->getChain(); 8515 SDValue BasePtr = SN->getBasePtr(); 8516 SDValue Value = SN->getValue(); 8517 8518 if (Value.getValueType() == MVT::v4f64 || 8519 Value.getValueType() == MVT::v4f32) { 8520 EVT MemVT = SN->getMemoryVT(); 8521 unsigned Alignment = SN->getAlignment(); 8522 8523 // If this store is properly aligned, then it is legal. 8524 if (Alignment >= MemVT.getStoreSize()) 8525 return Op; 8526 8527 EVT ScalarVT = Value.getValueType().getScalarType(), 8528 ScalarMemVT = MemVT.getScalarType(); 8529 unsigned Stride = ScalarMemVT.getStoreSize(); 8530 8531 SDValue Stores[4]; 8532 for (unsigned Idx = 0; Idx < 4; ++Idx) { 8533 SDValue Ex = DAG.getNode( 8534 ISD::EXTRACT_VECTOR_ELT, dl, ScalarVT, Value, 8535 DAG.getConstant(Idx, dl, getVectorIdxTy(DAG.getDataLayout()))); 8536 SDValue Store; 8537 if (ScalarVT != ScalarMemVT) 8538 Store = 8539 DAG.getTruncStore(StoreChain, dl, Ex, BasePtr, 8540 SN->getPointerInfo().getWithOffset(Idx * Stride), 8541 ScalarMemVT, MinAlign(Alignment, Idx * Stride), 8542 SN->getMemOperand()->getFlags(), SN->getAAInfo()); 8543 else 8544 Store = DAG.getStore(StoreChain, dl, Ex, BasePtr, 8545 SN->getPointerInfo().getWithOffset(Idx * Stride), 8546 MinAlign(Alignment, Idx * Stride), 8547 SN->getMemOperand()->getFlags(), SN->getAAInfo()); 8548 8549 if (Idx == 0 && SN->isIndexed()) { 8550 assert(SN->getAddressingMode() == ISD::PRE_INC && 8551 "Unknown addressing mode on vector store"); 8552 Store = DAG.getIndexedStore(Store, dl, BasePtr, SN->getOffset(), 8553 SN->getAddressingMode()); 8554 } 8555 8556 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, 8557 DAG.getConstant(Stride, dl, 8558 BasePtr.getValueType())); 8559 Stores[Idx] = Store; 8560 } 8561 8562 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 8563 8564 if (SN->isIndexed()) { 8565 SDValue RetOps[] = { TF, Stores[0].getValue(1) }; 8566 return DAG.getMergeValues(RetOps, dl); 8567 } 8568 8569 return TF; 8570 } 8571 8572 assert(SN->isUnindexed() && "Indexed v4i1 stores are not supported"); 8573 assert(Value.getValueType() == MVT::v4i1 && "Unknown store to lower"); 8574 8575 // The values are now known to be -1 (false) or 1 (true). To convert this 8576 // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5). 8577 // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5 8578 Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value); 8579 8580 // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to 8581 // understand how to form the extending load. 8582 SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64); 8583 8584 Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs); 8585 8586 // Now convert to an integer and store. 8587 Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64, 8588 DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32), 8589 Value); 8590 8591 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 8592 int FrameIdx = MFI.CreateStackObject(16, 16, false); 8593 MachinePointerInfo PtrInfo = 8594 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); 8595 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 8596 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); 8597 8598 SDValue Ops[] = {StoreChain, 8599 DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32), 8600 Value, FIdx}; 8601 SDVTList VTs = DAG.getVTList(/*chain*/ MVT::Other); 8602 8603 StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID, 8604 dl, VTs, Ops, MVT::v4i32, PtrInfo); 8605 8606 // Move data into the byte array. 8607 SDValue Loads[4], LoadChains[4]; 8608 for (unsigned i = 0; i < 4; ++i) { 8609 unsigned Offset = 4*i; 8610 SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType()); 8611 Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx); 8612 8613 Loads[i] = DAG.getLoad(MVT::i32, dl, StoreChain, Idx, 8614 PtrInfo.getWithOffset(Offset)); 8615 LoadChains[i] = Loads[i].getValue(1); 8616 } 8617 8618 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains); 8619 8620 SDValue Stores[4]; 8621 for (unsigned i = 0; i < 4; ++i) { 8622 SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType()); 8623 Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx); 8624 8625 Stores[i] = DAG.getTruncStore( 8626 StoreChain, dl, Loads[i], Idx, SN->getPointerInfo().getWithOffset(i), 8627 MVT::i8, /* Alignment = */ 1, SN->getMemOperand()->getFlags(), 8628 SN->getAAInfo()); 8629 } 8630 8631 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 8632 8633 return StoreChain; 8634 } 8635 8636 SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const { 8637 SDLoc dl(Op); 8638 if (Op.getValueType() == MVT::v4i32) { 8639 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); 8640 8641 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl); 8642 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt. 8643 8644 SDValue RHSSwap = // = vrlw RHS, 16 8645 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl); 8646 8647 // Shrinkify inputs to v8i16. 8648 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS); 8649 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS); 8650 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap); 8651 8652 // Low parts multiplied together, generating 32-bit results (we ignore the 8653 // top parts). 8654 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh, 8655 LHS, RHS, DAG, dl, MVT::v4i32); 8656 8657 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm, 8658 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32); 8659 // Shift the high parts up 16 bits. 8660 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd, 8661 Neg16, DAG, dl); 8662 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd); 8663 } else if (Op.getValueType() == MVT::v8i16) { 8664 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); 8665 8666 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl); 8667 8668 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm, 8669 LHS, RHS, Zero, DAG, dl); 8670 } else if (Op.getValueType() == MVT::v16i8) { 8671 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); 8672 bool isLittleEndian = Subtarget.isLittleEndian(); 8673 8674 // Multiply the even 8-bit parts, producing 16-bit sums. 8675 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub, 8676 LHS, RHS, DAG, dl, MVT::v8i16); 8677 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts); 8678 8679 // Multiply the odd 8-bit parts, producing 16-bit sums. 8680 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub, 8681 LHS, RHS, DAG, dl, MVT::v8i16); 8682 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts); 8683 8684 // Merge the results together. Because vmuleub and vmuloub are 8685 // instructions with a big-endian bias, we must reverse the 8686 // element numbering and reverse the meaning of "odd" and "even" 8687 // when generating little endian code. 8688 int Ops[16]; 8689 for (unsigned i = 0; i != 8; ++i) { 8690 if (isLittleEndian) { 8691 Ops[i*2 ] = 2*i; 8692 Ops[i*2+1] = 2*i+16; 8693 } else { 8694 Ops[i*2 ] = 2*i+1; 8695 Ops[i*2+1] = 2*i+1+16; 8696 } 8697 } 8698 if (isLittleEndian) 8699 return DAG.getVectorShuffle(MVT::v16i8, dl, OddParts, EvenParts, Ops); 8700 else 8701 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops); 8702 } else { 8703 llvm_unreachable("Unknown mul to lower!"); 8704 } 8705 } 8706 8707 /// LowerOperation - Provide custom lowering hooks for some operations. 8708 /// 8709 SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 8710 switch (Op.getOpcode()) { 8711 default: llvm_unreachable("Wasn't expecting to be able to lower this!"); 8712 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 8713 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 8714 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); 8715 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 8716 case ISD::JumpTable: return LowerJumpTable(Op, DAG); 8717 case ISD::SETCC: return LowerSETCC(Op, DAG); 8718 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG); 8719 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG); 8720 case ISD::VASTART: 8721 return LowerVASTART(Op, DAG); 8722 8723 case ISD::VAARG: 8724 return LowerVAARG(Op, DAG); 8725 8726 case ISD::VACOPY: 8727 return LowerVACOPY(Op, DAG); 8728 8729 case ISD::STACKRESTORE: 8730 return LowerSTACKRESTORE(Op, DAG); 8731 8732 case ISD::DYNAMIC_STACKALLOC: 8733 return LowerDYNAMIC_STACKALLOC(Op, DAG); 8734 8735 case ISD::GET_DYNAMIC_AREA_OFFSET: 8736 return LowerGET_DYNAMIC_AREA_OFFSET(Op, DAG); 8737 8738 case ISD::EH_DWARF_CFA: 8739 return LowerEH_DWARF_CFA(Op, DAG); 8740 8741 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG); 8742 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG); 8743 8744 case ISD::LOAD: return LowerLOAD(Op, DAG); 8745 case ISD::STORE: return LowerSTORE(Op, DAG); 8746 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG); 8747 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 8748 case ISD::FP_TO_UINT: 8749 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG, 8750 SDLoc(Op)); 8751 case ISD::UINT_TO_FP: 8752 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG); 8753 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); 8754 8755 // Lower 64-bit shifts. 8756 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG); 8757 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG); 8758 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG); 8759 8760 // Vector-related lowering. 8761 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); 8762 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 8763 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); 8764 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); 8765 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG); 8766 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 8767 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 8768 case ISD::MUL: return LowerMUL(Op, DAG); 8769 8770 // For counter-based loop handling. 8771 case ISD::INTRINSIC_W_CHAIN: return SDValue(); 8772 8773 // Frame & Return address. 8774 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 8775 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 8776 8777 case ISD::INTRINSIC_VOID: 8778 return LowerINTRINSIC_VOID(Op, DAG); 8779 } 8780 } 8781 8782 void PPCTargetLowering::ReplaceNodeResults(SDNode *N, 8783 SmallVectorImpl<SDValue>&Results, 8784 SelectionDAG &DAG) const { 8785 SDLoc dl(N); 8786 switch (N->getOpcode()) { 8787 default: 8788 llvm_unreachable("Do not know how to custom type legalize this operation!"); 8789 case ISD::READCYCLECOUNTER: { 8790 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other); 8791 SDValue RTB = DAG.getNode(PPCISD::READ_TIME_BASE, dl, VTs, N->getOperand(0)); 8792 8793 Results.push_back(RTB); 8794 Results.push_back(RTB.getValue(1)); 8795 Results.push_back(RTB.getValue(2)); 8796 break; 8797 } 8798 case ISD::INTRINSIC_W_CHAIN: { 8799 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 8800 Intrinsic::ppc_is_decremented_ctr_nonzero) 8801 break; 8802 8803 assert(N->getValueType(0) == MVT::i1 && 8804 "Unexpected result type for CTR decrement intrinsic"); 8805 EVT SVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), 8806 N->getValueType(0)); 8807 SDVTList VTs = DAG.getVTList(SVT, MVT::Other); 8808 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0), 8809 N->getOperand(1)); 8810 8811 Results.push_back(NewInt); 8812 Results.push_back(NewInt.getValue(1)); 8813 break; 8814 } 8815 case ISD::VAARG: { 8816 if (!Subtarget.isSVR4ABI() || Subtarget.isPPC64()) 8817 return; 8818 8819 EVT VT = N->getValueType(0); 8820 8821 if (VT == MVT::i64) { 8822 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG); 8823 8824 Results.push_back(NewNode); 8825 Results.push_back(NewNode.getValue(1)); 8826 } 8827 return; 8828 } 8829 case ISD::FP_ROUND_INREG: { 8830 assert(N->getValueType(0) == MVT::ppcf128); 8831 assert(N->getOperand(0).getValueType() == MVT::ppcf128); 8832 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, 8833 MVT::f64, N->getOperand(0), 8834 DAG.getIntPtrConstant(0, dl)); 8835 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, 8836 MVT::f64, N->getOperand(0), 8837 DAG.getIntPtrConstant(1, dl)); 8838 8839 // Add the two halves of the long double in round-to-zero mode. 8840 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi); 8841 8842 // We know the low half is about to be thrown away, so just use something 8843 // convenient. 8844 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128, 8845 FPreg, FPreg)); 8846 return; 8847 } 8848 case ISD::FP_TO_SINT: 8849 case ISD::FP_TO_UINT: 8850 // LowerFP_TO_INT() can only handle f32 and f64. 8851 if (N->getOperand(0).getValueType() == MVT::ppcf128) 8852 return; 8853 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl)); 8854 return; 8855 } 8856 } 8857 8858 //===----------------------------------------------------------------------===// 8859 // Other Lowering Code 8860 //===----------------------------------------------------------------------===// 8861 8862 static Instruction* callIntrinsic(IRBuilder<> &Builder, Intrinsic::ID Id) { 8863 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 8864 Function *Func = Intrinsic::getDeclaration(M, Id); 8865 return Builder.CreateCall(Func, {}); 8866 } 8867 8868 // The mappings for emitLeading/TrailingFence is taken from 8869 // http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html 8870 Instruction *PPCTargetLowering::emitLeadingFence(IRBuilder<> &Builder, 8871 Instruction *Inst, 8872 AtomicOrdering Ord) const { 8873 if (Ord == AtomicOrdering::SequentiallyConsistent) 8874 return callIntrinsic(Builder, Intrinsic::ppc_sync); 8875 if (isReleaseOrStronger(Ord)) 8876 return callIntrinsic(Builder, Intrinsic::ppc_lwsync); 8877 return nullptr; 8878 } 8879 8880 Instruction *PPCTargetLowering::emitTrailingFence(IRBuilder<> &Builder, 8881 Instruction *Inst, 8882 AtomicOrdering Ord) const { 8883 if (Inst->hasAtomicLoad() && isAcquireOrStronger(Ord)) { 8884 // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html and 8885 // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2011.03.04a.html 8886 // and http://www.cl.cam.ac.uk/~pes20/cppppc/ for justification. 8887 if (isa<LoadInst>(Inst) && Subtarget.isPPC64()) 8888 return Builder.CreateCall( 8889 Intrinsic::getDeclaration( 8890 Builder.GetInsertBlock()->getParent()->getParent(), 8891 Intrinsic::ppc_cfence, {Inst->getType()}), 8892 {Inst}); 8893 // FIXME: Can use isync for rmw operation. 8894 return callIntrinsic(Builder, Intrinsic::ppc_lwsync); 8895 } 8896 return nullptr; 8897 } 8898 8899 MachineBasicBlock * 8900 PPCTargetLowering::EmitAtomicBinary(MachineInstr &MI, MachineBasicBlock *BB, 8901 unsigned AtomicSize, 8902 unsigned BinOpcode, 8903 unsigned CmpOpcode, 8904 unsigned CmpPred) const { 8905 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. 8906 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 8907 8908 auto LoadMnemonic = PPC::LDARX; 8909 auto StoreMnemonic = PPC::STDCX; 8910 switch (AtomicSize) { 8911 default: 8912 llvm_unreachable("Unexpected size of atomic entity"); 8913 case 1: 8914 LoadMnemonic = PPC::LBARX; 8915 StoreMnemonic = PPC::STBCX; 8916 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4"); 8917 break; 8918 case 2: 8919 LoadMnemonic = PPC::LHARX; 8920 StoreMnemonic = PPC::STHCX; 8921 assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4"); 8922 break; 8923 case 4: 8924 LoadMnemonic = PPC::LWARX; 8925 StoreMnemonic = PPC::STWCX; 8926 break; 8927 case 8: 8928 LoadMnemonic = PPC::LDARX; 8929 StoreMnemonic = PPC::STDCX; 8930 break; 8931 } 8932 8933 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 8934 MachineFunction *F = BB->getParent(); 8935 MachineFunction::iterator It = ++BB->getIterator(); 8936 8937 unsigned dest = MI.getOperand(0).getReg(); 8938 unsigned ptrA = MI.getOperand(1).getReg(); 8939 unsigned ptrB = MI.getOperand(2).getReg(); 8940 unsigned incr = MI.getOperand(3).getReg(); 8941 DebugLoc dl = MI.getDebugLoc(); 8942 8943 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB); 8944 MachineBasicBlock *loop2MBB = 8945 CmpOpcode ? F->CreateMachineBasicBlock(LLVM_BB) : nullptr; 8946 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); 8947 F->insert(It, loopMBB); 8948 if (CmpOpcode) 8949 F->insert(It, loop2MBB); 8950 F->insert(It, exitMBB); 8951 exitMBB->splice(exitMBB->begin(), BB, 8952 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 8953 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 8954 8955 MachineRegisterInfo &RegInfo = F->getRegInfo(); 8956 unsigned TmpReg = (!BinOpcode) ? incr : 8957 RegInfo.createVirtualRegister( AtomicSize == 8 ? &PPC::G8RCRegClass 8958 : &PPC::GPRCRegClass); 8959 8960 // thisMBB: 8961 // ... 8962 // fallthrough --> loopMBB 8963 BB->addSuccessor(loopMBB); 8964 8965 // loopMBB: 8966 // l[wd]arx dest, ptr 8967 // add r0, dest, incr 8968 // st[wd]cx. r0, ptr 8969 // bne- loopMBB 8970 // fallthrough --> exitMBB 8971 8972 // For max/min... 8973 // loopMBB: 8974 // l[wd]arx dest, ptr 8975 // cmpl?[wd] incr, dest 8976 // bgt exitMBB 8977 // loop2MBB: 8978 // st[wd]cx. dest, ptr 8979 // bne- loopMBB 8980 // fallthrough --> exitMBB 8981 8982 BB = loopMBB; 8983 BuildMI(BB, dl, TII->get(LoadMnemonic), dest) 8984 .addReg(ptrA).addReg(ptrB); 8985 if (BinOpcode) 8986 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest); 8987 if (CmpOpcode) { 8988 // Signed comparisons of byte or halfword values must be sign-extended. 8989 if (CmpOpcode == PPC::CMPW && AtomicSize < 4) { 8990 unsigned ExtReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass); 8991 BuildMI(BB, dl, TII->get(AtomicSize == 1 ? PPC::EXTSB : PPC::EXTSH), 8992 ExtReg).addReg(dest); 8993 BuildMI(BB, dl, TII->get(CmpOpcode), PPC::CR0) 8994 .addReg(incr).addReg(ExtReg); 8995 } else 8996 BuildMI(BB, dl, TII->get(CmpOpcode), PPC::CR0) 8997 .addReg(incr).addReg(dest); 8998 8999 BuildMI(BB, dl, TII->get(PPC::BCC)) 9000 .addImm(CmpPred).addReg(PPC::CR0).addMBB(exitMBB); 9001 BB->addSuccessor(loop2MBB); 9002 BB->addSuccessor(exitMBB); 9003 BB = loop2MBB; 9004 } 9005 BuildMI(BB, dl, TII->get(StoreMnemonic)) 9006 .addReg(TmpReg).addReg(ptrA).addReg(ptrB); 9007 BuildMI(BB, dl, TII->get(PPC::BCC)) 9008 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB); 9009 BB->addSuccessor(loopMBB); 9010 BB->addSuccessor(exitMBB); 9011 9012 // exitMBB: 9013 // ... 9014 BB = exitMBB; 9015 return BB; 9016 } 9017 9018 MachineBasicBlock * 9019 PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr &MI, 9020 MachineBasicBlock *BB, 9021 bool is8bit, // operation 9022 unsigned BinOpcode, 9023 unsigned CmpOpcode, 9024 unsigned CmpPred) const { 9025 // If we support part-word atomic mnemonics, just use them 9026 if (Subtarget.hasPartwordAtomics()) 9027 return EmitAtomicBinary(MI, BB, is8bit ? 1 : 2, BinOpcode, 9028 CmpOpcode, CmpPred); 9029 9030 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. 9031 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 9032 // In 64 bit mode we have to use 64 bits for addresses, even though the 9033 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address 9034 // registers without caring whether they're 32 or 64, but here we're 9035 // doing actual arithmetic on the addresses. 9036 bool is64bit = Subtarget.isPPC64(); 9037 bool isLittleEndian = Subtarget.isLittleEndian(); 9038 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO; 9039 9040 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 9041 MachineFunction *F = BB->getParent(); 9042 MachineFunction::iterator It = ++BB->getIterator(); 9043 9044 unsigned dest = MI.getOperand(0).getReg(); 9045 unsigned ptrA = MI.getOperand(1).getReg(); 9046 unsigned ptrB = MI.getOperand(2).getReg(); 9047 unsigned incr = MI.getOperand(3).getReg(); 9048 DebugLoc dl = MI.getDebugLoc(); 9049 9050 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB); 9051 MachineBasicBlock *loop2MBB = 9052 CmpOpcode ? F->CreateMachineBasicBlock(LLVM_BB) : nullptr; 9053 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); 9054 F->insert(It, loopMBB); 9055 if (CmpOpcode) 9056 F->insert(It, loop2MBB); 9057 F->insert(It, exitMBB); 9058 exitMBB->splice(exitMBB->begin(), BB, 9059 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 9060 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 9061 9062 MachineRegisterInfo &RegInfo = F->getRegInfo(); 9063 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass 9064 : &PPC::GPRCRegClass; 9065 unsigned PtrReg = RegInfo.createVirtualRegister(RC); 9066 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC); 9067 unsigned ShiftReg = 9068 isLittleEndian ? Shift1Reg : RegInfo.createVirtualRegister(RC); 9069 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC); 9070 unsigned MaskReg = RegInfo.createVirtualRegister(RC); 9071 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC); 9072 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC); 9073 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC); 9074 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC); 9075 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC); 9076 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC); 9077 unsigned Ptr1Reg; 9078 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC); 9079 9080 // thisMBB: 9081 // ... 9082 // fallthrough --> loopMBB 9083 BB->addSuccessor(loopMBB); 9084 9085 // The 4-byte load must be aligned, while a char or short may be 9086 // anywhere in the word. Hence all this nasty bookkeeping code. 9087 // add ptr1, ptrA, ptrB [copy if ptrA==0] 9088 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27] 9089 // xori shift, shift1, 24 [16] 9090 // rlwinm ptr, ptr1, 0, 0, 29 9091 // slw incr2, incr, shift 9092 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535] 9093 // slw mask, mask2, shift 9094 // loopMBB: 9095 // lwarx tmpDest, ptr 9096 // add tmp, tmpDest, incr2 9097 // andc tmp2, tmpDest, mask 9098 // and tmp3, tmp, mask 9099 // or tmp4, tmp3, tmp2 9100 // stwcx. tmp4, ptr 9101 // bne- loopMBB 9102 // fallthrough --> exitMBB 9103 // srw dest, tmpDest, shift 9104 if (ptrA != ZeroReg) { 9105 Ptr1Reg = RegInfo.createVirtualRegister(RC); 9106 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg) 9107 .addReg(ptrA).addReg(ptrB); 9108 } else { 9109 Ptr1Reg = ptrB; 9110 } 9111 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg) 9112 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27); 9113 if (!isLittleEndian) 9114 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg) 9115 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16); 9116 if (is64bit) 9117 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg) 9118 .addReg(Ptr1Reg).addImm(0).addImm(61); 9119 else 9120 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg) 9121 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29); 9122 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg) 9123 .addReg(incr).addReg(ShiftReg); 9124 if (is8bit) 9125 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255); 9126 else { 9127 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0); 9128 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535); 9129 } 9130 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg) 9131 .addReg(Mask2Reg).addReg(ShiftReg); 9132 9133 BB = loopMBB; 9134 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg) 9135 .addReg(ZeroReg).addReg(PtrReg); 9136 if (BinOpcode) 9137 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg) 9138 .addReg(Incr2Reg).addReg(TmpDestReg); 9139 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg) 9140 .addReg(TmpDestReg).addReg(MaskReg); 9141 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg) 9142 .addReg(TmpReg).addReg(MaskReg); 9143 if (CmpOpcode) { 9144 // For unsigned comparisons, we can directly compare the shifted values. 9145 // For signed comparisons we shift and sign extend. 9146 unsigned SReg = RegInfo.createVirtualRegister(RC); 9147 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), SReg) 9148 .addReg(TmpDestReg).addReg(MaskReg); 9149 unsigned ValueReg = SReg; 9150 unsigned CmpReg = Incr2Reg; 9151 if (CmpOpcode == PPC::CMPW) { 9152 ValueReg = RegInfo.createVirtualRegister(RC); 9153 BuildMI(BB, dl, TII->get(PPC::SRW), ValueReg) 9154 .addReg(SReg).addReg(ShiftReg); 9155 unsigned ValueSReg = RegInfo.createVirtualRegister(RC); 9156 BuildMI(BB, dl, TII->get(is8bit ? PPC::EXTSB : PPC::EXTSH), ValueSReg) 9157 .addReg(ValueReg); 9158 ValueReg = ValueSReg; 9159 CmpReg = incr; 9160 } 9161 BuildMI(BB, dl, TII->get(CmpOpcode), PPC::CR0) 9162 .addReg(CmpReg).addReg(ValueReg); 9163 BuildMI(BB, dl, TII->get(PPC::BCC)) 9164 .addImm(CmpPred).addReg(PPC::CR0).addMBB(exitMBB); 9165 BB->addSuccessor(loop2MBB); 9166 BB->addSuccessor(exitMBB); 9167 BB = loop2MBB; 9168 } 9169 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg) 9170 .addReg(Tmp3Reg).addReg(Tmp2Reg); 9171 BuildMI(BB, dl, TII->get(PPC::STWCX)) 9172 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg); 9173 BuildMI(BB, dl, TII->get(PPC::BCC)) 9174 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB); 9175 BB->addSuccessor(loopMBB); 9176 BB->addSuccessor(exitMBB); 9177 9178 // exitMBB: 9179 // ... 9180 BB = exitMBB; 9181 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg) 9182 .addReg(ShiftReg); 9183 return BB; 9184 } 9185 9186 llvm::MachineBasicBlock * 9187 PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr &MI, 9188 MachineBasicBlock *MBB) const { 9189 DebugLoc DL = MI.getDebugLoc(); 9190 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 9191 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo(); 9192 9193 MachineFunction *MF = MBB->getParent(); 9194 MachineRegisterInfo &MRI = MF->getRegInfo(); 9195 9196 const BasicBlock *BB = MBB->getBasicBlock(); 9197 MachineFunction::iterator I = ++MBB->getIterator(); 9198 9199 // Memory Reference 9200 MachineInstr::mmo_iterator MMOBegin = MI.memoperands_begin(); 9201 MachineInstr::mmo_iterator MMOEnd = MI.memoperands_end(); 9202 9203 unsigned DstReg = MI.getOperand(0).getReg(); 9204 const TargetRegisterClass *RC = MRI.getRegClass(DstReg); 9205 assert(TRI->isTypeLegalForClass(*RC, MVT::i32) && "Invalid destination!"); 9206 unsigned mainDstReg = MRI.createVirtualRegister(RC); 9207 unsigned restoreDstReg = MRI.createVirtualRegister(RC); 9208 9209 MVT PVT = getPointerTy(MF->getDataLayout()); 9210 assert((PVT == MVT::i64 || PVT == MVT::i32) && 9211 "Invalid Pointer Size!"); 9212 // For v = setjmp(buf), we generate 9213 // 9214 // thisMBB: 9215 // SjLjSetup mainMBB 9216 // bl mainMBB 9217 // v_restore = 1 9218 // b sinkMBB 9219 // 9220 // mainMBB: 9221 // buf[LabelOffset] = LR 9222 // v_main = 0 9223 // 9224 // sinkMBB: 9225 // v = phi(main, restore) 9226 // 9227 9228 MachineBasicBlock *thisMBB = MBB; 9229 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB); 9230 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB); 9231 MF->insert(I, mainMBB); 9232 MF->insert(I, sinkMBB); 9233 9234 MachineInstrBuilder MIB; 9235 9236 // Transfer the remainder of BB and its successor edges to sinkMBB. 9237 sinkMBB->splice(sinkMBB->begin(), MBB, 9238 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 9239 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB); 9240 9241 // Note that the structure of the jmp_buf used here is not compatible 9242 // with that used by libc, and is not designed to be. Specifically, it 9243 // stores only those 'reserved' registers that LLVM does not otherwise 9244 // understand how to spill. Also, by convention, by the time this 9245 // intrinsic is called, Clang has already stored the frame address in the 9246 // first slot of the buffer and stack address in the third. Following the 9247 // X86 target code, we'll store the jump address in the second slot. We also 9248 // need to save the TOC pointer (R2) to handle jumps between shared 9249 // libraries, and that will be stored in the fourth slot. The thread 9250 // identifier (R13) is not affected. 9251 9252 // thisMBB: 9253 const int64_t LabelOffset = 1 * PVT.getStoreSize(); 9254 const int64_t TOCOffset = 3 * PVT.getStoreSize(); 9255 const int64_t BPOffset = 4 * PVT.getStoreSize(); 9256 9257 // Prepare IP either in reg. 9258 const TargetRegisterClass *PtrRC = getRegClassFor(PVT); 9259 unsigned LabelReg = MRI.createVirtualRegister(PtrRC); 9260 unsigned BufReg = MI.getOperand(1).getReg(); 9261 9262 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI()) { 9263 setUsesTOCBasePtr(*MBB->getParent()); 9264 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD)) 9265 .addReg(PPC::X2) 9266 .addImm(TOCOffset) 9267 .addReg(BufReg); 9268 MIB.setMemRefs(MMOBegin, MMOEnd); 9269 } 9270 9271 // Naked functions never have a base pointer, and so we use r1. For all 9272 // other functions, this decision must be delayed until during PEI. 9273 unsigned BaseReg; 9274 if (MF->getFunction()->hasFnAttribute(Attribute::Naked)) 9275 BaseReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1; 9276 else 9277 BaseReg = Subtarget.isPPC64() ? PPC::BP8 : PPC::BP; 9278 9279 MIB = BuildMI(*thisMBB, MI, DL, 9280 TII->get(Subtarget.isPPC64() ? PPC::STD : PPC::STW)) 9281 .addReg(BaseReg) 9282 .addImm(BPOffset) 9283 .addReg(BufReg); 9284 MIB.setMemRefs(MMOBegin, MMOEnd); 9285 9286 // Setup 9287 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB); 9288 MIB.addRegMask(TRI->getNoPreservedMask()); 9289 9290 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1); 9291 9292 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup)) 9293 .addMBB(mainMBB); 9294 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB); 9295 9296 thisMBB->addSuccessor(mainMBB, BranchProbability::getZero()); 9297 thisMBB->addSuccessor(sinkMBB, BranchProbability::getOne()); 9298 9299 // mainMBB: 9300 // mainDstReg = 0 9301 MIB = 9302 BuildMI(mainMBB, DL, 9303 TII->get(Subtarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg); 9304 9305 // Store IP 9306 if (Subtarget.isPPC64()) { 9307 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD)) 9308 .addReg(LabelReg) 9309 .addImm(LabelOffset) 9310 .addReg(BufReg); 9311 } else { 9312 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW)) 9313 .addReg(LabelReg) 9314 .addImm(LabelOffset) 9315 .addReg(BufReg); 9316 } 9317 9318 MIB.setMemRefs(MMOBegin, MMOEnd); 9319 9320 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0); 9321 mainMBB->addSuccessor(sinkMBB); 9322 9323 // sinkMBB: 9324 BuildMI(*sinkMBB, sinkMBB->begin(), DL, 9325 TII->get(PPC::PHI), DstReg) 9326 .addReg(mainDstReg).addMBB(mainMBB) 9327 .addReg(restoreDstReg).addMBB(thisMBB); 9328 9329 MI.eraseFromParent(); 9330 return sinkMBB; 9331 } 9332 9333 MachineBasicBlock * 9334 PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr &MI, 9335 MachineBasicBlock *MBB) const { 9336 DebugLoc DL = MI.getDebugLoc(); 9337 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 9338 9339 MachineFunction *MF = MBB->getParent(); 9340 MachineRegisterInfo &MRI = MF->getRegInfo(); 9341 9342 // Memory Reference 9343 MachineInstr::mmo_iterator MMOBegin = MI.memoperands_begin(); 9344 MachineInstr::mmo_iterator MMOEnd = MI.memoperands_end(); 9345 9346 MVT PVT = getPointerTy(MF->getDataLayout()); 9347 assert((PVT == MVT::i64 || PVT == MVT::i32) && 9348 "Invalid Pointer Size!"); 9349 9350 const TargetRegisterClass *RC = 9351 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass; 9352 unsigned Tmp = MRI.createVirtualRegister(RC); 9353 // Since FP is only updated here but NOT referenced, it's treated as GPR. 9354 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31; 9355 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1; 9356 unsigned BP = 9357 (PVT == MVT::i64) 9358 ? PPC::X30 9359 : (Subtarget.isSVR4ABI() && isPositionIndependent() ? PPC::R29 9360 : PPC::R30); 9361 9362 MachineInstrBuilder MIB; 9363 9364 const int64_t LabelOffset = 1 * PVT.getStoreSize(); 9365 const int64_t SPOffset = 2 * PVT.getStoreSize(); 9366 const int64_t TOCOffset = 3 * PVT.getStoreSize(); 9367 const int64_t BPOffset = 4 * PVT.getStoreSize(); 9368 9369 unsigned BufReg = MI.getOperand(0).getReg(); 9370 9371 // Reload FP (the jumped-to function may not have had a 9372 // frame pointer, and if so, then its r31 will be restored 9373 // as necessary). 9374 if (PVT == MVT::i64) { 9375 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP) 9376 .addImm(0) 9377 .addReg(BufReg); 9378 } else { 9379 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP) 9380 .addImm(0) 9381 .addReg(BufReg); 9382 } 9383 MIB.setMemRefs(MMOBegin, MMOEnd); 9384 9385 // Reload IP 9386 if (PVT == MVT::i64) { 9387 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp) 9388 .addImm(LabelOffset) 9389 .addReg(BufReg); 9390 } else { 9391 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp) 9392 .addImm(LabelOffset) 9393 .addReg(BufReg); 9394 } 9395 MIB.setMemRefs(MMOBegin, MMOEnd); 9396 9397 // Reload SP 9398 if (PVT == MVT::i64) { 9399 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP) 9400 .addImm(SPOffset) 9401 .addReg(BufReg); 9402 } else { 9403 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP) 9404 .addImm(SPOffset) 9405 .addReg(BufReg); 9406 } 9407 MIB.setMemRefs(MMOBegin, MMOEnd); 9408 9409 // Reload BP 9410 if (PVT == MVT::i64) { 9411 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP) 9412 .addImm(BPOffset) 9413 .addReg(BufReg); 9414 } else { 9415 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP) 9416 .addImm(BPOffset) 9417 .addReg(BufReg); 9418 } 9419 MIB.setMemRefs(MMOBegin, MMOEnd); 9420 9421 // Reload TOC 9422 if (PVT == MVT::i64 && Subtarget.isSVR4ABI()) { 9423 setUsesTOCBasePtr(*MBB->getParent()); 9424 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2) 9425 .addImm(TOCOffset) 9426 .addReg(BufReg); 9427 9428 MIB.setMemRefs(MMOBegin, MMOEnd); 9429 } 9430 9431 // Jump 9432 BuildMI(*MBB, MI, DL, 9433 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp); 9434 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR)); 9435 9436 MI.eraseFromParent(); 9437 return MBB; 9438 } 9439 9440 MachineBasicBlock * 9441 PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, 9442 MachineBasicBlock *BB) const { 9443 if (MI.getOpcode() == TargetOpcode::STACKMAP || 9444 MI.getOpcode() == TargetOpcode::PATCHPOINT) { 9445 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI() && 9446 MI.getOpcode() == TargetOpcode::PATCHPOINT) { 9447 // Call lowering should have added an r2 operand to indicate a dependence 9448 // on the TOC base pointer value. It can't however, because there is no 9449 // way to mark the dependence as implicit there, and so the stackmap code 9450 // will confuse it with a regular operand. Instead, add the dependence 9451 // here. 9452 setUsesTOCBasePtr(*BB->getParent()); 9453 MI.addOperand(MachineOperand::CreateReg(PPC::X2, false, true)); 9454 } 9455 9456 return emitPatchPoint(MI, BB); 9457 } 9458 9459 if (MI.getOpcode() == PPC::EH_SjLj_SetJmp32 || 9460 MI.getOpcode() == PPC::EH_SjLj_SetJmp64) { 9461 return emitEHSjLjSetJmp(MI, BB); 9462 } else if (MI.getOpcode() == PPC::EH_SjLj_LongJmp32 || 9463 MI.getOpcode() == PPC::EH_SjLj_LongJmp64) { 9464 return emitEHSjLjLongJmp(MI, BB); 9465 } 9466 9467 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 9468 9469 // To "insert" these instructions we actually have to insert their 9470 // control-flow patterns. 9471 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 9472 MachineFunction::iterator It = ++BB->getIterator(); 9473 9474 MachineFunction *F = BB->getParent(); 9475 9476 if (MI.getOpcode() == PPC::SELECT_CC_I4 || 9477 MI.getOpcode() == PPC::SELECT_CC_I8 || 9478 MI.getOpcode() == PPC::SELECT_I4 || MI.getOpcode() == PPC::SELECT_I8) { 9479 SmallVector<MachineOperand, 2> Cond; 9480 if (MI.getOpcode() == PPC::SELECT_CC_I4 || 9481 MI.getOpcode() == PPC::SELECT_CC_I8) 9482 Cond.push_back(MI.getOperand(4)); 9483 else 9484 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); 9485 Cond.push_back(MI.getOperand(1)); 9486 9487 DebugLoc dl = MI.getDebugLoc(); 9488 TII->insertSelect(*BB, MI, dl, MI.getOperand(0).getReg(), Cond, 9489 MI.getOperand(2).getReg(), MI.getOperand(3).getReg()); 9490 } else if (MI.getOpcode() == PPC::SELECT_CC_I4 || 9491 MI.getOpcode() == PPC::SELECT_CC_I8 || 9492 MI.getOpcode() == PPC::SELECT_CC_F4 || 9493 MI.getOpcode() == PPC::SELECT_CC_F8 || 9494 MI.getOpcode() == PPC::SELECT_CC_QFRC || 9495 MI.getOpcode() == PPC::SELECT_CC_QSRC || 9496 MI.getOpcode() == PPC::SELECT_CC_QBRC || 9497 MI.getOpcode() == PPC::SELECT_CC_VRRC || 9498 MI.getOpcode() == PPC::SELECT_CC_VSFRC || 9499 MI.getOpcode() == PPC::SELECT_CC_VSSRC || 9500 MI.getOpcode() == PPC::SELECT_CC_VSRC || 9501 MI.getOpcode() == PPC::SELECT_I4 || 9502 MI.getOpcode() == PPC::SELECT_I8 || 9503 MI.getOpcode() == PPC::SELECT_F4 || 9504 MI.getOpcode() == PPC::SELECT_F8 || 9505 MI.getOpcode() == PPC::SELECT_QFRC || 9506 MI.getOpcode() == PPC::SELECT_QSRC || 9507 MI.getOpcode() == PPC::SELECT_QBRC || 9508 MI.getOpcode() == PPC::SELECT_VRRC || 9509 MI.getOpcode() == PPC::SELECT_VSFRC || 9510 MI.getOpcode() == PPC::SELECT_VSSRC || 9511 MI.getOpcode() == PPC::SELECT_VSRC) { 9512 // The incoming instruction knows the destination vreg to set, the 9513 // condition code register to branch on, the true/false values to 9514 // select between, and a branch opcode to use. 9515 9516 // thisMBB: 9517 // ... 9518 // TrueVal = ... 9519 // cmpTY ccX, r1, r2 9520 // bCC copy1MBB 9521 // fallthrough --> copy0MBB 9522 MachineBasicBlock *thisMBB = BB; 9523 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 9524 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 9525 DebugLoc dl = MI.getDebugLoc(); 9526 F->insert(It, copy0MBB); 9527 F->insert(It, sinkMBB); 9528 9529 // Transfer the remainder of BB and its successor edges to sinkMBB. 9530 sinkMBB->splice(sinkMBB->begin(), BB, 9531 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 9532 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 9533 9534 // Next, add the true and fallthrough blocks as its successors. 9535 BB->addSuccessor(copy0MBB); 9536 BB->addSuccessor(sinkMBB); 9537 9538 if (MI.getOpcode() == PPC::SELECT_I4 || MI.getOpcode() == PPC::SELECT_I8 || 9539 MI.getOpcode() == PPC::SELECT_F4 || MI.getOpcode() == PPC::SELECT_F8 || 9540 MI.getOpcode() == PPC::SELECT_QFRC || 9541 MI.getOpcode() == PPC::SELECT_QSRC || 9542 MI.getOpcode() == PPC::SELECT_QBRC || 9543 MI.getOpcode() == PPC::SELECT_VRRC || 9544 MI.getOpcode() == PPC::SELECT_VSFRC || 9545 MI.getOpcode() == PPC::SELECT_VSSRC || 9546 MI.getOpcode() == PPC::SELECT_VSRC) { 9547 BuildMI(BB, dl, TII->get(PPC::BC)) 9548 .addReg(MI.getOperand(1).getReg()) 9549 .addMBB(sinkMBB); 9550 } else { 9551 unsigned SelectPred = MI.getOperand(4).getImm(); 9552 BuildMI(BB, dl, TII->get(PPC::BCC)) 9553 .addImm(SelectPred) 9554 .addReg(MI.getOperand(1).getReg()) 9555 .addMBB(sinkMBB); 9556 } 9557 9558 // copy0MBB: 9559 // %FalseValue = ... 9560 // # fallthrough to sinkMBB 9561 BB = copy0MBB; 9562 9563 // Update machine-CFG edges 9564 BB->addSuccessor(sinkMBB); 9565 9566 // sinkMBB: 9567 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 9568 // ... 9569 BB = sinkMBB; 9570 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::PHI), MI.getOperand(0).getReg()) 9571 .addReg(MI.getOperand(3).getReg()) 9572 .addMBB(copy0MBB) 9573 .addReg(MI.getOperand(2).getReg()) 9574 .addMBB(thisMBB); 9575 } else if (MI.getOpcode() == PPC::ReadTB) { 9576 // To read the 64-bit time-base register on a 32-bit target, we read the 9577 // two halves. Should the counter have wrapped while it was being read, we 9578 // need to try again. 9579 // ... 9580 // readLoop: 9581 // mfspr Rx,TBU # load from TBU 9582 // mfspr Ry,TB # load from TB 9583 // mfspr Rz,TBU # load from TBU 9584 // cmpw crX,Rx,Rz # check if 'old'='new' 9585 // bne readLoop # branch if they're not equal 9586 // ... 9587 9588 MachineBasicBlock *readMBB = F->CreateMachineBasicBlock(LLVM_BB); 9589 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 9590 DebugLoc dl = MI.getDebugLoc(); 9591 F->insert(It, readMBB); 9592 F->insert(It, sinkMBB); 9593 9594 // Transfer the remainder of BB and its successor edges to sinkMBB. 9595 sinkMBB->splice(sinkMBB->begin(), BB, 9596 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 9597 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 9598 9599 BB->addSuccessor(readMBB); 9600 BB = readMBB; 9601 9602 MachineRegisterInfo &RegInfo = F->getRegInfo(); 9603 unsigned ReadAgainReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass); 9604 unsigned LoReg = MI.getOperand(0).getReg(); 9605 unsigned HiReg = MI.getOperand(1).getReg(); 9606 9607 BuildMI(BB, dl, TII->get(PPC::MFSPR), HiReg).addImm(269); 9608 BuildMI(BB, dl, TII->get(PPC::MFSPR), LoReg).addImm(268); 9609 BuildMI(BB, dl, TII->get(PPC::MFSPR), ReadAgainReg).addImm(269); 9610 9611 unsigned CmpReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass); 9612 9613 BuildMI(BB, dl, TII->get(PPC::CMPW), CmpReg) 9614 .addReg(HiReg).addReg(ReadAgainReg); 9615 BuildMI(BB, dl, TII->get(PPC::BCC)) 9616 .addImm(PPC::PRED_NE).addReg(CmpReg).addMBB(readMBB); 9617 9618 BB->addSuccessor(readMBB); 9619 BB->addSuccessor(sinkMBB); 9620 } else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I8) 9621 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4); 9622 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I16) 9623 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4); 9624 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I32) 9625 BB = EmitAtomicBinary(MI, BB, 4, PPC::ADD4); 9626 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I64) 9627 BB = EmitAtomicBinary(MI, BB, 8, PPC::ADD8); 9628 9629 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I8) 9630 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND); 9631 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I16) 9632 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND); 9633 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I32) 9634 BB = EmitAtomicBinary(MI, BB, 4, PPC::AND); 9635 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I64) 9636 BB = EmitAtomicBinary(MI, BB, 8, PPC::AND8); 9637 9638 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I8) 9639 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR); 9640 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I16) 9641 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR); 9642 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I32) 9643 BB = EmitAtomicBinary(MI, BB, 4, PPC::OR); 9644 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I64) 9645 BB = EmitAtomicBinary(MI, BB, 8, PPC::OR8); 9646 9647 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I8) 9648 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR); 9649 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I16) 9650 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR); 9651 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I32) 9652 BB = EmitAtomicBinary(MI, BB, 4, PPC::XOR); 9653 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I64) 9654 BB = EmitAtomicBinary(MI, BB, 8, PPC::XOR8); 9655 9656 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I8) 9657 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::NAND); 9658 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I16) 9659 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::NAND); 9660 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I32) 9661 BB = EmitAtomicBinary(MI, BB, 4, PPC::NAND); 9662 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I64) 9663 BB = EmitAtomicBinary(MI, BB, 8, PPC::NAND8); 9664 9665 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I8) 9666 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF); 9667 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I16) 9668 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF); 9669 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I32) 9670 BB = EmitAtomicBinary(MI, BB, 4, PPC::SUBF); 9671 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I64) 9672 BB = EmitAtomicBinary(MI, BB, 8, PPC::SUBF8); 9673 9674 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I8) 9675 BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPW, PPC::PRED_GE); 9676 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I16) 9677 BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPW, PPC::PRED_GE); 9678 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I32) 9679 BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPW, PPC::PRED_GE); 9680 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I64) 9681 BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPD, PPC::PRED_GE); 9682 9683 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I8) 9684 BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPW, PPC::PRED_LE); 9685 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I16) 9686 BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPW, PPC::PRED_LE); 9687 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I32) 9688 BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPW, PPC::PRED_LE); 9689 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I64) 9690 BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPD, PPC::PRED_LE); 9691 9692 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I8) 9693 BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPLW, PPC::PRED_GE); 9694 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I16) 9695 BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPLW, PPC::PRED_GE); 9696 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I32) 9697 BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPLW, PPC::PRED_GE); 9698 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I64) 9699 BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPLD, PPC::PRED_GE); 9700 9701 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I8) 9702 BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPLW, PPC::PRED_LE); 9703 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I16) 9704 BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPLW, PPC::PRED_LE); 9705 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I32) 9706 BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPLW, PPC::PRED_LE); 9707 else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I64) 9708 BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPLD, PPC::PRED_LE); 9709 9710 else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I8) 9711 BB = EmitPartwordAtomicBinary(MI, BB, true, 0); 9712 else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I16) 9713 BB = EmitPartwordAtomicBinary(MI, BB, false, 0); 9714 else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I32) 9715 BB = EmitAtomicBinary(MI, BB, 4, 0); 9716 else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I64) 9717 BB = EmitAtomicBinary(MI, BB, 8, 0); 9718 else if (MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 || 9719 MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I64 || 9720 (Subtarget.hasPartwordAtomics() && 9721 MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I8) || 9722 (Subtarget.hasPartwordAtomics() && 9723 MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I16)) { 9724 bool is64bit = MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I64; 9725 9726 auto LoadMnemonic = PPC::LDARX; 9727 auto StoreMnemonic = PPC::STDCX; 9728 switch (MI.getOpcode()) { 9729 default: 9730 llvm_unreachable("Compare and swap of unknown size"); 9731 case PPC::ATOMIC_CMP_SWAP_I8: 9732 LoadMnemonic = PPC::LBARX; 9733 StoreMnemonic = PPC::STBCX; 9734 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics."); 9735 break; 9736 case PPC::ATOMIC_CMP_SWAP_I16: 9737 LoadMnemonic = PPC::LHARX; 9738 StoreMnemonic = PPC::STHCX; 9739 assert(Subtarget.hasPartwordAtomics() && "No support partword atomics."); 9740 break; 9741 case PPC::ATOMIC_CMP_SWAP_I32: 9742 LoadMnemonic = PPC::LWARX; 9743 StoreMnemonic = PPC::STWCX; 9744 break; 9745 case PPC::ATOMIC_CMP_SWAP_I64: 9746 LoadMnemonic = PPC::LDARX; 9747 StoreMnemonic = PPC::STDCX; 9748 break; 9749 } 9750 unsigned dest = MI.getOperand(0).getReg(); 9751 unsigned ptrA = MI.getOperand(1).getReg(); 9752 unsigned ptrB = MI.getOperand(2).getReg(); 9753 unsigned oldval = MI.getOperand(3).getReg(); 9754 unsigned newval = MI.getOperand(4).getReg(); 9755 DebugLoc dl = MI.getDebugLoc(); 9756 9757 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB); 9758 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB); 9759 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB); 9760 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); 9761 F->insert(It, loop1MBB); 9762 F->insert(It, loop2MBB); 9763 F->insert(It, midMBB); 9764 F->insert(It, exitMBB); 9765 exitMBB->splice(exitMBB->begin(), BB, 9766 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 9767 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 9768 9769 // thisMBB: 9770 // ... 9771 // fallthrough --> loopMBB 9772 BB->addSuccessor(loop1MBB); 9773 9774 // loop1MBB: 9775 // l[bhwd]arx dest, ptr 9776 // cmp[wd] dest, oldval 9777 // bne- midMBB 9778 // loop2MBB: 9779 // st[bhwd]cx. newval, ptr 9780 // bne- loopMBB 9781 // b exitBB 9782 // midMBB: 9783 // st[bhwd]cx. dest, ptr 9784 // exitBB: 9785 BB = loop1MBB; 9786 BuildMI(BB, dl, TII->get(LoadMnemonic), dest) 9787 .addReg(ptrA).addReg(ptrB); 9788 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0) 9789 .addReg(oldval).addReg(dest); 9790 BuildMI(BB, dl, TII->get(PPC::BCC)) 9791 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB); 9792 BB->addSuccessor(loop2MBB); 9793 BB->addSuccessor(midMBB); 9794 9795 BB = loop2MBB; 9796 BuildMI(BB, dl, TII->get(StoreMnemonic)) 9797 .addReg(newval).addReg(ptrA).addReg(ptrB); 9798 BuildMI(BB, dl, TII->get(PPC::BCC)) 9799 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB); 9800 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB); 9801 BB->addSuccessor(loop1MBB); 9802 BB->addSuccessor(exitMBB); 9803 9804 BB = midMBB; 9805 BuildMI(BB, dl, TII->get(StoreMnemonic)) 9806 .addReg(dest).addReg(ptrA).addReg(ptrB); 9807 BB->addSuccessor(exitMBB); 9808 9809 // exitMBB: 9810 // ... 9811 BB = exitMBB; 9812 } else if (MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 || 9813 MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) { 9814 // We must use 64-bit registers for addresses when targeting 64-bit, 9815 // since we're actually doing arithmetic on them. Other registers 9816 // can be 32-bit. 9817 bool is64bit = Subtarget.isPPC64(); 9818 bool isLittleEndian = Subtarget.isLittleEndian(); 9819 bool is8bit = MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I8; 9820 9821 unsigned dest = MI.getOperand(0).getReg(); 9822 unsigned ptrA = MI.getOperand(1).getReg(); 9823 unsigned ptrB = MI.getOperand(2).getReg(); 9824 unsigned oldval = MI.getOperand(3).getReg(); 9825 unsigned newval = MI.getOperand(4).getReg(); 9826 DebugLoc dl = MI.getDebugLoc(); 9827 9828 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB); 9829 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB); 9830 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB); 9831 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); 9832 F->insert(It, loop1MBB); 9833 F->insert(It, loop2MBB); 9834 F->insert(It, midMBB); 9835 F->insert(It, exitMBB); 9836 exitMBB->splice(exitMBB->begin(), BB, 9837 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 9838 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 9839 9840 MachineRegisterInfo &RegInfo = F->getRegInfo(); 9841 const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass 9842 : &PPC::GPRCRegClass; 9843 unsigned PtrReg = RegInfo.createVirtualRegister(RC); 9844 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC); 9845 unsigned ShiftReg = 9846 isLittleEndian ? Shift1Reg : RegInfo.createVirtualRegister(RC); 9847 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC); 9848 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC); 9849 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC); 9850 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC); 9851 unsigned MaskReg = RegInfo.createVirtualRegister(RC); 9852 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC); 9853 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC); 9854 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC); 9855 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC); 9856 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC); 9857 unsigned Ptr1Reg; 9858 unsigned TmpReg = RegInfo.createVirtualRegister(RC); 9859 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO; 9860 // thisMBB: 9861 // ... 9862 // fallthrough --> loopMBB 9863 BB->addSuccessor(loop1MBB); 9864 9865 // The 4-byte load must be aligned, while a char or short may be 9866 // anywhere in the word. Hence all this nasty bookkeeping code. 9867 // add ptr1, ptrA, ptrB [copy if ptrA==0] 9868 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27] 9869 // xori shift, shift1, 24 [16] 9870 // rlwinm ptr, ptr1, 0, 0, 29 9871 // slw newval2, newval, shift 9872 // slw oldval2, oldval,shift 9873 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535] 9874 // slw mask, mask2, shift 9875 // and newval3, newval2, mask 9876 // and oldval3, oldval2, mask 9877 // loop1MBB: 9878 // lwarx tmpDest, ptr 9879 // and tmp, tmpDest, mask 9880 // cmpw tmp, oldval3 9881 // bne- midMBB 9882 // loop2MBB: 9883 // andc tmp2, tmpDest, mask 9884 // or tmp4, tmp2, newval3 9885 // stwcx. tmp4, ptr 9886 // bne- loop1MBB 9887 // b exitBB 9888 // midMBB: 9889 // stwcx. tmpDest, ptr 9890 // exitBB: 9891 // srw dest, tmpDest, shift 9892 if (ptrA != ZeroReg) { 9893 Ptr1Reg = RegInfo.createVirtualRegister(RC); 9894 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg) 9895 .addReg(ptrA).addReg(ptrB); 9896 } else { 9897 Ptr1Reg = ptrB; 9898 } 9899 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg) 9900 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27); 9901 if (!isLittleEndian) 9902 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg) 9903 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16); 9904 if (is64bit) 9905 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg) 9906 .addReg(Ptr1Reg).addImm(0).addImm(61); 9907 else 9908 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg) 9909 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29); 9910 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg) 9911 .addReg(newval).addReg(ShiftReg); 9912 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg) 9913 .addReg(oldval).addReg(ShiftReg); 9914 if (is8bit) 9915 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255); 9916 else { 9917 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0); 9918 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg) 9919 .addReg(Mask3Reg).addImm(65535); 9920 } 9921 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg) 9922 .addReg(Mask2Reg).addReg(ShiftReg); 9923 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg) 9924 .addReg(NewVal2Reg).addReg(MaskReg); 9925 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg) 9926 .addReg(OldVal2Reg).addReg(MaskReg); 9927 9928 BB = loop1MBB; 9929 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg) 9930 .addReg(ZeroReg).addReg(PtrReg); 9931 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg) 9932 .addReg(TmpDestReg).addReg(MaskReg); 9933 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0) 9934 .addReg(TmpReg).addReg(OldVal3Reg); 9935 BuildMI(BB, dl, TII->get(PPC::BCC)) 9936 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB); 9937 BB->addSuccessor(loop2MBB); 9938 BB->addSuccessor(midMBB); 9939 9940 BB = loop2MBB; 9941 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg) 9942 .addReg(TmpDestReg).addReg(MaskReg); 9943 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg) 9944 .addReg(Tmp2Reg).addReg(NewVal3Reg); 9945 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg) 9946 .addReg(ZeroReg).addReg(PtrReg); 9947 BuildMI(BB, dl, TII->get(PPC::BCC)) 9948 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB); 9949 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB); 9950 BB->addSuccessor(loop1MBB); 9951 BB->addSuccessor(exitMBB); 9952 9953 BB = midMBB; 9954 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg) 9955 .addReg(ZeroReg).addReg(PtrReg); 9956 BB->addSuccessor(exitMBB); 9957 9958 // exitMBB: 9959 // ... 9960 BB = exitMBB; 9961 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg) 9962 .addReg(ShiftReg); 9963 } else if (MI.getOpcode() == PPC::FADDrtz) { 9964 // This pseudo performs an FADD with rounding mode temporarily forced 9965 // to round-to-zero. We emit this via custom inserter since the FPSCR 9966 // is not modeled at the SelectionDAG level. 9967 unsigned Dest = MI.getOperand(0).getReg(); 9968 unsigned Src1 = MI.getOperand(1).getReg(); 9969 unsigned Src2 = MI.getOperand(2).getReg(); 9970 DebugLoc dl = MI.getDebugLoc(); 9971 9972 MachineRegisterInfo &RegInfo = F->getRegInfo(); 9973 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass); 9974 9975 // Save FPSCR value. 9976 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg); 9977 9978 // Set rounding mode to round-to-zero. 9979 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31); 9980 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30); 9981 9982 // Perform addition. 9983 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2); 9984 9985 // Restore FPSCR value. 9986 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSFb)).addImm(1).addReg(MFFSReg); 9987 } else if (MI.getOpcode() == PPC::ANDIo_1_EQ_BIT || 9988 MI.getOpcode() == PPC::ANDIo_1_GT_BIT || 9989 MI.getOpcode() == PPC::ANDIo_1_EQ_BIT8 || 9990 MI.getOpcode() == PPC::ANDIo_1_GT_BIT8) { 9991 unsigned Opcode = (MI.getOpcode() == PPC::ANDIo_1_EQ_BIT8 || 9992 MI.getOpcode() == PPC::ANDIo_1_GT_BIT8) 9993 ? PPC::ANDIo8 9994 : PPC::ANDIo; 9995 bool isEQ = (MI.getOpcode() == PPC::ANDIo_1_EQ_BIT || 9996 MI.getOpcode() == PPC::ANDIo_1_EQ_BIT8); 9997 9998 MachineRegisterInfo &RegInfo = F->getRegInfo(); 9999 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ? 10000 &PPC::GPRCRegClass : 10001 &PPC::G8RCRegClass); 10002 10003 DebugLoc dl = MI.getDebugLoc(); 10004 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest) 10005 .addReg(MI.getOperand(1).getReg()) 10006 .addImm(1); 10007 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), 10008 MI.getOperand(0).getReg()) 10009 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT); 10010 } else if (MI.getOpcode() == PPC::TCHECK_RET) { 10011 DebugLoc Dl = MI.getDebugLoc(); 10012 MachineRegisterInfo &RegInfo = F->getRegInfo(); 10013 unsigned CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass); 10014 BuildMI(*BB, MI, Dl, TII->get(PPC::TCHECK), CRReg); 10015 return BB; 10016 } else { 10017 llvm_unreachable("Unexpected instr type to insert"); 10018 } 10019 10020 MI.eraseFromParent(); // The pseudo instruction is gone now. 10021 return BB; 10022 } 10023 10024 //===----------------------------------------------------------------------===// 10025 // Target Optimization Hooks 10026 //===----------------------------------------------------------------------===// 10027 10028 static int getEstimateRefinementSteps(EVT VT, const PPCSubtarget &Subtarget) { 10029 // For the estimates, convergence is quadratic, so we essentially double the 10030 // number of digits correct after every iteration. For both FRE and FRSQRTE, 10031 // the minimum architected relative accuracy is 2^-5. When hasRecipPrec(), 10032 // this is 2^-14. IEEE float has 23 digits and double has 52 digits. 10033 int RefinementSteps = Subtarget.hasRecipPrec() ? 1 : 3; 10034 if (VT.getScalarType() == MVT::f64) 10035 RefinementSteps++; 10036 return RefinementSteps; 10037 } 10038 10039 SDValue PPCTargetLowering::getSqrtEstimate(SDValue Operand, SelectionDAG &DAG, 10040 int Enabled, int &RefinementSteps, 10041 bool &UseOneConstNR, 10042 bool Reciprocal) const { 10043 EVT VT = Operand.getValueType(); 10044 if ((VT == MVT::f32 && Subtarget.hasFRSQRTES()) || 10045 (VT == MVT::f64 && Subtarget.hasFRSQRTE()) || 10046 (VT == MVT::v4f32 && Subtarget.hasAltivec()) || 10047 (VT == MVT::v2f64 && Subtarget.hasVSX()) || 10048 (VT == MVT::v4f32 && Subtarget.hasQPX()) || 10049 (VT == MVT::v4f64 && Subtarget.hasQPX())) { 10050 if (RefinementSteps == ReciprocalEstimate::Unspecified) 10051 RefinementSteps = getEstimateRefinementSteps(VT, Subtarget); 10052 10053 UseOneConstNR = true; 10054 return DAG.getNode(PPCISD::FRSQRTE, SDLoc(Operand), VT, Operand); 10055 } 10056 return SDValue(); 10057 } 10058 10059 SDValue PPCTargetLowering::getRecipEstimate(SDValue Operand, SelectionDAG &DAG, 10060 int Enabled, 10061 int &RefinementSteps) const { 10062 EVT VT = Operand.getValueType(); 10063 if ((VT == MVT::f32 && Subtarget.hasFRES()) || 10064 (VT == MVT::f64 && Subtarget.hasFRE()) || 10065 (VT == MVT::v4f32 && Subtarget.hasAltivec()) || 10066 (VT == MVT::v2f64 && Subtarget.hasVSX()) || 10067 (VT == MVT::v4f32 && Subtarget.hasQPX()) || 10068 (VT == MVT::v4f64 && Subtarget.hasQPX())) { 10069 if (RefinementSteps == ReciprocalEstimate::Unspecified) 10070 RefinementSteps = getEstimateRefinementSteps(VT, Subtarget); 10071 return DAG.getNode(PPCISD::FRE, SDLoc(Operand), VT, Operand); 10072 } 10073 return SDValue(); 10074 } 10075 10076 unsigned PPCTargetLowering::combineRepeatedFPDivisors() const { 10077 // Note: This functionality is used only when unsafe-fp-math is enabled, and 10078 // on cores with reciprocal estimates (which are used when unsafe-fp-math is 10079 // enabled for division), this functionality is redundant with the default 10080 // combiner logic (once the division -> reciprocal/multiply transformation 10081 // has taken place). As a result, this matters more for older cores than for 10082 // newer ones. 10083 10084 // Combine multiple FDIVs with the same divisor into multiple FMULs by the 10085 // reciprocal if there are two or more FDIVs (for embedded cores with only 10086 // one FP pipeline) for three or more FDIVs (for generic OOO cores). 10087 switch (Subtarget.getDarwinDirective()) { 10088 default: 10089 return 3; 10090 case PPC::DIR_440: 10091 case PPC::DIR_A2: 10092 case PPC::DIR_E500mc: 10093 case PPC::DIR_E5500: 10094 return 2; 10095 } 10096 } 10097 10098 // isConsecutiveLSLoc needs to work even if all adds have not yet been 10099 // collapsed, and so we need to look through chains of them. 10100 static void getBaseWithConstantOffset(SDValue Loc, SDValue &Base, 10101 int64_t& Offset, SelectionDAG &DAG) { 10102 if (DAG.isBaseWithConstantOffset(Loc)) { 10103 Base = Loc.getOperand(0); 10104 Offset += cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue(); 10105 10106 // The base might itself be a base plus an offset, and if so, accumulate 10107 // that as well. 10108 getBaseWithConstantOffset(Loc.getOperand(0), Base, Offset, DAG); 10109 } 10110 } 10111 10112 static bool isConsecutiveLSLoc(SDValue Loc, EVT VT, LSBaseSDNode *Base, 10113 unsigned Bytes, int Dist, 10114 SelectionDAG &DAG) { 10115 if (VT.getSizeInBits() / 8 != Bytes) 10116 return false; 10117 10118 SDValue BaseLoc = Base->getBasePtr(); 10119 if (Loc.getOpcode() == ISD::FrameIndex) { 10120 if (BaseLoc.getOpcode() != ISD::FrameIndex) 10121 return false; 10122 const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 10123 int FI = cast<FrameIndexSDNode>(Loc)->getIndex(); 10124 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex(); 10125 int FS = MFI.getObjectSize(FI); 10126 int BFS = MFI.getObjectSize(BFI); 10127 if (FS != BFS || FS != (int)Bytes) return false; 10128 return MFI.getObjectOffset(FI) == (MFI.getObjectOffset(BFI) + Dist*Bytes); 10129 } 10130 10131 SDValue Base1 = Loc, Base2 = BaseLoc; 10132 int64_t Offset1 = 0, Offset2 = 0; 10133 getBaseWithConstantOffset(Loc, Base1, Offset1, DAG); 10134 getBaseWithConstantOffset(BaseLoc, Base2, Offset2, DAG); 10135 if (Base1 == Base2 && Offset1 == (Offset2 + Dist * Bytes)) 10136 return true; 10137 10138 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 10139 const GlobalValue *GV1 = nullptr; 10140 const GlobalValue *GV2 = nullptr; 10141 Offset1 = 0; 10142 Offset2 = 0; 10143 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1); 10144 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2); 10145 if (isGA1 && isGA2 && GV1 == GV2) 10146 return Offset1 == (Offset2 + Dist*Bytes); 10147 return false; 10148 } 10149 10150 // Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does 10151 // not enforce equality of the chain operands. 10152 static bool isConsecutiveLS(SDNode *N, LSBaseSDNode *Base, 10153 unsigned Bytes, int Dist, 10154 SelectionDAG &DAG) { 10155 if (LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(N)) { 10156 EVT VT = LS->getMemoryVT(); 10157 SDValue Loc = LS->getBasePtr(); 10158 return isConsecutiveLSLoc(Loc, VT, Base, Bytes, Dist, DAG); 10159 } 10160 10161 if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN) { 10162 EVT VT; 10163 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 10164 default: return false; 10165 case Intrinsic::ppc_qpx_qvlfd: 10166 case Intrinsic::ppc_qpx_qvlfda: 10167 VT = MVT::v4f64; 10168 break; 10169 case Intrinsic::ppc_qpx_qvlfs: 10170 case Intrinsic::ppc_qpx_qvlfsa: 10171 VT = MVT::v4f32; 10172 break; 10173 case Intrinsic::ppc_qpx_qvlfcd: 10174 case Intrinsic::ppc_qpx_qvlfcda: 10175 VT = MVT::v2f64; 10176 break; 10177 case Intrinsic::ppc_qpx_qvlfcs: 10178 case Intrinsic::ppc_qpx_qvlfcsa: 10179 VT = MVT::v2f32; 10180 break; 10181 case Intrinsic::ppc_qpx_qvlfiwa: 10182 case Intrinsic::ppc_qpx_qvlfiwz: 10183 case Intrinsic::ppc_altivec_lvx: 10184 case Intrinsic::ppc_altivec_lvxl: 10185 case Intrinsic::ppc_vsx_lxvw4x: 10186 case Intrinsic::ppc_vsx_lxvw4x_be: 10187 VT = MVT::v4i32; 10188 break; 10189 case Intrinsic::ppc_vsx_lxvd2x: 10190 case Intrinsic::ppc_vsx_lxvd2x_be: 10191 VT = MVT::v2f64; 10192 break; 10193 case Intrinsic::ppc_altivec_lvebx: 10194 VT = MVT::i8; 10195 break; 10196 case Intrinsic::ppc_altivec_lvehx: 10197 VT = MVT::i16; 10198 break; 10199 case Intrinsic::ppc_altivec_lvewx: 10200 VT = MVT::i32; 10201 break; 10202 } 10203 10204 return isConsecutiveLSLoc(N->getOperand(2), VT, Base, Bytes, Dist, DAG); 10205 } 10206 10207 if (N->getOpcode() == ISD::INTRINSIC_VOID) { 10208 EVT VT; 10209 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 10210 default: return false; 10211 case Intrinsic::ppc_qpx_qvstfd: 10212 case Intrinsic::ppc_qpx_qvstfda: 10213 VT = MVT::v4f64; 10214 break; 10215 case Intrinsic::ppc_qpx_qvstfs: 10216 case Intrinsic::ppc_qpx_qvstfsa: 10217 VT = MVT::v4f32; 10218 break; 10219 case Intrinsic::ppc_qpx_qvstfcd: 10220 case Intrinsic::ppc_qpx_qvstfcda: 10221 VT = MVT::v2f64; 10222 break; 10223 case Intrinsic::ppc_qpx_qvstfcs: 10224 case Intrinsic::ppc_qpx_qvstfcsa: 10225 VT = MVT::v2f32; 10226 break; 10227 case Intrinsic::ppc_qpx_qvstfiw: 10228 case Intrinsic::ppc_qpx_qvstfiwa: 10229 case Intrinsic::ppc_altivec_stvx: 10230 case Intrinsic::ppc_altivec_stvxl: 10231 case Intrinsic::ppc_vsx_stxvw4x: 10232 VT = MVT::v4i32; 10233 break; 10234 case Intrinsic::ppc_vsx_stxvd2x: 10235 VT = MVT::v2f64; 10236 break; 10237 case Intrinsic::ppc_vsx_stxvw4x_be: 10238 VT = MVT::v4i32; 10239 break; 10240 case Intrinsic::ppc_vsx_stxvd2x_be: 10241 VT = MVT::v2f64; 10242 break; 10243 case Intrinsic::ppc_altivec_stvebx: 10244 VT = MVT::i8; 10245 break; 10246 case Intrinsic::ppc_altivec_stvehx: 10247 VT = MVT::i16; 10248 break; 10249 case Intrinsic::ppc_altivec_stvewx: 10250 VT = MVT::i32; 10251 break; 10252 } 10253 10254 return isConsecutiveLSLoc(N->getOperand(3), VT, Base, Bytes, Dist, DAG); 10255 } 10256 10257 return false; 10258 } 10259 10260 // Return true is there is a nearyby consecutive load to the one provided 10261 // (regardless of alignment). We search up and down the chain, looking though 10262 // token factors and other loads (but nothing else). As a result, a true result 10263 // indicates that it is safe to create a new consecutive load adjacent to the 10264 // load provided. 10265 static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) { 10266 SDValue Chain = LD->getChain(); 10267 EVT VT = LD->getMemoryVT(); 10268 10269 SmallSet<SDNode *, 16> LoadRoots; 10270 SmallVector<SDNode *, 8> Queue(1, Chain.getNode()); 10271 SmallSet<SDNode *, 16> Visited; 10272 10273 // First, search up the chain, branching to follow all token-factor operands. 10274 // If we find a consecutive load, then we're done, otherwise, record all 10275 // nodes just above the top-level loads and token factors. 10276 while (!Queue.empty()) { 10277 SDNode *ChainNext = Queue.pop_back_val(); 10278 if (!Visited.insert(ChainNext).second) 10279 continue; 10280 10281 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(ChainNext)) { 10282 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG)) 10283 return true; 10284 10285 if (!Visited.count(ChainLD->getChain().getNode())) 10286 Queue.push_back(ChainLD->getChain().getNode()); 10287 } else if (ChainNext->getOpcode() == ISD::TokenFactor) { 10288 for (const SDUse &O : ChainNext->ops()) 10289 if (!Visited.count(O.getNode())) 10290 Queue.push_back(O.getNode()); 10291 } else 10292 LoadRoots.insert(ChainNext); 10293 } 10294 10295 // Second, search down the chain, starting from the top-level nodes recorded 10296 // in the first phase. These top-level nodes are the nodes just above all 10297 // loads and token factors. Starting with their uses, recursively look though 10298 // all loads (just the chain uses) and token factors to find a consecutive 10299 // load. 10300 Visited.clear(); 10301 Queue.clear(); 10302 10303 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(), 10304 IE = LoadRoots.end(); I != IE; ++I) { 10305 Queue.push_back(*I); 10306 10307 while (!Queue.empty()) { 10308 SDNode *LoadRoot = Queue.pop_back_val(); 10309 if (!Visited.insert(LoadRoot).second) 10310 continue; 10311 10312 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(LoadRoot)) 10313 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG)) 10314 return true; 10315 10316 for (SDNode::use_iterator UI = LoadRoot->use_begin(), 10317 UE = LoadRoot->use_end(); UI != UE; ++UI) 10318 if (((isa<MemSDNode>(*UI) && 10319 cast<MemSDNode>(*UI)->getChain().getNode() == LoadRoot) || 10320 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI)) 10321 Queue.push_back(*UI); 10322 } 10323 } 10324 10325 return false; 10326 } 10327 10328 /// This function is called when we have proved that a SETCC node can be replaced 10329 /// by subtraction (and other supporting instructions) so that the result of 10330 /// comparison is kept in a GPR instead of CR. This function is purely for 10331 /// codegen purposes and has some flags to guide the codegen process. 10332 static SDValue generateEquivalentSub(SDNode *N, int Size, bool Complement, 10333 bool Swap, SDLoc &DL, SelectionDAG &DAG) { 10334 assert(N->getOpcode() == ISD::SETCC && "ISD::SETCC Expected."); 10335 10336 // Zero extend the operands to the largest legal integer. Originally, they 10337 // must be of a strictly smaller size. 10338 auto Op0 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, N->getOperand(0), 10339 DAG.getConstant(Size, DL, MVT::i32)); 10340 auto Op1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, N->getOperand(1), 10341 DAG.getConstant(Size, DL, MVT::i32)); 10342 10343 // Swap if needed. Depends on the condition code. 10344 if (Swap) 10345 std::swap(Op0, Op1); 10346 10347 // Subtract extended integers. 10348 auto SubNode = DAG.getNode(ISD::SUB, DL, MVT::i64, Op0, Op1); 10349 10350 // Move the sign bit to the least significant position and zero out the rest. 10351 // Now the least significant bit carries the result of original comparison. 10352 auto Shifted = DAG.getNode(ISD::SRL, DL, MVT::i64, SubNode, 10353 DAG.getConstant(Size - 1, DL, MVT::i32)); 10354 auto Final = Shifted; 10355 10356 // Complement the result if needed. Based on the condition code. 10357 if (Complement) 10358 Final = DAG.getNode(ISD::XOR, DL, MVT::i64, Shifted, 10359 DAG.getConstant(1, DL, MVT::i64)); 10360 10361 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Final); 10362 } 10363 10364 SDValue PPCTargetLowering::ConvertSETCCToSubtract(SDNode *N, 10365 DAGCombinerInfo &DCI) const { 10366 assert(N->getOpcode() == ISD::SETCC && "ISD::SETCC Expected."); 10367 10368 SelectionDAG &DAG = DCI.DAG; 10369 SDLoc DL(N); 10370 10371 // Size of integers being compared has a critical role in the following 10372 // analysis, so we prefer to do this when all types are legal. 10373 if (!DCI.isAfterLegalizeVectorOps()) 10374 return SDValue(); 10375 10376 // If all users of SETCC extend its value to a legal integer type 10377 // then we replace SETCC with a subtraction 10378 for (SDNode::use_iterator UI = N->use_begin(), 10379 UE = N->use_end(); UI != UE; ++UI) { 10380 if (UI->getOpcode() != ISD::ZERO_EXTEND) 10381 return SDValue(); 10382 } 10383 10384 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); 10385 auto OpSize = N->getOperand(0).getValueSizeInBits(); 10386 10387 unsigned Size = DAG.getDataLayout().getLargestLegalIntTypeSizeInBits(); 10388 10389 if (OpSize < Size) { 10390 switch (CC) { 10391 default: break; 10392 case ISD::SETULT: 10393 return generateEquivalentSub(N, Size, false, false, DL, DAG); 10394 case ISD::SETULE: 10395 return generateEquivalentSub(N, Size, true, true, DL, DAG); 10396 case ISD::SETUGT: 10397 return generateEquivalentSub(N, Size, false, true, DL, DAG); 10398 case ISD::SETUGE: 10399 return generateEquivalentSub(N, Size, true, false, DL, DAG); 10400 } 10401 } 10402 10403 return SDValue(); 10404 } 10405 10406 SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N, 10407 DAGCombinerInfo &DCI) const { 10408 SelectionDAG &DAG = DCI.DAG; 10409 SDLoc dl(N); 10410 10411 assert(Subtarget.useCRBits() && "Expecting to be tracking CR bits"); 10412 // If we're tracking CR bits, we need to be careful that we don't have: 10413 // trunc(binary-ops(zext(x), zext(y))) 10414 // or 10415 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...) 10416 // such that we're unnecessarily moving things into GPRs when it would be 10417 // better to keep them in CR bits. 10418 10419 // Note that trunc here can be an actual i1 trunc, or can be the effective 10420 // truncation that comes from a setcc or select_cc. 10421 if (N->getOpcode() == ISD::TRUNCATE && 10422 N->getValueType(0) != MVT::i1) 10423 return SDValue(); 10424 10425 if (N->getOperand(0).getValueType() != MVT::i32 && 10426 N->getOperand(0).getValueType() != MVT::i64) 10427 return SDValue(); 10428 10429 if (N->getOpcode() == ISD::SETCC || 10430 N->getOpcode() == ISD::SELECT_CC) { 10431 // If we're looking at a comparison, then we need to make sure that the 10432 // high bits (all except for the first) don't matter the result. 10433 ISD::CondCode CC = 10434 cast<CondCodeSDNode>(N->getOperand( 10435 N->getOpcode() == ISD::SETCC ? 2 : 4))->get(); 10436 unsigned OpBits = N->getOperand(0).getValueSizeInBits(); 10437 10438 if (ISD::isSignedIntSetCC(CC)) { 10439 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits || 10440 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits) 10441 return SDValue(); 10442 } else if (ISD::isUnsignedIntSetCC(CC)) { 10443 if (!DAG.MaskedValueIsZero(N->getOperand(0), 10444 APInt::getHighBitsSet(OpBits, OpBits-1)) || 10445 !DAG.MaskedValueIsZero(N->getOperand(1), 10446 APInt::getHighBitsSet(OpBits, OpBits-1))) 10447 return (N->getOpcode() == ISD::SETCC ? ConvertSETCCToSubtract(N, DCI) 10448 : SDValue()); 10449 } else { 10450 // This is neither a signed nor an unsigned comparison, just make sure 10451 // that the high bits are equal. 10452 KnownBits Op1Known, Op2Known; 10453 DAG.computeKnownBits(N->getOperand(0), Op1Known); 10454 DAG.computeKnownBits(N->getOperand(1), Op2Known); 10455 10456 // We don't really care about what is known about the first bit (if 10457 // anything), so clear it in all masks prior to comparing them. 10458 Op1Known.Zero.clearBit(0); Op1Known.One.clearBit(0); 10459 Op2Known.Zero.clearBit(0); Op2Known.One.clearBit(0); 10460 10461 if (Op1Known.Zero != Op2Known.Zero || Op1Known.One != Op2Known.One) 10462 return SDValue(); 10463 } 10464 } 10465 10466 // We now know that the higher-order bits are irrelevant, we just need to 10467 // make sure that all of the intermediate operations are bit operations, and 10468 // all inputs are extensions. 10469 if (N->getOperand(0).getOpcode() != ISD::AND && 10470 N->getOperand(0).getOpcode() != ISD::OR && 10471 N->getOperand(0).getOpcode() != ISD::XOR && 10472 N->getOperand(0).getOpcode() != ISD::SELECT && 10473 N->getOperand(0).getOpcode() != ISD::SELECT_CC && 10474 N->getOperand(0).getOpcode() != ISD::TRUNCATE && 10475 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND && 10476 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND && 10477 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND) 10478 return SDValue(); 10479 10480 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) && 10481 N->getOperand(1).getOpcode() != ISD::AND && 10482 N->getOperand(1).getOpcode() != ISD::OR && 10483 N->getOperand(1).getOpcode() != ISD::XOR && 10484 N->getOperand(1).getOpcode() != ISD::SELECT && 10485 N->getOperand(1).getOpcode() != ISD::SELECT_CC && 10486 N->getOperand(1).getOpcode() != ISD::TRUNCATE && 10487 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND && 10488 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND && 10489 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND) 10490 return SDValue(); 10491 10492 SmallVector<SDValue, 4> Inputs; 10493 SmallVector<SDValue, 8> BinOps, PromOps; 10494 SmallPtrSet<SDNode *, 16> Visited; 10495 10496 for (unsigned i = 0; i < 2; ++i) { 10497 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND || 10498 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND || 10499 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) && 10500 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) || 10501 isa<ConstantSDNode>(N->getOperand(i))) 10502 Inputs.push_back(N->getOperand(i)); 10503 else 10504 BinOps.push_back(N->getOperand(i)); 10505 10506 if (N->getOpcode() == ISD::TRUNCATE) 10507 break; 10508 } 10509 10510 // Visit all inputs, collect all binary operations (and, or, xor and 10511 // select) that are all fed by extensions. 10512 while (!BinOps.empty()) { 10513 SDValue BinOp = BinOps.back(); 10514 BinOps.pop_back(); 10515 10516 if (!Visited.insert(BinOp.getNode()).second) 10517 continue; 10518 10519 PromOps.push_back(BinOp); 10520 10521 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) { 10522 // The condition of the select is not promoted. 10523 if (BinOp.getOpcode() == ISD::SELECT && i == 0) 10524 continue; 10525 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3) 10526 continue; 10527 10528 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND || 10529 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND || 10530 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) && 10531 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) || 10532 isa<ConstantSDNode>(BinOp.getOperand(i))) { 10533 Inputs.push_back(BinOp.getOperand(i)); 10534 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND || 10535 BinOp.getOperand(i).getOpcode() == ISD::OR || 10536 BinOp.getOperand(i).getOpcode() == ISD::XOR || 10537 BinOp.getOperand(i).getOpcode() == ISD::SELECT || 10538 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC || 10539 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE || 10540 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND || 10541 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND || 10542 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) { 10543 BinOps.push_back(BinOp.getOperand(i)); 10544 } else { 10545 // We have an input that is not an extension or another binary 10546 // operation; we'll abort this transformation. 10547 return SDValue(); 10548 } 10549 } 10550 } 10551 10552 // Make sure that this is a self-contained cluster of operations (which 10553 // is not quite the same thing as saying that everything has only one 10554 // use). 10555 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { 10556 if (isa<ConstantSDNode>(Inputs[i])) 10557 continue; 10558 10559 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(), 10560 UE = Inputs[i].getNode()->use_end(); 10561 UI != UE; ++UI) { 10562 SDNode *User = *UI; 10563 if (User != N && !Visited.count(User)) 10564 return SDValue(); 10565 10566 // Make sure that we're not going to promote the non-output-value 10567 // operand(s) or SELECT or SELECT_CC. 10568 // FIXME: Although we could sometimes handle this, and it does occur in 10569 // practice that one of the condition inputs to the select is also one of 10570 // the outputs, we currently can't deal with this. 10571 if (User->getOpcode() == ISD::SELECT) { 10572 if (User->getOperand(0) == Inputs[i]) 10573 return SDValue(); 10574 } else if (User->getOpcode() == ISD::SELECT_CC) { 10575 if (User->getOperand(0) == Inputs[i] || 10576 User->getOperand(1) == Inputs[i]) 10577 return SDValue(); 10578 } 10579 } 10580 } 10581 10582 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) { 10583 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(), 10584 UE = PromOps[i].getNode()->use_end(); 10585 UI != UE; ++UI) { 10586 SDNode *User = *UI; 10587 if (User != N && !Visited.count(User)) 10588 return SDValue(); 10589 10590 // Make sure that we're not going to promote the non-output-value 10591 // operand(s) or SELECT or SELECT_CC. 10592 // FIXME: Although we could sometimes handle this, and it does occur in 10593 // practice that one of the condition inputs to the select is also one of 10594 // the outputs, we currently can't deal with this. 10595 if (User->getOpcode() == ISD::SELECT) { 10596 if (User->getOperand(0) == PromOps[i]) 10597 return SDValue(); 10598 } else if (User->getOpcode() == ISD::SELECT_CC) { 10599 if (User->getOperand(0) == PromOps[i] || 10600 User->getOperand(1) == PromOps[i]) 10601 return SDValue(); 10602 } 10603 } 10604 } 10605 10606 // Replace all inputs with the extension operand. 10607 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { 10608 // Constants may have users outside the cluster of to-be-promoted nodes, 10609 // and so we need to replace those as we do the promotions. 10610 if (isa<ConstantSDNode>(Inputs[i])) 10611 continue; 10612 else 10613 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0)); 10614 } 10615 10616 std::list<HandleSDNode> PromOpHandles; 10617 for (auto &PromOp : PromOps) 10618 PromOpHandles.emplace_back(PromOp); 10619 10620 // Replace all operations (these are all the same, but have a different 10621 // (i1) return type). DAG.getNode will validate that the types of 10622 // a binary operator match, so go through the list in reverse so that 10623 // we've likely promoted both operands first. Any intermediate truncations or 10624 // extensions disappear. 10625 while (!PromOpHandles.empty()) { 10626 SDValue PromOp = PromOpHandles.back().getValue(); 10627 PromOpHandles.pop_back(); 10628 10629 if (PromOp.getOpcode() == ISD::TRUNCATE || 10630 PromOp.getOpcode() == ISD::SIGN_EXTEND || 10631 PromOp.getOpcode() == ISD::ZERO_EXTEND || 10632 PromOp.getOpcode() == ISD::ANY_EXTEND) { 10633 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) && 10634 PromOp.getOperand(0).getValueType() != MVT::i1) { 10635 // The operand is not yet ready (see comment below). 10636 PromOpHandles.emplace_front(PromOp); 10637 continue; 10638 } 10639 10640 SDValue RepValue = PromOp.getOperand(0); 10641 if (isa<ConstantSDNode>(RepValue)) 10642 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue); 10643 10644 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue); 10645 continue; 10646 } 10647 10648 unsigned C; 10649 switch (PromOp.getOpcode()) { 10650 default: C = 0; break; 10651 case ISD::SELECT: C = 1; break; 10652 case ISD::SELECT_CC: C = 2; break; 10653 } 10654 10655 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) && 10656 PromOp.getOperand(C).getValueType() != MVT::i1) || 10657 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) && 10658 PromOp.getOperand(C+1).getValueType() != MVT::i1)) { 10659 // The to-be-promoted operands of this node have not yet been 10660 // promoted (this should be rare because we're going through the 10661 // list backward, but if one of the operands has several users in 10662 // this cluster of to-be-promoted nodes, it is possible). 10663 PromOpHandles.emplace_front(PromOp); 10664 continue; 10665 } 10666 10667 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(), 10668 PromOp.getNode()->op_end()); 10669 10670 // If there are any constant inputs, make sure they're replaced now. 10671 for (unsigned i = 0; i < 2; ++i) 10672 if (isa<ConstantSDNode>(Ops[C+i])) 10673 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]); 10674 10675 DAG.ReplaceAllUsesOfValueWith(PromOp, 10676 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops)); 10677 } 10678 10679 // Now we're left with the initial truncation itself. 10680 if (N->getOpcode() == ISD::TRUNCATE) 10681 return N->getOperand(0); 10682 10683 // Otherwise, this is a comparison. The operands to be compared have just 10684 // changed type (to i1), but everything else is the same. 10685 return SDValue(N, 0); 10686 } 10687 10688 SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N, 10689 DAGCombinerInfo &DCI) const { 10690 SelectionDAG &DAG = DCI.DAG; 10691 SDLoc dl(N); 10692 10693 // If we're tracking CR bits, we need to be careful that we don't have: 10694 // zext(binary-ops(trunc(x), trunc(y))) 10695 // or 10696 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...) 10697 // such that we're unnecessarily moving things into CR bits that can more 10698 // efficiently stay in GPRs. Note that if we're not certain that the high 10699 // bits are set as required by the final extension, we still may need to do 10700 // some masking to get the proper behavior. 10701 10702 // This same functionality is important on PPC64 when dealing with 10703 // 32-to-64-bit extensions; these occur often when 32-bit values are used as 10704 // the return values of functions. Because it is so similar, it is handled 10705 // here as well. 10706 10707 if (N->getValueType(0) != MVT::i32 && 10708 N->getValueType(0) != MVT::i64) 10709 return SDValue(); 10710 10711 if (!((N->getOperand(0).getValueType() == MVT::i1 && Subtarget.useCRBits()) || 10712 (N->getOperand(0).getValueType() == MVT::i32 && Subtarget.isPPC64()))) 10713 return SDValue(); 10714 10715 if (N->getOperand(0).getOpcode() != ISD::AND && 10716 N->getOperand(0).getOpcode() != ISD::OR && 10717 N->getOperand(0).getOpcode() != ISD::XOR && 10718 N->getOperand(0).getOpcode() != ISD::SELECT && 10719 N->getOperand(0).getOpcode() != ISD::SELECT_CC) 10720 return SDValue(); 10721 10722 SmallVector<SDValue, 4> Inputs; 10723 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps; 10724 SmallPtrSet<SDNode *, 16> Visited; 10725 10726 // Visit all inputs, collect all binary operations (and, or, xor and 10727 // select) that are all fed by truncations. 10728 while (!BinOps.empty()) { 10729 SDValue BinOp = BinOps.back(); 10730 BinOps.pop_back(); 10731 10732 if (!Visited.insert(BinOp.getNode()).second) 10733 continue; 10734 10735 PromOps.push_back(BinOp); 10736 10737 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) { 10738 // The condition of the select is not promoted. 10739 if (BinOp.getOpcode() == ISD::SELECT && i == 0) 10740 continue; 10741 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3) 10742 continue; 10743 10744 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE || 10745 isa<ConstantSDNode>(BinOp.getOperand(i))) { 10746 Inputs.push_back(BinOp.getOperand(i)); 10747 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND || 10748 BinOp.getOperand(i).getOpcode() == ISD::OR || 10749 BinOp.getOperand(i).getOpcode() == ISD::XOR || 10750 BinOp.getOperand(i).getOpcode() == ISD::SELECT || 10751 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) { 10752 BinOps.push_back(BinOp.getOperand(i)); 10753 } else { 10754 // We have an input that is not a truncation or another binary 10755 // operation; we'll abort this transformation. 10756 return SDValue(); 10757 } 10758 } 10759 } 10760 10761 // The operands of a select that must be truncated when the select is 10762 // promoted because the operand is actually part of the to-be-promoted set. 10763 DenseMap<SDNode *, EVT> SelectTruncOp[2]; 10764 10765 // Make sure that this is a self-contained cluster of operations (which 10766 // is not quite the same thing as saying that everything has only one 10767 // use). 10768 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { 10769 if (isa<ConstantSDNode>(Inputs[i])) 10770 continue; 10771 10772 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(), 10773 UE = Inputs[i].getNode()->use_end(); 10774 UI != UE; ++UI) { 10775 SDNode *User = *UI; 10776 if (User != N && !Visited.count(User)) 10777 return SDValue(); 10778 10779 // If we're going to promote the non-output-value operand(s) or SELECT or 10780 // SELECT_CC, record them for truncation. 10781 if (User->getOpcode() == ISD::SELECT) { 10782 if (User->getOperand(0) == Inputs[i]) 10783 SelectTruncOp[0].insert(std::make_pair(User, 10784 User->getOperand(0).getValueType())); 10785 } else if (User->getOpcode() == ISD::SELECT_CC) { 10786 if (User->getOperand(0) == Inputs[i]) 10787 SelectTruncOp[0].insert(std::make_pair(User, 10788 User->getOperand(0).getValueType())); 10789 if (User->getOperand(1) == Inputs[i]) 10790 SelectTruncOp[1].insert(std::make_pair(User, 10791 User->getOperand(1).getValueType())); 10792 } 10793 } 10794 } 10795 10796 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) { 10797 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(), 10798 UE = PromOps[i].getNode()->use_end(); 10799 UI != UE; ++UI) { 10800 SDNode *User = *UI; 10801 if (User != N && !Visited.count(User)) 10802 return SDValue(); 10803 10804 // If we're going to promote the non-output-value operand(s) or SELECT or 10805 // SELECT_CC, record them for truncation. 10806 if (User->getOpcode() == ISD::SELECT) { 10807 if (User->getOperand(0) == PromOps[i]) 10808 SelectTruncOp[0].insert(std::make_pair(User, 10809 User->getOperand(0).getValueType())); 10810 } else if (User->getOpcode() == ISD::SELECT_CC) { 10811 if (User->getOperand(0) == PromOps[i]) 10812 SelectTruncOp[0].insert(std::make_pair(User, 10813 User->getOperand(0).getValueType())); 10814 if (User->getOperand(1) == PromOps[i]) 10815 SelectTruncOp[1].insert(std::make_pair(User, 10816 User->getOperand(1).getValueType())); 10817 } 10818 } 10819 } 10820 10821 unsigned PromBits = N->getOperand(0).getValueSizeInBits(); 10822 bool ReallyNeedsExt = false; 10823 if (N->getOpcode() != ISD::ANY_EXTEND) { 10824 // If all of the inputs are not already sign/zero extended, then 10825 // we'll still need to do that at the end. 10826 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { 10827 if (isa<ConstantSDNode>(Inputs[i])) 10828 continue; 10829 10830 unsigned OpBits = 10831 Inputs[i].getOperand(0).getValueSizeInBits(); 10832 assert(PromBits < OpBits && "Truncation not to a smaller bit count?"); 10833 10834 if ((N->getOpcode() == ISD::ZERO_EXTEND && 10835 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0), 10836 APInt::getHighBitsSet(OpBits, 10837 OpBits-PromBits))) || 10838 (N->getOpcode() == ISD::SIGN_EXTEND && 10839 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) < 10840 (OpBits-(PromBits-1)))) { 10841 ReallyNeedsExt = true; 10842 break; 10843 } 10844 } 10845 } 10846 10847 // Replace all inputs, either with the truncation operand, or a 10848 // truncation or extension to the final output type. 10849 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { 10850 // Constant inputs need to be replaced with the to-be-promoted nodes that 10851 // use them because they might have users outside of the cluster of 10852 // promoted nodes. 10853 if (isa<ConstantSDNode>(Inputs[i])) 10854 continue; 10855 10856 SDValue InSrc = Inputs[i].getOperand(0); 10857 if (Inputs[i].getValueType() == N->getValueType(0)) 10858 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc); 10859 else if (N->getOpcode() == ISD::SIGN_EXTEND) 10860 DAG.ReplaceAllUsesOfValueWith(Inputs[i], 10861 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0))); 10862 else if (N->getOpcode() == ISD::ZERO_EXTEND) 10863 DAG.ReplaceAllUsesOfValueWith(Inputs[i], 10864 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0))); 10865 else 10866 DAG.ReplaceAllUsesOfValueWith(Inputs[i], 10867 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0))); 10868 } 10869 10870 std::list<HandleSDNode> PromOpHandles; 10871 for (auto &PromOp : PromOps) 10872 PromOpHandles.emplace_back(PromOp); 10873 10874 // Replace all operations (these are all the same, but have a different 10875 // (promoted) return type). DAG.getNode will validate that the types of 10876 // a binary operator match, so go through the list in reverse so that 10877 // we've likely promoted both operands first. 10878 while (!PromOpHandles.empty()) { 10879 SDValue PromOp = PromOpHandles.back().getValue(); 10880 PromOpHandles.pop_back(); 10881 10882 unsigned C; 10883 switch (PromOp.getOpcode()) { 10884 default: C = 0; break; 10885 case ISD::SELECT: C = 1; break; 10886 case ISD::SELECT_CC: C = 2; break; 10887 } 10888 10889 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) && 10890 PromOp.getOperand(C).getValueType() != N->getValueType(0)) || 10891 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) && 10892 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) { 10893 // The to-be-promoted operands of this node have not yet been 10894 // promoted (this should be rare because we're going through the 10895 // list backward, but if one of the operands has several users in 10896 // this cluster of to-be-promoted nodes, it is possible). 10897 PromOpHandles.emplace_front(PromOp); 10898 continue; 10899 } 10900 10901 // For SELECT and SELECT_CC nodes, we do a similar check for any 10902 // to-be-promoted comparison inputs. 10903 if (PromOp.getOpcode() == ISD::SELECT || 10904 PromOp.getOpcode() == ISD::SELECT_CC) { 10905 if ((SelectTruncOp[0].count(PromOp.getNode()) && 10906 PromOp.getOperand(0).getValueType() != N->getValueType(0)) || 10907 (SelectTruncOp[1].count(PromOp.getNode()) && 10908 PromOp.getOperand(1).getValueType() != N->getValueType(0))) { 10909 PromOpHandles.emplace_front(PromOp); 10910 continue; 10911 } 10912 } 10913 10914 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(), 10915 PromOp.getNode()->op_end()); 10916 10917 // If this node has constant inputs, then they'll need to be promoted here. 10918 for (unsigned i = 0; i < 2; ++i) { 10919 if (!isa<ConstantSDNode>(Ops[C+i])) 10920 continue; 10921 if (Ops[C+i].getValueType() == N->getValueType(0)) 10922 continue; 10923 10924 if (N->getOpcode() == ISD::SIGN_EXTEND) 10925 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0)); 10926 else if (N->getOpcode() == ISD::ZERO_EXTEND) 10927 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0)); 10928 else 10929 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0)); 10930 } 10931 10932 // If we've promoted the comparison inputs of a SELECT or SELECT_CC, 10933 // truncate them again to the original value type. 10934 if (PromOp.getOpcode() == ISD::SELECT || 10935 PromOp.getOpcode() == ISD::SELECT_CC) { 10936 auto SI0 = SelectTruncOp[0].find(PromOp.getNode()); 10937 if (SI0 != SelectTruncOp[0].end()) 10938 Ops[0] = DAG.getNode(ISD::TRUNCATE, dl, SI0->second, Ops[0]); 10939 auto SI1 = SelectTruncOp[1].find(PromOp.getNode()); 10940 if (SI1 != SelectTruncOp[1].end()) 10941 Ops[1] = DAG.getNode(ISD::TRUNCATE, dl, SI1->second, Ops[1]); 10942 } 10943 10944 DAG.ReplaceAllUsesOfValueWith(PromOp, 10945 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops)); 10946 } 10947 10948 // Now we're left with the initial extension itself. 10949 if (!ReallyNeedsExt) 10950 return N->getOperand(0); 10951 10952 // To zero extend, just mask off everything except for the first bit (in the 10953 // i1 case). 10954 if (N->getOpcode() == ISD::ZERO_EXTEND) 10955 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0), 10956 DAG.getConstant(APInt::getLowBitsSet( 10957 N->getValueSizeInBits(0), PromBits), 10958 dl, N->getValueType(0))); 10959 10960 assert(N->getOpcode() == ISD::SIGN_EXTEND && 10961 "Invalid extension type"); 10962 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0), DAG.getDataLayout()); 10963 SDValue ShiftCst = 10964 DAG.getConstant(N->getValueSizeInBits(0) - PromBits, dl, ShiftAmountTy); 10965 return DAG.getNode( 10966 ISD::SRA, dl, N->getValueType(0), 10967 DAG.getNode(ISD::SHL, dl, N->getValueType(0), N->getOperand(0), ShiftCst), 10968 ShiftCst); 10969 } 10970 10971 /// \brief Reduces the number of fp-to-int conversion when building a vector. 10972 /// 10973 /// If this vector is built out of floating to integer conversions, 10974 /// transform it to a vector built out of floating point values followed by a 10975 /// single floating to integer conversion of the vector. 10976 /// Namely (build_vector (fptosi $A), (fptosi $B), ...) 10977 /// becomes (fptosi (build_vector ($A, $B, ...))) 10978 SDValue PPCTargetLowering:: 10979 combineElementTruncationToVectorTruncation(SDNode *N, 10980 DAGCombinerInfo &DCI) const { 10981 assert(N->getOpcode() == ISD::BUILD_VECTOR && 10982 "Should be called with a BUILD_VECTOR node"); 10983 10984 SelectionDAG &DAG = DCI.DAG; 10985 SDLoc dl(N); 10986 10987 SDValue FirstInput = N->getOperand(0); 10988 assert(FirstInput.getOpcode() == PPCISD::MFVSR && 10989 "The input operand must be an fp-to-int conversion."); 10990 10991 // This combine happens after legalization so the fp_to_[su]i nodes are 10992 // already converted to PPCSISD nodes. 10993 unsigned FirstConversion = FirstInput.getOperand(0).getOpcode(); 10994 if (FirstConversion == PPCISD::FCTIDZ || 10995 FirstConversion == PPCISD::FCTIDUZ || 10996 FirstConversion == PPCISD::FCTIWZ || 10997 FirstConversion == PPCISD::FCTIWUZ) { 10998 bool IsSplat = true; 10999 bool Is32Bit = FirstConversion == PPCISD::FCTIWZ || 11000 FirstConversion == PPCISD::FCTIWUZ; 11001 EVT SrcVT = FirstInput.getOperand(0).getValueType(); 11002 SmallVector<SDValue, 4> Ops; 11003 EVT TargetVT = N->getValueType(0); 11004 for (int i = 0, e = N->getNumOperands(); i < e; ++i) { 11005 if (N->getOperand(i).getOpcode() != PPCISD::MFVSR) 11006 return SDValue(); 11007 unsigned NextConversion = N->getOperand(i).getOperand(0).getOpcode(); 11008 if (NextConversion != FirstConversion) 11009 return SDValue(); 11010 if (N->getOperand(i) != FirstInput) 11011 IsSplat = false; 11012 } 11013 11014 // If this is a splat, we leave it as-is since there will be only a single 11015 // fp-to-int conversion followed by a splat of the integer. This is better 11016 // for 32-bit and smaller ints and neutral for 64-bit ints. 11017 if (IsSplat) 11018 return SDValue(); 11019 11020 // Now that we know we have the right type of node, get its operands 11021 for (int i = 0, e = N->getNumOperands(); i < e; ++i) { 11022 SDValue In = N->getOperand(i).getOperand(0); 11023 // For 32-bit values, we need to add an FP_ROUND node. 11024 if (Is32Bit) { 11025 if (In.isUndef()) 11026 Ops.push_back(DAG.getUNDEF(SrcVT)); 11027 else { 11028 SDValue Trunc = DAG.getNode(ISD::FP_ROUND, dl, 11029 MVT::f32, In.getOperand(0), 11030 DAG.getIntPtrConstant(1, dl)); 11031 Ops.push_back(Trunc); 11032 } 11033 } else 11034 Ops.push_back(In.isUndef() ? DAG.getUNDEF(SrcVT) : In.getOperand(0)); 11035 } 11036 11037 unsigned Opcode; 11038 if (FirstConversion == PPCISD::FCTIDZ || 11039 FirstConversion == PPCISD::FCTIWZ) 11040 Opcode = ISD::FP_TO_SINT; 11041 else 11042 Opcode = ISD::FP_TO_UINT; 11043 11044 EVT NewVT = TargetVT == MVT::v2i64 ? MVT::v2f64 : MVT::v4f32; 11045 SDValue BV = DAG.getBuildVector(NewVT, dl, Ops); 11046 return DAG.getNode(Opcode, dl, TargetVT, BV); 11047 } 11048 return SDValue(); 11049 } 11050 11051 /// \brief Reduce the number of loads when building a vector. 11052 /// 11053 /// Building a vector out of multiple loads can be converted to a load 11054 /// of the vector type if the loads are consecutive. If the loads are 11055 /// consecutive but in descending order, a shuffle is added at the end 11056 /// to reorder the vector. 11057 static SDValue combineBVOfConsecutiveLoads(SDNode *N, SelectionDAG &DAG) { 11058 assert(N->getOpcode() == ISD::BUILD_VECTOR && 11059 "Should be called with a BUILD_VECTOR node"); 11060 11061 SDLoc dl(N); 11062 bool InputsAreConsecutiveLoads = true; 11063 bool InputsAreReverseConsecutive = true; 11064 unsigned ElemSize = N->getValueType(0).getScalarSizeInBits() / 8; 11065 SDValue FirstInput = N->getOperand(0); 11066 bool IsRoundOfExtLoad = false; 11067 11068 if (FirstInput.getOpcode() == ISD::FP_ROUND && 11069 FirstInput.getOperand(0).getOpcode() == ISD::LOAD) { 11070 LoadSDNode *LD = dyn_cast<LoadSDNode>(FirstInput.getOperand(0)); 11071 IsRoundOfExtLoad = LD->getExtensionType() == ISD::EXTLOAD; 11072 } 11073 // Not a build vector of (possibly fp_rounded) loads. 11074 if (!IsRoundOfExtLoad && FirstInput.getOpcode() != ISD::LOAD) 11075 return SDValue(); 11076 11077 for (int i = 1, e = N->getNumOperands(); i < e; ++i) { 11078 // If any inputs are fp_round(extload), they all must be. 11079 if (IsRoundOfExtLoad && N->getOperand(i).getOpcode() != ISD::FP_ROUND) 11080 return SDValue(); 11081 11082 SDValue NextInput = IsRoundOfExtLoad ? N->getOperand(i).getOperand(0) : 11083 N->getOperand(i); 11084 if (NextInput.getOpcode() != ISD::LOAD) 11085 return SDValue(); 11086 11087 SDValue PreviousInput = 11088 IsRoundOfExtLoad ? N->getOperand(i-1).getOperand(0) : N->getOperand(i-1); 11089 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(PreviousInput); 11090 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(NextInput); 11091 11092 // If any inputs are fp_round(extload), they all must be. 11093 if (IsRoundOfExtLoad && LD2->getExtensionType() != ISD::EXTLOAD) 11094 return SDValue(); 11095 11096 if (!isConsecutiveLS(LD2, LD1, ElemSize, 1, DAG)) 11097 InputsAreConsecutiveLoads = false; 11098 if (!isConsecutiveLS(LD1, LD2, ElemSize, 1, DAG)) 11099 InputsAreReverseConsecutive = false; 11100 11101 // Exit early if the loads are neither consecutive nor reverse consecutive. 11102 if (!InputsAreConsecutiveLoads && !InputsAreReverseConsecutive) 11103 return SDValue(); 11104 } 11105 11106 assert(!(InputsAreConsecutiveLoads && InputsAreReverseConsecutive) && 11107 "The loads cannot be both consecutive and reverse consecutive."); 11108 11109 SDValue FirstLoadOp = 11110 IsRoundOfExtLoad ? FirstInput.getOperand(0) : FirstInput; 11111 SDValue LastLoadOp = 11112 IsRoundOfExtLoad ? N->getOperand(N->getNumOperands()-1).getOperand(0) : 11113 N->getOperand(N->getNumOperands()-1); 11114 11115 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(FirstLoadOp); 11116 LoadSDNode *LDL = dyn_cast<LoadSDNode>(LastLoadOp); 11117 if (InputsAreConsecutiveLoads) { 11118 assert(LD1 && "Input needs to be a LoadSDNode."); 11119 return DAG.getLoad(N->getValueType(0), dl, LD1->getChain(), 11120 LD1->getBasePtr(), LD1->getPointerInfo(), 11121 LD1->getAlignment()); 11122 } 11123 if (InputsAreReverseConsecutive) { 11124 assert(LDL && "Input needs to be a LoadSDNode."); 11125 SDValue Load = DAG.getLoad(N->getValueType(0), dl, LDL->getChain(), 11126 LDL->getBasePtr(), LDL->getPointerInfo(), 11127 LDL->getAlignment()); 11128 SmallVector<int, 16> Ops; 11129 for (int i = N->getNumOperands() - 1; i >= 0; i--) 11130 Ops.push_back(i); 11131 11132 return DAG.getVectorShuffle(N->getValueType(0), dl, Load, 11133 DAG.getUNDEF(N->getValueType(0)), Ops); 11134 } 11135 return SDValue(); 11136 } 11137 11138 SDValue PPCTargetLowering::DAGCombineBuildVector(SDNode *N, 11139 DAGCombinerInfo &DCI) const { 11140 assert(N->getOpcode() == ISD::BUILD_VECTOR && 11141 "Should be called with a BUILD_VECTOR node"); 11142 11143 SelectionDAG &DAG = DCI.DAG; 11144 SDLoc dl(N); 11145 11146 if (!Subtarget.hasVSX()) 11147 return SDValue(); 11148 11149 // The target independent DAG combiner will leave a build_vector of 11150 // float-to-int conversions intact. We can generate MUCH better code for 11151 // a float-to-int conversion of a vector of floats. 11152 SDValue FirstInput = N->getOperand(0); 11153 if (FirstInput.getOpcode() == PPCISD::MFVSR) { 11154 SDValue Reduced = combineElementTruncationToVectorTruncation(N, DCI); 11155 if (Reduced) 11156 return Reduced; 11157 } 11158 11159 // If we're building a vector out of consecutive loads, just load that 11160 // vector type. 11161 SDValue Reduced = combineBVOfConsecutiveLoads(N, DAG); 11162 if (Reduced) 11163 return Reduced; 11164 11165 if (N->getValueType(0) != MVT::v2f64) 11166 return SDValue(); 11167 11168 // Looking for: 11169 // (build_vector ([su]int_to_fp (extractelt 0)), [su]int_to_fp (extractelt 1)) 11170 if (FirstInput.getOpcode() != ISD::SINT_TO_FP && 11171 FirstInput.getOpcode() != ISD::UINT_TO_FP) 11172 return SDValue(); 11173 if (N->getOperand(1).getOpcode() != ISD::SINT_TO_FP && 11174 N->getOperand(1).getOpcode() != ISD::UINT_TO_FP) 11175 return SDValue(); 11176 if (FirstInput.getOpcode() != N->getOperand(1).getOpcode()) 11177 return SDValue(); 11178 11179 SDValue Ext1 = FirstInput.getOperand(0); 11180 SDValue Ext2 = N->getOperand(1).getOperand(0); 11181 if(Ext1.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 11182 Ext2.getOpcode() != ISD::EXTRACT_VECTOR_ELT) 11183 return SDValue(); 11184 11185 ConstantSDNode *Ext1Op = dyn_cast<ConstantSDNode>(Ext1.getOperand(1)); 11186 ConstantSDNode *Ext2Op = dyn_cast<ConstantSDNode>(Ext2.getOperand(1)); 11187 if (!Ext1Op || !Ext2Op) 11188 return SDValue(); 11189 if (Ext1.getValueType() != MVT::i32 || 11190 Ext2.getValueType() != MVT::i32) 11191 if (Ext1.getOperand(0) != Ext2.getOperand(0)) 11192 return SDValue(); 11193 11194 int FirstElem = Ext1Op->getZExtValue(); 11195 int SecondElem = Ext2Op->getZExtValue(); 11196 int SubvecIdx; 11197 if (FirstElem == 0 && SecondElem == 1) 11198 SubvecIdx = Subtarget.isLittleEndian() ? 1 : 0; 11199 else if (FirstElem == 2 && SecondElem == 3) 11200 SubvecIdx = Subtarget.isLittleEndian() ? 0 : 1; 11201 else 11202 return SDValue(); 11203 11204 SDValue SrcVec = Ext1.getOperand(0); 11205 auto NodeType = (N->getOperand(1).getOpcode() == ISD::SINT_TO_FP) ? 11206 PPCISD::SINT_VEC_TO_FP : PPCISD::UINT_VEC_TO_FP; 11207 return DAG.getNode(NodeType, dl, MVT::v2f64, 11208 SrcVec, DAG.getIntPtrConstant(SubvecIdx, dl)); 11209 } 11210 11211 SDValue PPCTargetLowering::combineFPToIntToFP(SDNode *N, 11212 DAGCombinerInfo &DCI) const { 11213 assert((N->getOpcode() == ISD::SINT_TO_FP || 11214 N->getOpcode() == ISD::UINT_TO_FP) && 11215 "Need an int -> FP conversion node here"); 11216 11217 if (useSoftFloat() || !Subtarget.has64BitSupport()) 11218 return SDValue(); 11219 11220 SelectionDAG &DAG = DCI.DAG; 11221 SDLoc dl(N); 11222 SDValue Op(N, 0); 11223 11224 SDValue FirstOperand(Op.getOperand(0)); 11225 bool SubWordLoad = FirstOperand.getOpcode() == ISD::LOAD && 11226 (FirstOperand.getValueType() == MVT::i8 || 11227 FirstOperand.getValueType() == MVT::i16); 11228 if (Subtarget.hasP9Vector() && Subtarget.hasP9Altivec() && SubWordLoad) { 11229 bool Signed = N->getOpcode() == ISD::SINT_TO_FP; 11230 bool DstDouble = Op.getValueType() == MVT::f64; 11231 unsigned ConvOp = Signed ? 11232 (DstDouble ? PPCISD::FCFID : PPCISD::FCFIDS) : 11233 (DstDouble ? PPCISD::FCFIDU : PPCISD::FCFIDUS); 11234 SDValue WidthConst = 11235 DAG.getIntPtrConstant(FirstOperand.getValueType() == MVT::i8 ? 1 : 2, 11236 dl, false); 11237 LoadSDNode *LDN = cast<LoadSDNode>(FirstOperand.getNode()); 11238 SDValue Ops[] = { LDN->getChain(), LDN->getBasePtr(), WidthConst }; 11239 SDValue Ld = DAG.getMemIntrinsicNode(PPCISD::LXSIZX, dl, 11240 DAG.getVTList(MVT::f64, MVT::Other), 11241 Ops, MVT::i8, LDN->getMemOperand()); 11242 11243 // For signed conversion, we need to sign-extend the value in the VSR 11244 if (Signed) { 11245 SDValue ExtOps[] = { Ld, WidthConst }; 11246 SDValue Ext = DAG.getNode(PPCISD::VEXTS, dl, MVT::f64, ExtOps); 11247 return DAG.getNode(ConvOp, dl, DstDouble ? MVT::f64 : MVT::f32, Ext); 11248 } else 11249 return DAG.getNode(ConvOp, dl, DstDouble ? MVT::f64 : MVT::f32, Ld); 11250 } 11251 11252 // Don't handle ppc_fp128 here or i1 conversions. 11253 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64) 11254 return SDValue(); 11255 if (Op.getOperand(0).getValueType() == MVT::i1) 11256 return SDValue(); 11257 11258 // For i32 intermediate values, unfortunately, the conversion functions 11259 // leave the upper 32 bits of the value are undefined. Within the set of 11260 // scalar instructions, we have no method for zero- or sign-extending the 11261 // value. Thus, we cannot handle i32 intermediate values here. 11262 if (Op.getOperand(0).getValueType() == MVT::i32) 11263 return SDValue(); 11264 11265 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) && 11266 "UINT_TO_FP is supported only with FPCVT"); 11267 11268 // If we have FCFIDS, then use it when converting to single-precision. 11269 // Otherwise, convert to double-precision and then round. 11270 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) 11271 ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS 11272 : PPCISD::FCFIDS) 11273 : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU 11274 : PPCISD::FCFID); 11275 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) 11276 ? MVT::f32 11277 : MVT::f64; 11278 11279 // If we're converting from a float, to an int, and back to a float again, 11280 // then we don't need the store/load pair at all. 11281 if ((Op.getOperand(0).getOpcode() == ISD::FP_TO_UINT && 11282 Subtarget.hasFPCVT()) || 11283 (Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT)) { 11284 SDValue Src = Op.getOperand(0).getOperand(0); 11285 if (Src.getValueType() == MVT::f32) { 11286 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src); 11287 DCI.AddToWorklist(Src.getNode()); 11288 } else if (Src.getValueType() != MVT::f64) { 11289 // Make sure that we don't pick up a ppc_fp128 source value. 11290 return SDValue(); 11291 } 11292 11293 unsigned FCTOp = 11294 Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT ? PPCISD::FCTIDZ : 11295 PPCISD::FCTIDUZ; 11296 11297 SDValue Tmp = DAG.getNode(FCTOp, dl, MVT::f64, Src); 11298 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Tmp); 11299 11300 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) { 11301 FP = DAG.getNode(ISD::FP_ROUND, dl, 11302 MVT::f32, FP, DAG.getIntPtrConstant(0, dl)); 11303 DCI.AddToWorklist(FP.getNode()); 11304 } 11305 11306 return FP; 11307 } 11308 11309 return SDValue(); 11310 } 11311 11312 // expandVSXLoadForLE - Convert VSX loads (which may be intrinsics for 11313 // builtins) into loads with swaps. 11314 SDValue PPCTargetLowering::expandVSXLoadForLE(SDNode *N, 11315 DAGCombinerInfo &DCI) const { 11316 SelectionDAG &DAG = DCI.DAG; 11317 SDLoc dl(N); 11318 SDValue Chain; 11319 SDValue Base; 11320 MachineMemOperand *MMO; 11321 11322 switch (N->getOpcode()) { 11323 default: 11324 llvm_unreachable("Unexpected opcode for little endian VSX load"); 11325 case ISD::LOAD: { 11326 LoadSDNode *LD = cast<LoadSDNode>(N); 11327 Chain = LD->getChain(); 11328 Base = LD->getBasePtr(); 11329 MMO = LD->getMemOperand(); 11330 // If the MMO suggests this isn't a load of a full vector, leave 11331 // things alone. For a built-in, we have to make the change for 11332 // correctness, so if there is a size problem that will be a bug. 11333 if (MMO->getSize() < 16) 11334 return SDValue(); 11335 break; 11336 } 11337 case ISD::INTRINSIC_W_CHAIN: { 11338 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N); 11339 Chain = Intrin->getChain(); 11340 // Similarly to the store case below, Intrin->getBasePtr() doesn't get 11341 // us what we want. Get operand 2 instead. 11342 Base = Intrin->getOperand(2); 11343 MMO = Intrin->getMemOperand(); 11344 break; 11345 } 11346 } 11347 11348 MVT VecTy = N->getValueType(0).getSimpleVT(); 11349 11350 // Do not expand to PPCISD::LXVD2X + PPCISD::XXSWAPD when the load is 11351 // aligned and the type is a vector with elements up to 4 bytes 11352 if (Subtarget.needsSwapsForVSXMemOps() && !(MMO->getAlignment()%16) 11353 && VecTy.getScalarSizeInBits() <= 32 ) { 11354 return SDValue(); 11355 } 11356 11357 SDValue LoadOps[] = { Chain, Base }; 11358 SDValue Load = DAG.getMemIntrinsicNode(PPCISD::LXVD2X, dl, 11359 DAG.getVTList(MVT::v2f64, MVT::Other), 11360 LoadOps, MVT::v2f64, MMO); 11361 11362 DCI.AddToWorklist(Load.getNode()); 11363 Chain = Load.getValue(1); 11364 SDValue Swap = DAG.getNode( 11365 PPCISD::XXSWAPD, dl, DAG.getVTList(MVT::v2f64, MVT::Other), Chain, Load); 11366 DCI.AddToWorklist(Swap.getNode()); 11367 11368 // Add a bitcast if the resulting load type doesn't match v2f64. 11369 if (VecTy != MVT::v2f64) { 11370 SDValue N = DAG.getNode(ISD::BITCAST, dl, VecTy, Swap); 11371 DCI.AddToWorklist(N.getNode()); 11372 // Package {bitcast value, swap's chain} to match Load's shape. 11373 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(VecTy, MVT::Other), 11374 N, Swap.getValue(1)); 11375 } 11376 11377 return Swap; 11378 } 11379 11380 // expandVSXStoreForLE - Convert VSX stores (which may be intrinsics for 11381 // builtins) into stores with swaps. 11382 SDValue PPCTargetLowering::expandVSXStoreForLE(SDNode *N, 11383 DAGCombinerInfo &DCI) const { 11384 SelectionDAG &DAG = DCI.DAG; 11385 SDLoc dl(N); 11386 SDValue Chain; 11387 SDValue Base; 11388 unsigned SrcOpnd; 11389 MachineMemOperand *MMO; 11390 11391 switch (N->getOpcode()) { 11392 default: 11393 llvm_unreachable("Unexpected opcode for little endian VSX store"); 11394 case ISD::STORE: { 11395 StoreSDNode *ST = cast<StoreSDNode>(N); 11396 Chain = ST->getChain(); 11397 Base = ST->getBasePtr(); 11398 MMO = ST->getMemOperand(); 11399 SrcOpnd = 1; 11400 // If the MMO suggests this isn't a store of a full vector, leave 11401 // things alone. For a built-in, we have to make the change for 11402 // correctness, so if there is a size problem that will be a bug. 11403 if (MMO->getSize() < 16) 11404 return SDValue(); 11405 break; 11406 } 11407 case ISD::INTRINSIC_VOID: { 11408 MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N); 11409 Chain = Intrin->getChain(); 11410 // Intrin->getBasePtr() oddly does not get what we want. 11411 Base = Intrin->getOperand(3); 11412 MMO = Intrin->getMemOperand(); 11413 SrcOpnd = 2; 11414 break; 11415 } 11416 } 11417 11418 SDValue Src = N->getOperand(SrcOpnd); 11419 MVT VecTy = Src.getValueType().getSimpleVT(); 11420 11421 // Do not expand to PPCISD::XXSWAPD and PPCISD::STXVD2X when the load is 11422 // aligned and the type is a vector with elements up to 4 bytes 11423 if (Subtarget.needsSwapsForVSXMemOps() && !(MMO->getAlignment()%16) 11424 && VecTy.getScalarSizeInBits() <= 32 ) { 11425 return SDValue(); 11426 } 11427 11428 // All stores are done as v2f64 and possible bit cast. 11429 if (VecTy != MVT::v2f64) { 11430 Src = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Src); 11431 DCI.AddToWorklist(Src.getNode()); 11432 } 11433 11434 SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl, 11435 DAG.getVTList(MVT::v2f64, MVT::Other), Chain, Src); 11436 DCI.AddToWorklist(Swap.getNode()); 11437 Chain = Swap.getValue(1); 11438 SDValue StoreOps[] = { Chain, Swap, Base }; 11439 SDValue Store = DAG.getMemIntrinsicNode(PPCISD::STXVD2X, dl, 11440 DAG.getVTList(MVT::Other), 11441 StoreOps, VecTy, MMO); 11442 DCI.AddToWorklist(Store.getNode()); 11443 return Store; 11444 } 11445 11446 SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N, 11447 DAGCombinerInfo &DCI) const { 11448 SelectionDAG &DAG = DCI.DAG; 11449 SDLoc dl(N); 11450 switch (N->getOpcode()) { 11451 default: break; 11452 case ISD::SHL: 11453 return combineSHL(N, DCI); 11454 case ISD::SRA: 11455 return combineSRA(N, DCI); 11456 case ISD::SRL: 11457 return combineSRL(N, DCI); 11458 case PPCISD::SHL: 11459 if (isNullConstant(N->getOperand(0))) // 0 << V -> 0. 11460 return N->getOperand(0); 11461 break; 11462 case PPCISD::SRL: 11463 if (isNullConstant(N->getOperand(0))) // 0 >>u V -> 0. 11464 return N->getOperand(0); 11465 break; 11466 case PPCISD::SRA: 11467 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) { 11468 if (C->isNullValue() || // 0 >>s V -> 0. 11469 C->isAllOnesValue()) // -1 >>s V -> -1. 11470 return N->getOperand(0); 11471 } 11472 break; 11473 case ISD::SIGN_EXTEND: 11474 case ISD::ZERO_EXTEND: 11475 case ISD::ANY_EXTEND: 11476 return DAGCombineExtBoolTrunc(N, DCI); 11477 case ISD::TRUNCATE: 11478 case ISD::SETCC: 11479 case ISD::SELECT_CC: 11480 return DAGCombineTruncBoolExt(N, DCI); 11481 case ISD::SINT_TO_FP: 11482 case ISD::UINT_TO_FP: 11483 return combineFPToIntToFP(N, DCI); 11484 case ISD::STORE: { 11485 EVT Op1VT = N->getOperand(1).getValueType(); 11486 bool ValidTypeForStoreFltAsInt = (Op1VT == MVT::i32) || 11487 (Subtarget.hasP9Vector() && (Op1VT == MVT::i8 || Op1VT == MVT::i16)); 11488 11489 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)). 11490 if (Subtarget.hasSTFIWX() && !cast<StoreSDNode>(N)->isTruncatingStore() && 11491 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT && 11492 ValidTypeForStoreFltAsInt && 11493 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) { 11494 SDValue Val = N->getOperand(1).getOperand(0); 11495 if (Val.getValueType() == MVT::f32) { 11496 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val); 11497 DCI.AddToWorklist(Val.getNode()); 11498 } 11499 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val); 11500 DCI.AddToWorklist(Val.getNode()); 11501 11502 if (Op1VT == MVT::i32) { 11503 SDValue Ops[] = { 11504 N->getOperand(0), Val, N->getOperand(2), 11505 DAG.getValueType(N->getOperand(1).getValueType()) 11506 }; 11507 11508 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl, 11509 DAG.getVTList(MVT::Other), Ops, 11510 cast<StoreSDNode>(N)->getMemoryVT(), 11511 cast<StoreSDNode>(N)->getMemOperand()); 11512 } else { 11513 unsigned WidthInBytes = 11514 N->getOperand(1).getValueType() == MVT::i8 ? 1 : 2; 11515 SDValue WidthConst = DAG.getIntPtrConstant(WidthInBytes, dl, false); 11516 11517 SDValue Ops[] = { 11518 N->getOperand(0), Val, N->getOperand(2), WidthConst, 11519 DAG.getValueType(N->getOperand(1).getValueType()) 11520 }; 11521 Val = DAG.getMemIntrinsicNode(PPCISD::STXSIX, dl, 11522 DAG.getVTList(MVT::Other), Ops, 11523 cast<StoreSDNode>(N)->getMemoryVT(), 11524 cast<StoreSDNode>(N)->getMemOperand()); 11525 } 11526 11527 DCI.AddToWorklist(Val.getNode()); 11528 return Val; 11529 } 11530 11531 // Turn STORE (BSWAP) -> sthbrx/stwbrx. 11532 if (cast<StoreSDNode>(N)->isUnindexed() && 11533 N->getOperand(1).getOpcode() == ISD::BSWAP && 11534 N->getOperand(1).getNode()->hasOneUse() && 11535 (N->getOperand(1).getValueType() == MVT::i32 || 11536 N->getOperand(1).getValueType() == MVT::i16 || 11537 (Subtarget.hasLDBRX() && Subtarget.isPPC64() && 11538 N->getOperand(1).getValueType() == MVT::i64))) { 11539 SDValue BSwapOp = N->getOperand(1).getOperand(0); 11540 // Do an any-extend to 32-bits if this is a half-word input. 11541 if (BSwapOp.getValueType() == MVT::i16) 11542 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp); 11543 11544 // If the type of BSWAP operand is wider than stored memory width 11545 // it need to be shifted to the right side before STBRX. 11546 EVT mVT = cast<StoreSDNode>(N)->getMemoryVT(); 11547 if (Op1VT.bitsGT(mVT)) { 11548 int Shift = Op1VT.getSizeInBits() - mVT.getSizeInBits(); 11549 BSwapOp = DAG.getNode(ISD::SRL, dl, Op1VT, BSwapOp, 11550 DAG.getConstant(Shift, dl, MVT::i32)); 11551 // Need to truncate if this is a bswap of i64 stored as i32/i16. 11552 if (Op1VT == MVT::i64) 11553 BSwapOp = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BSwapOp); 11554 } 11555 11556 SDValue Ops[] = { 11557 N->getOperand(0), BSwapOp, N->getOperand(2), DAG.getValueType(mVT) 11558 }; 11559 return 11560 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other), 11561 Ops, cast<StoreSDNode>(N)->getMemoryVT(), 11562 cast<StoreSDNode>(N)->getMemOperand()); 11563 } 11564 11565 // For little endian, VSX stores require generating xxswapd/lxvd2x. 11566 // Not needed on ISA 3.0 based CPUs since we have a non-permuting store. 11567 EVT VT = N->getOperand(1).getValueType(); 11568 if (VT.isSimple()) { 11569 MVT StoreVT = VT.getSimpleVT(); 11570 if (Subtarget.needsSwapsForVSXMemOps() && 11571 (StoreVT == MVT::v2f64 || StoreVT == MVT::v2i64 || 11572 StoreVT == MVT::v4f32 || StoreVT == MVT::v4i32)) 11573 return expandVSXStoreForLE(N, DCI); 11574 } 11575 break; 11576 } 11577 case ISD::LOAD: { 11578 LoadSDNode *LD = cast<LoadSDNode>(N); 11579 EVT VT = LD->getValueType(0); 11580 11581 // For little endian, VSX loads require generating lxvd2x/xxswapd. 11582 // Not needed on ISA 3.0 based CPUs since we have a non-permuting load. 11583 if (VT.isSimple()) { 11584 MVT LoadVT = VT.getSimpleVT(); 11585 if (Subtarget.needsSwapsForVSXMemOps() && 11586 (LoadVT == MVT::v2f64 || LoadVT == MVT::v2i64 || 11587 LoadVT == MVT::v4f32 || LoadVT == MVT::v4i32)) 11588 return expandVSXLoadForLE(N, DCI); 11589 } 11590 11591 // We sometimes end up with a 64-bit integer load, from which we extract 11592 // two single-precision floating-point numbers. This happens with 11593 // std::complex<float>, and other similar structures, because of the way we 11594 // canonicalize structure copies. However, if we lack direct moves, 11595 // then the final bitcasts from the extracted integer values to the 11596 // floating-point numbers turn into store/load pairs. Even with direct moves, 11597 // just loading the two floating-point numbers is likely better. 11598 auto ReplaceTwoFloatLoad = [&]() { 11599 if (VT != MVT::i64) 11600 return false; 11601 11602 if (LD->getExtensionType() != ISD::NON_EXTLOAD || 11603 LD->isVolatile()) 11604 return false; 11605 11606 // We're looking for a sequence like this: 11607 // t13: i64,ch = load<LD8[%ref.tmp]> t0, t6, undef:i64 11608 // t16: i64 = srl t13, Constant:i32<32> 11609 // t17: i32 = truncate t16 11610 // t18: f32 = bitcast t17 11611 // t19: i32 = truncate t13 11612 // t20: f32 = bitcast t19 11613 11614 if (!LD->hasNUsesOfValue(2, 0)) 11615 return false; 11616 11617 auto UI = LD->use_begin(); 11618 while (UI.getUse().getResNo() != 0) ++UI; 11619 SDNode *Trunc = *UI++; 11620 while (UI.getUse().getResNo() != 0) ++UI; 11621 SDNode *RightShift = *UI; 11622 if (Trunc->getOpcode() != ISD::TRUNCATE) 11623 std::swap(Trunc, RightShift); 11624 11625 if (Trunc->getOpcode() != ISD::TRUNCATE || 11626 Trunc->getValueType(0) != MVT::i32 || 11627 !Trunc->hasOneUse()) 11628 return false; 11629 if (RightShift->getOpcode() != ISD::SRL || 11630 !isa<ConstantSDNode>(RightShift->getOperand(1)) || 11631 RightShift->getConstantOperandVal(1) != 32 || 11632 !RightShift->hasOneUse()) 11633 return false; 11634 11635 SDNode *Trunc2 = *RightShift->use_begin(); 11636 if (Trunc2->getOpcode() != ISD::TRUNCATE || 11637 Trunc2->getValueType(0) != MVT::i32 || 11638 !Trunc2->hasOneUse()) 11639 return false; 11640 11641 SDNode *Bitcast = *Trunc->use_begin(); 11642 SDNode *Bitcast2 = *Trunc2->use_begin(); 11643 11644 if (Bitcast->getOpcode() != ISD::BITCAST || 11645 Bitcast->getValueType(0) != MVT::f32) 11646 return false; 11647 if (Bitcast2->getOpcode() != ISD::BITCAST || 11648 Bitcast2->getValueType(0) != MVT::f32) 11649 return false; 11650 11651 if (Subtarget.isLittleEndian()) 11652 std::swap(Bitcast, Bitcast2); 11653 11654 // Bitcast has the second float (in memory-layout order) and Bitcast2 11655 // has the first one. 11656 11657 SDValue BasePtr = LD->getBasePtr(); 11658 if (LD->isIndexed()) { 11659 assert(LD->getAddressingMode() == ISD::PRE_INC && 11660 "Non-pre-inc AM on PPC?"); 11661 BasePtr = 11662 DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, 11663 LD->getOffset()); 11664 } 11665 11666 auto MMOFlags = 11667 LD->getMemOperand()->getFlags() & ~MachineMemOperand::MOVolatile; 11668 SDValue FloatLoad = DAG.getLoad(MVT::f32, dl, LD->getChain(), BasePtr, 11669 LD->getPointerInfo(), LD->getAlignment(), 11670 MMOFlags, LD->getAAInfo()); 11671 SDValue AddPtr = 11672 DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), 11673 BasePtr, DAG.getIntPtrConstant(4, dl)); 11674 SDValue FloatLoad2 = DAG.getLoad( 11675 MVT::f32, dl, SDValue(FloatLoad.getNode(), 1), AddPtr, 11676 LD->getPointerInfo().getWithOffset(4), 11677 MinAlign(LD->getAlignment(), 4), MMOFlags, LD->getAAInfo()); 11678 11679 if (LD->isIndexed()) { 11680 // Note that DAGCombine should re-form any pre-increment load(s) from 11681 // what is produced here if that makes sense. 11682 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), BasePtr); 11683 } 11684 11685 DCI.CombineTo(Bitcast2, FloatLoad); 11686 DCI.CombineTo(Bitcast, FloatLoad2); 11687 11688 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, LD->isIndexed() ? 2 : 1), 11689 SDValue(FloatLoad2.getNode(), 1)); 11690 return true; 11691 }; 11692 11693 if (ReplaceTwoFloatLoad()) 11694 return SDValue(N, 0); 11695 11696 EVT MemVT = LD->getMemoryVT(); 11697 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext()); 11698 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty); 11699 Type *STy = MemVT.getScalarType().getTypeForEVT(*DAG.getContext()); 11700 unsigned ScalarABIAlignment = DAG.getDataLayout().getABITypeAlignment(STy); 11701 if (LD->isUnindexed() && VT.isVector() && 11702 ((Subtarget.hasAltivec() && ISD::isNON_EXTLoad(N) && 11703 // P8 and later hardware should just use LOAD. 11704 !Subtarget.hasP8Vector() && (VT == MVT::v16i8 || VT == MVT::v8i16 || 11705 VT == MVT::v4i32 || VT == MVT::v4f32)) || 11706 (Subtarget.hasQPX() && (VT == MVT::v4f64 || VT == MVT::v4f32) && 11707 LD->getAlignment() >= ScalarABIAlignment)) && 11708 LD->getAlignment() < ABIAlignment) { 11709 // This is a type-legal unaligned Altivec or QPX load. 11710 SDValue Chain = LD->getChain(); 11711 SDValue Ptr = LD->getBasePtr(); 11712 bool isLittleEndian = Subtarget.isLittleEndian(); 11713 11714 // This implements the loading of unaligned vectors as described in 11715 // the venerable Apple Velocity Engine overview. Specifically: 11716 // https://developer.apple.com/hardwaredrivers/ve/alignment.html 11717 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html 11718 // 11719 // The general idea is to expand a sequence of one or more unaligned 11720 // loads into an alignment-based permutation-control instruction (lvsl 11721 // or lvsr), a series of regular vector loads (which always truncate 11722 // their input address to an aligned address), and a series of 11723 // permutations. The results of these permutations are the requested 11724 // loaded values. The trick is that the last "extra" load is not taken 11725 // from the address you might suspect (sizeof(vector) bytes after the 11726 // last requested load), but rather sizeof(vector) - 1 bytes after the 11727 // last requested vector. The point of this is to avoid a page fault if 11728 // the base address happened to be aligned. This works because if the 11729 // base address is aligned, then adding less than a full vector length 11730 // will cause the last vector in the sequence to be (re)loaded. 11731 // Otherwise, the next vector will be fetched as you might suspect was 11732 // necessary. 11733 11734 // We might be able to reuse the permutation generation from 11735 // a different base address offset from this one by an aligned amount. 11736 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this 11737 // optimization later. 11738 Intrinsic::ID Intr, IntrLD, IntrPerm; 11739 MVT PermCntlTy, PermTy, LDTy; 11740 if (Subtarget.hasAltivec()) { 11741 Intr = isLittleEndian ? Intrinsic::ppc_altivec_lvsr : 11742 Intrinsic::ppc_altivec_lvsl; 11743 IntrLD = Intrinsic::ppc_altivec_lvx; 11744 IntrPerm = Intrinsic::ppc_altivec_vperm; 11745 PermCntlTy = MVT::v16i8; 11746 PermTy = MVT::v4i32; 11747 LDTy = MVT::v4i32; 11748 } else { 11749 Intr = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlpcld : 11750 Intrinsic::ppc_qpx_qvlpcls; 11751 IntrLD = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlfd : 11752 Intrinsic::ppc_qpx_qvlfs; 11753 IntrPerm = Intrinsic::ppc_qpx_qvfperm; 11754 PermCntlTy = MVT::v4f64; 11755 PermTy = MVT::v4f64; 11756 LDTy = MemVT.getSimpleVT(); 11757 } 11758 11759 SDValue PermCntl = BuildIntrinsicOp(Intr, Ptr, DAG, dl, PermCntlTy); 11760 11761 // Create the new MMO for the new base load. It is like the original MMO, 11762 // but represents an area in memory almost twice the vector size centered 11763 // on the original address. If the address is unaligned, we might start 11764 // reading up to (sizeof(vector)-1) bytes below the address of the 11765 // original unaligned load. 11766 MachineFunction &MF = DAG.getMachineFunction(); 11767 MachineMemOperand *BaseMMO = 11768 MF.getMachineMemOperand(LD->getMemOperand(), 11769 -(long)MemVT.getStoreSize()+1, 11770 2*MemVT.getStoreSize()-1); 11771 11772 // Create the new base load. 11773 SDValue LDXIntID = 11774 DAG.getTargetConstant(IntrLD, dl, getPointerTy(MF.getDataLayout())); 11775 SDValue BaseLoadOps[] = { Chain, LDXIntID, Ptr }; 11776 SDValue BaseLoad = 11777 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl, 11778 DAG.getVTList(PermTy, MVT::Other), 11779 BaseLoadOps, LDTy, BaseMMO); 11780 11781 // Note that the value of IncOffset (which is provided to the next 11782 // load's pointer info offset value, and thus used to calculate the 11783 // alignment), and the value of IncValue (which is actually used to 11784 // increment the pointer value) are different! This is because we 11785 // require the next load to appear to be aligned, even though it 11786 // is actually offset from the base pointer by a lesser amount. 11787 int IncOffset = VT.getSizeInBits() / 8; 11788 int IncValue = IncOffset; 11789 11790 // Walk (both up and down) the chain looking for another load at the real 11791 // (aligned) offset (the alignment of the other load does not matter in 11792 // this case). If found, then do not use the offset reduction trick, as 11793 // that will prevent the loads from being later combined (as they would 11794 // otherwise be duplicates). 11795 if (!findConsecutiveLoad(LD, DAG)) 11796 --IncValue; 11797 11798 SDValue Increment = 11799 DAG.getConstant(IncValue, dl, getPointerTy(MF.getDataLayout())); 11800 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); 11801 11802 MachineMemOperand *ExtraMMO = 11803 MF.getMachineMemOperand(LD->getMemOperand(), 11804 1, 2*MemVT.getStoreSize()-1); 11805 SDValue ExtraLoadOps[] = { Chain, LDXIntID, Ptr }; 11806 SDValue ExtraLoad = 11807 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl, 11808 DAG.getVTList(PermTy, MVT::Other), 11809 ExtraLoadOps, LDTy, ExtraMMO); 11810 11811 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 11812 BaseLoad.getValue(1), ExtraLoad.getValue(1)); 11813 11814 // Because vperm has a big-endian bias, we must reverse the order 11815 // of the input vectors and complement the permute control vector 11816 // when generating little endian code. We have already handled the 11817 // latter by using lvsr instead of lvsl, so just reverse BaseLoad 11818 // and ExtraLoad here. 11819 SDValue Perm; 11820 if (isLittleEndian) 11821 Perm = BuildIntrinsicOp(IntrPerm, 11822 ExtraLoad, BaseLoad, PermCntl, DAG, dl); 11823 else 11824 Perm = BuildIntrinsicOp(IntrPerm, 11825 BaseLoad, ExtraLoad, PermCntl, DAG, dl); 11826 11827 if (VT != PermTy) 11828 Perm = Subtarget.hasAltivec() ? 11829 DAG.getNode(ISD::BITCAST, dl, VT, Perm) : 11830 DAG.getNode(ISD::FP_ROUND, dl, VT, Perm, // QPX 11831 DAG.getTargetConstant(1, dl, MVT::i64)); 11832 // second argument is 1 because this rounding 11833 // is always exact. 11834 11835 // The output of the permutation is our loaded result, the TokenFactor is 11836 // our new chain. 11837 DCI.CombineTo(N, Perm, TF); 11838 return SDValue(N, 0); 11839 } 11840 } 11841 break; 11842 case ISD::INTRINSIC_WO_CHAIN: { 11843 bool isLittleEndian = Subtarget.isLittleEndian(); 11844 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 11845 Intrinsic::ID Intr = (isLittleEndian ? Intrinsic::ppc_altivec_lvsr 11846 : Intrinsic::ppc_altivec_lvsl); 11847 if ((IID == Intr || 11848 IID == Intrinsic::ppc_qpx_qvlpcld || 11849 IID == Intrinsic::ppc_qpx_qvlpcls) && 11850 N->getOperand(1)->getOpcode() == ISD::ADD) { 11851 SDValue Add = N->getOperand(1); 11852 11853 int Bits = IID == Intrinsic::ppc_qpx_qvlpcld ? 11854 5 /* 32 byte alignment */ : 4 /* 16 byte alignment */; 11855 11856 if (DAG.MaskedValueIsZero(Add->getOperand(1), 11857 APInt::getAllOnesValue(Bits /* alignment */) 11858 .zext(Add.getScalarValueSizeInBits()))) { 11859 SDNode *BasePtr = Add->getOperand(0).getNode(); 11860 for (SDNode::use_iterator UI = BasePtr->use_begin(), 11861 UE = BasePtr->use_end(); 11862 UI != UE; ++UI) { 11863 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN && 11864 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() == IID) { 11865 // We've found another LVSL/LVSR, and this address is an aligned 11866 // multiple of that one. The results will be the same, so use the 11867 // one we've just found instead. 11868 11869 return SDValue(*UI, 0); 11870 } 11871 } 11872 } 11873 11874 if (isa<ConstantSDNode>(Add->getOperand(1))) { 11875 SDNode *BasePtr = Add->getOperand(0).getNode(); 11876 for (SDNode::use_iterator UI = BasePtr->use_begin(), 11877 UE = BasePtr->use_end(); UI != UE; ++UI) { 11878 if (UI->getOpcode() == ISD::ADD && 11879 isa<ConstantSDNode>(UI->getOperand(1)) && 11880 (cast<ConstantSDNode>(Add->getOperand(1))->getZExtValue() - 11881 cast<ConstantSDNode>(UI->getOperand(1))->getZExtValue()) % 11882 (1ULL << Bits) == 0) { 11883 SDNode *OtherAdd = *UI; 11884 for (SDNode::use_iterator VI = OtherAdd->use_begin(), 11885 VE = OtherAdd->use_end(); VI != VE; ++VI) { 11886 if (VI->getOpcode() == ISD::INTRINSIC_WO_CHAIN && 11887 cast<ConstantSDNode>(VI->getOperand(0))->getZExtValue() == IID) { 11888 return SDValue(*VI, 0); 11889 } 11890 } 11891 } 11892 } 11893 } 11894 } 11895 } 11896 11897 break; 11898 case ISD::INTRINSIC_W_CHAIN: 11899 // For little endian, VSX loads require generating lxvd2x/xxswapd. 11900 // Not needed on ISA 3.0 based CPUs since we have a non-permuting load. 11901 if (Subtarget.needsSwapsForVSXMemOps()) { 11902 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 11903 default: 11904 break; 11905 case Intrinsic::ppc_vsx_lxvw4x: 11906 case Intrinsic::ppc_vsx_lxvd2x: 11907 return expandVSXLoadForLE(N, DCI); 11908 } 11909 } 11910 break; 11911 case ISD::INTRINSIC_VOID: 11912 // For little endian, VSX stores require generating xxswapd/stxvd2x. 11913 // Not needed on ISA 3.0 based CPUs since we have a non-permuting store. 11914 if (Subtarget.needsSwapsForVSXMemOps()) { 11915 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 11916 default: 11917 break; 11918 case Intrinsic::ppc_vsx_stxvw4x: 11919 case Intrinsic::ppc_vsx_stxvd2x: 11920 return expandVSXStoreForLE(N, DCI); 11921 } 11922 } 11923 break; 11924 case ISD::BSWAP: 11925 // Turn BSWAP (LOAD) -> lhbrx/lwbrx. 11926 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) && 11927 N->getOperand(0).hasOneUse() && 11928 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 || 11929 (Subtarget.hasLDBRX() && Subtarget.isPPC64() && 11930 N->getValueType(0) == MVT::i64))) { 11931 SDValue Load = N->getOperand(0); 11932 LoadSDNode *LD = cast<LoadSDNode>(Load); 11933 // Create the byte-swapping load. 11934 SDValue Ops[] = { 11935 LD->getChain(), // Chain 11936 LD->getBasePtr(), // Ptr 11937 DAG.getValueType(N->getValueType(0)) // VT 11938 }; 11939 SDValue BSLoad = 11940 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl, 11941 DAG.getVTList(N->getValueType(0) == MVT::i64 ? 11942 MVT::i64 : MVT::i32, MVT::Other), 11943 Ops, LD->getMemoryVT(), LD->getMemOperand()); 11944 11945 // If this is an i16 load, insert the truncate. 11946 SDValue ResVal = BSLoad; 11947 if (N->getValueType(0) == MVT::i16) 11948 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad); 11949 11950 // First, combine the bswap away. This makes the value produced by the 11951 // load dead. 11952 DCI.CombineTo(N, ResVal); 11953 11954 // Next, combine the load away, we give it a bogus result value but a real 11955 // chain result. The result value is dead because the bswap is dead. 11956 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1)); 11957 11958 // Return N so it doesn't get rechecked! 11959 return SDValue(N, 0); 11960 } 11961 break; 11962 case PPCISD::VCMP: 11963 // If a VCMPo node already exists with exactly the same operands as this 11964 // node, use its result instead of this node (VCMPo computes both a CR6 and 11965 // a normal output). 11966 // 11967 if (!N->getOperand(0).hasOneUse() && 11968 !N->getOperand(1).hasOneUse() && 11969 !N->getOperand(2).hasOneUse()) { 11970 11971 // Scan all of the users of the LHS, looking for VCMPo's that match. 11972 SDNode *VCMPoNode = nullptr; 11973 11974 SDNode *LHSN = N->getOperand(0).getNode(); 11975 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end(); 11976 UI != E; ++UI) 11977 if (UI->getOpcode() == PPCISD::VCMPo && 11978 UI->getOperand(1) == N->getOperand(1) && 11979 UI->getOperand(2) == N->getOperand(2) && 11980 UI->getOperand(0) == N->getOperand(0)) { 11981 VCMPoNode = *UI; 11982 break; 11983 } 11984 11985 // If there is no VCMPo node, or if the flag value has a single use, don't 11986 // transform this. 11987 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1)) 11988 break; 11989 11990 // Look at the (necessarily single) use of the flag value. If it has a 11991 // chain, this transformation is more complex. Note that multiple things 11992 // could use the value result, which we should ignore. 11993 SDNode *FlagUser = nullptr; 11994 for (SDNode::use_iterator UI = VCMPoNode->use_begin(); 11995 FlagUser == nullptr; ++UI) { 11996 assert(UI != VCMPoNode->use_end() && "Didn't find user!"); 11997 SDNode *User = *UI; 11998 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { 11999 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) { 12000 FlagUser = User; 12001 break; 12002 } 12003 } 12004 } 12005 12006 // If the user is a MFOCRF instruction, we know this is safe. 12007 // Otherwise we give up for right now. 12008 if (FlagUser->getOpcode() == PPCISD::MFOCRF) 12009 return SDValue(VCMPoNode, 0); 12010 } 12011 break; 12012 case ISD::BRCOND: { 12013 SDValue Cond = N->getOperand(1); 12014 SDValue Target = N->getOperand(2); 12015 12016 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN && 12017 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() == 12018 Intrinsic::ppc_is_decremented_ctr_nonzero) { 12019 12020 // We now need to make the intrinsic dead (it cannot be instruction 12021 // selected). 12022 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0)); 12023 assert(Cond.getNode()->hasOneUse() && 12024 "Counter decrement has more than one use"); 12025 12026 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other, 12027 N->getOperand(0), Target); 12028 } 12029 } 12030 break; 12031 case ISD::BR_CC: { 12032 // If this is a branch on an altivec predicate comparison, lower this so 12033 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This 12034 // lowering is done pre-legalize, because the legalizer lowers the predicate 12035 // compare down to code that is difficult to reassemble. 12036 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); 12037 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3); 12038 12039 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero 12040 // value. If so, pass-through the AND to get to the intrinsic. 12041 if (LHS.getOpcode() == ISD::AND && 12042 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN && 12043 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() == 12044 Intrinsic::ppc_is_decremented_ctr_nonzero && 12045 isa<ConstantSDNode>(LHS.getOperand(1)) && 12046 !isNullConstant(LHS.getOperand(1))) 12047 LHS = LHS.getOperand(0); 12048 12049 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN && 12050 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 12051 Intrinsic::ppc_is_decremented_ctr_nonzero && 12052 isa<ConstantSDNode>(RHS)) { 12053 assert((CC == ISD::SETEQ || CC == ISD::SETNE) && 12054 "Counter decrement comparison is not EQ or NE"); 12055 12056 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue(); 12057 bool isBDNZ = (CC == ISD::SETEQ && Val) || 12058 (CC == ISD::SETNE && !Val); 12059 12060 // We now need to make the intrinsic dead (it cannot be instruction 12061 // selected). 12062 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0)); 12063 assert(LHS.getNode()->hasOneUse() && 12064 "Counter decrement has more than one use"); 12065 12066 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other, 12067 N->getOperand(0), N->getOperand(4)); 12068 } 12069 12070 int CompareOpc; 12071 bool isDot; 12072 12073 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN && 12074 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) && 12075 getVectorCompareInfo(LHS, CompareOpc, isDot, Subtarget)) { 12076 assert(isDot && "Can't compare against a vector result!"); 12077 12078 // If this is a comparison against something other than 0/1, then we know 12079 // that the condition is never/always true. 12080 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue(); 12081 if (Val != 0 && Val != 1) { 12082 if (CC == ISD::SETEQ) // Cond never true, remove branch. 12083 return N->getOperand(0); 12084 // Always !=, turn it into an unconditional branch. 12085 return DAG.getNode(ISD::BR, dl, MVT::Other, 12086 N->getOperand(0), N->getOperand(4)); 12087 } 12088 12089 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0); 12090 12091 // Create the PPCISD altivec 'dot' comparison node. 12092 SDValue Ops[] = { 12093 LHS.getOperand(2), // LHS of compare 12094 LHS.getOperand(3), // RHS of compare 12095 DAG.getConstant(CompareOpc, dl, MVT::i32) 12096 }; 12097 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue }; 12098 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops); 12099 12100 // Unpack the result based on how the target uses it. 12101 PPC::Predicate CompOpc; 12102 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) { 12103 default: // Can't happen, don't crash on invalid number though. 12104 case 0: // Branch on the value of the EQ bit of CR6. 12105 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE; 12106 break; 12107 case 1: // Branch on the inverted value of the EQ bit of CR6. 12108 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ; 12109 break; 12110 case 2: // Branch on the value of the LT bit of CR6. 12111 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE; 12112 break; 12113 case 3: // Branch on the inverted value of the LT bit of CR6. 12114 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT; 12115 break; 12116 } 12117 12118 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0), 12119 DAG.getConstant(CompOpc, dl, MVT::i32), 12120 DAG.getRegister(PPC::CR6, MVT::i32), 12121 N->getOperand(4), CompNode.getValue(1)); 12122 } 12123 break; 12124 } 12125 case ISD::BUILD_VECTOR: 12126 return DAGCombineBuildVector(N, DCI); 12127 } 12128 12129 return SDValue(); 12130 } 12131 12132 SDValue 12133 PPCTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor, 12134 SelectionDAG &DAG, 12135 std::vector<SDNode *> *Created) const { 12136 // fold (sdiv X, pow2) 12137 EVT VT = N->getValueType(0); 12138 if (VT == MVT::i64 && !Subtarget.isPPC64()) 12139 return SDValue(); 12140 if ((VT != MVT::i32 && VT != MVT::i64) || 12141 !(Divisor.isPowerOf2() || (-Divisor).isPowerOf2())) 12142 return SDValue(); 12143 12144 SDLoc DL(N); 12145 SDValue N0 = N->getOperand(0); 12146 12147 bool IsNegPow2 = (-Divisor).isPowerOf2(); 12148 unsigned Lg2 = (IsNegPow2 ? -Divisor : Divisor).countTrailingZeros(); 12149 SDValue ShiftAmt = DAG.getConstant(Lg2, DL, VT); 12150 12151 SDValue Op = DAG.getNode(PPCISD::SRA_ADDZE, DL, VT, N0, ShiftAmt); 12152 if (Created) 12153 Created->push_back(Op.getNode()); 12154 12155 if (IsNegPow2) { 12156 Op = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Op); 12157 if (Created) 12158 Created->push_back(Op.getNode()); 12159 } 12160 12161 return Op; 12162 } 12163 12164 //===----------------------------------------------------------------------===// 12165 // Inline Assembly Support 12166 //===----------------------------------------------------------------------===// 12167 12168 void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, 12169 KnownBits &Known, 12170 const APInt &DemandedElts, 12171 const SelectionDAG &DAG, 12172 unsigned Depth) const { 12173 Known.resetAll(); 12174 switch (Op.getOpcode()) { 12175 default: break; 12176 case PPCISD::LBRX: { 12177 // lhbrx is known to have the top bits cleared out. 12178 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16) 12179 Known.Zero = 0xFFFF0000; 12180 break; 12181 } 12182 case ISD::INTRINSIC_WO_CHAIN: { 12183 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) { 12184 default: break; 12185 case Intrinsic::ppc_altivec_vcmpbfp_p: 12186 case Intrinsic::ppc_altivec_vcmpeqfp_p: 12187 case Intrinsic::ppc_altivec_vcmpequb_p: 12188 case Intrinsic::ppc_altivec_vcmpequh_p: 12189 case Intrinsic::ppc_altivec_vcmpequw_p: 12190 case Intrinsic::ppc_altivec_vcmpequd_p: 12191 case Intrinsic::ppc_altivec_vcmpgefp_p: 12192 case Intrinsic::ppc_altivec_vcmpgtfp_p: 12193 case Intrinsic::ppc_altivec_vcmpgtsb_p: 12194 case Intrinsic::ppc_altivec_vcmpgtsh_p: 12195 case Intrinsic::ppc_altivec_vcmpgtsw_p: 12196 case Intrinsic::ppc_altivec_vcmpgtsd_p: 12197 case Intrinsic::ppc_altivec_vcmpgtub_p: 12198 case Intrinsic::ppc_altivec_vcmpgtuh_p: 12199 case Intrinsic::ppc_altivec_vcmpgtuw_p: 12200 case Intrinsic::ppc_altivec_vcmpgtud_p: 12201 Known.Zero = ~1U; // All bits but the low one are known to be zero. 12202 break; 12203 } 12204 } 12205 } 12206 } 12207 12208 unsigned PPCTargetLowering::getPrefLoopAlignment(MachineLoop *ML) const { 12209 switch (Subtarget.getDarwinDirective()) { 12210 default: break; 12211 case PPC::DIR_970: 12212 case PPC::DIR_PWR4: 12213 case PPC::DIR_PWR5: 12214 case PPC::DIR_PWR5X: 12215 case PPC::DIR_PWR6: 12216 case PPC::DIR_PWR6X: 12217 case PPC::DIR_PWR7: 12218 case PPC::DIR_PWR8: 12219 case PPC::DIR_PWR9: { 12220 if (!ML) 12221 break; 12222 12223 const PPCInstrInfo *TII = Subtarget.getInstrInfo(); 12224 12225 // For small loops (between 5 and 8 instructions), align to a 32-byte 12226 // boundary so that the entire loop fits in one instruction-cache line. 12227 uint64_t LoopSize = 0; 12228 for (auto I = ML->block_begin(), IE = ML->block_end(); I != IE; ++I) 12229 for (auto J = (*I)->begin(), JE = (*I)->end(); J != JE; ++J) { 12230 LoopSize += TII->getInstSizeInBytes(*J); 12231 if (LoopSize > 32) 12232 break; 12233 } 12234 12235 if (LoopSize > 16 && LoopSize <= 32) 12236 return 5; 12237 12238 break; 12239 } 12240 } 12241 12242 return TargetLowering::getPrefLoopAlignment(ML); 12243 } 12244 12245 /// getConstraintType - Given a constraint, return the type of 12246 /// constraint it is for this target. 12247 PPCTargetLowering::ConstraintType 12248 PPCTargetLowering::getConstraintType(StringRef Constraint) const { 12249 if (Constraint.size() == 1) { 12250 switch (Constraint[0]) { 12251 default: break; 12252 case 'b': 12253 case 'r': 12254 case 'f': 12255 case 'd': 12256 case 'v': 12257 case 'y': 12258 return C_RegisterClass; 12259 case 'Z': 12260 // FIXME: While Z does indicate a memory constraint, it specifically 12261 // indicates an r+r address (used in conjunction with the 'y' modifier 12262 // in the replacement string). Currently, we're forcing the base 12263 // register to be r0 in the asm printer (which is interpreted as zero) 12264 // and forming the complete address in the second register. This is 12265 // suboptimal. 12266 return C_Memory; 12267 } 12268 } else if (Constraint == "wc") { // individual CR bits. 12269 return C_RegisterClass; 12270 } else if (Constraint == "wa" || Constraint == "wd" || 12271 Constraint == "wf" || Constraint == "ws") { 12272 return C_RegisterClass; // VSX registers. 12273 } 12274 return TargetLowering::getConstraintType(Constraint); 12275 } 12276 12277 /// Examine constraint type and operand type and determine a weight value. 12278 /// This object must already have been set up with the operand type 12279 /// and the current alternative constraint selected. 12280 TargetLowering::ConstraintWeight 12281 PPCTargetLowering::getSingleConstraintMatchWeight( 12282 AsmOperandInfo &info, const char *constraint) const { 12283 ConstraintWeight weight = CW_Invalid; 12284 Value *CallOperandVal = info.CallOperandVal; 12285 // If we don't have a value, we can't do a match, 12286 // but allow it at the lowest weight. 12287 if (!CallOperandVal) 12288 return CW_Default; 12289 Type *type = CallOperandVal->getType(); 12290 12291 // Look at the constraint type. 12292 if (StringRef(constraint) == "wc" && type->isIntegerTy(1)) 12293 return CW_Register; // an individual CR bit. 12294 else if ((StringRef(constraint) == "wa" || 12295 StringRef(constraint) == "wd" || 12296 StringRef(constraint) == "wf") && 12297 type->isVectorTy()) 12298 return CW_Register; 12299 else if (StringRef(constraint) == "ws" && type->isDoubleTy()) 12300 return CW_Register; 12301 12302 switch (*constraint) { 12303 default: 12304 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 12305 break; 12306 case 'b': 12307 if (type->isIntegerTy()) 12308 weight = CW_Register; 12309 break; 12310 case 'f': 12311 if (type->isFloatTy()) 12312 weight = CW_Register; 12313 break; 12314 case 'd': 12315 if (type->isDoubleTy()) 12316 weight = CW_Register; 12317 break; 12318 case 'v': 12319 if (type->isVectorTy()) 12320 weight = CW_Register; 12321 break; 12322 case 'y': 12323 weight = CW_Register; 12324 break; 12325 case 'Z': 12326 weight = CW_Memory; 12327 break; 12328 } 12329 return weight; 12330 } 12331 12332 std::pair<unsigned, const TargetRegisterClass *> 12333 PPCTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 12334 StringRef Constraint, 12335 MVT VT) const { 12336 if (Constraint.size() == 1) { 12337 // GCC RS6000 Constraint Letters 12338 switch (Constraint[0]) { 12339 case 'b': // R1-R31 12340 if (VT == MVT::i64 && Subtarget.isPPC64()) 12341 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass); 12342 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass); 12343 case 'r': // R0-R31 12344 if (VT == MVT::i64 && Subtarget.isPPC64()) 12345 return std::make_pair(0U, &PPC::G8RCRegClass); 12346 return std::make_pair(0U, &PPC::GPRCRegClass); 12347 // 'd' and 'f' constraints are both defined to be "the floating point 12348 // registers", where one is for 32-bit and the other for 64-bit. We don't 12349 // really care overly much here so just give them all the same reg classes. 12350 case 'd': 12351 case 'f': 12352 if (VT == MVT::f32 || VT == MVT::i32) 12353 return std::make_pair(0U, &PPC::F4RCRegClass); 12354 if (VT == MVT::f64 || VT == MVT::i64) 12355 return std::make_pair(0U, &PPC::F8RCRegClass); 12356 if (VT == MVT::v4f64 && Subtarget.hasQPX()) 12357 return std::make_pair(0U, &PPC::QFRCRegClass); 12358 if (VT == MVT::v4f32 && Subtarget.hasQPX()) 12359 return std::make_pair(0U, &PPC::QSRCRegClass); 12360 break; 12361 case 'v': 12362 if (VT == MVT::v4f64 && Subtarget.hasQPX()) 12363 return std::make_pair(0U, &PPC::QFRCRegClass); 12364 if (VT == MVT::v4f32 && Subtarget.hasQPX()) 12365 return std::make_pair(0U, &PPC::QSRCRegClass); 12366 if (Subtarget.hasAltivec()) 12367 return std::make_pair(0U, &PPC::VRRCRegClass); 12368 case 'y': // crrc 12369 return std::make_pair(0U, &PPC::CRRCRegClass); 12370 } 12371 } else if (Constraint == "wc" && Subtarget.useCRBits()) { 12372 // An individual CR bit. 12373 return std::make_pair(0U, &PPC::CRBITRCRegClass); 12374 } else if ((Constraint == "wa" || Constraint == "wd" || 12375 Constraint == "wf") && Subtarget.hasVSX()) { 12376 return std::make_pair(0U, &PPC::VSRCRegClass); 12377 } else if (Constraint == "ws" && Subtarget.hasVSX()) { 12378 if (VT == MVT::f32 && Subtarget.hasP8Vector()) 12379 return std::make_pair(0U, &PPC::VSSRCRegClass); 12380 else 12381 return std::make_pair(0U, &PPC::VSFRCRegClass); 12382 } 12383 12384 std::pair<unsigned, const TargetRegisterClass *> R = 12385 TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 12386 12387 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers 12388 // (which we call X[0-9]+). If a 64-bit value has been requested, and a 12389 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent 12390 // register. 12391 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use 12392 // the AsmName field from *RegisterInfo.td, then this would not be necessary. 12393 if (R.first && VT == MVT::i64 && Subtarget.isPPC64() && 12394 PPC::GPRCRegClass.contains(R.first)) 12395 return std::make_pair(TRI->getMatchingSuperReg(R.first, 12396 PPC::sub_32, &PPC::G8RCRegClass), 12397 &PPC::G8RCRegClass); 12398 12399 // GCC accepts 'cc' as an alias for 'cr0', and we need to do the same. 12400 if (!R.second && StringRef("{cc}").equals_lower(Constraint)) { 12401 R.first = PPC::CR0; 12402 R.second = &PPC::CRRCRegClass; 12403 } 12404 12405 return R; 12406 } 12407 12408 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 12409 /// vector. If it is invalid, don't add anything to Ops. 12410 void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 12411 std::string &Constraint, 12412 std::vector<SDValue>&Ops, 12413 SelectionDAG &DAG) const { 12414 SDValue Result; 12415 12416 // Only support length 1 constraints. 12417 if (Constraint.length() > 1) return; 12418 12419 char Letter = Constraint[0]; 12420 switch (Letter) { 12421 default: break; 12422 case 'I': 12423 case 'J': 12424 case 'K': 12425 case 'L': 12426 case 'M': 12427 case 'N': 12428 case 'O': 12429 case 'P': { 12430 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op); 12431 if (!CST) return; // Must be an immediate to match. 12432 SDLoc dl(Op); 12433 int64_t Value = CST->getSExtValue(); 12434 EVT TCVT = MVT::i64; // All constants taken to be 64 bits so that negative 12435 // numbers are printed as such. 12436 switch (Letter) { 12437 default: llvm_unreachable("Unknown constraint letter!"); 12438 case 'I': // "I" is a signed 16-bit constant. 12439 if (isInt<16>(Value)) 12440 Result = DAG.getTargetConstant(Value, dl, TCVT); 12441 break; 12442 case 'J': // "J" is a constant with only the high-order 16 bits nonzero. 12443 if (isShiftedUInt<16, 16>(Value)) 12444 Result = DAG.getTargetConstant(Value, dl, TCVT); 12445 break; 12446 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits. 12447 if (isShiftedInt<16, 16>(Value)) 12448 Result = DAG.getTargetConstant(Value, dl, TCVT); 12449 break; 12450 case 'K': // "K" is a constant with only the low-order 16 bits nonzero. 12451 if (isUInt<16>(Value)) 12452 Result = DAG.getTargetConstant(Value, dl, TCVT); 12453 break; 12454 case 'M': // "M" is a constant that is greater than 31. 12455 if (Value > 31) 12456 Result = DAG.getTargetConstant(Value, dl, TCVT); 12457 break; 12458 case 'N': // "N" is a positive constant that is an exact power of two. 12459 if (Value > 0 && isPowerOf2_64(Value)) 12460 Result = DAG.getTargetConstant(Value, dl, TCVT); 12461 break; 12462 case 'O': // "O" is the constant zero. 12463 if (Value == 0) 12464 Result = DAG.getTargetConstant(Value, dl, TCVT); 12465 break; 12466 case 'P': // "P" is a constant whose negation is a signed 16-bit constant. 12467 if (isInt<16>(-Value)) 12468 Result = DAG.getTargetConstant(Value, dl, TCVT); 12469 break; 12470 } 12471 break; 12472 } 12473 } 12474 12475 if (Result.getNode()) { 12476 Ops.push_back(Result); 12477 return; 12478 } 12479 12480 // Handle standard constraint letters. 12481 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 12482 } 12483 12484 // isLegalAddressingMode - Return true if the addressing mode represented 12485 // by AM is legal for this target, for a load/store of the specified type. 12486 bool PPCTargetLowering::isLegalAddressingMode(const DataLayout &DL, 12487 const AddrMode &AM, Type *Ty, 12488 unsigned AS) const { 12489 // PPC does not allow r+i addressing modes for vectors! 12490 if (Ty->isVectorTy() && AM.BaseOffs != 0) 12491 return false; 12492 12493 // PPC allows a sign-extended 16-bit immediate field. 12494 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1) 12495 return false; 12496 12497 // No global is ever allowed as a base. 12498 if (AM.BaseGV) 12499 return false; 12500 12501 // PPC only support r+r, 12502 switch (AM.Scale) { 12503 case 0: // "r+i" or just "i", depending on HasBaseReg. 12504 break; 12505 case 1: 12506 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed. 12507 return false; 12508 // Otherwise we have r+r or r+i. 12509 break; 12510 case 2: 12511 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed. 12512 return false; 12513 // Allow 2*r as r+r. 12514 break; 12515 default: 12516 // No other scales are supported. 12517 return false; 12518 } 12519 12520 return true; 12521 } 12522 12523 SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op, 12524 SelectionDAG &DAG) const { 12525 MachineFunction &MF = DAG.getMachineFunction(); 12526 MachineFrameInfo &MFI = MF.getFrameInfo(); 12527 MFI.setReturnAddressIsTaken(true); 12528 12529 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 12530 return SDValue(); 12531 12532 SDLoc dl(Op); 12533 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 12534 12535 // Make sure the function does not optimize away the store of the RA to 12536 // the stack. 12537 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 12538 FuncInfo->setLRStoreRequired(); 12539 bool isPPC64 = Subtarget.isPPC64(); 12540 auto PtrVT = getPointerTy(MF.getDataLayout()); 12541 12542 if (Depth > 0) { 12543 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 12544 SDValue Offset = 12545 DAG.getConstant(Subtarget.getFrameLowering()->getReturnSaveOffset(), dl, 12546 isPPC64 ? MVT::i64 : MVT::i32); 12547 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), 12548 DAG.getNode(ISD::ADD, dl, PtrVT, FrameAddr, Offset), 12549 MachinePointerInfo()); 12550 } 12551 12552 // Just load the return address off the stack. 12553 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG); 12554 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), RetAddrFI, 12555 MachinePointerInfo()); 12556 } 12557 12558 SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op, 12559 SelectionDAG &DAG) const { 12560 SDLoc dl(Op); 12561 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 12562 12563 MachineFunction &MF = DAG.getMachineFunction(); 12564 MachineFrameInfo &MFI = MF.getFrameInfo(); 12565 MFI.setFrameAddressIsTaken(true); 12566 12567 EVT PtrVT = getPointerTy(MF.getDataLayout()); 12568 bool isPPC64 = PtrVT == MVT::i64; 12569 12570 // Naked functions never have a frame pointer, and so we use r1. For all 12571 // other functions, this decision must be delayed until during PEI. 12572 unsigned FrameReg; 12573 if (MF.getFunction()->hasFnAttribute(Attribute::Naked)) 12574 FrameReg = isPPC64 ? PPC::X1 : PPC::R1; 12575 else 12576 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP; 12577 12578 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, 12579 PtrVT); 12580 while (Depth--) 12581 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(), 12582 FrameAddr, MachinePointerInfo()); 12583 return FrameAddr; 12584 } 12585 12586 // FIXME? Maybe this could be a TableGen attribute on some registers and 12587 // this table could be generated automatically from RegInfo. 12588 unsigned PPCTargetLowering::getRegisterByName(const char* RegName, EVT VT, 12589 SelectionDAG &DAG) const { 12590 bool isPPC64 = Subtarget.isPPC64(); 12591 bool isDarwinABI = Subtarget.isDarwinABI(); 12592 12593 if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) || 12594 (!isPPC64 && VT != MVT::i32)) 12595 report_fatal_error("Invalid register global variable type"); 12596 12597 bool is64Bit = isPPC64 && VT == MVT::i64; 12598 unsigned Reg = StringSwitch<unsigned>(RegName) 12599 .Case("r1", is64Bit ? PPC::X1 : PPC::R1) 12600 .Case("r2", (isDarwinABI || isPPC64) ? 0 : PPC::R2) 12601 .Case("r13", (!isPPC64 && isDarwinABI) ? 0 : 12602 (is64Bit ? PPC::X13 : PPC::R13)) 12603 .Default(0); 12604 12605 if (Reg) 12606 return Reg; 12607 report_fatal_error("Invalid register name global variable"); 12608 } 12609 12610 bool 12611 PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 12612 // The PowerPC target isn't yet aware of offsets. 12613 return false; 12614 } 12615 12616 bool PPCTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 12617 const CallInst &I, 12618 unsigned Intrinsic) const { 12619 switch (Intrinsic) { 12620 case Intrinsic::ppc_qpx_qvlfd: 12621 case Intrinsic::ppc_qpx_qvlfs: 12622 case Intrinsic::ppc_qpx_qvlfcd: 12623 case Intrinsic::ppc_qpx_qvlfcs: 12624 case Intrinsic::ppc_qpx_qvlfiwa: 12625 case Intrinsic::ppc_qpx_qvlfiwz: 12626 case Intrinsic::ppc_altivec_lvx: 12627 case Intrinsic::ppc_altivec_lvxl: 12628 case Intrinsic::ppc_altivec_lvebx: 12629 case Intrinsic::ppc_altivec_lvehx: 12630 case Intrinsic::ppc_altivec_lvewx: 12631 case Intrinsic::ppc_vsx_lxvd2x: 12632 case Intrinsic::ppc_vsx_lxvw4x: { 12633 EVT VT; 12634 switch (Intrinsic) { 12635 case Intrinsic::ppc_altivec_lvebx: 12636 VT = MVT::i8; 12637 break; 12638 case Intrinsic::ppc_altivec_lvehx: 12639 VT = MVT::i16; 12640 break; 12641 case Intrinsic::ppc_altivec_lvewx: 12642 VT = MVT::i32; 12643 break; 12644 case Intrinsic::ppc_vsx_lxvd2x: 12645 VT = MVT::v2f64; 12646 break; 12647 case Intrinsic::ppc_qpx_qvlfd: 12648 VT = MVT::v4f64; 12649 break; 12650 case Intrinsic::ppc_qpx_qvlfs: 12651 VT = MVT::v4f32; 12652 break; 12653 case Intrinsic::ppc_qpx_qvlfcd: 12654 VT = MVT::v2f64; 12655 break; 12656 case Intrinsic::ppc_qpx_qvlfcs: 12657 VT = MVT::v2f32; 12658 break; 12659 default: 12660 VT = MVT::v4i32; 12661 break; 12662 } 12663 12664 Info.opc = ISD::INTRINSIC_W_CHAIN; 12665 Info.memVT = VT; 12666 Info.ptrVal = I.getArgOperand(0); 12667 Info.offset = -VT.getStoreSize()+1; 12668 Info.size = 2*VT.getStoreSize()-1; 12669 Info.align = 1; 12670 Info.vol = false; 12671 Info.readMem = true; 12672 Info.writeMem = false; 12673 return true; 12674 } 12675 case Intrinsic::ppc_qpx_qvlfda: 12676 case Intrinsic::ppc_qpx_qvlfsa: 12677 case Intrinsic::ppc_qpx_qvlfcda: 12678 case Intrinsic::ppc_qpx_qvlfcsa: 12679 case Intrinsic::ppc_qpx_qvlfiwaa: 12680 case Intrinsic::ppc_qpx_qvlfiwza: { 12681 EVT VT; 12682 switch (Intrinsic) { 12683 case Intrinsic::ppc_qpx_qvlfda: 12684 VT = MVT::v4f64; 12685 break; 12686 case Intrinsic::ppc_qpx_qvlfsa: 12687 VT = MVT::v4f32; 12688 break; 12689 case Intrinsic::ppc_qpx_qvlfcda: 12690 VT = MVT::v2f64; 12691 break; 12692 case Intrinsic::ppc_qpx_qvlfcsa: 12693 VT = MVT::v2f32; 12694 break; 12695 default: 12696 VT = MVT::v4i32; 12697 break; 12698 } 12699 12700 Info.opc = ISD::INTRINSIC_W_CHAIN; 12701 Info.memVT = VT; 12702 Info.ptrVal = I.getArgOperand(0); 12703 Info.offset = 0; 12704 Info.size = VT.getStoreSize(); 12705 Info.align = 1; 12706 Info.vol = false; 12707 Info.readMem = true; 12708 Info.writeMem = false; 12709 return true; 12710 } 12711 case Intrinsic::ppc_qpx_qvstfd: 12712 case Intrinsic::ppc_qpx_qvstfs: 12713 case Intrinsic::ppc_qpx_qvstfcd: 12714 case Intrinsic::ppc_qpx_qvstfcs: 12715 case Intrinsic::ppc_qpx_qvstfiw: 12716 case Intrinsic::ppc_altivec_stvx: 12717 case Intrinsic::ppc_altivec_stvxl: 12718 case Intrinsic::ppc_altivec_stvebx: 12719 case Intrinsic::ppc_altivec_stvehx: 12720 case Intrinsic::ppc_altivec_stvewx: 12721 case Intrinsic::ppc_vsx_stxvd2x: 12722 case Intrinsic::ppc_vsx_stxvw4x: { 12723 EVT VT; 12724 switch (Intrinsic) { 12725 case Intrinsic::ppc_altivec_stvebx: 12726 VT = MVT::i8; 12727 break; 12728 case Intrinsic::ppc_altivec_stvehx: 12729 VT = MVT::i16; 12730 break; 12731 case Intrinsic::ppc_altivec_stvewx: 12732 VT = MVT::i32; 12733 break; 12734 case Intrinsic::ppc_vsx_stxvd2x: 12735 VT = MVT::v2f64; 12736 break; 12737 case Intrinsic::ppc_qpx_qvstfd: 12738 VT = MVT::v4f64; 12739 break; 12740 case Intrinsic::ppc_qpx_qvstfs: 12741 VT = MVT::v4f32; 12742 break; 12743 case Intrinsic::ppc_qpx_qvstfcd: 12744 VT = MVT::v2f64; 12745 break; 12746 case Intrinsic::ppc_qpx_qvstfcs: 12747 VT = MVT::v2f32; 12748 break; 12749 default: 12750 VT = MVT::v4i32; 12751 break; 12752 } 12753 12754 Info.opc = ISD::INTRINSIC_VOID; 12755 Info.memVT = VT; 12756 Info.ptrVal = I.getArgOperand(1); 12757 Info.offset = -VT.getStoreSize()+1; 12758 Info.size = 2*VT.getStoreSize()-1; 12759 Info.align = 1; 12760 Info.vol = false; 12761 Info.readMem = false; 12762 Info.writeMem = true; 12763 return true; 12764 } 12765 case Intrinsic::ppc_qpx_qvstfda: 12766 case Intrinsic::ppc_qpx_qvstfsa: 12767 case Intrinsic::ppc_qpx_qvstfcda: 12768 case Intrinsic::ppc_qpx_qvstfcsa: 12769 case Intrinsic::ppc_qpx_qvstfiwa: { 12770 EVT VT; 12771 switch (Intrinsic) { 12772 case Intrinsic::ppc_qpx_qvstfda: 12773 VT = MVT::v4f64; 12774 break; 12775 case Intrinsic::ppc_qpx_qvstfsa: 12776 VT = MVT::v4f32; 12777 break; 12778 case Intrinsic::ppc_qpx_qvstfcda: 12779 VT = MVT::v2f64; 12780 break; 12781 case Intrinsic::ppc_qpx_qvstfcsa: 12782 VT = MVT::v2f32; 12783 break; 12784 default: 12785 VT = MVT::v4i32; 12786 break; 12787 } 12788 12789 Info.opc = ISD::INTRINSIC_VOID; 12790 Info.memVT = VT; 12791 Info.ptrVal = I.getArgOperand(1); 12792 Info.offset = 0; 12793 Info.size = VT.getStoreSize(); 12794 Info.align = 1; 12795 Info.vol = false; 12796 Info.readMem = false; 12797 Info.writeMem = true; 12798 return true; 12799 } 12800 default: 12801 break; 12802 } 12803 12804 return false; 12805 } 12806 12807 /// getOptimalMemOpType - Returns the target specific optimal type for load 12808 /// and store operations as a result of memset, memcpy, and memmove 12809 /// lowering. If DstAlign is zero that means it's safe to destination 12810 /// alignment can satisfy any constraint. Similarly if SrcAlign is zero it 12811 /// means there isn't a need to check it against alignment requirement, 12812 /// probably because the source does not need to be loaded. If 'IsMemset' is 12813 /// true, that means it's expanding a memset. If 'ZeroMemset' is true, that 12814 /// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy 12815 /// source is constant so it does not need to be loaded. 12816 /// It returns EVT::Other if the type should be determined using generic 12817 /// target-independent logic. 12818 EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size, 12819 unsigned DstAlign, unsigned SrcAlign, 12820 bool IsMemset, bool ZeroMemset, 12821 bool MemcpyStrSrc, 12822 MachineFunction &MF) const { 12823 if (getTargetMachine().getOptLevel() != CodeGenOpt::None) { 12824 const Function *F = MF.getFunction(); 12825 // When expanding a memset, require at least two QPX instructions to cover 12826 // the cost of loading the value to be stored from the constant pool. 12827 if (Subtarget.hasQPX() && Size >= 32 && (!IsMemset || Size >= 64) && 12828 (!SrcAlign || SrcAlign >= 32) && (!DstAlign || DstAlign >= 32) && 12829 !F->hasFnAttribute(Attribute::NoImplicitFloat)) { 12830 return MVT::v4f64; 12831 } 12832 12833 // We should use Altivec/VSX loads and stores when available. For unaligned 12834 // addresses, unaligned VSX loads are only fast starting with the P8. 12835 if (Subtarget.hasAltivec() && Size >= 16 && 12836 (((!SrcAlign || SrcAlign >= 16) && (!DstAlign || DstAlign >= 16)) || 12837 ((IsMemset && Subtarget.hasVSX()) || Subtarget.hasP8Vector()))) 12838 return MVT::v4i32; 12839 } 12840 12841 if (Subtarget.isPPC64()) { 12842 return MVT::i64; 12843 } 12844 12845 return MVT::i32; 12846 } 12847 12848 /// \brief Returns true if it is beneficial to convert a load of a constant 12849 /// to just the constant itself. 12850 bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 12851 Type *Ty) const { 12852 assert(Ty->isIntegerTy()); 12853 12854 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 12855 return !(BitSize == 0 || BitSize > 64); 12856 } 12857 12858 bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const { 12859 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 12860 return false; 12861 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits(); 12862 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits(); 12863 return NumBits1 == 64 && NumBits2 == 32; 12864 } 12865 12866 bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const { 12867 if (!VT1.isInteger() || !VT2.isInteger()) 12868 return false; 12869 unsigned NumBits1 = VT1.getSizeInBits(); 12870 unsigned NumBits2 = VT2.getSizeInBits(); 12871 return NumBits1 == 64 && NumBits2 == 32; 12872 } 12873 12874 bool PPCTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 12875 // Generally speaking, zexts are not free, but they are free when they can be 12876 // folded with other operations. 12877 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Val)) { 12878 EVT MemVT = LD->getMemoryVT(); 12879 if ((MemVT == MVT::i1 || MemVT == MVT::i8 || MemVT == MVT::i16 || 12880 (Subtarget.isPPC64() && MemVT == MVT::i32)) && 12881 (LD->getExtensionType() == ISD::NON_EXTLOAD || 12882 LD->getExtensionType() == ISD::ZEXTLOAD)) 12883 return true; 12884 } 12885 12886 // FIXME: Add other cases... 12887 // - 32-bit shifts with a zext to i64 12888 // - zext after ctlz, bswap, etc. 12889 // - zext after and by a constant mask 12890 12891 return TargetLowering::isZExtFree(Val, VT2); 12892 } 12893 12894 bool PPCTargetLowering::isFPExtFree(EVT VT) const { 12895 assert(VT.isFloatingPoint()); 12896 return true; 12897 } 12898 12899 bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 12900 return isInt<16>(Imm) || isUInt<16>(Imm); 12901 } 12902 12903 bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const { 12904 return isInt<16>(Imm) || isUInt<16>(Imm); 12905 } 12906 12907 bool PPCTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 12908 unsigned, 12909 unsigned, 12910 bool *Fast) const { 12911 if (DisablePPCUnaligned) 12912 return false; 12913 12914 // PowerPC supports unaligned memory access for simple non-vector types. 12915 // Although accessing unaligned addresses is not as efficient as accessing 12916 // aligned addresses, it is generally more efficient than manual expansion, 12917 // and generally only traps for software emulation when crossing page 12918 // boundaries. 12919 12920 if (!VT.isSimple()) 12921 return false; 12922 12923 if (VT.getSimpleVT().isVector()) { 12924 if (Subtarget.hasVSX()) { 12925 if (VT != MVT::v2f64 && VT != MVT::v2i64 && 12926 VT != MVT::v4f32 && VT != MVT::v4i32) 12927 return false; 12928 } else { 12929 return false; 12930 } 12931 } 12932 12933 if (VT == MVT::ppcf128) 12934 return false; 12935 12936 if (Fast) 12937 *Fast = true; 12938 12939 return true; 12940 } 12941 12942 bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { 12943 VT = VT.getScalarType(); 12944 12945 if (!VT.isSimple()) 12946 return false; 12947 12948 switch (VT.getSimpleVT().SimpleTy) { 12949 case MVT::f32: 12950 case MVT::f64: 12951 return true; 12952 default: 12953 break; 12954 } 12955 12956 return false; 12957 } 12958 12959 const MCPhysReg * 12960 PPCTargetLowering::getScratchRegisters(CallingConv::ID) const { 12961 // LR is a callee-save register, but we must treat it as clobbered by any call 12962 // site. Hence we include LR in the scratch registers, which are in turn added 12963 // as implicit-defs for stackmaps and patchpoints. The same reasoning applies 12964 // to CTR, which is used by any indirect call. 12965 static const MCPhysReg ScratchRegs[] = { 12966 PPC::X12, PPC::LR8, PPC::CTR8, 0 12967 }; 12968 12969 return ScratchRegs; 12970 } 12971 12972 unsigned PPCTargetLowering::getExceptionPointerRegister( 12973 const Constant *PersonalityFn) const { 12974 return Subtarget.isPPC64() ? PPC::X3 : PPC::R3; 12975 } 12976 12977 unsigned PPCTargetLowering::getExceptionSelectorRegister( 12978 const Constant *PersonalityFn) const { 12979 return Subtarget.isPPC64() ? PPC::X4 : PPC::R4; 12980 } 12981 12982 bool 12983 PPCTargetLowering::shouldExpandBuildVectorWithShuffles( 12984 EVT VT , unsigned DefinedValues) const { 12985 if (VT == MVT::v2i64) 12986 return Subtarget.hasDirectMove(); // Don't need stack ops with direct moves 12987 12988 if (Subtarget.hasVSX() || Subtarget.hasQPX()) 12989 return true; 12990 12991 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues); 12992 } 12993 12994 Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const { 12995 if (DisableILPPref || Subtarget.enableMachineScheduler()) 12996 return TargetLowering::getSchedulingPreference(N); 12997 12998 return Sched::ILP; 12999 } 13000 13001 // Create a fast isel object. 13002 FastISel * 13003 PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo, 13004 const TargetLibraryInfo *LibInfo) const { 13005 return PPC::createFastISel(FuncInfo, LibInfo); 13006 } 13007 13008 void PPCTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const { 13009 if (Subtarget.isDarwinABI()) return; 13010 if (!Subtarget.isPPC64()) return; 13011 13012 // Update IsSplitCSR in PPCFunctionInfo 13013 PPCFunctionInfo *PFI = Entry->getParent()->getInfo<PPCFunctionInfo>(); 13014 PFI->setIsSplitCSR(true); 13015 } 13016 13017 void PPCTargetLowering::insertCopiesSplitCSR( 13018 MachineBasicBlock *Entry, 13019 const SmallVectorImpl<MachineBasicBlock *> &Exits) const { 13020 const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo(); 13021 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent()); 13022 if (!IStart) 13023 return; 13024 13025 const TargetInstrInfo *TII = Subtarget.getInstrInfo(); 13026 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo(); 13027 MachineBasicBlock::iterator MBBI = Entry->begin(); 13028 for (const MCPhysReg *I = IStart; *I; ++I) { 13029 const TargetRegisterClass *RC = nullptr; 13030 if (PPC::G8RCRegClass.contains(*I)) 13031 RC = &PPC::G8RCRegClass; 13032 else if (PPC::F8RCRegClass.contains(*I)) 13033 RC = &PPC::F8RCRegClass; 13034 else if (PPC::CRRCRegClass.contains(*I)) 13035 RC = &PPC::CRRCRegClass; 13036 else if (PPC::VRRCRegClass.contains(*I)) 13037 RC = &PPC::VRRCRegClass; 13038 else 13039 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 13040 13041 unsigned NewVR = MRI->createVirtualRegister(RC); 13042 // Create copy from CSR to a virtual register. 13043 // FIXME: this currently does not emit CFI pseudo-instructions, it works 13044 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be 13045 // nounwind. If we want to generalize this later, we may need to emit 13046 // CFI pseudo-instructions. 13047 assert(Entry->getParent()->getFunction()->hasFnAttribute( 13048 Attribute::NoUnwind) && 13049 "Function should be nounwind in insertCopiesSplitCSR!"); 13050 Entry->addLiveIn(*I); 13051 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR) 13052 .addReg(*I); 13053 13054 // Insert the copy-back instructions right before the terminator 13055 for (auto *Exit : Exits) 13056 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(), 13057 TII->get(TargetOpcode::COPY), *I) 13058 .addReg(NewVR); 13059 } 13060 } 13061 13062 // Override to enable LOAD_STACK_GUARD lowering on Linux. 13063 bool PPCTargetLowering::useLoadStackGuardNode() const { 13064 if (!Subtarget.isTargetLinux()) 13065 return TargetLowering::useLoadStackGuardNode(); 13066 return true; 13067 } 13068 13069 // Override to disable global variable loading on Linux. 13070 void PPCTargetLowering::insertSSPDeclarations(Module &M) const { 13071 if (!Subtarget.isTargetLinux()) 13072 return TargetLowering::insertSSPDeclarations(M); 13073 } 13074 13075 bool PPCTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 13076 if (!VT.isSimple() || !Subtarget.hasVSX()) 13077 return false; 13078 13079 switch(VT.getSimpleVT().SimpleTy) { 13080 default: 13081 // For FP types that are currently not supported by PPC backend, return 13082 // false. Examples: f16, f80. 13083 return false; 13084 case MVT::f32: 13085 case MVT::f64: 13086 case MVT::ppcf128: 13087 return Imm.isPosZero(); 13088 } 13089 } 13090 13091 // For vector shift operation op, fold 13092 // (op x, (and y, ((1 << numbits(x)) - 1))) -> (target op x, y) 13093 static SDValue stripModuloOnShift(const TargetLowering &TLI, SDNode *N, 13094 SelectionDAG &DAG) { 13095 SDValue N0 = N->getOperand(0); 13096 SDValue N1 = N->getOperand(1); 13097 EVT VT = N0.getValueType(); 13098 unsigned OpSizeInBits = VT.getScalarSizeInBits(); 13099 unsigned Opcode = N->getOpcode(); 13100 unsigned TargetOpcode; 13101 13102 switch (Opcode) { 13103 default: 13104 llvm_unreachable("Unexpected shift operation"); 13105 case ISD::SHL: 13106 TargetOpcode = PPCISD::SHL; 13107 break; 13108 case ISD::SRL: 13109 TargetOpcode = PPCISD::SRL; 13110 break; 13111 case ISD::SRA: 13112 TargetOpcode = PPCISD::SRA; 13113 break; 13114 } 13115 13116 if (VT.isVector() && TLI.isOperationLegal(Opcode, VT) && 13117 N1->getOpcode() == ISD::AND) 13118 if (ConstantSDNode *Mask = isConstOrConstSplat(N1->getOperand(1))) 13119 if (Mask->getZExtValue() == OpSizeInBits - 1) 13120 return DAG.getNode(TargetOpcode, SDLoc(N), VT, N0, N1->getOperand(0)); 13121 13122 return SDValue(); 13123 } 13124 13125 SDValue PPCTargetLowering::combineSHL(SDNode *N, DAGCombinerInfo &DCI) const { 13126 if (auto Value = stripModuloOnShift(*this, N, DCI.DAG)) 13127 return Value; 13128 13129 return SDValue(); 13130 } 13131 13132 SDValue PPCTargetLowering::combineSRA(SDNode *N, DAGCombinerInfo &DCI) const { 13133 if (auto Value = stripModuloOnShift(*this, N, DCI.DAG)) 13134 return Value; 13135 13136 return SDValue(); 13137 } 13138 13139 SDValue PPCTargetLowering::combineSRL(SDNode *N, DAGCombinerInfo &DCI) const { 13140 if (auto Value = stripModuloOnShift(*this, N, DCI.DAG)) 13141 return Value; 13142 13143 return SDValue(); 13144 } 13145