LLVM  13.0.0git
InstCombineCalls.cpp
Go to the documentation of this file.
1 //===- InstCombineCalls.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the visitCall, visitInvoke, and visitCallBr functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "InstCombineInternal.h"
14 #include "llvm/ADT/APFloat.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/APSInt.h"
17 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/ADT/Twine.h"
29 #include "llvm/Analysis/Loads.h"
34 #include "llvm/IR/Attributes.h"
35 #include "llvm/IR/BasicBlock.h"
36 #include "llvm/IR/Constant.h"
37 #include "llvm/IR/Constants.h"
38 #include "llvm/IR/DataLayout.h"
39 #include "llvm/IR/DerivedTypes.h"
40 #include "llvm/IR/Function.h"
41 #include "llvm/IR/GlobalVariable.h"
42 #include "llvm/IR/InstrTypes.h"
43 #include "llvm/IR/Instruction.h"
44 #include "llvm/IR/Instructions.h"
45 #include "llvm/IR/IntrinsicInst.h"
46 #include "llvm/IR/Intrinsics.h"
47 #include "llvm/IR/IntrinsicsAArch64.h"
48 #include "llvm/IR/IntrinsicsAMDGPU.h"
49 #include "llvm/IR/IntrinsicsARM.h"
50 #include "llvm/IR/IntrinsicsHexagon.h"
51 #include "llvm/IR/LLVMContext.h"
52 #include "llvm/IR/Metadata.h"
53 #include "llvm/IR/PatternMatch.h"
54 #include "llvm/IR/Statepoint.h"
55 #include "llvm/IR/Type.h"
56 #include "llvm/IR/User.h"
57 #include "llvm/IR/Value.h"
58 #include "llvm/IR/ValueHandle.h"
60 #include "llvm/Support/Casting.h"
62 #include "llvm/Support/Compiler.h"
63 #include "llvm/Support/Debug.h"
65 #include "llvm/Support/KnownBits.h"
73 #include <algorithm>
74 #include <cassert>
75 #include <cstdint>
76 #include <cstring>
77 #include <utility>
78 #include <vector>
79 
80 using namespace llvm;
81 using namespace PatternMatch;
82 
83 #define DEBUG_TYPE "instcombine"
84 
85 STATISTIC(NumSimplified, "Number of library calls simplified");
86 
88  "instcombine-guard-widening-window",
89  cl::init(3),
90  cl::desc("How wide an instruction window to bypass looking for "
91  "another guard"));
92 
93 /// enable preservation of attributes in assume like:
94 /// call void @llvm.assume(i1 true) [ "nonnull"(i32* %PTR) ]
96 
97 /// Return the specified type promoted as it would be to pass though a va_arg
98 /// area.
99 static Type *getPromotedType(Type *Ty) {
100  if (IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
101  if (ITy->getBitWidth() < 32)
102  return Type::getInt32Ty(Ty->getContext());
103  }
104  return Ty;
105 }
106 
108  Align DstAlign = getKnownAlignment(MI->getRawDest(), DL, MI, &AC, &DT);
109  MaybeAlign CopyDstAlign = MI->getDestAlign();
110  if (!CopyDstAlign || *CopyDstAlign < DstAlign) {
111  MI->setDestAlignment(DstAlign);
112  return MI;
113  }
114 
115  Align SrcAlign = getKnownAlignment(MI->getRawSource(), DL, MI, &AC, &DT);
116  MaybeAlign CopySrcAlign = MI->getSourceAlign();
117  if (!CopySrcAlign || *CopySrcAlign < SrcAlign) {
118  MI->setSourceAlignment(SrcAlign);
119  return MI;
120  }
121 
122  // If we have a store to a location which is known constant, we can conclude
123  // that the store must be storing the constant value (else the memory
124  // wouldn't be constant), and this must be a noop.
125  if (AA->pointsToConstantMemory(MI->getDest())) {
126  // Set the size of the copy to 0, it will be deleted on the next iteration.
127  MI->setLength(Constant::getNullValue(MI->getLength()->getType()));
128  return MI;
129  }
130 
131  // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
132  // load/store.
133  ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getLength());
134  if (!MemOpLength) return nullptr;
135 
136  // Source and destination pointer types are always "i8*" for intrinsic. See
137  // if the size is something we can handle with a single primitive load/store.
138  // A single load+store correctly handles overlapping memory in the memmove
139  // case.
140  uint64_t Size = MemOpLength->getLimitedValue();
141  assert(Size && "0-sized memory transferring should be removed already.");
142 
143  if (Size > 8 || (Size&(Size-1)))
144  return nullptr; // If not 1/2/4/8 bytes, exit.
145 
146  // If it is an atomic and alignment is less than the size then we will
147  // introduce the unaligned memory access which will be later transformed
148  // into libcall in CodeGen. This is not evident performance gain so disable
149  // it now.
150  if (isa<AtomicMemTransferInst>(MI))
151  if (*CopyDstAlign < Size || *CopySrcAlign < Size)
152  return nullptr;
153 
154  // Use an integer load+store unless we can find something better.
155  unsigned SrcAddrSp =
156  cast<PointerType>(MI->getArgOperand(1)->getType())->getAddressSpace();
157  unsigned DstAddrSp =
158  cast<PointerType>(MI->getArgOperand(0)->getType())->getAddressSpace();
159 
160  IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
161  Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp);
162  Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp);
163 
164  // If the memcpy has metadata describing the members, see if we can get the
165  // TBAA tag describing our copy.
166  MDNode *CopyMD = nullptr;
167  if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa)) {
168  CopyMD = M;
169  } else if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa_struct)) {
170  if (M->getNumOperands() == 3 && M->getOperand(0) &&
171  mdconst::hasa<ConstantInt>(M->getOperand(0)) &&
172  mdconst::extract<ConstantInt>(M->getOperand(0))->isZero() &&
173  M->getOperand(1) &&
174  mdconst::hasa<ConstantInt>(M->getOperand(1)) &&
175  mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
176  Size &&
177  M->getOperand(2) && isa<MDNode>(M->getOperand(2)))
178  CopyMD = cast<MDNode>(M->getOperand(2));
179  }
180 
181  Value *Src = Builder.CreateBitCast(MI->getArgOperand(1), NewSrcPtrTy);
182  Value *Dest = Builder.CreateBitCast(MI->getArgOperand(0), NewDstPtrTy);
183  LoadInst *L = Builder.CreateLoad(IntType, Src);
184  // Alignment from the mem intrinsic will be better, so use it.
185  L->setAlignment(*CopySrcAlign);
186  if (CopyMD)
187  L->setMetadata(LLVMContext::MD_tbaa, CopyMD);
188  MDNode *LoopMemParallelMD =
189  MI->getMetadata(LLVMContext::MD_mem_parallel_loop_access);
190  if (LoopMemParallelMD)
191  L->setMetadata(LLVMContext::MD_mem_parallel_loop_access, LoopMemParallelMD);
192  MDNode *AccessGroupMD = MI->getMetadata(LLVMContext::MD_access_group);
193  if (AccessGroupMD)
194  L->setMetadata(LLVMContext::MD_access_group, AccessGroupMD);
195 
196  StoreInst *S = Builder.CreateStore(L, Dest);
197  // Alignment from the mem intrinsic will be better, so use it.
198  S->setAlignment(*CopyDstAlign);
199  if (CopyMD)
200  S->setMetadata(LLVMContext::MD_tbaa, CopyMD);
201  if (LoopMemParallelMD)
202  S->setMetadata(LLVMContext::MD_mem_parallel_loop_access, LoopMemParallelMD);
203  if (AccessGroupMD)
204  S->setMetadata(LLVMContext::MD_access_group, AccessGroupMD);
205 
206  if (auto *MT = dyn_cast<MemTransferInst>(MI)) {
207  // non-atomics can be volatile
208  L->setVolatile(MT->isVolatile());
209  S->setVolatile(MT->isVolatile());
210  }
211  if (isa<AtomicMemTransferInst>(MI)) {
212  // atomics have to be unordered
214  S->setOrdering(AtomicOrdering::Unordered);
215  }
216 
217  // Set the size of the copy to 0, it will be deleted on the next iteration.
218  MI->setLength(Constant::getNullValue(MemOpLength->getType()));
219  return MI;
220 }
221 
223  const Align KnownAlignment =
224  getKnownAlignment(MI->getDest(), DL, MI, &AC, &DT);
225  MaybeAlign MemSetAlign = MI->getDestAlign();
226  if (!MemSetAlign || *MemSetAlign < KnownAlignment) {
227  MI->setDestAlignment(KnownAlignment);
228  return MI;
229  }
230 
231  // If we have a store to a location which is known constant, we can conclude
232  // that the store must be storing the constant value (else the memory
233  // wouldn't be constant), and this must be a noop.
234  if (AA->pointsToConstantMemory(MI->getDest())) {
235  // Set the size of the copy to 0, it will be deleted on the next iteration.
236  MI->setLength(Constant::getNullValue(MI->getLength()->getType()));
237  return MI;
238  }
239 
240  // Extract the length and alignment and fill if they are constant.
241  ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
242  ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
243  if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8))
244  return nullptr;
245  const uint64_t Len = LenC->getLimitedValue();
246  assert(Len && "0-sized memory setting should be removed already.");
247  const Align Alignment = assumeAligned(MI->getDestAlignment());
248 
249  // If it is an atomic and alignment is less than the size then we will
250  // introduce the unaligned memory access which will be later transformed
251  // into libcall in CodeGen. This is not evident performance gain so disable
252  // it now.
253  if (isa<AtomicMemSetInst>(MI))
254  if (Alignment < Len)
255  return nullptr;
256 
257  // memset(s,c,n) -> store s, c (for n=1,2,4,8)
258  if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
259  Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8.
260 
261  Value *Dest = MI->getDest();
262  unsigned DstAddrSp = cast<PointerType>(Dest->getType())->getAddressSpace();
263  Type *NewDstPtrTy = PointerType::get(ITy, DstAddrSp);
264  Dest = Builder.CreateBitCast(Dest, NewDstPtrTy);
265 
266  // Extract the fill value and store.
267  uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
268  StoreInst *S = Builder.CreateStore(ConstantInt::get(ITy, Fill), Dest,
269  MI->isVolatile());
270  S->setAlignment(Alignment);
271  if (isa<AtomicMemSetInst>(MI))
272  S->setOrdering(AtomicOrdering::Unordered);
273 
274  // Set the size of the copy to 0, it will be deleted on the next iteration.
275  MI->setLength(Constant::getNullValue(LenC->getType()));
276  return MI;
277  }
278 
279  return nullptr;
280 }
281 
282 // TODO, Obvious Missing Transforms:
283 // * Narrow width by halfs excluding zero/undef lanes
284 Value *InstCombinerImpl::simplifyMaskedLoad(IntrinsicInst &II) {
285  Value *LoadPtr = II.getArgOperand(0);
286  const Align Alignment =
287  cast<ConstantInt>(II.getArgOperand(1))->getAlignValue();
288 
289  // If the mask is all ones or undefs, this is a plain vector load of the 1st
290  // argument.
292  return Builder.CreateAlignedLoad(II.getType(), LoadPtr, Alignment,
293  "unmaskedload");
294 
295  // If we can unconditionally load from this address, replace with a
296  // load/select idiom. TODO: use DT for context sensitive query
297  if (isDereferenceablePointer(LoadPtr, II.getType(),
298  II.getModule()->getDataLayout(), &II, nullptr)) {
299  Value *LI = Builder.CreateAlignedLoad(II.getType(), LoadPtr, Alignment,
300  "unmaskedload");
301  return Builder.CreateSelect(II.getArgOperand(2), LI, II.getArgOperand(3));
302  }
303 
304  return nullptr;
305 }
306 
307 // TODO, Obvious Missing Transforms:
308 // * Single constant active lane -> store
309 // * Narrow width by halfs excluding zero/undef lanes
310 Instruction *InstCombinerImpl::simplifyMaskedStore(IntrinsicInst &II) {
311  auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3));
312  if (!ConstMask)
313  return nullptr;
314 
315  // If the mask is all zeros, this instruction does nothing.
316  if (ConstMask->isNullValue())
317  return eraseInstFromFunction(II);
318 
319  // If the mask is all ones, this is a plain vector store of the 1st argument.
320  if (ConstMask->isAllOnesValue()) {
321  Value *StorePtr = II.getArgOperand(1);
322  Align Alignment = cast<ConstantInt>(II.getArgOperand(2))->getAlignValue();
323  return new StoreInst(II.getArgOperand(0), StorePtr, false, Alignment);
324  }
325 
326  if (isa<ScalableVectorType>(ConstMask->getType()))
327  return nullptr;
328 
329  // Use masked off lanes to simplify operands via SimplifyDemandedVectorElts
330  APInt DemandedElts = possiblyDemandedEltsInMask(ConstMask);
331  APInt UndefElts(DemandedElts.getBitWidth(), 0);
332  if (Value *V =
333  SimplifyDemandedVectorElts(II.getOperand(0), DemandedElts, UndefElts))
334  return replaceOperand(II, 0, V);
335 
336  return nullptr;
337 }
338 
339 // TODO, Obvious Missing Transforms:
340 // * Single constant active lane load -> load
341 // * Dereferenceable address & few lanes -> scalarize speculative load/selects
342 // * Adjacent vector addresses -> masked.load
343 // * Narrow width by halfs excluding zero/undef lanes
344 // * Vector splat address w/known mask -> scalar load
345 // * Vector incrementing address -> vector masked load
346 Instruction *InstCombinerImpl::simplifyMaskedGather(IntrinsicInst &II) {
347  return nullptr;
348 }
349 
350 // TODO, Obvious Missing Transforms:
351 // * Single constant active lane -> store
352 // * Adjacent vector addresses -> masked.store
353 // * Narrow store width by halfs excluding zero/undef lanes
354 // * Vector splat address w/known mask -> scalar store
355 // * Vector incrementing address -> vector masked store
356 Instruction *InstCombinerImpl::simplifyMaskedScatter(IntrinsicInst &II) {
357  auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3));
358  if (!ConstMask)
359  return nullptr;
360 
361  // If the mask is all zeros, a scatter does nothing.
362  if (ConstMask->isNullValue())
363  return eraseInstFromFunction(II);
364 
365  if (isa<ScalableVectorType>(ConstMask->getType()))
366  return nullptr;
367 
368  // Use masked off lanes to simplify operands via SimplifyDemandedVectorElts
369  APInt DemandedElts = possiblyDemandedEltsInMask(ConstMask);
370  APInt UndefElts(DemandedElts.getBitWidth(), 0);
371  if (Value *V =
372  SimplifyDemandedVectorElts(II.getOperand(0), DemandedElts, UndefElts))
373  return replaceOperand(II, 0, V);
374  if (Value *V =
375  SimplifyDemandedVectorElts(II.getOperand(1), DemandedElts, UndefElts))
376  return replaceOperand(II, 1, V);
377 
378  return nullptr;
379 }
380 
381 /// This function transforms launder.invariant.group and strip.invariant.group
382 /// like:
383 /// launder(launder(%x)) -> launder(%x) (the result is not the argument)
384 /// launder(strip(%x)) -> launder(%x)
385 /// strip(strip(%x)) -> strip(%x) (the result is not the argument)
386 /// strip(launder(%x)) -> strip(%x)
387 /// This is legal because it preserves the most recent information about
388 /// the presence or absence of invariant.group.
390  InstCombinerImpl &IC) {
391  auto *Arg = II.getArgOperand(0);
392  auto *StrippedArg = Arg->stripPointerCasts();
393  auto *StrippedInvariantGroupsArg = StrippedArg;
394  while (auto *Intr = dyn_cast<IntrinsicInst>(StrippedInvariantGroupsArg)) {
395  if (Intr->getIntrinsicID() != Intrinsic::launder_invariant_group &&
396  Intr->getIntrinsicID() != Intrinsic::strip_invariant_group)
397  break;
398  StrippedInvariantGroupsArg = Intr->getArgOperand(0)->stripPointerCasts();
399  }
400  if (StrippedArg == StrippedInvariantGroupsArg)
401  return nullptr; // No launders/strips to remove.
402 
403  Value *Result = nullptr;
404 
405  if (II.getIntrinsicID() == Intrinsic::launder_invariant_group)
406  Result = IC.Builder.CreateLaunderInvariantGroup(StrippedInvariantGroupsArg);
407  else if (II.getIntrinsicID() == Intrinsic::strip_invariant_group)
408  Result = IC.Builder.CreateStripInvariantGroup(StrippedInvariantGroupsArg);
409  else
411  "simplifyInvariantGroupIntrinsic only handles launder and strip");
412  if (Result->getType()->getPointerAddressSpace() !=
414  Result = IC.Builder.CreateAddrSpaceCast(Result, II.getType());
415  if (Result->getType() != II.getType())
416  Result = IC.Builder.CreateBitCast(Result, II.getType());
417 
418  return cast<Instruction>(Result);
419 }
420 
422  assert((II.getIntrinsicID() == Intrinsic::cttz ||
423  II.getIntrinsicID() == Intrinsic::ctlz) &&
424  "Expected cttz or ctlz intrinsic");
425  bool IsTZ = II.getIntrinsicID() == Intrinsic::cttz;
426  Value *Op0 = II.getArgOperand(0);
427  Value *X;
428  // ctlz(bitreverse(x)) -> cttz(x)
429  // cttz(bitreverse(x)) -> ctlz(x)
430  if (match(Op0, m_BitReverse(m_Value(X)))) {
431  Intrinsic::ID ID = IsTZ ? Intrinsic::ctlz : Intrinsic::cttz;
433  return CallInst::Create(F, {X, II.getArgOperand(1)});
434  }
435 
436  if (IsTZ) {
437  // cttz(-x) -> cttz(x)
438  if (match(Op0, m_Neg(m_Value(X))))
439  return IC.replaceOperand(II, 0, X);
440 
441  // cttz(abs(x)) -> cttz(x)
442  // cttz(nabs(x)) -> cttz(x)
443  Value *Y;
445  if (SPF == SPF_ABS || SPF == SPF_NABS)
446  return IC.replaceOperand(II, 0, X);
447 
448  if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X))))
449  return IC.replaceOperand(II, 0, X);
450  }
451 
452  KnownBits Known = IC.computeKnownBits(Op0, 0, &II);
453 
454  // Create a mask for bits above (ctlz) or below (cttz) the first known one.
455  unsigned PossibleZeros = IsTZ ? Known.countMaxTrailingZeros()
456  : Known.countMaxLeadingZeros();
457  unsigned DefiniteZeros = IsTZ ? Known.countMinTrailingZeros()
458  : Known.countMinLeadingZeros();
459 
460  // If all bits above (ctlz) or below (cttz) the first known one are known
461  // zero, this value is constant.
462  // FIXME: This should be in InstSimplify because we're replacing an
463  // instruction with a constant.
464  if (PossibleZeros == DefiniteZeros) {
465  auto *C = ConstantInt::get(Op0->getType(), DefiniteZeros);
466  return IC.replaceInstUsesWith(II, C);
467  }
468 
469  // If the input to cttz/ctlz is known to be non-zero,
470  // then change the 'ZeroIsUndef' parameter to 'true'
471  // because we know the zero behavior can't affect the result.
472  if (!Known.One.isNullValue() ||
473  isKnownNonZero(Op0, IC.getDataLayout(), 0, &IC.getAssumptionCache(), &II,
474  &IC.getDominatorTree())) {
475  if (!match(II.getArgOperand(1), m_One()))
476  return IC.replaceOperand(II, 1, IC.Builder.getTrue());
477  }
478 
479  // Add range metadata since known bits can't completely reflect what we know.
480  // TODO: Handle splat vectors.
481  auto *IT = dyn_cast<IntegerType>(Op0->getType());
482  if (IT && IT->getBitWidth() != 1 && !II.getMetadata(LLVMContext::MD_range)) {
483  Metadata *LowAndHigh[] = {
485  ConstantAsMetadata::get(ConstantInt::get(IT, PossibleZeros + 1))};
486  II.setMetadata(LLVMContext::MD_range,
488  return &II;
489  }
490 
491  return nullptr;
492 }
493 
495  assert(II.getIntrinsicID() == Intrinsic::ctpop &&
496  "Expected ctpop intrinsic");
497  Type *Ty = II.getType();
498  unsigned BitWidth = Ty->getScalarSizeInBits();
499  Value *Op0 = II.getArgOperand(0);
500  Value *X;
501 
502  // ctpop(bitreverse(x)) -> ctpop(x)
503  // ctpop(bswap(x)) -> ctpop(x)
504  if (match(Op0, m_BitReverse(m_Value(X))) || match(Op0, m_BSwap(m_Value(X))))
505  return IC.replaceOperand(II, 0, X);
506 
507  // ctpop(x | -x) -> bitwidth - cttz(x, false)
508  if (Op0->hasOneUse() &&
509  match(Op0, m_c_Or(m_Value(X), m_Neg(m_Deferred(X))))) {
510  Function *F =
511  Intrinsic::getDeclaration(II.getModule(), Intrinsic::cttz, Ty);
512  auto *Cttz = IC.Builder.CreateCall(F, {X, IC.Builder.getFalse()});
513  auto *Bw = ConstantInt::get(Ty, APInt(BitWidth, BitWidth));
514  return IC.replaceInstUsesWith(II, IC.Builder.CreateSub(Bw, Cttz));
515  }
516 
517  // ctpop(~x & (x - 1)) -> cttz(x, false)
518  if (match(Op0,
520  Function *F =
521  Intrinsic::getDeclaration(II.getModule(), Intrinsic::cttz, Ty);
522  return CallInst::Create(F, {X, IC.Builder.getFalse()});
523  }
524 
525  KnownBits Known(BitWidth);
526  IC.computeKnownBits(Op0, Known, 0, &II);
527 
528  // If all bits are zero except for exactly one fixed bit, then the result
529  // must be 0 or 1, and we can get that answer by shifting to LSB:
530  // ctpop (X & 32) --> (X & 32) >> 5
531  if ((~Known.Zero).isPowerOf2())
532  return BinaryOperator::CreateLShr(
533  Op0, ConstantInt::get(Ty, (~Known.Zero).exactLogBase2()));
534 
535  // FIXME: Try to simplify vectors of integers.
536  auto *IT = dyn_cast<IntegerType>(Ty);
537  if (!IT)
538  return nullptr;
539 
540  // Add range metadata since known bits can't completely reflect what we know.
541  unsigned MinCount = Known.countMinPopulation();
542  unsigned MaxCount = Known.countMaxPopulation();
543  if (IT->getBitWidth() != 1 && !II.getMetadata(LLVMContext::MD_range)) {
544  Metadata *LowAndHigh[] = {
547  II.setMetadata(LLVMContext::MD_range,
549  return &II;
550  }
551 
552  return nullptr;
553 }
554 
555 /// Convert a table lookup to shufflevector if the mask is constant.
556 /// This could benefit tbl1 if the mask is { 7,6,5,4,3,2,1,0 }, in
557 /// which case we could lower the shufflevector with rev64 instructions
558 /// as it's actually a byte reverse.
561  // Bail out if the mask is not a constant.
562  auto *C = dyn_cast<Constant>(II.getArgOperand(1));
563  if (!C)
564  return nullptr;
565 
566  auto *VecTy = cast<FixedVectorType>(II.getType());
567  unsigned NumElts = VecTy->getNumElements();
568 
569  // Only perform this transformation for <8 x i8> vector types.
570  if (!VecTy->getElementType()->isIntegerTy(8) || NumElts != 8)
571  return nullptr;
572 
573  int Indexes[8];
574 
575  for (unsigned I = 0; I < NumElts; ++I) {
576  Constant *COp = C->getAggregateElement(I);
577 
578  if (!COp || !isa<ConstantInt>(COp))
579  return nullptr;
580 
581  Indexes[I] = cast<ConstantInt>(COp)->getLimitedValue();
582 
583  // Make sure the mask indices are in range.
584  if ((unsigned)Indexes[I] >= NumElts)
585  return nullptr;
586  }
587 
588  auto *V1 = II.getArgOperand(0);
589  auto *V2 = Constant::getNullValue(V1->getType());
590  return Builder.CreateShuffleVector(V1, V2, makeArrayRef(Indexes));
591 }
592 
593 // Returns true iff the 2 intrinsics have the same operands, limiting the
594 // comparison to the first NumOperands.
595 static bool haveSameOperands(const IntrinsicInst &I, const IntrinsicInst &E,
596  unsigned NumOperands) {
597  assert(I.getNumArgOperands() >= NumOperands && "Not enough operands");
598  assert(E.getNumArgOperands() >= NumOperands && "Not enough operands");
599  for (unsigned i = 0; i < NumOperands; i++)
600  if (I.getArgOperand(i) != E.getArgOperand(i))
601  return false;
602  return true;
603 }
604 
605 // Remove trivially empty start/end intrinsic ranges, i.e. a start
606 // immediately followed by an end (ignoring debuginfo or other
607 // start/end intrinsics in between). As this handles only the most trivial
608 // cases, tracking the nesting level is not needed:
609 //
610 // call @llvm.foo.start(i1 0)
611 // call @llvm.foo.start(i1 0) ; This one won't be skipped: it will be removed
612 // call @llvm.foo.end(i1 0)
613 // call @llvm.foo.end(i1 0) ; &I
614 static bool
616  std::function<bool(const IntrinsicInst &)> IsStart) {
617  // We start from the end intrinsic and scan backwards, so that InstCombine
618  // has already processed (and potentially removed) all the instructions
619  // before the end intrinsic.
620  BasicBlock::reverse_iterator BI(EndI), BE(EndI.getParent()->rend());
621  for (; BI != BE; ++BI) {
622  if (auto *I = dyn_cast<IntrinsicInst>(&*BI)) {
623  if (isa<DbgInfoIntrinsic>(I) ||
624  I->getIntrinsicID() == EndI.getIntrinsicID())
625  continue;
626  if (IsStart(*I)) {
627  if (haveSameOperands(EndI, *I, EndI.getNumArgOperands())) {
629  IC.eraseInstFromFunction(EndI);
630  return true;
631  }
632  // Skip start intrinsics that don't pair with this end intrinsic.
633  continue;
634  }
635  }
636  break;
637  }
638 
639  return false;
640 }
641 
643  removeTriviallyEmptyRange(I, *this, [](const IntrinsicInst &I) {
644  return I.getIntrinsicID() == Intrinsic::vastart ||
645  I.getIntrinsicID() == Intrinsic::vacopy;
646  });
647  return nullptr;
648 }
649 
651  assert(Call.getNumArgOperands() > 1 && "Need at least 2 args to swap");
652  Value *Arg0 = Call.getArgOperand(0), *Arg1 = Call.getArgOperand(1);
653  if (isa<Constant>(Arg0) && !isa<Constant>(Arg1)) {
654  Call.setArgOperand(0, Arg1);
655  Call.setArgOperand(1, Arg0);
656  return &Call;
657  }
658  return nullptr;
659 }
660 
661 /// Creates a result tuple for an overflow intrinsic \p II with a given
662 /// \p Result and a constant \p Overflow value.
664  Constant *Overflow) {
665  Constant *V[] = {UndefValue::get(Result->getType()), Overflow};
666  StructType *ST = cast<StructType>(II->getType());
667  Constant *Struct = ConstantStruct::get(ST, V);
668  return InsertValueInst::Create(Struct, Result, 0);
669 }
670 
671 Instruction *
672 InstCombinerImpl::foldIntrinsicWithOverflowCommon(IntrinsicInst *II) {
673  WithOverflowInst *WO = cast<WithOverflowInst>(II);
674  Value *OperationResult = nullptr;
675  Constant *OverflowResult = nullptr;
676  if (OptimizeOverflowCheck(WO->getBinaryOp(), WO->isSigned(), WO->getLHS(),
677  WO->getRHS(), *WO, OperationResult, OverflowResult))
678  return createOverflowTuple(WO, OperationResult, OverflowResult);
679  return nullptr;
680 }
681 
683  const DataLayout &DL, AssumptionCache *AC,
684  DominatorTree *DT) {
685  KnownBits Known = computeKnownBits(Op, DL, 0, AC, CxtI, DT);
686  if (Known.isNonNegative())
687  return false;
688  if (Known.isNegative())
689  return true;
690 
692  ICmpInst::ICMP_SLT, Op, Constant::getNullValue(Op->getType()), CxtI, DL);
693 }
694 
695 /// CallInst simplification. This mostly only handles folding of intrinsic
696 /// instructions. For normal calls, it allows visitCallBase to do the heavy
697 /// lifting.
699  // Don't try to simplify calls without uses. It will not do anything useful,
700  // but will result in the following folds being skipped.
701  if (!CI.use_empty())
702  if (Value *V = SimplifyCall(&CI, SQ.getWithInstruction(&CI)))
703  return replaceInstUsesWith(CI, V);
704 
705  if (isFreeCall(&CI, &TLI))
706  return visitFree(CI);
707 
708  // If the caller function is nounwind, mark the call as nounwind, even if the
709  // callee isn't.
710  if (CI.getFunction()->doesNotThrow() && !CI.doesNotThrow()) {
711  CI.setDoesNotThrow();
712  return &CI;
713  }
714 
715  IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
716  if (!II) return visitCallBase(CI);
717 
718  // For atomic unordered mem intrinsics if len is not a positive or
719  // not a multiple of element size then behavior is undefined.
720  if (auto *AMI = dyn_cast<AtomicMemIntrinsic>(II))
721  if (ConstantInt *NumBytes = dyn_cast<ConstantInt>(AMI->getLength()))
722  if (NumBytes->getSExtValue() < 0 ||
723  (NumBytes->getZExtValue() % AMI->getElementSizeInBytes() != 0)) {
724  CreateNonTerminatorUnreachable(AMI);
725  assert(AMI->getType()->isVoidTy() &&
726  "non void atomic unordered mem intrinsic");
727  return eraseInstFromFunction(*AMI);
728  }
729 
730  // Intrinsics cannot occur in an invoke or a callbr, so handle them here
731  // instead of in visitCallBase.
732  if (auto *MI = dyn_cast<AnyMemIntrinsic>(II)) {
733  bool Changed = false;
734 
735  // memmove/cpy/set of zero bytes is a noop.
736  if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
737  if (NumBytes->isNullValue())
738  return eraseInstFromFunction(CI);
739 
740  if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
741  if (CI->getZExtValue() == 1) {
742  // Replace the instruction with just byte operations. We would
743  // transform other cases to loads/stores, but we don't know if
744  // alignment is sufficient.
745  }
746  }
747 
748  // No other transformations apply to volatile transfers.
749  if (auto *M = dyn_cast<MemIntrinsic>(MI))
750  if (M->isVolatile())
751  return nullptr;
752 
753  // If we have a memmove and the source operation is a constant global,
754  // then the source and dest pointers can't alias, so we can change this
755  // into a call to memcpy.
756  if (auto *MMI = dyn_cast<AnyMemMoveInst>(MI)) {
757  if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
758  if (GVSrc->isConstant()) {
759  Module *M = CI.getModule();
760  Intrinsic::ID MemCpyID =
761  isa<AtomicMemMoveInst>(MMI)
762  ? Intrinsic::memcpy_element_unordered_atomic
764  Type *Tys[3] = { CI.getArgOperand(0)->getType(),
765  CI.getArgOperand(1)->getType(),
766  CI.getArgOperand(2)->getType() };
767  CI.setCalledFunction(Intrinsic::getDeclaration(M, MemCpyID, Tys));
768  Changed = true;
769  }
770  }
771 
772  if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(MI)) {
773  // memmove(x,x,size) -> noop.
774  if (MTI->getSource() == MTI->getDest())
775  return eraseInstFromFunction(CI);
776  }
777 
778  // If we can determine a pointer alignment that is bigger than currently
779  // set, update the alignment.
780  if (auto *MTI = dyn_cast<AnyMemTransferInst>(MI)) {
781  if (Instruction *I = SimplifyAnyMemTransfer(MTI))
782  return I;
783  } else if (auto *MSI = dyn_cast<AnyMemSetInst>(MI)) {
784  if (Instruction *I = SimplifyAnyMemSet(MSI))
785  return I;
786  }
787 
788  if (Changed) return II;
789  }
790 
791  // For fixed width vector result intrinsics, use the generic demanded vector
792  // support.
793  if (auto *IIFVTy = dyn_cast<FixedVectorType>(II->getType())) {
794  auto VWidth = IIFVTy->getNumElements();
795  APInt UndefElts(VWidth, 0);
796  APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
797  if (Value *V = SimplifyDemandedVectorElts(II, AllOnesEltMask, UndefElts)) {
798  if (V != II)
799  return replaceInstUsesWith(*II, V);
800  return II;
801  }
802  }
803 
804  if (II->isCommutative()) {
805  if (CallInst *NewCall = canonicalizeConstantArg0ToArg1(CI))
806  return NewCall;
807  }
808 
809  Intrinsic::ID IID = II->getIntrinsicID();
810  switch (IID) {
811  case Intrinsic::objectsize:
812  if (Value *V = lowerObjectSizeCall(II, DL, &TLI, /*MustSucceed=*/false))
813  return replaceInstUsesWith(CI, V);
814  return nullptr;
815  case Intrinsic::abs: {
816  Value *IIOperand = II->getArgOperand(0);
817  bool IntMinIsPoison = cast<Constant>(II->getArgOperand(1))->isOneValue();
818 
819  // abs(-x) -> abs(x)
820  // TODO: Copy nsw if it was present on the neg?
821  Value *X;
822  if (match(IIOperand, m_Neg(m_Value(X))))
823  return replaceOperand(*II, 0, X);
824  if (match(IIOperand, m_Select(m_Value(), m_Value(X), m_Neg(m_Deferred(X)))))
825  return replaceOperand(*II, 0, X);
826  if (match(IIOperand, m_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
827  return replaceOperand(*II, 0, X);
828 
829  if (Optional<bool> Sign = getKnownSign(IIOperand, II, DL, &AC, &DT)) {
830  // abs(x) -> x if x >= 0
831  if (!*Sign)
832  return replaceInstUsesWith(*II, IIOperand);
833 
834  // abs(x) -> -x if x < 0
835  if (IntMinIsPoison)
836  return BinaryOperator::CreateNSWNeg(IIOperand);
837  return BinaryOperator::CreateNeg(IIOperand);
838  }
839 
840  // abs (sext X) --> zext (abs X*)
841  // Clear the IsIntMin (nsw) bit on the abs to allow narrowing.
842  if (match(IIOperand, m_OneUse(m_SExt(m_Value(X))))) {
843  Value *NarrowAbs =
844  Builder.CreateBinaryIntrinsic(Intrinsic::abs, X, Builder.getFalse());
845  return CastInst::Create(Instruction::ZExt, NarrowAbs, II->getType());
846  }
847 
848  // Match a complicated way to check if a number is odd/even:
849  // abs (srem X, 2) --> and X, 1
850  const APInt *C;
851  if (match(IIOperand, m_SRem(m_Value(X), m_APInt(C))) && *C == 2)
852  return BinaryOperator::CreateAnd(X, ConstantInt::get(II->getType(), 1));
853 
854  break;
855  }
856  case Intrinsic::umax:
857  case Intrinsic::umin: {
858  Value *I0 = II->getArgOperand(0), *I1 = II->getArgOperand(1);
859  Value *X, *Y;
860  if (match(I0, m_ZExt(m_Value(X))) && match(I1, m_ZExt(m_Value(Y))) &&
861  (I0->hasOneUse() || I1->hasOneUse()) && X->getType() == Y->getType()) {
862  Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, Y);
863  return CastInst::Create(Instruction::ZExt, NarrowMaxMin, II->getType());
864  }
865  Constant *C;
866  if (match(I0, m_ZExt(m_Value(X))) && match(I1, m_Constant(C)) &&
867  I0->hasOneUse()) {
868  Constant *NarrowC = ConstantExpr::getTrunc(C, X->getType());
869  if (ConstantExpr::getZExt(NarrowC, II->getType()) == C) {
870  Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, NarrowC);
871  return CastInst::Create(Instruction::ZExt, NarrowMaxMin, II->getType());
872  }
873  }
874  // If both operands of unsigned min/max are sign-extended, it is still ok
875  // to narrow the operation.
877  }
878  case Intrinsic::smax:
879  case Intrinsic::smin: {
880  Value *I0 = II->getArgOperand(0), *I1 = II->getArgOperand(1);
881  Value *X, *Y;
882  if (match(I0, m_SExt(m_Value(X))) && match(I1, m_SExt(m_Value(Y))) &&
883  (I0->hasOneUse() || I1->hasOneUse()) && X->getType() == Y->getType()) {
884  Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, Y);
885  return CastInst::Create(Instruction::SExt, NarrowMaxMin, II->getType());
886  }
887 
888  Constant *C;
889  if (match(I0, m_SExt(m_Value(X))) && match(I1, m_Constant(C)) &&
890  I0->hasOneUse()) {
891  Constant *NarrowC = ConstantExpr::getTrunc(C, X->getType());
892  if (ConstantExpr::getSExt(NarrowC, II->getType()) == C) {
893  Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, NarrowC);
894  return CastInst::Create(Instruction::SExt, NarrowMaxMin, II->getType());
895  }
896  }
897 
898  if (match(I0, m_Not(m_Value(X)))) {
899  // max (not X), (not Y) --> not (min X, Y)
901  if (match(I1, m_Not(m_Value(Y))) &&
902  (I0->hasOneUse() || I1->hasOneUse())) {
903  Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, Y);
904  return BinaryOperator::CreateNot(InvMaxMin);
905  }
906  // max (not X), C --> not(min X, ~C)
907  if (match(I1, m_Constant(C)) && I0->hasOneUse()) {
909  Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, NotC);
910  return BinaryOperator::CreateNot(InvMaxMin);
911  }
912  }
913 
914  // smax(X, -X) --> abs(X)
915  // smin(X, -X) --> -abs(X)
916  // umax(X, -X) --> -abs(X)
917  // umin(X, -X) --> abs(X)
918  if (isKnownNegation(I0, I1)) {
919  // We can choose either operand as the input to abs(), but if we can
920  // eliminate the only use of a value, that's better for subsequent
921  // transforms/analysis.
922  if (I0->hasOneUse() && !I1->hasOneUse())
923  std::swap(I0, I1);
924 
925  // This is some variant of abs(). See if we can propagate 'nsw' to the abs
926  // operation and potentially its negation.
927  bool IntMinIsPoison = isKnownNegation(I0, I1, /* NeedNSW */ true);
928  Value *Abs = Builder.CreateBinaryIntrinsic(
929  Intrinsic::abs, I0,
930  ConstantInt::getBool(II->getContext(), IntMinIsPoison));
931 
932  // We don't have a "nabs" intrinsic, so negate if needed based on the
933  // max/min operation.
934  if (IID == Intrinsic::smin || IID == Intrinsic::umax)
935  Abs = Builder.CreateNeg(Abs, "nabs", /* NUW */ false, IntMinIsPoison);
936  return replaceInstUsesWith(CI, Abs);
937  }
938 
939  break;
940  }
941  case Intrinsic::bswap: {
942  Value *IIOperand = II->getArgOperand(0);
943  Value *X = nullptr;
944 
945  // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
946  if (match(IIOperand, m_Trunc(m_BSwap(m_Value(X))))) {
947  unsigned C = X->getType()->getScalarSizeInBits() -
948  IIOperand->getType()->getScalarSizeInBits();
949  Value *CV = ConstantInt::get(X->getType(), C);
950  Value *V = Builder.CreateLShr(X, CV);
951  return new TruncInst(V, IIOperand->getType());
952  }
953  break;
954  }
955  case Intrinsic::masked_load:
956  if (Value *SimplifiedMaskedOp = simplifyMaskedLoad(*II))
957  return replaceInstUsesWith(CI, SimplifiedMaskedOp);
958  break;
959  case Intrinsic::masked_store:
960  return simplifyMaskedStore(*II);
961  case Intrinsic::masked_gather:
962  return simplifyMaskedGather(*II);
963  case Intrinsic::masked_scatter:
964  return simplifyMaskedScatter(*II);
965  case Intrinsic::launder_invariant_group:
966  case Intrinsic::strip_invariant_group:
967  if (auto *SkippedBarrier = simplifyInvariantGroupIntrinsic(*II, *this))
968  return replaceInstUsesWith(*II, SkippedBarrier);
969  break;
970  case Intrinsic::powi:
971  if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
972  // 0 and 1 are handled in instsimplify
973  // powi(x, -1) -> 1/x
974  if (Power->isMinusOne())
976  II->getArgOperand(0), II);
977  // powi(x, 2) -> x*x
978  if (Power->equalsInt(2))
980  II->getArgOperand(0), II);
981  }
982  break;
983 
984  case Intrinsic::cttz:
985  case Intrinsic::ctlz:
986  if (auto *I = foldCttzCtlz(*II, *this))
987  return I;
988  break;
989 
990  case Intrinsic::ctpop:
991  if (auto *I = foldCtpop(*II, *this))
992  return I;
993  break;
994 
995  case Intrinsic::fshl:
996  case Intrinsic::fshr: {
997  Value *Op0 = II->getArgOperand(0), *Op1 = II->getArgOperand(1);
998  Type *Ty = II->getType();
999  unsigned BitWidth = Ty->getScalarSizeInBits();
1000  Constant *ShAmtC;
1001  if (match(II->getArgOperand(2), m_ImmConstant(ShAmtC)) &&
1002  !ShAmtC->containsConstantExpression()) {
1003  // Canonicalize a shift amount constant operand to modulo the bit-width.
1004  Constant *WidthC = ConstantInt::get(Ty, BitWidth);
1005  Constant *ModuloC = ConstantExpr::getURem(ShAmtC, WidthC);
1006  if (ModuloC != ShAmtC)
1007  return replaceOperand(*II, 2, ModuloC);
1008 
1009  assert(ConstantExpr::getICmp(ICmpInst::ICMP_UGT, WidthC, ShAmtC) ==
1011  "Shift amount expected to be modulo bitwidth");
1012 
1013  // Canonicalize funnel shift right by constant to funnel shift left. This
1014  // is not entirely arbitrary. For historical reasons, the backend may
1015  // recognize rotate left patterns but miss rotate right patterns.
1016  if (IID == Intrinsic::fshr) {
1017  // fshr X, Y, C --> fshl X, Y, (BitWidth - C)
1018  Constant *LeftShiftC = ConstantExpr::getSub(WidthC, ShAmtC);
1019  Module *Mod = II->getModule();
1020  Function *Fshl = Intrinsic::getDeclaration(Mod, Intrinsic::fshl, Ty);
1021  return CallInst::Create(Fshl, { Op0, Op1, LeftShiftC });
1022  }
1023  assert(IID == Intrinsic::fshl &&
1024  "All funnel shifts by simple constants should go left");
1025 
1026  // fshl(X, 0, C) --> shl X, C
1027  // fshl(X, undef, C) --> shl X, C
1028  if (match(Op1, m_ZeroInt()) || match(Op1, m_Undef()))
1029  return BinaryOperator::CreateShl(Op0, ShAmtC);
1030 
1031  // fshl(0, X, C) --> lshr X, (BW-C)
1032  // fshl(undef, X, C) --> lshr X, (BW-C)
1033  if (match(Op0, m_ZeroInt()) || match(Op0, m_Undef()))
1034  return BinaryOperator::CreateLShr(Op1,
1035  ConstantExpr::getSub(WidthC, ShAmtC));
1036 
1037  // fshl i16 X, X, 8 --> bswap i16 X (reduce to more-specific form)
1038  if (Op0 == Op1 && BitWidth == 16 && match(ShAmtC, m_SpecificInt(8))) {
1039  Module *Mod = II->getModule();
1040  Function *Bswap = Intrinsic::getDeclaration(Mod, Intrinsic::bswap, Ty);
1041  return CallInst::Create(Bswap, { Op0 });
1042  }
1043  }
1044 
1045  // Left or right might be masked.
1046  if (SimplifyDemandedInstructionBits(*II))
1047  return &CI;
1048 
1049  // The shift amount (operand 2) of a funnel shift is modulo the bitwidth,
1050  // so only the low bits of the shift amount are demanded if the bitwidth is
1051  // a power-of-2.
1052  if (!isPowerOf2_32(BitWidth))
1053  break;
1055  KnownBits Op2Known(BitWidth);
1056  if (SimplifyDemandedBits(II, 2, Op2Demanded, Op2Known))
1057  return &CI;
1058  break;
1059  }
1060  case Intrinsic::uadd_with_overflow:
1061  case Intrinsic::sadd_with_overflow: {
1062  if (Instruction *I = foldIntrinsicWithOverflowCommon(II))
1063  return I;
1064 
1065  // Given 2 constant operands whose sum does not overflow:
1066  // uaddo (X +nuw C0), C1 -> uaddo X, C0 + C1
1067  // saddo (X +nsw C0), C1 -> saddo X, C0 + C1
1068  Value *X;
1069  const APInt *C0, *C1;
1070  Value *Arg0 = II->getArgOperand(0);
1071  Value *Arg1 = II->getArgOperand(1);
1072  bool IsSigned = IID == Intrinsic::sadd_with_overflow;
1073  bool HasNWAdd = IsSigned ? match(Arg0, m_NSWAdd(m_Value(X), m_APInt(C0)))
1074  : match(Arg0, m_NUWAdd(m_Value(X), m_APInt(C0)));
1075  if (HasNWAdd && match(Arg1, m_APInt(C1))) {
1076  bool Overflow;
1077  APInt NewC =
1078  IsSigned ? C1->sadd_ov(*C0, Overflow) : C1->uadd_ov(*C0, Overflow);
1079  if (!Overflow)
1080  return replaceInstUsesWith(
1081  *II, Builder.CreateBinaryIntrinsic(
1082  IID, X, ConstantInt::get(Arg1->getType(), NewC)));
1083  }
1084  break;
1085  }
1086 
1087  case Intrinsic::umul_with_overflow:
1088  case Intrinsic::smul_with_overflow:
1089  case Intrinsic::usub_with_overflow:
1090  if (Instruction *I = foldIntrinsicWithOverflowCommon(II))
1091  return I;
1092  break;
1093 
1094  case Intrinsic::ssub_with_overflow: {
1095  if (Instruction *I = foldIntrinsicWithOverflowCommon(II))
1096  return I;
1097 
1098  Constant *C;
1099  Value *Arg0 = II->getArgOperand(0);
1100  Value *Arg1 = II->getArgOperand(1);
1101  // Given a constant C that is not the minimum signed value
1102  // for an integer of a given bit width:
1103  //
1104  // ssubo X, C -> saddo X, -C
1105  if (match(Arg1, m_Constant(C)) && C->isNotMinSignedValue()) {
1106  Value *NegVal = ConstantExpr::getNeg(C);
1107  // Build a saddo call that is equivalent to the discovered
1108  // ssubo call.
1109  return replaceInstUsesWith(
1110  *II, Builder.CreateBinaryIntrinsic(Intrinsic::sadd_with_overflow,
1111  Arg0, NegVal));
1112  }
1113 
1114  break;
1115  }
1116 
1117  case Intrinsic::uadd_sat:
1118  case Intrinsic::sadd_sat:
1119  case Intrinsic::usub_sat:
1120  case Intrinsic::ssub_sat: {
1121  SaturatingInst *SI = cast<SaturatingInst>(II);
1122  Type *Ty = SI->getType();
1123  Value *Arg0 = SI->getLHS();
1124  Value *Arg1 = SI->getRHS();
1125 
1126  // Make use of known overflow information.
1127  OverflowResult OR = computeOverflow(SI->getBinaryOp(), SI->isSigned(),
1128  Arg0, Arg1, SI);
1129  switch (OR) {
1131  break;
1133  if (SI->isSigned())
1134  return BinaryOperator::CreateNSW(SI->getBinaryOp(), Arg0, Arg1);
1135  else
1136  return BinaryOperator::CreateNUW(SI->getBinaryOp(), Arg0, Arg1);
1138  unsigned BitWidth = Ty->getScalarSizeInBits();
1139  APInt Min = APSInt::getMinValue(BitWidth, !SI->isSigned());
1140  return replaceInstUsesWith(*SI, ConstantInt::get(Ty, Min));
1141  }
1143  unsigned BitWidth = Ty->getScalarSizeInBits();
1144  APInt Max = APSInt::getMaxValue(BitWidth, !SI->isSigned());
1145  return replaceInstUsesWith(*SI, ConstantInt::get(Ty, Max));
1146  }
1147  }
1148 
1149  // ssub.sat(X, C) -> sadd.sat(X, -C) if C != MIN
1150  Constant *C;
1151  if (IID == Intrinsic::ssub_sat && match(Arg1, m_Constant(C)) &&
1152  C->isNotMinSignedValue()) {
1153  Value *NegVal = ConstantExpr::getNeg(C);
1154  return replaceInstUsesWith(
1155  *II, Builder.CreateBinaryIntrinsic(
1156  Intrinsic::sadd_sat, Arg0, NegVal));
1157  }
1158 
1159  // sat(sat(X + Val2) + Val) -> sat(X + (Val+Val2))
1160  // sat(sat(X - Val2) - Val) -> sat(X - (Val+Val2))
1161  // if Val and Val2 have the same sign
1162  if (auto *Other = dyn_cast<IntrinsicInst>(Arg0)) {
1163  Value *X;
1164  const APInt *Val, *Val2;
1165  APInt NewVal;
1166  bool IsUnsigned =
1167  IID == Intrinsic::uadd_sat || IID == Intrinsic::usub_sat;
1168  if (Other->getIntrinsicID() == IID &&
1169  match(Arg1, m_APInt(Val)) &&
1170  match(Other->getArgOperand(0), m_Value(X)) &&
1171  match(Other->getArgOperand(1), m_APInt(Val2))) {
1172  if (IsUnsigned)
1173  NewVal = Val->uadd_sat(*Val2);
1174  else if (Val->isNonNegative() == Val2->isNonNegative()) {
1175  bool Overflow;
1176  NewVal = Val->sadd_ov(*Val2, Overflow);
1177  if (Overflow) {
1178  // Both adds together may add more than SignedMaxValue
1179  // without saturating the final result.
1180  break;
1181  }
1182  } else {
1183  // Cannot fold saturated addition with different signs.
1184  break;
1185  }
1186 
1187  return replaceInstUsesWith(
1188  *II, Builder.CreateBinaryIntrinsic(
1189  IID, X, ConstantInt::get(II->getType(), NewVal)));
1190  }
1191  }
1192  break;
1193  }
1194 
1195  case Intrinsic::minnum:
1196  case Intrinsic::maxnum:
1197  case Intrinsic::minimum:
1198  case Intrinsic::maximum: {
1199  Value *Arg0 = II->getArgOperand(0);
1200  Value *Arg1 = II->getArgOperand(1);
1201  Value *X, *Y;
1202  if (match(Arg0, m_FNeg(m_Value(X))) && match(Arg1, m_FNeg(m_Value(Y))) &&
1203  (Arg0->hasOneUse() || Arg1->hasOneUse())) {
1204  // If both operands are negated, invert the call and negate the result:
1205  // min(-X, -Y) --> -(max(X, Y))
1206  // max(-X, -Y) --> -(min(X, Y))
1207  Intrinsic::ID NewIID;
1208  switch (IID) {
1209  case Intrinsic::maxnum:
1210  NewIID = Intrinsic::minnum;
1211  break;
1212  case Intrinsic::minnum:
1213  NewIID = Intrinsic::maxnum;
1214  break;
1215  case Intrinsic::maximum:
1216  NewIID = Intrinsic::minimum;
1217  break;
1218  case Intrinsic::minimum:
1219  NewIID = Intrinsic::maximum;
1220  break;
1221  default:
1222  llvm_unreachable("unexpected intrinsic ID");
1223  }
1224  Value *NewCall = Builder.CreateBinaryIntrinsic(NewIID, X, Y, II);
1225  Instruction *FNeg = UnaryOperator::CreateFNeg(NewCall);
1226  FNeg->copyIRFlags(II);
1227  return FNeg;
1228  }
1229 
1230  // m(m(X, C2), C1) -> m(X, C)
1231  const APFloat *C1, *C2;
1232  if (auto *M = dyn_cast<IntrinsicInst>(Arg0)) {
1233  if (M->getIntrinsicID() == IID && match(Arg1, m_APFloat(C1)) &&
1234  ((match(M->getArgOperand(0), m_Value(X)) &&
1235  match(M->getArgOperand(1), m_APFloat(C2))) ||
1236  (match(M->getArgOperand(1), m_Value(X)) &&
1237  match(M->getArgOperand(0), m_APFloat(C2))))) {
1238  APFloat Res(0.0);
1239  switch (IID) {
1240  case Intrinsic::maxnum:
1241  Res = maxnum(*C1, *C2);
1242  break;
1243  case Intrinsic::minnum:
1244  Res = minnum(*C1, *C2);
1245  break;
1246  case Intrinsic::maximum:
1247  Res = maximum(*C1, *C2);
1248  break;
1249  case Intrinsic::minimum:
1250  Res = minimum(*C1, *C2);
1251  break;
1252  default:
1253  llvm_unreachable("unexpected intrinsic ID");
1254  }
1255  Instruction *NewCall = Builder.CreateBinaryIntrinsic(
1256  IID, X, ConstantFP::get(Arg0->getType(), Res), II);
1257  // TODO: Conservatively intersecting FMF. If Res == C2, the transform
1258  // was a simplification (so Arg0 and its original flags could
1259  // propagate?)
1260  NewCall->andIRFlags(M);
1261  return replaceInstUsesWith(*II, NewCall);
1262  }
1263  }
1264 
1265  Value *ExtSrc0;
1266  Value *ExtSrc1;
1267 
1268  // minnum (fpext x), (fpext y) -> minnum x, y
1269  // maxnum (fpext x), (fpext y) -> maxnum x, y
1270  if (match(II->getArgOperand(0), m_OneUse(m_FPExt(m_Value(ExtSrc0)))) &&
1271  match(II->getArgOperand(1), m_OneUse(m_FPExt(m_Value(ExtSrc1)))) &&
1272  ExtSrc0->getType() == ExtSrc1->getType()) {
1274  II->getModule(), II->getIntrinsicID(), {ExtSrc0->getType()});
1275  CallInst *NewCall = Builder.CreateCall(F, { ExtSrc0, ExtSrc1 });
1276  NewCall->copyFastMathFlags(II);
1277  NewCall->takeName(II);
1278  return new FPExtInst(NewCall, II->getType());
1279  }
1280 
1281  break;
1282  }
1283  case Intrinsic::fmuladd: {
1284  // Canonicalize fast fmuladd to the separate fmul + fadd.
1285  if (II->isFast()) {
1287  Builder.setFastMathFlags(II->getFastMathFlags());
1288  Value *Mul = Builder.CreateFMul(II->getArgOperand(0),
1289  II->getArgOperand(1));
1290  Value *Add = Builder.CreateFAdd(Mul, II->getArgOperand(2));
1291  Add->takeName(II);
1292  return replaceInstUsesWith(*II, Add);
1293  }
1294 
1295  // Try to simplify the underlying FMul.
1296  if (Value *V = SimplifyFMulInst(II->getArgOperand(0), II->getArgOperand(1),
1297  II->getFastMathFlags(),
1298  SQ.getWithInstruction(II))) {
1299  auto *FAdd = BinaryOperator::CreateFAdd(V, II->getArgOperand(2));
1300  FAdd->copyFastMathFlags(II);
1301  return FAdd;
1302  }
1303 
1305  }
1306  case Intrinsic::fma: {
1307  // fma fneg(x), fneg(y), z -> fma x, y, z
1308  Value *Src0 = II->getArgOperand(0);
1309  Value *Src1 = II->getArgOperand(1);
1310  Value *X, *Y;
1311  if (match(Src0, m_FNeg(m_Value(X))) && match(Src1, m_FNeg(m_Value(Y)))) {
1312  replaceOperand(*II, 0, X);
1313  replaceOperand(*II, 1, Y);
1314  return II;
1315  }
1316 
1317  // fma fabs(x), fabs(x), z -> fma x, x, z
1318  if (match(Src0, m_FAbs(m_Value(X))) &&
1319  match(Src1, m_FAbs(m_Specific(X)))) {
1320  replaceOperand(*II, 0, X);
1321  replaceOperand(*II, 1, X);
1322  return II;
1323  }
1324 
1325  // Try to simplify the underlying FMul. We can only apply simplifications
1326  // that do not require rounding.
1327  if (Value *V = SimplifyFMAFMul(II->getArgOperand(0), II->getArgOperand(1),
1328  II->getFastMathFlags(),
1329  SQ.getWithInstruction(II))) {
1330  auto *FAdd = BinaryOperator::CreateFAdd(V, II->getArgOperand(2));
1331  FAdd->copyFastMathFlags(II);
1332  return FAdd;
1333  }
1334 
1335  // fma x, y, 0 -> fmul x, y
1336  // This is always valid for -0.0, but requires nsz for +0.0 as
1337  // -0.0 + 0.0 = 0.0, which would not be the same as the fmul on its own.
1338  if (match(II->getArgOperand(2), m_NegZeroFP()) ||
1339  (match(II->getArgOperand(2), m_PosZeroFP()) &&
1341  return BinaryOperator::CreateFMulFMF(Src0, Src1, II);
1342 
1343  break;
1344  }
1345  case Intrinsic::copysign: {
1346  Value *Mag = II->getArgOperand(0), *Sign = II->getArgOperand(1);
1347  if (SignBitMustBeZero(Sign, &TLI)) {
1348  // If we know that the sign argument is positive, reduce to FABS:
1349  // copysign Mag, +Sign --> fabs Mag
1350  Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, Mag, II);
1351  return replaceInstUsesWith(*II, Fabs);
1352  }
1353  // TODO: There should be a ValueTracking sibling like SignBitMustBeOne.
1354  const APFloat *C;
1355  if (match(Sign, m_APFloat(C)) && C->isNegative()) {
1356  // If we know that the sign argument is negative, reduce to FNABS:
1357  // copysign Mag, -Sign --> fneg (fabs Mag)
1358  Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, Mag, II);
1359  return replaceInstUsesWith(*II, Builder.CreateFNegFMF(Fabs, II));
1360  }
1361 
1362  // Propagate sign argument through nested calls:
1363  // copysign Mag, (copysign ?, X) --> copysign Mag, X
1364  Value *X;
1365  if (match(Sign, m_Intrinsic<Intrinsic::copysign>(m_Value(), m_Value(X))))
1366  return replaceOperand(*II, 1, X);
1367 
1368  // Peek through changes of magnitude's sign-bit. This call rewrites those:
1369  // copysign (fabs X), Sign --> copysign X, Sign
1370  // copysign (fneg X), Sign --> copysign X, Sign
1371  if (match(Mag, m_FAbs(m_Value(X))) || match(Mag, m_FNeg(m_Value(X))))
1372  return replaceOperand(*II, 0, X);
1373 
1374  break;
1375  }
1376  case Intrinsic::fabs: {
1377  Value *Cond, *TVal, *FVal;
1378  if (match(II->getArgOperand(0),
1379  m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
1380  // fabs (select Cond, TrueC, FalseC) --> select Cond, AbsT, AbsF
1381  if (isa<Constant>(TVal) && isa<Constant>(FVal)) {
1382  CallInst *AbsT = Builder.CreateCall(II->getCalledFunction(), {TVal});
1383  CallInst *AbsF = Builder.CreateCall(II->getCalledFunction(), {FVal});
1384  return SelectInst::Create(Cond, AbsT, AbsF);
1385  }
1386  // fabs (select Cond, -FVal, FVal) --> fabs FVal
1387  if (match(TVal, m_FNeg(m_Specific(FVal))))
1388  return replaceOperand(*II, 0, FVal);
1389  // fabs (select Cond, TVal, -TVal) --> fabs TVal
1390  if (match(FVal, m_FNeg(m_Specific(TVal))))
1391  return replaceOperand(*II, 0, TVal);
1392  }
1393 
1395  }
1396  case Intrinsic::ceil:
1397  case Intrinsic::floor:
1398  case Intrinsic::round:
1399  case Intrinsic::roundeven:
1400  case Intrinsic::nearbyint:
1401  case Intrinsic::rint:
1402  case Intrinsic::trunc: {
1403  Value *ExtSrc;
1404  if (match(II->getArgOperand(0), m_OneUse(m_FPExt(m_Value(ExtSrc))))) {
1405  // Narrow the call: intrinsic (fpext x) -> fpext (intrinsic x)
1406  Value *NarrowII = Builder.CreateUnaryIntrinsic(IID, ExtSrc, II);
1407  return new FPExtInst(NarrowII, II->getType());
1408  }
1409  break;
1410  }
1411  case Intrinsic::cos:
1412  case Intrinsic::amdgcn_cos: {
1413  Value *X;
1414  Value *Src = II->getArgOperand(0);
1415  if (match(Src, m_FNeg(m_Value(X))) || match(Src, m_FAbs(m_Value(X)))) {
1416  // cos(-x) -> cos(x)
1417  // cos(fabs(x)) -> cos(x)
1418  return replaceOperand(*II, 0, X);
1419  }
1420  break;
1421  }
1422  case Intrinsic::sin: {
1423  Value *X;
1424  if (match(II->getArgOperand(0), m_OneUse(m_FNeg(m_Value(X))))) {
1425  // sin(-x) --> -sin(x)
1426  Value *NewSin = Builder.CreateUnaryIntrinsic(Intrinsic::sin, X, II);
1427  Instruction *FNeg = UnaryOperator::CreateFNeg(NewSin);
1428  FNeg->copyFastMathFlags(II);
1429  return FNeg;
1430  }
1431  break;
1432  }
1433 
1434  case Intrinsic::arm_neon_vtbl1:
1435  case Intrinsic::aarch64_neon_tbl1:
1436  if (Value *V = simplifyNeonTbl1(*II, Builder))
1437  return replaceInstUsesWith(*II, V);
1438  break;
1439 
1440  case Intrinsic::arm_neon_vmulls:
1441  case Intrinsic::arm_neon_vmullu:
1442  case Intrinsic::aarch64_neon_smull:
1443  case Intrinsic::aarch64_neon_umull: {
1444  Value *Arg0 = II->getArgOperand(0);
1445  Value *Arg1 = II->getArgOperand(1);
1446 
1447  // Handle mul by zero first:
1448  if (isa<ConstantAggregateZero>(Arg0) || isa<ConstantAggregateZero>(Arg1)) {
1449  return replaceInstUsesWith(CI, ConstantAggregateZero::get(II->getType()));
1450  }
1451 
1452  // Check for constant LHS & RHS - in this case we just simplify.
1453  bool Zext = (IID == Intrinsic::arm_neon_vmullu ||
1454  IID == Intrinsic::aarch64_neon_umull);
1455  VectorType *NewVT = cast<VectorType>(II->getType());
1456  if (Constant *CV0 = dyn_cast<Constant>(Arg0)) {
1457  if (Constant *CV1 = dyn_cast<Constant>(Arg1)) {
1458  CV0 = ConstantExpr::getIntegerCast(CV0, NewVT, /*isSigned=*/!Zext);
1459  CV1 = ConstantExpr::getIntegerCast(CV1, NewVT, /*isSigned=*/!Zext);
1460 
1461  return replaceInstUsesWith(CI, ConstantExpr::getMul(CV0, CV1));
1462  }
1463 
1464  // Couldn't simplify - canonicalize constant to the RHS.
1465  std::swap(Arg0, Arg1);
1466  }
1467 
1468  // Handle mul by one:
1469  if (Constant *CV1 = dyn_cast<Constant>(Arg1))
1470  if (ConstantInt *Splat =
1471  dyn_cast_or_null<ConstantInt>(CV1->getSplatValue()))
1472  if (Splat->isOne())
1473  return CastInst::CreateIntegerCast(Arg0, II->getType(),
1474  /*isSigned=*/!Zext);
1475 
1476  break;
1477  }
1478  case Intrinsic::arm_neon_aesd:
1479  case Intrinsic::arm_neon_aese:
1480  case Intrinsic::aarch64_crypto_aesd:
1481  case Intrinsic::aarch64_crypto_aese: {
1482  Value *DataArg = II->getArgOperand(0);
1483  Value *KeyArg = II->getArgOperand(1);
1484 
1485  // Try to use the builtin XOR in AESE and AESD to eliminate a prior XOR
1486  Value *Data, *Key;
1487  if (match(KeyArg, m_ZeroInt()) &&
1488  match(DataArg, m_Xor(m_Value(Data), m_Value(Key)))) {
1489  replaceOperand(*II, 0, Data);
1490  replaceOperand(*II, 1, Key);
1491  return II;
1492  }
1493  break;
1494  }
1495  case Intrinsic::hexagon_V6_vandvrt:
1496  case Intrinsic::hexagon_V6_vandvrt_128B: {
1497  // Simplify Q -> V -> Q conversion.
1498  if (auto Op0 = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
1499  Intrinsic::ID ID0 = Op0->getIntrinsicID();
1500  if (ID0 != Intrinsic::hexagon_V6_vandqrt &&
1501  ID0 != Intrinsic::hexagon_V6_vandqrt_128B)
1502  break;
1503  Value *Bytes = Op0->getArgOperand(1), *Mask = II->getArgOperand(1);
1504  uint64_t Bytes1 = computeKnownBits(Bytes, 0, Op0).One.getZExtValue();
1505  uint64_t Mask1 = computeKnownBits(Mask, 0, II).One.getZExtValue();
1506  // Check if every byte has common bits in Bytes and Mask.
1507  uint64_t C = Bytes1 & Mask1;
1508  if ((C & 0xFF) && (C & 0xFF00) && (C & 0xFF0000) && (C & 0xFF000000))
1509  return replaceInstUsesWith(*II, Op0->getArgOperand(0));
1510  }
1511  break;
1512  }
1513  case Intrinsic::stackrestore: {
1514  // If the save is right next to the restore, remove the restore. This can
1515  // happen when variable allocas are DCE'd.
1516  if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
1517  if (SS->getIntrinsicID() == Intrinsic::stacksave) {
1518  // Skip over debug info.
1519  if (SS->getNextNonDebugInstruction() == II) {
1520  return eraseInstFromFunction(CI);
1521  }
1522  }
1523  }
1524 
1525  // Scan down this block to see if there is another stack restore in the
1526  // same block without an intervening call/alloca.
1527  BasicBlock::iterator BI(II);
1528  Instruction *TI = II->getParent()->getTerminator();
1529  bool CannotRemove = false;
1530  for (++BI; &*BI != TI; ++BI) {
1531  if (isa<AllocaInst>(BI)) {
1532  CannotRemove = true;
1533  break;
1534  }
1535  if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
1536  if (auto *II2 = dyn_cast<IntrinsicInst>(BCI)) {
1537  // If there is a stackrestore below this one, remove this one.
1538  if (II2->getIntrinsicID() == Intrinsic::stackrestore)
1539  return eraseInstFromFunction(CI);
1540 
1541  // Bail if we cross over an intrinsic with side effects, such as
1542  // llvm.stacksave, or llvm.read_register.
1543  if (II2->mayHaveSideEffects()) {
1544  CannotRemove = true;
1545  break;
1546  }
1547  } else {
1548  // If we found a non-intrinsic call, we can't remove the stack
1549  // restore.
1550  CannotRemove = true;
1551  break;
1552  }
1553  }
1554  }
1555 
1556  // If the stack restore is in a return, resume, or unwind block and if there
1557  // are no allocas or calls between the restore and the return, nuke the
1558  // restore.
1559  if (!CannotRemove && (isa<ReturnInst>(TI) || isa<ResumeInst>(TI)))
1560  return eraseInstFromFunction(CI);
1561  break;
1562  }
1563  case Intrinsic::lifetime_end:
1564  // Asan needs to poison memory to detect invalid access which is possible
1565  // even for empty lifetime range.
1566  if (II->getFunction()->hasFnAttribute(Attribute::SanitizeAddress) ||
1567  II->getFunction()->hasFnAttribute(Attribute::SanitizeMemory) ||
1568  II->getFunction()->hasFnAttribute(Attribute::SanitizeHWAddress))
1569  break;
1570 
1571  if (removeTriviallyEmptyRange(*II, *this, [](const IntrinsicInst &I) {
1572  return I.getIntrinsicID() == Intrinsic::lifetime_start;
1573  }))
1574  return nullptr;
1575  break;
1576  case Intrinsic::assume: {
1577  Value *IIOperand = II->getArgOperand(0);
1579  II->getOperandBundlesAsDefs(OpBundles);
1580 
1581  /// This will remove the boolean Condition from the assume given as
1582  /// argument and remove the assume if it becomes useless.
1583  /// always returns nullptr for use as a return values.
1584  auto RemoveConditionFromAssume = [&](Instruction *Assume) -> Instruction * {
1585  assert(isa<AssumeInst>(Assume));
1586  if (isAssumeWithEmptyBundle(*cast<AssumeInst>(II)))
1587  return eraseInstFromFunction(CI);
1588  replaceUse(II->getOperandUse(0), ConstantInt::getTrue(II->getContext()));
1589  return nullptr;
1590  };
1591  // Remove an assume if it is followed by an identical assume.
1592  // TODO: Do we need this? Unless there are conflicting assumptions, the
1593  // computeKnownBits(IIOperand) below here eliminates redundant assumes.
1595  if (match(Next, m_Intrinsic<Intrinsic::assume>(m_Specific(IIOperand))))
1596  return RemoveConditionFromAssume(Next);
1597 
1598  // Canonicalize assume(a && b) -> assume(a); assume(b);
1599  // Note: New assumption intrinsics created here are registered by
1600  // the InstCombineIRInserter object.
1601  FunctionType *AssumeIntrinsicTy = II->getFunctionType();
1602  Value *AssumeIntrinsic = II->getCalledOperand();
1603  Value *A, *B;
1604  if (match(IIOperand, m_LogicalAnd(m_Value(A), m_Value(B)))) {
1605  Builder.CreateCall(AssumeIntrinsicTy, AssumeIntrinsic, A, OpBundles,
1606  II->getName());
1607  Builder.CreateCall(AssumeIntrinsicTy, AssumeIntrinsic, B, II->getName());
1608  return eraseInstFromFunction(*II);
1609  }
1610  // assume(!(a || b)) -> assume(!a); assume(!b);
1611  if (match(IIOperand, m_Not(m_LogicalOr(m_Value(A), m_Value(B))))) {
1612  Builder.CreateCall(AssumeIntrinsicTy, AssumeIntrinsic,
1613  Builder.CreateNot(A), OpBundles, II->getName());
1614  Builder.CreateCall(AssumeIntrinsicTy, AssumeIntrinsic,
1615  Builder.CreateNot(B), II->getName());
1616  return eraseInstFromFunction(*II);
1617  }
1618 
1619  // assume( (load addr) != null ) -> add 'nonnull' metadata to load
1620  // (if assume is valid at the load)
1621  CmpInst::Predicate Pred;
1622  Instruction *LHS;
1623  if (match(IIOperand, m_ICmp(Pred, m_Instruction(LHS), m_Zero())) &&
1624  Pred == ICmpInst::ICMP_NE && LHS->getOpcode() == Instruction::Load &&
1625  LHS->getType()->isPointerTy() &&
1626  isValidAssumeForContext(II, LHS, &DT)) {
1627  MDNode *MD = MDNode::get(II->getContext(), None);
1628  LHS->setMetadata(LLVMContext::MD_nonnull, MD);
1629  return RemoveConditionFromAssume(II);
1630 
1631  // TODO: apply nonnull return attributes to calls and invokes
1632  // TODO: apply range metadata for range check patterns?
1633  }
1634 
1635  // Convert nonnull assume like:
1636  // %A = icmp ne i32* %PTR, null
1637  // call void @llvm.assume(i1 %A)
1638  // into
1639  // call void @llvm.assume(i1 true) [ "nonnull"(i32* %PTR) ]
1641  match(IIOperand, m_Cmp(Pred, m_Value(A), m_Zero())) &&
1642  Pred == CmpInst::ICMP_NE && A->getType()->isPointerTy()) {
1643  if (auto *Replacement = buildAssumeFromKnowledge(
1644  {RetainedKnowledge{Attribute::NonNull, 0, A}}, Next, &AC, &DT)) {
1645 
1646  Replacement->insertBefore(Next);
1647  AC.registerAssumption(Replacement);
1648  return RemoveConditionFromAssume(II);
1649  }
1650  }
1651 
1652  // Convert alignment assume like:
1653  // %B = ptrtoint i32* %A to i64
1654  // %C = and i64 %B, Constant
1655  // %D = icmp eq i64 %C, 0
1656  // call void @llvm.assume(i1 %D)
1657  // into
1658  // call void @llvm.assume(i1 true) [ "align"(i32* [[A]], i64 Constant + 1)]
1659  uint64_t AlignMask;
1661  match(IIOperand,
1662  m_Cmp(Pred, m_And(m_Value(A), m_ConstantInt(AlignMask)),
1663  m_Zero())) &&
1664  Pred == CmpInst::ICMP_EQ) {
1665  if (isPowerOf2_64(AlignMask + 1)) {
1666  uint64_t Offset = 0;
1668  if (match(A, m_PtrToInt(m_Value(A)))) {
1669  /// Note: this doesn't preserve the offset information but merges
1670  /// offset and alignment.
1671  /// TODO: we can generate a GEP instead of merging the alignment with
1672  /// the offset.
1673  RetainedKnowledge RK{Attribute::Alignment,
1674  (unsigned)MinAlign(Offset, AlignMask + 1), A};
1675  if (auto *Replacement =
1676  buildAssumeFromKnowledge(RK, Next, &AC, &DT)) {
1677 
1678  Replacement->insertAfter(II);
1679  AC.registerAssumption(Replacement);
1680  }
1681  return RemoveConditionFromAssume(II);
1682  }
1683  }
1684  }
1685 
1686  /// Canonicalize Knowledge in operand bundles.
1688  for (unsigned Idx = 0; Idx < II->getNumOperandBundles(); Idx++) {
1689  auto &BOI = II->bundle_op_info_begin()[Idx];
1690  RetainedKnowledge RK =
1691  llvm::getKnowledgeFromBundle(cast<AssumeInst>(*II), BOI);
1692  if (BOI.End - BOI.Begin > 2)
1693  continue; // Prevent reducing knowledge in an align with offset since
1694  // extracting a RetainedKnowledge form them looses offset
1695  // information
1696  RetainedKnowledge CanonRK =
1697  llvm::simplifyRetainedKnowledge(cast<AssumeInst>(II), RK,
1698  &getAssumptionCache(),
1699  &getDominatorTree());
1700  if (CanonRK == RK)
1701  continue;
1702  if (!CanonRK) {
1703  if (BOI.End - BOI.Begin > 0) {
1704  Worklist.pushValue(II->op_begin()[BOI.Begin]);
1705  Value::dropDroppableUse(II->op_begin()[BOI.Begin]);
1706  }
1707  continue;
1708  }
1709  assert(RK.AttrKind == CanonRK.AttrKind);
1710  if (BOI.End - BOI.Begin > 0)
1711  II->op_begin()[BOI.Begin].set(CanonRK.WasOn);
1712  if (BOI.End - BOI.Begin > 1)
1713  II->op_begin()[BOI.Begin + 1].set(ConstantInt::get(
1714  Type::getInt64Ty(II->getContext()), CanonRK.ArgValue));
1715  if (RK.WasOn)
1716  Worklist.pushValue(RK.WasOn);
1717  return II;
1718  }
1719  }
1720 
1721  // If there is a dominating assume with the same condition as this one,
1722  // then this one is redundant, and should be removed.
1723  KnownBits Known(1);
1724  computeKnownBits(IIOperand, Known, 0, II);
1725  if (Known.isAllOnes() && isAssumeWithEmptyBundle(cast<AssumeInst>(*II)))
1726  return eraseInstFromFunction(*II);
1727 
1728  // Update the cache of affected values for this assumption (we might be
1729  // here because we just simplified the condition).
1730  AC.updateAffectedValues(cast<AssumeInst>(II));
1731  break;
1732  }
1733  case Intrinsic::experimental_guard: {
1734  // Is this guard followed by another guard? We scan forward over a small
1735  // fixed window of instructions to handle common cases with conditions
1736  // computed between guards.
1737  Instruction *NextInst = II->getNextNonDebugInstruction();
1738  for (unsigned i = 0; i < GuardWideningWindow; i++) {
1739  // Note: Using context-free form to avoid compile time blow up
1740  if (!isSafeToSpeculativelyExecute(NextInst))
1741  break;
1742  NextInst = NextInst->getNextNonDebugInstruction();
1743  }
1744  Value *NextCond = nullptr;
1745  if (match(NextInst,
1746  m_Intrinsic<Intrinsic::experimental_guard>(m_Value(NextCond)))) {
1747  Value *CurrCond = II->getArgOperand(0);
1748 
1749  // Remove a guard that it is immediately preceded by an identical guard.
1750  // Otherwise canonicalize guard(a); guard(b) -> guard(a & b).
1751  if (CurrCond != NextCond) {
1752  Instruction *MoveI = II->getNextNonDebugInstruction();
1753  while (MoveI != NextInst) {
1754  auto *Temp = MoveI;
1755  MoveI = MoveI->getNextNonDebugInstruction();
1756  Temp->moveBefore(II);
1757  }
1758  replaceOperand(*II, 0, Builder.CreateAnd(CurrCond, NextCond));
1759  }
1760  eraseInstFromFunction(*NextInst);
1761  return II;
1762  }
1763  break;
1764  }
1765  case Intrinsic::experimental_vector_insert: {
1766  Value *Vec = II->getArgOperand(0);
1767  Value *SubVec = II->getArgOperand(1);
1768  Value *Idx = II->getArgOperand(2);
1769  auto *DstTy = dyn_cast<FixedVectorType>(II->getType());
1770  auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType());
1771  auto *SubVecTy = dyn_cast<FixedVectorType>(SubVec->getType());
1772 
1773  // Only canonicalize if the destination vector, Vec, and SubVec are all
1774  // fixed vectors.
1775  if (DstTy && VecTy && SubVecTy) {
1776  unsigned DstNumElts = DstTy->getNumElements();
1777  unsigned VecNumElts = VecTy->getNumElements();
1778  unsigned SubVecNumElts = SubVecTy->getNumElements();
1779  unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
1780 
1781  // The result of this call is undefined if IdxN is not a constant multiple
1782  // of the SubVec's minimum vector length OR the insertion overruns Vec.
1783  if (IdxN % SubVecNumElts != 0 || IdxN + SubVecNumElts > VecNumElts) {
1784  replaceInstUsesWith(CI, UndefValue::get(CI.getType()));
1785  return eraseInstFromFunction(CI);
1786  }
1787 
1788  // An insert that entirely overwrites Vec with SubVec is a nop.
1789  if (VecNumElts == SubVecNumElts) {
1790  replaceInstUsesWith(CI, SubVec);
1791  return eraseInstFromFunction(CI);
1792  }
1793 
1794  // Widen SubVec into a vector of the same width as Vec, since
1795  // shufflevector requires the two input vectors to be the same width.
1796  // Elements beyond the bounds of SubVec within the widened vector are
1797  // undefined.
1798  SmallVector<int, 8> WidenMask;
1799  unsigned i;
1800  for (i = 0; i != SubVecNumElts; ++i)
1801  WidenMask.push_back(i);
1802  for (; i != VecNumElts; ++i)
1803  WidenMask.push_back(UndefMaskElem);
1804 
1805  Value *WidenShuffle = Builder.CreateShuffleVector(SubVec, WidenMask);
1806 
1808  for (unsigned i = 0; i != IdxN; ++i)
1809  Mask.push_back(i);
1810  for (unsigned i = DstNumElts; i != DstNumElts + SubVecNumElts; ++i)
1811  Mask.push_back(i);
1812  for (unsigned i = IdxN + SubVecNumElts; i != DstNumElts; ++i)
1813  Mask.push_back(i);
1814 
1815  Value *Shuffle = Builder.CreateShuffleVector(Vec, WidenShuffle, Mask);
1816  replaceInstUsesWith(CI, Shuffle);
1817  return eraseInstFromFunction(CI);
1818  }
1819  break;
1820  }
1821  case Intrinsic::experimental_vector_extract: {
1822  Value *Vec = II->getArgOperand(0);
1823  Value *Idx = II->getArgOperand(1);
1824 
1825  auto *DstTy = dyn_cast<FixedVectorType>(II->getType());
1826  auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType());
1827 
1828  // Only canonicalize if the the destination vector and Vec are fixed
1829  // vectors.
1830  if (DstTy && VecTy) {
1831  unsigned DstNumElts = DstTy->getNumElements();
1832  unsigned VecNumElts = VecTy->getNumElements();
1833  unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
1834 
1835  // The result of this call is undefined if IdxN is not a constant multiple
1836  // of the result type's minimum vector length OR the extraction overruns
1837  // Vec.
1838  if (IdxN % DstNumElts != 0 || IdxN + DstNumElts > VecNumElts) {
1839  replaceInstUsesWith(CI, UndefValue::get(CI.getType()));
1840  return eraseInstFromFunction(CI);
1841  }
1842 
1843  // Extracting the entirety of Vec is a nop.
1844  if (VecNumElts == DstNumElts) {
1845  replaceInstUsesWith(CI, Vec);
1846  return eraseInstFromFunction(CI);
1847  }
1848 
1850  for (unsigned i = 0; i != DstNumElts; ++i)
1851  Mask.push_back(IdxN + i);
1852 
1853  Value *Shuffle =
1854  Builder.CreateShuffleVector(Vec, UndefValue::get(VecTy), Mask);
1855  replaceInstUsesWith(CI, Shuffle);
1856  return eraseInstFromFunction(CI);
1857  }
1858  break;
1859  }
1860  case Intrinsic::vector_reduce_or:
1861  case Intrinsic::vector_reduce_and: {
1862  // Canonicalize logical or/and reductions:
1863  // Or reduction for i1 is represented as:
1864  // %val = bitcast <ReduxWidth x i1> to iReduxWidth
1865  // %res = cmp ne iReduxWidth %val, 0
1866  // And reduction for i1 is represented as:
1867  // %val = bitcast <ReduxWidth x i1> to iReduxWidth
1868  // %res = cmp eq iReduxWidth %val, 11111
1869  Value *Arg = II->getArgOperand(0);
1870  Type *RetTy = II->getType();
1871  if (RetTy == Builder.getInt1Ty())
1872  if (auto *FVTy = dyn_cast<FixedVectorType>(Arg->getType())) {
1873  Value *Res = Builder.CreateBitCast(
1874  Arg, Builder.getIntNTy(FVTy->getNumElements()));
1875  if (IID == Intrinsic::vector_reduce_and) {
1876  Res = Builder.CreateICmpEQ(
1877  Res, ConstantInt::getAllOnesValue(Res->getType()));
1878  } else {
1879  assert(IID == Intrinsic::vector_reduce_or &&
1880  "Expected or reduction.");
1881  Res = Builder.CreateIsNotNull(Res);
1882  }
1883  replaceInstUsesWith(CI, Res);
1884  return eraseInstFromFunction(CI);
1885  }
1886  break;
1887  }
1888  default: {
1889  // Handle target specific intrinsics
1890  Optional<Instruction *> V = targetInstCombineIntrinsic(*II);
1891  if (V.hasValue())
1892  return V.getValue();
1893  break;
1894  }
1895  }
1896  // Some intrinsics (like experimental_gc_statepoint) can be used in invoke
1897  // context, so it is handled in visitCallBase and we should trigger it.
1898  return visitCallBase(*II);
1899 }
1900 
1901 // Fence instruction simplification
1903  // Remove identical consecutive fences.
1905  if (auto *NFI = dyn_cast<FenceInst>(Next))
1906  if (FI.isIdenticalTo(NFI))
1907  return eraseInstFromFunction(FI);
1908  return nullptr;
1909 }
1910 
1911 // InvokeInst simplification
1913  return visitCallBase(II);
1914 }
1915 
1916 // CallBrInst simplification
1918  return visitCallBase(CBI);
1919 }
1920 
1921 /// If this cast does not affect the value passed through the varargs area, we
1922 /// can eliminate the use of the cast.
1923 static bool isSafeToEliminateVarargsCast(const CallBase &Call,
1924  const DataLayout &DL,
1925  const CastInst *const CI,
1926  const int ix) {
1927  if (!CI->isLosslessCast())
1928  return false;
1929 
1930  // If this is a GC intrinsic, avoid munging types. We need types for
1931  // statepoint reconstruction in SelectionDAG.
1932  // TODO: This is probably something which should be expanded to all
1933  // intrinsics since the entire point of intrinsics is that
1934  // they are understandable by the optimizer.
1935  if (isa<GCStatepointInst>(Call) || isa<GCRelocateInst>(Call) ||
1936  isa<GCResultInst>(Call))
1937  return false;
1938 
1939  // The size of ByVal or InAlloca arguments is derived from the type, so we
1940  // can't change to a type with a different size. If the size were
1941  // passed explicitly we could avoid this check.
1942  if (!Call.isPassPointeeByValueArgument(ix))
1943  return true;
1944 
1945  Type* SrcTy =
1946  cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
1947  Type *DstTy = Call.isByValArgument(ix)
1948  ? Call.getParamByValType(ix)
1949  : cast<PointerType>(CI->getType())->getElementType();
1950  if (!SrcTy->isSized() || !DstTy->isSized())
1951  return false;
1952  if (DL.getTypeAllocSize(SrcTy) != DL.getTypeAllocSize(DstTy))
1953  return false;
1954  return true;
1955 }
1956 
1957 Instruction *InstCombinerImpl::tryOptimizeCall(CallInst *CI) {
1958  if (!CI->getCalledFunction()) return nullptr;
1959 
1960  auto InstCombineRAUW = [this](Instruction *From, Value *With) {
1961  replaceInstUsesWith(*From, With);
1962  };
1963  auto InstCombineErase = [this](Instruction *I) {
1964  eraseInstFromFunction(*I);
1965  };
1966  LibCallSimplifier Simplifier(DL, &TLI, ORE, BFI, PSI, InstCombineRAUW,
1967  InstCombineErase);
1968  if (Value *With = Simplifier.optimizeCall(CI, Builder)) {
1969  ++NumSimplified;
1970  return CI->use_empty() ? CI : replaceInstUsesWith(*CI, With);
1971  }
1972 
1973  return nullptr;
1974 }
1975 
1977  // Strip off at most one level of pointer casts, looking for an alloca. This
1978  // is good enough in practice and simpler than handling any number of casts.
1979  Value *Underlying = TrampMem->stripPointerCasts();
1980  if (Underlying != TrampMem &&
1981  (!Underlying->hasOneUse() || Underlying->user_back() != TrampMem))
1982  return nullptr;
1983  if (!isa<AllocaInst>(Underlying))
1984  return nullptr;
1985 
1986  IntrinsicInst *InitTrampoline = nullptr;
1987  for (User *U : TrampMem->users()) {
1988  IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
1989  if (!II)
1990  return nullptr;
1991  if (II->getIntrinsicID() == Intrinsic::init_trampoline) {
1992  if (InitTrampoline)
1993  // More than one init_trampoline writes to this value. Give up.
1994  return nullptr;
1995  InitTrampoline = II;
1996  continue;
1997  }
1998  if (II->getIntrinsicID() == Intrinsic::adjust_trampoline)
1999  // Allow any number of calls to adjust.trampoline.
2000  continue;
2001  return nullptr;
2002  }
2003 
2004  // No call to init.trampoline found.
2005  if (!InitTrampoline)
2006  return nullptr;
2007 
2008  // Check that the alloca is being used in the expected way.
2009  if (InitTrampoline->getOperand(0) != TrampMem)
2010  return nullptr;
2011 
2012  return InitTrampoline;
2013 }
2014 
2016  Value *TrampMem) {
2017  // Visit all the previous instructions in the basic block, and try to find a
2018  // init.trampoline which has a direct path to the adjust.trampoline.
2019  for (BasicBlock::iterator I = AdjustTramp->getIterator(),
2020  E = AdjustTramp->getParent()->begin();
2021  I != E;) {
2022  Instruction *Inst = &*--I;
2023  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2024  if (II->getIntrinsicID() == Intrinsic::init_trampoline &&
2025  II->getOperand(0) == TrampMem)
2026  return II;
2027  if (Inst->mayWriteToMemory())
2028  return nullptr;
2029  }
2030  return nullptr;
2031 }
2032 
2033 // Given a call to llvm.adjust.trampoline, find and return the corresponding
2034 // call to llvm.init.trampoline if the call to the trampoline can be optimized
2035 // to a direct call to a function. Otherwise return NULL.
2037  Callee = Callee->stripPointerCasts();
2038  IntrinsicInst *AdjustTramp = dyn_cast<IntrinsicInst>(Callee);
2039  if (!AdjustTramp ||
2040  AdjustTramp->getIntrinsicID() != Intrinsic::adjust_trampoline)
2041  return nullptr;
2042 
2043  Value *TrampMem = AdjustTramp->getOperand(0);
2044 
2046  return IT;
2047  if (IntrinsicInst *IT = findInitTrampolineFromBB(AdjustTramp, TrampMem))
2048  return IT;
2049  return nullptr;
2050 }
2051 
2052 static void annotateAnyAllocSite(CallBase &Call, const TargetLibraryInfo *TLI) {
2053  unsigned NumArgs = Call.getNumArgOperands();
2054  ConstantInt *Op0C = dyn_cast<ConstantInt>(Call.getOperand(0));
2055  ConstantInt *Op1C =
2056  (NumArgs == 1) ? nullptr : dyn_cast<ConstantInt>(Call.getOperand(1));
2057  // Bail out if the allocation size is zero (or an invalid alignment of zero
2058  // with aligned_alloc).
2059  if ((Op0C && Op0C->isNullValue()) || (Op1C && Op1C->isNullValue()))
2060  return;
2061 
2062  if (isMallocLikeFn(&Call, TLI) && Op0C) {
2063  if (isOpNewLikeFn(&Call, TLI))
2064  Call.addAttribute(AttributeList::ReturnIndex,
2066  Call.getContext(), Op0C->getZExtValue()));
2067  else
2068  Call.addAttribute(AttributeList::ReturnIndex,
2070  Call.getContext(), Op0C->getZExtValue()));
2071  } else if (isAlignedAllocLikeFn(&Call, TLI) && Op1C) {
2072  Call.addAttribute(AttributeList::ReturnIndex,
2074  Call.getContext(), Op1C->getZExtValue()));
2075  // Add alignment attribute if alignment is a power of two constant.
2076  if (Op0C && Op0C->getValue().ult(llvm::Value::MaximumAlignment)) {
2077  uint64_t AlignmentVal = Op0C->getZExtValue();
2078  if (llvm::isPowerOf2_64(AlignmentVal))
2079  Call.addAttribute(AttributeList::ReturnIndex,
2080  Attribute::getWithAlignment(Call.getContext(),
2081  Align(AlignmentVal)));
2082  }
2083  } else if (isReallocLikeFn(&Call, TLI) && Op1C) {
2084  Call.addAttribute(AttributeList::ReturnIndex,
2086  Call.getContext(), Op1C->getZExtValue()));
2087  } else if (isCallocLikeFn(&Call, TLI) && Op0C && Op1C) {
2088  bool Overflow;
2089  const APInt &N = Op0C->getValue();
2090  APInt Size = N.umul_ov(Op1C->getValue(), Overflow);
2091  if (!Overflow)
2092  Call.addAttribute(AttributeList::ReturnIndex,
2094  Call.getContext(), Size.getZExtValue()));
2095  } else if (isStrdupLikeFn(&Call, TLI)) {
2096  uint64_t Len = GetStringLength(Call.getOperand(0));
2097  if (Len) {
2098  // strdup
2099  if (NumArgs == 1)
2100  Call.addAttribute(AttributeList::ReturnIndex,
2102  Call.getContext(), Len));
2103  // strndup
2104  else if (NumArgs == 2 && Op1C)
2105  Call.addAttribute(
2108  Call.getContext(), std::min(Len, Op1C->getZExtValue() + 1)));
2109  }
2110  }
2111 }
2112 
2113 /// Improvements for call, callbr and invoke instructions.
2114 Instruction *InstCombinerImpl::visitCallBase(CallBase &Call) {
2115  if (isAllocationFn(&Call, &TLI))
2116  annotateAnyAllocSite(Call, &TLI);
2117 
2118  bool Changed = false;
2119 
2120  // Mark any parameters that are known to be non-null with the nonnull
2121  // attribute. This is helpful for inlining calls to functions with null
2122  // checks on their arguments.
2123  SmallVector<unsigned, 4> ArgNos;
2124  unsigned ArgNo = 0;
2125 
2126  for (Value *V : Call.args()) {
2127  if (V->getType()->isPointerTy() &&
2128  !Call.paramHasAttr(ArgNo, Attribute::NonNull) &&
2129  isKnownNonZero(V, DL, 0, &AC, &Call, &DT))
2130  ArgNos.push_back(ArgNo);
2131  ArgNo++;
2132  }
2133 
2134  assert(ArgNo == Call.arg_size() && "sanity check");
2135 
2136  if (!ArgNos.empty()) {
2137  AttributeList AS = Call.getAttributes();
2138  LLVMContext &Ctx = Call.getContext();
2139  AS = AS.addParamAttribute(Ctx, ArgNos,
2140  Attribute::get(Ctx, Attribute::NonNull));
2141  Call.setAttributes(AS);
2142  Changed = true;
2143  }
2144 
2145  // If the callee is a pointer to a function, attempt to move any casts to the
2146  // arguments of the call/callbr/invoke.
2147  Value *Callee = Call.getCalledOperand();
2148  if (!isa<Function>(Callee) && transformConstExprCastCall(Call))
2149  return nullptr;
2150 
2151  if (Function *CalleeF = dyn_cast<Function>(Callee)) {
2152  // Remove the convergent attr on calls when the callee is not convergent.
2153  if (Call.isConvergent() && !CalleeF->isConvergent() &&
2154  !CalleeF->isIntrinsic()) {
2155  LLVM_DEBUG(dbgs() << "Removing convergent attr from instr " << Call
2156  << "\n");
2157  Call.setNotConvergent();
2158  return &Call;
2159  }
2160 
2161  // If the call and callee calling conventions don't match, and neither one
2162  // of the calling conventions is compatible with C calling convention
2163  // this call must be unreachable, as the call is undefined.
2164  if ((CalleeF->getCallingConv() != Call.getCallingConv() &&
2165  !(CalleeF->getCallingConv() == llvm::CallingConv::C &&
2167  !(Call.getCallingConv() == llvm::CallingConv::C &&
2169  // Only do this for calls to a function with a body. A prototype may
2170  // not actually end up matching the implementation's calling conv for a
2171  // variety of reasons (e.g. it may be written in assembly).
2172  !CalleeF->isDeclaration()) {
2173  Instruction *OldCall = &Call;
2174  CreateNonTerminatorUnreachable(OldCall);
2175  // If OldCall does not return void then replaceInstUsesWith undef.
2176  // This allows ValueHandlers and custom metadata to adjust itself.
2177  if (!OldCall->getType()->isVoidTy())
2178  replaceInstUsesWith(*OldCall, UndefValue::get(OldCall->getType()));
2179  if (isa<CallInst>(OldCall))
2180  return eraseInstFromFunction(*OldCall);
2181 
2182  // We cannot remove an invoke or a callbr, because it would change thexi
2183  // CFG, just change the callee to a null pointer.
2184  cast<CallBase>(OldCall)->setCalledFunction(
2185  CalleeF->getFunctionType(),
2186  Constant::getNullValue(CalleeF->getType()));
2187  return nullptr;
2188  }
2189  }
2190 
2191  if ((isa<ConstantPointerNull>(Callee) &&
2192  !NullPointerIsDefined(Call.getFunction())) ||
2193  isa<UndefValue>(Callee)) {
2194  // If Call does not return void then replaceInstUsesWith undef.
2195  // This allows ValueHandlers and custom metadata to adjust itself.
2196  if (!Call.getType()->isVoidTy())
2197  replaceInstUsesWith(Call, UndefValue::get(Call.getType()));
2198 
2199  if (Call.isTerminator()) {
2200  // Can't remove an invoke or callbr because we cannot change the CFG.
2201  return nullptr;
2202  }
2203 
2204  // This instruction is not reachable, just remove it.
2205  CreateNonTerminatorUnreachable(&Call);
2206  return eraseInstFromFunction(Call);
2207  }
2208 
2209  if (IntrinsicInst *II = findInitTrampoline(Callee))
2210  return transformCallThroughTrampoline(Call, *II);
2211 
2212  PointerType *PTy = cast<PointerType>(Callee->getType());
2213  FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
2214  if (FTy->isVarArg()) {
2215  int ix = FTy->getNumParams();
2216  // See if we can optimize any arguments passed through the varargs area of
2217  // the call.
2218  for (auto I = Call.arg_begin() + FTy->getNumParams(), E = Call.arg_end();
2219  I != E; ++I, ++ix) {
2220  CastInst *CI = dyn_cast<CastInst>(*I);
2221  if (CI && isSafeToEliminateVarargsCast(Call, DL, CI, ix)) {
2222  replaceUse(*I, CI->getOperand(0));
2223 
2224  // Update the byval type to match the argument type.
2225  if (Call.isByValArgument(ix)) {
2226  Call.removeParamAttr(ix, Attribute::ByVal);
2227  Call.addParamAttr(
2229  Call.getContext(),
2230  CI->getOperand(0)->getType()->getPointerElementType()));
2231  }
2232  Changed = true;
2233  }
2234  }
2235  }
2236 
2237  if (isa<InlineAsm>(Callee) && !Call.doesNotThrow()) {
2238  // Inline asm calls cannot throw - mark them 'nounwind'.
2239  Call.setDoesNotThrow();
2240  Changed = true;
2241  }
2242 
2243  // Try to optimize the call if possible, we require DataLayout for most of
2244  // this. None of these calls are seen as possibly dead so go ahead and
2245  // delete the instruction now.
2246  if (CallInst *CI = dyn_cast<CallInst>(&Call)) {
2247  Instruction *I = tryOptimizeCall(CI);
2248  // If we changed something return the result, etc. Otherwise let
2249  // the fallthrough check.
2250  if (I) return eraseInstFromFunction(*I);
2251  }
2252 
2253  if (!Call.use_empty() && !Call.isMustTailCall())
2254  if (Value *ReturnedArg = Call.getReturnedArgOperand()) {
2255  Type *CallTy = Call.getType();
2256  Type *RetArgTy = ReturnedArg->getType();
2257  if (RetArgTy->canLosslesslyBitCastTo(CallTy))
2258  return replaceInstUsesWith(
2259  Call, Builder.CreateBitOrPointerCast(ReturnedArg, CallTy));
2260  }
2261 
2262  if (isAllocLikeFn(&Call, &TLI))
2263  return visitAllocSite(Call);
2264 
2265  // Handle intrinsics which can be used in both call and invoke context.
2266  switch (Call.getIntrinsicID()) {
2267  case Intrinsic::experimental_gc_statepoint: {
2268  GCStatepointInst &GCSP = *cast<GCStatepointInst>(&Call);
2269  SmallPtrSet<Value *, 32> LiveGcValues;
2270  for (const GCRelocateInst *Reloc : GCSP.getGCRelocates()) {
2271  GCRelocateInst &GCR = *const_cast<GCRelocateInst *>(Reloc);
2272 
2273  // Remove the relocation if unused.
2274  if (GCR.use_empty()) {
2275  eraseInstFromFunction(GCR);
2276  continue;
2277  }
2278 
2279  Value *DerivedPtr = GCR.getDerivedPtr();
2280  Value *BasePtr = GCR.getBasePtr();
2281 
2282  // Undef is undef, even after relocation.
2283  if (isa<UndefValue>(DerivedPtr) || isa<UndefValue>(BasePtr)) {
2284  replaceInstUsesWith(GCR, UndefValue::get(GCR.getType()));
2285  eraseInstFromFunction(GCR);
2286  continue;
2287  }
2288 
2289  if (auto *PT = dyn_cast<PointerType>(GCR.getType())) {
2290  // The relocation of null will be null for most any collector.
2291  // TODO: provide a hook for this in GCStrategy. There might be some
2292  // weird collector this property does not hold for.
2293  if (isa<ConstantPointerNull>(DerivedPtr)) {
2294  // Use null-pointer of gc_relocate's type to replace it.
2295  replaceInstUsesWith(GCR, ConstantPointerNull::get(PT));
2296  eraseInstFromFunction(GCR);
2297  continue;
2298  }
2299 
2300  // isKnownNonNull -> nonnull attribute
2301  if (!GCR.hasRetAttr(Attribute::NonNull) &&
2302  isKnownNonZero(DerivedPtr, DL, 0, &AC, &Call, &DT)) {
2303  GCR.addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
2304  // We discovered new fact, re-check users.
2305  Worklist.pushUsersToWorkList(GCR);
2306  }
2307  }
2308 
2309  // If we have two copies of the same pointer in the statepoint argument
2310  // list, canonicalize to one. This may let us common gc.relocates.
2311  if (GCR.getBasePtr() == GCR.getDerivedPtr() &&
2312  GCR.getBasePtrIndex() != GCR.getDerivedPtrIndex()) {
2313  auto *OpIntTy = GCR.getOperand(2)->getType();
2314  GCR.setOperand(2, ConstantInt::get(OpIntTy, GCR.getBasePtrIndex()));
2315  }
2316 
2317  // TODO: bitcast(relocate(p)) -> relocate(bitcast(p))
2318  // Canonicalize on the type from the uses to the defs
2319 
2320  // TODO: relocate((gep p, C, C2, ...)) -> gep(relocate(p), C, C2, ...)
2321  LiveGcValues.insert(BasePtr);
2322  LiveGcValues.insert(DerivedPtr);
2323  }
2326  unsigned NumOfGCLives = LiveGcValues.size();
2327  if (!Bundle.hasValue() || NumOfGCLives == Bundle->Inputs.size())
2328  break;
2329  // We can reduce the size of gc live bundle.
2331  std::vector<Value *> NewLiveGc;
2332  for (unsigned I = 0, E = Bundle->Inputs.size(); I < E; ++I) {
2333  Value *V = Bundle->Inputs[I];
2334  if (Val2Idx.count(V))
2335  continue;
2336  if (LiveGcValues.count(V)) {
2337  Val2Idx[V] = NewLiveGc.size();
2338  NewLiveGc.push_back(V);
2339  } else
2340  Val2Idx[V] = NumOfGCLives;
2341  }
2342  // Update all gc.relocates
2343  for (const GCRelocateInst *Reloc : GCSP.getGCRelocates()) {
2344  GCRelocateInst &GCR = *const_cast<GCRelocateInst *>(Reloc);
2345  Value *BasePtr = GCR.getBasePtr();
2346  assert(Val2Idx.count(BasePtr) && Val2Idx[BasePtr] != NumOfGCLives &&
2347  "Missed live gc for base pointer");
2348  auto *OpIntTy1 = GCR.getOperand(1)->getType();
2349  GCR.setOperand(1, ConstantInt::get(OpIntTy1, Val2Idx[BasePtr]));
2350  Value *DerivedPtr = GCR.getDerivedPtr();
2351  assert(Val2Idx.count(DerivedPtr) && Val2Idx[DerivedPtr] != NumOfGCLives &&
2352  "Missed live gc for derived pointer");
2353  auto *OpIntTy2 = GCR.getOperand(2)->getType();
2354  GCR.setOperand(2, ConstantInt::get(OpIntTy2, Val2Idx[DerivedPtr]));
2355  }
2356  // Create new statepoint instruction.
2357  OperandBundleDef NewBundle("gc-live", NewLiveGc);
2358  return CallBase::Create(&Call, NewBundle);
2359  }
2360  default: { break; }
2361  }
2362 
2363  return Changed ? &Call : nullptr;
2364 }
2365 
2366 /// If the callee is a constexpr cast of a function, attempt to move the cast to
2367 /// the arguments of the call/callbr/invoke.
2368 bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {
2369  auto *Callee =
2370  dyn_cast<Function>(Call.getCalledOperand()->stripPointerCasts());
2371  if (!Callee)
2372  return false;
2373 
2374  // If this is a call to a thunk function, don't remove the cast. Thunks are
2375  // used to transparently forward all incoming parameters and outgoing return
2376  // values, so it's important to leave the cast in place.
2377  if (Callee->hasFnAttribute("thunk"))
2378  return false;
2379 
2380  // If this is a musttail call, the callee's prototype must match the caller's
2381  // prototype with the exception of pointee types. The code below doesn't
2382  // implement that, so we can't do this transform.
2383  // TODO: Do the transform if it only requires adding pointer casts.
2384  if (Call.isMustTailCall())
2385  return false;
2386 
2387  Instruction *Caller = &Call;
2388  const AttributeList &CallerPAL = Call.getAttributes();
2389 
2390  // Okay, this is a cast from a function to a different type. Unless doing so
2391  // would cause a type conversion of one of our arguments, change this call to
2392  // be a direct call with arguments casted to the appropriate types.
2394  Type *OldRetTy = Caller->getType();
2395  Type *NewRetTy = FT->getReturnType();
2396 
2397  // Check to see if we are changing the return type...
2398  if (OldRetTy != NewRetTy) {
2399 
2400  if (NewRetTy->isStructTy())
2401  return false; // TODO: Handle multiple return values.
2402 
2403  if (!CastInst::isBitOrNoopPointerCastable(NewRetTy, OldRetTy, DL)) {
2404  if (Callee->isDeclaration())
2405  return false; // Cannot transform this return value.
2406 
2407  if (!Caller->use_empty() &&
2408  // void -> non-void is handled specially
2409  !NewRetTy->isVoidTy())
2410  return false; // Cannot transform this return value.
2411  }
2412 
2413  if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
2414  AttrBuilder RAttrs(CallerPAL, AttributeList::ReturnIndex);
2415  if (RAttrs.overlaps(AttributeFuncs::typeIncompatible(NewRetTy)))
2416  return false; // Attribute not compatible with transformed value.
2417  }
2418 
2419  // If the callbase is an invoke/callbr instruction, and the return value is
2420  // used by a PHI node in a successor, we cannot change the return type of
2421  // the call because there is no place to put the cast instruction (without
2422  // breaking the critical edge). Bail out in this case.
2423  if (!Caller->use_empty()) {
2424  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
2425  for (User *U : II->users())
2426  if (PHINode *PN = dyn_cast<PHINode>(U))
2427  if (PN->getParent() == II->getNormalDest() ||
2428  PN->getParent() == II->getUnwindDest())
2429  return false;
2430  // FIXME: Be conservative for callbr to avoid a quadratic search.
2431  if (isa<CallBrInst>(Caller))
2432  return false;
2433  }
2434  }
2435 
2436  unsigned NumActualArgs = Call.arg_size();
2437  unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
2438 
2439  // Prevent us turning:
2440  // declare void @takes_i32_inalloca(i32* inalloca)
2441  // call void bitcast (void (i32*)* @takes_i32_inalloca to void (i32)*)(i32 0)
2442  //
2443  // into:
2444  // call void @takes_i32_inalloca(i32* null)
2445  //
2446  // Similarly, avoid folding away bitcasts of byval calls.
2447  if (Callee->getAttributes().hasAttrSomewhere(Attribute::InAlloca) ||
2448  Callee->getAttributes().hasAttrSomewhere(Attribute::Preallocated) ||
2449  Callee->getAttributes().hasAttrSomewhere(Attribute::ByVal))
2450  return false;
2451 
2452  auto AI = Call.arg_begin();
2453  for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
2454  Type *ParamTy = FT->getParamType(i);
2455  Type *ActTy = (*AI)->getType();
2456 
2457  if (!CastInst::isBitOrNoopPointerCastable(ActTy, ParamTy, DL))
2458  return false; // Cannot transform this parameter value.
2459 
2460  if (AttrBuilder(CallerPAL.getParamAttributes(i))
2462  return false; // Attribute not compatible with transformed value.
2463 
2464  if (Call.isInAllocaArgument(i))
2465  return false; // Cannot transform to and from inalloca.
2466 
2467  if (CallerPAL.hasParamAttribute(i, Attribute::SwiftError))
2468  return false;
2469 
2470  // If the parameter is passed as a byval argument, then we have to have a
2471  // sized type and the sized type has to have the same size as the old type.
2472  if (ParamTy != ActTy && CallerPAL.hasParamAttribute(i, Attribute::ByVal)) {
2473  PointerType *ParamPTy = dyn_cast<PointerType>(ParamTy);
2474  if (!ParamPTy || !ParamPTy->getElementType()->isSized())
2475  return false;
2476 
2477  Type *CurElTy = Call.getParamByValType(i);
2478  if (DL.getTypeAllocSize(CurElTy) !=
2479  DL.getTypeAllocSize(ParamPTy->getElementType()))
2480  return false;
2481  }
2482  }
2483 
2484  if (Callee->isDeclaration()) {
2485  // Do not delete arguments unless we have a function body.
2486  if (FT->getNumParams() < NumActualArgs && !FT->isVarArg())
2487  return false;
2488 
2489  // If the callee is just a declaration, don't change the varargsness of the
2490  // call. We don't want to introduce a varargs call where one doesn't
2491  // already exist.
2492  PointerType *APTy = cast<PointerType>(Call.getCalledOperand()->getType());
2493  if (FT->isVarArg()!=cast<FunctionType>(APTy->getElementType())->isVarArg())
2494  return false;
2495 
2496  // If both the callee and the cast type are varargs, we still have to make
2497  // sure the number of fixed parameters are the same or we have the same
2498  // ABI issues as if we introduce a varargs call.
2499  if (FT->isVarArg() &&
2500  cast<FunctionType>(APTy->getElementType())->isVarArg() &&
2501  FT->getNumParams() !=
2502  cast<FunctionType>(APTy->getElementType())->getNumParams())
2503  return false;
2504  }
2505 
2506  if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
2507  !CallerPAL.isEmpty()) {
2508  // In this case we have more arguments than the new function type, but we
2509  // won't be dropping them. Check that these extra arguments have attributes
2510  // that are compatible with being a vararg call argument.
2511  unsigned SRetIdx;
2512  if (CallerPAL.hasAttrSomewhere(Attribute::StructRet, &SRetIdx) &&
2513  SRetIdx > FT->getNumParams())
2514  return false;
2515  }
2516 
2517  // Okay, we decided that this is a safe thing to do: go ahead and start
2518  // inserting cast instructions as necessary.
2521  Args.reserve(NumActualArgs);
2522  ArgAttrs.reserve(NumActualArgs);
2523 
2524  // Get any return attributes.
2525  AttrBuilder RAttrs(CallerPAL, AttributeList::ReturnIndex);
2526 
2527  // If the return value is not being used, the type may not be compatible
2528  // with the existing attributes. Wipe out any problematic attributes.
2529  RAttrs.remove(AttributeFuncs::typeIncompatible(NewRetTy));
2530 
2531  LLVMContext &Ctx = Call.getContext();
2532  AI = Call.arg_begin();
2533  for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
2534  Type *ParamTy = FT->getParamType(i);
2535 
2536  Value *NewArg = *AI;
2537  if ((*AI)->getType() != ParamTy)
2538  NewArg = Builder.CreateBitOrPointerCast(*AI, ParamTy);
2539  Args.push_back(NewArg);
2540 
2541  // Add any parameter attributes.
2542  if (CallerPAL.hasParamAttribute(i, Attribute::ByVal)) {
2543  AttrBuilder AB(CallerPAL.getParamAttributes(i));
2544  AB.addByValAttr(NewArg->getType()->getPointerElementType());
2545  ArgAttrs.push_back(AttributeSet::get(Ctx, AB));
2546  } else
2547  ArgAttrs.push_back(CallerPAL.getParamAttributes(i));
2548  }
2549 
2550  // If the function takes more arguments than the call was taking, add them
2551  // now.
2552  for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) {
2553  Args.push_back(Constant::getNullValue(FT->getParamType(i)));
2554  ArgAttrs.push_back(AttributeSet());
2555  }
2556 
2557  // If we are removing arguments to the function, emit an obnoxious warning.
2558  if (FT->getNumParams() < NumActualArgs) {
2559  // TODO: if (!FT->isVarArg()) this call may be unreachable. PR14722
2560  if (FT->isVarArg()) {
2561  // Add all of the arguments in their promoted form to the arg list.
2562  for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
2563  Type *PTy = getPromotedType((*AI)->getType());
2564  Value *NewArg = *AI;
2565  if (PTy != (*AI)->getType()) {
2566  // Must promote to pass through va_arg area!
2567  Instruction::CastOps opcode =
2568  CastInst::getCastOpcode(*AI, false, PTy, false);
2569  NewArg = Builder.CreateCast(opcode, *AI, PTy);
2570  }
2571  Args.push_back(NewArg);
2572 
2573  // Add any parameter attributes.
2574  ArgAttrs.push_back(CallerPAL.getParamAttributes(i));
2575  }
2576  }
2577  }
2578 
2579  AttributeSet FnAttrs = CallerPAL.getFnAttributes();
2580 
2581  if (NewRetTy->isVoidTy())
2582  Caller->setName(""); // Void type should not have a name.
2583 
2584  assert((ArgAttrs.size() == FT->getNumParams() || FT->isVarArg()) &&
2585  "missing argument attributes");
2586  AttributeList NewCallerPAL = AttributeList::get(
2587  Ctx, FnAttrs, AttributeSet::get(Ctx, RAttrs), ArgAttrs);
2588 
2590  Call.getOperandBundlesAsDefs(OpBundles);
2591 
2592  CallBase *NewCall;
2593  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
2594  NewCall = Builder.CreateInvoke(Callee, II->getNormalDest(),
2595  II->getUnwindDest(), Args, OpBundles);
2596  } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(Caller)) {
2597  NewCall = Builder.CreateCallBr(Callee, CBI->getDefaultDest(),
2598  CBI->getIndirectDests(), Args, OpBundles);
2599  } else {
2600  NewCall = Builder.CreateCall(Callee, Args, OpBundles);
2601  cast<CallInst>(NewCall)->setTailCallKind(
2602  cast<CallInst>(Caller)->getTailCallKind());
2603  }
2604  NewCall->takeName(Caller);
2605  NewCall->setCallingConv(Call.getCallingConv());
2606  NewCall->setAttributes(NewCallerPAL);
2607 
2608  // Preserve prof metadata if any.
2609  NewCall->copyMetadata(*Caller, {LLVMContext::MD_prof});
2610 
2611  // Insert a cast of the return type as necessary.
2612  Instruction *NC = NewCall;
2613  Value *NV = NC;
2614  if (OldRetTy != NV->getType() && !Caller->use_empty()) {
2615  if (!NV->getType()->isVoidTy()) {
2616  NV = NC = CastInst::CreateBitOrPointerCast(NC, OldRetTy);
2617  NC->setDebugLoc(Caller->getDebugLoc());
2618 
2619  // If this is an invoke/callbr instruction, we should insert it after the
2620  // first non-phi instruction in the normal successor block.
2621  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
2622  BasicBlock::iterator I = II->getNormalDest()->getFirstInsertionPt();
2623  InsertNewInstBefore(NC, *I);
2624  } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(Caller)) {
2625  BasicBlock::iterator I = CBI->getDefaultDest()->getFirstInsertionPt();
2626  InsertNewInstBefore(NC, *I);
2627  } else {
2628  // Otherwise, it's a call, just insert cast right after the call.
2629  InsertNewInstBefore(NC, *Caller);
2630  }
2631  Worklist.pushUsersToWorkList(*Caller);
2632  } else {
2633  NV = UndefValue::get(Caller->getType());
2634  }
2635  }
2636 
2637  if (!Caller->use_empty())
2638  replaceInstUsesWith(*Caller, NV);
2639  else if (Caller->hasValueHandle()) {
2640  if (OldRetTy == NV->getType())
2642  else
2643  // We cannot call ValueIsRAUWd with a different type, and the
2644  // actual tracked value will disappear.
2646  }
2647 
2648  eraseInstFromFunction(*Caller);
2649  return true;
2650 }
2651 
2652 /// Turn a call to a function created by init_trampoline / adjust_trampoline
2653 /// intrinsic pair into a direct call to the underlying function.
2654 Instruction *
2655 InstCombinerImpl::transformCallThroughTrampoline(CallBase &Call,
2656  IntrinsicInst &Tramp) {
2657  Value *Callee = Call.getCalledOperand();
2658  Type *CalleeTy = Callee->getType();
2659  FunctionType *FTy = Call.getFunctionType();
2660  AttributeList Attrs = Call.getAttributes();
2661 
2662  // If the call already has the 'nest' attribute somewhere then give up -
2663  // otherwise 'nest' would occur twice after splicing in the chain.
2664  if (Attrs.hasAttrSomewhere(Attribute::Nest))
2665  return nullptr;
2666 
2667  Function *NestF = cast<Function>(Tramp.getArgOperand(1)->stripPointerCasts());
2668  FunctionType *NestFTy = NestF->getFunctionType();
2669 
2670  AttributeList NestAttrs = NestF->getAttributes();
2671  if (!NestAttrs.isEmpty()) {
2672  unsigned NestArgNo = 0;
2673  Type *NestTy = nullptr;
2674  AttributeSet NestAttr;
2675 
2676  // Look for a parameter marked with the 'nest' attribute.
2677  for (FunctionType::param_iterator I = NestFTy->param_begin(),
2678  E = NestFTy->param_end();
2679  I != E; ++NestArgNo, ++I) {
2680  AttributeSet AS = NestAttrs.getParamAttributes(NestArgNo);
2681  if (AS.hasAttribute(Attribute::Nest)) {
2682  // Record the parameter type and any other attributes.
2683  NestTy = *I;
2684  NestAttr = AS;
2685  break;
2686  }
2687  }
2688 
2689  if (NestTy) {
2690  std::vector<Value*> NewArgs;
2691  std::vector<AttributeSet> NewArgAttrs;
2692  NewArgs.reserve(Call.arg_size() + 1);
2693  NewArgAttrs.reserve(Call.arg_size());
2694 
2695  // Insert the nest argument into the call argument list, which may
2696  // mean appending it. Likewise for attributes.
2697 
2698  {
2699  unsigned ArgNo = 0;
2700  auto I = Call.arg_begin(), E = Call.arg_end();
2701  do {
2702  if (ArgNo == NestArgNo) {
2703  // Add the chain argument and attributes.
2704  Value *NestVal = Tramp.getArgOperand(2);
2705  if (NestVal->getType() != NestTy)
2706  NestVal = Builder.CreateBitCast(NestVal, NestTy, "nest");
2707  NewArgs.push_back(NestVal);
2708  NewArgAttrs.push_back(NestAttr);
2709  }
2710 
2711  if (I == E)
2712  break;
2713 
2714  // Add the original argument and attributes.
2715  NewArgs.push_back(*I);
2716  NewArgAttrs.push_back(Attrs.getParamAttributes(ArgNo));
2717 
2718  ++ArgNo;
2719  ++I;
2720  } while (true);
2721  }
2722 
2723  // The trampoline may have been bitcast to a bogus type (FTy).
2724  // Handle this by synthesizing a new function type, equal to FTy
2725  // with the chain parameter inserted.
2726 
2727  std::vector<Type*> NewTypes;
2728  NewTypes.reserve(FTy->getNumParams()+1);
2729 
2730  // Insert the chain's type into the list of parameter types, which may
2731  // mean appending it.
2732  {
2733  unsigned ArgNo = 0;
2735  E = FTy->param_end();
2736 
2737  do {
2738  if (ArgNo == NestArgNo)
2739  // Add the chain's type.
2740  NewTypes.push_back(NestTy);
2741 
2742  if (I == E)
2743  break;
2744 
2745  // Add the original type.
2746  NewTypes.push_back(*I);
2747 
2748  ++ArgNo;
2749  ++I;
2750  } while (true);
2751  }
2752 
2753  // Replace the trampoline call with a direct call. Let the generic
2754  // code sort out any function type mismatches.
2755  FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes,
2756  FTy->isVarArg());
2757  Constant *NewCallee =
2758  NestF->getType() == PointerType::getUnqual(NewFTy) ?
2759  NestF : ConstantExpr::getBitCast(NestF,
2760  PointerType::getUnqual(NewFTy));
2761  AttributeList NewPAL =
2762  AttributeList::get(FTy->getContext(), Attrs.getFnAttributes(),
2763  Attrs.getRetAttributes(), NewArgAttrs);
2764 
2766  Call.getOperandBundlesAsDefs(OpBundles);
2767 
2768  Instruction *NewCaller;
2769  if (InvokeInst *II = dyn_cast<InvokeInst>(&Call)) {
2770  NewCaller = InvokeInst::Create(NewFTy, NewCallee,
2771  II->getNormalDest(), II->getUnwindDest(),
2772  NewArgs, OpBundles);
2773  cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
2774  cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
2775  } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(&Call)) {
2776  NewCaller =
2777  CallBrInst::Create(NewFTy, NewCallee, CBI->getDefaultDest(),
2778  CBI->getIndirectDests(), NewArgs, OpBundles);
2779  cast<CallBrInst>(NewCaller)->setCallingConv(CBI->getCallingConv());
2780  cast<CallBrInst>(NewCaller)->setAttributes(NewPAL);
2781  } else {
2782  NewCaller = CallInst::Create(NewFTy, NewCallee, NewArgs, OpBundles);
2783  cast<CallInst>(NewCaller)->setTailCallKind(
2784  cast<CallInst>(Call).getTailCallKind());
2785  cast<CallInst>(NewCaller)->setCallingConv(
2786  cast<CallInst>(Call).getCallingConv());
2787  cast<CallInst>(NewCaller)->setAttributes(NewPAL);
2788  }
2789  NewCaller->setDebugLoc(Call.getDebugLoc());
2790 
2791  return NewCaller;
2792  }
2793  }
2794 
2795  // Replace the trampoline call with a direct call. Since there is no 'nest'
2796  // parameter, there is no need to adjust the argument list. Let the generic
2797  // code sort out any function type mismatches.
2798  Constant *NewCallee = ConstantExpr::getBitCast(NestF, CalleeTy);
2799  Call.setCalledFunction(FTy, NewCallee);
2800  return &Call;
2801 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::RetainedKnowledge::WasOn
Value * WasOn
Definition: AssumeBundleQueries.h:104
i
i
Definition: README.txt:29
llvm::CallBase::getNumOperandBundles
unsigned getNumOperandBundles() const
Return the number of operand bundles associated with this User.
Definition: InstrTypes.h:1898
FloatingPointMode.h
AssumptionCache.h
llvm::BinaryOpIntrinsic::getBinaryOp
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
Definition: IntrinsicInst.cpp:377
Attrs
Function Attrs
Definition: README_ALTIVEC.txt:215
llvm::Type::isSized
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:272
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:100
MathExtras.h
llvm::APInt::sadd_ov
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1955
llvm
Definition: AllocatorList.h:23
llvm::isCallocLikeFn
bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates zero-filled memory (such as...
Definition: MemoryBuiltins.cpp:290
llvm::maskIsAllOneOrUndef
bool maskIsAllOneOrUndef(Value *Mask)
Given a mask vector of i1, Return true if all of the elements of this predicate mask are known to be ...
Definition: VectorUtils.cpp:895
llvm::CallBase::getOperandBundlesAsDefs
void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.
Definition: Instructions.cpp:361
llvm::InvokeInst::Create
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, Instruction *InsertBefore=nullptr)
Definition: Instructions.h:3730
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::Instruction::getModule
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:66
llvm::Value::dropDroppableUse
static void dropDroppableUse(Use &U)
Remove the droppable use U.
Definition: Value.cpp:206
llvm::CmpInst::ICMP_EQ
@ ICMP_EQ
equal
Definition: InstrTypes.h:743
llvm::OverflowResult::NeverOverflows
@ NeverOverflows
Never overflows.
llvm::ConstantExpr::getNot
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2658
llvm::CallBase::getOperandBundle
Optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Definition: InstrTypes.h:1978
Optional.h
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
llvm::ISD::OR
@ OR
Definition: ISDOpcodes.h:618
llvm::Value::hasOneUse
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:447
llvm::AttrBuilder::overlaps
bool overlaps(const AttrBuilder &B) const
Return true if the builder has any attribute that's in the specified builder.
Definition: Attributes.cpp:1977
InstCombiner.h
llvm::Intrinsic::getDeclaration
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:1291
Metadata.h
llvm::AttributeList::hasAttrSomewhere
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.
Definition: Attributes.cpp:1564
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:722
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:90
llvm::isSafeToSpeculativelyExecute
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
Definition: ValueTracking.cpp:4523
llvm::ConstantInt::getType
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
Definition: Constants.h:171
llvm::InstCombiner::getDominatorTree
DominatorTree & getDominatorTree() const
Definition: InstCombiner.h:369
IntrinsicInst.h
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:229
ceil
We have fiadd patterns now but the followings have the same cost and complexity We need a way to specify the later is more profitable def def The FP stackifier should handle simple permutates to reduce number of shuffle e g ceil
Definition: README-FPStack.txt:54
AtomicOrdering.h
llvm::ConstantExpr::getZExt
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2097
llvm::Instruction::getNextNonDebugInstruction
const Instruction * getNextNonDebugInstruction(bool SkipPseudoOp=false) const
Return a pointer to the next non-debug instruction in the same basic block as 'this',...
Definition: Instruction.cpp:699
Loads.h
GuardWideningWindow
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"))
llvm::PointerType::getElementType
Type * getElementType() const
Definition: DerivedTypes.h:653
llvm::Function
Definition: Function.h:61
llvm::ConstantStruct::get
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1312
llvm::BinaryOperator::CreateNot
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Definition: Instructions.cpp:2605
llvm::IntrinsicInst::getIntrinsicID
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:52
llvm::SelectPatternResult::Flavor
SelectPatternFlavor Flavor
Definition: ValueTracking.h:681
llvm::CallBase::setCalledFunction
void setCalledFunction(Function *Fn)
Sets the function called, including updating the function type.
Definition: InstrTypes.h:1432
llvm::possiblyDemandedEltsInMask
APInt possiblyDemandedEltsInMask(Value *Mask)
Given a mask vector of the form <Y x i1>, return an APInt (of bitwidth Y) for each lane which may be ...
Definition: VectorUtils.cpp:923
llvm::PointerType::get
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:693
minimum
Should compile r2 movcc movcs str strb mov lr r1 movcs movcc mov lr r1 str mov mov cmp r1 movlo r2 str bx lr r0 mov mov cmp r0 movhs r2 mov r1 bx lr Some of the NEON intrinsics may be appropriate for more general either as target independent intrinsics or perhaps elsewhere in the ARM backend Some of them may also be lowered to target independent and perhaps some new SDNodes could be added For minimum
Definition: README.txt:489
llvm::KnownBits::Zero
APInt Zero
Definition: KnownBits.h:24
C1
instcombine should handle this C2 when C1
Definition: README.txt:263
llvm::Attribute::get
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:92
llvm::ConstantInt::getValue
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:131
llvm::BinaryOpIntrinsic::getRHS
Value * getRHS() const
Definition: IntrinsicInst.h:528
llvm::ConstantExpr::getSExt
static Constant * getSExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2083
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::InstCombiner::Builder
BuilderTy & Builder
Definition: InstCombiner.h:56
Statistic.h
llvm::AttributeList::addParamAttribute
LLVM_NODISCARD AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const
Add an argument attribute to the list.
Definition: Attributes.h:457
llvm::ConstantExpr::getICmp
static Constant * getICmp(unsigned short pred, Constant *LHS, Constant *RHS, bool OnlyIfReduced=false)
get* - Return some common constants without having to specify the full Instruction::OPCODE identifier...
Definition: Constants.cpp:2476
llvm::BinaryOperator::CreateFDivFMF
static BinaryOperator * CreateFDivFMF(Value *V1, Value *V2, Instruction *FMFSource, const Twine &Name="")
Definition: InstrTypes.h:275
llvm::PatternMatch::m_Add
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
Definition: PatternMatch.h:959
llvm::BinaryOpIntrinsic::getLHS
Value * getLHS() const
Definition: IntrinsicInst.h:527
ErrorHandling.h
llvm::CmpInst::makeCmpResultType
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:1034
llvm::Type::getPointerAddressSpace
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:693
llvm::IRBuilder< TargetFolder, IRBuilderCallbackInserter >
llvm::InsertValueInst::Create
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:2469
llvm::X86::SecondMacroFusionInstKind::AB
@ AB
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::CastInst::Create
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's ...
Definition: Instructions.cpp:2945
llvm::CmpInst::ICMP_NE
@ ICMP_NE
not equal
Definition: InstrTypes.h:744
llvm::ConstantExpr::getBitCast
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2207
llvm::FunctionType::get
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:328
llvm::SignBitMustBeZero
bool SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI)
Return true if we can prove that the specified FP value's sign bit is 0.
Definition: ValueTracking.cpp:3589
ValueTracking.h
llvm::isAssumeWithEmptyBundle
bool isAssumeWithEmptyBundle(AssumeInst &Assume)
Return true iff the operand bundles of the provided llvm.assume doesn't contain any valuable informat...
Definition: AssumeBundleQueries.cpp:125
Local.h
llvm::AttributeList::get
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute >> Attrs)
Create an AttributeList with the specified parameters in it.
Definition: Attributes.cpp:1175
llvm::X86AS::SS
@ SS
Definition: X86.h:184
llvm::isAllocLikeFn
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,...
Definition: MemoryBuiltins.cpp:305
llvm::PatternMatch::m_APFloat
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:243
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
llvm::minimum
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 minimum semantics.
Definition: APFloat.h:1308
llvm::PatternMatch::m_BitReverse
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
Definition: PatternMatch.h:2107
llvm::AttributeFuncs::typeIncompatible
AttrBuilder typeIncompatible(Type *Ty)
Which attributes cannot be applied to a type.
Definition: Attributes.cpp:2039
llvm::isKnownNonZero
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.
Definition: ValueTracking.cpp:335
llvm::ConstantInt::getLimitedValue
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:242
APInt.h
llvm::IRBuilderBase::CreateStripInvariantGroup
Value * CreateStripInvariantGroup(Value *Ptr)
Create a strip.invariant.group intrinsic call.
Definition: IRBuilder.cpp:987
llvm::PatternMatch::m_NegZeroFP
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
Definition: PatternMatch.h:670
llvm::InstCombinerImpl::visitInvokeInst
Instruction * visitInvokeInst(InvokeInst &II)
Definition: InstCombineCalls.cpp:1912
llvm::BitmaskEnumDetail::Underlying
std::underlying_type_t< E > Underlying(E Val)
Check that Val is in range for E, and return Val cast to E's underlying type.
Definition: BitmaskEnum.h:90
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
llvm::OverflowResult::AlwaysOverflowsLow
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
llvm::APInt::getBitWidth
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1581
llvm::InstCombinerImpl::visitCallInst
Instruction * visitCallInst(CallInst &CI)
CallInst simplification.
Definition: InstCombineCalls.cpp:698
MemoryBuiltins.h
llvm::AttributeList
Definition: Attributes.h:375
llvm::OperandBundleDefT
A container for an operand bundle being viewed as a set of values rather than a set of uses.
Definition: InstrTypes.h:1117
llvm::CallBase::getFunctionType
FunctionType * getFunctionType() const
Definition: InstrTypes.h:1244
llvm::simplifyRetainedKnowledge
RetainedKnowledge simplifyRetainedKnowledge(AssumeInst *Assume, RetainedKnowledge RK, AssumptionCache *AC, DominatorTree *DT)
canonicalize the RetainedKnowledge RK.
Definition: AssumeBundleBuilder.cpp:314
llvm::FastMathFlags::noSignedZeros
bool noSignedZeros() const
Definition: Operator.h:208
llvm::GCRelocateInst::getBasePtrIndex
unsigned getBasePtrIndex() const
The index into the associate statepoint's argument list which contains the base pointer of the pointe...
Definition: IntrinsicInst.h:1242
llvm::InstCombinerImpl::replaceInstUsesWith
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Definition: InstCombineInternal.h:385
llvm::Function::hasFnAttribute
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:345
llvm::Optional< bool >
foldCttzCtlz
static Instruction * foldCttzCtlz(IntrinsicInst &II, InstCombinerImpl &IC)
Definition: InstCombineCalls.cpp:421
llvm::DenseMapBase::count
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:145
llvm::CastInst::getCastOpcode
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.
Definition: Instructions.cpp:3227
llvm::FenceInst
An instruction for ordering other memory operations.
Definition: Instructions.h:444
llvm::ConstantAsMetadata::get
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:419
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:449
llvm::ore::NV
DiagnosticInfoOptimizationBase::Argument NV
Definition: OptimizationRemarkEmitter.h:128
llvm::Instruction::copyMetadata
void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
Definition: Instruction.cpp:800
llvm::FunctionType::getNumParams
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:138
llvm::CallBase::getNumArgOperands
unsigned getNumArgOperands() const
Definition: InstrTypes.h:1339
llvm::CastInst::CreateIntegerCast
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.
Definition: Instructions.cpp:3111
STLExtras.h
llvm::isPowerOf2_32
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:492
llvm::matchSelectPattern
SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
Definition: ValueTracking.cpp:6079
llvm::PatternMatch::m_c_And
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
Definition: PatternMatch.h:2185
llvm::BinaryOperator::CreateNeg
NUW NUW NUW NUW Exact static Exact BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
Definition: Instructions.cpp:2565
llvm::CallBase::setDoesNotThrow
void setDoesNotThrow()
Definition: InstrTypes.h:1848
findInitTrampolineFromBB
static IntrinsicInst * findInitTrampolineFromBB(IntrinsicInst *AdjustTramp, Value *TrampMem)
Definition: InstCombineCalls.cpp:2015
llvm::PatternMatch::m_Not
BinaryOp_match< ValTy, cst_pred_ty< is_all_ones >, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
Definition: PatternMatch.h:2223
llvm::SelectPatternFlavor
SelectPatternFlavor
Specific patterns of select instructions we can match.
Definition: ValueTracking.h:657
llvm::CallBrInst::Create
static CallBrInst * Create(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args, const Twine &NameStr, Instruction *InsertBefore=nullptr)
Definition: Instructions.h:3938
llvm::SimplifyFMAFMul
Value * SimplifyFMAFMul(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for the multiplication of a FMA, fold the result or return null.
Definition: InstructionSimplify.cpp:5042
llvm::BitmaskEnumDetail::Mask
std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:80
llvm::InstCombinerImpl::SimplifyAnyMemTransfer
Instruction * SimplifyAnyMemTransfer(AnyMemTransferInst *MI)
Definition: InstCombineCalls.cpp:107
llvm::BasicBlock::rend
reverse_iterator rend()
Definition: BasicBlock.h:303
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:204
llvm::PatternMatch::m_Deferred
deferredval_ty< Value > m_Deferred(Value *const &V)
A commutative-friendly version of m_Specific().
Definition: PatternMatch.h:771
llvm::isAllocationFn
bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates or reallocates memory (eith...
Definition: MemoryBuiltins.cpp:241
llvm::APIntOps::umin
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition: APInt.h:2183
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:122
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1198
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::FunctionType::isVarArg
bool isVarArg() const
Definition: DerivedTypes.h:122
llvm::Instruction::setMetadata
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1330
KnownBits.h
llvm::KnownBits::countMinTrailingZeros
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:219
floor
We have fiadd patterns now but the followings have the same cost and complexity We need a way to specify the later is more profitable def def The FP stackifier should handle simple permutates to reduce number of shuffle e g floor
Definition: README-FPStack.txt:54
llvm::GCRelocateInst::getDerivedPtrIndex
unsigned getDerivedPtrIndex() const
The index into the associate statepoint's argument list which contains the pointer whose relocation t...
Definition: IntrinsicInst.h:1248
llvm::UndefMaskElem
constexpr int UndefMaskElem
Definition: Instructions.h:1974
AliasAnalysis.h
llvm::PatternMatch::m_OneUse
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:67
llvm::Optional::hasValue
constexpr bool hasValue() const
Definition: Optional.h:286
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
llvm::ConstantExpr::getSub
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2675
llvm::InstCombinerImpl::eraseInstFromFunction
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Definition: InstCombineInternal.h:443
llvm::GCStatepointInst
Represents a gc.statepoint intrinsic call.
Definition: Statepoint.h:63
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:205
llvm::User::getOperandUse
const Use & getOperandUse(unsigned i) const
Definition: User.h:182
Instruction.h
llvm::PatternMatch::m_APInt
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:226
llvm::SPF_NABS
@ SPF_NABS
Absolute value.
Definition: ValueTracking.h:666
CommandLine.h
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:77
llvm::KnownBits::isNonNegative
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:99
llvm::Instruction::getOpcode
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:160
EnableKnowledgeRetention
cl::opt< bool > EnableKnowledgeRetention
enable preservation of attributes in assume like: call void @llvm.assume(i1 true) [ "nonnull"(i32* PT...
llvm::MinAlign
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:673
llvm::APInt::isNonNegative
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:369
llvm::isValidAssumeForContext
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,...
Definition: ValueTracking.cpp:546
llvm::SelectInst::Create
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
Definition: Instructions.h:1746
InstCombineInternal.h
llvm::PatternMatch::m_Select
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
Definition: PatternMatch.h:1423
llvm::SimplifyCall
Value * SimplifyCall(CallBase *Call, const SimplifyQuery &Q)
Given a callsite, fold the result or return null.
Definition: InstructionSimplify.cpp:5813
llvm::Constant::containsConstantExpression
bool containsConstantExpression() const
Return true if this is a fixed width vector constant that includes any constant expressions.
Definition: Constants.cpp:337
Constants.h
llvm::maximum
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 maximum semantics.
Definition: APFloat.h:1321
llvm::PatternMatch::match
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
llvm::Constant::isNullValue
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:86
llvm::OverflowResult::MayOverflow
@ MayOverflow
May or may not overflow.
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::User
Definition: User.h:44
Intr
unsigned Intr
Definition: AMDGPUBaseInfo.cpp:1927
llvm::getKnownAlignment
Align 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:224
Intrinsics.h
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::FunctionType::param_end
param_iterator param_end() const
Definition: DerivedTypes.h:128
llvm::AttributeSet::hasAttribute
bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists in this set.
Definition: Attributes.cpp:772
Twine.h
llvm::AttributeList::getFnAttributes
AttributeSet getFnAttributes() const
The function attributes are returned.
Definition: Attributes.cpp:1534
round
static uint64_t round(uint64_t Acc, uint64_t Input)
Definition: xxhash.cpp:57
InstrTypes.h
llvm::CallBase::getCalledFunction
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1396
llvm::CallBase::setAttributes
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1476
llvm::BinaryOperator::CreateNSW
static BinaryOperator * CreateNSW(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name="")
Definition: InstrTypes.h:286
llvm::BasicBlock::begin
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:296
llvm::KnownBits::One
APInt One
Definition: KnownBits.h:25
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
llvm::PatternMatch::m_PosZeroFP
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
Definition: PatternMatch.h:661
llvm::LoadInst::setVolatile
void setVolatile(bool V)
Specify whether this is a volatile load or not.
Definition: Instructions.h:214
llvm::CallInst::Create
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:1493
SI
@ SI
Definition: SIInstrInfo.cpp:7342
AssumeBundleQueries.h
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
llvm::AttributeList::getParamAttributes
AttributeSet getParamAttributes(unsigned ArgNo) const
The attributes for the argument or parameter at the given index are returned.
Definition: Attributes.cpp:1526
llvm::PatternMatch::m_Instruction
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:704
AssumeBundleBuilder.h
llvm::MaybeAlign
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:119
llvm::PatternMatch::m_ZExt
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
Definition: PatternMatch.h:1590
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::IntegerType
Class to represent integer types.
Definition: DerivedTypes.h:40
llvm::Instruction::CastOps
CastOps
Definition: Instruction.h:782
llvm::PatternMatch::m_FNeg
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
Definition: PatternMatch.h:1014
llvm::SaturatingInst
Represents a saturating add/sub intrinsic.
Definition: IntrinsicInst.h:562
llvm::Constant::getAllOnesValue
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:405
llvm::pdb::PDB_SymType::Caller
@ Caller
llvm::PatternMatch::m_ConstantInt
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:101
llvm::FunctionType::param_begin
param_iterator param_begin() const
Definition: DerivedTypes.h:127
llvm::Instruction
Definition: Instruction.h:45
llvm::Type::getScalarSizeInBits
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition: Type.cpp:154
llvm::isDereferenceablePointer
bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if this is always a dereferenceable pointer.
Definition: Loads.cpp:234
llvm::RetainedKnowledge::ArgValue
unsigned ArgValue
Definition: AssumeBundleQueries.h:103
llvm::PatternMatch::m_c_Or
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
Definition: PatternMatch.h:2192
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
APFloat.h
This file declares a class to represent arbitrary precision floating point values and provide a varie...
llvm::codeview::EncodedFramePtrReg::BasePtr
@ BasePtr
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1770
llvm::CallBase::bundle_op_info_begin
bundle_op_iterator bundle_op_info_begin()
Return the start of the list of BundleOpInfo instances associated with this OperandBundleUser.
Definition: InstrTypes.h:2185
llvm::ConstantExpr::getURem
static Constant * getURem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2711
llvm::InstCombinerImpl
Definition: InstCombineInternal.h:60
llvm::ConstantInt::get
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:885
InstCombineWorklist.h
llvm::isReallocLikeFn
bool isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that reallocates memory (e....
Definition: MemoryBuiltins.cpp:312
llvm::FunctionCallee::getFunctionType
FunctionType * getFunctionType()
Definition: DerivedTypes.h:181
llvm::SimplifyFMulInst
Value * SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FMul, fold the result or return null.
Definition: InstructionSimplify.cpp:5037
llvm::APSInt::getMinValue
static APSInt getMinValue(uint32_t numBits, bool Unsigned)
Return the APSInt representing the minimum integer value with the given bit width and signedness.
Definition: APSInt.h:294
llvm::getKnowledgeFromBundle
RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
Definition: AssumeBundleQueries.cpp:99
llvm::AttributeList::hasParamAttribute
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
Definition: Attributes.cpp:1559
llvm::KnownBits::isNegative
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:96
canonicalizeConstantArg0ToArg1
static CallInst * canonicalizeConstantArg0ToArg1(CallInst &Call)
Definition: InstCombineCalls.cpp:650
Align
uint64_t Align
Definition: ELFObjHandler.cpp:83
PatternMatch.h
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::MCID::Call
@ Call
Definition: MCInstrDesc.h:154
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::Instruction::mayWriteToMemory
bool mayWriteToMemory() const
Return true if this instruction may modify memory.
Definition: Instruction.cpp:563
llvm::CallBase::getCallingConv
CallingConv::ID getCallingConv() const
Definition: InstrTypes.h:1453
llvm::None
const NoneType None
Definition: None.h:23
Statepoint.h
llvm::Value::use_empty
bool use_empty() const
Definition: Value.h:357
Type.h
llvm::IRBuilderBase::CreateAddrSpaceCast
Value * CreateAddrSpaceCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2071
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::AnyMemTransferInst
Definition: IntrinsicInst.h:980
llvm::Log2_32_Ceil
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:610
llvm::CastInst::isLosslessCast
bool isLosslessCast() const
A lossless cast is one that does not alter the basic value.
Definition: Instructions.cpp:2661
llvm::PatternMatch::m_One
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:469
llvm::Instruction::getMetadata
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:277
llvm::OverflowResult
OverflowResult
Definition: ValueTracking.h:487
llvm::maxnum
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
Definition: APFloat.h:1297
findInitTrampolineFromAlloca
static IntrinsicInst * findInitTrampolineFromAlloca(Value *TrampMem)
Definition: InstCombineCalls.cpp:1976
llvm::ValueHandleBase::ValueIsRAUWd
static void ValueIsRAUWd(Value *Old, Value *New)
Definition: Value.cpp:1117
llvm::PatternMatch::m_NSWAdd
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
Definition: PatternMatch.h:1137
llvm::IRBuilderBase::CreateLaunderInvariantGroup
Value * CreateLaunderInvariantGroup(Value *Ptr)
Create a launder.invariant.group intrinsic call.
Definition: IRBuilder.cpp:963
llvm::Function::getAttributes
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:239
llvm::InvokeInst
Invoke instruction.
Definition: Instructions.h:3686
llvm::PatternMatch::m_Xor
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Definition: PatternMatch.h:1086
llvm::Type::isIntegerTy
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:202
removeTriviallyEmptyRange
static bool removeTriviallyEmptyRange(IntrinsicInst &EndI, InstCombinerImpl &IC, std::function< bool(const IntrinsicInst &)> IsStart)
Definition: InstCombineCalls.cpp:615
llvm::VectorType
Base class of all SIMD vector types.
Definition: DerivedTypes.h:391
llvm::Instruction::andIRFlags
void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction.
Definition: Instruction.cpp:292
llvm::IRBuilderBase::FastMathFlagGuard
Definition: IRBuilder.h:389
annotateAnyAllocSite
static void annotateAnyAllocSite(CallBase &Call, const TargetLibraryInfo *TLI)
Definition: InstCombineCalls.cpp:2052
VectorUtils.h
llvm::KnownBits::isAllOnes
bool isAllOnes() const
Returns true if value is all one bits.
Definition: KnownBits.h:78
llvm::Use::set
void set(Value *Val)
Definition: Value.h:869
llvm::ConstantExpr::getTrunc
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2069
llvm::GCStatepointInst::getGCRelocates
std::vector< const GCRelocateInst * > getGCRelocates() const
Get list of all gc reloactes linked to this statepoint May contain several relocations for the same b...
Definition: Statepoint.h:214
BasicBlock.h
llvm::cl::opt
Definition: CommandLine.h:1419
llvm::APFloat
Definition: APFloat.h:701
llvm::AttributeList::ReturnIndex
@ ReturnIndex
Definition: Attributes.h:378
llvm::IRBuilderBase::CreateBitCast
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2066
llvm::RetainedKnowledge::AttrKind
Attribute::AttrKind AttrKind
Definition: AssumeBundleQueries.h:102
llvm::PatternMatch::m_Zero
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:491
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:303
llvm::PatternMatch::m_NUWAdd
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
Definition: PatternMatch.h:1170
llvm::FunctionType::param_iterator
Type::subtype_iterator param_iterator
Definition: DerivedTypes.h:125
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::isMallocLikeFn
bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates uninitialized memory (such ...
Definition: MemoryBuiltins.cpp:263
llvm::GCRelocateInst
Represents calls to the gc.relocate intrinsic.
Definition: IntrinsicInst.h:1229
findInitTrampoline
static IntrinsicInst * findInitTrampoline(Value *Callee)
Definition: InstCombineCalls.cpp:2036
llvm::PointerType::getUnqual
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:649
llvm::PatternMatch::m_ImmConstant
match_combine_and< class_match< Constant >, match_unless< class_match< ConstantExpr > > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:737
llvm::LibCallSimplifier
LibCallSimplifier - This class implements a collection of optimizations that replace well formed call...
Definition: SimplifyLibCalls.h:100
llvm::InstCombiner::getAssumptionCache
AssumptionCache & getAssumptionCache() const
Definition: InstCombiner.h:367
llvm::BinaryOperator::CreateFMulFMF
static BinaryOperator * CreateFMulFMF(Value *V1, Value *V2, Instruction *FMFSource, const Twine &Name="")
Definition: InstrTypes.h:270
llvm::BinaryOpIntrinsic::isSigned
bool isSigned() const
Whether the intrinsic is signed or unsigned.
Definition: IntrinsicInst.cpp:397
llvm::InstCombiner::getDataLayout
const DataLayout & getDataLayout() const
Definition: InstCombiner.h:370
llvm::PatternMatch::m_LogicalOr
LogicalOp_match< LHS, RHS, Instruction::Or > m_LogicalOr(const LHS &L, const RHS &R)
Matches L || R either in the form of L | R or L ? true : R.
Definition: PatternMatch.h:2457
llvm::LoadInst::setOrdering
void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this load instruction.
Definition: Instructions.h:236
llvm::ARM_MB::ST
@ ST
Definition: ARMBaseInfo.h:73
llvm::TruncInst
This class represents a truncation of integer types.
Definition: Instructions.h:4691
llvm::GCRelocateInst::getDerivedPtr
Value * getDerivedPtr() const
Definition: IntrinsicInst.cpp:442
llvm::AttributeSet::get
static AttributeSet get(LLVMContext &C, const AttrBuilder &B)
Definition: Attributes.cpp:707
llvm::isStrdupLikeFn
bool isStrdupLikeFn(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 (strdup,...
Definition: MemoryBuiltins.cpp:332
llvm::ConstantPointerNull::get
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1756
llvm::CallBrInst
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
Definition: Instructions.h:3895
llvm::PatternMatch::m_AllOnes
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:401
simplifyNeonTbl1
static Value * simplifyNeonTbl1(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder)
Convert a table lookup to shufflevector if the mask is constant.
Definition: InstCombineCalls.cpp:559
llvm::Instruction::isIdenticalTo
bool isIdenticalTo(const Instruction *I) const
Return true if the specified instruction is exactly identical to the current one.
Definition: Instruction.cpp:469
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:58
llvm::assumeAligned
Align assumeAligned(uint64_t Value)
Treats the value 0 as a 1, so Align is always at least 1.
Definition: Alignment.h:113
llvm::DenseMap< Value *, unsigned >
llvm::InstCombinerImpl::visitFenceInst
Instruction * visitFenceInst(FenceInst &FI)
Definition: InstCombineCalls.cpp:1902
llvm::AtomicOrdering::Unordered
@ Unordered
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::AttrBuilder
Definition: Attributes.h:786
llvm::FunctionType::getParamType
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:134
llvm::LoadInst::setAlignment
void setAlignment(Align Align)
Definition: Instructions.h:226
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:440
llvm::lowerObjectSizeCall
Value * 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.
Definition: MemoryBuiltins.cpp:525
llvm::PatternMatch::m_FAbs
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
Definition: PatternMatch.h:2117
llvm::Attribute::getWithDereferenceableBytes
static Attribute getWithDereferenceableBytes(LLVMContext &Context, uint64_t Bytes)
Definition: Attributes.cpp:170
llvm::PatternMatch::m_And
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:1074
llvm::PointerType
Class to represent pointers.
Definition: DerivedTypes.h:634
ArrayRef.h
llvm::Instruction::setDebugLoc
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:362
llvm::PatternMatch::m_SRem
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:1062
llvm::computeKnownBits
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...
Definition: ValueTracking.cpp:238
maximum
Should compile r2 movcc movcs str strb mov lr r1 movcs movcc mov lr r1 str mov mov cmp r1 movlo r2 str bx lr r0 mov mov cmp r0 movhs r2 mov r1 bx lr Some of the NEON intrinsics may be appropriate for more general either as target independent intrinsics or perhaps elsewhere in the ARM backend Some of them may also be lowered to target independent and perhaps some new SDNodes could be added For maximum
Definition: README.txt:489
llvm::KnownBits::countMaxLeadingZeros
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:259
llvm::Value::MaximumAlignment
static const unsigned MaximumAlignment
Definition: Value.h:794
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::KnownBits::countMaxTrailingZeros
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:249
llvm::SPF_ABS
@ SPF_ABS
Floating point maxnum.
Definition: ValueTracking.h:665
llvm::CallBase::hasOperandBundles
bool hasOperandBundles() const
Return true if this User has any operand bundles.
Definition: InstrTypes.h:1903
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:958
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
llvm::isKnownNegation
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false)
Return true if the two given values are negation.
Definition: ValueTracking.cpp:5768
llvm::isAlignedAllocLikeFn
bool isAlignedAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates uninitialized memory with a...
Definition: MemoryBuiltins.cpp:276
llvm::Instruction::isFast
bool isFast() const
Determine whether all fast-math-flags are set.
Definition: Instruction.cpp:219
llvm::KnownBits::countMinLeadingZeros
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:229
llvm::KnownBits::countMaxPopulation
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:274
APSInt.h
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::Type::isVoidTy
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:139
simplifyInvariantGroupIntrinsic
static Instruction * simplifyInvariantGroupIntrinsic(IntrinsicInst &II, InstCombinerImpl &IC)
This function transforms launder.invariant.group and strip.invariant.group like: launder(launder(x)) ...
Definition: InstCombineCalls.cpp:389
foldCtpop
static Instruction * foldCtpop(IntrinsicInst &II, InstCombinerImpl &IC)
Definition: InstCombineCalls.cpp:494
llvm::IRBuilderBase::getTrue
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:458
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::MDNode
Metadata node.
Definition: Metadata.h:897
llvm::CallBase::Create
static CallBase * Create(CallBase *CB, ArrayRef< OperandBundleDef > Bundles, Instruction *InsertPt=nullptr)
Create a clone of CB with a different set of operand bundles and insert it before InsertPt.
Definition: Instructions.cpp:251
llvm::PatternMatch::m_Constant
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:98
haveSameOperands
static bool haveSameOperands(const IntrinsicInst &I, const IntrinsicInst &E, unsigned NumOperands)
Definition: InstCombineCalls.cpp:595
llvm::SmallPtrSetImpl::count
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:382
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:649
llvm::PatternMatch::m_Value
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:76
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:70
llvm::User::setOperand
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
llvm::CmpInst::ICMP_SLT
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:751
llvm::APSInt::getMaxValue
static APSInt getMaxValue(uint32_t numBits, bool Unsigned)
Return the APSInt representing the maximum integer value with the given bit width and signedness.
Definition: APSInt.h:287
llvm::APIntOps::smin
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition: APInt.h:2173
llvm::PatternMatch::m_SExt
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
Definition: PatternMatch.h:1584
llvm::PatternMatch::m_SpecificInt
specific_intval< false > m_SpecificInt(APInt V)
Match a specific integer value or vector with all elements equal to the value.
Definition: PatternMatch.h:836
llvm::ValueHandleBase::ValueIsDeleted
static void ValueIsDeleted(Value *V)
Definition: Value.cpp:1064
IT
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")))
llvm::SPII::Load
@ Load
Definition: SparcInstrInfo.h:32
llvm::VAEndInst
This represents the llvm.va_end intrinsic.
Definition: IntrinsicInst.h:1052
llvm::Instruction::getFastMathFlags
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
Definition: Instruction.cpp:259
None.h
llvm::APInt::getAllOnesValue
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:567
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
DataLayout.h
llvm::StructType
Class to represent struct types.
Definition: DerivedTypes.h:212
Cond
SmallVector< MachineOperand, 4 > Cond
Definition: BasicBlockSections.cpp:167
llvm::AssumptionCache
A cache of @llvm.assume calls within a function.
Definition: AssumptionCache.h:41
SimplifyLibCalls.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::Constant::getAggregateElement
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:421
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
isSafeToEliminateVarargsCast
static bool isSafeToEliminateVarargsCast(const CallBase &Call, 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...
Definition: InstCombineCalls.cpp:1923
llvm::Instruction::getFunction
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:70
uint32_t
Compiler.h
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:937
llvm::InstCombinerImpl::visitVAEndInst
Instruction * visitVAEndInst(VAEndInst &I)
Definition: InstCombineCalls.cpp:642
llvm::ModRefInfo::Mod
@ Mod
The access may modify the value stored in memory.
llvm::ilist_node_impl::getIterator
self_iterator getIterator()
Definition: ilist_node.h:81
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::CastInst::isBitOrNoopPointerCastable
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.
Definition: Instructions.cpp:3205
llvm::ConstantExpr::getIntegerCast
static Constant * getIntegerCast(Constant *C, Type *Ty, bool IsSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:2045
llvm::RecurKind::Mul
@ Mul
Product of integers.
llvm::FPExtInst
This class represents an extension of floating point types.
Definition: Instructions.h:4847
trunc
We have fiadd patterns now but the followings have the same cost and complexity We need a way to specify the later is more profitable def def The FP stackifier should handle simple permutates to reduce number of shuffle e g trunc
Definition: README-FPStack.txt:63
llvm::RetainedKnowledge
Represent one information held inside an operand bundle of an llvm.assume.
Definition: AssumeBundleQueries.h:101
llvm::Instruction::copyFastMathFlags
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction,...
Definition: Instruction.cpp:214
llvm::CastInst
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:432
llvm::WithOverflowInst
Represents an op.with.overflow intrinsic.
Definition: IntrinsicInst.h:541
llvm::AMDGPUISD::BFI
@ BFI
Definition: AMDGPUISelLowering.h:419
llvm::APInt::ult
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1205
llvm::Attribute::getWithDereferenceableOrNullBytes
static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context, uint64_t Bytes)
Definition: Attributes.cpp:176
llvm::CallBase::hasRetAttr
bool hasRetAttr(Attribute::AttrKind Kind) const
Determine whether the return value has the given attribute.
Definition: InstrTypes.h:1583
LLVM_FALLTHROUGH
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:281
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:298
llvm::Type::getContext
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:174
llvm::isOpNewLikeFn
bool isOpNewLikeFn(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 and throws if an all...
Definition: MemoryBuiltins.cpp:325
ValueHandle.h
llvm::BasicBlock::getTerminator
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:148
llvm::Value::stripPointerCasts
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:636
llvm::SmallPtrSetImplBase::size
size_type size() const
Definition: SmallPtrSet.h:92
llvm::CallBase::doesNotThrow
bool doesNotThrow() const
Determine if the call cannot unwind.
Definition: InstrTypes.h:1847
llvm::OperandBundleUse::Inputs
ArrayRef< Use > Inputs
Definition: InstrTypes.h:1060
llvm::ConstantInt::getZExtValue
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:140
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:205
getPromotedType
static Type * getPromotedType(Type *Ty)
Return the specified type promoted as it would be to pass though a va_arg area.
Definition: InstCombineCalls.cpp:99
llvm::Attribute::getWithAlignment
static Attribute getWithAlignment(LLVMContext &Context, Align Alignment)
Return a uniquified Attribute object that has the specific alignment set.
Definition: Attributes.cpp:160
llvm::GetStringLength
uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
Definition: ValueTracking.cpp:4229
llvm::LLVMContext::OB_gc_live
@ OB_gc_live
Definition: LLVMContext.h:95
llvm::Function::doesNotThrow
bool doesNotThrow() const
Determine if the function cannot unwind.
Definition: Function.h:594
Attributes.h
llvm::PatternMatch::m_FPExt
CastClass_match< OpTy, Instruction::FPExt > m_FPExt(const OpTy &Op)
Definition: PatternMatch.h:1648
llvm::APIntOps::umax
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition: APInt.h:2188
Constant.h
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition: NVPTX.h:123
llvm::GCRelocateInst::getBasePtr
Value * getBasePtr() const
Definition: IntrinsicInst.cpp:436
llvm::minnum
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE minNum semantics.
Definition: APFloat.h:1286
llvm::IRBuilderBase::getFalse
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:463
llvm::APInt::uadd_sat
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2047
llvm::Type::getInt64Ty
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:205
llvm::ConstantInt::getTrue
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:833
llvm::CallingConv::C
@ C
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
llvm::Constant::getNullValue
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:347
llvm::KnownBits
Definition: KnownBits.h:23
llvm::ConstantExpr::getNeg
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2645
llvm::AnyMemSetInst
This class represents any memset intrinsic.
Definition: IntrinsicInst.h:961
llvm::CastInst::CreateBitOrPointerCast
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
Definition: Instructions.cpp:3100
llvm::KnownBits::countMinPopulation
unsigned countMinPopulation() const
Returns the number of bits known to be one.
Definition: KnownBits.h:269
NC
#define NC
Definition: regutils.h:42
llvm::BinaryOperator::CreateNUW
static BinaryOperator * CreateNUW(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name="")
Definition: InstrTypes.h:305
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:314
llvm::ConstantInt::getBool
static ConstantInt * getBool(LLVMContext &Context, bool V)
Definition: Constants.cpp:847
GlobalVariable.h
llvm::ConstantFP::get
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:932
Casting.h
llvm::InstCombinerImpl::visitCallBrInst
Instruction * visitCallBrInst(CallBrInst &CBI)
Definition: InstCombineCalls.cpp:1917
LowAndHigh
Metadata * LowAndHigh[]
Definition: NVVMIntrRange.cpp:68
Function.h
llvm::BitWidth
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:147
llvm::BinaryOperator::CreateNSWNeg
static BinaryOperator * CreateNSWNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Definition: Instructions.cpp:2581
llvm::DenseMapBase::size
unsigned size() const
Definition: DenseMap.h:100
llvm::PatternMatch::m_BSwap
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
Definition: PatternMatch.h:2112
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:207
powi
This is blocked on not handling X *X *X powi(X, 3)(see note above). The issue is that we end up getting t
llvm::OverflowResult::AlwaysOverflowsHigh
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
llvm::Function::getFunctionType
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:165
llvm::PatternMatch::m_ZeroInt
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:478
llvm::getInverseMinMaxIntrinsic
Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
Definition: ValueTracking.cpp:6157
llvm::PatternMatch::m_PtrToInt
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
Definition: PatternMatch.h:1560
llvm::CallBase::getCalledOperand
Value * getCalledOperand() const
Definition: InstrTypes.h:1389
createOverflowTuple
static Instruction * createOverflowTuple(IntrinsicInst *II, Value *Result, Constant *Overflow)
Creates a result tuple for an overflow intrinsic II with a given Result and a constant Overflow value...
Definition: InstCombineCalls.cpp:663
llvm::IntrinsicInst
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:45
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:474
llvm::APInt::isNullValue
bool isNullValue() const
Determine if all bits are clear.
Definition: APInt.h:411
llvm::InstCombinerImpl::replaceOperand
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
Definition: InstCombineInternal.h:406
llvm::BasicBlock::reverse_iterator
InstListType::reverse_iterator reverse_iterator
Definition: BasicBlock.h:92
llvm::RecurKind::FAdd
@ FAdd
Sum of floats.
Instructions.h
llvm::isImpliedByDomCondition
Optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
Definition: ValueTracking.cpp:6573
llvm::Type::isStructTy
bool isStructTy() const
True if this is an instance of StructType.
Definition: Type.h:223
llvm::AttributeSet
Definition: Attributes.h:247
SmallVector.h
getKnownSign
static Optional< bool > getKnownSign(Value *Op, Instruction *CxtI, const DataLayout &DL, AssumptionCache *AC, DominatorTree *DT)
Definition: InstCombineCalls.cpp:682
llvm::PatternMatch::m_Specific
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:758
User.h
llvm::PatternMatch::m_ICmp
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Definition: PatternMatch.h:1355
llvm::CmpInst::ICMP_UGT
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:745
llvm::User::op_begin
op_iterator op_begin()
Definition: User.h:234
llvm::Type::getPointerElementType
Type * getPointerElementType() const
Definition: Type.h:378
llvm::CallBase::getArgOperand
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1341
N
#define N
llvm::IntrinsicInst::isCommutative
bool isCommutative() const
Return true if swapping the first two arguments to the intrinsic produces the same result.
Definition: IntrinsicInst.h:58
llvm::Instruction::getParent
const BasicBlock * getParent() const
Definition: Instruction.h:94
InstructionSimplify.h
llvm::ConstantExpr::getMul
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2686
TargetTransformInfo.h
llvm::PHINode
Definition: Instructions.h:2572
llvm::Instruction::copyIRFlags
void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
Definition: Instruction.cpp:268
llvm::buildAssumeFromKnowledge
AssumeInst * buildAssumeFromKnowledge(ArrayRef< RetainedKnowledge > Knowledge, Instruction *CtxI, AssumptionCache *AC=nullptr, DominatorTree *DT=nullptr)
Build and return a new assume created from the provided knowledge if the knowledge in the assume is f...
Definition: AssumeBundleBuilder.cpp:305
llvm::CallBase::addAttribute
void addAttribute(unsigned i, Attribute::AttrKind Kind)
adds the attribute to the list of attributes.
Definition: InstrTypes.h:1493
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1164
llvm::GlobalValue::getType
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:271
llvm::Module::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:397
DerivedTypes.h
llvm::PatternMatch::m_Neg
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
Definition: PatternMatch.h:2207
llvm::APInt::getLowBitsSet
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:667
llvm::IntegerType::get
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:276
llvm::TargetLibraryInfoImpl::isCallingConvCCompatible
static bool isCallingConvCCompatible(CallBase *CI)
Returns true if call site / callee has cdecl-compatible calling conventions.
Definition: TargetLibraryInfo.cpp:99
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1450
LLVMContext.h
From
BlockVerifier::State From
Definition: BlockVerifier.cpp:55
llvm::IRBuilderBase::CreateSub
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1207
llvm::Value::takeName
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:376
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::InstCombinerImpl::SimplifyAnyMemSet
Instruction * SimplifyAnyMemSet(AnyMemSetInst *MI)
Definition: InstCombineCalls.cpp:222
llvm::User::getOperand
Value * getOperand(unsigned i) const
Definition: User.h:169
llvm::cl::desc
Definition: CommandLine.h:411
raw_ostream.h
llvm::PatternMatch::m_Trunc
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
Definition: PatternMatch.h:1572
llvm::NullPointerIsDefined
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:1847
llvm::Attribute::getWithByValType
static Attribute getWithByValType(LLVMContext &Context, Type *Ty)
Definition: Attributes.cpp:182
llvm::ConstantAggregateZero::get
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1649
llvm::Type::canLosslesslyBitCastTo
bool canLosslesslyBitCastTo(Type *Ty) const
Return true if this type could be converted with a lossless BitCast to type 'Ty'.
Definition: Type.cpp:63
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:624
llvm::isPowerOf2_64
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:497
llvm::PatternMatch::m_Undef
class_match< UndefValue > m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:92
Value.h
llvm::abs
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:1272
llvm::PatternMatch::m_Cmp
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
Definition: PatternMatch.h:89
llvm::FunctionType::getReturnType
Type * getReturnType() const
Definition: DerivedTypes.h:123
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
Debug.h
llvm::Value::users
iterator_range< user_iterator > users()
Definition: Value.h:434
llvm::InstCombinerImpl::computeKnownBits
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Instruction *CxtI) const
Definition: InstCombineInternal.h:460
llvm::APIntOps::smax
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition: APInt.h:2178
llvm::AttributeList::isEmpty
bool isEmpty() const
Return true if there are no attributes.
Definition: Attributes.h:749
llvm::PatternMatch::m_LogicalAnd
LogicalOp_match< LHS, RHS, Instruction::And > m_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R either in the form of L & R or L ? R : false.
Definition: PatternMatch.h:2446
llvm::IRBuilderBase::CreateCall
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2344
llvm::Optional::getValue
constexpr const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:280
llvm::isFreeCall
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
Definition: MemoryBuiltins.cpp:485
llvm::Instruction::moveBefore
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Definition: Instruction.cpp:97
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1161
llvm::CallBase::setCallingConv
void setCallingConv(CallingConv::ID CC)
Definition: InstrTypes.h:1457
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:102
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:364
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38