1 //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===// 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 implements the TargetLowering class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/CodeGen/TargetLowering.h" 14 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/CodeGen/CallingConvLower.h" 16 #include "llvm/CodeGen/MachineFrameInfo.h" 17 #include "llvm/CodeGen/MachineFunction.h" 18 #include "llvm/CodeGen/MachineJumpTableInfo.h" 19 #include "llvm/CodeGen/MachineRegisterInfo.h" 20 #include "llvm/CodeGen/SelectionDAG.h" 21 #include "llvm/CodeGen/TargetRegisterInfo.h" 22 #include "llvm/CodeGen/TargetSubtargetInfo.h" 23 #include "llvm/IR/DataLayout.h" 24 #include "llvm/IR/DerivedTypes.h" 25 #include "llvm/IR/GlobalVariable.h" 26 #include "llvm/IR/LLVMContext.h" 27 #include "llvm/MC/MCAsmInfo.h" 28 #include "llvm/MC/MCExpr.h" 29 #include "llvm/Support/ErrorHandling.h" 30 #include "llvm/Support/KnownBits.h" 31 #include "llvm/Support/MathExtras.h" 32 #include "llvm/Target/TargetLoweringObjectFile.h" 33 #include "llvm/Target/TargetMachine.h" 34 #include <cctype> 35 using namespace llvm; 36 37 /// NOTE: The TargetMachine owns TLOF. 38 TargetLowering::TargetLowering(const TargetMachine &tm) 39 : TargetLoweringBase(tm) {} 40 41 const char *TargetLowering::getTargetNodeName(unsigned Opcode) const { 42 return nullptr; 43 } 44 45 bool TargetLowering::isPositionIndependent() const { 46 return getTargetMachine().isPositionIndependent(); 47 } 48 49 /// Check whether a given call node is in tail position within its function. If 50 /// so, it sets Chain to the input chain of the tail call. 51 bool TargetLowering::isInTailCallPosition(SelectionDAG &DAG, SDNode *Node, 52 SDValue &Chain) const { 53 const Function &F = DAG.getMachineFunction().getFunction(); 54 55 // First, check if tail calls have been disabled in this function. 56 if (F.getFnAttribute("disable-tail-calls").getValueAsString() == "true") 57 return false; 58 59 // Conservatively require the attributes of the call to match those of 60 // the return. Ignore NoAlias and NonNull because they don't affect the 61 // call sequence. 62 AttributeList CallerAttrs = F.getAttributes(); 63 if (AttrBuilder(CallerAttrs, AttributeList::ReturnIndex) 64 .removeAttribute(Attribute::NoAlias) 65 .removeAttribute(Attribute::NonNull) 66 .hasAttributes()) 67 return false; 68 69 // It's not safe to eliminate the sign / zero extension of the return value. 70 if (CallerAttrs.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt) || 71 CallerAttrs.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt)) 72 return false; 73 74 // Check if the only use is a function return node. 75 return isUsedByReturnOnly(Node, Chain); 76 } 77 78 bool TargetLowering::parametersInCSRMatch(const MachineRegisterInfo &MRI, 79 const uint32_t *CallerPreservedMask, 80 const SmallVectorImpl<CCValAssign> &ArgLocs, 81 const SmallVectorImpl<SDValue> &OutVals) const { 82 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { 83 const CCValAssign &ArgLoc = ArgLocs[I]; 84 if (!ArgLoc.isRegLoc()) 85 continue; 86 MCRegister Reg = ArgLoc.getLocReg(); 87 // Only look at callee saved registers. 88 if (MachineOperand::clobbersPhysReg(CallerPreservedMask, Reg)) 89 continue; 90 // Check that we pass the value used for the caller. 91 // (We look for a CopyFromReg reading a virtual register that is used 92 // for the function live-in value of register Reg) 93 SDValue Value = OutVals[I]; 94 if (Value->getOpcode() != ISD::CopyFromReg) 95 return false; 96 Register ArgReg = cast<RegisterSDNode>(Value->getOperand(1))->getReg(); 97 if (MRI.getLiveInPhysReg(ArgReg) != Reg) 98 return false; 99 } 100 return true; 101 } 102 103 /// Set CallLoweringInfo attribute flags based on a call instruction 104 /// and called function attributes. 105 void TargetLoweringBase::ArgListEntry::setAttributes(const CallBase *Call, 106 unsigned ArgIdx) { 107 IsSExt = Call->paramHasAttr(ArgIdx, Attribute::SExt); 108 IsZExt = Call->paramHasAttr(ArgIdx, Attribute::ZExt); 109 IsInReg = Call->paramHasAttr(ArgIdx, Attribute::InReg); 110 IsSRet = Call->paramHasAttr(ArgIdx, Attribute::StructRet); 111 IsNest = Call->paramHasAttr(ArgIdx, Attribute::Nest); 112 IsByVal = Call->paramHasAttr(ArgIdx, Attribute::ByVal); 113 IsPreallocated = Call->paramHasAttr(ArgIdx, Attribute::Preallocated); 114 IsInAlloca = Call->paramHasAttr(ArgIdx, Attribute::InAlloca); 115 IsReturned = Call->paramHasAttr(ArgIdx, Attribute::Returned); 116 IsSwiftSelf = Call->paramHasAttr(ArgIdx, Attribute::SwiftSelf); 117 IsSwiftError = Call->paramHasAttr(ArgIdx, Attribute::SwiftError); 118 Alignment = Call->getParamAlign(ArgIdx); 119 ByValType = nullptr; 120 if (IsByVal) 121 ByValType = Call->getParamByValType(ArgIdx); 122 PreallocatedType = nullptr; 123 if (IsPreallocated) 124 PreallocatedType = Call->getParamPreallocatedType(ArgIdx); 125 } 126 127 /// Generate a libcall taking the given operands as arguments and returning a 128 /// result of type RetVT. 129 std::pair<SDValue, SDValue> 130 TargetLowering::makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT, 131 ArrayRef<SDValue> Ops, 132 MakeLibCallOptions CallOptions, 133 const SDLoc &dl, 134 SDValue InChain) const { 135 if (!InChain) 136 InChain = DAG.getEntryNode(); 137 138 TargetLowering::ArgListTy Args; 139 Args.reserve(Ops.size()); 140 141 TargetLowering::ArgListEntry Entry; 142 for (unsigned i = 0; i < Ops.size(); ++i) { 143 SDValue NewOp = Ops[i]; 144 Entry.Node = NewOp; 145 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext()); 146 Entry.IsSExt = shouldSignExtendTypeInLibCall(NewOp.getValueType(), 147 CallOptions.IsSExt); 148 Entry.IsZExt = !Entry.IsSExt; 149 150 if (CallOptions.IsSoften && 151 !shouldExtendTypeInLibCall(CallOptions.OpsVTBeforeSoften[i])) { 152 Entry.IsSExt = Entry.IsZExt = false; 153 } 154 Args.push_back(Entry); 155 } 156 157 if (LC == RTLIB::UNKNOWN_LIBCALL) 158 report_fatal_error("Unsupported library call operation!"); 159 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 160 getPointerTy(DAG.getDataLayout())); 161 162 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 163 TargetLowering::CallLoweringInfo CLI(DAG); 164 bool signExtend = shouldSignExtendTypeInLibCall(RetVT, CallOptions.IsSExt); 165 bool zeroExtend = !signExtend; 166 167 if (CallOptions.IsSoften && 168 !shouldExtendTypeInLibCall(CallOptions.RetVTBeforeSoften)) { 169 signExtend = zeroExtend = false; 170 } 171 172 CLI.setDebugLoc(dl) 173 .setChain(InChain) 174 .setLibCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args)) 175 .setNoReturn(CallOptions.DoesNotReturn) 176 .setDiscardResult(!CallOptions.IsReturnValueUsed) 177 .setIsPostTypeLegalization(CallOptions.IsPostTypeLegalization) 178 .setSExtResult(signExtend) 179 .setZExtResult(zeroExtend); 180 return LowerCallTo(CLI); 181 } 182 183 bool TargetLowering::findOptimalMemOpLowering( 184 std::vector<EVT> &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, 185 unsigned SrcAS, const AttributeList &FuncAttributes) const { 186 if (Op.isMemcpyWithFixedDstAlign() && Op.getSrcAlign() < Op.getDstAlign()) 187 return false; 188 189 EVT VT = getOptimalMemOpType(Op, FuncAttributes); 190 191 if (VT == MVT::Other) { 192 // Use the largest integer type whose alignment constraints are satisfied. 193 // We only need to check DstAlign here as SrcAlign is always greater or 194 // equal to DstAlign (or zero). 195 VT = MVT::i64; 196 if (Op.isFixedDstAlign()) 197 while ( 198 Op.getDstAlign() < (VT.getSizeInBits() / 8) && 199 !allowsMisalignedMemoryAccesses(VT, DstAS, Op.getDstAlign().value())) 200 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1); 201 assert(VT.isInteger()); 202 203 // Find the largest legal integer type. 204 MVT LVT = MVT::i64; 205 while (!isTypeLegal(LVT)) 206 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1); 207 assert(LVT.isInteger()); 208 209 // If the type we've chosen is larger than the largest legal integer type 210 // then use that instead. 211 if (VT.bitsGT(LVT)) 212 VT = LVT; 213 } 214 215 unsigned NumMemOps = 0; 216 uint64_t Size = Op.size(); 217 while (Size) { 218 unsigned VTSize = VT.getSizeInBits() / 8; 219 while (VTSize > Size) { 220 // For now, only use non-vector load / store's for the left-over pieces. 221 EVT NewVT = VT; 222 unsigned NewVTSize; 223 224 bool Found = false; 225 if (VT.isVector() || VT.isFloatingPoint()) { 226 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32; 227 if (isOperationLegalOrCustom(ISD::STORE, NewVT) && 228 isSafeMemOpType(NewVT.getSimpleVT())) 229 Found = true; 230 else if (NewVT == MVT::i64 && 231 isOperationLegalOrCustom(ISD::STORE, MVT::f64) && 232 isSafeMemOpType(MVT::f64)) { 233 // i64 is usually not legal on 32-bit targets, but f64 may be. 234 NewVT = MVT::f64; 235 Found = true; 236 } 237 } 238 239 if (!Found) { 240 do { 241 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1); 242 if (NewVT == MVT::i8) 243 break; 244 } while (!isSafeMemOpType(NewVT.getSimpleVT())); 245 } 246 NewVTSize = NewVT.getSizeInBits() / 8; 247 248 // If the new VT cannot cover all of the remaining bits, then consider 249 // issuing a (or a pair of) unaligned and overlapping load / store. 250 bool Fast; 251 if (NumMemOps && Op.allowOverlap() && NewVTSize < Size && 252 allowsMisalignedMemoryAccesses( 253 VT, DstAS, Op.isFixedDstAlign() ? Op.getDstAlign().value() : 1, 254 MachineMemOperand::MONone, &Fast) && 255 Fast) 256 VTSize = Size; 257 else { 258 VT = NewVT; 259 VTSize = NewVTSize; 260 } 261 } 262 263 if (++NumMemOps > Limit) 264 return false; 265 266 MemOps.push_back(VT); 267 Size -= VTSize; 268 } 269 270 return true; 271 } 272 273 /// Soften the operands of a comparison. This code is shared among BR_CC, 274 /// SELECT_CC, and SETCC handlers. 275 void TargetLowering::softenSetCCOperands(SelectionDAG &DAG, EVT VT, 276 SDValue &NewLHS, SDValue &NewRHS, 277 ISD::CondCode &CCCode, 278 const SDLoc &dl, const SDValue OldLHS, 279 const SDValue OldRHS) const { 280 SDValue Chain; 281 return softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, dl, OldLHS, 282 OldRHS, Chain); 283 } 284 285 void TargetLowering::softenSetCCOperands(SelectionDAG &DAG, EVT VT, 286 SDValue &NewLHS, SDValue &NewRHS, 287 ISD::CondCode &CCCode, 288 const SDLoc &dl, const SDValue OldLHS, 289 const SDValue OldRHS, 290 SDValue &Chain, 291 bool IsSignaling) const { 292 // FIXME: Currently we cannot really respect all IEEE predicates due to libgcc 293 // not supporting it. We can update this code when libgcc provides such 294 // functions. 295 296 assert((VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128 || VT == MVT::ppcf128) 297 && "Unsupported setcc type!"); 298 299 // Expand into one or more soft-fp libcall(s). 300 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL; 301 bool ShouldInvertCC = false; 302 switch (CCCode) { 303 case ISD::SETEQ: 304 case ISD::SETOEQ: 305 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : 306 (VT == MVT::f64) ? RTLIB::OEQ_F64 : 307 (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128; 308 break; 309 case ISD::SETNE: 310 case ISD::SETUNE: 311 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : 312 (VT == MVT::f64) ? RTLIB::UNE_F64 : 313 (VT == MVT::f128) ? RTLIB::UNE_F128 : RTLIB::UNE_PPCF128; 314 break; 315 case ISD::SETGE: 316 case ISD::SETOGE: 317 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : 318 (VT == MVT::f64) ? RTLIB::OGE_F64 : 319 (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128; 320 break; 321 case ISD::SETLT: 322 case ISD::SETOLT: 323 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : 324 (VT == MVT::f64) ? RTLIB::OLT_F64 : 325 (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128; 326 break; 327 case ISD::SETLE: 328 case ISD::SETOLE: 329 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : 330 (VT == MVT::f64) ? RTLIB::OLE_F64 : 331 (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128; 332 break; 333 case ISD::SETGT: 334 case ISD::SETOGT: 335 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : 336 (VT == MVT::f64) ? RTLIB::OGT_F64 : 337 (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128; 338 break; 339 case ISD::SETO: 340 ShouldInvertCC = true; 341 LLVM_FALLTHROUGH; 342 case ISD::SETUO: 343 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : 344 (VT == MVT::f64) ? RTLIB::UO_F64 : 345 (VT == MVT::f128) ? RTLIB::UO_F128 : RTLIB::UO_PPCF128; 346 break; 347 case ISD::SETONE: 348 // SETONE = O && UNE 349 ShouldInvertCC = true; 350 LLVM_FALLTHROUGH; 351 case ISD::SETUEQ: 352 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : 353 (VT == MVT::f64) ? RTLIB::UO_F64 : 354 (VT == MVT::f128) ? RTLIB::UO_F128 : RTLIB::UO_PPCF128; 355 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : 356 (VT == MVT::f64) ? RTLIB::OEQ_F64 : 357 (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128; 358 break; 359 default: 360 // Invert CC for unordered comparisons 361 ShouldInvertCC = true; 362 switch (CCCode) { 363 case ISD::SETULT: 364 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : 365 (VT == MVT::f64) ? RTLIB::OGE_F64 : 366 (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128; 367 break; 368 case ISD::SETULE: 369 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : 370 (VT == MVT::f64) ? RTLIB::OGT_F64 : 371 (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128; 372 break; 373 case ISD::SETUGT: 374 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : 375 (VT == MVT::f64) ? RTLIB::OLE_F64 : 376 (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128; 377 break; 378 case ISD::SETUGE: 379 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : 380 (VT == MVT::f64) ? RTLIB::OLT_F64 : 381 (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128; 382 break; 383 default: llvm_unreachable("Do not know how to soften this setcc!"); 384 } 385 } 386 387 // Use the target specific return value for comparions lib calls. 388 EVT RetVT = getCmpLibcallReturnType(); 389 SDValue Ops[2] = {NewLHS, NewRHS}; 390 TargetLowering::MakeLibCallOptions CallOptions; 391 EVT OpsVT[2] = { OldLHS.getValueType(), 392 OldRHS.getValueType() }; 393 CallOptions.setTypeListBeforeSoften(OpsVT, RetVT, true); 394 auto Call = makeLibCall(DAG, LC1, RetVT, Ops, CallOptions, dl, Chain); 395 NewLHS = Call.first; 396 NewRHS = DAG.getConstant(0, dl, RetVT); 397 398 CCCode = getCmpLibcallCC(LC1); 399 if (ShouldInvertCC) { 400 assert(RetVT.isInteger()); 401 CCCode = getSetCCInverse(CCCode, RetVT); 402 } 403 404 if (LC2 == RTLIB::UNKNOWN_LIBCALL) { 405 // Update Chain. 406 Chain = Call.second; 407 } else { 408 EVT SetCCVT = 409 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), RetVT); 410 SDValue Tmp = DAG.getSetCC(dl, SetCCVT, NewLHS, NewRHS, CCCode); 411 auto Call2 = makeLibCall(DAG, LC2, RetVT, Ops, CallOptions, dl, Chain); 412 CCCode = getCmpLibcallCC(LC2); 413 if (ShouldInvertCC) 414 CCCode = getSetCCInverse(CCCode, RetVT); 415 NewLHS = DAG.getSetCC(dl, SetCCVT, Call2.first, NewRHS, CCCode); 416 if (Chain) 417 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Call.second, 418 Call2.second); 419 NewLHS = DAG.getNode(ShouldInvertCC ? ISD::AND : ISD::OR, dl, 420 Tmp.getValueType(), Tmp, NewLHS); 421 NewRHS = SDValue(); 422 } 423 } 424 425 /// Return the entry encoding for a jump table in the current function. The 426 /// returned value is a member of the MachineJumpTableInfo::JTEntryKind enum. 427 unsigned TargetLowering::getJumpTableEncoding() const { 428 // In non-pic modes, just use the address of a block. 429 if (!isPositionIndependent()) 430 return MachineJumpTableInfo::EK_BlockAddress; 431 432 // In PIC mode, if the target supports a GPRel32 directive, use it. 433 if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != nullptr) 434 return MachineJumpTableInfo::EK_GPRel32BlockAddress; 435 436 // Otherwise, use a label difference. 437 return MachineJumpTableInfo::EK_LabelDifference32; 438 } 439 440 SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table, 441 SelectionDAG &DAG) const { 442 // If our PIC model is GP relative, use the global offset table as the base. 443 unsigned JTEncoding = getJumpTableEncoding(); 444 445 if ((JTEncoding == MachineJumpTableInfo::EK_GPRel64BlockAddress) || 446 (JTEncoding == MachineJumpTableInfo::EK_GPRel32BlockAddress)) 447 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy(DAG.getDataLayout())); 448 449 return Table; 450 } 451 452 /// This returns the relocation base for the given PIC jumptable, the same as 453 /// getPICJumpTableRelocBase, but as an MCExpr. 454 const MCExpr * 455 TargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF, 456 unsigned JTI,MCContext &Ctx) const{ 457 // The normal PIC reloc base is the label at the start of the jump table. 458 return MCSymbolRefExpr::create(MF->getJTISymbol(JTI, Ctx), Ctx); 459 } 460 461 bool 462 TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 463 const TargetMachine &TM = getTargetMachine(); 464 const GlobalValue *GV = GA->getGlobal(); 465 466 // If the address is not even local to this DSO we will have to load it from 467 // a got and then add the offset. 468 if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV)) 469 return false; 470 471 // If the code is position independent we will have to add a base register. 472 if (isPositionIndependent()) 473 return false; 474 475 // Otherwise we can do it. 476 return true; 477 } 478 479 //===----------------------------------------------------------------------===// 480 // Optimization Methods 481 //===----------------------------------------------------------------------===// 482 483 /// If the specified instruction has a constant integer operand and there are 484 /// bits set in that constant that are not demanded, then clear those bits and 485 /// return true. 486 bool TargetLowering::ShrinkDemandedConstant(SDValue Op, 487 const APInt &DemandedBits, 488 const APInt &DemandedElts, 489 TargetLoweringOpt &TLO) const { 490 SDLoc DL(Op); 491 unsigned Opcode = Op.getOpcode(); 492 493 // Do target-specific constant optimization. 494 if (targetShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO)) 495 return TLO.New.getNode(); 496 497 // FIXME: ISD::SELECT, ISD::SELECT_CC 498 switch (Opcode) { 499 default: 500 break; 501 case ISD::XOR: 502 case ISD::AND: 503 case ISD::OR: { 504 auto *Op1C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 505 if (!Op1C) 506 return false; 507 508 // If this is a 'not' op, don't touch it because that's a canonical form. 509 const APInt &C = Op1C->getAPIntValue(); 510 if (Opcode == ISD::XOR && DemandedBits.isSubsetOf(C)) 511 return false; 512 513 if (!C.isSubsetOf(DemandedBits)) { 514 EVT VT = Op.getValueType(); 515 SDValue NewC = TLO.DAG.getConstant(DemandedBits & C, DL, VT); 516 SDValue NewOp = TLO.DAG.getNode(Opcode, DL, VT, Op.getOperand(0), NewC); 517 return TLO.CombineTo(Op, NewOp); 518 } 519 520 break; 521 } 522 } 523 524 return false; 525 } 526 527 bool TargetLowering::ShrinkDemandedConstant(SDValue Op, 528 const APInt &DemandedBits, 529 TargetLoweringOpt &TLO) const { 530 EVT VT = Op.getValueType(); 531 APInt DemandedElts = VT.isVector() 532 ? APInt::getAllOnesValue(VT.getVectorNumElements()) 533 : APInt(1, 1); 534 return ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO); 535 } 536 537 /// Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the casts are free. 538 /// This uses isZExtFree and ZERO_EXTEND for the widening cast, but it could be 539 /// generalized for targets with other types of implicit widening casts. 540 bool TargetLowering::ShrinkDemandedOp(SDValue Op, unsigned BitWidth, 541 const APInt &Demanded, 542 TargetLoweringOpt &TLO) const { 543 assert(Op.getNumOperands() == 2 && 544 "ShrinkDemandedOp only supports binary operators!"); 545 assert(Op.getNode()->getNumValues() == 1 && 546 "ShrinkDemandedOp only supports nodes with one result!"); 547 548 SelectionDAG &DAG = TLO.DAG; 549 SDLoc dl(Op); 550 551 // Early return, as this function cannot handle vector types. 552 if (Op.getValueType().isVector()) 553 return false; 554 555 // Don't do this if the node has another user, which may require the 556 // full value. 557 if (!Op.getNode()->hasOneUse()) 558 return false; 559 560 // Search for the smallest integer type with free casts to and from 561 // Op's type. For expedience, just check power-of-2 integer types. 562 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 563 unsigned DemandedSize = Demanded.getActiveBits(); 564 unsigned SmallVTBits = DemandedSize; 565 if (!isPowerOf2_32(SmallVTBits)) 566 SmallVTBits = NextPowerOf2(SmallVTBits); 567 for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) { 568 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits); 569 if (TLI.isTruncateFree(Op.getValueType(), SmallVT) && 570 TLI.isZExtFree(SmallVT, Op.getValueType())) { 571 // We found a type with free casts. 572 SDValue X = DAG.getNode( 573 Op.getOpcode(), dl, SmallVT, 574 DAG.getNode(ISD::TRUNCATE, dl, SmallVT, Op.getOperand(0)), 575 DAG.getNode(ISD::TRUNCATE, dl, SmallVT, Op.getOperand(1))); 576 assert(DemandedSize <= SmallVTBits && "Narrowed below demanded bits?"); 577 SDValue Z = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), X); 578 return TLO.CombineTo(Op, Z); 579 } 580 } 581 return false; 582 } 583 584 bool TargetLowering::SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits, 585 DAGCombinerInfo &DCI) const { 586 SelectionDAG &DAG = DCI.DAG; 587 TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 588 !DCI.isBeforeLegalizeOps()); 589 KnownBits Known; 590 591 bool Simplified = SimplifyDemandedBits(Op, DemandedBits, Known, TLO); 592 if (Simplified) { 593 DCI.AddToWorklist(Op.getNode()); 594 DCI.CommitTargetLoweringOpt(TLO); 595 } 596 return Simplified; 597 } 598 599 bool TargetLowering::SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits, 600 KnownBits &Known, 601 TargetLoweringOpt &TLO, 602 unsigned Depth, 603 bool AssumeSingleUse) const { 604 EVT VT = Op.getValueType(); 605 606 // TODO: We can probably do more work on calculating the known bits and 607 // simplifying the operations for scalable vectors, but for now we just 608 // bail out. 609 if (VT.isScalableVector()) { 610 // Pretend we don't know anything for now. 611 Known = KnownBits(DemandedBits.getBitWidth()); 612 return false; 613 } 614 615 APInt DemandedElts = VT.isVector() 616 ? APInt::getAllOnesValue(VT.getVectorNumElements()) 617 : APInt(1, 1); 618 return SimplifyDemandedBits(Op, DemandedBits, DemandedElts, Known, TLO, Depth, 619 AssumeSingleUse); 620 } 621 622 // TODO: Can we merge SelectionDAG::GetDemandedBits into this? 623 // TODO: Under what circumstances can we create nodes? Constant folding? 624 SDValue TargetLowering::SimplifyMultipleUseDemandedBits( 625 SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts, 626 SelectionDAG &DAG, unsigned Depth) const { 627 // Limit search depth. 628 if (Depth >= SelectionDAG::MaxRecursionDepth) 629 return SDValue(); 630 631 // Ignore UNDEFs. 632 if (Op.isUndef()) 633 return SDValue(); 634 635 // Not demanding any bits/elts from Op. 636 if (DemandedBits == 0 || DemandedElts == 0) 637 return DAG.getUNDEF(Op.getValueType()); 638 639 unsigned NumElts = DemandedElts.getBitWidth(); 640 unsigned BitWidth = DemandedBits.getBitWidth(); 641 KnownBits LHSKnown, RHSKnown; 642 switch (Op.getOpcode()) { 643 case ISD::BITCAST: { 644 SDValue Src = peekThroughBitcasts(Op.getOperand(0)); 645 EVT SrcVT = Src.getValueType(); 646 EVT DstVT = Op.getValueType(); 647 if (SrcVT == DstVT) 648 return Src; 649 650 unsigned NumSrcEltBits = SrcVT.getScalarSizeInBits(); 651 unsigned NumDstEltBits = DstVT.getScalarSizeInBits(); 652 if (NumSrcEltBits == NumDstEltBits) 653 if (SDValue V = SimplifyMultipleUseDemandedBits( 654 Src, DemandedBits, DemandedElts, DAG, Depth + 1)) 655 return DAG.getBitcast(DstVT, V); 656 657 // TODO - bigendian once we have test coverage. 658 if (SrcVT.isVector() && (NumDstEltBits % NumSrcEltBits) == 0 && 659 DAG.getDataLayout().isLittleEndian()) { 660 unsigned Scale = NumDstEltBits / NumSrcEltBits; 661 unsigned NumSrcElts = SrcVT.getVectorNumElements(); 662 APInt DemandedSrcBits = APInt::getNullValue(NumSrcEltBits); 663 APInt DemandedSrcElts = APInt::getNullValue(NumSrcElts); 664 for (unsigned i = 0; i != Scale; ++i) { 665 unsigned Offset = i * NumSrcEltBits; 666 APInt Sub = DemandedBits.extractBits(NumSrcEltBits, Offset); 667 if (!Sub.isNullValue()) { 668 DemandedSrcBits |= Sub; 669 for (unsigned j = 0; j != NumElts; ++j) 670 if (DemandedElts[j]) 671 DemandedSrcElts.setBit((j * Scale) + i); 672 } 673 } 674 675 if (SDValue V = SimplifyMultipleUseDemandedBits( 676 Src, DemandedSrcBits, DemandedSrcElts, DAG, Depth + 1)) 677 return DAG.getBitcast(DstVT, V); 678 } 679 680 // TODO - bigendian once we have test coverage. 681 if ((NumSrcEltBits % NumDstEltBits) == 0 && 682 DAG.getDataLayout().isLittleEndian()) { 683 unsigned Scale = NumSrcEltBits / NumDstEltBits; 684 unsigned NumSrcElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1; 685 APInt DemandedSrcBits = APInt::getNullValue(NumSrcEltBits); 686 APInt DemandedSrcElts = APInt::getNullValue(NumSrcElts); 687 for (unsigned i = 0; i != NumElts; ++i) 688 if (DemandedElts[i]) { 689 unsigned Offset = (i % Scale) * NumDstEltBits; 690 DemandedSrcBits.insertBits(DemandedBits, Offset); 691 DemandedSrcElts.setBit(i / Scale); 692 } 693 694 if (SDValue V = SimplifyMultipleUseDemandedBits( 695 Src, DemandedSrcBits, DemandedSrcElts, DAG, Depth + 1)) 696 return DAG.getBitcast(DstVT, V); 697 } 698 699 break; 700 } 701 case ISD::AND: { 702 LHSKnown = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); 703 RHSKnown = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1); 704 705 // If all of the demanded bits are known 1 on one side, return the other. 706 // These bits cannot contribute to the result of the 'and' in this 707 // context. 708 if (DemandedBits.isSubsetOf(LHSKnown.Zero | RHSKnown.One)) 709 return Op.getOperand(0); 710 if (DemandedBits.isSubsetOf(RHSKnown.Zero | LHSKnown.One)) 711 return Op.getOperand(1); 712 break; 713 } 714 case ISD::OR: { 715 LHSKnown = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); 716 RHSKnown = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1); 717 718 // If all of the demanded bits are known zero on one side, return the 719 // other. These bits cannot contribute to the result of the 'or' in this 720 // context. 721 if (DemandedBits.isSubsetOf(LHSKnown.One | RHSKnown.Zero)) 722 return Op.getOperand(0); 723 if (DemandedBits.isSubsetOf(RHSKnown.One | LHSKnown.Zero)) 724 return Op.getOperand(1); 725 break; 726 } 727 case ISD::XOR: { 728 LHSKnown = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); 729 RHSKnown = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1); 730 731 // If all of the demanded bits are known zero on one side, return the 732 // other. 733 if (DemandedBits.isSubsetOf(RHSKnown.Zero)) 734 return Op.getOperand(0); 735 if (DemandedBits.isSubsetOf(LHSKnown.Zero)) 736 return Op.getOperand(1); 737 break; 738 } 739 case ISD::SHL: { 740 // If we are only demanding sign bits then we can use the shift source 741 // directly. 742 if (const APInt *MaxSA = 743 DAG.getValidMaximumShiftAmountConstant(Op, DemandedElts)) { 744 SDValue Op0 = Op.getOperand(0); 745 unsigned ShAmt = MaxSA->getZExtValue(); 746 unsigned NumSignBits = 747 DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1); 748 unsigned UpperDemandedBits = BitWidth - DemandedBits.countTrailingZeros(); 749 if (NumSignBits > ShAmt && (NumSignBits - ShAmt) >= (UpperDemandedBits)) 750 return Op0; 751 } 752 break; 753 } 754 case ISD::SETCC: { 755 SDValue Op0 = Op.getOperand(0); 756 SDValue Op1 = Op.getOperand(1); 757 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 758 // If (1) we only need the sign-bit, (2) the setcc operands are the same 759 // width as the setcc result, and (3) the result of a setcc conforms to 0 or 760 // -1, we may be able to bypass the setcc. 761 if (DemandedBits.isSignMask() && 762 Op0.getScalarValueSizeInBits() == BitWidth && 763 getBooleanContents(Op0.getValueType()) == 764 BooleanContent::ZeroOrNegativeOneBooleanContent) { 765 // If we're testing X < 0, then this compare isn't needed - just use X! 766 // FIXME: We're limiting to integer types here, but this should also work 767 // if we don't care about FP signed-zero. The use of SETLT with FP means 768 // that we don't care about NaNs. 769 if (CC == ISD::SETLT && Op1.getValueType().isInteger() && 770 (isNullConstant(Op1) || ISD::isBuildVectorAllZeros(Op1.getNode()))) 771 return Op0; 772 } 773 break; 774 } 775 case ISD::SIGN_EXTEND_INREG: { 776 // If none of the extended bits are demanded, eliminate the sextinreg. 777 SDValue Op0 = Op.getOperand(0); 778 EVT ExVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 779 unsigned ExBits = ExVT.getScalarSizeInBits(); 780 if (DemandedBits.getActiveBits() <= ExBits) 781 return Op0; 782 // If the input is already sign extended, just drop the extension. 783 unsigned NumSignBits = DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1); 784 if (NumSignBits >= (BitWidth - ExBits + 1)) 785 return Op0; 786 break; 787 } 788 case ISD::ANY_EXTEND_VECTOR_INREG: 789 case ISD::SIGN_EXTEND_VECTOR_INREG: 790 case ISD::ZERO_EXTEND_VECTOR_INREG: { 791 // If we only want the lowest element and none of extended bits, then we can 792 // return the bitcasted source vector. 793 SDValue Src = Op.getOperand(0); 794 EVT SrcVT = Src.getValueType(); 795 EVT DstVT = Op.getValueType(); 796 if (DemandedElts == 1 && DstVT.getSizeInBits() == SrcVT.getSizeInBits() && 797 DAG.getDataLayout().isLittleEndian() && 798 DemandedBits.getActiveBits() <= SrcVT.getScalarSizeInBits()) { 799 return DAG.getBitcast(DstVT, Src); 800 } 801 break; 802 } 803 case ISD::INSERT_VECTOR_ELT: { 804 // If we don't demand the inserted element, return the base vector. 805 SDValue Vec = Op.getOperand(0); 806 auto *CIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 807 EVT VecVT = Vec.getValueType(); 808 if (CIdx && CIdx->getAPIntValue().ult(VecVT.getVectorNumElements()) && 809 !DemandedElts[CIdx->getZExtValue()]) 810 return Vec; 811 break; 812 } 813 case ISD::INSERT_SUBVECTOR: { 814 // If we don't demand the inserted subvector, return the base vector. 815 SDValue Vec = Op.getOperand(0); 816 SDValue Sub = Op.getOperand(1); 817 uint64_t Idx = Op.getConstantOperandVal(2); 818 unsigned NumSubElts = Sub.getValueType().getVectorNumElements(); 819 if (DemandedElts.extractBits(NumSubElts, Idx) == 0) 820 return Vec; 821 break; 822 } 823 case ISD::VECTOR_SHUFFLE: { 824 ArrayRef<int> ShuffleMask = cast<ShuffleVectorSDNode>(Op)->getMask(); 825 826 // If all the demanded elts are from one operand and are inline, 827 // then we can use the operand directly. 828 bool AllUndef = true, IdentityLHS = true, IdentityRHS = true; 829 for (unsigned i = 0; i != NumElts; ++i) { 830 int M = ShuffleMask[i]; 831 if (M < 0 || !DemandedElts[i]) 832 continue; 833 AllUndef = false; 834 IdentityLHS &= (M == (int)i); 835 IdentityRHS &= ((M - NumElts) == i); 836 } 837 838 if (AllUndef) 839 return DAG.getUNDEF(Op.getValueType()); 840 if (IdentityLHS) 841 return Op.getOperand(0); 842 if (IdentityRHS) 843 return Op.getOperand(1); 844 break; 845 } 846 default: 847 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) 848 if (SDValue V = SimplifyMultipleUseDemandedBitsForTargetNode( 849 Op, DemandedBits, DemandedElts, DAG, Depth)) 850 return V; 851 break; 852 } 853 return SDValue(); 854 } 855 856 SDValue TargetLowering::SimplifyMultipleUseDemandedBits( 857 SDValue Op, const APInt &DemandedBits, SelectionDAG &DAG, 858 unsigned Depth) const { 859 EVT VT = Op.getValueType(); 860 APInt DemandedElts = VT.isVector() 861 ? APInt::getAllOnesValue(VT.getVectorNumElements()) 862 : APInt(1, 1); 863 return SimplifyMultipleUseDemandedBits(Op, DemandedBits, DemandedElts, DAG, 864 Depth); 865 } 866 867 SDValue TargetLowering::SimplifyMultipleUseDemandedVectorElts( 868 SDValue Op, const APInt &DemandedElts, SelectionDAG &DAG, 869 unsigned Depth) const { 870 APInt DemandedBits = APInt::getAllOnesValue(Op.getScalarValueSizeInBits()); 871 return SimplifyMultipleUseDemandedBits(Op, DemandedBits, DemandedElts, DAG, 872 Depth); 873 } 874 875 /// Look at Op. At this point, we know that only the OriginalDemandedBits of the 876 /// result of Op are ever used downstream. If we can use this information to 877 /// simplify Op, create a new simplified DAG node and return true, returning the 878 /// original and new nodes in Old and New. Otherwise, analyze the expression and 879 /// return a mask of Known bits for the expression (used to simplify the 880 /// caller). The Known bits may only be accurate for those bits in the 881 /// OriginalDemandedBits and OriginalDemandedElts. 882 bool TargetLowering::SimplifyDemandedBits( 883 SDValue Op, const APInt &OriginalDemandedBits, 884 const APInt &OriginalDemandedElts, KnownBits &Known, TargetLoweringOpt &TLO, 885 unsigned Depth, bool AssumeSingleUse) const { 886 unsigned BitWidth = OriginalDemandedBits.getBitWidth(); 887 assert(Op.getScalarValueSizeInBits() == BitWidth && 888 "Mask size mismatches value type size!"); 889 890 // Don't know anything. 891 Known = KnownBits(BitWidth); 892 893 // TODO: We can probably do more work on calculating the known bits and 894 // simplifying the operations for scalable vectors, but for now we just 895 // bail out. 896 if (Op.getValueType().isScalableVector()) 897 return false; 898 899 unsigned NumElts = OriginalDemandedElts.getBitWidth(); 900 assert((!Op.getValueType().isVector() || 901 NumElts == Op.getValueType().getVectorNumElements()) && 902 "Unexpected vector size"); 903 904 APInt DemandedBits = OriginalDemandedBits; 905 APInt DemandedElts = OriginalDemandedElts; 906 SDLoc dl(Op); 907 auto &DL = TLO.DAG.getDataLayout(); 908 909 // Undef operand. 910 if (Op.isUndef()) 911 return false; 912 913 if (Op.getOpcode() == ISD::Constant) { 914 // We know all of the bits for a constant! 915 Known = KnownBits::makeConstant(cast<ConstantSDNode>(Op)->getAPIntValue()); 916 return false; 917 } 918 919 if (Op.getOpcode() == ISD::ConstantFP) { 920 // We know all of the bits for a floating point constant! 921 Known = KnownBits::makeConstant( 922 cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()); 923 return false; 924 } 925 926 // Other users may use these bits. 927 EVT VT = Op.getValueType(); 928 if (!Op.getNode()->hasOneUse() && !AssumeSingleUse) { 929 if (Depth != 0) { 930 // If not at the root, Just compute the Known bits to 931 // simplify things downstream. 932 Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth); 933 return false; 934 } 935 // If this is the root being simplified, allow it to have multiple uses, 936 // just set the DemandedBits/Elts to all bits. 937 DemandedBits = APInt::getAllOnesValue(BitWidth); 938 DemandedElts = APInt::getAllOnesValue(NumElts); 939 } else if (OriginalDemandedBits == 0 || OriginalDemandedElts == 0) { 940 // Not demanding any bits/elts from Op. 941 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT)); 942 } else if (Depth >= SelectionDAG::MaxRecursionDepth) { 943 // Limit search depth. 944 return false; 945 } 946 947 KnownBits Known2; 948 switch (Op.getOpcode()) { 949 case ISD::TargetConstant: 950 llvm_unreachable("Can't simplify this node"); 951 case ISD::SCALAR_TO_VECTOR: { 952 if (!DemandedElts[0]) 953 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT)); 954 955 KnownBits SrcKnown; 956 SDValue Src = Op.getOperand(0); 957 unsigned SrcBitWidth = Src.getScalarValueSizeInBits(); 958 APInt SrcDemandedBits = DemandedBits.zextOrSelf(SrcBitWidth); 959 if (SimplifyDemandedBits(Src, SrcDemandedBits, SrcKnown, TLO, Depth + 1)) 960 return true; 961 962 // Upper elements are undef, so only get the knownbits if we just demand 963 // the bottom element. 964 if (DemandedElts == 1) 965 Known = SrcKnown.anyextOrTrunc(BitWidth); 966 break; 967 } 968 case ISD::BUILD_VECTOR: 969 // Collect the known bits that are shared by every demanded element. 970 // TODO: Call SimplifyDemandedBits for non-constant demanded elements. 971 Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth); 972 return false; // Don't fall through, will infinitely loop. 973 case ISD::LOAD: { 974 LoadSDNode *LD = cast<LoadSDNode>(Op); 975 if (getTargetConstantFromLoad(LD)) { 976 Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth); 977 return false; // Don't fall through, will infinitely loop. 978 } else if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) { 979 // If this is a ZEXTLoad and we are looking at the loaded value. 980 EVT MemVT = LD->getMemoryVT(); 981 unsigned MemBits = MemVT.getScalarSizeInBits(); 982 Known.Zero.setBitsFrom(MemBits); 983 return false; // Don't fall through, will infinitely loop. 984 } 985 break; 986 } 987 case ISD::INSERT_VECTOR_ELT: { 988 SDValue Vec = Op.getOperand(0); 989 SDValue Scl = Op.getOperand(1); 990 auto *CIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 991 EVT VecVT = Vec.getValueType(); 992 993 // If index isn't constant, assume we need all vector elements AND the 994 // inserted element. 995 APInt DemandedVecElts(DemandedElts); 996 if (CIdx && CIdx->getAPIntValue().ult(VecVT.getVectorNumElements())) { 997 unsigned Idx = CIdx->getZExtValue(); 998 DemandedVecElts.clearBit(Idx); 999 1000 // Inserted element is not required. 1001 if (!DemandedElts[Idx]) 1002 return TLO.CombineTo(Op, Vec); 1003 } 1004 1005 KnownBits KnownScl; 1006 unsigned NumSclBits = Scl.getScalarValueSizeInBits(); 1007 APInt DemandedSclBits = DemandedBits.zextOrTrunc(NumSclBits); 1008 if (SimplifyDemandedBits(Scl, DemandedSclBits, KnownScl, TLO, Depth + 1)) 1009 return true; 1010 1011 Known = KnownScl.anyextOrTrunc(BitWidth); 1012 1013 KnownBits KnownVec; 1014 if (SimplifyDemandedBits(Vec, DemandedBits, DemandedVecElts, KnownVec, TLO, 1015 Depth + 1)) 1016 return true; 1017 1018 if (!!DemandedVecElts) 1019 Known = KnownBits::commonBits(Known, KnownVec); 1020 1021 return false; 1022 } 1023 case ISD::INSERT_SUBVECTOR: { 1024 // Demand any elements from the subvector and the remainder from the src its 1025 // inserted into. 1026 SDValue Src = Op.getOperand(0); 1027 SDValue Sub = Op.getOperand(1); 1028 uint64_t Idx = Op.getConstantOperandVal(2); 1029 unsigned NumSubElts = Sub.getValueType().getVectorNumElements(); 1030 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx); 1031 APInt DemandedSrcElts = DemandedElts; 1032 DemandedSrcElts.insertBits(APInt::getNullValue(NumSubElts), Idx); 1033 1034 KnownBits KnownSub, KnownSrc; 1035 if (SimplifyDemandedBits(Sub, DemandedBits, DemandedSubElts, KnownSub, TLO, 1036 Depth + 1)) 1037 return true; 1038 if (SimplifyDemandedBits(Src, DemandedBits, DemandedSrcElts, KnownSrc, TLO, 1039 Depth + 1)) 1040 return true; 1041 1042 Known.Zero.setAllBits(); 1043 Known.One.setAllBits(); 1044 if (!!DemandedSubElts) 1045 Known = KnownBits::commonBits(Known, KnownSub); 1046 if (!!DemandedSrcElts) 1047 Known = KnownBits::commonBits(Known, KnownSrc); 1048 1049 // Attempt to avoid multi-use src if we don't need anything from it. 1050 if (!DemandedBits.isAllOnesValue() || !DemandedSubElts.isAllOnesValue() || 1051 !DemandedSrcElts.isAllOnesValue()) { 1052 SDValue NewSub = SimplifyMultipleUseDemandedBits( 1053 Sub, DemandedBits, DemandedSubElts, TLO.DAG, Depth + 1); 1054 SDValue NewSrc = SimplifyMultipleUseDemandedBits( 1055 Src, DemandedBits, DemandedSrcElts, TLO.DAG, Depth + 1); 1056 if (NewSub || NewSrc) { 1057 NewSub = NewSub ? NewSub : Sub; 1058 NewSrc = NewSrc ? NewSrc : Src; 1059 SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, NewSrc, NewSub, 1060 Op.getOperand(2)); 1061 return TLO.CombineTo(Op, NewOp); 1062 } 1063 } 1064 break; 1065 } 1066 case ISD::EXTRACT_SUBVECTOR: { 1067 // Offset the demanded elts by the subvector index. 1068 SDValue Src = Op.getOperand(0); 1069 if (Src.getValueType().isScalableVector()) 1070 break; 1071 uint64_t Idx = Op.getConstantOperandVal(1); 1072 unsigned NumSrcElts = Src.getValueType().getVectorNumElements(); 1073 APInt DemandedSrcElts = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx); 1074 1075 if (SimplifyDemandedBits(Src, DemandedBits, DemandedSrcElts, Known, TLO, 1076 Depth + 1)) 1077 return true; 1078 1079 // Attempt to avoid multi-use src if we don't need anything from it. 1080 if (!DemandedBits.isAllOnesValue() || !DemandedSrcElts.isAllOnesValue()) { 1081 SDValue DemandedSrc = SimplifyMultipleUseDemandedBits( 1082 Src, DemandedBits, DemandedSrcElts, TLO.DAG, Depth + 1); 1083 if (DemandedSrc) { 1084 SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, DemandedSrc, 1085 Op.getOperand(1)); 1086 return TLO.CombineTo(Op, NewOp); 1087 } 1088 } 1089 break; 1090 } 1091 case ISD::CONCAT_VECTORS: { 1092 Known.Zero.setAllBits(); 1093 Known.One.setAllBits(); 1094 EVT SubVT = Op.getOperand(0).getValueType(); 1095 unsigned NumSubVecs = Op.getNumOperands(); 1096 unsigned NumSubElts = SubVT.getVectorNumElements(); 1097 for (unsigned i = 0; i != NumSubVecs; ++i) { 1098 APInt DemandedSubElts = 1099 DemandedElts.extractBits(NumSubElts, i * NumSubElts); 1100 if (SimplifyDemandedBits(Op.getOperand(i), DemandedBits, DemandedSubElts, 1101 Known2, TLO, Depth + 1)) 1102 return true; 1103 // Known bits are shared by every demanded subvector element. 1104 if (!!DemandedSubElts) 1105 Known = KnownBits::commonBits(Known, Known2); 1106 } 1107 break; 1108 } 1109 case ISD::VECTOR_SHUFFLE: { 1110 ArrayRef<int> ShuffleMask = cast<ShuffleVectorSDNode>(Op)->getMask(); 1111 1112 // Collect demanded elements from shuffle operands.. 1113 APInt DemandedLHS(NumElts, 0); 1114 APInt DemandedRHS(NumElts, 0); 1115 for (unsigned i = 0; i != NumElts; ++i) { 1116 if (!DemandedElts[i]) 1117 continue; 1118 int M = ShuffleMask[i]; 1119 if (M < 0) { 1120 // For UNDEF elements, we don't know anything about the common state of 1121 // the shuffle result. 1122 DemandedLHS.clearAllBits(); 1123 DemandedRHS.clearAllBits(); 1124 break; 1125 } 1126 assert(0 <= M && M < (int)(2 * NumElts) && "Shuffle index out of range"); 1127 if (M < (int)NumElts) 1128 DemandedLHS.setBit(M); 1129 else 1130 DemandedRHS.setBit(M - NumElts); 1131 } 1132 1133 if (!!DemandedLHS || !!DemandedRHS) { 1134 SDValue Op0 = Op.getOperand(0); 1135 SDValue Op1 = Op.getOperand(1); 1136 1137 Known.Zero.setAllBits(); 1138 Known.One.setAllBits(); 1139 if (!!DemandedLHS) { 1140 if (SimplifyDemandedBits(Op0, DemandedBits, DemandedLHS, Known2, TLO, 1141 Depth + 1)) 1142 return true; 1143 Known = KnownBits::commonBits(Known, Known2); 1144 } 1145 if (!!DemandedRHS) { 1146 if (SimplifyDemandedBits(Op1, DemandedBits, DemandedRHS, Known2, TLO, 1147 Depth + 1)) 1148 return true; 1149 Known = KnownBits::commonBits(Known, Known2); 1150 } 1151 1152 // Attempt to avoid multi-use ops if we don't need anything from them. 1153 SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits( 1154 Op0, DemandedBits, DemandedLHS, TLO.DAG, Depth + 1); 1155 SDValue DemandedOp1 = SimplifyMultipleUseDemandedBits( 1156 Op1, DemandedBits, DemandedRHS, TLO.DAG, Depth + 1); 1157 if (DemandedOp0 || DemandedOp1) { 1158 Op0 = DemandedOp0 ? DemandedOp0 : Op0; 1159 Op1 = DemandedOp1 ? DemandedOp1 : Op1; 1160 SDValue NewOp = TLO.DAG.getVectorShuffle(VT, dl, Op0, Op1, ShuffleMask); 1161 return TLO.CombineTo(Op, NewOp); 1162 } 1163 } 1164 break; 1165 } 1166 case ISD::AND: { 1167 SDValue Op0 = Op.getOperand(0); 1168 SDValue Op1 = Op.getOperand(1); 1169 1170 // If the RHS is a constant, check to see if the LHS would be zero without 1171 // using the bits from the RHS. Below, we use knowledge about the RHS to 1172 // simplify the LHS, here we're using information from the LHS to simplify 1173 // the RHS. 1174 if (ConstantSDNode *RHSC = isConstOrConstSplat(Op1)) { 1175 // Do not increment Depth here; that can cause an infinite loop. 1176 KnownBits LHSKnown = TLO.DAG.computeKnownBits(Op0, DemandedElts, Depth); 1177 // If the LHS already has zeros where RHSC does, this 'and' is dead. 1178 if ((LHSKnown.Zero & DemandedBits) == 1179 (~RHSC->getAPIntValue() & DemandedBits)) 1180 return TLO.CombineTo(Op, Op0); 1181 1182 // If any of the set bits in the RHS are known zero on the LHS, shrink 1183 // the constant. 1184 if (ShrinkDemandedConstant(Op, ~LHSKnown.Zero & DemandedBits, 1185 DemandedElts, TLO)) 1186 return true; 1187 1188 // Bitwise-not (xor X, -1) is a special case: we don't usually shrink its 1189 // constant, but if this 'and' is only clearing bits that were just set by 1190 // the xor, then this 'and' can be eliminated by shrinking the mask of 1191 // the xor. For example, for a 32-bit X: 1192 // and (xor (srl X, 31), -1), 1 --> xor (srl X, 31), 1 1193 if (isBitwiseNot(Op0) && Op0.hasOneUse() && 1194 LHSKnown.One == ~RHSC->getAPIntValue()) { 1195 SDValue Xor = TLO.DAG.getNode(ISD::XOR, dl, VT, Op0.getOperand(0), Op1); 1196 return TLO.CombineTo(Op, Xor); 1197 } 1198 } 1199 1200 if (SimplifyDemandedBits(Op1, DemandedBits, DemandedElts, Known, TLO, 1201 Depth + 1)) 1202 return true; 1203 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 1204 if (SimplifyDemandedBits(Op0, ~Known.Zero & DemandedBits, DemandedElts, 1205 Known2, TLO, Depth + 1)) 1206 return true; 1207 assert(!Known2.hasConflict() && "Bits known to be one AND zero?"); 1208 1209 // Attempt to avoid multi-use ops if we don't need anything from them. 1210 if (!DemandedBits.isAllOnesValue() || !DemandedElts.isAllOnesValue()) { 1211 SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits( 1212 Op0, DemandedBits, DemandedElts, TLO.DAG, Depth + 1); 1213 SDValue DemandedOp1 = SimplifyMultipleUseDemandedBits( 1214 Op1, DemandedBits, DemandedElts, TLO.DAG, Depth + 1); 1215 if (DemandedOp0 || DemandedOp1) { 1216 Op0 = DemandedOp0 ? DemandedOp0 : Op0; 1217 Op1 = DemandedOp1 ? DemandedOp1 : Op1; 1218 SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Op1); 1219 return TLO.CombineTo(Op, NewOp); 1220 } 1221 } 1222 1223 // If all of the demanded bits are known one on one side, return the other. 1224 // These bits cannot contribute to the result of the 'and'. 1225 if (DemandedBits.isSubsetOf(Known2.Zero | Known.One)) 1226 return TLO.CombineTo(Op, Op0); 1227 if (DemandedBits.isSubsetOf(Known.Zero | Known2.One)) 1228 return TLO.CombineTo(Op, Op1); 1229 // If all of the demanded bits in the inputs are known zeros, return zero. 1230 if (DemandedBits.isSubsetOf(Known.Zero | Known2.Zero)) 1231 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, dl, VT)); 1232 // If the RHS is a constant, see if we can simplify it. 1233 if (ShrinkDemandedConstant(Op, ~Known2.Zero & DemandedBits, DemandedElts, 1234 TLO)) 1235 return true; 1236 // If the operation can be done in a smaller type, do so. 1237 if (ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO)) 1238 return true; 1239 1240 Known &= Known2; 1241 break; 1242 } 1243 case ISD::OR: { 1244 SDValue Op0 = Op.getOperand(0); 1245 SDValue Op1 = Op.getOperand(1); 1246 1247 if (SimplifyDemandedBits(Op1, DemandedBits, DemandedElts, Known, TLO, 1248 Depth + 1)) 1249 return true; 1250 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 1251 if (SimplifyDemandedBits(Op0, ~Known.One & DemandedBits, DemandedElts, 1252 Known2, TLO, Depth + 1)) 1253 return true; 1254 assert(!Known2.hasConflict() && "Bits known to be one AND zero?"); 1255 1256 // Attempt to avoid multi-use ops if we don't need anything from them. 1257 if (!DemandedBits.isAllOnesValue() || !DemandedElts.isAllOnesValue()) { 1258 SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits( 1259 Op0, DemandedBits, DemandedElts, TLO.DAG, Depth + 1); 1260 SDValue DemandedOp1 = SimplifyMultipleUseDemandedBits( 1261 Op1, DemandedBits, DemandedElts, TLO.DAG, Depth + 1); 1262 if (DemandedOp0 || DemandedOp1) { 1263 Op0 = DemandedOp0 ? DemandedOp0 : Op0; 1264 Op1 = DemandedOp1 ? DemandedOp1 : Op1; 1265 SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Op1); 1266 return TLO.CombineTo(Op, NewOp); 1267 } 1268 } 1269 1270 // If all of the demanded bits are known zero on one side, return the other. 1271 // These bits cannot contribute to the result of the 'or'. 1272 if (DemandedBits.isSubsetOf(Known2.One | Known.Zero)) 1273 return TLO.CombineTo(Op, Op0); 1274 if (DemandedBits.isSubsetOf(Known.One | Known2.Zero)) 1275 return TLO.CombineTo(Op, Op1); 1276 // If the RHS is a constant, see if we can simplify it. 1277 if (ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO)) 1278 return true; 1279 // If the operation can be done in a smaller type, do so. 1280 if (ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO)) 1281 return true; 1282 1283 Known |= Known2; 1284 break; 1285 } 1286 case ISD::XOR: { 1287 SDValue Op0 = Op.getOperand(0); 1288 SDValue Op1 = Op.getOperand(1); 1289 1290 if (SimplifyDemandedBits(Op1, DemandedBits, DemandedElts, Known, TLO, 1291 Depth + 1)) 1292 return true; 1293 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 1294 if (SimplifyDemandedBits(Op0, DemandedBits, DemandedElts, Known2, TLO, 1295 Depth + 1)) 1296 return true; 1297 assert(!Known2.hasConflict() && "Bits known to be one AND zero?"); 1298 1299 // Attempt to avoid multi-use ops if we don't need anything from them. 1300 if (!DemandedBits.isAllOnesValue() || !DemandedElts.isAllOnesValue()) { 1301 SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits( 1302 Op0, DemandedBits, DemandedElts, TLO.DAG, Depth + 1); 1303 SDValue DemandedOp1 = SimplifyMultipleUseDemandedBits( 1304 Op1, DemandedBits, DemandedElts, TLO.DAG, Depth + 1); 1305 if (DemandedOp0 || DemandedOp1) { 1306 Op0 = DemandedOp0 ? DemandedOp0 : Op0; 1307 Op1 = DemandedOp1 ? DemandedOp1 : Op1; 1308 SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Op1); 1309 return TLO.CombineTo(Op, NewOp); 1310 } 1311 } 1312 1313 // If all of the demanded bits are known zero on one side, return the other. 1314 // These bits cannot contribute to the result of the 'xor'. 1315 if (DemandedBits.isSubsetOf(Known.Zero)) 1316 return TLO.CombineTo(Op, Op0); 1317 if (DemandedBits.isSubsetOf(Known2.Zero)) 1318 return TLO.CombineTo(Op, Op1); 1319 // If the operation can be done in a smaller type, do so. 1320 if (ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO)) 1321 return true; 1322 1323 // If all of the unknown bits are known to be zero on one side or the other 1324 // turn this into an *inclusive* or. 1325 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 1326 if (DemandedBits.isSubsetOf(Known.Zero | Known2.Zero)) 1327 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, VT, Op0, Op1)); 1328 1329 ConstantSDNode* C = isConstOrConstSplat(Op1, DemandedElts); 1330 if (C) { 1331 // If one side is a constant, and all of the set bits in the constant are 1332 // also known set on the other side, turn this into an AND, as we know 1333 // the bits will be cleared. 1334 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 1335 // NB: it is okay if more bits are known than are requested 1336 if (C->getAPIntValue() == Known2.One) { 1337 SDValue ANDC = 1338 TLO.DAG.getConstant(~C->getAPIntValue() & DemandedBits, dl, VT); 1339 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT, Op0, ANDC)); 1340 } 1341 1342 // If the RHS is a constant, see if we can change it. Don't alter a -1 1343 // constant because that's a 'not' op, and that is better for combining 1344 // and codegen. 1345 if (!C->isAllOnesValue() && 1346 DemandedBits.isSubsetOf(C->getAPIntValue())) { 1347 // We're flipping all demanded bits. Flip the undemanded bits too. 1348 SDValue New = TLO.DAG.getNOT(dl, Op0, VT); 1349 return TLO.CombineTo(Op, New); 1350 } 1351 } 1352 1353 // If we can't turn this into a 'not', try to shrink the constant. 1354 if (!C || !C->isAllOnesValue()) 1355 if (ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO)) 1356 return true; 1357 1358 Known ^= Known2; 1359 break; 1360 } 1361 case ISD::SELECT: 1362 if (SimplifyDemandedBits(Op.getOperand(2), DemandedBits, Known, TLO, 1363 Depth + 1)) 1364 return true; 1365 if (SimplifyDemandedBits(Op.getOperand(1), DemandedBits, Known2, TLO, 1366 Depth + 1)) 1367 return true; 1368 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 1369 assert(!Known2.hasConflict() && "Bits known to be one AND zero?"); 1370 1371 // If the operands are constants, see if we can simplify them. 1372 if (ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO)) 1373 return true; 1374 1375 // Only known if known in both the LHS and RHS. 1376 Known = KnownBits::commonBits(Known, Known2); 1377 break; 1378 case ISD::SELECT_CC: 1379 if (SimplifyDemandedBits(Op.getOperand(3), DemandedBits, Known, TLO, 1380 Depth + 1)) 1381 return true; 1382 if (SimplifyDemandedBits(Op.getOperand(2), DemandedBits, Known2, TLO, 1383 Depth + 1)) 1384 return true; 1385 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 1386 assert(!Known2.hasConflict() && "Bits known to be one AND zero?"); 1387 1388 // If the operands are constants, see if we can simplify them. 1389 if (ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO)) 1390 return true; 1391 1392 // Only known if known in both the LHS and RHS. 1393 Known = KnownBits::commonBits(Known, Known2); 1394 break; 1395 case ISD::SETCC: { 1396 SDValue Op0 = Op.getOperand(0); 1397 SDValue Op1 = Op.getOperand(1); 1398 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 1399 // If (1) we only need the sign-bit, (2) the setcc operands are the same 1400 // width as the setcc result, and (3) the result of a setcc conforms to 0 or 1401 // -1, we may be able to bypass the setcc. 1402 if (DemandedBits.isSignMask() && 1403 Op0.getScalarValueSizeInBits() == BitWidth && 1404 getBooleanContents(Op0.getValueType()) == 1405 BooleanContent::ZeroOrNegativeOneBooleanContent) { 1406 // If we're testing X < 0, then this compare isn't needed - just use X! 1407 // FIXME: We're limiting to integer types here, but this should also work 1408 // if we don't care about FP signed-zero. The use of SETLT with FP means 1409 // that we don't care about NaNs. 1410 if (CC == ISD::SETLT && Op1.getValueType().isInteger() && 1411 (isNullConstant(Op1) || ISD::isBuildVectorAllZeros(Op1.getNode()))) 1412 return TLO.CombineTo(Op, Op0); 1413 1414 // TODO: Should we check for other forms of sign-bit comparisons? 1415 // Examples: X <= -1, X >= 0 1416 } 1417 if (getBooleanContents(Op0.getValueType()) == 1418 TargetLowering::ZeroOrOneBooleanContent && 1419 BitWidth > 1) 1420 Known.Zero.setBitsFrom(1); 1421 break; 1422 } 1423 case ISD::SHL: { 1424 SDValue Op0 = Op.getOperand(0); 1425 SDValue Op1 = Op.getOperand(1); 1426 EVT ShiftVT = Op1.getValueType(); 1427 1428 if (const APInt *SA = 1429 TLO.DAG.getValidShiftAmountConstant(Op, DemandedElts)) { 1430 unsigned ShAmt = SA->getZExtValue(); 1431 if (ShAmt == 0) 1432 return TLO.CombineTo(Op, Op0); 1433 1434 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a 1435 // single shift. We can do this if the bottom bits (which are shifted 1436 // out) are never demanded. 1437 // TODO - support non-uniform vector amounts. 1438 if (Op0.getOpcode() == ISD::SRL) { 1439 if (!DemandedBits.intersects(APInt::getLowBitsSet(BitWidth, ShAmt))) { 1440 if (const APInt *SA2 = 1441 TLO.DAG.getValidShiftAmountConstant(Op0, DemandedElts)) { 1442 unsigned C1 = SA2->getZExtValue(); 1443 unsigned Opc = ISD::SHL; 1444 int Diff = ShAmt - C1; 1445 if (Diff < 0) { 1446 Diff = -Diff; 1447 Opc = ISD::SRL; 1448 } 1449 SDValue NewSA = TLO.DAG.getConstant(Diff, dl, ShiftVT); 1450 return TLO.CombineTo( 1451 Op, TLO.DAG.getNode(Opc, dl, VT, Op0.getOperand(0), NewSA)); 1452 } 1453 } 1454 } 1455 1456 // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits 1457 // are not demanded. This will likely allow the anyext to be folded away. 1458 // TODO - support non-uniform vector amounts. 1459 if (Op0.getOpcode() == ISD::ANY_EXTEND) { 1460 SDValue InnerOp = Op0.getOperand(0); 1461 EVT InnerVT = InnerOp.getValueType(); 1462 unsigned InnerBits = InnerVT.getScalarSizeInBits(); 1463 if (ShAmt < InnerBits && DemandedBits.getActiveBits() <= InnerBits && 1464 isTypeDesirableForOp(ISD::SHL, InnerVT)) { 1465 EVT ShTy = getShiftAmountTy(InnerVT, DL); 1466 if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits())) 1467 ShTy = InnerVT; 1468 SDValue NarrowShl = 1469 TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp, 1470 TLO.DAG.getConstant(ShAmt, dl, ShTy)); 1471 return TLO.CombineTo( 1472 Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT, NarrowShl)); 1473 } 1474 1475 // Repeat the SHL optimization above in cases where an extension 1476 // intervenes: (shl (anyext (shr x, c1)), c2) to 1477 // (shl (anyext x), c2-c1). This requires that the bottom c1 bits 1478 // aren't demanded (as above) and that the shifted upper c1 bits of 1479 // x aren't demanded. 1480 // TODO - support non-uniform vector amounts. 1481 if (Op0.hasOneUse() && InnerOp.getOpcode() == ISD::SRL && 1482 InnerOp.hasOneUse()) { 1483 if (const APInt *SA2 = 1484 TLO.DAG.getValidShiftAmountConstant(InnerOp, DemandedElts)) { 1485 unsigned InnerShAmt = SA2->getZExtValue(); 1486 if (InnerShAmt < ShAmt && InnerShAmt < InnerBits && 1487 DemandedBits.getActiveBits() <= 1488 (InnerBits - InnerShAmt + ShAmt) && 1489 DemandedBits.countTrailingZeros() >= ShAmt) { 1490 SDValue NewSA = 1491 TLO.DAG.getConstant(ShAmt - InnerShAmt, dl, ShiftVT); 1492 SDValue NewExt = TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT, 1493 InnerOp.getOperand(0)); 1494 return TLO.CombineTo( 1495 Op, TLO.DAG.getNode(ISD::SHL, dl, VT, NewExt, NewSA)); 1496 } 1497 } 1498 } 1499 } 1500 1501 APInt InDemandedMask = DemandedBits.lshr(ShAmt); 1502 if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO, 1503 Depth + 1)) 1504 return true; 1505 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 1506 Known.Zero <<= ShAmt; 1507 Known.One <<= ShAmt; 1508 // low bits known zero. 1509 Known.Zero.setLowBits(ShAmt); 1510 1511 // Try shrinking the operation as long as the shift amount will still be 1512 // in range. 1513 if ((ShAmt < DemandedBits.getActiveBits()) && 1514 ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO)) 1515 return true; 1516 } 1517 1518 // If we are only demanding sign bits then we can use the shift source 1519 // directly. 1520 if (const APInt *MaxSA = 1521 TLO.DAG.getValidMaximumShiftAmountConstant(Op, DemandedElts)) { 1522 unsigned ShAmt = MaxSA->getZExtValue(); 1523 unsigned NumSignBits = 1524 TLO.DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1); 1525 unsigned UpperDemandedBits = BitWidth - DemandedBits.countTrailingZeros(); 1526 if (NumSignBits > ShAmt && (NumSignBits - ShAmt) >= (UpperDemandedBits)) 1527 return TLO.CombineTo(Op, Op0); 1528 } 1529 break; 1530 } 1531 case ISD::SRL: { 1532 SDValue Op0 = Op.getOperand(0); 1533 SDValue Op1 = Op.getOperand(1); 1534 EVT ShiftVT = Op1.getValueType(); 1535 1536 if (const APInt *SA = 1537 TLO.DAG.getValidShiftAmountConstant(Op, DemandedElts)) { 1538 unsigned ShAmt = SA->getZExtValue(); 1539 if (ShAmt == 0) 1540 return TLO.CombineTo(Op, Op0); 1541 1542 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a 1543 // single shift. We can do this if the top bits (which are shifted out) 1544 // are never demanded. 1545 // TODO - support non-uniform vector amounts. 1546 if (Op0.getOpcode() == ISD::SHL) { 1547 if (!DemandedBits.intersects(APInt::getHighBitsSet(BitWidth, ShAmt))) { 1548 if (const APInt *SA2 = 1549 TLO.DAG.getValidShiftAmountConstant(Op0, DemandedElts)) { 1550 unsigned C1 = SA2->getZExtValue(); 1551 unsigned Opc = ISD::SRL; 1552 int Diff = ShAmt - C1; 1553 if (Diff < 0) { 1554 Diff = -Diff; 1555 Opc = ISD::SHL; 1556 } 1557 SDValue NewSA = TLO.DAG.getConstant(Diff, dl, ShiftVT); 1558 return TLO.CombineTo( 1559 Op, TLO.DAG.getNode(Opc, dl, VT, Op0.getOperand(0), NewSA)); 1560 } 1561 } 1562 } 1563 1564 APInt InDemandedMask = (DemandedBits << ShAmt); 1565 1566 // If the shift is exact, then it does demand the low bits (and knows that 1567 // they are zero). 1568 if (Op->getFlags().hasExact()) 1569 InDemandedMask.setLowBits(ShAmt); 1570 1571 // Compute the new bits that are at the top now. 1572 if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO, 1573 Depth + 1)) 1574 return true; 1575 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 1576 Known.Zero.lshrInPlace(ShAmt); 1577 Known.One.lshrInPlace(ShAmt); 1578 // High bits known zero. 1579 Known.Zero.setHighBits(ShAmt); 1580 } 1581 break; 1582 } 1583 case ISD::SRA: { 1584 SDValue Op0 = Op.getOperand(0); 1585 SDValue Op1 = Op.getOperand(1); 1586 EVT ShiftVT = Op1.getValueType(); 1587 1588 // If we only want bits that already match the signbit then we don't need 1589 // to shift. 1590 unsigned NumHiDemandedBits = BitWidth - DemandedBits.countTrailingZeros(); 1591 if (TLO.DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1) >= 1592 NumHiDemandedBits) 1593 return TLO.CombineTo(Op, Op0); 1594 1595 // If this is an arithmetic shift right and only the low-bit is set, we can 1596 // always convert this into a logical shr, even if the shift amount is 1597 // variable. The low bit of the shift cannot be an input sign bit unless 1598 // the shift amount is >= the size of the datatype, which is undefined. 1599 if (DemandedBits.isOneValue()) 1600 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT, Op0, Op1)); 1601 1602 if (const APInt *SA = 1603 TLO.DAG.getValidShiftAmountConstant(Op, DemandedElts)) { 1604 unsigned ShAmt = SA->getZExtValue(); 1605 if (ShAmt == 0) 1606 return TLO.CombineTo(Op, Op0); 1607 1608 APInt InDemandedMask = (DemandedBits << ShAmt); 1609 1610 // If the shift is exact, then it does demand the low bits (and knows that 1611 // they are zero). 1612 if (Op->getFlags().hasExact()) 1613 InDemandedMask.setLowBits(ShAmt); 1614 1615 // If any of the demanded bits are produced by the sign extension, we also 1616 // demand the input sign bit. 1617 if (DemandedBits.countLeadingZeros() < ShAmt) 1618 InDemandedMask.setSignBit(); 1619 1620 if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO, 1621 Depth + 1)) 1622 return true; 1623 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 1624 Known.Zero.lshrInPlace(ShAmt); 1625 Known.One.lshrInPlace(ShAmt); 1626 1627 // If the input sign bit is known to be zero, or if none of the top bits 1628 // are demanded, turn this into an unsigned shift right. 1629 if (Known.Zero[BitWidth - ShAmt - 1] || 1630 DemandedBits.countLeadingZeros() >= ShAmt) { 1631 SDNodeFlags Flags; 1632 Flags.setExact(Op->getFlags().hasExact()); 1633 return TLO.CombineTo( 1634 Op, TLO.DAG.getNode(ISD::SRL, dl, VT, Op0, Op1, Flags)); 1635 } 1636 1637 int Log2 = DemandedBits.exactLogBase2(); 1638 if (Log2 >= 0) { 1639 // The bit must come from the sign. 1640 SDValue NewSA = TLO.DAG.getConstant(BitWidth - 1 - Log2, dl, ShiftVT); 1641 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT, Op0, NewSA)); 1642 } 1643 1644 if (Known.One[BitWidth - ShAmt - 1]) 1645 // New bits are known one. 1646 Known.One.setHighBits(ShAmt); 1647 1648 // Attempt to avoid multi-use ops if we don't need anything from them. 1649 if (!InDemandedMask.isAllOnesValue() || !DemandedElts.isAllOnesValue()) { 1650 SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits( 1651 Op0, InDemandedMask, DemandedElts, TLO.DAG, Depth + 1); 1652 if (DemandedOp0) { 1653 SDValue NewOp = TLO.DAG.getNode(ISD::SRA, dl, VT, DemandedOp0, Op1); 1654 return TLO.CombineTo(Op, NewOp); 1655 } 1656 } 1657 } 1658 break; 1659 } 1660 case ISD::FSHL: 1661 case ISD::FSHR: { 1662 SDValue Op0 = Op.getOperand(0); 1663 SDValue Op1 = Op.getOperand(1); 1664 SDValue Op2 = Op.getOperand(2); 1665 bool IsFSHL = (Op.getOpcode() == ISD::FSHL); 1666 1667 if (ConstantSDNode *SA = isConstOrConstSplat(Op2, DemandedElts)) { 1668 unsigned Amt = SA->getAPIntValue().urem(BitWidth); 1669 1670 // For fshl, 0-shift returns the 1st arg. 1671 // For fshr, 0-shift returns the 2nd arg. 1672 if (Amt == 0) { 1673 if (SimplifyDemandedBits(IsFSHL ? Op0 : Op1, DemandedBits, DemandedElts, 1674 Known, TLO, Depth + 1)) 1675 return true; 1676 break; 1677 } 1678 1679 // fshl: (Op0 << Amt) | (Op1 >> (BW - Amt)) 1680 // fshr: (Op0 << (BW - Amt)) | (Op1 >> Amt) 1681 APInt Demanded0 = DemandedBits.lshr(IsFSHL ? Amt : (BitWidth - Amt)); 1682 APInt Demanded1 = DemandedBits << (IsFSHL ? (BitWidth - Amt) : Amt); 1683 if (SimplifyDemandedBits(Op0, Demanded0, DemandedElts, Known2, TLO, 1684 Depth + 1)) 1685 return true; 1686 if (SimplifyDemandedBits(Op1, Demanded1, DemandedElts, Known, TLO, 1687 Depth + 1)) 1688 return true; 1689 1690 Known2.One <<= (IsFSHL ? Amt : (BitWidth - Amt)); 1691 Known2.Zero <<= (IsFSHL ? Amt : (BitWidth - Amt)); 1692 Known.One.lshrInPlace(IsFSHL ? (BitWidth - Amt) : Amt); 1693 Known.Zero.lshrInPlace(IsFSHL ? (BitWidth - Amt) : Amt); 1694 Known.One |= Known2.One; 1695 Known.Zero |= Known2.Zero; 1696 } 1697 1698 // For pow-2 bitwidths we only demand the bottom modulo amt bits. 1699 if (isPowerOf2_32(BitWidth)) { 1700 APInt DemandedAmtBits(Op2.getScalarValueSizeInBits(), BitWidth - 1); 1701 if (SimplifyDemandedBits(Op2, DemandedAmtBits, DemandedElts, 1702 Known2, TLO, Depth + 1)) 1703 return true; 1704 } 1705 break; 1706 } 1707 case ISD::ROTL: 1708 case ISD::ROTR: { 1709 SDValue Op0 = Op.getOperand(0); 1710 SDValue Op1 = Op.getOperand(1); 1711 1712 // If we're rotating an 0/-1 value, then it stays an 0/-1 value. 1713 if (BitWidth == TLO.DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1)) 1714 return TLO.CombineTo(Op, Op0); 1715 1716 // For pow-2 bitwidths we only demand the bottom modulo amt bits. 1717 if (isPowerOf2_32(BitWidth)) { 1718 APInt DemandedAmtBits(Op1.getScalarValueSizeInBits(), BitWidth - 1); 1719 if (SimplifyDemandedBits(Op1, DemandedAmtBits, DemandedElts, Known2, TLO, 1720 Depth + 1)) 1721 return true; 1722 } 1723 break; 1724 } 1725 case ISD::UMIN: { 1726 // Check if one arg is always less than (or equal) to the other arg. 1727 SDValue Op0 = Op.getOperand(0); 1728 SDValue Op1 = Op.getOperand(1); 1729 KnownBits Known0 = TLO.DAG.computeKnownBits(Op0, DemandedElts, Depth + 1); 1730 KnownBits Known1 = TLO.DAG.computeKnownBits(Op1, DemandedElts, Depth + 1); 1731 Known = KnownBits::umin(Known0, Known1); 1732 if (Optional<bool> IsULE = KnownBits::ule(Known0, Known1)) 1733 return TLO.CombineTo(Op, IsULE.getValue() ? Op0 : Op1); 1734 if (Optional<bool> IsULT = KnownBits::ult(Known0, Known1)) 1735 return TLO.CombineTo(Op, IsULT.getValue() ? Op0 : Op1); 1736 break; 1737 } 1738 case ISD::UMAX: { 1739 // Check if one arg is always greater than (or equal) to the other arg. 1740 SDValue Op0 = Op.getOperand(0); 1741 SDValue Op1 = Op.getOperand(1); 1742 KnownBits Known0 = TLO.DAG.computeKnownBits(Op0, DemandedElts, Depth + 1); 1743 KnownBits Known1 = TLO.DAG.computeKnownBits(Op1, DemandedElts, Depth + 1); 1744 Known = KnownBits::umax(Known0, Known1); 1745 if (Optional<bool> IsUGE = KnownBits::uge(Known0, Known1)) 1746 return TLO.CombineTo(Op, IsUGE.getValue() ? Op0 : Op1); 1747 if (Optional<bool> IsUGT = KnownBits::ugt(Known0, Known1)) 1748 return TLO.CombineTo(Op, IsUGT.getValue() ? Op0 : Op1); 1749 break; 1750 } 1751 case ISD::BITREVERSE: { 1752 SDValue Src = Op.getOperand(0); 1753 APInt DemandedSrcBits = DemandedBits.reverseBits(); 1754 if (SimplifyDemandedBits(Src, DemandedSrcBits, DemandedElts, Known2, TLO, 1755 Depth + 1)) 1756 return true; 1757 Known.One = Known2.One.reverseBits(); 1758 Known.Zero = Known2.Zero.reverseBits(); 1759 break; 1760 } 1761 case ISD::BSWAP: { 1762 SDValue Src = Op.getOperand(0); 1763 APInt DemandedSrcBits = DemandedBits.byteSwap(); 1764 if (SimplifyDemandedBits(Src, DemandedSrcBits, DemandedElts, Known2, TLO, 1765 Depth + 1)) 1766 return true; 1767 Known.One = Known2.One.byteSwap(); 1768 Known.Zero = Known2.Zero.byteSwap(); 1769 break; 1770 } 1771 case ISD::CTPOP: { 1772 // If only 1 bit is demanded, replace with PARITY as long as we're before 1773 // op legalization. 1774 // FIXME: Limit to scalars for now. 1775 if (DemandedBits.isOneValue() && !TLO.LegalOps && !VT.isVector()) 1776 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::PARITY, dl, VT, 1777 Op.getOperand(0))); 1778 1779 Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth); 1780 break; 1781 } 1782 case ISD::SIGN_EXTEND_INREG: { 1783 SDValue Op0 = Op.getOperand(0); 1784 EVT ExVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 1785 unsigned ExVTBits = ExVT.getScalarSizeInBits(); 1786 1787 // If we only care about the highest bit, don't bother shifting right. 1788 if (DemandedBits.isSignMask()) { 1789 unsigned NumSignBits = 1790 TLO.DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1); 1791 bool AlreadySignExtended = NumSignBits >= BitWidth - ExVTBits + 1; 1792 // However if the input is already sign extended we expect the sign 1793 // extension to be dropped altogether later and do not simplify. 1794 if (!AlreadySignExtended) { 1795 // Compute the correct shift amount type, which must be getShiftAmountTy 1796 // for scalar types after legalization. 1797 EVT ShiftAmtTy = VT; 1798 if (TLO.LegalTypes() && !ShiftAmtTy.isVector()) 1799 ShiftAmtTy = getShiftAmountTy(ShiftAmtTy, DL); 1800 1801 SDValue ShiftAmt = 1802 TLO.DAG.getConstant(BitWidth - ExVTBits, dl, ShiftAmtTy); 1803 return TLO.CombineTo(Op, 1804 TLO.DAG.getNode(ISD::SHL, dl, VT, Op0, ShiftAmt)); 1805 } 1806 } 1807 1808 // If none of the extended bits are demanded, eliminate the sextinreg. 1809 if (DemandedBits.getActiveBits() <= ExVTBits) 1810 return TLO.CombineTo(Op, Op0); 1811 1812 APInt InputDemandedBits = DemandedBits.getLoBits(ExVTBits); 1813 1814 // Since the sign extended bits are demanded, we know that the sign 1815 // bit is demanded. 1816 InputDemandedBits.setBit(ExVTBits - 1); 1817 1818 if (SimplifyDemandedBits(Op0, InputDemandedBits, Known, TLO, Depth + 1)) 1819 return true; 1820 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 1821 1822 // If the sign bit of the input is known set or clear, then we know the 1823 // top bits of the result. 1824 1825 // If the input sign bit is known zero, convert this into a zero extension. 1826 if (Known.Zero[ExVTBits - 1]) 1827 return TLO.CombineTo(Op, TLO.DAG.getZeroExtendInReg(Op0, dl, ExVT)); 1828 1829 APInt Mask = APInt::getLowBitsSet(BitWidth, ExVTBits); 1830 if (Known.One[ExVTBits - 1]) { // Input sign bit known set 1831 Known.One.setBitsFrom(ExVTBits); 1832 Known.Zero &= Mask; 1833 } else { // Input sign bit unknown 1834 Known.Zero &= Mask; 1835 Known.One &= Mask; 1836 } 1837 break; 1838 } 1839 case ISD::BUILD_PAIR: { 1840 EVT HalfVT = Op.getOperand(0).getValueType(); 1841 unsigned HalfBitWidth = HalfVT.getScalarSizeInBits(); 1842 1843 APInt MaskLo = DemandedBits.getLoBits(HalfBitWidth).trunc(HalfBitWidth); 1844 APInt MaskHi = DemandedBits.getHiBits(HalfBitWidth).trunc(HalfBitWidth); 1845 1846 KnownBits KnownLo, KnownHi; 1847 1848 if (SimplifyDemandedBits(Op.getOperand(0), MaskLo, KnownLo, TLO, Depth + 1)) 1849 return true; 1850 1851 if (SimplifyDemandedBits(Op.getOperand(1), MaskHi, KnownHi, TLO, Depth + 1)) 1852 return true; 1853 1854 Known.Zero = KnownLo.Zero.zext(BitWidth) | 1855 KnownHi.Zero.zext(BitWidth).shl(HalfBitWidth); 1856 1857 Known.One = KnownLo.One.zext(BitWidth) | 1858 KnownHi.One.zext(BitWidth).shl(HalfBitWidth); 1859 break; 1860 } 1861 case ISD::ZERO_EXTEND: 1862 case ISD::ZERO_EXTEND_VECTOR_INREG: { 1863 SDValue Src = Op.getOperand(0); 1864 EVT SrcVT = Src.getValueType(); 1865 unsigned InBits = SrcVT.getScalarSizeInBits(); 1866 unsigned InElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1; 1867 bool IsVecInReg = Op.getOpcode() == ISD::ZERO_EXTEND_VECTOR_INREG; 1868 1869 // If none of the top bits are demanded, convert this into an any_extend. 1870 if (DemandedBits.getActiveBits() <= InBits) { 1871 // If we only need the non-extended bits of the bottom element 1872 // then we can just bitcast to the result. 1873 if (IsVecInReg && DemandedElts == 1 && 1874 VT.getSizeInBits() == SrcVT.getSizeInBits() && 1875 TLO.DAG.getDataLayout().isLittleEndian()) 1876 return TLO.CombineTo(Op, TLO.DAG.getBitcast(VT, Src)); 1877 1878 unsigned Opc = 1879 IsVecInReg ? ISD::ANY_EXTEND_VECTOR_INREG : ISD::ANY_EXTEND; 1880 if (!TLO.LegalOperations() || isOperationLegal(Opc, VT)) 1881 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT, Src)); 1882 } 1883 1884 APInt InDemandedBits = DemandedBits.trunc(InBits); 1885 APInt InDemandedElts = DemandedElts.zextOrSelf(InElts); 1886 if (SimplifyDemandedBits(Src, InDemandedBits, InDemandedElts, Known, TLO, 1887 Depth + 1)) 1888 return true; 1889 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 1890 assert(Known.getBitWidth() == InBits && "Src width has changed?"); 1891 Known = Known.zext(BitWidth); 1892 1893 // Attempt to avoid multi-use ops if we don't need anything from them. 1894 if (SDValue NewSrc = SimplifyMultipleUseDemandedBits( 1895 Src, InDemandedBits, InDemandedElts, TLO.DAG, Depth + 1)) 1896 return TLO.CombineTo(Op, TLO.DAG.getNode(Op.getOpcode(), dl, VT, NewSrc)); 1897 break; 1898 } 1899 case ISD::SIGN_EXTEND: 1900 case ISD::SIGN_EXTEND_VECTOR_INREG: { 1901 SDValue Src = Op.getOperand(0); 1902 EVT SrcVT = Src.getValueType(); 1903 unsigned InBits = SrcVT.getScalarSizeInBits(); 1904 unsigned InElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1; 1905 bool IsVecInReg = Op.getOpcode() == ISD::SIGN_EXTEND_VECTOR_INREG; 1906 1907 // If none of the top bits are demanded, convert this into an any_extend. 1908 if (DemandedBits.getActiveBits() <= InBits) { 1909 // If we only need the non-extended bits of the bottom element 1910 // then we can just bitcast to the result. 1911 if (IsVecInReg && DemandedElts == 1 && 1912 VT.getSizeInBits() == SrcVT.getSizeInBits() && 1913 TLO.DAG.getDataLayout().isLittleEndian()) 1914 return TLO.CombineTo(Op, TLO.DAG.getBitcast(VT, Src)); 1915 1916 unsigned Opc = 1917 IsVecInReg ? ISD::ANY_EXTEND_VECTOR_INREG : ISD::ANY_EXTEND; 1918 if (!TLO.LegalOperations() || isOperationLegal(Opc, VT)) 1919 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT, Src)); 1920 } 1921 1922 APInt InDemandedBits = DemandedBits.trunc(InBits); 1923 APInt InDemandedElts = DemandedElts.zextOrSelf(InElts); 1924 1925 // Since some of the sign extended bits are demanded, we know that the sign 1926 // bit is demanded. 1927 InDemandedBits.setBit(InBits - 1); 1928 1929 if (SimplifyDemandedBits(Src, InDemandedBits, InDemandedElts, Known, TLO, 1930 Depth + 1)) 1931 return true; 1932 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 1933 assert(Known.getBitWidth() == InBits && "Src width has changed?"); 1934 1935 // If the sign bit is known one, the top bits match. 1936 Known = Known.sext(BitWidth); 1937 1938 // If the sign bit is known zero, convert this to a zero extend. 1939 if (Known.isNonNegative()) { 1940 unsigned Opc = 1941 IsVecInReg ? ISD::ZERO_EXTEND_VECTOR_INREG : ISD::ZERO_EXTEND; 1942 if (!TLO.LegalOperations() || isOperationLegal(Opc, VT)) 1943 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT, Src)); 1944 } 1945 1946 // Attempt to avoid multi-use ops if we don't need anything from them. 1947 if (SDValue NewSrc = SimplifyMultipleUseDemandedBits( 1948 Src, InDemandedBits, InDemandedElts, TLO.DAG, Depth + 1)) 1949 return TLO.CombineTo(Op, TLO.DAG.getNode(Op.getOpcode(), dl, VT, NewSrc)); 1950 break; 1951 } 1952 case ISD::ANY_EXTEND: 1953 case ISD::ANY_EXTEND_VECTOR_INREG: { 1954 SDValue Src = Op.getOperand(0); 1955 EVT SrcVT = Src.getValueType(); 1956 unsigned InBits = SrcVT.getScalarSizeInBits(); 1957 unsigned InElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1; 1958 bool IsVecInReg = Op.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG; 1959 1960 // If we only need the bottom element then we can just bitcast. 1961 // TODO: Handle ANY_EXTEND? 1962 if (IsVecInReg && DemandedElts == 1 && 1963 VT.getSizeInBits() == SrcVT.getSizeInBits() && 1964 TLO.DAG.getDataLayout().isLittleEndian()) 1965 return TLO.CombineTo(Op, TLO.DAG.getBitcast(VT, Src)); 1966 1967 APInt InDemandedBits = DemandedBits.trunc(InBits); 1968 APInt InDemandedElts = DemandedElts.zextOrSelf(InElts); 1969 if (SimplifyDemandedBits(Src, InDemandedBits, InDemandedElts, Known, TLO, 1970 Depth + 1)) 1971 return true; 1972 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 1973 assert(Known.getBitWidth() == InBits && "Src width has changed?"); 1974 Known = Known.anyext(BitWidth); 1975 1976 // Attempt to avoid multi-use ops if we don't need anything from them. 1977 if (SDValue NewSrc = SimplifyMultipleUseDemandedBits( 1978 Src, InDemandedBits, InDemandedElts, TLO.DAG, Depth + 1)) 1979 return TLO.CombineTo(Op, TLO.DAG.getNode(Op.getOpcode(), dl, VT, NewSrc)); 1980 break; 1981 } 1982 case ISD::TRUNCATE: { 1983 SDValue Src = Op.getOperand(0); 1984 1985 // Simplify the input, using demanded bit information, and compute the known 1986 // zero/one bits live out. 1987 unsigned OperandBitWidth = Src.getScalarValueSizeInBits(); 1988 APInt TruncMask = DemandedBits.zext(OperandBitWidth); 1989 if (SimplifyDemandedBits(Src, TruncMask, DemandedElts, Known, TLO, 1990 Depth + 1)) 1991 return true; 1992 Known = Known.trunc(BitWidth); 1993 1994 // Attempt to avoid multi-use ops if we don't need anything from them. 1995 if (SDValue NewSrc = SimplifyMultipleUseDemandedBits( 1996 Src, TruncMask, DemandedElts, TLO.DAG, Depth + 1)) 1997 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::TRUNCATE, dl, VT, NewSrc)); 1998 1999 // If the input is only used by this truncate, see if we can shrink it based 2000 // on the known demanded bits. 2001 if (Src.getNode()->hasOneUse()) { 2002 switch (Src.getOpcode()) { 2003 default: 2004 break; 2005 case ISD::SRL: 2006 // Shrink SRL by a constant if none of the high bits shifted in are 2007 // demanded. 2008 if (TLO.LegalTypes() && !isTypeDesirableForOp(ISD::SRL, VT)) 2009 // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is 2010 // undesirable. 2011 break; 2012 2013 const APInt *ShAmtC = 2014 TLO.DAG.getValidShiftAmountConstant(Src, DemandedElts); 2015 if (!ShAmtC) 2016 break; 2017 uint64_t ShVal = ShAmtC->getZExtValue(); 2018 2019 APInt HighBits = 2020 APInt::getHighBitsSet(OperandBitWidth, OperandBitWidth - BitWidth); 2021 HighBits.lshrInPlace(ShVal); 2022 HighBits = HighBits.trunc(BitWidth); 2023 2024 if (!(HighBits & DemandedBits)) { 2025 // None of the shifted in bits are needed. Add a truncate of the 2026 // shift input, then shift it. 2027 SDValue NewShAmt = TLO.DAG.getConstant( 2028 ShVal, dl, getShiftAmountTy(VT, DL, TLO.LegalTypes())); 2029 SDValue NewTrunc = 2030 TLO.DAG.getNode(ISD::TRUNCATE, dl, VT, Src.getOperand(0)); 2031 return TLO.CombineTo( 2032 Op, TLO.DAG.getNode(ISD::SRL, dl, VT, NewTrunc, NewShAmt)); 2033 } 2034 break; 2035 } 2036 } 2037 2038 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 2039 break; 2040 } 2041 case ISD::AssertZext: { 2042 // AssertZext demands all of the high bits, plus any of the low bits 2043 // demanded by its users. 2044 EVT ZVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 2045 APInt InMask = APInt::getLowBitsSet(BitWidth, ZVT.getSizeInBits()); 2046 if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | DemandedBits, Known, 2047 TLO, Depth + 1)) 2048 return true; 2049 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 2050 2051 Known.Zero |= ~InMask; 2052 break; 2053 } 2054 case ISD::EXTRACT_VECTOR_ELT: { 2055 SDValue Src = Op.getOperand(0); 2056 SDValue Idx = Op.getOperand(1); 2057 ElementCount SrcEltCnt = Src.getValueType().getVectorElementCount(); 2058 unsigned EltBitWidth = Src.getScalarValueSizeInBits(); 2059 2060 if (SrcEltCnt.isScalable()) 2061 return false; 2062 2063 // Demand the bits from every vector element without a constant index. 2064 unsigned NumSrcElts = SrcEltCnt.getFixedValue(); 2065 APInt DemandedSrcElts = APInt::getAllOnesValue(NumSrcElts); 2066 if (auto *CIdx = dyn_cast<ConstantSDNode>(Idx)) 2067 if (CIdx->getAPIntValue().ult(NumSrcElts)) 2068 DemandedSrcElts = APInt::getOneBitSet(NumSrcElts, CIdx->getZExtValue()); 2069 2070 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know 2071 // anything about the extended bits. 2072 APInt DemandedSrcBits = DemandedBits; 2073 if (BitWidth > EltBitWidth) 2074 DemandedSrcBits = DemandedSrcBits.trunc(EltBitWidth); 2075 2076 if (SimplifyDemandedBits(Src, DemandedSrcBits, DemandedSrcElts, Known2, TLO, 2077 Depth + 1)) 2078 return true; 2079 2080 // Attempt to avoid multi-use ops if we don't need anything from them. 2081 if (!DemandedSrcBits.isAllOnesValue() || 2082 !DemandedSrcElts.isAllOnesValue()) { 2083 if (SDValue DemandedSrc = SimplifyMultipleUseDemandedBits( 2084 Src, DemandedSrcBits, DemandedSrcElts, TLO.DAG, Depth + 1)) { 2085 SDValue NewOp = 2086 TLO.DAG.getNode(Op.getOpcode(), dl, VT, DemandedSrc, Idx); 2087 return TLO.CombineTo(Op, NewOp); 2088 } 2089 } 2090 2091 Known = Known2; 2092 if (BitWidth > EltBitWidth) 2093 Known = Known.anyext(BitWidth); 2094 break; 2095 } 2096 case ISD::BITCAST: { 2097 SDValue Src = Op.getOperand(0); 2098 EVT SrcVT = Src.getValueType(); 2099 unsigned NumSrcEltBits = SrcVT.getScalarSizeInBits(); 2100 2101 // If this is an FP->Int bitcast and if the sign bit is the only 2102 // thing demanded, turn this into a FGETSIGN. 2103 if (!TLO.LegalOperations() && !VT.isVector() && !SrcVT.isVector() && 2104 DemandedBits == APInt::getSignMask(Op.getValueSizeInBits()) && 2105 SrcVT.isFloatingPoint()) { 2106 bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, VT); 2107 bool i32Legal = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32); 2108 if ((OpVTLegal || i32Legal) && VT.isSimple() && SrcVT != MVT::f16 && 2109 SrcVT != MVT::f128) { 2110 // Cannot eliminate/lower SHL for f128 yet. 2111 EVT Ty = OpVTLegal ? VT : MVT::i32; 2112 // Make a FGETSIGN + SHL to move the sign bit into the appropriate 2113 // place. We expect the SHL to be eliminated by other optimizations. 2114 SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Src); 2115 unsigned OpVTSizeInBits = Op.getValueSizeInBits(); 2116 if (!OpVTLegal && OpVTSizeInBits > 32) 2117 Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Sign); 2118 unsigned ShVal = Op.getValueSizeInBits() - 1; 2119 SDValue ShAmt = TLO.DAG.getConstant(ShVal, dl, VT); 2120 return TLO.CombineTo(Op, 2121 TLO.DAG.getNode(ISD::SHL, dl, VT, Sign, ShAmt)); 2122 } 2123 } 2124 2125 // Bitcast from a vector using SimplifyDemanded Bits/VectorElts. 2126 // Demand the elt/bit if any of the original elts/bits are demanded. 2127 // TODO - bigendian once we have test coverage. 2128 if (SrcVT.isVector() && (BitWidth % NumSrcEltBits) == 0 && 2129 TLO.DAG.getDataLayout().isLittleEndian()) { 2130 unsigned Scale = BitWidth / NumSrcEltBits; 2131 unsigned NumSrcElts = SrcVT.getVectorNumElements(); 2132 APInt DemandedSrcBits = APInt::getNullValue(NumSrcEltBits); 2133 APInt DemandedSrcElts = APInt::getNullValue(NumSrcElts); 2134 for (unsigned i = 0; i != Scale; ++i) { 2135 unsigned Offset = i * NumSrcEltBits; 2136 APInt Sub = DemandedBits.extractBits(NumSrcEltBits, Offset); 2137 if (!Sub.isNullValue()) { 2138 DemandedSrcBits |= Sub; 2139 for (unsigned j = 0; j != NumElts; ++j) 2140 if (DemandedElts[j]) 2141 DemandedSrcElts.setBit((j * Scale) + i); 2142 } 2143 } 2144 2145 APInt KnownSrcUndef, KnownSrcZero; 2146 if (SimplifyDemandedVectorElts(Src, DemandedSrcElts, KnownSrcUndef, 2147 KnownSrcZero, TLO, Depth + 1)) 2148 return true; 2149 2150 KnownBits KnownSrcBits; 2151 if (SimplifyDemandedBits(Src, DemandedSrcBits, DemandedSrcElts, 2152 KnownSrcBits, TLO, Depth + 1)) 2153 return true; 2154 } else if ((NumSrcEltBits % BitWidth) == 0 && 2155 TLO.DAG.getDataLayout().isLittleEndian()) { 2156 unsigned Scale = NumSrcEltBits / BitWidth; 2157 unsigned NumSrcElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1; 2158 APInt DemandedSrcBits = APInt::getNullValue(NumSrcEltBits); 2159 APInt DemandedSrcElts = APInt::getNullValue(NumSrcElts); 2160 for (unsigned i = 0; i != NumElts; ++i) 2161 if (DemandedElts[i]) { 2162 unsigned Offset = (i % Scale) * BitWidth; 2163 DemandedSrcBits.insertBits(DemandedBits, Offset); 2164 DemandedSrcElts.setBit(i / Scale); 2165 } 2166 2167 if (SrcVT.isVector()) { 2168 APInt KnownSrcUndef, KnownSrcZero; 2169 if (SimplifyDemandedVectorElts(Src, DemandedSrcElts, KnownSrcUndef, 2170 KnownSrcZero, TLO, Depth + 1)) 2171 return true; 2172 } 2173 2174 KnownBits KnownSrcBits; 2175 if (SimplifyDemandedBits(Src, DemandedSrcBits, DemandedSrcElts, 2176 KnownSrcBits, TLO, Depth + 1)) 2177 return true; 2178 } 2179 2180 // If this is a bitcast, let computeKnownBits handle it. Only do this on a 2181 // recursive call where Known may be useful to the caller. 2182 if (Depth > 0) { 2183 Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth); 2184 return false; 2185 } 2186 break; 2187 } 2188 case ISD::ADD: 2189 case ISD::MUL: 2190 case ISD::SUB: { 2191 // Add, Sub, and Mul don't demand any bits in positions beyond that 2192 // of the highest bit demanded of them. 2193 SDValue Op0 = Op.getOperand(0), Op1 = Op.getOperand(1); 2194 SDNodeFlags Flags = Op.getNode()->getFlags(); 2195 unsigned DemandedBitsLZ = DemandedBits.countLeadingZeros(); 2196 APInt LoMask = APInt::getLowBitsSet(BitWidth, BitWidth - DemandedBitsLZ); 2197 if (SimplifyDemandedBits(Op0, LoMask, DemandedElts, Known2, TLO, 2198 Depth + 1) || 2199 SimplifyDemandedBits(Op1, LoMask, DemandedElts, Known2, TLO, 2200 Depth + 1) || 2201 // See if the operation should be performed at a smaller bit width. 2202 ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO)) { 2203 if (Flags.hasNoSignedWrap() || Flags.hasNoUnsignedWrap()) { 2204 // Disable the nsw and nuw flags. We can no longer guarantee that we 2205 // won't wrap after simplification. 2206 Flags.setNoSignedWrap(false); 2207 Flags.setNoUnsignedWrap(false); 2208 SDValue NewOp = 2209 TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Op1, Flags); 2210 return TLO.CombineTo(Op, NewOp); 2211 } 2212 return true; 2213 } 2214 2215 // Attempt to avoid multi-use ops if we don't need anything from them. 2216 if (!LoMask.isAllOnesValue() || !DemandedElts.isAllOnesValue()) { 2217 SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits( 2218 Op0, LoMask, DemandedElts, TLO.DAG, Depth + 1); 2219 SDValue DemandedOp1 = SimplifyMultipleUseDemandedBits( 2220 Op1, LoMask, DemandedElts, TLO.DAG, Depth + 1); 2221 if (DemandedOp0 || DemandedOp1) { 2222 Flags.setNoSignedWrap(false); 2223 Flags.setNoUnsignedWrap(false); 2224 Op0 = DemandedOp0 ? DemandedOp0 : Op0; 2225 Op1 = DemandedOp1 ? DemandedOp1 : Op1; 2226 SDValue NewOp = 2227 TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Op1, Flags); 2228 return TLO.CombineTo(Op, NewOp); 2229 } 2230 } 2231 2232 // If we have a constant operand, we may be able to turn it into -1 if we 2233 // do not demand the high bits. This can make the constant smaller to 2234 // encode, allow more general folding, or match specialized instruction 2235 // patterns (eg, 'blsr' on x86). Don't bother changing 1 to -1 because that 2236 // is probably not useful (and could be detrimental). 2237 ConstantSDNode *C = isConstOrConstSplat(Op1); 2238 APInt HighMask = APInt::getHighBitsSet(BitWidth, DemandedBitsLZ); 2239 if (C && !C->isAllOnesValue() && !C->isOne() && 2240 (C->getAPIntValue() | HighMask).isAllOnesValue()) { 2241 SDValue Neg1 = TLO.DAG.getAllOnesConstant(dl, VT); 2242 // Disable the nsw and nuw flags. We can no longer guarantee that we 2243 // won't wrap after simplification. 2244 Flags.setNoSignedWrap(false); 2245 Flags.setNoUnsignedWrap(false); 2246 SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Neg1, Flags); 2247 return TLO.CombineTo(Op, NewOp); 2248 } 2249 2250 LLVM_FALLTHROUGH; 2251 } 2252 default: 2253 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) { 2254 if (SimplifyDemandedBitsForTargetNode(Op, DemandedBits, DemandedElts, 2255 Known, TLO, Depth)) 2256 return true; 2257 break; 2258 } 2259 2260 // Just use computeKnownBits to compute output bits. 2261 Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth); 2262 break; 2263 } 2264 2265 // If we know the value of all of the demanded bits, return this as a 2266 // constant. 2267 if (DemandedBits.isSubsetOf(Known.Zero | Known.One)) { 2268 // Avoid folding to a constant if any OpaqueConstant is involved. 2269 const SDNode *N = Op.getNode(); 2270 for (SDNodeIterator I = SDNodeIterator::begin(N), 2271 E = SDNodeIterator::end(N); 2272 I != E; ++I) { 2273 SDNode *Op = *I; 2274 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) 2275 if (C->isOpaque()) 2276 return false; 2277 } 2278 if (VT.isInteger()) 2279 return TLO.CombineTo(Op, TLO.DAG.getConstant(Known.One, dl, VT)); 2280 if (VT.isFloatingPoint()) 2281 return TLO.CombineTo( 2282 Op, 2283 TLO.DAG.getConstantFP( 2284 APFloat(TLO.DAG.EVTToAPFloatSemantics(VT), Known.One), dl, VT)); 2285 } 2286 2287 return false; 2288 } 2289 2290 bool TargetLowering::SimplifyDemandedVectorElts(SDValue Op, 2291 const APInt &DemandedElts, 2292 APInt &KnownUndef, 2293 APInt &KnownZero, 2294 DAGCombinerInfo &DCI) const { 2295 SelectionDAG &DAG = DCI.DAG; 2296 TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 2297 !DCI.isBeforeLegalizeOps()); 2298 2299 bool Simplified = 2300 SimplifyDemandedVectorElts(Op, DemandedElts, KnownUndef, KnownZero, TLO); 2301 if (Simplified) { 2302 DCI.AddToWorklist(Op.getNode()); 2303 DCI.CommitTargetLoweringOpt(TLO); 2304 } 2305 2306 return Simplified; 2307 } 2308 2309 /// Given a vector binary operation and known undefined elements for each input 2310 /// operand, compute whether each element of the output is undefined. 2311 static APInt getKnownUndefForVectorBinop(SDValue BO, SelectionDAG &DAG, 2312 const APInt &UndefOp0, 2313 const APInt &UndefOp1) { 2314 EVT VT = BO.getValueType(); 2315 assert(DAG.getTargetLoweringInfo().isBinOp(BO.getOpcode()) && VT.isVector() && 2316 "Vector binop only"); 2317 2318 EVT EltVT = VT.getVectorElementType(); 2319 unsigned NumElts = VT.getVectorNumElements(); 2320 assert(UndefOp0.getBitWidth() == NumElts && 2321 UndefOp1.getBitWidth() == NumElts && "Bad type for undef analysis"); 2322 2323 auto getUndefOrConstantElt = [&](SDValue V, unsigned Index, 2324 const APInt &UndefVals) { 2325 if (UndefVals[Index]) 2326 return DAG.getUNDEF(EltVT); 2327 2328 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) { 2329 // Try hard to make sure that the getNode() call is not creating temporary 2330 // nodes. Ignore opaque integers because they do not constant fold. 2331 SDValue Elt = BV->getOperand(Index); 2332 auto *C = dyn_cast<ConstantSDNode>(Elt); 2333 if (isa<ConstantFPSDNode>(Elt) || Elt.isUndef() || (C && !C->isOpaque())) 2334 return Elt; 2335 } 2336 2337 return SDValue(); 2338 }; 2339 2340 APInt KnownUndef = APInt::getNullValue(NumElts); 2341 for (unsigned i = 0; i != NumElts; ++i) { 2342 // If both inputs for this element are either constant or undef and match 2343 // the element type, compute the constant/undef result for this element of 2344 // the vector. 2345 // TODO: Ideally we would use FoldConstantArithmetic() here, but that does 2346 // not handle FP constants. The code within getNode() should be refactored 2347 // to avoid the danger of creating a bogus temporary node here. 2348 SDValue C0 = getUndefOrConstantElt(BO.getOperand(0), i, UndefOp0); 2349 SDValue C1 = getUndefOrConstantElt(BO.getOperand(1), i, UndefOp1); 2350 if (C0 && C1 && C0.getValueType() == EltVT && C1.getValueType() == EltVT) 2351 if (DAG.getNode(BO.getOpcode(), SDLoc(BO), EltVT, C0, C1).isUndef()) 2352 KnownUndef.setBit(i); 2353 } 2354 return KnownUndef; 2355 } 2356 2357 bool TargetLowering::SimplifyDemandedVectorElts( 2358 SDValue Op, const APInt &OriginalDemandedElts, APInt &KnownUndef, 2359 APInt &KnownZero, TargetLoweringOpt &TLO, unsigned Depth, 2360 bool AssumeSingleUse) const { 2361 EVT VT = Op.getValueType(); 2362 unsigned Opcode = Op.getOpcode(); 2363 APInt DemandedElts = OriginalDemandedElts; 2364 unsigned NumElts = DemandedElts.getBitWidth(); 2365 assert(VT.isVector() && "Expected vector op"); 2366 2367 KnownUndef = KnownZero = APInt::getNullValue(NumElts); 2368 2369 // TODO: For now we assume we know nothing about scalable vectors. 2370 if (VT.isScalableVector()) 2371 return false; 2372 2373 assert(VT.getVectorNumElements() == NumElts && 2374 "Mask size mismatches value type element count!"); 2375 2376 // Undef operand. 2377 if (Op.isUndef()) { 2378 KnownUndef.setAllBits(); 2379 return false; 2380 } 2381 2382 // If Op has other users, assume that all elements are needed. 2383 if (!Op.getNode()->hasOneUse() && !AssumeSingleUse) 2384 DemandedElts.setAllBits(); 2385 2386 // Not demanding any elements from Op. 2387 if (DemandedElts == 0) { 2388 KnownUndef.setAllBits(); 2389 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT)); 2390 } 2391 2392 // Limit search depth. 2393 if (Depth >= SelectionDAG::MaxRecursionDepth) 2394 return false; 2395 2396 SDLoc DL(Op); 2397 unsigned EltSizeInBits = VT.getScalarSizeInBits(); 2398 2399 // Helper for demanding the specified elements and all the bits of both binary 2400 // operands. 2401 auto SimplifyDemandedVectorEltsBinOp = [&](SDValue Op0, SDValue Op1) { 2402 SDValue NewOp0 = SimplifyMultipleUseDemandedVectorElts(Op0, DemandedElts, 2403 TLO.DAG, Depth + 1); 2404 SDValue NewOp1 = SimplifyMultipleUseDemandedVectorElts(Op1, DemandedElts, 2405 TLO.DAG, Depth + 1); 2406 if (NewOp0 || NewOp1) { 2407 SDValue NewOp = TLO.DAG.getNode( 2408 Opcode, SDLoc(Op), VT, NewOp0 ? NewOp0 : Op0, NewOp1 ? NewOp1 : Op1); 2409 return TLO.CombineTo(Op, NewOp); 2410 } 2411 return false; 2412 }; 2413 2414 switch (Opcode) { 2415 case ISD::SCALAR_TO_VECTOR: { 2416 if (!DemandedElts[0]) { 2417 KnownUndef.setAllBits(); 2418 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT)); 2419 } 2420 KnownUndef.setHighBits(NumElts - 1); 2421 break; 2422 } 2423 case ISD::BITCAST: { 2424 SDValue Src = Op.getOperand(0); 2425 EVT SrcVT = Src.getValueType(); 2426 2427 // We only handle vectors here. 2428 // TODO - investigate calling SimplifyDemandedBits/ComputeKnownBits? 2429 if (!SrcVT.isVector()) 2430 break; 2431 2432 // Fast handling of 'identity' bitcasts. 2433 unsigned NumSrcElts = SrcVT.getVectorNumElements(); 2434 if (NumSrcElts == NumElts) 2435 return SimplifyDemandedVectorElts(Src, DemandedElts, KnownUndef, 2436 KnownZero, TLO, Depth + 1); 2437 2438 APInt SrcZero, SrcUndef; 2439 APInt SrcDemandedElts = APInt::getNullValue(NumSrcElts); 2440 2441 // Bitcast from 'large element' src vector to 'small element' vector, we 2442 // must demand a source element if any DemandedElt maps to it. 2443 if ((NumElts % NumSrcElts) == 0) { 2444 unsigned Scale = NumElts / NumSrcElts; 2445 for (unsigned i = 0; i != NumElts; ++i) 2446 if (DemandedElts[i]) 2447 SrcDemandedElts.setBit(i / Scale); 2448 2449 if (SimplifyDemandedVectorElts(Src, SrcDemandedElts, SrcUndef, SrcZero, 2450 TLO, Depth + 1)) 2451 return true; 2452 2453 // Try calling SimplifyDemandedBits, converting demanded elts to the bits 2454 // of the large element. 2455 // TODO - bigendian once we have test coverage. 2456 if (TLO.DAG.getDataLayout().isLittleEndian()) { 2457 unsigned SrcEltSizeInBits = SrcVT.getScalarSizeInBits(); 2458 APInt SrcDemandedBits = APInt::getNullValue(SrcEltSizeInBits); 2459 for (unsigned i = 0; i != NumElts; ++i) 2460 if (DemandedElts[i]) { 2461 unsigned Ofs = (i % Scale) * EltSizeInBits; 2462 SrcDemandedBits.setBits(Ofs, Ofs + EltSizeInBits); 2463 } 2464 2465 KnownBits Known; 2466 if (SimplifyDemandedBits(Src, SrcDemandedBits, SrcDemandedElts, Known, 2467 TLO, Depth + 1)) 2468 return true; 2469 } 2470 2471 // If the src element is zero/undef then all the output elements will be - 2472 // only demanded elements are guaranteed to be correct. 2473 for (unsigned i = 0; i != NumSrcElts; ++i) { 2474 if (SrcDemandedElts[i]) { 2475 if (SrcZero[i]) 2476 KnownZero.setBits(i * Scale, (i + 1) * Scale); 2477 if (SrcUndef[i]) 2478 KnownUndef.setBits(i * Scale, (i + 1) * Scale); 2479 } 2480 } 2481 } 2482 2483 // Bitcast from 'small element' src vector to 'large element' vector, we 2484 // demand all smaller source elements covered by the larger demanded element 2485 // of this vector. 2486 if ((NumSrcElts % NumElts) == 0) { 2487 unsigned Scale = NumSrcElts / NumElts; 2488 for (unsigned i = 0; i != NumElts; ++i) 2489 if (DemandedElts[i]) 2490 SrcDemandedElts.setBits(i * Scale, (i + 1) * Scale); 2491 2492 if (SimplifyDemandedVectorElts(Src, SrcDemandedElts, SrcUndef, SrcZero, 2493 TLO, Depth + 1)) 2494 return true; 2495 2496 // If all the src elements covering an output element are zero/undef, then 2497 // the output element will be as well, assuming it was demanded. 2498 for (unsigned i = 0; i != NumElts; ++i) { 2499 if (DemandedElts[i]) { 2500 if (SrcZero.extractBits(Scale, i * Scale).isAllOnesValue()) 2501 KnownZero.setBit(i); 2502 if (SrcUndef.extractBits(Scale, i * Scale).isAllOnesValue()) 2503 KnownUndef.setBit(i); 2504 } 2505 } 2506 } 2507 break; 2508 } 2509 case ISD::BUILD_VECTOR: { 2510 // Check all elements and simplify any unused elements with UNDEF. 2511 if (!DemandedElts.isAllOnesValue()) { 2512 // Don't simplify BROADCASTS. 2513 if (llvm::any_of(Op->op_values(), 2514 [&](SDValue Elt) { return Op.getOperand(0) != Elt; })) { 2515 SmallVector<SDValue, 32> Ops(Op->op_begin(), Op->op_end()); 2516 bool Updated = false; 2517 for (unsigned i = 0; i != NumElts; ++i) { 2518 if (!DemandedElts[i] && !Ops[i].isUndef()) { 2519 Ops[i] = TLO.DAG.getUNDEF(Ops[0].getValueType()); 2520 KnownUndef.setBit(i); 2521 Updated = true; 2522 } 2523 } 2524 if (Updated) 2525 return TLO.CombineTo(Op, TLO.DAG.getBuildVector(VT, DL, Ops)); 2526 } 2527 } 2528 for (unsigned i = 0; i != NumElts; ++i) { 2529 SDValue SrcOp = Op.getOperand(i); 2530 if (SrcOp.isUndef()) { 2531 KnownUndef.setBit(i); 2532 } else if (EltSizeInBits == SrcOp.getScalarValueSizeInBits() && 2533 (isNullConstant(SrcOp) || isNullFPConstant(SrcOp))) { 2534 KnownZero.setBit(i); 2535 } 2536 } 2537 break; 2538 } 2539 case ISD::CONCAT_VECTORS: { 2540 EVT SubVT = Op.getOperand(0).getValueType(); 2541 unsigned NumSubVecs = Op.getNumOperands(); 2542 unsigned NumSubElts = SubVT.getVectorNumElements(); 2543 for (unsigned i = 0; i != NumSubVecs; ++i) { 2544 SDValue SubOp = Op.getOperand(i); 2545 APInt SubElts = DemandedElts.extractBits(NumSubElts, i * NumSubElts); 2546 APInt SubUndef, SubZero; 2547 if (SimplifyDemandedVectorElts(SubOp, SubElts, SubUndef, SubZero, TLO, 2548 Depth + 1)) 2549 return true; 2550 KnownUndef.insertBits(SubUndef, i * NumSubElts); 2551 KnownZero.insertBits(SubZero, i * NumSubElts); 2552 } 2553 break; 2554 } 2555 case ISD::INSERT_SUBVECTOR: { 2556 // Demand any elements from the subvector and the remainder from the src its 2557 // inserted into. 2558 SDValue Src = Op.getOperand(0); 2559 SDValue Sub = Op.getOperand(1); 2560 uint64_t Idx = Op.getConstantOperandVal(2); 2561 unsigned NumSubElts = Sub.getValueType().getVectorNumElements(); 2562 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx); 2563 APInt DemandedSrcElts = DemandedElts; 2564 DemandedSrcElts.insertBits(APInt::getNullValue(NumSubElts), Idx); 2565 2566 APInt SubUndef, SubZero; 2567 if (SimplifyDemandedVectorElts(Sub, DemandedSubElts, SubUndef, SubZero, TLO, 2568 Depth + 1)) 2569 return true; 2570 2571 // If none of the src operand elements are demanded, replace it with undef. 2572 if (!DemandedSrcElts && !Src.isUndef()) 2573 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, 2574 TLO.DAG.getUNDEF(VT), Sub, 2575 Op.getOperand(2))); 2576 2577 if (SimplifyDemandedVectorElts(Src, DemandedSrcElts, KnownUndef, KnownZero, 2578 TLO, Depth + 1)) 2579 return true; 2580 KnownUndef.insertBits(SubUndef, Idx); 2581 KnownZero.insertBits(SubZero, Idx); 2582 2583 // Attempt to avoid multi-use ops if we don't need anything from them. 2584 if (!DemandedSrcElts.isAllOnesValue() || 2585 !DemandedSubElts.isAllOnesValue()) { 2586 SDValue NewSrc = SimplifyMultipleUseDemandedVectorElts( 2587 Src, DemandedSrcElts, TLO.DAG, Depth + 1); 2588 SDValue NewSub = SimplifyMultipleUseDemandedVectorElts( 2589 Sub, DemandedSubElts, TLO.DAG, Depth + 1); 2590 if (NewSrc || NewSub) { 2591 NewSrc = NewSrc ? NewSrc : Src; 2592 NewSub = NewSub ? NewSub : Sub; 2593 SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, NewSrc, 2594 NewSub, Op.getOperand(2)); 2595 return TLO.CombineTo(Op, NewOp); 2596 } 2597 } 2598 break; 2599 } 2600 case ISD::EXTRACT_SUBVECTOR: { 2601 // Offset the demanded elts by the subvector index. 2602 SDValue Src = Op.getOperand(0); 2603 if (Src.getValueType().isScalableVector()) 2604 break; 2605 uint64_t Idx = Op.getConstantOperandVal(1); 2606 unsigned NumSrcElts = Src.getValueType().getVectorNumElements(); 2607 APInt DemandedSrcElts = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx); 2608 2609 APInt SrcUndef, SrcZero; 2610 if (SimplifyDemandedVectorElts(Src, DemandedSrcElts, SrcUndef, SrcZero, TLO, 2611 Depth + 1)) 2612 return true; 2613 KnownUndef = SrcUndef.extractBits(NumElts, Idx); 2614 KnownZero = SrcZero.extractBits(NumElts, Idx); 2615 2616 // Attempt to avoid multi-use ops if we don't need anything from them. 2617 if (!DemandedElts.isAllOnesValue()) { 2618 SDValue NewSrc = SimplifyMultipleUseDemandedVectorElts( 2619 Src, DemandedSrcElts, TLO.DAG, Depth + 1); 2620 if (NewSrc) { 2621 SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, NewSrc, 2622 Op.getOperand(1)); 2623 return TLO.CombineTo(Op, NewOp); 2624 } 2625 } 2626 break; 2627 } 2628 case ISD::INSERT_VECTOR_ELT: { 2629 SDValue Vec = Op.getOperand(0); 2630 SDValue Scl = Op.getOperand(1); 2631 auto *CIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2)); 2632 2633 // For a legal, constant insertion index, if we don't need this insertion 2634 // then strip it, else remove it from the demanded elts. 2635 if (CIdx && CIdx->getAPIntValue().ult(NumElts)) { 2636 unsigned Idx = CIdx->getZExtValue(); 2637 if (!DemandedElts[Idx]) 2638 return TLO.CombineTo(Op, Vec); 2639 2640 APInt DemandedVecElts(DemandedElts); 2641 DemandedVecElts.clearBit(Idx); 2642 if (SimplifyDemandedVectorElts(Vec, DemandedVecElts, KnownUndef, 2643 KnownZero, TLO, Depth + 1)) 2644 return true; 2645 2646 KnownUndef.setBitVal(Idx, Scl.isUndef()); 2647 2648 KnownZero.setBitVal(Idx, isNullConstant(Scl) || isNullFPConstant(Scl)); 2649 break; 2650 } 2651 2652 APInt VecUndef, VecZero; 2653 if (SimplifyDemandedVectorElts(Vec, DemandedElts, VecUndef, VecZero, TLO, 2654 Depth + 1)) 2655 return true; 2656 // Without knowing the insertion index we can't set KnownUndef/KnownZero. 2657 break; 2658 } 2659 case ISD::VSELECT: { 2660 // Try to transform the select condition based on the current demanded 2661 // elements. 2662 // TODO: If a condition element is undef, we can choose from one arm of the 2663 // select (and if one arm is undef, then we can propagate that to the 2664 // result). 2665 // TODO - add support for constant vselect masks (see IR version of this). 2666 APInt UnusedUndef, UnusedZero; 2667 if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedElts, UnusedUndef, 2668 UnusedZero, TLO, Depth + 1)) 2669 return true; 2670 2671 // See if we can simplify either vselect operand. 2672 APInt DemandedLHS(DemandedElts); 2673 APInt DemandedRHS(DemandedElts); 2674 APInt UndefLHS, ZeroLHS; 2675 APInt UndefRHS, ZeroRHS; 2676 if (SimplifyDemandedVectorElts(Op.getOperand(1), DemandedLHS, UndefLHS, 2677 ZeroLHS, TLO, Depth + 1)) 2678 return true; 2679 if (SimplifyDemandedVectorElts(Op.getOperand(2), DemandedRHS, UndefRHS, 2680 ZeroRHS, TLO, Depth + 1)) 2681 return true; 2682 2683 KnownUndef = UndefLHS & UndefRHS; 2684 KnownZero = ZeroLHS & ZeroRHS; 2685 break; 2686 } 2687 case ISD::VECTOR_SHUFFLE: { 2688 ArrayRef<int> ShuffleMask = cast<ShuffleVectorSDNode>(Op)->getMask(); 2689 2690 // Collect demanded elements from shuffle operands.. 2691 APInt DemandedLHS(NumElts, 0); 2692 APInt DemandedRHS(NumElts, 0); 2693 for (unsigned i = 0; i != NumElts; ++i) { 2694 int M = ShuffleMask[i]; 2695 if (M < 0 || !DemandedElts[i]) 2696 continue; 2697 assert(0 <= M && M < (int)(2 * NumElts) && "Shuffle index out of range"); 2698 if (M < (int)NumElts) 2699 DemandedLHS.setBit(M); 2700 else 2701 DemandedRHS.setBit(M - NumElts); 2702 } 2703 2704 // See if we can simplify either shuffle operand. 2705 APInt UndefLHS, ZeroLHS; 2706 APInt UndefRHS, ZeroRHS; 2707 if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedLHS, UndefLHS, 2708 ZeroLHS, TLO, Depth + 1)) 2709 return true; 2710 if (SimplifyDemandedVectorElts(Op.getOperand(1), DemandedRHS, UndefRHS, 2711 ZeroRHS, TLO, Depth + 1)) 2712 return true; 2713 2714 // Simplify mask using undef elements from LHS/RHS. 2715 bool Updated = false; 2716 bool IdentityLHS = true, IdentityRHS = true; 2717 SmallVector<int, 32> NewMask(ShuffleMask.begin(), ShuffleMask.end()); 2718 for (unsigned i = 0; i != NumElts; ++i) { 2719 int &M = NewMask[i]; 2720 if (M < 0) 2721 continue; 2722 if (!DemandedElts[i] || (M < (int)NumElts && UndefLHS[M]) || 2723 (M >= (int)NumElts && UndefRHS[M - NumElts])) { 2724 Updated = true; 2725 M = -1; 2726 } 2727 IdentityLHS &= (M < 0) || (M == (int)i); 2728 IdentityRHS &= (M < 0) || ((M - NumElts) == i); 2729 } 2730 2731 // Update legal shuffle masks based on demanded elements if it won't reduce 2732 // to Identity which can cause premature removal of the shuffle mask. 2733 if (Updated && !IdentityLHS && !IdentityRHS && !TLO.LegalOps) { 2734 SDValue LegalShuffle = 2735 buildLegalVectorShuffle(VT, DL, Op.getOperand(0), Op.getOperand(1), 2736 NewMask, TLO.DAG); 2737 if (LegalShuffle) 2738 return TLO.CombineTo(Op, LegalShuffle); 2739 } 2740 2741 // Propagate undef/zero elements from LHS/RHS. 2742 for (unsigned i = 0; i != NumElts; ++i) { 2743 int M = ShuffleMask[i]; 2744 if (M < 0) { 2745 KnownUndef.setBit(i); 2746 } else if (M < (int)NumElts) { 2747 if (UndefLHS[M]) 2748 KnownUndef.setBit(i); 2749 if (ZeroLHS[M]) 2750 KnownZero.setBit(i); 2751 } else { 2752 if (UndefRHS[M - NumElts]) 2753 KnownUndef.setBit(i); 2754 if (ZeroRHS[M - NumElts]) 2755 KnownZero.setBit(i); 2756 } 2757 } 2758 break; 2759 } 2760 case ISD::ANY_EXTEND_VECTOR_INREG: 2761 case ISD::SIGN_EXTEND_VECTOR_INREG: 2762 case ISD::ZERO_EXTEND_VECTOR_INREG: { 2763 APInt SrcUndef, SrcZero; 2764 SDValue Src = Op.getOperand(0); 2765 unsigned NumSrcElts = Src.getValueType().getVectorNumElements(); 2766 APInt DemandedSrcElts = DemandedElts.zextOrSelf(NumSrcElts); 2767 if (SimplifyDemandedVectorElts(Src, DemandedSrcElts, SrcUndef, SrcZero, TLO, 2768 Depth + 1)) 2769 return true; 2770 KnownZero = SrcZero.zextOrTrunc(NumElts); 2771 KnownUndef = SrcUndef.zextOrTrunc(NumElts); 2772 2773 if (Op.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG && 2774 Op.getValueSizeInBits() == Src.getValueSizeInBits() && 2775 DemandedSrcElts == 1 && TLO.DAG.getDataLayout().isLittleEndian()) { 2776 // aext - if we just need the bottom element then we can bitcast. 2777 return TLO.CombineTo(Op, TLO.DAG.getBitcast(VT, Src)); 2778 } 2779 2780 if (Op.getOpcode() == ISD::ZERO_EXTEND_VECTOR_INREG) { 2781 // zext(undef) upper bits are guaranteed to be zero. 2782 if (DemandedElts.isSubsetOf(KnownUndef)) 2783 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, SDLoc(Op), VT)); 2784 KnownUndef.clearAllBits(); 2785 } 2786 break; 2787 } 2788 2789 // TODO: There are more binop opcodes that could be handled here - MIN, 2790 // MAX, saturated math, etc. 2791 case ISD::OR: 2792 case ISD::XOR: 2793 case ISD::ADD: 2794 case ISD::SUB: 2795 case ISD::FADD: 2796 case ISD::FSUB: 2797 case ISD::FMUL: 2798 case ISD::FDIV: 2799 case ISD::FREM: { 2800 SDValue Op0 = Op.getOperand(0); 2801 SDValue Op1 = Op.getOperand(1); 2802 2803 APInt UndefRHS, ZeroRHS; 2804 if (SimplifyDemandedVectorElts(Op1, DemandedElts, UndefRHS, ZeroRHS, TLO, 2805 Depth + 1)) 2806 return true; 2807 APInt UndefLHS, ZeroLHS; 2808 if (SimplifyDemandedVectorElts(Op0, DemandedElts, UndefLHS, ZeroLHS, TLO, 2809 Depth + 1)) 2810 return true; 2811 2812 KnownZero = ZeroLHS & ZeroRHS; 2813 KnownUndef = getKnownUndefForVectorBinop(Op, TLO.DAG, UndefLHS, UndefRHS); 2814 2815 // Attempt to avoid multi-use ops if we don't need anything from them. 2816 // TODO - use KnownUndef to relax the demandedelts? 2817 if (!DemandedElts.isAllOnesValue()) 2818 if (SimplifyDemandedVectorEltsBinOp(Op0, Op1)) 2819 return true; 2820 break; 2821 } 2822 case ISD::SHL: 2823 case ISD::SRL: 2824 case ISD::SRA: 2825 case ISD::ROTL: 2826 case ISD::ROTR: { 2827 SDValue Op0 = Op.getOperand(0); 2828 SDValue Op1 = Op.getOperand(1); 2829 2830 APInt UndefRHS, ZeroRHS; 2831 if (SimplifyDemandedVectorElts(Op1, DemandedElts, UndefRHS, ZeroRHS, TLO, 2832 Depth + 1)) 2833 return true; 2834 APInt UndefLHS, ZeroLHS; 2835 if (SimplifyDemandedVectorElts(Op0, DemandedElts, UndefLHS, ZeroLHS, TLO, 2836 Depth + 1)) 2837 return true; 2838 2839 KnownZero = ZeroLHS; 2840 KnownUndef = UndefLHS & UndefRHS; // TODO: use getKnownUndefForVectorBinop? 2841 2842 // Attempt to avoid multi-use ops if we don't need anything from them. 2843 // TODO - use KnownUndef to relax the demandedelts? 2844 if (!DemandedElts.isAllOnesValue()) 2845 if (SimplifyDemandedVectorEltsBinOp(Op0, Op1)) 2846 return true; 2847 break; 2848 } 2849 case ISD::MUL: 2850 case ISD::AND: { 2851 SDValue Op0 = Op.getOperand(0); 2852 SDValue Op1 = Op.getOperand(1); 2853 2854 APInt SrcUndef, SrcZero; 2855 if (SimplifyDemandedVectorElts(Op1, DemandedElts, SrcUndef, SrcZero, TLO, 2856 Depth + 1)) 2857 return true; 2858 if (SimplifyDemandedVectorElts(Op0, DemandedElts, KnownUndef, KnownZero, 2859 TLO, Depth + 1)) 2860 return true; 2861 2862 // If either side has a zero element, then the result element is zero, even 2863 // if the other is an UNDEF. 2864 // TODO: Extend getKnownUndefForVectorBinop to also deal with known zeros 2865 // and then handle 'and' nodes with the rest of the binop opcodes. 2866 KnownZero |= SrcZero; 2867 KnownUndef &= SrcUndef; 2868 KnownUndef &= ~KnownZero; 2869 2870 // Attempt to avoid multi-use ops if we don't need anything from them. 2871 // TODO - use KnownUndef to relax the demandedelts? 2872 if (!DemandedElts.isAllOnesValue()) 2873 if (SimplifyDemandedVectorEltsBinOp(Op0, Op1)) 2874 return true; 2875 break; 2876 } 2877 case ISD::TRUNCATE: 2878 case ISD::SIGN_EXTEND: 2879 case ISD::ZERO_EXTEND: 2880 if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedElts, KnownUndef, 2881 KnownZero, TLO, Depth + 1)) 2882 return true; 2883 2884 if (Op.getOpcode() == ISD::ZERO_EXTEND) { 2885 // zext(undef) upper bits are guaranteed to be zero. 2886 if (DemandedElts.isSubsetOf(KnownUndef)) 2887 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, SDLoc(Op), VT)); 2888 KnownUndef.clearAllBits(); 2889 } 2890 break; 2891 default: { 2892 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) { 2893 if (SimplifyDemandedVectorEltsForTargetNode(Op, DemandedElts, KnownUndef, 2894 KnownZero, TLO, Depth)) 2895 return true; 2896 } else { 2897 KnownBits Known; 2898 APInt DemandedBits = APInt::getAllOnesValue(EltSizeInBits); 2899 if (SimplifyDemandedBits(Op, DemandedBits, OriginalDemandedElts, Known, 2900 TLO, Depth, AssumeSingleUse)) 2901 return true; 2902 } 2903 break; 2904 } 2905 } 2906 assert((KnownUndef & KnownZero) == 0 && "Elements flagged as undef AND zero"); 2907 2908 // Constant fold all undef cases. 2909 // TODO: Handle zero cases as well. 2910 if (DemandedElts.isSubsetOf(KnownUndef)) 2911 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT)); 2912 2913 return false; 2914 } 2915 2916 /// Determine which of the bits specified in Mask are known to be either zero or 2917 /// one and return them in the Known. 2918 void TargetLowering::computeKnownBitsForTargetNode(const SDValue Op, 2919 KnownBits &Known, 2920 const APInt &DemandedElts, 2921 const SelectionDAG &DAG, 2922 unsigned Depth) const { 2923 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END || 2924 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 2925 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 2926 Op.getOpcode() == ISD::INTRINSIC_VOID) && 2927 "Should use MaskedValueIsZero if you don't know whether Op" 2928 " is a target node!"); 2929 Known.resetAll(); 2930 } 2931 2932 void TargetLowering::computeKnownBitsForTargetInstr( 2933 GISelKnownBits &Analysis, Register R, KnownBits &Known, 2934 const APInt &DemandedElts, const MachineRegisterInfo &MRI, 2935 unsigned Depth) const { 2936 Known.resetAll(); 2937 } 2938 2939 void TargetLowering::computeKnownBitsForFrameIndex( 2940 const int FrameIdx, KnownBits &Known, const MachineFunction &MF) const { 2941 // The low bits are known zero if the pointer is aligned. 2942 Known.Zero.setLowBits(Log2(MF.getFrameInfo().getObjectAlign(FrameIdx))); 2943 } 2944 2945 Align TargetLowering::computeKnownAlignForTargetInstr( 2946 GISelKnownBits &Analysis, Register R, const MachineRegisterInfo &MRI, 2947 unsigned Depth) const { 2948 return Align(1); 2949 } 2950 2951 /// This method can be implemented by targets that want to expose additional 2952 /// information about sign bits to the DAG Combiner. 2953 unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op, 2954 const APInt &, 2955 const SelectionDAG &, 2956 unsigned Depth) const { 2957 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END || 2958 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 2959 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 2960 Op.getOpcode() == ISD::INTRINSIC_VOID) && 2961 "Should use ComputeNumSignBits if you don't know whether Op" 2962 " is a target node!"); 2963 return 1; 2964 } 2965 2966 unsigned TargetLowering::computeNumSignBitsForTargetInstr( 2967 GISelKnownBits &Analysis, Register R, const APInt &DemandedElts, 2968 const MachineRegisterInfo &MRI, unsigned Depth) const { 2969 return 1; 2970 } 2971 2972 bool TargetLowering::SimplifyDemandedVectorEltsForTargetNode( 2973 SDValue Op, const APInt &DemandedElts, APInt &KnownUndef, APInt &KnownZero, 2974 TargetLoweringOpt &TLO, unsigned Depth) const { 2975 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END || 2976 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 2977 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 2978 Op.getOpcode() == ISD::INTRINSIC_VOID) && 2979 "Should use SimplifyDemandedVectorElts if you don't know whether Op" 2980 " is a target node!"); 2981 return false; 2982 } 2983 2984 bool TargetLowering::SimplifyDemandedBitsForTargetNode( 2985 SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts, 2986 KnownBits &Known, TargetLoweringOpt &TLO, unsigned Depth) const { 2987 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END || 2988 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 2989 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 2990 Op.getOpcode() == ISD::INTRINSIC_VOID) && 2991 "Should use SimplifyDemandedBits if you don't know whether Op" 2992 " is a target node!"); 2993 computeKnownBitsForTargetNode(Op, Known, DemandedElts, TLO.DAG, Depth); 2994 return false; 2995 } 2996 2997 SDValue TargetLowering::SimplifyMultipleUseDemandedBitsForTargetNode( 2998 SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts, 2999 SelectionDAG &DAG, unsigned Depth) const { 3000 assert( 3001 (Op.getOpcode() >= ISD::BUILTIN_OP_END || 3002 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 3003 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 3004 Op.getOpcode() == ISD::INTRINSIC_VOID) && 3005 "Should use SimplifyMultipleUseDemandedBits if you don't know whether Op" 3006 " is a target node!"); 3007 return SDValue(); 3008 } 3009 3010 SDValue 3011 TargetLowering::buildLegalVectorShuffle(EVT VT, const SDLoc &DL, SDValue N0, 3012 SDValue N1, MutableArrayRef<int> Mask, 3013 SelectionDAG &DAG) const { 3014 bool LegalMask = isShuffleMaskLegal(Mask, VT); 3015 if (!LegalMask) { 3016 std::swap(N0, N1); 3017 ShuffleVectorSDNode::commuteMask(Mask); 3018 LegalMask = isShuffleMaskLegal(Mask, VT); 3019 } 3020 3021 if (!LegalMask) 3022 return SDValue(); 3023 3024 return DAG.getVectorShuffle(VT, DL, N0, N1, Mask); 3025 } 3026 3027 const Constant *TargetLowering::getTargetConstantFromLoad(LoadSDNode*) const { 3028 return nullptr; 3029 } 3030 3031 bool TargetLowering::isKnownNeverNaNForTargetNode(SDValue Op, 3032 const SelectionDAG &DAG, 3033 bool SNaN, 3034 unsigned Depth) const { 3035 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END || 3036 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 3037 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 3038 Op.getOpcode() == ISD::INTRINSIC_VOID) && 3039 "Should use isKnownNeverNaN if you don't know whether Op" 3040 " is a target node!"); 3041 return false; 3042 } 3043 3044 // FIXME: Ideally, this would use ISD::isConstantSplatVector(), but that must 3045 // work with truncating build vectors and vectors with elements of less than 3046 // 8 bits. 3047 bool TargetLowering::isConstTrueVal(const SDNode *N) const { 3048 if (!N) 3049 return false; 3050 3051 APInt CVal; 3052 if (auto *CN = dyn_cast<ConstantSDNode>(N)) { 3053 CVal = CN->getAPIntValue(); 3054 } else if (auto *BV = dyn_cast<BuildVectorSDNode>(N)) { 3055 auto *CN = BV->getConstantSplatNode(); 3056 if (!CN) 3057 return false; 3058 3059 // If this is a truncating build vector, truncate the splat value. 3060 // Otherwise, we may fail to match the expected values below. 3061 unsigned BVEltWidth = BV->getValueType(0).getScalarSizeInBits(); 3062 CVal = CN->getAPIntValue(); 3063 if (BVEltWidth < CVal.getBitWidth()) 3064 CVal = CVal.trunc(BVEltWidth); 3065 } else { 3066 return false; 3067 } 3068 3069 switch (getBooleanContents(N->getValueType(0))) { 3070 case UndefinedBooleanContent: 3071 return CVal[0]; 3072 case ZeroOrOneBooleanContent: 3073 return CVal.isOneValue(); 3074 case ZeroOrNegativeOneBooleanContent: 3075 return CVal.isAllOnesValue(); 3076 } 3077 3078 llvm_unreachable("Invalid boolean contents"); 3079 } 3080 3081 bool TargetLowering::isConstFalseVal(const SDNode *N) const { 3082 if (!N) 3083 return false; 3084 3085 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N); 3086 if (!CN) { 3087 const BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N); 3088 if (!BV) 3089 return false; 3090 3091 // Only interested in constant splats, we don't care about undef 3092 // elements in identifying boolean constants and getConstantSplatNode 3093 // returns NULL if all ops are undef; 3094 CN = BV->getConstantSplatNode(); 3095 if (!CN) 3096 return false; 3097 } 3098 3099 if (getBooleanContents(N->getValueType(0)) == UndefinedBooleanContent) 3100 return !CN->getAPIntValue()[0]; 3101 3102 return CN->isNullValue(); 3103 } 3104 3105 bool TargetLowering::isExtendedTrueVal(const ConstantSDNode *N, EVT VT, 3106 bool SExt) const { 3107 if (VT == MVT::i1) 3108 return N->isOne(); 3109 3110 TargetLowering::BooleanContent Cnt = getBooleanContents(VT); 3111 switch (Cnt) { 3112 case TargetLowering::ZeroOrOneBooleanContent: 3113 // An extended value of 1 is always true, unless its original type is i1, 3114 // in which case it will be sign extended to -1. 3115 return (N->isOne() && !SExt) || (SExt && (N->getValueType(0) != MVT::i1)); 3116 case TargetLowering::UndefinedBooleanContent: 3117 case TargetLowering::ZeroOrNegativeOneBooleanContent: 3118 return N->isAllOnesValue() && SExt; 3119 } 3120 llvm_unreachable("Unexpected enumeration."); 3121 } 3122 3123 /// This helper function of SimplifySetCC tries to optimize the comparison when 3124 /// either operand of the SetCC node is a bitwise-and instruction. 3125 SDValue TargetLowering::foldSetCCWithAnd(EVT VT, SDValue N0, SDValue N1, 3126 ISD::CondCode Cond, const SDLoc &DL, 3127 DAGCombinerInfo &DCI) const { 3128 // Match these patterns in any of their permutations: 3129 // (X & Y) == Y 3130 // (X & Y) != Y 3131 if (N1.getOpcode() == ISD::AND && N0.getOpcode() != ISD::AND) 3132 std::swap(N0, N1); 3133 3134 EVT OpVT = N0.getValueType(); 3135 if (N0.getOpcode() != ISD::AND || !OpVT.isInteger() || 3136 (Cond != ISD::SETEQ && Cond != ISD::SETNE)) 3137 return SDValue(); 3138 3139 SDValue X, Y; 3140 if (N0.getOperand(0) == N1) { 3141 X = N0.getOperand(1); 3142 Y = N0.getOperand(0); 3143 } else if (N0.getOperand(1) == N1) { 3144 X = N0.getOperand(0); 3145 Y = N0.getOperand(1); 3146 } else { 3147 return SDValue(); 3148 } 3149 3150 SelectionDAG &DAG = DCI.DAG; 3151 SDValue Zero = DAG.getConstant(0, DL, OpVT); 3152 if (DAG.isKnownToBeAPowerOfTwo(Y)) { 3153 // Simplify X & Y == Y to X & Y != 0 if Y has exactly one bit set. 3154 // Note that where Y is variable and is known to have at most one bit set 3155 // (for example, if it is Z & 1) we cannot do this; the expressions are not 3156 // equivalent when Y == 0. 3157 assert(OpVT.isInteger()); 3158 Cond = ISD::getSetCCInverse(Cond, OpVT); 3159 if (DCI.isBeforeLegalizeOps() || 3160 isCondCodeLegal(Cond, N0.getSimpleValueType())) 3161 return DAG.getSetCC(DL, VT, N0, Zero, Cond); 3162 } else if (N0.hasOneUse() && hasAndNotCompare(Y)) { 3163 // If the target supports an 'and-not' or 'and-complement' logic operation, 3164 // try to use that to make a comparison operation more efficient. 3165 // But don't do this transform if the mask is a single bit because there are 3166 // more efficient ways to deal with that case (for example, 'bt' on x86 or 3167 // 'rlwinm' on PPC). 3168 3169 // Bail out if the compare operand that we want to turn into a zero is 3170 // already a zero (otherwise, infinite loop). 3171 auto *YConst = dyn_cast<ConstantSDNode>(Y); 3172 if (YConst && YConst->isNullValue()) 3173 return SDValue(); 3174 3175 // Transform this into: ~X & Y == 0. 3176 SDValue NotX = DAG.getNOT(SDLoc(X), X, OpVT); 3177 SDValue NewAnd = DAG.getNode(ISD::AND, SDLoc(N0), OpVT, NotX, Y); 3178 return DAG.getSetCC(DL, VT, NewAnd, Zero, Cond); 3179 } 3180 3181 return SDValue(); 3182 } 3183 3184 /// There are multiple IR patterns that could be checking whether certain 3185 /// truncation of a signed number would be lossy or not. The pattern which is 3186 /// best at IR level, may not lower optimally. Thus, we want to unfold it. 3187 /// We are looking for the following pattern: (KeptBits is a constant) 3188 /// (add %x, (1 << (KeptBits-1))) srccond (1 << KeptBits) 3189 /// KeptBits won't be bitwidth(x), that will be constant-folded to true/false. 3190 /// KeptBits also can't be 1, that would have been folded to %x dstcond 0 3191 /// We will unfold it into the natural trunc+sext pattern: 3192 /// ((%x << C) a>> C) dstcond %x 3193 /// Where C = bitwidth(x) - KeptBits and C u< bitwidth(x) 3194 SDValue TargetLowering::optimizeSetCCOfSignedTruncationCheck( 3195 EVT SCCVT, SDValue N0, SDValue N1, ISD::CondCode Cond, DAGCombinerInfo &DCI, 3196 const SDLoc &DL) const { 3197 // We must be comparing with a constant. 3198 ConstantSDNode *C1; 3199 if (!(C1 = dyn_cast<ConstantSDNode>(N1))) 3200 return SDValue(); 3201 3202 // N0 should be: add %x, (1 << (KeptBits-1)) 3203 if (N0->getOpcode() != ISD::ADD) 3204 return SDValue(); 3205 3206 // And we must be 'add'ing a constant. 3207 ConstantSDNode *C01; 3208 if (!(C01 = dyn_cast<ConstantSDNode>(N0->getOperand(1)))) 3209 return SDValue(); 3210 3211 SDValue X = N0->getOperand(0); 3212 EVT XVT = X.getValueType(); 3213 3214 // Validate constants ... 3215 3216 APInt I1 = C1->getAPIntValue(); 3217 3218 ISD::CondCode NewCond; 3219 if (Cond == ISD::CondCode::SETULT) { 3220 NewCond = ISD::CondCode::SETEQ; 3221 } else if (Cond == ISD::CondCode::SETULE) { 3222 NewCond = ISD::CondCode::SETEQ; 3223 // But need to 'canonicalize' the constant. 3224 I1 += 1; 3225 } else if (Cond == ISD::CondCode::SETUGT) { 3226 NewCond = ISD::CondCode::SETNE; 3227 // But need to 'canonicalize' the constant. 3228 I1 += 1; 3229 } else if (Cond == ISD::CondCode::SETUGE) { 3230 NewCond = ISD::CondCode::SETNE; 3231 } else 3232 return SDValue(); 3233 3234 APInt I01 = C01->getAPIntValue(); 3235 3236 auto checkConstants = [&I1, &I01]() -> bool { 3237 // Both of them must be power-of-two, and the constant from setcc is bigger. 3238 return I1.ugt(I01) && I1.isPowerOf2() && I01.isPowerOf2(); 3239 }; 3240 3241 if (checkConstants()) { 3242 // Great, e.g. got icmp ult i16 (add i16 %x, 128), 256 3243 } else { 3244 // What if we invert constants? (and the target predicate) 3245 I1.negate(); 3246 I01.negate(); 3247 assert(XVT.isInteger()); 3248 NewCond = getSetCCInverse(NewCond, XVT); 3249 if (!checkConstants()) 3250 return SDValue(); 3251 // Great, e.g. got icmp uge i16 (add i16 %x, -128), -256 3252 } 3253 3254 // They are power-of-two, so which bit is set? 3255 const unsigned KeptBits = I1.logBase2(); 3256 const unsigned KeptBitsMinusOne = I01.logBase2(); 3257 3258 // Magic! 3259 if (KeptBits != (KeptBitsMinusOne + 1)) 3260 return SDValue(); 3261 assert(KeptBits > 0 && KeptBits < XVT.getSizeInBits() && "unreachable"); 3262 3263 // We don't want to do this in every single case. 3264 SelectionDAG &DAG = DCI.DAG; 3265 if (!DAG.getTargetLoweringInfo().shouldTransformSignedTruncationCheck( 3266 XVT, KeptBits)) 3267 return SDValue(); 3268 3269 const unsigned MaskedBits = XVT.getSizeInBits() - KeptBits; 3270 assert(MaskedBits > 0 && MaskedBits < XVT.getSizeInBits() && "unreachable"); 3271 3272 // Unfold into: ((%x << C) a>> C) cond %x 3273 // Where 'cond' will be either 'eq' or 'ne'. 3274 SDValue ShiftAmt = DAG.getConstant(MaskedBits, DL, XVT); 3275 SDValue T0 = DAG.getNode(ISD::SHL, DL, XVT, X, ShiftAmt); 3276 SDValue T1 = DAG.getNode(ISD::SRA, DL, XVT, T0, ShiftAmt); 3277 SDValue T2 = DAG.getSetCC(DL, SCCVT, T1, X, NewCond); 3278 3279 return T2; 3280 } 3281 3282 // (X & (C l>>/<< Y)) ==/!= 0 --> ((X <</l>> Y) & C) ==/!= 0 3283 SDValue TargetLowering::optimizeSetCCByHoistingAndByConstFromLogicalShift( 3284 EVT SCCVT, SDValue N0, SDValue N1C, ISD::CondCode Cond, 3285 DAGCombinerInfo &DCI, const SDLoc &DL) const { 3286 assert(isConstOrConstSplat(N1C) && 3287 isConstOrConstSplat(N1C)->getAPIntValue().isNullValue() && 3288 "Should be a comparison with 0."); 3289 assert((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 3290 "Valid only for [in]equality comparisons."); 3291 3292 unsigned NewShiftOpcode; 3293 SDValue X, C, Y; 3294 3295 SelectionDAG &DAG = DCI.DAG; 3296 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3297 3298 // Look for '(C l>>/<< Y)'. 3299 auto Match = [&NewShiftOpcode, &X, &C, &Y, &TLI, &DAG](SDValue V) { 3300 // The shift should be one-use. 3301 if (!V.hasOneUse()) 3302 return false; 3303 unsigned OldShiftOpcode = V.getOpcode(); 3304 switch (OldShiftOpcode) { 3305 case ISD::SHL: 3306 NewShiftOpcode = ISD::SRL; 3307 break; 3308 case ISD::SRL: 3309 NewShiftOpcode = ISD::SHL; 3310 break; 3311 default: 3312 return false; // must be a logical shift. 3313 } 3314 // We should be shifting a constant. 3315 // FIXME: best to use isConstantOrConstantVector(). 3316 C = V.getOperand(0); 3317 ConstantSDNode *CC = 3318 isConstOrConstSplat(C, /*AllowUndefs=*/true, /*AllowTruncation=*/true); 3319 if (!CC) 3320 return false; 3321 Y = V.getOperand(1); 3322 3323 ConstantSDNode *XC = 3324 isConstOrConstSplat(X, /*AllowUndefs=*/true, /*AllowTruncation=*/true); 3325 return TLI.shouldProduceAndByConstByHoistingConstFromShiftsLHSOfAnd( 3326 X, XC, CC, Y, OldShiftOpcode, NewShiftOpcode, DAG); 3327 }; 3328 3329 // LHS of comparison should be an one-use 'and'. 3330 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse()) 3331 return SDValue(); 3332 3333 X = N0.getOperand(0); 3334 SDValue Mask = N0.getOperand(1); 3335 3336 // 'and' is commutative! 3337 if (!Match(Mask)) { 3338 std::swap(X, Mask); 3339 if (!Match(Mask)) 3340 return SDValue(); 3341 } 3342 3343 EVT VT = X.getValueType(); 3344 3345 // Produce: 3346 // ((X 'OppositeShiftOpcode' Y) & C) Cond 0 3347 SDValue T0 = DAG.getNode(NewShiftOpcode, DL, VT, X, Y); 3348 SDValue T1 = DAG.getNode(ISD::AND, DL, VT, T0, C); 3349 SDValue T2 = DAG.getSetCC(DL, SCCVT, T1, N1C, Cond); 3350 return T2; 3351 } 3352 3353 /// Try to fold an equality comparison with a {add/sub/xor} binary operation as 3354 /// the 1st operand (N0). Callers are expected to swap the N0/N1 parameters to 3355 /// handle the commuted versions of these patterns. 3356 SDValue TargetLowering::foldSetCCWithBinOp(EVT VT, SDValue N0, SDValue N1, 3357 ISD::CondCode Cond, const SDLoc &DL, 3358 DAGCombinerInfo &DCI) const { 3359 unsigned BOpcode = N0.getOpcode(); 3360 assert((BOpcode == ISD::ADD || BOpcode == ISD::SUB || BOpcode == ISD::XOR) && 3361 "Unexpected binop"); 3362 assert((Cond == ISD::SETEQ || Cond == ISD::SETNE) && "Unexpected condcode"); 3363 3364 // (X + Y) == X --> Y == 0 3365 // (X - Y) == X --> Y == 0 3366 // (X ^ Y) == X --> Y == 0 3367 SelectionDAG &DAG = DCI.DAG; 3368 EVT OpVT = N0.getValueType(); 3369 SDValue X = N0.getOperand(0); 3370 SDValue Y = N0.getOperand(1); 3371 if (X == N1) 3372 return DAG.getSetCC(DL, VT, Y, DAG.getConstant(0, DL, OpVT), Cond); 3373 3374 if (Y != N1) 3375 return SDValue(); 3376 3377 // (X + Y) == Y --> X == 0 3378 // (X ^ Y) == Y --> X == 0 3379 if (BOpcode == ISD::ADD || BOpcode == ISD::XOR) 3380 return DAG.getSetCC(DL, VT, X, DAG.getConstant(0, DL, OpVT), Cond); 3381 3382 // The shift would not be valid if the operands are boolean (i1). 3383 if (!N0.hasOneUse() || OpVT.getScalarSizeInBits() == 1) 3384 return SDValue(); 3385 3386 // (X - Y) == Y --> X == Y << 1 3387 EVT ShiftVT = getShiftAmountTy(OpVT, DAG.getDataLayout(), 3388 !DCI.isBeforeLegalize()); 3389 SDValue One = DAG.getConstant(1, DL, ShiftVT); 3390 SDValue YShl1 = DAG.getNode(ISD::SHL, DL, N1.getValueType(), Y, One); 3391 if (!DCI.isCalledByLegalizer()) 3392 DCI.AddToWorklist(YShl1.getNode()); 3393 return DAG.getSetCC(DL, VT, X, YShl1, Cond); 3394 } 3395 3396 static SDValue simplifySetCCWithCTPOP(const TargetLowering &TLI, EVT VT, 3397 SDValue N0, const APInt &C1, 3398 ISD::CondCode Cond, const SDLoc &dl, 3399 SelectionDAG &DAG) { 3400 // Look through truncs that don't change the value of a ctpop. 3401 // FIXME: Add vector support? Need to be careful with setcc result type below. 3402 SDValue CTPOP = N0; 3403 if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse() && !VT.isVector() && 3404 N0.getScalarValueSizeInBits() > Log2_32(N0.getOperand(0).getScalarValueSizeInBits())) 3405 CTPOP = N0.getOperand(0); 3406 3407 if (CTPOP.getOpcode() != ISD::CTPOP || !CTPOP.hasOneUse()) 3408 return SDValue(); 3409 3410 EVT CTVT = CTPOP.getValueType(); 3411 SDValue CTOp = CTPOP.getOperand(0); 3412 3413 // If this is a vector CTPOP, keep the CTPOP if it is legal. 3414 // TODO: Should we check if CTPOP is legal(or custom) for scalars? 3415 if (VT.isVector() && TLI.isOperationLegal(ISD::CTPOP, CTVT)) 3416 return SDValue(); 3417 3418 // (ctpop x) u< 2 -> (x & x-1) == 0 3419 // (ctpop x) u> 1 -> (x & x-1) != 0 3420 if (Cond == ISD::SETULT || Cond == ISD::SETUGT) { 3421 unsigned CostLimit = TLI.getCustomCtpopCost(CTVT, Cond); 3422 if (C1.ugt(CostLimit + (Cond == ISD::SETULT))) 3423 return SDValue(); 3424 if (C1 == 0 && (Cond == ISD::SETULT)) 3425 return SDValue(); // This is handled elsewhere. 3426 3427 unsigned Passes = C1.getLimitedValue() - (Cond == ISD::SETULT); 3428 3429 SDValue NegOne = DAG.getAllOnesConstant(dl, CTVT); 3430 SDValue Result = CTOp; 3431 for (unsigned i = 0; i < Passes; i++) { 3432 SDValue Add = DAG.getNode(ISD::ADD, dl, CTVT, Result, NegOne); 3433 Result = DAG.getNode(ISD::AND, dl, CTVT, Result, Add); 3434 } 3435 ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE; 3436 return DAG.getSetCC(dl, VT, Result, DAG.getConstant(0, dl, CTVT), CC); 3437 } 3438 3439 // If ctpop is not supported, expand a power-of-2 comparison based on it. 3440 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && C1 == 1) { 3441 // For scalars, keep CTPOP if it is legal or custom. 3442 if (!VT.isVector() && TLI.isOperationLegalOrCustom(ISD::CTPOP, CTVT)) 3443 return SDValue(); 3444 // This is based on X86's custom lowering for CTPOP which produces more 3445 // instructions than the expansion here. 3446 3447 // (ctpop x) == 1 --> (x != 0) && ((x & x-1) == 0) 3448 // (ctpop x) != 1 --> (x == 0) || ((x & x-1) != 0) 3449 SDValue Zero = DAG.getConstant(0, dl, CTVT); 3450 SDValue NegOne = DAG.getAllOnesConstant(dl, CTVT); 3451 assert(CTVT.isInteger()); 3452 ISD::CondCode InvCond = ISD::getSetCCInverse(Cond, CTVT); 3453 SDValue Add = DAG.getNode(ISD::ADD, dl, CTVT, CTOp, NegOne); 3454 SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Add); 3455 SDValue LHS = DAG.getSetCC(dl, VT, CTOp, Zero, InvCond); 3456 SDValue RHS = DAG.getSetCC(dl, VT, And, Zero, Cond); 3457 unsigned LogicOpcode = Cond == ISD::SETEQ ? ISD::AND : ISD::OR; 3458 return DAG.getNode(LogicOpcode, dl, VT, LHS, RHS); 3459 } 3460 3461 return SDValue(); 3462 } 3463 3464 /// Try to simplify a setcc built with the specified operands and cc. If it is 3465 /// unable to simplify it, return a null SDValue. 3466 SDValue TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1, 3467 ISD::CondCode Cond, bool foldBooleans, 3468 DAGCombinerInfo &DCI, 3469 const SDLoc &dl) const { 3470 SelectionDAG &DAG = DCI.DAG; 3471 const DataLayout &Layout = DAG.getDataLayout(); 3472 EVT OpVT = N0.getValueType(); 3473 3474 // Constant fold or commute setcc. 3475 if (SDValue Fold = DAG.FoldSetCC(VT, N0, N1, Cond, dl)) 3476 return Fold; 3477 3478 // Ensure that the constant occurs on the RHS and fold constant comparisons. 3479 // TODO: Handle non-splat vector constants. All undef causes trouble. 3480 // FIXME: We can't yet fold constant scalable vector splats, so avoid an 3481 // infinite loop here when we encounter one. 3482 ISD::CondCode SwappedCC = ISD::getSetCCSwappedOperands(Cond); 3483 if (isConstOrConstSplat(N0) && 3484 (!OpVT.isScalableVector() || !isConstOrConstSplat(N1)) && 3485 (DCI.isBeforeLegalizeOps() || 3486 isCondCodeLegal(SwappedCC, N0.getSimpleValueType()))) 3487 return DAG.getSetCC(dl, VT, N1, N0, SwappedCC); 3488 3489 // If we have a subtract with the same 2 non-constant operands as this setcc 3490 // -- but in reverse order -- then try to commute the operands of this setcc 3491 // to match. A matching pair of setcc (cmp) and sub may be combined into 1 3492 // instruction on some targets. 3493 if (!isConstOrConstSplat(N0) && !isConstOrConstSplat(N1) && 3494 (DCI.isBeforeLegalizeOps() || 3495 isCondCodeLegal(SwappedCC, N0.getSimpleValueType())) && 3496 DAG.doesNodeExist(ISD::SUB, DAG.getVTList(OpVT), {N1, N0}) && 3497 !DAG.doesNodeExist(ISD::SUB, DAG.getVTList(OpVT), {N0, N1})) 3498 return DAG.getSetCC(dl, VT, N1, N0, SwappedCC); 3499 3500 if (auto *N1C = isConstOrConstSplat(N1)) { 3501 const APInt &C1 = N1C->getAPIntValue(); 3502 3503 // Optimize some CTPOP cases. 3504 if (SDValue V = simplifySetCCWithCTPOP(*this, VT, N0, C1, Cond, dl, DAG)) 3505 return V; 3506 3507 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an 3508 // equality comparison, then we're just comparing whether X itself is 3509 // zero. 3510 if (N0.getOpcode() == ISD::SRL && (C1.isNullValue() || C1.isOneValue()) && 3511 N0.getOperand(0).getOpcode() == ISD::CTLZ && 3512 isPowerOf2_32(N0.getScalarValueSizeInBits())) { 3513 if (ConstantSDNode *ShAmt = isConstOrConstSplat(N0.getOperand(1))) { 3514 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 3515 ShAmt->getAPIntValue() == Log2_32(N0.getScalarValueSizeInBits())) { 3516 if ((C1 == 0) == (Cond == ISD::SETEQ)) { 3517 // (srl (ctlz x), 5) == 0 -> X != 0 3518 // (srl (ctlz x), 5) != 1 -> X != 0 3519 Cond = ISD::SETNE; 3520 } else { 3521 // (srl (ctlz x), 5) != 0 -> X == 0 3522 // (srl (ctlz x), 5) == 1 -> X == 0 3523 Cond = ISD::SETEQ; 3524 } 3525 SDValue Zero = DAG.getConstant(0, dl, N0.getValueType()); 3526 return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0), Zero, 3527 Cond); 3528 } 3529 } 3530 } 3531 } 3532 3533 // FIXME: Support vectors. 3534 if (auto *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) { 3535 const APInt &C1 = N1C->getAPIntValue(); 3536 3537 // (zext x) == C --> x == (trunc C) 3538 // (sext x) == C --> x == (trunc C) 3539 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 3540 DCI.isBeforeLegalize() && N0->hasOneUse()) { 3541 unsigned MinBits = N0.getValueSizeInBits(); 3542 SDValue PreExt; 3543 bool Signed = false; 3544 if (N0->getOpcode() == ISD::ZERO_EXTEND) { 3545 // ZExt 3546 MinBits = N0->getOperand(0).getValueSizeInBits(); 3547 PreExt = N0->getOperand(0); 3548 } else if (N0->getOpcode() == ISD::AND) { 3549 // DAGCombine turns costly ZExts into ANDs 3550 if (auto *C = dyn_cast<ConstantSDNode>(N0->getOperand(1))) 3551 if ((C->getAPIntValue()+1).isPowerOf2()) { 3552 MinBits = C->getAPIntValue().countTrailingOnes(); 3553 PreExt = N0->getOperand(0); 3554 } 3555 } else if (N0->getOpcode() == ISD::SIGN_EXTEND) { 3556 // SExt 3557 MinBits = N0->getOperand(0).getValueSizeInBits(); 3558 PreExt = N0->getOperand(0); 3559 Signed = true; 3560 } else if (auto *LN0 = dyn_cast<LoadSDNode>(N0)) { 3561 // ZEXTLOAD / SEXTLOAD 3562 if (LN0->getExtensionType() == ISD::ZEXTLOAD) { 3563 MinBits = LN0->getMemoryVT().getSizeInBits(); 3564 PreExt = N0; 3565 } else if (LN0->getExtensionType() == ISD::SEXTLOAD) { 3566 Signed = true; 3567 MinBits = LN0->getMemoryVT().getSizeInBits(); 3568 PreExt = N0; 3569 } 3570 } 3571 3572 // Figure out how many bits we need to preserve this constant. 3573 unsigned ReqdBits = Signed ? 3574 C1.getBitWidth() - C1.getNumSignBits() + 1 : 3575 C1.getActiveBits(); 3576 3577 // Make sure we're not losing bits from the constant. 3578 if (MinBits > 0 && 3579 MinBits < C1.getBitWidth() && 3580 MinBits >= ReqdBits) { 3581 EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits); 3582 if (isTypeDesirableForOp(ISD::SETCC, MinVT)) { 3583 // Will get folded away. 3584 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreExt); 3585 if (MinBits == 1 && C1 == 1) 3586 // Invert the condition. 3587 return DAG.getSetCC(dl, VT, Trunc, DAG.getConstant(0, dl, MVT::i1), 3588 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ); 3589 SDValue C = DAG.getConstant(C1.trunc(MinBits), dl, MinVT); 3590 return DAG.getSetCC(dl, VT, Trunc, C, Cond); 3591 } 3592 3593 // If truncating the setcc operands is not desirable, we can still 3594 // simplify the expression in some cases: 3595 // setcc ([sz]ext (setcc x, y, cc)), 0, setne) -> setcc (x, y, cc) 3596 // setcc ([sz]ext (setcc x, y, cc)), 0, seteq) -> setcc (x, y, inv(cc)) 3597 // setcc (zext (setcc x, y, cc)), 1, setne) -> setcc (x, y, inv(cc)) 3598 // setcc (zext (setcc x, y, cc)), 1, seteq) -> setcc (x, y, cc) 3599 // setcc (sext (setcc x, y, cc)), -1, setne) -> setcc (x, y, inv(cc)) 3600 // setcc (sext (setcc x, y, cc)), -1, seteq) -> setcc (x, y, cc) 3601 SDValue TopSetCC = N0->getOperand(0); 3602 unsigned N0Opc = N0->getOpcode(); 3603 bool SExt = (N0Opc == ISD::SIGN_EXTEND); 3604 if (TopSetCC.getValueType() == MVT::i1 && VT == MVT::i1 && 3605 TopSetCC.getOpcode() == ISD::SETCC && 3606 (N0Opc == ISD::ZERO_EXTEND || N0Opc == ISD::SIGN_EXTEND) && 3607 (isConstFalseVal(N1C) || 3608 isExtendedTrueVal(N1C, N0->getValueType(0), SExt))) { 3609 3610 bool Inverse = (N1C->isNullValue() && Cond == ISD::SETEQ) || 3611 (!N1C->isNullValue() && Cond == ISD::SETNE); 3612 3613 if (!Inverse) 3614 return TopSetCC; 3615 3616 ISD::CondCode InvCond = ISD::getSetCCInverse( 3617 cast<CondCodeSDNode>(TopSetCC.getOperand(2))->get(), 3618 TopSetCC.getOperand(0).getValueType()); 3619 return DAG.getSetCC(dl, VT, TopSetCC.getOperand(0), 3620 TopSetCC.getOperand(1), 3621 InvCond); 3622 } 3623 } 3624 } 3625 3626 // If the LHS is '(and load, const)', the RHS is 0, the test is for 3627 // equality or unsigned, and all 1 bits of the const are in the same 3628 // partial word, see if we can shorten the load. 3629 if (DCI.isBeforeLegalize() && 3630 !ISD::isSignedIntSetCC(Cond) && 3631 N0.getOpcode() == ISD::AND && C1 == 0 && 3632 N0.getNode()->hasOneUse() && 3633 isa<LoadSDNode>(N0.getOperand(0)) && 3634 N0.getOperand(0).getNode()->hasOneUse() && 3635 isa<ConstantSDNode>(N0.getOperand(1))) { 3636 LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0)); 3637 APInt bestMask; 3638 unsigned bestWidth = 0, bestOffset = 0; 3639 if (Lod->isSimple() && Lod->isUnindexed()) { 3640 unsigned origWidth = N0.getValueSizeInBits(); 3641 unsigned maskWidth = origWidth; 3642 // We can narrow (e.g.) 16-bit extending loads on 32-bit target to 3643 // 8 bits, but have to be careful... 3644 if (Lod->getExtensionType() != ISD::NON_EXTLOAD) 3645 origWidth = Lod->getMemoryVT().getSizeInBits(); 3646 const APInt &Mask = N0.getConstantOperandAPInt(1); 3647 for (unsigned width = origWidth / 2; width>=8; width /= 2) { 3648 APInt newMask = APInt::getLowBitsSet(maskWidth, width); 3649 for (unsigned offset=0; offset<origWidth/width; offset++) { 3650 if (Mask.isSubsetOf(newMask)) { 3651 if (Layout.isLittleEndian()) 3652 bestOffset = (uint64_t)offset * (width/8); 3653 else 3654 bestOffset = (origWidth/width - offset - 1) * (width/8); 3655 bestMask = Mask.lshr(offset * (width/8) * 8); 3656 bestWidth = width; 3657 break; 3658 } 3659 newMask <<= width; 3660 } 3661 } 3662 } 3663 if (bestWidth) { 3664 EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth); 3665 if (newVT.isRound() && 3666 shouldReduceLoadWidth(Lod, ISD::NON_EXTLOAD, newVT)) { 3667 SDValue Ptr = Lod->getBasePtr(); 3668 if (bestOffset != 0) 3669 Ptr = 3670 DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(bestOffset), dl); 3671 SDValue NewLoad = 3672 DAG.getLoad(newVT, dl, Lod->getChain(), Ptr, 3673 Lod->getPointerInfo().getWithOffset(bestOffset), 3674 Lod->getOriginalAlign()); 3675 return DAG.getSetCC(dl, VT, 3676 DAG.getNode(ISD::AND, dl, newVT, NewLoad, 3677 DAG.getConstant(bestMask.trunc(bestWidth), 3678 dl, newVT)), 3679 DAG.getConstant(0LL, dl, newVT), Cond); 3680 } 3681 } 3682 } 3683 3684 // If the LHS is a ZERO_EXTEND, perform the comparison on the input. 3685 if (N0.getOpcode() == ISD::ZERO_EXTEND) { 3686 unsigned InSize = N0.getOperand(0).getValueSizeInBits(); 3687 3688 // If the comparison constant has bits in the upper part, the 3689 // zero-extended value could never match. 3690 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(), 3691 C1.getBitWidth() - InSize))) { 3692 switch (Cond) { 3693 case ISD::SETUGT: 3694 case ISD::SETUGE: 3695 case ISD::SETEQ: 3696 return DAG.getConstant(0, dl, VT); 3697 case ISD::SETULT: 3698 case ISD::SETULE: 3699 case ISD::SETNE: 3700 return DAG.getConstant(1, dl, VT); 3701 case ISD::SETGT: 3702 case ISD::SETGE: 3703 // True if the sign bit of C1 is set. 3704 return DAG.getConstant(C1.isNegative(), dl, VT); 3705 case ISD::SETLT: 3706 case ISD::SETLE: 3707 // True if the sign bit of C1 isn't set. 3708 return DAG.getConstant(C1.isNonNegative(), dl, VT); 3709 default: 3710 break; 3711 } 3712 } 3713 3714 // Otherwise, we can perform the comparison with the low bits. 3715 switch (Cond) { 3716 case ISD::SETEQ: 3717 case ISD::SETNE: 3718 case ISD::SETUGT: 3719 case ISD::SETUGE: 3720 case ISD::SETULT: 3721 case ISD::SETULE: { 3722 EVT newVT = N0.getOperand(0).getValueType(); 3723 if (DCI.isBeforeLegalizeOps() || 3724 (isOperationLegal(ISD::SETCC, newVT) && 3725 isCondCodeLegal(Cond, newVT.getSimpleVT()))) { 3726 EVT NewSetCCVT = getSetCCResultType(Layout, *DAG.getContext(), newVT); 3727 SDValue NewConst = DAG.getConstant(C1.trunc(InSize), dl, newVT); 3728 3729 SDValue NewSetCC = DAG.getSetCC(dl, NewSetCCVT, N0.getOperand(0), 3730 NewConst, Cond); 3731 return DAG.getBoolExtOrTrunc(NewSetCC, dl, VT, N0.getValueType()); 3732 } 3733 break; 3734 } 3735 default: 3736 break; // todo, be more careful with signed comparisons 3737 } 3738 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && 3739 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { 3740 EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT(); 3741 unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits(); 3742 EVT ExtDstTy = N0.getValueType(); 3743 unsigned ExtDstTyBits = ExtDstTy.getSizeInBits(); 3744 3745 // If the constant doesn't fit into the number of bits for the source of 3746 // the sign extension, it is impossible for both sides to be equal. 3747 if (C1.getMinSignedBits() > ExtSrcTyBits) 3748 return DAG.getBoolConstant(Cond == ISD::SETNE, dl, VT, OpVT); 3749 3750 assert(ExtDstTy == N0.getOperand(0).getValueType() && 3751 ExtDstTy != ExtSrcTy && "Unexpected types!"); 3752 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits); 3753 SDValue ZextOp = DAG.getNode(ISD::AND, dl, ExtDstTy, N0.getOperand(0), 3754 DAG.getConstant(Imm, dl, ExtDstTy)); 3755 if (!DCI.isCalledByLegalizer()) 3756 DCI.AddToWorklist(ZextOp.getNode()); 3757 // Otherwise, make this a use of a zext. 3758 return DAG.getSetCC(dl, VT, ZextOp, 3759 DAG.getConstant(C1 & Imm, dl, ExtDstTy), Cond); 3760 } else if ((N1C->isNullValue() || N1C->isOne()) && 3761 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { 3762 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC 3763 if (N0.getOpcode() == ISD::SETCC && 3764 isTypeLegal(VT) && VT.bitsLE(N0.getValueType()) && 3765 (N0.getValueType() == MVT::i1 || 3766 getBooleanContents(N0.getOperand(0).getValueType()) == 3767 ZeroOrOneBooleanContent)) { 3768 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (!N1C->isOne()); 3769 if (TrueWhenTrue) 3770 return DAG.getNode(ISD::TRUNCATE, dl, VT, N0); 3771 // Invert the condition. 3772 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get(); 3773 CC = ISD::getSetCCInverse(CC, N0.getOperand(0).getValueType()); 3774 if (DCI.isBeforeLegalizeOps() || 3775 isCondCodeLegal(CC, N0.getOperand(0).getSimpleValueType())) 3776 return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC); 3777 } 3778 3779 if ((N0.getOpcode() == ISD::XOR || 3780 (N0.getOpcode() == ISD::AND && 3781 N0.getOperand(0).getOpcode() == ISD::XOR && 3782 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) && 3783 isOneConstant(N0.getOperand(1))) { 3784 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We 3785 // can only do this if the top bits are known zero. 3786 unsigned BitWidth = N0.getValueSizeInBits(); 3787 if (DAG.MaskedValueIsZero(N0, 3788 APInt::getHighBitsSet(BitWidth, 3789 BitWidth-1))) { 3790 // Okay, get the un-inverted input value. 3791 SDValue Val; 3792 if (N0.getOpcode() == ISD::XOR) { 3793 Val = N0.getOperand(0); 3794 } else { 3795 assert(N0.getOpcode() == ISD::AND && 3796 N0.getOperand(0).getOpcode() == ISD::XOR); 3797 // ((X^1)&1)^1 -> X & 1 3798 Val = DAG.getNode(ISD::AND, dl, N0.getValueType(), 3799 N0.getOperand(0).getOperand(0), 3800 N0.getOperand(1)); 3801 } 3802 3803 return DAG.getSetCC(dl, VT, Val, N1, 3804 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ); 3805 } 3806 } else if (N1C->isOne()) { 3807 SDValue Op0 = N0; 3808 if (Op0.getOpcode() == ISD::TRUNCATE) 3809 Op0 = Op0.getOperand(0); 3810 3811 if ((Op0.getOpcode() == ISD::XOR) && 3812 Op0.getOperand(0).getOpcode() == ISD::SETCC && 3813 Op0.getOperand(1).getOpcode() == ISD::SETCC) { 3814 SDValue XorLHS = Op0.getOperand(0); 3815 SDValue XorRHS = Op0.getOperand(1); 3816 // Ensure that the input setccs return an i1 type or 0/1 value. 3817 if (Op0.getValueType() == MVT::i1 || 3818 (getBooleanContents(XorLHS.getOperand(0).getValueType()) == 3819 ZeroOrOneBooleanContent && 3820 getBooleanContents(XorRHS.getOperand(0).getValueType()) == 3821 ZeroOrOneBooleanContent)) { 3822 // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc) 3823 Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ; 3824 return DAG.getSetCC(dl, VT, XorLHS, XorRHS, Cond); 3825 } 3826 } 3827 if (Op0.getOpcode() == ISD::AND && isOneConstant(Op0.getOperand(1))) { 3828 // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0. 3829 if (Op0.getValueType().bitsGT(VT)) 3830 Op0 = DAG.getNode(ISD::AND, dl, VT, 3831 DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)), 3832 DAG.getConstant(1, dl, VT)); 3833 else if (Op0.getValueType().bitsLT(VT)) 3834 Op0 = DAG.getNode(ISD::AND, dl, VT, 3835 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)), 3836 DAG.getConstant(1, dl, VT)); 3837 3838 return DAG.getSetCC(dl, VT, Op0, 3839 DAG.getConstant(0, dl, Op0.getValueType()), 3840 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ); 3841 } 3842 if (Op0.getOpcode() == ISD::AssertZext && 3843 cast<VTSDNode>(Op0.getOperand(1))->getVT() == MVT::i1) 3844 return DAG.getSetCC(dl, VT, Op0, 3845 DAG.getConstant(0, dl, Op0.getValueType()), 3846 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ); 3847 } 3848 } 3849 3850 // Given: 3851 // icmp eq/ne (urem %x, %y), 0 3852 // Iff %x has 0 or 1 bits set, and %y has at least 2 bits set, omit 'urem': 3853 // icmp eq/ne %x, 0 3854 if (N0.getOpcode() == ISD::UREM && N1C->isNullValue() && 3855 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { 3856 KnownBits XKnown = DAG.computeKnownBits(N0.getOperand(0)); 3857 KnownBits YKnown = DAG.computeKnownBits(N0.getOperand(1)); 3858 if (XKnown.countMaxPopulation() == 1 && YKnown.countMinPopulation() >= 2) 3859 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1, Cond); 3860 } 3861 3862 if (SDValue V = 3863 optimizeSetCCOfSignedTruncationCheck(VT, N0, N1, Cond, DCI, dl)) 3864 return V; 3865 } 3866 3867 // These simplifications apply to splat vectors as well. 3868 // TODO: Handle more splat vector cases. 3869 if (auto *N1C = isConstOrConstSplat(N1)) { 3870 const APInt &C1 = N1C->getAPIntValue(); 3871 3872 APInt MinVal, MaxVal; 3873 unsigned OperandBitSize = N1C->getValueType(0).getScalarSizeInBits(); 3874 if (ISD::isSignedIntSetCC(Cond)) { 3875 MinVal = APInt::getSignedMinValue(OperandBitSize); 3876 MaxVal = APInt::getSignedMaxValue(OperandBitSize); 3877 } else { 3878 MinVal = APInt::getMinValue(OperandBitSize); 3879 MaxVal = APInt::getMaxValue(OperandBitSize); 3880 } 3881 3882 // Canonicalize GE/LE comparisons to use GT/LT comparisons. 3883 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { 3884 // X >= MIN --> true 3885 if (C1 == MinVal) 3886 return DAG.getBoolConstant(true, dl, VT, OpVT); 3887 3888 if (!VT.isVector()) { // TODO: Support this for vectors. 3889 // X >= C0 --> X > (C0 - 1) 3890 APInt C = C1 - 1; 3891 ISD::CondCode NewCC = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT; 3892 if ((DCI.isBeforeLegalizeOps() || 3893 isCondCodeLegal(NewCC, VT.getSimpleVT())) && 3894 (!N1C->isOpaque() || (C.getBitWidth() <= 64 && 3895 isLegalICmpImmediate(C.getSExtValue())))) { 3896 return DAG.getSetCC(dl, VT, N0, 3897 DAG.getConstant(C, dl, N1.getValueType()), 3898 NewCC); 3899 } 3900 } 3901 } 3902 3903 if (Cond == ISD::SETLE || Cond == ISD::SETULE) { 3904 // X <= MAX --> true 3905 if (C1 == MaxVal) 3906 return DAG.getBoolConstant(true, dl, VT, OpVT); 3907 3908 // X <= C0 --> X < (C0 + 1) 3909 if (!VT.isVector()) { // TODO: Support this for vectors. 3910 APInt C = C1 + 1; 3911 ISD::CondCode NewCC = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT; 3912 if ((DCI.isBeforeLegalizeOps() || 3913 isCondCodeLegal(NewCC, VT.getSimpleVT())) && 3914 (!N1C->isOpaque() || (C.getBitWidth() <= 64 && 3915 isLegalICmpImmediate(C.getSExtValue())))) { 3916 return DAG.getSetCC(dl, VT, N0, 3917 DAG.getConstant(C, dl, N1.getValueType()), 3918 NewCC); 3919 } 3920 } 3921 } 3922 3923 if (Cond == ISD::SETLT || Cond == ISD::SETULT) { 3924 if (C1 == MinVal) 3925 return DAG.getBoolConstant(false, dl, VT, OpVT); // X < MIN --> false 3926 3927 // TODO: Support this for vectors after legalize ops. 3928 if (!VT.isVector() || DCI.isBeforeLegalizeOps()) { 3929 // Canonicalize setlt X, Max --> setne X, Max 3930 if (C1 == MaxVal) 3931 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE); 3932 3933 // If we have setult X, 1, turn it into seteq X, 0 3934 if (C1 == MinVal+1) 3935 return DAG.getSetCC(dl, VT, N0, 3936 DAG.getConstant(MinVal, dl, N0.getValueType()), 3937 ISD::SETEQ); 3938 } 3939 } 3940 3941 if (Cond == ISD::SETGT || Cond == ISD::SETUGT) { 3942 if (C1 == MaxVal) 3943 return DAG.getBoolConstant(false, dl, VT, OpVT); // X > MAX --> false 3944 3945 // TODO: Support this for vectors after legalize ops. 3946 if (!VT.isVector() || DCI.isBeforeLegalizeOps()) { 3947 // Canonicalize setgt X, Min --> setne X, Min 3948 if (C1 == MinVal) 3949 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE); 3950 3951 // If we have setugt X, Max-1, turn it into seteq X, Max 3952 if (C1 == MaxVal-1) 3953 return DAG.getSetCC(dl, VT, N0, 3954 DAG.getConstant(MaxVal, dl, N0.getValueType()), 3955 ISD::SETEQ); 3956 } 3957 } 3958 3959 if (Cond == ISD::SETEQ || Cond == ISD::SETNE) { 3960 // (X & (C l>>/<< Y)) ==/!= 0 --> ((X <</l>> Y) & C) ==/!= 0 3961 if (C1.isNullValue()) 3962 if (SDValue CC = optimizeSetCCByHoistingAndByConstFromLogicalShift( 3963 VT, N0, N1, Cond, DCI, dl)) 3964 return CC; 3965 3966 // For all/any comparisons, replace or(x,shl(y,bw/2)) with and/or(x,y). 3967 // For example, when high 32-bits of i64 X are known clear: 3968 // all bits clear: (X | (Y<<32)) == 0 --> (X | Y) == 0 3969 // all bits set: (X | (Y<<32)) == -1 --> (X & Y) == -1 3970 bool CmpZero = N1C->getAPIntValue().isNullValue(); 3971 bool CmpNegOne = N1C->getAPIntValue().isAllOnesValue(); 3972 if ((CmpZero || CmpNegOne) && N0.hasOneUse()) { 3973 // Match or(lo,shl(hi,bw/2)) pattern. 3974 auto IsConcat = [&](SDValue V, SDValue &Lo, SDValue &Hi) { 3975 unsigned EltBits = V.getScalarValueSizeInBits(); 3976 if (V.getOpcode() != ISD::OR || (EltBits % 2) != 0) 3977 return false; 3978 SDValue LHS = V.getOperand(0); 3979 SDValue RHS = V.getOperand(1); 3980 APInt HiBits = APInt::getHighBitsSet(EltBits, EltBits / 2); 3981 // Unshifted element must have zero upperbits. 3982 if (RHS.getOpcode() == ISD::SHL && 3983 isa<ConstantSDNode>(RHS.getOperand(1)) && 3984 RHS.getConstantOperandAPInt(1) == (EltBits / 2) && 3985 DAG.MaskedValueIsZero(LHS, HiBits)) { 3986 Lo = LHS; 3987 Hi = RHS.getOperand(0); 3988 return true; 3989 } 3990 if (LHS.getOpcode() == ISD::SHL && 3991 isa<ConstantSDNode>(LHS.getOperand(1)) && 3992 LHS.getConstantOperandAPInt(1) == (EltBits / 2) && 3993 DAG.MaskedValueIsZero(RHS, HiBits)) { 3994 Lo = RHS; 3995 Hi = LHS.getOperand(0); 3996 return true; 3997 } 3998 return false; 3999 }; 4000 4001 auto MergeConcat = [&](SDValue Lo, SDValue Hi) { 4002 unsigned EltBits = N0.getScalarValueSizeInBits(); 4003 unsigned HalfBits = EltBits / 2; 4004 APInt HiBits = APInt::getHighBitsSet(EltBits, HalfBits); 4005 SDValue LoBits = DAG.getConstant(~HiBits, dl, OpVT); 4006 SDValue HiMask = DAG.getNode(ISD::AND, dl, OpVT, Hi, LoBits); 4007 SDValue NewN0 = 4008 DAG.getNode(CmpZero ? ISD::OR : ISD::AND, dl, OpVT, Lo, HiMask); 4009 SDValue NewN1 = CmpZero ? DAG.getConstant(0, dl, OpVT) : LoBits; 4010 return DAG.getSetCC(dl, VT, NewN0, NewN1, Cond); 4011 }; 4012 4013 SDValue Lo, Hi; 4014 if (IsConcat(N0, Lo, Hi)) 4015 return MergeConcat(Lo, Hi); 4016 4017 if (N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR) { 4018 SDValue Lo0, Lo1, Hi0, Hi1; 4019 if (IsConcat(N0.getOperand(0), Lo0, Hi0) && 4020 IsConcat(N0.getOperand(1), Lo1, Hi1)) { 4021 return MergeConcat(DAG.getNode(N0.getOpcode(), dl, OpVT, Lo0, Lo1), 4022 DAG.getNode(N0.getOpcode(), dl, OpVT, Hi0, Hi1)); 4023 } 4024 } 4025 } 4026 } 4027 4028 // If we have "setcc X, C0", check to see if we can shrink the immediate 4029 // by changing cc. 4030 // TODO: Support this for vectors after legalize ops. 4031 if (!VT.isVector() || DCI.isBeforeLegalizeOps()) { 4032 // SETUGT X, SINTMAX -> SETLT X, 0 4033 // SETUGE X, SINTMIN -> SETLT X, 0 4034 if ((Cond == ISD::SETUGT && C1.isMaxSignedValue()) || 4035 (Cond == ISD::SETUGE && C1.isMinSignedValue())) 4036 return DAG.getSetCC(dl, VT, N0, 4037 DAG.getConstant(0, dl, N1.getValueType()), 4038 ISD::SETLT); 4039 4040 // SETULT X, SINTMIN -> SETGT X, -1 4041 // SETULE X, SINTMAX -> SETGT X, -1 4042 if ((Cond == ISD::SETULT && C1.isMinSignedValue()) || 4043 (Cond == ISD::SETULE && C1.isMaxSignedValue())) 4044 return DAG.getSetCC(dl, VT, N0, 4045 DAG.getAllOnesConstant(dl, N1.getValueType()), 4046 ISD::SETGT); 4047 } 4048 } 4049 4050 // Back to non-vector simplifications. 4051 // TODO: Can we do these for vector splats? 4052 if (auto *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) { 4053 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4054 const APInt &C1 = N1C->getAPIntValue(); 4055 EVT ShValTy = N0.getValueType(); 4056 4057 // Fold bit comparisons when we can. This will result in an 4058 // incorrect value when boolean false is negative one, unless 4059 // the bitsize is 1 in which case the false value is the same 4060 // in practice regardless of the representation. 4061 if ((VT.getSizeInBits() == 1 || 4062 getBooleanContents(N0.getValueType()) == ZeroOrOneBooleanContent) && 4063 (Cond == ISD::SETEQ || Cond == ISD::SETNE) && 4064 (VT == ShValTy || (isTypeLegal(VT) && VT.bitsLE(ShValTy))) && 4065 N0.getOpcode() == ISD::AND) { 4066 if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { 4067 EVT ShiftTy = 4068 getShiftAmountTy(ShValTy, Layout, !DCI.isBeforeLegalize()); 4069 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 4070 // Perform the xform if the AND RHS is a single bit. 4071 unsigned ShCt = AndRHS->getAPIntValue().logBase2(); 4072 if (AndRHS->getAPIntValue().isPowerOf2() && 4073 !TLI.shouldAvoidTransformToShift(ShValTy, ShCt)) { 4074 return DAG.getNode(ISD::TRUNCATE, dl, VT, 4075 DAG.getNode(ISD::SRL, dl, ShValTy, N0, 4076 DAG.getConstant(ShCt, dl, ShiftTy))); 4077 } 4078 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) { 4079 // (X & 8) == 8 --> (X & 8) >> 3 4080 // Perform the xform if C1 is a single bit. 4081 unsigned ShCt = C1.logBase2(); 4082 if (C1.isPowerOf2() && 4083 !TLI.shouldAvoidTransformToShift(ShValTy, ShCt)) { 4084 return DAG.getNode(ISD::TRUNCATE, dl, VT, 4085 DAG.getNode(ISD::SRL, dl, ShValTy, N0, 4086 DAG.getConstant(ShCt, dl, ShiftTy))); 4087 } 4088 } 4089 } 4090 } 4091 4092 if (C1.getMinSignedBits() <= 64 && 4093 !isLegalICmpImmediate(C1.getSExtValue())) { 4094 EVT ShiftTy = getShiftAmountTy(ShValTy, Layout, !DCI.isBeforeLegalize()); 4095 // (X & -256) == 256 -> (X >> 8) == 1 4096 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 4097 N0.getOpcode() == ISD::AND && N0.hasOneUse()) { 4098 if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { 4099 const APInt &AndRHSC = AndRHS->getAPIntValue(); 4100 if ((-AndRHSC).isPowerOf2() && (AndRHSC & C1) == C1) { 4101 unsigned ShiftBits = AndRHSC.countTrailingZeros(); 4102 if (!TLI.shouldAvoidTransformToShift(ShValTy, ShiftBits)) { 4103 SDValue Shift = 4104 DAG.getNode(ISD::SRL, dl, ShValTy, N0.getOperand(0), 4105 DAG.getConstant(ShiftBits, dl, ShiftTy)); 4106 SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), dl, ShValTy); 4107 return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond); 4108 } 4109 } 4110 } 4111 } else if (Cond == ISD::SETULT || Cond == ISD::SETUGE || 4112 Cond == ISD::SETULE || Cond == ISD::SETUGT) { 4113 bool AdjOne = (Cond == ISD::SETULE || Cond == ISD::SETUGT); 4114 // X < 0x100000000 -> (X >> 32) < 1 4115 // X >= 0x100000000 -> (X >> 32) >= 1 4116 // X <= 0x0ffffffff -> (X >> 32) < 1 4117 // X > 0x0ffffffff -> (X >> 32) >= 1 4118 unsigned ShiftBits; 4119 APInt NewC = C1; 4120 ISD::CondCode NewCond = Cond; 4121 if (AdjOne) { 4122 ShiftBits = C1.countTrailingOnes(); 4123 NewC = NewC + 1; 4124 NewCond = (Cond == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 4125 } else { 4126 ShiftBits = C1.countTrailingZeros(); 4127 } 4128 NewC.lshrInPlace(ShiftBits); 4129 if (ShiftBits && NewC.getMinSignedBits() <= 64 && 4130 isLegalICmpImmediate(NewC.getSExtValue()) && 4131 !TLI.shouldAvoidTransformToShift(ShValTy, ShiftBits)) { 4132 SDValue Shift = DAG.getNode(ISD::SRL, dl, ShValTy, N0, 4133 DAG.getConstant(ShiftBits, dl, ShiftTy)); 4134 SDValue CmpRHS = DAG.getConstant(NewC, dl, ShValTy); 4135 return DAG.getSetCC(dl, VT, Shift, CmpRHS, NewCond); 4136 } 4137 } 4138 } 4139 } 4140 4141 if (!isa<ConstantFPSDNode>(N0) && isa<ConstantFPSDNode>(N1)) { 4142 auto *CFP = cast<ConstantFPSDNode>(N1); 4143 assert(!CFP->getValueAPF().isNaN() && "Unexpected NaN value"); 4144 4145 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the 4146 // constant if knowing that the operand is non-nan is enough. We prefer to 4147 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to 4148 // materialize 0.0. 4149 if (Cond == ISD::SETO || Cond == ISD::SETUO) 4150 return DAG.getSetCC(dl, VT, N0, N0, Cond); 4151 4152 // setcc (fneg x), C -> setcc swap(pred) x, -C 4153 if (N0.getOpcode() == ISD::FNEG) { 4154 ISD::CondCode SwapCond = ISD::getSetCCSwappedOperands(Cond); 4155 if (DCI.isBeforeLegalizeOps() || 4156 isCondCodeLegal(SwapCond, N0.getSimpleValueType())) { 4157 SDValue NegN1 = DAG.getNode(ISD::FNEG, dl, N0.getValueType(), N1); 4158 return DAG.getSetCC(dl, VT, N0.getOperand(0), NegN1, SwapCond); 4159 } 4160 } 4161 4162 // If the condition is not legal, see if we can find an equivalent one 4163 // which is legal. 4164 if (!isCondCodeLegal(Cond, N0.getSimpleValueType())) { 4165 // If the comparison was an awkward floating-point == or != and one of 4166 // the comparison operands is infinity or negative infinity, convert the 4167 // condition to a less-awkward <= or >=. 4168 if (CFP->getValueAPF().isInfinity()) { 4169 bool IsNegInf = CFP->getValueAPF().isNegative(); 4170 ISD::CondCode NewCond = ISD::SETCC_INVALID; 4171 switch (Cond) { 4172 case ISD::SETOEQ: NewCond = IsNegInf ? ISD::SETOLE : ISD::SETOGE; break; 4173 case ISD::SETUEQ: NewCond = IsNegInf ? ISD::SETULE : ISD::SETUGE; break; 4174 case ISD::SETUNE: NewCond = IsNegInf ? ISD::SETUGT : ISD::SETULT; break; 4175 case ISD::SETONE: NewCond = IsNegInf ? ISD::SETOGT : ISD::SETOLT; break; 4176 default: break; 4177 } 4178 if (NewCond != ISD::SETCC_INVALID && 4179 isCondCodeLegal(NewCond, N0.getSimpleValueType())) 4180 return DAG.getSetCC(dl, VT, N0, N1, NewCond); 4181 } 4182 } 4183 } 4184 4185 if (N0 == N1) { 4186 // The sext(setcc()) => setcc() optimization relies on the appropriate 4187 // constant being emitted. 4188 assert(!N0.getValueType().isInteger() && 4189 "Integer types should be handled by FoldSetCC"); 4190 4191 bool EqTrue = ISD::isTrueWhenEqual(Cond); 4192 unsigned UOF = ISD::getUnorderedFlavor(Cond); 4193 if (UOF == 2) // FP operators that are undefined on NaNs. 4194 return DAG.getBoolConstant(EqTrue, dl, VT, OpVT); 4195 if (UOF == unsigned(EqTrue)) 4196 return DAG.getBoolConstant(EqTrue, dl, VT, OpVT); 4197 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO 4198 // if it is not already. 4199 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO; 4200 if (NewCond != Cond && 4201 (DCI.isBeforeLegalizeOps() || 4202 isCondCodeLegal(NewCond, N0.getSimpleValueType()))) 4203 return DAG.getSetCC(dl, VT, N0, N1, NewCond); 4204 } 4205 4206 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 4207 N0.getValueType().isInteger()) { 4208 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB || 4209 N0.getOpcode() == ISD::XOR) { 4210 // Simplify (X+Y) == (X+Z) --> Y == Z 4211 if (N0.getOpcode() == N1.getOpcode()) { 4212 if (N0.getOperand(0) == N1.getOperand(0)) 4213 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond); 4214 if (N0.getOperand(1) == N1.getOperand(1)) 4215 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond); 4216 if (isCommutativeBinOp(N0.getOpcode())) { 4217 // If X op Y == Y op X, try other combinations. 4218 if (N0.getOperand(0) == N1.getOperand(1)) 4219 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0), 4220 Cond); 4221 if (N0.getOperand(1) == N1.getOperand(0)) 4222 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1), 4223 Cond); 4224 } 4225 } 4226 4227 // If RHS is a legal immediate value for a compare instruction, we need 4228 // to be careful about increasing register pressure needlessly. 4229 bool LegalRHSImm = false; 4230 4231 if (auto *RHSC = dyn_cast<ConstantSDNode>(N1)) { 4232 if (auto *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { 4233 // Turn (X+C1) == C2 --> X == C2-C1 4234 if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) { 4235 return DAG.getSetCC(dl, VT, N0.getOperand(0), 4236 DAG.getConstant(RHSC->getAPIntValue()- 4237 LHSR->getAPIntValue(), 4238 dl, N0.getValueType()), Cond); 4239 } 4240 4241 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. 4242 if (N0.getOpcode() == ISD::XOR) 4243 // If we know that all of the inverted bits are zero, don't bother 4244 // performing the inversion. 4245 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue())) 4246 return 4247 DAG.getSetCC(dl, VT, N0.getOperand(0), 4248 DAG.getConstant(LHSR->getAPIntValue() ^ 4249 RHSC->getAPIntValue(), 4250 dl, N0.getValueType()), 4251 Cond); 4252 } 4253 4254 // Turn (C1-X) == C2 --> X == C1-C2 4255 if (auto *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) { 4256 if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) { 4257 return 4258 DAG.getSetCC(dl, VT, N0.getOperand(1), 4259 DAG.getConstant(SUBC->getAPIntValue() - 4260 RHSC->getAPIntValue(), 4261 dl, N0.getValueType()), 4262 Cond); 4263 } 4264 } 4265 4266 // Could RHSC fold directly into a compare? 4267 if (RHSC->getValueType(0).getSizeInBits() <= 64) 4268 LegalRHSImm = isLegalICmpImmediate(RHSC->getSExtValue()); 4269 } 4270 4271 // (X+Y) == X --> Y == 0 and similar folds. 4272 // Don't do this if X is an immediate that can fold into a cmp 4273 // instruction and X+Y has other uses. It could be an induction variable 4274 // chain, and the transform would increase register pressure. 4275 if (!LegalRHSImm || N0.hasOneUse()) 4276 if (SDValue V = foldSetCCWithBinOp(VT, N0, N1, Cond, dl, DCI)) 4277 return V; 4278 } 4279 4280 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB || 4281 N1.getOpcode() == ISD::XOR) 4282 if (SDValue V = foldSetCCWithBinOp(VT, N1, N0, Cond, dl, DCI)) 4283 return V; 4284 4285 if (SDValue V = foldSetCCWithAnd(VT, N0, N1, Cond, dl, DCI)) 4286 return V; 4287 } 4288 4289 // Fold remainder of division by a constant. 4290 if ((N0.getOpcode() == ISD::UREM || N0.getOpcode() == ISD::SREM) && 4291 N0.hasOneUse() && (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { 4292 AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes(); 4293 4294 // When division is cheap or optimizing for minimum size, 4295 // fall through to DIVREM creation by skipping this fold. 4296 if (!isIntDivCheap(VT, Attr) && !Attr.hasFnAttribute(Attribute::MinSize)) { 4297 if (N0.getOpcode() == ISD::UREM) { 4298 if (SDValue Folded = buildUREMEqFold(VT, N0, N1, Cond, DCI, dl)) 4299 return Folded; 4300 } else if (N0.getOpcode() == ISD::SREM) { 4301 if (SDValue Folded = buildSREMEqFold(VT, N0, N1, Cond, DCI, dl)) 4302 return Folded; 4303 } 4304 } 4305 } 4306 4307 // Fold away ALL boolean setcc's. 4308 if (N0.getValueType().getScalarType() == MVT::i1 && foldBooleans) { 4309 SDValue Temp; 4310 switch (Cond) { 4311 default: llvm_unreachable("Unknown integer setcc!"); 4312 case ISD::SETEQ: // X == Y -> ~(X^Y) 4313 Temp = DAG.getNode(ISD::XOR, dl, OpVT, N0, N1); 4314 N0 = DAG.getNOT(dl, Temp, OpVT); 4315 if (!DCI.isCalledByLegalizer()) 4316 DCI.AddToWorklist(Temp.getNode()); 4317 break; 4318 case ISD::SETNE: // X != Y --> (X^Y) 4319 N0 = DAG.getNode(ISD::XOR, dl, OpVT, N0, N1); 4320 break; 4321 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y 4322 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y 4323 Temp = DAG.getNOT(dl, N0, OpVT); 4324 N0 = DAG.getNode(ISD::AND, dl, OpVT, N1, Temp); 4325 if (!DCI.isCalledByLegalizer()) 4326 DCI.AddToWorklist(Temp.getNode()); 4327 break; 4328 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X 4329 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X 4330 Temp = DAG.getNOT(dl, N1, OpVT); 4331 N0 = DAG.getNode(ISD::AND, dl, OpVT, N0, Temp); 4332 if (!DCI.isCalledByLegalizer()) 4333 DCI.AddToWorklist(Temp.getNode()); 4334 break; 4335 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y 4336 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y 4337 Temp = DAG.getNOT(dl, N0, OpVT); 4338 N0 = DAG.getNode(ISD::OR, dl, OpVT, N1, Temp); 4339 if (!DCI.isCalledByLegalizer()) 4340 DCI.AddToWorklist(Temp.getNode()); 4341 break; 4342 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X 4343 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X 4344 Temp = DAG.getNOT(dl, N1, OpVT); 4345 N0 = DAG.getNode(ISD::OR, dl, OpVT, N0, Temp); 4346 break; 4347 } 4348 if (VT.getScalarType() != MVT::i1) { 4349 if (!DCI.isCalledByLegalizer()) 4350 DCI.AddToWorklist(N0.getNode()); 4351 // FIXME: If running after legalize, we probably can't do this. 4352 ISD::NodeType ExtendCode = getExtendForContent(getBooleanContents(OpVT)); 4353 N0 = DAG.getNode(ExtendCode, dl, VT, N0); 4354 } 4355 return N0; 4356 } 4357 4358 // Could not fold it. 4359 return SDValue(); 4360 } 4361 4362 /// Returns true (and the GlobalValue and the offset) if the node is a 4363 /// GlobalAddress + offset. 4364 bool TargetLowering::isGAPlusOffset(SDNode *WN, const GlobalValue *&GA, 4365 int64_t &Offset) const { 4366 4367 SDNode *N = unwrapAddress(SDValue(WN, 0)).getNode(); 4368 4369 if (auto *GASD = dyn_cast<GlobalAddressSDNode>(N)) { 4370 GA = GASD->getGlobal(); 4371 Offset += GASD->getOffset(); 4372 return true; 4373 } 4374 4375 if (N->getOpcode() == ISD::ADD) { 4376 SDValue N1 = N->getOperand(0); 4377 SDValue N2 = N->getOperand(1); 4378 if (isGAPlusOffset(N1.getNode(), GA, Offset)) { 4379 if (auto *V = dyn_cast<ConstantSDNode>(N2)) { 4380 Offset += V->getSExtValue(); 4381 return true; 4382 } 4383 } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) { 4384 if (auto *V = dyn_cast<ConstantSDNode>(N1)) { 4385 Offset += V->getSExtValue(); 4386 return true; 4387 } 4388 } 4389 } 4390 4391 return false; 4392 } 4393 4394 SDValue TargetLowering::PerformDAGCombine(SDNode *N, 4395 DAGCombinerInfo &DCI) const { 4396 // Default implementation: no optimization. 4397 return SDValue(); 4398 } 4399 4400 //===----------------------------------------------------------------------===// 4401 // Inline Assembler Implementation Methods 4402 //===----------------------------------------------------------------------===// 4403 4404 TargetLowering::ConstraintType 4405 TargetLowering::getConstraintType(StringRef Constraint) const { 4406 unsigned S = Constraint.size(); 4407 4408 if (S == 1) { 4409 switch (Constraint[0]) { 4410 default: break; 4411 case 'r': 4412 return C_RegisterClass; 4413 case 'm': // memory 4414 case 'o': // offsetable 4415 case 'V': // not offsetable 4416 return C_Memory; 4417 case 'n': // Simple Integer 4418 case 'E': // Floating Point Constant 4419 case 'F': // Floating Point Constant 4420 return C_Immediate; 4421 case 'i': // Simple Integer or Relocatable Constant 4422 case 's': // Relocatable Constant 4423 case 'p': // Address. 4424 case 'X': // Allow ANY value. 4425 case 'I': // Target registers. 4426 case 'J': 4427 case 'K': 4428 case 'L': 4429 case 'M': 4430 case 'N': 4431 case 'O': 4432 case 'P': 4433 case '<': 4434 case '>': 4435 return C_Other; 4436 } 4437 } 4438 4439 if (S > 1 && Constraint[0] == '{' && Constraint[S - 1] == '}') { 4440 if (S == 8 && Constraint.substr(1, 6) == "memory") // "{memory}" 4441 return C_Memory; 4442 return C_Register; 4443 } 4444 return C_Unknown; 4445 } 4446 4447 /// Try to replace an X constraint, which matches anything, with another that 4448 /// has more specific requirements based on the type of the corresponding 4449 /// operand. 4450 const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const { 4451 if (ConstraintVT.isInteger()) 4452 return "r"; 4453 if (ConstraintVT.isFloatingPoint()) 4454 return "f"; // works for many targets 4455 return nullptr; 4456 } 4457 4458 SDValue TargetLowering::LowerAsmOutputForConstraint( 4459 SDValue &Chain, SDValue &Flag, const SDLoc &DL, 4460 const AsmOperandInfo &OpInfo, SelectionDAG &DAG) const { 4461 return SDValue(); 4462 } 4463 4464 /// Lower the specified operand into the Ops vector. 4465 /// If it is invalid, don't add anything to Ops. 4466 void TargetLowering::LowerAsmOperandForConstraint(SDValue Op, 4467 std::string &Constraint, 4468 std::vector<SDValue> &Ops, 4469 SelectionDAG &DAG) const { 4470 4471 if (Constraint.length() > 1) return; 4472 4473 char ConstraintLetter = Constraint[0]; 4474 switch (ConstraintLetter) { 4475 default: break; 4476 case 'X': // Allows any operand; labels (basic block) use this. 4477 if (Op.getOpcode() == ISD::BasicBlock || 4478 Op.getOpcode() == ISD::TargetBlockAddress) { 4479 Ops.push_back(Op); 4480 return; 4481 } 4482 LLVM_FALLTHROUGH; 4483 case 'i': // Simple Integer or Relocatable Constant 4484 case 'n': // Simple Integer 4485 case 's': { // Relocatable Constant 4486 4487 GlobalAddressSDNode *GA; 4488 ConstantSDNode *C; 4489 BlockAddressSDNode *BA; 4490 uint64_t Offset = 0; 4491 4492 // Match (GA) or (C) or (GA+C) or (GA-C) or ((GA+C)+C) or (((GA+C)+C)+C), 4493 // etc., since getelementpointer is variadic. We can't use 4494 // SelectionDAG::FoldSymbolOffset because it expects the GA to be accessible 4495 // while in this case the GA may be furthest from the root node which is 4496 // likely an ISD::ADD. 4497 while (1) { 4498 if ((GA = dyn_cast<GlobalAddressSDNode>(Op)) && ConstraintLetter != 'n') { 4499 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(), SDLoc(Op), 4500 GA->getValueType(0), 4501 Offset + GA->getOffset())); 4502 return; 4503 } else if ((C = dyn_cast<ConstantSDNode>(Op)) && 4504 ConstraintLetter != 's') { 4505 // gcc prints these as sign extended. Sign extend value to 64 bits 4506 // now; without this it would get ZExt'd later in 4507 // ScheduleDAGSDNodes::EmitNode, which is very generic. 4508 bool IsBool = C->getConstantIntValue()->getBitWidth() == 1; 4509 BooleanContent BCont = getBooleanContents(MVT::i64); 4510 ISD::NodeType ExtOpc = IsBool ? getExtendForContent(BCont) 4511 : ISD::SIGN_EXTEND; 4512 int64_t ExtVal = ExtOpc == ISD::ZERO_EXTEND ? C->getZExtValue() 4513 : C->getSExtValue(); 4514 Ops.push_back(DAG.getTargetConstant(Offset + ExtVal, 4515 SDLoc(C), MVT::i64)); 4516 return; 4517 } else if ((BA = dyn_cast<BlockAddressSDNode>(Op)) && 4518 ConstraintLetter != 'n') { 4519 Ops.push_back(DAG.getTargetBlockAddress( 4520 BA->getBlockAddress(), BA->getValueType(0), 4521 Offset + BA->getOffset(), BA->getTargetFlags())); 4522 return; 4523 } else { 4524 const unsigned OpCode = Op.getOpcode(); 4525 if (OpCode == ISD::ADD || OpCode == ISD::SUB) { 4526 if ((C = dyn_cast<ConstantSDNode>(Op.getOperand(0)))) 4527 Op = Op.getOperand(1); 4528 // Subtraction is not commutative. 4529 else if (OpCode == ISD::ADD && 4530 (C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))) 4531 Op = Op.getOperand(0); 4532 else 4533 return; 4534 Offset += (OpCode == ISD::ADD ? 1 : -1) * C->getSExtValue(); 4535 continue; 4536 } 4537 } 4538 return; 4539 } 4540 break; 4541 } 4542 } 4543 } 4544 4545 std::pair<unsigned, const TargetRegisterClass *> 4546 TargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *RI, 4547 StringRef Constraint, 4548 MVT VT) const { 4549 if (Constraint.empty() || Constraint[0] != '{') 4550 return std::make_pair(0u, static_cast<TargetRegisterClass *>(nullptr)); 4551 assert(*(Constraint.end() - 1) == '}' && "Not a brace enclosed constraint?"); 4552 4553 // Remove the braces from around the name. 4554 StringRef RegName(Constraint.data() + 1, Constraint.size() - 2); 4555 4556 std::pair<unsigned, const TargetRegisterClass *> R = 4557 std::make_pair(0u, static_cast<const TargetRegisterClass *>(nullptr)); 4558 4559 // Figure out which register class contains this reg. 4560 for (const TargetRegisterClass *RC : RI->regclasses()) { 4561 // If none of the value types for this register class are valid, we 4562 // can't use it. For example, 64-bit reg classes on 32-bit targets. 4563 if (!isLegalRC(*RI, *RC)) 4564 continue; 4565 4566 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); 4567 I != E; ++I) { 4568 if (RegName.equals_lower(RI->getRegAsmName(*I))) { 4569 std::pair<unsigned, const TargetRegisterClass *> S = 4570 std::make_pair(*I, RC); 4571 4572 // If this register class has the requested value type, return it, 4573 // otherwise keep searching and return the first class found 4574 // if no other is found which explicitly has the requested type. 4575 if (RI->isTypeLegalForClass(*RC, VT)) 4576 return S; 4577 if (!R.second) 4578 R = S; 4579 } 4580 } 4581 } 4582 4583 return R; 4584 } 4585 4586 //===----------------------------------------------------------------------===// 4587 // Constraint Selection. 4588 4589 /// Return true of this is an input operand that is a matching constraint like 4590 /// "4". 4591 bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const { 4592 assert(!ConstraintCode.empty() && "No known constraint!"); 4593 return isdigit(static_cast<unsigned char>(ConstraintCode[0])); 4594 } 4595 4596 /// If this is an input matching constraint, this method returns the output 4597 /// operand it matches. 4598 unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const { 4599 assert(!ConstraintCode.empty() && "No known constraint!"); 4600 return atoi(ConstraintCode.c_str()); 4601 } 4602 4603 /// Split up the constraint string from the inline assembly value into the 4604 /// specific constraints and their prefixes, and also tie in the associated 4605 /// operand values. 4606 /// If this returns an empty vector, and if the constraint string itself 4607 /// isn't empty, there was an error parsing. 4608 TargetLowering::AsmOperandInfoVector 4609 TargetLowering::ParseConstraints(const DataLayout &DL, 4610 const TargetRegisterInfo *TRI, 4611 const CallBase &Call) const { 4612 /// Information about all of the constraints. 4613 AsmOperandInfoVector ConstraintOperands; 4614 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand()); 4615 unsigned maCount = 0; // Largest number of multiple alternative constraints. 4616 4617 // Do a prepass over the constraints, canonicalizing them, and building up the 4618 // ConstraintOperands list. 4619 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. 4620 unsigned ResNo = 0; // ResNo - The result number of the next output. 4621 4622 for (InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) { 4623 ConstraintOperands.emplace_back(std::move(CI)); 4624 AsmOperandInfo &OpInfo = ConstraintOperands.back(); 4625 4626 // Update multiple alternative constraint count. 4627 if (OpInfo.multipleAlternatives.size() > maCount) 4628 maCount = OpInfo.multipleAlternatives.size(); 4629 4630 OpInfo.ConstraintVT = MVT::Other; 4631 4632 // Compute the value type for each operand. 4633 switch (OpInfo.Type) { 4634 case InlineAsm::isOutput: 4635 // Indirect outputs just consume an argument. 4636 if (OpInfo.isIndirect) { 4637 OpInfo.CallOperandVal = Call.getArgOperand(ArgNo++); 4638 break; 4639 } 4640 4641 // The return value of the call is this value. As such, there is no 4642 // corresponding argument. 4643 assert(!Call.getType()->isVoidTy() && "Bad inline asm!"); 4644 if (StructType *STy = dyn_cast<StructType>(Call.getType())) { 4645 OpInfo.ConstraintVT = 4646 getSimpleValueType(DL, STy->getElementType(ResNo)); 4647 } else { 4648 assert(ResNo == 0 && "Asm only has one result!"); 4649 OpInfo.ConstraintVT = getSimpleValueType(DL, Call.getType()); 4650 } 4651 ++ResNo; 4652 break; 4653 case InlineAsm::isInput: 4654 OpInfo.CallOperandVal = Call.getArgOperand(ArgNo++); 4655 break; 4656 case InlineAsm::isClobber: 4657 // Nothing to do. 4658 break; 4659 } 4660 4661 if (OpInfo.CallOperandVal) { 4662 llvm::Type *OpTy = OpInfo.CallOperandVal->getType(); 4663 if (OpInfo.isIndirect) { 4664 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy); 4665 if (!PtrTy) 4666 report_fatal_error("Indirect operand for inline asm not a pointer!"); 4667 OpTy = PtrTy->getElementType(); 4668 } 4669 4670 // Look for vector wrapped in a struct. e.g. { <16 x i8> }. 4671 if (StructType *STy = dyn_cast<StructType>(OpTy)) 4672 if (STy->getNumElements() == 1) 4673 OpTy = STy->getElementType(0); 4674 4675 // If OpTy is not a single value, it may be a struct/union that we 4676 // can tile with integers. 4677 if (!OpTy->isSingleValueType() && OpTy->isSized()) { 4678 unsigned BitSize = DL.getTypeSizeInBits(OpTy); 4679 switch (BitSize) { 4680 default: break; 4681 case 1: 4682 case 8: 4683 case 16: 4684 case 32: 4685 case 64: 4686 case 128: 4687 OpInfo.ConstraintVT = 4688 MVT::getVT(IntegerType::get(OpTy->getContext(), BitSize), true); 4689 break; 4690 } 4691 } else if (PointerType *PT = dyn_cast<PointerType>(OpTy)) { 4692 unsigned PtrSize = DL.getPointerSizeInBits(PT->getAddressSpace()); 4693 OpInfo.ConstraintVT = MVT::getIntegerVT(PtrSize); 4694 } else { 4695 OpInfo.ConstraintVT = MVT::getVT(OpTy, true); 4696 } 4697 } 4698 } 4699 4700 // If we have multiple alternative constraints, select the best alternative. 4701 if (!ConstraintOperands.empty()) { 4702 if (maCount) { 4703 unsigned bestMAIndex = 0; 4704 int bestWeight = -1; 4705 // weight: -1 = invalid match, and 0 = so-so match to 5 = good match. 4706 int weight = -1; 4707 unsigned maIndex; 4708 // Compute the sums of the weights for each alternative, keeping track 4709 // of the best (highest weight) one so far. 4710 for (maIndex = 0; maIndex < maCount; ++maIndex) { 4711 int weightSum = 0; 4712 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size(); 4713 cIndex != eIndex; ++cIndex) { 4714 AsmOperandInfo &OpInfo = ConstraintOperands[cIndex]; 4715 if (OpInfo.Type == InlineAsm::isClobber) 4716 continue; 4717 4718 // If this is an output operand with a matching input operand, 4719 // look up the matching input. If their types mismatch, e.g. one 4720 // is an integer, the other is floating point, or their sizes are 4721 // different, flag it as an maCantMatch. 4722 if (OpInfo.hasMatchingInput()) { 4723 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; 4724 if (OpInfo.ConstraintVT != Input.ConstraintVT) { 4725 if ((OpInfo.ConstraintVT.isInteger() != 4726 Input.ConstraintVT.isInteger()) || 4727 (OpInfo.ConstraintVT.getSizeInBits() != 4728 Input.ConstraintVT.getSizeInBits())) { 4729 weightSum = -1; // Can't match. 4730 break; 4731 } 4732 } 4733 } 4734 weight = getMultipleConstraintMatchWeight(OpInfo, maIndex); 4735 if (weight == -1) { 4736 weightSum = -1; 4737 break; 4738 } 4739 weightSum += weight; 4740 } 4741 // Update best. 4742 if (weightSum > bestWeight) { 4743 bestWeight = weightSum; 4744 bestMAIndex = maIndex; 4745 } 4746 } 4747 4748 // Now select chosen alternative in each constraint. 4749 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size(); 4750 cIndex != eIndex; ++cIndex) { 4751 AsmOperandInfo &cInfo = ConstraintOperands[cIndex]; 4752 if (cInfo.Type == InlineAsm::isClobber) 4753 continue; 4754 cInfo.selectAlternative(bestMAIndex); 4755 } 4756 } 4757 } 4758 4759 // Check and hook up tied operands, choose constraint code to use. 4760 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size(); 4761 cIndex != eIndex; ++cIndex) { 4762 AsmOperandInfo &OpInfo = ConstraintOperands[cIndex]; 4763 4764 // If this is an output operand with a matching input operand, look up the 4765 // matching input. If their types mismatch, e.g. one is an integer, the 4766 // other is floating point, or their sizes are different, flag it as an 4767 // error. 4768 if (OpInfo.hasMatchingInput()) { 4769 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; 4770 4771 if (OpInfo.ConstraintVT != Input.ConstraintVT) { 4772 std::pair<unsigned, const TargetRegisterClass *> MatchRC = 4773 getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode, 4774 OpInfo.ConstraintVT); 4775 std::pair<unsigned, const TargetRegisterClass *> InputRC = 4776 getRegForInlineAsmConstraint(TRI, Input.ConstraintCode, 4777 Input.ConstraintVT); 4778 if ((OpInfo.ConstraintVT.isInteger() != 4779 Input.ConstraintVT.isInteger()) || 4780 (MatchRC.second != InputRC.second)) { 4781 report_fatal_error("Unsupported asm: input constraint" 4782 " with a matching output constraint of" 4783 " incompatible type!"); 4784 } 4785 } 4786 } 4787 } 4788 4789 return ConstraintOperands; 4790 } 4791 4792 /// Return an integer indicating how general CT is. 4793 static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) { 4794 switch (CT) { 4795 case TargetLowering::C_Immediate: 4796 case TargetLowering::C_Other: 4797 case TargetLowering::C_Unknown: 4798 return 0; 4799 case TargetLowering::C_Register: 4800 return 1; 4801 case TargetLowering::C_RegisterClass: 4802 return 2; 4803 case TargetLowering::C_Memory: 4804 return 3; 4805 } 4806 llvm_unreachable("Invalid constraint type"); 4807 } 4808 4809 /// Examine constraint type and operand type and determine a weight value. 4810 /// This object must already have been set up with the operand type 4811 /// and the current alternative constraint selected. 4812 TargetLowering::ConstraintWeight 4813 TargetLowering::getMultipleConstraintMatchWeight( 4814 AsmOperandInfo &info, int maIndex) const { 4815 InlineAsm::ConstraintCodeVector *rCodes; 4816 if (maIndex >= (int)info.multipleAlternatives.size()) 4817 rCodes = &info.Codes; 4818 else 4819 rCodes = &info.multipleAlternatives[maIndex].Codes; 4820 ConstraintWeight BestWeight = CW_Invalid; 4821 4822 // Loop over the options, keeping track of the most general one. 4823 for (unsigned i = 0, e = rCodes->size(); i != e; ++i) { 4824 ConstraintWeight weight = 4825 getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str()); 4826 if (weight > BestWeight) 4827 BestWeight = weight; 4828 } 4829 4830 return BestWeight; 4831 } 4832 4833 /// Examine constraint type and operand type and determine a weight value. 4834 /// This object must already have been set up with the operand type 4835 /// and the current alternative constraint selected. 4836 TargetLowering::ConstraintWeight 4837 TargetLowering::getSingleConstraintMatchWeight( 4838 AsmOperandInfo &info, const char *constraint) const { 4839 ConstraintWeight weight = CW_Invalid; 4840 Value *CallOperandVal = info.CallOperandVal; 4841 // If we don't have a value, we can't do a match, 4842 // but allow it at the lowest weight. 4843 if (!CallOperandVal) 4844 return CW_Default; 4845 // Look at the constraint type. 4846 switch (*constraint) { 4847 case 'i': // immediate integer. 4848 case 'n': // immediate integer with a known value. 4849 if (isa<ConstantInt>(CallOperandVal)) 4850 weight = CW_Constant; 4851 break; 4852 case 's': // non-explicit intregal immediate. 4853 if (isa<GlobalValue>(CallOperandVal)) 4854 weight = CW_Constant; 4855 break; 4856 case 'E': // immediate float if host format. 4857 case 'F': // immediate float. 4858 if (isa<ConstantFP>(CallOperandVal)) 4859 weight = CW_Constant; 4860 break; 4861 case '<': // memory operand with autodecrement. 4862 case '>': // memory operand with autoincrement. 4863 case 'm': // memory operand. 4864 case 'o': // offsettable memory operand 4865 case 'V': // non-offsettable memory operand 4866 weight = CW_Memory; 4867 break; 4868 case 'r': // general register. 4869 case 'g': // general register, memory operand or immediate integer. 4870 // note: Clang converts "g" to "imr". 4871 if (CallOperandVal->getType()->isIntegerTy()) 4872 weight = CW_Register; 4873 break; 4874 case 'X': // any operand. 4875 default: 4876 weight = CW_Default; 4877 break; 4878 } 4879 return weight; 4880 } 4881 4882 /// If there are multiple different constraints that we could pick for this 4883 /// operand (e.g. "imr") try to pick the 'best' one. 4884 /// This is somewhat tricky: constraints fall into four classes: 4885 /// Other -> immediates and magic values 4886 /// Register -> one specific register 4887 /// RegisterClass -> a group of regs 4888 /// Memory -> memory 4889 /// Ideally, we would pick the most specific constraint possible: if we have 4890 /// something that fits into a register, we would pick it. The problem here 4891 /// is that if we have something that could either be in a register or in 4892 /// memory that use of the register could cause selection of *other* 4893 /// operands to fail: they might only succeed if we pick memory. Because of 4894 /// this the heuristic we use is: 4895 /// 4896 /// 1) If there is an 'other' constraint, and if the operand is valid for 4897 /// that constraint, use it. This makes us take advantage of 'i' 4898 /// constraints when available. 4899 /// 2) Otherwise, pick the most general constraint present. This prefers 4900 /// 'm' over 'r', for example. 4901 /// 4902 static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo, 4903 const TargetLowering &TLI, 4904 SDValue Op, SelectionDAG *DAG) { 4905 assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options"); 4906 unsigned BestIdx = 0; 4907 TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown; 4908 int BestGenerality = -1; 4909 4910 // Loop over the options, keeping track of the most general one. 4911 for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) { 4912 TargetLowering::ConstraintType CType = 4913 TLI.getConstraintType(OpInfo.Codes[i]); 4914 4915 // Indirect 'other' or 'immediate' constraints are not allowed. 4916 if (OpInfo.isIndirect && !(CType == TargetLowering::C_Memory || 4917 CType == TargetLowering::C_Register || 4918 CType == TargetLowering::C_RegisterClass)) 4919 continue; 4920 4921 // If this is an 'other' or 'immediate' constraint, see if the operand is 4922 // valid for it. For example, on X86 we might have an 'rI' constraint. If 4923 // the operand is an integer in the range [0..31] we want to use I (saving a 4924 // load of a register), otherwise we must use 'r'. 4925 if ((CType == TargetLowering::C_Other || 4926 CType == TargetLowering::C_Immediate) && Op.getNode()) { 4927 assert(OpInfo.Codes[i].size() == 1 && 4928 "Unhandled multi-letter 'other' constraint"); 4929 std::vector<SDValue> ResultOps; 4930 TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i], 4931 ResultOps, *DAG); 4932 if (!ResultOps.empty()) { 4933 BestType = CType; 4934 BestIdx = i; 4935 break; 4936 } 4937 } 4938 4939 // Things with matching constraints can only be registers, per gcc 4940 // documentation. This mainly affects "g" constraints. 4941 if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput()) 4942 continue; 4943 4944 // This constraint letter is more general than the previous one, use it. 4945 int Generality = getConstraintGenerality(CType); 4946 if (Generality > BestGenerality) { 4947 BestType = CType; 4948 BestIdx = i; 4949 BestGenerality = Generality; 4950 } 4951 } 4952 4953 OpInfo.ConstraintCode = OpInfo.Codes[BestIdx]; 4954 OpInfo.ConstraintType = BestType; 4955 } 4956 4957 /// Determines the constraint code and constraint type to use for the specific 4958 /// AsmOperandInfo, setting OpInfo.ConstraintCode and OpInfo.ConstraintType. 4959 void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo, 4960 SDValue Op, 4961 SelectionDAG *DAG) const { 4962 assert(!OpInfo.Codes.empty() && "Must have at least one constraint"); 4963 4964 // Single-letter constraints ('r') are very common. 4965 if (OpInfo.Codes.size() == 1) { 4966 OpInfo.ConstraintCode = OpInfo.Codes[0]; 4967 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode); 4968 } else { 4969 ChooseConstraint(OpInfo, *this, Op, DAG); 4970 } 4971 4972 // 'X' matches anything. 4973 if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) { 4974 // Labels and constants are handled elsewhere ('X' is the only thing 4975 // that matches labels). For Functions, the type here is the type of 4976 // the result, which is not what we want to look at; leave them alone. 4977 Value *v = OpInfo.CallOperandVal; 4978 if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) { 4979 OpInfo.CallOperandVal = v; 4980 return; 4981 } 4982 4983 if (Op.getNode() && Op.getOpcode() == ISD::TargetBlockAddress) 4984 return; 4985 4986 // Otherwise, try to resolve it to something we know about by looking at 4987 // the actual operand type. 4988 if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) { 4989 OpInfo.ConstraintCode = Repl; 4990 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode); 4991 } 4992 } 4993 } 4994 4995 /// Given an exact SDIV by a constant, create a multiplication 4996 /// with the multiplicative inverse of the constant. 4997 static SDValue BuildExactSDIV(const TargetLowering &TLI, SDNode *N, 4998 const SDLoc &dl, SelectionDAG &DAG, 4999 SmallVectorImpl<SDNode *> &Created) { 5000 SDValue Op0 = N->getOperand(0); 5001 SDValue Op1 = N->getOperand(1); 5002 EVT VT = N->getValueType(0); 5003 EVT SVT = VT.getScalarType(); 5004 EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 5005 EVT ShSVT = ShVT.getScalarType(); 5006 5007 bool UseSRA = false; 5008 SmallVector<SDValue, 16> Shifts, Factors; 5009 5010 auto BuildSDIVPattern = [&](ConstantSDNode *C) { 5011 if (C->isNullValue()) 5012 return false; 5013 APInt Divisor = C->getAPIntValue(); 5014 unsigned Shift = Divisor.countTrailingZeros(); 5015 if (Shift) { 5016 Divisor.ashrInPlace(Shift); 5017 UseSRA = true; 5018 } 5019 // Calculate the multiplicative inverse, using Newton's method. 5020 APInt t; 5021 APInt Factor = Divisor; 5022 while ((t = Divisor * Factor) != 1) 5023 Factor *= APInt(Divisor.getBitWidth(), 2) - t; 5024 Shifts.push_back(DAG.getConstant(Shift, dl, ShSVT)); 5025 Factors.push_back(DAG.getConstant(Factor, dl, SVT)); 5026 return true; 5027 }; 5028 5029 // Collect all magic values from the build vector. 5030 if (!ISD::matchUnaryPredicate(Op1, BuildSDIVPattern)) 5031 return SDValue(); 5032 5033 SDValue Shift, Factor; 5034 if (VT.isFixedLengthVector()) { 5035 Shift = DAG.getBuildVector(ShVT, dl, Shifts); 5036 Factor = DAG.getBuildVector(VT, dl, Factors); 5037 } else if (VT.isScalableVector()) { 5038 assert(Shifts.size() == 1 && Factors.size() == 1 && 5039 "Expected matchUnaryPredicate to return one element for scalable " 5040 "vectors"); 5041 Shift = DAG.getSplatVector(ShVT, dl, Shifts[0]); 5042 Factor = DAG.getSplatVector(VT, dl, Factors[0]); 5043 } else { 5044 Shift = Shifts[0]; 5045 Factor = Factors[0]; 5046 } 5047 5048 SDValue Res = Op0; 5049 5050 // Shift the value upfront if it is even, so the LSB is one. 5051 if (UseSRA) { 5052 // TODO: For UDIV use SRL instead of SRA. 5053 SDNodeFlags Flags; 5054 Flags.setExact(true); 5055 Res = DAG.getNode(ISD::SRA, dl, VT, Res, Shift, Flags); 5056 Created.push_back(Res.getNode()); 5057 } 5058 5059 return DAG.getNode(ISD::MUL, dl, VT, Res, Factor); 5060 } 5061 5062 SDValue TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor, 5063 SelectionDAG &DAG, 5064 SmallVectorImpl<SDNode *> &Created) const { 5065 AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes(); 5066 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 5067 if (TLI.isIntDivCheap(N->getValueType(0), Attr)) 5068 return SDValue(N, 0); // Lower SDIV as SDIV 5069 return SDValue(); 5070 } 5071 5072 /// Given an ISD::SDIV node expressing a divide by constant, 5073 /// return a DAG expression to select that will generate the same value by 5074 /// multiplying by a magic number. 5075 /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide". 5076 SDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG, 5077 bool IsAfterLegalization, 5078 SmallVectorImpl<SDNode *> &Created) const { 5079 SDLoc dl(N); 5080 EVT VT = N->getValueType(0); 5081 EVT SVT = VT.getScalarType(); 5082 EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout()); 5083 EVT ShSVT = ShVT.getScalarType(); 5084 unsigned EltBits = VT.getScalarSizeInBits(); 5085 5086 // Check to see if we can do this. 5087 // FIXME: We should be more aggressive here. 5088 if (!isTypeLegal(VT)) 5089 return SDValue(); 5090 5091 // If the sdiv has an 'exact' bit we can use a simpler lowering. 5092 if (N->getFlags().hasExact()) 5093 return BuildExactSDIV(*this, N, dl, DAG, Created); 5094 5095 SmallVector<SDValue, 16> MagicFactors, Factors, Shifts, ShiftMasks; 5096 5097 auto BuildSDIVPattern = [&](ConstantSDNode *C) { 5098 if (C->isNullValue()) 5099 return false; 5100 5101 const APInt &Divisor = C->getAPIntValue(); 5102 APInt::ms magics = Divisor.magic(); 5103 int NumeratorFactor = 0; 5104 int ShiftMask = -1; 5105 5106 if (Divisor.isOneValue() || Divisor.isAllOnesValue()) { 5107 // If d is +1/-1, we just multiply the numerator by +1/-1. 5108 NumeratorFactor = Divisor.getSExtValue(); 5109 magics.m = 0; 5110 magics.s = 0; 5111 ShiftMask = 0; 5112 } else if (Divisor.isStrictlyPositive() && magics.m.isNegative()) { 5113 // If d > 0 and m < 0, add the numerator. 5114 NumeratorFactor = 1; 5115 } else if (Divisor.isNegative() && magics.m.isStrictlyPositive()) { 5116 // If d < 0 and m > 0, subtract the numerator. 5117 NumeratorFactor = -1; 5118 } 5119 5120 MagicFactors.push_back(DAG.getConstant(magics.m, dl, SVT)); 5121 Factors.push_back(DAG.getConstant(NumeratorFactor, dl, SVT)); 5122 Shifts.push_back(DAG.getConstant(magics.s, dl, ShSVT)); 5123 ShiftMasks.push_back(DAG.getConstant(ShiftMask, dl, SVT)); 5124 return true; 5125 }; 5126 5127 SDValue N0 = N->getOperand(0); 5128 SDValue N1 = N->getOperand(1); 5129 5130 // Collect the shifts / magic values from each element. 5131 if (!ISD::matchUnaryPredicate(N1, BuildSDIVPattern)) 5132 return SDValue(); 5133 5134 SDValue MagicFactor, Factor, Shift, ShiftMask; 5135 if (VT.isFixedLengthVector()) { 5136 MagicFactor = DAG.getBuildVector(VT, dl, MagicFactors); 5137 Factor = DAG.getBuildVector(VT, dl, Factors); 5138 Shift = DAG.getBuildVector(ShVT, dl, Shifts); 5139 ShiftMask = DAG.getBuildVector(VT, dl, ShiftMasks); 5140 } else if (VT.isScalableVector()) { 5141 assert(MagicFactors.size() == 1 && Factors.size() == 1 && 5142 Shifts.size() == 1 && ShiftMasks.size() == 1 && 5143 "Expected matchUnaryPredicate to return one element for scalable " 5144 "vectors"); 5145 MagicFactor = DAG.getSplatVector(VT, dl, MagicFactors[0]); 5146 Factor = DAG.getSplatVector(VT, dl, Factors[0]); 5147 Shift = DAG.getSplatVector(ShVT, dl, Shifts[0]); 5148 ShiftMask = DAG.getSplatVector(VT, dl, ShiftMasks[0]); 5149 } else { 5150 MagicFactor = MagicFactors[0]; 5151 Factor = Factors[0]; 5152 Shift = Shifts[0]; 5153 ShiftMask = ShiftMasks[0]; 5154 } 5155 5156 // Multiply the numerator (operand 0) by the magic value. 5157 // FIXME: We should support doing a MUL in a wider type. 5158 SDValue Q; 5159 if (IsAfterLegalization ? isOperationLegal(ISD::MULHS, VT) 5160 : isOperationLegalOrCustom(ISD::MULHS, VT)) 5161 Q = DAG.getNode(ISD::MULHS, dl, VT, N0, MagicFactor); 5162 else if (IsAfterLegalization ? isOperationLegal(ISD::SMUL_LOHI, VT) 5163 : isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) { 5164 SDValue LoHi = 5165 DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT), N0, MagicFactor); 5166 Q = SDValue(LoHi.getNode(), 1); 5167 } else 5168 return SDValue(); // No mulhs or equivalent. 5169 Created.push_back(Q.getNode()); 5170 5171 // (Optionally) Add/subtract the numerator using Factor. 5172 Factor = DAG.getNode(ISD::MUL, dl, VT, N0, Factor); 5173 Created.push_back(Factor.getNode()); 5174 Q = DAG.getNode(ISD::ADD, dl, VT, Q, Factor); 5175 Created.push_back(Q.getNode()); 5176 5177 // Shift right algebraic by shift value. 5178 Q = DAG.getNode(ISD::SRA, dl, VT, Q, Shift); 5179 Created.push_back(Q.getNode()); 5180 5181 // Extract the sign bit, mask it and add it to the quotient. 5182 SDValue SignShift = DAG.getConstant(EltBits - 1, dl, ShVT); 5183 SDValue T = DAG.getNode(ISD::SRL, dl, VT, Q, SignShift); 5184 Created.push_back(T.getNode()); 5185 T = DAG.getNode(ISD::AND, dl, VT, T, ShiftMask); 5186 Created.push_back(T.getNode()); 5187 return DAG.getNode(ISD::ADD, dl, VT, Q, T); 5188 } 5189 5190 /// Given an ISD::UDIV node expressing a divide by constant, 5191 /// return a DAG expression to select that will generate the same value by 5192 /// multiplying by a magic number. 5193 /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide". 5194 SDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG, 5195 bool IsAfterLegalization, 5196 SmallVectorImpl<SDNode *> &Created) const { 5197 SDLoc dl(N); 5198 EVT VT = N->getValueType(0); 5199 EVT SVT = VT.getScalarType(); 5200 EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout()); 5201 EVT ShSVT = ShVT.getScalarType(); 5202 unsigned EltBits = VT.getScalarSizeInBits(); 5203 5204 // Check to see if we can do this. 5205 // FIXME: We should be more aggressive here. 5206 if (!isTypeLegal(VT)) 5207 return SDValue(); 5208 5209 bool UseNPQ = false; 5210 SmallVector<SDValue, 16> PreShifts, PostShifts, MagicFactors, NPQFactors; 5211 5212 auto BuildUDIVPattern = [&](ConstantSDNode *C) { 5213 if (C->isNullValue()) 5214 return false; 5215 // FIXME: We should use a narrower constant when the upper 5216 // bits are known to be zero. 5217 APInt Divisor = C->getAPIntValue(); 5218 APInt::mu magics = Divisor.magicu(); 5219 unsigned PreShift = 0, PostShift = 0; 5220 5221 // If the divisor is even, we can avoid using the expensive fixup by 5222 // shifting the divided value upfront. 5223 if (magics.a != 0 && !Divisor[0]) { 5224 PreShift = Divisor.countTrailingZeros(); 5225 // Get magic number for the shifted divisor. 5226 magics = Divisor.lshr(PreShift).magicu(PreShift); 5227 assert(magics.a == 0 && "Should use cheap fixup now"); 5228 } 5229 5230 APInt Magic = magics.m; 5231 5232 unsigned SelNPQ; 5233 if (magics.a == 0 || Divisor.isOneValue()) { 5234 assert(magics.s < Divisor.getBitWidth() && 5235 "We shouldn't generate an undefined shift!"); 5236 PostShift = magics.s; 5237 SelNPQ = false; 5238 } else { 5239 PostShift = magics.s - 1; 5240 SelNPQ = true; 5241 } 5242 5243 PreShifts.push_back(DAG.getConstant(PreShift, dl, ShSVT)); 5244 MagicFactors.push_back(DAG.getConstant(Magic, dl, SVT)); 5245 NPQFactors.push_back( 5246 DAG.getConstant(SelNPQ ? APInt::getOneBitSet(EltBits, EltBits - 1) 5247 : APInt::getNullValue(EltBits), 5248 dl, SVT)); 5249 PostShifts.push_back(DAG.getConstant(PostShift, dl, ShSVT)); 5250 UseNPQ |= SelNPQ; 5251 return true; 5252 }; 5253 5254 SDValue N0 = N->getOperand(0); 5255 SDValue N1 = N->getOperand(1); 5256 5257 // Collect the shifts/magic values from each element. 5258 if (!ISD::matchUnaryPredicate(N1, BuildUDIVPattern)) 5259 return SDValue(); 5260 5261 SDValue PreShift, PostShift, MagicFactor, NPQFactor; 5262 if (VT.isFixedLengthVector()) { 5263 PreShift = DAG.getBuildVector(ShVT, dl, PreShifts); 5264 MagicFactor = DAG.getBuildVector(VT, dl, MagicFactors); 5265 NPQFactor = DAG.getBuildVector(VT, dl, NPQFactors); 5266 PostShift = DAG.getBuildVector(ShVT, dl, PostShifts); 5267 } else if (VT.isScalableVector()) { 5268 assert(PreShifts.size() == 1 && MagicFactors.size() == 1 && 5269 NPQFactors.size() == 1 && PostShifts.size() == 1 && 5270 "Expected matchUnaryPredicate to return one for scalable vectors"); 5271 PreShift = DAG.getSplatVector(ShVT, dl, PreShifts[0]); 5272 MagicFactor = DAG.getSplatVector(VT, dl, MagicFactors[0]); 5273 NPQFactor = DAG.getSplatVector(VT, dl, NPQFactors[0]); 5274 PostShift = DAG.getSplatVector(ShVT, dl, PostShifts[0]); 5275 } else { 5276 PreShift = PreShifts[0]; 5277 MagicFactor = MagicFactors[0]; 5278 PostShift = PostShifts[0]; 5279 } 5280 5281 SDValue Q = N0; 5282 Q = DAG.getNode(ISD::SRL, dl, VT, Q, PreShift); 5283 Created.push_back(Q.getNode()); 5284 5285 // FIXME: We should support doing a MUL in a wider type. 5286 auto GetMULHU = [&](SDValue X, SDValue Y) { 5287 if (IsAfterLegalization ? isOperationLegal(ISD::MULHU, VT) 5288 : isOperationLegalOrCustom(ISD::MULHU, VT)) 5289 return DAG.getNode(ISD::MULHU, dl, VT, X, Y); 5290 if (IsAfterLegalization ? isOperationLegal(ISD::UMUL_LOHI, VT) 5291 : isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) { 5292 SDValue LoHi = 5293 DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), X, Y); 5294 return SDValue(LoHi.getNode(), 1); 5295 } 5296 return SDValue(); // No mulhu or equivalent 5297 }; 5298 5299 // Multiply the numerator (operand 0) by the magic value. 5300 Q = GetMULHU(Q, MagicFactor); 5301 if (!Q) 5302 return SDValue(); 5303 5304 Created.push_back(Q.getNode()); 5305 5306 if (UseNPQ) { 5307 SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N0, Q); 5308 Created.push_back(NPQ.getNode()); 5309 5310 // For vectors we might have a mix of non-NPQ/NPQ paths, so use 5311 // MULHU to act as a SRL-by-1 for NPQ, else multiply by zero. 5312 if (VT.isVector()) 5313 NPQ = GetMULHU(NPQ, NPQFactor); 5314 else 5315 NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ, DAG.getConstant(1, dl, ShVT)); 5316 5317 Created.push_back(NPQ.getNode()); 5318 5319 Q = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q); 5320 Created.push_back(Q.getNode()); 5321 } 5322 5323 Q = DAG.getNode(ISD::SRL, dl, VT, Q, PostShift); 5324 Created.push_back(Q.getNode()); 5325 5326 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 5327 5328 SDValue One = DAG.getConstant(1, dl, VT); 5329 SDValue IsOne = DAG.getSetCC(dl, SetCCVT, N1, One, ISD::SETEQ); 5330 return DAG.getSelect(dl, VT, IsOne, N0, Q); 5331 } 5332 5333 /// If all values in Values that *don't* match the predicate are same 'splat' 5334 /// value, then replace all values with that splat value. 5335 /// Else, if AlternativeReplacement was provided, then replace all values that 5336 /// do match predicate with AlternativeReplacement value. 5337 static void 5338 turnVectorIntoSplatVector(MutableArrayRef<SDValue> Values, 5339 std::function<bool(SDValue)> Predicate, 5340 SDValue AlternativeReplacement = SDValue()) { 5341 SDValue Replacement; 5342 // Is there a value for which the Predicate does *NOT* match? What is it? 5343 auto SplatValue = llvm::find_if_not(Values, Predicate); 5344 if (SplatValue != Values.end()) { 5345 // Does Values consist only of SplatValue's and values matching Predicate? 5346 if (llvm::all_of(Values, [Predicate, SplatValue](SDValue Value) { 5347 return Value == *SplatValue || Predicate(Value); 5348 })) // Then we shall replace values matching predicate with SplatValue. 5349 Replacement = *SplatValue; 5350 } 5351 if (!Replacement) { 5352 // Oops, we did not find the "baseline" splat value. 5353 if (!AlternativeReplacement) 5354 return; // Nothing to do. 5355 // Let's replace with provided value then. 5356 Replacement = AlternativeReplacement; 5357 } 5358 std::replace_if(Values.begin(), Values.end(), Predicate, Replacement); 5359 } 5360 5361 /// Given an ISD::UREM used only by an ISD::SETEQ or ISD::SETNE 5362 /// where the divisor is constant and the comparison target is zero, 5363 /// return a DAG expression that will generate the same comparison result 5364 /// using only multiplications, additions and shifts/rotations. 5365 /// Ref: "Hacker's Delight" 10-17. 5366 SDValue TargetLowering::buildUREMEqFold(EVT SETCCVT, SDValue REMNode, 5367 SDValue CompTargetNode, 5368 ISD::CondCode Cond, 5369 DAGCombinerInfo &DCI, 5370 const SDLoc &DL) const { 5371 SmallVector<SDNode *, 5> Built; 5372 if (SDValue Folded = prepareUREMEqFold(SETCCVT, REMNode, CompTargetNode, Cond, 5373 DCI, DL, Built)) { 5374 for (SDNode *N : Built) 5375 DCI.AddToWorklist(N); 5376 return Folded; 5377 } 5378 5379 return SDValue(); 5380 } 5381 5382 SDValue 5383 TargetLowering::prepareUREMEqFold(EVT SETCCVT, SDValue REMNode, 5384 SDValue CompTargetNode, ISD::CondCode Cond, 5385 DAGCombinerInfo &DCI, const SDLoc &DL, 5386 SmallVectorImpl<SDNode *> &Created) const { 5387 // fold (seteq/ne (urem N, D), 0) -> (setule/ugt (rotr (mul N, P), K), Q) 5388 // - D must be constant, with D = D0 * 2^K where D0 is odd 5389 // - P is the multiplicative inverse of D0 modulo 2^W 5390 // - Q = floor(((2^W) - 1) / D) 5391 // where W is the width of the common type of N and D. 5392 assert((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 5393 "Only applicable for (in)equality comparisons."); 5394 5395 SelectionDAG &DAG = DCI.DAG; 5396 5397 EVT VT = REMNode.getValueType(); 5398 EVT SVT = VT.getScalarType(); 5399 EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout()); 5400 EVT ShSVT = ShVT.getScalarType(); 5401 5402 // If MUL is unavailable, we cannot proceed in any case. 5403 if (!isOperationLegalOrCustom(ISD::MUL, VT)) 5404 return SDValue(); 5405 5406 bool ComparingWithAllZeros = true; 5407 bool AllComparisonsWithNonZerosAreTautological = true; 5408 bool HadTautologicalLanes = false; 5409 bool AllLanesAreTautological = true; 5410 bool HadEvenDivisor = false; 5411 bool AllDivisorsArePowerOfTwo = true; 5412 bool HadTautologicalInvertedLanes = false; 5413 SmallVector<SDValue, 16> PAmts, KAmts, QAmts, IAmts; 5414 5415 auto BuildUREMPattern = [&](ConstantSDNode *CDiv, ConstantSDNode *CCmp) { 5416 // Division by 0 is UB. Leave it to be constant-folded elsewhere. 5417 if (CDiv->isNullValue()) 5418 return false; 5419 5420 const APInt &D = CDiv->getAPIntValue(); 5421 const APInt &Cmp = CCmp->getAPIntValue(); 5422 5423 ComparingWithAllZeros &= Cmp.isNullValue(); 5424 5425 // x u% C1` is *always* less than C1. So given `x u% C1 == C2`, 5426 // if C2 is not less than C1, the comparison is always false. 5427 // But we will only be able to produce the comparison that will give the 5428 // opposive tautological answer. So this lane would need to be fixed up. 5429 bool TautologicalInvertedLane = D.ule(Cmp); 5430 HadTautologicalInvertedLanes |= TautologicalInvertedLane; 5431 5432 // If all lanes are tautological (either all divisors are ones, or divisor 5433 // is not greater than the constant we are comparing with), 5434 // we will prefer to avoid the fold. 5435 bool TautologicalLane = D.isOneValue() || TautologicalInvertedLane; 5436 HadTautologicalLanes |= TautologicalLane; 5437 AllLanesAreTautological &= TautologicalLane; 5438 5439 // If we are comparing with non-zero, we need'll need to subtract said 5440 // comparison value from the LHS. But there is no point in doing that if 5441 // every lane where we are comparing with non-zero is tautological.. 5442 if (!Cmp.isNullValue()) 5443 AllComparisonsWithNonZerosAreTautological &= TautologicalLane; 5444 5445 // Decompose D into D0 * 2^K 5446 unsigned K = D.countTrailingZeros(); 5447 assert((!D.isOneValue() || (K == 0)) && "For divisor '1' we won't rotate."); 5448 APInt D0 = D.lshr(K); 5449 5450 // D is even if it has trailing zeros. 5451 HadEvenDivisor |= (K != 0); 5452 // D is a power-of-two if D0 is one. 5453 // If all divisors are power-of-two, we will prefer to avoid the fold. 5454 AllDivisorsArePowerOfTwo &= D0.isOneValue(); 5455 5456 // P = inv(D0, 2^W) 5457 // 2^W requires W + 1 bits, so we have to extend and then truncate. 5458 unsigned W = D.getBitWidth(); 5459 APInt P = D0.zext(W + 1) 5460 .multiplicativeInverse(APInt::getSignedMinValue(W + 1)) 5461 .trunc(W); 5462 assert(!P.isNullValue() && "No multiplicative inverse!"); // unreachable 5463 assert((D0 * P).isOneValue() && "Multiplicative inverse sanity check."); 5464 5465 // Q = floor((2^W - 1) u/ D) 5466 // R = ((2^W - 1) u% D) 5467 APInt Q, R; 5468 APInt::udivrem(APInt::getAllOnesValue(W), D, Q, R); 5469 5470 // If we are comparing with zero, then that comparison constant is okay, 5471 // else it may need to be one less than that. 5472 if (Cmp.ugt(R)) 5473 Q -= 1; 5474 5475 assert(APInt::getAllOnesValue(ShSVT.getSizeInBits()).ugt(K) && 5476 "We are expecting that K is always less than all-ones for ShSVT"); 5477 5478 // If the lane is tautological the result can be constant-folded. 5479 if (TautologicalLane) { 5480 // Set P and K amount to a bogus values so we can try to splat them. 5481 P = 0; 5482 K = -1; 5483 // And ensure that comparison constant is tautological, 5484 // it will always compare true/false. 5485 Q = -1; 5486 } 5487 5488 PAmts.push_back(DAG.getConstant(P, DL, SVT)); 5489 KAmts.push_back( 5490 DAG.getConstant(APInt(ShSVT.getSizeInBits(), K), DL, ShSVT)); 5491 QAmts.push_back(DAG.getConstant(Q, DL, SVT)); 5492 return true; 5493 }; 5494 5495 SDValue N = REMNode.getOperand(0); 5496 SDValue D = REMNode.getOperand(1); 5497 5498 // Collect the values from each element. 5499 if (!ISD::matchBinaryPredicate(D, CompTargetNode, BuildUREMPattern)) 5500 return SDValue(); 5501 5502 // If all lanes are tautological, the result can be constant-folded. 5503 if (AllLanesAreTautological) 5504 return SDValue(); 5505 5506 // If this is a urem by a powers-of-two, avoid the fold since it can be 5507 // best implemented as a bit test. 5508 if (AllDivisorsArePowerOfTwo) 5509 return SDValue(); 5510 5511 SDValue PVal, KVal, QVal; 5512 if (VT.isVector()) { 5513 if (HadTautologicalLanes) { 5514 // Try to turn PAmts into a splat, since we don't care about the values 5515 // that are currently '0'. If we can't, just keep '0'`s. 5516 turnVectorIntoSplatVector(PAmts, isNullConstant); 5517 // Try to turn KAmts into a splat, since we don't care about the values 5518 // that are currently '-1'. If we can't, change them to '0'`s. 5519 turnVectorIntoSplatVector(KAmts, isAllOnesConstant, 5520 DAG.getConstant(0, DL, ShSVT)); 5521 } 5522 5523 PVal = DAG.getBuildVector(VT, DL, PAmts); 5524 KVal = DAG.getBuildVector(ShVT, DL, KAmts); 5525 QVal = DAG.getBuildVector(VT, DL, QAmts); 5526 } else { 5527 PVal = PAmts[0]; 5528 KVal = KAmts[0]; 5529 QVal = QAmts[0]; 5530 } 5531 5532 if (!ComparingWithAllZeros && !AllComparisonsWithNonZerosAreTautological) { 5533 if (!isOperationLegalOrCustom(ISD::SUB, VT)) 5534 return SDValue(); // FIXME: Could/should use `ISD::ADD`? 5535 assert(CompTargetNode.getValueType() == N.getValueType() && 5536 "Expecting that the types on LHS and RHS of comparisons match."); 5537 N = DAG.getNode(ISD::SUB, DL, VT, N, CompTargetNode); 5538 } 5539 5540 // (mul N, P) 5541 SDValue Op0 = DAG.getNode(ISD::MUL, DL, VT, N, PVal); 5542 Created.push_back(Op0.getNode()); 5543 5544 // Rotate right only if any divisor was even. We avoid rotates for all-odd 5545 // divisors as a performance improvement, since rotating by 0 is a no-op. 5546 if (HadEvenDivisor) { 5547 // We need ROTR to do this. 5548 if (!isOperationLegalOrCustom(ISD::ROTR, VT)) 5549 return SDValue(); 5550 SDNodeFlags Flags; 5551 Flags.setExact(true); 5552 // UREM: (rotr (mul N, P), K) 5553 Op0 = DAG.getNode(ISD::ROTR, DL, VT, Op0, KVal, Flags); 5554 Created.push_back(Op0.getNode()); 5555 } 5556 5557 // UREM: (setule/setugt (rotr (mul N, P), K), Q) 5558 SDValue NewCC = 5559 DAG.getSetCC(DL, SETCCVT, Op0, QVal, 5560 ((Cond == ISD::SETEQ) ? ISD::SETULE : ISD::SETUGT)); 5561 if (!HadTautologicalInvertedLanes) 5562 return NewCC; 5563 5564 // If any lanes previously compared always-false, the NewCC will give 5565 // always-true result for them, so we need to fixup those lanes. 5566 // Or the other way around for inequality predicate. 5567 assert(VT.isVector() && "Can/should only get here for vectors."); 5568 Created.push_back(NewCC.getNode()); 5569 5570 // x u% C1` is *always* less than C1. So given `x u% C1 == C2`, 5571 // if C2 is not less than C1, the comparison is always false. 5572 // But we have produced the comparison that will give the 5573 // opposive tautological answer. So these lanes would need to be fixed up. 5574 SDValue TautologicalInvertedChannels = 5575 DAG.getSetCC(DL, SETCCVT, D, CompTargetNode, ISD::SETULE); 5576 Created.push_back(TautologicalInvertedChannels.getNode()); 5577 5578 if (isOperationLegalOrCustom(ISD::VSELECT, SETCCVT)) { 5579 // If we have a vector select, let's replace the comparison results in the 5580 // affected lanes with the correct tautological result. 5581 SDValue Replacement = DAG.getBoolConstant(Cond == ISD::SETEQ ? false : true, 5582 DL, SETCCVT, SETCCVT); 5583 return DAG.getNode(ISD::VSELECT, DL, SETCCVT, TautologicalInvertedChannels, 5584 Replacement, NewCC); 5585 } 5586 5587 // Else, we can just invert the comparison result in the appropriate lanes. 5588 if (isOperationLegalOrCustom(ISD::XOR, SETCCVT)) 5589 return DAG.getNode(ISD::XOR, DL, SETCCVT, NewCC, 5590 TautologicalInvertedChannels); 5591 5592 return SDValue(); // Don't know how to lower. 5593 } 5594 5595 /// Given an ISD::SREM used only by an ISD::SETEQ or ISD::SETNE 5596 /// where the divisor is constant and the comparison target is zero, 5597 /// return a DAG expression that will generate the same comparison result 5598 /// using only multiplications, additions and shifts/rotations. 5599 /// Ref: "Hacker's Delight" 10-17. 5600 SDValue TargetLowering::buildSREMEqFold(EVT SETCCVT, SDValue REMNode, 5601 SDValue CompTargetNode, 5602 ISD::CondCode Cond, 5603 DAGCombinerInfo &DCI, 5604 const SDLoc &DL) const { 5605 SmallVector<SDNode *, 7> Built; 5606 if (SDValue Folded = prepareSREMEqFold(SETCCVT, REMNode, CompTargetNode, Cond, 5607 DCI, DL, Built)) { 5608 assert(Built.size() <= 7 && "Max size prediction failed."); 5609 for (SDNode *N : Built) 5610 DCI.AddToWorklist(N); 5611 return Folded; 5612 } 5613 5614 return SDValue(); 5615 } 5616 5617 SDValue 5618 TargetLowering::prepareSREMEqFold(EVT SETCCVT, SDValue REMNode, 5619 SDValue CompTargetNode, ISD::CondCode Cond, 5620 DAGCombinerInfo &DCI, const SDLoc &DL, 5621 SmallVectorImpl<SDNode *> &Created) const { 5622 // Fold: 5623 // (seteq/ne (srem N, D), 0) 5624 // To: 5625 // (setule/ugt (rotr (add (mul N, P), A), K), Q) 5626 // 5627 // - D must be constant, with D = D0 * 2^K where D0 is odd 5628 // - P is the multiplicative inverse of D0 modulo 2^W 5629 // - A = bitwiseand(floor((2^(W - 1) - 1) / D0), (-(2^k))) 5630 // - Q = floor((2 * A) / (2^K)) 5631 // where W is the width of the common type of N and D. 5632 assert((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 5633 "Only applicable for (in)equality comparisons."); 5634 5635 SelectionDAG &DAG = DCI.DAG; 5636 5637 EVT VT = REMNode.getValueType(); 5638 EVT SVT = VT.getScalarType(); 5639 EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout()); 5640 EVT ShSVT = ShVT.getScalarType(); 5641 5642 // If MUL is unavailable, we cannot proceed in any case. 5643 if (!isOperationLegalOrCustom(ISD::MUL, VT)) 5644 return SDValue(); 5645 5646 // TODO: Could support comparing with non-zero too. 5647 ConstantSDNode *CompTarget = isConstOrConstSplat(CompTargetNode); 5648 if (!CompTarget || !CompTarget->isNullValue()) 5649 return SDValue(); 5650 5651 bool HadIntMinDivisor = false; 5652 bool HadOneDivisor = false; 5653 bool AllDivisorsAreOnes = true; 5654 bool HadEvenDivisor = false; 5655 bool NeedToApplyOffset = false; 5656 bool AllDivisorsArePowerOfTwo = true; 5657 SmallVector<SDValue, 16> PAmts, AAmts, KAmts, QAmts; 5658 5659 auto BuildSREMPattern = [&](ConstantSDNode *C) { 5660 // Division by 0 is UB. Leave it to be constant-folded elsewhere. 5661 if (C->isNullValue()) 5662 return false; 5663 5664 // FIXME: we don't fold `rem %X, -C` to `rem %X, C` in DAGCombine. 5665 5666 // WARNING: this fold is only valid for positive divisors! 5667 APInt D = C->getAPIntValue(); 5668 if (D.isNegative()) 5669 D.negate(); // `rem %X, -C` is equivalent to `rem %X, C` 5670 5671 HadIntMinDivisor |= D.isMinSignedValue(); 5672 5673 // If all divisors are ones, we will prefer to avoid the fold. 5674 HadOneDivisor |= D.isOneValue(); 5675 AllDivisorsAreOnes &= D.isOneValue(); 5676 5677 // Decompose D into D0 * 2^K 5678 unsigned K = D.countTrailingZeros(); 5679 assert((!D.isOneValue() || (K == 0)) && "For divisor '1' we won't rotate."); 5680 APInt D0 = D.lshr(K); 5681 5682 if (!D.isMinSignedValue()) { 5683 // D is even if it has trailing zeros; unless it's INT_MIN, in which case 5684 // we don't care about this lane in this fold, we'll special-handle it. 5685 HadEvenDivisor |= (K != 0); 5686 } 5687 5688 // D is a power-of-two if D0 is one. This includes INT_MIN. 5689 // If all divisors are power-of-two, we will prefer to avoid the fold. 5690 AllDivisorsArePowerOfTwo &= D0.isOneValue(); 5691 5692 // P = inv(D0, 2^W) 5693 // 2^W requires W + 1 bits, so we have to extend and then truncate. 5694 unsigned W = D.getBitWidth(); 5695 APInt P = D0.zext(W + 1) 5696 .multiplicativeInverse(APInt::getSignedMinValue(W + 1)) 5697 .trunc(W); 5698 assert(!P.isNullValue() && "No multiplicative inverse!"); // unreachable 5699 assert((D0 * P).isOneValue() && "Multiplicative inverse sanity check."); 5700 5701 // A = floor((2^(W - 1) - 1) / D0) & -2^K 5702 APInt A = APInt::getSignedMaxValue(W).udiv(D0); 5703 A.clearLowBits(K); 5704 5705 if (!D.isMinSignedValue()) { 5706 // If divisor INT_MIN, then we don't care about this lane in this fold, 5707 // we'll special-handle it. 5708 NeedToApplyOffset |= A != 0; 5709 } 5710 5711 // Q = floor((2 * A) / (2^K)) 5712 APInt Q = (2 * A).udiv(APInt::getOneBitSet(W, K)); 5713 5714 assert(APInt::getAllOnesValue(SVT.getSizeInBits()).ugt(A) && 5715 "We are expecting that A is always less than all-ones for SVT"); 5716 assert(APInt::getAllOnesValue(ShSVT.getSizeInBits()).ugt(K) && 5717 "We are expecting that K is always less than all-ones for ShSVT"); 5718 5719 // If the divisor is 1 the result can be constant-folded. Likewise, we 5720 // don't care about INT_MIN lanes, those can be set to undef if appropriate. 5721 if (D.isOneValue()) { 5722 // Set P, A and K to a bogus values so we can try to splat them. 5723 P = 0; 5724 A = -1; 5725 K = -1; 5726 5727 // x ?% 1 == 0 <--> true <--> x u<= -1 5728 Q = -1; 5729 } 5730 5731 PAmts.push_back(DAG.getConstant(P, DL, SVT)); 5732 AAmts.push_back(DAG.getConstant(A, DL, SVT)); 5733 KAmts.push_back( 5734 DAG.getConstant(APInt(ShSVT.getSizeInBits(), K), DL, ShSVT)); 5735 QAmts.push_back(DAG.getConstant(Q, DL, SVT)); 5736 return true; 5737 }; 5738 5739 SDValue N = REMNode.getOperand(0); 5740 SDValue D = REMNode.getOperand(1); 5741 5742 // Collect the values from each element. 5743 if (!ISD::matchUnaryPredicate(D, BuildSREMPattern)) 5744 return SDValue(); 5745 5746 // If this is a srem by a one, avoid the fold since it can be constant-folded. 5747 if (AllDivisorsAreOnes) 5748 return SDValue(); 5749 5750 // If this is a srem by a powers-of-two (including INT_MIN), avoid the fold 5751 // since it can be best implemented as a bit test. 5752 if (AllDivisorsArePowerOfTwo) 5753 return SDValue(); 5754 5755 SDValue PVal, AVal, KVal, QVal; 5756 if (VT.isFixedLengthVector()) { 5757 if (HadOneDivisor) { 5758 // Try to turn PAmts into a splat, since we don't care about the values 5759 // that are currently '0'. If we can't, just keep '0'`s. 5760 turnVectorIntoSplatVector(PAmts, isNullConstant); 5761 // Try to turn AAmts into a splat, since we don't care about the 5762 // values that are currently '-1'. If we can't, change them to '0'`s. 5763 turnVectorIntoSplatVector(AAmts, isAllOnesConstant, 5764 DAG.getConstant(0, DL, SVT)); 5765 // Try to turn KAmts into a splat, since we don't care about the values 5766 // that are currently '-1'. If we can't, change them to '0'`s. 5767 turnVectorIntoSplatVector(KAmts, isAllOnesConstant, 5768 DAG.getConstant(0, DL, ShSVT)); 5769 } 5770 5771 PVal = DAG.getBuildVector(VT, DL, PAmts); 5772 AVal = DAG.getBuildVector(VT, DL, AAmts); 5773 KVal = DAG.getBuildVector(ShVT, DL, KAmts); 5774 QVal = DAG.getBuildVector(VT, DL, QAmts); 5775 } else if (VT.isScalableVector()) { 5776 assert(PAmts.size() == 1 && AAmts.size() == 1 && KAmts.size() == 1 && 5777 QAmts.size() == 1 && 5778 "Expected matchUnaryPredicate to return one element for scalable " 5779 "vectors"); 5780 PVal = DAG.getSplatVector(VT, DL, PAmts[0]); 5781 AVal = DAG.getSplatVector(VT, DL, AAmts[0]); 5782 KVal = DAG.getSplatVector(ShVT, DL, KAmts[0]); 5783 QVal = DAG.getSplatVector(VT, DL, QAmts[0]); 5784 } else { 5785 PVal = PAmts[0]; 5786 AVal = AAmts[0]; 5787 KVal = KAmts[0]; 5788 QVal = QAmts[0]; 5789 } 5790 5791 // (mul N, P) 5792 SDValue Op0 = DAG.getNode(ISD::MUL, DL, VT, N, PVal); 5793 Created.push_back(Op0.getNode()); 5794 5795 if (NeedToApplyOffset) { 5796 // We need ADD to do this. 5797 if (!isOperationLegalOrCustom(ISD::ADD, VT)) 5798 return SDValue(); 5799 5800 // (add (mul N, P), A) 5801 Op0 = DAG.getNode(ISD::ADD, DL, VT, Op0, AVal); 5802 Created.push_back(Op0.getNode()); 5803 } 5804 5805 // Rotate right only if any divisor was even. We avoid rotates for all-odd 5806 // divisors as a performance improvement, since rotating by 0 is a no-op. 5807 if (HadEvenDivisor) { 5808 // We need ROTR to do this. 5809 if (!isOperationLegalOrCustom(ISD::ROTR, VT)) 5810 return SDValue(); 5811 SDNodeFlags Flags; 5812 Flags.setExact(true); 5813 // SREM: (rotr (add (mul N, P), A), K) 5814 Op0 = DAG.getNode(ISD::ROTR, DL, VT, Op0, KVal, Flags); 5815 Created.push_back(Op0.getNode()); 5816 } 5817 5818 // SREM: (setule/setugt (rotr (add (mul N, P), A), K), Q) 5819 SDValue Fold = 5820 DAG.getSetCC(DL, SETCCVT, Op0, QVal, 5821 ((Cond == ISD::SETEQ) ? ISD::SETULE : ISD::SETUGT)); 5822 5823 // If we didn't have lanes with INT_MIN divisor, then we're done. 5824 if (!HadIntMinDivisor) 5825 return Fold; 5826 5827 // That fold is only valid for positive divisors. Which effectively means, 5828 // it is invalid for INT_MIN divisors. So if we have such a lane, 5829 // we must fix-up results for said lanes. 5830 assert(VT.isVector() && "Can/should only get here for vectors."); 5831 5832 if (!isOperationLegalOrCustom(ISD::SETEQ, VT) || 5833 !isOperationLegalOrCustom(ISD::AND, VT) || 5834 !isOperationLegalOrCustom(Cond, VT) || 5835 !isOperationLegalOrCustom(ISD::VSELECT, VT)) 5836 return SDValue(); 5837 5838 Created.push_back(Fold.getNode()); 5839 5840 SDValue IntMin = DAG.getConstant( 5841 APInt::getSignedMinValue(SVT.getScalarSizeInBits()), DL, VT); 5842 SDValue IntMax = DAG.getConstant( 5843 APInt::getSignedMaxValue(SVT.getScalarSizeInBits()), DL, VT); 5844 SDValue Zero = 5845 DAG.getConstant(APInt::getNullValue(SVT.getScalarSizeInBits()), DL, VT); 5846 5847 // Which lanes had INT_MIN divisors? Divisor is constant, so const-folded. 5848 SDValue DivisorIsIntMin = DAG.getSetCC(DL, SETCCVT, D, IntMin, ISD::SETEQ); 5849 Created.push_back(DivisorIsIntMin.getNode()); 5850 5851 // (N s% INT_MIN) ==/!= 0 <--> (N & INT_MAX) ==/!= 0 5852 SDValue Masked = DAG.getNode(ISD::AND, DL, VT, N, IntMax); 5853 Created.push_back(Masked.getNode()); 5854 SDValue MaskedIsZero = DAG.getSetCC(DL, SETCCVT, Masked, Zero, Cond); 5855 Created.push_back(MaskedIsZero.getNode()); 5856 5857 // To produce final result we need to blend 2 vectors: 'SetCC' and 5858 // 'MaskedIsZero'. If the divisor for channel was *NOT* INT_MIN, we pick 5859 // from 'Fold', else pick from 'MaskedIsZero'. Since 'DivisorIsIntMin' is 5860 // constant-folded, select can get lowered to a shuffle with constant mask. 5861 SDValue Blended = 5862 DAG.getNode(ISD::VSELECT, DL, VT, DivisorIsIntMin, MaskedIsZero, Fold); 5863 5864 return Blended; 5865 } 5866 5867 bool TargetLowering:: 5868 verifyReturnAddressArgumentIsConstant(SDValue Op, SelectionDAG &DAG) const { 5869 if (!isa<ConstantSDNode>(Op.getOperand(0))) { 5870 DAG.getContext()->emitError("argument to '__builtin_return_address' must " 5871 "be a constant integer"); 5872 return true; 5873 } 5874 5875 return false; 5876 } 5877 5878 SDValue TargetLowering::getSqrtInputTest(SDValue Op, SelectionDAG &DAG, 5879 const DenormalMode &Mode) const { 5880 SDLoc DL(Op); 5881 EVT VT = Op.getValueType(); 5882 EVT CCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 5883 SDValue FPZero = DAG.getConstantFP(0.0, DL, VT); 5884 // Testing it with denormal inputs to avoid wrong estimate. 5885 if (Mode.Input == DenormalMode::IEEE) { 5886 // This is specifically a check for the handling of denormal inputs, 5887 // not the result. 5888 5889 // Test = fabs(X) < SmallestNormal 5890 const fltSemantics &FltSem = DAG.EVTToAPFloatSemantics(VT); 5891 APFloat SmallestNorm = APFloat::getSmallestNormalized(FltSem); 5892 SDValue NormC = DAG.getConstantFP(SmallestNorm, DL, VT); 5893 SDValue Fabs = DAG.getNode(ISD::FABS, DL, VT, Op); 5894 return DAG.getSetCC(DL, CCVT, Fabs, NormC, ISD::SETLT); 5895 } 5896 // Test = X == 0.0 5897 return DAG.getSetCC(DL, CCVT, Op, FPZero, ISD::SETEQ); 5898 } 5899 5900 SDValue TargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG, 5901 bool LegalOps, bool OptForSize, 5902 NegatibleCost &Cost, 5903 unsigned Depth) const { 5904 // fneg is removable even if it has multiple uses. 5905 if (Op.getOpcode() == ISD::FNEG) { 5906 Cost = NegatibleCost::Cheaper; 5907 return Op.getOperand(0); 5908 } 5909 5910 // Don't recurse exponentially. 5911 if (Depth > SelectionDAG::MaxRecursionDepth) 5912 return SDValue(); 5913 5914 // Pre-increment recursion depth for use in recursive calls. 5915 ++Depth; 5916 const SDNodeFlags Flags = Op->getFlags(); 5917 const TargetOptions &Options = DAG.getTarget().Options; 5918 EVT VT = Op.getValueType(); 5919 unsigned Opcode = Op.getOpcode(); 5920 5921 // Don't allow anything with multiple uses unless we know it is free. 5922 if (!Op.hasOneUse() && Opcode != ISD::ConstantFP) { 5923 bool IsFreeExtend = Opcode == ISD::FP_EXTEND && 5924 isFPExtFree(VT, Op.getOperand(0).getValueType()); 5925 if (!IsFreeExtend) 5926 return SDValue(); 5927 } 5928 5929 auto RemoveDeadNode = [&](SDValue N) { 5930 if (N && N.getNode()->use_empty()) 5931 DAG.RemoveDeadNode(N.getNode()); 5932 }; 5933 5934 SDLoc DL(Op); 5935 5936 switch (Opcode) { 5937 case ISD::ConstantFP: { 5938 // Don't invert constant FP values after legalization unless the target says 5939 // the negated constant is legal. 5940 bool IsOpLegal = 5941 isOperationLegal(ISD::ConstantFP, VT) || 5942 isFPImmLegal(neg(cast<ConstantFPSDNode>(Op)->getValueAPF()), VT, 5943 OptForSize); 5944 5945 if (LegalOps && !IsOpLegal) 5946 break; 5947 5948 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF(); 5949 V.changeSign(); 5950 SDValue CFP = DAG.getConstantFP(V, DL, VT); 5951 5952 // If we already have the use of the negated floating constant, it is free 5953 // to negate it even it has multiple uses. 5954 if (!Op.hasOneUse() && CFP.use_empty()) 5955 break; 5956 Cost = NegatibleCost::Neutral; 5957 return CFP; 5958 } 5959 case ISD::BUILD_VECTOR: { 5960 // Only permit BUILD_VECTOR of constants. 5961 if (llvm::any_of(Op->op_values(), [&](SDValue N) { 5962 return !N.isUndef() && !isa<ConstantFPSDNode>(N); 5963 })) 5964 break; 5965 5966 bool IsOpLegal = 5967 (isOperationLegal(ISD::ConstantFP, VT) && 5968 isOperationLegal(ISD::BUILD_VECTOR, VT)) || 5969 llvm::all_of(Op->op_values(), [&](SDValue N) { 5970 return N.isUndef() || 5971 isFPImmLegal(neg(cast<ConstantFPSDNode>(N)->getValueAPF()), VT, 5972 OptForSize); 5973 }); 5974 5975 if (LegalOps && !IsOpLegal) 5976 break; 5977 5978 SmallVector<SDValue, 4> Ops; 5979 for (SDValue C : Op->op_values()) { 5980 if (C.isUndef()) { 5981 Ops.push_back(C); 5982 continue; 5983 } 5984 APFloat V = cast<ConstantFPSDNode>(C)->getValueAPF(); 5985 V.changeSign(); 5986 Ops.push_back(DAG.getConstantFP(V, DL, C.getValueType())); 5987 } 5988 Cost = NegatibleCost::Neutral; 5989 return DAG.getBuildVector(VT, DL, Ops); 5990 } 5991 case ISD::FADD: { 5992 if (!Options.NoSignedZerosFPMath && !Flags.hasNoSignedZeros()) 5993 break; 5994 5995 // After operation legalization, it might not be legal to create new FSUBs. 5996 if (LegalOps && !isOperationLegalOrCustom(ISD::FSUB, VT)) 5997 break; 5998 SDValue X = Op.getOperand(0), Y = Op.getOperand(1); 5999 6000 // fold (fneg (fadd X, Y)) -> (fsub (fneg X), Y) 6001 NegatibleCost CostX = NegatibleCost::Expensive; 6002 SDValue NegX = 6003 getNegatedExpression(X, DAG, LegalOps, OptForSize, CostX, Depth); 6004 // fold (fneg (fadd X, Y)) -> (fsub (fneg Y), X) 6005 NegatibleCost CostY = NegatibleCost::Expensive; 6006 SDValue NegY = 6007 getNegatedExpression(Y, DAG, LegalOps, OptForSize, CostY, Depth); 6008 6009 // Negate the X if its cost is less or equal than Y. 6010 if (NegX && (CostX <= CostY)) { 6011 Cost = CostX; 6012 SDValue N = DAG.getNode(ISD::FSUB, DL, VT, NegX, Y, Flags); 6013 if (NegY != N) 6014 RemoveDeadNode(NegY); 6015 return N; 6016 } 6017 6018 // Negate the Y if it is not expensive. 6019 if (NegY) { 6020 Cost = CostY; 6021 SDValue N = DAG.getNode(ISD::FSUB, DL, VT, NegY, X, Flags); 6022 if (NegX != N) 6023 RemoveDeadNode(NegX); 6024 return N; 6025 } 6026 break; 6027 } 6028 case ISD::FSUB: { 6029 // We can't turn -(A-B) into B-A when we honor signed zeros. 6030 if (!Options.NoSignedZerosFPMath && !Flags.hasNoSignedZeros()) 6031 break; 6032 6033 SDValue X = Op.getOperand(0), Y = Op.getOperand(1); 6034 // fold (fneg (fsub 0, Y)) -> Y 6035 if (ConstantFPSDNode *C = isConstOrConstSplatFP(X, /*AllowUndefs*/ true)) 6036 if (C->isZero()) { 6037 Cost = NegatibleCost::Cheaper; 6038 return Y; 6039 } 6040 6041 // fold (fneg (fsub X, Y)) -> (fsub Y, X) 6042 Cost = NegatibleCost::Neutral; 6043 return DAG.getNode(ISD::FSUB, DL, VT, Y, X, Flags); 6044 } 6045 case ISD::FMUL: 6046 case ISD::FDIV: { 6047 SDValue X = Op.getOperand(0), Y = Op.getOperand(1); 6048 6049 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) 6050 NegatibleCost CostX = NegatibleCost::Expensive; 6051 SDValue NegX = 6052 getNegatedExpression(X, DAG, LegalOps, OptForSize, CostX, Depth); 6053 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y)) 6054 NegatibleCost CostY = NegatibleCost::Expensive; 6055 SDValue NegY = 6056 getNegatedExpression(Y, DAG, LegalOps, OptForSize, CostY, Depth); 6057 6058 // Negate the X if its cost is less or equal than Y. 6059 if (NegX && (CostX <= CostY)) { 6060 Cost = CostX; 6061 SDValue N = DAG.getNode(Opcode, DL, VT, NegX, Y, Flags); 6062 if (NegY != N) 6063 RemoveDeadNode(NegY); 6064 return N; 6065 } 6066 6067 // Ignore X * 2.0 because that is expected to be canonicalized to X + X. 6068 if (auto *C = isConstOrConstSplatFP(Op.getOperand(1))) 6069 if (C->isExactlyValue(2.0) && Op.getOpcode() == ISD::FMUL) 6070 break; 6071 6072 // Negate the Y if it is not expensive. 6073 if (NegY) { 6074 Cost = CostY; 6075 SDValue N = DAG.getNode(Opcode, DL, VT, X, NegY, Flags); 6076 if (NegX != N) 6077 RemoveDeadNode(NegX); 6078 return N; 6079 } 6080 break; 6081 } 6082 case ISD::FMA: 6083 case ISD::FMAD: { 6084 if (!Options.NoSignedZerosFPMath && !Flags.hasNoSignedZeros()) 6085 break; 6086 6087 SDValue X = Op.getOperand(0), Y = Op.getOperand(1), Z = Op.getOperand(2); 6088 NegatibleCost CostZ = NegatibleCost::Expensive; 6089 SDValue NegZ = 6090 getNegatedExpression(Z, DAG, LegalOps, OptForSize, CostZ, Depth); 6091 // Give up if fail to negate the Z. 6092 if (!NegZ) 6093 break; 6094 6095 // fold (fneg (fma X, Y, Z)) -> (fma (fneg X), Y, (fneg Z)) 6096 NegatibleCost CostX = NegatibleCost::Expensive; 6097 SDValue NegX = 6098 getNegatedExpression(X, DAG, LegalOps, OptForSize, CostX, Depth); 6099 // fold (fneg (fma X, Y, Z)) -> (fma X, (fneg Y), (fneg Z)) 6100 NegatibleCost CostY = NegatibleCost::Expensive; 6101 SDValue NegY = 6102 getNegatedExpression(Y, DAG, LegalOps, OptForSize, CostY, Depth); 6103 6104 // Negate the X if its cost is less or equal than Y. 6105 if (NegX && (CostX <= CostY)) { 6106 Cost = std::min(CostX, CostZ); 6107 SDValue N = DAG.getNode(Opcode, DL, VT, NegX, Y, NegZ, Flags); 6108 if (NegY != N) 6109 RemoveDeadNode(NegY); 6110 return N; 6111 } 6112 6113 // Negate the Y if it is not expensive. 6114 if (NegY) { 6115 Cost = std::min(CostY, CostZ); 6116 SDValue N = DAG.getNode(Opcode, DL, VT, X, NegY, NegZ, Flags); 6117 if (NegX != N) 6118 RemoveDeadNode(NegX); 6119 return N; 6120 } 6121 break; 6122 } 6123 6124 case ISD::FP_EXTEND: 6125 case ISD::FSIN: 6126 if (SDValue NegV = getNegatedExpression(Op.getOperand(0), DAG, LegalOps, 6127 OptForSize, Cost, Depth)) 6128 return DAG.getNode(Opcode, DL, VT, NegV); 6129 break; 6130 case ISD::FP_ROUND: 6131 if (SDValue NegV = getNegatedExpression(Op.getOperand(0), DAG, LegalOps, 6132 OptForSize, Cost, Depth)) 6133 return DAG.getNode(ISD::FP_ROUND, DL, VT, NegV, Op.getOperand(1)); 6134 break; 6135 } 6136 6137 return SDValue(); 6138 } 6139 6140 //===----------------------------------------------------------------------===// 6141 // Legalization Utilities 6142 //===----------------------------------------------------------------------===// 6143 6144 bool TargetLowering::expandMUL_LOHI(unsigned Opcode, EVT VT, const SDLoc &dl, 6145 SDValue LHS, SDValue RHS, 6146 SmallVectorImpl<SDValue> &Result, 6147 EVT HiLoVT, SelectionDAG &DAG, 6148 MulExpansionKind Kind, SDValue LL, 6149 SDValue LH, SDValue RL, SDValue RH) const { 6150 assert(Opcode == ISD::MUL || Opcode == ISD::UMUL_LOHI || 6151 Opcode == ISD::SMUL_LOHI); 6152 6153 bool HasMULHS = (Kind == MulExpansionKind::Always) || 6154 isOperationLegalOrCustom(ISD::MULHS, HiLoVT); 6155 bool HasMULHU = (Kind == MulExpansionKind::Always) || 6156 isOperationLegalOrCustom(ISD::MULHU, HiLoVT); 6157 bool HasSMUL_LOHI = (Kind == MulExpansionKind::Always) || 6158 isOperationLegalOrCustom(ISD::SMUL_LOHI, HiLoVT); 6159 bool HasUMUL_LOHI = (Kind == MulExpansionKind::Always) || 6160 isOperationLegalOrCustom(ISD::UMUL_LOHI, HiLoVT); 6161 6162 if (!HasMULHU && !HasMULHS && !HasUMUL_LOHI && !HasSMUL_LOHI) 6163 return false; 6164 6165 unsigned OuterBitSize = VT.getScalarSizeInBits(); 6166 unsigned InnerBitSize = HiLoVT.getScalarSizeInBits(); 6167 6168 // LL, LH, RL, and RH must be either all NULL or all set to a value. 6169 assert((LL.getNode() && LH.getNode() && RL.getNode() && RH.getNode()) || 6170 (!LL.getNode() && !LH.getNode() && !RL.getNode() && !RH.getNode())); 6171 6172 SDVTList VTs = DAG.getVTList(HiLoVT, HiLoVT); 6173 auto MakeMUL_LOHI = [&](SDValue L, SDValue R, SDValue &Lo, SDValue &Hi, 6174 bool Signed) -> bool { 6175 if ((Signed && HasSMUL_LOHI) || (!Signed && HasUMUL_LOHI)) { 6176 Lo = DAG.getNode(Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI, dl, VTs, L, R); 6177 Hi = SDValue(Lo.getNode(), 1); 6178 return true; 6179 } 6180 if ((Signed && HasMULHS) || (!Signed && HasMULHU)) { 6181 Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, L, R); 6182 Hi = DAG.getNode(Signed ? ISD::MULHS : ISD::MULHU, dl, HiLoVT, L, R); 6183 return true; 6184 } 6185 return false; 6186 }; 6187 6188 SDValue Lo, Hi; 6189 6190 if (!LL.getNode() && !RL.getNode() && 6191 isOperationLegalOrCustom(ISD::TRUNCATE, HiLoVT)) { 6192 LL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, LHS); 6193 RL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, RHS); 6194 } 6195 6196 if (!LL.getNode()) 6197 return false; 6198 6199 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize); 6200 if (DAG.MaskedValueIsZero(LHS, HighMask) && 6201 DAG.MaskedValueIsZero(RHS, HighMask)) { 6202 // The inputs are both zero-extended. 6203 if (MakeMUL_LOHI(LL, RL, Lo, Hi, false)) { 6204 Result.push_back(Lo); 6205 Result.push_back(Hi); 6206 if (Opcode != ISD::MUL) { 6207 SDValue Zero = DAG.getConstant(0, dl, HiLoVT); 6208 Result.push_back(Zero); 6209 Result.push_back(Zero); 6210 } 6211 return true; 6212 } 6213 } 6214 6215 if (!VT.isVector() && Opcode == ISD::MUL && 6216 DAG.ComputeNumSignBits(LHS) > InnerBitSize && 6217 DAG.ComputeNumSignBits(RHS) > InnerBitSize) { 6218 // The input values are both sign-extended. 6219 // TODO non-MUL case? 6220 if (MakeMUL_LOHI(LL, RL, Lo, Hi, true)) { 6221 Result.push_back(Lo); 6222 Result.push_back(Hi); 6223 return true; 6224 } 6225 } 6226 6227 unsigned ShiftAmount = OuterBitSize - InnerBitSize; 6228 EVT ShiftAmountTy = getShiftAmountTy(VT, DAG.getDataLayout()); 6229 if (APInt::getMaxValue(ShiftAmountTy.getSizeInBits()).ult(ShiftAmount)) { 6230 // FIXME getShiftAmountTy does not always return a sensible result when VT 6231 // is an illegal type, and so the type may be too small to fit the shift 6232 // amount. Override it with i32. The shift will have to be legalized. 6233 ShiftAmountTy = MVT::i32; 6234 } 6235 SDValue Shift = DAG.getConstant(ShiftAmount, dl, ShiftAmountTy); 6236 6237 if (!LH.getNode() && !RH.getNode() && 6238 isOperationLegalOrCustom(ISD::SRL, VT) && 6239 isOperationLegalOrCustom(ISD::TRUNCATE, HiLoVT)) { 6240 LH = DAG.getNode(ISD::SRL, dl, VT, LHS, Shift); 6241 LH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, LH); 6242 RH = DAG.getNode(ISD::SRL, dl, VT, RHS, Shift); 6243 RH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, RH); 6244 } 6245 6246 if (!LH.getNode()) 6247 return false; 6248 6249 if (!MakeMUL_LOHI(LL, RL, Lo, Hi, false)) 6250 return false; 6251 6252 Result.push_back(Lo); 6253 6254 if (Opcode == ISD::MUL) { 6255 RH = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RH); 6256 LH = DAG.getNode(ISD::MUL, dl, HiLoVT, LH, RL); 6257 Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, RH); 6258 Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, LH); 6259 Result.push_back(Hi); 6260 return true; 6261 } 6262 6263 // Compute the full width result. 6264 auto Merge = [&](SDValue Lo, SDValue Hi) -> SDValue { 6265 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo); 6266 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Hi); 6267 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift); 6268 return DAG.getNode(ISD::OR, dl, VT, Lo, Hi); 6269 }; 6270 6271 SDValue Next = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Hi); 6272 if (!MakeMUL_LOHI(LL, RH, Lo, Hi, false)) 6273 return false; 6274 6275 // This is effectively the add part of a multiply-add of half-sized operands, 6276 // so it cannot overflow. 6277 Next = DAG.getNode(ISD::ADD, dl, VT, Next, Merge(Lo, Hi)); 6278 6279 if (!MakeMUL_LOHI(LH, RL, Lo, Hi, false)) 6280 return false; 6281 6282 SDValue Zero = DAG.getConstant(0, dl, HiLoVT); 6283 EVT BoolType = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 6284 6285 bool UseGlue = (isOperationLegalOrCustom(ISD::ADDC, VT) && 6286 isOperationLegalOrCustom(ISD::ADDE, VT)); 6287 if (UseGlue) 6288 Next = DAG.getNode(ISD::ADDC, dl, DAG.getVTList(VT, MVT::Glue), Next, 6289 Merge(Lo, Hi)); 6290 else 6291 Next = DAG.getNode(ISD::ADDCARRY, dl, DAG.getVTList(VT, BoolType), Next, 6292 Merge(Lo, Hi), DAG.getConstant(0, dl, BoolType)); 6293 6294 SDValue Carry = Next.getValue(1); 6295 Result.push_back(DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, Next)); 6296 Next = DAG.getNode(ISD::SRL, dl, VT, Next, Shift); 6297 6298 if (!MakeMUL_LOHI(LH, RH, Lo, Hi, Opcode == ISD::SMUL_LOHI)) 6299 return false; 6300 6301 if (UseGlue) 6302 Hi = DAG.getNode(ISD::ADDE, dl, DAG.getVTList(HiLoVT, MVT::Glue), Hi, Zero, 6303 Carry); 6304 else 6305 Hi = DAG.getNode(ISD::ADDCARRY, dl, DAG.getVTList(HiLoVT, BoolType), Hi, 6306 Zero, Carry); 6307 6308 Next = DAG.getNode(ISD::ADD, dl, VT, Next, Merge(Lo, Hi)); 6309 6310 if (Opcode == ISD::SMUL_LOHI) { 6311 SDValue NextSub = DAG.getNode(ISD::SUB, dl, VT, Next, 6312 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, RL)); 6313 Next = DAG.getSelectCC(dl, LH, Zero, NextSub, Next, ISD::SETLT); 6314 6315 NextSub = DAG.getNode(ISD::SUB, dl, VT, Next, 6316 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, LL)); 6317 Next = DAG.getSelectCC(dl, RH, Zero, NextSub, Next, ISD::SETLT); 6318 } 6319 6320 Result.push_back(DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, Next)); 6321 Next = DAG.getNode(ISD::SRL, dl, VT, Next, Shift); 6322 Result.push_back(DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, Next)); 6323 return true; 6324 } 6325 6326 bool TargetLowering::expandMUL(SDNode *N, SDValue &Lo, SDValue &Hi, EVT HiLoVT, 6327 SelectionDAG &DAG, MulExpansionKind Kind, 6328 SDValue LL, SDValue LH, SDValue RL, 6329 SDValue RH) const { 6330 SmallVector<SDValue, 2> Result; 6331 bool Ok = expandMUL_LOHI(N->getOpcode(), N->getValueType(0), SDLoc(N), 6332 N->getOperand(0), N->getOperand(1), Result, HiLoVT, 6333 DAG, Kind, LL, LH, RL, RH); 6334 if (Ok) { 6335 assert(Result.size() == 2); 6336 Lo = Result[0]; 6337 Hi = Result[1]; 6338 } 6339 return Ok; 6340 } 6341 6342 // Check that (every element of) Z is undef or not an exact multiple of BW. 6343 static bool isNonZeroModBitWidthOrUndef(SDValue Z, unsigned BW) { 6344 return ISD::matchUnaryPredicate( 6345 Z, 6346 [=](ConstantSDNode *C) { return !C || C->getAPIntValue().urem(BW) != 0; }, 6347 true); 6348 } 6349 6350 bool TargetLowering::expandFunnelShift(SDNode *Node, SDValue &Result, 6351 SelectionDAG &DAG) const { 6352 EVT VT = Node->getValueType(0); 6353 6354 if (VT.isVector() && (!isOperationLegalOrCustom(ISD::SHL, VT) || 6355 !isOperationLegalOrCustom(ISD::SRL, VT) || 6356 !isOperationLegalOrCustom(ISD::SUB, VT) || 6357 !isOperationLegalOrCustomOrPromote(ISD::OR, VT))) 6358 return false; 6359 6360 SDValue X = Node->getOperand(0); 6361 SDValue Y = Node->getOperand(1); 6362 SDValue Z = Node->getOperand(2); 6363 6364 unsigned BW = VT.getScalarSizeInBits(); 6365 bool IsFSHL = Node->getOpcode() == ISD::FSHL; 6366 SDLoc DL(SDValue(Node, 0)); 6367 6368 EVT ShVT = Z.getValueType(); 6369 6370 // If a funnel shift in the other direction is more supported, use it. 6371 unsigned RevOpcode = IsFSHL ? ISD::FSHR : ISD::FSHL; 6372 if (!isOperationLegalOrCustom(Node->getOpcode(), VT) && 6373 isOperationLegalOrCustom(RevOpcode, VT) && isPowerOf2_32(BW)) { 6374 if (isNonZeroModBitWidthOrUndef(Z, BW)) { 6375 // fshl X, Y, Z -> fshr X, Y, -Z 6376 // fshr X, Y, Z -> fshl X, Y, -Z 6377 SDValue Zero = DAG.getConstant(0, DL, ShVT); 6378 Z = DAG.getNode(ISD::SUB, DL, VT, Zero, Z); 6379 } else { 6380 // fshl X, Y, Z -> fshr (srl X, 1), (fshr X, Y, 1), ~Z 6381 // fshr X, Y, Z -> fshl (fshl X, Y, 1), (shl Y, 1), ~Z 6382 SDValue One = DAG.getConstant(1, DL, ShVT); 6383 if (IsFSHL) { 6384 Y = DAG.getNode(RevOpcode, DL, VT, X, Y, One); 6385 X = DAG.getNode(ISD::SRL, DL, VT, X, One); 6386 } else { 6387 X = DAG.getNode(RevOpcode, DL, VT, X, Y, One); 6388 Y = DAG.getNode(ISD::SHL, DL, VT, Y, One); 6389 } 6390 Z = DAG.getNOT(DL, Z, ShVT); 6391 } 6392 Result = DAG.getNode(RevOpcode, DL, VT, X, Y, Z); 6393 return true; 6394 } 6395 6396 SDValue ShX, ShY; 6397 SDValue ShAmt, InvShAmt; 6398 if (isNonZeroModBitWidthOrUndef(Z, BW)) { 6399 // fshl: X << C | Y >> (BW - C) 6400 // fshr: X << (BW - C) | Y >> C 6401 // where C = Z % BW is not zero 6402 SDValue BitWidthC = DAG.getConstant(BW, DL, ShVT); 6403 ShAmt = DAG.getNode(ISD::UREM, DL, ShVT, Z, BitWidthC); 6404 InvShAmt = DAG.getNode(ISD::SUB, DL, ShVT, BitWidthC, ShAmt); 6405 ShX = DAG.getNode(ISD::SHL, DL, VT, X, IsFSHL ? ShAmt : InvShAmt); 6406 ShY = DAG.getNode(ISD::SRL, DL, VT, Y, IsFSHL ? InvShAmt : ShAmt); 6407 } else { 6408 // fshl: X << (Z % BW) | Y >> 1 >> (BW - 1 - (Z % BW)) 6409 // fshr: X << 1 << (BW - 1 - (Z % BW)) | Y >> (Z % BW) 6410 SDValue Mask = DAG.getConstant(BW - 1, DL, ShVT); 6411 if (isPowerOf2_32(BW)) { 6412 // Z % BW -> Z & (BW - 1) 6413 ShAmt = DAG.getNode(ISD::AND, DL, ShVT, Z, Mask); 6414 // (BW - 1) - (Z % BW) -> ~Z & (BW - 1) 6415 InvShAmt = DAG.getNode(ISD::AND, DL, ShVT, DAG.getNOT(DL, Z, ShVT), Mask); 6416 } else { 6417 SDValue BitWidthC = DAG.getConstant(BW, DL, ShVT); 6418 ShAmt = DAG.getNode(ISD::UREM, DL, ShVT, Z, BitWidthC); 6419 InvShAmt = DAG.getNode(ISD::SUB, DL, ShVT, Mask, ShAmt); 6420 } 6421 6422 SDValue One = DAG.getConstant(1, DL, ShVT); 6423 if (IsFSHL) { 6424 ShX = DAG.getNode(ISD::SHL, DL, VT, X, ShAmt); 6425 SDValue ShY1 = DAG.getNode(ISD::SRL, DL, VT, Y, One); 6426 ShY = DAG.getNode(ISD::SRL, DL, VT, ShY1, InvShAmt); 6427 } else { 6428 SDValue ShX1 = DAG.getNode(ISD::SHL, DL, VT, X, One); 6429 ShX = DAG.getNode(ISD::SHL, DL, VT, ShX1, InvShAmt); 6430 ShY = DAG.getNode(ISD::SRL, DL, VT, Y, ShAmt); 6431 } 6432 } 6433 Result = DAG.getNode(ISD::OR, DL, VT, ShX, ShY); 6434 return true; 6435 } 6436 6437 // TODO: Merge with expandFunnelShift. 6438 bool TargetLowering::expandROT(SDNode *Node, bool AllowVectorOps, 6439 SDValue &Result, SelectionDAG &DAG) const { 6440 EVT VT = Node->getValueType(0); 6441 unsigned EltSizeInBits = VT.getScalarSizeInBits(); 6442 bool IsLeft = Node->getOpcode() == ISD::ROTL; 6443 SDValue Op0 = Node->getOperand(0); 6444 SDValue Op1 = Node->getOperand(1); 6445 SDLoc DL(SDValue(Node, 0)); 6446 6447 EVT ShVT = Op1.getValueType(); 6448 SDValue Zero = DAG.getConstant(0, DL, ShVT); 6449 6450 // If a rotate in the other direction is supported, use it. 6451 unsigned RevRot = IsLeft ? ISD::ROTR : ISD::ROTL; 6452 if (isOperationLegalOrCustom(RevRot, VT) && isPowerOf2_32(EltSizeInBits)) { 6453 SDValue Sub = DAG.getNode(ISD::SUB, DL, ShVT, Zero, Op1); 6454 Result = DAG.getNode(RevRot, DL, VT, Op0, Sub); 6455 return true; 6456 } 6457 6458 if (!AllowVectorOps && VT.isVector() && 6459 (!isOperationLegalOrCustom(ISD::SHL, VT) || 6460 !isOperationLegalOrCustom(ISD::SRL, VT) || 6461 !isOperationLegalOrCustom(ISD::SUB, VT) || 6462 !isOperationLegalOrCustomOrPromote(ISD::OR, VT) || 6463 !isOperationLegalOrCustomOrPromote(ISD::AND, VT))) 6464 return false; 6465 6466 unsigned ShOpc = IsLeft ? ISD::SHL : ISD::SRL; 6467 unsigned HsOpc = IsLeft ? ISD::SRL : ISD::SHL; 6468 SDValue BitWidthMinusOneC = DAG.getConstant(EltSizeInBits - 1, DL, ShVT); 6469 SDValue ShVal; 6470 SDValue HsVal; 6471 if (isPowerOf2_32(EltSizeInBits)) { 6472 // (rotl x, c) -> x << (c & (w - 1)) | x >> (-c & (w - 1)) 6473 // (rotr x, c) -> x >> (c & (w - 1)) | x << (-c & (w - 1)) 6474 SDValue NegOp1 = DAG.getNode(ISD::SUB, DL, ShVT, Zero, Op1); 6475 SDValue ShAmt = DAG.getNode(ISD::AND, DL, ShVT, Op1, BitWidthMinusOneC); 6476 ShVal = DAG.getNode(ShOpc, DL, VT, Op0, ShAmt); 6477 SDValue HsAmt = DAG.getNode(ISD::AND, DL, ShVT, NegOp1, BitWidthMinusOneC); 6478 HsVal = DAG.getNode(HsOpc, DL, VT, Op0, HsAmt); 6479 } else { 6480 // (rotl x, c) -> x << (c % w) | x >> 1 >> (w - 1 - (c % w)) 6481 // (rotr x, c) -> x >> (c % w) | x << 1 << (w - 1 - (c % w)) 6482 SDValue BitWidthC = DAG.getConstant(EltSizeInBits, DL, ShVT); 6483 SDValue ShAmt = DAG.getNode(ISD::UREM, DL, ShVT, Op1, BitWidthC); 6484 ShVal = DAG.getNode(ShOpc, DL, VT, Op0, ShAmt); 6485 SDValue HsAmt = DAG.getNode(ISD::SUB, DL, ShVT, BitWidthMinusOneC, ShAmt); 6486 SDValue One = DAG.getConstant(1, DL, ShVT); 6487 HsVal = 6488 DAG.getNode(HsOpc, DL, VT, DAG.getNode(HsOpc, DL, VT, Op0, One), HsAmt); 6489 } 6490 Result = DAG.getNode(ISD::OR, DL, VT, ShVal, HsVal); 6491 return true; 6492 } 6493 6494 bool TargetLowering::expandFP_TO_SINT(SDNode *Node, SDValue &Result, 6495 SelectionDAG &DAG) const { 6496 unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0; 6497 SDValue Src = Node->getOperand(OpNo); 6498 EVT SrcVT = Src.getValueType(); 6499 EVT DstVT = Node->getValueType(0); 6500 SDLoc dl(SDValue(Node, 0)); 6501 6502 // FIXME: Only f32 to i64 conversions are supported. 6503 if (SrcVT != MVT::f32 || DstVT != MVT::i64) 6504 return false; 6505 6506 if (Node->isStrictFPOpcode()) 6507 // When a NaN is converted to an integer a trap is allowed. We can't 6508 // use this expansion here because it would eliminate that trap. Other 6509 // traps are also allowed and cannot be eliminated. See 6510 // IEEE 754-2008 sec 5.8. 6511 return false; 6512 6513 // Expand f32 -> i64 conversion 6514 // This algorithm comes from compiler-rt's implementation of fixsfdi: 6515 // https://github.com/llvm/llvm-project/blob/master/compiler-rt/lib/builtins/fixsfdi.c 6516 unsigned SrcEltBits = SrcVT.getScalarSizeInBits(); 6517 EVT IntVT = SrcVT.changeTypeToInteger(); 6518 EVT IntShVT = getShiftAmountTy(IntVT, DAG.getDataLayout()); 6519 6520 SDValue ExponentMask = DAG.getConstant(0x7F800000, dl, IntVT); 6521 SDValue ExponentLoBit = DAG.getConstant(23, dl, IntVT); 6522 SDValue Bias = DAG.getConstant(127, dl, IntVT); 6523 SDValue SignMask = DAG.getConstant(APInt::getSignMask(SrcEltBits), dl, IntVT); 6524 SDValue SignLowBit = DAG.getConstant(SrcEltBits - 1, dl, IntVT); 6525 SDValue MantissaMask = DAG.getConstant(0x007FFFFF, dl, IntVT); 6526 6527 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, IntVT, Src); 6528 6529 SDValue ExponentBits = DAG.getNode( 6530 ISD::SRL, dl, IntVT, DAG.getNode(ISD::AND, dl, IntVT, Bits, ExponentMask), 6531 DAG.getZExtOrTrunc(ExponentLoBit, dl, IntShVT)); 6532 SDValue Exponent = DAG.getNode(ISD::SUB, dl, IntVT, ExponentBits, Bias); 6533 6534 SDValue Sign = DAG.getNode(ISD::SRA, dl, IntVT, 6535 DAG.getNode(ISD::AND, dl, IntVT, Bits, SignMask), 6536 DAG.getZExtOrTrunc(SignLowBit, dl, IntShVT)); 6537 Sign = DAG.getSExtOrTrunc(Sign, dl, DstVT); 6538 6539 SDValue R = DAG.getNode(ISD::OR, dl, IntVT, 6540 DAG.getNode(ISD::AND, dl, IntVT, Bits, MantissaMask), 6541 DAG.getConstant(0x00800000, dl, IntVT)); 6542 6543 R = DAG.getZExtOrTrunc(R, dl, DstVT); 6544 6545 R = DAG.getSelectCC( 6546 dl, Exponent, ExponentLoBit, 6547 DAG.getNode(ISD::SHL, dl, DstVT, R, 6548 DAG.getZExtOrTrunc( 6549 DAG.getNode(ISD::SUB, dl, IntVT, Exponent, ExponentLoBit), 6550 dl, IntShVT)), 6551 DAG.getNode(ISD::SRL, dl, DstVT, R, 6552 DAG.getZExtOrTrunc( 6553 DAG.getNode(ISD::SUB, dl, IntVT, ExponentLoBit, Exponent), 6554 dl, IntShVT)), 6555 ISD::SETGT); 6556 6557 SDValue Ret = DAG.getNode(ISD::SUB, dl, DstVT, 6558 DAG.getNode(ISD::XOR, dl, DstVT, R, Sign), Sign); 6559 6560 Result = DAG.getSelectCC(dl, Exponent, DAG.getConstant(0, dl, IntVT), 6561 DAG.getConstant(0, dl, DstVT), Ret, ISD::SETLT); 6562 return true; 6563 } 6564 6565 bool TargetLowering::expandFP_TO_UINT(SDNode *Node, SDValue &Result, 6566 SDValue &Chain, 6567 SelectionDAG &DAG) const { 6568 SDLoc dl(SDValue(Node, 0)); 6569 unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0; 6570 SDValue Src = Node->getOperand(OpNo); 6571 6572 EVT SrcVT = Src.getValueType(); 6573 EVT DstVT = Node->getValueType(0); 6574 EVT SetCCVT = 6575 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), SrcVT); 6576 EVT DstSetCCVT = 6577 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), DstVT); 6578 6579 // Only expand vector types if we have the appropriate vector bit operations. 6580 unsigned SIntOpcode = Node->isStrictFPOpcode() ? ISD::STRICT_FP_TO_SINT : 6581 ISD::FP_TO_SINT; 6582 if (DstVT.isVector() && (!isOperationLegalOrCustom(SIntOpcode, DstVT) || 6583 !isOperationLegalOrCustomOrPromote(ISD::XOR, SrcVT))) 6584 return false; 6585 6586 // If the maximum float value is smaller then the signed integer range, 6587 // the destination signmask can't be represented by the float, so we can 6588 // just use FP_TO_SINT directly. 6589 const fltSemantics &APFSem = DAG.EVTToAPFloatSemantics(SrcVT); 6590 APFloat APF(APFSem, APInt::getNullValue(SrcVT.getScalarSizeInBits())); 6591 APInt SignMask = APInt::getSignMask(DstVT.getScalarSizeInBits()); 6592 if (APFloat::opOverflow & 6593 APF.convertFromAPInt(SignMask, false, APFloat::rmNearestTiesToEven)) { 6594 if (Node->isStrictFPOpcode()) { 6595 Result = DAG.getNode(ISD::STRICT_FP_TO_SINT, dl, { DstVT, MVT::Other }, 6596 { Node->getOperand(0), Src }); 6597 Chain = Result.getValue(1); 6598 } else 6599 Result = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, Src); 6600 return true; 6601 } 6602 6603 // Don't expand it if there isn't cheap fsub instruction. 6604 if (!isOperationLegalOrCustom( 6605 Node->isStrictFPOpcode() ? ISD::STRICT_FSUB : ISD::FSUB, SrcVT)) 6606 return false; 6607 6608 SDValue Cst = DAG.getConstantFP(APF, dl, SrcVT); 6609 SDValue Sel; 6610 6611 if (Node->isStrictFPOpcode()) { 6612 Sel = DAG.getSetCC(dl, SetCCVT, Src, Cst, ISD::SETLT, 6613 Node->getOperand(0), /*IsSignaling*/ true); 6614 Chain = Sel.getValue(1); 6615 } else { 6616 Sel = DAG.getSetCC(dl, SetCCVT, Src, Cst, ISD::SETLT); 6617 } 6618 6619 bool Strict = Node->isStrictFPOpcode() || 6620 shouldUseStrictFP_TO_INT(SrcVT, DstVT, /*IsSigned*/ false); 6621 6622 if (Strict) { 6623 // Expand based on maximum range of FP_TO_SINT, if the value exceeds the 6624 // signmask then offset (the result of which should be fully representable). 6625 // Sel = Src < 0x8000000000000000 6626 // FltOfs = select Sel, 0, 0x8000000000000000 6627 // IntOfs = select Sel, 0, 0x8000000000000000 6628 // Result = fp_to_sint(Src - FltOfs) ^ IntOfs 6629 6630 // TODO: Should any fast-math-flags be set for the FSUB? 6631 SDValue FltOfs = DAG.getSelect(dl, SrcVT, Sel, 6632 DAG.getConstantFP(0.0, dl, SrcVT), Cst); 6633 Sel = DAG.getBoolExtOrTrunc(Sel, dl, DstSetCCVT, DstVT); 6634 SDValue IntOfs = DAG.getSelect(dl, DstVT, Sel, 6635 DAG.getConstant(0, dl, DstVT), 6636 DAG.getConstant(SignMask, dl, DstVT)); 6637 SDValue SInt; 6638 if (Node->isStrictFPOpcode()) { 6639 SDValue Val = DAG.getNode(ISD::STRICT_FSUB, dl, { SrcVT, MVT::Other }, 6640 { Chain, Src, FltOfs }); 6641 SInt = DAG.getNode(ISD::STRICT_FP_TO_SINT, dl, { DstVT, MVT::Other }, 6642 { Val.getValue(1), Val }); 6643 Chain = SInt.getValue(1); 6644 } else { 6645 SDValue Val = DAG.getNode(ISD::FSUB, dl, SrcVT, Src, FltOfs); 6646 SInt = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, Val); 6647 } 6648 Result = DAG.getNode(ISD::XOR, dl, DstVT, SInt, IntOfs); 6649 } else { 6650 // Expand based on maximum range of FP_TO_SINT: 6651 // True = fp_to_sint(Src) 6652 // False = 0x8000000000000000 + fp_to_sint(Src - 0x8000000000000000) 6653 // Result = select (Src < 0x8000000000000000), True, False 6654 6655 SDValue True = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, Src); 6656 // TODO: Should any fast-math-flags be set for the FSUB? 6657 SDValue False = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, 6658 DAG.getNode(ISD::FSUB, dl, SrcVT, Src, Cst)); 6659 False = DAG.getNode(ISD::XOR, dl, DstVT, False, 6660 DAG.getConstant(SignMask, dl, DstVT)); 6661 Sel = DAG.getBoolExtOrTrunc(Sel, dl, DstSetCCVT, DstVT); 6662 Result = DAG.getSelect(dl, DstVT, Sel, True, False); 6663 } 6664 return true; 6665 } 6666 6667 bool TargetLowering::expandUINT_TO_FP(SDNode *Node, SDValue &Result, 6668 SDValue &Chain, 6669 SelectionDAG &DAG) const { 6670 // This transform is not correct for converting 0 when rounding mode is set 6671 // to round toward negative infinity which will produce -0.0. So disable under 6672 // strictfp. 6673 if (Node->isStrictFPOpcode()) 6674 return false; 6675 6676 SDValue Src = Node->getOperand(0); 6677 EVT SrcVT = Src.getValueType(); 6678 EVT DstVT = Node->getValueType(0); 6679 6680 if (SrcVT.getScalarType() != MVT::i64 || DstVT.getScalarType() != MVT::f64) 6681 return false; 6682 6683 // Only expand vector types if we have the appropriate vector bit operations. 6684 if (SrcVT.isVector() && (!isOperationLegalOrCustom(ISD::SRL, SrcVT) || 6685 !isOperationLegalOrCustom(ISD::FADD, DstVT) || 6686 !isOperationLegalOrCustom(ISD::FSUB, DstVT) || 6687 !isOperationLegalOrCustomOrPromote(ISD::OR, SrcVT) || 6688 !isOperationLegalOrCustomOrPromote(ISD::AND, SrcVT))) 6689 return false; 6690 6691 SDLoc dl(SDValue(Node, 0)); 6692 EVT ShiftVT = getShiftAmountTy(SrcVT, DAG.getDataLayout()); 6693 6694 // Implementation of unsigned i64 to f64 following the algorithm in 6695 // __floatundidf in compiler_rt. This implementation performs rounding 6696 // correctly in all rounding modes with the exception of converting 0 6697 // when rounding toward negative infinity. In that case the fsub will produce 6698 // -0.0. This will be added to +0.0 and produce -0.0 which is incorrect. 6699 SDValue TwoP52 = DAG.getConstant(UINT64_C(0x4330000000000000), dl, SrcVT); 6700 SDValue TwoP84PlusTwoP52 = DAG.getConstantFP( 6701 BitsToDouble(UINT64_C(0x4530000000100000)), dl, DstVT); 6702 SDValue TwoP84 = DAG.getConstant(UINT64_C(0x4530000000000000), dl, SrcVT); 6703 SDValue LoMask = DAG.getConstant(UINT64_C(0x00000000FFFFFFFF), dl, SrcVT); 6704 SDValue HiShift = DAG.getConstant(32, dl, ShiftVT); 6705 6706 SDValue Lo = DAG.getNode(ISD::AND, dl, SrcVT, Src, LoMask); 6707 SDValue Hi = DAG.getNode(ISD::SRL, dl, SrcVT, Src, HiShift); 6708 SDValue LoOr = DAG.getNode(ISD::OR, dl, SrcVT, Lo, TwoP52); 6709 SDValue HiOr = DAG.getNode(ISD::OR, dl, SrcVT, Hi, TwoP84); 6710 SDValue LoFlt = DAG.getBitcast(DstVT, LoOr); 6711 SDValue HiFlt = DAG.getBitcast(DstVT, HiOr); 6712 SDValue HiSub = 6713 DAG.getNode(ISD::FSUB, dl, DstVT, HiFlt, TwoP84PlusTwoP52); 6714 Result = DAG.getNode(ISD::FADD, dl, DstVT, LoFlt, HiSub); 6715 return true; 6716 } 6717 6718 SDValue TargetLowering::expandFMINNUM_FMAXNUM(SDNode *Node, 6719 SelectionDAG &DAG) const { 6720 SDLoc dl(Node); 6721 unsigned NewOp = Node->getOpcode() == ISD::FMINNUM ? 6722 ISD::FMINNUM_IEEE : ISD::FMAXNUM_IEEE; 6723 EVT VT = Node->getValueType(0); 6724 6725 if (VT.isScalableVector()) 6726 report_fatal_error( 6727 "Expanding fminnum/fmaxnum for scalable vectors is undefined."); 6728 6729 if (isOperationLegalOrCustom(NewOp, VT)) { 6730 SDValue Quiet0 = Node->getOperand(0); 6731 SDValue Quiet1 = Node->getOperand(1); 6732 6733 if (!Node->getFlags().hasNoNaNs()) { 6734 // Insert canonicalizes if it's possible we need to quiet to get correct 6735 // sNaN behavior. 6736 if (!DAG.isKnownNeverSNaN(Quiet0)) { 6737 Quiet0 = DAG.getNode(ISD::FCANONICALIZE, dl, VT, Quiet0, 6738 Node->getFlags()); 6739 } 6740 if (!DAG.isKnownNeverSNaN(Quiet1)) { 6741 Quiet1 = DAG.getNode(ISD::FCANONICALIZE, dl, VT, Quiet1, 6742 Node->getFlags()); 6743 } 6744 } 6745 6746 return DAG.getNode(NewOp, dl, VT, Quiet0, Quiet1, Node->getFlags()); 6747 } 6748 6749 // If the target has FMINIMUM/FMAXIMUM but not FMINNUM/FMAXNUM use that 6750 // instead if there are no NaNs. 6751 if (Node->getFlags().hasNoNaNs()) { 6752 unsigned IEEE2018Op = 6753 Node->getOpcode() == ISD::FMINNUM ? ISD::FMINIMUM : ISD::FMAXIMUM; 6754 if (isOperationLegalOrCustom(IEEE2018Op, VT)) { 6755 return DAG.getNode(IEEE2018Op, dl, VT, Node->getOperand(0), 6756 Node->getOperand(1), Node->getFlags()); 6757 } 6758 } 6759 6760 // If none of the above worked, but there are no NaNs, then expand to 6761 // a compare/select sequence. This is required for correctness since 6762 // InstCombine might have canonicalized a fcmp+select sequence to a 6763 // FMINNUM/FMAXNUM node. If we were to fall through to the default 6764 // expansion to libcall, we might introduce a link-time dependency 6765 // on libm into a file that originally did not have one. 6766 if (Node->getFlags().hasNoNaNs()) { 6767 ISD::CondCode Pred = 6768 Node->getOpcode() == ISD::FMINNUM ? ISD::SETLT : ISD::SETGT; 6769 SDValue Op1 = Node->getOperand(0); 6770 SDValue Op2 = Node->getOperand(1); 6771 SDValue SelCC = DAG.getSelectCC(dl, Op1, Op2, Op1, Op2, Pred); 6772 // Copy FMF flags, but always set the no-signed-zeros flag 6773 // as this is implied by the FMINNUM/FMAXNUM semantics. 6774 SDNodeFlags Flags = Node->getFlags(); 6775 Flags.setNoSignedZeros(true); 6776 SelCC->setFlags(Flags); 6777 return SelCC; 6778 } 6779 6780 return SDValue(); 6781 } 6782 6783 bool TargetLowering::expandCTPOP(SDNode *Node, SDValue &Result, 6784 SelectionDAG &DAG) const { 6785 SDLoc dl(Node); 6786 EVT VT = Node->getValueType(0); 6787 EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout()); 6788 SDValue Op = Node->getOperand(0); 6789 unsigned Len = VT.getScalarSizeInBits(); 6790 assert(VT.isInteger() && "CTPOP not implemented for this type."); 6791 6792 // TODO: Add support for irregular type lengths. 6793 if (!(Len <= 128 && Len % 8 == 0)) 6794 return false; 6795 6796 // Only expand vector types if we have the appropriate vector bit operations. 6797 if (VT.isVector() && (!isOperationLegalOrCustom(ISD::ADD, VT) || 6798 !isOperationLegalOrCustom(ISD::SUB, VT) || 6799 !isOperationLegalOrCustom(ISD::SRL, VT) || 6800 (Len != 8 && !isOperationLegalOrCustom(ISD::MUL, VT)) || 6801 !isOperationLegalOrCustomOrPromote(ISD::AND, VT))) 6802 return false; 6803 6804 // This is the "best" algorithm from 6805 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel 6806 SDValue Mask55 = 6807 DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x55)), dl, VT); 6808 SDValue Mask33 = 6809 DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x33)), dl, VT); 6810 SDValue Mask0F = 6811 DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x0F)), dl, VT); 6812 SDValue Mask01 = 6813 DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x01)), dl, VT); 6814 6815 // v = v - ((v >> 1) & 0x55555555...) 6816 Op = DAG.getNode(ISD::SUB, dl, VT, Op, 6817 DAG.getNode(ISD::AND, dl, VT, 6818 DAG.getNode(ISD::SRL, dl, VT, Op, 6819 DAG.getConstant(1, dl, ShVT)), 6820 Mask55)); 6821 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...) 6822 Op = DAG.getNode(ISD::ADD, dl, VT, DAG.getNode(ISD::AND, dl, VT, Op, Mask33), 6823 DAG.getNode(ISD::AND, dl, VT, 6824 DAG.getNode(ISD::SRL, dl, VT, Op, 6825 DAG.getConstant(2, dl, ShVT)), 6826 Mask33)); 6827 // v = (v + (v >> 4)) & 0x0F0F0F0F... 6828 Op = DAG.getNode(ISD::AND, dl, VT, 6829 DAG.getNode(ISD::ADD, dl, VT, Op, 6830 DAG.getNode(ISD::SRL, dl, VT, Op, 6831 DAG.getConstant(4, dl, ShVT))), 6832 Mask0F); 6833 // v = (v * 0x01010101...) >> (Len - 8) 6834 if (Len > 8) 6835 Op = 6836 DAG.getNode(ISD::SRL, dl, VT, DAG.getNode(ISD::MUL, dl, VT, Op, Mask01), 6837 DAG.getConstant(Len - 8, dl, ShVT)); 6838 6839 Result = Op; 6840 return true; 6841 } 6842 6843 bool TargetLowering::expandCTLZ(SDNode *Node, SDValue &Result, 6844 SelectionDAG &DAG) const { 6845 SDLoc dl(Node); 6846 EVT VT = Node->getValueType(0); 6847 EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout()); 6848 SDValue Op = Node->getOperand(0); 6849 unsigned NumBitsPerElt = VT.getScalarSizeInBits(); 6850 6851 // If the non-ZERO_UNDEF version is supported we can use that instead. 6852 if (Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF && 6853 isOperationLegalOrCustom(ISD::CTLZ, VT)) { 6854 Result = DAG.getNode(ISD::CTLZ, dl, VT, Op); 6855 return true; 6856 } 6857 6858 // If the ZERO_UNDEF version is supported use that and handle the zero case. 6859 if (isOperationLegalOrCustom(ISD::CTLZ_ZERO_UNDEF, VT)) { 6860 EVT SetCCVT = 6861 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 6862 SDValue CTLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, VT, Op); 6863 SDValue Zero = DAG.getConstant(0, dl, VT); 6864 SDValue SrcIsZero = DAG.getSetCC(dl, SetCCVT, Op, Zero, ISD::SETEQ); 6865 Result = DAG.getNode(ISD::SELECT, dl, VT, SrcIsZero, 6866 DAG.getConstant(NumBitsPerElt, dl, VT), CTLZ); 6867 return true; 6868 } 6869 6870 // Only expand vector types if we have the appropriate vector bit operations. 6871 if (VT.isVector() && (!isPowerOf2_32(NumBitsPerElt) || 6872 !isOperationLegalOrCustom(ISD::CTPOP, VT) || 6873 !isOperationLegalOrCustom(ISD::SRL, VT) || 6874 !isOperationLegalOrCustomOrPromote(ISD::OR, VT))) 6875 return false; 6876 6877 // for now, we do this: 6878 // x = x | (x >> 1); 6879 // x = x | (x >> 2); 6880 // ... 6881 // x = x | (x >>16); 6882 // x = x | (x >>32); // for 64-bit input 6883 // return popcount(~x); 6884 // 6885 // Ref: "Hacker's Delight" by Henry Warren 6886 for (unsigned i = 0; (1U << i) <= (NumBitsPerElt / 2); ++i) { 6887 SDValue Tmp = DAG.getConstant(1ULL << i, dl, ShVT); 6888 Op = DAG.getNode(ISD::OR, dl, VT, Op, 6889 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp)); 6890 } 6891 Op = DAG.getNOT(dl, Op, VT); 6892 Result = DAG.getNode(ISD::CTPOP, dl, VT, Op); 6893 return true; 6894 } 6895 6896 bool TargetLowering::expandCTTZ(SDNode *Node, SDValue &Result, 6897 SelectionDAG &DAG) const { 6898 SDLoc dl(Node); 6899 EVT VT = Node->getValueType(0); 6900 SDValue Op = Node->getOperand(0); 6901 unsigned NumBitsPerElt = VT.getScalarSizeInBits(); 6902 6903 // If the non-ZERO_UNDEF version is supported we can use that instead. 6904 if (Node->getOpcode() == ISD::CTTZ_ZERO_UNDEF && 6905 isOperationLegalOrCustom(ISD::CTTZ, VT)) { 6906 Result = DAG.getNode(ISD::CTTZ, dl, VT, Op); 6907 return true; 6908 } 6909 6910 // If the ZERO_UNDEF version is supported use that and handle the zero case. 6911 if (isOperationLegalOrCustom(ISD::CTTZ_ZERO_UNDEF, VT)) { 6912 EVT SetCCVT = 6913 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 6914 SDValue CTTZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, VT, Op); 6915 SDValue Zero = DAG.getConstant(0, dl, VT); 6916 SDValue SrcIsZero = DAG.getSetCC(dl, SetCCVT, Op, Zero, ISD::SETEQ); 6917 Result = DAG.getNode(ISD::SELECT, dl, VT, SrcIsZero, 6918 DAG.getConstant(NumBitsPerElt, dl, VT), CTTZ); 6919 return true; 6920 } 6921 6922 // Only expand vector types if we have the appropriate vector bit operations. 6923 if (VT.isVector() && (!isPowerOf2_32(NumBitsPerElt) || 6924 (!isOperationLegalOrCustom(ISD::CTPOP, VT) && 6925 !isOperationLegalOrCustom(ISD::CTLZ, VT)) || 6926 !isOperationLegalOrCustom(ISD::SUB, VT) || 6927 !isOperationLegalOrCustomOrPromote(ISD::AND, VT) || 6928 !isOperationLegalOrCustomOrPromote(ISD::XOR, VT))) 6929 return false; 6930 6931 // for now, we use: { return popcount(~x & (x - 1)); } 6932 // unless the target has ctlz but not ctpop, in which case we use: 6933 // { return 32 - nlz(~x & (x-1)); } 6934 // Ref: "Hacker's Delight" by Henry Warren 6935 SDValue Tmp = DAG.getNode( 6936 ISD::AND, dl, VT, DAG.getNOT(dl, Op, VT), 6937 DAG.getNode(ISD::SUB, dl, VT, Op, DAG.getConstant(1, dl, VT))); 6938 6939 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. 6940 if (isOperationLegal(ISD::CTLZ, VT) && !isOperationLegal(ISD::CTPOP, VT)) { 6941 Result = 6942 DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(NumBitsPerElt, dl, VT), 6943 DAG.getNode(ISD::CTLZ, dl, VT, Tmp)); 6944 return true; 6945 } 6946 6947 Result = DAG.getNode(ISD::CTPOP, dl, VT, Tmp); 6948 return true; 6949 } 6950 6951 bool TargetLowering::expandABS(SDNode *N, SDValue &Result, 6952 SelectionDAG &DAG, bool IsNegative) const { 6953 SDLoc dl(N); 6954 EVT VT = N->getValueType(0); 6955 EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout()); 6956 SDValue Op = N->getOperand(0); 6957 6958 // abs(x) -> smax(x,sub(0,x)) 6959 if (!IsNegative && isOperationLegal(ISD::SUB, VT) && 6960 isOperationLegal(ISD::SMAX, VT)) { 6961 SDValue Zero = DAG.getConstant(0, dl, VT); 6962 Result = DAG.getNode(ISD::SMAX, dl, VT, Op, 6963 DAG.getNode(ISD::SUB, dl, VT, Zero, Op)); 6964 return true; 6965 } 6966 6967 // abs(x) -> umin(x,sub(0,x)) 6968 if (!IsNegative && isOperationLegal(ISD::SUB, VT) && 6969 isOperationLegal(ISD::UMIN, VT)) { 6970 SDValue Zero = DAG.getConstant(0, dl, VT); 6971 Result = DAG.getNode(ISD::UMIN, dl, VT, Op, 6972 DAG.getNode(ISD::SUB, dl, VT, Zero, Op)); 6973 return true; 6974 } 6975 6976 // 0 - abs(x) -> smin(x, sub(0,x)) 6977 if (IsNegative && isOperationLegal(ISD::SUB, VT) && 6978 isOperationLegal(ISD::SMIN, VT)) { 6979 SDValue Zero = DAG.getConstant(0, dl, VT); 6980 Result = DAG.getNode(ISD::SMIN, dl, VT, Op, 6981 DAG.getNode(ISD::SUB, dl, VT, Zero, Op)); 6982 return true; 6983 } 6984 6985 // Only expand vector types if we have the appropriate vector operations. 6986 if (VT.isVector() && 6987 (!isOperationLegalOrCustom(ISD::SRA, VT) || 6988 (!IsNegative && !isOperationLegalOrCustom(ISD::ADD, VT)) || 6989 (IsNegative && !isOperationLegalOrCustom(ISD::SUB, VT)) || 6990 !isOperationLegalOrCustomOrPromote(ISD::XOR, VT))) 6991 return false; 6992 6993 SDValue Shift = 6994 DAG.getNode(ISD::SRA, dl, VT, Op, 6995 DAG.getConstant(VT.getScalarSizeInBits() - 1, dl, ShVT)); 6996 if (!IsNegative) { 6997 SDValue Add = DAG.getNode(ISD::ADD, dl, VT, Op, Shift); 6998 Result = DAG.getNode(ISD::XOR, dl, VT, Add, Shift); 6999 } else { 7000 // 0 - abs(x) -> Y = sra (X, size(X)-1); sub (Y, xor (X, Y)) 7001 SDValue Xor = DAG.getNode(ISD::XOR, dl, VT, Op, Shift); 7002 Result = DAG.getNode(ISD::SUB, dl, VT, Shift, Xor); 7003 } 7004 return true; 7005 } 7006 7007 std::pair<SDValue, SDValue> 7008 TargetLowering::scalarizeVectorLoad(LoadSDNode *LD, 7009 SelectionDAG &DAG) const { 7010 SDLoc SL(LD); 7011 SDValue Chain = LD->getChain(); 7012 SDValue BasePTR = LD->getBasePtr(); 7013 EVT SrcVT = LD->getMemoryVT(); 7014 EVT DstVT = LD->getValueType(0); 7015 ISD::LoadExtType ExtType = LD->getExtensionType(); 7016 7017 if (SrcVT.isScalableVector()) 7018 report_fatal_error("Cannot scalarize scalable vector loads"); 7019 7020 unsigned NumElem = SrcVT.getVectorNumElements(); 7021 7022 EVT SrcEltVT = SrcVT.getScalarType(); 7023 EVT DstEltVT = DstVT.getScalarType(); 7024 7025 // A vector must always be stored in memory as-is, i.e. without any padding 7026 // between the elements, since various code depend on it, e.g. in the 7027 // handling of a bitcast of a vector type to int, which may be done with a 7028 // vector store followed by an integer load. A vector that does not have 7029 // elements that are byte-sized must therefore be stored as an integer 7030 // built out of the extracted vector elements. 7031 if (!SrcEltVT.isByteSized()) { 7032 unsigned NumLoadBits = SrcVT.getStoreSizeInBits(); 7033 EVT LoadVT = EVT::getIntegerVT(*DAG.getContext(), NumLoadBits); 7034 7035 unsigned NumSrcBits = SrcVT.getSizeInBits(); 7036 EVT SrcIntVT = EVT::getIntegerVT(*DAG.getContext(), NumSrcBits); 7037 7038 unsigned SrcEltBits = SrcEltVT.getSizeInBits(); 7039 SDValue SrcEltBitMask = DAG.getConstant( 7040 APInt::getLowBitsSet(NumLoadBits, SrcEltBits), SL, LoadVT); 7041 7042 // Load the whole vector and avoid masking off the top bits as it makes 7043 // the codegen worse. 7044 SDValue Load = 7045 DAG.getExtLoad(ISD::EXTLOAD, SL, LoadVT, Chain, BasePTR, 7046 LD->getPointerInfo(), SrcIntVT, LD->getOriginalAlign(), 7047 LD->getMemOperand()->getFlags(), LD->getAAInfo()); 7048 7049 SmallVector<SDValue, 8> Vals; 7050 for (unsigned Idx = 0; Idx < NumElem; ++Idx) { 7051 unsigned ShiftIntoIdx = 7052 (DAG.getDataLayout().isBigEndian() ? (NumElem - 1) - Idx : Idx); 7053 SDValue ShiftAmount = 7054 DAG.getShiftAmountConstant(ShiftIntoIdx * SrcEltVT.getSizeInBits(), 7055 LoadVT, SL, /*LegalTypes=*/false); 7056 SDValue ShiftedElt = DAG.getNode(ISD::SRL, SL, LoadVT, Load, ShiftAmount); 7057 SDValue Elt = 7058 DAG.getNode(ISD::AND, SL, LoadVT, ShiftedElt, SrcEltBitMask); 7059 SDValue Scalar = DAG.getNode(ISD::TRUNCATE, SL, SrcEltVT, Elt); 7060 7061 if (ExtType != ISD::NON_EXTLOAD) { 7062 unsigned ExtendOp = ISD::getExtForLoadExtType(false, ExtType); 7063 Scalar = DAG.getNode(ExtendOp, SL, DstEltVT, Scalar); 7064 } 7065 7066 Vals.push_back(Scalar); 7067 } 7068 7069 SDValue Value = DAG.getBuildVector(DstVT, SL, Vals); 7070 return std::make_pair(Value, Load.getValue(1)); 7071 } 7072 7073 unsigned Stride = SrcEltVT.getSizeInBits() / 8; 7074 assert(SrcEltVT.isByteSized()); 7075 7076 SmallVector<SDValue, 8> Vals; 7077 SmallVector<SDValue, 8> LoadChains; 7078 7079 for (unsigned Idx = 0; Idx < NumElem; ++Idx) { 7080 SDValue ScalarLoad = 7081 DAG.getExtLoad(ExtType, SL, DstEltVT, Chain, BasePTR, 7082 LD->getPointerInfo().getWithOffset(Idx * Stride), 7083 SrcEltVT, LD->getOriginalAlign(), 7084 LD->getMemOperand()->getFlags(), LD->getAAInfo()); 7085 7086 BasePTR = DAG.getObjectPtrOffset(SL, BasePTR, TypeSize::Fixed(Stride)); 7087 7088 Vals.push_back(ScalarLoad.getValue(0)); 7089 LoadChains.push_back(ScalarLoad.getValue(1)); 7090 } 7091 7092 SDValue NewChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoadChains); 7093 SDValue Value = DAG.getBuildVector(DstVT, SL, Vals); 7094 7095 return std::make_pair(Value, NewChain); 7096 } 7097 7098 SDValue TargetLowering::scalarizeVectorStore(StoreSDNode *ST, 7099 SelectionDAG &DAG) const { 7100 SDLoc SL(ST); 7101 7102 SDValue Chain = ST->getChain(); 7103 SDValue BasePtr = ST->getBasePtr(); 7104 SDValue Value = ST->getValue(); 7105 EVT StVT = ST->getMemoryVT(); 7106 7107 if (StVT.isScalableVector()) 7108 report_fatal_error("Cannot scalarize scalable vector stores"); 7109 7110 // The type of the data we want to save 7111 EVT RegVT = Value.getValueType(); 7112 EVT RegSclVT = RegVT.getScalarType(); 7113 7114 // The type of data as saved in memory. 7115 EVT MemSclVT = StVT.getScalarType(); 7116 7117 unsigned NumElem = StVT.getVectorNumElements(); 7118 7119 // A vector must always be stored in memory as-is, i.e. without any padding 7120 // between the elements, since various code depend on it, e.g. in the 7121 // handling of a bitcast of a vector type to int, which may be done with a 7122 // vector store followed by an integer load. A vector that does not have 7123 // elements that are byte-sized must therefore be stored as an integer 7124 // built out of the extracted vector elements. 7125 if (!MemSclVT.isByteSized()) { 7126 unsigned NumBits = StVT.getSizeInBits(); 7127 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), NumBits); 7128 7129 SDValue CurrVal = DAG.getConstant(0, SL, IntVT); 7130 7131 for (unsigned Idx = 0; Idx < NumElem; ++Idx) { 7132 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, RegSclVT, Value, 7133 DAG.getVectorIdxConstant(Idx, SL)); 7134 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, MemSclVT, Elt); 7135 SDValue ExtElt = DAG.getNode(ISD::ZERO_EXTEND, SL, IntVT, Trunc); 7136 unsigned ShiftIntoIdx = 7137 (DAG.getDataLayout().isBigEndian() ? (NumElem - 1) - Idx : Idx); 7138 SDValue ShiftAmount = 7139 DAG.getConstant(ShiftIntoIdx * MemSclVT.getSizeInBits(), SL, IntVT); 7140 SDValue ShiftedElt = 7141 DAG.getNode(ISD::SHL, SL, IntVT, ExtElt, ShiftAmount); 7142 CurrVal = DAG.getNode(ISD::OR, SL, IntVT, CurrVal, ShiftedElt); 7143 } 7144 7145 return DAG.getStore(Chain, SL, CurrVal, BasePtr, ST->getPointerInfo(), 7146 ST->getOriginalAlign(), ST->getMemOperand()->getFlags(), 7147 ST->getAAInfo()); 7148 } 7149 7150 // Store Stride in bytes 7151 unsigned Stride = MemSclVT.getSizeInBits() / 8; 7152 assert(Stride && "Zero stride!"); 7153 // Extract each of the elements from the original vector and save them into 7154 // memory individually. 7155 SmallVector<SDValue, 8> Stores; 7156 for (unsigned Idx = 0; Idx < NumElem; ++Idx) { 7157 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, RegSclVT, Value, 7158 DAG.getVectorIdxConstant(Idx, SL)); 7159 7160 SDValue Ptr = 7161 DAG.getObjectPtrOffset(SL, BasePtr, TypeSize::Fixed(Idx * Stride)); 7162 7163 // This scalar TruncStore may be illegal, but we legalize it later. 7164 SDValue Store = DAG.getTruncStore( 7165 Chain, SL, Elt, Ptr, ST->getPointerInfo().getWithOffset(Idx * Stride), 7166 MemSclVT, ST->getOriginalAlign(), ST->getMemOperand()->getFlags(), 7167 ST->getAAInfo()); 7168 7169 Stores.push_back(Store); 7170 } 7171 7172 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, Stores); 7173 } 7174 7175 std::pair<SDValue, SDValue> 7176 TargetLowering::expandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG) const { 7177 assert(LD->getAddressingMode() == ISD::UNINDEXED && 7178 "unaligned indexed loads not implemented!"); 7179 SDValue Chain = LD->getChain(); 7180 SDValue Ptr = LD->getBasePtr(); 7181 EVT VT = LD->getValueType(0); 7182 EVT LoadedVT = LD->getMemoryVT(); 7183 SDLoc dl(LD); 7184 auto &MF = DAG.getMachineFunction(); 7185 7186 if (VT.isFloatingPoint() || VT.isVector()) { 7187 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits()); 7188 if (isTypeLegal(intVT) && isTypeLegal(LoadedVT)) { 7189 if (!isOperationLegalOrCustom(ISD::LOAD, intVT) && 7190 LoadedVT.isVector()) { 7191 // Scalarize the load and let the individual components be handled. 7192 return scalarizeVectorLoad(LD, DAG); 7193 } 7194 7195 // Expand to a (misaligned) integer load of the same size, 7196 // then bitconvert to floating point or vector. 7197 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, 7198 LD->getMemOperand()); 7199 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad); 7200 if (LoadedVT != VT) 7201 Result = DAG.getNode(VT.isFloatingPoint() ? ISD::FP_EXTEND : 7202 ISD::ANY_EXTEND, dl, VT, Result); 7203 7204 return std::make_pair(Result, newLoad.getValue(1)); 7205 } 7206 7207 // Copy the value to a (aligned) stack slot using (unaligned) integer 7208 // loads and stores, then do a (aligned) load from the stack slot. 7209 MVT RegVT = getRegisterType(*DAG.getContext(), intVT); 7210 unsigned LoadedBytes = LoadedVT.getStoreSize(); 7211 unsigned RegBytes = RegVT.getSizeInBits() / 8; 7212 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes; 7213 7214 // Make sure the stack slot is also aligned for the register type. 7215 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT); 7216 auto FrameIndex = cast<FrameIndexSDNode>(StackBase.getNode())->getIndex(); 7217 SmallVector<SDValue, 8> Stores; 7218 SDValue StackPtr = StackBase; 7219 unsigned Offset = 0; 7220 7221 EVT PtrVT = Ptr.getValueType(); 7222 EVT StackPtrVT = StackPtr.getValueType(); 7223 7224 SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT); 7225 SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT); 7226 7227 // Do all but one copies using the full register width. 7228 for (unsigned i = 1; i < NumRegs; i++) { 7229 // Load one integer register's worth from the original location. 7230 SDValue Load = DAG.getLoad( 7231 RegVT, dl, Chain, Ptr, LD->getPointerInfo().getWithOffset(Offset), 7232 LD->getOriginalAlign(), LD->getMemOperand()->getFlags(), 7233 LD->getAAInfo()); 7234 // Follow the load with a store to the stack slot. Remember the store. 7235 Stores.push_back(DAG.getStore( 7236 Load.getValue(1), dl, Load, StackPtr, 7237 MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset))); 7238 // Increment the pointers. 7239 Offset += RegBytes; 7240 7241 Ptr = DAG.getObjectPtrOffset(dl, Ptr, PtrIncrement); 7242 StackPtr = DAG.getObjectPtrOffset(dl, StackPtr, StackPtrIncrement); 7243 } 7244 7245 // The last copy may be partial. Do an extending load. 7246 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), 7247 8 * (LoadedBytes - Offset)); 7248 SDValue Load = 7249 DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr, 7250 LD->getPointerInfo().getWithOffset(Offset), MemVT, 7251 LD->getOriginalAlign(), LD->getMemOperand()->getFlags(), 7252 LD->getAAInfo()); 7253 // Follow the load with a store to the stack slot. Remember the store. 7254 // On big-endian machines this requires a truncating store to ensure 7255 // that the bits end up in the right place. 7256 Stores.push_back(DAG.getTruncStore( 7257 Load.getValue(1), dl, Load, StackPtr, 7258 MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset), MemVT)); 7259 7260 // The order of the stores doesn't matter - say it with a TokenFactor. 7261 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 7262 7263 // Finally, perform the original load only redirected to the stack slot. 7264 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase, 7265 MachinePointerInfo::getFixedStack(MF, FrameIndex, 0), 7266 LoadedVT); 7267 7268 // Callers expect a MERGE_VALUES node. 7269 return std::make_pair(Load, TF); 7270 } 7271 7272 assert(LoadedVT.isInteger() && !LoadedVT.isVector() && 7273 "Unaligned load of unsupported type."); 7274 7275 // Compute the new VT that is half the size of the old one. This is an 7276 // integer MVT. 7277 unsigned NumBits = LoadedVT.getSizeInBits(); 7278 EVT NewLoadedVT; 7279 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2); 7280 NumBits >>= 1; 7281 7282 Align Alignment = LD->getOriginalAlign(); 7283 unsigned IncrementSize = NumBits / 8; 7284 ISD::LoadExtType HiExtType = LD->getExtensionType(); 7285 7286 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD. 7287 if (HiExtType == ISD::NON_EXTLOAD) 7288 HiExtType = ISD::ZEXTLOAD; 7289 7290 // Load the value in two parts 7291 SDValue Lo, Hi; 7292 if (DAG.getDataLayout().isLittleEndian()) { 7293 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(), 7294 NewLoadedVT, Alignment, LD->getMemOperand()->getFlags(), 7295 LD->getAAInfo()); 7296 7297 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::Fixed(IncrementSize)); 7298 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, 7299 LD->getPointerInfo().getWithOffset(IncrementSize), 7300 NewLoadedVT, Alignment, LD->getMemOperand()->getFlags(), 7301 LD->getAAInfo()); 7302 } else { 7303 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(), 7304 NewLoadedVT, Alignment, LD->getMemOperand()->getFlags(), 7305 LD->getAAInfo()); 7306 7307 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::Fixed(IncrementSize)); 7308 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, 7309 LD->getPointerInfo().getWithOffset(IncrementSize), 7310 NewLoadedVT, Alignment, LD->getMemOperand()->getFlags(), 7311 LD->getAAInfo()); 7312 } 7313 7314 // aggregate the two parts 7315 SDValue ShiftAmount = 7316 DAG.getConstant(NumBits, dl, getShiftAmountTy(Hi.getValueType(), 7317 DAG.getDataLayout())); 7318 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount); 7319 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo); 7320 7321 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 7322 Hi.getValue(1)); 7323 7324 return std::make_pair(Result, TF); 7325 } 7326 7327 SDValue TargetLowering::expandUnalignedStore(StoreSDNode *ST, 7328 SelectionDAG &DAG) const { 7329 assert(ST->getAddressingMode() == ISD::UNINDEXED && 7330 "unaligned indexed stores not implemented!"); 7331 SDValue Chain = ST->getChain(); 7332 SDValue Ptr = ST->getBasePtr(); 7333 SDValue Val = ST->getValue(); 7334 EVT VT = Val.getValueType(); 7335 Align Alignment = ST->getOriginalAlign(); 7336 auto &MF = DAG.getMachineFunction(); 7337 EVT StoreMemVT = ST->getMemoryVT(); 7338 7339 SDLoc dl(ST); 7340 if (StoreMemVT.isFloatingPoint() || StoreMemVT.isVector()) { 7341 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); 7342 if (isTypeLegal(intVT)) { 7343 if (!isOperationLegalOrCustom(ISD::STORE, intVT) && 7344 StoreMemVT.isVector()) { 7345 // Scalarize the store and let the individual components be handled. 7346 SDValue Result = scalarizeVectorStore(ST, DAG); 7347 return Result; 7348 } 7349 // Expand to a bitconvert of the value to the integer type of the 7350 // same size, then a (misaligned) int store. 7351 // FIXME: Does not handle truncating floating point stores! 7352 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val); 7353 Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(), 7354 Alignment, ST->getMemOperand()->getFlags()); 7355 return Result; 7356 } 7357 // Do a (aligned) store to a stack slot, then copy from the stack slot 7358 // to the final destination using (unaligned) integer loads and stores. 7359 MVT RegVT = getRegisterType( 7360 *DAG.getContext(), 7361 EVT::getIntegerVT(*DAG.getContext(), StoreMemVT.getSizeInBits())); 7362 EVT PtrVT = Ptr.getValueType(); 7363 unsigned StoredBytes = StoreMemVT.getStoreSize(); 7364 unsigned RegBytes = RegVT.getSizeInBits() / 8; 7365 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes; 7366 7367 // Make sure the stack slot is also aligned for the register type. 7368 SDValue StackPtr = DAG.CreateStackTemporary(StoreMemVT, RegVT); 7369 auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 7370 7371 // Perform the original store, only redirected to the stack slot. 7372 SDValue Store = DAG.getTruncStore( 7373 Chain, dl, Val, StackPtr, 7374 MachinePointerInfo::getFixedStack(MF, FrameIndex, 0), StoreMemVT); 7375 7376 EVT StackPtrVT = StackPtr.getValueType(); 7377 7378 SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT); 7379 SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT); 7380 SmallVector<SDValue, 8> Stores; 7381 unsigned Offset = 0; 7382 7383 // Do all but one copies using the full register width. 7384 for (unsigned i = 1; i < NumRegs; i++) { 7385 // Load one integer register's worth from the stack slot. 7386 SDValue Load = DAG.getLoad( 7387 RegVT, dl, Store, StackPtr, 7388 MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset)); 7389 // Store it to the final location. Remember the store. 7390 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr, 7391 ST->getPointerInfo().getWithOffset(Offset), 7392 ST->getOriginalAlign(), 7393 ST->getMemOperand()->getFlags())); 7394 // Increment the pointers. 7395 Offset += RegBytes; 7396 StackPtr = DAG.getObjectPtrOffset(dl, StackPtr, StackPtrIncrement); 7397 Ptr = DAG.getObjectPtrOffset(dl, Ptr, PtrIncrement); 7398 } 7399 7400 // The last store may be partial. Do a truncating store. On big-endian 7401 // machines this requires an extending load from the stack slot to ensure 7402 // that the bits are in the right place. 7403 EVT LoadMemVT = 7404 EVT::getIntegerVT(*DAG.getContext(), 8 * (StoredBytes - Offset)); 7405 7406 // Load from the stack slot. 7407 SDValue Load = DAG.getExtLoad( 7408 ISD::EXTLOAD, dl, RegVT, Store, StackPtr, 7409 MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset), LoadMemVT); 7410 7411 Stores.push_back( 7412 DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr, 7413 ST->getPointerInfo().getWithOffset(Offset), LoadMemVT, 7414 ST->getOriginalAlign(), 7415 ST->getMemOperand()->getFlags(), ST->getAAInfo())); 7416 // The order of the stores doesn't matter - say it with a TokenFactor. 7417 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 7418 return Result; 7419 } 7420 7421 assert(StoreMemVT.isInteger() && !StoreMemVT.isVector() && 7422 "Unaligned store of unknown type."); 7423 // Get the half-size VT 7424 EVT NewStoredVT = StoreMemVT.getHalfSizedIntegerVT(*DAG.getContext()); 7425 unsigned NumBits = NewStoredVT.getFixedSizeInBits(); 7426 unsigned IncrementSize = NumBits / 8; 7427 7428 // Divide the stored value in two parts. 7429 SDValue ShiftAmount = DAG.getConstant( 7430 NumBits, dl, getShiftAmountTy(Val.getValueType(), DAG.getDataLayout())); 7431 SDValue Lo = Val; 7432 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount); 7433 7434 // Store the two parts 7435 SDValue Store1, Store2; 7436 Store1 = DAG.getTruncStore(Chain, dl, 7437 DAG.getDataLayout().isLittleEndian() ? Lo : Hi, 7438 Ptr, ST->getPointerInfo(), NewStoredVT, Alignment, 7439 ST->getMemOperand()->getFlags()); 7440 7441 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::Fixed(IncrementSize)); 7442 Store2 = DAG.getTruncStore( 7443 Chain, dl, DAG.getDataLayout().isLittleEndian() ? Hi : Lo, Ptr, 7444 ST->getPointerInfo().getWithOffset(IncrementSize), NewStoredVT, Alignment, 7445 ST->getMemOperand()->getFlags(), ST->getAAInfo()); 7446 7447 SDValue Result = 7448 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2); 7449 return Result; 7450 } 7451 7452 SDValue 7453 TargetLowering::IncrementMemoryAddress(SDValue Addr, SDValue Mask, 7454 const SDLoc &DL, EVT DataVT, 7455 SelectionDAG &DAG, 7456 bool IsCompressedMemory) const { 7457 SDValue Increment; 7458 EVT AddrVT = Addr.getValueType(); 7459 EVT MaskVT = Mask.getValueType(); 7460 assert(DataVT.getVectorElementCount() == MaskVT.getVectorElementCount() && 7461 "Incompatible types of Data and Mask"); 7462 if (IsCompressedMemory) { 7463 if (DataVT.isScalableVector()) 7464 report_fatal_error( 7465 "Cannot currently handle compressed memory with scalable vectors"); 7466 // Incrementing the pointer according to number of '1's in the mask. 7467 EVT MaskIntVT = EVT::getIntegerVT(*DAG.getContext(), MaskVT.getSizeInBits()); 7468 SDValue MaskInIntReg = DAG.getBitcast(MaskIntVT, Mask); 7469 if (MaskIntVT.getSizeInBits() < 32) { 7470 MaskInIntReg = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, MaskInIntReg); 7471 MaskIntVT = MVT::i32; 7472 } 7473 7474 // Count '1's with POPCNT. 7475 Increment = DAG.getNode(ISD::CTPOP, DL, MaskIntVT, MaskInIntReg); 7476 Increment = DAG.getZExtOrTrunc(Increment, DL, AddrVT); 7477 // Scale is an element size in bytes. 7478 SDValue Scale = DAG.getConstant(DataVT.getScalarSizeInBits() / 8, DL, 7479 AddrVT); 7480 Increment = DAG.getNode(ISD::MUL, DL, AddrVT, Increment, Scale); 7481 } else if (DataVT.isScalableVector()) { 7482 Increment = DAG.getVScale(DL, AddrVT, 7483 APInt(AddrVT.getFixedSizeInBits(), 7484 DataVT.getStoreSize().getKnownMinSize())); 7485 } else 7486 Increment = DAG.getConstant(DataVT.getStoreSize(), DL, AddrVT); 7487 7488 return DAG.getNode(ISD::ADD, DL, AddrVT, Addr, Increment); 7489 } 7490 7491 static SDValue clampDynamicVectorIndex(SelectionDAG &DAG, 7492 SDValue Idx, 7493 EVT VecVT, 7494 const SDLoc &dl) { 7495 if (!VecVT.isScalableVector() && isa<ConstantSDNode>(Idx)) 7496 return Idx; 7497 7498 EVT IdxVT = Idx.getValueType(); 7499 unsigned NElts = VecVT.getVectorMinNumElements(); 7500 if (VecVT.isScalableVector()) { 7501 SDValue VS = DAG.getVScale(dl, IdxVT, 7502 APInt(IdxVT.getFixedSizeInBits(), 7503 NElts)); 7504 SDValue Sub = DAG.getNode(ISD::SUB, dl, IdxVT, VS, 7505 DAG.getConstant(1, dl, IdxVT)); 7506 7507 return DAG.getNode(ISD::UMIN, dl, IdxVT, Idx, Sub); 7508 } else { 7509 if (isPowerOf2_32(NElts)) { 7510 APInt Imm = APInt::getLowBitsSet(IdxVT.getSizeInBits(), 7511 Log2_32(NElts)); 7512 return DAG.getNode(ISD::AND, dl, IdxVT, Idx, 7513 DAG.getConstant(Imm, dl, IdxVT)); 7514 } 7515 } 7516 7517 return DAG.getNode(ISD::UMIN, dl, IdxVT, Idx, 7518 DAG.getConstant(NElts - 1, dl, IdxVT)); 7519 } 7520 7521 SDValue TargetLowering::getVectorElementPointer(SelectionDAG &DAG, 7522 SDValue VecPtr, EVT VecVT, 7523 SDValue Index) const { 7524 SDLoc dl(Index); 7525 // Make sure the index type is big enough to compute in. 7526 Index = DAG.getZExtOrTrunc(Index, dl, VecPtr.getValueType()); 7527 7528 EVT EltVT = VecVT.getVectorElementType(); 7529 7530 // Calculate the element offset and add it to the pointer. 7531 unsigned EltSize = EltVT.getFixedSizeInBits() / 8; // FIXME: should be ABI size. 7532 assert(EltSize * 8 == EltVT.getFixedSizeInBits() && 7533 "Converting bits to bytes lost precision"); 7534 7535 Index = clampDynamicVectorIndex(DAG, Index, VecVT, dl); 7536 7537 EVT IdxVT = Index.getValueType(); 7538 7539 Index = DAG.getNode(ISD::MUL, dl, IdxVT, Index, 7540 DAG.getConstant(EltSize, dl, IdxVT)); 7541 return DAG.getMemBasePlusOffset(VecPtr, Index, dl); 7542 } 7543 7544 //===----------------------------------------------------------------------===// 7545 // Implementation of Emulated TLS Model 7546 //===----------------------------------------------------------------------===// 7547 7548 SDValue TargetLowering::LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA, 7549 SelectionDAG &DAG) const { 7550 // Access to address of TLS varialbe xyz is lowered to a function call: 7551 // __emutls_get_address( address of global variable named "__emutls_v.xyz" ) 7552 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 7553 PointerType *VoidPtrType = Type::getInt8PtrTy(*DAG.getContext()); 7554 SDLoc dl(GA); 7555 7556 ArgListTy Args; 7557 ArgListEntry Entry; 7558 std::string NameString = ("__emutls_v." + GA->getGlobal()->getName()).str(); 7559 Module *VariableModule = const_cast<Module*>(GA->getGlobal()->getParent()); 7560 StringRef EmuTlsVarName(NameString); 7561 GlobalVariable *EmuTlsVar = VariableModule->getNamedGlobal(EmuTlsVarName); 7562 assert(EmuTlsVar && "Cannot find EmuTlsVar "); 7563 Entry.Node = DAG.getGlobalAddress(EmuTlsVar, dl, PtrVT); 7564 Entry.Ty = VoidPtrType; 7565 Args.push_back(Entry); 7566 7567 SDValue EmuTlsGetAddr = DAG.getExternalSymbol("__emutls_get_address", PtrVT); 7568 7569 TargetLowering::CallLoweringInfo CLI(DAG); 7570 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode()); 7571 CLI.setLibCallee(CallingConv::C, VoidPtrType, EmuTlsGetAddr, std::move(Args)); 7572 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 7573 7574 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls. 7575 // At last for X86 targets, maybe good for other targets too? 7576 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 7577 MFI.setAdjustsStack(true); // Is this only for X86 target? 7578 MFI.setHasCalls(true); 7579 7580 assert((GA->getOffset() == 0) && 7581 "Emulated TLS must have zero offset in GlobalAddressSDNode"); 7582 return CallResult.first; 7583 } 7584 7585 SDValue TargetLowering::lowerCmpEqZeroToCtlzSrl(SDValue Op, 7586 SelectionDAG &DAG) const { 7587 assert((Op->getOpcode() == ISD::SETCC) && "Input has to be a SETCC node."); 7588 if (!isCtlzFast()) 7589 return SDValue(); 7590 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 7591 SDLoc dl(Op); 7592 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 7593 if (C->isNullValue() && CC == ISD::SETEQ) { 7594 EVT VT = Op.getOperand(0).getValueType(); 7595 SDValue Zext = Op.getOperand(0); 7596 if (VT.bitsLT(MVT::i32)) { 7597 VT = MVT::i32; 7598 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0)); 7599 } 7600 unsigned Log2b = Log2_32(VT.getSizeInBits()); 7601 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext); 7602 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz, 7603 DAG.getConstant(Log2b, dl, MVT::i32)); 7604 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc); 7605 } 7606 } 7607 return SDValue(); 7608 } 7609 7610 // Convert redundant addressing modes (e.g. scaling is redundant 7611 // when accessing bytes). 7612 ISD::MemIndexType 7613 TargetLowering::getCanonicalIndexType(ISD::MemIndexType IndexType, EVT MemVT, 7614 SDValue Offsets) const { 7615 bool IsScaledIndex = 7616 (IndexType == ISD::SIGNED_SCALED) || (IndexType == ISD::UNSIGNED_SCALED); 7617 bool IsSignedIndex = 7618 (IndexType == ISD::SIGNED_SCALED) || (IndexType == ISD::SIGNED_UNSCALED); 7619 7620 // Scaling is unimportant for bytes, canonicalize to unscaled. 7621 if (IsScaledIndex && MemVT.getScalarType() == MVT::i8) { 7622 IsScaledIndex = false; 7623 IndexType = IsSignedIndex ? ISD::SIGNED_UNSCALED : ISD::UNSIGNED_UNSCALED; 7624 } 7625 7626 return IndexType; 7627 } 7628 7629 SDValue TargetLowering::expandIntMINMAX(SDNode *Node, SelectionDAG &DAG) const { 7630 SDValue Op0 = Node->getOperand(0); 7631 SDValue Op1 = Node->getOperand(1); 7632 EVT VT = Op0.getValueType(); 7633 unsigned Opcode = Node->getOpcode(); 7634 SDLoc DL(Node); 7635 7636 // umin(x,y) -> sub(x,usubsat(x,y)) 7637 if (Opcode == ISD::UMIN && isOperationLegal(ISD::SUB, VT) && 7638 isOperationLegal(ISD::USUBSAT, VT)) { 7639 return DAG.getNode(ISD::SUB, DL, VT, Op0, 7640 DAG.getNode(ISD::USUBSAT, DL, VT, Op0, Op1)); 7641 } 7642 7643 // umax(x,y) -> add(x,usubsat(y,x)) 7644 if (Opcode == ISD::UMAX && isOperationLegal(ISD::ADD, VT) && 7645 isOperationLegal(ISD::USUBSAT, VT)) { 7646 return DAG.getNode(ISD::ADD, DL, VT, Op0, 7647 DAG.getNode(ISD::USUBSAT, DL, VT, Op1, Op0)); 7648 } 7649 7650 // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B 7651 ISD::CondCode CC; 7652 switch (Opcode) { 7653 default: llvm_unreachable("How did we get here?"); 7654 case ISD::SMAX: CC = ISD::SETGT; break; 7655 case ISD::SMIN: CC = ISD::SETLT; break; 7656 case ISD::UMAX: CC = ISD::SETUGT; break; 7657 case ISD::UMIN: CC = ISD::SETULT; break; 7658 } 7659 7660 // FIXME: Should really try to split the vector in case it's legal on a 7661 // subvector. 7662 if (VT.isVector() && !isOperationLegalOrCustom(ISD::VSELECT, VT)) 7663 return DAG.UnrollVectorOp(Node); 7664 7665 SDValue Cond = DAG.getSetCC(DL, VT, Op0, Op1, CC); 7666 return DAG.getSelect(DL, VT, Cond, Op0, Op1); 7667 } 7668 7669 SDValue TargetLowering::expandAddSubSat(SDNode *Node, SelectionDAG &DAG) const { 7670 unsigned Opcode = Node->getOpcode(); 7671 SDValue LHS = Node->getOperand(0); 7672 SDValue RHS = Node->getOperand(1); 7673 EVT VT = LHS.getValueType(); 7674 SDLoc dl(Node); 7675 7676 assert(VT == RHS.getValueType() && "Expected operands to be the same type"); 7677 assert(VT.isInteger() && "Expected operands to be integers"); 7678 7679 // usub.sat(a, b) -> umax(a, b) - b 7680 if (Opcode == ISD::USUBSAT && isOperationLegal(ISD::UMAX, VT)) { 7681 SDValue Max = DAG.getNode(ISD::UMAX, dl, VT, LHS, RHS); 7682 return DAG.getNode(ISD::SUB, dl, VT, Max, RHS); 7683 } 7684 7685 // uadd.sat(a, b) -> umin(a, ~b) + b 7686 if (Opcode == ISD::UADDSAT && isOperationLegal(ISD::UMIN, VT)) { 7687 SDValue InvRHS = DAG.getNOT(dl, RHS, VT); 7688 SDValue Min = DAG.getNode(ISD::UMIN, dl, VT, LHS, InvRHS); 7689 return DAG.getNode(ISD::ADD, dl, VT, Min, RHS); 7690 } 7691 7692 unsigned OverflowOp; 7693 switch (Opcode) { 7694 case ISD::SADDSAT: 7695 OverflowOp = ISD::SADDO; 7696 break; 7697 case ISD::UADDSAT: 7698 OverflowOp = ISD::UADDO; 7699 break; 7700 case ISD::SSUBSAT: 7701 OverflowOp = ISD::SSUBO; 7702 break; 7703 case ISD::USUBSAT: 7704 OverflowOp = ISD::USUBO; 7705 break; 7706 default: 7707 llvm_unreachable("Expected method to receive signed or unsigned saturation " 7708 "addition or subtraction node."); 7709 } 7710 7711 // FIXME: Should really try to split the vector in case it's legal on a 7712 // subvector. 7713 if (VT.isVector() && !isOperationLegalOrCustom(ISD::VSELECT, VT)) 7714 return DAG.UnrollVectorOp(Node); 7715 7716 unsigned BitWidth = LHS.getScalarValueSizeInBits(); 7717 EVT BoolVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 7718 SDValue Result = DAG.getNode(OverflowOp, dl, DAG.getVTList(VT, BoolVT), 7719 LHS, RHS); 7720 SDValue SumDiff = Result.getValue(0); 7721 SDValue Overflow = Result.getValue(1); 7722 SDValue Zero = DAG.getConstant(0, dl, VT); 7723 SDValue AllOnes = DAG.getAllOnesConstant(dl, VT); 7724 7725 if (Opcode == ISD::UADDSAT) { 7726 if (getBooleanContents(VT) == ZeroOrNegativeOneBooleanContent) { 7727 // (LHS + RHS) | OverflowMask 7728 SDValue OverflowMask = DAG.getSExtOrTrunc(Overflow, dl, VT); 7729 return DAG.getNode(ISD::OR, dl, VT, SumDiff, OverflowMask); 7730 } 7731 // Overflow ? 0xffff.... : (LHS + RHS) 7732 return DAG.getSelect(dl, VT, Overflow, AllOnes, SumDiff); 7733 } else if (Opcode == ISD::USUBSAT) { 7734 if (getBooleanContents(VT) == ZeroOrNegativeOneBooleanContent) { 7735 // (LHS - RHS) & ~OverflowMask 7736 SDValue OverflowMask = DAG.getSExtOrTrunc(Overflow, dl, VT); 7737 SDValue Not = DAG.getNOT(dl, OverflowMask, VT); 7738 return DAG.getNode(ISD::AND, dl, VT, SumDiff, Not); 7739 } 7740 // Overflow ? 0 : (LHS - RHS) 7741 return DAG.getSelect(dl, VT, Overflow, Zero, SumDiff); 7742 } else { 7743 // SatMax -> Overflow && SumDiff < 0 7744 // SatMin -> Overflow && SumDiff >= 0 7745 APInt MinVal = APInt::getSignedMinValue(BitWidth); 7746 APInt MaxVal = APInt::getSignedMaxValue(BitWidth); 7747 SDValue SatMin = DAG.getConstant(MinVal, dl, VT); 7748 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT); 7749 SDValue SumNeg = DAG.getSetCC(dl, BoolVT, SumDiff, Zero, ISD::SETLT); 7750 Result = DAG.getSelect(dl, VT, SumNeg, SatMax, SatMin); 7751 return DAG.getSelect(dl, VT, Overflow, Result, SumDiff); 7752 } 7753 } 7754 7755 SDValue TargetLowering::expandShlSat(SDNode *Node, SelectionDAG &DAG) const { 7756 unsigned Opcode = Node->getOpcode(); 7757 bool IsSigned = Opcode == ISD::SSHLSAT; 7758 SDValue LHS = Node->getOperand(0); 7759 SDValue RHS = Node->getOperand(1); 7760 EVT VT = LHS.getValueType(); 7761 SDLoc dl(Node); 7762 7763 assert((Node->getOpcode() == ISD::SSHLSAT || 7764 Node->getOpcode() == ISD::USHLSAT) && 7765 "Expected a SHLSAT opcode"); 7766 assert(VT == RHS.getValueType() && "Expected operands to be the same type"); 7767 assert(VT.isInteger() && "Expected operands to be integers"); 7768 7769 // If LHS != (LHS << RHS) >> RHS, we have overflow and must saturate. 7770 7771 unsigned BW = VT.getScalarSizeInBits(); 7772 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, LHS, RHS); 7773 SDValue Orig = 7774 DAG.getNode(IsSigned ? ISD::SRA : ISD::SRL, dl, VT, Result, RHS); 7775 7776 SDValue SatVal; 7777 if (IsSigned) { 7778 SDValue SatMin = DAG.getConstant(APInt::getSignedMinValue(BW), dl, VT); 7779 SDValue SatMax = DAG.getConstant(APInt::getSignedMaxValue(BW), dl, VT); 7780 SatVal = DAG.getSelectCC(dl, LHS, DAG.getConstant(0, dl, VT), 7781 SatMin, SatMax, ISD::SETLT); 7782 } else { 7783 SatVal = DAG.getConstant(APInt::getMaxValue(BW), dl, VT); 7784 } 7785 Result = DAG.getSelectCC(dl, LHS, Orig, SatVal, Result, ISD::SETNE); 7786 7787 return Result; 7788 } 7789 7790 SDValue 7791 TargetLowering::expandFixedPointMul(SDNode *Node, SelectionDAG &DAG) const { 7792 assert((Node->getOpcode() == ISD::SMULFIX || 7793 Node->getOpcode() == ISD::UMULFIX || 7794 Node->getOpcode() == ISD::SMULFIXSAT || 7795 Node->getOpcode() == ISD::UMULFIXSAT) && 7796 "Expected a fixed point multiplication opcode"); 7797 7798 SDLoc dl(Node); 7799 SDValue LHS = Node->getOperand(0); 7800 SDValue RHS = Node->getOperand(1); 7801 EVT VT = LHS.getValueType(); 7802 unsigned Scale = Node->getConstantOperandVal(2); 7803 bool Saturating = (Node->getOpcode() == ISD::SMULFIXSAT || 7804 Node->getOpcode() == ISD::UMULFIXSAT); 7805 bool Signed = (Node->getOpcode() == ISD::SMULFIX || 7806 Node->getOpcode() == ISD::SMULFIXSAT); 7807 EVT BoolVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 7808 unsigned VTSize = VT.getScalarSizeInBits(); 7809 7810 if (!Scale) { 7811 // [us]mul.fix(a, b, 0) -> mul(a, b) 7812 if (!Saturating) { 7813 if (isOperationLegalOrCustom(ISD::MUL, VT)) 7814 return DAG.getNode(ISD::MUL, dl, VT, LHS, RHS); 7815 } else if (Signed && isOperationLegalOrCustom(ISD::SMULO, VT)) { 7816 SDValue Result = 7817 DAG.getNode(ISD::SMULO, dl, DAG.getVTList(VT, BoolVT), LHS, RHS); 7818 SDValue Product = Result.getValue(0); 7819 SDValue Overflow = Result.getValue(1); 7820 SDValue Zero = DAG.getConstant(0, dl, VT); 7821 7822 APInt MinVal = APInt::getSignedMinValue(VTSize); 7823 APInt MaxVal = APInt::getSignedMaxValue(VTSize); 7824 SDValue SatMin = DAG.getConstant(MinVal, dl, VT); 7825 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT); 7826 SDValue ProdNeg = DAG.getSetCC(dl, BoolVT, Product, Zero, ISD::SETLT); 7827 Result = DAG.getSelect(dl, VT, ProdNeg, SatMax, SatMin); 7828 return DAG.getSelect(dl, VT, Overflow, Result, Product); 7829 } else if (!Signed && isOperationLegalOrCustom(ISD::UMULO, VT)) { 7830 SDValue Result = 7831 DAG.getNode(ISD::UMULO, dl, DAG.getVTList(VT, BoolVT), LHS, RHS); 7832 SDValue Product = Result.getValue(0); 7833 SDValue Overflow = Result.getValue(1); 7834 7835 APInt MaxVal = APInt::getMaxValue(VTSize); 7836 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT); 7837 return DAG.getSelect(dl, VT, Overflow, SatMax, Product); 7838 } 7839 } 7840 7841 assert(((Signed && Scale < VTSize) || (!Signed && Scale <= VTSize)) && 7842 "Expected scale to be less than the number of bits if signed or at " 7843 "most the number of bits if unsigned."); 7844 assert(LHS.getValueType() == RHS.getValueType() && 7845 "Expected both operands to be the same type"); 7846 7847 // Get the upper and lower bits of the result. 7848 SDValue Lo, Hi; 7849 unsigned LoHiOp = Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI; 7850 unsigned HiOp = Signed ? ISD::MULHS : ISD::MULHU; 7851 if (isOperationLegalOrCustom(LoHiOp, VT)) { 7852 SDValue Result = DAG.getNode(LoHiOp, dl, DAG.getVTList(VT, VT), LHS, RHS); 7853 Lo = Result.getValue(0); 7854 Hi = Result.getValue(1); 7855 } else if (isOperationLegalOrCustom(HiOp, VT)) { 7856 Lo = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS); 7857 Hi = DAG.getNode(HiOp, dl, VT, LHS, RHS); 7858 } else if (VT.isVector()) { 7859 return SDValue(); 7860 } else { 7861 report_fatal_error("Unable to expand fixed point multiplication."); 7862 } 7863 7864 if (Scale == VTSize) 7865 // Result is just the top half since we'd be shifting by the width of the 7866 // operand. Overflow impossible so this works for both UMULFIX and 7867 // UMULFIXSAT. 7868 return Hi; 7869 7870 // The result will need to be shifted right by the scale since both operands 7871 // are scaled. The result is given to us in 2 halves, so we only want part of 7872 // both in the result. 7873 EVT ShiftTy = getShiftAmountTy(VT, DAG.getDataLayout()); 7874 SDValue Result = DAG.getNode(ISD::FSHR, dl, VT, Hi, Lo, 7875 DAG.getConstant(Scale, dl, ShiftTy)); 7876 if (!Saturating) 7877 return Result; 7878 7879 if (!Signed) { 7880 // Unsigned overflow happened if the upper (VTSize - Scale) bits (of the 7881 // widened multiplication) aren't all zeroes. 7882 7883 // Saturate to max if ((Hi >> Scale) != 0), 7884 // which is the same as if (Hi > ((1 << Scale) - 1)) 7885 APInt MaxVal = APInt::getMaxValue(VTSize); 7886 SDValue LowMask = DAG.getConstant(APInt::getLowBitsSet(VTSize, Scale), 7887 dl, VT); 7888 Result = DAG.getSelectCC(dl, Hi, LowMask, 7889 DAG.getConstant(MaxVal, dl, VT), Result, 7890 ISD::SETUGT); 7891 7892 return Result; 7893 } 7894 7895 // Signed overflow happened if the upper (VTSize - Scale + 1) bits (of the 7896 // widened multiplication) aren't all ones or all zeroes. 7897 7898 SDValue SatMin = DAG.getConstant(APInt::getSignedMinValue(VTSize), dl, VT); 7899 SDValue SatMax = DAG.getConstant(APInt::getSignedMaxValue(VTSize), dl, VT); 7900 7901 if (Scale == 0) { 7902 SDValue Sign = DAG.getNode(ISD::SRA, dl, VT, Lo, 7903 DAG.getConstant(VTSize - 1, dl, ShiftTy)); 7904 SDValue Overflow = DAG.getSetCC(dl, BoolVT, Hi, Sign, ISD::SETNE); 7905 // Saturated to SatMin if wide product is negative, and SatMax if wide 7906 // product is positive ... 7907 SDValue Zero = DAG.getConstant(0, dl, VT); 7908 SDValue ResultIfOverflow = DAG.getSelectCC(dl, Hi, Zero, SatMin, SatMax, 7909 ISD::SETLT); 7910 // ... but only if we overflowed. 7911 return DAG.getSelect(dl, VT, Overflow, ResultIfOverflow, Result); 7912 } 7913 7914 // We handled Scale==0 above so all the bits to examine is in Hi. 7915 7916 // Saturate to max if ((Hi >> (Scale - 1)) > 0), 7917 // which is the same as if (Hi > (1 << (Scale - 1)) - 1) 7918 SDValue LowMask = DAG.getConstant(APInt::getLowBitsSet(VTSize, Scale - 1), 7919 dl, VT); 7920 Result = DAG.getSelectCC(dl, Hi, LowMask, SatMax, Result, ISD::SETGT); 7921 // Saturate to min if (Hi >> (Scale - 1)) < -1), 7922 // which is the same as if (HI < (-1 << (Scale - 1)) 7923 SDValue HighMask = 7924 DAG.getConstant(APInt::getHighBitsSet(VTSize, VTSize - Scale + 1), 7925 dl, VT); 7926 Result = DAG.getSelectCC(dl, Hi, HighMask, SatMin, Result, ISD::SETLT); 7927 return Result; 7928 } 7929 7930 SDValue 7931 TargetLowering::expandFixedPointDiv(unsigned Opcode, const SDLoc &dl, 7932 SDValue LHS, SDValue RHS, 7933 unsigned Scale, SelectionDAG &DAG) const { 7934 assert((Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT || 7935 Opcode == ISD::UDIVFIX || Opcode == ISD::UDIVFIXSAT) && 7936 "Expected a fixed point division opcode"); 7937 7938 EVT VT = LHS.getValueType(); 7939 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT; 7940 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT; 7941 EVT BoolVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 7942 7943 // If there is enough room in the type to upscale the LHS or downscale the 7944 // RHS before the division, we can perform it in this type without having to 7945 // resize. For signed operations, the LHS headroom is the number of 7946 // redundant sign bits, and for unsigned ones it is the number of zeroes. 7947 // The headroom for the RHS is the number of trailing zeroes. 7948 unsigned LHSLead = Signed ? DAG.ComputeNumSignBits(LHS) - 1 7949 : DAG.computeKnownBits(LHS).countMinLeadingZeros(); 7950 unsigned RHSTrail = DAG.computeKnownBits(RHS).countMinTrailingZeros(); 7951 7952 // For signed saturating operations, we need to be able to detect true integer 7953 // division overflow; that is, when you have MIN / -EPS. However, this 7954 // is undefined behavior and if we emit divisions that could take such 7955 // values it may cause undesired behavior (arithmetic exceptions on x86, for 7956 // example). 7957 // Avoid this by requiring an extra bit so that we never get this case. 7958 // FIXME: This is a bit unfortunate as it means that for an 8-bit 7-scale 7959 // signed saturating division, we need to emit a whopping 32-bit division. 7960 if (LHSLead + RHSTrail < Scale + (unsigned)(Saturating && Signed)) 7961 return SDValue(); 7962 7963 unsigned LHSShift = std::min(LHSLead, Scale); 7964 unsigned RHSShift = Scale - LHSShift; 7965 7966 // At this point, we know that if we shift the LHS up by LHSShift and the 7967 // RHS down by RHSShift, we can emit a regular division with a final scaling 7968 // factor of Scale. 7969 7970 EVT ShiftTy = getShiftAmountTy(VT, DAG.getDataLayout()); 7971 if (LHSShift) 7972 LHS = DAG.getNode(ISD::SHL, dl, VT, LHS, 7973 DAG.getConstant(LHSShift, dl, ShiftTy)); 7974 if (RHSShift) 7975 RHS = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, dl, VT, RHS, 7976 DAG.getConstant(RHSShift, dl, ShiftTy)); 7977 7978 SDValue Quot; 7979 if (Signed) { 7980 // For signed operations, if the resulting quotient is negative and the 7981 // remainder is nonzero, subtract 1 from the quotient to round towards 7982 // negative infinity. 7983 SDValue Rem; 7984 // FIXME: Ideally we would always produce an SDIVREM here, but if the 7985 // type isn't legal, SDIVREM cannot be expanded. There is no reason why 7986 // we couldn't just form a libcall, but the type legalizer doesn't do it. 7987 if (isTypeLegal(VT) && 7988 isOperationLegalOrCustom(ISD::SDIVREM, VT)) { 7989 Quot = DAG.getNode(ISD::SDIVREM, dl, 7990 DAG.getVTList(VT, VT), 7991 LHS, RHS); 7992 Rem = Quot.getValue(1); 7993 Quot = Quot.getValue(0); 7994 } else { 7995 Quot = DAG.getNode(ISD::SDIV, dl, VT, 7996 LHS, RHS); 7997 Rem = DAG.getNode(ISD::SREM, dl, VT, 7998 LHS, RHS); 7999 } 8000 SDValue Zero = DAG.getConstant(0, dl, VT); 8001 SDValue RemNonZero = DAG.getSetCC(dl, BoolVT, Rem, Zero, ISD::SETNE); 8002 SDValue LHSNeg = DAG.getSetCC(dl, BoolVT, LHS, Zero, ISD::SETLT); 8003 SDValue RHSNeg = DAG.getSetCC(dl, BoolVT, RHS, Zero, ISD::SETLT); 8004 SDValue QuotNeg = DAG.getNode(ISD::XOR, dl, BoolVT, LHSNeg, RHSNeg); 8005 SDValue Sub1 = DAG.getNode(ISD::SUB, dl, VT, Quot, 8006 DAG.getConstant(1, dl, VT)); 8007 Quot = DAG.getSelect(dl, VT, 8008 DAG.getNode(ISD::AND, dl, BoolVT, RemNonZero, QuotNeg), 8009 Sub1, Quot); 8010 } else 8011 Quot = DAG.getNode(ISD::UDIV, dl, VT, 8012 LHS, RHS); 8013 8014 return Quot; 8015 } 8016 8017 void TargetLowering::expandUADDSUBO( 8018 SDNode *Node, SDValue &Result, SDValue &Overflow, SelectionDAG &DAG) const { 8019 SDLoc dl(Node); 8020 SDValue LHS = Node->getOperand(0); 8021 SDValue RHS = Node->getOperand(1); 8022 bool IsAdd = Node->getOpcode() == ISD::UADDO; 8023 8024 // If ADD/SUBCARRY is legal, use that instead. 8025 unsigned OpcCarry = IsAdd ? ISD::ADDCARRY : ISD::SUBCARRY; 8026 if (isOperationLegalOrCustom(OpcCarry, Node->getValueType(0))) { 8027 SDValue CarryIn = DAG.getConstant(0, dl, Node->getValueType(1)); 8028 SDValue NodeCarry = DAG.getNode(OpcCarry, dl, Node->getVTList(), 8029 { LHS, RHS, CarryIn }); 8030 Result = SDValue(NodeCarry.getNode(), 0); 8031 Overflow = SDValue(NodeCarry.getNode(), 1); 8032 return; 8033 } 8034 8035 Result = DAG.getNode(IsAdd ? ISD::ADD : ISD::SUB, dl, 8036 LHS.getValueType(), LHS, RHS); 8037 8038 EVT ResultType = Node->getValueType(1); 8039 EVT SetCCType = getSetCCResultType( 8040 DAG.getDataLayout(), *DAG.getContext(), Node->getValueType(0)); 8041 ISD::CondCode CC = IsAdd ? ISD::SETULT : ISD::SETUGT; 8042 SDValue SetCC = DAG.getSetCC(dl, SetCCType, Result, LHS, CC); 8043 Overflow = DAG.getBoolExtOrTrunc(SetCC, dl, ResultType, ResultType); 8044 } 8045 8046 void TargetLowering::expandSADDSUBO( 8047 SDNode *Node, SDValue &Result, SDValue &Overflow, SelectionDAG &DAG) const { 8048 SDLoc dl(Node); 8049 SDValue LHS = Node->getOperand(0); 8050 SDValue RHS = Node->getOperand(1); 8051 bool IsAdd = Node->getOpcode() == ISD::SADDO; 8052 8053 Result = DAG.getNode(IsAdd ? ISD::ADD : ISD::SUB, dl, 8054 LHS.getValueType(), LHS, RHS); 8055 8056 EVT ResultType = Node->getValueType(1); 8057 EVT OType = getSetCCResultType( 8058 DAG.getDataLayout(), *DAG.getContext(), Node->getValueType(0)); 8059 8060 // If SADDSAT/SSUBSAT is legal, compare results to detect overflow. 8061 unsigned OpcSat = IsAdd ? ISD::SADDSAT : ISD::SSUBSAT; 8062 if (isOperationLegalOrCustom(OpcSat, LHS.getValueType())) { 8063 SDValue Sat = DAG.getNode(OpcSat, dl, LHS.getValueType(), LHS, RHS); 8064 SDValue SetCC = DAG.getSetCC(dl, OType, Result, Sat, ISD::SETNE); 8065 Overflow = DAG.getBoolExtOrTrunc(SetCC, dl, ResultType, ResultType); 8066 return; 8067 } 8068 8069 SDValue Zero = DAG.getConstant(0, dl, LHS.getValueType()); 8070 8071 // For an addition, the result should be less than one of the operands (LHS) 8072 // if and only if the other operand (RHS) is negative, otherwise there will 8073 // be overflow. 8074 // For a subtraction, the result should be less than one of the operands 8075 // (LHS) if and only if the other operand (RHS) is (non-zero) positive, 8076 // otherwise there will be overflow. 8077 SDValue ResultLowerThanLHS = DAG.getSetCC(dl, OType, Result, LHS, ISD::SETLT); 8078 SDValue ConditionRHS = 8079 DAG.getSetCC(dl, OType, RHS, Zero, IsAdd ? ISD::SETLT : ISD::SETGT); 8080 8081 Overflow = DAG.getBoolExtOrTrunc( 8082 DAG.getNode(ISD::XOR, dl, OType, ConditionRHS, ResultLowerThanLHS), dl, 8083 ResultType, ResultType); 8084 } 8085 8086 bool TargetLowering::expandMULO(SDNode *Node, SDValue &Result, 8087 SDValue &Overflow, SelectionDAG &DAG) const { 8088 SDLoc dl(Node); 8089 EVT VT = Node->getValueType(0); 8090 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 8091 SDValue LHS = Node->getOperand(0); 8092 SDValue RHS = Node->getOperand(1); 8093 bool isSigned = Node->getOpcode() == ISD::SMULO; 8094 8095 // For power-of-two multiplications we can use a simpler shift expansion. 8096 if (ConstantSDNode *RHSC = isConstOrConstSplat(RHS)) { 8097 const APInt &C = RHSC->getAPIntValue(); 8098 // mulo(X, 1 << S) -> { X << S, (X << S) >> S != X } 8099 if (C.isPowerOf2()) { 8100 // smulo(x, signed_min) is same as umulo(x, signed_min). 8101 bool UseArithShift = isSigned && !C.isMinSignedValue(); 8102 EVT ShiftAmtTy = getShiftAmountTy(VT, DAG.getDataLayout()); 8103 SDValue ShiftAmt = DAG.getConstant(C.logBase2(), dl, ShiftAmtTy); 8104 Result = DAG.getNode(ISD::SHL, dl, VT, LHS, ShiftAmt); 8105 Overflow = DAG.getSetCC(dl, SetCCVT, 8106 DAG.getNode(UseArithShift ? ISD::SRA : ISD::SRL, 8107 dl, VT, Result, ShiftAmt), 8108 LHS, ISD::SETNE); 8109 return true; 8110 } 8111 } 8112 8113 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getScalarSizeInBits() * 2); 8114 if (VT.isVector()) 8115 WideVT = EVT::getVectorVT(*DAG.getContext(), WideVT, 8116 VT.getVectorNumElements()); 8117 8118 SDValue BottomHalf; 8119 SDValue TopHalf; 8120 static const unsigned Ops[2][3] = 8121 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND }, 8122 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }}; 8123 if (isOperationLegalOrCustom(Ops[isSigned][0], VT)) { 8124 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS); 8125 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS); 8126 } else if (isOperationLegalOrCustom(Ops[isSigned][1], VT)) { 8127 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS, 8128 RHS); 8129 TopHalf = BottomHalf.getValue(1); 8130 } else if (isTypeLegal(WideVT)) { 8131 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS); 8132 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS); 8133 SDValue Mul = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS); 8134 BottomHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, Mul); 8135 SDValue ShiftAmt = DAG.getConstant(VT.getScalarSizeInBits(), dl, 8136 getShiftAmountTy(WideVT, DAG.getDataLayout())); 8137 TopHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, 8138 DAG.getNode(ISD::SRL, dl, WideVT, Mul, ShiftAmt)); 8139 } else { 8140 if (VT.isVector()) 8141 return false; 8142 8143 // We can fall back to a libcall with an illegal type for the MUL if we 8144 // have a libcall big enough. 8145 // Also, we can fall back to a division in some cases, but that's a big 8146 // performance hit in the general case. 8147 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 8148 if (WideVT == MVT::i16) 8149 LC = RTLIB::MUL_I16; 8150 else if (WideVT == MVT::i32) 8151 LC = RTLIB::MUL_I32; 8152 else if (WideVT == MVT::i64) 8153 LC = RTLIB::MUL_I64; 8154 else if (WideVT == MVT::i128) 8155 LC = RTLIB::MUL_I128; 8156 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!"); 8157 8158 SDValue HiLHS; 8159 SDValue HiRHS; 8160 if (isSigned) { 8161 // The high part is obtained by SRA'ing all but one of the bits of low 8162 // part. 8163 unsigned LoSize = VT.getFixedSizeInBits(); 8164 HiLHS = 8165 DAG.getNode(ISD::SRA, dl, VT, LHS, 8166 DAG.getConstant(LoSize - 1, dl, 8167 getPointerTy(DAG.getDataLayout()))); 8168 HiRHS = 8169 DAG.getNode(ISD::SRA, dl, VT, RHS, 8170 DAG.getConstant(LoSize - 1, dl, 8171 getPointerTy(DAG.getDataLayout()))); 8172 } else { 8173 HiLHS = DAG.getConstant(0, dl, VT); 8174 HiRHS = DAG.getConstant(0, dl, VT); 8175 } 8176 8177 // Here we're passing the 2 arguments explicitly as 4 arguments that are 8178 // pre-lowered to the correct types. This all depends upon WideVT not 8179 // being a legal type for the architecture and thus has to be split to 8180 // two arguments. 8181 SDValue Ret; 8182 TargetLowering::MakeLibCallOptions CallOptions; 8183 CallOptions.setSExt(isSigned); 8184 CallOptions.setIsPostTypeLegalization(true); 8185 if (shouldSplitFunctionArgumentsAsLittleEndian(DAG.getDataLayout())) { 8186 // Halves of WideVT are packed into registers in different order 8187 // depending on platform endianness. This is usually handled by 8188 // the C calling convention, but we can't defer to it in 8189 // the legalizer. 8190 SDValue Args[] = { LHS, HiLHS, RHS, HiRHS }; 8191 Ret = makeLibCall(DAG, LC, WideVT, Args, CallOptions, dl).first; 8192 } else { 8193 SDValue Args[] = { HiLHS, LHS, HiRHS, RHS }; 8194 Ret = makeLibCall(DAG, LC, WideVT, Args, CallOptions, dl).first; 8195 } 8196 assert(Ret.getOpcode() == ISD::MERGE_VALUES && 8197 "Ret value is a collection of constituent nodes holding result."); 8198 if (DAG.getDataLayout().isLittleEndian()) { 8199 // Same as above. 8200 BottomHalf = Ret.getOperand(0); 8201 TopHalf = Ret.getOperand(1); 8202 } else { 8203 BottomHalf = Ret.getOperand(1); 8204 TopHalf = Ret.getOperand(0); 8205 } 8206 } 8207 8208 Result = BottomHalf; 8209 if (isSigned) { 8210 SDValue ShiftAmt = DAG.getConstant( 8211 VT.getScalarSizeInBits() - 1, dl, 8212 getShiftAmountTy(BottomHalf.getValueType(), DAG.getDataLayout())); 8213 SDValue Sign = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt); 8214 Overflow = DAG.getSetCC(dl, SetCCVT, TopHalf, Sign, ISD::SETNE); 8215 } else { 8216 Overflow = DAG.getSetCC(dl, SetCCVT, TopHalf, 8217 DAG.getConstant(0, dl, VT), ISD::SETNE); 8218 } 8219 8220 // Truncate the result if SetCC returns a larger type than needed. 8221 EVT RType = Node->getValueType(1); 8222 if (RType.bitsLT(Overflow.getValueType())) 8223 Overflow = DAG.getNode(ISD::TRUNCATE, dl, RType, Overflow); 8224 8225 assert(RType.getSizeInBits() == Overflow.getValueSizeInBits() && 8226 "Unexpected result type for S/UMULO legalization"); 8227 return true; 8228 } 8229 8230 SDValue TargetLowering::expandVecReduce(SDNode *Node, SelectionDAG &DAG) const { 8231 SDLoc dl(Node); 8232 unsigned BaseOpcode = ISD::getVecReduceBaseOpcode(Node->getOpcode()); 8233 SDValue Op = Node->getOperand(0); 8234 EVT VT = Op.getValueType(); 8235 8236 if (VT.isScalableVector()) 8237 report_fatal_error( 8238 "Expanding reductions for scalable vectors is undefined."); 8239 8240 // Try to use a shuffle reduction for power of two vectors. 8241 if (VT.isPow2VectorType()) { 8242 while (VT.getVectorNumElements() > 1) { 8243 EVT HalfVT = VT.getHalfNumVectorElementsVT(*DAG.getContext()); 8244 if (!isOperationLegalOrCustom(BaseOpcode, HalfVT)) 8245 break; 8246 8247 SDValue Lo, Hi; 8248 std::tie(Lo, Hi) = DAG.SplitVector(Op, dl); 8249 Op = DAG.getNode(BaseOpcode, dl, HalfVT, Lo, Hi); 8250 VT = HalfVT; 8251 } 8252 } 8253 8254 EVT EltVT = VT.getVectorElementType(); 8255 unsigned NumElts = VT.getVectorNumElements(); 8256 8257 SmallVector<SDValue, 8> Ops; 8258 DAG.ExtractVectorElements(Op, Ops, 0, NumElts); 8259 8260 SDValue Res = Ops[0]; 8261 for (unsigned i = 1; i < NumElts; i++) 8262 Res = DAG.getNode(BaseOpcode, dl, EltVT, Res, Ops[i], Node->getFlags()); 8263 8264 // Result type may be wider than element type. 8265 if (EltVT != Node->getValueType(0)) 8266 Res = DAG.getNode(ISD::ANY_EXTEND, dl, Node->getValueType(0), Res); 8267 return Res; 8268 } 8269 8270 SDValue TargetLowering::expandVecReduceSeq(SDNode *Node, SelectionDAG &DAG) const { 8271 SDLoc dl(Node); 8272 SDValue AccOp = Node->getOperand(0); 8273 SDValue VecOp = Node->getOperand(1); 8274 SDNodeFlags Flags = Node->getFlags(); 8275 8276 EVT VT = VecOp.getValueType(); 8277 EVT EltVT = VT.getVectorElementType(); 8278 8279 if (VT.isScalableVector()) 8280 report_fatal_error( 8281 "Expanding reductions for scalable vectors is undefined."); 8282 8283 unsigned NumElts = VT.getVectorNumElements(); 8284 8285 SmallVector<SDValue, 8> Ops; 8286 DAG.ExtractVectorElements(VecOp, Ops, 0, NumElts); 8287 8288 unsigned BaseOpcode = ISD::getVecReduceBaseOpcode(Node->getOpcode()); 8289 8290 SDValue Res = AccOp; 8291 for (unsigned i = 0; i < NumElts; i++) 8292 Res = DAG.getNode(BaseOpcode, dl, EltVT, Res, Ops[i], Flags); 8293 8294 return Res; 8295 } 8296 8297 bool TargetLowering::expandREM(SDNode *Node, SDValue &Result, 8298 SelectionDAG &DAG) const { 8299 EVT VT = Node->getValueType(0); 8300 SDLoc dl(Node); 8301 bool isSigned = Node->getOpcode() == ISD::SREM; 8302 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV; 8303 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; 8304 SDValue Dividend = Node->getOperand(0); 8305 SDValue Divisor = Node->getOperand(1); 8306 if (isOperationLegalOrCustom(DivRemOpc, VT)) { 8307 SDVTList VTs = DAG.getVTList(VT, VT); 8308 Result = DAG.getNode(DivRemOpc, dl, VTs, Dividend, Divisor).getValue(1); 8309 return true; 8310 } else if (isOperationLegalOrCustom(DivOpc, VT)) { 8311 // X % Y -> X-X/Y*Y 8312 SDValue Divide = DAG.getNode(DivOpc, dl, VT, Dividend, Divisor); 8313 SDValue Mul = DAG.getNode(ISD::MUL, dl, VT, Divide, Divisor); 8314 Result = DAG.getNode(ISD::SUB, dl, VT, Dividend, Mul); 8315 return true; 8316 } 8317 return false; 8318 } 8319 8320 SDValue TargetLowering::expandFP_TO_INT_SAT(SDNode *Node, 8321 SelectionDAG &DAG) const { 8322 bool IsSigned = Node->getOpcode() == ISD::FP_TO_SINT_SAT; 8323 SDLoc dl(SDValue(Node, 0)); 8324 SDValue Src = Node->getOperand(0); 8325 8326 // DstVT is the result type, while SatVT is the size to which we saturate 8327 EVT SrcVT = Src.getValueType(); 8328 EVT DstVT = Node->getValueType(0); 8329 8330 unsigned SatWidth = Node->getConstantOperandVal(1); 8331 unsigned DstWidth = DstVT.getScalarSizeInBits(); 8332 assert(SatWidth <= DstWidth && 8333 "Expected saturation width smaller than result width"); 8334 8335 // Determine minimum and maximum integer values and their corresponding 8336 // floating-point values. 8337 APInt MinInt, MaxInt; 8338 if (IsSigned) { 8339 MinInt = APInt::getSignedMinValue(SatWidth).sextOrSelf(DstWidth); 8340 MaxInt = APInt::getSignedMaxValue(SatWidth).sextOrSelf(DstWidth); 8341 } else { 8342 MinInt = APInt::getMinValue(SatWidth).zextOrSelf(DstWidth); 8343 MaxInt = APInt::getMaxValue(SatWidth).zextOrSelf(DstWidth); 8344 } 8345 8346 // We cannot risk emitting FP_TO_XINT nodes with a source VT of f16, as 8347 // libcall emission cannot handle this. Large result types will fail. 8348 if (SrcVT == MVT::f16) { 8349 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f32, Src); 8350 SrcVT = Src.getValueType(); 8351 } 8352 8353 APFloat MinFloat(DAG.EVTToAPFloatSemantics(SrcVT)); 8354 APFloat MaxFloat(DAG.EVTToAPFloatSemantics(SrcVT)); 8355 8356 APFloat::opStatus MinStatus = 8357 MinFloat.convertFromAPInt(MinInt, IsSigned, APFloat::rmTowardZero); 8358 APFloat::opStatus MaxStatus = 8359 MaxFloat.convertFromAPInt(MaxInt, IsSigned, APFloat::rmTowardZero); 8360 bool AreExactFloatBounds = !(MinStatus & APFloat::opStatus::opInexact) && 8361 !(MaxStatus & APFloat::opStatus::opInexact); 8362 8363 SDValue MinFloatNode = DAG.getConstantFP(MinFloat, dl, SrcVT); 8364 SDValue MaxFloatNode = DAG.getConstantFP(MaxFloat, dl, SrcVT); 8365 8366 // If the integer bounds are exactly representable as floats and min/max are 8367 // legal, emit a min+max+fptoi sequence. Otherwise we have to use a sequence 8368 // of comparisons and selects. 8369 bool MinMaxLegal = isOperationLegal(ISD::FMINNUM, SrcVT) && 8370 isOperationLegal(ISD::FMAXNUM, SrcVT); 8371 if (AreExactFloatBounds && MinMaxLegal) { 8372 SDValue Clamped = Src; 8373 8374 // Clamp Src by MinFloat from below. If Src is NaN the result is MinFloat. 8375 Clamped = DAG.getNode(ISD::FMAXNUM, dl, SrcVT, Clamped, MinFloatNode); 8376 // Clamp by MaxFloat from above. NaN cannot occur. 8377 Clamped = DAG.getNode(ISD::FMINNUM, dl, SrcVT, Clamped, MaxFloatNode); 8378 // Convert clamped value to integer. 8379 SDValue FpToInt = DAG.getNode(IsSigned ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, 8380 dl, DstVT, Clamped); 8381 8382 // In the unsigned case we're done, because we mapped NaN to MinFloat, 8383 // which will cast to zero. 8384 if (!IsSigned) 8385 return FpToInt; 8386 8387 // Otherwise, select 0 if Src is NaN. 8388 SDValue ZeroInt = DAG.getConstant(0, dl, DstVT); 8389 return DAG.getSelectCC(dl, Src, Src, ZeroInt, FpToInt, 8390 ISD::CondCode::SETUO); 8391 } 8392 8393 SDValue MinIntNode = DAG.getConstant(MinInt, dl, DstVT); 8394 SDValue MaxIntNode = DAG.getConstant(MaxInt, dl, DstVT); 8395 8396 // Result of direct conversion. The assumption here is that the operation is 8397 // non-trapping and it's fine to apply it to an out-of-range value if we 8398 // select it away later. 8399 SDValue FpToInt = 8400 DAG.getNode(IsSigned ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, dl, DstVT, Src); 8401 8402 SDValue Select = FpToInt; 8403 8404 // If Src ULT MinFloat, select MinInt. In particular, this also selects 8405 // MinInt if Src is NaN. 8406 Select = DAG.getSelectCC(dl, Src, MinFloatNode, MinIntNode, Select, 8407 ISD::CondCode::SETULT); 8408 // If Src OGT MaxFloat, select MaxInt. 8409 Select = DAG.getSelectCC(dl, Src, MaxFloatNode, MaxIntNode, Select, 8410 ISD::CondCode::SETOGT); 8411 8412 // In the unsigned case we are done, because we mapped NaN to MinInt, which 8413 // is already zero. 8414 if (!IsSigned) 8415 return Select; 8416 8417 // Otherwise, select 0 if Src is NaN. 8418 SDValue ZeroInt = DAG.getConstant(0, dl, DstVT); 8419 return DAG.getSelectCC(dl, Src, Src, ZeroInt, Select, ISD::CondCode::SETUO); 8420 } 8421