1 //===-- SystemZISelLowering.cpp - SystemZ DAG lowering implementation -----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the SystemZTargetLowering class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "SystemZISelLowering.h" 15 #include "SystemZCallingConv.h" 16 #include "SystemZConstantPoolValue.h" 17 #include "SystemZMachineFunctionInfo.h" 18 #include "SystemZTargetMachine.h" 19 #include "llvm/CodeGen/CallingConvLower.h" 20 #include "llvm/CodeGen/MachineInstrBuilder.h" 21 #include "llvm/CodeGen/MachineRegisterInfo.h" 22 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 23 #include "llvm/IR/Intrinsics.h" 24 #include <cctype> 25 26 using namespace llvm; 27 28 #define DEBUG_TYPE "systemz-lower" 29 30 namespace { 31 // Represents a sequence for extracting a 0/1 value from an IPM result: 32 // (((X ^ XORValue) + AddValue) >> Bit) 33 struct IPMConversion { 34 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit) 35 : XORValue(xorValue), AddValue(addValue), Bit(bit) {} 36 37 int64_t XORValue; 38 int64_t AddValue; 39 unsigned Bit; 40 }; 41 42 // Represents information about a comparison. 43 struct Comparison { 44 Comparison(SDValue Op0In, SDValue Op1In) 45 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {} 46 47 // The operands to the comparison. 48 SDValue Op0, Op1; 49 50 // The opcode that should be used to compare Op0 and Op1. 51 unsigned Opcode; 52 53 // A SystemZICMP value. Only used for integer comparisons. 54 unsigned ICmpType; 55 56 // The mask of CC values that Opcode can produce. 57 unsigned CCValid; 58 59 // The mask of CC values for which the original condition is true. 60 unsigned CCMask; 61 }; 62 } // end anonymous namespace 63 64 // Classify VT as either 32 or 64 bit. 65 static bool is32Bit(EVT VT) { 66 switch (VT.getSimpleVT().SimpleTy) { 67 case MVT::i32: 68 return true; 69 case MVT::i64: 70 return false; 71 default: 72 llvm_unreachable("Unsupported type"); 73 } 74 } 75 76 // Return a version of MachineOperand that can be safely used before the 77 // final use. 78 static MachineOperand earlyUseOperand(MachineOperand Op) { 79 if (Op.isReg()) 80 Op.setIsKill(false); 81 return Op; 82 } 83 84 SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM, 85 const SystemZSubtarget &STI) 86 : TargetLowering(TM), Subtarget(STI) { 87 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize()); 88 89 // Set up the register classes. 90 if (Subtarget.hasHighWord()) 91 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass); 92 else 93 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass); 94 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass); 95 if (Subtarget.hasVector()) { 96 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass); 97 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass); 98 } else { 99 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass); 100 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass); 101 } 102 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass); 103 104 if (Subtarget.hasVector()) { 105 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass); 106 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass); 107 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass); 108 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass); 109 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass); 110 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass); 111 } 112 113 // Compute derived properties from the register classes 114 computeRegisterProperties(Subtarget.getRegisterInfo()); 115 116 // Set up special registers. 117 setStackPointerRegisterToSaveRestore(SystemZ::R15D); 118 119 // TODO: It may be better to default to latency-oriented scheduling, however 120 // LLVM's current latency-oriented scheduler can't handle physreg definitions 121 // such as SystemZ has with CC, so set this to the register-pressure 122 // scheduler, because it can. 123 setSchedulingPreference(Sched::RegPressure); 124 125 setBooleanContents(ZeroOrOneBooleanContent); 126 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 127 128 // Instructions are strings of 2-byte aligned 2-byte values. 129 setMinFunctionAlignment(2); 130 131 // Handle operations that are handled in a similar way for all types. 132 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE; 133 I <= MVT::LAST_FP_VALUETYPE; 134 ++I) { 135 MVT VT = MVT::SimpleValueType(I); 136 if (isTypeLegal(VT)) { 137 // Lower SET_CC into an IPM-based sequence. 138 setOperationAction(ISD::SETCC, VT, Custom); 139 140 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE). 141 setOperationAction(ISD::SELECT, VT, Expand); 142 143 // Lower SELECT_CC and BR_CC into separate comparisons and branches. 144 setOperationAction(ISD::SELECT_CC, VT, Custom); 145 setOperationAction(ISD::BR_CC, VT, Custom); 146 } 147 } 148 149 // Expand jump table branches as address arithmetic followed by an 150 // indirect jump. 151 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 152 153 // Expand BRCOND into a BR_CC (see above). 154 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 155 156 // Handle integer types. 157 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE; 158 I <= MVT::LAST_INTEGER_VALUETYPE; 159 ++I) { 160 MVT VT = MVT::SimpleValueType(I); 161 if (isTypeLegal(VT)) { 162 // Expand individual DIV and REMs into DIVREMs. 163 setOperationAction(ISD::SDIV, VT, Expand); 164 setOperationAction(ISD::UDIV, VT, Expand); 165 setOperationAction(ISD::SREM, VT, Expand); 166 setOperationAction(ISD::UREM, VT, Expand); 167 setOperationAction(ISD::SDIVREM, VT, Custom); 168 setOperationAction(ISD::UDIVREM, VT, Custom); 169 170 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and 171 // stores, putting a serialization instruction after the stores. 172 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom); 173 setOperationAction(ISD::ATOMIC_STORE, VT, Custom); 174 175 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are 176 // available, or if the operand is constant. 177 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom); 178 179 // Use POPCNT on z196 and above. 180 if (Subtarget.hasPopulationCount()) 181 setOperationAction(ISD::CTPOP, VT, Custom); 182 else 183 setOperationAction(ISD::CTPOP, VT, Expand); 184 185 // No special instructions for these. 186 setOperationAction(ISD::CTTZ, VT, Expand); 187 setOperationAction(ISD::ROTR, VT, Expand); 188 189 // Use *MUL_LOHI where possible instead of MULH*. 190 setOperationAction(ISD::MULHS, VT, Expand); 191 setOperationAction(ISD::MULHU, VT, Expand); 192 setOperationAction(ISD::SMUL_LOHI, VT, Custom); 193 setOperationAction(ISD::UMUL_LOHI, VT, Custom); 194 195 // Only z196 and above have native support for conversions to unsigned. 196 if (!Subtarget.hasFPExtension()) 197 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 198 } 199 } 200 201 // Type legalization will convert 8- and 16-bit atomic operations into 202 // forms that operate on i32s (but still keeping the original memory VT). 203 // Lower them into full i32 operations. 204 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom); 205 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom); 206 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom); 207 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom); 208 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom); 209 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom); 210 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom); 211 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom); 212 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom); 213 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom); 214 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom); 215 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); 216 217 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom); 218 219 // z10 has instructions for signed but not unsigned FP conversion. 220 // Handle unsigned 32-bit types as signed 64-bit types. 221 if (!Subtarget.hasFPExtension()) { 222 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote); 223 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); 224 } 225 226 // We have native support for a 64-bit CTLZ, via FLOGR. 227 setOperationAction(ISD::CTLZ, MVT::i32, Promote); 228 setOperationAction(ISD::CTLZ, MVT::i64, Legal); 229 230 // Give LowerOperation the chance to replace 64-bit ORs with subregs. 231 setOperationAction(ISD::OR, MVT::i64, Custom); 232 233 // FIXME: Can we support these natively? 234 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand); 235 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand); 236 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand); 237 238 // We have native instructions for i8, i16 and i32 extensions, but not i1. 239 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 240 for (MVT VT : MVT::integer_valuetypes()) { 241 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 242 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); 243 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote); 244 } 245 246 // Handle the various types of symbolic address. 247 setOperationAction(ISD::ConstantPool, PtrVT, Custom); 248 setOperationAction(ISD::GlobalAddress, PtrVT, Custom); 249 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom); 250 setOperationAction(ISD::BlockAddress, PtrVT, Custom); 251 setOperationAction(ISD::JumpTable, PtrVT, Custom); 252 253 // We need to handle dynamic allocations specially because of the 254 // 160-byte area at the bottom of the stack. 255 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom); 256 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, PtrVT, Custom); 257 258 // Use custom expanders so that we can force the function to use 259 // a frame pointer. 260 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom); 261 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom); 262 263 // Handle prefetches with PFD or PFDRL. 264 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 265 266 for (MVT VT : MVT::vector_valuetypes()) { 267 // Assume by default that all vector operations need to be expanded. 268 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode) 269 if (getOperationAction(Opcode, VT) == Legal) 270 setOperationAction(Opcode, VT, Expand); 271 272 // Likewise all truncating stores and extending loads. 273 for (MVT InnerVT : MVT::vector_valuetypes()) { 274 setTruncStoreAction(VT, InnerVT, Expand); 275 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand); 276 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand); 277 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand); 278 } 279 280 if (isTypeLegal(VT)) { 281 // These operations are legal for anything that can be stored in a 282 // vector register, even if there is no native support for the format 283 // as such. In particular, we can do these for v4f32 even though there 284 // are no specific instructions for that format. 285 setOperationAction(ISD::LOAD, VT, Legal); 286 setOperationAction(ISD::STORE, VT, Legal); 287 setOperationAction(ISD::VSELECT, VT, Legal); 288 setOperationAction(ISD::BITCAST, VT, Legal); 289 setOperationAction(ISD::UNDEF, VT, Legal); 290 291 // Likewise, except that we need to replace the nodes with something 292 // more specific. 293 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 294 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 295 } 296 } 297 298 // Handle integer vector types. 299 for (MVT VT : MVT::integer_vector_valuetypes()) { 300 if (isTypeLegal(VT)) { 301 // These operations have direct equivalents. 302 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal); 303 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal); 304 setOperationAction(ISD::ADD, VT, Legal); 305 setOperationAction(ISD::SUB, VT, Legal); 306 if (VT != MVT::v2i64) 307 setOperationAction(ISD::MUL, VT, Legal); 308 setOperationAction(ISD::AND, VT, Legal); 309 setOperationAction(ISD::OR, VT, Legal); 310 setOperationAction(ISD::XOR, VT, Legal); 311 setOperationAction(ISD::CTPOP, VT, Custom); 312 setOperationAction(ISD::CTTZ, VT, Legal); 313 setOperationAction(ISD::CTLZ, VT, Legal); 314 315 // Convert a GPR scalar to a vector by inserting it into element 0. 316 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom); 317 318 // Use a series of unpacks for extensions. 319 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom); 320 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom); 321 322 // Detect shifts by a scalar amount and convert them into 323 // V*_BY_SCALAR. 324 setOperationAction(ISD::SHL, VT, Custom); 325 setOperationAction(ISD::SRA, VT, Custom); 326 setOperationAction(ISD::SRL, VT, Custom); 327 328 // At present ROTL isn't matched by DAGCombiner. ROTR should be 329 // converted into ROTL. 330 setOperationAction(ISD::ROTL, VT, Expand); 331 setOperationAction(ISD::ROTR, VT, Expand); 332 333 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands 334 // and inverting the result as necessary. 335 setOperationAction(ISD::SETCC, VT, Custom); 336 } 337 } 338 339 if (Subtarget.hasVector()) { 340 // There should be no need to check for float types other than v2f64 341 // since <2 x f32> isn't a legal type. 342 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal); 343 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal); 344 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal); 345 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal); 346 } 347 348 // Handle floating-point types. 349 for (unsigned I = MVT::FIRST_FP_VALUETYPE; 350 I <= MVT::LAST_FP_VALUETYPE; 351 ++I) { 352 MVT VT = MVT::SimpleValueType(I); 353 if (isTypeLegal(VT)) { 354 // We can use FI for FRINT. 355 setOperationAction(ISD::FRINT, VT, Legal); 356 357 // We can use the extended form of FI for other rounding operations. 358 if (Subtarget.hasFPExtension()) { 359 setOperationAction(ISD::FNEARBYINT, VT, Legal); 360 setOperationAction(ISD::FFLOOR, VT, Legal); 361 setOperationAction(ISD::FCEIL, VT, Legal); 362 setOperationAction(ISD::FTRUNC, VT, Legal); 363 setOperationAction(ISD::FROUND, VT, Legal); 364 } 365 366 // No special instructions for these. 367 setOperationAction(ISD::FSIN, VT, Expand); 368 setOperationAction(ISD::FCOS, VT, Expand); 369 setOperationAction(ISD::FSINCOS, VT, Expand); 370 setOperationAction(ISD::FREM, VT, Expand); 371 setOperationAction(ISD::FPOW, VT, Expand); 372 } 373 } 374 375 // Handle floating-point vector types. 376 if (Subtarget.hasVector()) { 377 // Scalar-to-vector conversion is just a subreg. 378 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal); 379 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal); 380 381 // Some insertions and extractions can be done directly but others 382 // need to go via integers. 383 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); 384 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom); 385 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); 386 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); 387 388 // These operations have direct equivalents. 389 setOperationAction(ISD::FADD, MVT::v2f64, Legal); 390 setOperationAction(ISD::FNEG, MVT::v2f64, Legal); 391 setOperationAction(ISD::FSUB, MVT::v2f64, Legal); 392 setOperationAction(ISD::FMUL, MVT::v2f64, Legal); 393 setOperationAction(ISD::FMA, MVT::v2f64, Legal); 394 setOperationAction(ISD::FDIV, MVT::v2f64, Legal); 395 setOperationAction(ISD::FABS, MVT::v2f64, Legal); 396 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); 397 setOperationAction(ISD::FRINT, MVT::v2f64, Legal); 398 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal); 399 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal); 400 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal); 401 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal); 402 setOperationAction(ISD::FROUND, MVT::v2f64, Legal); 403 } 404 405 // We have fused multiply-addition for f32 and f64 but not f128. 406 setOperationAction(ISD::FMA, MVT::f32, Legal); 407 setOperationAction(ISD::FMA, MVT::f64, Legal); 408 setOperationAction(ISD::FMA, MVT::f128, Expand); 409 410 // Needed so that we don't try to implement f128 constant loads using 411 // a load-and-extend of a f80 constant (in cases where the constant 412 // would fit in an f80). 413 for (MVT VT : MVT::fp_valuetypes()) 414 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand); 415 416 // Floating-point truncation and stores need to be done separately. 417 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 418 setTruncStoreAction(MVT::f128, MVT::f32, Expand); 419 setTruncStoreAction(MVT::f128, MVT::f64, Expand); 420 421 // We have 64-bit FPR<->GPR moves, but need special handling for 422 // 32-bit forms. 423 if (!Subtarget.hasVector()) { 424 setOperationAction(ISD::BITCAST, MVT::i32, Custom); 425 setOperationAction(ISD::BITCAST, MVT::f32, Custom); 426 } 427 428 // VASTART and VACOPY need to deal with the SystemZ-specific varargs 429 // structure, but VAEND is a no-op. 430 setOperationAction(ISD::VASTART, MVT::Other, Custom); 431 setOperationAction(ISD::VACOPY, MVT::Other, Custom); 432 setOperationAction(ISD::VAEND, MVT::Other, Expand); 433 434 // Codes for which we want to perform some z-specific combinations. 435 setTargetDAGCombine(ISD::SIGN_EXTEND); 436 setTargetDAGCombine(ISD::STORE); 437 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT); 438 setTargetDAGCombine(ISD::FP_ROUND); 439 440 // Handle intrinsics. 441 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom); 442 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 443 444 // We want to use MVC in preference to even a single load/store pair. 445 MaxStoresPerMemcpy = 0; 446 MaxStoresPerMemcpyOptSize = 0; 447 448 // The main memset sequence is a byte store followed by an MVC. 449 // Two STC or MV..I stores win over that, but the kind of fused stores 450 // generated by target-independent code don't when the byte value is 451 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better 452 // than "STC;MVC". Handle the choice in target-specific code instead. 453 MaxStoresPerMemset = 0; 454 MaxStoresPerMemsetOptSize = 0; 455 } 456 457 EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL, 458 LLVMContext &, EVT VT) const { 459 if (!VT.isVector()) 460 return MVT::i32; 461 return VT.changeVectorElementTypeToInteger(); 462 } 463 464 bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { 465 VT = VT.getScalarType(); 466 467 if (!VT.isSimple()) 468 return false; 469 470 switch (VT.getSimpleVT().SimpleTy) { 471 case MVT::f32: 472 case MVT::f64: 473 return true; 474 case MVT::f128: 475 return false; 476 default: 477 break; 478 } 479 480 return false; 481 } 482 483 bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 484 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR. 485 return Imm.isZero() || Imm.isNegZero(); 486 } 487 488 bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 489 // We can use CGFI or CLGFI. 490 return isInt<32>(Imm) || isUInt<32>(Imm); 491 } 492 493 bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const { 494 // We can use ALGFI or SLGFI. 495 return isUInt<32>(Imm) || isUInt<32>(-Imm); 496 } 497 498 bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 499 unsigned, 500 unsigned, 501 bool *Fast) const { 502 // Unaligned accesses should never be slower than the expanded version. 503 // We check specifically for aligned accesses in the few cases where 504 // they are required. 505 if (Fast) 506 *Fast = true; 507 return true; 508 } 509 510 bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL, 511 const AddrMode &AM, Type *Ty, 512 unsigned AS) const { 513 // Punt on globals for now, although they can be used in limited 514 // RELATIVE LONG cases. 515 if (AM.BaseGV) 516 return false; 517 518 // Require a 20-bit signed offset. 519 if (!isInt<20>(AM.BaseOffs)) 520 return false; 521 522 // Indexing is OK but no scale factor can be applied. 523 return AM.Scale == 0 || AM.Scale == 1; 524 } 525 526 bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const { 527 if (!FromType->isIntegerTy() || !ToType->isIntegerTy()) 528 return false; 529 unsigned FromBits = FromType->getPrimitiveSizeInBits(); 530 unsigned ToBits = ToType->getPrimitiveSizeInBits(); 531 return FromBits > ToBits; 532 } 533 534 bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const { 535 if (!FromVT.isInteger() || !ToVT.isInteger()) 536 return false; 537 unsigned FromBits = FromVT.getSizeInBits(); 538 unsigned ToBits = ToVT.getSizeInBits(); 539 return FromBits > ToBits; 540 } 541 542 //===----------------------------------------------------------------------===// 543 // Inline asm support 544 //===----------------------------------------------------------------------===// 545 546 TargetLowering::ConstraintType 547 SystemZTargetLowering::getConstraintType(StringRef Constraint) const { 548 if (Constraint.size() == 1) { 549 switch (Constraint[0]) { 550 case 'a': // Address register 551 case 'd': // Data register (equivalent to 'r') 552 case 'f': // Floating-point register 553 case 'h': // High-part register 554 case 'r': // General-purpose register 555 return C_RegisterClass; 556 557 case 'Q': // Memory with base and unsigned 12-bit displacement 558 case 'R': // Likewise, plus an index 559 case 'S': // Memory with base and signed 20-bit displacement 560 case 'T': // Likewise, plus an index 561 case 'm': // Equivalent to 'T'. 562 return C_Memory; 563 564 case 'I': // Unsigned 8-bit constant 565 case 'J': // Unsigned 12-bit constant 566 case 'K': // Signed 16-bit constant 567 case 'L': // Signed 20-bit displacement (on all targets we support) 568 case 'M': // 0x7fffffff 569 return C_Other; 570 571 default: 572 break; 573 } 574 } 575 return TargetLowering::getConstraintType(Constraint); 576 } 577 578 TargetLowering::ConstraintWeight SystemZTargetLowering:: 579 getSingleConstraintMatchWeight(AsmOperandInfo &info, 580 const char *constraint) const { 581 ConstraintWeight weight = CW_Invalid; 582 Value *CallOperandVal = info.CallOperandVal; 583 // If we don't have a value, we can't do a match, 584 // but allow it at the lowest weight. 585 if (!CallOperandVal) 586 return CW_Default; 587 Type *type = CallOperandVal->getType(); 588 // Look at the constraint type. 589 switch (*constraint) { 590 default: 591 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 592 break; 593 594 case 'a': // Address register 595 case 'd': // Data register (equivalent to 'r') 596 case 'h': // High-part register 597 case 'r': // General-purpose register 598 if (CallOperandVal->getType()->isIntegerTy()) 599 weight = CW_Register; 600 break; 601 602 case 'f': // Floating-point register 603 if (type->isFloatingPointTy()) 604 weight = CW_Register; 605 break; 606 607 case 'I': // Unsigned 8-bit constant 608 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) 609 if (isUInt<8>(C->getZExtValue())) 610 weight = CW_Constant; 611 break; 612 613 case 'J': // Unsigned 12-bit constant 614 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) 615 if (isUInt<12>(C->getZExtValue())) 616 weight = CW_Constant; 617 break; 618 619 case 'K': // Signed 16-bit constant 620 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) 621 if (isInt<16>(C->getSExtValue())) 622 weight = CW_Constant; 623 break; 624 625 case 'L': // Signed 20-bit displacement (on all targets we support) 626 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) 627 if (isInt<20>(C->getSExtValue())) 628 weight = CW_Constant; 629 break; 630 631 case 'M': // 0x7fffffff 632 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) 633 if (C->getZExtValue() == 0x7fffffff) 634 weight = CW_Constant; 635 break; 636 } 637 return weight; 638 } 639 640 // Parse a "{tNNN}" register constraint for which the register type "t" 641 // has already been verified. MC is the class associated with "t" and 642 // Map maps 0-based register numbers to LLVM register numbers. 643 static std::pair<unsigned, const TargetRegisterClass *> 644 parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC, 645 const unsigned *Map) { 646 assert(*(Constraint.end()-1) == '}' && "Missing '}'"); 647 if (isdigit(Constraint[2])) { 648 unsigned Index; 649 bool Failed = 650 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index); 651 if (!Failed && Index < 16 && Map[Index]) 652 return std::make_pair(Map[Index], RC); 653 } 654 return std::make_pair(0U, nullptr); 655 } 656 657 std::pair<unsigned, const TargetRegisterClass *> 658 SystemZTargetLowering::getRegForInlineAsmConstraint( 659 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { 660 if (Constraint.size() == 1) { 661 // GCC Constraint Letters 662 switch (Constraint[0]) { 663 default: break; 664 case 'd': // Data register (equivalent to 'r') 665 case 'r': // General-purpose register 666 if (VT == MVT::i64) 667 return std::make_pair(0U, &SystemZ::GR64BitRegClass); 668 else if (VT == MVT::i128) 669 return std::make_pair(0U, &SystemZ::GR128BitRegClass); 670 return std::make_pair(0U, &SystemZ::GR32BitRegClass); 671 672 case 'a': // Address register 673 if (VT == MVT::i64) 674 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass); 675 else if (VT == MVT::i128) 676 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass); 677 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass); 678 679 case 'h': // High-part register (an LLVM extension) 680 return std::make_pair(0U, &SystemZ::GRH32BitRegClass); 681 682 case 'f': // Floating-point register 683 if (VT == MVT::f64) 684 return std::make_pair(0U, &SystemZ::FP64BitRegClass); 685 else if (VT == MVT::f128) 686 return std::make_pair(0U, &SystemZ::FP128BitRegClass); 687 return std::make_pair(0U, &SystemZ::FP32BitRegClass); 688 } 689 } 690 if (Constraint.size() > 0 && Constraint[0] == '{') { 691 // We need to override the default register parsing for GPRs and FPRs 692 // because the interpretation depends on VT. The internal names of 693 // the registers are also different from the external names 694 // (F0D and F0S instead of F0, etc.). 695 if (Constraint[1] == 'r') { 696 if (VT == MVT::i32) 697 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass, 698 SystemZMC::GR32Regs); 699 if (VT == MVT::i128) 700 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass, 701 SystemZMC::GR128Regs); 702 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass, 703 SystemZMC::GR64Regs); 704 } 705 if (Constraint[1] == 'f') { 706 if (VT == MVT::f32) 707 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass, 708 SystemZMC::FP32Regs); 709 if (VT == MVT::f128) 710 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass, 711 SystemZMC::FP128Regs); 712 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass, 713 SystemZMC::FP64Regs); 714 } 715 } 716 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 717 } 718 719 void SystemZTargetLowering:: 720 LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint, 721 std::vector<SDValue> &Ops, 722 SelectionDAG &DAG) const { 723 // Only support length 1 constraints for now. 724 if (Constraint.length() == 1) { 725 switch (Constraint[0]) { 726 case 'I': // Unsigned 8-bit constant 727 if (auto *C = dyn_cast<ConstantSDNode>(Op)) 728 if (isUInt<8>(C->getZExtValue())) 729 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), 730 Op.getValueType())); 731 return; 732 733 case 'J': // Unsigned 12-bit constant 734 if (auto *C = dyn_cast<ConstantSDNode>(Op)) 735 if (isUInt<12>(C->getZExtValue())) 736 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), 737 Op.getValueType())); 738 return; 739 740 case 'K': // Signed 16-bit constant 741 if (auto *C = dyn_cast<ConstantSDNode>(Op)) 742 if (isInt<16>(C->getSExtValue())) 743 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op), 744 Op.getValueType())); 745 return; 746 747 case 'L': // Signed 20-bit displacement (on all targets we support) 748 if (auto *C = dyn_cast<ConstantSDNode>(Op)) 749 if (isInt<20>(C->getSExtValue())) 750 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op), 751 Op.getValueType())); 752 return; 753 754 case 'M': // 0x7fffffff 755 if (auto *C = dyn_cast<ConstantSDNode>(Op)) 756 if (C->getZExtValue() == 0x7fffffff) 757 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), 758 Op.getValueType())); 759 return; 760 } 761 } 762 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 763 } 764 765 //===----------------------------------------------------------------------===// 766 // Calling conventions 767 //===----------------------------------------------------------------------===// 768 769 #include "SystemZGenCallingConv.inc" 770 771 bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType, 772 Type *ToType) const { 773 return isTruncateFree(FromType, ToType); 774 } 775 776 bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { 777 return CI->isTailCall(); 778 } 779 780 // We do not yet support 128-bit single-element vector types. If the user 781 // attempts to use such types as function argument or return type, prefer 782 // to error out instead of emitting code violating the ABI. 783 static void VerifyVectorType(MVT VT, EVT ArgVT) { 784 if (ArgVT.isVector() && !VT.isVector()) 785 report_fatal_error("Unsupported vector argument or return type"); 786 } 787 788 static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) { 789 for (unsigned i = 0; i < Ins.size(); ++i) 790 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT); 791 } 792 793 static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) { 794 for (unsigned i = 0; i < Outs.size(); ++i) 795 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT); 796 } 797 798 // Value is a value that has been passed to us in the location described by VA 799 // (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining 800 // any loads onto Chain. 801 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL, 802 CCValAssign &VA, SDValue Chain, 803 SDValue Value) { 804 // If the argument has been promoted from a smaller type, insert an 805 // assertion to capture this. 806 if (VA.getLocInfo() == CCValAssign::SExt) 807 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value, 808 DAG.getValueType(VA.getValVT())); 809 else if (VA.getLocInfo() == CCValAssign::ZExt) 810 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value, 811 DAG.getValueType(VA.getValVT())); 812 813 if (VA.isExtInLoc()) 814 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value); 815 else if (VA.getLocInfo() == CCValAssign::BCvt) { 816 // If this is a short vector argument loaded from the stack, 817 // extend from i64 to full vector size and then bitcast. 818 assert(VA.getLocVT() == MVT::i64); 819 assert(VA.getValVT().isVector()); 820 Value = DAG.getBuildVector(MVT::v2i64, DL, {Value, DAG.getUNDEF(MVT::i64)}); 821 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value); 822 } else 823 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo"); 824 return Value; 825 } 826 827 // Value is a value of type VA.getValVT() that we need to copy into 828 // the location described by VA. Return a copy of Value converted to 829 // VA.getValVT(). The caller is responsible for handling indirect values. 830 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL, 831 CCValAssign &VA, SDValue Value) { 832 switch (VA.getLocInfo()) { 833 case CCValAssign::SExt: 834 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value); 835 case CCValAssign::ZExt: 836 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value); 837 case CCValAssign::AExt: 838 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value); 839 case CCValAssign::BCvt: 840 // If this is a short vector argument to be stored to the stack, 841 // bitcast to v2i64 and then extract first element. 842 assert(VA.getLocVT() == MVT::i64); 843 assert(VA.getValVT().isVector()); 844 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value); 845 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value, 846 DAG.getConstant(0, DL, MVT::i32)); 847 case CCValAssign::Full: 848 return Value; 849 default: 850 llvm_unreachable("Unhandled getLocInfo()"); 851 } 852 } 853 854 SDValue SystemZTargetLowering:: 855 LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 856 const SmallVectorImpl<ISD::InputArg> &Ins, 857 SDLoc DL, SelectionDAG &DAG, 858 SmallVectorImpl<SDValue> &InVals) const { 859 MachineFunction &MF = DAG.getMachineFunction(); 860 MachineFrameInfo *MFI = MF.getFrameInfo(); 861 MachineRegisterInfo &MRI = MF.getRegInfo(); 862 SystemZMachineFunctionInfo *FuncInfo = 863 MF.getInfo<SystemZMachineFunctionInfo>(); 864 auto *TFL = 865 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering()); 866 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 867 868 // Detect unsupported vector argument types. 869 if (Subtarget.hasVector()) 870 VerifyVectorTypes(Ins); 871 872 // Assign locations to all of the incoming arguments. 873 SmallVector<CCValAssign, 16> ArgLocs; 874 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); 875 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ); 876 877 unsigned NumFixedGPRs = 0; 878 unsigned NumFixedFPRs = 0; 879 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { 880 SDValue ArgValue; 881 CCValAssign &VA = ArgLocs[I]; 882 EVT LocVT = VA.getLocVT(); 883 if (VA.isRegLoc()) { 884 // Arguments passed in registers 885 const TargetRegisterClass *RC; 886 switch (LocVT.getSimpleVT().SimpleTy) { 887 default: 888 // Integers smaller than i64 should be promoted to i64. 889 llvm_unreachable("Unexpected argument type"); 890 case MVT::i32: 891 NumFixedGPRs += 1; 892 RC = &SystemZ::GR32BitRegClass; 893 break; 894 case MVT::i64: 895 NumFixedGPRs += 1; 896 RC = &SystemZ::GR64BitRegClass; 897 break; 898 case MVT::f32: 899 NumFixedFPRs += 1; 900 RC = &SystemZ::FP32BitRegClass; 901 break; 902 case MVT::f64: 903 NumFixedFPRs += 1; 904 RC = &SystemZ::FP64BitRegClass; 905 break; 906 case MVT::v16i8: 907 case MVT::v8i16: 908 case MVT::v4i32: 909 case MVT::v2i64: 910 case MVT::v4f32: 911 case MVT::v2f64: 912 RC = &SystemZ::VR128BitRegClass; 913 break; 914 } 915 916 unsigned VReg = MRI.createVirtualRegister(RC); 917 MRI.addLiveIn(VA.getLocReg(), VReg); 918 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT); 919 } else { 920 assert(VA.isMemLoc() && "Argument not register or memory"); 921 922 // Create the frame index object for this incoming parameter. 923 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8, 924 VA.getLocMemOffset(), true); 925 926 // Create the SelectionDAG nodes corresponding to a load 927 // from this parameter. Unpromoted ints and floats are 928 // passed as right-justified 8-byte values. 929 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 930 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32) 931 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, 932 DAG.getIntPtrConstant(4, DL)); 933 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN, 934 MachinePointerInfo::getFixedStack(MF, FI), false, 935 false, false, 0); 936 } 937 938 // Convert the value of the argument register into the value that's 939 // being passed. 940 if (VA.getLocInfo() == CCValAssign::Indirect) { 941 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, 942 ArgValue, MachinePointerInfo(), 943 false, false, false, 0)); 944 // If the original argument was split (e.g. i128), we need 945 // to load all parts of it here (using the same address). 946 unsigned ArgIndex = Ins[I].OrigArgIndex; 947 assert (Ins[I].PartOffset == 0); 948 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) { 949 CCValAssign &PartVA = ArgLocs[I + 1]; 950 unsigned PartOffset = Ins[I + 1].PartOffset; 951 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue, 952 DAG.getIntPtrConstant(PartOffset, DL)); 953 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, 954 Address, MachinePointerInfo(), 955 false, false, false, 0)); 956 ++I; 957 } 958 } else 959 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue)); 960 } 961 962 if (IsVarArg) { 963 // Save the number of non-varargs registers for later use by va_start, etc. 964 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs); 965 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs); 966 967 // Likewise the address (in the form of a frame index) of where the 968 // first stack vararg would be. The 1-byte size here is arbitrary. 969 int64_t StackSize = CCInfo.getNextStackOffset(); 970 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true)); 971 972 // ...and a similar frame index for the caller-allocated save area 973 // that will be used to store the incoming registers. 974 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea(); 975 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true); 976 FuncInfo->setRegSaveFrameIndex(RegSaveIndex); 977 978 // Store the FPR varargs in the reserved frame slots. (We store the 979 // GPRs as part of the prologue.) 980 if (NumFixedFPRs < SystemZ::NumArgFPRs) { 981 SDValue MemOps[SystemZ::NumArgFPRs]; 982 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) { 983 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]); 984 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true); 985 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 986 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I], 987 &SystemZ::FP64BitRegClass); 988 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64); 989 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN, 990 MachinePointerInfo::getFixedStack(MF, FI), 991 false, false, 0); 992 } 993 // Join the stores, which are independent of one another. 994 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, 995 makeArrayRef(&MemOps[NumFixedFPRs], 996 SystemZ::NumArgFPRs-NumFixedFPRs)); 997 } 998 } 999 1000 return Chain; 1001 } 1002 1003 static bool canUseSiblingCall(const CCState &ArgCCInfo, 1004 SmallVectorImpl<CCValAssign> &ArgLocs, 1005 SmallVectorImpl<ISD::OutputArg> &Outs) { 1006 // Punt if there are any indirect or stack arguments, or if the call 1007 // needs the callee-saved argument register R6, or if the call uses 1008 // the callee-saved register arguments SwiftSelf and SwiftError. 1009 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { 1010 CCValAssign &VA = ArgLocs[I]; 1011 if (VA.getLocInfo() == CCValAssign::Indirect) 1012 return false; 1013 if (!VA.isRegLoc()) 1014 return false; 1015 unsigned Reg = VA.getLocReg(); 1016 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D) 1017 return false; 1018 if (Outs[I].Flags.isSwiftSelf() || Outs[I].Flags.isSwiftError()) 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, Outs)) 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 MachineFunction &MF = DAG.getMachineFunction(); 2840 bool RealignOpt = !MF.getFunction()-> hasFnAttribute("no-realign-stack"); 2841 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain"); 2842 2843 SDValue Chain = Op.getOperand(0); 2844 SDValue Size = Op.getOperand(1); 2845 SDValue Align = Op.getOperand(2); 2846 SDLoc DL(Op); 2847 2848 // If user has set the no alignment function attribute, ignore 2849 // alloca alignments. 2850 uint64_t AlignVal = (RealignOpt ? 2851 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0); 2852 2853 uint64_t StackAlign = TFI->getStackAlignment(); 2854 uint64_t RequiredAlign = std::max(AlignVal, StackAlign); 2855 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign; 2856 2857 unsigned SPReg = getStackPointerRegisterToSaveRestore(); 2858 SDValue NeededSpace = Size; 2859 2860 // Get a reference to the stack pointer. 2861 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64); 2862 2863 // If we need a backchain, save it now. 2864 SDValue Backchain; 2865 if (StoreBackchain) 2866 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo(), 2867 false, false, false, 0); 2868 2869 // Add extra space for alignment if needed. 2870 if (ExtraAlignSpace) 2871 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace, 2872 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64)); 2873 2874 // Get the new stack pointer value. 2875 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace); 2876 2877 // Copy the new stack pointer back. 2878 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP); 2879 2880 // The allocated data lives above the 160 bytes allocated for the standard 2881 // frame, plus any outgoing stack arguments. We don't know how much that 2882 // amounts to yet, so emit a special ADJDYNALLOC placeholder. 2883 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64); 2884 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust); 2885 2886 // Dynamically realign if needed. 2887 if (RequiredAlign > StackAlign) { 2888 Result = 2889 DAG.getNode(ISD::ADD, DL, MVT::i64, Result, 2890 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64)); 2891 Result = 2892 DAG.getNode(ISD::AND, DL, MVT::i64, Result, 2893 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64)); 2894 } 2895 2896 if (StoreBackchain) 2897 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo(), 2898 false, false, 0); 2899 2900 SDValue Ops[2] = { Result, Chain }; 2901 return DAG.getMergeValues(Ops, DL); 2902 } 2903 2904 SDValue SystemZTargetLowering::lowerGET_DYNAMIC_AREA_OFFSET( 2905 SDValue Op, SelectionDAG &DAG) const { 2906 SDLoc DL(Op); 2907 2908 return DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64); 2909 } 2910 2911 SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op, 2912 SelectionDAG &DAG) const { 2913 EVT VT = Op.getValueType(); 2914 SDLoc DL(Op); 2915 SDValue Ops[2]; 2916 if (is32Bit(VT)) 2917 // Just do a normal 64-bit multiplication and extract the results. 2918 // We define this so that it can be used for constant division. 2919 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0), 2920 Op.getOperand(1), Ops[1], Ops[0]); 2921 else { 2922 // Do a full 128-bit multiplication based on UMUL_LOHI64: 2923 // 2924 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64) 2925 // 2926 // but using the fact that the upper halves are either all zeros 2927 // or all ones: 2928 // 2929 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64) 2930 // 2931 // and grouping the right terms together since they are quicker than the 2932 // multiplication: 2933 // 2934 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64) 2935 SDValue C63 = DAG.getConstant(63, DL, MVT::i64); 2936 SDValue LL = Op.getOperand(0); 2937 SDValue RL = Op.getOperand(1); 2938 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63); 2939 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63); 2940 // UMUL_LOHI64 returns the low result in the odd register and the high 2941 // result in the even register. SMUL_LOHI is defined to return the 2942 // low half first, so the results are in reverse order. 2943 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64, 2944 LL, RL, Ops[1], Ops[0]); 2945 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH); 2946 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL); 2947 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL); 2948 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum); 2949 } 2950 return DAG.getMergeValues(Ops, DL); 2951 } 2952 2953 SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op, 2954 SelectionDAG &DAG) const { 2955 EVT VT = Op.getValueType(); 2956 SDLoc DL(Op); 2957 SDValue Ops[2]; 2958 if (is32Bit(VT)) 2959 // Just do a normal 64-bit multiplication and extract the results. 2960 // We define this so that it can be used for constant division. 2961 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0), 2962 Op.getOperand(1), Ops[1], Ops[0]); 2963 else 2964 // UMUL_LOHI64 returns the low result in the odd register and the high 2965 // result in the even register. UMUL_LOHI is defined to return the 2966 // low half first, so the results are in reverse order. 2967 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64, 2968 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); 2969 return DAG.getMergeValues(Ops, DL); 2970 } 2971 2972 SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op, 2973 SelectionDAG &DAG) const { 2974 SDValue Op0 = Op.getOperand(0); 2975 SDValue Op1 = Op.getOperand(1); 2976 EVT VT = Op.getValueType(); 2977 SDLoc DL(Op); 2978 unsigned Opcode; 2979 2980 // We use DSGF for 32-bit division. 2981 if (is32Bit(VT)) { 2982 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0); 2983 Opcode = SystemZISD::SDIVREM32; 2984 } else if (DAG.ComputeNumSignBits(Op1) > 32) { 2985 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1); 2986 Opcode = SystemZISD::SDIVREM32; 2987 } else 2988 Opcode = SystemZISD::SDIVREM64; 2989 2990 // DSG(F) takes a 64-bit dividend, so the even register in the GR128 2991 // input is "don't care". The instruction returns the remainder in 2992 // the even register and the quotient in the odd register. 2993 SDValue Ops[2]; 2994 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode, 2995 Op0, Op1, Ops[1], Ops[0]); 2996 return DAG.getMergeValues(Ops, DL); 2997 } 2998 2999 SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op, 3000 SelectionDAG &DAG) const { 3001 EVT VT = Op.getValueType(); 3002 SDLoc DL(Op); 3003 3004 // DL(G) uses a double-width dividend, so we need to clear the even 3005 // register in the GR128 input. The instruction returns the remainder 3006 // in the even register and the quotient in the odd register. 3007 SDValue Ops[2]; 3008 if (is32Bit(VT)) 3009 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32, 3010 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); 3011 else 3012 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64, 3013 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); 3014 return DAG.getMergeValues(Ops, DL); 3015 } 3016 3017 SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const { 3018 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation"); 3019 3020 // Get the known-zero masks for each operand. 3021 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) }; 3022 APInt KnownZero[2], KnownOne[2]; 3023 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]); 3024 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]); 3025 3026 // See if the upper 32 bits of one operand and the lower 32 bits of the 3027 // other are known zero. They are the low and high operands respectively. 3028 uint64_t Masks[] = { KnownZero[0].getZExtValue(), 3029 KnownZero[1].getZExtValue() }; 3030 unsigned High, Low; 3031 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff) 3032 High = 1, Low = 0; 3033 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff) 3034 High = 0, Low = 1; 3035 else 3036 return Op; 3037 3038 SDValue LowOp = Ops[Low]; 3039 SDValue HighOp = Ops[High]; 3040 3041 // If the high part is a constant, we're better off using IILH. 3042 if (HighOp.getOpcode() == ISD::Constant) 3043 return Op; 3044 3045 // If the low part is a constant that is outside the range of LHI, 3046 // then we're better off using IILF. 3047 if (LowOp.getOpcode() == ISD::Constant) { 3048 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue()); 3049 if (!isInt<16>(Value)) 3050 return Op; 3051 } 3052 3053 // Check whether the high part is an AND that doesn't change the 3054 // high 32 bits and just masks out low bits. We can skip it if so. 3055 if (HighOp.getOpcode() == ISD::AND && 3056 HighOp.getOperand(1).getOpcode() == ISD::Constant) { 3057 SDValue HighOp0 = HighOp.getOperand(0); 3058 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue(); 3059 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff)))) 3060 HighOp = HighOp0; 3061 } 3062 3063 // Take advantage of the fact that all GR32 operations only change the 3064 // low 32 bits by truncating Low to an i32 and inserting it directly 3065 // using a subreg. The interesting cases are those where the truncation 3066 // can be folded. 3067 SDLoc DL(Op); 3068 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp); 3069 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL, 3070 MVT::i64, HighOp, Low32); 3071 } 3072 3073 SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op, 3074 SelectionDAG &DAG) const { 3075 EVT VT = Op.getValueType(); 3076 SDLoc DL(Op); 3077 Op = Op.getOperand(0); 3078 3079 // Handle vector types via VPOPCT. 3080 if (VT.isVector()) { 3081 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op); 3082 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op); 3083 switch (VT.getVectorElementType().getSizeInBits()) { 3084 case 8: 3085 break; 3086 case 16: { 3087 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op); 3088 SDValue Shift = DAG.getConstant(8, DL, MVT::i32); 3089 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift); 3090 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp); 3091 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift); 3092 break; 3093 } 3094 case 32: { 3095 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, 3096 DAG.getConstant(0, DL, MVT::i32)); 3097 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp); 3098 break; 3099 } 3100 case 64: { 3101 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, 3102 DAG.getConstant(0, DL, MVT::i32)); 3103 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp); 3104 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp); 3105 break; 3106 } 3107 default: 3108 llvm_unreachable("Unexpected type"); 3109 } 3110 return Op; 3111 } 3112 3113 // Get the known-zero mask for the operand. 3114 APInt KnownZero, KnownOne; 3115 DAG.computeKnownBits(Op, KnownZero, KnownOne); 3116 unsigned NumSignificantBits = (~KnownZero).getActiveBits(); 3117 if (NumSignificantBits == 0) 3118 return DAG.getConstant(0, DL, VT); 3119 3120 // Skip known-zero high parts of the operand. 3121 int64_t OrigBitSize = VT.getSizeInBits(); 3122 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits); 3123 BitSize = std::min(BitSize, OrigBitSize); 3124 3125 // The POPCNT instruction counts the number of bits in each byte. 3126 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op); 3127 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op); 3128 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op); 3129 3130 // Add up per-byte counts in a binary tree. All bits of Op at 3131 // position larger than BitSize remain zero throughout. 3132 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) { 3133 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT)); 3134 if (BitSize != OrigBitSize) 3135 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp, 3136 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT)); 3137 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp); 3138 } 3139 3140 // Extract overall result from high byte. 3141 if (BitSize > 8) 3142 Op = DAG.getNode(ISD::SRL, DL, VT, Op, 3143 DAG.getConstant(BitSize - 8, DL, VT)); 3144 3145 return Op; 3146 } 3147 3148 SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op, 3149 SelectionDAG &DAG) const { 3150 SDLoc DL(Op); 3151 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>( 3152 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()); 3153 SynchronizationScope FenceScope = static_cast<SynchronizationScope>( 3154 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue()); 3155 3156 // The only fence that needs an instruction is a sequentially-consistent 3157 // cross-thread fence. 3158 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent && 3159 FenceScope == CrossThread) { 3160 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other, 3161 Op.getOperand(0)), 3162 0); 3163 } 3164 3165 // MEMBARRIER is a compiler barrier; it codegens to a no-op. 3166 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0)); 3167 } 3168 3169 // Op is an atomic load. Lower it into a normal volatile load. 3170 SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op, 3171 SelectionDAG &DAG) const { 3172 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3173 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(), 3174 Node->getChain(), Node->getBasePtr(), 3175 Node->getMemoryVT(), Node->getMemOperand()); 3176 } 3177 3178 // Op is an atomic store. Lower it into a normal volatile store followed 3179 // by a serialization. 3180 SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op, 3181 SelectionDAG &DAG) const { 3182 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3183 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(), 3184 Node->getBasePtr(), Node->getMemoryVT(), 3185 Node->getMemOperand()); 3186 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other, 3187 Chain), 0); 3188 } 3189 3190 // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first 3191 // two into the fullword ATOMIC_LOADW_* operation given by Opcode. 3192 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op, 3193 SelectionDAG &DAG, 3194 unsigned Opcode) const { 3195 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3196 3197 // 32-bit operations need no code outside the main loop. 3198 EVT NarrowVT = Node->getMemoryVT(); 3199 EVT WideVT = MVT::i32; 3200 if (NarrowVT == WideVT) 3201 return Op; 3202 3203 int64_t BitSize = NarrowVT.getSizeInBits(); 3204 SDValue ChainIn = Node->getChain(); 3205 SDValue Addr = Node->getBasePtr(); 3206 SDValue Src2 = Node->getVal(); 3207 MachineMemOperand *MMO = Node->getMemOperand(); 3208 SDLoc DL(Node); 3209 EVT PtrVT = Addr.getValueType(); 3210 3211 // Convert atomic subtracts of constants into additions. 3212 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB) 3213 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) { 3214 Opcode = SystemZISD::ATOMIC_LOADW_ADD; 3215 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType()); 3216 } 3217 3218 // Get the address of the containing word. 3219 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr, 3220 DAG.getConstant(-4, DL, PtrVT)); 3221 3222 // Get the number of bits that the word must be rotated left in order 3223 // to bring the field to the top bits of a GR32. 3224 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr, 3225 DAG.getConstant(3, DL, PtrVT)); 3226 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift); 3227 3228 // Get the complementing shift amount, for rotating a field in the top 3229 // bits back to its proper position. 3230 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT, 3231 DAG.getConstant(0, DL, WideVT), BitShift); 3232 3233 // Extend the source operand to 32 bits and prepare it for the inner loop. 3234 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other 3235 // operations require the source to be shifted in advance. (This shift 3236 // can be folded if the source is constant.) For AND and NAND, the lower 3237 // bits must be set, while for other opcodes they should be left clear. 3238 if (Opcode != SystemZISD::ATOMIC_SWAPW) 3239 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2, 3240 DAG.getConstant(32 - BitSize, DL, WideVT)); 3241 if (Opcode == SystemZISD::ATOMIC_LOADW_AND || 3242 Opcode == SystemZISD::ATOMIC_LOADW_NAND) 3243 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2, 3244 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT)); 3245 3246 // Construct the ATOMIC_LOADW_* node. 3247 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other); 3248 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift, 3249 DAG.getConstant(BitSize, DL, WideVT) }; 3250 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops, 3251 NarrowVT, MMO); 3252 3253 // Rotate the result of the final CS so that the field is in the lower 3254 // bits of a GR32, then truncate it. 3255 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift, 3256 DAG.getConstant(BitSize, DL, WideVT)); 3257 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift); 3258 3259 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) }; 3260 return DAG.getMergeValues(RetOps, DL); 3261 } 3262 3263 // Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations 3264 // into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit 3265 // operations into additions. 3266 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op, 3267 SelectionDAG &DAG) const { 3268 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3269 EVT MemVT = Node->getMemoryVT(); 3270 if (MemVT == MVT::i32 || MemVT == MVT::i64) { 3271 // A full-width operation. 3272 assert(Op.getValueType() == MemVT && "Mismatched VTs"); 3273 SDValue Src2 = Node->getVal(); 3274 SDValue NegSrc2; 3275 SDLoc DL(Src2); 3276 3277 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) { 3278 // Use an addition if the operand is constant and either LAA(G) is 3279 // available or the negative value is in the range of A(G)FHI. 3280 int64_t Value = (-Op2->getAPIntValue()).getSExtValue(); 3281 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1()) 3282 NegSrc2 = DAG.getConstant(Value, DL, MemVT); 3283 } else if (Subtarget.hasInterlockedAccess1()) 3284 // Use LAA(G) if available. 3285 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT), 3286 Src2); 3287 3288 if (NegSrc2.getNode()) 3289 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT, 3290 Node->getChain(), Node->getBasePtr(), NegSrc2, 3291 Node->getMemOperand(), Node->getOrdering(), 3292 Node->getSynchScope()); 3293 3294 // Use the node as-is. 3295 return Op; 3296 } 3297 3298 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB); 3299 } 3300 3301 // Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two 3302 // into a fullword ATOMIC_CMP_SWAPW operation. 3303 SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op, 3304 SelectionDAG &DAG) const { 3305 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3306 3307 // We have native support for 32-bit compare and swap. 3308 EVT NarrowVT = Node->getMemoryVT(); 3309 EVT WideVT = MVT::i32; 3310 if (NarrowVT == WideVT) 3311 return Op; 3312 3313 int64_t BitSize = NarrowVT.getSizeInBits(); 3314 SDValue ChainIn = Node->getOperand(0); 3315 SDValue Addr = Node->getOperand(1); 3316 SDValue CmpVal = Node->getOperand(2); 3317 SDValue SwapVal = Node->getOperand(3); 3318 MachineMemOperand *MMO = Node->getMemOperand(); 3319 SDLoc DL(Node); 3320 EVT PtrVT = Addr.getValueType(); 3321 3322 // Get the address of the containing word. 3323 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr, 3324 DAG.getConstant(-4, DL, PtrVT)); 3325 3326 // Get the number of bits that the word must be rotated left in order 3327 // to bring the field to the top bits of a GR32. 3328 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr, 3329 DAG.getConstant(3, DL, PtrVT)); 3330 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift); 3331 3332 // Get the complementing shift amount, for rotating a field in the top 3333 // bits back to its proper position. 3334 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT, 3335 DAG.getConstant(0, DL, WideVT), BitShift); 3336 3337 // Construct the ATOMIC_CMP_SWAPW node. 3338 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other); 3339 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift, 3340 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) }; 3341 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL, 3342 VTList, Ops, NarrowVT, MMO); 3343 return AtomicOp; 3344 } 3345 3346 SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op, 3347 SelectionDAG &DAG) const { 3348 MachineFunction &MF = DAG.getMachineFunction(); 3349 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); 3350 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op), 3351 SystemZ::R15D, Op.getValueType()); 3352 } 3353 3354 SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op, 3355 SelectionDAG &DAG) const { 3356 MachineFunction &MF = DAG.getMachineFunction(); 3357 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); 3358 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain"); 3359 3360 SDValue Chain = Op.getOperand(0); 3361 SDValue NewSP = Op.getOperand(1); 3362 SDValue Backchain; 3363 SDLoc DL(Op); 3364 3365 if (StoreBackchain) { 3366 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64); 3367 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo(), 3368 false, false, false, 0); 3369 } 3370 3371 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP); 3372 3373 if (StoreBackchain) 3374 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo(), 3375 false, false, 0); 3376 3377 return Chain; 3378 } 3379 3380 SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op, 3381 SelectionDAG &DAG) const { 3382 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 3383 if (!IsData) 3384 // Just preserve the chain. 3385 return Op.getOperand(0); 3386 3387 SDLoc DL(Op); 3388 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); 3389 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ; 3390 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode()); 3391 SDValue Ops[] = { 3392 Op.getOperand(0), 3393 DAG.getConstant(Code, DL, MVT::i32), 3394 Op.getOperand(1) 3395 }; 3396 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL, 3397 Node->getVTList(), Ops, 3398 Node->getMemoryVT(), Node->getMemOperand()); 3399 } 3400 3401 // Return an i32 that contains the value of CC immediately after After, 3402 // whose final operand must be MVT::Glue. 3403 static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) { 3404 SDLoc DL(After); 3405 SDValue Glue = SDValue(After, After->getNumValues() - 1); 3406 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue); 3407 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM, 3408 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32)); 3409 } 3410 3411 SDValue 3412 SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op, 3413 SelectionDAG &DAG) const { 3414 unsigned Opcode, CCValid; 3415 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) { 3416 assert(Op->getNumValues() == 2 && "Expected only CC result and chain"); 3417 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode); 3418 SDValue CC = getCCResult(DAG, Glued.getNode()); 3419 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC); 3420 return SDValue(); 3421 } 3422 3423 return SDValue(); 3424 } 3425 3426 SDValue 3427 SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op, 3428 SelectionDAG &DAG) const { 3429 unsigned Opcode, CCValid; 3430 if (isIntrinsicWithCC(Op, Opcode, CCValid)) { 3431 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode); 3432 SDValue CC = getCCResult(DAG, Glued.getNode()); 3433 if (Op->getNumValues() == 1) 3434 return CC; 3435 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result"); 3436 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued, 3437 CC); 3438 } 3439 3440 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 3441 switch (Id) { 3442 case Intrinsic::thread_pointer: 3443 return lowerThreadPointer(SDLoc(Op), DAG); 3444 3445 case Intrinsic::s390_vpdi: 3446 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(), 3447 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 3448 3449 case Intrinsic::s390_vperm: 3450 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(), 3451 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 3452 3453 case Intrinsic::s390_vuphb: 3454 case Intrinsic::s390_vuphh: 3455 case Intrinsic::s390_vuphf: 3456 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(), 3457 Op.getOperand(1)); 3458 3459 case Intrinsic::s390_vuplhb: 3460 case Intrinsic::s390_vuplhh: 3461 case Intrinsic::s390_vuplhf: 3462 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(), 3463 Op.getOperand(1)); 3464 3465 case Intrinsic::s390_vuplb: 3466 case Intrinsic::s390_vuplhw: 3467 case Intrinsic::s390_vuplf: 3468 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(), 3469 Op.getOperand(1)); 3470 3471 case Intrinsic::s390_vupllb: 3472 case Intrinsic::s390_vupllh: 3473 case Intrinsic::s390_vupllf: 3474 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(), 3475 Op.getOperand(1)); 3476 3477 case Intrinsic::s390_vsumb: 3478 case Intrinsic::s390_vsumh: 3479 case Intrinsic::s390_vsumgh: 3480 case Intrinsic::s390_vsumgf: 3481 case Intrinsic::s390_vsumqf: 3482 case Intrinsic::s390_vsumqg: 3483 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(), 3484 Op.getOperand(1), Op.getOperand(2)); 3485 } 3486 3487 return SDValue(); 3488 } 3489 3490 namespace { 3491 // Says that SystemZISD operation Opcode can be used to perform the equivalent 3492 // of a VPERM with permute vector Bytes. If Opcode takes three operands, 3493 // Operand is the constant third operand, otherwise it is the number of 3494 // bytes in each element of the result. 3495 struct Permute { 3496 unsigned Opcode; 3497 unsigned Operand; 3498 unsigned char Bytes[SystemZ::VectorBytes]; 3499 }; 3500 } 3501 3502 static const Permute PermuteForms[] = { 3503 // VMRHG 3504 { SystemZISD::MERGE_HIGH, 8, 3505 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } }, 3506 // VMRHF 3507 { SystemZISD::MERGE_HIGH, 4, 3508 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } }, 3509 // VMRHH 3510 { SystemZISD::MERGE_HIGH, 2, 3511 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } }, 3512 // VMRHB 3513 { SystemZISD::MERGE_HIGH, 1, 3514 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } }, 3515 // VMRLG 3516 { SystemZISD::MERGE_LOW, 8, 3517 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } }, 3518 // VMRLF 3519 { SystemZISD::MERGE_LOW, 4, 3520 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } }, 3521 // VMRLH 3522 { SystemZISD::MERGE_LOW, 2, 3523 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } }, 3524 // VMRLB 3525 { SystemZISD::MERGE_LOW, 1, 3526 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } }, 3527 // VPKG 3528 { SystemZISD::PACK, 4, 3529 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } }, 3530 // VPKF 3531 { SystemZISD::PACK, 2, 3532 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } }, 3533 // VPKH 3534 { SystemZISD::PACK, 1, 3535 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } }, 3536 // VPDI V1, V2, 4 (low half of V1, high half of V2) 3537 { SystemZISD::PERMUTE_DWORDS, 4, 3538 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } }, 3539 // VPDI V1, V2, 1 (high half of V1, low half of V2) 3540 { SystemZISD::PERMUTE_DWORDS, 1, 3541 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } } 3542 }; 3543 3544 // Called after matching a vector shuffle against a particular pattern. 3545 // Both the original shuffle and the pattern have two vector operands. 3546 // OpNos[0] is the operand of the original shuffle that should be used for 3547 // operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything. 3548 // OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and 3549 // set OpNo0 and OpNo1 to the shuffle operands that should actually be used 3550 // for operands 0 and 1 of the pattern. 3551 static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) { 3552 if (OpNos[0] < 0) { 3553 if (OpNos[1] < 0) 3554 return false; 3555 OpNo0 = OpNo1 = OpNos[1]; 3556 } else if (OpNos[1] < 0) { 3557 OpNo0 = OpNo1 = OpNos[0]; 3558 } else { 3559 OpNo0 = OpNos[0]; 3560 OpNo1 = OpNos[1]; 3561 } 3562 return true; 3563 } 3564 3565 // Bytes is a VPERM-like permute vector, except that -1 is used for 3566 // undefined bytes. Return true if the VPERM can be implemented using P. 3567 // When returning true set OpNo0 to the VPERM operand that should be 3568 // used for operand 0 of P and likewise OpNo1 for operand 1 of P. 3569 // 3570 // For example, if swapping the VPERM operands allows P to match, OpNo0 3571 // will be 1 and OpNo1 will be 0. If instead Bytes only refers to one 3572 // operand, but rewriting it to use two duplicated operands allows it to 3573 // match P, then OpNo0 and OpNo1 will be the same. 3574 static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P, 3575 unsigned &OpNo0, unsigned &OpNo1) { 3576 int OpNos[] = { -1, -1 }; 3577 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) { 3578 int Elt = Bytes[I]; 3579 if (Elt >= 0) { 3580 // Make sure that the two permute vectors use the same suboperand 3581 // byte number. Only the operand numbers (the high bits) are 3582 // allowed to differ. 3583 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1)) 3584 return false; 3585 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes; 3586 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes; 3587 // Make sure that the operand mappings are consistent with previous 3588 // elements. 3589 if (OpNos[ModelOpNo] == 1 - RealOpNo) 3590 return false; 3591 OpNos[ModelOpNo] = RealOpNo; 3592 } 3593 } 3594 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1); 3595 } 3596 3597 // As above, but search for a matching permute. 3598 static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes, 3599 unsigned &OpNo0, unsigned &OpNo1) { 3600 for (auto &P : PermuteForms) 3601 if (matchPermute(Bytes, P, OpNo0, OpNo1)) 3602 return &P; 3603 return nullptr; 3604 } 3605 3606 // Bytes is a VPERM-like permute vector, except that -1 is used for 3607 // undefined bytes. This permute is an operand of an outer permute. 3608 // See whether redistributing the -1 bytes gives a shuffle that can be 3609 // implemented using P. If so, set Transform to a VPERM-like permute vector 3610 // that, when applied to the result of P, gives the original permute in Bytes. 3611 static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes, 3612 const Permute &P, 3613 SmallVectorImpl<int> &Transform) { 3614 unsigned To = 0; 3615 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) { 3616 int Elt = Bytes[From]; 3617 if (Elt < 0) 3618 // Byte number From of the result is undefined. 3619 Transform[From] = -1; 3620 else { 3621 while (P.Bytes[To] != Elt) { 3622 To += 1; 3623 if (To == SystemZ::VectorBytes) 3624 return false; 3625 } 3626 Transform[From] = To; 3627 } 3628 } 3629 return true; 3630 } 3631 3632 // As above, but search for a matching permute. 3633 static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes, 3634 SmallVectorImpl<int> &Transform) { 3635 for (auto &P : PermuteForms) 3636 if (matchDoublePermute(Bytes, P, Transform)) 3637 return &P; 3638 return nullptr; 3639 } 3640 3641 // Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask, 3642 // as if it had type vNi8. 3643 static void getVPermMask(ShuffleVectorSDNode *VSN, 3644 SmallVectorImpl<int> &Bytes) { 3645 EVT VT = VSN->getValueType(0); 3646 unsigned NumElements = VT.getVectorNumElements(); 3647 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); 3648 Bytes.resize(NumElements * BytesPerElement, -1); 3649 for (unsigned I = 0; I < NumElements; ++I) { 3650 int Index = VSN->getMaskElt(I); 3651 if (Index >= 0) 3652 for (unsigned J = 0; J < BytesPerElement; ++J) 3653 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J; 3654 } 3655 } 3656 3657 // Bytes is a VPERM-like permute vector, except that -1 is used for 3658 // undefined bytes. See whether bytes [Start, Start + BytesPerElement) of 3659 // the result come from a contiguous sequence of bytes from one input. 3660 // Set Base to the selector for the first byte if so. 3661 static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start, 3662 unsigned BytesPerElement, int &Base) { 3663 Base = -1; 3664 for (unsigned I = 0; I < BytesPerElement; ++I) { 3665 if (Bytes[Start + I] >= 0) { 3666 unsigned Elem = Bytes[Start + I]; 3667 if (Base < 0) { 3668 Base = Elem - I; 3669 // Make sure the bytes would come from one input operand. 3670 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size()) 3671 return false; 3672 } else if (unsigned(Base) != Elem - I) 3673 return false; 3674 } 3675 } 3676 return true; 3677 } 3678 3679 // Bytes is a VPERM-like permute vector, except that -1 is used for 3680 // undefined bytes. Return true if it can be performed using VSLDI. 3681 // When returning true, set StartIndex to the shift amount and OpNo0 3682 // and OpNo1 to the VPERM operands that should be used as the first 3683 // and second shift operand respectively. 3684 static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes, 3685 unsigned &StartIndex, unsigned &OpNo0, 3686 unsigned &OpNo1) { 3687 int OpNos[] = { -1, -1 }; 3688 int Shift = -1; 3689 for (unsigned I = 0; I < 16; ++I) { 3690 int Index = Bytes[I]; 3691 if (Index >= 0) { 3692 int ExpectedShift = (Index - I) % SystemZ::VectorBytes; 3693 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes; 3694 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes; 3695 if (Shift < 0) 3696 Shift = ExpectedShift; 3697 else if (Shift != ExpectedShift) 3698 return false; 3699 // Make sure that the operand mappings are consistent with previous 3700 // elements. 3701 if (OpNos[ModelOpNo] == 1 - RealOpNo) 3702 return false; 3703 OpNos[ModelOpNo] = RealOpNo; 3704 } 3705 } 3706 StartIndex = Shift; 3707 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1); 3708 } 3709 3710 // Create a node that performs P on operands Op0 and Op1, casting the 3711 // operands to the appropriate type. The type of the result is determined by P. 3712 static SDValue getPermuteNode(SelectionDAG &DAG, SDLoc DL, 3713 const Permute &P, SDValue Op0, SDValue Op1) { 3714 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input 3715 // elements of a PACK are twice as wide as the outputs. 3716 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 : 3717 P.Opcode == SystemZISD::PACK ? P.Operand * 2 : 3718 P.Operand); 3719 // Cast both operands to the appropriate type. 3720 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8), 3721 SystemZ::VectorBytes / InBytes); 3722 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0); 3723 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1); 3724 SDValue Op; 3725 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) { 3726 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32); 3727 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2); 3728 } else if (P.Opcode == SystemZISD::PACK) { 3729 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8), 3730 SystemZ::VectorBytes / P.Operand); 3731 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1); 3732 } else { 3733 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1); 3734 } 3735 return Op; 3736 } 3737 3738 // Bytes is a VPERM-like permute vector, except that -1 is used for 3739 // undefined bytes. Implement it on operands Ops[0] and Ops[1] using 3740 // VSLDI or VPERM. 3741 static SDValue getGeneralPermuteNode(SelectionDAG &DAG, SDLoc DL, SDValue *Ops, 3742 const SmallVectorImpl<int> &Bytes) { 3743 for (unsigned I = 0; I < 2; ++I) 3744 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]); 3745 3746 // First see whether VSLDI can be used. 3747 unsigned StartIndex, OpNo0, OpNo1; 3748 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1)) 3749 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0], 3750 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32)); 3751 3752 // Fall back on VPERM. Construct an SDNode for the permute vector. 3753 SDValue IndexNodes[SystemZ::VectorBytes]; 3754 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) 3755 if (Bytes[I] >= 0) 3756 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32); 3757 else 3758 IndexNodes[I] = DAG.getUNDEF(MVT::i32); 3759 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes); 3760 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2); 3761 } 3762 3763 namespace { 3764 // Describes a general N-operand vector shuffle. 3765 struct GeneralShuffle { 3766 GeneralShuffle(EVT vt) : VT(vt) {} 3767 void addUndef(); 3768 void add(SDValue, unsigned); 3769 SDValue getNode(SelectionDAG &, SDLoc); 3770 3771 // The operands of the shuffle. 3772 SmallVector<SDValue, SystemZ::VectorBytes> Ops; 3773 3774 // Index I is -1 if byte I of the result is undefined. Otherwise the 3775 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand 3776 // Bytes[I] / SystemZ::VectorBytes. 3777 SmallVector<int, SystemZ::VectorBytes> Bytes; 3778 3779 // The type of the shuffle result. 3780 EVT VT; 3781 }; 3782 } 3783 3784 // Add an extra undefined element to the shuffle. 3785 void GeneralShuffle::addUndef() { 3786 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); 3787 for (unsigned I = 0; I < BytesPerElement; ++I) 3788 Bytes.push_back(-1); 3789 } 3790 3791 // Add an extra element to the shuffle, taking it from element Elem of Op. 3792 // A null Op indicates a vector input whose value will be calculated later; 3793 // there is at most one such input per shuffle and it always has the same 3794 // type as the result. 3795 void GeneralShuffle::add(SDValue Op, unsigned Elem) { 3796 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); 3797 3798 // The source vector can have wider elements than the result, 3799 // either through an explicit TRUNCATE or because of type legalization. 3800 // We want the least significant part. 3801 EVT FromVT = Op.getNode() ? Op.getValueType() : VT; 3802 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize(); 3803 assert(FromBytesPerElement >= BytesPerElement && 3804 "Invalid EXTRACT_VECTOR_ELT"); 3805 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes + 3806 (FromBytesPerElement - BytesPerElement)); 3807 3808 // Look through things like shuffles and bitcasts. 3809 while (Op.getNode()) { 3810 if (Op.getOpcode() == ISD::BITCAST) 3811 Op = Op.getOperand(0); 3812 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) { 3813 // See whether the bytes we need come from a contiguous part of one 3814 // operand. 3815 SmallVector<int, SystemZ::VectorBytes> OpBytes; 3816 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes); 3817 int NewByte; 3818 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte)) 3819 break; 3820 if (NewByte < 0) { 3821 addUndef(); 3822 return; 3823 } 3824 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes); 3825 Byte = unsigned(NewByte) % SystemZ::VectorBytes; 3826 } else if (Op.isUndef()) { 3827 addUndef(); 3828 return; 3829 } else 3830 break; 3831 } 3832 3833 // Make sure that the source of the extraction is in Ops. 3834 unsigned OpNo = 0; 3835 for (; OpNo < Ops.size(); ++OpNo) 3836 if (Ops[OpNo] == Op) 3837 break; 3838 if (OpNo == Ops.size()) 3839 Ops.push_back(Op); 3840 3841 // Add the element to Bytes. 3842 unsigned Base = OpNo * SystemZ::VectorBytes + Byte; 3843 for (unsigned I = 0; I < BytesPerElement; ++I) 3844 Bytes.push_back(Base + I); 3845 } 3846 3847 // Return SDNodes for the completed shuffle. 3848 SDValue GeneralShuffle::getNode(SelectionDAG &DAG, SDLoc DL) { 3849 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector"); 3850 3851 if (Ops.size() == 0) 3852 return DAG.getUNDEF(VT); 3853 3854 // Make sure that there are at least two shuffle operands. 3855 if (Ops.size() == 1) 3856 Ops.push_back(DAG.getUNDEF(MVT::v16i8)); 3857 3858 // Create a tree of shuffles, deferring root node until after the loop. 3859 // Try to redistribute the undefined elements of non-root nodes so that 3860 // the non-root shuffles match something like a pack or merge, then adjust 3861 // the parent node's permute vector to compensate for the new order. 3862 // Among other things, this copes with vectors like <2 x i16> that were 3863 // padded with undefined elements during type legalization. 3864 // 3865 // In the best case this redistribution will lead to the whole tree 3866 // using packs and merges. It should rarely be a loss in other cases. 3867 unsigned Stride = 1; 3868 for (; Stride * 2 < Ops.size(); Stride *= 2) { 3869 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) { 3870 SDValue SubOps[] = { Ops[I], Ops[I + Stride] }; 3871 3872 // Create a mask for just these two operands. 3873 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes); 3874 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) { 3875 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes; 3876 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes; 3877 if (OpNo == I) 3878 NewBytes[J] = Byte; 3879 else if (OpNo == I + Stride) 3880 NewBytes[J] = SystemZ::VectorBytes + Byte; 3881 else 3882 NewBytes[J] = -1; 3883 } 3884 // See if it would be better to reorganize NewMask to avoid using VPERM. 3885 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes); 3886 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) { 3887 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]); 3888 // Applying NewBytesMap to Ops[I] gets back to NewBytes. 3889 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) { 3890 if (NewBytes[J] >= 0) { 3891 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes && 3892 "Invalid double permute"); 3893 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J]; 3894 } else 3895 assert(NewBytesMap[J] < 0 && "Invalid double permute"); 3896 } 3897 } else { 3898 // Just use NewBytes on the operands. 3899 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes); 3900 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) 3901 if (NewBytes[J] >= 0) 3902 Bytes[J] = I * SystemZ::VectorBytes + J; 3903 } 3904 } 3905 } 3906 3907 // Now we just have 2 inputs. Put the second operand in Ops[1]. 3908 if (Stride > 1) { 3909 Ops[1] = Ops[Stride]; 3910 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) 3911 if (Bytes[I] >= int(SystemZ::VectorBytes)) 3912 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes; 3913 } 3914 3915 // Look for an instruction that can do the permute without resorting 3916 // to VPERM. 3917 unsigned OpNo0, OpNo1; 3918 SDValue Op; 3919 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1)) 3920 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]); 3921 else 3922 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes); 3923 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 3924 } 3925 3926 // Return true if the given BUILD_VECTOR is a scalar-to-vector conversion. 3927 static bool isScalarToVector(SDValue Op) { 3928 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I) 3929 if (!Op.getOperand(I).isUndef()) 3930 return false; 3931 return true; 3932 } 3933 3934 // Return a vector of type VT that contains Value in the first element. 3935 // The other elements don't matter. 3936 static SDValue buildScalarToVector(SelectionDAG &DAG, SDLoc DL, EVT VT, 3937 SDValue Value) { 3938 // If we have a constant, replicate it to all elements and let the 3939 // BUILD_VECTOR lowering take care of it. 3940 if (Value.getOpcode() == ISD::Constant || 3941 Value.getOpcode() == ISD::ConstantFP) { 3942 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value); 3943 return DAG.getBuildVector(VT, DL, Ops); 3944 } 3945 if (Value.isUndef()) 3946 return DAG.getUNDEF(VT); 3947 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value); 3948 } 3949 3950 // Return a vector of type VT in which Op0 is in element 0 and Op1 is in 3951 // element 1. Used for cases in which replication is cheap. 3952 static SDValue buildMergeScalars(SelectionDAG &DAG, SDLoc DL, EVT VT, 3953 SDValue Op0, SDValue Op1) { 3954 if (Op0.isUndef()) { 3955 if (Op1.isUndef()) 3956 return DAG.getUNDEF(VT); 3957 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1); 3958 } 3959 if (Op1.isUndef()) 3960 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0); 3961 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT, 3962 buildScalarToVector(DAG, DL, VT, Op0), 3963 buildScalarToVector(DAG, DL, VT, Op1)); 3964 } 3965 3966 // Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64 3967 // vector for them. 3968 static SDValue joinDwords(SelectionDAG &DAG, SDLoc DL, SDValue Op0, 3969 SDValue Op1) { 3970 if (Op0.isUndef() && Op1.isUndef()) 3971 return DAG.getUNDEF(MVT::v2i64); 3972 // If one of the two inputs is undefined then replicate the other one, 3973 // in order to avoid using another register unnecessarily. 3974 if (Op0.isUndef()) 3975 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1); 3976 else if (Op1.isUndef()) 3977 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0); 3978 else { 3979 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0); 3980 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1); 3981 } 3982 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1); 3983 } 3984 3985 // Try to represent constant BUILD_VECTOR node BVN using a 3986 // SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask 3987 // on success. 3988 static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) { 3989 EVT ElemVT = BVN->getValueType(0).getVectorElementType(); 3990 unsigned BytesPerElement = ElemVT.getStoreSize(); 3991 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) { 3992 SDValue Op = BVN->getOperand(I); 3993 if (!Op.isUndef()) { 3994 uint64_t Value; 3995 if (Op.getOpcode() == ISD::Constant) 3996 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue(); 3997 else if (Op.getOpcode() == ISD::ConstantFP) 3998 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt() 3999 .getZExtValue()); 4000 else 4001 return false; 4002 for (unsigned J = 0; J < BytesPerElement; ++J) { 4003 uint64_t Byte = (Value >> (J * 8)) & 0xff; 4004 if (Byte == 0xff) 4005 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J); 4006 else if (Byte != 0) 4007 return false; 4008 } 4009 } 4010 } 4011 return true; 4012 } 4013 4014 // Try to load a vector constant in which BitsPerElement-bit value Value 4015 // is replicated to fill the vector. VT is the type of the resulting 4016 // constant, which may have elements of a different size from BitsPerElement. 4017 // Return the SDValue of the constant on success, otherwise return 4018 // an empty value. 4019 static SDValue tryBuildVectorReplicate(SelectionDAG &DAG, 4020 const SystemZInstrInfo *TII, 4021 SDLoc DL, EVT VT, uint64_t Value, 4022 unsigned BitsPerElement) { 4023 // Signed 16-bit values can be replicated using VREPI. 4024 int64_t SignedValue = SignExtend64(Value, BitsPerElement); 4025 if (isInt<16>(SignedValue)) { 4026 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement), 4027 SystemZ::VectorBits / BitsPerElement); 4028 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT, 4029 DAG.getConstant(SignedValue, DL, MVT::i32)); 4030 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 4031 } 4032 // See whether rotating the constant left some N places gives a value that 4033 // is one less than a power of 2 (i.e. all zeros followed by all ones). 4034 // If so we can use VGM. 4035 unsigned Start, End; 4036 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) { 4037 // isRxSBGMask returns the bit numbers for a full 64-bit value, 4038 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to 4039 // bit numbers for an BitsPerElement value, so that 0 denotes 4040 // 1 << (BitsPerElement-1). 4041 Start -= 64 - BitsPerElement; 4042 End -= 64 - BitsPerElement; 4043 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement), 4044 SystemZ::VectorBits / BitsPerElement); 4045 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT, 4046 DAG.getConstant(Start, DL, MVT::i32), 4047 DAG.getConstant(End, DL, MVT::i32)); 4048 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 4049 } 4050 return SDValue(); 4051 } 4052 4053 // If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually 4054 // better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for 4055 // the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR 4056 // would benefit from this representation and return it if so. 4057 static SDValue tryBuildVectorShuffle(SelectionDAG &DAG, 4058 BuildVectorSDNode *BVN) { 4059 EVT VT = BVN->getValueType(0); 4060 unsigned NumElements = VT.getVectorNumElements(); 4061 4062 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation 4063 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still 4064 // need a BUILD_VECTOR, add an additional placeholder operand for that 4065 // BUILD_VECTOR and store its operands in ResidueOps. 4066 GeneralShuffle GS(VT); 4067 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps; 4068 bool FoundOne = false; 4069 for (unsigned I = 0; I < NumElements; ++I) { 4070 SDValue Op = BVN->getOperand(I); 4071 if (Op.getOpcode() == ISD::TRUNCATE) 4072 Op = Op.getOperand(0); 4073 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 4074 Op.getOperand(1).getOpcode() == ISD::Constant) { 4075 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 4076 GS.add(Op.getOperand(0), Elem); 4077 FoundOne = true; 4078 } else if (Op.isUndef()) { 4079 GS.addUndef(); 4080 } else { 4081 GS.add(SDValue(), ResidueOps.size()); 4082 ResidueOps.push_back(BVN->getOperand(I)); 4083 } 4084 } 4085 4086 // Nothing to do if there are no EXTRACT_VECTOR_ELTs. 4087 if (!FoundOne) 4088 return SDValue(); 4089 4090 // Create the BUILD_VECTOR for the remaining elements, if any. 4091 if (!ResidueOps.empty()) { 4092 while (ResidueOps.size() < NumElements) 4093 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType())); 4094 for (auto &Op : GS.Ops) { 4095 if (!Op.getNode()) { 4096 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps); 4097 break; 4098 } 4099 } 4100 } 4101 return GS.getNode(DAG, SDLoc(BVN)); 4102 } 4103 4104 // Combine GPR scalar values Elems into a vector of type VT. 4105 static SDValue buildVector(SelectionDAG &DAG, SDLoc DL, EVT VT, 4106 SmallVectorImpl<SDValue> &Elems) { 4107 // See whether there is a single replicated value. 4108 SDValue Single; 4109 unsigned int NumElements = Elems.size(); 4110 unsigned int Count = 0; 4111 for (auto Elem : Elems) { 4112 if (!Elem.isUndef()) { 4113 if (!Single.getNode()) 4114 Single = Elem; 4115 else if (Elem != Single) { 4116 Single = SDValue(); 4117 break; 4118 } 4119 Count += 1; 4120 } 4121 } 4122 // There are three cases here: 4123 // 4124 // - if the only defined element is a loaded one, the best sequence 4125 // is a replicating load. 4126 // 4127 // - otherwise, if the only defined element is an i64 value, we will 4128 // end up with the same VLVGP sequence regardless of whether we short-cut 4129 // for replication or fall through to the later code. 4130 // 4131 // - otherwise, if the only defined element is an i32 or smaller value, 4132 // we would need 2 instructions to replicate it: VLVGP followed by VREPx. 4133 // This is only a win if the single defined element is used more than once. 4134 // In other cases we're better off using a single VLVGx. 4135 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD)) 4136 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single); 4137 4138 // The best way of building a v2i64 from two i64s is to use VLVGP. 4139 if (VT == MVT::v2i64) 4140 return joinDwords(DAG, DL, Elems[0], Elems[1]); 4141 4142 // Use a 64-bit merge high to combine two doubles. 4143 if (VT == MVT::v2f64) 4144 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]); 4145 4146 // Build v4f32 values directly from the FPRs: 4147 // 4148 // <Axxx> <Bxxx> <Cxxxx> <Dxxx> 4149 // V V VMRHF 4150 // <ABxx> <CDxx> 4151 // V VMRHG 4152 // <ABCD> 4153 if (VT == MVT::v4f32) { 4154 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]); 4155 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]); 4156 // Avoid unnecessary undefs by reusing the other operand. 4157 if (Op01.isUndef()) 4158 Op01 = Op23; 4159 else if (Op23.isUndef()) 4160 Op23 = Op01; 4161 // Merging identical replications is a no-op. 4162 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23) 4163 return Op01; 4164 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01); 4165 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23); 4166 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH, 4167 DL, MVT::v2i64, Op01, Op23); 4168 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 4169 } 4170 4171 // Collect the constant terms. 4172 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue()); 4173 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false); 4174 4175 unsigned NumConstants = 0; 4176 for (unsigned I = 0; I < NumElements; ++I) { 4177 SDValue Elem = Elems[I]; 4178 if (Elem.getOpcode() == ISD::Constant || 4179 Elem.getOpcode() == ISD::ConstantFP) { 4180 NumConstants += 1; 4181 Constants[I] = Elem; 4182 Done[I] = true; 4183 } 4184 } 4185 // If there was at least one constant, fill in the other elements of 4186 // Constants with undefs to get a full vector constant and use that 4187 // as the starting point. 4188 SDValue Result; 4189 if (NumConstants > 0) { 4190 for (unsigned I = 0; I < NumElements; ++I) 4191 if (!Constants[I].getNode()) 4192 Constants[I] = DAG.getUNDEF(Elems[I].getValueType()); 4193 Result = DAG.getBuildVector(VT, DL, Constants); 4194 } else { 4195 // Otherwise try to use VLVGP to start the sequence in order to 4196 // avoid a false dependency on any previous contents of the vector 4197 // register. This only makes sense if one of the associated elements 4198 // is defined. 4199 unsigned I1 = NumElements / 2 - 1; 4200 unsigned I2 = NumElements - 1; 4201 bool Def1 = !Elems[I1].isUndef(); 4202 bool Def2 = !Elems[I2].isUndef(); 4203 if (Def1 || Def2) { 4204 SDValue Elem1 = Elems[Def1 ? I1 : I2]; 4205 SDValue Elem2 = Elems[Def2 ? I2 : I1]; 4206 Result = DAG.getNode(ISD::BITCAST, DL, VT, 4207 joinDwords(DAG, DL, Elem1, Elem2)); 4208 Done[I1] = true; 4209 Done[I2] = true; 4210 } else 4211 Result = DAG.getUNDEF(VT); 4212 } 4213 4214 // Use VLVGx to insert the other elements. 4215 for (unsigned I = 0; I < NumElements; ++I) 4216 if (!Done[I] && !Elems[I].isUndef()) 4217 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I], 4218 DAG.getConstant(I, DL, MVT::i32)); 4219 return Result; 4220 } 4221 4222 SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op, 4223 SelectionDAG &DAG) const { 4224 const SystemZInstrInfo *TII = 4225 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 4226 auto *BVN = cast<BuildVectorSDNode>(Op.getNode()); 4227 SDLoc DL(Op); 4228 EVT VT = Op.getValueType(); 4229 4230 if (BVN->isConstant()) { 4231 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally- 4232 // preferred way of creating all-zero and all-one vectors so give it 4233 // priority over other methods below. 4234 uint64_t Mask = 0; 4235 if (tryBuildVectorByteMask(BVN, Mask)) { 4236 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, 4237 DAG.getConstant(Mask, DL, MVT::i32)); 4238 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 4239 } 4240 4241 // Try using some form of replication. 4242 APInt SplatBits, SplatUndef; 4243 unsigned SplatBitSize; 4244 bool HasAnyUndefs; 4245 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, 4246 8, true) && 4247 SplatBitSize <= 64) { 4248 // First try assuming that any undefined bits above the highest set bit 4249 // and below the lowest set bit are 1s. This increases the likelihood of 4250 // being able to use a sign-extended element value in VECTOR REPLICATE 4251 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK. 4252 uint64_t SplatBitsZ = SplatBits.getZExtValue(); 4253 uint64_t SplatUndefZ = SplatUndef.getZExtValue(); 4254 uint64_t Lower = (SplatUndefZ 4255 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1)); 4256 uint64_t Upper = (SplatUndefZ 4257 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1)); 4258 uint64_t Value = SplatBitsZ | Upper | Lower; 4259 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, 4260 SplatBitSize); 4261 if (Op.getNode()) 4262 return Op; 4263 4264 // Now try assuming that any undefined bits between the first and 4265 // last defined set bits are set. This increases the chances of 4266 // using a non-wraparound mask. 4267 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower; 4268 Value = SplatBitsZ | Middle; 4269 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize); 4270 if (Op.getNode()) 4271 return Op; 4272 } 4273 4274 // Fall back to loading it from memory. 4275 return SDValue(); 4276 } 4277 4278 // See if we should use shuffles to construct the vector from other vectors. 4279 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN)) 4280 return Res; 4281 4282 // Detect SCALAR_TO_VECTOR conversions. 4283 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op)) 4284 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0)); 4285 4286 // Otherwise use buildVector to build the vector up from GPRs. 4287 unsigned NumElements = Op.getNumOperands(); 4288 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements); 4289 for (unsigned I = 0; I < NumElements; ++I) 4290 Ops[I] = Op.getOperand(I); 4291 return buildVector(DAG, DL, VT, Ops); 4292 } 4293 4294 SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op, 4295 SelectionDAG &DAG) const { 4296 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode()); 4297 SDLoc DL(Op); 4298 EVT VT = Op.getValueType(); 4299 unsigned NumElements = VT.getVectorNumElements(); 4300 4301 if (VSN->isSplat()) { 4302 SDValue Op0 = Op.getOperand(0); 4303 unsigned Index = VSN->getSplatIndex(); 4304 assert(Index < VT.getVectorNumElements() && 4305 "Splat index should be defined and in first operand"); 4306 // See whether the value we're splatting is directly available as a scalar. 4307 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) || 4308 Op0.getOpcode() == ISD::BUILD_VECTOR) 4309 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index)); 4310 // Otherwise keep it as a vector-to-vector operation. 4311 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0), 4312 DAG.getConstant(Index, DL, MVT::i32)); 4313 } 4314 4315 GeneralShuffle GS(VT); 4316 for (unsigned I = 0; I < NumElements; ++I) { 4317 int Elt = VSN->getMaskElt(I); 4318 if (Elt < 0) 4319 GS.addUndef(); 4320 else 4321 GS.add(Op.getOperand(unsigned(Elt) / NumElements), 4322 unsigned(Elt) % NumElements); 4323 } 4324 return GS.getNode(DAG, SDLoc(VSN)); 4325 } 4326 4327 SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op, 4328 SelectionDAG &DAG) const { 4329 SDLoc DL(Op); 4330 // Just insert the scalar into element 0 of an undefined vector. 4331 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, 4332 Op.getValueType(), DAG.getUNDEF(Op.getValueType()), 4333 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32)); 4334 } 4335 4336 SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op, 4337 SelectionDAG &DAG) const { 4338 // Handle insertions of floating-point values. 4339 SDLoc DL(Op); 4340 SDValue Op0 = Op.getOperand(0); 4341 SDValue Op1 = Op.getOperand(1); 4342 SDValue Op2 = Op.getOperand(2); 4343 EVT VT = Op.getValueType(); 4344 4345 // Insertions into constant indices of a v2f64 can be done using VPDI. 4346 // However, if the inserted value is a bitcast or a constant then it's 4347 // better to use GPRs, as below. 4348 if (VT == MVT::v2f64 && 4349 Op1.getOpcode() != ISD::BITCAST && 4350 Op1.getOpcode() != ISD::ConstantFP && 4351 Op2.getOpcode() == ISD::Constant) { 4352 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue(); 4353 unsigned Mask = VT.getVectorNumElements() - 1; 4354 if (Index <= Mask) 4355 return Op; 4356 } 4357 4358 // Otherwise bitcast to the equivalent integer form and insert via a GPR. 4359 MVT IntVT = MVT::getIntegerVT(VT.getVectorElementType().getSizeInBits()); 4360 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements()); 4361 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT, 4362 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), 4363 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2); 4364 return DAG.getNode(ISD::BITCAST, DL, VT, Res); 4365 } 4366 4367 SDValue 4368 SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op, 4369 SelectionDAG &DAG) const { 4370 // Handle extractions of floating-point values. 4371 SDLoc DL(Op); 4372 SDValue Op0 = Op.getOperand(0); 4373 SDValue Op1 = Op.getOperand(1); 4374 EVT VT = Op.getValueType(); 4375 EVT VecVT = Op0.getValueType(); 4376 4377 // Extractions of constant indices can be done directly. 4378 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) { 4379 uint64_t Index = CIndexN->getZExtValue(); 4380 unsigned Mask = VecVT.getVectorNumElements() - 1; 4381 if (Index <= Mask) 4382 return Op; 4383 } 4384 4385 // Otherwise bitcast to the equivalent integer form and extract via a GPR. 4386 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits()); 4387 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements()); 4388 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT, 4389 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1); 4390 return DAG.getNode(ISD::BITCAST, DL, VT, Res); 4391 } 4392 4393 SDValue 4394 SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG, 4395 unsigned UnpackHigh) const { 4396 SDValue PackedOp = Op.getOperand(0); 4397 EVT OutVT = Op.getValueType(); 4398 EVT InVT = PackedOp.getValueType(); 4399 unsigned ToBits = OutVT.getVectorElementType().getSizeInBits(); 4400 unsigned FromBits = InVT.getVectorElementType().getSizeInBits(); 4401 do { 4402 FromBits *= 2; 4403 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits), 4404 SystemZ::VectorBits / FromBits); 4405 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp); 4406 } while (FromBits != ToBits); 4407 return PackedOp; 4408 } 4409 4410 SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG, 4411 unsigned ByScalar) const { 4412 // Look for cases where a vector shift can use the *_BY_SCALAR form. 4413 SDValue Op0 = Op.getOperand(0); 4414 SDValue Op1 = Op.getOperand(1); 4415 SDLoc DL(Op); 4416 EVT VT = Op.getValueType(); 4417 unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits(); 4418 4419 // See whether the shift vector is a splat represented as BUILD_VECTOR. 4420 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) { 4421 APInt SplatBits, SplatUndef; 4422 unsigned SplatBitSize; 4423 bool HasAnyUndefs; 4424 // Check for constant splats. Use ElemBitSize as the minimum element 4425 // width and reject splats that need wider elements. 4426 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, 4427 ElemBitSize, true) && 4428 SplatBitSize == ElemBitSize) { 4429 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff, 4430 DL, MVT::i32); 4431 return DAG.getNode(ByScalar, DL, VT, Op0, Shift); 4432 } 4433 // Check for variable splats. 4434 BitVector UndefElements; 4435 SDValue Splat = BVN->getSplatValue(&UndefElements); 4436 if (Splat) { 4437 // Since i32 is the smallest legal type, we either need a no-op 4438 // or a truncation. 4439 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat); 4440 return DAG.getNode(ByScalar, DL, VT, Op0, Shift); 4441 } 4442 } 4443 4444 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR, 4445 // and the shift amount is directly available in a GPR. 4446 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) { 4447 if (VSN->isSplat()) { 4448 SDValue VSNOp0 = VSN->getOperand(0); 4449 unsigned Index = VSN->getSplatIndex(); 4450 assert(Index < VT.getVectorNumElements() && 4451 "Splat index should be defined and in first operand"); 4452 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) || 4453 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) { 4454 // Since i32 is the smallest legal type, we either need a no-op 4455 // or a truncation. 4456 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, 4457 VSNOp0.getOperand(Index)); 4458 return DAG.getNode(ByScalar, DL, VT, Op0, Shift); 4459 } 4460 } 4461 } 4462 4463 // Otherwise just treat the current form as legal. 4464 return Op; 4465 } 4466 4467 SDValue SystemZTargetLowering::LowerOperation(SDValue Op, 4468 SelectionDAG &DAG) const { 4469 switch (Op.getOpcode()) { 4470 case ISD::FRAMEADDR: 4471 return lowerFRAMEADDR(Op, DAG); 4472 case ISD::RETURNADDR: 4473 return lowerRETURNADDR(Op, DAG); 4474 case ISD::BR_CC: 4475 return lowerBR_CC(Op, DAG); 4476 case ISD::SELECT_CC: 4477 return lowerSELECT_CC(Op, DAG); 4478 case ISD::SETCC: 4479 return lowerSETCC(Op, DAG); 4480 case ISD::GlobalAddress: 4481 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG); 4482 case ISD::GlobalTLSAddress: 4483 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG); 4484 case ISD::BlockAddress: 4485 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG); 4486 case ISD::JumpTable: 4487 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG); 4488 case ISD::ConstantPool: 4489 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG); 4490 case ISD::BITCAST: 4491 return lowerBITCAST(Op, DAG); 4492 case ISD::VASTART: 4493 return lowerVASTART(Op, DAG); 4494 case ISD::VACOPY: 4495 return lowerVACOPY(Op, DAG); 4496 case ISD::DYNAMIC_STACKALLOC: 4497 return lowerDYNAMIC_STACKALLOC(Op, DAG); 4498 case ISD::GET_DYNAMIC_AREA_OFFSET: 4499 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG); 4500 case ISD::SMUL_LOHI: 4501 return lowerSMUL_LOHI(Op, DAG); 4502 case ISD::UMUL_LOHI: 4503 return lowerUMUL_LOHI(Op, DAG); 4504 case ISD::SDIVREM: 4505 return lowerSDIVREM(Op, DAG); 4506 case ISD::UDIVREM: 4507 return lowerUDIVREM(Op, DAG); 4508 case ISD::OR: 4509 return lowerOR(Op, DAG); 4510 case ISD::CTPOP: 4511 return lowerCTPOP(Op, DAG); 4512 case ISD::ATOMIC_FENCE: 4513 return lowerATOMIC_FENCE(Op, DAG); 4514 case ISD::ATOMIC_SWAP: 4515 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW); 4516 case ISD::ATOMIC_STORE: 4517 return lowerATOMIC_STORE(Op, DAG); 4518 case ISD::ATOMIC_LOAD: 4519 return lowerATOMIC_LOAD(Op, DAG); 4520 case ISD::ATOMIC_LOAD_ADD: 4521 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD); 4522 case ISD::ATOMIC_LOAD_SUB: 4523 return lowerATOMIC_LOAD_SUB(Op, DAG); 4524 case ISD::ATOMIC_LOAD_AND: 4525 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND); 4526 case ISD::ATOMIC_LOAD_OR: 4527 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR); 4528 case ISD::ATOMIC_LOAD_XOR: 4529 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR); 4530 case ISD::ATOMIC_LOAD_NAND: 4531 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND); 4532 case ISD::ATOMIC_LOAD_MIN: 4533 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN); 4534 case ISD::ATOMIC_LOAD_MAX: 4535 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX); 4536 case ISD::ATOMIC_LOAD_UMIN: 4537 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN); 4538 case ISD::ATOMIC_LOAD_UMAX: 4539 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX); 4540 case ISD::ATOMIC_CMP_SWAP: 4541 return lowerATOMIC_CMP_SWAP(Op, DAG); 4542 case ISD::STACKSAVE: 4543 return lowerSTACKSAVE(Op, DAG); 4544 case ISD::STACKRESTORE: 4545 return lowerSTACKRESTORE(Op, DAG); 4546 case ISD::PREFETCH: 4547 return lowerPREFETCH(Op, DAG); 4548 case ISD::INTRINSIC_W_CHAIN: 4549 return lowerINTRINSIC_W_CHAIN(Op, DAG); 4550 case ISD::INTRINSIC_WO_CHAIN: 4551 return lowerINTRINSIC_WO_CHAIN(Op, DAG); 4552 case ISD::BUILD_VECTOR: 4553 return lowerBUILD_VECTOR(Op, DAG); 4554 case ISD::VECTOR_SHUFFLE: 4555 return lowerVECTOR_SHUFFLE(Op, DAG); 4556 case ISD::SCALAR_TO_VECTOR: 4557 return lowerSCALAR_TO_VECTOR(Op, DAG); 4558 case ISD::INSERT_VECTOR_ELT: 4559 return lowerINSERT_VECTOR_ELT(Op, DAG); 4560 case ISD::EXTRACT_VECTOR_ELT: 4561 return lowerEXTRACT_VECTOR_ELT(Op, DAG); 4562 case ISD::SIGN_EXTEND_VECTOR_INREG: 4563 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH); 4564 case ISD::ZERO_EXTEND_VECTOR_INREG: 4565 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH); 4566 case ISD::SHL: 4567 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR); 4568 case ISD::SRL: 4569 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR); 4570 case ISD::SRA: 4571 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR); 4572 default: 4573 llvm_unreachable("Unexpected node to lower"); 4574 } 4575 } 4576 4577 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const { 4578 #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME 4579 switch ((SystemZISD::NodeType)Opcode) { 4580 case SystemZISD::FIRST_NUMBER: break; 4581 OPCODE(RET_FLAG); 4582 OPCODE(CALL); 4583 OPCODE(SIBCALL); 4584 OPCODE(TLS_GDCALL); 4585 OPCODE(TLS_LDCALL); 4586 OPCODE(PCREL_WRAPPER); 4587 OPCODE(PCREL_OFFSET); 4588 OPCODE(IABS); 4589 OPCODE(ICMP); 4590 OPCODE(FCMP); 4591 OPCODE(TM); 4592 OPCODE(BR_CCMASK); 4593 OPCODE(SELECT_CCMASK); 4594 OPCODE(ADJDYNALLOC); 4595 OPCODE(EXTRACT_ACCESS); 4596 OPCODE(POPCNT); 4597 OPCODE(UMUL_LOHI64); 4598 OPCODE(SDIVREM32); 4599 OPCODE(SDIVREM64); 4600 OPCODE(UDIVREM32); 4601 OPCODE(UDIVREM64); 4602 OPCODE(MVC); 4603 OPCODE(MVC_LOOP); 4604 OPCODE(NC); 4605 OPCODE(NC_LOOP); 4606 OPCODE(OC); 4607 OPCODE(OC_LOOP); 4608 OPCODE(XC); 4609 OPCODE(XC_LOOP); 4610 OPCODE(CLC); 4611 OPCODE(CLC_LOOP); 4612 OPCODE(STPCPY); 4613 OPCODE(STRCMP); 4614 OPCODE(SEARCH_STRING); 4615 OPCODE(IPM); 4616 OPCODE(SERIALIZE); 4617 OPCODE(MEMBARRIER); 4618 OPCODE(TBEGIN); 4619 OPCODE(TBEGIN_NOFLOAT); 4620 OPCODE(TEND); 4621 OPCODE(BYTE_MASK); 4622 OPCODE(ROTATE_MASK); 4623 OPCODE(REPLICATE); 4624 OPCODE(JOIN_DWORDS); 4625 OPCODE(SPLAT); 4626 OPCODE(MERGE_HIGH); 4627 OPCODE(MERGE_LOW); 4628 OPCODE(SHL_DOUBLE); 4629 OPCODE(PERMUTE_DWORDS); 4630 OPCODE(PERMUTE); 4631 OPCODE(PACK); 4632 OPCODE(PACKS_CC); 4633 OPCODE(PACKLS_CC); 4634 OPCODE(UNPACK_HIGH); 4635 OPCODE(UNPACKL_HIGH); 4636 OPCODE(UNPACK_LOW); 4637 OPCODE(UNPACKL_LOW); 4638 OPCODE(VSHL_BY_SCALAR); 4639 OPCODE(VSRL_BY_SCALAR); 4640 OPCODE(VSRA_BY_SCALAR); 4641 OPCODE(VSUM); 4642 OPCODE(VICMPE); 4643 OPCODE(VICMPH); 4644 OPCODE(VICMPHL); 4645 OPCODE(VICMPES); 4646 OPCODE(VICMPHS); 4647 OPCODE(VICMPHLS); 4648 OPCODE(VFCMPE); 4649 OPCODE(VFCMPH); 4650 OPCODE(VFCMPHE); 4651 OPCODE(VFCMPES); 4652 OPCODE(VFCMPHS); 4653 OPCODE(VFCMPHES); 4654 OPCODE(VFTCI); 4655 OPCODE(VEXTEND); 4656 OPCODE(VROUND); 4657 OPCODE(VTM); 4658 OPCODE(VFAE_CC); 4659 OPCODE(VFAEZ_CC); 4660 OPCODE(VFEE_CC); 4661 OPCODE(VFEEZ_CC); 4662 OPCODE(VFENE_CC); 4663 OPCODE(VFENEZ_CC); 4664 OPCODE(VISTR_CC); 4665 OPCODE(VSTRC_CC); 4666 OPCODE(VSTRCZ_CC); 4667 OPCODE(ATOMIC_SWAPW); 4668 OPCODE(ATOMIC_LOADW_ADD); 4669 OPCODE(ATOMIC_LOADW_SUB); 4670 OPCODE(ATOMIC_LOADW_AND); 4671 OPCODE(ATOMIC_LOADW_OR); 4672 OPCODE(ATOMIC_LOADW_XOR); 4673 OPCODE(ATOMIC_LOADW_NAND); 4674 OPCODE(ATOMIC_LOADW_MIN); 4675 OPCODE(ATOMIC_LOADW_MAX); 4676 OPCODE(ATOMIC_LOADW_UMIN); 4677 OPCODE(ATOMIC_LOADW_UMAX); 4678 OPCODE(ATOMIC_CMP_SWAPW); 4679 OPCODE(PREFETCH); 4680 } 4681 return nullptr; 4682 #undef OPCODE 4683 } 4684 4685 // Return true if VT is a vector whose elements are a whole number of bytes 4686 // in width. 4687 static bool canTreatAsByteVector(EVT VT) { 4688 return VT.isVector() && VT.getVectorElementType().getSizeInBits() % 8 == 0; 4689 } 4690 4691 // Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT 4692 // producing a result of type ResVT. Op is a possibly bitcast version 4693 // of the input vector and Index is the index (based on type VecVT) that 4694 // should be extracted. Return the new extraction if a simplification 4695 // was possible or if Force is true. 4696 SDValue SystemZTargetLowering::combineExtract(SDLoc DL, EVT ResVT, EVT VecVT, 4697 SDValue Op, unsigned Index, 4698 DAGCombinerInfo &DCI, 4699 bool Force) const { 4700 SelectionDAG &DAG = DCI.DAG; 4701 4702 // The number of bytes being extracted. 4703 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize(); 4704 4705 for (;;) { 4706 unsigned Opcode = Op.getOpcode(); 4707 if (Opcode == ISD::BITCAST) 4708 // Look through bitcasts. 4709 Op = Op.getOperand(0); 4710 else if (Opcode == ISD::VECTOR_SHUFFLE && 4711 canTreatAsByteVector(Op.getValueType())) { 4712 // Get a VPERM-like permute mask and see whether the bytes covered 4713 // by the extracted element are a contiguous sequence from one 4714 // source operand. 4715 SmallVector<int, SystemZ::VectorBytes> Bytes; 4716 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes); 4717 int First; 4718 if (!getShuffleInput(Bytes, Index * BytesPerElement, 4719 BytesPerElement, First)) 4720 break; 4721 if (First < 0) 4722 return DAG.getUNDEF(ResVT); 4723 // Make sure the contiguous sequence starts at a multiple of the 4724 // original element size. 4725 unsigned Byte = unsigned(First) % Bytes.size(); 4726 if (Byte % BytesPerElement != 0) 4727 break; 4728 // We can get the extracted value directly from an input. 4729 Index = Byte / BytesPerElement; 4730 Op = Op.getOperand(unsigned(First) / Bytes.size()); 4731 Force = true; 4732 } else if (Opcode == ISD::BUILD_VECTOR && 4733 canTreatAsByteVector(Op.getValueType())) { 4734 // We can only optimize this case if the BUILD_VECTOR elements are 4735 // at least as wide as the extracted value. 4736 EVT OpVT = Op.getValueType(); 4737 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize(); 4738 if (OpBytesPerElement < BytesPerElement) 4739 break; 4740 // Make sure that the least-significant bit of the extracted value 4741 // is the least significant bit of an input. 4742 unsigned End = (Index + 1) * BytesPerElement; 4743 if (End % OpBytesPerElement != 0) 4744 break; 4745 // We're extracting the low part of one operand of the BUILD_VECTOR. 4746 Op = Op.getOperand(End / OpBytesPerElement - 1); 4747 if (!Op.getValueType().isInteger()) { 4748 EVT VT = MVT::getIntegerVT(Op.getValueType().getSizeInBits()); 4749 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op); 4750 DCI.AddToWorklist(Op.getNode()); 4751 } 4752 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits()); 4753 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op); 4754 if (VT != ResVT) { 4755 DCI.AddToWorklist(Op.getNode()); 4756 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op); 4757 } 4758 return Op; 4759 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG || 4760 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG || 4761 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) && 4762 canTreatAsByteVector(Op.getValueType()) && 4763 canTreatAsByteVector(Op.getOperand(0).getValueType())) { 4764 // Make sure that only the unextended bits are significant. 4765 EVT ExtVT = Op.getValueType(); 4766 EVT OpVT = Op.getOperand(0).getValueType(); 4767 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize(); 4768 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize(); 4769 unsigned Byte = Index * BytesPerElement; 4770 unsigned SubByte = Byte % ExtBytesPerElement; 4771 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement; 4772 if (SubByte < MinSubByte || 4773 SubByte + BytesPerElement > ExtBytesPerElement) 4774 break; 4775 // Get the byte offset of the unextended element 4776 Byte = Byte / ExtBytesPerElement * OpBytesPerElement; 4777 // ...then add the byte offset relative to that element. 4778 Byte += SubByte - MinSubByte; 4779 if (Byte % BytesPerElement != 0) 4780 break; 4781 Op = Op.getOperand(0); 4782 Index = Byte / BytesPerElement; 4783 Force = true; 4784 } else 4785 break; 4786 } 4787 if (Force) { 4788 if (Op.getValueType() != VecVT) { 4789 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op); 4790 DCI.AddToWorklist(Op.getNode()); 4791 } 4792 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op, 4793 DAG.getConstant(Index, DL, MVT::i32)); 4794 } 4795 return SDValue(); 4796 } 4797 4798 // Optimize vector operations in scalar value Op on the basis that Op 4799 // is truncated to TruncVT. 4800 SDValue 4801 SystemZTargetLowering::combineTruncateExtract(SDLoc DL, EVT TruncVT, SDValue Op, 4802 DAGCombinerInfo &DCI) const { 4803 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into 4804 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements 4805 // of type TruncVT. 4806 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 4807 TruncVT.getSizeInBits() % 8 == 0) { 4808 SDValue Vec = Op.getOperand(0); 4809 EVT VecVT = Vec.getValueType(); 4810 if (canTreatAsByteVector(VecVT)) { 4811 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 4812 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize(); 4813 unsigned TruncBytes = TruncVT.getStoreSize(); 4814 if (BytesPerElement % TruncBytes == 0) { 4815 // Calculate the value of Y' in the above description. We are 4816 // splitting the original elements into Scale equal-sized pieces 4817 // and for truncation purposes want the last (least-significant) 4818 // of these pieces for IndexN. This is easiest to do by calculating 4819 // the start index of the following element and then subtracting 1. 4820 unsigned Scale = BytesPerElement / TruncBytes; 4821 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1; 4822 4823 // Defer the creation of the bitcast from X to combineExtract, 4824 // which might be able to optimize the extraction. 4825 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8), 4826 VecVT.getStoreSize() / TruncBytes); 4827 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT); 4828 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true); 4829 } 4830 } 4831 } 4832 } 4833 return SDValue(); 4834 } 4835 4836 SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N, 4837 DAGCombinerInfo &DCI) const { 4838 SelectionDAG &DAG = DCI.DAG; 4839 unsigned Opcode = N->getOpcode(); 4840 if (Opcode == ISD::SIGN_EXTEND) { 4841 // Convert (sext (ashr (shl X, C1), C2)) to 4842 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as 4843 // cheap as narrower ones. 4844 SDValue N0 = N->getOperand(0); 4845 EVT VT = N->getValueType(0); 4846 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) { 4847 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 4848 SDValue Inner = N0.getOperand(0); 4849 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) { 4850 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) { 4851 unsigned Extra = (VT.getSizeInBits() - 4852 N0.getValueType().getSizeInBits()); 4853 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra; 4854 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra; 4855 EVT ShiftVT = N0.getOperand(1).getValueType(); 4856 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT, 4857 Inner.getOperand(0)); 4858 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext, 4859 DAG.getConstant(NewShlAmt, SDLoc(Inner), 4860 ShiftVT)); 4861 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, 4862 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT)); 4863 } 4864 } 4865 } 4866 } 4867 if (Opcode == SystemZISD::MERGE_HIGH || 4868 Opcode == SystemZISD::MERGE_LOW) { 4869 SDValue Op0 = N->getOperand(0); 4870 SDValue Op1 = N->getOperand(1); 4871 if (Op0.getOpcode() == ISD::BITCAST) 4872 Op0 = Op0.getOperand(0); 4873 if (Op0.getOpcode() == SystemZISD::BYTE_MASK && 4874 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) { 4875 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF 4876 // for v4f32. 4877 if (Op1 == N->getOperand(0)) 4878 return Op1; 4879 // (z_merge_? 0, X) -> (z_unpackl_? 0, X). 4880 EVT VT = Op1.getValueType(); 4881 unsigned ElemBytes = VT.getVectorElementType().getStoreSize(); 4882 if (ElemBytes <= 4) { 4883 Opcode = (Opcode == SystemZISD::MERGE_HIGH ? 4884 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW); 4885 EVT InVT = VT.changeVectorElementTypeToInteger(); 4886 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16), 4887 SystemZ::VectorBytes / ElemBytes / 2); 4888 if (VT != InVT) { 4889 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1); 4890 DCI.AddToWorklist(Op1.getNode()); 4891 } 4892 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1); 4893 DCI.AddToWorklist(Op.getNode()); 4894 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); 4895 } 4896 } 4897 } 4898 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better 4899 // for the extraction to be done on a vMiN value, so that we can use VSTE. 4900 // If X has wider elements then convert it to: 4901 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z). 4902 if (Opcode == ISD::STORE) { 4903 auto *SN = cast<StoreSDNode>(N); 4904 EVT MemVT = SN->getMemoryVT(); 4905 if (MemVT.isInteger()) { 4906 if (SDValue Value = 4907 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) { 4908 DCI.AddToWorklist(Value.getNode()); 4909 4910 // Rewrite the store with the new form of stored value. 4911 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value, 4912 SN->getBasePtr(), SN->getMemoryVT(), 4913 SN->getMemOperand()); 4914 } 4915 } 4916 } 4917 // Try to simplify a vector extraction. 4918 if (Opcode == ISD::EXTRACT_VECTOR_ELT) { 4919 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) { 4920 SDValue Op0 = N->getOperand(0); 4921 EVT VecVT = Op0.getValueType(); 4922 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0, 4923 IndexN->getZExtValue(), DCI, false); 4924 } 4925 } 4926 // (join_dwords X, X) == (replicate X) 4927 if (Opcode == SystemZISD::JOIN_DWORDS && 4928 N->getOperand(0) == N->getOperand(1)) 4929 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0), 4930 N->getOperand(0)); 4931 // (fround (extract_vector_elt X 0)) 4932 // (fround (extract_vector_elt X 1)) -> 4933 // (extract_vector_elt (VROUND X) 0) 4934 // (extract_vector_elt (VROUND X) 1) 4935 // 4936 // This is a special case since the target doesn't really support v2f32s. 4937 if (Opcode == ISD::FP_ROUND) { 4938 SDValue Op0 = N->getOperand(0); 4939 if (N->getValueType(0) == MVT::f32 && 4940 Op0.hasOneUse() && 4941 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 4942 Op0.getOperand(0).getValueType() == MVT::v2f64 && 4943 Op0.getOperand(1).getOpcode() == ISD::Constant && 4944 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) { 4945 SDValue Vec = Op0.getOperand(0); 4946 for (auto *U : Vec->uses()) { 4947 if (U != Op0.getNode() && 4948 U->hasOneUse() && 4949 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 4950 U->getOperand(0) == Vec && 4951 U->getOperand(1).getOpcode() == ISD::Constant && 4952 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) { 4953 SDValue OtherRound = SDValue(*U->use_begin(), 0); 4954 if (OtherRound.getOpcode() == ISD::FP_ROUND && 4955 OtherRound.getOperand(0) == SDValue(U, 0) && 4956 OtherRound.getValueType() == MVT::f32) { 4957 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N), 4958 MVT::v4f32, Vec); 4959 DCI.AddToWorklist(VRound.getNode()); 4960 SDValue Extract1 = 4961 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32, 4962 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32)); 4963 DCI.AddToWorklist(Extract1.getNode()); 4964 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1); 4965 SDValue Extract0 = 4966 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32, 4967 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32)); 4968 return Extract0; 4969 } 4970 } 4971 } 4972 } 4973 } 4974 return SDValue(); 4975 } 4976 4977 //===----------------------------------------------------------------------===// 4978 // Custom insertion 4979 //===----------------------------------------------------------------------===// 4980 4981 // Create a new basic block after MBB. 4982 static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) { 4983 MachineFunction &MF = *MBB->getParent(); 4984 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock()); 4985 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB); 4986 return NewMBB; 4987 } 4988 4989 // Split MBB after MI and return the new block (the one that contains 4990 // instructions after MI). 4991 static MachineBasicBlock *splitBlockAfter(MachineInstr *MI, 4992 MachineBasicBlock *MBB) { 4993 MachineBasicBlock *NewMBB = emitBlockAfter(MBB); 4994 NewMBB->splice(NewMBB->begin(), MBB, 4995 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 4996 NewMBB->transferSuccessorsAndUpdatePHIs(MBB); 4997 return NewMBB; 4998 } 4999 5000 // Split MBB before MI and return the new block (the one that contains MI). 5001 static MachineBasicBlock *splitBlockBefore(MachineInstr *MI, 5002 MachineBasicBlock *MBB) { 5003 MachineBasicBlock *NewMBB = emitBlockAfter(MBB); 5004 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end()); 5005 NewMBB->transferSuccessorsAndUpdatePHIs(MBB); 5006 return NewMBB; 5007 } 5008 5009 // Force base value Base into a register before MI. Return the register. 5010 static unsigned forceReg(MachineInstr *MI, MachineOperand &Base, 5011 const SystemZInstrInfo *TII) { 5012 if (Base.isReg()) 5013 return Base.getReg(); 5014 5015 MachineBasicBlock *MBB = MI->getParent(); 5016 MachineFunction &MF = *MBB->getParent(); 5017 MachineRegisterInfo &MRI = MF.getRegInfo(); 5018 5019 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 5020 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg) 5021 .addOperand(Base).addImm(0).addReg(0); 5022 return Reg; 5023 } 5024 5025 // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI. 5026 MachineBasicBlock * 5027 SystemZTargetLowering::emitSelect(MachineInstr *MI, 5028 MachineBasicBlock *MBB) const { 5029 const SystemZInstrInfo *TII = 5030 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5031 5032 unsigned DestReg = MI->getOperand(0).getReg(); 5033 unsigned TrueReg = MI->getOperand(1).getReg(); 5034 unsigned FalseReg = MI->getOperand(2).getReg(); 5035 unsigned CCValid = MI->getOperand(3).getImm(); 5036 unsigned CCMask = MI->getOperand(4).getImm(); 5037 DebugLoc DL = MI->getDebugLoc(); 5038 5039 MachineBasicBlock *StartMBB = MBB; 5040 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB); 5041 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB); 5042 5043 // StartMBB: 5044 // BRC CCMask, JoinMBB 5045 // # fallthrough to FalseMBB 5046 MBB = StartMBB; 5047 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5048 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); 5049 MBB->addSuccessor(JoinMBB); 5050 MBB->addSuccessor(FalseMBB); 5051 5052 // FalseMBB: 5053 // # fallthrough to JoinMBB 5054 MBB = FalseMBB; 5055 MBB->addSuccessor(JoinMBB); 5056 5057 // JoinMBB: 5058 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ] 5059 // ... 5060 MBB = JoinMBB; 5061 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg) 5062 .addReg(TrueReg).addMBB(StartMBB) 5063 .addReg(FalseReg).addMBB(FalseMBB); 5064 5065 MI->eraseFromParent(); 5066 return JoinMBB; 5067 } 5068 5069 // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI. 5070 // StoreOpcode is the store to use and Invert says whether the store should 5071 // happen when the condition is false rather than true. If a STORE ON 5072 // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0. 5073 MachineBasicBlock * 5074 SystemZTargetLowering::emitCondStore(MachineInstr *MI, 5075 MachineBasicBlock *MBB, 5076 unsigned StoreOpcode, unsigned STOCOpcode, 5077 bool Invert) const { 5078 const SystemZInstrInfo *TII = 5079 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5080 5081 unsigned SrcReg = MI->getOperand(0).getReg(); 5082 MachineOperand Base = MI->getOperand(1); 5083 int64_t Disp = MI->getOperand(2).getImm(); 5084 unsigned IndexReg = MI->getOperand(3).getReg(); 5085 unsigned CCValid = MI->getOperand(4).getImm(); 5086 unsigned CCMask = MI->getOperand(5).getImm(); 5087 DebugLoc DL = MI->getDebugLoc(); 5088 5089 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp); 5090 5091 // Use STOCOpcode if possible. We could use different store patterns in 5092 // order to avoid matching the index register, but the performance trade-offs 5093 // might be more complicated in that case. 5094 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) { 5095 if (Invert) 5096 CCMask ^= CCValid; 5097 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode)) 5098 .addReg(SrcReg).addOperand(Base).addImm(Disp) 5099 .addImm(CCValid).addImm(CCMask); 5100 MI->eraseFromParent(); 5101 return MBB; 5102 } 5103 5104 // Get the condition needed to branch around the store. 5105 if (!Invert) 5106 CCMask ^= CCValid; 5107 5108 MachineBasicBlock *StartMBB = MBB; 5109 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB); 5110 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB); 5111 5112 // StartMBB: 5113 // BRC CCMask, JoinMBB 5114 // # fallthrough to FalseMBB 5115 MBB = StartMBB; 5116 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5117 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); 5118 MBB->addSuccessor(JoinMBB); 5119 MBB->addSuccessor(FalseMBB); 5120 5121 // FalseMBB: 5122 // store %SrcReg, %Disp(%Index,%Base) 5123 // # fallthrough to JoinMBB 5124 MBB = FalseMBB; 5125 BuildMI(MBB, DL, TII->get(StoreOpcode)) 5126 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg); 5127 MBB->addSuccessor(JoinMBB); 5128 5129 MI->eraseFromParent(); 5130 return JoinMBB; 5131 } 5132 5133 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_* 5134 // or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that 5135 // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}. 5136 // BitSize is the width of the field in bits, or 0 if this is a partword 5137 // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize 5138 // is one of the operands. Invert says whether the field should be 5139 // inverted after performing BinOpcode (e.g. for NAND). 5140 MachineBasicBlock * 5141 SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI, 5142 MachineBasicBlock *MBB, 5143 unsigned BinOpcode, 5144 unsigned BitSize, 5145 bool Invert) const { 5146 MachineFunction &MF = *MBB->getParent(); 5147 const SystemZInstrInfo *TII = 5148 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5149 MachineRegisterInfo &MRI = MF.getRegInfo(); 5150 bool IsSubWord = (BitSize < 32); 5151 5152 // Extract the operands. Base can be a register or a frame index. 5153 // Src2 can be a register or immediate. 5154 unsigned Dest = MI->getOperand(0).getReg(); 5155 MachineOperand Base = earlyUseOperand(MI->getOperand(1)); 5156 int64_t Disp = MI->getOperand(2).getImm(); 5157 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3)); 5158 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0); 5159 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0); 5160 DebugLoc DL = MI->getDebugLoc(); 5161 if (IsSubWord) 5162 BitSize = MI->getOperand(6).getImm(); 5163 5164 // Subword operations use 32-bit registers. 5165 const TargetRegisterClass *RC = (BitSize <= 32 ? 5166 &SystemZ::GR32BitRegClass : 5167 &SystemZ::GR64BitRegClass); 5168 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; 5169 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; 5170 5171 // Get the right opcodes for the displacement. 5172 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); 5173 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); 5174 assert(LOpcode && CSOpcode && "Displacement out of range"); 5175 5176 // Create virtual registers for temporary results. 5177 unsigned OrigVal = MRI.createVirtualRegister(RC); 5178 unsigned OldVal = MRI.createVirtualRegister(RC); 5179 unsigned NewVal = (BinOpcode || IsSubWord ? 5180 MRI.createVirtualRegister(RC) : Src2.getReg()); 5181 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); 5182 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); 5183 5184 // Insert a basic block for the main loop. 5185 MachineBasicBlock *StartMBB = MBB; 5186 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 5187 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 5188 5189 // StartMBB: 5190 // ... 5191 // %OrigVal = L Disp(%Base) 5192 // # fall through to LoopMMB 5193 MBB = StartMBB; 5194 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal) 5195 .addOperand(Base).addImm(Disp).addReg(0); 5196 MBB->addSuccessor(LoopMBB); 5197 5198 // LoopMBB: 5199 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ] 5200 // %RotatedOldVal = RLL %OldVal, 0(%BitShift) 5201 // %RotatedNewVal = OP %RotatedOldVal, %Src2 5202 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) 5203 // %Dest = CS %OldVal, %NewVal, Disp(%Base) 5204 // JNE LoopMBB 5205 // # fall through to DoneMMB 5206 MBB = LoopMBB; 5207 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 5208 .addReg(OrigVal).addMBB(StartMBB) 5209 .addReg(Dest).addMBB(LoopMBB); 5210 if (IsSubWord) 5211 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) 5212 .addReg(OldVal).addReg(BitShift).addImm(0); 5213 if (Invert) { 5214 // Perform the operation normally and then invert every bit of the field. 5215 unsigned Tmp = MRI.createVirtualRegister(RC); 5216 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp) 5217 .addReg(RotatedOldVal).addOperand(Src2); 5218 if (BitSize <= 32) 5219 // XILF with the upper BitSize bits set. 5220 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal) 5221 .addReg(Tmp).addImm(-1U << (32 - BitSize)); 5222 else { 5223 // Use LCGR and add -1 to the result, which is more compact than 5224 // an XILF, XILH pair. 5225 unsigned Tmp2 = MRI.createVirtualRegister(RC); 5226 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp); 5227 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal) 5228 .addReg(Tmp2).addImm(-1); 5229 } 5230 } else if (BinOpcode) 5231 // A simply binary operation. 5232 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal) 5233 .addReg(RotatedOldVal).addOperand(Src2); 5234 else if (IsSubWord) 5235 // Use RISBG to rotate Src2 into position and use it to replace the 5236 // field in RotatedOldVal. 5237 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal) 5238 .addReg(RotatedOldVal).addReg(Src2.getReg()) 5239 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize); 5240 if (IsSubWord) 5241 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) 5242 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); 5243 BuildMI(MBB, DL, TII->get(CSOpcode), Dest) 5244 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp); 5245 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5246 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 5247 MBB->addSuccessor(LoopMBB); 5248 MBB->addSuccessor(DoneMBB); 5249 5250 MI->eraseFromParent(); 5251 return DoneMBB; 5252 } 5253 5254 // Implement EmitInstrWithCustomInserter for pseudo 5255 // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the 5256 // instruction that should be used to compare the current field with the 5257 // minimum or maximum value. KeepOldMask is the BRC condition-code mask 5258 // for when the current field should be kept. BitSize is the width of 5259 // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction. 5260 MachineBasicBlock * 5261 SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI, 5262 MachineBasicBlock *MBB, 5263 unsigned CompareOpcode, 5264 unsigned KeepOldMask, 5265 unsigned BitSize) const { 5266 MachineFunction &MF = *MBB->getParent(); 5267 const SystemZInstrInfo *TII = 5268 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5269 MachineRegisterInfo &MRI = MF.getRegInfo(); 5270 bool IsSubWord = (BitSize < 32); 5271 5272 // Extract the operands. Base can be a register or a frame index. 5273 unsigned Dest = MI->getOperand(0).getReg(); 5274 MachineOperand Base = earlyUseOperand(MI->getOperand(1)); 5275 int64_t Disp = MI->getOperand(2).getImm(); 5276 unsigned Src2 = MI->getOperand(3).getReg(); 5277 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0); 5278 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0); 5279 DebugLoc DL = MI->getDebugLoc(); 5280 if (IsSubWord) 5281 BitSize = MI->getOperand(6).getImm(); 5282 5283 // Subword operations use 32-bit registers. 5284 const TargetRegisterClass *RC = (BitSize <= 32 ? 5285 &SystemZ::GR32BitRegClass : 5286 &SystemZ::GR64BitRegClass); 5287 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; 5288 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; 5289 5290 // Get the right opcodes for the displacement. 5291 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); 5292 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); 5293 assert(LOpcode && CSOpcode && "Displacement out of range"); 5294 5295 // Create virtual registers for temporary results. 5296 unsigned OrigVal = MRI.createVirtualRegister(RC); 5297 unsigned OldVal = MRI.createVirtualRegister(RC); 5298 unsigned NewVal = MRI.createVirtualRegister(RC); 5299 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); 5300 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2); 5301 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); 5302 5303 // Insert 3 basic blocks for the loop. 5304 MachineBasicBlock *StartMBB = MBB; 5305 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 5306 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 5307 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB); 5308 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB); 5309 5310 // StartMBB: 5311 // ... 5312 // %OrigVal = L Disp(%Base) 5313 // # fall through to LoopMMB 5314 MBB = StartMBB; 5315 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal) 5316 .addOperand(Base).addImm(Disp).addReg(0); 5317 MBB->addSuccessor(LoopMBB); 5318 5319 // LoopMBB: 5320 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ] 5321 // %RotatedOldVal = RLL %OldVal, 0(%BitShift) 5322 // CompareOpcode %RotatedOldVal, %Src2 5323 // BRC KeepOldMask, UpdateMBB 5324 MBB = LoopMBB; 5325 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 5326 .addReg(OrigVal).addMBB(StartMBB) 5327 .addReg(Dest).addMBB(UpdateMBB); 5328 if (IsSubWord) 5329 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) 5330 .addReg(OldVal).addReg(BitShift).addImm(0); 5331 BuildMI(MBB, DL, TII->get(CompareOpcode)) 5332 .addReg(RotatedOldVal).addReg(Src2); 5333 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5334 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB); 5335 MBB->addSuccessor(UpdateMBB); 5336 MBB->addSuccessor(UseAltMBB); 5337 5338 // UseAltMBB: 5339 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0 5340 // # fall through to UpdateMMB 5341 MBB = UseAltMBB; 5342 if (IsSubWord) 5343 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal) 5344 .addReg(RotatedOldVal).addReg(Src2) 5345 .addImm(32).addImm(31 + BitSize).addImm(0); 5346 MBB->addSuccessor(UpdateMBB); 5347 5348 // UpdateMBB: 5349 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ], 5350 // [ %RotatedAltVal, UseAltMBB ] 5351 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) 5352 // %Dest = CS %OldVal, %NewVal, Disp(%Base) 5353 // JNE LoopMBB 5354 // # fall through to DoneMMB 5355 MBB = UpdateMBB; 5356 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal) 5357 .addReg(RotatedOldVal).addMBB(LoopMBB) 5358 .addReg(RotatedAltVal).addMBB(UseAltMBB); 5359 if (IsSubWord) 5360 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) 5361 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); 5362 BuildMI(MBB, DL, TII->get(CSOpcode), Dest) 5363 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp); 5364 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5365 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 5366 MBB->addSuccessor(LoopMBB); 5367 MBB->addSuccessor(DoneMBB); 5368 5369 MI->eraseFromParent(); 5370 return DoneMBB; 5371 } 5372 5373 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW 5374 // instruction MI. 5375 MachineBasicBlock * 5376 SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI, 5377 MachineBasicBlock *MBB) const { 5378 5379 MachineFunction &MF = *MBB->getParent(); 5380 const SystemZInstrInfo *TII = 5381 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5382 MachineRegisterInfo &MRI = MF.getRegInfo(); 5383 5384 // Extract the operands. Base can be a register or a frame index. 5385 unsigned Dest = MI->getOperand(0).getReg(); 5386 MachineOperand Base = earlyUseOperand(MI->getOperand(1)); 5387 int64_t Disp = MI->getOperand(2).getImm(); 5388 unsigned OrigCmpVal = MI->getOperand(3).getReg(); 5389 unsigned OrigSwapVal = MI->getOperand(4).getReg(); 5390 unsigned BitShift = MI->getOperand(5).getReg(); 5391 unsigned NegBitShift = MI->getOperand(6).getReg(); 5392 int64_t BitSize = MI->getOperand(7).getImm(); 5393 DebugLoc DL = MI->getDebugLoc(); 5394 5395 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass; 5396 5397 // Get the right opcodes for the displacement. 5398 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp); 5399 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp); 5400 assert(LOpcode && CSOpcode && "Displacement out of range"); 5401 5402 // Create virtual registers for temporary results. 5403 unsigned OrigOldVal = MRI.createVirtualRegister(RC); 5404 unsigned OldVal = MRI.createVirtualRegister(RC); 5405 unsigned CmpVal = MRI.createVirtualRegister(RC); 5406 unsigned SwapVal = MRI.createVirtualRegister(RC); 5407 unsigned StoreVal = MRI.createVirtualRegister(RC); 5408 unsigned RetryOldVal = MRI.createVirtualRegister(RC); 5409 unsigned RetryCmpVal = MRI.createVirtualRegister(RC); 5410 unsigned RetrySwapVal = MRI.createVirtualRegister(RC); 5411 5412 // Insert 2 basic blocks for the loop. 5413 MachineBasicBlock *StartMBB = MBB; 5414 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 5415 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 5416 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB); 5417 5418 // StartMBB: 5419 // ... 5420 // %OrigOldVal = L Disp(%Base) 5421 // # fall through to LoopMMB 5422 MBB = StartMBB; 5423 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal) 5424 .addOperand(Base).addImm(Disp).addReg(0); 5425 MBB->addSuccessor(LoopMBB); 5426 5427 // LoopMBB: 5428 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ] 5429 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ] 5430 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ] 5431 // %Dest = RLL %OldVal, BitSize(%BitShift) 5432 // ^^ The low BitSize bits contain the field 5433 // of interest. 5434 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0 5435 // ^^ Replace the upper 32-BitSize bits of the 5436 // comparison value with those that we loaded, 5437 // so that we can use a full word comparison. 5438 // CR %Dest, %RetryCmpVal 5439 // JNE DoneMBB 5440 // # Fall through to SetMBB 5441 MBB = LoopMBB; 5442 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 5443 .addReg(OrigOldVal).addMBB(StartMBB) 5444 .addReg(RetryOldVal).addMBB(SetMBB); 5445 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal) 5446 .addReg(OrigCmpVal).addMBB(StartMBB) 5447 .addReg(RetryCmpVal).addMBB(SetMBB); 5448 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal) 5449 .addReg(OrigSwapVal).addMBB(StartMBB) 5450 .addReg(RetrySwapVal).addMBB(SetMBB); 5451 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest) 5452 .addReg(OldVal).addReg(BitShift).addImm(BitSize); 5453 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal) 5454 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); 5455 BuildMI(MBB, DL, TII->get(SystemZ::CR)) 5456 .addReg(Dest).addReg(RetryCmpVal); 5457 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5458 .addImm(SystemZ::CCMASK_ICMP) 5459 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB); 5460 MBB->addSuccessor(DoneMBB); 5461 MBB->addSuccessor(SetMBB); 5462 5463 // SetMBB: 5464 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0 5465 // ^^ Replace the upper 32-BitSize bits of the new 5466 // value with those that we loaded. 5467 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift) 5468 // ^^ Rotate the new field to its proper position. 5469 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base) 5470 // JNE LoopMBB 5471 // # fall through to ExitMMB 5472 MBB = SetMBB; 5473 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal) 5474 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); 5475 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal) 5476 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize); 5477 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal) 5478 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp); 5479 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5480 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 5481 MBB->addSuccessor(LoopMBB); 5482 MBB->addSuccessor(DoneMBB); 5483 5484 MI->eraseFromParent(); 5485 return DoneMBB; 5486 } 5487 5488 // Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true 5489 // if the high register of the GR128 value must be cleared or false if 5490 // it's "don't care". SubReg is subreg_l32 when extending a GR32 5491 // and subreg_l64 when extending a GR64. 5492 MachineBasicBlock * 5493 SystemZTargetLowering::emitExt128(MachineInstr *MI, 5494 MachineBasicBlock *MBB, 5495 bool ClearEven, unsigned SubReg) 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 unsigned Dest = MI->getOperand(0).getReg(); 5503 unsigned Src = MI->getOperand(1).getReg(); 5504 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 5505 5506 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128); 5507 if (ClearEven) { 5508 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 5509 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass); 5510 5511 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64) 5512 .addImm(0); 5513 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128) 5514 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64); 5515 In128 = NewIn128; 5516 } 5517 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest) 5518 .addReg(In128).addReg(Src).addImm(SubReg); 5519 5520 MI->eraseFromParent(); 5521 return MBB; 5522 } 5523 5524 MachineBasicBlock * 5525 SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI, 5526 MachineBasicBlock *MBB, 5527 unsigned Opcode) const { 5528 MachineFunction &MF = *MBB->getParent(); 5529 const SystemZInstrInfo *TII = 5530 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5531 MachineRegisterInfo &MRI = MF.getRegInfo(); 5532 DebugLoc DL = MI->getDebugLoc(); 5533 5534 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0)); 5535 uint64_t DestDisp = MI->getOperand(1).getImm(); 5536 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2)); 5537 uint64_t SrcDisp = MI->getOperand(3).getImm(); 5538 uint64_t Length = MI->getOperand(4).getImm(); 5539 5540 // When generating more than one CLC, all but the last will need to 5541 // branch to the end when a difference is found. 5542 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ? 5543 splitBlockAfter(MI, MBB) : nullptr); 5544 5545 // Check for the loop form, in which operand 5 is the trip count. 5546 if (MI->getNumExplicitOperands() > 5) { 5547 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase); 5548 5549 uint64_t StartCountReg = MI->getOperand(5).getReg(); 5550 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII); 5551 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg : 5552 forceReg(MI, DestBase, TII)); 5553 5554 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass; 5555 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC); 5556 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg : 5557 MRI.createVirtualRegister(RC)); 5558 uint64_t NextSrcReg = MRI.createVirtualRegister(RC); 5559 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg : 5560 MRI.createVirtualRegister(RC)); 5561 5562 RC = &SystemZ::GR64BitRegClass; 5563 uint64_t ThisCountReg = MRI.createVirtualRegister(RC); 5564 uint64_t NextCountReg = MRI.createVirtualRegister(RC); 5565 5566 MachineBasicBlock *StartMBB = MBB; 5567 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 5568 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 5569 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB); 5570 5571 // StartMBB: 5572 // # fall through to LoopMMB 5573 MBB->addSuccessor(LoopMBB); 5574 5575 // LoopMBB: 5576 // %ThisDestReg = phi [ %StartDestReg, StartMBB ], 5577 // [ %NextDestReg, NextMBB ] 5578 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ], 5579 // [ %NextSrcReg, NextMBB ] 5580 // %ThisCountReg = phi [ %StartCountReg, StartMBB ], 5581 // [ %NextCountReg, NextMBB ] 5582 // ( PFD 2, 768+DestDisp(%ThisDestReg) ) 5583 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg) 5584 // ( JLH EndMBB ) 5585 // 5586 // The prefetch is used only for MVC. The JLH is used only for CLC. 5587 MBB = LoopMBB; 5588 5589 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg) 5590 .addReg(StartDestReg).addMBB(StartMBB) 5591 .addReg(NextDestReg).addMBB(NextMBB); 5592 if (!HaveSingleBase) 5593 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg) 5594 .addReg(StartSrcReg).addMBB(StartMBB) 5595 .addReg(NextSrcReg).addMBB(NextMBB); 5596 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg) 5597 .addReg(StartCountReg).addMBB(StartMBB) 5598 .addReg(NextCountReg).addMBB(NextMBB); 5599 if (Opcode == SystemZ::MVC) 5600 BuildMI(MBB, DL, TII->get(SystemZ::PFD)) 5601 .addImm(SystemZ::PFD_WRITE) 5602 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0); 5603 BuildMI(MBB, DL, TII->get(Opcode)) 5604 .addReg(ThisDestReg).addImm(DestDisp).addImm(256) 5605 .addReg(ThisSrcReg).addImm(SrcDisp); 5606 if (EndMBB) { 5607 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5608 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 5609 .addMBB(EndMBB); 5610 MBB->addSuccessor(EndMBB); 5611 MBB->addSuccessor(NextMBB); 5612 } 5613 5614 // NextMBB: 5615 // %NextDestReg = LA 256(%ThisDestReg) 5616 // %NextSrcReg = LA 256(%ThisSrcReg) 5617 // %NextCountReg = AGHI %ThisCountReg, -1 5618 // CGHI %NextCountReg, 0 5619 // JLH LoopMBB 5620 // # fall through to DoneMMB 5621 // 5622 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes. 5623 MBB = NextMBB; 5624 5625 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg) 5626 .addReg(ThisDestReg).addImm(256).addReg(0); 5627 if (!HaveSingleBase) 5628 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg) 5629 .addReg(ThisSrcReg).addImm(256).addReg(0); 5630 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg) 5631 .addReg(ThisCountReg).addImm(-1); 5632 BuildMI(MBB, DL, TII->get(SystemZ::CGHI)) 5633 .addReg(NextCountReg).addImm(0); 5634 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5635 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 5636 .addMBB(LoopMBB); 5637 MBB->addSuccessor(LoopMBB); 5638 MBB->addSuccessor(DoneMBB); 5639 5640 DestBase = MachineOperand::CreateReg(NextDestReg, false); 5641 SrcBase = MachineOperand::CreateReg(NextSrcReg, false); 5642 Length &= 255; 5643 MBB = DoneMBB; 5644 } 5645 // Handle any remaining bytes with straight-line code. 5646 while (Length > 0) { 5647 uint64_t ThisLength = std::min(Length, uint64_t(256)); 5648 // The previous iteration might have created out-of-range displacements. 5649 // Apply them using LAY if so. 5650 if (!isUInt<12>(DestDisp)) { 5651 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 5652 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg) 5653 .addOperand(DestBase).addImm(DestDisp).addReg(0); 5654 DestBase = MachineOperand::CreateReg(Reg, false); 5655 DestDisp = 0; 5656 } 5657 if (!isUInt<12>(SrcDisp)) { 5658 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 5659 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg) 5660 .addOperand(SrcBase).addImm(SrcDisp).addReg(0); 5661 SrcBase = MachineOperand::CreateReg(Reg, false); 5662 SrcDisp = 0; 5663 } 5664 BuildMI(*MBB, MI, DL, TII->get(Opcode)) 5665 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength) 5666 .addOperand(SrcBase).addImm(SrcDisp); 5667 DestDisp += ThisLength; 5668 SrcDisp += ThisLength; 5669 Length -= ThisLength; 5670 // If there's another CLC to go, branch to the end if a difference 5671 // was found. 5672 if (EndMBB && Length > 0) { 5673 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB); 5674 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5675 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 5676 .addMBB(EndMBB); 5677 MBB->addSuccessor(EndMBB); 5678 MBB->addSuccessor(NextMBB); 5679 MBB = NextMBB; 5680 } 5681 } 5682 if (EndMBB) { 5683 MBB->addSuccessor(EndMBB); 5684 MBB = EndMBB; 5685 MBB->addLiveIn(SystemZ::CC); 5686 } 5687 5688 MI->eraseFromParent(); 5689 return MBB; 5690 } 5691 5692 // Decompose string pseudo-instruction MI into a loop that continually performs 5693 // Opcode until CC != 3. 5694 MachineBasicBlock * 5695 SystemZTargetLowering::emitStringWrapper(MachineInstr *MI, 5696 MachineBasicBlock *MBB, 5697 unsigned Opcode) const { 5698 MachineFunction &MF = *MBB->getParent(); 5699 const SystemZInstrInfo *TII = 5700 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5701 MachineRegisterInfo &MRI = MF.getRegInfo(); 5702 DebugLoc DL = MI->getDebugLoc(); 5703 5704 uint64_t End1Reg = MI->getOperand(0).getReg(); 5705 uint64_t Start1Reg = MI->getOperand(1).getReg(); 5706 uint64_t Start2Reg = MI->getOperand(2).getReg(); 5707 uint64_t CharReg = MI->getOperand(3).getReg(); 5708 5709 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass; 5710 uint64_t This1Reg = MRI.createVirtualRegister(RC); 5711 uint64_t This2Reg = MRI.createVirtualRegister(RC); 5712 uint64_t End2Reg = MRI.createVirtualRegister(RC); 5713 5714 MachineBasicBlock *StartMBB = MBB; 5715 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 5716 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 5717 5718 // StartMBB: 5719 // # fall through to LoopMMB 5720 MBB->addSuccessor(LoopMBB); 5721 5722 // LoopMBB: 5723 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ] 5724 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ] 5725 // R0L = %CharReg 5726 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L 5727 // JO LoopMBB 5728 // # fall through to DoneMMB 5729 // 5730 // The load of R0L can be hoisted by post-RA LICM. 5731 MBB = LoopMBB; 5732 5733 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg) 5734 .addReg(Start1Reg).addMBB(StartMBB) 5735 .addReg(End1Reg).addMBB(LoopMBB); 5736 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg) 5737 .addReg(Start2Reg).addMBB(StartMBB) 5738 .addReg(End2Reg).addMBB(LoopMBB); 5739 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg); 5740 BuildMI(MBB, DL, TII->get(Opcode)) 5741 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define) 5742 .addReg(This1Reg).addReg(This2Reg); 5743 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 5744 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB); 5745 MBB->addSuccessor(LoopMBB); 5746 MBB->addSuccessor(DoneMBB); 5747 5748 DoneMBB->addLiveIn(SystemZ::CC); 5749 5750 MI->eraseFromParent(); 5751 return DoneMBB; 5752 } 5753 5754 // Update TBEGIN instruction with final opcode and register clobbers. 5755 MachineBasicBlock * 5756 SystemZTargetLowering::emitTransactionBegin(MachineInstr *MI, 5757 MachineBasicBlock *MBB, 5758 unsigned Opcode, 5759 bool NoFloat) const { 5760 MachineFunction &MF = *MBB->getParent(); 5761 const TargetFrameLowering *TFI = Subtarget.getFrameLowering(); 5762 const SystemZInstrInfo *TII = Subtarget.getInstrInfo(); 5763 5764 // Update opcode. 5765 MI->setDesc(TII->get(Opcode)); 5766 5767 // We cannot handle a TBEGIN that clobbers the stack or frame pointer. 5768 // Make sure to add the corresponding GRSM bits if they are missing. 5769 uint64_t Control = MI->getOperand(2).getImm(); 5770 static const unsigned GPRControlBit[16] = { 5771 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000, 5772 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100 5773 }; 5774 Control |= GPRControlBit[15]; 5775 if (TFI->hasFP(MF)) 5776 Control |= GPRControlBit[11]; 5777 MI->getOperand(2).setImm(Control); 5778 5779 // Add GPR clobbers. 5780 for (int I = 0; I < 16; I++) { 5781 if ((Control & GPRControlBit[I]) == 0) { 5782 unsigned Reg = SystemZMC::GR64Regs[I]; 5783 MI->addOperand(MachineOperand::CreateReg(Reg, true, true)); 5784 } 5785 } 5786 5787 // Add FPR/VR clobbers. 5788 if (!NoFloat && (Control & 4) != 0) { 5789 if (Subtarget.hasVector()) { 5790 for (int I = 0; I < 32; I++) { 5791 unsigned Reg = SystemZMC::VR128Regs[I]; 5792 MI->addOperand(MachineOperand::CreateReg(Reg, true, true)); 5793 } 5794 } else { 5795 for (int I = 0; I < 16; I++) { 5796 unsigned Reg = SystemZMC::FP64Regs[I]; 5797 MI->addOperand(MachineOperand::CreateReg(Reg, true, true)); 5798 } 5799 } 5800 } 5801 5802 return MBB; 5803 } 5804 5805 MachineBasicBlock * 5806 SystemZTargetLowering::emitLoadAndTestCmp0(MachineInstr *MI, 5807 MachineBasicBlock *MBB, 5808 unsigned Opcode) const { 5809 MachineFunction &MF = *MBB->getParent(); 5810 MachineRegisterInfo *MRI = &MF.getRegInfo(); 5811 const SystemZInstrInfo *TII = 5812 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 5813 DebugLoc DL = MI->getDebugLoc(); 5814 5815 unsigned SrcReg = MI->getOperand(0).getReg(); 5816 5817 // Create new virtual register of the same class as source. 5818 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg); 5819 unsigned DstReg = MRI->createVirtualRegister(RC); 5820 5821 // Replace pseudo with a normal load-and-test that models the def as 5822 // well. 5823 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg) 5824 .addReg(SrcReg); 5825 MI->eraseFromParent(); 5826 5827 return MBB; 5828 } 5829 5830 MachineBasicBlock *SystemZTargetLowering:: 5831 EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const { 5832 switch (MI->getOpcode()) { 5833 case SystemZ::Select32Mux: 5834 case SystemZ::Select32: 5835 case SystemZ::SelectF32: 5836 case SystemZ::Select64: 5837 case SystemZ::SelectF64: 5838 case SystemZ::SelectF128: 5839 return emitSelect(MI, MBB); 5840 5841 case SystemZ::CondStore8Mux: 5842 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false); 5843 case SystemZ::CondStore8MuxInv: 5844 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true); 5845 case SystemZ::CondStore16Mux: 5846 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false); 5847 case SystemZ::CondStore16MuxInv: 5848 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true); 5849 case SystemZ::CondStore8: 5850 return emitCondStore(MI, MBB, SystemZ::STC, 0, false); 5851 case SystemZ::CondStore8Inv: 5852 return emitCondStore(MI, MBB, SystemZ::STC, 0, true); 5853 case SystemZ::CondStore16: 5854 return emitCondStore(MI, MBB, SystemZ::STH, 0, false); 5855 case SystemZ::CondStore16Inv: 5856 return emitCondStore(MI, MBB, SystemZ::STH, 0, true); 5857 case SystemZ::CondStore32: 5858 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false); 5859 case SystemZ::CondStore32Inv: 5860 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true); 5861 case SystemZ::CondStore64: 5862 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false); 5863 case SystemZ::CondStore64Inv: 5864 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true); 5865 case SystemZ::CondStoreF32: 5866 return emitCondStore(MI, MBB, SystemZ::STE, 0, false); 5867 case SystemZ::CondStoreF32Inv: 5868 return emitCondStore(MI, MBB, SystemZ::STE, 0, true); 5869 case SystemZ::CondStoreF64: 5870 return emitCondStore(MI, MBB, SystemZ::STD, 0, false); 5871 case SystemZ::CondStoreF64Inv: 5872 return emitCondStore(MI, MBB, SystemZ::STD, 0, true); 5873 5874 case SystemZ::AEXT128_64: 5875 return emitExt128(MI, MBB, false, SystemZ::subreg_l64); 5876 case SystemZ::ZEXT128_32: 5877 return emitExt128(MI, MBB, true, SystemZ::subreg_l32); 5878 case SystemZ::ZEXT128_64: 5879 return emitExt128(MI, MBB, true, SystemZ::subreg_l64); 5880 5881 case SystemZ::ATOMIC_SWAPW: 5882 return emitAtomicLoadBinary(MI, MBB, 0, 0); 5883 case SystemZ::ATOMIC_SWAP_32: 5884 return emitAtomicLoadBinary(MI, MBB, 0, 32); 5885 case SystemZ::ATOMIC_SWAP_64: 5886 return emitAtomicLoadBinary(MI, MBB, 0, 64); 5887 5888 case SystemZ::ATOMIC_LOADW_AR: 5889 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0); 5890 case SystemZ::ATOMIC_LOADW_AFI: 5891 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0); 5892 case SystemZ::ATOMIC_LOAD_AR: 5893 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32); 5894 case SystemZ::ATOMIC_LOAD_AHI: 5895 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32); 5896 case SystemZ::ATOMIC_LOAD_AFI: 5897 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32); 5898 case SystemZ::ATOMIC_LOAD_AGR: 5899 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64); 5900 case SystemZ::ATOMIC_LOAD_AGHI: 5901 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64); 5902 case SystemZ::ATOMIC_LOAD_AGFI: 5903 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64); 5904 5905 case SystemZ::ATOMIC_LOADW_SR: 5906 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0); 5907 case SystemZ::ATOMIC_LOAD_SR: 5908 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32); 5909 case SystemZ::ATOMIC_LOAD_SGR: 5910 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64); 5911 5912 case SystemZ::ATOMIC_LOADW_NR: 5913 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0); 5914 case SystemZ::ATOMIC_LOADW_NILH: 5915 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0); 5916 case SystemZ::ATOMIC_LOAD_NR: 5917 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32); 5918 case SystemZ::ATOMIC_LOAD_NILL: 5919 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32); 5920 case SystemZ::ATOMIC_LOAD_NILH: 5921 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32); 5922 case SystemZ::ATOMIC_LOAD_NILF: 5923 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32); 5924 case SystemZ::ATOMIC_LOAD_NGR: 5925 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64); 5926 case SystemZ::ATOMIC_LOAD_NILL64: 5927 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64); 5928 case SystemZ::ATOMIC_LOAD_NILH64: 5929 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64); 5930 case SystemZ::ATOMIC_LOAD_NIHL64: 5931 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64); 5932 case SystemZ::ATOMIC_LOAD_NIHH64: 5933 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64); 5934 case SystemZ::ATOMIC_LOAD_NILF64: 5935 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64); 5936 case SystemZ::ATOMIC_LOAD_NIHF64: 5937 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64); 5938 5939 case SystemZ::ATOMIC_LOADW_OR: 5940 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0); 5941 case SystemZ::ATOMIC_LOADW_OILH: 5942 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0); 5943 case SystemZ::ATOMIC_LOAD_OR: 5944 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32); 5945 case SystemZ::ATOMIC_LOAD_OILL: 5946 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32); 5947 case SystemZ::ATOMIC_LOAD_OILH: 5948 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32); 5949 case SystemZ::ATOMIC_LOAD_OILF: 5950 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32); 5951 case SystemZ::ATOMIC_LOAD_OGR: 5952 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64); 5953 case SystemZ::ATOMIC_LOAD_OILL64: 5954 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64); 5955 case SystemZ::ATOMIC_LOAD_OILH64: 5956 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64); 5957 case SystemZ::ATOMIC_LOAD_OIHL64: 5958 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64); 5959 case SystemZ::ATOMIC_LOAD_OIHH64: 5960 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64); 5961 case SystemZ::ATOMIC_LOAD_OILF64: 5962 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64); 5963 case SystemZ::ATOMIC_LOAD_OIHF64: 5964 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64); 5965 5966 case SystemZ::ATOMIC_LOADW_XR: 5967 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0); 5968 case SystemZ::ATOMIC_LOADW_XILF: 5969 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0); 5970 case SystemZ::ATOMIC_LOAD_XR: 5971 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32); 5972 case SystemZ::ATOMIC_LOAD_XILF: 5973 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32); 5974 case SystemZ::ATOMIC_LOAD_XGR: 5975 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64); 5976 case SystemZ::ATOMIC_LOAD_XILF64: 5977 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64); 5978 case SystemZ::ATOMIC_LOAD_XIHF64: 5979 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64); 5980 5981 case SystemZ::ATOMIC_LOADW_NRi: 5982 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true); 5983 case SystemZ::ATOMIC_LOADW_NILHi: 5984 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true); 5985 case SystemZ::ATOMIC_LOAD_NRi: 5986 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true); 5987 case SystemZ::ATOMIC_LOAD_NILLi: 5988 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true); 5989 case SystemZ::ATOMIC_LOAD_NILHi: 5990 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true); 5991 case SystemZ::ATOMIC_LOAD_NILFi: 5992 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true); 5993 case SystemZ::ATOMIC_LOAD_NGRi: 5994 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true); 5995 case SystemZ::ATOMIC_LOAD_NILL64i: 5996 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true); 5997 case SystemZ::ATOMIC_LOAD_NILH64i: 5998 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true); 5999 case SystemZ::ATOMIC_LOAD_NIHL64i: 6000 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true); 6001 case SystemZ::ATOMIC_LOAD_NIHH64i: 6002 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true); 6003 case SystemZ::ATOMIC_LOAD_NILF64i: 6004 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true); 6005 case SystemZ::ATOMIC_LOAD_NIHF64i: 6006 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true); 6007 6008 case SystemZ::ATOMIC_LOADW_MIN: 6009 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 6010 SystemZ::CCMASK_CMP_LE, 0); 6011 case SystemZ::ATOMIC_LOAD_MIN_32: 6012 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 6013 SystemZ::CCMASK_CMP_LE, 32); 6014 case SystemZ::ATOMIC_LOAD_MIN_64: 6015 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, 6016 SystemZ::CCMASK_CMP_LE, 64); 6017 6018 case SystemZ::ATOMIC_LOADW_MAX: 6019 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 6020 SystemZ::CCMASK_CMP_GE, 0); 6021 case SystemZ::ATOMIC_LOAD_MAX_32: 6022 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 6023 SystemZ::CCMASK_CMP_GE, 32); 6024 case SystemZ::ATOMIC_LOAD_MAX_64: 6025 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, 6026 SystemZ::CCMASK_CMP_GE, 64); 6027 6028 case SystemZ::ATOMIC_LOADW_UMIN: 6029 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 6030 SystemZ::CCMASK_CMP_LE, 0); 6031 case SystemZ::ATOMIC_LOAD_UMIN_32: 6032 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 6033 SystemZ::CCMASK_CMP_LE, 32); 6034 case SystemZ::ATOMIC_LOAD_UMIN_64: 6035 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, 6036 SystemZ::CCMASK_CMP_LE, 64); 6037 6038 case SystemZ::ATOMIC_LOADW_UMAX: 6039 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 6040 SystemZ::CCMASK_CMP_GE, 0); 6041 case SystemZ::ATOMIC_LOAD_UMAX_32: 6042 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 6043 SystemZ::CCMASK_CMP_GE, 32); 6044 case SystemZ::ATOMIC_LOAD_UMAX_64: 6045 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, 6046 SystemZ::CCMASK_CMP_GE, 64); 6047 6048 case SystemZ::ATOMIC_CMP_SWAPW: 6049 return emitAtomicCmpSwapW(MI, MBB); 6050 case SystemZ::MVCSequence: 6051 case SystemZ::MVCLoop: 6052 return emitMemMemWrapper(MI, MBB, SystemZ::MVC); 6053 case SystemZ::NCSequence: 6054 case SystemZ::NCLoop: 6055 return emitMemMemWrapper(MI, MBB, SystemZ::NC); 6056 case SystemZ::OCSequence: 6057 case SystemZ::OCLoop: 6058 return emitMemMemWrapper(MI, MBB, SystemZ::OC); 6059 case SystemZ::XCSequence: 6060 case SystemZ::XCLoop: 6061 return emitMemMemWrapper(MI, MBB, SystemZ::XC); 6062 case SystemZ::CLCSequence: 6063 case SystemZ::CLCLoop: 6064 return emitMemMemWrapper(MI, MBB, SystemZ::CLC); 6065 case SystemZ::CLSTLoop: 6066 return emitStringWrapper(MI, MBB, SystemZ::CLST); 6067 case SystemZ::MVSTLoop: 6068 return emitStringWrapper(MI, MBB, SystemZ::MVST); 6069 case SystemZ::SRSTLoop: 6070 return emitStringWrapper(MI, MBB, SystemZ::SRST); 6071 case SystemZ::TBEGIN: 6072 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false); 6073 case SystemZ::TBEGIN_nofloat: 6074 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true); 6075 case SystemZ::TBEGINC: 6076 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true); 6077 case SystemZ::LTEBRCompare_VecPseudo: 6078 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR); 6079 case SystemZ::LTDBRCompare_VecPseudo: 6080 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR); 6081 case SystemZ::LTXBRCompare_VecPseudo: 6082 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR); 6083 6084 default: 6085 llvm_unreachable("Unexpected instr type to insert"); 6086 } 6087 } 6088