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