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