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