LLVM  8.0.1
InstCombineCalls.cpp
Go to the documentation of this file.
1 //===- InstCombineCalls.cpp -----------------------------------------------===//
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 file implements the visitCall and visitInvoke functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "InstCombineInternal.h"
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/Twine.h"
29 #include "llvm/IR/Attributes.h"
30 #include "llvm/IR/BasicBlock.h"
31 #include "llvm/IR/CallSite.h"
32 #include "llvm/IR/Constant.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/DataLayout.h"
35 #include "llvm/IR/DerivedTypes.h"
36 #include "llvm/IR/Function.h"
37 #include "llvm/IR/GlobalVariable.h"
38 #include "llvm/IR/InstrTypes.h"
39 #include "llvm/IR/Instruction.h"
40 #include "llvm/IR/Instructions.h"
41 #include "llvm/IR/IntrinsicInst.h"
42 #include "llvm/IR/Intrinsics.h"
43 #include "llvm/IR/LLVMContext.h"
44 #include "llvm/IR/Metadata.h"
45 #include "llvm/IR/PatternMatch.h"
46 #include "llvm/IR/Statepoint.h"
47 #include "llvm/IR/Type.h"
48 #include "llvm/IR/User.h"
49 #include "llvm/IR/Value.h"
50 #include "llvm/IR/ValueHandle.h"
52 #include "llvm/Support/Casting.h"
54 #include "llvm/Support/Compiler.h"
55 #include "llvm/Support/Debug.h"
57 #include "llvm/Support/KnownBits.h"
62 #include <algorithm>
63 #include <cassert>
64 #include <cstdint>
65 #include <cstring>
66 #include <utility>
67 #include <vector>
68 
69 using namespace llvm;
70 using namespace PatternMatch;
71 
72 #define DEBUG_TYPE "instcombine"
73 
74 STATISTIC(NumSimplified, "Number of library calls simplified");
75 
77  "instcombine-guard-widening-window",
78  cl::init(3),
79  cl::desc("How wide an instruction window to bypass looking for "
80  "another guard"));
81 
82 /// Return the specified type promoted as it would be to pass though a va_arg
83 /// area.
84 static Type *getPromotedType(Type *Ty) {
85  if (IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
86  if (ITy->getBitWidth() < 32)
87  return Type::getInt32Ty(Ty->getContext());
88  }
89  return Ty;
90 }
91 
92 /// Return a constant boolean vector that has true elements in all positions
93 /// where the input constant data vector has an element with the sign bit set.
96  IntegerType *BoolTy = Type::getInt1Ty(V->getContext());
97  for (unsigned I = 0, E = V->getNumElements(); I != E; ++I) {
98  Constant *Elt = V->getElementAsConstant(I);
99  assert((isa<ConstantInt>(Elt) || isa<ConstantFP>(Elt)) &&
100  "Unexpected constant data vector element type");
101  bool Sign = V->getElementType()->isIntegerTy()
102  ? cast<ConstantInt>(Elt)->isNegative()
103  : cast<ConstantFP>(Elt)->isNegative();
104  BoolVec.push_back(ConstantInt::get(BoolTy, Sign));
105  }
106  return ConstantVector::get(BoolVec);
107 }
108 
109 Instruction *InstCombiner::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) {
110  unsigned DstAlign = getKnownAlignment(MI->getRawDest(), DL, MI, &AC, &DT);
111  unsigned CopyDstAlign = MI->getDestAlignment();
112  if (CopyDstAlign < DstAlign){
113  MI->setDestAlignment(DstAlign);
114  return MI;
115  }
116 
117  unsigned SrcAlign = getKnownAlignment(MI->getRawSource(), DL, MI, &AC, &DT);
118  unsigned CopySrcAlign = MI->getSourceAlignment();
119  if (CopySrcAlign < SrcAlign) {
120  MI->setSourceAlignment(SrcAlign);
121  return MI;
122  }
123 
124  // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
125  // load/store.
126  ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getLength());
127  if (!MemOpLength) return nullptr;
128 
129  // Source and destination pointer types are always "i8*" for intrinsic. See
130  // if the size is something we can handle with a single primitive load/store.
131  // A single load+store correctly handles overlapping memory in the memmove
132  // case.
133  uint64_t Size = MemOpLength->getLimitedValue();
134  assert(Size && "0-sized memory transferring should be removed already.");
135 
136  if (Size > 8 || (Size&(Size-1)))
137  return nullptr; // If not 1/2/4/8 bytes, exit.
138 
139  // If it is an atomic and alignment is less than the size then we will
140  // introduce the unaligned memory access which will be later transformed
141  // into libcall in CodeGen. This is not evident performance gain so disable
142  // it now.
143  if (isa<AtomicMemTransferInst>(MI))
144  if (CopyDstAlign < Size || CopySrcAlign < Size)
145  return nullptr;
146 
147  // Use an integer load+store unless we can find something better.
148  unsigned SrcAddrSp =
149  cast<PointerType>(MI->getArgOperand(1)->getType())->getAddressSpace();
150  unsigned DstAddrSp =
151  cast<PointerType>(MI->getArgOperand(0)->getType())->getAddressSpace();
152 
153  IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
154  Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp);
155  Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp);
156 
157  // If the memcpy has metadata describing the members, see if we can get the
158  // TBAA tag describing our copy.
159  MDNode *CopyMD = nullptr;
160  if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa)) {
161  CopyMD = M;
162  } else if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa_struct)) {
163  if (M->getNumOperands() == 3 && M->getOperand(0) &&
164  mdconst::hasa<ConstantInt>(M->getOperand(0)) &&
165  mdconst::extract<ConstantInt>(M->getOperand(0))->isZero() &&
166  M->getOperand(1) &&
167  mdconst::hasa<ConstantInt>(M->getOperand(1)) &&
168  mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
169  Size &&
170  M->getOperand(2) && isa<MDNode>(M->getOperand(2)))
171  CopyMD = cast<MDNode>(M->getOperand(2));
172  }
173 
174  Value *Src = Builder.CreateBitCast(MI->getArgOperand(1), NewSrcPtrTy);
175  Value *Dest = Builder.CreateBitCast(MI->getArgOperand(0), NewDstPtrTy);
176  LoadInst *L = Builder.CreateLoad(Src);
177  // Alignment from the mem intrinsic will be better, so use it.
178  L->setAlignment(CopySrcAlign);
179  if (CopyMD)
180  L->setMetadata(LLVMContext::MD_tbaa, CopyMD);
181  MDNode *LoopMemParallelMD =
183  if (LoopMemParallelMD)
185  MDNode *AccessGroupMD = MI->getMetadata(LLVMContext::MD_access_group);
186  if (AccessGroupMD)
187  L->setMetadata(LLVMContext::MD_access_group, AccessGroupMD);
188 
189  StoreInst *S = Builder.CreateStore(L, Dest);
190  // Alignment from the mem intrinsic will be better, so use it.
191  S->setAlignment(CopyDstAlign);
192  if (CopyMD)
193  S->setMetadata(LLVMContext::MD_tbaa, CopyMD);
194  if (LoopMemParallelMD)
196  if (AccessGroupMD)
197  S->setMetadata(LLVMContext::MD_access_group, AccessGroupMD);
198 
199  if (auto *MT = dyn_cast<MemTransferInst>(MI)) {
200  // non-atomics can be volatile
201  L->setVolatile(MT->isVolatile());
202  S->setVolatile(MT->isVolatile());
203  }
204  if (isa<AtomicMemTransferInst>(MI)) {
205  // atomics have to be unordered
208  }
209 
210  // Set the size of the copy to 0, it will be deleted on the next iteration.
211  MI->setLength(Constant::getNullValue(MemOpLength->getType()));
212  return MI;
213 }
214 
215 Instruction *InstCombiner::SimplifyAnyMemSet(AnyMemSetInst *MI) {
216  unsigned Alignment = getKnownAlignment(MI->getDest(), DL, MI, &AC, &DT);
217  if (MI->getDestAlignment() < Alignment) {
218  MI->setDestAlignment(Alignment);
219  return MI;
220  }
221 
222  // Extract the length and alignment and fill if they are constant.
223  ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
224  ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
225  if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8))
226  return nullptr;
227  uint64_t Len = LenC->getLimitedValue();
228  Alignment = MI->getDestAlignment();
229  assert(Len && "0-sized memory setting should be removed already.");
230 
231  // Alignment 0 is identity for alignment 1 for memset, but not store.
232  if (Alignment == 0)
233  Alignment = 1;
234 
235  // If it is an atomic and alignment is less than the size then we will
236  // introduce the unaligned memory access which will be later transformed
237  // into libcall in CodeGen. This is not evident performance gain so disable
238  // it now.
239  if (isa<AtomicMemSetInst>(MI))
240  if (Alignment < Len)
241  return nullptr;
242 
243  // memset(s,c,n) -> store s, c (for n=1,2,4,8)
244  if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
245  Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8.
246 
247  Value *Dest = MI->getDest();
248  unsigned DstAddrSp = cast<PointerType>(Dest->getType())->getAddressSpace();
249  Type *NewDstPtrTy = PointerType::get(ITy, DstAddrSp);
250  Dest = Builder.CreateBitCast(Dest, NewDstPtrTy);
251 
252  // Extract the fill value and store.
253  uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
254  StoreInst *S = Builder.CreateStore(ConstantInt::get(ITy, Fill), Dest,
255  MI->isVolatile());
256  S->setAlignment(Alignment);
257  if (isa<AtomicMemSetInst>(MI))
259 
260  // Set the size of the copy to 0, it will be deleted on the next iteration.
261  MI->setLength(Constant::getNullValue(LenC->getType()));
262  return MI;
263  }
264 
265  return nullptr;
266 }
267 
269  InstCombiner::BuilderTy &Builder) {
270  bool LogicalShift = false;
271  bool ShiftLeft = false;
272 
273  switch (II.getIntrinsicID()) {
274  default: llvm_unreachable("Unexpected intrinsic!");
293  LogicalShift = false; ShiftLeft = false;
294  break;
313  LogicalShift = true; ShiftLeft = false;
314  break;
333  LogicalShift = true; ShiftLeft = true;
334  break;
335  }
336  assert((LogicalShift || !ShiftLeft) && "Only logical shifts can shift left");
337 
338  // Simplify if count is constant.
339  auto Arg1 = II.getArgOperand(1);
340  auto CAZ = dyn_cast<ConstantAggregateZero>(Arg1);
341  auto CDV = dyn_cast<ConstantDataVector>(Arg1);
342  auto CInt = dyn_cast<ConstantInt>(Arg1);
343  if (!CAZ && !CDV && !CInt)
344  return nullptr;
345 
346  APInt Count(64, 0);
347  if (CDV) {
348  // SSE2/AVX2 uses all the first 64-bits of the 128-bit vector
349  // operand to compute the shift amount.
350  auto VT = cast<VectorType>(CDV->getType());
351  unsigned BitWidth = VT->getElementType()->getPrimitiveSizeInBits();
352  assert((64 % BitWidth) == 0 && "Unexpected packed shift size");
353  unsigned NumSubElts = 64 / BitWidth;
354 
355  // Concatenate the sub-elements to create the 64-bit value.
356  for (unsigned i = 0; i != NumSubElts; ++i) {
357  unsigned SubEltIdx = (NumSubElts - 1) - i;
358  auto SubElt = cast<ConstantInt>(CDV->getElementAsConstant(SubEltIdx));
359  Count <<= BitWidth;
360  Count |= SubElt->getValue().zextOrTrunc(64);
361  }
362  }
363  else if (CInt)
364  Count = CInt->getValue();
365 
366  auto Vec = II.getArgOperand(0);
367  auto VT = cast<VectorType>(Vec->getType());
368  auto SVT = VT->getElementType();
369  unsigned VWidth = VT->getNumElements();
370  unsigned BitWidth = SVT->getPrimitiveSizeInBits();
371 
372  // If shift-by-zero then just return the original value.
373  if (Count.isNullValue())
374  return Vec;
375 
376  // Handle cases when Shift >= BitWidth.
377  if (Count.uge(BitWidth)) {
378  // If LogicalShift - just return zero.
379  if (LogicalShift)
380  return ConstantAggregateZero::get(VT);
381 
382  // If ArithmeticShift - clamp Shift to (BitWidth - 1).
383  Count = APInt(64, BitWidth - 1);
384  }
385 
386  // Get a constant vector of the same type as the first operand.
387  auto ShiftAmt = ConstantInt::get(SVT, Count.zextOrTrunc(BitWidth));
388  auto ShiftVec = Builder.CreateVectorSplat(VWidth, ShiftAmt);
389 
390  if (ShiftLeft)
391  return Builder.CreateShl(Vec, ShiftVec);
392 
393  if (LogicalShift)
394  return Builder.CreateLShr(Vec, ShiftVec);
395 
396  return Builder.CreateAShr(Vec, ShiftVec);
397 }
398 
399 // Attempt to simplify AVX2 per-element shift intrinsics to a generic IR shift.
400 // Unlike the generic IR shifts, the intrinsics have defined behaviour for out
401 // of range shift amounts (logical - set to zero, arithmetic - splat sign bit).
403  InstCombiner::BuilderTy &Builder) {
404  bool LogicalShift = false;
405  bool ShiftLeft = false;
406 
407  switch (II.getIntrinsicID()) {
408  default: llvm_unreachable("Unexpected intrinsic!");
418  LogicalShift = false;
419  ShiftLeft = false;
420  break;
430  LogicalShift = true;
431  ShiftLeft = false;
432  break;
442  LogicalShift = true;
443  ShiftLeft = true;
444  break;
445  }
446  assert((LogicalShift || !ShiftLeft) && "Only logical shifts can shift left");
447 
448  // Simplify if all shift amounts are constant/undef.
449  auto *CShift = dyn_cast<Constant>(II.getArgOperand(1));
450  if (!CShift)
451  return nullptr;
452 
453  auto Vec = II.getArgOperand(0);
454  auto VT = cast<VectorType>(II.getType());
455  auto SVT = VT->getVectorElementType();
456  int NumElts = VT->getNumElements();
457  int BitWidth = SVT->getIntegerBitWidth();
458 
459  // Collect each element's shift amount.
460  // We also collect special cases: UNDEF = -1, OUT-OF-RANGE = BitWidth.
461  bool AnyOutOfRange = false;
462  SmallVector<int, 8> ShiftAmts;
463  for (int I = 0; I < NumElts; ++I) {
464  auto *CElt = CShift->getAggregateElement(I);
465  if (CElt && isa<UndefValue>(CElt)) {
466  ShiftAmts.push_back(-1);
467  continue;
468  }
469 
470  auto *COp = dyn_cast_or_null<ConstantInt>(CElt);
471  if (!COp)
472  return nullptr;
473 
474  // Handle out of range shifts.
475  // If LogicalShift - set to BitWidth (special case).
476  // If ArithmeticShift - set to (BitWidth - 1) (sign splat).
477  APInt ShiftVal = COp->getValue();
478  if (ShiftVal.uge(BitWidth)) {
479  AnyOutOfRange = LogicalShift;
480  ShiftAmts.push_back(LogicalShift ? BitWidth : BitWidth - 1);
481  continue;
482  }
483 
484  ShiftAmts.push_back((int)ShiftVal.getZExtValue());
485  }
486 
487  // If all elements out of range or UNDEF, return vector of zeros/undefs.
488  // ArithmeticShift should only hit this if they are all UNDEF.
489  auto OutOfRange = [&](int Idx) { return (Idx < 0) || (BitWidth <= Idx); };
490  if (llvm::all_of(ShiftAmts, OutOfRange)) {
491  SmallVector<Constant *, 8> ConstantVec;
492  for (int Idx : ShiftAmts) {
493  if (Idx < 0) {
494  ConstantVec.push_back(UndefValue::get(SVT));
495  } else {
496  assert(LogicalShift && "Logical shift expected");
497  ConstantVec.push_back(ConstantInt::getNullValue(SVT));
498  }
499  }
500  return ConstantVector::get(ConstantVec);
501  }
502 
503  // We can't handle only some out of range values with generic logical shifts.
504  if (AnyOutOfRange)
505  return nullptr;
506 
507  // Build the shift amount constant vector.
508  SmallVector<Constant *, 8> ShiftVecAmts;
509  for (int Idx : ShiftAmts) {
510  if (Idx < 0)
511  ShiftVecAmts.push_back(UndefValue::get(SVT));
512  else
513  ShiftVecAmts.push_back(ConstantInt::get(SVT, Idx));
514  }
515  auto ShiftVec = ConstantVector::get(ShiftVecAmts);
516 
517  if (ShiftLeft)
518  return Builder.CreateShl(Vec, ShiftVec);
519 
520  if (LogicalShift)
521  return Builder.CreateLShr(Vec, ShiftVec);
522 
523  return Builder.CreateAShr(Vec, ShiftVec);
524 }
525 
526 static Value *simplifyX86pack(IntrinsicInst &II, bool IsSigned) {
527  Value *Arg0 = II.getArgOperand(0);
528  Value *Arg1 = II.getArgOperand(1);
529  Type *ResTy = II.getType();
530 
531  // Fast all undef handling.
532  if (isa<UndefValue>(Arg0) && isa<UndefValue>(Arg1))
533  return UndefValue::get(ResTy);
534 
535  Type *ArgTy = Arg0->getType();
536  unsigned NumLanes = ResTy->getPrimitiveSizeInBits() / 128;
537  unsigned NumDstElts = ResTy->getVectorNumElements();
538  unsigned NumSrcElts = ArgTy->getVectorNumElements();
539  assert(NumDstElts == (2 * NumSrcElts) && "Unexpected packing types");
540 
541  unsigned NumDstEltsPerLane = NumDstElts / NumLanes;
542  unsigned NumSrcEltsPerLane = NumSrcElts / NumLanes;
543  unsigned DstScalarSizeInBits = ResTy->getScalarSizeInBits();
544  assert(ArgTy->getScalarSizeInBits() == (2 * DstScalarSizeInBits) &&
545  "Unexpected packing types");
546 
547  // Constant folding.
548  auto *Cst0 = dyn_cast<Constant>(Arg0);
549  auto *Cst1 = dyn_cast<Constant>(Arg1);
550  if (!Cst0 || !Cst1)
551  return nullptr;
552 
554  for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
555  for (unsigned Elt = 0; Elt != NumDstEltsPerLane; ++Elt) {
556  unsigned SrcIdx = Lane * NumSrcEltsPerLane + Elt % NumSrcEltsPerLane;
557  auto *Cst = (Elt >= NumSrcEltsPerLane) ? Cst1 : Cst0;
558  auto *COp = Cst->getAggregateElement(SrcIdx);
559  if (COp && isa<UndefValue>(COp)) {
560  Vals.push_back(UndefValue::get(ResTy->getScalarType()));
561  continue;
562  }
563 
564  auto *CInt = dyn_cast_or_null<ConstantInt>(COp);
565  if (!CInt)
566  return nullptr;
567 
568  APInt Val = CInt->getValue();
569  assert(Val.getBitWidth() == ArgTy->getScalarSizeInBits() &&
570  "Unexpected constant bitwidth");
571 
572  if (IsSigned) {
573  // PACKSS: Truncate signed value with signed saturation.
574  // Source values less than dst minint are saturated to minint.
575  // Source values greater than dst maxint are saturated to maxint.
576  if (Val.isSignedIntN(DstScalarSizeInBits))
577  Val = Val.trunc(DstScalarSizeInBits);
578  else if (Val.isNegative())
579  Val = APInt::getSignedMinValue(DstScalarSizeInBits);
580  else
581  Val = APInt::getSignedMaxValue(DstScalarSizeInBits);
582  } else {
583  // PACKUS: Truncate signed value with unsigned saturation.
584  // Source values less than zero are saturated to zero.
585  // Source values greater than dst maxuint are saturated to maxuint.
586  if (Val.isIntN(DstScalarSizeInBits))
587  Val = Val.trunc(DstScalarSizeInBits);
588  else if (Val.isNegative())
589  Val = APInt::getNullValue(DstScalarSizeInBits);
590  else
591  Val = APInt::getAllOnesValue(DstScalarSizeInBits);
592  }
593 
594  Vals.push_back(ConstantInt::get(ResTy->getScalarType(), Val));
595  }
596  }
597 
598  return ConstantVector::get(Vals);
599 }
600 
601 // Replace X86-specific intrinsics with generic floor-ceil where applicable.
603  InstCombiner::BuilderTy &Builder) {
604  ConstantInt *Arg = nullptr;
605  Intrinsic::ID IntrinsicID = II.getIntrinsicID();
606 
607  if (IntrinsicID == Intrinsic::x86_sse41_round_ss ||
608  IntrinsicID == Intrinsic::x86_sse41_round_sd)
609  Arg = dyn_cast<ConstantInt>(II.getArgOperand(2));
610  else if (IntrinsicID == Intrinsic::x86_avx512_mask_rndscale_ss ||
612  Arg = dyn_cast<ConstantInt>(II.getArgOperand(4));
613  else
614  Arg = dyn_cast<ConstantInt>(II.getArgOperand(1));
615  if (!Arg)
616  return nullptr;
617  unsigned RoundControl = Arg->getZExtValue();
618 
619  Arg = nullptr;
620  unsigned SAE = 0;
621  if (IntrinsicID == Intrinsic::x86_avx512_mask_rndscale_ps_512 ||
623  Arg = dyn_cast<ConstantInt>(II.getArgOperand(4));
624  else if (IntrinsicID == Intrinsic::x86_avx512_mask_rndscale_ss ||
626  Arg = dyn_cast<ConstantInt>(II.getArgOperand(5));
627  else
628  SAE = 4;
629  if (!SAE) {
630  if (!Arg)
631  return nullptr;
632  SAE = Arg->getZExtValue();
633  }
634 
635  if (SAE != 4 || (RoundControl != 2 /*ceil*/ && RoundControl != 1 /*floor*/))
636  return nullptr;
637 
638  Value *Src, *Dst, *Mask;
639  bool IsScalar = false;
640  if (IntrinsicID == Intrinsic::x86_sse41_round_ss ||
641  IntrinsicID == Intrinsic::x86_sse41_round_sd ||
644  IsScalar = true;
645  if (IntrinsicID == Intrinsic::x86_avx512_mask_rndscale_ss ||
647  Mask = II.getArgOperand(3);
648  Value *Zero = Constant::getNullValue(Mask->getType());
649  Mask = Builder.CreateAnd(Mask, 1);
650  Mask = Builder.CreateICmp(ICmpInst::ICMP_NE, Mask, Zero);
651  Dst = II.getArgOperand(2);
652  } else
653  Dst = II.getArgOperand(0);
654  Src = Builder.CreateExtractElement(II.getArgOperand(1), (uint64_t)0);
655  } else {
656  Src = II.getArgOperand(0);
657  if (IntrinsicID == Intrinsic::x86_avx512_mask_rndscale_ps_128 ||
663  Dst = II.getArgOperand(2);
664  Mask = II.getArgOperand(3);
665  } else {
666  Dst = Src;
668  Builder.getIntNTy(Src->getType()->getVectorNumElements()));
669  }
670  }
671 
672  Intrinsic::ID ID = (RoundControl == 2) ? Intrinsic::ceil : Intrinsic::floor;
673  Value *Res = Builder.CreateUnaryIntrinsic(ID, Src, &II);
674  if (!IsScalar) {
675  if (auto *C = dyn_cast<Constant>(Mask))
676  if (C->isAllOnesValue())
677  return Res;
678  auto *MaskTy = VectorType::get(
679  Builder.getInt1Ty(), cast<IntegerType>(Mask->getType())->getBitWidth());
680  Mask = Builder.CreateBitCast(Mask, MaskTy);
681  unsigned Width = Src->getType()->getVectorNumElements();
682  if (MaskTy->getVectorNumElements() > Width) {
683  uint32_t Indices[4];
684  for (unsigned i = 0; i != Width; ++i)
685  Indices[i] = i;
686  Mask = Builder.CreateShuffleVector(Mask, Mask,
687  makeArrayRef(Indices, Width));
688  }
689  return Builder.CreateSelect(Mask, Res, Dst);
690  }
691  if (IntrinsicID == Intrinsic::x86_avx512_mask_rndscale_ss ||
693  Dst = Builder.CreateExtractElement(Dst, (uint64_t)0);
694  Res = Builder.CreateSelect(Mask, Res, Dst);
695  Dst = II.getArgOperand(0);
696  }
697  return Builder.CreateInsertElement(Dst, Res, (uint64_t)0);
698 }
699 
701  InstCombiner::BuilderTy &Builder) {
702  Value *Arg = II.getArgOperand(0);
703  Type *ResTy = II.getType();
704  Type *ArgTy = Arg->getType();
705 
706  // movmsk(undef) -> zero as we must ensure the upper bits are zero.
707  if (isa<UndefValue>(Arg))
708  return Constant::getNullValue(ResTy);
709 
710  // We can't easily peek through x86_mmx types.
711  if (!ArgTy->isVectorTy())
712  return nullptr;
713 
714  if (auto *C = dyn_cast<Constant>(Arg)) {
715  // Extract signbits of the vector input and pack into integer result.
716  APInt Result(ResTy->getPrimitiveSizeInBits(), 0);
717  for (unsigned I = 0, E = ArgTy->getVectorNumElements(); I != E; ++I) {
718  auto *COp = C->getAggregateElement(I);
719  if (!COp)
720  return nullptr;
721  if (isa<UndefValue>(COp))
722  continue;
723 
724  auto *CInt = dyn_cast<ConstantInt>(COp);
725  auto *CFp = dyn_cast<ConstantFP>(COp);
726  if (!CInt && !CFp)
727  return nullptr;
728 
729  if ((CInt && CInt->isNegative()) || (CFp && CFp->isNegative()))
730  Result.setBit(I);
731  }
732  return Constant::getIntegerValue(ResTy, Result);
733  }
734 
735  // Look for a sign-extended boolean source vector as the argument to this
736  // movmsk. If the argument is bitcast, look through that, but make sure the
737  // source of that bitcast is still a vector with the same number of elements.
738  // TODO: We can also convert a bitcast with wider elements, but that requires
739  // duplicating the bool source sign bits to match the number of elements
740  // expected by the movmsk call.
741  Arg = peekThroughBitcast(Arg);
742  Value *X;
743  if (Arg->getType()->isVectorTy() &&
744  Arg->getType()->getVectorNumElements() == ArgTy->getVectorNumElements() &&
745  match(Arg, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
746  // call iM movmsk(sext <N x i1> X) --> zext (bitcast <N x i1> X to iN) to iM
747  unsigned NumElts = X->getType()->getVectorNumElements();
748  Type *ScalarTy = Type::getIntNTy(Arg->getContext(), NumElts);
749  Value *BC = Builder.CreateBitCast(X, ScalarTy);
750  return Builder.CreateZExtOrTrunc(BC, ResTy);
751  }
752 
753  return nullptr;
754 }
755 
757  InstCombiner::BuilderTy &Builder) {
758  auto *CInt = dyn_cast<ConstantInt>(II.getArgOperand(2));
759  if (!CInt)
760  return nullptr;
761 
762  VectorType *VecTy = cast<VectorType>(II.getType());
763  assert(VecTy->getNumElements() == 4 && "insertps with wrong vector type");
764 
765  // The immediate permute control byte looks like this:
766  // [3:0] - zero mask for each 32-bit lane
767  // [5:4] - select one 32-bit destination lane
768  // [7:6] - select one 32-bit source lane
769 
770  uint8_t Imm = CInt->getZExtValue();
771  uint8_t ZMask = Imm & 0xf;
772  uint8_t DestLane = (Imm >> 4) & 0x3;
773  uint8_t SourceLane = (Imm >> 6) & 0x3;
774 
776 
777  // If all zero mask bits are set, this was just a weird way to
778  // generate a zero vector.
779  if (ZMask == 0xf)
780  return ZeroVector;
781 
782  // Initialize by passing all of the first source bits through.
783  uint32_t ShuffleMask[4] = { 0, 1, 2, 3 };
784 
785  // We may replace the second operand with the zero vector.
786  Value *V1 = II.getArgOperand(1);
787 
788  if (ZMask) {
789  // If the zero mask is being used with a single input or the zero mask
790  // overrides the destination lane, this is a shuffle with the zero vector.
791  if ((II.getArgOperand(0) == II.getArgOperand(1)) ||
792  (ZMask & (1 << DestLane))) {
793  V1 = ZeroVector;
794  // We may still move 32-bits of the first source vector from one lane
795  // to another.
796  ShuffleMask[DestLane] = SourceLane;
797  // The zero mask may override the previous insert operation.
798  for (unsigned i = 0; i < 4; ++i)
799  if ((ZMask >> i) & 0x1)
800  ShuffleMask[i] = i + 4;
801  } else {
802  // TODO: Model this case as 2 shuffles or a 'logical and' plus shuffle?
803  return nullptr;
804  }
805  } else {
806  // Replace the selected destination lane with the selected source lane.
807  ShuffleMask[DestLane] = SourceLane + 4;
808  }
809 
810  return Builder.CreateShuffleVector(II.getArgOperand(0), V1, ShuffleMask);
811 }
812 
813 /// Attempt to simplify SSE4A EXTRQ/EXTRQI instructions using constant folding
814 /// or conversion to a shuffle vector.
816  ConstantInt *CILength, ConstantInt *CIIndex,
817  InstCombiner::BuilderTy &Builder) {
818  auto LowConstantHighUndef = [&](uint64_t Val) {
819  Type *IntTy64 = Type::getInt64Ty(II.getContext());
820  Constant *Args[] = {ConstantInt::get(IntTy64, Val),
821  UndefValue::get(IntTy64)};
822  return ConstantVector::get(Args);
823  };
824 
825  // See if we're dealing with constant values.
826  Constant *C0 = dyn_cast<Constant>(Op0);
827  ConstantInt *CI0 =
828  C0 ? dyn_cast_or_null<ConstantInt>(C0->getAggregateElement((unsigned)0))
829  : nullptr;
830 
831  // Attempt to constant fold.
832  if (CILength && CIIndex) {
833  // From AMD documentation: "The bit index and field length are each six
834  // bits in length other bits of the field are ignored."
835  APInt APIndex = CIIndex->getValue().zextOrTrunc(6);
836  APInt APLength = CILength->getValue().zextOrTrunc(6);
837 
838  unsigned Index = APIndex.getZExtValue();
839 
840  // From AMD documentation: "a value of zero in the field length is
841  // defined as length of 64".
842  unsigned Length = APLength == 0 ? 64 : APLength.getZExtValue();
843 
844  // From AMD documentation: "If the sum of the bit index + length field
845  // is greater than 64, the results are undefined".
846  unsigned End = Index + Length;
847 
848  // Note that both field index and field length are 8-bit quantities.
849  // Since variables 'Index' and 'Length' are unsigned values
850  // obtained from zero-extending field index and field length
851  // respectively, their sum should never wrap around.
852  if (End > 64)
853  return UndefValue::get(II.getType());
854 
855  // If we are inserting whole bytes, we can convert this to a shuffle.
856  // Lowering can recognize EXTRQI shuffle masks.
857  if ((Length % 8) == 0 && (Index % 8) == 0) {
858  // Convert bit indices to byte indices.
859  Length /= 8;
860  Index /= 8;
861 
862  Type *IntTy8 = Type::getInt8Ty(II.getContext());
863  Type *IntTy32 = Type::getInt32Ty(II.getContext());
864  VectorType *ShufTy = VectorType::get(IntTy8, 16);
865 
866  SmallVector<Constant *, 16> ShuffleMask;
867  for (int i = 0; i != (int)Length; ++i)
868  ShuffleMask.push_back(
869  Constant::getIntegerValue(IntTy32, APInt(32, i + Index)));
870  for (int i = Length; i != 8; ++i)
871  ShuffleMask.push_back(
872  Constant::getIntegerValue(IntTy32, APInt(32, i + 16)));
873  for (int i = 8; i != 16; ++i)
874  ShuffleMask.push_back(UndefValue::get(IntTy32));
875 
876  Value *SV = Builder.CreateShuffleVector(
877  Builder.CreateBitCast(Op0, ShufTy),
878  ConstantAggregateZero::get(ShufTy), ConstantVector::get(ShuffleMask));
879  return Builder.CreateBitCast(SV, II.getType());
880  }
881 
882  // Constant Fold - shift Index'th bit to lowest position and mask off
883  // Length bits.
884  if (CI0) {
885  APInt Elt = CI0->getValue();
886  Elt.lshrInPlace(Index);
887  Elt = Elt.zextOrTrunc(Length);
888  return LowConstantHighUndef(Elt.getZExtValue());
889  }
890 
891  // If we were an EXTRQ call, we'll save registers if we convert to EXTRQI.
893  Value *Args[] = {Op0, CILength, CIIndex};
894  Module *M = II.getModule();
896  return Builder.CreateCall(F, Args);
897  }
898  }
899 
900  // Constant Fold - extraction from zero is always {zero, undef}.
901  if (CI0 && CI0->isZero())
902  return LowConstantHighUndef(0);
903 
904  return nullptr;
905 }
906 
907 /// Attempt to simplify SSE4A INSERTQ/INSERTQI instructions using constant
908 /// folding or conversion to a shuffle vector.
910  APInt APLength, APInt APIndex,
911  InstCombiner::BuilderTy &Builder) {
912  // From AMD documentation: "The bit index and field length are each six bits
913  // in length other bits of the field are ignored."
914  APIndex = APIndex.zextOrTrunc(6);
915  APLength = APLength.zextOrTrunc(6);
916 
917  // Attempt to constant fold.
918  unsigned Index = APIndex.getZExtValue();
919 
920  // From AMD documentation: "a value of zero in the field length is
921  // defined as length of 64".
922  unsigned Length = APLength == 0 ? 64 : APLength.getZExtValue();
923 
924  // From AMD documentation: "If the sum of the bit index + length field
925  // is greater than 64, the results are undefined".
926  unsigned End = Index + Length;
927 
928  // Note that both field index and field length are 8-bit quantities.
929  // Since variables 'Index' and 'Length' are unsigned values
930  // obtained from zero-extending field index and field length
931  // respectively, their sum should never wrap around.
932  if (End > 64)
933  return UndefValue::get(II.getType());
934 
935  // If we are inserting whole bytes, we can convert this to a shuffle.
936  // Lowering can recognize INSERTQI shuffle masks.
937  if ((Length % 8) == 0 && (Index % 8) == 0) {
938  // Convert bit indices to byte indices.
939  Length /= 8;
940  Index /= 8;
941 
942  Type *IntTy8 = Type::getInt8Ty(II.getContext());
943  Type *IntTy32 = Type::getInt32Ty(II.getContext());
944  VectorType *ShufTy = VectorType::get(IntTy8, 16);
945 
946  SmallVector<Constant *, 16> ShuffleMask;
947  for (int i = 0; i != (int)Index; ++i)
948  ShuffleMask.push_back(Constant::getIntegerValue(IntTy32, APInt(32, i)));
949  for (int i = 0; i != (int)Length; ++i)
950  ShuffleMask.push_back(
951  Constant::getIntegerValue(IntTy32, APInt(32, i + 16)));
952  for (int i = Index + Length; i != 8; ++i)
953  ShuffleMask.push_back(Constant::getIntegerValue(IntTy32, APInt(32, i)));
954  for (int i = 8; i != 16; ++i)
955  ShuffleMask.push_back(UndefValue::get(IntTy32));
956 
957  Value *SV = Builder.CreateShuffleVector(Builder.CreateBitCast(Op0, ShufTy),
958  Builder.CreateBitCast(Op1, ShufTy),
959  ConstantVector::get(ShuffleMask));
960  return Builder.CreateBitCast(SV, II.getType());
961  }
962 
963  // See if we're dealing with constant values.
964  Constant *C0 = dyn_cast<Constant>(Op0);
965  Constant *C1 = dyn_cast<Constant>(Op1);
966  ConstantInt *CI00 =
967  C0 ? dyn_cast_or_null<ConstantInt>(C0->getAggregateElement((unsigned)0))
968  : nullptr;
969  ConstantInt *CI10 =
970  C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)0))
971  : nullptr;
972 
973  // Constant Fold - insert bottom Length bits starting at the Index'th bit.
974  if (CI00 && CI10) {
975  APInt V00 = CI00->getValue();
976  APInt V10 = CI10->getValue();
977  APInt Mask = APInt::getLowBitsSet(64, Length).shl(Index);
978  V00 = V00 & ~Mask;
979  V10 = V10.zextOrTrunc(Length).zextOrTrunc(64).shl(Index);
980  APInt Val = V00 | V10;
981  Type *IntTy64 = Type::getInt64Ty(II.getContext());
982  Constant *Args[] = {ConstantInt::get(IntTy64, Val.getZExtValue()),
983  UndefValue::get(IntTy64)};
984  return ConstantVector::get(Args);
985  }
986 
987  // If we were an INSERTQ call, we'll save demanded elements if we convert to
988  // INSERTQI.
990  Type *IntTy8 = Type::getInt8Ty(II.getContext());
991  Constant *CILength = ConstantInt::get(IntTy8, Length, false);
992  Constant *CIIndex = ConstantInt::get(IntTy8, Index, false);
993 
994  Value *Args[] = {Op0, Op1, CILength, CIIndex};
995  Module *M = II.getModule();
997  return Builder.CreateCall(F, Args);
998  }
999 
1000  return nullptr;
1001 }
1002 
1003 /// Attempt to convert pshufb* to shufflevector if the mask is constant.
1005  InstCombiner::BuilderTy &Builder) {
1006  Constant *V = dyn_cast<Constant>(II.getArgOperand(1));
1007  if (!V)
1008  return nullptr;
1009 
1010  auto *VecTy = cast<VectorType>(II.getType());
1011  auto *MaskEltTy = Type::getInt32Ty(II.getContext());
1012  unsigned NumElts = VecTy->getNumElements();
1013  assert((NumElts == 16 || NumElts == 32 || NumElts == 64) &&
1014  "Unexpected number of elements in shuffle mask!");
1015 
1016  // Construct a shuffle mask from constant integers or UNDEFs.
1017  Constant *Indexes[64] = {nullptr};
1018 
1019  // Each byte in the shuffle control mask forms an index to permute the
1020  // corresponding byte in the destination operand.
1021  for (unsigned I = 0; I < NumElts; ++I) {
1022  Constant *COp = V->getAggregateElement(I);
1023  if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
1024  return nullptr;
1025 
1026  if (isa<UndefValue>(COp)) {
1027  Indexes[I] = UndefValue::get(MaskEltTy);
1028  continue;
1029  }
1030 
1031  int8_t Index = cast<ConstantInt>(COp)->getValue().getZExtValue();
1032 
1033  // If the most significant bit (bit[7]) of each byte of the shuffle
1034  // control mask is set, then zero is written in the result byte.
1035  // The zero vector is in the right-hand side of the resulting
1036  // shufflevector.
1037 
1038  // The value of each index for the high 128-bit lane is the least
1039  // significant 4 bits of the respective shuffle control byte.
1040  Index = ((Index < 0) ? NumElts : Index & 0x0F) + (I & 0xF0);
1041  Indexes[I] = ConstantInt::get(MaskEltTy, Index);
1042  }
1043 
1044  auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, NumElts));
1045  auto V1 = II.getArgOperand(0);
1046  auto V2 = Constant::getNullValue(VecTy);
1047  return Builder.CreateShuffleVector(V1, V2, ShuffleMask);
1048 }
1049 
1050 /// Attempt to convert vpermilvar* to shufflevector if the mask is constant.
1052  InstCombiner::BuilderTy &Builder) {
1053  Constant *V = dyn_cast<Constant>(II.getArgOperand(1));
1054  if (!V)
1055  return nullptr;
1056 
1057  auto *VecTy = cast<VectorType>(II.getType());
1058  auto *MaskEltTy = Type::getInt32Ty(II.getContext());
1059  unsigned NumElts = VecTy->getVectorNumElements();
1060  bool IsPD = VecTy->getScalarType()->isDoubleTy();
1061  unsigned NumLaneElts = IsPD ? 2 : 4;
1062  assert(NumElts == 16 || NumElts == 8 || NumElts == 4 || NumElts == 2);
1063 
1064  // Construct a shuffle mask from constant integers or UNDEFs.
1065  Constant *Indexes[16] = {nullptr};
1066 
1067  // The intrinsics only read one or two bits, clear the rest.
1068  for (unsigned I = 0; I < NumElts; ++I) {
1069  Constant *COp = V->getAggregateElement(I);
1070  if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
1071  return nullptr;
1072 
1073  if (isa<UndefValue>(COp)) {
1074  Indexes[I] = UndefValue::get(MaskEltTy);
1075  continue;
1076  }
1077 
1078  APInt Index = cast<ConstantInt>(COp)->getValue();
1079  Index = Index.zextOrTrunc(32).getLoBits(2);
1080 
1081  // The PD variants uses bit 1 to select per-lane element index, so
1082  // shift down to convert to generic shuffle mask index.
1083  if (IsPD)
1084  Index.lshrInPlace(1);
1085 
1086  // The _256 variants are a bit trickier since the mask bits always index
1087  // into the corresponding 128 half. In order to convert to a generic
1088  // shuffle, we have to make that explicit.
1089  Index += APInt(32, (I / NumLaneElts) * NumLaneElts);
1090 
1091  Indexes[I] = ConstantInt::get(MaskEltTy, Index);
1092  }
1093 
1094  auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, NumElts));
1095  auto V1 = II.getArgOperand(0);
1096  auto V2 = UndefValue::get(V1->getType());
1097  return Builder.CreateShuffleVector(V1, V2, ShuffleMask);
1098 }
1099 
1100 /// Attempt to convert vpermd/vpermps to shufflevector if the mask is constant.
1102  InstCombiner::BuilderTy &Builder) {
1103  auto *V = dyn_cast<Constant>(II.getArgOperand(1));
1104  if (!V)
1105  return nullptr;
1106 
1107  auto *VecTy = cast<VectorType>(II.getType());
1108  auto *MaskEltTy = Type::getInt32Ty(II.getContext());
1109  unsigned Size = VecTy->getNumElements();
1110  assert((Size == 4 || Size == 8 || Size == 16 || Size == 32 || Size == 64) &&
1111  "Unexpected shuffle mask size");
1112 
1113  // Construct a shuffle mask from constant integers or UNDEFs.
1114  Constant *Indexes[64] = {nullptr};
1115 
1116  for (unsigned I = 0; I < Size; ++I) {
1117  Constant *COp = V->getAggregateElement(I);
1118  if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
1119  return nullptr;
1120 
1121  if (isa<UndefValue>(COp)) {
1122  Indexes[I] = UndefValue::get(MaskEltTy);
1123  continue;
1124  }
1125 
1126  uint32_t Index = cast<ConstantInt>(COp)->getZExtValue();
1127  Index &= Size - 1;
1128  Indexes[I] = ConstantInt::get(MaskEltTy, Index);
1129  }
1130 
1131  auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, Size));
1132  auto V1 = II.getArgOperand(0);
1133  auto V2 = UndefValue::get(VecTy);
1134  return Builder.CreateShuffleVector(V1, V2, ShuffleMask);
1135 }
1136 
1137 /// Decode XOP integer vector comparison intrinsics.
1139  InstCombiner::BuilderTy &Builder,
1140  bool IsSigned) {
1141  if (auto *CInt = dyn_cast<ConstantInt>(II.getArgOperand(2))) {
1142  uint64_t Imm = CInt->getZExtValue() & 0x7;
1143  VectorType *VecTy = cast<VectorType>(II.getType());
1145 
1146  switch (Imm) {
1147  case 0x0:
1148  Pred = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1149  break;
1150  case 0x1:
1151  Pred = IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
1152  break;
1153  case 0x2:
1154  Pred = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1155  break;
1156  case 0x3:
1157  Pred = IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
1158  break;
1159  case 0x4:
1160  Pred = ICmpInst::ICMP_EQ; break;
1161  case 0x5:
1162  Pred = ICmpInst::ICMP_NE; break;
1163  case 0x6:
1164  return ConstantInt::getSigned(VecTy, 0); // FALSE
1165  case 0x7:
1166  return ConstantInt::getSigned(VecTy, -1); // TRUE
1167  }
1168 
1169  if (Value *Cmp = Builder.CreateICmp(Pred, II.getArgOperand(0),
1170  II.getArgOperand(1)))
1171  return Builder.CreateSExtOrTrunc(Cmp, VecTy);
1172  }
1173  return nullptr;
1174 }
1175 
1177  auto *ConstMask = dyn_cast<Constant>(Mask);
1178  if (!ConstMask)
1179  return false;
1180  if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask))
1181  return true;
1182  for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E;
1183  ++I) {
1184  if (auto *MaskElt = ConstMask->getAggregateElement(I))
1185  if (MaskElt->isAllOnesValue() || isa<UndefValue>(MaskElt))
1186  continue;
1187  return false;
1188  }
1189  return true;
1190 }
1191 
1193  InstCombiner::BuilderTy &Builder) {
1194  // If the mask is all ones or undefs, this is a plain vector load of the 1st
1195  // argument.
1196  if (maskIsAllOneOrUndef(II.getArgOperand(2))) {
1197  Value *LoadPtr = II.getArgOperand(0);
1198  unsigned Alignment = cast<ConstantInt>(II.getArgOperand(1))->getZExtValue();
1199  return Builder.CreateAlignedLoad(LoadPtr, Alignment, "unmaskedload");
1200  }
1201 
1202  return nullptr;
1203 }
1204 
1206  auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3));
1207  if (!ConstMask)
1208  return nullptr;
1209 
1210  // If the mask is all zeros, this instruction does nothing.
1211  if (ConstMask->isNullValue())
1212  return IC.eraseInstFromFunction(II);
1213 
1214  // If the mask is all ones, this is a plain vector store of the 1st argument.
1215  if (ConstMask->isAllOnesValue()) {
1216  Value *StorePtr = II.getArgOperand(1);
1217  unsigned Alignment = cast<ConstantInt>(II.getArgOperand(2))->getZExtValue();
1218  return new StoreInst(II.getArgOperand(0), StorePtr, false, Alignment);
1219  }
1220 
1221  return nullptr;
1222 }
1223 
1225  // If the mask is all zeros, return the "passthru" argument of the gather.
1226  auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(2));
1227  if (ConstMask && ConstMask->isNullValue())
1228  return IC.replaceInstUsesWith(II, II.getArgOperand(3));
1229 
1230  return nullptr;
1231 }
1232 
1233 /// This function transforms launder.invariant.group and strip.invariant.group
1234 /// like:
1235 /// launder(launder(%x)) -> launder(%x) (the result is not the argument)
1236 /// launder(strip(%x)) -> launder(%x)
1237 /// strip(strip(%x)) -> strip(%x) (the result is not the argument)
1238 /// strip(launder(%x)) -> strip(%x)
1239 /// This is legal because it preserves the most recent information about
1240 /// the presence or absence of invariant.group.
1242  InstCombiner &IC) {
1243  auto *Arg = II.getArgOperand(0);
1244  auto *StrippedArg = Arg->stripPointerCasts();
1245  auto *StrippedInvariantGroupsArg = Arg->stripPointerCastsAndInvariantGroups();
1246  if (StrippedArg == StrippedInvariantGroupsArg)
1247  return nullptr; // No launders/strips to remove.
1248 
1249  Value *Result = nullptr;
1250 
1252  Result = IC.Builder.CreateLaunderInvariantGroup(StrippedInvariantGroupsArg);
1254  Result = IC.Builder.CreateStripInvariantGroup(StrippedInvariantGroupsArg);
1255  else
1257  "simplifyInvariantGroupIntrinsic only handles launder and strip");
1258  if (Result->getType()->getPointerAddressSpace() !=
1260  Result = IC.Builder.CreateAddrSpaceCast(Result, II.getType());
1261  if (Result->getType() != II.getType())
1262  Result = IC.Builder.CreateBitCast(Result, II.getType());
1263 
1264  return cast<Instruction>(Result);
1265 }
1266 
1268  // If the mask is all zeros, a scatter does nothing.
1269  auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3));
1270  if (ConstMask && ConstMask->isNullValue())
1271  return IC.eraseInstFromFunction(II);
1272 
1273  return nullptr;
1274 }
1275 
1278  II.getIntrinsicID() == Intrinsic::ctlz) &&
1279  "Expected cttz or ctlz intrinsic");
1280  Value *Op0 = II.getArgOperand(0);
1281 
1282  KnownBits Known = IC.computeKnownBits(Op0, 0, &II);
1283 
1284  // Create a mask for bits above (ctlz) or below (cttz) the first known one.
1285  bool IsTZ = II.getIntrinsicID() == Intrinsic::cttz;
1286  unsigned PossibleZeros = IsTZ ? Known.countMaxTrailingZeros()
1287  : Known.countMaxLeadingZeros();
1288  unsigned DefiniteZeros = IsTZ ? Known.countMinTrailingZeros()
1289  : Known.countMinLeadingZeros();
1290 
1291  // If all bits above (ctlz) or below (cttz) the first known one are known
1292  // zero, this value is constant.
1293  // FIXME: This should be in InstSimplify because we're replacing an
1294  // instruction with a constant.
1295  if (PossibleZeros == DefiniteZeros) {
1296  auto *C = ConstantInt::get(Op0->getType(), DefiniteZeros);
1297  return IC.replaceInstUsesWith(II, C);
1298  }
1299 
1300  // If the input to cttz/ctlz is known to be non-zero,
1301  // then change the 'ZeroIsUndef' parameter to 'true'
1302  // because we know the zero behavior can't affect the result.
1303  if (!Known.One.isNullValue() ||
1304  isKnownNonZero(Op0, IC.getDataLayout(), 0, &IC.getAssumptionCache(), &II,
1305  &IC.getDominatorTree())) {
1306  if (!match(II.getArgOperand(1), m_One())) {
1307  II.setOperand(1, IC.Builder.getTrue());
1308  return &II;
1309  }
1310  }
1311 
1312  // Add range metadata since known bits can't completely reflect what we know.
1313  // TODO: Handle splat vectors.
1314  auto *IT = dyn_cast<IntegerType>(Op0->getType());
1315  if (IT && IT->getBitWidth() != 1 && !II.getMetadata(LLVMContext::MD_range)) {
1316  Metadata *LowAndHigh[] = {
1317  ConstantAsMetadata::get(ConstantInt::get(IT, DefiniteZeros)),
1318  ConstantAsMetadata::get(ConstantInt::get(IT, PossibleZeros + 1))};
1321  return &II;
1322  }
1323 
1324  return nullptr;
1325 }
1326 
1329  "Expected ctpop intrinsic");
1330  Value *Op0 = II.getArgOperand(0);
1331  // FIXME: Try to simplify vectors of integers.
1332  auto *IT = dyn_cast<IntegerType>(Op0->getType());
1333  if (!IT)
1334  return nullptr;
1335 
1336  unsigned BitWidth = IT->getBitWidth();
1337  KnownBits Known(BitWidth);
1338  IC.computeKnownBits(Op0, Known, 0, &II);
1339 
1340  unsigned MinCount = Known.countMinPopulation();
1341  unsigned MaxCount = Known.countMaxPopulation();
1342 
1343  // Add range metadata since known bits can't completely reflect what we know.
1344  if (IT->getBitWidth() != 1 && !II.getMetadata(LLVMContext::MD_range)) {
1345  Metadata *LowAndHigh[] = {
1347  ConstantAsMetadata::get(ConstantInt::get(IT, MaxCount + 1))};
1350  return &II;
1351  }
1352 
1353  return nullptr;
1354 }
1355 
1356 // TODO: If the x86 backend knew how to convert a bool vector mask back to an
1357 // XMM register mask efficiently, we could transform all x86 masked intrinsics
1358 // to LLVM masked intrinsics and remove the x86 masked intrinsic defs.
1360  Value *Ptr = II.getOperand(0);
1361  Value *Mask = II.getOperand(1);
1362  Constant *ZeroVec = Constant::getNullValue(II.getType());
1363 
1364  // Special case a zero mask since that's not a ConstantDataVector.
1365  // This masked load instruction creates a zero vector.
1366  if (isa<ConstantAggregateZero>(Mask))
1367  return IC.replaceInstUsesWith(II, ZeroVec);
1368 
1369  auto *ConstMask = dyn_cast<ConstantDataVector>(Mask);
1370  if (!ConstMask)
1371  return nullptr;
1372 
1373  // The mask is constant. Convert this x86 intrinsic to the LLVM instrinsic
1374  // to allow target-independent optimizations.
1375 
1376  // First, cast the x86 intrinsic scalar pointer to a vector pointer to match
1377  // the LLVM intrinsic definition for the pointer argument.
1378  unsigned AddrSpace = cast<PointerType>(Ptr->getType())->getAddressSpace();
1379  PointerType *VecPtrTy = PointerType::get(II.getType(), AddrSpace);
1380  Value *PtrCast = IC.Builder.CreateBitCast(Ptr, VecPtrTy, "castvec");
1381 
1382  // Second, convert the x86 XMM integer vector mask to a vector of bools based
1383  // on each element's most significant bit (the sign bit).
1384  Constant *BoolMask = getNegativeIsTrueBoolVec(ConstMask);
1385 
1386  // The pass-through vector for an x86 masked load is a zero vector.
1387  CallInst *NewMaskedLoad =
1388  IC.Builder.CreateMaskedLoad(PtrCast, 1, BoolMask, ZeroVec);
1389  return IC.replaceInstUsesWith(II, NewMaskedLoad);
1390 }
1391 
1392 // TODO: If the x86 backend knew how to convert a bool vector mask back to an
1393 // XMM register mask efficiently, we could transform all x86 masked intrinsics
1394 // to LLVM masked intrinsics and remove the x86 masked intrinsic defs.
1396  Value *Ptr = II.getOperand(0);
1397  Value *Mask = II.getOperand(1);
1398  Value *Vec = II.getOperand(2);
1399 
1400  // Special case a zero mask since that's not a ConstantDataVector:
1401  // this masked store instruction does nothing.
1402  if (isa<ConstantAggregateZero>(Mask)) {
1403  IC.eraseInstFromFunction(II);
1404  return true;
1405  }
1406 
1407  // The SSE2 version is too weird (eg, unaligned but non-temporal) to do
1408  // anything else at this level.
1410  return false;
1411 
1412  auto *ConstMask = dyn_cast<ConstantDataVector>(Mask);
1413  if (!ConstMask)
1414  return false;
1415 
1416  // The mask is constant. Convert this x86 intrinsic to the LLVM instrinsic
1417  // to allow target-independent optimizations.
1418 
1419  // First, cast the x86 intrinsic scalar pointer to a vector pointer to match
1420  // the LLVM intrinsic definition for the pointer argument.
1421  unsigned AddrSpace = cast<PointerType>(Ptr->getType())->getAddressSpace();
1422  PointerType *VecPtrTy = PointerType::get(Vec->getType(), AddrSpace);
1423  Value *PtrCast = IC.Builder.CreateBitCast(Ptr, VecPtrTy, "castvec");
1424 
1425  // Second, convert the x86 XMM integer vector mask to a vector of bools based
1426  // on each element's most significant bit (the sign bit).
1427  Constant *BoolMask = getNegativeIsTrueBoolVec(ConstMask);
1428 
1429  IC.Builder.CreateMaskedStore(Vec, PtrCast, 1, BoolMask);
1430 
1431  // 'Replace uses' doesn't work for stores. Erase the original masked store.
1432  IC.eraseInstFromFunction(II);
1433  return true;
1434 }
1435 
1436 // Constant fold llvm.amdgcn.fmed3 intrinsics for standard inputs.
1437 //
1438 // A single NaN input is folded to minnum, so we rely on that folding for
1439 // handling NaNs.
1440 static APFloat fmed3AMDGCN(const APFloat &Src0, const APFloat &Src1,
1441  const APFloat &Src2) {
1442  APFloat Max3 = maxnum(maxnum(Src0, Src1), Src2);
1443 
1444  APFloat::cmpResult Cmp0 = Max3.compare(Src0);
1445  assert(Cmp0 != APFloat::cmpUnordered && "nans handled separately");
1446  if (Cmp0 == APFloat::cmpEqual)
1447  return maxnum(Src1, Src2);
1448 
1449  APFloat::cmpResult Cmp1 = Max3.compare(Src1);
1450  assert(Cmp1 != APFloat::cmpUnordered && "nans handled separately");
1451  if (Cmp1 == APFloat::cmpEqual)
1452  return maxnum(Src0, Src2);
1453 
1454  return maxnum(Src0, Src1);
1455 }
1456 
1457 /// Convert a table lookup to shufflevector if the mask is constant.
1458 /// This could benefit tbl1 if the mask is { 7,6,5,4,3,2,1,0 }, in
1459 /// which case we could lower the shufflevector with rev64 instructions
1460 /// as it's actually a byte reverse.
1462  InstCombiner::BuilderTy &Builder) {
1463  // Bail out if the mask is not a constant.
1464  auto *C = dyn_cast<Constant>(II.getArgOperand(1));
1465  if (!C)
1466  return nullptr;
1467 
1468  auto *VecTy = cast<VectorType>(II.getType());
1469  unsigned NumElts = VecTy->getNumElements();
1470 
1471  // Only perform this transformation for <8 x i8> vector types.
1472  if (!VecTy->getElementType()->isIntegerTy(8) || NumElts != 8)
1473  return nullptr;
1474 
1475  uint32_t Indexes[8];
1476 
1477  for (unsigned I = 0; I < NumElts; ++I) {
1478  Constant *COp = C->getAggregateElement(I);
1479 
1480  if (!COp || !isa<ConstantInt>(COp))
1481  return nullptr;
1482 
1483  Indexes[I] = cast<ConstantInt>(COp)->getLimitedValue();
1484 
1485  // Make sure the mask indices are in range.
1486  if (Indexes[I] >= NumElts)
1487  return nullptr;
1488  }
1489 
1490  auto *ShuffleMask = ConstantDataVector::get(II.getContext(),
1491  makeArrayRef(Indexes));
1492  auto *V1 = II.getArgOperand(0);
1493  auto *V2 = Constant::getNullValue(V1->getType());
1494  return Builder.CreateShuffleVector(V1, V2, ShuffleMask);
1495 }
1496 
1497 /// Convert a vector load intrinsic into a simple llvm load instruction.
1498 /// This is beneficial when the underlying object being addressed comes
1499 /// from a constant, since we get constant-folding for free.
1501  unsigned MemAlign,
1502  InstCombiner::BuilderTy &Builder) {
1503  auto *IntrAlign = dyn_cast<ConstantInt>(II.getArgOperand(1));
1504 
1505  if (!IntrAlign)
1506  return nullptr;
1507 
1508  unsigned Alignment = IntrAlign->getLimitedValue() < MemAlign ?
1509  MemAlign : IntrAlign->getLimitedValue();
1510 
1511  if (!isPowerOf2_32(Alignment))
1512  return nullptr;
1513 
1514  auto *BCastInst = Builder.CreateBitCast(II.getArgOperand(0),
1515  PointerType::get(II.getType(), 0));
1516  return Builder.CreateAlignedLoad(BCastInst, Alignment);
1517 }
1518 
1519 // Returns true iff the 2 intrinsics have the same operands, limiting the
1520 // comparison to the first NumOperands.
1521 static bool haveSameOperands(const IntrinsicInst &I, const IntrinsicInst &E,
1522  unsigned NumOperands) {
1523  assert(I.getNumArgOperands() >= NumOperands && "Not enough operands");
1524  assert(E.getNumArgOperands() >= NumOperands && "Not enough operands");
1525  for (unsigned i = 0; i < NumOperands; i++)
1526  if (I.getArgOperand(i) != E.getArgOperand(i))
1527  return false;
1528  return true;
1529 }
1530 
1531 // Remove trivially empty start/end intrinsic ranges, i.e. a start
1532 // immediately followed by an end (ignoring debuginfo or other
1533 // start/end intrinsics in between). As this handles only the most trivial
1534 // cases, tracking the nesting level is not needed:
1535 //
1536 // call @llvm.foo.start(i1 0) ; &I
1537 // call @llvm.foo.start(i1 0)
1538 // call @llvm.foo.end(i1 0) ; This one will not be skipped: it will be removed
1539 // call @llvm.foo.end(i1 0)
1540 static bool removeTriviallyEmptyRange(IntrinsicInst &I, unsigned StartID,
1541  unsigned EndID, InstCombiner &IC) {
1542  assert(I.getIntrinsicID() == StartID &&
1543  "Start intrinsic does not have expected ID");
1544  BasicBlock::iterator BI(I), BE(I.getParent()->end());
1545  for (++BI; BI != BE; ++BI) {
1546  if (auto *E = dyn_cast<IntrinsicInst>(BI)) {
1547  if (isa<DbgInfoIntrinsic>(E) || E->getIntrinsicID() == StartID)
1548  continue;
1549  if (E->getIntrinsicID() == EndID &&
1550  haveSameOperands(I, *E, E->getNumArgOperands())) {
1551  IC.eraseInstFromFunction(*E);
1552  IC.eraseInstFromFunction(I);
1553  return true;
1554  }
1555  }
1556  break;
1557  }
1558 
1559  return false;
1560 }
1561 
1562 // Convert NVVM intrinsics to target-generic LLVM code where possible.
1564  // Each NVVM intrinsic we can simplify can be replaced with one of:
1565  //
1566  // * an LLVM intrinsic,
1567  // * an LLVM cast operation,
1568  // * an LLVM binary operation, or
1569  // * ad-hoc LLVM IR for the particular operation.
1570 
1571  // Some transformations are only valid when the module's
1572  // flush-denormals-to-zero (ftz) setting is true/false, whereas other
1573  // transformations are valid regardless of the module's ftz setting.
1574  enum FtzRequirementTy {
1575  FTZ_Any, // Any ftz setting is ok.
1576  FTZ_MustBeOn, // Transformation is valid only if ftz is on.
1577  FTZ_MustBeOff, // Transformation is valid only if ftz is off.
1578  };
1579  // Classes of NVVM intrinsics that can't be replaced one-to-one with a
1580  // target-generic intrinsic, cast op, or binary op but that we can nonetheless
1581  // simplify.
1582  enum SpecialCase {
1583  SPC_Reciprocal,
1584  };
1585 
1586  // SimplifyAction is a poor-man's variant (plus an additional flag) that
1587  // represents how to replace an NVVM intrinsic with target-generic LLVM IR.
1588  struct SimplifyAction {
1589  // Invariant: At most one of these Optionals has a value.
1593  Optional<SpecialCase> Special;
1594 
1595  FtzRequirementTy FtzRequirement = FTZ_Any;
1596 
1597  SimplifyAction() = default;
1598 
1599  SimplifyAction(Intrinsic::ID IID, FtzRequirementTy FtzReq)
1600  : IID(IID), FtzRequirement(FtzReq) {}
1601 
1602  // Cast operations don't have anything to do with FTZ, so we skip that
1603  // argument.
1604  SimplifyAction(Instruction::CastOps CastOp) : CastOp(CastOp) {}
1605 
1606  SimplifyAction(Instruction::BinaryOps BinaryOp, FtzRequirementTy FtzReq)
1607  : BinaryOp(BinaryOp), FtzRequirement(FtzReq) {}
1608 
1609  SimplifyAction(SpecialCase Special, FtzRequirementTy FtzReq)
1610  : Special(Special), FtzRequirement(FtzReq) {}
1611  };
1612 
1613  // Try to generate a SimplifyAction describing how to replace our
1614  // IntrinsicInstr with target-generic LLVM IR.
1615  const SimplifyAction Action = [II]() -> SimplifyAction {
1616  switch (II->getIntrinsicID()) {
1617  // NVVM intrinsics that map directly to LLVM intrinsics.
1619  return {Intrinsic::ceil, FTZ_Any};
1621  return {Intrinsic::ceil, FTZ_MustBeOff};
1623  return {Intrinsic::ceil, FTZ_MustBeOn};
1625  return {Intrinsic::fabs, FTZ_Any};
1627  return {Intrinsic::fabs, FTZ_MustBeOff};
1629  return {Intrinsic::fabs, FTZ_MustBeOn};
1631  return {Intrinsic::floor, FTZ_Any};
1633  return {Intrinsic::floor, FTZ_MustBeOff};
1635  return {Intrinsic::floor, FTZ_MustBeOn};
1637  return {Intrinsic::fma, FTZ_Any};
1639  return {Intrinsic::fma, FTZ_MustBeOff};
1641  return {Intrinsic::fma, FTZ_MustBeOn};
1643  return {Intrinsic::maxnum, FTZ_Any};
1645  return {Intrinsic::maxnum, FTZ_MustBeOff};
1647  return {Intrinsic::maxnum, FTZ_MustBeOn};
1649  return {Intrinsic::minnum, FTZ_Any};
1651  return {Intrinsic::minnum, FTZ_MustBeOff};
1653  return {Intrinsic::minnum, FTZ_MustBeOn};
1655  return {Intrinsic::round, FTZ_Any};
1657  return {Intrinsic::round, FTZ_MustBeOff};
1659  return {Intrinsic::round, FTZ_MustBeOn};
1661  return {Intrinsic::sqrt, FTZ_Any};
1663  // nvvm_sqrt_f is a special case. For most intrinsics, foo_ftz_f is the
1664  // ftz version, and foo_f is the non-ftz version. But nvvm_sqrt_f adopts
1665  // the ftz-ness of the surrounding code. sqrt_rn_f and sqrt_rn_ftz_f are
1666  // the versions with explicit ftz-ness.
1667  return {Intrinsic::sqrt, FTZ_Any};
1669  return {Intrinsic::sqrt, FTZ_MustBeOff};
1671  return {Intrinsic::sqrt, FTZ_MustBeOn};
1673  return {Intrinsic::trunc, FTZ_Any};
1675  return {Intrinsic::trunc, FTZ_MustBeOff};
1677  return {Intrinsic::trunc, FTZ_MustBeOn};
1678 
1679  // NVVM intrinsics that map to LLVM cast operations.
1680  //
1681  // Note that llvm's target-generic conversion operators correspond to the rz
1682  // (round to zero) versions of the nvvm conversion intrinsics, even though
1683  // most everything else here uses the rn (round to nearest even) nvvm ops.
1688  return {Instruction::FPToSI};
1693  return {Instruction::FPToUI};
1698  return {Instruction::SIToFP};
1703  return {Instruction::UIToFP};
1704 
1705  // NVVM intrinsics that map to LLVM binary ops.
1707  return {Instruction::FAdd, FTZ_Any};
1709  return {Instruction::FAdd, FTZ_MustBeOff};
1711  return {Instruction::FAdd, FTZ_MustBeOn};
1713  return {Instruction::FMul, FTZ_Any};
1715  return {Instruction::FMul, FTZ_MustBeOff};
1717  return {Instruction::FMul, FTZ_MustBeOn};
1719  return {Instruction::FDiv, FTZ_Any};
1721  return {Instruction::FDiv, FTZ_MustBeOff};
1723  return {Instruction::FDiv, FTZ_MustBeOn};
1724 
1725  // The remainder of cases are NVVM intrinsics that map to LLVM idioms, but
1726  // need special handling.
1727  //
1728  // We seem to be missing intrinsics for rcp.approx.{ftz.}f32, which is just
1729  // as well.
1731  return {SPC_Reciprocal, FTZ_Any};
1733  return {SPC_Reciprocal, FTZ_MustBeOff};
1735  return {SPC_Reciprocal, FTZ_MustBeOn};
1736 
1737  // We do not currently simplify intrinsics that give an approximate answer.
1738  // These include:
1739  //
1740  // - nvvm_cos_approx_{f,ftz_f}
1741  // - nvvm_ex2_approx_{d,f,ftz_f}
1742  // - nvvm_lg2_approx_{d,f,ftz_f}
1743  // - nvvm_sin_approx_{f,ftz_f}
1744  // - nvvm_sqrt_approx_{f,ftz_f}
1745  // - nvvm_rsqrt_approx_{d,f,ftz_f}
1746  // - nvvm_div_approx_{ftz_d,ftz_f,f}
1747  // - nvvm_rcp_approx_ftz_d
1748  //
1749  // Ideally we'd encode them as e.g. "fast call @llvm.cos", where "fast"
1750  // means that fastmath is enabled in the intrinsic. Unfortunately only
1751  // binary operators (currently) have a fastmath bit in SelectionDAG, so this
1752  // information gets lost and we can't select on it.
1753  //
1754  // TODO: div and rcp are lowered to a binary op, so these we could in theory
1755  // lower them to "fast fdiv".
1756 
1757  default:
1758  return {};
1759  }
1760  }();
1761 
1762  // If Action.FtzRequirementTy is not satisfied by the module's ftz state, we
1763  // can bail out now. (Notice that in the case that IID is not an NVVM
1764  // intrinsic, we don't have to look up any module metadata, as
1765  // FtzRequirementTy will be FTZ_Any.)
1766  if (Action.FtzRequirement != FTZ_Any) {
1767  bool FtzEnabled =
1768  II->getFunction()->getFnAttribute("nvptx-f32ftz").getValueAsString() ==
1769  "true";
1770 
1771  if (FtzEnabled != (Action.FtzRequirement == FTZ_MustBeOn))
1772  return nullptr;
1773  }
1774 
1775  // Simplify to target-generic intrinsic.
1776  if (Action.IID) {
1778  // All the target-generic intrinsics currently of interest to us have one
1779  // type argument, equal to that of the nvvm intrinsic's argument.
1780  Type *Tys[] = {II->getArgOperand(0)->getType()};
1781  return CallInst::Create(
1782  Intrinsic::getDeclaration(II->getModule(), *Action.IID, Tys), Args);
1783  }
1784 
1785  // Simplify to target-generic binary op.
1786  if (Action.BinaryOp)
1787  return BinaryOperator::Create(*Action.BinaryOp, II->getArgOperand(0),
1788  II->getArgOperand(1), II->getName());
1789 
1790  // Simplify to target-generic cast op.
1791  if (Action.CastOp)
1792  return CastInst::Create(*Action.CastOp, II->getArgOperand(0), II->getType(),
1793  II->getName());
1794 
1795  // All that's left are the special cases.
1796  if (!Action.Special)
1797  return nullptr;
1798 
1799  switch (*Action.Special) {
1800  case SPC_Reciprocal:
1801  // Simplify reciprocal.
1802  return BinaryOperator::Create(
1803  Instruction::FDiv, ConstantFP::get(II->getArgOperand(0)->getType(), 1),
1804  II->getArgOperand(0), II->getName());
1805  }
1806  llvm_unreachable("All SpecialCase enumerators should be handled in switch.");
1807 }
1808 
1811  return nullptr;
1812 }
1813 
1816  return nullptr;
1817 }
1818 
1820  assert(Call.getNumArgOperands() > 1 && "Need at least 2 args to swap");
1821  Value *Arg0 = Call.getArgOperand(0), *Arg1 = Call.getArgOperand(1);
1822  if (isa<Constant>(Arg0) && !isa<Constant>(Arg1)) {
1823  Call.setArgOperand(0, Arg1);
1824  Call.setArgOperand(1, Arg0);
1825  return &Call;
1826  }
1827  return nullptr;
1828 }
1829 
1830 /// CallInst simplification. This mostly only handles folding of intrinsic
1831 /// instructions. For normal calls, it allows visitCallSite to do the heavy
1832 /// lifting.
1834  if (Value *V = SimplifyCall(&CI, SQ.getWithInstruction(&CI)))
1835  return replaceInstUsesWith(CI, V);
1836 
1837  if (isFreeCall(&CI, &TLI))
1838  return visitFree(CI);
1839 
1840  // If the caller function is nounwind, mark the call as nounwind, even if the
1841  // callee isn't.
1842  if (CI.getFunction()->doesNotThrow() && !CI.doesNotThrow()) {
1843  CI.setDoesNotThrow();
1844  return &CI;
1845  }
1846 
1847  IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
1848  if (!II) return visitCallSite(&CI);
1849 
1850  // Intrinsics cannot occur in an invoke, so handle them here instead of in
1851  // visitCallSite.
1852  if (auto *MI = dyn_cast<AnyMemIntrinsic>(II)) {
1853  bool Changed = false;
1854 
1855  // memmove/cpy/set of zero bytes is a noop.
1856  if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
1857  if (NumBytes->isNullValue())
1858  return eraseInstFromFunction(CI);
1859 
1860  if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
1861  if (CI->getZExtValue() == 1) {
1862  // Replace the instruction with just byte operations. We would
1863  // transform other cases to loads/stores, but we don't know if
1864  // alignment is sufficient.
1865  }
1866  }
1867 
1868  // No other transformations apply to volatile transfers.
1869  if (auto *M = dyn_cast<MemIntrinsic>(MI))
1870  if (M->isVolatile())
1871  return nullptr;
1872 
1873  // If we have a memmove and the source operation is a constant global,
1874  // then the source and dest pointers can't alias, so we can change this
1875  // into a call to memcpy.
1876  if (auto *MMI = dyn_cast<AnyMemMoveInst>(MI)) {
1877  if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
1878  if (GVSrc->isConstant()) {
1879  Module *M = CI.getModule();
1880  Intrinsic::ID MemCpyID =
1881  isa<AtomicMemMoveInst>(MMI)
1884  Type *Tys[3] = { CI.getArgOperand(0)->getType(),
1885  CI.getArgOperand(1)->getType(),
1886  CI.getArgOperand(2)->getType() };
1887  CI.setCalledFunction(Intrinsic::getDeclaration(M, MemCpyID, Tys));
1888  Changed = true;
1889  }
1890  }
1891 
1892  if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(MI)) {
1893  // memmove(x,x,size) -> noop.
1894  if (MTI->getSource() == MTI->getDest())
1895  return eraseInstFromFunction(CI);
1896  }
1897 
1898  // If we can determine a pointer alignment that is bigger than currently
1899  // set, update the alignment.
1900  if (auto *MTI = dyn_cast<AnyMemTransferInst>(MI)) {
1901  if (Instruction *I = SimplifyAnyMemTransfer(MTI))
1902  return I;
1903  } else if (auto *MSI = dyn_cast<AnyMemSetInst>(MI)) {
1904  if (Instruction *I = SimplifyAnyMemSet(MSI))
1905  return I;
1906  }
1907 
1908  if (Changed) return II;
1909  }
1910 
1911  if (Instruction *I = SimplifyNVVMIntrinsic(II, *this))
1912  return I;
1913 
1914  auto SimplifyDemandedVectorEltsLow = [this](Value *Op, unsigned Width,
1915  unsigned DemandedWidth) {
1916  APInt UndefElts(Width, 0);
1917  APInt DemandedElts = APInt::getLowBitsSet(Width, DemandedWidth);
1918  return SimplifyDemandedVectorElts(Op, DemandedElts, UndefElts);
1919  };
1920 
1921  switch (II->getIntrinsicID()) {
1922  default: break;
1923  case Intrinsic::objectsize:
1924  if (ConstantInt *N =
1925  lowerObjectSizeCall(II, DL, &TLI, /*MustSucceed=*/false))
1926  return replaceInstUsesWith(CI, N);
1927  return nullptr;
1928  case Intrinsic::bswap: {
1929  Value *IIOperand = II->getArgOperand(0);
1930  Value *X = nullptr;
1931 
1932  // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
1933  if (match(IIOperand, m_Trunc(m_BSwap(m_Value(X))))) {
1934  unsigned C = X->getType()->getPrimitiveSizeInBits() -
1935  IIOperand->getType()->getPrimitiveSizeInBits();
1936  Value *CV = ConstantInt::get(X->getType(), C);
1937  Value *V = Builder.CreateLShr(X, CV);
1938  return new TruncInst(V, IIOperand->getType());
1939  }
1940  break;
1941  }
1943  if (Value *SimplifiedMaskedOp = simplifyMaskedLoad(*II, Builder))
1944  return replaceInstUsesWith(CI, SimplifiedMaskedOp);
1945  break;
1947  return simplifyMaskedStore(*II, *this);
1949  return simplifyMaskedGather(*II, *this);
1951  return simplifyMaskedScatter(*II, *this);
1954  if (auto *SkippedBarrier = simplifyInvariantGroupIntrinsic(*II, *this))
1955  return replaceInstUsesWith(*II, SkippedBarrier);
1956  break;
1957  case Intrinsic::powi:
1958  if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
1959  // 0 and 1 are handled in instsimplify
1960 
1961  // powi(x, -1) -> 1/x
1962  if (Power->isMinusOne())
1963  return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0),
1964  II->getArgOperand(0));
1965  // powi(x, 2) -> x*x
1966  if (Power->equalsInt(2))
1967  return BinaryOperator::CreateFMul(II->getArgOperand(0),
1968  II->getArgOperand(0));
1969  }
1970  break;
1971 
1972  case Intrinsic::cttz:
1973  case Intrinsic::ctlz:
1974  if (auto *I = foldCttzCtlz(*II, *this))
1975  return I;
1976  break;
1977 
1978  case Intrinsic::ctpop:
1979  if (auto *I = foldCtpop(*II, *this))
1980  return I;
1981  break;
1982 
1983  case Intrinsic::fshl:
1984  case Intrinsic::fshr: {
1985  const APInt *SA;
1986  if (match(II->getArgOperand(2), m_APInt(SA))) {
1987  Value *Op0 = II->getArgOperand(0), *Op1 = II->getArgOperand(1);
1988  unsigned BitWidth = SA->getBitWidth();
1989  uint64_t ShiftAmt = SA->urem(BitWidth);
1990  assert(ShiftAmt != 0 && "SimplifyCall should have handled zero shift");
1991  // Normalize to funnel shift left.
1992  if (II->getIntrinsicID() == Intrinsic::fshr)
1993  ShiftAmt = BitWidth - ShiftAmt;
1994 
1995  // fshl(X, 0, C) -> shl X, C
1996  // fshl(X, undef, C) -> shl X, C
1997  if (match(Op1, m_Zero()) || match(Op1, m_Undef()))
1998  return BinaryOperator::CreateShl(
1999  Op0, ConstantInt::get(II->getType(), ShiftAmt));
2000 
2001  // fshl(0, X, C) -> lshr X, (BW-C)
2002  // fshl(undef, X, C) -> lshr X, (BW-C)
2003  if (match(Op0, m_Zero()) || match(Op0, m_Undef()))
2004  return BinaryOperator::CreateLShr(
2005  Op1, ConstantInt::get(II->getType(), BitWidth - ShiftAmt));
2006  }
2007 
2008  // The shift amount (operand 2) of a funnel shift is modulo the bitwidth,
2009  // so only the low bits of the shift amount are demanded if the bitwidth is
2010  // a power-of-2.
2011  unsigned BitWidth = II->getType()->getScalarSizeInBits();
2012  if (!isPowerOf2_32(BitWidth))
2013  break;
2014  APInt Op2Demanded = APInt::getLowBitsSet(BitWidth, Log2_32_Ceil(BitWidth));
2015  KnownBits Op2Known(BitWidth);
2016  if (SimplifyDemandedBits(II, 2, Op2Demanded, Op2Known))
2017  return &CI;
2018  break;
2019  }
2025  return I;
2027 
2030  OverflowCheckFlavor OCF =
2032  assert(OCF != OCF_INVALID && "unexpected!");
2033 
2034  Value *OperationResult = nullptr;
2035  Constant *OverflowResult = nullptr;
2036  if (OptimizeOverflowCheck(OCF, II->getArgOperand(0), II->getArgOperand(1),
2037  *II, OperationResult, OverflowResult))
2038  return CreateOverflowTuple(II, OperationResult, OverflowResult);
2039 
2040  break;
2041  }
2042 
2043  case Intrinsic::uadd_sat:
2044  case Intrinsic::sadd_sat:
2046  return I;
2048  case Intrinsic::usub_sat:
2049  case Intrinsic::ssub_sat: {
2050  Value *Arg0 = II->getArgOperand(0);
2051  Value *Arg1 = II->getArgOperand(1);
2052  Intrinsic::ID IID = II->getIntrinsicID();
2053 
2054  // Make use of known overflow information.
2056  switch (IID) {
2057  default:
2058  llvm_unreachable("Unexpected intrinsic!");
2059  case Intrinsic::uadd_sat:
2060  OR = computeOverflowForUnsignedAdd(Arg0, Arg1, II);
2062  return BinaryOperator::CreateNUWAdd(Arg0, Arg1);
2064  return replaceInstUsesWith(*II,
2066  break;
2067  case Intrinsic::usub_sat:
2068  OR = computeOverflowForUnsignedSub(Arg0, Arg1, II);
2070  return BinaryOperator::CreateNUWSub(Arg0, Arg1);
2072  return replaceInstUsesWith(*II,
2074  break;
2075  case Intrinsic::sadd_sat:
2076  if (willNotOverflowSignedAdd(Arg0, Arg1, *II))
2077  return BinaryOperator::CreateNSWAdd(Arg0, Arg1);
2078  break;
2079  case Intrinsic::ssub_sat:
2080  if (willNotOverflowSignedSub(Arg0, Arg1, *II))
2081  return BinaryOperator::CreateNSWSub(Arg0, Arg1);
2082  break;
2083  }
2084 
2085  // ssub.sat(X, C) -> sadd.sat(X, -C) if C != MIN
2086  Constant *C;
2087  if (IID == Intrinsic::ssub_sat && match(Arg1, m_Constant(C)) &&
2088  C->isNotMinSignedValue()) {
2089  Value *NegVal = ConstantExpr::getNeg(C);
2090  return replaceInstUsesWith(
2091  *II, Builder.CreateBinaryIntrinsic(
2092  Intrinsic::sadd_sat, Arg0, NegVal));
2093  }
2094 
2095  // sat(sat(X + Val2) + Val) -> sat(X + (Val+Val2))
2096  // sat(sat(X - Val2) - Val) -> sat(X - (Val+Val2))
2097  // if Val and Val2 have the same sign
2098  if (auto *Other = dyn_cast<IntrinsicInst>(Arg0)) {
2099  Value *X;
2100  const APInt *Val, *Val2;
2101  APInt NewVal;
2102  bool IsUnsigned =
2103  IID == Intrinsic::uadd_sat || IID == Intrinsic::usub_sat;
2104  if (Other->getIntrinsicID() == II->getIntrinsicID() &&
2105  match(Arg1, m_APInt(Val)) &&
2106  match(Other->getArgOperand(0), m_Value(X)) &&
2107  match(Other->getArgOperand(1), m_APInt(Val2))) {
2108  if (IsUnsigned)
2109  NewVal = Val->uadd_sat(*Val2);
2110  else if (Val->isNonNegative() == Val2->isNonNegative()) {
2111  bool Overflow;
2112  NewVal = Val->sadd_ov(*Val2, Overflow);
2113  if (Overflow) {
2114  // Both adds together may add more than SignedMaxValue
2115  // without saturating the final result.
2116  break;
2117  }
2118  } else {
2119  // Cannot fold saturated addition with different signs.
2120  break;
2121  }
2122 
2123  return replaceInstUsesWith(
2124  *II, Builder.CreateBinaryIntrinsic(
2125  IID, X, ConstantInt::get(II->getType(), NewVal)));
2126  }
2127  }
2128  break;
2129  }
2130 
2131  case Intrinsic::minnum:
2132  case Intrinsic::maxnum:
2133  case Intrinsic::minimum:
2134  case Intrinsic::maximum: {
2136  return I;
2137  Value *Arg0 = II->getArgOperand(0);
2138  Value *Arg1 = II->getArgOperand(1);
2139  Intrinsic::ID IID = II->getIntrinsicID();
2140  Value *X, *Y;
2141  if (match(Arg0, m_FNeg(m_Value(X))) && match(Arg1, m_FNeg(m_Value(Y))) &&
2142  (Arg0->hasOneUse() || Arg1->hasOneUse())) {
2143  // If both operands are negated, invert the call and negate the result:
2144  // min(-X, -Y) --> -(max(X, Y))
2145  // max(-X, -Y) --> -(min(X, Y))
2146  Intrinsic::ID NewIID;
2147  switch (IID) {
2148  case Intrinsic::maxnum:
2149  NewIID = Intrinsic::minnum;
2150  break;
2151  case Intrinsic::minnum:
2152  NewIID = Intrinsic::maxnum;
2153  break;
2154  case Intrinsic::maximum:
2155  NewIID = Intrinsic::minimum;
2156  break;
2157  case Intrinsic::minimum:
2158  NewIID = Intrinsic::maximum;
2159  break;
2160  default:
2161  llvm_unreachable("unexpected intrinsic ID");
2162  }
2163  Value *NewCall = Builder.CreateBinaryIntrinsic(NewIID, X, Y, II);
2164  Instruction *FNeg = BinaryOperator::CreateFNeg(NewCall);
2165  FNeg->copyIRFlags(II);
2166  return FNeg;
2167  }
2168 
2169  // m(m(X, C2), C1) -> m(X, C)
2170  const APFloat *C1, *C2;
2171  if (auto *M = dyn_cast<IntrinsicInst>(Arg0)) {
2172  if (M->getIntrinsicID() == IID && match(Arg1, m_APFloat(C1)) &&
2173  ((match(M->getArgOperand(0), m_Value(X)) &&
2174  match(M->getArgOperand(1), m_APFloat(C2))) ||
2175  (match(M->getArgOperand(1), m_Value(X)) &&
2176  match(M->getArgOperand(0), m_APFloat(C2))))) {
2177  APFloat Res(0.0);
2178  switch (IID) {
2179  case Intrinsic::maxnum:
2180  Res = maxnum(*C1, *C2);
2181  break;
2182  case Intrinsic::minnum:
2183  Res = minnum(*C1, *C2);
2184  break;
2185  case Intrinsic::maximum:
2186  Res = maximum(*C1, *C2);
2187  break;
2188  case Intrinsic::minimum:
2189  Res = minimum(*C1, *C2);
2190  break;
2191  default:
2192  llvm_unreachable("unexpected intrinsic ID");
2193  }
2194  Instruction *NewCall = Builder.CreateBinaryIntrinsic(
2195  IID, X, ConstantFP::get(Arg0->getType(), Res));
2196  NewCall->copyIRFlags(II);
2197  return replaceInstUsesWith(*II, NewCall);
2198  }
2199  }
2200 
2201  break;
2202  }
2203  case Intrinsic::fmuladd: {
2204  // Canonicalize fast fmuladd to the separate fmul + fadd.
2205  if (II->isFast()) {
2206  BuilderTy::FastMathFlagGuard Guard(Builder);
2207  Builder.setFastMathFlags(II->getFastMathFlags());
2208  Value *Mul = Builder.CreateFMul(II->getArgOperand(0),
2209  II->getArgOperand(1));
2210  Value *Add = Builder.CreateFAdd(Mul, II->getArgOperand(2));
2211  Add->takeName(II);
2212  return replaceInstUsesWith(*II, Add);
2213  }
2214 
2216  }
2217  case Intrinsic::fma: {
2219  return I;
2220 
2221  // fma fneg(x), fneg(y), z -> fma x, y, z
2222  Value *Src0 = II->getArgOperand(0);
2223  Value *Src1 = II->getArgOperand(1);
2224  Value *X, *Y;
2225  if (match(Src0, m_FNeg(m_Value(X))) && match(Src1, m_FNeg(m_Value(Y)))) {
2226  II->setArgOperand(0, X);
2227  II->setArgOperand(1, Y);
2228  return II;
2229  }
2230 
2231  // fma fabs(x), fabs(x), z -> fma x, x, z
2232  if (match(Src0, m_FAbs(m_Value(X))) &&
2233  match(Src1, m_FAbs(m_Specific(X)))) {
2234  II->setArgOperand(0, X);
2235  II->setArgOperand(1, X);
2236  return II;
2237  }
2238 
2239  // fma x, 1, z -> fadd x, z
2240  if (match(Src1, m_FPOne())) {
2241  auto *FAdd = BinaryOperator::CreateFAdd(Src0, II->getArgOperand(2));
2242  FAdd->copyFastMathFlags(II);
2243  return FAdd;
2244  }
2245 
2246  break;
2247  }
2248  case Intrinsic::fabs: {
2249  Value *Cond;
2250  Constant *LHS, *RHS;
2251  if (match(II->getArgOperand(0),
2252  m_Select(m_Value(Cond), m_Constant(LHS), m_Constant(RHS)))) {
2253  CallInst *Call0 = Builder.CreateCall(II->getCalledFunction(), {LHS});
2254  CallInst *Call1 = Builder.CreateCall(II->getCalledFunction(), {RHS});
2255  return SelectInst::Create(Cond, Call0, Call1);
2256  }
2257 
2259  }
2260  case Intrinsic::ceil:
2261  case Intrinsic::floor:
2262  case Intrinsic::round:
2263  case Intrinsic::nearbyint:
2264  case Intrinsic::rint:
2265  case Intrinsic::trunc: {
2266  Value *ExtSrc;
2267  if (match(II->getArgOperand(0), m_OneUse(m_FPExt(m_Value(ExtSrc))))) {
2268  // Narrow the call: intrinsic (fpext x) -> fpext (intrinsic x)
2269  Value *NarrowII =
2270  Builder.CreateUnaryIntrinsic(II->getIntrinsicID(), ExtSrc, II);
2271  return new FPExtInst(NarrowII, II->getType());
2272  }
2273  break;
2274  }
2275  case Intrinsic::cos:
2276  case Intrinsic::amdgcn_cos: {
2277  Value *X;
2278  Value *Src = II->getArgOperand(0);
2279  if (match(Src, m_FNeg(m_Value(X))) || match(Src, m_FAbs(m_Value(X)))) {
2280  // cos(-x) -> cos(x)
2281  // cos(fabs(x)) -> cos(x)
2282  II->setArgOperand(0, X);
2283  return II;
2284  }
2285  break;
2286  }
2287  case Intrinsic::sin: {
2288  Value *X;
2289  if (match(II->getArgOperand(0), m_OneUse(m_FNeg(m_Value(X))))) {
2290  // sin(-x) --> -sin(x)
2291  Value *NewSin = Builder.CreateUnaryIntrinsic(Intrinsic::sin, X, II);
2292  Instruction *FNeg = BinaryOperator::CreateFNeg(NewSin);
2293  FNeg->copyFastMathFlags(II);
2294  return FNeg;
2295  }
2296  break;
2297  }
2300  // Turn PPC lvx -> load if the pointer is known aligned.
2301  if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, DL, II, &AC,
2302  &DT) >= 16) {
2303  Value *Ptr = Builder.CreateBitCast(II->getArgOperand(0),
2304  PointerType::getUnqual(II->getType()));
2305  return new LoadInst(Ptr);
2306  }
2307  break;
2310  // Turn PPC VSX loads into normal loads.
2311  Value *Ptr = Builder.CreateBitCast(II->getArgOperand(0),
2312  PointerType::getUnqual(II->getType()));
2313  return new LoadInst(Ptr, Twine(""), false, 1);
2314  }
2317  // Turn stvx -> store if the pointer is known aligned.
2318  if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, DL, II, &AC,
2319  &DT) >= 16) {
2320  Type *OpPtrTy =
2321  PointerType::getUnqual(II->getArgOperand(0)->getType());
2322  Value *Ptr = Builder.CreateBitCast(II->getArgOperand(1), OpPtrTy);
2323  return new StoreInst(II->getArgOperand(0), Ptr);
2324  }
2325  break;
2328  // Turn PPC VSX stores into normal stores.
2329  Type *OpPtrTy = PointerType::getUnqual(II->getArgOperand(0)->getType());
2330  Value *Ptr = Builder.CreateBitCast(II->getArgOperand(1), OpPtrTy);
2331  return new StoreInst(II->getArgOperand(0), Ptr, false, 1);
2332  }
2334  // Turn PPC QPX qvlfs -> load if the pointer is known aligned.
2335  if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, DL, II, &AC,
2336  &DT) >= 16) {
2337  Type *VTy = VectorType::get(Builder.getFloatTy(),
2338  II->getType()->getVectorNumElements());
2339  Value *Ptr = Builder.CreateBitCast(II->getArgOperand(0),
2340  PointerType::getUnqual(VTy));
2341  Value *Load = Builder.CreateLoad(Ptr);
2342  return new FPExtInst(Load, II->getType());
2343  }
2344  break;
2346  // Turn PPC QPX qvlfd -> load if the pointer is known aligned.
2347  if (getOrEnforceKnownAlignment(II->getArgOperand(0), 32, DL, II, &AC,
2348  &DT) >= 32) {
2349  Value *Ptr = Builder.CreateBitCast(II->getArgOperand(0),
2350  PointerType::getUnqual(II->getType()));
2351  return new LoadInst(Ptr);
2352  }
2353  break;
2355  // Turn PPC QPX qvstfs -> store if the pointer is known aligned.
2356  if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, DL, II, &AC,
2357  &DT) >= 16) {
2358  Type *VTy = VectorType::get(Builder.getFloatTy(),
2359  II->getArgOperand(0)->getType()->getVectorNumElements());
2360  Value *TOp = Builder.CreateFPTrunc(II->getArgOperand(0), VTy);
2361  Type *OpPtrTy = PointerType::getUnqual(VTy);
2362  Value *Ptr = Builder.CreateBitCast(II->getArgOperand(1), OpPtrTy);
2363  return new StoreInst(TOp, Ptr);
2364  }
2365  break;
2367  // Turn PPC QPX qvstfd -> store if the pointer is known aligned.
2368  if (getOrEnforceKnownAlignment(II->getArgOperand(1), 32, DL, II, &AC,
2369  &DT) >= 32) {
2370  Type *OpPtrTy =
2371  PointerType::getUnqual(II->getArgOperand(0)->getType());
2372  Value *Ptr = Builder.CreateBitCast(II->getArgOperand(1), OpPtrTy);
2373  return new StoreInst(II->getArgOperand(0), Ptr);
2374  }
2375  break;
2376 
2381  // If the RHS is a constant we can try some simplifications.
2382  if (auto *C = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
2383  uint64_t Shift = C->getZExtValue();
2384  uint64_t Length = (Shift >> 8) & 0xff;
2385  Shift &= 0xff;
2386  unsigned BitWidth = II->getType()->getIntegerBitWidth();
2387  // If the length is 0 or the shift is out of range, replace with zero.
2388  if (Length == 0 || Shift >= BitWidth)
2389  return replaceInstUsesWith(CI, ConstantInt::get(II->getType(), 0));
2390  // If the LHS is also a constant, we can completely constant fold this.
2391  if (auto *InC = dyn_cast<ConstantInt>(II->getArgOperand(0))) {
2392  uint64_t Result = InC->getZExtValue() >> Shift;
2393  if (Length > BitWidth)
2394  Length = BitWidth;
2395  Result &= maskTrailingOnes<uint64_t>(Length);
2396  return replaceInstUsesWith(CI, ConstantInt::get(II->getType(), Result));
2397  }
2398  // TODO should we turn this into 'and' if shift is 0? Or 'shl' if we
2399  // are only masking bits that a shift already cleared?
2400  }
2401  break;
2402 
2405  // If the RHS is a constant we can try some simplifications.
2406  if (auto *C = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
2407  uint64_t Index = C->getZExtValue() & 0xff;
2408  unsigned BitWidth = II->getType()->getIntegerBitWidth();
2409  if (Index >= BitWidth)
2410  return replaceInstUsesWith(CI, II->getArgOperand(0));
2411  if (Index == 0)
2412  return replaceInstUsesWith(CI, ConstantInt::get(II->getType(), 0));
2413  // If the LHS is also a constant, we can completely constant fold this.
2414  if (auto *InC = dyn_cast<ConstantInt>(II->getArgOperand(0))) {
2415  uint64_t Result = InC->getZExtValue();
2416  Result &= maskTrailingOnes<uint64_t>(Index);
2417  return replaceInstUsesWith(CI, ConstantInt::get(II->getType(), Result));
2418  }
2419  // TODO should we convert this to an AND if the RHS is constant?
2420  }
2421  break;
2422 
2425  auto Arg = II->getArgOperand(0);
2426  auto ArgType = cast<VectorType>(Arg->getType());
2427  auto RetType = cast<VectorType>(II->getType());
2428  unsigned ArgWidth = ArgType->getNumElements();
2429  unsigned RetWidth = RetType->getNumElements();
2430  assert(RetWidth <= ArgWidth && "Unexpected input/return vector widths");
2431  assert(ArgType->isIntOrIntVectorTy() &&
2432  ArgType->getScalarSizeInBits() == 16 &&
2433  "CVTPH2PS input type should be 16-bit integer vector");
2434  assert(RetType->getScalarType()->isFloatTy() &&
2435  "CVTPH2PS output type should be 32-bit float vector");
2436 
2437  // Constant folding: Convert to generic half to single conversion.
2438  if (isa<ConstantAggregateZero>(Arg))
2439  return replaceInstUsesWith(*II, ConstantAggregateZero::get(RetType));
2440 
2441  if (isa<ConstantDataVector>(Arg)) {
2442  auto VectorHalfAsShorts = Arg;
2443  if (RetWidth < ArgWidth) {
2444  SmallVector<uint32_t, 8> SubVecMask;
2445  for (unsigned i = 0; i != RetWidth; ++i)
2446  SubVecMask.push_back((int)i);
2447  VectorHalfAsShorts = Builder.CreateShuffleVector(
2448  Arg, UndefValue::get(ArgType), SubVecMask);
2449  }
2450 
2451  auto VectorHalfType =
2452  VectorType::get(Type::getHalfTy(II->getContext()), RetWidth);
2453  auto VectorHalfs =
2454  Builder.CreateBitCast(VectorHalfAsShorts, VectorHalfType);
2455  auto VectorFloats = Builder.CreateFPExt(VectorHalfs, RetType);
2456  return replaceInstUsesWith(*II, VectorFloats);
2457  }
2458 
2459  // We only use the lowest lanes of the argument.
2460  if (Value *V = SimplifyDemandedVectorEltsLow(Arg, ArgWidth, RetWidth)) {
2461  II->setArgOperand(0, V);
2462  return II;
2463  }
2464  break;
2465  }
2466 
2491  // These intrinsics only demand the 0th element of their input vectors. If
2492  // we can simplify the input based on that, do so now.
2493  Value *Arg = II->getArgOperand(0);
2494  unsigned VWidth = Arg->getType()->getVectorNumElements();
2495  if (Value *V = SimplifyDemandedVectorEltsLow(Arg, VWidth, 1)) {
2496  II->setArgOperand(0, V);
2497  return II;
2498  }
2499  break;
2500  }
2501 
2514  if (Value *V = simplifyX86round(*II, Builder))
2515  return replaceInstUsesWith(*II, V);
2516  break;
2517 
2525  if (Value *V = simplifyX86movmsk(*II, Builder))
2526  return replaceInstUsesWith(*II, V);
2527  break;
2528 
2557  // These intrinsics only demand the 0th element of their input vectors. If
2558  // we can simplify the input based on that, do so now.
2559  bool MadeChange = false;
2560  Value *Arg0 = II->getArgOperand(0);
2561  Value *Arg1 = II->getArgOperand(1);
2562  unsigned VWidth = Arg0->getType()->getVectorNumElements();
2563  if (Value *V = SimplifyDemandedVectorEltsLow(Arg0, VWidth, 1)) {
2564  II->setArgOperand(0, V);
2565  MadeChange = true;
2566  }
2567  if (Value *V = SimplifyDemandedVectorEltsLow(Arg1, VWidth, 1)) {
2568  II->setArgOperand(1, V);
2569  MadeChange = true;
2570  }
2571  if (MadeChange)
2572  return II;
2573  break;
2574  }
2581  // Folding cmp(sub(a,b),0) -> cmp(a,b) and cmp(0,sub(a,b)) -> cmp(b,a)
2582  Value *Arg0 = II->getArgOperand(0);
2583  Value *Arg1 = II->getArgOperand(1);
2584  bool Arg0IsZero = match(Arg0, m_PosZeroFP());
2585  if (Arg0IsZero)
2586  std::swap(Arg0, Arg1);
2587  Value *A, *B;
2588  // This fold requires only the NINF(not +/- inf) since inf minus
2589  // inf is nan.
2590  // NSZ(No Signed Zeros) is not needed because zeros of any sign are
2591  // equal for both compares.
2592  // NNAN is not needed because nans compare the same for both compares.
2593  // The compare intrinsic uses the above assumptions and therefore
2594  // doesn't require additional flags.
2595  if ((match(Arg0, m_OneUse(m_FSub(m_Value(A), m_Value(B)))) &&
2596  match(Arg1, m_PosZeroFP()) && isa<Instruction>(Arg0) &&
2597  cast<Instruction>(Arg0)->getFastMathFlags().noInfs())) {
2598  if (Arg0IsZero)
2599  std::swap(A, B);
2600  II->setArgOperand(0, A);
2601  II->setArgOperand(1, B);
2602  return II;
2603  }
2604  break;
2605  }
2606 
2615  // If the rounding mode is CUR_DIRECTION(4) we can turn these into regular
2616  // IR operations.
2617  if (auto *R = dyn_cast<ConstantInt>(II->getArgOperand(2))) {
2618  if (R->getValue() == 4) {
2619  Value *Arg0 = II->getArgOperand(0);
2620  Value *Arg1 = II->getArgOperand(1);
2621 
2622  Value *V;
2623  switch (II->getIntrinsicID()) {
2624  default: llvm_unreachable("Case stmts out of sync!");
2627  V = Builder.CreateFAdd(Arg0, Arg1);
2628  break;
2631  V = Builder.CreateFSub(Arg0, Arg1);
2632  break;
2635  V = Builder.CreateFMul(Arg0, Arg1);
2636  break;
2639  V = Builder.CreateFDiv(Arg0, Arg1);
2640  break;
2641  }
2642 
2643  return replaceInstUsesWith(*II, V);
2644  }
2645  }
2646  break;
2647 
2656  // If the rounding mode is CUR_DIRECTION(4) we can turn these into regular
2657  // IR operations.
2658  if (auto *R = dyn_cast<ConstantInt>(II->getArgOperand(4))) {
2659  if (R->getValue() == 4) {
2660  // Extract the element as scalars.
2661  Value *Arg0 = II->getArgOperand(0);
2662  Value *Arg1 = II->getArgOperand(1);
2663  Value *LHS = Builder.CreateExtractElement(Arg0, (uint64_t)0);
2664  Value *RHS = Builder.CreateExtractElement(Arg1, (uint64_t)0);
2665 
2666  Value *V;
2667  switch (II->getIntrinsicID()) {
2668  default: llvm_unreachable("Case stmts out of sync!");
2671  V = Builder.CreateFAdd(LHS, RHS);
2672  break;
2675  V = Builder.CreateFSub(LHS, RHS);
2676  break;
2679  V = Builder.CreateFMul(LHS, RHS);
2680  break;
2683  V = Builder.CreateFDiv(LHS, RHS);
2684  break;
2685  }
2686 
2687  // Handle the masking aspect of the intrinsic.
2688  Value *Mask = II->getArgOperand(3);
2689  auto *C = dyn_cast<ConstantInt>(Mask);
2690  // We don't need a select if we know the mask bit is a 1.
2691  if (!C || !C->getValue()[0]) {
2692  // Cast the mask to an i1 vector and then extract the lowest element.
2693  auto *MaskTy = VectorType::get(Builder.getInt1Ty(),
2694  cast<IntegerType>(Mask->getType())->getBitWidth());
2695  Mask = Builder.CreateBitCast(Mask, MaskTy);
2696  Mask = Builder.CreateExtractElement(Mask, (uint64_t)0);
2697  // Extract the lowest element from the passthru operand.
2698  Value *Passthru = Builder.CreateExtractElement(II->getArgOperand(2),
2699  (uint64_t)0);
2700  V = Builder.CreateSelect(Mask, V, Passthru);
2701  }
2702 
2703  // Insert the result back into the original argument 0.
2704  V = Builder.CreateInsertElement(Arg0, V, (uint64_t)0);
2705 
2706  return replaceInstUsesWith(*II, V);
2707  }
2708  }
2710 
2711  // X86 scalar intrinsics simplified with SimplifyDemandedVectorElts.
2724  unsigned VWidth = II->getType()->getVectorNumElements();
2725  APInt UndefElts(VWidth, 0);
2726  APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
2727  if (Value *V = SimplifyDemandedVectorElts(II, AllOnesEltMask, UndefElts)) {
2728  if (V != II)
2729  return replaceInstUsesWith(*II, V);
2730  return II;
2731  }
2732  break;
2733  }
2736  unsigned VWidth = II->getType()->getVectorNumElements();
2737  APInt UndefElts(VWidth, 0);
2738  APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
2739  if (Value *V = SimplifyDemandedVectorElts(II, AllOnesEltMask, UndefElts)) {
2740  if (V != II)
2741  return replaceInstUsesWith(*II, V);
2742  return II;
2743  } else if (Value *V = simplifyX86round(*II, Builder))
2744  return replaceInstUsesWith(*II, V);
2745  break;
2746  }
2747 
2748  // Constant fold ashr( <A x Bi>, Ci ).
2749  // Constant fold lshr( <A x Bi>, Ci ).
2750  // Constant fold shl( <A x Bi>, Ci ).
2778  if (Value *V = simplifyX86immShift(*II, Builder))
2779  return replaceInstUsesWith(*II, V);
2780  break;
2781 
2809  if (Value *V = simplifyX86immShift(*II, Builder))
2810  return replaceInstUsesWith(*II, V);
2811 
2812  // SSE2/AVX2 uses only the first 64-bits of the 128-bit vector
2813  // operand to compute the shift amount.
2814  Value *Arg1 = II->getArgOperand(1);
2815  assert(Arg1->getType()->getPrimitiveSizeInBits() == 128 &&
2816  "Unexpected packed shift size");
2817  unsigned VWidth = Arg1->getType()->getVectorNumElements();
2818 
2819  if (Value *V = SimplifyDemandedVectorEltsLow(Arg1, VWidth, VWidth / 2)) {
2820  II->setArgOperand(1, V);
2821  return II;
2822  }
2823  break;
2824  }
2825 
2853  if (Value *V = simplifyX86varShift(*II, Builder))
2854  return replaceInstUsesWith(*II, V);
2855  break;
2856 
2863  if (Value *V = simplifyX86pack(*II, true))
2864  return replaceInstUsesWith(*II, V);
2865  break;
2866 
2873  if (Value *V = simplifyX86pack(*II, false))
2874  return replaceInstUsesWith(*II, V);
2875  break;
2876 
2880  if (auto *C = dyn_cast<ConstantInt>(II->getArgOperand(2))) {
2881  unsigned Imm = C->getZExtValue();
2882 
2883  bool MadeChange = false;
2884  Value *Arg0 = II->getArgOperand(0);
2885  Value *Arg1 = II->getArgOperand(1);
2886  unsigned VWidth = Arg0->getType()->getVectorNumElements();
2887 
2888  APInt UndefElts1(VWidth, 0);
2889  APInt DemandedElts1 = APInt::getSplat(VWidth,
2890  APInt(2, (Imm & 0x01) ? 2 : 1));
2891  if (Value *V = SimplifyDemandedVectorElts(Arg0, DemandedElts1,
2892  UndefElts1)) {
2893  II->setArgOperand(0, V);
2894  MadeChange = true;
2895  }
2896 
2897  APInt UndefElts2(VWidth, 0);
2898  APInt DemandedElts2 = APInt::getSplat(VWidth,
2899  APInt(2, (Imm & 0x10) ? 2 : 1));
2900  if (Value *V = SimplifyDemandedVectorElts(Arg1, DemandedElts2,
2901  UndefElts2)) {
2902  II->setArgOperand(1, V);
2903  MadeChange = true;
2904  }
2905 
2906  // If either input elements are undef, the result is zero.
2907  if (DemandedElts1.isSubsetOf(UndefElts1) ||
2908  DemandedElts2.isSubsetOf(UndefElts2))
2909  return replaceInstUsesWith(*II,
2910  ConstantAggregateZero::get(II->getType()));
2911 
2912  if (MadeChange)
2913  return II;
2914  }
2915  break;
2916  }
2917 
2919  if (Value *V = simplifyX86insertps(*II, Builder))
2920  return replaceInstUsesWith(*II, V);
2921  break;
2922 
2924  Value *Op0 = II->getArgOperand(0);
2925  Value *Op1 = II->getArgOperand(1);
2926  unsigned VWidth0 = Op0->getType()->getVectorNumElements();
2927  unsigned VWidth1 = Op1->getType()->getVectorNumElements();
2928  assert(Op0->getType()->getPrimitiveSizeInBits() == 128 &&
2929  Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth0 == 2 &&
2930  VWidth1 == 16 && "Unexpected operand sizes");
2931 
2932  // See if we're dealing with constant values.
2933  Constant *C1 = dyn_cast<Constant>(Op1);
2934  ConstantInt *CILength =
2935  C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)0))
2936  : nullptr;
2937  ConstantInt *CIIndex =
2938  C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)1))
2939  : nullptr;
2940 
2941  // Attempt to simplify to a constant, shuffle vector or EXTRQI call.
2942  if (Value *V = simplifyX86extrq(*II, Op0, CILength, CIIndex, Builder))
2943  return replaceInstUsesWith(*II, V);
2944 
2945  // EXTRQ only uses the lowest 64-bits of the first 128-bit vector
2946  // operands and the lowest 16-bits of the second.
2947  bool MadeChange = false;
2948  if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth0, 1)) {
2949  II->setArgOperand(0, V);
2950  MadeChange = true;
2951  }
2952  if (Value *V = SimplifyDemandedVectorEltsLow(Op1, VWidth1, 2)) {
2953  II->setArgOperand(1, V);
2954  MadeChange = true;
2955  }
2956  if (MadeChange)
2957  return II;
2958  break;
2959  }
2960 
2962  // EXTRQI: Extract Length bits starting from Index. Zero pad the remaining
2963  // bits of the lower 64-bits. The upper 64-bits are undefined.
2964  Value *Op0 = II->getArgOperand(0);
2965  unsigned VWidth = Op0->getType()->getVectorNumElements();
2966  assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && VWidth == 2 &&
2967  "Unexpected operand size");
2968 
2969  // See if we're dealing with constant values.
2970  ConstantInt *CILength = dyn_cast<ConstantInt>(II->getArgOperand(1));
2971  ConstantInt *CIIndex = dyn_cast<ConstantInt>(II->getArgOperand(2));
2972 
2973  // Attempt to simplify to a constant or shuffle vector.
2974  if (Value *V = simplifyX86extrq(*II, Op0, CILength, CIIndex, Builder))
2975  return replaceInstUsesWith(*II, V);
2976 
2977  // EXTRQI only uses the lowest 64-bits of the first 128-bit vector
2978  // operand.
2979  if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth, 1)) {
2980  II->setArgOperand(0, V);
2981  return II;
2982  }
2983  break;
2984  }
2985 
2987  Value *Op0 = II->getArgOperand(0);
2988  Value *Op1 = II->getArgOperand(1);
2989  unsigned VWidth = Op0->getType()->getVectorNumElements();
2990  assert(Op0->getType()->getPrimitiveSizeInBits() == 128 &&
2991  Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth == 2 &&
2992  Op1->getType()->getVectorNumElements() == 2 &&
2993  "Unexpected operand size");
2994 
2995  // See if we're dealing with constant values.
2996  Constant *C1 = dyn_cast<Constant>(Op1);
2997  ConstantInt *CI11 =
2998  C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)1))
2999  : nullptr;
3000 
3001  // Attempt to simplify to a constant, shuffle vector or INSERTQI call.
3002  if (CI11) {
3003  const APInt &V11 = CI11->getValue();
3004  APInt Len = V11.zextOrTrunc(6);
3005  APInt Idx = V11.lshr(8).zextOrTrunc(6);
3006  if (Value *V = simplifyX86insertq(*II, Op0, Op1, Len, Idx, Builder))
3007  return replaceInstUsesWith(*II, V);
3008  }
3009 
3010  // INSERTQ only uses the lowest 64-bits of the first 128-bit vector
3011  // operand.
3012  if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth, 1)) {
3013  II->setArgOperand(0, V);
3014  return II;
3015  }
3016  break;
3017  }
3018 
3020  // INSERTQI: Extract lowest Length bits from lower half of second source and
3021  // insert over first source starting at Index bit. The upper 64-bits are
3022  // undefined.
3023  Value *Op0 = II->getArgOperand(0);
3024  Value *Op1 = II->getArgOperand(1);
3025  unsigned VWidth0 = Op0->getType()->getVectorNumElements();
3026  unsigned VWidth1 = Op1->getType()->getVectorNumElements();
3027  assert(Op0->getType()->getPrimitiveSizeInBits() == 128 &&
3028  Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth0 == 2 &&
3029  VWidth1 == 2 && "Unexpected operand sizes");
3030 
3031  // See if we're dealing with constant values.
3032  ConstantInt *CILength = dyn_cast<ConstantInt>(II->getArgOperand(2));
3033  ConstantInt *CIIndex = dyn_cast<ConstantInt>(II->getArgOperand(3));
3034 
3035  // Attempt to simplify to a constant or shuffle vector.
3036  if (CILength && CIIndex) {
3037  APInt Len = CILength->getValue().zextOrTrunc(6);
3038  APInt Idx = CIIndex->getValue().zextOrTrunc(6);
3039  if (Value *V = simplifyX86insertq(*II, Op0, Op1, Len, Idx, Builder))
3040  return replaceInstUsesWith(*II, V);
3041  }
3042 
3043  // INSERTQI only uses the lowest 64-bits of the first two 128-bit vector
3044  // operands.
3045  bool MadeChange = false;
3046  if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth0, 1)) {
3047  II->setArgOperand(0, V);
3048  MadeChange = true;
3049  }
3050  if (Value *V = SimplifyDemandedVectorEltsLow(Op1, VWidth1, 1)) {
3051  II->setArgOperand(1, V);
3052  MadeChange = true;
3053  }
3054  if (MadeChange)
3055  return II;
3056  break;
3057  }
3058 
3065  // fold (blend A, A, Mask) -> A
3066  Value *Op0 = II->getArgOperand(0);
3067  Value *Op1 = II->getArgOperand(1);
3068  Value *Mask = II->getArgOperand(2);
3069  if (Op0 == Op1)
3070  return replaceInstUsesWith(CI, Op0);
3071 
3072  // Zero Mask - select 1st argument.
3073  if (isa<ConstantAggregateZero>(Mask))
3074  return replaceInstUsesWith(CI, Op0);
3075 
3076  // Constant Mask - select 1st/2nd argument lane based on top bit of mask.
3077  if (auto *ConstantMask = dyn_cast<ConstantDataVector>(Mask)) {
3078  Constant *NewSelector = getNegativeIsTrueBoolVec(ConstantMask);
3079  return SelectInst::Create(NewSelector, Op1, Op0, "blendv");
3080  }
3081 
3082  // Convert to a vector select if we can bypass casts and find a boolean
3083  // vector condition value.
3084  Value *BoolVec;
3085  Mask = peekThroughBitcast(Mask);
3086  if (match(Mask, m_SExt(m_Value(BoolVec))) &&
3087  BoolVec->getType()->isVectorTy() &&
3088  BoolVec->getType()->getScalarSizeInBits() == 1) {
3089  assert(Mask->getType()->getPrimitiveSizeInBits() ==
3090  II->getType()->getPrimitiveSizeInBits() &&
3091  "Not expecting mask and operands with different sizes");
3092 
3093  unsigned NumMaskElts = Mask->getType()->getVectorNumElements();
3094  unsigned NumOperandElts = II->getType()->getVectorNumElements();
3095  if (NumMaskElts == NumOperandElts)
3096  return SelectInst::Create(BoolVec, Op1, Op0);
3097 
3098  // If the mask has less elements than the operands, each mask bit maps to
3099  // multiple elements of the operands. Bitcast back and forth.
3100  if (NumMaskElts < NumOperandElts) {
3101  Value *CastOp0 = Builder.CreateBitCast(Op0, Mask->getType());
3102  Value *CastOp1 = Builder.CreateBitCast(Op1, Mask->getType());
3103  Value *Sel = Builder.CreateSelect(BoolVec, CastOp1, CastOp0);
3104  return new BitCastInst(Sel, II->getType());
3105  }
3106  }
3107 
3108  break;
3109  }
3110 
3114  if (Value *V = simplifyX86pshufb(*II, Builder))
3115  return replaceInstUsesWith(*II, V);
3116  break;
3117 
3124  if (Value *V = simplifyX86vpermilvar(*II, Builder))
3125  return replaceInstUsesWith(*II, V);
3126  break;
3127 
3142  if (Value *V = simplifyX86vpermv(*II, Builder))
3143  return replaceInstUsesWith(*II, V);
3144  break;
3145 
3154  if (Instruction *I = simplifyX86MaskedLoad(*II, *this))
3155  return I;
3156  break;
3157 
3167  if (simplifyX86MaskedStore(*II, *this))
3168  return nullptr;
3169  break;
3170 
3175  if (Value *V = simplifyX86vpcom(*II, Builder, true))
3176  return replaceInstUsesWith(*II, V);
3177  break;
3178 
3183  if (Value *V = simplifyX86vpcom(*II, Builder, false))
3184  return replaceInstUsesWith(*II, V);
3185  break;
3186 
3188  // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
3189  // Note that ppc_altivec_vperm has a big-endian bias, so when creating
3190  // a vectorshuffle for little endian, we must undo the transformation
3191  // performed on vec_perm in altivec.h. That is, we must complement
3192  // the permutation mask with respect to 31 and reverse the order of
3193  // V1 and V2.
3194  if (Constant *Mask = dyn_cast<Constant>(II->getArgOperand(2))) {
3195  assert(Mask->getType()->getVectorNumElements() == 16 &&
3196  "Bad type for intrinsic!");
3197 
3198  // Check that all of the elements are integer constants or undefs.
3199  bool AllEltsOk = true;
3200  for (unsigned i = 0; i != 16; ++i) {
3201  Constant *Elt = Mask->getAggregateElement(i);
3202  if (!Elt || !(isa<ConstantInt>(Elt) || isa<UndefValue>(Elt))) {
3203  AllEltsOk = false;
3204  break;
3205  }
3206  }
3207 
3208  if (AllEltsOk) {
3209  // Cast the input vectors to byte vectors.
3210  Value *Op0 = Builder.CreateBitCast(II->getArgOperand(0),
3211  Mask->getType());
3212  Value *Op1 = Builder.CreateBitCast(II->getArgOperand(1),
3213  Mask->getType());
3214  Value *Result = UndefValue::get(Op0->getType());
3215 
3216  // Only extract each element once.
3217  Value *ExtractedElts[32];
3218  memset(ExtractedElts, 0, sizeof(ExtractedElts));
3219 
3220  for (unsigned i = 0; i != 16; ++i) {
3221  if (isa<UndefValue>(Mask->getAggregateElement(i)))
3222  continue;
3223  unsigned Idx =
3224  cast<ConstantInt>(Mask->getAggregateElement(i))->getZExtValue();
3225  Idx &= 31; // Match the hardware behavior.
3226  if (DL.isLittleEndian())
3227  Idx = 31 - Idx;
3228 
3229  if (!ExtractedElts[Idx]) {
3230  Value *Op0ToUse = (DL.isLittleEndian()) ? Op1 : Op0;
3231  Value *Op1ToUse = (DL.isLittleEndian()) ? Op0 : Op1;
3232  ExtractedElts[Idx] =
3233  Builder.CreateExtractElement(Idx < 16 ? Op0ToUse : Op1ToUse,
3234  Builder.getInt32(Idx&15));
3235  }
3236 
3237  // Insert this value into the result vector.
3238  Result = Builder.CreateInsertElement(Result, ExtractedElts[Idx],
3239  Builder.getInt32(i));
3240  }
3241  return CastInst::Create(Instruction::BitCast, Result, CI.getType());
3242  }
3243  }
3244  break;
3245 
3246  case Intrinsic::arm_neon_vld1: {
3247  unsigned MemAlign = getKnownAlignment(II->getArgOperand(0),
3248  DL, II, &AC, &DT);
3249  if (Value *V = simplifyNeonVld1(*II, MemAlign, Builder))
3250  return replaceInstUsesWith(*II, V);
3251  break;
3252  }
3253 
3267  unsigned MemAlign =
3268  getKnownAlignment(II->getArgOperand(0), DL, II, &AC, &DT);
3269  unsigned AlignArg = II->getNumArgOperands() - 1;
3270  ConstantInt *IntrAlign = dyn_cast<ConstantInt>(II->getArgOperand(AlignArg));
3271  if (IntrAlign && IntrAlign->getZExtValue() < MemAlign) {
3272  II->setArgOperand(AlignArg,
3273  ConstantInt::get(Type::getInt32Ty(II->getContext()),
3274  MemAlign, false));
3275  return II;
3276  }
3277  break;
3278  }
3279 
3282  if (Value *V = simplifyNeonTbl1(*II, Builder))
3283  return replaceInstUsesWith(*II, V);
3284  break;
3285 
3290  Value *Arg0 = II->getArgOperand(0);
3291  Value *Arg1 = II->getArgOperand(1);
3292 
3293  // Handle mul by zero first:
3294  if (isa<ConstantAggregateZero>(Arg0) || isa<ConstantAggregateZero>(Arg1)) {
3295  return replaceInstUsesWith(CI, ConstantAggregateZero::get(II->getType()));
3296  }
3297 
3298  // Check for constant LHS & RHS - in this case we just simplify.
3299  bool Zext = (II->getIntrinsicID() == Intrinsic::arm_neon_vmullu ||
3300  II->getIntrinsicID() == Intrinsic::aarch64_neon_umull);
3301  VectorType *NewVT = cast<VectorType>(II->getType());
3302  if (Constant *CV0 = dyn_cast<Constant>(Arg0)) {
3303  if (Constant *CV1 = dyn_cast<Constant>(Arg1)) {
3304  CV0 = ConstantExpr::getIntegerCast(CV0, NewVT, /*isSigned=*/!Zext);
3305  CV1 = ConstantExpr::getIntegerCast(CV1, NewVT, /*isSigned=*/!Zext);
3306 
3307  return replaceInstUsesWith(CI, ConstantExpr::getMul(CV0, CV1));
3308  }
3309 
3310  // Couldn't simplify - canonicalize constant to the RHS.
3311  std::swap(Arg0, Arg1);
3312  }
3313 
3314  // Handle mul by one:
3315  if (Constant *CV1 = dyn_cast<Constant>(Arg1))
3316  if (ConstantInt *Splat =
3317  dyn_cast_or_null<ConstantInt>(CV1->getSplatValue()))
3318  if (Splat->isOne())
3319  return CastInst::CreateIntegerCast(Arg0, II->getType(),
3320  /*isSigned=*/!Zext);
3321 
3322  break;
3323  }
3328  Value *DataArg = II->getArgOperand(0);
3329  Value *KeyArg = II->getArgOperand(1);
3330 
3331  // Try to use the builtin XOR in AESE and AESD to eliminate a prior XOR
3332  Value *Data, *Key;
3333  if (match(KeyArg, m_ZeroInt()) &&
3334  match(DataArg, m_Xor(m_Value(Data), m_Value(Key)))) {
3335  II->setArgOperand(0, Data);
3336  II->setArgOperand(1, Key);
3337  return II;
3338  }
3339  break;
3340  }
3341  case Intrinsic::amdgcn_rcp: {
3342  Value *Src = II->getArgOperand(0);
3343 
3344  // TODO: Move to ConstantFolding/InstSimplify?
3345  if (isa<UndefValue>(Src))
3346  return replaceInstUsesWith(CI, Src);
3347 
3348  if (const ConstantFP *C = dyn_cast<ConstantFP>(Src)) {
3349  const APFloat &ArgVal = C->getValueAPF();
3350  APFloat Val(ArgVal.getSemantics(), 1.0);
3351  APFloat::opStatus Status = Val.divide(ArgVal,
3353  // Only do this if it was exact and therefore not dependent on the
3354  // rounding mode.
3355  if (Status == APFloat::opOK)
3356  return replaceInstUsesWith(CI, ConstantFP::get(II->getContext(), Val));
3357  }
3358 
3359  break;
3360  }
3361  case Intrinsic::amdgcn_rsq: {
3362  Value *Src = II->getArgOperand(0);
3363 
3364  // TODO: Move to ConstantFolding/InstSimplify?
3365  if (isa<UndefValue>(Src))
3366  return replaceInstUsesWith(CI, Src);
3367  break;
3368  }
3371  Value *Src = II->getArgOperand(0);
3372  if (const ConstantFP *C = dyn_cast<ConstantFP>(Src)) {
3373  int Exp;
3374  APFloat Significand = frexp(C->getValueAPF(), Exp,
3376 
3377  if (II->getIntrinsicID() == Intrinsic::amdgcn_frexp_mant) {
3378  return replaceInstUsesWith(CI, ConstantFP::get(II->getContext(),
3379  Significand));
3380  }
3381 
3382  // Match instruction special case behavior.
3383  if (Exp == APFloat::IEK_NaN || Exp == APFloat::IEK_Inf)
3384  Exp = 0;
3385 
3386  return replaceInstUsesWith(CI, ConstantInt::get(II->getType(), Exp));
3387  }
3388 
3389  if (isa<UndefValue>(Src))
3390  return replaceInstUsesWith(CI, UndefValue::get(II->getType()));
3391 
3392  break;
3393  }
3394  case Intrinsic::amdgcn_class: {
3395  enum {
3396  S_NAN = 1 << 0, // Signaling NaN
3397  Q_NAN = 1 << 1, // Quiet NaN
3398  N_INFINITY = 1 << 2, // Negative infinity
3399  N_NORMAL = 1 << 3, // Negative normal
3400  N_SUBNORMAL = 1 << 4, // Negative subnormal
3401  N_ZERO = 1 << 5, // Negative zero
3402  P_ZERO = 1 << 6, // Positive zero
3403  P_SUBNORMAL = 1 << 7, // Positive subnormal
3404  P_NORMAL = 1 << 8, // Positive normal
3405  P_INFINITY = 1 << 9 // Positive infinity
3406  };
3407 
3408  const uint32_t FullMask = S_NAN | Q_NAN | N_INFINITY | N_NORMAL |
3410 
3411  Value *Src0 = II->getArgOperand(0);
3412  Value *Src1 = II->getArgOperand(1);
3413  const ConstantInt *CMask = dyn_cast<ConstantInt>(Src1);
3414  if (!CMask) {
3415  if (isa<UndefValue>(Src0))
3416  return replaceInstUsesWith(*II, UndefValue::get(II->getType()));
3417 
3418  if (isa<UndefValue>(Src1))
3419  return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), false));
3420  break;
3421  }
3422 
3423  uint32_t Mask = CMask->getZExtValue();
3424 
3425  // If all tests are made, it doesn't matter what the value is.
3426  if ((Mask & FullMask) == FullMask)
3427  return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), true));
3428 
3429  if ((Mask & FullMask) == 0)
3430  return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), false));
3431 
3432  if (Mask == (S_NAN | Q_NAN)) {
3433  // Equivalent of isnan. Replace with standard fcmp.
3434  Value *FCmp = Builder.CreateFCmpUNO(Src0, Src0);
3435  FCmp->takeName(II);
3436  return replaceInstUsesWith(*II, FCmp);
3437  }
3438 
3439  if (Mask == (N_ZERO | P_ZERO)) {
3440  // Equivalent of == 0.
3441  Value *FCmp = Builder.CreateFCmpOEQ(
3442  Src0, ConstantFP::get(Src0->getType(), 0.0));
3443 
3444  FCmp->takeName(II);
3445  return replaceInstUsesWith(*II, FCmp);
3446  }
3447 
3448  // fp_class (nnan x), qnan|snan|other -> fp_class (nnan x), other
3449  if (((Mask & S_NAN) || (Mask & Q_NAN)) && isKnownNeverNaN(Src0, &TLI)) {
3450  II->setArgOperand(1, ConstantInt::get(Src1->getType(),
3451  Mask & ~(S_NAN | Q_NAN)));
3452  return II;
3453  }
3454 
3455  const ConstantFP *CVal = dyn_cast<ConstantFP>(Src0);
3456  if (!CVal) {
3457  if (isa<UndefValue>(Src0))
3458  return replaceInstUsesWith(*II, UndefValue::get(II->getType()));
3459 
3460  // Clamp mask to used bits
3461  if ((Mask & FullMask) != Mask) {
3462  CallInst *NewCall = Builder.CreateCall(II->getCalledFunction(),
3463  { Src0, ConstantInt::get(Src1->getType(), Mask & FullMask) }
3464  );
3465 
3466  NewCall->takeName(II);
3467  return replaceInstUsesWith(*II, NewCall);
3468  }
3469 
3470  break;
3471  }
3472 
3473  const APFloat &Val = CVal->getValueAPF();
3474 
3475  bool Result =
3476  ((Mask & S_NAN) && Val.isNaN() && Val.isSignaling()) ||
3477  ((Mask & Q_NAN) && Val.isNaN() && !Val.isSignaling()) ||
3478  ((Mask & N_INFINITY) && Val.isInfinity() && Val.isNegative()) ||
3479  ((Mask & N_NORMAL) && Val.isNormal() && Val.isNegative()) ||
3480  ((Mask & N_SUBNORMAL) && Val.isDenormal() && Val.isNegative()) ||
3481  ((Mask & N_ZERO) && Val.isZero() && Val.isNegative()) ||
3482  ((Mask & P_ZERO) && Val.isZero() && !Val.isNegative()) ||
3483  ((Mask & P_SUBNORMAL) && Val.isDenormal() && !Val.isNegative()) ||
3484  ((Mask & P_NORMAL) && Val.isNormal() && !Val.isNegative()) ||
3485  ((Mask & P_INFINITY) && Val.isInfinity() && !Val.isNegative());
3486 
3487  return replaceInstUsesWith(*II, ConstantInt::get(II->getType(), Result));
3488  }
3490  Value *Src0 = II->getArgOperand(0);
3491  Value *Src1 = II->getArgOperand(1);
3492  if (const ConstantFP *C0 = dyn_cast<ConstantFP>(Src0)) {
3493  if (const ConstantFP *C1 = dyn_cast<ConstantFP>(Src1)) {
3494  const fltSemantics &HalfSem
3495  = II->getType()->getScalarType()->getFltSemantics();
3496  bool LosesInfo;
3497  APFloat Val0 = C0->getValueAPF();
3498  APFloat Val1 = C1->getValueAPF();
3499  Val0.convert(HalfSem, APFloat::rmTowardZero, &LosesInfo);
3500  Val1.convert(HalfSem, APFloat::rmTowardZero, &LosesInfo);
3501 
3502  Constant *Folded = ConstantVector::get({
3503  ConstantFP::get(II->getContext(), Val0),
3504  ConstantFP::get(II->getContext(), Val1) });
3505  return replaceInstUsesWith(*II, Folded);
3506  }
3507  }
3508 
3509  if (isa<UndefValue>(Src0) && isa<UndefValue>(Src1))
3510  return replaceInstUsesWith(*II, UndefValue::get(II->getType()));
3511 
3512  break;
3513  }
3518  Value *Src0 = II->getArgOperand(0);
3519  Value *Src1 = II->getArgOperand(1);
3520 
3521  if (isa<UndefValue>(Src0) && isa<UndefValue>(Src1))
3522  return replaceInstUsesWith(*II, UndefValue::get(II->getType()));
3523 
3524  break;
3525  }
3527  case Intrinsic::amdgcn_sbfe: {
3528  // Decompose simple cases into standard shifts.
3529  Value *Src = II->getArgOperand(0);
3530  if (isa<UndefValue>(Src))
3531  return replaceInstUsesWith(*II, Src);
3532 
3533  unsigned Width;
3534  Type *Ty = II->getType();
3535  unsigned IntSize = Ty->getIntegerBitWidth();
3536 
3537  ConstantInt *CWidth = dyn_cast<ConstantInt>(II->getArgOperand(2));
3538  if (CWidth) {
3539  Width = CWidth->getZExtValue();
3540  if ((Width & (IntSize - 1)) == 0)
3541  return replaceInstUsesWith(*II, ConstantInt::getNullValue(Ty));
3542 
3543  if (Width >= IntSize) {
3544  // Hardware ignores high bits, so remove those.
3545  II->setArgOperand(2, ConstantInt::get(CWidth->getType(),
3546  Width & (IntSize - 1)));
3547  return II;
3548  }
3549  }
3550 
3551  unsigned Offset;
3552  ConstantInt *COffset = dyn_cast<ConstantInt>(II->getArgOperand(1));
3553  if (COffset) {
3554  Offset = COffset->getZExtValue();
3555  if (Offset >= IntSize) {
3556  II->setArgOperand(1, ConstantInt::get(COffset->getType(),
3557  Offset & (IntSize - 1)));
3558  return II;
3559  }
3560  }
3561 
3562  bool Signed = II->getIntrinsicID() == Intrinsic::amdgcn_sbfe;
3563 
3564  if (!CWidth || !COffset)
3565  break;
3566 
3567  // The case of Width == 0 is handled above, which makes this tranformation
3568  // safe. If Width == 0, then the ashr and lshr instructions become poison
3569  // value since the shift amount would be equal to the bit size.
3570  assert(Width != 0);
3571 
3572  // TODO: This allows folding to undef when the hardware has specific
3573  // behavior?
3574  if (Offset + Width < IntSize) {
3575  Value *Shl = Builder.CreateShl(Src, IntSize - Offset - Width);
3576  Value *RightShift = Signed ? Builder.CreateAShr(Shl, IntSize - Width)
3577  : Builder.CreateLShr(Shl, IntSize - Width);
3578  RightShift->takeName(II);
3579  return replaceInstUsesWith(*II, RightShift);
3580  }
3581 
3582  Value *RightShift = Signed ? Builder.CreateAShr(Src, Offset)
3583  : Builder.CreateLShr(Src, Offset);
3584 
3585  RightShift->takeName(II);
3586  return replaceInstUsesWith(*II, RightShift);
3587  }
3588  case Intrinsic::amdgcn_exp:
3590  ConstantInt *En = dyn_cast<ConstantInt>(II->getArgOperand(1));
3591  if (!En) // Illegal.
3592  break;
3593 
3594  unsigned EnBits = En->getZExtValue();
3595  if (EnBits == 0xf)
3596  break; // All inputs enabled.
3597 
3598  bool IsCompr = II->getIntrinsicID() == Intrinsic::amdgcn_exp_compr;
3599  bool Changed = false;
3600  for (int I = 0; I < (IsCompr ? 2 : 4); ++I) {
3601  if ((!IsCompr && (EnBits & (1 << I)) == 0) ||
3602  (IsCompr && ((EnBits & (0x3 << (2 * I))) == 0))) {
3603  Value *Src = II->getArgOperand(I + 2);
3604  if (!isa<UndefValue>(Src)) {
3605  II->setArgOperand(I + 2, UndefValue::get(Src->getType()));
3606  Changed = true;
3607  }
3608  }
3609  }
3610 
3611  if (Changed)
3612  return II;
3613 
3614  break;
3615  }
3616  case Intrinsic::amdgcn_fmed3: {
3617  // Note this does not preserve proper sNaN behavior if IEEE-mode is enabled
3618  // for the shader.
3619 
3620  Value *Src0 = II->getArgOperand(0);
3621  Value *Src1 = II->getArgOperand(1);
3622  Value *Src2 = II->getArgOperand(2);
3623 
3624  // Checking for NaN before canonicalization provides better fidelity when
3625  // mapping other operations onto fmed3 since the order of operands is
3626  // unchanged.
3627  CallInst *NewCall = nullptr;
3628  if (match(Src0, m_NaN()) || isa<UndefValue>(Src0)) {
3629  NewCall = Builder.CreateMinNum(Src1, Src2);
3630  } else if (match(Src1, m_NaN()) || isa<UndefValue>(Src1)) {
3631  NewCall = Builder.CreateMinNum(Src0, Src2);
3632  } else if (match(Src2, m_NaN()) || isa<UndefValue>(Src2)) {
3633  NewCall = Builder.CreateMaxNum(Src0, Src1);
3634  }
3635 
3636  if (NewCall) {
3637  NewCall->copyFastMathFlags(II);
3638  NewCall->takeName(II);
3639  return replaceInstUsesWith(*II, NewCall);
3640  }
3641 
3642  bool Swap = false;
3643  // Canonicalize constants to RHS operands.
3644  //
3645  // fmed3(c0, x, c1) -> fmed3(x, c0, c1)
3646  if (isa<Constant>(Src0) && !isa<Constant>(Src1)) {
3647  std::swap(Src0, Src1);
3648  Swap = true;
3649  }
3650 
3651  if (isa<Constant>(Src1) && !isa<Constant>(Src2)) {
3652  std::swap(Src1, Src2);
3653  Swap = true;
3654  }
3655 
3656  if (isa<Constant>(Src0) && !isa<Constant>(Src1)) {
3657  std::swap(Src0, Src1);
3658  Swap = true;
3659  }
3660 
3661  if (Swap) {
3662  II->setArgOperand(0, Src0);
3663  II->setArgOperand(1, Src1);
3664  II->setArgOperand(2, Src2);
3665  return II;
3666  }
3667 
3668  if (const ConstantFP *C0 = dyn_cast<ConstantFP>(Src0)) {
3669  if (const ConstantFP *C1 = dyn_cast<ConstantFP>(Src1)) {
3670  if (const ConstantFP *C2 = dyn_cast<ConstantFP>(Src2)) {
3671  APFloat Result = fmed3AMDGCN(C0->getValueAPF(), C1->getValueAPF(),
3672  C2->getValueAPF());
3673  return replaceInstUsesWith(*II,
3674  ConstantFP::get(Builder.getContext(), Result));
3675  }
3676  }
3677  }
3678 
3679  break;
3680  }
3682  case Intrinsic::amdgcn_fcmp: {
3683  const ConstantInt *CC = dyn_cast<ConstantInt>(II->getArgOperand(2));
3684  if (!CC)
3685  break;
3686 
3687  // Guard against invalid arguments.
3688  int64_t CCVal = CC->getZExtValue();
3689  bool IsInteger = II->getIntrinsicID() == Intrinsic::amdgcn_icmp;
3690  if ((IsInteger && (CCVal < CmpInst::FIRST_ICMP_PREDICATE ||
3691  CCVal > CmpInst::LAST_ICMP_PREDICATE)) ||
3692  (!IsInteger && (CCVal < CmpInst::FIRST_FCMP_PREDICATE ||
3693  CCVal > CmpInst::LAST_FCMP_PREDICATE)))
3694  break;
3695 
3696  Value *Src0 = II->getArgOperand(0);
3697  Value *Src1 = II->getArgOperand(1);
3698 
3699  if (auto *CSrc0 = dyn_cast<Constant>(Src0)) {
3700  if (auto *CSrc1 = dyn_cast<Constant>(Src1)) {
3701  Constant *CCmp = ConstantExpr::getCompare(CCVal, CSrc0, CSrc1);
3702  if (CCmp->isNullValue()) {
3703  return replaceInstUsesWith(
3704  *II, ConstantExpr::getSExt(CCmp, II->getType()));
3705  }
3706 
3707  // The result of V_ICMP/V_FCMP assembly instructions (which this
3708  // intrinsic exposes) is one bit per thread, masked with the EXEC
3709  // register (which contains the bitmask of live threads). So a
3710  // comparison that always returns true is the same as a read of the
3711  // EXEC register.
3713  II->getModule(), Intrinsic::read_register, II->getType());
3714  Metadata *MDArgs[] = {MDString::get(II->getContext(), "exec")};
3715  MDNode *MD = MDNode::get(II->getContext(), MDArgs);
3716  Value *Args[] = {MetadataAsValue::get(II->getContext(), MD)};
3717  CallInst *NewCall = Builder.CreateCall(NewF, Args);
3720  NewCall->takeName(II);
3721  return replaceInstUsesWith(*II, NewCall);
3722  }
3723 
3724  // Canonicalize constants to RHS.
3725  CmpInst::Predicate SwapPred
3726  = CmpInst::getSwappedPredicate(static_cast<CmpInst::Predicate>(CCVal));
3727  II->setArgOperand(0, Src1);
3728  II->setArgOperand(1, Src0);
3729  II->setArgOperand(2, ConstantInt::get(CC->getType(),
3730  static_cast<int>(SwapPred)));
3731  return II;
3732  }
3733 
3734  if (CCVal != CmpInst::ICMP_EQ && CCVal != CmpInst::ICMP_NE)
3735  break;
3736 
3737  // Canonicalize compare eq with true value to compare != 0
3738  // llvm.amdgcn.icmp(zext (i1 x), 1, eq)
3739  // -> llvm.amdgcn.icmp(zext (i1 x), 0, ne)
3740  // llvm.amdgcn.icmp(sext (i1 x), -1, eq)
3741  // -> llvm.amdgcn.icmp(sext (i1 x), 0, ne)
3742  Value *ExtSrc;
3743  if (CCVal == CmpInst::ICMP_EQ &&
3744  ((match(Src1, m_One()) && match(Src0, m_ZExt(m_Value(ExtSrc)))) ||
3745  (match(Src1, m_AllOnes()) && match(Src0, m_SExt(m_Value(ExtSrc))))) &&
3746  ExtSrc->getType()->isIntegerTy(1)) {
3747  II->setArgOperand(1, ConstantInt::getNullValue(Src1->getType()));
3748  II->setArgOperand(2, ConstantInt::get(CC->getType(), CmpInst::ICMP_NE));
3749  return II;
3750  }
3751 
3752  CmpInst::Predicate SrcPred;
3753  Value *SrcLHS;
3754  Value *SrcRHS;
3755 
3756  // Fold compare eq/ne with 0 from a compare result as the predicate to the
3757  // intrinsic. The typical use is a wave vote function in the library, which
3758  // will be fed from a user code condition compared with 0. Fold in the
3759  // redundant compare.
3760 
3761  // llvm.amdgcn.icmp([sz]ext ([if]cmp pred a, b), 0, ne)
3762  // -> llvm.amdgcn.[if]cmp(a, b, pred)
3763  //
3764  // llvm.amdgcn.icmp([sz]ext ([if]cmp pred a, b), 0, eq)
3765  // -> llvm.amdgcn.[if]cmp(a, b, inv pred)
3766  if (match(Src1, m_Zero()) &&
3767  match(Src0,
3768  m_ZExtOrSExt(m_Cmp(SrcPred, m_Value(SrcLHS), m_Value(SrcRHS))))) {
3769  if (CCVal == CmpInst::ICMP_EQ)
3770  SrcPred = CmpInst::getInversePredicate(SrcPred);
3771 
3772  Intrinsic::ID NewIID = CmpInst::isFPPredicate(SrcPred) ?
3774 
3775  Type *Ty = SrcLHS->getType();
3776  if (auto *CmpType = dyn_cast<IntegerType>(Ty)) {
3777  // Promote to next legal integer type.
3778  unsigned Width = CmpType->getBitWidth();
3779  unsigned NewWidth = Width;
3780 
3781  // Don't do anything for i1 comparisons.
3782  if (Width == 1)
3783  break;
3784 
3785  if (Width <= 16)
3786  NewWidth = 16;
3787  else if (Width <= 32)
3788  NewWidth = 32;
3789  else if (Width <= 64)
3790  NewWidth = 64;
3791  else if (Width > 64)
3792  break; // Can't handle this.
3793 
3794  if (Width != NewWidth) {
3795  IntegerType *CmpTy = Builder.getIntNTy(NewWidth);
3796  if (CmpInst::isSigned(SrcPred)) {
3797  SrcLHS = Builder.CreateSExt(SrcLHS, CmpTy);
3798  SrcRHS = Builder.CreateSExt(SrcRHS, CmpTy);
3799  } else {
3800  SrcLHS = Builder.CreateZExt(SrcLHS, CmpTy);
3801  SrcRHS = Builder.CreateZExt(SrcRHS, CmpTy);
3802  }
3803  }
3804  } else if (!Ty->isFloatTy() && !Ty->isDoubleTy() && !Ty->isHalfTy())
3805  break;
3806 
3807  Value *NewF = Intrinsic::getDeclaration(II->getModule(), NewIID,
3808  SrcLHS->getType());
3809  Value *Args[] = { SrcLHS, SrcRHS,
3810  ConstantInt::get(CC->getType(), SrcPred) };
3811  CallInst *NewCall = Builder.CreateCall(NewF, Args);
3812  NewCall->takeName(II);
3813  return replaceInstUsesWith(*II, NewCall);
3814  }
3815 
3816  break;
3817  }
3819  // wqm_vote is identity when the argument is constant.
3820  if (!isa<Constant>(II->getArgOperand(0)))
3821  break;
3822 
3823  return replaceInstUsesWith(*II, II->getArgOperand(0));
3824  }
3825  case Intrinsic::amdgcn_kill: {
3826  const ConstantInt *C = dyn_cast<ConstantInt>(II->getArgOperand(0));
3827  if (!C || !C->getZExtValue())
3828  break;
3829 
3830  // amdgcn.kill(i1 1) is a no-op
3831  return eraseInstFromFunction(CI);
3832  }
3834  Value *Old = II->getArgOperand(0);
3835 
3836  auto BC = dyn_cast<ConstantInt>(II->getArgOperand(5));
3837  auto RM = dyn_cast<ConstantInt>(II->getArgOperand(3));
3838  auto BM = dyn_cast<ConstantInt>(II->getArgOperand(4));
3839  if (!BC || !RM || !BM ||
3840  BC->isZeroValue() ||
3841  RM->getZExtValue() != 0xF ||
3842  BM->getZExtValue() != 0xF ||
3843  isa<UndefValue>(Old))
3844  break;
3845 
3846  // If bound_ctrl = 1, row mask = bank mask = 0xf we can omit old value.
3847  II->setOperand(0, UndefValue::get(Old->getType()));
3848  return II;
3849  }
3850  case Intrinsic::stackrestore: {
3851  // If the save is right next to the restore, remove the restore. This can
3852  // happen when variable allocas are DCE'd.
3853  if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
3854  if (SS->getIntrinsicID() == Intrinsic::stacksave) {
3855  // Skip over debug info.
3856  if (SS->getNextNonDebugInstruction() == II) {
3857  return eraseInstFromFunction(CI);
3858  }
3859  }
3860  }
3861 
3862  // Scan down this block to see if there is another stack restore in the
3863  // same block without an intervening call/alloca.
3864  BasicBlock::iterator BI(II);
3865  Instruction *TI = II->getParent()->getTerminator();
3866  bool CannotRemove = false;
3867  for (++BI; &*BI != TI; ++BI) {
3868  if (isa<AllocaInst>(BI)) {
3869  CannotRemove = true;
3870  break;
3871  }
3872  if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
3873  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) {
3874  // If there is a stackrestore below this one, remove this one.
3875  if (II->getIntrinsicID() == Intrinsic::stackrestore)
3876  return eraseInstFromFunction(CI);
3877 
3878  // Bail if we cross over an intrinsic with side effects, such as
3879  // llvm.stacksave, llvm.read_register, or llvm.setjmp.
3880  if (II->mayHaveSideEffects()) {
3881  CannotRemove = true;
3882  break;
3883  }
3884  } else {
3885  // If we found a non-intrinsic call, we can't remove the stack
3886  // restore.
3887  CannotRemove = true;
3888  break;
3889  }
3890  }
3891  }
3892 
3893  // If the stack restore is in a return, resume, or unwind block and if there
3894  // are no allocas or calls between the restore and the return, nuke the
3895  // restore.
3896  if (!CannotRemove && (isa<ReturnInst>(TI) || isa<ResumeInst>(TI)))
3897  return eraseInstFromFunction(CI);
3898  break;
3899  }
3901  // Asan needs to poison memory to detect invalid access which is possible
3902  // even for empty lifetime range.
3903  if (II->getFunction()->hasFnAttribute(Attribute::SanitizeAddress) ||
3904  II->getFunction()->hasFnAttribute(Attribute::SanitizeHWAddress))
3905  break;
3906 
3908  Intrinsic::lifetime_end, *this))
3909  return nullptr;
3910  break;
3911  case Intrinsic::assume: {
3912  Value *IIOperand = II->getArgOperand(0);
3913  // Remove an assume if it is followed by an identical assume.
3914  // TODO: Do we need this? Unless there are conflicting assumptions, the
3915  // computeKnownBits(IIOperand) below here eliminates redundant assumes.
3917  if (match(Next, m_Intrinsic<Intrinsic::assume>(m_Specific(IIOperand))))
3918  return eraseInstFromFunction(CI);
3919 
3920  // Canonicalize assume(a && b) -> assume(a); assume(b);
3921  // Note: New assumption intrinsics created here are registered by
3922  // the InstCombineIRInserter object.
3923  Value *AssumeIntrinsic = II->getCalledValue(), *A, *B;
3924  if (match(IIOperand, m_And(m_Value(A), m_Value(B)))) {
3925  Builder.CreateCall(AssumeIntrinsic, A, II->getName());
3926  Builder.CreateCall(AssumeIntrinsic, B, II->getName());
3927  return eraseInstFromFunction(*II);
3928  }
3929  // assume(!(a || b)) -> assume(!a); assume(!b);
3930  if (match(IIOperand, m_Not(m_Or(m_Value(A), m_Value(B))))) {
3931  Builder.CreateCall(AssumeIntrinsic, Builder.CreateNot(A), II->getName());
3932  Builder.CreateCall(AssumeIntrinsic, Builder.CreateNot(B), II->getName());
3933  return eraseInstFromFunction(*II);
3934  }
3935 
3936  // assume( (load addr) != null ) -> add 'nonnull' metadata to load
3937  // (if assume is valid at the load)
3938  CmpInst::Predicate Pred;
3939  Instruction *LHS;
3940  if (match(IIOperand, m_ICmp(Pred, m_Instruction(LHS), m_Zero())) &&
3941  Pred == ICmpInst::ICMP_NE && LHS->getOpcode() == Instruction::Load &&
3942  LHS->getType()->isPointerTy() &&
3943  isValidAssumeForContext(II, LHS, &DT)) {
3944  MDNode *MD = MDNode::get(II->getContext(), None);
3946  return eraseInstFromFunction(*II);
3947 
3948  // TODO: apply nonnull return attributes to calls and invokes
3949  // TODO: apply range metadata for range check patterns?
3950  }
3951 
3952  // If there is a dominating assume with the same condition as this one,
3953  // then this one is redundant, and should be removed.
3954  KnownBits Known(1);
3955  computeKnownBits(IIOperand, Known, 0, II);
3956  if (Known.isAllOnes())
3957  return eraseInstFromFunction(*II);
3958 
3959  // Update the cache of affected values for this assumption (we might be
3960  // here because we just simplified the condition).
3961  AC.updateAffectedValues(II);
3962  break;
3963  }
3965  // Translate facts known about a pointer before relocating into
3966  // facts about the relocate value, while being careful to
3967  // preserve relocation semantics.
3968  Value *DerivedPtr = cast<GCRelocateInst>(II)->getDerivedPtr();
3969 
3970  // Remove the relocation if unused, note that this check is required
3971  // to prevent the cases below from looping forever.
3972  if (II->use_empty())
3973  return eraseInstFromFunction(*II);
3974 
3975  // Undef is undef, even after relocation.
3976  // TODO: provide a hook for this in GCStrategy. This is clearly legal for
3977  // most practical collectors, but there was discussion in the review thread
3978  // about whether it was legal for all possible collectors.
3979  if (isa<UndefValue>(DerivedPtr))
3980  // Use undef of gc_relocate's type to replace it.
3981  return replaceInstUsesWith(*II, UndefValue::get(II->getType()));
3982 
3983  if (auto *PT = dyn_cast<PointerType>(II->getType())) {
3984  // The relocation of null will be null for most any collector.
3985  // TODO: provide a hook for this in GCStrategy. There might be some
3986  // weird collector this property does not hold for.
3987  if (isa<ConstantPointerNull>(DerivedPtr))
3988  // Use null-pointer of gc_relocate's type to replace it.
3989  return replaceInstUsesWith(*II, ConstantPointerNull::get(PT));
3990 
3991  // isKnownNonNull -> nonnull attribute
3992  if (!II->hasRetAttr(Attribute::NonNull) &&
3993  isKnownNonZero(DerivedPtr, DL, 0, &AC, II, &DT)) {
3995  return II;
3996  }
3997  }
3998 
3999  // TODO: bitcast(relocate(p)) -> relocate(bitcast(p))
4000  // Canonicalize on the type from the uses to the defs
4001 
4002  // TODO: relocate((gep p, C, C2, ...)) -> gep(relocate(p), C, C2, ...)
4003  break;
4004  }
4005 
4007  // Is this guard followed by another guard? We scan forward over a small
4008  // fixed window of instructions to handle common cases with conditions
4009  // computed between guards.
4010  Instruction *NextInst = II->getNextNode();
4011  for (unsigned i = 0; i < GuardWideningWindow; i++) {
4012  // Note: Using context-free form to avoid compile time blow up
4013  if (!isSafeToSpeculativelyExecute(NextInst))
4014  break;
4015  NextInst = NextInst->getNextNode();
4016  }
4017  Value *NextCond = nullptr;
4018  if (match(NextInst,
4019  m_Intrinsic<Intrinsic::experimental_guard>(m_Value(NextCond)))) {
4020  Value *CurrCond = II->getArgOperand(0);
4021 
4022  // Remove a guard that it is immediately preceded by an identical guard.
4023  if (CurrCond == NextCond)
4024  return eraseInstFromFunction(*NextInst);
4025 
4026  // Otherwise canonicalize guard(a); guard(b) -> guard(a & b).
4027  Instruction* MoveI = II->getNextNode();
4028  while (MoveI != NextInst) {
4029  auto *Temp = MoveI;
4030  MoveI = MoveI->getNextNode();
4031  Temp->moveBefore(II);
4032  }
4033  II->setArgOperand(0, Builder.CreateAnd(CurrCond, NextCond));
4034  return eraseInstFromFunction(*NextInst);
4035  }
4036  break;
4037  }
4038  }
4039  return visitCallSite(II);
4040 }
4041 
4042 // Fence instruction simplification
4044  // Remove identical consecutive fences.
4046  if (auto *NFI = dyn_cast<FenceInst>(Next))
4047  if (FI.isIdenticalTo(NFI))
4048  return eraseInstFromFunction(FI);
4049  return nullptr;
4050 }
4051 
4052 // InvokeInst simplification
4054  return visitCallSite(&II);
4055 }
4056 
4057 /// If this cast does not affect the value passed through the varargs area, we
4058 /// can eliminate the use of the cast.
4060  const DataLayout &DL,
4061  const CastInst *const CI,
4062  const int ix) {
4063  if (!CI->isLosslessCast())
4064  return false;
4065 
4066  // If this is a GC intrinsic, avoid munging types. We need types for
4067  // statepoint reconstruction in SelectionDAG.
4068  // TODO: This is probably something which should be expanded to all
4069  // intrinsics since the entire point of intrinsics is that
4070  // they are understandable by the optimizer.
4071  if (isStatepoint(CS) || isGCRelocate(CS) || isGCResult(CS))
4072  return false;
4073 
4074  // The size of ByVal or InAlloca arguments is derived from the type, so we
4075  // can't change to a type with a different size. If the size were
4076  // passed explicitly we could avoid this check.
4077  if (!CS.isByValOrInAllocaArgument(ix))
4078  return true;
4079 
4080  Type* SrcTy =
4081  cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
4082  Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
4083  if (!SrcTy->isSized() || !DstTy->isSized())
4084  return false;
4085  if (DL.getTypeAllocSize(SrcTy) != DL.getTypeAllocSize(DstTy))
4086  return false;
4087  return true;
4088 }
4089 
4090 Instruction *InstCombiner::tryOptimizeCall(CallInst *CI) {
4091  if (!CI->getCalledFunction()) return nullptr;
4092 
4093  auto InstCombineRAUW = [this](Instruction *From, Value *With) {
4094  replaceInstUsesWith(*From, With);
4095  };
4096  auto InstCombineErase = [this](Instruction *I) {
4097  eraseInstFromFunction(*I);
4098  };
4099  LibCallSimplifier Simplifier(DL, &TLI, ORE, InstCombineRAUW,
4100  InstCombineErase);
4101  if (Value *With = Simplifier.optimizeCall(CI)) {
4102  ++NumSimplified;
4103  return CI->use_empty() ? CI : replaceInstUsesWith(*CI, With);
4104  }
4105 
4106  return nullptr;
4107 }
4108 
4110  // Strip off at most one level of pointer casts, looking for an alloca. This
4111  // is good enough in practice and simpler than handling any number of casts.
4112  Value *Underlying = TrampMem->stripPointerCasts();
4113  if (Underlying != TrampMem &&
4114  (!Underlying->hasOneUse() || Underlying->user_back() != TrampMem))
4115  return nullptr;
4116  if (!isa<AllocaInst>(Underlying))
4117  return nullptr;
4118 
4119  IntrinsicInst *InitTrampoline = nullptr;
4120  for (User *U : TrampMem->users()) {
4122  if (!II)
4123  return nullptr;
4125  if (InitTrampoline)
4126  // More than one init_trampoline writes to this value. Give up.
4127  return nullptr;
4128  InitTrampoline = II;
4129  continue;
4130  }
4132  // Allow any number of calls to adjust.trampoline.
4133  continue;
4134  return nullptr;
4135  }
4136 
4137  // No call to init.trampoline found.
4138  if (!InitTrampoline)
4139  return nullptr;
4140 
4141  // Check that the alloca is being used in the expected way.
4142  if (InitTrampoline->getOperand(0) != TrampMem)
4143  return nullptr;
4144 
4145  return InitTrampoline;
4146 }
4147 
4149  Value *TrampMem) {
4150  // Visit all the previous instructions in the basic block, and try to find a
4151  // init.trampoline which has a direct path to the adjust.trampoline.
4152  for (BasicBlock::iterator I = AdjustTramp->getIterator(),
4153  E = AdjustTramp->getParent()->begin();
4154  I != E;) {
4155  Instruction *Inst = &*--I;
4156  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
4157  if (II->getIntrinsicID() == Intrinsic::init_trampoline &&
4158  II->getOperand(0) == TrampMem)
4159  return II;
4160  if (Inst->mayWriteToMemory())
4161  return nullptr;
4162  }
4163  return nullptr;
4164 }
4165 
4166 // Given a call to llvm.adjust.trampoline, find and return the corresponding
4167 // call to llvm.init.trampoline if the call to the trampoline can be optimized
4168 // to a direct call to a function. Otherwise return NULL.
4170  Callee = Callee->stripPointerCasts();
4171  IntrinsicInst *AdjustTramp = dyn_cast<IntrinsicInst>(Callee);
4172  if (!AdjustTramp ||
4173  AdjustTramp->getIntrinsicID() != Intrinsic::adjust_trampoline)
4174  return nullptr;
4175 
4176  Value *TrampMem = AdjustTramp->getOperand(0);
4177 
4179  return IT;
4180  if (IntrinsicInst *IT = findInitTrampolineFromBB(AdjustTramp, TrampMem))
4181  return IT;
4182  return nullptr;
4183 }
4184 
4185 /// Improvements for call and invoke instructions.
4186 Instruction *InstCombiner::visitCallSite(CallSite CS) {
4187  if (isAllocLikeFn(CS.getInstruction(), &TLI))
4188  return visitAllocSite(*CS.getInstruction());
4189 
4190  bool Changed = false;
4191 
4192  // Mark any parameters that are known to be non-null with the nonnull
4193  // attribute. This is helpful for inlining calls to functions with null
4194  // checks on their arguments.
4195  SmallVector<unsigned, 4> ArgNos;
4196  unsigned ArgNo = 0;
4197 
4198  for (Value *V : CS.args()) {
4199  if (V->getType()->isPointerTy() &&
4200  !CS.paramHasAttr(ArgNo, Attribute::NonNull) &&
4201  isKnownNonZero(V, DL, 0, &AC, CS.getInstruction(), &DT))
4202  ArgNos.push_back(ArgNo);
4203  ArgNo++;
4204  }
4205 
4206  assert(ArgNo == CS.arg_size() && "sanity check");
4207 
4208  if (!ArgNos.empty()) {
4209  AttributeList AS = CS.getAttributes();
4210  LLVMContext &Ctx = CS.getInstruction()->getContext();
4211  AS = AS.addParamAttribute(Ctx, ArgNos,
4213  CS.setAttributes(AS);
4214  Changed = true;
4215  }
4216 
4217  // If the callee is a pointer to a function, attempt to move any casts to the
4218  // arguments of the call/invoke.
4219  Value *Callee = CS.getCalledValue();
4220  if (!isa<Function>(Callee) && transformConstExprCastCall(CS))
4221  return nullptr;
4222 
4223  if (Function *CalleeF = dyn_cast<Function>(Callee)) {
4224  // Remove the convergent attr on calls when the callee is not convergent.
4225  if (CS.isConvergent() && !CalleeF->isConvergent() &&
4226  !CalleeF->isIntrinsic()) {
4227  LLVM_DEBUG(dbgs() << "Removing convergent attr from instr "
4228  << CS.getInstruction() << "\n");
4229  CS.setNotConvergent();
4230  return CS.getInstruction();
4231  }
4232 
4233  // If the call and callee calling conventions don't match, this call must
4234  // be unreachable, as the call is undefined.
4235  if (CalleeF->getCallingConv() != CS.getCallingConv() &&
4236  // Only do this for calls to a function with a body. A prototype may
4237  // not actually end up matching the implementation's calling conv for a
4238  // variety of reasons (e.g. it may be written in assembly).
4239  !CalleeF->isDeclaration()) {
4240  Instruction *OldCall = CS.getInstruction();
4241  new StoreInst(ConstantInt::getTrue(Callee->getContext()),
4243  OldCall);
4244  // If OldCall does not return void then replaceAllUsesWith undef.
4245  // This allows ValueHandlers and custom metadata to adjust itself.
4246  if (!OldCall->getType()->isVoidTy())
4247  replaceInstUsesWith(*OldCall, UndefValue::get(OldCall->getType()));
4248  if (isa<CallInst>(OldCall))
4249  return eraseInstFromFunction(*OldCall);
4250 
4251  // We cannot remove an invoke, because it would change the CFG, just
4252  // change the callee to a null pointer.
4253  cast<InvokeInst>(OldCall)->setCalledFunction(
4254  Constant::getNullValue(CalleeF->getType()));
4255  return nullptr;
4256  }
4257  }
4258 
4259  if ((isa<ConstantPointerNull>(Callee) &&
4261  isa<UndefValue>(Callee)) {
4262  // If CS does not return void then replaceAllUsesWith undef.
4263  // This allows ValueHandlers and custom metadata to adjust itself.
4264  if (!CS.getInstruction()->getType()->isVoidTy())
4265  replaceInstUsesWith(*CS.getInstruction(),
4267 
4268  if (isa<InvokeInst>(CS.getInstruction())) {
4269  // Can't remove an invoke because we cannot change the CFG.
4270  return nullptr;
4271  }
4272 
4273  // This instruction is not reachable, just remove it. We insert a store to
4274  // undef so that we know that this code is not reachable, despite the fact
4275  // that we can't modify the CFG here.
4276  new StoreInst(ConstantInt::getTrue(Callee->getContext()),
4278  CS.getInstruction());
4279 
4280  return eraseInstFromFunction(*CS.getInstruction());
4281  }
4282 
4283  if (IntrinsicInst *II = findInitTrampoline(Callee))
4284  return transformCallThroughTrampoline(CS, II);
4285 
4286  PointerType *PTy = cast<PointerType>(Callee->getType());
4287  FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
4288  if (FTy->isVarArg()) {
4289  int ix = FTy->getNumParams();
4290  // See if we can optimize any arguments passed through the varargs area of
4291  // the call.
4292  for (CallSite::arg_iterator I = CS.arg_begin() + FTy->getNumParams(),
4293  E = CS.arg_end(); I != E; ++I, ++ix) {
4294  CastInst *CI = dyn_cast<CastInst>(*I);
4295  if (CI && isSafeToEliminateVarargsCast(CS, DL, CI, ix)) {
4296  *I = CI->getOperand(0);
4297  Changed = true;
4298  }
4299  }
4300  }
4301 
4302  if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
4303  // Inline asm calls cannot throw - mark them 'nounwind'.
4304  CS.setDoesNotThrow();
4305  Changed = true;
4306  }
4307 
4308  // Try to optimize the call if possible, we require DataLayout for most of
4309  // this. None of these calls are seen as possibly dead so go ahead and
4310  // delete the instruction now.
4311  if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
4312  Instruction *I = tryOptimizeCall(CI);
4313  // If we changed something return the result, etc. Otherwise let
4314  // the fallthrough check.
4315  if (I) return eraseInstFromFunction(*I);
4316  }
4317 
4318  return Changed ? CS.getInstruction() : nullptr;
4319 }
4320 
4321 /// If the callee is a constexpr cast of a function, attempt to move the cast to
4322 /// the arguments of the call/invoke.
4323 bool InstCombiner::transformConstExprCastCall(CallSite CS) {
4325  if (!Callee)
4326  return false;
4327 
4328  // If this is a call to a thunk function, don't remove the cast. Thunks are
4329  // used to transparently forward all incoming parameters and outgoing return
4330  // values, so it's important to leave the cast in place.
4331  if (Callee->hasFnAttribute("thunk"))
4332  return false;
4333 
4334  // If this is a musttail call, the callee's prototype must match the caller's
4335  // prototype with the exception of pointee types. The code below doesn't
4336  // implement that, so we can't do this transform.
4337  // TODO: Do the transform if it only requires adding pointer casts.
4338  if (CS.isMustTailCall())
4339  return false;
4340 
4341  Instruction *Caller = CS.getInstruction();
4342  const AttributeList &CallerPAL = CS.getAttributes();
4343 
4344  // Okay, this is a cast from a function to a different type. Unless doing so
4345  // would cause a type conversion of one of our arguments, change this call to
4346  // be a direct call with arguments casted to the appropriate types.
4347  FunctionType *FT = Callee->getFunctionType();
4348  Type *OldRetTy = Caller->getType();
4349  Type *NewRetTy = FT->getReturnType();
4350 
4351  // Check to see if we are changing the return type...
4352  if (OldRetTy != NewRetTy) {
4353 
4354  if (NewRetTy->isStructTy())
4355  return false; // TODO: Handle multiple return values.
4356 
4357  if (!CastInst::isBitOrNoopPointerCastable(NewRetTy, OldRetTy, DL)) {
4358  if (Callee->isDeclaration())
4359  return false; // Cannot transform this return value.
4360 
4361  if (!Caller->use_empty() &&
4362  // void -> non-void is handled specially
4363  !NewRetTy->isVoidTy())
4364  return false; // Cannot transform this return value.
4365  }
4366 
4367  if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
4368  AttrBuilder RAttrs(CallerPAL, AttributeList::ReturnIndex);
4369  if (RAttrs.overlaps(AttributeFuncs::typeIncompatible(NewRetTy)))
4370  return false; // Attribute not compatible with transformed value.
4371  }
4372 
4373  // If the callsite is an invoke instruction, and the return value is used by
4374  // a PHI node in a successor, we cannot change the return type of the call
4375  // because there is no place to put the cast instruction (without breaking
4376  // the critical edge). Bail out in this case.
4377  if (!Caller->use_empty())
4378  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
4379  for (User *U : II->users())
4380  if (PHINode *PN = dyn_cast<PHINode>(U))
4381  if (PN->getParent() == II->getNormalDest() ||
4382  PN->getParent() == II->getUnwindDest())
4383  return false;
4384  }
4385 
4386  unsigned NumActualArgs = CS.arg_size();
4387  unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
4388 
4389  // Prevent us turning:
4390  // declare void @takes_i32_inalloca(i32* inalloca)
4391  // call void bitcast (void (i32*)* @takes_i32_inalloca to void (i32)*)(i32 0)
4392  //
4393  // into:
4394  // call void @takes_i32_inalloca(i32* null)
4395  //
4396  // Similarly, avoid folding away bitcasts of byval calls.
4397  if (Callee->getAttributes().hasAttrSomewhere(Attribute::InAlloca) ||
4398  Callee->getAttributes().hasAttrSomewhere(Attribute::ByVal))
4399  return false;
4400 
4402  for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
4403  Type *ParamTy = FT->getParamType(i);
4404  Type *ActTy = (*AI)->getType();
4405 
4406  if (!CastInst::isBitOrNoopPointerCastable(ActTy, ParamTy, DL))
4407  return false; // Cannot transform this parameter value.
4408 
4409  if (AttrBuilder(CallerPAL.getParamAttributes(i))
4410  .overlaps(AttributeFuncs::typeIncompatible(ParamTy)))
4411  return false; // Attribute not compatible with transformed value.
4412 
4413  if (CS.isInAllocaArgument(i))
4414  return false; // Cannot transform to and from inalloca.
4415 
4416  // If the parameter is passed as a byval argument, then we have to have a
4417  // sized type and the sized type has to have the same size as the old type.
4418  if (ParamTy != ActTy && CallerPAL.hasParamAttribute(i, Attribute::ByVal)) {
4419  PointerType *ParamPTy = dyn_cast<PointerType>(ParamTy);
4420  if (!ParamPTy || !ParamPTy->getElementType()->isSized())
4421  return false;
4422 
4423  Type *CurElTy = ActTy->getPointerElementType();
4424  if (DL.getTypeAllocSize(CurElTy) !=
4425  DL.getTypeAllocSize(ParamPTy->getElementType()))
4426  return false;
4427  }
4428  }
4429 
4430  if (Callee->isDeclaration()) {
4431  // Do not delete arguments unless we have a function body.
4432  if (FT->getNumParams() < NumActualArgs && !FT->isVarArg())
4433  return false;
4434 
4435  // If the callee is just a declaration, don't change the varargsness of the
4436  // call. We don't want to introduce a varargs call where one doesn't
4437  // already exist.
4438  PointerType *APTy = cast<PointerType>(CS.getCalledValue()->getType());
4439  if (FT->isVarArg()!=cast<FunctionType>(APTy->getElementType())->isVarArg())
4440  return false;
4441 
4442  // If both the callee and the cast type are varargs, we still have to make
4443  // sure the number of fixed parameters are the same or we have the same
4444  // ABI issues as if we introduce a varargs call.
4445  if (FT->isVarArg() &&
4446  cast<FunctionType>(APTy->getElementType())->isVarArg() &&
4447  FT->getNumParams() !=
4448  cast<FunctionType>(APTy->getElementType())->getNumParams())
4449  return false;
4450  }
4451 
4452  if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
4453  !CallerPAL.isEmpty()) {
4454  // In this case we have more arguments than the new function type, but we
4455  // won't be dropping them. Check that these extra arguments have attributes
4456  // that are compatible with being a vararg call argument.
4457  unsigned SRetIdx;
4458  if (CallerPAL.hasAttrSomewhere(Attribute::StructRet, &SRetIdx) &&
4459  SRetIdx > FT->getNumParams())
4460  return false;
4461  }
4462 
4463  // Okay, we decided that this is a safe thing to do: go ahead and start
4464  // inserting cast instructions as necessary.
4467  Args.reserve(NumActualArgs);
4468  ArgAttrs.reserve(NumActualArgs);
4469 
4470  // Get any return attributes.
4471  AttrBuilder RAttrs(CallerPAL, AttributeList::ReturnIndex);
4472 
4473  // If the return value is not being used, the type may not be compatible
4474  // with the existing attributes. Wipe out any problematic attributes.
4475  RAttrs.remove(AttributeFuncs::typeIncompatible(NewRetTy));
4476 
4477  AI = CS.arg_begin();
4478  for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
4479  Type *ParamTy = FT->getParamType(i);
4480 
4481  Value *NewArg = *AI;
4482  if ((*AI)->getType() != ParamTy)
4483  NewArg = Builder.CreateBitOrPointerCast(*AI, ParamTy);
4484  Args.push_back(NewArg);
4485 
4486  // Add any parameter attributes.
4487  ArgAttrs.push_back(CallerPAL.getParamAttributes(i));
4488  }
4489 
4490  // If the function takes more arguments than the call was taking, add them
4491  // now.
4492  for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) {
4494  ArgAttrs.push_back(AttributeSet());
4495  }
4496 
4497  // If we are removing arguments to the function, emit an obnoxious warning.
4498  if (FT->getNumParams() < NumActualArgs) {
4499  // TODO: if (!FT->isVarArg()) this call may be unreachable. PR14722
4500  if (FT->isVarArg()) {
4501  // Add all of the arguments in their promoted form to the arg list.
4502  for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
4503  Type *PTy = getPromotedType((*AI)->getType());
4504  Value *NewArg = *AI;
4505  if (PTy != (*AI)->getType()) {
4506  // Must promote to pass through va_arg area!
4507  Instruction::CastOps opcode =
4508  CastInst::getCastOpcode(*AI, false, PTy, false);
4509  NewArg = Builder.CreateCast(opcode, *AI, PTy);
4510  }
4511  Args.push_back(NewArg);
4512 
4513  // Add any parameter attributes.
4514  ArgAttrs.push_back(CallerPAL.getParamAttributes(i));
4515  }
4516  }
4517  }
4518 
4519  AttributeSet FnAttrs = CallerPAL.getFnAttributes();
4520 
4521  if (NewRetTy->isVoidTy())
4522  Caller->setName(""); // Void type should not have a name.
4523 
4524  assert((ArgAttrs.size() == FT->getNumParams() || FT->isVarArg()) &&
4525  "missing argument attributes");
4526  LLVMContext &Ctx = Callee->getContext();
4527  AttributeList NewCallerPAL = AttributeList::get(
4528  Ctx, FnAttrs, AttributeSet::get(Ctx, RAttrs), ArgAttrs);
4529 
4531  CS.getOperandBundlesAsDefs(OpBundles);
4532 
4533  CallSite NewCS;
4534  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
4535  NewCS = Builder.CreateInvoke(Callee, II->getNormalDest(),
4536  II->getUnwindDest(), Args, OpBundles);
4537  } else {
4538  NewCS = Builder.CreateCall(Callee, Args, OpBundles);
4539  cast<CallInst>(NewCS.getInstruction())
4540  ->setTailCallKind(cast<CallInst>(Caller)->getTailCallKind());
4541  }
4542  NewCS->takeName(Caller);
4543  NewCS.setCallingConv(CS.getCallingConv());
4544  NewCS.setAttributes(NewCallerPAL);
4545 
4546  // Preserve the weight metadata for the new call instruction. The metadata
4547  // is used by SamplePGO to check callsite's hotness.
4548  uint64_t W;
4549  if (Caller->extractProfTotalWeight(W))
4550  NewCS->setProfWeight(W);
4551 
4552  // Insert a cast of the return type as necessary.
4553  Instruction *NC = NewCS.getInstruction();
4554  Value *NV = NC;
4555  if (OldRetTy != NV->getType() && !Caller->use_empty()) {
4556  if (!NV->getType()->isVoidTy()) {
4557  NV = NC = CastInst::CreateBitOrPointerCast(NC, OldRetTy);
4558  NC->setDebugLoc(Caller->getDebugLoc());
4559 
4560  // If this is an invoke instruction, we should insert it after the first
4561  // non-phi, instruction in the normal successor block.
4562  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
4563  BasicBlock::iterator I = II->getNormalDest()->getFirstInsertionPt();
4564  InsertNewInstBefore(NC, *I);
4565  } else {
4566  // Otherwise, it's a call, just insert cast right after the call.
4567  InsertNewInstBefore(NC, *Caller);
4568  }
4569  Worklist.AddUsersToWorkList(*Caller);
4570  } else {
4571  NV = UndefValue::get(Caller->getType());
4572  }
4573  }
4574 
4575  if (!Caller->use_empty())
4576  replaceInstUsesWith(*Caller, NV);
4577  else if (Caller->hasValueHandle()) {
4578  if (OldRetTy == NV->getType())
4579  ValueHandleBase::ValueIsRAUWd(Caller, NV);
4580  else
4581  // We cannot call ValueIsRAUWd with a different type, and the
4582  // actual tracked value will disappear.
4584  }
4585 
4586  eraseInstFromFunction(*Caller);
4587  return true;
4588 }
4589 
4590 /// Turn a call to a function created by init_trampoline / adjust_trampoline
4591 /// intrinsic pair into a direct call to the underlying function.
4592 Instruction *
4593 InstCombiner::transformCallThroughTrampoline(CallSite CS,
4594  IntrinsicInst *Tramp) {
4595  Value *Callee = CS.getCalledValue();
4596  PointerType *PTy = cast<PointerType>(Callee->getType());
4597  FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
4599 
4600  // If the call already has the 'nest' attribute somewhere then give up -
4601  // otherwise 'nest' would occur twice after splicing in the chain.
4602  if (Attrs.hasAttrSomewhere(Attribute::Nest))
4603  return nullptr;
4604 
4605  assert(Tramp &&
4606  "transformCallThroughTrampoline called with incorrect CallSite.");
4607 
4608  Function *NestF =cast<Function>(Tramp->getArgOperand(1)->stripPointerCasts());
4609  FunctionType *NestFTy = cast<FunctionType>(NestF->getValueType());
4610 
4611  AttributeList NestAttrs = NestF->getAttributes();
4612  if (!NestAttrs.isEmpty()) {
4613  unsigned NestArgNo = 0;
4614  Type *NestTy = nullptr;
4615  AttributeSet NestAttr;
4616 
4617  // Look for a parameter marked with the 'nest' attribute.
4618  for (FunctionType::param_iterator I = NestFTy->param_begin(),
4619  E = NestFTy->param_end();
4620  I != E; ++NestArgNo, ++I) {
4621  AttributeSet AS = NestAttrs.getParamAttributes(NestArgNo);
4622  if (AS.hasAttribute(Attribute::Nest)) {
4623  // Record the parameter type and any other attributes.
4624  NestTy = *I;
4625  NestAttr = AS;
4626  break;
4627  }
4628  }
4629 
4630  if (NestTy) {
4631  Instruction *Caller = CS.getInstruction();
4632  std::vector<Value*> NewArgs;
4633  std::vector<AttributeSet> NewArgAttrs;
4634  NewArgs.reserve(CS.arg_size() + 1);
4635  NewArgAttrs.reserve(CS.arg_size());
4636 
4637  // Insert the nest argument into the call argument list, which may
4638  // mean appending it. Likewise for attributes.
4639 
4640  {
4641  unsigned ArgNo = 0;
4642  CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
4643  do {
4644  if (ArgNo == NestArgNo) {
4645  // Add the chain argument and attributes.
4646  Value *NestVal = Tramp->getArgOperand(2);
4647  if (NestVal->getType() != NestTy)
4648  NestVal = Builder.CreateBitCast(NestVal, NestTy, "nest");
4649  NewArgs.push_back(NestVal);
4650  NewArgAttrs.push_back(NestAttr);
4651  }
4652 
4653  if (I == E)
4654  break;
4655 
4656  // Add the original argument and attributes.
4657  NewArgs.push_back(*I);
4658  NewArgAttrs.push_back(Attrs.getParamAttributes(ArgNo));
4659 
4660  ++ArgNo;
4661  ++I;
4662  } while (true);
4663  }
4664 
4665  // The trampoline may have been bitcast to a bogus type (FTy).
4666  // Handle this by synthesizing a new function type, equal to FTy
4667  // with the chain parameter inserted.
4668 
4669  std::vector<Type*> NewTypes;
4670  NewTypes.reserve(FTy->getNumParams()+1);
4671 
4672  // Insert the chain's type into the list of parameter types, which may
4673  // mean appending it.
4674  {
4675  unsigned ArgNo = 0;
4676  FunctionType::param_iterator I = FTy->param_begin(),
4677  E = FTy->param_end();
4678 
4679  do {
4680  if (ArgNo == NestArgNo)
4681  // Add the chain's type.
4682  NewTypes.push_back(NestTy);
4683 
4684  if (I == E)
4685  break;
4686 
4687  // Add the original type.
4688  NewTypes.push_back(*I);
4689 
4690  ++ArgNo;
4691  ++I;
4692  } while (true);
4693  }
4694 
4695  // Replace the trampoline call with a direct call. Let the generic
4696  // code sort out any function type mismatches.
4697  FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes,
4698  FTy->isVarArg());
4699  Constant *NewCallee =
4700  NestF->getType() == PointerType::getUnqual(NewFTy) ?
4701  NestF : ConstantExpr::getBitCast(NestF,
4702  PointerType::getUnqual(NewFTy));
4703  AttributeList NewPAL =
4704  AttributeList::get(FTy->getContext(), Attrs.getFnAttributes(),
4705  Attrs.getRetAttributes(), NewArgAttrs);
4706 
4708  CS.getOperandBundlesAsDefs(OpBundles);
4709 
4710  Instruction *NewCaller;
4711  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
4712  NewCaller = InvokeInst::Create(NewCallee,
4713  II->getNormalDest(), II->getUnwindDest(),
4714  NewArgs, OpBundles);
4715  cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
4716  cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
4717  } else {
4718  NewCaller = CallInst::Create(NewCallee, NewArgs, OpBundles);
4719  cast<CallInst>(NewCaller)->setTailCallKind(
4720  cast<CallInst>(Caller)->getTailCallKind());
4721  cast<CallInst>(NewCaller)->setCallingConv(
4722  cast<CallInst>(Caller)->getCallingConv());
4723  cast<CallInst>(NewCaller)->setAttributes(NewPAL);
4724  }
4725  NewCaller->setDebugLoc(Caller->getDebugLoc());
4726 
4727  return NewCaller;
4728  }
4729  }
4730 
4731  // Replace the trampoline call with a direct call. Since there is no 'nest'
4732  // parameter, there is no need to adjust the argument list. Let the generic
4733  // code sort out any function type mismatches.
4734  Constant *NewCallee =
4735  NestF->getType() == PTy ? NestF :
4736  ConstantExpr::getBitCast(NestF, PTy);
4737  CS.setCalledFunction(NewCallee);
4738  return CS.getInstruction();
4739 }
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
bool isFPPredicate() const
Definition: InstrTypes.h:738
const NoneType None
Definition: None.h:24
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double, and whose elements are just simple data values (i.e.
Definition: Constants.h:762
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:749
uint64_t CallInst * C
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, OptimizationRemarkEmitter *ORE=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
User::op_iterator arg_iterator
The type of iterator to use when looping over actual arguments at this call site. ...
Definition: CallSite.h:213
LibCallSimplifier - This class implements a collection of optimizations that replace well formed call...
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
Definition: Constants.h:172
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
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Instruction *CxtI) const
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction, which must be an operator which supports these flags.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:71
static void ValueIsDeleted(Value *V)
Definition: Value.cpp:832
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1949
class_match< UndefValue > m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:87
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
bool isZero() const
Definition: APFloat.h:1143
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:173
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
Definition: PatternMatch.h:79
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1563
unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
Try to ensure that the alignment of V is at least PrefAlign bytes.
Definition: Local.cpp:1184
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
static Value * simplifyX86immShift(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder)
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:562
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:376
DiagnosticInfoOptimizationBase::Argument NV
unsigned arg_size() const
Definition: CallSite.h:219
CallingConv::ID getCallingConv() const
Get the calling convention of the call.
Definition: CallSite.h:312
Atomic ordering constants.
Value * CreateAddrSpaceCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1737
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition: IRBuilder.h:1669
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:289
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:135
Constant * getElementAsConstant(unsigned i) const
Return a Constant for a specified index&#39;s element.
Definition: Constants.cpp:2761
unsigned countMinPopulation() const
Returns the number of bits known to be one.
Definition: KnownBits.h:186
bool isInAllocaArgument(unsigned ArgNo) const
Determine whether this argument is passed in an alloca.
Definition: CallSite.h:603
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
Instruction * visitCallInst(CallInst &CI)
CallInst simplification.
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:265
#define LLVM_FALLTHROUGH
Definition: Compiler.h:86
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align, const char *Name)
Provided to resolve &#39;CreateAlignedLoad(Ptr, Align, "...")&#39; correctly, instead of converting the strin...
Definition: IRBuilder.h:1393
An instruction for ordering other memory operations.
Definition: Instructions.h:455
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:454
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Instruction * visitVACopyInst(VACopyInst &I)
static Instruction * simplifyInvariantGroupIntrinsic(IntrinsicInst &II, InstCombiner &IC)
This function transforms launder.invariant.group and strip.invariant.group like: launder(launder(x)) ...
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1332
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:1960
static bool simplifyX86MaskedStore(IntrinsicInst &II, InstCombiner &IC)
This class represents a function call, abstracting a target machine&#39;s calling convention.
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
This file contains the declarations for metadata subclasses.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:648
void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this load instruction.
Definition: Instructions.h:254
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:90
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
Definition: Type.cpp:630
iterator_range< IterTy > args() const
Definition: CallSite.h:215
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
bool hasValueHandle() const
Return true if there is a value handle associated with this value.
Definition: Value.h:486
unsigned less or equal
Definition: InstrTypes.h:672
bool mayWriteToMemory() const
Return true if this instruction may modify memory.
unsigned less than
Definition: InstrTypes.h:671
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 SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
static Instruction * foldCttzCtlz(IntrinsicInst &II, InstCombiner &IC)
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:705
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1186
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:811
bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr)
Return true if it is valid to use the assumptions provided by an assume intrinsic, I, at the point in the control-flow identified by the context instruction, CxtI.
STATISTIC(NumFunctions, "Total number of functions")
void setArgOperand(unsigned i, Value *v)
Definition: InstrTypes.h:1140
Metadata node.
Definition: Metadata.h:864
F(f)
const fltSemantics & getSemantics() const
Definition: APFloat.h:1155
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:503
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:660
An instruction for reading from memory.
Definition: Instructions.h:168
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:177
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:876
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
Definition: Constants.cpp:1956
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:138
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:166
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:230
static OverflowCheckFlavor IntrinsicIDToOverflowCheckFlavor(unsigned ID)
Returns the OverflowCheckFlavor corresponding to a overflow_with_op intrinsic.
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 maximum semantics.
Definition: APFloat.h:1262
void reserve(size_type N)
Definition: SmallVector.h:376
void addAttribute(unsigned i, Attribute::AttrKind Kind)
adds the attribute to the list of attributes.
Definition: InstrTypes.h:1261
Value * getLength() const
void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
static Instruction * simplifyMaskedStore(IntrinsicInst &II, InstCombiner &IC)
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:364
Instruction * visitVAStartInst(VAStartInst &I)
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:535
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1509
Value * CreateLaunderInvariantGroup(Value *Ptr)
Create a launder.invariant.group intrinsic call.
Definition: IRBuilder.h:2142
bool isGCRelocate(ImmutableCallSite CS)
Definition: Statepoint.cpp:43
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:130
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:265
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:136
static bool isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, const DataLayout &DL)
Check whether a bitcast, inttoptr, or ptrtoint cast between these types is valid and a no-op...
Value * getDest() const
This is just like getRawDest, but it strips off any cast instructions (including addrspacecast) that ...
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:269
bool isIdenticalTo(const Instruction *I) const
Return true if the specified instruction is exactly identical to the current one. ...
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1135
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:968
static Instruction * SimplifyNVVMIntrinsic(IntrinsicInst *II, InstCombiner &IC)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:48
Instruction * visitInvokeInst(InvokeInst &II)
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1613
bool isSigned() const
Definition: InstrTypes.h:816
APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
Definition: APInt.cpp:516
static APFloat fmed3AMDGCN(const APFloat &Src0, const APFloat &Src1, const APFloat &Src2)
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Definition: PatternMatch.h:761
Type * getPointerElementType() const
Definition: Type.h:376
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
Definition: InstrTypes.h:745
OverflowCheckFlavor
Specific patterns of overflow check idioms that we match.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static Value * simplifyX86movmsk(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder)
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:353
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:369
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:993
static Value * simplifyNeonTbl1(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder)
Convert a table lookup to shufflevector if the mask is constant.
IterTy arg_end() const
Definition: CallSite.h:575
OverflowResult computeOverflowForUnsignedAdd(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo=true)
Instruction * eraseInstFromFunction(Instruction &I)
Combiner aware instruction erasure.
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:197
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:743
The core instruction combiner logic.
static bool isSafeToEliminateVarargsCast(const CallSite CS, const DataLayout &DL, const CastInst *const CI, const int ix)
If this cast does not affect the value passed through the varargs area, we can eliminate the use of t...
void setCalledFunction(Value *Fn)
Sets the function called, including updating the function type.
Definition: InstrTypes.h:1210
This file contains the simple types necessary to represent the attributes associated with functions a...
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 minimum semantics.
Definition: APFloat.h:1249
InstrTy * getInstruction() const
Definition: CallSite.h:92
static Constant * getSExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1651
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:285
uint64_t getNumElements() const
Definition: DerivedTypes.h:359
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:978
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:784
This file implements a class to represent arbitrary precision integral constant values and operations...
All zero aggregate value.
Definition: Constants.h:341
static Value * simplifyX86vpermv(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder)
Attempt to convert vpermd/vpermps to shufflevector if the mask is constant.
Metadata * LowAndHigh[]
ValTy * getCalledValue() const
Return the pointer to function that is being called.
Definition: CallSite.h:100
static bool haveSameOperands(const IntrinsicInst &I, const IntrinsicInst &E, unsigned NumOperands)
DominatorTree & getDominatorTree() const
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:191
Key
PAL metadata keys.
bool doesNotThrow() const
Determine if the call cannot unwind.
Definition: InstrTypes.h:1555
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:85
Class to represent function types.
Definition: DerivedTypes.h:103
static Value * peekThroughBitcast(Value *V, bool OneUseOnly=false)
Return the source operand of a potentially bitcasted value while optionally checking if it has one us...
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1732
bool isInfinity() const
Definition: APFloat.h:1144
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
Value * CreateSExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a SExt or Trunc from the integer value V to DestTy.
Definition: IRBuilder.h:1684
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
Definition: PatternMatch.h:427
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:180
This represents the llvm.va_start intrinsic.
CastClass_match< OpTy, Instruction::FPExt > m_FPExt(const OpTy &Op)
Matches FPExt.
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:4444
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
AttributeSet getParamAttributes(unsigned ArgNo) const
The attributes for the argument or parameter at the given index are returned.
bool isVarArg() const
Definition: DerivedTypes.h:123
This class represents a no-op cast from one type to another.
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Return true if the call or the callee has the given attribute.
Definition: CallSite.h:377
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:221
Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.h:2196
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:138
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:126
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
Definition: PatternMatch.h:445
iterator_range< User::op_iterator > arg_operands()
Definition: InstrTypes.h:1127
AttrBuilder & remove(const AttrBuilder &B)
Remove the attributes from the builder.
static Value * simplifyX86pack(IntrinsicInst &II, bool IsSigned)
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:224
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:166
An instruction for storing to memory.
Definition: Instructions.h:321
bool extractProfTotalWeight(uint64_t &TotalVal) const
Retrieve total raw weight values of a branch.
Definition: Metadata.cpp:1340
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:203
CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
Definition: IRBuilder.cpp:734
static void ValueIsRAUWd(Value *Old, Value *New)
Definition: Value.cpp:885
static Value * simplifyX86vpcom(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder, bool IsSigned)
Decode XOP integer vector comparison intrinsics.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:291
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:410
amdgpu Simplify well known AMD library false Value * Callee
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1020
This class represents a truncation of integer types.
Type * getElementType() const
Return the element type of the array/vector.
Definition: Constants.cpp:2421
Value * getOperand(unsigned i) const
Definition: User.h:170
Class to represent pointers.
Definition: DerivedTypes.h:467
bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists in this set.
Definition: Attributes.cpp:578
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:335
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return &#39;this&#39;.
Definition: Type.h:304
const DataLayout & getDataLayout() const
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:106
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1773
bool isVoidTy() const
Return true if this is &#39;void&#39;.
Definition: Type.h:141
bool isFloatTy() const
Return true if this is &#39;float&#39;, a 32-bit IEEE fp type.
Definition: Type.h:147
bool hasAttrSomewhere(Attribute::AttrKind Kind, unsigned *Index=nullptr) const
Return true if the specified attribute is set for at least one parameter or for the return value...
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:62
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1166
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:364
void setAttributes(AttributeList PAL)
Set the parameter attributes of the call.
Definition: CallSite.h:333
Instruction * visitFenceInst(FenceInst &FI)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
static Instruction * simplifyMaskedScatter(IntrinsicInst &II, InstCombiner &IC)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:149
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static AttributeSet get(LLVMContext &C, const AttrBuilder &B)
Definition: Attributes.cpp:513
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
Definition: PatternMatch.h:176
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:308
bool isNegative() const
Definition: APFloat.h:1147
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1401
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1144
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1613
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:429
ConstantInt * lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL, const TargetLibraryInfo *TLI, bool MustSucceed)
Try to turn a call to @llvm.objectsize into an integer value of the given Type.
LLVM_NODISCARD AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const
Add an argument attribute to the list.
Definition: Attributes.h:403
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Definition: PatternMatch.h:755
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:287
bool isNaN() const
Definition: APFloat.h:1145
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:42
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.h:2021
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:224
static ManagedStatic< OptionRegistry > OR
Definition: Options.cpp:31
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:139
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:264
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:309
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:502
const Instruction * getNextNonDebugInstruction() const
Return a pointer to the next non-debug instruction in the same basic block as &#39;this&#39;, or nullptr if no such instruction exists.
This file declares a class to represent arbitrary precision floating point values and provide a varie...
bool isFast() const
Determine whether all fast-math-flags are set.
std::underlying_type< E >::type Underlying(E Val)
Check that Val is in range for E, and return Val cast to E&#39;s underlying type.
Definition: BitmaskEnum.h:91
static IntrinsicInst * findInitTrampolineFromBB(IntrinsicInst *AdjustTramp, Value *TrampMem)
bool isHalfTy() const
Return true if this is &#39;half&#39;, a 16-bit IEEE fp type.
Definition: Type.h:144
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:646
match_combine_or< CastClass_match< OpTy, Instruction::ZExt >, CastClass_match< OpTy, Instruction::SExt > > m_ZExtOrSExt(const OpTy &Op)
bool isAllOnes() const
Returns true if value is all one bits.
Definition: KnownBits.h:78
void setCallingConv(CallingConv::ID CC)
Set the calling convention of the call.
Definition: CallSite.h:316
bool isGCResult(ImmutableCallSite CS)
Definition: Statepoint.cpp:53
This class represents any memset intrinsic.
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:297
self_iterator getIterator()
Definition: ilist_node.h:82
Class to represent integer types.
Definition: DerivedTypes.h:40
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Definition: IRBuilder.h:360
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2041
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
Definition: APInt.h:450
void setNotConvergent()
Definition: CallSite.h:527
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:60
void setAlignment(unsigned Align)
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:319
static Value * simplifyX86varShift(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder)
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1415
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs, and aliases.
Definition: Value.cpp:529
size_t size() const
Definition: SmallVector.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1226
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
Definition: APFloat.h:1238
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:971
signed greater than
Definition: InstrTypes.h:673
static Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
Definition: Constants.cpp:302
static Value * simplifyX86extrq(IntrinsicInst &II, Value *Op0, ConstantInt *CILength, ConstantInt *CIIndex, InstCombiner::BuilderTy &Builder)
Attempt to simplify SSE4A EXTRQ/EXTRQI instructions using constant folding or conversion to a shuffle...
const APFloat & getValueAPF() const
Definition: Constants.h:303
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:51
bool doesNotThrow() const
Determine if the function cannot unwind.
Definition: Function.h:520
static BinaryOperator * CreateFNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
static Type * getHalfTy(LLVMContext &C)
Definition: Type.cpp:163
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:240
static CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
Iterator for intrusive lists based on ilist_node.
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:176
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
static PointerType * getInt1PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:216
static cl::opt< unsigned > GuardWideningWindow("instcombine-guard-widening-window", cl::init(3), cl::desc("How wide an instruction window to bypass looking for " "another guard"))
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
Definition: Constants.h:251
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the generic address space (address sp...
Definition: DerivedTypes.h:482
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
BlockVerifier::State From
static Value * simplifyX86vpermilvar(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder)
Attempt to convert vpermilvar* to shufflevector if the mask is constant.
iterator end()
Definition: BasicBlock.h:271
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
Definition: Type.cpp:130
IterTy arg_begin() const
Definition: CallSite.h:571
static IntrinsicInst * findInitTrampolineFromAlloca(Value *TrampMem)
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Value * CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2054
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:523
static Instruction * canonicalizeConstantArg0ToArg1(CallInst &Call)
Type::subtype_iterator param_iterator
Definition: DerivedTypes.h:126
bool overlaps(const AttrBuilder &B) const
Return true if the builder has any attribute that&#39;s in the specified builder.
static Value * simplifyNeonVld1(const IntrinsicInst &II, unsigned MemAlign, InstCombiner::BuilderTy &Builder)
Convert a vector load intrinsic into a simple llvm load instruction.
static Instruction * simplifyMaskedGather(IntrinsicInst &II, InstCombiner &IC)
void setDoesNotThrow()
Definition: CallSite.h:508
signed less than
Definition: InstrTypes.h:675
Type * getReturnType() const
Definition: DerivedTypes.h:124
CallInst * CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align, Value *Mask)
Create a call to Masked Store intrinsic.
Definition: IRBuilder.cpp:492
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition: Type.cpp:180
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition: APFloat.h:1205
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:2068
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:622
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition: Constants.cpp:636
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
Definition: Constants.cpp:685
#define NC
Definition: regutils.h:42
CallInst * CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask, Value *PassThru=nullptr, const Twine &Name="")
Create a call to Masked Load intrinsic.
Definition: IRBuilder.cpp:471
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1293
const Value * stripPointerCastsAndInvariantGroups() const
Strip off pointer casts, all-zero GEPs, aliases and invariant group info.
Definition: Value.cpp:541
Value * SimplifyCall(ImmutableCallSite CS, const SimplifyQuery &Q)
Given a callsite, fold the result or return null.
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition: Function.cpp:1437
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:578
bool isDenormal() const
Definition: APFloat.h:1148
void setOperand(unsigned i, Value *Val)
Definition: User.h:175
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:941
unsigned getVectorNumElements() const
Definition: DerivedTypes.h:462
signed less or equal
Definition: InstrTypes.h:676
Class to represent vector types.
Definition: DerivedTypes.h:393
void setVolatile(bool V)
Specify whether this is a volatile store or not.
Definition: Instructions.h:357
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:56
Class for arbitrary precision integers.
Definition: APInt.h:70
bool isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to be non-zero when defined.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
iterator_range< user_iterator > users()
Definition: Value.h:400
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1103
static Value * simplifyX86pshufb(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder)
Attempt to convert pshufb* to shufflevector if the mask is constant.
static cl::opt< bool > FtzEnabled("nvptx-f32ftz", cl::ZeroOrMore, cl::Hidden, cl::desc("NVPTX Specific: Flush f32 subnormals to sign-preserving zero."), cl::init(false))
amdgpu Simplify well known AMD library false Value Value * Arg
IntegerType * getInt1Ty()
Fetch the type representing a single bit.
Definition: IRBuilder.h:332
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate IT block based on arch"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT, "arm-no-restrict-it", "Allow IT blocks based on ARMv7")))
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:436
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2219
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
Definition: PatternMatch.h:543
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass&#39;s ...
void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this store instruction.
Definition: Instructions.h:379
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:546
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:185
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match &#39;fneg X&#39; as &#39;fsub -0.0, X&#39;.
Definition: PatternMatch.h:689
static Value * simplifyMaskedLoad(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder)
static Instruction * simplifyX86MaskedLoad(IntrinsicInst &II, InstCombiner &IC)
unsigned getNumArgOperands() const
Definition: InstrTypes.h:1133
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:311
static bool maskIsAllOneOrUndef(Value *Mask)
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:176
unsigned getIntegerBitWidth() const
Definition: DerivedTypes.h:97
OverflowResult
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
StringRef getValueAsString() const
Return the attribute&#39;s value as a string.
Definition: Attributes.cpp:195
unsigned greater or equal
Definition: InstrTypes.h:670
void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Definition: CallSite.h:582
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value *> Args, const Twine &NameStr, Instruction *InsertBefore=nullptr)
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1181
static Instruction::CastOps getCastOpcode(const Value *Val, bool SrcIsSigned, Type *Ty, bool DstIsSigned)
Returns the opcode necessary to cast Val into Ty using usual casting rules.
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
bool doesNotThrow() const
Determine if the call cannot unwind.
Definition: CallSite.h:505
bool isNormal() const
Definition: APFloat.h:1151
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc...
Value * CreateStripInvariantGroup(Value *Ptr)
Create a strip.invariant.group intrinsic call.
Definition: IRBuilder.h:2169
Value * optimizeCall(CallInst *CI)
optimizeCall - Take the given call instruction and return a more optimal value to replace the instruc...
static bool removeTriviallyEmptyRange(IntrinsicInst &I, unsigned StartID, unsigned EndID, InstCombiner &IC)
unsigned getKnownAlignment(Value *V, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
Try to infer an alignment for the specified pointer.
Definition: Local.h:268
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
Type * getValueType() const
Definition: GlobalValue.h:276
uint32_t Size
Definition: Profile.cpp:47
static IntrinsicInst * findInitTrampoline(Value *Callee)
bool isByValOrInAllocaArgument(unsigned ArgNo) const
Determine whether this argument is passed by value or in an alloca.
Definition: CallSite.h:608
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1974
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:81
AssumptionCache & getAssumptionCache() const
bool isSignedIntN(unsigned N) const
Check if this APInt has an N-bits signed integer value.
Definition: APInt.h:456
bool isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1164
static Value * simplifyX86insertq(IntrinsicInst &II, Value *Op0, Value *Op1, APInt APLength, APInt APIndex, InstCombiner::BuilderTy &Builder)
Attempt to simplify SSE4A INSERTQ/INSERTQI instructions using constant folding or conversion to a shu...
bool isStatepoint(ImmutableCallSite CS)
Definition: Statepoint.cpp:27
static Constant * getNegativeIsTrueBoolVec(ConstantDataVector *V)
Return a constant boolean vector that has true elements in all positions where the input constant dat...
This represents the llvm.va_copy intrinsic.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:545
static Value * simplifyX86round(IntrinsicInst &II, InstCombiner::BuilderTy &Builder)
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
static Instruction * foldCtpop(IntrinsicInst &II, InstCombiner &IC)
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1875
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:115
LLVM Value Representation.
Definition: Value.h:73
void setAlignment(unsigned Align)
This file provides internal interfaces used to implement the InstCombine.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:355
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:606
AttrBuilder typeIncompatible(Type *Ty)
Which attributes cannot be applied to a type.
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
AttributeSet getFnAttributes() const
The function attributes are returned.
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Definition: Instruction.cpp:87
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:331
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1124
Invoke instruction.
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:146
bool isNotMinSignedValue() const
Return true if the value is not the smallest signed value.
Definition: Constants.cpp:178
IRTranslator LLVM IR MI
bool hasOneUse() const
Return true if there is exactly one user of this value.
Definition: Value.h:413
unsigned greater than
Definition: InstrTypes.h:669
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:761
AttributeList getAttributes() const
Get the parameter attributes of the call.
Definition: CallSite.h:329
unsigned getNumElements() const
Return the number of elements in the array or vector.
Definition: Constants.cpp:2444
bool isConvergent() const
Determine if the call is convergent.
Definition: CallSite.h:521
static APInt getNullValue(unsigned numBits)
Get the &#39;0&#39; value.
Definition: APInt.h:569
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2260
static Constant * get(LLVMContext &Context, ArrayRef< uint8_t > Elts)
get() constructors - Return a constant with vector type with an element count and element type matchi...
Definition: Constants.cpp:2583
#define LLVM_DEBUG(X)
Definition: Debug.h:123
static Value * simplifyX86insertps(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder)
This class represents an extension of floating point types.
bool isMustTailCall() const
Tests if this call site must be tail call optimized.
Definition: CallSite.h:271
void setVolatile(bool V)
Specify whether this is a volatile load or not.
Definition: Instructions.h:235
bool isEmpty() const
Return true if there are no attributes.
Definition: Attributes.h:656
bool isDoubleTy() const
Return true if this is &#39;double&#39;, a 64-bit IEEE fp type.
Definition: Type.h:150
Root of the metadata hierarchy.
Definition: Metadata.h:58
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
void setDoesNotThrow()
Definition: InstrTypes.h:1556
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:174
void setCalledFunction(Value *V)
Set the callee to the specified value.
Definition: CallSite.h:126
bool isSignaling() const
Definition: APFloat.h:1149
Value * getRawDest() const
static Type * getPromotedType(Type *Ty)
Return the specified type promoted as it would be to pass though a va_arg area.
bool use_empty() const
Definition: Value.h:323
static Constant * get(ArrayRef< Constant *> V)
Definition: Constants.cpp:1079
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Type * getElementType() const
Definition: DerivedTypes.h:486
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE minNum semantics.
Definition: APFloat.h:1227
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:274
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:479
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute >> Attrs)
Create an AttributeList with the specified parameters in it.
Definition: Attributes.cpp:873
BinaryOp_match< ValTy, cst_pred_ty< is_all_ones >, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a &#39;Not&#39; as &#39;xor V, -1&#39; or &#39;xor -1, V&#39;.
bool isLosslessCast() const
A lossless cast is one that does not alter the basic value.
bool isNullValue() const
Determine if all bits are clear.
Definition: APInt.h:406
bool isStructTy() const
True if this is an instance of StructType.
Definition: Type.h:218
signed greater or equal
Definition: InstrTypes.h:674
User * user_back()
Definition: Value.h:386
cmpResult compare(const APFloat &RHS) const
Definition: APFloat.h:1102
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
const BasicBlock * getParent() const
Definition: Instruction.h:67
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)