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