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