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