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