LLVM 22.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"
21#include "llvm/ADT/Statistic.h"
26#include "llvm/Analysis/Loads.h"
31#include "llvm/IR/Attributes.h"
32#include "llvm/IR/BasicBlock.h"
33#include "llvm/IR/Constant.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/DebugInfo.h"
38#include "llvm/IR/Function.h"
40#include "llvm/IR/InlineAsm.h"
41#include "llvm/IR/InstrTypes.h"
42#include "llvm/IR/Instruction.h"
45#include "llvm/IR/Intrinsics.h"
46#include "llvm/IR/IntrinsicsAArch64.h"
47#include "llvm/IR/IntrinsicsAMDGPU.h"
48#include "llvm/IR/IntrinsicsARM.h"
49#include "llvm/IR/IntrinsicsHexagon.h"
50#include "llvm/IR/LLVMContext.h"
51#include "llvm/IR/Metadata.h"
53#include "llvm/IR/Statepoint.h"
54#include "llvm/IR/Type.h"
55#include "llvm/IR/User.h"
56#include "llvm/IR/Value.h"
57#include "llvm/IR/ValueHandle.h"
62#include "llvm/Support/Debug.h"
72#include <algorithm>
73#include <cassert>
74#include <cstdint>
75#include <optional>
76#include <utility>
77#include <vector>
78
79#define DEBUG_TYPE "instcombine"
81
82using namespace llvm;
83using namespace PatternMatch;
84
85STATISTIC(NumSimplified, "Number of library calls simplified");
86
88 "instcombine-guard-widening-window",
89 cl::init(3),
90 cl::desc("How wide an instruction window to bypass looking for "
91 "another guard"));
92
93/// Return the specified type promoted as it would be to pass though a va_arg
94/// area.
96 if (IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
97 if (ITy->getBitWidth() < 32)
98 return Type::getInt32Ty(Ty->getContext());
99 }
100 return Ty;
101}
102
103/// Recognize a memcpy/memmove from a trivially otherwise unused alloca.
104/// TODO: This should probably be integrated with visitAllocSites, but that
105/// requires a deeper change to allow either unread or unwritten objects.
107 auto *Src = MI->getRawSource();
108 while (isa<GetElementPtrInst>(Src)) {
109 if (!Src->hasOneUse())
110 return false;
111 Src = cast<Instruction>(Src)->getOperand(0);
112 }
113 return isa<AllocaInst>(Src) && Src->hasOneUse();
114}
115
117 Align DstAlign = getKnownAlignment(MI->getRawDest(), DL, MI, &AC, &DT);
118 MaybeAlign CopyDstAlign = MI->getDestAlign();
119 if (!CopyDstAlign || *CopyDstAlign < DstAlign) {
120 MI->setDestAlignment(DstAlign);
121 return MI;
122 }
123
124 Align SrcAlign = getKnownAlignment(MI->getRawSource(), DL, MI, &AC, &DT);
125 MaybeAlign CopySrcAlign = MI->getSourceAlign();
126 if (!CopySrcAlign || *CopySrcAlign < SrcAlign) {
127 MI->setSourceAlignment(SrcAlign);
128 return MI;
129 }
130
131 // If we have a store to a location which is known constant, we can conclude
132 // that the store must be storing the constant value (else the memory
133 // wouldn't be constant), and this must be a noop.
134 if (!isModSet(AA->getModRefInfoMask(MI->getDest()))) {
135 // Set the size of the copy to 0, it will be deleted on the next iteration.
136 MI->setLength((uint64_t)0);
137 return MI;
138 }
139
140 // If the source is provably undef, the memcpy/memmove doesn't do anything
141 // (unless the transfer is volatile).
142 if (hasUndefSource(MI) && !MI->isVolatile()) {
143 // Set the size of the copy to 0, it will be deleted on the next iteration.
144 MI->setLength((uint64_t)0);
145 return MI;
146 }
147
148 // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
149 // load/store.
150 ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getLength());
151 if (!MemOpLength) return nullptr;
152
153 // Source and destination pointer types are always "i8*" for intrinsic. See
154 // if the size is something we can handle with a single primitive load/store.
155 // A single load+store correctly handles overlapping memory in the memmove
156 // case.
157 uint64_t Size = MemOpLength->getLimitedValue();
158 assert(Size && "0-sized memory transferring should be removed already.");
159
160 if (Size > 8 || (Size&(Size-1)))
161 return nullptr; // If not 1/2/4/8 bytes, exit.
162
163 // If it is an atomic and alignment is less than the size then we will
164 // introduce the unaligned memory access which will be later transformed
165 // into libcall in CodeGen. This is not evident performance gain so disable
166 // it now.
167 if (MI->isAtomic())
168 if (*CopyDstAlign < Size || *CopySrcAlign < Size)
169 return nullptr;
170
171 // Use an integer load+store unless we can find something better.
172 IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
173
174 // If the memcpy has metadata describing the members, see if we can get the
175 // TBAA, scope and noalias tags describing our copy.
176 AAMDNodes AACopyMD = MI->getAAMetadata().adjustForAccess(Size);
177
178 Value *Src = MI->getArgOperand(1);
179 Value *Dest = MI->getArgOperand(0);
180 LoadInst *L = Builder.CreateLoad(IntType, Src);
181 // Alignment from the mem intrinsic will be better, so use it.
182 L->setAlignment(*CopySrcAlign);
183 L->setAAMetadata(AACopyMD);
184 MDNode *LoopMemParallelMD =
185 MI->getMetadata(LLVMContext::MD_mem_parallel_loop_access);
186 if (LoopMemParallelMD)
187 L->setMetadata(LLVMContext::MD_mem_parallel_loop_access, LoopMemParallelMD);
188 MDNode *AccessGroupMD = MI->getMetadata(LLVMContext::MD_access_group);
189 if (AccessGroupMD)
190 L->setMetadata(LLVMContext::MD_access_group, AccessGroupMD);
191
192 StoreInst *S = Builder.CreateStore(L, Dest);
193 // Alignment from the mem intrinsic will be better, so use it.
194 S->setAlignment(*CopyDstAlign);
195 S->setAAMetadata(AACopyMD);
196 if (LoopMemParallelMD)
197 S->setMetadata(LLVMContext::MD_mem_parallel_loop_access, LoopMemParallelMD);
198 if (AccessGroupMD)
199 S->setMetadata(LLVMContext::MD_access_group, AccessGroupMD);
200 S->copyMetadata(*MI, LLVMContext::MD_DIAssignID);
201
202 if (auto *MT = dyn_cast<MemTransferInst>(MI)) {
203 // non-atomics can be volatile
204 L->setVolatile(MT->isVolatile());
205 S->setVolatile(MT->isVolatile());
206 }
207 if (MI->isAtomic()) {
208 // atomics have to be unordered
209 L->setOrdering(AtomicOrdering::Unordered);
211 }
212
213 // Set the size of the copy to 0, it will be deleted on the next iteration.
214 MI->setLength((uint64_t)0);
215 return MI;
216}
217
219 const Align KnownAlignment =
220 getKnownAlignment(MI->getDest(), DL, MI, &AC, &DT);
221 MaybeAlign MemSetAlign = MI->getDestAlign();
222 if (!MemSetAlign || *MemSetAlign < KnownAlignment) {
223 MI->setDestAlignment(KnownAlignment);
224 return MI;
225 }
226
227 // If we have a store to a location which is known constant, we can conclude
228 // that the store must be storing the constant value (else the memory
229 // wouldn't be constant), and this must be a noop.
230 if (!isModSet(AA->getModRefInfoMask(MI->getDest()))) {
231 // Set the size of the copy to 0, it will be deleted on the next iteration.
232 MI->setLength((uint64_t)0);
233 return MI;
234 }
235
236 // Remove memset with an undef value.
237 // FIXME: This is technically incorrect because it might overwrite a poison
238 // value. Change to PoisonValue once #52930 is resolved.
239 if (isa<UndefValue>(MI->getValue())) {
240 // Set the size of the copy to 0, it will be deleted on the next iteration.
241 MI->setLength((uint64_t)0);
242 return MI;
243 }
244
245 // Extract the length and alignment and fill if they are constant.
246 ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
247 ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
248 if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8))
249 return nullptr;
250 const uint64_t Len = LenC->getLimitedValue();
251 assert(Len && "0-sized memory setting should be removed already.");
252 const Align Alignment = MI->getDestAlign().valueOrOne();
253
254 // If it is an atomic and alignment is less than the size then we will
255 // introduce the unaligned memory access which will be later transformed
256 // into libcall in CodeGen. This is not evident performance gain so disable
257 // it now.
258 if (MI->isAtomic() && Alignment < Len)
259 return nullptr;
260
261 // memset(s,c,n) -> store s, c (for n=1,2,4,8)
262 if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
263 Value *Dest = MI->getDest();
264
265 // Extract the fill value and store.
266 Constant *FillVal = ConstantInt::get(
267 MI->getContext(), APInt::getSplat(Len * 8, FillC->getValue()));
268 StoreInst *S = Builder.CreateStore(FillVal, Dest, MI->isVolatile());
269 S->copyMetadata(*MI, LLVMContext::MD_DIAssignID);
270 for (DbgVariableRecord *DbgAssign : at::getDVRAssignmentMarkers(S)) {
271 if (llvm::is_contained(DbgAssign->location_ops(), FillC))
272 DbgAssign->replaceVariableLocationOp(FillC, FillVal);
273 }
274
275 S->setAlignment(Alignment);
276 if (MI->isAtomic())
278
279 // Set the size of the copy to 0, it will be deleted on the next iteration.
280 MI->setLength((uint64_t)0);
281 return MI;
282 }
283
284 return nullptr;
285}
286
287// TODO, Obvious Missing Transforms:
288// * Narrow width by halfs excluding zero/undef lanes
289Value *InstCombinerImpl::simplifyMaskedLoad(IntrinsicInst &II) {
290 Value *LoadPtr = II.getArgOperand(0);
291 const Align Alignment =
292 cast<ConstantInt>(II.getArgOperand(1))->getAlignValue();
293
294 // If the mask is all ones or undefs, this is a plain vector load of the 1st
295 // argument.
296 if (maskIsAllOneOrUndef(II.getArgOperand(2))) {
297 LoadInst *L = Builder.CreateAlignedLoad(II.getType(), LoadPtr, Alignment,
298 "unmaskedload");
299 L->copyMetadata(II);
300 return L;
301 }
302
303 // If we can unconditionally load from this address, replace with a
304 // load/select idiom. TODO: use DT for context sensitive query
305 if (isDereferenceablePointer(LoadPtr, II.getType(),
306 II.getDataLayout(), &II, &AC)) {
307 LoadInst *LI = Builder.CreateAlignedLoad(II.getType(), LoadPtr, Alignment,
308 "unmaskedload");
309 LI->copyMetadata(II);
310 return Builder.CreateSelect(II.getArgOperand(2), LI, II.getArgOperand(3));
311 }
312
313 return nullptr;
314}
315
316// TODO, Obvious Missing Transforms:
317// * Single constant active lane -> store
318// * Narrow width by halfs excluding zero/undef lanes
319Instruction *InstCombinerImpl::simplifyMaskedStore(IntrinsicInst &II) {
320 auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3));
321 if (!ConstMask)
322 return nullptr;
323
324 // If the mask is all zeros, this instruction does nothing.
325 if (ConstMask->isNullValue())
327
328 // If the mask is all ones, this is a plain vector store of the 1st argument.
329 if (ConstMask->isAllOnesValue()) {
330 Value *StorePtr = II.getArgOperand(1);
331 Align Alignment = cast<ConstantInt>(II.getArgOperand(2))->getAlignValue();
332 StoreInst *S =
333 new StoreInst(II.getArgOperand(0), StorePtr, false, Alignment);
334 S->copyMetadata(II);
335 return S;
336 }
337
338 if (isa<ScalableVectorType>(ConstMask->getType()))
339 return nullptr;
340
341 // Use masked off lanes to simplify operands via SimplifyDemandedVectorElts
342 APInt DemandedElts = possiblyDemandedEltsInMask(ConstMask);
343 APInt PoisonElts(DemandedElts.getBitWidth(), 0);
344 if (Value *V = SimplifyDemandedVectorElts(II.getOperand(0), DemandedElts,
345 PoisonElts))
346 return replaceOperand(II, 0, V);
347
348 return nullptr;
349}
350
351// TODO, Obvious Missing Transforms:
352// * Single constant active lane load -> load
353// * Dereferenceable address & few lanes -> scalarize speculative load/selects
354// * Adjacent vector addresses -> masked.load
355// * Narrow width by halfs excluding zero/undef lanes
356// * Vector incrementing address -> vector masked load
357Instruction *InstCombinerImpl::simplifyMaskedGather(IntrinsicInst &II) {
358 auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(2));
359 if (!ConstMask)
360 return nullptr;
361
362 // Vector splat address w/known mask -> scalar load
363 // Fold the gather to load the source vector first lane
364 // because it is reloading the same value each time
365 if (ConstMask->isAllOnesValue())
366 if (auto *SplatPtr = getSplatValue(II.getArgOperand(0))) {
367 auto *VecTy = cast<VectorType>(II.getType());
368 const Align Alignment =
369 cast<ConstantInt>(II.getArgOperand(1))->getAlignValue();
370 LoadInst *L = Builder.CreateAlignedLoad(VecTy->getElementType(), SplatPtr,
371 Alignment, "load.scalar");
372 Value *Shuf =
373 Builder.CreateVectorSplat(VecTy->getElementCount(), L, "broadcast");
374 return replaceInstUsesWith(II, cast<Instruction>(Shuf));
375 }
376
377 return nullptr;
378}
379
380// TODO, Obvious Missing Transforms:
381// * Single constant active lane -> store
382// * Adjacent vector addresses -> masked.store
383// * Narrow store width by halfs excluding zero/undef lanes
384// * Vector incrementing address -> vector masked store
385Instruction *InstCombinerImpl::simplifyMaskedScatter(IntrinsicInst &II) {
386 auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3));
387 if (!ConstMask)
388 return nullptr;
389
390 // If the mask is all zeros, a scatter does nothing.
391 if (ConstMask->isNullValue())
393
394 // Vector splat address -> scalar store
395 if (auto *SplatPtr = getSplatValue(II.getArgOperand(1))) {
396 // scatter(splat(value), splat(ptr), non-zero-mask) -> store value, ptr
397 if (auto *SplatValue = getSplatValue(II.getArgOperand(0))) {
398 if (maskContainsAllOneOrUndef(ConstMask)) {
399 Align Alignment =
400 cast<ConstantInt>(II.getArgOperand(2))->getAlignValue();
401 StoreInst *S = new StoreInst(SplatValue, SplatPtr, /*IsVolatile=*/false,
402 Alignment);
403 S->copyMetadata(II);
404 return S;
405 }
406 }
407 // scatter(vector, splat(ptr), splat(true)) -> store extract(vector,
408 // lastlane), ptr
409 if (ConstMask->isAllOnesValue()) {
410 Align Alignment = cast<ConstantInt>(II.getArgOperand(2))->getAlignValue();
411 VectorType *WideLoadTy = cast<VectorType>(II.getArgOperand(1)->getType());
412 ElementCount VF = WideLoadTy->getElementCount();
414 Value *LastLane = Builder.CreateSub(RunTimeVF, Builder.getInt32(1));
415 Value *Extract =
416 Builder.CreateExtractElement(II.getArgOperand(0), LastLane);
417 StoreInst *S =
418 new StoreInst(Extract, SplatPtr, /*IsVolatile=*/false, Alignment);
419 S->copyMetadata(II);
420 return S;
421 }
422 }
423 if (isa<ScalableVectorType>(ConstMask->getType()))
424 return nullptr;
425
426 // Use masked off lanes to simplify operands via SimplifyDemandedVectorElts
427 APInt DemandedElts = possiblyDemandedEltsInMask(ConstMask);
428 APInt PoisonElts(DemandedElts.getBitWidth(), 0);
429 if (Value *V = SimplifyDemandedVectorElts(II.getOperand(0), DemandedElts,
430 PoisonElts))
431 return replaceOperand(II, 0, V);
432 if (Value *V = SimplifyDemandedVectorElts(II.getOperand(1), DemandedElts,
433 PoisonElts))
434 return replaceOperand(II, 1, V);
435
436 return nullptr;
437}
438
439/// This function transforms launder.invariant.group and strip.invariant.group
440/// like:
441/// launder(launder(%x)) -> launder(%x) (the result is not the argument)
442/// launder(strip(%x)) -> launder(%x)
443/// strip(strip(%x)) -> strip(%x) (the result is not the argument)
444/// strip(launder(%x)) -> strip(%x)
445/// This is legal because it preserves the most recent information about
446/// the presence or absence of invariant.group.
448 InstCombinerImpl &IC) {
449 auto *Arg = II.getArgOperand(0);
450 auto *StrippedArg = Arg->stripPointerCasts();
451 auto *StrippedInvariantGroupsArg = StrippedArg;
452 while (auto *Intr = dyn_cast<IntrinsicInst>(StrippedInvariantGroupsArg)) {
453 if (Intr->getIntrinsicID() != Intrinsic::launder_invariant_group &&
454 Intr->getIntrinsicID() != Intrinsic::strip_invariant_group)
455 break;
456 StrippedInvariantGroupsArg = Intr->getArgOperand(0)->stripPointerCasts();
457 }
458 if (StrippedArg == StrippedInvariantGroupsArg)
459 return nullptr; // No launders/strips to remove.
460
461 Value *Result = nullptr;
462
463 if (II.getIntrinsicID() == Intrinsic::launder_invariant_group)
464 Result = IC.Builder.CreateLaunderInvariantGroup(StrippedInvariantGroupsArg);
465 else if (II.getIntrinsicID() == Intrinsic::strip_invariant_group)
466 Result = IC.Builder.CreateStripInvariantGroup(StrippedInvariantGroupsArg);
467 else
469 "simplifyInvariantGroupIntrinsic only handles launder and strip");
470 if (Result->getType()->getPointerAddressSpace() !=
471 II.getType()->getPointerAddressSpace())
472 Result = IC.Builder.CreateAddrSpaceCast(Result, II.getType());
473
474 return cast<Instruction>(Result);
475}
476
478 assert((II.getIntrinsicID() == Intrinsic::cttz ||
479 II.getIntrinsicID() == Intrinsic::ctlz) &&
480 "Expected cttz or ctlz intrinsic");
481 bool IsTZ = II.getIntrinsicID() == Intrinsic::cttz;
482 Value *Op0 = II.getArgOperand(0);
483 Value *Op1 = II.getArgOperand(1);
484 Value *X;
485 // ctlz(bitreverse(x)) -> cttz(x)
486 // cttz(bitreverse(x)) -> ctlz(x)
487 if (match(Op0, m_BitReverse(m_Value(X)))) {
488 Intrinsic::ID ID = IsTZ ? Intrinsic::ctlz : Intrinsic::cttz;
489 Function *F =
490 Intrinsic::getOrInsertDeclaration(II.getModule(), ID, II.getType());
491 return CallInst::Create(F, {X, II.getArgOperand(1)});
492 }
493
494 if (II.getType()->isIntOrIntVectorTy(1)) {
495 // ctlz/cttz i1 Op0 --> not Op0
496 if (match(Op1, m_Zero()))
497 return BinaryOperator::CreateNot(Op0);
498 // If zero is poison, then the input can be assumed to be "true", so the
499 // instruction simplifies to "false".
500 assert(match(Op1, m_One()) && "Expected ctlz/cttz operand to be 0 or 1");
501 return IC.replaceInstUsesWith(II, ConstantInt::getNullValue(II.getType()));
502 }
503
504 // If ctlz/cttz is only used as a shift amount, set is_zero_poison to true.
505 if (II.hasOneUse() && match(Op1, m_Zero()) &&
506 match(II.user_back(), m_Shift(m_Value(), m_Specific(&II)))) {
507 II.dropUBImplyingAttrsAndMetadata();
508 return IC.replaceOperand(II, 1, IC.Builder.getTrue());
509 }
510
511 Constant *C;
512
513 if (IsTZ) {
514 // cttz(-x) -> cttz(x)
515 if (match(Op0, m_Neg(m_Value(X))))
516 return IC.replaceOperand(II, 0, X);
517
518 // cttz(-x & x) -> cttz(x)
519 if (match(Op0, m_c_And(m_Neg(m_Value(X)), m_Deferred(X))))
520 return IC.replaceOperand(II, 0, X);
521
522 // cttz(sext(x)) -> cttz(zext(x))
523 if (match(Op0, m_OneUse(m_SExt(m_Value(X))))) {
524 auto *Zext = IC.Builder.CreateZExt(X, II.getType());
525 auto *CttzZext =
526 IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, Zext, Op1);
527 return IC.replaceInstUsesWith(II, CttzZext);
528 }
529
530 // Zext doesn't change the number of trailing zeros, so narrow:
531 // cttz(zext(x)) -> zext(cttz(x)) if the 'ZeroIsPoison' parameter is 'true'.
532 if (match(Op0, m_OneUse(m_ZExt(m_Value(X)))) && match(Op1, m_One())) {
533 auto *Cttz = IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, X,
534 IC.Builder.getTrue());
535 auto *ZextCttz = IC.Builder.CreateZExt(Cttz, II.getType());
536 return IC.replaceInstUsesWith(II, ZextCttz);
537 }
538
539 // cttz(abs(x)) -> cttz(x)
540 // cttz(nabs(x)) -> cttz(x)
541 Value *Y;
543 if (SPF == SPF_ABS || SPF == SPF_NABS)
544 return IC.replaceOperand(II, 0, X);
545
546 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X))))
547 return IC.replaceOperand(II, 0, X);
548
549 // cttz(shl(%const, %val), 1) --> add(cttz(%const, 1), %val)
550 if (match(Op0, m_Shl(m_ImmConstant(C), m_Value(X))) &&
551 match(Op1, m_One())) {
552 Value *ConstCttz =
553 IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, C, Op1);
554 return BinaryOperator::CreateAdd(ConstCttz, X);
555 }
556
557 // cttz(lshr exact (%const, %val), 1) --> sub(cttz(%const, 1), %val)
558 if (match(Op0, m_Exact(m_LShr(m_ImmConstant(C), m_Value(X)))) &&
559 match(Op1, m_One())) {
560 Value *ConstCttz =
561 IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, C, Op1);
562 return BinaryOperator::CreateSub(ConstCttz, X);
563 }
564
565 // cttz(add(lshr(UINT_MAX, %val), 1)) --> sub(width, %val)
566 if (match(Op0, m_Add(m_LShr(m_AllOnes(), m_Value(X)), m_One()))) {
567 Value *Width =
568 ConstantInt::get(II.getType(), II.getType()->getScalarSizeInBits());
569 return BinaryOperator::CreateSub(Width, X);
570 }
571 } else {
572 // ctlz(lshr(%const, %val), 1) --> add(ctlz(%const, 1), %val)
573 if (match(Op0, m_LShr(m_ImmConstant(C), m_Value(X))) &&
574 match(Op1, m_One())) {
575 Value *ConstCtlz =
576 IC.Builder.CreateBinaryIntrinsic(Intrinsic::ctlz, C, Op1);
577 return BinaryOperator::CreateAdd(ConstCtlz, X);
578 }
579
580 // ctlz(shl nuw (%const, %val), 1) --> sub(ctlz(%const, 1), %val)
581 if (match(Op0, m_NUWShl(m_ImmConstant(C), m_Value(X))) &&
582 match(Op1, m_One())) {
583 Value *ConstCtlz =
584 IC.Builder.CreateBinaryIntrinsic(Intrinsic::ctlz, C, Op1);
585 return BinaryOperator::CreateSub(ConstCtlz, X);
586 }
587 }
588
589 // cttz(Pow2) -> Log2(Pow2)
590 // ctlz(Pow2) -> BitWidth - 1 - Log2(Pow2)
591 if (auto *R = IC.tryGetLog2(Op0, match(Op1, m_One()))) {
592 if (IsTZ)
593 return IC.replaceInstUsesWith(II, R);
594 BinaryOperator *BO = BinaryOperator::CreateSub(
595 ConstantInt::get(R->getType(), R->getType()->getScalarSizeInBits() - 1),
596 R);
597 BO->setHasNoSignedWrap();
599 return BO;
600 }
601
602 KnownBits Known = IC.computeKnownBits(Op0, &II);
603
604 // Create a mask for bits above (ctlz) or below (cttz) the first known one.
605 unsigned PossibleZeros = IsTZ ? Known.countMaxTrailingZeros()
606 : Known.countMaxLeadingZeros();
607 unsigned DefiniteZeros = IsTZ ? Known.countMinTrailingZeros()
608 : Known.countMinLeadingZeros();
609
610 // If all bits above (ctlz) or below (cttz) the first known one are known
611 // zero, this value is constant.
612 // FIXME: This should be in InstSimplify because we're replacing an
613 // instruction with a constant.
614 if (PossibleZeros == DefiniteZeros) {
615 auto *C = ConstantInt::get(Op0->getType(), DefiniteZeros);
616 return IC.replaceInstUsesWith(II, C);
617 }
618
619 // If the input to cttz/ctlz is known to be non-zero,
620 // then change the 'ZeroIsPoison' parameter to 'true'
621 // because we know the zero behavior can't affect the result.
622 if (!Known.One.isZero() ||
624 if (!match(II.getArgOperand(1), m_One()))
625 return IC.replaceOperand(II, 1, IC.Builder.getTrue());
626 }
627
628 // Add range attribute since known bits can't completely reflect what we know.
629 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
630 if (BitWidth != 1 && !II.hasRetAttr(Attribute::Range) &&
631 !II.getMetadata(LLVMContext::MD_range)) {
632 ConstantRange Range(APInt(BitWidth, DefiniteZeros),
633 APInt(BitWidth, PossibleZeros + 1));
634 II.addRangeRetAttr(Range);
635 return &II;
636 }
637
638 return nullptr;
639}
640
642 assert(II.getIntrinsicID() == Intrinsic::ctpop &&
643 "Expected ctpop intrinsic");
644 Type *Ty = II.getType();
645 unsigned BitWidth = Ty->getScalarSizeInBits();
646 Value *Op0 = II.getArgOperand(0);
647 Value *X, *Y;
648
649 // ctpop(bitreverse(x)) -> ctpop(x)
650 // ctpop(bswap(x)) -> ctpop(x)
651 if (match(Op0, m_BitReverse(m_Value(X))) || match(Op0, m_BSwap(m_Value(X))))
652 return IC.replaceOperand(II, 0, X);
653
654 // ctpop(rot(x)) -> ctpop(x)
655 if ((match(Op0, m_FShl(m_Value(X), m_Value(Y), m_Value())) ||
656 match(Op0, m_FShr(m_Value(X), m_Value(Y), m_Value()))) &&
657 X == Y)
658 return IC.replaceOperand(II, 0, X);
659
660 // ctpop(x | -x) -> bitwidth - cttz(x, false)
661 if (Op0->hasOneUse() &&
662 match(Op0, m_c_Or(m_Value(X), m_Neg(m_Deferred(X))))) {
663 auto *Cttz = IC.Builder.CreateIntrinsic(Intrinsic::cttz, Ty,
664 {X, IC.Builder.getFalse()});
665 auto *Bw = ConstantInt::get(Ty, APInt(BitWidth, BitWidth));
666 return IC.replaceInstUsesWith(II, IC.Builder.CreateSub(Bw, Cttz));
667 }
668
669 // ctpop(~x & (x - 1)) -> cttz(x, false)
670 if (match(Op0,
672 Function *F =
673 Intrinsic::getOrInsertDeclaration(II.getModule(), Intrinsic::cttz, Ty);
674 return CallInst::Create(F, {X, IC.Builder.getFalse()});
675 }
676
677 // Zext doesn't change the number of set bits, so narrow:
678 // ctpop (zext X) --> zext (ctpop X)
679 if (match(Op0, m_OneUse(m_ZExt(m_Value(X))))) {
680 Value *NarrowPop = IC.Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, X);
681 return CastInst::Create(Instruction::ZExt, NarrowPop, Ty);
682 }
683
684 KnownBits Known(BitWidth);
685 IC.computeKnownBits(Op0, Known, &II);
686
687 // If all bits are zero except for exactly one fixed bit, then the result
688 // must be 0 or 1, and we can get that answer by shifting to LSB:
689 // ctpop (X & 32) --> (X & 32) >> 5
690 // TODO: Investigate removing this as its likely unnecessary given the below
691 // `isKnownToBeAPowerOfTwo` check.
692 if ((~Known.Zero).isPowerOf2())
693 return BinaryOperator::CreateLShr(
694 Op0, ConstantInt::get(Ty, (~Known.Zero).exactLogBase2()));
695
696 // More generally we can also handle non-constant power of 2 patterns such as
697 // shl/shr(Pow2, X), (X & -X), etc... by transforming:
698 // ctpop(Pow2OrZero) --> icmp ne X, 0
699 if (IC.isKnownToBeAPowerOfTwo(Op0, /* OrZero */ true))
700 return CastInst::Create(Instruction::ZExt,
703 Ty);
704
705 // Add range attribute since known bits can't completely reflect what we know.
706 if (BitWidth != 1) {
707 ConstantRange OldRange =
708 II.getRange().value_or(ConstantRange::getFull(BitWidth));
709
710 unsigned Lower = Known.countMinPopulation();
711 unsigned Upper = Known.countMaxPopulation() + 1;
712
713 if (Lower == 0 && OldRange.contains(APInt::getZero(BitWidth)) &&
715 Lower = 1;
716
719
720 if (Range != OldRange) {
721 II.addRangeRetAttr(Range);
722 return &II;
723 }
724 }
725
726 return nullptr;
727}
728
729/// Convert a table lookup to shufflevector if the mask is constant.
730/// This could benefit tbl1 if the mask is { 7,6,5,4,3,2,1,0 }, in
731/// which case we could lower the shufflevector with rev64 instructions
732/// as it's actually a byte reverse.
734 InstCombiner::BuilderTy &Builder) {
735 // Bail out if the mask is not a constant.
736 auto *C = dyn_cast<Constant>(II.getArgOperand(1));
737 if (!C)
738 return nullptr;
739
740 auto *VecTy = cast<FixedVectorType>(II.getType());
741 unsigned NumElts = VecTy->getNumElements();
742
743 // Only perform this transformation for <8 x i8> vector types.
744 if (!VecTy->getElementType()->isIntegerTy(8) || NumElts != 8)
745 return nullptr;
746
747 int Indexes[8];
748
749 for (unsigned I = 0; I < NumElts; ++I) {
750 Constant *COp = C->getAggregateElement(I);
751
752 if (!COp || !isa<ConstantInt>(COp))
753 return nullptr;
754
755 Indexes[I] = cast<ConstantInt>(COp)->getLimitedValue();
756
757 // Make sure the mask indices are in range.
758 if ((unsigned)Indexes[I] >= NumElts)
759 return nullptr;
760 }
761
762 auto *V1 = II.getArgOperand(0);
763 auto *V2 = Constant::getNullValue(V1->getType());
764 return Builder.CreateShuffleVector(V1, V2, ArrayRef(Indexes));
765}
766
767// Returns true iff the 2 intrinsics have the same operands, limiting the
768// comparison to the first NumOperands.
769static bool haveSameOperands(const IntrinsicInst &I, const IntrinsicInst &E,
770 unsigned NumOperands) {
771 assert(I.arg_size() >= NumOperands && "Not enough operands");
772 assert(E.arg_size() >= NumOperands && "Not enough operands");
773 for (unsigned i = 0; i < NumOperands; i++)
774 if (I.getArgOperand(i) != E.getArgOperand(i))
775 return false;
776 return true;
777}
778
779// Remove trivially empty start/end intrinsic ranges, i.e. a start
780// immediately followed by an end (ignoring debuginfo or other
781// start/end intrinsics in between). As this handles only the most trivial
782// cases, tracking the nesting level is not needed:
783//
784// call @llvm.foo.start(i1 0)
785// call @llvm.foo.start(i1 0) ; This one won't be skipped: it will be removed
786// call @llvm.foo.end(i1 0)
787// call @llvm.foo.end(i1 0) ; &I
788static bool
790 std::function<bool(const IntrinsicInst &)> IsStart) {
791 // We start from the end intrinsic and scan backwards, so that InstCombine
792 // has already processed (and potentially removed) all the instructions
793 // before the end intrinsic.
794 BasicBlock::reverse_iterator BI(EndI), BE(EndI.getParent()->rend());
795 for (; BI != BE; ++BI) {
796 if (auto *I = dyn_cast<IntrinsicInst>(&*BI)) {
797 if (I->isDebugOrPseudoInst() ||
798 I->getIntrinsicID() == EndI.getIntrinsicID())
799 continue;
800 if (IsStart(*I)) {
801 if (haveSameOperands(EndI, *I, EndI.arg_size())) {
803 IC.eraseInstFromFunction(EndI);
804 return true;
805 }
806 // Skip start intrinsics that don't pair with this end intrinsic.
807 continue;
808 }
809 }
810 break;
811 }
812
813 return false;
814}
815
817 removeTriviallyEmptyRange(I, *this, [&I](const IntrinsicInst &II) {
818 // Bail out on the case where the source va_list of a va_copy is destroyed
819 // immediately by a follow-up va_end.
820 return II.getIntrinsicID() == Intrinsic::vastart ||
821 (II.getIntrinsicID() == Intrinsic::vacopy &&
822 I.getArgOperand(0) != II.getArgOperand(1));
823 });
824 return nullptr;
825}
826
828 assert(Call.arg_size() > 1 && "Need at least 2 args to swap");
829 Value *Arg0 = Call.getArgOperand(0), *Arg1 = Call.getArgOperand(1);
830 if (isa<Constant>(Arg0) && !isa<Constant>(Arg1)) {
831 Call.setArgOperand(0, Arg1);
832 Call.setArgOperand(1, Arg0);
833 return &Call;
834 }
835 return nullptr;
836}
837
838/// Creates a result tuple for an overflow intrinsic \p II with a given
839/// \p Result and a constant \p Overflow value.
841 Constant *Overflow) {
842 Constant *V[] = {PoisonValue::get(Result->getType()), Overflow};
843 StructType *ST = cast<StructType>(II->getType());
845 return InsertValueInst::Create(Struct, Result, 0);
846}
847
849InstCombinerImpl::foldIntrinsicWithOverflowCommon(IntrinsicInst *II) {
850 WithOverflowInst *WO = cast<WithOverflowInst>(II);
851 Value *OperationResult = nullptr;
852 Constant *OverflowResult = nullptr;
853 if (OptimizeOverflowCheck(WO->getBinaryOp(), WO->isSigned(), WO->getLHS(),
854 WO->getRHS(), *WO, OperationResult, OverflowResult))
855 return createOverflowTuple(WO, OperationResult, OverflowResult);
856
857 // See whether we can optimize the overflow check with assumption information.
858 for (User *U : WO->users()) {
859 if (!match(U, m_ExtractValue<1>(m_Value())))
860 continue;
861
862 for (auto &AssumeVH : AC.assumptionsFor(U)) {
863 if (!AssumeVH)
864 continue;
865 CallInst *I = cast<CallInst>(AssumeVH);
866 if (!match(I->getArgOperand(0), m_Not(m_Specific(U))))
867 continue;
868 if (!isValidAssumeForContext(I, II, /*DT=*/nullptr,
869 /*AllowEphemerals=*/true))
870 continue;
871 Value *Result =
872 Builder.CreateBinOp(WO->getBinaryOp(), WO->getLHS(), WO->getRHS());
873 Result->takeName(WO);
874 if (auto *Inst = dyn_cast<Instruction>(Result)) {
875 if (WO->isSigned())
876 Inst->setHasNoSignedWrap();
877 else
878 Inst->setHasNoUnsignedWrap();
879 }
880 return createOverflowTuple(WO, Result,
881 ConstantInt::getFalse(U->getType()));
882 }
883 }
884
885 return nullptr;
886}
887
888static bool inputDenormalIsIEEE(const Function &F, const Type *Ty) {
889 Ty = Ty->getScalarType();
890 return F.getDenormalMode(Ty->getFltSemantics()).Input == DenormalMode::IEEE;
891}
892
893static bool inputDenormalIsDAZ(const Function &F, const Type *Ty) {
894 Ty = Ty->getScalarType();
895 return F.getDenormalMode(Ty->getFltSemantics()).inputsAreZero();
896}
897
898/// \returns the compare predicate type if the test performed by
899/// llvm.is.fpclass(x, \p Mask) is equivalent to fcmp o__ x, 0.0 with the
900/// floating-point environment assumed for \p F for type \p Ty
902 const Function &F, Type *Ty) {
903 switch (static_cast<unsigned>(Mask)) {
904 case fcZero:
905 if (inputDenormalIsIEEE(F, Ty))
906 return FCmpInst::FCMP_OEQ;
907 break;
908 case fcZero | fcSubnormal:
909 if (inputDenormalIsDAZ(F, Ty))
910 return FCmpInst::FCMP_OEQ;
911 break;
912 case fcPositive | fcNegZero:
913 if (inputDenormalIsIEEE(F, Ty))
914 return FCmpInst::FCMP_OGE;
915 break;
917 if (inputDenormalIsDAZ(F, Ty))
918 return FCmpInst::FCMP_OGE;
919 break;
921 if (inputDenormalIsIEEE(F, Ty))
922 return FCmpInst::FCMP_OGT;
923 break;
924 case fcNegative | fcPosZero:
925 if (inputDenormalIsIEEE(F, Ty))
926 return FCmpInst::FCMP_OLE;
927 break;
929 if (inputDenormalIsDAZ(F, Ty))
930 return FCmpInst::FCMP_OLE;
931 break;
933 if (inputDenormalIsIEEE(F, Ty))
934 return FCmpInst::FCMP_OLT;
935 break;
936 case fcPosNormal | fcPosInf:
937 if (inputDenormalIsDAZ(F, Ty))
938 return FCmpInst::FCMP_OGT;
939 break;
940 case fcNegNormal | fcNegInf:
941 if (inputDenormalIsDAZ(F, Ty))
942 return FCmpInst::FCMP_OLT;
943 break;
944 case ~fcZero & ~fcNan:
945 if (inputDenormalIsIEEE(F, Ty))
946 return FCmpInst::FCMP_ONE;
947 break;
948 case ~(fcZero | fcSubnormal) & ~fcNan:
949 if (inputDenormalIsDAZ(F, Ty))
950 return FCmpInst::FCMP_ONE;
951 break;
952 default:
953 break;
954 }
955
957}
958
959Instruction *InstCombinerImpl::foldIntrinsicIsFPClass(IntrinsicInst &II) {
960 Value *Src0 = II.getArgOperand(0);
961 Value *Src1 = II.getArgOperand(1);
962 const ConstantInt *CMask = cast<ConstantInt>(Src1);
963 FPClassTest Mask = static_cast<FPClassTest>(CMask->getZExtValue());
964 const bool IsUnordered = (Mask & fcNan) == fcNan;
965 const bool IsOrdered = (Mask & fcNan) == fcNone;
966 const FPClassTest OrderedMask = Mask & ~fcNan;
967 const FPClassTest OrderedInvertedMask = ~OrderedMask & ~fcNan;
968
969 const bool IsStrict =
970 II.getFunction()->getAttributes().hasFnAttr(Attribute::StrictFP);
971
972 Value *FNegSrc;
973 if (match(Src0, m_FNeg(m_Value(FNegSrc)))) {
974 // is.fpclass (fneg x), mask -> is.fpclass x, (fneg mask)
975
976 II.setArgOperand(1, ConstantInt::get(Src1->getType(), fneg(Mask)));
977 return replaceOperand(II, 0, FNegSrc);
978 }
979
980 Value *FAbsSrc;
981 if (match(Src0, m_FAbs(m_Value(FAbsSrc)))) {
982 II.setArgOperand(1, ConstantInt::get(Src1->getType(), inverse_fabs(Mask)));
983 return replaceOperand(II, 0, FAbsSrc);
984 }
985
986 if ((OrderedMask == fcInf || OrderedInvertedMask == fcInf) &&
987 (IsOrdered || IsUnordered) && !IsStrict) {
988 // is.fpclass(x, fcInf) -> fcmp oeq fabs(x), +inf
989 // is.fpclass(x, ~fcInf) -> fcmp one fabs(x), +inf
990 // is.fpclass(x, fcInf|fcNan) -> fcmp ueq fabs(x), +inf
991 // is.fpclass(x, ~(fcInf|fcNan)) -> fcmp une fabs(x), +inf
995 if (OrderedInvertedMask == fcInf)
996 Pred = IsUnordered ? FCmpInst::FCMP_UNE : FCmpInst::FCMP_ONE;
997
998 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, Src0);
999 Value *CmpInf = Builder.CreateFCmp(Pred, Fabs, Inf);
1000 CmpInf->takeName(&II);
1001 return replaceInstUsesWith(II, CmpInf);
1002 }
1003
1004 if ((OrderedMask == fcPosInf || OrderedMask == fcNegInf) &&
1005 (IsOrdered || IsUnordered) && !IsStrict) {
1006 // is.fpclass(x, fcPosInf) -> fcmp oeq x, +inf
1007 // is.fpclass(x, fcNegInf) -> fcmp oeq x, -inf
1008 // is.fpclass(x, fcPosInf|fcNan) -> fcmp ueq x, +inf
1009 // is.fpclass(x, fcNegInf|fcNan) -> fcmp ueq x, -inf
1010 Constant *Inf =
1011 ConstantFP::getInfinity(Src0->getType(), OrderedMask == fcNegInf);
1012 Value *EqInf = IsUnordered ? Builder.CreateFCmpUEQ(Src0, Inf)
1013 : Builder.CreateFCmpOEQ(Src0, Inf);
1014
1015 EqInf->takeName(&II);
1016 return replaceInstUsesWith(II, EqInf);
1017 }
1018
1019 if ((OrderedInvertedMask == fcPosInf || OrderedInvertedMask == fcNegInf) &&
1020 (IsOrdered || IsUnordered) && !IsStrict) {
1021 // is.fpclass(x, ~fcPosInf) -> fcmp one x, +inf
1022 // is.fpclass(x, ~fcNegInf) -> fcmp one x, -inf
1023 // is.fpclass(x, ~fcPosInf|fcNan) -> fcmp une x, +inf
1024 // is.fpclass(x, ~fcNegInf|fcNan) -> fcmp une x, -inf
1026 OrderedInvertedMask == fcNegInf);
1027 Value *NeInf = IsUnordered ? Builder.CreateFCmpUNE(Src0, Inf)
1028 : Builder.CreateFCmpONE(Src0, Inf);
1029 NeInf->takeName(&II);
1030 return replaceInstUsesWith(II, NeInf);
1031 }
1032
1033 if (Mask == fcNan && !IsStrict) {
1034 // Equivalent of isnan. Replace with standard fcmp if we don't care about FP
1035 // exceptions.
1036 Value *IsNan =
1038 IsNan->takeName(&II);
1039 return replaceInstUsesWith(II, IsNan);
1040 }
1041
1042 if (Mask == (~fcNan & fcAllFlags) && !IsStrict) {
1043 // Equivalent of !isnan. Replace with standard fcmp.
1044 Value *FCmp =
1046 FCmp->takeName(&II);
1047 return replaceInstUsesWith(II, FCmp);
1048 }
1049
1051
1052 // Try to replace with an fcmp with 0
1053 //
1054 // is.fpclass(x, fcZero) -> fcmp oeq x, 0.0
1055 // is.fpclass(x, fcZero | fcNan) -> fcmp ueq x, 0.0
1056 // is.fpclass(x, ~fcZero & ~fcNan) -> fcmp one x, 0.0
1057 // is.fpclass(x, ~fcZero) -> fcmp une x, 0.0
1058 //
1059 // is.fpclass(x, fcPosSubnormal | fcPosNormal | fcPosInf) -> fcmp ogt x, 0.0
1060 // is.fpclass(x, fcPositive | fcNegZero) -> fcmp oge x, 0.0
1061 //
1062 // is.fpclass(x, fcNegSubnormal | fcNegNormal | fcNegInf) -> fcmp olt x, 0.0
1063 // is.fpclass(x, fcNegative | fcPosZero) -> fcmp ole x, 0.0
1064 //
1065 if (!IsStrict && (IsOrdered || IsUnordered) &&
1066 (PredType = fpclassTestIsFCmp0(OrderedMask, *II.getFunction(),
1067 Src0->getType())) !=
1070 // Equivalent of == 0.
1071 Value *FCmp = Builder.CreateFCmp(
1072 IsUnordered ? FCmpInst::getUnorderedPredicate(PredType) : PredType,
1073 Src0, Zero);
1074
1075 FCmp->takeName(&II);
1076 return replaceInstUsesWith(II, FCmp);
1077 }
1078
1079 KnownFPClass Known = computeKnownFPClass(Src0, Mask, &II);
1080
1081 // Clear test bits we know must be false from the source value.
1082 // fp_class (nnan x), qnan|snan|other -> fp_class (nnan x), other
1083 // fp_class (ninf x), ninf|pinf|other -> fp_class (ninf x), other
1084 if ((Mask & Known.KnownFPClasses) != Mask) {
1085 II.setArgOperand(
1086 1, ConstantInt::get(Src1->getType(), Mask & Known.KnownFPClasses));
1087 return &II;
1088 }
1089
1090 // If none of the tests which can return false are possible, fold to true.
1091 // fp_class (nnan x), ~(qnan|snan) -> true
1092 // fp_class (ninf x), ~(ninf|pinf) -> true
1093 if (Mask == Known.KnownFPClasses)
1094 return replaceInstUsesWith(II, ConstantInt::get(II.getType(), true));
1095
1096 return nullptr;
1097}
1098
1099static std::optional<bool> getKnownSign(Value *Op, const SimplifyQuery &SQ) {
1100 KnownBits Known = computeKnownBits(Op, SQ);
1101 if (Known.isNonNegative())
1102 return false;
1103 if (Known.isNegative())
1104 return true;
1105
1106 Value *X, *Y;
1107 if (match(Op, m_NSWSub(m_Value(X), m_Value(Y))))
1109
1110 return std::nullopt;
1111}
1112
1113static std::optional<bool> getKnownSignOrZero(Value *Op,
1114 const SimplifyQuery &SQ) {
1115 if (std::optional<bool> Sign = getKnownSign(Op, SQ))
1116 return Sign;
1117
1118 Value *X, *Y;
1119 if (match(Op, m_NSWSub(m_Value(X), m_Value(Y))))
1121
1122 return std::nullopt;
1123}
1124
1125/// Return true if two values \p Op0 and \p Op1 are known to have the same sign.
1126static bool signBitMustBeTheSame(Value *Op0, Value *Op1,
1127 const SimplifyQuery &SQ) {
1128 std::optional<bool> Known1 = getKnownSign(Op1, SQ);
1129 if (!Known1)
1130 return false;
1131 std::optional<bool> Known0 = getKnownSign(Op0, SQ);
1132 if (!Known0)
1133 return false;
1134 return *Known0 == *Known1;
1135}
1136
1137/// Try to canonicalize min/max(X + C0, C1) as min/max(X, C1 - C0) + C0. This
1138/// can trigger other combines.
1140 InstCombiner::BuilderTy &Builder) {
1141 Intrinsic::ID MinMaxID = II->getIntrinsicID();
1142 assert((MinMaxID == Intrinsic::smax || MinMaxID == Intrinsic::smin ||
1143 MinMaxID == Intrinsic::umax || MinMaxID == Intrinsic::umin) &&
1144 "Expected a min or max intrinsic");
1145
1146 // TODO: Match vectors with undef elements, but undef may not propagate.
1147 Value *Op0 = II->getArgOperand(0), *Op1 = II->getArgOperand(1);
1148 Value *X;
1149 const APInt *C0, *C1;
1150 if (!match(Op0, m_OneUse(m_Add(m_Value(X), m_APInt(C0)))) ||
1151 !match(Op1, m_APInt(C1)))
1152 return nullptr;
1153
1154 // Check for necessary no-wrap and overflow constraints.
1155 bool IsSigned = MinMaxID == Intrinsic::smax || MinMaxID == Intrinsic::smin;
1156 auto *Add = cast<BinaryOperator>(Op0);
1157 if ((IsSigned && !Add->hasNoSignedWrap()) ||
1158 (!IsSigned && !Add->hasNoUnsignedWrap()))
1159 return nullptr;
1160
1161 // If the constant difference overflows, then instsimplify should reduce the
1162 // min/max to the add or C1.
1163 bool Overflow;
1164 APInt CDiff =
1165 IsSigned ? C1->ssub_ov(*C0, Overflow) : C1->usub_ov(*C0, Overflow);
1166 assert(!Overflow && "Expected simplify of min/max");
1167
1168 // min/max (add X, C0), C1 --> add (min/max X, C1 - C0), C0
1169 // Note: the "mismatched" no-overflow setting does not propagate.
1170 Constant *NewMinMaxC = ConstantInt::get(II->getType(), CDiff);
1171 Value *NewMinMax = Builder.CreateBinaryIntrinsic(MinMaxID, X, NewMinMaxC);
1172 return IsSigned ? BinaryOperator::CreateNSWAdd(NewMinMax, Add->getOperand(1))
1173 : BinaryOperator::CreateNUWAdd(NewMinMax, Add->getOperand(1));
1174}
1175/// Match a sadd_sat or ssub_sat which is using min/max to clamp the value.
1176Instruction *InstCombinerImpl::matchSAddSubSat(IntrinsicInst &MinMax1) {
1177 Type *Ty = MinMax1.getType();
1178
1179 // We are looking for a tree of:
1180 // max(INT_MIN, min(INT_MAX, add(sext(A), sext(B))))
1181 // Where the min and max could be reversed
1182 Instruction *MinMax2;
1184 const APInt *MinValue, *MaxValue;
1185 if (match(&MinMax1, m_SMin(m_Instruction(MinMax2), m_APInt(MaxValue)))) {
1186 if (!match(MinMax2, m_SMax(m_BinOp(AddSub), m_APInt(MinValue))))
1187 return nullptr;
1188 } else if (match(&MinMax1,
1189 m_SMax(m_Instruction(MinMax2), m_APInt(MinValue)))) {
1190 if (!match(MinMax2, m_SMin(m_BinOp(AddSub), m_APInt(MaxValue))))
1191 return nullptr;
1192 } else
1193 return nullptr;
1194
1195 // Check that the constants clamp a saturate, and that the new type would be
1196 // sensible to convert to.
1197 if (!(*MaxValue + 1).isPowerOf2() || -*MinValue != *MaxValue + 1)
1198 return nullptr;
1199 // In what bitwidth can this be treated as saturating arithmetics?
1200 unsigned NewBitWidth = (*MaxValue + 1).logBase2() + 1;
1201 // FIXME: This isn't quite right for vectors, but using the scalar type is a
1202 // good first approximation for what should be done there.
1203 if (!shouldChangeType(Ty->getScalarType()->getIntegerBitWidth(), NewBitWidth))
1204 return nullptr;
1205
1206 // Also make sure that the inner min/max and the add/sub have one use.
1207 if (!MinMax2->hasOneUse() || !AddSub->hasOneUse())
1208 return nullptr;
1209
1210 // Create the new type (which can be a vector type)
1211 Type *NewTy = Ty->getWithNewBitWidth(NewBitWidth);
1212
1213 Intrinsic::ID IntrinsicID;
1214 if (AddSub->getOpcode() == Instruction::Add)
1215 IntrinsicID = Intrinsic::sadd_sat;
1216 else if (AddSub->getOpcode() == Instruction::Sub)
1217 IntrinsicID = Intrinsic::ssub_sat;
1218 else
1219 return nullptr;
1220
1221 // The two operands of the add/sub must be nsw-truncatable to the NewTy. This
1222 // is usually achieved via a sext from a smaller type.
1223 if (ComputeMaxSignificantBits(AddSub->getOperand(0), AddSub) > NewBitWidth ||
1224 ComputeMaxSignificantBits(AddSub->getOperand(1), AddSub) > NewBitWidth)
1225 return nullptr;
1226
1227 // Finally create and return the sat intrinsic, truncated to the new type
1228 Value *AT = Builder.CreateTrunc(AddSub->getOperand(0), NewTy);
1229 Value *BT = Builder.CreateTrunc(AddSub->getOperand(1), NewTy);
1230 Value *Sat = Builder.CreateIntrinsic(IntrinsicID, NewTy, {AT, BT});
1231 return CastInst::Create(Instruction::SExt, Sat, Ty);
1232}
1233
1234
1235/// If we have a clamp pattern like max (min X, 42), 41 -- where the output
1236/// can only be one of two possible constant values -- turn that into a select
1237/// of constants.
1239 InstCombiner::BuilderTy &Builder) {
1240 Value *I0 = II->getArgOperand(0), *I1 = II->getArgOperand(1);
1241 Value *X;
1242 const APInt *C0, *C1;
1243 if (!match(I1, m_APInt(C1)) || !I0->hasOneUse())
1244 return nullptr;
1245
1247 switch (II->getIntrinsicID()) {
1248 case Intrinsic::smax:
1249 if (match(I0, m_SMin(m_Value(X), m_APInt(C0))) && *C0 == *C1 + 1)
1250 Pred = ICmpInst::ICMP_SGT;
1251 break;
1252 case Intrinsic::smin:
1253 if (match(I0, m_SMax(m_Value(X), m_APInt(C0))) && *C1 == *C0 + 1)
1254 Pred = ICmpInst::ICMP_SLT;
1255 break;
1256 case Intrinsic::umax:
1257 if (match(I0, m_UMin(m_Value(X), m_APInt(C0))) && *C0 == *C1 + 1)
1258 Pred = ICmpInst::ICMP_UGT;
1259 break;
1260 case Intrinsic::umin:
1261 if (match(I0, m_UMax(m_Value(X), m_APInt(C0))) && *C1 == *C0 + 1)
1262 Pred = ICmpInst::ICMP_ULT;
1263 break;
1264 default:
1265 llvm_unreachable("Expected min/max intrinsic");
1266 }
1267 if (Pred == CmpInst::BAD_ICMP_PREDICATE)
1268 return nullptr;
1269
1270 // max (min X, 42), 41 --> X > 41 ? 42 : 41
1271 // min (max X, 42), 43 --> X < 43 ? 42 : 43
1272 Value *Cmp = Builder.CreateICmp(Pred, X, I1);
1273 return SelectInst::Create(Cmp, ConstantInt::get(II->getType(), *C0), I1);
1274}
1275
1276/// If this min/max has a constant operand and an operand that is a matching
1277/// min/max with a constant operand, constant-fold the 2 constant operands.
1279 IRBuilderBase &Builder,
1280 const SimplifyQuery &SQ) {
1281 Intrinsic::ID MinMaxID = II->getIntrinsicID();
1282 auto *LHS = dyn_cast<MinMaxIntrinsic>(II->getArgOperand(0));
1283 if (!LHS)
1284 return nullptr;
1285
1286 Constant *C0, *C1;
1287 if (!match(LHS->getArgOperand(1), m_ImmConstant(C0)) ||
1288 !match(II->getArgOperand(1), m_ImmConstant(C1)))
1289 return nullptr;
1290
1291 // max (max X, C0), C1 --> max X, (max C0, C1)
1292 // min (min X, C0), C1 --> min X, (min C0, C1)
1293 // umax (smax X, nneg C0), nneg C1 --> smax X, (umax C0, C1)
1294 // smin (umin X, nneg C0), nneg C1 --> umin X, (smin C0, C1)
1295 Intrinsic::ID InnerMinMaxID = LHS->getIntrinsicID();
1296 if (InnerMinMaxID != MinMaxID &&
1297 !(((MinMaxID == Intrinsic::umax && InnerMinMaxID == Intrinsic::smax) ||
1298 (MinMaxID == Intrinsic::smin && InnerMinMaxID == Intrinsic::umin)) &&
1299 isKnownNonNegative(C0, SQ) && isKnownNonNegative(C1, SQ)))
1300 return nullptr;
1301
1303 Value *CondC = Builder.CreateICmp(Pred, C0, C1);
1304 Value *NewC = Builder.CreateSelect(CondC, C0, C1);
1305 return Builder.CreateIntrinsic(InnerMinMaxID, II->getType(),
1306 {LHS->getArgOperand(0), NewC});
1307}
1308
1309/// If this min/max has a matching min/max operand with a constant, try to push
1310/// the constant operand into this instruction. This can enable more folds.
1311static Instruction *
1313 InstCombiner::BuilderTy &Builder) {
1314 // Match and capture a min/max operand candidate.
1315 Value *X, *Y;
1316 Constant *C;
1317 Instruction *Inner;
1319 m_Instruction(Inner),
1321 m_Value(Y))))
1322 return nullptr;
1323
1324 // The inner op must match. Check for constants to avoid infinite loops.
1325 Intrinsic::ID MinMaxID = II->getIntrinsicID();
1326 auto *InnerMM = dyn_cast<IntrinsicInst>(Inner);
1327 if (!InnerMM || InnerMM->getIntrinsicID() != MinMaxID ||
1329 return nullptr;
1330
1331 // max (max X, C), Y --> max (max X, Y), C
1333 MinMaxID, II->getType());
1334 Value *NewInner = Builder.CreateBinaryIntrinsic(MinMaxID, X, Y);
1335 NewInner->takeName(Inner);
1336 return CallInst::Create(MinMax, {NewInner, C});
1337}
1338
1339/// Reduce a sequence of min/max intrinsics with a common operand.
1341 // Match 3 of the same min/max ops. Example: umin(umin(), umin()).
1342 auto *LHS = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1343 auto *RHS = dyn_cast<IntrinsicInst>(II->getArgOperand(1));
1344 Intrinsic::ID MinMaxID = II->getIntrinsicID();
1345 if (!LHS || !RHS || LHS->getIntrinsicID() != MinMaxID ||
1346 RHS->getIntrinsicID() != MinMaxID ||
1347 (!LHS->hasOneUse() && !RHS->hasOneUse()))
1348 return nullptr;
1349
1350 Value *A = LHS->getArgOperand(0);
1351 Value *B = LHS->getArgOperand(1);
1352 Value *C = RHS->getArgOperand(0);
1353 Value *D = RHS->getArgOperand(1);
1354
1355 // Look for a common operand.
1356 Value *MinMaxOp = nullptr;
1357 Value *ThirdOp = nullptr;
1358 if (LHS->hasOneUse()) {
1359 // If the LHS is only used in this chain and the RHS is used outside of it,
1360 // reuse the RHS min/max because that will eliminate the LHS.
1361 if (D == A || C == A) {
1362 // min(min(a, b), min(c, a)) --> min(min(c, a), b)
1363 // min(min(a, b), min(a, d)) --> min(min(a, d), b)
1364 MinMaxOp = RHS;
1365 ThirdOp = B;
1366 } else if (D == B || C == B) {
1367 // min(min(a, b), min(c, b)) --> min(min(c, b), a)
1368 // min(min(a, b), min(b, d)) --> min(min(b, d), a)
1369 MinMaxOp = RHS;
1370 ThirdOp = A;
1371 }
1372 } else {
1373 assert(RHS->hasOneUse() && "Expected one-use operand");
1374 // Reuse the LHS. This will eliminate the RHS.
1375 if (D == A || D == B) {
1376 // min(min(a, b), min(c, a)) --> min(min(a, b), c)
1377 // min(min(a, b), min(c, b)) --> min(min(a, b), c)
1378 MinMaxOp = LHS;
1379 ThirdOp = C;
1380 } else if (C == A || C == B) {
1381 // min(min(a, b), min(b, d)) --> min(min(a, b), d)
1382 // min(min(a, b), min(c, b)) --> min(min(a, b), d)
1383 MinMaxOp = LHS;
1384 ThirdOp = D;
1385 }
1386 }
1387
1388 if (!MinMaxOp || !ThirdOp)
1389 return nullptr;
1390
1391 Module *Mod = II->getModule();
1392 Function *MinMax =
1393 Intrinsic::getOrInsertDeclaration(Mod, MinMaxID, II->getType());
1394 return CallInst::Create(MinMax, { MinMaxOp, ThirdOp });
1395}
1396
1397/// If all arguments of the intrinsic are unary shuffles with the same mask,
1398/// try to shuffle after the intrinsic.
1401 if (!isTriviallyVectorizable(II->getIntrinsicID()) ||
1402 !II->getCalledFunction()->isSpeculatable())
1403 return nullptr;
1404
1405 Value *X;
1406 Constant *C;
1407 ArrayRef<int> Mask;
1408 auto *NonConstArg = find_if_not(II->args(), [&II](Use &Arg) {
1409 return isa<Constant>(Arg.get()) ||
1410 isVectorIntrinsicWithScalarOpAtArg(II->getIntrinsicID(),
1411 Arg.getOperandNo(), nullptr);
1412 });
1413 if (!NonConstArg ||
1414 !match(NonConstArg, m_Shuffle(m_Value(X), m_Poison(), m_Mask(Mask))))
1415 return nullptr;
1416
1417 // At least 1 operand must be a shuffle with 1 use because we are creating 2
1418 // instructions.
1419 if (none_of(II->args(), [](Value *V) {
1420 return isa<ShuffleVectorInst>(V) && V->hasOneUse();
1421 }))
1422 return nullptr;
1423
1424 // See if all arguments are shuffled with the same mask.
1426 Type *SrcTy = X->getType();
1427 for (Use &Arg : II->args()) {
1428 if (isVectorIntrinsicWithScalarOpAtArg(II->getIntrinsicID(),
1429 Arg.getOperandNo(), nullptr))
1430 NewArgs.push_back(Arg);
1431 else if (match(&Arg,
1432 m_Shuffle(m_Value(X), m_Poison(), m_SpecificMask(Mask))) &&
1433 X->getType() == SrcTy)
1434 NewArgs.push_back(X);
1435 else if (match(&Arg, m_ImmConstant(C))) {
1436 // If it's a constant, try find the constant that would be shuffled to C.
1437 if (Constant *ShuffledC =
1438 unshuffleConstant(Mask, C, cast<VectorType>(SrcTy)))
1439 NewArgs.push_back(ShuffledC);
1440 else
1441 return nullptr;
1442 } else
1443 return nullptr;
1444 }
1445
1446 // intrinsic (shuf X, M), (shuf Y, M), ... --> shuf (intrinsic X, Y, ...), M
1447 Instruction *FPI = isa<FPMathOperator>(II) ? II : nullptr;
1448 // Result type might be a different vector width.
1449 // TODO: Check that the result type isn't widened?
1450 VectorType *ResTy =
1451 VectorType::get(II->getType()->getScalarType(), cast<VectorType>(SrcTy));
1452 Value *NewIntrinsic =
1453 Builder.CreateIntrinsic(ResTy, II->getIntrinsicID(), NewArgs, FPI);
1454 return new ShuffleVectorInst(NewIntrinsic, Mask);
1455}
1456
1457/// If all arguments of the intrinsic are reverses, try to pull the reverse
1458/// after the intrinsic.
1460 if (!isTriviallyVectorizable(II->getIntrinsicID()))
1461 return nullptr;
1462
1463 // At least 1 operand must be a reverse with 1 use because we are creating 2
1464 // instructions.
1465 if (none_of(II->args(), [](Value *V) {
1466 return match(V, m_OneUse(m_VecReverse(m_Value())));
1467 }))
1468 return nullptr;
1469
1470 Value *X;
1471 Constant *C;
1472 SmallVector<Value *> NewArgs;
1473 for (Use &Arg : II->args()) {
1474 if (isVectorIntrinsicWithScalarOpAtArg(II->getIntrinsicID(),
1475 Arg.getOperandNo(), nullptr))
1476 NewArgs.push_back(Arg);
1477 else if (match(&Arg, m_VecReverse(m_Value(X))))
1478 NewArgs.push_back(X);
1479 else if (isSplatValue(Arg))
1480 NewArgs.push_back(Arg);
1481 else if (match(&Arg, m_ImmConstant(C)))
1483 else
1484 return nullptr;
1485 }
1486
1487 // intrinsic (reverse X), (reverse Y), ... --> reverse (intrinsic X, Y, ...)
1488 Instruction *FPI = isa<FPMathOperator>(II) ? II : nullptr;
1489 Instruction *NewIntrinsic = Builder.CreateIntrinsic(
1490 II->getType(), II->getIntrinsicID(), NewArgs, FPI);
1491 return Builder.CreateVectorReverse(NewIntrinsic);
1492}
1493
1494/// Fold the following cases and accepts bswap and bitreverse intrinsics:
1495/// bswap(logic_op(bswap(x), y)) --> logic_op(x, bswap(y))
1496/// bswap(logic_op(bswap(x), bswap(y))) --> logic_op(x, y) (ignores multiuse)
1497template <Intrinsic::ID IntrID>
1499 InstCombiner::BuilderTy &Builder) {
1500 static_assert(IntrID == Intrinsic::bswap || IntrID == Intrinsic::bitreverse,
1501 "This helper only supports BSWAP and BITREVERSE intrinsics");
1502
1503 Value *X, *Y;
1504 // Find bitwise logic op. Check that it is a BinaryOperator explicitly so we
1505 // don't match ConstantExpr that aren't meaningful for this transform.
1507 isa<BinaryOperator>(V)) {
1508 Value *OldReorderX, *OldReorderY;
1509 BinaryOperator::BinaryOps Op = cast<BinaryOperator>(V)->getOpcode();
1510
1511 // If both X and Y are bswap/bitreverse, the transform reduces the number
1512 // of instructions even if there's multiuse.
1513 // If only one operand is bswap/bitreverse, we need to ensure the operand
1514 // have only one use.
1515 if (match(X, m_Intrinsic<IntrID>(m_Value(OldReorderX))) &&
1516 match(Y, m_Intrinsic<IntrID>(m_Value(OldReorderY)))) {
1517 return BinaryOperator::Create(Op, OldReorderX, OldReorderY);
1518 }
1519
1520 if (match(X, m_OneUse(m_Intrinsic<IntrID>(m_Value(OldReorderX))))) {
1521 Value *NewReorder = Builder.CreateUnaryIntrinsic(IntrID, Y);
1522 return BinaryOperator::Create(Op, OldReorderX, NewReorder);
1523 }
1524
1525 if (match(Y, m_OneUse(m_Intrinsic<IntrID>(m_Value(OldReorderY))))) {
1526 Value *NewReorder = Builder.CreateUnaryIntrinsic(IntrID, X);
1527 return BinaryOperator::Create(Op, NewReorder, OldReorderY);
1528 }
1529 }
1530 return nullptr;
1531}
1532
1533/// Helper to match idempotent binary intrinsics, namely, intrinsics where
1534/// `f(f(x, y), y) == f(x, y)` holds.
1536 switch (IID) {
1537 case Intrinsic::smax:
1538 case Intrinsic::smin:
1539 case Intrinsic::umax:
1540 case Intrinsic::umin:
1541 case Intrinsic::maximum:
1542 case Intrinsic::minimum:
1543 case Intrinsic::maximumnum:
1544 case Intrinsic::minimumnum:
1545 case Intrinsic::maxnum:
1546 case Intrinsic::minnum:
1547 return true;
1548 default:
1549 return false;
1550 }
1551}
1552
1553/// Attempt to simplify value-accumulating recurrences of kind:
1554/// %umax.acc = phi i8 [ %umax, %backedge ], [ %a, %entry ]
1555/// %umax = call i8 @llvm.umax.i8(i8 %umax.acc, i8 %b)
1556/// And let the idempotent binary intrinsic be hoisted, when the operands are
1557/// known to be loop-invariant.
1559 IntrinsicInst *II) {
1560 PHINode *PN;
1561 Value *Init, *OtherOp;
1562
1563 // A binary intrinsic recurrence with loop-invariant operands is equivalent to
1564 // `call @llvm.binary.intrinsic(Init, OtherOp)`.
1565 auto IID = II->getIntrinsicID();
1566 if (!isIdempotentBinaryIntrinsic(IID) ||
1568 !IC.getDominatorTree().dominates(OtherOp, PN))
1569 return nullptr;
1570
1571 auto *InvariantBinaryInst =
1572 IC.Builder.CreateBinaryIntrinsic(IID, Init, OtherOp);
1573 if (isa<FPMathOperator>(InvariantBinaryInst))
1574 cast<Instruction>(InvariantBinaryInst)->copyFastMathFlags(II);
1575 return InvariantBinaryInst;
1576}
1577
1578static Value *simplifyReductionOperand(Value *Arg, bool CanReorderLanes) {
1579 if (!CanReorderLanes)
1580 return nullptr;
1581
1582 Value *V;
1583 if (match(Arg, m_VecReverse(m_Value(V))))
1584 return V;
1585
1586 ArrayRef<int> Mask;
1587 if (!isa<FixedVectorType>(Arg->getType()) ||
1588 !match(Arg, m_Shuffle(m_Value(V), m_Undef(), m_Mask(Mask))) ||
1589 !cast<ShuffleVectorInst>(Arg)->isSingleSource())
1590 return nullptr;
1591
1592 int Sz = Mask.size();
1593 SmallBitVector UsedIndices(Sz);
1594 for (int Idx : Mask) {
1595 if (Idx == PoisonMaskElem || UsedIndices.test(Idx))
1596 return nullptr;
1597 UsedIndices.set(Idx);
1598 }
1599
1600 // Can remove shuffle iff just shuffled elements, no repeats, undefs, or
1601 // other changes.
1602 return UsedIndices.all() ? V : nullptr;
1603}
1604
1605/// Fold an unsigned minimum of trailing or leading zero bits counts:
1606/// umin(cttz(CtOp, ZeroUndef), ConstOp) --> cttz(CtOp | (1 << ConstOp))
1607/// umin(ctlz(CtOp, ZeroUndef), ConstOp) --> ctlz(CtOp | (SignedMin
1608/// >> ConstOp))
1609template <Intrinsic::ID IntrID>
1610static Value *
1612 const DataLayout &DL,
1613 InstCombiner::BuilderTy &Builder) {
1614 static_assert(IntrID == Intrinsic::cttz || IntrID == Intrinsic::ctlz,
1615 "This helper only supports cttz and ctlz intrinsics");
1616
1617 Value *CtOp;
1618 Value *ZeroUndef;
1619 if (!match(I0,
1620 m_OneUse(m_Intrinsic<IntrID>(m_Value(CtOp), m_Value(ZeroUndef)))))
1621 return nullptr;
1622
1623 unsigned BitWidth = I1->getType()->getScalarSizeInBits();
1624 auto LessBitWidth = [BitWidth](auto &C) { return C.ult(BitWidth); };
1625 if (!match(I1, m_CheckedInt(LessBitWidth)))
1626 // We have a constant >= BitWidth (which can be handled by CVP)
1627 // or a non-splat vector with elements < and >= BitWidth
1628 return nullptr;
1629
1630 Type *Ty = I1->getType();
1632 IntrID == Intrinsic::cttz ? Instruction::Shl : Instruction::LShr,
1633 IntrID == Intrinsic::cttz
1634 ? ConstantInt::get(Ty, 1)
1635 : ConstantInt::get(Ty, APInt::getSignedMinValue(BitWidth)),
1636 cast<Constant>(I1), DL);
1637 return Builder.CreateBinaryIntrinsic(
1638 IntrID, Builder.CreateOr(CtOp, NewConst),
1639 ConstantInt::getTrue(ZeroUndef->getType()));
1640}
1641
1642/// Return whether "X LOp (Y ROp Z)" is always equal to
1643/// "(X LOp Y) ROp (X LOp Z)".
1645 bool HasNSW, Intrinsic::ID ROp) {
1646 switch (ROp) {
1647 case Intrinsic::umax:
1648 case Intrinsic::umin:
1649 if (HasNUW && LOp == Instruction::Add)
1650 return true;
1651 if (HasNUW && LOp == Instruction::Shl)
1652 return true;
1653 return false;
1654 case Intrinsic::smax:
1655 case Intrinsic::smin:
1656 return HasNSW && LOp == Instruction::Add;
1657 default:
1658 return false;
1659 }
1660}
1661
1662// Attempts to factorise a common term
1663// in an instruction that has the form "(A op' B) op (C op' D)
1664// where op is an intrinsic and op' is a binop
1665static Value *
1667 InstCombiner::BuilderTy &Builder) {
1668 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
1669 Intrinsic::ID TopLevelOpcode = II->getIntrinsicID();
1670
1671 OverflowingBinaryOperator *Op0 = dyn_cast<OverflowingBinaryOperator>(LHS);
1672 OverflowingBinaryOperator *Op1 = dyn_cast<OverflowingBinaryOperator>(RHS);
1673
1674 if (!Op0 || !Op1)
1675 return nullptr;
1676
1677 if (Op0->getOpcode() != Op1->getOpcode())
1678 return nullptr;
1679
1680 if (!Op0->hasOneUse() || !Op1->hasOneUse())
1681 return nullptr;
1682
1683 Instruction::BinaryOps InnerOpcode =
1684 static_cast<Instruction::BinaryOps>(Op0->getOpcode());
1685 bool HasNUW = Op0->hasNoUnsignedWrap() && Op1->hasNoUnsignedWrap();
1686 bool HasNSW = Op0->hasNoSignedWrap() && Op1->hasNoSignedWrap();
1687
1688 if (!leftDistributesOverRight(InnerOpcode, HasNUW, HasNSW, TopLevelOpcode))
1689 return nullptr;
1690
1691 Value *A = Op0->getOperand(0);
1692 Value *B = Op0->getOperand(1);
1693 Value *C = Op1->getOperand(0);
1694 Value *D = Op1->getOperand(1);
1695
1696 // Attempts to swap variables such that A equals C or B equals D,
1697 // if the inner operation is commutative.
1698 if (Op0->isCommutative() && A != C && B != D) {
1699 if (A == D || B == C)
1700 std::swap(C, D);
1701 else
1702 return nullptr;
1703 }
1704
1705 BinaryOperator *NewBinop;
1706 if (A == C) {
1707 Value *NewIntrinsic = Builder.CreateBinaryIntrinsic(TopLevelOpcode, B, D);
1708 NewBinop =
1709 cast<BinaryOperator>(Builder.CreateBinOp(InnerOpcode, A, NewIntrinsic));
1710 } else if (B == D) {
1711 Value *NewIntrinsic = Builder.CreateBinaryIntrinsic(TopLevelOpcode, A, C);
1712 NewBinop =
1713 cast<BinaryOperator>(Builder.CreateBinOp(InnerOpcode, NewIntrinsic, B));
1714 } else {
1715 return nullptr;
1716 }
1717
1718 NewBinop->setHasNoUnsignedWrap(HasNUW);
1719 NewBinop->setHasNoSignedWrap(HasNSW);
1720
1721 return NewBinop;
1722}
1723
1724/// CallInst simplification. This mostly only handles folding of intrinsic
1725/// instructions. For normal calls, it allows visitCallBase to do the heavy
1726/// lifting.
1728 // Don't try to simplify calls without uses. It will not do anything useful,
1729 // but will result in the following folds being skipped.
1730 if (!CI.use_empty()) {
1731 SmallVector<Value *, 8> Args(CI.args());
1732 if (Value *V = simplifyCall(&CI, CI.getCalledOperand(), Args,
1733 SQ.getWithInstruction(&CI)))
1734 return replaceInstUsesWith(CI, V);
1735 }
1736
1737 if (Value *FreedOp = getFreedOperand(&CI, &TLI))
1738 return visitFree(CI, FreedOp);
1739
1740 // If the caller function (i.e. us, the function that contains this CallInst)
1741 // is nounwind, mark the call as nounwind, even if the callee isn't.
1742 if (CI.getFunction()->doesNotThrow() && !CI.doesNotThrow()) {
1743 CI.setDoesNotThrow();
1744 return &CI;
1745 }
1746
1747 IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
1748 if (!II)
1749 return visitCallBase(CI);
1750
1751 // Intrinsics cannot occur in an invoke or a callbr, so handle them here
1752 // instead of in visitCallBase.
1753 if (auto *MI = dyn_cast<AnyMemIntrinsic>(II)) {
1754 if (auto NumBytes = MI->getLengthInBytes()) {
1755 // memmove/cpy/set of zero bytes is a noop.
1756 if (NumBytes->isZero())
1757 return eraseInstFromFunction(CI);
1758
1759 // For atomic unordered mem intrinsics if len is not a positive or
1760 // not a multiple of element size then behavior is undefined.
1761 if (MI->isAtomic() &&
1762 (NumBytes->isNegative() ||
1763 (NumBytes->getZExtValue() % MI->getElementSizeInBytes() != 0))) {
1765 assert(MI->getType()->isVoidTy() &&
1766 "non void atomic unordered mem intrinsic");
1767 return eraseInstFromFunction(*MI);
1768 }
1769 }
1770
1771 // No other transformations apply to volatile transfers.
1772 if (MI->isVolatile())
1773 return nullptr;
1774
1775 if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(MI)) {
1776 // memmove(x,x,size) -> noop.
1777 if (MTI->getSource() == MTI->getDest())
1778 return eraseInstFromFunction(CI);
1779 }
1780
1781 auto IsPointerUndefined = [MI](Value *Ptr) {
1782 return isa<ConstantPointerNull>(Ptr) &&
1784 MI->getFunction(),
1785 cast<PointerType>(Ptr->getType())->getAddressSpace());
1786 };
1787 bool SrcIsUndefined = false;
1788 // If we can determine a pointer alignment that is bigger than currently
1789 // set, update the alignment.
1790 if (auto *MTI = dyn_cast<AnyMemTransferInst>(MI)) {
1792 return I;
1793 SrcIsUndefined = IsPointerUndefined(MTI->getRawSource());
1794 } else if (auto *MSI = dyn_cast<AnyMemSetInst>(MI)) {
1795 if (Instruction *I = SimplifyAnyMemSet(MSI))
1796 return I;
1797 }
1798
1799 // If src/dest is null, this memory intrinsic must be a noop.
1800 if (SrcIsUndefined || IsPointerUndefined(MI->getRawDest())) {
1802 return eraseInstFromFunction(CI);
1803 }
1804
1805 // If we have a memmove and the source operation is a constant global,
1806 // then the source and dest pointers can't alias, so we can change this
1807 // into a call to memcpy.
1808 if (auto *MMI = dyn_cast<AnyMemMoveInst>(MI)) {
1809 if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
1810 if (GVSrc->isConstant()) {
1811 Module *M = CI.getModule();
1812 Intrinsic::ID MemCpyID =
1813 MMI->isAtomic()
1814 ? Intrinsic::memcpy_element_unordered_atomic
1815 : Intrinsic::memcpy;
1816 Type *Tys[3] = { CI.getArgOperand(0)->getType(),
1817 CI.getArgOperand(1)->getType(),
1818 CI.getArgOperand(2)->getType() };
1820 Intrinsic::getOrInsertDeclaration(M, MemCpyID, Tys));
1821 return II;
1822 }
1823 }
1824 }
1825
1826 // For fixed width vector result intrinsics, use the generic demanded vector
1827 // support.
1828 if (auto *IIFVTy = dyn_cast<FixedVectorType>(II->getType())) {
1829 auto VWidth = IIFVTy->getNumElements();
1830 APInt PoisonElts(VWidth, 0);
1831 APInt AllOnesEltMask(APInt::getAllOnes(VWidth));
1832 if (Value *V = SimplifyDemandedVectorElts(II, AllOnesEltMask, PoisonElts)) {
1833 if (V != II)
1834 return replaceInstUsesWith(*II, V);
1835 return II;
1836 }
1837 }
1838
1839 if (II->isCommutative()) {
1840 if (auto Pair = matchSymmetricPair(II->getOperand(0), II->getOperand(1))) {
1841 replaceOperand(*II, 0, Pair->first);
1842 replaceOperand(*II, 1, Pair->second);
1843 return II;
1844 }
1845
1846 if (CallInst *NewCall = canonicalizeConstantArg0ToArg1(CI))
1847 return NewCall;
1848 }
1849
1850 // Unused constrained FP intrinsic calls may have declared side effect, which
1851 // prevents it from being removed. In some cases however the side effect is
1852 // actually absent. To detect this case, call SimplifyConstrainedFPCall. If it
1853 // returns a replacement, the call may be removed.
1854 if (CI.use_empty() && isa<ConstrainedFPIntrinsic>(CI)) {
1856 return eraseInstFromFunction(CI);
1857 }
1858
1859 Intrinsic::ID IID = II->getIntrinsicID();
1860 switch (IID) {
1861 case Intrinsic::objectsize: {
1862 SmallVector<Instruction *> InsertedInstructions;
1863 if (Value *V = lowerObjectSizeCall(II, DL, &TLI, AA, /*MustSucceed=*/false,
1864 &InsertedInstructions)) {
1865 for (Instruction *Inserted : InsertedInstructions)
1866 Worklist.add(Inserted);
1867 return replaceInstUsesWith(CI, V);
1868 }
1869 return nullptr;
1870 }
1871 case Intrinsic::abs: {
1872 Value *IIOperand = II->getArgOperand(0);
1873 bool IntMinIsPoison = cast<Constant>(II->getArgOperand(1))->isOneValue();
1874
1875 // abs(-x) -> abs(x)
1876 Value *X;
1877 if (match(IIOperand, m_Neg(m_Value(X)))) {
1878 if (cast<Instruction>(IIOperand)->hasNoSignedWrap() || IntMinIsPoison)
1880 return replaceOperand(*II, 0, X);
1881 }
1882 if (match(IIOperand, m_c_Select(m_Neg(m_Value(X)), m_Deferred(X))))
1883 return replaceOperand(*II, 0, X);
1884
1885 Value *Y;
1886 // abs(a * abs(b)) -> abs(a * b)
1887 if (match(IIOperand,
1889 m_Intrinsic<Intrinsic::abs>(m_Value(Y)))))) {
1890 bool NSW =
1891 cast<Instruction>(IIOperand)->hasNoSignedWrap() && IntMinIsPoison;
1892 auto *XY = NSW ? Builder.CreateNSWMul(X, Y) : Builder.CreateMul(X, Y);
1893 return replaceOperand(*II, 0, XY);
1894 }
1895
1896 if (std::optional<bool> Known =
1898 // abs(x) -> x if x >= 0 (include abs(x-y) --> x - y where x >= y)
1899 // abs(x) -> x if x > 0 (include abs(x-y) --> x - y where x > y)
1900 if (!*Known)
1901 return replaceInstUsesWith(*II, IIOperand);
1902
1903 // abs(x) -> -x if x < 0
1904 // abs(x) -> -x if x < = 0 (include abs(x-y) --> y - x where x <= y)
1905 if (IntMinIsPoison)
1906 return BinaryOperator::CreateNSWNeg(IIOperand);
1907 return BinaryOperator::CreateNeg(IIOperand);
1908 }
1909
1910 // abs (sext X) --> zext (abs X*)
1911 // Clear the IsIntMin (nsw) bit on the abs to allow narrowing.
1912 if (match(IIOperand, m_OneUse(m_SExt(m_Value(X))))) {
1913 Value *NarrowAbs =
1914 Builder.CreateBinaryIntrinsic(Intrinsic::abs, X, Builder.getFalse());
1915 return CastInst::Create(Instruction::ZExt, NarrowAbs, II->getType());
1916 }
1917
1918 // Match a complicated way to check if a number is odd/even:
1919 // abs (srem X, 2) --> and X, 1
1920 const APInt *C;
1921 if (match(IIOperand, m_SRem(m_Value(X), m_APInt(C))) && *C == 2)
1922 return BinaryOperator::CreateAnd(X, ConstantInt::get(II->getType(), 1));
1923
1924 break;
1925 }
1926 case Intrinsic::umin: {
1927 Value *I0 = II->getArgOperand(0), *I1 = II->getArgOperand(1);
1928 // umin(x, 1) == zext(x != 0)
1929 if (match(I1, m_One())) {
1930 assert(II->getType()->getScalarSizeInBits() != 1 &&
1931 "Expected simplify of umin with max constant");
1932 Value *Zero = Constant::getNullValue(I0->getType());
1933 Value *Cmp = Builder.CreateICmpNE(I0, Zero);
1934 return CastInst::Create(Instruction::ZExt, Cmp, II->getType());
1935 }
1936 // umin(cttz(x), const) --> cttz(x | (1 << const))
1937 if (Value *FoldedCttz =
1938 foldMinimumOverTrailingOrLeadingZeroCount<Intrinsic::cttz>(
1939 I0, I1, DL, Builder))
1940 return replaceInstUsesWith(*II, FoldedCttz);
1941 // umin(ctlz(x), const) --> ctlz(x | (SignedMin >> const))
1942 if (Value *FoldedCtlz =
1943 foldMinimumOverTrailingOrLeadingZeroCount<Intrinsic::ctlz>(
1944 I0, I1, DL, Builder))
1945 return replaceInstUsesWith(*II, FoldedCtlz);
1946 [[fallthrough]];
1947 }
1948 case Intrinsic::umax: {
1949 Value *I0 = II->getArgOperand(0), *I1 = II->getArgOperand(1);
1950 Value *X, *Y;
1951 if (match(I0, m_ZExt(m_Value(X))) && match(I1, m_ZExt(m_Value(Y))) &&
1952 (I0->hasOneUse() || I1->hasOneUse()) && X->getType() == Y->getType()) {
1953 Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, Y);
1954 return CastInst::Create(Instruction::ZExt, NarrowMaxMin, II->getType());
1955 }
1956 Constant *C;
1957 if (match(I0, m_ZExt(m_Value(X))) && match(I1, m_Constant(C)) &&
1958 I0->hasOneUse()) {
1959 if (Constant *NarrowC = getLosslessUnsignedTrunc(C, X->getType())) {
1960 Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, NarrowC);
1961 return CastInst::Create(Instruction::ZExt, NarrowMaxMin, II->getType());
1962 }
1963 }
1964 // If C is not 0:
1965 // umax(nuw_shl(x, C), x + 1) -> x == 0 ? 1 : nuw_shl(x, C)
1966 // If C is not 0 or 1:
1967 // umax(nuw_mul(x, C), x + 1) -> x == 0 ? 1 : nuw_mul(x, C)
1968 auto foldMaxMulShift = [&](Value *A, Value *B) -> Instruction * {
1969 const APInt *C;
1970 Value *X;
1971 if (!match(A, m_NUWShl(m_Value(X), m_APInt(C))) &&
1972 !(match(A, m_NUWMul(m_Value(X), m_APInt(C))) && !C->isOne()))
1973 return nullptr;
1974 if (C->isZero())
1975 return nullptr;
1976 if (!match(B, m_OneUse(m_Add(m_Specific(X), m_One()))))
1977 return nullptr;
1978
1979 Value *Cmp = Builder.CreateICmpEQ(X, ConstantInt::get(X->getType(), 0));
1980 Value *NewSelect =
1981 Builder.CreateSelect(Cmp, ConstantInt::get(X->getType(), 1), A);
1982 return replaceInstUsesWith(*II, NewSelect);
1983 };
1984
1985 if (IID == Intrinsic::umax) {
1986 if (Instruction *I = foldMaxMulShift(I0, I1))
1987 return I;
1988 if (Instruction *I = foldMaxMulShift(I1, I0))
1989 return I;
1990 }
1991
1992 // If both operands of unsigned min/max are sign-extended, it is still ok
1993 // to narrow the operation.
1994 [[fallthrough]];
1995 }
1996 case Intrinsic::smax:
1997 case Intrinsic::smin: {
1998 Value *I0 = II->getArgOperand(0), *I1 = II->getArgOperand(1);
1999 Value *X, *Y;
2000 if (match(I0, m_SExt(m_Value(X))) && match(I1, m_SExt(m_Value(Y))) &&
2001 (I0->hasOneUse() || I1->hasOneUse()) && X->getType() == Y->getType()) {
2002 Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, Y);
2003 return CastInst::Create(Instruction::SExt, NarrowMaxMin, II->getType());
2004 }
2005
2006 Constant *C;
2007 if (match(I0, m_SExt(m_Value(X))) && match(I1, m_Constant(C)) &&
2008 I0->hasOneUse()) {
2009 if (Constant *NarrowC = getLosslessSignedTrunc(C, X->getType())) {
2010 Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, NarrowC);
2011 return CastInst::Create(Instruction::SExt, NarrowMaxMin, II->getType());
2012 }
2013 }
2014
2015 // smax(smin(X, MinC), MaxC) -> smin(smax(X, MaxC), MinC) if MinC s>= MaxC
2016 // umax(umin(X, MinC), MaxC) -> umin(umax(X, MaxC), MinC) if MinC u>= MaxC
2017 const APInt *MinC, *MaxC;
2018 auto CreateCanonicalClampForm = [&](bool IsSigned) {
2019 auto MaxIID = IsSigned ? Intrinsic::smax : Intrinsic::umax;
2020 auto MinIID = IsSigned ? Intrinsic::smin : Intrinsic::umin;
2022 MaxIID, X, ConstantInt::get(X->getType(), *MaxC));
2023 return replaceInstUsesWith(
2025 MinIID, NewMax, ConstantInt::get(X->getType(), *MinC)));
2026 };
2027 if (IID == Intrinsic::smax &&
2028 match(I0, m_OneUse(m_Intrinsic<Intrinsic::smin>(m_Value(X),
2029 m_APInt(MinC)))) &&
2030 match(I1, m_APInt(MaxC)) && MinC->sgt(*MaxC))
2031 return CreateCanonicalClampForm(true);
2032 if (IID == Intrinsic::umax &&
2033 match(I0, m_OneUse(m_Intrinsic<Intrinsic::umin>(m_Value(X),
2034 m_APInt(MinC)))) &&
2035 match(I1, m_APInt(MaxC)) && MinC->ugt(*MaxC))
2036 return CreateCanonicalClampForm(false);
2037
2038 // umin(i1 X, i1 Y) -> and i1 X, Y
2039 // smax(i1 X, i1 Y) -> and i1 X, Y
2040 if ((IID == Intrinsic::umin || IID == Intrinsic::smax) &&
2041 II->getType()->isIntOrIntVectorTy(1)) {
2042 return BinaryOperator::CreateAnd(I0, I1);
2043 }
2044
2045 // umax(i1 X, i1 Y) -> or i1 X, Y
2046 // smin(i1 X, i1 Y) -> or i1 X, Y
2047 if ((IID == Intrinsic::umax || IID == Intrinsic::smin) &&
2048 II->getType()->isIntOrIntVectorTy(1)) {
2049 return BinaryOperator::CreateOr(I0, I1);
2050 }
2051
2052 // smin(smax(X, -1), 1) -> scmp(X, 0)
2053 // smax(smin(X, 1), -1) -> scmp(X, 0)
2054 // At this point, smax(smin(X, 1), -1) is changed to smin(smax(X, -1)
2055 // And i1's have been changed to and/ors
2056 // So we only need to check for smin
2057 if (IID == Intrinsic::smin) {
2058 if (match(I0, m_OneUse(m_SMax(m_Value(X), m_AllOnes()))) &&
2059 match(I1, m_One())) {
2060 Value *Zero = ConstantInt::get(X->getType(), 0);
2061 return replaceInstUsesWith(
2062 CI,
2063 Builder.CreateIntrinsic(II->getType(), Intrinsic::scmp, {X, Zero}));
2064 }
2065 }
2066
2067 if (IID == Intrinsic::smax || IID == Intrinsic::smin) {
2068 // smax (neg nsw X), (neg nsw Y) --> neg nsw (smin X, Y)
2069 // smin (neg nsw X), (neg nsw Y) --> neg nsw (smax X, Y)
2070 // TODO: Canonicalize neg after min/max if I1 is constant.
2071 if (match(I0, m_NSWNeg(m_Value(X))) && match(I1, m_NSWNeg(m_Value(Y))) &&
2072 (I0->hasOneUse() || I1->hasOneUse())) {
2074 Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, Y);
2075 return BinaryOperator::CreateNSWNeg(InvMaxMin);
2076 }
2077 }
2078
2079 // (umax X, (xor X, Pow2))
2080 // -> (or X, Pow2)
2081 // (umin X, (xor X, Pow2))
2082 // -> (and X, ~Pow2)
2083 // (smax X, (xor X, Pos_Pow2))
2084 // -> (or X, Pos_Pow2)
2085 // (smin X, (xor X, Pos_Pow2))
2086 // -> (and X, ~Pos_Pow2)
2087 // (smax X, (xor X, Neg_Pow2))
2088 // -> (and X, ~Neg_Pow2)
2089 // (smin X, (xor X, Neg_Pow2))
2090 // -> (or X, Neg_Pow2)
2091 if ((match(I0, m_c_Xor(m_Specific(I1), m_Value(X))) ||
2092 match(I1, m_c_Xor(m_Specific(I0), m_Value(X)))) &&
2093 isKnownToBeAPowerOfTwo(X, /* OrZero */ true)) {
2094 bool UseOr = IID == Intrinsic::smax || IID == Intrinsic::umax;
2095 bool UseAndN = IID == Intrinsic::smin || IID == Intrinsic::umin;
2096
2097 if (IID == Intrinsic::smax || IID == Intrinsic::smin) {
2098 auto KnownSign = getKnownSign(X, SQ.getWithInstruction(II));
2099 if (KnownSign == std::nullopt) {
2100 UseOr = false;
2101 UseAndN = false;
2102 } else if (*KnownSign /* true is Signed. */) {
2103 UseOr ^= true;
2104 UseAndN ^= true;
2105 Type *Ty = I0->getType();
2106 // Negative power of 2 must be IntMin. It's possible to be able to
2107 // prove negative / power of 2 without actually having known bits, so
2108 // just get the value by hand.
2111 }
2112 }
2113 if (UseOr)
2114 return BinaryOperator::CreateOr(I0, X);
2115 else if (UseAndN)
2116 return BinaryOperator::CreateAnd(I0, Builder.CreateNot(X));
2117 }
2118
2119 // If we can eliminate ~A and Y is free to invert:
2120 // max ~A, Y --> ~(min A, ~Y)
2121 //
2122 // Examples:
2123 // max ~A, ~Y --> ~(min A, Y)
2124 // max ~A, C --> ~(min A, ~C)
2125 // max ~A, (max ~Y, ~Z) --> ~min( A, (min Y, Z))
2126 auto moveNotAfterMinMax = [&](Value *X, Value *Y) -> Instruction * {
2127 Value *A;
2128 if (match(X, m_OneUse(m_Not(m_Value(A)))) &&
2129 !isFreeToInvert(A, A->hasOneUse())) {
2130 if (Value *NotY = getFreelyInverted(Y, Y->hasOneUse(), &Builder)) {
2132 Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, A, NotY);
2133 return BinaryOperator::CreateNot(InvMaxMin);
2134 }
2135 }
2136 return nullptr;
2137 };
2138
2139 if (Instruction *I = moveNotAfterMinMax(I0, I1))
2140 return I;
2141 if (Instruction *I = moveNotAfterMinMax(I1, I0))
2142 return I;
2143
2145 return I;
2146
2147 // minmax (X & NegPow2C, Y & NegPow2C) --> minmax(X, Y) & NegPow2C
2148 const APInt *RHSC;
2149 if (match(I0, m_OneUse(m_And(m_Value(X), m_NegatedPower2(RHSC)))) &&
2150 match(I1, m_OneUse(m_And(m_Value(Y), m_SpecificInt(*RHSC)))))
2151 return BinaryOperator::CreateAnd(Builder.CreateBinaryIntrinsic(IID, X, Y),
2152 ConstantInt::get(II->getType(), *RHSC));
2153
2154 // smax(X, -X) --> abs(X)
2155 // smin(X, -X) --> -abs(X)
2156 // umax(X, -X) --> -abs(X)
2157 // umin(X, -X) --> abs(X)
2158 if (isKnownNegation(I0, I1)) {
2159 // We can choose either operand as the input to abs(), but if we can
2160 // eliminate the only use of a value, that's better for subsequent
2161 // transforms/analysis.
2162 if (I0->hasOneUse() && !I1->hasOneUse())
2163 std::swap(I0, I1);
2164
2165 // This is some variant of abs(). See if we can propagate 'nsw' to the abs
2166 // operation and potentially its negation.
2167 bool IntMinIsPoison = isKnownNegation(I0, I1, /* NeedNSW */ true);
2169 Intrinsic::abs, I0,
2170 ConstantInt::getBool(II->getContext(), IntMinIsPoison));
2171
2172 // We don't have a "nabs" intrinsic, so negate if needed based on the
2173 // max/min operation.
2174 if (IID == Intrinsic::smin || IID == Intrinsic::umax)
2175 Abs = Builder.CreateNeg(Abs, "nabs", IntMinIsPoison);
2176 return replaceInstUsesWith(CI, Abs);
2177 }
2178
2180 return Sel;
2181
2182 if (Instruction *SAdd = matchSAddSubSat(*II))
2183 return SAdd;
2184
2185 if (Value *NewMinMax = reassociateMinMaxWithConstants(II, Builder, SQ))
2186 return replaceInstUsesWith(*II, NewMinMax);
2187
2189 return R;
2190
2191 if (Instruction *NewMinMax = factorizeMinMaxTree(II))
2192 return NewMinMax;
2193
2194 // Try to fold minmax with constant RHS based on range information
2195 if (match(I1, m_APIntAllowPoison(RHSC))) {
2196 ICmpInst::Predicate Pred =
2198 bool IsSigned = MinMaxIntrinsic::isSigned(IID);
2200 I0, IsSigned, SQ.getWithInstruction(II));
2201 if (!LHS_CR.isFullSet()) {
2202 if (LHS_CR.icmp(Pred, *RHSC))
2203 return replaceInstUsesWith(*II, I0);
2204 if (LHS_CR.icmp(ICmpInst::getSwappedPredicate(Pred), *RHSC))
2205 return replaceInstUsesWith(*II,
2206 ConstantInt::get(II->getType(), *RHSC));
2207 }
2208 }
2209
2211 return replaceInstUsesWith(*II, V);
2212
2213 break;
2214 }
2215 case Intrinsic::scmp: {
2216 Value *I0 = II->getArgOperand(0), *I1 = II->getArgOperand(1);
2217 Value *LHS, *RHS;
2218 if (match(I0, m_NSWSub(m_Value(LHS), m_Value(RHS))) && match(I1, m_Zero()))
2219 return replaceInstUsesWith(
2220 CI,
2221 Builder.CreateIntrinsic(II->getType(), Intrinsic::scmp, {LHS, RHS}));
2222 break;
2223 }
2224 case Intrinsic::bitreverse: {
2225 Value *IIOperand = II->getArgOperand(0);
2226 // bitrev (zext i1 X to ?) --> X ? SignBitC : 0
2227 Value *X;
2228 if (match(IIOperand, m_ZExt(m_Value(X))) &&
2229 X->getType()->isIntOrIntVectorTy(1)) {
2230 Type *Ty = II->getType();
2232 return SelectInst::Create(X, ConstantInt::get(Ty, SignBit),
2234 }
2235
2236 if (Instruction *crossLogicOpFold =
2237 foldBitOrderCrossLogicOp<Intrinsic::bitreverse>(IIOperand, Builder))
2238 return crossLogicOpFold;
2239
2240 break;
2241 }
2242 case Intrinsic::bswap: {
2243 Value *IIOperand = II->getArgOperand(0);
2244
2245 // Try to canonicalize bswap-of-logical-shift-by-8-bit-multiple as
2246 // inverse-shift-of-bswap:
2247 // bswap (shl X, Y) --> lshr (bswap X), Y
2248 // bswap (lshr X, Y) --> shl (bswap X), Y
2249 Value *X, *Y;
2250 if (match(IIOperand, m_OneUse(m_LogicalShift(m_Value(X), m_Value(Y))))) {
2251 unsigned BitWidth = IIOperand->getType()->getScalarSizeInBits();
2253 Value *NewSwap = Builder.CreateUnaryIntrinsic(Intrinsic::bswap, X);
2254 BinaryOperator::BinaryOps InverseShift =
2255 cast<BinaryOperator>(IIOperand)->getOpcode() == Instruction::Shl
2256 ? Instruction::LShr
2257 : Instruction::Shl;
2258 return BinaryOperator::Create(InverseShift, NewSwap, Y);
2259 }
2260 }
2261
2262 KnownBits Known = computeKnownBits(IIOperand, II);
2263 uint64_t LZ = alignDown(Known.countMinLeadingZeros(), 8);
2264 uint64_t TZ = alignDown(Known.countMinTrailingZeros(), 8);
2265 unsigned BW = Known.getBitWidth();
2266
2267 // bswap(x) -> shift(x) if x has exactly one "active byte"
2268 if (BW - LZ - TZ == 8) {
2269 assert(LZ != TZ && "active byte cannot be in the middle");
2270 if (LZ > TZ) // -> shl(x) if the "active byte" is in the low part of x
2271 return BinaryOperator::CreateNUWShl(
2272 IIOperand, ConstantInt::get(IIOperand->getType(), LZ - TZ));
2273 // -> lshr(x) if the "active byte" is in the high part of x
2274 return BinaryOperator::CreateExactLShr(
2275 IIOperand, ConstantInt::get(IIOperand->getType(), TZ - LZ));
2276 }
2277
2278 // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
2279 if (match(IIOperand, m_Trunc(m_BSwap(m_Value(X))))) {
2280 unsigned C = X->getType()->getScalarSizeInBits() - BW;
2281 Value *CV = ConstantInt::get(X->getType(), C);
2282 Value *V = Builder.CreateLShr(X, CV);
2283 return new TruncInst(V, IIOperand->getType());
2284 }
2285
2286 if (Instruction *crossLogicOpFold =
2287 foldBitOrderCrossLogicOp<Intrinsic::bswap>(IIOperand, Builder)) {
2288 return crossLogicOpFold;
2289 }
2290
2291 // Try to fold into bitreverse if bswap is the root of the expression tree.
2292 if (Instruction *BitOp = matchBSwapOrBitReverse(*II, /*MatchBSwaps*/ false,
2293 /*MatchBitReversals*/ true))
2294 return BitOp;
2295 break;
2296 }
2297 case Intrinsic::masked_load:
2298 if (Value *SimplifiedMaskedOp = simplifyMaskedLoad(*II))
2299 return replaceInstUsesWith(CI, SimplifiedMaskedOp);
2300 break;
2301 case Intrinsic::masked_store:
2302 return simplifyMaskedStore(*II);
2303 case Intrinsic::masked_gather:
2304 return simplifyMaskedGather(*II);
2305 case Intrinsic::masked_scatter:
2306 return simplifyMaskedScatter(*II);
2307 case Intrinsic::launder_invariant_group:
2308 case Intrinsic::strip_invariant_group:
2309 if (auto *SkippedBarrier = simplifyInvariantGroupIntrinsic(*II, *this))
2310 return replaceInstUsesWith(*II, SkippedBarrier);
2311 break;
2312 case Intrinsic::powi:
2313 if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
2314 // 0 and 1 are handled in instsimplify
2315 // powi(x, -1) -> 1/x
2316 if (Power->isMinusOne())
2317 return BinaryOperator::CreateFDivFMF(ConstantFP::get(CI.getType(), 1.0),
2318 II->getArgOperand(0), II);
2319 // powi(x, 2) -> x*x
2320 if (Power->equalsInt(2))
2321 return BinaryOperator::CreateFMulFMF(II->getArgOperand(0),
2322 II->getArgOperand(0), II);
2323
2324 if (!Power->getValue()[0]) {
2325 Value *X;
2326 // If power is even:
2327 // powi(-x, p) -> powi(x, p)
2328 // powi(fabs(x), p) -> powi(x, p)
2329 // powi(copysign(x, y), p) -> powi(x, p)
2330 if (match(II->getArgOperand(0), m_FNeg(m_Value(X))) ||
2331 match(II->getArgOperand(0), m_FAbs(m_Value(X))) ||
2332 match(II->getArgOperand(0),
2333 m_Intrinsic<Intrinsic::copysign>(m_Value(X), m_Value())))
2334 return replaceOperand(*II, 0, X);
2335 }
2336 }
2337 break;
2338
2339 case Intrinsic::cttz:
2340 case Intrinsic::ctlz:
2341 if (auto *I = foldCttzCtlz(*II, *this))
2342 return I;
2343 break;
2344
2345 case Intrinsic::ctpop:
2346 if (auto *I = foldCtpop(*II, *this))
2347 return I;
2348 break;
2349
2350 case Intrinsic::fshl:
2351 case Intrinsic::fshr: {
2352 Value *Op0 = II->getArgOperand(0), *Op1 = II->getArgOperand(1);
2353 Type *Ty = II->getType();
2354 unsigned BitWidth = Ty->getScalarSizeInBits();
2355 Constant *ShAmtC;
2356 if (match(II->getArgOperand(2), m_ImmConstant(ShAmtC))) {
2357 // Canonicalize a shift amount constant operand to modulo the bit-width.
2358 Constant *WidthC = ConstantInt::get(Ty, BitWidth);
2359 Constant *ModuloC =
2360 ConstantFoldBinaryOpOperands(Instruction::URem, ShAmtC, WidthC, DL);
2361 if (!ModuloC)
2362 return nullptr;
2363 if (ModuloC != ShAmtC)
2364 return replaceOperand(*II, 2, ModuloC);
2365
2367 ShAmtC, DL),
2368 m_One()) &&
2369 "Shift amount expected to be modulo bitwidth");
2370
2371 // Canonicalize funnel shift right by constant to funnel shift left. This
2372 // is not entirely arbitrary. For historical reasons, the backend may
2373 // recognize rotate left patterns but miss rotate right patterns.
2374 if (IID == Intrinsic::fshr) {
2375 // fshr X, Y, C --> fshl X, Y, (BitWidth - C) if C is not zero.
2376 if (!isKnownNonZero(ShAmtC, SQ.getWithInstruction(II)))
2377 return nullptr;
2378
2379 Constant *LeftShiftC = ConstantExpr::getSub(WidthC, ShAmtC);
2380 Module *Mod = II->getModule();
2381 Function *Fshl =
2382 Intrinsic::getOrInsertDeclaration(Mod, Intrinsic::fshl, Ty);
2383 return CallInst::Create(Fshl, { Op0, Op1, LeftShiftC });
2384 }
2385 assert(IID == Intrinsic::fshl &&
2386 "All funnel shifts by simple constants should go left");
2387
2388 // fshl(X, 0, C) --> shl X, C
2389 // fshl(X, undef, C) --> shl X, C
2390 if (match(Op1, m_ZeroInt()) || match(Op1, m_Undef()))
2391 return BinaryOperator::CreateShl(Op0, ShAmtC);
2392
2393 // fshl(0, X, C) --> lshr X, (BW-C)
2394 // fshl(undef, X, C) --> lshr X, (BW-C)
2395 if (match(Op0, m_ZeroInt()) || match(Op0, m_Undef()))
2396 return BinaryOperator::CreateLShr(Op1,
2397 ConstantExpr::getSub(WidthC, ShAmtC));
2398
2399 // fshl i16 X, X, 8 --> bswap i16 X (reduce to more-specific form)
2400 if (Op0 == Op1 && BitWidth == 16 && match(ShAmtC, m_SpecificInt(8))) {
2401 Module *Mod = II->getModule();
2402 Function *Bswap =
2403 Intrinsic::getOrInsertDeclaration(Mod, Intrinsic::bswap, Ty);
2404 return CallInst::Create(Bswap, { Op0 });
2405 }
2406 if (Instruction *BitOp =
2407 matchBSwapOrBitReverse(*II, /*MatchBSwaps*/ true,
2408 /*MatchBitReversals*/ true))
2409 return BitOp;
2410 }
2411
2412 // fshl(X, X, Neg(Y)) --> fshr(X, X, Y)
2413 // fshr(X, X, Neg(Y)) --> fshl(X, X, Y)
2414 // if BitWidth is a power-of-2
2415 Value *Y;
2416 if (Op0 == Op1 && isPowerOf2_32(BitWidth) &&
2417 match(II->getArgOperand(2), m_Neg(m_Value(Y)))) {
2418 Module *Mod = II->getModule();
2420 Mod, IID == Intrinsic::fshl ? Intrinsic::fshr : Intrinsic::fshl, Ty);
2421 return CallInst::Create(OppositeShift, {Op0, Op1, Y});
2422 }
2423
2424 // fshl(X, 0, Y) --> shl(X, and(Y, BitWidth - 1)) if bitwidth is a
2425 // power-of-2
2426 if (IID == Intrinsic::fshl && isPowerOf2_32(BitWidth) &&
2427 match(Op1, m_ZeroInt())) {
2428 Value *Op2 = II->getArgOperand(2);
2429 Value *And = Builder.CreateAnd(Op2, ConstantInt::get(Ty, BitWidth - 1));
2430 return BinaryOperator::CreateShl(Op0, And);
2431 }
2432
2433 // Left or right might be masked.
2435 return &CI;
2436
2437 // The shift amount (operand 2) of a funnel shift is modulo the bitwidth,
2438 // so only the low bits of the shift amount are demanded if the bitwidth is
2439 // a power-of-2.
2440 if (!isPowerOf2_32(BitWidth))
2441 break;
2443 KnownBits Op2Known(BitWidth);
2444 if (SimplifyDemandedBits(II, 2, Op2Demanded, Op2Known))
2445 return &CI;
2446 break;
2447 }
2448 case Intrinsic::ptrmask: {
2449 unsigned BitWidth = DL.getPointerTypeSizeInBits(II->getType());
2450 KnownBits Known(BitWidth);
2452 return II;
2453
2454 Value *InnerPtr, *InnerMask;
2455 bool Changed = false;
2456 // Combine:
2457 // (ptrmask (ptrmask p, A), B)
2458 // -> (ptrmask p, (and A, B))
2459 if (match(II->getArgOperand(0),
2460 m_OneUse(m_Intrinsic<Intrinsic::ptrmask>(m_Value(InnerPtr),
2461 m_Value(InnerMask))))) {
2462 assert(II->getArgOperand(1)->getType() == InnerMask->getType() &&
2463 "Mask types must match");
2464 // TODO: If InnerMask == Op1, we could copy attributes from inner
2465 // callsite -> outer callsite.
2466 Value *NewMask = Builder.CreateAnd(II->getArgOperand(1), InnerMask);
2467 replaceOperand(CI, 0, InnerPtr);
2468 replaceOperand(CI, 1, NewMask);
2469 Changed = true;
2470 }
2471
2472 // See if we can deduce non-null.
2473 if (!CI.hasRetAttr(Attribute::NonNull) &&
2474 (Known.isNonZero() ||
2475 isKnownNonZero(II, getSimplifyQuery().getWithInstruction(II)))) {
2476 CI.addRetAttr(Attribute::NonNull);
2477 Changed = true;
2478 }
2479
2480 unsigned NewAlignmentLog =
2482 std::min(BitWidth - 1, Known.countMinTrailingZeros()));
2483 // Known bits will capture if we had alignment information associated with
2484 // the pointer argument.
2485 if (NewAlignmentLog > Log2(CI.getRetAlign().valueOrOne())) {
2487 CI.getContext(), Align(uint64_t(1) << NewAlignmentLog)));
2488 Changed = true;
2489 }
2490 if (Changed)
2491 return &CI;
2492 break;
2493 }
2494 case Intrinsic::uadd_with_overflow:
2495 case Intrinsic::sadd_with_overflow: {
2496 if (Instruction *I = foldIntrinsicWithOverflowCommon(II))
2497 return I;
2498
2499 // Given 2 constant operands whose sum does not overflow:
2500 // uaddo (X +nuw C0), C1 -> uaddo X, C0 + C1
2501 // saddo (X +nsw C0), C1 -> saddo X, C0 + C1
2502 Value *X;
2503 const APInt *C0, *C1;
2504 Value *Arg0 = II->getArgOperand(0);
2505 Value *Arg1 = II->getArgOperand(1);
2506 bool IsSigned = IID == Intrinsic::sadd_with_overflow;
2507 bool HasNWAdd = IsSigned
2508 ? match(Arg0, m_NSWAddLike(m_Value(X), m_APInt(C0)))
2509 : match(Arg0, m_NUWAddLike(m_Value(X), m_APInt(C0)));
2510 if (HasNWAdd && match(Arg1, m_APInt(C1))) {
2511 bool Overflow;
2512 APInt NewC =
2513 IsSigned ? C1->sadd_ov(*C0, Overflow) : C1->uadd_ov(*C0, Overflow);
2514 if (!Overflow)
2515 return replaceInstUsesWith(
2517 IID, X, ConstantInt::get(Arg1->getType(), NewC)));
2518 }
2519 break;
2520 }
2521
2522 case Intrinsic::umul_with_overflow:
2523 case Intrinsic::smul_with_overflow:
2524 case Intrinsic::usub_with_overflow:
2525 if (Instruction *I = foldIntrinsicWithOverflowCommon(II))
2526 return I;
2527 break;
2528
2529 case Intrinsic::ssub_with_overflow: {
2530 if (Instruction *I = foldIntrinsicWithOverflowCommon(II))
2531 return I;
2532
2533 Constant *C;
2534 Value *Arg0 = II->getArgOperand(0);
2535 Value *Arg1 = II->getArgOperand(1);
2536 // Given a constant C that is not the minimum signed value
2537 // for an integer of a given bit width:
2538 //
2539 // ssubo X, C -> saddo X, -C
2540 if (match(Arg1, m_Constant(C)) && C->isNotMinSignedValue()) {
2541 Value *NegVal = ConstantExpr::getNeg(C);
2542 // Build a saddo call that is equivalent to the discovered
2543 // ssubo call.
2544 return replaceInstUsesWith(
2545 *II, Builder.CreateBinaryIntrinsic(Intrinsic::sadd_with_overflow,
2546 Arg0, NegVal));
2547 }
2548
2549 break;
2550 }
2551
2552 case Intrinsic::uadd_sat:
2553 case Intrinsic::sadd_sat:
2554 case Intrinsic::usub_sat:
2555 case Intrinsic::ssub_sat: {
2556 SaturatingInst *SI = cast<SaturatingInst>(II);
2557 Type *Ty = SI->getType();
2558 Value *Arg0 = SI->getLHS();
2559 Value *Arg1 = SI->getRHS();
2560
2561 // Make use of known overflow information.
2562 OverflowResult OR = computeOverflow(SI->getBinaryOp(), SI->isSigned(),
2563 Arg0, Arg1, SI);
2564 switch (OR) {
2566 break;
2568 if (SI->isSigned())
2569 return BinaryOperator::CreateNSW(SI->getBinaryOp(), Arg0, Arg1);
2570 else
2571 return BinaryOperator::CreateNUW(SI->getBinaryOp(), Arg0, Arg1);
2573 unsigned BitWidth = Ty->getScalarSizeInBits();
2574 APInt Min = APSInt::getMinValue(BitWidth, !SI->isSigned());
2575 return replaceInstUsesWith(*SI, ConstantInt::get(Ty, Min));
2576 }
2578 unsigned BitWidth = Ty->getScalarSizeInBits();
2579 APInt Max = APSInt::getMaxValue(BitWidth, !SI->isSigned());
2580 return replaceInstUsesWith(*SI, ConstantInt::get(Ty, Max));
2581 }
2582 }
2583
2584 // usub_sat((sub nuw C, A), C1) -> usub_sat(usub_sat(C, C1), A)
2585 // which after that:
2586 // usub_sat((sub nuw C, A), C1) -> usub_sat(C - C1, A) if C1 u< C
2587 // usub_sat((sub nuw C, A), C1) -> 0 otherwise
2588 Constant *C, *C1;
2589 Value *A;
2590 if (IID == Intrinsic::usub_sat &&
2591 match(Arg0, m_NUWSub(m_ImmConstant(C), m_Value(A))) &&
2592 match(Arg1, m_ImmConstant(C1))) {
2593 auto *NewC = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, C, C1);
2594 auto *NewSub =
2595 Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, NewC, A);
2596 return replaceInstUsesWith(*SI, NewSub);
2597 }
2598
2599 // ssub.sat(X, C) -> sadd.sat(X, -C) if C != MIN
2600 if (IID == Intrinsic::ssub_sat && match(Arg1, m_Constant(C)) &&
2601 C->isNotMinSignedValue()) {
2602 Value *NegVal = ConstantExpr::getNeg(C);
2603 return replaceInstUsesWith(
2605 Intrinsic::sadd_sat, Arg0, NegVal));
2606 }
2607
2608 // sat(sat(X + Val2) + Val) -> sat(X + (Val+Val2))
2609 // sat(sat(X - Val2) - Val) -> sat(X - (Val+Val2))
2610 // if Val and Val2 have the same sign
2611 if (auto *Other = dyn_cast<IntrinsicInst>(Arg0)) {
2612 Value *X;
2613 const APInt *Val, *Val2;
2614 APInt NewVal;
2615 bool IsUnsigned =
2616 IID == Intrinsic::uadd_sat || IID == Intrinsic::usub_sat;
2617 if (Other->getIntrinsicID() == IID &&
2618 match(Arg1, m_APInt(Val)) &&
2619 match(Other->getArgOperand(0), m_Value(X)) &&
2620 match(Other->getArgOperand(1), m_APInt(Val2))) {
2621 if (IsUnsigned)
2622 NewVal = Val->uadd_sat(*Val2);
2623 else if (Val->isNonNegative() == Val2->isNonNegative()) {
2624 bool Overflow;
2625 NewVal = Val->sadd_ov(*Val2, Overflow);
2626 if (Overflow) {
2627 // Both adds together may add more than SignedMaxValue
2628 // without saturating the final result.
2629 break;
2630 }
2631 } else {
2632 // Cannot fold saturated addition with different signs.
2633 break;
2634 }
2635
2636 return replaceInstUsesWith(
2638 IID, X, ConstantInt::get(II->getType(), NewVal)));
2639 }
2640 }
2641 break;
2642 }
2643
2644 case Intrinsic::minnum:
2645 case Intrinsic::maxnum:
2646 case Intrinsic::minimum:
2647 case Intrinsic::maximum: {
2648 Value *Arg0 = II->getArgOperand(0);
2649 Value *Arg1 = II->getArgOperand(1);
2650 Value *X, *Y;
2651 if (match(Arg0, m_FNeg(m_Value(X))) && match(Arg1, m_FNeg(m_Value(Y))) &&
2652 (Arg0->hasOneUse() || Arg1->hasOneUse())) {
2653 // If both operands are negated, invert the call and negate the result:
2654 // min(-X, -Y) --> -(max(X, Y))
2655 // max(-X, -Y) --> -(min(X, Y))
2656 Intrinsic::ID NewIID;
2657 switch (IID) {
2658 case Intrinsic::maxnum:
2659 NewIID = Intrinsic::minnum;
2660 break;
2661 case Intrinsic::minnum:
2662 NewIID = Intrinsic::maxnum;
2663 break;
2664 case Intrinsic::maximum:
2665 NewIID = Intrinsic::minimum;
2666 break;
2667 case Intrinsic::minimum:
2668 NewIID = Intrinsic::maximum;
2669 break;
2670 default:
2671 llvm_unreachable("unexpected intrinsic ID");
2672 }
2673 Value *NewCall = Builder.CreateBinaryIntrinsic(NewIID, X, Y, II);
2674 Instruction *FNeg = UnaryOperator::CreateFNeg(NewCall);
2675 FNeg->copyIRFlags(II);
2676 return FNeg;
2677 }
2678
2679 // m(m(X, C2), C1) -> m(X, C)
2680 const APFloat *C1, *C2;
2681 if (auto *M = dyn_cast<IntrinsicInst>(Arg0)) {
2682 if (M->getIntrinsicID() == IID && match(Arg1, m_APFloat(C1)) &&
2683 ((match(M->getArgOperand(0), m_Value(X)) &&
2684 match(M->getArgOperand(1), m_APFloat(C2))) ||
2685 (match(M->getArgOperand(1), m_Value(X)) &&
2686 match(M->getArgOperand(0), m_APFloat(C2))))) {
2687 APFloat Res(0.0);
2688 switch (IID) {
2689 case Intrinsic::maxnum:
2690 Res = maxnum(*C1, *C2);
2691 break;
2692 case Intrinsic::minnum:
2693 Res = minnum(*C1, *C2);
2694 break;
2695 case Intrinsic::maximum:
2696 Res = maximum(*C1, *C2);
2697 break;
2698 case Intrinsic::minimum:
2699 Res = minimum(*C1, *C2);
2700 break;
2701 default:
2702 llvm_unreachable("unexpected intrinsic ID");
2703 }
2704 // TODO: Conservatively intersecting FMF. If Res == C2, the transform
2705 // was a simplification (so Arg0 and its original flags could
2706 // propagate?)
2708 IID, X, ConstantFP::get(Arg0->getType(), Res),
2710 return replaceInstUsesWith(*II, V);
2711 }
2712 }
2713
2714 // m((fpext X), (fpext Y)) -> fpext (m(X, Y))
2715 if (match(Arg0, m_OneUse(m_FPExt(m_Value(X)))) &&
2716 match(Arg1, m_OneUse(m_FPExt(m_Value(Y)))) &&
2717 X->getType() == Y->getType()) {
2718 Value *NewCall =
2719 Builder.CreateBinaryIntrinsic(IID, X, Y, II, II->getName());
2720 return new FPExtInst(NewCall, II->getType());
2721 }
2722
2723 // max X, -X --> fabs X
2724 // min X, -X --> -(fabs X)
2725 // TODO: Remove one-use limitation? That is obviously better for max,
2726 // hence why we don't check for one-use for that. However,
2727 // it would be an extra instruction for min (fnabs), but
2728 // that is still likely better for analysis and codegen.
2729 auto IsMinMaxOrXNegX = [IID, &X](Value *Op0, Value *Op1) {
2730 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_Specific(X)))
2731 return Op0->hasOneUse() ||
2732 (IID != Intrinsic::minimum && IID != Intrinsic::minnum);
2733 return false;
2734 };
2735
2736 if (IsMinMaxOrXNegX(Arg0, Arg1) || IsMinMaxOrXNegX(Arg1, Arg0)) {
2737 Value *R = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, II);
2738 if (IID == Intrinsic::minimum || IID == Intrinsic::minnum)
2739 R = Builder.CreateFNegFMF(R, II);
2740 return replaceInstUsesWith(*II, R);
2741 }
2742
2743 break;
2744 }
2745 case Intrinsic::matrix_multiply: {
2746 // Optimize negation in matrix multiplication.
2747
2748 // -A * -B -> A * B
2749 Value *A, *B;
2750 if (match(II->getArgOperand(0), m_FNeg(m_Value(A))) &&
2751 match(II->getArgOperand(1), m_FNeg(m_Value(B)))) {
2752 replaceOperand(*II, 0, A);
2753 replaceOperand(*II, 1, B);
2754 return II;
2755 }
2756
2757 Value *Op0 = II->getOperand(0);
2758 Value *Op1 = II->getOperand(1);
2759 Value *OpNotNeg, *NegatedOp;
2760 unsigned NegatedOpArg, OtherOpArg;
2761 if (match(Op0, m_FNeg(m_Value(OpNotNeg)))) {
2762 NegatedOp = Op0;
2763 NegatedOpArg = 0;
2764 OtherOpArg = 1;
2765 } else if (match(Op1, m_FNeg(m_Value(OpNotNeg)))) {
2766 NegatedOp = Op1;
2767 NegatedOpArg = 1;
2768 OtherOpArg = 0;
2769 } else
2770 // Multiplication doesn't have a negated operand.
2771 break;
2772
2773 // Only optimize if the negated operand has only one use.
2774 if (!NegatedOp->hasOneUse())
2775 break;
2776
2777 Value *OtherOp = II->getOperand(OtherOpArg);
2778 VectorType *RetTy = cast<VectorType>(II->getType());
2779 VectorType *NegatedOpTy = cast<VectorType>(NegatedOp->getType());
2780 VectorType *OtherOpTy = cast<VectorType>(OtherOp->getType());
2781 ElementCount NegatedCount = NegatedOpTy->getElementCount();
2782 ElementCount OtherCount = OtherOpTy->getElementCount();
2783 ElementCount RetCount = RetTy->getElementCount();
2784 // (-A) * B -> A * (-B), if it is cheaper to negate B and vice versa.
2785 if (ElementCount::isKnownGT(NegatedCount, OtherCount) &&
2786 ElementCount::isKnownLT(OtherCount, RetCount)) {
2787 Value *InverseOtherOp = Builder.CreateFNeg(OtherOp);
2788 replaceOperand(*II, NegatedOpArg, OpNotNeg);
2789 replaceOperand(*II, OtherOpArg, InverseOtherOp);
2790 return II;
2791 }
2792 // (-A) * B -> -(A * B), if it is cheaper to negate the result
2793 if (ElementCount::isKnownGT(NegatedCount, RetCount)) {
2794 SmallVector<Value *, 5> NewArgs(II->args());
2795 NewArgs[NegatedOpArg] = OpNotNeg;
2796 Instruction *NewMul =
2797 Builder.CreateIntrinsic(II->getType(), IID, NewArgs, II);
2798 return replaceInstUsesWith(*II, Builder.CreateFNegFMF(NewMul, II));
2799 }
2800 break;
2801 }
2802 case Intrinsic::fmuladd: {
2803 // Try to simplify the underlying FMul.
2804 if (Value *V =
2805 simplifyFMulInst(II->getArgOperand(0), II->getArgOperand(1),
2806 II->getFastMathFlags(), SQ.getWithInstruction(II)))
2807 return BinaryOperator::CreateFAddFMF(V, II->getArgOperand(2),
2808 II->getFastMathFlags());
2809
2810 [[fallthrough]];
2811 }
2812 case Intrinsic::fma: {
2813 // fma fneg(x), fneg(y), z -> fma x, y, z
2814 Value *Src0 = II->getArgOperand(0);
2815 Value *Src1 = II->getArgOperand(1);
2816 Value *Src2 = II->getArgOperand(2);
2817 Value *X, *Y;
2818 if (match(Src0, m_FNeg(m_Value(X))) && match(Src1, m_FNeg(m_Value(Y)))) {
2819 replaceOperand(*II, 0, X);
2820 replaceOperand(*II, 1, Y);
2821 return II;
2822 }
2823
2824 // fma fabs(x), fabs(x), z -> fma x, x, z
2825 if (match(Src0, m_FAbs(m_Value(X))) &&
2826 match(Src1, m_FAbs(m_Specific(X)))) {
2827 replaceOperand(*II, 0, X);
2828 replaceOperand(*II, 1, X);
2829 return II;
2830 }
2831
2832 // Try to simplify the underlying FMul. We can only apply simplifications
2833 // that do not require rounding.
2834 if (Value *V = simplifyFMAFMul(Src0, Src1, II->getFastMathFlags(),
2836 return BinaryOperator::CreateFAddFMF(V, Src2, II->getFastMathFlags());
2837
2838 // fma x, y, 0 -> fmul x, y
2839 // This is always valid for -0.0, but requires nsz for +0.0 as
2840 // -0.0 + 0.0 = 0.0, which would not be the same as the fmul on its own.
2841 if (match(Src2, m_NegZeroFP()) ||
2842 (match(Src2, m_PosZeroFP()) && II->getFastMathFlags().noSignedZeros()))
2843 return BinaryOperator::CreateFMulFMF(Src0, Src1, II);
2844
2845 // fma x, -1.0, y -> fsub y, x
2846 if (match(Src1, m_SpecificFP(-1.0)))
2847 return BinaryOperator::CreateFSubFMF(Src2, Src0, II);
2848
2849 break;
2850 }
2851 case Intrinsic::copysign: {
2852 Value *Mag = II->getArgOperand(0), *Sign = II->getArgOperand(1);
2853 if (std::optional<bool> KnownSignBit = computeKnownFPSignBit(
2854 Sign, getSimplifyQuery().getWithInstruction(II))) {
2855 if (*KnownSignBit) {
2856 // If we know that the sign argument is negative, reduce to FNABS:
2857 // copysign Mag, -Sign --> fneg (fabs Mag)
2858 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, Mag, II);
2860 }
2861
2862 // If we know that the sign argument is positive, reduce to FABS:
2863 // copysign Mag, +Sign --> fabs Mag
2864 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, Mag, II);
2865 return replaceInstUsesWith(*II, Fabs);
2866 }
2867
2868 // Propagate sign argument through nested calls:
2869 // copysign Mag, (copysign ?, X) --> copysign Mag, X
2870 Value *X;
2871 if (match(Sign, m_Intrinsic<Intrinsic::copysign>(m_Value(), m_Value(X)))) {
2872 Value *CopySign =
2874 return replaceInstUsesWith(*II, CopySign);
2875 }
2876
2877 // Clear sign-bit of constant magnitude:
2878 // copysign -MagC, X --> copysign MagC, X
2879 // TODO: Support constant folding for fabs
2880 const APFloat *MagC;
2881 if (match(Mag, m_APFloat(MagC)) && MagC->isNegative()) {
2882 APFloat PosMagC = *MagC;
2883 PosMagC.clearSign();
2884 return replaceOperand(*II, 0, ConstantFP::get(Mag->getType(), PosMagC));
2885 }
2886
2887 // Peek through changes of magnitude's sign-bit. This call rewrites those:
2888 // copysign (fabs X), Sign --> copysign X, Sign
2889 // copysign (fneg X), Sign --> copysign X, Sign
2890 if (match(Mag, m_FAbs(m_Value(X))) || match(Mag, m_FNeg(m_Value(X))))
2891 return replaceOperand(*II, 0, X);
2892
2893 break;
2894 }
2895 case Intrinsic::fabs: {
2896 Value *Cond, *TVal, *FVal;
2897 Value *Arg = II->getArgOperand(0);
2898 Value *X;
2899 // fabs (-X) --> fabs (X)
2900 if (match(Arg, m_FNeg(m_Value(X)))) {
2901 CallInst *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, II);
2902 return replaceInstUsesWith(CI, Fabs);
2903 }
2904
2905 if (match(Arg, m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
2906 // fabs (select Cond, TrueC, FalseC) --> select Cond, AbsT, AbsF
2907 if (Arg->hasOneUse() ? (isa<Constant>(TVal) || isa<Constant>(FVal))
2908 : (isa<Constant>(TVal) && isa<Constant>(FVal))) {
2909 CallInst *AbsT = Builder.CreateCall(II->getCalledFunction(), {TVal});
2910 CallInst *AbsF = Builder.CreateCall(II->getCalledFunction(), {FVal});
2911 SelectInst *SI = SelectInst::Create(Cond, AbsT, AbsF);
2912 FastMathFlags FMF1 = II->getFastMathFlags();
2913 FastMathFlags FMF2 = cast<SelectInst>(Arg)->getFastMathFlags();
2914 FMF2.setNoSignedZeros(false);
2915 SI->setFastMathFlags(FMF1 | FMF2);
2916 return SI;
2917 }
2918 // fabs (select Cond, -FVal, FVal) --> fabs FVal
2919 if (match(TVal, m_FNeg(m_Specific(FVal))))
2920 return replaceOperand(*II, 0, FVal);
2921 // fabs (select Cond, TVal, -TVal) --> fabs TVal
2922 if (match(FVal, m_FNeg(m_Specific(TVal))))
2923 return replaceOperand(*II, 0, TVal);
2924 }
2925
2926 Value *Magnitude, *Sign;
2927 if (match(II->getArgOperand(0),
2928 m_CopySign(m_Value(Magnitude), m_Value(Sign)))) {
2929 // fabs (copysign x, y) -> (fabs x)
2930 CallInst *AbsSign =
2931 Builder.CreateUnaryIntrinsic(Intrinsic::fabs, Magnitude, II);
2932 return replaceInstUsesWith(*II, AbsSign);
2933 }
2934
2935 [[fallthrough]];
2936 }
2937 case Intrinsic::ceil:
2938 case Intrinsic::floor:
2939 case Intrinsic::round:
2940 case Intrinsic::roundeven:
2941 case Intrinsic::nearbyint:
2942 case Intrinsic::rint:
2943 case Intrinsic::trunc: {
2944 Value *ExtSrc;
2945 if (match(II->getArgOperand(0), m_OneUse(m_FPExt(m_Value(ExtSrc))))) {
2946 // Narrow the call: intrinsic (fpext x) -> fpext (intrinsic x)
2947 Value *NarrowII = Builder.CreateUnaryIntrinsic(IID, ExtSrc, II);
2948 return new FPExtInst(NarrowII, II->getType());
2949 }
2950 break;
2951 }
2952 case Intrinsic::cos:
2953 case Intrinsic::amdgcn_cos: {
2954 Value *X, *Sign;
2955 Value *Src = II->getArgOperand(0);
2956 if (match(Src, m_FNeg(m_Value(X))) || match(Src, m_FAbs(m_Value(X))) ||
2957 match(Src, m_CopySign(m_Value(X), m_Value(Sign)))) {
2958 // cos(-x) --> cos(x)
2959 // cos(fabs(x)) --> cos(x)
2960 // cos(copysign(x, y)) --> cos(x)
2961 return replaceOperand(*II, 0, X);
2962 }
2963 break;
2964 }
2965 case Intrinsic::sin:
2966 case Intrinsic::amdgcn_sin: {
2967 Value *X;
2968 if (match(II->getArgOperand(0), m_OneUse(m_FNeg(m_Value(X))))) {
2969 // sin(-x) --> -sin(x)
2970 Value *NewSin = Builder.CreateUnaryIntrinsic(IID, X, II);
2971 return UnaryOperator::CreateFNegFMF(NewSin, II);
2972 }
2973 break;
2974 }
2975 case Intrinsic::ldexp: {
2976 // ldexp(ldexp(x, a), b) -> ldexp(x, a + b)
2977 //
2978 // The danger is if the first ldexp would overflow to infinity or underflow
2979 // to zero, but the combined exponent avoids it. We ignore this with
2980 // reassoc.
2981 //
2982 // It's also safe to fold if we know both exponents are >= 0 or <= 0 since
2983 // it would just double down on the overflow/underflow which would occur
2984 // anyway.
2985 //
2986 // TODO: Could do better if we had range tracking for the input value
2987 // exponent. Also could broaden sign check to cover == 0 case.
2988 Value *Src = II->getArgOperand(0);
2989 Value *Exp = II->getArgOperand(1);
2990 Value *InnerSrc;
2991 Value *InnerExp;
2992 if (match(Src, m_OneUse(m_Intrinsic<Intrinsic::ldexp>(
2993 m_Value(InnerSrc), m_Value(InnerExp)))) &&
2994 Exp->getType() == InnerExp->getType()) {
2995 FastMathFlags FMF = II->getFastMathFlags();
2996 FastMathFlags InnerFlags = cast<FPMathOperator>(Src)->getFastMathFlags();
2997
2998 if ((FMF.allowReassoc() && InnerFlags.allowReassoc()) ||
2999 signBitMustBeTheSame(Exp, InnerExp, SQ.getWithInstruction(II))) {
3000 // TODO: Add nsw/nuw probably safe if integer type exceeds exponent
3001 // width.
3002 Value *NewExp = Builder.CreateAdd(InnerExp, Exp);
3003 II->setArgOperand(1, NewExp);
3004 II->setFastMathFlags(InnerFlags); // Or the inner flags.
3005 return replaceOperand(*II, 0, InnerSrc);
3006 }
3007 }
3008
3009 // ldexp(x, zext(i1 y)) -> fmul x, (select y, 2.0, 1.0)
3010 // ldexp(x, sext(i1 y)) -> fmul x, (select y, 0.5, 1.0)
3011 Value *ExtSrc;
3012 if (match(Exp, m_ZExt(m_Value(ExtSrc))) &&
3013 ExtSrc->getType()->getScalarSizeInBits() == 1) {
3014 Value *Select =
3015 Builder.CreateSelect(ExtSrc, ConstantFP::get(II->getType(), 2.0),
3016 ConstantFP::get(II->getType(), 1.0));
3018 }
3019 if (match(Exp, m_SExt(m_Value(ExtSrc))) &&
3020 ExtSrc->getType()->getScalarSizeInBits() == 1) {
3021 Value *Select =
3022 Builder.CreateSelect(ExtSrc, ConstantFP::get(II->getType(), 0.5),
3023 ConstantFP::get(II->getType(), 1.0));
3025 }
3026
3027 // ldexp(x, c ? exp : 0) -> c ? ldexp(x, exp) : x
3028 // ldexp(x, c ? 0 : exp) -> c ? x : ldexp(x, exp)
3029 ///
3030 // TODO: If we cared, should insert a canonicalize for x
3031 Value *SelectCond, *SelectLHS, *SelectRHS;
3032 if (match(II->getArgOperand(1),
3033 m_OneUse(m_Select(m_Value(SelectCond), m_Value(SelectLHS),
3034 m_Value(SelectRHS))))) {
3035 Value *NewLdexp = nullptr;
3036 Value *Select = nullptr;
3037 if (match(SelectRHS, m_ZeroInt())) {
3038 NewLdexp = Builder.CreateLdexp(Src, SelectLHS, II);
3039 Select = Builder.CreateSelect(SelectCond, NewLdexp, Src);
3040 } else if (match(SelectLHS, m_ZeroInt())) {
3041 NewLdexp = Builder.CreateLdexp(Src, SelectRHS, II);
3042 Select = Builder.CreateSelect(SelectCond, Src, NewLdexp);
3043 }
3044
3045 if (NewLdexp) {
3046 Select->takeName(II);
3047 return replaceInstUsesWith(*II, Select);
3048 }
3049 }
3050
3051 break;
3052 }
3053 case Intrinsic::ptrauth_auth:
3054 case Intrinsic::ptrauth_resign: {
3055 // (sign|resign) + (auth|resign) can be folded by omitting the middle
3056 // sign+auth component if the key and discriminator match.
3057 bool NeedSign = II->getIntrinsicID() == Intrinsic::ptrauth_resign;
3058 Value *Ptr = II->getArgOperand(0);
3059 Value *Key = II->getArgOperand(1);
3060 Value *Disc = II->getArgOperand(2);
3061
3062 // AuthKey will be the key we need to end up authenticating against in
3063 // whatever we replace this sequence with.
3064 Value *AuthKey = nullptr, *AuthDisc = nullptr, *BasePtr;
3065 if (const auto *CI = dyn_cast<CallBase>(Ptr)) {
3066 BasePtr = CI->getArgOperand(0);
3067 if (CI->getIntrinsicID() == Intrinsic::ptrauth_sign) {
3068 if (CI->getArgOperand(1) != Key || CI->getArgOperand(2) != Disc)
3069 break;
3070 } else if (CI->getIntrinsicID() == Intrinsic::ptrauth_resign) {
3071 if (CI->getArgOperand(3) != Key || CI->getArgOperand(4) != Disc)
3072 break;
3073 AuthKey = CI->getArgOperand(1);
3074 AuthDisc = CI->getArgOperand(2);
3075 } else
3076 break;
3077 } else if (const auto *PtrToInt = dyn_cast<PtrToIntOperator>(Ptr)) {
3078 // ptrauth constants are equivalent to a call to @llvm.ptrauth.sign for
3079 // our purposes, so check for that too.
3080 const auto *CPA = dyn_cast<ConstantPtrAuth>(PtrToInt->getOperand(0));
3081 if (!CPA || !CPA->isKnownCompatibleWith(Key, Disc, DL))
3082 break;
3083
3084 // resign(ptrauth(p,ks,ds),ks,ds,kr,dr) -> ptrauth(p,kr,dr)
3085 if (NeedSign && isa<ConstantInt>(II->getArgOperand(4))) {
3086 auto *SignKey = cast<ConstantInt>(II->getArgOperand(3));
3087 auto *SignDisc = cast<ConstantInt>(II->getArgOperand(4));
3088 auto *SignAddrDisc = ConstantPointerNull::get(Builder.getPtrTy());
3089 auto *NewCPA = ConstantPtrAuth::get(CPA->getPointer(), SignKey,
3090 SignDisc, SignAddrDisc);
3092 *II, ConstantExpr::getPointerCast(NewCPA, II->getType()));
3093 return eraseInstFromFunction(*II);
3094 }
3095
3096 // auth(ptrauth(p,k,d),k,d) -> p
3097 BasePtr = Builder.CreatePtrToInt(CPA->getPointer(), II->getType());
3098 } else
3099 break;
3100
3101 unsigned NewIntrin;
3102 if (AuthKey && NeedSign) {
3103 // resign(0,1) + resign(1,2) = resign(0, 2)
3104 NewIntrin = Intrinsic::ptrauth_resign;
3105 } else if (AuthKey) {
3106 // resign(0,1) + auth(1) = auth(0)
3107 NewIntrin = Intrinsic::ptrauth_auth;
3108 } else if (NeedSign) {
3109 // sign(0) + resign(0, 1) = sign(1)
3110 NewIntrin = Intrinsic::ptrauth_sign;
3111 } else {
3112 // sign(0) + auth(0) = nop
3113 replaceInstUsesWith(*II, BasePtr);
3114 return eraseInstFromFunction(*II);
3115 }
3116
3117 SmallVector<Value *, 4> CallArgs;
3118 CallArgs.push_back(BasePtr);
3119 if (AuthKey) {
3120 CallArgs.push_back(AuthKey);
3121 CallArgs.push_back(AuthDisc);
3122 }
3123
3124 if (NeedSign) {
3125 CallArgs.push_back(II->getArgOperand(3));
3126 CallArgs.push_back(II->getArgOperand(4));
3127 }
3128
3129 Function *NewFn =
3130 Intrinsic::getOrInsertDeclaration(II->getModule(), NewIntrin);
3131 return CallInst::Create(NewFn, CallArgs);
3132 }
3133 case Intrinsic::arm_neon_vtbl1:
3134 case Intrinsic::aarch64_neon_tbl1:
3135 if (Value *V = simplifyNeonTbl1(*II, Builder))
3136 return replaceInstUsesWith(*II, V);
3137 break;
3138
3139 case Intrinsic::arm_neon_vmulls:
3140 case Intrinsic::arm_neon_vmullu:
3141 case Intrinsic::aarch64_neon_smull:
3142 case Intrinsic::aarch64_neon_umull: {
3143 Value *Arg0 = II->getArgOperand(0);
3144 Value *Arg1 = II->getArgOperand(1);
3145
3146 // Handle mul by zero first:
3147 if (isa<ConstantAggregateZero>(Arg0) || isa<ConstantAggregateZero>(Arg1)) {
3148 return replaceInstUsesWith(CI, ConstantAggregateZero::get(II->getType()));
3149 }
3150
3151 // Check for constant LHS & RHS - in this case we just simplify.
3152 bool Zext = (IID == Intrinsic::arm_neon_vmullu ||
3153 IID == Intrinsic::aarch64_neon_umull);
3154 VectorType *NewVT = cast<VectorType>(II->getType());
3155 if (Constant *CV0 = dyn_cast<Constant>(Arg0)) {
3156 if (Constant *CV1 = dyn_cast<Constant>(Arg1)) {
3157 Value *V0 = Builder.CreateIntCast(CV0, NewVT, /*isSigned=*/!Zext);
3158 Value *V1 = Builder.CreateIntCast(CV1, NewVT, /*isSigned=*/!Zext);
3159 return replaceInstUsesWith(CI, Builder.CreateMul(V0, V1));
3160 }
3161
3162 // Couldn't simplify - canonicalize constant to the RHS.
3163 std::swap(Arg0, Arg1);
3164 }
3165
3166 // Handle mul by one:
3167 if (Constant *CV1 = dyn_cast<Constant>(Arg1))
3168 if (ConstantInt *Splat =
3169 dyn_cast_or_null<ConstantInt>(CV1->getSplatValue()))
3170 if (Splat->isOne())
3171 return CastInst::CreateIntegerCast(Arg0, II->getType(),
3172 /*isSigned=*/!Zext);
3173
3174 break;
3175 }
3176 case Intrinsic::arm_neon_aesd:
3177 case Intrinsic::arm_neon_aese:
3178 case Intrinsic::aarch64_crypto_aesd:
3179 case Intrinsic::aarch64_crypto_aese:
3180 case Intrinsic::aarch64_sve_aesd:
3181 case Intrinsic::aarch64_sve_aese: {
3182 Value *DataArg = II->getArgOperand(0);
3183 Value *KeyArg = II->getArgOperand(1);
3184
3185 // Accept zero on either operand.
3186 if (!match(KeyArg, m_ZeroInt()))
3187 std::swap(KeyArg, DataArg);
3188
3189 // Try to use the builtin XOR in AESE and AESD to eliminate a prior XOR
3190 Value *Data, *Key;
3191 if (match(KeyArg, m_ZeroInt()) &&
3192 match(DataArg, m_Xor(m_Value(Data), m_Value(Key)))) {
3193 replaceOperand(*II, 0, Data);
3194 replaceOperand(*II, 1, Key);
3195 return II;
3196 }
3197 break;
3198 }
3199 case Intrinsic::hexagon_V6_vandvrt:
3200 case Intrinsic::hexagon_V6_vandvrt_128B: {
3201 // Simplify Q -> V -> Q conversion.
3202 if (auto Op0 = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
3203 Intrinsic::ID ID0 = Op0->getIntrinsicID();
3204 if (ID0 != Intrinsic::hexagon_V6_vandqrt &&
3205 ID0 != Intrinsic::hexagon_V6_vandqrt_128B)
3206 break;
3207 Value *Bytes = Op0->getArgOperand(1), *Mask = II->getArgOperand(1);
3208 uint64_t Bytes1 = computeKnownBits(Bytes, Op0).One.getZExtValue();
3209 uint64_t Mask1 = computeKnownBits(Mask, II).One.getZExtValue();
3210 // Check if every byte has common bits in Bytes and Mask.
3211 uint64_t C = Bytes1 & Mask1;
3212 if ((C & 0xFF) && (C & 0xFF00) && (C & 0xFF0000) && (C & 0xFF000000))
3213 return replaceInstUsesWith(*II, Op0->getArgOperand(0));
3214 }
3215 break;
3216 }
3217 case Intrinsic::stackrestore: {
3218 enum class ClassifyResult {
3219 None,
3220 Alloca,
3221 StackRestore,
3222 CallWithSideEffects,
3223 };
3224 auto Classify = [](const Instruction *I) {
3225 if (isa<AllocaInst>(I))
3226 return ClassifyResult::Alloca;
3227
3228 if (auto *CI = dyn_cast<CallInst>(I)) {
3229 if (auto *II = dyn_cast<IntrinsicInst>(CI)) {
3230 if (II->getIntrinsicID() == Intrinsic::stackrestore)
3231 return ClassifyResult::StackRestore;
3232
3233 if (II->mayHaveSideEffects())
3234 return ClassifyResult::CallWithSideEffects;
3235 } else {
3236 // Consider all non-intrinsic calls to be side effects
3237 return ClassifyResult::CallWithSideEffects;
3238 }
3239 }
3240
3241 return ClassifyResult::None;
3242 };
3243
3244 // If the stacksave and the stackrestore are in the same BB, and there is
3245 // no intervening call, alloca, or stackrestore of a different stacksave,
3246 // remove the restore. This can happen when variable allocas are DCE'd.
3247 if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
3248 if (SS->getIntrinsicID() == Intrinsic::stacksave &&
3249 SS->getParent() == II->getParent()) {
3250 BasicBlock::iterator BI(SS);
3251 bool CannotRemove = false;
3252 for (++BI; &*BI != II; ++BI) {
3253 switch (Classify(&*BI)) {
3254 case ClassifyResult::None:
3255 // So far so good, look at next instructions.
3256 break;
3257
3258 case ClassifyResult::StackRestore:
3259 // If we found an intervening stackrestore for a different
3260 // stacksave, we can't remove the stackrestore. Otherwise, continue.
3261 if (cast<IntrinsicInst>(*BI).getArgOperand(0) != SS)
3262 CannotRemove = true;
3263 break;
3264
3265 case ClassifyResult::Alloca:
3266 case ClassifyResult::CallWithSideEffects:
3267 // If we found an alloca, a non-intrinsic call, or an intrinsic
3268 // call with side effects, we can't remove the stackrestore.
3269 CannotRemove = true;
3270 break;
3271 }
3272 if (CannotRemove)
3273 break;
3274 }
3275
3276 if (!CannotRemove)
3277 return eraseInstFromFunction(CI);
3278 }
3279 }
3280
3281 // Scan down this block to see if there is another stack restore in the
3282 // same block without an intervening call/alloca.
3284 Instruction *TI = II->getParent()->getTerminator();
3285 bool CannotRemove = false;
3286 for (++BI; &*BI != TI; ++BI) {
3287 switch (Classify(&*BI)) {
3288 case ClassifyResult::None:
3289 // So far so good, look at next instructions.
3290 break;
3291
3292 case ClassifyResult::StackRestore:
3293 // If there is a stackrestore below this one, remove this one.
3294 return eraseInstFromFunction(CI);
3295
3296 case ClassifyResult::Alloca:
3297 case ClassifyResult::CallWithSideEffects:
3298 // If we found an alloca, a non-intrinsic call, or an intrinsic call
3299 // with side effects (such as llvm.stacksave and llvm.read_register),
3300 // we can't remove the stack restore.
3301 CannotRemove = true;
3302 break;
3303 }
3304 if (CannotRemove)
3305 break;
3306 }
3307
3308 // If the stack restore is in a return, resume, or unwind block and if there
3309 // are no allocas or calls between the restore and the return, nuke the
3310 // restore.
3311 if (!CannotRemove && (isa<ReturnInst>(TI) || isa<ResumeInst>(TI)))
3312 return eraseInstFromFunction(CI);
3313 break;
3314 }
3315 case Intrinsic::lifetime_end:
3316 // Asan needs to poison memory to detect invalid access which is possible
3317 // even for empty lifetime range.
3318 if (II->getFunction()->hasFnAttribute(Attribute::SanitizeAddress) ||
3319 II->getFunction()->hasFnAttribute(Attribute::SanitizeMemory) ||
3320 II->getFunction()->hasFnAttribute(Attribute::SanitizeHWAddress))
3321 break;
3322
3323 if (removeTriviallyEmptyRange(*II, *this, [](const IntrinsicInst &I) {
3324 return I.getIntrinsicID() == Intrinsic::lifetime_start;
3325 }))
3326 return nullptr;
3327 break;
3328 case Intrinsic::assume: {
3329 Value *IIOperand = II->getArgOperand(0);
3331 II->getOperandBundlesAsDefs(OpBundles);
3332
3333 /// This will remove the boolean Condition from the assume given as
3334 /// argument and remove the assume if it becomes useless.
3335 /// always returns nullptr for use as a return values.
3336 auto RemoveConditionFromAssume = [&](Instruction *Assume) -> Instruction * {
3337 assert(isa<AssumeInst>(Assume));
3338 if (isAssumeWithEmptyBundle(*cast<AssumeInst>(II)))
3339 return eraseInstFromFunction(CI);
3340 replaceUse(II->getOperandUse(0), ConstantInt::getTrue(II->getContext()));
3341 return nullptr;
3342 };
3343 // Remove an assume if it is followed by an identical assume.
3344 // TODO: Do we need this? Unless there are conflicting assumptions, the
3345 // computeKnownBits(IIOperand) below here eliminates redundant assumes.
3346 Instruction *Next = II->getNextNode();
3347 if (match(Next, m_Intrinsic<Intrinsic::assume>(m_Specific(IIOperand))))
3348 return RemoveConditionFromAssume(Next);
3349
3350 // Canonicalize assume(a && b) -> assume(a); assume(b);
3351 // Note: New assumption intrinsics created here are registered by
3352 // the InstCombineIRInserter object.
3353 FunctionType *AssumeIntrinsicTy = II->getFunctionType();
3354 Value *AssumeIntrinsic = II->getCalledOperand();
3355 Value *A, *B;
3356 if (match(IIOperand, m_LogicalAnd(m_Value(A), m_Value(B)))) {
3357 Builder.CreateCall(AssumeIntrinsicTy, AssumeIntrinsic, A, OpBundles,
3358 II->getName());
3359 Builder.CreateCall(AssumeIntrinsicTy, AssumeIntrinsic, B, II->getName());
3360 return eraseInstFromFunction(*II);
3361 }
3362 // assume(!(a || b)) -> assume(!a); assume(!b);
3363 if (match(IIOperand, m_Not(m_LogicalOr(m_Value(A), m_Value(B))))) {
3364 Builder.CreateCall(AssumeIntrinsicTy, AssumeIntrinsic,
3365 Builder.CreateNot(A), OpBundles, II->getName());
3366 Builder.CreateCall(AssumeIntrinsicTy, AssumeIntrinsic,
3367 Builder.CreateNot(B), II->getName());
3368 return eraseInstFromFunction(*II);
3369 }
3370
3371 // assume( (load addr) != null ) -> add 'nonnull' metadata to load
3372 // (if assume is valid at the load)
3375 m_Zero())) &&
3376 LHS->getOpcode() == Instruction::Load &&
3377 LHS->getType()->isPointerTy() &&
3379 MDNode *MD = MDNode::get(II->getContext(), {});
3380 LHS->setMetadata(LLVMContext::MD_nonnull, MD);
3381 LHS->setMetadata(LLVMContext::MD_noundef, MD);
3382 return RemoveConditionFromAssume(II);
3383
3384 // TODO: apply nonnull return attributes to calls and invokes
3385 // TODO: apply range metadata for range check patterns?
3386 }
3387
3388 // Separate storage assumptions apply to the underlying allocations, not any
3389 // particular pointer within them. When evaluating the hints for AA purposes
3390 // we getUnderlyingObject them; by precomputing the answers here we can
3391 // avoid having to do so repeatedly there.
3392 for (unsigned Idx = 0; Idx < II->getNumOperandBundles(); Idx++) {
3393 OperandBundleUse OBU = II->getOperandBundleAt(Idx);
3394 if (OBU.getTagName() == "separate_storage") {
3395 assert(OBU.Inputs.size() == 2);
3396 auto MaybeSimplifyHint = [&](const Use &U) {
3397 Value *Hint = U.get();
3398 // Not having a limit is safe because InstCombine removes unreachable
3399 // code.
3400 Value *UnderlyingObject = getUnderlyingObject(Hint, /*MaxLookup*/ 0);
3401 if (Hint != UnderlyingObject)
3402 replaceUse(const_cast<Use &>(U), UnderlyingObject);
3403 };
3404 MaybeSimplifyHint(OBU.Inputs[0]);
3405 MaybeSimplifyHint(OBU.Inputs[1]);
3406 }
3407 }
3408
3409 // Convert nonnull assume like:
3410 // %A = icmp ne i32* %PTR, null
3411 // call void @llvm.assume(i1 %A)
3412 // into
3413 // call void @llvm.assume(i1 true) [ "nonnull"(i32* %PTR) ]
3415 match(IIOperand,
3417 A->getType()->isPointerTy()) {
3418 if (auto *Replacement = buildAssumeFromKnowledge(
3419 {RetainedKnowledge{Attribute::NonNull, 0, A}}, Next, &AC, &DT)) {
3420
3421 Replacement->insertBefore(Next->getIterator());
3422 AC.registerAssumption(Replacement);
3423 return RemoveConditionFromAssume(II);
3424 }
3425 }
3426
3427 // Convert alignment assume like:
3428 // %B = ptrtoint i32* %A to i64
3429 // %C = and i64 %B, Constant
3430 // %D = icmp eq i64 %C, 0
3431 // call void @llvm.assume(i1 %D)
3432 // into
3433 // call void @llvm.assume(i1 true) [ "align"(i32* [[A]], i64 Constant + 1)]
3434 uint64_t AlignMask = 1;
3436 (match(IIOperand, m_Not(m_Trunc(m_Value(A)))) ||
3437 match(IIOperand,
3439 m_And(m_Value(A), m_ConstantInt(AlignMask)),
3440 m_Zero())))) {
3441 if (isPowerOf2_64(AlignMask + 1)) {
3442 uint64_t Offset = 0;
3444 if (match(A, m_PtrToInt(m_Value(A)))) {
3445 /// Note: this doesn't preserve the offset information but merges
3446 /// offset and alignment.
3447 /// TODO: we can generate a GEP instead of merging the alignment with
3448 /// the offset.
3449 RetainedKnowledge RK{Attribute::Alignment,
3450 (unsigned)MinAlign(Offset, AlignMask + 1), A};
3451 if (auto *Replacement =
3452 buildAssumeFromKnowledge(RK, Next, &AC, &DT)) {
3453
3454 Replacement->insertAfter(II->getIterator());
3455 AC.registerAssumption(Replacement);
3456 }
3457 return RemoveConditionFromAssume(II);
3458 }
3459 }
3460 }
3461
3462 /// Canonicalize Knowledge in operand bundles.
3463 if (EnableKnowledgeRetention && II->hasOperandBundles()) {
3464 for (unsigned Idx = 0; Idx < II->getNumOperandBundles(); Idx++) {
3465 auto &BOI = II->bundle_op_info_begin()[Idx];
3467 llvm::getKnowledgeFromBundle(cast<AssumeInst>(*II), BOI);
3468 if (BOI.End - BOI.Begin > 2)
3469 continue; // Prevent reducing knowledge in an align with offset since
3470 // extracting a RetainedKnowledge from them looses offset
3471 // information
3472 RetainedKnowledge CanonRK =
3473 llvm::simplifyRetainedKnowledge(cast<AssumeInst>(II), RK,
3475 &getDominatorTree());
3476 if (CanonRK == RK)
3477 continue;
3478 if (!CanonRK) {
3479 if (BOI.End - BOI.Begin > 0) {
3480 Worklist.pushValue(II->op_begin()[BOI.Begin]);
3481 Value::dropDroppableUse(II->op_begin()[BOI.Begin]);
3482 }
3483 continue;
3484 }
3485 assert(RK.AttrKind == CanonRK.AttrKind);
3486 if (BOI.End - BOI.Begin > 0)
3487 II->op_begin()[BOI.Begin].set(CanonRK.WasOn);
3488 if (BOI.End - BOI.Begin > 1)
3489 II->op_begin()[BOI.Begin + 1].set(ConstantInt::get(
3490 Type::getInt64Ty(II->getContext()), CanonRK.ArgValue));
3491 if (RK.WasOn)
3493 return II;
3494 }
3495 }
3496
3497 // If there is a dominating assume with the same condition as this one,
3498 // then this one is redundant, and should be removed.
3499 KnownBits Known(1);
3500 computeKnownBits(IIOperand, Known, II);
3501 if (Known.isAllOnes() && isAssumeWithEmptyBundle(cast<AssumeInst>(*II)))
3502 return eraseInstFromFunction(*II);
3503
3504 // assume(false) is unreachable.
3505 if (match(IIOperand, m_CombineOr(m_Zero(), m_Undef()))) {
3507 return eraseInstFromFunction(*II);
3508 }
3509
3510 // Update the cache of affected values for this assumption (we might be
3511 // here because we just simplified the condition).
3512 AC.updateAffectedValues(cast<AssumeInst>(II));
3513 break;
3514 }
3515 case Intrinsic::experimental_guard: {
3516 // Is this guard followed by another guard? We scan forward over a small
3517 // fixed window of instructions to handle common cases with conditions
3518 // computed between guards.
3519 Instruction *NextInst = II->getNextNode();
3520 for (unsigned i = 0; i < GuardWideningWindow; i++) {
3521 // Note: Using context-free form to avoid compile time blow up
3522 if (!isSafeToSpeculativelyExecute(NextInst))
3523 break;
3524 NextInst = NextInst->getNextNode();
3525 }
3526 Value *NextCond = nullptr;
3527 if (match(NextInst,
3528 m_Intrinsic<Intrinsic::experimental_guard>(m_Value(NextCond)))) {
3529 Value *CurrCond = II->getArgOperand(0);
3530
3531 // Remove a guard that it is immediately preceded by an identical guard.
3532 // Otherwise canonicalize guard(a); guard(b) -> guard(a & b).
3533 if (CurrCond != NextCond) {
3534 Instruction *MoveI = II->getNextNode();
3535 while (MoveI != NextInst) {
3536 auto *Temp = MoveI;
3537 MoveI = MoveI->getNextNode();
3538 Temp->moveBefore(II->getIterator());
3539 }
3540 replaceOperand(*II, 0, Builder.CreateAnd(CurrCond, NextCond));
3541 }
3542 eraseInstFromFunction(*NextInst);
3543 return II;
3544 }
3545 break;
3546 }
3547 case Intrinsic::vector_insert: {
3548 Value *Vec = II->getArgOperand(0);
3549 Value *SubVec = II->getArgOperand(1);
3550 Value *Idx = II->getArgOperand(2);
3551 auto *DstTy = dyn_cast<FixedVectorType>(II->getType());
3552 auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType());
3553 auto *SubVecTy = dyn_cast<FixedVectorType>(SubVec->getType());
3554
3555 // Only canonicalize if the destination vector, Vec, and SubVec are all
3556 // fixed vectors.
3557 if (DstTy && VecTy && SubVecTy) {
3558 unsigned DstNumElts = DstTy->getNumElements();
3559 unsigned VecNumElts = VecTy->getNumElements();
3560 unsigned SubVecNumElts = SubVecTy->getNumElements();
3561 unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
3562
3563 // An insert that entirely overwrites Vec with SubVec is a nop.
3564 if (VecNumElts == SubVecNumElts)
3565 return replaceInstUsesWith(CI, SubVec);
3566
3567 // Widen SubVec into a vector of the same width as Vec, since
3568 // shufflevector requires the two input vectors to be the same width.
3569 // Elements beyond the bounds of SubVec within the widened vector are
3570 // undefined.
3571 SmallVector<int, 8> WidenMask;
3572 unsigned i;
3573 for (i = 0; i != SubVecNumElts; ++i)
3574 WidenMask.push_back(i);
3575 for (; i != VecNumElts; ++i)
3576 WidenMask.push_back(PoisonMaskElem);
3577
3578 Value *WidenShuffle = Builder.CreateShuffleVector(SubVec, WidenMask);
3579
3581 for (unsigned i = 0; i != IdxN; ++i)
3582 Mask.push_back(i);
3583 for (unsigned i = DstNumElts; i != DstNumElts + SubVecNumElts; ++i)
3584 Mask.push_back(i);
3585 for (unsigned i = IdxN + SubVecNumElts; i != DstNumElts; ++i)
3586 Mask.push_back(i);
3587
3588 Value *Shuffle = Builder.CreateShuffleVector(Vec, WidenShuffle, Mask);
3589 return replaceInstUsesWith(CI, Shuffle);
3590 }
3591 break;
3592 }
3593 case Intrinsic::vector_extract: {
3594 Value *Vec = II->getArgOperand(0);
3595 Value *Idx = II->getArgOperand(1);
3596
3597 Type *ReturnType = II->getType();
3598 // (extract_vector (insert_vector InsertTuple, InsertValue, InsertIdx),
3599 // ExtractIdx)
3600 unsigned ExtractIdx = cast<ConstantInt>(Idx)->getZExtValue();
3601 Value *InsertTuple, *InsertIdx, *InsertValue;
3602 if (match(Vec, m_Intrinsic<Intrinsic::vector_insert>(m_Value(InsertTuple),
3603 m_Value(InsertValue),
3604 m_Value(InsertIdx))) &&
3605 InsertValue->getType() == ReturnType) {
3606 unsigned Index = cast<ConstantInt>(InsertIdx)->getZExtValue();
3607 // Case where we get the same index right after setting it.
3608 // extract.vector(insert.vector(InsertTuple, InsertValue, Idx), Idx) -->
3609 // InsertValue
3610 if (ExtractIdx == Index)
3611 return replaceInstUsesWith(CI, InsertValue);
3612 // If we are getting a different index than what was set in the
3613 // insert.vector intrinsic. We can just set the input tuple to the one up
3614 // in the chain. extract.vector(insert.vector(InsertTuple, InsertValue,
3615 // InsertIndex), ExtractIndex)
3616 // --> extract.vector(InsertTuple, ExtractIndex)
3617 else
3618 return replaceOperand(CI, 0, InsertTuple);
3619 }
3620
3621 auto *DstTy = dyn_cast<VectorType>(ReturnType);
3622 auto *VecTy = dyn_cast<VectorType>(Vec->getType());
3623
3624 if (DstTy && VecTy) {
3625 auto DstEltCnt = DstTy->getElementCount();
3626 auto VecEltCnt = VecTy->getElementCount();
3627 unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
3628
3629 // Extracting the entirety of Vec is a nop.
3630 if (DstEltCnt == VecTy->getElementCount()) {
3631 replaceInstUsesWith(CI, Vec);
3632 return eraseInstFromFunction(CI);
3633 }
3634
3635 // Only canonicalize to shufflevector if the destination vector and
3636 // Vec are fixed vectors.
3637 if (VecEltCnt.isScalable() || DstEltCnt.isScalable())
3638 break;
3639
3641 for (unsigned i = 0; i != DstEltCnt.getKnownMinValue(); ++i)
3642 Mask.push_back(IdxN + i);
3643
3644 Value *Shuffle = Builder.CreateShuffleVector(Vec, Mask);
3645 return replaceInstUsesWith(CI, Shuffle);
3646 }
3647 break;
3648 }
3649 case Intrinsic::experimental_vp_reverse: {
3650 Value *X;
3651 Value *Vec = II->getArgOperand(0);
3652 Value *Mask = II->getArgOperand(1);
3653 if (!match(Mask, m_AllOnes()))
3654 break;
3655 Value *EVL = II->getArgOperand(2);
3656 // TODO: Canonicalize experimental.vp.reverse after unop/binops?
3657 // rev(unop rev(X)) --> unop X
3658 if (match(Vec,
3659 m_OneUse(m_UnOp(m_Intrinsic<Intrinsic::experimental_vp_reverse>(
3660 m_Value(X), m_AllOnes(), m_Specific(EVL)))))) {
3661 auto *OldUnOp = cast<UnaryOperator>(Vec);
3663 OldUnOp->getOpcode(), X, OldUnOp, OldUnOp->getName(),
3664 II->getIterator());
3665 return replaceInstUsesWith(CI, NewUnOp);
3666 }
3667 break;
3668 }
3669 case Intrinsic::vector_reduce_or:
3670 case Intrinsic::vector_reduce_and: {
3671 // Canonicalize logical or/and reductions:
3672 // Or reduction for i1 is represented as:
3673 // %val = bitcast <ReduxWidth x i1> to iReduxWidth
3674 // %res = cmp ne iReduxWidth %val, 0
3675 // And reduction for i1 is represented as:
3676 // %val = bitcast <ReduxWidth x i1> to iReduxWidth
3677 // %res = cmp eq iReduxWidth %val, 11111
3678 Value *Arg = II->getArgOperand(0);
3679 Value *Vect;
3680
3681 if (Value *NewOp =
3682 simplifyReductionOperand(Arg, /*CanReorderLanes=*/true)) {
3683 replaceUse(II->getOperandUse(0), NewOp);
3684 return II;
3685 }
3686
3687 if (match(Arg, m_ZExtOrSExtOrSelf(m_Value(Vect)))) {
3688 if (auto *FTy = dyn_cast<FixedVectorType>(Vect->getType()))
3689 if (FTy->getElementType() == Builder.getInt1Ty()) {
3691 Vect, Builder.getIntNTy(FTy->getNumElements()));
3692 if (IID == Intrinsic::vector_reduce_and) {
3693 Res = Builder.CreateICmpEQ(
3695 } else {
3696 assert(IID == Intrinsic::vector_reduce_or &&
3697 "Expected or reduction.");
3698 Res = Builder.CreateIsNotNull(Res);
3699 }
3700 if (Arg != Vect)
3701 Res = Builder.CreateCast(cast<CastInst>(Arg)->getOpcode(), Res,
3702 II->getType());
3703 return replaceInstUsesWith(CI, Res);
3704 }
3705 }
3706 [[fallthrough]];
3707 }
3708 case Intrinsic::vector_reduce_add: {
3709 if (IID == Intrinsic::vector_reduce_add) {
3710 // Convert vector_reduce_add(ZExt(<n x i1>)) to
3711 // ZExtOrTrunc(ctpop(bitcast <n x i1> to in)).
3712 // Convert vector_reduce_add(SExt(<n x i1>)) to
3713 // -ZExtOrTrunc(ctpop(bitcast <n x i1> to in)).
3714 // Convert vector_reduce_add(<n x i1>) to
3715 // Trunc(ctpop(bitcast <n x i1> to in)).
3716 Value *Arg = II->getArgOperand(0);
3717 Value *Vect;
3718
3719 if (Value *NewOp =
3720 simplifyReductionOperand(Arg, /*CanReorderLanes=*/true)) {
3721 replaceUse(II->getOperandUse(0), NewOp);
3722 return II;
3723 }
3724
3725 if (match(Arg, m_ZExtOrSExtOrSelf(m_Value(Vect)))) {
3726 if (auto *FTy = dyn_cast<FixedVectorType>(Vect->getType()))
3727 if (FTy->getElementType() == Builder.getInt1Ty()) {
3729 Vect, Builder.getIntNTy(FTy->getNumElements()));
3730 Value *Res = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, V);
3731 if (Res->getType() != II->getType())
3732 Res = Builder.CreateZExtOrTrunc(Res, II->getType());
3733 if (Arg != Vect &&
3734 cast<Instruction>(Arg)->getOpcode() == Instruction::SExt)
3735 Res = Builder.CreateNeg(Res);
3736 return replaceInstUsesWith(CI, Res);
3737 }
3738 }
3739 }
3740 [[fallthrough]];
3741 }
3742 case Intrinsic::vector_reduce_xor: {
3743 if (IID == Intrinsic::vector_reduce_xor) {
3744 // Exclusive disjunction reduction over the vector with
3745 // (potentially-extended) i1 element type is actually a
3746 // (potentially-extended) arithmetic `add` reduction over the original
3747 // non-extended value:
3748 // vector_reduce_xor(?ext(<n x i1>))
3749 // -->
3750 // ?ext(vector_reduce_add(<n x i1>))
3751 Value *Arg = II->getArgOperand(0);
3752 Value *Vect;
3753
3754 if (Value *NewOp =
3755 simplifyReductionOperand(Arg, /*CanReorderLanes=*/true)) {
3756 replaceUse(II->getOperandUse(0), NewOp);
3757 return II;
3758 }
3759
3760 if (match(Arg, m_ZExtOrSExtOrSelf(m_Value(Vect)))) {
3761 if (auto *VTy = dyn_cast<VectorType>(Vect->getType()))
3762 if (VTy->getElementType() == Builder.getInt1Ty()) {
3763 Value *Res = Builder.CreateAddReduce(Vect);
3764 if (Arg != Vect)
3765 Res = Builder.CreateCast(cast<CastInst>(Arg)->getOpcode(), Res,
3766 II->getType());
3767 return replaceInstUsesWith(CI, Res);
3768 }
3769 }
3770 }
3771 [[fallthrough]];
3772 }
3773 case Intrinsic::vector_reduce_mul: {
3774 if (IID == Intrinsic::vector_reduce_mul) {
3775 // Multiplicative reduction over the vector with (potentially-extended)
3776 // i1 element type is actually a (potentially zero-extended)
3777 // logical `and` reduction over the original non-extended value:
3778 // vector_reduce_mul(?ext(<n x i1>))
3779 // -->
3780 // zext(vector_reduce_and(<n x i1>))
3781 Value *Arg = II->getArgOperand(0);
3782 Value *Vect;
3783
3784 if (Value *NewOp =
3785 simplifyReductionOperand(Arg, /*CanReorderLanes=*/true)) {
3786 replaceUse(II->getOperandUse(0), NewOp);
3787 return II;
3788 }
3789
3790 if (match(Arg, m_ZExtOrSExtOrSelf(m_Value(Vect)))) {
3791 if (auto *VTy = dyn_cast<VectorType>(Vect->getType()))
3792 if (VTy->getElementType() == Builder.getInt1Ty()) {
3793 Value *Res = Builder.CreateAndReduce(Vect);
3794 if (Res->getType() != II->getType())
3795 Res = Builder.CreateZExt(Res, II->getType());
3796 return replaceInstUsesWith(CI, Res);
3797 }
3798 }
3799 }
3800 [[fallthrough]];
3801 }
3802 case Intrinsic::vector_reduce_umin:
3803 case Intrinsic::vector_reduce_umax: {
3804 if (IID == Intrinsic::vector_reduce_umin ||
3805 IID == Intrinsic::vector_reduce_umax) {
3806 // UMin/UMax reduction over the vector with (potentially-extended)
3807 // i1 element type is actually a (potentially-extended)
3808 // logical `and`/`or` reduction over the original non-extended value:
3809 // vector_reduce_u{min,max}(?ext(<n x i1>))
3810 // -->
3811 // ?ext(vector_reduce_{and,or}(<n x i1>))
3812 Value *Arg = II->getArgOperand(0);
3813 Value *Vect;
3814
3815 if (Value *NewOp =
3816 simplifyReductionOperand(Arg, /*CanReorderLanes=*/true)) {
3817 replaceUse(II->getOperandUse(0), NewOp);
3818 return II;
3819 }
3820
3821 if (match(Arg, m_ZExtOrSExtOrSelf(m_Value(Vect)))) {
3822 if (auto *VTy = dyn_cast<VectorType>(Vect->getType()))
3823 if (VTy->getElementType() == Builder.getInt1Ty()) {
3824 Value *Res = IID == Intrinsic::vector_reduce_umin
3825 ? Builder.CreateAndReduce(Vect)
3826 : Builder.CreateOrReduce(Vect);
3827 if (Arg != Vect)
3828 Res = Builder.CreateCast(cast<CastInst>(Arg)->getOpcode(), Res,
3829 II->getType());
3830 return replaceInstUsesWith(CI, Res);
3831 }
3832 }
3833 }
3834 [[fallthrough]];
3835 }
3836 case Intrinsic::vector_reduce_smin:
3837 case Intrinsic::vector_reduce_smax: {
3838 if (IID == Intrinsic::vector_reduce_smin ||
3839 IID == Intrinsic::vector_reduce_smax) {
3840 // SMin/SMax reduction over the vector with (potentially-extended)
3841 // i1 element type is actually a (potentially-extended)
3842 // logical `and`/`or` reduction over the original non-extended value:
3843 // vector_reduce_s{min,max}(<n x i1>)
3844 // -->
3845 // vector_reduce_{or,and}(<n x i1>)
3846 // and
3847 // vector_reduce_s{min,max}(sext(<n x i1>))
3848 // -->
3849 // sext(vector_reduce_{or,and}(<n x i1>))
3850 // and
3851 // vector_reduce_s{min,max}(zext(<n x i1>))
3852 // -->
3853 // zext(vector_reduce_{and,or}(<n x i1>))
3854 Value *Arg = II->getArgOperand(0);
3855 Value *Vect;
3856
3857 if (Value *NewOp =
3858 simplifyReductionOperand(Arg, /*CanReorderLanes=*/true)) {
3859 replaceUse(II->getOperandUse(0), NewOp);
3860 return II;
3861 }
3862
3863 if (match(Arg, m_ZExtOrSExtOrSelf(m_Value(Vect)))) {
3864 if (auto *VTy = dyn_cast<VectorType>(Vect->getType()))
3865 if (VTy->getElementType() == Builder.getInt1Ty()) {
3866 Instruction::CastOps ExtOpc = Instruction::CastOps::CastOpsEnd;
3867 if (Arg != Vect)
3868 ExtOpc = cast<CastInst>(Arg)->getOpcode();
3869 Value *Res = ((IID == Intrinsic::vector_reduce_smin) ==
3870 (ExtOpc == Instruction::CastOps::ZExt))
3871 ? Builder.CreateAndReduce(Vect)
3872 : Builder.CreateOrReduce(Vect);
3873 if (Arg != Vect)
3874 Res = Builder.CreateCast(ExtOpc, Res, II->getType());
3875 return replaceInstUsesWith(CI, Res);
3876 }
3877 }
3878 }
3879 [[fallthrough]];
3880 }
3881 case Intrinsic::vector_reduce_fmax:
3882 case Intrinsic::vector_reduce_fmin:
3883 case Intrinsic::vector_reduce_fadd:
3884 case Intrinsic::vector_reduce_fmul: {
3885 bool CanReorderLanes = (IID != Intrinsic::vector_reduce_fadd &&
3886 IID != Intrinsic::vector_reduce_fmul) ||
3887 II->hasAllowReassoc();
3888 const unsigned ArgIdx = (IID == Intrinsic::vector_reduce_fadd ||
3889 IID == Intrinsic::vector_reduce_fmul)
3890 ? 1
3891 : 0;
3892 Value *Arg = II->getArgOperand(ArgIdx);
3893 if (Value *NewOp = simplifyReductionOperand(Arg, CanReorderLanes)) {
3894 replaceUse(II->getOperandUse(ArgIdx), NewOp);
3895 return nullptr;
3896 }
3897 break;
3898 }
3899 case Intrinsic::is_fpclass: {
3900 if (Instruction *I = foldIntrinsicIsFPClass(*II))
3901 return I;
3902 break;
3903 }
3904 case Intrinsic::threadlocal_address: {
3905 Align MinAlign = getKnownAlignment(II->getArgOperand(0), DL, II, &AC, &DT);
3906 MaybeAlign Align = II->getRetAlign();
3907 if (MinAlign > Align.valueOrOne()) {
3908 II->addRetAttr(Attribute::getWithAlignment(II->getContext(), MinAlign));
3909 return II;
3910 }
3911 break;
3912 }
3913 case Intrinsic::frexp: {
3914 Value *X;
3915 // The first result is idempotent with the added complication of the struct
3916 // return, and the second result is zero because the value is already
3917 // normalized.
3918 if (match(II->getArgOperand(0), m_ExtractValue<0>(m_Value(X)))) {
3919 if (match(X, m_Intrinsic<Intrinsic::frexp>(m_Value()))) {
3921 X, Constant::getNullValue(II->getType()->getStructElementType(1)),
3922 1);
3923 return replaceInstUsesWith(*II, X);
3924 }
3925 }
3926 break;
3927 }
3928 default: {
3929 // Handle target specific intrinsics
3930 std::optional<Instruction *> V = targetInstCombineIntrinsic(*II);
3931 if (V)
3932 return *V;
3933 break;
3934 }
3935 }
3936
3937 // Try to fold intrinsic into select/phi operands. This is legal if:
3938 // * The intrinsic is speculatable.
3939 // * The select condition is not a vector, or the intrinsic does not
3940 // perform cross-lane operations.
3943 for (Value *Op : II->args()) {
3944 if (auto *Sel = dyn_cast<SelectInst>(Op))
3945 if (Instruction *R = FoldOpIntoSelect(*II, Sel))
3946 return R;
3947 if (auto *Phi = dyn_cast<PHINode>(Op))
3948 if (Instruction *R = foldOpIntoPhi(*II, Phi))
3949 return R;
3950 }
3951
3953 return Shuf;
3954
3956 return replaceInstUsesWith(*II, Reverse);
3957
3959 return replaceInstUsesWith(*II, Res);
3960
3961 // Some intrinsics (like experimental_gc_statepoint) can be used in invoke
3962 // context, so it is handled in visitCallBase and we should trigger it.
3963 return visitCallBase(*II);
3964}
3965
3966// Fence instruction simplification
3968 auto *NFI = dyn_cast<FenceInst>(FI.getNextNode());
3969 // This check is solely here to handle arbitrary target-dependent syncscopes.
3970 // TODO: Can remove if does not matter in practice.
3971 if (NFI && FI.isIdenticalTo(NFI))
3972 return eraseInstFromFunction(FI);
3973
3974 // Returns true if FI1 is identical or stronger fence than FI2.
3975 auto isIdenticalOrStrongerFence = [](FenceInst *FI1, FenceInst *FI2) {
3976 auto FI1SyncScope = FI1->getSyncScopeID();
3977 // Consider same scope, where scope is global or single-thread.
3978 if (FI1SyncScope != FI2->getSyncScopeID() ||
3979 (FI1SyncScope != SyncScope::System &&
3980 FI1SyncScope != SyncScope::SingleThread))
3981 return false;
3982
3983 return isAtLeastOrStrongerThan(FI1->getOrdering(), FI2->getOrdering());
3984 };
3985 if (NFI && isIdenticalOrStrongerFence(NFI, &FI))
3986 return eraseInstFromFunction(FI);
3987
3988 if (auto *PFI = dyn_cast_or_null<FenceInst>(FI.getPrevNode()))
3989 if (isIdenticalOrStrongerFence(PFI, &FI))
3990 return eraseInstFromFunction(FI);
3991 return nullptr;
3992}
3993
3994// InvokeInst simplification
3996 return visitCallBase(II);
3997}
3998
3999// CallBrInst simplification
4001 return visitCallBase(CBI);
4002}
4003
4004Instruction *InstCombinerImpl::tryOptimizeCall(CallInst *CI) {
4005 if (!CI->getCalledFunction()) return nullptr;
4006
4007 // Skip optimizing notail and musttail calls so
4008 // LibCallSimplifier::optimizeCall doesn't have to preserve those invariants.
4009 // LibCallSimplifier::optimizeCall should try to preserve tail calls though.
4010 if (CI->isMustTailCall() || CI->isNoTailCall())
4011 return nullptr;
4012
4013 auto InstCombineRAUW = [this](Instruction *From, Value *With) {
4014 replaceInstUsesWith(*From, With);
4015 };
4016 auto InstCombineErase = [this](Instruction *I) {
4018 };
4019 LibCallSimplifier Simplifier(DL, &TLI, &DT, &DC, &AC, ORE, BFI, PSI,
4020 InstCombineRAUW, InstCombineErase);
4021 if (Value *With = Simplifier.optimizeCall(CI, Builder)) {
4022 ++NumSimplified;
4023 return CI->use_empty() ? CI : replaceInstUsesWith(*CI, With);
4024 }
4025
4026 return nullptr;
4027}
4028
4030 // Strip off at most one level of pointer casts, looking for an alloca. This
4031 // is good enough in practice and simpler than handling any number of casts.
4032 Value *Underlying = TrampMem->stripPointerCasts();
4033 if (Underlying != TrampMem &&
4034 (!Underlying->hasOneUse() || Underlying->user_back() != TrampMem))
4035 return nullptr;
4036 if (!isa<AllocaInst>(Underlying))
4037 return nullptr;
4038
4039 IntrinsicInst *InitTrampoline = nullptr;
4040 for (User *U : TrampMem->users()) {
4041 IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
4042 if (!II)
4043 return nullptr;
4044 if (II->getIntrinsicID() == Intrinsic::init_trampoline) {
4045 if (InitTrampoline)
4046 // More than one init_trampoline writes to this value. Give up.
4047 return nullptr;
4048 InitTrampoline = II;
4049 continue;
4050 }
4051 if (II->getIntrinsicID() == Intrinsic::adjust_trampoline)
4052 // Allow any number of calls to adjust.trampoline.
4053 continue;
4054 return nullptr;
4055 }
4056
4057 // No call to init.trampoline found.
4058 if (!InitTrampoline)
4059 return nullptr;
4060
4061 // Check that the alloca is being used in the expected way.
4062 if (InitTrampoline->getOperand(0) != TrampMem)
4063 return nullptr;
4064
4065 return InitTrampoline;
4066}
4067
4069 Value *TrampMem) {
4070 // Visit all the previous instructions in the basic block, and try to find a
4071 // init.trampoline which has a direct path to the adjust.trampoline.
4072 for (BasicBlock::iterator I = AdjustTramp->getIterator(),
4073 E = AdjustTramp->getParent()->begin();
4074 I != E;) {
4075 Instruction *Inst = &*--I;
4076 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
4077 if (II->getIntrinsicID() == Intrinsic::init_trampoline &&
4078 II->getOperand(0) == TrampMem)
4079 return II;
4080 if (Inst->mayWriteToMemory())
4081 return nullptr;
4082 }
4083 return nullptr;
4084}
4085
4086// Given a call to llvm.adjust.trampoline, find and return the corresponding
4087// call to llvm.init.trampoline if the call to the trampoline can be optimized
4088// to a direct call to a function. Otherwise return NULL.
4090 Callee = Callee->stripPointerCasts();
4091 IntrinsicInst *AdjustTramp = dyn_cast<IntrinsicInst>(Callee);
4092 if (!AdjustTramp ||
4093 AdjustTramp->getIntrinsicID() != Intrinsic::adjust_trampoline)
4094 return nullptr;
4095
4096 Value *TrampMem = AdjustTramp->getOperand(0);
4097
4099 return IT;
4100 if (IntrinsicInst *IT = findInitTrampolineFromBB(AdjustTramp, TrampMem))
4101 return IT;
4102 return nullptr;
4103}
4104
4105Instruction *InstCombinerImpl::foldPtrAuthIntrinsicCallee(CallBase &Call) {
4106 const Value *Callee = Call.getCalledOperand();
4107 const auto *IPC = dyn_cast<IntToPtrInst>(Callee);
4108 if (!IPC || !IPC->isNoopCast(DL))
4109 return nullptr;
4110
4111 const auto *II = dyn_cast<IntrinsicInst>(IPC->getOperand(0));
4112 if (!II)
4113 return nullptr;
4114
4115 Intrinsic::ID IIID = II->getIntrinsicID();
4116 if (IIID != Intrinsic::ptrauth_resign && IIID != Intrinsic::ptrauth_sign)
4117 return nullptr;
4118
4119 // Isolate the ptrauth bundle from the others.
4120 std::optional<OperandBundleUse> PtrAuthBundleOrNone;
4122 for (unsigned BI = 0, BE = Call.getNumOperandBundles(); BI != BE; ++BI) {
4123 OperandBundleUse Bundle = Call.getOperandBundleAt(BI);
4124 if (Bundle.getTagID() == LLVMContext::OB_ptrauth)
4125 PtrAuthBundleOrNone = Bundle;
4126 else
4127 NewBundles.emplace_back(Bundle);
4128 }
4129
4130 if (!PtrAuthBundleOrNone)
4131 return nullptr;
4132
4133 Value *NewCallee = nullptr;
4134 switch (IIID) {
4135 // call(ptrauth.resign(p)), ["ptrauth"()] -> call p, ["ptrauth"()]
4136 // assuming the call bundle and the sign operands match.
4137 case Intrinsic::ptrauth_resign: {
4138 // Resign result key should match bundle.
4139 if (II->getOperand(3) != PtrAuthBundleOrNone->Inputs[0])
4140 return nullptr;
4141 // Resign result discriminator should match bundle.
4142 if (II->getOperand(4) != PtrAuthBundleOrNone->Inputs[1])
4143 return nullptr;
4144
4145 // Resign input (auth) key should also match: we can't change the key on
4146 // the new call we're generating, because we don't know what keys are valid.
4147 if (II->getOperand(1) != PtrAuthBundleOrNone->Inputs[0])
4148 return nullptr;
4149
4150 Value *NewBundleOps[] = {II->getOperand(1), II->getOperand(2)};
4151 NewBundles.emplace_back("ptrauth", NewBundleOps);
4152 NewCallee = II->getOperand(0);
4153 break;
4154 }
4155
4156 // call(ptrauth.sign(p)), ["ptrauth"()] -> call p
4157 // assuming the call bundle and the sign operands match.
4158 // Non-ptrauth indirect calls are undesirable, but so is ptrauth.sign.
4159 case Intrinsic::ptrauth_sign: {
4160 // Sign key should match bundle.
4161 if (II->getOperand(1) != PtrAuthBundleOrNone->Inputs[0])
4162 return nullptr;
4163 // Sign discriminator should match bundle.
4164 if (II->getOperand(2) != PtrAuthBundleOrNone->Inputs[1])
4165 return nullptr;
4166 NewCallee = II->getOperand(0);
4167 break;
4168 }
4169 default:
4170 llvm_unreachable("unexpected intrinsic ID");
4171 }
4172
4173 if (!NewCallee)
4174 return nullptr;
4175
4176 NewCallee = Builder.CreateBitOrPointerCast(NewCallee, Callee->getType());
4177 CallBase *NewCall = CallBase::Create(&Call, NewBundles);
4178 NewCall->setCalledOperand(NewCallee);
4179 return NewCall;
4180}
4181
4182Instruction *InstCombinerImpl::foldPtrAuthConstantCallee(CallBase &Call) {
4183 auto *CPA = dyn_cast<ConstantPtrAuth>(Call.getCalledOperand());
4184 if (!CPA)
4185 return nullptr;
4186
4187 auto *CalleeF = dyn_cast<Function>(CPA->getPointer());
4188 // If the ptrauth constant isn't based on a function pointer, bail out.
4189 if (!CalleeF)
4190 return nullptr;
4191
4192 // Inspect the call ptrauth bundle to check it matches the ptrauth constant.
4193 auto PAB = Call.getOperandBundle(LLVMContext::OB_ptrauth);
4194 if (!PAB)
4195 return nullptr;
4196
4197 auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
4198 Value *Discriminator = PAB->Inputs[1];
4199
4200 // If the bundle doesn't match, this is probably going to fail to auth.
4201 if (!CPA->isKnownCompatibleWith(Key, Discriminator, DL))
4202 return nullptr;
4203
4204 // If the bundle matches the constant, proceed in making this a direct call.
4206 NewCall->setCalledOperand(CalleeF);
4207 return NewCall;
4208}
4209
4210bool InstCombinerImpl::annotateAnyAllocSite(CallBase &Call,
4211 const TargetLibraryInfo *TLI) {
4212 // Note: We only handle cases which can't be driven from generic attributes
4213 // here. So, for example, nonnull and noalias (which are common properties
4214 // of some allocation functions) are expected to be handled via annotation
4215 // of the respective allocator declaration with generic attributes.
4216 bool Changed = false;
4217
4218 if (!Call.getType()->isPointerTy())
4219 return Changed;
4220
4221 std::optional<APInt> Size = getAllocSize(&Call, TLI);
4222 if (Size && *Size != 0) {
4223 // TODO: We really should just emit deref_or_null here and then
4224 // let the generic inference code combine that with nonnull.
4225 if (Call.hasRetAttr(Attribute::NonNull)) {
4226 Changed = !Call.hasRetAttr(Attribute::Dereferenceable);
4228 Call.getContext(), Size->getLimitedValue()));
4229 } else {
4230 Changed = !Call.hasRetAttr(Attribute::DereferenceableOrNull);
4232 Call.getContext(), Size->getLimitedValue()));
4233 }
4234 }
4235
4236 // Add alignment attribute if alignment is a power of two constant.
4237 Value *Alignment = getAllocAlignment(&Call, TLI);
4238 if (!Alignment)
4239 return Changed;
4240
4241 ConstantInt *AlignOpC = dyn_cast<ConstantInt>(Alignment);
4242 if (AlignOpC && AlignOpC->getValue().ult(llvm::Value::MaximumAlignment)) {
4243 uint64_t AlignmentVal = AlignOpC->getZExtValue();
4244 if (llvm::isPowerOf2_64(AlignmentVal)) {
4245 Align ExistingAlign = Call.getRetAlign().valueOrOne();
4246 Align NewAlign = Align(AlignmentVal);
4247 if (NewAlign > ExistingAlign) {
4248 Call.addRetAttr(
4249 Attribute::getWithAlignment(Call.getContext(), NewAlign));
4250 Changed = true;
4251 }
4252 }
4253 }
4254 return Changed;
4255}
4256
4257/// Improvements for call, callbr and invoke instructions.
4258Instruction *InstCombinerImpl::visitCallBase(CallBase &Call) {
4259 bool Changed = annotateAnyAllocSite(Call, &TLI);
4260
4261 // Mark any parameters that are known to be non-null with the nonnull
4262 // attribute. This is helpful for inlining calls to functions with null
4263 // checks on their arguments.
4265 unsigned ArgNo = 0;
4266
4267 for (Value *V : Call.args()) {
4268 if (V->getType()->isPointerTy()) {
4269 // Simplify the nonnull operand if the parameter is known to be nonnull.
4270 // Otherwise, try to infer nonnull for it.
4271 bool HasDereferenceable = Call.getParamDereferenceableBytes(ArgNo) > 0;
4272 if (Call.paramHasAttr(ArgNo, Attribute::NonNull) ||
4273 (HasDereferenceable &&
4274 !NullPointerIsDefined(Call.getFunction(),
4275 V->getType()->getPointerAddressSpace()))) {
4276 if (Value *Res = simplifyNonNullOperand(V, HasDereferenceable)) {
4277 replaceOperand(Call, ArgNo, Res);
4278 Changed = true;
4279 }
4280 } else if (isKnownNonZero(V,
4281 getSimplifyQuery().getWithInstruction(&Call))) {
4282 ArgNos.push_back(ArgNo);
4283 }
4284 }
4285 ArgNo++;
4286 }
4287
4288 assert(ArgNo == Call.arg_size() && "Call arguments not processed correctly.");
4289
4290 if (!ArgNos.empty()) {
4291 AttributeList AS = Call.getAttributes();
4292 LLVMContext &Ctx = Call.getContext();
4293 AS = AS.addParamAttribute(Ctx, ArgNos,
4294 Attribute::get(Ctx, Attribute::NonNull));
4295 Call.setAttributes(AS);
4296 Changed = true;
4297 }
4298
4299 // If the callee is a pointer to a function, attempt to move any casts to the
4300 // arguments of the call/callbr/invoke.
4301 Value *Callee = Call.getCalledOperand();
4302 Function *CalleeF = dyn_cast<Function>(Callee);
4303 if ((!CalleeF || CalleeF->getFunctionType() != Call.getFunctionType()) &&
4304 transformConstExprCastCall(Call))
4305 return nullptr;
4306
4307 if (CalleeF) {
4308 // Remove the convergent attr on calls when the callee is not convergent.
4309 if (Call.isConvergent() && !CalleeF->isConvergent() &&
4310 !CalleeF->isIntrinsic()) {
4311 LLVM_DEBUG(dbgs() << "Removing convergent attr from instr " << Call
4312 << "\n");
4313 Call.setNotConvergent();
4314 return &Call;
4315 }
4316
4317 // If the call and callee calling conventions don't match, and neither one
4318 // of the calling conventions is compatible with C calling convention
4319 // this call must be unreachable, as the call is undefined.
4320 if ((CalleeF->getCallingConv() != Call.getCallingConv() &&
4321 !(CalleeF->getCallingConv() == llvm::CallingConv::C &&
4323 !(Call.getCallingConv() == llvm::CallingConv::C &&
4325 // Only do this for calls to a function with a body. A prototype may
4326 // not actually end up matching the implementation's calling conv for a
4327 // variety of reasons (e.g. it may be written in assembly).
4328 !CalleeF->isDeclaration()) {
4329 Instruction *OldCall = &Call;
4331 // If OldCall does not return void then replaceInstUsesWith poison.
4332 // This allows ValueHandlers and custom metadata to adjust itself.
4333 if (!OldCall->getType()->isVoidTy())
4334 replaceInstUsesWith(*OldCall, PoisonValue::get(OldCall->getType()));
4335 if (isa<CallInst>(OldCall))
4336 return eraseInstFromFunction(*OldCall);
4337
4338 // We cannot remove an invoke or a callbr, because it would change thexi
4339 // CFG, just change the callee to a null pointer.
4340 cast<CallBase>(OldCall)->setCalledFunction(
4341 CalleeF->getFunctionType(),
4342 Constant::getNullValue(CalleeF->getType()));
4343 return nullptr;
4344 }
4345 }
4346
4347 // Calling a null function pointer is undefined if a null address isn't
4348 // dereferenceable.
4349 if ((isa<ConstantPointerNull>(Callee) &&
4350 !NullPointerIsDefined(Call.getFunction())) ||
4351 isa<UndefValue>(Callee)) {
4352 // If Call does not return void then replaceInstUsesWith poison.
4353 // This allows ValueHandlers and custom metadata to adjust itself.
4354 if (!Call.getType()->isVoidTy())
4355 replaceInstUsesWith(Call, PoisonValue::get(Call.getType()));
4356
4357 if (Call.isTerminator()) {
4358 // Can't remove an invoke or callbr because we cannot change the CFG.
4359 return nullptr;
4360 }
4361
4362 // This instruction is not reachable, just remove it.
4364 return eraseInstFromFunction(Call);
4365 }
4366
4367 if (IntrinsicInst *II = findInitTrampoline(Callee))
4368 return transformCallThroughTrampoline(Call, *II);
4369
4370 // Combine calls involving pointer authentication intrinsics.
4371 if (Instruction *NewCall = foldPtrAuthIntrinsicCallee(Call))
4372 return NewCall;
4373
4374 // Combine calls to ptrauth constants.
4375 if (Instruction *NewCall = foldPtrAuthConstantCallee(Call))
4376 return NewCall;
4377
4378 if (isa<InlineAsm>(Callee) && !Call.doesNotThrow()) {
4379 InlineAsm *IA = cast<InlineAsm>(Callee);
4380 if (!IA->canThrow()) {
4381 // Normal inline asm calls cannot throw - mark them
4382 // 'nounwind'.
4383 Call.setDoesNotThrow();
4384 Changed = true;
4385 }
4386 }
4387
4388 // Try to optimize the call if possible, we require DataLayout for most of
4389 // this. None of these calls are seen as possibly dead so go ahead and
4390 // delete the instruction now.
4391 if (CallInst *CI = dyn_cast<CallInst>(&Call)) {
4392 Instruction *I = tryOptimizeCall(CI);
4393 // If we changed something return the result, etc. Otherwise let
4394 // the fallthrough check.
4395 if (I) return eraseInstFromFunction(*I);
4396 }
4397
4398 if (!Call.use_empty() && !Call.isMustTailCall())
4399 if (Value *ReturnedArg = Call.getReturnedArgOperand()) {
4400 Type *CallTy = Call.getType();
4401 Type *RetArgTy = ReturnedArg->getType();
4402 if (RetArgTy->canLosslesslyBitCastTo(CallTy))
4403 return replaceInstUsesWith(
4404 Call, Builder.CreateBitOrPointerCast(ReturnedArg, CallTy));
4405 }
4406
4407 // Drop unnecessary callee_type metadata from calls that were converted
4408 // into direct calls.
4409 if (Call.getMetadata(LLVMContext::MD_callee_type) && !Call.isIndirectCall()) {
4410 Call.setMetadata(LLVMContext::MD_callee_type, nullptr);
4411 Changed = true;
4412 }
4413
4414 // Drop unnecessary kcfi operand bundles from calls that were converted
4415 // into direct calls.
4416 auto Bundle = Call.getOperandBundle(LLVMContext::OB_kcfi);
4417 if (Bundle && !Call.isIndirectCall()) {
4418 DEBUG_WITH_TYPE(DEBUG_TYPE "-kcfi", {
4419 if (CalleeF) {
4420 ConstantInt *FunctionType = nullptr;
4421 ConstantInt *ExpectedType = cast<ConstantInt>(Bundle->Inputs[0]);
4422
4423 if (MDNode *MD = CalleeF->getMetadata(LLVMContext::MD_kcfi_type))
4424 FunctionType = mdconst::extract<ConstantInt>(MD->getOperand(0));
4425
4426 if (FunctionType &&
4427 FunctionType->getZExtValue() != ExpectedType->getZExtValue())
4428 dbgs() << Call.getModule()->getName()
4429 << ": warning: kcfi: " << Call.getCaller()->getName()
4430 << ": call to " << CalleeF->getName()
4431 << " using a mismatching function pointer type\n";
4432 }
4433 });
4434
4436 }
4437
4438 if (isRemovableAlloc(&Call, &TLI))
4439 return visitAllocSite(Call);
4440
4441 // Handle intrinsics which can be used in both call and invoke context.
4442 switch (Call.getIntrinsicID()) {
4443 case Intrinsic::experimental_gc_statepoint: {
4444 GCStatepointInst &GCSP = *cast<GCStatepointInst>(&Call);
4445 SmallPtrSet<Value *, 32> LiveGcValues;
4446 for (const GCRelocateInst *Reloc : GCSP.getGCRelocates()) {
4447 GCRelocateInst &GCR = *const_cast<GCRelocateInst *>(Reloc);
4448
4449 // Remove the relocation if unused.
4450 if (GCR.use_empty()) {
4452 continue;
4453 }
4454
4455 Value *DerivedPtr = GCR.getDerivedPtr();
4456 Value *BasePtr = GCR.getBasePtr();
4457
4458 // Undef is undef, even after relocation.
4459 if (isa<UndefValue>(DerivedPtr) || isa<UndefValue>(BasePtr)) {
4462 continue;
4463 }
4464
4465 if (auto *PT = dyn_cast<PointerType>(GCR.getType())) {
4466 // The relocation of null will be null for most any collector.
4467 // TODO: provide a hook for this in GCStrategy. There might be some
4468 // weird collector this property does not hold for.
4469 if (isa<ConstantPointerNull>(DerivedPtr)) {
4470 // Use null-pointer of gc_relocate's type to replace it.
4473 continue;
4474 }
4475
4476 // isKnownNonNull -> nonnull attribute
4477 if (!GCR.hasRetAttr(Attribute::NonNull) &&
4478 isKnownNonZero(DerivedPtr,
4479 getSimplifyQuery().getWithInstruction(&Call))) {
4480 GCR.addRetAttr(Attribute::NonNull);
4481 // We discovered new fact, re-check users.
4483 }
4484 }
4485
4486 // If we have two copies of the same pointer in the statepoint argument
4487 // list, canonicalize to one. This may let us common gc.relocates.
4488 if (GCR.getBasePtr() == GCR.getDerivedPtr() &&
4489 GCR.getBasePtrIndex() != GCR.getDerivedPtrIndex()) {
4490 auto *OpIntTy = GCR.getOperand(2)->getType();
4491 GCR.setOperand(2, ConstantInt::get(OpIntTy, GCR.getBasePtrIndex()));
4492 }
4493
4494 // TODO: bitcast(relocate(p)) -> relocate(bitcast(p))
4495 // Canonicalize on the type from the uses to the defs
4496
4497 // TODO: relocate((gep p, C, C2, ...)) -> gep(relocate(p), C, C2, ...)
4498 LiveGcValues.insert(BasePtr);
4499 LiveGcValues.insert(DerivedPtr);
4500 }
4501 std::optional<OperandBundleUse> Bundle =
4503 unsigned NumOfGCLives = LiveGcValues.size();
4504 if (!Bundle || NumOfGCLives == Bundle->Inputs.size())
4505 break;
4506 // We can reduce the size of gc live bundle.
4508 std::vector<Value *> NewLiveGc;
4509 for (Value *V : Bundle->Inputs) {
4510 auto [It, Inserted] = Val2Idx.try_emplace(V);
4511 if (!Inserted)
4512 continue;
4513 if (LiveGcValues.count(V)) {
4514 It->second = NewLiveGc.size();
4515 NewLiveGc.push_back(V);
4516 } else
4517 It->second = NumOfGCLives;
4518 }
4519 // Update all gc.relocates
4520 for (const GCRelocateInst *Reloc : GCSP.getGCRelocates()) {
4521 GCRelocateInst &GCR = *const_cast<GCRelocateInst *>(Reloc);
4522 Value *BasePtr = GCR.getBasePtr();
4523 assert(Val2Idx.count(BasePtr) && Val2Idx[BasePtr] != NumOfGCLives &&
4524 "Missed live gc for base pointer");
4525 auto *OpIntTy1 = GCR.getOperand(1)->getType();
4526 GCR.setOperand(1, ConstantInt::get(OpIntTy1, Val2Idx[BasePtr]));
4527 Value *DerivedPtr = GCR.getDerivedPtr();
4528 assert(Val2Idx.count(DerivedPtr) && Val2Idx[DerivedPtr] != NumOfGCLives &&
4529 "Missed live gc for derived pointer");
4530 auto *OpIntTy2 = GCR.getOperand(2)->getType();
4531 GCR.setOperand(2, ConstantInt::get(OpIntTy2, Val2Idx[DerivedPtr]));
4532 }
4533 // Create new statepoint instruction.
4534 OperandBundleDef NewBundle("gc-live", NewLiveGc);
4535 return CallBase::Create(&Call, NewBundle);
4536 }
4537 default: { break; }
4538 }
4539
4540 return Changed ? &Call : nullptr;
4541}
4542
4543/// If the callee is a constexpr cast of a function, attempt to move the cast to
4544/// the arguments of the call/invoke.
4545/// CallBrInst is not supported.
4546bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {
4547 auto *Callee =
4548 dyn_cast<Function>(Call.getCalledOperand()->stripPointerCasts());
4549 if (!Callee)
4550 return false;
4551
4552 assert(!isa<CallBrInst>(Call) &&
4553 "CallBr's don't have a single point after a def to insert at");
4554
4555 // Don't perform the transform for declarations, which may not be fully
4556 // accurate. For example, void @foo() is commonly used as a placeholder for
4557 // unknown prototypes.
4558 if (Callee->isDeclaration())
4559 return false;
4560
4561 // If this is a call to a thunk function, don't remove the cast. Thunks are
4562 // used to transparently forward all incoming parameters and outgoing return
4563 // values, so it's important to leave the cast in place.
4564 if (Callee->hasFnAttribute("thunk"))
4565 return false;
4566
4567 // If this is a call to a naked function, the assembly might be
4568 // using an argument, or otherwise rely on the frame layout,
4569 // the function prototype will mismatch.
4570 if (Callee->hasFnAttribute(Attribute::Naked))
4571 return false;
4572
4573 // If this is a musttail call, the callee's prototype must match the caller's
4574 // prototype with the exception of pointee types. The code below doesn't
4575 // implement that, so we can't do this transform.
4576 // TODO: Do the transform if it only requires adding pointer casts.
4577 if (Call.isMustTailCall())
4578 return false;
4579
4581 const AttributeList &CallerPAL = Call.getAttributes();
4582
4583 // Okay, this is a cast from a function to a different type. Unless doing so
4584 // would cause a type conversion of one of our arguments, change this call to
4585 // be a direct call with arguments casted to the appropriate types.
4586 FunctionType *FT = Callee->getFunctionType();
4587 Type *OldRetTy = Caller->getType();
4588 Type *NewRetTy = FT->getReturnType();
4589
4590 // Check to see if we are changing the return type...
4591 if (OldRetTy != NewRetTy) {
4592
4593 if (NewRetTy->isStructTy())
4594 return false; // TODO: Handle multiple return values.
4595
4596 if (!CastInst::isBitOrNoopPointerCastable(NewRetTy, OldRetTy, DL)) {
4597 if (!Caller->use_empty())
4598 return false; // Cannot transform this return value.
4599 }
4600
4601 if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
4602 AttrBuilder RAttrs(FT->getContext(), CallerPAL.getRetAttrs());
4603 if (RAttrs.overlaps(AttributeFuncs::typeIncompatible(
4604 NewRetTy, CallerPAL.getRetAttrs())))
4605 return false; // Attribute not compatible with transformed value.
4606 }
4607
4608 // If the callbase is an invoke instruction, and the return value is
4609 // used by a PHI node in a successor, we cannot change the return type of
4610 // the call because there is no place to put the cast instruction (without
4611 // breaking the critical edge). Bail out in this case.
4612 if (!Caller->use_empty()) {
4613 BasicBlock *PhisNotSupportedBlock = nullptr;
4614 if (auto *II = dyn_cast<InvokeInst>(Caller))
4615 PhisNotSupportedBlock = II->getNormalDest();
4616 if (PhisNotSupportedBlock)
4617 for (User *U : Caller->users())
4618 if (PHINode *PN = dyn_cast<PHINode>(U))
4619 if (PN->getParent() == PhisNotSupportedBlock)
4620 return false;
4621 }
4622 }
4623
4624 unsigned NumActualArgs = Call.arg_size();
4625 unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
4626
4627 // Prevent us turning:
4628 // declare void @takes_i32_inalloca(i32* inalloca)
4629 // call void bitcast (void (i32*)* @takes_i32_inalloca to void (i32)*)(i32 0)
4630 //
4631 // into:
4632 // call void @takes_i32_inalloca(i32* null)
4633 //
4634 // Similarly, avoid folding away bitcasts of byval calls.
4635 if (Callee->getAttributes().hasAttrSomewhere(Attribute::InAlloca) ||
4636 Callee->getAttributes().hasAttrSomewhere(Attribute::Preallocated))
4637 return false;
4638
4639 auto AI = Call.arg_begin();
4640 for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
4641 Type *ParamTy = FT->getParamType(i);
4642 Type *ActTy = (*AI)->getType();
4643
4644 if (!CastInst::isBitOrNoopPointerCastable(ActTy, ParamTy, DL))
4645 return false; // Cannot transform this parameter value.
4646
4647 // Check if there are any incompatible attributes we cannot drop safely.
4648 if (AttrBuilder(FT->getContext(), CallerPAL.getParamAttrs(i))
4650 ParamTy, CallerPAL.getParamAttrs(i),
4652 return false; // Attribute not compatible with transformed value.
4653
4654 if (Call.isInAllocaArgument(i) ||
4655 CallerPAL.hasParamAttr(i, Attribute::Preallocated))
4656 return false; // Cannot transform to and from inalloca/preallocated.
4657
4658 if (CallerPAL.hasParamAttr(i, Attribute::SwiftError))
4659 return false;
4660
4661 if (CallerPAL.hasParamAttr(i, Attribute::ByVal) !=
4662 Callee->getAttributes().hasParamAttr(i, Attribute::ByVal))
4663 return false; // Cannot transform to or from byval.
4664 }
4665
4666 if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
4667 !CallerPAL.isEmpty()) {
4668 // In this case we have more arguments than the new function type, but we
4669 // won't be dropping them. Check that these extra arguments have attributes
4670 // that are compatible with being a vararg call argument.
4671 unsigned SRetIdx;
4672 if (CallerPAL.hasAttrSomewhere(Attribute::StructRet, &SRetIdx) &&
4673 SRetIdx - AttributeList::FirstArgIndex >= FT->getNumParams())
4674 return false;
4675 }
4676
4677 // Okay, we decided that this is a safe thing to do: go ahead and start
4678 // inserting cast instructions as necessary.
4681 Args.reserve(NumActualArgs);
4682 ArgAttrs.reserve(NumActualArgs);
4683
4684 // Get any return attributes.
4685 AttrBuilder RAttrs(FT->getContext(), CallerPAL.getRetAttrs());
4686
4687 // If the return value is not being used, the type may not be compatible
4688 // with the existing attributes. Wipe out any problematic attributes.
4689 RAttrs.remove(
4690 AttributeFuncs::typeIncompatible(NewRetTy, CallerPAL.getRetAttrs()));
4691
4692 LLVMContext &Ctx = Call.getContext();
4693 AI = Call.arg_begin();
4694 for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
4695 Type *ParamTy = FT->getParamType(i);
4696
4697 Value *NewArg = *AI;
4698 if ((*AI)->getType() != ParamTy)
4699 NewArg = Builder.CreateBitOrPointerCast(*AI, ParamTy);
4700 Args.push_back(NewArg);
4701
4702 // Add any parameter attributes except the ones incompatible with the new
4703 // type. Note that we made sure all incompatible ones are safe to drop.
4705 ParamTy, CallerPAL.getParamAttrs(i), AttributeFuncs::ASK_SAFE_TO_DROP);
4706 ArgAttrs.push_back(
4707 CallerPAL.getParamAttrs(i).removeAttributes(Ctx, IncompatibleAttrs));
4708 }
4709
4710 // If the function takes more arguments than the call was taking, add them
4711 // now.
4712 for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) {
4713 Args.push_back(Constant::getNullValue(FT->getParamType(i)));
4714 ArgAttrs.push_back(AttributeSet());
4715 }
4716
4717 // If we are removing arguments to the function, emit an obnoxious warning.
4718 if (FT->getNumParams() < NumActualArgs) {
4719 // TODO: if (!FT->isVarArg()) this call may be unreachable. PR14722
4720 if (FT->isVarArg()) {
4721 // Add all of the arguments in their promoted form to the arg list.
4722 for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
4723 Type *PTy = getPromotedType((*AI)->getType());
4724 Value *NewArg = *AI;
4725 if (PTy != (*AI)->getType()) {
4726 // Must promote to pass through va_arg area!
4727 Instruction::CastOps opcode =
4728 CastInst::getCastOpcode(*AI, false, PTy, false);
4729 NewArg = Builder.CreateCast(opcode, *AI, PTy);
4730 }
4731 Args.push_back(NewArg);
4732
4733 // Add any parameter attributes.
4734 ArgAttrs.push_back(CallerPAL.getParamAttrs(i));
4735 }
4736 }
4737 }
4738
4739 AttributeSet FnAttrs = CallerPAL.getFnAttrs();
4740
4741 if (NewRetTy->isVoidTy())
4742 Caller->setName(""); // Void type should not have a name.
4743
4744 assert((ArgAttrs.size() == FT->getNumParams() || FT->isVarArg()) &&
4745 "missing argument attributes");
4746 AttributeList NewCallerPAL = AttributeList::get(
4747 Ctx, FnAttrs, AttributeSet::get(Ctx, RAttrs), ArgAttrs);
4748
4750 Call.getOperandBundlesAsDefs(OpBundles);
4751
4752 CallBase *NewCall;
4753 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
4754 NewCall = Builder.CreateInvoke(Callee, II->getNormalDest(),
4755 II->getUnwindDest(), Args, OpBundles);
4756 } else {
4757 NewCall = Builder.CreateCall(Callee, Args, OpBundles);
4758 cast<CallInst>(NewCall)->setTailCallKind(
4759 cast<CallInst>(Caller)->getTailCallKind());
4760 }
4761 NewCall->takeName(Caller);
4762 NewCall->setCallingConv(Call.getCallingConv());
4763 NewCall->setAttributes(NewCallerPAL);
4764
4765 // Preserve prof metadata if any.
4766 NewCall->copyMetadata(*Caller, {LLVMContext::MD_prof});
4767
4768 // Insert a cast of the return type as necessary.
4769 Instruction *NC = NewCall;
4770 Value *NV = NC;
4771 if (OldRetTy != NV->getType() && !Caller->use_empty()) {
4772 assert(!NV->getType()->isVoidTy());
4774 NC->setDebugLoc(Caller->getDebugLoc());
4775
4776 auto OptInsertPt = NewCall->getInsertionPointAfterDef();
4777 assert(OptInsertPt && "No place to insert cast");
4778 InsertNewInstBefore(NC, *OptInsertPt);
4780 }
4781
4782 if (!Caller->use_empty())
4783 replaceInstUsesWith(*Caller, NV);
4784 else if (Caller->hasValueHandle()) {
4785 if (OldRetTy == NV->getType())
4787 else
4788 // We cannot call ValueIsRAUWd with a different type, and the
4789 // actual tracked value will disappear.
4791 }
4792
4793 eraseInstFromFunction(*Caller);
4794 return true;
4795}
4796
4797/// Turn a call to a function created by init_trampoline / adjust_trampoline
4798/// intrinsic pair into a direct call to the underlying function.
4800InstCombinerImpl::transformCallThroughTrampoline(CallBase &Call,
4801 IntrinsicInst &Tramp) {
4802 FunctionType *FTy = Call.getFunctionType();
4803 AttributeList Attrs = Call.getAttributes();
4804
4805 // If the call already has the 'nest' attribute somewhere then give up -
4806 // otherwise 'nest' would occur twice after splicing in the chain.
4807 if (Attrs.hasAttrSomewhere(Attribute::Nest))
4808 return nullptr;
4809
4810 Function *NestF = cast<Function>(Tramp.getArgOperand(1)->stripPointerCasts());
4811 FunctionType *NestFTy = NestF->getFunctionType();
4812
4813 AttributeList NestAttrs = NestF->getAttributes();
4814 if (!NestAttrs.isEmpty()) {
4815 unsigned NestArgNo = 0;
4816 Type *NestTy = nullptr;
4817 AttributeSet NestAttr;
4818
4819 // Look for a parameter marked with the 'nest' attribute.
4820 for (FunctionType::param_iterator I = NestFTy->param_begin(),
4821 E = NestFTy->param_end();
4822 I != E; ++NestArgNo, ++I) {
4823 AttributeSet AS = NestAttrs.getParamAttrs(NestArgNo);
4824 if (AS.hasAttribute(Attribute::Nest)) {
4825 // Record the parameter type and any other attributes.
4826 NestTy = *I;
4827 NestAttr = AS;
4828 break;
4829 }
4830 }
4831
4832 if (NestTy) {
4833 std::vector<Value*> NewArgs;
4834 std::vector<AttributeSet> NewArgAttrs;
4835 NewArgs.reserve(Call.arg_size() + 1);
4836 NewArgAttrs.reserve(Call.arg_size());
4837
4838 // Insert the nest argument into the call argument list, which may
4839 // mean appending it. Likewise for attributes.
4840
4841 {
4842 unsigned ArgNo = 0;
4843 auto I = Call.arg_begin(), E = Call.arg_end();
4844 do {
4845 if (ArgNo == NestArgNo) {
4846 // Add the chain argument and attributes.
4847 Value *NestVal = Tramp.getArgOperand(2);
4848 if (NestVal->getType() != NestTy)
4849 NestVal = Builder.CreateBitCast(NestVal, NestTy, "nest");
4850 NewArgs.push_back(NestVal);
4851 NewArgAttrs.push_back(NestAttr);
4852 }
4853
4854 if (I == E)
4855 break;
4856
4857 // Add the original argument and attributes.
4858 NewArgs.push_back(*I);
4859 NewArgAttrs.push_back(Attrs.getParamAttrs(ArgNo));
4860
4861 ++ArgNo;
4862 ++I;
4863 } while (true);
4864 }
4865
4866 // The trampoline may have been bitcast to a bogus type (FTy).
4867 // Handle this by synthesizing a new function type, equal to FTy
4868 // with the chain parameter inserted.
4869
4870 std::vector<Type*> NewTypes;
4871 NewTypes.reserve(FTy->getNumParams()+1);
4872
4873 // Insert the chain's type into the list of parameter types, which may
4874 // mean appending it.
4875 {
4876 unsigned ArgNo = 0;
4877 FunctionType::param_iterator I = FTy->param_begin(),
4878 E = FTy->param_end();
4879
4880 do {
4881 if (ArgNo == NestArgNo)
4882 // Add the chain's type.
4883 NewTypes.push_back(NestTy);
4884
4885 if (I == E)
4886 break;
4887
4888 // Add the original type.
4889 NewTypes.push_back(*I);
4890
4891 ++ArgNo;
4892 ++I;
4893 } while (true);
4894 }
4895
4896 // Replace the trampoline call with a direct call. Let the generic
4897 // code sort out any function type mismatches.
4898 FunctionType *NewFTy =
4899 FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg());
4900 AttributeList NewPAL =
4901 AttributeList::get(FTy->getContext(), Attrs.getFnAttrs(),
4902 Attrs.getRetAttrs(), NewArgAttrs);
4903
4905 Call.getOperandBundlesAsDefs(OpBundles);
4906
4907 Instruction *NewCaller;
4908 if (InvokeInst *II = dyn_cast<InvokeInst>(&Call)) {
4909 NewCaller = InvokeInst::Create(NewFTy, NestF, II->getNormalDest(),
4910 II->getUnwindDest(), NewArgs, OpBundles);
4911 cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
4912 cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
4913 } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(&Call)) {
4914 NewCaller =
4915 CallBrInst::Create(NewFTy, NestF, CBI->getDefaultDest(),
4916 CBI->getIndirectDests(), NewArgs, OpBundles);
4917 cast<CallBrInst>(NewCaller)->setCallingConv(CBI->getCallingConv());
4918 cast<CallBrInst>(NewCaller)->setAttributes(NewPAL);
4919 } else {
4920 NewCaller = CallInst::Create(NewFTy, NestF, NewArgs, OpBundles);
4921 cast<CallInst>(NewCaller)->setTailCallKind(
4922 cast<CallInst>(Call).getTailCallKind());
4923 cast<CallInst>(NewCaller)->setCallingConv(
4924 cast<CallInst>(Call).getCallingConv());
4925 cast<CallInst>(NewCaller)->setAttributes(NewPAL);
4926 }
4927 NewCaller->setDebugLoc(Call.getDebugLoc());
4928
4929 return NewCaller;
4930 }
4931 }
4932
4933 // Replace the trampoline call with a direct call. Since there is no 'nest'
4934 // parameter, there is no need to adjust the argument list. Let the generic
4935 // code sort out any function type mismatches.
4936 Call.setCalledFunction(FTy, NestF);
4937 return &Call;
4938}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned Intr
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...
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...
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static SDValue foldBitOrderCrossLogicOp(SDNode *N, SelectionDAG &DAG)
return RetTy
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
uint64_t Size
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
#define DEBUG_TYPE
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 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 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 Value * simplifyNeonTbl1(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder)
Convert a table lookup to shufflevector if the mask is constant.
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 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(CtOp, ZeroUndef),...
static Value * foldIdempotentBinaryIntrinsicRecurrence(InstCombinerImpl &IC, IntrinsicInst *II)
Attempt to simplify value-accumulating recurrences of kind: umax.acc = phi i8 [ umax,...
static Instruction * foldCtpop(IntrinsicInst &II, InstCombinerImpl &IC)
static Instruction * foldCttzCtlz(IntrinsicInst &II, InstCombinerImpl &IC)
static IntrinsicInst * findInitTrampoline(Value *Callee)
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 CallInst * canonicalizeConstantArg0ToArg1(CallInst &Call)
This file provides internal interfaces used to implement the InstCombine.
This file provides the interface for the instcombine pass implementation.
static bool hasNoSignedWrap(BinaryOperator &I)
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:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
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:167
#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:77
@ Struct
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:247
Value * RHS
Value * LHS
LLVM_ABI ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, bool IgnoreLocals=false)
Returns a bitmask that should be unconditionally applied to the ModRef info of a memory location.
bool isNegative() const
Definition: APFloat.h:1449
void clearSign()
Definition: APFloat.h:1298
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:234
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:229
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1201
LLVM_ABI APInt usub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1948
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1182
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:380
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1488
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1111
LLVM_ABI APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1928
LLVM_ABI APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1935
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:651
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:219
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2036
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:334
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:306
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
LLVM_ABI APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1941
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:312
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:304
This class represents any memset intrinsic.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM_ABI void registerAssumption(AssumeInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
LLVM_ABI void updateAffectedValues(AssumeInst *CI)
Update the cache of values being affected by this assumption (i.e.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
LLVM_ABI bool overlaps(const AttributeMask &AM) const
Return true if the builder has any attribute that's in the specified builder.
LLVM_ABI AttributeSet getFnAttrs() const
The function attributes are returned.
static LLVM_ABI AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute > > Attrs)
Create an AttributeList with the specified parameters in it.
bool isEmpty() const
Return true if there are no attributes.
Definition: Attributes.h:1039
LLVM_ABI AttributeSet getRetAttrs() const
The attributes for the ret value are returned.
LLVM_ABI bool hasAttrSomewhere(Attribute::AttrKind Kind, unsigned *Index=nullptr) const
Return true if the specified attribute is set for at least one parameter or for the return value.
bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Return true if the attribute exists for the given argument.
Definition: Attributes.h:845
LLVM_ABI AttributeSet getParamAttrs(unsigned ArgNo) const
The attributes for the argument or parameter at the given index are returned.
AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const
Add an argument attribute to the list.
Definition: Attributes.h:644
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists in this set.
LLVM_ABI AttributeSet removeAttributes(LLVMContext &C, const AttributeMask &AttrsToRemove) const
Remove the specified attributes from this set.
Definition: Attributes.cpp:973
static LLVM_ABI AttributeSet get(LLVMContext &C, const AttrBuilder &B)
Definition: Attributes.cpp:921
static LLVM_ABI Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:95
static LLVM_ABI Attribute getWithDereferenceableBytes(LLVMContext &Context, uint64_t Bytes)
Definition: Attributes.cpp:244
static LLVM_ABI Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context, uint64_t Bytes)
Definition: Attributes.cpp:250
static LLVM_ABI Attribute getWithAlignment(LLVMContext &Context, Align Alignment)
Return a uniquified Attribute object that has the specific alignment set.
Definition: Attributes.cpp:234
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
InstListType::reverse_iterator reverse_iterator
Definition: BasicBlock.h:172
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:170
Value * getRHS() const
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.
Value * getLHS() const
static BinaryOperator * CreateFAddFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
Definition: InstrTypes.h:236
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:279
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:294
static BinaryOperator * CreateFMulFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
Definition: InstrTypes.h:244
static BinaryOperator * CreateFDivFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
Definition: InstrTypes.h:248
static BinaryOperator * CreateFSubFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
Definition: InstrTypes.h:240
static LLVM_ABI BinaryOperator * CreateNSWNeg(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1116
void setCallingConv(CallingConv::ID CC)
Definition: InstrTypes.h:1410
void setDoesNotThrow()
Definition: InstrTypes.h:1956
MaybeAlign getRetAlign() const
Extract the alignment of the return value.
Definition: InstrTypes.h:1769
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Definition: InstrTypes.h:2083
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1348
bool hasRetAttr(Attribute::AttrKind Kind) const
Determine whether the return value has the given attribute.
Definition: InstrTypes.h:1591
Value * getCalledOperand() const
Definition: InstrTypes.h:1340
void setAttributes(AttributeList A)
Set the attributes for this call.
Definition: InstrTypes.h:1427
bool doesNotThrow() const
Determine if the call cannot unwind.
Definition: InstrTypes.h:1955
void addRetAttr(Attribute::AttrKind Kind)
Adds the attribute to the return value.
Definition: InstrTypes.h:1491
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1292
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
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.
Definition: InstrTypes.h:1283
void setCalledOperand(Value *V)
Definition: InstrTypes.h:1384
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
Definition: InstrTypes.h:1290
void setCalledFunction(Function *Fn)
Sets the function called, including updating the function type.
Definition: InstrTypes.h:1387
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
void setTailCallKind(TailCallKind TCK)
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:678
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:681
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:707
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:708
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:684
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:682
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:683
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:701
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:705
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:686
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:689
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:703
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:685
@ ICMP_EQ
equal
Definition: InstrTypes.h:699
@ ICMP_NE
not equal
Definition: InstrTypes.h:700
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:694
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:829
Predicate getNonStrictPredicate() const
For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
Definition: InstrTypes.h:873
Predicate getUnorderedPredicate() const
Definition: InstrTypes.h:813
static LLVM_ABI ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1677
static LLVM_ABI Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:2246
static LLVM_ABI Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2654
static LLVM_ABI Constant * getNeg(Constant *C, bool HasNSW=false)
Definition: Constants.cpp:2635
static LLVM_ABI Constant * getInfinity(Type *Ty, bool Negative=false)
Definition: Constants.cpp:1105
static LLVM_ABI Constant * getZero(Type *Ty, bool Negative=false)
Definition: Constants.cpp:1059
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:264
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:868
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:875
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:163
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:154
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
Definition: Constants.cpp:882
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1833
static LLVM_ABI ConstantPtrAuth * get(Constant *Ptr, ConstantInt *Key, ConstantInt *Disc, Constant *AddrDisc)
Return a pointer signed with the specified parameters.
Definition: Constants.cpp:2063
This class represents a range of values.
Definition: ConstantRange.h:47
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? NOTE: false does not mean that inverse pr...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1380
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...
Definition: Constants.cpp:403
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:420
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
Definition: DataLayout.cpp:742
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:245
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:173
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.
Definition: Dominators.cpp:135
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:22
void setNoSignedZeros(bool B=true)
Definition: FMF.h:84
bool allowReassoc() const
Flag queries.
Definition: FMF.h:64
An instruction for ordering other memory operations.
Definition: Instructions.h:429
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this fence instruction.
Definition: Instructions.h:465
AtomicOrdering getOrdering() const
Returns the ordering constraint of this fence instruction.
Definition: Instructions.h:454
Class to represent function types.
Definition: DerivedTypes.h:105
Type::subtype_iterator param_iterator
Definition: DerivedTypes.h:128
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:610
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:209
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:270
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:352
bool doesNotThrow() const
Determine if the function cannot unwind.
Definition: Function.h:594
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition: Function.h:249
Represents calls to the gc.relocate intrinsic.
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...
Represents a gc.statepoint intrinsic call.
Definition: Statepoint.h:61
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 current metadata attachments for the given kind, if any.
Definition: Value.h:576
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:316
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:296
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:114
Value * CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2394
Value * CreateLdexp(Value *Src, Value *Exp, FMFSource FMFSource={}, const Twine &Name="")
Create call to the ldexp intrinsic.
Definition: IRBuilder.h:1065
LLVM_ABI Value * CreateLaunderInvariantGroup(Value *Ptr)
Create a launder.invariant.group intrinsic call.
Definition: IRBuilder.cpp:1038
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2449
IntegerType * getInt1Ty()
Fetch the type representing a single bit.
Definition: IRBuilder.h:547
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2625
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2559
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Definition: IRBuilder.h:575
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
Definition: IRBuilder.h:1864
Value * CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2399
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition: IRBuilder.h:2100
LLVM_ABI CallInst * CreateAndReduce(Value *Src)
Create a vector int AND reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:374
LLVM_ABI CallInst * CreateAssumption(Value *Cond, ArrayRef< OperandBundleDef > OpBundles={})
Create an assume intrinsic call that allows the optimizer to assume that the provided condition will ...
Definition: IRBuilder.cpp:463
LLVM_ABI Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.cpp:1115
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:502
LLVM_ABI Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1005
InvokeInst * CreateInvoke(FunctionType *Ty, Value *Callee, BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > OpBundles, const Twine &Name="")
Create an invoke instruction.
Definition: IRBuilder.h:1235
Value * CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2434
LLVM_ABI CallInst * CreateAddReduce(Value *Src)
Create a vector int add reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:366
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1513
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:562
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr, FMFSource FMFSource={})
Definition: IRBuilder.h:2238
Value * CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1446
LLVM_ABI Value * CreateVectorReverse(Value *V, const Twine &Name="")
Return a vector value that contains the vector V reversed.
Definition: IRBuilder.cpp:1071
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2333
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Definition: IRBuilder.h:1781
LLVM_ABI CallInst * CreateOrReduce(Value *Src)
Create a vector int OR reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:378
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.
Definition: IRBuilder.cpp:823
LLVM_ABI CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition: IRBuilder.cpp:834
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:522
Value * CreateBitOrPointerCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2286
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1805
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2329
Value * CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2409
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1420
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2204
Value * CreateCopySign(Value *LHS, Value *RHS, FMFSource FMFSource={}, const Twine &Name="")
Create call to the copysign intrinsic.
Definition: IRBuilder.h:1058
LLVM_ABI CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
Definition: IRBuilder.cpp:815
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition: IRBuilder.h:1847
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2082
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:2593
Value * CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2369
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1551
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1860
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1403
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2194
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:507
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Definition: IRBuilder.h:2651
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2508
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2068
PointerType * getPtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer.
Definition: IRBuilder.h:605
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1708
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:2277
Value * CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2404
Value * CreateIsNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg == 0.
Definition: IRBuilder.h:2646
Value * CreateFNegFMF(Value *V, FMFSource FMFSource, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1795
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2439
Value * CreateFNeg(Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1790
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
Definition: IRBuilder.h:1573
Value * CreateAddrSpaceCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2209
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1437
LLVM_ABI Value * CreateElementCount(Type *Ty, ElementCount EC)
Create an expression which evaluates to the number of elements in EC at runtime.
Definition: IRBuilder.cpp:123
LLVM_ABI Value * CreateStripInvariantGroup(Value *Ptr)
Create a strip.invariant.group intrinsic call.
Definition: IRBuilder.cpp:1054
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false)
Given an instruction with a select as one operand and a constant as the other operand,...
KnownFPClass computeKnownFPClass(Value *Val, FastMathFlags FMF, FPClassTest Interested=fcAllFlags, const Instruction *CtxI=nullptr, unsigned Depth=0) const
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 * SimplifyAnyMemSet(AnyMemSetInst *MI)
Constant * getLosslessUnsignedTrunc(Constant *C, Type *TruncTy)
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)
Constant * getLosslessSignedTrunc(Constant *C, Type *TruncTy)
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, ShMask) = C Returns nullptr if such a constant ...
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.
SimplifyQuery SQ
Definition: InstCombiner.h:77
unsigned ComputeMaxSignificantBits(const Value *Op, const Instruction *CxtI=nullptr, unsigned Depth=0) const
Definition: InstCombiner.h:462
bool isFreeToInvert(Value *V, bool WillInvertAllUses, bool &DoesConsume)
Return true if the specified value is free to invert (apply ~ to).
Definition: InstCombiner.h:228
DominatorTree & getDominatorTree() const
Definition: InstCombiner.h:336
BlockFrequencyInfo * BFI
Definition: InstCombiner.h:79
TargetLibraryInfo & TLI
Definition: InstCombiner.h:74
Instruction * InsertNewInstBefore(Instruction *New, BasicBlock::iterator Old)
Inserts an instruction New before instruction Old.
Definition: InstCombiner.h:368
AAResults * AA
Definition: InstCombiner.h:70
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Definition: InstCombiner.h:388
void replaceUse(Use &U, Value *NewValue)
Replace use and add the previously used value to the worklist.
Definition: InstCombiner.h:420
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
Definition: InstCombiner.h:65
const DataLayout & DL
Definition: InstCombiner.h:76
DomConditionCache DC
Definition: InstCombiner.h:82
void computeKnownBits(const Value *V, KnownBits &Known, const Instruction *CxtI, unsigned Depth=0) const
Definition: InstCombiner.h:433
std::optional< Instruction * > targetInstCombineIntrinsic(IntrinsicInst &II)
AssumptionCache & AC
Definition: InstCombiner.h:73
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
Definition: InstCombiner.h:412
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const Instruction *CxtI=nullptr, unsigned Depth=0) const
Definition: InstCombiner.h:450
DominatorTree & DT
Definition: InstCombiner.h:75
ProfileSummaryInfo * PSI
Definition: InstCombiner.h:81
BuilderTy & Builder
Definition: InstCombiner.h:61
AssumptionCache & getAssumptionCache() const
Definition: InstCombiner.h:334
OptimizationRemarkEmitter & ORE
Definition: InstCombiner.h:78
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
Definition: InstCombiner.h:209
const SimplifyQuery & getSimplifyQuery() const
Definition: InstCombiner.h:338
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, const Instruction *CxtI=nullptr, unsigned Depth=0)
Definition: InstCombiner.h:443
void pushUsersToWorkList(Instruction &I)
When an instruction is simplified, add all users of the instruction to the work lists because they mi...
void add(Instruction *I)
Add instruction to the worklist.
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.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:78
LLVM_ABI void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
Definition: Metadata.cpp:1804
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 const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:82
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1718
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:312
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.
Definition: Instruction.h:510
LLVM_ABI void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
Class to represent integer types.
Definition: DerivedTypes.h:42
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:319
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:49
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:56
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
LibCallSimplifier - This class implements a collection of optimizations that replace well formed call...
An instruction for reading from memory.
Definition: Instructions.h:180
Metadata node.
Definition: Metadata.h:1077
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1565
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
A container for an operand bundle being viewed as a set of values rather than a set of uses.
Definition: InstrTypes.h:1069
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:111
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
Definition: Operator.h:105
bool isCommutative() const
Return true if the instruction is commutative.
Definition: Operator.h:128
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1885
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, 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
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.
Definition: SmallPtrSet.h:470
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:401
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:938
void reserve(size_type N)
Definition: SmallVector.h:664
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
An instruction for storing to memory.
Definition: Instructions.h:296
void setVolatile(bool V)
Specify whether this is a volatile store or not.
Definition: Instructions.h:333
void setAlignment(Align Align)
Definition: Instructions.h:342
void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this store instruction.
Definition: Instructions.h:353
Class to represent struct types.
Definition: DerivedTypes.h:218
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:45
LLVM_ABI bool canLosslesslyBitCastTo(Type *Ty) const
Return true if this type could be converted with a lossless BitCast to type 'Ty'.
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:267
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
LLVM_ABI Type * getWithNewBitWidth(unsigned NewBitWidth) const
Given an integer or vector type, change the lane bitwidth to NewBitwidth, whilst keeping the old numb...
bool isStructTy() const
True if this is an instance of StructType.
Definition: Type.h:261
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
LLVM_ABI const fltSemantics & getFltSemantics() const
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:240
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:139
LLVM_ABI unsigned getIntegerBitWidth() const
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:352
static UnaryOperator * CreateWithCopiedFlags(UnaryOps Opc, Value *V, Instruction *CopyO, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Definition: InstrTypes.h:139
static UnaryOperator * CreateFNegFMF(Value *Op, Instruction *FMFSource, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Definition: InstrTypes.h:147
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1866
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:35
void setOperand(unsigned i, Value *Val)
Definition: User.h:237
Value * getOperand(unsigned i) const
Definition: User.h:232
This represents the llvm.va_end intrinsic.
static LLVM_ABI void ValueIsDeleted(Value *V)
Definition: Value.cpp:1225
static LLVM_ABI void ValueIsRAUWd(Value *Old, Value *New)
Definition: Value.cpp:1278
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
static constexpr uint64_t MaximumAlignment
Definition: Value.h:830
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1571
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:439
iterator_range< user_iterator > users()
Definition: Value.h:426
static LLVM_ABI void dropDroppableUse(Use &U)
Remove the droppable use U.
Definition: Value.cpp:226
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:701
bool use_empty() const
Definition: Value.h:346
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1098
static constexpr unsigned MaxAlignmentExponent
The maximum alignment for instructions.
Definition: Value.h:829
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:396
Base class of all SIMD vector types.
Definition: DerivedTypes.h:430
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Definition: DerivedTypes.h:695
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Represents an op.with.overflow intrinsic.
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:219
static constexpr bool isKnownGT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:226
const ParentTy * getParent() const
Definition: ilist_node.h:34
self_iterator getIterator()
Definition: ilist_node.h:134
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:359
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
Key
PAL metadata keys.
LLVM_ABI AttributeMask typeIncompatible(Type *Ty, AttributeSet AS, AttributeSafetyKind ASK=ASK_ALL)
Which attributes cannot be applied to a type.
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.
Definition: BitmaskEnum.h:126
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
Definition: Intrinsics.cpp:751
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
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)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
class_match< PoisonValue > m_Poison()
Match an arbitrary poison constant.
Definition: PatternMatch.h:160
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
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)
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)
Definition: PatternMatch.h:49
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:862
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:962
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'.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:592
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.
Definition: PatternMatch.h:784
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
Definition: PatternMatch.h:245
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
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.
deferredval_ty< 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()...
Definition: PatternMatch.h:980
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
Definition: PatternMatch.h:305
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.
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)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
Definition: PatternMatch.h:627
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:931
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.
Definition: PatternMatch.h:481
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
class_match< UnaryOperator > m_UnOp()
Match an arbitrary unary operation and ignore it.
Definition: PatternMatch.h:95
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
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".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
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.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
Definition: PatternMatch.h:775
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:316
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:152
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(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.
Definition: PatternMatch.h:612
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.
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
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.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_CopySign(const Opnd0 &Op0, const Opnd1 &Op1)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
@ 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:201
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
constexpr double e
Definition: MathExtras.h:47
DiagnosticInfoOptimizationBase::Argument NV
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI cl::opt< bool > EnableKnowledgeRetention
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:349
@ Offset
Definition: DWP.cpp:477
OverflowResult
@ 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 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 ...
LLVM_ABI RetainedKnowledge simplifyRetainedKnowledge(AssumeInst *Assume, RetainedKnowledge RK, AssumptionCache *AC, DominatorTree *DT)
canonicalize the RetainedKnowledge RK.
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 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 Value * getAllocAlignment(const CallBase *V, const TargetLibraryInfo *TLI)
Gets the alignment argument for an aligned_alloc-like function, using either built-in knowledge based...
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition: APFloat.h:1643
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:551
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:293
LLVM_ABI bool isAssumeWithEmptyBundle(const AssumeInst &Assume)
Return true iff the operand bundles of the provided llvm.assume doesn't contain any valuable informat...
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.
constexpr T MinAlign(U A, V B)
A and B are either alignments or offsets.
Definition: MathExtras.h:362
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
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:252
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:1598
LLVM_ABI FPClassTest fneg(FPClassTest Mask)
Return the test mask which returns true if the value's sign bit is flipped.
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:288
bool isModSet(const ModRefInfo MRI)
Definition: ModRef.h:49
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
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...
@ None
Definition: CodeGenData.h:107
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 ...
Definition: Function.cpp:1172
auto find_if_not(R &&Range, UnaryPredicate P)
Definition: STLExtras.h:1782
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
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:1758
bool isAtLeastOrStrongerThan(AtomicOrdering AO, AtomicOrdering Other)
LLVM_ABI AssumeInst * buildAssumeFromKnowledge(ArrayRef< RetainedKnowledge > Knowledge, Instruction *CtxI, AssumptionCache *AC=nullptr, DominatorTree *DT=nullptr)
Build and return a new assume created from the provided knowledge if the knowledge in the assume is f...
LLVM_ABI FPClassTest inverse_fabs(FPClassTest Mask)
Return the test mask which returns true after fabs is applied to the value.
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
LLVM_ABI bool maskIsAllOneOrUndef(Value *Mask)
Given a mask vector of i1, Return true if all of the elements of this predicate mask are known to be ...
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.
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.
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:1579
@ 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.
LLVM_ABI ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
bool isSafeToSpeculativelyExecuteWithVariableReplaced(const Instruction *I, bool IgnoreUBImplyingAttrs=true)
Don't use information from its non-constant operands.
LLVM_ABI Value * getFreedOperand(const CallBase *CB, const TargetLibraryInfo *TLI)
If this if a call to a free function, return the freed operand.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:223
LLVM_ABI bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if this is always a dereferenceable pointer.
Definition: Loads.cpp:252
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1916
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.
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
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_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition: APFloat.h:1616
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_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 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 bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
Definition: VectorUtils.cpp:46
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.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:858
#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:760
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.
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:101
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:235
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:267
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:282
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:44
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition: KnownBits.h:104
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:241
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:98
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:273
unsigned countMinPopulation() const
Returns the number of bits known to be one.
Definition: KnownBits.h:279
bool isAllOnes() const
Returns true if value is all one bits.
Definition: KnownBits.h:83
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
Definition: KnownFPClass.h:25
Matching combinators.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:141
A lightweight accessor for an operand bundle meant to be passed around by value.
Definition: InstrTypes.h:1011
StringRef getTagName() const
Return the tag of this operand bundle as a string.
Definition: InstrTypes.h:1030
uint32_t getTagID() const
Return the tag of this operand bundle as an integer.
Definition: InstrTypes.h:1039
ArrayRef< Use > Inputs
Definition: InstrTypes.h:1012
Represent one information held inside an operand bundle of an llvm.assume.
Attribute::AttrKind AttrKind
SelectPatternFlavor Flavor
const DataLayout & DL
Definition: SimplifyQuery.h:72
const Instruction * CxtI
Definition: SimplifyQuery.h:76
SimplifyQuery getWithInstruction(const Instruction *I) const