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