LLVM  8.0.1
TargetLowering.cpp
Go to the documentation of this file.
1 //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements the TargetLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/BitVector.h"
16 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/GlobalVariable.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCExpr.h"
32 #include "llvm/Support/KnownBits.h"
36 #include <cctype>
37 using namespace llvm;
38 
39 /// NOTE: The TargetMachine owns TLOF.
41  : TargetLoweringBase(tm) {}
42 
43 const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
44  return nullptr;
45 }
46 
49 }
50 
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.
54  SDValue &Chain) const {
55  const Function &F = DAG.getMachineFunction().getFunction();
56 
57  // Conservatively require the attributes of the call to match those of
58  // the return. Ignore NoAlias and NonNull because they don't affect the
59  // call sequence.
60  AttributeList CallerAttrs = F.getAttributes();
61  if (AttrBuilder(CallerAttrs, AttributeList::ReturnIndex)
62  .removeAttribute(Attribute::NoAlias)
63  .removeAttribute(Attribute::NonNull)
64  .hasAttributes())
65  return false;
66 
67  // It's not safe to eliminate the sign / zero extension of the return value.
70  return false;
71 
72  // Check if the only use is a function return node.
73  return isUsedByReturnOnly(Node, Chain);
74 }
75 
77  const uint32_t *CallerPreservedMask,
78  const SmallVectorImpl<CCValAssign> &ArgLocs,
79  const SmallVectorImpl<SDValue> &OutVals) const {
80  for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
81  const CCValAssign &ArgLoc = ArgLocs[I];
82  if (!ArgLoc.isRegLoc())
83  continue;
84  unsigned Reg = ArgLoc.getLocReg();
85  // Only look at callee saved registers.
86  if (MachineOperand::clobbersPhysReg(CallerPreservedMask, Reg))
87  continue;
88  // Check that we pass the value used for the caller.
89  // (We look for a CopyFromReg reading a virtual register that is used
90  // for the function live-in value of register Reg)
91  SDValue Value = OutVals[I];
92  if (Value->getOpcode() != ISD::CopyFromReg)
93  return false;
94  unsigned ArgReg = cast<RegisterSDNode>(Value->getOperand(1))->getReg();
95  if (MRI.getLiveInPhysReg(ArgReg) != Reg)
96  return false;
97  }
98  return true;
99 }
100 
101 /// Set CallLoweringInfo attribute flags based on a call instruction
102 /// and called function attributes.
104  unsigned ArgIdx) {
105  IsSExt = CS->paramHasAttr(ArgIdx, Attribute::SExt);
106  IsZExt = CS->paramHasAttr(ArgIdx, Attribute::ZExt);
107  IsInReg = CS->paramHasAttr(ArgIdx, Attribute::InReg);
108  IsSRet = CS->paramHasAttr(ArgIdx, Attribute::StructRet);
109  IsNest = CS->paramHasAttr(ArgIdx, Attribute::Nest);
110  IsByVal = CS->paramHasAttr(ArgIdx, Attribute::ByVal);
111  IsInAlloca = CS->paramHasAttr(ArgIdx, Attribute::InAlloca);
112  IsReturned = CS->paramHasAttr(ArgIdx, Attribute::Returned);
113  IsSwiftSelf = CS->paramHasAttr(ArgIdx, Attribute::SwiftSelf);
114  IsSwiftError = CS->paramHasAttr(ArgIdx, Attribute::SwiftError);
115  Alignment = CS->getParamAlignment(ArgIdx);
116 }
117 
118 /// Generate a libcall taking the given operands as arguments and returning a
119 /// result of type RetVT.
120 std::pair<SDValue, SDValue>
122  ArrayRef<SDValue> Ops, bool isSigned,
123  const SDLoc &dl, bool doesNotReturn,
124  bool isReturnValueUsed) const {
126  Args.reserve(Ops.size());
127 
129  for (SDValue Op : Ops) {
130  Entry.Node = Op;
131  Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
132  Entry.IsSExt = shouldSignExtendTypeInLibCall(Op.getValueType(), isSigned);
133  Entry.IsZExt = !shouldSignExtendTypeInLibCall(Op.getValueType(), isSigned);
134  Args.push_back(Entry);
135  }
136 
137  if (LC == RTLIB::UNKNOWN_LIBCALL)
138  report_fatal_error("Unsupported library call operation!");
140  getPointerTy(DAG.getDataLayout()));
141 
142  Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
144  bool signExtend = shouldSignExtendTypeInLibCall(RetVT, isSigned);
145  CLI.setDebugLoc(dl)
146  .setChain(DAG.getEntryNode())
147  .setLibCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
148  .setNoReturn(doesNotReturn)
149  .setDiscardResult(!isReturnValueUsed)
150  .setSExtResult(signExtend)
151  .setZExtResult(!signExtend);
152  return LowerCallTo(CLI);
153 }
154 
155 /// Soften the operands of a comparison. This code is shared among BR_CC,
156 /// SELECT_CC, and SETCC handlers.
158  SDValue &NewLHS, SDValue &NewRHS,
159  ISD::CondCode &CCCode,
160  const SDLoc &dl) const {
161  assert((VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128 || VT == MVT::ppcf128)
162  && "Unsupported setcc type!");
163 
164  // Expand into one or more soft-fp libcall(s).
165  RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
166  bool ShouldInvertCC = false;
167  switch (CCCode) {
168  case ISD::SETEQ:
169  case ISD::SETOEQ:
170  LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
171  (VT == MVT::f64) ? RTLIB::OEQ_F64 :
172  (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128;
173  break;
174  case ISD::SETNE:
175  case ISD::SETUNE:
176  LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 :
177  (VT == MVT::f64) ? RTLIB::UNE_F64 :
178  (VT == MVT::f128) ? RTLIB::UNE_F128 : RTLIB::UNE_PPCF128;
179  break;
180  case ISD::SETGE:
181  case ISD::SETOGE:
182  LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
183  (VT == MVT::f64) ? RTLIB::OGE_F64 :
184  (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128;
185  break;
186  case ISD::SETLT:
187  case ISD::SETOLT:
188  LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
189  (VT == MVT::f64) ? RTLIB::OLT_F64 :
190  (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128;
191  break;
192  case ISD::SETLE:
193  case ISD::SETOLE:
194  LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
195  (VT == MVT::f64) ? RTLIB::OLE_F64 :
196  (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128;
197  break;
198  case ISD::SETGT:
199  case ISD::SETOGT:
200  LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
201  (VT == MVT::f64) ? RTLIB::OGT_F64 :
202  (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128;
203  break;
204  case ISD::SETUO:
205  LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
206  (VT == MVT::f64) ? RTLIB::UO_F64 :
207  (VT == MVT::f128) ? RTLIB::UO_F128 : RTLIB::UO_PPCF128;
208  break;
209  case ISD::SETO:
210  LC1 = (VT == MVT::f32) ? RTLIB::O_F32 :
211  (VT == MVT::f64) ? RTLIB::O_F64 :
212  (VT == MVT::f128) ? RTLIB::O_F128 : RTLIB::O_PPCF128;
213  break;
214  case ISD::SETONE:
215  // SETONE = SETOLT | SETOGT
216  LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
217  (VT == MVT::f64) ? RTLIB::OLT_F64 :
218  (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128;
219  LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
220  (VT == MVT::f64) ? RTLIB::OGT_F64 :
221  (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128;
222  break;
223  case ISD::SETUEQ:
224  LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
225  (VT == MVT::f64) ? RTLIB::UO_F64 :
226  (VT == MVT::f128) ? RTLIB::UO_F128 : RTLIB::UO_PPCF128;
227  LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
228  (VT == MVT::f64) ? RTLIB::OEQ_F64 :
229  (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128;
230  break;
231  default:
232  // Invert CC for unordered comparisons
233  ShouldInvertCC = true;
234  switch (CCCode) {
235  case ISD::SETULT:
236  LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
237  (VT == MVT::f64) ? RTLIB::OGE_F64 :
238  (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128;
239  break;
240  case ISD::SETULE:
241  LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
242  (VT == MVT::f64) ? RTLIB::OGT_F64 :
243  (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128;
244  break;
245  case ISD::SETUGT:
246  LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
247  (VT == MVT::f64) ? RTLIB::OLE_F64 :
248  (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128;
249  break;
250  case ISD::SETUGE:
251  LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
252  (VT == MVT::f64) ? RTLIB::OLT_F64 :
253  (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128;
254  break;
255  default: llvm_unreachable("Do not know how to soften this setcc!");
256  }
257  }
258 
259  // Use the target specific return value for comparions lib calls.
260  EVT RetVT = getCmpLibcallReturnType();
261  SDValue Ops[2] = {NewLHS, NewRHS};
262  NewLHS = makeLibCall(DAG, LC1, RetVT, Ops, false /*sign irrelevant*/,
263  dl).first;
264  NewRHS = DAG.getConstant(0, dl, RetVT);
265 
266  CCCode = getCmpLibcallCC(LC1);
267  if (ShouldInvertCC)
268  CCCode = getSetCCInverse(CCCode, /*isInteger=*/true);
269 
270  if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
271  SDValue Tmp = DAG.getNode(
272  ISD::SETCC, dl,
273  getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), RetVT),
274  NewLHS, NewRHS, DAG.getCondCode(CCCode));
275  NewLHS = makeLibCall(DAG, LC2, RetVT, Ops, false/*sign irrelevant*/,
276  dl).first;
277  NewLHS = DAG.getNode(
278  ISD::SETCC, dl,
279  getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), RetVT),
280  NewLHS, NewRHS, DAG.getCondCode(getCmpLibcallCC(LC2)));
281  NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS);
282  NewRHS = SDValue();
283  }
284 }
285 
286 /// Return the entry encoding for a jump table in the current function. The
287 /// returned value is a member of the MachineJumpTableInfo::JTEntryKind enum.
289  // In non-pic modes, just use the address of a block.
290  if (!isPositionIndependent())
292 
293  // In PIC mode, if the target supports a GPRel32 directive, use it.
294  if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != nullptr)
296 
297  // Otherwise, use a label difference.
299 }
300 
302  SelectionDAG &DAG) const {
303  // If our PIC model is GP relative, use the global offset table as the base.
304  unsigned JTEncoding = getJumpTableEncoding();
305 
306  if ((JTEncoding == MachineJumpTableInfo::EK_GPRel64BlockAddress) ||
309 
310  return Table;
311 }
312 
313 /// This returns the relocation base for the given PIC jumptable, the same as
314 /// getPICJumpTableRelocBase, but as an MCExpr.
315 const MCExpr *
317  unsigned JTI,MCContext &Ctx) const{
318  // The normal PIC reloc base is the label at the start of the jump table.
319  return MCSymbolRefExpr::create(MF->getJTISymbol(JTI, Ctx), Ctx);
320 }
321 
322 bool
324  const TargetMachine &TM = getTargetMachine();
325  const GlobalValue *GV = GA->getGlobal();
326 
327  // If the address is not even local to this DSO we will have to load it from
328  // a got and then add the offset.
329  if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
330  return false;
331 
332  // If the code is position independent we will have to add a base register.
333  if (isPositionIndependent())
334  return false;
335 
336  // Otherwise we can do it.
337  return true;
338 }
339 
340 //===----------------------------------------------------------------------===//
341 // Optimization Methods
342 //===----------------------------------------------------------------------===//
343 
344 /// If the specified instruction has a constant integer operand and there are
345 /// bits set in that constant that are not demanded, then clear those bits and
346 /// return true.
348  TargetLoweringOpt &TLO) const {
349  SelectionDAG &DAG = TLO.DAG;
350  SDLoc DL(Op);
351  unsigned Opcode = Op.getOpcode();
352 
353  // Do target-specific constant optimization.
354  if (targetShrinkDemandedConstant(Op, Demanded, TLO))
355  return TLO.New.getNode();
356 
357  // FIXME: ISD::SELECT, ISD::SELECT_CC
358  switch (Opcode) {
359  default:
360  break;
361  case ISD::XOR:
362  case ISD::AND:
363  case ISD::OR: {
364  auto *Op1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
365  if (!Op1C)
366  return false;
367 
368  // If this is a 'not' op, don't touch it because that's a canonical form.
369  const APInt &C = Op1C->getAPIntValue();
370  if (Opcode == ISD::XOR && Demanded.isSubsetOf(C))
371  return false;
372 
373  if (!C.isSubsetOf(Demanded)) {
374  EVT VT = Op.getValueType();
375  SDValue NewC = DAG.getConstant(Demanded & C, DL, VT);
376  SDValue NewOp = DAG.getNode(Opcode, DL, VT, Op.getOperand(0), NewC);
377  return TLO.CombineTo(Op, NewOp);
378  }
379 
380  break;
381  }
382  }
383 
384  return false;
385 }
386 
387 /// Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the casts are free.
388 /// This uses isZExtFree and ZERO_EXTEND for the widening cast, but it could be
389 /// generalized for targets with other types of implicit widening casts.
391  const APInt &Demanded,
392  TargetLoweringOpt &TLO) const {
393  assert(Op.getNumOperands() == 2 &&
394  "ShrinkDemandedOp only supports binary operators!");
395  assert(Op.getNode()->getNumValues() == 1 &&
396  "ShrinkDemandedOp only supports nodes with one result!");
397 
398  SelectionDAG &DAG = TLO.DAG;
399  SDLoc dl(Op);
400 
401  // Early return, as this function cannot handle vector types.
402  if (Op.getValueType().isVector())
403  return false;
404 
405  // Don't do this if the node has another user, which may require the
406  // full value.
407  if (!Op.getNode()->hasOneUse())
408  return false;
409 
410  // Search for the smallest integer type with free casts to and from
411  // Op's type. For expedience, just check power-of-2 integer types.
412  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
413  unsigned DemandedSize = Demanded.getActiveBits();
414  unsigned SmallVTBits = DemandedSize;
415  if (!isPowerOf2_32(SmallVTBits))
416  SmallVTBits = NextPowerOf2(SmallVTBits);
417  for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
418  EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
419  if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
420  TLI.isZExtFree(SmallVT, Op.getValueType())) {
421  // We found a type with free casts.
422  SDValue X = DAG.getNode(
423  Op.getOpcode(), dl, SmallVT,
424  DAG.getNode(ISD::TRUNCATE, dl, SmallVT, Op.getOperand(0)),
425  DAG.getNode(ISD::TRUNCATE, dl, SmallVT, Op.getOperand(1)));
426  assert(DemandedSize <= SmallVTBits && "Narrowed below demanded bits?");
427  SDValue Z = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), X);
428  return TLO.CombineTo(Op, Z);
429  }
430  }
431  return false;
432 }
433 
435  DAGCombinerInfo &DCI) const {
436  SelectionDAG &DAG = DCI.DAG;
437  TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
438  !DCI.isBeforeLegalizeOps());
439  KnownBits Known;
440 
441  bool Simplified = SimplifyDemandedBits(Op, DemandedBits, Known, TLO);
442  if (Simplified) {
443  DCI.AddToWorklist(Op.getNode());
444  DCI.CommitTargetLoweringOpt(TLO);
445  }
446  return Simplified;
447 }
448 
450  KnownBits &Known,
451  TargetLoweringOpt &TLO,
452  unsigned Depth,
453  bool AssumeSingleUse) const {
454  EVT VT = Op.getValueType();
455  APInt DemandedElts = VT.isVector()
457  : APInt(1, 1);
458  return SimplifyDemandedBits(Op, DemandedBits, DemandedElts, Known, TLO, Depth,
459  AssumeSingleUse);
460 }
461 
462 /// Look at Op. At this point, we know that only the OriginalDemandedBits of the
463 /// result of Op are ever used downstream. If we can use this information to
464 /// simplify Op, create a new simplified DAG node and return true, returning the
465 /// original and new nodes in Old and New. Otherwise, analyze the expression and
466 /// return a mask of Known bits for the expression (used to simplify the
467 /// caller). The Known bits may only be accurate for those bits in the
468 /// OriginalDemandedBits and OriginalDemandedElts.
470  SDValue Op, const APInt &OriginalDemandedBits,
471  const APInt &OriginalDemandedElts, KnownBits &Known, TargetLoweringOpt &TLO,
472  unsigned Depth, bool AssumeSingleUse) const {
473  unsigned BitWidth = OriginalDemandedBits.getBitWidth();
474  assert(Op.getScalarValueSizeInBits() == BitWidth &&
475  "Mask size mismatches value type size!");
476 
477  unsigned NumElts = OriginalDemandedElts.getBitWidth();
478  assert((!Op.getValueType().isVector() ||
479  NumElts == Op.getValueType().getVectorNumElements()) &&
480  "Unexpected vector size");
481 
482  APInt DemandedBits = OriginalDemandedBits;
483  APInt DemandedElts = OriginalDemandedElts;
484  SDLoc dl(Op);
485  auto &DL = TLO.DAG.getDataLayout();
486 
487  // Don't know anything.
488  Known = KnownBits(BitWidth);
489 
490  if (Op.getOpcode() == ISD::Constant) {
491  // We know all of the bits for a constant!
492  Known.One = cast<ConstantSDNode>(Op)->getAPIntValue();
493  Known.Zero = ~Known.One;
494  return false;
495  }
496 
497  // Other users may use these bits.
498  EVT VT = Op.getValueType();
499  if (!Op.getNode()->hasOneUse() && !AssumeSingleUse) {
500  if (Depth != 0) {
501  // If not at the root, Just compute the Known bits to
502  // simplify things downstream.
503  Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth);
504  return false;
505  }
506  // If this is the root being simplified, allow it to have multiple uses,
507  // just set the DemandedBits/Elts to all bits.
508  DemandedBits = APInt::getAllOnesValue(BitWidth);
509  DemandedElts = APInt::getAllOnesValue(NumElts);
510  } else if (OriginalDemandedBits == 0 || OriginalDemandedElts == 0) {
511  // Not demanding any bits/elts from Op.
512  if (!Op.isUndef())
513  return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT));
514  return false;
515  } else if (Depth == 6) { // Limit search depth.
516  return false;
517  }
518 
519  KnownBits Known2, KnownOut;
520  switch (Op.getOpcode()) {
521  case ISD::BUILD_VECTOR:
522  // Collect the known bits that are shared by every constant vector element.
523  Known.Zero.setAllBits(); Known.One.setAllBits();
524  for (SDValue SrcOp : Op->ops()) {
525  if (!isa<ConstantSDNode>(SrcOp)) {
526  // We can only handle all constant values - bail out with no known bits.
527  Known = KnownBits(BitWidth);
528  return false;
529  }
530  Known2.One = cast<ConstantSDNode>(SrcOp)->getAPIntValue();
531  Known2.Zero = ~Known2.One;
532 
533  // BUILD_VECTOR can implicitly truncate sources, we must handle this.
534  if (Known2.One.getBitWidth() != BitWidth) {
535  assert(Known2.getBitWidth() > BitWidth &&
536  "Expected BUILD_VECTOR implicit truncation");
537  Known2 = Known2.trunc(BitWidth);
538  }
539 
540  // Known bits are the values that are shared by every element.
541  // TODO: support per-element known bits.
542  Known.One &= Known2.One;
543  Known.Zero &= Known2.Zero;
544  }
545  return false; // Don't fall through, will infinitely loop.
546  case ISD::CONCAT_VECTORS: {
547  Known.Zero.setAllBits();
548  Known.One.setAllBits();
549  EVT SubVT = Op.getOperand(0).getValueType();
550  unsigned NumSubVecs = Op.getNumOperands();
551  unsigned NumSubElts = SubVT.getVectorNumElements();
552  for (unsigned i = 0; i != NumSubVecs; ++i) {
553  APInt DemandedSubElts =
554  DemandedElts.extractBits(NumSubElts, i * NumSubElts);
555  if (SimplifyDemandedBits(Op.getOperand(i), DemandedBits, DemandedSubElts,
556  Known2, TLO, Depth + 1))
557  return true;
558  // Known bits are shared by every demanded subvector element.
559  if (!!DemandedSubElts) {
560  Known.One &= Known2.One;
561  Known.Zero &= Known2.Zero;
562  }
563  }
564  break;
565  }
566  case ISD::VECTOR_SHUFFLE: {
567  ArrayRef<int> ShuffleMask = cast<ShuffleVectorSDNode>(Op)->getMask();
568 
569  // Collect demanded elements from shuffle operands..
570  APInt DemandedLHS(NumElts, 0);
571  APInt DemandedRHS(NumElts, 0);
572  for (unsigned i = 0; i != NumElts; ++i) {
573  if (!DemandedElts[i])
574  continue;
575  int M = ShuffleMask[i];
576  if (M < 0) {
577  // For UNDEF elements, we don't know anything about the common state of
578  // the shuffle result.
579  DemandedLHS.clearAllBits();
580  DemandedRHS.clearAllBits();
581  break;
582  }
583  assert(0 <= M && M < (int)(2 * NumElts) && "Shuffle index out of range");
584  if (M < (int)NumElts)
585  DemandedLHS.setBit(M);
586  else
587  DemandedRHS.setBit(M - NumElts);
588  }
589 
590  if (!!DemandedLHS || !!DemandedRHS) {
591  Known.Zero.setAllBits();
592  Known.One.setAllBits();
593  if (!!DemandedLHS) {
594  if (SimplifyDemandedBits(Op.getOperand(0), DemandedBits, DemandedLHS,
595  Known2, TLO, Depth + 1))
596  return true;
597  Known.One &= Known2.One;
598  Known.Zero &= Known2.Zero;
599  }
600  if (!!DemandedRHS) {
601  if (SimplifyDemandedBits(Op.getOperand(1), DemandedBits, DemandedRHS,
602  Known2, TLO, Depth + 1))
603  return true;
604  Known.One &= Known2.One;
605  Known.Zero &= Known2.Zero;
606  }
607  }
608  break;
609  }
610  case ISD::AND: {
611  SDValue Op0 = Op.getOperand(0);
612  SDValue Op1 = Op.getOperand(1);
613 
614  // If the RHS is a constant, check to see if the LHS would be zero without
615  // using the bits from the RHS. Below, we use knowledge about the RHS to
616  // simplify the LHS, here we're using information from the LHS to simplify
617  // the RHS.
618  if (ConstantSDNode *RHSC = isConstOrConstSplat(Op1)) {
619  // Do not increment Depth here; that can cause an infinite loop.
620  KnownBits LHSKnown = TLO.DAG.computeKnownBits(Op0, DemandedElts, Depth);
621  // If the LHS already has zeros where RHSC does, this 'and' is dead.
622  if ((LHSKnown.Zero & DemandedBits) ==
623  (~RHSC->getAPIntValue() & DemandedBits))
624  return TLO.CombineTo(Op, Op0);
625 
626  // If any of the set bits in the RHS are known zero on the LHS, shrink
627  // the constant.
628  if (ShrinkDemandedConstant(Op, ~LHSKnown.Zero & DemandedBits, TLO))
629  return true;
630 
631  // Bitwise-not (xor X, -1) is a special case: we don't usually shrink its
632  // constant, but if this 'and' is only clearing bits that were just set by
633  // the xor, then this 'and' can be eliminated by shrinking the mask of
634  // the xor. For example, for a 32-bit X:
635  // and (xor (srl X, 31), -1), 1 --> xor (srl X, 31), 1
636  if (isBitwiseNot(Op0) && Op0.hasOneUse() &&
637  LHSKnown.One == ~RHSC->getAPIntValue()) {
638  SDValue Xor = TLO.DAG.getNode(ISD::XOR, dl, VT, Op0.getOperand(0), Op1);
639  return TLO.CombineTo(Op, Xor);
640  }
641  }
642 
643  if (SimplifyDemandedBits(Op1, DemandedBits, DemandedElts, Known, TLO, Depth + 1))
644  return true;
645  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
646  if (SimplifyDemandedBits(Op0, ~Known.Zero & DemandedBits, DemandedElts, Known2, TLO,
647  Depth + 1))
648  return true;
649  assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
650 
651  // If all of the demanded bits are known one on one side, return the other.
652  // These bits cannot contribute to the result of the 'and'.
653  if (DemandedBits.isSubsetOf(Known2.Zero | Known.One))
654  return TLO.CombineTo(Op, Op0);
655  if (DemandedBits.isSubsetOf(Known.Zero | Known2.One))
656  return TLO.CombineTo(Op, Op1);
657  // If all of the demanded bits in the inputs are known zeros, return zero.
658  if (DemandedBits.isSubsetOf(Known.Zero | Known2.Zero))
659  return TLO.CombineTo(Op, TLO.DAG.getConstant(0, dl, VT));
660  // If the RHS is a constant, see if we can simplify it.
661  if (ShrinkDemandedConstant(Op, ~Known2.Zero & DemandedBits, TLO))
662  return true;
663  // If the operation can be done in a smaller type, do so.
664  if (ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO))
665  return true;
666 
667  // Output known-1 bits are only known if set in both the LHS & RHS.
668  Known.One &= Known2.One;
669  // Output known-0 are known to be clear if zero in either the LHS | RHS.
670  Known.Zero |= Known2.Zero;
671  break;
672  }
673  case ISD::OR: {
674  SDValue Op0 = Op.getOperand(0);
675  SDValue Op1 = Op.getOperand(1);
676 
677  if (SimplifyDemandedBits(Op1, DemandedBits, DemandedElts, Known, TLO, Depth + 1))
678  return true;
679  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
680  if (SimplifyDemandedBits(Op0, ~Known.One & DemandedBits, DemandedElts, Known2, TLO,
681  Depth + 1))
682  return true;
683  assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
684 
685  // If all of the demanded bits are known zero on one side, return the other.
686  // These bits cannot contribute to the result of the 'or'.
687  if (DemandedBits.isSubsetOf(Known2.One | Known.Zero))
688  return TLO.CombineTo(Op, Op0);
689  if (DemandedBits.isSubsetOf(Known.One | Known2.Zero))
690  return TLO.CombineTo(Op, Op1);
691  // If the RHS is a constant, see if we can simplify it.
692  if (ShrinkDemandedConstant(Op, DemandedBits, TLO))
693  return true;
694  // If the operation can be done in a smaller type, do so.
695  if (ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO))
696  return true;
697 
698  // Output known-0 bits are only known if clear in both the LHS & RHS.
699  Known.Zero &= Known2.Zero;
700  // Output known-1 are known to be set if set in either the LHS | RHS.
701  Known.One |= Known2.One;
702  break;
703  }
704  case ISD::XOR: {
705  SDValue Op0 = Op.getOperand(0);
706  SDValue Op1 = Op.getOperand(1);
707 
708  if (SimplifyDemandedBits(Op1, DemandedBits, DemandedElts, Known, TLO, Depth + 1))
709  return true;
710  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
711  if (SimplifyDemandedBits(Op0, DemandedBits, DemandedElts, Known2, TLO, Depth + 1))
712  return true;
713  assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
714 
715  // If all of the demanded bits are known zero on one side, return the other.
716  // These bits cannot contribute to the result of the 'xor'.
717  if (DemandedBits.isSubsetOf(Known.Zero))
718  return TLO.CombineTo(Op, Op0);
719  if (DemandedBits.isSubsetOf(Known2.Zero))
720  return TLO.CombineTo(Op, Op1);
721  // If the operation can be done in a smaller type, do so.
722  if (ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO))
723  return true;
724 
725  // If all of the unknown bits are known to be zero on one side or the other
726  // (but not both) turn this into an *inclusive* or.
727  // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
728  if (DemandedBits.isSubsetOf(Known.Zero | Known2.Zero))
729  return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, VT, Op0, Op1));
730 
731  // Output known-0 bits are known if clear or set in both the LHS & RHS.
732  KnownOut.Zero = (Known.Zero & Known2.Zero) | (Known.One & Known2.One);
733  // Output known-1 are known to be set if set in only one of the LHS, RHS.
734  KnownOut.One = (Known.Zero & Known2.One) | (Known.One & Known2.Zero);
735 
736  if (ConstantSDNode *C = isConstOrConstSplat(Op1)) {
737  // If one side is a constant, and all of the known set bits on the other
738  // side are also set in the constant, turn this into an AND, as we know
739  // the bits will be cleared.
740  // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
741  // NB: it is okay if more bits are known than are requested
742  if (C->getAPIntValue() == Known2.One) {
743  SDValue ANDC =
744  TLO.DAG.getConstant(~C->getAPIntValue() & DemandedBits, dl, VT);
745  return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT, Op0, ANDC));
746  }
747 
748  // If the RHS is a constant, see if we can change it. Don't alter a -1
749  // constant because that's a 'not' op, and that is better for combining
750  // and codegen.
751  if (!C->isAllOnesValue()) {
752  if (DemandedBits.isSubsetOf(C->getAPIntValue())) {
753  // We're flipping all demanded bits. Flip the undemanded bits too.
754  SDValue New = TLO.DAG.getNOT(dl, Op0, VT);
755  return TLO.CombineTo(Op, New);
756  }
757  // If we can't turn this into a 'not', try to shrink the constant.
758  if (ShrinkDemandedConstant(Op, DemandedBits, TLO))
759  return true;
760  }
761  }
762 
763  Known = std::move(KnownOut);
764  break;
765  }
766  case ISD::SELECT:
767  if (SimplifyDemandedBits(Op.getOperand(2), DemandedBits, Known, TLO,
768  Depth + 1))
769  return true;
770  if (SimplifyDemandedBits(Op.getOperand(1), DemandedBits, Known2, TLO,
771  Depth + 1))
772  return true;
773  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
774  assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
775 
776  // If the operands are constants, see if we can simplify them.
777  if (ShrinkDemandedConstant(Op, DemandedBits, TLO))
778  return true;
779 
780  // Only known if known in both the LHS and RHS.
781  Known.One &= Known2.One;
782  Known.Zero &= Known2.Zero;
783  break;
784  case ISD::SELECT_CC:
785  if (SimplifyDemandedBits(Op.getOperand(3), DemandedBits, Known, TLO,
786  Depth + 1))
787  return true;
788  if (SimplifyDemandedBits(Op.getOperand(2), DemandedBits, Known2, TLO,
789  Depth + 1))
790  return true;
791  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
792  assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
793 
794  // If the operands are constants, see if we can simplify them.
795  if (ShrinkDemandedConstant(Op, DemandedBits, TLO))
796  return true;
797 
798  // Only known if known in both the LHS and RHS.
799  Known.One &= Known2.One;
800  Known.Zero &= Known2.Zero;
801  break;
802  case ISD::SETCC: {
803  SDValue Op0 = Op.getOperand(0);
804  SDValue Op1 = Op.getOperand(1);
805  ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
806  // If (1) we only need the sign-bit, (2) the setcc operands are the same
807  // width as the setcc result, and (3) the result of a setcc conforms to 0 or
808  // -1, we may be able to bypass the setcc.
809  if (DemandedBits.isSignMask() &&
810  Op0.getScalarValueSizeInBits() == BitWidth &&
811  getBooleanContents(VT) ==
812  BooleanContent::ZeroOrNegativeOneBooleanContent) {
813  // If we're testing X < 0, then this compare isn't needed - just use X!
814  // FIXME: We're limiting to integer types here, but this should also work
815  // if we don't care about FP signed-zero. The use of SETLT with FP means
816  // that we don't care about NaNs.
817  if (CC == ISD::SETLT && Op1.getValueType().isInteger() &&
819  return TLO.CombineTo(Op, Op0);
820 
821  // TODO: Should we check for other forms of sign-bit comparisons?
822  // Examples: X <= -1, X >= 0
823  }
824  if (getBooleanContents(Op0.getValueType()) ==
826  BitWidth > 1)
827  Known.Zero.setBitsFrom(1);
828  break;
829  }
830  case ISD::SHL: {
831  SDValue Op0 = Op.getOperand(0);
832  SDValue Op1 = Op.getOperand(1);
833 
834  if (ConstantSDNode *SA = isConstOrConstSplat(Op1)) {
835  // If the shift count is an invalid immediate, don't do anything.
836  if (SA->getAPIntValue().uge(BitWidth))
837  break;
838 
839  unsigned ShAmt = SA->getZExtValue();
840 
841  // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
842  // single shift. We can do this if the bottom bits (which are shifted
843  // out) are never demanded.
844  if (Op0.getOpcode() == ISD::SRL) {
845  if (ShAmt &&
846  (DemandedBits & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
847  if (ConstantSDNode *SA2 = isConstOrConstSplat(Op0.getOperand(1))) {
848  if (SA2->getAPIntValue().ult(BitWidth)) {
849  unsigned C1 = SA2->getZExtValue();
850  unsigned Opc = ISD::SHL;
851  int Diff = ShAmt - C1;
852  if (Diff < 0) {
853  Diff = -Diff;
854  Opc = ISD::SRL;
855  }
856 
857  SDValue NewSA = TLO.DAG.getConstant(Diff, dl, Op1.getValueType());
858  return TLO.CombineTo(
859  Op, TLO.DAG.getNode(Opc, dl, VT, Op0.getOperand(0), NewSA));
860  }
861  }
862  }
863  }
864 
865  if (SimplifyDemandedBits(Op0, DemandedBits.lshr(ShAmt), DemandedElts, Known, TLO,
866  Depth + 1))
867  return true;
868 
869  // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits
870  // are not demanded. This will likely allow the anyext to be folded away.
871  if (Op0.getOpcode() == ISD::ANY_EXTEND) {
872  SDValue InnerOp = Op0.getOperand(0);
873  EVT InnerVT = InnerOp.getValueType();
874  unsigned InnerBits = InnerVT.getScalarSizeInBits();
875  if (ShAmt < InnerBits && DemandedBits.getActiveBits() <= InnerBits &&
876  isTypeDesirableForOp(ISD::SHL, InnerVT)) {
877  EVT ShTy = getShiftAmountTy(InnerVT, DL);
878  if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits()))
879  ShTy = InnerVT;
880  SDValue NarrowShl =
881  TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp,
882  TLO.DAG.getConstant(ShAmt, dl, ShTy));
883  return TLO.CombineTo(
884  Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT, NarrowShl));
885  }
886  // Repeat the SHL optimization above in cases where an extension
887  // intervenes: (shl (anyext (shr x, c1)), c2) to
888  // (shl (anyext x), c2-c1). This requires that the bottom c1 bits
889  // aren't demanded (as above) and that the shifted upper c1 bits of
890  // x aren't demanded.
891  if (Op0.hasOneUse() && InnerOp.getOpcode() == ISD::SRL &&
892  InnerOp.hasOneUse()) {
893  if (ConstantSDNode *SA2 =
894  isConstOrConstSplat(InnerOp.getOperand(1))) {
895  unsigned InnerShAmt = SA2->getLimitedValue(InnerBits);
896  if (InnerShAmt < ShAmt && InnerShAmt < InnerBits &&
897  DemandedBits.getActiveBits() <=
898  (InnerBits - InnerShAmt + ShAmt) &&
899  DemandedBits.countTrailingZeros() >= ShAmt) {
900  SDValue NewSA = TLO.DAG.getConstant(ShAmt - InnerShAmt, dl,
901  Op1.getValueType());
902  SDValue NewExt = TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT,
903  InnerOp.getOperand(0));
904  return TLO.CombineTo(
905  Op, TLO.DAG.getNode(ISD::SHL, dl, VT, NewExt, NewSA));
906  }
907  }
908  }
909  }
910 
911  Known.Zero <<= ShAmt;
912  Known.One <<= ShAmt;
913  // low bits known zero.
914  Known.Zero.setLowBits(ShAmt);
915  }
916  break;
917  }
918  case ISD::SRL: {
919  SDValue Op0 = Op.getOperand(0);
920  SDValue Op1 = Op.getOperand(1);
921 
922  if (ConstantSDNode *SA = isConstOrConstSplat(Op1)) {
923  // If the shift count is an invalid immediate, don't do anything.
924  if (SA->getAPIntValue().uge(BitWidth))
925  break;
926 
927  unsigned ShAmt = SA->getZExtValue();
928  APInt InDemandedMask = (DemandedBits << ShAmt);
929 
930  // If the shift is exact, then it does demand the low bits (and knows that
931  // they are zero).
932  if (Op->getFlags().hasExact())
933  InDemandedMask.setLowBits(ShAmt);
934 
935  // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
936  // single shift. We can do this if the top bits (which are shifted out)
937  // are never demanded.
938  if (Op0.getOpcode() == ISD::SHL) {
939  if (ConstantSDNode *SA2 = isConstOrConstSplat(Op0.getOperand(1))) {
940  if (ShAmt &&
941  (DemandedBits & APInt::getHighBitsSet(BitWidth, ShAmt)) == 0) {
942  if (SA2->getAPIntValue().ult(BitWidth)) {
943  unsigned C1 = SA2->getZExtValue();
944  unsigned Opc = ISD::SRL;
945  int Diff = ShAmt - C1;
946  if (Diff < 0) {
947  Diff = -Diff;
948  Opc = ISD::SHL;
949  }
950 
951  SDValue NewSA = TLO.DAG.getConstant(Diff, dl, Op1.getValueType());
952  return TLO.CombineTo(
953  Op, TLO.DAG.getNode(Opc, dl, VT, Op0.getOperand(0), NewSA));
954  }
955  }
956  }
957  }
958 
959  // Compute the new bits that are at the top now.
960  if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO, Depth + 1))
961  return true;
962  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
963  Known.Zero.lshrInPlace(ShAmt);
964  Known.One.lshrInPlace(ShAmt);
965 
966  Known.Zero.setHighBits(ShAmt); // High bits known zero.
967  }
968  break;
969  }
970  case ISD::SRA: {
971  SDValue Op0 = Op.getOperand(0);
972  SDValue Op1 = Op.getOperand(1);
973 
974  // If this is an arithmetic shift right and only the low-bit is set, we can
975  // always convert this into a logical shr, even if the shift amount is
976  // variable. The low bit of the shift cannot be an input sign bit unless
977  // the shift amount is >= the size of the datatype, which is undefined.
978  if (DemandedBits.isOneValue())
979  return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT, Op0, Op1));
980 
981  if (ConstantSDNode *SA = isConstOrConstSplat(Op1)) {
982  // If the shift count is an invalid immediate, don't do anything.
983  if (SA->getAPIntValue().uge(BitWidth))
984  break;
985 
986  unsigned ShAmt = SA->getZExtValue();
987  APInt InDemandedMask = (DemandedBits << ShAmt);
988 
989  // If the shift is exact, then it does demand the low bits (and knows that
990  // they are zero).
991  if (Op->getFlags().hasExact())
992  InDemandedMask.setLowBits(ShAmt);
993 
994  // If any of the demanded bits are produced by the sign extension, we also
995  // demand the input sign bit.
996  if (DemandedBits.countLeadingZeros() < ShAmt)
997  InDemandedMask.setSignBit();
998 
999  if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO, Depth + 1))
1000  return true;
1001  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1002  Known.Zero.lshrInPlace(ShAmt);
1003  Known.One.lshrInPlace(ShAmt);
1004 
1005  // If the input sign bit is known to be zero, or if none of the top bits
1006  // are demanded, turn this into an unsigned shift right.
1007  if (Known.Zero[BitWidth - ShAmt - 1] ||
1008  DemandedBits.countLeadingZeros() >= ShAmt) {
1009  SDNodeFlags Flags;
1010  Flags.setExact(Op->getFlags().hasExact());
1011  return TLO.CombineTo(
1012  Op, TLO.DAG.getNode(ISD::SRL, dl, VT, Op0, Op1, Flags));
1013  }
1014 
1015  int Log2 = DemandedBits.exactLogBase2();
1016  if (Log2 >= 0) {
1017  // The bit must come from the sign.
1018  SDValue NewSA =
1019  TLO.DAG.getConstant(BitWidth - 1 - Log2, dl, Op1.getValueType());
1020  return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT, Op0, NewSA));
1021  }
1022 
1023  if (Known.One[BitWidth - ShAmt - 1])
1024  // New bits are known one.
1025  Known.One.setHighBits(ShAmt);
1026  }
1027  break;
1028  }
1029  case ISD::SIGN_EXTEND_INREG: {
1030  SDValue Op0 = Op.getOperand(0);
1031  EVT ExVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1032  unsigned ExVTBits = ExVT.getScalarSizeInBits();
1033 
1034  // If we only care about the highest bit, don't bother shifting right.
1035  if (DemandedBits.isSignMask()) {
1036  bool AlreadySignExtended =
1037  TLO.DAG.ComputeNumSignBits(Op0) >= BitWidth - ExVTBits + 1;
1038  // However if the input is already sign extended we expect the sign
1039  // extension to be dropped altogether later and do not simplify.
1040  if (!AlreadySignExtended) {
1041  // Compute the correct shift amount type, which must be getShiftAmountTy
1042  // for scalar types after legalization.
1043  EVT ShiftAmtTy = VT;
1044  if (TLO.LegalTypes() && !ShiftAmtTy.isVector())
1045  ShiftAmtTy = getShiftAmountTy(ShiftAmtTy, DL);
1046 
1047  SDValue ShiftAmt =
1048  TLO.DAG.getConstant(BitWidth - ExVTBits, dl, ShiftAmtTy);
1049  return TLO.CombineTo(Op,
1050  TLO.DAG.getNode(ISD::SHL, dl, VT, Op0, ShiftAmt));
1051  }
1052  }
1053 
1054  // If none of the extended bits are demanded, eliminate the sextinreg.
1055  if (DemandedBits.getActiveBits() <= ExVTBits)
1056  return TLO.CombineTo(Op, Op0);
1057 
1058  APInt InputDemandedBits = DemandedBits.getLoBits(ExVTBits);
1059 
1060  // Since the sign extended bits are demanded, we know that the sign
1061  // bit is demanded.
1062  InputDemandedBits.setBit(ExVTBits - 1);
1063 
1064  if (SimplifyDemandedBits(Op0, InputDemandedBits, Known, TLO, Depth + 1))
1065  return true;
1066  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1067 
1068  // If the sign bit of the input is known set or clear, then we know the
1069  // top bits of the result.
1070 
1071  // If the input sign bit is known zero, convert this into a zero extension.
1072  if (Known.Zero[ExVTBits - 1])
1073  return TLO.CombineTo(
1074  Op, TLO.DAG.getZeroExtendInReg(Op0, dl, ExVT.getScalarType()));
1075 
1076  APInt Mask = APInt::getLowBitsSet(BitWidth, ExVTBits);
1077  if (Known.One[ExVTBits - 1]) { // Input sign bit known set
1078  Known.One.setBitsFrom(ExVTBits);
1079  Known.Zero &= Mask;
1080  } else { // Input sign bit unknown
1081  Known.Zero &= Mask;
1082  Known.One &= Mask;
1083  }
1084  break;
1085  }
1086  case ISD::BUILD_PAIR: {
1087  EVT HalfVT = Op.getOperand(0).getValueType();
1088  unsigned HalfBitWidth = HalfVT.getScalarSizeInBits();
1089 
1090  APInt MaskLo = DemandedBits.getLoBits(HalfBitWidth).trunc(HalfBitWidth);
1091  APInt MaskHi = DemandedBits.getHiBits(HalfBitWidth).trunc(HalfBitWidth);
1092 
1093  KnownBits KnownLo, KnownHi;
1094 
1095  if (SimplifyDemandedBits(Op.getOperand(0), MaskLo, KnownLo, TLO, Depth + 1))
1096  return true;
1097 
1098  if (SimplifyDemandedBits(Op.getOperand(1), MaskHi, KnownHi, TLO, Depth + 1))
1099  return true;
1100 
1101  Known.Zero = KnownLo.Zero.zext(BitWidth) |
1102  KnownHi.Zero.zext(BitWidth).shl(HalfBitWidth);
1103 
1104  Known.One = KnownLo.One.zext(BitWidth) |
1105  KnownHi.One.zext(BitWidth).shl(HalfBitWidth);
1106  break;
1107  }
1108  case ISD::ZERO_EXTEND: {
1109  SDValue Src = Op.getOperand(0);
1110  unsigned InBits = Src.getScalarValueSizeInBits();
1111 
1112  // If none of the top bits are demanded, convert this into an any_extend.
1113  if (DemandedBits.getActiveBits() <= InBits)
1114  return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT, Src));
1115 
1116  APInt InDemandedBits = DemandedBits.trunc(InBits);
1117  if (SimplifyDemandedBits(Src, InDemandedBits, Known, TLO, Depth+1))
1118  return true;
1119  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1120  Known = Known.zext(BitWidth);
1121  Known.Zero.setBitsFrom(InBits);
1122  break;
1123  }
1124  case ISD::SIGN_EXTEND: {
1125  SDValue Src = Op.getOperand(0);
1126  unsigned InBits = Src.getScalarValueSizeInBits();
1127 
1128  // If none of the top bits are demanded, convert this into an any_extend.
1129  if (DemandedBits.getActiveBits() <= InBits)
1130  return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT, Src));
1131 
1132  // Since some of the sign extended bits are demanded, we know that the sign
1133  // bit is demanded.
1134  APInt InDemandedBits = DemandedBits.trunc(InBits);
1135  InDemandedBits.setBit(InBits - 1);
1136 
1137  if (SimplifyDemandedBits(Src, InDemandedBits, Known, TLO, Depth + 1))
1138  return true;
1139  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1140  // If the sign bit is known one, the top bits match.
1141  Known = Known.sext(BitWidth);
1142 
1143  // If the sign bit is known zero, convert this to a zero extend.
1144  if (Known.isNonNegative())
1145  return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Src));
1146  break;
1147  }
1149  // TODO - merge this with SIGN_EXTEND above?
1150  SDValue Src = Op.getOperand(0);
1151  unsigned InBits = Src.getScalarValueSizeInBits();
1152 
1153  APInt InDemandedBits = DemandedBits.trunc(InBits);
1154 
1155  // If some of the sign extended bits are demanded, we know that the sign
1156  // bit is demanded.
1157  if (InBits < DemandedBits.getActiveBits())
1158  InDemandedBits.setBit(InBits - 1);
1159 
1160  if (SimplifyDemandedBits(Src, InDemandedBits, Known, TLO, Depth + 1))
1161  return true;
1162  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1163  // If the sign bit is known one, the top bits match.
1164  Known = Known.sext(BitWidth);
1165  break;
1166  }
1167  case ISD::ANY_EXTEND: {
1168  SDValue Src = Op.getOperand(0);
1169  unsigned InBits = Src.getScalarValueSizeInBits();
1170  APInt InDemandedBits = DemandedBits.trunc(InBits);
1171  if (SimplifyDemandedBits(Src, InDemandedBits, Known, TLO, Depth+1))
1172  return true;
1173  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1174  Known = Known.zext(BitWidth);
1175  break;
1176  }
1177  case ISD::TRUNCATE: {
1178  SDValue Src = Op.getOperand(0);
1179 
1180  // Simplify the input, using demanded bit information, and compute the known
1181  // zero/one bits live out.
1182  unsigned OperandBitWidth = Src.getScalarValueSizeInBits();
1183  APInt TruncMask = DemandedBits.zext(OperandBitWidth);
1184  if (SimplifyDemandedBits(Src, TruncMask, Known, TLO, Depth + 1))
1185  return true;
1186  Known = Known.trunc(BitWidth);
1187 
1188  // If the input is only used by this truncate, see if we can shrink it based
1189  // on the known demanded bits.
1190  if (Src.getNode()->hasOneUse()) {
1191  switch (Src.getOpcode()) {
1192  default:
1193  break;
1194  case ISD::SRL:
1195  // Shrink SRL by a constant if none of the high bits shifted in are
1196  // demanded.
1197  if (TLO.LegalTypes() && !isTypeDesirableForOp(ISD::SRL, VT))
1198  // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is
1199  // undesirable.
1200  break;
1202  if (!ShAmt)
1203  break;
1204  SDValue Shift = Src.getOperand(1);
1205  if (TLO.LegalTypes()) {
1206  uint64_t ShVal = ShAmt->getZExtValue();
1207  Shift = TLO.DAG.getConstant(ShVal, dl, getShiftAmountTy(VT, DL));
1208  }
1209 
1210  if (ShAmt->getZExtValue() < BitWidth) {
1211  APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
1212  OperandBitWidth - BitWidth);
1213  HighBits.lshrInPlace(ShAmt->getZExtValue());
1214  HighBits = HighBits.trunc(BitWidth);
1215 
1216  if (!(HighBits & DemandedBits)) {
1217  // None of the shifted in bits are needed. Add a truncate of the
1218  // shift input, then shift it.
1219  SDValue NewTrunc =
1220  TLO.DAG.getNode(ISD::TRUNCATE, dl, VT, Src.getOperand(0));
1221  return TLO.CombineTo(
1222  Op, TLO.DAG.getNode(ISD::SRL, dl, VT, NewTrunc, Shift));
1223  }
1224  }
1225  break;
1226  }
1227  }
1228 
1229  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1230  break;
1231  }
1232  case ISD::AssertZext: {
1233  // AssertZext demands all of the high bits, plus any of the low bits
1234  // demanded by its users.
1235  EVT ZVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1236  APInt InMask = APInt::getLowBitsSet(BitWidth, ZVT.getSizeInBits());
1237  if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | DemandedBits,
1238  Known, TLO, Depth+1))
1239  return true;
1240  assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1241 
1242  Known.Zero |= ~InMask;
1243  break;
1244  }
1245  case ISD::EXTRACT_VECTOR_ELT: {
1246  SDValue Src = Op.getOperand(0);
1247  SDValue Idx = Op.getOperand(1);
1248  unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
1249  unsigned EltBitWidth = Src.getScalarValueSizeInBits();
1250 
1251  // Demand the bits from every vector element without a constant index.
1252  APInt DemandedSrcElts = APInt::getAllOnesValue(NumSrcElts);
1253  if (auto *CIdx = dyn_cast<ConstantSDNode>(Idx))
1254  if (CIdx->getAPIntValue().ult(NumSrcElts))
1255  DemandedSrcElts = APInt::getOneBitSet(NumSrcElts, CIdx->getZExtValue());
1256 
1257  // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
1258  // anything about the extended bits.
1259  APInt DemandedSrcBits = DemandedBits;
1260  if (BitWidth > EltBitWidth)
1261  DemandedSrcBits = DemandedSrcBits.trunc(EltBitWidth);
1262 
1263  if (SimplifyDemandedBits(Src, DemandedSrcBits, DemandedSrcElts, Known2, TLO,
1264  Depth + 1))
1265  return true;
1266 
1267  Known = Known2;
1268  if (BitWidth > EltBitWidth)
1269  Known = Known.zext(BitWidth);
1270  break;
1271  }
1272  case ISD::BITCAST: {
1273  SDValue Src = Op.getOperand(0);
1274  EVT SrcVT = Src.getValueType();
1275  unsigned NumSrcEltBits = SrcVT.getScalarSizeInBits();
1276 
1277  // If this is an FP->Int bitcast and if the sign bit is the only
1278  // thing demanded, turn this into a FGETSIGN.
1279  if (!TLO.LegalOperations() && !VT.isVector() && !SrcVT.isVector() &&
1280  DemandedBits == APInt::getSignMask(Op.getValueSizeInBits()) &&
1281  SrcVT.isFloatingPoint()) {
1282  bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, VT);
1284  if ((OpVTLegal || i32Legal) && VT.isSimple() && SrcVT != MVT::f16 &&
1285  SrcVT != MVT::f128) {
1286  // Cannot eliminate/lower SHL for f128 yet.
1287  EVT Ty = OpVTLegal ? VT : MVT::i32;
1288  // Make a FGETSIGN + SHL to move the sign bit into the appropriate
1289  // place. We expect the SHL to be eliminated by other optimizations.
1290  SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Src);
1291  unsigned OpVTSizeInBits = Op.getValueSizeInBits();
1292  if (!OpVTLegal && OpVTSizeInBits > 32)
1293  Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Sign);
1294  unsigned ShVal = Op.getValueSizeInBits() - 1;
1295  SDValue ShAmt = TLO.DAG.getConstant(ShVal, dl, VT);
1296  return TLO.CombineTo(Op,
1297  TLO.DAG.getNode(ISD::SHL, dl, VT, Sign, ShAmt));
1298  }
1299  }
1300  // If bitcast from a vector, see if we can use SimplifyDemandedVectorElts by
1301  // demanding the element if any bits from it are demanded.
1302  // TODO - bigendian once we have test coverage.
1303  // TODO - bool vectors once SimplifyDemandedVectorElts has SETCC support.
1304  if (SrcVT.isVector() && NumSrcEltBits > 1 &&
1305  (BitWidth % NumSrcEltBits) == 0 &&
1306  TLO.DAG.getDataLayout().isLittleEndian()) {
1307  unsigned Scale = BitWidth / NumSrcEltBits;
1308  auto GetDemandedSubMask = [&](APInt &DemandedSubElts) -> bool {
1309  DemandedSubElts = APInt::getNullValue(Scale);
1310  for (unsigned i = 0; i != Scale; ++i) {
1311  unsigned Offset = i * NumSrcEltBits;
1312  APInt Sub = DemandedBits.extractBits(NumSrcEltBits, Offset);
1313  if (!Sub.isNullValue())
1314  DemandedSubElts.setBit(i);
1315  }
1316  return true;
1317  };
1318 
1319  APInt DemandedSubElts;
1320  if (GetDemandedSubMask(DemandedSubElts)) {
1321  unsigned NumSrcElts = SrcVT.getVectorNumElements();
1322  APInt DemandedElts = APInt::getSplat(NumSrcElts, DemandedSubElts);
1323 
1324  APInt KnownUndef, KnownZero;
1325  if (SimplifyDemandedVectorElts(Src, DemandedElts, KnownUndef, KnownZero,
1326  TLO, Depth + 1))
1327  return true;
1328  }
1329  }
1330  // If this is a bitcast, let computeKnownBits handle it. Only do this on a
1331  // recursive call where Known may be useful to the caller.
1332  if (Depth > 0) {
1333  Known = TLO.DAG.computeKnownBits(Op, Depth);
1334  return false;
1335  }
1336  break;
1337  }
1338  case ISD::ADD:
1339  case ISD::MUL:
1340  case ISD::SUB: {
1341  // Add, Sub, and Mul don't demand any bits in positions beyond that
1342  // of the highest bit demanded of them.
1343  SDValue Op0 = Op.getOperand(0), Op1 = Op.getOperand(1);
1344  unsigned DemandedBitsLZ = DemandedBits.countLeadingZeros();
1345  APInt LoMask = APInt::getLowBitsSet(BitWidth, BitWidth - DemandedBitsLZ);
1346  if (SimplifyDemandedBits(Op0, LoMask, DemandedElts, Known2, TLO, Depth + 1) ||
1347  SimplifyDemandedBits(Op1, LoMask, DemandedElts, Known2, TLO, Depth + 1) ||
1348  // See if the operation should be performed at a smaller bit width.
1349  ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO)) {
1350  SDNodeFlags Flags = Op.getNode()->getFlags();
1351  if (Flags.hasNoSignedWrap() || Flags.hasNoUnsignedWrap()) {
1352  // Disable the nsw and nuw flags. We can no longer guarantee that we
1353  // won't wrap after simplification.
1354  Flags.setNoSignedWrap(false);
1355  Flags.setNoUnsignedWrap(false);
1356  SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Op1,
1357  Flags);
1358  return TLO.CombineTo(Op, NewOp);
1359  }
1360  return true;
1361  }
1362 
1363  // If we have a constant operand, we may be able to turn it into -1 if we
1364  // do not demand the high bits. This can make the constant smaller to
1365  // encode, allow more general folding, or match specialized instruction
1366  // patterns (eg, 'blsr' on x86). Don't bother changing 1 to -1 because that
1367  // is probably not useful (and could be detrimental).
1369  APInt HighMask = APInt::getHighBitsSet(BitWidth, DemandedBitsLZ);
1370  if (C && !C->isAllOnesValue() && !C->isOne() &&
1371  (C->getAPIntValue() | HighMask).isAllOnesValue()) {
1372  SDValue Neg1 = TLO.DAG.getAllOnesConstant(dl, VT);
1373  // We can't guarantee that the new math op doesn't wrap, so explicitly
1374  // clear those flags to prevent folding with a potential existing node
1375  // that has those flags set.
1376  SDNodeFlags Flags;
1377  Flags.setNoSignedWrap(false);
1378  Flags.setNoUnsignedWrap(false);
1379  SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Neg1, Flags);
1380  return TLO.CombineTo(Op, NewOp);
1381  }
1382 
1384  }
1385  default:
1386  if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1387  if (SimplifyDemandedBitsForTargetNode(Op, DemandedBits, DemandedElts,
1388  Known, TLO, Depth))
1389  return true;
1390  break;
1391  }
1392 
1393  // Just use computeKnownBits to compute output bits.
1394  Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth);
1395  break;
1396  }
1397 
1398  // If we know the value of all of the demanded bits, return this as a
1399  // constant.
1400  if (DemandedBits.isSubsetOf(Known.Zero | Known.One)) {
1401  // Avoid folding to a constant if any OpaqueConstant is involved.
1402  const SDNode *N = Op.getNode();
1404  E = SDNodeIterator::end(N);
1405  I != E; ++I) {
1406  SDNode *Op = *I;
1407  if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
1408  if (C->isOpaque())
1409  return false;
1410  }
1411  // TODO: Handle float bits as well.
1412  if (VT.isInteger())
1413  return TLO.CombineTo(Op, TLO.DAG.getConstant(Known.One, dl, VT));
1414  }
1415 
1416  return false;
1417 }
1418 
1420  const APInt &DemandedElts,
1421  APInt &KnownUndef,
1422  APInt &KnownZero,
1423  DAGCombinerInfo &DCI) const {
1424  SelectionDAG &DAG = DCI.DAG;
1425  TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1426  !DCI.isBeforeLegalizeOps());
1427 
1428  bool Simplified =
1429  SimplifyDemandedVectorElts(Op, DemandedElts, KnownUndef, KnownZero, TLO);
1430  if (Simplified) {
1431  DCI.AddToWorklist(Op.getNode());
1432  DCI.CommitTargetLoweringOpt(TLO);
1433  }
1434  return Simplified;
1435 }
1436 
1438  SDValue Op, const APInt &DemandedEltMask, APInt &KnownUndef,
1439  APInt &KnownZero, TargetLoweringOpt &TLO, unsigned Depth,
1440  bool AssumeSingleUse) const {
1441  EVT VT = Op.getValueType();
1442  APInt DemandedElts = DemandedEltMask;
1443  unsigned NumElts = DemandedElts.getBitWidth();
1444  assert(VT.isVector() && "Expected vector op");
1445  assert(VT.getVectorNumElements() == NumElts &&
1446  "Mask size mismatches value type element count!");
1447 
1448  KnownUndef = KnownZero = APInt::getNullValue(NumElts);
1449 
1450  // Undef operand.
1451  if (Op.isUndef()) {
1452  KnownUndef.setAllBits();
1453  return false;
1454  }
1455 
1456  // If Op has other users, assume that all elements are needed.
1457  if (!Op.getNode()->hasOneUse() && !AssumeSingleUse)
1458  DemandedElts.setAllBits();
1459 
1460  // Not demanding any elements from Op.
1461  if (DemandedElts == 0) {
1462  KnownUndef.setAllBits();
1463  return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT));
1464  }
1465 
1466  // Limit search depth.
1467  if (Depth >= 6)
1468  return false;
1469 
1470  SDLoc DL(Op);
1471  unsigned EltSizeInBits = VT.getScalarSizeInBits();
1472 
1473  switch (Op.getOpcode()) {
1474  case ISD::SCALAR_TO_VECTOR: {
1475  if (!DemandedElts[0]) {
1476  KnownUndef.setAllBits();
1477  return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT));
1478  }
1479  KnownUndef.setHighBits(NumElts - 1);
1480  break;
1481  }
1482  case ISD::BITCAST: {
1483  SDValue Src = Op.getOperand(0);
1484  EVT SrcVT = Src.getValueType();
1485 
1486  // We only handle vectors here.
1487  // TODO - investigate calling SimplifyDemandedBits/ComputeKnownBits?
1488  if (!SrcVT.isVector())
1489  break;
1490 
1491  // Fast handling of 'identity' bitcasts.
1492  unsigned NumSrcElts = SrcVT.getVectorNumElements();
1493  if (NumSrcElts == NumElts)
1494  return SimplifyDemandedVectorElts(Src, DemandedElts, KnownUndef,
1495  KnownZero, TLO, Depth + 1);
1496 
1497  APInt SrcZero, SrcUndef;
1498  APInt SrcDemandedElts = APInt::getNullValue(NumSrcElts);
1499 
1500  // Bitcast from 'large element' src vector to 'small element' vector, we
1501  // must demand a source element if any DemandedElt maps to it.
1502  if ((NumElts % NumSrcElts) == 0) {
1503  unsigned Scale = NumElts / NumSrcElts;
1504  for (unsigned i = 0; i != NumElts; ++i)
1505  if (DemandedElts[i])
1506  SrcDemandedElts.setBit(i / Scale);
1507 
1508  if (SimplifyDemandedVectorElts(Src, SrcDemandedElts, SrcUndef, SrcZero,
1509  TLO, Depth + 1))
1510  return true;
1511 
1512  // Try calling SimplifyDemandedBits, converting demanded elts to the bits
1513  // of the large element.
1514  // TODO - bigendian once we have test coverage.
1515  if (TLO.DAG.getDataLayout().isLittleEndian()) {
1516  unsigned SrcEltSizeInBits = SrcVT.getScalarSizeInBits();
1517  APInt SrcDemandedBits = APInt::getNullValue(SrcEltSizeInBits);
1518  for (unsigned i = 0; i != NumElts; ++i)
1519  if (DemandedElts[i]) {
1520  unsigned Ofs = (i % Scale) * EltSizeInBits;
1521  SrcDemandedBits.setBits(Ofs, Ofs + EltSizeInBits);
1522  }
1523 
1524  KnownBits Known;
1525  if (SimplifyDemandedBits(Src, SrcDemandedBits, Known, TLO, Depth + 1))
1526  return true;
1527  }
1528 
1529  // If the src element is zero/undef then all the output elements will be -
1530  // only demanded elements are guaranteed to be correct.
1531  for (unsigned i = 0; i != NumSrcElts; ++i) {
1532  if (SrcDemandedElts[i]) {
1533  if (SrcZero[i])
1534  KnownZero.setBits(i * Scale, (i + 1) * Scale);
1535  if (SrcUndef[i])
1536  KnownUndef.setBits(i * Scale, (i + 1) * Scale);
1537  }
1538  }
1539  }
1540 
1541  // Bitcast from 'small element' src vector to 'large element' vector, we
1542  // demand all smaller source elements covered by the larger demanded element
1543  // of this vector.
1544  if ((NumSrcElts % NumElts) == 0) {
1545  unsigned Scale = NumSrcElts / NumElts;
1546  for (unsigned i = 0; i != NumElts; ++i)
1547  if (DemandedElts[i])
1548  SrcDemandedElts.setBits(i * Scale, (i + 1) * Scale);
1549 
1550  if (SimplifyDemandedVectorElts(Src, SrcDemandedElts, SrcUndef, SrcZero,
1551  TLO, Depth + 1))
1552  return true;
1553 
1554  // If all the src elements covering an output element are zero/undef, then
1555  // the output element will be as well, assuming it was demanded.
1556  for (unsigned i = 0; i != NumElts; ++i) {
1557  if (DemandedElts[i]) {
1558  if (SrcZero.extractBits(Scale, i * Scale).isAllOnesValue())
1559  KnownZero.setBit(i);
1560  if (SrcUndef.extractBits(Scale, i * Scale).isAllOnesValue())
1561  KnownUndef.setBit(i);
1562  }
1563  }
1564  }
1565  break;
1566  }
1567  case ISD::BUILD_VECTOR: {
1568  // Check all elements and simplify any unused elements with UNDEF.
1569  if (!DemandedElts.isAllOnesValue()) {
1570  // Don't simplify BROADCASTS.
1571  if (llvm::any_of(Op->op_values(),
1572  [&](SDValue Elt) { return Op.getOperand(0) != Elt; })) {
1573  SmallVector<SDValue, 32> Ops(Op->op_begin(), Op->op_end());
1574  bool Updated = false;
1575  for (unsigned i = 0; i != NumElts; ++i) {
1576  if (!DemandedElts[i] && !Ops[i].isUndef()) {
1577  Ops[i] = TLO.DAG.getUNDEF(Ops[0].getValueType());
1578  KnownUndef.setBit(i);
1579  Updated = true;
1580  }
1581  }
1582  if (Updated)
1583  return TLO.CombineTo(Op, TLO.DAG.getBuildVector(VT, DL, Ops));
1584  }
1585  }
1586  for (unsigned i = 0; i != NumElts; ++i) {
1587  SDValue SrcOp = Op.getOperand(i);
1588  if (SrcOp.isUndef()) {
1589  KnownUndef.setBit(i);
1590  } else if (EltSizeInBits == SrcOp.getScalarValueSizeInBits() &&
1591  (isNullConstant(SrcOp) || isNullFPConstant(SrcOp))) {
1592  KnownZero.setBit(i);
1593  }
1594  }
1595  break;
1596  }
1597  case ISD::CONCAT_VECTORS: {
1598  EVT SubVT = Op.getOperand(0).getValueType();
1599  unsigned NumSubVecs = Op.getNumOperands();
1600  unsigned NumSubElts = SubVT.getVectorNumElements();
1601  for (unsigned i = 0; i != NumSubVecs; ++i) {
1602  SDValue SubOp = Op.getOperand(i);
1603  APInt SubElts = DemandedElts.extractBits(NumSubElts, i * NumSubElts);
1604  APInt SubUndef, SubZero;
1605  if (SimplifyDemandedVectorElts(SubOp, SubElts, SubUndef, SubZero, TLO,
1606  Depth + 1))
1607  return true;
1608  KnownUndef.insertBits(SubUndef, i * NumSubElts);
1609  KnownZero.insertBits(SubZero, i * NumSubElts);
1610  }
1611  break;
1612  }
1613  case ISD::INSERT_SUBVECTOR: {
1614  if (!isa<ConstantSDNode>(Op.getOperand(2)))
1615  break;
1616  SDValue Base = Op.getOperand(0);
1617  SDValue Sub = Op.getOperand(1);
1618  EVT SubVT = Sub.getValueType();
1619  unsigned NumSubElts = SubVT.getVectorNumElements();
1620  const APInt& Idx = cast<ConstantSDNode>(Op.getOperand(2))->getAPIntValue();
1621  if (Idx.ugt(NumElts - NumSubElts))
1622  break;
1623  unsigned SubIdx = Idx.getZExtValue();
1624  APInt SubElts = DemandedElts.extractBits(NumSubElts, SubIdx);
1625  APInt SubUndef, SubZero;
1626  if (SimplifyDemandedVectorElts(Sub, SubElts, SubUndef, SubZero, TLO,
1627  Depth + 1))
1628  return true;
1629  APInt BaseElts = DemandedElts;
1630  BaseElts.insertBits(APInt::getNullValue(NumSubElts), SubIdx);
1631  if (SimplifyDemandedVectorElts(Base, BaseElts, KnownUndef, KnownZero, TLO,
1632  Depth + 1))
1633  return true;
1634  KnownUndef.insertBits(SubUndef, SubIdx);
1635  KnownZero.insertBits(SubZero, SubIdx);
1636  break;
1637  }
1638  case ISD::EXTRACT_SUBVECTOR: {
1639  SDValue Src = Op.getOperand(0);
1641  unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
1642  if (SubIdx && SubIdx->getAPIntValue().ule(NumSrcElts - NumElts)) {
1643  // Offset the demanded elts by the subvector index.
1644  uint64_t Idx = SubIdx->getZExtValue();
1645  APInt SrcElts = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
1646  APInt SrcUndef, SrcZero;
1647  if (SimplifyDemandedVectorElts(Src, SrcElts, SrcUndef, SrcZero, TLO,
1648  Depth + 1))
1649  return true;
1650  KnownUndef = SrcUndef.extractBits(NumElts, Idx);
1651  KnownZero = SrcZero.extractBits(NumElts, Idx);
1652  }
1653  break;
1654  }
1655  case ISD::INSERT_VECTOR_ELT: {
1656  SDValue Vec = Op.getOperand(0);
1657  SDValue Scl = Op.getOperand(1);
1658  auto *CIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1659 
1660  // For a legal, constant insertion index, if we don't need this insertion
1661  // then strip it, else remove it from the demanded elts.
1662  if (CIdx && CIdx->getAPIntValue().ult(NumElts)) {
1663  unsigned Idx = CIdx->getZExtValue();
1664  if (!DemandedElts[Idx])
1665  return TLO.CombineTo(Op, Vec);
1666 
1667  APInt DemandedVecElts(DemandedElts);
1668  DemandedVecElts.clearBit(Idx);
1669  if (SimplifyDemandedVectorElts(Vec, DemandedVecElts, KnownUndef,
1670  KnownZero, TLO, Depth + 1))
1671  return true;
1672 
1673  KnownUndef.clearBit(Idx);
1674  if (Scl.isUndef())
1675  KnownUndef.setBit(Idx);
1676 
1677  KnownZero.clearBit(Idx);
1678  if (isNullConstant(Scl) || isNullFPConstant(Scl))
1679  KnownZero.setBit(Idx);
1680  break;
1681  }
1682 
1683  APInt VecUndef, VecZero;
1684  if (SimplifyDemandedVectorElts(Vec, DemandedElts, VecUndef, VecZero, TLO,
1685  Depth + 1))
1686  return true;
1687  // Without knowing the insertion index we can't set KnownUndef/KnownZero.
1688  break;
1689  }
1690  case ISD::VSELECT: {
1691  // Try to transform the select condition based on the current demanded
1692  // elements.
1693  // TODO: If a condition element is undef, we can choose from one arm of the
1694  // select (and if one arm is undef, then we can propagate that to the
1695  // result).
1696  // TODO - add support for constant vselect masks (see IR version of this).
1697  APInt UnusedUndef, UnusedZero;
1698  if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedElts, UnusedUndef,
1699  UnusedZero, TLO, Depth + 1))
1700  return true;
1701 
1702  // See if we can simplify either vselect operand.
1703  APInt DemandedLHS(DemandedElts);
1704  APInt DemandedRHS(DemandedElts);
1705  APInt UndefLHS, ZeroLHS;
1706  APInt UndefRHS, ZeroRHS;
1707  if (SimplifyDemandedVectorElts(Op.getOperand(1), DemandedLHS, UndefLHS,
1708  ZeroLHS, TLO, Depth + 1))
1709  return true;
1710  if (SimplifyDemandedVectorElts(Op.getOperand(2), DemandedRHS, UndefRHS,
1711  ZeroRHS, TLO, Depth + 1))
1712  return true;
1713 
1714  KnownUndef = UndefLHS & UndefRHS;
1715  KnownZero = ZeroLHS & ZeroRHS;
1716  break;
1717  }
1718  case ISD::VECTOR_SHUFFLE: {
1719  ArrayRef<int> ShuffleMask = cast<ShuffleVectorSDNode>(Op)->getMask();
1720 
1721  // Collect demanded elements from shuffle operands..
1722  APInt DemandedLHS(NumElts, 0);
1723  APInt DemandedRHS(NumElts, 0);
1724  for (unsigned i = 0; i != NumElts; ++i) {
1725  int M = ShuffleMask[i];
1726  if (M < 0 || !DemandedElts[i])
1727  continue;
1728  assert(0 <= M && M < (int)(2 * NumElts) && "Shuffle index out of range");
1729  if (M < (int)NumElts)
1730  DemandedLHS.setBit(M);
1731  else
1732  DemandedRHS.setBit(M - NumElts);
1733  }
1734 
1735  // See if we can simplify either shuffle operand.
1736  APInt UndefLHS, ZeroLHS;
1737  APInt UndefRHS, ZeroRHS;
1738  if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedLHS, UndefLHS,
1739  ZeroLHS, TLO, Depth + 1))
1740  return true;
1741  if (SimplifyDemandedVectorElts(Op.getOperand(1), DemandedRHS, UndefRHS,
1742  ZeroRHS, TLO, Depth + 1))
1743  return true;
1744 
1745  // Simplify mask using undef elements from LHS/RHS.
1746  bool Updated = false;
1747  bool IdentityLHS = true, IdentityRHS = true;
1748  SmallVector<int, 32> NewMask(ShuffleMask.begin(), ShuffleMask.end());
1749  for (unsigned i = 0; i != NumElts; ++i) {
1750  int &M = NewMask[i];
1751  if (M < 0)
1752  continue;
1753  if (!DemandedElts[i] || (M < (int)NumElts && UndefLHS[M]) ||
1754  (M >= (int)NumElts && UndefRHS[M - NumElts])) {
1755  Updated = true;
1756  M = -1;
1757  }
1758  IdentityLHS &= (M < 0) || (M == (int)i);
1759  IdentityRHS &= (M < 0) || ((M - NumElts) == i);
1760  }
1761 
1762  // Update legal shuffle masks based on demanded elements if it won't reduce
1763  // to Identity which can cause premature removal of the shuffle mask.
1764  if (Updated && !IdentityLHS && !IdentityRHS && !TLO.LegalOps &&
1765  isShuffleMaskLegal(NewMask, VT))
1766  return TLO.CombineTo(Op,
1767  TLO.DAG.getVectorShuffle(VT, DL, Op.getOperand(0),
1768  Op.getOperand(1), NewMask));
1769 
1770  // Propagate undef/zero elements from LHS/RHS.
1771  for (unsigned i = 0; i != NumElts; ++i) {
1772  int M = ShuffleMask[i];
1773  if (M < 0) {
1774  KnownUndef.setBit(i);
1775  } else if (M < (int)NumElts) {
1776  if (UndefLHS[M])
1777  KnownUndef.setBit(i);
1778  if (ZeroLHS[M])
1779  KnownZero.setBit(i);
1780  } else {
1781  if (UndefRHS[M - NumElts])
1782  KnownUndef.setBit(i);
1783  if (ZeroRHS[M - NumElts])
1784  KnownZero.setBit(i);
1785  }
1786  }
1787  break;
1788  }
1791  APInt SrcUndef, SrcZero;
1792  SDValue Src = Op.getOperand(0);
1793  unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
1794  APInt DemandedSrcElts = DemandedElts.zextOrSelf(NumSrcElts);
1795  if (SimplifyDemandedVectorElts(Src, DemandedSrcElts, SrcUndef,
1796  SrcZero, TLO, Depth + 1))
1797  return true;
1798  KnownZero = SrcZero.zextOrTrunc(NumElts);
1799  KnownUndef = SrcUndef.zextOrTrunc(NumElts);
1800 
1802  // zext(undef) upper bits are guaranteed to be zero.
1803  if (DemandedElts.isSubsetOf(KnownUndef))
1804  return TLO.CombineTo(Op, TLO.DAG.getConstant(0, SDLoc(Op), VT));
1805  KnownUndef.clearAllBits();
1806  }
1807  break;
1808  }
1809  case ISD::OR:
1810  case ISD::XOR:
1811  case ISD::ADD:
1812  case ISD::SUB:
1813  case ISD::FADD:
1814  case ISD::FSUB:
1815  case ISD::FMUL:
1816  case ISD::FDIV:
1817  case ISD::FREM: {
1818  APInt SrcUndef, SrcZero;
1819  if (SimplifyDemandedVectorElts(Op.getOperand(1), DemandedElts, SrcUndef,
1820  SrcZero, TLO, Depth + 1))
1821  return true;
1822  if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedElts, KnownUndef,
1823  KnownZero, TLO, Depth + 1))
1824  return true;
1825  KnownZero &= SrcZero;
1826  KnownUndef &= SrcUndef;
1827  break;
1828  }
1829  case ISD::AND: {
1830  APInt SrcUndef, SrcZero;
1831  if (SimplifyDemandedVectorElts(Op.getOperand(1), DemandedElts, SrcUndef,
1832  SrcZero, TLO, Depth + 1))
1833  return true;
1834  if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedElts, KnownUndef,
1835  KnownZero, TLO, Depth + 1))
1836  return true;
1837 
1838  // If either side has a zero element, then the result element is zero, even
1839  // if the other is an UNDEF.
1840  KnownZero |= SrcZero;
1841  KnownUndef &= SrcUndef;
1842  KnownUndef &= ~KnownZero;
1843  break;
1844  }
1845  case ISD::TRUNCATE:
1846  case ISD::SIGN_EXTEND:
1847  case ISD::ZERO_EXTEND:
1848  if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedElts, KnownUndef,
1849  KnownZero, TLO, Depth + 1))
1850  return true;
1851 
1852  if (Op.getOpcode() == ISD::ZERO_EXTEND) {
1853  // zext(undef) upper bits are guaranteed to be zero.
1854  if (DemandedElts.isSubsetOf(KnownUndef))
1855  return TLO.CombineTo(Op, TLO.DAG.getConstant(0, SDLoc(Op), VT));
1856  KnownUndef.clearAllBits();
1857  }
1858  break;
1859  default: {
1860  if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1861  if (SimplifyDemandedVectorEltsForTargetNode(Op, DemandedElts, KnownUndef,
1862  KnownZero, TLO, Depth))
1863  return true;
1864  } else {
1865  KnownBits Known;
1866  APInt DemandedBits = APInt::getAllOnesValue(EltSizeInBits);
1867  if (SimplifyDemandedBits(Op, DemandedBits, DemandedEltMask, Known, TLO,
1868  Depth, AssumeSingleUse))
1869  return true;
1870  }
1871  break;
1872  }
1873  }
1874  assert((KnownUndef & KnownZero) == 0 && "Elements flagged as undef AND zero");
1875 
1876  // Constant fold all undef cases.
1877  // TODO: Handle zero cases as well.
1878  if (DemandedElts.isSubsetOf(KnownUndef))
1879  return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT));
1880 
1881  return false;
1882 }
1883 
1884 /// Determine which of the bits specified in Mask are known to be either zero or
1885 /// one and return them in the Known.
1887  KnownBits &Known,
1888  const APInt &DemandedElts,
1889  const SelectionDAG &DAG,
1890  unsigned Depth) const {
1894  Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1895  "Should use MaskedValueIsZero if you don't know whether Op"
1896  " is a target node!");
1897  Known.resetAll();
1898 }
1899 
1901  KnownBits &Known,
1902  const APInt &DemandedElts,
1903  const SelectionDAG &DAG,
1904  unsigned Depth) const {
1905  assert(isa<FrameIndexSDNode>(Op) && "expected FrameIndex");
1906 
1907  if (unsigned Align = DAG.InferPtrAlignment(Op)) {
1908  // The low bits are known zero if the pointer is aligned.
1909  Known.Zero.setLowBits(Log2_32(Align));
1910  }
1911 }
1912 
1913 /// This method can be implemented by targets that want to expose additional
1914 /// information about sign bits to the DAG Combiner.
1916  const APInt &,
1917  const SelectionDAG &,
1918  unsigned Depth) const {
1922  Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1923  "Should use ComputeNumSignBits if you don't know whether Op"
1924  " is a target node!");
1925  return 1;
1926 }
1927 
1929  SDValue Op, const APInt &DemandedElts, APInt &KnownUndef, APInt &KnownZero,
1930  TargetLoweringOpt &TLO, unsigned Depth) const {
1934  Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1935  "Should use SimplifyDemandedVectorElts if you don't know whether Op"
1936  " is a target node!");
1937  return false;
1938 }
1939 
1941  SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts,
1942  KnownBits &Known, TargetLoweringOpt &TLO, unsigned Depth) const {
1946  Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1947  "Should use SimplifyDemandedBits if you don't know whether Op"
1948  " is a target node!");
1949  computeKnownBitsForTargetNode(Op, Known, DemandedElts, TLO.DAG, Depth);
1950  return false;
1951 }
1952 
1954  const SelectionDAG &DAG,
1955  bool SNaN,
1956  unsigned Depth) const {
1960  Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1961  "Should use isKnownNeverNaN if you don't know whether Op"
1962  " is a target node!");
1963  return false;
1964 }
1965 
1966 // FIXME: Ideally, this would use ISD::isConstantSplatVector(), but that must
1967 // work with truncating build vectors and vectors with elements of less than
1968 // 8 bits.
1970  if (!N)
1971  return false;
1972 
1973  APInt CVal;
1974  if (auto *CN = dyn_cast<ConstantSDNode>(N)) {
1975  CVal = CN->getAPIntValue();
1976  } else if (auto *BV = dyn_cast<BuildVectorSDNode>(N)) {
1977  auto *CN = BV->getConstantSplatNode();
1978  if (!CN)
1979  return false;
1980 
1981  // If this is a truncating build vector, truncate the splat value.
1982  // Otherwise, we may fail to match the expected values below.
1983  unsigned BVEltWidth = BV->getValueType(0).getScalarSizeInBits();
1984  CVal = CN->getAPIntValue();
1985  if (BVEltWidth < CVal.getBitWidth())
1986  CVal = CVal.trunc(BVEltWidth);
1987  } else {
1988  return false;
1989  }
1990 
1991  switch (getBooleanContents(N->getValueType(0))) {
1993  return CVal[0];
1995  return CVal.isOneValue();
1997  return CVal.isAllOnesValue();
1998  }
1999 
2000  llvm_unreachable("Invalid boolean contents");
2001 }
2002 
2004  if (!N)
2005  return false;
2006 
2007  const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
2008  if (!CN) {
2010  if (!BV)
2011  return false;
2012 
2013  // Only interested in constant splats, we don't care about undef
2014  // elements in identifying boolean constants and getConstantSplatNode
2015  // returns NULL if all ops are undef;
2016  CN = BV->getConstantSplatNode();
2017  if (!CN)
2018  return false;
2019  }
2020 
2022  return !CN->getAPIntValue()[0];
2023 
2024  return CN->isNullValue();
2025 }
2026 
2028  bool SExt) const {
2029  if (VT == MVT::i1)
2030  return N->isOne();
2031 
2033  switch (Cnt) {
2035  // An extended value of 1 is always true, unless its original type is i1,
2036  // in which case it will be sign extended to -1.
2037  return (N->isOne() && !SExt) || (SExt && (N->getValueType(0) != MVT::i1));
2040  return N->isAllOnesValue() && SExt;
2041  }
2042  llvm_unreachable("Unexpected enumeration.");
2043 }
2044 
2045 /// This helper function of SimplifySetCC tries to optimize the comparison when
2046 /// either operand of the SetCC node is a bitwise-and instruction.
2047 SDValue TargetLowering::simplifySetCCWithAnd(EVT VT, SDValue N0, SDValue N1,
2048  ISD::CondCode Cond,
2049  DAGCombinerInfo &DCI,
2050  const SDLoc &DL) const {
2051  // Match these patterns in any of their permutations:
2052  // (X & Y) == Y
2053  // (X & Y) != Y
2054  if (N1.getOpcode() == ISD::AND && N0.getOpcode() != ISD::AND)
2055  std::swap(N0, N1);
2056 
2057  EVT OpVT = N0.getValueType();
2058  if (N0.getOpcode() != ISD::AND || !OpVT.isInteger() ||
2059  (Cond != ISD::SETEQ && Cond != ISD::SETNE))
2060  return SDValue();
2061 
2062  SDValue X, Y;
2063  if (N0.getOperand(0) == N1) {
2064  X = N0.getOperand(1);
2065  Y = N0.getOperand(0);
2066  } else if (N0.getOperand(1) == N1) {
2067  X = N0.getOperand(0);
2068  Y = N0.getOperand(1);
2069  } else {
2070  return SDValue();
2071  }
2072 
2073  SelectionDAG &DAG = DCI.DAG;
2074  SDValue Zero = DAG.getConstant(0, DL, OpVT);
2075  if (DAG.isKnownToBeAPowerOfTwo(Y)) {
2076  // Simplify X & Y == Y to X & Y != 0 if Y has exactly one bit set.
2077  // Note that where Y is variable and is known to have at most one bit set
2078  // (for example, if it is Z & 1) we cannot do this; the expressions are not
2079  // equivalent when Y == 0.
2080  Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2081  if (DCI.isBeforeLegalizeOps() ||
2082  isCondCodeLegal(Cond, N0.getSimpleValueType()))
2083  return DAG.getSetCC(DL, VT, N0, Zero, Cond);
2084  } else if (N0.hasOneUse() && hasAndNotCompare(Y)) {
2085  // If the target supports an 'and-not' or 'and-complement' logic operation,
2086  // try to use that to make a comparison operation more efficient.
2087  // But don't do this transform if the mask is a single bit because there are
2088  // more efficient ways to deal with that case (for example, 'bt' on x86 or
2089  // 'rlwinm' on PPC).
2090 
2091  // Bail out if the compare operand that we want to turn into a zero is
2092  // already a zero (otherwise, infinite loop).
2093  auto *YConst = dyn_cast<ConstantSDNode>(Y);
2094  if (YConst && YConst->isNullValue())
2095  return SDValue();
2096 
2097  // Transform this into: ~X & Y == 0.
2098  SDValue NotX = DAG.getNOT(SDLoc(X), X, OpVT);
2099  SDValue NewAnd = DAG.getNode(ISD::AND, SDLoc(N0), OpVT, NotX, Y);
2100  return DAG.getSetCC(DL, VT, NewAnd, Zero, Cond);
2101  }
2102 
2103  return SDValue();
2104 }
2105 
2106 /// There are multiple IR patterns that could be checking whether certain
2107 /// truncation of a signed number would be lossy or not. The pattern which is
2108 /// best at IR level, may not lower optimally. Thus, we want to unfold it.
2109 /// We are looking for the following pattern: (KeptBits is a constant)
2110 /// (add %x, (1 << (KeptBits-1))) srccond (1 << KeptBits)
2111 /// KeptBits won't be bitwidth(x), that will be constant-folded to true/false.
2112 /// KeptBits also can't be 1, that would have been folded to %x dstcond 0
2113 /// We will unfold it into the natural trunc+sext pattern:
2114 /// ((%x << C) a>> C) dstcond %x
2115 /// Where C = bitwidth(x) - KeptBits and C u< bitwidth(x)
2116 SDValue TargetLowering::optimizeSetCCOfSignedTruncationCheck(
2117  EVT SCCVT, SDValue N0, SDValue N1, ISD::CondCode Cond, DAGCombinerInfo &DCI,
2118  const SDLoc &DL) const {
2119  // We must be comparing with a constant.
2120  ConstantSDNode *C1;
2121  if (!(C1 = dyn_cast<ConstantSDNode>(N1)))
2122  return SDValue();
2123 
2124  // N0 should be: add %x, (1 << (KeptBits-1))
2125  if (N0->getOpcode() != ISD::ADD)
2126  return SDValue();
2127 
2128  // And we must be 'add'ing a constant.
2129  ConstantSDNode *C01;
2130  if (!(C01 = dyn_cast<ConstantSDNode>(N0->getOperand(1))))
2131  return SDValue();
2132 
2133  SDValue X = N0->getOperand(0);
2134  EVT XVT = X.getValueType();
2135 
2136  // Validate constants ...
2137 
2138  APInt I1 = C1->getAPIntValue();
2139 
2140  ISD::CondCode NewCond;
2141  if (Cond == ISD::CondCode::SETULT) {
2142  NewCond = ISD::CondCode::SETEQ;
2143  } else if (Cond == ISD::CondCode::SETULE) {
2144  NewCond = ISD::CondCode::SETEQ;
2145  // But need to 'canonicalize' the constant.
2146  I1 += 1;
2147  } else if (Cond == ISD::CondCode::SETUGT) {
2148  NewCond = ISD::CondCode::SETNE;
2149  // But need to 'canonicalize' the constant.
2150  I1 += 1;
2151  } else if (Cond == ISD::CondCode::SETUGE) {
2152  NewCond = ISD::CondCode::SETNE;
2153  } else
2154  return SDValue();
2155 
2156  APInt I01 = C01->getAPIntValue();
2157 
2158  auto checkConstants = [&I1, &I01]() -> bool {
2159  // Both of them must be power-of-two, and the constant from setcc is bigger.
2160  return I1.ugt(I01) && I1.isPowerOf2() && I01.isPowerOf2();
2161  };
2162 
2163  if (checkConstants()) {
2164  // Great, e.g. got icmp ult i16 (add i16 %x, 128), 256
2165  } else {
2166  // What if we invert constants? (and the target predicate)
2167  I1.negate();
2168  I01.negate();
2169  NewCond = getSetCCInverse(NewCond, /*isInteger=*/true);
2170  if (!checkConstants())
2171  return SDValue();
2172  // Great, e.g. got icmp uge i16 (add i16 %x, -128), -256
2173  }
2174 
2175  // They are power-of-two, so which bit is set?
2176  const unsigned KeptBits = I1.logBase2();
2177  const unsigned KeptBitsMinusOne = I01.logBase2();
2178 
2179  // Magic!
2180  if (KeptBits != (KeptBitsMinusOne + 1))
2181  return SDValue();
2182  assert(KeptBits > 0 && KeptBits < XVT.getSizeInBits() && "unreachable");
2183 
2184  // We don't want to do this in every single case.
2185  SelectionDAG &DAG = DCI.DAG;
2186  if (!DAG.getTargetLoweringInfo().shouldTransformSignedTruncationCheck(
2187  XVT, KeptBits))
2188  return SDValue();
2189 
2190  const unsigned MaskedBits = XVT.getSizeInBits() - KeptBits;
2191  assert(MaskedBits > 0 && MaskedBits < XVT.getSizeInBits() && "unreachable");
2192 
2193  // Unfold into: ((%x << C) a>> C) cond %x
2194  // Where 'cond' will be either 'eq' or 'ne'.
2195  SDValue ShiftAmt = DAG.getConstant(MaskedBits, DL, XVT);
2196  SDValue T0 = DAG.getNode(ISD::SHL, DL, XVT, X, ShiftAmt);
2197  SDValue T1 = DAG.getNode(ISD::SRA, DL, XVT, T0, ShiftAmt);
2198  SDValue T2 = DAG.getSetCC(DL, SCCVT, T1, X, NewCond);
2199 
2200  return T2;
2201 }
2202 
2203 /// Try to simplify a setcc built with the specified operands and cc. If it is
2204 /// unable to simplify it, return a null SDValue.
2206  ISD::CondCode Cond, bool foldBooleans,
2207  DAGCombinerInfo &DCI,
2208  const SDLoc &dl) const {
2209  SelectionDAG &DAG = DCI.DAG;
2210  EVT OpVT = N0.getValueType();
2211 
2212  // These setcc operations always fold.
2213  switch (Cond) {
2214  default: break;
2215  case ISD::SETFALSE:
2216  case ISD::SETFALSE2: return DAG.getBoolConstant(false, dl, VT, OpVT);
2217  case ISD::SETTRUE:
2218  case ISD::SETTRUE2: return DAG.getBoolConstant(true, dl, VT, OpVT);
2219  }
2220 
2221  // Ensure that the constant occurs on the RHS and fold constant comparisons.
2222  // TODO: Handle non-splat vector constants. All undef causes trouble.
2223  ISD::CondCode SwappedCC = ISD::getSetCCSwappedOperands(Cond);
2224  if (isConstOrConstSplat(N0) &&
2225  (DCI.isBeforeLegalizeOps() ||
2226  isCondCodeLegal(SwappedCC, N0.getSimpleValueType())))
2227  return DAG.getSetCC(dl, VT, N1, N0, SwappedCC);
2228 
2229  if (auto *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
2230  const APInt &C1 = N1C->getAPIntValue();
2231 
2232  // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
2233  // equality comparison, then we're just comparing whether X itself is
2234  // zero.
2235  if (N0.getOpcode() == ISD::SRL && (C1.isNullValue() || C1.isOneValue()) &&
2236  N0.getOperand(0).getOpcode() == ISD::CTLZ &&
2237  N0.getOperand(1).getOpcode() == ISD::Constant) {
2238  const APInt &ShAmt
2239  = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2240  if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2241  ShAmt == Log2_32(N0.getValueSizeInBits())) {
2242  if ((C1 == 0) == (Cond == ISD::SETEQ)) {
2243  // (srl (ctlz x), 5) == 0 -> X != 0
2244  // (srl (ctlz x), 5) != 1 -> X != 0
2245  Cond = ISD::SETNE;
2246  } else {
2247  // (srl (ctlz x), 5) != 0 -> X == 0
2248  // (srl (ctlz x), 5) == 1 -> X == 0
2249  Cond = ISD::SETEQ;
2250  }
2251  SDValue Zero = DAG.getConstant(0, dl, N0.getValueType());
2252  return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
2253  Zero, Cond);
2254  }
2255  }
2256 
2257  SDValue CTPOP = N0;
2258  // Look through truncs that don't change the value of a ctpop.
2259  if (N0.hasOneUse() && N0.getOpcode() == ISD::TRUNCATE)
2260  CTPOP = N0.getOperand(0);
2261 
2262  if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP &&
2263  (N0 == CTPOP ||
2265  EVT CTVT = CTPOP.getValueType();
2266  SDValue CTOp = CTPOP.getOperand(0);
2267 
2268  // (ctpop x) u< 2 -> (x & x-1) == 0
2269  // (ctpop x) u> 1 -> (x & x-1) != 0
2270  if ((Cond == ISD::SETULT && C1 == 2) || (Cond == ISD::SETUGT && C1 == 1)){
2271  SDValue Sub = DAG.getNode(ISD::SUB, dl, CTVT, CTOp,
2272  DAG.getConstant(1, dl, CTVT));
2273  SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Sub);
2275  return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, dl, CTVT), CC);
2276  }
2277 
2278  // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal.
2279  }
2280 
2281  // (zext x) == C --> x == (trunc C)
2282  // (sext x) == C --> x == (trunc C)
2283  if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2284  DCI.isBeforeLegalize() && N0->hasOneUse()) {
2285  unsigned MinBits = N0.getValueSizeInBits();
2286  SDValue PreExt;
2287  bool Signed = false;
2288  if (N0->getOpcode() == ISD::ZERO_EXTEND) {
2289  // ZExt
2290  MinBits = N0->getOperand(0).getValueSizeInBits();
2291  PreExt = N0->getOperand(0);
2292  } else if (N0->getOpcode() == ISD::AND) {
2293  // DAGCombine turns costly ZExts into ANDs
2294  if (auto *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
2295  if ((C->getAPIntValue()+1).isPowerOf2()) {
2296  MinBits = C->getAPIntValue().countTrailingOnes();
2297  PreExt = N0->getOperand(0);
2298  }
2299  } else if (N0->getOpcode() == ISD::SIGN_EXTEND) {
2300  // SExt
2301  MinBits = N0->getOperand(0).getValueSizeInBits();
2302  PreExt = N0->getOperand(0);
2303  Signed = true;
2304  } else if (auto *LN0 = dyn_cast<LoadSDNode>(N0)) {
2305  // ZEXTLOAD / SEXTLOAD
2306  if (LN0->getExtensionType() == ISD::ZEXTLOAD) {
2307  MinBits = LN0->getMemoryVT().getSizeInBits();
2308  PreExt = N0;
2309  } else if (LN0->getExtensionType() == ISD::SEXTLOAD) {
2310  Signed = true;
2311  MinBits = LN0->getMemoryVT().getSizeInBits();
2312  PreExt = N0;
2313  }
2314  }
2315 
2316  // Figure out how many bits we need to preserve this constant.
2317  unsigned ReqdBits = Signed ?
2318  C1.getBitWidth() - C1.getNumSignBits() + 1 :
2319  C1.getActiveBits();
2320 
2321  // Make sure we're not losing bits from the constant.
2322  if (MinBits > 0 &&
2323  MinBits < C1.getBitWidth() &&
2324  MinBits >= ReqdBits) {
2325  EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits);
2326  if (isTypeDesirableForOp(ISD::SETCC, MinVT)) {
2327  // Will get folded away.
2328  SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreExt);
2329  if (MinBits == 1 && C1 == 1)
2330  // Invert the condition.
2331  return DAG.getSetCC(dl, VT, Trunc, DAG.getConstant(0, dl, MVT::i1),
2332  Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2333  SDValue C = DAG.getConstant(C1.trunc(MinBits), dl, MinVT);
2334  return DAG.getSetCC(dl, VT, Trunc, C, Cond);
2335  }
2336 
2337  // If truncating the setcc operands is not desirable, we can still
2338  // simplify the expression in some cases:
2339  // setcc ([sz]ext (setcc x, y, cc)), 0, setne) -> setcc (x, y, cc)
2340  // setcc ([sz]ext (setcc x, y, cc)), 0, seteq) -> setcc (x, y, inv(cc))
2341  // setcc (zext (setcc x, y, cc)), 1, setne) -> setcc (x, y, inv(cc))
2342  // setcc (zext (setcc x, y, cc)), 1, seteq) -> setcc (x, y, cc)
2343  // setcc (sext (setcc x, y, cc)), -1, setne) -> setcc (x, y, inv(cc))
2344  // setcc (sext (setcc x, y, cc)), -1, seteq) -> setcc (x, y, cc)
2345  SDValue TopSetCC = N0->getOperand(0);
2346  unsigned N0Opc = N0->getOpcode();
2347  bool SExt = (N0Opc == ISD::SIGN_EXTEND);
2348  if (TopSetCC.getValueType() == MVT::i1 && VT == MVT::i1 &&
2349  TopSetCC.getOpcode() == ISD::SETCC &&
2350  (N0Opc == ISD::ZERO_EXTEND || N0Opc == ISD::SIGN_EXTEND) &&
2351  (isConstFalseVal(N1C) ||
2352  isExtendedTrueVal(N1C, N0->getValueType(0), SExt))) {
2353 
2354  bool Inverse = (N1C->isNullValue() && Cond == ISD::SETEQ) ||
2355  (!N1C->isNullValue() && Cond == ISD::SETNE);
2356 
2357  if (!Inverse)
2358  return TopSetCC;
2359 
2361  cast<CondCodeSDNode>(TopSetCC.getOperand(2))->get(),
2362  TopSetCC.getOperand(0).getValueType().isInteger());
2363  return DAG.getSetCC(dl, VT, TopSetCC.getOperand(0),
2364  TopSetCC.getOperand(1),
2365  InvCond);
2366  }
2367  }
2368  }
2369 
2370  // If the LHS is '(and load, const)', the RHS is 0, the test is for
2371  // equality or unsigned, and all 1 bits of the const are in the same
2372  // partial word, see if we can shorten the load.
2373  if (DCI.isBeforeLegalize() &&
2374  !ISD::isSignedIntSetCC(Cond) &&
2375  N0.getOpcode() == ISD::AND && C1 == 0 &&
2376  N0.getNode()->hasOneUse() &&
2377  isa<LoadSDNode>(N0.getOperand(0)) &&
2378  N0.getOperand(0).getNode()->hasOneUse() &&
2379  isa<ConstantSDNode>(N0.getOperand(1))) {
2380  LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
2381  APInt bestMask;
2382  unsigned bestWidth = 0, bestOffset = 0;
2383  if (!Lod->isVolatile() && Lod->isUnindexed()) {
2384  unsigned origWidth = N0.getValueSizeInBits();
2385  unsigned maskWidth = origWidth;
2386  // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
2387  // 8 bits, but have to be careful...
2388  if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
2389  origWidth = Lod->getMemoryVT().getSizeInBits();
2390  const APInt &Mask =
2391  cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2392  for (unsigned width = origWidth / 2; width>=8; width /= 2) {
2393  APInt newMask = APInt::getLowBitsSet(maskWidth, width);
2394  for (unsigned offset=0; offset<origWidth/width; offset++) {
2395  if (Mask.isSubsetOf(newMask)) {
2396  if (DAG.getDataLayout().isLittleEndian())
2397  bestOffset = (uint64_t)offset * (width/8);
2398  else
2399  bestOffset = (origWidth/width - offset - 1) * (width/8);
2400  bestMask = Mask.lshr(offset * (width/8) * 8);
2401  bestWidth = width;
2402  break;
2403  }
2404  newMask <<= width;
2405  }
2406  }
2407  }
2408  if (bestWidth) {
2409  EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth);
2410  if (newVT.isRound() &&
2411  shouldReduceLoadWidth(Lod, ISD::NON_EXTLOAD, newVT)) {
2412  EVT PtrType = Lod->getOperand(1).getValueType();
2413  SDValue Ptr = Lod->getBasePtr();
2414  if (bestOffset != 0)
2415  Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
2416  DAG.getConstant(bestOffset, dl, PtrType));
2417  unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
2418  SDValue NewLoad = DAG.getLoad(
2419  newVT, dl, Lod->getChain(), Ptr,
2420  Lod->getPointerInfo().getWithOffset(bestOffset), NewAlign);
2421  return DAG.getSetCC(dl, VT,
2422  DAG.getNode(ISD::AND, dl, newVT, NewLoad,
2423  DAG.getConstant(bestMask.trunc(bestWidth),
2424  dl, newVT)),
2425  DAG.getConstant(0LL, dl, newVT), Cond);
2426  }
2427  }
2428  }
2429 
2430  // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2431  if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2432  unsigned InSize = N0.getOperand(0).getValueSizeInBits();
2433 
2434  // If the comparison constant has bits in the upper part, the
2435  // zero-extended value could never match.
2436  if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
2437  C1.getBitWidth() - InSize))) {
2438  switch (Cond) {
2439  case ISD::SETUGT:
2440  case ISD::SETUGE:
2441  case ISD::SETEQ:
2442  return DAG.getConstant(0, dl, VT);
2443  case ISD::SETULT:
2444  case ISD::SETULE:
2445  case ISD::SETNE:
2446  return DAG.getConstant(1, dl, VT);
2447  case ISD::SETGT:
2448  case ISD::SETGE:
2449  // True if the sign bit of C1 is set.
2450  return DAG.getConstant(C1.isNegative(), dl, VT);
2451  case ISD::SETLT:
2452  case ISD::SETLE:
2453  // True if the sign bit of C1 isn't set.
2454  return DAG.getConstant(C1.isNonNegative(), dl, VT);
2455  default:
2456  break;
2457  }
2458  }
2459 
2460  // Otherwise, we can perform the comparison with the low bits.
2461  switch (Cond) {
2462  case ISD::SETEQ:
2463  case ISD::SETNE:
2464  case ISD::SETUGT:
2465  case ISD::SETUGE:
2466  case ISD::SETULT:
2467  case ISD::SETULE: {
2468  EVT newVT = N0.getOperand(0).getValueType();
2469  if (DCI.isBeforeLegalizeOps() ||
2470  (isOperationLegal(ISD::SETCC, newVT) &&
2471  isCondCodeLegal(Cond, newVT.getSimpleVT()))) {
2472  EVT NewSetCCVT =
2473  getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), newVT);
2474  SDValue NewConst = DAG.getConstant(C1.trunc(InSize), dl, newVT);
2475 
2476  SDValue NewSetCC = DAG.getSetCC(dl, NewSetCCVT, N0.getOperand(0),
2477  NewConst, Cond);
2478  return DAG.getBoolExtOrTrunc(NewSetCC, dl, VT, N0.getValueType());
2479  }
2480  break;
2481  }
2482  default:
2483  break; // todo, be more careful with signed comparisons
2484  }
2485  } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2486  (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2487  EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2488  unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
2489  EVT ExtDstTy = N0.getValueType();
2490  unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
2491 
2492  // If the constant doesn't fit into the number of bits for the source of
2493  // the sign extension, it is impossible for both sides to be equal.
2494  if (C1.getMinSignedBits() > ExtSrcTyBits)
2495  return DAG.getConstant(Cond == ISD::SETNE, dl, VT);
2496 
2497  SDValue ZextOp;
2498  EVT Op0Ty = N0.getOperand(0).getValueType();
2499  if (Op0Ty == ExtSrcTy) {
2500  ZextOp = N0.getOperand(0);
2501  } else {
2502  APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
2503  ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
2504  DAG.getConstant(Imm, dl, Op0Ty));
2505  }
2506  if (!DCI.isCalledByLegalizer())
2507  DCI.AddToWorklist(ZextOp.getNode());
2508  // Otherwise, make this a use of a zext.
2509  return DAG.getSetCC(dl, VT, ZextOp,
2511  ExtDstTyBits,
2512  ExtSrcTyBits),
2513  dl, ExtDstTy),
2514  Cond);
2515  } else if ((N1C->isNullValue() || N1C->isOne()) &&
2516  (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2517  // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
2518  if (N0.getOpcode() == ISD::SETCC &&
2519  isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
2520  bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (!N1C->isOne());
2521  if (TrueWhenTrue)
2522  return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
2523  // Invert the condition.
2524  ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
2525  CC = ISD::getSetCCInverse(CC,
2526  N0.getOperand(0).getValueType().isInteger());
2527  if (DCI.isBeforeLegalizeOps() ||
2528  isCondCodeLegal(CC, N0.getOperand(0).getSimpleValueType()))
2529  return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
2530  }
2531 
2532  if ((N0.getOpcode() == ISD::XOR ||
2533  (N0.getOpcode() == ISD::AND &&
2534  N0.getOperand(0).getOpcode() == ISD::XOR &&
2535  N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2536  isa<ConstantSDNode>(N0.getOperand(1)) &&
2537  cast<ConstantSDNode>(N0.getOperand(1))->isOne()) {
2538  // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
2539  // can only do this if the top bits are known zero.
2540  unsigned BitWidth = N0.getValueSizeInBits();
2541  if (DAG.MaskedValueIsZero(N0,
2542  APInt::getHighBitsSet(BitWidth,
2543  BitWidth-1))) {
2544  // Okay, get the un-inverted input value.
2545  SDValue Val;
2546  if (N0.getOpcode() == ISD::XOR) {
2547  Val = N0.getOperand(0);
2548  } else {
2549  assert(N0.getOpcode() == ISD::AND &&
2550  N0.getOperand(0).getOpcode() == ISD::XOR);
2551  // ((X^1)&1)^1 -> X & 1
2552  Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
2553  N0.getOperand(0).getOperand(0),
2554  N0.getOperand(1));
2555  }
2556 
2557  return DAG.getSetCC(dl, VT, Val, N1,
2558  Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2559  }
2560  } else if (N1C->isOne() &&
2561  (VT == MVT::i1 ||
2562  getBooleanContents(N0->getValueType(0)) ==
2564  SDValue Op0 = N0;
2565  if (Op0.getOpcode() == ISD::TRUNCATE)
2566  Op0 = Op0.getOperand(0);
2567 
2568  if ((Op0.getOpcode() == ISD::XOR) &&
2569  Op0.getOperand(0).getOpcode() == ISD::SETCC &&
2570  Op0.getOperand(1).getOpcode() == ISD::SETCC) {
2571  // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
2572  Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
2573  return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
2574  Cond);
2575  }
2576  if (Op0.getOpcode() == ISD::AND &&
2577  isa<ConstantSDNode>(Op0.getOperand(1)) &&
2578  cast<ConstantSDNode>(Op0.getOperand(1))->isOne()) {
2579  // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
2580  if (Op0.getValueType().bitsGT(VT))
2581  Op0 = DAG.getNode(ISD::AND, dl, VT,
2582  DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
2583  DAG.getConstant(1, dl, VT));
2584  else if (Op0.getValueType().bitsLT(VT))
2585  Op0 = DAG.getNode(ISD::AND, dl, VT,
2586  DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)),
2587  DAG.getConstant(1, dl, VT));
2588 
2589  return DAG.getSetCC(dl, VT, Op0,
2590  DAG.getConstant(0, dl, Op0.getValueType()),
2591  Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2592  }
2593  if (Op0.getOpcode() == ISD::AssertZext &&
2594  cast<VTSDNode>(Op0.getOperand(1))->getVT() == MVT::i1)
2595  return DAG.getSetCC(dl, VT, Op0,
2596  DAG.getConstant(0, dl, Op0.getValueType()),
2597  Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2598  }
2599  }
2600 
2601  if (SDValue V =
2602  optimizeSetCCOfSignedTruncationCheck(VT, N0, N1, Cond, DCI, dl))
2603  return V;
2604  }
2605 
2606  // These simplifications apply to splat vectors as well.
2607  // TODO: Handle more splat vector cases.
2608  if (auto *N1C = isConstOrConstSplat(N1)) {
2609  const APInt &C1 = N1C->getAPIntValue();
2610 
2611  APInt MinVal, MaxVal;
2612  unsigned OperandBitSize = N1C->getValueType(0).getScalarSizeInBits();
2613  if (ISD::isSignedIntSetCC(Cond)) {
2614  MinVal = APInt::getSignedMinValue(OperandBitSize);
2615  MaxVal = APInt::getSignedMaxValue(OperandBitSize);
2616  } else {
2617  MinVal = APInt::getMinValue(OperandBitSize);
2618  MaxVal = APInt::getMaxValue(OperandBitSize);
2619  }
2620 
2621  // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2622  if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2623  // X >= MIN --> true
2624  if (C1 == MinVal)
2625  return DAG.getBoolConstant(true, dl, VT, OpVT);
2626 
2627  if (!VT.isVector()) { // TODO: Support this for vectors.
2628  // X >= C0 --> X > (C0 - 1)
2629  APInt C = C1 - 1;
2630  ISD::CondCode NewCC = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT;
2631  if ((DCI.isBeforeLegalizeOps() ||
2632  isCondCodeLegal(NewCC, VT.getSimpleVT())) &&
2633  (!N1C->isOpaque() || (C.getBitWidth() <= 64 &&
2635  return DAG.getSetCC(dl, VT, N0,
2636  DAG.getConstant(C, dl, N1.getValueType()),
2637  NewCC);
2638  }
2639  }
2640  }
2641 
2642  if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2643  // X <= MAX --> true
2644  if (C1 == MaxVal)
2645  return DAG.getBoolConstant(true, dl, VT, OpVT);
2646 
2647  // X <= C0 --> X < (C0 + 1)
2648  if (!VT.isVector()) { // TODO: Support this for vectors.
2649  APInt C = C1 + 1;
2650  ISD::CondCode NewCC = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT;
2651  if ((DCI.isBeforeLegalizeOps() ||
2652  isCondCodeLegal(NewCC, VT.getSimpleVT())) &&
2653  (!N1C->isOpaque() || (C.getBitWidth() <= 64 &&
2655  return DAG.getSetCC(dl, VT, N0,
2656  DAG.getConstant(C, dl, N1.getValueType()),
2657  NewCC);
2658  }
2659  }
2660  }
2661 
2662  if (Cond == ISD::SETLT || Cond == ISD::SETULT) {
2663  if (C1 == MinVal)
2664  return DAG.getBoolConstant(false, dl, VT, OpVT); // X < MIN --> false
2665 
2666  // TODO: Support this for vectors after legalize ops.
2667  if (!VT.isVector() || DCI.isBeforeLegalizeOps()) {
2668  // Canonicalize setlt X, Max --> setne X, Max
2669  if (C1 == MaxVal)
2670  return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
2671 
2672  // If we have setult X, 1, turn it into seteq X, 0
2673  if (C1 == MinVal+1)
2674  return DAG.getSetCC(dl, VT, N0,
2675  DAG.getConstant(MinVal, dl, N0.getValueType()),
2676  ISD::SETEQ);
2677  }
2678  }
2679 
2680  if (Cond == ISD::SETGT || Cond == ISD::SETUGT) {
2681  if (C1 == MaxVal)
2682  return DAG.getBoolConstant(false, dl, VT, OpVT); // X > MAX --> false
2683 
2684  // TODO: Support this for vectors after legalize ops.
2685  if (!VT.isVector() || DCI.isBeforeLegalizeOps()) {
2686  // Canonicalize setgt X, Min --> setne X, Min
2687  if (C1 == MinVal)
2688  return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
2689 
2690  // If we have setugt X, Max-1, turn it into seteq X, Max
2691  if (C1 == MaxVal-1)
2692  return DAG.getSetCC(dl, VT, N0,
2693  DAG.getConstant(MaxVal, dl, N0.getValueType()),
2694  ISD::SETEQ);
2695  }
2696  }
2697 
2698  // If we have "setcc X, C0", check to see if we can shrink the immediate
2699  // by changing cc.
2700  // TODO: Support this for vectors after legalize ops.
2701  if (!VT.isVector() || DCI.isBeforeLegalizeOps()) {
2702  // SETUGT X, SINTMAX -> SETLT X, 0
2703  if (Cond == ISD::SETUGT &&
2704  C1 == APInt::getSignedMaxValue(OperandBitSize))
2705  return DAG.getSetCC(dl, VT, N0,
2706  DAG.getConstant(0, dl, N1.getValueType()),
2707  ISD::SETLT);
2708 
2709  // SETULT X, SINTMIN -> SETGT X, -1
2710  if (Cond == ISD::SETULT &&
2711  C1 == APInt::getSignedMinValue(OperandBitSize)) {
2712  SDValue ConstMinusOne =
2713  DAG.getConstant(APInt::getAllOnesValue(OperandBitSize), dl,
2714  N1.getValueType());
2715  return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
2716  }
2717  }
2718  }
2719 
2720  // Back to non-vector simplifications.
2721  // TODO: Can we do these for vector splats?
2722  if (auto *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
2723  const APInt &C1 = N1C->getAPIntValue();
2724 
2725  // Fold bit comparisons when we can.
2726  if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2727  (VT == N0.getValueType() ||
2728  (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
2729  N0.getOpcode() == ISD::AND) {
2730  auto &DL = DAG.getDataLayout();
2731  if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2732  EVT ShiftTy = getShiftAmountTy(N0.getValueType(), DL,
2733  !DCI.isBeforeLegalize());
2734  if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2735  // Perform the xform if the AND RHS is a single bit.
2736  if (AndRHS->getAPIntValue().isPowerOf2()) {
2737  return DAG.getNode(ISD::TRUNCATE, dl, VT,
2738  DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
2739  DAG.getConstant(AndRHS->getAPIntValue().logBase2(), dl,
2740  ShiftTy)));
2741  }
2742  } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
2743  // (X & 8) == 8 --> (X & 8) >> 3
2744  // Perform the xform if C1 is a single bit.
2745  if (C1.isPowerOf2()) {
2746  return DAG.getNode(ISD::TRUNCATE, dl, VT,
2747  DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
2748  DAG.getConstant(C1.logBase2(), dl,
2749  ShiftTy)));
2750  }
2751  }
2752  }
2753  }
2754 
2755  if (C1.getMinSignedBits() <= 64 &&
2757  // (X & -256) == 256 -> (X >> 8) == 1
2758  if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2759  N0.getOpcode() == ISD::AND && N0.hasOneUse()) {
2760  if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2761  const APInt &AndRHSC = AndRHS->getAPIntValue();
2762  if ((-AndRHSC).isPowerOf2() && (AndRHSC & C1) == C1) {
2763  unsigned ShiftBits = AndRHSC.countTrailingZeros();
2764  auto &DL = DAG.getDataLayout();
2765  EVT ShiftTy = getShiftAmountTy(N0.getValueType(), DL,
2766  !DCI.isBeforeLegalize());
2767  EVT CmpTy = N0.getValueType();
2768  SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0.getOperand(0),
2769  DAG.getConstant(ShiftBits, dl,
2770  ShiftTy));
2771  SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), dl, CmpTy);
2772  return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond);
2773  }
2774  }
2775  } else if (Cond == ISD::SETULT || Cond == ISD::SETUGE ||
2776  Cond == ISD::SETULE || Cond == ISD::SETUGT) {
2777  bool AdjOne = (Cond == ISD::SETULE || Cond == ISD::SETUGT);
2778  // X < 0x100000000 -> (X >> 32) < 1
2779  // X >= 0x100000000 -> (X >> 32) >= 1
2780  // X <= 0x0ffffffff -> (X >> 32) < 1
2781  // X > 0x0ffffffff -> (X >> 32) >= 1
2782  unsigned ShiftBits;
2783  APInt NewC = C1;
2784  ISD::CondCode NewCond = Cond;
2785  if (AdjOne) {
2786  ShiftBits = C1.countTrailingOnes();
2787  NewC = NewC + 1;
2788  NewCond = (Cond == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
2789  } else {
2790  ShiftBits = C1.countTrailingZeros();
2791  }
2792  NewC.lshrInPlace(ShiftBits);
2793  if (ShiftBits && NewC.getMinSignedBits() <= 64 &&
2795  auto &DL = DAG.getDataLayout();
2796  EVT ShiftTy = getShiftAmountTy(N0.getValueType(), DL,
2797  !DCI.isBeforeLegalize());
2798  EVT CmpTy = N0.getValueType();
2799  SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0,
2800  DAG.getConstant(ShiftBits, dl, ShiftTy));
2801  SDValue CmpRHS = DAG.getConstant(NewC, dl, CmpTy);
2802  return DAG.getSetCC(dl, VT, Shift, CmpRHS, NewCond);
2803  }
2804  }
2805  }
2806  }
2807 
2808  if (isa<ConstantFPSDNode>(N0.getNode())) {
2809  // Constant fold or commute setcc.
2810  SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
2811  if (O.getNode()) return O;
2812  } else if (auto *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
2813  // If the RHS of an FP comparison is a constant, simplify it away in
2814  // some cases.
2815  if (CFP->getValueAPF().isNaN()) {
2816  // If an operand is known to be a nan, we can fold it.
2817  switch (ISD::getUnorderedFlavor(Cond)) {
2818  default: llvm_unreachable("Unknown flavor!");
2819  case 0: // Known false.
2820  return DAG.getBoolConstant(false, dl, VT, OpVT);
2821  case 1: // Known true.
2822  return DAG.getBoolConstant(true, dl, VT, OpVT);
2823  case 2: // Undefined.
2824  return DAG.getUNDEF(VT);
2825  }
2826  }
2827 
2828  // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
2829  // constant if knowing that the operand is non-nan is enough. We prefer to
2830  // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
2831  // materialize 0.0.
2832  if (Cond == ISD::SETO || Cond == ISD::SETUO)
2833  return DAG.getSetCC(dl, VT, N0, N0, Cond);
2834 
2835  // setcc (fneg x), C -> setcc swap(pred) x, -C
2836  if (N0.getOpcode() == ISD::FNEG) {
2838  if (DCI.isBeforeLegalizeOps() ||
2839  isCondCodeLegal(SwapCond, N0.getSimpleValueType())) {
2840  SDValue NegN1 = DAG.getNode(ISD::FNEG, dl, N0.getValueType(), N1);
2841  return DAG.getSetCC(dl, VT, N0.getOperand(0), NegN1, SwapCond);
2842  }
2843  }
2844 
2845  // If the condition is not legal, see if we can find an equivalent one
2846  // which is legal.
2847  if (!isCondCodeLegal(Cond, N0.getSimpleValueType())) {
2848  // If the comparison was an awkward floating-point == or != and one of
2849  // the comparison operands is infinity or negative infinity, convert the
2850  // condition to a less-awkward <= or >=.
2851  if (CFP->getValueAPF().isInfinity()) {
2852  if (CFP->getValueAPF().isNegative()) {
2853  if (Cond == ISD::SETOEQ &&
2855  return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
2856  if (Cond == ISD::SETUEQ &&
2858  return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
2859  if (Cond == ISD::SETUNE &&
2861  return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
2862  if (Cond == ISD::SETONE &&
2864  return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
2865  } else {
2866  if (Cond == ISD::SETOEQ &&
2868  return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
2869  if (Cond == ISD::SETUEQ &&
2871  return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
2872  if (Cond == ISD::SETUNE &&
2874  return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
2875  if (Cond == ISD::SETONE &&
2877  return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
2878  }
2879  }
2880  }
2881  }
2882 
2883  if (N0 == N1) {
2884  // The sext(setcc()) => setcc() optimization relies on the appropriate
2885  // constant being emitted.
2886 
2887  bool EqTrue = ISD::isTrueWhenEqual(Cond);
2888 
2889  // We can always fold X == X for integer setcc's.
2890  if (N0.getValueType().isInteger())
2891  return DAG.getBoolConstant(EqTrue, dl, VT, OpVT);
2892 
2893  unsigned UOF = ISD::getUnorderedFlavor(Cond);
2894  if (UOF == 2) // FP operators that are undefined on NaNs.
2895  return DAG.getBoolConstant(EqTrue, dl, VT, OpVT);
2896  if (UOF == unsigned(EqTrue))
2897  return DAG.getBoolConstant(EqTrue, dl, VT, OpVT);
2898  // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2899  // if it is not already.
2900  ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
2901  if (NewCond != Cond &&
2902  (DCI.isBeforeLegalizeOps() ||
2903  isCondCodeLegal(NewCond, N0.getSimpleValueType())))
2904  return DAG.getSetCC(dl, VT, N0, N1, NewCond);
2905  }
2906 
2907  if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2908  N0.getValueType().isInteger()) {
2909  if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2910  N0.getOpcode() == ISD::XOR) {
2911  // Simplify (X+Y) == (X+Z) --> Y == Z
2912  if (N0.getOpcode() == N1.getOpcode()) {
2913  if (N0.getOperand(0) == N1.getOperand(0))
2914  return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
2915  if (N0.getOperand(1) == N1.getOperand(1))
2916  return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
2917  if (isCommutativeBinOp(N0.getOpcode())) {
2918  // If X op Y == Y op X, try other combinations.
2919  if (N0.getOperand(0) == N1.getOperand(1))
2920  return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
2921  Cond);
2922  if (N0.getOperand(1) == N1.getOperand(0))
2923  return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
2924  Cond);
2925  }
2926  }
2927 
2928  // If RHS is a legal immediate value for a compare instruction, we need
2929  // to be careful about increasing register pressure needlessly.
2930  bool LegalRHSImm = false;
2931 
2932  if (auto *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2933  if (auto *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2934  // Turn (X+C1) == C2 --> X == C2-C1
2935  if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
2936  return DAG.getSetCC(dl, VT, N0.getOperand(0),
2937  DAG.getConstant(RHSC->getAPIntValue()-
2938  LHSR->getAPIntValue(),
2939  dl, N0.getValueType()), Cond);
2940  }
2941 
2942  // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2943  if (N0.getOpcode() == ISD::XOR)
2944  // If we know that all of the inverted bits are zero, don't bother
2945  // performing the inversion.
2946  if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
2947  return
2948  DAG.getSetCC(dl, VT, N0.getOperand(0),
2949  DAG.getConstant(LHSR->getAPIntValue() ^
2950  RHSC->getAPIntValue(),
2951  dl, N0.getValueType()),
2952  Cond);
2953  }
2954 
2955  // Turn (C1-X) == C2 --> X == C1-C2
2956  if (auto *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2957  if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
2958  return
2959  DAG.getSetCC(dl, VT, N0.getOperand(1),
2960  DAG.getConstant(SUBC->getAPIntValue() -
2961  RHSC->getAPIntValue(),
2962  dl, N0.getValueType()),
2963  Cond);
2964  }
2965  }
2966 
2967  // Could RHSC fold directly into a compare?
2968  if (RHSC->getValueType(0).getSizeInBits() <= 64)
2969  LegalRHSImm = isLegalICmpImmediate(RHSC->getSExtValue());
2970  }
2971 
2972  // Simplify (X+Z) == X --> Z == 0
2973  // Don't do this if X is an immediate that can fold into a cmp
2974  // instruction and X+Z has other uses. It could be an induction variable
2975  // chain, and the transform would increase register pressure.
2976  if (!LegalRHSImm || N0.getNode()->hasOneUse()) {
2977  if (N0.getOperand(0) == N1)
2978  return DAG.getSetCC(dl, VT, N0.getOperand(1),
2979  DAG.getConstant(0, dl, N0.getValueType()), Cond);
2980  if (N0.getOperand(1) == N1) {
2981  if (isCommutativeBinOp(N0.getOpcode()))
2982  return DAG.getSetCC(dl, VT, N0.getOperand(0),
2983  DAG.getConstant(0, dl, N0.getValueType()),
2984  Cond);
2985  if (N0.getNode()->hasOneUse()) {
2986  assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2987  auto &DL = DAG.getDataLayout();
2988  // (Z-X) == X --> Z == X<<1
2989  SDValue SH = DAG.getNode(
2990  ISD::SHL, dl, N1.getValueType(), N1,
2991  DAG.getConstant(1, dl,
2992  getShiftAmountTy(N1.getValueType(), DL,
2993  !DCI.isBeforeLegalize())));
2994  if (!DCI.isCalledByLegalizer())
2995  DCI.AddToWorklist(SH.getNode());
2996  return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
2997  }
2998  }
2999  }
3000  }
3001 
3002  if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3003  N1.getOpcode() == ISD::XOR) {
3004  // Simplify X == (X+Z) --> Z == 0
3005  if (N1.getOperand(0) == N0)
3006  return DAG.getSetCC(dl, VT, N1.getOperand(1),
3007  DAG.getConstant(0, dl, N1.getValueType()), Cond);
3008  if (N1.getOperand(1) == N0) {
3009  if (isCommutativeBinOp(N1.getOpcode()))
3010  return DAG.getSetCC(dl, VT, N1.getOperand(0),
3011  DAG.getConstant(0, dl, N1.getValueType()), Cond);
3012  if (N1.getNode()->hasOneUse()) {
3013  assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3014  auto &DL = DAG.getDataLayout();
3015  // X == (Z-X) --> X<<1 == Z
3016  SDValue SH = DAG.getNode(
3017  ISD::SHL, dl, N1.getValueType(), N0,
3018  DAG.getConstant(1, dl, getShiftAmountTy(N0.getValueType(), DL,
3019  !DCI.isBeforeLegalize())));
3020  if (!DCI.isCalledByLegalizer())
3021  DCI.AddToWorklist(SH.getNode());
3022  return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
3023  }
3024  }
3025  }
3026 
3027  if (SDValue V = simplifySetCCWithAnd(VT, N0, N1, Cond, DCI, dl))
3028  return V;
3029  }
3030 
3031  // Fold away ALL boolean setcc's.
3032  SDValue Temp;
3033  if (N0.getValueType().getScalarType() == MVT::i1 && foldBooleans) {
3034  EVT OpVT = N0.getValueType();
3035  switch (Cond) {
3036  default: llvm_unreachable("Unknown integer setcc!");
3037  case ISD::SETEQ: // X == Y -> ~(X^Y)
3038  Temp = DAG.getNode(ISD::XOR, dl, OpVT, N0, N1);
3039  N0 = DAG.getNOT(dl, Temp, OpVT);
3040  if (!DCI.isCalledByLegalizer())
3041  DCI.AddToWorklist(Temp.getNode());
3042  break;
3043  case ISD::SETNE: // X != Y --> (X^Y)
3044  N0 = DAG.getNode(ISD::XOR, dl, OpVT, N0, N1);
3045  break;
3046  case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y
3047  case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y
3048  Temp = DAG.getNOT(dl, N0, OpVT);
3049  N0 = DAG.getNode(ISD::AND, dl, OpVT, N1, Temp);
3050  if (!DCI.isCalledByLegalizer())
3051  DCI.AddToWorklist(Temp.getNode());
3052  break;
3053  case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X
3054  case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X
3055  Temp = DAG.getNOT(dl, N1, OpVT);
3056  N0 = DAG.getNode(ISD::AND, dl, OpVT, N0, Temp);
3057  if (!DCI.isCalledByLegalizer())
3058  DCI.AddToWorklist(Temp.getNode());
3059  break;
3060  case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y
3061  case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y
3062  Temp = DAG.getNOT(dl, N0, OpVT);
3063  N0 = DAG.getNode(ISD::OR, dl, OpVT, N1, Temp);
3064  if (!DCI.isCalledByLegalizer())
3065  DCI.AddToWorklist(Temp.getNode());
3066  break;
3067  case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X
3068  case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X
3069  Temp = DAG.getNOT(dl, N1, OpVT);
3070  N0 = DAG.getNode(ISD::OR, dl, OpVT, N0, Temp);
3071  break;
3072  }
3073  if (VT.getScalarType() != MVT::i1) {
3074  if (!DCI.isCalledByLegalizer())
3075  DCI.AddToWorklist(N0.getNode());
3076  // FIXME: If running after legalize, we probably can't do this.
3078  N0 = DAG.getNode(ExtendCode, dl, VT, N0);
3079  }
3080  return N0;
3081  }
3082 
3083  // Could not fold it.
3084  return SDValue();
3085 }
3086 
3087 /// Returns true (and the GlobalValue and the offset) if the node is a
3088 /// GlobalAddress + offset.
3090  int64_t &Offset) const {
3091 
3092  SDNode *N = unwrapAddress(SDValue(WN, 0)).getNode();
3093 
3094  if (auto *GASD = dyn_cast<GlobalAddressSDNode>(N)) {
3095  GA = GASD->getGlobal();
3096  Offset += GASD->getOffset();
3097  return true;
3098  }
3099 
3100  if (N->getOpcode() == ISD::ADD) {
3101  SDValue N1 = N->getOperand(0);
3102  SDValue N2 = N->getOperand(1);
3103  if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
3104  if (auto *V = dyn_cast<ConstantSDNode>(N2)) {
3105  Offset += V->getSExtValue();
3106  return true;
3107  }
3108  } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
3109  if (auto *V = dyn_cast<ConstantSDNode>(N1)) {
3110  Offset += V->getSExtValue();
3111  return true;
3112  }
3113  }
3114  }
3115 
3116  return false;
3117 }
3118 
3120  DAGCombinerInfo &DCI) const {
3121  // Default implementation: no optimization.
3122  return SDValue();
3123 }
3124 
3125 //===----------------------------------------------------------------------===//
3126 // Inline Assembler Implementation Methods
3127 //===----------------------------------------------------------------------===//
3128 
3131  unsigned S = Constraint.size();
3132 
3133  if (S == 1) {
3134  switch (Constraint[0]) {
3135  default: break;
3136  case 'r': return C_RegisterClass;
3137  case 'm': // memory
3138  case 'o': // offsetable
3139  case 'V': // not offsetable
3140  return C_Memory;
3141  case 'i': // Simple Integer or Relocatable Constant
3142  case 'n': // Simple Integer
3143  case 'E': // Floating Point Constant
3144  case 'F': // Floating Point Constant
3145  case 's': // Relocatable Constant
3146  case 'p': // Address.
3147  case 'X': // Allow ANY value.
3148  case 'I': // Target registers.
3149  case 'J':
3150  case 'K':
3151  case 'L':
3152  case 'M':
3153  case 'N':
3154  case 'O':
3155  case 'P':
3156  case '<':
3157  case '>':
3158  return C_Other;
3159  }
3160  }
3161 
3162  if (S > 1 && Constraint[0] == '{' && Constraint[S-1] == '}') {
3163  if (S == 8 && Constraint.substr(1, 6) == "memory") // "{memory}"
3164  return C_Memory;
3165  return C_Register;
3166  }
3167  return C_Unknown;
3168 }
3169 
3170 /// Try to replace an X constraint, which matches anything, with another that
3171 /// has more specific requirements based on the type of the corresponding
3172 /// operand.
3173 const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
3174  if (ConstraintVT.isInteger())
3175  return "r";
3176  if (ConstraintVT.isFloatingPoint())
3177  return "f"; // works for many targets
3178  return nullptr;
3179 }
3180 
3181 /// Lower the specified operand into the Ops vector.
3182 /// If it is invalid, don't add anything to Ops.
3184  std::string &Constraint,
3185  std::vector<SDValue> &Ops,
3186  SelectionDAG &DAG) const {
3187 
3188  if (Constraint.length() > 1) return;
3189 
3190  char ConstraintLetter = Constraint[0];
3191  switch (ConstraintLetter) {
3192  default: break;
3193  case 'X': // Allows any operand; labels (basic block) use this.
3194  if (Op.getOpcode() == ISD::BasicBlock) {
3195  Ops.push_back(Op);
3196  return;
3197  }
3199  case 'i': // Simple Integer or Relocatable Constant
3200  case 'n': // Simple Integer
3201  case 's': { // Relocatable Constant
3202  // These operands are interested in values of the form (GV+C), where C may
3203  // be folded in as an offset of GV, or it may be explicitly added. Also, it
3204  // is possible and fine if either GV or C are missing.
3207 
3208  // If we have "(add GV, C)", pull out GV/C
3209  if (Op.getOpcode() == ISD::ADD) {
3210  C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
3212  if (!C || !GA) {
3213  C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
3215  }
3216  if (!C || !GA) {
3217  C = nullptr;
3218  GA = nullptr;
3219  }
3220  }
3221 
3222  // If we find a valid operand, map to the TargetXXX version so that the
3223  // value itself doesn't get selected.
3224  if (GA) { // Either &GV or &GV+C
3225  if (ConstraintLetter != 'n') {
3226  int64_t Offs = GA->getOffset();
3227  if (C) Offs += C->getZExtValue();
3228  Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
3229  C ? SDLoc(C) : SDLoc(),
3230  Op.getValueType(), Offs));
3231  }
3232  return;
3233  }
3234  if (C) { // just C, no GV.
3235  // Simple constants are not allowed for 's'.
3236  if (ConstraintLetter != 's') {
3237  // gcc prints these as sign extended. Sign extend value to 64 bits
3238  // now; without this it would get ZExt'd later in
3239  // ScheduleDAGSDNodes::EmitNode, which is very generic.
3240  Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
3241  SDLoc(C), MVT::i64));
3242  }
3243  return;
3244  }
3245  break;
3246  }
3247  }
3248 }
3249 
3250 std::pair<unsigned, const TargetRegisterClass *>
3252  StringRef Constraint,
3253  MVT VT) const {
3254  if (Constraint.empty() || Constraint[0] != '{')
3255  return std::make_pair(0u, static_cast<TargetRegisterClass*>(nullptr));
3256  assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
3257 
3258  // Remove the braces from around the name.
3259  StringRef RegName(Constraint.data()+1, Constraint.size()-2);
3260 
3261  std::pair<unsigned, const TargetRegisterClass*> R =
3262  std::make_pair(0u, static_cast<const TargetRegisterClass*>(nullptr));
3263 
3264  // Figure out which register class contains this reg.
3265  for (const TargetRegisterClass *RC : RI->regclasses()) {
3266  // If none of the value types for this register class are valid, we
3267  // can't use it. For example, 64-bit reg classes on 32-bit targets.
3268  if (!isLegalRC(*RI, *RC))
3269  continue;
3270 
3271  for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
3272  I != E; ++I) {
3273  if (RegName.equals_lower(RI->getRegAsmName(*I))) {
3274  std::pair<unsigned, const TargetRegisterClass*> S =
3275  std::make_pair(*I, RC);
3276 
3277  // If this register class has the requested value type, return it,
3278  // otherwise keep searching and return the first class found
3279  // if no other is found which explicitly has the requested type.
3280  if (RI->isTypeLegalForClass(*RC, VT))
3281  return S;
3282  if (!R.second)
3283  R = S;
3284  }
3285  }
3286  }
3287 
3288  return R;
3289 }
3290 
3291 //===----------------------------------------------------------------------===//
3292 // Constraint Selection.
3293 
3294 /// Return true of this is an input operand that is a matching constraint like
3295 /// "4".
3297  assert(!ConstraintCode.empty() && "No known constraint!");
3298  return isdigit(static_cast<unsigned char>(ConstraintCode[0]));
3299 }
3300 
3301 /// If this is an input matching constraint, this method returns the output
3302 /// operand it matches.
3304  assert(!ConstraintCode.empty() && "No known constraint!");
3305  return atoi(ConstraintCode.c_str());
3306 }
3307 
3308 /// Split up the constraint string from the inline assembly value into the
3309 /// specific constraints and their prefixes, and also tie in the associated
3310 /// operand values.
3311 /// If this returns an empty vector, and if the constraint string itself
3312 /// isn't empty, there was an error parsing.
3315  const TargetRegisterInfo *TRI,
3316  ImmutableCallSite CS) const {
3317  /// Information about all of the constraints.
3318  AsmOperandInfoVector ConstraintOperands;
3319  const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
3320  unsigned maCount = 0; // Largest number of multiple alternative constraints.
3321 
3322  // Do a prepass over the constraints, canonicalizing them, and building up the
3323  // ConstraintOperands list.
3324  unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
3325  unsigned ResNo = 0; // ResNo - The result number of the next output.
3326 
3327  for (InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {
3328  ConstraintOperands.emplace_back(std::move(CI));
3329  AsmOperandInfo &OpInfo = ConstraintOperands.back();
3330 
3331  // Update multiple alternative constraint count.
3332  if (OpInfo.multipleAlternatives.size() > maCount)
3333  maCount = OpInfo.multipleAlternatives.size();
3334 
3335  OpInfo.ConstraintVT = MVT::Other;
3336 
3337  // Compute the value type for each operand.
3338  switch (OpInfo.Type) {
3339  case InlineAsm::isOutput:
3340  // Indirect outputs just consume an argument.
3341  if (OpInfo.isIndirect) {
3342  OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
3343  break;
3344  }
3345 
3346  // The return value of the call is this value. As such, there is no
3347  // corresponding argument.
3348  assert(!CS.getType()->isVoidTy() &&
3349  "Bad inline asm!");
3350  if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
3351  OpInfo.ConstraintVT =
3352  getSimpleValueType(DL, STy->getElementType(ResNo));
3353  } else {
3354  assert(ResNo == 0 && "Asm only has one result!");
3355  OpInfo.ConstraintVT = getSimpleValueType(DL, CS.getType());
3356  }
3357  ++ResNo;
3358  break;
3359  case InlineAsm::isInput:
3360  OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
3361  break;
3362  case InlineAsm::isClobber:
3363  // Nothing to do.
3364  break;
3365  }
3366 
3367  if (OpInfo.CallOperandVal) {
3368  llvm::Type *OpTy = OpInfo.CallOperandVal->getType();
3369  if (OpInfo.isIndirect) {
3370  llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
3371  if (!PtrTy)
3372  report_fatal_error("Indirect operand for inline asm not a pointer!");
3373  OpTy = PtrTy->getElementType();
3374  }
3375 
3376  // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
3377  if (StructType *STy = dyn_cast<StructType>(OpTy))
3378  if (STy->getNumElements() == 1)
3379  OpTy = STy->getElementType(0);
3380 
3381  // If OpTy is not a single value, it may be a struct/union that we
3382  // can tile with integers.
3383  if (!OpTy->isSingleValueType() && OpTy->isSized()) {
3384  unsigned BitSize = DL.getTypeSizeInBits(OpTy);
3385  switch (BitSize) {
3386  default: break;
3387  case 1:
3388  case 8:
3389  case 16:
3390  case 32:
3391  case 64:
3392  case 128:
3393  OpInfo.ConstraintVT =
3394  MVT::getVT(IntegerType::get(OpTy->getContext(), BitSize), true);
3395  break;
3396  }
3397  } else if (PointerType *PT = dyn_cast<PointerType>(OpTy)) {
3398  unsigned PtrSize = DL.getPointerSizeInBits(PT->getAddressSpace());
3399  OpInfo.ConstraintVT = MVT::getIntegerVT(PtrSize);
3400  } else {
3401  OpInfo.ConstraintVT = MVT::getVT(OpTy, true);
3402  }
3403  }
3404  }
3405 
3406  // If we have multiple alternative constraints, select the best alternative.
3407  if (!ConstraintOperands.empty()) {
3408  if (maCount) {
3409  unsigned bestMAIndex = 0;
3410  int bestWeight = -1;
3411  // weight: -1 = invalid match, and 0 = so-so match to 5 = good match.
3412  int weight = -1;
3413  unsigned maIndex;
3414  // Compute the sums of the weights for each alternative, keeping track
3415  // of the best (highest weight) one so far.
3416  for (maIndex = 0; maIndex < maCount; ++maIndex) {
3417  int weightSum = 0;
3418  for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
3419  cIndex != eIndex; ++cIndex) {
3420  AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
3421  if (OpInfo.Type == InlineAsm::isClobber)
3422  continue;
3423 
3424  // If this is an output operand with a matching input operand,
3425  // look up the matching input. If their types mismatch, e.g. one
3426  // is an integer, the other is floating point, or their sizes are
3427  // different, flag it as an maCantMatch.
3428  if (OpInfo.hasMatchingInput()) {
3429  AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
3430  if (OpInfo.ConstraintVT != Input.ConstraintVT) {
3431  if ((OpInfo.ConstraintVT.isInteger() !=
3432  Input.ConstraintVT.isInteger()) ||
3433  (OpInfo.ConstraintVT.getSizeInBits() !=
3434  Input.ConstraintVT.getSizeInBits())) {
3435  weightSum = -1; // Can't match.
3436  break;
3437  }
3438  }
3439  }
3440  weight = getMultipleConstraintMatchWeight(OpInfo, maIndex);
3441  if (weight == -1) {
3442  weightSum = -1;
3443  break;
3444  }
3445  weightSum += weight;
3446  }
3447  // Update best.
3448  if (weightSum > bestWeight) {
3449  bestWeight = weightSum;
3450  bestMAIndex = maIndex;
3451  }
3452  }
3453 
3454  // Now select chosen alternative in each constraint.
3455  for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
3456  cIndex != eIndex; ++cIndex) {
3457  AsmOperandInfo& cInfo = ConstraintOperands[cIndex];
3458  if (cInfo.Type == InlineAsm::isClobber)
3459  continue;
3460  cInfo.selectAlternative(bestMAIndex);
3461  }
3462  }
3463  }
3464 
3465  // Check and hook up tied operands, choose constraint code to use.
3466  for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
3467  cIndex != eIndex; ++cIndex) {
3468  AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
3469 
3470  // If this is an output operand with a matching input operand, look up the
3471  // matching input. If their types mismatch, e.g. one is an integer, the
3472  // other is floating point, or their sizes are different, flag it as an
3473  // error.
3474  if (OpInfo.hasMatchingInput()) {
3475  AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
3476 
3477  if (OpInfo.ConstraintVT != Input.ConstraintVT) {
3478  std::pair<unsigned, const TargetRegisterClass *> MatchRC =
3480  OpInfo.ConstraintVT);
3481  std::pair<unsigned, const TargetRegisterClass *> InputRC =
3483  Input.ConstraintVT);
3484  if ((OpInfo.ConstraintVT.isInteger() !=
3485  Input.ConstraintVT.isInteger()) ||
3486  (MatchRC.second != InputRC.second)) {
3487  report_fatal_error("Unsupported asm: input constraint"
3488  " with a matching output constraint of"
3489  " incompatible type!");
3490  }
3491  }
3492  }
3493  }
3494 
3495  return ConstraintOperands;
3496 }
3497 
3498 /// Return an integer indicating how general CT is.
3500  switch (CT) {
3503  return 0;
3505  return 1;
3507  return 2;
3509  return 3;
3510  }
3511  llvm_unreachable("Invalid constraint type");
3512 }
3513 
3514 /// Examine constraint type and operand type and determine a weight value.
3515 /// This object must already have been set up with the operand type
3516 /// and the current alternative constraint selected.
3519  AsmOperandInfo &info, int maIndex) const {
3521  if (maIndex >= (int)info.multipleAlternatives.size())
3522  rCodes = &info.Codes;
3523  else
3524  rCodes = &info.multipleAlternatives[maIndex].Codes;
3525  ConstraintWeight BestWeight = CW_Invalid;
3526 
3527  // Loop over the options, keeping track of the most general one.
3528  for (unsigned i = 0, e = rCodes->size(); i != e; ++i) {
3529  ConstraintWeight weight =
3530  getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str());
3531  if (weight > BestWeight)
3532  BestWeight = weight;
3533  }
3534 
3535  return BestWeight;
3536 }
3537 
3538 /// Examine constraint type and operand type and determine a weight value.
3539 /// This object must already have been set up with the operand type
3540 /// and the current alternative constraint selected.
3543  AsmOperandInfo &info, const char *constraint) const {
3544  ConstraintWeight weight = CW_Invalid;
3545  Value *CallOperandVal = info.CallOperandVal;
3546  // If we don't have a value, we can't do a match,
3547  // but allow it at the lowest weight.
3548  if (!CallOperandVal)
3549  return CW_Default;
3550  // Look at the constraint type.
3551  switch (*constraint) {
3552  case 'i': // immediate integer.
3553  case 'n': // immediate integer with a known value.
3554  if (isa<ConstantInt>(CallOperandVal))
3555  weight = CW_Constant;
3556  break;
3557  case 's': // non-explicit intregal immediate.
3558  if (isa<GlobalValue>(CallOperandVal))
3559  weight = CW_Constant;
3560  break;
3561  case 'E': // immediate float if host format.
3562  case 'F': // immediate float.
3563  if (isa<ConstantFP>(CallOperandVal))
3564  weight = CW_Constant;
3565  break;
3566  case '<': // memory operand with autodecrement.
3567  case '>': // memory operand with autoincrement.
3568  case 'm': // memory operand.
3569  case 'o': // offsettable memory operand
3570  case 'V': // non-offsettable memory operand
3571  weight = CW_Memory;
3572  break;
3573  case 'r': // general register.
3574  case 'g': // general register, memory operand or immediate integer.
3575  // note: Clang converts "g" to "imr".
3576  if (CallOperandVal->getType()->isIntegerTy())
3577  weight = CW_Register;
3578  break;
3579  case 'X': // any operand.
3580  default:
3581  weight = CW_Default;
3582  break;
3583  }
3584  return weight;
3585 }
3586 
3587 /// If there are multiple different constraints that we could pick for this
3588 /// operand (e.g. "imr") try to pick the 'best' one.
3589 /// This is somewhat tricky: constraints fall into four classes:
3590 /// Other -> immediates and magic values
3591 /// Register -> one specific register
3592 /// RegisterClass -> a group of regs
3593 /// Memory -> memory
3594 /// Ideally, we would pick the most specific constraint possible: if we have
3595 /// something that fits into a register, we would pick it. The problem here
3596 /// is that if we have something that could either be in a register or in
3597 /// memory that use of the register could cause selection of *other*
3598 /// operands to fail: they might only succeed if we pick memory. Because of
3599 /// this the heuristic we use is:
3600 ///
3601 /// 1) If there is an 'other' constraint, and if the operand is valid for
3602 /// that constraint, use it. This makes us take advantage of 'i'
3603 /// constraints when available.
3604 /// 2) Otherwise, pick the most general constraint present. This prefers
3605 /// 'm' over 'r', for example.
3606 ///
3608  const TargetLowering &TLI,
3609  SDValue Op, SelectionDAG *DAG) {
3610  assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
3611  unsigned BestIdx = 0;
3613  int BestGenerality = -1;
3614 
3615  // Loop over the options, keeping track of the most general one.
3616  for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
3618  TLI.getConstraintType(OpInfo.Codes[i]);
3619 
3620  // If this is an 'other' constraint, see if the operand is valid for it.
3621  // For example, on X86 we might have an 'rI' constraint. If the operand
3622  // is an integer in the range [0..31] we want to use I (saving a load
3623  // of a register), otherwise we must use 'r'.
3624  if (CType == TargetLowering::C_Other && Op.getNode()) {
3625  assert(OpInfo.Codes[i].size() == 1 &&
3626  "Unhandled multi-letter 'other' constraint");
3627  std::vector<SDValue> ResultOps;
3628  TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i],
3629  ResultOps, *DAG);
3630  if (!ResultOps.empty()) {
3631  BestType = CType;
3632  BestIdx = i;
3633  break;
3634  }
3635  }
3636 
3637  // Things with matching constraints can only be registers, per gcc
3638  // documentation. This mainly affects "g" constraints.
3639  if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
3640  continue;
3641 
3642  // This constraint letter is more general than the previous one, use it.
3643  int Generality = getConstraintGenerality(CType);
3644  if (Generality > BestGenerality) {
3645  BestType = CType;
3646  BestIdx = i;
3647  BestGenerality = Generality;
3648  }
3649  }
3650 
3651  OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
3652  OpInfo.ConstraintType = BestType;
3653 }
3654 
3655 /// Determines the constraint code and constraint type to use for the specific
3656 /// AsmOperandInfo, setting OpInfo.ConstraintCode and OpInfo.ConstraintType.
3658  SDValue Op,
3659  SelectionDAG *DAG) const {
3660  assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
3661 
3662  // Single-letter constraints ('r') are very common.
3663  if (OpInfo.Codes.size() == 1) {
3664  OpInfo.ConstraintCode = OpInfo.Codes[0];
3666  } else {
3667  ChooseConstraint(OpInfo, *this, Op, DAG);
3668  }
3669 
3670  // 'X' matches anything.
3671  if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
3672  // Labels and constants are handled elsewhere ('X' is the only thing
3673  // that matches labels). For Functions, the type here is the type of
3674  // the result, which is not what we want to look at; leave them alone.
3675  Value *v = OpInfo.CallOperandVal;
3676  if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
3677  OpInfo.CallOperandVal = v;
3678  return;
3679  }
3680 
3681  // Otherwise, try to resolve it to something we know about by looking at
3682  // the actual operand type.
3683  if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
3684  OpInfo.ConstraintCode = Repl;
3686  }
3687  }
3688 }
3689 
3690 /// Given an exact SDIV by a constant, create a multiplication
3691 /// with the multiplicative inverse of the constant.
3693  const SDLoc &dl, SelectionDAG &DAG,
3694  SmallVectorImpl<SDNode *> &Created) {
3695  SDValue Op0 = N->getOperand(0);
3696  SDValue Op1 = N->getOperand(1);
3697  EVT VT = N->getValueType(0);
3698  EVT SVT = VT.getScalarType();
3699  EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
3700  EVT ShSVT = ShVT.getScalarType();
3701 
3702  bool UseSRA = false;
3703  SmallVector<SDValue, 16> Shifts, Factors;
3704 
3705  auto BuildSDIVPattern = [&](ConstantSDNode *C) {
3706  if (C->isNullValue())
3707  return false;
3708  APInt Divisor = C->getAPIntValue();
3709  unsigned Shift = Divisor.countTrailingZeros();
3710  if (Shift) {
3711  Divisor.ashrInPlace(Shift);
3712  UseSRA = true;
3713  }
3714  // Calculate the multiplicative inverse, using Newton's method.
3715  APInt t;
3716  APInt Factor = Divisor;
3717  while ((t = Divisor * Factor) != 1)
3718  Factor *= APInt(Divisor.getBitWidth(), 2) - t;
3719  Shifts.push_back(DAG.getConstant(Shift, dl, ShSVT));
3720  Factors.push_back(DAG.getConstant(Factor, dl, SVT));
3721  return true;
3722  };
3723 
3724  // Collect all magic values from the build vector.
3725  if (!ISD::matchUnaryPredicate(Op1, BuildSDIVPattern))
3726  return SDValue();
3727 
3728  SDValue Shift, Factor;
3729  if (VT.isVector()) {
3730  Shift = DAG.getBuildVector(ShVT, dl, Shifts);
3731  Factor = DAG.getBuildVector(VT, dl, Factors);
3732  } else {
3733  Shift = Shifts[0];
3734  Factor = Factors[0];
3735  }
3736 
3737  SDValue Res = Op0;
3738 
3739  // Shift the value upfront if it is even, so the LSB is one.
3740  if (UseSRA) {
3741  // TODO: For UDIV use SRL instead of SRA.
3742  SDNodeFlags Flags;
3743  Flags.setExact(true);
3744  Res = DAG.getNode(ISD::SRA, dl, VT, Res, Shift, Flags);
3745  Created.push_back(Res.getNode());
3746  }
3747 
3748  return DAG.getNode(ISD::MUL, dl, VT, Res, Factor);
3749 }
3750 
3752  SelectionDAG &DAG,
3753  SmallVectorImpl<SDNode *> &Created) const {
3755  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3756  if (TLI.isIntDivCheap(N->getValueType(0), Attr))
3757  return SDValue(N,0); // Lower SDIV as SDIV
3758  return SDValue();
3759 }
3760 
3761 /// Given an ISD::SDIV node expressing a divide by constant,
3762 /// return a DAG expression to select that will generate the same value by
3763 /// multiplying by a magic number.
3764 /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
3766  bool IsAfterLegalization,
3767  SmallVectorImpl<SDNode *> &Created) const {
3768  SDLoc dl(N);
3769  EVT VT = N->getValueType(0);
3770  EVT SVT = VT.getScalarType();
3771  EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout());
3772  EVT ShSVT = ShVT.getScalarType();
3773  unsigned EltBits = VT.getScalarSizeInBits();
3774 
3775  // Check to see if we can do this.
3776  // FIXME: We should be more aggressive here.
3777  if (!isTypeLegal(VT))
3778  return SDValue();
3779 
3780  // If the sdiv has an 'exact' bit we can use a simpler lowering.
3781  if (N->getFlags().hasExact())
3782  return BuildExactSDIV(*this, N, dl, DAG, Created);
3783 
3784  SmallVector<SDValue, 16> MagicFactors, Factors, Shifts, ShiftMasks;
3785 
3786  auto BuildSDIVPattern = [&](ConstantSDNode *C) {
3787  if (C->isNullValue())
3788  return false;
3789 
3790  const APInt &Divisor = C->getAPIntValue();
3791  APInt::ms magics = Divisor.magic();
3792  int NumeratorFactor = 0;
3793  int ShiftMask = -1;
3794 
3795  if (Divisor.isOneValue() || Divisor.isAllOnesValue()) {
3796  // If d is +1/-1, we just multiply the numerator by +1/-1.
3797  NumeratorFactor = Divisor.getSExtValue();
3798  magics.m = 0;
3799  magics.s = 0;
3800  ShiftMask = 0;
3801  } else if (Divisor.isStrictlyPositive() && magics.m.isNegative()) {
3802  // If d > 0 and m < 0, add the numerator.
3803  NumeratorFactor = 1;
3804  } else if (Divisor.isNegative() && magics.m.isStrictlyPositive()) {
3805  // If d < 0 and m > 0, subtract the numerator.
3806  NumeratorFactor = -1;
3807  }
3808 
3809  MagicFactors.push_back(DAG.getConstant(magics.m, dl, SVT));
3810  Factors.push_back(DAG.getConstant(NumeratorFactor, dl, SVT));
3811  Shifts.push_back(DAG.getConstant(magics.s, dl, ShSVT));
3812  ShiftMasks.push_back(DAG.getConstant(ShiftMask, dl, SVT));
3813  return true;
3814  };
3815 
3816  SDValue N0 = N->getOperand(0);
3817  SDValue N1 = N->getOperand(1);
3818 
3819  // Collect the shifts / magic values from each element.
3820  if (!ISD::matchUnaryPredicate(N1, BuildSDIVPattern))
3821  return SDValue();
3822 
3823  SDValue MagicFactor, Factor, Shift, ShiftMask;
3824  if (VT.isVector()) {
3825  MagicFactor = DAG.getBuildVector(VT, dl, MagicFactors);
3826  Factor = DAG.getBuildVector(VT, dl, Factors);
3827  Shift = DAG.getBuildVector(ShVT, dl, Shifts);
3828  ShiftMask = DAG.getBuildVector(VT, dl, ShiftMasks);
3829  } else {
3830  MagicFactor = MagicFactors[0];
3831  Factor = Factors[0];
3832  Shift = Shifts[0];
3833  ShiftMask = ShiftMasks[0];
3834  }
3835 
3836  // Multiply the numerator (operand 0) by the magic value.
3837  // FIXME: We should support doing a MUL in a wider type.
3838  SDValue Q;
3839  if (IsAfterLegalization ? isOperationLegal(ISD::MULHS, VT)
3841  Q = DAG.getNode(ISD::MULHS, dl, VT, N0, MagicFactor);
3842  else if (IsAfterLegalization ? isOperationLegal(ISD::SMUL_LOHI, VT)
3844  SDValue LoHi =
3845  DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT), N0, MagicFactor);
3846  Q = SDValue(LoHi.getNode(), 1);
3847  } else
3848  return SDValue(); // No mulhs or equivalent.
3849  Created.push_back(Q.getNode());
3850 
3851  // (Optionally) Add/subtract the numerator using Factor.
3852  Factor = DAG.getNode(ISD::MUL, dl, VT, N0, Factor);
3853  Created.push_back(Factor.getNode());
3854  Q = DAG.getNode(ISD::ADD, dl, VT, Q, Factor);
3855  Created.push_back(Q.getNode());
3856 
3857  // Shift right algebraic by shift value.
3858  Q = DAG.getNode(ISD::SRA, dl, VT, Q, Shift);
3859  Created.push_back(Q.getNode());
3860 
3861  // Extract the sign bit, mask it and add it to the quotient.
3862  SDValue SignShift = DAG.getConstant(EltBits - 1, dl, ShVT);
3863  SDValue T = DAG.getNode(ISD::SRL, dl, VT, Q, SignShift);
3864  Created.push_back(T.getNode());
3865  T = DAG.getNode(ISD::AND, dl, VT, T, ShiftMask);
3866  Created.push_back(T.getNode());
3867  return DAG.getNode(ISD::ADD, dl, VT, Q, T);
3868 }
3869 
3870 /// Given an ISD::UDIV node expressing a divide by constant,
3871 /// return a DAG expression to select that will generate the same value by
3872 /// multiplying by a magic number.
3873 /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
3875  bool IsAfterLegalization,
3876  SmallVectorImpl<SDNode *> &Created) const {
3877  SDLoc dl(N);
3878  EVT VT = N->getValueType(0);
3879  EVT SVT = VT.getScalarType();
3880  EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout());
3881  EVT ShSVT = ShVT.getScalarType();
3882  unsigned EltBits = VT.getScalarSizeInBits();
3883 
3884  // Check to see if we can do this.
3885  // FIXME: We should be more aggressive here.
3886  if (!isTypeLegal(VT))
3887  return SDValue();
3888 
3889  bool UseNPQ = false;
3890  SmallVector<SDValue, 16> PreShifts, PostShifts, MagicFactors, NPQFactors;
3891 
3892  auto BuildUDIVPattern = [&](ConstantSDNode *C) {
3893  if (C->isNullValue())
3894  return false;
3895  // FIXME: We should use a narrower constant when the upper
3896  // bits are known to be zero.
3897  APInt Divisor = C->getAPIntValue();
3898  APInt::mu magics = Divisor.magicu();
3899  unsigned PreShift = 0, PostShift = 0;
3900 
3901  // If the divisor is even, we can avoid using the expensive fixup by
3902  // shifting the divided value upfront.
3903  if (magics.a != 0 && !Divisor[0]) {
3904  PreShift = Divisor.countTrailingZeros();
3905  // Get magic number for the shifted divisor.
3906  magics = Divisor.lshr(PreShift).magicu(PreShift);
3907  assert(magics.a == 0 && "Should use cheap fixup now");
3908  }
3909 
3910  APInt Magic = magics.m;
3911 
3912  unsigned SelNPQ;
3913  if (magics.a == 0 || Divisor.isOneValue()) {
3914  assert(magics.s < Divisor.getBitWidth() &&
3915  "We shouldn't generate an undefined shift!");
3916  PostShift = magics.s;
3917  SelNPQ = false;
3918  } else {
3919  PostShift = magics.s - 1;
3920  SelNPQ = true;
3921  }
3922 
3923  PreShifts.push_back(DAG.getConstant(PreShift, dl, ShSVT));
3924  MagicFactors.push_back(DAG.getConstant(Magic, dl, SVT));
3925  NPQFactors.push_back(
3926  DAG.getConstant(SelNPQ ? APInt::getOneBitSet(EltBits, EltBits - 1)
3927  : APInt::getNullValue(EltBits),
3928  dl, SVT));
3929  PostShifts.push_back(DAG.getConstant(PostShift, dl, ShSVT));
3930  UseNPQ |= SelNPQ;
3931  return true;
3932  };
3933 
3934  SDValue N0 = N->getOperand(0);
3935  SDValue N1 = N->getOperand(1);
3936 
3937  // Collect the shifts/magic values from each element.
3938  if (!ISD::matchUnaryPredicate(N1, BuildUDIVPattern))
3939  return SDValue();
3940 
3941  SDValue PreShift, PostShift, MagicFactor, NPQFactor;
3942  if (VT.isVector()) {
3943  PreShift = DAG.getBuildVector(ShVT, dl, PreShifts);
3944  MagicFactor = DAG.getBuildVector(VT, dl, MagicFactors);
3945  NPQFactor = DAG.getBuildVector(VT, dl, NPQFactors);
3946  PostShift = DAG.getBuildVector(ShVT, dl, PostShifts);
3947  } else {
3948  PreShift = PreShifts[0];
3949  MagicFactor = MagicFactors[0];
3950  PostShift = PostShifts[0];
3951  }
3952 
3953  SDValue Q = N0;
3954  Q = DAG.getNode(ISD::SRL, dl, VT, Q, PreShift);
3955  Created.push_back(Q.getNode());
3956 
3957  // FIXME: We should support doing a MUL in a wider type.
3958  auto GetMULHU = [&](SDValue X, SDValue Y) {
3959  if (IsAfterLegalization ? isOperationLegal(ISD::MULHU, VT)
3961  return DAG.getNode(ISD::MULHU, dl, VT, X, Y);
3962  if (IsAfterLegalization ? isOperationLegal(ISD::UMUL_LOHI, VT)
3964  SDValue LoHi =
3965  DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), X, Y);
3966  return SDValue(LoHi.getNode(), 1);
3967  }
3968  return SDValue(); // No mulhu or equivalent
3969  };
3970 
3971  // Multiply the numerator (operand 0) by the magic value.
3972  Q = GetMULHU(Q, MagicFactor);
3973  if (!Q)
3974  return SDValue();
3975 
3976  Created.push_back(Q.getNode());
3977 
3978  if (UseNPQ) {
3979  SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N0, Q);
3980  Created.push_back(NPQ.getNode());
3981 
3982  // For vectors we might have a mix of non-NPQ/NPQ paths, so use
3983  // MULHU to act as a SRL-by-1 for NPQ, else multiply by zero.
3984  if (VT.isVector())
3985  NPQ = GetMULHU(NPQ, NPQFactor);
3986  else
3987  NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ, DAG.getConstant(1, dl, ShVT));
3988 
3989  Created.push_back(NPQ.getNode());
3990 
3991  Q = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
3992  Created.push_back(Q.getNode());
3993  }
3994 
3995  Q = DAG.getNode(ISD::SRL, dl, VT, Q, PostShift);
3996  Created.push_back(Q.getNode());
3997 
3998  SDValue One = DAG.getConstant(1, dl, VT);
3999  SDValue IsOne = DAG.getSetCC(dl, VT, N1, One, ISD::SETEQ);
4000  return DAG.getSelect(dl, VT, IsOne, N0, Q);
4001 }
4002 
4003 bool TargetLowering::
4005  if (!isa<ConstantSDNode>(Op.getOperand(0))) {
4006  DAG.getContext()->emitError("argument to '__builtin_return_address' must "
4007  "be a constant integer");
4008  return true;
4009  }
4010 
4011  return false;
4012 }
4013 
4014 //===----------------------------------------------------------------------===//
4015 // Legalization Utilities
4016 //===----------------------------------------------------------------------===//
4017 
4018 bool TargetLowering::expandMUL_LOHI(unsigned Opcode, EVT VT, SDLoc dl,
4019  SDValue LHS, SDValue RHS,
4020  SmallVectorImpl<SDValue> &Result,
4021  EVT HiLoVT, SelectionDAG &DAG,
4023  SDValue LH, SDValue RL, SDValue RH) const {
4024  assert(Opcode == ISD::MUL || Opcode == ISD::UMUL_LOHI ||
4025  Opcode == ISD::SMUL_LOHI);
4026 
4027  bool HasMULHS = (Kind == MulExpansionKind::Always) ||
4029  bool HasMULHU = (Kind == MulExpansionKind::Always) ||
4031  bool HasSMUL_LOHI = (Kind == MulExpansionKind::Always) ||
4033  bool HasUMUL_LOHI = (Kind == MulExpansionKind::Always) ||
4035 
4036  if (!HasMULHU && !HasMULHS && !HasUMUL_LOHI && !HasSMUL_LOHI)
4037  return false;
4038 
4039  unsigned OuterBitSize = VT.getScalarSizeInBits();
4040  unsigned InnerBitSize = HiLoVT.getScalarSizeInBits();
4041  unsigned LHSSB = DAG.ComputeNumSignBits(LHS);
4042  unsigned RHSSB = DAG.ComputeNumSignBits(RHS);
4043 
4044  // LL, LH, RL, and RH must be either all NULL or all set to a value.
4045  assert((LL.getNode() && LH.getNode() && RL.getNode() && RH.getNode()) ||
4046  (!LL.getNode() && !LH.getNode() && !RL.getNode() && !RH.getNode()));
4047 
4048  SDVTList VTs = DAG.getVTList(HiLoVT, HiLoVT);
4049  auto MakeMUL_LOHI = [&](SDValue L, SDValue R, SDValue &Lo, SDValue &Hi,
4050  bool Signed) -> bool {
4051  if ((Signed && HasSMUL_LOHI) || (!Signed && HasUMUL_LOHI)) {
4052  Lo = DAG.getNode(Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI, dl, VTs, L, R);
4053  Hi = SDValue(Lo.getNode(), 1);
4054  return true;
4055  }
4056  if ((Signed && HasMULHS) || (!Signed && HasMULHU)) {
4057  Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, L, R);
4058  Hi = DAG.getNode(Signed ? ISD::MULHS : ISD::MULHU, dl, HiLoVT, L, R);
4059  return true;
4060  }
4061  return false;
4062  };
4063 
4064  SDValue Lo, Hi;
4065 
4066  if (!LL.getNode() && !RL.getNode() &&
4068  LL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, LHS);
4069  RL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, RHS);
4070  }
4071 
4072  if (!LL.getNode())
4073  return false;
4074 
4075  APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
4076  if (DAG.MaskedValueIsZero(LHS, HighMask) &&
4077  DAG.MaskedValueIsZero(RHS, HighMask)) {
4078  // The inputs are both zero-extended.
4079  if (MakeMUL_LOHI(LL, RL, Lo, Hi, false)) {
4080  Result.push_back(Lo);
4081  Result.push_back(Hi);
4082  if (Opcode != ISD::MUL) {
4083  SDValue Zero = DAG.getConstant(0, dl, HiLoVT);
4084  Result.push_back(Zero);
4085  Result.push_back(Zero);
4086  }
4087  return true;
4088  }
4089  }
4090 
4091  if (!VT.isVector() && Opcode == ISD::MUL && LHSSB > InnerBitSize &&
4092  RHSSB > InnerBitSize) {
4093  // The input values are both sign-extended.
4094  // TODO non-MUL case?
4095  if (MakeMUL_LOHI(LL, RL, Lo, Hi, true)) {
4096  Result.push_back(Lo);
4097  Result.push_back(Hi);
4098  return true;
4099  }
4100  }
4101 
4102  unsigned ShiftAmount = OuterBitSize - InnerBitSize;
4103  EVT ShiftAmountTy = getShiftAmountTy(VT, DAG.getDataLayout());
4104  if (APInt::getMaxValue(ShiftAmountTy.getSizeInBits()).ult(ShiftAmount)) {
4105  // FIXME getShiftAmountTy does not always return a sensible result when VT
4106  // is an illegal type, and so the type may be too small to fit the shift
4107  // amount. Override it with i32. The shift will have to be legalized.
4108  ShiftAmountTy = MVT::i32;
4109  }
4110  SDValue Shift = DAG.getConstant(ShiftAmount, dl, ShiftAmountTy);
4111 
4112  if (!LH.getNode() && !RH.getNode() &&
4115  LH = DAG.getNode(ISD::SRL, dl, VT, LHS, Shift);
4116  LH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, LH);
4117  RH = DAG.getNode(ISD::SRL, dl, VT, RHS, Shift);
4118  RH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, RH);
4119  }
4120 
4121  if (!LH.getNode())
4122  return false;
4123 
4124  if (!MakeMUL_LOHI(LL, RL, Lo, Hi, false))
4125  return false;
4126 
4127  Result.push_back(Lo);
4128 
4129  if (Opcode == ISD::MUL) {
4130  RH = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RH);
4131  LH = DAG.getNode(ISD::MUL, dl, HiLoVT, LH, RL);
4132  Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, RH);
4133  Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, LH);
4134  Result.push_back(Hi);
4135  return true;
4136  }
4137 
4138  // Compute the full width result.
4139  auto Merge = [&](SDValue Lo, SDValue Hi) -> SDValue {
4140  Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
4141  Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Hi);
4142  Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
4143  return DAG.getNode(ISD::OR, dl, VT, Lo, Hi);
4144  };
4145 
4146  SDValue Next = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Hi);
4147  if (!MakeMUL_LOHI(LL, RH, Lo, Hi, false))
4148  return false;
4149 
4150  // This is effectively the add part of a multiply-add of half-sized operands,
4151  // so it cannot overflow.
4152  Next = DAG.getNode(ISD::ADD, dl, VT, Next, Merge(Lo, Hi));
4153 
4154  if (!MakeMUL_LOHI(LH, RL, Lo, Hi, false))
4155  return false;
4156 
4157  SDValue Zero = DAG.getConstant(0, dl, HiLoVT);
4158  EVT BoolType = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
4159 
4160  bool UseGlue = (isOperationLegalOrCustom(ISD::ADDC, VT) &&
4162  if (UseGlue)
4163  Next = DAG.getNode(ISD::ADDC, dl, DAG.getVTList(VT, MVT::Glue), Next,
4164  Merge(Lo, Hi));
4165  else
4166  Next = DAG.getNode(ISD::ADDCARRY, dl, DAG.getVTList(VT, BoolType), Next,
4167  Merge(Lo, Hi), DAG.getConstant(0, dl, BoolType));
4168 
4169  SDValue Carry = Next.getValue(1);
4170  Result.push_back(DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, Next));
4171  Next = DAG.getNode(ISD::SRL, dl, VT, Next, Shift);
4172 
4173  if (!MakeMUL_LOHI(LH, RH, Lo, Hi, Opcode == ISD::SMUL_LOHI))
4174  return false;
4175 
4176  if (UseGlue)
4177  Hi = DAG.getNode(ISD::ADDE, dl, DAG.getVTList(HiLoVT, MVT::Glue), Hi, Zero,
4178  Carry);
4179  else
4180  Hi = DAG.getNode(ISD::ADDCARRY, dl, DAG.getVTList(HiLoVT, BoolType), Hi,
4181  Zero, Carry);
4182 
4183  Next = DAG.getNode(ISD::ADD, dl, VT, Next, Merge(Lo, Hi));
4184 
4185  if (Opcode == ISD::SMUL_LOHI) {
4186  SDValue NextSub = DAG.getNode(ISD::SUB, dl, VT, Next,
4187  DAG.getNode(ISD::ZERO_EXTEND, dl, VT, RL));
4188  Next = DAG.getSelectCC(dl, LH, Zero, NextSub, Next, ISD::SETLT);
4189 
4190  NextSub = DAG.getNode(ISD::SUB, dl, VT, Next,
4191  DAG.getNode(ISD::ZERO_EXTEND, dl, VT, LL));
4192  Next = DAG.getSelectCC(dl, RH, Zero, NextSub, Next, ISD::SETLT);
4193  }
4194 
4195  Result.push_back(DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, Next));
4196  Next = DAG.getNode(ISD::SRL, dl, VT, Next, Shift);
4197  Result.push_back(DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, Next));
4198  return true;
4199 }
4200 
4203  SDValue LL, SDValue LH, SDValue RL,
4204  SDValue RH) const {
4205  SmallVector<SDValue, 2> Result;
4206  bool Ok = expandMUL_LOHI(N->getOpcode(), N->getValueType(0), N,
4207  N->getOperand(0), N->getOperand(1), Result, HiLoVT,
4208  DAG, Kind, LL, LH, RL, RH);
4209  if (Ok) {
4210  assert(Result.size() == 2);
4211  Lo = Result[0];
4212  Hi = Result[1];
4213  }
4214  return Ok;
4215 }
4216 
4218  SelectionDAG &DAG) const {
4219  EVT VT = Node->getValueType(0);
4220 
4221  if (VT.isVector() && (!isOperationLegalOrCustom(ISD::SHL, VT) ||
4225  return false;
4226 
4227  // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
4228  // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
4229  SDValue X = Node->getOperand(0);
4230  SDValue Y = Node->getOperand(1);
4231  SDValue Z = Node->getOperand(2);
4232 
4233  unsigned EltSizeInBits = VT.getScalarSizeInBits();
4234  bool IsFSHL = Node->getOpcode() == ISD::FSHL;
4235  SDLoc DL(SDValue(Node, 0));
4236 
4237  EVT ShVT = Z.getValueType();
4238  SDValue BitWidthC = DAG.getConstant(EltSizeInBits, DL, ShVT);
4239  SDValue Zero = DAG.getConstant(0, DL, ShVT);
4240 
4241  SDValue ShAmt;
4242  if (isPowerOf2_32(EltSizeInBits)) {
4243  SDValue Mask = DAG.getConstant(EltSizeInBits - 1, DL, ShVT);
4244  ShAmt = DAG.getNode(ISD::AND, DL, ShVT, Z, Mask);
4245  } else {
4246  ShAmt = DAG.getNode(ISD::UREM, DL, ShVT, Z, BitWidthC);
4247  }
4248 
4249  SDValue InvShAmt = DAG.getNode(ISD::SUB, DL, ShVT, BitWidthC, ShAmt);
4250  SDValue ShX = DAG.getNode(ISD::SHL, DL, VT, X, IsFSHL ? ShAmt : InvShAmt);
4251  SDValue ShY = DAG.getNode(ISD::SRL, DL, VT, Y, IsFSHL ? InvShAmt : ShAmt);
4252  SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShX, ShY);
4253 
4254  // If (Z % BW == 0), then the opposite direction shift is shift-by-bitwidth,
4255  // and that is undefined. We must compare and select to avoid UB.
4256  EVT CCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ShVT);
4257 
4258  // For fshl, 0-shift returns the 1st arg (X).
4259  // For fshr, 0-shift returns the 2nd arg (Y).
4260  SDValue IsZeroShift = DAG.getSetCC(DL, CCVT, ShAmt, Zero, ISD::SETEQ);
4261  Result = DAG.getSelect(DL, VT, IsZeroShift, IsFSHL ? X : Y, Or);
4262  return true;
4263 }
4264 
4265 // TODO: Merge with expandFunnelShift.
4267  SelectionDAG &DAG) const {
4268  EVT VT = Node->getValueType(0);
4269  unsigned EltSizeInBits = VT.getScalarSizeInBits();
4270  bool IsLeft = Node->getOpcode() == ISD::ROTL;
4271  SDValue Op0 = Node->getOperand(0);
4272  SDValue Op1 = Node->getOperand(1);
4273  SDLoc DL(SDValue(Node, 0));
4274 
4275  EVT ShVT = Op1.getValueType();
4276  SDValue BitWidthC = DAG.getConstant(EltSizeInBits, DL, ShVT);
4277 
4278  // If a rotate in the other direction is legal, use it.
4279  unsigned RevRot = IsLeft ? ISD::ROTR : ISD::ROTL;
4280  if (isOperationLegal(RevRot, VT)) {
4281  SDValue Sub = DAG.getNode(ISD::SUB, DL, ShVT, BitWidthC, Op1);
4282  Result = DAG.getNode(RevRot, DL, VT, Op0, Sub);
4283  return true;
4284  }
4285 
4286  if (VT.isVector() && (!isOperationLegalOrCustom(ISD::SHL, VT) ||
4291  return false;
4292 
4293  // Otherwise,
4294  // (rotl x, c) -> (or (shl x, (and c, w-1)), (srl x, (and w-c, w-1)))
4295  // (rotr x, c) -> (or (srl x, (and c, w-1)), (shl x, (and w-c, w-1)))
4296  //
4297  assert(isPowerOf2_32(EltSizeInBits) && EltSizeInBits > 1 &&
4298  "Expecting the type bitwidth to be a power of 2");
4299  unsigned ShOpc = IsLeft ? ISD::SHL : ISD::SRL;
4300  unsigned HsOpc = IsLeft ? ISD::SRL : ISD::SHL;
4301  SDValue BitWidthMinusOneC = DAG.getConstant(EltSizeInBits - 1, DL, ShVT);
4302  SDValue NegOp1 = DAG.getNode(ISD::SUB, DL, ShVT, BitWidthC, Op1);
4303  SDValue And0 = DAG.getNode(ISD::AND, DL, ShVT, Op1, BitWidthMinusOneC);
4304  SDValue And1 = DAG.getNode(ISD::AND, DL, ShVT, NegOp1, BitWidthMinusOneC);
4305  Result = DAG.getNode(ISD::OR, DL, VT, DAG.getNode(ShOpc, DL, VT, Op0, And0),
4306  DAG.getNode(HsOpc, DL, VT, Op0, And1));
4307  return true;
4308 }
4309 
4311  SelectionDAG &DAG) const {
4312  SDValue Src = Node->getOperand(0);
4313  EVT SrcVT = Src.getValueType();
4314  EVT DstVT = Node->getValueType(0);
4315  SDLoc dl(SDValue(Node, 0));
4316 
4317  // FIXME: Only f32 to i64 conversions are supported.
4318  if (SrcVT != MVT::f32 || DstVT != MVT::i64)
4319  return false;
4320 
4321  // Expand f32 -> i64 conversion
4322  // This algorithm comes from compiler-rt's implementation of fixsfdi:
4323  // https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/fixsfdi.c
4324  unsigned SrcEltBits = SrcVT.getScalarSizeInBits();
4325  EVT IntVT = SrcVT.changeTypeToInteger();
4326  EVT IntShVT = getShiftAmountTy(IntVT, DAG.getDataLayout());
4327 
4328  SDValue ExponentMask = DAG.getConstant(0x7F800000, dl, IntVT);
4329  SDValue ExponentLoBit = DAG.getConstant(23, dl, IntVT);
4330  SDValue Bias = DAG.getConstant(127, dl, IntVT);
4331  SDValue SignMask = DAG.getConstant(APInt::getSignMask(SrcEltBits), dl, IntVT);
4332  SDValue SignLowBit = DAG.getConstant(SrcEltBits - 1, dl, IntVT);
4333  SDValue MantissaMask = DAG.getConstant(0x007FFFFF, dl, IntVT);
4334 
4335  SDValue Bits = DAG.getNode(ISD::BITCAST, dl, IntVT, Src);
4336 
4337  SDValue ExponentBits = DAG.getNode(
4338  ISD::SRL, dl, IntVT, DAG.getNode(ISD::AND, dl, IntVT, Bits, ExponentMask),
4339  DAG.getZExtOrTrunc(ExponentLoBit, dl, IntShVT));
4340  SDValue Exponent = DAG.getNode(ISD::SUB, dl, IntVT, ExponentBits, Bias);
4341 
4342  SDValue Sign = DAG.getNode(ISD::SRA, dl, IntVT,
4343  DAG.getNode(ISD::AND, dl, IntVT, Bits, SignMask),
4344  DAG.getZExtOrTrunc(SignLowBit, dl, IntShVT));
4345  Sign = DAG.getSExtOrTrunc(Sign, dl, DstVT);
4346 
4347  SDValue R = DAG.getNode(ISD::OR, dl, IntVT,
4348  DAG.getNode(ISD::AND, dl, IntVT, Bits, MantissaMask),
4349  DAG.getConstant(0x00800000, dl, IntVT));
4350 
4351  R = DAG.getZExtOrTrunc(R, dl, DstVT);
4352 
4353  R = DAG.getSelectCC(
4354  dl, Exponent, ExponentLoBit,
4355  DAG.getNode(ISD::SHL, dl, DstVT, R,
4356  DAG.getZExtOrTrunc(
4357  DAG.getNode(ISD::SUB, dl, IntVT, Exponent, ExponentLoBit),
4358  dl, IntShVT)),
4359  DAG.getNode(ISD::SRL, dl, DstVT, R,
4360  DAG.getZExtOrTrunc(
4361  DAG.getNode(ISD::SUB, dl, IntVT, ExponentLoBit, Exponent),
4362  dl, IntShVT)),
4363  ISD::SETGT);
4364 
4365  SDValue Ret = DAG.getNode(ISD::SUB, dl, DstVT,
4366  DAG.getNode(ISD::XOR, dl, DstVT, R, Sign), Sign);
4367 
4368  Result = DAG.getSelectCC(dl, Exponent, DAG.getConstant(0, dl, IntVT),
4369  DAG.getConstant(0, dl, DstVT), Ret, ISD::SETLT);
4370  return true;
4371 }
4372 
4374  SelectionDAG &DAG) const {
4375  SDLoc dl(SDValue(Node, 0));
4376  SDValue Src = Node->getOperand(0);
4377 
4378  EVT SrcVT = Src.getValueType();
4379  EVT DstVT = Node->getValueType(0);
4380  EVT SetCCVT =
4381  getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), SrcVT);
4382 
4383  // Only expand vector types if we have the appropriate vector bit operations.
4384  if (DstVT.isVector() && (!isOperationLegalOrCustom(ISD::FP_TO_SINT, DstVT) ||
4386  return false;
4387 
4388  // If the maximum float value is smaller then the signed integer range,
4389  // the destination signmask can't be represented by the float, so we can
4390  // just use FP_TO_SINT directly.
4391  const fltSemantics &APFSem = DAG.EVTToAPFloatSemantics(SrcVT);
4392  APFloat APF(APFSem, APInt::getNullValue(SrcVT.getScalarSizeInBits()));
4393  APInt SignMask = APInt::getSignMask(DstVT.getScalarSizeInBits());
4394  if (APFloat::opOverflow &
4395  APF.convertFromAPInt(SignMask, false, APFloat::rmNearestTiesToEven)) {
4396  Result = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, Src);
4397  return true;
4398  }
4399 
4400  SDValue Cst = DAG.getConstantFP(APF, dl, SrcVT);
4401  SDValue Sel = DAG.getSetCC(dl, SetCCVT, Src, Cst, ISD::SETLT);
4402 
4403  bool Strict = shouldUseStrictFP_TO_INT(SrcVT, DstVT, /*IsSigned*/ false);
4404  if (Strict) {
4405  // Expand based on maximum range of FP_TO_SINT, if the value exceeds the
4406  // signmask then offset (the result of which should be fully representable).
4407  // Sel = Src < 0x8000000000000000
4408  // Val = select Sel, Src, Src - 0x8000000000000000
4409  // Ofs = select Sel, 0, 0x8000000000000000
4410  // Result = fp_to_sint(Val) ^ Ofs
4411 
4412  // TODO: Should any fast-math-flags be set for the FSUB?
4413  SDValue Val = DAG.getSelect(dl, SrcVT, Sel, Src,
4414  DAG.getNode(ISD::FSUB, dl, SrcVT, Src, Cst));
4415  SDValue Ofs = DAG.getSelect(dl, DstVT, Sel, DAG.getConstant(0, dl, DstVT),
4416  DAG.getConstant(SignMask, dl, DstVT));
4417  Result = DAG.getNode(ISD::XOR, dl, DstVT,
4418  DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, Val), Ofs);
4419  } else {
4420  // Expand based on maximum range of FP_TO_SINT:
4421  // True = fp_to_sint(Src)
4422  // False = 0x8000000000000000 + fp_to_sint(Src - 0x8000000000000000)
4423  // Result = select (Src < 0x8000000000000000), True, False
4424 
4425  SDValue True = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, Src);
4426  // TODO: Should any fast-math-flags be set for the FSUB?
4427  SDValue False = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT,
4428  DAG.getNode(ISD::FSUB, dl, SrcVT, Src, Cst));
4429  False = DAG.getNode(ISD::XOR, dl, DstVT, False,
4430  DAG.getConstant(SignMask, dl, DstVT));
4431  Result = DAG.getSelect(dl, DstVT, Sel, True, False);
4432  }
4433  return true;
4434 }
4435 
4437  SelectionDAG &DAG) const {
4438  SDValue Src = Node->getOperand(0);
4439  EVT SrcVT = Src.getValueType();
4440  EVT DstVT = Node->getValueType(0);
4441 
4442  if (SrcVT.getScalarType() != MVT::i64)
4443  return false;
4444 
4445  SDLoc dl(SDValue(Node, 0));
4446  EVT ShiftVT = getShiftAmountTy(SrcVT, DAG.getDataLayout());
4447 
4448  if (DstVT.getScalarType() == MVT::f32) {
4449  // Only expand vector types if we have the appropriate vector bit
4450  // operations.
4451  if (SrcVT.isVector() &&
4452  (!isOperationLegalOrCustom(ISD::SRL, SrcVT) ||
4457  return false;
4458 
4459  // For unsigned conversions, convert them to signed conversions using the
4460  // algorithm from the x86_64 __floatundidf in compiler_rt.
4461  SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Src);
4462 
4463  SDValue ShiftConst = DAG.getConstant(1, dl, ShiftVT);
4464  SDValue Shr = DAG.getNode(ISD::SRL, dl, SrcVT, Src, ShiftConst);
4465  SDValue AndConst = DAG.getConstant(1, dl, SrcVT);
4466  SDValue And = DAG.getNode(ISD::AND, dl, SrcVT, Src, AndConst);
4467  SDValue Or = DAG.getNode(ISD::OR, dl, SrcVT, And, Shr);
4468 
4469  SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Or);
4470  SDValue Slow = DAG.getNode(ISD::FADD, dl, DstVT, SignCvt, SignCvt);
4471 
4472  // TODO: This really should be implemented using a branch rather than a
4473  // select. We happen to get lucky and machinesink does the right
4474  // thing most of the time. This would be a good candidate for a
4475  // pseudo-op, or, even better, for whole-function isel.
4476  EVT SetCCVT =
4477  getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), SrcVT);
4478 
4479  SDValue SignBitTest = DAG.getSetCC(
4480  dl, SetCCVT, Src, DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
4481  Result = DAG.getSelect(dl, DstVT, SignBitTest, Slow, Fast);
4482  return true;
4483  }
4484 
4485  if (DstVT.getScalarType() == MVT::f64) {
4486  // Only expand vector types if we have the appropriate vector bit
4487  // operations.
4488  if (SrcVT.isVector() &&
4489  (!isOperationLegalOrCustom(ISD::SRL, SrcVT) ||
4494  return false;
4495 
4496  // Implementation of unsigned i64 to f64 following the algorithm in
4497  // __floatundidf in compiler_rt. This implementation has the advantage
4498  // of performing rounding correctly, both in the default rounding mode
4499  // and in all alternate rounding modes.
4500  SDValue TwoP52 = DAG.getConstant(UINT64_C(0x4330000000000000), dl, SrcVT);
4501  SDValue TwoP84PlusTwoP52 = DAG.getConstantFP(
4502  BitsToDouble(UINT64_C(0x4530000000100000)), dl, DstVT);
4503  SDValue TwoP84 = DAG.getConstant(UINT64_C(0x4530000000000000), dl, SrcVT);
4504  SDValue LoMask = DAG.getConstant(UINT64_C(0x00000000FFFFFFFF), dl, SrcVT);
4505  SDValue HiShift = DAG.getConstant(32, dl, ShiftVT);
4506 
4507  SDValue Lo = DAG.getNode(ISD::AND, dl, SrcVT, Src, LoMask);
4508  SDValue Hi = DAG.getNode(ISD::SRL, dl, SrcVT, Src, HiShift);
4509  SDValue LoOr = DAG.getNode(ISD::OR, dl, SrcVT, Lo, TwoP52);
4510  SDValue HiOr = DAG.getNode(ISD::OR, dl, SrcVT, Hi, TwoP84);
4511  SDValue LoFlt = DAG.getBitcast(DstVT, LoOr);
4512  SDValue HiFlt = DAG.getBitcast(DstVT, HiOr);
4513  SDValue HiSub = DAG.getNode(ISD::FSUB, dl, DstVT, HiFlt, TwoP84PlusTwoP52);
4514  Result = DAG.getNode(ISD::FADD, dl, DstVT, LoFlt, HiSub);
4515  return true;
4516  }
4517 
4518  return false;
4519 }
4520 
4522  SelectionDAG &DAG) const {
4523  SDLoc dl(Node);
4524  unsigned NewOp = Node->getOpcode() == ISD::FMINNUM ?
4526  EVT VT = Node->getValueType(0);
4527  if (isOperationLegalOrCustom(NewOp, VT)) {
4528  SDValue Quiet0 = Node->getOperand(0);
4529  SDValue Quiet1 = Node->getOperand(1);
4530 
4531  if (!Node->getFlags().hasNoNaNs()) {
4532  // Insert canonicalizes if it's possible we need to quiet to get correct
4533  // sNaN behavior.
4534  if (!DAG.isKnownNeverSNaN(Quiet0)) {
4535  Quiet0 = DAG.getNode(ISD::FCANONICALIZE, dl, VT, Quiet0,
4536  Node->getFlags());
4537  }
4538  if (!DAG.isKnownNeverSNaN(Quiet1)) {
4539  Quiet1 = DAG.getNode(ISD::FCANONICALIZE, dl, VT, Quiet1,
4540  Node->getFlags());
4541  }
4542  }
4543 
4544  return DAG.getNode(NewOp, dl, VT, Quiet0, Quiet1, Node->getFlags());
4545  }
4546 
4547  return SDValue();
4548 }
4549 
4551  SelectionDAG &DAG) const {
4552  SDLoc dl(Node);
4553  EVT VT = Node->getValueType(0);
4554  EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout());
4555  SDValue Op = Node->getOperand(0);
4556  unsigned Len = VT.getScalarSizeInBits();
4557  assert(VT.isInteger() && "CTPOP not implemented for this type.");
4558 
4559  // TODO: Add support for irregular type lengths.
4560  if (!(Len <= 128 && Len % 8 == 0))
4561  return false;
4562 
4563  // Only expand vector types if we have the appropriate vector bit operations.
4564  if (VT.isVector() && (!isOperationLegalOrCustom(ISD::ADD, VT) ||
4567  (Len != 8 && !isOperationLegalOrCustom(ISD::MUL, VT)) ||
4569  return false;
4570 
4571  // This is the "best" algorithm from
4572  // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
4573  SDValue Mask55 =
4574  DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x55)), dl, VT);
4575  SDValue Mask33 =
4576  DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x33)), dl, VT);
4577  SDValue Mask0F =
4578  DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x0F)), dl, VT);
4579  SDValue Mask01 =
4580  DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x01)), dl, VT);
4581 
4582  // v = v - ((v >> 1) & 0x55555555...)
4583  Op = DAG.getNode(ISD::SUB, dl, VT, Op,
4584  DAG.getNode(ISD::AND, dl, VT,
4585  DAG.getNode(ISD::SRL, dl, VT, Op,
4586  DAG.getConstant(1, dl, ShVT)),
4587  Mask55));
4588  // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
4589  Op = DAG.getNode(ISD::ADD, dl, VT, DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
4590  DAG.getNode(ISD::AND, dl, VT,
4591  DAG.getNode(ISD::SRL, dl, VT, Op,
4592  DAG.getConstant(2, dl, ShVT)),
4593  Mask33));
4594  // v = (v + (v >> 4)) & 0x0F0F0F0F...
4595  Op = DAG.getNode(ISD::AND, dl, VT,
4596  DAG.getNode(ISD::ADD, dl, VT, Op,
4597  DAG.getNode(ISD::SRL, dl, VT, Op,
4598  DAG.getConstant(4, dl, ShVT))),
4599  Mask0F);
4600  // v = (v * 0x01010101...) >> (Len - 8)
4601  if (Len > 8)
4602  Op =
4603  DAG.getNode(ISD::SRL, dl, VT, DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
4604  DAG.getConstant(Len - 8, dl, ShVT));
4605 
4606  Result = Op;
4607  return true;
4608 }
4609 
4611  SelectionDAG &DAG) const {
4612  SDLoc dl(Node);
4613  EVT VT = Node->getValueType(0);
4614  EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout());
4615  SDValue Op = Node->getOperand(0);
4616  unsigned NumBitsPerElt = VT.getScalarSizeInBits();
4617 
4618  // If the non-ZERO_UNDEF version is supported we can use that instead.
4619  if (Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF &&
4621  Result = DAG.getNode(ISD::CTLZ, dl, VT, Op);
4622  return true;
4623  }
4624 
4625  // If the ZERO_UNDEF version is supported use that and handle the zero case.
4627  EVT SetCCVT =
4628  getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
4629  SDValue CTLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, VT, Op);
4630  SDValue Zero = DAG.getConstant(0, dl, VT);
4631  SDValue SrcIsZero = DAG.getSetCC(dl, SetCCVT, Op, Zero, ISD::SETEQ);
4632  Result = DAG.getNode(ISD::SELECT, dl, VT, SrcIsZero,
4633  DAG.getConstant(NumBitsPerElt, dl, VT), CTLZ);
4634  return true;
4635  }
4636 
4637  // Only expand vector types if we have the appropriate vector bit operations.
4638  if (VT.isVector() && (!isPowerOf2_32(NumBitsPerElt) ||
4642  return false;
4643 
4644  // for now, we do this:
4645  // x = x | (x >> 1);
4646  // x = x | (x >> 2);
4647  // ...
4648  // x = x | (x >>16);
4649  // x = x | (x >>32); // for 64-bit input
4650  // return popcount(~x);
4651  //
4652  // Ref: "Hacker's Delight" by Henry Warren
4653  for (unsigned i = 0; (1U << i) <= (NumBitsPerElt / 2); ++i) {
4654  SDValue Tmp = DAG.getConstant(1ULL << i, dl, ShVT);
4655  Op = DAG.getNode(ISD::OR, dl, VT, Op,
4656  DAG.getNode(ISD::SRL, dl, VT, Op, Tmp));
4657  }
4658  Op = DAG.getNOT(dl, Op, VT);
4659  Result = DAG.getNode(ISD::CTPOP, dl, VT, Op);
4660  return true;
4661 }
4662 
4664  SelectionDAG &DAG) const {
4665  SDLoc dl(Node);
4666  EVT VT = Node->getValueType(0);
4667  SDValue Op = Node->getOperand(0);
4668  unsigned NumBitsPerElt = VT.getScalarSizeInBits();
4669 
4670  // If the non-ZERO_UNDEF version is supported we can use that instead.
4671  if (Node->getOpcode() == ISD::CTTZ_ZERO_UNDEF &&
4673  Result = DAG.getNode(ISD::CTTZ, dl, VT, Op);
4674  return true;
4675  }
4676 
4677  // If the ZERO_UNDEF version is supported use that and handle the zero case.
4679  EVT SetCCVT =
4680  getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
4681  SDValue CTTZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, VT, Op);
4682  SDValue Zero = DAG.getConstant(0, dl, VT);
4683  SDValue SrcIsZero = DAG.getSetCC(dl, SetCCVT, Op, Zero, ISD::SETEQ);
4684  Result = DAG.getNode(ISD::SELECT, dl, VT, SrcIsZero,
4685  DAG.getConstant(NumBitsPerElt, dl, VT), CTTZ);
4686  return true;
4687  }
4688 
4689  // Only expand vector types if we have the appropriate vector bit operations.
4690  if (VT.isVector() && (!isPowerOf2_32(NumBitsPerElt) ||
4696  return false;
4697 
4698  // for now, we use: { return popcount(~x & (x - 1)); }
4699  // unless the target has ctlz but not ctpop, in which case we use:
4700  // { return 32 - nlz(~x & (x-1)); }
4701  // Ref: "Hacker's Delight" by Henry Warren
4702  SDValue Tmp = DAG.getNode(
4703  ISD::AND, dl, VT, DAG.getNOT(dl, Op, VT),
4704  DAG.getNode(ISD::SUB, dl, VT, Op, DAG.getConstant(1, dl, VT)));
4705 
4706  // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4708  Result =
4709  DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(NumBitsPerElt, dl, VT),
4710  DAG.getNode(ISD::CTLZ, dl, VT, Tmp));
4711  return true;
4712  }
4713 
4714  Result = DAG.getNode(ISD::CTPOP, dl, VT, Tmp);
4715  return true;
4716 }
4717 
4719  SelectionDAG &DAG) const {
4720  SDLoc dl(N);
4721  EVT VT = N->getValueType(0);
4722  EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout());
4723  SDValue Op = N->getOperand(0);
4724 
4725  // Only expand vector types if we have the appropriate vector operations.
4726  if (VT.isVector() && (!isOperationLegalOrCustom(ISD::SRA, VT) ||
4729  return false;
4730 
4731  SDValue Shift =
4732  DAG.getNode(ISD::SRA, dl, VT, Op,
4733  DAG.getConstant(VT.getScalarSizeInBits() - 1, dl, ShVT));
4734  SDValue Add = DAG.getNode(ISD::ADD, dl, VT, Op, Shift);
4735  Result = DAG.getNode(ISD::XOR, dl, VT, Add, Shift);
4736  return true;
4737 }
4738 
4740  SelectionDAG &DAG) const {
4741  SDLoc SL(LD);
4742  SDValue Chain = LD->getChain();
4743  SDValue BasePTR = LD->getBasePtr();
4744  EVT SrcVT = LD->getMemoryVT();
4746 
4747  unsigned NumElem = SrcVT.getVectorNumElements();
4748 
4749  EVT SrcEltVT = SrcVT.getScalarType();
4750  EVT DstEltVT = LD->getValueType(0).getScalarType();
4751 
4752  unsigned Stride = SrcEltVT.getSizeInBits() / 8;
4753  assert(SrcEltVT.isByteSized());
4754 
4756  SmallVector<SDValue, 8> LoadChains;
4757 
4758  for (unsigned Idx = 0; Idx < NumElem; ++Idx) {
4759  SDValue ScalarLoad =
4760  DAG.getExtLoad(ExtType, SL, DstEltVT, Chain, BasePTR,
4761  LD->getPointerInfo().getWithOffset(Idx * Stride),
4762  SrcEltVT, MinAlign(LD->getAlignment(), Idx * Stride),
4763  LD->getMemOperand()->getFlags(), LD->getAAInfo());
4764 
4765  BasePTR = DAG.getObjectPtrOffset(SL, BasePTR, Stride);
4766 
4767  Vals.push_back(ScalarLoad.getValue(0));
4768  LoadChains.push_back(ScalarLoad.getValue(1));
4769  }
4770 
4771  SDValue NewChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoadChains);
4772  SDValue Value = DAG.getBuildVector(LD->getValueType(0), SL, Vals);
4773 
4774  return DAG.getMergeValues({ Value, NewChain }, SL);
4775 }
4776 
4778  SelectionDAG &DAG) const {
4779  SDLoc SL(ST);
4780 
4781  SDValue Chain = ST->getChain();
4782  SDValue BasePtr = ST->getBasePtr();
4783  SDValue Value = ST->getValue();
4784  EVT StVT = ST->getMemoryVT();
4785 
4786  // The type of the data we want to save
4787  EVT RegVT = Value.getValueType();
4788  EVT RegSclVT = RegVT.getScalarType();
4789 
4790  // The type of data as saved in memory.
4791  EVT MemSclVT = StVT.getScalarType();
4792 
4793  EVT IdxVT = getVectorIdxTy(DAG.getDataLayout());
4794  unsigned NumElem = StVT.getVectorNumElements();
4795 
4796  // A vector must always be stored in memory as-is, i.e. without any padding
4797  // between the elements, since various code depend on it, e.g. in the
4798  // handling of a bitcast of a vector type to int, which may be done with a
4799  // vector store followed by an integer load. A vector that does not have
4800  // elements that are byte-sized must therefore be stored as an integer
4801  // built out of the extracted vector elements.
4802  if (!MemSclVT.isByteSized()) {
4803  unsigned NumBits = StVT.getSizeInBits();
4804  EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), NumBits);
4805 
4806  SDValue CurrVal = DAG.getConstant(0, SL, IntVT);
4807 
4808  for (unsigned Idx = 0; Idx < NumElem; ++Idx) {
4809  SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, RegSclVT, Value,
4810  DAG.getConstant(Idx, SL, IdxVT));
4811  SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, MemSclVT, Elt);
4812  SDValue ExtElt = DAG.getNode(ISD::ZERO_EXTEND, SL, IntVT, Trunc);
4813  unsigned ShiftIntoIdx =
4814  (DAG.getDataLayout().isBigEndian() ? (NumElem - 1) - Idx : Idx);
4815  SDValue ShiftAmount =
4816  DAG.getConstant(ShiftIntoIdx * MemSclVT.getSizeInBits(), SL, IntVT);
4817  SDValue ShiftedElt =
4818  DAG.getNode(ISD::SHL, SL, IntVT, ExtElt, ShiftAmount);
4819  CurrVal = DAG.getNode(ISD::OR, SL, IntVT, CurrVal, ShiftedElt);
4820  }
4821 
4822  return DAG.getStore(Chain, SL, CurrVal, BasePtr, ST->getPointerInfo(),
4823  ST->getAlignment(), ST->getMemOperand()->getFlags(),
4824  ST->getAAInfo());
4825  }
4826 
4827  // Store Stride in bytes
4828  unsigned Stride = MemSclVT.getSizeInBits() / 8;
4829  assert (Stride && "Zero stride!");
4830  // Extract each of the elements from the original vector and save them into
4831  // memory individually.
4832  SmallVector<SDValue, 8> Stores;
4833  for (unsigned Idx = 0; Idx < NumElem; ++Idx) {
4834  SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, RegSclVT, Value,
4835  DAG.getConstant(Idx, SL, IdxVT));
4836 
4837  SDValue Ptr = DAG.getObjectPtrOffset(SL, BasePtr, Idx * Stride);
4838 
4839  // This scalar TruncStore may be illegal, but we legalize it later.
4840  SDValue Store = DAG.getTruncStore(
4841  Chain, SL, Elt, Ptr, ST->getPointerInfo().getWithOffset(Idx * Stride),
4842  MemSclVT, MinAlign(ST->getAlignment(), Idx * Stride),
4843  ST->getMemOperand()->getFlags(), ST->getAAInfo());
4844 
4845  Stores.push_back(Store);
4846  }
4847 
4848  return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, Stores);
4849 }
4850 
4851 std::pair<SDValue, SDValue>
4854  "unaligned indexed loads not implemented!");
4855  SDValue Chain = LD->getChain();
4856  SDValue Ptr = LD->getBasePtr();
4857  EVT VT = LD->getValueType(0);
4858  EVT LoadedVT = LD->getMemoryVT();
4859  SDLoc dl(LD);
4860  auto &MF = DAG.getMachineFunction();
4861 
4862  if (VT.isFloatingPoint() || VT.isVector()) {
4863  EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
4864  if (isTypeLegal(intVT) && isTypeLegal(LoadedVT)) {
4865  if (!isOperationLegalOrCustom(ISD::LOAD, intVT) &&
4866  LoadedVT.isVector()) {
4867  // Scalarize the load and let the individual components be handled.
4868  SDValue Scalarized = scalarizeVectorLoad(LD, DAG);
4869  if (Scalarized->getOpcode() == ISD::MERGE_VALUES)
4870  return std::make_pair(Scalarized.getOperand(0), Scalarized.getOperand(1));
4871  return std::make_pair(Scalarized.getValue(0), Scalarized.getValue(1));
4872  }
4873 
4874  // Expand to a (misaligned) integer load of the same size,
4875  // then bitconvert to floating point or vector.
4876  SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr,
4877  LD->getMemOperand());
4878  SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
4879  if (LoadedVT != VT)
4880  Result = DAG.getNode(VT.isFloatingPoint() ? ISD::FP_EXTEND :
4881  ISD::ANY_EXTEND, dl, VT, Result);
4882 
4883  return std::make_pair(Result, newLoad.getValue(1));
4884  }
4885 
4886  // Copy the value to a (aligned) stack slot using (unaligned) integer
4887  // loads and stores, then do a (aligned) load from the stack slot.
4888  MVT RegVT = getRegisterType(*DAG.getContext(), intVT);
4889  unsigned LoadedBytes = LoadedVT.getStoreSize();
4890  unsigned RegBytes = RegVT.getSizeInBits() / 8;
4891  unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
4892 
4893  // Make sure the stack slot is also aligned for the register type.
4894  SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
4895  auto FrameIndex = cast<FrameIndexSDNode>(StackBase.getNode())->getIndex();
4896  SmallVector<SDValue, 8> Stores;
4897  SDValue StackPtr = StackBase;
4898  unsigned Offset = 0;
4899 
4900  EVT PtrVT = Ptr.getValueType();
4901  EVT StackPtrVT = StackPtr.getValueType();
4902 
4903  SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT);
4904  SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT);
4905 
4906  // Do all but one copies using the full register width.
4907  for (unsigned i = 1; i < NumRegs; i++) {
4908  // Load one integer register's worth from the original location.
4909  SDValue Load = DAG.getLoad(
4910  RegVT, dl, Chain, Ptr, LD->getPointerInfo().getWithOffset(Offset),
4912  LD->getAAInfo());
4913  // Follow the load with a store to the stack slot. Remember the store.
4914  Stores.push_back(DAG.getStore(
4915  Load.getValue(1), dl, Load, StackPtr,
4917  // Increment the pointers.
4918  Offset += RegBytes;
4919 
4920  Ptr = DAG.getObjectPtrOffset(dl, Ptr, PtrIncrement);
4921  StackPtr = DAG.getObjectPtrOffset(dl, StackPtr, StackPtrIncrement);
4922  }
4923 
4924  // The last copy may be partial. Do an extending load.
4925  EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
4926  8 * (LoadedBytes - Offset));
4927  SDValue Load =
4928  DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
4929  LD->getPointerInfo().getWithOffset(Offset), MemVT,
4930  MinAlign(LD->getAlignment(), Offset),
4931  LD->getMemOperand()->getFlags(), LD->getAAInfo());
4932  // Follow the load with a store to the stack slot. Remember the store.
4933  // On big-endian machines this requires a truncating store to ensure
4934  // that the bits end up in the right place.
4935  Stores.push_back(DAG.getTruncStore(
4936  Load.getValue(1), dl, Load, StackPtr,
4937  MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset), MemVT));
4938 
4939  // The order of the stores doesn't matter - say it with a TokenFactor.
4940  SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
4941 
4942  // Finally, perform the original load only redirected to the stack slot.
4943  Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
4945  LoadedVT);
4946 
4947  // Callers expect a MERGE_VALUES node.
4948  return std::make_pair(Load, TF);
4949  }
4950 
4951  assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
4952  "Unaligned load of unsupported type.");
4953 
4954  // Compute the new VT that is half the size of the old one. This is an
4955  // integer MVT.
4956  unsigned NumBits = LoadedVT.getSizeInBits();
4957  EVT NewLoadedVT;
4958  NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
4959  NumBits >>= 1;
4960 
4961  unsigned Alignment = LD->getAlignment();
4962  unsigned IncrementSize = NumBits / 8;
4963  ISD::LoadExtType HiExtType = LD->getExtensionType();
4964 
4965  // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
4966  if (HiExtType == ISD::NON_EXTLOAD)
4967  HiExtType = ISD::ZEXTLOAD;
4968 
4969  // Load the value in two parts
4970  SDValue Lo, Hi;
4971  if (DAG.getDataLayout().isLittleEndian()) {
4972  Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
4973  NewLoadedVT, Alignment, LD->getMemOperand()->getFlags(),
4974  LD->getAAInfo());
4975 
4976  Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
4977  Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
4978  LD->getPointerInfo().getWithOffset(IncrementSize),
4979  NewLoadedVT, MinAlign(Alignment, IncrementSize),
4980  LD->getMemOperand()->getFlags(), LD->getAAInfo());
4981  } else {
4982  Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
4983  NewLoadedVT, Alignment, LD->getMemOperand()->getFlags(),
4984  LD->getAAInfo());
4985 
4986  Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
4987  Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
4988  LD->getPointerInfo().getWithOffset(IncrementSize),
4989  NewLoadedVT, MinAlign(Alignment, IncrementSize),
4990  LD->getMemOperand()->getFlags(), LD->getAAInfo());
4991  }
4992 
4993  // aggregate the two parts
4994  SDValue ShiftAmount =
4995  DAG.getConstant(NumBits, dl, getShiftAmountTy(Hi.getValueType(),
4996  DAG.getDataLayout()));
4997  SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
4998  Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
4999 
5000  SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
5001  Hi.getValue(1));
5002 
5003  return std::make_pair(Result, TF);
5004 }
5005 
5007  SelectionDAG &DAG) const {
5009  "unaligned indexed stores not implemented!");
5010  SDValue Chain = ST->getChain();
5011  SDValue Ptr = ST->getBasePtr();
5012  SDValue Val = ST->getValue();
5013  EVT VT = Val.getValueType();
5014  int Alignment = ST->getAlignment();
5015  auto &MF = DAG.getMachineFunction();
5016  EVT MemVT = ST->getMemoryVT();
5017 
5018  SDLoc dl(ST);
5019  if (MemVT.isFloatingPoint() || MemVT.isVector()) {
5020  EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
5021  if (isTypeLegal(intVT)) {
5022  if (!isOperationLegalOrCustom(ISD::STORE, intVT) &&
5023  MemVT.isVector()) {
5024  // Scalarize the store and let the individual components be handled.
5025  SDValue Result = scalarizeVectorStore(ST, DAG);
5026 
5027  return Result;
5028  }
5029  // Expand to a bitconvert of the value to the integer type of the
5030  // same size, then a (misaligned) int store.
5031  // FIXME: Does not handle truncating floating point stores!
5032  SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
5033  Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
5034  Alignment, ST->getMemOperand()->getFlags());
5035  return Result;
5036  }
5037  // Do a (aligned) store to a stack slot, then copy from the stack slot
5038  // to the final destination using (unaligned) integer loads and stores.
5039  EVT StoredVT = ST->getMemoryVT();
5040  MVT RegVT =
5041  getRegisterType(*DAG.getContext(),
5043  StoredVT.getSizeInBits()));
5044  EVT PtrVT = Ptr.getValueType();
5045  unsigned StoredBytes = StoredVT.getStoreSize();
5046  unsigned RegBytes = RegVT.getSizeInBits() / 8;
5047  unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
5048 
5049  // Make sure the stack slot is also aligned for the register type.
5050  SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
5051  auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
5052 
5053  // Perform the original store, only redirected to the stack slot.
5054  SDValue Store = DAG.getTruncStore(
5055  Chain, dl, Val, StackPtr,
5056  MachinePointerInfo::getFixedStack(MF, FrameIndex, 0), StoredVT);
5057 
5058  EVT StackPtrVT = StackPtr.getValueType();
5059 
5060  SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT);
5061  SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT);
5062  SmallVector<SDValue, 8> Stores;
5063  unsigned Offset = 0;
5064 
5065  // Do all but one copies using the full register width.
5066  for (unsigned i = 1; i < NumRegs; i++) {
5067  // Load one integer register's worth from the stack slot.
5068  SDValue Load = DAG.getLoad(
5069  RegVT, dl, Store, StackPtr,
5071  // Store it to the final location. Remember the store.
5072  Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
5073  ST->getPointerInfo().getWithOffset(Offset),
5074  MinAlign(ST->getAlignment(), Offset),
5075  ST->getMemOperand()->getFlags()));
5076  // Increment the pointers.
5077  Offset += RegBytes;
5078  StackPtr = DAG.getObjectPtrOffset(dl, StackPtr, StackPtrIncrement);
5079  Ptr = DAG.getObjectPtrOffset(dl, Ptr, PtrIncrement);
5080  }
5081 
5082  // The last store may be partial. Do a truncating store. On big-endian
5083  // machines this requires an extending load from the stack slot to ensure
5084  // that the bits are in the right place.
5085  EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
5086  8 * (StoredBytes - Offset));
5087 
5088  // Load from the stack slot.
5089  SDValue Load = DAG.getExtLoad(
5090  ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
5091  MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset), MemVT);
5092 
5093  Stores.push_back(
5094  DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
5095  ST->getPointerInfo().getWithOffset(Offset), MemVT,
5096  MinAlign(ST->getAlignment(), Offset),
5097  ST->getMemOperand()->getFlags(), ST->getAAInfo()));
5098  // The order of the stores doesn't matter - say it with a TokenFactor.
5099  SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
5100  return Result;
5101  }
5102 
5103  assert(ST->getMemoryVT().isInteger() &&
5104  !ST->getMemoryVT().isVector() &&
5105  "Unaligned store of unknown type.");
5106  // Get the half-size VT
5107  EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
5108  int NumBits = NewStoredVT.getSizeInBits();
5109  int IncrementSize = NumBits / 8;
5110 
5111  // Divide the stored value in two parts.
5112  SDValue ShiftAmount =
5113  DAG.getConstant(NumBits, dl, getShiftAmountTy(Val.getValueType(),
5114  DAG.getDataLayout()));
5115  SDValue Lo = Val;
5116  SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
5117 
5118  // Store the two parts
5119  SDValue Store1, Store2;
5120  Store1 = DAG.getTruncStore(Chain, dl,
5121  DAG.getDataLayout().isLittleEndian() ? Lo : Hi,
5122  Ptr, ST->getPointerInfo(), NewStoredVT, Alignment,
5123  ST->getMemOperand()->getFlags());
5124 
5125  Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
5126  Alignment = MinAlign(Alignment, IncrementSize);
5127  Store2 = DAG.getTruncStore(
5128  Chain, dl, DAG.getDataLayout().isLittleEndian() ? Hi : Lo, Ptr,
5129  ST->getPointerInfo().getWithOffset(IncrementSize), NewStoredVT, Alignment,
5130  ST->getMemOperand()->getFlags(), ST->getAAInfo());
5131 
5132  SDValue Result =
5133  DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
5134  return Result;
5135 }
5136 
5137 SDValue
5139  const SDLoc &DL, EVT DataVT,
5140  SelectionDAG &DAG,
5141  bool IsCompressedMemory) const {
5142  SDValue Increment;
5143  EVT AddrVT = Addr.getValueType();
5144  EVT MaskVT = Mask.getValueType();
5145  assert(DataVT.getVectorNumElements() == MaskVT.getVectorNumElements() &&
5146  "Incompatible types of Data and Mask");
5147  if (IsCompressedMemory) {
5148  // Incrementing the pointer according to number of '1's in the mask.
5149  EVT MaskIntVT = EVT::getIntegerVT(*DAG.getContext(), MaskVT.getSizeInBits());
5150  SDValue MaskInIntReg = DAG.getBitcast(MaskIntVT, Mask);
5151  if (MaskIntVT.getSizeInBits() < 32) {
5152  MaskInIntReg = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, MaskInIntReg);
5153  MaskIntVT = MVT::i32;
5154  }
5155 
5156  // Count '1's with POPCNT.
5157  Increment = DAG.getNode(ISD::CTPOP, DL, MaskIntVT, MaskInIntReg);
5158  Increment = DAG.getZExtOrTrunc(Increment, DL, AddrVT);
5159  // Scale is an element size in bytes.
5160  SDValue Scale = DAG.getConstant(DataVT.getScalarSizeInBits() / 8, DL,
5161  AddrVT);
5162  Increment = DAG.getNode(ISD::MUL, DL, AddrVT, Increment, Scale);
5163  } else
5164  Increment = DAG.getConstant(DataVT.getStoreSize(), DL, AddrVT);
5165 
5166  return DAG.getNode(ISD::ADD, DL, AddrVT, Addr, Increment);
5167 }
5168 
5170  SDValue Idx,
5171  EVT VecVT,
5172  const SDLoc &dl) {
5173  if (isa<ConstantSDNode>(Idx))
5174  return Idx;
5175 
5176  EVT IdxVT = Idx.getValueType();
5177  unsigned NElts = VecVT.getVectorNumElements();
5178  if (isPowerOf2_32(NElts)) {
5180  Log2_32(NElts));
5181  return DAG.getNode(ISD::AND, dl, IdxVT, Idx,
5182  DAG.getConstant(Imm, dl, IdxVT));
5183  }
5184 
5185  return DAG.getNode(ISD::UMIN, dl, IdxVT, Idx,
5186  DAG.getConstant(NElts - 1, dl, IdxVT));
5187 }
5188 
5190  SDValue VecPtr, EVT VecVT,
5191  SDValue Index) const {
5192  SDLoc dl(Index);
5193  // Make sure the index type is big enough to compute in.
5194  Index = DAG.getZExtOrTrunc(Index, dl, VecPtr.getValueType());
5195 
5196  EVT EltVT = VecVT.getVectorElementType();
5197 
5198  // Calculate the element offset and add it to the pointer.
5199  unsigned EltSize = EltVT.getSizeInBits() / 8; // FIXME: should be ABI size.
5200  assert(EltSize * 8 == EltVT.getSizeInBits() &&
5201  "Converting bits to bytes lost precision");
5202 
5203  Index = clampDynamicVectorIndex(DAG, Index, VecVT, dl);
5204 
5205  EVT IdxVT = Index.getValueType();
5206 
5207  Index = DAG.getNode(ISD::MUL, dl, IdxVT, Index,
5208  DAG.getConstant(EltSize, dl, IdxVT));
5209  return DAG.getNode(ISD::ADD, dl, IdxVT, VecPtr, Index);
5210 }
5211 
5212 //===----------------------------------------------------------------------===//
5213 // Implementation of Emulated TLS Model
5214 //===----------------------------------------------------------------------===//
5215 
5217  SelectionDAG &DAG) const {
5218  // Access to address of TLS varialbe xyz is lowered to a function call:
5219  // __emutls_get_address( address of global variable named "__emutls_v.xyz" )
5220  EVT PtrVT = getPointerTy(DAG.getDataLayout());
5221  PointerType *VoidPtrType = Type::getInt8PtrTy(*DAG.getContext());
5222  SDLoc dl(GA);
5223 
5224  ArgListTy Args;
5225  ArgListEntry Entry;
5226  std::string NameString = ("__emutls_v." + GA->getGlobal()->getName()).str();
5227  Module *VariableModule = const_cast<Module*>(GA->getGlobal()->getParent());
5228  StringRef EmuTlsVarName(NameString);
5229  GlobalVariable *EmuTlsVar = VariableModule->getNamedGlobal(EmuTlsVarName);
5230  assert(EmuTlsVar && "Cannot find EmuTlsVar ");
5231  Entry.Node = DAG.getGlobalAddress(EmuTlsVar, dl, PtrVT);
5232  Entry.Ty = VoidPtrType;
5233  Args.push_back(Entry);
5234 
5235  SDValue EmuTlsGetAddr = DAG.getExternalSymbol("__emutls_get_address", PtrVT);
5236 
5238  CLI.setDebugLoc(dl).setChain(DAG.getEntryNode());
5239  CLI.setLibCallee(CallingConv::C, VoidPtrType, EmuTlsGetAddr, std::move(Args));
5240  std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
5241 
5242  // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5243  // At last for X86 targets, maybe good for other targets too?
5245  MFI.setAdjustsStack(true); // Is this only for X86 target?
5246  MFI.setHasCalls(true);
5247 
5248  assert((GA->getOffset() == 0) &&
5249  "Emulated TLS must have zero offset in GlobalAddressSDNode");
5250  return CallResult.first;
5251 }
5252 
5254  SelectionDAG &DAG) const {
5255  assert((Op->getOpcode() == ISD::SETCC) && "Input has to be a SETCC node.");
5256  if (!isCtlzFast())
5257  return SDValue();
5258  ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5259  SDLoc dl(Op);
5260  if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
5261  if (C->isNullValue() && CC == ISD::SETEQ) {
5262  EVT VT = Op.getOperand(0).getValueType();
5263  SDValue Zext = Op.getOperand(0);
5264  if (VT.bitsLT(MVT::i32)) {
5265  VT = MVT::i32;
5266  Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
5267  }
5268  unsigned Log2b = Log2_32(VT.getSizeInBits());
5269  SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
5270  SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
5271  DAG.getConstant(Log2b, dl, MVT::i32));
5272  return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
5273  }
5274  }
5275  return SDValue();
5276 }
5277 
5279  unsigned Opcode = Node->getOpcode();
5280  SDValue LHS = Node->getOperand(0);
5281  SDValue RHS = Node->getOperand(1);
5282  EVT VT = LHS.getValueType();
5283  SDLoc dl(Node);
5284 
5285  // usub.sat(a, b) -> umax(a, b) - b
5286  if (Opcode == ISD::USUBSAT && isOperationLegalOrCustom(ISD::UMAX, VT)) {
5287  SDValue Max = DAG.getNode(ISD::UMAX, dl, VT, LHS, RHS);
5288  return DAG.getNode(ISD::SUB, dl, VT, Max, RHS);
5289  }
5290 
5291  if (VT.isVector()) {
5292  // TODO: Consider not scalarizing here.
5293  return SDValue();
5294  }
5295 
5296  unsigned OverflowOp;
5297  switch (Opcode) {
5298  case ISD::SADDSAT:
5299  OverflowOp = ISD::SADDO;
5300  break;
5301  case ISD::UADDSAT:
5302  OverflowOp = ISD::UADDO;
5303  break;
5304  case ISD::SSUBSAT:
5305  OverflowOp = ISD::SSUBO;
5306  break;
5307  case ISD::USUBSAT:
5308  OverflowOp = ISD::USUBO;
5309  break;
5310  default:
5311  llvm_unreachable("Expected method to receive signed or unsigned saturation "
5312  "addition or subtraction node.");
5313  }
5314 
5316  "Expected operands to be integers. Vector of int arguments should "
5317  "already be unrolled.");
5319  "Expected operands to be integers. Vector of int arguments should "
5320  "already be unrolled.");
5321  assert(LHS.getValueType() == RHS.getValueType() &&
5322  "Expected both operands to be the same type");
5323 
5324  unsigned BitWidth = LHS.getValueSizeInBits();
5325  EVT ResultType = LHS.getValueType();
5326  EVT BoolVT =
5327  getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ResultType);
5328  SDValue Result =
5329  DAG.getNode(OverflowOp, dl, DAG.getVTList(ResultType, BoolVT), LHS, RHS);
5330  SDValue SumDiff = Result.getValue(0);
5331  SDValue Overflow = Result.getValue(1);
5332  SDValue Zero = DAG.getConstant(0, dl, ResultType);
5333 
5334  if (Opcode == ISD::UADDSAT) {
5335  // Just need to check overflow for SatMax.
5336  APInt MaxVal = APInt::getMaxValue(BitWidth);
5337  SDValue SatMax = DAG.getConstant(MaxVal, dl, ResultType);
5338  return DAG.getSelect(dl, ResultType, Overflow, SatMax, SumDiff);
5339  } else if (Opcode == ISD::USUBSAT) {
5340  // Just need to check overflow for SatMin.
5341  APInt MinVal = APInt::getMinValue(BitWidth);
5342  SDValue SatMin = DAG.getConstant(MinVal, dl, ResultType);
5343  return DAG.getSelect(dl, ResultType, Overflow, SatMin, SumDiff);
5344  } else {
5345  // SatMax -> Overflow && SumDiff < 0
5346  // SatMin -> Overflow && SumDiff >= 0
5347  APInt MinVal = APInt::getSignedMinValue(BitWidth);
5348  APInt MaxVal = APInt::getSignedMaxValue(BitWidth);
5349  SDValue SatMin = DAG.getConstant(MinVal, dl, ResultType);
5350  SDValue SatMax = DAG.getConstant(MaxVal, dl, ResultType);
5351  SDValue SumNeg = DAG.getSetCC(dl, BoolVT, SumDiff, Zero, ISD::SETLT);
5352  Result = DAG.getSelect(dl, ResultType, SumNeg, SatMax, SatMin);
5353  return DAG.getSelect(dl, ResultType, Overflow, Result, SumDiff);
5354  }
5355 }
5356 
5357 SDValue
5359  SelectionDAG &DAG) const {
5360  assert(Node->getOpcode() == ISD::SMULFIX && "Expected opcode to be SMULFIX.");
5361  assert(Node->getNumOperands() == 3 &&
5362  "Expected signed fixed point multiplication to have 3 operands.");
5363 
5364  SDLoc dl(Node);
5365  SDValue LHS = Node->getOperand(0);
5366  SDValue RHS = Node->getOperand(1);
5368  "Expected operands to be integers. Vector of int arguments should "
5369  "already be unrolled.");
5371  "Expected operands to be integers. Vector of int arguments should "
5372  "already be unrolled.");
5373  assert(LHS.getValueType() == RHS.getValueType() &&
5374  "Expected both operands to be the same type");
5375 
5376  unsigned Scale = Node->getConstantOperandVal(2);
5377  EVT VT = LHS.getValueType();
5378  assert(Scale < VT.getScalarSizeInBits() &&
5379  "Expected scale to be less than the number of bits.");
5380 
5381  if (!Scale)
5382  return DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
5383 
5384  // Get the upper and lower bits of the result.
5385  SDValue Lo, Hi;
5387  SDValue Result =
5388  DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT), LHS, RHS);
5389  Lo = Result.getValue(0);
5390  Hi = Result.getValue(1);
5391  } else if (isOperationLegalOrCustom(ISD::MULHS, VT)) {
5392  Lo = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
5393  Hi = DAG.getNode(ISD::MULHS, dl, VT, LHS, RHS);
5394  } else {
5395  report_fatal_error("Unable to expand signed fixed point multiplication.");
5396  }
5397 
5398  // The result will need to be shifted right by the scale since both operands
5399  // are scaled. The result is given to us in 2 halves, so we only want part of
5400  // both in the result.
5401  EVT ShiftTy = getShiftAmountTy(VT, DAG.getDataLayout());
5402  Lo = DAG.getNode(ISD::SRL, dl, VT, Lo, DAG.getConstant(Scale, dl, ShiftTy));
5403  Hi = DAG.getNode(
5404  ISD::SHL, dl, VT, Hi,
5405  DAG.getConstant(VT.getScalarSizeInBits() - Scale, dl, ShiftTy));
5406  return DAG.getNode(ISD::OR, dl, VT, Lo, Hi);
5407 }
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1452
SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
bool expandCTTZ(SDNode *N, SDValue &Result, SelectionDAG &DAG) const
Expand CTTZ/CTTZ_ZERO_UNDEF nodes.
uint64_t CallInst * C
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:571
SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned char TargetFlags=0)
static MVT getIntegerVT(unsigned BitWidth)
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition: MathExtras.h:552
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:877
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:594
virtual MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
EVT getValueType() const
Return the ValueType of the referenced return value.
bool isInteger() const
Return true if this is an integer or a vector integer type.
void setSignBit()
Set the sign bit to 1.
Definition: APInt.h:1413
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
bool isSignMask() const
Check if the APInt&#39;s value is returned by getSignMask.
Definition: APInt.h:473
bool isUndef() const
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1077
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:35
SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
const GlobalValue * getGlobal() const
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1563
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:562
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition: APInt.h:1418
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond)
Helper function to make it easier to build SetCC&#39;s if you just have an ISD::CondCode instead of an SD...
Definition: SelectionDAG.h:937
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:323
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR (an vector value) starting with the ...
Definition: ISDOpcodes.h:358
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
unsigned getParamAlignment(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
Definition: CallSite.h:406
Various leaf nodes.
Definition: ISDOpcodes.h:60
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:367
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:260
virtual MVT::SimpleValueType getCmpLibcallReturnType() const
Return the ValueType for comparison libcalls.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:265
iterator begin() const
Definition: ArrayRef.h:137
#define LLVM_FALLTHROUGH
Definition: Compiler.h:86
SDValue scalarizeVectorStore(StoreSDNode *ST, SelectionDAG &DAG) const
bool hasConflict() const
Returns true if there is conflicting information.
Definition: KnownBits.h:47
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition: ISDOpcodes.h:519
const SDValue & getBasePtr() const
Carry-setting nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:223
virtual unsigned getJumpTableEncoding() const
Return the entry encoding for a jump table in the current function.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
virtual void LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
unsigned s
shift amount
Definition: APInt.h:1957
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:858
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition: ValueTypes.h:146
const SDValue & getValue() const
unsigned Reg
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:648
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:253
bool expandCTLZ(SDNode *N, SDValue &Result, SelectionDAG &DAG) const
Expand CTLZ/CTLZ_ZERO_UNDEF nodes.
Libcall
RTLIB::Libcall enum - This enum defines all of the runtime library calls the backend can emit...
int MatchingInput
MatchingInput - If this is not -1, this is an output constraint where an input constraint is required...
Definition: InlineAsm.h:131
virtual SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const
This method will be invoked for all target nodes and for any target-independent nodes that the target...
AAMDNodes getAAInfo() const
Returns the AA info that describes the dereference.
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition: ISDOpcodes.h:251
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition: APInt.h:1329
static ISD::NodeType getExtendForContent(BooleanContent Content)
const GlobalVariable * getNamedGlobal(StringRef Name) const
Return the global variable in the module with the specified name, of arbitrary type.
Definition: Module.h:402
const SDValue & getChain() const
ISD::MemIndexedMode getAddressingMode() const
Return the addressing mode for this load or store: unindexed, pre-inc, pre-dec, post-inc, or post-dec.
virtual bool isCommutativeBinOp(unsigned Opcode) const
Returns true if the opcode is a commutative binary operation.
static const fltSemantics & EVTToAPFloatSemantics(EVT VT)
Returns an APFloat semantics tag appropriate for the given type.
unsigned getAlignment() const
bool isBitwiseNot(SDValue V)
Returns true if V is a bitwise not operation.
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:811
virtual const char * getTargetNodeName(unsigned Opcode) const
This method returns the name of a target specific DAG node.
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1390
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
Definition: DataLayout.h:363
unsigned const TargetRegisterInfo * TRI
virtual AsmOperandInfoVector ParseConstraints(const DataLayout &DL, const TargetRegisterInfo *TRI, ImmutableCallSite CS) const
Split up the constraint string from the inline assembly value into the specific constraints and their...
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:141
F(f)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS)
Helper function to make it easier to build Select&#39;s if you just have operands and don&#39;t want to check...
Definition: SelectionDAG.h:950
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition: ISDOpcodes.h:508
void softenSetCCOperands(SelectionDAG &DAG, EVT VT, SDValue &NewLHS, SDValue &NewRHS, ISD::CondCode &CCCode, const SDLoc &DL) const
Soften the operands of a comparison.
virtual bool isTypeDesirableForOp(unsigned, EVT VT) const
Return true if the target has native support for the specified value type and it is &#39;desirable&#39; to us...
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:876
const SDNodeFlags getFlags() const
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
Same for subtraction.
Definition: ISDOpcodes.h:254
Value * CallOperandVal
If this is the result output operand or a clobber, this is null, otherwise it is the incoming operand...
bool isOperationLegalOrCustom(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
bool isByteSized() const
Return true if the bit size is a multiple of 8.
Definition: ValueTypes.h:212
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal...
Definition: ISDOpcodes.h:1005
bool isConstTrueVal(const SDNode *N) const
Return if the N is a constant or constant vector equal to the true value from getBooleanContents().
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1 at the ...
Definition: ISDOpcodes.h:353
CallLoweringInfo & setDiscardResult(bool Value=true)
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
virtual SDValue LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA, SelectionDAG &DAG) const
Lower TLS global address SDNode for target independent emulated TLS model.
virtual SDValue BuildSDIVPow2(SDNode *N, const APInt &Divisor, SelectionDAG &DAG, SmallVectorImpl< SDNode *> &Created) const
Targets may override this function to provide custom SDIV lowering for power-of-2 denominators...
unsigned getValueSizeInBits() const
Returns the size of the value in bits.
Magic data for optimising unsigned division by a constant.
Definition: APInt.h:1961
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:435
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition: ISDOpcodes.h:39
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:535
bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return true if the attribute exists at the given index.
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition: ISDOpcodes.h:316
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1509
SDValue BuildUDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization, SmallVectorImpl< SDNode *> &Created) const
Given an ISD::UDIV node expressing a divide by constant, return a DAG expression to select that will ...
SDValue getExternalSymbol(const char *Sym, EVT VT)
bool hasMatchingInput() const
hasMatchingInput - Return true if this is an output constraint that has a matching input constraint...
Definition: InlineAsm.h:135
bool isCondCodeLegal(ISD::CondCode CC, MVT VT) const
Return true if the specified condition code is legal on this target.
ConstraintCodeVector Codes
Code - The constraint code, either the register name (in braces) or the constraint letter/number...
Definition: InlineAsm.h:149
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:130
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition: ISDOpcodes.h:159
virtual bool isCtlzFast() const
Return true if ctlz instruction is fast.
A convenience struct that encapsulates a DAG, and two SDValues for returning information from TargetL...
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:136
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
Definition: APInt.h:1632
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
virtual unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
This method can be implemented by targets that want to expose additional information about sign bits ...
void setNoSignedWrap(bool b)
ms magic() const
Calculate the magic numbers required to implement a signed integer division by a constant as a sequen...
Definition: APInt.cpp:1145
std::vector< std::string > ConstraintCodeVector
Definition: InlineAsm.h:97
bool hasOneUse() const
Return true if there is exactly one use of this node.
APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
Definition: APInt.cpp:516
virtual bool isGAPlusOffset(SDNode *N, const GlobalValue *&GA, int64_t &Offset) const
Returns true (and the GlobalValue and the offset) if the node is a GlobalAddress + offset...
bool SimplifyDemandedVectorElts(SDValue Op, const APInt &DemandedEltMask, APInt &KnownUndef, APInt &KnownZero, TargetLoweringOpt &TLO, unsigned Depth=0, bool AssumeSingleUse=false) const
Look at Vector Op.
void setBit(unsigned BitPosition)
Set a given bit to 1.
Definition: APInt.h:1403
static SDValue clampDynamicVectorIndex(SelectionDAG &DAG, SDValue Idx, EVT VecVT, const SDLoc &dl)
SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond, bool foldBooleans, DAGCombinerInfo &DCI, const SDLoc &dl) const
Try to simplify a setcc built with the specified operands and cc.
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:993
Shift and rotation operations.
Definition: ISDOpcodes.h:410
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:202
Class to represent struct types.
Definition: DerivedTypes.h:201
APInt zextOrSelf(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:892
bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef...
TargetLowering::ConstraintType ConstraintType
Information about the constraint code, e.g.
CallLoweringInfo & setChain(SDValue InChain)
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:191
RESULT = SMULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same wi...
Definition: ISDOpcodes.h:280
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:197
op_iterator op_end() const
unsigned getScalarValueSizeInBits() const
ISD::LoadExtType getExtensionType() const
Return whether this is a plain node, or one of the varieties of value-extending loads.
iterator_range< regclass_iterator > regclasses() const
unsigned getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:304
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
Definition: SelectionDAG.h:460
virtual bool shouldUseStrictFP_TO_INT(EVT FpVT, EVT IntVT, bool IsSigned) const
Return true if it is more correct/profitable to use strict FP_TO_INT conversion operations - canonica...
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:401
SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition: APInt.cpp:511
bool expandMUL(SDNode *N, SDValue &Lo, SDValue &Hi, EVT HiLoVT, SelectionDAG &DAG, MulExpansionKind Kind, SDValue LL=SDValue(), SDValue LH=SDValue(), SDValue RL=SDValue(), SDValue RH=SDValue()) const
Expand a MUL into two nodes.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:978
SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
zlib-gnu style compression
bool ShrinkDemandedOp(SDValue Op, unsigned BitWidth, const APInt &Demanded, TargetLoweringOpt &TLO) const
Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the casts are free.
This represents a list of ValueType&#39;s that has been intern&#39;d by a SelectionDAG.
SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
unsigned getLiveInPhysReg(unsigned VReg) const
getLiveInPhysReg - If VReg is a live-in virtual register, return the corresponding live-in physical r...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it&#39;s free to truncate a value of type FromTy to type ToTy.
SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
unsigned getSizeInBits() const
Context object for machine code objects.
Definition: MCContext.h:63
void emitError(unsigned LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
ValTy * getCalledValue() const
Return the pointer to function that is being called.
Definition: CallSite.h:100
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1533
int64_t getSExtValue() const
SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:954
Fast - This calling convention attempts to make calls as fast as possible (e.g.
Definition: CallingConv.h:43
unsigned getScalarSizeInBits() const
Definition: ValueTypes.h:298
unsigned getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:292
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1575
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:398
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:478
static SDNodeIterator begin(const SDNode *N)
EK_LabelDifference32 - Each entry is the address of the block minus the address of the jump table...
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const
Get the CallingConv that should be used for the specified libcall.
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:429
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1462
This contains information for each constraint that we are lowering.
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:201
int32_t exactLogBase2() const
Definition: APInt.h:1788
APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition: APInt.cpp:405
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
bool expandABS(SDNode *N, SDValue &Result, SelectionDAG &DAG) const
Expand ABS nodes.
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Return true if the call or the callee has the given attribute.
Definition: CallSite.h:377
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
EK_BlockAddress - Each entry is a plain address of block, e.g.
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:221
CallLoweringInfo & setZExtResult(bool Value=true)
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
Definition: SelectionDAG.h:852
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:224
virtual bool SimplifyDemandedVectorEltsForTargetNode(SDValue Op, const APInt &DemandedElts, APInt &KnownUndef, APInt &KnownZero, TargetLoweringOpt &TLO, unsigned Depth=0) const
Attempt to simplify any target nodes based on the demanded vector elements, returning true on success...
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out...
Definition: ISDOpcodes.h:959
op_iterator op_begin() const
std::pair< SDValue, SDValue > expandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG) const
Expands an unaligned load to 2 half-size loads for an integer, and possibly more for vectors...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
Definition: SelectionDAG.h:576
ConstraintPrefix Type
Type - The basic type of the constraint: input/output/clobber.
Definition: InlineAsm.h:121
ArrayRef< SDUse > ops() const
bool parametersInCSRMatch(const MachineRegisterInfo &MRI, const uint32_t *CallerPreservedMask, const SmallVectorImpl< CCValAssign > &ArgLocs, const SmallVectorImpl< SDValue > &OutVals) const
Check whether parameters to a call that are passed in callee saved registers are the same as from the...
bool isSignedIntSetCC(CondCode Code)
Return true if this is a setcc instruction that performs a signed comparison when used with integer o...
Definition: ISDOpcodes.h:992
bool isTypeLegalForClass(const TargetRegisterClass &RC, MVT T) const
Return true if the given TargetRegisterClass has the ValueType T.
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:151
amdgpu Simplify well known AMD library false Value * Callee
Class to represent pointers.
Definition: DerivedTypes.h:467
virtual StringRef getRegAsmName(unsigned Reg) const
Return the assembly name for Reg.
This class is used to represent ISD::STORE nodes.
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:524
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a vector with the specified, possibly variable...
Definition: ISDOpcodes.h:327
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:636
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:229
unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits...
void setAttributes(ImmutableCallSite *CS, unsigned ArgIdx)
Set CallLoweringInfo attribute flags based on a call instruction and called function attributes...
bool isVoidTy() const
Return true if this is &#39;void&#39;.
Definition: Type.h:141
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:610
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
bool ShrinkDemandedConstant(SDValue Op, const APInt &Demanded, TargetLoweringOpt &TLO) const
Check to see if the specified operand of the specified instruction is a constant integer.
void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition: APInt.cpp:346
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:364
const SDValue & getBasePtr() const
EK_GPRel64BlockAddress - Each entry is an address of block, encoded with a relocation as gp-relative...
std::vector< AsmOperandInfo > AsmOperandInfoVector
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:166
MVT getSimpleValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the MVT corresponding to this LLVM type. See getValueType.
bool isAllOnesValue() const
Determine if all bits are set.
Definition: APInt.h:396
virtual const char * LowerXConstraint(EVT ConstraintVT) const
Try to replace an X constraint, which matches anything, with another that has more specific requireme...
SDValue getVectorElementPointer(SelectionDAG &DAG, SDValue VecPtr, EVT VecVT, SDValue Index) const
Get a pointer to vector element Idx located in memory for a vector of type VecVT starting at a base a...
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:391
unsigned const MachineRegisterInfo * MRI
MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool shouldAssumeDSOLocal(const Module &M, const GlobalValue *GV) const
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:429
Machine Value Type.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
Simple binary floating point operators.
Definition: ISDOpcodes.h:283
bool isIndirect
isIndirect - True if this operand is an indirect operand.
Definition: InlineAsm.h:145
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:273
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:66
iterator_range< value_op_iterator > op_values() const
bool isRound() const
Return true if the size is a power-of-two number of bytes.
Definition: ValueTypes.h:217
const SDValue & getOperand(unsigned Num) const
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
Definition: ISDOpcodes.h:934
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL...
Definition: ISDOpcodes.h:332
virtual bool SimplifyDemandedBitsForTargetNode(SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts, KnownBits &Known, TargetLoweringOpt &TLO, unsigned Depth=0) const
Attempt to simplify any target nodes based on the demanded bits/elts, returning true on success...
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:247
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:232
bool isKnownNeverSNaN(SDValue Op, unsigned Depth=0) const
bool isOneValue() const
Determine if this is a value of 1.
Definition: APInt.h:411
virtual SDValue unwrapAddress(SDValue N) const
static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo, const TargetLowering &TLI, SDValue Op, SelectionDAG *DAG)
If there are multiple different constraints that we could pick for this operand (e.g.
virtual void computeKnownBitsForFrameIndex(const SDValue FIOp, KnownBits &Known, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
Determine which of the bits of FrameIndex FIOp are known to be 0.
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:588
SubConstraintInfoVector multipleAlternatives
multipleAlternatives - If there are multiple alternative constraints, this array will contain them...
Definition: InlineAsm.h:156
mu magicu(unsigned LeadingZeros=0) const
Calculate the magic numbers required to implement an unsigned integer division by a constant as a seq...
Definition: APInt.cpp:1189
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1193
bool hasNoNaNs() const
KnownBits trunc(unsigned BitWidth)
Truncate the underlying known Zero and One bits.
Definition: KnownBits.h:113
virtual ConstraintWeight getSingleConstraintMatchWeight(AsmOperandInfo &info, const char *constraint) const
Examine constraint string and operand type and determine a weight value.
SDValue expandAddSubSat(SDNode *Node, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US][ADD|SUB]SAT.
bool CombineTo(SDValue O, SDValue N)
MCSymbol * getJTISymbol(unsigned JTI, MCContext &Ctx, bool isLinkerPrivate=false) const
getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
const APInt & getAPIntValue() const
bool isOperationLegalOrCustomOrPromote(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
virtual bool targetShrinkDemandedConstant(SDValue Op, const APInt &Demanded, TargetLoweringOpt &TLO) const
unsigned getUnorderedFlavor(CondCode Cond)
This function returns 0 if the condition is always false if an operand is a NaN, 1 if the condition i...
Definition: ISDOpcodes.h:1012
CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y)...
static MVT getVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:281
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
lazy value info
bool isExtendedTrueVal(const ConstantSDNode *N, EVT VT, bool SExt) const
Return if N is a True value when extended to VT.
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:416
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:556
std::vector< ArgListEntry > ArgListTy
Extended Value Type.
Definition: ValueTypes.h:34
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:398
R600 Clause Merge
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:640
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
SDValue expandFMINNUM_FMAXNUM(SDNode *N, SelectionDAG &DAG) const
Expand fminnum/fmaxnum into fminnum_ieee/fmaxnum_ieee with quieted inputs.
This structure contains all information that is necessary for lowering calls.
bool isPositionIndependent() const
size_t size() const
Definition: SmallVector.h:53
virtual bool isUsedByReturnOnly(SDNode *, SDValue &) const
Return true if result of the specified node is used by a return node only.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:220
bool isVolatile() const
const TargetMachine & getTargetMachine() const
unsigned getNumOperands() const
Return the number of values used by this operation.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
std::string ConstraintCode
This contains the actual string for the code, like "m".
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:971
SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target&#39;s BooleanContent for type OpVT...
SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands...
SDValue CreateStackTemporary(EVT VT, unsigned minAlign=1)
Create a stack temporary, suitable for holding the specified value type.
bool isUnindexed() const
Return true if this is NOT a pre/post inc/dec load/store.
This base class for TargetLowering contains the SelectionDAG-independent parts that can be used from ...
static const char *const Magic
Definition: Archive.cpp:42
SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type...
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:555
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition: ISDOpcodes.h:265
static bool isUndef(ArrayRef< int > Mask)
TokenFactor - This node takes multiple tokens as input and produces a single token result...
Definition: ISDOpcodes.h:50
bool isLegalRC(const TargetRegisterInfo &TRI, const TargetRegisterClass &RC) const
Return true if the value types that can be represented by the specified register class are all legal...
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:240
const TargetLowering & getTargetLoweringInfo() const
Definition: SelectionDAG.h:404
void setNoUnsignedWrap(bool b)
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
SDValue scalarizeVectorLoad(LoadSDNode *LD, SelectionDAG &DAG) const
Turn load of vector type into a load of the individual elements.
virtual bool shouldReduceLoadWidth(SDNode *Load, ISD::LoadExtType ExtTy, EVT NewVT) const
Return true if it is profitable to reduce a load to a smaller type.
SDValue BuildSDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization, SmallVectorImpl< SDNode *> &Created) const
Given an ISD::SDIV node expressing a divide by constant, return a DAG expression to select that will ...
virtual ConstraintWeight getMultipleConstraintMatchWeight(AsmOperandInfo &info, int maIndex) const
Examine constraint type and operand type and determine a weight value.
SDValue getExpandedFixedPointMultiplication(SDNode *Node, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::SMULFIX.
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:319
ValTy * getArgument(unsigned ArgNo) const
Definition: CallSite.h:186
SDValue lowerCmpEqZeroToCtlzSrl(SDValue Op, SelectionDAG &DAG) const
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition: ISDOpcodes.h:339
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:265
double Log2(double Value)
Return the log base 2 of the specified value.
Definition: MathExtras.h:528
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:222
APInt m
magic number
Definition: APInt.h:1962
SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
virtual SDValue getPICJumpTableRelocBase(SDValue Table, SelectionDAG &DAG) const
Returns relocation base for the given PIC jumptable.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
bool expandCTPOP(SDNode *N, SDValue &Result, SelectionDAG &DAG) const
Expand CTPOP nodes.
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
Definition: SelectionDAG.h:734
bool SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts, KnownBits &Known, TargetLoweringOpt &TLO, unsigned Depth=0, bool AssumeSingleUse=false) const
Look at Op.
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:523
CCValAssign - Represent assignment of one arg/retval to a location.
iterator end() const
Definition: ArrayRef.h:138
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimum or maximum on two values, following the IEEE-754 2008 definition.
Definition: ISDOpcodes.h:600
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:542
void negate()
Negate this APInt in place.
Definition: APInt.h:1493
CallLoweringInfo & setSExtResult(bool Value=true)
SDValue IncrementMemoryAddress(SDValue Addr, SDValue Mask, const SDLoc &DL, EVT DataVT, SelectionDAG &DAG, bool IsCompressedMemory) const
Increments memory address Addr according to the type of the value DataVT that should be stored...
Represents one node in the SelectionDAG.
virtual void computeKnownBitsForTargetNode(const SDValue Op, KnownBits &Known, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
Determine which of the bits specified in Mask are known to be either zero or one and return them in t...
CondCode getSetCCInverse(CondCode Operation, bool isInteger)
Return the operation corresponding to !(X op Y), where &#39;op&#39; is a valid SetCC operation.
SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
double BitsToDouble(uint64_t Bits)
This function takes a 64-bit integer and returns the bit equivalent double.
Definition: MathExtras.h:573
const Function & getFunction() const
Return the LLVM function that this machine code represents.
bool isConstFalseVal(const SDNode *N) const
Return if the N is a constant or constant vector equal to the false value from getBooleanContents().
unsigned logBase2() const
Definition: APInt.h:1748
MachinePointerInfo getWithOffset(int64_t O) const
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:539
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:941
unsigned countTrailingOnes() const
Count the number of trailing one bits.
Definition: APInt.h:1646
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
EVT getMemoryVT() const
Return the type of the in-memory value.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
Class for arbitrary precision integers.
Definition: APInt.h:70
virtual void ComputeConstraintToUse(AsmOperandInfo &OpInfo, SDValue Op, SelectionDAG *DAG=nullptr) const
Determines the constraint code and constraint type to use for the specific AsmOperandInfo, setting OpInfo.ConstraintCode and OpInfo.ConstraintType.
SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1223
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
A "pseudo-class" with methods for operating on BUILD_VECTORs.
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:420
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:241
bool isPowerOf2() const
Check if this APInt&#39;s value is a power of two greater than zero.
Definition: APInt.h:464
bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:468
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:471
MulExpansionKind
Enum that specifies when a multiplication should be expanded.
SDValue expandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG) const
Expands an unaligned store to 2 half-size stores for integer values, and possibly more for vectors...
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:530
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
virtual bool hasAndNotCompare(SDValue Y) const
Return true if the target should transform: (X & Y) == Y —> (~X & Y) == 0 (X & Y) != Y —> (~X & Y) ...
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:568
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
bool hasNoSignedWrap() const
virtual bool isLegalICmpImmediate(int64_t) const
Return true if the specified immediate is legal icmp immediate, that is the target has icmp instructi...
static SDValue BuildExactSDIV(const TargetLowering &TLI, SDNode *N, const SDLoc &dl, SelectionDAG &DAG, SmallVectorImpl< SDNode *> &Created)
Given an exact SDIV by a constant, create a multiplication with the multiplicative inverse of the con...
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
Definition: APInt.h:1255
These are IR-level optimization flags that may be propagated to SDNodes.
Magic data for optimising signed division by a constant.
Definition: APInt.h:1955
ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:151
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:387
unsigned s
shift amount
Definition: APInt.h:1964
std::pair< SDValue, SDValue > makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT, ArrayRef< SDValue > Ops, bool isSigned, const SDLoc &dl, bool doesNotReturn=false, bool isReturnValueUsed=true) const
Returns a pair of (return value, chain).
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition: ISDOpcodes.h:206
APInt m
magic number
Definition: APInt.h:1956
bool isKnownToBeAPowerOfTwo(SDValue Val) const
Test if the given value is known to have exactly one bit set.
bool isPositionIndependent() const
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:486
virtual bool isKnownNeverNaNForTargetNode(SDValue Op, const SelectionDAG &DAG, bool SNaN=false, unsigned Depth=0) const
If SNaN is false,.
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:614
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
Establish a view to a call site for examination.
Definition: CallSite.h:711
bool expandMUL_LOHI(unsigned Opcode, EVT VT, SDLoc dl, SDValue LHS, SDValue RHS, SmallVectorImpl< SDValue > &Result, EVT HiLoVT, SelectionDAG &DAG, MulExpansionKind Kind, SDValue LL=SDValue(), SDValue LH=SDValue(), SDValue RL=SDValue(), SDValue RH=SDValue()) const
Expand a MUL or [US]MUL_LOHI of n-bit values into two or four nodes, respectively, each computing an n/2-bit part of the result.
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
Flags getFlags() const
Return the raw flags of the source value,.
unsigned InferPtrAlignment(SDValue Ptr) const
Infer alignment of a load / store address.
bool isMatchingInputConstraint() const
Return true of this is an input operand that is a matching constraint like "4".
ISD::CondCode getCmpLibcallCC(RTLIB::Libcall Call) const
Get the CondCode that&#39;s to be used to test the result of the comparison libcall against zero...
unsigned getMatchedOperand() const
If this is an input matching constraint, this method returns the output operand it matches...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition: ISDOpcodes.h:273
unsigned getOpcode() const
SDValue getValue(unsigned R) const
virtual bool isIntDivCheap(EVT VT, AttributeList Attr) const
Return true if integer divide is usually cheaper than a sequence of several shifts, adds, and multiplies for this target.
bool expandFunnelShift(SDNode *N, SDValue &Result, SelectionDAG &DAG) const
Expand funnel shift.
static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT)
Return an integer indicating how general CT is.
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition: ISDOpcodes.h:175
Type * getType() const
Return the type of the instruction that generated this call site.
Definition: CallSite.h:264
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
SDValue getCondCode(ISD::CondCode Cond)
bool isRegLoc() const
SDValue getGLOBAL_OFFSET_TABLE(EVT VT)
Return a GLOBAL_OFFSET_TABLE node. This does not have a useful SDLoc.
Definition: SelectionDAG.h:857
const MachinePointerInfo & getPointerInfo() const
const unsigned Kind
bool isInTailCallPosition(SelectionDAG &DAG, SDNode *Node, SDValue &Chain) const
Check whether a given call node is in tail position within its function.
ConstantSDNode * getConstantSplatNode(BitVector *UndefElements=nullptr) const
Returns the splatted constant or null if this is not a constant splat.
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:345
bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if &#39;Op & Mask&#39; is known to be zero.
bool expandFP_TO_UINT(SDNode *N, SDValue &Result, SelectionDAG &DAG) const
Expand float to UINT conversion.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
unsigned getMinSignedBits() const
Get the minimum bit size for this signed APInt.
Definition: APInt.h:1552
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:545
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit. ...
Definition: APInt.h:1620
TargetLowering(const TargetLowering &)=delete
bool isSingleValueType() const
Return true if the type is a valid type for a register in codegen.
Definition: Type.h:250
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:566
LLVM Value Representation.
Definition: Value.h:73
uint64_t getConstantOperandVal(unsigned Num) const
Helper method returns the integer value of a ConstantSDNode operand.
static SDNodeIterator end(const SDNode *N)
EVT changeTypeToInteger()
Return the type converted to an equivalently sized integer or vector with integer element type...
Definition: ValueTypes.h:115
bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
SDValue getSelectCC(const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue True, SDValue False, ISD::CondCode Cond)
Helper function to make it easier to build SelectCC&#39;s if you just have an ISD::CondCode instead of an...
Definition: SelectionDAG.h:962
bool hasNoUnsignedWrap() const
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:81
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
SetCC operator - This evaluates to a true value iff the condition is true.
Definition: ISDOpcodes.h:443
virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const
Return true if folding a constant offset with the given GlobalAddress is legal.
EK_GPRel32BlockAddress - Each entry is an address of block, encoded with a relocation as gp-relative...
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
Definition: APInt.h:1596
KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
MVT ConstraintVT
The ValueType for the operand value.
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition: ISDOpcodes.h:198
unsigned getNumOperands() const
virtual bool isShuffleMaskLegal(ArrayRef< int >, EVT) const
Targets can use this to indicate that they only support some VECTOR_SHUFFLE operations, those with specific masks.
Conversion operators.
Definition: ISDOpcodes.h:465
bool expandUINT_TO_FP(SDNode *N, SDValue &Result, SelectionDAG &DAG) const
Expand UINT(i64) to double(f64) conversion.
BooleanContent
Enum that describes how the target represents true/false values.
static APInt getNullValue(unsigned numBits)
Get the &#39;0&#39; value.
Definition: APInt.h:569
const SDValue & getOperand(unsigned i) const
bool verifyReturnAddressArgumentIsConstant(SDValue Op, SelectionDAG &DAG) const
unsigned getLocReg() const
uint64_t getZExtValue() const
bool isBigEndian() const
Definition: DataLayout.h:222
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:474
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Op, int64_t Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object...
Definition: SelectionDAG.h:806
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:126
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
iterator end() const
Definition: StringRef.h:108
bool expandROT(SDNode *N, SDValue &Result, SelectionDAG &DAG) const
Expand rotations.
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:584
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:64
virtual bool shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const
Returns true if arguments should be sign-extended in lib calls.
virtual const MCExpr * getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI, MCContext &Ctx) const
This returns the relocation base for the given PIC jumptable, the same as getPICJumpTableRelocBase, but as an MCExpr.
LLVMContext * getContext() const
Definition: SelectionDAG.h:407
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:375
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1442
Type * getElementType() const
Definition: DerivedTypes.h:486
CallLoweringInfo & setLibCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL, bool LegalTypes=true) const
#define T1
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:242
EVT getHalfSizedIntegerVT(LLVMContext &Context) const
Finds the smallest simple value type that is greater than or equal to half the width of this EVT...
Definition: ValueTypes.h:328
bool isNullValue() const
Determine if all bits are clear.
Definition: APInt.h:406
static ConstraintInfoVector ParseConstraints(StringRef ConstraintString)
ParseConstraints - Split up the constraint string into the specific constraints and their prefixes...
Definition: InlineAsm.cpp:208
bool a
add indicator
Definition: APInt.h:1963
This file describes how to lower LLVM code to machine code.
void CommitTargetLoweringOpt(const TargetLoweringOpt &TLO)
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, unsigned char TargetFlags=0)
Definition: SelectionDAG.h:622
void selectAlternative(unsigned index)
selectAlternative - Point this constraint to the alternative constraint indicated by the index...
Definition: InlineAsm.cpp:197
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:380
This class is used to represent ISD::LOAD nodes.
bool expandFP_TO_SINT(SDNode *N, SDValue &Result, SelectionDAG &DAG) const
Expand float(f32) to SINT(i64) conversion.