LLVM 24.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"
18#include "llvm/ADT/Bitset.h"
22#include "llvm/ADT/Statistic.h"
28#include "llvm/Analysis/Loads.h"
33#include "llvm/IR/Attributes.h"
34#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/DebugInfo.h"
41#include "llvm/IR/Function.h"
43#include "llvm/IR/InlineAsm.h"
44#include "llvm/IR/InstrTypes.h"
45#include "llvm/IR/Instruction.h"
48#include "llvm/IR/Intrinsics.h"
49#include "llvm/IR/IntrinsicsAArch64.h"
50#include "llvm/IR/IntrinsicsAMDGPU.h"
51#include "llvm/IR/IntrinsicsARM.h"
52#include "llvm/IR/IntrinsicsHexagon.h"
53#include "llvm/IR/LLVMContext.h"
54#include "llvm/IR/Metadata.h"
57#include "llvm/IR/Statepoint.h"
58#include "llvm/IR/Type.h"
59#include "llvm/IR/User.h"
60#include "llvm/IR/Value.h"
61#include "llvm/IR/ValueHandle.h"
66#include "llvm/Support/Debug.h"
77#include <algorithm>
78#include <cassert>
79#include <cstdint>
80#include <optional>
81#include <utility>
82#include <vector>
83
84#define DEBUG_TYPE "instcombine"
86
87using namespace llvm;
88using namespace PatternMatch;
89
90STATISTIC(NumSimplified, "Number of library calls simplified");
91
93 "instcombine-guard-widening-window",
94 cl::init(3),
95 cl::desc("How wide an instruction window to bypass looking for "
96 "another guard"));
97
98/// Return the specified type promoted as it would be to pass though a va_arg
99/// area.
101 if (IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
102 if (ITy->getBitWidth() < 32)
103 return Type::getInt32Ty(Ty->getContext());
104 }
105 return Ty;
106}
107
108/// Recognize a memcpy/memmove from a trivially otherwise unused alloca.
109/// TODO: This should probably be integrated with visitAllocSites, but that
110/// requires a deeper change to allow either unread or unwritten objects.
112 auto *Src = MI->getRawSource();
113 while (isa<GetElementPtrInst>(Src)) {
114 if (!Src->hasOneUse())
115 return false;
116 Src = cast<Instruction>(Src)->getOperand(0);
117 }
118 return isa<AllocaInst>(Src) && Src->hasOneUse();
119}
120
122 Align DstAlign = getKnownAlignment(MI->getRawDest(), DL, MI, &AC, &DT);
123 MaybeAlign CopyDstAlign = MI->getDestAlign();
124 if (!CopyDstAlign || *CopyDstAlign < DstAlign) {
125 MI->setDestAlignment(DstAlign);
126 return MI;
127 }
128
129 Align SrcAlign = getKnownAlignment(MI->getRawSource(), DL, MI, &AC, &DT);
130 MaybeAlign CopySrcAlign = MI->getSourceAlign();
131 if (!CopySrcAlign || *CopySrcAlign < SrcAlign) {
132 MI->setSourceAlignment(SrcAlign);
133 return MI;
134 }
135
136 // If we have a store to a location which is known constant, we can conclude
137 // that the store must be storing the constant value (else the memory
138 // wouldn't be constant), and this must be a noop.
139 if (!isModSet(AA->getModRefInfoMask(MI->getDest()))) {
140 // Set the size of the copy to 0, it will be deleted on the next iteration.
141 MI->setLength((uint64_t)0);
142 return MI;
143 }
144
145 // If the source is provably undef, the memcpy/memmove doesn't do anything
146 // (unless the transfer is volatile).
147 if (hasUndefSource(MI) && !MI->isVolatile()) {
148 // Set the size of the copy to 0, it will be deleted on the next iteration.
149 MI->setLength((uint64_t)0);
150 return MI;
151 }
152
153 // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
154 // load/store.
155 ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getLength());
156 if (!MemOpLength) return nullptr;
157
158 // Source and destination pointer types are always "i8*" for intrinsic. See
159 // if the size is something we can handle with a single primitive load/store.
160 // A single load+store correctly handles overlapping memory in the memmove
161 // case.
162 uint64_t Size = MemOpLength->getLimitedValue();
163 assert(Size && "0-sized memory transferring should be removed already.");
164
165 if (Size > 8 || (Size&(Size-1)))
166 return nullptr; // If not 1/2/4/8 bytes, exit.
167
168 // If it is an atomic and alignment is less than the size then we will
169 // introduce the unaligned memory access which will be later transformed
170 // into libcall in CodeGen. This is not evident performance gain so disable
171 // it now.
172 if (MI->isAtomic())
173 if (*CopyDstAlign < Size || *CopySrcAlign < Size)
174 return nullptr;
175
176 // Use an integer load+store unless we can find something better.
177 IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
178
179 // If the memcpy has metadata describing the members, see if we can get the
180 // TBAA, scope and noalias tags describing our copy.
181 AAMDNodes AACopyMD = MI->getAAMetadata().adjustForAccess(Size);
182
183 Value *Src = MI->getArgOperand(1);
184 Value *Dest = MI->getArgOperand(0);
185 LoadInst *L = Builder.CreateLoad(IntType, Src);
186 // Alignment from the mem intrinsic will be better, so use it.
187 L->setAlignment(*CopySrcAlign);
188 L->setAAMetadata(AACopyMD);
189 MDNode *LoopMemParallelMD =
190 MI->getMetadata(LLVMContext::MD_mem_parallel_loop_access);
191 if (LoopMemParallelMD)
192 L->setMetadata(LLVMContext::MD_mem_parallel_loop_access, LoopMemParallelMD);
193 MDNode *AccessGroupMD = MI->getMetadata(LLVMContext::MD_access_group);
194 if (AccessGroupMD)
195 L->setMetadata(LLVMContext::MD_access_group, AccessGroupMD);
196
197 StoreInst *S = Builder.CreateStore(L, Dest);
198 // Alignment from the mem intrinsic will be better, so use it.
199 S->setAlignment(*CopyDstAlign);
200 S->setAAMetadata(AACopyMD);
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 S->copyMetadata(*MI, LLVMContext::MD_DIAssignID);
206
207 if (auto *MT = dyn_cast<MemTransferInst>(MI)) {
208 // non-atomics can be volatile
209 L->setVolatile(MT->isVolatile());
210 S->setVolatile(MT->isVolatile());
211 }
212 if (MI->isAtomic()) {
213 // atomics have to be unordered
214 L->setOrdering(AtomicOrdering::Unordered);
216 }
217
218 // Set the size of the copy to 0, it will be deleted on the next iteration.
219 MI->setLength((uint64_t)0);
220 return MI;
221}
222
224 const Align KnownAlignment =
225 getKnownAlignment(MI->getDest(), DL, MI, &AC, &DT);
226 MaybeAlign MemSetAlign = MI->getDestAlign();
227 if (!MemSetAlign || *MemSetAlign < KnownAlignment) {
228 MI->setDestAlignment(KnownAlignment);
229 return MI;
230 }
231
232 // If we have a store to a location which is known constant, we can conclude
233 // that the store must be storing the constant value (else the memory
234 // wouldn't be constant), and this must be a noop.
235 if (!isModSet(AA->getModRefInfoMask(MI->getDest()))) {
236 // Set the size of the copy to 0, it will be deleted on the next iteration.
237 MI->setLength((uint64_t)0);
238 return MI;
239 }
240
241 // Remove memset with an undef value.
242 // FIXME: This is technically incorrect because it might overwrite a poison
243 // value. Change to PoisonValue once #52930 is resolved.
244 if (isa<UndefValue>(MI->getValue())) {
245 // Set the size of the copy to 0, it will be deleted on the next iteration.
246 MI->setLength((uint64_t)0);
247 return MI;
248 }
249
250 // Extract the length and validate the fill type.
251 ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
252 Value *Fill = MI->getValue();
253 if (!LenC || !Fill->getType()->isIntegerTy(8))
254 return nullptr;
255 const uint64_t Len = LenC->getLimitedValue();
256 assert(Len && "0-sized memory setting should be removed already.");
257 const Align Alignment = MI->getDestAlign().valueOrOne();
258
259 // If it is an atomic and alignment is less than the size then we will
260 // introduce the unaligned memory access which will be later transformed
261 // into libcall in CodeGen. This is not evident performance gain so disable
262 // it now.
263 if (MI->isAtomic() && Alignment < Len)
264 return nullptr;
265
266 // memset(s,c,n) -> store s, c (for n=1,2,4,8)
267 if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
268 Value *Dest = MI->getDest();
269
270 // Extract the fill value and store. A one-byte memset does not need
271 // replication so a nonconstant i8 fill can be stored directly.
272 Value *FillVal;
273 if (auto *FillC = dyn_cast<ConstantInt>(Fill))
274 FillVal = ConstantInt::get(MI->getContext(),
275 APInt::getSplat(Len * 8, FillC->getValue()));
276 else if (Len == 1)
277 FillVal = Fill;
278 else
279 return nullptr;
280
281 StoreInst *S = Builder.CreateStore(FillVal, Dest, MI->isVolatile());
282 S->copyMetadata(*MI, LLVMContext::MD_DIAssignID);
283 for (DbgVariableRecord *DbgAssign : at::getDVRAssignmentMarkers(S)) {
284 if (llvm::is_contained(DbgAssign->location_ops(), Fill))
285 DbgAssign->replaceVariableLocationOp(Fill, FillVal);
286 }
287
288 S->setAlignment(Alignment);
289 if (MI->isAtomic())
291
292 // Set the size of the copy to 0, it will be deleted on the next iteration.
293 MI->setLength((uint64_t)0);
294 return MI;
295 }
296
297 return nullptr;
298}
299
300// TODO, Obvious Missing Transforms:
301// * Narrow width by halfs excluding zero/undef lanes
302Value *InstCombinerImpl::simplifyMaskedLoad(IntrinsicInst &II) {
303 Value *LoadPtr = II.getArgOperand(0);
304 const Align Alignment = II.getParamAlign(0).valueOrOne();
305 Value *Mask = II.getArgOperand(1);
306
307 // If the mask is all ones or poison, this is a plain vector load of the 1st
308 // argument.
309 if (match(Mask, m_AllOnesOrPoison())) {
310 LoadInst *L = Builder.CreateAlignedLoad(II.getType(), LoadPtr, Alignment,
311 "unmaskedload");
312 L->copyMetadata(II);
313 return L;
314 }
315
316 // If we can unconditionally load from this address, replace with a
317 // load/select idiom.
318 if (isDereferenceablePointer(LoadPtr, II.getType(),
320 LoadInst *LI = Builder.CreateAlignedLoad(II.getType(), LoadPtr, Alignment,
321 "unmaskedload");
322 LI->copyMetadata(II);
323 return Builder.CreateSelect(II.getArgOperand(1), LI, II.getArgOperand(2));
324 }
325
326 return nullptr;
327}
328
329// TODO, Obvious Missing Transforms:
330// * Single constant active lane -> store
331// * Narrow width by halfs excluding zero/undef lanes
332Instruction *InstCombinerImpl::simplifyMaskedStore(IntrinsicInst &II) {
333 Value *StorePtr = II.getArgOperand(1);
334 Align Alignment = II.getParamAlign(1).valueOrOne();
335 auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(2));
336 if (!ConstMask)
337 return nullptr;
338
339 // If the mask is all zeros or poison, this instruction does nothing.
340 if (match(ConstMask, m_ZeroOrPoison()))
342
343 // If the mask is all ones or poison, this is a plain vector store of the 1st
344 // argument.
345 if (match(ConstMask, m_AllOnesOrPoison())) {
346 StoreInst *S =
347 new StoreInst(II.getArgOperand(0), StorePtr, false, Alignment);
348 S->copyMetadata(II);
349 return S;
350 }
351
352 if (isa<ScalableVectorType>(ConstMask->getType()))
353 return nullptr;
354
355 // Use masked off lanes to simplify operands via SimplifyDemandedVectorElts
356 APInt DemandedElts = possiblyDemandedEltsInMask(ConstMask);
357 APInt PoisonElts(DemandedElts.getBitWidth(), 0);
358 if (Value *V = SimplifyDemandedVectorElts(II.getOperand(0), DemandedElts,
359 PoisonElts))
360 return replaceOperand(II, 0, V);
361
362 return nullptr;
363}
364
365// TODO, Obvious Missing Transforms:
366// * Single constant active lane load -> load
367// * Dereferenceable address & few lanes -> scalarize speculative load/selects
368// * Adjacent vector addresses -> masked.load
369// * Narrow width by halfs excluding zero/undef lanes
370// * Vector incrementing address -> vector masked load
371Instruction *InstCombinerImpl::simplifyMaskedGather(IntrinsicInst &II) {
372 auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(1));
373 if (!ConstMask)
374 return nullptr;
375
376 // Vector splat address w/known mask -> scalar load
377 // Fold the gather to load the source vector first lane
378 // because it is reloading the same value each time
379 if (ConstMask->isAllOnesValue())
380 if (auto *SplatPtr = getSplatValue(II.getArgOperand(0))) {
381 auto *VecTy = cast<VectorType>(II.getType());
382 const Align Alignment = II.getParamAlign(0).valueOrOne();
383 LoadInst *L = Builder.CreateAlignedLoad(VecTy->getElementType(), SplatPtr,
384 Alignment, "load.scalar");
385 Value *Shuf =
386 Builder.CreateVectorSplat(VecTy->getElementCount(), L, "broadcast");
388 }
389
390 return nullptr;
391}
392
393// TODO, Obvious Missing Transforms:
394// * Single constant active lane -> store
395// * Adjacent vector addresses -> masked.store
396// * Narrow store width by halfs excluding zero/undef lanes
397// * Vector incrementing address -> vector masked store
398Instruction *InstCombinerImpl::simplifyMaskedScatter(IntrinsicInst &II) {
399 auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(2));
400 if (!ConstMask)
401 return nullptr;
402
403 // If the mask is all zeros or poison, a scatter does nothing.
404 if (match(ConstMask, m_ZeroOrPoison()))
406
407 // Vector splat address -> scalar store
408 if (auto *SplatPtr = getSplatValue(II.getArgOperand(1))) {
409 // scatter(splat(value), splat(ptr), non-zero-mask) -> store value, ptr
410 if (auto *SplatValue = getSplatValue(II.getArgOperand(0))) {
411 if (maskContainsAllOneOrUndef(ConstMask)) {
412 Align Alignment = II.getParamAlign(1).valueOrOne();
413 StoreInst *S = new StoreInst(SplatValue, SplatPtr, /*IsVolatile=*/false,
414 Alignment);
415 S->copyMetadata(II);
416 return S;
417 }
418 }
419 // scatter(vector, splat(ptr), splat(true)) -> store extract(vector,
420 // lastlane), ptr
421 if (ConstMask->isAllOnesValue()) {
422 Align Alignment = II.getParamAlign(1).valueOrOne();
423 VectorType *WideLoadTy = cast<VectorType>(II.getArgOperand(1)->getType());
424 ElementCount VF = WideLoadTy->getElementCount();
425 Value *RunTimeVF = Builder.CreateElementCount(Builder.getInt32Ty(), VF);
426 Value *LastLane = Builder.CreateSub(RunTimeVF, Builder.getInt32(1));
427 Value *Extract =
428 Builder.CreateExtractElement(II.getArgOperand(0), LastLane);
429 StoreInst *S =
430 new StoreInst(Extract, SplatPtr, /*IsVolatile=*/false, Alignment);
431 S->copyMetadata(II);
432 return S;
433 }
434 }
435 if (isa<ScalableVectorType>(ConstMask->getType()))
436 return nullptr;
437
438 // Use masked off lanes to simplify operands via SimplifyDemandedVectorElts
439 APInt DemandedElts = possiblyDemandedEltsInMask(ConstMask);
440 APInt PoisonElts(DemandedElts.getBitWidth(), 0);
441 if (Value *V = SimplifyDemandedVectorElts(II.getOperand(0), DemandedElts,
442 PoisonElts))
443 return replaceOperand(II, 0, V);
444 if (Value *V = SimplifyDemandedVectorElts(II.getOperand(1), DemandedElts,
445 PoisonElts))
446 return replaceOperand(II, 1, V);
447
448 return nullptr;
449}
450
451/// This function transforms launder.invariant.group and strip.invariant.group
452/// like:
453/// launder(launder(%x)) -> launder(%x) (the result is not the argument)
454/// launder(strip(%x)) -> launder(%x)
455/// strip(strip(%x)) -> strip(%x) (the result is not the argument)
456/// strip(launder(%x)) -> strip(%x)
457/// This is legal because it preserves the most recent information about
458/// the presence or absence of invariant.group.
460 InstCombinerImpl &IC) {
461 auto *Arg = II.getArgOperand(0);
462 auto *StrippedArg = Arg->stripPointerCasts();
463 auto *StrippedInvariantGroupsArg = StrippedArg;
464 while (auto *Intr = dyn_cast<IntrinsicInst>(StrippedInvariantGroupsArg)) {
465 if (Intr->getIntrinsicID() != Intrinsic::launder_invariant_group &&
466 Intr->getIntrinsicID() != Intrinsic::strip_invariant_group)
467 break;
468 StrippedInvariantGroupsArg = Intr->getArgOperand(0)->stripPointerCasts();
469 }
470 if (StrippedArg == StrippedInvariantGroupsArg)
471 return nullptr; // No launders/strips to remove.
472
473 Value *Result = nullptr;
474
475 if (II.getIntrinsicID() == Intrinsic::launder_invariant_group)
476 Result = IC.Builder.CreateLaunderInvariantGroup(StrippedInvariantGroupsArg);
477 else if (II.getIntrinsicID() == Intrinsic::strip_invariant_group)
478 Result = IC.Builder.CreateStripInvariantGroup(StrippedInvariantGroupsArg);
479 else
481 "simplifyInvariantGroupIntrinsic only handles launder and strip");
482 if (Result->getType()->getPointerAddressSpace() !=
483 II.getType()->getPointerAddressSpace())
484 Result = IC.Builder.CreateAddrSpaceCast(Result, II.getType());
485
486 return cast<Instruction>(Result);
487}
488
490 assert((II.getIntrinsicID() == Intrinsic::cttz ||
491 II.getIntrinsicID() == Intrinsic::ctlz) &&
492 "Expected cttz or ctlz intrinsic");
493 bool IsTZ = II.getIntrinsicID() == Intrinsic::cttz;
494 Value *Op0 = II.getArgOperand(0);
495 Value *Op1 = II.getArgOperand(1);
496 Value *X;
497 // ctlz(bitreverse(x)) -> cttz(x)
498 // cttz(bitreverse(x)) -> ctlz(x)
499 if (match(Op0, m_BitReverse(m_Value(X)))) {
500 Intrinsic::ID ID = IsTZ ? Intrinsic::ctlz : Intrinsic::cttz;
501 Function *F =
502 Intrinsic::getOrInsertDeclaration(II.getModule(), ID, II.getType());
503 return CallInst::Create(F, {X, II.getArgOperand(1)});
504 }
505
506 if (II.getType()->isIntOrIntVectorTy(1)) {
507 // ctlz/cttz i1 Op0 --> not Op0
508 if (match(Op1, m_Zero()))
509 return BinaryOperator::CreateNot(Op0);
510 // If zero is poison, then the input can be assumed to be "true", so the
511 // instruction simplifies to "false".
512 assert(match(Op1, m_One()) && "Expected ctlz/cttz operand to be 0 or 1");
513 return IC.replaceInstUsesWith(II, ConstantInt::getNullValue(II.getType()));
514 }
515
516 // If ctlz/cttz is only used as a shift amount, set is_zero_poison to true.
517 if (II.hasOneUse() && match(Op1, m_Zero()) &&
518 match(II.user_back(), m_Shift(m_Value(), m_Specific(&II))))
519 return CallInst::Create(II.getCalledFunction(),
520 {Op0, IC.Builder.getTrue()});
521
522 Constant *C;
523
524 if (IsTZ) {
525 // cttz(-x) -> cttz(x)
526 if (match(Op0, m_Neg(m_Value(X))))
527 return CallInst::Create(II.getCalledFunction(), {X, Op1});
528
529 // cttz(-x & x) -> cttz(x)
530 if (match(Op0, m_c_And(m_Neg(m_Value(X)), m_Deferred(X))))
531 return CallInst::Create(II.getCalledFunction(), {X, Op1});
532
533 // cttz(mul(X, OddC)) -> cttz(X)
534 if (match(Op0, m_Mul(m_Value(X),
535 m_CheckedInt([](const APInt &C) { return C[0]; }))))
536 return CallInst::Create(II.getCalledFunction(), {X, Op1});
537
538 // cttz(sext(x)) -> cttz(zext(x))
539 if (match(Op0, m_OneUse(m_SExt(m_Value(X))))) {
540 auto *Zext = IC.Builder.CreateZExt(X, II.getType());
541 auto *CttzZext =
542 IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, Zext, Op1);
543 return IC.replaceInstUsesWith(II, CttzZext);
544 }
545
546 // Zext doesn't change the number of trailing zeros, so narrow:
547 // cttz(zext(x)) -> zext(cttz(x)) if the 'ZeroIsPoison' parameter is 'true'.
548 if (match(Op0, m_OneUse(m_ZExt(m_Value(X)))) && match(Op1, m_One())) {
549 auto *Cttz = IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, X,
550 IC.Builder.getTrue());
551 auto *ZextCttz = IC.Builder.CreateZExt(Cttz, II.getType());
552 return IC.replaceInstUsesWith(II, ZextCttz);
553 }
554
555 // cttz(abs(x)) -> cttz(x)
556 // cttz(nabs(x)) -> cttz(x)
557 Value *Y;
559 if (SPF == SPF_ABS || SPF == SPF_NABS)
560 return CallInst::Create(II.getCalledFunction(), {X, Op1});
561
563 return CallInst::Create(II.getCalledFunction(), {X, Op1});
564
565 // cttz(shl(%const, %val), 1) --> add(cttz(%const, 1), %val)
566 if (match(Op0, m_Shl(m_ImmConstant(C), m_Value(X))) &&
567 match(Op1, m_One())) {
568 Value *ConstCttz =
569 IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, C, Op1);
570 return BinaryOperator::CreateAdd(ConstCttz, X);
571 }
572
573 // cttz(lshr exact (%const, %val), 1) --> sub(cttz(%const, 1), %val)
574 if (match(Op0, m_Exact(m_LShr(m_ImmConstant(C), m_Value(X)))) &&
575 match(Op1, m_One())) {
576 Value *ConstCttz =
577 IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, C, Op1);
578 return BinaryOperator::CreateSub(ConstCttz, X);
579 }
580
581 // cttz(add(lshr(UINT_MAX, %val), 1)) --> sub(width, %val)
582 if (match(Op0, m_Add(m_LShr(m_AllOnes(), m_Value(X)), m_One()))) {
583 Value *Width =
584 ConstantInt::get(II.getType(), II.getType()->getScalarSizeInBits());
585 return BinaryOperator::CreateSub(Width, X);
586 }
587 } else {
588 // ctlz(lshr(%const, %val), 1) --> add(ctlz(%const, 1), %val)
589 if (match(Op0, m_LShr(m_ImmConstant(C), m_Value(X))) &&
590 match(Op1, m_One())) {
591 Value *ConstCtlz =
592 IC.Builder.CreateBinaryIntrinsic(Intrinsic::ctlz, C, Op1);
593 return BinaryOperator::CreateAdd(ConstCtlz, X);
594 }
595
596 // ctlz(shl nuw (%const, %val), 1) --> sub(ctlz(%const, 1), %val)
597 if (match(Op0, m_NUWShl(m_ImmConstant(C), m_Value(X))) &&
598 match(Op1, m_One())) {
599 Value *ConstCtlz =
600 IC.Builder.CreateBinaryIntrinsic(Intrinsic::ctlz, C, Op1);
601 return BinaryOperator::CreateSub(ConstCtlz, X);
602 }
603
604 // ctlz(~x & (x - 1)) -> bitwidth - cttz(x, false)
605 if (Op0->hasOneUse() &&
606 match(Op0,
608 Type *Ty = II.getType();
609 unsigned BitWidth = Ty->getScalarSizeInBits();
610 auto *Cttz = IC.Builder.CreateIntrinsic(Intrinsic::cttz, Ty,
611 {X, IC.Builder.getFalse()});
612 auto *Bw = ConstantInt::get(Ty, APInt(BitWidth, BitWidth));
613 return IC.replaceInstUsesWith(II, IC.Builder.CreateSub(Bw, Cttz));
614 }
615 }
616
617 // cttz(Pow2) -> Log2(Pow2)
618 // ctlz(Pow2) -> BitWidth - 1 - Log2(Pow2)
619 if (auto *R = IC.tryGetLog2(Op0, match(Op1, m_One()))) {
620 if (IsTZ)
621 return IC.replaceInstUsesWith(II, R);
622 BinaryOperator *BO = BinaryOperator::CreateSub(
623 ConstantInt::get(R->getType(), R->getType()->getScalarSizeInBits() - 1),
624 R);
625 BO->setHasNoSignedWrap();
627 return BO;
628 }
629
631
632 // Create a mask for bits above (ctlz) or below (cttz) the first known one.
633 unsigned PossibleZeros = IsTZ ? Known.countMaxTrailingZeros()
634 : Known.countMaxLeadingZeros();
635 unsigned DefiniteZeros = IsTZ ? Known.countMinTrailingZeros()
636 : Known.countMinLeadingZeros();
637
638 // If all bits above (ctlz) or below (cttz) the first known one are known
639 // zero, this value is constant.
640 // FIXME: This should be in InstSimplify because we're replacing an
641 // instruction with a constant.
642 if (PossibleZeros == DefiniteZeros) {
643 auto *C = ConstantInt::get(Op0->getType(), DefiniteZeros);
644 return IC.replaceInstUsesWith(II, C);
645 }
646
647 // If the input to cttz/ctlz is known to be non-zero,
648 // then change the 'ZeroIsPoison' parameter to 'true'
649 // because we know the zero behavior can't affect the result.
650 if (!Known.One.isZero() ||
652 if (!match(II.getArgOperand(1), m_One()))
653 return CallInst::Create(II.getCalledFunction(),
654 {Op0, IC.Builder.getTrue()});
655 }
656
657 // Add range attribute since known bits can't completely reflect what we know.
658 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
659 if (BitWidth != 1 && !II.hasRetAttr(Attribute::Range) &&
660 !II.getMetadata(LLVMContext::MD_range)) {
661 ConstantRange Range(APInt(BitWidth, DefiniteZeros),
662 APInt(BitWidth, PossibleZeros + 1));
663 II.addRangeRetAttr(Range);
664 return &II;
665 }
666
667 return nullptr;
668}
669
671 assert(II.getIntrinsicID() == Intrinsic::ctpop &&
672 "Expected ctpop intrinsic");
673 Type *Ty = II.getType();
674 unsigned BitWidth = Ty->getScalarSizeInBits();
675 Value *Op0 = II.getArgOperand(0);
676 Value *X, *Y;
677
678 // ctpop(bitreverse(x)) -> ctpop(x)
679 // ctpop(bswap(x)) -> ctpop(x)
680 if (match(Op0, m_BitReverse(m_Value(X))) || match(Op0, m_BSwap(m_Value(X))))
681 return CallInst::Create(II.getCalledFunction(), X);
682
683 // ctpop(rot(x)) -> ctpop(x)
684 if ((match(Op0, m_FShl(m_Value(X), m_Value(Y), m_Value())) ||
685 match(Op0, m_FShr(m_Value(X), m_Value(Y), m_Value()))) &&
686 X == Y)
687 return CallInst::Create(II.getCalledFunction(), X);
688
689 // ctpop(x | -x) -> bitwidth - cttz(x, false)
690 if (Op0->hasOneUse() &&
691 match(Op0, m_c_Or(m_Value(X), m_Neg(m_Deferred(X))))) {
692 auto *Cttz = IC.Builder.CreateIntrinsic(Intrinsic::cttz, Ty,
693 {X, IC.Builder.getFalse()});
694 auto *Bw = ConstantInt::get(Ty, APInt(BitWidth, BitWidth));
695 return IC.replaceInstUsesWith(II, IC.Builder.CreateSub(Bw, Cttz));
696 }
697
698 // ctpop(~x & (x - 1)) -> cttz(x, false)
699 if (match(Op0,
701 Function *F =
702 Intrinsic::getOrInsertDeclaration(II.getModule(), Intrinsic::cttz, Ty);
703 return CallInst::Create(F, {X, IC.Builder.getFalse()});
704 }
705
706 // Zext doesn't change the number of set bits, so narrow:
707 // ctpop (zext X) --> zext (ctpop X)
708 if (match(Op0, m_OneUse(m_ZExt(m_Value(X))))) {
709 Value *NarrowPop = IC.Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, X);
710 return CastInst::Create(Instruction::ZExt, NarrowPop, Ty);
711 }
712
714 IC.computeKnownBits(Op0, Known, &II);
715
716 // If all bits are zero except for exactly one fixed bit, then the result
717 // must be 0 or 1, and we can get that answer by shifting to LSB:
718 // ctpop (X & 32) --> (X & 32) >> 5
719 // TODO: Investigate removing this as its likely unnecessary given the below
720 // `isKnownToBeAPowerOfTwo` check.
721 if ((~Known.Zero).isPowerOf2())
722 return BinaryOperator::CreateLShr(
723 Op0, ConstantInt::get(Ty, (~Known.Zero).exactLogBase2()));
724
725 // More generally we can also handle non-constant power of 2 patterns such as
726 // shl/shr(Pow2, X), (X & -X), etc... by transforming:
727 // ctpop(Pow2OrZero) --> icmp ne X, 0
728 if (IC.isKnownToBeAPowerOfTwo(Op0, /* OrZero */ true))
729 return CastInst::Create(Instruction::ZExt,
732 Ty);
733
734 // Add range attribute since known bits can't completely reflect what we know.
735 if (BitWidth != 1) {
736 ConstantRange OldRange =
737 II.getRange().value_or(ConstantRange::getFull(BitWidth));
738
739 unsigned Lower = Known.countMinPopulation();
740 unsigned Upper = Known.countMaxPopulation() + 1;
741
742 if (Lower == 0 && OldRange.contains(APInt::getZero(BitWidth)) &&
744 Lower = 1;
745
747 Range = Range.intersectWith(OldRange, ConstantRange::Unsigned);
748
749 if (Range != OldRange) {
750 II.addRangeRetAttr(Range);
751 return &II;
752 }
753 }
754
755 return nullptr;
756}
757
758/// Convert `tbl`/`tbx` intrinsics to shufflevector if the mask is constant, and
759/// at most two source operands are actually referenced.
761 bool IsExtension) {
762 // Bail out if the mask is not a constant.
763 auto *C = dyn_cast<Constant>(II.getArgOperand(II.arg_size() - 1));
764 if (!C)
765 return nullptr;
766
767 auto *RetTy = cast<FixedVectorType>(II.getType());
768 unsigned NumIndexes = RetTy->getNumElements();
769
770 // Only perform this transformation for <8 x i8> and <16 x i8> vector types.
771 if (!RetTy->getElementType()->isIntegerTy(8) ||
772 (NumIndexes != 8 && NumIndexes != 16))
773 return nullptr;
774
775 // For tbx instructions, the first argument is the "fallback" vector, which
776 // has the same length as the mask and return type.
777 unsigned int StartIndex = (unsigned)IsExtension;
778 auto *SourceTy =
779 cast<FixedVectorType>(II.getArgOperand(StartIndex)->getType());
780 // Note that the element count of each source vector does *not* need to be the
781 // same as the element count of the return type and mask! All source vectors
782 // must have the same element count as each other, though.
783 unsigned NumElementsPerSource = SourceTy->getNumElements();
784
785 // There are no tbl/tbx intrinsics for which the destination size exceeds the
786 // source size. However, our definitions of the intrinsics, at least in
787 // IntrinsicsAArch64.td, allow for arbitrary destination vector sizes, so it
788 // *could* technically happen.
789 if (NumIndexes > NumElementsPerSource)
790 return nullptr;
791
792 // The tbl/tbx intrinsics take several source operands followed by a mask
793 // operand.
794 unsigned int NumSourceOperands = II.arg_size() - 1 - (unsigned)IsExtension;
795
796 // Map input operands to shuffle indices. This also helpfully deduplicates the
797 // input arguments, in case the same value is passed as an argument multiple
798 // times.
799 SmallDenseMap<Value *, unsigned, 2> ValueToShuffleSlot;
800 Value *ShuffleOperands[2] = {PoisonValue::get(SourceTy),
801 PoisonValue::get(SourceTy)};
802
803 int Indexes[16];
804 for (unsigned I = 0; I < NumIndexes; ++I) {
805 Constant *COp = C->getAggregateElement(I);
806
807 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
808 return nullptr;
809
810 if (isa<UndefValue>(COp)) {
811 Indexes[I] = -1;
812 continue;
813 }
814
815 uint64_t Index = cast<ConstantInt>(COp)->getZExtValue();
816 // The index of the input argument that this index references (0 = first
817 // source argument, etc).
818 unsigned SourceOperandIndex = Index / NumElementsPerSource;
819 // The index of the element at that source operand.
820 unsigned SourceOperandElementIndex = Index % NumElementsPerSource;
821
822 Value *SourceOperand;
823 if (SourceOperandIndex >= NumSourceOperands) {
824 // This index is out of bounds. Map it to index into either the fallback
825 // vector (tbx) or vector of zeroes (tbl).
826 SourceOperandIndex = NumSourceOperands;
827 if (IsExtension) {
828 // For out-of-bounds indices in tbx, choose the `I`th element of the
829 // fallback.
830 SourceOperand = II.getArgOperand(0);
831 SourceOperandElementIndex = I;
832 } else {
833 // Otherwise, choose some element from the dummy vector of zeroes (we'll
834 // always choose the first).
835 SourceOperand = Constant::getNullValue(SourceTy);
836 SourceOperandElementIndex = 0;
837 }
838 } else {
839 SourceOperand = II.getArgOperand(SourceOperandIndex + StartIndex);
840 }
841
842 // The source operand may be the fallback vector, which may not have the
843 // same number of elements as the source vector. In that case, we *could*
844 // choose to extend its length with another shufflevector, but it's simpler
845 // to just bail instead.
846 if (cast<FixedVectorType>(SourceOperand->getType())->getNumElements() !=
847 NumElementsPerSource)
848 return nullptr;
849
850 // We now know the source operand referenced by this index. Make it a
851 // shufflevector operand, if it isn't already.
852 unsigned NumSlots = ValueToShuffleSlot.size();
853 // This shuffle references more than two sources, and hence cannot be
854 // represented as a shufflevector.
855 if (NumSlots == 2 && !ValueToShuffleSlot.contains(SourceOperand))
856 return nullptr;
857
858 auto [It, Inserted] =
859 ValueToShuffleSlot.try_emplace(SourceOperand, NumSlots);
860 if (Inserted)
861 ShuffleOperands[It->getSecond()] = SourceOperand;
862
863 unsigned RemappedIndex =
864 (It->getSecond() * NumElementsPerSource) + SourceOperandElementIndex;
865 Indexes[I] = RemappedIndex;
866 }
867
869 ShuffleOperands[0], ShuffleOperands[1], ArrayRef(Indexes, NumIndexes));
870 return IC.replaceInstUsesWith(II, Shuf);
871}
872
873// Returns true iff the 2 intrinsics have the same operands, limiting the
874// comparison to the first NumOperands.
875static bool haveSameOperands(const IntrinsicInst &I, const IntrinsicInst &E,
876 unsigned NumOperands) {
877 assert(I.arg_size() >= NumOperands && "Not enough operands");
878 assert(E.arg_size() >= NumOperands && "Not enough operands");
879 for (unsigned i = 0; i < NumOperands; i++)
880 if (I.getArgOperand(i) != E.getArgOperand(i))
881 return false;
882 return true;
883}
884
885// Remove trivially empty start/end intrinsic ranges, i.e. a start
886// immediately followed by an end (ignoring debuginfo or other
887// start/end intrinsics in between). As this handles only the most trivial
888// cases, tracking the nesting level is not needed:
889//
890// call @llvm.foo.start(i1 0)
891// call @llvm.foo.start(i1 0) ; This one won't be skipped: it will be removed
892// call @llvm.foo.end(i1 0)
893// call @llvm.foo.end(i1 0) ; &I
894static bool
896 std::function<bool(const IntrinsicInst &)> IsStart) {
897 // We start from the end intrinsic and scan backwards, so that InstCombine
898 // has already processed (and potentially removed) all the instructions
899 // before the end intrinsic.
900 BasicBlock::reverse_iterator BI(EndI), BE(EndI.getParent()->rend());
901 for (; BI != BE; ++BI) {
902 if (auto *I = dyn_cast<IntrinsicInst>(&*BI)) {
903 if (I->isDebugOrPseudoInst() ||
904 I->getIntrinsicID() == EndI.getIntrinsicID())
905 continue;
906 if (IsStart(*I)) {
907 if (haveSameOperands(EndI, *I, EndI.arg_size())) {
909 IC.eraseInstFromFunction(EndI);
910 return true;
911 }
912 // Skip start intrinsics that don't pair with this end intrinsic.
913 continue;
914 }
915 }
916 break;
917 }
918
919 return false;
920}
921
923 removeTriviallyEmptyRange(I, *this, [&I](const IntrinsicInst &II) {
924 // Bail out on the case where the source va_list of a va_copy is destroyed
925 // immediately by a follow-up va_end.
926 return II.getIntrinsicID() == Intrinsic::vastart ||
927 (II.getIntrinsicID() == Intrinsic::vacopy &&
928 I.getArgOperand(0) != II.getArgOperand(1));
929 });
930 return nullptr;
931}
932
934 assert(Call.arg_size() > 1 && "Need at least 2 args to swap");
935 Value *Arg0 = Call.getArgOperand(0), *Arg1 = Call.getArgOperand(1);
936 if (isa<Constant>(Arg0) && !isa<Constant>(Arg1)) {
937 Call.setArgOperand(0, Arg1);
938 Call.setArgOperand(1, Arg0);
939 AttributeList CallAttr = Call.getAttributes();
940 AttributeSet LHSAttr = CallAttr.getParamAttrs(0);
941 AttributeSet RHSAttr = CallAttr.getParamAttrs(1);
942 LLVMContext &Ctx = Call.getContext();
943 Call.setAttributes(CallAttr
944 .setAttributesAtIndex(
945 Ctx, AttributeList::FirstArgIndex + 0, RHSAttr)
946 .setAttributesAtIndex(
947 Ctx, AttributeList::FirstArgIndex + 1, LHSAttr));
948 return &Call;
949 }
950 return nullptr;
951}
952
953/// Creates a result tuple for an overflow intrinsic \p II with a given
954/// \p Result and a constant \p Overflow value.
956 Constant *Overflow) {
957 Constant *V[] = {PoisonValue::get(Result->getType()), Overflow};
958 StructType *ST = cast<StructType>(II->getType());
959 Constant *Struct = ConstantStruct::get(ST, V);
960 return InsertValueInst::Create(Struct, Result, 0);
961}
962
964InstCombinerImpl::foldIntrinsicWithOverflowCommon(IntrinsicInst *II) {
965 WithOverflowInst *WO = cast<WithOverflowInst>(II);
966 Value *OperationResult = nullptr;
967 Constant *OverflowResult = nullptr;
968 if (OptimizeOverflowCheck(WO->getBinaryOp(), WO->isSigned(), WO->getLHS(),
969 WO->getRHS(), *WO, OperationResult, OverflowResult))
970 return createOverflowTuple(WO, OperationResult, OverflowResult);
971
972 // See whether we can optimize the overflow check with assumption information.
973 for (User *U : WO->users()) {
974 if (!match(U, m_ExtractValue<1>(m_Value())))
975 continue;
976
977 for (auto &AssumeVH : AC.assumptionsFor(U)) {
978 if (!AssumeVH)
979 continue;
980 CallInst *I = cast<CallInst>(AssumeVH);
981 if (!match(I->getArgOperand(0), m_Not(m_Specific(U))))
982 continue;
983 if (!isValidAssumeForContext(I, II, /*DT=*/nullptr,
984 /*AllowEphemerals=*/true))
985 continue;
986 Value *Result =
987 Builder.CreateBinOp(WO->getBinaryOp(), WO->getLHS(), WO->getRHS());
988 Result->takeName(WO);
989 if (auto *Inst = dyn_cast<Instruction>(Result)) {
990 if (WO->isSigned())
991 Inst->setHasNoSignedWrap();
992 else
993 Inst->setHasNoUnsignedWrap();
994 }
995 return createOverflowTuple(WO, Result,
996 ConstantInt::getFalse(U->getType()));
997 }
998 }
999
1000 return nullptr;
1001}
1002
1003static bool inputDenormalIsIEEE(const Function &F, const Type *Ty) {
1004 Ty = Ty->getScalarType();
1005 return F.getDenormalMode(Ty->getFltSemantics()).Input == DenormalMode::IEEE;
1006}
1007
1008static bool inputDenormalIsDAZ(const Function &F, const Type *Ty) {
1009 Ty = Ty->getScalarType();
1010 return F.getDenormalMode(Ty->getFltSemantics()).inputsAreZero();
1011}
1012
1013/// \returns the compare predicate type if the test performed by
1014/// llvm.is.fpclass(x, \p Mask) is equivalent to fcmp o__ x, 0.0 with the
1015/// floating-point environment assumed for \p F for type \p Ty
1017 const Function &F, Type *Ty) {
1018 switch (static_cast<unsigned>(Mask)) {
1019 case fcZero:
1020 if (inputDenormalIsIEEE(F, Ty))
1021 return FCmpInst::FCMP_OEQ;
1022 break;
1023 case fcZero | fcSubnormal:
1024 if (inputDenormalIsDAZ(F, Ty))
1025 return FCmpInst::FCMP_OEQ;
1026 break;
1027 case fcPositive | fcNegZero:
1028 if (inputDenormalIsIEEE(F, Ty))
1029 return FCmpInst::FCMP_OGE;
1030 break;
1032 if (inputDenormalIsDAZ(F, Ty))
1033 return FCmpInst::FCMP_OGE;
1034 break;
1036 if (inputDenormalIsIEEE(F, Ty))
1037 return FCmpInst::FCMP_OGT;
1038 break;
1039 case fcNegative | fcPosZero:
1040 if (inputDenormalIsIEEE(F, Ty))
1041 return FCmpInst::FCMP_OLE;
1042 break;
1044 if (inputDenormalIsDAZ(F, Ty))
1045 return FCmpInst::FCMP_OLE;
1046 break;
1048 if (inputDenormalIsIEEE(F, Ty))
1049 return FCmpInst::FCMP_OLT;
1050 break;
1051 case fcPosNormal | fcPosInf:
1052 if (inputDenormalIsDAZ(F, Ty))
1053 return FCmpInst::FCMP_OGT;
1054 break;
1055 case fcNegNormal | fcNegInf:
1056 if (inputDenormalIsDAZ(F, Ty))
1057 return FCmpInst::FCMP_OLT;
1058 break;
1059 case ~fcZero & ~fcNan:
1060 if (inputDenormalIsIEEE(F, Ty))
1061 return FCmpInst::FCMP_ONE;
1062 break;
1063 case ~(fcZero | fcSubnormal) & ~fcNan:
1064 if (inputDenormalIsDAZ(F, Ty))
1065 return FCmpInst::FCMP_ONE;
1066 break;
1067 default:
1068 break;
1069 }
1070
1072}
1073
1074Instruction *InstCombinerImpl::foldIntrinsicIsFPClass(IntrinsicInst &II) {
1075 Value *Src0 = II.getArgOperand(0);
1076 Value *Src1 = II.getArgOperand(1);
1077 const ConstantInt *CMask = cast<ConstantInt>(Src1);
1078 FPClassTest Mask = static_cast<FPClassTest>(CMask->getZExtValue());
1079 const bool IsUnordered = (Mask & fcNan) == fcNan;
1080 const bool IsOrdered = (Mask & fcNan) == fcNone;
1081 const FPClassTest OrderedMask = Mask & ~fcNan;
1082 const FPClassTest OrderedInvertedMask = ~OrderedMask & ~fcNan;
1083
1084 const bool IsStrict =
1085 II.getFunction()->getAttributes().hasFnAttr(Attribute::StrictFP);
1086
1087 Value *FNegSrc;
1088 // is.fpclass (fneg x), mask -> is.fpclass x, (fneg mask)
1089 if (match(Src0, m_FNeg(m_Value(FNegSrc))))
1090 return CallInst::Create(
1091 II.getCalledFunction(),
1092 {FNegSrc, ConstantInt::get(Src1->getType(), fneg(Mask))});
1093
1094 Value *FAbsSrc;
1095 if (match(Src0, m_FAbs(m_Value(FAbsSrc))))
1096 return CallInst::Create(
1097 II.getCalledFunction(),
1098 {FAbsSrc, ConstantInt::get(Src1->getType(), inverse_fabs(Mask))});
1099
1100 if ((OrderedMask == fcInf || OrderedInvertedMask == fcInf) &&
1101 (IsOrdered || IsUnordered) && !IsStrict) {
1102 // is.fpclass(x, fcInf) -> fcmp oeq fabs(x), +inf
1103 // is.fpclass(x, ~fcInf) -> fcmp one fabs(x), +inf
1104 // is.fpclass(x, fcInf|fcNan) -> fcmp ueq fabs(x), +inf
1105 // is.fpclass(x, ~(fcInf|fcNan)) -> fcmp une fabs(x), +inf
1107 FCmpInst::Predicate Pred =
1108 IsUnordered ? FCmpInst::FCMP_UEQ : FCmpInst::FCMP_OEQ;
1109 if (OrderedInvertedMask == fcInf)
1110 Pred = IsUnordered ? FCmpInst::FCMP_UNE : FCmpInst::FCMP_ONE;
1111
1112 Value *Fabs = Builder.CreateFAbs(Src0);
1113 Value *CmpInf = Builder.CreateFCmp(Pred, Fabs, Inf);
1114 CmpInf->takeName(&II);
1115 return replaceInstUsesWith(II, CmpInf);
1116 }
1117
1118 if ((OrderedMask == fcPosInf || OrderedMask == fcNegInf) &&
1119 (IsOrdered || IsUnordered) && !IsStrict) {
1120 // is.fpclass(x, fcPosInf) -> fcmp oeq x, +inf
1121 // is.fpclass(x, fcNegInf) -> fcmp oeq x, -inf
1122 // is.fpclass(x, fcPosInf|fcNan) -> fcmp ueq x, +inf
1123 // is.fpclass(x, fcNegInf|fcNan) -> fcmp ueq x, -inf
1124 Constant *Inf =
1125 ConstantFP::getInfinity(Src0->getType(), OrderedMask == fcNegInf);
1126 Value *EqInf = IsUnordered ? Builder.CreateFCmpUEQ(Src0, Inf)
1127 : Builder.CreateFCmpOEQ(Src0, Inf);
1128
1129 EqInf->takeName(&II);
1130 return replaceInstUsesWith(II, EqInf);
1131 }
1132
1133 if ((OrderedInvertedMask == fcPosInf || OrderedInvertedMask == fcNegInf) &&
1134 (IsOrdered || IsUnordered) && !IsStrict) {
1135 // is.fpclass(x, ~fcPosInf) -> fcmp one x, +inf
1136 // is.fpclass(x, ~fcNegInf) -> fcmp one x, -inf
1137 // is.fpclass(x, ~fcPosInf|fcNan) -> fcmp une x, +inf
1138 // is.fpclass(x, ~fcNegInf|fcNan) -> fcmp une x, -inf
1140 OrderedInvertedMask == fcNegInf);
1141 Value *NeInf = IsUnordered ? Builder.CreateFCmpUNE(Src0, Inf)
1142 : Builder.CreateFCmpONE(Src0, Inf);
1143 NeInf->takeName(&II);
1144 return replaceInstUsesWith(II, NeInf);
1145 }
1146
1147 if (Mask == fcNan && !IsStrict) {
1148 // Equivalent of isnan. Replace with standard fcmp if we don't care about FP
1149 // exceptions.
1150 Value *IsNan =
1151 Builder.CreateFCmpUNO(Src0, ConstantFP::getZero(Src0->getType()));
1152 IsNan->takeName(&II);
1153 return replaceInstUsesWith(II, IsNan);
1154 }
1155
1156 if (Mask == (~fcNan & fcAllFlags) && !IsStrict) {
1157 // Equivalent of !isnan. Replace with standard fcmp.
1158 Value *FCmp =
1159 Builder.CreateFCmpORD(Src0, ConstantFP::getZero(Src0->getType()));
1160 FCmp->takeName(&II);
1161 return replaceInstUsesWith(II, FCmp);
1162 }
1163
1165
1166 // Try to replace with an fcmp with 0
1167 //
1168 // is.fpclass(x, fcZero) -> fcmp oeq x, 0.0
1169 // is.fpclass(x, fcZero | fcNan) -> fcmp ueq x, 0.0
1170 // is.fpclass(x, ~fcZero & ~fcNan) -> fcmp one x, 0.0
1171 // is.fpclass(x, ~fcZero) -> fcmp une x, 0.0
1172 //
1173 // is.fpclass(x, fcPosSubnormal | fcPosNormal | fcPosInf) -> fcmp ogt x, 0.0
1174 // is.fpclass(x, fcPositive | fcNegZero) -> fcmp oge x, 0.0
1175 //
1176 // is.fpclass(x, fcNegSubnormal | fcNegNormal | fcNegInf) -> fcmp olt x, 0.0
1177 // is.fpclass(x, fcNegative | fcPosZero) -> fcmp ole x, 0.0
1178 //
1179 if (!IsStrict && (IsOrdered || IsUnordered) &&
1180 (PredType = fpclassTestIsFCmp0(OrderedMask, *II.getFunction(),
1181 Src0->getType())) !=
1184 // Equivalent of == 0.
1185 Value *FCmp = Builder.CreateFCmp(
1186 IsUnordered ? FCmpInst::getUnorderedPredicate(PredType) : PredType,
1187 Src0, Zero);
1188
1189 FCmp->takeName(&II);
1190 return replaceInstUsesWith(II, FCmp);
1191 }
1192
1193 KnownFPClass Known =
1194 computeKnownFPClass(Src0, Mask, SQ.getWithInstruction(&II));
1195
1196 // If none of the tests which can return false are possible, fold to true.
1197 // fp_class (nnan x), ~(qnan|snan) -> true
1198 // fp_class (ninf x), ~(ninf|pinf) -> true
1199 if (Known.isKnownAlways(Mask))
1200 return replaceInstUsesWith(II, ConstantInt::get(II.getType(), true));
1201
1202 // Clear test bits we know must be false from the source value.
1203 // fp_class (nnan x), qnan|snan|other -> fp_class (nnan x), other
1204 // fp_class (ninf x), ninf|pinf|other -> fp_class (ninf x), other
1205 if ((Mask & Known.KnownFPClasses) != Mask) {
1206 II.setArgOperand(
1207 1, ConstantInt::get(Src1->getType(), Mask & Known.KnownFPClasses));
1208 return &II;
1209 }
1210
1211 return nullptr;
1212}
1213
1214static std::optional<bool> getKnownSign(Value *Op, const SimplifyQuery &SQ) {
1216 if (Known.isNonNegative())
1217 return false;
1218 if (Known.isNegative())
1219 return true;
1220
1221 Value *X, *Y;
1222 if (match(Op, m_NSWSub(m_Value(X), m_Value(Y))))
1224
1225 return std::nullopt;
1226}
1227
1228static std::optional<bool> getKnownSignOrZero(Value *Op,
1229 const SimplifyQuery &SQ) {
1230 if (std::optional<bool> Sign = getKnownSign(Op, SQ))
1231 return Sign;
1232
1233 Value *X, *Y;
1234 if (match(Op, m_NSWSub(m_Value(X), m_Value(Y))))
1236
1237 return std::nullopt;
1238}
1239
1240/// Return true if two values \p Op0 and \p Op1 are known to have the same sign.
1241static bool signBitMustBeTheSame(Value *Op0, Value *Op1,
1242 const SimplifyQuery &SQ) {
1243 std::optional<bool> Known1 = getKnownSign(Op1, SQ);
1244 if (!Known1)
1245 return false;
1246 std::optional<bool> Known0 = getKnownSign(Op0, SQ);
1247 if (!Known0)
1248 return false;
1249 return *Known0 == *Known1;
1250}
1251
1252// Determines if ldexp(ldexp(x, a), b) -> ldexp(x, sadd.sat(a, b)) is safe.
1253//
1254// This is true if, when the add saturates, the resulting ldexp is guaranteed to
1255// produce 0 or inf.
1256static bool ldexpSaturatingAddIsSafe(Type *FpTy, Type *ExpTy) {
1257 const fltSemantics &FltSem = FpTy->getScalarType()->getFltSemantics();
1258 if (!APFloat::semanticsHasInf(FltSem))
1259 return false;
1260
1261 // Cap ExpBits at 32 because scalbn takes an int. This is sufficient for any
1262 // reasonable fp type (for example, `double` only has 11 exponent bits).
1263 unsigned ExpBits = std::min(ExpTy->getScalarSizeInBits(), 32u);
1264 int SignedMax = static_cast<int>(maxIntN(ExpBits));
1265 int SignedMin = static_cast<int>(minIntN(ExpBits));
1266 APFloat ScaledUp = scalbn(APFloat::getSmallest(FltSem), SignedMax,
1268 APFloat ScaledDown = scalbn(APFloat::getLargest(FltSem), SignedMin,
1270 return ScaledUp.isInfinity() && ScaledDown.isZero();
1271}
1272
1273/// Try to canonicalize min/max(X + C0, C1) as min/max(X, C1 - C0) + C0. This
1274/// can trigger other combines.
1276 InstCombiner::BuilderTy &Builder) {
1277 Intrinsic::ID MinMaxID = II->getIntrinsicID();
1278 assert((MinMaxID == Intrinsic::smax || MinMaxID == Intrinsic::smin ||
1279 MinMaxID == Intrinsic::umax || MinMaxID == Intrinsic::umin) &&
1280 "Expected a min or max intrinsic");
1281
1282 // TODO: Match vectors with undef elements, but undef may not propagate.
1283 Value *Op0 = II->getArgOperand(0), *Op1 = II->getArgOperand(1);
1284 Value *X;
1285 const APInt *C0, *C1;
1286 if (!match(Op0, m_OneUse(m_Add(m_Value(X), m_APInt(C0)))) ||
1287 !match(Op1, m_APInt(C1)))
1288 return nullptr;
1289
1290 // Check for necessary no-wrap and overflow constraints.
1291 bool IsSigned = MinMaxID == Intrinsic::smax || MinMaxID == Intrinsic::smin;
1292 auto *Add = cast<BinaryOperator>(Op0);
1293 if ((IsSigned && !Add->hasNoSignedWrap()) ||
1294 (!IsSigned && !Add->hasNoUnsignedWrap()))
1295 return nullptr;
1296
1297 // If the constant difference overflows, then instsimplify should reduce the
1298 // min/max to the add or C1.
1299 bool Overflow;
1300 APInt CDiff =
1301 IsSigned ? C1->ssub_ov(*C0, Overflow) : C1->usub_ov(*C0, Overflow);
1302 assert(!Overflow && "Expected simplify of min/max");
1303
1304 // min/max (add X, C0), C1 --> add (min/max X, C1 - C0), C0
1305 // Note: the "mismatched" no-overflow setting does not propagate.
1306 Constant *NewMinMaxC = ConstantInt::get(II->getType(), CDiff);
1307 Value *NewMinMax = Builder.CreateBinaryIntrinsic(MinMaxID, X, NewMinMaxC);
1308 return IsSigned ? BinaryOperator::CreateNSWAdd(NewMinMax, Add->getOperand(1))
1309 : BinaryOperator::CreateNUWAdd(NewMinMax, Add->getOperand(1));
1310}
1311/// Match a sadd_sat or ssub_sat which is using min/max to clamp the value.
1312Instruction *InstCombinerImpl::matchSAddSubSat(IntrinsicInst &MinMax1) {
1313 Type *Ty = MinMax1.getType();
1314
1315 // We are looking for a tree of:
1316 // max(INT_MIN, min(INT_MAX, add(sext(A), sext(B))))
1317 // Where the min and max could be reversed
1318 Instruction *MinMax2;
1319 BinaryOperator *AddSub;
1320 const APInt *MinValue, *MaxValue;
1321 if (match(&MinMax1, m_SMin(m_Instruction(MinMax2), m_APInt(MaxValue)))) {
1322 if (!match(MinMax2, m_SMax(m_BinOp(AddSub), m_APInt(MinValue))))
1323 return nullptr;
1324 } else if (match(&MinMax1,
1325 m_SMax(m_Instruction(MinMax2), m_APInt(MinValue)))) {
1326 if (!match(MinMax2, m_SMin(m_BinOp(AddSub), m_APInt(MaxValue))))
1327 return nullptr;
1328 } else
1329 return nullptr;
1330
1331 // Check that the constants clamp a saturate, and that the new type would be
1332 // sensible to convert to.
1333 if (!(*MaxValue + 1).isPowerOf2() || -*MinValue != *MaxValue + 1)
1334 return nullptr;
1335 // In what bitwidth can this be treated as saturating arithmetics?
1336 unsigned NewBitWidth = (*MaxValue + 1).logBase2() + 1;
1337 // FIXME: This isn't quite right for vectors, but using the scalar type is a
1338 // good first approximation for what should be done there.
1339 if (!shouldChangeType(Ty->getScalarType()->getIntegerBitWidth(), NewBitWidth))
1340 return nullptr;
1341
1342 // Also make sure that the inner min/max and the add/sub have one use.
1343 if (!MinMax2->hasOneUse() || !AddSub->hasOneUse())
1344 return nullptr;
1345
1346 // Create the new type (which can be a vector type)
1347 Type *NewTy = Ty->getWithNewBitWidth(NewBitWidth);
1348
1349 Intrinsic::ID IntrinsicID;
1350 if (AddSub->getOpcode() == Instruction::Add)
1351 IntrinsicID = Intrinsic::sadd_sat;
1352 else if (AddSub->getOpcode() == Instruction::Sub)
1353 IntrinsicID = Intrinsic::ssub_sat;
1354 else
1355 return nullptr;
1356
1357 // The two operands of the add/sub must be nsw-truncatable to the NewTy. This
1358 // is usually achieved via a sext from a smaller type.
1359 if (ComputeMaxSignificantBits(AddSub->getOperand(0), AddSub) > NewBitWidth ||
1360 ComputeMaxSignificantBits(AddSub->getOperand(1), AddSub) > NewBitWidth)
1361 return nullptr;
1362
1363 // Finally create and return the sat intrinsic, truncated to the new type
1364 Value *AT = Builder.CreateTrunc(AddSub->getOperand(0), NewTy);
1365 Value *BT = Builder.CreateTrunc(AddSub->getOperand(1), NewTy);
1366 Value *Sat = Builder.CreateIntrinsic(IntrinsicID, NewTy, {AT, BT});
1367 return CastInst::Create(Instruction::SExt, Sat, Ty);
1368}
1369
1370
1371/// If we have a clamp pattern like max (min X, 42), 41 -- where the output
1372/// can only be one of two possible constant values -- turn that into a select
1373/// of constants.
1375 InstCombiner::BuilderTy &Builder) {
1376 Value *I0 = II->getArgOperand(0), *I1 = II->getArgOperand(1);
1377 Value *X;
1378 const APInt *C0, *C1;
1379 if (!match(I1, m_APInt(C1)) || !I0->hasOneUse())
1380 return nullptr;
1381
1383 switch (II->getIntrinsicID()) {
1384 case Intrinsic::smax:
1385 if (match(I0, m_SMin(m_Value(X), m_APInt(C0))) && *C0 == *C1 + 1)
1386 Pred = ICmpInst::ICMP_SGT;
1387 break;
1388 case Intrinsic::smin:
1389 if (match(I0, m_SMax(m_Value(X), m_APInt(C0))) && *C1 == *C0 + 1)
1390 Pred = ICmpInst::ICMP_SLT;
1391 break;
1392 case Intrinsic::umax:
1393 if (match(I0, m_UMin(m_Value(X), m_APInt(C0))) && *C0 == *C1 + 1)
1394 Pred = ICmpInst::ICMP_UGT;
1395 break;
1396 case Intrinsic::umin:
1397 if (match(I0, m_UMax(m_Value(X), m_APInt(C0))) && *C1 == *C0 + 1)
1398 Pred = ICmpInst::ICMP_ULT;
1399 break;
1400 default:
1401 llvm_unreachable("Expected min/max intrinsic");
1402 }
1403 if (Pred == CmpInst::BAD_ICMP_PREDICATE)
1404 return nullptr;
1405
1406 // max (min X, 42), 41 --> X > 41 ? 42 : 41
1407 // min (max X, 42), 43 --> X < 43 ? 42 : 43
1408 Value *Cmp = Builder.CreateICmp(Pred, X, I1);
1409 return SelectInst::Create(Cmp, ConstantInt::get(II->getType(), *C0), I1);
1410}
1411
1412/// If this min/max has a constant operand and an operand that is a matching
1413/// min/max with a constant operand, constant-fold the 2 constant operands.
1415 IRBuilderBase &Builder,
1416 const SimplifyQuery &SQ) {
1417 Intrinsic::ID MinMaxID = II->getIntrinsicID();
1418 auto *LHS = dyn_cast<MinMaxIntrinsic>(II->getArgOperand(0));
1419 if (!LHS)
1420 return nullptr;
1421
1422 Constant *C0, *C1;
1423 if (!match(LHS->getArgOperand(1), m_ImmConstant(C0)) ||
1424 !match(II->getArgOperand(1), m_ImmConstant(C1)))
1425 return nullptr;
1426
1427 // max (max X, C0), C1 --> max X, (max C0, C1)
1428 // min (min X, C0), C1 --> min X, (min C0, C1)
1429 // umax (smax X, nneg C0), nneg C1 --> smax X, (umax C0, C1)
1430 // smin (umin X, nneg C0), nneg C1 --> umin X, (smin C0, C1)
1431 Intrinsic::ID InnerMinMaxID = LHS->getIntrinsicID();
1432 if (InnerMinMaxID != MinMaxID &&
1433 !(((MinMaxID == Intrinsic::umax && InnerMinMaxID == Intrinsic::smax) ||
1434 (MinMaxID == Intrinsic::smin && InnerMinMaxID == Intrinsic::umin)) &&
1435 isKnownNonNegative(C0, SQ) && isKnownNonNegative(C1, SQ)))
1436 return nullptr;
1437
1439 Value *CondC = Builder.CreateICmp(Pred, C0, C1);
1440 Value *NewC = Builder.CreateSelect(CondC, C0, C1);
1441 return Builder.CreateIntrinsic(InnerMinMaxID, II->getType(),
1442 {LHS->getArgOperand(0), NewC});
1443}
1444
1445/// If this min/max has a matching min/max operand with a constant, try to push
1446/// the constant operand into this instruction. This can enable more folds.
1447static Instruction *
1449 InstCombiner::BuilderTy &Builder) {
1450 // Match and capture a min/max operand candidate.
1451 Value *X, *Y;
1452 Constant *C;
1453 Instruction *Inner;
1455 m_Instruction(Inner),
1457 m_Value(Y))))
1458 return nullptr;
1459
1460 // The inner op must match. Check for constants to avoid infinite loops.
1461 Intrinsic::ID MinMaxID = II->getIntrinsicID();
1462 auto *InnerMM = dyn_cast<IntrinsicInst>(Inner);
1463 if (!InnerMM || InnerMM->getIntrinsicID() != MinMaxID ||
1465 return nullptr;
1466
1467 // max (max X, C), Y --> max (max X, Y), C
1469 MinMaxID, II->getType());
1470 Value *NewInner = Builder.CreateBinaryIntrinsic(MinMaxID, X, Y);
1471 NewInner->takeName(Inner);
1472 return CallInst::Create(MinMax, {NewInner, C});
1473}
1474
1475/// Reduce a sequence of min/max intrinsics with a common operand.
1477 // Match 3 of the same min/max ops. Example: umin(umin(), umin()).
1478 auto *LHS = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1479 auto *RHS = dyn_cast<IntrinsicInst>(II->getArgOperand(1));
1480 Intrinsic::ID MinMaxID = II->getIntrinsicID();
1481 if (!LHS || !RHS || LHS->getIntrinsicID() != MinMaxID ||
1482 RHS->getIntrinsicID() != MinMaxID ||
1483 (!LHS->hasOneUse() && !RHS->hasOneUse()))
1484 return nullptr;
1485
1486 Value *A = LHS->getArgOperand(0);
1487 Value *B = LHS->getArgOperand(1);
1488 Value *C = RHS->getArgOperand(0);
1489 Value *D = RHS->getArgOperand(1);
1490
1491 // Look for a common operand.
1492 Value *MinMaxOp = nullptr;
1493 Value *ThirdOp = nullptr;
1494 if (LHS->hasOneUse()) {
1495 // If the LHS is only used in this chain and the RHS is used outside of it,
1496 // reuse the RHS min/max because that will eliminate the LHS.
1497 if (D == A || C == A) {
1498 // min(min(a, b), min(c, a)) --> min(min(c, a), b)
1499 // min(min(a, b), min(a, d)) --> min(min(a, d), b)
1500 MinMaxOp = RHS;
1501 ThirdOp = B;
1502 } else if (D == B || C == B) {
1503 // min(min(a, b), min(c, b)) --> min(min(c, b), a)
1504 // min(min(a, b), min(b, d)) --> min(min(b, d), a)
1505 MinMaxOp = RHS;
1506 ThirdOp = A;
1507 }
1508 } else {
1509 assert(RHS->hasOneUse() && "Expected one-use operand");
1510 // Reuse the LHS. This will eliminate the RHS.
1511 if (D == A || D == B) {
1512 // min(min(a, b), min(c, a)) --> min(min(a, b), c)
1513 // min(min(a, b), min(c, b)) --> min(min(a, b), c)
1514 MinMaxOp = LHS;
1515 ThirdOp = C;
1516 } else if (C == A || C == B) {
1517 // min(min(a, b), min(b, d)) --> min(min(a, b), d)
1518 // min(min(a, b), min(c, b)) --> min(min(a, b), d)
1519 MinMaxOp = LHS;
1520 ThirdOp = D;
1521 }
1522 }
1523
1524 if (!MinMaxOp || !ThirdOp)
1525 return nullptr;
1526
1527 Module *Mod = II->getModule();
1528 Function *MinMax =
1529 Intrinsic::getOrInsertDeclaration(Mod, MinMaxID, II->getType());
1530 return CallInst::Create(MinMax, { MinMaxOp, ThirdOp });
1531}
1532
1533/// If all arguments of the intrinsic are unary shuffles with the same mask,
1534/// try to shuffle after the intrinsic.
1537 if (!II->getType()->isVectorTy() ||
1538 !isTriviallyVectorizable(II->getIntrinsicID()) ||
1539 !II->getCalledFunction()->isSpeculatable())
1540 return nullptr;
1541
1542 Value *X;
1543 Constant *C;
1544 ArrayRef<int> Mask;
1545 auto *NonConstArg = find_if_not(II->args(), [&II](Use &Arg) {
1546 return isa<Constant>(Arg.get()) ||
1547 isVectorIntrinsicWithScalarOpAtArg(II->getIntrinsicID(),
1548 Arg.getOperandNo(), nullptr);
1549 });
1550 if (!NonConstArg ||
1551 !match(NonConstArg, m_Shuffle(m_Value(X), m_Poison(), m_Mask(Mask))))
1552 return nullptr;
1553
1554 // At least 1 operand must be a shuffle with 1 use because we are creating 2
1555 // instructions.
1556 if (none_of(II->args(), match_fn(m_OneUse(m_Shuffle(m_Value(), m_Value())))))
1557 return nullptr;
1558
1559 // See if all arguments are shuffled with the same mask.
1561 Type *SrcTy = X->getType();
1562 for (Use &Arg : II->args()) {
1563 if (isVectorIntrinsicWithScalarOpAtArg(II->getIntrinsicID(),
1564 Arg.getOperandNo(), nullptr))
1565 NewArgs.push_back(Arg);
1566 else if (match(&Arg,
1567 m_Shuffle(m_Value(X), m_Poison(), m_SpecificMask(Mask))) &&
1568 X->getType() == SrcTy)
1569 NewArgs.push_back(X);
1570 else if (match(&Arg, m_ImmConstant(C))) {
1571 // If it's a constant, try find the constant that would be shuffled to C.
1572 if (Constant *ShuffledC =
1573 unshuffleConstant(Mask, C, cast<VectorType>(SrcTy)))
1574 NewArgs.push_back(ShuffledC);
1575 else
1576 return nullptr;
1577 } else
1578 return nullptr;
1579 }
1580
1581 // intrinsic (shuf X, M), (shuf Y, M), ... --> shuf (intrinsic X, Y, ...), M
1582 Instruction *FPI = isa<FPMathOperator>(II) ? II : nullptr;
1583 // Result type might be a different vector width.
1584 // TODO: Check that the result type isn't widened?
1585 VectorType *ResTy =
1586 VectorType::get(II->getType()->getScalarType(), cast<VectorType>(SrcTy));
1587 Value *NewIntrinsic =
1588 Builder.CreateIntrinsic(ResTy, II->getIntrinsicID(), NewArgs, FPI);
1589 return new ShuffleVectorInst(NewIntrinsic, Mask);
1590}
1591
1592/// If all arguments of the intrinsic are reverses, try to pull the reverse
1593/// after the intrinsic.
1595 if (!II->getType()->isVectorTy() ||
1596 !isTriviallyVectorizable(II->getIntrinsicID()))
1597 return nullptr;
1598
1599 // At least 1 operand must be a reverse with 1 use because we are creating 2
1600 // instructions.
1601 if (none_of(II->args(), [](Value *V) {
1602 return match(V, m_OneUse(m_VecReverse(m_Value())));
1603 }))
1604 return nullptr;
1605
1606 Value *X;
1607 Constant *C;
1608 SmallVector<Value *> NewArgs;
1609 for (Use &Arg : II->args()) {
1610 if (isVectorIntrinsicWithScalarOpAtArg(II->getIntrinsicID(),
1611 Arg.getOperandNo(), nullptr))
1612 NewArgs.push_back(Arg);
1613 else if (match(&Arg, m_VecReverse(m_Value(X))))
1614 NewArgs.push_back(X);
1615 else if (isSplatValue(Arg))
1616 NewArgs.push_back(Arg);
1617 else if (match(&Arg, m_ImmConstant(C)))
1618 NewArgs.push_back(Builder.CreateVectorReverse(C));
1619 else
1620 return nullptr;
1621 }
1622
1623 // intrinsic (reverse X), (reverse Y), ... --> reverse (intrinsic X, Y, ...)
1624 Instruction *FPI = isa<FPMathOperator>(II) ? II : nullptr;
1625 Value *NewIntrinsic = Builder.CreateIntrinsic(
1626 II->getType(), II->getIntrinsicID(), NewArgs, FPI);
1627 return Builder.CreateVectorReverse(NewIntrinsic);
1628}
1629
1630/// Fold the following cases and accepts bswap and bitreverse intrinsics:
1631/// bswap(logic_op(bswap(x), y)) --> logic_op(x, bswap(y))
1632/// bswap(logic_op(bswap(x), bswap(y))) --> logic_op(x, y) (ignores multiuse)
1633template <Intrinsic::ID IntrID>
1635 InstCombiner::BuilderTy &Builder) {
1636 static_assert(IntrID == Intrinsic::bswap || IntrID == Intrinsic::bitreverse,
1637 "This helper only supports BSWAP and BITREVERSE intrinsics");
1638
1639 Value *X, *Y;
1640 // Find bitwise logic op. Check that it is a BinaryOperator explicitly so we
1641 // don't match ConstantExpr that aren't meaningful for this transform.
1644 Value *OldReorderX, *OldReorderY;
1646
1647 // If both X and Y are bswap/bitreverse, the transform reduces the number
1648 // of instructions even if there's multiuse.
1649 // If only one operand is bswap/bitreverse, we need to ensure the operand
1650 // have only one use.
1651 if (match(X, m_Intrinsic<IntrID>(m_Value(OldReorderX))) &&
1652 match(Y, m_Intrinsic<IntrID>(m_Value(OldReorderY)))) {
1653 return BinaryOperator::Create(Op, OldReorderX, OldReorderY);
1654 }
1655
1656 if (match(X, m_OneUse(m_Intrinsic<IntrID>(m_Value(OldReorderX))))) {
1657 Value *NewReorder = Builder.CreateUnaryIntrinsic(IntrID, Y);
1658 return BinaryOperator::Create(Op, OldReorderX, NewReorder);
1659 }
1660
1661 if (match(Y, m_OneUse(m_Intrinsic<IntrID>(m_Value(OldReorderY))))) {
1662 Value *NewReorder = Builder.CreateUnaryIntrinsic(IntrID, X);
1663 return BinaryOperator::Create(Op, NewReorder, OldReorderY);
1664 }
1665 }
1666 return nullptr;
1667}
1668
1669/// Helper to match idempotent binary intrinsics, namely, intrinsics where
1670/// `f(f(x, y), y) == f(x, y)` holds.
1672 switch (IID) {
1673 case Intrinsic::smax:
1674 case Intrinsic::smin:
1675 case Intrinsic::umax:
1676 case Intrinsic::umin:
1677 case Intrinsic::maximum:
1678 case Intrinsic::minimum:
1679 case Intrinsic::maximumnum:
1680 case Intrinsic::minimumnum:
1681 case Intrinsic::maxnum:
1682 case Intrinsic::minnum:
1683 return true;
1684 default:
1685 return false;
1686 }
1687}
1688
1689/// Attempt to simplify value-accumulating recurrences of kind:
1690/// %umax.acc = phi i8 [ %umax, %backedge ], [ %a, %entry ]
1691/// %umax = call i8 @llvm.umax.i8(i8 %umax.acc, i8 %b)
1692/// And let the idempotent binary intrinsic be hoisted, when the operands are
1693/// known to be loop-invariant.
1695 IntrinsicInst *II) {
1696 PHINode *PN;
1697 Value *Init, *OtherOp;
1698
1699 // A binary intrinsic recurrence with loop-invariant operands is equivalent to
1700 // `call @llvm.binary.intrinsic(Init, OtherOp)`.
1701 auto IID = II->getIntrinsicID();
1702 if (!isIdempotentBinaryIntrinsic(IID) ||
1704 !IC.getDominatorTree().dominates(OtherOp, PN))
1705 return nullptr;
1706
1707 auto *InvariantBinaryInst =
1708 IC.Builder.CreateBinaryIntrinsic(IID, Init, OtherOp);
1709 if (isa<FPMathOperator>(InvariantBinaryInst))
1710 cast<Instruction>(InvariantBinaryInst)->copyFastMathFlags(II);
1711 return InvariantBinaryInst;
1712}
1713
1714static Value *simplifyReductionOperand(Value *Arg, bool CanReorderLanes) {
1715 if (!CanReorderLanes)
1716 return nullptr;
1717
1718 Value *V;
1719 if (match(Arg, m_VecReverse(m_Value(V))))
1720 return V;
1721
1722 ArrayRef<int> Mask;
1723 if (!isa<FixedVectorType>(Arg->getType()) ||
1724 !match(Arg, m_Shuffle(m_Value(V), m_Undef(), m_Mask(Mask))) ||
1725 !cast<ShuffleVectorInst>(Arg)->isSingleSource())
1726 return nullptr;
1727
1728 int Sz = Mask.size();
1729 SmallBitVector UsedIndices(Sz);
1730 for (int Idx : Mask) {
1731 if (Idx == PoisonMaskElem || UsedIndices.test(Idx))
1732 return nullptr;
1733 UsedIndices.set(Idx);
1734 }
1735
1736 // Can remove shuffle iff just shuffled elements, no repeats, undefs, or
1737 // other changes.
1738 return UsedIndices.all() ? V : nullptr;
1739}
1740
1741/// Fold an unsigned minimum of trailing or leading zero bits counts:
1742/// umin(cttz(CtOp1, ZeroUndef), ConstOp) --> cttz(CtOp1 | (1 << ConstOp))
1743/// umin(ctlz(CtOp1, ZeroUndef), ConstOp) --> ctlz(CtOp1 | (SignedMin
1744/// >> ConstOp))
1745/// umin(cttz(CtOp1), cttz(CtOp2)) --> cttz(CtOp1 | CtOp2)
1746/// umin(ctlz(CtOp1), ctlz(CtOp2)) --> ctlz(CtOp1 | CtOp2)
1747template <Intrinsic::ID IntrID>
1748static Value *
1750 const DataLayout &DL,
1751 InstCombiner::BuilderTy &Builder) {
1752 static_assert(IntrID == Intrinsic::cttz || IntrID == Intrinsic::ctlz,
1753 "This helper only supports cttz and ctlz intrinsics");
1754
1755 Value *CtOp1, *CtOp2;
1756 Value *ZeroUndef1, *ZeroUndef2;
1757 if (!match(I0, m_OneUse(
1758 m_Intrinsic<IntrID>(m_Value(CtOp1), m_Value(ZeroUndef1)))))
1759 return nullptr;
1760
1761 if (match(I1,
1762 m_OneUse(m_Intrinsic<IntrID>(m_Value(CtOp2), m_Value(ZeroUndef2)))))
1763 return Builder.CreateBinaryIntrinsic(
1764 IntrID, Builder.CreateOr(CtOp1, CtOp2),
1765 Builder.CreateOr(ZeroUndef1, ZeroUndef2));
1766
1767 unsigned BitWidth = I1->getType()->getScalarSizeInBits();
1768 auto LessBitWidth = [BitWidth](auto &C) { return C.ult(BitWidth); };
1769 if (!match(I1, m_CheckedInt(LessBitWidth)))
1770 // We have a constant >= BitWidth (which can be handled by CVP)
1771 // or a non-splat vector with elements < and >= BitWidth
1772 return nullptr;
1773
1774 Type *Ty = I1->getType();
1776 IntrID == Intrinsic::cttz ? Instruction::Shl : Instruction::LShr,
1777 IntrID == Intrinsic::cttz
1778 ? ConstantInt::get(Ty, 1)
1779 : ConstantInt::get(Ty, APInt::getSignedMinValue(BitWidth)),
1780 cast<Constant>(I1), DL);
1781 return Builder.CreateBinaryIntrinsic(
1782 IntrID, Builder.CreateOr(CtOp1, NewConst),
1783 ConstantInt::getTrue(ZeroUndef1->getType()));
1784}
1785
1786/// Return whether "X LOp (Y ROp Z)" is always equal to
1787/// "(X LOp Y) ROp (X LOp Z)".
1789 bool HasNSW, Intrinsic::ID ROp) {
1790 switch (ROp) {
1791 case Intrinsic::umax:
1792 case Intrinsic::umin:
1793 if (HasNUW && LOp == Instruction::Add)
1794 return true;
1795 if (HasNUW && LOp == Instruction::Shl)
1796 return true;
1797 return false;
1798 case Intrinsic::smax:
1799 case Intrinsic::smin:
1800 return HasNSW && LOp == Instruction::Add;
1801 default:
1802 return false;
1803 }
1804}
1805
1806/// Return whether "(X ROp Y) LOp Z" is always equal to
1807/// "(X LOp Z) ROp (Y LOp Z)".
1809 bool HasNSW, Intrinsic::ID ROp) {
1810 if (Instruction::isCommutative(LOp) || LOp == Instruction::Shl)
1811 return leftDistributesOverRight(LOp, HasNUW, HasNSW, ROp);
1812 switch (ROp) {
1813 case Intrinsic::umax:
1814 case Intrinsic::umin:
1815 return HasNUW && LOp == Instruction::Sub;
1816 case Intrinsic::smax:
1817 case Intrinsic::smin:
1818 return HasNSW && LOp == Instruction::Sub;
1819 default:
1820 return false;
1821 }
1822}
1823
1824// Attempts to factorise a common term
1825// in an instruction that has the form "(A op' B) op (C op' D)
1826// where op is an intrinsic and op' is a binop
1827static Value *
1829 InstCombiner::BuilderTy &Builder) {
1830 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
1831 Intrinsic::ID TopLevelOpcode = II->getIntrinsicID();
1832
1835
1836 if (!Op0 || !Op1)
1837 return nullptr;
1838
1839 if (Op0->getOpcode() != Op1->getOpcode())
1840 return nullptr;
1841
1842 if (!Op0->hasOneUse() || !Op1->hasOneUse())
1843 return nullptr;
1844
1845 Instruction::BinaryOps InnerOpcode =
1846 static_cast<Instruction::BinaryOps>(Op0->getOpcode());
1847 bool HasNUW = Op0->hasNoUnsignedWrap() && Op1->hasNoUnsignedWrap();
1848 bool HasNSW = Op0->hasNoSignedWrap() && Op1->hasNoSignedWrap();
1849
1850 Value *A = Op0->getOperand(0);
1851 Value *B = Op0->getOperand(1);
1852 Value *C = Op1->getOperand(0);
1853 Value *D = Op1->getOperand(1);
1854
1855 // Attempts to swap variables such that A equals C or B equals D,
1856 // if the inner operation is commutative.
1857 if (Op0->isCommutative() && A != C && B != D) {
1858 if (A == D || B == C)
1859 std::swap(C, D);
1860 else
1861 return nullptr;
1862 }
1863
1864 if (A == C &&
1865 leftDistributesOverRight(InnerOpcode, HasNUW, HasNSW, TopLevelOpcode)) {
1866 Value *NewIntrinsic = Builder.CreateBinaryIntrinsic(TopLevelOpcode, B, D);
1867 return Builder.CreateNoWrapBinOp(InnerOpcode, A, NewIntrinsic, HasNUW,
1868 HasNSW);
1869 }
1870 if (B == D &&
1871 rightDistributesOverLeft(InnerOpcode, HasNUW, HasNSW, TopLevelOpcode)) {
1872 Value *NewIntrinsic = Builder.CreateBinaryIntrinsic(TopLevelOpcode, A, C);
1873 return Builder.CreateNoWrapBinOp(InnerOpcode, NewIntrinsic, B, HasNUW,
1874 HasNSW);
1875 }
1876 return nullptr;
1877}
1878
1880 Value *Arg0 = II->getArgOperand(0);
1881 auto *ShiftConst = dyn_cast<Constant>(II->getArgOperand(1));
1882 if (!ShiftConst)
1883 return nullptr;
1884
1885 int ElemBits = Arg0->getType()->getScalarSizeInBits();
1886 bool AllPositive = true;
1887 bool AllNegative = true;
1888
1889 auto Check = [&](Constant *C) -> bool {
1890 if (auto *CI = dyn_cast_or_null<ConstantInt>(C)) {
1891 const APInt &V = CI->getValue();
1892 if (V.isNonNegative()) {
1893 AllNegative = false;
1894 return AllPositive && V.ult(ElemBits);
1895 }
1896 AllPositive = false;
1897 return AllNegative && V.sgt(-ElemBits);
1898 }
1899 return false;
1900 };
1901
1902 if (auto *VTy = dyn_cast<FixedVectorType>(Arg0->getType())) {
1903 for (unsigned I = 0, E = VTy->getNumElements(); I < E; ++I) {
1904 if (!Check(ShiftConst->getAggregateElement(I)))
1905 return nullptr;
1906 }
1907
1908 } else if (!Check(ShiftConst))
1909 return nullptr;
1910
1911 IRBuilderBase &B = IC.Builder;
1912 if (AllPositive)
1913 return IC.replaceInstUsesWith(*II, B.CreateShl(Arg0, ShiftConst));
1914
1915 Value *NegAmt = B.CreateNeg(ShiftConst);
1916 Intrinsic::ID IID = II->getIntrinsicID();
1917 const bool IsSigned =
1918 IID == Intrinsic::arm_neon_vshifts || IID == Intrinsic::aarch64_neon_sshl;
1919 Value *Result =
1920 IsSigned ? B.CreateAShr(Arg0, NegAmt) : B.CreateLShr(Arg0, NegAmt);
1921 return IC.replaceInstUsesWith(*II, Result);
1922}
1923
1924// If II is llvm.sin(x) or llvm.cos(x), and there is a matching
1925// llvm.cos(x) or llvm.sin(x) using the same argument, combine them
1926// into a single llvm.sincos(x) call. Returns the result for II
1927// extracted from sincos, or nullptr if no match is found.
1929 InstCombinerImpl &IC) {
1930 Intrinsic::ID IID = II->getIntrinsicID();
1931 bool IsSin = IID == Intrinsic::sin;
1932 Intrinsic::ID MatchID = IsSin ? Intrinsic::cos : Intrinsic::sin;
1933
1934 Value *Arg = II->getArgOperand(0);
1935
1936 // Don't bother looking through uses of constants.
1937 if (isa<Constant>(Arg))
1938 return nullptr;
1939
1940 // Look for a matching cos/sin intrinsic with the same argument.
1941 IntrinsicInst *Match = nullptr;
1942 for (User *U : Arg->users()) {
1943 if (auto *Cand = dyn_cast<IntrinsicInst>(U)) {
1944 if (Cand != II && !Cand->use_empty() &&
1945 Cand->getIntrinsicID() == MatchID) {
1946 Match = Cand;
1947 break;
1948 }
1949 }
1950 }
1951
1952 if (!Match)
1953 return nullptr;
1954
1955 // Insert sincos right after the argument definition.
1957 if (auto *ArgInst = dyn_cast<Instruction>(Arg)) {
1958 std::optional<BasicBlock::iterator> InsertPt =
1959 ArgInst->getInsertionPointAfterDef();
1960 if (!InsertPt)
1961 return nullptr;
1962 B.SetInsertPoint(*InsertPt);
1963 } else {
1964 BasicBlock &EntryBB = II->getFunction()->getEntryBlock();
1965 B.SetInsertPoint(&EntryBB, EntryBB.begin());
1966 }
1967
1969 II->getModule(), Intrinsic::sincos, Arg->getType());
1970 CallInst *SinCos = B.CreateCall(SinCosFunc, Arg, "sincos");
1971 // Intersect fast-math flags from the two calls.
1972 SinCos->setFastMathFlags(II->getFastMathFlags() & Match->getFastMathFlags());
1973 // Propagate the most-generic fpmath metadata from the two original calls.
1975 II->getMetadata(LLVMContext::MD_fpmath),
1976 Match->getMetadata(LLVMContext::MD_fpmath)))
1977 SinCos->setMetadata(LLVMContext::MD_fpmath, MD);
1978 Value *Sin = B.CreateExtractValue(SinCos, 0, "sin");
1979 Value *Cos = B.CreateExtractValue(SinCos, 1, "cos");
1980
1981 // Replace the matching call and erase it.
1982 IC.replaceInstUsesWith(*Match, IsSin ? Cos : Sin);
1983 IC.eraseInstFromFunction(*Match);
1984 return IsSin ? Sin : Cos;
1985}
1986
1987/// CallInst simplification. This mostly only handles folding of intrinsic
1988/// instructions. For normal calls, it allows visitCallBase to do the heavy
1989/// lifting.
1991 // Don't try to simplify calls without uses. It will not do anything useful,
1992 // but will result in the following folds being skipped.
1993 if (!CI.use_empty()) {
1994 SmallVector<Value *, 8> Args(CI.args());
1995 if (Value *V = simplifyCall(&CI, CI.getCalledOperand(), Args,
1996 SQ.getWithInstruction(&CI)))
1997 return replaceInstUsesWith(CI, V);
1998 }
1999
2000 if (Value *FreedOp = getFreedOperand(&CI, &TLI))
2001 return visitFree(CI, FreedOp);
2002
2003 // If the caller function (i.e. us, the function that contains this CallInst)
2004 // is nounwind, mark the call as nounwind, even if the callee isn't.
2005 if (CI.getFunction()->doesNotThrow() && !CI.doesNotThrow()) {
2006 CI.setDoesNotThrow();
2007 return &CI;
2008 }
2009
2011 if (!II)
2012 return visitCallBase(CI);
2013
2014 // Intrinsics cannot occur in an invoke or a callbr, so handle them here
2015 // instead of in visitCallBase.
2016 if (auto *MI = dyn_cast<AnyMemIntrinsic>(II)) {
2017 if (auto NumBytes = MI->getLengthInBytes()) {
2018 // memmove/cpy/set of zero bytes is a noop.
2019 if (NumBytes->isZero())
2020 return eraseInstFromFunction(CI);
2021
2022 // For atomic unordered mem intrinsics if len is not a positive or
2023 // not a multiple of element size then behavior is undefined.
2024 if (MI->isAtomic() &&
2025 (NumBytes->isNegative() ||
2026 (NumBytes->getZExtValue() % MI->getElementSizeInBytes() != 0))) {
2028 assert(MI->getType()->isVoidTy() &&
2029 "non void atomic unordered mem intrinsic");
2030 return eraseInstFromFunction(*MI);
2031 }
2032 }
2033
2034 // No other transformations apply to volatile transfers.
2035 if (MI->isVolatile())
2036 return nullptr;
2037
2039 // memmove(x,x,size) -> noop.
2040 if (MTI->getSource() == MTI->getDest())
2041 return eraseInstFromFunction(CI);
2042 }
2043
2044 auto IsPointerUndefined = [MI](Value *Ptr) {
2045 return isa<ConstantPointerNull>(Ptr) &&
2047 MI->getFunction(),
2048 cast<PointerType>(Ptr->getType())->getAddressSpace());
2049 };
2050 bool SrcIsUndefined = false;
2051 // If we can determine a pointer alignment that is bigger than currently
2052 // set, update the alignment.
2053 if (auto *MTI = dyn_cast<AnyMemTransferInst>(MI)) {
2055 return I;
2056 SrcIsUndefined = IsPointerUndefined(MTI->getRawSource());
2057 } else if (auto *MSI = dyn_cast<AnyMemSetInst>(MI)) {
2058 if (Instruction *I = SimplifyAnyMemSet(MSI))
2059 return I;
2060 }
2061
2062 // If src/dest is null, this memory intrinsic must be a noop.
2063 if (SrcIsUndefined || IsPointerUndefined(MI->getRawDest())) {
2064 Builder.CreateAssumption(Builder.CreateIsNull(MI->getLength()));
2065 return eraseInstFromFunction(CI);
2066 }
2067
2068 // If we have a memmove and the source operation is a constant global,
2069 // then the source and dest pointers can't alias, so we can change this
2070 // into a call to memcpy.
2071 if (auto *MMI = dyn_cast<AnyMemMoveInst>(MI)) {
2072 if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
2073 if (GVSrc->isConstant()) {
2074 Module *M = CI.getModule();
2075 Intrinsic::ID MemCpyID =
2076 MMI->isAtomic()
2077 ? Intrinsic::memcpy_element_unordered_atomic
2078 : Intrinsic::memcpy;
2079 Type *Tys[3] = { CI.getArgOperand(0)->getType(),
2080 CI.getArgOperand(1)->getType(),
2081 CI.getArgOperand(2)->getType() };
2083 Intrinsic::getOrInsertDeclaration(M, MemCpyID, Tys));
2084 return II;
2085 }
2086 }
2087 }
2088
2089 // For fixed width vector result intrinsics, use the generic demanded vector
2090 // support.
2091 if (auto *IIFVTy = dyn_cast<FixedVectorType>(II->getType())) {
2092 auto VWidth = IIFVTy->getNumElements();
2093 APInt PoisonElts(VWidth, 0);
2094 APInt AllOnesEltMask(APInt::getAllOnes(VWidth));
2095 if (Value *V = SimplifyDemandedVectorElts(II, AllOnesEltMask, PoisonElts)) {
2096 if (V != II)
2097 return replaceInstUsesWith(*II, V);
2098 return II;
2099 }
2100 }
2101
2102 if (II->isCommutative()) {
2103 if (auto Pair = matchSymmetricPair(II->getOperand(0), II->getOperand(1))) {
2104 replaceOperand(*II, 0, Pair->first);
2105 replaceOperand(*II, 1, Pair->second);
2106 II->dropPoisonGeneratingAnnotations();
2107 II->dropUBImplyingAttrsAndMetadata();
2108 return II;
2109 }
2110
2111 if (CallInst *NewCall = canonicalizeConstantArg0ToArg1(CI))
2112 return NewCall;
2113 }
2114
2115 // Unused constrained FP intrinsic calls may have declared side effect, which
2116 // prevents it from being removed. In some cases however the side effect is
2117 // actually absent. To detect this case, call SimplifyConstrainedFPCall. If it
2118 // returns a replacement, the call may be removed.
2119 if (CI.use_empty() && isa<ConstrainedFPIntrinsic>(CI)) {
2120 if (simplifyConstrainedFPCall(&CI, SQ.getWithInstruction(&CI)))
2121 return eraseInstFromFunction(CI);
2122 }
2123
2124 Intrinsic::ID IID = II->getIntrinsicID();
2125 switch (IID) {
2126 case Intrinsic::objectsize: {
2127 SmallVector<Instruction *> InsertedInstructions;
2128 if (Value *V = lowerObjectSizeCall(II, DL, &TLI, AA, /*MustSucceed=*/false,
2129 &InsertedInstructions)) {
2130 for (Instruction *Inserted : InsertedInstructions)
2131 Worklist.add(Inserted);
2132 return replaceInstUsesWith(CI, V);
2133 }
2134 return nullptr;
2135 }
2136 case Intrinsic::abs: {
2137 Value *IIOperand = II->getArgOperand(0);
2138 bool IntMinIsPoison = cast<Constant>(II->getArgOperand(1))->isOneValue();
2139
2140 // abs(-x) -> abs(x)
2141 Value *X;
2142 if (match(IIOperand, m_Neg(m_Value(X))))
2143 return CallInst::Create(
2144 II->getCalledFunction(),
2145 {X,
2146 Builder.getInt1(IntMinIsPoison ||
2147 cast<Instruction>(IIOperand)->hasNoSignedWrap())});
2148
2149 if (match(IIOperand, m_c_Select(m_Neg(m_Value(X)), m_Deferred(X))))
2150 return CallInst::Create(II->getCalledFunction(),
2151 {X, II->getArgOperand(1)});
2152
2153 Value *Y;
2154 // abs(a * abs(b)) -> abs(a * b)
2155 if (match(IIOperand,
2158 bool NSW =
2159 cast<Instruction>(IIOperand)->hasNoSignedWrap() && IntMinIsPoison;
2160 auto *XY = NSW ? Builder.CreateNSWMul(X, Y) : Builder.CreateMul(X, Y);
2161 return CallInst::Create(II->getCalledFunction(),
2162 {XY, II->getArgOperand(1)});
2163 }
2164
2165 if (std::optional<bool> Known =
2166 getKnownSignOrZero(IIOperand, SQ.getWithInstruction(II))) {
2167 // abs(x) -> x if x >= 0 (include abs(x-y) --> x - y where x >= y)
2168 // abs(x) -> x if x > 0 (include abs(x-y) --> x - y where x > y)
2169 if (!*Known)
2170 return replaceInstUsesWith(*II, IIOperand);
2171
2172 // abs(x) -> -x if x < 0
2173 // abs(x) -> -x if x < = 0 (include abs(x-y) --> y - x where x <= y)
2174 if (IntMinIsPoison)
2175 return BinaryOperator::CreateNSWNeg(IIOperand);
2176 return BinaryOperator::CreateNeg(IIOperand);
2177 }
2178
2179 // abs (sext X) --> zext (abs X*)
2180 // Clear the IsIntMin (nsw) bit on the abs to allow narrowing.
2181 if (match(IIOperand, m_OneUse(m_SExt(m_Value(X))))) {
2182 Value *NarrowAbs =
2183 Builder.CreateBinaryIntrinsic(Intrinsic::abs, X, Builder.getFalse());
2184 return CastInst::Create(Instruction::ZExt, NarrowAbs, II->getType());
2185 }
2186
2187 // Match a complicated way to check if a number is odd/even:
2188 // abs (srem X, 2) --> and X, 1
2189 const APInt *C;
2190 if (match(IIOperand, m_SRem(m_Value(X), m_APInt(C))) && *C == 2)
2191 return BinaryOperator::CreateAnd(X, ConstantInt::get(II->getType(), 1));
2192
2193 break;
2194 }
2195 case Intrinsic::umin: {
2196 Value *I0 = II->getArgOperand(0), *I1 = II->getArgOperand(1);
2197 // umin(x, 1) == zext(x != 0)
2198 if (match(I1, m_One())) {
2199 assert(II->getType()->getScalarSizeInBits() != 1 &&
2200 "Expected simplify of umin with max constant");
2201 Value *Zero = Constant::getNullValue(I0->getType());
2202 Value *Cmp = Builder.CreateICmpNE(I0, Zero);
2203 return CastInst::Create(Instruction::ZExt, Cmp, II->getType());
2204 }
2205 // umin(cttz(x), const) --> cttz(x | (1 << const))
2206 if (Value *FoldedCttz =
2208 I0, I1, DL, Builder))
2209 return replaceInstUsesWith(*II, FoldedCttz);
2210 // umin(ctlz(x), const) --> ctlz(x | (SignedMin >> const))
2211 if (Value *FoldedCtlz =
2213 I0, I1, DL, Builder))
2214 return replaceInstUsesWith(*II, FoldedCtlz);
2215 [[fallthrough]];
2216 }
2217 case Intrinsic::umax: {
2218 Value *I0 = II->getArgOperand(0), *I1 = II->getArgOperand(1);
2219 Value *X, *Y;
2220 if (match(I0, m_ZExt(m_Value(X))) && match(I1, m_ZExt(m_Value(Y))) &&
2221 (I0->hasOneUse() || I1->hasOneUse()) && X->getType() == Y->getType()) {
2222 Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, Y);
2223 return CastInst::Create(Instruction::ZExt, NarrowMaxMin, II->getType());
2224 }
2225 Constant *C;
2226 if (match(I0, m_ZExt(m_Value(X))) && match(I1, m_Constant(C)) &&
2227 I0->hasOneUse()) {
2228 if (Constant *NarrowC = getLosslessUnsignedTrunc(C, X->getType(), DL)) {
2229 Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, NarrowC);
2230 return CastInst::Create(Instruction::ZExt, NarrowMaxMin, II->getType());
2231 }
2232 }
2233 // If C is not 0:
2234 // umax(nuw_shl(x, C), x + 1) -> x == 0 ? 1 : nuw_shl(x, C)
2235 // If C is not 0 or 1:
2236 // umax(nuw_mul(x, C), x + 1) -> x == 0 ? 1 : nuw_mul(x, C)
2237 auto foldMaxMulShift = [&](Value *A, Value *B) -> Instruction * {
2238 const APInt *C;
2239 Value *X;
2240 if (!match(A, m_NUWShl(m_Value(X), m_APInt(C))) &&
2241 !(match(A, m_NUWMul(m_Value(X), m_APInt(C))) && !C->isOne()))
2242 return nullptr;
2243 if (C->isZero())
2244 return nullptr;
2245 if (!match(B, m_OneUse(m_Add(m_Specific(X), m_One()))))
2246 return nullptr;
2247
2248 Value *Cmp = Builder.CreateICmpEQ(X, ConstantInt::get(X->getType(), 0));
2249 Value *NewSelect = nullptr;
2250 NewSelect = Builder.CreateSelectWithUnknownProfile(
2251 Cmp, ConstantInt::get(X->getType(), 1), A, DEBUG_TYPE);
2252 return replaceInstUsesWith(*II, NewSelect);
2253 };
2254
2255 if (IID == Intrinsic::umax) {
2256 if (Instruction *I = foldMaxMulShift(I0, I1))
2257 return I;
2258 if (Instruction *I = foldMaxMulShift(I1, I0))
2259 return I;
2260 }
2261
2262 // If both operands of unsigned min/max are sign-extended, it is still ok
2263 // to narrow the operation.
2264 [[fallthrough]];
2265 }
2266 case Intrinsic::smax:
2267 case Intrinsic::smin: {
2268 Value *I0 = II->getArgOperand(0), *I1 = II->getArgOperand(1);
2269 Value *X, *Y;
2270 if (match(I0, m_SExt(m_Value(X))) && match(I1, m_SExt(m_Value(Y))) &&
2271 (I0->hasOneUse() || I1->hasOneUse()) && X->getType() == Y->getType()) {
2272 Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, Y);
2273 return CastInst::Create(Instruction::SExt, NarrowMaxMin, II->getType());
2274 }
2275
2276 Constant *C;
2277 if (match(I0, m_SExt(m_Value(X))) && match(I1, m_Constant(C)) &&
2278 I0->hasOneUse()) {
2279 if (Constant *NarrowC = getLosslessSignedTrunc(C, X->getType(), DL)) {
2280 Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, NarrowC);
2281 return CastInst::Create(Instruction::SExt, NarrowMaxMin, II->getType());
2282 }
2283 }
2284
2285 // smax(smin(X, MinC), MaxC) -> smin(smax(X, MaxC), MinC) if MinC s>= MaxC
2286 // umax(umin(X, MinC), MaxC) -> umin(umax(X, MaxC), MinC) if MinC u>= MaxC
2287 const APInt *MinC, *MaxC;
2288 auto CreateCanonicalClampForm = [&](bool IsSigned) {
2289 auto MaxIID = IsSigned ? Intrinsic::smax : Intrinsic::umax;
2290 auto MinIID = IsSigned ? Intrinsic::smin : Intrinsic::umin;
2291 Value *NewMax = Builder.CreateBinaryIntrinsic(
2292 MaxIID, X, ConstantInt::get(X->getType(), *MaxC));
2293 return replaceInstUsesWith(
2294 *II, Builder.CreateBinaryIntrinsic(
2295 MinIID, NewMax, ConstantInt::get(X->getType(), *MinC)));
2296 };
2297 if (IID == Intrinsic::smax &&
2299 m_APInt(MinC)))) &&
2300 match(I1, m_APInt(MaxC)) && MinC->sgt(*MaxC))
2301 return CreateCanonicalClampForm(true);
2302 if (IID == Intrinsic::umax &&
2304 m_APInt(MinC)))) &&
2305 match(I1, m_APInt(MaxC)) && MinC->ugt(*MaxC))
2306 return CreateCanonicalClampForm(false);
2307
2308 // umin(i1 X, i1 Y) -> and i1 X, Y
2309 // smax(i1 X, i1 Y) -> and i1 X, Y
2310 if ((IID == Intrinsic::umin || IID == Intrinsic::smax) &&
2311 II->getType()->isIntOrIntVectorTy(1)) {
2312 return BinaryOperator::CreateAnd(I0, I1);
2313 }
2314
2315 // umax(i1 X, i1 Y) -> or i1 X, Y
2316 // smin(i1 X, i1 Y) -> or i1 X, Y
2317 if ((IID == Intrinsic::umax || IID == Intrinsic::smin) &&
2318 II->getType()->isIntOrIntVectorTy(1)) {
2319 return BinaryOperator::CreateOr(I0, I1);
2320 }
2321
2322 // smin(smax(X, -1), 1) -> scmp(X, 0)
2323 // smax(smin(X, 1), -1) -> scmp(X, 0)
2324 // At this point, smax(smin(X, 1), -1) is changed to smin(smax(X, -1)
2325 // And i1's have been changed to and/ors
2326 // So we only need to check for smin
2327 if (IID == Intrinsic::smin) {
2328 if (match(I0, m_OneUse(m_SMax(m_Value(X), m_AllOnes()))) &&
2329 match(I1, m_One())) {
2330 Value *Zero = ConstantInt::get(X->getType(), 0);
2331 return replaceInstUsesWith(
2332 CI,
2333 Builder.CreateIntrinsic(II->getType(), Intrinsic::scmp, {X, Zero}));
2334 }
2335 }
2336
2337 if (IID == Intrinsic::smax || IID == Intrinsic::smin) {
2338 // smax (neg nsw X), (neg nsw Y) --> neg nsw (smin X, Y)
2339 // smin (neg nsw X), (neg nsw Y) --> neg nsw (smax X, Y)
2340 // TODO: Canonicalize neg after min/max if I1 is constant.
2341 if (match(I0, m_NSWNeg(m_Value(X))) && match(I1, m_NSWNeg(m_Value(Y))) &&
2342 (I0->hasOneUse() || I1->hasOneUse())) {
2344 Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, Y);
2345 return BinaryOperator::CreateNSWNeg(InvMaxMin);
2346 }
2347 }
2348
2349 // (umax X, (xor X, Pow2))
2350 // -> (or X, Pow2)
2351 // (umin X, (xor X, Pow2))
2352 // -> (and X, ~Pow2)
2353 // (smax X, (xor X, Pos_Pow2))
2354 // -> (or X, Pos_Pow2)
2355 // (smin X, (xor X, Pos_Pow2))
2356 // -> (and X, ~Pos_Pow2)
2357 // (smax X, (xor X, Neg_Pow2))
2358 // -> (and X, ~Neg_Pow2)
2359 // (smin X, (xor X, Neg_Pow2))
2360 // -> (or X, Neg_Pow2)
2361 if ((match(I0, m_c_Xor(m_Specific(I1), m_Value(X))) ||
2362 match(I1, m_c_Xor(m_Specific(I0), m_Value(X)))) &&
2363 isKnownToBeAPowerOfTwo(X, /* OrZero */ true)) {
2364 bool UseOr = IID == Intrinsic::smax || IID == Intrinsic::umax;
2365 bool UseAndN = IID == Intrinsic::smin || IID == Intrinsic::umin;
2366
2367 if (IID == Intrinsic::smax || IID == Intrinsic::smin) {
2368 auto KnownSign = getKnownSign(X, SQ.getWithInstruction(II));
2369 if (KnownSign == std::nullopt) {
2370 UseOr = false;
2371 UseAndN = false;
2372 } else if (*KnownSign /* true is Signed. */) {
2373 UseOr ^= true;
2374 UseAndN ^= true;
2375 Type *Ty = I0->getType();
2376 // Negative power of 2 must be IntMin. It's possible to be able to
2377 // prove negative / power of 2 without actually having known bits, so
2378 // just get the value by hand.
2380 Ty, APInt::getSignedMinValue(Ty->getScalarSizeInBits()));
2381 }
2382 }
2383 if (UseOr)
2384 return BinaryOperator::CreateOr(I0, X);
2385 else if (UseAndN)
2386 return BinaryOperator::CreateAnd(I0, Builder.CreateNot(X));
2387 }
2388
2389 // If we can eliminate ~A and Y is free to invert:
2390 // max ~A, Y --> ~(min A, ~Y)
2391 //
2392 // Examples:
2393 // max ~A, ~Y --> ~(min A, Y)
2394 // max ~A, C --> ~(min A, ~C)
2395 // max ~A, (max ~Y, ~Z) --> ~min( A, (min Y, Z))
2396 auto moveNotAfterMinMax = [&](Value *X, Value *Y) -> Instruction * {
2397 Value *A;
2398 if (match(X, m_OneUse(m_Not(m_Value(A)))) &&
2399 !isFreeToInvert(A, A->hasOneUse())) {
2400 if (Value *NotY = getFreelyInverted(Y, Y->hasOneUse(), &Builder)) {
2402 Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, A, NotY);
2403 return BinaryOperator::CreateNot(InvMaxMin);
2404 }
2405 }
2406 return nullptr;
2407 };
2408
2409 if (Instruction *I = moveNotAfterMinMax(I0, I1))
2410 return I;
2411 if (Instruction *I = moveNotAfterMinMax(I1, I0))
2412 return I;
2413
2415 return I;
2416
2417 // minmax (X & NegPow2C, Y & NegPow2C) --> minmax(X, Y) & NegPow2C
2418 const APInt *RHSC;
2419 if (match(I0, m_OneUse(m_And(m_Value(X), m_NegatedPower2(RHSC)))) &&
2420 match(I1, m_OneUse(m_And(m_Value(Y), m_SpecificInt(*RHSC)))))
2421 return BinaryOperator::CreateAnd(Builder.CreateBinaryIntrinsic(IID, X, Y),
2422 ConstantInt::get(II->getType(), *RHSC));
2423
2424 // smax(X, -X) --> abs(X)
2425 // smin(X, -X) --> -abs(X)
2426 // umax(X, -X) --> -abs(X)
2427 // umin(X, -X) --> abs(X)
2428 if (isKnownNegation(I0, I1)) {
2429 // We can choose either operand as the input to abs(), but if we can
2430 // eliminate the only use of a value, that's better for subsequent
2431 // transforms/analysis.
2432 if (I0->hasOneUse() && !I1->hasOneUse())
2433 std::swap(I0, I1);
2434
2435 // This is some variant of abs(). See if we can propagate 'nsw' to the abs
2436 // operation and potentially its negation.
2437 bool IntMinIsPoison = isKnownNegation(I0, I1, /* NeedNSW */ true);
2438 Value *Abs = Builder.CreateBinaryIntrinsic(
2439 Intrinsic::abs, I0,
2440 ConstantInt::getBool(II->getContext(), IntMinIsPoison));
2441
2442 // We don't have a "nabs" intrinsic, so negate if needed based on the
2443 // max/min operation.
2444 if (IID == Intrinsic::smin || IID == Intrinsic::umax)
2445 Abs = Builder.CreateNeg(Abs, "nabs", IntMinIsPoison);
2446 return replaceInstUsesWith(CI, Abs);
2447 }
2448
2450 return Sel;
2451
2452 if (Instruction *SAdd = matchSAddSubSat(*II))
2453 return SAdd;
2454
2455 if (Value *NewMinMax = reassociateMinMaxWithConstants(II, Builder, SQ))
2456 return replaceInstUsesWith(*II, NewMinMax);
2457
2459 return R;
2460
2461 if (Instruction *NewMinMax = factorizeMinMaxTree(II))
2462 return NewMinMax;
2463
2464 // Try to fold minmax with constant RHS based on range information
2465 if (match(I1, m_APIntAllowPoison(RHSC))) {
2466 ICmpInst::Predicate Pred =
2468 bool IsSigned = MinMaxIntrinsic::isSigned(IID);
2470 I0, IsSigned, SQ.getWithInstruction(II));
2471 if (!LHS_CR.isFullSet()) {
2472 if (LHS_CR.icmp(Pred, *RHSC))
2473 return replaceInstUsesWith(*II, I0);
2474 if (LHS_CR.icmp(ICmpInst::getSwappedPredicate(Pred), *RHSC))
2475 return replaceInstUsesWith(*II,
2476 ConstantInt::get(II->getType(), *RHSC));
2477 }
2478 }
2479
2481 return replaceInstUsesWith(*II, V);
2482
2483 break;
2484 }
2485 case Intrinsic::scmp: {
2486 Value *I0 = II->getArgOperand(0), *I1 = II->getArgOperand(1);
2487
2488 // scmp(X, 0) -> sext_or_trunc(X) if X is known to be one of -1, 0, 1.
2489 if (match(I1, m_Zero())) {
2490 ConstantRange Range = computeConstantRange(I0, /*ForSigned=*/true,
2491 SQ.getWithInstruction(II));
2492 if (Range.getSignedMin().sge(-1) && Range.getSignedMax().sle(1))
2493 return replaceInstUsesWith(
2494 CI, Builder.CreateSExtOrTrunc(I0, II->getType()));
2495 }
2496 Value *LHS, *RHS;
2497 if (match(I0, m_NSWSub(m_Value(LHS), m_Value(RHS))) && match(I1, m_Zero()))
2498 return replaceInstUsesWith(
2499 CI,
2500 Builder.CreateIntrinsic(II->getType(), Intrinsic::scmp, {LHS, RHS}));
2501 break;
2502 }
2503 case Intrinsic::bitreverse: {
2504 Value *IIOperand = II->getArgOperand(0);
2505 // bitrev (zext i1 X to ?) --> X ? SignBitC : 0
2506 Value *X;
2507 if (match(IIOperand, m_ZExt(m_Value(X))) &&
2508 X->getType()->isIntOrIntVectorTy(1)) {
2509 Type *Ty = II->getType();
2510 APInt SignBit = APInt::getSignMask(Ty->getScalarSizeInBits());
2511 return SelectInst::Create(X, ConstantInt::get(Ty, SignBit),
2513 }
2514
2515 if (Instruction *crossLogicOpFold =
2517 return crossLogicOpFold;
2518
2519 break;
2520 }
2521 case Intrinsic::bswap: {
2522 Value *IIOperand = II->getArgOperand(0);
2523
2524 // Try to canonicalize bswap-of-logical-shift-by-8-bit-multiple as
2525 // inverse-shift-of-bswap:
2526 // bswap (shl X, Y) --> lshr (bswap X), Y
2527 // bswap (lshr X, Y) --> shl (bswap X), Y
2528 Value *X, *Y;
2529 if (match(IIOperand, m_OneUse(m_LogicalShift(m_Value(X), m_Value(Y))))) {
2530 unsigned BitWidth = IIOperand->getType()->getScalarSizeInBits();
2532 Value *NewSwap = Builder.CreateUnaryIntrinsic(Intrinsic::bswap, X);
2533 BinaryOperator::BinaryOps InverseShift =
2534 cast<BinaryOperator>(IIOperand)->getOpcode() == Instruction::Shl
2535 ? Instruction::LShr
2536 : Instruction::Shl;
2537 return BinaryOperator::Create(InverseShift, NewSwap, Y);
2538 }
2539 }
2540
2541 KnownBits Known = computeKnownBits(IIOperand, II);
2542 uint64_t LZ = alignDown(Known.countMinLeadingZeros(), 8);
2543 uint64_t TZ = alignDown(Known.countMinTrailingZeros(), 8);
2544 unsigned BW = Known.getBitWidth();
2545
2546 // bswap(x) -> shift(x) if x has exactly one "active byte"
2547 if (BW - LZ - TZ == 8) {
2548 assert(LZ != TZ && "active byte cannot be in the middle");
2549 if (LZ > TZ) // -> shl(x) if the "active byte" is in the low part of x
2550 return BinaryOperator::CreateNUWShl(
2551 IIOperand, ConstantInt::get(IIOperand->getType(), LZ - TZ));
2552 // -> lshr(x) if the "active byte" is in the high part of x
2553 return BinaryOperator::CreateExactLShr(
2554 IIOperand, ConstantInt::get(IIOperand->getType(), TZ - LZ));
2555 }
2556
2557 // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
2558 if (match(IIOperand, m_Trunc(m_BSwap(m_Value(X))))) {
2559 unsigned C = X->getType()->getScalarSizeInBits() - BW;
2560 Value *CV = ConstantInt::get(X->getType(), C);
2561 Value *V = Builder.CreateLShr(X, CV);
2562 return new TruncInst(V, IIOperand->getType());
2563 }
2564
2565 if (Instruction *crossLogicOpFold =
2567 return crossLogicOpFold;
2568 }
2569
2570 // Try to fold into bitreverse if bswap is the root of the expression tree.
2571 if (Instruction *BitOp = matchBSwapOrBitReverse(*II, /*MatchBSwaps*/ false,
2572 /*MatchBitReversals*/ true))
2573 return BitOp;
2574 break;
2575 }
2576 case Intrinsic::masked_load:
2577 if (Value *SimplifiedMaskedOp = simplifyMaskedLoad(*II))
2578 return replaceInstUsesWith(CI, SimplifiedMaskedOp);
2579 break;
2580 case Intrinsic::masked_store:
2581 return simplifyMaskedStore(*II);
2582 case Intrinsic::masked_gather:
2583 return simplifyMaskedGather(*II);
2584 case Intrinsic::masked_scatter:
2585 return simplifyMaskedScatter(*II);
2586 case Intrinsic::launder_invariant_group:
2587 case Intrinsic::strip_invariant_group:
2588 if (auto *SkippedBarrier = simplifyInvariantGroupIntrinsic(*II, *this))
2589 return replaceInstUsesWith(*II, SkippedBarrier);
2590 break;
2591 case Intrinsic::powi: {
2592 if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
2593 // 0 and 1 are handled in instsimplify
2594 // powi(x, -1) -> 1/x
2595 if (Power->isMinusOne())
2596 return BinaryOperator::CreateFDivFMF(ConstantFP::get(CI.getType(), 1.0),
2597 II->getArgOperand(0), II);
2598 // powi(x, 2) -> x*x
2599 if (Power->equalsInt(2))
2600 return BinaryOperator::CreateFMulFMF(II->getArgOperand(0),
2601 II->getArgOperand(0), II);
2602
2603 if (!Power->getValue()[0]) {
2604 Value *X;
2605 // If power is even:
2606 // powi(-x, p) -> powi(x, p)
2607 // powi(fabs(x), p) -> powi(x, p)
2608 // powi(copysign(x, y), p) -> powi(x, p)
2609 if (match(II->getArgOperand(0), m_FNeg(m_Value(X))) ||
2610 match(II->getArgOperand(0), m_FAbs(m_Value(X))) ||
2611 match(II->getArgOperand(0),
2613 return CallInst::Create(II->getCalledFunction(), {X, Power});
2614 }
2615 }
2616 if (ConstantFP *Base = dyn_cast<ConstantFP>(II->getArgOperand(0))) {
2617 Value *Exp = II->getArgOperand(1);
2618 Type *Ty = Base->getType();
2619 // powi(2.0, p) -> ldexp(1.0, p)
2620 if (II->hasApproxFunc() && Base->isExactlyValue(2.0)) {
2621 ConstantFP *One = ConstantFP::get(Ty, 1.0);
2622 if (auto *VTy = dyn_cast<VectorType>(Ty))
2623 Exp = Builder.CreateVectorSplat(VTy->getElementCount(), Exp);
2624 Value *Ldexp = Builder.CreateLdexp(One, Exp, II);
2625 return replaceInstUsesWith(*II, Ldexp);
2626 }
2627 }
2628 break;
2629 }
2630
2631 case Intrinsic::cttz:
2632 case Intrinsic::ctlz:
2633 if (auto *I = foldCttzCtlz(*II, *this))
2634 return I;
2635 break;
2636
2637 case Intrinsic::ctpop:
2638 if (auto *I = foldCtpop(*II, *this))
2639 return I;
2640 break;
2641
2642 case Intrinsic::fshl:
2643 case Intrinsic::fshr: {
2644 Value *Op0 = II->getArgOperand(0), *Op1 = II->getArgOperand(1);
2645 Type *Ty = II->getType();
2646 unsigned BitWidth = Ty->getScalarSizeInBits();
2647 Constant *ShAmtC;
2648 if (match(II->getArgOperand(2), m_ImmConstant(ShAmtC))) {
2649 // Canonicalize a shift amount constant operand to modulo the bit-width.
2650 Constant *WidthC = ConstantInt::get(Ty, BitWidth);
2651 Constant *ModuloC =
2652 ConstantFoldBinaryOpOperands(Instruction::URem, ShAmtC, WidthC, DL);
2653 if (!ModuloC)
2654 return nullptr;
2655 if (ModuloC != ShAmtC)
2656 return CallInst::Create(II->getCalledFunction(), {Op0, Op1, ModuloC});
2657
2659 ShAmtC, DL),
2660 m_One()) &&
2661 "Shift amount expected to be modulo bitwidth");
2662
2663 // Canonicalize funnel shift right by constant to funnel shift left. This
2664 // is not entirely arbitrary. For historical reasons, the backend may
2665 // recognize rotate left patterns but miss rotate right patterns.
2666 if (IID == Intrinsic::fshr) {
2667 // fshr X, Y, C --> fshl X, Y, (BitWidth - C) if C is not zero.
2668 if (!isKnownNonZero(ShAmtC, SQ.getWithInstruction(II)))
2669 return nullptr;
2670
2671 Constant *LeftShiftC = ConstantExpr::getSub(WidthC, ShAmtC);
2672 Module *Mod = II->getModule();
2673 Function *Fshl =
2674 Intrinsic::getOrInsertDeclaration(Mod, Intrinsic::fshl, Ty);
2675 return CallInst::Create(Fshl, { Op0, Op1, LeftShiftC });
2676 }
2677 assert(IID == Intrinsic::fshl &&
2678 "All funnel shifts by simple constants should go left");
2679
2680 // fshl(X, 0, C) --> shl X, C
2681 // fshl(X, undef, C) --> shl X, C
2682 if (match(Op1, m_ZeroInt()) || match(Op1, m_Undef()))
2683 return BinaryOperator::CreateShl(Op0, ShAmtC);
2684
2685 // fshl(0, X, C) --> lshr X, (BW-C)
2686 // fshl(undef, X, C) --> lshr X, (BW-C)
2687 // Similar to fshr -> fshl fold above, this is only valid if C is not zero
2688 if ((match(Op0, m_ZeroInt()) || match(Op0, m_Undef())) &&
2689 isKnownNonZero(ShAmtC, SQ.getWithInstruction(II)))
2690 return BinaryOperator::CreateLShr(Op1,
2691 ConstantExpr::getSub(WidthC, ShAmtC));
2692
2693 // fshl i16 X, X, 8 --> bswap i16 X (reduce to more-specific form)
2694 if (Op0 == Op1 && BitWidth == 16 && match(ShAmtC, m_SpecificInt(8))) {
2695 Module *Mod = II->getModule();
2696 Function *Bswap =
2697 Intrinsic::getOrInsertDeclaration(Mod, Intrinsic::bswap, Ty);
2698 return CallInst::Create(Bswap, { Op0 });
2699 }
2700 if (Instruction *BitOp =
2701 matchBSwapOrBitReverse(*II, /*MatchBSwaps*/ true,
2702 /*MatchBitReversals*/ true))
2703 return BitOp;
2704
2705 // R = fshl(X, X, C2)
2706 // fshl(R, R, C1) --> fshl(X, X, (C1 + C2) % bitsize)
2707 Value *InnerOp;
2708 const APInt *ShAmtInnerC, *ShAmtOuterC;
2709 if (match(Op0, m_FShl(m_Value(InnerOp), m_Deferred(InnerOp),
2710 m_APInt(ShAmtInnerC))) &&
2711 match(ShAmtC, m_APInt(ShAmtOuterC)) && Op0 == Op1) {
2712 APInt Sum = *ShAmtOuterC + *ShAmtInnerC;
2713 APInt Modulo = Sum.urem(APInt(Sum.getBitWidth(), BitWidth));
2714 if (Modulo.isZero())
2715 return replaceInstUsesWith(*II, InnerOp);
2716 Constant *ModuloC = ConstantInt::get(Ty, Modulo);
2718 {InnerOp, InnerOp, ModuloC});
2719 }
2720 }
2721
2722 // fshl(X, X, Neg(Y)) --> fshr(X, X, Y)
2723 // fshr(X, X, Neg(Y)) --> fshl(X, X, Y)
2724 // if BitWidth is a power-of-2
2725 Value *Y;
2726 if (Op0 == Op1 && isPowerOf2_32(BitWidth) &&
2727 match(II->getArgOperand(2), m_Neg(m_Value(Y)))) {
2728 Module *Mod = II->getModule();
2730 Mod, IID == Intrinsic::fshl ? Intrinsic::fshr : Intrinsic::fshl, Ty);
2731 return CallInst::Create(OppositeShift, {Op0, Op1, Y});
2732 }
2733
2734 // fshl(X, 0, Y) --> shl(X, and(Y, BitWidth - 1)) if bitwidth is a
2735 // power-of-2
2736 if (IID == Intrinsic::fshl && isPowerOf2_32(BitWidth) &&
2737 match(Op1, m_ZeroInt())) {
2738 Value *Op2 = II->getArgOperand(2);
2739 Value *And = Builder.CreateAnd(Op2, ConstantInt::get(Ty, BitWidth - 1));
2740 return BinaryOperator::CreateShl(Op0, And);
2741 }
2742
2743 // Left or right might be masked.
2745 return &CI;
2746
2747 // The shift amount (operand 2) of a funnel shift is modulo the bitwidth,
2748 // so only the low bits of the shift amount are demanded if the bitwidth is
2749 // a power-of-2.
2750 if (!isPowerOf2_32(BitWidth))
2751 break;
2753 KnownBits Op2Known(BitWidth);
2754 if (SimplifyDemandedBits(II, 2, Op2Demanded, Op2Known))
2755 return &CI;
2756 break;
2757 }
2758 case Intrinsic::pdep: {
2759 const APInt *MaskC;
2760 if (match(II->getArgOperand(1), m_APInt(MaskC))) {
2761 unsigned MaskIdx, MaskLen;
2762 if (MaskC->isShiftedMask(MaskIdx, MaskLen)) {
2763 // any single contiguous sequence of 1s anywhere in the mask simply
2764 // describes a subset of the input bits shifted to the appropriate
2765 // position. Replace with the straight forward IR.
2766 Value *Input = II->getArgOperand(0);
2767 Value *ShiftAmt = ConstantInt::get(II->getType(), MaskIdx);
2768 Value *Shifted = Builder.CreateShl(Input, ShiftAmt);
2769 Value *Masked = Builder.CreateAnd(Shifted, II->getArgOperand(1));
2770 return replaceInstUsesWith(*II, Masked);
2771 }
2772 }
2773 break;
2774 }
2775 case Intrinsic::pext: {
2776 const APInt *MaskC;
2777 if (match(II->getArgOperand(1), m_APInt(MaskC))) {
2778 unsigned MaskIdx, MaskLen;
2779 if (MaskC->isShiftedMask(MaskIdx, MaskLen)) {
2780 // any single contiguous sequence of 1s anywhere in the mask simply
2781 // describes a subset of the input bits shifted to the appropriate
2782 // position. Replace with the straight forward IR.
2783 Value *Input = II->getArgOperand(0);
2784 Value *Masked = Builder.CreateAnd(Input, II->getArgOperand(1));
2785 Value *ShiftAmt = ConstantInt::get(II->getType(), MaskIdx);
2786 Value *Shifted = Builder.CreateLShr(Masked, ShiftAmt);
2787 return replaceInstUsesWith(*II, Shifted);
2788 }
2789 }
2790 break;
2791 }
2792 case Intrinsic::ptrmask: {
2793 unsigned BitWidth = DL.getPointerTypeSizeInBits(II->getType());
2796 return II;
2797
2798 Value *InnerPtr, *InnerMask;
2799 bool Changed = false;
2800 // Combine:
2801 // (ptrmask (ptrmask p, A), B)
2802 // -> (ptrmask p, (and A, B))
2803 if (match(II->getArgOperand(0),
2805 m_Value(InnerMask))))) {
2806 assert(II->getArgOperand(1)->getType() == InnerMask->getType() &&
2807 "Mask types must match");
2808 // TODO: If InnerMask == Op1, we could copy attributes from inner
2809 // callsite -> outer callsite.
2810 Value *NewMask = Builder.CreateAnd(II->getArgOperand(1), InnerMask);
2811 replaceOperand(CI, 0, InnerPtr);
2812 replaceOperand(CI, 1, NewMask);
2813 Changed = true;
2814 }
2815
2816 // See if we can deduce non-null.
2817 if (!CI.hasRetAttr(Attribute::NonNull) &&
2818 (Known.isNonZero() ||
2819 isKnownNonZero(II, getSimplifyQuery().getWithInstruction(II)))) {
2820 CI.addRetAttr(Attribute::NonNull);
2821 Changed = true;
2822 }
2823
2824 unsigned NewAlignmentLog =
2826 std::min(BitWidth - 1, Known.countMinTrailingZeros()));
2827 // Known bits will capture if we had alignment information associated with
2828 // the pointer argument.
2829 if (NewAlignmentLog > Log2(CI.getRetAlign().valueOrOne())) {
2831 CI.getContext(), Align(uint64_t(1) << NewAlignmentLog)));
2832 Changed = true;
2833 }
2834 if (Changed)
2835 return &CI;
2836 break;
2837 }
2838 case Intrinsic::uadd_with_overflow:
2839 case Intrinsic::sadd_with_overflow: {
2840 if (Instruction *I = foldIntrinsicWithOverflowCommon(II))
2841 return I;
2842
2843 // Given 2 constant operands whose sum does not overflow:
2844 // uaddo (X +nuw C0), C1 -> uaddo X, C0 + C1
2845 // saddo (X +nsw C0), C1 -> saddo X, C0 + C1
2846 Value *X;
2847 const APInt *C0, *C1;
2848 Value *Arg0 = II->getArgOperand(0);
2849 Value *Arg1 = II->getArgOperand(1);
2850 bool IsSigned = IID == Intrinsic::sadd_with_overflow;
2851 bool HasNWAdd = IsSigned
2852 ? match(Arg0, m_NSWAddLike(m_Value(X), m_APInt(C0)))
2853 : match(Arg0, m_NUWAddLike(m_Value(X), m_APInt(C0)));
2854 if (HasNWAdd && match(Arg1, m_APInt(C1))) {
2855 bool Overflow;
2856 APInt NewC =
2857 IsSigned ? C1->sadd_ov(*C0, Overflow) : C1->uadd_ov(*C0, Overflow);
2858 if (!Overflow)
2859 return replaceInstUsesWith(
2860 *II, Builder.CreateBinaryIntrinsic(
2861 IID, X, ConstantInt::get(Arg1->getType(), NewC)));
2862 }
2863 break;
2864 }
2865
2866 case Intrinsic::umul_with_overflow:
2867 case Intrinsic::smul_with_overflow:
2868 case Intrinsic::usub_with_overflow:
2869 if (Instruction *I = foldIntrinsicWithOverflowCommon(II))
2870 return I;
2871 break;
2872
2873 case Intrinsic::ssub_with_overflow: {
2874 if (Instruction *I = foldIntrinsicWithOverflowCommon(II))
2875 return I;
2876
2877 Constant *C;
2878 Value *Arg0 = II->getArgOperand(0);
2879 Value *Arg1 = II->getArgOperand(1);
2880 // Given a constant C that is not the minimum signed value
2881 // for an integer of a given bit width:
2882 //
2883 // ssubo X, C -> saddo X, -C
2884 if (match(Arg1, m_Constant(C)) && C->isNotMinSignedValue()) {
2885 Value *NegVal = ConstantExpr::getNeg(C);
2886 // Build a saddo call that is equivalent to the discovered
2887 // ssubo call.
2888 return replaceInstUsesWith(
2889 *II, Builder.CreateBinaryIntrinsic(Intrinsic::sadd_with_overflow,
2890 Arg0, NegVal));
2891 }
2892
2893 break;
2894 }
2895
2896 case Intrinsic::uadd_sat:
2897 case Intrinsic::sadd_sat:
2898 case Intrinsic::usub_sat:
2899 case Intrinsic::ssub_sat: {
2901 Type *Ty = SI->getType();
2902 Value *Arg0 = SI->getLHS();
2903 Value *Arg1 = SI->getRHS();
2904
2905 // Make use of known overflow information.
2906 OverflowResult OR = computeOverflow(SI->getBinaryOp(), SI->isSigned(),
2907 Arg0, Arg1, SI);
2908 switch (OR) {
2910 break;
2912 if (SI->isSigned())
2913 return BinaryOperator::CreateNSW(SI->getBinaryOp(), Arg0, Arg1);
2914 else
2915 return BinaryOperator::CreateNUW(SI->getBinaryOp(), Arg0, Arg1);
2917 unsigned BitWidth = Ty->getScalarSizeInBits();
2918 APInt Min = APSInt::getMinValue(BitWidth, !SI->isSigned());
2919 return replaceInstUsesWith(*SI, ConstantInt::get(Ty, Min));
2920 }
2922 unsigned BitWidth = Ty->getScalarSizeInBits();
2923 APInt Max = APSInt::getMaxValue(BitWidth, !SI->isSigned());
2924 return replaceInstUsesWith(*SI, ConstantInt::get(Ty, Max));
2925 }
2926 }
2927
2928 // usub_sat((sub nuw C, A), C1) -> usub_sat(usub_sat(C, C1), A)
2929 // which after that:
2930 // usub_sat((sub nuw C, A), C1) -> usub_sat(C - C1, A) if C1 u< C
2931 // usub_sat((sub nuw C, A), C1) -> 0 otherwise
2932 Constant *C, *C1;
2933 Value *A;
2934 if (IID == Intrinsic::usub_sat &&
2935 match(Arg0, m_NUWSub(m_ImmConstant(C), m_Value(A))) &&
2936 match(Arg1, m_ImmConstant(C1))) {
2937 auto *NewC = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, C, C1);
2938 auto *NewSub =
2939 Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, NewC, A);
2940 return replaceInstUsesWith(*SI, NewSub);
2941 }
2942
2943 // ssub.sat(X, C) -> sadd.sat(X, -C) if C != MIN
2944 if (IID == Intrinsic::ssub_sat && match(Arg1, m_Constant(C)) &&
2945 C->isNotMinSignedValue()) {
2946 Value *NegVal = ConstantExpr::getNeg(C);
2947 return replaceInstUsesWith(
2948 *II, Builder.CreateBinaryIntrinsic(
2949 Intrinsic::sadd_sat, Arg0, NegVal));
2950 }
2951
2952 // sat(sat(X + Val2) + Val) -> sat(X + (Val+Val2))
2953 // sat(sat(X - Val2) - Val) -> sat(X - (Val+Val2))
2954 // if Val and Val2 have the same sign
2955 if (auto *Other = dyn_cast<IntrinsicInst>(Arg0)) {
2956 Value *X;
2957 const APInt *Val, *Val2;
2958 APInt NewVal;
2959 bool IsUnsigned =
2960 IID == Intrinsic::uadd_sat || IID == Intrinsic::usub_sat;
2961 if (Other->getIntrinsicID() == IID &&
2962 match(Arg1, m_APInt(Val)) &&
2963 match(Other->getArgOperand(0), m_Value(X)) &&
2964 match(Other->getArgOperand(1), m_APInt(Val2))) {
2965 if (IsUnsigned)
2966 NewVal = Val->uadd_sat(*Val2);
2967 else if (Val->isNonNegative() == Val2->isNonNegative()) {
2968 bool Overflow;
2969 NewVal = Val->sadd_ov(*Val2, Overflow);
2970 if (Overflow) {
2971 // Both adds together may add more than SignedMaxValue
2972 // without saturating the final result.
2973 break;
2974 }
2975 } else {
2976 // Cannot fold saturated addition with different signs.
2977 break;
2978 }
2979
2980 return replaceInstUsesWith(
2981 *II, Builder.CreateBinaryIntrinsic(
2982 IID, X, ConstantInt::get(II->getType(), NewVal)));
2983 }
2984 }
2985 break;
2986 }
2987
2988 case Intrinsic::minnum:
2989 case Intrinsic::maxnum:
2990 case Intrinsic::minimumnum:
2991 case Intrinsic::maximumnum:
2992 case Intrinsic::minimum:
2993 case Intrinsic::maximum: {
2994 Value *Arg0 = II->getArgOperand(0);
2995 Value *Arg1 = II->getArgOperand(1);
2996 Value *X, *Y;
2997 if (match(Arg0, m_FNeg(m_Value(X))) && match(Arg1, m_FNeg(m_Value(Y))) &&
2998 (Arg0->hasOneUse() || Arg1->hasOneUse())) {
2999 // If both operands are negated, invert the call and negate the result:
3000 // min(-X, -Y) --> -(max(X, Y))
3001 // max(-X, -Y) --> -(min(X, Y))
3002 Intrinsic::ID NewIID;
3003 switch (IID) {
3004 case Intrinsic::maxnum:
3005 NewIID = Intrinsic::minnum;
3006 break;
3007 case Intrinsic::minnum:
3008 NewIID = Intrinsic::maxnum;
3009 break;
3010 case Intrinsic::maximumnum:
3011 NewIID = Intrinsic::minimumnum;
3012 break;
3013 case Intrinsic::minimumnum:
3014 NewIID = Intrinsic::maximumnum;
3015 break;
3016 case Intrinsic::maximum:
3017 NewIID = Intrinsic::minimum;
3018 break;
3019 case Intrinsic::minimum:
3020 NewIID = Intrinsic::maximum;
3021 break;
3022 default:
3023 llvm_unreachable("unexpected intrinsic ID");
3024 }
3025 Value *NewCall = Builder.CreateBinaryIntrinsic(NewIID, X, Y, II);
3026 Instruction *FNeg = UnaryOperator::CreateFNeg(NewCall);
3027 FNeg->copyIRFlags(II);
3028 return FNeg;
3029 }
3030
3031 // m(m(X, C2), C1) -> m(X, C)
3032 const APFloat *C1, *C2;
3033 if (auto *M = dyn_cast<IntrinsicInst>(Arg0)) {
3034 if (M->getIntrinsicID() == IID && match(Arg1, m_APFloat(C1)) &&
3035 ((match(M->getArgOperand(0), m_Value(X)) &&
3036 match(M->getArgOperand(1), m_APFloat(C2))) ||
3037 (match(M->getArgOperand(1), m_Value(X)) &&
3038 match(M->getArgOperand(0), m_APFloat(C2))))) {
3039 APFloat Res(0.0);
3040 switch (IID) {
3041 case Intrinsic::maxnum:
3042 Res = maxnum(*C1, *C2);
3043 break;
3044 case Intrinsic::minnum:
3045 Res = minnum(*C1, *C2);
3046 break;
3047 case Intrinsic::maximumnum:
3048 Res = maximumnum(*C1, *C2);
3049 break;
3050 case Intrinsic::minimumnum:
3051 Res = minimumnum(*C1, *C2);
3052 break;
3053 case Intrinsic::maximum:
3054 Res = maximum(*C1, *C2);
3055 break;
3056 case Intrinsic::minimum:
3057 Res = minimum(*C1, *C2);
3058 break;
3059 default:
3060 llvm_unreachable("unexpected intrinsic ID");
3061 }
3062 // TODO: Conservatively intersecting FMF. If Res == C2, the transform
3063 // was a simplification (so Arg0 and its original flags could
3064 // propagate?)
3065 Value *V = Builder.CreateBinaryIntrinsic(
3066 IID, X, ConstantFP::get(Arg0->getType(), Res),
3068 return replaceInstUsesWith(*II, V);
3069 }
3070 }
3071
3072 // m((fpext X), (fpext Y)) -> fpext (m(X, Y))
3073 if (match(Arg0, m_FPExt(m_Value(X))) && match(Arg1, m_FPExt(m_Value(Y))) &&
3074 (Arg0->hasOneUse() || Arg1->hasOneUse()) &&
3075 X->getType() == Y->getType()) {
3076 Value *NewCall =
3077 Builder.CreateBinaryIntrinsic(IID, X, Y, II, II->getName());
3078 return new FPExtInst(NewCall, II->getType());
3079 }
3080
3081 // m(fpext X, C) -> fpext m(X, TruncC) if C can be losslessly truncated.
3082 Constant *C;
3083 if (match(Arg0, m_OneUse(m_FPExt(m_Value(X)))) &&
3084 match(Arg1, m_ImmConstant(C))) {
3085 if (Constant *TruncC =
3086 getLosslessInvCast(C, X->getType(), Instruction::FPExt, DL)) {
3087 Value *NewCall =
3088 Builder.CreateBinaryIntrinsic(IID, X, TruncC, II, II->getName());
3089 return new FPExtInst(NewCall, II->getType());
3090 }
3091 }
3092
3093 // max X, -X --> fabs X
3094 // min X, -X --> -(fabs X)
3095 // TODO: Remove one-use limitation? That is obviously better for max,
3096 // hence why we don't check for one-use for that. However,
3097 // it would be an extra instruction for min (fnabs), but
3098 // that is still likely better for analysis and codegen.
3099 auto IsMinMaxOrXNegX = [IID, &X](Value *Op0, Value *Op1) {
3100 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_Specific(X)))
3101 return Op0->hasOneUse() ||
3102 (IID != Intrinsic::minimum && IID != Intrinsic::minnum &&
3103 IID != Intrinsic::minimumnum);
3104 return false;
3105 };
3106
3107 if (IsMinMaxOrXNegX(Arg0, Arg1) || IsMinMaxOrXNegX(Arg1, Arg0)) {
3108 Value *R = Builder.CreateFAbs(X, II);
3109 if (IID == Intrinsic::minimum || IID == Intrinsic::minnum ||
3110 IID == Intrinsic::minimumnum)
3111 R = Builder.CreateFNegFMF(R, II);
3112 return replaceInstUsesWith(*II, R);
3113 }
3114
3115 break;
3116 }
3117 case Intrinsic::matrix_multiply: {
3118 // Optimize negation in matrix multiplication.
3119
3120 // -A * -B -> A * B
3121 Value *A, *B;
3122 if (match(II->getArgOperand(0), m_FNeg(m_Value(A))) &&
3123 match(II->getArgOperand(1), m_FNeg(m_Value(B)))) {
3124 replaceOperand(*II, 0, A);
3125 replaceOperand(*II, 1, B);
3126 return II;
3127 }
3128
3129 Value *Op0 = II->getOperand(0);
3130 Value *Op1 = II->getOperand(1);
3131 Value *OpNotNeg, *NegatedOp;
3132 unsigned NegatedOpArg, OtherOpArg;
3133 if (match(Op0, m_FNeg(m_Value(OpNotNeg)))) {
3134 NegatedOp = Op0;
3135 NegatedOpArg = 0;
3136 OtherOpArg = 1;
3137 } else if (match(Op1, m_FNeg(m_Value(OpNotNeg)))) {
3138 NegatedOp = Op1;
3139 NegatedOpArg = 1;
3140 OtherOpArg = 0;
3141 } else
3142 // Multiplication doesn't have a negated operand.
3143 break;
3144
3145 // Only optimize if the negated operand has only one use.
3146 if (!NegatedOp->hasOneUse())
3147 break;
3148
3149 Value *OtherOp = II->getOperand(OtherOpArg);
3150 VectorType *RetTy = cast<VectorType>(II->getType());
3151 VectorType *NegatedOpTy = cast<VectorType>(NegatedOp->getType());
3152 VectorType *OtherOpTy = cast<VectorType>(OtherOp->getType());
3153 ElementCount NegatedCount = NegatedOpTy->getElementCount();
3154 ElementCount OtherCount = OtherOpTy->getElementCount();
3155 ElementCount RetCount = RetTy->getElementCount();
3156 // (-A) * B -> A * (-B), if it is cheaper to negate B and vice versa.
3157 if (ElementCount::isKnownGT(NegatedCount, OtherCount) &&
3158 ElementCount::isKnownLT(OtherCount, RetCount)) {
3159 Value *InverseOtherOp = Builder.CreateFNeg(OtherOp);
3160 replaceOperand(*II, NegatedOpArg, OpNotNeg);
3161 replaceOperand(*II, OtherOpArg, InverseOtherOp);
3162 return II;
3163 }
3164 // (-A) * B -> -(A * B), if it is cheaper to negate the result
3165 if (ElementCount::isKnownGT(NegatedCount, RetCount)) {
3166 SmallVector<Value *, 5> NewArgs(II->args());
3167 NewArgs[NegatedOpArg] = OpNotNeg;
3168 Value *NewMul = Builder.CreateIntrinsic(II->getType(), IID, NewArgs, II);
3169 return replaceInstUsesWith(*II, Builder.CreateFNegFMF(NewMul, II));
3170 }
3171 break;
3172 }
3173 case Intrinsic::fmuladd: {
3174 // Try to simplify the underlying FMul.
3175 if (Value *V =
3176 simplifyFMulInst(II->getArgOperand(0), II->getArgOperand(1),
3177 II->getFastMathFlags(), SQ.getWithInstruction(II)))
3178 return BinaryOperator::CreateFAddFMF(V, II->getArgOperand(2),
3179 II->getFastMathFlags());
3180
3181 [[fallthrough]];
3182 }
3183 case Intrinsic::fma: {
3184 // fma fneg(x), fneg(y), z -> fma x, y, z
3185 Value *Src0 = II->getArgOperand(0);
3186 Value *Src1 = II->getArgOperand(1);
3187 Value *Src2 = II->getArgOperand(2);
3188 Value *X, *Y;
3189 if (match(Src0, m_FNeg(m_Value(X))) && match(Src1, m_FNeg(m_Value(Y))))
3190 return replaceInstUsesWith(
3191 *II, Builder.CreateIntrinsic(IID, II->getType(), {X, Y, Src2}, II));
3192
3193 // fma fabs(x), fabs(x), z -> fma x, x, z
3194 if (match(Src0, m_FAbs(m_Value(X))) && match(Src1, m_FAbs(m_Specific(X))))
3195 return replaceInstUsesWith(
3196 *II, Builder.CreateIntrinsic(IID, II->getType(), {X, X, Src2}, II));
3197
3198 // Try to simplify the underlying FMul. We can only apply simplifications
3199 // that do not require rounding.
3200 if (Value *V = simplifyFMAFMul(Src0, Src1, II->getFastMathFlags(),
3201 SQ.getWithInstruction(II)))
3202 return BinaryOperator::CreateFAddFMF(V, Src2, II->getFastMathFlags());
3203
3204 // fma x, y, 0 -> fmul x, y
3205 // This is always valid for -0.0, but requires nsz for +0.0 as
3206 // -0.0 + 0.0 = 0.0, which would not be the same as the fmul on its own.
3207 if (match(Src2, m_NegZeroFP()) ||
3208 (match(Src2, m_PosZeroFP()) && II->getFastMathFlags().noSignedZeros()))
3209 return BinaryOperator::CreateFMulFMF(Src0, Src1, II);
3210
3211 // fma x, -1.0, y -> fsub y, x
3212 if (match(Src1, m_SpecificFP(-1.0)))
3213 return BinaryOperator::CreateFSubFMF(Src2, Src0, II);
3214
3215 break;
3216 }
3217 case Intrinsic::copysign: {
3218 Value *Mag = II->getArgOperand(0), *Sign = II->getArgOperand(1);
3219 if (std::optional<bool> KnownSignBit = computeKnownFPSignBit(
3220 Sign, getSimplifyQuery().getWithInstruction(II))) {
3221 if (*KnownSignBit) {
3222 // If we know that the sign argument is negative, reduce to FNABS:
3223 // copysign Mag, -Sign --> fneg (fabs Mag)
3224 Value *Fabs = Builder.CreateFAbs(Mag, II);
3225 return replaceInstUsesWith(*II, Builder.CreateFNegFMF(Fabs, II));
3226 }
3227
3228 // If we know that the sign argument is positive, reduce to FABS:
3229 // copysign Mag, +Sign --> fabs Mag
3230 Value *Fabs = Builder.CreateFAbs(Mag, II);
3231 return replaceInstUsesWith(*II, Fabs);
3232 }
3233
3234 // Propagate sign argument through nested calls:
3235 // copysign Mag, (copysign ?, X) --> copysign Mag, X
3236 Value *X;
3238 Value *CopySign =
3239 Builder.CreateCopySign(Mag, X, FMFSource::intersect(II, Sign));
3240 return replaceInstUsesWith(*II, CopySign);
3241 }
3242
3243 // Clear sign-bit of constant magnitude:
3244 // copysign -MagC, X --> copysign MagC, X
3245 // TODO: Support constant folding for fabs
3246 const APFloat *MagC;
3247 if (match(Mag, m_APFloat(MagC)) && MagC->isNegative()) {
3248 APFloat PosMagC = *MagC;
3249 PosMagC.clearSign();
3250 return replaceInstUsesWith(
3251 *II, Builder.CreateCopySign(ConstantFP::get(Mag->getType(), PosMagC),
3252 Sign, II));
3253 }
3254
3255 // Peek through changes of magnitude's sign-bit. This call rewrites those:
3256 // copysign (fabs X), Sign --> copysign X, Sign
3257 // copysign (fneg X), Sign --> copysign X, Sign
3258 if (match(Mag, m_FAbs(m_Value(X))) || match(Mag, m_FNeg(m_Value(X))))
3259 return replaceInstUsesWith(*II, Builder.CreateCopySign(X, Sign, II));
3260
3261 // copysign(floor(fabs(X)), X) --> copysign(trunc(X), X)
3262 // copysign ignores the sign bit of its magnitude argument (implicit fabs),
3263 // so replacing floor(fabs(X)) with trunc(X) is correct for all inputs
3264 // including NaN without requiring nnan. The m_FAbs match also ensures
3265 // the floor argument is non-negative, so floor == trunc.
3266 Value *FAbsArg;
3267 if (match(Mag, m_Intrinsic<Intrinsic::floor>(m_FAbs(m_Value(FAbsArg)))) &&
3268 FAbsArg == Sign) {
3269 Value *Trunc = Builder.CreateUnaryIntrinsic(Intrinsic::trunc, Sign, II);
3270 return replaceInstUsesWith(*II, Builder.CreateCopySign(Trunc, Sign, II));
3271 }
3272
3273 Type *SignEltTy = Sign->getType()->getScalarType();
3274
3275 Value *CastSrc;
3276 if (match(Sign,
3278 CastSrc->getType()->isIntOrIntVectorTy() &&
3282 APInt::getSignMask(Known.getBitWidth()), Known,
3283 SQ))
3284 return II;
3285 }
3286
3287 break;
3288 }
3289 case Intrinsic::fabs: {
3290 Value *Cond, *TVal, *FVal;
3291 Value *Arg = II->getArgOperand(0);
3292 Value *X;
3293 // fabs (-X) --> fabs (X)
3294 if (match(Arg, m_FNeg(m_Value(X)))) {
3295 Value *Fabs = Builder.CreateFAbs(X, II);
3296 return replaceInstUsesWith(CI, Fabs);
3297 }
3298
3299 if (match(Arg, m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
3300 // fabs (select Cond, TrueC, FalseC) --> select Cond, AbsT, AbsF
3301 if (Arg->hasOneUse() ? (isa<Constant>(TVal) || isa<Constant>(FVal))
3302 : (isa<Constant>(TVal) && isa<Constant>(FVal))) {
3303 CallInst *AbsT = Builder.CreateCall(II->getCalledFunction(), {TVal});
3304 CallInst *AbsF = Builder.CreateCall(II->getCalledFunction(), {FVal});
3305 SelectInst *SI = SelectInst::Create(Cond, AbsT, AbsF);
3306 SI->setFastMathFlags(II->getFastMathFlags() |
3307 cast<SelectInst>(Arg)->getFastMathFlags());
3308 // Can't copy nsz to select, as even with the nsz flag the fabs result
3309 // always has the sign bit unset.
3310 SI->setHasNoSignedZeros(false);
3311 return SI;
3312 }
3313 // fabs (select Cond, -FVal, FVal) --> fabs FVal
3314 if (match(TVal, m_FNeg(m_Specific(FVal))))
3315 return replaceInstUsesWith(*II, Builder.CreateFAbs(FVal, II));
3316 // fabs (select Cond, TVal, -TVal) --> fabs TVal
3317 if (match(FVal, m_FNeg(m_Specific(TVal))))
3318 return replaceInstUsesWith(*II, Builder.CreateFAbs(TVal, II));
3319 }
3320
3321 Value *Magnitude, *Sign;
3322 if (match(II->getArgOperand(0),
3323 m_CopySign(m_Value(Magnitude), m_Value(Sign)))) {
3324 // fabs (copysign x, y) -> (fabs x)
3325 Value *AbsSign = Builder.CreateFAbs(Magnitude, II);
3326 return replaceInstUsesWith(*II, AbsSign);
3327 }
3328
3329 [[fallthrough]];
3330 }
3331 case Intrinsic::ceil:
3332 case Intrinsic::floor:
3333 case Intrinsic::round:
3334 case Intrinsic::roundeven:
3335 case Intrinsic::nearbyint:
3336 case Intrinsic::rint:
3337 case Intrinsic::trunc: {
3338 Value *ExtSrc;
3339 if (match(II->getArgOperand(0), m_OneUse(m_FPExt(m_Value(ExtSrc))))) {
3340 // Narrow the call: intrinsic (fpext x) -> fpext (intrinsic x)
3341 Value *NarrowII = Builder.CreateUnaryIntrinsic(IID, ExtSrc, II);
3342 return new FPExtInst(NarrowII, II->getType());
3343 }
3344 break;
3345 }
3346 case Intrinsic::cos:
3347 case Intrinsic::amdgcn_cos:
3348 case Intrinsic::cosh: {
3349 Value *X, *Sign;
3350 Value *Src = II->getArgOperand(0);
3351 if (match(Src, m_FNeg(m_Value(X))) || match(Src, m_FAbs(m_Value(X))) ||
3352 match(Src, m_CopySign(m_Value(X), m_Value(Sign)))) {
3353 // f(-x) --> f(x)
3354 // f(fabs(x)) --> f(x)
3355 // f(copysign(x, y)) --> f(x)
3356 // for f in {cos, cosh}
3357 return replaceInstUsesWith(*II, Builder.CreateUnaryIntrinsic(IID, X, II));
3358 }
3359 if (IID == Intrinsic::cos) {
3360 if (Value *Result = foldSinAndCosToSinCos(II, Builder, *this))
3361 return replaceInstUsesWith(*II, Result);
3362 }
3363 break;
3364 }
3365 case Intrinsic::sin:
3366 case Intrinsic::amdgcn_sin:
3367 case Intrinsic::sinh:
3368 case Intrinsic::tan:
3369 case Intrinsic::tanh: {
3370 Value *X;
3371 if (match(II->getArgOperand(0), m_OneUse(m_FNeg(m_Value(X))))) {
3372 // f(-x) --> -f(x)
3373 // for f in {sin, sinh, tan, tanh}
3374 Value *NewFunc = Builder.CreateUnaryIntrinsic(IID, X, II);
3375 return UnaryOperator::CreateFNegFMF(NewFunc, II);
3376 }
3377 if (IID == Intrinsic::sin) {
3378 if (Value *Result = foldSinAndCosToSinCos(II, Builder, *this))
3379 return replaceInstUsesWith(*II, Result);
3380 }
3381 break;
3382 }
3383 case Intrinsic::ldexp: {
3384 Value *Src = II->getArgOperand(0);
3385 Value *Exp = II->getArgOperand(1);
3386
3387 // ldexp(x, K) -> fmul x, 2^K
3388 uint64_t ConstExp;
3389 if (match(Exp, m_ConstantInt(ConstExp))) {
3390 const fltSemantics &FPTy =
3391 Src->getType()->getScalarType()->getFltSemantics();
3392
3393 APFloat Scaled = scalbn(APFloat::getOne(FPTy), static_cast<int>(ConstExp),
3395 if (!Scaled.isZero() && !Scaled.isInfinity()) {
3396 // Skip overflow and underflow cases.
3397 Constant *FPConst = ConstantFP::get(Src->getType(), Scaled);
3398 return BinaryOperator::CreateFMulFMF(Src, FPConst, II);
3399 }
3400 }
3401
3402 // ldexp(ldexp(x, a), b) -> ldexp(x, sadd.sat(a, b))
3403 //
3404 // A danger is if the first ldexp would overflow to infinity or underflow to
3405 // zero, but the combined exponent avoids it.
3406 //
3407 // We ignore this with reassoc, or if we know both exponents have the same
3408 // sign (since then we'd just double down on the over/underflow which would
3409 // occur anyway).
3410 //
3411 // ldexp can take arbitrary integer types, so we also need to ensure that
3412 // our exponent type is wide enough so that if sadd.sat(a, b) saturates,
3413 // then ldexp at the saturated exponent saturates to inf or zero as well.
3414 //
3415 // TODO: Could do better if we had range tracking for the input value
3416 // exponent. Also could broaden sign check to cover == 0 case.
3417 Value *InnerSrc;
3418 Value *InnerExp;
3420 m_Value(InnerSrc), m_Value(InnerExp)))) &&
3421 Exp->getType() == InnerExp->getType()) {
3422 FastMathFlags FMF = II->getFastMathFlags();
3423 FastMathFlags InnerFlags = cast<FPMathOperator>(Src)->getFastMathFlags();
3424
3425 if (ldexpSaturatingAddIsSafe(II->getType(), Exp->getType()) &&
3426 ((FMF.allowReassoc() && InnerFlags.allowReassoc()) ||
3427 signBitMustBeTheSame(Exp, InnerExp, SQ.getWithInstruction(II)))) {
3428 Value *NewExp =
3429 Builder.CreateBinaryIntrinsic(Intrinsic::sadd_sat, InnerExp, Exp);
3430 return replaceInstUsesWith(
3431 *II, Builder.CreateLdexp(InnerSrc, NewExp, FMF | InnerFlags));
3432 }
3433 }
3434
3435 // ldexp(x, zext(i1 y)) -> fmul x, (select y, 2.0, 1.0)
3436 // ldexp(x, sext(i1 y)) -> fmul x, (select y, 0.5, 1.0)
3437 Value *ExtSrc;
3438 if (match(Exp, m_ZExt(m_Value(ExtSrc))) &&
3439 ExtSrc->getType()->getScalarSizeInBits() == 1) {
3440 Value *Select =
3441 Builder.CreateSelect(ExtSrc, ConstantFP::get(II->getType(), 2.0),
3442 ConstantFP::get(II->getType(), 1.0));
3444 }
3445 if (match(Exp, m_SExt(m_Value(ExtSrc))) &&
3446 ExtSrc->getType()->getScalarSizeInBits() == 1) {
3447 Value *Select =
3448 Builder.CreateSelect(ExtSrc, ConstantFP::get(II->getType(), 0.5),
3449 ConstantFP::get(II->getType(), 1.0));
3451 }
3452
3453 // ldexp(x, c ? exp : 0) -> c ? ldexp(x, exp) : x
3454 // ldexp(x, c ? 0 : exp) -> c ? x : ldexp(x, exp)
3455 ///
3456 // TODO: If we cared, should insert a canonicalize for x
3457 Value *SelectCond, *SelectLHS, *SelectRHS;
3458 if (match(II->getArgOperand(1),
3459 m_OneUse(m_Select(m_Value(SelectCond), m_Value(SelectLHS),
3460 m_Value(SelectRHS))))) {
3461 Value *NewLdexp = nullptr;
3462 Value *Select = nullptr;
3463 if (match(SelectRHS, m_ZeroInt())) {
3464 NewLdexp = Builder.CreateLdexp(Src, SelectLHS, II);
3465 Select = Builder.CreateSelect(SelectCond, NewLdexp, Src);
3466 } else if (match(SelectLHS, m_ZeroInt())) {
3467 NewLdexp = Builder.CreateLdexp(Src, SelectRHS, II);
3468 Select = Builder.CreateSelect(SelectCond, Src, NewLdexp);
3469 }
3470
3471 if (NewLdexp) {
3472 Select->takeName(II);
3473 return replaceInstUsesWith(*II, Select);
3474 }
3475 }
3476
3477 break;
3478 }
3479 case Intrinsic::ptrauth_auth:
3480 case Intrinsic::ptrauth_resign: {
3481 // (sign|resign) + (auth|resign) can be folded by omitting the middle
3482 // sign+auth component if the key and discriminator match.
3483 bool NeedSign = II->getIntrinsicID() == Intrinsic::ptrauth_resign;
3484 Value *Ptr = II->getArgOperand(0);
3485 Value *Key = II->getArgOperand(1);
3486 Value *Disc = II->getArgOperand(2);
3487 Value *DS = nullptr;
3488 if (auto Bundle = II->getOperandBundle(LLVMContext::OB_deactivation_symbol))
3489 DS = Bundle->Inputs[0];
3490
3491 // AuthKey will be the key we need to end up authenticating against in
3492 // whatever we replace this sequence with.
3493 Value *AuthKey = nullptr, *AuthDisc = nullptr, *BasePtr;
3494 if (const auto *CI = dyn_cast<CallBase>(Ptr)) {
3495 Value *OtherDS = nullptr;
3496 if (auto Bundle =
3498 OtherDS = Bundle->Inputs[0];
3499 if (DS != OtherDS)
3500 break;
3501
3502 if (CI->getIntrinsicID() == Intrinsic::ptrauth_sign) {
3503 if (CI->getArgOperand(1) != Key || CI->getArgOperand(2) != Disc)
3504 break;
3505 } else if (CI->getIntrinsicID() == Intrinsic::ptrauth_resign) {
3506 // The resign intrinsic does not support deactivation symbols.
3507 assert(!DS);
3508 if (CI->getArgOperand(3) != Key || CI->getArgOperand(4) != Disc)
3509 break;
3510 AuthKey = CI->getArgOperand(1);
3511 AuthDisc = CI->getArgOperand(2);
3512 } else
3513 break;
3514 BasePtr = CI->getArgOperand(0);
3515 } else if (const auto *PtrToInt = dyn_cast<PtrToIntOperator>(Ptr)) {
3516 // ptrauth constants are equivalent to a call to @llvm.ptrauth.sign for
3517 // our purposes, so check for that too.
3518 const auto *CPA = dyn_cast<ConstantPtrAuth>(PtrToInt->getOperand(0));
3519 if (!CPA || DS || !CPA->isKnownCompatibleWith(Key, Disc, DL))
3520 break;
3521
3522 // resign(ptrauth(p,ks,ds),ks,ds,kr,dr) -> ptrauth(p,kr,dr)
3523 if (NeedSign && isa<ConstantInt>(II->getArgOperand(4))) {
3524 auto *SignKey = cast<ConstantInt>(II->getArgOperand(3));
3525 auto *SignDisc = cast<ConstantInt>(II->getArgOperand(4));
3526 auto *Null = ConstantPointerNull::get(Builder.getPtrTy());
3527 auto *NewCPA = ConstantPtrAuth::get(CPA->getPointer(), SignKey,
3528 SignDisc, /*AddrDisc=*/Null,
3529 /*DeactivationSymbol=*/Null);
3531 *II, ConstantExpr::getPointerCast(NewCPA, II->getType()));
3532 return eraseInstFromFunction(*II);
3533 }
3534
3535 // auth(ptrauth(p,k,d),k,d) -> p
3536 BasePtr = Builder.CreatePtrToInt(CPA->getPointer(), II->getType());
3537 } else
3538 break;
3539
3540 unsigned NewIntrin;
3541 if (AuthKey && NeedSign) {
3542 // resign(0,1) + resign(1,2) = resign(0, 2)
3543 NewIntrin = Intrinsic::ptrauth_resign;
3544 } else if (AuthKey) {
3545 // resign(0,1) + auth(1) = auth(0)
3546 NewIntrin = Intrinsic::ptrauth_auth;
3547 } else if (NeedSign) {
3548 // sign(0) + resign(0, 1) = sign(1)
3549 NewIntrin = Intrinsic::ptrauth_sign;
3550 } else {
3551 // sign(0) + auth(0) = nop
3552 replaceInstUsesWith(*II, BasePtr);
3553 return eraseInstFromFunction(*II);
3554 }
3555
3556 SmallVector<Value *, 4> CallArgs;
3557 CallArgs.push_back(BasePtr);
3558 if (AuthKey) {
3559 CallArgs.push_back(AuthKey);
3560 CallArgs.push_back(AuthDisc);
3561 }
3562
3563 if (NeedSign) {
3564 CallArgs.push_back(II->getArgOperand(3));
3565 CallArgs.push_back(II->getArgOperand(4));
3566 }
3567
3568 std::vector<OperandBundleDef> Bundles;
3569 if (DS)
3570 Bundles.push_back(OperandBundleDef("deactivation-symbol", DS));
3571
3572 Function *NewFn =
3573 Intrinsic::getOrInsertDeclaration(II->getModule(), NewIntrin);
3574 return CallInst::Create(NewFn, CallArgs, Bundles);
3575 }
3576 case Intrinsic::arm_neon_vtbl1:
3577 case Intrinsic::arm_neon_vtbl2:
3578 case Intrinsic::arm_neon_vtbl3:
3579 case Intrinsic::arm_neon_vtbl4:
3580 case Intrinsic::aarch64_neon_tbl1:
3581 case Intrinsic::aarch64_neon_tbl2:
3582 case Intrinsic::aarch64_neon_tbl3:
3583 case Intrinsic::aarch64_neon_tbl4:
3584 return simplifyNeonTbl(*II, *this, /*IsExtension=*/false);
3585 case Intrinsic::arm_neon_vtbx1:
3586 case Intrinsic::arm_neon_vtbx2:
3587 case Intrinsic::arm_neon_vtbx3:
3588 case Intrinsic::arm_neon_vtbx4:
3589 case Intrinsic::aarch64_neon_tbx1:
3590 case Intrinsic::aarch64_neon_tbx2:
3591 case Intrinsic::aarch64_neon_tbx3:
3592 case Intrinsic::aarch64_neon_tbx4:
3593 return simplifyNeonTbl(*II, *this, /*IsExtension=*/true);
3594
3595 case Intrinsic::arm_neon_vmulls:
3596 case Intrinsic::arm_neon_vmullu:
3597 case Intrinsic::aarch64_neon_smull:
3598 case Intrinsic::aarch64_neon_umull: {
3599 Value *Arg0 = II->getArgOperand(0);
3600 Value *Arg1 = II->getArgOperand(1);
3601
3602 // Handle mul by zero first:
3604 return replaceInstUsesWith(CI, ConstantAggregateZero::get(II->getType()));
3605 }
3606
3607 // Check for constant LHS & RHS - in this case we just simplify.
3608 bool Zext = (IID == Intrinsic::arm_neon_vmullu ||
3609 IID == Intrinsic::aarch64_neon_umull);
3610 VectorType *NewVT = cast<VectorType>(II->getType());
3611 if (Constant *CV0 = dyn_cast<Constant>(Arg0)) {
3612 if (Constant *CV1 = dyn_cast<Constant>(Arg1)) {
3613 Value *V0 = Builder.CreateIntCast(CV0, NewVT, /*isSigned=*/!Zext);
3614 Value *V1 = Builder.CreateIntCast(CV1, NewVT, /*isSigned=*/!Zext);
3615 return replaceInstUsesWith(CI, Builder.CreateMul(V0, V1));
3616 }
3617
3618 // Couldn't simplify - canonicalize constant to the RHS.
3619 std::swap(Arg0, Arg1);
3620 }
3621
3622 // Handle mul by one:
3623 if (Constant *CV1 = dyn_cast<Constant>(Arg1))
3624 if (ConstantInt *Splat =
3625 dyn_cast_or_null<ConstantInt>(CV1->getSplatValue()))
3626 if (Splat->isOne())
3627 return CastInst::CreateIntegerCast(Arg0, II->getType(),
3628 /*isSigned=*/!Zext);
3629
3630 break;
3631 }
3632 case Intrinsic::arm_neon_aesd:
3633 case Intrinsic::arm_neon_aese:
3634 case Intrinsic::aarch64_crypto_aesd:
3635 case Intrinsic::aarch64_crypto_aese:
3636 case Intrinsic::aarch64_sve_aesd:
3637 case Intrinsic::aarch64_sve_aese: {
3638 Value *DataArg = II->getArgOperand(0);
3639 Value *KeyArg = II->getArgOperand(1);
3640
3641 // Accept zero on either operand.
3642 if (!match(KeyArg, m_ZeroInt()))
3643 std::swap(KeyArg, DataArg);
3644
3645 // Try to use the builtin XOR in AESE and AESD to eliminate a prior XOR
3646 Value *Data, *Key;
3647 if (match(KeyArg, m_ZeroInt()) &&
3648 match(DataArg, m_Xor(m_Value(Data), m_Value(Key)))) {
3649 replaceOperand(*II, 0, Data);
3650 replaceOperand(*II, 1, Key);
3651 return II;
3652 }
3653 break;
3654 }
3655 case Intrinsic::arm_neon_vshifts:
3656 case Intrinsic::arm_neon_vshiftu:
3657 case Intrinsic::aarch64_neon_sshl:
3658 case Intrinsic::aarch64_neon_ushl:
3659 return foldNeonShift(II, *this);
3660 case Intrinsic::hexagon_V6_vandvrt:
3661 case Intrinsic::hexagon_V6_vandvrt_128B: {
3662 // Simplify Q -> V -> Q conversion.
3663 if (auto Op0 = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
3664 Intrinsic::ID ID0 = Op0->getIntrinsicID();
3665 if (ID0 != Intrinsic::hexagon_V6_vandqrt &&
3666 ID0 != Intrinsic::hexagon_V6_vandqrt_128B)
3667 break;
3668 Value *Bytes = Op0->getArgOperand(1), *Mask = II->getArgOperand(1);
3669 uint64_t Bytes1 = computeKnownBits(Bytes, Op0).One.getZExtValue();
3670 uint64_t Mask1 = computeKnownBits(Mask, II).One.getZExtValue();
3671 // Check if every byte has common bits in Bytes and Mask.
3672 uint64_t C = Bytes1 & Mask1;
3673 if ((C & 0xFF) && (C & 0xFF00) && (C & 0xFF0000) && (C & 0xFF000000))
3674 return replaceInstUsesWith(*II, Op0->getArgOperand(0));
3675 }
3676 break;
3677 }
3678 case Intrinsic::stackrestore: {
3679 enum class ClassifyResult {
3680 None,
3681 Alloca,
3682 StackRestore,
3683 CallWithSideEffects,
3684 };
3685 auto Classify = [](const Instruction *I) {
3686 if (isa<AllocaInst>(I))
3687 return ClassifyResult::Alloca;
3688
3689 if (auto *CI = dyn_cast<CallInst>(I)) {
3690 if (auto *II = dyn_cast<IntrinsicInst>(CI)) {
3691 if (II->getIntrinsicID() == Intrinsic::stackrestore)
3692 return ClassifyResult::StackRestore;
3693
3694 if (II->mayHaveSideEffects())
3695 return ClassifyResult::CallWithSideEffects;
3696 } else {
3697 // Consider all non-intrinsic calls to be side effects
3698 return ClassifyResult::CallWithSideEffects;
3699 }
3700 }
3701
3702 return ClassifyResult::None;
3703 };
3704
3705 // If the stacksave and the stackrestore are in the same BB, and there is
3706 // no intervening call, alloca, or stackrestore of a different stacksave,
3707 // remove the restore. This can happen when variable allocas are DCE'd.
3708 if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
3709 if (SS->getIntrinsicID() == Intrinsic::stacksave &&
3710 SS->getParent() == II->getParent()) {
3711 BasicBlock::iterator BI(SS);
3712 bool CannotRemove = false;
3713 for (++BI; &*BI != II; ++BI) {
3714 switch (Classify(&*BI)) {
3715 case ClassifyResult::None:
3716 // So far so good, look at next instructions.
3717 break;
3718
3719 case ClassifyResult::StackRestore:
3720 // If we found an intervening stackrestore for a different
3721 // stacksave, we can't remove the stackrestore. Otherwise, continue.
3722 if (cast<IntrinsicInst>(*BI).getArgOperand(0) != SS)
3723 CannotRemove = true;
3724 break;
3725
3726 case ClassifyResult::Alloca:
3727 case ClassifyResult::CallWithSideEffects:
3728 // If we found an alloca, a non-intrinsic call, or an intrinsic
3729 // call with side effects, we can't remove the stackrestore.
3730 CannotRemove = true;
3731 break;
3732 }
3733 if (CannotRemove)
3734 break;
3735 }
3736
3737 if (!CannotRemove)
3738 return eraseInstFromFunction(CI);
3739 }
3740 }
3741
3742 // Scan down this block to see if there is another stack restore in the
3743 // same block without an intervening call/alloca.
3745 Instruction *TI = II->getParent()->getTerminator();
3746 bool CannotRemove = false;
3747 for (++BI; &*BI != TI; ++BI) {
3748 switch (Classify(&*BI)) {
3749 case ClassifyResult::None:
3750 // So far so good, look at next instructions.
3751 break;
3752
3753 case ClassifyResult::StackRestore:
3754 // If there is a stackrestore below this one, remove this one.
3755 return eraseInstFromFunction(CI);
3756
3757 case ClassifyResult::Alloca:
3758 case ClassifyResult::CallWithSideEffects:
3759 // If we found an alloca, a non-intrinsic call, or an intrinsic call
3760 // with side effects (such as llvm.stacksave and llvm.read_register),
3761 // we can't remove the stack restore.
3762 CannotRemove = true;
3763 break;
3764 }
3765 if (CannotRemove)
3766 break;
3767 }
3768
3769 // If the stack restore is in a return, resume, or unwind block and if there
3770 // are no allocas or calls between the restore and the return, nuke the
3771 // restore.
3772 if (!CannotRemove && (isa<ReturnInst>(TI) || isa<ResumeInst>(TI)))
3773 return eraseInstFromFunction(CI);
3774 break;
3775 }
3776 case Intrinsic::lifetime_end:
3777 // Asan needs to poison memory to detect invalid access which is possible
3778 // even for empty lifetime range.
3779 if (II->getFunction()->hasFnAttribute(Attribute::SanitizeAddress) ||
3780 II->getFunction()->hasFnAttribute(Attribute::SanitizeMemory) ||
3781 II->getFunction()->hasFnAttribute(Attribute::SanitizeHWAddress) ||
3782 II->getFunction()->hasFnAttribute(Attribute::SanitizeMemTag))
3783 break;
3784
3785 if (removeTriviallyEmptyRange(*II, *this, [](const IntrinsicInst &I) {
3786 return I.getIntrinsicID() == Intrinsic::lifetime_start;
3787 }))
3788 return nullptr;
3789 break;
3790 case Intrinsic::assume: {
3791 for (auto [Idx, OBU] : llvm::enumerate(II->operand_bundles())) {
3792 auto RemoveBundle = [&, Idx = Idx]() -> Instruction * {
3793 if (II->getNumOperandBundles() == 1)
3794 return eraseInstFromFunction(*II);
3796 };
3797
3798 switch (getBundleAttrFromOBU(OBU)) {
3799 case BundleAttr::None:
3800 llvm_unreachable("Unexpected Attribute");
3801 case BundleAttr::Align: {
3802 // Try to remove redundant alignment assumptions.
3803 auto [Ptr, _, OffsetPtr, Alignment, Offset] = getAssumeAlignInfo(OBU);
3804
3805 if (!Alignment)
3806 break;
3807
3808 // Remove align 1 and non-power-of-two bundles; they don't add any
3809 // useful information.
3810 if (*Alignment == 1 || !isPowerOf2_64(*Alignment))
3811 return RemoveBundle();
3812
3813 if (auto *GEP = dyn_cast<GEPOperator>(Ptr);
3814 GEP &&
3815 GEP->getMaxPreservedAlignment(getDataLayout()) >= *Alignment) {
3816 Builder.CreateAlignmentAssumption(
3817 getDataLayout(), GEP->getPointerOperand(), *Alignment,
3818 OffsetPtr ? const_cast<Value *>(OffsetPtr->get()) : nullptr);
3819 return RemoveBundle();
3820 }
3821
3822 if (!Offset)
3823 break;
3824
3825 Value *BasePtr;
3826 const APInt *PtrOffset;
3827 if (match(Ptr.get(), m_PtrAdd(m_Value(BasePtr), m_APInt(PtrOffset)))) {
3828 auto PtrOffsetVal =
3829 PtrOffset->sextOrTrunc(DL.getIndexTypeSizeInBits(Ptr->getType()))
3830 .trySExtValue();
3831 if (!PtrOffsetVal)
3832 break;
3833 Builder.CreateAlignmentAssumption(
3834 DL, BasePtr, *Alignment,
3835 Builder.getInt64(*Offset - *PtrOffsetVal));
3836 return RemoveBundle();
3837 }
3838
3839 // Don't try to remove align assumptions for pointers derived from
3840 // arguments. We might lose information if the function gets inline and
3841 // the align argument attribute disappears.
3842 Value *UO = getUnderlyingObject(Ptr);
3843 if (!UO || isa<Argument>(UO))
3844 break;
3845
3846 // Compute known bits for the pointer and drop the assume if the
3847 // known alignment isn't increased by it.
3848 auto AlignMask = (*Alignment - 1);
3849 if (KnownBits KB = computeKnownBits(Ptr, II);
3850 (KB.Zero & AlignMask) == (~*Offset & AlignMask) &&
3851 (KB.One & AlignMask) == (*Offset & AlignMask))
3852 return RemoveBundle();
3853 break;
3854 }
3855
3856 case BundleAttr::Dereferenceable: {
3857 auto [Ptr, _, Count] = getAssumeDereferenceableInfo(OBU);
3858
3859 if (!Count)
3860 break;
3861
3862 if (*Count == 0 ||
3864 getSimplifyQuery().getWithInstruction(II)))
3865 return RemoveBundle();
3866
3867 break;
3868 }
3869
3870 case BundleAttr::Ignore:
3871 return RemoveBundle();
3872
3873 case BundleAttr::NonNull: {
3874 auto [Ptr] = llvm::getAssumeNonNullInfo(OBU);
3875
3876 // Drop assume if we can prove nonnull without it
3877 if (isKnownNonZero(Ptr, getSimplifyQuery().getWithInstruction(II)))
3878 return RemoveBundle();
3879
3880 // Fold the assume into metadata if it's valid at the load
3881 if (auto *LI = dyn_cast<LoadInst>(Ptr);
3882 LI &&
3883 isValidAssumeForContext(II, LI, &DT, /*AllowEphemerals=*/true)) {
3884 MDNode *MD = MDNode::get(II->getContext(), {});
3885 LI->setMetadata(LLVMContext::MD_nonnull, MD);
3886 LI->setMetadata(LLVMContext::MD_noundef, MD);
3887 return RemoveBundle();
3888 }
3889
3890 if (auto *GEP = dyn_cast<GEPOperator>(Ptr);
3891 GEP && GEP->isInBounds() &&
3892 !NullPointerIsDefined(II->getFunction(),
3893 Ptr->getType()->getPointerAddressSpace())) {
3894 Builder.CreateNonnullAssumption(GEP->stripInBoundsOffsets());
3895 return RemoveBundle();
3896 }
3897
3898 // TODO: apply nonnull return attributes to calls and invokes
3899 break;
3900 }
3901
3902 case BundleAttr::NoUndef: {
3903 auto [Val] = getAssumeNoUndefInfo(OBU);
3904
3906 return RemoveBundle();
3907
3908 if (auto *LI = dyn_cast<LoadInst>(Val);
3909 LI &&
3910 isValidAssumeForContext(II, LI, &DT, /*AllowEphemerals=*/true)) {
3911 LI->setMetadata(LLVMContext::MD_noundef,
3912 MDNode::get(II->getContext(), {}));
3913 return RemoveBundle();
3914 }
3915
3916 } break;
3917
3918 case BundleAttr::SeparateStorage: {
3919 auto [Ptr1, Ptr2] = getAssumeSeparateStorageInfo(OBU);
3920 // Separate storage assumptions apply to the underlying allocations, not
3921 // any particular pointer within them. When evaluating the hints for AA
3922 // purposes we getUnderlyingObject them; by precomputing the answers
3923 // here we can avoid having to do so repeatedly there.
3924 auto MaybeSimplifyHint = [&](const Use &U) {
3925 Value *Hint = U.get();
3926 // Not having a limit is safe because InstCombine removes unreachable
3927 // code.
3928 Value *UnderlyingObject = getUnderlyingObject(Hint, /*MaxLookup*/ 0);
3929 if (Hint != UnderlyingObject)
3930 replaceUse(const_cast<Use &>(U), UnderlyingObject);
3931 };
3932 MaybeSimplifyHint(Ptr1);
3933 MaybeSimplifyHint(Ptr2);
3934 } break;
3935
3936 // TODO: Drop these assumes when they are redundant
3937 case BundleAttr::DereferenceableOrNull:
3938 break;
3939
3940 // This cannot be simplified
3941 case BundleAttr::Cold:
3942 break;
3943 }
3944 }
3945
3946 // If the assume has operand bundles, the folds below will never work, so
3947 // don't bother trying.
3948 if (II->hasOperandBundles())
3949 break;
3950
3951 Value *IIOperand = II->getArgOperand(0);
3952
3953 // Canonicalize assume(a && b) -> assume(a); assume(b);
3954 // Note: New assumption intrinsics created here are registered by
3955 // the InstCombineIRInserter object.
3956 Value *A, *B;
3957 if (match(IIOperand, m_LogicalAnd(m_Value(A), m_Value(B)))) {
3958 Builder.CreateAssumption(A);
3959 Builder.CreateAssumption(B);
3960 return eraseInstFromFunction(*II);
3961 }
3962 // assume(!(a || b)) -> assume(!a); assume(!b);
3963 if (match(IIOperand, m_Not(m_LogicalOr(m_Value(A), m_Value(B))))) {
3964 Builder.CreateAssumption(Builder.CreateNot(A));
3965 Builder.CreateAssumption(Builder.CreateNot(B));
3966 return eraseInstFromFunction(*II);
3967 }
3968
3969 // Convert nonnull assume like:
3970 // %A = icmp ne i32* %PTR, null
3971 // call void @llvm.assume(i1 %A)
3972 // into
3973 // call void @llvm.assume(i1 true) [ "nonnull"(i32* %PTR) ]
3974 if (match(IIOperand,
3976 A->getType()->isPointerTy()) {
3977 Builder.CreateNonnullAssumption(A);
3978 return eraseInstFromFunction(*II);
3979 }
3980
3981 // Convert alignment assume like:
3982 // %B = ptrtoint ptr %A to i64
3983 // %C = and i64 %B, Constant
3984 // %D = icmp eq i64 %C, 0
3985 // call void @llvm.assume(i1 %D)
3986 // into
3987 // call void @llvm.assume(i1 true) [ "align"(ptr [[A]], i64 Constant + 1)]
3988 uint64_t AlignMask = 1;
3989 if ((match(IIOperand, m_Not(m_Trunc(m_Value(A)))) ||
3990 match(IIOperand,
3992 m_And(m_Value(A), m_ConstantInt(AlignMask)),
3993 m_Zero())))) {
3994 if (isPowerOf2_64(AlignMask + 1) &&
3996 Builder.CreateAlignmentAssumption(getDataLayout(), A, AlignMask + 1);
3997 return eraseInstFromFunction(*II);
3998 }
3999 }
4000
4001 // Remove assumes on true/false
4002 if (auto *CI = dyn_cast<ConstantInt>(IIOperand);
4003 CI || isa<UndefValue, PoisonValue>(IIOperand)) {
4004 if (!CI || CI->isZero())
4006 return eraseInstFromFunction(*II);
4007 }
4008
4009 // Update the cache of affected values for this assumption (we might be
4010 // here because we just simplified the condition).
4011 AC.updateAffectedValues(cast<AssumeInst>(II));
4012 break;
4013 }
4014 case Intrinsic::experimental_guard: {
4015 // Is this guard followed by another guard? We scan forward over a small
4016 // fixed window of instructions to handle common cases with conditions
4017 // computed between guards.
4018 Instruction *NextInst = II->getNextNode();
4019 for (unsigned i = 0; i < GuardWideningWindow; i++) {
4020 // Note: Using context-free form to avoid compile time blow up
4021 if (!isSafeToSpeculativelyExecute(NextInst))
4022 break;
4023 NextInst = NextInst->getNextNode();
4024 }
4025 Value *NextCond = nullptr;
4026 if (match(NextInst,
4028 Value *CurrCond = II->getArgOperand(0);
4029
4030 // Remove a guard that it is immediately preceded by an identical guard.
4031 // Otherwise canonicalize guard(a); guard(b) -> guard(a & b).
4032 if (CurrCond != NextCond) {
4033 Instruction *MoveI = II->getNextNode();
4034 while (MoveI != NextInst) {
4035 auto *Temp = MoveI;
4036 MoveI = MoveI->getNextNode();
4037 Temp->moveBefore(II->getIterator());
4038 }
4039 replaceOperand(*II, 0, Builder.CreateAnd(CurrCond, NextCond));
4040 }
4041 eraseInstFromFunction(*NextInst);
4042 return II;
4043 }
4044 break;
4045 }
4046 case Intrinsic::vector_insert: {
4047 Value *Vec = II->getArgOperand(0);
4048 Value *SubVec = II->getArgOperand(1);
4049 Value *Idx = II->getArgOperand(2);
4050 auto *DstTy = dyn_cast<FixedVectorType>(II->getType());
4051 auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType());
4052 auto *SubVecTy = dyn_cast<FixedVectorType>(SubVec->getType());
4053
4054 // Only canonicalize if the destination vector, Vec, and SubVec are all
4055 // fixed vectors.
4056 if (DstTy && VecTy && SubVecTy) {
4057 unsigned DstNumElts = DstTy->getNumElements();
4058 unsigned VecNumElts = VecTy->getNumElements();
4059 unsigned SubVecNumElts = SubVecTy->getNumElements();
4060 unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
4061
4062 // An insert that entirely overwrites Vec with SubVec is a nop.
4063 if (VecNumElts == SubVecNumElts)
4064 return replaceInstUsesWith(CI, SubVec);
4065
4066 // Widen SubVec into a vector of the same width as Vec, since
4067 // shufflevector requires the two input vectors to be the same width.
4068 // Elements beyond the bounds of SubVec within the widened vector are
4069 // undefined.
4070 SmallVector<int, 8> WidenMask;
4071 unsigned i;
4072 for (i = 0; i != SubVecNumElts; ++i)
4073 WidenMask.push_back(i);
4074 for (; i != VecNumElts; ++i)
4075 WidenMask.push_back(PoisonMaskElem);
4076
4077 Value *WidenShuffle = Builder.CreateShuffleVector(SubVec, WidenMask);
4078
4080 for (unsigned i = 0; i != IdxN; ++i)
4081 Mask.push_back(i);
4082 for (unsigned i = DstNumElts; i != DstNumElts + SubVecNumElts; ++i)
4083 Mask.push_back(i);
4084 for (unsigned i = IdxN + SubVecNumElts; i != DstNumElts; ++i)
4085 Mask.push_back(i);
4086
4087 Value *Shuffle = Builder.CreateShuffleVector(Vec, WidenShuffle, Mask);
4088 return replaceInstUsesWith(CI, Shuffle);
4089 }
4090 break;
4091 }
4092 case Intrinsic::vector_extract: {
4093 Value *Vec = II->getArgOperand(0);
4094 Value *Idx = II->getArgOperand(1);
4095
4096 Type *ReturnType = II->getType();
4097 // (extract_vector (insert_vector InsertTuple, InsertValue, InsertIdx),
4098 // ExtractIdx)
4099 unsigned ExtractIdx = cast<ConstantInt>(Idx)->getZExtValue();
4100 Value *InsertTuple, *InsertIdx, *InsertValue;
4102 m_Value(InsertValue),
4103 m_Value(InsertIdx))) &&
4104 InsertValue->getType() == ReturnType) {
4105 unsigned Index = cast<ConstantInt>(InsertIdx)->getZExtValue();
4106 // Case where we get the same index right after setting it.
4107 // extract.vector(insert.vector(InsertTuple, InsertValue, Idx), Idx) -->
4108 // InsertValue
4109 if (ExtractIdx == Index)
4110 return replaceInstUsesWith(CI, InsertValue);
4111 // If we are getting a different index than what was set in the
4112 // insert.vector intrinsic. We can just set the input tuple to the one up
4113 // in the chain. extract.vector(insert.vector(InsertTuple, InsertValue,
4114 // InsertIndex), ExtractIndex)
4115 // --> extract.vector(InsertTuple, ExtractIndex)
4116 else
4117 return replaceOperand(CI, 0, InsertTuple);
4118 }
4119
4120 ConstantInt *ALMUpperBound;
4122 m_Value(), m_ConstantInt(ALMUpperBound)))) {
4123 const auto &Attrs = II->getFunction()->getAttributes().getFnAttrs();
4124 unsigned VScaleMin = Attrs.getVScaleRangeMin();
4125 unsigned ScaleFactor =
4126 cast<VectorType>(ReturnType)->isScalableTy() ? VScaleMin : 1;
4127 if (ExtractIdx * ScaleFactor >= ALMUpperBound->getZExtValue())
4128 return replaceInstUsesWith(CI,
4129 ConstantVector::getNullValue(ReturnType));
4130 }
4131
4132 auto *DstTy = dyn_cast<VectorType>(ReturnType);
4133 auto *VecTy = dyn_cast<VectorType>(Vec->getType());
4134
4135 if (DstTy && VecTy) {
4136 auto DstEltCnt = DstTy->getElementCount();
4137 auto VecEltCnt = VecTy->getElementCount();
4138 unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
4139
4140 // Extracting the entirety of Vec is a nop.
4141 if (DstEltCnt == VecTy->getElementCount()) {
4142 replaceInstUsesWith(CI, Vec);
4143 return eraseInstFromFunction(CI);
4144 }
4145
4146 // Only canonicalize to shufflevector if the destination vector and
4147 // Vec are fixed vectors.
4148 if (VecEltCnt.isScalable() || DstEltCnt.isScalable())
4149 break;
4150
4152 for (unsigned i = 0; i != DstEltCnt.getKnownMinValue(); ++i)
4153 Mask.push_back(IdxN + i);
4154
4155 Value *Shuffle = Builder.CreateShuffleVector(Vec, Mask);
4156 return replaceInstUsesWith(CI, Shuffle);
4157 }
4158 break;
4159 }
4160 case Intrinsic::experimental_vp_reverse: {
4161 Value *X;
4162 Value *Vec = II->getArgOperand(0);
4163 Value *Mask = II->getArgOperand(1);
4164 if (!match(Mask, m_AllOnes()))
4165 break;
4166 Value *EVL = II->getArgOperand(2);
4167 // TODO: Canonicalize experimental.vp.reverse after unop/binops?
4168 // rev(unop rev(X)) --> unop X
4169 if (match(Vec,
4171 m_Value(X), m_AllOnes(), m_Specific(EVL)))))) {
4172 auto *OldUnOp = cast<UnaryOperator>(Vec);
4174 OldUnOp->getOpcode(), X, OldUnOp, OldUnOp->getName(),
4175 II->getIterator());
4176 return replaceInstUsesWith(CI, NewUnOp);
4177 }
4178 break;
4179 }
4180 case Intrinsic::vector_reduce_or:
4181 case Intrinsic::vector_reduce_and: {
4182 // Canonicalize logical or/and reductions:
4183 // Or reduction for i1 is represented as:
4184 // %val = bitcast <ReduxWidth x i1> to iReduxWidth
4185 // %res = cmp ne iReduxWidth %val, 0
4186 // And reduction for i1 is represented as:
4187 // %val = bitcast <ReduxWidth x i1> to iReduxWidth
4188 // %res = cmp eq iReduxWidth %val, 11111
4189 Value *Arg = II->getArgOperand(0);
4190 Value *Vect;
4191
4192 if (Value *NewOp =
4193 simplifyReductionOperand(Arg, /*CanReorderLanes=*/true)) {
4194 replaceUse(II->getOperandUse(0), NewOp);
4195 return II;
4196 }
4197
4198 if (match(Arg, m_ZExtOrSExtOrSelf(m_Value(Vect)))) {
4199 if (auto *FTy = dyn_cast<FixedVectorType>(Vect->getType()))
4200 if (FTy->getElementType() == Builder.getInt1Ty()) {
4201 Value *Res = Builder.CreateBitCast(
4202 Vect, Builder.getIntNTy(FTy->getNumElements()));
4203 if (IID == Intrinsic::vector_reduce_and) {
4204 Res = Builder.CreateICmpEQ(
4206 } else {
4207 assert(IID == Intrinsic::vector_reduce_or &&
4208 "Expected or reduction.");
4209 Res = Builder.CreateIsNotNull(Res);
4210 }
4211 if (Arg != Vect)
4212 Res = Builder.CreateCast(cast<CastInst>(Arg)->getOpcode(), Res,
4213 II->getType());
4214 return replaceInstUsesWith(CI, Res);
4215 }
4216 }
4217 [[fallthrough]];
4218 }
4219 case Intrinsic::vector_reduce_add: {
4220 if (IID == Intrinsic::vector_reduce_add) {
4221 // Convert vector_reduce_add(ZExt(<n x i1>)) to
4222 // ZExtOrTrunc(ctpop(bitcast <n x i1> to in)).
4223 // Convert vector_reduce_add(SExt(<n x i1>)) to
4224 // -ZExtOrTrunc(ctpop(bitcast <n x i1> to in)).
4225 // Convert vector_reduce_add(<n x i1>) to
4226 // Trunc(ctpop(bitcast <n x i1> to in)).
4227 Value *Arg = II->getArgOperand(0);
4228 Value *Vect;
4229
4230 if (Value *NewOp =
4231 simplifyReductionOperand(Arg, /*CanReorderLanes=*/true)) {
4232 replaceUse(II->getOperandUse(0), NewOp);
4233 return II;
4234 }
4235
4236 // vector.reduce.add.vNiM(splat(%x)) -> mul(%x, N)
4237 if (Value *Splat = getSplatValue(Arg)) {
4238 ElementCount VecToReduceCount =
4239 cast<VectorType>(Arg->getType())->getElementCount();
4240 if (VecToReduceCount.isFixed()) {
4241 unsigned VectorSize = VecToReduceCount.getFixedValue();
4242 return BinaryOperator::CreateMul(
4243 Splat,
4244 ConstantInt::get(Splat->getType(), VectorSize, /*IsSigned=*/false,
4245 /*ImplicitTrunc=*/true));
4246 }
4247 }
4248
4249 if (match(Arg, m_ZExtOrSExtOrSelf(m_Value(Vect)))) {
4250 if (auto *FTy = dyn_cast<FixedVectorType>(Vect->getType()))
4251 if (FTy->getElementType() == Builder.getInt1Ty()) {
4252 Value *V = Builder.CreateBitCast(
4253 Vect, Builder.getIntNTy(FTy->getNumElements()));
4254 Value *Res = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, V);
4255 Res = Builder.CreateZExtOrTrunc(Res, II->getType());
4256 if (Arg != Vect &&
4257 cast<Instruction>(Arg)->getOpcode() == Instruction::SExt)
4258 Res = Builder.CreateNeg(Res);
4259 return replaceInstUsesWith(CI, Res);
4260 }
4261 }
4262 }
4263 [[fallthrough]];
4264 }
4265 case Intrinsic::vector_reduce_xor: {
4266 if (IID == Intrinsic::vector_reduce_xor) {
4267 // Exclusive disjunction reduction over the vector with
4268 // (potentially-extended) i1 element type is actually a
4269 // (potentially-extended) arithmetic `add` reduction over the original
4270 // non-extended value:
4271 // vector_reduce_xor(?ext(<n x i1>))
4272 // -->
4273 // ?ext(vector_reduce_add(<n x i1>))
4274 Value *Arg = II->getArgOperand(0);
4275 Value *Vect;
4276
4277 if (Value *NewOp =
4278 simplifyReductionOperand(Arg, /*CanReorderLanes=*/true)) {
4279 replaceUse(II->getOperandUse(0), NewOp);
4280 return II;
4281 }
4282
4283 if (match(Arg, m_ZExtOrSExtOrSelf(m_Value(Vect)))) {
4284 if (auto *VTy = dyn_cast<VectorType>(Vect->getType()))
4285 if (VTy->getElementType() == Builder.getInt1Ty()) {
4286 Value *Res = Builder.CreateAddReduce(Vect);
4287 if (Arg != Vect)
4288 Res = Builder.CreateCast(cast<CastInst>(Arg)->getOpcode(), Res,
4289 II->getType());
4290 return replaceInstUsesWith(CI, Res);
4291 }
4292 }
4293 }
4294 [[fallthrough]];
4295 }
4296 case Intrinsic::vector_reduce_mul: {
4297 if (IID == Intrinsic::vector_reduce_mul) {
4298 Value *Arg = II->getArgOperand(0);
4299
4300 if (Value *NewOp =
4301 simplifyReductionOperand(Arg, /*CanReorderLanes=*/true)) {
4302 replaceUse(II->getOperandUse(0), NewOp);
4303 return II;
4304 }
4305
4306 // vector_reduce_mul(zext(<n x i1>)), or
4307 // vector_reduce_mul(sext(<n x i1>)) (if n is even) -->
4308 // zext(vector_reduce_and(<n x i1>)).
4309 // (The sext case doesn't work if n is odd because multiplying an odd
4310 // number of -1's produces -1, not 1.)
4311 Value *Vect;
4312 bool IsZext = match(Arg, m_ZExt(m_Value(Vect))) &&
4313 Vect->getType()->isIntOrIntVectorTy(1);
4314 bool IsSext =
4315 match(Arg, m_SExt(m_Value(Vect))) &&
4316 Vect->getType()->isIntOrIntVectorTy(1) &&
4317 cast<VectorType>(Vect->getType())->getElementCount().isKnownEven();
4318 if (IsZext || IsSext) {
4319 Value *Res = Builder.CreateAndReduce(Vect);
4320 return CastInst::Create(Instruction::ZExt, Res, II->getType());
4321 }
4322
4323 // vector_reduce_mul(<n x i1>) --> vector_reduce_and(<n x i1>)
4324 if (Arg->getType()->isIntOrIntVectorTy(1))
4325 return replaceInstUsesWith(CI, Builder.CreateAndReduce(Arg));
4326 }
4327 [[fallthrough]];
4328 }
4329 case Intrinsic::vector_reduce_umin:
4330 case Intrinsic::vector_reduce_umax: {
4331 if (IID == Intrinsic::vector_reduce_umin ||
4332 IID == Intrinsic::vector_reduce_umax) {
4333 // UMin/UMax reduction over the vector with (potentially-extended)
4334 // i1 element type is actually a (potentially-extended)
4335 // logical `and`/`or` reduction over the original non-extended value:
4336 // vector_reduce_u{min,max}(?ext(<n x i1>))
4337 // -->
4338 // ?ext(vector_reduce_{and,or}(<n x i1>))
4339 Value *Arg = II->getArgOperand(0);
4340 Value *Vect;
4341
4342 if (Value *NewOp =
4343 simplifyReductionOperand(Arg, /*CanReorderLanes=*/true)) {
4344 replaceUse(II->getOperandUse(0), NewOp);
4345 return II;
4346 }
4347
4348 if (match(Arg, m_ZExtOrSExtOrSelf(m_Value(Vect)))) {
4349 if (auto *VTy = dyn_cast<VectorType>(Vect->getType()))
4350 if (VTy->getElementType() == Builder.getInt1Ty()) {
4351 Value *Res = IID == Intrinsic::vector_reduce_umin
4352 ? Builder.CreateAndReduce(Vect)
4353 : Builder.CreateOrReduce(Vect);
4354 if (Arg != Vect)
4355 Res = Builder.CreateCast(cast<CastInst>(Arg)->getOpcode(), Res,
4356 II->getType());
4357 return replaceInstUsesWith(CI, Res);
4358 }
4359 }
4360 }
4361 [[fallthrough]];
4362 }
4363 case Intrinsic::vector_reduce_smin:
4364 case Intrinsic::vector_reduce_smax: {
4365 if (IID == Intrinsic::vector_reduce_smin ||
4366 IID == Intrinsic::vector_reduce_smax) {
4367 // SMin/SMax reduction over the vector with (potentially-extended)
4368 // i1 element type is actually a (potentially-extended)
4369 // logical `and`/`or` reduction over the original non-extended value:
4370 // vector_reduce_s{min,max}(<n x i1>)
4371 // -->
4372 // vector_reduce_{or,and}(<n x i1>)
4373 // and
4374 // vector_reduce_s{min,max}(sext(<n x i1>))
4375 // -->
4376 // sext(vector_reduce_{or,and}(<n x i1>))
4377 // and
4378 // vector_reduce_s{min,max}(zext(<n x i1>))
4379 // -->
4380 // zext(vector_reduce_{and,or}(<n x i1>))
4381 Value *Arg = II->getArgOperand(0);
4382 Value *Vect;
4383
4384 if (Value *NewOp =
4385 simplifyReductionOperand(Arg, /*CanReorderLanes=*/true)) {
4386 replaceUse(II->getOperandUse(0), NewOp);
4387 return II;
4388 }
4389
4390 if (match(Arg, m_ZExtOrSExtOrSelf(m_Value(Vect)))) {
4391 if (auto *VTy = dyn_cast<VectorType>(Vect->getType()))
4392 if (VTy->getElementType() == Builder.getInt1Ty()) {
4393 Instruction::CastOps ExtOpc = Instruction::CastOps::CastOpsEnd;
4394 if (Arg != Vect)
4395 ExtOpc = cast<CastInst>(Arg)->getOpcode();
4396 Value *Res = ((IID == Intrinsic::vector_reduce_smin) ==
4397 (ExtOpc == Instruction::CastOps::ZExt))
4398 ? Builder.CreateAndReduce(Vect)
4399 : Builder.CreateOrReduce(Vect);
4400 if (Arg != Vect)
4401 Res = Builder.CreateCast(ExtOpc, Res, II->getType());
4402 return replaceInstUsesWith(CI, Res);
4403 }
4404 }
4405 }
4406 [[fallthrough]];
4407 }
4408 case Intrinsic::vector_reduce_fmax:
4409 case Intrinsic::vector_reduce_fmin:
4410 case Intrinsic::vector_reduce_fadd:
4411 case Intrinsic::vector_reduce_fmul: {
4412 bool CanReorderLanes = (IID != Intrinsic::vector_reduce_fadd &&
4413 IID != Intrinsic::vector_reduce_fmul) ||
4414 II->hasAllowReassoc();
4415 const unsigned ArgIdx = (IID == Intrinsic::vector_reduce_fadd ||
4416 IID == Intrinsic::vector_reduce_fmul)
4417 ? 1
4418 : 0;
4419 Value *Arg = II->getArgOperand(ArgIdx);
4420 if (Value *NewOp = simplifyReductionOperand(Arg, CanReorderLanes)) {
4421 replaceUse(II->getOperandUse(ArgIdx), NewOp);
4422 return nullptr;
4423 }
4424 break;
4425 }
4426 case Intrinsic::is_fpclass: {
4427 if (Instruction *I = foldIntrinsicIsFPClass(*II))
4428 return I;
4429 break;
4430 }
4431 case Intrinsic::threadlocal_address: {
4432 Align MinAlign = getKnownAlignment(II->getArgOperand(0), DL, II, &AC, &DT);
4433 MaybeAlign Align = II->getRetAlign();
4434 if (MinAlign > Align.valueOrOne()) {
4435 II->addRetAttr(Attribute::getWithAlignment(II->getContext(), MinAlign));
4436 return II;
4437 }
4438 break;
4439 }
4440 case Intrinsic::fptoui_sat:
4441 case Intrinsic::fptosi_sat:
4442 if (Instruction *I = foldItoFPtoI(*II))
4443 return I;
4444 break;
4445 case Intrinsic::frexp: {
4446 // frexp(frexp(x).fract) -> { frexp(x).fract, 0 }: the fraction operand is
4447 // already normalized, so the first result is idempotent and the second is
4448 // zero.
4449 if (match(II->getArgOperand(0),
4451 Value *Res = Builder.CreateInsertValue(PoisonValue::get(II->getType()),
4452 II->getArgOperand(0), 0);
4453 Res = Builder.CreateInsertValue(
4454 Res, Constant::getNullValue(II->getType()->getStructElementType(1)),
4455 1);
4456 return replaceInstUsesWith(*II, Res);
4457 }
4458 break;
4459 }
4460 case Intrinsic::get_active_lane_mask: {
4461 const APInt *Op0, *Op1;
4462 if (match(II->getOperand(0), m_StrictlyPositive(Op0)) &&
4463 match(II->getOperand(1), m_APInt(Op1))) {
4464 Type *OpTy = II->getOperand(0)->getType();
4465 return replaceInstUsesWith(
4466 *II, Builder.CreateIntrinsic(
4467 II->getType(), Intrinsic::get_active_lane_mask,
4468 {Constant::getNullValue(OpTy),
4469 ConstantInt::get(OpTy, Op1->usub_sat(*Op0))}));
4470 }
4471 break;
4472 }
4473 case Intrinsic::experimental_get_vector_length: {
4474 // get.vector.length(Cnt, MaxLanes) --> Cnt when Cnt <= MaxLanes
4475 unsigned BitWidth =
4476 std::max(II->getArgOperand(0)->getType()->getScalarSizeInBits(),
4477 II->getType()->getScalarSizeInBits());
4478 ConstantRange Cnt =
4479 computeConstantRangeIncludingKnownBits(II->getArgOperand(0), false,
4480 SQ.getWithInstruction(II))
4482 ConstantRange MaxLanes = cast<ConstantInt>(II->getArgOperand(1))
4483 ->getValue()
4484 .zextOrTrunc(Cnt.getBitWidth());
4485 if (cast<ConstantInt>(II->getArgOperand(2))->isOne())
4486 MaxLanes = MaxLanes.multiply(
4487 getVScaleRange(II->getFunction(), Cnt.getBitWidth()));
4488
4489 if (Cnt.icmp(CmpInst::ICMP_ULE, MaxLanes))
4490 return replaceInstUsesWith(
4491 *II, Builder.CreateZExtOrTrunc(II->getArgOperand(0), II->getType()));
4492 return nullptr;
4493 }
4494 default: {
4495 // Handle target specific intrinsics
4496 std::optional<Instruction *> V = targetInstCombineIntrinsic(*II);
4497 if (V)
4498 return *V;
4499 break;
4500 }
4501 }
4502
4503 // Try to fold intrinsic into select/phi operands. This is legal if:
4504 // * The intrinsic is speculatable.
4505 // * The operand is one of the following:
4506 // - a phi.
4507 // - a select with a scalar condition.
4508 // - a select with a vector condition and II is not a cross lane operation.
4510 for (Value *Op : II->args()) {
4511 if (auto *Sel = dyn_cast<SelectInst>(Op)) {
4512 bool IsVectorCond = Sel->getCondition()->getType()->isVectorTy();
4513 if (IsVectorCond &&
4514 (!isNotCrossLaneOperation(II) || !II->getType()->isVectorTy()))
4515 continue;
4516 // Don't replace a scalar select with a more expensive vector select if
4517 // we can't simplify both arms of the select.
4518 bool SimplifyBothArms =
4519 !Op->getType()->isVectorTy() && II->getType()->isVectorTy();
4521 *II, Sel, /*FoldWithMultiUse=*/false, SimplifyBothArms))
4522 return R;
4523 }
4524 if (auto *Phi = dyn_cast<PHINode>(Op))
4525 if (Instruction *R = foldOpIntoPhi(*II, Phi))
4526 return R;
4527 }
4528 }
4529
4531 return Shuf;
4532
4534 return replaceInstUsesWith(*II, Reverse);
4535
4537 return replaceInstUsesWith(*II, Res);
4538
4539 // Some intrinsics (like experimental_gc_statepoint) can be used in invoke
4540 // context, so it is handled in visitCallBase and we should trigger it.
4541 return visitCallBase(*II);
4542}
4543
4544// Fence instruction simplification
4546 auto *NFI = dyn_cast<FenceInst>(FI.getNextNode());
4547 // This check is solely here to handle arbitrary target-dependent syncscopes.
4548 // TODO: Can remove if does not matter in practice.
4549 if (NFI && FI.isIdenticalTo(NFI))
4550 return eraseInstFromFunction(FI);
4551
4552 // Returns true if FI1 is identical or stronger fence than FI2.
4553 auto isIdenticalOrStrongerFence = [](FenceInst *FI1, FenceInst *FI2) {
4554 auto FI1SyncScope = FI1->getSyncScopeID();
4555 // Consider same scope, where scope is global or single-thread.
4556 if (FI1SyncScope != FI2->getSyncScopeID() ||
4557 (FI1SyncScope != SyncScope::System &&
4558 FI1SyncScope != SyncScope::SingleThread))
4559 return false;
4560
4561 return isAtLeastOrStrongerThan(FI1->getOrdering(), FI2->getOrdering());
4562 };
4563 if (NFI && isIdenticalOrStrongerFence(NFI, &FI))
4564 return eraseInstFromFunction(FI);
4565
4566 if (auto *PFI = dyn_cast_or_null<FenceInst>(FI.getPrevNode()))
4567 if (isIdenticalOrStrongerFence(PFI, &FI))
4568 return eraseInstFromFunction(FI);
4569 return nullptr;
4570}
4571
4572// InvokeInst simplification
4574 return visitCallBase(II);
4575}
4576
4577// CallBrInst simplification
4579 return visitCallBase(CBI);
4580}
4581
4582// A simple parser for format string specifiers for the purposes of the
4583// modular-format attribute. In the case of malformed format strings this might
4584// under or over report the specifiers present, but such cases are undefined
4585// behavior.
4587 Bitset<256> Specifiers;
4588 for (size_t I = 0; I < FormatStr.size(); ++I) {
4589 if (FormatStr[I] != '%')
4590 continue;
4591
4592 // Check for escaped '%'.
4593 if (I + 1 < FormatStr.size() && FormatStr[I + 1] == '%') {
4594 ++I; // Skip the second '%'.
4595 continue;
4596 }
4597
4598 // Scan past allowed prefix characters.
4599 size_t J =
4600 FormatStr.find_first_not_of("0123456789-+ #0$.*'hlLjztqwvI", I + 1);
4601 if (J == StringRef::npos)
4602 break;
4603
4604 Specifiers.set(static_cast<unsigned char>(FormatStr[J]));
4605 I = J; // Resume search from after the specifier.
4606 }
4607 return Specifiers;
4608}
4609
4610static bool isAspectNeeded(StringRef Aspect, CallInst *CI,
4611 std::optional<unsigned> FirstArgIdx,
4612 const std::optional<Bitset<256>> &Specifiers) {
4613 if (Aspect == "float") {
4614 if (Specifiers) {
4615 static constexpr Bitset<256> FloatSpecifiers{'f', 'F', 'e', 'E',
4616 'g', 'G', 'a', 'A'};
4617 return (*Specifiers & FloatSpecifiers).any();
4618 }
4619 // Fallback to type-based check for dynamic format string.
4620 if (!FirstArgIdx)
4621 return true;
4622 return llvm::any_of(
4623 llvm::make_range(std::next(CI->arg_begin(), *FirstArgIdx),
4624 CI->arg_end()),
4625 [](Value *V) { return V->getType()->isFloatingPointTy(); });
4626 }
4627 if (Aspect == "fixed") {
4628 if (Specifiers) {
4629 static constexpr Bitset<256> FixedSpecifiers{'r', 'R', 'k', 'K'};
4630 return (*Specifiers & FixedSpecifiers).any();
4631 }
4632 // Fallback for fixed-point: assume needed if format is dynamic.
4633 return true;
4634 }
4635 // Unknown aspects are always considered to be needed.
4636 return true;
4637}
4638
4639static void referenceAspect(StringRef Aspect, StringRef ImplName, Module *M,
4640 IRBuilderBase &B) {
4641 SmallString<20> Name = ImplName;
4642 Name += '_';
4643 Name += Aspect;
4644 LLVMContext &Ctx = M->getContext();
4645 Function *RelocNoneFn =
4646 Intrinsic::getOrInsertDeclaration(M, Intrinsic::reloc_none);
4647 B.CreateCall(RelocNoneFn,
4648 {MetadataAsValue::get(Ctx, MDString::get(Ctx, Name))});
4649}
4650
4652 if (!CI->hasFnAttr("modular-format"))
4653 return nullptr;
4654
4656 llvm::split(CI->getFnAttr("modular-format").getValueAsString(), ','));
4657 if (Args.size() < 5)
4658 return nullptr;
4659
4660 StringRef FormatIdxStr = Args[1];
4661 StringRef FirstArgIdxStr = Args[2];
4662 StringRef FnName = Args[3];
4663 StringRef ImplName = Args[4];
4665
4666 unsigned FormatIdx;
4667 std::optional<unsigned> FirstArgIdx;
4668 [[maybe_unused]] bool Error;
4669 Error = FormatIdxStr.getAsInteger(10, FormatIdx);
4670 assert(!Error && "invalid format arg index");
4671 --FormatIdx; // 1-based to 0-based
4672
4673 FirstArgIdx.emplace();
4674 Error = FirstArgIdxStr.getAsInteger(10, *FirstArgIdx);
4675 assert(!Error && "invalid first arg index");
4676 if (*FirstArgIdx > 0)
4677 --*FirstArgIdx; // 1-based to 0-based
4678 else
4679 FirstArgIdx.reset();
4680
4681 if (AllAspects.empty())
4682 return nullptr;
4683
4684 Value *FormatVal = CI->getArgOperand(FormatIdx);
4685 StringRef FormatStr;
4686
4687 std::optional<Bitset<256>> Specifiers;
4688 if (getConstantStringInfo(FormatVal, FormatStr))
4689 Specifiers = parseFormatStringSpecifiers(FormatStr);
4690
4691 SmallVector<StringRef> NeededAspects;
4692 for (StringRef Aspect : AllAspects)
4693 if (isAspectNeeded(Aspect, CI, FirstArgIdx, Specifiers))
4694 NeededAspects.push_back(Aspect);
4695
4696 if (NeededAspects.size() == AllAspects.size())
4697 return nullptr;
4698
4699 Module *M = CI->getModule();
4700 LLVMContext &Ctx = M->getContext();
4701 Function *Callee = CI->getCalledFunction();
4702 FunctionCallee ModularFn = M->getOrInsertFunction(
4703 FnName, Callee->getFunctionType(),
4704 Callee->getAttributes().removeFnAttribute(Ctx, "modular-format"));
4705 CallInst *New = cast<CallInst>(CI->clone());
4706 New->setCalledFunction(ModularFn);
4707 New->removeFnAttr("modular-format");
4708 B.Insert(New);
4709
4710 llvm::sort(NeededAspects);
4711 for (StringRef Request : NeededAspects)
4712 referenceAspect(Request, ImplName, M, B);
4713
4714 return New;
4715}
4716
4717Instruction *InstCombinerImpl::tryOptimizeCall(CallInst *CI) {
4718 if (!CI->getCalledFunction()) return nullptr;
4719
4720 // Skip optimizing notail and musttail calls so
4721 // LibCallSimplifier::optimizeCall doesn't have to preserve those invariants.
4722 // LibCallSimplifier::optimizeCall should try to preserve tail calls though.
4723 if (CI->isMustTailCall() || CI->isNoTailCall())
4724 return nullptr;
4725
4726 auto InstCombineRAUW = [this](Instruction *From, Value *With) {
4727 replaceInstUsesWith(*From, With);
4728 };
4729 auto InstCombineErase = [this](Instruction *I) {
4731 };
4732 LibCallSimplifier Simplifier(DL, &TLI, &DT, &DC, &AC, ORE, BFI, PSI,
4733 InstCombineRAUW, InstCombineErase);
4734 if (Value *With = Simplifier.optimizeCall(CI, Builder)) {
4735 ++NumSimplified;
4736 return CI->use_empty() ? CI : replaceInstUsesWith(*CI, With);
4737 }
4738 if (Value *With = optimizeModularFormat(CI, Builder)) {
4739 ++NumSimplified;
4740 return CI->use_empty() ? CI : replaceInstUsesWith(*CI, With);
4741 }
4742
4743 return nullptr;
4744}
4745
4747 // Strip off at most one level of pointer casts, looking for an alloca. This
4748 // is good enough in practice and simpler than handling any number of casts.
4749 Value *Underlying = TrampMem->stripPointerCasts();
4750 if (Underlying != TrampMem &&
4751 (!Underlying->hasOneUse() || Underlying->user_back() != TrampMem))
4752 return nullptr;
4753 if (!isa<AllocaInst>(Underlying))
4754 return nullptr;
4755
4756 IntrinsicInst *InitTrampoline = nullptr;
4757 for (User *U : TrampMem->users()) {
4759 if (!II)
4760 return nullptr;
4761 if (II->getIntrinsicID() == Intrinsic::init_trampoline) {
4762 if (InitTrampoline)
4763 // More than one init_trampoline writes to this value. Give up.
4764 return nullptr;
4765 InitTrampoline = II;
4766 continue;
4767 }
4768 if (II->getIntrinsicID() == Intrinsic::adjust_trampoline)
4769 // Allow any number of calls to adjust.trampoline.
4770 continue;
4771 return nullptr;
4772 }
4773
4774 // No call to init.trampoline found.
4775 if (!InitTrampoline)
4776 return nullptr;
4777
4778 // Check that the alloca is being used in the expected way.
4779 if (InitTrampoline->getOperand(0) != TrampMem)
4780 return nullptr;
4781
4782 return InitTrampoline;
4783}
4784
4786 Value *TrampMem) {
4787 // Visit all the previous instructions in the basic block, and try to find a
4788 // init.trampoline which has a direct path to the adjust.trampoline.
4789 for (BasicBlock::iterator I = AdjustTramp->getIterator(),
4790 E = AdjustTramp->getParent()->begin();
4791 I != E;) {
4792 Instruction *Inst = &*--I;
4794 if (II->getIntrinsicID() == Intrinsic::init_trampoline &&
4795 II->getOperand(0) == TrampMem)
4796 return II;
4797 if (Inst->mayWriteToMemory())
4798 return nullptr;
4799 }
4800 return nullptr;
4801}
4802
4803// Given a call to llvm.adjust.trampoline, find and return the corresponding
4804// call to llvm.init.trampoline if the call to the trampoline can be optimized
4805// to a direct call to a function. Otherwise return NULL.
4807 Callee = Callee->stripPointerCasts();
4808 IntrinsicInst *AdjustTramp = dyn_cast<IntrinsicInst>(Callee);
4809 if (!AdjustTramp ||
4810 AdjustTramp->getIntrinsicID() != Intrinsic::adjust_trampoline)
4811 return nullptr;
4812
4813 Value *TrampMem = AdjustTramp->getOperand(0);
4814
4816 return IT;
4817 if (IntrinsicInst *IT = findInitTrampolineFromBB(AdjustTramp, TrampMem))
4818 return IT;
4819 return nullptr;
4820}
4821
4822Instruction *InstCombinerImpl::foldPtrAuthIntrinsicCallee(CallBase &Call) {
4823 const Value *Callee = Call.getCalledOperand();
4824 const auto *IPC = dyn_cast<IntToPtrInst>(Callee);
4825 if (!IPC || !IPC->isNoopCast(DL))
4826 return nullptr;
4827
4828 const auto *II = dyn_cast<IntrinsicInst>(IPC->getOperand(0));
4829 if (!II)
4830 return nullptr;
4831
4832 Intrinsic::ID IIID = II->getIntrinsicID();
4833 if (IIID != Intrinsic::ptrauth_resign && IIID != Intrinsic::ptrauth_sign)
4834 return nullptr;
4835
4836 // Isolate the ptrauth bundle from the others.
4837 std::optional<OperandBundleUse> PtrAuthBundleOrNone;
4839 for (unsigned BI = 0, BE = Call.getNumOperandBundles(); BI != BE; ++BI) {
4840 OperandBundleUse Bundle = Call.getOperandBundleAt(BI);
4841 if (Bundle.getTagID() == LLVMContext::OB_ptrauth)
4842 PtrAuthBundleOrNone = Bundle;
4843 else
4844 NewBundles.emplace_back(Bundle);
4845 }
4846
4847 if (!PtrAuthBundleOrNone)
4848 return nullptr;
4849
4850 Value *NewCallee = nullptr;
4851 switch (IIID) {
4852 // call(ptrauth.resign(p)), ["ptrauth"()] -> call p, ["ptrauth"()]
4853 // assuming the call bundle and the sign operands match.
4854 case Intrinsic::ptrauth_resign: {
4855 // Resign result key should match bundle.
4856 if (II->getOperand(3) != PtrAuthBundleOrNone->Inputs[0])
4857 return nullptr;
4858 // Resign result discriminator should match bundle.
4859 if (II->getOperand(4) != PtrAuthBundleOrNone->Inputs[1])
4860 return nullptr;
4861
4862 // Resign input (auth) key should also match: we can't change the key on
4863 // the new call we're generating, because we don't know what keys are valid.
4864 if (II->getOperand(1) != PtrAuthBundleOrNone->Inputs[0])
4865 return nullptr;
4866
4867 Value *NewBundleOps[] = {II->getOperand(1), II->getOperand(2)};
4868 NewBundles.emplace_back("ptrauth", NewBundleOps);
4869 NewCallee = II->getOperand(0);
4870 break;
4871 }
4872
4873 // call(ptrauth.sign(p)), ["ptrauth"()] -> call p
4874 // assuming the call bundle and the sign operands match.
4875 // Non-ptrauth indirect calls are undesirable, but so is ptrauth.sign.
4876 case Intrinsic::ptrauth_sign: {
4877 // Sign key should match bundle.
4878 if (II->getOperand(1) != PtrAuthBundleOrNone->Inputs[0])
4879 return nullptr;
4880 // Sign discriminator should match bundle.
4881 if (II->getOperand(2) != PtrAuthBundleOrNone->Inputs[1])
4882 return nullptr;
4883 NewCallee = II->getOperand(0);
4884 break;
4885 }
4886 default:
4887 llvm_unreachable("unexpected intrinsic ID");
4888 }
4889
4890 if (!NewCallee)
4891 return nullptr;
4892
4893 NewCallee = Builder.CreateBitOrPointerCast(NewCallee, Callee->getType());
4894 CallBase *NewCall = CallBase::Create(&Call, NewBundles);
4895 NewCall->setCalledOperand(NewCallee);
4896 return NewCall;
4897}
4898
4899Instruction *InstCombinerImpl::foldPtrAuthConstantCallee(CallBase &Call) {
4901 if (!CPA)
4902 return nullptr;
4903
4904 auto *CalleeF = dyn_cast<Function>(CPA->getPointer());
4905 // If the ptrauth constant isn't based on a function pointer, bail out.
4906 if (!CalleeF)
4907 return nullptr;
4908
4909 // Inspect the call ptrauth bundle to check it matches the ptrauth constant.
4911 if (!PAB)
4912 return nullptr;
4913
4914 auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
4915 Value *Discriminator = PAB->Inputs[1];
4916
4917 // If the bundle doesn't match, this is probably going to fail to auth.
4918 if (!CPA->isKnownCompatibleWith(Key, Discriminator, DL))
4919 return nullptr;
4920
4921 // If the bundle matches the constant, proceed in making this a direct call.
4923 NewCall->setCalledOperand(CalleeF);
4924 return NewCall;
4925}
4926
4927bool InstCombinerImpl::annotateAnyAllocSite(CallBase &Call,
4928 const TargetLibraryInfo *TLI) {
4929 // Note: We only handle cases which can't be driven from generic attributes
4930 // here. So, for example, nonnull and noalias (which are common properties
4931 // of some allocation functions) are expected to be handled via annotation
4932 // of the respective allocator declaration with generic attributes.
4933 bool Changed = false;
4934
4935 if (!Call.getType()->isPointerTy())
4936 return Changed;
4937
4938 std::optional<APInt> Size = getAllocSize(&Call, TLI);
4939 if (Size && *Size != 0) {
4940 // TODO: We really should just emit deref_or_null here and then
4941 // let the generic inference code combine that with nonnull.
4942 if (Call.hasRetAttr(Attribute::NonNull)) {
4943 Changed = !Call.hasRetAttr(Attribute::Dereferenceable);
4945 Call.getContext(), Size->getLimitedValue()));
4946 } else {
4947 Changed = !Call.hasRetAttr(Attribute::DereferenceableOrNull);
4949 Call.getContext(), Size->getLimitedValue()));
4950 }
4951 }
4952
4953 // Add alignment attribute if alignment is a power of two constant.
4954 Value *Alignment = getAllocAlignment(&Call, TLI);
4955 if (!Alignment)
4956 return Changed;
4957
4958 ConstantInt *AlignOpC = dyn_cast<ConstantInt>(Alignment);
4959 if (AlignOpC && AlignOpC->getValue().ult(llvm::Value::MaximumAlignment)) {
4960 uint64_t AlignmentVal = AlignOpC->getZExtValue();
4961 if (llvm::isPowerOf2_64(AlignmentVal)) {
4962 Align ExistingAlign = Call.getRetAlign().valueOrOne();
4963 Align NewAlign = Align(AlignmentVal);
4964 if (NewAlign > ExistingAlign) {
4967 Changed = true;
4968 }
4969 }
4970 }
4971 return Changed;
4972}
4973
4974/// Improvements for call, callbr and invoke instructions.
4975Instruction *InstCombinerImpl::visitCallBase(CallBase &Call) {
4976 bool Changed = annotateAnyAllocSite(Call, &TLI);
4977
4978 // Mark any parameters that are known to be non-null with the nonnull
4979 // attribute. This is helpful for inlining calls to functions with null
4980 // checks on their arguments.
4981 SmallVector<unsigned, 4> ArgNos;
4982 unsigned ArgNo = 0;
4983
4984 for (Value *V : Call.args()) {
4985 if (V->getType()->isPointerTy()) {
4986 // Simplify the nonnull operand if the parameter is known to be nonnull.
4987 // Otherwise, try to infer nonnull for it.
4988 bool HasDereferenceable = Call.getParamDereferenceableBytes(ArgNo) > 0;
4989 if (Call.paramHasAttr(ArgNo, Attribute::NonNull) ||
4990 (HasDereferenceable &&
4992 V->getType()->getPointerAddressSpace()))) {
4993 if (Value *Res = simplifyNonNullOperand(V, HasDereferenceable)) {
4994 replaceOperand(Call, ArgNo, Res);
4995 Changed = true;
4996 }
4997 } else if (isKnownNonZero(V,
4998 getSimplifyQuery().getWithInstruction(&Call))) {
4999 ArgNos.push_back(ArgNo);
5000 }
5001 }
5002 ArgNo++;
5003 }
5004
5005 assert(ArgNo == Call.arg_size() && "Call arguments not processed correctly.");
5006
5007 if (!ArgNos.empty()) {
5008 AttributeList AS = Call.getAttributes();
5009 LLVMContext &Ctx = Call.getContext();
5010 AS = AS.addParamAttribute(Ctx, ArgNos,
5011 Attribute::get(Ctx, Attribute::NonNull));
5012 Call.setAttributes(AS);
5013 Changed = true;
5014 }
5015
5016 // If the callee is a pointer to a function, attempt to move any casts to the
5017 // arguments of the call/callbr/invoke.
5019 Function *CalleeF = dyn_cast<Function>(Callee);
5020 if ((!CalleeF || CalleeF->getFunctionType() != Call.getFunctionType()) &&
5021 transformConstExprCastCall(Call))
5022 return nullptr;
5023
5024 if (CalleeF) {
5025 // Remove the convergent attr on calls when the callee is not convergent.
5026 if (Call.isConvergent() && !CalleeF->isConvergent() &&
5027 !CalleeF->isIntrinsic()) {
5028 LLVM_DEBUG(dbgs() << "Removing convergent attr from instr " << Call
5029 << "\n");
5031 return &Call;
5032 }
5033
5034 // If the call and callee calling conventions don't match, and neither one
5035 // of the calling conventions is compatible with C calling convention
5036 // this call must be unreachable, as the call is undefined.
5037 if ((CalleeF->getCallingConv() != Call.getCallingConv() &&
5038 !(CalleeF->getCallingConv() == llvm::CallingConv::C &&
5042 // Only do this for calls to a function with a body. A prototype may
5043 // not actually end up matching the implementation's calling conv for a
5044 // variety of reasons (e.g. it may be written in assembly).
5045 !CalleeF->isDeclaration()) {
5046 Instruction *OldCall = &Call;
5048 // If OldCall does not return void then replaceInstUsesWith poison.
5049 // This allows ValueHandlers and custom metadata to adjust itself.
5050 if (!OldCall->getType()->isVoidTy())
5051 replaceInstUsesWith(*OldCall, PoisonValue::get(OldCall->getType()));
5052 if (isa<CallInst>(OldCall))
5053 return eraseInstFromFunction(*OldCall);
5054
5055 // We cannot remove an invoke or a callbr, because it would change thexi
5056 // CFG, just change the callee to a null pointer.
5057 cast<CallBase>(OldCall)->setCalledFunction(
5058 CalleeF->getFunctionType(),
5059 Constant::getNullValue(CalleeF->getType()));
5060 return nullptr;
5061 }
5062 }
5063
5064 // Calling a null function pointer is undefined if a null address isn't
5065 // dereferenceable.
5066 if ((isa<ConstantPointerNull>(Callee) &&
5068 isa<UndefValue>(Callee)) {
5069 // If Call does not return void then replaceInstUsesWith poison.
5070 // This allows ValueHandlers and custom metadata to adjust itself.
5071 if (!Call.getType()->isVoidTy())
5073
5074 if (Call.isTerminator()) {
5075 // Can't remove an invoke or callbr because we cannot change the CFG.
5076 return nullptr;
5077 }
5078
5079 // This instruction is not reachable, just remove it.
5082 }
5083
5084 if (IntrinsicInst *II = findInitTrampoline(Callee))
5085 return transformCallThroughTrampoline(Call, *II);
5086
5087 // Combine calls involving pointer authentication intrinsics.
5088 if (Instruction *NewCall = foldPtrAuthIntrinsicCallee(Call))
5089 return NewCall;
5090
5091 // Combine calls to ptrauth constants.
5092 if (Instruction *NewCall = foldPtrAuthConstantCallee(Call))
5093 return NewCall;
5094
5095 if (isa<InlineAsm>(Callee) && !Call.doesNotThrow()) {
5096 InlineAsm *IA = cast<InlineAsm>(Callee);
5097 if (!IA->canThrow()) {
5098 // Normal inline asm calls cannot throw - mark them
5099 // 'nounwind'.
5101 Changed = true;
5102 }
5103 }
5104
5105 // Try to optimize the call if possible, we require DataLayout for most of
5106 // this. None of these calls are seen as possibly dead so go ahead and
5107 // delete the instruction now.
5108 if (CallInst *CI = dyn_cast<CallInst>(&Call)) {
5109 Instruction *I = tryOptimizeCall(CI);
5110 // If we changed something return the result, etc. Otherwise let
5111 // the fallthrough check.
5112 if (I) return eraseInstFromFunction(*I);
5113 }
5114
5115 if (!Call.use_empty() && !Call.isMustTailCall())
5116 if (Value *ReturnedArg = Call.getReturnedArgOperand()) {
5117 Type *CallTy = Call.getType();
5118 Type *RetArgTy = ReturnedArg->getType();
5119 if (RetArgTy->canLosslesslyBitCastTo(CallTy))
5120 return replaceInstUsesWith(
5121 Call, Builder.CreateBitOrPointerCast(ReturnedArg, CallTy));
5122 }
5123
5124 // Drop unnecessary callee_type metadata from calls that were converted
5125 // into direct calls.
5126 if (Call.getMetadata(LLVMContext::MD_callee_type) && !Call.isIndirectCall()) {
5127 Call.setMetadata(LLVMContext::MD_callee_type, nullptr);
5128 Changed = true;
5129 }
5130
5131 // Drop unnecessary kcfi operand bundles from calls that were converted
5132 // into direct calls.
5134 if (Bundle && !Call.isIndirectCall()) {
5135 DEBUG_WITH_TYPE(DEBUG_TYPE "-kcfi", {
5136 if (CalleeF) {
5137 ConstantInt *FunctionType = nullptr;
5138 ConstantInt *ExpectedType = cast<ConstantInt>(Bundle->Inputs[0]);
5139
5140 if (MDNode *MD = CalleeF->getMetadata(LLVMContext::MD_kcfi_type))
5141 FunctionType = mdconst::extract<ConstantInt>(MD->getOperand(0));
5142
5143 if (FunctionType &&
5144 FunctionType->getZExtValue() != ExpectedType->getZExtValue())
5145 dbgs() << Call.getModule()->getName()
5146 << ": warning: kcfi: " << Call.getCaller()->getName()
5147 << ": call to " << CalleeF->getName()
5148 << " using a mismatching function pointer type\n";
5149 }
5150 });
5151
5153 }
5154
5155 if (isRemovableAlloc(&Call, &TLI))
5156 return visitAllocSite(Call);
5157
5158 // Handle intrinsics which can be used in both call and invoke context.
5159 switch (Call.getIntrinsicID()) {
5160 case Intrinsic::experimental_gc_statepoint: {
5161 GCStatepointInst &GCSP = *cast<GCStatepointInst>(&Call);
5162 SmallPtrSet<Value *, 32> LiveGcValues;
5163 for (const GCRelocateInst *Reloc : GCSP.getGCRelocates()) {
5164 GCRelocateInst &GCR = *const_cast<GCRelocateInst *>(Reloc);
5165
5166 // Remove the relocation if unused.
5167 if (GCR.use_empty()) {
5169 continue;
5170 }
5171
5172 Value *DerivedPtr = GCR.getDerivedPtr();
5173 Value *BasePtr = GCR.getBasePtr();
5174
5175 // Undef is undef, even after relocation.
5176 if (isa<UndefValue>(DerivedPtr) || isa<UndefValue>(BasePtr)) {
5179 continue;
5180 }
5181
5182 if (auto *PT = dyn_cast<PointerType>(GCR.getType())) {
5183 // The relocation of null will be null for most any collector.
5184 // TODO: provide a hook for this in GCStrategy. There might be some
5185 // weird collector this property does not hold for.
5186 if (isa<ConstantPointerNull>(DerivedPtr)) {
5187 // Use null-pointer of gc_relocate's type to replace it.
5190 continue;
5191 }
5192
5193 // isKnownNonNull -> nonnull attribute
5194 if (!GCR.hasRetAttr(Attribute::NonNull) &&
5195 isKnownNonZero(DerivedPtr,
5196 getSimplifyQuery().getWithInstruction(&Call))) {
5197 GCR.addRetAttr(Attribute::NonNull);
5198 // We discovered new fact, re-check users.
5199 Worklist.pushUsersToWorkList(GCR);
5200 }
5201 }
5202
5203 // If we have two copies of the same pointer in the statepoint argument
5204 // list, canonicalize to one. This may let us common gc.relocates.
5205 if (GCR.getBasePtr() == GCR.getDerivedPtr() &&
5206 GCR.getBasePtrIndex() != GCR.getDerivedPtrIndex()) {
5207 auto *OpIntTy = GCR.getOperand(2)->getType();
5208 GCR.setOperand(2, ConstantInt::get(OpIntTy, GCR.getBasePtrIndex()));
5209 }
5210
5211 // TODO: bitcast(relocate(p)) -> relocate(bitcast(p))
5212 // Canonicalize on the type from the uses to the defs
5213
5214 // TODO: relocate((gep p, C, C2, ...)) -> gep(relocate(p), C, C2, ...)
5215 LiveGcValues.insert(BasePtr);
5216 LiveGcValues.insert(DerivedPtr);
5217 }
5218 std::optional<OperandBundleUse> Bundle =
5220 unsigned NumOfGCLives = LiveGcValues.size();
5221 if (!Bundle || NumOfGCLives == Bundle->Inputs.size())
5222 break;
5223 // We can reduce the size of gc live bundle.
5224 DenseMap<Value *, unsigned> Val2Idx;
5225 std::vector<Value *> NewLiveGc;
5226 for (Value *V : Bundle->Inputs) {
5227 auto [It, Inserted] = Val2Idx.try_emplace(V);
5228 if (!Inserted)
5229 continue;
5230 if (LiveGcValues.count(V)) {
5231 It->second = NewLiveGc.size();
5232 NewLiveGc.push_back(V);
5233 } else
5234 It->second = NumOfGCLives;
5235 }
5236 // Update all gc.relocates
5237 for (const GCRelocateInst *Reloc : GCSP.getGCRelocates()) {
5238 GCRelocateInst &GCR = *const_cast<GCRelocateInst *>(Reloc);
5239 Value *BasePtr = GCR.getBasePtr();
5240 assert(Val2Idx.count(BasePtr) && Val2Idx[BasePtr] != NumOfGCLives &&
5241 "Missed live gc for base pointer");
5242 auto *OpIntTy1 = GCR.getOperand(1)->getType();
5243 GCR.setOperand(1, ConstantInt::get(OpIntTy1, Val2Idx[BasePtr]));
5244 Value *DerivedPtr = GCR.getDerivedPtr();
5245 assert(Val2Idx.count(DerivedPtr) && Val2Idx[DerivedPtr] != NumOfGCLives &&
5246 "Missed live gc for derived pointer");
5247 auto *OpIntTy2 = GCR.getOperand(2)->getType();
5248 GCR.setOperand(2, ConstantInt::get(OpIntTy2, Val2Idx[DerivedPtr]));
5249 }
5250 // Create new statepoint instruction.
5251 OperandBundleDef NewBundle("gc-live", std::move(NewLiveGc));
5252 return CallBase::Create(&Call, NewBundle);
5253 }
5254 default: { break; }
5255 }
5256
5257 return Changed ? &Call : nullptr;
5258}
5259
5260/// If the callee is a constexpr cast of a function, attempt to move the cast to
5261/// the arguments of the call/invoke.
5262/// CallBrInst is not supported.
5263bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {
5264 auto *Callee =
5266 if (!Callee)
5267 return false;
5268
5270 "CallBr's don't have a single point after a def to insert at");
5271
5272 // Don't perform the transform for declarations, which may not be fully
5273 // accurate. For example, void @foo() is commonly used as a placeholder for
5274 // unknown prototypes.
5275 if (Callee->isDeclaration())
5276 return false;
5277
5278 // If this is a call to a thunk function, don't remove the cast. Thunks are
5279 // used to transparently forward all incoming parameters and outgoing return
5280 // values, so it's important to leave the cast in place.
5281 if (Callee->hasFnAttribute("thunk"))
5282 return false;
5283
5284 // If this is a call to a naked function, the assembly might be
5285 // using an argument, or otherwise rely on the frame layout,
5286 // the function prototype will mismatch.
5287 if (Callee->hasFnAttribute(Attribute::Naked))
5288 return false;
5289
5290 // If this is a musttail call, the callee's prototype must match the caller's
5291 // prototype with the exception of pointee types. The code below doesn't
5292 // implement that, so we can't do this transform.
5293 // TODO: Do the transform if it only requires adding pointer casts.
5294 if (Call.isMustTailCall())
5295 return false;
5296
5298 const AttributeList &CallerPAL = Call.getAttributes();
5299
5300 // Okay, this is a cast from a function to a different type. Unless doing so
5301 // would cause a type conversion of one of our arguments, change this call to
5302 // be a direct call with arguments casted to the appropriate types.
5303 FunctionType *FT = Callee->getFunctionType();
5304 Type *OldRetTy = Caller->getType();
5305 Type *NewRetTy = FT->getReturnType();
5306
5307 // Check to see if we are changing the return type...
5308 if (OldRetTy != NewRetTy) {
5309
5310 if (NewRetTy->isStructTy())
5311 return false; // TODO: Handle multiple return values.
5312
5313 if (!CastInst::isBitOrNoopPointerCastable(NewRetTy, OldRetTy, DL)) {
5314 if (!Caller->use_empty())
5315 return false; // Cannot transform this return value.
5316 }
5317
5318 if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
5319 AttrBuilder RAttrs(FT->getContext(), CallerPAL.getRetAttrs());
5320 if (RAttrs.overlaps(AttributeFuncs::typeIncompatible(
5321 NewRetTy, CallerPAL.getRetAttrs())))
5322 return false; // Attribute not compatible with transformed value.
5323 }
5324
5325 // If the callbase is an invoke instruction, and the return value is
5326 // used by a PHI node in a successor, we cannot change the return type of
5327 // the call because there is no place to put the cast instruction (without
5328 // breaking the critical edge). Bail out in this case.
5329 if (!Caller->use_empty()) {
5330 BasicBlock *PhisNotSupportedBlock = nullptr;
5331 if (auto *II = dyn_cast<InvokeInst>(Caller))
5332 PhisNotSupportedBlock = II->getNormalDest();
5333 if (PhisNotSupportedBlock)
5334 for (User *U : Caller->users())
5335 if (PHINode *PN = dyn_cast<PHINode>(U))
5336 if (PN->getParent() == PhisNotSupportedBlock)
5337 return false;
5338 }
5339 }
5340
5341 unsigned NumActualArgs = Call.arg_size();
5342 unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
5343
5344 // Prevent us turning:
5345 // declare void @takes_i32_inalloca(i32* inalloca)
5346 // call void bitcast (void (i32*)* @takes_i32_inalloca to void (i32)*)(i32 0)
5347 //
5348 // into:
5349 // call void @takes_i32_inalloca(i32* null)
5350 //
5351 // Similarly, avoid folding away bitcasts of byval calls.
5352 if (Callee->getAttributes().hasAttrSomewhere(Attribute::InAlloca) ||
5353 Callee->getAttributes().hasAttrSomewhere(Attribute::Preallocated))
5354 return false;
5355
5356 auto AI = Call.arg_begin();
5357 for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
5358 Type *ParamTy = FT->getParamType(i);
5359 Type *ActTy = (*AI)->getType();
5360
5361 if (!CastInst::isBitOrNoopPointerCastable(ActTy, ParamTy, DL))
5362 return false; // Cannot transform this parameter value.
5363
5364 // Check if there are any incompatible attributes we cannot drop safely.
5365 if (AttrBuilder(FT->getContext(), CallerPAL.getParamAttrs(i))
5366 .overlaps(AttributeFuncs::typeIncompatible(
5367 ParamTy, CallerPAL.getParamAttrs(i),
5368 AttributeFuncs::ASK_UNSAFE_TO_DROP)))
5369 return false; // Attribute not compatible with transformed value.
5370
5371 if (Call.isInAllocaArgument(i) ||
5372 CallerPAL.hasParamAttr(i, Attribute::Preallocated))
5373 return false; // Cannot transform to and from inalloca/preallocated.
5374
5375 if (CallerPAL.hasParamAttr(i, Attribute::SwiftError))
5376 return false;
5377
5378 if (CallerPAL.hasParamAttr(i, Attribute::ByVal) !=
5379 Callee->getAttributes().hasParamAttr(i, Attribute::ByVal))
5380 return false; // Cannot transform to or from byval.
5381 }
5382
5383 if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
5384 !CallerPAL.isEmpty()) {
5385 // In this case we have more arguments than the new function type, but we
5386 // won't be dropping them. Check that these extra arguments have attributes
5387 // that are compatible with being a vararg call argument.
5388 unsigned SRetIdx;
5389 if (CallerPAL.hasAttrSomewhere(Attribute::StructRet, &SRetIdx) &&
5390 SRetIdx - AttributeList::FirstArgIndex >= FT->getNumParams())
5391 return false;
5392 }
5393
5394 // Okay, we decided that this is a safe thing to do: go ahead and start
5395 // inserting cast instructions as necessary.
5396 SmallVector<Value *, 8> Args;
5398 Args.reserve(NumActualArgs);
5399 ArgAttrs.reserve(NumActualArgs);
5400
5401 // Get any return attributes.
5402 AttrBuilder RAttrs(FT->getContext(), CallerPAL.getRetAttrs());
5403
5404 // If the return value is not being used, the type may not be compatible
5405 // with the existing attributes. Wipe out any problematic attributes.
5406 RAttrs.remove(
5407 AttributeFuncs::typeIncompatible(NewRetTy, CallerPAL.getRetAttrs()));
5408
5409 LLVMContext &Ctx = Call.getContext();
5410 AI = Call.arg_begin();
5411 for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
5412 Type *ParamTy = FT->getParamType(i);
5413
5414 Value *NewArg = *AI;
5415 if ((*AI)->getType() != ParamTy)
5416 NewArg = Builder.CreateBitOrPointerCast(*AI, ParamTy);
5417 Args.push_back(NewArg);
5418
5419 // Add any parameter attributes except the ones incompatible with the new
5420 // type. Note that we made sure all incompatible ones are safe to drop.
5421 AttributeMask IncompatibleAttrs = AttributeFuncs::typeIncompatible(
5422 ParamTy, CallerPAL.getParamAttrs(i), AttributeFuncs::ASK_SAFE_TO_DROP);
5423 ArgAttrs.push_back(
5424 CallerPAL.getParamAttrs(i).removeAttributes(Ctx, IncompatibleAttrs));
5425 }
5426
5427 // If the function takes more arguments than the call was taking, add them
5428 // now.
5429 for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) {
5430 Args.push_back(Constant::getNullValue(FT->getParamType(i)));
5431 ArgAttrs.push_back(AttributeSet());
5432 }
5433
5434 // If we are removing arguments to the function, emit an obnoxious warning.
5435 if (FT->getNumParams() < NumActualArgs) {
5436 // TODO: if (!FT->isVarArg()) this call may be unreachable. PR14722
5437 if (FT->isVarArg()) {
5438 // Add all of the arguments in their promoted form to the arg list.
5439 for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
5440 Type *PTy = getPromotedType((*AI)->getType());
5441 Value *NewArg = *AI;
5442 if (PTy != (*AI)->getType()) {
5443 // Must promote to pass through va_arg area!
5444 Instruction::CastOps opcode =
5445 CastInst::getCastOpcode(*AI, false, PTy, false);
5446 NewArg = Builder.CreateCast(opcode, *AI, PTy);
5447 }
5448 Args.push_back(NewArg);
5449
5450 // Add any parameter attributes.
5451 ArgAttrs.push_back(CallerPAL.getParamAttrs(i));
5452 }
5453 }
5454 }
5455
5456 AttributeSet FnAttrs = CallerPAL.getFnAttrs();
5457
5458 if (NewRetTy->isVoidTy())
5459 Caller->setName(""); // Void type should not have a name.
5460
5461 assert((ArgAttrs.size() == FT->getNumParams() || FT->isVarArg()) &&
5462 "missing argument attributes");
5463 AttributeList NewCallerPAL = AttributeList::get(
5464 Ctx, FnAttrs, AttributeSet::get(Ctx, RAttrs), ArgAttrs);
5465
5467 Call.getOperandBundlesAsDefs(OpBundles);
5468
5469 CallBase *NewCall;
5470 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
5471 NewCall = Builder.CreateInvoke(Callee, II->getNormalDest(),
5472 II->getUnwindDest(), Args, OpBundles);
5473 } else {
5474 NewCall = Builder.CreateCall(Callee, Args, OpBundles);
5475 cast<CallInst>(NewCall)->setTailCallKind(
5476 cast<CallInst>(Caller)->getTailCallKind());
5477 }
5478 NewCall->takeName(Caller);
5480 NewCall->setAttributes(NewCallerPAL);
5481
5482 // Preserve prof metadata if any.
5483 NewCall->copyMetadata(*Caller, {LLVMContext::MD_prof});
5484
5485 // Insert a cast of the return type as necessary.
5486 Instruction *NC = NewCall;
5487 Value *NV = NC;
5488 if (OldRetTy != NV->getType() && !Caller->use_empty()) {
5489 assert(!NV->getType()->isVoidTy());
5491 NC->setDebugLoc(Caller->getDebugLoc());
5492
5493 auto OptInsertPt = NewCall->getInsertionPointAfterDef();
5494 assert(OptInsertPt && "No place to insert cast");
5495 InsertNewInstBefore(NC, *OptInsertPt);
5496 Worklist.pushUsersToWorkList(*Caller);
5497 }
5498
5499 if (!Caller->use_empty())
5500 replaceInstUsesWith(*Caller, NV);
5501 else if (Caller->hasValueHandle()) {
5502 if (OldRetTy == NV->getType())
5504 else
5505 // We cannot call ValueIsRAUWd with a different type, and the
5506 // actual tracked value will disappear.
5508 }
5509
5510 eraseInstFromFunction(*Caller);
5511 return true;
5512}
5513
5514/// Turn a call to a function created by init_trampoline / adjust_trampoline
5515/// intrinsic pair into a direct call to the underlying function.
5517InstCombinerImpl::transformCallThroughTrampoline(CallBase &Call,
5518 IntrinsicInst &Tramp) {
5519 FunctionType *FTy = Call.getFunctionType();
5520 AttributeList Attrs = Call.getAttributes();
5521
5522 // If the call already has the 'nest' attribute somewhere then give up -
5523 // otherwise 'nest' would occur twice after splicing in the chain.
5524 if (Attrs.hasAttrSomewhere(Attribute::Nest))
5525 return nullptr;
5526
5528 FunctionType *NestFTy = NestF->getFunctionType();
5529
5530 AttributeList NestAttrs = NestF->getAttributes();
5531 if (!NestAttrs.isEmpty()) {
5532 unsigned NestArgNo = 0;
5533 Type *NestTy = nullptr;
5534 AttributeSet NestAttr;
5535
5536 // Look for a parameter marked with the 'nest' attribute.
5537 for (FunctionType::param_iterator I = NestFTy->param_begin(),
5538 E = NestFTy->param_end();
5539 I != E; ++NestArgNo, ++I) {
5540 AttributeSet AS = NestAttrs.getParamAttrs(NestArgNo);
5541 if (AS.hasAttribute(Attribute::Nest)) {
5542 // Record the parameter type and any other attributes.
5543 NestTy = *I;
5544 NestAttr = AS;
5545 break;
5546 }
5547 }
5548
5549 if (NestTy) {
5550 std::vector<Value*> NewArgs;
5551 std::vector<AttributeSet> NewArgAttrs;
5552 NewArgs.reserve(Call.arg_size() + 1);
5553 NewArgAttrs.reserve(Call.arg_size());
5554
5555 // Insert the nest argument into the call argument list, which may
5556 // mean appending it. Likewise for attributes.
5557
5558 {
5559 unsigned ArgNo = 0;
5560 auto I = Call.arg_begin(), E = Call.arg_end();
5561 do {
5562 if (ArgNo == NestArgNo) {
5563 // Add the chain argument and attributes.
5564 Value *NestVal = Tramp.getArgOperand(2);
5565 if (NestVal->getType() != NestTy)
5566 NestVal = Builder.CreateBitCast(NestVal, NestTy, "nest");
5567 NewArgs.push_back(NestVal);
5568 NewArgAttrs.push_back(NestAttr);
5569 }
5570
5571 if (I == E)
5572 break;
5573
5574 // Add the original argument and attributes.
5575 NewArgs.push_back(*I);
5576 NewArgAttrs.push_back(Attrs.getParamAttrs(ArgNo));
5577
5578 ++ArgNo;
5579 ++I;
5580 } while (true);
5581 }
5582
5583 // The trampoline may have been bitcast to a bogus type (FTy).
5584 // Handle this by synthesizing a new function type, equal to FTy
5585 // with the chain parameter inserted.
5586
5587 std::vector<Type*> NewTypes;
5588 NewTypes.reserve(FTy->getNumParams()+1);
5589
5590 // Insert the chain's type into the list of parameter types, which may
5591 // mean appending it.
5592 {
5593 unsigned ArgNo = 0;
5594 FunctionType::param_iterator I = FTy->param_begin(),
5595 E = FTy->param_end();
5596
5597 do {
5598 if (ArgNo == NestArgNo)
5599 // Add the chain's type.
5600 NewTypes.push_back(NestTy);
5601
5602 if (I == E)
5603 break;
5604
5605 // Add the original type.
5606 NewTypes.push_back(*I);
5607
5608 ++ArgNo;
5609 ++I;
5610 } while (true);
5611 }
5612
5613 // Replace the trampoline call with a direct call. Let the generic
5614 // code sort out any function type mismatches.
5615 FunctionType *NewFTy =
5616 FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg());
5617 AttributeList NewPAL =
5618 AttributeList::get(FTy->getContext(), Attrs.getFnAttrs(),
5619 Attrs.getRetAttrs(), NewArgAttrs);
5620
5622 Call.getOperandBundlesAsDefs(OpBundles);
5623
5624 Instruction *NewCaller;
5625 if (InvokeInst *II = dyn_cast<InvokeInst>(&Call)) {
5626 NewCaller = InvokeInst::Create(NewFTy, NestF, II->getNormalDest(),
5627 II->getUnwindDest(), NewArgs, OpBundles);
5628 cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
5629 cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
5630 } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(&Call)) {
5631 NewCaller =
5632 CallBrInst::Create(NewFTy, NestF, CBI->getDefaultDest(),
5633 CBI->getIndirectDests(), NewArgs, OpBundles);
5634 cast<CallBrInst>(NewCaller)->setCallingConv(CBI->getCallingConv());
5635 cast<CallBrInst>(NewCaller)->setAttributes(NewPAL);
5636 } else {
5637 NewCaller = CallInst::Create(NewFTy, NestF, NewArgs, OpBundles);
5638 cast<CallInst>(NewCaller)->setTailCallKind(
5639 cast<CallInst>(Call).getTailCallKind());
5640 cast<CallInst>(NewCaller)->setCallingConv(
5641 cast<CallInst>(Call).getCallingConv());
5642 cast<CallInst>(NewCaller)->setAttributes(NewPAL);
5643 }
5644 NewCaller->setDebugLoc(Call.getDebugLoc());
5645
5646 return NewCaller;
5647 }
5648 }
5649
5650 // Replace the trampoline call with a direct call. Since there is no 'nest'
5651 // parameter, there is no need to adjust the argument list. Let the generic
5652 // code sort out any function type mismatches.
5653 Call.setCalledFunction(FTy, NestF);
5654 return &Call;
5655}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
AMDGPU Register Bank Select
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
@ Scaled
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
BitTracker BT
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static SDValue foldBitOrderCrossLogicOp(SDNode *N, SelectionDAG &DAG)
#define Check(C,...)
#define DEBUG_TYPE
Hexagon Common GEP
#define _
IRTranslator LLVM IR MI
static Type * getPromotedType(Type *Ty)
Return the specified type promoted as it would be to pass though a va_arg area.
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...
static void referenceAspect(StringRef Aspect, StringRef ImplName, Module *M, IRBuilderBase &B)
static IntrinsicInst * findInitTrampolineFromAlloca(Value *TrampMem)
static bool removeTriviallyEmptyRange(IntrinsicInst &EndI, InstCombinerImpl &IC, std::function< bool(const IntrinsicInst &)> IsStart)
static bool inputDenormalIsDAZ(const Function &F, const Type *Ty)
static Instruction * reassociateMinMaxWithConstantInOperand(IntrinsicInst *II, InstCombiner::BuilderTy &Builder)
If this min/max has a matching min/max operand with a constant, try to push the constant operand into...
static bool isIdempotentBinaryIntrinsic(Intrinsic::ID IID)
Helper to match idempotent binary intrinsics, namely, intrinsics where f(f(x, y), y) == f(x,...
static bool signBitMustBeTheSame(Value *Op0, Value *Op1, const SimplifyQuery &SQ)
Return true if two values Op0 and Op1 are known to have the same sign.
static Value * optimizeModularFormat(CallInst *CI, IRBuilderBase &B)
static Instruction * moveAddAfterMinMax(IntrinsicInst *II, InstCombiner::BuilderTy &Builder)
Try to canonicalize min/max(X + C0, C1) as min/max(X, C1 - C0) + C0.
static Instruction * simplifyInvariantGroupIntrinsic(IntrinsicInst &II, InstCombinerImpl &IC)
This function transforms launder.invariant.group and strip.invariant.group like: launder(launder(x)) ...
static bool haveSameOperands(const IntrinsicInst &I, const IntrinsicInst &E, unsigned NumOperands)
static std::optional< bool > getKnownSign(Value *Op, const SimplifyQuery &SQ)
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"))
static bool hasUndefSource(AnyMemTransferInst *MI)
Recognize a memcpy/memmove from a trivially otherwise unused alloca.
static Instruction * factorizeMinMaxTree(IntrinsicInst *II)
Reduce a sequence of min/max intrinsics with a common operand.
static Instruction * foldClampRangeOfTwo(IntrinsicInst *II, InstCombiner::BuilderTy &Builder)
If we have a clamp pattern like max (min X, 42), 41 – where the output can only be one of two possibl...
static Value * simplifyReductionOperand(Value *Arg, bool CanReorderLanes)
static IntrinsicInst * findInitTrampolineFromBB(IntrinsicInst *AdjustTramp, Value *TrampMem)
static bool isAspectNeeded(StringRef Aspect, CallInst *CI, std::optional< unsigned > FirstArgIdx, const std::optional< Bitset< 256 > > &Specifiers)
static Value * foldIntrinsicUsingDistributiveLaws(IntrinsicInst *II, InstCombiner::BuilderTy &Builder)
static std::optional< bool > getKnownSignOrZero(Value *Op, const SimplifyQuery &SQ)
static Value * foldMinimumOverTrailingOrLeadingZeroCount(Value *I0, Value *I1, const DataLayout &DL, InstCombiner::BuilderTy &Builder)
Fold an unsigned minimum of trailing or leading zero bits counts: umin(cttz(CtOp1,...
static bool rightDistributesOverLeft(Instruction::BinaryOps LOp, bool HasNUW, bool HasNSW, Intrinsic::ID ROp)
Return whether "(X ROp Y) LOp Z" is always equal to "(X LOp Z) ROp (Y LOp Z)".
static Value * foldIdempotentBinaryIntrinsicRecurrence(InstCombinerImpl &IC, IntrinsicInst *II)
Attempt to simplify value-accumulating recurrences of kind: umax.acc = phi i8 [ umax,...
static bool ldexpSaturatingAddIsSafe(Type *FpTy, Type *ExpTy)
static Instruction * foldCtpop(IntrinsicInst &II, InstCombinerImpl &IC)
static Instruction * simplifyNeonTbl(IntrinsicInst &II, InstCombiner &IC, bool IsExtension)
Convert tbl/tbx intrinsics to shufflevector if the mask is constant, and at most two source operands ...
static Instruction * foldCttzCtlz(IntrinsicInst &II, InstCombinerImpl &IC)
static IntrinsicInst * findInitTrampoline(Value *Callee)
static Bitset< 256 > parseFormatStringSpecifiers(StringRef FormatStr)
static FCmpInst::Predicate fpclassTestIsFCmp0(FPClassTest Mask, const Function &F, Type *Ty)
static bool leftDistributesOverRight(Instruction::BinaryOps LOp, bool HasNUW, bool HasNSW, Intrinsic::ID ROp)
Return whether "X LOp (Y ROp Z)" is always equal to "(X LOp Y) ROp (X LOp Z)".
static Value * reassociateMinMaxWithConstants(IntrinsicInst *II, IRBuilderBase &Builder, const SimplifyQuery &SQ)
If this min/max has a constant operand and an operand that is a matching min/max with a constant oper...
static Value * foldSinAndCosToSinCos(IntrinsicInst *II, IRBuilderBase &B, InstCombinerImpl &IC)
static CallInst * canonicalizeConstantArg0ToArg1(CallInst &Call)
static Instruction * foldNeonShift(IntrinsicInst *II, InstCombinerImpl &IC)
This file provides internal interfaces used to implement the InstCombine.
This file provides the interface for the instcombine pass implementation.
static bool inputDenormalIsIEEE(DenormalMode Mode)
Return true if it's possible to assume IEEE treatment of input denormals in F for Val.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static const Function * getCalledFunction(const Value *V)
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
This file implements the SmallBitVector class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:119
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition Debug.h:72
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Value * RHS
Value * LHS
The Input class is used to parse a yaml document into in-memory structs and vectors.
static LLVM_ABI bool semanticsHasInf(const fltSemantics &)
Definition APFloat.cpp:287
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:353
static LLVM_ABI bool hasSignBitInMSB(const fltSemantics &)
Definition APFloat.cpp:300
bool isNegative() const
Definition APFloat.h:1575
void clearSign()
Definition APFloat.h:1394
static APFloat getOne(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative One.
Definition APFloat.h:1184
bool isZero() const
Definition APFloat.h:1571
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1234
static APFloat getSmallest(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) finite number in the given semantics.
Definition APFloat.h:1244
bool isInfinity() const
Definition APFloat.h:1572
Class for arbitrary precision integers.
Definition APInt.h:78
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:231
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:226
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1206
LLVM_ABI APInt usub_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1978
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1187
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:377
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1687
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1509
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1116
LLVM_ABI APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1958
LLVM_ABI APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1965
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition APInt.cpp:647
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:216
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1079
bool isShiftedMask() const
Return true if this APInt value contains a non-empty sequence of ones with the remainder zero.
Definition APInt.h:507
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2066
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:331
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:303
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:197
std::optional< int64_t > trySExtValue() const
Get sign extended value if possible.
Definition APInt.h:1595
LLVM_ABI APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1971
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:310
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:302
This class represents any memset intrinsic.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition ArrayRef.h:194
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:407
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists in this set.
static LLVM_ABI AttributeSet get(LLVMContext &C, const AttrBuilder &B)
static LLVM_ABI Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
static LLVM_ABI Attribute getWithDereferenceableBytes(LLVMContext &Context, uint64_t Bytes)
static LLVM_ABI Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context, uint64_t Bytes)
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
static LLVM_ABI Attribute getWithAlignment(LLVMContext &Context, Align Alignment)
Return a uniquified Attribute object that has the specific alignment set.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:446
InstListType::reverse_iterator reverse_iterator
Definition BasicBlock.h:172
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
LLVM_ABI bool isSigned() const
Whether the intrinsic is signed or unsigned.
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
static BinaryOperator * CreateFAddFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
Definition InstrTypes.h:271
static LLVM_ABI BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
static BinaryOperator * CreateNSW(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name="")
Definition InstrTypes.h:314
static LLVM_ABI BinaryOperator * CreateNot(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
static BinaryOperator * CreateNUW(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name="")
Definition InstrTypes.h:329
static BinaryOperator * CreateFMulFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
Definition InstrTypes.h:279
static BinaryOperator * CreateFDivFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
Definition InstrTypes.h:283
static BinaryOperator * CreateFSubFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
Definition InstrTypes.h:275
static LLVM_ABI BinaryOperator * CreateNSWNeg(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
This is a constexpr reimplementation of a subset of std::bitset.
Definition Bitset.h:30
constexpr bool any() const
Definition Bitset.h:113
constexpr Bitset & set()
Definition Bitset.h:81
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
void setCallingConv(CallingConv::ID CC)
void setDoesNotThrow()
MaybeAlign getRetAlign() const
Extract the alignment of the return value.
LLVM_ABI void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.
OperandBundleUse getOperandBundleAt(unsigned Index) const
Return the operand bundle at a specific index.
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
bool isInAllocaArgument(unsigned ArgNo) const
Determine whether this argument is passed in an alloca.
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
bool hasRetAttr(Attribute::AttrKind Kind) const
Determine whether the return value has the given attribute.
unsigned getNumOperandBundles() const
Return the number of operand bundles associated with this User.
uint64_t getParamDereferenceableBytes(unsigned i) const
Extract the number of dereferenceable bytes for a call or parameter (0=unknown).
CallingConv::ID getCallingConv() const
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
static LLVM_ABI CallBase * removeOperandBundleAt(CallBase *CB, size_t Offset, InsertPosition InsertPtr=nullptr)
void setNotConvergent()
Value * getCalledOperand() const
void setAttributes(AttributeList A)
Set the attributes for this call.
Attribute getFnAttr(StringRef Kind) const
Get the attribute of a given kind for the function.
bool doesNotThrow() const
Determine if the call cannot unwind.
void addRetAttr(Attribute::AttrKind Kind)
Adds the attribute to the return value.
Value * getArgOperand(unsigned i) const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
bool isConvergent() const
Determine if the invoke is convergent.
FunctionType * getFunctionType() const
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
Value * getReturnedArgOperand() const
If one of the arguments has the 'returned' attribute, returns its operand value.
static LLVM_ABI CallBase * Create(CallBase *CB, ArrayRef< OperandBundleDef > Bundles, InsertPosition InsertPt=nullptr)
Create a clone of CB with a different set of operand bundles and insert it before InsertPt.
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
void setCalledOperand(Value *V)
static LLVM_ABI CallBase * removeOperandBundle(CallBase *CB, uint32_t ID, InsertPosition InsertPt=nullptr)
Create a clone of CB with operand bundle ID removed.
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
void setCalledFunction(Function *Fn)
Sets the function called, including updating the function type.
LLVM_ABI Function * getCaller()
Helper to get the caller (the parent function).
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
static CallBrInst * Create(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
This class represents a function call, abstracting a target machine's calling convention.
bool isNoTailCall() const
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
bool isMustTailCall() const
static LLVM_ABI 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.
static LLVM_ABI CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
static LLVM_ABI 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.
static LLVM_ABI CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
static LLVM_ABI CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:743
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:748
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:751
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:756
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:890
Predicate getNonStrictPredicate() const
For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
Definition InstrTypes.h:934
Predicate getUnorderedPredicate() const
Definition InstrTypes.h:874
static LLVM_ABI ConstantAggregateZero * get(Type *Ty)
static LLVM_ABI Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
static LLVM_ABI Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getNeg(Constant *C, bool HasNSW=false)
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
static LLVM_ABI ConstantFP * getZero(Type *Ty, bool Negative=false)
static LLVM_ABI ConstantFP * getInfinity(Type *Ty, bool Negative=false)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
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:269
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
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:168
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
static LLVM_ABI ConstantPtrAuth * get(Constant *Ptr, ConstantInt *Key, ConstantInt *Disc, Constant *AddrDisc, Constant *DeactivationSymbol)
Return a pointer signed with the specified parameters.
This class represents a range of values.
LLVM_ABI ConstantRange zextOrTrunc(uint32_t BitWidth) const
Make this range have the bit width given by BitWidth.
LLVM_ABI bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other?
LLVM_ABI ConstantRange multiply(const ConstantRange &Other, unsigned NoWrapKind=0) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Record of a variable value-assignment, aka a non instruction representation of the dbg....
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:299
unsigned size() const
Definition DenseMap.h:172
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:219
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:214
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static FMFSource intersect(Value *A, Value *B)
Intersect the FMF from two instructions.
Definition IRBuilder.h:107
This class represents an extension of floating point types.
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
bool allowReassoc() const
Flag queries.
Definition FMF.h:64
An instruction for ordering other memory operations.
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this fence instruction.
AtomicOrdering getOrdering() const
Returns the ordering constraint of this fence instruction.
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Type::subtype_iterator param_iterator
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
bool isConvergent() const
Determine if the call is convergent.
Definition Function.h:592
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:211
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:272
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:328
bool doesNotThrow() const
Determine if the function cannot unwind.
Definition Function.h:576
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:251
LLVM_ABI Value * getBasePtr() const
unsigned getBasePtrIndex() const
The index into the associate statepoint's argument list which contains the base pointer of the pointe...
LLVM_ABI Value * getDerivedPtr() const
unsigned getDerivedPtrIndex() const
The index into the associate statepoint's argument list which contains the pointer whose relocation t...
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:206
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this GlobalObject.
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:408
PointerType * getType() const
Global values are always pointers.
Common base class shared among various IRBuilders.
Definition IRBuilder.h:114
LLVM_ABI Value * CreateLaunderInvariantGroup(Value *Ptr)
Create a launder.invariant.group intrinsic call.
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition IRBuilder.h:457
LLVM_ABI Value * CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 2 operands which is mangled on the first type.
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1439
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition IRBuilder.h:2121
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition IRBuilder.h:2684
LLVM_ABI Value * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > OverloadTypes, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="", ArrayRef< OperandBundleDef > OpBundles={}, function_ref< void(CallInst *)> SetFn=[](CallInst *) {})
Variant to create a possibly constant-folded intrinsic.
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition IRBuilder.h:462
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2485
Value * CreateAddrSpaceCast(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2248
LLVM_ABI Value * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *Op, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
LLVM_ABI Value * CreateStripInvariantGroup(Value *Ptr)
Create a strip.invariant.group intrinsic call.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Instruction * foldOpIntoPhi(Instruction &I, PHINode *PN, bool AllowMultipleUses=false)
Given a binary operator, cast instruction, or select which has a PHI node as operand #0,...
Value * SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &PoisonElts, unsigned Depth=0, bool AllowMultipleUsers=false) override
The specified value produces a vector with any number of elements.
bool SimplifyDemandedBits(Instruction *I, unsigned Op, const APInt &DemandedMask, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0) override
This form of SimplifyDemandedBits simplifies the specified instruction operand if possible,...
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false, bool SimplifyBothArms=false)
Given an instruction with a select as one operand and a constant as the other operand,...
Instruction * SimplifyAnyMemSet(AnyMemSetInst *MI)
Instruction * foldItoFPtoI(FPToIntTy &FI)
fpto{s/u}i.sat --> X or zext(X) or sext(X) or trunc(X) This is safe if the intermediate type has enou...
Instruction * visitFree(CallInst &FI, Value *FreedOp)
Instruction * visitCallBrInst(CallBrInst &CBI)
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Value * foldReversedIntrinsicOperands(IntrinsicInst *II)
If all arguments of the intrinsic are reverses, try to pull the reverse after the intrinsic.
Value * tryGetLog2(Value *Op, bool AssumeNonZero)
Instruction * visitFenceInst(FenceInst &FI)
Instruction * foldShuffledIntrinsicOperands(IntrinsicInst *II)
If all arguments of the intrinsic are unary shuffles with the same mask, try to shuffle after the int...
Instruction * visitInvokeInst(InvokeInst &II)
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Tries to simplify operands to an integer instruction based on its demanded bits.
void CreateNonTerminatorUnreachable(Instruction *InsertAt)
Create and insert the idiom we use to indicate a block is unreachable without having to rewrite the C...
Instruction * visitVAEndInst(VAEndInst &I)
Instruction * matchBSwapOrBitReverse(Instruction &I, bool MatchBSwaps, bool MatchBitReversals)
Given an initial instruction, check to see if it is the root of a bswap/bitreverse idiom.
Constant * unshuffleConstant(ArrayRef< int > ShMask, Constant *C, VectorType *NewCTy)
Find a constant NewC that has property: shuffle(NewC, poison, ShMask) = C for lanes that select NewC.
Instruction * visitAllocSite(Instruction &FI)
Instruction * SimplifyAnyMemTransfer(AnyMemTransferInst *MI)
OverflowResult computeOverflow(Instruction::BinaryOps BinaryOp, bool IsSigned, Value *LHS, Value *RHS, Instruction *CxtI) const
Instruction * visitCallInst(CallInst &CI)
CallInst simplification.
The core instruction combiner logic.
SimplifyQuery SQ
const DataLayout & getDataLayout() const
unsigned ComputeMaxSignificantBits(const Value *Op, const Instruction *CxtI=nullptr, unsigned Depth=0) const
bool isFreeToInvert(Value *V, bool WillInvertAllUses, bool &DoesConsume)
Return true if the specified value is free to invert (apply ~ to).
DominatorTree & getDominatorTree() const
BlockFrequencyInfo * BFI
TargetLibraryInfo & TLI
Instruction * InsertNewInstBefore(Instruction *New, BasicBlock::iterator Old)
Inserts an instruction New before instruction Old.
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
void replaceUse(Use &U, Value *NewValue)
Replace use and add the previously used value to the worklist.
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
const DataLayout & DL
DomConditionCache DC
void computeKnownBits(const Value *V, KnownBits &Known, const Instruction *CxtI, unsigned Depth=0) const
IRBuilder< TargetFolder, IRBuilderInstCombineInserter > BuilderTy
An IRBuilder that automatically inserts new instructions into the worklist.
LLVM_ABI std::optional< Instruction * > targetInstCombineIntrinsic(IntrinsicInst &II)
AssumptionCache & AC
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const Instruction *CxtI=nullptr, unsigned Depth=0) const
DominatorTree & DT
ProfileSummaryInfo * PSI
OptimizationRemarkEmitter & ORE
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
const SimplifyQuery & getSimplifyQuery() const
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, const Instruction *CxtI=nullptr, unsigned Depth=0)
LLVM_ABI Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
LLVM_ABI void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
LLVM_ABI bool mayWriteToMemory() const LLVM_READONLY
Return true if this instruction may modify memory.
LLVM_ABI void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
LLVM_ABI void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
LLVM_ABI bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
LLVM_ABI void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
bool isTerminator() const
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
LLVM_ABI std::optional< InstListType::iterator > getInsertionPointAfterDef()
Get the first insertion point at which the result of this instruction is defined.
LLVM_ABI bool isIdenticalTo(const Instruction *I) const LLVM_READONLY
Return true if the specified instruction is exactly identical to the current one.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
Class to represent integer types.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:348
A wrapper class for inspecting calls to intrinsic functions.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Invoke instruction.
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
Metadata node.
Definition Metadata.h:1069
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1567
static LLVM_ABI MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:615
static LLVM_ABI MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition Metadata.cpp:111
static ICmpInst::Predicate getPredicate(Intrinsic::ID ID)
Returns the comparison predicate underlying the intrinsic.
ICmpInst::Predicate getPredicate() const
Returns the comparison predicate underlying the intrinsic.
bool isSigned() const
Whether the intrinsic is signed or unsigned.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
StringRef getName() const
Get a short "name" for the module.
Definition Module.h:311
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition Operator.h:43
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition Operator.h:78
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property.
Definition Operator.h:113
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
Definition Operator.h:107
bool isCommutative() const
Return true if the instruction is commutative.
Definition Operator.h:130
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Represents a saturating add/sub intrinsic.
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, const Instruction *MDFrom=nullptr)
This instruction constructs a fixed permutation of two input vectors.
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
SmallBitVector & set()
bool test(unsigned Idx) const
Returns true if bit Idx is set.
bool all() const
Returns true if all bits are set.
size_type size() const
Definition SmallPtrSet.h:99
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
void setVolatile(bool V)
Specify whether this is a volatile store or not.
void setAlignment(Align Align)
void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this store instruction.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
static constexpr size_t npos
Definition StringRef.h:58
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:490
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
LLVM_ABI size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Class to represent struct types.
static LLVM_ABI bool isCallingConvCCompatible(CallBase *CI)
Returns true if call site / callee has cdecl-compatible calling conventions.
Provides information about what library functions are available for the current target.
This class represents a truncation of integer types.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI unsigned getIntegerBitWidth() const
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:309
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:263
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
LLVM_ABI bool canLosslesslyBitCastTo(Type *Ty) const
Return true if this type could be converted with a lossless BitCast to type 'Ty'.
Definition Type.cpp:153
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
bool isStructTy() const
True if this is an instance of StructType.
Definition Type.h:276
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
LLVM_ABI Type * getWithNewBitWidth(unsigned NewBitWidth) const
Given an integer or vector type, change the lane bitwidth to NewBitwidth, whilst keeping the old numb...
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:232
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:106
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
static UnaryOperator * CreateWithCopiedFlags(UnaryOps Opc, Value *V, Instruction *CopyO, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Definition InstrTypes.h:148
static UnaryOperator * CreateFNegFMF(Value *Op, Instruction *FMFSource, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Definition InstrTypes.h:156
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:36
void setOperand(unsigned i, Value *Val)
Definition User.h:212
Value * getOperand(unsigned i) const
Definition User.h:207
This represents the llvm.va_end intrinsic.
static LLVM_ABI void ValueIsDeleted(Value *V)
Definition Value.cpp:1272
static LLVM_ABI void ValueIsRAUWd(Value *Old, Value *New)
Definition Value.cpp:1325
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
static constexpr uint64_t MaximumAlignment
Definition Value.h:799
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:713
bool use_empty() const
Definition Value.h:346
static constexpr unsigned MaxAlignmentExponent
The maximum alignment for instructions.
Definition Value.h:798
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:400
Base class of all SIMD vector types.
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:216
constexpr bool isFixed() const
Returns true if the quantity is not scaled by vscale.
Definition TypeSize.h:171
static constexpr bool isKnownGT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:223
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
constexpr 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.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
auto m_PosZeroFP()
Matches a floating-point positive zero.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
AllOnesConstantMatch m_AllOnes()
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
match_combine_and< Ty... > m_CombineAnd(const Ty &...Ps)
Combine pattern matchers matching all of Ps patterns.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
auto m_BSwap(const Opnd0 &Op0)
PtrAdd_match< PointerOpTy, OffsetOpTy > m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
Matches GEP with i8 source element type.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
auto m_BitReverse(const Opnd0 &Op0)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
auto m_Poison()
Match an arbitrary poison constant.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
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.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
ap_match< APInt > m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
bool match(Val *V, const Pattern &P)
match_bind< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
auto m_UMin(const Opnd0 &Op0, const Opnd1 &Op1)
match_deferred< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWNeg(const ValTy &V)
Matches a 'Neg' as 'sub nsw 0, V'.
auto m_SMax(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
auto m_BinOp()
Match an arbitrary binary operation and ignore it.
auto m_UMax(const Opnd0 &Op0, const Opnd1 &Op1)
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
auto m_CopySign(const Opnd0 &Op0, const Opnd1 &Op1)
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
auto m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
auto m_Constant()
Match an arbitrary Constant and ignore it.
match_combine_or< match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > >, OpTy > m_ZExtOrSExtOrSelf(const OpTy &Op)
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
cst_pred_ty< is_strictlypositive > m_StrictlyPositive()
Match an integer or vector of strictly positive values.
ThreeOps_match< decltype(m_Value()), LHS, RHS, Instruction::Select, true > m_c_Select(const LHS &L, const RHS &R)
Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
CastInst_match< OpTy, FPExtInst > m_FPExt(const OpTy &Op)
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
auto m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
cst_pred_ty< custom_checkfn< APInt > > m_CheckedInt(function_ref< bool(const APInt &)> CheckFn)
Match an integer or vector where CheckFn(ele) for each element is true.
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
auto m_c_MaxOrMin(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
auto m_SMin(const Opnd0 &Op0, const Opnd1 &Op1)
auto m_FAbs(const Opnd0 &Op0)
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Exact_match< T > m_Exact(const T &SubPattern)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
auto m_UnOp()
Match an arbitrary unary operation and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
auto m_MaxOrMin(const Opnd0 &Op0, const Opnd1 &Op1)
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
auto m_VecReverse(const Opnd0 &Op0)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
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.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
auto m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
@ SingleThread
Synchronized with respect to signal handlers executing in the same thread.
Definition LLVMContext.h:55
@ System
Synchronized with respect to all concurrently executing threads.
Definition LLVMContext.h:58
SmallVector< DbgVariableRecord * > getDVRAssignmentMarkers(const Instruction *Inst)
Return a range of dbg_assign records for which Inst performs the assignment they encode.
Definition DebugInfo.h:205
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
constexpr double e
DiagnosticInfoOptimizationBase::Argument NV
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
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:345
@ Offset
Definition DWP.cpp:578
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
LLVM_ABI Value * simplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FMul, fold the result or return null.
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
LLVM_ABI 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 ...
BundleAttr getBundleAttrFromOBU(OperandBundleUse OBU)
@ Known
Known to have no common set bits.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool isRemovableAlloc(const CallBase *V, const TargetLibraryInfo *TLI)
Return true if this is a call to an allocation function that does not have side effects that we are r...
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
constexpr int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
Definition MathExtras.h:224
LLVM_ABI 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.
LLVM_ABI AssumeSeparateStorageInfo getAssumeSeparateStorageInfo(OperandBundleUse)
LLVM_ABI Value * getAllocAlignment(const CallBase *V, const TargetLibraryInfo *TLI)
Gets the alignment argument for an aligned_alloc-like function, using either built-in knowledge based...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1793
LLVM_ABI Value * simplifyCall(CallBase *Call, Value *Callee, ArrayRef< Value * > Args, const SimplifyQuery &Q)
Given a callsite, callee, and arguments, fold the result or return null.
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:547
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:285
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
constexpr T MinAlign(U A, V B)
A and B are either alignments or offsets.
Definition MathExtras.h:358
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
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:248
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI bool isSplatValue(const Value *V, int Index=-1, unsigned Depth=0)
Return true if each element of the vector value V is poisoned or equal to every other non-poisoned el...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1748
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
LLVM_ABI Constant * getLosslessUnsignedTrunc(Constant *C, Type *DestTy, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
bool isModSet(const ModRefInfo MRI)
Definition ModRef.h:49
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1779
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
Returns: X * 2^Exp for integral exponents.
Definition APFloat.h:1693
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI 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...
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
auto find_if_not(R &&Range, UnaryPredicate P)
Definition STLExtras.h:1777
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1753
bool isAtLeastOrStrongerThan(AtomicOrdering AO, AtomicOrdering Other)
LLVM_ABI Constant * getLosslessSignedTrunc(Constant *C, Type *DestTy, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
iterator_range< SplittingIterator > split(StringRef Str, StringRef Separator)
Split the specified string over a separator and return a range-compatible iterable over its partition...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
@ Mod
The access may modify the value stored in memory.
Definition ModRef.h:34
LLVM_ABI Value * simplifyFMAFMul(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for the multiplication of a FMA, fold the result or return null.
@ Other
Any other memory.
Definition ModRef.h:68
LLVM_ABI Value * simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q)
Given a constrained FP intrinsic call, tries to compute its simplified version.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1729
OperandBundleDefT< Value * > OperandBundleDef
Definition AutoUpgrade.h:34
LLVM_ABI AssumeNonNullInfo getAssumeNonNullInfo(OperandBundleUse)
@ Add
Sum of integers.
LLVM_ABI bool isVectorIntrinsicWithScalarOpAtArg(Intrinsic::ID ID, unsigned ScalarOpdIdx, const TargetTransformInfo *TTI)
Identifies if the vector form of the intrinsic has a scalar operand.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
LLVM_ABI ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
DWARFExpression::Operation Op
bool isSafeToSpeculativelyExecuteWithVariableReplaced(const Instruction *I, bool IgnoreUBImplyingAttrs=true)
Don't use information from its non-constant operands.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI Value * getFreedOperand(const CallBase *CB, const TargetLibraryInfo *TLI)
If this if a call to a free function, return the freed operand.
constexpr int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Definition MathExtras.h:233
constexpr unsigned BitWidth
LLVM_ABI Constant * getLosslessInvCast(Constant *C, Type *InvCastTo, unsigned CastOp, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
Try to cast C to InvC losslessly, satisfying CastOp(InvC) equals C, or CastOp(InvC) is a refined valu...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
LLVM_ABI std::optional< APInt > getAllocSize(const CallBase *CB, const TargetLibraryInfo *TLI, function_ref< const Value *(const Value *)> Mapper=[](const Value *V) { return V;})
Return the size of the requested allocation.
LLVM_ABI AssumeAlignInfo getAssumeAlignInfo(OperandBundleUse)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool maskContainsAllOneOrUndef(Value *Mask)
Given a mask vector of i1, Return true if any of the elements of this predicate mask are known to be ...
LLVM_ABI std::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...
LLVM_ABI bool isDereferenceablePointer(const Value *V, Type *Ty, const SimplifyQuery &Q, bool IgnoreFree=false)
Equivalent to isDereferenceableAndAlignedPointer with an alignment of 1.
Definition Loads.cpp:264
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1766
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1806
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI AssumeDereferenceableInfo getAssumeDereferenceableInfo(OperandBundleUse)
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI AssumeNoUndefInfo getAssumeNoUndefInfo(OperandBundleUse)
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, const SimplifyQuery &SQ, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
#define NC
Definition regutils.h:42
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:763
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
@ IEEE
IEEE-754 denormal numbers preserved.
Matching combinators.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition Alignment.h:130
uint32_t getTagID() const
Return the tag of this operand bundle as an integer.
ArrayRef< Use > Inputs
SelectPatternFlavor Flavor
const DataLayout & DL
const Instruction * CxtI
SimplifyQuery getWithInstruction(const Instruction *I) const