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