1 //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the TargetLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/TargetLowering.h"
15 #include "llvm/ADT/BitVector.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/CodeGen/CallingConvLower.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineJumpTableInfo.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/TargetLoweringObjectFile.h"
24 #include "llvm/CodeGen/TargetRegisterInfo.h"
25 #include "llvm/CodeGen/TargetSubtargetInfo.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCExpr.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/KnownBits.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/Target/TargetMachine.h"
39 /// NOTE: The TargetMachine owns TLOF.
40 TargetLowering::TargetLowering(const TargetMachine &tm)
41 : TargetLoweringBase(tm) {}
43 const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
47 bool TargetLowering::isPositionIndependent() const {
48 return getTargetMachine().isPositionIndependent();
51 /// Check whether a given call node is in tail position within its function. If
52 /// so, it sets Chain to the input chain of the tail call.
53 bool TargetLowering::isInTailCallPosition(SelectionDAG &DAG, SDNode *Node,
54 SDValue &Chain) const {
55 const Function &F = DAG.getMachineFunction().getFunction();
57 // Conservatively require the attributes of the call to match those of
58 // the return. Ignore noalias because it doesn't affect the call sequence.
59 AttributeList CallerAttrs = F.getAttributes();
60 if (AttrBuilder(CallerAttrs, AttributeList::ReturnIndex)
61 .removeAttribute(Attribute::NoAlias)
65 // It's not safe to eliminate the sign / zero extension of the return value.
66 if (CallerAttrs.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt) ||
67 CallerAttrs.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
70 // Check if the only use is a function return node.
71 return isUsedByReturnOnly(Node, Chain);
74 bool TargetLowering::parametersInCSRMatch(const MachineRegisterInfo &MRI,
75 const uint32_t *CallerPreservedMask,
76 const SmallVectorImpl<CCValAssign> &ArgLocs,
77 const SmallVectorImpl<SDValue> &OutVals) const {
78 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
79 const CCValAssign &ArgLoc = ArgLocs[I];
80 if (!ArgLoc.isRegLoc())
82 unsigned Reg = ArgLoc.getLocReg();
83 // Only look at callee saved registers.
84 if (MachineOperand::clobbersPhysReg(CallerPreservedMask, Reg))
86 // Check that we pass the value used for the caller.
87 // (We look for a CopyFromReg reading a virtual register that is used
88 // for the function live-in value of register Reg)
89 SDValue Value = OutVals[I];
90 if (Value->getOpcode() != ISD::CopyFromReg)
92 unsigned ArgReg = cast<RegisterSDNode>(Value->getOperand(1))->getReg();
93 if (MRI.getLiveInPhysReg(ArgReg) != Reg)
99 /// \brief Set CallLoweringInfo attribute flags based on a call instruction
100 /// and called function attributes.
101 void TargetLoweringBase::ArgListEntry::setAttributes(ImmutableCallSite *CS,
103 IsSExt = CS->paramHasAttr(ArgIdx, Attribute::SExt);
104 IsZExt = CS->paramHasAttr(ArgIdx, Attribute::ZExt);
105 IsInReg = CS->paramHasAttr(ArgIdx, Attribute::InReg);
106 IsSRet = CS->paramHasAttr(ArgIdx, Attribute::StructRet);
107 IsNest = CS->paramHasAttr(ArgIdx, Attribute::Nest);
108 IsByVal = CS->paramHasAttr(ArgIdx, Attribute::ByVal);
109 IsInAlloca = CS->paramHasAttr(ArgIdx, Attribute::InAlloca);
110 IsReturned = CS->paramHasAttr(ArgIdx, Attribute::Returned);
111 IsSwiftSelf = CS->paramHasAttr(ArgIdx, Attribute::SwiftSelf);
112 IsSwiftError = CS->paramHasAttr(ArgIdx, Attribute::SwiftError);
113 Alignment = CS->getParamAlignment(ArgIdx);
116 /// Generate a libcall taking the given operands as arguments and returning a
117 /// result of type RetVT.
118 std::pair<SDValue, SDValue>
119 TargetLowering::makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT,
120 ArrayRef<SDValue> Ops, bool isSigned,
121 const SDLoc &dl, bool doesNotReturn,
122 bool isReturnValueUsed) const {
123 TargetLowering::ArgListTy Args;
124 Args.reserve(Ops.size());
126 TargetLowering::ArgListEntry Entry;
127 for (SDValue Op : Ops) {
129 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
130 Entry.IsSExt = shouldSignExtendTypeInLibCall(Op.getValueType(), isSigned);
131 Entry.IsZExt = !shouldSignExtendTypeInLibCall(Op.getValueType(), isSigned);
132 Args.push_back(Entry);
135 if (LC == RTLIB::UNKNOWN_LIBCALL)
136 report_fatal_error("Unsupported library call operation!");
137 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
138 getPointerTy(DAG.getDataLayout()));
140 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
141 TargetLowering::CallLoweringInfo CLI(DAG);
142 bool signExtend = shouldSignExtendTypeInLibCall(RetVT, isSigned);
144 .setChain(DAG.getEntryNode())
145 .setLibCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
146 .setNoReturn(doesNotReturn)
147 .setDiscardResult(!isReturnValueUsed)
148 .setSExtResult(signExtend)
149 .setZExtResult(!signExtend);
150 return LowerCallTo(CLI);
153 /// Soften the operands of a comparison. This code is shared among BR_CC,
154 /// SELECT_CC, and SETCC handlers.
155 void TargetLowering::softenSetCCOperands(SelectionDAG &DAG, EVT VT,
156 SDValue &NewLHS, SDValue &NewRHS,
157 ISD::CondCode &CCCode,
158 const SDLoc &dl) const {
159 assert((VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128 || VT == MVT::ppcf128)
160 && "Unsupported setcc type!");
162 // Expand into one or more soft-fp libcall(s).
163 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
164 bool ShouldInvertCC = false;
168 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
169 (VT == MVT::f64) ? RTLIB::OEQ_F64 :
170 (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128;
174 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 :
175 (VT == MVT::f64) ? RTLIB::UNE_F64 :
176 (VT == MVT::f128) ? RTLIB::UNE_F128 : RTLIB::UNE_PPCF128;
180 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
181 (VT == MVT::f64) ? RTLIB::OGE_F64 :
182 (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128;
186 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
187 (VT == MVT::f64) ? RTLIB::OLT_F64 :
188 (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128;
192 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
193 (VT == MVT::f64) ? RTLIB::OLE_F64 :
194 (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128;
198 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
199 (VT == MVT::f64) ? RTLIB::OGT_F64 :
200 (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128;
203 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
204 (VT == MVT::f64) ? RTLIB::UO_F64 :
205 (VT == MVT::f128) ? RTLIB::UO_F128 : RTLIB::UO_PPCF128;
208 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 :
209 (VT == MVT::f64) ? RTLIB::O_F64 :
210 (VT == MVT::f128) ? RTLIB::O_F128 : RTLIB::O_PPCF128;
213 // SETONE = SETOLT | SETOGT
214 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
215 (VT == MVT::f64) ? RTLIB::OLT_F64 :
216 (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128;
217 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
218 (VT == MVT::f64) ? RTLIB::OGT_F64 :
219 (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128;
222 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
223 (VT == MVT::f64) ? RTLIB::UO_F64 :
224 (VT == MVT::f128) ? RTLIB::UO_F128 : RTLIB::UO_PPCF128;
225 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
226 (VT == MVT::f64) ? RTLIB::OEQ_F64 :
227 (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128;
230 // Invert CC for unordered comparisons
231 ShouldInvertCC = true;
234 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
235 (VT == MVT::f64) ? RTLIB::OGE_F64 :
236 (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128;
239 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
240 (VT == MVT::f64) ? RTLIB::OGT_F64 :
241 (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128;
244 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
245 (VT == MVT::f64) ? RTLIB::OLE_F64 :
246 (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128;
249 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
250 (VT == MVT::f64) ? RTLIB::OLT_F64 :
251 (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128;
253 default: llvm_unreachable("Do not know how to soften this setcc!");
257 // Use the target specific return value for comparions lib calls.
258 EVT RetVT = getCmpLibcallReturnType();
259 SDValue Ops[2] = {NewLHS, NewRHS};
260 NewLHS = makeLibCall(DAG, LC1, RetVT, Ops, false /*sign irrelevant*/,
262 NewRHS = DAG.getConstant(0, dl, RetVT);
264 CCCode = getCmpLibcallCC(LC1);
266 CCCode = getSetCCInverse(CCCode, /*isInteger=*/true);
268 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
269 SDValue Tmp = DAG.getNode(
271 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), RetVT),
272 NewLHS, NewRHS, DAG.getCondCode(CCCode));
273 NewLHS = makeLibCall(DAG, LC2, RetVT, Ops, false/*sign irrelevant*/,
275 NewLHS = DAG.getNode(
277 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), RetVT),
278 NewLHS, NewRHS, DAG.getCondCode(getCmpLibcallCC(LC2)));
279 NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS);
284 /// Return the entry encoding for a jump table in the current function. The
285 /// returned value is a member of the MachineJumpTableInfo::JTEntryKind enum.
286 unsigned TargetLowering::getJumpTableEncoding() const {
287 // In non-pic modes, just use the address of a block.
288 if (!isPositionIndependent())
289 return MachineJumpTableInfo::EK_BlockAddress;
291 // In PIC mode, if the target supports a GPRel32 directive, use it.
292 if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != nullptr)
293 return MachineJumpTableInfo::EK_GPRel32BlockAddress;
295 // Otherwise, use a label difference.
296 return MachineJumpTableInfo::EK_LabelDifference32;
299 SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
300 SelectionDAG &DAG) const {
301 // If our PIC model is GP relative, use the global offset table as the base.
302 unsigned JTEncoding = getJumpTableEncoding();
304 if ((JTEncoding == MachineJumpTableInfo::EK_GPRel64BlockAddress) ||
305 (JTEncoding == MachineJumpTableInfo::EK_GPRel32BlockAddress))
306 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy(DAG.getDataLayout()));
311 /// This returns the relocation base for the given PIC jumptable, the same as
312 /// getPICJumpTableRelocBase, but as an MCExpr.
314 TargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
315 unsigned JTI,MCContext &Ctx) const{
316 // The normal PIC reloc base is the label at the start of the jump table.
317 return MCSymbolRefExpr::create(MF->getJTISymbol(JTI, Ctx), Ctx);
321 TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
322 const TargetMachine &TM = getTargetMachine();
323 const GlobalValue *GV = GA->getGlobal();
325 // If the address is not even local to this DSO we will have to load it from
326 // a got and then add the offset.
327 if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
330 // If the code is position independent we will have to add a base register.
331 if (isPositionIndependent())
334 // Otherwise we can do it.
338 //===----------------------------------------------------------------------===//
339 // Optimization Methods
340 //===----------------------------------------------------------------------===//
342 /// If the specified instruction has a constant integer operand and there are
343 /// bits set in that constant that are not demanded, then clear those bits and
345 bool TargetLowering::ShrinkDemandedConstant(SDValue Op, const APInt &Demanded,
346 TargetLoweringOpt &TLO) const {
347 SelectionDAG &DAG = TLO.DAG;
349 unsigned Opcode = Op.getOpcode();
351 // Do target-specific constant optimization.
352 if (targetShrinkDemandedConstant(Op, Demanded, TLO))
353 return TLO.New.getNode();
355 // FIXME: ISD::SELECT, ISD::SELECT_CC
362 auto *Op1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
366 // If this is a 'not' op, don't touch it because that's a canonical form.
367 const APInt &C = Op1C->getAPIntValue();
368 if (Opcode == ISD::XOR && Demanded.isSubsetOf(C))
371 if (!C.isSubsetOf(Demanded)) {
372 EVT VT = Op.getValueType();
373 SDValue NewC = DAG.getConstant(Demanded & C, DL, VT);
374 SDValue NewOp = DAG.getNode(Opcode, DL, VT, Op.getOperand(0), NewC);
375 return TLO.CombineTo(Op, NewOp);
385 /// Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the casts are free.
386 /// This uses isZExtFree and ZERO_EXTEND for the widening cast, but it could be
387 /// generalized for targets with other types of implicit widening casts.
388 bool TargetLowering::ShrinkDemandedOp(SDValue Op, unsigned BitWidth,
389 const APInt &Demanded,
390 TargetLoweringOpt &TLO) const {
391 assert(Op.getNumOperands() == 2 &&
392 "ShrinkDemandedOp only supports binary operators!");
393 assert(Op.getNode()->getNumValues() == 1 &&
394 "ShrinkDemandedOp only supports nodes with one result!");
396 SelectionDAG &DAG = TLO.DAG;
399 // Early return, as this function cannot handle vector types.
400 if (Op.getValueType().isVector())
403 // Don't do this if the node has another user, which may require the
405 if (!Op.getNode()->hasOneUse())
408 // Search for the smallest integer type with free casts to and from
409 // Op's type. For expedience, just check power-of-2 integer types.
410 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
411 unsigned DemandedSize = Demanded.getActiveBits();
412 unsigned SmallVTBits = DemandedSize;
413 if (!isPowerOf2_32(SmallVTBits))
414 SmallVTBits = NextPowerOf2(SmallVTBits);
415 for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
416 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
417 if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
418 TLI.isZExtFree(SmallVT, Op.getValueType())) {
419 // We found a type with free casts.
420 SDValue X = DAG.getNode(
421 Op.getOpcode(), dl, SmallVT,
422 DAG.getNode(ISD::TRUNCATE, dl, SmallVT, Op.getOperand(0)),
423 DAG.getNode(ISD::TRUNCATE, dl, SmallVT, Op.getOperand(1)));
424 assert(DemandedSize <= SmallVTBits && "Narrowed below demanded bits?");
425 SDValue Z = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), X);
426 return TLO.CombineTo(Op, Z);
433 TargetLowering::SimplifyDemandedBits(SDNode *User, unsigned OpIdx,
434 const APInt &Demanded,
435 DAGCombinerInfo &DCI,
436 TargetLoweringOpt &TLO) const {
437 SDValue Op = User->getOperand(OpIdx);
440 if (!SimplifyDemandedBits(Op, Demanded, Known, TLO, 0, true))
444 // Old will not always be the same as Op. For example:
446 // Demanded = 0xffffff
447 // Op = i64 truncate (i32 and x, 0xffffff)
448 // In this case simplify demand bits will want to replace the 'and' node
449 // with the value 'x', which will give us:
450 // Old = i32 and x, 0xffffff
452 if (TLO.Old.hasOneUse()) {
453 // For the one use case, we just commit the change.
454 DCI.CommitTargetLoweringOpt(TLO);
458 // If Old has more than one use then it must be Op, because the
459 // AssumeSingleUse flag is not propogated to recursive calls of
460 // SimplifyDemanded bits, so the only node with multiple use that
461 // it will attempt to combine will be Op.
462 assert(TLO.Old == Op);
464 SmallVector <SDValue, 4> NewOps;
465 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
467 NewOps.push_back(TLO.New);
470 NewOps.push_back(User->getOperand(i));
472 User = TLO.DAG.UpdateNodeOperands(User, NewOps);
473 // Op has less users now, so we may be able to perform additional combines
475 DCI.AddToWorklist(Op.getNode());
476 // User's operands have been updated, so we may be able to do new combines
478 DCI.AddToWorklist(User);
482 bool TargetLowering::SimplifyDemandedBits(SDValue Op, const APInt &DemandedMask,
483 DAGCombinerInfo &DCI) const {
485 SelectionDAG &DAG = DCI.DAG;
486 TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
487 !DCI.isBeforeLegalizeOps());
490 bool Simplified = SimplifyDemandedBits(Op, DemandedMask, Known, TLO);
492 DCI.CommitTargetLoweringOpt(TLO);
496 /// Look at Op. At this point, we know that only the DemandedMask bits of the
497 /// result of Op are ever used downstream. If we can use this information to
498 /// simplify Op, create a new simplified DAG node and return true, returning the
499 /// original and new nodes in Old and New. Otherwise, analyze the expression and
500 /// return a mask of Known bits for the expression (used to simplify the
501 /// caller). The Known bits may only be accurate for those bits in the
503 bool TargetLowering::SimplifyDemandedBits(SDValue Op,
504 const APInt &DemandedMask,
506 TargetLoweringOpt &TLO,
508 bool AssumeSingleUse) const {
509 unsigned BitWidth = DemandedMask.getBitWidth();
510 assert(Op.getScalarValueSizeInBits() == BitWidth &&
511 "Mask size mismatches value type size!");
512 APInt NewMask = DemandedMask;
514 auto &DL = TLO.DAG.getDataLayout();
516 // Don't know anything.
517 Known = KnownBits(BitWidth);
519 if (Op.getOpcode() == ISD::Constant) {
520 // We know all of the bits for a constant!
521 Known.One = cast<ConstantSDNode>(Op)->getAPIntValue();
522 Known.Zero = ~Known.One;
526 // Other users may use these bits.
527 if (!Op.getNode()->hasOneUse() && !AssumeSingleUse) {
529 // If not at the root, Just compute the Known bits to
530 // simplify things downstream.
531 TLO.DAG.computeKnownBits(Op, Known, Depth);
534 // If this is the root being simplified, allow it to have multiple uses,
535 // just set the NewMask to all bits.
536 NewMask = APInt::getAllOnesValue(BitWidth);
537 } else if (DemandedMask == 0) {
538 // Not demanding any bits from Op.
540 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
542 } else if (Depth == 6) { // Limit search depth.
546 KnownBits Known2, KnownOut;
547 switch (Op.getOpcode()) {
548 case ISD::BUILD_VECTOR:
549 // Collect the known bits that are shared by every constant vector element.
550 Known.Zero.setAllBits(); Known.One.setAllBits();
551 for (SDValue SrcOp : Op->ops()) {
552 if (!isa<ConstantSDNode>(SrcOp)) {
553 // We can only handle all constant values - bail out with no known bits.
554 Known = KnownBits(BitWidth);
557 Known2.One = cast<ConstantSDNode>(SrcOp)->getAPIntValue();
558 Known2.Zero = ~Known2.One;
560 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
561 if (Known2.One.getBitWidth() != BitWidth) {
562 assert(Known2.getBitWidth() > BitWidth &&
563 "Expected BUILD_VECTOR implicit truncation");
564 Known2 = Known2.trunc(BitWidth);
567 // Known bits are the values that are shared by every element.
568 // TODO: support per-element known bits.
569 Known.One &= Known2.One;
570 Known.Zero &= Known2.Zero;
572 return false; // Don't fall through, will infinitely loop.
574 // If the RHS is a constant, check to see if the LHS would be zero without
575 // using the bits from the RHS. Below, we use knowledge about the RHS to
576 // simplify the LHS, here we're using information from the LHS to simplify
578 if (ConstantSDNode *RHSC = isConstOrConstSplat(Op.getOperand(1))) {
579 SDValue Op0 = Op.getOperand(0);
581 // Do not increment Depth here; that can cause an infinite loop.
582 TLO.DAG.computeKnownBits(Op0, LHSKnown, Depth);
583 // If the LHS already has zeros where RHSC does, this and is dead.
584 if ((LHSKnown.Zero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
585 return TLO.CombineTo(Op, Op0);
587 // If any of the set bits in the RHS are known zero on the LHS, shrink
589 if (ShrinkDemandedConstant(Op, ~LHSKnown.Zero & NewMask, TLO))
592 // Bitwise-not (xor X, -1) is a special case: we don't usually shrink its
593 // constant, but if this 'and' is only clearing bits that were just set by
594 // the xor, then this 'and' can be eliminated by shrinking the mask of
595 // the xor. For example, for a 32-bit X:
596 // and (xor (srl X, 31), -1), 1 --> xor (srl X, 31), 1
597 if (isBitwiseNot(Op0) && Op0.hasOneUse() &&
598 LHSKnown.One == ~RHSC->getAPIntValue()) {
599 SDValue Xor = TLO.DAG.getNode(ISD::XOR, dl, Op.getValueType(),
600 Op0.getOperand(0), Op.getOperand(1));
601 return TLO.CombineTo(Op, Xor);
605 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, Known, TLO, Depth+1))
607 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
608 if (SimplifyDemandedBits(Op.getOperand(0), ~Known.Zero & NewMask,
609 Known2, TLO, Depth+1))
611 assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
613 // If all of the demanded bits are known one on one side, return the other.
614 // These bits cannot contribute to the result of the 'and'.
615 if (NewMask.isSubsetOf(Known2.Zero | Known.One))
616 return TLO.CombineTo(Op, Op.getOperand(0));
617 if (NewMask.isSubsetOf(Known.Zero | Known2.One))
618 return TLO.CombineTo(Op, Op.getOperand(1));
619 // If all of the demanded bits in the inputs are known zeros, return zero.
620 if (NewMask.isSubsetOf(Known.Zero | Known2.Zero))
621 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, dl, Op.getValueType()));
622 // If the RHS is a constant, see if we can simplify it.
623 if (ShrinkDemandedConstant(Op, ~Known2.Zero & NewMask, TLO))
625 // If the operation can be done in a smaller type, do so.
626 if (ShrinkDemandedOp(Op, BitWidth, NewMask, TLO))
629 // Output known-1 bits are only known if set in both the LHS & RHS.
630 Known.One &= Known2.One;
631 // Output known-0 are known to be clear if zero in either the LHS | RHS.
632 Known.Zero |= Known2.Zero;
635 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, Known, TLO, Depth+1))
637 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
638 if (SimplifyDemandedBits(Op.getOperand(0), ~Known.One & NewMask,
639 Known2, TLO, Depth+1))
641 assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
643 // If all of the demanded bits are known zero on one side, return the other.
644 // These bits cannot contribute to the result of the 'or'.
645 if (NewMask.isSubsetOf(Known2.One | Known.Zero))
646 return TLO.CombineTo(Op, Op.getOperand(0));
647 if (NewMask.isSubsetOf(Known.One | Known2.Zero))
648 return TLO.CombineTo(Op, Op.getOperand(1));
649 // If the RHS is a constant, see if we can simplify it.
650 if (ShrinkDemandedConstant(Op, NewMask, TLO))
652 // If the operation can be done in a smaller type, do so.
653 if (ShrinkDemandedOp(Op, BitWidth, NewMask, TLO))
656 // Output known-0 bits are only known if clear in both the LHS & RHS.
657 Known.Zero &= Known2.Zero;
658 // Output known-1 are known to be set if set in either the LHS | RHS.
659 Known.One |= Known2.One;
662 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, Known, TLO, Depth+1))
664 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
665 if (SimplifyDemandedBits(Op.getOperand(0), NewMask, Known2, TLO, Depth+1))
667 assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
669 // If all of the demanded bits are known zero on one side, return the other.
670 // These bits cannot contribute to the result of the 'xor'.
671 if (NewMask.isSubsetOf(Known.Zero))
672 return TLO.CombineTo(Op, Op.getOperand(0));
673 if (NewMask.isSubsetOf(Known2.Zero))
674 return TLO.CombineTo(Op, Op.getOperand(1));
675 // If the operation can be done in a smaller type, do so.
676 if (ShrinkDemandedOp(Op, BitWidth, NewMask, TLO))
679 // If all of the unknown bits are known to be zero on one side or the other
680 // (but not both) turn this into an *inclusive* or.
681 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
682 if ((NewMask & ~Known.Zero & ~Known2.Zero) == 0)
683 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
687 // Output known-0 bits are known if clear or set in both the LHS & RHS.
688 KnownOut.Zero = (Known.Zero & Known2.Zero) | (Known.One & Known2.One);
689 // Output known-1 are known to be set if set in only one of the LHS, RHS.
690 KnownOut.One = (Known.Zero & Known2.One) | (Known.One & Known2.Zero);
692 // If all of the demanded bits on one side are known, and all of the set
693 // bits on that side are also known to be set on the other side, turn this
694 // into an AND, as we know the bits will be cleared.
695 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
696 // NB: it is okay if more bits are known than are requested
697 if (NewMask.isSubsetOf(Known.Zero|Known.One)) { // all known on one side
698 if (Known.One == Known2.One) { // set bits are the same on both sides
699 EVT VT = Op.getValueType();
700 SDValue ANDC = TLO.DAG.getConstant(~Known.One & NewMask, dl, VT);
701 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
702 Op.getOperand(0), ANDC));
706 // If the RHS is a constant, see if we can change it. Don't alter a -1
707 // constant because that's a 'not' op, and that is better for combining and
709 ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(1));
710 if (C && !C->isAllOnesValue()) {
711 if (NewMask.isSubsetOf(C->getAPIntValue())) {
712 // We're flipping all demanded bits. Flip the undemanded bits too.
713 SDValue New = TLO.DAG.getNOT(dl, Op.getOperand(0), Op.getValueType());
714 return TLO.CombineTo(Op, New);
716 // If we can't turn this into a 'not', try to shrink the constant.
717 if (ShrinkDemandedConstant(Op, NewMask, TLO))
721 Known = std::move(KnownOut);
725 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, Known, TLO, Depth+1))
727 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, Known2, TLO, Depth+1))
729 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
730 assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
732 // If the operands are constants, see if we can simplify them.
733 if (ShrinkDemandedConstant(Op, NewMask, TLO))
736 // Only known if known in both the LHS and RHS.
737 Known.One &= Known2.One;
738 Known.Zero &= Known2.Zero;
741 if (SimplifyDemandedBits(Op.getOperand(3), NewMask, Known, TLO, Depth+1))
743 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, Known2, TLO, Depth+1))
745 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
746 assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
748 // If the operands are constants, see if we can simplify them.
749 if (ShrinkDemandedConstant(Op, NewMask, TLO))
752 // Only known if known in both the LHS and RHS.
753 Known.One &= Known2.One;
754 Known.Zero &= Known2.Zero;
757 SDValue Op0 = Op.getOperand(0);
758 SDValue Op1 = Op.getOperand(1);
759 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
760 // If (1) we only need the sign-bit, (2) the setcc operands are the same
761 // width as the setcc result, and (3) the result of a setcc conforms to 0 or
762 // -1, we may be able to bypass the setcc.
763 if (NewMask.isSignMask() && Op0.getScalarValueSizeInBits() == BitWidth &&
764 getBooleanContents(Op.getValueType()) ==
765 BooleanContent::ZeroOrNegativeOneBooleanContent) {
766 // If we're testing X < 0, then this compare isn't needed - just use X!
767 // FIXME: We're limiting to integer types here, but this should also work
768 // if we don't care about FP signed-zero. The use of SETLT with FP means
769 // that we don't care about NaNs.
770 if (CC == ISD::SETLT && Op1.getValueType().isInteger() &&
771 (isNullConstant(Op1) || ISD::isBuildVectorAllZeros(Op1.getNode())))
772 return TLO.CombineTo(Op, Op0);
774 // TODO: Should we check for other forms of sign-bit comparisons?
775 // Examples: X <= -1, X >= 0
777 if (getBooleanContents(Op0.getValueType()) ==
778 TargetLowering::ZeroOrOneBooleanContent &&
780 Known.Zero.setBitsFrom(1);
784 if (ConstantSDNode *SA = isConstOrConstSplat(Op.getOperand(1))) {
785 SDValue InOp = Op.getOperand(0);
787 // If the shift count is an invalid immediate, don't do anything.
788 if (SA->getAPIntValue().uge(BitWidth))
791 unsigned ShAmt = SA->getZExtValue();
793 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
794 // single shift. We can do this if the bottom bits (which are shifted
795 // out) are never demanded.
796 if (InOp.getOpcode() == ISD::SRL) {
797 if (ConstantSDNode *SA2 = isConstOrConstSplat(InOp.getOperand(1))) {
798 if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
799 if (SA2->getAPIntValue().ult(BitWidth)) {
800 unsigned C1 = SA2->getZExtValue();
801 unsigned Opc = ISD::SHL;
809 TLO.DAG.getConstant(Diff, dl, Op.getOperand(1).getValueType());
810 EVT VT = Op.getValueType();
811 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
819 if (SimplifyDemandedBits(InOp, NewMask.lshr(ShAmt), Known, TLO, Depth+1))
822 // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits
823 // are not demanded. This will likely allow the anyext to be folded away.
824 if (InOp.getNode()->getOpcode() == ISD::ANY_EXTEND) {
825 SDValue InnerOp = InOp.getOperand(0);
826 EVT InnerVT = InnerOp.getValueType();
827 unsigned InnerBits = InnerVT.getScalarSizeInBits();
828 if (ShAmt < InnerBits && NewMask.getActiveBits() <= InnerBits &&
829 isTypeDesirableForOp(ISD::SHL, InnerVT)) {
830 EVT ShTy = getShiftAmountTy(InnerVT, DL);
831 if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits()))
834 TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp,
835 TLO.DAG.getConstant(ShAmt, dl, ShTy));
838 TLO.DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(),
841 // Repeat the SHL optimization above in cases where an extension
842 // intervenes: (shl (anyext (shr x, c1)), c2) to
843 // (shl (anyext x), c2-c1). This requires that the bottom c1 bits
844 // aren't demanded (as above) and that the shifted upper c1 bits of
845 // x aren't demanded.
846 if (InOp.hasOneUse() && InnerOp.getOpcode() == ISD::SRL &&
847 InnerOp.hasOneUse()) {
848 if (ConstantSDNode *SA2 = isConstOrConstSplat(InnerOp.getOperand(1))) {
849 unsigned InnerShAmt = SA2->getLimitedValue(InnerBits);
850 if (InnerShAmt < ShAmt &&
851 InnerShAmt < InnerBits &&
852 NewMask.getActiveBits() <= (InnerBits - InnerShAmt + ShAmt) &&
853 NewMask.countTrailingZeros() >= ShAmt) {
855 TLO.DAG.getConstant(ShAmt - InnerShAmt, dl,
856 Op.getOperand(1).getValueType());
857 EVT VT = Op.getValueType();
858 SDValue NewExt = TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT,
859 InnerOp.getOperand(0));
860 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl, VT,
867 Known.Zero <<= ShAmt;
869 // low bits known zero.
870 Known.Zero.setLowBits(ShAmt);
874 if (ConstantSDNode *SA = isConstOrConstSplat(Op.getOperand(1))) {
875 SDValue InOp = Op.getOperand(0);
877 // If the shift count is an invalid immediate, don't do anything.
878 if (SA->getAPIntValue().uge(BitWidth))
881 unsigned ShAmt = SA->getZExtValue();
882 APInt InDemandedMask = (NewMask << ShAmt);
884 // If the shift is exact, then it does demand the low bits (and knows that
886 if (Op->getFlags().hasExact())
887 InDemandedMask.setLowBits(ShAmt);
889 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
890 // single shift. We can do this if the top bits (which are shifted out)
891 // are never demanded.
892 if (InOp.getOpcode() == ISD::SHL) {
893 if (ConstantSDNode *SA2 = isConstOrConstSplat(InOp.getOperand(1))) {
895 (NewMask & APInt::getHighBitsSet(BitWidth, ShAmt)) == 0) {
896 if (SA2->getAPIntValue().ult(BitWidth)) {
897 unsigned C1 = SA2->getZExtValue();
898 unsigned Opc = ISD::SRL;
906 TLO.DAG.getConstant(Diff, dl, Op.getOperand(1).getValueType());
907 EVT VT = Op.getValueType();
908 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
916 // Compute the new bits that are at the top now.
917 if (SimplifyDemandedBits(InOp, InDemandedMask, Known, TLO, Depth+1))
919 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
920 Known.Zero.lshrInPlace(ShAmt);
921 Known.One.lshrInPlace(ShAmt);
923 Known.Zero.setHighBits(ShAmt); // High bits known zero.
927 // If this is an arithmetic shift right and only the low-bit is set, we can
928 // always convert this into a logical shr, even if the shift amount is
929 // variable. The low bit of the shift cannot be an input sign bit unless
930 // the shift amount is >= the size of the datatype, which is undefined.
931 if (NewMask.isOneValue())
932 return TLO.CombineTo(Op,
933 TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(),
934 Op.getOperand(0), Op.getOperand(1)));
936 if (ConstantSDNode *SA = isConstOrConstSplat(Op.getOperand(1))) {
937 EVT VT = Op.getValueType();
939 // If the shift count is an invalid immediate, don't do anything.
940 if (SA->getAPIntValue().uge(BitWidth))
943 unsigned ShAmt = SA->getZExtValue();
944 APInt InDemandedMask = (NewMask << ShAmt);
946 // If the shift is exact, then it does demand the low bits (and knows that
948 if (Op->getFlags().hasExact())
949 InDemandedMask.setLowBits(ShAmt);
951 // If any of the demanded bits are produced by the sign extension, we also
952 // demand the input sign bit.
953 if (NewMask.countLeadingZeros() < ShAmt)
954 InDemandedMask.setSignBit();
956 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask, Known, TLO,
959 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
960 Known.Zero.lshrInPlace(ShAmt);
961 Known.One.lshrInPlace(ShAmt);
963 // If the input sign bit is known to be zero, or if none of the top bits
964 // are demanded, turn this into an unsigned shift right.
965 if (Known.Zero[BitWidth - ShAmt - 1] ||
966 NewMask.countLeadingZeros() >= ShAmt) {
968 Flags.setExact(Op->getFlags().hasExact());
969 return TLO.CombineTo(Op,
970 TLO.DAG.getNode(ISD::SRL, dl, VT, Op.getOperand(0),
971 Op.getOperand(1), Flags));
974 int Log2 = NewMask.exactLogBase2();
976 // The bit must come from the sign.
978 TLO.DAG.getConstant(BitWidth - 1 - Log2, dl,
979 Op.getOperand(1).getValueType());
980 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
981 Op.getOperand(0), NewSA));
984 if (Known.One[BitWidth - ShAmt - 1])
985 // New bits are known one.
986 Known.One.setHighBits(ShAmt);
989 case ISD::SIGN_EXTEND_INREG: {
990 EVT ExVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
991 unsigned ExVTBits = ExVT.getScalarSizeInBits();
993 // If we only care about the highest bit, don't bother shifting right.
994 if (NewMask.isSignMask()) {
995 SDValue InOp = Op.getOperand(0);
996 bool AlreadySignExtended =
997 TLO.DAG.ComputeNumSignBits(InOp) >= BitWidth-ExVTBits+1;
998 // However if the input is already sign extended we expect the sign
999 // extension to be dropped altogether later and do not simplify.
1000 if (!AlreadySignExtended) {
1001 // Compute the correct shift amount type, which must be getShiftAmountTy
1002 // for scalar types after legalization.
1003 EVT ShiftAmtTy = Op.getValueType();
1004 if (TLO.LegalTypes() && !ShiftAmtTy.isVector())
1005 ShiftAmtTy = getShiftAmountTy(ShiftAmtTy, DL);
1007 SDValue ShiftAmt = TLO.DAG.getConstant(BitWidth - ExVTBits, dl,
1009 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
1010 Op.getValueType(), InOp,
1015 // If none of the extended bits are demanded, eliminate the sextinreg.
1016 if (NewMask.getActiveBits() <= ExVTBits)
1017 return TLO.CombineTo(Op, Op.getOperand(0));
1019 APInt InputDemandedBits = NewMask.getLoBits(ExVTBits);
1021 // Since the sign extended bits are demanded, we know that the sign
1023 InputDemandedBits.setBit(ExVTBits - 1);
1025 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
1026 Known, TLO, Depth+1))
1028 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1030 // If the sign bit of the input is known set or clear, then we know the
1031 // top bits of the result.
1033 // If the input sign bit is known zero, convert this into a zero extension.
1034 if (Known.Zero[ExVTBits - 1])
1035 return TLO.CombineTo(Op, TLO.DAG.getZeroExtendInReg(
1036 Op.getOperand(0), dl, ExVT.getScalarType()));
1038 APInt Mask = APInt::getLowBitsSet(BitWidth, ExVTBits);
1039 if (Known.One[ExVTBits - 1]) { // Input sign bit known set
1040 Known.One.setBitsFrom(ExVTBits);
1042 } else { // Input sign bit unknown
1048 case ISD::BUILD_PAIR: {
1049 EVT HalfVT = Op.getOperand(0).getValueType();
1050 unsigned HalfBitWidth = HalfVT.getScalarSizeInBits();
1052 APInt MaskLo = NewMask.getLoBits(HalfBitWidth).trunc(HalfBitWidth);
1053 APInt MaskHi = NewMask.getHiBits(HalfBitWidth).trunc(HalfBitWidth);
1055 KnownBits KnownLo, KnownHi;
1057 if (SimplifyDemandedBits(Op.getOperand(0), MaskLo, KnownLo, TLO, Depth + 1))
1060 if (SimplifyDemandedBits(Op.getOperand(1), MaskHi, KnownHi, TLO, Depth + 1))
1063 Known.Zero = KnownLo.Zero.zext(BitWidth) |
1064 KnownHi.Zero.zext(BitWidth).shl(HalfBitWidth);
1066 Known.One = KnownLo.One.zext(BitWidth) |
1067 KnownHi.One.zext(BitWidth).shl(HalfBitWidth);
1070 case ISD::ZERO_EXTEND: {
1071 unsigned OperandBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
1073 // If none of the top bits are demanded, convert this into an any_extend.
1074 if (NewMask.getActiveBits() <= OperandBitWidth)
1075 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1079 APInt InMask = NewMask.trunc(OperandBitWidth);
1080 if (SimplifyDemandedBits(Op.getOperand(0), InMask, Known, TLO, Depth+1))
1082 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1083 Known = Known.zext(BitWidth);
1084 Known.Zero.setBitsFrom(OperandBitWidth);
1087 case ISD::SIGN_EXTEND: {
1088 unsigned InBits = Op.getOperand(0).getValueType().getScalarSizeInBits();
1090 // If none of the top bits are demanded, convert this into an any_extend.
1091 if (NewMask.getActiveBits() <= InBits)
1092 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1096 // Since some of the sign extended bits are demanded, we know that the sign
1098 APInt InDemandedBits = NewMask.trunc(InBits);
1099 InDemandedBits.setBit(InBits - 1);
1101 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, Known, TLO,
1104 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1105 // If the sign bit is known one, the top bits match.
1106 Known = Known.sext(BitWidth);
1108 // If the sign bit is known zero, convert this to a zero extend.
1109 if (Known.isNonNegative())
1110 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
1115 case ISD::ANY_EXTEND: {
1116 unsigned OperandBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
1117 APInt InMask = NewMask.trunc(OperandBitWidth);
1118 if (SimplifyDemandedBits(Op.getOperand(0), InMask, Known, TLO, Depth+1))
1120 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1121 Known = Known.zext(BitWidth);
1124 case ISD::TRUNCATE: {
1125 // Simplify the input, using demanded bit information, and compute the known
1126 // zero/one bits live out.
1127 unsigned OperandBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
1128 APInt TruncMask = NewMask.zext(OperandBitWidth);
1129 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask, Known, TLO, Depth+1))
1131 Known = Known.trunc(BitWidth);
1133 // If the input is only used by this truncate, see if we can shrink it based
1134 // on the known demanded bits.
1135 if (Op.getOperand(0).getNode()->hasOneUse()) {
1136 SDValue In = Op.getOperand(0);
1137 switch (In.getOpcode()) {
1140 // Shrink SRL by a constant if none of the high bits shifted in are
1142 if (TLO.LegalTypes() &&
1143 !isTypeDesirableForOp(ISD::SRL, Op.getValueType()))
1144 // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is
1147 ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1));
1150 SDValue Shift = In.getOperand(1);
1151 if (TLO.LegalTypes()) {
1152 uint64_t ShVal = ShAmt->getZExtValue();
1153 Shift = TLO.DAG.getConstant(ShVal, dl,
1154 getShiftAmountTy(Op.getValueType(), DL));
1157 if (ShAmt->getZExtValue() < BitWidth) {
1158 APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
1159 OperandBitWidth - BitWidth);
1160 HighBits.lshrInPlace(ShAmt->getZExtValue());
1161 HighBits = HighBits.trunc(BitWidth);
1163 if (!(HighBits & NewMask)) {
1164 // None of the shifted in bits are needed. Add a truncate of the
1165 // shift input, then shift it.
1166 SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
1169 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
1179 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1182 case ISD::AssertZext: {
1183 // AssertZext demands all of the high bits, plus any of the low bits
1184 // demanded by its users.
1185 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1186 APInt InMask = APInt::getLowBitsSet(BitWidth,
1187 VT.getSizeInBits());
1188 if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | NewMask,
1189 Known, TLO, Depth+1))
1191 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1193 Known.Zero |= ~InMask;
1197 // If this is an FP->Int bitcast and if the sign bit is the only
1198 // thing demanded, turn this into a FGETSIGN.
1199 if (!TLO.LegalOperations() &&
1200 !Op.getValueType().isVector() &&
1201 !Op.getOperand(0).getValueType().isVector() &&
1202 NewMask == APInt::getSignMask(Op.getValueSizeInBits()) &&
1203 Op.getOperand(0).getValueType().isFloatingPoint()) {
1204 bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, Op.getValueType());
1205 bool i32Legal = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32);
1206 if ((OpVTLegal || i32Legal) && Op.getValueType().isSimple() &&
1207 Op.getOperand(0).getValueType() != MVT::f128) {
1208 // Cannot eliminate/lower SHL for f128 yet.
1209 EVT Ty = OpVTLegal ? Op.getValueType() : MVT::i32;
1210 // Make a FGETSIGN + SHL to move the sign bit into the appropriate
1211 // place. We expect the SHL to be eliminated by other optimizations.
1212 SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Op.getOperand(0));
1213 unsigned OpVTSizeInBits = Op.getValueSizeInBits();
1214 if (!OpVTLegal && OpVTSizeInBits > 32)
1215 Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), Sign);
1216 unsigned ShVal = Op.getValueSizeInBits() - 1;
1217 SDValue ShAmt = TLO.DAG.getConstant(ShVal, dl, Op.getValueType());
1218 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
1227 // Add, Sub, and Mul don't demand any bits in positions beyond that
1228 // of the highest bit demanded of them.
1229 APInt LoMask = APInt::getLowBitsSet(BitWidth,
1230 BitWidth - NewMask.countLeadingZeros());
1231 if (SimplifyDemandedBits(Op.getOperand(0), LoMask, Known2, TLO, Depth+1) ||
1232 SimplifyDemandedBits(Op.getOperand(1), LoMask, Known2, TLO, Depth+1) ||
1233 // See if the operation should be performed at a smaller bit width.
1234 ShrinkDemandedOp(Op, BitWidth, NewMask, TLO)) {
1235 SDNodeFlags Flags = Op.getNode()->getFlags();
1236 if (Flags.hasNoSignedWrap() || Flags.hasNoUnsignedWrap()) {
1237 // Disable the nsw and nuw flags. We can no longer guarantee that we
1238 // won't wrap after simplification.
1239 Flags.setNoSignedWrap(false);
1240 Flags.setNoUnsignedWrap(false);
1241 SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, Op.getValueType(),
1242 Op.getOperand(0), Op.getOperand(1),
1244 return TLO.CombineTo(Op, NewOp);
1251 // Just use computeKnownBits to compute output bits.
1252 TLO.DAG.computeKnownBits(Op, Known, Depth);
1256 // If we know the value of all of the demanded bits, return this as a
1258 if (NewMask.isSubsetOf(Known.Zero|Known.One)) {
1259 // Avoid folding to a constant if any OpaqueConstant is involved.
1260 const SDNode *N = Op.getNode();
1261 for (SDNodeIterator I = SDNodeIterator::begin(N),
1262 E = SDNodeIterator::end(N); I != E; ++I) {
1264 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
1268 return TLO.CombineTo(Op,
1269 TLO.DAG.getConstant(Known.One, dl, Op.getValueType()));
1275 /// Determine which of the bits specified in Mask are known to be either zero or
1276 /// one and return them in the Known.
1277 void TargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
1279 const APInt &DemandedElts,
1280 const SelectionDAG &DAG,
1281 unsigned Depth) const {
1282 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1283 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1284 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1285 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1286 "Should use MaskedValueIsZero if you don't know whether Op"
1287 " is a target node!");
1291 void TargetLowering::computeKnownBitsForFrameIndex(const SDValue Op,
1293 const APInt &DemandedElts,
1294 const SelectionDAG &DAG,
1295 unsigned Depth) const {
1296 assert(isa<FrameIndexSDNode>(Op) && "expected FrameIndex");
1298 if (unsigned Align = DAG.InferPtrAlignment(Op)) {
1299 // The low bits are known zero if the pointer is aligned.
1300 Known.Zero.setLowBits(Log2_32(Align));
1304 /// This method can be implemented by targets that want to expose additional
1305 /// information about sign bits to the DAG Combiner.
1306 unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
1308 const SelectionDAG &,
1309 unsigned Depth) const {
1310 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1311 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1312 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1313 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1314 "Should use ComputeNumSignBits if you don't know whether Op"
1315 " is a target node!");
1319 // FIXME: Ideally, this would use ISD::isConstantSplatVector(), but that must
1320 // work with truncating build vectors and vectors with elements of less than
1322 bool TargetLowering::isConstTrueVal(const SDNode *N) const {
1327 if (auto *CN = dyn_cast<ConstantSDNode>(N)) {
1328 CVal = CN->getAPIntValue();
1329 } else if (auto *BV = dyn_cast<BuildVectorSDNode>(N)) {
1330 auto *CN = BV->getConstantSplatNode();
1334 // If this is a truncating build vector, truncate the splat value.
1335 // Otherwise, we may fail to match the expected values below.
1336 unsigned BVEltWidth = BV->getValueType(0).getScalarSizeInBits();
1337 CVal = CN->getAPIntValue();
1338 if (BVEltWidth < CVal.getBitWidth())
1339 CVal = CVal.trunc(BVEltWidth);
1344 switch (getBooleanContents(N->getValueType(0))) {
1345 case UndefinedBooleanContent:
1347 case ZeroOrOneBooleanContent:
1348 return CVal.isOneValue();
1349 case ZeroOrNegativeOneBooleanContent:
1350 return CVal.isAllOnesValue();
1353 llvm_unreachable("Invalid boolean contents");
1356 SDValue TargetLowering::getConstTrueVal(SelectionDAG &DAG, EVT VT,
1357 const SDLoc &DL) const {
1358 unsigned ElementWidth = VT.getScalarSizeInBits();
1360 getBooleanContents(VT) == TargetLowering::ZeroOrOneBooleanContent
1361 ? APInt(ElementWidth, 1)
1362 : APInt::getAllOnesValue(ElementWidth);
1363 return DAG.getConstant(TrueInt, DL, VT);
1366 bool TargetLowering::isConstFalseVal(const SDNode *N) const {
1370 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
1372 const BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N);
1376 // Only interested in constant splats, we don't care about undef
1377 // elements in identifying boolean constants and getConstantSplatNode
1378 // returns NULL if all ops are undef;
1379 CN = BV->getConstantSplatNode();
1384 if (getBooleanContents(N->getValueType(0)) == UndefinedBooleanContent)
1385 return !CN->getAPIntValue()[0];
1387 return CN->isNullValue();
1390 bool TargetLowering::isExtendedTrueVal(const ConstantSDNode *N, EVT VT,
1395 TargetLowering::BooleanContent Cnt = getBooleanContents(VT);
1397 case TargetLowering::ZeroOrOneBooleanContent:
1398 // An extended value of 1 is always true, unless its original type is i1,
1399 // in which case it will be sign extended to -1.
1400 return (N->isOne() && !SExt) || (SExt && (N->getValueType(0) != MVT::i1));
1401 case TargetLowering::UndefinedBooleanContent:
1402 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1403 return N->isAllOnesValue() && SExt;
1405 llvm_unreachable("Unexpected enumeration.");
1408 /// This helper function of SimplifySetCC tries to optimize the comparison when
1409 /// either operand of the SetCC node is a bitwise-and instruction.
1410 SDValue TargetLowering::simplifySetCCWithAnd(EVT VT, SDValue N0, SDValue N1,
1412 DAGCombinerInfo &DCI,
1413 const SDLoc &DL) const {
1414 // Match these patterns in any of their permutations:
1417 if (N1.getOpcode() == ISD::AND && N0.getOpcode() != ISD::AND)
1420 EVT OpVT = N0.getValueType();
1421 if (N0.getOpcode() != ISD::AND || !OpVT.isInteger() ||
1422 (Cond != ISD::SETEQ && Cond != ISD::SETNE))
1426 if (N0.getOperand(0) == N1) {
1427 X = N0.getOperand(1);
1428 Y = N0.getOperand(0);
1429 } else if (N0.getOperand(1) == N1) {
1430 X = N0.getOperand(0);
1431 Y = N0.getOperand(1);
1436 SelectionDAG &DAG = DCI.DAG;
1437 SDValue Zero = DAG.getConstant(0, DL, OpVT);
1438 if (DAG.isKnownToBeAPowerOfTwo(Y)) {
1439 // Simplify X & Y == Y to X & Y != 0 if Y has exactly one bit set.
1440 // Note that where Y is variable and is known to have at most one bit set
1441 // (for example, if it is Z & 1) we cannot do this; the expressions are not
1442 // equivalent when Y == 0.
1443 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
1444 if (DCI.isBeforeLegalizeOps() ||
1445 isCondCodeLegal(Cond, N0.getSimpleValueType()))
1446 return DAG.getSetCC(DL, VT, N0, Zero, Cond);
1447 } else if (N0.hasOneUse() && hasAndNotCompare(Y)) {
1448 // If the target supports an 'and-not' or 'and-complement' logic operation,
1449 // try to use that to make a comparison operation more efficient.
1450 // But don't do this transform if the mask is a single bit because there are
1451 // more efficient ways to deal with that case (for example, 'bt' on x86 or
1452 // 'rlwinm' on PPC).
1454 // Bail out if the compare operand that we want to turn into a zero is
1455 // already a zero (otherwise, infinite loop).
1456 auto *YConst = dyn_cast<ConstantSDNode>(Y);
1457 if (YConst && YConst->isNullValue())
1460 // Transform this into: ~X & Y == 0.
1461 SDValue NotX = DAG.getNOT(SDLoc(X), X, OpVT);
1462 SDValue NewAnd = DAG.getNode(ISD::AND, SDLoc(N0), OpVT, NotX, Y);
1463 return DAG.getSetCC(DL, VT, NewAnd, Zero, Cond);
1469 /// Try to simplify a setcc built with the specified operands and cc. If it is
1470 /// unable to simplify it, return a null SDValue.
1471 SDValue TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
1472 ISD::CondCode Cond, bool foldBooleans,
1473 DAGCombinerInfo &DCI,
1474 const SDLoc &dl) const {
1475 SelectionDAG &DAG = DCI.DAG;
1477 // These setcc operations always fold.
1481 case ISD::SETFALSE2: return DAG.getConstant(0, dl, VT);
1483 case ISD::SETTRUE2: {
1484 TargetLowering::BooleanContent Cnt =
1485 getBooleanContents(N0->getValueType(0));
1486 return DAG.getConstant(
1487 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, dl,
1492 // Ensure that the constant occurs on the RHS and fold constant comparisons.
1493 ISD::CondCode SwappedCC = ISD::getSetCCSwappedOperands(Cond);
1494 if (isa<ConstantSDNode>(N0.getNode()) &&
1495 (DCI.isBeforeLegalizeOps() ||
1496 isCondCodeLegal(SwappedCC, N0.getSimpleValueType())))
1497 return DAG.getSetCC(dl, VT, N1, N0, SwappedCC);
1499 if (auto *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
1500 const APInt &C1 = N1C->getAPIntValue();
1502 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1503 // equality comparison, then we're just comparing whether X itself is
1505 if (N0.getOpcode() == ISD::SRL && (C1.isNullValue() || C1.isOneValue()) &&
1506 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1507 N0.getOperand(1).getOpcode() == ISD::Constant) {
1509 = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1510 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1511 ShAmt == Log2_32(N0.getValueSizeInBits())) {
1512 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1513 // (srl (ctlz x), 5) == 0 -> X != 0
1514 // (srl (ctlz x), 5) != 1 -> X != 0
1517 // (srl (ctlz x), 5) != 0 -> X == 0
1518 // (srl (ctlz x), 5) == 1 -> X == 0
1521 SDValue Zero = DAG.getConstant(0, dl, N0.getValueType());
1522 return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
1528 // Look through truncs that don't change the value of a ctpop.
1529 if (N0.hasOneUse() && N0.getOpcode() == ISD::TRUNCATE)
1530 CTPOP = N0.getOperand(0);
1532 if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP &&
1534 N0.getValueSizeInBits() > Log2_32_Ceil(CTPOP.getValueSizeInBits()))) {
1535 EVT CTVT = CTPOP.getValueType();
1536 SDValue CTOp = CTPOP.getOperand(0);
1538 // (ctpop x) u< 2 -> (x & x-1) == 0
1539 // (ctpop x) u> 1 -> (x & x-1) != 0
1540 if ((Cond == ISD::SETULT && C1 == 2) || (Cond == ISD::SETUGT && C1 == 1)){
1541 SDValue Sub = DAG.getNode(ISD::SUB, dl, CTVT, CTOp,
1542 DAG.getConstant(1, dl, CTVT));
1543 SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Sub);
1544 ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE;
1545 return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, dl, CTVT), CC);
1548 // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal.
1551 // (zext x) == C --> x == (trunc C)
1552 // (sext x) == C --> x == (trunc C)
1553 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1554 DCI.isBeforeLegalize() && N0->hasOneUse()) {
1555 unsigned MinBits = N0.getValueSizeInBits();
1557 bool Signed = false;
1558 if (N0->getOpcode() == ISD::ZERO_EXTEND) {
1560 MinBits = N0->getOperand(0).getValueSizeInBits();
1561 PreExt = N0->getOperand(0);
1562 } else if (N0->getOpcode() == ISD::AND) {
1563 // DAGCombine turns costly ZExts into ANDs
1564 if (auto *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
1565 if ((C->getAPIntValue()+1).isPowerOf2()) {
1566 MinBits = C->getAPIntValue().countTrailingOnes();
1567 PreExt = N0->getOperand(0);
1569 } else if (N0->getOpcode() == ISD::SIGN_EXTEND) {
1571 MinBits = N0->getOperand(0).getValueSizeInBits();
1572 PreExt = N0->getOperand(0);
1574 } else if (auto *LN0 = dyn_cast<LoadSDNode>(N0)) {
1575 // ZEXTLOAD / SEXTLOAD
1576 if (LN0->getExtensionType() == ISD::ZEXTLOAD) {
1577 MinBits = LN0->getMemoryVT().getSizeInBits();
1579 } else if (LN0->getExtensionType() == ISD::SEXTLOAD) {
1581 MinBits = LN0->getMemoryVT().getSizeInBits();
1586 // Figure out how many bits we need to preserve this constant.
1587 unsigned ReqdBits = Signed ?
1588 C1.getBitWidth() - C1.getNumSignBits() + 1 :
1591 // Make sure we're not losing bits from the constant.
1593 MinBits < C1.getBitWidth() &&
1594 MinBits >= ReqdBits) {
1595 EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits);
1596 if (isTypeDesirableForOp(ISD::SETCC, MinVT)) {
1597 // Will get folded away.
1598 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreExt);
1599 if (MinBits == 1 && C1 == 1)
1600 // Invert the condition.
1601 return DAG.getSetCC(dl, VT, Trunc, DAG.getConstant(0, dl, MVT::i1),
1602 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1603 SDValue C = DAG.getConstant(C1.trunc(MinBits), dl, MinVT);
1604 return DAG.getSetCC(dl, VT, Trunc, C, Cond);
1607 // If truncating the setcc operands is not desirable, we can still
1608 // simplify the expression in some cases:
1609 // setcc ([sz]ext (setcc x, y, cc)), 0, setne) -> setcc (x, y, cc)
1610 // setcc ([sz]ext (setcc x, y, cc)), 0, seteq) -> setcc (x, y, inv(cc))
1611 // setcc (zext (setcc x, y, cc)), 1, setne) -> setcc (x, y, inv(cc))
1612 // setcc (zext (setcc x, y, cc)), 1, seteq) -> setcc (x, y, cc)
1613 // setcc (sext (setcc x, y, cc)), -1, setne) -> setcc (x, y, inv(cc))
1614 // setcc (sext (setcc x, y, cc)), -1, seteq) -> setcc (x, y, cc)
1615 SDValue TopSetCC = N0->getOperand(0);
1616 unsigned N0Opc = N0->getOpcode();
1617 bool SExt = (N0Opc == ISD::SIGN_EXTEND);
1618 if (TopSetCC.getValueType() == MVT::i1 && VT == MVT::i1 &&
1619 TopSetCC.getOpcode() == ISD::SETCC &&
1620 (N0Opc == ISD::ZERO_EXTEND || N0Opc == ISD::SIGN_EXTEND) &&
1621 (isConstFalseVal(N1C) ||
1622 isExtendedTrueVal(N1C, N0->getValueType(0), SExt))) {
1624 bool Inverse = (N1C->isNullValue() && Cond == ISD::SETEQ) ||
1625 (!N1C->isNullValue() && Cond == ISD::SETNE);
1630 ISD::CondCode InvCond = ISD::getSetCCInverse(
1631 cast<CondCodeSDNode>(TopSetCC.getOperand(2))->get(),
1632 TopSetCC.getOperand(0).getValueType().isInteger());
1633 return DAG.getSetCC(dl, VT, TopSetCC.getOperand(0),
1634 TopSetCC.getOperand(1),
1640 // If the LHS is '(and load, const)', the RHS is 0, the test is for
1641 // equality or unsigned, and all 1 bits of the const are in the same
1642 // partial word, see if we can shorten the load.
1643 if (DCI.isBeforeLegalize() &&
1644 !ISD::isSignedIntSetCC(Cond) &&
1645 N0.getOpcode() == ISD::AND && C1 == 0 &&
1646 N0.getNode()->hasOneUse() &&
1647 isa<LoadSDNode>(N0.getOperand(0)) &&
1648 N0.getOperand(0).getNode()->hasOneUse() &&
1649 isa<ConstantSDNode>(N0.getOperand(1))) {
1650 LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
1652 unsigned bestWidth = 0, bestOffset = 0;
1653 if (!Lod->isVolatile() && Lod->isUnindexed()) {
1654 unsigned origWidth = N0.getValueSizeInBits();
1655 unsigned maskWidth = origWidth;
1656 // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
1657 // 8 bits, but have to be careful...
1658 if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
1659 origWidth = Lod->getMemoryVT().getSizeInBits();
1661 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1662 for (unsigned width = origWidth / 2; width>=8; width /= 2) {
1663 APInt newMask = APInt::getLowBitsSet(maskWidth, width);
1664 for (unsigned offset=0; offset<origWidth/width; offset++) {
1665 if (Mask.isSubsetOf(newMask)) {
1666 if (DAG.getDataLayout().isLittleEndian())
1667 bestOffset = (uint64_t)offset * (width/8);
1669 bestOffset = (origWidth/width - offset - 1) * (width/8);
1670 bestMask = Mask.lshr(offset * (width/8) * 8);
1679 EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth);
1680 if (newVT.isRound()) {
1681 EVT PtrType = Lod->getOperand(1).getValueType();
1682 SDValue Ptr = Lod->getBasePtr();
1683 if (bestOffset != 0)
1684 Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
1685 DAG.getConstant(bestOffset, dl, PtrType));
1686 unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
1687 SDValue NewLoad = DAG.getLoad(
1688 newVT, dl, Lod->getChain(), Ptr,
1689 Lod->getPointerInfo().getWithOffset(bestOffset), NewAlign);
1690 return DAG.getSetCC(dl, VT,
1691 DAG.getNode(ISD::AND, dl, newVT, NewLoad,
1692 DAG.getConstant(bestMask.trunc(bestWidth),
1694 DAG.getConstant(0LL, dl, newVT), Cond);
1699 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1700 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1701 unsigned InSize = N0.getOperand(0).getValueSizeInBits();
1703 // If the comparison constant has bits in the upper part, the
1704 // zero-extended value could never match.
1705 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
1706 C1.getBitWidth() - InSize))) {
1711 return DAG.getConstant(0, dl, VT);
1715 return DAG.getConstant(1, dl, VT);
1718 // True if the sign bit of C1 is set.
1719 return DAG.getConstant(C1.isNegative(), dl, VT);
1722 // True if the sign bit of C1 isn't set.
1723 return DAG.getConstant(C1.isNonNegative(), dl, VT);
1729 // Otherwise, we can perform the comparison with the low bits.
1737 EVT newVT = N0.getOperand(0).getValueType();
1738 if (DCI.isBeforeLegalizeOps() ||
1739 (isOperationLegal(ISD::SETCC, newVT) &&
1740 getCondCodeAction(Cond, newVT.getSimpleVT()) == Legal)) {
1742 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), newVT);
1743 SDValue NewConst = DAG.getConstant(C1.trunc(InSize), dl, newVT);
1745 SDValue NewSetCC = DAG.getSetCC(dl, NewSetCCVT, N0.getOperand(0),
1747 return DAG.getBoolExtOrTrunc(NewSetCC, dl, VT, N0.getValueType());
1752 break; // todo, be more careful with signed comparisons
1754 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1755 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1756 EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1757 unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
1758 EVT ExtDstTy = N0.getValueType();
1759 unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
1761 // If the constant doesn't fit into the number of bits for the source of
1762 // the sign extension, it is impossible for both sides to be equal.
1763 if (C1.getMinSignedBits() > ExtSrcTyBits)
1764 return DAG.getConstant(Cond == ISD::SETNE, dl, VT);
1767 EVT Op0Ty = N0.getOperand(0).getValueType();
1768 if (Op0Ty == ExtSrcTy) {
1769 ZextOp = N0.getOperand(0);
1771 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
1772 ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
1773 DAG.getConstant(Imm, dl, Op0Ty));
1775 if (!DCI.isCalledByLegalizer())
1776 DCI.AddToWorklist(ZextOp.getNode());
1777 // Otherwise, make this a use of a zext.
1778 return DAG.getSetCC(dl, VT, ZextOp,
1779 DAG.getConstant(C1 & APInt::getLowBitsSet(
1784 } else if ((N1C->isNullValue() || N1C->isOne()) &&
1785 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1786 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
1787 if (N0.getOpcode() == ISD::SETCC &&
1788 isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
1789 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (!N1C->isOne());
1791 return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
1792 // Invert the condition.
1793 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
1794 CC = ISD::getSetCCInverse(CC,
1795 N0.getOperand(0).getValueType().isInteger());
1796 if (DCI.isBeforeLegalizeOps() ||
1797 isCondCodeLegal(CC, N0.getOperand(0).getSimpleValueType()))
1798 return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
1801 if ((N0.getOpcode() == ISD::XOR ||
1802 (N0.getOpcode() == ISD::AND &&
1803 N0.getOperand(0).getOpcode() == ISD::XOR &&
1804 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
1805 isa<ConstantSDNode>(N0.getOperand(1)) &&
1806 cast<ConstantSDNode>(N0.getOperand(1))->isOne()) {
1807 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
1808 // can only do this if the top bits are known zero.
1809 unsigned BitWidth = N0.getValueSizeInBits();
1810 if (DAG.MaskedValueIsZero(N0,
1811 APInt::getHighBitsSet(BitWidth,
1813 // Okay, get the un-inverted input value.
1815 if (N0.getOpcode() == ISD::XOR) {
1816 Val = N0.getOperand(0);
1818 assert(N0.getOpcode() == ISD::AND &&
1819 N0.getOperand(0).getOpcode() == ISD::XOR);
1820 // ((X^1)&1)^1 -> X & 1
1821 Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
1822 N0.getOperand(0).getOperand(0),
1826 return DAG.getSetCC(dl, VT, Val, N1,
1827 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1829 } else if (N1C->isOne() &&
1831 getBooleanContents(N0->getValueType(0)) ==
1832 ZeroOrOneBooleanContent)) {
1834 if (Op0.getOpcode() == ISD::TRUNCATE)
1835 Op0 = Op0.getOperand(0);
1837 if ((Op0.getOpcode() == ISD::XOR) &&
1838 Op0.getOperand(0).getOpcode() == ISD::SETCC &&
1839 Op0.getOperand(1).getOpcode() == ISD::SETCC) {
1840 // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
1841 Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
1842 return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
1845 if (Op0.getOpcode() == ISD::AND &&
1846 isa<ConstantSDNode>(Op0.getOperand(1)) &&
1847 cast<ConstantSDNode>(Op0.getOperand(1))->isOne()) {
1848 // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
1849 if (Op0.getValueType().bitsGT(VT))
1850 Op0 = DAG.getNode(ISD::AND, dl, VT,
1851 DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
1852 DAG.getConstant(1, dl, VT));
1853 else if (Op0.getValueType().bitsLT(VT))
1854 Op0 = DAG.getNode(ISD::AND, dl, VT,
1855 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)),
1856 DAG.getConstant(1, dl, VT));
1858 return DAG.getSetCC(dl, VT, Op0,
1859 DAG.getConstant(0, dl, Op0.getValueType()),
1860 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1862 if (Op0.getOpcode() == ISD::AssertZext &&
1863 cast<VTSDNode>(Op0.getOperand(1))->getVT() == MVT::i1)
1864 return DAG.getSetCC(dl, VT, Op0,
1865 DAG.getConstant(0, dl, Op0.getValueType()),
1866 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1870 APInt MinVal, MaxVal;
1871 unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
1872 if (ISD::isSignedIntSetCC(Cond)) {
1873 MinVal = APInt::getSignedMinValue(OperandBitSize);
1874 MaxVal = APInt::getSignedMaxValue(OperandBitSize);
1876 MinVal = APInt::getMinValue(OperandBitSize);
1877 MaxVal = APInt::getMaxValue(OperandBitSize);
1880 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1881 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1882 // X >= MIN --> true
1884 return DAG.getConstant(1, dl, VT);
1886 // X >= C0 --> X > (C0 - 1)
1888 ISD::CondCode NewCC = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT;
1889 if ((DCI.isBeforeLegalizeOps() ||
1890 isCondCodeLegal(NewCC, VT.getSimpleVT())) &&
1891 (!N1C->isOpaque() || (N1C->isOpaque() && C.getBitWidth() <= 64 &&
1892 isLegalICmpImmediate(C.getSExtValue())))) {
1893 return DAG.getSetCC(dl, VT, N0,
1894 DAG.getConstant(C, dl, N1.getValueType()),
1899 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1900 // X <= MAX --> true
1902 return DAG.getConstant(1, dl, VT);
1904 // X <= C0 --> X < (C0 + 1)
1906 ISD::CondCode NewCC = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT;
1907 if ((DCI.isBeforeLegalizeOps() ||
1908 isCondCodeLegal(NewCC, VT.getSimpleVT())) &&
1909 (!N1C->isOpaque() || (N1C->isOpaque() && C.getBitWidth() <= 64 &&
1910 isLegalICmpImmediate(C.getSExtValue())))) {
1911 return DAG.getSetCC(dl, VT, N0,
1912 DAG.getConstant(C, dl, N1.getValueType()),
1917 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1918 return DAG.getConstant(0, dl, VT); // X < MIN --> false
1919 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1920 return DAG.getConstant(1, dl, VT); // X >= MIN --> true
1921 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1922 return DAG.getConstant(0, dl, VT); // X > MAX --> false
1923 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1924 return DAG.getConstant(1, dl, VT); // X <= MAX --> true
1926 // Canonicalize setgt X, Min --> setne X, Min
1927 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1928 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
1929 // Canonicalize setlt X, Max --> setne X, Max
1930 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1931 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
1933 // If we have setult X, 1, turn it into seteq X, 0
1934 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1935 return DAG.getSetCC(dl, VT, N0,
1936 DAG.getConstant(MinVal, dl, N0.getValueType()),
1938 // If we have setugt X, Max-1, turn it into seteq X, Max
1939 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1940 return DAG.getSetCC(dl, VT, N0,
1941 DAG.getConstant(MaxVal, dl, N0.getValueType()),
1944 // If we have "setcc X, C0", check to see if we can shrink the immediate
1947 // SETUGT X, SINTMAX -> SETLT X, 0
1948 if (Cond == ISD::SETUGT &&
1949 C1 == APInt::getSignedMaxValue(OperandBitSize))
1950 return DAG.getSetCC(dl, VT, N0,
1951 DAG.getConstant(0, dl, N1.getValueType()),
1954 // SETULT X, SINTMIN -> SETGT X, -1
1955 if (Cond == ISD::SETULT &&
1956 C1 == APInt::getSignedMinValue(OperandBitSize)) {
1957 SDValue ConstMinusOne =
1958 DAG.getConstant(APInt::getAllOnesValue(OperandBitSize), dl,
1960 return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
1963 // Fold bit comparisons when we can.
1964 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1965 (VT == N0.getValueType() ||
1966 (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
1967 N0.getOpcode() == ISD::AND) {
1968 auto &DL = DAG.getDataLayout();
1969 if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1970 EVT ShiftTy = DCI.isBeforeLegalize()
1972 : getShiftAmountTy(N0.getValueType(), DL);
1973 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1974 // Perform the xform if the AND RHS is a single bit.
1975 if (AndRHS->getAPIntValue().isPowerOf2()) {
1976 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1977 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
1978 DAG.getConstant(AndRHS->getAPIntValue().logBase2(), dl,
1981 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
1982 // (X & 8) == 8 --> (X & 8) >> 3
1983 // Perform the xform if C1 is a single bit.
1984 if (C1.isPowerOf2()) {
1985 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1986 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
1987 DAG.getConstant(C1.logBase2(), dl,
1994 if (C1.getMinSignedBits() <= 64 &&
1995 !isLegalICmpImmediate(C1.getSExtValue())) {
1996 // (X & -256) == 256 -> (X >> 8) == 1
1997 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1998 N0.getOpcode() == ISD::AND && N0.hasOneUse()) {
1999 if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2000 const APInt &AndRHSC = AndRHS->getAPIntValue();
2001 if ((-AndRHSC).isPowerOf2() && (AndRHSC & C1) == C1) {
2002 unsigned ShiftBits = AndRHSC.countTrailingZeros();
2003 auto &DL = DAG.getDataLayout();
2004 EVT ShiftTy = DCI.isBeforeLegalize()
2006 : getShiftAmountTy(N0.getValueType(), DL);
2007 EVT CmpTy = N0.getValueType();
2008 SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0.getOperand(0),
2009 DAG.getConstant(ShiftBits, dl,
2011 SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), dl, CmpTy);
2012 return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond);
2015 } else if (Cond == ISD::SETULT || Cond == ISD::SETUGE ||
2016 Cond == ISD::SETULE || Cond == ISD::SETUGT) {
2017 bool AdjOne = (Cond == ISD::SETULE || Cond == ISD::SETUGT);
2018 // X < 0x100000000 -> (X >> 32) < 1
2019 // X >= 0x100000000 -> (X >> 32) >= 1
2020 // X <= 0x0ffffffff -> (X >> 32) < 1
2021 // X > 0x0ffffffff -> (X >> 32) >= 1
2024 ISD::CondCode NewCond = Cond;
2026 ShiftBits = C1.countTrailingOnes();
2028 NewCond = (Cond == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
2030 ShiftBits = C1.countTrailingZeros();
2032 NewC.lshrInPlace(ShiftBits);
2033 if (ShiftBits && NewC.getMinSignedBits() <= 64 &&
2034 isLegalICmpImmediate(NewC.getSExtValue())) {
2035 auto &DL = DAG.getDataLayout();
2036 EVT ShiftTy = DCI.isBeforeLegalize()
2038 : getShiftAmountTy(N0.getValueType(), DL);
2039 EVT CmpTy = N0.getValueType();
2040 SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0,
2041 DAG.getConstant(ShiftBits, dl, ShiftTy));
2042 SDValue CmpRHS = DAG.getConstant(NewC, dl, CmpTy);
2043 return DAG.getSetCC(dl, VT, Shift, CmpRHS, NewCond);
2049 if (isa<ConstantFPSDNode>(N0.getNode())) {
2050 // Constant fold or commute setcc.
2051 SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
2052 if (O.getNode()) return O;
2053 } else if (auto *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
2054 // If the RHS of an FP comparison is a constant, simplify it away in
2056 if (CFP->getValueAPF().isNaN()) {
2057 // If an operand is known to be a nan, we can fold it.
2058 switch (ISD::getUnorderedFlavor(Cond)) {
2059 default: llvm_unreachable("Unknown flavor!");
2060 case 0: // Known false.
2061 return DAG.getConstant(0, dl, VT);
2062 case 1: // Known true.
2063 return DAG.getConstant(1, dl, VT);
2064 case 2: // Undefined.
2065 return DAG.getUNDEF(VT);
2069 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
2070 // constant if knowing that the operand is non-nan is enough. We prefer to
2071 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
2073 if (Cond == ISD::SETO || Cond == ISD::SETUO)
2074 return DAG.getSetCC(dl, VT, N0, N0, Cond);
2076 // setcc (fneg x), C -> setcc swap(pred) x, -C
2077 if (N0.getOpcode() == ISD::FNEG) {
2078 ISD::CondCode SwapCond = ISD::getSetCCSwappedOperands(Cond);
2079 if (DCI.isBeforeLegalizeOps() ||
2080 isCondCodeLegal(SwapCond, N0.getSimpleValueType())) {
2081 SDValue NegN1 = DAG.getNode(ISD::FNEG, dl, N0.getValueType(), N1);
2082 return DAG.getSetCC(dl, VT, N0.getOperand(0), NegN1, SwapCond);
2086 // If the condition is not legal, see if we can find an equivalent one
2088 if (!isCondCodeLegal(Cond, N0.getSimpleValueType())) {
2089 // If the comparison was an awkward floating-point == or != and one of
2090 // the comparison operands is infinity or negative infinity, convert the
2091 // condition to a less-awkward <= or >=.
2092 if (CFP->getValueAPF().isInfinity()) {
2093 if (CFP->getValueAPF().isNegative()) {
2094 if (Cond == ISD::SETOEQ &&
2095 isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType()))
2096 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
2097 if (Cond == ISD::SETUEQ &&
2098 isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType()))
2099 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
2100 if (Cond == ISD::SETUNE &&
2101 isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType()))
2102 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
2103 if (Cond == ISD::SETONE &&
2104 isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType()))
2105 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
2107 if (Cond == ISD::SETOEQ &&
2108 isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType()))
2109 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
2110 if (Cond == ISD::SETUEQ &&
2111 isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType()))
2112 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
2113 if (Cond == ISD::SETUNE &&
2114 isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType()))
2115 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
2116 if (Cond == ISD::SETONE &&
2117 isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType()))
2118 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
2125 // The sext(setcc()) => setcc() optimization relies on the appropriate
2126 // constant being emitted.
2128 switch (getBooleanContents(N0.getValueType())) {
2129 case UndefinedBooleanContent:
2130 case ZeroOrOneBooleanContent:
2131 EqVal = ISD::isTrueWhenEqual(Cond);
2133 case ZeroOrNegativeOneBooleanContent:
2134 EqVal = ISD::isTrueWhenEqual(Cond) ? -1 : 0;
2138 // We can always fold X == X for integer setcc's.
2139 if (N0.getValueType().isInteger()) {
2140 return DAG.getConstant(EqVal, dl, VT);
2142 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2143 if (UOF == 2) // FP operators that are undefined on NaNs.
2144 return DAG.getConstant(EqVal, dl, VT);
2145 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2146 return DAG.getConstant(EqVal, dl, VT);
2147 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2148 // if it is not already.
2149 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
2150 if (NewCond != Cond && (DCI.isBeforeLegalizeOps() ||
2151 getCondCodeAction(NewCond, N0.getSimpleValueType()) == Legal))
2152 return DAG.getSetCC(dl, VT, N0, N1, NewCond);
2155 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2156 N0.getValueType().isInteger()) {
2157 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2158 N0.getOpcode() == ISD::XOR) {
2159 // Simplify (X+Y) == (X+Z) --> Y == Z
2160 if (N0.getOpcode() == N1.getOpcode()) {
2161 if (N0.getOperand(0) == N1.getOperand(0))
2162 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
2163 if (N0.getOperand(1) == N1.getOperand(1))
2164 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
2165 if (isCommutativeBinOp(N0.getOpcode())) {
2166 // If X op Y == Y op X, try other combinations.
2167 if (N0.getOperand(0) == N1.getOperand(1))
2168 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
2170 if (N0.getOperand(1) == N1.getOperand(0))
2171 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
2176 // If RHS is a legal immediate value for a compare instruction, we need
2177 // to be careful about increasing register pressure needlessly.
2178 bool LegalRHSImm = false;
2180 if (auto *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2181 if (auto *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2182 // Turn (X+C1) == C2 --> X == C2-C1
2183 if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
2184 return DAG.getSetCC(dl, VT, N0.getOperand(0),
2185 DAG.getConstant(RHSC->getAPIntValue()-
2186 LHSR->getAPIntValue(),
2187 dl, N0.getValueType()), Cond);
2190 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2191 if (N0.getOpcode() == ISD::XOR)
2192 // If we know that all of the inverted bits are zero, don't bother
2193 // performing the inversion.
2194 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
2196 DAG.getSetCC(dl, VT, N0.getOperand(0),
2197 DAG.getConstant(LHSR->getAPIntValue() ^
2198 RHSC->getAPIntValue(),
2199 dl, N0.getValueType()),
2203 // Turn (C1-X) == C2 --> X == C1-C2
2204 if (auto *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2205 if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
2207 DAG.getSetCC(dl, VT, N0.getOperand(1),
2208 DAG.getConstant(SUBC->getAPIntValue() -
2209 RHSC->getAPIntValue(),
2210 dl, N0.getValueType()),
2215 // Could RHSC fold directly into a compare?
2216 if (RHSC->getValueType(0).getSizeInBits() <= 64)
2217 LegalRHSImm = isLegalICmpImmediate(RHSC->getSExtValue());
2220 // Simplify (X+Z) == X --> Z == 0
2221 // Don't do this if X is an immediate that can fold into a cmp
2222 // instruction and X+Z has other uses. It could be an induction variable
2223 // chain, and the transform would increase register pressure.
2224 if (!LegalRHSImm || N0.getNode()->hasOneUse()) {
2225 if (N0.getOperand(0) == N1)
2226 return DAG.getSetCC(dl, VT, N0.getOperand(1),
2227 DAG.getConstant(0, dl, N0.getValueType()), Cond);
2228 if (N0.getOperand(1) == N1) {
2229 if (isCommutativeBinOp(N0.getOpcode()))
2230 return DAG.getSetCC(dl, VT, N0.getOperand(0),
2231 DAG.getConstant(0, dl, N0.getValueType()),
2233 if (N0.getNode()->hasOneUse()) {
2234 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2235 auto &DL = DAG.getDataLayout();
2236 // (Z-X) == X --> Z == X<<1
2237 SDValue SH = DAG.getNode(
2238 ISD::SHL, dl, N1.getValueType(), N1,
2239 DAG.getConstant(1, dl,
2240 getShiftAmountTy(N1.getValueType(), DL)));
2241 if (!DCI.isCalledByLegalizer())
2242 DCI.AddToWorklist(SH.getNode());
2243 return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
2249 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2250 N1.getOpcode() == ISD::XOR) {
2251 // Simplify X == (X+Z) --> Z == 0
2252 if (N1.getOperand(0) == N0)
2253 return DAG.getSetCC(dl, VT, N1.getOperand(1),
2254 DAG.getConstant(0, dl, N1.getValueType()), Cond);
2255 if (N1.getOperand(1) == N0) {
2256 if (isCommutativeBinOp(N1.getOpcode()))
2257 return DAG.getSetCC(dl, VT, N1.getOperand(0),
2258 DAG.getConstant(0, dl, N1.getValueType()), Cond);
2259 if (N1.getNode()->hasOneUse()) {
2260 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2261 auto &DL = DAG.getDataLayout();
2262 // X == (Z-X) --> X<<1 == Z
2263 SDValue SH = DAG.getNode(
2264 ISD::SHL, dl, N1.getValueType(), N0,
2265 DAG.getConstant(1, dl, getShiftAmountTy(N0.getValueType(), DL)));
2266 if (!DCI.isCalledByLegalizer())
2267 DCI.AddToWorklist(SH.getNode());
2268 return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
2273 if (SDValue V = simplifySetCCWithAnd(VT, N0, N1, Cond, DCI, dl))
2277 // Fold away ALL boolean setcc's.
2279 if (N0.getValueType() == MVT::i1 && foldBooleans) {
2281 default: llvm_unreachable("Unknown integer setcc!");
2282 case ISD::SETEQ: // X == Y -> ~(X^Y)
2283 Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2284 N0 = DAG.getNOT(dl, Temp, MVT::i1);
2285 if (!DCI.isCalledByLegalizer())
2286 DCI.AddToWorklist(Temp.getNode());
2288 case ISD::SETNE: // X != Y --> (X^Y)
2289 N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2291 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y
2292 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y
2293 Temp = DAG.getNOT(dl, N0, MVT::i1);
2294 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
2295 if (!DCI.isCalledByLegalizer())
2296 DCI.AddToWorklist(Temp.getNode());
2298 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X
2299 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X
2300 Temp = DAG.getNOT(dl, N1, MVT::i1);
2301 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
2302 if (!DCI.isCalledByLegalizer())
2303 DCI.AddToWorklist(Temp.getNode());
2305 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y
2306 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y
2307 Temp = DAG.getNOT(dl, N0, MVT::i1);
2308 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
2309 if (!DCI.isCalledByLegalizer())
2310 DCI.AddToWorklist(Temp.getNode());
2312 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X
2313 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X
2314 Temp = DAG.getNOT(dl, N1, MVT::i1);
2315 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
2318 if (VT != MVT::i1) {
2319 if (!DCI.isCalledByLegalizer())
2320 DCI.AddToWorklist(N0.getNode());
2321 // FIXME: If running after legalize, we probably can't do this.
2322 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
2327 // Could not fold it.
2331 /// Returns true (and the GlobalValue and the offset) if the node is a
2332 /// GlobalAddress + offset.
2333 bool TargetLowering::isGAPlusOffset(SDNode *N, const GlobalValue *&GA,
2334 int64_t &Offset) const {
2335 if (auto *GASD = dyn_cast<GlobalAddressSDNode>(N)) {
2336 GA = GASD->getGlobal();
2337 Offset += GASD->getOffset();
2341 if (N->getOpcode() == ISD::ADD) {
2342 SDValue N1 = N->getOperand(0);
2343 SDValue N2 = N->getOperand(1);
2344 if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
2345 if (auto *V = dyn_cast<ConstantSDNode>(N2)) {
2346 Offset += V->getSExtValue();
2349 } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
2350 if (auto *V = dyn_cast<ConstantSDNode>(N1)) {
2351 Offset += V->getSExtValue();
2360 SDValue TargetLowering::PerformDAGCombine(SDNode *N,
2361 DAGCombinerInfo &DCI) const {
2362 // Default implementation: no optimization.
2366 //===----------------------------------------------------------------------===//
2367 // Inline Assembler Implementation Methods
2368 //===----------------------------------------------------------------------===//
2370 TargetLowering::ConstraintType
2371 TargetLowering::getConstraintType(StringRef Constraint) const {
2372 unsigned S = Constraint.size();
2375 switch (Constraint[0]) {
2377 case 'r': return C_RegisterClass;
2379 case 'o': // offsetable
2380 case 'V': // not offsetable
2382 case 'i': // Simple Integer or Relocatable Constant
2383 case 'n': // Simple Integer
2384 case 'E': // Floating Point Constant
2385 case 'F': // Floating Point Constant
2386 case 's': // Relocatable Constant
2387 case 'p': // Address.
2388 case 'X': // Allow ANY value.
2389 case 'I': // Target registers.
2403 if (S > 1 && Constraint[0] == '{' && Constraint[S-1] == '}') {
2404 if (S == 8 && Constraint.substr(1, 6) == "memory") // "{memory}"
2411 /// Try to replace an X constraint, which matches anything, with another that
2412 /// has more specific requirements based on the type of the corresponding
2414 const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
2415 if (ConstraintVT.isInteger())
2417 if (ConstraintVT.isFloatingPoint())
2418 return "f"; // works for many targets
2422 /// Lower the specified operand into the Ops vector.
2423 /// If it is invalid, don't add anything to Ops.
2424 void TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
2425 std::string &Constraint,
2426 std::vector<SDValue> &Ops,
2427 SelectionDAG &DAG) const {
2429 if (Constraint.length() > 1) return;
2431 char ConstraintLetter = Constraint[0];
2432 switch (ConstraintLetter) {
2434 case 'X': // Allows any operand; labels (basic block) use this.
2435 if (Op.getOpcode() == ISD::BasicBlock) {
2440 case 'i': // Simple Integer or Relocatable Constant
2441 case 'n': // Simple Integer
2442 case 's': { // Relocatable Constant
2443 // These operands are interested in values of the form (GV+C), where C may
2444 // be folded in as an offset of GV, or it may be explicitly added. Also, it
2445 // is possible and fine if either GV or C are missing.
2446 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2447 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2449 // If we have "(add GV, C)", pull out GV/C
2450 if (Op.getOpcode() == ISD::ADD) {
2451 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2452 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
2454 C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
2455 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
2463 // If we find a valid operand, map to the TargetXXX version so that the
2464 // value itself doesn't get selected.
2465 if (GA) { // Either &GV or &GV+C
2466 if (ConstraintLetter != 'n') {
2467 int64_t Offs = GA->getOffset();
2468 if (C) Offs += C->getZExtValue();
2469 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
2470 C ? SDLoc(C) : SDLoc(),
2471 Op.getValueType(), Offs));
2475 if (C) { // just C, no GV.
2476 // Simple constants are not allowed for 's'.
2477 if (ConstraintLetter != 's') {
2478 // gcc prints these as sign extended. Sign extend value to 64 bits
2479 // now; without this it would get ZExt'd later in
2480 // ScheduleDAGSDNodes::EmitNode, which is very generic.
2481 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
2482 SDLoc(C), MVT::i64));
2491 std::pair<unsigned, const TargetRegisterClass *>
2492 TargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *RI,
2493 StringRef Constraint,
2495 if (Constraint.empty() || Constraint[0] != '{')
2496 return std::make_pair(0u, static_cast<TargetRegisterClass*>(nullptr));
2497 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
2499 // Remove the braces from around the name.
2500 StringRef RegName(Constraint.data()+1, Constraint.size()-2);
2502 std::pair<unsigned, const TargetRegisterClass*> R =
2503 std::make_pair(0u, static_cast<const TargetRegisterClass*>(nullptr));
2505 // Figure out which register class contains this reg.
2506 for (const TargetRegisterClass *RC : RI->regclasses()) {
2507 // If none of the value types for this register class are valid, we
2508 // can't use it. For example, 64-bit reg classes on 32-bit targets.
2509 if (!isLegalRC(*RI, *RC))
2512 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
2514 if (RegName.equals_lower(RI->getRegAsmName(*I))) {
2515 std::pair<unsigned, const TargetRegisterClass*> S =
2516 std::make_pair(*I, RC);
2518 // If this register class has the requested value type, return it,
2519 // otherwise keep searching and return the first class found
2520 // if no other is found which explicitly has the requested type.
2521 if (RI->isTypeLegalForClass(*RC, VT))
2532 //===----------------------------------------------------------------------===//
2533 // Constraint Selection.
2535 /// Return true of this is an input operand that is a matching constraint like
2537 bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
2538 assert(!ConstraintCode.empty() && "No known constraint!");
2539 return isdigit(static_cast<unsigned char>(ConstraintCode[0]));
2542 /// If this is an input matching constraint, this method returns the output
2543 /// operand it matches.
2544 unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
2545 assert(!ConstraintCode.empty() && "No known constraint!");
2546 return atoi(ConstraintCode.c_str());
2549 /// Split up the constraint string from the inline assembly value into the
2550 /// specific constraints and their prefixes, and also tie in the associated
2552 /// If this returns an empty vector, and if the constraint string itself
2553 /// isn't empty, there was an error parsing.
2554 TargetLowering::AsmOperandInfoVector
2555 TargetLowering::ParseConstraints(const DataLayout &DL,
2556 const TargetRegisterInfo *TRI,
2557 ImmutableCallSite CS) const {
2558 /// Information about all of the constraints.
2559 AsmOperandInfoVector ConstraintOperands;
2560 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
2561 unsigned maCount = 0; // Largest number of multiple alternative constraints.
2563 // Do a prepass over the constraints, canonicalizing them, and building up the
2564 // ConstraintOperands list.
2565 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
2566 unsigned ResNo = 0; // ResNo - The result number of the next output.
2568 for (InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {
2569 ConstraintOperands.emplace_back(std::move(CI));
2570 AsmOperandInfo &OpInfo = ConstraintOperands.back();
2572 // Update multiple alternative constraint count.
2573 if (OpInfo.multipleAlternatives.size() > maCount)
2574 maCount = OpInfo.multipleAlternatives.size();
2576 OpInfo.ConstraintVT = MVT::Other;
2578 // Compute the value type for each operand.
2579 switch (OpInfo.Type) {
2580 case InlineAsm::isOutput:
2581 // Indirect outputs just consume an argument.
2582 if (OpInfo.isIndirect) {
2583 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2587 // The return value of the call is this value. As such, there is no
2588 // corresponding argument.
2589 assert(!CS.getType()->isVoidTy() &&
2591 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
2592 OpInfo.ConstraintVT =
2593 getSimpleValueType(DL, STy->getElementType(ResNo));
2595 assert(ResNo == 0 && "Asm only has one result!");
2596 OpInfo.ConstraintVT = getSimpleValueType(DL, CS.getType());
2600 case InlineAsm::isInput:
2601 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2603 case InlineAsm::isClobber:
2608 if (OpInfo.CallOperandVal) {
2609 llvm::Type *OpTy = OpInfo.CallOperandVal->getType();
2610 if (OpInfo.isIndirect) {
2611 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
2613 report_fatal_error("Indirect operand for inline asm not a pointer!");
2614 OpTy = PtrTy->getElementType();
2617 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
2618 if (StructType *STy = dyn_cast<StructType>(OpTy))
2619 if (STy->getNumElements() == 1)
2620 OpTy = STy->getElementType(0);
2622 // If OpTy is not a single value, it may be a struct/union that we
2623 // can tile with integers.
2624 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
2625 unsigned BitSize = DL.getTypeSizeInBits(OpTy);
2634 OpInfo.ConstraintVT =
2635 MVT::getVT(IntegerType::get(OpTy->getContext(), BitSize), true);
2638 } else if (PointerType *PT = dyn_cast<PointerType>(OpTy)) {
2639 unsigned PtrSize = DL.getPointerSizeInBits(PT->getAddressSpace());
2640 OpInfo.ConstraintVT = MVT::getIntegerVT(PtrSize);
2642 OpInfo.ConstraintVT = MVT::getVT(OpTy, true);
2647 // If we have multiple alternative constraints, select the best alternative.
2648 if (!ConstraintOperands.empty()) {
2650 unsigned bestMAIndex = 0;
2651 int bestWeight = -1;
2652 // weight: -1 = invalid match, and 0 = so-so match to 5 = good match.
2655 // Compute the sums of the weights for each alternative, keeping track
2656 // of the best (highest weight) one so far.
2657 for (maIndex = 0; maIndex < maCount; ++maIndex) {
2659 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2660 cIndex != eIndex; ++cIndex) {
2661 AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
2662 if (OpInfo.Type == InlineAsm::isClobber)
2665 // If this is an output operand with a matching input operand,
2666 // look up the matching input. If their types mismatch, e.g. one
2667 // is an integer, the other is floating point, or their sizes are
2668 // different, flag it as an maCantMatch.
2669 if (OpInfo.hasMatchingInput()) {
2670 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
2671 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
2672 if ((OpInfo.ConstraintVT.isInteger() !=
2673 Input.ConstraintVT.isInteger()) ||
2674 (OpInfo.ConstraintVT.getSizeInBits() !=
2675 Input.ConstraintVT.getSizeInBits())) {
2676 weightSum = -1; // Can't match.
2681 weight = getMultipleConstraintMatchWeight(OpInfo, maIndex);
2686 weightSum += weight;
2689 if (weightSum > bestWeight) {
2690 bestWeight = weightSum;
2691 bestMAIndex = maIndex;
2695 // Now select chosen alternative in each constraint.
2696 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2697 cIndex != eIndex; ++cIndex) {
2698 AsmOperandInfo& cInfo = ConstraintOperands[cIndex];
2699 if (cInfo.Type == InlineAsm::isClobber)
2701 cInfo.selectAlternative(bestMAIndex);
2706 // Check and hook up tied operands, choose constraint code to use.
2707 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2708 cIndex != eIndex; ++cIndex) {
2709 AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
2711 // If this is an output operand with a matching input operand, look up the
2712 // matching input. If their types mismatch, e.g. one is an integer, the
2713 // other is floating point, or their sizes are different, flag it as an
2715 if (OpInfo.hasMatchingInput()) {
2716 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
2718 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
2719 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
2720 getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
2721 OpInfo.ConstraintVT);
2722 std::pair<unsigned, const TargetRegisterClass *> InputRC =
2723 getRegForInlineAsmConstraint(TRI, Input.ConstraintCode,
2724 Input.ConstraintVT);
2725 if ((OpInfo.ConstraintVT.isInteger() !=
2726 Input.ConstraintVT.isInteger()) ||
2727 (MatchRC.second != InputRC.second)) {
2728 report_fatal_error("Unsupported asm: input constraint"
2729 " with a matching output constraint of"
2730 " incompatible type!");
2736 return ConstraintOperands;
2739 /// Return an integer indicating how general CT is.
2740 static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
2742 case TargetLowering::C_Other:
2743 case TargetLowering::C_Unknown:
2745 case TargetLowering::C_Register:
2747 case TargetLowering::C_RegisterClass:
2749 case TargetLowering::C_Memory:
2752 llvm_unreachable("Invalid constraint type");
2755 /// Examine constraint type and operand type and determine a weight value.
2756 /// This object must already have been set up with the operand type
2757 /// and the current alternative constraint selected.
2758 TargetLowering::ConstraintWeight
2759 TargetLowering::getMultipleConstraintMatchWeight(
2760 AsmOperandInfo &info, int maIndex) const {
2761 InlineAsm::ConstraintCodeVector *rCodes;
2762 if (maIndex >= (int)info.multipleAlternatives.size())
2763 rCodes = &info.Codes;
2765 rCodes = &info.multipleAlternatives[maIndex].Codes;
2766 ConstraintWeight BestWeight = CW_Invalid;
2768 // Loop over the options, keeping track of the most general one.
2769 for (unsigned i = 0, e = rCodes->size(); i != e; ++i) {
2770 ConstraintWeight weight =
2771 getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str());
2772 if (weight > BestWeight)
2773 BestWeight = weight;
2779 /// Examine constraint type and operand type and determine a weight value.
2780 /// This object must already have been set up with the operand type
2781 /// and the current alternative constraint selected.
2782 TargetLowering::ConstraintWeight
2783 TargetLowering::getSingleConstraintMatchWeight(
2784 AsmOperandInfo &info, const char *constraint) const {
2785 ConstraintWeight weight = CW_Invalid;
2786 Value *CallOperandVal = info.CallOperandVal;
2787 // If we don't have a value, we can't do a match,
2788 // but allow it at the lowest weight.
2789 if (!CallOperandVal)
2791 // Look at the constraint type.
2792 switch (*constraint) {
2793 case 'i': // immediate integer.
2794 case 'n': // immediate integer with a known value.
2795 if (isa<ConstantInt>(CallOperandVal))
2796 weight = CW_Constant;
2798 case 's': // non-explicit intregal immediate.
2799 if (isa<GlobalValue>(CallOperandVal))
2800 weight = CW_Constant;
2802 case 'E': // immediate float if host format.
2803 case 'F': // immediate float.
2804 if (isa<ConstantFP>(CallOperandVal))
2805 weight = CW_Constant;
2807 case '<': // memory operand with autodecrement.
2808 case '>': // memory operand with autoincrement.
2809 case 'm': // memory operand.
2810 case 'o': // offsettable memory operand
2811 case 'V': // non-offsettable memory operand
2814 case 'r': // general register.
2815 case 'g': // general register, memory operand or immediate integer.
2816 // note: Clang converts "g" to "imr".
2817 if (CallOperandVal->getType()->isIntegerTy())
2818 weight = CW_Register;
2820 case 'X': // any operand.
2822 weight = CW_Default;
2828 /// If there are multiple different constraints that we could pick for this
2829 /// operand (e.g. "imr") try to pick the 'best' one.
2830 /// This is somewhat tricky: constraints fall into four classes:
2831 /// Other -> immediates and magic values
2832 /// Register -> one specific register
2833 /// RegisterClass -> a group of regs
2834 /// Memory -> memory
2835 /// Ideally, we would pick the most specific constraint possible: if we have
2836 /// something that fits into a register, we would pick it. The problem here
2837 /// is that if we have something that could either be in a register or in
2838 /// memory that use of the register could cause selection of *other*
2839 /// operands to fail: they might only succeed if we pick memory. Because of
2840 /// this the heuristic we use is:
2842 /// 1) If there is an 'other' constraint, and if the operand is valid for
2843 /// that constraint, use it. This makes us take advantage of 'i'
2844 /// constraints when available.
2845 /// 2) Otherwise, pick the most general constraint present. This prefers
2846 /// 'm' over 'r', for example.
2848 static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
2849 const TargetLowering &TLI,
2850 SDValue Op, SelectionDAG *DAG) {
2851 assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
2852 unsigned BestIdx = 0;
2853 TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
2854 int BestGenerality = -1;
2856 // Loop over the options, keeping track of the most general one.
2857 for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
2858 TargetLowering::ConstraintType CType =
2859 TLI.getConstraintType(OpInfo.Codes[i]);
2861 // If this is an 'other' constraint, see if the operand is valid for it.
2862 // For example, on X86 we might have an 'rI' constraint. If the operand
2863 // is an integer in the range [0..31] we want to use I (saving a load
2864 // of a register), otherwise we must use 'r'.
2865 if (CType == TargetLowering::C_Other && Op.getNode()) {
2866 assert(OpInfo.Codes[i].size() == 1 &&
2867 "Unhandled multi-letter 'other' constraint");
2868 std::vector<SDValue> ResultOps;
2869 TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i],
2871 if (!ResultOps.empty()) {
2878 // Things with matching constraints can only be registers, per gcc
2879 // documentation. This mainly affects "g" constraints.
2880 if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
2883 // This constraint letter is more general than the previous one, use it.
2884 int Generality = getConstraintGenerality(CType);
2885 if (Generality > BestGenerality) {
2888 BestGenerality = Generality;
2892 OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
2893 OpInfo.ConstraintType = BestType;
2896 /// Determines the constraint code and constraint type to use for the specific
2897 /// AsmOperandInfo, setting OpInfo.ConstraintCode and OpInfo.ConstraintType.
2898 void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
2900 SelectionDAG *DAG) const {
2901 assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
2903 // Single-letter constraints ('r') are very common.
2904 if (OpInfo.Codes.size() == 1) {
2905 OpInfo.ConstraintCode = OpInfo.Codes[0];
2906 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2908 ChooseConstraint(OpInfo, *this, Op, DAG);
2911 // 'X' matches anything.
2912 if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
2913 // Labels and constants are handled elsewhere ('X' is the only thing
2914 // that matches labels). For Functions, the type here is the type of
2915 // the result, which is not what we want to look at; leave them alone.
2916 Value *v = OpInfo.CallOperandVal;
2917 if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
2918 OpInfo.CallOperandVal = v;
2922 // Otherwise, try to resolve it to something we know about by looking at
2923 // the actual operand type.
2924 if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
2925 OpInfo.ConstraintCode = Repl;
2926 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2931 /// \brief Given an exact SDIV by a constant, create a multiplication
2932 /// with the multiplicative inverse of the constant.
2933 static SDValue BuildExactSDIV(const TargetLowering &TLI, SDValue Op1, APInt d,
2934 const SDLoc &dl, SelectionDAG &DAG,
2935 std::vector<SDNode *> &Created) {
2936 assert(d != 0 && "Division by zero!");
2938 // Shift the value upfront if it is even, so the LSB is one.
2939 unsigned ShAmt = d.countTrailingZeros();
2941 // TODO: For UDIV use SRL instead of SRA.
2943 DAG.getConstant(ShAmt, dl, TLI.getShiftAmountTy(Op1.getValueType(),
2944 DAG.getDataLayout()));
2946 Flags.setExact(true);
2947 Op1 = DAG.getNode(ISD::SRA, dl, Op1.getValueType(), Op1, Amt, Flags);
2948 Created.push_back(Op1.getNode());
2949 d.ashrInPlace(ShAmt);
2952 // Calculate the multiplicative inverse, using Newton's method.
2954 while ((t = d*xn) != 1)
2955 xn *= APInt(d.getBitWidth(), 2) - t;
2957 SDValue Op2 = DAG.getConstant(xn, dl, Op1.getValueType());
2958 SDValue Mul = DAG.getNode(ISD::MUL, dl, Op1.getValueType(), Op1, Op2);
2959 Created.push_back(Mul.getNode());
2963 SDValue TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
2965 std::vector<SDNode *> *Created) const {
2966 AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes();
2967 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2968 if (TLI.isIntDivCheap(N->getValueType(0), Attr))
2969 return SDValue(N,0); // Lower SDIV as SDIV
2973 /// \brief Given an ISD::SDIV node expressing a divide by constant,
2974 /// return a DAG expression to select that will generate the same value by
2975 /// multiplying by a magic number.
2976 /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
2977 SDValue TargetLowering::BuildSDIV(SDNode *N, const APInt &Divisor,
2978 SelectionDAG &DAG, bool IsAfterLegalization,
2979 std::vector<SDNode *> *Created) const {
2980 assert(Created && "No vector to hold sdiv ops.");
2982 EVT VT = N->getValueType(0);
2985 // Check to see if we can do this.
2986 // FIXME: We should be more aggressive here.
2987 if (!isTypeLegal(VT))
2990 // If the sdiv has an 'exact' bit we can use a simpler lowering.
2991 if (N->getFlags().hasExact())
2992 return BuildExactSDIV(*this, N->getOperand(0), Divisor, dl, DAG, *Created);
2994 APInt::ms magics = Divisor.magic();
2996 // Multiply the numerator (operand 0) by the magic value
2997 // FIXME: We should support doing a MUL in a wider type
2999 if (IsAfterLegalization ? isOperationLegal(ISD::MULHS, VT) :
3000 isOperationLegalOrCustom(ISD::MULHS, VT))
3001 Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
3002 DAG.getConstant(magics.m, dl, VT));
3003 else if (IsAfterLegalization ? isOperationLegal(ISD::SMUL_LOHI, VT) :
3004 isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
3005 Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
3007 DAG.getConstant(magics.m, dl, VT)).getNode(), 1);
3009 return SDValue(); // No mulhs or equvialent
3010 // If d > 0 and m < 0, add the numerator
3011 if (Divisor.isStrictlyPositive() && magics.m.isNegative()) {
3012 Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
3013 Created->push_back(Q.getNode());
3015 // If d < 0 and m > 0, subtract the numerator.
3016 if (Divisor.isNegative() && magics.m.isStrictlyPositive()) {
3017 Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
3018 Created->push_back(Q.getNode());
3020 auto &DL = DAG.getDataLayout();
3021 // Shift right algebraic if shift value is nonzero
3024 ISD::SRA, dl, VT, Q,
3025 DAG.getConstant(magics.s, dl, getShiftAmountTy(Q.getValueType(), DL)));
3026 Created->push_back(Q.getNode());
3028 // Extract the sign bit and add it to the quotient
3030 DAG.getNode(ISD::SRL, dl, VT, Q,
3031 DAG.getConstant(VT.getScalarSizeInBits() - 1, dl,
3032 getShiftAmountTy(Q.getValueType(), DL)));
3033 Created->push_back(T.getNode());
3034 return DAG.getNode(ISD::ADD, dl, VT, Q, T);
3037 /// \brief Given an ISD::UDIV node expressing a divide by constant,
3038 /// return a DAG expression to select that will generate the same value by
3039 /// multiplying by a magic number.
3040 /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
3041 SDValue TargetLowering::BuildUDIV(SDNode *N, const APInt &Divisor,
3042 SelectionDAG &DAG, bool IsAfterLegalization,
3043 std::vector<SDNode *> *Created) const {
3044 assert(Created && "No vector to hold udiv ops.");
3046 EVT VT = N->getValueType(0);
3048 auto &DL = DAG.getDataLayout();
3050 // Check to see if we can do this.
3051 // FIXME: We should be more aggressive here.
3052 if (!isTypeLegal(VT))
3055 // FIXME: We should use a narrower constant when the upper
3056 // bits are known to be zero.
3057 APInt::mu magics = Divisor.magicu();
3059 SDValue Q = N->getOperand(0);
3061 // If the divisor is even, we can avoid using the expensive fixup by shifting
3062 // the divided value upfront.
3063 if (magics.a != 0 && !Divisor[0]) {
3064 unsigned Shift = Divisor.countTrailingZeros();
3066 ISD::SRL, dl, VT, Q,
3067 DAG.getConstant(Shift, dl, getShiftAmountTy(Q.getValueType(), DL)));
3068 Created->push_back(Q.getNode());
3070 // Get magic number for the shifted divisor.
3071 magics = Divisor.lshr(Shift).magicu(Shift);
3072 assert(magics.a == 0 && "Should use cheap fixup now");
3075 // Multiply the numerator (operand 0) by the magic value
3076 // FIXME: We should support doing a MUL in a wider type
3077 if (IsAfterLegalization ? isOperationLegal(ISD::MULHU, VT) :
3078 isOperationLegalOrCustom(ISD::MULHU, VT))
3079 Q = DAG.getNode(ISD::MULHU, dl, VT, Q, DAG.getConstant(magics.m, dl, VT));
3080 else if (IsAfterLegalization ? isOperationLegal(ISD::UMUL_LOHI, VT) :
3081 isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
3082 Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), Q,
3083 DAG.getConstant(magics.m, dl, VT)).getNode(), 1);
3085 return SDValue(); // No mulhu or equivalent
3087 Created->push_back(Q.getNode());
3089 if (magics.a == 0) {
3090 assert(magics.s < Divisor.getBitWidth() &&
3091 "We shouldn't generate an undefined shift!");
3093 ISD::SRL, dl, VT, Q,
3094 DAG.getConstant(magics.s, dl, getShiftAmountTy(Q.getValueType(), DL)));
3096 SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
3097 Created->push_back(NPQ.getNode());
3099 ISD::SRL, dl, VT, NPQ,
3100 DAG.getConstant(1, dl, getShiftAmountTy(NPQ.getValueType(), DL)));
3101 Created->push_back(NPQ.getNode());
3102 NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
3103 Created->push_back(NPQ.getNode());
3105 ISD::SRL, dl, VT, NPQ,
3106 DAG.getConstant(magics.s - 1, dl,
3107 getShiftAmountTy(NPQ.getValueType(), DL)));
3111 bool TargetLowering::
3112 verifyReturnAddressArgumentIsConstant(SDValue Op, SelectionDAG &DAG) const {
3113 if (!isa<ConstantSDNode>(Op.getOperand(0))) {
3114 DAG.getContext()->emitError("argument to '__builtin_return_address' must "
3115 "be a constant integer");
3122 //===----------------------------------------------------------------------===//
3123 // Legalization Utilities
3124 //===----------------------------------------------------------------------===//
3126 bool TargetLowering::expandMUL_LOHI(unsigned Opcode, EVT VT, SDLoc dl,
3127 SDValue LHS, SDValue RHS,
3128 SmallVectorImpl<SDValue> &Result,
3129 EVT HiLoVT, SelectionDAG &DAG,
3130 MulExpansionKind Kind, SDValue LL,
3131 SDValue LH, SDValue RL, SDValue RH) const {
3132 assert(Opcode == ISD::MUL || Opcode == ISD::UMUL_LOHI ||
3133 Opcode == ISD::SMUL_LOHI);
3135 bool HasMULHS = (Kind == MulExpansionKind::Always) ||
3136 isOperationLegalOrCustom(ISD::MULHS, HiLoVT);
3137 bool HasMULHU = (Kind == MulExpansionKind::Always) ||
3138 isOperationLegalOrCustom(ISD::MULHU, HiLoVT);
3139 bool HasSMUL_LOHI = (Kind == MulExpansionKind::Always) ||
3140 isOperationLegalOrCustom(ISD::SMUL_LOHI, HiLoVT);
3141 bool HasUMUL_LOHI = (Kind == MulExpansionKind::Always) ||
3142 isOperationLegalOrCustom(ISD::UMUL_LOHI, HiLoVT);
3144 if (!HasMULHU && !HasMULHS && !HasUMUL_LOHI && !HasSMUL_LOHI)
3147 unsigned OuterBitSize = VT.getScalarSizeInBits();
3148 unsigned InnerBitSize = HiLoVT.getScalarSizeInBits();
3149 unsigned LHSSB = DAG.ComputeNumSignBits(LHS);
3150 unsigned RHSSB = DAG.ComputeNumSignBits(RHS);
3152 // LL, LH, RL, and RH must be either all NULL or all set to a value.
3153 assert((LL.getNode() && LH.getNode() && RL.getNode() && RH.getNode()) ||
3154 (!LL.getNode() && !LH.getNode() && !RL.getNode() && !RH.getNode()));
3156 SDVTList VTs = DAG.getVTList(HiLoVT, HiLoVT);
3157 auto MakeMUL_LOHI = [&](SDValue L, SDValue R, SDValue &Lo, SDValue &Hi,
3158 bool Signed) -> bool {
3159 if ((Signed && HasSMUL_LOHI) || (!Signed && HasUMUL_LOHI)) {
3160 Lo = DAG.getNode(Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI, dl, VTs, L, R);
3161 Hi = SDValue(Lo.getNode(), 1);
3164 if ((Signed && HasMULHS) || (!Signed && HasMULHU)) {
3165 Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, L, R);
3166 Hi = DAG.getNode(Signed ? ISD::MULHS : ISD::MULHU, dl, HiLoVT, L, R);
3174 if (!LL.getNode() && !RL.getNode() &&
3175 isOperationLegalOrCustom(ISD::TRUNCATE, HiLoVT)) {
3176 LL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, LHS);
3177 RL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, RHS);
3183 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
3184 if (DAG.MaskedValueIsZero(LHS, HighMask) &&
3185 DAG.MaskedValueIsZero(RHS, HighMask)) {
3186 // The inputs are both zero-extended.
3187 if (MakeMUL_LOHI(LL, RL, Lo, Hi, false)) {
3188 Result.push_back(Lo);
3189 Result.push_back(Hi);
3190 if (Opcode != ISD::MUL) {
3191 SDValue Zero = DAG.getConstant(0, dl, HiLoVT);
3192 Result.push_back(Zero);
3193 Result.push_back(Zero);
3199 if (!VT.isVector() && Opcode == ISD::MUL && LHSSB > InnerBitSize &&
3200 RHSSB > InnerBitSize) {
3201 // The input values are both sign-extended.
3202 // TODO non-MUL case?
3203 if (MakeMUL_LOHI(LL, RL, Lo, Hi, true)) {
3204 Result.push_back(Lo);
3205 Result.push_back(Hi);
3210 unsigned ShiftAmount = OuterBitSize - InnerBitSize;
3211 EVT ShiftAmountTy = getShiftAmountTy(VT, DAG.getDataLayout());
3212 if (APInt::getMaxValue(ShiftAmountTy.getSizeInBits()).ult(ShiftAmount)) {
3213 // FIXME getShiftAmountTy does not always return a sensible result when VT
3214 // is an illegal type, and so the type may be too small to fit the shift
3215 // amount. Override it with i32. The shift will have to be legalized.
3216 ShiftAmountTy = MVT::i32;
3218 SDValue Shift = DAG.getConstant(ShiftAmount, dl, ShiftAmountTy);
3220 if (!LH.getNode() && !RH.getNode() &&
3221 isOperationLegalOrCustom(ISD::SRL, VT) &&
3222 isOperationLegalOrCustom(ISD::TRUNCATE, HiLoVT)) {
3223 LH = DAG.getNode(ISD::SRL, dl, VT, LHS, Shift);
3224 LH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, LH);
3225 RH = DAG.getNode(ISD::SRL, dl, VT, RHS, Shift);
3226 RH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, RH);
3232 if (!MakeMUL_LOHI(LL, RL, Lo, Hi, false))
3235 Result.push_back(Lo);
3237 if (Opcode == ISD::MUL) {
3238 RH = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RH);
3239 LH = DAG.getNode(ISD::MUL, dl, HiLoVT, LH, RL);
3240 Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, RH);
3241 Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, LH);
3242 Result.push_back(Hi);
3246 // Compute the full width result.
3247 auto Merge = [&](SDValue Lo, SDValue Hi) -> SDValue {
3248 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
3249 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Hi);
3250 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
3251 return DAG.getNode(ISD::OR, dl, VT, Lo, Hi);
3254 SDValue Next = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Hi);
3255 if (!MakeMUL_LOHI(LL, RH, Lo, Hi, false))
3258 // This is effectively the add part of a multiply-add of half-sized operands,
3259 // so it cannot overflow.
3260 Next = DAG.getNode(ISD::ADD, dl, VT, Next, Merge(Lo, Hi));
3262 if (!MakeMUL_LOHI(LH, RL, Lo, Hi, false))
3265 Next = DAG.getNode(ISD::ADDC, dl, DAG.getVTList(VT, MVT::Glue), Next,
3268 SDValue Carry = Next.getValue(1);
3269 Result.push_back(DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, Next));
3270 Next = DAG.getNode(ISD::SRL, dl, VT, Next, Shift);
3272 if (!MakeMUL_LOHI(LH, RH, Lo, Hi, Opcode == ISD::SMUL_LOHI))
3275 SDValue Zero = DAG.getConstant(0, dl, HiLoVT);
3276 Hi = DAG.getNode(ISD::ADDE, dl, DAG.getVTList(HiLoVT, MVT::Glue), Hi, Zero,
3278 Next = DAG.getNode(ISD::ADD, dl, VT, Next, Merge(Lo, Hi));
3280 if (Opcode == ISD::SMUL_LOHI) {
3281 SDValue NextSub = DAG.getNode(ISD::SUB, dl, VT, Next,
3282 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, RL));
3283 Next = DAG.getSelectCC(dl, LH, Zero, NextSub, Next, ISD::SETLT);
3285 NextSub = DAG.getNode(ISD::SUB, dl, VT, Next,
3286 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, LL));
3287 Next = DAG.getSelectCC(dl, RH, Zero, NextSub, Next, ISD::SETLT);
3290 Result.push_back(DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, Next));
3291 Next = DAG.getNode(ISD::SRL, dl, VT, Next, Shift);
3292 Result.push_back(DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, Next));
3296 bool TargetLowering::expandMUL(SDNode *N, SDValue &Lo, SDValue &Hi, EVT HiLoVT,
3297 SelectionDAG &DAG, MulExpansionKind Kind,
3298 SDValue LL, SDValue LH, SDValue RL,
3300 SmallVector<SDValue, 2> Result;
3301 bool Ok = expandMUL_LOHI(N->getOpcode(), N->getValueType(0), N,
3302 N->getOperand(0), N->getOperand(1), Result, HiLoVT,
3303 DAG, Kind, LL, LH, RL, RH);
3305 assert(Result.size() == 2);
3312 bool TargetLowering::expandFP_TO_SINT(SDNode *Node, SDValue &Result,
3313 SelectionDAG &DAG) const {
3314 EVT VT = Node->getOperand(0).getValueType();
3315 EVT NVT = Node->getValueType(0);
3316 SDLoc dl(SDValue(Node, 0));
3318 // FIXME: Only f32 to i64 conversions are supported.
3319 if (VT != MVT::f32 || NVT != MVT::i64)
3322 // Expand f32 -> i64 conversion
3323 // This algorithm comes from compiler-rt's implementation of fixsfdi:
3324 // https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/fixsfdi.c
3325 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(),
3326 VT.getSizeInBits());
3327 SDValue ExponentMask = DAG.getConstant(0x7F800000, dl, IntVT);
3328 SDValue ExponentLoBit = DAG.getConstant(23, dl, IntVT);
3329 SDValue Bias = DAG.getConstant(127, dl, IntVT);
3330 SDValue SignMask = DAG.getConstant(APInt::getSignMask(VT.getSizeInBits()), dl,
3332 SDValue SignLowBit = DAG.getConstant(VT.getSizeInBits() - 1, dl, IntVT);
3333 SDValue MantissaMask = DAG.getConstant(0x007FFFFF, dl, IntVT);
3335 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, IntVT, Node->getOperand(0));
3337 auto &DL = DAG.getDataLayout();
3338 SDValue ExponentBits = DAG.getNode(
3339 ISD::SRL, dl, IntVT, DAG.getNode(ISD::AND, dl, IntVT, Bits, ExponentMask),
3340 DAG.getZExtOrTrunc(ExponentLoBit, dl, getShiftAmountTy(IntVT, DL)));
3341 SDValue Exponent = DAG.getNode(ISD::SUB, dl, IntVT, ExponentBits, Bias);
3343 SDValue Sign = DAG.getNode(
3344 ISD::SRA, dl, IntVT, DAG.getNode(ISD::AND, dl, IntVT, Bits, SignMask),
3345 DAG.getZExtOrTrunc(SignLowBit, dl, getShiftAmountTy(IntVT, DL)));
3346 Sign = DAG.getSExtOrTrunc(Sign, dl, NVT);
3348 SDValue R = DAG.getNode(ISD::OR, dl, IntVT,
3349 DAG.getNode(ISD::AND, dl, IntVT, Bits, MantissaMask),
3350 DAG.getConstant(0x00800000, dl, IntVT));
3352 R = DAG.getZExtOrTrunc(R, dl, NVT);
3354 R = DAG.getSelectCC(
3355 dl, Exponent, ExponentLoBit,
3356 DAG.getNode(ISD::SHL, dl, NVT, R,
3358 DAG.getNode(ISD::SUB, dl, IntVT, Exponent, ExponentLoBit),
3359 dl, getShiftAmountTy(IntVT, DL))),
3360 DAG.getNode(ISD::SRL, dl, NVT, R,
3362 DAG.getNode(ISD::SUB, dl, IntVT, ExponentLoBit, Exponent),
3363 dl, getShiftAmountTy(IntVT, DL))),
3366 SDValue Ret = DAG.getNode(ISD::SUB, dl, NVT,
3367 DAG.getNode(ISD::XOR, dl, NVT, R, Sign),
3370 Result = DAG.getSelectCC(dl, Exponent, DAG.getConstant(0, dl, IntVT),
3371 DAG.getConstant(0, dl, NVT), Ret, ISD::SETLT);
3375 SDValue TargetLowering::scalarizeVectorLoad(LoadSDNode *LD,
3376 SelectionDAG &DAG) const {
3378 SDValue Chain = LD->getChain();
3379 SDValue BasePTR = LD->getBasePtr();
3380 EVT SrcVT = LD->getMemoryVT();
3381 ISD::LoadExtType ExtType = LD->getExtensionType();
3383 unsigned NumElem = SrcVT.getVectorNumElements();
3385 EVT SrcEltVT = SrcVT.getScalarType();
3386 EVT DstEltVT = LD->getValueType(0).getScalarType();
3388 unsigned Stride = SrcEltVT.getSizeInBits() / 8;
3389 assert(SrcEltVT.isByteSized());
3391 EVT PtrVT = BasePTR.getValueType();
3393 SmallVector<SDValue, 8> Vals;
3394 SmallVector<SDValue, 8> LoadChains;
3396 for (unsigned Idx = 0; Idx < NumElem; ++Idx) {
3397 SDValue ScalarLoad =
3398 DAG.getExtLoad(ExtType, SL, DstEltVT, Chain, BasePTR,
3399 LD->getPointerInfo().getWithOffset(Idx * Stride),
3400 SrcEltVT, MinAlign(LD->getAlignment(), Idx * Stride),
3401 LD->getMemOperand()->getFlags(), LD->getAAInfo());
3403 BasePTR = DAG.getNode(ISD::ADD, SL, PtrVT, BasePTR,
3404 DAG.getConstant(Stride, SL, PtrVT));
3406 Vals.push_back(ScalarLoad.getValue(0));
3407 LoadChains.push_back(ScalarLoad.getValue(1));
3410 SDValue NewChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoadChains);
3411 SDValue Value = DAG.getBuildVector(LD->getValueType(0), SL, Vals);
3413 return DAG.getMergeValues({ Value, NewChain }, SL);
3416 // FIXME: This relies on each element having a byte size, otherwise the stride
3417 // is 0 and just overwrites the same location. ExpandStore currently expects
3418 // this broken behavior.
3419 SDValue TargetLowering::scalarizeVectorStore(StoreSDNode *ST,
3420 SelectionDAG &DAG) const {
3423 SDValue Chain = ST->getChain();
3424 SDValue BasePtr = ST->getBasePtr();
3425 SDValue Value = ST->getValue();
3426 EVT StVT = ST->getMemoryVT();
3428 // The type of the data we want to save
3429 EVT RegVT = Value.getValueType();
3430 EVT RegSclVT = RegVT.getScalarType();
3432 // The type of data as saved in memory.
3433 EVT MemSclVT = StVT.getScalarType();
3435 // Store Stride in bytes
3436 unsigned Stride = MemSclVT.getSizeInBits() / 8;
3437 EVT IdxVT = getVectorIdxTy(DAG.getDataLayout());
3438 unsigned NumElem = StVT.getVectorNumElements();
3440 // Extract each of the elements from the original vector and save them into
3441 // memory individually.
3442 SmallVector<SDValue, 8> Stores;
3443 for (unsigned Idx = 0; Idx < NumElem; ++Idx) {
3444 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, RegSclVT, Value,
3445 DAG.getConstant(Idx, SL, IdxVT));
3447 SDValue Ptr = DAG.getObjectPtrOffset(SL, BasePtr, Idx * Stride);
3449 // This scalar TruncStore may be illegal, but we legalize it later.
3450 SDValue Store = DAG.getTruncStore(
3451 Chain, SL, Elt, Ptr, ST->getPointerInfo().getWithOffset(Idx * Stride),
3452 MemSclVT, MinAlign(ST->getAlignment(), Idx * Stride),
3453 ST->getMemOperand()->getFlags(), ST->getAAInfo());
3455 Stores.push_back(Store);
3458 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, Stores);
3461 std::pair<SDValue, SDValue>
3462 TargetLowering::expandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG) const {
3463 assert(LD->getAddressingMode() == ISD::UNINDEXED &&
3464 "unaligned indexed loads not implemented!");
3465 SDValue Chain = LD->getChain();
3466 SDValue Ptr = LD->getBasePtr();
3467 EVT VT = LD->getValueType(0);
3468 EVT LoadedVT = LD->getMemoryVT();
3470 auto &MF = DAG.getMachineFunction();
3472 if (VT.isFloatingPoint() || VT.isVector()) {
3473 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
3474 if (isTypeLegal(intVT) && isTypeLegal(LoadedVT)) {
3475 if (!isOperationLegalOrCustom(ISD::LOAD, intVT)) {
3476 // Scalarize the load and let the individual components be handled.
3477 SDValue Scalarized = scalarizeVectorLoad(LD, DAG);
3478 return std::make_pair(Scalarized.getValue(0), Scalarized.getValue(1));
3481 // Expand to a (misaligned) integer load of the same size,
3482 // then bitconvert to floating point or vector.
3483 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr,
3484 LD->getMemOperand());
3485 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
3487 Result = DAG.getNode(VT.isFloatingPoint() ? ISD::FP_EXTEND :
3488 ISD::ANY_EXTEND, dl, VT, Result);
3490 return std::make_pair(Result, newLoad.getValue(1));
3493 // Copy the value to a (aligned) stack slot using (unaligned) integer
3494 // loads and stores, then do a (aligned) load from the stack slot.
3495 MVT RegVT = getRegisterType(*DAG.getContext(), intVT);
3496 unsigned LoadedBytes = LoadedVT.getStoreSize();
3497 unsigned RegBytes = RegVT.getSizeInBits() / 8;
3498 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
3500 // Make sure the stack slot is also aligned for the register type.
3501 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
3502 auto FrameIndex = cast<FrameIndexSDNode>(StackBase.getNode())->getIndex();
3503 SmallVector<SDValue, 8> Stores;
3504 SDValue StackPtr = StackBase;
3505 unsigned Offset = 0;
3507 EVT PtrVT = Ptr.getValueType();
3508 EVT StackPtrVT = StackPtr.getValueType();
3510 SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT);
3511 SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT);
3513 // Do all but one copies using the full register width.
3514 for (unsigned i = 1; i < NumRegs; i++) {
3515 // Load one integer register's worth from the original location.
3516 SDValue Load = DAG.getLoad(
3517 RegVT, dl, Chain, Ptr, LD->getPointerInfo().getWithOffset(Offset),
3518 MinAlign(LD->getAlignment(), Offset), LD->getMemOperand()->getFlags(),
3520 // Follow the load with a store to the stack slot. Remember the store.
3521 Stores.push_back(DAG.getStore(
3522 Load.getValue(1), dl, Load, StackPtr,
3523 MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset)));
3524 // Increment the pointers.
3527 Ptr = DAG.getObjectPtrOffset(dl, Ptr, PtrIncrement);
3528 StackPtr = DAG.getObjectPtrOffset(dl, StackPtr, StackPtrIncrement);
3531 // The last copy may be partial. Do an extending load.
3532 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
3533 8 * (LoadedBytes - Offset));
3535 DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
3536 LD->getPointerInfo().getWithOffset(Offset), MemVT,
3537 MinAlign(LD->getAlignment(), Offset),
3538 LD->getMemOperand()->getFlags(), LD->getAAInfo());
3539 // Follow the load with a store to the stack slot. Remember the store.
3540 // On big-endian machines this requires a truncating store to ensure
3541 // that the bits end up in the right place.
3542 Stores.push_back(DAG.getTruncStore(
3543 Load.getValue(1), dl, Load, StackPtr,
3544 MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset), MemVT));
3546 // The order of the stores doesn't matter - say it with a TokenFactor.
3547 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
3549 // Finally, perform the original load only redirected to the stack slot.
3550 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
3551 MachinePointerInfo::getFixedStack(MF, FrameIndex, 0),
3554 // Callers expect a MERGE_VALUES node.
3555 return std::make_pair(Load, TF);
3558 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
3559 "Unaligned load of unsupported type.");
3561 // Compute the new VT that is half the size of the old one. This is an
3563 unsigned NumBits = LoadedVT.getSizeInBits();
3565 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
3568 unsigned Alignment = LD->getAlignment();
3569 unsigned IncrementSize = NumBits / 8;
3570 ISD::LoadExtType HiExtType = LD->getExtensionType();
3572 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
3573 if (HiExtType == ISD::NON_EXTLOAD)
3574 HiExtType = ISD::ZEXTLOAD;
3576 // Load the value in two parts
3578 if (DAG.getDataLayout().isLittleEndian()) {
3579 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
3580 NewLoadedVT, Alignment, LD->getMemOperand()->getFlags(),
3583 Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
3584 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
3585 LD->getPointerInfo().getWithOffset(IncrementSize),
3586 NewLoadedVT, MinAlign(Alignment, IncrementSize),
3587 LD->getMemOperand()->getFlags(), LD->getAAInfo());
3589 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
3590 NewLoadedVT, Alignment, LD->getMemOperand()->getFlags(),
3593 Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
3594 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
3595 LD->getPointerInfo().getWithOffset(IncrementSize),
3596 NewLoadedVT, MinAlign(Alignment, IncrementSize),
3597 LD->getMemOperand()->getFlags(), LD->getAAInfo());
3600 // aggregate the two parts
3601 SDValue ShiftAmount =
3602 DAG.getConstant(NumBits, dl, getShiftAmountTy(Hi.getValueType(),
3603 DAG.getDataLayout()));
3604 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
3605 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
3607 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
3610 return std::make_pair(Result, TF);
3613 SDValue TargetLowering::expandUnalignedStore(StoreSDNode *ST,
3614 SelectionDAG &DAG) const {
3615 assert(ST->getAddressingMode() == ISD::UNINDEXED &&
3616 "unaligned indexed stores not implemented!");
3617 SDValue Chain = ST->getChain();
3618 SDValue Ptr = ST->getBasePtr();
3619 SDValue Val = ST->getValue();
3620 EVT VT = Val.getValueType();
3621 int Alignment = ST->getAlignment();
3622 auto &MF = DAG.getMachineFunction();
3625 if (ST->getMemoryVT().isFloatingPoint() ||
3626 ST->getMemoryVT().isVector()) {
3627 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
3628 if (isTypeLegal(intVT)) {
3629 if (!isOperationLegalOrCustom(ISD::STORE, intVT)) {
3630 // Scalarize the store and let the individual components be handled.
3631 SDValue Result = scalarizeVectorStore(ST, DAG);
3635 // Expand to a bitconvert of the value to the integer type of the
3636 // same size, then a (misaligned) int store.
3637 // FIXME: Does not handle truncating floating point stores!
3638 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
3639 Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
3640 Alignment, ST->getMemOperand()->getFlags());
3643 // Do a (aligned) store to a stack slot, then copy from the stack slot
3644 // to the final destination using (unaligned) integer loads and stores.
3645 EVT StoredVT = ST->getMemoryVT();
3647 getRegisterType(*DAG.getContext(),
3648 EVT::getIntegerVT(*DAG.getContext(),
3649 StoredVT.getSizeInBits()));
3650 EVT PtrVT = Ptr.getValueType();
3651 unsigned StoredBytes = StoredVT.getStoreSize();
3652 unsigned RegBytes = RegVT.getSizeInBits() / 8;
3653 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
3655 // Make sure the stack slot is also aligned for the register type.
3656 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
3657 auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
3659 // Perform the original store, only redirected to the stack slot.
3660 SDValue Store = DAG.getTruncStore(
3661 Chain, dl, Val, StackPtr,
3662 MachinePointerInfo::getFixedStack(MF, FrameIndex, 0), StoredVT);
3664 EVT StackPtrVT = StackPtr.getValueType();
3666 SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT);
3667 SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT);
3668 SmallVector<SDValue, 8> Stores;
3669 unsigned Offset = 0;
3671 // Do all but one copies using the full register width.
3672 for (unsigned i = 1; i < NumRegs; i++) {
3673 // Load one integer register's worth from the stack slot.
3674 SDValue Load = DAG.getLoad(
3675 RegVT, dl, Store, StackPtr,
3676 MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset));
3677 // Store it to the final location. Remember the store.
3678 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
3679 ST->getPointerInfo().getWithOffset(Offset),
3680 MinAlign(ST->getAlignment(), Offset),
3681 ST->getMemOperand()->getFlags()));
3682 // Increment the pointers.
3684 StackPtr = DAG.getObjectPtrOffset(dl, StackPtr, StackPtrIncrement);
3685 Ptr = DAG.getObjectPtrOffset(dl, Ptr, PtrIncrement);
3688 // The last store may be partial. Do a truncating store. On big-endian
3689 // machines this requires an extending load from the stack slot to ensure
3690 // that the bits are in the right place.
3691 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
3692 8 * (StoredBytes - Offset));
3694 // Load from the stack slot.
3695 SDValue Load = DAG.getExtLoad(
3696 ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
3697 MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset), MemVT);
3700 DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
3701 ST->getPointerInfo().getWithOffset(Offset), MemVT,
3702 MinAlign(ST->getAlignment(), Offset),
3703 ST->getMemOperand()->getFlags(), ST->getAAInfo()));
3704 // The order of the stores doesn't matter - say it with a TokenFactor.
3705 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
3709 assert(ST->getMemoryVT().isInteger() &&
3710 !ST->getMemoryVT().isVector() &&
3711 "Unaligned store of unknown type.");
3712 // Get the half-size VT
3713 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
3714 int NumBits = NewStoredVT.getSizeInBits();
3715 int IncrementSize = NumBits / 8;
3717 // Divide the stored value in two parts.
3718 SDValue ShiftAmount =
3719 DAG.getConstant(NumBits, dl, getShiftAmountTy(Val.getValueType(),
3720 DAG.getDataLayout()));
3722 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
3724 // Store the two parts
3725 SDValue Store1, Store2;
3726 Store1 = DAG.getTruncStore(Chain, dl,
3727 DAG.getDataLayout().isLittleEndian() ? Lo : Hi,
3728 Ptr, ST->getPointerInfo(), NewStoredVT, Alignment,
3729 ST->getMemOperand()->getFlags());
3731 Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
3732 Alignment = MinAlign(Alignment, IncrementSize);
3733 Store2 = DAG.getTruncStore(
3734 Chain, dl, DAG.getDataLayout().isLittleEndian() ? Hi : Lo, Ptr,
3735 ST->getPointerInfo().getWithOffset(IncrementSize), NewStoredVT, Alignment,
3736 ST->getMemOperand()->getFlags(), ST->getAAInfo());
3739 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
3744 TargetLowering::IncrementMemoryAddress(SDValue Addr, SDValue Mask,
3745 const SDLoc &DL, EVT DataVT,
3747 bool IsCompressedMemory) const {
3749 EVT AddrVT = Addr.getValueType();
3750 EVT MaskVT = Mask.getValueType();
3751 assert(DataVT.getVectorNumElements() == MaskVT.getVectorNumElements() &&
3752 "Incompatible types of Data and Mask");
3753 if (IsCompressedMemory) {
3754 // Incrementing the pointer according to number of '1's in the mask.
3755 EVT MaskIntVT = EVT::getIntegerVT(*DAG.getContext(), MaskVT.getSizeInBits());
3756 SDValue MaskInIntReg = DAG.getBitcast(MaskIntVT, Mask);
3757 if (MaskIntVT.getSizeInBits() < 32) {
3758 MaskInIntReg = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, MaskInIntReg);
3759 MaskIntVT = MVT::i32;
3762 // Count '1's with POPCNT.
3763 Increment = DAG.getNode(ISD::CTPOP, DL, MaskIntVT, MaskInIntReg);
3764 Increment = DAG.getZExtOrTrunc(Increment, DL, AddrVT);
3765 // Scale is an element size in bytes.
3766 SDValue Scale = DAG.getConstant(DataVT.getScalarSizeInBits() / 8, DL,
3768 Increment = DAG.getNode(ISD::MUL, DL, AddrVT, Increment, Scale);
3770 Increment = DAG.getConstant(DataVT.getStoreSize(), DL, AddrVT);
3772 return DAG.getNode(ISD::ADD, DL, AddrVT, Addr, Increment);
3775 static SDValue clampDynamicVectorIndex(SelectionDAG &DAG,
3779 if (isa<ConstantSDNode>(Idx))
3782 EVT IdxVT = Idx.getValueType();
3783 unsigned NElts = VecVT.getVectorNumElements();
3784 if (isPowerOf2_32(NElts)) {
3785 APInt Imm = APInt::getLowBitsSet(IdxVT.getSizeInBits(),
3787 return DAG.getNode(ISD::AND, dl, IdxVT, Idx,
3788 DAG.getConstant(Imm, dl, IdxVT));
3791 return DAG.getNode(ISD::UMIN, dl, IdxVT, Idx,
3792 DAG.getConstant(NElts - 1, dl, IdxVT));
3795 SDValue TargetLowering::getVectorElementPointer(SelectionDAG &DAG,
3796 SDValue VecPtr, EVT VecVT,
3797 SDValue Index) const {
3799 // Make sure the index type is big enough to compute in.
3800 Index = DAG.getZExtOrTrunc(Index, dl, VecPtr.getValueType());
3802 EVT EltVT = VecVT.getVectorElementType();
3804 // Calculate the element offset and add it to the pointer.
3805 unsigned EltSize = EltVT.getSizeInBits() / 8; // FIXME: should be ABI size.
3806 assert(EltSize * 8 == EltVT.getSizeInBits() &&
3807 "Converting bits to bytes lost precision");
3809 Index = clampDynamicVectorIndex(DAG, Index, VecVT, dl);
3811 EVT IdxVT = Index.getValueType();
3813 Index = DAG.getNode(ISD::MUL, dl, IdxVT, Index,
3814 DAG.getConstant(EltSize, dl, IdxVT));
3815 return DAG.getNode(ISD::ADD, dl, IdxVT, VecPtr, Index);
3818 //===----------------------------------------------------------------------===//
3819 // Implementation of Emulated TLS Model
3820 //===----------------------------------------------------------------------===//
3822 SDValue TargetLowering::LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA,
3823 SelectionDAG &DAG) const {
3824 // Access to address of TLS varialbe xyz is lowered to a function call:
3825 // __emutls_get_address( address of global variable named "__emutls_v.xyz" )
3826 EVT PtrVT = getPointerTy(DAG.getDataLayout());
3827 PointerType *VoidPtrType = Type::getInt8PtrTy(*DAG.getContext());
3832 std::string NameString = ("__emutls_v." + GA->getGlobal()->getName()).str();
3833 Module *VariableModule = const_cast<Module*>(GA->getGlobal()->getParent());
3834 StringRef EmuTlsVarName(NameString);
3835 GlobalVariable *EmuTlsVar = VariableModule->getNamedGlobal(EmuTlsVarName);
3836 assert(EmuTlsVar && "Cannot find EmuTlsVar ");
3837 Entry.Node = DAG.getGlobalAddress(EmuTlsVar, dl, PtrVT);
3838 Entry.Ty = VoidPtrType;
3839 Args.push_back(Entry);
3841 SDValue EmuTlsGetAddr = DAG.getExternalSymbol("__emutls_get_address", PtrVT);
3843 TargetLowering::CallLoweringInfo CLI(DAG);
3844 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode());
3845 CLI.setLibCallee(CallingConv::C, VoidPtrType, EmuTlsGetAddr, std::move(Args));
3846 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
3848 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
3849 // At last for X86 targets, maybe good for other targets too?
3850 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
3851 MFI.setAdjustsStack(true); // Is this only for X86 target?
3852 MFI.setHasCalls(true);
3854 assert((GA->getOffset() == 0) &&
3855 "Emulated TLS must have zero offset in GlobalAddressSDNode");
3856 return CallResult.first;
3859 SDValue TargetLowering::lowerCmpEqZeroToCtlzSrl(SDValue Op,
3860 SelectionDAG &DAG) const {
3861 assert((Op->getOpcode() == ISD::SETCC) && "Input has to be a SETCC node.");
3864 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
3866 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
3867 if (C->isNullValue() && CC == ISD::SETEQ) {
3868 EVT VT = Op.getOperand(0).getValueType();
3869 SDValue Zext = Op.getOperand(0);
3870 if (VT.bitsLT(MVT::i32)) {
3872 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
3874 unsigned Log2b = Log2_32(VT.getSizeInBits());
3875 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
3876 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
3877 DAG.getConstant(Log2b, dl, MVT::i32));
3878 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);