1 //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This implements the TargetLowering class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Target/TargetLowering.h" 15 #include "llvm/ADT/BitVector.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/CodeGen/Analysis.h" 18 #include "llvm/CodeGen/CallingConvLower.h" 19 #include "llvm/CodeGen/MachineFrameInfo.h" 20 #include "llvm/CodeGen/MachineFunction.h" 21 #include "llvm/CodeGen/MachineJumpTableInfo.h" 22 #include "llvm/CodeGen/MachineRegisterInfo.h" 23 #include "llvm/CodeGen/SelectionDAG.h" 24 #include "llvm/IR/DataLayout.h" 25 #include "llvm/IR/DerivedTypes.h" 26 #include "llvm/IR/GlobalVariable.h" 27 #include "llvm/IR/LLVMContext.h" 28 #include "llvm/MC/MCAsmInfo.h" 29 #include "llvm/MC/MCExpr.h" 30 #include "llvm/Support/ErrorHandling.h" 31 #include "llvm/Support/MathExtras.h" 32 #include "llvm/Target/TargetLoweringObjectFile.h" 33 #include "llvm/Target/TargetMachine.h" 34 #include "llvm/Target/TargetRegisterInfo.h" 35 #include "llvm/Target/TargetSubtargetInfo.h" 36 #include <cctype> 37 using namespace llvm; 38 39 /// NOTE: The TargetMachine owns TLOF. 40 TargetLowering::TargetLowering(const TargetMachine &tm) 41 : TargetLoweringBase(tm) {} 42 43 const char *TargetLowering::getTargetNodeName(unsigned Opcode) const { 44 return nullptr; 45 } 46 47 /// Check whether a given call node is in tail position within its function. If 48 /// so, it sets Chain to the input chain of the tail call. 49 bool TargetLowering::isInTailCallPosition(SelectionDAG &DAG, SDNode *Node, 50 SDValue &Chain) const { 51 const Function *F = DAG.getMachineFunction().getFunction(); 52 53 // Conservatively require the attributes of the call to match those of 54 // the return. Ignore noalias because it doesn't affect the call sequence. 55 AttributeSet CallerAttrs = F->getAttributes(); 56 if (AttrBuilder(CallerAttrs, AttributeSet::ReturnIndex) 57 .removeAttribute(Attribute::NoAlias).hasAttributes()) 58 return false; 59 60 // It's not safe to eliminate the sign / zero extension of the return value. 61 if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) || 62 CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt)) 63 return false; 64 65 // Check if the only use is a function return node. 66 return isUsedByReturnOnly(Node, Chain); 67 } 68 69 bool TargetLowering::parametersInCSRMatch(const MachineRegisterInfo &MRI, 70 const uint32_t *CallerPreservedMask, 71 const SmallVectorImpl<CCValAssign> &ArgLocs, 72 const SmallVectorImpl<SDValue> &OutVals) const { 73 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { 74 const CCValAssign &ArgLoc = ArgLocs[I]; 75 if (!ArgLoc.isRegLoc()) 76 continue; 77 unsigned Reg = ArgLoc.getLocReg(); 78 // Only look at callee saved registers. 79 if (MachineOperand::clobbersPhysReg(CallerPreservedMask, Reg)) 80 continue; 81 // Check that we pass the value used for the caller. 82 // (We look for a CopyFromReg reading a virtual register that is used 83 // for the function live-in value of register Reg) 84 SDValue Value = OutVals[I]; 85 if (Value->getOpcode() != ISD::CopyFromReg) 86 return false; 87 unsigned ArgReg = cast<RegisterSDNode>(Value->getOperand(1))->getReg(); 88 if (MRI.getLiveInPhysReg(ArgReg) != Reg) 89 return false; 90 } 91 return true; 92 } 93 94 /// \brief Set CallLoweringInfo attribute flags based on a call instruction 95 /// and called function attributes. 96 void TargetLowering::ArgListEntry::setAttributes(ImmutableCallSite *CS, 97 unsigned AttrIdx) { 98 isSExt = CS->paramHasAttr(AttrIdx, Attribute::SExt); 99 isZExt = CS->paramHasAttr(AttrIdx, Attribute::ZExt); 100 isInReg = CS->paramHasAttr(AttrIdx, Attribute::InReg); 101 isSRet = CS->paramHasAttr(AttrIdx, Attribute::StructRet); 102 isNest = CS->paramHasAttr(AttrIdx, Attribute::Nest); 103 isByVal = CS->paramHasAttr(AttrIdx, Attribute::ByVal); 104 isInAlloca = CS->paramHasAttr(AttrIdx, Attribute::InAlloca); 105 isReturned = CS->paramHasAttr(AttrIdx, Attribute::Returned); 106 isSwiftSelf = CS->paramHasAttr(AttrIdx, Attribute::SwiftSelf); 107 isSwiftError = CS->paramHasAttr(AttrIdx, Attribute::SwiftError); 108 Alignment = CS->getParamAlignment(AttrIdx); 109 } 110 111 /// Generate a libcall taking the given operands as arguments and returning a 112 /// result of type RetVT. 113 std::pair<SDValue, SDValue> 114 TargetLowering::makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT, 115 ArrayRef<SDValue> Ops, bool isSigned, 116 const SDLoc &dl, bool doesNotReturn, 117 bool isReturnValueUsed) const { 118 TargetLowering::ArgListTy Args; 119 Args.reserve(Ops.size()); 120 121 TargetLowering::ArgListEntry Entry; 122 for (SDValue Op : Ops) { 123 Entry.Node = Op; 124 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext()); 125 Entry.isSExt = shouldSignExtendTypeInLibCall(Op.getValueType(), isSigned); 126 Entry.isZExt = !shouldSignExtendTypeInLibCall(Op.getValueType(), isSigned); 127 Args.push_back(Entry); 128 } 129 130 if (LC == RTLIB::UNKNOWN_LIBCALL) 131 report_fatal_error("Unsupported library call operation!"); 132 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 133 getPointerTy(DAG.getDataLayout())); 134 135 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 136 TargetLowering::CallLoweringInfo CLI(DAG); 137 bool signExtend = shouldSignExtendTypeInLibCall(RetVT, isSigned); 138 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode()) 139 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0) 140 .setNoReturn(doesNotReturn).setDiscardResult(!isReturnValueUsed) 141 .setSExtResult(signExtend).setZExtResult(!signExtend); 142 return LowerCallTo(CLI); 143 } 144 145 /// Soften the operands of a comparison. This code is shared among BR_CC, 146 /// SELECT_CC, and SETCC handlers. 147 void TargetLowering::softenSetCCOperands(SelectionDAG &DAG, EVT VT, 148 SDValue &NewLHS, SDValue &NewRHS, 149 ISD::CondCode &CCCode, 150 const SDLoc &dl) const { 151 assert((VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128 || VT == MVT::ppcf128) 152 && "Unsupported setcc type!"); 153 154 // Expand into one or more soft-fp libcall(s). 155 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL; 156 bool ShouldInvertCC = false; 157 switch (CCCode) { 158 case ISD::SETEQ: 159 case ISD::SETOEQ: 160 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : 161 (VT == MVT::f64) ? RTLIB::OEQ_F64 : 162 (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128; 163 break; 164 case ISD::SETNE: 165 case ISD::SETUNE: 166 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : 167 (VT == MVT::f64) ? RTLIB::UNE_F64 : 168 (VT == MVT::f128) ? RTLIB::UNE_F128 : RTLIB::UNE_PPCF128; 169 break; 170 case ISD::SETGE: 171 case ISD::SETOGE: 172 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : 173 (VT == MVT::f64) ? RTLIB::OGE_F64 : 174 (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128; 175 break; 176 case ISD::SETLT: 177 case ISD::SETOLT: 178 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : 179 (VT == MVT::f64) ? RTLIB::OLT_F64 : 180 (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128; 181 break; 182 case ISD::SETLE: 183 case ISD::SETOLE: 184 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : 185 (VT == MVT::f64) ? RTLIB::OLE_F64 : 186 (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128; 187 break; 188 case ISD::SETGT: 189 case ISD::SETOGT: 190 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : 191 (VT == MVT::f64) ? RTLIB::OGT_F64 : 192 (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128; 193 break; 194 case ISD::SETUO: 195 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : 196 (VT == MVT::f64) ? RTLIB::UO_F64 : 197 (VT == MVT::f128) ? RTLIB::UO_F128 : RTLIB::UO_PPCF128; 198 break; 199 case ISD::SETO: 200 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : 201 (VT == MVT::f64) ? RTLIB::O_F64 : 202 (VT == MVT::f128) ? RTLIB::O_F128 : RTLIB::O_PPCF128; 203 break; 204 case ISD::SETONE: 205 // SETONE = SETOLT | SETOGT 206 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : 207 (VT == MVT::f64) ? RTLIB::OLT_F64 : 208 (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128; 209 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : 210 (VT == MVT::f64) ? RTLIB::OGT_F64 : 211 (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128; 212 break; 213 case ISD::SETUEQ: 214 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : 215 (VT == MVT::f64) ? RTLIB::UO_F64 : 216 (VT == MVT::f128) ? RTLIB::UO_F64 : RTLIB::UO_PPCF128; 217 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : 218 (VT == MVT::f64) ? RTLIB::OEQ_F64 : 219 (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128; 220 break; 221 default: 222 // Invert CC for unordered comparisons 223 ShouldInvertCC = true; 224 switch (CCCode) { 225 case ISD::SETULT: 226 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : 227 (VT == MVT::f64) ? RTLIB::OGE_F64 : 228 (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128; 229 break; 230 case ISD::SETULE: 231 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : 232 (VT == MVT::f64) ? RTLIB::OGT_F64 : 233 (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128; 234 break; 235 case ISD::SETUGT: 236 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : 237 (VT == MVT::f64) ? RTLIB::OLE_F64 : 238 (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128; 239 break; 240 case ISD::SETUGE: 241 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : 242 (VT == MVT::f64) ? RTLIB::OLT_F64 : 243 (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128; 244 break; 245 default: llvm_unreachable("Do not know how to soften this setcc!"); 246 } 247 } 248 249 // Use the target specific return value for comparions lib calls. 250 EVT RetVT = getCmpLibcallReturnType(); 251 SDValue Ops[2] = {NewLHS, NewRHS}; 252 NewLHS = makeLibCall(DAG, LC1, RetVT, Ops, false /*sign irrelevant*/, 253 dl).first; 254 NewRHS = DAG.getConstant(0, dl, RetVT); 255 256 CCCode = getCmpLibcallCC(LC1); 257 if (ShouldInvertCC) 258 CCCode = getSetCCInverse(CCCode, /*isInteger=*/true); 259 260 if (LC2 != RTLIB::UNKNOWN_LIBCALL) { 261 SDValue Tmp = DAG.getNode( 262 ISD::SETCC, dl, 263 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), RetVT), 264 NewLHS, NewRHS, DAG.getCondCode(CCCode)); 265 NewLHS = makeLibCall(DAG, LC2, RetVT, Ops, false/*sign irrelevant*/, 266 dl).first; 267 NewLHS = DAG.getNode( 268 ISD::SETCC, dl, 269 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), RetVT), 270 NewLHS, NewRHS, DAG.getCondCode(getCmpLibcallCC(LC2))); 271 NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS); 272 NewRHS = SDValue(); 273 } 274 } 275 276 /// Return the entry encoding for a jump table in the current function. The 277 /// returned value is a member of the MachineJumpTableInfo::JTEntryKind enum. 278 unsigned TargetLowering::getJumpTableEncoding() const { 279 // In non-pic modes, just use the address of a block. 280 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) 281 return MachineJumpTableInfo::EK_BlockAddress; 282 283 // In PIC mode, if the target supports a GPRel32 directive, use it. 284 if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != nullptr) 285 return MachineJumpTableInfo::EK_GPRel32BlockAddress; 286 287 // Otherwise, use a label difference. 288 return MachineJumpTableInfo::EK_LabelDifference32; 289 } 290 291 SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table, 292 SelectionDAG &DAG) const { 293 // If our PIC model is GP relative, use the global offset table as the base. 294 unsigned JTEncoding = getJumpTableEncoding(); 295 296 if ((JTEncoding == MachineJumpTableInfo::EK_GPRel64BlockAddress) || 297 (JTEncoding == MachineJumpTableInfo::EK_GPRel32BlockAddress)) 298 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy(DAG.getDataLayout())); 299 300 return Table; 301 } 302 303 /// This returns the relocation base for the given PIC jumptable, the same as 304 /// getPICJumpTableRelocBase, but as an MCExpr. 305 const MCExpr * 306 TargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF, 307 unsigned JTI,MCContext &Ctx) const{ 308 // The normal PIC reloc base is the label at the start of the jump table. 309 return MCSymbolRefExpr::create(MF->getJTISymbol(JTI, Ctx), Ctx); 310 } 311 312 bool 313 TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 314 // Assume that everything is safe in static mode. 315 if (getTargetMachine().getRelocationModel() == Reloc::Static) 316 return true; 317 318 // In dynamic-no-pic mode, assume that known defined values are safe. 319 if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC && 320 GA && GA->getGlobal()->isStrongDefinitionForLinker()) 321 return true; 322 323 // Otherwise assume nothing is safe. 324 return false; 325 } 326 327 //===----------------------------------------------------------------------===// 328 // Optimization Methods 329 //===----------------------------------------------------------------------===// 330 331 /// Check to see if the specified operand of the specified instruction is a 332 /// constant integer. If so, check to see if there are any bits set in the 333 /// constant that are not demanded. If so, shrink the constant and return true. 334 bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op, 335 const APInt &Demanded) { 336 SDLoc dl(Op); 337 338 // FIXME: ISD::SELECT, ISD::SELECT_CC 339 switch (Op.getOpcode()) { 340 default: break; 341 case ISD::XOR: 342 case ISD::AND: 343 case ISD::OR: { 344 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 345 if (!C) return false; 346 347 if (Op.getOpcode() == ISD::XOR && 348 (C->getAPIntValue() | (~Demanded)).isAllOnesValue()) 349 return false; 350 351 // if we can expand it to have all bits set, do it 352 if (C->getAPIntValue().intersects(~Demanded)) { 353 EVT VT = Op.getValueType(); 354 SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0), 355 DAG.getConstant(Demanded & 356 C->getAPIntValue(), 357 dl, VT)); 358 return CombineTo(Op, New); 359 } 360 361 break; 362 } 363 } 364 365 return false; 366 } 367 368 /// Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the casts are free. 369 /// This uses isZExtFree and ZERO_EXTEND for the widening cast, but it could be 370 /// generalized for targets with other types of implicit widening casts. 371 bool TargetLowering::TargetLoweringOpt::ShrinkDemandedOp(SDValue Op, 372 unsigned BitWidth, 373 const APInt &Demanded, 374 const SDLoc &dl) { 375 assert(Op.getNumOperands() == 2 && 376 "ShrinkDemandedOp only supports binary operators!"); 377 assert(Op.getNode()->getNumValues() == 1 && 378 "ShrinkDemandedOp only supports nodes with one result!"); 379 380 // Early return, as this function cannot handle vector types. 381 if (Op.getValueType().isVector()) 382 return false; 383 384 // Don't do this if the node has another user, which may require the 385 // full value. 386 if (!Op.getNode()->hasOneUse()) 387 return false; 388 389 // Search for the smallest integer type with free casts to and from 390 // Op's type. For expedience, just check power-of-2 integer types. 391 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 392 unsigned DemandedSize = BitWidth - Demanded.countLeadingZeros(); 393 unsigned SmallVTBits = DemandedSize; 394 if (!isPowerOf2_32(SmallVTBits)) 395 SmallVTBits = NextPowerOf2(SmallVTBits); 396 for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) { 397 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits); 398 if (TLI.isTruncateFree(Op.getValueType(), SmallVT) && 399 TLI.isZExtFree(SmallVT, Op.getValueType())) { 400 // We found a type with free casts. 401 SDValue X = DAG.getNode(Op.getOpcode(), dl, SmallVT, 402 DAG.getNode(ISD::TRUNCATE, dl, SmallVT, 403 Op.getNode()->getOperand(0)), 404 DAG.getNode(ISD::TRUNCATE, dl, SmallVT, 405 Op.getNode()->getOperand(1))); 406 bool NeedZext = DemandedSize > SmallVTBits; 407 SDValue Z = DAG.getNode(NeedZext ? ISD::ZERO_EXTEND : ISD::ANY_EXTEND, 408 dl, Op.getValueType(), X); 409 return CombineTo(Op, Z); 410 } 411 } 412 return false; 413 } 414 415 /// Look at Op. At this point, we know that only the DemandedMask bits of the 416 /// result of Op are ever used downstream. If we can use this information to 417 /// simplify Op, create a new simplified DAG node and return true, returning the 418 /// original and new nodes in Old and New. Otherwise, analyze the expression and 419 /// return a mask of KnownOne and KnownZero bits for the expression (used to 420 /// simplify the caller). The KnownZero/One bits may only be accurate for those 421 /// bits in the DemandedMask. 422 bool TargetLowering::SimplifyDemandedBits(SDValue Op, 423 const APInt &DemandedMask, 424 APInt &KnownZero, 425 APInt &KnownOne, 426 TargetLoweringOpt &TLO, 427 unsigned Depth) const { 428 unsigned BitWidth = DemandedMask.getBitWidth(); 429 assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth && 430 "Mask size mismatches value type size!"); 431 APInt NewMask = DemandedMask; 432 SDLoc dl(Op); 433 auto &DL = TLO.DAG.getDataLayout(); 434 435 // Don't know anything. 436 KnownZero = KnownOne = APInt(BitWidth, 0); 437 438 // Other users may use these bits. 439 if (!Op.getNode()->hasOneUse()) { 440 if (Depth != 0) { 441 // If not at the root, Just compute the KnownZero/KnownOne bits to 442 // simplify things downstream. 443 TLO.DAG.computeKnownBits(Op, KnownZero, KnownOne, Depth); 444 return false; 445 } 446 // If this is the root being simplified, allow it to have multiple uses, 447 // just set the NewMask to all bits. 448 NewMask = APInt::getAllOnesValue(BitWidth); 449 } else if (DemandedMask == 0) { 450 // Not demanding any bits from Op. 451 if (!Op.isUndef()) 452 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType())); 453 return false; 454 } else if (Depth == 6) { // Limit search depth. 455 return false; 456 } 457 458 APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut; 459 switch (Op.getOpcode()) { 460 case ISD::Constant: 461 // We know all of the bits for a constant! 462 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue(); 463 KnownZero = ~KnownOne; 464 return false; // Don't fall through, will infinitely loop. 465 case ISD::AND: 466 // If the RHS is a constant, check to see if the LHS would be zero without 467 // using the bits from the RHS. Below, we use knowledge about the RHS to 468 // simplify the LHS, here we're using information from the LHS to simplify 469 // the RHS. 470 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 471 APInt LHSZero, LHSOne; 472 // Do not increment Depth here; that can cause an infinite loop. 473 TLO.DAG.computeKnownBits(Op.getOperand(0), LHSZero, LHSOne, Depth); 474 // If the LHS already has zeros where RHSC does, this and is dead. 475 if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask)) 476 return TLO.CombineTo(Op, Op.getOperand(0)); 477 // If any of the set bits in the RHS are known zero on the LHS, shrink 478 // the constant. 479 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask)) 480 return true; 481 } 482 483 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero, 484 KnownOne, TLO, Depth+1)) 485 return true; 486 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 487 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask, 488 KnownZero2, KnownOne2, TLO, Depth+1)) 489 return true; 490 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 491 492 // If all of the demanded bits are known one on one side, return the other. 493 // These bits cannot contribute to the result of the 'and'. 494 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask)) 495 return TLO.CombineTo(Op, Op.getOperand(0)); 496 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask)) 497 return TLO.CombineTo(Op, Op.getOperand(1)); 498 // If all of the demanded bits in the inputs are known zeros, return zero. 499 if ((NewMask & (KnownZero|KnownZero2)) == NewMask) 500 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, dl, Op.getValueType())); 501 // If the RHS is a constant, see if we can simplify it. 502 if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask)) 503 return true; 504 // If the operation can be done in a smaller type, do so. 505 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl)) 506 return true; 507 508 // Output known-1 bits are only known if set in both the LHS & RHS. 509 KnownOne &= KnownOne2; 510 // Output known-0 are known to be clear if zero in either the LHS | RHS. 511 KnownZero |= KnownZero2; 512 break; 513 case ISD::OR: 514 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero, 515 KnownOne, TLO, Depth+1)) 516 return true; 517 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 518 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask, 519 KnownZero2, KnownOne2, TLO, Depth+1)) 520 return true; 521 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 522 523 // If all of the demanded bits are known zero on one side, return the other. 524 // These bits cannot contribute to the result of the 'or'. 525 if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask)) 526 return TLO.CombineTo(Op, Op.getOperand(0)); 527 if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask)) 528 return TLO.CombineTo(Op, Op.getOperand(1)); 529 // If all of the potentially set bits on one side are known to be set on 530 // the other side, just use the 'other' side. 531 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask)) 532 return TLO.CombineTo(Op, Op.getOperand(0)); 533 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask)) 534 return TLO.CombineTo(Op, Op.getOperand(1)); 535 // If the RHS is a constant, see if we can simplify it. 536 if (TLO.ShrinkDemandedConstant(Op, NewMask)) 537 return true; 538 // If the operation can be done in a smaller type, do so. 539 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl)) 540 return true; 541 542 // Output known-0 bits are only known if clear in both the LHS & RHS. 543 KnownZero &= KnownZero2; 544 // Output known-1 are known to be set if set in either the LHS | RHS. 545 KnownOne |= KnownOne2; 546 break; 547 case ISD::XOR: 548 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero, 549 KnownOne, TLO, Depth+1)) 550 return true; 551 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 552 if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2, 553 KnownOne2, TLO, Depth+1)) 554 return true; 555 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 556 557 // If all of the demanded bits are known zero on one side, return the other. 558 // These bits cannot contribute to the result of the 'xor'. 559 if ((KnownZero & NewMask) == NewMask) 560 return TLO.CombineTo(Op, Op.getOperand(0)); 561 if ((KnownZero2 & NewMask) == NewMask) 562 return TLO.CombineTo(Op, Op.getOperand(1)); 563 // If the operation can be done in a smaller type, do so. 564 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl)) 565 return true; 566 567 // If all of the unknown bits are known to be zero on one side or the other 568 // (but not both) turn this into an *inclusive* or. 569 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 570 if ((NewMask & ~KnownZero & ~KnownZero2) == 0) 571 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(), 572 Op.getOperand(0), 573 Op.getOperand(1))); 574 575 // Output known-0 bits are known if clear or set in both the LHS & RHS. 576 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); 577 // Output known-1 are known to be set if set in only one of the LHS, RHS. 578 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); 579 580 // If all of the demanded bits on one side are known, and all of the set 581 // bits on that side are also known to be set on the other side, turn this 582 // into an AND, as we know the bits will be cleared. 583 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 584 // NB: it is okay if more bits are known than are requested 585 if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known on one side 586 if (KnownOne == KnownOne2) { // set bits are the same on both sides 587 EVT VT = Op.getValueType(); 588 SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, dl, VT); 589 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT, 590 Op.getOperand(0), ANDC)); 591 } 592 } 593 594 // If the RHS is a constant, see if we can simplify it. 595 // for XOR, we prefer to force bits to 1 if they will make a -1. 596 // if we can't force bits, try to shrink constant 597 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 598 APInt Expanded = C->getAPIntValue() | (~NewMask); 599 // if we can expand it to have all bits set, do it 600 if (Expanded.isAllOnesValue()) { 601 if (Expanded != C->getAPIntValue()) { 602 EVT VT = Op.getValueType(); 603 SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0), 604 TLO.DAG.getConstant(Expanded, dl, VT)); 605 return TLO.CombineTo(Op, New); 606 } 607 // if it already has all the bits set, nothing to change 608 // but don't shrink either! 609 } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) { 610 return true; 611 } 612 } 613 614 KnownZero = KnownZeroOut; 615 KnownOne = KnownOneOut; 616 break; 617 case ISD::SELECT: 618 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero, 619 KnownOne, TLO, Depth+1)) 620 return true; 621 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2, 622 KnownOne2, TLO, Depth+1)) 623 return true; 624 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 625 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 626 627 // If the operands are constants, see if we can simplify them. 628 if (TLO.ShrinkDemandedConstant(Op, NewMask)) 629 return true; 630 631 // Only known if known in both the LHS and RHS. 632 KnownOne &= KnownOne2; 633 KnownZero &= KnownZero2; 634 break; 635 case ISD::SELECT_CC: 636 if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero, 637 KnownOne, TLO, Depth+1)) 638 return true; 639 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2, 640 KnownOne2, TLO, Depth+1)) 641 return true; 642 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 643 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 644 645 // If the operands are constants, see if we can simplify them. 646 if (TLO.ShrinkDemandedConstant(Op, NewMask)) 647 return true; 648 649 // Only known if known in both the LHS and RHS. 650 KnownOne &= KnownOne2; 651 KnownZero &= KnownZero2; 652 break; 653 case ISD::SHL: 654 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 655 unsigned ShAmt = SA->getZExtValue(); 656 SDValue InOp = Op.getOperand(0); 657 658 // If the shift count is an invalid immediate, don't do anything. 659 if (ShAmt >= BitWidth) 660 break; 661 662 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a 663 // single shift. We can do this if the bottom bits (which are shifted 664 // out) are never demanded. 665 if (InOp.getOpcode() == ISD::SRL && 666 isa<ConstantSDNode>(InOp.getOperand(1))) { 667 if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) { 668 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue(); 669 unsigned Opc = ISD::SHL; 670 int Diff = ShAmt-C1; 671 if (Diff < 0) { 672 Diff = -Diff; 673 Opc = ISD::SRL; 674 } 675 676 SDValue NewSA = 677 TLO.DAG.getConstant(Diff, dl, Op.getOperand(1).getValueType()); 678 EVT VT = Op.getValueType(); 679 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT, 680 InOp.getOperand(0), NewSA)); 681 } 682 } 683 684 if (SimplifyDemandedBits(InOp, NewMask.lshr(ShAmt), 685 KnownZero, KnownOne, TLO, Depth+1)) 686 return true; 687 688 // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits 689 // are not demanded. This will likely allow the anyext to be folded away. 690 if (InOp.getNode()->getOpcode() == ISD::ANY_EXTEND) { 691 SDValue InnerOp = InOp.getNode()->getOperand(0); 692 EVT InnerVT = InnerOp.getValueType(); 693 unsigned InnerBits = InnerVT.getSizeInBits(); 694 if (ShAmt < InnerBits && NewMask.lshr(InnerBits) == 0 && 695 isTypeDesirableForOp(ISD::SHL, InnerVT)) { 696 EVT ShTy = getShiftAmountTy(InnerVT, DL); 697 if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits())) 698 ShTy = InnerVT; 699 SDValue NarrowShl = 700 TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp, 701 TLO.DAG.getConstant(ShAmt, dl, ShTy)); 702 return 703 TLO.CombineTo(Op, 704 TLO.DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), 705 NarrowShl)); 706 } 707 // Repeat the SHL optimization above in cases where an extension 708 // intervenes: (shl (anyext (shr x, c1)), c2) to 709 // (shl (anyext x), c2-c1). This requires that the bottom c1 bits 710 // aren't demanded (as above) and that the shifted upper c1 bits of 711 // x aren't demanded. 712 if (InOp.hasOneUse() && 713 InnerOp.getOpcode() == ISD::SRL && 714 InnerOp.hasOneUse() && 715 isa<ConstantSDNode>(InnerOp.getOperand(1))) { 716 uint64_t InnerShAmt = cast<ConstantSDNode>(InnerOp.getOperand(1)) 717 ->getZExtValue(); 718 if (InnerShAmt < ShAmt && 719 InnerShAmt < InnerBits && 720 NewMask.lshr(InnerBits - InnerShAmt + ShAmt) == 0 && 721 NewMask.trunc(ShAmt) == 0) { 722 SDValue NewSA = 723 TLO.DAG.getConstant(ShAmt - InnerShAmt, dl, 724 Op.getOperand(1).getValueType()); 725 EVT VT = Op.getValueType(); 726 SDValue NewExt = TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT, 727 InnerOp.getOperand(0)); 728 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl, VT, 729 NewExt, NewSA)); 730 } 731 } 732 } 733 734 KnownZero <<= SA->getZExtValue(); 735 KnownOne <<= SA->getZExtValue(); 736 // low bits known zero. 737 KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue()); 738 } 739 break; 740 case ISD::SRL: 741 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 742 EVT VT = Op.getValueType(); 743 unsigned ShAmt = SA->getZExtValue(); 744 unsigned VTSize = VT.getSizeInBits(); 745 SDValue InOp = Op.getOperand(0); 746 747 // If the shift count is an invalid immediate, don't do anything. 748 if (ShAmt >= BitWidth) 749 break; 750 751 APInt InDemandedMask = (NewMask << ShAmt); 752 753 // If the shift is exact, then it does demand the low bits (and knows that 754 // they are zero). 755 if (cast<BinaryWithFlagsSDNode>(Op)->Flags.hasExact()) 756 InDemandedMask |= APInt::getLowBitsSet(BitWidth, ShAmt); 757 758 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a 759 // single shift. We can do this if the top bits (which are shifted out) 760 // are never demanded. 761 if (InOp.getOpcode() == ISD::SHL && 762 isa<ConstantSDNode>(InOp.getOperand(1))) { 763 if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) { 764 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue(); 765 unsigned Opc = ISD::SRL; 766 int Diff = ShAmt-C1; 767 if (Diff < 0) { 768 Diff = -Diff; 769 Opc = ISD::SHL; 770 } 771 772 SDValue NewSA = 773 TLO.DAG.getConstant(Diff, dl, Op.getOperand(1).getValueType()); 774 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT, 775 InOp.getOperand(0), NewSA)); 776 } 777 } 778 779 // Compute the new bits that are at the top now. 780 if (SimplifyDemandedBits(InOp, InDemandedMask, 781 KnownZero, KnownOne, TLO, Depth+1)) 782 return true; 783 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 784 KnownZero = KnownZero.lshr(ShAmt); 785 KnownOne = KnownOne.lshr(ShAmt); 786 787 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt); 788 KnownZero |= HighBits; // High bits known zero. 789 } 790 break; 791 case ISD::SRA: 792 // If this is an arithmetic shift right and only the low-bit is set, we can 793 // always convert this into a logical shr, even if the shift amount is 794 // variable. The low bit of the shift cannot be an input sign bit unless 795 // the shift amount is >= the size of the datatype, which is undefined. 796 if (NewMask == 1) 797 return TLO.CombineTo(Op, 798 TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(), 799 Op.getOperand(0), Op.getOperand(1))); 800 801 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 802 EVT VT = Op.getValueType(); 803 unsigned ShAmt = SA->getZExtValue(); 804 805 // If the shift count is an invalid immediate, don't do anything. 806 if (ShAmt >= BitWidth) 807 break; 808 809 APInt InDemandedMask = (NewMask << ShAmt); 810 811 // If the shift is exact, then it does demand the low bits (and knows that 812 // they are zero). 813 if (cast<BinaryWithFlagsSDNode>(Op)->Flags.hasExact()) 814 InDemandedMask |= APInt::getLowBitsSet(BitWidth, ShAmt); 815 816 // If any of the demanded bits are produced by the sign extension, we also 817 // demand the input sign bit. 818 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt); 819 if (HighBits.intersects(NewMask)) 820 InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits()); 821 822 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask, 823 KnownZero, KnownOne, TLO, Depth+1)) 824 return true; 825 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 826 KnownZero = KnownZero.lshr(ShAmt); 827 KnownOne = KnownOne.lshr(ShAmt); 828 829 // Handle the sign bit, adjusted to where it is now in the mask. 830 APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt); 831 832 // If the input sign bit is known to be zero, or if none of the top bits 833 // are demanded, turn this into an unsigned shift right. 834 if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) { 835 SDNodeFlags Flags; 836 Flags.setExact(cast<BinaryWithFlagsSDNode>(Op)->Flags.hasExact()); 837 return TLO.CombineTo(Op, 838 TLO.DAG.getNode(ISD::SRL, dl, VT, Op.getOperand(0), 839 Op.getOperand(1), &Flags)); 840 } 841 842 int Log2 = NewMask.exactLogBase2(); 843 if (Log2 >= 0) { 844 // The bit must come from the sign. 845 SDValue NewSA = 846 TLO.DAG.getConstant(BitWidth - 1 - Log2, dl, 847 Op.getOperand(1).getValueType()); 848 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT, 849 Op.getOperand(0), NewSA)); 850 } 851 852 if (KnownOne.intersects(SignBit)) 853 // New bits are known one. 854 KnownOne |= HighBits; 855 } 856 break; 857 case ISD::SIGN_EXTEND_INREG: { 858 EVT ExVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 859 860 APInt MsbMask = APInt::getHighBitsSet(BitWidth, 1); 861 // If we only care about the highest bit, don't bother shifting right. 862 if (MsbMask == NewMask) { 863 unsigned ShAmt = ExVT.getScalarType().getSizeInBits(); 864 SDValue InOp = Op.getOperand(0); 865 unsigned VTBits = Op->getValueType(0).getScalarType().getSizeInBits(); 866 bool AlreadySignExtended = 867 TLO.DAG.ComputeNumSignBits(InOp) >= VTBits-ShAmt+1; 868 // However if the input is already sign extended we expect the sign 869 // extension to be dropped altogether later and do not simplify. 870 if (!AlreadySignExtended) { 871 // Compute the correct shift amount type, which must be getShiftAmountTy 872 // for scalar types after legalization. 873 EVT ShiftAmtTy = Op.getValueType(); 874 if (TLO.LegalTypes() && !ShiftAmtTy.isVector()) 875 ShiftAmtTy = getShiftAmountTy(ShiftAmtTy, DL); 876 877 SDValue ShiftAmt = TLO.DAG.getConstant(BitWidth - ShAmt, dl, 878 ShiftAmtTy); 879 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl, 880 Op.getValueType(), InOp, 881 ShiftAmt)); 882 } 883 } 884 885 // Sign extension. Compute the demanded bits in the result that are not 886 // present in the input. 887 APInt NewBits = 888 APInt::getHighBitsSet(BitWidth, 889 BitWidth - ExVT.getScalarType().getSizeInBits()); 890 891 // If none of the extended bits are demanded, eliminate the sextinreg. 892 if ((NewBits & NewMask) == 0) 893 return TLO.CombineTo(Op, Op.getOperand(0)); 894 895 APInt InSignBit = 896 APInt::getSignBit(ExVT.getScalarType().getSizeInBits()).zext(BitWidth); 897 APInt InputDemandedBits = 898 APInt::getLowBitsSet(BitWidth, 899 ExVT.getScalarType().getSizeInBits()) & 900 NewMask; 901 902 // Since the sign extended bits are demanded, we know that the sign 903 // bit is demanded. 904 InputDemandedBits |= InSignBit; 905 906 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits, 907 KnownZero, KnownOne, TLO, Depth+1)) 908 return true; 909 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 910 911 // If the sign bit of the input is known set or clear, then we know the 912 // top bits of the result. 913 914 // If the input sign bit is known zero, convert this into a zero extension. 915 if (KnownZero.intersects(InSignBit)) 916 return TLO.CombineTo(Op, 917 TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,ExVT)); 918 919 if (KnownOne.intersects(InSignBit)) { // Input sign bit known set 920 KnownOne |= NewBits; 921 KnownZero &= ~NewBits; 922 } else { // Input sign bit unknown 923 KnownZero &= ~NewBits; 924 KnownOne &= ~NewBits; 925 } 926 break; 927 } 928 case ISD::BUILD_PAIR: { 929 EVT HalfVT = Op.getOperand(0).getValueType(); 930 unsigned HalfBitWidth = HalfVT.getScalarSizeInBits(); 931 932 APInt MaskLo = NewMask.getLoBits(HalfBitWidth).trunc(HalfBitWidth); 933 APInt MaskHi = NewMask.getHiBits(HalfBitWidth).trunc(HalfBitWidth); 934 935 APInt KnownZeroLo, KnownOneLo; 936 APInt KnownZeroHi, KnownOneHi; 937 938 if (SimplifyDemandedBits(Op.getOperand(0), MaskLo, KnownZeroLo, 939 KnownOneLo, TLO, Depth + 1)) 940 return true; 941 942 if (SimplifyDemandedBits(Op.getOperand(1), MaskHi, KnownZeroHi, 943 KnownOneHi, TLO, Depth + 1)) 944 return true; 945 946 KnownZero = KnownZeroLo.zext(BitWidth) | 947 KnownZeroHi.zext(BitWidth).shl(HalfBitWidth); 948 949 KnownOne = KnownOneLo.zext(BitWidth) | 950 KnownOneHi.zext(BitWidth).shl(HalfBitWidth); 951 break; 952 } 953 case ISD::ZERO_EXTEND: { 954 unsigned OperandBitWidth = 955 Op.getOperand(0).getValueType().getScalarType().getSizeInBits(); 956 APInt InMask = NewMask.trunc(OperandBitWidth); 957 958 // If none of the top bits are demanded, convert this into an any_extend. 959 APInt NewBits = 960 APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask; 961 if (!NewBits.intersects(NewMask)) 962 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl, 963 Op.getValueType(), 964 Op.getOperand(0))); 965 966 if (SimplifyDemandedBits(Op.getOperand(0), InMask, 967 KnownZero, KnownOne, TLO, Depth+1)) 968 return true; 969 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 970 KnownZero = KnownZero.zext(BitWidth); 971 KnownOne = KnownOne.zext(BitWidth); 972 KnownZero |= NewBits; 973 break; 974 } 975 case ISD::SIGN_EXTEND: { 976 EVT InVT = Op.getOperand(0).getValueType(); 977 unsigned InBits = InVT.getScalarType().getSizeInBits(); 978 APInt InMask = APInt::getLowBitsSet(BitWidth, InBits); 979 APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits); 980 APInt NewBits = ~InMask & NewMask; 981 982 // If none of the top bits are demanded, convert this into an any_extend. 983 if (NewBits == 0) 984 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl, 985 Op.getValueType(), 986 Op.getOperand(0))); 987 988 // Since some of the sign extended bits are demanded, we know that the sign 989 // bit is demanded. 990 APInt InDemandedBits = InMask & NewMask; 991 InDemandedBits |= InSignBit; 992 InDemandedBits = InDemandedBits.trunc(InBits); 993 994 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero, 995 KnownOne, TLO, Depth+1)) 996 return true; 997 KnownZero = KnownZero.zext(BitWidth); 998 KnownOne = KnownOne.zext(BitWidth); 999 1000 // If the sign bit is known zero, convert this to a zero extend. 1001 if (KnownZero.intersects(InSignBit)) 1002 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, 1003 Op.getValueType(), 1004 Op.getOperand(0))); 1005 1006 // If the sign bit is known one, the top bits match. 1007 if (KnownOne.intersects(InSignBit)) { 1008 KnownOne |= NewBits; 1009 assert((KnownZero & NewBits) == 0); 1010 } else { // Otherwise, top bits aren't known. 1011 assert((KnownOne & NewBits) == 0); 1012 assert((KnownZero & NewBits) == 0); 1013 } 1014 break; 1015 } 1016 case ISD::ANY_EXTEND: { 1017 unsigned OperandBitWidth = 1018 Op.getOperand(0).getValueType().getScalarType().getSizeInBits(); 1019 APInt InMask = NewMask.trunc(OperandBitWidth); 1020 if (SimplifyDemandedBits(Op.getOperand(0), InMask, 1021 KnownZero, KnownOne, TLO, Depth+1)) 1022 return true; 1023 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1024 KnownZero = KnownZero.zext(BitWidth); 1025 KnownOne = KnownOne.zext(BitWidth); 1026 break; 1027 } 1028 case ISD::TRUNCATE: { 1029 // Simplify the input, using demanded bit information, and compute the known 1030 // zero/one bits live out. 1031 unsigned OperandBitWidth = 1032 Op.getOperand(0).getValueType().getScalarType().getSizeInBits(); 1033 APInt TruncMask = NewMask.zext(OperandBitWidth); 1034 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask, 1035 KnownZero, KnownOne, TLO, Depth+1)) 1036 return true; 1037 KnownZero = KnownZero.trunc(BitWidth); 1038 KnownOne = KnownOne.trunc(BitWidth); 1039 1040 // If the input is only used by this truncate, see if we can shrink it based 1041 // on the known demanded bits. 1042 if (Op.getOperand(0).getNode()->hasOneUse()) { 1043 SDValue In = Op.getOperand(0); 1044 switch (In.getOpcode()) { 1045 default: break; 1046 case ISD::SRL: 1047 // Shrink SRL by a constant if none of the high bits shifted in are 1048 // demanded. 1049 if (TLO.LegalTypes() && 1050 !isTypeDesirableForOp(ISD::SRL, Op.getValueType())) 1051 // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is 1052 // undesirable. 1053 break; 1054 ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1)); 1055 if (!ShAmt) 1056 break; 1057 SDValue Shift = In.getOperand(1); 1058 if (TLO.LegalTypes()) { 1059 uint64_t ShVal = ShAmt->getZExtValue(); 1060 Shift = TLO.DAG.getConstant(ShVal, dl, 1061 getShiftAmountTy(Op.getValueType(), DL)); 1062 } 1063 1064 APInt HighBits = APInt::getHighBitsSet(OperandBitWidth, 1065 OperandBitWidth - BitWidth); 1066 HighBits = HighBits.lshr(ShAmt->getZExtValue()).trunc(BitWidth); 1067 1068 if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) { 1069 // None of the shifted in bits are needed. Add a truncate of the 1070 // shift input, then shift it. 1071 SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl, 1072 Op.getValueType(), 1073 In.getOperand(0)); 1074 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, 1075 Op.getValueType(), 1076 NewTrunc, 1077 Shift)); 1078 } 1079 break; 1080 } 1081 } 1082 1083 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1084 break; 1085 } 1086 case ISD::AssertZext: { 1087 // AssertZext demands all of the high bits, plus any of the low bits 1088 // demanded by its users. 1089 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 1090 APInt InMask = APInt::getLowBitsSet(BitWidth, 1091 VT.getSizeInBits()); 1092 if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | NewMask, 1093 KnownZero, KnownOne, TLO, Depth+1)) 1094 return true; 1095 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 1096 1097 KnownZero |= ~InMask & NewMask; 1098 break; 1099 } 1100 case ISD::BITCAST: 1101 // If this is an FP->Int bitcast and if the sign bit is the only 1102 // thing demanded, turn this into a FGETSIGN. 1103 if (!TLO.LegalOperations() && 1104 !Op.getValueType().isVector() && 1105 !Op.getOperand(0).getValueType().isVector() && 1106 NewMask == APInt::getSignBit(Op.getValueType().getSizeInBits()) && 1107 Op.getOperand(0).getValueType().isFloatingPoint()) { 1108 bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, Op.getValueType()); 1109 bool i32Legal = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32); 1110 if ((OpVTLegal || i32Legal) && Op.getValueType().isSimple() && 1111 Op.getOperand(0).getValueType() != MVT::f128) { 1112 // Cannot eliminate/lower SHL for f128 yet. 1113 EVT Ty = OpVTLegal ? Op.getValueType() : MVT::i32; 1114 // Make a FGETSIGN + SHL to move the sign bit into the appropriate 1115 // place. We expect the SHL to be eliminated by other optimizations. 1116 SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Op.getOperand(0)); 1117 unsigned OpVTSizeInBits = Op.getValueType().getSizeInBits(); 1118 if (!OpVTLegal && OpVTSizeInBits > 32) 1119 Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), Sign); 1120 unsigned ShVal = Op.getValueType().getSizeInBits()-1; 1121 SDValue ShAmt = TLO.DAG.getConstant(ShVal, dl, Op.getValueType()); 1122 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl, 1123 Op.getValueType(), 1124 Sign, ShAmt)); 1125 } 1126 } 1127 break; 1128 case ISD::ADD: 1129 case ISD::MUL: 1130 case ISD::SUB: { 1131 // Add, Sub, and Mul don't demand any bits in positions beyond that 1132 // of the highest bit demanded of them. 1133 APInt LoMask = APInt::getLowBitsSet(BitWidth, 1134 BitWidth - NewMask.countLeadingZeros()); 1135 if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2, 1136 KnownOne2, TLO, Depth+1)) 1137 return true; 1138 if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2, 1139 KnownOne2, TLO, Depth+1)) 1140 return true; 1141 // See if the operation should be performed at a smaller bit width. 1142 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl)) 1143 return true; 1144 } 1145 // FALL THROUGH 1146 default: 1147 // Just use computeKnownBits to compute output bits. 1148 TLO.DAG.computeKnownBits(Op, KnownZero, KnownOne, Depth); 1149 break; 1150 } 1151 1152 // If we know the value of all of the demanded bits, return this as a 1153 // constant. 1154 if ((NewMask & (KnownZero|KnownOne)) == NewMask) { 1155 // Avoid folding to a constant if any OpaqueConstant is involved. 1156 const SDNode *N = Op.getNode(); 1157 for (SDNodeIterator I = SDNodeIterator::begin(N), 1158 E = SDNodeIterator::end(N); I != E; ++I) { 1159 SDNode *Op = *I; 1160 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) 1161 if (C->isOpaque()) 1162 return false; 1163 } 1164 return TLO.CombineTo(Op, 1165 TLO.DAG.getConstant(KnownOne, dl, Op.getValueType())); 1166 } 1167 1168 return false; 1169 } 1170 1171 /// Determine which of the bits specified in Mask are known to be either zero or 1172 /// one and return them in the KnownZero/KnownOne bitsets. 1173 void TargetLowering::computeKnownBitsForTargetNode(const SDValue Op, 1174 APInt &KnownZero, 1175 APInt &KnownOne, 1176 const SelectionDAG &DAG, 1177 unsigned Depth) const { 1178 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END || 1179 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 1180 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 1181 Op.getOpcode() == ISD::INTRINSIC_VOID) && 1182 "Should use MaskedValueIsZero if you don't know whether Op" 1183 " is a target node!"); 1184 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); 1185 } 1186 1187 /// This method can be implemented by targets that want to expose additional 1188 /// information about sign bits to the DAG Combiner. 1189 unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op, 1190 const SelectionDAG &, 1191 unsigned Depth) const { 1192 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END || 1193 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 1194 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 1195 Op.getOpcode() == ISD::INTRINSIC_VOID) && 1196 "Should use ComputeNumSignBits if you don't know whether Op" 1197 " is a target node!"); 1198 return 1; 1199 } 1200 1201 bool TargetLowering::isConstTrueVal(const SDNode *N) const { 1202 if (!N) 1203 return false; 1204 1205 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N); 1206 if (!CN) { 1207 const BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N); 1208 if (!BV) 1209 return false; 1210 1211 BitVector UndefElements; 1212 CN = BV->getConstantSplatNode(&UndefElements); 1213 // Only interested in constant splats, and we don't try to handle undef 1214 // elements in identifying boolean constants. 1215 if (!CN || UndefElements.none()) 1216 return false; 1217 } 1218 1219 switch (getBooleanContents(N->getValueType(0))) { 1220 case UndefinedBooleanContent: 1221 return CN->getAPIntValue()[0]; 1222 case ZeroOrOneBooleanContent: 1223 return CN->isOne(); 1224 case ZeroOrNegativeOneBooleanContent: 1225 return CN->isAllOnesValue(); 1226 } 1227 1228 llvm_unreachable("Invalid boolean contents"); 1229 } 1230 1231 bool TargetLowering::isConstFalseVal(const SDNode *N) const { 1232 if (!N) 1233 return false; 1234 1235 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N); 1236 if (!CN) { 1237 const BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N); 1238 if (!BV) 1239 return false; 1240 1241 BitVector UndefElements; 1242 CN = BV->getConstantSplatNode(&UndefElements); 1243 // Only interested in constant splats, and we don't try to handle undef 1244 // elements in identifying boolean constants. 1245 if (!CN || UndefElements.none()) 1246 return false; 1247 } 1248 1249 if (getBooleanContents(N->getValueType(0)) == UndefinedBooleanContent) 1250 return !CN->getAPIntValue()[0]; 1251 1252 return CN->isNullValue(); 1253 } 1254 1255 bool TargetLowering::isExtendedTrueVal(const ConstantSDNode *N, EVT VT, 1256 bool SExt) const { 1257 if (VT == MVT::i1) 1258 return N->isOne(); 1259 1260 TargetLowering::BooleanContent Cnt = getBooleanContents(VT); 1261 switch (Cnt) { 1262 case TargetLowering::ZeroOrOneBooleanContent: 1263 // An extended value of 1 is always true, unless its original type is i1, 1264 // in which case it will be sign extended to -1. 1265 return (N->isOne() && !SExt) || (SExt && (N->getValueType(0) != MVT::i1)); 1266 case TargetLowering::UndefinedBooleanContent: 1267 case TargetLowering::ZeroOrNegativeOneBooleanContent: 1268 return N->isAllOnesValue() && SExt; 1269 } 1270 llvm_unreachable("Unexpected enumeration."); 1271 } 1272 1273 /// This helper function of SimplifySetCC tries to optimize the comparison when 1274 /// either operand of the SetCC node is a bitwise-and instruction. 1275 SDValue TargetLowering::simplifySetCCWithAnd(EVT VT, SDValue N0, SDValue N1, 1276 ISD::CondCode Cond, 1277 DAGCombinerInfo &DCI, 1278 const SDLoc &DL) const { 1279 // Match these patterns in any of their permutations: 1280 // (X & Y) == Y 1281 // (X & Y) != Y 1282 if (N1.getOpcode() == ISD::AND && N0.getOpcode() != ISD::AND) 1283 std::swap(N0, N1); 1284 1285 EVT OpVT = N0.getValueType(); 1286 if (N0.getOpcode() != ISD::AND || !OpVT.isInteger() || 1287 (Cond != ISD::SETEQ && Cond != ISD::SETNE)) 1288 return SDValue(); 1289 1290 SDValue X, Y; 1291 if (N0.getOperand(0) == N1) { 1292 X = N0.getOperand(1); 1293 Y = N0.getOperand(0); 1294 } else if (N0.getOperand(1) == N1) { 1295 X = N0.getOperand(0); 1296 Y = N0.getOperand(1); 1297 } else { 1298 return SDValue(); 1299 } 1300 1301 SelectionDAG &DAG = DCI.DAG; 1302 SDValue Zero = DAG.getConstant(0, DL, OpVT); 1303 if (DAG.isKnownToBeAPowerOfTwo(Y)) { 1304 // Simplify X & Y == Y to X & Y != 0 if Y has exactly one bit set. 1305 // Note that where Y is variable and is known to have at most one bit set 1306 // (for example, if it is Z & 1) we cannot do this; the expressions are not 1307 // equivalent when Y == 0. 1308 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true); 1309 if (DCI.isBeforeLegalizeOps() || 1310 isCondCodeLegal(Cond, N0.getSimpleValueType())) 1311 return DAG.getSetCC(DL, VT, N0, Zero, Cond); 1312 } else if (N0.hasOneUse() && hasAndNotCompare(Y)) { 1313 // If the target supports an 'and-not' or 'and-complement' logic operation, 1314 // try to use that to make a comparison operation more efficient. 1315 // But don't do this transform if the mask is a single bit because there are 1316 // more efficient ways to deal with that case (for example, 'bt' on x86 or 1317 // 'rlwinm' on PPC). 1318 1319 // Bail out if the compare operand that we want to turn into a zero is 1320 // already a zero (otherwise, infinite loop). 1321 auto *YConst = dyn_cast<ConstantSDNode>(Y); 1322 if (YConst && YConst->isNullValue()) 1323 return SDValue(); 1324 1325 // Transform this into: ~X & Y == 0. 1326 SDValue NotX = DAG.getNOT(SDLoc(X), X, OpVT); 1327 SDValue NewAnd = DAG.getNode(ISD::AND, SDLoc(N0), OpVT, NotX, Y); 1328 return DAG.getSetCC(DL, VT, NewAnd, Zero, Cond); 1329 } 1330 1331 return SDValue(); 1332 } 1333 1334 /// Try to simplify a setcc built with the specified operands and cc. If it is 1335 /// unable to simplify it, return a null SDValue. 1336 SDValue TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1, 1337 ISD::CondCode Cond, bool foldBooleans, 1338 DAGCombinerInfo &DCI, 1339 const SDLoc &dl) const { 1340 SelectionDAG &DAG = DCI.DAG; 1341 1342 // These setcc operations always fold. 1343 switch (Cond) { 1344 default: break; 1345 case ISD::SETFALSE: 1346 case ISD::SETFALSE2: return DAG.getConstant(0, dl, VT); 1347 case ISD::SETTRUE: 1348 case ISD::SETTRUE2: { 1349 TargetLowering::BooleanContent Cnt = 1350 getBooleanContents(N0->getValueType(0)); 1351 return DAG.getConstant( 1352 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, dl, 1353 VT); 1354 } 1355 } 1356 1357 // Ensure that the constant occurs on the RHS, and fold constant 1358 // comparisons. 1359 ISD::CondCode SwappedCC = ISD::getSetCCSwappedOperands(Cond); 1360 if (isa<ConstantSDNode>(N0.getNode()) && 1361 (DCI.isBeforeLegalizeOps() || 1362 isCondCodeLegal(SwappedCC, N0.getSimpleValueType()))) 1363 return DAG.getSetCC(dl, VT, N1, N0, SwappedCC); 1364 1365 if (auto *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) { 1366 const APInt &C1 = N1C->getAPIntValue(); 1367 1368 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an 1369 // equality comparison, then we're just comparing whether X itself is 1370 // zero. 1371 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) && 1372 N0.getOperand(0).getOpcode() == ISD::CTLZ && 1373 N0.getOperand(1).getOpcode() == ISD::Constant) { 1374 const APInt &ShAmt 1375 = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); 1376 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 1377 ShAmt == Log2_32(N0.getValueType().getSizeInBits())) { 1378 if ((C1 == 0) == (Cond == ISD::SETEQ)) { 1379 // (srl (ctlz x), 5) == 0 -> X != 0 1380 // (srl (ctlz x), 5) != 1 -> X != 0 1381 Cond = ISD::SETNE; 1382 } else { 1383 // (srl (ctlz x), 5) != 0 -> X == 0 1384 // (srl (ctlz x), 5) == 1 -> X == 0 1385 Cond = ISD::SETEQ; 1386 } 1387 SDValue Zero = DAG.getConstant(0, dl, N0.getValueType()); 1388 return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0), 1389 Zero, Cond); 1390 } 1391 } 1392 1393 SDValue CTPOP = N0; 1394 // Look through truncs that don't change the value of a ctpop. 1395 if (N0.hasOneUse() && N0.getOpcode() == ISD::TRUNCATE) 1396 CTPOP = N0.getOperand(0); 1397 1398 if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP && 1399 (N0 == CTPOP || N0.getValueType().getSizeInBits() > 1400 Log2_32_Ceil(CTPOP.getValueType().getSizeInBits()))) { 1401 EVT CTVT = CTPOP.getValueType(); 1402 SDValue CTOp = CTPOP.getOperand(0); 1403 1404 // (ctpop x) u< 2 -> (x & x-1) == 0 1405 // (ctpop x) u> 1 -> (x & x-1) != 0 1406 if ((Cond == ISD::SETULT && C1 == 2) || (Cond == ISD::SETUGT && C1 == 1)){ 1407 SDValue Sub = DAG.getNode(ISD::SUB, dl, CTVT, CTOp, 1408 DAG.getConstant(1, dl, CTVT)); 1409 SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Sub); 1410 ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE; 1411 return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, dl, CTVT), CC); 1412 } 1413 1414 // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal. 1415 } 1416 1417 // (zext x) == C --> x == (trunc C) 1418 // (sext x) == C --> x == (trunc C) 1419 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 1420 DCI.isBeforeLegalize() && N0->hasOneUse()) { 1421 unsigned MinBits = N0.getValueSizeInBits(); 1422 SDValue PreExt; 1423 bool Signed = false; 1424 if (N0->getOpcode() == ISD::ZERO_EXTEND) { 1425 // ZExt 1426 MinBits = N0->getOperand(0).getValueSizeInBits(); 1427 PreExt = N0->getOperand(0); 1428 } else if (N0->getOpcode() == ISD::AND) { 1429 // DAGCombine turns costly ZExts into ANDs 1430 if (auto *C = dyn_cast<ConstantSDNode>(N0->getOperand(1))) 1431 if ((C->getAPIntValue()+1).isPowerOf2()) { 1432 MinBits = C->getAPIntValue().countTrailingOnes(); 1433 PreExt = N0->getOperand(0); 1434 } 1435 } else if (N0->getOpcode() == ISD::SIGN_EXTEND) { 1436 // SExt 1437 MinBits = N0->getOperand(0).getValueSizeInBits(); 1438 PreExt = N0->getOperand(0); 1439 Signed = true; 1440 } else if (auto *LN0 = dyn_cast<LoadSDNode>(N0)) { 1441 // ZEXTLOAD / SEXTLOAD 1442 if (LN0->getExtensionType() == ISD::ZEXTLOAD) { 1443 MinBits = LN0->getMemoryVT().getSizeInBits(); 1444 PreExt = N0; 1445 } else if (LN0->getExtensionType() == ISD::SEXTLOAD) { 1446 Signed = true; 1447 MinBits = LN0->getMemoryVT().getSizeInBits(); 1448 PreExt = N0; 1449 } 1450 } 1451 1452 // Figure out how many bits we need to preserve this constant. 1453 unsigned ReqdBits = Signed ? 1454 C1.getBitWidth() - C1.getNumSignBits() + 1 : 1455 C1.getActiveBits(); 1456 1457 // Make sure we're not losing bits from the constant. 1458 if (MinBits > 0 && 1459 MinBits < C1.getBitWidth() && 1460 MinBits >= ReqdBits) { 1461 EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits); 1462 if (isTypeDesirableForOp(ISD::SETCC, MinVT)) { 1463 // Will get folded away. 1464 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreExt); 1465 SDValue C = DAG.getConstant(C1.trunc(MinBits), dl, MinVT); 1466 return DAG.getSetCC(dl, VT, Trunc, C, Cond); 1467 } 1468 1469 // If truncating the setcc operands is not desirable, we can still 1470 // simplify the expression in some cases: 1471 // setcc ([sz]ext (setcc x, y, cc)), 0, setne) -> setcc (x, y, cc) 1472 // setcc ([sz]ext (setcc x, y, cc)), 0, seteq) -> setcc (x, y, inv(cc)) 1473 // setcc (zext (setcc x, y, cc)), 1, setne) -> setcc (x, y, inv(cc)) 1474 // setcc (zext (setcc x, y, cc)), 1, seteq) -> setcc (x, y, cc) 1475 // setcc (sext (setcc x, y, cc)), -1, setne) -> setcc (x, y, inv(cc)) 1476 // setcc (sext (setcc x, y, cc)), -1, seteq) -> setcc (x, y, cc) 1477 SDValue TopSetCC = N0->getOperand(0); 1478 unsigned N0Opc = N0->getOpcode(); 1479 bool SExt = (N0Opc == ISD::SIGN_EXTEND); 1480 if (TopSetCC.getValueType() == MVT::i1 && VT == MVT::i1 && 1481 TopSetCC.getOpcode() == ISD::SETCC && 1482 (N0Opc == ISD::ZERO_EXTEND || N0Opc == ISD::SIGN_EXTEND) && 1483 (isConstFalseVal(N1C) || 1484 isExtendedTrueVal(N1C, N0->getValueType(0), SExt))) { 1485 1486 bool Inverse = (N1C->isNullValue() && Cond == ISD::SETEQ) || 1487 (!N1C->isNullValue() && Cond == ISD::SETNE); 1488 1489 if (!Inverse) 1490 return TopSetCC; 1491 1492 ISD::CondCode InvCond = ISD::getSetCCInverse( 1493 cast<CondCodeSDNode>(TopSetCC.getOperand(2))->get(), 1494 TopSetCC.getOperand(0).getValueType().isInteger()); 1495 return DAG.getSetCC(dl, VT, TopSetCC.getOperand(0), 1496 TopSetCC.getOperand(1), 1497 InvCond); 1498 1499 } 1500 } 1501 } 1502 1503 // If the LHS is '(and load, const)', the RHS is 0, 1504 // the test is for equality or unsigned, and all 1 bits of the const are 1505 // in the same partial word, see if we can shorten the load. 1506 if (DCI.isBeforeLegalize() && 1507 !ISD::isSignedIntSetCC(Cond) && 1508 N0.getOpcode() == ISD::AND && C1 == 0 && 1509 N0.getNode()->hasOneUse() && 1510 isa<LoadSDNode>(N0.getOperand(0)) && 1511 N0.getOperand(0).getNode()->hasOneUse() && 1512 isa<ConstantSDNode>(N0.getOperand(1))) { 1513 LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0)); 1514 APInt bestMask; 1515 unsigned bestWidth = 0, bestOffset = 0; 1516 if (!Lod->isVolatile() && Lod->isUnindexed()) { 1517 unsigned origWidth = N0.getValueType().getSizeInBits(); 1518 unsigned maskWidth = origWidth; 1519 // We can narrow (e.g.) 16-bit extending loads on 32-bit target to 1520 // 8 bits, but have to be careful... 1521 if (Lod->getExtensionType() != ISD::NON_EXTLOAD) 1522 origWidth = Lod->getMemoryVT().getSizeInBits(); 1523 const APInt &Mask = 1524 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); 1525 for (unsigned width = origWidth / 2; width>=8; width /= 2) { 1526 APInt newMask = APInt::getLowBitsSet(maskWidth, width); 1527 for (unsigned offset=0; offset<origWidth/width; offset++) { 1528 if ((newMask & Mask) == Mask) { 1529 if (!DAG.getDataLayout().isLittleEndian()) 1530 bestOffset = (origWidth/width - offset - 1) * (width/8); 1531 else 1532 bestOffset = (uint64_t)offset * (width/8); 1533 bestMask = Mask.lshr(offset * (width/8) * 8); 1534 bestWidth = width; 1535 break; 1536 } 1537 newMask = newMask << width; 1538 } 1539 } 1540 } 1541 if (bestWidth) { 1542 EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth); 1543 if (newVT.isRound()) { 1544 EVT PtrType = Lod->getOperand(1).getValueType(); 1545 SDValue Ptr = Lod->getBasePtr(); 1546 if (bestOffset != 0) 1547 Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(), 1548 DAG.getConstant(bestOffset, dl, PtrType)); 1549 unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset); 1550 SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr, 1551 Lod->getPointerInfo().getWithOffset(bestOffset), 1552 false, false, false, NewAlign); 1553 return DAG.getSetCC(dl, VT, 1554 DAG.getNode(ISD::AND, dl, newVT, NewLoad, 1555 DAG.getConstant(bestMask.trunc(bestWidth), 1556 dl, newVT)), 1557 DAG.getConstant(0LL, dl, newVT), Cond); 1558 } 1559 } 1560 } 1561 1562 // If the LHS is a ZERO_EXTEND, perform the comparison on the input. 1563 if (N0.getOpcode() == ISD::ZERO_EXTEND) { 1564 unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits(); 1565 1566 // If the comparison constant has bits in the upper part, the 1567 // zero-extended value could never match. 1568 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(), 1569 C1.getBitWidth() - InSize))) { 1570 switch (Cond) { 1571 case ISD::SETUGT: 1572 case ISD::SETUGE: 1573 case ISD::SETEQ: return DAG.getConstant(0, dl, VT); 1574 case ISD::SETULT: 1575 case ISD::SETULE: 1576 case ISD::SETNE: return DAG.getConstant(1, dl, VT); 1577 case ISD::SETGT: 1578 case ISD::SETGE: 1579 // True if the sign bit of C1 is set. 1580 return DAG.getConstant(C1.isNegative(), dl, VT); 1581 case ISD::SETLT: 1582 case ISD::SETLE: 1583 // True if the sign bit of C1 isn't set. 1584 return DAG.getConstant(C1.isNonNegative(), dl, VT); 1585 default: 1586 break; 1587 } 1588 } 1589 1590 // Otherwise, we can perform the comparison with the low bits. 1591 switch (Cond) { 1592 case ISD::SETEQ: 1593 case ISD::SETNE: 1594 case ISD::SETUGT: 1595 case ISD::SETUGE: 1596 case ISD::SETULT: 1597 case ISD::SETULE: { 1598 EVT newVT = N0.getOperand(0).getValueType(); 1599 if (DCI.isBeforeLegalizeOps() || 1600 (isOperationLegal(ISD::SETCC, newVT) && 1601 getCondCodeAction(Cond, newVT.getSimpleVT()) == Legal)) { 1602 EVT NewSetCCVT = 1603 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), newVT); 1604 SDValue NewConst = DAG.getConstant(C1.trunc(InSize), dl, newVT); 1605 1606 SDValue NewSetCC = DAG.getSetCC(dl, NewSetCCVT, N0.getOperand(0), 1607 NewConst, Cond); 1608 return DAG.getBoolExtOrTrunc(NewSetCC, dl, VT, N0.getValueType()); 1609 } 1610 break; 1611 } 1612 default: 1613 break; // todo, be more careful with signed comparisons 1614 } 1615 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && 1616 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { 1617 EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT(); 1618 unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits(); 1619 EVT ExtDstTy = N0.getValueType(); 1620 unsigned ExtDstTyBits = ExtDstTy.getSizeInBits(); 1621 1622 // If the constant doesn't fit into the number of bits for the source of 1623 // the sign extension, it is impossible for both sides to be equal. 1624 if (C1.getMinSignedBits() > ExtSrcTyBits) 1625 return DAG.getConstant(Cond == ISD::SETNE, dl, VT); 1626 1627 SDValue ZextOp; 1628 EVT Op0Ty = N0.getOperand(0).getValueType(); 1629 if (Op0Ty == ExtSrcTy) { 1630 ZextOp = N0.getOperand(0); 1631 } else { 1632 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits); 1633 ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0), 1634 DAG.getConstant(Imm, dl, Op0Ty)); 1635 } 1636 if (!DCI.isCalledByLegalizer()) 1637 DCI.AddToWorklist(ZextOp.getNode()); 1638 // Otherwise, make this a use of a zext. 1639 return DAG.getSetCC(dl, VT, ZextOp, 1640 DAG.getConstant(C1 & APInt::getLowBitsSet( 1641 ExtDstTyBits, 1642 ExtSrcTyBits), 1643 dl, ExtDstTy), 1644 Cond); 1645 } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) && 1646 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { 1647 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC 1648 if (N0.getOpcode() == ISD::SETCC && 1649 isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) { 1650 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1); 1651 if (TrueWhenTrue) 1652 return DAG.getNode(ISD::TRUNCATE, dl, VT, N0); 1653 // Invert the condition. 1654 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get(); 1655 CC = ISD::getSetCCInverse(CC, 1656 N0.getOperand(0).getValueType().isInteger()); 1657 if (DCI.isBeforeLegalizeOps() || 1658 isCondCodeLegal(CC, N0.getOperand(0).getSimpleValueType())) 1659 return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC); 1660 } 1661 1662 if ((N0.getOpcode() == ISD::XOR || 1663 (N0.getOpcode() == ISD::AND && 1664 N0.getOperand(0).getOpcode() == ISD::XOR && 1665 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) && 1666 isa<ConstantSDNode>(N0.getOperand(1)) && 1667 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) { 1668 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We 1669 // can only do this if the top bits are known zero. 1670 unsigned BitWidth = N0.getValueSizeInBits(); 1671 if (DAG.MaskedValueIsZero(N0, 1672 APInt::getHighBitsSet(BitWidth, 1673 BitWidth-1))) { 1674 // Okay, get the un-inverted input value. 1675 SDValue Val; 1676 if (N0.getOpcode() == ISD::XOR) 1677 Val = N0.getOperand(0); 1678 else { 1679 assert(N0.getOpcode() == ISD::AND && 1680 N0.getOperand(0).getOpcode() == ISD::XOR); 1681 // ((X^1)&1)^1 -> X & 1 1682 Val = DAG.getNode(ISD::AND, dl, N0.getValueType(), 1683 N0.getOperand(0).getOperand(0), 1684 N0.getOperand(1)); 1685 } 1686 1687 return DAG.getSetCC(dl, VT, Val, N1, 1688 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ); 1689 } 1690 } else if (N1C->getAPIntValue() == 1 && 1691 (VT == MVT::i1 || 1692 getBooleanContents(N0->getValueType(0)) == 1693 ZeroOrOneBooleanContent)) { 1694 SDValue Op0 = N0; 1695 if (Op0.getOpcode() == ISD::TRUNCATE) 1696 Op0 = Op0.getOperand(0); 1697 1698 if ((Op0.getOpcode() == ISD::XOR) && 1699 Op0.getOperand(0).getOpcode() == ISD::SETCC && 1700 Op0.getOperand(1).getOpcode() == ISD::SETCC) { 1701 // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc) 1702 Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ; 1703 return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1), 1704 Cond); 1705 } 1706 if (Op0.getOpcode() == ISD::AND && 1707 isa<ConstantSDNode>(Op0.getOperand(1)) && 1708 cast<ConstantSDNode>(Op0.getOperand(1))->getAPIntValue() == 1) { 1709 // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0. 1710 if (Op0.getValueType().bitsGT(VT)) 1711 Op0 = DAG.getNode(ISD::AND, dl, VT, 1712 DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)), 1713 DAG.getConstant(1, dl, VT)); 1714 else if (Op0.getValueType().bitsLT(VT)) 1715 Op0 = DAG.getNode(ISD::AND, dl, VT, 1716 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)), 1717 DAG.getConstant(1, dl, VT)); 1718 1719 return DAG.getSetCC(dl, VT, Op0, 1720 DAG.getConstant(0, dl, Op0.getValueType()), 1721 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ); 1722 } 1723 if (Op0.getOpcode() == ISD::AssertZext && 1724 cast<VTSDNode>(Op0.getOperand(1))->getVT() == MVT::i1) 1725 return DAG.getSetCC(dl, VT, Op0, 1726 DAG.getConstant(0, dl, Op0.getValueType()), 1727 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ); 1728 } 1729 } 1730 1731 APInt MinVal, MaxVal; 1732 unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits(); 1733 if (ISD::isSignedIntSetCC(Cond)) { 1734 MinVal = APInt::getSignedMinValue(OperandBitSize); 1735 MaxVal = APInt::getSignedMaxValue(OperandBitSize); 1736 } else { 1737 MinVal = APInt::getMinValue(OperandBitSize); 1738 MaxVal = APInt::getMaxValue(OperandBitSize); 1739 } 1740 1741 // Canonicalize GE/LE comparisons to use GT/LT comparisons. 1742 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { 1743 if (C1 == MinVal) return DAG.getConstant(1, dl, VT); // X >= MIN --> true 1744 // X >= C0 --> X > (C0 - 1) 1745 APInt C = C1 - 1; 1746 ISD::CondCode NewCC = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT; 1747 if ((DCI.isBeforeLegalizeOps() || 1748 isCondCodeLegal(NewCC, VT.getSimpleVT())) && 1749 (!N1C->isOpaque() || (N1C->isOpaque() && C.getBitWidth() <= 64 && 1750 isLegalICmpImmediate(C.getSExtValue())))) { 1751 return DAG.getSetCC(dl, VT, N0, 1752 DAG.getConstant(C, dl, N1.getValueType()), 1753 NewCC); 1754 } 1755 } 1756 1757 if (Cond == ISD::SETLE || Cond == ISD::SETULE) { 1758 if (C1 == MaxVal) return DAG.getConstant(1, dl, VT); // X <= MAX --> true 1759 // X <= C0 --> X < (C0 + 1) 1760 APInt C = C1 + 1; 1761 ISD::CondCode NewCC = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT; 1762 if ((DCI.isBeforeLegalizeOps() || 1763 isCondCodeLegal(NewCC, VT.getSimpleVT())) && 1764 (!N1C->isOpaque() || (N1C->isOpaque() && C.getBitWidth() <= 64 && 1765 isLegalICmpImmediate(C.getSExtValue())))) { 1766 return DAG.getSetCC(dl, VT, N0, 1767 DAG.getConstant(C, dl, N1.getValueType()), 1768 NewCC); 1769 } 1770 } 1771 1772 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal) 1773 return DAG.getConstant(0, dl, VT); // X < MIN --> false 1774 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal) 1775 return DAG.getConstant(1, dl, VT); // X >= MIN --> true 1776 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal) 1777 return DAG.getConstant(0, dl, VT); // X > MAX --> false 1778 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal) 1779 return DAG.getConstant(1, dl, VT); // X <= MAX --> true 1780 1781 // Canonicalize setgt X, Min --> setne X, Min 1782 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal) 1783 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE); 1784 // Canonicalize setlt X, Max --> setne X, Max 1785 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal) 1786 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE); 1787 1788 // If we have setult X, 1, turn it into seteq X, 0 1789 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1) 1790 return DAG.getSetCC(dl, VT, N0, 1791 DAG.getConstant(MinVal, dl, N0.getValueType()), 1792 ISD::SETEQ); 1793 // If we have setugt X, Max-1, turn it into seteq X, Max 1794 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1) 1795 return DAG.getSetCC(dl, VT, N0, 1796 DAG.getConstant(MaxVal, dl, N0.getValueType()), 1797 ISD::SETEQ); 1798 1799 // If we have "setcc X, C0", check to see if we can shrink the immediate 1800 // by changing cc. 1801 1802 // SETUGT X, SINTMAX -> SETLT X, 0 1803 if (Cond == ISD::SETUGT && 1804 C1 == APInt::getSignedMaxValue(OperandBitSize)) 1805 return DAG.getSetCC(dl, VT, N0, 1806 DAG.getConstant(0, dl, N1.getValueType()), 1807 ISD::SETLT); 1808 1809 // SETULT X, SINTMIN -> SETGT X, -1 1810 if (Cond == ISD::SETULT && 1811 C1 == APInt::getSignedMinValue(OperandBitSize)) { 1812 SDValue ConstMinusOne = 1813 DAG.getConstant(APInt::getAllOnesValue(OperandBitSize), dl, 1814 N1.getValueType()); 1815 return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT); 1816 } 1817 1818 // Fold bit comparisons when we can. 1819 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 1820 (VT == N0.getValueType() || 1821 (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) && 1822 N0.getOpcode() == ISD::AND) { 1823 auto &DL = DAG.getDataLayout(); 1824 if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { 1825 EVT ShiftTy = DCI.isBeforeLegalize() 1826 ? getPointerTy(DL) 1827 : getShiftAmountTy(N0.getValueType(), DL); 1828 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 1829 // Perform the xform if the AND RHS is a single bit. 1830 if (AndRHS->getAPIntValue().isPowerOf2()) { 1831 return DAG.getNode(ISD::TRUNCATE, dl, VT, 1832 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0, 1833 DAG.getConstant(AndRHS->getAPIntValue().logBase2(), dl, 1834 ShiftTy))); 1835 } 1836 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) { 1837 // (X & 8) == 8 --> (X & 8) >> 3 1838 // Perform the xform if C1 is a single bit. 1839 if (C1.isPowerOf2()) { 1840 return DAG.getNode(ISD::TRUNCATE, dl, VT, 1841 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0, 1842 DAG.getConstant(C1.logBase2(), dl, 1843 ShiftTy))); 1844 } 1845 } 1846 } 1847 } 1848 1849 if (C1.getMinSignedBits() <= 64 && 1850 !isLegalICmpImmediate(C1.getSExtValue())) { 1851 // (X & -256) == 256 -> (X >> 8) == 1 1852 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 1853 N0.getOpcode() == ISD::AND && N0.hasOneUse()) { 1854 if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { 1855 const APInt &AndRHSC = AndRHS->getAPIntValue(); 1856 if ((-AndRHSC).isPowerOf2() && (AndRHSC & C1) == C1) { 1857 unsigned ShiftBits = AndRHSC.countTrailingZeros(); 1858 auto &DL = DAG.getDataLayout(); 1859 EVT ShiftTy = DCI.isBeforeLegalize() 1860 ? getPointerTy(DL) 1861 : getShiftAmountTy(N0.getValueType(), DL); 1862 EVT CmpTy = N0.getValueType(); 1863 SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0.getOperand(0), 1864 DAG.getConstant(ShiftBits, dl, 1865 ShiftTy)); 1866 SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), dl, CmpTy); 1867 return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond); 1868 } 1869 } 1870 } else if (Cond == ISD::SETULT || Cond == ISD::SETUGE || 1871 Cond == ISD::SETULE || Cond == ISD::SETUGT) { 1872 bool AdjOne = (Cond == ISD::SETULE || Cond == ISD::SETUGT); 1873 // X < 0x100000000 -> (X >> 32) < 1 1874 // X >= 0x100000000 -> (X >> 32) >= 1 1875 // X <= 0x0ffffffff -> (X >> 32) < 1 1876 // X > 0x0ffffffff -> (X >> 32) >= 1 1877 unsigned ShiftBits; 1878 APInt NewC = C1; 1879 ISD::CondCode NewCond = Cond; 1880 if (AdjOne) { 1881 ShiftBits = C1.countTrailingOnes(); 1882 NewC = NewC + 1; 1883 NewCond = (Cond == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 1884 } else { 1885 ShiftBits = C1.countTrailingZeros(); 1886 } 1887 NewC = NewC.lshr(ShiftBits); 1888 if (ShiftBits && NewC.getMinSignedBits() <= 64 && 1889 isLegalICmpImmediate(NewC.getSExtValue())) { 1890 auto &DL = DAG.getDataLayout(); 1891 EVT ShiftTy = DCI.isBeforeLegalize() 1892 ? getPointerTy(DL) 1893 : getShiftAmountTy(N0.getValueType(), DL); 1894 EVT CmpTy = N0.getValueType(); 1895 SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0, 1896 DAG.getConstant(ShiftBits, dl, ShiftTy)); 1897 SDValue CmpRHS = DAG.getConstant(NewC, dl, CmpTy); 1898 return DAG.getSetCC(dl, VT, Shift, CmpRHS, NewCond); 1899 } 1900 } 1901 } 1902 } 1903 1904 if (isa<ConstantFPSDNode>(N0.getNode())) { 1905 // Constant fold or commute setcc. 1906 SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl); 1907 if (O.getNode()) return O; 1908 } else if (auto *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) { 1909 // If the RHS of an FP comparison is a constant, simplify it away in 1910 // some cases. 1911 if (CFP->getValueAPF().isNaN()) { 1912 // If an operand is known to be a nan, we can fold it. 1913 switch (ISD::getUnorderedFlavor(Cond)) { 1914 default: llvm_unreachable("Unknown flavor!"); 1915 case 0: // Known false. 1916 return DAG.getConstant(0, dl, VT); 1917 case 1: // Known true. 1918 return DAG.getConstant(1, dl, VT); 1919 case 2: // Undefined. 1920 return DAG.getUNDEF(VT); 1921 } 1922 } 1923 1924 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the 1925 // constant if knowing that the operand is non-nan is enough. We prefer to 1926 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to 1927 // materialize 0.0. 1928 if (Cond == ISD::SETO || Cond == ISD::SETUO) 1929 return DAG.getSetCC(dl, VT, N0, N0, Cond); 1930 1931 // If the condition is not legal, see if we can find an equivalent one 1932 // which is legal. 1933 if (!isCondCodeLegal(Cond, N0.getSimpleValueType())) { 1934 // If the comparison was an awkward floating-point == or != and one of 1935 // the comparison operands is infinity or negative infinity, convert the 1936 // condition to a less-awkward <= or >=. 1937 if (CFP->getValueAPF().isInfinity()) { 1938 if (CFP->getValueAPF().isNegative()) { 1939 if (Cond == ISD::SETOEQ && 1940 isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType())) 1941 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE); 1942 if (Cond == ISD::SETUEQ && 1943 isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType())) 1944 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE); 1945 if (Cond == ISD::SETUNE && 1946 isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType())) 1947 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT); 1948 if (Cond == ISD::SETONE && 1949 isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType())) 1950 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT); 1951 } else { 1952 if (Cond == ISD::SETOEQ && 1953 isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType())) 1954 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE); 1955 if (Cond == ISD::SETUEQ && 1956 isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType())) 1957 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE); 1958 if (Cond == ISD::SETUNE && 1959 isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType())) 1960 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT); 1961 if (Cond == ISD::SETONE && 1962 isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType())) 1963 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT); 1964 } 1965 } 1966 } 1967 } 1968 1969 if (N0 == N1) { 1970 // The sext(setcc()) => setcc() optimization relies on the appropriate 1971 // constant being emitted. 1972 uint64_t EqVal = 0; 1973 switch (getBooleanContents(N0.getValueType())) { 1974 case UndefinedBooleanContent: 1975 case ZeroOrOneBooleanContent: 1976 EqVal = ISD::isTrueWhenEqual(Cond); 1977 break; 1978 case ZeroOrNegativeOneBooleanContent: 1979 EqVal = ISD::isTrueWhenEqual(Cond) ? -1 : 0; 1980 break; 1981 } 1982 1983 // We can always fold X == X for integer setcc's. 1984 if (N0.getValueType().isInteger()) { 1985 return DAG.getConstant(EqVal, dl, VT); 1986 } 1987 unsigned UOF = ISD::getUnorderedFlavor(Cond); 1988 if (UOF == 2) // FP operators that are undefined on NaNs. 1989 return DAG.getConstant(EqVal, dl, VT); 1990 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond))) 1991 return DAG.getConstant(EqVal, dl, VT); 1992 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO 1993 // if it is not already. 1994 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO; 1995 if (NewCond != Cond && (DCI.isBeforeLegalizeOps() || 1996 getCondCodeAction(NewCond, N0.getSimpleValueType()) == Legal)) 1997 return DAG.getSetCC(dl, VT, N0, N1, NewCond); 1998 } 1999 2000 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 2001 N0.getValueType().isInteger()) { 2002 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB || 2003 N0.getOpcode() == ISD::XOR) { 2004 // Simplify (X+Y) == (X+Z) --> Y == Z 2005 if (N0.getOpcode() == N1.getOpcode()) { 2006 if (N0.getOperand(0) == N1.getOperand(0)) 2007 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond); 2008 if (N0.getOperand(1) == N1.getOperand(1)) 2009 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond); 2010 if (DAG.isCommutativeBinOp(N0.getOpcode())) { 2011 // If X op Y == Y op X, try other combinations. 2012 if (N0.getOperand(0) == N1.getOperand(1)) 2013 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0), 2014 Cond); 2015 if (N0.getOperand(1) == N1.getOperand(0)) 2016 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1), 2017 Cond); 2018 } 2019 } 2020 2021 // If RHS is a legal immediate value for a compare instruction, we need 2022 // to be careful about increasing register pressure needlessly. 2023 bool LegalRHSImm = false; 2024 2025 if (auto *RHSC = dyn_cast<ConstantSDNode>(N1)) { 2026 if (auto *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { 2027 // Turn (X+C1) == C2 --> X == C2-C1 2028 if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) { 2029 return DAG.getSetCC(dl, VT, N0.getOperand(0), 2030 DAG.getConstant(RHSC->getAPIntValue()- 2031 LHSR->getAPIntValue(), 2032 dl, N0.getValueType()), Cond); 2033 } 2034 2035 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. 2036 if (N0.getOpcode() == ISD::XOR) 2037 // If we know that all of the inverted bits are zero, don't bother 2038 // performing the inversion. 2039 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue())) 2040 return 2041 DAG.getSetCC(dl, VT, N0.getOperand(0), 2042 DAG.getConstant(LHSR->getAPIntValue() ^ 2043 RHSC->getAPIntValue(), 2044 dl, N0.getValueType()), 2045 Cond); 2046 } 2047 2048 // Turn (C1-X) == C2 --> X == C1-C2 2049 if (auto *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) { 2050 if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) { 2051 return 2052 DAG.getSetCC(dl, VT, N0.getOperand(1), 2053 DAG.getConstant(SUBC->getAPIntValue() - 2054 RHSC->getAPIntValue(), 2055 dl, N0.getValueType()), 2056 Cond); 2057 } 2058 } 2059 2060 // Could RHSC fold directly into a compare? 2061 if (RHSC->getValueType(0).getSizeInBits() <= 64) 2062 LegalRHSImm = isLegalICmpImmediate(RHSC->getSExtValue()); 2063 } 2064 2065 // Simplify (X+Z) == X --> Z == 0 2066 // Don't do this if X is an immediate that can fold into a cmp 2067 // instruction and X+Z has other uses. It could be an induction variable 2068 // chain, and the transform would increase register pressure. 2069 if (!LegalRHSImm || N0.getNode()->hasOneUse()) { 2070 if (N0.getOperand(0) == N1) 2071 return DAG.getSetCC(dl, VT, N0.getOperand(1), 2072 DAG.getConstant(0, dl, N0.getValueType()), Cond); 2073 if (N0.getOperand(1) == N1) { 2074 if (DAG.isCommutativeBinOp(N0.getOpcode())) 2075 return DAG.getSetCC(dl, VT, N0.getOperand(0), 2076 DAG.getConstant(0, dl, N0.getValueType()), 2077 Cond); 2078 if (N0.getNode()->hasOneUse()) { 2079 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!"); 2080 auto &DL = DAG.getDataLayout(); 2081 // (Z-X) == X --> Z == X<<1 2082 SDValue SH = DAG.getNode( 2083 ISD::SHL, dl, N1.getValueType(), N1, 2084 DAG.getConstant(1, dl, 2085 getShiftAmountTy(N1.getValueType(), DL))); 2086 if (!DCI.isCalledByLegalizer()) 2087 DCI.AddToWorklist(SH.getNode()); 2088 return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond); 2089 } 2090 } 2091 } 2092 } 2093 2094 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB || 2095 N1.getOpcode() == ISD::XOR) { 2096 // Simplify X == (X+Z) --> Z == 0 2097 if (N1.getOperand(0) == N0) 2098 return DAG.getSetCC(dl, VT, N1.getOperand(1), 2099 DAG.getConstant(0, dl, N1.getValueType()), Cond); 2100 if (N1.getOperand(1) == N0) { 2101 if (DAG.isCommutativeBinOp(N1.getOpcode())) 2102 return DAG.getSetCC(dl, VT, N1.getOperand(0), 2103 DAG.getConstant(0, dl, N1.getValueType()), Cond); 2104 if (N1.getNode()->hasOneUse()) { 2105 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!"); 2106 auto &DL = DAG.getDataLayout(); 2107 // X == (Z-X) --> X<<1 == Z 2108 SDValue SH = DAG.getNode( 2109 ISD::SHL, dl, N1.getValueType(), N0, 2110 DAG.getConstant(1, dl, getShiftAmountTy(N0.getValueType(), DL))); 2111 if (!DCI.isCalledByLegalizer()) 2112 DCI.AddToWorklist(SH.getNode()); 2113 return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond); 2114 } 2115 } 2116 } 2117 2118 if (SDValue V = simplifySetCCWithAnd(VT, N0, N1, Cond, DCI, dl)) 2119 return V; 2120 } 2121 2122 // Fold away ALL boolean setcc's. 2123 SDValue Temp; 2124 if (N0.getValueType() == MVT::i1 && foldBooleans) { 2125 switch (Cond) { 2126 default: llvm_unreachable("Unknown integer setcc!"); 2127 case ISD::SETEQ: // X == Y -> ~(X^Y) 2128 Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1); 2129 N0 = DAG.getNOT(dl, Temp, MVT::i1); 2130 if (!DCI.isCalledByLegalizer()) 2131 DCI.AddToWorklist(Temp.getNode()); 2132 break; 2133 case ISD::SETNE: // X != Y --> (X^Y) 2134 N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1); 2135 break; 2136 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y 2137 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y 2138 Temp = DAG.getNOT(dl, N0, MVT::i1); 2139 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp); 2140 if (!DCI.isCalledByLegalizer()) 2141 DCI.AddToWorklist(Temp.getNode()); 2142 break; 2143 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X 2144 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X 2145 Temp = DAG.getNOT(dl, N1, MVT::i1); 2146 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp); 2147 if (!DCI.isCalledByLegalizer()) 2148 DCI.AddToWorklist(Temp.getNode()); 2149 break; 2150 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y 2151 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y 2152 Temp = DAG.getNOT(dl, N0, MVT::i1); 2153 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp); 2154 if (!DCI.isCalledByLegalizer()) 2155 DCI.AddToWorklist(Temp.getNode()); 2156 break; 2157 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X 2158 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X 2159 Temp = DAG.getNOT(dl, N1, MVT::i1); 2160 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp); 2161 break; 2162 } 2163 if (VT != MVT::i1) { 2164 if (!DCI.isCalledByLegalizer()) 2165 DCI.AddToWorklist(N0.getNode()); 2166 // FIXME: If running after legalize, we probably can't do this. 2167 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0); 2168 } 2169 return N0; 2170 } 2171 2172 // Could not fold it. 2173 return SDValue(); 2174 } 2175 2176 /// Returns true (and the GlobalValue and the offset) if the node is a 2177 /// GlobalAddress + offset. 2178 bool TargetLowering::isGAPlusOffset(SDNode *N, const GlobalValue *&GA, 2179 int64_t &Offset) const { 2180 if (auto *GASD = dyn_cast<GlobalAddressSDNode>(N)) { 2181 GA = GASD->getGlobal(); 2182 Offset += GASD->getOffset(); 2183 return true; 2184 } 2185 2186 if (N->getOpcode() == ISD::ADD) { 2187 SDValue N1 = N->getOperand(0); 2188 SDValue N2 = N->getOperand(1); 2189 if (isGAPlusOffset(N1.getNode(), GA, Offset)) { 2190 if (auto *V = dyn_cast<ConstantSDNode>(N2)) { 2191 Offset += V->getSExtValue(); 2192 return true; 2193 } 2194 } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) { 2195 if (auto *V = dyn_cast<ConstantSDNode>(N1)) { 2196 Offset += V->getSExtValue(); 2197 return true; 2198 } 2199 } 2200 } 2201 2202 return false; 2203 } 2204 2205 SDValue TargetLowering::PerformDAGCombine(SDNode *N, 2206 DAGCombinerInfo &DCI) const { 2207 // Default implementation: no optimization. 2208 return SDValue(); 2209 } 2210 2211 //===----------------------------------------------------------------------===// 2212 // Inline Assembler Implementation Methods 2213 //===----------------------------------------------------------------------===// 2214 2215 TargetLowering::ConstraintType 2216 TargetLowering::getConstraintType(StringRef Constraint) const { 2217 unsigned S = Constraint.size(); 2218 2219 if (S == 1) { 2220 switch (Constraint[0]) { 2221 default: break; 2222 case 'r': return C_RegisterClass; 2223 case 'm': // memory 2224 case 'o': // offsetable 2225 case 'V': // not offsetable 2226 return C_Memory; 2227 case 'i': // Simple Integer or Relocatable Constant 2228 case 'n': // Simple Integer 2229 case 'E': // Floating Point Constant 2230 case 'F': // Floating Point Constant 2231 case 's': // Relocatable Constant 2232 case 'p': // Address. 2233 case 'X': // Allow ANY value. 2234 case 'I': // Target registers. 2235 case 'J': 2236 case 'K': 2237 case 'L': 2238 case 'M': 2239 case 'N': 2240 case 'O': 2241 case 'P': 2242 case '<': 2243 case '>': 2244 return C_Other; 2245 } 2246 } 2247 2248 if (S > 1 && Constraint[0] == '{' && Constraint[S-1] == '}') { 2249 if (S == 8 && Constraint.substr(1, 6) == "memory") // "{memory}" 2250 return C_Memory; 2251 return C_Register; 2252 } 2253 return C_Unknown; 2254 } 2255 2256 /// Try to replace an X constraint, which matches anything, with another that 2257 /// has more specific requirements based on the type of the corresponding 2258 /// operand. 2259 const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{ 2260 if (ConstraintVT.isInteger()) 2261 return "r"; 2262 if (ConstraintVT.isFloatingPoint()) 2263 return "f"; // works for many targets 2264 return nullptr; 2265 } 2266 2267 /// Lower the specified operand into the Ops vector. 2268 /// If it is invalid, don't add anything to Ops. 2269 void TargetLowering::LowerAsmOperandForConstraint(SDValue Op, 2270 std::string &Constraint, 2271 std::vector<SDValue> &Ops, 2272 SelectionDAG &DAG) const { 2273 2274 if (Constraint.length() > 1) return; 2275 2276 char ConstraintLetter = Constraint[0]; 2277 switch (ConstraintLetter) { 2278 default: break; 2279 case 'X': // Allows any operand; labels (basic block) use this. 2280 if (Op.getOpcode() == ISD::BasicBlock) { 2281 Ops.push_back(Op); 2282 return; 2283 } 2284 // fall through 2285 case 'i': // Simple Integer or Relocatable Constant 2286 case 'n': // Simple Integer 2287 case 's': { // Relocatable Constant 2288 // These operands are interested in values of the form (GV+C), where C may 2289 // be folded in as an offset of GV, or it may be explicitly added. Also, it 2290 // is possible and fine if either GV or C are missing. 2291 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 2292 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op); 2293 2294 // If we have "(add GV, C)", pull out GV/C 2295 if (Op.getOpcode() == ISD::ADD) { 2296 C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 2297 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0)); 2298 if (!C || !GA) { 2299 C = dyn_cast<ConstantSDNode>(Op.getOperand(0)); 2300 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1)); 2301 } 2302 if (!C || !GA) { 2303 C = nullptr; 2304 GA = nullptr; 2305 } 2306 } 2307 2308 // If we find a valid operand, map to the TargetXXX version so that the 2309 // value itself doesn't get selected. 2310 if (GA) { // Either &GV or &GV+C 2311 if (ConstraintLetter != 'n') { 2312 int64_t Offs = GA->getOffset(); 2313 if (C) Offs += C->getZExtValue(); 2314 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(), 2315 C ? SDLoc(C) : SDLoc(), 2316 Op.getValueType(), Offs)); 2317 } 2318 return; 2319 } 2320 if (C) { // just C, no GV. 2321 // Simple constants are not allowed for 's'. 2322 if (ConstraintLetter != 's') { 2323 // gcc prints these as sign extended. Sign extend value to 64 bits 2324 // now; without this it would get ZExt'd later in 2325 // ScheduleDAGSDNodes::EmitNode, which is very generic. 2326 Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(), 2327 SDLoc(C), MVT::i64)); 2328 } 2329 return; 2330 } 2331 break; 2332 } 2333 } 2334 } 2335 2336 std::pair<unsigned, const TargetRegisterClass *> 2337 TargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *RI, 2338 StringRef Constraint, 2339 MVT VT) const { 2340 if (Constraint.empty() || Constraint[0] != '{') 2341 return std::make_pair(0u, static_cast<TargetRegisterClass*>(nullptr)); 2342 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?"); 2343 2344 // Remove the braces from around the name. 2345 StringRef RegName(Constraint.data()+1, Constraint.size()-2); 2346 2347 std::pair<unsigned, const TargetRegisterClass*> R = 2348 std::make_pair(0u, static_cast<const TargetRegisterClass*>(nullptr)); 2349 2350 // Figure out which register class contains this reg. 2351 for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(), 2352 E = RI->regclass_end(); RCI != E; ++RCI) { 2353 const TargetRegisterClass *RC = *RCI; 2354 2355 // If none of the value types for this register class are valid, we 2356 // can't use it. For example, 64-bit reg classes on 32-bit targets. 2357 if (!isLegalRC(RC)) 2358 continue; 2359 2360 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); 2361 I != E; ++I) { 2362 if (RegName.equals_lower(RI->getRegAsmName(*I))) { 2363 std::pair<unsigned, const TargetRegisterClass*> S = 2364 std::make_pair(*I, RC); 2365 2366 // If this register class has the requested value type, return it, 2367 // otherwise keep searching and return the first class found 2368 // if no other is found which explicitly has the requested type. 2369 if (RC->hasType(VT)) 2370 return S; 2371 else if (!R.second) 2372 R = S; 2373 } 2374 } 2375 } 2376 2377 return R; 2378 } 2379 2380 //===----------------------------------------------------------------------===// 2381 // Constraint Selection. 2382 2383 /// Return true of this is an input operand that is a matching constraint like 2384 /// "4". 2385 bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const { 2386 assert(!ConstraintCode.empty() && "No known constraint!"); 2387 return isdigit(static_cast<unsigned char>(ConstraintCode[0])); 2388 } 2389 2390 /// If this is an input matching constraint, this method returns the output 2391 /// operand it matches. 2392 unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const { 2393 assert(!ConstraintCode.empty() && "No known constraint!"); 2394 return atoi(ConstraintCode.c_str()); 2395 } 2396 2397 /// Split up the constraint string from the inline assembly value into the 2398 /// specific constraints and their prefixes, and also tie in the associated 2399 /// operand values. 2400 /// If this returns an empty vector, and if the constraint string itself 2401 /// isn't empty, there was an error parsing. 2402 TargetLowering::AsmOperandInfoVector 2403 TargetLowering::ParseConstraints(const DataLayout &DL, 2404 const TargetRegisterInfo *TRI, 2405 ImmutableCallSite CS) const { 2406 /// Information about all of the constraints. 2407 AsmOperandInfoVector ConstraintOperands; 2408 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); 2409 unsigned maCount = 0; // Largest number of multiple alternative constraints. 2410 2411 // Do a prepass over the constraints, canonicalizing them, and building up the 2412 // ConstraintOperands list. 2413 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. 2414 unsigned ResNo = 0; // ResNo - The result number of the next output. 2415 2416 for (InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) { 2417 ConstraintOperands.emplace_back(std::move(CI)); 2418 AsmOperandInfo &OpInfo = ConstraintOperands.back(); 2419 2420 // Update multiple alternative constraint count. 2421 if (OpInfo.multipleAlternatives.size() > maCount) 2422 maCount = OpInfo.multipleAlternatives.size(); 2423 2424 OpInfo.ConstraintVT = MVT::Other; 2425 2426 // Compute the value type for each operand. 2427 switch (OpInfo.Type) { 2428 case InlineAsm::isOutput: 2429 // Indirect outputs just consume an argument. 2430 if (OpInfo.isIndirect) { 2431 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); 2432 break; 2433 } 2434 2435 // The return value of the call is this value. As such, there is no 2436 // corresponding argument. 2437 assert(!CS.getType()->isVoidTy() && 2438 "Bad inline asm!"); 2439 if (StructType *STy = dyn_cast<StructType>(CS.getType())) { 2440 OpInfo.ConstraintVT = 2441 getSimpleValueType(DL, STy->getElementType(ResNo)); 2442 } else { 2443 assert(ResNo == 0 && "Asm only has one result!"); 2444 OpInfo.ConstraintVT = getSimpleValueType(DL, CS.getType()); 2445 } 2446 ++ResNo; 2447 break; 2448 case InlineAsm::isInput: 2449 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); 2450 break; 2451 case InlineAsm::isClobber: 2452 // Nothing to do. 2453 break; 2454 } 2455 2456 if (OpInfo.CallOperandVal) { 2457 llvm::Type *OpTy = OpInfo.CallOperandVal->getType(); 2458 if (OpInfo.isIndirect) { 2459 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy); 2460 if (!PtrTy) 2461 report_fatal_error("Indirect operand for inline asm not a pointer!"); 2462 OpTy = PtrTy->getElementType(); 2463 } 2464 2465 // Look for vector wrapped in a struct. e.g. { <16 x i8> }. 2466 if (StructType *STy = dyn_cast<StructType>(OpTy)) 2467 if (STy->getNumElements() == 1) 2468 OpTy = STy->getElementType(0); 2469 2470 // If OpTy is not a single value, it may be a struct/union that we 2471 // can tile with integers. 2472 if (!OpTy->isSingleValueType() && OpTy->isSized()) { 2473 unsigned BitSize = DL.getTypeSizeInBits(OpTy); 2474 switch (BitSize) { 2475 default: break; 2476 case 1: 2477 case 8: 2478 case 16: 2479 case 32: 2480 case 64: 2481 case 128: 2482 OpInfo.ConstraintVT = 2483 MVT::getVT(IntegerType::get(OpTy->getContext(), BitSize), true); 2484 break; 2485 } 2486 } else if (PointerType *PT = dyn_cast<PointerType>(OpTy)) { 2487 unsigned PtrSize = DL.getPointerSizeInBits(PT->getAddressSpace()); 2488 OpInfo.ConstraintVT = MVT::getIntegerVT(PtrSize); 2489 } else { 2490 OpInfo.ConstraintVT = MVT::getVT(OpTy, true); 2491 } 2492 } 2493 } 2494 2495 // If we have multiple alternative constraints, select the best alternative. 2496 if (!ConstraintOperands.empty()) { 2497 if (maCount) { 2498 unsigned bestMAIndex = 0; 2499 int bestWeight = -1; 2500 // weight: -1 = invalid match, and 0 = so-so match to 5 = good match. 2501 int weight = -1; 2502 unsigned maIndex; 2503 // Compute the sums of the weights for each alternative, keeping track 2504 // of the best (highest weight) one so far. 2505 for (maIndex = 0; maIndex < maCount; ++maIndex) { 2506 int weightSum = 0; 2507 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size(); 2508 cIndex != eIndex; ++cIndex) { 2509 AsmOperandInfo& OpInfo = ConstraintOperands[cIndex]; 2510 if (OpInfo.Type == InlineAsm::isClobber) 2511 continue; 2512 2513 // If this is an output operand with a matching input operand, 2514 // look up the matching input. If their types mismatch, e.g. one 2515 // is an integer, the other is floating point, or their sizes are 2516 // different, flag it as an maCantMatch. 2517 if (OpInfo.hasMatchingInput()) { 2518 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; 2519 if (OpInfo.ConstraintVT != Input.ConstraintVT) { 2520 if ((OpInfo.ConstraintVT.isInteger() != 2521 Input.ConstraintVT.isInteger()) || 2522 (OpInfo.ConstraintVT.getSizeInBits() != 2523 Input.ConstraintVT.getSizeInBits())) { 2524 weightSum = -1; // Can't match. 2525 break; 2526 } 2527 } 2528 } 2529 weight = getMultipleConstraintMatchWeight(OpInfo, maIndex); 2530 if (weight == -1) { 2531 weightSum = -1; 2532 break; 2533 } 2534 weightSum += weight; 2535 } 2536 // Update best. 2537 if (weightSum > bestWeight) { 2538 bestWeight = weightSum; 2539 bestMAIndex = maIndex; 2540 } 2541 } 2542 2543 // Now select chosen alternative in each constraint. 2544 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size(); 2545 cIndex != eIndex; ++cIndex) { 2546 AsmOperandInfo& cInfo = ConstraintOperands[cIndex]; 2547 if (cInfo.Type == InlineAsm::isClobber) 2548 continue; 2549 cInfo.selectAlternative(bestMAIndex); 2550 } 2551 } 2552 } 2553 2554 // Check and hook up tied operands, choose constraint code to use. 2555 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size(); 2556 cIndex != eIndex; ++cIndex) { 2557 AsmOperandInfo& OpInfo = ConstraintOperands[cIndex]; 2558 2559 // If this is an output operand with a matching input operand, look up the 2560 // matching input. If their types mismatch, e.g. one is an integer, the 2561 // other is floating point, or their sizes are different, flag it as an 2562 // error. 2563 if (OpInfo.hasMatchingInput()) { 2564 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; 2565 2566 if (OpInfo.ConstraintVT != Input.ConstraintVT) { 2567 std::pair<unsigned, const TargetRegisterClass *> MatchRC = 2568 getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode, 2569 OpInfo.ConstraintVT); 2570 std::pair<unsigned, const TargetRegisterClass *> InputRC = 2571 getRegForInlineAsmConstraint(TRI, Input.ConstraintCode, 2572 Input.ConstraintVT); 2573 if ((OpInfo.ConstraintVT.isInteger() != 2574 Input.ConstraintVT.isInteger()) || 2575 (MatchRC.second != InputRC.second)) { 2576 report_fatal_error("Unsupported asm: input constraint" 2577 " with a matching output constraint of" 2578 " incompatible type!"); 2579 } 2580 } 2581 } 2582 } 2583 2584 return ConstraintOperands; 2585 } 2586 2587 /// Return an integer indicating how general CT is. 2588 static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) { 2589 switch (CT) { 2590 case TargetLowering::C_Other: 2591 case TargetLowering::C_Unknown: 2592 return 0; 2593 case TargetLowering::C_Register: 2594 return 1; 2595 case TargetLowering::C_RegisterClass: 2596 return 2; 2597 case TargetLowering::C_Memory: 2598 return 3; 2599 } 2600 llvm_unreachable("Invalid constraint type"); 2601 } 2602 2603 /// Examine constraint type and operand type and determine a weight value. 2604 /// This object must already have been set up with the operand type 2605 /// and the current alternative constraint selected. 2606 TargetLowering::ConstraintWeight 2607 TargetLowering::getMultipleConstraintMatchWeight( 2608 AsmOperandInfo &info, int maIndex) const { 2609 InlineAsm::ConstraintCodeVector *rCodes; 2610 if (maIndex >= (int)info.multipleAlternatives.size()) 2611 rCodes = &info.Codes; 2612 else 2613 rCodes = &info.multipleAlternatives[maIndex].Codes; 2614 ConstraintWeight BestWeight = CW_Invalid; 2615 2616 // Loop over the options, keeping track of the most general one. 2617 for (unsigned i = 0, e = rCodes->size(); i != e; ++i) { 2618 ConstraintWeight weight = 2619 getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str()); 2620 if (weight > BestWeight) 2621 BestWeight = weight; 2622 } 2623 2624 return BestWeight; 2625 } 2626 2627 /// Examine constraint type and operand type and determine a weight value. 2628 /// This object must already have been set up with the operand type 2629 /// and the current alternative constraint selected. 2630 TargetLowering::ConstraintWeight 2631 TargetLowering::getSingleConstraintMatchWeight( 2632 AsmOperandInfo &info, const char *constraint) const { 2633 ConstraintWeight weight = CW_Invalid; 2634 Value *CallOperandVal = info.CallOperandVal; 2635 // If we don't have a value, we can't do a match, 2636 // but allow it at the lowest weight. 2637 if (!CallOperandVal) 2638 return CW_Default; 2639 // Look at the constraint type. 2640 switch (*constraint) { 2641 case 'i': // immediate integer. 2642 case 'n': // immediate integer with a known value. 2643 if (isa<ConstantInt>(CallOperandVal)) 2644 weight = CW_Constant; 2645 break; 2646 case 's': // non-explicit intregal immediate. 2647 if (isa<GlobalValue>(CallOperandVal)) 2648 weight = CW_Constant; 2649 break; 2650 case 'E': // immediate float if host format. 2651 case 'F': // immediate float. 2652 if (isa<ConstantFP>(CallOperandVal)) 2653 weight = CW_Constant; 2654 break; 2655 case '<': // memory operand with autodecrement. 2656 case '>': // memory operand with autoincrement. 2657 case 'm': // memory operand. 2658 case 'o': // offsettable memory operand 2659 case 'V': // non-offsettable memory operand 2660 weight = CW_Memory; 2661 break; 2662 case 'r': // general register. 2663 case 'g': // general register, memory operand or immediate integer. 2664 // note: Clang converts "g" to "imr". 2665 if (CallOperandVal->getType()->isIntegerTy()) 2666 weight = CW_Register; 2667 break; 2668 case 'X': // any operand. 2669 default: 2670 weight = CW_Default; 2671 break; 2672 } 2673 return weight; 2674 } 2675 2676 /// If there are multiple different constraints that we could pick for this 2677 /// operand (e.g. "imr") try to pick the 'best' one. 2678 /// This is somewhat tricky: constraints fall into four classes: 2679 /// Other -> immediates and magic values 2680 /// Register -> one specific register 2681 /// RegisterClass -> a group of regs 2682 /// Memory -> memory 2683 /// Ideally, we would pick the most specific constraint possible: if we have 2684 /// something that fits into a register, we would pick it. The problem here 2685 /// is that if we have something that could either be in a register or in 2686 /// memory that use of the register could cause selection of *other* 2687 /// operands to fail: they might only succeed if we pick memory. Because of 2688 /// this the heuristic we use is: 2689 /// 2690 /// 1) If there is an 'other' constraint, and if the operand is valid for 2691 /// that constraint, use it. This makes us take advantage of 'i' 2692 /// constraints when available. 2693 /// 2) Otherwise, pick the most general constraint present. This prefers 2694 /// 'm' over 'r', for example. 2695 /// 2696 static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo, 2697 const TargetLowering &TLI, 2698 SDValue Op, SelectionDAG *DAG) { 2699 assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options"); 2700 unsigned BestIdx = 0; 2701 TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown; 2702 int BestGenerality = -1; 2703 2704 // Loop over the options, keeping track of the most general one. 2705 for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) { 2706 TargetLowering::ConstraintType CType = 2707 TLI.getConstraintType(OpInfo.Codes[i]); 2708 2709 // If this is an 'other' constraint, see if the operand is valid for it. 2710 // For example, on X86 we might have an 'rI' constraint. If the operand 2711 // is an integer in the range [0..31] we want to use I (saving a load 2712 // of a register), otherwise we must use 'r'. 2713 if (CType == TargetLowering::C_Other && Op.getNode()) { 2714 assert(OpInfo.Codes[i].size() == 1 && 2715 "Unhandled multi-letter 'other' constraint"); 2716 std::vector<SDValue> ResultOps; 2717 TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i], 2718 ResultOps, *DAG); 2719 if (!ResultOps.empty()) { 2720 BestType = CType; 2721 BestIdx = i; 2722 break; 2723 } 2724 } 2725 2726 // Things with matching constraints can only be registers, per gcc 2727 // documentation. This mainly affects "g" constraints. 2728 if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput()) 2729 continue; 2730 2731 // This constraint letter is more general than the previous one, use it. 2732 int Generality = getConstraintGenerality(CType); 2733 if (Generality > BestGenerality) { 2734 BestType = CType; 2735 BestIdx = i; 2736 BestGenerality = Generality; 2737 } 2738 } 2739 2740 OpInfo.ConstraintCode = OpInfo.Codes[BestIdx]; 2741 OpInfo.ConstraintType = BestType; 2742 } 2743 2744 /// Determines the constraint code and constraint type to use for the specific 2745 /// AsmOperandInfo, setting OpInfo.ConstraintCode and OpInfo.ConstraintType. 2746 void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo, 2747 SDValue Op, 2748 SelectionDAG *DAG) const { 2749 assert(!OpInfo.Codes.empty() && "Must have at least one constraint"); 2750 2751 // Single-letter constraints ('r') are very common. 2752 if (OpInfo.Codes.size() == 1) { 2753 OpInfo.ConstraintCode = OpInfo.Codes[0]; 2754 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode); 2755 } else { 2756 ChooseConstraint(OpInfo, *this, Op, DAG); 2757 } 2758 2759 // 'X' matches anything. 2760 if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) { 2761 // Labels and constants are handled elsewhere ('X' is the only thing 2762 // that matches labels). For Functions, the type here is the type of 2763 // the result, which is not what we want to look at; leave them alone. 2764 Value *v = OpInfo.CallOperandVal; 2765 if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) { 2766 OpInfo.CallOperandVal = v; 2767 return; 2768 } 2769 2770 // Otherwise, try to resolve it to something we know about by looking at 2771 // the actual operand type. 2772 if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) { 2773 OpInfo.ConstraintCode = Repl; 2774 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode); 2775 } 2776 } 2777 } 2778 2779 /// \brief Given an exact SDIV by a constant, create a multiplication 2780 /// with the multiplicative inverse of the constant. 2781 static SDValue BuildExactSDIV(const TargetLowering &TLI, SDValue Op1, APInt d, 2782 const SDLoc &dl, SelectionDAG &DAG, 2783 std::vector<SDNode *> &Created) { 2784 assert(d != 0 && "Division by zero!"); 2785 2786 // Shift the value upfront if it is even, so the LSB is one. 2787 unsigned ShAmt = d.countTrailingZeros(); 2788 if (ShAmt) { 2789 // TODO: For UDIV use SRL instead of SRA. 2790 SDValue Amt = 2791 DAG.getConstant(ShAmt, dl, TLI.getShiftAmountTy(Op1.getValueType(), 2792 DAG.getDataLayout())); 2793 SDNodeFlags Flags; 2794 Flags.setExact(true); 2795 Op1 = DAG.getNode(ISD::SRA, dl, Op1.getValueType(), Op1, Amt, &Flags); 2796 Created.push_back(Op1.getNode()); 2797 d = d.ashr(ShAmt); 2798 } 2799 2800 // Calculate the multiplicative inverse, using Newton's method. 2801 APInt t, xn = d; 2802 while ((t = d*xn) != 1) 2803 xn *= APInt(d.getBitWidth(), 2) - t; 2804 2805 SDValue Op2 = DAG.getConstant(xn, dl, Op1.getValueType()); 2806 SDValue Mul = DAG.getNode(ISD::MUL, dl, Op1.getValueType(), Op1, Op2); 2807 Created.push_back(Mul.getNode()); 2808 return Mul; 2809 } 2810 2811 SDValue TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor, 2812 SelectionDAG &DAG, 2813 std::vector<SDNode *> *Created) const { 2814 AttributeSet Attr = DAG.getMachineFunction().getFunction()->getAttributes(); 2815 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2816 if (TLI.isIntDivCheap(N->getValueType(0), Attr)) 2817 return SDValue(N,0); // Lower SDIV as SDIV 2818 return SDValue(); 2819 } 2820 2821 /// \brief Given an ISD::SDIV node expressing a divide by constant, 2822 /// return a DAG expression to select that will generate the same value by 2823 /// multiplying by a magic number. 2824 /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide". 2825 SDValue TargetLowering::BuildSDIV(SDNode *N, const APInt &Divisor, 2826 SelectionDAG &DAG, bool IsAfterLegalization, 2827 std::vector<SDNode *> *Created) const { 2828 assert(Created && "No vector to hold sdiv ops."); 2829 2830 EVT VT = N->getValueType(0); 2831 SDLoc dl(N); 2832 2833 // Check to see if we can do this. 2834 // FIXME: We should be more aggressive here. 2835 if (!isTypeLegal(VT)) 2836 return SDValue(); 2837 2838 // If the sdiv has an 'exact' bit we can use a simpler lowering. 2839 if (cast<BinaryWithFlagsSDNode>(N)->Flags.hasExact()) 2840 return BuildExactSDIV(*this, N->getOperand(0), Divisor, dl, DAG, *Created); 2841 2842 APInt::ms magics = Divisor.magic(); 2843 2844 // Multiply the numerator (operand 0) by the magic value 2845 // FIXME: We should support doing a MUL in a wider type 2846 SDValue Q; 2847 if (IsAfterLegalization ? isOperationLegal(ISD::MULHS, VT) : 2848 isOperationLegalOrCustom(ISD::MULHS, VT)) 2849 Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0), 2850 DAG.getConstant(magics.m, dl, VT)); 2851 else if (IsAfterLegalization ? isOperationLegal(ISD::SMUL_LOHI, VT) : 2852 isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) 2853 Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT), 2854 N->getOperand(0), 2855 DAG.getConstant(magics.m, dl, VT)).getNode(), 1); 2856 else 2857 return SDValue(); // No mulhs or equvialent 2858 // If d > 0 and m < 0, add the numerator 2859 if (Divisor.isStrictlyPositive() && magics.m.isNegative()) { 2860 Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0)); 2861 Created->push_back(Q.getNode()); 2862 } 2863 // If d < 0 and m > 0, subtract the numerator. 2864 if (Divisor.isNegative() && magics.m.isStrictlyPositive()) { 2865 Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0)); 2866 Created->push_back(Q.getNode()); 2867 } 2868 auto &DL = DAG.getDataLayout(); 2869 // Shift right algebraic if shift value is nonzero 2870 if (magics.s > 0) { 2871 Q = DAG.getNode( 2872 ISD::SRA, dl, VT, Q, 2873 DAG.getConstant(magics.s, dl, getShiftAmountTy(Q.getValueType(), DL))); 2874 Created->push_back(Q.getNode()); 2875 } 2876 // Extract the sign bit and add it to the quotient 2877 SDValue T = 2878 DAG.getNode(ISD::SRL, dl, VT, Q, 2879 DAG.getConstant(VT.getScalarSizeInBits() - 1, dl, 2880 getShiftAmountTy(Q.getValueType(), DL))); 2881 Created->push_back(T.getNode()); 2882 return DAG.getNode(ISD::ADD, dl, VT, Q, T); 2883 } 2884 2885 /// \brief Given an ISD::UDIV node expressing a divide by constant, 2886 /// return a DAG expression to select that will generate the same value by 2887 /// multiplying by a magic number. 2888 /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide". 2889 SDValue TargetLowering::BuildUDIV(SDNode *N, const APInt &Divisor, 2890 SelectionDAG &DAG, bool IsAfterLegalization, 2891 std::vector<SDNode *> *Created) const { 2892 assert(Created && "No vector to hold udiv ops."); 2893 2894 EVT VT = N->getValueType(0); 2895 SDLoc dl(N); 2896 auto &DL = DAG.getDataLayout(); 2897 2898 // Check to see if we can do this. 2899 // FIXME: We should be more aggressive here. 2900 if (!isTypeLegal(VT)) 2901 return SDValue(); 2902 2903 // FIXME: We should use a narrower constant when the upper 2904 // bits are known to be zero. 2905 APInt::mu magics = Divisor.magicu(); 2906 2907 SDValue Q = N->getOperand(0); 2908 2909 // If the divisor is even, we can avoid using the expensive fixup by shifting 2910 // the divided value upfront. 2911 if (magics.a != 0 && !Divisor[0]) { 2912 unsigned Shift = Divisor.countTrailingZeros(); 2913 Q = DAG.getNode( 2914 ISD::SRL, dl, VT, Q, 2915 DAG.getConstant(Shift, dl, getShiftAmountTy(Q.getValueType(), DL))); 2916 Created->push_back(Q.getNode()); 2917 2918 // Get magic number for the shifted divisor. 2919 magics = Divisor.lshr(Shift).magicu(Shift); 2920 assert(magics.a == 0 && "Should use cheap fixup now"); 2921 } 2922 2923 // Multiply the numerator (operand 0) by the magic value 2924 // FIXME: We should support doing a MUL in a wider type 2925 if (IsAfterLegalization ? isOperationLegal(ISD::MULHU, VT) : 2926 isOperationLegalOrCustom(ISD::MULHU, VT)) 2927 Q = DAG.getNode(ISD::MULHU, dl, VT, Q, DAG.getConstant(magics.m, dl, VT)); 2928 else if (IsAfterLegalization ? isOperationLegal(ISD::UMUL_LOHI, VT) : 2929 isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) 2930 Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), Q, 2931 DAG.getConstant(magics.m, dl, VT)).getNode(), 1); 2932 else 2933 return SDValue(); // No mulhu or equvialent 2934 2935 Created->push_back(Q.getNode()); 2936 2937 if (magics.a == 0) { 2938 assert(magics.s < Divisor.getBitWidth() && 2939 "We shouldn't generate an undefined shift!"); 2940 return DAG.getNode( 2941 ISD::SRL, dl, VT, Q, 2942 DAG.getConstant(magics.s, dl, getShiftAmountTy(Q.getValueType(), DL))); 2943 } else { 2944 SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q); 2945 Created->push_back(NPQ.getNode()); 2946 NPQ = DAG.getNode( 2947 ISD::SRL, dl, VT, NPQ, 2948 DAG.getConstant(1, dl, getShiftAmountTy(NPQ.getValueType(), DL))); 2949 Created->push_back(NPQ.getNode()); 2950 NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q); 2951 Created->push_back(NPQ.getNode()); 2952 return DAG.getNode( 2953 ISD::SRL, dl, VT, NPQ, 2954 DAG.getConstant(magics.s - 1, dl, 2955 getShiftAmountTy(NPQ.getValueType(), DL))); 2956 } 2957 } 2958 2959 bool TargetLowering:: 2960 verifyReturnAddressArgumentIsConstant(SDValue Op, SelectionDAG &DAG) const { 2961 if (!isa<ConstantSDNode>(Op.getOperand(0))) { 2962 DAG.getContext()->emitError("argument to '__builtin_return_address' must " 2963 "be a constant integer"); 2964 return true; 2965 } 2966 2967 return false; 2968 } 2969 2970 //===----------------------------------------------------------------------===// 2971 // Legalization Utilities 2972 //===----------------------------------------------------------------------===// 2973 2974 bool TargetLowering::expandMUL(SDNode *N, SDValue &Lo, SDValue &Hi, EVT HiLoVT, 2975 SelectionDAG &DAG, SDValue LL, SDValue LH, 2976 SDValue RL, SDValue RH) const { 2977 EVT VT = N->getValueType(0); 2978 SDLoc dl(N); 2979 2980 bool HasMULHS = isOperationLegalOrCustom(ISD::MULHS, HiLoVT); 2981 bool HasMULHU = isOperationLegalOrCustom(ISD::MULHU, HiLoVT); 2982 bool HasSMUL_LOHI = isOperationLegalOrCustom(ISD::SMUL_LOHI, HiLoVT); 2983 bool HasUMUL_LOHI = isOperationLegalOrCustom(ISD::UMUL_LOHI, HiLoVT); 2984 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) { 2985 unsigned OuterBitSize = VT.getSizeInBits(); 2986 unsigned InnerBitSize = HiLoVT.getSizeInBits(); 2987 unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0)); 2988 unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1)); 2989 2990 // LL, LH, RL, and RH must be either all NULL or all set to a value. 2991 assert((LL.getNode() && LH.getNode() && RL.getNode() && RH.getNode()) || 2992 (!LL.getNode() && !LH.getNode() && !RL.getNode() && !RH.getNode())); 2993 2994 if (!LL.getNode() && !RL.getNode() && 2995 isOperationLegalOrCustom(ISD::TRUNCATE, HiLoVT)) { 2996 LL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, N->getOperand(0)); 2997 RL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, N->getOperand(1)); 2998 } 2999 3000 if (!LL.getNode()) 3001 return false; 3002 3003 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize); 3004 if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) && 3005 DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) { 3006 // The inputs are both zero-extended. 3007 if (HasUMUL_LOHI) { 3008 // We can emit a umul_lohi. 3009 Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(HiLoVT, HiLoVT), LL, 3010 RL); 3011 Hi = SDValue(Lo.getNode(), 1); 3012 return true; 3013 } 3014 if (HasMULHU) { 3015 // We can emit a mulhu+mul. 3016 Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RL); 3017 Hi = DAG.getNode(ISD::MULHU, dl, HiLoVT, LL, RL); 3018 return true; 3019 } 3020 } 3021 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) { 3022 // The input values are both sign-extended. 3023 if (HasSMUL_LOHI) { 3024 // We can emit a smul_lohi. 3025 Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(HiLoVT, HiLoVT), LL, 3026 RL); 3027 Hi = SDValue(Lo.getNode(), 1); 3028 return true; 3029 } 3030 if (HasMULHS) { 3031 // We can emit a mulhs+mul. 3032 Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RL); 3033 Hi = DAG.getNode(ISD::MULHS, dl, HiLoVT, LL, RL); 3034 return true; 3035 } 3036 } 3037 3038 if (!LH.getNode() && !RH.getNode() && 3039 isOperationLegalOrCustom(ISD::SRL, VT) && 3040 isOperationLegalOrCustom(ISD::TRUNCATE, HiLoVT)) { 3041 auto &DL = DAG.getDataLayout(); 3042 unsigned ShiftAmt = VT.getSizeInBits() - HiLoVT.getSizeInBits(); 3043 SDValue Shift = DAG.getConstant(ShiftAmt, dl, getShiftAmountTy(VT, DL)); 3044 LH = DAG.getNode(ISD::SRL, dl, VT, N->getOperand(0), Shift); 3045 LH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, LH); 3046 RH = DAG.getNode(ISD::SRL, dl, VT, N->getOperand(1), Shift); 3047 RH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, RH); 3048 } 3049 3050 if (!LH.getNode()) 3051 return false; 3052 3053 if (HasUMUL_LOHI) { 3054 // Lo,Hi = umul LHS, RHS. 3055 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl, 3056 DAG.getVTList(HiLoVT, HiLoVT), LL, RL); 3057 Lo = UMulLOHI; 3058 Hi = UMulLOHI.getValue(1); 3059 RH = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RH); 3060 LH = DAG.getNode(ISD::MUL, dl, HiLoVT, LH, RL); 3061 Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, RH); 3062 Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, LH); 3063 return true; 3064 } 3065 if (HasMULHU) { 3066 Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RL); 3067 Hi = DAG.getNode(ISD::MULHU, dl, HiLoVT, LL, RL); 3068 RH = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RH); 3069 LH = DAG.getNode(ISD::MUL, dl, HiLoVT, LH, RL); 3070 Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, RH); 3071 Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, LH); 3072 return true; 3073 } 3074 } 3075 return false; 3076 } 3077 3078 bool TargetLowering::expandFP_TO_SINT(SDNode *Node, SDValue &Result, 3079 SelectionDAG &DAG) const { 3080 EVT VT = Node->getOperand(0).getValueType(); 3081 EVT NVT = Node->getValueType(0); 3082 SDLoc dl(SDValue(Node, 0)); 3083 3084 // FIXME: Only f32 to i64 conversions are supported. 3085 if (VT != MVT::f32 || NVT != MVT::i64) 3086 return false; 3087 3088 // Expand f32 -> i64 conversion 3089 // This algorithm comes from compiler-rt's implementation of fixsfdi: 3090 // https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/fixsfdi.c 3091 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), 3092 VT.getSizeInBits()); 3093 SDValue ExponentMask = DAG.getConstant(0x7F800000, dl, IntVT); 3094 SDValue ExponentLoBit = DAG.getConstant(23, dl, IntVT); 3095 SDValue Bias = DAG.getConstant(127, dl, IntVT); 3096 SDValue SignMask = DAG.getConstant(APInt::getSignBit(VT.getSizeInBits()), dl, 3097 IntVT); 3098 SDValue SignLowBit = DAG.getConstant(VT.getSizeInBits() - 1, dl, IntVT); 3099 SDValue MantissaMask = DAG.getConstant(0x007FFFFF, dl, IntVT); 3100 3101 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, IntVT, Node->getOperand(0)); 3102 3103 auto &DL = DAG.getDataLayout(); 3104 SDValue ExponentBits = DAG.getNode( 3105 ISD::SRL, dl, IntVT, DAG.getNode(ISD::AND, dl, IntVT, Bits, ExponentMask), 3106 DAG.getZExtOrTrunc(ExponentLoBit, dl, getShiftAmountTy(IntVT, DL))); 3107 SDValue Exponent = DAG.getNode(ISD::SUB, dl, IntVT, ExponentBits, Bias); 3108 3109 SDValue Sign = DAG.getNode( 3110 ISD::SRA, dl, IntVT, DAG.getNode(ISD::AND, dl, IntVT, Bits, SignMask), 3111 DAG.getZExtOrTrunc(SignLowBit, dl, getShiftAmountTy(IntVT, DL))); 3112 Sign = DAG.getSExtOrTrunc(Sign, dl, NVT); 3113 3114 SDValue R = DAG.getNode(ISD::OR, dl, IntVT, 3115 DAG.getNode(ISD::AND, dl, IntVT, Bits, MantissaMask), 3116 DAG.getConstant(0x00800000, dl, IntVT)); 3117 3118 R = DAG.getZExtOrTrunc(R, dl, NVT); 3119 3120 R = DAG.getSelectCC( 3121 dl, Exponent, ExponentLoBit, 3122 DAG.getNode(ISD::SHL, dl, NVT, R, 3123 DAG.getZExtOrTrunc( 3124 DAG.getNode(ISD::SUB, dl, IntVT, Exponent, ExponentLoBit), 3125 dl, getShiftAmountTy(IntVT, DL))), 3126 DAG.getNode(ISD::SRL, dl, NVT, R, 3127 DAG.getZExtOrTrunc( 3128 DAG.getNode(ISD::SUB, dl, IntVT, ExponentLoBit, Exponent), 3129 dl, getShiftAmountTy(IntVT, DL))), 3130 ISD::SETGT); 3131 3132 SDValue Ret = DAG.getNode(ISD::SUB, dl, NVT, 3133 DAG.getNode(ISD::XOR, dl, NVT, R, Sign), 3134 Sign); 3135 3136 Result = DAG.getSelectCC(dl, Exponent, DAG.getConstant(0, dl, IntVT), 3137 DAG.getConstant(0, dl, NVT), Ret, ISD::SETLT); 3138 return true; 3139 } 3140 3141 SDValue TargetLowering::scalarizeVectorLoad(LoadSDNode *LD, 3142 SelectionDAG &DAG) const { 3143 SDLoc SL(LD); 3144 SDValue Chain = LD->getChain(); 3145 SDValue BasePTR = LD->getBasePtr(); 3146 EVT SrcVT = LD->getMemoryVT(); 3147 ISD::LoadExtType ExtType = LD->getExtensionType(); 3148 3149 unsigned NumElem = SrcVT.getVectorNumElements(); 3150 3151 EVT SrcEltVT = SrcVT.getScalarType(); 3152 EVT DstEltVT = LD->getValueType(0).getScalarType(); 3153 3154 unsigned Stride = SrcEltVT.getSizeInBits() / 8; 3155 assert(SrcEltVT.isByteSized()); 3156 3157 EVT PtrVT = BasePTR.getValueType(); 3158 3159 SmallVector<SDValue, 8> Vals; 3160 SmallVector<SDValue, 8> LoadChains; 3161 3162 for (unsigned Idx = 0; Idx < NumElem; ++Idx) { 3163 SDValue ScalarLoad = DAG.getExtLoad( 3164 ExtType, SL, DstEltVT, 3165 Chain, BasePTR, LD->getPointerInfo().getWithOffset(Idx * Stride), 3166 SrcEltVT, 3167 LD->isVolatile(), LD->isNonTemporal(), LD->isInvariant(), 3168 MinAlign(LD->getAlignment(), Idx * Stride), LD->getAAInfo()); 3169 3170 BasePTR = DAG.getNode(ISD::ADD, SL, PtrVT, BasePTR, 3171 DAG.getConstant(Stride, SL, PtrVT)); 3172 3173 Vals.push_back(ScalarLoad.getValue(0)); 3174 LoadChains.push_back(ScalarLoad.getValue(1)); 3175 } 3176 3177 SDValue NewChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoadChains); 3178 SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, SL, LD->getValueType(0), Vals); 3179 3180 return DAG.getMergeValues({ Value, NewChain }, SL); 3181 } 3182 3183 // FIXME: This relies on each element having a byte size, otherwise the stride 3184 // is 0 and just overwrites the same location. ExpandStore currently expects 3185 // this broken behavior. 3186 SDValue TargetLowering::scalarizeVectorStore(StoreSDNode *ST, 3187 SelectionDAG &DAG) const { 3188 SDLoc SL(ST); 3189 3190 SDValue Chain = ST->getChain(); 3191 SDValue BasePtr = ST->getBasePtr(); 3192 SDValue Value = ST->getValue(); 3193 EVT StVT = ST->getMemoryVT(); 3194 3195 unsigned Alignment = ST->getAlignment(); 3196 bool isVolatile = ST->isVolatile(); 3197 bool isNonTemporal = ST->isNonTemporal(); 3198 AAMDNodes AAInfo = ST->getAAInfo(); 3199 3200 // The type of the data we want to save 3201 EVT RegVT = Value.getValueType(); 3202 EVT RegSclVT = RegVT.getScalarType(); 3203 3204 // The type of data as saved in memory. 3205 EVT MemSclVT = StVT.getScalarType(); 3206 3207 EVT PtrVT = BasePtr.getValueType(); 3208 3209 // Store Stride in bytes 3210 unsigned Stride = MemSclVT.getSizeInBits() / 8; 3211 EVT IdxVT = getVectorIdxTy(DAG.getDataLayout()); 3212 unsigned NumElem = StVT.getVectorNumElements(); 3213 3214 // Extract each of the elements from the original vector and save them into 3215 // memory individually. 3216 SmallVector<SDValue, 8> Stores; 3217 for (unsigned Idx = 0; Idx < NumElem; ++Idx) { 3218 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, RegSclVT, Value, 3219 DAG.getConstant(Idx, SL, IdxVT)); 3220 3221 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr, 3222 DAG.getConstant(Idx * Stride, SL, PtrVT)); 3223 3224 // This scalar TruncStore may be illegal, but we legalize it later. 3225 SDValue Store = DAG.getTruncStore( 3226 Chain, SL, Elt, Ptr, 3227 ST->getPointerInfo().getWithOffset(Idx * Stride), MemSclVT, 3228 isVolatile, isNonTemporal, MinAlign(Alignment, Idx * Stride), 3229 AAInfo); 3230 3231 Stores.push_back(Store); 3232 } 3233 3234 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, Stores); 3235 } 3236 3237 std::pair<SDValue, SDValue> 3238 TargetLowering::expandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG) const { 3239 assert(LD->getAddressingMode() == ISD::UNINDEXED && 3240 "unaligned indexed loads not implemented!"); 3241 SDValue Chain = LD->getChain(); 3242 SDValue Ptr = LD->getBasePtr(); 3243 EVT VT = LD->getValueType(0); 3244 EVT LoadedVT = LD->getMemoryVT(); 3245 SDLoc dl(LD); 3246 if (VT.isFloatingPoint() || VT.isVector()) { 3247 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits()); 3248 if (isTypeLegal(intVT) && isTypeLegal(LoadedVT)) { 3249 if (!isOperationLegalOrCustom(ISD::LOAD, intVT)) { 3250 // Scalarize the load and let the individual components be handled. 3251 SDValue Scalarized = scalarizeVectorLoad(LD, DAG); 3252 return std::make_pair(Scalarized.getValue(0), Scalarized.getValue(1)); 3253 } 3254 3255 // Expand to a (misaligned) integer load of the same size, 3256 // then bitconvert to floating point or vector. 3257 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, 3258 LD->getMemOperand()); 3259 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad); 3260 if (LoadedVT != VT) 3261 Result = DAG.getNode(VT.isFloatingPoint() ? ISD::FP_EXTEND : 3262 ISD::ANY_EXTEND, dl, VT, Result); 3263 3264 return std::make_pair(Result, newLoad.getValue(1)); 3265 } 3266 3267 // Copy the value to a (aligned) stack slot using (unaligned) integer 3268 // loads and stores, then do a (aligned) load from the stack slot. 3269 MVT RegVT = getRegisterType(*DAG.getContext(), intVT); 3270 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8; 3271 unsigned RegBytes = RegVT.getSizeInBits() / 8; 3272 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes; 3273 3274 // Make sure the stack slot is also aligned for the register type. 3275 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT); 3276 3277 SmallVector<SDValue, 8> Stores; 3278 SDValue StackPtr = StackBase; 3279 unsigned Offset = 0; 3280 3281 EVT PtrVT = Ptr.getValueType(); 3282 EVT StackPtrVT = StackPtr.getValueType(); 3283 3284 SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT); 3285 SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT); 3286 3287 // Do all but one copies using the full register width. 3288 for (unsigned i = 1; i < NumRegs; i++) { 3289 // Load one integer register's worth from the original location. 3290 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, 3291 LD->getPointerInfo().getWithOffset(Offset), 3292 LD->isVolatile(), LD->isNonTemporal(), 3293 LD->isInvariant(), 3294 MinAlign(LD->getAlignment(), Offset), 3295 LD->getAAInfo()); 3296 // Follow the load with a store to the stack slot. Remember the store. 3297 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr, 3298 MachinePointerInfo(), false, false, 0)); 3299 // Increment the pointers. 3300 Offset += RegBytes; 3301 Ptr = DAG.getNode(ISD::ADD, dl, PtrVT, Ptr, PtrIncrement); 3302 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtrVT, StackPtr, 3303 StackPtrIncrement); 3304 } 3305 3306 // The last copy may be partial. Do an extending load. 3307 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), 3308 8 * (LoadedBytes - Offset)); 3309 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr, 3310 LD->getPointerInfo().getWithOffset(Offset), 3311 MemVT, LD->isVolatile(), 3312 LD->isNonTemporal(), 3313 LD->isInvariant(), 3314 MinAlign(LD->getAlignment(), Offset), 3315 LD->getAAInfo()); 3316 // Follow the load with a store to the stack slot. Remember the store. 3317 // On big-endian machines this requires a truncating store to ensure 3318 // that the bits end up in the right place. 3319 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr, 3320 MachinePointerInfo(), MemVT, 3321 false, false, 0)); 3322 3323 // The order of the stores doesn't matter - say it with a TokenFactor. 3324 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 3325 3326 // Finally, perform the original load only redirected to the stack slot. 3327 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase, 3328 MachinePointerInfo(), LoadedVT, false,false, false, 3329 0); 3330 3331 // Callers expect a MERGE_VALUES node. 3332 return std::make_pair(Load, TF); 3333 } 3334 3335 assert(LoadedVT.isInteger() && !LoadedVT.isVector() && 3336 "Unaligned load of unsupported type."); 3337 3338 // Compute the new VT that is half the size of the old one. This is an 3339 // integer MVT. 3340 unsigned NumBits = LoadedVT.getSizeInBits(); 3341 EVT NewLoadedVT; 3342 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2); 3343 NumBits >>= 1; 3344 3345 unsigned Alignment = LD->getAlignment(); 3346 unsigned IncrementSize = NumBits / 8; 3347 ISD::LoadExtType HiExtType = LD->getExtensionType(); 3348 3349 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD. 3350 if (HiExtType == ISD::NON_EXTLOAD) 3351 HiExtType = ISD::ZEXTLOAD; 3352 3353 // Load the value in two parts 3354 SDValue Lo, Hi; 3355 if (DAG.getDataLayout().isLittleEndian()) { 3356 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(), 3357 NewLoadedVT, LD->isVolatile(), 3358 LD->isNonTemporal(), LD->isInvariant(), Alignment, 3359 LD->getAAInfo()); 3360 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 3361 DAG.getConstant(IncrementSize, dl, Ptr.getValueType())); 3362 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, 3363 LD->getPointerInfo().getWithOffset(IncrementSize), 3364 NewLoadedVT, LD->isVolatile(), 3365 LD->isNonTemporal(),LD->isInvariant(), 3366 MinAlign(Alignment, IncrementSize), LD->getAAInfo()); 3367 } else { 3368 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(), 3369 NewLoadedVT, LD->isVolatile(), 3370 LD->isNonTemporal(), LD->isInvariant(), Alignment, 3371 LD->getAAInfo()); 3372 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 3373 DAG.getConstant(IncrementSize, dl, Ptr.getValueType())); 3374 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, 3375 LD->getPointerInfo().getWithOffset(IncrementSize), 3376 NewLoadedVT, LD->isVolatile(), 3377 LD->isNonTemporal(), LD->isInvariant(), 3378 MinAlign(Alignment, IncrementSize), LD->getAAInfo()); 3379 } 3380 3381 // aggregate the two parts 3382 SDValue ShiftAmount = 3383 DAG.getConstant(NumBits, dl, getShiftAmountTy(Hi.getValueType(), 3384 DAG.getDataLayout())); 3385 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount); 3386 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo); 3387 3388 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 3389 Hi.getValue(1)); 3390 3391 return std::make_pair(Result, TF); 3392 } 3393 3394 SDValue TargetLowering::expandUnalignedStore(StoreSDNode *ST, 3395 SelectionDAG &DAG) const { 3396 assert(ST->getAddressingMode() == ISD::UNINDEXED && 3397 "unaligned indexed stores not implemented!"); 3398 SDValue Chain = ST->getChain(); 3399 SDValue Ptr = ST->getBasePtr(); 3400 SDValue Val = ST->getValue(); 3401 EVT VT = Val.getValueType(); 3402 int Alignment = ST->getAlignment(); 3403 3404 SDLoc dl(ST); 3405 if (ST->getMemoryVT().isFloatingPoint() || 3406 ST->getMemoryVT().isVector()) { 3407 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); 3408 if (isTypeLegal(intVT)) { 3409 if (!isOperationLegalOrCustom(ISD::STORE, intVT)) { 3410 // Scalarize the store and let the individual components be handled. 3411 SDValue Result = scalarizeVectorStore(ST, DAG); 3412 3413 return Result; 3414 } 3415 // Expand to a bitconvert of the value to the integer type of the 3416 // same size, then a (misaligned) int store. 3417 // FIXME: Does not handle truncating floating point stores! 3418 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val); 3419 Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(), 3420 ST->isVolatile(), ST->isNonTemporal(), Alignment); 3421 return Result; 3422 } 3423 // Do a (aligned) store to a stack slot, then copy from the stack slot 3424 // to the final destination using (unaligned) integer loads and stores. 3425 EVT StoredVT = ST->getMemoryVT(); 3426 MVT RegVT = 3427 getRegisterType(*DAG.getContext(), 3428 EVT::getIntegerVT(*DAG.getContext(), 3429 StoredVT.getSizeInBits())); 3430 EVT PtrVT = Ptr.getValueType(); 3431 unsigned StoredBytes = StoredVT.getSizeInBits() / 8; 3432 unsigned RegBytes = RegVT.getSizeInBits() / 8; 3433 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes; 3434 3435 // Make sure the stack slot is also aligned for the register type. 3436 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT); 3437 3438 // Perform the original store, only redirected to the stack slot. 3439 SDValue Store = DAG.getTruncStore(Chain, dl, 3440 Val, StackPtr, MachinePointerInfo(), 3441 StoredVT, false, false, 0); 3442 3443 EVT StackPtrVT = StackPtr.getValueType(); 3444 3445 SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT); 3446 SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT); 3447 SmallVector<SDValue, 8> Stores; 3448 unsigned Offset = 0; 3449 3450 // Do all but one copies using the full register width. 3451 for (unsigned i = 1; i < NumRegs; i++) { 3452 // Load one integer register's worth from the stack slot. 3453 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, 3454 MachinePointerInfo(), 3455 false, false, false, 0); 3456 // Store it to the final location. Remember the store. 3457 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr, 3458 ST->getPointerInfo().getWithOffset(Offset), 3459 ST->isVolatile(), ST->isNonTemporal(), 3460 MinAlign(ST->getAlignment(), Offset))); 3461 // Increment the pointers. 3462 Offset += RegBytes; 3463 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtrVT, 3464 StackPtr, StackPtrIncrement); 3465 Ptr = DAG.getNode(ISD::ADD, dl, PtrVT, Ptr, PtrIncrement); 3466 } 3467 3468 // The last store may be partial. Do a truncating store. On big-endian 3469 // machines this requires an extending load from the stack slot to ensure 3470 // that the bits are in the right place. 3471 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), 3472 8 * (StoredBytes - Offset)); 3473 3474 // Load from the stack slot. 3475 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr, 3476 MachinePointerInfo(), 3477 MemVT, false, false, false, 0); 3478 3479 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr, 3480 ST->getPointerInfo() 3481 .getWithOffset(Offset), 3482 MemVT, ST->isVolatile(), 3483 ST->isNonTemporal(), 3484 MinAlign(ST->getAlignment(), Offset), 3485 ST->getAAInfo())); 3486 // The order of the stores doesn't matter - say it with a TokenFactor. 3487 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 3488 return Result; 3489 } 3490 3491 assert(ST->getMemoryVT().isInteger() && 3492 !ST->getMemoryVT().isVector() && 3493 "Unaligned store of unknown type."); 3494 // Get the half-size VT 3495 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext()); 3496 int NumBits = NewStoredVT.getSizeInBits(); 3497 int IncrementSize = NumBits / 8; 3498 3499 // Divide the stored value in two parts. 3500 SDValue ShiftAmount = 3501 DAG.getConstant(NumBits, dl, getShiftAmountTy(Val.getValueType(), 3502 DAG.getDataLayout())); 3503 SDValue Lo = Val; 3504 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount); 3505 3506 // Store the two parts 3507 SDValue Store1, Store2; 3508 Store1 = DAG.getTruncStore(Chain, dl, 3509 DAG.getDataLayout().isLittleEndian() ? Lo : Hi, 3510 Ptr, ST->getPointerInfo(), NewStoredVT, 3511 ST->isVolatile(), ST->isNonTemporal(), Alignment); 3512 3513 EVT PtrVT = Ptr.getValueType(); 3514 Ptr = DAG.getNode(ISD::ADD, dl, PtrVT, Ptr, 3515 DAG.getConstant(IncrementSize, dl, PtrVT)); 3516 Alignment = MinAlign(Alignment, IncrementSize); 3517 Store2 = DAG.getTruncStore( 3518 Chain, dl, DAG.getDataLayout().isLittleEndian() ? Hi : Lo, Ptr, 3519 ST->getPointerInfo().getWithOffset(IncrementSize), NewStoredVT, 3520 ST->isVolatile(), ST->isNonTemporal(), Alignment, ST->getAAInfo()); 3521 3522 SDValue Result = 3523 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2); 3524 return Result; 3525 } 3526 3527 //===----------------------------------------------------------------------===// 3528 // Implementation of Emulated TLS Model 3529 //===----------------------------------------------------------------------===// 3530 3531 SDValue TargetLowering::LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA, 3532 SelectionDAG &DAG) const { 3533 // Access to address of TLS varialbe xyz is lowered to a function call: 3534 // __emutls_get_address( address of global variable named "__emutls_v.xyz" ) 3535 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 3536 PointerType *VoidPtrType = Type::getInt8PtrTy(*DAG.getContext()); 3537 SDLoc dl(GA); 3538 3539 ArgListTy Args; 3540 ArgListEntry Entry; 3541 std::string NameString = ("__emutls_v." + GA->getGlobal()->getName()).str(); 3542 Module *VariableModule = const_cast<Module*>(GA->getGlobal()->getParent()); 3543 StringRef EmuTlsVarName(NameString); 3544 GlobalVariable *EmuTlsVar = VariableModule->getNamedGlobal(EmuTlsVarName); 3545 assert(EmuTlsVar && "Cannot find EmuTlsVar "); 3546 Entry.Node = DAG.getGlobalAddress(EmuTlsVar, dl, PtrVT); 3547 Entry.Ty = VoidPtrType; 3548 Args.push_back(Entry); 3549 3550 SDValue EmuTlsGetAddr = DAG.getExternalSymbol("__emutls_get_address", PtrVT); 3551 3552 TargetLowering::CallLoweringInfo CLI(DAG); 3553 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode()); 3554 CLI.setCallee(CallingConv::C, VoidPtrType, EmuTlsGetAddr, std::move(Args), 0); 3555 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 3556 3557 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls. 3558 // At last for X86 targets, maybe good for other targets too? 3559 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 3560 MFI->setAdjustsStack(true); // Is this only for X86 target? 3561 MFI->setHasCalls(true); 3562 3563 assert((GA->getOffset() == 0) && 3564 "Emulated TLS must have zero offset in GlobalAddressSDNode"); 3565 return CallResult.first; 3566 } 3567