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