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