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