1 //===-- SystemZISelLowering.cpp - SystemZ 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 SystemZTargetLowering class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "SystemZISelLowering.h" 15 #include "SystemZCallingConv.h" 16 #include "SystemZConstantPoolValue.h" 17 #include "SystemZMachineFunctionInfo.h" 18 #include "SystemZTargetMachine.h" 19 #include "llvm/CodeGen/CallingConvLower.h" 20 #include "llvm/CodeGen/MachineInstrBuilder.h" 21 #include "llvm/CodeGen/MachineRegisterInfo.h" 22 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 23 #include "llvm/IR/Intrinsics.h" 24 #include <cctype> 25 26 using namespace llvm; 27 28 #define DEBUG_TYPE "systemz-lower" 29 30 namespace { 31 // Represents a sequence for extracting a 0/1 value from an IPM result: 32 // (((X ^ XORValue) + AddValue) >> Bit) 33 struct IPMConversion { 34 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit) 35 : XORValue(xorValue), AddValue(addValue), Bit(bit) {} 36 37 int64_t XORValue; 38 int64_t AddValue; 39 unsigned Bit; 40 }; 41 42 // Represents information about a comparison. 43 struct Comparison { 44 Comparison(SDValue Op0In, SDValue Op1In) 45 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {} 46 47 // The operands to the comparison. 48 SDValue Op0, Op1; 49 50 // The opcode that should be used to compare Op0 and Op1. 51 unsigned Opcode; 52 53 // A SystemZICMP value. Only used for integer comparisons. 54 unsigned ICmpType; 55 56 // The mask of CC values that Opcode can produce. 57 unsigned CCValid; 58 59 // The mask of CC values for which the original condition is true. 60 unsigned CCMask; 61 }; 62 } // end anonymous namespace 63 64 // Classify VT as either 32 or 64 bit. 65 static bool is32Bit(EVT VT) { 66 switch (VT.getSimpleVT().SimpleTy) { 67 case MVT::i32: 68 return true; 69 case MVT::i64: 70 return false; 71 default: 72 llvm_unreachable("Unsupported type"); 73 } 74 } 75 76 // Return a version of MachineOperand that can be safely used before the 77 // final use. 78 static MachineOperand earlyUseOperand(MachineOperand Op) { 79 if (Op.isReg()) 80 Op.setIsKill(false); 81 return Op; 82 } 83 84 SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM, 85 const SystemZSubtarget &STI) 86 : TargetLowering(TM), Subtarget(STI) { 87 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize()); 88 89 // Set up the register classes. 90 if (Subtarget.hasHighWord()) 91 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass); 92 else 93 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass); 94 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass); 95 if (Subtarget.hasVector()) { 96 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass); 97 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass); 98 } else { 99 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass); 100 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass); 101 } 102 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass); 103 104 if (Subtarget.hasVector()) { 105 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass); 106 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass); 107 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass); 108 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass); 109 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass); 110 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass); 111 } 112 113 // Compute derived properties from the register classes 114 computeRegisterProperties(Subtarget.getRegisterInfo()); 115 116 // Set up special registers. 117 setStackPointerRegisterToSaveRestore(SystemZ::R15D); 118 119 // TODO: It may be better to default to latency-oriented scheduling, however 120 // LLVM's current latency-oriented scheduler can't handle physreg definitions 121 // such as SystemZ has with CC, so set this to the register-pressure 122 // scheduler, because it can. 123 setSchedulingPreference(Sched::RegPressure); 124 125 setBooleanContents(ZeroOrOneBooleanContent); 126 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 127 128 // Instructions are strings of 2-byte aligned 2-byte values. 129 setMinFunctionAlignment(2); 130 131 // Handle operations that are handled in a similar way for all types. 132 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE; 133 I <= MVT::LAST_FP_VALUETYPE; 134 ++I) { 135 MVT VT = MVT::SimpleValueType(I); 136 if (isTypeLegal(VT)) { 137 // Lower SET_CC into an IPM-based sequence. 138 setOperationAction(ISD::SETCC, VT, Custom); 139 140 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE). 141 setOperationAction(ISD::SELECT, VT, Expand); 142 143 // Lower SELECT_CC and BR_CC into separate comparisons and branches. 144 setOperationAction(ISD::SELECT_CC, VT, Custom); 145 setOperationAction(ISD::BR_CC, VT, Custom); 146 } 147 } 148 149 // Expand jump table branches as address arithmetic followed by an 150 // indirect jump. 151 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 152 153 // Expand BRCOND into a BR_CC (see above). 154 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 155 156 // Handle integer types. 157 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE; 158 I <= MVT::LAST_INTEGER_VALUETYPE; 159 ++I) { 160 MVT VT = MVT::SimpleValueType(I); 161 if (isTypeLegal(VT)) { 162 // Expand individual DIV and REMs into DIVREMs. 163 setOperationAction(ISD::SDIV, VT, Expand); 164 setOperationAction(ISD::UDIV, VT, Expand); 165 setOperationAction(ISD::SREM, VT, Expand); 166 setOperationAction(ISD::UREM, VT, Expand); 167 setOperationAction(ISD::SDIVREM, VT, Custom); 168 setOperationAction(ISD::UDIVREM, VT, Custom); 169 170 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and 171 // stores, putting a serialization instruction after the stores. 172 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom); 173 setOperationAction(ISD::ATOMIC_STORE, VT, Custom); 174 175 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are 176 // available, or if the operand is constant. 177 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom); 178 179 // Use POPCNT on z196 and above. 180 if (Subtarget.hasPopulationCount()) 181 setOperationAction(ISD::CTPOP, VT, Custom); 182 else 183 setOperationAction(ISD::CTPOP, VT, Expand); 184 185 // No special instructions for these. 186 setOperationAction(ISD::CTTZ, VT, Expand); 187 setOperationAction(ISD::ROTR, VT, Expand); 188 189 // Use *MUL_LOHI where possible instead of MULH*. 190 setOperationAction(ISD::MULHS, VT, Expand); 191 setOperationAction(ISD::MULHU, VT, Expand); 192 setOperationAction(ISD::SMUL_LOHI, VT, Custom); 193 setOperationAction(ISD::UMUL_LOHI, VT, Custom); 194 195 // Only z196 and above have native support for conversions to unsigned. 196 if (!Subtarget.hasFPExtension()) 197 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 198 } 199 } 200 201 // Type legalization will convert 8- and 16-bit atomic operations into 202 // forms that operate on i32s (but still keeping the original memory VT). 203 // Lower them into full i32 operations. 204 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom); 205 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom); 206 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom); 207 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom); 208 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom); 209 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom); 210 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom); 211 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom); 212 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom); 213 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom); 214 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom); 215 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); 216 217 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom); 218 219 // Traps are legal, as we will convert them to "j .+2". 220 setOperationAction(ISD::TRAP, MVT::Other, Legal); 221 222 // z10 has instructions for signed but not unsigned FP conversion. 223 // Handle unsigned 32-bit types as signed 64-bit types. 224 if (!Subtarget.hasFPExtension()) { 225 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote); 226 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); 227 } 228 229 // We have native support for a 64-bit CTLZ, via FLOGR. 230 setOperationAction(ISD::CTLZ, MVT::i32, Promote); 231 setOperationAction(ISD::CTLZ, MVT::i64, Legal); 232 233 // Give LowerOperation the chance to replace 64-bit ORs with subregs. 234 setOperationAction(ISD::OR, MVT::i64, Custom); 235 236 // FIXME: Can we support these natively? 237 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand); 238 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand); 239 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand); 240 241 // We have native instructions for i8, i16 and i32 extensions, but not i1. 242 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 243 for (MVT VT : MVT::integer_valuetypes()) { 244 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 245 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); 246 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote); 247 } 248 249 // Handle the various types of symbolic address. 250 setOperationAction(ISD::ConstantPool, PtrVT, Custom); 251 setOperationAction(ISD::GlobalAddress, PtrVT, Custom); 252 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom); 253 setOperationAction(ISD::BlockAddress, PtrVT, Custom); 254 setOperationAction(ISD::JumpTable, PtrVT, Custom); 255 256 // We need to handle dynamic allocations specially because of the 257 // 160-byte area at the bottom of the stack. 258 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom); 259 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, PtrVT, Custom); 260 261 // Use custom expanders so that we can force the function to use 262 // a frame pointer. 263 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom); 264 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom); 265 266 // Handle prefetches with PFD or PFDRL. 267 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 268 269 for (MVT VT : MVT::vector_valuetypes()) { 270 // Assume by default that all vector operations need to be expanded. 271 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode) 272 if (getOperationAction(Opcode, VT) == Legal) 273 setOperationAction(Opcode, VT, Expand); 274 275 // Likewise all truncating stores and extending loads. 276 for (MVT InnerVT : MVT::vector_valuetypes()) { 277 setTruncStoreAction(VT, InnerVT, Expand); 278 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand); 279 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand); 280 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand); 281 } 282 283 if (isTypeLegal(VT)) { 284 // These operations are legal for anything that can be stored in a 285 // vector register, even if there is no native support for the format 286 // as such. In particular, we can do these for v4f32 even though there 287 // are no specific instructions for that format. 288 setOperationAction(ISD::LOAD, VT, Legal); 289 setOperationAction(ISD::STORE, VT, Legal); 290 setOperationAction(ISD::VSELECT, VT, Legal); 291 setOperationAction(ISD::BITCAST, VT, Legal); 292 setOperationAction(ISD::UNDEF, VT, Legal); 293 294 // Likewise, except that we need to replace the nodes with something 295 // more specific. 296 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 297 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 298 } 299 } 300 301 // Handle integer vector types. 302 for (MVT VT : MVT::integer_vector_valuetypes()) { 303 if (isTypeLegal(VT)) { 304 // These operations have direct equivalents. 305 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal); 306 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal); 307 setOperationAction(ISD::ADD, VT, Legal); 308 setOperationAction(ISD::SUB, VT, Legal); 309 if (VT != MVT::v2i64) 310 setOperationAction(ISD::MUL, VT, Legal); 311 setOperationAction(ISD::AND, VT, Legal); 312 setOperationAction(ISD::OR, VT, Legal); 313 setOperationAction(ISD::XOR, VT, Legal); 314 setOperationAction(ISD::CTPOP, VT, Custom); 315 setOperationAction(ISD::CTTZ, VT, Legal); 316 setOperationAction(ISD::CTLZ, VT, Legal); 317 318 // Convert a GPR scalar to a vector by inserting it into element 0. 319 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom); 320 321 // Use a series of unpacks for extensions. 322 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom); 323 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom); 324 325 // Detect shifts by a scalar amount and convert them into 326 // V*_BY_SCALAR. 327 setOperationAction(ISD::SHL, VT, Custom); 328 setOperationAction(ISD::SRA, VT, Custom); 329 setOperationAction(ISD::SRL, VT, Custom); 330 331 // At present ROTL isn't matched by DAGCombiner. ROTR should be 332 // converted into ROTL. 333 setOperationAction(ISD::ROTL, VT, Expand); 334 setOperationAction(ISD::ROTR, VT, Expand); 335 336 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands 337 // and inverting the result as necessary. 338 setOperationAction(ISD::SETCC, VT, Custom); 339 } 340 } 341 342 if (Subtarget.hasVector()) { 343 // There should be no need to check for float types other than v2f64 344 // since <2 x f32> isn't a legal type. 345 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal); 346 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal); 347 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal); 348 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal); 349 } 350 351 // Handle floating-point types. 352 for (unsigned I = MVT::FIRST_FP_VALUETYPE; 353 I <= MVT::LAST_FP_VALUETYPE; 354 ++I) { 355 MVT VT = MVT::SimpleValueType(I); 356 if (isTypeLegal(VT)) { 357 // We can use FI for FRINT. 358 setOperationAction(ISD::FRINT, VT, Legal); 359 360 // We can use the extended form of FI for other rounding operations. 361 if (Subtarget.hasFPExtension()) { 362 setOperationAction(ISD::FNEARBYINT, VT, Legal); 363 setOperationAction(ISD::FFLOOR, VT, Legal); 364 setOperationAction(ISD::FCEIL, VT, Legal); 365 setOperationAction(ISD::FTRUNC, VT, Legal); 366 setOperationAction(ISD::FROUND, VT, Legal); 367 } 368 369 // No special instructions for these. 370 setOperationAction(ISD::FSIN, VT, Expand); 371 setOperationAction(ISD::FCOS, VT, Expand); 372 setOperationAction(ISD::FSINCOS, VT, Expand); 373 setOperationAction(ISD::FREM, VT, Expand); 374 setOperationAction(ISD::FPOW, VT, Expand); 375 } 376 } 377 378 // Handle floating-point vector types. 379 if (Subtarget.hasVector()) { 380 // Scalar-to-vector conversion is just a subreg. 381 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal); 382 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal); 383 384 // Some insertions and extractions can be done directly but others 385 // need to go via integers. 386 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); 387 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom); 388 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); 389 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); 390 391 // These operations have direct equivalents. 392 setOperationAction(ISD::FADD, MVT::v2f64, Legal); 393 setOperationAction(ISD::FNEG, MVT::v2f64, Legal); 394 setOperationAction(ISD::FSUB, MVT::v2f64, Legal); 395 setOperationAction(ISD::FMUL, MVT::v2f64, Legal); 396 setOperationAction(ISD::FMA, MVT::v2f64, Legal); 397 setOperationAction(ISD::FDIV, MVT::v2f64, Legal); 398 setOperationAction(ISD::FABS, MVT::v2f64, Legal); 399 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); 400 setOperationAction(ISD::FRINT, MVT::v2f64, Legal); 401 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal); 402 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal); 403 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal); 404 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal); 405 setOperationAction(ISD::FROUND, MVT::v2f64, Legal); 406 } 407 408 // We have fused multiply-addition for f32 and f64 but not f128. 409 setOperationAction(ISD::FMA, MVT::f32, Legal); 410 setOperationAction(ISD::FMA, MVT::f64, Legal); 411 setOperationAction(ISD::FMA, MVT::f128, Expand); 412 413 // Needed so that we don't try to implement f128 constant loads using 414 // a load-and-extend of a f80 constant (in cases where the constant 415 // would fit in an f80). 416 for (MVT VT : MVT::fp_valuetypes()) 417 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand); 418 419 // Floating-point truncation and stores need to be done separately. 420 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 421 setTruncStoreAction(MVT::f128, MVT::f32, Expand); 422 setTruncStoreAction(MVT::f128, MVT::f64, Expand); 423 424 // We have 64-bit FPR<->GPR moves, but need special handling for 425 // 32-bit forms. 426 if (!Subtarget.hasVector()) { 427 setOperationAction(ISD::BITCAST, MVT::i32, Custom); 428 setOperationAction(ISD::BITCAST, MVT::f32, Custom); 429 } 430 431 // VASTART and VACOPY need to deal with the SystemZ-specific varargs 432 // structure, but VAEND is a no-op. 433 setOperationAction(ISD::VASTART, MVT::Other, Custom); 434 setOperationAction(ISD::VACOPY, MVT::Other, Custom); 435 setOperationAction(ISD::VAEND, MVT::Other, Expand); 436 437 // Codes for which we want to perform some z-specific combinations. 438 setTargetDAGCombine(ISD::SIGN_EXTEND); 439 setTargetDAGCombine(ISD::STORE); 440 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT); 441 setTargetDAGCombine(ISD::FP_ROUND); 442 setTargetDAGCombine(ISD::BSWAP); 443 444 // Handle intrinsics. 445 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom); 446 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 447 448 // We want to use MVC in preference to even a single load/store pair. 449 MaxStoresPerMemcpy = 0; 450 MaxStoresPerMemcpyOptSize = 0; 451 452 // The main memset sequence is a byte store followed by an MVC. 453 // Two STC or MV..I stores win over that, but the kind of fused stores 454 // generated by target-independent code don't when the byte value is 455 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better 456 // than "STC;MVC". Handle the choice in target-specific code instead. 457 MaxStoresPerMemset = 0; 458 MaxStoresPerMemsetOptSize = 0; 459 } 460 461 EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL, 462 LLVMContext &, EVT VT) const { 463 if (!VT.isVector()) 464 return MVT::i32; 465 return VT.changeVectorElementTypeToInteger(); 466 } 467 468 bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { 469 VT = VT.getScalarType(); 470 471 if (!VT.isSimple()) 472 return false; 473 474 switch (VT.getSimpleVT().SimpleTy) { 475 case MVT::f32: 476 case MVT::f64: 477 return true; 478 case MVT::f128: 479 return false; 480 default: 481 break; 482 } 483 484 return false; 485 } 486 487 bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 488 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR. 489 return Imm.isZero() || Imm.isNegZero(); 490 } 491 492 bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 493 // We can use CGFI or CLGFI. 494 return isInt<32>(Imm) || isUInt<32>(Imm); 495 } 496 497 bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const { 498 // We can use ALGFI or SLGFI. 499 return isUInt<32>(Imm) || isUInt<32>(-Imm); 500 } 501 502 bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 503 unsigned, 504 unsigned, 505 bool *Fast) const { 506 // Unaligned accesses should never be slower than the expanded version. 507 // We check specifically for aligned accesses in the few cases where 508 // they are required. 509 if (Fast) 510 *Fast = true; 511 return true; 512 } 513 514 bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL, 515 const AddrMode &AM, Type *Ty, 516 unsigned AS) const { 517 // Punt on globals for now, although they can be used in limited 518 // RELATIVE LONG cases. 519 if (AM.BaseGV) 520 return false; 521 522 // Require a 20-bit signed offset. 523 if (!isInt<20>(AM.BaseOffs)) 524 return false; 525 526 // Indexing is OK but no scale factor can be applied. 527 return AM.Scale == 0 || AM.Scale == 1; 528 } 529 530 bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const { 531 if (!FromType->isIntegerTy() || !ToType->isIntegerTy()) 532 return false; 533 unsigned FromBits = FromType->getPrimitiveSizeInBits(); 534 unsigned ToBits = ToType->getPrimitiveSizeInBits(); 535 return FromBits > ToBits; 536 } 537 538 bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const { 539 if (!FromVT.isInteger() || !ToVT.isInteger()) 540 return false; 541 unsigned FromBits = FromVT.getSizeInBits(); 542 unsigned ToBits = ToVT.getSizeInBits(); 543 return FromBits > ToBits; 544 } 545 546 //===----------------------------------------------------------------------===// 547 // Inline asm support 548 //===----------------------------------------------------------------------===// 549 550 TargetLowering::ConstraintType 551 SystemZTargetLowering::getConstraintType(StringRef Constraint) const { 552 if (Constraint.size() == 1) { 553 switch (Constraint[0]) { 554 case 'a': // Address register 555 case 'd': // Data register (equivalent to 'r') 556 case 'f': // Floating-point register 557 case 'h': // High-part register 558 case 'r': // General-purpose register 559 return C_RegisterClass; 560 561 case 'Q': // Memory with base and unsigned 12-bit displacement 562 case 'R': // Likewise, plus an index 563 case 'S': // Memory with base and signed 20-bit displacement 564 case 'T': // Likewise, plus an index 565 case 'm': // Equivalent to 'T'. 566 return C_Memory; 567 568 case 'I': // Unsigned 8-bit constant 569 case 'J': // Unsigned 12-bit constant 570 case 'K': // Signed 16-bit constant 571 case 'L': // Signed 20-bit displacement (on all targets we support) 572 case 'M': // 0x7fffffff 573 return C_Other; 574 575 default: 576 break; 577 } 578 } 579 return TargetLowering::getConstraintType(Constraint); 580 } 581 582 TargetLowering::ConstraintWeight SystemZTargetLowering:: 583 getSingleConstraintMatchWeight(AsmOperandInfo &info, 584 const char *constraint) const { 585 ConstraintWeight weight = CW_Invalid; 586 Value *CallOperandVal = info.CallOperandVal; 587 // If we don't have a value, we can't do a match, 588 // but allow it at the lowest weight. 589 if (!CallOperandVal) 590 return CW_Default; 591 Type *type = CallOperandVal->getType(); 592 // Look at the constraint type. 593 switch (*constraint) { 594 default: 595 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 596 break; 597 598 case 'a': // Address register 599 case 'd': // Data register (equivalent to 'r') 600 case 'h': // High-part register 601 case 'r': // General-purpose register 602 if (CallOperandVal->getType()->isIntegerTy()) 603 weight = CW_Register; 604 break; 605 606 case 'f': // Floating-point register 607 if (type->isFloatingPointTy()) 608 weight = CW_Register; 609 break; 610 611 case 'I': // Unsigned 8-bit constant 612 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) 613 if (isUInt<8>(C->getZExtValue())) 614 weight = CW_Constant; 615 break; 616 617 case 'J': // Unsigned 12-bit constant 618 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) 619 if (isUInt<12>(C->getZExtValue())) 620 weight = CW_Constant; 621 break; 622 623 case 'K': // Signed 16-bit constant 624 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) 625 if (isInt<16>(C->getSExtValue())) 626 weight = CW_Constant; 627 break; 628 629 case 'L': // Signed 20-bit displacement (on all targets we support) 630 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) 631 if (isInt<20>(C->getSExtValue())) 632 weight = CW_Constant; 633 break; 634 635 case 'M': // 0x7fffffff 636 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) 637 if (C->getZExtValue() == 0x7fffffff) 638 weight = CW_Constant; 639 break; 640 } 641 return weight; 642 } 643 644 // Parse a "{tNNN}" register constraint for which the register type "t" 645 // has already been verified. MC is the class associated with "t" and 646 // Map maps 0-based register numbers to LLVM register numbers. 647 static std::pair<unsigned, const TargetRegisterClass *> 648 parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC, 649 const unsigned *Map) { 650 assert(*(Constraint.end()-1) == '}' && "Missing '}'"); 651 if (isdigit(Constraint[2])) { 652 unsigned Index; 653 bool Failed = 654 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index); 655 if (!Failed && Index < 16 && Map[Index]) 656 return std::make_pair(Map[Index], RC); 657 } 658 return std::make_pair(0U, nullptr); 659 } 660 661 std::pair<unsigned, const TargetRegisterClass *> 662 SystemZTargetLowering::getRegForInlineAsmConstraint( 663 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { 664 if (Constraint.size() == 1) { 665 // GCC Constraint Letters 666 switch (Constraint[0]) { 667 default: break; 668 case 'd': // Data register (equivalent to 'r') 669 case 'r': // General-purpose register 670 if (VT == MVT::i64) 671 return std::make_pair(0U, &SystemZ::GR64BitRegClass); 672 else if (VT == MVT::i128) 673 return std::make_pair(0U, &SystemZ::GR128BitRegClass); 674 return std::make_pair(0U, &SystemZ::GR32BitRegClass); 675 676 case 'a': // Address register 677 if (VT == MVT::i64) 678 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass); 679 else if (VT == MVT::i128) 680 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass); 681 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass); 682 683 case 'h': // High-part register (an LLVM extension) 684 return std::make_pair(0U, &SystemZ::GRH32BitRegClass); 685 686 case 'f': // Floating-point register 687 if (VT == MVT::f64) 688 return std::make_pair(0U, &SystemZ::FP64BitRegClass); 689 else if (VT == MVT::f128) 690 return std::make_pair(0U, &SystemZ::FP128BitRegClass); 691 return std::make_pair(0U, &SystemZ::FP32BitRegClass); 692 } 693 } 694 if (Constraint.size() > 0 && Constraint[0] == '{') { 695 // We need to override the default register parsing for GPRs and FPRs 696 // because the interpretation depends on VT. The internal names of 697 // the registers are also different from the external names 698 // (F0D and F0S instead of F0, etc.). 699 if (Constraint[1] == 'r') { 700 if (VT == MVT::i32) 701 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass, 702 SystemZMC::GR32Regs); 703 if (VT == MVT::i128) 704 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass, 705 SystemZMC::GR128Regs); 706 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass, 707 SystemZMC::GR64Regs); 708 } 709 if (Constraint[1] == 'f') { 710 if (VT == MVT::f32) 711 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass, 712 SystemZMC::FP32Regs); 713 if (VT == MVT::f128) 714 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass, 715 SystemZMC::FP128Regs); 716 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass, 717 SystemZMC::FP64Regs); 718 } 719 } 720 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 721 } 722 723 void SystemZTargetLowering:: 724 LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint, 725 std::vector<SDValue> &Ops, 726 SelectionDAG &DAG) const { 727 // Only support length 1 constraints for now. 728 if (Constraint.length() == 1) { 729 switch (Constraint[0]) { 730 case 'I': // Unsigned 8-bit constant 731 if (auto *C = dyn_cast<ConstantSDNode>(Op)) 732 if (isUInt<8>(C->getZExtValue())) 733 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), 734 Op.getValueType())); 735 return; 736 737 case 'J': // Unsigned 12-bit constant 738 if (auto *C = dyn_cast<ConstantSDNode>(Op)) 739 if (isUInt<12>(C->getZExtValue())) 740 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), 741 Op.getValueType())); 742 return; 743 744 case 'K': // Signed 16-bit constant 745 if (auto *C = dyn_cast<ConstantSDNode>(Op)) 746 if (isInt<16>(C->getSExtValue())) 747 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op), 748 Op.getValueType())); 749 return; 750 751 case 'L': // Signed 20-bit displacement (on all targets we support) 752 if (auto *C = dyn_cast<ConstantSDNode>(Op)) 753 if (isInt<20>(C->getSExtValue())) 754 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op), 755 Op.getValueType())); 756 return; 757 758 case 'M': // 0x7fffffff 759 if (auto *C = dyn_cast<ConstantSDNode>(Op)) 760 if (C->getZExtValue() == 0x7fffffff) 761 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), 762 Op.getValueType())); 763 return; 764 } 765 } 766 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 767 } 768 769 //===----------------------------------------------------------------------===// 770 // Calling conventions 771 //===----------------------------------------------------------------------===// 772 773 #include "SystemZGenCallingConv.inc" 774 775 bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType, 776 Type *ToType) const { 777 return isTruncateFree(FromType, ToType); 778 } 779 780 bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { 781 return CI->isTailCall(); 782 } 783 784 // We do not yet support 128-bit single-element vector types. If the user 785 // attempts to use such types as function argument or return type, prefer 786 // to error out instead of emitting code violating the ABI. 787 static void VerifyVectorType(MVT VT, EVT ArgVT) { 788 if (ArgVT.isVector() && !VT.isVector()) 789 report_fatal_error("Unsupported vector argument or return type"); 790 } 791 792 static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) { 793 for (unsigned i = 0; i < Ins.size(); ++i) 794 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT); 795 } 796 797 static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) { 798 for (unsigned i = 0; i < Outs.size(); ++i) 799 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT); 800 } 801 802 // Value is a value that has been passed to us in the location described by VA 803 // (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining 804 // any loads onto Chain. 805 static SDValue convertLocVTToValVT(SelectionDAG &DAG, const SDLoc &DL, 806 CCValAssign &VA, SDValue Chain, 807 SDValue Value) { 808 // If the argument has been promoted from a smaller type, insert an 809 // assertion to capture this. 810 if (VA.getLocInfo() == CCValAssign::SExt) 811 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value, 812 DAG.getValueType(VA.getValVT())); 813 else if (VA.getLocInfo() == CCValAssign::ZExt) 814 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value, 815 DAG.getValueType(VA.getValVT())); 816 817 if (VA.isExtInLoc()) 818 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value); 819 else if (VA.getLocInfo() == CCValAssign::BCvt) { 820 // If this is a short vector argument loaded from the stack, 821 // extend from i64 to full vector size and then bitcast. 822 assert(VA.getLocVT() == MVT::i64); 823 assert(VA.getValVT().isVector()); 824 Value = DAG.getBuildVector(MVT::v2i64, DL, {Value, DAG.getUNDEF(MVT::i64)}); 825 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value); 826 } else 827 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo"); 828 return Value; 829 } 830 831 // Value is a value of type VA.getValVT() that we need to copy into 832 // the location described by VA. Return a copy of Value converted to 833 // VA.getValVT(). The caller is responsible for handling indirect values. 834 static SDValue convertValVTToLocVT(SelectionDAG &DAG, const SDLoc &DL, 835 CCValAssign &VA, SDValue Value) { 836 switch (VA.getLocInfo()) { 837 case CCValAssign::SExt: 838 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value); 839 case CCValAssign::ZExt: 840 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value); 841 case CCValAssign::AExt: 842 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value); 843 case CCValAssign::BCvt: 844 // If this is a short vector argument to be stored to the stack, 845 // bitcast to v2i64 and then extract first element. 846 assert(VA.getLocVT() == MVT::i64); 847 assert(VA.getValVT().isVector()); 848 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value); 849 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value, 850 DAG.getConstant(0, DL, MVT::i32)); 851 case CCValAssign::Full: 852 return Value; 853 default: 854 llvm_unreachable("Unhandled getLocInfo()"); 855 } 856 } 857 858 SDValue SystemZTargetLowering::LowerFormalArguments( 859 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 860 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 861 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 862 MachineFunction &MF = DAG.getMachineFunction(); 863 MachineFrameInfo *MFI = MF.getFrameInfo(); 864 MachineRegisterInfo &MRI = MF.getRegInfo(); 865 SystemZMachineFunctionInfo *FuncInfo = 866 MF.getInfo<SystemZMachineFunctionInfo>(); 867 auto *TFL = 868 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering()); 869 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 870 871 // Detect unsupported vector argument types. 872 if (Subtarget.hasVector()) 873 VerifyVectorTypes(Ins); 874 875 // Assign locations to all of the incoming arguments. 876 SmallVector<CCValAssign, 16> ArgLocs; 877 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); 878 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ); 879 880 unsigned NumFixedGPRs = 0; 881 unsigned NumFixedFPRs = 0; 882 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { 883 SDValue ArgValue; 884 CCValAssign &VA = ArgLocs[I]; 885 EVT LocVT = VA.getLocVT(); 886 if (VA.isRegLoc()) { 887 // Arguments passed in registers 888 const TargetRegisterClass *RC; 889 switch (LocVT.getSimpleVT().SimpleTy) { 890 default: 891 // Integers smaller than i64 should be promoted to i64. 892 llvm_unreachable("Unexpected argument type"); 893 case MVT::i32: 894 NumFixedGPRs += 1; 895 RC = &SystemZ::GR32BitRegClass; 896 break; 897 case MVT::i64: 898 NumFixedGPRs += 1; 899 RC = &SystemZ::GR64BitRegClass; 900 break; 901 case MVT::f32: 902 NumFixedFPRs += 1; 903 RC = &SystemZ::FP32BitRegClass; 904 break; 905 case MVT::f64: 906 NumFixedFPRs += 1; 907 RC = &SystemZ::FP64BitRegClass; 908 break; 909 case MVT::v16i8: 910 case MVT::v8i16: 911 case MVT::v4i32: 912 case MVT::v2i64: 913 case MVT::v4f32: 914 case MVT::v2f64: 915 RC = &SystemZ::VR128BitRegClass; 916 break; 917 } 918 919 unsigned VReg = MRI.createVirtualRegister(RC); 920 MRI.addLiveIn(VA.getLocReg(), VReg); 921 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT); 922 } else { 923 assert(VA.isMemLoc() && "Argument not register or memory"); 924 925 // Create the frame index object for this incoming parameter. 926 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8, 927 VA.getLocMemOffset(), true); 928 929 // Create the SelectionDAG nodes corresponding to a load 930 // from this parameter. Unpromoted ints and floats are 931 // passed as right-justified 8-byte values. 932 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 933 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32) 934 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, 935 DAG.getIntPtrConstant(4, DL)); 936 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN, 937 MachinePointerInfo::getFixedStack(MF, FI), false, 938 false, false, 0); 939 } 940 941 // Convert the value of the argument register into the value that's 942 // being passed. 943 if (VA.getLocInfo() == CCValAssign::Indirect) { 944 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, 945 ArgValue, MachinePointerInfo(), 946 false, false, false, 0)); 947 // If the original argument was split (e.g. i128), we need 948 // to load all parts of it here (using the same address). 949 unsigned ArgIndex = Ins[I].OrigArgIndex; 950 assert (Ins[I].PartOffset == 0); 951 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) { 952 CCValAssign &PartVA = ArgLocs[I + 1]; 953 unsigned PartOffset = Ins[I + 1].PartOffset; 954 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue, 955 DAG.getIntPtrConstant(PartOffset, DL)); 956 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, 957 Address, MachinePointerInfo(), 958 false, false, false, 0)); 959 ++I; 960 } 961 } else 962 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue)); 963 } 964 965 if (IsVarArg) { 966 // Save the number of non-varargs registers for later use by va_start, etc. 967 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs); 968 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs); 969 970 // Likewise the address (in the form of a frame index) of where the 971 // first stack vararg would be. The 1-byte size here is arbitrary. 972 int64_t StackSize = CCInfo.getNextStackOffset(); 973 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true)); 974 975 // ...and a similar frame index for the caller-allocated save area 976 // that will be used to store the incoming registers. 977 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea(); 978 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true); 979 FuncInfo->setRegSaveFrameIndex(RegSaveIndex); 980 981 // Store the FPR varargs in the reserved frame slots. (We store the 982 // GPRs as part of the prologue.) 983 if (NumFixedFPRs < SystemZ::NumArgFPRs) { 984 SDValue MemOps[SystemZ::NumArgFPRs]; 985 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) { 986 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]); 987 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true); 988 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 989 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I], 990 &SystemZ::FP64BitRegClass); 991 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64); 992 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN, 993 MachinePointerInfo::getFixedStack(MF, FI), 994 false, false, 0); 995 } 996 // Join the stores, which are independent of one another. 997 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, 998 makeArrayRef(&MemOps[NumFixedFPRs], 999 SystemZ::NumArgFPRs-NumFixedFPRs)); 1000 } 1001 } 1002 1003 return Chain; 1004 } 1005 1006 static bool canUseSiblingCall(const CCState &ArgCCInfo, 1007 SmallVectorImpl<CCValAssign> &ArgLocs, 1008 SmallVectorImpl<ISD::OutputArg> &Outs) { 1009 // Punt if there are any indirect or stack arguments, or if the call 1010 // needs the callee-saved argument register R6, or if the call uses 1011 // the callee-saved register arguments SwiftSelf and SwiftError. 1012 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { 1013 CCValAssign &VA = ArgLocs[I]; 1014 if (VA.getLocInfo() == CCValAssign::Indirect) 1015 return false; 1016 if (!VA.isRegLoc()) 1017 return false; 1018 unsigned Reg = VA.getLocReg(); 1019 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D) 1020 return false; 1021 if (Outs[I].Flags.isSwiftSelf() || Outs[I].Flags.isSwiftError()) 1022 return false; 1023 } 1024 return true; 1025 } 1026 1027 SDValue 1028 SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI, 1029 SmallVectorImpl<SDValue> &InVals) const { 1030 SelectionDAG &DAG = CLI.DAG; 1031 SDLoc &DL = CLI.DL; 1032 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 1033 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 1034 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 1035 SDValue Chain = CLI.Chain; 1036 SDValue Callee = CLI.Callee; 1037 bool &IsTailCall = CLI.IsTailCall; 1038 CallingConv::ID CallConv = CLI.CallConv; 1039 bool IsVarArg = CLI.IsVarArg; 1040 MachineFunction &MF = DAG.getMachineFunction(); 1041 EVT PtrVT = getPointerTy(MF.getDataLayout()); 1042 1043 // Detect unsupported vector argument and return types. 1044 if (Subtarget.hasVector()) { 1045 VerifyVectorTypes(Outs); 1046 VerifyVectorTypes(Ins); 1047 } 1048 1049 // Analyze the operands of the call, assigning locations to each operand. 1050 SmallVector<CCValAssign, 16> ArgLocs; 1051 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); 1052 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ); 1053 1054 // We don't support GuaranteedTailCallOpt, only automatically-detected 1055 // sibling calls. 1056 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs, Outs)) 1057 IsTailCall = false; 1058 1059 // Get a count of how many bytes are to be pushed on the stack. 1060 unsigned NumBytes = ArgCCInfo.getNextStackOffset(); 1061 1062 // Mark the start of the call. 1063 if (!IsTailCall) 1064 Chain = DAG.getCALLSEQ_START(Chain, 1065 DAG.getConstant(NumBytes, DL, PtrVT, true), 1066 DL); 1067 1068 // Copy argument values to their designated locations. 1069 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass; 1070 SmallVector<SDValue, 8> MemOpChains; 1071 SDValue StackPtr; 1072 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { 1073 CCValAssign &VA = ArgLocs[I]; 1074 SDValue ArgValue = OutVals[I]; 1075 1076 if (VA.getLocInfo() == CCValAssign::Indirect) { 1077 // Store the argument in a stack slot and pass its address. 1078 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT); 1079 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); 1080 MemOpChains.push_back(DAG.getStore( 1081 Chain, DL, ArgValue, SpillSlot, 1082 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0)); 1083 // If the original argument was split (e.g. i128), we need 1084 // to store all parts of it here (and pass just one address). 1085 unsigned ArgIndex = Outs[I].OrigArgIndex; 1086 assert (Outs[I].PartOffset == 0); 1087 while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) { 1088 SDValue PartValue = OutVals[I + 1]; 1089 unsigned PartOffset = Outs[I + 1].PartOffset; 1090 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot, 1091 DAG.getIntPtrConstant(PartOffset, DL)); 1092 MemOpChains.push_back(DAG.getStore( 1093 Chain, DL, PartValue, Address, 1094 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0)); 1095 ++I; 1096 } 1097 ArgValue = SpillSlot; 1098 } else 1099 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue); 1100 1101 if (VA.isRegLoc()) 1102 // Queue up the argument copies and emit them at the end. 1103 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue)); 1104 else { 1105 assert(VA.isMemLoc() && "Argument not register or memory"); 1106 1107 // Work out the address of the stack slot. Unpromoted ints and 1108 // floats are passed as right-justified 8-byte values. 1109 if (!StackPtr.getNode()) 1110 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT); 1111 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset(); 1112 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32) 1113 Offset += 4; 1114 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, 1115 DAG.getIntPtrConstant(Offset, DL)); 1116 1117 // Emit the store. 1118 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address, 1119 MachinePointerInfo(), 1120 false, false, 0)); 1121 } 1122 } 1123 1124 // Join the stores, which are independent of one another. 1125 if (!MemOpChains.empty()) 1126 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains); 1127 1128 // Accept direct calls by converting symbolic call addresses to the 1129 // associated Target* opcodes. Force %r1 to be used for indirect 1130 // tail calls. 1131 SDValue Glue; 1132 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1133 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT); 1134 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee); 1135 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) { 1136 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT); 1137 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee); 1138 } else if (IsTailCall) { 1139 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue); 1140 Glue = Chain.getValue(1); 1141 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType()); 1142 } 1143 1144 // Build a sequence of copy-to-reg nodes, chained and glued together. 1145 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) { 1146 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first, 1147 RegsToPass[I].second, Glue); 1148 Glue = Chain.getValue(1); 1149 } 1150 1151 // The first call operand is the chain and the second is the target address. 1152 SmallVector<SDValue, 8> Ops; 1153 Ops.push_back(Chain); 1154 Ops.push_back(Callee); 1155 1156 // Add argument registers to the end of the list so that they are 1157 // known live into the call. 1158 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) 1159 Ops.push_back(DAG.getRegister(RegsToPass[I].first, 1160 RegsToPass[I].second.getValueType())); 1161 1162 // Add a register mask operand representing the call-preserved registers. 1163 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); 1164 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv); 1165 assert(Mask && "Missing call preserved mask for calling convention"); 1166 Ops.push_back(DAG.getRegisterMask(Mask)); 1167 1168 // Glue the call to the argument copies, if any. 1169 if (Glue.getNode()) 1170 Ops.push_back(Glue); 1171 1172 // Emit the call. 1173 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 1174 if (IsTailCall) 1175 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops); 1176 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops); 1177 Glue = Chain.getValue(1); 1178 1179 // Mark the end of the call, which is glued to the call itself. 1180 Chain = DAG.getCALLSEQ_END(Chain, 1181 DAG.getConstant(NumBytes, DL, PtrVT, true), 1182 DAG.getConstant(0, DL, PtrVT, true), 1183 Glue, DL); 1184 Glue = Chain.getValue(1); 1185 1186 // Assign locations to each value returned by this call. 1187 SmallVector<CCValAssign, 16> RetLocs; 1188 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext()); 1189 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ); 1190 1191 // Copy all of the result registers out of their specified physreg. 1192 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) { 1193 CCValAssign &VA = RetLocs[I]; 1194 1195 // Copy the value out, gluing the copy to the end of the call sequence. 1196 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), 1197 VA.getLocVT(), Glue); 1198 Chain = RetValue.getValue(1); 1199 Glue = RetValue.getValue(2); 1200 1201 // Convert the value of the return register into the value that's 1202 // being returned. 1203 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue)); 1204 } 1205 1206 return Chain; 1207 } 1208 1209 bool SystemZTargetLowering:: 1210 CanLowerReturn(CallingConv::ID CallConv, 1211 MachineFunction &MF, bool isVarArg, 1212 const SmallVectorImpl<ISD::OutputArg> &Outs, 1213 LLVMContext &Context) const { 1214 // Detect unsupported vector return types. 1215 if (Subtarget.hasVector()) 1216 VerifyVectorTypes(Outs); 1217 1218 // Special case that we cannot easily detect in RetCC_SystemZ since 1219 // i128 is not a legal type. 1220 for (auto &Out : Outs) 1221 if (Out.ArgVT == MVT::i128) 1222 return false; 1223 1224 SmallVector<CCValAssign, 16> RetLocs; 1225 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context); 1226 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ); 1227 } 1228 1229 SDValue 1230 SystemZTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 1231 bool IsVarArg, 1232 const SmallVectorImpl<ISD::OutputArg> &Outs, 1233 const SmallVectorImpl<SDValue> &OutVals, 1234 const SDLoc &DL, SelectionDAG &DAG) const { 1235 MachineFunction &MF = DAG.getMachineFunction(); 1236 1237 // Detect unsupported vector return types. 1238 if (Subtarget.hasVector()) 1239 VerifyVectorTypes(Outs); 1240 1241 // Assign locations to each returned value. 1242 SmallVector<CCValAssign, 16> RetLocs; 1243 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext()); 1244 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ); 1245 1246 // Quick exit for void returns 1247 if (RetLocs.empty()) 1248 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain); 1249 1250 // Copy the result values into the output registers. 1251 SDValue Glue; 1252 SmallVector<SDValue, 4> RetOps; 1253 RetOps.push_back(Chain); 1254 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) { 1255 CCValAssign &VA = RetLocs[I]; 1256 SDValue RetValue = OutVals[I]; 1257 1258 // Make the return register live on exit. 1259 assert(VA.isRegLoc() && "Can only return in registers!"); 1260 1261 // Promote the value as required. 1262 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue); 1263 1264 // Chain and glue the copies together. 1265 unsigned Reg = VA.getLocReg(); 1266 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue); 1267 Glue = Chain.getValue(1); 1268 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT())); 1269 } 1270 1271 // Update chain and glue. 1272 RetOps[0] = Chain; 1273 if (Glue.getNode()) 1274 RetOps.push_back(Glue); 1275 1276 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps); 1277 } 1278 1279 SDValue SystemZTargetLowering::prepareVolatileOrAtomicLoad( 1280 SDValue Chain, const SDLoc &DL, SelectionDAG &DAG) const { 1281 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain); 1282 } 1283 1284 // Return true if Op is an intrinsic node with chain that returns the CC value 1285 // as its only (other) argument. Provide the associated SystemZISD opcode and 1286 // the mask of valid CC values if so. 1287 static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode, 1288 unsigned &CCValid) { 1289 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 1290 switch (Id) { 1291 case Intrinsic::s390_tbegin: 1292 Opcode = SystemZISD::TBEGIN; 1293 CCValid = SystemZ::CCMASK_TBEGIN; 1294 return true; 1295 1296 case Intrinsic::s390_tbegin_nofloat: 1297 Opcode = SystemZISD::TBEGIN_NOFLOAT; 1298 CCValid = SystemZ::CCMASK_TBEGIN; 1299 return true; 1300 1301 case Intrinsic::s390_tend: 1302 Opcode = SystemZISD::TEND; 1303 CCValid = SystemZ::CCMASK_TEND; 1304 return true; 1305 1306 default: 1307 return false; 1308 } 1309 } 1310 1311 // Return true if Op is an intrinsic node without chain that returns the 1312 // CC value as its final argument. Provide the associated SystemZISD 1313 // opcode and the mask of valid CC values if so. 1314 static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) { 1315 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 1316 switch (Id) { 1317 case Intrinsic::s390_vpkshs: 1318 case Intrinsic::s390_vpksfs: 1319 case Intrinsic::s390_vpksgs: 1320 Opcode = SystemZISD::PACKS_CC; 1321 CCValid = SystemZ::CCMASK_VCMP; 1322 return true; 1323 1324 case Intrinsic::s390_vpklshs: 1325 case Intrinsic::s390_vpklsfs: 1326 case Intrinsic::s390_vpklsgs: 1327 Opcode = SystemZISD::PACKLS_CC; 1328 CCValid = SystemZ::CCMASK_VCMP; 1329 return true; 1330 1331 case Intrinsic::s390_vceqbs: 1332 case Intrinsic::s390_vceqhs: 1333 case Intrinsic::s390_vceqfs: 1334 case Intrinsic::s390_vceqgs: 1335 Opcode = SystemZISD::VICMPES; 1336 CCValid = SystemZ::CCMASK_VCMP; 1337 return true; 1338 1339 case Intrinsic::s390_vchbs: 1340 case Intrinsic::s390_vchhs: 1341 case Intrinsic::s390_vchfs: 1342 case Intrinsic::s390_vchgs: 1343 Opcode = SystemZISD::VICMPHS; 1344 CCValid = SystemZ::CCMASK_VCMP; 1345 return true; 1346 1347 case Intrinsic::s390_vchlbs: 1348 case Intrinsic::s390_vchlhs: 1349 case Intrinsic::s390_vchlfs: 1350 case Intrinsic::s390_vchlgs: 1351 Opcode = SystemZISD::VICMPHLS; 1352 CCValid = SystemZ::CCMASK_VCMP; 1353 return true; 1354 1355 case Intrinsic::s390_vtm: 1356 Opcode = SystemZISD::VTM; 1357 CCValid = SystemZ::CCMASK_VCMP; 1358 return true; 1359 1360 case Intrinsic::s390_vfaebs: 1361 case Intrinsic::s390_vfaehs: 1362 case Intrinsic::s390_vfaefs: 1363 Opcode = SystemZISD::VFAE_CC; 1364 CCValid = SystemZ::CCMASK_ANY; 1365 return true; 1366 1367 case Intrinsic::s390_vfaezbs: 1368 case Intrinsic::s390_vfaezhs: 1369 case Intrinsic::s390_vfaezfs: 1370 Opcode = SystemZISD::VFAEZ_CC; 1371 CCValid = SystemZ::CCMASK_ANY; 1372 return true; 1373 1374 case Intrinsic::s390_vfeebs: 1375 case Intrinsic::s390_vfeehs: 1376 case Intrinsic::s390_vfeefs: 1377 Opcode = SystemZISD::VFEE_CC; 1378 CCValid = SystemZ::CCMASK_ANY; 1379 return true; 1380 1381 case Intrinsic::s390_vfeezbs: 1382 case Intrinsic::s390_vfeezhs: 1383 case Intrinsic::s390_vfeezfs: 1384 Opcode = SystemZISD::VFEEZ_CC; 1385 CCValid = SystemZ::CCMASK_ANY; 1386 return true; 1387 1388 case Intrinsic::s390_vfenebs: 1389 case Intrinsic::s390_vfenehs: 1390 case Intrinsic::s390_vfenefs: 1391 Opcode = SystemZISD::VFENE_CC; 1392 CCValid = SystemZ::CCMASK_ANY; 1393 return true; 1394 1395 case Intrinsic::s390_vfenezbs: 1396 case Intrinsic::s390_vfenezhs: 1397 case Intrinsic::s390_vfenezfs: 1398 Opcode = SystemZISD::VFENEZ_CC; 1399 CCValid = SystemZ::CCMASK_ANY; 1400 return true; 1401 1402 case Intrinsic::s390_vistrbs: 1403 case Intrinsic::s390_vistrhs: 1404 case Intrinsic::s390_vistrfs: 1405 Opcode = SystemZISD::VISTR_CC; 1406 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3; 1407 return true; 1408 1409 case Intrinsic::s390_vstrcbs: 1410 case Intrinsic::s390_vstrchs: 1411 case Intrinsic::s390_vstrcfs: 1412 Opcode = SystemZISD::VSTRC_CC; 1413 CCValid = SystemZ::CCMASK_ANY; 1414 return true; 1415 1416 case Intrinsic::s390_vstrczbs: 1417 case Intrinsic::s390_vstrczhs: 1418 case Intrinsic::s390_vstrczfs: 1419 Opcode = SystemZISD::VSTRCZ_CC; 1420 CCValid = SystemZ::CCMASK_ANY; 1421 return true; 1422 1423 case Intrinsic::s390_vfcedbs: 1424 Opcode = SystemZISD::VFCMPES; 1425 CCValid = SystemZ::CCMASK_VCMP; 1426 return true; 1427 1428 case Intrinsic::s390_vfchdbs: 1429 Opcode = SystemZISD::VFCMPHS; 1430 CCValid = SystemZ::CCMASK_VCMP; 1431 return true; 1432 1433 case Intrinsic::s390_vfchedbs: 1434 Opcode = SystemZISD::VFCMPHES; 1435 CCValid = SystemZ::CCMASK_VCMP; 1436 return true; 1437 1438 case Intrinsic::s390_vftcidb: 1439 Opcode = SystemZISD::VFTCI; 1440 CCValid = SystemZ::CCMASK_VCMP; 1441 return true; 1442 1443 default: 1444 return false; 1445 } 1446 } 1447 1448 // Emit an intrinsic with chain with a glued value instead of its CC result. 1449 static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op, 1450 unsigned Opcode) { 1451 // Copy all operands except the intrinsic ID. 1452 unsigned NumOps = Op.getNumOperands(); 1453 SmallVector<SDValue, 6> Ops; 1454 Ops.reserve(NumOps - 1); 1455 Ops.push_back(Op.getOperand(0)); 1456 for (unsigned I = 2; I < NumOps; ++I) 1457 Ops.push_back(Op.getOperand(I)); 1458 1459 assert(Op->getNumValues() == 2 && "Expected only CC result and chain"); 1460 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue); 1461 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops); 1462 SDValue OldChain = SDValue(Op.getNode(), 1); 1463 SDValue NewChain = SDValue(Intr.getNode(), 0); 1464 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain); 1465 return Intr; 1466 } 1467 1468 // Emit an intrinsic with a glued value instead of its CC result. 1469 static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op, 1470 unsigned Opcode) { 1471 // Copy all operands except the intrinsic ID. 1472 unsigned NumOps = Op.getNumOperands(); 1473 SmallVector<SDValue, 6> Ops; 1474 Ops.reserve(NumOps - 1); 1475 for (unsigned I = 1; I < NumOps; ++I) 1476 Ops.push_back(Op.getOperand(I)); 1477 1478 if (Op->getNumValues() == 1) 1479 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops); 1480 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result"); 1481 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue); 1482 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops); 1483 } 1484 1485 // CC is a comparison that will be implemented using an integer or 1486 // floating-point comparison. Return the condition code mask for 1487 // a branch on true. In the integer case, CCMASK_CMP_UO is set for 1488 // unsigned comparisons and clear for signed ones. In the floating-point 1489 // case, CCMASK_CMP_UO has its normal mask meaning (unordered). 1490 static unsigned CCMaskForCondCode(ISD::CondCode CC) { 1491 #define CONV(X) \ 1492 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \ 1493 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \ 1494 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X 1495 1496 switch (CC) { 1497 default: 1498 llvm_unreachable("Invalid integer condition!"); 1499 1500 CONV(EQ); 1501 CONV(NE); 1502 CONV(GT); 1503 CONV(GE); 1504 CONV(LT); 1505 CONV(LE); 1506 1507 case ISD::SETO: return SystemZ::CCMASK_CMP_O; 1508 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO; 1509 } 1510 #undef CONV 1511 } 1512 1513 // Return a sequence for getting a 1 from an IPM result when CC has a 1514 // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask. 1515 // The handling of CC values outside CCValid doesn't matter. 1516 static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) { 1517 // Deal with cases where the result can be taken directly from a bit 1518 // of the IPM result. 1519 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3))) 1520 return IPMConversion(0, 0, SystemZ::IPM_CC); 1521 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3))) 1522 return IPMConversion(0, 0, SystemZ::IPM_CC + 1); 1523 1524 // Deal with cases where we can add a value to force the sign bit 1525 // to contain the right value. Putting the bit in 31 means we can 1526 // use SRL rather than RISBG(L), and also makes it easier to get a 1527 // 0/-1 value, so it has priority over the other tests below. 1528 // 1529 // These sequences rely on the fact that the upper two bits of the 1530 // IPM result are zero. 1531 uint64_t TopBit = uint64_t(1) << 31; 1532 if (CCMask == (CCValid & SystemZ::CCMASK_0)) 1533 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31); 1534 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1))) 1535 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31); 1536 if (CCMask == (CCValid & (SystemZ::CCMASK_0 1537 | SystemZ::CCMASK_1 1538 | SystemZ::CCMASK_2))) 1539 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31); 1540 if (CCMask == (CCValid & SystemZ::CCMASK_3)) 1541 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31); 1542 if (CCMask == (CCValid & (SystemZ::CCMASK_1 1543 | SystemZ::CCMASK_2 1544 | SystemZ::CCMASK_3))) 1545 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31); 1546 1547 // Next try inverting the value and testing a bit. 0/1 could be 1548 // handled this way too, but we dealt with that case above. 1549 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2))) 1550 return IPMConversion(-1, 0, SystemZ::IPM_CC); 1551 1552 // Handle cases where adding a value forces a non-sign bit to contain 1553 // the right value. 1554 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2))) 1555 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1); 1556 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3))) 1557 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1); 1558 1559 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are 1560 // can be done by inverting the low CC bit and applying one of the 1561 // sign-based extractions above. 1562 if (CCMask == (CCValid & SystemZ::CCMASK_1)) 1563 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31); 1564 if (CCMask == (CCValid & SystemZ::CCMASK_2)) 1565 return IPMConversion(1 << SystemZ::IPM_CC, 1566 TopBit - (3 << SystemZ::IPM_CC), 31); 1567 if (CCMask == (CCValid & (SystemZ::CCMASK_0 1568 | SystemZ::CCMASK_1 1569 | SystemZ::CCMASK_3))) 1570 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31); 1571 if (CCMask == (CCValid & (SystemZ::CCMASK_0 1572 | SystemZ::CCMASK_2 1573 | SystemZ::CCMASK_3))) 1574 return IPMConversion(1 << SystemZ::IPM_CC, 1575 TopBit - (1 << SystemZ::IPM_CC), 31); 1576 1577 llvm_unreachable("Unexpected CC combination"); 1578 } 1579 1580 // If C can be converted to a comparison against zero, adjust the operands 1581 // as necessary. 1582 static void adjustZeroCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) { 1583 if (C.ICmpType == SystemZICMP::UnsignedOnly) 1584 return; 1585 1586 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode()); 1587 if (!ConstOp1) 1588 return; 1589 1590 int64_t Value = ConstOp1->getSExtValue(); 1591 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) || 1592 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) || 1593 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) || 1594 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) { 1595 C.CCMask ^= SystemZ::CCMASK_CMP_EQ; 1596 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType()); 1597 } 1598 } 1599 1600 // If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI, 1601 // adjust the operands as necessary. 1602 static void adjustSubwordCmp(SelectionDAG &DAG, const SDLoc &DL, 1603 Comparison &C) { 1604 // For us to make any changes, it must a comparison between a single-use 1605 // load and a constant. 1606 if (!C.Op0.hasOneUse() || 1607 C.Op0.getOpcode() != ISD::LOAD || 1608 C.Op1.getOpcode() != ISD::Constant) 1609 return; 1610 1611 // We must have an 8- or 16-bit load. 1612 auto *Load = cast<LoadSDNode>(C.Op0); 1613 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits(); 1614 if (NumBits != 8 && NumBits != 16) 1615 return; 1616 1617 // The load must be an extending one and the constant must be within the 1618 // range of the unextended value. 1619 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1); 1620 uint64_t Value = ConstOp1->getZExtValue(); 1621 uint64_t Mask = (1 << NumBits) - 1; 1622 if (Load->getExtensionType() == ISD::SEXTLOAD) { 1623 // Make sure that ConstOp1 is in range of C.Op0. 1624 int64_t SignedValue = ConstOp1->getSExtValue(); 1625 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask) 1626 return; 1627 if (C.ICmpType != SystemZICMP::SignedOnly) { 1628 // Unsigned comparison between two sign-extended values is equivalent 1629 // to unsigned comparison between two zero-extended values. 1630 Value &= Mask; 1631 } else if (NumBits == 8) { 1632 // Try to treat the comparison as unsigned, so that we can use CLI. 1633 // Adjust CCMask and Value as necessary. 1634 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT) 1635 // Test whether the high bit of the byte is set. 1636 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT; 1637 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE) 1638 // Test whether the high bit of the byte is clear. 1639 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT; 1640 else 1641 // No instruction exists for this combination. 1642 return; 1643 C.ICmpType = SystemZICMP::UnsignedOnly; 1644 } 1645 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) { 1646 if (Value > Mask) 1647 return; 1648 // If the constant is in range, we can use any comparison. 1649 C.ICmpType = SystemZICMP::Any; 1650 } else 1651 return; 1652 1653 // Make sure that the first operand is an i32 of the right extension type. 1654 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ? 1655 ISD::SEXTLOAD : 1656 ISD::ZEXTLOAD); 1657 if (C.Op0.getValueType() != MVT::i32 || 1658 Load->getExtensionType() != ExtType) 1659 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, 1660 Load->getChain(), Load->getBasePtr(), 1661 Load->getPointerInfo(), Load->getMemoryVT(), 1662 Load->isVolatile(), Load->isNonTemporal(), 1663 Load->isInvariant(), Load->getAlignment()); 1664 1665 // Make sure that the second operand is an i32 with the right value. 1666 if (C.Op1.getValueType() != MVT::i32 || 1667 Value != ConstOp1->getZExtValue()) 1668 C.Op1 = DAG.getConstant(Value, DL, MVT::i32); 1669 } 1670 1671 // Return true if Op is either an unextended load, or a load suitable 1672 // for integer register-memory comparisons of type ICmpType. 1673 static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) { 1674 auto *Load = dyn_cast<LoadSDNode>(Op.getNode()); 1675 if (Load) { 1676 // There are no instructions to compare a register with a memory byte. 1677 if (Load->getMemoryVT() == MVT::i8) 1678 return false; 1679 // Otherwise decide on extension type. 1680 switch (Load->getExtensionType()) { 1681 case ISD::NON_EXTLOAD: 1682 return true; 1683 case ISD::SEXTLOAD: 1684 return ICmpType != SystemZICMP::UnsignedOnly; 1685 case ISD::ZEXTLOAD: 1686 return ICmpType != SystemZICMP::SignedOnly; 1687 default: 1688 break; 1689 } 1690 } 1691 return false; 1692 } 1693 1694 // Return true if it is better to swap the operands of C. 1695 static bool shouldSwapCmpOperands(const Comparison &C) { 1696 // Leave f128 comparisons alone, since they have no memory forms. 1697 if (C.Op0.getValueType() == MVT::f128) 1698 return false; 1699 1700 // Always keep a floating-point constant second, since comparisons with 1701 // zero can use LOAD TEST and comparisons with other constants make a 1702 // natural memory operand. 1703 if (isa<ConstantFPSDNode>(C.Op1)) 1704 return false; 1705 1706 // Never swap comparisons with zero since there are many ways to optimize 1707 // those later. 1708 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1); 1709 if (ConstOp1 && ConstOp1->getZExtValue() == 0) 1710 return false; 1711 1712 // Also keep natural memory operands second if the loaded value is 1713 // only used here. Several comparisons have memory forms. 1714 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse()) 1715 return false; 1716 1717 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't. 1718 // In that case we generally prefer the memory to be second. 1719 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) { 1720 // The only exceptions are when the second operand is a constant and 1721 // we can use things like CHHSI. 1722 if (!ConstOp1) 1723 return true; 1724 // The unsigned memory-immediate instructions can handle 16-bit 1725 // unsigned integers. 1726 if (C.ICmpType != SystemZICMP::SignedOnly && 1727 isUInt<16>(ConstOp1->getZExtValue())) 1728 return false; 1729 // The signed memory-immediate instructions can handle 16-bit 1730 // signed integers. 1731 if (C.ICmpType != SystemZICMP::UnsignedOnly && 1732 isInt<16>(ConstOp1->getSExtValue())) 1733 return false; 1734 return true; 1735 } 1736 1737 // Try to promote the use of CGFR and CLGFR. 1738 unsigned Opcode0 = C.Op0.getOpcode(); 1739 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND) 1740 return true; 1741 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND) 1742 return true; 1743 if (C.ICmpType != SystemZICMP::SignedOnly && 1744 Opcode0 == ISD::AND && 1745 C.Op0.getOperand(1).getOpcode() == ISD::Constant && 1746 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff) 1747 return true; 1748 1749 return false; 1750 } 1751 1752 // Return a version of comparison CC mask CCMask in which the LT and GT 1753 // actions are swapped. 1754 static unsigned reverseCCMask(unsigned CCMask) { 1755 return ((CCMask & SystemZ::CCMASK_CMP_EQ) | 1756 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) | 1757 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) | 1758 (CCMask & SystemZ::CCMASK_CMP_UO)); 1759 } 1760 1761 // Check whether C tests for equality between X and Y and whether X - Y 1762 // or Y - X is also computed. In that case it's better to compare the 1763 // result of the subtraction against zero. 1764 static void adjustForSubtraction(SelectionDAG &DAG, const SDLoc &DL, 1765 Comparison &C) { 1766 if (C.CCMask == SystemZ::CCMASK_CMP_EQ || 1767 C.CCMask == SystemZ::CCMASK_CMP_NE) { 1768 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) { 1769 SDNode *N = *I; 1770 if (N->getOpcode() == ISD::SUB && 1771 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) || 1772 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) { 1773 C.Op0 = SDValue(N, 0); 1774 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0)); 1775 return; 1776 } 1777 } 1778 } 1779 } 1780 1781 // Check whether C compares a floating-point value with zero and if that 1782 // floating-point value is also negated. In this case we can use the 1783 // negation to set CC, so avoiding separate LOAD AND TEST and 1784 // LOAD (NEGATIVE/COMPLEMENT) instructions. 1785 static void adjustForFNeg(Comparison &C) { 1786 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1); 1787 if (C1 && C1->isZero()) { 1788 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) { 1789 SDNode *N = *I; 1790 if (N->getOpcode() == ISD::FNEG) { 1791 C.Op0 = SDValue(N, 0); 1792 C.CCMask = reverseCCMask(C.CCMask); 1793 return; 1794 } 1795 } 1796 } 1797 } 1798 1799 // Check whether C compares (shl X, 32) with 0 and whether X is 1800 // also sign-extended. In that case it is better to test the result 1801 // of the sign extension using LTGFR. 1802 // 1803 // This case is important because InstCombine transforms a comparison 1804 // with (sext (trunc X)) into a comparison with (shl X, 32). 1805 static void adjustForLTGFR(Comparison &C) { 1806 // Check for a comparison between (shl X, 32) and 0. 1807 if (C.Op0.getOpcode() == ISD::SHL && 1808 C.Op0.getValueType() == MVT::i64 && 1809 C.Op1.getOpcode() == ISD::Constant && 1810 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) { 1811 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1)); 1812 if (C1 && C1->getZExtValue() == 32) { 1813 SDValue ShlOp0 = C.Op0.getOperand(0); 1814 // See whether X has any SIGN_EXTEND_INREG uses. 1815 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) { 1816 SDNode *N = *I; 1817 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG && 1818 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) { 1819 C.Op0 = SDValue(N, 0); 1820 return; 1821 } 1822 } 1823 } 1824 } 1825 } 1826 1827 // If C compares the truncation of an extending load, try to compare 1828 // the untruncated value instead. This exposes more opportunities to 1829 // reuse CC. 1830 static void adjustICmpTruncate(SelectionDAG &DAG, const SDLoc &DL, 1831 Comparison &C) { 1832 if (C.Op0.getOpcode() == ISD::TRUNCATE && 1833 C.Op0.getOperand(0).getOpcode() == ISD::LOAD && 1834 C.Op1.getOpcode() == ISD::Constant && 1835 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) { 1836 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0)); 1837 if (L->getMemoryVT().getStoreSizeInBits() 1838 <= C.Op0.getValueType().getSizeInBits()) { 1839 unsigned Type = L->getExtensionType(); 1840 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) || 1841 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) { 1842 C.Op0 = C.Op0.getOperand(0); 1843 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType()); 1844 } 1845 } 1846 } 1847 } 1848 1849 // Return true if shift operation N has an in-range constant shift value. 1850 // Store it in ShiftVal if so. 1851 static bool isSimpleShift(SDValue N, unsigned &ShiftVal) { 1852 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1)); 1853 if (!Shift) 1854 return false; 1855 1856 uint64_t Amount = Shift->getZExtValue(); 1857 if (Amount >= N.getValueType().getSizeInBits()) 1858 return false; 1859 1860 ShiftVal = Amount; 1861 return true; 1862 } 1863 1864 // Check whether an AND with Mask is suitable for a TEST UNDER MASK 1865 // instruction and whether the CC value is descriptive enough to handle 1866 // a comparison of type Opcode between the AND result and CmpVal. 1867 // CCMask says which comparison result is being tested and BitSize is 1868 // the number of bits in the operands. If TEST UNDER MASK can be used, 1869 // return the corresponding CC mask, otherwise return 0. 1870 static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask, 1871 uint64_t Mask, uint64_t CmpVal, 1872 unsigned ICmpType) { 1873 assert(Mask != 0 && "ANDs with zero should have been removed by now"); 1874 1875 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL. 1876 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) && 1877 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask)) 1878 return 0; 1879 1880 // Work out the masks for the lowest and highest bits. 1881 unsigned HighShift = 63 - countLeadingZeros(Mask); 1882 uint64_t High = uint64_t(1) << HighShift; 1883 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask); 1884 1885 // Signed ordered comparisons are effectively unsigned if the sign 1886 // bit is dropped. 1887 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly); 1888 1889 // Check for equality comparisons with 0, or the equivalent. 1890 if (CmpVal == 0) { 1891 if (CCMask == SystemZ::CCMASK_CMP_EQ) 1892 return SystemZ::CCMASK_TM_ALL_0; 1893 if (CCMask == SystemZ::CCMASK_CMP_NE) 1894 return SystemZ::CCMASK_TM_SOME_1; 1895 } 1896 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) { 1897 if (CCMask == SystemZ::CCMASK_CMP_LT) 1898 return SystemZ::CCMASK_TM_ALL_0; 1899 if (CCMask == SystemZ::CCMASK_CMP_GE) 1900 return SystemZ::CCMASK_TM_SOME_1; 1901 } 1902 if (EffectivelyUnsigned && CmpVal < Low) { 1903 if (CCMask == SystemZ::CCMASK_CMP_LE) 1904 return SystemZ::CCMASK_TM_ALL_0; 1905 if (CCMask == SystemZ::CCMASK_CMP_GT) 1906 return SystemZ::CCMASK_TM_SOME_1; 1907 } 1908 1909 // Check for equality comparisons with the mask, or the equivalent. 1910 if (CmpVal == Mask) { 1911 if (CCMask == SystemZ::CCMASK_CMP_EQ) 1912 return SystemZ::CCMASK_TM_ALL_1; 1913 if (CCMask == SystemZ::CCMASK_CMP_NE) 1914 return SystemZ::CCMASK_TM_SOME_0; 1915 } 1916 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) { 1917 if (CCMask == SystemZ::CCMASK_CMP_GT) 1918 return SystemZ::CCMASK_TM_ALL_1; 1919 if (CCMask == SystemZ::CCMASK_CMP_LE) 1920 return SystemZ::CCMASK_TM_SOME_0; 1921 } 1922 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) { 1923 if (CCMask == SystemZ::CCMASK_CMP_GE) 1924 return SystemZ::CCMASK_TM_ALL_1; 1925 if (CCMask == SystemZ::CCMASK_CMP_LT) 1926 return SystemZ::CCMASK_TM_SOME_0; 1927 } 1928 1929 // Check for ordered comparisons with the top bit. 1930 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) { 1931 if (CCMask == SystemZ::CCMASK_CMP_LE) 1932 return SystemZ::CCMASK_TM_MSB_0; 1933 if (CCMask == SystemZ::CCMASK_CMP_GT) 1934 return SystemZ::CCMASK_TM_MSB_1; 1935 } 1936 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) { 1937 if (CCMask == SystemZ::CCMASK_CMP_LT) 1938 return SystemZ::CCMASK_TM_MSB_0; 1939 if (CCMask == SystemZ::CCMASK_CMP_GE) 1940 return SystemZ::CCMASK_TM_MSB_1; 1941 } 1942 1943 // If there are just two bits, we can do equality checks for Low and High 1944 // as well. 1945 if (Mask == Low + High) { 1946 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low) 1947 return SystemZ::CCMASK_TM_MIXED_MSB_0; 1948 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low) 1949 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY; 1950 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High) 1951 return SystemZ::CCMASK_TM_MIXED_MSB_1; 1952 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High) 1953 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY; 1954 } 1955 1956 // Looks like we've exhausted our options. 1957 return 0; 1958 } 1959 1960 // See whether C can be implemented as a TEST UNDER MASK instruction. 1961 // Update the arguments with the TM version if so. 1962 static void adjustForTestUnderMask(SelectionDAG &DAG, const SDLoc &DL, 1963 Comparison &C) { 1964 // Check that we have a comparison with a constant. 1965 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1); 1966 if (!ConstOp1) 1967 return; 1968 uint64_t CmpVal = ConstOp1->getZExtValue(); 1969 1970 // Check whether the nonconstant input is an AND with a constant mask. 1971 Comparison NewC(C); 1972 uint64_t MaskVal; 1973 ConstantSDNode *Mask = nullptr; 1974 if (C.Op0.getOpcode() == ISD::AND) { 1975 NewC.Op0 = C.Op0.getOperand(0); 1976 NewC.Op1 = C.Op0.getOperand(1); 1977 Mask = dyn_cast<ConstantSDNode>(NewC.Op1); 1978 if (!Mask) 1979 return; 1980 MaskVal = Mask->getZExtValue(); 1981 } else { 1982 // There is no instruction to compare with a 64-bit immediate 1983 // so use TMHH instead if possible. We need an unsigned ordered 1984 // comparison with an i64 immediate. 1985 if (NewC.Op0.getValueType() != MVT::i64 || 1986 NewC.CCMask == SystemZ::CCMASK_CMP_EQ || 1987 NewC.CCMask == SystemZ::CCMASK_CMP_NE || 1988 NewC.ICmpType == SystemZICMP::SignedOnly) 1989 return; 1990 // Convert LE and GT comparisons into LT and GE. 1991 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE || 1992 NewC.CCMask == SystemZ::CCMASK_CMP_GT) { 1993 if (CmpVal == uint64_t(-1)) 1994 return; 1995 CmpVal += 1; 1996 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ; 1997 } 1998 // If the low N bits of Op1 are zero than the low N bits of Op0 can 1999 // be masked off without changing the result. 2000 MaskVal = -(CmpVal & -CmpVal); 2001 NewC.ICmpType = SystemZICMP::UnsignedOnly; 2002 } 2003 if (!MaskVal) 2004 return; 2005 2006 // Check whether the combination of mask, comparison value and comparison 2007 // type are suitable. 2008 unsigned BitSize = NewC.Op0.getValueType().getSizeInBits(); 2009 unsigned NewCCMask, ShiftVal; 2010 if (NewC.ICmpType != SystemZICMP::SignedOnly && 2011 NewC.Op0.getOpcode() == ISD::SHL && 2012 isSimpleShift(NewC.Op0, ShiftVal) && 2013 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, 2014 MaskVal >> ShiftVal, 2015 CmpVal >> ShiftVal, 2016 SystemZICMP::Any))) { 2017 NewC.Op0 = NewC.Op0.getOperand(0); 2018 MaskVal >>= ShiftVal; 2019 } else if (NewC.ICmpType != SystemZICMP::SignedOnly && 2020 NewC.Op0.getOpcode() == ISD::SRL && 2021 isSimpleShift(NewC.Op0, ShiftVal) && 2022 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, 2023 MaskVal << ShiftVal, 2024 CmpVal << ShiftVal, 2025 SystemZICMP::UnsignedOnly))) { 2026 NewC.Op0 = NewC.Op0.getOperand(0); 2027 MaskVal <<= ShiftVal; 2028 } else { 2029 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal, 2030 NewC.ICmpType); 2031 if (!NewCCMask) 2032 return; 2033 } 2034 2035 // Go ahead and make the change. 2036 C.Opcode = SystemZISD::TM; 2037 C.Op0 = NewC.Op0; 2038 if (Mask && Mask->getZExtValue() == MaskVal) 2039 C.Op1 = SDValue(Mask, 0); 2040 else 2041 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType()); 2042 C.CCValid = SystemZ::CCMASK_TM; 2043 C.CCMask = NewCCMask; 2044 } 2045 2046 // Return a Comparison that tests the condition-code result of intrinsic 2047 // node Call against constant integer CC using comparison code Cond. 2048 // Opcode is the opcode of the SystemZISD operation for the intrinsic 2049 // and CCValid is the set of possible condition-code results. 2050 static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode, 2051 SDValue Call, unsigned CCValid, uint64_t CC, 2052 ISD::CondCode Cond) { 2053 Comparison C(Call, SDValue()); 2054 C.Opcode = Opcode; 2055 C.CCValid = CCValid; 2056 if (Cond == ISD::SETEQ) 2057 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3. 2058 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0; 2059 else if (Cond == ISD::SETNE) 2060 // ...and the inverse of that. 2061 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1; 2062 else if (Cond == ISD::SETLT || Cond == ISD::SETULT) 2063 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3, 2064 // always true for CC>3. 2065 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1; 2066 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE) 2067 // ...and the inverse of that. 2068 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0; 2069 else if (Cond == ISD::SETLE || Cond == ISD::SETULE) 2070 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true), 2071 // always true for CC>3. 2072 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1; 2073 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT) 2074 // ...and the inverse of that. 2075 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0; 2076 else 2077 llvm_unreachable("Unexpected integer comparison type"); 2078 C.CCMask &= CCValid; 2079 return C; 2080 } 2081 2082 // Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1. 2083 static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1, 2084 ISD::CondCode Cond, const SDLoc &DL) { 2085 if (CmpOp1.getOpcode() == ISD::Constant) { 2086 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue(); 2087 unsigned Opcode, CCValid; 2088 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN && 2089 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) && 2090 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid)) 2091 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond); 2092 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN && 2093 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 && 2094 isIntrinsicWithCC(CmpOp0, Opcode, CCValid)) 2095 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond); 2096 } 2097 Comparison C(CmpOp0, CmpOp1); 2098 C.CCMask = CCMaskForCondCode(Cond); 2099 if (C.Op0.getValueType().isFloatingPoint()) { 2100 C.CCValid = SystemZ::CCMASK_FCMP; 2101 C.Opcode = SystemZISD::FCMP; 2102 adjustForFNeg(C); 2103 } else { 2104 C.CCValid = SystemZ::CCMASK_ICMP; 2105 C.Opcode = SystemZISD::ICMP; 2106 // Choose the type of comparison. Equality and inequality tests can 2107 // use either signed or unsigned comparisons. The choice also doesn't 2108 // matter if both sign bits are known to be clear. In those cases we 2109 // want to give the main isel code the freedom to choose whichever 2110 // form fits best. 2111 if (C.CCMask == SystemZ::CCMASK_CMP_EQ || 2112 C.CCMask == SystemZ::CCMASK_CMP_NE || 2113 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1))) 2114 C.ICmpType = SystemZICMP::Any; 2115 else if (C.CCMask & SystemZ::CCMASK_CMP_UO) 2116 C.ICmpType = SystemZICMP::UnsignedOnly; 2117 else 2118 C.ICmpType = SystemZICMP::SignedOnly; 2119 C.CCMask &= ~SystemZ::CCMASK_CMP_UO; 2120 adjustZeroCmp(DAG, DL, C); 2121 adjustSubwordCmp(DAG, DL, C); 2122 adjustForSubtraction(DAG, DL, C); 2123 adjustForLTGFR(C); 2124 adjustICmpTruncate(DAG, DL, C); 2125 } 2126 2127 if (shouldSwapCmpOperands(C)) { 2128 std::swap(C.Op0, C.Op1); 2129 C.CCMask = reverseCCMask(C.CCMask); 2130 } 2131 2132 adjustForTestUnderMask(DAG, DL, C); 2133 return C; 2134 } 2135 2136 // Emit the comparison instruction described by C. 2137 static SDValue emitCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) { 2138 if (!C.Op1.getNode()) { 2139 SDValue Op; 2140 switch (C.Op0.getOpcode()) { 2141 case ISD::INTRINSIC_W_CHAIN: 2142 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode); 2143 break; 2144 case ISD::INTRINSIC_WO_CHAIN: 2145 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode); 2146 break; 2147 default: 2148 llvm_unreachable("Invalid comparison operands"); 2149 } 2150 return SDValue(Op.getNode(), Op->getNumValues() - 1); 2151 } 2152 if (C.Opcode == SystemZISD::ICMP) 2153 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1, 2154 DAG.getConstant(C.ICmpType, DL, MVT::i32)); 2155 if (C.Opcode == SystemZISD::TM) { 2156 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) != 2157 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1)); 2158 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1, 2159 DAG.getConstant(RegisterOnly, DL, MVT::i32)); 2160 } 2161 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1); 2162 } 2163 2164 // Implement a 32-bit *MUL_LOHI operation by extending both operands to 2165 // 64 bits. Extend is the extension type to use. Store the high part 2166 // in Hi and the low part in Lo. 2167 static void lowerMUL_LOHI32(SelectionDAG &DAG, const SDLoc &DL, unsigned Extend, 2168 SDValue Op0, SDValue Op1, SDValue &Hi, 2169 SDValue &Lo) { 2170 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0); 2171 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1); 2172 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1); 2173 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, 2174 DAG.getConstant(32, DL, MVT::i64)); 2175 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi); 2176 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul); 2177 } 2178 2179 // Lower a binary operation that produces two VT results, one in each 2180 // half of a GR128 pair. Op0 and Op1 are the VT operands to the operation, 2181 // Extend extends Op0 to a GR128, and Opcode performs the GR128 operation 2182 // on the extended Op0 and (unextended) Op1. Store the even register result 2183 // in Even and the odd register result in Odd. 2184 static void lowerGR128Binary(SelectionDAG &DAG, const SDLoc &DL, EVT VT, 2185 unsigned Extend, unsigned Opcode, SDValue Op0, 2186 SDValue Op1, SDValue &Even, SDValue &Odd) { 2187 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0); 2188 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped, 2189 SDValue(In128, 0), Op1); 2190 bool Is32Bit = is32Bit(VT); 2191 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result); 2192 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result); 2193 } 2194 2195 // Return an i32 value that is 1 if the CC value produced by Glue is 2196 // in the mask CCMask and 0 otherwise. CC is known to have a value 2197 // in CCValid, so other values can be ignored. 2198 static SDValue emitSETCC(SelectionDAG &DAG, const SDLoc &DL, SDValue Glue, 2199 unsigned CCValid, unsigned CCMask) { 2200 IPMConversion Conversion = getIPMConversion(CCValid, CCMask); 2201 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue); 2202 2203 if (Conversion.XORValue) 2204 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result, 2205 DAG.getConstant(Conversion.XORValue, DL, MVT::i32)); 2206 2207 if (Conversion.AddValue) 2208 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result, 2209 DAG.getConstant(Conversion.AddValue, DL, MVT::i32)); 2210 2211 // The SHR/AND sequence should get optimized to an RISBG. 2212 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result, 2213 DAG.getConstant(Conversion.Bit, DL, MVT::i32)); 2214 if (Conversion.Bit != 31) 2215 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result, 2216 DAG.getConstant(1, DL, MVT::i32)); 2217 return Result; 2218 } 2219 2220 // Return the SystemISD vector comparison operation for CC, or 0 if it cannot 2221 // be done directly. IsFP is true if CC is for a floating-point rather than 2222 // integer comparison. 2223 static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) { 2224 switch (CC) { 2225 case ISD::SETOEQ: 2226 case ISD::SETEQ: 2227 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE; 2228 2229 case ISD::SETOGE: 2230 case ISD::SETGE: 2231 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0); 2232 2233 case ISD::SETOGT: 2234 case ISD::SETGT: 2235 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH; 2236 2237 case ISD::SETUGT: 2238 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL; 2239 2240 default: 2241 return 0; 2242 } 2243 } 2244 2245 // Return the SystemZISD vector comparison operation for CC or its inverse, 2246 // or 0 if neither can be done directly. Indicate in Invert whether the 2247 // result is for the inverse of CC. IsFP is true if CC is for a 2248 // floating-point rather than integer comparison. 2249 static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP, 2250 bool &Invert) { 2251 if (unsigned Opcode = getVectorComparison(CC, IsFP)) { 2252 Invert = false; 2253 return Opcode; 2254 } 2255 2256 CC = ISD::getSetCCInverse(CC, !IsFP); 2257 if (unsigned Opcode = getVectorComparison(CC, IsFP)) { 2258 Invert = true; 2259 return Opcode; 2260 } 2261 2262 return 0; 2263 } 2264 2265 // Return a v2f64 that contains the extended form of elements Start and Start+1 2266 // of v4f32 value Op. 2267 static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, const SDLoc &DL, 2268 SDValue Op) { 2269 int Mask[] = { Start, -1, Start + 1, -1 }; 2270 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask); 2271 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op); 2272 } 2273 2274 // Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode, 2275 // producing a result of type VT. 2276 static SDValue getVectorCmp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &DL, 2277 EVT VT, SDValue CmpOp0, SDValue CmpOp1) { 2278 // There is no hardware support for v4f32, so extend the vector into 2279 // two v2f64s and compare those. 2280 if (CmpOp0.getValueType() == MVT::v4f32) { 2281 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0); 2282 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0); 2283 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1); 2284 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1); 2285 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1); 2286 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1); 2287 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes); 2288 } 2289 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1); 2290 } 2291 2292 // Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing 2293 // an integer mask of type VT. 2294 static SDValue lowerVectorSETCC(SelectionDAG &DAG, const SDLoc &DL, EVT VT, 2295 ISD::CondCode CC, SDValue CmpOp0, 2296 SDValue CmpOp1) { 2297 bool IsFP = CmpOp0.getValueType().isFloatingPoint(); 2298 bool Invert = false; 2299 SDValue Cmp; 2300 switch (CC) { 2301 // Handle tests for order using (or (ogt y x) (oge x y)). 2302 case ISD::SETUO: 2303 Invert = true; 2304 case ISD::SETO: { 2305 assert(IsFP && "Unexpected integer comparison"); 2306 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0); 2307 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1); 2308 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE); 2309 break; 2310 } 2311 2312 // Handle <> tests using (or (ogt y x) (ogt x y)). 2313 case ISD::SETUEQ: 2314 Invert = true; 2315 case ISD::SETONE: { 2316 assert(IsFP && "Unexpected integer comparison"); 2317 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0); 2318 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1); 2319 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT); 2320 break; 2321 } 2322 2323 // Otherwise a single comparison is enough. It doesn't really 2324 // matter whether we try the inversion or the swap first, since 2325 // there are no cases where both work. 2326 default: 2327 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert)) 2328 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1); 2329 else { 2330 CC = ISD::getSetCCSwappedOperands(CC); 2331 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert)) 2332 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0); 2333 else 2334 llvm_unreachable("Unhandled comparison"); 2335 } 2336 break; 2337 } 2338 if (Invert) { 2339 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, 2340 DAG.getConstant(65535, DL, MVT::i32)); 2341 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask); 2342 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask); 2343 } 2344 return Cmp; 2345 } 2346 2347 SDValue SystemZTargetLowering::lowerSETCC(SDValue Op, 2348 SelectionDAG &DAG) const { 2349 SDValue CmpOp0 = Op.getOperand(0); 2350 SDValue CmpOp1 = Op.getOperand(1); 2351 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 2352 SDLoc DL(Op); 2353 EVT VT = Op.getValueType(); 2354 if (VT.isVector()) 2355 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1); 2356 2357 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL)); 2358 SDValue Glue = emitCmp(DAG, DL, C); 2359 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask); 2360 } 2361 2362 SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 2363 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 2364 SDValue CmpOp0 = Op.getOperand(2); 2365 SDValue CmpOp1 = Op.getOperand(3); 2366 SDValue Dest = Op.getOperand(4); 2367 SDLoc DL(Op); 2368 2369 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL)); 2370 SDValue Glue = emitCmp(DAG, DL, C); 2371 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(), 2372 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32), 2373 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue); 2374 } 2375 2376 // Return true if Pos is CmpOp and Neg is the negative of CmpOp, 2377 // allowing Pos and Neg to be wider than CmpOp. 2378 static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) { 2379 return (Neg.getOpcode() == ISD::SUB && 2380 Neg.getOperand(0).getOpcode() == ISD::Constant && 2381 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 && 2382 Neg.getOperand(1) == Pos && 2383 (Pos == CmpOp || 2384 (Pos.getOpcode() == ISD::SIGN_EXTEND && 2385 Pos.getOperand(0) == CmpOp))); 2386 } 2387 2388 // Return the absolute or negative absolute of Op; IsNegative decides which. 2389 static SDValue getAbsolute(SelectionDAG &DAG, const SDLoc &DL, SDValue Op, 2390 bool IsNegative) { 2391 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op); 2392 if (IsNegative) 2393 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(), 2394 DAG.getConstant(0, DL, Op.getValueType()), Op); 2395 return Op; 2396 } 2397 2398 SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op, 2399 SelectionDAG &DAG) const { 2400 SDValue CmpOp0 = Op.getOperand(0); 2401 SDValue CmpOp1 = Op.getOperand(1); 2402 SDValue TrueOp = Op.getOperand(2); 2403 SDValue FalseOp = Op.getOperand(3); 2404 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 2405 SDLoc DL(Op); 2406 2407 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL)); 2408 2409 // Check for absolute and negative-absolute selections, including those 2410 // where the comparison value is sign-extended (for LPGFR and LNGFR). 2411 // This check supplements the one in DAGCombiner. 2412 if (C.Opcode == SystemZISD::ICMP && 2413 C.CCMask != SystemZ::CCMASK_CMP_EQ && 2414 C.CCMask != SystemZ::CCMASK_CMP_NE && 2415 C.Op1.getOpcode() == ISD::Constant && 2416 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) { 2417 if (isAbsolute(C.Op0, TrueOp, FalseOp)) 2418 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT); 2419 if (isAbsolute(C.Op0, FalseOp, TrueOp)) 2420 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT); 2421 } 2422 2423 SDValue Glue = emitCmp(DAG, DL, C); 2424 2425 // Special case for handling -1/0 results. The shifts we use here 2426 // should get optimized with the IPM conversion sequence. 2427 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp); 2428 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp); 2429 if (TrueC && FalseC) { 2430 int64_t TrueVal = TrueC->getSExtValue(); 2431 int64_t FalseVal = FalseC->getSExtValue(); 2432 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) { 2433 // Invert the condition if we want -1 on false. 2434 if (TrueVal == 0) 2435 C.CCMask ^= C.CCValid; 2436 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask); 2437 EVT VT = Op.getValueType(); 2438 // Extend the result to VT. Upper bits are ignored. 2439 if (!is32Bit(VT)) 2440 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result); 2441 // Sign-extend from the low bit. 2442 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32); 2443 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt); 2444 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt); 2445 } 2446 } 2447 2448 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32), 2449 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue}; 2450 2451 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); 2452 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops); 2453 } 2454 2455 SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node, 2456 SelectionDAG &DAG) const { 2457 SDLoc DL(Node); 2458 const GlobalValue *GV = Node->getGlobal(); 2459 int64_t Offset = Node->getOffset(); 2460 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2461 Reloc::Model RM = DAG.getTarget().getRelocationModel(); 2462 CodeModel::Model CM = DAG.getTarget().getCodeModel(); 2463 2464 SDValue Result; 2465 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) { 2466 // Assign anchors at 1<<12 byte boundaries. 2467 uint64_t Anchor = Offset & ~uint64_t(0xfff); 2468 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor); 2469 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); 2470 2471 // The offset can be folded into the address if it is aligned to a halfword. 2472 Offset -= Anchor; 2473 if (Offset != 0 && (Offset & 1) == 0) { 2474 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset); 2475 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result); 2476 Offset = 0; 2477 } 2478 } else { 2479 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT); 2480 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); 2481 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result, 2482 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 2483 false, false, false, 0); 2484 } 2485 2486 // If there was a non-zero offset that we didn't fold, create an explicit 2487 // addition for it. 2488 if (Offset != 0) 2489 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result, 2490 DAG.getConstant(Offset, DL, PtrVT)); 2491 2492 return Result; 2493 } 2494 2495 SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node, 2496 SelectionDAG &DAG, 2497 unsigned Opcode, 2498 SDValue GOTOffset) const { 2499 SDLoc DL(Node); 2500 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2501 SDValue Chain = DAG.getEntryNode(); 2502 SDValue Glue; 2503 2504 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12. 2505 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT); 2506 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue); 2507 Glue = Chain.getValue(1); 2508 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue); 2509 Glue = Chain.getValue(1); 2510 2511 // The first call operand is the chain and the second is the TLS symbol. 2512 SmallVector<SDValue, 8> Ops; 2513 Ops.push_back(Chain); 2514 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL, 2515 Node->getValueType(0), 2516 0, 0)); 2517 2518 // Add argument registers to the end of the list so that they are 2519 // known live into the call. 2520 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT)); 2521 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT)); 2522 2523 // Add a register mask operand representing the call-preserved registers. 2524 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); 2525 const uint32_t *Mask = 2526 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C); 2527 assert(Mask && "Missing call preserved mask for calling convention"); 2528 Ops.push_back(DAG.getRegisterMask(Mask)); 2529 2530 // Glue the call to the argument copies. 2531 Ops.push_back(Glue); 2532 2533 // Emit the call. 2534 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 2535 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops); 2536 Glue = Chain.getValue(1); 2537 2538 // Copy the return value from %r2. 2539 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue); 2540 } 2541 2542 SDValue SystemZTargetLowering::lowerThreadPointer(const SDLoc &DL, 2543 SelectionDAG &DAG) const { 2544 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2545 2546 // The high part of the thread pointer is in access register 0. 2547 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32, 2548 DAG.getConstant(0, DL, MVT::i32)); 2549 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi); 2550 2551 // The low part of the thread pointer is in access register 1. 2552 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32, 2553 DAG.getConstant(1, DL, MVT::i32)); 2554 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo); 2555 2556 // Merge them into a single 64-bit address. 2557 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi, 2558 DAG.getConstant(32, DL, PtrVT)); 2559 return DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo); 2560 } 2561 2562 SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node, 2563 SelectionDAG &DAG) const { 2564 if (DAG.getTarget().Options.EmulatedTLS) 2565 return LowerToTLSEmulatedModel(Node, DAG); 2566 SDLoc DL(Node); 2567 const GlobalValue *GV = Node->getGlobal(); 2568 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2569 TLSModel::Model model = DAG.getTarget().getTLSModel(GV); 2570 2571 SDValue TP = lowerThreadPointer(DL, DAG); 2572 2573 // Get the offset of GA from the thread pointer, based on the TLS model. 2574 SDValue Offset; 2575 switch (model) { 2576 case TLSModel::GeneralDynamic: { 2577 // Load the GOT offset of the tls_index (module ID / per-symbol offset). 2578 SystemZConstantPoolValue *CPV = 2579 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD); 2580 2581 Offset = DAG.getConstantPool(CPV, PtrVT, 8); 2582 Offset = DAG.getLoad( 2583 PtrVT, DL, DAG.getEntryNode(), Offset, 2584 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 2585 false, false, 0); 2586 2587 // Call __tls_get_offset to retrieve the offset. 2588 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset); 2589 break; 2590 } 2591 2592 case TLSModel::LocalDynamic: { 2593 // Load the GOT offset of the module ID. 2594 SystemZConstantPoolValue *CPV = 2595 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM); 2596 2597 Offset = DAG.getConstantPool(CPV, PtrVT, 8); 2598 Offset = DAG.getLoad( 2599 PtrVT, DL, DAG.getEntryNode(), Offset, 2600 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 2601 false, false, 0); 2602 2603 // Call __tls_get_offset to retrieve the module base offset. 2604 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset); 2605 2606 // Note: The SystemZLDCleanupPass will remove redundant computations 2607 // of the module base offset. Count total number of local-dynamic 2608 // accesses to trigger execution of that pass. 2609 SystemZMachineFunctionInfo* MFI = 2610 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>(); 2611 MFI->incNumLocalDynamicTLSAccesses(); 2612 2613 // Add the per-symbol offset. 2614 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF); 2615 2616 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8); 2617 DTPOffset = DAG.getLoad( 2618 PtrVT, DL, DAG.getEntryNode(), DTPOffset, 2619 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 2620 false, false, 0); 2621 2622 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset); 2623 break; 2624 } 2625 2626 case TLSModel::InitialExec: { 2627 // Load the offset from the GOT. 2628 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 2629 SystemZII::MO_INDNTPOFF); 2630 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset); 2631 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset, 2632 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 2633 false, false, false, 0); 2634 break; 2635 } 2636 2637 case TLSModel::LocalExec: { 2638 // Force the offset into the constant pool and load it from there. 2639 SystemZConstantPoolValue *CPV = 2640 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF); 2641 2642 Offset = DAG.getConstantPool(CPV, PtrVT, 8); 2643 Offset = DAG.getLoad( 2644 PtrVT, DL, DAG.getEntryNode(), Offset, 2645 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 2646 false, false, 0); 2647 break; 2648 } 2649 } 2650 2651 // Add the base and offset together. 2652 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset); 2653 } 2654 2655 SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node, 2656 SelectionDAG &DAG) const { 2657 SDLoc DL(Node); 2658 const BlockAddress *BA = Node->getBlockAddress(); 2659 int64_t Offset = Node->getOffset(); 2660 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2661 2662 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset); 2663 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); 2664 return Result; 2665 } 2666 2667 SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT, 2668 SelectionDAG &DAG) const { 2669 SDLoc DL(JT); 2670 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2671 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); 2672 2673 // Use LARL to load the address of the table. 2674 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); 2675 } 2676 2677 SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP, 2678 SelectionDAG &DAG) const { 2679 SDLoc DL(CP); 2680 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2681 2682 SDValue Result; 2683 if (CP->isMachineConstantPoolEntry()) 2684 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, 2685 CP->getAlignment()); 2686 else 2687 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, 2688 CP->getAlignment(), CP->getOffset()); 2689 2690 // Use LARL to load the address of the constant pool entry. 2691 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); 2692 } 2693 2694 SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op, 2695 SelectionDAG &DAG) const { 2696 MachineFunction &MF = DAG.getMachineFunction(); 2697 MachineFrameInfo *MFI = MF.getFrameInfo(); 2698 MFI->setFrameAddressIsTaken(true); 2699 2700 SDLoc DL(Op); 2701 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2702 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2703 2704 // If the back chain frame index has not been allocated yet, do so. 2705 SystemZMachineFunctionInfo *FI = MF.getInfo<SystemZMachineFunctionInfo>(); 2706 int BackChainIdx = FI->getFramePointerSaveIndex(); 2707 if (!BackChainIdx) { 2708 // By definition, the frame address is the address of the back chain. 2709 BackChainIdx = MFI->CreateFixedObject(8, -SystemZMC::CallFrameSize, false); 2710 FI->setFramePointerSaveIndex(BackChainIdx); 2711 } 2712 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT); 2713 2714 // FIXME The frontend should detect this case. 2715 if (Depth > 0) { 2716 report_fatal_error("Unsupported stack frame traversal count"); 2717 } 2718 2719 return BackChain; 2720 } 2721 2722 SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op, 2723 SelectionDAG &DAG) const { 2724 MachineFunction &MF = DAG.getMachineFunction(); 2725 MachineFrameInfo *MFI = MF.getFrameInfo(); 2726 MFI->setReturnAddressIsTaken(true); 2727 2728 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 2729 return SDValue(); 2730 2731 SDLoc DL(Op); 2732 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2733 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2734 2735 // FIXME The frontend should detect this case. 2736 if (Depth > 0) { 2737 report_fatal_error("Unsupported stack frame traversal count"); 2738 } 2739 2740 // Return R14D, which has the return address. Mark it an implicit live-in. 2741 unsigned LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass); 2742 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT); 2743 } 2744 2745 SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op, 2746 SelectionDAG &DAG) const { 2747 SDLoc DL(Op); 2748 SDValue In = Op.getOperand(0); 2749 EVT InVT = In.getValueType(); 2750 EVT ResVT = Op.getValueType(); 2751 2752 // Convert loads directly. This is normally done by DAGCombiner, 2753 // but we need this case for bitcasts that are created during lowering 2754 // and which are then lowered themselves. 2755 if (auto *LoadN = dyn_cast<LoadSDNode>(In)) 2756 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(), 2757 LoadN->getMemOperand()); 2758 2759 if (InVT == MVT::i32 && ResVT == MVT::f32) { 2760 SDValue In64; 2761 if (Subtarget.hasHighWord()) { 2762 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, 2763 MVT::i64); 2764 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL, 2765 MVT::i64, SDValue(U64, 0), In); 2766 } else { 2767 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In); 2768 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64, 2769 DAG.getConstant(32, DL, MVT::i64)); 2770 } 2771 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64); 2772 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32, 2773 DL, MVT::f32, Out64); 2774 } 2775 if (InVT == MVT::f32 && ResVT == MVT::i32) { 2776 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64); 2777 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL, 2778 MVT::f64, SDValue(U64, 0), In); 2779 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64); 2780 if (Subtarget.hasHighWord()) 2781 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL, 2782 MVT::i32, Out64); 2783 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64, 2784 DAG.getConstant(32, DL, MVT::i64)); 2785 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift); 2786 } 2787 llvm_unreachable("Unexpected bitcast combination"); 2788 } 2789 2790 SDValue SystemZTargetLowering::lowerVASTART(SDValue Op, 2791 SelectionDAG &DAG) const { 2792 MachineFunction &MF = DAG.getMachineFunction(); 2793 SystemZMachineFunctionInfo *FuncInfo = 2794 MF.getInfo<SystemZMachineFunctionInfo>(); 2795 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2796 2797 SDValue Chain = Op.getOperand(0); 2798 SDValue Addr = Op.getOperand(1); 2799 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 2800 SDLoc DL(Op); 2801 2802 // The initial values of each field. 2803 const unsigned NumFields = 4; 2804 SDValue Fields[NumFields] = { 2805 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT), 2806 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT), 2807 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT), 2808 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT) 2809 }; 2810 2811 // Store each field into its respective slot. 2812 SDValue MemOps[NumFields]; 2813 unsigned Offset = 0; 2814 for (unsigned I = 0; I < NumFields; ++I) { 2815 SDValue FieldAddr = Addr; 2816 if (Offset != 0) 2817 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr, 2818 DAG.getIntPtrConstant(Offset, DL)); 2819 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr, 2820 MachinePointerInfo(SV, Offset), 2821 false, false, 0); 2822 Offset += 8; 2823 } 2824 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps); 2825 } 2826 2827 SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op, 2828 SelectionDAG &DAG) const { 2829 SDValue Chain = Op.getOperand(0); 2830 SDValue DstPtr = Op.getOperand(1); 2831 SDValue SrcPtr = Op.getOperand(2); 2832 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue(); 2833 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); 2834 SDLoc DL(Op); 2835 2836 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL), 2837 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false, 2838 /*isTailCall*/false, 2839 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV)); 2840 } 2841 2842 SDValue SystemZTargetLowering:: 2843 lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const { 2844 const TargetFrameLowering *TFI = Subtarget.getFrameLowering(); 2845 MachineFunction &MF = DAG.getMachineFunction(); 2846 bool RealignOpt = !MF.getFunction()-> hasFnAttribute("no-realign-stack"); 2847 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain"); 2848 2849 SDValue Chain = Op.getOperand(0); 2850 SDValue Size = Op.getOperand(1); 2851 SDValue Align = Op.getOperand(2); 2852 SDLoc DL(Op); 2853 2854 // If user has set the no alignment function attribute, ignore 2855 // alloca alignments. 2856 uint64_t AlignVal = (RealignOpt ? 2857 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0); 2858 2859 uint64_t StackAlign = TFI->getStackAlignment(); 2860 uint64_t RequiredAlign = std::max(AlignVal, StackAlign); 2861 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign; 2862 2863 unsigned SPReg = getStackPointerRegisterToSaveRestore(); 2864 SDValue NeededSpace = Size; 2865 2866 // Get a reference to the stack pointer. 2867 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64); 2868 2869 // If we need a backchain, save it now. 2870 SDValue Backchain; 2871 if (StoreBackchain) 2872 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo(), 2873 false, false, false, 0); 2874 2875 // Add extra space for alignment if needed. 2876 if (ExtraAlignSpace) 2877 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace, 2878 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64)); 2879 2880 // Get the new stack pointer value. 2881 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace); 2882 2883 // Copy the new stack pointer back. 2884 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP); 2885 2886 // The allocated data lives above the 160 bytes allocated for the standard 2887 // frame, plus any outgoing stack arguments. We don't know how much that 2888 // amounts to yet, so emit a special ADJDYNALLOC placeholder. 2889 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64); 2890 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust); 2891 2892 // Dynamically realign if needed. 2893 if (RequiredAlign > StackAlign) { 2894 Result = 2895 DAG.getNode(ISD::ADD, DL, MVT::i64, Result, 2896 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64)); 2897 Result = 2898 DAG.getNode(ISD::AND, DL, MVT::i64, Result, 2899 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64)); 2900 } 2901 2902 if (StoreBackchain) 2903 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo(), 2904 false, false, 0); 2905 2906 SDValue Ops[2] = { Result, Chain }; 2907 return DAG.getMergeValues(Ops, DL); 2908 } 2909 2910 SDValue SystemZTargetLowering::lowerGET_DYNAMIC_AREA_OFFSET( 2911 SDValue Op, SelectionDAG &DAG) const { 2912 SDLoc DL(Op); 2913 2914 return DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64); 2915 } 2916 2917 SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op, 2918 SelectionDAG &DAG) const { 2919 EVT VT = Op.getValueType(); 2920 SDLoc DL(Op); 2921 SDValue Ops[2]; 2922 if (is32Bit(VT)) 2923 // Just do a normal 64-bit multiplication and extract the results. 2924 // We define this so that it can be used for constant division. 2925 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0), 2926 Op.getOperand(1), Ops[1], Ops[0]); 2927 else { 2928 // Do a full 128-bit multiplication based on UMUL_LOHI64: 2929 // 2930 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64) 2931 // 2932 // but using the fact that the upper halves are either all zeros 2933 // or all ones: 2934 // 2935 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64) 2936 // 2937 // and grouping the right terms together since they are quicker than the 2938 // multiplication: 2939 // 2940 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64) 2941 SDValue C63 = DAG.getConstant(63, DL, MVT::i64); 2942 SDValue LL = Op.getOperand(0); 2943 SDValue RL = Op.getOperand(1); 2944 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63); 2945 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63); 2946 // UMUL_LOHI64 returns the low result in the odd register and the high 2947 // result in the even register. SMUL_LOHI is defined to return the 2948 // low half first, so the results are in reverse order. 2949 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64, 2950 LL, RL, Ops[1], Ops[0]); 2951 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH); 2952 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL); 2953 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL); 2954 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum); 2955 } 2956 return DAG.getMergeValues(Ops, DL); 2957 } 2958 2959 SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op, 2960 SelectionDAG &DAG) const { 2961 EVT VT = Op.getValueType(); 2962 SDLoc DL(Op); 2963 SDValue Ops[2]; 2964 if (is32Bit(VT)) 2965 // Just do a normal 64-bit multiplication and extract the results. 2966 // We define this so that it can be used for constant division. 2967 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0), 2968 Op.getOperand(1), Ops[1], Ops[0]); 2969 else 2970 // UMUL_LOHI64 returns the low result in the odd register and the high 2971 // result in the even register. UMUL_LOHI is defined to return the 2972 // low half first, so the results are in reverse order. 2973 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64, 2974 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); 2975 return DAG.getMergeValues(Ops, DL); 2976 } 2977 2978 SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op, 2979 SelectionDAG &DAG) const { 2980 SDValue Op0 = Op.getOperand(0); 2981 SDValue Op1 = Op.getOperand(1); 2982 EVT VT = Op.getValueType(); 2983 SDLoc DL(Op); 2984 unsigned Opcode; 2985 2986 // We use DSGF for 32-bit division. 2987 if (is32Bit(VT)) { 2988 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0); 2989 Opcode = SystemZISD::SDIVREM32; 2990 } else if (DAG.ComputeNumSignBits(Op1) > 32) { 2991 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1); 2992 Opcode = SystemZISD::SDIVREM32; 2993 } else 2994 Opcode = SystemZISD::SDIVREM64; 2995 2996 // DSG(F) takes a 64-bit dividend, so the even register in the GR128 2997 // input is "don't care". The instruction returns the remainder in 2998 // the even register and the quotient in the odd register. 2999 SDValue Ops[2]; 3000 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode, 3001 Op0, Op1, Ops[1], Ops[0]); 3002 return DAG.getMergeValues(Ops, DL); 3003 } 3004 3005 SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op, 3006 SelectionDAG &DAG) const { 3007 EVT VT = Op.getValueType(); 3008 SDLoc DL(Op); 3009 3010 // DL(G) uses a double-width dividend, so we need to clear the even 3011 // register in the GR128 input. The instruction returns the remainder 3012 // in the even register and the quotient in the odd register. 3013 SDValue Ops[2]; 3014 if (is32Bit(VT)) 3015 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32, 3016 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); 3017 else 3018 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64, 3019 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); 3020 return DAG.getMergeValues(Ops, DL); 3021 } 3022 3023 SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const { 3024 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation"); 3025 3026 // Get the known-zero masks for each operand. 3027 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) }; 3028 APInt KnownZero[2], KnownOne[2]; 3029 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]); 3030 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]); 3031 3032 // See if the upper 32 bits of one operand and the lower 32 bits of the 3033 // other are known zero. They are the low and high operands respectively. 3034 uint64_t Masks[] = { KnownZero[0].getZExtValue(), 3035 KnownZero[1].getZExtValue() }; 3036 unsigned High, Low; 3037 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff) 3038 High = 1, Low = 0; 3039 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff) 3040 High = 0, Low = 1; 3041 else 3042 return Op; 3043 3044 SDValue LowOp = Ops[Low]; 3045 SDValue HighOp = Ops[High]; 3046 3047 // If the high part is a constant, we're better off using IILH. 3048 if (HighOp.getOpcode() == ISD::Constant) 3049 return Op; 3050 3051 // If the low part is a constant that is outside the range of LHI, 3052 // then we're better off using IILF. 3053 if (LowOp.getOpcode() == ISD::Constant) { 3054 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue()); 3055 if (!isInt<16>(Value)) 3056 return Op; 3057 } 3058 3059 // Check whether the high part is an AND that doesn't change the 3060 // high 32 bits and just masks out low bits. We can skip it if so. 3061 if (HighOp.getOpcode() == ISD::AND && 3062 HighOp.getOperand(1).getOpcode() == ISD::Constant) { 3063 SDValue HighOp0 = HighOp.getOperand(0); 3064 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue(); 3065 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff)))) 3066 HighOp = HighOp0; 3067 } 3068 3069 // Take advantage of the fact that all GR32 operations only change the 3070 // low 32 bits by truncating Low to an i32 and inserting it directly 3071 // using a subreg. The interesting cases are those where the truncation 3072 // can be folded. 3073 SDLoc DL(Op); 3074 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp); 3075 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL, 3076 MVT::i64, HighOp, Low32); 3077 } 3078 3079 SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op, 3080 SelectionDAG &DAG) const { 3081 EVT VT = Op.getValueType(); 3082 SDLoc DL(Op); 3083 Op = Op.getOperand(0); 3084 3085 // Handle vector types via VPOPCT. 3086 if (VT.isVector()) { 3087 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op); 3088 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op); 3089 switch (VT.getVectorElementType().getSizeInBits()) { 3090 case 8: 3091 break; 3092 case 16: { 3093 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op); 3094 SDValue Shift = DAG.getConstant(8, DL, MVT::i32); 3095 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift); 3096 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp); 3097 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift); 3098 break; 3099 } 3100 case 32: { 3101 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, 3102 DAG.getConstant(0, DL, MVT::i32)); 3103 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp); 3104 break; 3105 } 3106 case 64: { 3107 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, 3108 DAG.getConstant(0, DL, MVT::i32)); 3109 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp); 3110 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp); 3111 break; 3112 } 3113 default: 3114 llvm_unreachable("Unexpected type"); 3115 } 3116 return Op; 3117 } 3118 3119 // Get the known-zero mask for the operand. 3120 APInt KnownZero, KnownOne; 3121 DAG.computeKnownBits(Op, KnownZero, KnownOne); 3122 unsigned NumSignificantBits = (~KnownZero).getActiveBits(); 3123 if (NumSignificantBits == 0) 3124 return DAG.getConstant(0, DL, VT); 3125 3126 // Skip known-zero high parts of the operand. 3127 int64_t OrigBitSize = VT.getSizeInBits(); 3128 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits); 3129 BitSize = std::min(BitSize, OrigBitSize); 3130 3131 // The POPCNT instruction counts the number of bits in each byte. 3132 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op); 3133 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op); 3134 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op); 3135 3136 // Add up per-byte counts in a binary tree. All bits of Op at 3137 // position larger than BitSize remain zero throughout. 3138 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) { 3139 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT)); 3140 if (BitSize != OrigBitSize) 3141 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp, 3142 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT)); 3143 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp); 3144 } 3145 3146 // Extract overall result from high byte. 3147 if (BitSize > 8) 3148 Op = DAG.getNode(ISD::SRL, DL, VT, Op, 3149 DAG.getConstant(BitSize - 8, DL, VT)); 3150 3151 return Op; 3152 } 3153 3154 SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op, 3155 SelectionDAG &DAG) const { 3156 SDLoc DL(Op); 3157 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>( 3158 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()); 3159 SynchronizationScope FenceScope = static_cast<SynchronizationScope>( 3160 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue()); 3161 3162 // The only fence that needs an instruction is a sequentially-consistent 3163 // cross-thread fence. 3164 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent && 3165 FenceScope == CrossThread) { 3166 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other, 3167 Op.getOperand(0)), 3168 0); 3169 } 3170 3171 // MEMBARRIER is a compiler barrier; it codegens to a no-op. 3172 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0)); 3173 } 3174 3175 // Op is an atomic load. Lower it into a normal volatile load. 3176 SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op, 3177 SelectionDAG &DAG) const { 3178 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3179 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(), 3180 Node->getChain(), Node->getBasePtr(), 3181 Node->getMemoryVT(), Node->getMemOperand()); 3182 } 3183 3184 // Op is an atomic store. Lower it into a normal volatile store followed 3185 // by a serialization. 3186 SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op, 3187 SelectionDAG &DAG) const { 3188 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3189 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(), 3190 Node->getBasePtr(), Node->getMemoryVT(), 3191 Node->getMemOperand()); 3192 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other, 3193 Chain), 0); 3194 } 3195 3196 // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first 3197 // two into the fullword ATOMIC_LOADW_* operation given by Opcode. 3198 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op, 3199 SelectionDAG &DAG, 3200 unsigned Opcode) const { 3201 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3202 3203 // 32-bit operations need no code outside the main loop. 3204 EVT NarrowVT = Node->getMemoryVT(); 3205 EVT WideVT = MVT::i32; 3206 if (NarrowVT == WideVT) 3207 return Op; 3208 3209 int64_t BitSize = NarrowVT.getSizeInBits(); 3210 SDValue ChainIn = Node->getChain(); 3211 SDValue Addr = Node->getBasePtr(); 3212 SDValue Src2 = Node->getVal(); 3213 MachineMemOperand *MMO = Node->getMemOperand(); 3214 SDLoc DL(Node); 3215 EVT PtrVT = Addr.getValueType(); 3216 3217 // Convert atomic subtracts of constants into additions. 3218 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB) 3219 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) { 3220 Opcode = SystemZISD::ATOMIC_LOADW_ADD; 3221 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType()); 3222 } 3223 3224 // Get the address of the containing word. 3225 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr, 3226 DAG.getConstant(-4, DL, PtrVT)); 3227 3228 // Get the number of bits that the word must be rotated left in order 3229 // to bring the field to the top bits of a GR32. 3230 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr, 3231 DAG.getConstant(3, DL, PtrVT)); 3232 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift); 3233 3234 // Get the complementing shift amount, for rotating a field in the top 3235 // bits back to its proper position. 3236 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT, 3237 DAG.getConstant(0, DL, WideVT), BitShift); 3238 3239 // Extend the source operand to 32 bits and prepare it for the inner loop. 3240 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other 3241 // operations require the source to be shifted in advance. (This shift 3242 // can be folded if the source is constant.) For AND and NAND, the lower 3243 // bits must be set, while for other opcodes they should be left clear. 3244 if (Opcode != SystemZISD::ATOMIC_SWAPW) 3245 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2, 3246 DAG.getConstant(32 - BitSize, DL, WideVT)); 3247 if (Opcode == SystemZISD::ATOMIC_LOADW_AND || 3248 Opcode == SystemZISD::ATOMIC_LOADW_NAND) 3249 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2, 3250 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT)); 3251 3252 // Construct the ATOMIC_LOADW_* node. 3253 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other); 3254 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift, 3255 DAG.getConstant(BitSize, DL, WideVT) }; 3256 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops, 3257 NarrowVT, MMO); 3258 3259 // Rotate the result of the final CS so that the field is in the lower 3260 // bits of a GR32, then truncate it. 3261 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift, 3262 DAG.getConstant(BitSize, DL, WideVT)); 3263 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift); 3264 3265 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) }; 3266 return DAG.getMergeValues(RetOps, DL); 3267 } 3268 3269 // Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations 3270 // into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit 3271 // operations into additions. 3272 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op, 3273 SelectionDAG &DAG) const { 3274 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3275 EVT MemVT = Node->getMemoryVT(); 3276 if (MemVT == MVT::i32 || MemVT == MVT::i64) { 3277 // A full-width operation. 3278 assert(Op.getValueType() == MemVT && "Mismatched VTs"); 3279 SDValue Src2 = Node->getVal(); 3280 SDValue NegSrc2; 3281 SDLoc DL(Src2); 3282 3283 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) { 3284 // Use an addition if the operand is constant and either LAA(G) is 3285 // available or the negative value is in the range of A(G)FHI. 3286 int64_t Value = (-Op2->getAPIntValue()).getSExtValue(); 3287 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1()) 3288 NegSrc2 = DAG.getConstant(Value, DL, MemVT); 3289 } else if (Subtarget.hasInterlockedAccess1()) 3290 // Use LAA(G) if available. 3291 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT), 3292 Src2); 3293 3294 if (NegSrc2.getNode()) 3295 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT, 3296 Node->getChain(), Node->getBasePtr(), NegSrc2, 3297 Node->getMemOperand(), Node->getOrdering(), 3298 Node->getSynchScope()); 3299 3300 // Use the node as-is. 3301 return Op; 3302 } 3303 3304 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB); 3305 } 3306 3307 // Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two 3308 // into a fullword ATOMIC_CMP_SWAPW operation. 3309 SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op, 3310 SelectionDAG &DAG) const { 3311 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3312 3313 // We have native support for 32-bit compare and swap. 3314 EVT NarrowVT = Node->getMemoryVT(); 3315 EVT WideVT = MVT::i32; 3316 if (NarrowVT == WideVT) 3317 return Op; 3318 3319 int64_t BitSize = NarrowVT.getSizeInBits(); 3320 SDValue ChainIn = Node->getOperand(0); 3321 SDValue Addr = Node->getOperand(1); 3322 SDValue CmpVal = Node->getOperand(2); 3323 SDValue SwapVal = Node->getOperand(3); 3324 MachineMemOperand *MMO = Node->getMemOperand(); 3325 SDLoc DL(Node); 3326 EVT PtrVT = Addr.getValueType(); 3327 3328 // Get the address of the containing word. 3329 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr, 3330 DAG.getConstant(-4, DL, PtrVT)); 3331 3332 // Get the number of bits that the word must be rotated left in order 3333 // to bring the field to the top bits of a GR32. 3334 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr, 3335 DAG.getConstant(3, DL, PtrVT)); 3336 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift); 3337 3338 // Get the complementing shift amount, for rotating a field in the top 3339 // bits back to its proper position. 3340 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT, 3341 DAG.getConstant(0, DL, WideVT), BitShift); 3342 3343 // Construct the ATOMIC_CMP_SWAPW node. 3344 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other); 3345 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift, 3346 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) }; 3347 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL, 3348 VTList, Ops, NarrowVT, MMO); 3349 return AtomicOp; 3350 } 3351 3352 SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op, 3353 SelectionDAG &DAG) const { 3354 MachineFunction &MF = DAG.getMachineFunction(); 3355 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); 3356 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op), 3357 SystemZ::R15D, Op.getValueType()); 3358 } 3359 3360 SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op, 3361 SelectionDAG &DAG) const { 3362 MachineFunction &MF = DAG.getMachineFunction(); 3363 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); 3364 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain"); 3365 3366 SDValue Chain = Op.getOperand(0); 3367 SDValue NewSP = Op.getOperand(1); 3368 SDValue Backchain; 3369 SDLoc DL(Op); 3370 3371 if (StoreBackchain) { 3372 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64); 3373 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo(), 3374 false, false, false, 0); 3375 } 3376 3377 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP); 3378 3379 if (StoreBackchain) 3380 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo(), 3381 false, false, 0); 3382 3383 return Chain; 3384 } 3385 3386 SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op, 3387 SelectionDAG &DAG) const { 3388 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 3389 if (!IsData) 3390 // Just preserve the chain. 3391 return Op.getOperand(0); 3392 3393 SDLoc DL(Op); 3394 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); 3395 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ; 3396 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode()); 3397 SDValue Ops[] = { 3398 Op.getOperand(0), 3399 DAG.getConstant(Code, DL, MVT::i32), 3400 Op.getOperand(1) 3401 }; 3402 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL, 3403 Node->getVTList(), Ops, 3404 Node->getMemoryVT(), Node->getMemOperand()); 3405 } 3406 3407 // Return an i32 that contains the value of CC immediately after After, 3408 // whose final operand must be MVT::Glue. 3409 static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) { 3410 SDLoc DL(After); 3411 SDValue Glue = SDValue(After, After->getNumValues() - 1); 3412 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue); 3413 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM, 3414 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32)); 3415 } 3416 3417 SDValue 3418 SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op, 3419 SelectionDAG &DAG) const { 3420 unsigned Opcode, CCValid; 3421 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) { 3422 assert(Op->getNumValues() == 2 && "Expected only CC result and chain"); 3423 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode); 3424 SDValue CC = getCCResult(DAG, Glued.getNode()); 3425 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC); 3426 return SDValue(); 3427 } 3428 3429 return SDValue(); 3430 } 3431 3432 SDValue 3433 SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op, 3434 SelectionDAG &DAG) const { 3435 unsigned Opcode, CCValid; 3436 if (isIntrinsicWithCC(Op, Opcode, CCValid)) { 3437 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode); 3438 SDValue CC = getCCResult(DAG, Glued.getNode()); 3439 if (Op->getNumValues() == 1) 3440 return CC; 3441 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result"); 3442 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued, 3443 CC); 3444 } 3445 3446 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 3447 switch (Id) { 3448 case Intrinsic::thread_pointer: 3449 return lowerThreadPointer(SDLoc(Op), DAG); 3450 3451 case Intrinsic::s390_vpdi: 3452 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(), 3453 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 3454 3455 case Intrinsic::s390_vperm: 3456 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(), 3457 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 3458 3459 case Intrinsic::s390_vuphb: 3460 case Intrinsic::s390_vuphh: 3461 case Intrinsic::s390_vuphf: 3462 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(), 3463 Op.getOperand(1)); 3464 3465 case Intrinsic::s390_vuplhb: 3466 case Intrinsic::s390_vuplhh: 3467 case Intrinsic::s390_vuplhf: 3468 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(), 3469 Op.getOperand(1)); 3470 3471 case Intrinsic::s390_vuplb: 3472 case Intrinsic::s390_vuplhw: 3473 case Intrinsic::s390_vuplf: 3474 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(), 3475 Op.getOperand(1)); 3476 3477 case Intrinsic::s390_vupllb: 3478 case Intrinsic::s390_vupllh: 3479 case Intrinsic::s390_vupllf: 3480 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(), 3481 Op.getOperand(1)); 3482 3483 case Intrinsic::s390_vsumb: 3484 case Intrinsic::s390_vsumh: 3485 case Intrinsic::s390_vsumgh: 3486 case Intrinsic::s390_vsumgf: 3487 case Intrinsic::s390_vsumqf: 3488 case Intrinsic::s390_vsumqg: 3489 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(), 3490 Op.getOperand(1), Op.getOperand(2)); 3491 } 3492 3493 return SDValue(); 3494 } 3495 3496 namespace { 3497 // Says that SystemZISD operation Opcode can be used to perform the equivalent 3498 // of a VPERM with permute vector Bytes. If Opcode takes three operands, 3499 // Operand is the constant third operand, otherwise it is the number of 3500 // bytes in each element of the result. 3501 struct Permute { 3502 unsigned Opcode; 3503 unsigned Operand; 3504 unsigned char Bytes[SystemZ::VectorBytes]; 3505 }; 3506 } 3507 3508 static const Permute PermuteForms[] = { 3509 // VMRHG 3510 { SystemZISD::MERGE_HIGH, 8, 3511 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } }, 3512 // VMRHF 3513 { SystemZISD::MERGE_HIGH, 4, 3514 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } }, 3515 // VMRHH 3516 { SystemZISD::MERGE_HIGH, 2, 3517 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } }, 3518 // VMRHB 3519 { SystemZISD::MERGE_HIGH, 1, 3520 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } }, 3521 // VMRLG 3522 { SystemZISD::MERGE_LOW, 8, 3523 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } }, 3524 // VMRLF 3525 { SystemZISD::MERGE_LOW, 4, 3526 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } }, 3527 // VMRLH 3528 { SystemZISD::MERGE_LOW, 2, 3529 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } }, 3530 // VMRLB 3531 { SystemZISD::MERGE_LOW, 1, 3532 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } }, 3533 // VPKG 3534 { SystemZISD::PACK, 4, 3535 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } }, 3536 // VPKF 3537 { SystemZISD::PACK, 2, 3538 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } }, 3539 // VPKH 3540 { SystemZISD::PACK, 1, 3541 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } }, 3542 // VPDI V1, V2, 4 (low half of V1, high half of V2) 3543 { SystemZISD::PERMUTE_DWORDS, 4, 3544 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } }, 3545 // VPDI V1, V2, 1 (high half of V1, low half of V2) 3546 { SystemZISD::PERMUTE_DWORDS, 1, 3547 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } } 3548 }; 3549 3550 // Called after matching a vector shuffle against a particular pattern. 3551 // Both the original shuffle and the pattern have two vector operands. 3552 // OpNos[0] is the operand of the original shuffle that should be used for 3553 // operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything. 3554 // OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and 3555 // set OpNo0 and OpNo1 to the shuffle operands that should actually be used 3556 // for operands 0 and 1 of the pattern. 3557 static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) { 3558 if (OpNos[0] < 0) { 3559 if (OpNos[1] < 0) 3560 return false; 3561 OpNo0 = OpNo1 = OpNos[1]; 3562 } else if (OpNos[1] < 0) { 3563 OpNo0 = OpNo1 = OpNos[0]; 3564 } else { 3565 OpNo0 = OpNos[0]; 3566 OpNo1 = OpNos[1]; 3567 } 3568 return true; 3569 } 3570 3571 // Bytes is a VPERM-like permute vector, except that -1 is used for 3572 // undefined bytes. Return true if the VPERM can be implemented using P. 3573 // When returning true set OpNo0 to the VPERM operand that should be 3574 // used for operand 0 of P and likewise OpNo1 for operand 1 of P. 3575 // 3576 // For example, if swapping the VPERM operands allows P to match, OpNo0 3577 // will be 1 and OpNo1 will be 0. If instead Bytes only refers to one 3578 // operand, but rewriting it to use two duplicated operands allows it to 3579 // match P, then OpNo0 and OpNo1 will be the same. 3580 static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P, 3581 unsigned &OpNo0, unsigned &OpNo1) { 3582 int OpNos[] = { -1, -1 }; 3583 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) { 3584 int Elt = Bytes[I]; 3585 if (Elt >= 0) { 3586 // Make sure that the two permute vectors use the same suboperand 3587 // byte number. Only the operand numbers (the high bits) are 3588 // allowed to differ. 3589 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1)) 3590 return false; 3591 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes; 3592 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes; 3593 // Make sure that the operand mappings are consistent with previous 3594 // elements. 3595 if (OpNos[ModelOpNo] == 1 - RealOpNo) 3596 return false; 3597 OpNos[ModelOpNo] = RealOpNo; 3598 } 3599 } 3600 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1); 3601 } 3602 3603 // As above, but search for a matching permute. 3604 static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes, 3605 unsigned &OpNo0, unsigned &OpNo1) { 3606 for (auto &P : PermuteForms) 3607 if (matchPermute(Bytes, P, OpNo0, OpNo1)) 3608 return &P; 3609 return nullptr; 3610 } 3611 3612 // Bytes is a VPERM-like permute vector, except that -1 is used for 3613 // undefined bytes. This permute is an operand of an outer permute. 3614 // See whether redistributing the -1 bytes gives a shuffle that can be 3615 // implemented using P. If so, set Transform to a VPERM-like permute vector 3616 // that, when applied to the result of P, gives the original permute in Bytes. 3617 static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes, 3618 const Permute &P, 3619 SmallVectorImpl<int> &Transform) { 3620 unsigned To = 0; 3621 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) { 3622 int Elt = Bytes[From]; 3623 if (Elt < 0) 3624 // Byte number From of the result is undefined. 3625 Transform[From] = -1; 3626 else { 3627 while (P.Bytes[To] != Elt) { 3628 To += 1; 3629 if (To == SystemZ::VectorBytes) 3630 return false; 3631 } 3632 Transform[From] = To; 3633 } 3634 } 3635 return true; 3636 } 3637 3638 // As above, but search for a matching permute. 3639 static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes, 3640 SmallVectorImpl<int> &Transform) { 3641 for (auto &P : PermuteForms) 3642 if (matchDoublePermute(Bytes, P, Transform)) 3643 return &P; 3644 return nullptr; 3645 } 3646 3647 // Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask, 3648 // as if it had type vNi8. 3649 static void getVPermMask(ShuffleVectorSDNode *VSN, 3650 SmallVectorImpl<int> &Bytes) { 3651 EVT VT = VSN->getValueType(0); 3652 unsigned NumElements = VT.getVectorNumElements(); 3653 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); 3654 Bytes.resize(NumElements * BytesPerElement, -1); 3655 for (unsigned I = 0; I < NumElements; ++I) { 3656 int Index = VSN->getMaskElt(I); 3657 if (Index >= 0) 3658 for (unsigned J = 0; J < BytesPerElement; ++J) 3659 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J; 3660 } 3661 } 3662 3663 // Bytes is a VPERM-like permute vector, except that -1 is used for 3664 // undefined bytes. See whether bytes [Start, Start + BytesPerElement) of 3665 // the result come from a contiguous sequence of bytes from one input. 3666 // Set Base to the selector for the first byte if so. 3667 static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start, 3668 unsigned BytesPerElement, int &Base) { 3669 Base = -1; 3670 for (unsigned I = 0; I < BytesPerElement; ++I) { 3671 if (Bytes[Start + I] >= 0) { 3672 unsigned Elem = Bytes[Start + I]; 3673 if (Base < 0) { 3674 Base = Elem - I; 3675 // Make sure the bytes would come from one input operand. 3676 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size()) 3677 return false; 3678 } else if (unsigned(Base) != Elem - I) 3679 return false; 3680 } 3681 } 3682 return true; 3683 } 3684 3685 // Bytes is a VPERM-like permute vector, except that -1 is used for 3686 // undefined bytes. Return true if it can be performed using VSLDI. 3687 // When returning true, set StartIndex to the shift amount and OpNo0 3688 // and OpNo1 to the VPERM operands that should be used as the first 3689 // and second shift operand respectively. 3690 static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes, 3691 unsigned &StartIndex, unsigned &OpNo0, 3692 unsigned &OpNo1) { 3693 int OpNos[] = { -1, -1 }; 3694 int Shift = -1; 3695 for (unsigned I = 0; I < 16; ++I) { 3696 int Index = Bytes[I]; 3697 if (Index >= 0) { 3698 int ExpectedShift = (Index - I) % SystemZ::VectorBytes; 3699 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes; 3700 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes; 3701 if (Shift < 0) 3702 Shift = ExpectedShift; 3703 else if (Shift != ExpectedShift) 3704 return false; 3705 // Make sure that the operand mappings are consistent with previous 3706 // elements. 3707 if (OpNos[ModelOpNo] == 1 - RealOpNo) 3708 return false; 3709 OpNos[ModelOpNo] = RealOpNo; 3710 } 3711 } 3712 StartIndex = Shift; 3713 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1); 3714 } 3715 3716 // Create a node that performs P on operands Op0 and Op1, casting the 3717 // operands to the appropriate type. The type of the result is determined by P. 3718 static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL, 3719 const Permute &P, SDValue Op0, SDValue Op1) { 3720 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input 3721 // elements of a PACK are twice as wide as the outputs. 3722 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 : 3723 P.Opcode == SystemZISD::PACK ? P.Operand * 2 : 3724 P.Operand); 3725 // Cast both operands to the appropriate type. 3726 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8), 3727 SystemZ::VectorBytes / InBytes); 3728 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0); 3729 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1); 3730 SDValue Op; 3731 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) { 3732 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32); 3733 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2); 3734 } else if (P.Opcode == SystemZISD::PACK) { 3735 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8), 3736 SystemZ::VectorBytes / P.Operand); 3737 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1); 3738 } else { 3739 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1); 3740 } 3741 return Op; 3742 } 3743 3744 // Bytes is a VPERM-like permute vector, except that -1 is used for 3745 // undefined bytes. Implement it on operands Ops[0] and Ops[1] using 3746 // VSLDI or VPERM. 3747 static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL, 3748 SDValue *Ops, 3749 const SmallVectorImpl<int> &Bytes) { 3750 for (unsigned I = 0; I < 2; ++I) 3751 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]); 3752 3753 // First see whether VSLDI can be used. 3754 unsigned StartIndex, OpNo0, OpNo1; 3755 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1)) 3756 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0], 3757 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32)); 3758 3759 // Fall back on VPERM. Construct an SDNode for the permute vector. 3760 SDValue IndexNodes[SystemZ::VectorBytes]; 3761 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) 3762 if (Bytes[I] >= 0) 3763 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32); 3764 else 3765 IndexNodes[I] = DAG.getUNDEF(MVT::i32); 3766 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes); 3767 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2); 3768 } 3769 3770 namespace { 3771 // Describes a general N-operand vector shuffle. 3772 struct GeneralShuffle { 3773 GeneralShuffle(EVT vt) : VT(vt) {} 3774 void addUndef(); 3775 void add(SDValue, unsigned); 3776 SDValue getNode(SelectionDAG &, const SDLoc &); 3777 3778 // The operands of the shuffle. 3779 SmallVector<SDValue, SystemZ::VectorBytes> Ops; 3780 3781 // Index I is -1 if byte I of the result is undefined. Otherwise the 3782 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand 3783 // Bytes[I] / SystemZ::VectorBytes. 3784 SmallVector<int, SystemZ::VectorBytes> Bytes; 3785 3786 // The type of the shuffle result. 3787 EVT VT; 3788 }; 3789 } 3790 3791 // Add an extra undefined element to the shuffle. 3792 void GeneralShuffle::addUndef() { 3793 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); 3794 for (unsigned I = 0; I < BytesPerElement; ++I) 3795 Bytes.push_back(-1); 3796 } 3797 3798 // Add an extra element to the shuffle, taking it from element Elem of Op. 3799 // A null Op indicates a vector input whose value will be calculated later; 3800 // there is at most one such input per shuffle and it always has the same 3801 // type as the result. 3802 void GeneralShuffle::add(SDValue Op, unsigned Elem) { 3803 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); 3804 3805 // The source vector can have wider elements than the result, 3806 // either through an explicit TRUNCATE or because of type legalization. 3807 // We want the least significant part. 3808 EVT FromVT = Op.getNode() ? Op.getValueType() : VT; 3809 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize(); 3810 assert(FromBytesPerElement >= BytesPerElement && 3811 "Invalid EXTRACT_VECTOR_ELT"); 3812 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes + 3813 (FromBytesPerElement - BytesPerElement)); 3814 3815 // Look through things like shuffles and bitcasts. 3816 while (Op.getNode()) { 3817 if (Op.getOpcode() == ISD::BITCAST) 3818 Op = Op.getOperand(0); 3819 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) { 3820 // See whether the bytes we need come from a contiguous part of one 3821 // operand. 3822 SmallVector<int, SystemZ::VectorBytes> OpBytes; 3823 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes); 3824 int NewByte; 3825 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte)) 3826 break; 3827 if (NewByte < 0) { 3828 addUndef(); 3829 return; 3830 } 3831 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes); 3832 Byte = unsigned(NewByte) % SystemZ::VectorBytes; 3833 } else if (Op.isUndef()) { 3834 addUndef(); 3835 return; 3836 } else 3837 break; 3838 } 3839 3840 // Make sure that the source of the extraction is in Ops. 3841 unsigned OpNo = 0; 3842 for (; OpNo < Ops.size(); ++OpNo) 3843 if (Ops[OpNo] == Op) 3844 break; 3845 if (OpNo == Ops.size()) 3846 Ops.push_back(Op); 3847 3848 // Add the element to Bytes. 3849 unsigned Base = OpNo * SystemZ::VectorBytes + Byte; 3850 for (unsigned I = 0; I < BytesPerElement; ++I) 3851 Bytes.push_back(Base + I); 3852 } 3853 3854 // Return SDNodes for the completed shuffle. 3855 SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) { 3856 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector"); 3857 3858 if (Ops.size() == 0) 3859 return DAG.getUNDEF(VT); 3860 3861 // Make sure that there are at least two shuffle operands. 3862 if (Ops.size() == 1) 3863 Ops.push_back(DAG.getUNDEF(MVT::v16i8)); 3864 3865 // Create a tree of shuffles, deferring root node until after the loop. 3866 // Try to redistribute the undefined elements of non-root nodes so that 3867 // the non-root shuffles match something like a pack or merge, then adjust 3868 // the parent node's permute vector to compensate for the new order. 3869 // Among other things, this copes with vectors like <2 x i16> that were 3870 // padded with undefined elements during type legalization. 3871 // 3872 // In the best case this redistribution will lead to the whole tree 3873 // using packs and merges. It should rarely be a loss in other cases. 3874 unsigned Stride = 1; 3875 for (; Stride * 2 < Ops.size(); Stride *= 2) { 3876 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) { 3877 SDValue SubOps[] = { Ops[I], Ops[I + Stride] }; 3878 3879 // Create a mask for just these two operands. 3880 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes); 3881 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) { 3882 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes; 3883 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes; 3884 if (OpNo == I) 3885 NewBytes[J] = Byte; 3886 else if (OpNo == I + Stride) 3887 NewBytes[J] = SystemZ::VectorBytes + Byte; 3888 else 3889 NewBytes[J] = -1; 3890 } 3891 // See if it would be better to reorganize NewMask to avoid using VPERM. 3892 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes); 3893 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) { 3894 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]); 3895 // Applying NewBytesMap to Ops[I] gets back to NewBytes. 3896 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) { 3897 if (NewBytes[J] >= 0) { 3898 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes && 3899 "Invalid double permute"); 3900 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J]; 3901 } else 3902 assert(NewBytesMap[J] < 0 && "Invalid double permute"); 3903 } 3904 } else { 3905 // Just use NewBytes on the operands. 3906 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes); 3907 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) 3908 if (NewBytes[J] >= 0) 3909 Bytes[J] = I * SystemZ::VectorBytes + J; 3910 } 3911 } 3912 } 3913 3914 // Now we just have 2 inputs. Put the second operand in Ops[1]. 3915 if (Stride > 1) { 3916 Ops[1] = Ops[Stride]; 3917 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) 3918 if (Bytes[I] >= int(SystemZ::VectorBytes)) 3919 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes; 3920 } 3921 3922 // Look for an instruction that can do the permute without resorting 3923 // to VPERM. 3924 unsigned OpNo0, OpNo1; 3925 SDValue Op; 3926 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1)) 3927 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]); 3928 else 3929 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes); 3930 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 3931 } 3932 3933 // Return true if the given BUILD_VECTOR is a scalar-to-vector conversion. 3934 static bool isScalarToVector(SDValue Op) { 3935 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I) 3936 if (!Op.getOperand(I).isUndef()) 3937 return false; 3938 return true; 3939 } 3940 3941 // Return a vector of type VT that contains Value in the first element. 3942 // The other elements don't matter. 3943 static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT, 3944 SDValue Value) { 3945 // If we have a constant, replicate it to all elements and let the 3946 // BUILD_VECTOR lowering take care of it. 3947 if (Value.getOpcode() == ISD::Constant || 3948 Value.getOpcode() == ISD::ConstantFP) { 3949 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value); 3950 return DAG.getBuildVector(VT, DL, Ops); 3951 } 3952 if (Value.isUndef()) 3953 return DAG.getUNDEF(VT); 3954 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value); 3955 } 3956 3957 // Return a vector of type VT in which Op0 is in element 0 and Op1 is in 3958 // element 1. Used for cases in which replication is cheap. 3959 static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT, 3960 SDValue Op0, SDValue Op1) { 3961 if (Op0.isUndef()) { 3962 if (Op1.isUndef()) 3963 return DAG.getUNDEF(VT); 3964 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1); 3965 } 3966 if (Op1.isUndef()) 3967 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0); 3968 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT, 3969 buildScalarToVector(DAG, DL, VT, Op0), 3970 buildScalarToVector(DAG, DL, VT, Op1)); 3971 } 3972 3973 // Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64 3974 // vector for them. 3975 static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0, 3976 SDValue Op1) { 3977 if (Op0.isUndef() && Op1.isUndef()) 3978 return DAG.getUNDEF(MVT::v2i64); 3979 // If one of the two inputs is undefined then replicate the other one, 3980 // in order to avoid using another register unnecessarily. 3981 if (Op0.isUndef()) 3982 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1); 3983 else if (Op1.isUndef()) 3984 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0); 3985 else { 3986 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0); 3987 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1); 3988 } 3989 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1); 3990 } 3991 3992 // Try to represent constant BUILD_VECTOR node BVN using a 3993 // SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask 3994 // on success. 3995 static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) { 3996 EVT ElemVT = BVN->getValueType(0).getVectorElementType(); 3997 unsigned BytesPerElement = ElemVT.getStoreSize(); 3998 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) { 3999 SDValue Op = BVN->getOperand(I); 4000 if (!Op.isUndef()) { 4001 uint64_t Value; 4002 if (Op.getOpcode() == ISD::Constant) 4003 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue(); 4004 else if (Op.getOpcode() == ISD::ConstantFP) 4005 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt() 4006 .getZExtValue()); 4007 else 4008 return false; 4009 for (unsigned J = 0; J < BytesPerElement; ++J) { 4010 uint64_t Byte = (Value >> (J * 8)) & 0xff; 4011 if (Byte == 0xff) 4012 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J); 4013 else if (Byte != 0) 4014 return false; 4015 } 4016 } 4017 } 4018 return true; 4019 } 4020 4021 // Try to load a vector constant in which BitsPerElement-bit value Value 4022 // is replicated to fill the vector. VT is the type of the resulting 4023 // constant, which may have elements of a different size from BitsPerElement. 4024 // Return the SDValue of the constant on success, otherwise return 4025 // an empty value. 4026 static SDValue tryBuildVectorReplicate(SelectionDAG &DAG, 4027 const SystemZInstrInfo *TII, 4028 const SDLoc &DL, EVT VT, uint64_t Value, 4029 unsigned BitsPerElement) { 4030 // Signed 16-bit values can be replicated using VREPI. 4031 int64_t SignedValue = SignExtend64(Value, BitsPerElement); 4032 if (isInt<16>(SignedValue)) { 4033 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement), 4034 SystemZ::VectorBits / BitsPerElement); 4035 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT, 4036 DAG.getConstant(SignedValue, DL, MVT::i32)); 4037 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 4038 } 4039 // See whether rotating the constant left some N places gives a value that 4040 // is one less than a power of 2 (i.e. all zeros followed by all ones). 4041 // If so we can use VGM. 4042 unsigned Start, End; 4043 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) { 4044 // isRxSBGMask returns the bit numbers for a full 64-bit value, 4045 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to 4046 // bit numbers for an BitsPerElement value, so that 0 denotes 4047 // 1 << (BitsPerElement-1). 4048 Start -= 64 - BitsPerElement; 4049 End -= 64 - BitsPerElement; 4050 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement), 4051 SystemZ::VectorBits / BitsPerElement); 4052 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT, 4053 DAG.getConstant(Start, DL, MVT::i32), 4054 DAG.getConstant(End, DL, MVT::i32)); 4055 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 4056 } 4057 return SDValue(); 4058 } 4059 4060 // If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually 4061 // better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for 4062 // the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR 4063 // would benefit from this representation and return it if so. 4064 static SDValue tryBuildVectorShuffle(SelectionDAG &DAG, 4065 BuildVectorSDNode *BVN) { 4066 EVT VT = BVN->getValueType(0); 4067 unsigned NumElements = VT.getVectorNumElements(); 4068 4069 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation 4070 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still 4071 // need a BUILD_VECTOR, add an additional placeholder operand for that 4072 // BUILD_VECTOR and store its operands in ResidueOps. 4073 GeneralShuffle GS(VT); 4074 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps; 4075 bool FoundOne = false; 4076 for (unsigned I = 0; I < NumElements; ++I) { 4077 SDValue Op = BVN->getOperand(I); 4078 if (Op.getOpcode() == ISD::TRUNCATE) 4079 Op = Op.getOperand(0); 4080 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 4081 Op.getOperand(1).getOpcode() == ISD::Constant) { 4082 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 4083 GS.add(Op.getOperand(0), Elem); 4084 FoundOne = true; 4085 } else if (Op.isUndef()) { 4086 GS.addUndef(); 4087 } else { 4088 GS.add(SDValue(), ResidueOps.size()); 4089 ResidueOps.push_back(BVN->getOperand(I)); 4090 } 4091 } 4092 4093 // Nothing to do if there are no EXTRACT_VECTOR_ELTs. 4094 if (!FoundOne) 4095 return SDValue(); 4096 4097 // Create the BUILD_VECTOR for the remaining elements, if any. 4098 if (!ResidueOps.empty()) { 4099 while (ResidueOps.size() < NumElements) 4100 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType())); 4101 for (auto &Op : GS.Ops) { 4102 if (!Op.getNode()) { 4103 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps); 4104 break; 4105 } 4106 } 4107 } 4108 return GS.getNode(DAG, SDLoc(BVN)); 4109 } 4110 4111 // Combine GPR scalar values Elems into a vector of type VT. 4112 static SDValue buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT, 4113 SmallVectorImpl<SDValue> &Elems) { 4114 // See whether there is a single replicated value. 4115 SDValue Single; 4116 unsigned int NumElements = Elems.size(); 4117 unsigned int Count = 0; 4118 for (auto Elem : Elems) { 4119 if (!Elem.isUndef()) { 4120 if (!Single.getNode()) 4121 Single = Elem; 4122 else if (Elem != Single) { 4123 Single = SDValue(); 4124 break; 4125 } 4126 Count += 1; 4127 } 4128 } 4129 // There are three cases here: 4130 // 4131 // - if the only defined element is a loaded one, the best sequence 4132 // is a replicating load. 4133 // 4134 // - otherwise, if the only defined element is an i64 value, we will 4135 // end up with the same VLVGP sequence regardless of whether we short-cut 4136 // for replication or fall through to the later code. 4137 // 4138 // - otherwise, if the only defined element is an i32 or smaller value, 4139 // we would need 2 instructions to replicate it: VLVGP followed by VREPx. 4140 // This is only a win if the single defined element is used more than once. 4141 // In other cases we're better off using a single VLVGx. 4142 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD)) 4143 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single); 4144 4145 // The best way of building a v2i64 from two i64s is to use VLVGP. 4146 if (VT == MVT::v2i64) 4147 return joinDwords(DAG, DL, Elems[0], Elems[1]); 4148 4149 // Use a 64-bit merge high to combine two doubles. 4150 if (VT == MVT::v2f64) 4151 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]); 4152 4153 // Build v4f32 values directly from the FPRs: 4154 // 4155 // <Axxx> <Bxxx> <Cxxxx> <Dxxx> 4156 // V V VMRHF 4157 // <ABxx> <CDxx> 4158 // V VMRHG 4159 // <ABCD> 4160 if (VT == MVT::v4f32) { 4161 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]); 4162 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]); 4163 // Avoid unnecessary undefs by reusing the other operand. 4164 if (Op01.isUndef()) 4165 Op01 = Op23; 4166 else if (Op23.isUndef()) 4167 Op23 = Op01; 4168 // Merging identical replications is a no-op. 4169 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23) 4170 return Op01; 4171 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01); 4172 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23); 4173 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH, 4174 DL, MVT::v2i64, Op01, Op23); 4175 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 4176 } 4177 4178 // Collect the constant terms. 4179 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue()); 4180 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false); 4181 4182 unsigned NumConstants = 0; 4183 for (unsigned I = 0; I < NumElements; ++I) { 4184 SDValue Elem = Elems[I]; 4185 if (Elem.getOpcode() == ISD::Constant || 4186 Elem.getOpcode() == ISD::ConstantFP) { 4187 NumConstants += 1; 4188 Constants[I] = Elem; 4189 Done[I] = true; 4190 } 4191 } 4192 // If there was at least one constant, fill in the other elements of 4193 // Constants with undefs to get a full vector constant and use that 4194 // as the starting point. 4195 SDValue Result; 4196 if (NumConstants > 0) { 4197 for (unsigned I = 0; I < NumElements; ++I) 4198 if (!Constants[I].getNode()) 4199 Constants[I] = DAG.getUNDEF(Elems[I].getValueType()); 4200 Result = DAG.getBuildVector(VT, DL, Constants); 4201 } else { 4202 // Otherwise try to use VLVGP to start the sequence in order to 4203 // avoid a false dependency on any previous contents of the vector 4204 // register. This only makes sense if one of the associated elements 4205 // is defined. 4206 unsigned I1 = NumElements / 2 - 1; 4207 unsigned I2 = NumElements - 1; 4208 bool Def1 = !Elems[I1].isUndef(); 4209 bool Def2 = !Elems[I2].isUndef(); 4210 if (Def1 || Def2) { 4211 SDValue Elem1 = Elems[Def1 ? I1 : I2]; 4212 SDValue Elem2 = Elems[Def2 ? I2 : I1]; 4213 Result = DAG.getNode(ISD::BITCAST, DL, VT, 4214 joinDwords(DAG, DL, Elem1, Elem2)); 4215 Done[I1] = true; 4216 Done[I2] = true; 4217 } else 4218 Result = DAG.getUNDEF(VT); 4219 } 4220 4221 // Use VLVGx to insert the other elements. 4222 for (unsigned I = 0; I < NumElements; ++I) 4223 if (!Done[I] && !Elems[I].isUndef()) 4224 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I], 4225 DAG.getConstant(I, DL, MVT::i32)); 4226 return Result; 4227 } 4228 4229 SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op, 4230 SelectionDAG &DAG) const { 4231 const SystemZInstrInfo *TII = 4232 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 4233 auto *BVN = cast<BuildVectorSDNode>(Op.getNode()); 4234 SDLoc DL(Op); 4235 EVT VT = Op.getValueType(); 4236 4237 if (BVN->isConstant()) { 4238 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally- 4239 // preferred way of creating all-zero and all-one vectors so give it 4240 // priority over other methods below. 4241 uint64_t Mask = 0; 4242 if (tryBuildVectorByteMask(BVN, Mask)) { 4243 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, 4244 DAG.getConstant(Mask, DL, MVT::i32)); 4245 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 4246 } 4247 4248 // Try using some form of replication. 4249 APInt SplatBits, SplatUndef; 4250 unsigned SplatBitSize; 4251 bool HasAnyUndefs; 4252 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, 4253 8, true) && 4254 SplatBitSize <= 64) { 4255 // First try assuming that any undefined bits above the highest set bit 4256 // and below the lowest set bit are 1s. This increases the likelihood of 4257 // being able to use a sign-extended element value in VECTOR REPLICATE 4258 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK. 4259 uint64_t SplatBitsZ = SplatBits.getZExtValue(); 4260 uint64_t SplatUndefZ = SplatUndef.getZExtValue(); 4261 uint64_t Lower = (SplatUndefZ 4262 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1)); 4263 uint64_t Upper = (SplatUndefZ 4264 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1)); 4265 uint64_t Value = SplatBitsZ | Upper | Lower; 4266 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, 4267 SplatBitSize); 4268 if (Op.getNode()) 4269 return Op; 4270 4271 // Now try assuming that any undefined bits between the first and 4272 // last defined set bits are set. This increases the chances of 4273 // using a non-wraparound mask. 4274 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower; 4275 Value = SplatBitsZ | Middle; 4276 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize); 4277 if (Op.getNode()) 4278 return Op; 4279 } 4280 4281 // Fall back to loading it from memory. 4282 return SDValue(); 4283 } 4284 4285 // See if we should use shuffles to construct the vector from other vectors. 4286 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN)) 4287 return Res; 4288 4289 // Detect SCALAR_TO_VECTOR conversions. 4290 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op)) 4291 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0)); 4292 4293 // Otherwise use buildVector to build the vector up from GPRs. 4294 unsigned NumElements = Op.getNumOperands(); 4295 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements); 4296 for (unsigned I = 0; I < NumElements; ++I) 4297 Ops[I] = Op.getOperand(I); 4298 return buildVector(DAG, DL, VT, Ops); 4299 } 4300 4301 SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op, 4302 SelectionDAG &DAG) const { 4303 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode()); 4304 SDLoc DL(Op); 4305 EVT VT = Op.getValueType(); 4306 unsigned NumElements = VT.getVectorNumElements(); 4307 4308 if (VSN->isSplat()) { 4309 SDValue Op0 = Op.getOperand(0); 4310 unsigned Index = VSN->getSplatIndex(); 4311 assert(Index < VT.getVectorNumElements() && 4312 "Splat index should be defined and in first operand"); 4313 // See whether the value we're splatting is directly available as a scalar. 4314 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) || 4315 Op0.getOpcode() == ISD::BUILD_VECTOR) 4316 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index)); 4317 // Otherwise keep it as a vector-to-vector operation. 4318 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0), 4319 DAG.getConstant(Index, DL, MVT::i32)); 4320 } 4321 4322 GeneralShuffle GS(VT); 4323 for (unsigned I = 0; I < NumElements; ++I) { 4324 int Elt = VSN->getMaskElt(I); 4325 if (Elt < 0) 4326 GS.addUndef(); 4327 else 4328 GS.add(Op.getOperand(unsigned(Elt) / NumElements), 4329 unsigned(Elt) % NumElements); 4330 } 4331 return GS.getNode(DAG, SDLoc(VSN)); 4332 } 4333 4334 SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op, 4335 SelectionDAG &DAG) const { 4336 SDLoc DL(Op); 4337 // Just insert the scalar into element 0 of an undefined vector. 4338 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, 4339 Op.getValueType(), DAG.getUNDEF(Op.getValueType()), 4340 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32)); 4341 } 4342 4343 SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op, 4344 SelectionDAG &DAG) const { 4345 // Handle insertions of floating-point values. 4346 SDLoc DL(Op); 4347 SDValue Op0 = Op.getOperand(0); 4348 SDValue Op1 = Op.getOperand(1); 4349 SDValue Op2 = Op.getOperand(2); 4350 EVT VT = Op.getValueType(); 4351 4352 // Insertions into constant indices of a v2f64 can be done using VPDI. 4353 // However, if the inserted value is a bitcast or a constant then it's 4354 // better to use GPRs, as below. 4355 if (VT == MVT::v2f64 && 4356 Op1.getOpcode() != ISD::BITCAST && 4357 Op1.getOpcode() != ISD::ConstantFP && 4358 Op2.getOpcode() == ISD::Constant) { 4359 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue(); 4360 unsigned Mask = VT.getVectorNumElements() - 1; 4361 if (Index <= Mask) 4362 return Op; 4363 } 4364 4365 // Otherwise bitcast to the equivalent integer form and insert via a GPR. 4366 MVT IntVT = MVT::getIntegerVT(VT.getVectorElementType().getSizeInBits()); 4367 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements()); 4368 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT, 4369 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), 4370 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2); 4371 return DAG.getNode(ISD::BITCAST, DL, VT, Res); 4372 } 4373 4374 SDValue 4375 SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op, 4376 SelectionDAG &DAG) const { 4377 // Handle extractions of floating-point values. 4378 SDLoc DL(Op); 4379 SDValue Op0 = Op.getOperand(0); 4380 SDValue Op1 = Op.getOperand(1); 4381 EVT VT = Op.getValueType(); 4382 EVT VecVT = Op0.getValueType(); 4383 4384 // Extractions of constant indices can be done directly. 4385 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) { 4386 uint64_t Index = CIndexN->getZExtValue(); 4387 unsigned Mask = VecVT.getVectorNumElements() - 1; 4388 if (Index <= Mask) 4389 return Op; 4390 } 4391 4392 // Otherwise bitcast to the equivalent integer form and extract via a GPR. 4393 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits()); 4394 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements()); 4395 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT, 4396 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1); 4397 return DAG.getNode(ISD::BITCAST, DL, VT, Res); 4398 } 4399 4400 SDValue 4401 SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG, 4402 unsigned UnpackHigh) const { 4403 SDValue PackedOp = Op.getOperand(0); 4404 EVT OutVT = Op.getValueType(); 4405 EVT InVT = PackedOp.getValueType(); 4406 unsigned ToBits = OutVT.getVectorElementType().getSizeInBits(); 4407 unsigned FromBits = InVT.getVectorElementType().getSizeInBits(); 4408 do { 4409 FromBits *= 2; 4410 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits), 4411 SystemZ::VectorBits / FromBits); 4412 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp); 4413 } while (FromBits != ToBits); 4414 return PackedOp; 4415 } 4416 4417 SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG, 4418 unsigned ByScalar) const { 4419 // Look for cases where a vector shift can use the *_BY_SCALAR form. 4420 SDValue Op0 = Op.getOperand(0); 4421 SDValue Op1 = Op.getOperand(1); 4422 SDLoc DL(Op); 4423 EVT VT = Op.getValueType(); 4424 unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits(); 4425 4426 // See whether the shift vector is a splat represented as BUILD_VECTOR. 4427 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) { 4428 APInt SplatBits, SplatUndef; 4429 unsigned SplatBitSize; 4430 bool HasAnyUndefs; 4431 // Check for constant splats. Use ElemBitSize as the minimum element 4432 // width and reject splats that need wider elements. 4433 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, 4434 ElemBitSize, true) && 4435 SplatBitSize == ElemBitSize) { 4436 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff, 4437 DL, MVT::i32); 4438 return DAG.getNode(ByScalar, DL, VT, Op0, Shift); 4439 } 4440 // Check for variable splats. 4441 BitVector UndefElements; 4442 SDValue Splat = BVN->getSplatValue(&UndefElements); 4443 if (Splat) { 4444 // Since i32 is the smallest legal type, we either need a no-op 4445 // or a truncation. 4446 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat); 4447 return DAG.getNode(ByScalar, DL, VT, Op0, Shift); 4448 } 4449 } 4450 4451 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR, 4452 // and the shift amount is directly available in a GPR. 4453 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) { 4454 if (VSN->isSplat()) { 4455 SDValue VSNOp0 = VSN->getOperand(0); 4456 unsigned Index = VSN->getSplatIndex(); 4457 assert(Index < VT.getVectorNumElements() && 4458 "Splat index should be defined and in first operand"); 4459 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) || 4460 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) { 4461 // Since i32 is the smallest legal type, we either need a no-op 4462 // or a truncation. 4463 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, 4464 VSNOp0.getOperand(Index)); 4465 return DAG.getNode(ByScalar, DL, VT, Op0, Shift); 4466 } 4467 } 4468 } 4469 4470 // Otherwise just treat the current form as legal. 4471 return Op; 4472 } 4473 4474 SDValue SystemZTargetLowering::LowerOperation(SDValue Op, 4475 SelectionDAG &DAG) const { 4476 switch (Op.getOpcode()) { 4477 case ISD::FRAMEADDR: 4478 return lowerFRAMEADDR(Op, DAG); 4479 case ISD::RETURNADDR: 4480 return lowerRETURNADDR(Op, DAG); 4481 case ISD::BR_CC: 4482 return lowerBR_CC(Op, DAG); 4483 case ISD::SELECT_CC: 4484 return lowerSELECT_CC(Op, DAG); 4485 case ISD::SETCC: 4486 return lowerSETCC(Op, DAG); 4487 case ISD::GlobalAddress: 4488 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG); 4489 case ISD::GlobalTLSAddress: 4490 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG); 4491 case ISD::BlockAddress: 4492 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG); 4493 case ISD::JumpTable: 4494 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG); 4495 case ISD::ConstantPool: 4496 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG); 4497 case ISD::BITCAST: 4498 return lowerBITCAST(Op, DAG); 4499 case ISD::VASTART: 4500 return lowerVASTART(Op, DAG); 4501 case ISD::VACOPY: 4502 return lowerVACOPY(Op, DAG); 4503 case ISD::DYNAMIC_STACKALLOC: 4504 return lowerDYNAMIC_STACKALLOC(Op, DAG); 4505 case ISD::GET_DYNAMIC_AREA_OFFSET: 4506 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG); 4507 case ISD::SMUL_LOHI: 4508 return lowerSMUL_LOHI(Op, DAG); 4509 case ISD::UMUL_LOHI: 4510 return lowerUMUL_LOHI(Op, DAG); 4511 case ISD::SDIVREM: 4512 return lowerSDIVREM(Op, DAG); 4513 case ISD::UDIVREM: 4514 return lowerUDIVREM(Op, DAG); 4515 case ISD::OR: 4516 return lowerOR(Op, DAG); 4517 case ISD::CTPOP: 4518 return lowerCTPOP(Op, DAG); 4519 case ISD::ATOMIC_FENCE: 4520 return lowerATOMIC_FENCE(Op, DAG); 4521 case ISD::ATOMIC_SWAP: 4522 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW); 4523 case ISD::ATOMIC_STORE: 4524 return lowerATOMIC_STORE(Op, DAG); 4525 case ISD::ATOMIC_LOAD: 4526 return lowerATOMIC_LOAD(Op, DAG); 4527 case ISD::ATOMIC_LOAD_ADD: 4528 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD); 4529 case ISD::ATOMIC_LOAD_SUB: 4530 return lowerATOMIC_LOAD_SUB(Op, DAG); 4531 case ISD::ATOMIC_LOAD_AND: 4532 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND); 4533 case ISD::ATOMIC_LOAD_OR: 4534 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR); 4535 case ISD::ATOMIC_LOAD_XOR: 4536 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR); 4537 case ISD::ATOMIC_LOAD_NAND: 4538 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND); 4539 case ISD::ATOMIC_LOAD_MIN: 4540 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN); 4541 case ISD::ATOMIC_LOAD_MAX: 4542 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX); 4543 case ISD::ATOMIC_LOAD_UMIN: 4544 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN); 4545 case ISD::ATOMIC_LOAD_UMAX: 4546 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX); 4547 case ISD::ATOMIC_CMP_SWAP: 4548 return lowerATOMIC_CMP_SWAP(Op, DAG); 4549 case ISD::STACKSAVE: 4550 return lowerSTACKSAVE(Op, DAG); 4551 case ISD::STACKRESTORE: 4552 return lowerSTACKRESTORE(Op, DAG); 4553 case ISD::PREFETCH: 4554 return lowerPREFETCH(Op, DAG); 4555 case ISD::INTRINSIC_W_CHAIN: 4556 return lowerINTRINSIC_W_CHAIN(Op, DAG); 4557 case ISD::INTRINSIC_WO_CHAIN: 4558 return lowerINTRINSIC_WO_CHAIN(Op, DAG); 4559 case ISD::BUILD_VECTOR: 4560 return lowerBUILD_VECTOR(Op, DAG); 4561 case ISD::VECTOR_SHUFFLE: 4562 return lowerVECTOR_SHUFFLE(Op, DAG); 4563 case ISD::SCALAR_TO_VECTOR: 4564 return lowerSCALAR_TO_VECTOR(Op, DAG); 4565 case ISD::INSERT_VECTOR_ELT: 4566 return lowerINSERT_VECTOR_ELT(Op, DAG); 4567 case ISD::EXTRACT_VECTOR_ELT: 4568 return lowerEXTRACT_VECTOR_ELT(Op, DAG); 4569 case ISD::SIGN_EXTEND_VECTOR_INREG: 4570 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH); 4571 case ISD::ZERO_EXTEND_VECTOR_INREG: 4572 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH); 4573 case ISD::SHL: 4574 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR); 4575 case ISD::SRL: 4576 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR); 4577 case ISD::SRA: 4578 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR); 4579 default: 4580 llvm_unreachable("Unexpected node to lower"); 4581 } 4582 } 4583 4584 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const { 4585 #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME 4586 switch ((SystemZISD::NodeType)Opcode) { 4587 case SystemZISD::FIRST_NUMBER: break; 4588 OPCODE(RET_FLAG); 4589 OPCODE(CALL); 4590 OPCODE(SIBCALL); 4591 OPCODE(TLS_GDCALL); 4592 OPCODE(TLS_LDCALL); 4593 OPCODE(PCREL_WRAPPER); 4594 OPCODE(PCREL_OFFSET); 4595 OPCODE(IABS); 4596 OPCODE(ICMP); 4597 OPCODE(FCMP); 4598 OPCODE(TM); 4599 OPCODE(BR_CCMASK); 4600 OPCODE(SELECT_CCMASK); 4601 OPCODE(ADJDYNALLOC); 4602 OPCODE(EXTRACT_ACCESS); 4603 OPCODE(POPCNT); 4604 OPCODE(UMUL_LOHI64); 4605 OPCODE(SDIVREM32); 4606 OPCODE(SDIVREM64); 4607 OPCODE(UDIVREM32); 4608 OPCODE(UDIVREM64); 4609 OPCODE(MVC); 4610 OPCODE(MVC_LOOP); 4611 OPCODE(NC); 4612 OPCODE(NC_LOOP); 4613 OPCODE(OC); 4614 OPCODE(OC_LOOP); 4615 OPCODE(XC); 4616 OPCODE(XC_LOOP); 4617 OPCODE(CLC); 4618 OPCODE(CLC_LOOP); 4619 OPCODE(STPCPY); 4620 OPCODE(STRCMP); 4621 OPCODE(SEARCH_STRING); 4622 OPCODE(IPM); 4623 OPCODE(SERIALIZE); 4624 OPCODE(MEMBARRIER); 4625 OPCODE(TBEGIN); 4626 OPCODE(TBEGIN_NOFLOAT); 4627 OPCODE(TEND); 4628 OPCODE(BYTE_MASK); 4629 OPCODE(ROTATE_MASK); 4630 OPCODE(REPLICATE); 4631 OPCODE(JOIN_DWORDS); 4632 OPCODE(SPLAT); 4633 OPCODE(MERGE_HIGH); 4634 OPCODE(MERGE_LOW); 4635 OPCODE(SHL_DOUBLE); 4636 OPCODE(PERMUTE_DWORDS); 4637 OPCODE(PERMUTE); 4638 OPCODE(PACK); 4639 OPCODE(PACKS_CC); 4640 OPCODE(PACKLS_CC); 4641 OPCODE(UNPACK_HIGH); 4642 OPCODE(UNPACKL_HIGH); 4643 OPCODE(UNPACK_LOW); 4644 OPCODE(UNPACKL_LOW); 4645 OPCODE(VSHL_BY_SCALAR); 4646 OPCODE(VSRL_BY_SCALAR); 4647 OPCODE(VSRA_BY_SCALAR); 4648 OPCODE(VSUM); 4649 OPCODE(VICMPE); 4650 OPCODE(VICMPH); 4651 OPCODE(VICMPHL); 4652 OPCODE(VICMPES); 4653 OPCODE(VICMPHS); 4654 OPCODE(VICMPHLS); 4655 OPCODE(VFCMPE); 4656 OPCODE(VFCMPH); 4657 OPCODE(VFCMPHE); 4658 OPCODE(VFCMPES); 4659 OPCODE(VFCMPHS); 4660 OPCODE(VFCMPHES); 4661 OPCODE(VFTCI); 4662 OPCODE(VEXTEND); 4663 OPCODE(VROUND); 4664 OPCODE(VTM); 4665 OPCODE(VFAE_CC); 4666 OPCODE(VFAEZ_CC); 4667 OPCODE(VFEE_CC); 4668 OPCODE(VFEEZ_CC); 4669 OPCODE(VFENE_CC); 4670 OPCODE(VFENEZ_CC); 4671 OPCODE(VISTR_CC); 4672 OPCODE(VSTRC_CC); 4673 OPCODE(VSTRCZ_CC); 4674 OPCODE(ATOMIC_SWAPW); 4675 OPCODE(ATOMIC_LOADW_ADD); 4676 OPCODE(ATOMIC_LOADW_SUB); 4677 OPCODE(ATOMIC_LOADW_AND); 4678 OPCODE(ATOMIC_LOADW_OR); 4679 OPCODE(ATOMIC_LOADW_XOR); 4680 OPCODE(ATOMIC_LOADW_NAND); 4681 OPCODE(ATOMIC_LOADW_MIN); 4682 OPCODE(ATOMIC_LOADW_MAX); 4683 OPCODE(ATOMIC_LOADW_UMIN); 4684 OPCODE(ATOMIC_LOADW_UMAX); 4685 OPCODE(ATOMIC_CMP_SWAPW); 4686 OPCODE(LRV); 4687 OPCODE(STRV); 4688 OPCODE(PREFETCH); 4689 } 4690 return nullptr; 4691 #undef OPCODE 4692 } 4693 4694 // Return true if VT is a vector whose elements are a whole number of bytes 4695 // in width. 4696 static bool canTreatAsByteVector(EVT VT) { 4697 return VT.isVector() && VT.getVectorElementType().getSizeInBits() % 8 == 0; 4698 } 4699 4700 // Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT 4701 // producing a result of type ResVT. Op is a possibly bitcast version 4702 // of the input vector and Index is the index (based on type VecVT) that 4703 // should be extracted. Return the new extraction if a simplification 4704 // was possible or if Force is true. 4705 SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT, 4706 EVT VecVT, SDValue Op, 4707 unsigned Index, 4708 DAGCombinerInfo &DCI, 4709 bool Force) const { 4710 SelectionDAG &DAG = DCI.DAG; 4711 4712 // The number of bytes being extracted. 4713 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize(); 4714 4715 for (;;) { 4716 unsigned Opcode = Op.getOpcode(); 4717 if (Opcode == ISD::BITCAST) 4718 // Look through bitcasts. 4719 Op = Op.getOperand(0); 4720 else if (Opcode == ISD::VECTOR_SHUFFLE && 4721 canTreatAsByteVector(Op.getValueType())) { 4722 // Get a VPERM-like permute mask and see whether the bytes covered 4723 // by the extracted element are a contiguous sequence from one 4724 // source operand. 4725 SmallVector<int, SystemZ::VectorBytes> Bytes; 4726 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes); 4727 int First; 4728 if (!getShuffleInput(Bytes, Index * BytesPerElement, 4729 BytesPerElement, First)) 4730 break; 4731 if (First < 0) 4732 return DAG.getUNDEF(ResVT); 4733 // Make sure the contiguous sequence starts at a multiple of the 4734 // original element size. 4735 unsigned Byte = unsigned(First) % Bytes.size(); 4736 if (Byte % BytesPerElement != 0) 4737 break; 4738 // We can get the extracted value directly from an input. 4739 Index = Byte / BytesPerElement; 4740 Op = Op.getOperand(unsigned(First) / Bytes.size()); 4741 Force = true; 4742 } else if (Opcode == ISD::BUILD_VECTOR && 4743 canTreatAsByteVector(Op.getValueType())) { 4744 // We can only optimize this case if the BUILD_VECTOR elements are 4745 // at least as wide as the extracted value. 4746 EVT OpVT = Op.getValueType(); 4747 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize(); 4748 if (OpBytesPerElement < BytesPerElement) 4749 break; 4750 // Make sure that the least-significant bit of the extracted value 4751 // is the least significant bit of an input. 4752 unsigned End = (Index + 1) * BytesPerElement; 4753 if (End % OpBytesPerElement != 0) 4754 break; 4755 // We're extracting the low part of one operand of the BUILD_VECTOR. 4756 Op = Op.getOperand(End / OpBytesPerElement - 1); 4757 if (!Op.getValueType().isInteger()) { 4758 EVT VT = MVT::getIntegerVT(Op.getValueType().getSizeInBits()); 4759 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op); 4760 DCI.AddToWorklist(Op.getNode()); 4761 } 4762 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits()); 4763 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op); 4764 if (VT != ResVT) { 4765 DCI.AddToWorklist(Op.getNode()); 4766 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op); 4767 } 4768 return Op; 4769 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG || 4770 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG || 4771 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) && 4772 canTreatAsByteVector(Op.getValueType()) && 4773 canTreatAsByteVector(Op.getOperand(0).getValueType())) { 4774 // Make sure that only the unextended bits are significant. 4775 EVT ExtVT = Op.getValueType(); 4776 EVT OpVT = Op.getOperand(0).getValueType(); 4777 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize(); 4778 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize(); 4779 unsigned Byte = Index * BytesPerElement; 4780 unsigned SubByte = Byte % ExtBytesPerElement; 4781 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement; 4782 if (SubByte < MinSubByte || 4783 SubByte + BytesPerElement > ExtBytesPerElement) 4784 break; 4785 // Get the byte offset of the unextended element 4786 Byte = Byte / ExtBytesPerElement * OpBytesPerElement; 4787 // ...then add the byte offset relative to that element. 4788 Byte += SubByte - MinSubByte; 4789 if (Byte % BytesPerElement != 0) 4790 break; 4791 Op = Op.getOperand(0); 4792 Index = Byte / BytesPerElement; 4793 Force = true; 4794 } else 4795 break; 4796 } 4797 if (Force) { 4798 if (Op.getValueType() != VecVT) { 4799 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op); 4800 DCI.AddToWorklist(Op.getNode()); 4801 } 4802 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op, 4803 DAG.getConstant(Index, DL, MVT::i32)); 4804 } 4805 return SDValue(); 4806 } 4807 4808 // Optimize vector operations in scalar value Op on the basis that Op 4809 // is truncated to TruncVT. 4810 SDValue SystemZTargetLowering::combineTruncateExtract( 4811 const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const { 4812 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into 4813 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements 4814 // of type TruncVT. 4815 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 4816 TruncVT.getSizeInBits() % 8 == 0) { 4817 SDValue Vec = Op.getOperand(0); 4818 EVT VecVT = Vec.getValueType(); 4819 if (canTreatAsByteVector(VecVT)) { 4820 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 4821 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize(); 4822 unsigned TruncBytes = TruncVT.getStoreSize(); 4823 if (BytesPerElement % TruncBytes == 0) { 4824 // Calculate the value of Y' in the above description. We are 4825 // splitting the original elements into Scale equal-sized pieces 4826 // and for truncation purposes want the last (least-significant) 4827 // of these pieces for IndexN. This is easiest to do by calculating 4828 // the start index of the following element and then subtracting 1. 4829 unsigned Scale = BytesPerElement / TruncBytes; 4830 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1; 4831 4832 // Defer the creation of the bitcast from X to combineExtract, 4833 // which might be able to optimize the extraction. 4834 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8), 4835 VecVT.getStoreSize() / TruncBytes); 4836 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT); 4837 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true); 4838 } 4839 } 4840 } 4841 } 4842 return SDValue(); 4843 } 4844 4845 SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N, 4846 DAGCombinerInfo &DCI) const { 4847 SelectionDAG &DAG = DCI.DAG; 4848 unsigned Opcode = N->getOpcode(); 4849 if (Opcode == ISD::SIGN_EXTEND) { 4850 // Convert (sext (ashr (shl X, C1), C2)) to 4851 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as 4852 // cheap as narrower ones. 4853 SDValue N0 = N->getOperand(0); 4854 EVT VT = N->getValueType(0); 4855 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) { 4856 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 4857 SDValue Inner = N0.getOperand(0); 4858 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) { 4859 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) { 4860 unsigned Extra = (VT.getSizeInBits() - 4861 N0.getValueType().getSizeInBits()); 4862 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra; 4863 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra; 4864 EVT ShiftVT = N0.getOperand(1).getValueType(); 4865 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT, 4866 Inner.getOperand(0)); 4867 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext, 4868 DAG.getConstant(NewShlAmt, SDLoc(Inner), 4869 ShiftVT)); 4870 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, 4871 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT)); 4872 } 4873 } 4874 } 4875 } 4876 if (Opcode == SystemZISD::MERGE_HIGH || 4877 Opcode == SystemZISD::MERGE_LOW) { 4878 SDValue Op0 = N->getOperand(0); 4879 SDValue Op1 = N->getOperand(1); 4880 if (Op0.getOpcode() == ISD::BITCAST) 4881 Op0 = Op0.getOperand(0); 4882 if (Op0.getOpcode() == SystemZISD::BYTE_MASK && 4883 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) { 4884 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF 4885 // for v4f32. 4886 if (Op1 == N->getOperand(0)) 4887 return Op1; 4888 // (z_merge_? 0, X) -> (z_unpackl_? 0, X). 4889 EVT VT = Op1.getValueType(); 4890 unsigned ElemBytes = VT.getVectorElementType().getStoreSize(); 4891 if (ElemBytes <= 4) { 4892 Opcode = (Opcode == SystemZISD::MERGE_HIGH ? 4893 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW); 4894 EVT InVT = VT.changeVectorElementTypeToInteger(); 4895 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16), 4896 SystemZ::VectorBytes / ElemBytes / 2); 4897 if (VT != InVT) { 4898 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1); 4899 DCI.AddToWorklist(Op1.getNode()); 4900 } 4901 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1); 4902 DCI.AddToWorklist(Op.getNode()); 4903 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); 4904 } 4905 } 4906 } 4907 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better 4908 // for the extraction to be done on a vMiN value, so that we can use VSTE. 4909 // If X has wider elements then convert it to: 4910 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z). 4911 if (Opcode == ISD::STORE) { 4912 auto *SN = cast<StoreSDNode>(N); 4913 EVT MemVT = SN->getMemoryVT(); 4914 if (MemVT.isInteger()) { 4915 if (SDValue Value = 4916 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) { 4917 DCI.AddToWorklist(Value.getNode()); 4918 4919 // Rewrite the store with the new form of stored value. 4920 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value, 4921 SN->getBasePtr(), SN->getMemoryVT(), 4922 SN->getMemOperand()); 4923 } 4924 } 4925 } 4926 // Try to simplify a vector extraction. 4927 if (Opcode == ISD::EXTRACT_VECTOR_ELT) { 4928 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) { 4929 SDValue Op0 = N->getOperand(0); 4930 EVT VecVT = Op0.getValueType(); 4931 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0, 4932 IndexN->getZExtValue(), DCI, false); 4933 } 4934 } 4935 // (join_dwords X, X) == (replicate X) 4936 if (Opcode == SystemZISD::JOIN_DWORDS && 4937 N->getOperand(0) == N->getOperand(1)) 4938 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0), 4939 N->getOperand(0)); 4940 // (fround (extract_vector_elt X 0)) 4941 // (fround (extract_vector_elt X 1)) -> 4942 // (extract_vector_elt (VROUND X) 0) 4943 // (extract_vector_elt (VROUND X) 1) 4944 // 4945 // This is a special case since the target doesn't really support v2f32s. 4946 if (Opcode == ISD::FP_ROUND) { 4947 SDValue Op0 = N->getOperand(0); 4948 if (N->getValueType(0) == MVT::f32 && 4949 Op0.hasOneUse() && 4950 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 4951 Op0.getOperand(0).getValueType() == MVT::v2f64 && 4952 Op0.getOperand(1).getOpcode() == ISD::Constant && 4953 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) { 4954 SDValue Vec = Op0.getOperand(0); 4955 for (auto *U : Vec->uses()) { 4956 if (U != Op0.getNode() && 4957 U->hasOneUse() && 4958 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 4959 U->getOperand(0) == Vec && 4960 U->getOperand(1).getOpcode() == ISD::Constant && 4961 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) { 4962 SDValue OtherRound = SDValue(*U->use_begin(), 0); 4963 if (OtherRound.getOpcode() == ISD::FP_ROUND && 4964 OtherRound.getOperand(0) == SDValue(U, 0) && 4965 OtherRound.getValueType() == MVT::f32) { 4966 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N), 4967 MVT::v4f32, Vec); 4968 DCI.AddToWorklist(VRound.getNode()); 4969 SDValue Extract1 = 4970 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32, 4971 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32)); 4972 DCI.AddToWorklist(Extract1.getNode()); 4973 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1); 4974 SDValue Extract0 = 4975 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32, 4976 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32)); 4977 return Extract0; 4978 } 4979 } 4980 } 4981 } 4982 } 4983 4984 // Combine BSWAP (LOAD) into LRVH/LRV/LRVG 4985 // These loads are allowed to access memory multiple times, and so we must check 4986 // that the loads are not volatile before performing the combine. 4987 if (Opcode == ISD::BSWAP && 4988 ISD::isNON_EXTLoad(N->getOperand(0).getNode()) && 4989 N->getOperand(0).hasOneUse() && 4990 (N->getValueType(0) == MVT::i16 || N->getValueType(0) == MVT::i32 || 4991 N->getValueType(0) == MVT::i64) && 4992 !cast<LoadSDNode>(N->getOperand(0))->isVolatile()) { 4993 SDValue Load = N->getOperand(0); 4994 LoadSDNode *LD = cast<LoadSDNode>(Load); 4995 4996 // Create the byte-swapping load. 4997 SDValue Ops[] = { 4998 LD->getChain(), // Chain 4999 LD->getBasePtr(), // Ptr 5000 DAG.getValueType(N->getValueType(0)) // VT 5001 }; 5002 SDValue BSLoad = 5003 DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N), 5004 DAG.getVTList(N->getValueType(0) == MVT::i64 ? 5005 MVT::i64 : MVT::i32, MVT::Other), 5006 Ops, LD->getMemoryVT(), LD->getMemOperand()); 5007 5008 // If this is an i16 load, insert the truncate. 5009 SDValue ResVal = BSLoad; 5010 if (N->getValueType(0) == MVT::i16) 5011 ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad); 5012 5013 // First, combine the bswap away. This makes the value produced by the 5014 // load dead. 5015 DCI.CombineTo(N, ResVal); 5016 5017 // Next, combine the load away, we give it a bogus result value but a real 5018 // chain result. The result value is dead because the bswap is dead. 5019 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1)); 5020 5021 // Return N so it doesn't get rechecked! 5022 return SDValue(N, 0); 5023 } 5024 5025 // Combine STORE (BSWAP) into STRVH/STRV/STRVG 5026 // See comment above about volatile accesses. 5027 if (Opcode == ISD::STORE && 5028 !cast<StoreSDNode>(N)->isVolatile() && 5029 N->getOperand(1).getOpcode() == ISD::BSWAP && 5030 N->getOperand(1).getNode()->hasOneUse() && 5031 (N->getOperand(1).getValueType() == MVT::i16 || 5032 N->getOperand(1).getValueType() == MVT::i32 || 5033 N->getOperand(1).getValueType() == MVT::i64)) { 5034 5035 SDValue BSwapOp = N->getOperand(1).getOperand(0); 5036 5037 if (BSwapOp.getValueType() == MVT::i16) 5038 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp); 5039 5040 SDValue Ops[] = { 5041 N->getOperand(0), BSwapOp, N->getOperand(2), 5042 DAG.getValueType(N->getOperand(1).getValueType()) 5043 }; 5044 5045 return 5046 DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other), 5047 Ops, cast<StoreSDNode>(N)->getMemoryVT(), 5048 cast<StoreSDNode>(N)->getMemOperand()); 5049 } 5050 5051 return SDValue(); 5052 } 5053 5054 //===----------------------------------------------------------------------===// 5055 // Custom insertion 5056 //===----------------------------------------------------------------------===// 5057 5058 // Create a new basic block after MBB. 5059 static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) { 5060 MachineFunction &MF = *MBB->getParent(); 5061 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock()); 5062 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB); 5063 return NewMBB; 5064 } 5065 5066 // Split MBB after MI and return the new block (the one that contains 5067 // instructions after MI). 5068 static MachineBasicBlock *splitBlockAfter(MachineInstr *MI, 5069 MachineBasicBlock *MBB) { 5070 MachineBasicBlock *NewMBB = emitBlockAfter(MBB); 5071 NewMBB->splice(NewMBB->begin(), MBB, 5072 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 5073 NewMBB->transferSuccessorsAndUpdatePHIs(MBB); 5074 return NewMBB; 5075 } 5076 5077 // Split MBB before MI and return the new block (the one that contains MI). 5078 static MachineBasicBlock *splitBlockBefore(MachineInstr *MI, 5079 MachineBasicBlock *MBB) { 5080 MachineBasicBlock *NewMBB = emitBlockAfter(MBB); 5081 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end()); 5082 NewMBB->transferSuccessorsAndUpdatePHIs(MBB); 5083 return NewMBB; 5084 } 5085 5086 // Force base value Base into a register before MI. Return the register. 5087 static unsigned forceReg(MachineInstr *MI, MachineOperand &Base, 5088 const SystemZInstrInfo *TII) { 5089 if (Base.isReg()) 5090 return Base.getReg(); 5091 5092 MachineBasicBlock *MBB = MI->getParent(); 5093 MachineFunction &MF = *MBB->getParent(); 5094 MachineRegisterInfo &MRI = MF.getRegInfo(); 5095 5096 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 5097 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg) 5098 .addOperand(Base).addImm(0).addReg(0); 5099 return Reg; 5100 } 5101 5102 // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI. 5103 MachineBasicBlock * 5104 SystemZTargetLowering::emitSelect(MachineInstr *MI, 5105 MachineBasicBlock *MBB) const { 5106 const SystemZInstrInfo *TII = 5107 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5108 5109 unsigned DestReg = MI->getOperand(0).getReg(); 5110 unsigned TrueReg = MI->getOperand(1).getReg(); 5111 unsigned FalseReg = MI->getOperand(2).getReg(); 5112 unsigned CCValid = MI->getOperand(3).getImm(); 5113 unsigned CCMask = MI->getOperand(4).getImm(); 5114 DebugLoc DL = MI->getDebugLoc(); 5115 5116 MachineBasicBlock *StartMBB = MBB; 5117 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB); 5118 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB); 5119 5120 // StartMBB: 5121 // BRC CCMask, JoinMBB 5122 // # fallthrough to FalseMBB 5123 MBB = StartMBB; 5124 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5125 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); 5126 MBB->addSuccessor(JoinMBB); 5127 MBB->addSuccessor(FalseMBB); 5128 5129 // FalseMBB: 5130 // # fallthrough to JoinMBB 5131 MBB = FalseMBB; 5132 MBB->addSuccessor(JoinMBB); 5133 5134 // JoinMBB: 5135 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ] 5136 // ... 5137 MBB = JoinMBB; 5138 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg) 5139 .addReg(TrueReg).addMBB(StartMBB) 5140 .addReg(FalseReg).addMBB(FalseMBB); 5141 5142 MI->eraseFromParent(); 5143 return JoinMBB; 5144 } 5145 5146 // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI. 5147 // StoreOpcode is the store to use and Invert says whether the store should 5148 // happen when the condition is false rather than true. If a STORE ON 5149 // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0. 5150 MachineBasicBlock * 5151 SystemZTargetLowering::emitCondStore(MachineInstr *MI, 5152 MachineBasicBlock *MBB, 5153 unsigned StoreOpcode, unsigned STOCOpcode, 5154 bool Invert) const { 5155 const SystemZInstrInfo *TII = 5156 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5157 5158 unsigned SrcReg = MI->getOperand(0).getReg(); 5159 MachineOperand Base = MI->getOperand(1); 5160 int64_t Disp = MI->getOperand(2).getImm(); 5161 unsigned IndexReg = MI->getOperand(3).getReg(); 5162 unsigned CCValid = MI->getOperand(4).getImm(); 5163 unsigned CCMask = MI->getOperand(5).getImm(); 5164 DebugLoc DL = MI->getDebugLoc(); 5165 5166 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp); 5167 5168 // Use STOCOpcode if possible. We could use different store patterns in 5169 // order to avoid matching the index register, but the performance trade-offs 5170 // might be more complicated in that case. 5171 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) { 5172 if (Invert) 5173 CCMask ^= CCValid; 5174 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode)) 5175 .addReg(SrcReg).addOperand(Base).addImm(Disp) 5176 .addImm(CCValid).addImm(CCMask); 5177 MI->eraseFromParent(); 5178 return MBB; 5179 } 5180 5181 // Get the condition needed to branch around the store. 5182 if (!Invert) 5183 CCMask ^= CCValid; 5184 5185 MachineBasicBlock *StartMBB = MBB; 5186 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB); 5187 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB); 5188 5189 // StartMBB: 5190 // BRC CCMask, JoinMBB 5191 // # fallthrough to FalseMBB 5192 MBB = StartMBB; 5193 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5194 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); 5195 MBB->addSuccessor(JoinMBB); 5196 MBB->addSuccessor(FalseMBB); 5197 5198 // FalseMBB: 5199 // store %SrcReg, %Disp(%Index,%Base) 5200 // # fallthrough to JoinMBB 5201 MBB = FalseMBB; 5202 BuildMI(MBB, DL, TII->get(StoreOpcode)) 5203 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg); 5204 MBB->addSuccessor(JoinMBB); 5205 5206 MI->eraseFromParent(); 5207 return JoinMBB; 5208 } 5209 5210 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_* 5211 // or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that 5212 // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}. 5213 // BitSize is the width of the field in bits, or 0 if this is a partword 5214 // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize 5215 // is one of the operands. Invert says whether the field should be 5216 // inverted after performing BinOpcode (e.g. for NAND). 5217 MachineBasicBlock * 5218 SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI, 5219 MachineBasicBlock *MBB, 5220 unsigned BinOpcode, 5221 unsigned BitSize, 5222 bool Invert) const { 5223 MachineFunction &MF = *MBB->getParent(); 5224 const SystemZInstrInfo *TII = 5225 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5226 MachineRegisterInfo &MRI = MF.getRegInfo(); 5227 bool IsSubWord = (BitSize < 32); 5228 5229 // Extract the operands. Base can be a register or a frame index. 5230 // Src2 can be a register or immediate. 5231 unsigned Dest = MI->getOperand(0).getReg(); 5232 MachineOperand Base = earlyUseOperand(MI->getOperand(1)); 5233 int64_t Disp = MI->getOperand(2).getImm(); 5234 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3)); 5235 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0); 5236 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0); 5237 DebugLoc DL = MI->getDebugLoc(); 5238 if (IsSubWord) 5239 BitSize = MI->getOperand(6).getImm(); 5240 5241 // Subword operations use 32-bit registers. 5242 const TargetRegisterClass *RC = (BitSize <= 32 ? 5243 &SystemZ::GR32BitRegClass : 5244 &SystemZ::GR64BitRegClass); 5245 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; 5246 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; 5247 5248 // Get the right opcodes for the displacement. 5249 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); 5250 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); 5251 assert(LOpcode && CSOpcode && "Displacement out of range"); 5252 5253 // Create virtual registers for temporary results. 5254 unsigned OrigVal = MRI.createVirtualRegister(RC); 5255 unsigned OldVal = MRI.createVirtualRegister(RC); 5256 unsigned NewVal = (BinOpcode || IsSubWord ? 5257 MRI.createVirtualRegister(RC) : Src2.getReg()); 5258 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); 5259 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); 5260 5261 // Insert a basic block for the main loop. 5262 MachineBasicBlock *StartMBB = MBB; 5263 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 5264 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 5265 5266 // StartMBB: 5267 // ... 5268 // %OrigVal = L Disp(%Base) 5269 // # fall through to LoopMMB 5270 MBB = StartMBB; 5271 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal) 5272 .addOperand(Base).addImm(Disp).addReg(0); 5273 MBB->addSuccessor(LoopMBB); 5274 5275 // LoopMBB: 5276 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ] 5277 // %RotatedOldVal = RLL %OldVal, 0(%BitShift) 5278 // %RotatedNewVal = OP %RotatedOldVal, %Src2 5279 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) 5280 // %Dest = CS %OldVal, %NewVal, Disp(%Base) 5281 // JNE LoopMBB 5282 // # fall through to DoneMMB 5283 MBB = LoopMBB; 5284 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 5285 .addReg(OrigVal).addMBB(StartMBB) 5286 .addReg(Dest).addMBB(LoopMBB); 5287 if (IsSubWord) 5288 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) 5289 .addReg(OldVal).addReg(BitShift).addImm(0); 5290 if (Invert) { 5291 // Perform the operation normally and then invert every bit of the field. 5292 unsigned Tmp = MRI.createVirtualRegister(RC); 5293 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp) 5294 .addReg(RotatedOldVal).addOperand(Src2); 5295 if (BitSize <= 32) 5296 // XILF with the upper BitSize bits set. 5297 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal) 5298 .addReg(Tmp).addImm(-1U << (32 - BitSize)); 5299 else { 5300 // Use LCGR and add -1 to the result, which is more compact than 5301 // an XILF, XILH pair. 5302 unsigned Tmp2 = MRI.createVirtualRegister(RC); 5303 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp); 5304 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal) 5305 .addReg(Tmp2).addImm(-1); 5306 } 5307 } else if (BinOpcode) 5308 // A simply binary operation. 5309 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal) 5310 .addReg(RotatedOldVal).addOperand(Src2); 5311 else if (IsSubWord) 5312 // Use RISBG to rotate Src2 into position and use it to replace the 5313 // field in RotatedOldVal. 5314 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal) 5315 .addReg(RotatedOldVal).addReg(Src2.getReg()) 5316 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize); 5317 if (IsSubWord) 5318 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) 5319 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); 5320 BuildMI(MBB, DL, TII->get(CSOpcode), Dest) 5321 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp); 5322 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5323 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 5324 MBB->addSuccessor(LoopMBB); 5325 MBB->addSuccessor(DoneMBB); 5326 5327 MI->eraseFromParent(); 5328 return DoneMBB; 5329 } 5330 5331 // Implement EmitInstrWithCustomInserter for pseudo 5332 // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the 5333 // instruction that should be used to compare the current field with the 5334 // minimum or maximum value. KeepOldMask is the BRC condition-code mask 5335 // for when the current field should be kept. BitSize is the width of 5336 // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction. 5337 MachineBasicBlock * 5338 SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI, 5339 MachineBasicBlock *MBB, 5340 unsigned CompareOpcode, 5341 unsigned KeepOldMask, 5342 unsigned BitSize) const { 5343 MachineFunction &MF = *MBB->getParent(); 5344 const SystemZInstrInfo *TII = 5345 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5346 MachineRegisterInfo &MRI = MF.getRegInfo(); 5347 bool IsSubWord = (BitSize < 32); 5348 5349 // Extract the operands. Base can be a register or a frame index. 5350 unsigned Dest = MI->getOperand(0).getReg(); 5351 MachineOperand Base = earlyUseOperand(MI->getOperand(1)); 5352 int64_t Disp = MI->getOperand(2).getImm(); 5353 unsigned Src2 = MI->getOperand(3).getReg(); 5354 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0); 5355 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0); 5356 DebugLoc DL = MI->getDebugLoc(); 5357 if (IsSubWord) 5358 BitSize = MI->getOperand(6).getImm(); 5359 5360 // Subword operations use 32-bit registers. 5361 const TargetRegisterClass *RC = (BitSize <= 32 ? 5362 &SystemZ::GR32BitRegClass : 5363 &SystemZ::GR64BitRegClass); 5364 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; 5365 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; 5366 5367 // Get the right opcodes for the displacement. 5368 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); 5369 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); 5370 assert(LOpcode && CSOpcode && "Displacement out of range"); 5371 5372 // Create virtual registers for temporary results. 5373 unsigned OrigVal = MRI.createVirtualRegister(RC); 5374 unsigned OldVal = MRI.createVirtualRegister(RC); 5375 unsigned NewVal = MRI.createVirtualRegister(RC); 5376 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); 5377 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2); 5378 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); 5379 5380 // Insert 3 basic blocks for the loop. 5381 MachineBasicBlock *StartMBB = MBB; 5382 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 5383 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 5384 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB); 5385 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB); 5386 5387 // StartMBB: 5388 // ... 5389 // %OrigVal = L Disp(%Base) 5390 // # fall through to LoopMMB 5391 MBB = StartMBB; 5392 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal) 5393 .addOperand(Base).addImm(Disp).addReg(0); 5394 MBB->addSuccessor(LoopMBB); 5395 5396 // LoopMBB: 5397 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ] 5398 // %RotatedOldVal = RLL %OldVal, 0(%BitShift) 5399 // CompareOpcode %RotatedOldVal, %Src2 5400 // BRC KeepOldMask, UpdateMBB 5401 MBB = LoopMBB; 5402 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 5403 .addReg(OrigVal).addMBB(StartMBB) 5404 .addReg(Dest).addMBB(UpdateMBB); 5405 if (IsSubWord) 5406 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) 5407 .addReg(OldVal).addReg(BitShift).addImm(0); 5408 BuildMI(MBB, DL, TII->get(CompareOpcode)) 5409 .addReg(RotatedOldVal).addReg(Src2); 5410 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5411 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB); 5412 MBB->addSuccessor(UpdateMBB); 5413 MBB->addSuccessor(UseAltMBB); 5414 5415 // UseAltMBB: 5416 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0 5417 // # fall through to UpdateMMB 5418 MBB = UseAltMBB; 5419 if (IsSubWord) 5420 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal) 5421 .addReg(RotatedOldVal).addReg(Src2) 5422 .addImm(32).addImm(31 + BitSize).addImm(0); 5423 MBB->addSuccessor(UpdateMBB); 5424 5425 // UpdateMBB: 5426 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ], 5427 // [ %RotatedAltVal, UseAltMBB ] 5428 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) 5429 // %Dest = CS %OldVal, %NewVal, Disp(%Base) 5430 // JNE LoopMBB 5431 // # fall through to DoneMMB 5432 MBB = UpdateMBB; 5433 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal) 5434 .addReg(RotatedOldVal).addMBB(LoopMBB) 5435 .addReg(RotatedAltVal).addMBB(UseAltMBB); 5436 if (IsSubWord) 5437 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) 5438 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); 5439 BuildMI(MBB, DL, TII->get(CSOpcode), Dest) 5440 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp); 5441 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5442 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 5443 MBB->addSuccessor(LoopMBB); 5444 MBB->addSuccessor(DoneMBB); 5445 5446 MI->eraseFromParent(); 5447 return DoneMBB; 5448 } 5449 5450 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW 5451 // instruction MI. 5452 MachineBasicBlock * 5453 SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI, 5454 MachineBasicBlock *MBB) const { 5455 5456 MachineFunction &MF = *MBB->getParent(); 5457 const SystemZInstrInfo *TII = 5458 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5459 MachineRegisterInfo &MRI = MF.getRegInfo(); 5460 5461 // Extract the operands. Base can be a register or a frame index. 5462 unsigned Dest = MI->getOperand(0).getReg(); 5463 MachineOperand Base = earlyUseOperand(MI->getOperand(1)); 5464 int64_t Disp = MI->getOperand(2).getImm(); 5465 unsigned OrigCmpVal = MI->getOperand(3).getReg(); 5466 unsigned OrigSwapVal = MI->getOperand(4).getReg(); 5467 unsigned BitShift = MI->getOperand(5).getReg(); 5468 unsigned NegBitShift = MI->getOperand(6).getReg(); 5469 int64_t BitSize = MI->getOperand(7).getImm(); 5470 DebugLoc DL = MI->getDebugLoc(); 5471 5472 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass; 5473 5474 // Get the right opcodes for the displacement. 5475 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp); 5476 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp); 5477 assert(LOpcode && CSOpcode && "Displacement out of range"); 5478 5479 // Create virtual registers for temporary results. 5480 unsigned OrigOldVal = MRI.createVirtualRegister(RC); 5481 unsigned OldVal = MRI.createVirtualRegister(RC); 5482 unsigned CmpVal = MRI.createVirtualRegister(RC); 5483 unsigned SwapVal = MRI.createVirtualRegister(RC); 5484 unsigned StoreVal = MRI.createVirtualRegister(RC); 5485 unsigned RetryOldVal = MRI.createVirtualRegister(RC); 5486 unsigned RetryCmpVal = MRI.createVirtualRegister(RC); 5487 unsigned RetrySwapVal = MRI.createVirtualRegister(RC); 5488 5489 // Insert 2 basic blocks for the loop. 5490 MachineBasicBlock *StartMBB = MBB; 5491 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 5492 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 5493 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB); 5494 5495 // StartMBB: 5496 // ... 5497 // %OrigOldVal = L Disp(%Base) 5498 // # fall through to LoopMMB 5499 MBB = StartMBB; 5500 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal) 5501 .addOperand(Base).addImm(Disp).addReg(0); 5502 MBB->addSuccessor(LoopMBB); 5503 5504 // LoopMBB: 5505 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ] 5506 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ] 5507 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ] 5508 // %Dest = RLL %OldVal, BitSize(%BitShift) 5509 // ^^ The low BitSize bits contain the field 5510 // of interest. 5511 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0 5512 // ^^ Replace the upper 32-BitSize bits of the 5513 // comparison value with those that we loaded, 5514 // so that we can use a full word comparison. 5515 // CR %Dest, %RetryCmpVal 5516 // JNE DoneMBB 5517 // # Fall through to SetMBB 5518 MBB = LoopMBB; 5519 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 5520 .addReg(OrigOldVal).addMBB(StartMBB) 5521 .addReg(RetryOldVal).addMBB(SetMBB); 5522 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal) 5523 .addReg(OrigCmpVal).addMBB(StartMBB) 5524 .addReg(RetryCmpVal).addMBB(SetMBB); 5525 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal) 5526 .addReg(OrigSwapVal).addMBB(StartMBB) 5527 .addReg(RetrySwapVal).addMBB(SetMBB); 5528 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest) 5529 .addReg(OldVal).addReg(BitShift).addImm(BitSize); 5530 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal) 5531 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); 5532 BuildMI(MBB, DL, TII->get(SystemZ::CR)) 5533 .addReg(Dest).addReg(RetryCmpVal); 5534 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5535 .addImm(SystemZ::CCMASK_ICMP) 5536 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB); 5537 MBB->addSuccessor(DoneMBB); 5538 MBB->addSuccessor(SetMBB); 5539 5540 // SetMBB: 5541 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0 5542 // ^^ Replace the upper 32-BitSize bits of the new 5543 // value with those that we loaded. 5544 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift) 5545 // ^^ Rotate the new field to its proper position. 5546 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base) 5547 // JNE LoopMBB 5548 // # fall through to ExitMMB 5549 MBB = SetMBB; 5550 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal) 5551 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); 5552 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal) 5553 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize); 5554 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal) 5555 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp); 5556 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5557 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 5558 MBB->addSuccessor(LoopMBB); 5559 MBB->addSuccessor(DoneMBB); 5560 5561 MI->eraseFromParent(); 5562 return DoneMBB; 5563 } 5564 5565 // Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true 5566 // if the high register of the GR128 value must be cleared or false if 5567 // it's "don't care". SubReg is subreg_l32 when extending a GR32 5568 // and subreg_l64 when extending a GR64. 5569 MachineBasicBlock * 5570 SystemZTargetLowering::emitExt128(MachineInstr *MI, 5571 MachineBasicBlock *MBB, 5572 bool ClearEven, unsigned SubReg) const { 5573 MachineFunction &MF = *MBB->getParent(); 5574 const SystemZInstrInfo *TII = 5575 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5576 MachineRegisterInfo &MRI = MF.getRegInfo(); 5577 DebugLoc DL = MI->getDebugLoc(); 5578 5579 unsigned Dest = MI->getOperand(0).getReg(); 5580 unsigned Src = MI->getOperand(1).getReg(); 5581 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 5582 5583 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128); 5584 if (ClearEven) { 5585 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 5586 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass); 5587 5588 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64) 5589 .addImm(0); 5590 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128) 5591 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64); 5592 In128 = NewIn128; 5593 } 5594 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest) 5595 .addReg(In128).addReg(Src).addImm(SubReg); 5596 5597 MI->eraseFromParent(); 5598 return MBB; 5599 } 5600 5601 MachineBasicBlock * 5602 SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI, 5603 MachineBasicBlock *MBB, 5604 unsigned Opcode) const { 5605 MachineFunction &MF = *MBB->getParent(); 5606 const SystemZInstrInfo *TII = 5607 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5608 MachineRegisterInfo &MRI = MF.getRegInfo(); 5609 DebugLoc DL = MI->getDebugLoc(); 5610 5611 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0)); 5612 uint64_t DestDisp = MI->getOperand(1).getImm(); 5613 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2)); 5614 uint64_t SrcDisp = MI->getOperand(3).getImm(); 5615 uint64_t Length = MI->getOperand(4).getImm(); 5616 5617 // When generating more than one CLC, all but the last will need to 5618 // branch to the end when a difference is found. 5619 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ? 5620 splitBlockAfter(MI, MBB) : nullptr); 5621 5622 // Check for the loop form, in which operand 5 is the trip count. 5623 if (MI->getNumExplicitOperands() > 5) { 5624 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase); 5625 5626 uint64_t StartCountReg = MI->getOperand(5).getReg(); 5627 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII); 5628 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg : 5629 forceReg(MI, DestBase, TII)); 5630 5631 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass; 5632 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC); 5633 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg : 5634 MRI.createVirtualRegister(RC)); 5635 uint64_t NextSrcReg = MRI.createVirtualRegister(RC); 5636 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg : 5637 MRI.createVirtualRegister(RC)); 5638 5639 RC = &SystemZ::GR64BitRegClass; 5640 uint64_t ThisCountReg = MRI.createVirtualRegister(RC); 5641 uint64_t NextCountReg = MRI.createVirtualRegister(RC); 5642 5643 MachineBasicBlock *StartMBB = MBB; 5644 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 5645 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 5646 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB); 5647 5648 // StartMBB: 5649 // # fall through to LoopMMB 5650 MBB->addSuccessor(LoopMBB); 5651 5652 // LoopMBB: 5653 // %ThisDestReg = phi [ %StartDestReg, StartMBB ], 5654 // [ %NextDestReg, NextMBB ] 5655 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ], 5656 // [ %NextSrcReg, NextMBB ] 5657 // %ThisCountReg = phi [ %StartCountReg, StartMBB ], 5658 // [ %NextCountReg, NextMBB ] 5659 // ( PFD 2, 768+DestDisp(%ThisDestReg) ) 5660 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg) 5661 // ( JLH EndMBB ) 5662 // 5663 // The prefetch is used only for MVC. The JLH is used only for CLC. 5664 MBB = LoopMBB; 5665 5666 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg) 5667 .addReg(StartDestReg).addMBB(StartMBB) 5668 .addReg(NextDestReg).addMBB(NextMBB); 5669 if (!HaveSingleBase) 5670 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg) 5671 .addReg(StartSrcReg).addMBB(StartMBB) 5672 .addReg(NextSrcReg).addMBB(NextMBB); 5673 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg) 5674 .addReg(StartCountReg).addMBB(StartMBB) 5675 .addReg(NextCountReg).addMBB(NextMBB); 5676 if (Opcode == SystemZ::MVC) 5677 BuildMI(MBB, DL, TII->get(SystemZ::PFD)) 5678 .addImm(SystemZ::PFD_WRITE) 5679 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0); 5680 BuildMI(MBB, DL, TII->get(Opcode)) 5681 .addReg(ThisDestReg).addImm(DestDisp).addImm(256) 5682 .addReg(ThisSrcReg).addImm(SrcDisp); 5683 if (EndMBB) { 5684 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5685 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 5686 .addMBB(EndMBB); 5687 MBB->addSuccessor(EndMBB); 5688 MBB->addSuccessor(NextMBB); 5689 } 5690 5691 // NextMBB: 5692 // %NextDestReg = LA 256(%ThisDestReg) 5693 // %NextSrcReg = LA 256(%ThisSrcReg) 5694 // %NextCountReg = AGHI %ThisCountReg, -1 5695 // CGHI %NextCountReg, 0 5696 // JLH LoopMBB 5697 // # fall through to DoneMMB 5698 // 5699 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes. 5700 MBB = NextMBB; 5701 5702 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg) 5703 .addReg(ThisDestReg).addImm(256).addReg(0); 5704 if (!HaveSingleBase) 5705 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg) 5706 .addReg(ThisSrcReg).addImm(256).addReg(0); 5707 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg) 5708 .addReg(ThisCountReg).addImm(-1); 5709 BuildMI(MBB, DL, TII->get(SystemZ::CGHI)) 5710 .addReg(NextCountReg).addImm(0); 5711 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5712 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 5713 .addMBB(LoopMBB); 5714 MBB->addSuccessor(LoopMBB); 5715 MBB->addSuccessor(DoneMBB); 5716 5717 DestBase = MachineOperand::CreateReg(NextDestReg, false); 5718 SrcBase = MachineOperand::CreateReg(NextSrcReg, false); 5719 Length &= 255; 5720 MBB = DoneMBB; 5721 } 5722 // Handle any remaining bytes with straight-line code. 5723 while (Length > 0) { 5724 uint64_t ThisLength = std::min(Length, uint64_t(256)); 5725 // The previous iteration might have created out-of-range displacements. 5726 // Apply them using LAY if so. 5727 if (!isUInt<12>(DestDisp)) { 5728 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 5729 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg) 5730 .addOperand(DestBase).addImm(DestDisp).addReg(0); 5731 DestBase = MachineOperand::CreateReg(Reg, false); 5732 DestDisp = 0; 5733 } 5734 if (!isUInt<12>(SrcDisp)) { 5735 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 5736 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg) 5737 .addOperand(SrcBase).addImm(SrcDisp).addReg(0); 5738 SrcBase = MachineOperand::CreateReg(Reg, false); 5739 SrcDisp = 0; 5740 } 5741 BuildMI(*MBB, MI, DL, TII->get(Opcode)) 5742 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength) 5743 .addOperand(SrcBase).addImm(SrcDisp); 5744 DestDisp += ThisLength; 5745 SrcDisp += ThisLength; 5746 Length -= ThisLength; 5747 // If there's another CLC to go, branch to the end if a difference 5748 // was found. 5749 if (EndMBB && Length > 0) { 5750 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB); 5751 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5752 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 5753 .addMBB(EndMBB); 5754 MBB->addSuccessor(EndMBB); 5755 MBB->addSuccessor(NextMBB); 5756 MBB = NextMBB; 5757 } 5758 } 5759 if (EndMBB) { 5760 MBB->addSuccessor(EndMBB); 5761 MBB = EndMBB; 5762 MBB->addLiveIn(SystemZ::CC); 5763 } 5764 5765 MI->eraseFromParent(); 5766 return MBB; 5767 } 5768 5769 // Decompose string pseudo-instruction MI into a loop that continually performs 5770 // Opcode until CC != 3. 5771 MachineBasicBlock * 5772 SystemZTargetLowering::emitStringWrapper(MachineInstr *MI, 5773 MachineBasicBlock *MBB, 5774 unsigned Opcode) const { 5775 MachineFunction &MF = *MBB->getParent(); 5776 const SystemZInstrInfo *TII = 5777 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5778 MachineRegisterInfo &MRI = MF.getRegInfo(); 5779 DebugLoc DL = MI->getDebugLoc(); 5780 5781 uint64_t End1Reg = MI->getOperand(0).getReg(); 5782 uint64_t Start1Reg = MI->getOperand(1).getReg(); 5783 uint64_t Start2Reg = MI->getOperand(2).getReg(); 5784 uint64_t CharReg = MI->getOperand(3).getReg(); 5785 5786 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass; 5787 uint64_t This1Reg = MRI.createVirtualRegister(RC); 5788 uint64_t This2Reg = MRI.createVirtualRegister(RC); 5789 uint64_t End2Reg = MRI.createVirtualRegister(RC); 5790 5791 MachineBasicBlock *StartMBB = MBB; 5792 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 5793 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 5794 5795 // StartMBB: 5796 // # fall through to LoopMMB 5797 MBB->addSuccessor(LoopMBB); 5798 5799 // LoopMBB: 5800 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ] 5801 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ] 5802 // R0L = %CharReg 5803 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L 5804 // JO LoopMBB 5805 // # fall through to DoneMMB 5806 // 5807 // The load of R0L can be hoisted by post-RA LICM. 5808 MBB = LoopMBB; 5809 5810 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg) 5811 .addReg(Start1Reg).addMBB(StartMBB) 5812 .addReg(End1Reg).addMBB(LoopMBB); 5813 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg) 5814 .addReg(Start2Reg).addMBB(StartMBB) 5815 .addReg(End2Reg).addMBB(LoopMBB); 5816 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg); 5817 BuildMI(MBB, DL, TII->get(Opcode)) 5818 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define) 5819 .addReg(This1Reg).addReg(This2Reg); 5820 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5821 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB); 5822 MBB->addSuccessor(LoopMBB); 5823 MBB->addSuccessor(DoneMBB); 5824 5825 DoneMBB->addLiveIn(SystemZ::CC); 5826 5827 MI->eraseFromParent(); 5828 return DoneMBB; 5829 } 5830 5831 // Update TBEGIN instruction with final opcode and register clobbers. 5832 MachineBasicBlock * 5833 SystemZTargetLowering::emitTransactionBegin(MachineInstr *MI, 5834 MachineBasicBlock *MBB, 5835 unsigned Opcode, 5836 bool NoFloat) const { 5837 MachineFunction &MF = *MBB->getParent(); 5838 const TargetFrameLowering *TFI = Subtarget.getFrameLowering(); 5839 const SystemZInstrInfo *TII = Subtarget.getInstrInfo(); 5840 5841 // Update opcode. 5842 MI->setDesc(TII->get(Opcode)); 5843 5844 // We cannot handle a TBEGIN that clobbers the stack or frame pointer. 5845 // Make sure to add the corresponding GRSM bits if they are missing. 5846 uint64_t Control = MI->getOperand(2).getImm(); 5847 static const unsigned GPRControlBit[16] = { 5848 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000, 5849 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100 5850 }; 5851 Control |= GPRControlBit[15]; 5852 if (TFI->hasFP(MF)) 5853 Control |= GPRControlBit[11]; 5854 MI->getOperand(2).setImm(Control); 5855 5856 // Add GPR clobbers. 5857 for (int I = 0; I < 16; I++) { 5858 if ((Control & GPRControlBit[I]) == 0) { 5859 unsigned Reg = SystemZMC::GR64Regs[I]; 5860 MI->addOperand(MachineOperand::CreateReg(Reg, true, true)); 5861 } 5862 } 5863 5864 // Add FPR/VR clobbers. 5865 if (!NoFloat && (Control & 4) != 0) { 5866 if (Subtarget.hasVector()) { 5867 for (int I = 0; I < 32; I++) { 5868 unsigned Reg = SystemZMC::VR128Regs[I]; 5869 MI->addOperand(MachineOperand::CreateReg(Reg, true, true)); 5870 } 5871 } else { 5872 for (int I = 0; I < 16; I++) { 5873 unsigned Reg = SystemZMC::FP64Regs[I]; 5874 MI->addOperand(MachineOperand::CreateReg(Reg, true, true)); 5875 } 5876 } 5877 } 5878 5879 return MBB; 5880 } 5881 5882 MachineBasicBlock * 5883 SystemZTargetLowering::emitLoadAndTestCmp0(MachineInstr *MI, 5884 MachineBasicBlock *MBB, 5885 unsigned Opcode) const { 5886 MachineFunction &MF = *MBB->getParent(); 5887 MachineRegisterInfo *MRI = &MF.getRegInfo(); 5888 const SystemZInstrInfo *TII = 5889 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5890 DebugLoc DL = MI->getDebugLoc(); 5891 5892 unsigned SrcReg = MI->getOperand(0).getReg(); 5893 5894 // Create new virtual register of the same class as source. 5895 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg); 5896 unsigned DstReg = MRI->createVirtualRegister(RC); 5897 5898 // Replace pseudo with a normal load-and-test that models the def as 5899 // well. 5900 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg) 5901 .addReg(SrcReg); 5902 MI->eraseFromParent(); 5903 5904 return MBB; 5905 } 5906 5907 MachineBasicBlock *SystemZTargetLowering:: 5908 EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const { 5909 switch (MI->getOpcode()) { 5910 case SystemZ::Select32Mux: 5911 case SystemZ::Select32: 5912 case SystemZ::SelectF32: 5913 case SystemZ::Select64: 5914 case SystemZ::SelectF64: 5915 case SystemZ::SelectF128: 5916 return emitSelect(MI, MBB); 5917 5918 case SystemZ::CondStore8Mux: 5919 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false); 5920 case SystemZ::CondStore8MuxInv: 5921 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true); 5922 case SystemZ::CondStore16Mux: 5923 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false); 5924 case SystemZ::CondStore16MuxInv: 5925 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true); 5926 case SystemZ::CondStore8: 5927 return emitCondStore(MI, MBB, SystemZ::STC, 0, false); 5928 case SystemZ::CondStore8Inv: 5929 return emitCondStore(MI, MBB, SystemZ::STC, 0, true); 5930 case SystemZ::CondStore16: 5931 return emitCondStore(MI, MBB, SystemZ::STH, 0, false); 5932 case SystemZ::CondStore16Inv: 5933 return emitCondStore(MI, MBB, SystemZ::STH, 0, true); 5934 case SystemZ::CondStore32: 5935 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false); 5936 case SystemZ::CondStore32Inv: 5937 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true); 5938 case SystemZ::CondStore64: 5939 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false); 5940 case SystemZ::CondStore64Inv: 5941 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true); 5942 case SystemZ::CondStoreF32: 5943 return emitCondStore(MI, MBB, SystemZ::STE, 0, false); 5944 case SystemZ::CondStoreF32Inv: 5945 return emitCondStore(MI, MBB, SystemZ::STE, 0, true); 5946 case SystemZ::CondStoreF64: 5947 return emitCondStore(MI, MBB, SystemZ::STD, 0, false); 5948 case SystemZ::CondStoreF64Inv: 5949 return emitCondStore(MI, MBB, SystemZ::STD, 0, true); 5950 5951 case SystemZ::AEXT128_64: 5952 return emitExt128(MI, MBB, false, SystemZ::subreg_l64); 5953 case SystemZ::ZEXT128_32: 5954 return emitExt128(MI, MBB, true, SystemZ::subreg_l32); 5955 case SystemZ::ZEXT128_64: 5956 return emitExt128(MI, MBB, true, SystemZ::subreg_l64); 5957 5958 case SystemZ::ATOMIC_SWAPW: 5959 return emitAtomicLoadBinary(MI, MBB, 0, 0); 5960 case SystemZ::ATOMIC_SWAP_32: 5961 return emitAtomicLoadBinary(MI, MBB, 0, 32); 5962 case SystemZ::ATOMIC_SWAP_64: 5963 return emitAtomicLoadBinary(MI, MBB, 0, 64); 5964 5965 case SystemZ::ATOMIC_LOADW_AR: 5966 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0); 5967 case SystemZ::ATOMIC_LOADW_AFI: 5968 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0); 5969 case SystemZ::ATOMIC_LOAD_AR: 5970 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32); 5971 case SystemZ::ATOMIC_LOAD_AHI: 5972 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32); 5973 case SystemZ::ATOMIC_LOAD_AFI: 5974 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32); 5975 case SystemZ::ATOMIC_LOAD_AGR: 5976 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64); 5977 case SystemZ::ATOMIC_LOAD_AGHI: 5978 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64); 5979 case SystemZ::ATOMIC_LOAD_AGFI: 5980 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64); 5981 5982 case SystemZ::ATOMIC_LOADW_SR: 5983 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0); 5984 case SystemZ::ATOMIC_LOAD_SR: 5985 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32); 5986 case SystemZ::ATOMIC_LOAD_SGR: 5987 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64); 5988 5989 case SystemZ::ATOMIC_LOADW_NR: 5990 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0); 5991 case SystemZ::ATOMIC_LOADW_NILH: 5992 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0); 5993 case SystemZ::ATOMIC_LOAD_NR: 5994 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32); 5995 case SystemZ::ATOMIC_LOAD_NILL: 5996 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32); 5997 case SystemZ::ATOMIC_LOAD_NILH: 5998 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32); 5999 case SystemZ::ATOMIC_LOAD_NILF: 6000 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32); 6001 case SystemZ::ATOMIC_LOAD_NGR: 6002 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64); 6003 case SystemZ::ATOMIC_LOAD_NILL64: 6004 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64); 6005 case SystemZ::ATOMIC_LOAD_NILH64: 6006 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64); 6007 case SystemZ::ATOMIC_LOAD_NIHL64: 6008 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64); 6009 case SystemZ::ATOMIC_LOAD_NIHH64: 6010 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64); 6011 case SystemZ::ATOMIC_LOAD_NILF64: 6012 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64); 6013 case SystemZ::ATOMIC_LOAD_NIHF64: 6014 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64); 6015 6016 case SystemZ::ATOMIC_LOADW_OR: 6017 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0); 6018 case SystemZ::ATOMIC_LOADW_OILH: 6019 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0); 6020 case SystemZ::ATOMIC_LOAD_OR: 6021 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32); 6022 case SystemZ::ATOMIC_LOAD_OILL: 6023 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32); 6024 case SystemZ::ATOMIC_LOAD_OILH: 6025 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32); 6026 case SystemZ::ATOMIC_LOAD_OILF: 6027 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32); 6028 case SystemZ::ATOMIC_LOAD_OGR: 6029 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64); 6030 case SystemZ::ATOMIC_LOAD_OILL64: 6031 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64); 6032 case SystemZ::ATOMIC_LOAD_OILH64: 6033 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64); 6034 case SystemZ::ATOMIC_LOAD_OIHL64: 6035 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64); 6036 case SystemZ::ATOMIC_LOAD_OIHH64: 6037 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64); 6038 case SystemZ::ATOMIC_LOAD_OILF64: 6039 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64); 6040 case SystemZ::ATOMIC_LOAD_OIHF64: 6041 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64); 6042 6043 case SystemZ::ATOMIC_LOADW_XR: 6044 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0); 6045 case SystemZ::ATOMIC_LOADW_XILF: 6046 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0); 6047 case SystemZ::ATOMIC_LOAD_XR: 6048 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32); 6049 case SystemZ::ATOMIC_LOAD_XILF: 6050 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32); 6051 case SystemZ::ATOMIC_LOAD_XGR: 6052 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64); 6053 case SystemZ::ATOMIC_LOAD_XILF64: 6054 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64); 6055 case SystemZ::ATOMIC_LOAD_XIHF64: 6056 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64); 6057 6058 case SystemZ::ATOMIC_LOADW_NRi: 6059 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true); 6060 case SystemZ::ATOMIC_LOADW_NILHi: 6061 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true); 6062 case SystemZ::ATOMIC_LOAD_NRi: 6063 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true); 6064 case SystemZ::ATOMIC_LOAD_NILLi: 6065 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true); 6066 case SystemZ::ATOMIC_LOAD_NILHi: 6067 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true); 6068 case SystemZ::ATOMIC_LOAD_NILFi: 6069 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true); 6070 case SystemZ::ATOMIC_LOAD_NGRi: 6071 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true); 6072 case SystemZ::ATOMIC_LOAD_NILL64i: 6073 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true); 6074 case SystemZ::ATOMIC_LOAD_NILH64i: 6075 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true); 6076 case SystemZ::ATOMIC_LOAD_NIHL64i: 6077 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true); 6078 case SystemZ::ATOMIC_LOAD_NIHH64i: 6079 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true); 6080 case SystemZ::ATOMIC_LOAD_NILF64i: 6081 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true); 6082 case SystemZ::ATOMIC_LOAD_NIHF64i: 6083 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true); 6084 6085 case SystemZ::ATOMIC_LOADW_MIN: 6086 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 6087 SystemZ::CCMASK_CMP_LE, 0); 6088 case SystemZ::ATOMIC_LOAD_MIN_32: 6089 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 6090 SystemZ::CCMASK_CMP_LE, 32); 6091 case SystemZ::ATOMIC_LOAD_MIN_64: 6092 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, 6093 SystemZ::CCMASK_CMP_LE, 64); 6094 6095 case SystemZ::ATOMIC_LOADW_MAX: 6096 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 6097 SystemZ::CCMASK_CMP_GE, 0); 6098 case SystemZ::ATOMIC_LOAD_MAX_32: 6099 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 6100 SystemZ::CCMASK_CMP_GE, 32); 6101 case SystemZ::ATOMIC_LOAD_MAX_64: 6102 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, 6103 SystemZ::CCMASK_CMP_GE, 64); 6104 6105 case SystemZ::ATOMIC_LOADW_UMIN: 6106 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 6107 SystemZ::CCMASK_CMP_LE, 0); 6108 case SystemZ::ATOMIC_LOAD_UMIN_32: 6109 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 6110 SystemZ::CCMASK_CMP_LE, 32); 6111 case SystemZ::ATOMIC_LOAD_UMIN_64: 6112 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, 6113 SystemZ::CCMASK_CMP_LE, 64); 6114 6115 case SystemZ::ATOMIC_LOADW_UMAX: 6116 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 6117 SystemZ::CCMASK_CMP_GE, 0); 6118 case SystemZ::ATOMIC_LOAD_UMAX_32: 6119 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 6120 SystemZ::CCMASK_CMP_GE, 32); 6121 case SystemZ::ATOMIC_LOAD_UMAX_64: 6122 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, 6123 SystemZ::CCMASK_CMP_GE, 64); 6124 6125 case SystemZ::ATOMIC_CMP_SWAPW: 6126 return emitAtomicCmpSwapW(MI, MBB); 6127 case SystemZ::MVCSequence: 6128 case SystemZ::MVCLoop: 6129 return emitMemMemWrapper(MI, MBB, SystemZ::MVC); 6130 case SystemZ::NCSequence: 6131 case SystemZ::NCLoop: 6132 return emitMemMemWrapper(MI, MBB, SystemZ::NC); 6133 case SystemZ::OCSequence: 6134 case SystemZ::OCLoop: 6135 return emitMemMemWrapper(MI, MBB, SystemZ::OC); 6136 case SystemZ::XCSequence: 6137 case SystemZ::XCLoop: 6138 return emitMemMemWrapper(MI, MBB, SystemZ::XC); 6139 case SystemZ::CLCSequence: 6140 case SystemZ::CLCLoop: 6141 return emitMemMemWrapper(MI, MBB, SystemZ::CLC); 6142 case SystemZ::CLSTLoop: 6143 return emitStringWrapper(MI, MBB, SystemZ::CLST); 6144 case SystemZ::MVSTLoop: 6145 return emitStringWrapper(MI, MBB, SystemZ::MVST); 6146 case SystemZ::SRSTLoop: 6147 return emitStringWrapper(MI, MBB, SystemZ::SRST); 6148 case SystemZ::TBEGIN: 6149 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false); 6150 case SystemZ::TBEGIN_nofloat: 6151 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true); 6152 case SystemZ::TBEGINC: 6153 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true); 6154 case SystemZ::LTEBRCompare_VecPseudo: 6155 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR); 6156 case SystemZ::LTDBRCompare_VecPseudo: 6157 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR); 6158 case SystemZ::LTXBRCompare_VecPseudo: 6159 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR); 6160 6161 default: 6162 llvm_unreachable("Unexpected instr type to insert"); 6163 } 6164 } 6165