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::CallFrameSize + 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::NumArgFPRs && !useSoftFloat()) { 1492 SDValue MemOps[SystemZ::NumArgFPRs]; 1493 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) { 1494 unsigned Offset = TFL->getRegSpillOffset(MF, SystemZ::ArgFPRs[I]); 1495 int FI = 1496 MFI.CreateFixedObject(8, -SystemZMC::CallFrameSize + Offset, true); 1497 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 1498 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[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::NumArgFPRs-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::CallFrameSize + 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 DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), AtomicOp.getValue(0)); 4060 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success); 4061 DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(2)); 4062 return SDValue(); 4063 } 4064 4065 MachineMemOperand::Flags 4066 SystemZTargetLowering::getTargetMMOFlags(const Instruction &I) const { 4067 // Because of how we convert atomic_load and atomic_store to normal loads and 4068 // stores in the DAG, we need to ensure that the MMOs are marked volatile 4069 // since DAGCombine hasn't been updated to account for atomic, but non 4070 // volatile loads. (See D57601) 4071 if (auto *SI = dyn_cast<StoreInst>(&I)) 4072 if (SI->isAtomic()) 4073 return MachineMemOperand::MOVolatile; 4074 if (auto *LI = dyn_cast<LoadInst>(&I)) 4075 if (LI->isAtomic()) 4076 return MachineMemOperand::MOVolatile; 4077 if (auto *AI = dyn_cast<AtomicRMWInst>(&I)) 4078 if (AI->isAtomic()) 4079 return MachineMemOperand::MOVolatile; 4080 if (auto *AI = dyn_cast<AtomicCmpXchgInst>(&I)) 4081 if (AI->isAtomic()) 4082 return MachineMemOperand::MOVolatile; 4083 return MachineMemOperand::MONone; 4084 } 4085 4086 SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op, 4087 SelectionDAG &DAG) const { 4088 MachineFunction &MF = DAG.getMachineFunction(); 4089 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); 4090 if (MF.getFunction().getCallingConv() == CallingConv::GHC) 4091 report_fatal_error("Variable-sized stack allocations are not supported " 4092 "in GHC calling convention"); 4093 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op), 4094 SystemZ::R15D, Op.getValueType()); 4095 } 4096 4097 SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op, 4098 SelectionDAG &DAG) const { 4099 MachineFunction &MF = DAG.getMachineFunction(); 4100 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); 4101 bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain"); 4102 4103 if (MF.getFunction().getCallingConv() == CallingConv::GHC) 4104 report_fatal_error("Variable-sized stack allocations are not supported " 4105 "in GHC calling convention"); 4106 4107 SDValue Chain = Op.getOperand(0); 4108 SDValue NewSP = Op.getOperand(1); 4109 SDValue Backchain; 4110 SDLoc DL(Op); 4111 4112 if (StoreBackchain) { 4113 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64); 4114 Backchain = DAG.getLoad(MVT::i64, DL, Chain, getBackchainAddress(OldSP, DAG), 4115 MachinePointerInfo()); 4116 } 4117 4118 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP); 4119 4120 if (StoreBackchain) 4121 Chain = DAG.getStore(Chain, DL, Backchain, getBackchainAddress(NewSP, DAG), 4122 MachinePointerInfo()); 4123 4124 return Chain; 4125 } 4126 4127 SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op, 4128 SelectionDAG &DAG) const { 4129 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 4130 if (!IsData) 4131 // Just preserve the chain. 4132 return Op.getOperand(0); 4133 4134 SDLoc DL(Op); 4135 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); 4136 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ; 4137 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode()); 4138 SDValue Ops[] = {Op.getOperand(0), DAG.getTargetConstant(Code, DL, MVT::i32), 4139 Op.getOperand(1)}; 4140 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL, 4141 Node->getVTList(), Ops, 4142 Node->getMemoryVT(), Node->getMemOperand()); 4143 } 4144 4145 // Convert condition code in CCReg to an i32 value. 4146 static SDValue getCCResult(SelectionDAG &DAG, SDValue CCReg) { 4147 SDLoc DL(CCReg); 4148 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, CCReg); 4149 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM, 4150 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32)); 4151 } 4152 4153 SDValue 4154 SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op, 4155 SelectionDAG &DAG) const { 4156 unsigned Opcode, CCValid; 4157 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) { 4158 assert(Op->getNumValues() == 2 && "Expected only CC result and chain"); 4159 SDNode *Node = emitIntrinsicWithCCAndChain(DAG, Op, Opcode); 4160 SDValue CC = getCCResult(DAG, SDValue(Node, 0)); 4161 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC); 4162 return SDValue(); 4163 } 4164 4165 return SDValue(); 4166 } 4167 4168 SDValue 4169 SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op, 4170 SelectionDAG &DAG) const { 4171 unsigned Opcode, CCValid; 4172 if (isIntrinsicWithCC(Op, Opcode, CCValid)) { 4173 SDNode *Node = emitIntrinsicWithCC(DAG, Op, Opcode); 4174 if (Op->getNumValues() == 1) 4175 return getCCResult(DAG, SDValue(Node, 0)); 4176 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result"); 4177 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), 4178 SDValue(Node, 0), getCCResult(DAG, SDValue(Node, 1))); 4179 } 4180 4181 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4182 switch (Id) { 4183 case Intrinsic::thread_pointer: 4184 return lowerThreadPointer(SDLoc(Op), DAG); 4185 4186 case Intrinsic::s390_vpdi: 4187 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(), 4188 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 4189 4190 case Intrinsic::s390_vperm: 4191 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(), 4192 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 4193 4194 case Intrinsic::s390_vuphb: 4195 case Intrinsic::s390_vuphh: 4196 case Intrinsic::s390_vuphf: 4197 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(), 4198 Op.getOperand(1)); 4199 4200 case Intrinsic::s390_vuplhb: 4201 case Intrinsic::s390_vuplhh: 4202 case Intrinsic::s390_vuplhf: 4203 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(), 4204 Op.getOperand(1)); 4205 4206 case Intrinsic::s390_vuplb: 4207 case Intrinsic::s390_vuplhw: 4208 case Intrinsic::s390_vuplf: 4209 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(), 4210 Op.getOperand(1)); 4211 4212 case Intrinsic::s390_vupllb: 4213 case Intrinsic::s390_vupllh: 4214 case Intrinsic::s390_vupllf: 4215 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(), 4216 Op.getOperand(1)); 4217 4218 case Intrinsic::s390_vsumb: 4219 case Intrinsic::s390_vsumh: 4220 case Intrinsic::s390_vsumgh: 4221 case Intrinsic::s390_vsumgf: 4222 case Intrinsic::s390_vsumqf: 4223 case Intrinsic::s390_vsumqg: 4224 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(), 4225 Op.getOperand(1), Op.getOperand(2)); 4226 } 4227 4228 return SDValue(); 4229 } 4230 4231 namespace { 4232 // Says that SystemZISD operation Opcode can be used to perform the equivalent 4233 // of a VPERM with permute vector Bytes. If Opcode takes three operands, 4234 // Operand is the constant third operand, otherwise it is the number of 4235 // bytes in each element of the result. 4236 struct Permute { 4237 unsigned Opcode; 4238 unsigned Operand; 4239 unsigned char Bytes[SystemZ::VectorBytes]; 4240 }; 4241 } 4242 4243 static const Permute PermuteForms[] = { 4244 // VMRHG 4245 { SystemZISD::MERGE_HIGH, 8, 4246 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } }, 4247 // VMRHF 4248 { SystemZISD::MERGE_HIGH, 4, 4249 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } }, 4250 // VMRHH 4251 { SystemZISD::MERGE_HIGH, 2, 4252 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } }, 4253 // VMRHB 4254 { SystemZISD::MERGE_HIGH, 1, 4255 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } }, 4256 // VMRLG 4257 { SystemZISD::MERGE_LOW, 8, 4258 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } }, 4259 // VMRLF 4260 { SystemZISD::MERGE_LOW, 4, 4261 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } }, 4262 // VMRLH 4263 { SystemZISD::MERGE_LOW, 2, 4264 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } }, 4265 // VMRLB 4266 { SystemZISD::MERGE_LOW, 1, 4267 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } }, 4268 // VPKG 4269 { SystemZISD::PACK, 4, 4270 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } }, 4271 // VPKF 4272 { SystemZISD::PACK, 2, 4273 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } }, 4274 // VPKH 4275 { SystemZISD::PACK, 1, 4276 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } }, 4277 // VPDI V1, V2, 4 (low half of V1, high half of V2) 4278 { SystemZISD::PERMUTE_DWORDS, 4, 4279 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } }, 4280 // VPDI V1, V2, 1 (high half of V1, low half of V2) 4281 { SystemZISD::PERMUTE_DWORDS, 1, 4282 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } } 4283 }; 4284 4285 // Called after matching a vector shuffle against a particular pattern. 4286 // Both the original shuffle and the pattern have two vector operands. 4287 // OpNos[0] is the operand of the original shuffle that should be used for 4288 // operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything. 4289 // OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and 4290 // set OpNo0 and OpNo1 to the shuffle operands that should actually be used 4291 // for operands 0 and 1 of the pattern. 4292 static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) { 4293 if (OpNos[0] < 0) { 4294 if (OpNos[1] < 0) 4295 return false; 4296 OpNo0 = OpNo1 = OpNos[1]; 4297 } else if (OpNos[1] < 0) { 4298 OpNo0 = OpNo1 = OpNos[0]; 4299 } else { 4300 OpNo0 = OpNos[0]; 4301 OpNo1 = OpNos[1]; 4302 } 4303 return true; 4304 } 4305 4306 // Bytes is a VPERM-like permute vector, except that -1 is used for 4307 // undefined bytes. Return true if the VPERM can be implemented using P. 4308 // When returning true set OpNo0 to the VPERM operand that should be 4309 // used for operand 0 of P and likewise OpNo1 for operand 1 of P. 4310 // 4311 // For example, if swapping the VPERM operands allows P to match, OpNo0 4312 // will be 1 and OpNo1 will be 0. If instead Bytes only refers to one 4313 // operand, but rewriting it to use two duplicated operands allows it to 4314 // match P, then OpNo0 and OpNo1 will be the same. 4315 static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P, 4316 unsigned &OpNo0, unsigned &OpNo1) { 4317 int OpNos[] = { -1, -1 }; 4318 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) { 4319 int Elt = Bytes[I]; 4320 if (Elt >= 0) { 4321 // Make sure that the two permute vectors use the same suboperand 4322 // byte number. Only the operand numbers (the high bits) are 4323 // allowed to differ. 4324 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1)) 4325 return false; 4326 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes; 4327 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes; 4328 // Make sure that the operand mappings are consistent with previous 4329 // elements. 4330 if (OpNos[ModelOpNo] == 1 - RealOpNo) 4331 return false; 4332 OpNos[ModelOpNo] = RealOpNo; 4333 } 4334 } 4335 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1); 4336 } 4337 4338 // As above, but search for a matching permute. 4339 static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes, 4340 unsigned &OpNo0, unsigned &OpNo1) { 4341 for (auto &P : PermuteForms) 4342 if (matchPermute(Bytes, P, OpNo0, OpNo1)) 4343 return &P; 4344 return nullptr; 4345 } 4346 4347 // Bytes is a VPERM-like permute vector, except that -1 is used for 4348 // undefined bytes. This permute is an operand of an outer permute. 4349 // See whether redistributing the -1 bytes gives a shuffle that can be 4350 // implemented using P. If so, set Transform to a VPERM-like permute vector 4351 // that, when applied to the result of P, gives the original permute in Bytes. 4352 static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes, 4353 const Permute &P, 4354 SmallVectorImpl<int> &Transform) { 4355 unsigned To = 0; 4356 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) { 4357 int Elt = Bytes[From]; 4358 if (Elt < 0) 4359 // Byte number From of the result is undefined. 4360 Transform[From] = -1; 4361 else { 4362 while (P.Bytes[To] != Elt) { 4363 To += 1; 4364 if (To == SystemZ::VectorBytes) 4365 return false; 4366 } 4367 Transform[From] = To; 4368 } 4369 } 4370 return true; 4371 } 4372 4373 // As above, but search for a matching permute. 4374 static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes, 4375 SmallVectorImpl<int> &Transform) { 4376 for (auto &P : PermuteForms) 4377 if (matchDoublePermute(Bytes, P, Transform)) 4378 return &P; 4379 return nullptr; 4380 } 4381 4382 // Convert the mask of the given shuffle op into a byte-level mask, 4383 // as if it had type vNi8. 4384 static bool getVPermMask(SDValue ShuffleOp, 4385 SmallVectorImpl<int> &Bytes) { 4386 EVT VT = ShuffleOp.getValueType(); 4387 unsigned NumElements = VT.getVectorNumElements(); 4388 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); 4389 4390 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(ShuffleOp)) { 4391 Bytes.resize(NumElements * BytesPerElement, -1); 4392 for (unsigned I = 0; I < NumElements; ++I) { 4393 int Index = VSN->getMaskElt(I); 4394 if (Index >= 0) 4395 for (unsigned J = 0; J < BytesPerElement; ++J) 4396 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J; 4397 } 4398 return true; 4399 } 4400 if (SystemZISD::SPLAT == ShuffleOp.getOpcode() && 4401 isa<ConstantSDNode>(ShuffleOp.getOperand(1))) { 4402 unsigned Index = ShuffleOp.getConstantOperandVal(1); 4403 Bytes.resize(NumElements * BytesPerElement, -1); 4404 for (unsigned I = 0; I < NumElements; ++I) 4405 for (unsigned J = 0; J < BytesPerElement; ++J) 4406 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J; 4407 return true; 4408 } 4409 return false; 4410 } 4411 4412 // Bytes is a VPERM-like permute vector, except that -1 is used for 4413 // undefined bytes. See whether bytes [Start, Start + BytesPerElement) of 4414 // the result come from a contiguous sequence of bytes from one input. 4415 // Set Base to the selector for the first byte if so. 4416 static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start, 4417 unsigned BytesPerElement, int &Base) { 4418 Base = -1; 4419 for (unsigned I = 0; I < BytesPerElement; ++I) { 4420 if (Bytes[Start + I] >= 0) { 4421 unsigned Elem = Bytes[Start + I]; 4422 if (Base < 0) { 4423 Base = Elem - I; 4424 // Make sure the bytes would come from one input operand. 4425 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size()) 4426 return false; 4427 } else if (unsigned(Base) != Elem - I) 4428 return false; 4429 } 4430 } 4431 return true; 4432 } 4433 4434 // Bytes is a VPERM-like permute vector, except that -1 is used for 4435 // undefined bytes. Return true if it can be performed using VSLDB. 4436 // When returning true, set StartIndex to the shift amount and OpNo0 4437 // and OpNo1 to the VPERM operands that should be used as the first 4438 // and second shift operand respectively. 4439 static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes, 4440 unsigned &StartIndex, unsigned &OpNo0, 4441 unsigned &OpNo1) { 4442 int OpNos[] = { -1, -1 }; 4443 int Shift = -1; 4444 for (unsigned I = 0; I < 16; ++I) { 4445 int Index = Bytes[I]; 4446 if (Index >= 0) { 4447 int ExpectedShift = (Index - I) % SystemZ::VectorBytes; 4448 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes; 4449 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes; 4450 if (Shift < 0) 4451 Shift = ExpectedShift; 4452 else if (Shift != ExpectedShift) 4453 return false; 4454 // Make sure that the operand mappings are consistent with previous 4455 // elements. 4456 if (OpNos[ModelOpNo] == 1 - RealOpNo) 4457 return false; 4458 OpNos[ModelOpNo] = RealOpNo; 4459 } 4460 } 4461 StartIndex = Shift; 4462 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1); 4463 } 4464 4465 // Create a node that performs P on operands Op0 and Op1, casting the 4466 // operands to the appropriate type. The type of the result is determined by P. 4467 static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL, 4468 const Permute &P, SDValue Op0, SDValue Op1) { 4469 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input 4470 // elements of a PACK are twice as wide as the outputs. 4471 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 : 4472 P.Opcode == SystemZISD::PACK ? P.Operand * 2 : 4473 P.Operand); 4474 // Cast both operands to the appropriate type. 4475 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8), 4476 SystemZ::VectorBytes / InBytes); 4477 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0); 4478 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1); 4479 SDValue Op; 4480 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) { 4481 SDValue Op2 = DAG.getTargetConstant(P.Operand, DL, MVT::i32); 4482 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2); 4483 } else if (P.Opcode == SystemZISD::PACK) { 4484 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8), 4485 SystemZ::VectorBytes / P.Operand); 4486 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1); 4487 } else { 4488 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1); 4489 } 4490 return Op; 4491 } 4492 4493 static bool isZeroVector(SDValue N) { 4494 if (N->getOpcode() == ISD::BITCAST) 4495 N = N->getOperand(0); 4496 if (N->getOpcode() == ISD::SPLAT_VECTOR) 4497 if (auto *Op = dyn_cast<ConstantSDNode>(N->getOperand(0))) 4498 return Op->getZExtValue() == 0; 4499 return ISD::isBuildVectorAllZeros(N.getNode()); 4500 } 4501 4502 // Return the index of the zero/undef vector, or UINT32_MAX if not found. 4503 static uint32_t findZeroVectorIdx(SDValue *Ops, unsigned Num) { 4504 for (unsigned I = 0; I < Num ; I++) 4505 if (isZeroVector(Ops[I])) 4506 return I; 4507 return UINT32_MAX; 4508 } 4509 4510 // Bytes is a VPERM-like permute vector, except that -1 is used for 4511 // undefined bytes. Implement it on operands Ops[0] and Ops[1] using 4512 // VSLDB or VPERM. 4513 static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL, 4514 SDValue *Ops, 4515 const SmallVectorImpl<int> &Bytes) { 4516 for (unsigned I = 0; I < 2; ++I) 4517 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]); 4518 4519 // First see whether VSLDB can be used. 4520 unsigned StartIndex, OpNo0, OpNo1; 4521 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1)) 4522 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0], 4523 Ops[OpNo1], 4524 DAG.getTargetConstant(StartIndex, DL, MVT::i32)); 4525 4526 // Fall back on VPERM. Construct an SDNode for the permute vector. Try to 4527 // eliminate a zero vector by reusing any zero index in the permute vector. 4528 unsigned ZeroVecIdx = findZeroVectorIdx(&Ops[0], 2); 4529 if (ZeroVecIdx != UINT32_MAX) { 4530 bool MaskFirst = true; 4531 int ZeroIdx = -1; 4532 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) { 4533 unsigned OpNo = unsigned(Bytes[I]) / SystemZ::VectorBytes; 4534 unsigned Byte = unsigned(Bytes[I]) % SystemZ::VectorBytes; 4535 if (OpNo == ZeroVecIdx && I == 0) { 4536 // If the first byte is zero, use mask as first operand. 4537 ZeroIdx = 0; 4538 break; 4539 } 4540 if (OpNo != ZeroVecIdx && Byte == 0) { 4541 // If mask contains a zero, use it by placing that vector first. 4542 ZeroIdx = I + SystemZ::VectorBytes; 4543 MaskFirst = false; 4544 break; 4545 } 4546 } 4547 if (ZeroIdx != -1) { 4548 SDValue IndexNodes[SystemZ::VectorBytes]; 4549 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) { 4550 if (Bytes[I] >= 0) { 4551 unsigned OpNo = unsigned(Bytes[I]) / SystemZ::VectorBytes; 4552 unsigned Byte = unsigned(Bytes[I]) % SystemZ::VectorBytes; 4553 if (OpNo == ZeroVecIdx) 4554 IndexNodes[I] = DAG.getConstant(ZeroIdx, DL, MVT::i32); 4555 else { 4556 unsigned BIdx = MaskFirst ? Byte + SystemZ::VectorBytes : Byte; 4557 IndexNodes[I] = DAG.getConstant(BIdx, DL, MVT::i32); 4558 } 4559 } else 4560 IndexNodes[I] = DAG.getUNDEF(MVT::i32); 4561 } 4562 SDValue Mask = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes); 4563 SDValue Src = ZeroVecIdx == 0 ? Ops[1] : Ops[0]; 4564 if (MaskFirst) 4565 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Mask, Src, 4566 Mask); 4567 else 4568 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Src, Mask, 4569 Mask); 4570 } 4571 } 4572 4573 SDValue IndexNodes[SystemZ::VectorBytes]; 4574 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) 4575 if (Bytes[I] >= 0) 4576 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32); 4577 else 4578 IndexNodes[I] = DAG.getUNDEF(MVT::i32); 4579 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes); 4580 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], 4581 (!Ops[1].isUndef() ? Ops[1] : Ops[0]), Op2); 4582 } 4583 4584 namespace { 4585 // Describes a general N-operand vector shuffle. 4586 struct GeneralShuffle { 4587 GeneralShuffle(EVT vt) : VT(vt), UnpackFromEltSize(UINT_MAX) {} 4588 void addUndef(); 4589 bool add(SDValue, unsigned); 4590 SDValue getNode(SelectionDAG &, const SDLoc &); 4591 void tryPrepareForUnpack(); 4592 bool unpackWasPrepared() { return UnpackFromEltSize <= 4; } 4593 SDValue insertUnpackIfPrepared(SelectionDAG &DAG, const SDLoc &DL, SDValue Op); 4594 4595 // The operands of the shuffle. 4596 SmallVector<SDValue, SystemZ::VectorBytes> Ops; 4597 4598 // Index I is -1 if byte I of the result is undefined. Otherwise the 4599 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand 4600 // Bytes[I] / SystemZ::VectorBytes. 4601 SmallVector<int, SystemZ::VectorBytes> Bytes; 4602 4603 // The type of the shuffle result. 4604 EVT VT; 4605 4606 // Holds a value of 1, 2 or 4 if a final unpack has been prepared for. 4607 unsigned UnpackFromEltSize; 4608 }; 4609 } 4610 4611 // Add an extra undefined element to the shuffle. 4612 void GeneralShuffle::addUndef() { 4613 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); 4614 for (unsigned I = 0; I < BytesPerElement; ++I) 4615 Bytes.push_back(-1); 4616 } 4617 4618 // Add an extra element to the shuffle, taking it from element Elem of Op. 4619 // A null Op indicates a vector input whose value will be calculated later; 4620 // there is at most one such input per shuffle and it always has the same 4621 // type as the result. Aborts and returns false if the source vector elements 4622 // of an EXTRACT_VECTOR_ELT are smaller than the destination elements. Per 4623 // LLVM they become implicitly extended, but this is rare and not optimized. 4624 bool GeneralShuffle::add(SDValue Op, unsigned Elem) { 4625 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); 4626 4627 // The source vector can have wider elements than the result, 4628 // either through an explicit TRUNCATE or because of type legalization. 4629 // We want the least significant part. 4630 EVT FromVT = Op.getNode() ? Op.getValueType() : VT; 4631 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize(); 4632 4633 // Return false if the source elements are smaller than their destination 4634 // elements. 4635 if (FromBytesPerElement < BytesPerElement) 4636 return false; 4637 4638 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes + 4639 (FromBytesPerElement - BytesPerElement)); 4640 4641 // Look through things like shuffles and bitcasts. 4642 while (Op.getNode()) { 4643 if (Op.getOpcode() == ISD::BITCAST) 4644 Op = Op.getOperand(0); 4645 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) { 4646 // See whether the bytes we need come from a contiguous part of one 4647 // operand. 4648 SmallVector<int, SystemZ::VectorBytes> OpBytes; 4649 if (!getVPermMask(Op, OpBytes)) 4650 break; 4651 int NewByte; 4652 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte)) 4653 break; 4654 if (NewByte < 0) { 4655 addUndef(); 4656 return true; 4657 } 4658 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes); 4659 Byte = unsigned(NewByte) % SystemZ::VectorBytes; 4660 } else if (Op.isUndef()) { 4661 addUndef(); 4662 return true; 4663 } else 4664 break; 4665 } 4666 4667 // Make sure that the source of the extraction is in Ops. 4668 unsigned OpNo = 0; 4669 for (; OpNo < Ops.size(); ++OpNo) 4670 if (Ops[OpNo] == Op) 4671 break; 4672 if (OpNo == Ops.size()) 4673 Ops.push_back(Op); 4674 4675 // Add the element to Bytes. 4676 unsigned Base = OpNo * SystemZ::VectorBytes + Byte; 4677 for (unsigned I = 0; I < BytesPerElement; ++I) 4678 Bytes.push_back(Base + I); 4679 4680 return true; 4681 } 4682 4683 // Return SDNodes for the completed shuffle. 4684 SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) { 4685 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector"); 4686 4687 if (Ops.size() == 0) 4688 return DAG.getUNDEF(VT); 4689 4690 // Use a single unpack if possible as the last operation. 4691 tryPrepareForUnpack(); 4692 4693 // Make sure that there are at least two shuffle operands. 4694 if (Ops.size() == 1) 4695 Ops.push_back(DAG.getUNDEF(MVT::v16i8)); 4696 4697 // Create a tree of shuffles, deferring root node until after the loop. 4698 // Try to redistribute the undefined elements of non-root nodes so that 4699 // the non-root shuffles match something like a pack or merge, then adjust 4700 // the parent node's permute vector to compensate for the new order. 4701 // Among other things, this copes with vectors like <2 x i16> that were 4702 // padded with undefined elements during type legalization. 4703 // 4704 // In the best case this redistribution will lead to the whole tree 4705 // using packs and merges. It should rarely be a loss in other cases. 4706 unsigned Stride = 1; 4707 for (; Stride * 2 < Ops.size(); Stride *= 2) { 4708 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) { 4709 SDValue SubOps[] = { Ops[I], Ops[I + Stride] }; 4710 4711 // Create a mask for just these two operands. 4712 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes); 4713 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) { 4714 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes; 4715 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes; 4716 if (OpNo == I) 4717 NewBytes[J] = Byte; 4718 else if (OpNo == I + Stride) 4719 NewBytes[J] = SystemZ::VectorBytes + Byte; 4720 else 4721 NewBytes[J] = -1; 4722 } 4723 // See if it would be better to reorganize NewMask to avoid using VPERM. 4724 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes); 4725 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) { 4726 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]); 4727 // Applying NewBytesMap to Ops[I] gets back to NewBytes. 4728 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) { 4729 if (NewBytes[J] >= 0) { 4730 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes && 4731 "Invalid double permute"); 4732 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J]; 4733 } else 4734 assert(NewBytesMap[J] < 0 && "Invalid double permute"); 4735 } 4736 } else { 4737 // Just use NewBytes on the operands. 4738 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes); 4739 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) 4740 if (NewBytes[J] >= 0) 4741 Bytes[J] = I * SystemZ::VectorBytes + J; 4742 } 4743 } 4744 } 4745 4746 // Now we just have 2 inputs. Put the second operand in Ops[1]. 4747 if (Stride > 1) { 4748 Ops[1] = Ops[Stride]; 4749 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) 4750 if (Bytes[I] >= int(SystemZ::VectorBytes)) 4751 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes; 4752 } 4753 4754 // Look for an instruction that can do the permute without resorting 4755 // to VPERM. 4756 unsigned OpNo0, OpNo1; 4757 SDValue Op; 4758 if (unpackWasPrepared() && Ops[1].isUndef()) 4759 Op = Ops[0]; 4760 else if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1)) 4761 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]); 4762 else 4763 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes); 4764 4765 Op = insertUnpackIfPrepared(DAG, DL, Op); 4766 4767 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 4768 } 4769 4770 #ifndef NDEBUG 4771 static void dumpBytes(const SmallVectorImpl<int> &Bytes, std::string Msg) { 4772 dbgs() << Msg.c_str() << " { "; 4773 for (unsigned i = 0; i < Bytes.size(); i++) 4774 dbgs() << Bytes[i] << " "; 4775 dbgs() << "}\n"; 4776 } 4777 #endif 4778 4779 // If the Bytes vector matches an unpack operation, prepare to do the unpack 4780 // after all else by removing the zero vector and the effect of the unpack on 4781 // Bytes. 4782 void GeneralShuffle::tryPrepareForUnpack() { 4783 uint32_t ZeroVecOpNo = findZeroVectorIdx(&Ops[0], Ops.size()); 4784 if (ZeroVecOpNo == UINT32_MAX || Ops.size() == 1) 4785 return; 4786 4787 // Only do this if removing the zero vector reduces the depth, otherwise 4788 // the critical path will increase with the final unpack. 4789 if (Ops.size() > 2 && 4790 Log2_32_Ceil(Ops.size()) == Log2_32_Ceil(Ops.size() - 1)) 4791 return; 4792 4793 // Find an unpack that would allow removing the zero vector from Ops. 4794 UnpackFromEltSize = 1; 4795 for (; UnpackFromEltSize <= 4; UnpackFromEltSize *= 2) { 4796 bool MatchUnpack = true; 4797 SmallVector<int, SystemZ::VectorBytes> SrcBytes; 4798 for (unsigned Elt = 0; Elt < SystemZ::VectorBytes; Elt++) { 4799 unsigned ToEltSize = UnpackFromEltSize * 2; 4800 bool IsZextByte = (Elt % ToEltSize) < UnpackFromEltSize; 4801 if (!IsZextByte) 4802 SrcBytes.push_back(Bytes[Elt]); 4803 if (Bytes[Elt] != -1) { 4804 unsigned OpNo = unsigned(Bytes[Elt]) / SystemZ::VectorBytes; 4805 if (IsZextByte != (OpNo == ZeroVecOpNo)) { 4806 MatchUnpack = false; 4807 break; 4808 } 4809 } 4810 } 4811 if (MatchUnpack) { 4812 if (Ops.size() == 2) { 4813 // Don't use unpack if a single source operand needs rearrangement. 4814 for (unsigned i = 0; i < SystemZ::VectorBytes / 2; i++) 4815 if (SrcBytes[i] != -1 && SrcBytes[i] % 16 != int(i)) { 4816 UnpackFromEltSize = UINT_MAX; 4817 return; 4818 } 4819 } 4820 break; 4821 } 4822 } 4823 if (UnpackFromEltSize > 4) 4824 return; 4825 4826 LLVM_DEBUG(dbgs() << "Preparing for final unpack of element size " 4827 << UnpackFromEltSize << ". Zero vector is Op#" << ZeroVecOpNo 4828 << ".\n"; 4829 dumpBytes(Bytes, "Original Bytes vector:");); 4830 4831 // Apply the unpack in reverse to the Bytes array. 4832 unsigned B = 0; 4833 for (unsigned Elt = 0; Elt < SystemZ::VectorBytes;) { 4834 Elt += UnpackFromEltSize; 4835 for (unsigned i = 0; i < UnpackFromEltSize; i++, Elt++, B++) 4836 Bytes[B] = Bytes[Elt]; 4837 } 4838 while (B < SystemZ::VectorBytes) 4839 Bytes[B++] = -1; 4840 4841 // Remove the zero vector from Ops 4842 Ops.erase(&Ops[ZeroVecOpNo]); 4843 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) 4844 if (Bytes[I] >= 0) { 4845 unsigned OpNo = unsigned(Bytes[I]) / SystemZ::VectorBytes; 4846 if (OpNo > ZeroVecOpNo) 4847 Bytes[I] -= SystemZ::VectorBytes; 4848 } 4849 4850 LLVM_DEBUG(dumpBytes(Bytes, "Resulting Bytes vector, zero vector removed:"); 4851 dbgs() << "\n";); 4852 } 4853 4854 SDValue GeneralShuffle::insertUnpackIfPrepared(SelectionDAG &DAG, 4855 const SDLoc &DL, 4856 SDValue Op) { 4857 if (!unpackWasPrepared()) 4858 return Op; 4859 unsigned InBits = UnpackFromEltSize * 8; 4860 EVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBits), 4861 SystemZ::VectorBits / InBits); 4862 SDValue PackedOp = DAG.getNode(ISD::BITCAST, DL, InVT, Op); 4863 unsigned OutBits = InBits * 2; 4864 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(OutBits), 4865 SystemZ::VectorBits / OutBits); 4866 return DAG.getNode(SystemZISD::UNPACKL_HIGH, DL, OutVT, PackedOp); 4867 } 4868 4869 // Return true if the given BUILD_VECTOR is a scalar-to-vector conversion. 4870 static bool isScalarToVector(SDValue Op) { 4871 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I) 4872 if (!Op.getOperand(I).isUndef()) 4873 return false; 4874 return true; 4875 } 4876 4877 // Return a vector of type VT that contains Value in the first element. 4878 // The other elements don't matter. 4879 static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT, 4880 SDValue Value) { 4881 // If we have a constant, replicate it to all elements and let the 4882 // BUILD_VECTOR lowering take care of it. 4883 if (Value.getOpcode() == ISD::Constant || 4884 Value.getOpcode() == ISD::ConstantFP) { 4885 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value); 4886 return DAG.getBuildVector(VT, DL, Ops); 4887 } 4888 if (Value.isUndef()) 4889 return DAG.getUNDEF(VT); 4890 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value); 4891 } 4892 4893 // Return a vector of type VT in which Op0 is in element 0 and Op1 is in 4894 // element 1. Used for cases in which replication is cheap. 4895 static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT, 4896 SDValue Op0, SDValue Op1) { 4897 if (Op0.isUndef()) { 4898 if (Op1.isUndef()) 4899 return DAG.getUNDEF(VT); 4900 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1); 4901 } 4902 if (Op1.isUndef()) 4903 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0); 4904 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT, 4905 buildScalarToVector(DAG, DL, VT, Op0), 4906 buildScalarToVector(DAG, DL, VT, Op1)); 4907 } 4908 4909 // Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64 4910 // vector for them. 4911 static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0, 4912 SDValue Op1) { 4913 if (Op0.isUndef() && Op1.isUndef()) 4914 return DAG.getUNDEF(MVT::v2i64); 4915 // If one of the two inputs is undefined then replicate the other one, 4916 // in order to avoid using another register unnecessarily. 4917 if (Op0.isUndef()) 4918 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1); 4919 else if (Op1.isUndef()) 4920 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0); 4921 else { 4922 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0); 4923 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1); 4924 } 4925 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1); 4926 } 4927 4928 // If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually 4929 // better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for 4930 // the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR 4931 // would benefit from this representation and return it if so. 4932 static SDValue tryBuildVectorShuffle(SelectionDAG &DAG, 4933 BuildVectorSDNode *BVN) { 4934 EVT VT = BVN->getValueType(0); 4935 unsigned NumElements = VT.getVectorNumElements(); 4936 4937 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation 4938 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still 4939 // need a BUILD_VECTOR, add an additional placeholder operand for that 4940 // BUILD_VECTOR and store its operands in ResidueOps. 4941 GeneralShuffle GS(VT); 4942 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps; 4943 bool FoundOne = false; 4944 for (unsigned I = 0; I < NumElements; ++I) { 4945 SDValue Op = BVN->getOperand(I); 4946 if (Op.getOpcode() == ISD::TRUNCATE) 4947 Op = Op.getOperand(0); 4948 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 4949 Op.getOperand(1).getOpcode() == ISD::Constant) { 4950 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 4951 if (!GS.add(Op.getOperand(0), Elem)) 4952 return SDValue(); 4953 FoundOne = true; 4954 } else if (Op.isUndef()) { 4955 GS.addUndef(); 4956 } else { 4957 if (!GS.add(SDValue(), ResidueOps.size())) 4958 return SDValue(); 4959 ResidueOps.push_back(BVN->getOperand(I)); 4960 } 4961 } 4962 4963 // Nothing to do if there are no EXTRACT_VECTOR_ELTs. 4964 if (!FoundOne) 4965 return SDValue(); 4966 4967 // Create the BUILD_VECTOR for the remaining elements, if any. 4968 if (!ResidueOps.empty()) { 4969 while (ResidueOps.size() < NumElements) 4970 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType())); 4971 for (auto &Op : GS.Ops) { 4972 if (!Op.getNode()) { 4973 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps); 4974 break; 4975 } 4976 } 4977 } 4978 return GS.getNode(DAG, SDLoc(BVN)); 4979 } 4980 4981 bool SystemZTargetLowering::isVectorElementLoad(SDValue Op) const { 4982 if (Op.getOpcode() == ISD::LOAD && cast<LoadSDNode>(Op)->isUnindexed()) 4983 return true; 4984 if (Subtarget.hasVectorEnhancements2() && Op.getOpcode() == SystemZISD::LRV) 4985 return true; 4986 return false; 4987 } 4988 4989 // Combine GPR scalar values Elems into a vector of type VT. 4990 SDValue 4991 SystemZTargetLowering::buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT, 4992 SmallVectorImpl<SDValue> &Elems) const { 4993 // See whether there is a single replicated value. 4994 SDValue Single; 4995 unsigned int NumElements = Elems.size(); 4996 unsigned int Count = 0; 4997 for (auto Elem : Elems) { 4998 if (!Elem.isUndef()) { 4999 if (!Single.getNode()) 5000 Single = Elem; 5001 else if (Elem != Single) { 5002 Single = SDValue(); 5003 break; 5004 } 5005 Count += 1; 5006 } 5007 } 5008 // There are three cases here: 5009 // 5010 // - if the only defined element is a loaded one, the best sequence 5011 // is a replicating load. 5012 // 5013 // - otherwise, if the only defined element is an i64 value, we will 5014 // end up with the same VLVGP sequence regardless of whether we short-cut 5015 // for replication or fall through to the later code. 5016 // 5017 // - otherwise, if the only defined element is an i32 or smaller value, 5018 // we would need 2 instructions to replicate it: VLVGP followed by VREPx. 5019 // This is only a win if the single defined element is used more than once. 5020 // In other cases we're better off using a single VLVGx. 5021 if (Single.getNode() && (Count > 1 || isVectorElementLoad(Single))) 5022 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single); 5023 5024 // If all elements are loads, use VLREP/VLEs (below). 5025 bool AllLoads = true; 5026 for (auto Elem : Elems) 5027 if (!isVectorElementLoad(Elem)) { 5028 AllLoads = false; 5029 break; 5030 } 5031 5032 // The best way of building a v2i64 from two i64s is to use VLVGP. 5033 if (VT == MVT::v2i64 && !AllLoads) 5034 return joinDwords(DAG, DL, Elems[0], Elems[1]); 5035 5036 // Use a 64-bit merge high to combine two doubles. 5037 if (VT == MVT::v2f64 && !AllLoads) 5038 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]); 5039 5040 // Build v4f32 values directly from the FPRs: 5041 // 5042 // <Axxx> <Bxxx> <Cxxxx> <Dxxx> 5043 // V V VMRHF 5044 // <ABxx> <CDxx> 5045 // V VMRHG 5046 // <ABCD> 5047 if (VT == MVT::v4f32 && !AllLoads) { 5048 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]); 5049 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]); 5050 // Avoid unnecessary undefs by reusing the other operand. 5051 if (Op01.isUndef()) 5052 Op01 = Op23; 5053 else if (Op23.isUndef()) 5054 Op23 = Op01; 5055 // Merging identical replications is a no-op. 5056 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23) 5057 return Op01; 5058 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01); 5059 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23); 5060 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH, 5061 DL, MVT::v2i64, Op01, Op23); 5062 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 5063 } 5064 5065 // Collect the constant terms. 5066 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue()); 5067 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false); 5068 5069 unsigned NumConstants = 0; 5070 for (unsigned I = 0; I < NumElements; ++I) { 5071 SDValue Elem = Elems[I]; 5072 if (Elem.getOpcode() == ISD::Constant || 5073 Elem.getOpcode() == ISD::ConstantFP) { 5074 NumConstants += 1; 5075 Constants[I] = Elem; 5076 Done[I] = true; 5077 } 5078 } 5079 // If there was at least one constant, fill in the other elements of 5080 // Constants with undefs to get a full vector constant and use that 5081 // as the starting point. 5082 SDValue Result; 5083 SDValue ReplicatedVal; 5084 if (NumConstants > 0) { 5085 for (unsigned I = 0; I < NumElements; ++I) 5086 if (!Constants[I].getNode()) 5087 Constants[I] = DAG.getUNDEF(Elems[I].getValueType()); 5088 Result = DAG.getBuildVector(VT, DL, Constants); 5089 } else { 5090 // Otherwise try to use VLREP or VLVGP to start the sequence in order to 5091 // avoid a false dependency on any previous contents of the vector 5092 // register. 5093 5094 // Use a VLREP if at least one element is a load. Make sure to replicate 5095 // the load with the most elements having its value. 5096 std::map<const SDNode*, unsigned> UseCounts; 5097 SDNode *LoadMaxUses = nullptr; 5098 for (unsigned I = 0; I < NumElements; ++I) 5099 if (isVectorElementLoad(Elems[I])) { 5100 SDNode *Ld = Elems[I].getNode(); 5101 UseCounts[Ld]++; 5102 if (LoadMaxUses == nullptr || UseCounts[LoadMaxUses] < UseCounts[Ld]) 5103 LoadMaxUses = Ld; 5104 } 5105 if (LoadMaxUses != nullptr) { 5106 ReplicatedVal = SDValue(LoadMaxUses, 0); 5107 Result = DAG.getNode(SystemZISD::REPLICATE, DL, VT, ReplicatedVal); 5108 } else { 5109 // Try to use VLVGP. 5110 unsigned I1 = NumElements / 2 - 1; 5111 unsigned I2 = NumElements - 1; 5112 bool Def1 = !Elems[I1].isUndef(); 5113 bool Def2 = !Elems[I2].isUndef(); 5114 if (Def1 || Def2) { 5115 SDValue Elem1 = Elems[Def1 ? I1 : I2]; 5116 SDValue Elem2 = Elems[Def2 ? I2 : I1]; 5117 Result = DAG.getNode(ISD::BITCAST, DL, VT, 5118 joinDwords(DAG, DL, Elem1, Elem2)); 5119 Done[I1] = true; 5120 Done[I2] = true; 5121 } else 5122 Result = DAG.getUNDEF(VT); 5123 } 5124 } 5125 5126 // Use VLVGx to insert the other elements. 5127 for (unsigned I = 0; I < NumElements; ++I) 5128 if (!Done[I] && !Elems[I].isUndef() && Elems[I] != ReplicatedVal) 5129 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I], 5130 DAG.getConstant(I, DL, MVT::i32)); 5131 return Result; 5132 } 5133 5134 SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op, 5135 SelectionDAG &DAG) const { 5136 auto *BVN = cast<BuildVectorSDNode>(Op.getNode()); 5137 SDLoc DL(Op); 5138 EVT VT = Op.getValueType(); 5139 5140 if (BVN->isConstant()) { 5141 if (SystemZVectorConstantInfo(BVN).isVectorConstantLegal(Subtarget)) 5142 return Op; 5143 5144 // Fall back to loading it from memory. 5145 return SDValue(); 5146 } 5147 5148 // See if we should use shuffles to construct the vector from other vectors. 5149 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN)) 5150 return Res; 5151 5152 // Detect SCALAR_TO_VECTOR conversions. 5153 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op)) 5154 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0)); 5155 5156 // Otherwise use buildVector to build the vector up from GPRs. 5157 unsigned NumElements = Op.getNumOperands(); 5158 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements); 5159 for (unsigned I = 0; I < NumElements; ++I) 5160 Ops[I] = Op.getOperand(I); 5161 return buildVector(DAG, DL, VT, Ops); 5162 } 5163 5164 SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op, 5165 SelectionDAG &DAG) const { 5166 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode()); 5167 SDLoc DL(Op); 5168 EVT VT = Op.getValueType(); 5169 unsigned NumElements = VT.getVectorNumElements(); 5170 5171 if (VSN->isSplat()) { 5172 SDValue Op0 = Op.getOperand(0); 5173 unsigned Index = VSN->getSplatIndex(); 5174 assert(Index < VT.getVectorNumElements() && 5175 "Splat index should be defined and in first operand"); 5176 // See whether the value we're splatting is directly available as a scalar. 5177 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) || 5178 Op0.getOpcode() == ISD::BUILD_VECTOR) 5179 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index)); 5180 // Otherwise keep it as a vector-to-vector operation. 5181 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0), 5182 DAG.getTargetConstant(Index, DL, MVT::i32)); 5183 } 5184 5185 GeneralShuffle GS(VT); 5186 for (unsigned I = 0; I < NumElements; ++I) { 5187 int Elt = VSN->getMaskElt(I); 5188 if (Elt < 0) 5189 GS.addUndef(); 5190 else if (!GS.add(Op.getOperand(unsigned(Elt) / NumElements), 5191 unsigned(Elt) % NumElements)) 5192 return SDValue(); 5193 } 5194 return GS.getNode(DAG, SDLoc(VSN)); 5195 } 5196 5197 SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op, 5198 SelectionDAG &DAG) const { 5199 SDLoc DL(Op); 5200 // Just insert the scalar into element 0 of an undefined vector. 5201 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, 5202 Op.getValueType(), DAG.getUNDEF(Op.getValueType()), 5203 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32)); 5204 } 5205 5206 SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op, 5207 SelectionDAG &DAG) const { 5208 // Handle insertions of floating-point values. 5209 SDLoc DL(Op); 5210 SDValue Op0 = Op.getOperand(0); 5211 SDValue Op1 = Op.getOperand(1); 5212 SDValue Op2 = Op.getOperand(2); 5213 EVT VT = Op.getValueType(); 5214 5215 // Insertions into constant indices of a v2f64 can be done using VPDI. 5216 // However, if the inserted value is a bitcast or a constant then it's 5217 // better to use GPRs, as below. 5218 if (VT == MVT::v2f64 && 5219 Op1.getOpcode() != ISD::BITCAST && 5220 Op1.getOpcode() != ISD::ConstantFP && 5221 Op2.getOpcode() == ISD::Constant) { 5222 uint64_t Index = cast<ConstantSDNode>(Op2)->getZExtValue(); 5223 unsigned Mask = VT.getVectorNumElements() - 1; 5224 if (Index <= Mask) 5225 return Op; 5226 } 5227 5228 // Otherwise bitcast to the equivalent integer form and insert via a GPR. 5229 MVT IntVT = MVT::getIntegerVT(VT.getScalarSizeInBits()); 5230 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements()); 5231 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT, 5232 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), 5233 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2); 5234 return DAG.getNode(ISD::BITCAST, DL, VT, Res); 5235 } 5236 5237 SDValue 5238 SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op, 5239 SelectionDAG &DAG) const { 5240 // Handle extractions of floating-point values. 5241 SDLoc DL(Op); 5242 SDValue Op0 = Op.getOperand(0); 5243 SDValue Op1 = Op.getOperand(1); 5244 EVT VT = Op.getValueType(); 5245 EVT VecVT = Op0.getValueType(); 5246 5247 // Extractions of constant indices can be done directly. 5248 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) { 5249 uint64_t Index = CIndexN->getZExtValue(); 5250 unsigned Mask = VecVT.getVectorNumElements() - 1; 5251 if (Index <= Mask) 5252 return Op; 5253 } 5254 5255 // Otherwise bitcast to the equivalent integer form and extract via a GPR. 5256 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits()); 5257 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements()); 5258 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT, 5259 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1); 5260 return DAG.getNode(ISD::BITCAST, DL, VT, Res); 5261 } 5262 5263 SDValue SystemZTargetLowering:: 5264 lowerSIGN_EXTEND_VECTOR_INREG(SDValue Op, SelectionDAG &DAG) const { 5265 SDValue PackedOp = Op.getOperand(0); 5266 EVT OutVT = Op.getValueType(); 5267 EVT InVT = PackedOp.getValueType(); 5268 unsigned ToBits = OutVT.getScalarSizeInBits(); 5269 unsigned FromBits = InVT.getScalarSizeInBits(); 5270 do { 5271 FromBits *= 2; 5272 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits), 5273 SystemZ::VectorBits / FromBits); 5274 PackedOp = 5275 DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(PackedOp), OutVT, PackedOp); 5276 } while (FromBits != ToBits); 5277 return PackedOp; 5278 } 5279 5280 // Lower a ZERO_EXTEND_VECTOR_INREG to a vector shuffle with a zero vector. 5281 SDValue SystemZTargetLowering:: 5282 lowerZERO_EXTEND_VECTOR_INREG(SDValue Op, SelectionDAG &DAG) const { 5283 SDValue PackedOp = Op.getOperand(0); 5284 SDLoc DL(Op); 5285 EVT OutVT = Op.getValueType(); 5286 EVT InVT = PackedOp.getValueType(); 5287 unsigned InNumElts = InVT.getVectorNumElements(); 5288 unsigned OutNumElts = OutVT.getVectorNumElements(); 5289 unsigned NumInPerOut = InNumElts / OutNumElts; 5290 5291 SDValue ZeroVec = 5292 DAG.getSplatVector(InVT, DL, DAG.getConstant(0, DL, InVT.getScalarType())); 5293 5294 SmallVector<int, 16> Mask(InNumElts); 5295 unsigned ZeroVecElt = InNumElts; 5296 for (unsigned PackedElt = 0; PackedElt < OutNumElts; PackedElt++) { 5297 unsigned MaskElt = PackedElt * NumInPerOut; 5298 unsigned End = MaskElt + NumInPerOut - 1; 5299 for (; MaskElt < End; MaskElt++) 5300 Mask[MaskElt] = ZeroVecElt++; 5301 Mask[MaskElt] = PackedElt; 5302 } 5303 SDValue Shuf = DAG.getVectorShuffle(InVT, DL, PackedOp, ZeroVec, Mask); 5304 return DAG.getNode(ISD::BITCAST, DL, OutVT, Shuf); 5305 } 5306 5307 SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG, 5308 unsigned ByScalar) const { 5309 // Look for cases where a vector shift can use the *_BY_SCALAR form. 5310 SDValue Op0 = Op.getOperand(0); 5311 SDValue Op1 = Op.getOperand(1); 5312 SDLoc DL(Op); 5313 EVT VT = Op.getValueType(); 5314 unsigned ElemBitSize = VT.getScalarSizeInBits(); 5315 5316 // See whether the shift vector is a splat represented as BUILD_VECTOR. 5317 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) { 5318 APInt SplatBits, SplatUndef; 5319 unsigned SplatBitSize; 5320 bool HasAnyUndefs; 5321 // Check for constant splats. Use ElemBitSize as the minimum element 5322 // width and reject splats that need wider elements. 5323 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, 5324 ElemBitSize, true) && 5325 SplatBitSize == ElemBitSize) { 5326 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff, 5327 DL, MVT::i32); 5328 return DAG.getNode(ByScalar, DL, VT, Op0, Shift); 5329 } 5330 // Check for variable splats. 5331 BitVector UndefElements; 5332 SDValue Splat = BVN->getSplatValue(&UndefElements); 5333 if (Splat) { 5334 // Since i32 is the smallest legal type, we either need a no-op 5335 // or a truncation. 5336 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat); 5337 return DAG.getNode(ByScalar, DL, VT, Op0, Shift); 5338 } 5339 } 5340 5341 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR, 5342 // and the shift amount is directly available in a GPR. 5343 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) { 5344 if (VSN->isSplat()) { 5345 SDValue VSNOp0 = VSN->getOperand(0); 5346 unsigned Index = VSN->getSplatIndex(); 5347 assert(Index < VT.getVectorNumElements() && 5348 "Splat index should be defined and in first operand"); 5349 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) || 5350 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) { 5351 // Since i32 is the smallest legal type, we either need a no-op 5352 // or a truncation. 5353 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, 5354 VSNOp0.getOperand(Index)); 5355 return DAG.getNode(ByScalar, DL, VT, Op0, Shift); 5356 } 5357 } 5358 } 5359 5360 // Otherwise just treat the current form as legal. 5361 return Op; 5362 } 5363 5364 SDValue SystemZTargetLowering::LowerOperation(SDValue Op, 5365 SelectionDAG &DAG) const { 5366 switch (Op.getOpcode()) { 5367 case ISD::FRAMEADDR: 5368 return lowerFRAMEADDR(Op, DAG); 5369 case ISD::RETURNADDR: 5370 return lowerRETURNADDR(Op, DAG); 5371 case ISD::BR_CC: 5372 return lowerBR_CC(Op, DAG); 5373 case ISD::SELECT_CC: 5374 return lowerSELECT_CC(Op, DAG); 5375 case ISD::SETCC: 5376 return lowerSETCC(Op, DAG); 5377 case ISD::STRICT_FSETCC: 5378 return lowerSTRICT_FSETCC(Op, DAG, false); 5379 case ISD::STRICT_FSETCCS: 5380 return lowerSTRICT_FSETCC(Op, DAG, true); 5381 case ISD::GlobalAddress: 5382 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG); 5383 case ISD::GlobalTLSAddress: 5384 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG); 5385 case ISD::BlockAddress: 5386 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG); 5387 case ISD::JumpTable: 5388 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG); 5389 case ISD::ConstantPool: 5390 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG); 5391 case ISD::BITCAST: 5392 return lowerBITCAST(Op, DAG); 5393 case ISD::VASTART: 5394 return lowerVASTART(Op, DAG); 5395 case ISD::VACOPY: 5396 return lowerVACOPY(Op, DAG); 5397 case ISD::DYNAMIC_STACKALLOC: 5398 return lowerDYNAMIC_STACKALLOC(Op, DAG); 5399 case ISD::GET_DYNAMIC_AREA_OFFSET: 5400 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG); 5401 case ISD::SMUL_LOHI: 5402 return lowerSMUL_LOHI(Op, DAG); 5403 case ISD::UMUL_LOHI: 5404 return lowerUMUL_LOHI(Op, DAG); 5405 case ISD::SDIVREM: 5406 return lowerSDIVREM(Op, DAG); 5407 case ISD::UDIVREM: 5408 return lowerUDIVREM(Op, DAG); 5409 case ISD::SADDO: 5410 case ISD::SSUBO: 5411 case ISD::UADDO: 5412 case ISD::USUBO: 5413 return lowerXALUO(Op, DAG); 5414 case ISD::ADDCARRY: 5415 case ISD::SUBCARRY: 5416 return lowerADDSUBCARRY(Op, DAG); 5417 case ISD::OR: 5418 return lowerOR(Op, DAG); 5419 case ISD::CTPOP: 5420 return lowerCTPOP(Op, DAG); 5421 case ISD::ATOMIC_FENCE: 5422 return lowerATOMIC_FENCE(Op, DAG); 5423 case ISD::ATOMIC_SWAP: 5424 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW); 5425 case ISD::ATOMIC_STORE: 5426 return lowerATOMIC_STORE(Op, DAG); 5427 case ISD::ATOMIC_LOAD: 5428 return lowerATOMIC_LOAD(Op, DAG); 5429 case ISD::ATOMIC_LOAD_ADD: 5430 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD); 5431 case ISD::ATOMIC_LOAD_SUB: 5432 return lowerATOMIC_LOAD_SUB(Op, DAG); 5433 case ISD::ATOMIC_LOAD_AND: 5434 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND); 5435 case ISD::ATOMIC_LOAD_OR: 5436 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR); 5437 case ISD::ATOMIC_LOAD_XOR: 5438 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR); 5439 case ISD::ATOMIC_LOAD_NAND: 5440 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND); 5441 case ISD::ATOMIC_LOAD_MIN: 5442 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN); 5443 case ISD::ATOMIC_LOAD_MAX: 5444 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX); 5445 case ISD::ATOMIC_LOAD_UMIN: 5446 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN); 5447 case ISD::ATOMIC_LOAD_UMAX: 5448 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX); 5449 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: 5450 return lowerATOMIC_CMP_SWAP(Op, DAG); 5451 case ISD::STACKSAVE: 5452 return lowerSTACKSAVE(Op, DAG); 5453 case ISD::STACKRESTORE: 5454 return lowerSTACKRESTORE(Op, DAG); 5455 case ISD::PREFETCH: 5456 return lowerPREFETCH(Op, DAG); 5457 case ISD::INTRINSIC_W_CHAIN: 5458 return lowerINTRINSIC_W_CHAIN(Op, DAG); 5459 case ISD::INTRINSIC_WO_CHAIN: 5460 return lowerINTRINSIC_WO_CHAIN(Op, DAG); 5461 case ISD::BUILD_VECTOR: 5462 return lowerBUILD_VECTOR(Op, DAG); 5463 case ISD::VECTOR_SHUFFLE: 5464 return lowerVECTOR_SHUFFLE(Op, DAG); 5465 case ISD::SCALAR_TO_VECTOR: 5466 return lowerSCALAR_TO_VECTOR(Op, DAG); 5467 case ISD::INSERT_VECTOR_ELT: 5468 return lowerINSERT_VECTOR_ELT(Op, DAG); 5469 case ISD::EXTRACT_VECTOR_ELT: 5470 return lowerEXTRACT_VECTOR_ELT(Op, DAG); 5471 case ISD::SIGN_EXTEND_VECTOR_INREG: 5472 return lowerSIGN_EXTEND_VECTOR_INREG(Op, DAG); 5473 case ISD::ZERO_EXTEND_VECTOR_INREG: 5474 return lowerZERO_EXTEND_VECTOR_INREG(Op, DAG); 5475 case ISD::SHL: 5476 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR); 5477 case ISD::SRL: 5478 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR); 5479 case ISD::SRA: 5480 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR); 5481 default: 5482 llvm_unreachable("Unexpected node to lower"); 5483 } 5484 } 5485 5486 // Lower operations with invalid operand or result types (currently used 5487 // only for 128-bit integer types). 5488 5489 static SDValue lowerI128ToGR128(SelectionDAG &DAG, SDValue In) { 5490 SDLoc DL(In); 5491 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In, 5492 DAG.getIntPtrConstant(0, DL)); 5493 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In, 5494 DAG.getIntPtrConstant(1, DL)); 5495 SDNode *Pair = DAG.getMachineNode(SystemZ::PAIR128, DL, 5496 MVT::Untyped, Hi, Lo); 5497 return SDValue(Pair, 0); 5498 } 5499 5500 static SDValue lowerGR128ToI128(SelectionDAG &DAG, SDValue In) { 5501 SDLoc DL(In); 5502 SDValue Hi = DAG.getTargetExtractSubreg(SystemZ::subreg_h64, 5503 DL, MVT::i64, In); 5504 SDValue Lo = DAG.getTargetExtractSubreg(SystemZ::subreg_l64, 5505 DL, MVT::i64, In); 5506 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i128, Lo, Hi); 5507 } 5508 5509 void 5510 SystemZTargetLowering::LowerOperationWrapper(SDNode *N, 5511 SmallVectorImpl<SDValue> &Results, 5512 SelectionDAG &DAG) const { 5513 switch (N->getOpcode()) { 5514 case ISD::ATOMIC_LOAD: { 5515 SDLoc DL(N); 5516 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other); 5517 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) }; 5518 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand(); 5519 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_LOAD_128, 5520 DL, Tys, Ops, MVT::i128, MMO); 5521 Results.push_back(lowerGR128ToI128(DAG, Res)); 5522 Results.push_back(Res.getValue(1)); 5523 break; 5524 } 5525 case ISD::ATOMIC_STORE: { 5526 SDLoc DL(N); 5527 SDVTList Tys = DAG.getVTList(MVT::Other); 5528 SDValue Ops[] = { N->getOperand(0), 5529 lowerI128ToGR128(DAG, N->getOperand(2)), 5530 N->getOperand(1) }; 5531 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand(); 5532 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_STORE_128, 5533 DL, Tys, Ops, MVT::i128, MMO); 5534 // We have to enforce sequential consistency by performing a 5535 // serialization operation after the store. 5536 if (cast<AtomicSDNode>(N)->getOrdering() == 5537 AtomicOrdering::SequentiallyConsistent) 5538 Res = SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, 5539 MVT::Other, Res), 0); 5540 Results.push_back(Res); 5541 break; 5542 } 5543 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: { 5544 SDLoc DL(N); 5545 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other); 5546 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), 5547 lowerI128ToGR128(DAG, N->getOperand(2)), 5548 lowerI128ToGR128(DAG, N->getOperand(3)) }; 5549 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand(); 5550 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP_128, 5551 DL, Tys, Ops, MVT::i128, MMO); 5552 SDValue Success = emitSETCC(DAG, DL, Res.getValue(1), 5553 SystemZ::CCMASK_CS, SystemZ::CCMASK_CS_EQ); 5554 Success = DAG.getZExtOrTrunc(Success, DL, N->getValueType(1)); 5555 Results.push_back(lowerGR128ToI128(DAG, Res)); 5556 Results.push_back(Success); 5557 Results.push_back(Res.getValue(2)); 5558 break; 5559 } 5560 default: 5561 llvm_unreachable("Unexpected node to lower"); 5562 } 5563 } 5564 5565 void 5566 SystemZTargetLowering::ReplaceNodeResults(SDNode *N, 5567 SmallVectorImpl<SDValue> &Results, 5568 SelectionDAG &DAG) const { 5569 return LowerOperationWrapper(N, Results, DAG); 5570 } 5571 5572 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const { 5573 #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME 5574 switch ((SystemZISD::NodeType)Opcode) { 5575 case SystemZISD::FIRST_NUMBER: break; 5576 OPCODE(RET_FLAG); 5577 OPCODE(CALL); 5578 OPCODE(SIBCALL); 5579 OPCODE(TLS_GDCALL); 5580 OPCODE(TLS_LDCALL); 5581 OPCODE(PCREL_WRAPPER); 5582 OPCODE(PCREL_OFFSET); 5583 OPCODE(ICMP); 5584 OPCODE(FCMP); 5585 OPCODE(STRICT_FCMP); 5586 OPCODE(STRICT_FCMPS); 5587 OPCODE(TM); 5588 OPCODE(BR_CCMASK); 5589 OPCODE(SELECT_CCMASK); 5590 OPCODE(ADJDYNALLOC); 5591 OPCODE(PROBED_ALLOCA); 5592 OPCODE(POPCNT); 5593 OPCODE(SMUL_LOHI); 5594 OPCODE(UMUL_LOHI); 5595 OPCODE(SDIVREM); 5596 OPCODE(UDIVREM); 5597 OPCODE(SADDO); 5598 OPCODE(SSUBO); 5599 OPCODE(UADDO); 5600 OPCODE(USUBO); 5601 OPCODE(ADDCARRY); 5602 OPCODE(SUBCARRY); 5603 OPCODE(GET_CCMASK); 5604 OPCODE(MVC); 5605 OPCODE(MVC_LOOP); 5606 OPCODE(NC); 5607 OPCODE(NC_LOOP); 5608 OPCODE(OC); 5609 OPCODE(OC_LOOP); 5610 OPCODE(XC); 5611 OPCODE(XC_LOOP); 5612 OPCODE(CLC); 5613 OPCODE(CLC_LOOP); 5614 OPCODE(STPCPY); 5615 OPCODE(STRCMP); 5616 OPCODE(SEARCH_STRING); 5617 OPCODE(IPM); 5618 OPCODE(MEMBARRIER); 5619 OPCODE(TBEGIN); 5620 OPCODE(TBEGIN_NOFLOAT); 5621 OPCODE(TEND); 5622 OPCODE(BYTE_MASK); 5623 OPCODE(ROTATE_MASK); 5624 OPCODE(REPLICATE); 5625 OPCODE(JOIN_DWORDS); 5626 OPCODE(SPLAT); 5627 OPCODE(MERGE_HIGH); 5628 OPCODE(MERGE_LOW); 5629 OPCODE(SHL_DOUBLE); 5630 OPCODE(PERMUTE_DWORDS); 5631 OPCODE(PERMUTE); 5632 OPCODE(PACK); 5633 OPCODE(PACKS_CC); 5634 OPCODE(PACKLS_CC); 5635 OPCODE(UNPACK_HIGH); 5636 OPCODE(UNPACKL_HIGH); 5637 OPCODE(UNPACK_LOW); 5638 OPCODE(UNPACKL_LOW); 5639 OPCODE(VSHL_BY_SCALAR); 5640 OPCODE(VSRL_BY_SCALAR); 5641 OPCODE(VSRA_BY_SCALAR); 5642 OPCODE(VSUM); 5643 OPCODE(VICMPE); 5644 OPCODE(VICMPH); 5645 OPCODE(VICMPHL); 5646 OPCODE(VICMPES); 5647 OPCODE(VICMPHS); 5648 OPCODE(VICMPHLS); 5649 OPCODE(VFCMPE); 5650 OPCODE(STRICT_VFCMPE); 5651 OPCODE(STRICT_VFCMPES); 5652 OPCODE(VFCMPH); 5653 OPCODE(STRICT_VFCMPH); 5654 OPCODE(STRICT_VFCMPHS); 5655 OPCODE(VFCMPHE); 5656 OPCODE(STRICT_VFCMPHE); 5657 OPCODE(STRICT_VFCMPHES); 5658 OPCODE(VFCMPES); 5659 OPCODE(VFCMPHS); 5660 OPCODE(VFCMPHES); 5661 OPCODE(VFTCI); 5662 OPCODE(VEXTEND); 5663 OPCODE(STRICT_VEXTEND); 5664 OPCODE(VROUND); 5665 OPCODE(STRICT_VROUND); 5666 OPCODE(VTM); 5667 OPCODE(VFAE_CC); 5668 OPCODE(VFAEZ_CC); 5669 OPCODE(VFEE_CC); 5670 OPCODE(VFEEZ_CC); 5671 OPCODE(VFENE_CC); 5672 OPCODE(VFENEZ_CC); 5673 OPCODE(VISTR_CC); 5674 OPCODE(VSTRC_CC); 5675 OPCODE(VSTRCZ_CC); 5676 OPCODE(VSTRS_CC); 5677 OPCODE(VSTRSZ_CC); 5678 OPCODE(TDC); 5679 OPCODE(ATOMIC_SWAPW); 5680 OPCODE(ATOMIC_LOADW_ADD); 5681 OPCODE(ATOMIC_LOADW_SUB); 5682 OPCODE(ATOMIC_LOADW_AND); 5683 OPCODE(ATOMIC_LOADW_OR); 5684 OPCODE(ATOMIC_LOADW_XOR); 5685 OPCODE(ATOMIC_LOADW_NAND); 5686 OPCODE(ATOMIC_LOADW_MIN); 5687 OPCODE(ATOMIC_LOADW_MAX); 5688 OPCODE(ATOMIC_LOADW_UMIN); 5689 OPCODE(ATOMIC_LOADW_UMAX); 5690 OPCODE(ATOMIC_CMP_SWAPW); 5691 OPCODE(ATOMIC_CMP_SWAP); 5692 OPCODE(ATOMIC_LOAD_128); 5693 OPCODE(ATOMIC_STORE_128); 5694 OPCODE(ATOMIC_CMP_SWAP_128); 5695 OPCODE(LRV); 5696 OPCODE(STRV); 5697 OPCODE(VLER); 5698 OPCODE(VSTER); 5699 OPCODE(PREFETCH); 5700 } 5701 return nullptr; 5702 #undef OPCODE 5703 } 5704 5705 // Return true if VT is a vector whose elements are a whole number of bytes 5706 // in width. Also check for presence of vector support. 5707 bool SystemZTargetLowering::canTreatAsByteVector(EVT VT) const { 5708 if (!Subtarget.hasVector()) 5709 return false; 5710 5711 return VT.isVector() && VT.getScalarSizeInBits() % 8 == 0 && VT.isSimple(); 5712 } 5713 5714 // Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT 5715 // producing a result of type ResVT. Op is a possibly bitcast version 5716 // of the input vector and Index is the index (based on type VecVT) that 5717 // should be extracted. Return the new extraction if a simplification 5718 // was possible or if Force is true. 5719 SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT, 5720 EVT VecVT, SDValue Op, 5721 unsigned Index, 5722 DAGCombinerInfo &DCI, 5723 bool Force) const { 5724 SelectionDAG &DAG = DCI.DAG; 5725 5726 // The number of bytes being extracted. 5727 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize(); 5728 5729 for (;;) { 5730 unsigned Opcode = Op.getOpcode(); 5731 if (Opcode == ISD::BITCAST) 5732 // Look through bitcasts. 5733 Op = Op.getOperand(0); 5734 else if ((Opcode == ISD::VECTOR_SHUFFLE || Opcode == SystemZISD::SPLAT) && 5735 canTreatAsByteVector(Op.getValueType())) { 5736 // Get a VPERM-like permute mask and see whether the bytes covered 5737 // by the extracted element are a contiguous sequence from one 5738 // source operand. 5739 SmallVector<int, SystemZ::VectorBytes> Bytes; 5740 if (!getVPermMask(Op, Bytes)) 5741 break; 5742 int First; 5743 if (!getShuffleInput(Bytes, Index * BytesPerElement, 5744 BytesPerElement, First)) 5745 break; 5746 if (First < 0) 5747 return DAG.getUNDEF(ResVT); 5748 // Make sure the contiguous sequence starts at a multiple of the 5749 // original element size. 5750 unsigned Byte = unsigned(First) % Bytes.size(); 5751 if (Byte % BytesPerElement != 0) 5752 break; 5753 // We can get the extracted value directly from an input. 5754 Index = Byte / BytesPerElement; 5755 Op = Op.getOperand(unsigned(First) / Bytes.size()); 5756 Force = true; 5757 } else if (Opcode == ISD::BUILD_VECTOR && 5758 canTreatAsByteVector(Op.getValueType())) { 5759 // We can only optimize this case if the BUILD_VECTOR elements are 5760 // at least as wide as the extracted value. 5761 EVT OpVT = Op.getValueType(); 5762 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize(); 5763 if (OpBytesPerElement < BytesPerElement) 5764 break; 5765 // Make sure that the least-significant bit of the extracted value 5766 // is the least significant bit of an input. 5767 unsigned End = (Index + 1) * BytesPerElement; 5768 if (End % OpBytesPerElement != 0) 5769 break; 5770 // We're extracting the low part of one operand of the BUILD_VECTOR. 5771 Op = Op.getOperand(End / OpBytesPerElement - 1); 5772 if (!Op.getValueType().isInteger()) { 5773 EVT VT = MVT::getIntegerVT(Op.getValueSizeInBits()); 5774 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op); 5775 DCI.AddToWorklist(Op.getNode()); 5776 } 5777 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits()); 5778 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op); 5779 if (VT != ResVT) { 5780 DCI.AddToWorklist(Op.getNode()); 5781 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op); 5782 } 5783 return Op; 5784 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG || 5785 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG || 5786 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) && 5787 canTreatAsByteVector(Op.getValueType()) && 5788 canTreatAsByteVector(Op.getOperand(0).getValueType())) { 5789 // Make sure that only the unextended bits are significant. 5790 EVT ExtVT = Op.getValueType(); 5791 EVT OpVT = Op.getOperand(0).getValueType(); 5792 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize(); 5793 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize(); 5794 unsigned Byte = Index * BytesPerElement; 5795 unsigned SubByte = Byte % ExtBytesPerElement; 5796 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement; 5797 if (SubByte < MinSubByte || 5798 SubByte + BytesPerElement > ExtBytesPerElement) 5799 break; 5800 // Get the byte offset of the unextended element 5801 Byte = Byte / ExtBytesPerElement * OpBytesPerElement; 5802 // ...then add the byte offset relative to that element. 5803 Byte += SubByte - MinSubByte; 5804 if (Byte % BytesPerElement != 0) 5805 break; 5806 Op = Op.getOperand(0); 5807 Index = Byte / BytesPerElement; 5808 Force = true; 5809 } else 5810 break; 5811 } 5812 if (Force) { 5813 if (Op.getValueType() != VecVT) { 5814 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op); 5815 DCI.AddToWorklist(Op.getNode()); 5816 } 5817 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op, 5818 DAG.getConstant(Index, DL, MVT::i32)); 5819 } 5820 return SDValue(); 5821 } 5822 5823 // Optimize vector operations in scalar value Op on the basis that Op 5824 // is truncated to TruncVT. 5825 SDValue SystemZTargetLowering::combineTruncateExtract( 5826 const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const { 5827 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into 5828 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements 5829 // of type TruncVT. 5830 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 5831 TruncVT.getSizeInBits() % 8 == 0) { 5832 SDValue Vec = Op.getOperand(0); 5833 EVT VecVT = Vec.getValueType(); 5834 if (canTreatAsByteVector(VecVT)) { 5835 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 5836 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize(); 5837 unsigned TruncBytes = TruncVT.getStoreSize(); 5838 if (BytesPerElement % TruncBytes == 0) { 5839 // Calculate the value of Y' in the above description. We are 5840 // splitting the original elements into Scale equal-sized pieces 5841 // and for truncation purposes want the last (least-significant) 5842 // of these pieces for IndexN. This is easiest to do by calculating 5843 // the start index of the following element and then subtracting 1. 5844 unsigned Scale = BytesPerElement / TruncBytes; 5845 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1; 5846 5847 // Defer the creation of the bitcast from X to combineExtract, 5848 // which might be able to optimize the extraction. 5849 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8), 5850 VecVT.getStoreSize() / TruncBytes); 5851 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT); 5852 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true); 5853 } 5854 } 5855 } 5856 } 5857 return SDValue(); 5858 } 5859 5860 SDValue SystemZTargetLowering::combineZERO_EXTEND( 5861 SDNode *N, DAGCombinerInfo &DCI) const { 5862 // Convert (zext (select_ccmask C1, C2)) into (select_ccmask C1', C2') 5863 SelectionDAG &DAG = DCI.DAG; 5864 SDValue N0 = N->getOperand(0); 5865 EVT VT = N->getValueType(0); 5866 if (N0.getOpcode() == SystemZISD::SELECT_CCMASK) { 5867 auto *TrueOp = dyn_cast<ConstantSDNode>(N0.getOperand(0)); 5868 auto *FalseOp = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 5869 if (TrueOp && FalseOp) { 5870 SDLoc DL(N0); 5871 SDValue Ops[] = { DAG.getConstant(TrueOp->getZExtValue(), DL, VT), 5872 DAG.getConstant(FalseOp->getZExtValue(), DL, VT), 5873 N0.getOperand(2), N0.getOperand(3), N0.getOperand(4) }; 5874 SDValue NewSelect = DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VT, Ops); 5875 // If N0 has multiple uses, change other uses as well. 5876 if (!N0.hasOneUse()) { 5877 SDValue TruncSelect = 5878 DAG.getNode(ISD::TRUNCATE, DL, N0.getValueType(), NewSelect); 5879 DCI.CombineTo(N0.getNode(), TruncSelect); 5880 } 5881 return NewSelect; 5882 } 5883 } 5884 return SDValue(); 5885 } 5886 5887 SDValue SystemZTargetLowering::combineSIGN_EXTEND_INREG( 5888 SDNode *N, DAGCombinerInfo &DCI) const { 5889 // Convert (sext_in_reg (setcc LHS, RHS, COND), i1) 5890 // and (sext_in_reg (any_extend (setcc LHS, RHS, COND)), i1) 5891 // into (select_cc LHS, RHS, -1, 0, COND) 5892 SelectionDAG &DAG = DCI.DAG; 5893 SDValue N0 = N->getOperand(0); 5894 EVT VT = N->getValueType(0); 5895 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); 5896 if (N0.hasOneUse() && N0.getOpcode() == ISD::ANY_EXTEND) 5897 N0 = N0.getOperand(0); 5898 if (EVT == MVT::i1 && N0.hasOneUse() && N0.getOpcode() == ISD::SETCC) { 5899 SDLoc DL(N0); 5900 SDValue Ops[] = { N0.getOperand(0), N0.getOperand(1), 5901 DAG.getConstant(-1, DL, VT), DAG.getConstant(0, DL, VT), 5902 N0.getOperand(2) }; 5903 return DAG.getNode(ISD::SELECT_CC, DL, VT, Ops); 5904 } 5905 return SDValue(); 5906 } 5907 5908 SDValue SystemZTargetLowering::combineSIGN_EXTEND( 5909 SDNode *N, DAGCombinerInfo &DCI) const { 5910 // Convert (sext (ashr (shl X, C1), C2)) to 5911 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as 5912 // cheap as narrower ones. 5913 SelectionDAG &DAG = DCI.DAG; 5914 SDValue N0 = N->getOperand(0); 5915 EVT VT = N->getValueType(0); 5916 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) { 5917 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 5918 SDValue Inner = N0.getOperand(0); 5919 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) { 5920 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) { 5921 unsigned Extra = (VT.getSizeInBits() - N0.getValueSizeInBits()); 5922 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra; 5923 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra; 5924 EVT ShiftVT = N0.getOperand(1).getValueType(); 5925 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT, 5926 Inner.getOperand(0)); 5927 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext, 5928 DAG.getConstant(NewShlAmt, SDLoc(Inner), 5929 ShiftVT)); 5930 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, 5931 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT)); 5932 } 5933 } 5934 } 5935 return SDValue(); 5936 } 5937 5938 SDValue SystemZTargetLowering::combineMERGE( 5939 SDNode *N, DAGCombinerInfo &DCI) const { 5940 SelectionDAG &DAG = DCI.DAG; 5941 unsigned Opcode = N->getOpcode(); 5942 SDValue Op0 = N->getOperand(0); 5943 SDValue Op1 = N->getOperand(1); 5944 if (Op0.getOpcode() == ISD::BITCAST) 5945 Op0 = Op0.getOperand(0); 5946 if (ISD::isBuildVectorAllZeros(Op0.getNode())) { 5947 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF 5948 // for v4f32. 5949 if (Op1 == N->getOperand(0)) 5950 return Op1; 5951 // (z_merge_? 0, X) -> (z_unpackl_? 0, X). 5952 EVT VT = Op1.getValueType(); 5953 unsigned ElemBytes = VT.getVectorElementType().getStoreSize(); 5954 if (ElemBytes <= 4) { 5955 Opcode = (Opcode == SystemZISD::MERGE_HIGH ? 5956 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW); 5957 EVT InVT = VT.changeVectorElementTypeToInteger(); 5958 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16), 5959 SystemZ::VectorBytes / ElemBytes / 2); 5960 if (VT != InVT) { 5961 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1); 5962 DCI.AddToWorklist(Op1.getNode()); 5963 } 5964 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1); 5965 DCI.AddToWorklist(Op.getNode()); 5966 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); 5967 } 5968 } 5969 return SDValue(); 5970 } 5971 5972 SDValue SystemZTargetLowering::combineLOAD( 5973 SDNode *N, DAGCombinerInfo &DCI) const { 5974 SelectionDAG &DAG = DCI.DAG; 5975 EVT LdVT = N->getValueType(0); 5976 if (LdVT.isVector() || LdVT.isInteger()) 5977 return SDValue(); 5978 // Transform a scalar load that is REPLICATEd as well as having other 5979 // use(s) to the form where the other use(s) use the first element of the 5980 // REPLICATE instead of the load. Otherwise instruction selection will not 5981 // produce a VLREP. Avoid extracting to a GPR, so only do this for floating 5982 // point loads. 5983 5984 SDValue Replicate; 5985 SmallVector<SDNode*, 8> OtherUses; 5986 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); 5987 UI != UE; ++UI) { 5988 if (UI->getOpcode() == SystemZISD::REPLICATE) { 5989 if (Replicate) 5990 return SDValue(); // Should never happen 5991 Replicate = SDValue(*UI, 0); 5992 } 5993 else if (UI.getUse().getResNo() == 0) 5994 OtherUses.push_back(*UI); 5995 } 5996 if (!Replicate || OtherUses.empty()) 5997 return SDValue(); 5998 5999 SDLoc DL(N); 6000 SDValue Extract0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, LdVT, 6001 Replicate, DAG.getConstant(0, DL, MVT::i32)); 6002 // Update uses of the loaded Value while preserving old chains. 6003 for (SDNode *U : OtherUses) { 6004 SmallVector<SDValue, 8> Ops; 6005 for (SDValue Op : U->ops()) 6006 Ops.push_back((Op.getNode() == N && Op.getResNo() == 0) ? Extract0 : Op); 6007 DAG.UpdateNodeOperands(U, Ops); 6008 } 6009 return SDValue(N, 0); 6010 } 6011 6012 bool SystemZTargetLowering::canLoadStoreByteSwapped(EVT VT) const { 6013 if (VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) 6014 return true; 6015 if (Subtarget.hasVectorEnhancements2()) 6016 if (VT == MVT::v8i16 || VT == MVT::v4i32 || VT == MVT::v2i64) 6017 return true; 6018 return false; 6019 } 6020 6021 static bool isVectorElementSwap(ArrayRef<int> M, EVT VT) { 6022 if (!VT.isVector() || !VT.isSimple() || 6023 VT.getSizeInBits() != 128 || 6024 VT.getScalarSizeInBits() % 8 != 0) 6025 return false; 6026 6027 unsigned NumElts = VT.getVectorNumElements(); 6028 for (unsigned i = 0; i < NumElts; ++i) { 6029 if (M[i] < 0) continue; // ignore UNDEF indices 6030 if ((unsigned) M[i] != NumElts - 1 - i) 6031 return false; 6032 } 6033 6034 return true; 6035 } 6036 6037 SDValue SystemZTargetLowering::combineSTORE( 6038 SDNode *N, DAGCombinerInfo &DCI) const { 6039 SelectionDAG &DAG = DCI.DAG; 6040 auto *SN = cast<StoreSDNode>(N); 6041 auto &Op1 = N->getOperand(1); 6042 EVT MemVT = SN->getMemoryVT(); 6043 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better 6044 // for the extraction to be done on a vMiN value, so that we can use VSTE. 6045 // If X has wider elements then convert it to: 6046 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z). 6047 if (MemVT.isInteger() && SN->isTruncatingStore()) { 6048 if (SDValue Value = 6049 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) { 6050 DCI.AddToWorklist(Value.getNode()); 6051 6052 // Rewrite the store with the new form of stored value. 6053 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value, 6054 SN->getBasePtr(), SN->getMemoryVT(), 6055 SN->getMemOperand()); 6056 } 6057 } 6058 // Combine STORE (BSWAP) into STRVH/STRV/STRVG/VSTBR 6059 if (!SN->isTruncatingStore() && 6060 Op1.getOpcode() == ISD::BSWAP && 6061 Op1.getNode()->hasOneUse() && 6062 canLoadStoreByteSwapped(Op1.getValueType())) { 6063 6064 SDValue BSwapOp = Op1.getOperand(0); 6065 6066 if (BSwapOp.getValueType() == MVT::i16) 6067 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp); 6068 6069 SDValue Ops[] = { 6070 N->getOperand(0), BSwapOp, N->getOperand(2) 6071 }; 6072 6073 return 6074 DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other), 6075 Ops, MemVT, SN->getMemOperand()); 6076 } 6077 // Combine STORE (element-swap) into VSTER 6078 if (!SN->isTruncatingStore() && 6079 Op1.getOpcode() == ISD::VECTOR_SHUFFLE && 6080 Op1.getNode()->hasOneUse() && 6081 Subtarget.hasVectorEnhancements2()) { 6082 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op1.getNode()); 6083 ArrayRef<int> ShuffleMask = SVN->getMask(); 6084 if (isVectorElementSwap(ShuffleMask, Op1.getValueType())) { 6085 SDValue Ops[] = { 6086 N->getOperand(0), Op1.getOperand(0), N->getOperand(2) 6087 }; 6088 6089 return DAG.getMemIntrinsicNode(SystemZISD::VSTER, SDLoc(N), 6090 DAG.getVTList(MVT::Other), 6091 Ops, MemVT, SN->getMemOperand()); 6092 } 6093 } 6094 6095 return SDValue(); 6096 } 6097 6098 SDValue SystemZTargetLowering::combineVECTOR_SHUFFLE( 6099 SDNode *N, DAGCombinerInfo &DCI) const { 6100 SelectionDAG &DAG = DCI.DAG; 6101 // Combine element-swap (LOAD) into VLER 6102 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) && 6103 N->getOperand(0).hasOneUse() && 6104 Subtarget.hasVectorEnhancements2()) { 6105 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 6106 ArrayRef<int> ShuffleMask = SVN->getMask(); 6107 if (isVectorElementSwap(ShuffleMask, N->getValueType(0))) { 6108 SDValue Load = N->getOperand(0); 6109 LoadSDNode *LD = cast<LoadSDNode>(Load); 6110 6111 // Create the element-swapping load. 6112 SDValue Ops[] = { 6113 LD->getChain(), // Chain 6114 LD->getBasePtr() // Ptr 6115 }; 6116 SDValue ESLoad = 6117 DAG.getMemIntrinsicNode(SystemZISD::VLER, SDLoc(N), 6118 DAG.getVTList(LD->getValueType(0), MVT::Other), 6119 Ops, LD->getMemoryVT(), LD->getMemOperand()); 6120 6121 // First, combine the VECTOR_SHUFFLE away. This makes the value produced 6122 // by the load dead. 6123 DCI.CombineTo(N, ESLoad); 6124 6125 // Next, combine the load away, we give it a bogus result value but a real 6126 // chain result. The result value is dead because the shuffle is dead. 6127 DCI.CombineTo(Load.getNode(), ESLoad, ESLoad.getValue(1)); 6128 6129 // Return N so it doesn't get rechecked! 6130 return SDValue(N, 0); 6131 } 6132 } 6133 6134 return SDValue(); 6135 } 6136 6137 SDValue SystemZTargetLowering::combineEXTRACT_VECTOR_ELT( 6138 SDNode *N, DAGCombinerInfo &DCI) const { 6139 SelectionDAG &DAG = DCI.DAG; 6140 6141 if (!Subtarget.hasVector()) 6142 return SDValue(); 6143 6144 // Look through bitcasts that retain the number of vector elements. 6145 SDValue Op = N->getOperand(0); 6146 if (Op.getOpcode() == ISD::BITCAST && 6147 Op.getValueType().isVector() && 6148 Op.getOperand(0).getValueType().isVector() && 6149 Op.getValueType().getVectorNumElements() == 6150 Op.getOperand(0).getValueType().getVectorNumElements()) 6151 Op = Op.getOperand(0); 6152 6153 // Pull BSWAP out of a vector extraction. 6154 if (Op.getOpcode() == ISD::BSWAP && Op.hasOneUse()) { 6155 EVT VecVT = Op.getValueType(); 6156 EVT EltVT = VecVT.getVectorElementType(); 6157 Op = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), EltVT, 6158 Op.getOperand(0), N->getOperand(1)); 6159 DCI.AddToWorklist(Op.getNode()); 6160 Op = DAG.getNode(ISD::BSWAP, SDLoc(N), EltVT, Op); 6161 if (EltVT != N->getValueType(0)) { 6162 DCI.AddToWorklist(Op.getNode()); 6163 Op = DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0), Op); 6164 } 6165 return Op; 6166 } 6167 6168 // Try to simplify a vector extraction. 6169 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) { 6170 SDValue Op0 = N->getOperand(0); 6171 EVT VecVT = Op0.getValueType(); 6172 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0, 6173 IndexN->getZExtValue(), DCI, false); 6174 } 6175 return SDValue(); 6176 } 6177 6178 SDValue SystemZTargetLowering::combineJOIN_DWORDS( 6179 SDNode *N, DAGCombinerInfo &DCI) const { 6180 SelectionDAG &DAG = DCI.DAG; 6181 // (join_dwords X, X) == (replicate X) 6182 if (N->getOperand(0) == N->getOperand(1)) 6183 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0), 6184 N->getOperand(0)); 6185 return SDValue(); 6186 } 6187 6188 static SDValue MergeInputChains(SDNode *N1, SDNode *N2) { 6189 SDValue Chain1 = N1->getOperand(0); 6190 SDValue Chain2 = N2->getOperand(0); 6191 6192 // Trivial case: both nodes take the same chain. 6193 if (Chain1 == Chain2) 6194 return Chain1; 6195 6196 // FIXME - we could handle more complex cases via TokenFactor, 6197 // assuming we can verify that this would not create a cycle. 6198 return SDValue(); 6199 } 6200 6201 SDValue SystemZTargetLowering::combineFP_ROUND( 6202 SDNode *N, DAGCombinerInfo &DCI) const { 6203 6204 if (!Subtarget.hasVector()) 6205 return SDValue(); 6206 6207 // (fpround (extract_vector_elt X 0)) 6208 // (fpround (extract_vector_elt X 1)) -> 6209 // (extract_vector_elt (VROUND X) 0) 6210 // (extract_vector_elt (VROUND X) 2) 6211 // 6212 // This is a special case since the target doesn't really support v2f32s. 6213 unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0; 6214 SelectionDAG &DAG = DCI.DAG; 6215 SDValue Op0 = N->getOperand(OpNo); 6216 if (N->getValueType(0) == MVT::f32 && 6217 Op0.hasOneUse() && 6218 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 6219 Op0.getOperand(0).getValueType() == MVT::v2f64 && 6220 Op0.getOperand(1).getOpcode() == ISD::Constant && 6221 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) { 6222 SDValue Vec = Op0.getOperand(0); 6223 for (auto *U : Vec->uses()) { 6224 if (U != Op0.getNode() && 6225 U->hasOneUse() && 6226 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 6227 U->getOperand(0) == Vec && 6228 U->getOperand(1).getOpcode() == ISD::Constant && 6229 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) { 6230 SDValue OtherRound = SDValue(*U->use_begin(), 0); 6231 if (OtherRound.getOpcode() == N->getOpcode() && 6232 OtherRound.getOperand(OpNo) == SDValue(U, 0) && 6233 OtherRound.getValueType() == MVT::f32) { 6234 SDValue VRound, Chain; 6235 if (N->isStrictFPOpcode()) { 6236 Chain = MergeInputChains(N, OtherRound.getNode()); 6237 if (!Chain) 6238 continue; 6239 VRound = DAG.getNode(SystemZISD::STRICT_VROUND, SDLoc(N), 6240 {MVT::v4f32, MVT::Other}, {Chain, Vec}); 6241 Chain = VRound.getValue(1); 6242 } else 6243 VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N), 6244 MVT::v4f32, Vec); 6245 DCI.AddToWorklist(VRound.getNode()); 6246 SDValue Extract1 = 6247 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32, 6248 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32)); 6249 DCI.AddToWorklist(Extract1.getNode()); 6250 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1); 6251 if (Chain) 6252 DAG.ReplaceAllUsesOfValueWith(OtherRound.getValue(1), Chain); 6253 SDValue Extract0 = 6254 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32, 6255 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32)); 6256 if (Chain) 6257 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op0), 6258 N->getVTList(), Extract0, Chain); 6259 return Extract0; 6260 } 6261 } 6262 } 6263 } 6264 return SDValue(); 6265 } 6266 6267 SDValue SystemZTargetLowering::combineFP_EXTEND( 6268 SDNode *N, DAGCombinerInfo &DCI) const { 6269 6270 if (!Subtarget.hasVector()) 6271 return SDValue(); 6272 6273 // (fpextend (extract_vector_elt X 0)) 6274 // (fpextend (extract_vector_elt X 2)) -> 6275 // (extract_vector_elt (VEXTEND X) 0) 6276 // (extract_vector_elt (VEXTEND X) 1) 6277 // 6278 // This is a special case since the target doesn't really support v2f32s. 6279 unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0; 6280 SelectionDAG &DAG = DCI.DAG; 6281 SDValue Op0 = N->getOperand(OpNo); 6282 if (N->getValueType(0) == MVT::f64 && 6283 Op0.hasOneUse() && 6284 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 6285 Op0.getOperand(0).getValueType() == MVT::v4f32 && 6286 Op0.getOperand(1).getOpcode() == ISD::Constant && 6287 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) { 6288 SDValue Vec = Op0.getOperand(0); 6289 for (auto *U : Vec->uses()) { 6290 if (U != Op0.getNode() && 6291 U->hasOneUse() && 6292 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 6293 U->getOperand(0) == Vec && 6294 U->getOperand(1).getOpcode() == ISD::Constant && 6295 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 2) { 6296 SDValue OtherExtend = SDValue(*U->use_begin(), 0); 6297 if (OtherExtend.getOpcode() == N->getOpcode() && 6298 OtherExtend.getOperand(OpNo) == SDValue(U, 0) && 6299 OtherExtend.getValueType() == MVT::f64) { 6300 SDValue VExtend, Chain; 6301 if (N->isStrictFPOpcode()) { 6302 Chain = MergeInputChains(N, OtherExtend.getNode()); 6303 if (!Chain) 6304 continue; 6305 VExtend = DAG.getNode(SystemZISD::STRICT_VEXTEND, SDLoc(N), 6306 {MVT::v2f64, MVT::Other}, {Chain, Vec}); 6307 Chain = VExtend.getValue(1); 6308 } else 6309 VExtend = DAG.getNode(SystemZISD::VEXTEND, SDLoc(N), 6310 MVT::v2f64, Vec); 6311 DCI.AddToWorklist(VExtend.getNode()); 6312 SDValue Extract1 = 6313 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f64, 6314 VExtend, DAG.getConstant(1, SDLoc(U), MVT::i32)); 6315 DCI.AddToWorklist(Extract1.getNode()); 6316 DAG.ReplaceAllUsesOfValueWith(OtherExtend, Extract1); 6317 if (Chain) 6318 DAG.ReplaceAllUsesOfValueWith(OtherExtend.getValue(1), Chain); 6319 SDValue Extract0 = 6320 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f64, 6321 VExtend, DAG.getConstant(0, SDLoc(Op0), MVT::i32)); 6322 if (Chain) 6323 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op0), 6324 N->getVTList(), Extract0, Chain); 6325 return Extract0; 6326 } 6327 } 6328 } 6329 } 6330 return SDValue(); 6331 } 6332 6333 SDValue SystemZTargetLowering::combineINT_TO_FP( 6334 SDNode *N, DAGCombinerInfo &DCI) const { 6335 if (DCI.Level != BeforeLegalizeTypes) 6336 return SDValue(); 6337 unsigned Opcode = N->getOpcode(); 6338 EVT OutVT = N->getValueType(0); 6339 SelectionDAG &DAG = DCI.DAG; 6340 SDValue Op = N->getOperand(0); 6341 unsigned OutScalarBits = OutVT.getScalarSizeInBits(); 6342 unsigned InScalarBits = Op->getValueType(0).getScalarSizeInBits(); 6343 6344 // Insert an extension before type-legalization to avoid scalarization, e.g.: 6345 // v2f64 = uint_to_fp v2i16 6346 // => 6347 // v2f64 = uint_to_fp (v2i64 zero_extend v2i16) 6348 if (OutVT.isVector() && OutScalarBits > InScalarBits) { 6349 MVT ExtVT = MVT::getVectorVT(MVT::getIntegerVT(OutVT.getScalarSizeInBits()), 6350 OutVT.getVectorNumElements()); 6351 unsigned ExtOpcode = 6352 (Opcode == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND : ISD::SIGN_EXTEND); 6353 SDValue ExtOp = DAG.getNode(ExtOpcode, SDLoc(N), ExtVT, Op); 6354 return DAG.getNode(Opcode, SDLoc(N), OutVT, ExtOp); 6355 } 6356 return SDValue(); 6357 } 6358 6359 SDValue SystemZTargetLowering::combineBSWAP( 6360 SDNode *N, DAGCombinerInfo &DCI) const { 6361 SelectionDAG &DAG = DCI.DAG; 6362 // Combine BSWAP (LOAD) into LRVH/LRV/LRVG/VLBR 6363 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) && 6364 N->getOperand(0).hasOneUse() && 6365 canLoadStoreByteSwapped(N->getValueType(0))) { 6366 SDValue Load = N->getOperand(0); 6367 LoadSDNode *LD = cast<LoadSDNode>(Load); 6368 6369 // Create the byte-swapping load. 6370 SDValue Ops[] = { 6371 LD->getChain(), // Chain 6372 LD->getBasePtr() // Ptr 6373 }; 6374 EVT LoadVT = N->getValueType(0); 6375 if (LoadVT == MVT::i16) 6376 LoadVT = MVT::i32; 6377 SDValue BSLoad = 6378 DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N), 6379 DAG.getVTList(LoadVT, MVT::Other), 6380 Ops, LD->getMemoryVT(), LD->getMemOperand()); 6381 6382 // If this is an i16 load, insert the truncate. 6383 SDValue ResVal = BSLoad; 6384 if (N->getValueType(0) == MVT::i16) 6385 ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad); 6386 6387 // First, combine the bswap away. This makes the value produced by the 6388 // load dead. 6389 DCI.CombineTo(N, ResVal); 6390 6391 // Next, combine the load away, we give it a bogus result value but a real 6392 // chain result. The result value is dead because the bswap is dead. 6393 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1)); 6394 6395 // Return N so it doesn't get rechecked! 6396 return SDValue(N, 0); 6397 } 6398 6399 // Look through bitcasts that retain the number of vector elements. 6400 SDValue Op = N->getOperand(0); 6401 if (Op.getOpcode() == ISD::BITCAST && 6402 Op.getValueType().isVector() && 6403 Op.getOperand(0).getValueType().isVector() && 6404 Op.getValueType().getVectorNumElements() == 6405 Op.getOperand(0).getValueType().getVectorNumElements()) 6406 Op = Op.getOperand(0); 6407 6408 // Push BSWAP into a vector insertion if at least one side then simplifies. 6409 if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT && Op.hasOneUse()) { 6410 SDValue Vec = Op.getOperand(0); 6411 SDValue Elt = Op.getOperand(1); 6412 SDValue Idx = Op.getOperand(2); 6413 6414 if (DAG.isConstantIntBuildVectorOrConstantInt(Vec) || 6415 Vec.getOpcode() == ISD::BSWAP || Vec.isUndef() || 6416 DAG.isConstantIntBuildVectorOrConstantInt(Elt) || 6417 Elt.getOpcode() == ISD::BSWAP || Elt.isUndef() || 6418 (canLoadStoreByteSwapped(N->getValueType(0)) && 6419 ISD::isNON_EXTLoad(Elt.getNode()) && Elt.hasOneUse())) { 6420 EVT VecVT = N->getValueType(0); 6421 EVT EltVT = N->getValueType(0).getVectorElementType(); 6422 if (VecVT != Vec.getValueType()) { 6423 Vec = DAG.getNode(ISD::BITCAST, SDLoc(N), VecVT, Vec); 6424 DCI.AddToWorklist(Vec.getNode()); 6425 } 6426 if (EltVT != Elt.getValueType()) { 6427 Elt = DAG.getNode(ISD::BITCAST, SDLoc(N), EltVT, Elt); 6428 DCI.AddToWorklist(Elt.getNode()); 6429 } 6430 Vec = DAG.getNode(ISD::BSWAP, SDLoc(N), VecVT, Vec); 6431 DCI.AddToWorklist(Vec.getNode()); 6432 Elt = DAG.getNode(ISD::BSWAP, SDLoc(N), EltVT, Elt); 6433 DCI.AddToWorklist(Elt.getNode()); 6434 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N), VecVT, 6435 Vec, Elt, Idx); 6436 } 6437 } 6438 6439 // Push BSWAP into a vector shuffle if at least one side then simplifies. 6440 ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(Op); 6441 if (SV && Op.hasOneUse()) { 6442 SDValue Op0 = Op.getOperand(0); 6443 SDValue Op1 = Op.getOperand(1); 6444 6445 if (DAG.isConstantIntBuildVectorOrConstantInt(Op0) || 6446 Op0.getOpcode() == ISD::BSWAP || Op0.isUndef() || 6447 DAG.isConstantIntBuildVectorOrConstantInt(Op1) || 6448 Op1.getOpcode() == ISD::BSWAP || Op1.isUndef()) { 6449 EVT VecVT = N->getValueType(0); 6450 if (VecVT != Op0.getValueType()) { 6451 Op0 = DAG.getNode(ISD::BITCAST, SDLoc(N), VecVT, Op0); 6452 DCI.AddToWorklist(Op0.getNode()); 6453 } 6454 if (VecVT != Op1.getValueType()) { 6455 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), VecVT, Op1); 6456 DCI.AddToWorklist(Op1.getNode()); 6457 } 6458 Op0 = DAG.getNode(ISD::BSWAP, SDLoc(N), VecVT, Op0); 6459 DCI.AddToWorklist(Op0.getNode()); 6460 Op1 = DAG.getNode(ISD::BSWAP, SDLoc(N), VecVT, Op1); 6461 DCI.AddToWorklist(Op1.getNode()); 6462 return DAG.getVectorShuffle(VecVT, SDLoc(N), Op0, Op1, SV->getMask()); 6463 } 6464 } 6465 6466 return SDValue(); 6467 } 6468 6469 static bool combineCCMask(SDValue &CCReg, int &CCValid, int &CCMask) { 6470 // We have a SELECT_CCMASK or BR_CCMASK comparing the condition code 6471 // set by the CCReg instruction using the CCValid / CCMask masks, 6472 // If the CCReg instruction is itself a ICMP testing the condition 6473 // code set by some other instruction, see whether we can directly 6474 // use that condition code. 6475 6476 // Verify that we have an ICMP against some constant. 6477 if (CCValid != SystemZ::CCMASK_ICMP) 6478 return false; 6479 auto *ICmp = CCReg.getNode(); 6480 if (ICmp->getOpcode() != SystemZISD::ICMP) 6481 return false; 6482 auto *CompareLHS = ICmp->getOperand(0).getNode(); 6483 auto *CompareRHS = dyn_cast<ConstantSDNode>(ICmp->getOperand(1)); 6484 if (!CompareRHS) 6485 return false; 6486 6487 // Optimize the case where CompareLHS is a SELECT_CCMASK. 6488 if (CompareLHS->getOpcode() == SystemZISD::SELECT_CCMASK) { 6489 // Verify that we have an appropriate mask for a EQ or NE comparison. 6490 bool Invert = false; 6491 if (CCMask == SystemZ::CCMASK_CMP_NE) 6492 Invert = !Invert; 6493 else if (CCMask != SystemZ::CCMASK_CMP_EQ) 6494 return false; 6495 6496 // Verify that the ICMP compares against one of select values. 6497 auto *TrueVal = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(0)); 6498 if (!TrueVal) 6499 return false; 6500 auto *FalseVal = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(1)); 6501 if (!FalseVal) 6502 return false; 6503 if (CompareRHS->getZExtValue() == FalseVal->getZExtValue()) 6504 Invert = !Invert; 6505 else if (CompareRHS->getZExtValue() != TrueVal->getZExtValue()) 6506 return false; 6507 6508 // Compute the effective CC mask for the new branch or select. 6509 auto *NewCCValid = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(2)); 6510 auto *NewCCMask = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(3)); 6511 if (!NewCCValid || !NewCCMask) 6512 return false; 6513 CCValid = NewCCValid->getZExtValue(); 6514 CCMask = NewCCMask->getZExtValue(); 6515 if (Invert) 6516 CCMask ^= CCValid; 6517 6518 // Return the updated CCReg link. 6519 CCReg = CompareLHS->getOperand(4); 6520 return true; 6521 } 6522 6523 // Optimize the case where CompareRHS is (SRA (SHL (IPM))). 6524 if (CompareLHS->getOpcode() == ISD::SRA) { 6525 auto *SRACount = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(1)); 6526 if (!SRACount || SRACount->getZExtValue() != 30) 6527 return false; 6528 auto *SHL = CompareLHS->getOperand(0).getNode(); 6529 if (SHL->getOpcode() != ISD::SHL) 6530 return false; 6531 auto *SHLCount = dyn_cast<ConstantSDNode>(SHL->getOperand(1)); 6532 if (!SHLCount || SHLCount->getZExtValue() != 30 - SystemZ::IPM_CC) 6533 return false; 6534 auto *IPM = SHL->getOperand(0).getNode(); 6535 if (IPM->getOpcode() != SystemZISD::IPM) 6536 return false; 6537 6538 // Avoid introducing CC spills (because SRA would clobber CC). 6539 if (!CompareLHS->hasOneUse()) 6540 return false; 6541 // Verify that the ICMP compares against zero. 6542 if (CompareRHS->getZExtValue() != 0) 6543 return false; 6544 6545 // Compute the effective CC mask for the new branch or select. 6546 CCMask = SystemZ::reverseCCMask(CCMask); 6547 6548 // Return the updated CCReg link. 6549 CCReg = IPM->getOperand(0); 6550 return true; 6551 } 6552 6553 return false; 6554 } 6555 6556 SDValue SystemZTargetLowering::combineBR_CCMASK( 6557 SDNode *N, DAGCombinerInfo &DCI) const { 6558 SelectionDAG &DAG = DCI.DAG; 6559 6560 // Combine BR_CCMASK (ICMP (SELECT_CCMASK)) into a single BR_CCMASK. 6561 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(1)); 6562 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(2)); 6563 if (!CCValid || !CCMask) 6564 return SDValue(); 6565 6566 int CCValidVal = CCValid->getZExtValue(); 6567 int CCMaskVal = CCMask->getZExtValue(); 6568 SDValue Chain = N->getOperand(0); 6569 SDValue CCReg = N->getOperand(4); 6570 6571 if (combineCCMask(CCReg, CCValidVal, CCMaskVal)) 6572 return DAG.getNode(SystemZISD::BR_CCMASK, SDLoc(N), N->getValueType(0), 6573 Chain, 6574 DAG.getTargetConstant(CCValidVal, SDLoc(N), MVT::i32), 6575 DAG.getTargetConstant(CCMaskVal, SDLoc(N), MVT::i32), 6576 N->getOperand(3), CCReg); 6577 return SDValue(); 6578 } 6579 6580 SDValue SystemZTargetLowering::combineSELECT_CCMASK( 6581 SDNode *N, DAGCombinerInfo &DCI) const { 6582 SelectionDAG &DAG = DCI.DAG; 6583 6584 // Combine SELECT_CCMASK (ICMP (SELECT_CCMASK)) into a single SELECT_CCMASK. 6585 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(2)); 6586 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(3)); 6587 if (!CCValid || !CCMask) 6588 return SDValue(); 6589 6590 int CCValidVal = CCValid->getZExtValue(); 6591 int CCMaskVal = CCMask->getZExtValue(); 6592 SDValue CCReg = N->getOperand(4); 6593 6594 if (combineCCMask(CCReg, CCValidVal, CCMaskVal)) 6595 return DAG.getNode(SystemZISD::SELECT_CCMASK, SDLoc(N), N->getValueType(0), 6596 N->getOperand(0), N->getOperand(1), 6597 DAG.getTargetConstant(CCValidVal, SDLoc(N), MVT::i32), 6598 DAG.getTargetConstant(CCMaskVal, SDLoc(N), MVT::i32), 6599 CCReg); 6600 return SDValue(); 6601 } 6602 6603 6604 SDValue SystemZTargetLowering::combineGET_CCMASK( 6605 SDNode *N, DAGCombinerInfo &DCI) const { 6606 6607 // Optimize away GET_CCMASK (SELECT_CCMASK) if the CC masks are compatible 6608 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(1)); 6609 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(2)); 6610 if (!CCValid || !CCMask) 6611 return SDValue(); 6612 int CCValidVal = CCValid->getZExtValue(); 6613 int CCMaskVal = CCMask->getZExtValue(); 6614 6615 SDValue Select = N->getOperand(0); 6616 if (Select->getOpcode() != SystemZISD::SELECT_CCMASK) 6617 return SDValue(); 6618 6619 auto *SelectCCValid = dyn_cast<ConstantSDNode>(Select->getOperand(2)); 6620 auto *SelectCCMask = dyn_cast<ConstantSDNode>(Select->getOperand(3)); 6621 if (!SelectCCValid || !SelectCCMask) 6622 return SDValue(); 6623 int SelectCCValidVal = SelectCCValid->getZExtValue(); 6624 int SelectCCMaskVal = SelectCCMask->getZExtValue(); 6625 6626 auto *TrueVal = dyn_cast<ConstantSDNode>(Select->getOperand(0)); 6627 auto *FalseVal = dyn_cast<ConstantSDNode>(Select->getOperand(1)); 6628 if (!TrueVal || !FalseVal) 6629 return SDValue(); 6630 if (TrueVal->getZExtValue() != 0 && FalseVal->getZExtValue() == 0) 6631 ; 6632 else if (TrueVal->getZExtValue() == 0 && FalseVal->getZExtValue() != 0) 6633 SelectCCMaskVal ^= SelectCCValidVal; 6634 else 6635 return SDValue(); 6636 6637 if (SelectCCValidVal & ~CCValidVal) 6638 return SDValue(); 6639 if (SelectCCMaskVal != (CCMaskVal & SelectCCValidVal)) 6640 return SDValue(); 6641 6642 return Select->getOperand(4); 6643 } 6644 6645 SDValue SystemZTargetLowering::combineIntDIVREM( 6646 SDNode *N, DAGCombinerInfo &DCI) const { 6647 SelectionDAG &DAG = DCI.DAG; 6648 EVT VT = N->getValueType(0); 6649 // In the case where the divisor is a vector of constants a cheaper 6650 // sequence of instructions can replace the divide. BuildSDIV is called to 6651 // do this during DAG combining, but it only succeeds when it can build a 6652 // multiplication node. The only option for SystemZ is ISD::SMUL_LOHI, and 6653 // since it is not Legal but Custom it can only happen before 6654 // legalization. Therefore we must scalarize this early before Combine 6655 // 1. For widened vectors, this is already the result of type legalization. 6656 if (DCI.Level == BeforeLegalizeTypes && VT.isVector() && isTypeLegal(VT) && 6657 DAG.isConstantIntBuildVectorOrConstantInt(N->getOperand(1))) 6658 return DAG.UnrollVectorOp(N); 6659 return SDValue(); 6660 } 6661 6662 SDValue SystemZTargetLowering::combineINTRINSIC( 6663 SDNode *N, DAGCombinerInfo &DCI) const { 6664 SelectionDAG &DAG = DCI.DAG; 6665 6666 unsigned Id = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 6667 switch (Id) { 6668 // VECTOR LOAD (RIGHTMOST) WITH LENGTH with a length operand of 15 6669 // or larger is simply a vector load. 6670 case Intrinsic::s390_vll: 6671 case Intrinsic::s390_vlrl: 6672 if (auto *C = dyn_cast<ConstantSDNode>(N->getOperand(2))) 6673 if (C->getZExtValue() >= 15) 6674 return DAG.getLoad(N->getValueType(0), SDLoc(N), N->getOperand(0), 6675 N->getOperand(3), MachinePointerInfo()); 6676 break; 6677 // Likewise for VECTOR STORE (RIGHTMOST) WITH LENGTH. 6678 case Intrinsic::s390_vstl: 6679 case Intrinsic::s390_vstrl: 6680 if (auto *C = dyn_cast<ConstantSDNode>(N->getOperand(3))) 6681 if (C->getZExtValue() >= 15) 6682 return DAG.getStore(N->getOperand(0), SDLoc(N), N->getOperand(2), 6683 N->getOperand(4), MachinePointerInfo()); 6684 break; 6685 } 6686 6687 return SDValue(); 6688 } 6689 6690 SDValue SystemZTargetLowering::unwrapAddress(SDValue N) const { 6691 if (N->getOpcode() == SystemZISD::PCREL_WRAPPER) 6692 return N->getOperand(0); 6693 return N; 6694 } 6695 6696 SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N, 6697 DAGCombinerInfo &DCI) const { 6698 switch(N->getOpcode()) { 6699 default: break; 6700 case ISD::ZERO_EXTEND: return combineZERO_EXTEND(N, DCI); 6701 case ISD::SIGN_EXTEND: return combineSIGN_EXTEND(N, DCI); 6702 case ISD::SIGN_EXTEND_INREG: return combineSIGN_EXTEND_INREG(N, DCI); 6703 case SystemZISD::MERGE_HIGH: 6704 case SystemZISD::MERGE_LOW: return combineMERGE(N, DCI); 6705 case ISD::LOAD: return combineLOAD(N, DCI); 6706 case ISD::STORE: return combineSTORE(N, DCI); 6707 case ISD::VECTOR_SHUFFLE: return combineVECTOR_SHUFFLE(N, DCI); 6708 case ISD::EXTRACT_VECTOR_ELT: return combineEXTRACT_VECTOR_ELT(N, DCI); 6709 case SystemZISD::JOIN_DWORDS: return combineJOIN_DWORDS(N, DCI); 6710 case ISD::STRICT_FP_ROUND: 6711 case ISD::FP_ROUND: return combineFP_ROUND(N, DCI); 6712 case ISD::STRICT_FP_EXTEND: 6713 case ISD::FP_EXTEND: return combineFP_EXTEND(N, DCI); 6714 case ISD::SINT_TO_FP: 6715 case ISD::UINT_TO_FP: return combineINT_TO_FP(N, DCI); 6716 case ISD::BSWAP: return combineBSWAP(N, DCI); 6717 case SystemZISD::BR_CCMASK: return combineBR_CCMASK(N, DCI); 6718 case SystemZISD::SELECT_CCMASK: return combineSELECT_CCMASK(N, DCI); 6719 case SystemZISD::GET_CCMASK: return combineGET_CCMASK(N, DCI); 6720 case ISD::SDIV: 6721 case ISD::UDIV: 6722 case ISD::SREM: 6723 case ISD::UREM: return combineIntDIVREM(N, DCI); 6724 case ISD::INTRINSIC_W_CHAIN: 6725 case ISD::INTRINSIC_VOID: return combineINTRINSIC(N, DCI); 6726 } 6727 6728 return SDValue(); 6729 } 6730 6731 // Return the demanded elements for the OpNo source operand of Op. DemandedElts 6732 // are for Op. 6733 static APInt getDemandedSrcElements(SDValue Op, const APInt &DemandedElts, 6734 unsigned OpNo) { 6735 EVT VT = Op.getValueType(); 6736 unsigned NumElts = (VT.isVector() ? VT.getVectorNumElements() : 1); 6737 APInt SrcDemE; 6738 unsigned Opcode = Op.getOpcode(); 6739 if (Opcode == ISD::INTRINSIC_WO_CHAIN) { 6740 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 6741 switch (Id) { 6742 case Intrinsic::s390_vpksh: // PACKS 6743 case Intrinsic::s390_vpksf: 6744 case Intrinsic::s390_vpksg: 6745 case Intrinsic::s390_vpkshs: // PACKS_CC 6746 case Intrinsic::s390_vpksfs: 6747 case Intrinsic::s390_vpksgs: 6748 case Intrinsic::s390_vpklsh: // PACKLS 6749 case Intrinsic::s390_vpklsf: 6750 case Intrinsic::s390_vpklsg: 6751 case Intrinsic::s390_vpklshs: // PACKLS_CC 6752 case Intrinsic::s390_vpklsfs: 6753 case Intrinsic::s390_vpklsgs: 6754 // VECTOR PACK truncates the elements of two source vectors into one. 6755 SrcDemE = DemandedElts; 6756 if (OpNo == 2) 6757 SrcDemE.lshrInPlace(NumElts / 2); 6758 SrcDemE = SrcDemE.trunc(NumElts / 2); 6759 break; 6760 // VECTOR UNPACK extends half the elements of the source vector. 6761 case Intrinsic::s390_vuphb: // VECTOR UNPACK HIGH 6762 case Intrinsic::s390_vuphh: 6763 case Intrinsic::s390_vuphf: 6764 case Intrinsic::s390_vuplhb: // VECTOR UNPACK LOGICAL HIGH 6765 case Intrinsic::s390_vuplhh: 6766 case Intrinsic::s390_vuplhf: 6767 SrcDemE = APInt(NumElts * 2, 0); 6768 SrcDemE.insertBits(DemandedElts, 0); 6769 break; 6770 case Intrinsic::s390_vuplb: // VECTOR UNPACK LOW 6771 case Intrinsic::s390_vuplhw: 6772 case Intrinsic::s390_vuplf: 6773 case Intrinsic::s390_vupllb: // VECTOR UNPACK LOGICAL LOW 6774 case Intrinsic::s390_vupllh: 6775 case Intrinsic::s390_vupllf: 6776 SrcDemE = APInt(NumElts * 2, 0); 6777 SrcDemE.insertBits(DemandedElts, NumElts); 6778 break; 6779 case Intrinsic::s390_vpdi: { 6780 // VECTOR PERMUTE DWORD IMMEDIATE selects one element from each source. 6781 SrcDemE = APInt(NumElts, 0); 6782 if (!DemandedElts[OpNo - 1]) 6783 break; 6784 unsigned Mask = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue(); 6785 unsigned MaskBit = ((OpNo - 1) ? 1 : 4); 6786 // Demand input element 0 or 1, given by the mask bit value. 6787 SrcDemE.setBit((Mask & MaskBit)? 1 : 0); 6788 break; 6789 } 6790 case Intrinsic::s390_vsldb: { 6791 // VECTOR SHIFT LEFT DOUBLE BY BYTE 6792 assert(VT == MVT::v16i8 && "Unexpected type."); 6793 unsigned FirstIdx = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue(); 6794 assert (FirstIdx > 0 && FirstIdx < 16 && "Unused operand."); 6795 unsigned NumSrc0Els = 16 - FirstIdx; 6796 SrcDemE = APInt(NumElts, 0); 6797 if (OpNo == 1) { 6798 APInt DemEls = DemandedElts.trunc(NumSrc0Els); 6799 SrcDemE.insertBits(DemEls, FirstIdx); 6800 } else { 6801 APInt DemEls = DemandedElts.lshr(NumSrc0Els); 6802 SrcDemE.insertBits(DemEls, 0); 6803 } 6804 break; 6805 } 6806 case Intrinsic::s390_vperm: 6807 SrcDemE = APInt(NumElts, 1); 6808 break; 6809 default: 6810 llvm_unreachable("Unhandled intrinsic."); 6811 break; 6812 } 6813 } else { 6814 switch (Opcode) { 6815 case SystemZISD::JOIN_DWORDS: 6816 // Scalar operand. 6817 SrcDemE = APInt(1, 1); 6818 break; 6819 case SystemZISD::SELECT_CCMASK: 6820 SrcDemE = DemandedElts; 6821 break; 6822 default: 6823 llvm_unreachable("Unhandled opcode."); 6824 break; 6825 } 6826 } 6827 return SrcDemE; 6828 } 6829 6830 static void computeKnownBitsBinOp(const SDValue Op, KnownBits &Known, 6831 const APInt &DemandedElts, 6832 const SelectionDAG &DAG, unsigned Depth, 6833 unsigned OpNo) { 6834 APInt Src0DemE = getDemandedSrcElements(Op, DemandedElts, OpNo); 6835 APInt Src1DemE = getDemandedSrcElements(Op, DemandedElts, OpNo + 1); 6836 KnownBits LHSKnown = 6837 DAG.computeKnownBits(Op.getOperand(OpNo), Src0DemE, Depth + 1); 6838 KnownBits RHSKnown = 6839 DAG.computeKnownBits(Op.getOperand(OpNo + 1), Src1DemE, Depth + 1); 6840 Known = KnownBits::commonBits(LHSKnown, RHSKnown); 6841 } 6842 6843 void 6844 SystemZTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, 6845 KnownBits &Known, 6846 const APInt &DemandedElts, 6847 const SelectionDAG &DAG, 6848 unsigned Depth) const { 6849 Known.resetAll(); 6850 6851 // Intrinsic CC result is returned in the two low bits. 6852 unsigned tmp0, tmp1; // not used 6853 if (Op.getResNo() == 1 && isIntrinsicWithCC(Op, tmp0, tmp1)) { 6854 Known.Zero.setBitsFrom(2); 6855 return; 6856 } 6857 EVT VT = Op.getValueType(); 6858 if (Op.getResNo() != 0 || VT == MVT::Untyped) 6859 return; 6860 assert (Known.getBitWidth() == VT.getScalarSizeInBits() && 6861 "KnownBits does not match VT in bitwidth"); 6862 assert ((!VT.isVector() || 6863 (DemandedElts.getBitWidth() == VT.getVectorNumElements())) && 6864 "DemandedElts does not match VT number of elements"); 6865 unsigned BitWidth = Known.getBitWidth(); 6866 unsigned Opcode = Op.getOpcode(); 6867 if (Opcode == ISD::INTRINSIC_WO_CHAIN) { 6868 bool IsLogical = false; 6869 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 6870 switch (Id) { 6871 case Intrinsic::s390_vpksh: // PACKS 6872 case Intrinsic::s390_vpksf: 6873 case Intrinsic::s390_vpksg: 6874 case Intrinsic::s390_vpkshs: // PACKS_CC 6875 case Intrinsic::s390_vpksfs: 6876 case Intrinsic::s390_vpksgs: 6877 case Intrinsic::s390_vpklsh: // PACKLS 6878 case Intrinsic::s390_vpklsf: 6879 case Intrinsic::s390_vpklsg: 6880 case Intrinsic::s390_vpklshs: // PACKLS_CC 6881 case Intrinsic::s390_vpklsfs: 6882 case Intrinsic::s390_vpklsgs: 6883 case Intrinsic::s390_vpdi: 6884 case Intrinsic::s390_vsldb: 6885 case Intrinsic::s390_vperm: 6886 computeKnownBitsBinOp(Op, Known, DemandedElts, DAG, Depth, 1); 6887 break; 6888 case Intrinsic::s390_vuplhb: // VECTOR UNPACK LOGICAL HIGH 6889 case Intrinsic::s390_vuplhh: 6890 case Intrinsic::s390_vuplhf: 6891 case Intrinsic::s390_vupllb: // VECTOR UNPACK LOGICAL LOW 6892 case Intrinsic::s390_vupllh: 6893 case Intrinsic::s390_vupllf: 6894 IsLogical = true; 6895 LLVM_FALLTHROUGH; 6896 case Intrinsic::s390_vuphb: // VECTOR UNPACK HIGH 6897 case Intrinsic::s390_vuphh: 6898 case Intrinsic::s390_vuphf: 6899 case Intrinsic::s390_vuplb: // VECTOR UNPACK LOW 6900 case Intrinsic::s390_vuplhw: 6901 case Intrinsic::s390_vuplf: { 6902 SDValue SrcOp = Op.getOperand(1); 6903 APInt SrcDemE = getDemandedSrcElements(Op, DemandedElts, 0); 6904 Known = DAG.computeKnownBits(SrcOp, SrcDemE, Depth + 1); 6905 if (IsLogical) { 6906 Known = Known.zext(BitWidth); 6907 } else 6908 Known = Known.sext(BitWidth); 6909 break; 6910 } 6911 default: 6912 break; 6913 } 6914 } else { 6915 switch (Opcode) { 6916 case SystemZISD::JOIN_DWORDS: 6917 case SystemZISD::SELECT_CCMASK: 6918 computeKnownBitsBinOp(Op, Known, DemandedElts, DAG, Depth, 0); 6919 break; 6920 case SystemZISD::REPLICATE: { 6921 SDValue SrcOp = Op.getOperand(0); 6922 Known = DAG.computeKnownBits(SrcOp, Depth + 1); 6923 if (Known.getBitWidth() < BitWidth && isa<ConstantSDNode>(SrcOp)) 6924 Known = Known.sext(BitWidth); // VREPI sign extends the immedate. 6925 break; 6926 } 6927 default: 6928 break; 6929 } 6930 } 6931 6932 // Known has the width of the source operand(s). Adjust if needed to match 6933 // the passed bitwidth. 6934 if (Known.getBitWidth() != BitWidth) 6935 Known = Known.anyextOrTrunc(BitWidth); 6936 } 6937 6938 static unsigned computeNumSignBitsBinOp(SDValue Op, const APInt &DemandedElts, 6939 const SelectionDAG &DAG, unsigned Depth, 6940 unsigned OpNo) { 6941 APInt Src0DemE = getDemandedSrcElements(Op, DemandedElts, OpNo); 6942 unsigned LHS = DAG.ComputeNumSignBits(Op.getOperand(OpNo), Src0DemE, Depth + 1); 6943 if (LHS == 1) return 1; // Early out. 6944 APInt Src1DemE = getDemandedSrcElements(Op, DemandedElts, OpNo + 1); 6945 unsigned RHS = DAG.ComputeNumSignBits(Op.getOperand(OpNo + 1), Src1DemE, Depth + 1); 6946 if (RHS == 1) return 1; // Early out. 6947 unsigned Common = std::min(LHS, RHS); 6948 unsigned SrcBitWidth = Op.getOperand(OpNo).getScalarValueSizeInBits(); 6949 EVT VT = Op.getValueType(); 6950 unsigned VTBits = VT.getScalarSizeInBits(); 6951 if (SrcBitWidth > VTBits) { // PACK 6952 unsigned SrcExtraBits = SrcBitWidth - VTBits; 6953 if (Common > SrcExtraBits) 6954 return (Common - SrcExtraBits); 6955 return 1; 6956 } 6957 assert (SrcBitWidth == VTBits && "Expected operands of same bitwidth."); 6958 return Common; 6959 } 6960 6961 unsigned 6962 SystemZTargetLowering::ComputeNumSignBitsForTargetNode( 6963 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, 6964 unsigned Depth) const { 6965 if (Op.getResNo() != 0) 6966 return 1; 6967 unsigned Opcode = Op.getOpcode(); 6968 if (Opcode == ISD::INTRINSIC_WO_CHAIN) { 6969 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 6970 switch (Id) { 6971 case Intrinsic::s390_vpksh: // PACKS 6972 case Intrinsic::s390_vpksf: 6973 case Intrinsic::s390_vpksg: 6974 case Intrinsic::s390_vpkshs: // PACKS_CC 6975 case Intrinsic::s390_vpksfs: 6976 case Intrinsic::s390_vpksgs: 6977 case Intrinsic::s390_vpklsh: // PACKLS 6978 case Intrinsic::s390_vpklsf: 6979 case Intrinsic::s390_vpklsg: 6980 case Intrinsic::s390_vpklshs: // PACKLS_CC 6981 case Intrinsic::s390_vpklsfs: 6982 case Intrinsic::s390_vpklsgs: 6983 case Intrinsic::s390_vpdi: 6984 case Intrinsic::s390_vsldb: 6985 case Intrinsic::s390_vperm: 6986 return computeNumSignBitsBinOp(Op, DemandedElts, DAG, Depth, 1); 6987 case Intrinsic::s390_vuphb: // VECTOR UNPACK HIGH 6988 case Intrinsic::s390_vuphh: 6989 case Intrinsic::s390_vuphf: 6990 case Intrinsic::s390_vuplb: // VECTOR UNPACK LOW 6991 case Intrinsic::s390_vuplhw: 6992 case Intrinsic::s390_vuplf: { 6993 SDValue PackedOp = Op.getOperand(1); 6994 APInt SrcDemE = getDemandedSrcElements(Op, DemandedElts, 1); 6995 unsigned Tmp = DAG.ComputeNumSignBits(PackedOp, SrcDemE, Depth + 1); 6996 EVT VT = Op.getValueType(); 6997 unsigned VTBits = VT.getScalarSizeInBits(); 6998 Tmp += VTBits - PackedOp.getScalarValueSizeInBits(); 6999 return Tmp; 7000 } 7001 default: 7002 break; 7003 } 7004 } else { 7005 switch (Opcode) { 7006 case SystemZISD::SELECT_CCMASK: 7007 return computeNumSignBitsBinOp(Op, DemandedElts, DAG, Depth, 0); 7008 default: 7009 break; 7010 } 7011 } 7012 7013 return 1; 7014 } 7015 7016 unsigned 7017 SystemZTargetLowering::getStackProbeSize(MachineFunction &MF) const { 7018 const TargetFrameLowering *TFI = Subtarget.getFrameLowering(); 7019 unsigned StackAlign = TFI->getStackAlignment(); 7020 assert(StackAlign >=1 && isPowerOf2_32(StackAlign) && 7021 "Unexpected stack alignment"); 7022 // The default stack probe size is 4096 if the function has no 7023 // stack-probe-size attribute. 7024 unsigned StackProbeSize = 4096; 7025 const Function &Fn = MF.getFunction(); 7026 if (Fn.hasFnAttribute("stack-probe-size")) 7027 Fn.getFnAttribute("stack-probe-size") 7028 .getValueAsString() 7029 .getAsInteger(0, StackProbeSize); 7030 // Round down to the stack alignment. 7031 StackProbeSize &= ~(StackAlign - 1); 7032 return StackProbeSize ? StackProbeSize : StackAlign; 7033 } 7034 7035 //===----------------------------------------------------------------------===// 7036 // Custom insertion 7037 //===----------------------------------------------------------------------===// 7038 7039 // Force base value Base into a register before MI. Return the register. 7040 static Register forceReg(MachineInstr &MI, MachineOperand &Base, 7041 const SystemZInstrInfo *TII) { 7042 if (Base.isReg()) 7043 return Base.getReg(); 7044 7045 MachineBasicBlock *MBB = MI.getParent(); 7046 MachineFunction &MF = *MBB->getParent(); 7047 MachineRegisterInfo &MRI = MF.getRegInfo(); 7048 7049 Register Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 7050 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LA), Reg) 7051 .add(Base) 7052 .addImm(0) 7053 .addReg(0); 7054 return Reg; 7055 } 7056 7057 // The CC operand of MI might be missing a kill marker because there 7058 // were multiple uses of CC, and ISel didn't know which to mark. 7059 // Figure out whether MI should have had a kill marker. 7060 static bool checkCCKill(MachineInstr &MI, MachineBasicBlock *MBB) { 7061 // Scan forward through BB for a use/def of CC. 7062 MachineBasicBlock::iterator miI(std::next(MachineBasicBlock::iterator(MI))); 7063 for (MachineBasicBlock::iterator miE = MBB->end(); miI != miE; ++miI) { 7064 const MachineInstr& mi = *miI; 7065 if (mi.readsRegister(SystemZ::CC)) 7066 return false; 7067 if (mi.definesRegister(SystemZ::CC)) 7068 break; // Should have kill-flag - update below. 7069 } 7070 7071 // If we hit the end of the block, check whether CC is live into a 7072 // successor. 7073 if (miI == MBB->end()) { 7074 for (auto SI = MBB->succ_begin(), SE = MBB->succ_end(); SI != SE; ++SI) 7075 if ((*SI)->isLiveIn(SystemZ::CC)) 7076 return false; 7077 } 7078 7079 return true; 7080 } 7081 7082 // Return true if it is OK for this Select pseudo-opcode to be cascaded 7083 // together with other Select pseudo-opcodes into a single basic-block with 7084 // a conditional jump around it. 7085 static bool isSelectPseudo(MachineInstr &MI) { 7086 switch (MI.getOpcode()) { 7087 case SystemZ::Select32: 7088 case SystemZ::Select64: 7089 case SystemZ::SelectF32: 7090 case SystemZ::SelectF64: 7091 case SystemZ::SelectF128: 7092 case SystemZ::SelectVR32: 7093 case SystemZ::SelectVR64: 7094 case SystemZ::SelectVR128: 7095 return true; 7096 7097 default: 7098 return false; 7099 } 7100 } 7101 7102 // Helper function, which inserts PHI functions into SinkMBB: 7103 // %Result(i) = phi [ %FalseValue(i), FalseMBB ], [ %TrueValue(i), TrueMBB ], 7104 // where %FalseValue(i) and %TrueValue(i) are taken from Selects. 7105 static void createPHIsForSelects(SmallVector<MachineInstr*, 8> &Selects, 7106 MachineBasicBlock *TrueMBB, 7107 MachineBasicBlock *FalseMBB, 7108 MachineBasicBlock *SinkMBB) { 7109 MachineFunction *MF = TrueMBB->getParent(); 7110 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 7111 7112 MachineInstr *FirstMI = Selects.front(); 7113 unsigned CCValid = FirstMI->getOperand(3).getImm(); 7114 unsigned CCMask = FirstMI->getOperand(4).getImm(); 7115 7116 MachineBasicBlock::iterator SinkInsertionPoint = SinkMBB->begin(); 7117 7118 // As we are creating the PHIs, we have to be careful if there is more than 7119 // one. Later Selects may reference the results of earlier Selects, but later 7120 // PHIs have to reference the individual true/false inputs from earlier PHIs. 7121 // That also means that PHI construction must work forward from earlier to 7122 // later, and that the code must maintain a mapping from earlier PHI's 7123 // destination registers, and the registers that went into the PHI. 7124 DenseMap<unsigned, std::pair<unsigned, unsigned>> RegRewriteTable; 7125 7126 for (auto MI : Selects) { 7127 Register DestReg = MI->getOperand(0).getReg(); 7128 Register TrueReg = MI->getOperand(1).getReg(); 7129 Register FalseReg = MI->getOperand(2).getReg(); 7130 7131 // If this Select we are generating is the opposite condition from 7132 // the jump we generated, then we have to swap the operands for the 7133 // PHI that is going to be generated. 7134 if (MI->getOperand(4).getImm() == (CCValid ^ CCMask)) 7135 std::swap(TrueReg, FalseReg); 7136 7137 if (RegRewriteTable.find(TrueReg) != RegRewriteTable.end()) 7138 TrueReg = RegRewriteTable[TrueReg].first; 7139 7140 if (RegRewriteTable.find(FalseReg) != RegRewriteTable.end()) 7141 FalseReg = RegRewriteTable[FalseReg].second; 7142 7143 DebugLoc DL = MI->getDebugLoc(); 7144 BuildMI(*SinkMBB, SinkInsertionPoint, DL, TII->get(SystemZ::PHI), DestReg) 7145 .addReg(TrueReg).addMBB(TrueMBB) 7146 .addReg(FalseReg).addMBB(FalseMBB); 7147 7148 // Add this PHI to the rewrite table. 7149 RegRewriteTable[DestReg] = std::make_pair(TrueReg, FalseReg); 7150 } 7151 7152 MF->getProperties().reset(MachineFunctionProperties::Property::NoPHIs); 7153 } 7154 7155 // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI. 7156 MachineBasicBlock * 7157 SystemZTargetLowering::emitSelect(MachineInstr &MI, 7158 MachineBasicBlock *MBB) const { 7159 assert(isSelectPseudo(MI) && "Bad call to emitSelect()"); 7160 const SystemZInstrInfo *TII = 7161 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 7162 7163 unsigned CCValid = MI.getOperand(3).getImm(); 7164 unsigned CCMask = MI.getOperand(4).getImm(); 7165 7166 // If we have a sequence of Select* pseudo instructions using the 7167 // same condition code value, we want to expand all of them into 7168 // a single pair of basic blocks using the same condition. 7169 SmallVector<MachineInstr*, 8> Selects; 7170 SmallVector<MachineInstr*, 8> DbgValues; 7171 Selects.push_back(&MI); 7172 unsigned Count = 0; 7173 for (MachineBasicBlock::iterator NextMIIt = 7174 std::next(MachineBasicBlock::iterator(MI)); 7175 NextMIIt != MBB->end(); ++NextMIIt) { 7176 if (isSelectPseudo(*NextMIIt)) { 7177 assert(NextMIIt->getOperand(3).getImm() == CCValid && 7178 "Bad CCValid operands since CC was not redefined."); 7179 if (NextMIIt->getOperand(4).getImm() == CCMask || 7180 NextMIIt->getOperand(4).getImm() == (CCValid ^ CCMask)) { 7181 Selects.push_back(&*NextMIIt); 7182 continue; 7183 } 7184 break; 7185 } 7186 if (NextMIIt->definesRegister(SystemZ::CC) || 7187 NextMIIt->usesCustomInsertionHook()) 7188 break; 7189 bool User = false; 7190 for (auto SelMI : Selects) 7191 if (NextMIIt->readsVirtualRegister(SelMI->getOperand(0).getReg())) { 7192 User = true; 7193 break; 7194 } 7195 if (NextMIIt->isDebugInstr()) { 7196 if (User) { 7197 assert(NextMIIt->isDebugValue() && "Unhandled debug opcode."); 7198 DbgValues.push_back(&*NextMIIt); 7199 } 7200 } 7201 else if (User || ++Count > 20) 7202 break; 7203 } 7204 7205 MachineInstr *LastMI = Selects.back(); 7206 bool CCKilled = 7207 (LastMI->killsRegister(SystemZ::CC) || checkCCKill(*LastMI, MBB)); 7208 MachineBasicBlock *StartMBB = MBB; 7209 MachineBasicBlock *JoinMBB = SystemZ::splitBlockAfter(LastMI, MBB); 7210 MachineBasicBlock *FalseMBB = SystemZ::emitBlockAfter(StartMBB); 7211 7212 // Unless CC was killed in the last Select instruction, mark it as 7213 // live-in to both FalseMBB and JoinMBB. 7214 if (!CCKilled) { 7215 FalseMBB->addLiveIn(SystemZ::CC); 7216 JoinMBB->addLiveIn(SystemZ::CC); 7217 } 7218 7219 // StartMBB: 7220 // BRC CCMask, JoinMBB 7221 // # fallthrough to FalseMBB 7222 MBB = StartMBB; 7223 BuildMI(MBB, MI.getDebugLoc(), TII->get(SystemZ::BRC)) 7224 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); 7225 MBB->addSuccessor(JoinMBB); 7226 MBB->addSuccessor(FalseMBB); 7227 7228 // FalseMBB: 7229 // # fallthrough to JoinMBB 7230 MBB = FalseMBB; 7231 MBB->addSuccessor(JoinMBB); 7232 7233 // JoinMBB: 7234 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ] 7235 // ... 7236 MBB = JoinMBB; 7237 createPHIsForSelects(Selects, StartMBB, FalseMBB, MBB); 7238 for (auto SelMI : Selects) 7239 SelMI->eraseFromParent(); 7240 7241 MachineBasicBlock::iterator InsertPos = MBB->getFirstNonPHI(); 7242 for (auto DbgMI : DbgValues) 7243 MBB->splice(InsertPos, StartMBB, DbgMI); 7244 7245 return JoinMBB; 7246 } 7247 7248 // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI. 7249 // StoreOpcode is the store to use and Invert says whether the store should 7250 // happen when the condition is false rather than true. If a STORE ON 7251 // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0. 7252 MachineBasicBlock *SystemZTargetLowering::emitCondStore(MachineInstr &MI, 7253 MachineBasicBlock *MBB, 7254 unsigned StoreOpcode, 7255 unsigned STOCOpcode, 7256 bool Invert) const { 7257 const SystemZInstrInfo *TII = 7258 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 7259 7260 Register SrcReg = MI.getOperand(0).getReg(); 7261 MachineOperand Base = MI.getOperand(1); 7262 int64_t Disp = MI.getOperand(2).getImm(); 7263 Register IndexReg = MI.getOperand(3).getReg(); 7264 unsigned CCValid = MI.getOperand(4).getImm(); 7265 unsigned CCMask = MI.getOperand(5).getImm(); 7266 DebugLoc DL = MI.getDebugLoc(); 7267 7268 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp); 7269 7270 // ISel pattern matching also adds a load memory operand of the same 7271 // address, so take special care to find the storing memory operand. 7272 MachineMemOperand *MMO = nullptr; 7273 for (auto *I : MI.memoperands()) 7274 if (I->isStore()) { 7275 MMO = I; 7276 break; 7277 } 7278 7279 // Use STOCOpcode if possible. We could use different store patterns in 7280 // order to avoid matching the index register, but the performance trade-offs 7281 // might be more complicated in that case. 7282 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) { 7283 if (Invert) 7284 CCMask ^= CCValid; 7285 7286 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode)) 7287 .addReg(SrcReg) 7288 .add(Base) 7289 .addImm(Disp) 7290 .addImm(CCValid) 7291 .addImm(CCMask) 7292 .addMemOperand(MMO); 7293 7294 MI.eraseFromParent(); 7295 return MBB; 7296 } 7297 7298 // Get the condition needed to branch around the store. 7299 if (!Invert) 7300 CCMask ^= CCValid; 7301 7302 MachineBasicBlock *StartMBB = MBB; 7303 MachineBasicBlock *JoinMBB = SystemZ::splitBlockBefore(MI, MBB); 7304 MachineBasicBlock *FalseMBB = SystemZ::emitBlockAfter(StartMBB); 7305 7306 // Unless CC was killed in the CondStore instruction, mark it as 7307 // live-in to both FalseMBB and JoinMBB. 7308 if (!MI.killsRegister(SystemZ::CC) && !checkCCKill(MI, JoinMBB)) { 7309 FalseMBB->addLiveIn(SystemZ::CC); 7310 JoinMBB->addLiveIn(SystemZ::CC); 7311 } 7312 7313 // StartMBB: 7314 // BRC CCMask, JoinMBB 7315 // # fallthrough to FalseMBB 7316 MBB = StartMBB; 7317 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7318 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); 7319 MBB->addSuccessor(JoinMBB); 7320 MBB->addSuccessor(FalseMBB); 7321 7322 // FalseMBB: 7323 // store %SrcReg, %Disp(%Index,%Base) 7324 // # fallthrough to JoinMBB 7325 MBB = FalseMBB; 7326 BuildMI(MBB, DL, TII->get(StoreOpcode)) 7327 .addReg(SrcReg) 7328 .add(Base) 7329 .addImm(Disp) 7330 .addReg(IndexReg) 7331 .addMemOperand(MMO); 7332 MBB->addSuccessor(JoinMBB); 7333 7334 MI.eraseFromParent(); 7335 return JoinMBB; 7336 } 7337 7338 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_* 7339 // or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that 7340 // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}. 7341 // BitSize is the width of the field in bits, or 0 if this is a partword 7342 // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize 7343 // is one of the operands. Invert says whether the field should be 7344 // inverted after performing BinOpcode (e.g. for NAND). 7345 MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadBinary( 7346 MachineInstr &MI, MachineBasicBlock *MBB, unsigned BinOpcode, 7347 unsigned BitSize, bool Invert) const { 7348 MachineFunction &MF = *MBB->getParent(); 7349 const SystemZInstrInfo *TII = 7350 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 7351 MachineRegisterInfo &MRI = MF.getRegInfo(); 7352 bool IsSubWord = (BitSize < 32); 7353 7354 // Extract the operands. Base can be a register or a frame index. 7355 // Src2 can be a register or immediate. 7356 Register Dest = MI.getOperand(0).getReg(); 7357 MachineOperand Base = earlyUseOperand(MI.getOperand(1)); 7358 int64_t Disp = MI.getOperand(2).getImm(); 7359 MachineOperand Src2 = earlyUseOperand(MI.getOperand(3)); 7360 Register BitShift = IsSubWord ? MI.getOperand(4).getReg() : Register(); 7361 Register NegBitShift = IsSubWord ? MI.getOperand(5).getReg() : Register(); 7362 DebugLoc DL = MI.getDebugLoc(); 7363 if (IsSubWord) 7364 BitSize = MI.getOperand(6).getImm(); 7365 7366 // Subword operations use 32-bit registers. 7367 const TargetRegisterClass *RC = (BitSize <= 32 ? 7368 &SystemZ::GR32BitRegClass : 7369 &SystemZ::GR64BitRegClass); 7370 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; 7371 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; 7372 7373 // Get the right opcodes for the displacement. 7374 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); 7375 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); 7376 assert(LOpcode && CSOpcode && "Displacement out of range"); 7377 7378 // Create virtual registers for temporary results. 7379 Register OrigVal = MRI.createVirtualRegister(RC); 7380 Register OldVal = MRI.createVirtualRegister(RC); 7381 Register NewVal = (BinOpcode || IsSubWord ? 7382 MRI.createVirtualRegister(RC) : Src2.getReg()); 7383 Register RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); 7384 Register RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); 7385 7386 // Insert a basic block for the main loop. 7387 MachineBasicBlock *StartMBB = MBB; 7388 MachineBasicBlock *DoneMBB = SystemZ::splitBlockBefore(MI, MBB); 7389 MachineBasicBlock *LoopMBB = SystemZ::emitBlockAfter(StartMBB); 7390 7391 // StartMBB: 7392 // ... 7393 // %OrigVal = L Disp(%Base) 7394 // # fall through to LoopMMB 7395 MBB = StartMBB; 7396 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0); 7397 MBB->addSuccessor(LoopMBB); 7398 7399 // LoopMBB: 7400 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ] 7401 // %RotatedOldVal = RLL %OldVal, 0(%BitShift) 7402 // %RotatedNewVal = OP %RotatedOldVal, %Src2 7403 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) 7404 // %Dest = CS %OldVal, %NewVal, Disp(%Base) 7405 // JNE LoopMBB 7406 // # fall through to DoneMMB 7407 MBB = LoopMBB; 7408 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 7409 .addReg(OrigVal).addMBB(StartMBB) 7410 .addReg(Dest).addMBB(LoopMBB); 7411 if (IsSubWord) 7412 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) 7413 .addReg(OldVal).addReg(BitShift).addImm(0); 7414 if (Invert) { 7415 // Perform the operation normally and then invert every bit of the field. 7416 Register Tmp = MRI.createVirtualRegister(RC); 7417 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp).addReg(RotatedOldVal).add(Src2); 7418 if (BitSize <= 32) 7419 // XILF with the upper BitSize bits set. 7420 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal) 7421 .addReg(Tmp).addImm(-1U << (32 - BitSize)); 7422 else { 7423 // Use LCGR and add -1 to the result, which is more compact than 7424 // an XILF, XILH pair. 7425 Register Tmp2 = MRI.createVirtualRegister(RC); 7426 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp); 7427 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal) 7428 .addReg(Tmp2).addImm(-1); 7429 } 7430 } else if (BinOpcode) 7431 // A simply binary operation. 7432 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal) 7433 .addReg(RotatedOldVal) 7434 .add(Src2); 7435 else if (IsSubWord) 7436 // Use RISBG to rotate Src2 into position and use it to replace the 7437 // field in RotatedOldVal. 7438 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal) 7439 .addReg(RotatedOldVal).addReg(Src2.getReg()) 7440 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize); 7441 if (IsSubWord) 7442 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) 7443 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); 7444 BuildMI(MBB, DL, TII->get(CSOpcode), Dest) 7445 .addReg(OldVal) 7446 .addReg(NewVal) 7447 .add(Base) 7448 .addImm(Disp); 7449 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7450 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 7451 MBB->addSuccessor(LoopMBB); 7452 MBB->addSuccessor(DoneMBB); 7453 7454 MI.eraseFromParent(); 7455 return DoneMBB; 7456 } 7457 7458 // Implement EmitInstrWithCustomInserter for pseudo 7459 // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the 7460 // instruction that should be used to compare the current field with the 7461 // minimum or maximum value. KeepOldMask is the BRC condition-code mask 7462 // for when the current field should be kept. BitSize is the width of 7463 // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction. 7464 MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadMinMax( 7465 MachineInstr &MI, MachineBasicBlock *MBB, unsigned CompareOpcode, 7466 unsigned KeepOldMask, unsigned BitSize) const { 7467 MachineFunction &MF = *MBB->getParent(); 7468 const SystemZInstrInfo *TII = 7469 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 7470 MachineRegisterInfo &MRI = MF.getRegInfo(); 7471 bool IsSubWord = (BitSize < 32); 7472 7473 // Extract the operands. Base can be a register or a frame index. 7474 Register Dest = MI.getOperand(0).getReg(); 7475 MachineOperand Base = earlyUseOperand(MI.getOperand(1)); 7476 int64_t Disp = MI.getOperand(2).getImm(); 7477 Register Src2 = MI.getOperand(3).getReg(); 7478 Register BitShift = (IsSubWord ? MI.getOperand(4).getReg() : Register()); 7479 Register NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : Register()); 7480 DebugLoc DL = MI.getDebugLoc(); 7481 if (IsSubWord) 7482 BitSize = MI.getOperand(6).getImm(); 7483 7484 // Subword operations use 32-bit registers. 7485 const TargetRegisterClass *RC = (BitSize <= 32 ? 7486 &SystemZ::GR32BitRegClass : 7487 &SystemZ::GR64BitRegClass); 7488 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; 7489 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; 7490 7491 // Get the right opcodes for the displacement. 7492 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); 7493 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); 7494 assert(LOpcode && CSOpcode && "Displacement out of range"); 7495 7496 // Create virtual registers for temporary results. 7497 Register OrigVal = MRI.createVirtualRegister(RC); 7498 Register OldVal = MRI.createVirtualRegister(RC); 7499 Register NewVal = MRI.createVirtualRegister(RC); 7500 Register RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); 7501 Register RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2); 7502 Register RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); 7503 7504 // Insert 3 basic blocks for the loop. 7505 MachineBasicBlock *StartMBB = MBB; 7506 MachineBasicBlock *DoneMBB = SystemZ::splitBlockBefore(MI, MBB); 7507 MachineBasicBlock *LoopMBB = SystemZ::emitBlockAfter(StartMBB); 7508 MachineBasicBlock *UseAltMBB = SystemZ::emitBlockAfter(LoopMBB); 7509 MachineBasicBlock *UpdateMBB = SystemZ::emitBlockAfter(UseAltMBB); 7510 7511 // StartMBB: 7512 // ... 7513 // %OrigVal = L Disp(%Base) 7514 // # fall through to LoopMMB 7515 MBB = StartMBB; 7516 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0); 7517 MBB->addSuccessor(LoopMBB); 7518 7519 // LoopMBB: 7520 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ] 7521 // %RotatedOldVal = RLL %OldVal, 0(%BitShift) 7522 // CompareOpcode %RotatedOldVal, %Src2 7523 // BRC KeepOldMask, UpdateMBB 7524 MBB = LoopMBB; 7525 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 7526 .addReg(OrigVal).addMBB(StartMBB) 7527 .addReg(Dest).addMBB(UpdateMBB); 7528 if (IsSubWord) 7529 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) 7530 .addReg(OldVal).addReg(BitShift).addImm(0); 7531 BuildMI(MBB, DL, TII->get(CompareOpcode)) 7532 .addReg(RotatedOldVal).addReg(Src2); 7533 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7534 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB); 7535 MBB->addSuccessor(UpdateMBB); 7536 MBB->addSuccessor(UseAltMBB); 7537 7538 // UseAltMBB: 7539 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0 7540 // # fall through to UpdateMMB 7541 MBB = UseAltMBB; 7542 if (IsSubWord) 7543 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal) 7544 .addReg(RotatedOldVal).addReg(Src2) 7545 .addImm(32).addImm(31 + BitSize).addImm(0); 7546 MBB->addSuccessor(UpdateMBB); 7547 7548 // UpdateMBB: 7549 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ], 7550 // [ %RotatedAltVal, UseAltMBB ] 7551 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) 7552 // %Dest = CS %OldVal, %NewVal, Disp(%Base) 7553 // JNE LoopMBB 7554 // # fall through to DoneMMB 7555 MBB = UpdateMBB; 7556 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal) 7557 .addReg(RotatedOldVal).addMBB(LoopMBB) 7558 .addReg(RotatedAltVal).addMBB(UseAltMBB); 7559 if (IsSubWord) 7560 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) 7561 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); 7562 BuildMI(MBB, DL, TII->get(CSOpcode), Dest) 7563 .addReg(OldVal) 7564 .addReg(NewVal) 7565 .add(Base) 7566 .addImm(Disp); 7567 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7568 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 7569 MBB->addSuccessor(LoopMBB); 7570 MBB->addSuccessor(DoneMBB); 7571 7572 MI.eraseFromParent(); 7573 return DoneMBB; 7574 } 7575 7576 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW 7577 // instruction MI. 7578 MachineBasicBlock * 7579 SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI, 7580 MachineBasicBlock *MBB) const { 7581 7582 MachineFunction &MF = *MBB->getParent(); 7583 const SystemZInstrInfo *TII = 7584 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 7585 MachineRegisterInfo &MRI = MF.getRegInfo(); 7586 7587 // Extract the operands. Base can be a register or a frame index. 7588 Register Dest = MI.getOperand(0).getReg(); 7589 MachineOperand Base = earlyUseOperand(MI.getOperand(1)); 7590 int64_t Disp = MI.getOperand(2).getImm(); 7591 Register OrigCmpVal = MI.getOperand(3).getReg(); 7592 Register OrigSwapVal = MI.getOperand(4).getReg(); 7593 Register BitShift = MI.getOperand(5).getReg(); 7594 Register NegBitShift = MI.getOperand(6).getReg(); 7595 int64_t BitSize = MI.getOperand(7).getImm(); 7596 DebugLoc DL = MI.getDebugLoc(); 7597 7598 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass; 7599 7600 // Get the right opcodes for the displacement. 7601 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp); 7602 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp); 7603 assert(LOpcode && CSOpcode && "Displacement out of range"); 7604 7605 // Create virtual registers for temporary results. 7606 Register OrigOldVal = MRI.createVirtualRegister(RC); 7607 Register OldVal = MRI.createVirtualRegister(RC); 7608 Register CmpVal = MRI.createVirtualRegister(RC); 7609 Register SwapVal = MRI.createVirtualRegister(RC); 7610 Register StoreVal = MRI.createVirtualRegister(RC); 7611 Register RetryOldVal = MRI.createVirtualRegister(RC); 7612 Register RetryCmpVal = MRI.createVirtualRegister(RC); 7613 Register RetrySwapVal = MRI.createVirtualRegister(RC); 7614 7615 // Insert 2 basic blocks for the loop. 7616 MachineBasicBlock *StartMBB = MBB; 7617 MachineBasicBlock *DoneMBB = SystemZ::splitBlockBefore(MI, MBB); 7618 MachineBasicBlock *LoopMBB = SystemZ::emitBlockAfter(StartMBB); 7619 MachineBasicBlock *SetMBB = SystemZ::emitBlockAfter(LoopMBB); 7620 7621 // StartMBB: 7622 // ... 7623 // %OrigOldVal = L Disp(%Base) 7624 // # fall through to LoopMMB 7625 MBB = StartMBB; 7626 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal) 7627 .add(Base) 7628 .addImm(Disp) 7629 .addReg(0); 7630 MBB->addSuccessor(LoopMBB); 7631 7632 // LoopMBB: 7633 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ] 7634 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ] 7635 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ] 7636 // %Dest = RLL %OldVal, BitSize(%BitShift) 7637 // ^^ The low BitSize bits contain the field 7638 // of interest. 7639 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0 7640 // ^^ Replace the upper 32-BitSize bits of the 7641 // comparison value with those that we loaded, 7642 // so that we can use a full word comparison. 7643 // CR %Dest, %RetryCmpVal 7644 // JNE DoneMBB 7645 // # Fall through to SetMBB 7646 MBB = LoopMBB; 7647 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 7648 .addReg(OrigOldVal).addMBB(StartMBB) 7649 .addReg(RetryOldVal).addMBB(SetMBB); 7650 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal) 7651 .addReg(OrigCmpVal).addMBB(StartMBB) 7652 .addReg(RetryCmpVal).addMBB(SetMBB); 7653 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal) 7654 .addReg(OrigSwapVal).addMBB(StartMBB) 7655 .addReg(RetrySwapVal).addMBB(SetMBB); 7656 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest) 7657 .addReg(OldVal).addReg(BitShift).addImm(BitSize); 7658 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal) 7659 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); 7660 BuildMI(MBB, DL, TII->get(SystemZ::CR)) 7661 .addReg(Dest).addReg(RetryCmpVal); 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 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0 7670 // ^^ Replace the upper 32-BitSize bits of the new 7671 // value with those that we loaded. 7672 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift) 7673 // ^^ Rotate the new field to its proper position. 7674 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base) 7675 // JNE LoopMBB 7676 // # fall through to ExitMMB 7677 MBB = SetMBB; 7678 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal) 7679 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); 7680 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal) 7681 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize); 7682 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal) 7683 .addReg(OldVal) 7684 .addReg(StoreVal) 7685 .add(Base) 7686 .addImm(Disp); 7687 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7688 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 7689 MBB->addSuccessor(LoopMBB); 7690 MBB->addSuccessor(DoneMBB); 7691 7692 // If the CC def wasn't dead in the ATOMIC_CMP_SWAPW, mark CC as live-in 7693 // to the block after the loop. At this point, CC may have been defined 7694 // either by the CR in LoopMBB or by the CS in SetMBB. 7695 if (!MI.registerDefIsDead(SystemZ::CC)) 7696 DoneMBB->addLiveIn(SystemZ::CC); 7697 7698 MI.eraseFromParent(); 7699 return DoneMBB; 7700 } 7701 7702 // Emit a move from two GR64s to a GR128. 7703 MachineBasicBlock * 7704 SystemZTargetLowering::emitPair128(MachineInstr &MI, 7705 MachineBasicBlock *MBB) const { 7706 MachineFunction &MF = *MBB->getParent(); 7707 const SystemZInstrInfo *TII = 7708 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 7709 MachineRegisterInfo &MRI = MF.getRegInfo(); 7710 DebugLoc DL = MI.getDebugLoc(); 7711 7712 Register Dest = MI.getOperand(0).getReg(); 7713 Register Hi = MI.getOperand(1).getReg(); 7714 Register Lo = MI.getOperand(2).getReg(); 7715 Register Tmp1 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 7716 Register Tmp2 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 7717 7718 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Tmp1); 7719 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Tmp2) 7720 .addReg(Tmp1).addReg(Hi).addImm(SystemZ::subreg_h64); 7721 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest) 7722 .addReg(Tmp2).addReg(Lo).addImm(SystemZ::subreg_l64); 7723 7724 MI.eraseFromParent(); 7725 return MBB; 7726 } 7727 7728 // Emit an extension from a GR64 to a GR128. ClearEven is true 7729 // if the high register of the GR128 value must be cleared or false if 7730 // it's "don't care". 7731 MachineBasicBlock *SystemZTargetLowering::emitExt128(MachineInstr &MI, 7732 MachineBasicBlock *MBB, 7733 bool ClearEven) const { 7734 MachineFunction &MF = *MBB->getParent(); 7735 const SystemZInstrInfo *TII = 7736 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 7737 MachineRegisterInfo &MRI = MF.getRegInfo(); 7738 DebugLoc DL = MI.getDebugLoc(); 7739 7740 Register Dest = MI.getOperand(0).getReg(); 7741 Register Src = MI.getOperand(1).getReg(); 7742 Register In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 7743 7744 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128); 7745 if (ClearEven) { 7746 Register NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 7747 Register Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass); 7748 7749 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64) 7750 .addImm(0); 7751 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128) 7752 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64); 7753 In128 = NewIn128; 7754 } 7755 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest) 7756 .addReg(In128).addReg(Src).addImm(SystemZ::subreg_l64); 7757 7758 MI.eraseFromParent(); 7759 return MBB; 7760 } 7761 7762 MachineBasicBlock *SystemZTargetLowering::emitMemMemWrapper( 7763 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const { 7764 MachineFunction &MF = *MBB->getParent(); 7765 const SystemZInstrInfo *TII = 7766 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 7767 MachineRegisterInfo &MRI = MF.getRegInfo(); 7768 DebugLoc DL = MI.getDebugLoc(); 7769 7770 MachineOperand DestBase = earlyUseOperand(MI.getOperand(0)); 7771 uint64_t DestDisp = MI.getOperand(1).getImm(); 7772 MachineOperand SrcBase = earlyUseOperand(MI.getOperand(2)); 7773 uint64_t SrcDisp = MI.getOperand(3).getImm(); 7774 uint64_t Length = MI.getOperand(4).getImm(); 7775 7776 // When generating more than one CLC, all but the last will need to 7777 // branch to the end when a difference is found. 7778 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ? 7779 SystemZ::splitBlockAfter(MI, MBB) : nullptr); 7780 7781 // Check for the loop form, in which operand 5 is the trip count. 7782 if (MI.getNumExplicitOperands() > 5) { 7783 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase); 7784 7785 Register StartCountReg = MI.getOperand(5).getReg(); 7786 Register StartSrcReg = forceReg(MI, SrcBase, TII); 7787 Register StartDestReg = (HaveSingleBase ? StartSrcReg : 7788 forceReg(MI, DestBase, TII)); 7789 7790 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass; 7791 Register ThisSrcReg = MRI.createVirtualRegister(RC); 7792 Register ThisDestReg = (HaveSingleBase ? ThisSrcReg : 7793 MRI.createVirtualRegister(RC)); 7794 Register NextSrcReg = MRI.createVirtualRegister(RC); 7795 Register NextDestReg = (HaveSingleBase ? NextSrcReg : 7796 MRI.createVirtualRegister(RC)); 7797 7798 RC = &SystemZ::GR64BitRegClass; 7799 Register ThisCountReg = MRI.createVirtualRegister(RC); 7800 Register NextCountReg = MRI.createVirtualRegister(RC); 7801 7802 MachineBasicBlock *StartMBB = MBB; 7803 MachineBasicBlock *DoneMBB = SystemZ::splitBlockBefore(MI, MBB); 7804 MachineBasicBlock *LoopMBB = SystemZ::emitBlockAfter(StartMBB); 7805 MachineBasicBlock *NextMBB = 7806 (EndMBB ? SystemZ::emitBlockAfter(LoopMBB) : LoopMBB); 7807 7808 // StartMBB: 7809 // # fall through to LoopMMB 7810 MBB->addSuccessor(LoopMBB); 7811 7812 // LoopMBB: 7813 // %ThisDestReg = phi [ %StartDestReg, StartMBB ], 7814 // [ %NextDestReg, NextMBB ] 7815 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ], 7816 // [ %NextSrcReg, NextMBB ] 7817 // %ThisCountReg = phi [ %StartCountReg, StartMBB ], 7818 // [ %NextCountReg, NextMBB ] 7819 // ( PFD 2, 768+DestDisp(%ThisDestReg) ) 7820 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg) 7821 // ( JLH EndMBB ) 7822 // 7823 // The prefetch is used only for MVC. The JLH is used only for CLC. 7824 MBB = LoopMBB; 7825 7826 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg) 7827 .addReg(StartDestReg).addMBB(StartMBB) 7828 .addReg(NextDestReg).addMBB(NextMBB); 7829 if (!HaveSingleBase) 7830 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg) 7831 .addReg(StartSrcReg).addMBB(StartMBB) 7832 .addReg(NextSrcReg).addMBB(NextMBB); 7833 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg) 7834 .addReg(StartCountReg).addMBB(StartMBB) 7835 .addReg(NextCountReg).addMBB(NextMBB); 7836 if (Opcode == SystemZ::MVC) 7837 BuildMI(MBB, DL, TII->get(SystemZ::PFD)) 7838 .addImm(SystemZ::PFD_WRITE) 7839 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0); 7840 BuildMI(MBB, DL, TII->get(Opcode)) 7841 .addReg(ThisDestReg).addImm(DestDisp).addImm(256) 7842 .addReg(ThisSrcReg).addImm(SrcDisp); 7843 if (EndMBB) { 7844 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7845 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 7846 .addMBB(EndMBB); 7847 MBB->addSuccessor(EndMBB); 7848 MBB->addSuccessor(NextMBB); 7849 } 7850 7851 // NextMBB: 7852 // %NextDestReg = LA 256(%ThisDestReg) 7853 // %NextSrcReg = LA 256(%ThisSrcReg) 7854 // %NextCountReg = AGHI %ThisCountReg, -1 7855 // CGHI %NextCountReg, 0 7856 // JLH LoopMBB 7857 // # fall through to DoneMMB 7858 // 7859 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes. 7860 MBB = NextMBB; 7861 7862 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg) 7863 .addReg(ThisDestReg).addImm(256).addReg(0); 7864 if (!HaveSingleBase) 7865 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg) 7866 .addReg(ThisSrcReg).addImm(256).addReg(0); 7867 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg) 7868 .addReg(ThisCountReg).addImm(-1); 7869 BuildMI(MBB, DL, TII->get(SystemZ::CGHI)) 7870 .addReg(NextCountReg).addImm(0); 7871 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7872 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 7873 .addMBB(LoopMBB); 7874 MBB->addSuccessor(LoopMBB); 7875 MBB->addSuccessor(DoneMBB); 7876 7877 DestBase = MachineOperand::CreateReg(NextDestReg, false); 7878 SrcBase = MachineOperand::CreateReg(NextSrcReg, false); 7879 Length &= 255; 7880 if (EndMBB && !Length) 7881 // If the loop handled the whole CLC range, DoneMBB will be empty with 7882 // CC live-through into EndMBB, so add it as live-in. 7883 DoneMBB->addLiveIn(SystemZ::CC); 7884 MBB = DoneMBB; 7885 } 7886 // Handle any remaining bytes with straight-line code. 7887 while (Length > 0) { 7888 uint64_t ThisLength = std::min(Length, uint64_t(256)); 7889 // The previous iteration might have created out-of-range displacements. 7890 // Apply them using LAY if so. 7891 if (!isUInt<12>(DestDisp)) { 7892 Register Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 7893 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg) 7894 .add(DestBase) 7895 .addImm(DestDisp) 7896 .addReg(0); 7897 DestBase = MachineOperand::CreateReg(Reg, false); 7898 DestDisp = 0; 7899 } 7900 if (!isUInt<12>(SrcDisp)) { 7901 Register Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 7902 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg) 7903 .add(SrcBase) 7904 .addImm(SrcDisp) 7905 .addReg(0); 7906 SrcBase = MachineOperand::CreateReg(Reg, false); 7907 SrcDisp = 0; 7908 } 7909 BuildMI(*MBB, MI, DL, TII->get(Opcode)) 7910 .add(DestBase) 7911 .addImm(DestDisp) 7912 .addImm(ThisLength) 7913 .add(SrcBase) 7914 .addImm(SrcDisp) 7915 .setMemRefs(MI.memoperands()); 7916 DestDisp += ThisLength; 7917 SrcDisp += ThisLength; 7918 Length -= ThisLength; 7919 // If there's another CLC to go, branch to the end if a difference 7920 // was found. 7921 if (EndMBB && Length > 0) { 7922 MachineBasicBlock *NextMBB = SystemZ::splitBlockBefore(MI, MBB); 7923 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7924 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 7925 .addMBB(EndMBB); 7926 MBB->addSuccessor(EndMBB); 7927 MBB->addSuccessor(NextMBB); 7928 MBB = NextMBB; 7929 } 7930 } 7931 if (EndMBB) { 7932 MBB->addSuccessor(EndMBB); 7933 MBB = EndMBB; 7934 MBB->addLiveIn(SystemZ::CC); 7935 } 7936 7937 MI.eraseFromParent(); 7938 return MBB; 7939 } 7940 7941 // Decompose string pseudo-instruction MI into a loop that continually performs 7942 // Opcode until CC != 3. 7943 MachineBasicBlock *SystemZTargetLowering::emitStringWrapper( 7944 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const { 7945 MachineFunction &MF = *MBB->getParent(); 7946 const SystemZInstrInfo *TII = 7947 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 7948 MachineRegisterInfo &MRI = MF.getRegInfo(); 7949 DebugLoc DL = MI.getDebugLoc(); 7950 7951 uint64_t End1Reg = MI.getOperand(0).getReg(); 7952 uint64_t Start1Reg = MI.getOperand(1).getReg(); 7953 uint64_t Start2Reg = MI.getOperand(2).getReg(); 7954 uint64_t CharReg = MI.getOperand(3).getReg(); 7955 7956 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass; 7957 uint64_t This1Reg = MRI.createVirtualRegister(RC); 7958 uint64_t This2Reg = MRI.createVirtualRegister(RC); 7959 uint64_t End2Reg = MRI.createVirtualRegister(RC); 7960 7961 MachineBasicBlock *StartMBB = MBB; 7962 MachineBasicBlock *DoneMBB = SystemZ::splitBlockBefore(MI, MBB); 7963 MachineBasicBlock *LoopMBB = SystemZ::emitBlockAfter(StartMBB); 7964 7965 // StartMBB: 7966 // # fall through to LoopMMB 7967 MBB->addSuccessor(LoopMBB); 7968 7969 // LoopMBB: 7970 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ] 7971 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ] 7972 // R0L = %CharReg 7973 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L 7974 // JO LoopMBB 7975 // # fall through to DoneMMB 7976 // 7977 // The load of R0L can be hoisted by post-RA LICM. 7978 MBB = LoopMBB; 7979 7980 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg) 7981 .addReg(Start1Reg).addMBB(StartMBB) 7982 .addReg(End1Reg).addMBB(LoopMBB); 7983 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg) 7984 .addReg(Start2Reg).addMBB(StartMBB) 7985 .addReg(End2Reg).addMBB(LoopMBB); 7986 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg); 7987 BuildMI(MBB, DL, TII->get(Opcode)) 7988 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define) 7989 .addReg(This1Reg).addReg(This2Reg); 7990 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7991 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB); 7992 MBB->addSuccessor(LoopMBB); 7993 MBB->addSuccessor(DoneMBB); 7994 7995 DoneMBB->addLiveIn(SystemZ::CC); 7996 7997 MI.eraseFromParent(); 7998 return DoneMBB; 7999 } 8000 8001 // Update TBEGIN instruction with final opcode and register clobbers. 8002 MachineBasicBlock *SystemZTargetLowering::emitTransactionBegin( 8003 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode, 8004 bool NoFloat) const { 8005 MachineFunction &MF = *MBB->getParent(); 8006 const TargetFrameLowering *TFI = Subtarget.getFrameLowering(); 8007 const SystemZInstrInfo *TII = Subtarget.getInstrInfo(); 8008 8009 // Update opcode. 8010 MI.setDesc(TII->get(Opcode)); 8011 8012 // We cannot handle a TBEGIN that clobbers the stack or frame pointer. 8013 // Make sure to add the corresponding GRSM bits if they are missing. 8014 uint64_t Control = MI.getOperand(2).getImm(); 8015 static const unsigned GPRControlBit[16] = { 8016 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000, 8017 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100 8018 }; 8019 Control |= GPRControlBit[15]; 8020 if (TFI->hasFP(MF)) 8021 Control |= GPRControlBit[11]; 8022 MI.getOperand(2).setImm(Control); 8023 8024 // Add GPR clobbers. 8025 for (int I = 0; I < 16; I++) { 8026 if ((Control & GPRControlBit[I]) == 0) { 8027 unsigned Reg = SystemZMC::GR64Regs[I]; 8028 MI.addOperand(MachineOperand::CreateReg(Reg, true, true)); 8029 } 8030 } 8031 8032 // Add FPR/VR clobbers. 8033 if (!NoFloat && (Control & 4) != 0) { 8034 if (Subtarget.hasVector()) { 8035 for (int I = 0; I < 32; I++) { 8036 unsigned Reg = SystemZMC::VR128Regs[I]; 8037 MI.addOperand(MachineOperand::CreateReg(Reg, true, true)); 8038 } 8039 } else { 8040 for (int I = 0; I < 16; I++) { 8041 unsigned Reg = SystemZMC::FP64Regs[I]; 8042 MI.addOperand(MachineOperand::CreateReg(Reg, true, true)); 8043 } 8044 } 8045 } 8046 8047 return MBB; 8048 } 8049 8050 MachineBasicBlock *SystemZTargetLowering::emitLoadAndTestCmp0( 8051 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const { 8052 MachineFunction &MF = *MBB->getParent(); 8053 MachineRegisterInfo *MRI = &MF.getRegInfo(); 8054 const SystemZInstrInfo *TII = 8055 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 8056 DebugLoc DL = MI.getDebugLoc(); 8057 8058 Register SrcReg = MI.getOperand(0).getReg(); 8059 8060 // Create new virtual register of the same class as source. 8061 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg); 8062 Register DstReg = MRI->createVirtualRegister(RC); 8063 8064 // Replace pseudo with a normal load-and-test that models the def as 8065 // well. 8066 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg) 8067 .addReg(SrcReg) 8068 .setMIFlags(MI.getFlags()); 8069 MI.eraseFromParent(); 8070 8071 return MBB; 8072 } 8073 8074 MachineBasicBlock *SystemZTargetLowering::emitProbedAlloca( 8075 MachineInstr &MI, MachineBasicBlock *MBB) const { 8076 MachineFunction &MF = *MBB->getParent(); 8077 MachineRegisterInfo *MRI = &MF.getRegInfo(); 8078 const SystemZInstrInfo *TII = 8079 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 8080 DebugLoc DL = MI.getDebugLoc(); 8081 const unsigned ProbeSize = getStackProbeSize(MF); 8082 Register DstReg = MI.getOperand(0).getReg(); 8083 Register SizeReg = MI.getOperand(2).getReg(); 8084 8085 MachineBasicBlock *StartMBB = MBB; 8086 MachineBasicBlock *DoneMBB = SystemZ::splitBlockAfter(MI, MBB); 8087 MachineBasicBlock *LoopTestMBB = SystemZ::emitBlockAfter(StartMBB); 8088 MachineBasicBlock *LoopBodyMBB = SystemZ::emitBlockAfter(LoopTestMBB); 8089 MachineBasicBlock *TailTestMBB = SystemZ::emitBlockAfter(LoopBodyMBB); 8090 MachineBasicBlock *TailMBB = SystemZ::emitBlockAfter(TailTestMBB); 8091 8092 MachineMemOperand *VolLdMMO = MF.getMachineMemOperand(MachinePointerInfo(), 8093 MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad, 8, Align(1)); 8094 8095 Register PHIReg = MRI->createVirtualRegister(&SystemZ::ADDR64BitRegClass); 8096 Register IncReg = MRI->createVirtualRegister(&SystemZ::ADDR64BitRegClass); 8097 8098 // LoopTestMBB 8099 // BRC TailTestMBB 8100 // # fallthrough to LoopBodyMBB 8101 StartMBB->addSuccessor(LoopTestMBB); 8102 MBB = LoopTestMBB; 8103 BuildMI(MBB, DL, TII->get(SystemZ::PHI), PHIReg) 8104 .addReg(SizeReg) 8105 .addMBB(StartMBB) 8106 .addReg(IncReg) 8107 .addMBB(LoopBodyMBB); 8108 BuildMI(MBB, DL, TII->get(SystemZ::CLGFI)) 8109 .addReg(PHIReg) 8110 .addImm(ProbeSize); 8111 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 8112 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_LT) 8113 .addMBB(TailTestMBB); 8114 MBB->addSuccessor(LoopBodyMBB); 8115 MBB->addSuccessor(TailTestMBB); 8116 8117 // LoopBodyMBB: Allocate and probe by means of a volatile compare. 8118 // J LoopTestMBB 8119 MBB = LoopBodyMBB; 8120 BuildMI(MBB, DL, TII->get(SystemZ::SLGFI), IncReg) 8121 .addReg(PHIReg) 8122 .addImm(ProbeSize); 8123 BuildMI(MBB, DL, TII->get(SystemZ::SLGFI), SystemZ::R15D) 8124 .addReg(SystemZ::R15D) 8125 .addImm(ProbeSize); 8126 BuildMI(MBB, DL, TII->get(SystemZ::CG)).addReg(SystemZ::R15D) 8127 .addReg(SystemZ::R15D).addImm(ProbeSize - 8).addReg(0) 8128 .setMemRefs(VolLdMMO); 8129 BuildMI(MBB, DL, TII->get(SystemZ::J)).addMBB(LoopTestMBB); 8130 MBB->addSuccessor(LoopTestMBB); 8131 8132 // TailTestMBB 8133 // BRC DoneMBB 8134 // # fallthrough to TailMBB 8135 MBB = TailTestMBB; 8136 BuildMI(MBB, DL, TII->get(SystemZ::CGHI)) 8137 .addReg(PHIReg) 8138 .addImm(0); 8139 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 8140 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_EQ) 8141 .addMBB(DoneMBB); 8142 MBB->addSuccessor(TailMBB); 8143 MBB->addSuccessor(DoneMBB); 8144 8145 // TailMBB 8146 // # fallthrough to DoneMBB 8147 MBB = TailMBB; 8148 BuildMI(MBB, DL, TII->get(SystemZ::SLGR), SystemZ::R15D) 8149 .addReg(SystemZ::R15D) 8150 .addReg(PHIReg); 8151 BuildMI(MBB, DL, TII->get(SystemZ::CG)).addReg(SystemZ::R15D) 8152 .addReg(SystemZ::R15D).addImm(-8).addReg(PHIReg) 8153 .setMemRefs(VolLdMMO); 8154 MBB->addSuccessor(DoneMBB); 8155 8156 // DoneMBB 8157 MBB = DoneMBB; 8158 BuildMI(*MBB, MBB->begin(), DL, TII->get(TargetOpcode::COPY), DstReg) 8159 .addReg(SystemZ::R15D); 8160 8161 MI.eraseFromParent(); 8162 return DoneMBB; 8163 } 8164 8165 SDValue SystemZTargetLowering:: 8166 getBackchainAddress(SDValue SP, SelectionDAG &DAG) const { 8167 MachineFunction &MF = DAG.getMachineFunction(); 8168 auto *TFL = 8169 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering()); 8170 SDLoc DL(SP); 8171 return DAG.getNode(ISD::ADD, DL, MVT::i64, SP, 8172 DAG.getIntPtrConstant(TFL->getBackchainOffset(MF), DL)); 8173 } 8174 8175 MachineBasicBlock *SystemZTargetLowering::EmitInstrWithCustomInserter( 8176 MachineInstr &MI, MachineBasicBlock *MBB) const { 8177 switch (MI.getOpcode()) { 8178 case SystemZ::Select32: 8179 case SystemZ::Select64: 8180 case SystemZ::SelectF32: 8181 case SystemZ::SelectF64: 8182 case SystemZ::SelectF128: 8183 case SystemZ::SelectVR32: 8184 case SystemZ::SelectVR64: 8185 case SystemZ::SelectVR128: 8186 return emitSelect(MI, MBB); 8187 8188 case SystemZ::CondStore8Mux: 8189 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false); 8190 case SystemZ::CondStore8MuxInv: 8191 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true); 8192 case SystemZ::CondStore16Mux: 8193 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false); 8194 case SystemZ::CondStore16MuxInv: 8195 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true); 8196 case SystemZ::CondStore32Mux: 8197 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, false); 8198 case SystemZ::CondStore32MuxInv: 8199 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, true); 8200 case SystemZ::CondStore8: 8201 return emitCondStore(MI, MBB, SystemZ::STC, 0, false); 8202 case SystemZ::CondStore8Inv: 8203 return emitCondStore(MI, MBB, SystemZ::STC, 0, true); 8204 case SystemZ::CondStore16: 8205 return emitCondStore(MI, MBB, SystemZ::STH, 0, false); 8206 case SystemZ::CondStore16Inv: 8207 return emitCondStore(MI, MBB, SystemZ::STH, 0, true); 8208 case SystemZ::CondStore32: 8209 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false); 8210 case SystemZ::CondStore32Inv: 8211 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true); 8212 case SystemZ::CondStore64: 8213 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false); 8214 case SystemZ::CondStore64Inv: 8215 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true); 8216 case SystemZ::CondStoreF32: 8217 return emitCondStore(MI, MBB, SystemZ::STE, 0, false); 8218 case SystemZ::CondStoreF32Inv: 8219 return emitCondStore(MI, MBB, SystemZ::STE, 0, true); 8220 case SystemZ::CondStoreF64: 8221 return emitCondStore(MI, MBB, SystemZ::STD, 0, false); 8222 case SystemZ::CondStoreF64Inv: 8223 return emitCondStore(MI, MBB, SystemZ::STD, 0, true); 8224 8225 case SystemZ::PAIR128: 8226 return emitPair128(MI, MBB); 8227 case SystemZ::AEXT128: 8228 return emitExt128(MI, MBB, false); 8229 case SystemZ::ZEXT128: 8230 return emitExt128(MI, MBB, true); 8231 8232 case SystemZ::ATOMIC_SWAPW: 8233 return emitAtomicLoadBinary(MI, MBB, 0, 0); 8234 case SystemZ::ATOMIC_SWAP_32: 8235 return emitAtomicLoadBinary(MI, MBB, 0, 32); 8236 case SystemZ::ATOMIC_SWAP_64: 8237 return emitAtomicLoadBinary(MI, MBB, 0, 64); 8238 8239 case SystemZ::ATOMIC_LOADW_AR: 8240 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0); 8241 case SystemZ::ATOMIC_LOADW_AFI: 8242 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0); 8243 case SystemZ::ATOMIC_LOAD_AR: 8244 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32); 8245 case SystemZ::ATOMIC_LOAD_AHI: 8246 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32); 8247 case SystemZ::ATOMIC_LOAD_AFI: 8248 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32); 8249 case SystemZ::ATOMIC_LOAD_AGR: 8250 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64); 8251 case SystemZ::ATOMIC_LOAD_AGHI: 8252 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64); 8253 case SystemZ::ATOMIC_LOAD_AGFI: 8254 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64); 8255 8256 case SystemZ::ATOMIC_LOADW_SR: 8257 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0); 8258 case SystemZ::ATOMIC_LOAD_SR: 8259 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32); 8260 case SystemZ::ATOMIC_LOAD_SGR: 8261 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64); 8262 8263 case SystemZ::ATOMIC_LOADW_NR: 8264 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0); 8265 case SystemZ::ATOMIC_LOADW_NILH: 8266 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0); 8267 case SystemZ::ATOMIC_LOAD_NR: 8268 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32); 8269 case SystemZ::ATOMIC_LOAD_NILL: 8270 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32); 8271 case SystemZ::ATOMIC_LOAD_NILH: 8272 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32); 8273 case SystemZ::ATOMIC_LOAD_NILF: 8274 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32); 8275 case SystemZ::ATOMIC_LOAD_NGR: 8276 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64); 8277 case SystemZ::ATOMIC_LOAD_NILL64: 8278 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64); 8279 case SystemZ::ATOMIC_LOAD_NILH64: 8280 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64); 8281 case SystemZ::ATOMIC_LOAD_NIHL64: 8282 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64); 8283 case SystemZ::ATOMIC_LOAD_NIHH64: 8284 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64); 8285 case SystemZ::ATOMIC_LOAD_NILF64: 8286 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64); 8287 case SystemZ::ATOMIC_LOAD_NIHF64: 8288 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64); 8289 8290 case SystemZ::ATOMIC_LOADW_OR: 8291 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0); 8292 case SystemZ::ATOMIC_LOADW_OILH: 8293 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0); 8294 case SystemZ::ATOMIC_LOAD_OR: 8295 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32); 8296 case SystemZ::ATOMIC_LOAD_OILL: 8297 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32); 8298 case SystemZ::ATOMIC_LOAD_OILH: 8299 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32); 8300 case SystemZ::ATOMIC_LOAD_OILF: 8301 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32); 8302 case SystemZ::ATOMIC_LOAD_OGR: 8303 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64); 8304 case SystemZ::ATOMIC_LOAD_OILL64: 8305 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64); 8306 case SystemZ::ATOMIC_LOAD_OILH64: 8307 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64); 8308 case SystemZ::ATOMIC_LOAD_OIHL64: 8309 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64); 8310 case SystemZ::ATOMIC_LOAD_OIHH64: 8311 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64); 8312 case SystemZ::ATOMIC_LOAD_OILF64: 8313 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64); 8314 case SystemZ::ATOMIC_LOAD_OIHF64: 8315 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64); 8316 8317 case SystemZ::ATOMIC_LOADW_XR: 8318 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0); 8319 case SystemZ::ATOMIC_LOADW_XILF: 8320 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0); 8321 case SystemZ::ATOMIC_LOAD_XR: 8322 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32); 8323 case SystemZ::ATOMIC_LOAD_XILF: 8324 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32); 8325 case SystemZ::ATOMIC_LOAD_XGR: 8326 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64); 8327 case SystemZ::ATOMIC_LOAD_XILF64: 8328 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64); 8329 case SystemZ::ATOMIC_LOAD_XIHF64: 8330 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64); 8331 8332 case SystemZ::ATOMIC_LOADW_NRi: 8333 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true); 8334 case SystemZ::ATOMIC_LOADW_NILHi: 8335 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true); 8336 case SystemZ::ATOMIC_LOAD_NRi: 8337 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true); 8338 case SystemZ::ATOMIC_LOAD_NILLi: 8339 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true); 8340 case SystemZ::ATOMIC_LOAD_NILHi: 8341 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true); 8342 case SystemZ::ATOMIC_LOAD_NILFi: 8343 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true); 8344 case SystemZ::ATOMIC_LOAD_NGRi: 8345 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true); 8346 case SystemZ::ATOMIC_LOAD_NILL64i: 8347 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true); 8348 case SystemZ::ATOMIC_LOAD_NILH64i: 8349 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true); 8350 case SystemZ::ATOMIC_LOAD_NIHL64i: 8351 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true); 8352 case SystemZ::ATOMIC_LOAD_NIHH64i: 8353 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true); 8354 case SystemZ::ATOMIC_LOAD_NILF64i: 8355 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true); 8356 case SystemZ::ATOMIC_LOAD_NIHF64i: 8357 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true); 8358 8359 case SystemZ::ATOMIC_LOADW_MIN: 8360 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 8361 SystemZ::CCMASK_CMP_LE, 0); 8362 case SystemZ::ATOMIC_LOAD_MIN_32: 8363 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 8364 SystemZ::CCMASK_CMP_LE, 32); 8365 case SystemZ::ATOMIC_LOAD_MIN_64: 8366 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, 8367 SystemZ::CCMASK_CMP_LE, 64); 8368 8369 case SystemZ::ATOMIC_LOADW_MAX: 8370 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 8371 SystemZ::CCMASK_CMP_GE, 0); 8372 case SystemZ::ATOMIC_LOAD_MAX_32: 8373 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 8374 SystemZ::CCMASK_CMP_GE, 32); 8375 case SystemZ::ATOMIC_LOAD_MAX_64: 8376 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, 8377 SystemZ::CCMASK_CMP_GE, 64); 8378 8379 case SystemZ::ATOMIC_LOADW_UMIN: 8380 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 8381 SystemZ::CCMASK_CMP_LE, 0); 8382 case SystemZ::ATOMIC_LOAD_UMIN_32: 8383 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 8384 SystemZ::CCMASK_CMP_LE, 32); 8385 case SystemZ::ATOMIC_LOAD_UMIN_64: 8386 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, 8387 SystemZ::CCMASK_CMP_LE, 64); 8388 8389 case SystemZ::ATOMIC_LOADW_UMAX: 8390 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 8391 SystemZ::CCMASK_CMP_GE, 0); 8392 case SystemZ::ATOMIC_LOAD_UMAX_32: 8393 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 8394 SystemZ::CCMASK_CMP_GE, 32); 8395 case SystemZ::ATOMIC_LOAD_UMAX_64: 8396 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, 8397 SystemZ::CCMASK_CMP_GE, 64); 8398 8399 case SystemZ::ATOMIC_CMP_SWAPW: 8400 return emitAtomicCmpSwapW(MI, MBB); 8401 case SystemZ::MVCSequence: 8402 case SystemZ::MVCLoop: 8403 return emitMemMemWrapper(MI, MBB, SystemZ::MVC); 8404 case SystemZ::NCSequence: 8405 case SystemZ::NCLoop: 8406 return emitMemMemWrapper(MI, MBB, SystemZ::NC); 8407 case SystemZ::OCSequence: 8408 case SystemZ::OCLoop: 8409 return emitMemMemWrapper(MI, MBB, SystemZ::OC); 8410 case SystemZ::XCSequence: 8411 case SystemZ::XCLoop: 8412 return emitMemMemWrapper(MI, MBB, SystemZ::XC); 8413 case SystemZ::CLCSequence: 8414 case SystemZ::CLCLoop: 8415 return emitMemMemWrapper(MI, MBB, SystemZ::CLC); 8416 case SystemZ::CLSTLoop: 8417 return emitStringWrapper(MI, MBB, SystemZ::CLST); 8418 case SystemZ::MVSTLoop: 8419 return emitStringWrapper(MI, MBB, SystemZ::MVST); 8420 case SystemZ::SRSTLoop: 8421 return emitStringWrapper(MI, MBB, SystemZ::SRST); 8422 case SystemZ::TBEGIN: 8423 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false); 8424 case SystemZ::TBEGIN_nofloat: 8425 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true); 8426 case SystemZ::TBEGINC: 8427 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true); 8428 case SystemZ::LTEBRCompare_VecPseudo: 8429 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR); 8430 case SystemZ::LTDBRCompare_VecPseudo: 8431 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR); 8432 case SystemZ::LTXBRCompare_VecPseudo: 8433 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR); 8434 8435 case SystemZ::PROBED_ALLOCA: 8436 return emitProbedAlloca(MI, MBB); 8437 8438 case TargetOpcode::STACKMAP: 8439 case TargetOpcode::PATCHPOINT: 8440 return emitPatchPoint(MI, MBB); 8441 8442 default: 8443 llvm_unreachable("Unexpected instr type to insert"); 8444 } 8445 } 8446 8447 // This is only used by the isel schedulers, and is needed only to prevent 8448 // compiler from crashing when list-ilp is used. 8449 const TargetRegisterClass * 8450 SystemZTargetLowering::getRepRegClassFor(MVT VT) const { 8451 if (VT == MVT::Untyped) 8452 return &SystemZ::ADDR128BitRegClass; 8453 return TargetLowering::getRepRegClassFor(VT); 8454 } 8455