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