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