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