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