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