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