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 BaseOp = SystemZISD::ADDCARRY; 3453 CCValid = SystemZ::CCMASK_LOGICAL; 3454 CCMask = SystemZ::CCMASK_LOGICAL_CARRY; 3455 break; 3456 case ISD::SUBCARRY: 3457 BaseOp = SystemZISD::SUBCARRY; 3458 CCValid = SystemZ::CCMASK_LOGICAL; 3459 CCMask = SystemZ::CCMASK_LOGICAL_BORROW; 3460 break; 3461 } 3462 3463 // Set the condition code from the carry flag. 3464 Carry = DAG.getNode(SystemZISD::GET_CCMASK, DL, MVT::i32, Carry, 3465 DAG.getConstant(CCValid, DL, MVT::i32), 3466 DAG.getConstant(CCMask, DL, MVT::i32)); 3467 3468 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 3469 SDValue Result = DAG.getNode(BaseOp, DL, VTs, LHS, RHS, Carry); 3470 3471 SDValue SetCC = emitSETCC(DAG, DL, Result.getValue(1), CCValid, CCMask); 3472 if (N->getValueType(1) == MVT::i1) 3473 SetCC = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, SetCC); 3474 3475 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Result, SetCC); 3476 } 3477 3478 SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op, 3479 SelectionDAG &DAG) const { 3480 EVT VT = Op.getValueType(); 3481 SDLoc DL(Op); 3482 Op = Op.getOperand(0); 3483 3484 // Handle vector types via VPOPCT. 3485 if (VT.isVector()) { 3486 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op); 3487 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op); 3488 switch (VT.getScalarSizeInBits()) { 3489 case 8: 3490 break; 3491 case 16: { 3492 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op); 3493 SDValue Shift = DAG.getConstant(8, DL, MVT::i32); 3494 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift); 3495 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp); 3496 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift); 3497 break; 3498 } 3499 case 32: { 3500 SDValue Tmp = DAG.getSplatBuildVector(MVT::v16i8, DL, 3501 DAG.getConstant(0, DL, MVT::i32)); 3502 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp); 3503 break; 3504 } 3505 case 64: { 3506 SDValue Tmp = DAG.getSplatBuildVector(MVT::v16i8, DL, 3507 DAG.getConstant(0, DL, MVT::i32)); 3508 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp); 3509 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp); 3510 break; 3511 } 3512 default: 3513 llvm_unreachable("Unexpected type"); 3514 } 3515 return Op; 3516 } 3517 3518 // Get the known-zero mask for the operand. 3519 KnownBits Known = DAG.computeKnownBits(Op); 3520 unsigned NumSignificantBits = (~Known.Zero).getActiveBits(); 3521 if (NumSignificantBits == 0) 3522 return DAG.getConstant(0, DL, VT); 3523 3524 // Skip known-zero high parts of the operand. 3525 int64_t OrigBitSize = VT.getSizeInBits(); 3526 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits); 3527 BitSize = std::min(BitSize, OrigBitSize); 3528 3529 // The POPCNT instruction counts the number of bits in each byte. 3530 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op); 3531 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op); 3532 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op); 3533 3534 // Add up per-byte counts in a binary tree. All bits of Op at 3535 // position larger than BitSize remain zero throughout. 3536 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) { 3537 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT)); 3538 if (BitSize != OrigBitSize) 3539 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp, 3540 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT)); 3541 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp); 3542 } 3543 3544 // Extract overall result from high byte. 3545 if (BitSize > 8) 3546 Op = DAG.getNode(ISD::SRL, DL, VT, Op, 3547 DAG.getConstant(BitSize - 8, DL, VT)); 3548 3549 return Op; 3550 } 3551 3552 SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op, 3553 SelectionDAG &DAG) const { 3554 SDLoc DL(Op); 3555 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>( 3556 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()); 3557 SyncScope::ID FenceSSID = static_cast<SyncScope::ID>( 3558 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue()); 3559 3560 // The only fence that needs an instruction is a sequentially-consistent 3561 // cross-thread fence. 3562 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent && 3563 FenceSSID == SyncScope::System) { 3564 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other, 3565 Op.getOperand(0)), 3566 0); 3567 } 3568 3569 // MEMBARRIER is a compiler barrier; it codegens to a no-op. 3570 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0)); 3571 } 3572 3573 // Op is an atomic load. Lower it into a normal volatile load. 3574 SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op, 3575 SelectionDAG &DAG) const { 3576 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3577 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(), 3578 Node->getChain(), Node->getBasePtr(), 3579 Node->getMemoryVT(), Node->getMemOperand()); 3580 } 3581 3582 // Op is an atomic store. Lower it into a normal volatile store. 3583 SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op, 3584 SelectionDAG &DAG) const { 3585 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3586 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(), 3587 Node->getBasePtr(), Node->getMemoryVT(), 3588 Node->getMemOperand()); 3589 // We have to enforce sequential consistency by performing a 3590 // serialization operation after the store. 3591 if (Node->getOrdering() == AtomicOrdering::SequentiallyConsistent) 3592 Chain = SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), 3593 MVT::Other, Chain), 0); 3594 return Chain; 3595 } 3596 3597 // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first 3598 // two into the fullword ATOMIC_LOADW_* operation given by Opcode. 3599 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op, 3600 SelectionDAG &DAG, 3601 unsigned Opcode) const { 3602 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3603 3604 // 32-bit operations need no code outside the main loop. 3605 EVT NarrowVT = Node->getMemoryVT(); 3606 EVT WideVT = MVT::i32; 3607 if (NarrowVT == WideVT) 3608 return Op; 3609 3610 int64_t BitSize = NarrowVT.getSizeInBits(); 3611 SDValue ChainIn = Node->getChain(); 3612 SDValue Addr = Node->getBasePtr(); 3613 SDValue Src2 = Node->getVal(); 3614 MachineMemOperand *MMO = Node->getMemOperand(); 3615 SDLoc DL(Node); 3616 EVT PtrVT = Addr.getValueType(); 3617 3618 // Convert atomic subtracts of constants into additions. 3619 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB) 3620 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) { 3621 Opcode = SystemZISD::ATOMIC_LOADW_ADD; 3622 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType()); 3623 } 3624 3625 // Get the address of the containing word. 3626 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr, 3627 DAG.getConstant(-4, DL, PtrVT)); 3628 3629 // Get the number of bits that the word must be rotated left in order 3630 // to bring the field to the top bits of a GR32. 3631 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr, 3632 DAG.getConstant(3, DL, PtrVT)); 3633 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift); 3634 3635 // Get the complementing shift amount, for rotating a field in the top 3636 // bits back to its proper position. 3637 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT, 3638 DAG.getConstant(0, DL, WideVT), BitShift); 3639 3640 // Extend the source operand to 32 bits and prepare it for the inner loop. 3641 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other 3642 // operations require the source to be shifted in advance. (This shift 3643 // can be folded if the source is constant.) For AND and NAND, the lower 3644 // bits must be set, while for other opcodes they should be left clear. 3645 if (Opcode != SystemZISD::ATOMIC_SWAPW) 3646 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2, 3647 DAG.getConstant(32 - BitSize, DL, WideVT)); 3648 if (Opcode == SystemZISD::ATOMIC_LOADW_AND || 3649 Opcode == SystemZISD::ATOMIC_LOADW_NAND) 3650 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2, 3651 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT)); 3652 3653 // Construct the ATOMIC_LOADW_* node. 3654 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other); 3655 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift, 3656 DAG.getConstant(BitSize, DL, WideVT) }; 3657 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops, 3658 NarrowVT, MMO); 3659 3660 // Rotate the result of the final CS so that the field is in the lower 3661 // bits of a GR32, then truncate it. 3662 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift, 3663 DAG.getConstant(BitSize, DL, WideVT)); 3664 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift); 3665 3666 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) }; 3667 return DAG.getMergeValues(RetOps, DL); 3668 } 3669 3670 // Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations 3671 // into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit 3672 // operations into additions. 3673 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op, 3674 SelectionDAG &DAG) const { 3675 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3676 EVT MemVT = Node->getMemoryVT(); 3677 if (MemVT == MVT::i32 || MemVT == MVT::i64) { 3678 // A full-width operation. 3679 assert(Op.getValueType() == MemVT && "Mismatched VTs"); 3680 SDValue Src2 = Node->getVal(); 3681 SDValue NegSrc2; 3682 SDLoc DL(Src2); 3683 3684 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) { 3685 // Use an addition if the operand is constant and either LAA(G) is 3686 // available or the negative value is in the range of A(G)FHI. 3687 int64_t Value = (-Op2->getAPIntValue()).getSExtValue(); 3688 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1()) 3689 NegSrc2 = DAG.getConstant(Value, DL, MemVT); 3690 } else if (Subtarget.hasInterlockedAccess1()) 3691 // Use LAA(G) if available. 3692 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT), 3693 Src2); 3694 3695 if (NegSrc2.getNode()) 3696 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT, 3697 Node->getChain(), Node->getBasePtr(), NegSrc2, 3698 Node->getMemOperand()); 3699 3700 // Use the node as-is. 3701 return Op; 3702 } 3703 3704 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB); 3705 } 3706 3707 // Lower 8/16/32/64-bit ATOMIC_CMP_SWAP_WITH_SUCCESS node. 3708 SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op, 3709 SelectionDAG &DAG) const { 3710 auto *Node = cast<AtomicSDNode>(Op.getNode()); 3711 SDValue ChainIn = Node->getOperand(0); 3712 SDValue Addr = Node->getOperand(1); 3713 SDValue CmpVal = Node->getOperand(2); 3714 SDValue SwapVal = Node->getOperand(3); 3715 MachineMemOperand *MMO = Node->getMemOperand(); 3716 SDLoc DL(Node); 3717 3718 // We have native support for 32-bit and 64-bit compare and swap, but we 3719 // still need to expand extracting the "success" result from the CC. 3720 EVT NarrowVT = Node->getMemoryVT(); 3721 EVT WideVT = NarrowVT == MVT::i64 ? MVT::i64 : MVT::i32; 3722 if (NarrowVT == WideVT) { 3723 SDVTList Tys = DAG.getVTList(WideVT, MVT::i32, MVT::Other); 3724 SDValue Ops[] = { ChainIn, Addr, CmpVal, SwapVal }; 3725 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP, 3726 DL, Tys, Ops, NarrowVT, MMO); 3727 SDValue Success = emitSETCC(DAG, DL, AtomicOp.getValue(1), 3728 SystemZ::CCMASK_CS, SystemZ::CCMASK_CS_EQ); 3729 3730 DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), AtomicOp.getValue(0)); 3731 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success); 3732 DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(2)); 3733 return SDValue(); 3734 } 3735 3736 // Convert 8-bit and 16-bit compare and swap to a loop, implemented 3737 // via a fullword ATOMIC_CMP_SWAPW operation. 3738 int64_t BitSize = NarrowVT.getSizeInBits(); 3739 EVT PtrVT = Addr.getValueType(); 3740 3741 // Get the address of the containing word. 3742 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr, 3743 DAG.getConstant(-4, DL, PtrVT)); 3744 3745 // Get the number of bits that the word must be rotated left in order 3746 // to bring the field to the top bits of a GR32. 3747 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr, 3748 DAG.getConstant(3, DL, PtrVT)); 3749 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift); 3750 3751 // Get the complementing shift amount, for rotating a field in the top 3752 // bits back to its proper position. 3753 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT, 3754 DAG.getConstant(0, DL, WideVT), BitShift); 3755 3756 // Construct the ATOMIC_CMP_SWAPW node. 3757 SDVTList VTList = DAG.getVTList(WideVT, MVT::i32, MVT::Other); 3758 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift, 3759 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) }; 3760 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL, 3761 VTList, Ops, NarrowVT, MMO); 3762 SDValue Success = emitSETCC(DAG, DL, AtomicOp.getValue(1), 3763 SystemZ::CCMASK_ICMP, SystemZ::CCMASK_CMP_EQ); 3764 3765 DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), AtomicOp.getValue(0)); 3766 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success); 3767 DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(2)); 3768 return SDValue(); 3769 } 3770 3771 MachineMemOperand::Flags 3772 SystemZTargetLowering::getMMOFlags(const Instruction &I) const { 3773 // Because of how we convert atomic_load and atomic_store to normal loads and 3774 // stores in the DAG, we need to ensure that the MMOs are marked volatile 3775 // since DAGCombine hasn't been updated to account for atomic, but non 3776 // volatile loads. (See D57601) 3777 if (auto *SI = dyn_cast<StoreInst>(&I)) 3778 if (SI->isAtomic()) 3779 return MachineMemOperand::MOVolatile; 3780 if (auto *LI = dyn_cast<LoadInst>(&I)) 3781 if (LI->isAtomic()) 3782 return MachineMemOperand::MOVolatile; 3783 if (auto *AI = dyn_cast<AtomicRMWInst>(&I)) 3784 if (AI->isAtomic()) 3785 return MachineMemOperand::MOVolatile; 3786 if (auto *AI = dyn_cast<AtomicCmpXchgInst>(&I)) 3787 if (AI->isAtomic()) 3788 return MachineMemOperand::MOVolatile; 3789 return MachineMemOperand::MONone; 3790 } 3791 3792 SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op, 3793 SelectionDAG &DAG) const { 3794 MachineFunction &MF = DAG.getMachineFunction(); 3795 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); 3796 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op), 3797 SystemZ::R15D, Op.getValueType()); 3798 } 3799 3800 SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op, 3801 SelectionDAG &DAG) const { 3802 MachineFunction &MF = DAG.getMachineFunction(); 3803 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); 3804 bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain"); 3805 3806 SDValue Chain = Op.getOperand(0); 3807 SDValue NewSP = Op.getOperand(1); 3808 SDValue Backchain; 3809 SDLoc DL(Op); 3810 3811 if (StoreBackchain) { 3812 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64); 3813 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo()); 3814 } 3815 3816 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP); 3817 3818 if (StoreBackchain) 3819 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo()); 3820 3821 return Chain; 3822 } 3823 3824 SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op, 3825 SelectionDAG &DAG) const { 3826 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 3827 if (!IsData) 3828 // Just preserve the chain. 3829 return Op.getOperand(0); 3830 3831 SDLoc DL(Op); 3832 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); 3833 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ; 3834 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode()); 3835 SDValue Ops[] = { 3836 Op.getOperand(0), 3837 DAG.getConstant(Code, DL, MVT::i32), 3838 Op.getOperand(1) 3839 }; 3840 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL, 3841 Node->getVTList(), Ops, 3842 Node->getMemoryVT(), Node->getMemOperand()); 3843 } 3844 3845 // Convert condition code in CCReg to an i32 value. 3846 static SDValue getCCResult(SelectionDAG &DAG, SDValue CCReg) { 3847 SDLoc DL(CCReg); 3848 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, CCReg); 3849 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM, 3850 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32)); 3851 } 3852 3853 SDValue 3854 SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op, 3855 SelectionDAG &DAG) const { 3856 unsigned Opcode, CCValid; 3857 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) { 3858 assert(Op->getNumValues() == 2 && "Expected only CC result and chain"); 3859 SDNode *Node = emitIntrinsicWithCCAndChain(DAG, Op, Opcode); 3860 SDValue CC = getCCResult(DAG, SDValue(Node, 0)); 3861 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC); 3862 return SDValue(); 3863 } 3864 3865 return SDValue(); 3866 } 3867 3868 SDValue 3869 SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op, 3870 SelectionDAG &DAG) const { 3871 unsigned Opcode, CCValid; 3872 if (isIntrinsicWithCC(Op, Opcode, CCValid)) { 3873 SDNode *Node = emitIntrinsicWithCC(DAG, Op, Opcode); 3874 if (Op->getNumValues() == 1) 3875 return getCCResult(DAG, SDValue(Node, 0)); 3876 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result"); 3877 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), 3878 SDValue(Node, 0), getCCResult(DAG, SDValue(Node, 1))); 3879 } 3880 3881 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 3882 switch (Id) { 3883 case Intrinsic::thread_pointer: 3884 return lowerThreadPointer(SDLoc(Op), DAG); 3885 3886 case Intrinsic::s390_vpdi: 3887 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(), 3888 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 3889 3890 case Intrinsic::s390_vperm: 3891 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(), 3892 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 3893 3894 case Intrinsic::s390_vuphb: 3895 case Intrinsic::s390_vuphh: 3896 case Intrinsic::s390_vuphf: 3897 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(), 3898 Op.getOperand(1)); 3899 3900 case Intrinsic::s390_vuplhb: 3901 case Intrinsic::s390_vuplhh: 3902 case Intrinsic::s390_vuplhf: 3903 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(), 3904 Op.getOperand(1)); 3905 3906 case Intrinsic::s390_vuplb: 3907 case Intrinsic::s390_vuplhw: 3908 case Intrinsic::s390_vuplf: 3909 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(), 3910 Op.getOperand(1)); 3911 3912 case Intrinsic::s390_vupllb: 3913 case Intrinsic::s390_vupllh: 3914 case Intrinsic::s390_vupllf: 3915 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(), 3916 Op.getOperand(1)); 3917 3918 case Intrinsic::s390_vsumb: 3919 case Intrinsic::s390_vsumh: 3920 case Intrinsic::s390_vsumgh: 3921 case Intrinsic::s390_vsumgf: 3922 case Intrinsic::s390_vsumqf: 3923 case Intrinsic::s390_vsumqg: 3924 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(), 3925 Op.getOperand(1), Op.getOperand(2)); 3926 } 3927 3928 return SDValue(); 3929 } 3930 3931 namespace { 3932 // Says that SystemZISD operation Opcode can be used to perform the equivalent 3933 // of a VPERM with permute vector Bytes. If Opcode takes three operands, 3934 // Operand is the constant third operand, otherwise it is the number of 3935 // bytes in each element of the result. 3936 struct Permute { 3937 unsigned Opcode; 3938 unsigned Operand; 3939 unsigned char Bytes[SystemZ::VectorBytes]; 3940 }; 3941 } 3942 3943 static const Permute PermuteForms[] = { 3944 // VMRHG 3945 { SystemZISD::MERGE_HIGH, 8, 3946 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } }, 3947 // VMRHF 3948 { SystemZISD::MERGE_HIGH, 4, 3949 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } }, 3950 // VMRHH 3951 { SystemZISD::MERGE_HIGH, 2, 3952 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } }, 3953 // VMRHB 3954 { SystemZISD::MERGE_HIGH, 1, 3955 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } }, 3956 // VMRLG 3957 { SystemZISD::MERGE_LOW, 8, 3958 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } }, 3959 // VMRLF 3960 { SystemZISD::MERGE_LOW, 4, 3961 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } }, 3962 // VMRLH 3963 { SystemZISD::MERGE_LOW, 2, 3964 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } }, 3965 // VMRLB 3966 { SystemZISD::MERGE_LOW, 1, 3967 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } }, 3968 // VPKG 3969 { SystemZISD::PACK, 4, 3970 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } }, 3971 // VPKF 3972 { SystemZISD::PACK, 2, 3973 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } }, 3974 // VPKH 3975 { SystemZISD::PACK, 1, 3976 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } }, 3977 // VPDI V1, V2, 4 (low half of V1, high half of V2) 3978 { SystemZISD::PERMUTE_DWORDS, 4, 3979 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } }, 3980 // VPDI V1, V2, 1 (high half of V1, low half of V2) 3981 { SystemZISD::PERMUTE_DWORDS, 1, 3982 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } } 3983 }; 3984 3985 // Called after matching a vector shuffle against a particular pattern. 3986 // Both the original shuffle and the pattern have two vector operands. 3987 // OpNos[0] is the operand of the original shuffle that should be used for 3988 // operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything. 3989 // OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and 3990 // set OpNo0 and OpNo1 to the shuffle operands that should actually be used 3991 // for operands 0 and 1 of the pattern. 3992 static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) { 3993 if (OpNos[0] < 0) { 3994 if (OpNos[1] < 0) 3995 return false; 3996 OpNo0 = OpNo1 = OpNos[1]; 3997 } else if (OpNos[1] < 0) { 3998 OpNo0 = OpNo1 = OpNos[0]; 3999 } else { 4000 OpNo0 = OpNos[0]; 4001 OpNo1 = OpNos[1]; 4002 } 4003 return true; 4004 } 4005 4006 // Bytes is a VPERM-like permute vector, except that -1 is used for 4007 // undefined bytes. Return true if the VPERM can be implemented using P. 4008 // When returning true set OpNo0 to the VPERM operand that should be 4009 // used for operand 0 of P and likewise OpNo1 for operand 1 of P. 4010 // 4011 // For example, if swapping the VPERM operands allows P to match, OpNo0 4012 // will be 1 and OpNo1 will be 0. If instead Bytes only refers to one 4013 // operand, but rewriting it to use two duplicated operands allows it to 4014 // match P, then OpNo0 and OpNo1 will be the same. 4015 static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P, 4016 unsigned &OpNo0, unsigned &OpNo1) { 4017 int OpNos[] = { -1, -1 }; 4018 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) { 4019 int Elt = Bytes[I]; 4020 if (Elt >= 0) { 4021 // Make sure that the two permute vectors use the same suboperand 4022 // byte number. Only the operand numbers (the high bits) are 4023 // allowed to differ. 4024 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1)) 4025 return false; 4026 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes; 4027 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes; 4028 // Make sure that the operand mappings are consistent with previous 4029 // elements. 4030 if (OpNos[ModelOpNo] == 1 - RealOpNo) 4031 return false; 4032 OpNos[ModelOpNo] = RealOpNo; 4033 } 4034 } 4035 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1); 4036 } 4037 4038 // As above, but search for a matching permute. 4039 static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes, 4040 unsigned &OpNo0, unsigned &OpNo1) { 4041 for (auto &P : PermuteForms) 4042 if (matchPermute(Bytes, P, OpNo0, OpNo1)) 4043 return &P; 4044 return nullptr; 4045 } 4046 4047 // Bytes is a VPERM-like permute vector, except that -1 is used for 4048 // undefined bytes. This permute is an operand of an outer permute. 4049 // See whether redistributing the -1 bytes gives a shuffle that can be 4050 // implemented using P. If so, set Transform to a VPERM-like permute vector 4051 // that, when applied to the result of P, gives the original permute in Bytes. 4052 static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes, 4053 const Permute &P, 4054 SmallVectorImpl<int> &Transform) { 4055 unsigned To = 0; 4056 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) { 4057 int Elt = Bytes[From]; 4058 if (Elt < 0) 4059 // Byte number From of the result is undefined. 4060 Transform[From] = -1; 4061 else { 4062 while (P.Bytes[To] != Elt) { 4063 To += 1; 4064 if (To == SystemZ::VectorBytes) 4065 return false; 4066 } 4067 Transform[From] = To; 4068 } 4069 } 4070 return true; 4071 } 4072 4073 // As above, but search for a matching permute. 4074 static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes, 4075 SmallVectorImpl<int> &Transform) { 4076 for (auto &P : PermuteForms) 4077 if (matchDoublePermute(Bytes, P, Transform)) 4078 return &P; 4079 return nullptr; 4080 } 4081 4082 // Convert the mask of the given shuffle op into a byte-level mask, 4083 // as if it had type vNi8. 4084 static bool getVPermMask(SDValue ShuffleOp, 4085 SmallVectorImpl<int> &Bytes) { 4086 EVT VT = ShuffleOp.getValueType(); 4087 unsigned NumElements = VT.getVectorNumElements(); 4088 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); 4089 4090 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(ShuffleOp)) { 4091 Bytes.resize(NumElements * BytesPerElement, -1); 4092 for (unsigned I = 0; I < NumElements; ++I) { 4093 int Index = VSN->getMaskElt(I); 4094 if (Index >= 0) 4095 for (unsigned J = 0; J < BytesPerElement; ++J) 4096 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J; 4097 } 4098 return true; 4099 } 4100 if (SystemZISD::SPLAT == ShuffleOp.getOpcode() && 4101 isa<ConstantSDNode>(ShuffleOp.getOperand(1))) { 4102 unsigned Index = ShuffleOp.getConstantOperandVal(1); 4103 Bytes.resize(NumElements * BytesPerElement, -1); 4104 for (unsigned I = 0; I < NumElements; ++I) 4105 for (unsigned J = 0; J < BytesPerElement; ++J) 4106 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J; 4107 return true; 4108 } 4109 return false; 4110 } 4111 4112 // Bytes is a VPERM-like permute vector, except that -1 is used for 4113 // undefined bytes. See whether bytes [Start, Start + BytesPerElement) of 4114 // the result come from a contiguous sequence of bytes from one input. 4115 // Set Base to the selector for the first byte if so. 4116 static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start, 4117 unsigned BytesPerElement, int &Base) { 4118 Base = -1; 4119 for (unsigned I = 0; I < BytesPerElement; ++I) { 4120 if (Bytes[Start + I] >= 0) { 4121 unsigned Elem = Bytes[Start + I]; 4122 if (Base < 0) { 4123 Base = Elem - I; 4124 // Make sure the bytes would come from one input operand. 4125 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size()) 4126 return false; 4127 } else if (unsigned(Base) != Elem - I) 4128 return false; 4129 } 4130 } 4131 return true; 4132 } 4133 4134 // Bytes is a VPERM-like permute vector, except that -1 is used for 4135 // undefined bytes. Return true if it can be performed using VSLDI. 4136 // When returning true, set StartIndex to the shift amount and OpNo0 4137 // and OpNo1 to the VPERM operands that should be used as the first 4138 // and second shift operand respectively. 4139 static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes, 4140 unsigned &StartIndex, unsigned &OpNo0, 4141 unsigned &OpNo1) { 4142 int OpNos[] = { -1, -1 }; 4143 int Shift = -1; 4144 for (unsigned I = 0; I < 16; ++I) { 4145 int Index = Bytes[I]; 4146 if (Index >= 0) { 4147 int ExpectedShift = (Index - I) % SystemZ::VectorBytes; 4148 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes; 4149 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes; 4150 if (Shift < 0) 4151 Shift = ExpectedShift; 4152 else if (Shift != ExpectedShift) 4153 return false; 4154 // Make sure that the operand mappings are consistent with previous 4155 // elements. 4156 if (OpNos[ModelOpNo] == 1 - RealOpNo) 4157 return false; 4158 OpNos[ModelOpNo] = RealOpNo; 4159 } 4160 } 4161 StartIndex = Shift; 4162 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1); 4163 } 4164 4165 // Create a node that performs P on operands Op0 and Op1, casting the 4166 // operands to the appropriate type. The type of the result is determined by P. 4167 static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL, 4168 const Permute &P, SDValue Op0, SDValue Op1) { 4169 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input 4170 // elements of a PACK are twice as wide as the outputs. 4171 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 : 4172 P.Opcode == SystemZISD::PACK ? P.Operand * 2 : 4173 P.Operand); 4174 // Cast both operands to the appropriate type. 4175 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8), 4176 SystemZ::VectorBytes / InBytes); 4177 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0); 4178 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1); 4179 SDValue Op; 4180 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) { 4181 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32); 4182 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2); 4183 } else if (P.Opcode == SystemZISD::PACK) { 4184 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8), 4185 SystemZ::VectorBytes / P.Operand); 4186 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1); 4187 } else { 4188 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1); 4189 } 4190 return Op; 4191 } 4192 4193 // Bytes is a VPERM-like permute vector, except that -1 is used for 4194 // undefined bytes. Implement it on operands Ops[0] and Ops[1] using 4195 // VSLDI or VPERM. 4196 static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL, 4197 SDValue *Ops, 4198 const SmallVectorImpl<int> &Bytes) { 4199 for (unsigned I = 0; I < 2; ++I) 4200 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]); 4201 4202 // First see whether VSLDI can be used. 4203 unsigned StartIndex, OpNo0, OpNo1; 4204 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1)) 4205 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0], 4206 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32)); 4207 4208 // Fall back on VPERM. Construct an SDNode for the permute vector. 4209 SDValue IndexNodes[SystemZ::VectorBytes]; 4210 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) 4211 if (Bytes[I] >= 0) 4212 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32); 4213 else 4214 IndexNodes[I] = DAG.getUNDEF(MVT::i32); 4215 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes); 4216 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2); 4217 } 4218 4219 namespace { 4220 // Describes a general N-operand vector shuffle. 4221 struct GeneralShuffle { 4222 GeneralShuffle(EVT vt) : VT(vt) {} 4223 void addUndef(); 4224 bool add(SDValue, unsigned); 4225 SDValue getNode(SelectionDAG &, const SDLoc &); 4226 4227 // The operands of the shuffle. 4228 SmallVector<SDValue, SystemZ::VectorBytes> Ops; 4229 4230 // Index I is -1 if byte I of the result is undefined. Otherwise the 4231 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand 4232 // Bytes[I] / SystemZ::VectorBytes. 4233 SmallVector<int, SystemZ::VectorBytes> Bytes; 4234 4235 // The type of the shuffle result. 4236 EVT VT; 4237 }; 4238 } 4239 4240 // Add an extra undefined element to the shuffle. 4241 void GeneralShuffle::addUndef() { 4242 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); 4243 for (unsigned I = 0; I < BytesPerElement; ++I) 4244 Bytes.push_back(-1); 4245 } 4246 4247 // Add an extra element to the shuffle, taking it from element Elem of Op. 4248 // A null Op indicates a vector input whose value will be calculated later; 4249 // there is at most one such input per shuffle and it always has the same 4250 // type as the result. Aborts and returns false if the source vector elements 4251 // of an EXTRACT_VECTOR_ELT are smaller than the destination elements. Per 4252 // LLVM they become implicitly extended, but this is rare and not optimized. 4253 bool GeneralShuffle::add(SDValue Op, unsigned Elem) { 4254 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); 4255 4256 // The source vector can have wider elements than the result, 4257 // either through an explicit TRUNCATE or because of type legalization. 4258 // We want the least significant part. 4259 EVT FromVT = Op.getNode() ? Op.getValueType() : VT; 4260 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize(); 4261 4262 // Return false if the source elements are smaller than their destination 4263 // elements. 4264 if (FromBytesPerElement < BytesPerElement) 4265 return false; 4266 4267 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes + 4268 (FromBytesPerElement - BytesPerElement)); 4269 4270 // Look through things like shuffles and bitcasts. 4271 while (Op.getNode()) { 4272 if (Op.getOpcode() == ISD::BITCAST) 4273 Op = Op.getOperand(0); 4274 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) { 4275 // See whether the bytes we need come from a contiguous part of one 4276 // operand. 4277 SmallVector<int, SystemZ::VectorBytes> OpBytes; 4278 if (!getVPermMask(Op, OpBytes)) 4279 break; 4280 int NewByte; 4281 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte)) 4282 break; 4283 if (NewByte < 0) { 4284 addUndef(); 4285 return true; 4286 } 4287 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes); 4288 Byte = unsigned(NewByte) % SystemZ::VectorBytes; 4289 } else if (Op.isUndef()) { 4290 addUndef(); 4291 return true; 4292 } else 4293 break; 4294 } 4295 4296 // Make sure that the source of the extraction is in Ops. 4297 unsigned OpNo = 0; 4298 for (; OpNo < Ops.size(); ++OpNo) 4299 if (Ops[OpNo] == Op) 4300 break; 4301 if (OpNo == Ops.size()) 4302 Ops.push_back(Op); 4303 4304 // Add the element to Bytes. 4305 unsigned Base = OpNo * SystemZ::VectorBytes + Byte; 4306 for (unsigned I = 0; I < BytesPerElement; ++I) 4307 Bytes.push_back(Base + I); 4308 4309 return true; 4310 } 4311 4312 // Return SDNodes for the completed shuffle. 4313 SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) { 4314 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector"); 4315 4316 if (Ops.size() == 0) 4317 return DAG.getUNDEF(VT); 4318 4319 // Make sure that there are at least two shuffle operands. 4320 if (Ops.size() == 1) 4321 Ops.push_back(DAG.getUNDEF(MVT::v16i8)); 4322 4323 // Create a tree of shuffles, deferring root node until after the loop. 4324 // Try to redistribute the undefined elements of non-root nodes so that 4325 // the non-root shuffles match something like a pack or merge, then adjust 4326 // the parent node's permute vector to compensate for the new order. 4327 // Among other things, this copes with vectors like <2 x i16> that were 4328 // padded with undefined elements during type legalization. 4329 // 4330 // In the best case this redistribution will lead to the whole tree 4331 // using packs and merges. It should rarely be a loss in other cases. 4332 unsigned Stride = 1; 4333 for (; Stride * 2 < Ops.size(); Stride *= 2) { 4334 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) { 4335 SDValue SubOps[] = { Ops[I], Ops[I + Stride] }; 4336 4337 // Create a mask for just these two operands. 4338 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes); 4339 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) { 4340 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes; 4341 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes; 4342 if (OpNo == I) 4343 NewBytes[J] = Byte; 4344 else if (OpNo == I + Stride) 4345 NewBytes[J] = SystemZ::VectorBytes + Byte; 4346 else 4347 NewBytes[J] = -1; 4348 } 4349 // See if it would be better to reorganize NewMask to avoid using VPERM. 4350 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes); 4351 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) { 4352 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]); 4353 // Applying NewBytesMap to Ops[I] gets back to NewBytes. 4354 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) { 4355 if (NewBytes[J] >= 0) { 4356 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes && 4357 "Invalid double permute"); 4358 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J]; 4359 } else 4360 assert(NewBytesMap[J] < 0 && "Invalid double permute"); 4361 } 4362 } else { 4363 // Just use NewBytes on the operands. 4364 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes); 4365 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) 4366 if (NewBytes[J] >= 0) 4367 Bytes[J] = I * SystemZ::VectorBytes + J; 4368 } 4369 } 4370 } 4371 4372 // Now we just have 2 inputs. Put the second operand in Ops[1]. 4373 if (Stride > 1) { 4374 Ops[1] = Ops[Stride]; 4375 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) 4376 if (Bytes[I] >= int(SystemZ::VectorBytes)) 4377 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes; 4378 } 4379 4380 // Look for an instruction that can do the permute without resorting 4381 // to VPERM. 4382 unsigned OpNo0, OpNo1; 4383 SDValue Op; 4384 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1)) 4385 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]); 4386 else 4387 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes); 4388 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 4389 } 4390 4391 // Return true if the given BUILD_VECTOR is a scalar-to-vector conversion. 4392 static bool isScalarToVector(SDValue Op) { 4393 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I) 4394 if (!Op.getOperand(I).isUndef()) 4395 return false; 4396 return true; 4397 } 4398 4399 // Return a vector of type VT that contains Value in the first element. 4400 // The other elements don't matter. 4401 static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT, 4402 SDValue Value) { 4403 // If we have a constant, replicate it to all elements and let the 4404 // BUILD_VECTOR lowering take care of it. 4405 if (Value.getOpcode() == ISD::Constant || 4406 Value.getOpcode() == ISD::ConstantFP) { 4407 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value); 4408 return DAG.getBuildVector(VT, DL, Ops); 4409 } 4410 if (Value.isUndef()) 4411 return DAG.getUNDEF(VT); 4412 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value); 4413 } 4414 4415 // Return a vector of type VT in which Op0 is in element 0 and Op1 is in 4416 // element 1. Used for cases in which replication is cheap. 4417 static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT, 4418 SDValue Op0, SDValue Op1) { 4419 if (Op0.isUndef()) { 4420 if (Op1.isUndef()) 4421 return DAG.getUNDEF(VT); 4422 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1); 4423 } 4424 if (Op1.isUndef()) 4425 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0); 4426 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT, 4427 buildScalarToVector(DAG, DL, VT, Op0), 4428 buildScalarToVector(DAG, DL, VT, Op1)); 4429 } 4430 4431 // Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64 4432 // vector for them. 4433 static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0, 4434 SDValue Op1) { 4435 if (Op0.isUndef() && Op1.isUndef()) 4436 return DAG.getUNDEF(MVT::v2i64); 4437 // If one of the two inputs is undefined then replicate the other one, 4438 // in order to avoid using another register unnecessarily. 4439 if (Op0.isUndef()) 4440 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1); 4441 else if (Op1.isUndef()) 4442 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0); 4443 else { 4444 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0); 4445 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1); 4446 } 4447 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1); 4448 } 4449 4450 // If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually 4451 // better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for 4452 // the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR 4453 // would benefit from this representation and return it if so. 4454 static SDValue tryBuildVectorShuffle(SelectionDAG &DAG, 4455 BuildVectorSDNode *BVN) { 4456 EVT VT = BVN->getValueType(0); 4457 unsigned NumElements = VT.getVectorNumElements(); 4458 4459 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation 4460 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still 4461 // need a BUILD_VECTOR, add an additional placeholder operand for that 4462 // BUILD_VECTOR and store its operands in ResidueOps. 4463 GeneralShuffle GS(VT); 4464 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps; 4465 bool FoundOne = false; 4466 for (unsigned I = 0; I < NumElements; ++I) { 4467 SDValue Op = BVN->getOperand(I); 4468 if (Op.getOpcode() == ISD::TRUNCATE) 4469 Op = Op.getOperand(0); 4470 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 4471 Op.getOperand(1).getOpcode() == ISD::Constant) { 4472 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 4473 if (!GS.add(Op.getOperand(0), Elem)) 4474 return SDValue(); 4475 FoundOne = true; 4476 } else if (Op.isUndef()) { 4477 GS.addUndef(); 4478 } else { 4479 if (!GS.add(SDValue(), ResidueOps.size())) 4480 return SDValue(); 4481 ResidueOps.push_back(BVN->getOperand(I)); 4482 } 4483 } 4484 4485 // Nothing to do if there are no EXTRACT_VECTOR_ELTs. 4486 if (!FoundOne) 4487 return SDValue(); 4488 4489 // Create the BUILD_VECTOR for the remaining elements, if any. 4490 if (!ResidueOps.empty()) { 4491 while (ResidueOps.size() < NumElements) 4492 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType())); 4493 for (auto &Op : GS.Ops) { 4494 if (!Op.getNode()) { 4495 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps); 4496 break; 4497 } 4498 } 4499 } 4500 return GS.getNode(DAG, SDLoc(BVN)); 4501 } 4502 4503 // Combine GPR scalar values Elems into a vector of type VT. 4504 static SDValue buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT, 4505 SmallVectorImpl<SDValue> &Elems) { 4506 // See whether there is a single replicated value. 4507 SDValue Single; 4508 unsigned int NumElements = Elems.size(); 4509 unsigned int Count = 0; 4510 for (auto Elem : Elems) { 4511 if (!Elem.isUndef()) { 4512 if (!Single.getNode()) 4513 Single = Elem; 4514 else if (Elem != Single) { 4515 Single = SDValue(); 4516 break; 4517 } 4518 Count += 1; 4519 } 4520 } 4521 // There are three cases here: 4522 // 4523 // - if the only defined element is a loaded one, the best sequence 4524 // is a replicating load. 4525 // 4526 // - otherwise, if the only defined element is an i64 value, we will 4527 // end up with the same VLVGP sequence regardless of whether we short-cut 4528 // for replication or fall through to the later code. 4529 // 4530 // - otherwise, if the only defined element is an i32 or smaller value, 4531 // we would need 2 instructions to replicate it: VLVGP followed by VREPx. 4532 // This is only a win if the single defined element is used more than once. 4533 // In other cases we're better off using a single VLVGx. 4534 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD)) 4535 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single); 4536 4537 // If all elements are loads, use VLREP/VLEs (below). 4538 bool AllLoads = true; 4539 for (auto Elem : Elems) 4540 if (Elem.getOpcode() != ISD::LOAD || cast<LoadSDNode>(Elem)->isIndexed()) { 4541 AllLoads = false; 4542 break; 4543 } 4544 4545 // The best way of building a v2i64 from two i64s is to use VLVGP. 4546 if (VT == MVT::v2i64 && !AllLoads) 4547 return joinDwords(DAG, DL, Elems[0], Elems[1]); 4548 4549 // Use a 64-bit merge high to combine two doubles. 4550 if (VT == MVT::v2f64 && !AllLoads) 4551 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]); 4552 4553 // Build v4f32 values directly from the FPRs: 4554 // 4555 // <Axxx> <Bxxx> <Cxxxx> <Dxxx> 4556 // V V VMRHF 4557 // <ABxx> <CDxx> 4558 // V VMRHG 4559 // <ABCD> 4560 if (VT == MVT::v4f32 && !AllLoads) { 4561 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]); 4562 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]); 4563 // Avoid unnecessary undefs by reusing the other operand. 4564 if (Op01.isUndef()) 4565 Op01 = Op23; 4566 else if (Op23.isUndef()) 4567 Op23 = Op01; 4568 // Merging identical replications is a no-op. 4569 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23) 4570 return Op01; 4571 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01); 4572 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23); 4573 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH, 4574 DL, MVT::v2i64, Op01, Op23); 4575 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 4576 } 4577 4578 // Collect the constant terms. 4579 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue()); 4580 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false); 4581 4582 unsigned NumConstants = 0; 4583 for (unsigned I = 0; I < NumElements; ++I) { 4584 SDValue Elem = Elems[I]; 4585 if (Elem.getOpcode() == ISD::Constant || 4586 Elem.getOpcode() == ISD::ConstantFP) { 4587 NumConstants += 1; 4588 Constants[I] = Elem; 4589 Done[I] = true; 4590 } 4591 } 4592 // If there was at least one constant, fill in the other elements of 4593 // Constants with undefs to get a full vector constant and use that 4594 // as the starting point. 4595 SDValue Result; 4596 SDValue ReplicatedVal; 4597 if (NumConstants > 0) { 4598 for (unsigned I = 0; I < NumElements; ++I) 4599 if (!Constants[I].getNode()) 4600 Constants[I] = DAG.getUNDEF(Elems[I].getValueType()); 4601 Result = DAG.getBuildVector(VT, DL, Constants); 4602 } else { 4603 // Otherwise try to use VLREP or VLVGP to start the sequence in order to 4604 // avoid a false dependency on any previous contents of the vector 4605 // register. 4606 4607 // Use a VLREP if at least one element is a load. Make sure to replicate 4608 // the load with the most elements having its value. 4609 std::map<const SDNode*, unsigned> UseCounts; 4610 SDNode *LoadMaxUses = nullptr; 4611 for (unsigned I = 0; I < NumElements; ++I) 4612 if (Elems[I].getOpcode() == ISD::LOAD && 4613 cast<LoadSDNode>(Elems[I])->isUnindexed()) { 4614 SDNode *Ld = Elems[I].getNode(); 4615 UseCounts[Ld]++; 4616 if (LoadMaxUses == nullptr || UseCounts[LoadMaxUses] < UseCounts[Ld]) 4617 LoadMaxUses = Ld; 4618 } 4619 if (LoadMaxUses != nullptr) { 4620 ReplicatedVal = SDValue(LoadMaxUses, 0); 4621 Result = DAG.getNode(SystemZISD::REPLICATE, DL, VT, ReplicatedVal); 4622 } else { 4623 // Try to use VLVGP. 4624 unsigned I1 = NumElements / 2 - 1; 4625 unsigned I2 = NumElements - 1; 4626 bool Def1 = !Elems[I1].isUndef(); 4627 bool Def2 = !Elems[I2].isUndef(); 4628 if (Def1 || Def2) { 4629 SDValue Elem1 = Elems[Def1 ? I1 : I2]; 4630 SDValue Elem2 = Elems[Def2 ? I2 : I1]; 4631 Result = DAG.getNode(ISD::BITCAST, DL, VT, 4632 joinDwords(DAG, DL, Elem1, Elem2)); 4633 Done[I1] = true; 4634 Done[I2] = true; 4635 } else 4636 Result = DAG.getUNDEF(VT); 4637 } 4638 } 4639 4640 // Use VLVGx to insert the other elements. 4641 for (unsigned I = 0; I < NumElements; ++I) 4642 if (!Done[I] && !Elems[I].isUndef() && Elems[I] != ReplicatedVal) 4643 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I], 4644 DAG.getConstant(I, DL, MVT::i32)); 4645 return Result; 4646 } 4647 4648 SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op, 4649 SelectionDAG &DAG) const { 4650 auto *BVN = cast<BuildVectorSDNode>(Op.getNode()); 4651 SDLoc DL(Op); 4652 EVT VT = Op.getValueType(); 4653 4654 if (BVN->isConstant()) { 4655 if (SystemZVectorConstantInfo(BVN).isVectorConstantLegal(Subtarget)) 4656 return Op; 4657 4658 // Fall back to loading it from memory. 4659 return SDValue(); 4660 } 4661 4662 // See if we should use shuffles to construct the vector from other vectors. 4663 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN)) 4664 return Res; 4665 4666 // Detect SCALAR_TO_VECTOR conversions. 4667 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op)) 4668 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0)); 4669 4670 // Otherwise use buildVector to build the vector up from GPRs. 4671 unsigned NumElements = Op.getNumOperands(); 4672 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements); 4673 for (unsigned I = 0; I < NumElements; ++I) 4674 Ops[I] = Op.getOperand(I); 4675 return buildVector(DAG, DL, VT, Ops); 4676 } 4677 4678 SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op, 4679 SelectionDAG &DAG) const { 4680 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode()); 4681 SDLoc DL(Op); 4682 EVT VT = Op.getValueType(); 4683 unsigned NumElements = VT.getVectorNumElements(); 4684 4685 if (VSN->isSplat()) { 4686 SDValue Op0 = Op.getOperand(0); 4687 unsigned Index = VSN->getSplatIndex(); 4688 assert(Index < VT.getVectorNumElements() && 4689 "Splat index should be defined and in first operand"); 4690 // See whether the value we're splatting is directly available as a scalar. 4691 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) || 4692 Op0.getOpcode() == ISD::BUILD_VECTOR) 4693 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index)); 4694 // Otherwise keep it as a vector-to-vector operation. 4695 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0), 4696 DAG.getConstant(Index, DL, MVT::i32)); 4697 } 4698 4699 GeneralShuffle GS(VT); 4700 for (unsigned I = 0; I < NumElements; ++I) { 4701 int Elt = VSN->getMaskElt(I); 4702 if (Elt < 0) 4703 GS.addUndef(); 4704 else if (!GS.add(Op.getOperand(unsigned(Elt) / NumElements), 4705 unsigned(Elt) % NumElements)) 4706 return SDValue(); 4707 } 4708 return GS.getNode(DAG, SDLoc(VSN)); 4709 } 4710 4711 SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op, 4712 SelectionDAG &DAG) const { 4713 SDLoc DL(Op); 4714 // Just insert the scalar into element 0 of an undefined vector. 4715 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, 4716 Op.getValueType(), DAG.getUNDEF(Op.getValueType()), 4717 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32)); 4718 } 4719 4720 SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op, 4721 SelectionDAG &DAG) const { 4722 // Handle insertions of floating-point values. 4723 SDLoc DL(Op); 4724 SDValue Op0 = Op.getOperand(0); 4725 SDValue Op1 = Op.getOperand(1); 4726 SDValue Op2 = Op.getOperand(2); 4727 EVT VT = Op.getValueType(); 4728 4729 // Insertions into constant indices of a v2f64 can be done using VPDI. 4730 // However, if the inserted value is a bitcast or a constant then it's 4731 // better to use GPRs, as below. 4732 if (VT == MVT::v2f64 && 4733 Op1.getOpcode() != ISD::BITCAST && 4734 Op1.getOpcode() != ISD::ConstantFP && 4735 Op2.getOpcode() == ISD::Constant) { 4736 uint64_t Index = cast<ConstantSDNode>(Op2)->getZExtValue(); 4737 unsigned Mask = VT.getVectorNumElements() - 1; 4738 if (Index <= Mask) 4739 return Op; 4740 } 4741 4742 // Otherwise bitcast to the equivalent integer form and insert via a GPR. 4743 MVT IntVT = MVT::getIntegerVT(VT.getScalarSizeInBits()); 4744 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements()); 4745 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT, 4746 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), 4747 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2); 4748 return DAG.getNode(ISD::BITCAST, DL, VT, Res); 4749 } 4750 4751 SDValue 4752 SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op, 4753 SelectionDAG &DAG) const { 4754 // Handle extractions of floating-point values. 4755 SDLoc DL(Op); 4756 SDValue Op0 = Op.getOperand(0); 4757 SDValue Op1 = Op.getOperand(1); 4758 EVT VT = Op.getValueType(); 4759 EVT VecVT = Op0.getValueType(); 4760 4761 // Extractions of constant indices can be done directly. 4762 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) { 4763 uint64_t Index = CIndexN->getZExtValue(); 4764 unsigned Mask = VecVT.getVectorNumElements() - 1; 4765 if (Index <= Mask) 4766 return Op; 4767 } 4768 4769 // Otherwise bitcast to the equivalent integer form and extract via a GPR. 4770 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits()); 4771 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements()); 4772 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT, 4773 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1); 4774 return DAG.getNode(ISD::BITCAST, DL, VT, Res); 4775 } 4776 4777 SDValue 4778 SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG, 4779 unsigned UnpackHigh) const { 4780 SDValue PackedOp = Op.getOperand(0); 4781 EVT OutVT = Op.getValueType(); 4782 EVT InVT = PackedOp.getValueType(); 4783 unsigned ToBits = OutVT.getScalarSizeInBits(); 4784 unsigned FromBits = InVT.getScalarSizeInBits(); 4785 do { 4786 FromBits *= 2; 4787 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits), 4788 SystemZ::VectorBits / FromBits); 4789 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp); 4790 } while (FromBits != ToBits); 4791 return PackedOp; 4792 } 4793 4794 SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG, 4795 unsigned ByScalar) const { 4796 // Look for cases where a vector shift can use the *_BY_SCALAR form. 4797 SDValue Op0 = Op.getOperand(0); 4798 SDValue Op1 = Op.getOperand(1); 4799 SDLoc DL(Op); 4800 EVT VT = Op.getValueType(); 4801 unsigned ElemBitSize = VT.getScalarSizeInBits(); 4802 4803 // See whether the shift vector is a splat represented as BUILD_VECTOR. 4804 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) { 4805 APInt SplatBits, SplatUndef; 4806 unsigned SplatBitSize; 4807 bool HasAnyUndefs; 4808 // Check for constant splats. Use ElemBitSize as the minimum element 4809 // width and reject splats that need wider elements. 4810 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, 4811 ElemBitSize, true) && 4812 SplatBitSize == ElemBitSize) { 4813 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff, 4814 DL, MVT::i32); 4815 return DAG.getNode(ByScalar, DL, VT, Op0, Shift); 4816 } 4817 // Check for variable splats. 4818 BitVector UndefElements; 4819 SDValue Splat = BVN->getSplatValue(&UndefElements); 4820 if (Splat) { 4821 // Since i32 is the smallest legal type, we either need a no-op 4822 // or a truncation. 4823 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat); 4824 return DAG.getNode(ByScalar, DL, VT, Op0, Shift); 4825 } 4826 } 4827 4828 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR, 4829 // and the shift amount is directly available in a GPR. 4830 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) { 4831 if (VSN->isSplat()) { 4832 SDValue VSNOp0 = VSN->getOperand(0); 4833 unsigned Index = VSN->getSplatIndex(); 4834 assert(Index < VT.getVectorNumElements() && 4835 "Splat index should be defined and in first operand"); 4836 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) || 4837 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) { 4838 // Since i32 is the smallest legal type, we either need a no-op 4839 // or a truncation. 4840 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, 4841 VSNOp0.getOperand(Index)); 4842 return DAG.getNode(ByScalar, DL, VT, Op0, Shift); 4843 } 4844 } 4845 } 4846 4847 // Otherwise just treat the current form as legal. 4848 return Op; 4849 } 4850 4851 SDValue SystemZTargetLowering::LowerOperation(SDValue Op, 4852 SelectionDAG &DAG) const { 4853 switch (Op.getOpcode()) { 4854 case ISD::FRAMEADDR: 4855 return lowerFRAMEADDR(Op, DAG); 4856 case ISD::RETURNADDR: 4857 return lowerRETURNADDR(Op, DAG); 4858 case ISD::BR_CC: 4859 return lowerBR_CC(Op, DAG); 4860 case ISD::SELECT_CC: 4861 return lowerSELECT_CC(Op, DAG); 4862 case ISD::SETCC: 4863 return lowerSETCC(Op, DAG); 4864 case ISD::GlobalAddress: 4865 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG); 4866 case ISD::GlobalTLSAddress: 4867 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG); 4868 case ISD::BlockAddress: 4869 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG); 4870 case ISD::JumpTable: 4871 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG); 4872 case ISD::ConstantPool: 4873 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG); 4874 case ISD::BITCAST: 4875 return lowerBITCAST(Op, DAG); 4876 case ISD::VASTART: 4877 return lowerVASTART(Op, DAG); 4878 case ISD::VACOPY: 4879 return lowerVACOPY(Op, DAG); 4880 case ISD::DYNAMIC_STACKALLOC: 4881 return lowerDYNAMIC_STACKALLOC(Op, DAG); 4882 case ISD::GET_DYNAMIC_AREA_OFFSET: 4883 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG); 4884 case ISD::SMUL_LOHI: 4885 return lowerSMUL_LOHI(Op, DAG); 4886 case ISD::UMUL_LOHI: 4887 return lowerUMUL_LOHI(Op, DAG); 4888 case ISD::SDIVREM: 4889 return lowerSDIVREM(Op, DAG); 4890 case ISD::UDIVREM: 4891 return lowerUDIVREM(Op, DAG); 4892 case ISD::SADDO: 4893 case ISD::SSUBO: 4894 case ISD::UADDO: 4895 case ISD::USUBO: 4896 return lowerXALUO(Op, DAG); 4897 case ISD::ADDCARRY: 4898 case ISD::SUBCARRY: 4899 return lowerADDSUBCARRY(Op, DAG); 4900 case ISD::OR: 4901 return lowerOR(Op, DAG); 4902 case ISD::CTPOP: 4903 return lowerCTPOP(Op, DAG); 4904 case ISD::ATOMIC_FENCE: 4905 return lowerATOMIC_FENCE(Op, DAG); 4906 case ISD::ATOMIC_SWAP: 4907 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW); 4908 case ISD::ATOMIC_STORE: 4909 return lowerATOMIC_STORE(Op, DAG); 4910 case ISD::ATOMIC_LOAD: 4911 return lowerATOMIC_LOAD(Op, DAG); 4912 case ISD::ATOMIC_LOAD_ADD: 4913 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD); 4914 case ISD::ATOMIC_LOAD_SUB: 4915 return lowerATOMIC_LOAD_SUB(Op, DAG); 4916 case ISD::ATOMIC_LOAD_AND: 4917 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND); 4918 case ISD::ATOMIC_LOAD_OR: 4919 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR); 4920 case ISD::ATOMIC_LOAD_XOR: 4921 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR); 4922 case ISD::ATOMIC_LOAD_NAND: 4923 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND); 4924 case ISD::ATOMIC_LOAD_MIN: 4925 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN); 4926 case ISD::ATOMIC_LOAD_MAX: 4927 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX); 4928 case ISD::ATOMIC_LOAD_UMIN: 4929 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN); 4930 case ISD::ATOMIC_LOAD_UMAX: 4931 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX); 4932 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: 4933 return lowerATOMIC_CMP_SWAP(Op, DAG); 4934 case ISD::STACKSAVE: 4935 return lowerSTACKSAVE(Op, DAG); 4936 case ISD::STACKRESTORE: 4937 return lowerSTACKRESTORE(Op, DAG); 4938 case ISD::PREFETCH: 4939 return lowerPREFETCH(Op, DAG); 4940 case ISD::INTRINSIC_W_CHAIN: 4941 return lowerINTRINSIC_W_CHAIN(Op, DAG); 4942 case ISD::INTRINSIC_WO_CHAIN: 4943 return lowerINTRINSIC_WO_CHAIN(Op, DAG); 4944 case ISD::BUILD_VECTOR: 4945 return lowerBUILD_VECTOR(Op, DAG); 4946 case ISD::VECTOR_SHUFFLE: 4947 return lowerVECTOR_SHUFFLE(Op, DAG); 4948 case ISD::SCALAR_TO_VECTOR: 4949 return lowerSCALAR_TO_VECTOR(Op, DAG); 4950 case ISD::INSERT_VECTOR_ELT: 4951 return lowerINSERT_VECTOR_ELT(Op, DAG); 4952 case ISD::EXTRACT_VECTOR_ELT: 4953 return lowerEXTRACT_VECTOR_ELT(Op, DAG); 4954 case ISD::SIGN_EXTEND_VECTOR_INREG: 4955 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH); 4956 case ISD::ZERO_EXTEND_VECTOR_INREG: 4957 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH); 4958 case ISD::SHL: 4959 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR); 4960 case ISD::SRL: 4961 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR); 4962 case ISD::SRA: 4963 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR); 4964 default: 4965 llvm_unreachable("Unexpected node to lower"); 4966 } 4967 } 4968 4969 // Lower operations with invalid operand or result types (currently used 4970 // only for 128-bit integer types). 4971 4972 static SDValue lowerI128ToGR128(SelectionDAG &DAG, SDValue In) { 4973 SDLoc DL(In); 4974 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In, 4975 DAG.getIntPtrConstant(0, DL)); 4976 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In, 4977 DAG.getIntPtrConstant(1, DL)); 4978 SDNode *Pair = DAG.getMachineNode(SystemZ::PAIR128, DL, 4979 MVT::Untyped, Hi, Lo); 4980 return SDValue(Pair, 0); 4981 } 4982 4983 static SDValue lowerGR128ToI128(SelectionDAG &DAG, SDValue In) { 4984 SDLoc DL(In); 4985 SDValue Hi = DAG.getTargetExtractSubreg(SystemZ::subreg_h64, 4986 DL, MVT::i64, In); 4987 SDValue Lo = DAG.getTargetExtractSubreg(SystemZ::subreg_l64, 4988 DL, MVT::i64, In); 4989 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i128, Lo, Hi); 4990 } 4991 4992 void 4993 SystemZTargetLowering::LowerOperationWrapper(SDNode *N, 4994 SmallVectorImpl<SDValue> &Results, 4995 SelectionDAG &DAG) const { 4996 switch (N->getOpcode()) { 4997 case ISD::ATOMIC_LOAD: { 4998 SDLoc DL(N); 4999 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other); 5000 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) }; 5001 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand(); 5002 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_LOAD_128, 5003 DL, Tys, Ops, MVT::i128, MMO); 5004 Results.push_back(lowerGR128ToI128(DAG, Res)); 5005 Results.push_back(Res.getValue(1)); 5006 break; 5007 } 5008 case ISD::ATOMIC_STORE: { 5009 SDLoc DL(N); 5010 SDVTList Tys = DAG.getVTList(MVT::Other); 5011 SDValue Ops[] = { N->getOperand(0), 5012 lowerI128ToGR128(DAG, N->getOperand(2)), 5013 N->getOperand(1) }; 5014 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand(); 5015 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_STORE_128, 5016 DL, Tys, Ops, MVT::i128, MMO); 5017 // We have to enforce sequential consistency by performing a 5018 // serialization operation after the store. 5019 if (cast<AtomicSDNode>(N)->getOrdering() == 5020 AtomicOrdering::SequentiallyConsistent) 5021 Res = SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, 5022 MVT::Other, Res), 0); 5023 Results.push_back(Res); 5024 break; 5025 } 5026 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: { 5027 SDLoc DL(N); 5028 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other); 5029 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), 5030 lowerI128ToGR128(DAG, N->getOperand(2)), 5031 lowerI128ToGR128(DAG, N->getOperand(3)) }; 5032 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand(); 5033 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP_128, 5034 DL, Tys, Ops, MVT::i128, MMO); 5035 SDValue Success = emitSETCC(DAG, DL, Res.getValue(1), 5036 SystemZ::CCMASK_CS, SystemZ::CCMASK_CS_EQ); 5037 Success = DAG.getZExtOrTrunc(Success, DL, N->getValueType(1)); 5038 Results.push_back(lowerGR128ToI128(DAG, Res)); 5039 Results.push_back(Success); 5040 Results.push_back(Res.getValue(2)); 5041 break; 5042 } 5043 default: 5044 llvm_unreachable("Unexpected node to lower"); 5045 } 5046 } 5047 5048 void 5049 SystemZTargetLowering::ReplaceNodeResults(SDNode *N, 5050 SmallVectorImpl<SDValue> &Results, 5051 SelectionDAG &DAG) const { 5052 return LowerOperationWrapper(N, Results, DAG); 5053 } 5054 5055 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const { 5056 #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME 5057 switch ((SystemZISD::NodeType)Opcode) { 5058 case SystemZISD::FIRST_NUMBER: break; 5059 OPCODE(RET_FLAG); 5060 OPCODE(CALL); 5061 OPCODE(SIBCALL); 5062 OPCODE(TLS_GDCALL); 5063 OPCODE(TLS_LDCALL); 5064 OPCODE(PCREL_WRAPPER); 5065 OPCODE(PCREL_OFFSET); 5066 OPCODE(IABS); 5067 OPCODE(ICMP); 5068 OPCODE(FCMP); 5069 OPCODE(TM); 5070 OPCODE(BR_CCMASK); 5071 OPCODE(SELECT_CCMASK); 5072 OPCODE(ADJDYNALLOC); 5073 OPCODE(POPCNT); 5074 OPCODE(SMUL_LOHI); 5075 OPCODE(UMUL_LOHI); 5076 OPCODE(SDIVREM); 5077 OPCODE(UDIVREM); 5078 OPCODE(SADDO); 5079 OPCODE(SSUBO); 5080 OPCODE(UADDO); 5081 OPCODE(USUBO); 5082 OPCODE(ADDCARRY); 5083 OPCODE(SUBCARRY); 5084 OPCODE(GET_CCMASK); 5085 OPCODE(MVC); 5086 OPCODE(MVC_LOOP); 5087 OPCODE(NC); 5088 OPCODE(NC_LOOP); 5089 OPCODE(OC); 5090 OPCODE(OC_LOOP); 5091 OPCODE(XC); 5092 OPCODE(XC_LOOP); 5093 OPCODE(CLC); 5094 OPCODE(CLC_LOOP); 5095 OPCODE(STPCPY); 5096 OPCODE(STRCMP); 5097 OPCODE(SEARCH_STRING); 5098 OPCODE(IPM); 5099 OPCODE(MEMBARRIER); 5100 OPCODE(TBEGIN); 5101 OPCODE(TBEGIN_NOFLOAT); 5102 OPCODE(TEND); 5103 OPCODE(BYTE_MASK); 5104 OPCODE(ROTATE_MASK); 5105 OPCODE(REPLICATE); 5106 OPCODE(JOIN_DWORDS); 5107 OPCODE(SPLAT); 5108 OPCODE(MERGE_HIGH); 5109 OPCODE(MERGE_LOW); 5110 OPCODE(SHL_DOUBLE); 5111 OPCODE(PERMUTE_DWORDS); 5112 OPCODE(PERMUTE); 5113 OPCODE(PACK); 5114 OPCODE(PACKS_CC); 5115 OPCODE(PACKLS_CC); 5116 OPCODE(UNPACK_HIGH); 5117 OPCODE(UNPACKL_HIGH); 5118 OPCODE(UNPACK_LOW); 5119 OPCODE(UNPACKL_LOW); 5120 OPCODE(VSHL_BY_SCALAR); 5121 OPCODE(VSRL_BY_SCALAR); 5122 OPCODE(VSRA_BY_SCALAR); 5123 OPCODE(VSUM); 5124 OPCODE(VICMPE); 5125 OPCODE(VICMPH); 5126 OPCODE(VICMPHL); 5127 OPCODE(VICMPES); 5128 OPCODE(VICMPHS); 5129 OPCODE(VICMPHLS); 5130 OPCODE(VFCMPE); 5131 OPCODE(VFCMPH); 5132 OPCODE(VFCMPHE); 5133 OPCODE(VFCMPES); 5134 OPCODE(VFCMPHS); 5135 OPCODE(VFCMPHES); 5136 OPCODE(VFTCI); 5137 OPCODE(VEXTEND); 5138 OPCODE(VROUND); 5139 OPCODE(VTM); 5140 OPCODE(VFAE_CC); 5141 OPCODE(VFAEZ_CC); 5142 OPCODE(VFEE_CC); 5143 OPCODE(VFEEZ_CC); 5144 OPCODE(VFENE_CC); 5145 OPCODE(VFENEZ_CC); 5146 OPCODE(VISTR_CC); 5147 OPCODE(VSTRC_CC); 5148 OPCODE(VSTRCZ_CC); 5149 OPCODE(TDC); 5150 OPCODE(ATOMIC_SWAPW); 5151 OPCODE(ATOMIC_LOADW_ADD); 5152 OPCODE(ATOMIC_LOADW_SUB); 5153 OPCODE(ATOMIC_LOADW_AND); 5154 OPCODE(ATOMIC_LOADW_OR); 5155 OPCODE(ATOMIC_LOADW_XOR); 5156 OPCODE(ATOMIC_LOADW_NAND); 5157 OPCODE(ATOMIC_LOADW_MIN); 5158 OPCODE(ATOMIC_LOADW_MAX); 5159 OPCODE(ATOMIC_LOADW_UMIN); 5160 OPCODE(ATOMIC_LOADW_UMAX); 5161 OPCODE(ATOMIC_CMP_SWAPW); 5162 OPCODE(ATOMIC_CMP_SWAP); 5163 OPCODE(ATOMIC_LOAD_128); 5164 OPCODE(ATOMIC_STORE_128); 5165 OPCODE(ATOMIC_CMP_SWAP_128); 5166 OPCODE(LRV); 5167 OPCODE(STRV); 5168 OPCODE(PREFETCH); 5169 } 5170 return nullptr; 5171 #undef OPCODE 5172 } 5173 5174 // Return true if VT is a vector whose elements are a whole number of bytes 5175 // in width. Also check for presence of vector support. 5176 bool SystemZTargetLowering::canTreatAsByteVector(EVT VT) const { 5177 if (!Subtarget.hasVector()) 5178 return false; 5179 5180 return VT.isVector() && VT.getScalarSizeInBits() % 8 == 0 && VT.isSimple(); 5181 } 5182 5183 // Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT 5184 // producing a result of type ResVT. Op is a possibly bitcast version 5185 // of the input vector and Index is the index (based on type VecVT) that 5186 // should be extracted. Return the new extraction if a simplification 5187 // was possible or if Force is true. 5188 SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT, 5189 EVT VecVT, SDValue Op, 5190 unsigned Index, 5191 DAGCombinerInfo &DCI, 5192 bool Force) const { 5193 SelectionDAG &DAG = DCI.DAG; 5194 5195 // The number of bytes being extracted. 5196 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize(); 5197 5198 for (;;) { 5199 unsigned Opcode = Op.getOpcode(); 5200 if (Opcode == ISD::BITCAST) 5201 // Look through bitcasts. 5202 Op = Op.getOperand(0); 5203 else if ((Opcode == ISD::VECTOR_SHUFFLE || Opcode == SystemZISD::SPLAT) && 5204 canTreatAsByteVector(Op.getValueType())) { 5205 // Get a VPERM-like permute mask and see whether the bytes covered 5206 // by the extracted element are a contiguous sequence from one 5207 // source operand. 5208 SmallVector<int, SystemZ::VectorBytes> Bytes; 5209 if (!getVPermMask(Op, Bytes)) 5210 break; 5211 int First; 5212 if (!getShuffleInput(Bytes, Index * BytesPerElement, 5213 BytesPerElement, First)) 5214 break; 5215 if (First < 0) 5216 return DAG.getUNDEF(ResVT); 5217 // Make sure the contiguous sequence starts at a multiple of the 5218 // original element size. 5219 unsigned Byte = unsigned(First) % Bytes.size(); 5220 if (Byte % BytesPerElement != 0) 5221 break; 5222 // We can get the extracted value directly from an input. 5223 Index = Byte / BytesPerElement; 5224 Op = Op.getOperand(unsigned(First) / Bytes.size()); 5225 Force = true; 5226 } else if (Opcode == ISD::BUILD_VECTOR && 5227 canTreatAsByteVector(Op.getValueType())) { 5228 // We can only optimize this case if the BUILD_VECTOR elements are 5229 // at least as wide as the extracted value. 5230 EVT OpVT = Op.getValueType(); 5231 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize(); 5232 if (OpBytesPerElement < BytesPerElement) 5233 break; 5234 // Make sure that the least-significant bit of the extracted value 5235 // is the least significant bit of an input. 5236 unsigned End = (Index + 1) * BytesPerElement; 5237 if (End % OpBytesPerElement != 0) 5238 break; 5239 // We're extracting the low part of one operand of the BUILD_VECTOR. 5240 Op = Op.getOperand(End / OpBytesPerElement - 1); 5241 if (!Op.getValueType().isInteger()) { 5242 EVT VT = MVT::getIntegerVT(Op.getValueSizeInBits()); 5243 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op); 5244 DCI.AddToWorklist(Op.getNode()); 5245 } 5246 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits()); 5247 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op); 5248 if (VT != ResVT) { 5249 DCI.AddToWorklist(Op.getNode()); 5250 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op); 5251 } 5252 return Op; 5253 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG || 5254 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG || 5255 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) && 5256 canTreatAsByteVector(Op.getValueType()) && 5257 canTreatAsByteVector(Op.getOperand(0).getValueType())) { 5258 // Make sure that only the unextended bits are significant. 5259 EVT ExtVT = Op.getValueType(); 5260 EVT OpVT = Op.getOperand(0).getValueType(); 5261 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize(); 5262 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize(); 5263 unsigned Byte = Index * BytesPerElement; 5264 unsigned SubByte = Byte % ExtBytesPerElement; 5265 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement; 5266 if (SubByte < MinSubByte || 5267 SubByte + BytesPerElement > ExtBytesPerElement) 5268 break; 5269 // Get the byte offset of the unextended element 5270 Byte = Byte / ExtBytesPerElement * OpBytesPerElement; 5271 // ...then add the byte offset relative to that element. 5272 Byte += SubByte - MinSubByte; 5273 if (Byte % BytesPerElement != 0) 5274 break; 5275 Op = Op.getOperand(0); 5276 Index = Byte / BytesPerElement; 5277 Force = true; 5278 } else 5279 break; 5280 } 5281 if (Force) { 5282 if (Op.getValueType() != VecVT) { 5283 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op); 5284 DCI.AddToWorklist(Op.getNode()); 5285 } 5286 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op, 5287 DAG.getConstant(Index, DL, MVT::i32)); 5288 } 5289 return SDValue(); 5290 } 5291 5292 // Optimize vector operations in scalar value Op on the basis that Op 5293 // is truncated to TruncVT. 5294 SDValue SystemZTargetLowering::combineTruncateExtract( 5295 const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const { 5296 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into 5297 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements 5298 // of type TruncVT. 5299 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 5300 TruncVT.getSizeInBits() % 8 == 0) { 5301 SDValue Vec = Op.getOperand(0); 5302 EVT VecVT = Vec.getValueType(); 5303 if (canTreatAsByteVector(VecVT)) { 5304 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 5305 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize(); 5306 unsigned TruncBytes = TruncVT.getStoreSize(); 5307 if (BytesPerElement % TruncBytes == 0) { 5308 // Calculate the value of Y' in the above description. We are 5309 // splitting the original elements into Scale equal-sized pieces 5310 // and for truncation purposes want the last (least-significant) 5311 // of these pieces for IndexN. This is easiest to do by calculating 5312 // the start index of the following element and then subtracting 1. 5313 unsigned Scale = BytesPerElement / TruncBytes; 5314 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1; 5315 5316 // Defer the creation of the bitcast from X to combineExtract, 5317 // which might be able to optimize the extraction. 5318 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8), 5319 VecVT.getStoreSize() / TruncBytes); 5320 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT); 5321 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true); 5322 } 5323 } 5324 } 5325 } 5326 return SDValue(); 5327 } 5328 5329 SDValue SystemZTargetLowering::combineZERO_EXTEND( 5330 SDNode *N, DAGCombinerInfo &DCI) const { 5331 // Convert (zext (select_ccmask C1, C2)) into (select_ccmask C1', C2') 5332 SelectionDAG &DAG = DCI.DAG; 5333 SDValue N0 = N->getOperand(0); 5334 EVT VT = N->getValueType(0); 5335 if (N0.getOpcode() == SystemZISD::SELECT_CCMASK) { 5336 auto *TrueOp = dyn_cast<ConstantSDNode>(N0.getOperand(0)); 5337 auto *FalseOp = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 5338 if (TrueOp && FalseOp) { 5339 SDLoc DL(N0); 5340 SDValue Ops[] = { DAG.getConstant(TrueOp->getZExtValue(), DL, VT), 5341 DAG.getConstant(FalseOp->getZExtValue(), DL, VT), 5342 N0.getOperand(2), N0.getOperand(3), N0.getOperand(4) }; 5343 SDValue NewSelect = DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VT, Ops); 5344 // If N0 has multiple uses, change other uses as well. 5345 if (!N0.hasOneUse()) { 5346 SDValue TruncSelect = 5347 DAG.getNode(ISD::TRUNCATE, DL, N0.getValueType(), NewSelect); 5348 DCI.CombineTo(N0.getNode(), TruncSelect); 5349 } 5350 return NewSelect; 5351 } 5352 } 5353 return SDValue(); 5354 } 5355 5356 SDValue SystemZTargetLowering::combineSIGN_EXTEND_INREG( 5357 SDNode *N, DAGCombinerInfo &DCI) const { 5358 // Convert (sext_in_reg (setcc LHS, RHS, COND), i1) 5359 // and (sext_in_reg (any_extend (setcc LHS, RHS, COND)), i1) 5360 // into (select_cc LHS, RHS, -1, 0, COND) 5361 SelectionDAG &DAG = DCI.DAG; 5362 SDValue N0 = N->getOperand(0); 5363 EVT VT = N->getValueType(0); 5364 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); 5365 if (N0.hasOneUse() && N0.getOpcode() == ISD::ANY_EXTEND) 5366 N0 = N0.getOperand(0); 5367 if (EVT == MVT::i1 && N0.hasOneUse() && N0.getOpcode() == ISD::SETCC) { 5368 SDLoc DL(N0); 5369 SDValue Ops[] = { N0.getOperand(0), N0.getOperand(1), 5370 DAG.getConstant(-1, DL, VT), DAG.getConstant(0, DL, VT), 5371 N0.getOperand(2) }; 5372 return DAG.getNode(ISD::SELECT_CC, DL, VT, Ops); 5373 } 5374 return SDValue(); 5375 } 5376 5377 SDValue SystemZTargetLowering::combineSIGN_EXTEND( 5378 SDNode *N, DAGCombinerInfo &DCI) const { 5379 // Convert (sext (ashr (shl X, C1), C2)) to 5380 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as 5381 // cheap as narrower ones. 5382 SelectionDAG &DAG = DCI.DAG; 5383 SDValue N0 = N->getOperand(0); 5384 EVT VT = N->getValueType(0); 5385 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) { 5386 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 5387 SDValue Inner = N0.getOperand(0); 5388 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) { 5389 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) { 5390 unsigned Extra = (VT.getSizeInBits() - N0.getValueSizeInBits()); 5391 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra; 5392 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra; 5393 EVT ShiftVT = N0.getOperand(1).getValueType(); 5394 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT, 5395 Inner.getOperand(0)); 5396 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext, 5397 DAG.getConstant(NewShlAmt, SDLoc(Inner), 5398 ShiftVT)); 5399 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, 5400 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT)); 5401 } 5402 } 5403 } 5404 return SDValue(); 5405 } 5406 5407 SDValue SystemZTargetLowering::combineMERGE( 5408 SDNode *N, DAGCombinerInfo &DCI) const { 5409 SelectionDAG &DAG = DCI.DAG; 5410 unsigned Opcode = N->getOpcode(); 5411 SDValue Op0 = N->getOperand(0); 5412 SDValue Op1 = N->getOperand(1); 5413 if (Op0.getOpcode() == ISD::BITCAST) 5414 Op0 = Op0.getOperand(0); 5415 if (ISD::isBuildVectorAllZeros(Op0.getNode())) { 5416 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF 5417 // for v4f32. 5418 if (Op1 == N->getOperand(0)) 5419 return Op1; 5420 // (z_merge_? 0, X) -> (z_unpackl_? 0, X). 5421 EVT VT = Op1.getValueType(); 5422 unsigned ElemBytes = VT.getVectorElementType().getStoreSize(); 5423 if (ElemBytes <= 4) { 5424 Opcode = (Opcode == SystemZISD::MERGE_HIGH ? 5425 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW); 5426 EVT InVT = VT.changeVectorElementTypeToInteger(); 5427 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16), 5428 SystemZ::VectorBytes / ElemBytes / 2); 5429 if (VT != InVT) { 5430 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1); 5431 DCI.AddToWorklist(Op1.getNode()); 5432 } 5433 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1); 5434 DCI.AddToWorklist(Op.getNode()); 5435 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); 5436 } 5437 } 5438 return SDValue(); 5439 } 5440 5441 SDValue SystemZTargetLowering::combineLOAD( 5442 SDNode *N, DAGCombinerInfo &DCI) const { 5443 SelectionDAG &DAG = DCI.DAG; 5444 EVT LdVT = N->getValueType(0); 5445 if (LdVT.isVector() || LdVT.isInteger()) 5446 return SDValue(); 5447 // Transform a scalar load that is REPLICATEd as well as having other 5448 // use(s) to the form where the other use(s) use the first element of the 5449 // REPLICATE instead of the load. Otherwise instruction selection will not 5450 // produce a VLREP. Avoid extracting to a GPR, so only do this for floating 5451 // point loads. 5452 5453 SDValue Replicate; 5454 SmallVector<SDNode*, 8> OtherUses; 5455 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); 5456 UI != UE; ++UI) { 5457 if (UI->getOpcode() == SystemZISD::REPLICATE) { 5458 if (Replicate) 5459 return SDValue(); // Should never happen 5460 Replicate = SDValue(*UI, 0); 5461 } 5462 else if (UI.getUse().getResNo() == 0) 5463 OtherUses.push_back(*UI); 5464 } 5465 if (!Replicate || OtherUses.empty()) 5466 return SDValue(); 5467 5468 SDLoc DL(N); 5469 SDValue Extract0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, LdVT, 5470 Replicate, DAG.getConstant(0, DL, MVT::i32)); 5471 // Update uses of the loaded Value while preserving old chains. 5472 for (SDNode *U : OtherUses) { 5473 SmallVector<SDValue, 8> Ops; 5474 for (SDValue Op : U->ops()) 5475 Ops.push_back((Op.getNode() == N && Op.getResNo() == 0) ? Extract0 : Op); 5476 DAG.UpdateNodeOperands(U, Ops); 5477 } 5478 return SDValue(N, 0); 5479 } 5480 5481 SDValue SystemZTargetLowering::combineSTORE( 5482 SDNode *N, DAGCombinerInfo &DCI) const { 5483 SelectionDAG &DAG = DCI.DAG; 5484 auto *SN = cast<StoreSDNode>(N); 5485 auto &Op1 = N->getOperand(1); 5486 EVT MemVT = SN->getMemoryVT(); 5487 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better 5488 // for the extraction to be done on a vMiN value, so that we can use VSTE. 5489 // If X has wider elements then convert it to: 5490 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z). 5491 if (MemVT.isInteger() && SN->isTruncatingStore()) { 5492 if (SDValue Value = 5493 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) { 5494 DCI.AddToWorklist(Value.getNode()); 5495 5496 // Rewrite the store with the new form of stored value. 5497 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value, 5498 SN->getBasePtr(), SN->getMemoryVT(), 5499 SN->getMemOperand()); 5500 } 5501 } 5502 // Combine STORE (BSWAP) into STRVH/STRV/STRVG 5503 if (!SN->isTruncatingStore() && 5504 Op1.getOpcode() == ISD::BSWAP && 5505 Op1.getNode()->hasOneUse() && 5506 (Op1.getValueType() == MVT::i16 || 5507 Op1.getValueType() == MVT::i32 || 5508 Op1.getValueType() == MVT::i64)) { 5509 5510 SDValue BSwapOp = Op1.getOperand(0); 5511 5512 if (BSwapOp.getValueType() == MVT::i16) 5513 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp); 5514 5515 SDValue Ops[] = { 5516 N->getOperand(0), BSwapOp, N->getOperand(2) 5517 }; 5518 5519 return 5520 DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other), 5521 Ops, MemVT, SN->getMemOperand()); 5522 } 5523 return SDValue(); 5524 } 5525 5526 SDValue SystemZTargetLowering::combineEXTRACT_VECTOR_ELT( 5527 SDNode *N, DAGCombinerInfo &DCI) const { 5528 5529 if (!Subtarget.hasVector()) 5530 return SDValue(); 5531 5532 // Try to simplify a vector extraction. 5533 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) { 5534 SDValue Op0 = N->getOperand(0); 5535 EVT VecVT = Op0.getValueType(); 5536 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0, 5537 IndexN->getZExtValue(), DCI, false); 5538 } 5539 return SDValue(); 5540 } 5541 5542 SDValue SystemZTargetLowering::combineJOIN_DWORDS( 5543 SDNode *N, DAGCombinerInfo &DCI) const { 5544 SelectionDAG &DAG = DCI.DAG; 5545 // (join_dwords X, X) == (replicate X) 5546 if (N->getOperand(0) == N->getOperand(1)) 5547 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0), 5548 N->getOperand(0)); 5549 return SDValue(); 5550 } 5551 5552 SDValue SystemZTargetLowering::combineFP_ROUND( 5553 SDNode *N, DAGCombinerInfo &DCI) const { 5554 5555 if (!Subtarget.hasVector()) 5556 return SDValue(); 5557 5558 // (fpround (extract_vector_elt X 0)) 5559 // (fpround (extract_vector_elt X 1)) -> 5560 // (extract_vector_elt (VROUND X) 0) 5561 // (extract_vector_elt (VROUND X) 2) 5562 // 5563 // This is a special case since the target doesn't really support v2f32s. 5564 SelectionDAG &DAG = DCI.DAG; 5565 SDValue Op0 = N->getOperand(0); 5566 if (N->getValueType(0) == MVT::f32 && 5567 Op0.hasOneUse() && 5568 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 5569 Op0.getOperand(0).getValueType() == MVT::v2f64 && 5570 Op0.getOperand(1).getOpcode() == ISD::Constant && 5571 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) { 5572 SDValue Vec = Op0.getOperand(0); 5573 for (auto *U : Vec->uses()) { 5574 if (U != Op0.getNode() && 5575 U->hasOneUse() && 5576 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 5577 U->getOperand(0) == Vec && 5578 U->getOperand(1).getOpcode() == ISD::Constant && 5579 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) { 5580 SDValue OtherRound = SDValue(*U->use_begin(), 0); 5581 if (OtherRound.getOpcode() == ISD::FP_ROUND && 5582 OtherRound.getOperand(0) == SDValue(U, 0) && 5583 OtherRound.getValueType() == MVT::f32) { 5584 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N), 5585 MVT::v4f32, Vec); 5586 DCI.AddToWorklist(VRound.getNode()); 5587 SDValue Extract1 = 5588 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32, 5589 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32)); 5590 DCI.AddToWorklist(Extract1.getNode()); 5591 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1); 5592 SDValue Extract0 = 5593 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32, 5594 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32)); 5595 return Extract0; 5596 } 5597 } 5598 } 5599 } 5600 return SDValue(); 5601 } 5602 5603 SDValue SystemZTargetLowering::combineFP_EXTEND( 5604 SDNode *N, DAGCombinerInfo &DCI) const { 5605 5606 if (!Subtarget.hasVector()) 5607 return SDValue(); 5608 5609 // (fpextend (extract_vector_elt X 0)) 5610 // (fpextend (extract_vector_elt X 2)) -> 5611 // (extract_vector_elt (VEXTEND X) 0) 5612 // (extract_vector_elt (VEXTEND X) 1) 5613 // 5614 // This is a special case since the target doesn't really support v2f32s. 5615 SelectionDAG &DAG = DCI.DAG; 5616 SDValue Op0 = N->getOperand(0); 5617 if (N->getValueType(0) == MVT::f64 && 5618 Op0.hasOneUse() && 5619 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 5620 Op0.getOperand(0).getValueType() == MVT::v4f32 && 5621 Op0.getOperand(1).getOpcode() == ISD::Constant && 5622 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) { 5623 SDValue Vec = Op0.getOperand(0); 5624 for (auto *U : Vec->uses()) { 5625 if (U != Op0.getNode() && 5626 U->hasOneUse() && 5627 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 5628 U->getOperand(0) == Vec && 5629 U->getOperand(1).getOpcode() == ISD::Constant && 5630 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 2) { 5631 SDValue OtherExtend = SDValue(*U->use_begin(), 0); 5632 if (OtherExtend.getOpcode() == ISD::FP_EXTEND && 5633 OtherExtend.getOperand(0) == SDValue(U, 0) && 5634 OtherExtend.getValueType() == MVT::f64) { 5635 SDValue VExtend = DAG.getNode(SystemZISD::VEXTEND, SDLoc(N), 5636 MVT::v2f64, Vec); 5637 DCI.AddToWorklist(VExtend.getNode()); 5638 SDValue Extract1 = 5639 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f64, 5640 VExtend, DAG.getConstant(1, SDLoc(U), MVT::i32)); 5641 DCI.AddToWorklist(Extract1.getNode()); 5642 DAG.ReplaceAllUsesOfValueWith(OtherExtend, Extract1); 5643 SDValue Extract0 = 5644 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f64, 5645 VExtend, DAG.getConstant(0, SDLoc(Op0), MVT::i32)); 5646 return Extract0; 5647 } 5648 } 5649 } 5650 } 5651 return SDValue(); 5652 } 5653 5654 SDValue SystemZTargetLowering::combineBSWAP( 5655 SDNode *N, DAGCombinerInfo &DCI) const { 5656 SelectionDAG &DAG = DCI.DAG; 5657 // Combine BSWAP (LOAD) into LRVH/LRV/LRVG 5658 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) && 5659 N->getOperand(0).hasOneUse() && 5660 (N->getValueType(0) == MVT::i16 || N->getValueType(0) == MVT::i32 || 5661 N->getValueType(0) == MVT::i64)) { 5662 SDValue Load = N->getOperand(0); 5663 LoadSDNode *LD = cast<LoadSDNode>(Load); 5664 5665 // Create the byte-swapping load. 5666 SDValue Ops[] = { 5667 LD->getChain(), // Chain 5668 LD->getBasePtr() // Ptr 5669 }; 5670 EVT LoadVT = N->getValueType(0); 5671 if (LoadVT == MVT::i16) 5672 LoadVT = MVT::i32; 5673 SDValue BSLoad = 5674 DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N), 5675 DAG.getVTList(LoadVT, MVT::Other), 5676 Ops, LD->getMemoryVT(), LD->getMemOperand()); 5677 5678 // If this is an i16 load, insert the truncate. 5679 SDValue ResVal = BSLoad; 5680 if (N->getValueType(0) == MVT::i16) 5681 ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad); 5682 5683 // First, combine the bswap away. This makes the value produced by the 5684 // load dead. 5685 DCI.CombineTo(N, ResVal); 5686 5687 // Next, combine the load away, we give it a bogus result value but a real 5688 // chain result. The result value is dead because the bswap is dead. 5689 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1)); 5690 5691 // Return N so it doesn't get rechecked! 5692 return SDValue(N, 0); 5693 } 5694 return SDValue(); 5695 } 5696 5697 static bool combineCCMask(SDValue &CCReg, int &CCValid, int &CCMask) { 5698 // We have a SELECT_CCMASK or BR_CCMASK comparing the condition code 5699 // set by the CCReg instruction using the CCValid / CCMask masks, 5700 // If the CCReg instruction is itself a ICMP testing the condition 5701 // code set by some other instruction, see whether we can directly 5702 // use that condition code. 5703 5704 // Verify that we have an ICMP against some constant. 5705 if (CCValid != SystemZ::CCMASK_ICMP) 5706 return false; 5707 auto *ICmp = CCReg.getNode(); 5708 if (ICmp->getOpcode() != SystemZISD::ICMP) 5709 return false; 5710 auto *CompareLHS = ICmp->getOperand(0).getNode(); 5711 auto *CompareRHS = dyn_cast<ConstantSDNode>(ICmp->getOperand(1)); 5712 if (!CompareRHS) 5713 return false; 5714 5715 // Optimize the case where CompareLHS is a SELECT_CCMASK. 5716 if (CompareLHS->getOpcode() == SystemZISD::SELECT_CCMASK) { 5717 // Verify that we have an appropriate mask for a EQ or NE comparison. 5718 bool Invert = false; 5719 if (CCMask == SystemZ::CCMASK_CMP_NE) 5720 Invert = !Invert; 5721 else if (CCMask != SystemZ::CCMASK_CMP_EQ) 5722 return false; 5723 5724 // Verify that the ICMP compares against one of select values. 5725 auto *TrueVal = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(0)); 5726 if (!TrueVal) 5727 return false; 5728 auto *FalseVal = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(1)); 5729 if (!FalseVal) 5730 return false; 5731 if (CompareRHS->getZExtValue() == FalseVal->getZExtValue()) 5732 Invert = !Invert; 5733 else if (CompareRHS->getZExtValue() != TrueVal->getZExtValue()) 5734 return false; 5735 5736 // Compute the effective CC mask for the new branch or select. 5737 auto *NewCCValid = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(2)); 5738 auto *NewCCMask = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(3)); 5739 if (!NewCCValid || !NewCCMask) 5740 return false; 5741 CCValid = NewCCValid->getZExtValue(); 5742 CCMask = NewCCMask->getZExtValue(); 5743 if (Invert) 5744 CCMask ^= CCValid; 5745 5746 // Return the updated CCReg link. 5747 CCReg = CompareLHS->getOperand(4); 5748 return true; 5749 } 5750 5751 // Optimize the case where CompareRHS is (SRA (SHL (IPM))). 5752 if (CompareLHS->getOpcode() == ISD::SRA) { 5753 auto *SRACount = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(1)); 5754 if (!SRACount || SRACount->getZExtValue() != 30) 5755 return false; 5756 auto *SHL = CompareLHS->getOperand(0).getNode(); 5757 if (SHL->getOpcode() != ISD::SHL) 5758 return false; 5759 auto *SHLCount = dyn_cast<ConstantSDNode>(SHL->getOperand(1)); 5760 if (!SHLCount || SHLCount->getZExtValue() != 30 - SystemZ::IPM_CC) 5761 return false; 5762 auto *IPM = SHL->getOperand(0).getNode(); 5763 if (IPM->getOpcode() != SystemZISD::IPM) 5764 return false; 5765 5766 // Avoid introducing CC spills (because SRA would clobber CC). 5767 if (!CompareLHS->hasOneUse()) 5768 return false; 5769 // Verify that the ICMP compares against zero. 5770 if (CompareRHS->getZExtValue() != 0) 5771 return false; 5772 5773 // Compute the effective CC mask for the new branch or select. 5774 switch (CCMask) { 5775 case SystemZ::CCMASK_CMP_EQ: break; 5776 case SystemZ::CCMASK_CMP_NE: break; 5777 case SystemZ::CCMASK_CMP_LT: CCMask = SystemZ::CCMASK_CMP_GT; break; 5778 case SystemZ::CCMASK_CMP_GT: CCMask = SystemZ::CCMASK_CMP_LT; break; 5779 case SystemZ::CCMASK_CMP_LE: CCMask = SystemZ::CCMASK_CMP_GE; break; 5780 case SystemZ::CCMASK_CMP_GE: CCMask = SystemZ::CCMASK_CMP_LE; break; 5781 default: return false; 5782 } 5783 5784 // Return the updated CCReg link. 5785 CCReg = IPM->getOperand(0); 5786 return true; 5787 } 5788 5789 return false; 5790 } 5791 5792 SDValue SystemZTargetLowering::combineBR_CCMASK( 5793 SDNode *N, DAGCombinerInfo &DCI) const { 5794 SelectionDAG &DAG = DCI.DAG; 5795 5796 // Combine BR_CCMASK (ICMP (SELECT_CCMASK)) into a single BR_CCMASK. 5797 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(1)); 5798 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(2)); 5799 if (!CCValid || !CCMask) 5800 return SDValue(); 5801 5802 int CCValidVal = CCValid->getZExtValue(); 5803 int CCMaskVal = CCMask->getZExtValue(); 5804 SDValue Chain = N->getOperand(0); 5805 SDValue CCReg = N->getOperand(4); 5806 5807 if (combineCCMask(CCReg, CCValidVal, CCMaskVal)) 5808 return DAG.getNode(SystemZISD::BR_CCMASK, SDLoc(N), N->getValueType(0), 5809 Chain, 5810 DAG.getConstant(CCValidVal, SDLoc(N), MVT::i32), 5811 DAG.getConstant(CCMaskVal, SDLoc(N), MVT::i32), 5812 N->getOperand(3), CCReg); 5813 return SDValue(); 5814 } 5815 5816 SDValue SystemZTargetLowering::combineSELECT_CCMASK( 5817 SDNode *N, DAGCombinerInfo &DCI) const { 5818 SelectionDAG &DAG = DCI.DAG; 5819 5820 // Combine SELECT_CCMASK (ICMP (SELECT_CCMASK)) into a single SELECT_CCMASK. 5821 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(2)); 5822 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(3)); 5823 if (!CCValid || !CCMask) 5824 return SDValue(); 5825 5826 int CCValidVal = CCValid->getZExtValue(); 5827 int CCMaskVal = CCMask->getZExtValue(); 5828 SDValue CCReg = N->getOperand(4); 5829 5830 if (combineCCMask(CCReg, CCValidVal, CCMaskVal)) 5831 return DAG.getNode(SystemZISD::SELECT_CCMASK, SDLoc(N), N->getValueType(0), 5832 N->getOperand(0), 5833 N->getOperand(1), 5834 DAG.getConstant(CCValidVal, SDLoc(N), MVT::i32), 5835 DAG.getConstant(CCMaskVal, SDLoc(N), MVT::i32), 5836 CCReg); 5837 return SDValue(); 5838 } 5839 5840 5841 SDValue SystemZTargetLowering::combineGET_CCMASK( 5842 SDNode *N, DAGCombinerInfo &DCI) const { 5843 5844 // Optimize away GET_CCMASK (SELECT_CCMASK) if the CC masks are compatible 5845 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(1)); 5846 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(2)); 5847 if (!CCValid || !CCMask) 5848 return SDValue(); 5849 int CCValidVal = CCValid->getZExtValue(); 5850 int CCMaskVal = CCMask->getZExtValue(); 5851 5852 SDValue Select = N->getOperand(0); 5853 if (Select->getOpcode() != SystemZISD::SELECT_CCMASK) 5854 return SDValue(); 5855 5856 auto *SelectCCValid = dyn_cast<ConstantSDNode>(Select->getOperand(2)); 5857 auto *SelectCCMask = dyn_cast<ConstantSDNode>(Select->getOperand(3)); 5858 if (!SelectCCValid || !SelectCCMask) 5859 return SDValue(); 5860 int SelectCCValidVal = SelectCCValid->getZExtValue(); 5861 int SelectCCMaskVal = SelectCCMask->getZExtValue(); 5862 5863 auto *TrueVal = dyn_cast<ConstantSDNode>(Select->getOperand(0)); 5864 auto *FalseVal = dyn_cast<ConstantSDNode>(Select->getOperand(1)); 5865 if (!TrueVal || !FalseVal) 5866 return SDValue(); 5867 if (TrueVal->getZExtValue() != 0 && FalseVal->getZExtValue() == 0) 5868 ; 5869 else if (TrueVal->getZExtValue() == 0 && FalseVal->getZExtValue() != 0) 5870 SelectCCMaskVal ^= SelectCCValidVal; 5871 else 5872 return SDValue(); 5873 5874 if (SelectCCValidVal & ~CCValidVal) 5875 return SDValue(); 5876 if (SelectCCMaskVal != (CCMaskVal & SelectCCValidVal)) 5877 return SDValue(); 5878 5879 return Select->getOperand(4); 5880 } 5881 5882 SDValue SystemZTargetLowering::combineIntDIVREM( 5883 SDNode *N, DAGCombinerInfo &DCI) const { 5884 SelectionDAG &DAG = DCI.DAG; 5885 EVT VT = N->getValueType(0); 5886 // In the case where the divisor is a vector of constants a cheaper 5887 // sequence of instructions can replace the divide. BuildSDIV is called to 5888 // do this during DAG combining, but it only succeeds when it can build a 5889 // multiplication node. The only option for SystemZ is ISD::SMUL_LOHI, and 5890 // since it is not Legal but Custom it can only happen before 5891 // legalization. Therefore we must scalarize this early before Combine 5892 // 1. For widened vectors, this is already the result of type legalization. 5893 if (DCI.Level == BeforeLegalizeTypes && VT.isVector() && isTypeLegal(VT) && 5894 DAG.isConstantIntBuildVectorOrConstantInt(N->getOperand(1))) 5895 return DAG.UnrollVectorOp(N); 5896 return SDValue(); 5897 } 5898 5899 SDValue SystemZTargetLowering::unwrapAddress(SDValue N) const { 5900 if (N->getOpcode() == SystemZISD::PCREL_WRAPPER) 5901 return N->getOperand(0); 5902 return N; 5903 } 5904 5905 SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N, 5906 DAGCombinerInfo &DCI) const { 5907 switch(N->getOpcode()) { 5908 default: break; 5909 case ISD::ZERO_EXTEND: return combineZERO_EXTEND(N, DCI); 5910 case ISD::SIGN_EXTEND: return combineSIGN_EXTEND(N, DCI); 5911 case ISD::SIGN_EXTEND_INREG: return combineSIGN_EXTEND_INREG(N, DCI); 5912 case SystemZISD::MERGE_HIGH: 5913 case SystemZISD::MERGE_LOW: return combineMERGE(N, DCI); 5914 case ISD::LOAD: return combineLOAD(N, DCI); 5915 case ISD::STORE: return combineSTORE(N, DCI); 5916 case ISD::EXTRACT_VECTOR_ELT: return combineEXTRACT_VECTOR_ELT(N, DCI); 5917 case SystemZISD::JOIN_DWORDS: return combineJOIN_DWORDS(N, DCI); 5918 case ISD::FP_ROUND: return combineFP_ROUND(N, DCI); 5919 case ISD::FP_EXTEND: return combineFP_EXTEND(N, DCI); 5920 case ISD::BSWAP: return combineBSWAP(N, DCI); 5921 case SystemZISD::BR_CCMASK: return combineBR_CCMASK(N, DCI); 5922 case SystemZISD::SELECT_CCMASK: return combineSELECT_CCMASK(N, DCI); 5923 case SystemZISD::GET_CCMASK: return combineGET_CCMASK(N, DCI); 5924 case ISD::SDIV: 5925 case ISD::UDIV: 5926 case ISD::SREM: 5927 case ISD::UREM: return combineIntDIVREM(N, DCI); 5928 } 5929 5930 return SDValue(); 5931 } 5932 5933 // Return the demanded elements for the OpNo source operand of Op. DemandedElts 5934 // are for Op. 5935 static APInt getDemandedSrcElements(SDValue Op, const APInt &DemandedElts, 5936 unsigned OpNo) { 5937 EVT VT = Op.getValueType(); 5938 unsigned NumElts = (VT.isVector() ? VT.getVectorNumElements() : 1); 5939 APInt SrcDemE; 5940 unsigned Opcode = Op.getOpcode(); 5941 if (Opcode == ISD::INTRINSIC_WO_CHAIN) { 5942 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 5943 switch (Id) { 5944 case Intrinsic::s390_vpksh: // PACKS 5945 case Intrinsic::s390_vpksf: 5946 case Intrinsic::s390_vpksg: 5947 case Intrinsic::s390_vpkshs: // PACKS_CC 5948 case Intrinsic::s390_vpksfs: 5949 case Intrinsic::s390_vpksgs: 5950 case Intrinsic::s390_vpklsh: // PACKLS 5951 case Intrinsic::s390_vpklsf: 5952 case Intrinsic::s390_vpklsg: 5953 case Intrinsic::s390_vpklshs: // PACKLS_CC 5954 case Intrinsic::s390_vpklsfs: 5955 case Intrinsic::s390_vpklsgs: 5956 // VECTOR PACK truncates the elements of two source vectors into one. 5957 SrcDemE = DemandedElts; 5958 if (OpNo == 2) 5959 SrcDemE.lshrInPlace(NumElts / 2); 5960 SrcDemE = SrcDemE.trunc(NumElts / 2); 5961 break; 5962 // VECTOR UNPACK extends half the elements of the source vector. 5963 case Intrinsic::s390_vuphb: // VECTOR UNPACK HIGH 5964 case Intrinsic::s390_vuphh: 5965 case Intrinsic::s390_vuphf: 5966 case Intrinsic::s390_vuplhb: // VECTOR UNPACK LOGICAL HIGH 5967 case Intrinsic::s390_vuplhh: 5968 case Intrinsic::s390_vuplhf: 5969 SrcDemE = APInt(NumElts * 2, 0); 5970 SrcDemE.insertBits(DemandedElts, 0); 5971 break; 5972 case Intrinsic::s390_vuplb: // VECTOR UNPACK LOW 5973 case Intrinsic::s390_vuplhw: 5974 case Intrinsic::s390_vuplf: 5975 case Intrinsic::s390_vupllb: // VECTOR UNPACK LOGICAL LOW 5976 case Intrinsic::s390_vupllh: 5977 case Intrinsic::s390_vupllf: 5978 SrcDemE = APInt(NumElts * 2, 0); 5979 SrcDemE.insertBits(DemandedElts, NumElts); 5980 break; 5981 case Intrinsic::s390_vpdi: { 5982 // VECTOR PERMUTE DWORD IMMEDIATE selects one element from each source. 5983 SrcDemE = APInt(NumElts, 0); 5984 if (!DemandedElts[OpNo - 1]) 5985 break; 5986 unsigned Mask = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue(); 5987 unsigned MaskBit = ((OpNo - 1) ? 1 : 4); 5988 // Demand input element 0 or 1, given by the mask bit value. 5989 SrcDemE.setBit((Mask & MaskBit)? 1 : 0); 5990 break; 5991 } 5992 case Intrinsic::s390_vsldb: { 5993 // VECTOR SHIFT LEFT DOUBLE BY BYTE 5994 assert(VT == MVT::v16i8 && "Unexpected type."); 5995 unsigned FirstIdx = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue(); 5996 assert (FirstIdx > 0 && FirstIdx < 16 && "Unused operand."); 5997 unsigned NumSrc0Els = 16 - FirstIdx; 5998 SrcDemE = APInt(NumElts, 0); 5999 if (OpNo == 1) { 6000 APInt DemEls = DemandedElts.trunc(NumSrc0Els); 6001 SrcDemE.insertBits(DemEls, FirstIdx); 6002 } else { 6003 APInt DemEls = DemandedElts.lshr(NumSrc0Els); 6004 SrcDemE.insertBits(DemEls, 0); 6005 } 6006 break; 6007 } 6008 case Intrinsic::s390_vperm: 6009 SrcDemE = APInt(NumElts, 1); 6010 break; 6011 default: 6012 llvm_unreachable("Unhandled intrinsic."); 6013 break; 6014 } 6015 } else { 6016 switch (Opcode) { 6017 case SystemZISD::JOIN_DWORDS: 6018 // Scalar operand. 6019 SrcDemE = APInt(1, 1); 6020 break; 6021 case SystemZISD::SELECT_CCMASK: 6022 SrcDemE = DemandedElts; 6023 break; 6024 default: 6025 llvm_unreachable("Unhandled opcode."); 6026 break; 6027 } 6028 } 6029 return SrcDemE; 6030 } 6031 6032 static void computeKnownBitsBinOp(const SDValue Op, KnownBits &Known, 6033 const APInt &DemandedElts, 6034 const SelectionDAG &DAG, unsigned Depth, 6035 unsigned OpNo) { 6036 APInt Src0DemE = getDemandedSrcElements(Op, DemandedElts, OpNo); 6037 APInt Src1DemE = getDemandedSrcElements(Op, DemandedElts, OpNo + 1); 6038 KnownBits LHSKnown = 6039 DAG.computeKnownBits(Op.getOperand(OpNo), Src0DemE, Depth + 1); 6040 KnownBits RHSKnown = 6041 DAG.computeKnownBits(Op.getOperand(OpNo + 1), Src1DemE, Depth + 1); 6042 Known.Zero = LHSKnown.Zero & RHSKnown.Zero; 6043 Known.One = LHSKnown.One & RHSKnown.One; 6044 } 6045 6046 void 6047 SystemZTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, 6048 KnownBits &Known, 6049 const APInt &DemandedElts, 6050 const SelectionDAG &DAG, 6051 unsigned Depth) const { 6052 Known.resetAll(); 6053 6054 // Intrinsic CC result is returned in the two low bits. 6055 unsigned tmp0, tmp1; // not used 6056 if (Op.getResNo() == 1 && isIntrinsicWithCC(Op, tmp0, tmp1)) { 6057 Known.Zero.setBitsFrom(2); 6058 return; 6059 } 6060 EVT VT = Op.getValueType(); 6061 if (Op.getResNo() != 0 || VT == MVT::Untyped) 6062 return; 6063 assert (Known.getBitWidth() == VT.getScalarSizeInBits() && 6064 "KnownBits does not match VT in bitwidth"); 6065 assert ((!VT.isVector() || 6066 (DemandedElts.getBitWidth() == VT.getVectorNumElements())) && 6067 "DemandedElts does not match VT number of elements"); 6068 unsigned BitWidth = Known.getBitWidth(); 6069 unsigned Opcode = Op.getOpcode(); 6070 if (Opcode == ISD::INTRINSIC_WO_CHAIN) { 6071 bool IsLogical = false; 6072 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 6073 switch (Id) { 6074 case Intrinsic::s390_vpksh: // PACKS 6075 case Intrinsic::s390_vpksf: 6076 case Intrinsic::s390_vpksg: 6077 case Intrinsic::s390_vpkshs: // PACKS_CC 6078 case Intrinsic::s390_vpksfs: 6079 case Intrinsic::s390_vpksgs: 6080 case Intrinsic::s390_vpklsh: // PACKLS 6081 case Intrinsic::s390_vpklsf: 6082 case Intrinsic::s390_vpklsg: 6083 case Intrinsic::s390_vpklshs: // PACKLS_CC 6084 case Intrinsic::s390_vpklsfs: 6085 case Intrinsic::s390_vpklsgs: 6086 case Intrinsic::s390_vpdi: 6087 case Intrinsic::s390_vsldb: 6088 case Intrinsic::s390_vperm: 6089 computeKnownBitsBinOp(Op, Known, DemandedElts, DAG, Depth, 1); 6090 break; 6091 case Intrinsic::s390_vuplhb: // VECTOR UNPACK LOGICAL HIGH 6092 case Intrinsic::s390_vuplhh: 6093 case Intrinsic::s390_vuplhf: 6094 case Intrinsic::s390_vupllb: // VECTOR UNPACK LOGICAL LOW 6095 case Intrinsic::s390_vupllh: 6096 case Intrinsic::s390_vupllf: 6097 IsLogical = true; 6098 LLVM_FALLTHROUGH; 6099 case Intrinsic::s390_vuphb: // VECTOR UNPACK HIGH 6100 case Intrinsic::s390_vuphh: 6101 case Intrinsic::s390_vuphf: 6102 case Intrinsic::s390_vuplb: // VECTOR UNPACK LOW 6103 case Intrinsic::s390_vuplhw: 6104 case Intrinsic::s390_vuplf: { 6105 SDValue SrcOp = Op.getOperand(1); 6106 APInt SrcDemE = getDemandedSrcElements(Op, DemandedElts, 0); 6107 Known = DAG.computeKnownBits(SrcOp, SrcDemE, Depth + 1); 6108 if (IsLogical) { 6109 Known = Known.zext(BitWidth, true); 6110 } else 6111 Known = Known.sext(BitWidth); 6112 break; 6113 } 6114 default: 6115 break; 6116 } 6117 } else { 6118 switch (Opcode) { 6119 case SystemZISD::JOIN_DWORDS: 6120 case SystemZISD::SELECT_CCMASK: 6121 computeKnownBitsBinOp(Op, Known, DemandedElts, DAG, Depth, 0); 6122 break; 6123 case SystemZISD::REPLICATE: { 6124 SDValue SrcOp = Op.getOperand(0); 6125 Known = DAG.computeKnownBits(SrcOp, Depth + 1); 6126 if (Known.getBitWidth() < BitWidth && isa<ConstantSDNode>(SrcOp)) 6127 Known = Known.sext(BitWidth); // VREPI sign extends the immedate. 6128 break; 6129 } 6130 default: 6131 break; 6132 } 6133 } 6134 6135 // Known has the width of the source operand(s). Adjust if needed to match 6136 // the passed bitwidth. 6137 if (Known.getBitWidth() != BitWidth) 6138 Known = Known.zextOrTrunc(BitWidth, false); 6139 } 6140 6141 static unsigned computeNumSignBitsBinOp(SDValue Op, const APInt &DemandedElts, 6142 const SelectionDAG &DAG, unsigned Depth, 6143 unsigned OpNo) { 6144 APInt Src0DemE = getDemandedSrcElements(Op, DemandedElts, OpNo); 6145 unsigned LHS = DAG.ComputeNumSignBits(Op.getOperand(OpNo), Src0DemE, Depth + 1); 6146 if (LHS == 1) return 1; // Early out. 6147 APInt Src1DemE = getDemandedSrcElements(Op, DemandedElts, OpNo + 1); 6148 unsigned RHS = DAG.ComputeNumSignBits(Op.getOperand(OpNo + 1), Src1DemE, Depth + 1); 6149 if (RHS == 1) return 1; // Early out. 6150 unsigned Common = std::min(LHS, RHS); 6151 unsigned SrcBitWidth = Op.getOperand(OpNo).getScalarValueSizeInBits(); 6152 EVT VT = Op.getValueType(); 6153 unsigned VTBits = VT.getScalarSizeInBits(); 6154 if (SrcBitWidth > VTBits) { // PACK 6155 unsigned SrcExtraBits = SrcBitWidth - VTBits; 6156 if (Common > SrcExtraBits) 6157 return (Common - SrcExtraBits); 6158 return 1; 6159 } 6160 assert (SrcBitWidth == VTBits && "Expected operands of same bitwidth."); 6161 return Common; 6162 } 6163 6164 unsigned 6165 SystemZTargetLowering::ComputeNumSignBitsForTargetNode( 6166 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, 6167 unsigned Depth) const { 6168 if (Op.getResNo() != 0) 6169 return 1; 6170 unsigned Opcode = Op.getOpcode(); 6171 if (Opcode == ISD::INTRINSIC_WO_CHAIN) { 6172 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 6173 switch (Id) { 6174 case Intrinsic::s390_vpksh: // PACKS 6175 case Intrinsic::s390_vpksf: 6176 case Intrinsic::s390_vpksg: 6177 case Intrinsic::s390_vpkshs: // PACKS_CC 6178 case Intrinsic::s390_vpksfs: 6179 case Intrinsic::s390_vpksgs: 6180 case Intrinsic::s390_vpklsh: // PACKLS 6181 case Intrinsic::s390_vpklsf: 6182 case Intrinsic::s390_vpklsg: 6183 case Intrinsic::s390_vpklshs: // PACKLS_CC 6184 case Intrinsic::s390_vpklsfs: 6185 case Intrinsic::s390_vpklsgs: 6186 case Intrinsic::s390_vpdi: 6187 case Intrinsic::s390_vsldb: 6188 case Intrinsic::s390_vperm: 6189 return computeNumSignBitsBinOp(Op, DemandedElts, DAG, Depth, 1); 6190 case Intrinsic::s390_vuphb: // VECTOR UNPACK HIGH 6191 case Intrinsic::s390_vuphh: 6192 case Intrinsic::s390_vuphf: 6193 case Intrinsic::s390_vuplb: // VECTOR UNPACK LOW 6194 case Intrinsic::s390_vuplhw: 6195 case Intrinsic::s390_vuplf: { 6196 SDValue PackedOp = Op.getOperand(1); 6197 APInt SrcDemE = getDemandedSrcElements(Op, DemandedElts, 1); 6198 unsigned Tmp = DAG.ComputeNumSignBits(PackedOp, SrcDemE, Depth + 1); 6199 EVT VT = Op.getValueType(); 6200 unsigned VTBits = VT.getScalarSizeInBits(); 6201 Tmp += VTBits - PackedOp.getScalarValueSizeInBits(); 6202 return Tmp; 6203 } 6204 default: 6205 break; 6206 } 6207 } else { 6208 switch (Opcode) { 6209 case SystemZISD::SELECT_CCMASK: 6210 return computeNumSignBitsBinOp(Op, DemandedElts, DAG, Depth, 0); 6211 default: 6212 break; 6213 } 6214 } 6215 6216 return 1; 6217 } 6218 6219 //===----------------------------------------------------------------------===// 6220 // Custom insertion 6221 //===----------------------------------------------------------------------===// 6222 6223 // Create a new basic block after MBB. 6224 static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) { 6225 MachineFunction &MF = *MBB->getParent(); 6226 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock()); 6227 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB); 6228 return NewMBB; 6229 } 6230 6231 // Split MBB after MI and return the new block (the one that contains 6232 // instructions after MI). 6233 static MachineBasicBlock *splitBlockAfter(MachineBasicBlock::iterator MI, 6234 MachineBasicBlock *MBB) { 6235 MachineBasicBlock *NewMBB = emitBlockAfter(MBB); 6236 NewMBB->splice(NewMBB->begin(), MBB, 6237 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 6238 NewMBB->transferSuccessorsAndUpdatePHIs(MBB); 6239 return NewMBB; 6240 } 6241 6242 // Split MBB before MI and return the new block (the one that contains MI). 6243 static MachineBasicBlock *splitBlockBefore(MachineBasicBlock::iterator MI, 6244 MachineBasicBlock *MBB) { 6245 MachineBasicBlock *NewMBB = emitBlockAfter(MBB); 6246 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end()); 6247 NewMBB->transferSuccessorsAndUpdatePHIs(MBB); 6248 return NewMBB; 6249 } 6250 6251 // Force base value Base into a register before MI. Return the register. 6252 static Register forceReg(MachineInstr &MI, MachineOperand &Base, 6253 const SystemZInstrInfo *TII) { 6254 if (Base.isReg()) 6255 return Base.getReg(); 6256 6257 MachineBasicBlock *MBB = MI.getParent(); 6258 MachineFunction &MF = *MBB->getParent(); 6259 MachineRegisterInfo &MRI = MF.getRegInfo(); 6260 6261 Register Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 6262 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LA), Reg) 6263 .add(Base) 6264 .addImm(0) 6265 .addReg(0); 6266 return Reg; 6267 } 6268 6269 // The CC operand of MI might be missing a kill marker because there 6270 // were multiple uses of CC, and ISel didn't know which to mark. 6271 // Figure out whether MI should have had a kill marker. 6272 static bool checkCCKill(MachineInstr &MI, MachineBasicBlock *MBB) { 6273 // Scan forward through BB for a use/def of CC. 6274 MachineBasicBlock::iterator miI(std::next(MachineBasicBlock::iterator(MI))); 6275 for (MachineBasicBlock::iterator miE = MBB->end(); miI != miE; ++miI) { 6276 const MachineInstr& mi = *miI; 6277 if (mi.readsRegister(SystemZ::CC)) 6278 return false; 6279 if (mi.definesRegister(SystemZ::CC)) 6280 break; // Should have kill-flag - update below. 6281 } 6282 6283 // If we hit the end of the block, check whether CC is live into a 6284 // successor. 6285 if (miI == MBB->end()) { 6286 for (auto SI = MBB->succ_begin(), SE = MBB->succ_end(); SI != SE; ++SI) 6287 if ((*SI)->isLiveIn(SystemZ::CC)) 6288 return false; 6289 } 6290 6291 return true; 6292 } 6293 6294 // Return true if it is OK for this Select pseudo-opcode to be cascaded 6295 // together with other Select pseudo-opcodes into a single basic-block with 6296 // a conditional jump around it. 6297 static bool isSelectPseudo(MachineInstr &MI) { 6298 switch (MI.getOpcode()) { 6299 case SystemZ::Select32: 6300 case SystemZ::Select64: 6301 case SystemZ::SelectF32: 6302 case SystemZ::SelectF64: 6303 case SystemZ::SelectF128: 6304 case SystemZ::SelectVR32: 6305 case SystemZ::SelectVR64: 6306 case SystemZ::SelectVR128: 6307 return true; 6308 6309 default: 6310 return false; 6311 } 6312 } 6313 6314 // Helper function, which inserts PHI functions into SinkMBB: 6315 // %Result(i) = phi [ %FalseValue(i), FalseMBB ], [ %TrueValue(i), TrueMBB ], 6316 // where %FalseValue(i) and %TrueValue(i) are taken from the consequent Selects 6317 // in [MIItBegin, MIItEnd) range. 6318 static void createPHIsForSelects(MachineBasicBlock::iterator MIItBegin, 6319 MachineBasicBlock::iterator MIItEnd, 6320 MachineBasicBlock *TrueMBB, 6321 MachineBasicBlock *FalseMBB, 6322 MachineBasicBlock *SinkMBB) { 6323 MachineFunction *MF = TrueMBB->getParent(); 6324 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 6325 6326 unsigned CCValid = MIItBegin->getOperand(3).getImm(); 6327 unsigned CCMask = MIItBegin->getOperand(4).getImm(); 6328 DebugLoc DL = MIItBegin->getDebugLoc(); 6329 6330 MachineBasicBlock::iterator SinkInsertionPoint = SinkMBB->begin(); 6331 6332 // As we are creating the PHIs, we have to be careful if there is more than 6333 // one. Later Selects may reference the results of earlier Selects, but later 6334 // PHIs have to reference the individual true/false inputs from earlier PHIs. 6335 // That also means that PHI construction must work forward from earlier to 6336 // later, and that the code must maintain a mapping from earlier PHI's 6337 // destination registers, and the registers that went into the PHI. 6338 DenseMap<unsigned, std::pair<unsigned, unsigned>> RegRewriteTable; 6339 6340 for (MachineBasicBlock::iterator MIIt = MIItBegin; MIIt != MIItEnd; 6341 MIIt = skipDebugInstructionsForward(++MIIt, MIItEnd)) { 6342 unsigned DestReg = MIIt->getOperand(0).getReg(); 6343 unsigned TrueReg = MIIt->getOperand(1).getReg(); 6344 unsigned FalseReg = MIIt->getOperand(2).getReg(); 6345 6346 // If this Select we are generating is the opposite condition from 6347 // the jump we generated, then we have to swap the operands for the 6348 // PHI that is going to be generated. 6349 if (MIIt->getOperand(4).getImm() == (CCValid ^ CCMask)) 6350 std::swap(TrueReg, FalseReg); 6351 6352 if (RegRewriteTable.find(TrueReg) != RegRewriteTable.end()) 6353 TrueReg = RegRewriteTable[TrueReg].first; 6354 6355 if (RegRewriteTable.find(FalseReg) != RegRewriteTable.end()) 6356 FalseReg = RegRewriteTable[FalseReg].second; 6357 6358 BuildMI(*SinkMBB, SinkInsertionPoint, DL, TII->get(SystemZ::PHI), DestReg) 6359 .addReg(TrueReg).addMBB(TrueMBB) 6360 .addReg(FalseReg).addMBB(FalseMBB); 6361 6362 // Add this PHI to the rewrite table. 6363 RegRewriteTable[DestReg] = std::make_pair(TrueReg, FalseReg); 6364 } 6365 6366 MF->getProperties().reset(MachineFunctionProperties::Property::NoPHIs); 6367 } 6368 6369 // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI. 6370 MachineBasicBlock * 6371 SystemZTargetLowering::emitSelect(MachineInstr &MI, 6372 MachineBasicBlock *MBB) const { 6373 const SystemZInstrInfo *TII = 6374 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 6375 6376 unsigned CCValid = MI.getOperand(3).getImm(); 6377 unsigned CCMask = MI.getOperand(4).getImm(); 6378 DebugLoc DL = MI.getDebugLoc(); 6379 6380 // If we have a sequence of Select* pseudo instructions using the 6381 // same condition code value, we want to expand all of them into 6382 // a single pair of basic blocks using the same condition. 6383 MachineInstr *LastMI = &MI; 6384 MachineBasicBlock::iterator NextMIIt = skipDebugInstructionsForward( 6385 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 6386 6387 if (isSelectPseudo(MI)) 6388 while (NextMIIt != MBB->end() && isSelectPseudo(*NextMIIt) && 6389 NextMIIt->getOperand(3).getImm() == CCValid && 6390 (NextMIIt->getOperand(4).getImm() == CCMask || 6391 NextMIIt->getOperand(4).getImm() == (CCValid ^ CCMask))) { 6392 LastMI = &*NextMIIt; 6393 NextMIIt = skipDebugInstructionsForward(++NextMIIt, MBB->end()); 6394 } 6395 6396 MachineBasicBlock *StartMBB = MBB; 6397 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB); 6398 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB); 6399 6400 // Unless CC was killed in the last Select instruction, mark it as 6401 // live-in to both FalseMBB and JoinMBB. 6402 if (!LastMI->killsRegister(SystemZ::CC) && !checkCCKill(*LastMI, JoinMBB)) { 6403 FalseMBB->addLiveIn(SystemZ::CC); 6404 JoinMBB->addLiveIn(SystemZ::CC); 6405 } 6406 6407 // StartMBB: 6408 // BRC CCMask, JoinMBB 6409 // # fallthrough to FalseMBB 6410 MBB = StartMBB; 6411 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 6412 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); 6413 MBB->addSuccessor(JoinMBB); 6414 MBB->addSuccessor(FalseMBB); 6415 6416 // FalseMBB: 6417 // # fallthrough to JoinMBB 6418 MBB = FalseMBB; 6419 MBB->addSuccessor(JoinMBB); 6420 6421 // JoinMBB: 6422 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ] 6423 // ... 6424 MBB = JoinMBB; 6425 MachineBasicBlock::iterator MIItBegin = MachineBasicBlock::iterator(MI); 6426 MachineBasicBlock::iterator MIItEnd = skipDebugInstructionsForward( 6427 std::next(MachineBasicBlock::iterator(LastMI)), MBB->end()); 6428 createPHIsForSelects(MIItBegin, MIItEnd, StartMBB, FalseMBB, MBB); 6429 6430 StartMBB->erase(MIItBegin, MIItEnd); 6431 return JoinMBB; 6432 } 6433 6434 // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI. 6435 // StoreOpcode is the store to use and Invert says whether the store should 6436 // happen when the condition is false rather than true. If a STORE ON 6437 // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0. 6438 MachineBasicBlock *SystemZTargetLowering::emitCondStore(MachineInstr &MI, 6439 MachineBasicBlock *MBB, 6440 unsigned StoreOpcode, 6441 unsigned STOCOpcode, 6442 bool Invert) const { 6443 const SystemZInstrInfo *TII = 6444 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 6445 6446 unsigned SrcReg = MI.getOperand(0).getReg(); 6447 MachineOperand Base = MI.getOperand(1); 6448 int64_t Disp = MI.getOperand(2).getImm(); 6449 unsigned IndexReg = MI.getOperand(3).getReg(); 6450 unsigned CCValid = MI.getOperand(4).getImm(); 6451 unsigned CCMask = MI.getOperand(5).getImm(); 6452 DebugLoc DL = MI.getDebugLoc(); 6453 6454 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp); 6455 6456 // Use STOCOpcode if possible. We could use different store patterns in 6457 // order to avoid matching the index register, but the performance trade-offs 6458 // might be more complicated in that case. 6459 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) { 6460 if (Invert) 6461 CCMask ^= CCValid; 6462 6463 // ISel pattern matching also adds a load memory operand of the same 6464 // address, so take special care to find the storing memory operand. 6465 MachineMemOperand *MMO = nullptr; 6466 for (auto *I : MI.memoperands()) 6467 if (I->isStore()) { 6468 MMO = I; 6469 break; 6470 } 6471 6472 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode)) 6473 .addReg(SrcReg) 6474 .add(Base) 6475 .addImm(Disp) 6476 .addImm(CCValid) 6477 .addImm(CCMask) 6478 .addMemOperand(MMO); 6479 6480 MI.eraseFromParent(); 6481 return MBB; 6482 } 6483 6484 // Get the condition needed to branch around the store. 6485 if (!Invert) 6486 CCMask ^= CCValid; 6487 6488 MachineBasicBlock *StartMBB = MBB; 6489 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB); 6490 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB); 6491 6492 // Unless CC was killed in the CondStore instruction, mark it as 6493 // live-in to both FalseMBB and JoinMBB. 6494 if (!MI.killsRegister(SystemZ::CC) && !checkCCKill(MI, JoinMBB)) { 6495 FalseMBB->addLiveIn(SystemZ::CC); 6496 JoinMBB->addLiveIn(SystemZ::CC); 6497 } 6498 6499 // StartMBB: 6500 // BRC CCMask, JoinMBB 6501 // # fallthrough to FalseMBB 6502 MBB = StartMBB; 6503 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 6504 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); 6505 MBB->addSuccessor(JoinMBB); 6506 MBB->addSuccessor(FalseMBB); 6507 6508 // FalseMBB: 6509 // store %SrcReg, %Disp(%Index,%Base) 6510 // # fallthrough to JoinMBB 6511 MBB = FalseMBB; 6512 BuildMI(MBB, DL, TII->get(StoreOpcode)) 6513 .addReg(SrcReg) 6514 .add(Base) 6515 .addImm(Disp) 6516 .addReg(IndexReg); 6517 MBB->addSuccessor(JoinMBB); 6518 6519 MI.eraseFromParent(); 6520 return JoinMBB; 6521 } 6522 6523 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_* 6524 // or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that 6525 // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}. 6526 // BitSize is the width of the field in bits, or 0 if this is a partword 6527 // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize 6528 // is one of the operands. Invert says whether the field should be 6529 // inverted after performing BinOpcode (e.g. for NAND). 6530 MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadBinary( 6531 MachineInstr &MI, MachineBasicBlock *MBB, unsigned BinOpcode, 6532 unsigned BitSize, bool Invert) const { 6533 MachineFunction &MF = *MBB->getParent(); 6534 const SystemZInstrInfo *TII = 6535 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 6536 MachineRegisterInfo &MRI = MF.getRegInfo(); 6537 bool IsSubWord = (BitSize < 32); 6538 6539 // Extract the operands. Base can be a register or a frame index. 6540 // Src2 can be a register or immediate. 6541 unsigned Dest = MI.getOperand(0).getReg(); 6542 MachineOperand Base = earlyUseOperand(MI.getOperand(1)); 6543 int64_t Disp = MI.getOperand(2).getImm(); 6544 MachineOperand Src2 = earlyUseOperand(MI.getOperand(3)); 6545 Register BitShift = IsSubWord ? MI.getOperand(4).getReg() : Register(); 6546 Register NegBitShift = IsSubWord ? MI.getOperand(5).getReg() : Register(); 6547 DebugLoc DL = MI.getDebugLoc(); 6548 if (IsSubWord) 6549 BitSize = MI.getOperand(6).getImm(); 6550 6551 // Subword operations use 32-bit registers. 6552 const TargetRegisterClass *RC = (BitSize <= 32 ? 6553 &SystemZ::GR32BitRegClass : 6554 &SystemZ::GR64BitRegClass); 6555 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; 6556 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; 6557 6558 // Get the right opcodes for the displacement. 6559 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); 6560 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); 6561 assert(LOpcode && CSOpcode && "Displacement out of range"); 6562 6563 // Create virtual registers for temporary results. 6564 Register OrigVal = MRI.createVirtualRegister(RC); 6565 Register OldVal = MRI.createVirtualRegister(RC); 6566 Register NewVal = (BinOpcode || IsSubWord ? 6567 MRI.createVirtualRegister(RC) : Src2.getReg()); 6568 Register RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); 6569 Register RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); 6570 6571 // Insert a basic block for the main loop. 6572 MachineBasicBlock *StartMBB = MBB; 6573 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 6574 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 6575 6576 // StartMBB: 6577 // ... 6578 // %OrigVal = L Disp(%Base) 6579 // # fall through to LoopMMB 6580 MBB = StartMBB; 6581 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0); 6582 MBB->addSuccessor(LoopMBB); 6583 6584 // LoopMBB: 6585 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ] 6586 // %RotatedOldVal = RLL %OldVal, 0(%BitShift) 6587 // %RotatedNewVal = OP %RotatedOldVal, %Src2 6588 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) 6589 // %Dest = CS %OldVal, %NewVal, Disp(%Base) 6590 // JNE LoopMBB 6591 // # fall through to DoneMMB 6592 MBB = LoopMBB; 6593 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 6594 .addReg(OrigVal).addMBB(StartMBB) 6595 .addReg(Dest).addMBB(LoopMBB); 6596 if (IsSubWord) 6597 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) 6598 .addReg(OldVal).addReg(BitShift).addImm(0); 6599 if (Invert) { 6600 // Perform the operation normally and then invert every bit of the field. 6601 unsigned Tmp = MRI.createVirtualRegister(RC); 6602 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp).addReg(RotatedOldVal).add(Src2); 6603 if (BitSize <= 32) 6604 // XILF with the upper BitSize bits set. 6605 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal) 6606 .addReg(Tmp).addImm(-1U << (32 - BitSize)); 6607 else { 6608 // Use LCGR and add -1 to the result, which is more compact than 6609 // an XILF, XILH pair. 6610 unsigned Tmp2 = MRI.createVirtualRegister(RC); 6611 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp); 6612 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal) 6613 .addReg(Tmp2).addImm(-1); 6614 } 6615 } else if (BinOpcode) 6616 // A simply binary operation. 6617 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal) 6618 .addReg(RotatedOldVal) 6619 .add(Src2); 6620 else if (IsSubWord) 6621 // Use RISBG to rotate Src2 into position and use it to replace the 6622 // field in RotatedOldVal. 6623 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal) 6624 .addReg(RotatedOldVal).addReg(Src2.getReg()) 6625 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize); 6626 if (IsSubWord) 6627 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) 6628 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); 6629 BuildMI(MBB, DL, TII->get(CSOpcode), Dest) 6630 .addReg(OldVal) 6631 .addReg(NewVal) 6632 .add(Base) 6633 .addImm(Disp); 6634 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 6635 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 6636 MBB->addSuccessor(LoopMBB); 6637 MBB->addSuccessor(DoneMBB); 6638 6639 MI.eraseFromParent(); 6640 return DoneMBB; 6641 } 6642 6643 // Implement EmitInstrWithCustomInserter for pseudo 6644 // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the 6645 // instruction that should be used to compare the current field with the 6646 // minimum or maximum value. KeepOldMask is the BRC condition-code mask 6647 // for when the current field should be kept. BitSize is the width of 6648 // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction. 6649 MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadMinMax( 6650 MachineInstr &MI, MachineBasicBlock *MBB, unsigned CompareOpcode, 6651 unsigned KeepOldMask, unsigned BitSize) const { 6652 MachineFunction &MF = *MBB->getParent(); 6653 const SystemZInstrInfo *TII = 6654 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 6655 MachineRegisterInfo &MRI = MF.getRegInfo(); 6656 bool IsSubWord = (BitSize < 32); 6657 6658 // Extract the operands. Base can be a register or a frame index. 6659 unsigned Dest = MI.getOperand(0).getReg(); 6660 MachineOperand Base = earlyUseOperand(MI.getOperand(1)); 6661 int64_t Disp = MI.getOperand(2).getImm(); 6662 Register Src2 = MI.getOperand(3).getReg(); 6663 Register BitShift = (IsSubWord ? MI.getOperand(4).getReg() : Register()); 6664 Register NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : Register()); 6665 DebugLoc DL = MI.getDebugLoc(); 6666 if (IsSubWord) 6667 BitSize = MI.getOperand(6).getImm(); 6668 6669 // Subword operations use 32-bit registers. 6670 const TargetRegisterClass *RC = (BitSize <= 32 ? 6671 &SystemZ::GR32BitRegClass : 6672 &SystemZ::GR64BitRegClass); 6673 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; 6674 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; 6675 6676 // Get the right opcodes for the displacement. 6677 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); 6678 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); 6679 assert(LOpcode && CSOpcode && "Displacement out of range"); 6680 6681 // Create virtual registers for temporary results. 6682 Register OrigVal = MRI.createVirtualRegister(RC); 6683 Register OldVal = MRI.createVirtualRegister(RC); 6684 Register NewVal = MRI.createVirtualRegister(RC); 6685 Register RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); 6686 Register RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2); 6687 Register RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); 6688 6689 // Insert 3 basic blocks for the loop. 6690 MachineBasicBlock *StartMBB = MBB; 6691 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 6692 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 6693 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB); 6694 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB); 6695 6696 // StartMBB: 6697 // ... 6698 // %OrigVal = L Disp(%Base) 6699 // # fall through to LoopMMB 6700 MBB = StartMBB; 6701 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0); 6702 MBB->addSuccessor(LoopMBB); 6703 6704 // LoopMBB: 6705 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ] 6706 // %RotatedOldVal = RLL %OldVal, 0(%BitShift) 6707 // CompareOpcode %RotatedOldVal, %Src2 6708 // BRC KeepOldMask, UpdateMBB 6709 MBB = LoopMBB; 6710 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 6711 .addReg(OrigVal).addMBB(StartMBB) 6712 .addReg(Dest).addMBB(UpdateMBB); 6713 if (IsSubWord) 6714 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) 6715 .addReg(OldVal).addReg(BitShift).addImm(0); 6716 BuildMI(MBB, DL, TII->get(CompareOpcode)) 6717 .addReg(RotatedOldVal).addReg(Src2); 6718 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 6719 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB); 6720 MBB->addSuccessor(UpdateMBB); 6721 MBB->addSuccessor(UseAltMBB); 6722 6723 // UseAltMBB: 6724 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0 6725 // # fall through to UpdateMMB 6726 MBB = UseAltMBB; 6727 if (IsSubWord) 6728 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal) 6729 .addReg(RotatedOldVal).addReg(Src2) 6730 .addImm(32).addImm(31 + BitSize).addImm(0); 6731 MBB->addSuccessor(UpdateMBB); 6732 6733 // UpdateMBB: 6734 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ], 6735 // [ %RotatedAltVal, UseAltMBB ] 6736 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) 6737 // %Dest = CS %OldVal, %NewVal, Disp(%Base) 6738 // JNE LoopMBB 6739 // # fall through to DoneMMB 6740 MBB = UpdateMBB; 6741 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal) 6742 .addReg(RotatedOldVal).addMBB(LoopMBB) 6743 .addReg(RotatedAltVal).addMBB(UseAltMBB); 6744 if (IsSubWord) 6745 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) 6746 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); 6747 BuildMI(MBB, DL, TII->get(CSOpcode), Dest) 6748 .addReg(OldVal) 6749 .addReg(NewVal) 6750 .add(Base) 6751 .addImm(Disp); 6752 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 6753 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 6754 MBB->addSuccessor(LoopMBB); 6755 MBB->addSuccessor(DoneMBB); 6756 6757 MI.eraseFromParent(); 6758 return DoneMBB; 6759 } 6760 6761 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW 6762 // instruction MI. 6763 MachineBasicBlock * 6764 SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI, 6765 MachineBasicBlock *MBB) const { 6766 6767 MachineFunction &MF = *MBB->getParent(); 6768 const SystemZInstrInfo *TII = 6769 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 6770 MachineRegisterInfo &MRI = MF.getRegInfo(); 6771 6772 // Extract the operands. Base can be a register or a frame index. 6773 unsigned Dest = MI.getOperand(0).getReg(); 6774 MachineOperand Base = earlyUseOperand(MI.getOperand(1)); 6775 int64_t Disp = MI.getOperand(2).getImm(); 6776 unsigned OrigCmpVal = MI.getOperand(3).getReg(); 6777 unsigned OrigSwapVal = MI.getOperand(4).getReg(); 6778 unsigned BitShift = MI.getOperand(5).getReg(); 6779 unsigned NegBitShift = MI.getOperand(6).getReg(); 6780 int64_t BitSize = MI.getOperand(7).getImm(); 6781 DebugLoc DL = MI.getDebugLoc(); 6782 6783 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass; 6784 6785 // Get the right opcodes for the displacement. 6786 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp); 6787 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp); 6788 assert(LOpcode && CSOpcode && "Displacement out of range"); 6789 6790 // Create virtual registers for temporary results. 6791 unsigned OrigOldVal = MRI.createVirtualRegister(RC); 6792 unsigned OldVal = MRI.createVirtualRegister(RC); 6793 unsigned CmpVal = MRI.createVirtualRegister(RC); 6794 unsigned SwapVal = MRI.createVirtualRegister(RC); 6795 unsigned StoreVal = MRI.createVirtualRegister(RC); 6796 unsigned RetryOldVal = MRI.createVirtualRegister(RC); 6797 unsigned RetryCmpVal = MRI.createVirtualRegister(RC); 6798 unsigned RetrySwapVal = MRI.createVirtualRegister(RC); 6799 6800 // Insert 2 basic blocks for the loop. 6801 MachineBasicBlock *StartMBB = MBB; 6802 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 6803 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 6804 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB); 6805 6806 // StartMBB: 6807 // ... 6808 // %OrigOldVal = L Disp(%Base) 6809 // # fall through to LoopMMB 6810 MBB = StartMBB; 6811 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal) 6812 .add(Base) 6813 .addImm(Disp) 6814 .addReg(0); 6815 MBB->addSuccessor(LoopMBB); 6816 6817 // LoopMBB: 6818 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ] 6819 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ] 6820 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ] 6821 // %Dest = RLL %OldVal, BitSize(%BitShift) 6822 // ^^ The low BitSize bits contain the field 6823 // of interest. 6824 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0 6825 // ^^ Replace the upper 32-BitSize bits of the 6826 // comparison value with those that we loaded, 6827 // so that we can use a full word comparison. 6828 // CR %Dest, %RetryCmpVal 6829 // JNE DoneMBB 6830 // # Fall through to SetMBB 6831 MBB = LoopMBB; 6832 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 6833 .addReg(OrigOldVal).addMBB(StartMBB) 6834 .addReg(RetryOldVal).addMBB(SetMBB); 6835 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal) 6836 .addReg(OrigCmpVal).addMBB(StartMBB) 6837 .addReg(RetryCmpVal).addMBB(SetMBB); 6838 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal) 6839 .addReg(OrigSwapVal).addMBB(StartMBB) 6840 .addReg(RetrySwapVal).addMBB(SetMBB); 6841 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest) 6842 .addReg(OldVal).addReg(BitShift).addImm(BitSize); 6843 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal) 6844 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); 6845 BuildMI(MBB, DL, TII->get(SystemZ::CR)) 6846 .addReg(Dest).addReg(RetryCmpVal); 6847 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 6848 .addImm(SystemZ::CCMASK_ICMP) 6849 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB); 6850 MBB->addSuccessor(DoneMBB); 6851 MBB->addSuccessor(SetMBB); 6852 6853 // SetMBB: 6854 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0 6855 // ^^ Replace the upper 32-BitSize bits of the new 6856 // value with those that we loaded. 6857 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift) 6858 // ^^ Rotate the new field to its proper position. 6859 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base) 6860 // JNE LoopMBB 6861 // # fall through to ExitMMB 6862 MBB = SetMBB; 6863 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal) 6864 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); 6865 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal) 6866 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize); 6867 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal) 6868 .addReg(OldVal) 6869 .addReg(StoreVal) 6870 .add(Base) 6871 .addImm(Disp); 6872 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 6873 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 6874 MBB->addSuccessor(LoopMBB); 6875 MBB->addSuccessor(DoneMBB); 6876 6877 // If the CC def wasn't dead in the ATOMIC_CMP_SWAPW, mark CC as live-in 6878 // to the block after the loop. At this point, CC may have been defined 6879 // either by the CR in LoopMBB or by the CS in SetMBB. 6880 if (!MI.registerDefIsDead(SystemZ::CC)) 6881 DoneMBB->addLiveIn(SystemZ::CC); 6882 6883 MI.eraseFromParent(); 6884 return DoneMBB; 6885 } 6886 6887 // Emit a move from two GR64s to a GR128. 6888 MachineBasicBlock * 6889 SystemZTargetLowering::emitPair128(MachineInstr &MI, 6890 MachineBasicBlock *MBB) const { 6891 MachineFunction &MF = *MBB->getParent(); 6892 const SystemZInstrInfo *TII = 6893 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 6894 MachineRegisterInfo &MRI = MF.getRegInfo(); 6895 DebugLoc DL = MI.getDebugLoc(); 6896 6897 unsigned Dest = MI.getOperand(0).getReg(); 6898 unsigned Hi = MI.getOperand(1).getReg(); 6899 unsigned Lo = MI.getOperand(2).getReg(); 6900 unsigned Tmp1 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 6901 unsigned Tmp2 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 6902 6903 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Tmp1); 6904 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Tmp2) 6905 .addReg(Tmp1).addReg(Hi).addImm(SystemZ::subreg_h64); 6906 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest) 6907 .addReg(Tmp2).addReg(Lo).addImm(SystemZ::subreg_l64); 6908 6909 MI.eraseFromParent(); 6910 return MBB; 6911 } 6912 6913 // Emit an extension from a GR64 to a GR128. ClearEven is true 6914 // if the high register of the GR128 value must be cleared or false if 6915 // it's "don't care". 6916 MachineBasicBlock *SystemZTargetLowering::emitExt128(MachineInstr &MI, 6917 MachineBasicBlock *MBB, 6918 bool ClearEven) const { 6919 MachineFunction &MF = *MBB->getParent(); 6920 const SystemZInstrInfo *TII = 6921 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 6922 MachineRegisterInfo &MRI = MF.getRegInfo(); 6923 DebugLoc DL = MI.getDebugLoc(); 6924 6925 unsigned Dest = MI.getOperand(0).getReg(); 6926 unsigned Src = MI.getOperand(1).getReg(); 6927 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 6928 6929 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128); 6930 if (ClearEven) { 6931 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 6932 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass); 6933 6934 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64) 6935 .addImm(0); 6936 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128) 6937 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64); 6938 In128 = NewIn128; 6939 } 6940 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest) 6941 .addReg(In128).addReg(Src).addImm(SystemZ::subreg_l64); 6942 6943 MI.eraseFromParent(); 6944 return MBB; 6945 } 6946 6947 MachineBasicBlock *SystemZTargetLowering::emitMemMemWrapper( 6948 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const { 6949 MachineFunction &MF = *MBB->getParent(); 6950 const SystemZInstrInfo *TII = 6951 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 6952 MachineRegisterInfo &MRI = MF.getRegInfo(); 6953 DebugLoc DL = MI.getDebugLoc(); 6954 6955 MachineOperand DestBase = earlyUseOperand(MI.getOperand(0)); 6956 uint64_t DestDisp = MI.getOperand(1).getImm(); 6957 MachineOperand SrcBase = earlyUseOperand(MI.getOperand(2)); 6958 uint64_t SrcDisp = MI.getOperand(3).getImm(); 6959 uint64_t Length = MI.getOperand(4).getImm(); 6960 6961 // When generating more than one CLC, all but the last will need to 6962 // branch to the end when a difference is found. 6963 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ? 6964 splitBlockAfter(MI, MBB) : nullptr); 6965 6966 // Check for the loop form, in which operand 5 is the trip count. 6967 if (MI.getNumExplicitOperands() > 5) { 6968 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase); 6969 6970 Register StartCountReg = MI.getOperand(5).getReg(); 6971 Register StartSrcReg = forceReg(MI, SrcBase, TII); 6972 Register StartDestReg = (HaveSingleBase ? StartSrcReg : 6973 forceReg(MI, DestBase, TII)); 6974 6975 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass; 6976 Register ThisSrcReg = MRI.createVirtualRegister(RC); 6977 Register ThisDestReg = (HaveSingleBase ? ThisSrcReg : 6978 MRI.createVirtualRegister(RC)); 6979 Register NextSrcReg = MRI.createVirtualRegister(RC); 6980 Register NextDestReg = (HaveSingleBase ? NextSrcReg : 6981 MRI.createVirtualRegister(RC)); 6982 6983 RC = &SystemZ::GR64BitRegClass; 6984 Register ThisCountReg = MRI.createVirtualRegister(RC); 6985 Register NextCountReg = MRI.createVirtualRegister(RC); 6986 6987 MachineBasicBlock *StartMBB = MBB; 6988 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 6989 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 6990 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB); 6991 6992 // StartMBB: 6993 // # fall through to LoopMMB 6994 MBB->addSuccessor(LoopMBB); 6995 6996 // LoopMBB: 6997 // %ThisDestReg = phi [ %StartDestReg, StartMBB ], 6998 // [ %NextDestReg, NextMBB ] 6999 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ], 7000 // [ %NextSrcReg, NextMBB ] 7001 // %ThisCountReg = phi [ %StartCountReg, StartMBB ], 7002 // [ %NextCountReg, NextMBB ] 7003 // ( PFD 2, 768+DestDisp(%ThisDestReg) ) 7004 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg) 7005 // ( JLH EndMBB ) 7006 // 7007 // The prefetch is used only for MVC. The JLH is used only for CLC. 7008 MBB = LoopMBB; 7009 7010 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg) 7011 .addReg(StartDestReg).addMBB(StartMBB) 7012 .addReg(NextDestReg).addMBB(NextMBB); 7013 if (!HaveSingleBase) 7014 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg) 7015 .addReg(StartSrcReg).addMBB(StartMBB) 7016 .addReg(NextSrcReg).addMBB(NextMBB); 7017 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg) 7018 .addReg(StartCountReg).addMBB(StartMBB) 7019 .addReg(NextCountReg).addMBB(NextMBB); 7020 if (Opcode == SystemZ::MVC) 7021 BuildMI(MBB, DL, TII->get(SystemZ::PFD)) 7022 .addImm(SystemZ::PFD_WRITE) 7023 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0); 7024 BuildMI(MBB, DL, TII->get(Opcode)) 7025 .addReg(ThisDestReg).addImm(DestDisp).addImm(256) 7026 .addReg(ThisSrcReg).addImm(SrcDisp); 7027 if (EndMBB) { 7028 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7029 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 7030 .addMBB(EndMBB); 7031 MBB->addSuccessor(EndMBB); 7032 MBB->addSuccessor(NextMBB); 7033 } 7034 7035 // NextMBB: 7036 // %NextDestReg = LA 256(%ThisDestReg) 7037 // %NextSrcReg = LA 256(%ThisSrcReg) 7038 // %NextCountReg = AGHI %ThisCountReg, -1 7039 // CGHI %NextCountReg, 0 7040 // JLH LoopMBB 7041 // # fall through to DoneMMB 7042 // 7043 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes. 7044 MBB = NextMBB; 7045 7046 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg) 7047 .addReg(ThisDestReg).addImm(256).addReg(0); 7048 if (!HaveSingleBase) 7049 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg) 7050 .addReg(ThisSrcReg).addImm(256).addReg(0); 7051 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg) 7052 .addReg(ThisCountReg).addImm(-1); 7053 BuildMI(MBB, DL, TII->get(SystemZ::CGHI)) 7054 .addReg(NextCountReg).addImm(0); 7055 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7056 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 7057 .addMBB(LoopMBB); 7058 MBB->addSuccessor(LoopMBB); 7059 MBB->addSuccessor(DoneMBB); 7060 7061 DestBase = MachineOperand::CreateReg(NextDestReg, false); 7062 SrcBase = MachineOperand::CreateReg(NextSrcReg, false); 7063 Length &= 255; 7064 if (EndMBB && !Length) 7065 // If the loop handled the whole CLC range, DoneMBB will be empty with 7066 // CC live-through into EndMBB, so add it as live-in. 7067 DoneMBB->addLiveIn(SystemZ::CC); 7068 MBB = DoneMBB; 7069 } 7070 // Handle any remaining bytes with straight-line code. 7071 while (Length > 0) { 7072 uint64_t ThisLength = std::min(Length, uint64_t(256)); 7073 // The previous iteration might have created out-of-range displacements. 7074 // Apply them using LAY if so. 7075 if (!isUInt<12>(DestDisp)) { 7076 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 7077 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg) 7078 .add(DestBase) 7079 .addImm(DestDisp) 7080 .addReg(0); 7081 DestBase = MachineOperand::CreateReg(Reg, false); 7082 DestDisp = 0; 7083 } 7084 if (!isUInt<12>(SrcDisp)) { 7085 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 7086 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg) 7087 .add(SrcBase) 7088 .addImm(SrcDisp) 7089 .addReg(0); 7090 SrcBase = MachineOperand::CreateReg(Reg, false); 7091 SrcDisp = 0; 7092 } 7093 BuildMI(*MBB, MI, DL, TII->get(Opcode)) 7094 .add(DestBase) 7095 .addImm(DestDisp) 7096 .addImm(ThisLength) 7097 .add(SrcBase) 7098 .addImm(SrcDisp) 7099 .setMemRefs(MI.memoperands()); 7100 DestDisp += ThisLength; 7101 SrcDisp += ThisLength; 7102 Length -= ThisLength; 7103 // If there's another CLC to go, branch to the end if a difference 7104 // was found. 7105 if (EndMBB && Length > 0) { 7106 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB); 7107 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7108 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 7109 .addMBB(EndMBB); 7110 MBB->addSuccessor(EndMBB); 7111 MBB->addSuccessor(NextMBB); 7112 MBB = NextMBB; 7113 } 7114 } 7115 if (EndMBB) { 7116 MBB->addSuccessor(EndMBB); 7117 MBB = EndMBB; 7118 MBB->addLiveIn(SystemZ::CC); 7119 } 7120 7121 MI.eraseFromParent(); 7122 return MBB; 7123 } 7124 7125 // Decompose string pseudo-instruction MI into a loop that continually performs 7126 // Opcode until CC != 3. 7127 MachineBasicBlock *SystemZTargetLowering::emitStringWrapper( 7128 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const { 7129 MachineFunction &MF = *MBB->getParent(); 7130 const SystemZInstrInfo *TII = 7131 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 7132 MachineRegisterInfo &MRI = MF.getRegInfo(); 7133 DebugLoc DL = MI.getDebugLoc(); 7134 7135 uint64_t End1Reg = MI.getOperand(0).getReg(); 7136 uint64_t Start1Reg = MI.getOperand(1).getReg(); 7137 uint64_t Start2Reg = MI.getOperand(2).getReg(); 7138 uint64_t CharReg = MI.getOperand(3).getReg(); 7139 7140 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass; 7141 uint64_t This1Reg = MRI.createVirtualRegister(RC); 7142 uint64_t This2Reg = MRI.createVirtualRegister(RC); 7143 uint64_t End2Reg = MRI.createVirtualRegister(RC); 7144 7145 MachineBasicBlock *StartMBB = MBB; 7146 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 7147 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 7148 7149 // StartMBB: 7150 // # fall through to LoopMMB 7151 MBB->addSuccessor(LoopMBB); 7152 7153 // LoopMBB: 7154 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ] 7155 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ] 7156 // R0L = %CharReg 7157 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L 7158 // JO LoopMBB 7159 // # fall through to DoneMMB 7160 // 7161 // The load of R0L can be hoisted by post-RA LICM. 7162 MBB = LoopMBB; 7163 7164 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg) 7165 .addReg(Start1Reg).addMBB(StartMBB) 7166 .addReg(End1Reg).addMBB(LoopMBB); 7167 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg) 7168 .addReg(Start2Reg).addMBB(StartMBB) 7169 .addReg(End2Reg).addMBB(LoopMBB); 7170 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg); 7171 BuildMI(MBB, DL, TII->get(Opcode)) 7172 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define) 7173 .addReg(This1Reg).addReg(This2Reg); 7174 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 7175 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB); 7176 MBB->addSuccessor(LoopMBB); 7177 MBB->addSuccessor(DoneMBB); 7178 7179 DoneMBB->addLiveIn(SystemZ::CC); 7180 7181 MI.eraseFromParent(); 7182 return DoneMBB; 7183 } 7184 7185 // Update TBEGIN instruction with final opcode and register clobbers. 7186 MachineBasicBlock *SystemZTargetLowering::emitTransactionBegin( 7187 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode, 7188 bool NoFloat) const { 7189 MachineFunction &MF = *MBB->getParent(); 7190 const TargetFrameLowering *TFI = Subtarget.getFrameLowering(); 7191 const SystemZInstrInfo *TII = Subtarget.getInstrInfo(); 7192 7193 // Update opcode. 7194 MI.setDesc(TII->get(Opcode)); 7195 7196 // We cannot handle a TBEGIN that clobbers the stack or frame pointer. 7197 // Make sure to add the corresponding GRSM bits if they are missing. 7198 uint64_t Control = MI.getOperand(2).getImm(); 7199 static const unsigned GPRControlBit[16] = { 7200 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000, 7201 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100 7202 }; 7203 Control |= GPRControlBit[15]; 7204 if (TFI->hasFP(MF)) 7205 Control |= GPRControlBit[11]; 7206 MI.getOperand(2).setImm(Control); 7207 7208 // Add GPR clobbers. 7209 for (int I = 0; I < 16; I++) { 7210 if ((Control & GPRControlBit[I]) == 0) { 7211 unsigned Reg = SystemZMC::GR64Regs[I]; 7212 MI.addOperand(MachineOperand::CreateReg(Reg, true, true)); 7213 } 7214 } 7215 7216 // Add FPR/VR clobbers. 7217 if (!NoFloat && (Control & 4) != 0) { 7218 if (Subtarget.hasVector()) { 7219 for (int I = 0; I < 32; I++) { 7220 unsigned Reg = SystemZMC::VR128Regs[I]; 7221 MI.addOperand(MachineOperand::CreateReg(Reg, true, true)); 7222 } 7223 } else { 7224 for (int I = 0; I < 16; I++) { 7225 unsigned Reg = SystemZMC::FP64Regs[I]; 7226 MI.addOperand(MachineOperand::CreateReg(Reg, true, true)); 7227 } 7228 } 7229 } 7230 7231 return MBB; 7232 } 7233 7234 MachineBasicBlock *SystemZTargetLowering::emitLoadAndTestCmp0( 7235 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const { 7236 MachineFunction &MF = *MBB->getParent(); 7237 MachineRegisterInfo *MRI = &MF.getRegInfo(); 7238 const SystemZInstrInfo *TII = 7239 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); 7240 DebugLoc DL = MI.getDebugLoc(); 7241 7242 unsigned SrcReg = MI.getOperand(0).getReg(); 7243 7244 // Create new virtual register of the same class as source. 7245 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg); 7246 unsigned DstReg = MRI->createVirtualRegister(RC); 7247 7248 // Replace pseudo with a normal load-and-test that models the def as 7249 // well. 7250 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg) 7251 .addReg(SrcReg); 7252 MI.eraseFromParent(); 7253 7254 return MBB; 7255 } 7256 7257 MachineBasicBlock *SystemZTargetLowering::EmitInstrWithCustomInserter( 7258 MachineInstr &MI, MachineBasicBlock *MBB) const { 7259 switch (MI.getOpcode()) { 7260 case SystemZ::Select32: 7261 case SystemZ::Select64: 7262 case SystemZ::SelectF32: 7263 case SystemZ::SelectF64: 7264 case SystemZ::SelectF128: 7265 case SystemZ::SelectVR32: 7266 case SystemZ::SelectVR64: 7267 case SystemZ::SelectVR128: 7268 return emitSelect(MI, MBB); 7269 7270 case SystemZ::CondStore8Mux: 7271 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false); 7272 case SystemZ::CondStore8MuxInv: 7273 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true); 7274 case SystemZ::CondStore16Mux: 7275 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false); 7276 case SystemZ::CondStore16MuxInv: 7277 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true); 7278 case SystemZ::CondStore32Mux: 7279 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, false); 7280 case SystemZ::CondStore32MuxInv: 7281 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, true); 7282 case SystemZ::CondStore8: 7283 return emitCondStore(MI, MBB, SystemZ::STC, 0, false); 7284 case SystemZ::CondStore8Inv: 7285 return emitCondStore(MI, MBB, SystemZ::STC, 0, true); 7286 case SystemZ::CondStore16: 7287 return emitCondStore(MI, MBB, SystemZ::STH, 0, false); 7288 case SystemZ::CondStore16Inv: 7289 return emitCondStore(MI, MBB, SystemZ::STH, 0, true); 7290 case SystemZ::CondStore32: 7291 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false); 7292 case SystemZ::CondStore32Inv: 7293 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true); 7294 case SystemZ::CondStore64: 7295 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false); 7296 case SystemZ::CondStore64Inv: 7297 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true); 7298 case SystemZ::CondStoreF32: 7299 return emitCondStore(MI, MBB, SystemZ::STE, 0, false); 7300 case SystemZ::CondStoreF32Inv: 7301 return emitCondStore(MI, MBB, SystemZ::STE, 0, true); 7302 case SystemZ::CondStoreF64: 7303 return emitCondStore(MI, MBB, SystemZ::STD, 0, false); 7304 case SystemZ::CondStoreF64Inv: 7305 return emitCondStore(MI, MBB, SystemZ::STD, 0, true); 7306 7307 case SystemZ::PAIR128: 7308 return emitPair128(MI, MBB); 7309 case SystemZ::AEXT128: 7310 return emitExt128(MI, MBB, false); 7311 case SystemZ::ZEXT128: 7312 return emitExt128(MI, MBB, true); 7313 7314 case SystemZ::ATOMIC_SWAPW: 7315 return emitAtomicLoadBinary(MI, MBB, 0, 0); 7316 case SystemZ::ATOMIC_SWAP_32: 7317 return emitAtomicLoadBinary(MI, MBB, 0, 32); 7318 case SystemZ::ATOMIC_SWAP_64: 7319 return emitAtomicLoadBinary(MI, MBB, 0, 64); 7320 7321 case SystemZ::ATOMIC_LOADW_AR: 7322 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0); 7323 case SystemZ::ATOMIC_LOADW_AFI: 7324 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0); 7325 case SystemZ::ATOMIC_LOAD_AR: 7326 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32); 7327 case SystemZ::ATOMIC_LOAD_AHI: 7328 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32); 7329 case SystemZ::ATOMIC_LOAD_AFI: 7330 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32); 7331 case SystemZ::ATOMIC_LOAD_AGR: 7332 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64); 7333 case SystemZ::ATOMIC_LOAD_AGHI: 7334 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64); 7335 case SystemZ::ATOMIC_LOAD_AGFI: 7336 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64); 7337 7338 case SystemZ::ATOMIC_LOADW_SR: 7339 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0); 7340 case SystemZ::ATOMIC_LOAD_SR: 7341 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32); 7342 case SystemZ::ATOMIC_LOAD_SGR: 7343 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64); 7344 7345 case SystemZ::ATOMIC_LOADW_NR: 7346 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0); 7347 case SystemZ::ATOMIC_LOADW_NILH: 7348 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0); 7349 case SystemZ::ATOMIC_LOAD_NR: 7350 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32); 7351 case SystemZ::ATOMIC_LOAD_NILL: 7352 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32); 7353 case SystemZ::ATOMIC_LOAD_NILH: 7354 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32); 7355 case SystemZ::ATOMIC_LOAD_NILF: 7356 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32); 7357 case SystemZ::ATOMIC_LOAD_NGR: 7358 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64); 7359 case SystemZ::ATOMIC_LOAD_NILL64: 7360 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64); 7361 case SystemZ::ATOMIC_LOAD_NILH64: 7362 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64); 7363 case SystemZ::ATOMIC_LOAD_NIHL64: 7364 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64); 7365 case SystemZ::ATOMIC_LOAD_NIHH64: 7366 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64); 7367 case SystemZ::ATOMIC_LOAD_NILF64: 7368 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64); 7369 case SystemZ::ATOMIC_LOAD_NIHF64: 7370 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64); 7371 7372 case SystemZ::ATOMIC_LOADW_OR: 7373 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0); 7374 case SystemZ::ATOMIC_LOADW_OILH: 7375 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0); 7376 case SystemZ::ATOMIC_LOAD_OR: 7377 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32); 7378 case SystemZ::ATOMIC_LOAD_OILL: 7379 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32); 7380 case SystemZ::ATOMIC_LOAD_OILH: 7381 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32); 7382 case SystemZ::ATOMIC_LOAD_OILF: 7383 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32); 7384 case SystemZ::ATOMIC_LOAD_OGR: 7385 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64); 7386 case SystemZ::ATOMIC_LOAD_OILL64: 7387 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64); 7388 case SystemZ::ATOMIC_LOAD_OILH64: 7389 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64); 7390 case SystemZ::ATOMIC_LOAD_OIHL64: 7391 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64); 7392 case SystemZ::ATOMIC_LOAD_OIHH64: 7393 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64); 7394 case SystemZ::ATOMIC_LOAD_OILF64: 7395 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64); 7396 case SystemZ::ATOMIC_LOAD_OIHF64: 7397 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64); 7398 7399 case SystemZ::ATOMIC_LOADW_XR: 7400 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0); 7401 case SystemZ::ATOMIC_LOADW_XILF: 7402 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0); 7403 case SystemZ::ATOMIC_LOAD_XR: 7404 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32); 7405 case SystemZ::ATOMIC_LOAD_XILF: 7406 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32); 7407 case SystemZ::ATOMIC_LOAD_XGR: 7408 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64); 7409 case SystemZ::ATOMIC_LOAD_XILF64: 7410 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64); 7411 case SystemZ::ATOMIC_LOAD_XIHF64: 7412 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64); 7413 7414 case SystemZ::ATOMIC_LOADW_NRi: 7415 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true); 7416 case SystemZ::ATOMIC_LOADW_NILHi: 7417 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true); 7418 case SystemZ::ATOMIC_LOAD_NRi: 7419 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true); 7420 case SystemZ::ATOMIC_LOAD_NILLi: 7421 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true); 7422 case SystemZ::ATOMIC_LOAD_NILHi: 7423 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true); 7424 case SystemZ::ATOMIC_LOAD_NILFi: 7425 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true); 7426 case SystemZ::ATOMIC_LOAD_NGRi: 7427 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true); 7428 case SystemZ::ATOMIC_LOAD_NILL64i: 7429 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true); 7430 case SystemZ::ATOMIC_LOAD_NILH64i: 7431 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true); 7432 case SystemZ::ATOMIC_LOAD_NIHL64i: 7433 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true); 7434 case SystemZ::ATOMIC_LOAD_NIHH64i: 7435 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true); 7436 case SystemZ::ATOMIC_LOAD_NILF64i: 7437 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true); 7438 case SystemZ::ATOMIC_LOAD_NIHF64i: 7439 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true); 7440 7441 case SystemZ::ATOMIC_LOADW_MIN: 7442 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 7443 SystemZ::CCMASK_CMP_LE, 0); 7444 case SystemZ::ATOMIC_LOAD_MIN_32: 7445 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 7446 SystemZ::CCMASK_CMP_LE, 32); 7447 case SystemZ::ATOMIC_LOAD_MIN_64: 7448 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, 7449 SystemZ::CCMASK_CMP_LE, 64); 7450 7451 case SystemZ::ATOMIC_LOADW_MAX: 7452 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 7453 SystemZ::CCMASK_CMP_GE, 0); 7454 case SystemZ::ATOMIC_LOAD_MAX_32: 7455 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 7456 SystemZ::CCMASK_CMP_GE, 32); 7457 case SystemZ::ATOMIC_LOAD_MAX_64: 7458 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, 7459 SystemZ::CCMASK_CMP_GE, 64); 7460 7461 case SystemZ::ATOMIC_LOADW_UMIN: 7462 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 7463 SystemZ::CCMASK_CMP_LE, 0); 7464 case SystemZ::ATOMIC_LOAD_UMIN_32: 7465 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 7466 SystemZ::CCMASK_CMP_LE, 32); 7467 case SystemZ::ATOMIC_LOAD_UMIN_64: 7468 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, 7469 SystemZ::CCMASK_CMP_LE, 64); 7470 7471 case SystemZ::ATOMIC_LOADW_UMAX: 7472 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 7473 SystemZ::CCMASK_CMP_GE, 0); 7474 case SystemZ::ATOMIC_LOAD_UMAX_32: 7475 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 7476 SystemZ::CCMASK_CMP_GE, 32); 7477 case SystemZ::ATOMIC_LOAD_UMAX_64: 7478 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, 7479 SystemZ::CCMASK_CMP_GE, 64); 7480 7481 case SystemZ::ATOMIC_CMP_SWAPW: 7482 return emitAtomicCmpSwapW(MI, MBB); 7483 case SystemZ::MVCSequence: 7484 case SystemZ::MVCLoop: 7485 return emitMemMemWrapper(MI, MBB, SystemZ::MVC); 7486 case SystemZ::NCSequence: 7487 case SystemZ::NCLoop: 7488 return emitMemMemWrapper(MI, MBB, SystemZ::NC); 7489 case SystemZ::OCSequence: 7490 case SystemZ::OCLoop: 7491 return emitMemMemWrapper(MI, MBB, SystemZ::OC); 7492 case SystemZ::XCSequence: 7493 case SystemZ::XCLoop: 7494 return emitMemMemWrapper(MI, MBB, SystemZ::XC); 7495 case SystemZ::CLCSequence: 7496 case SystemZ::CLCLoop: 7497 return emitMemMemWrapper(MI, MBB, SystemZ::CLC); 7498 case SystemZ::CLSTLoop: 7499 return emitStringWrapper(MI, MBB, SystemZ::CLST); 7500 case SystemZ::MVSTLoop: 7501 return emitStringWrapper(MI, MBB, SystemZ::MVST); 7502 case SystemZ::SRSTLoop: 7503 return emitStringWrapper(MI, MBB, SystemZ::SRST); 7504 case SystemZ::TBEGIN: 7505 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false); 7506 case SystemZ::TBEGIN_nofloat: 7507 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true); 7508 case SystemZ::TBEGINC: 7509 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true); 7510 case SystemZ::LTEBRCompare_VecPseudo: 7511 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR); 7512 case SystemZ::LTDBRCompare_VecPseudo: 7513 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR); 7514 case SystemZ::LTXBRCompare_VecPseudo: 7515 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR); 7516 7517 case TargetOpcode::STACKMAP: 7518 case TargetOpcode::PATCHPOINT: 7519 return emitPatchPoint(MI, MBB); 7520 7521 default: 7522 llvm_unreachable("Unexpected instr type to insert"); 7523 } 7524 } 7525 7526 // This is only used by the isel schedulers, and is needed only to prevent 7527 // compiler from crashing when list-ilp is used. 7528 const TargetRegisterClass * 7529 SystemZTargetLowering::getRepRegClassFor(MVT VT) const { 7530 if (VT == MVT::Untyped) 7531 return &SystemZ::ADDR128BitRegClass; 7532 return TargetLowering::getRepRegClassFor(VT); 7533 } 7534