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