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