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