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