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