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