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