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