LLVM: lib/Transforms/InstCombine/InstCombineInternal.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
16#define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
17
33#include
34
35#define DEBUG_TYPE "instcombine"
37
38
39
41
42
43
45
46namespace llvm {
47
48class AAResults;
49class APInt;
50class AssumptionCache;
51class BlockFrequencyInfo;
52class DataLayout;
53class DominatorTree;
54class GEPOperator;
55class GlobalVariable;
56class OptimizationRemarkEmitter;
57class ProfileSummaryInfo;
58class TargetLibraryInfo;
59class User;
60
63 public InstVisitor<InstCombinerImpl, Instruction *> {
64public:
72 : InstCombiner(Worklist, Builder, F, AA, AC, TLI, TTI, DT, ORE, BFI, BPI,
74
76
77
79
80
81
82
83 bool run();
84
85
86
87
88
89
90
91
122 bool AnalyzeForSignBitExtraction = false);
157
188
189
191
192
193
194
197
198
200 const unsigned SIOpd);
201
203 const Twine &Suffix = "");
204
208 unsigned Depth = 0) const {
210 Val, FMF, Interested, getSimplifyQuery().getWithInstruction(CtxI),
212 }
213
217 unsigned Depth = 0) const {
220 }
221
222
223
226
227 std::optional<std::pair<Intrinsic::ID, SmallVector<Value *, 3>>>
229
230private:
232 bool isDesirableIntType(unsigned BitWidth) const;
233 bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
234 bool shouldChangeType(Type *From, Type *To) const;
235 Value *dyn_castNegVal(Value *V) const;
236
237
238
239
240
241
242
243
244
245
246 bool shouldOptimizeCast(CastInst *CI);
247
248
249
250
251
252
253
254
255
256
257
258
259
264
267 bool transformConstExprCastCall(CallBase &Call);
270
271
272
273
274
275
276
278
279
280
281
283
284
285
286
287
288
289
290 std::optional<std::pair<Value *, Value *>> matchSymmetricPair(Value *LHS,
292
297
298
299
300
301
302
303
304
305
306
308
310
316 }
317
323 }
324
325 bool willNotOverflowAdd(const Value *LHS, const Value *RHS,
326 const Instruction &CxtI, bool IsSigned) const {
327 return IsSigned ? willNotOverflowSignedAdd(LHS, RHS, CxtI)
328 : willNotOverflowUnsignedAdd(LHS, RHS, CxtI);
329 }
330
331 bool willNotOverflowSignedSub(const Value *LHS, const Value *RHS,
332 const Instruction &CxtI) const {
334 OverflowResult::NeverOverflows;
335 }
336
337 bool willNotOverflowUnsignedSub(const Value *LHS, const Value *RHS,
338 const Instruction &CxtI) const {
340 OverflowResult::NeverOverflows;
341 }
342
344 const Instruction &CxtI, bool IsSigned) const {
345 return IsSigned ? willNotOverflowSignedSub(LHS, RHS, CxtI)
346 : willNotOverflowUnsignedSub(LHS, RHS, CxtI);
347 }
348
349 bool willNotOverflowSignedMul(const Value *LHS, const Value *RHS,
350 const Instruction &CxtI) const {
352 OverflowResult::NeverOverflows;
353 }
354
355 bool willNotOverflowUnsignedMul(const Value *LHS, const Value *RHS,
356 const Instruction &CxtI,
357 bool IsNSW = false) const {
359 OverflowResult::NeverOverflows;
360 }
361
363 const Instruction &CxtI, bool IsSigned) const {
364 return IsSigned ? willNotOverflowSignedMul(LHS, RHS, CxtI)
365 : willNotOverflowUnsignedMul(LHS, RHS, CxtI);
366 }
367
369 const Value *RHS, const Instruction &CxtI,
370 bool IsSigned) const {
371 switch (Opcode) {
372 case Instruction::Add: return willNotOverflowAdd(LHS, RHS, CxtI, IsSigned);
373 case Instruction::Sub: return willNotOverflowSub(LHS, RHS, CxtI, IsSigned);
374 case Instruction::Mul: return willNotOverflowMul(LHS, RHS, CxtI, IsSigned);
375 default: llvm_unreachable("Unexpected opcode for overflow query");
376 }
377 }
378
379 Value *EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP = false);
380
381
382 Value *EmitGEPOffsets(ArrayRef<GEPOperator *> GEPs, GEPNoWrapFlags NW,
383 Type *IdxTy, bool RewriteGEPs);
384 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
385 Instruction *foldBitcastExtElt(ExtractElementInst &ExtElt);
386 Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
387 Instruction *foldFBinOpOfIntCasts(BinaryOperator &I);
388
389 Instruction *foldFBinOpOfIntCastsFromSign(
390 BinaryOperator &BO, bool OpsFromSigned, std::array<Value *, 2> IntOps,
391 Constant *Op1FpC, SmallVectorImpl<WithCache<const Value *>> &OpsKnown);
392 Instruction *foldBinopOfSextBoolToSelect(BinaryOperator &I);
393 Instruction *narrowBinOp(TruncInst &Trunc);
394 Instruction *narrowMaskedBinOp(BinaryOperator &And);
395 Instruction *narrowMathIfNoOverflow(BinaryOperator &I);
396 Instruction *narrowFunnelShift(TruncInst &Trunc);
397 Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
398 Instruction *matchSAddSubSat(IntrinsicInst &MinMax1);
400 Instruction *foldBinOpOfDisplacedShifts(BinaryOperator &I);
401
402
403
404
405
406
407
408
409
410
411
412 Instruction::CastOps isEliminableCastPair(const CastInst *CI1,
413 const CastInst *CI2);
414 Value *simplifyIntToPtrRoundTripCast(Value *Val);
415
416 Value *foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &I,
417 bool IsAnd, bool IsLogical = false);
418 Value *foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS, BinaryOperator &Xor);
419
420 Value *foldEqOfParts(Value *Cmp0, Value *Cmp1, bool IsAnd);
421
422 Value *foldAndOrOfICmpsUsingRanges(ICmpInst *ICmp1, ICmpInst *ICmp2,
423 bool IsAnd);
424
425
426
427
428 Value *foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS, bool IsAnd,
429 bool IsLogicalSelect = false);
430
433
435 bool IsLogical);
436
438 bool IsAnd, bool RHSIsLogical);
439
441
443
445 canonicalizeConditionalNegationViaMathToSelect(BinaryOperator &i);
446
448 bool InvertFalseVal = false);
450
451 Instruction *foldLShrOverflowBit(BinaryOperator &I);
452 Instruction *foldExtractOfOverflowIntrinsic(ExtractValueInst &EV);
453 Instruction *foldIntrinsicWithOverflowCommon(IntrinsicInst *II);
454 Instruction *foldIntrinsicIsFPClass(IntrinsicInst &II);
455 Instruction *foldFPSignBitOps(BinaryOperator &I);
456 Instruction *foldFDivConstantDivisor(BinaryOperator &I);
457
458
459
460
461
462 Instruction *foldAndOrOfSelectUsingImpliedCond(Value *Op, SelectInst &SI,
463 bool IsAnd);
464
465 Instruction *hoistFNegAboveFMulFDiv(Value *FNegOp, Instruction &FMFSource);
466
467
468
469
470
471 Value *simplifyNonNullOperand(Value *V, bool HasDereferenceable,
472 unsigned Depth = 0);
473
474
475
476
477 SelectInst *
479 const Twine &NameStr = "",
480 InsertPosition InsertBefore = nullptr) {
481 auto *Sel = SelectInst::Create(C, S1, S2, NameStr, InsertBefore, nullptr);
483 return Sel;
484 }
485
486public:
487
488
493 false, Align(1));
495 }
496
497
498
499
500
501
504 assert(I.use_empty() && "Cannot erase instruction that is used!");
506
507
508
512 I.eraseFromParent();
514 Worklist.handleUseCountDecrement(Op);
516 return nullptr;
517 }
518
522
523
524
525 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
526
527
528
529
530
531
532
533
535
536
537
538
539
540
542
543
544
547
548
549
551
552
553
554
555
557
558
559
560
561
563
564
565
567
568
569
570
571
572
576
577
578
581 unsigned Depth = 0);
583 bool SimplifyDemandedBits(Instruction *I, unsigned Op,
586 unsigned Depth = 0) override;
587
588
589
590
592 const APInt &DemandedMask,
595 unsigned Depth = 0);
596
597
598
599 Value *simplifyShrShlDemandedBits(
602
603
604
605 bool SimplifyDemandedInstructionBits(Instruction &Inst);
607
608 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
609 APInt &PoisonElts, unsigned Depth = 0,
610 bool AllowMultipleUsers = false) override;
611
612
613
616 unsigned Depth = 0);
617 bool SimplifyDemandedFPClass(Instruction *I, unsigned Op,
619 unsigned Depth = 0);
620
621
623 bool NUW);
624
625
631
632
633
634
636 bool AllowMultipleUses = false);
637
638
639
640
641
642
643
644
645
646
647
648
649
651
652
653
654
655
656
657
658
659
661
662
663
664
665
667 bool FoldWithMultiUse = false,
668 bool SimplifyBothArms = false);
669
670
672
674
677
678
679
688
689
690
691
693
694
695
696
698
699
700
702
718
736
738
770 const APInt &C1);
776 const APInt &C2);
778 const APInt &C2);
779
794
795
807 Value *FalseVal);
810 unsigned Depth = 0);
811
815
816
817
818
820 bool MatchBitReversals);
821
824
826
831
840
841
842
843
844
846
848 if (takeLog2(Op, 0, AssumeNonZero, false))
849 return takeLog2(Op, 0, AssumeNonZero, true);
850 return nullptr;
851 }
852};
853
854class Negator final {
855
857
859 BuilderTy Builder;
860
862
863 const bool IsTrulyNegation;
864
866
868 bool IsTrulyNegation);
869
870#if LLVM_ENABLE_STATS
871 unsigned NumValuesVisitedInThisNegator = 0;
872 ~Negator();
873#endif
874
875 using Result = std::pair<ArrayRef<Instruction *> ,
876 Value * >;
877
878 std::array<Value *, 2> getSortedOperandsOfBinOp(Instruction *I);
879
880 [[nodiscard]] Value *visitImpl(Value *V, bool IsNSW, unsigned Depth);
881
882 [[nodiscard]] Value *negate(Value *V, bool IsNSW, unsigned Depth);
883
884
885
886 [[nodiscard]] std::optional run(Value *Root, bool IsNSW);
887
888 Negator(const Negator &) = delete;
889 Negator(Negator &&) = delete;
890 Negator &operator=(const Negator &) = delete;
891 Negator &operator=(Negator &&) = delete;
892
893public:
894
895
896 [[nodiscard]] static Value *Negate(bool LHSIsZero, bool IsNSW, Value *Root,
898};
899
901
903
905
907
909
911
913
914
916};
917
918}
919
920#undef DEBUG_TYPE
921
922#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool foldICmpWithDominatingICmp(CmpInst *Cmp, const TargetLowering &TLI)
For pattern like:
#define LLVM_LIBRARY_VISIBILITY
static bool isSigned(unsigned int Opcode)
static constexpr unsigned NegatorMaxNodesSSO
Definition InstCombineInternal.h:44
static constexpr unsigned NegatorDefaultMaxDepth
Definition InstCombineInternal.h:40
This file provides the interface for the instcombine pass implementation.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
uint64_t IntrinsicInst * II
StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
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 OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const AddOperator *Add, const SimplifyQuery &SQ)
static const uint32_t IV[8]
Class for arbitrary precision integers.
This class represents a conversion between pointers from one address space to another.
an instruction to allocate memory on the stack
This class represents any memset intrinsic.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
A cache of @llvm.assume calls within a function.
an instruction that atomically reads a memory location, combines it with another value,...
LLVM Basic Block Representation.
InstListType::iterator iterator
Instruction iterators...
This class represents a no-op cast from one type to another.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Conditional or Unconditional Branch instruction.
Analysis providing branch probability information.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
This class is the base class for the comparison instructions.
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
This is the shared class of boolean and integer constants.
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
This is an important base class in LLVM.
A parsed version of the target data layout string in and methods for querying it.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
This instruction compares its operands according to the predicate given to the constructor.
This class represents a cast from floating point to signed integer.
This class represents a cast from floating point to unsigned integer.
This class represents a truncation of floating point types.
Convenience struct for specifying and reasoning about fast-math flags.
An instruction for ordering other memory operations.
This class represents a freeze function that returns random concrete value if an operand is either a ...
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags all()
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
This instruction compares its operands according to the predicate given to the constructor.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
This instruction inserts a single (scalar) element into a VectorType value.
This instruction inserts a struct field of array element value into an aggregate value.
Instruction * visitMul(BinaryOperator &I)
Instruction * foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr, const APInt &C)
Fold icmp ({al}shr X, Y), C.
Instruction * foldICmpWithZextOrSext(ICmpInst &ICmp)
Instruction * foldICmpSelectConstant(ICmpInst &Cmp, SelectInst *Select, ConstantInt *C)
Instruction * foldICmpSRemConstant(ICmpInst &Cmp, BinaryOperator *UDiv, const APInt &C)
Instruction * foldSelectToCmp(SelectInst &SI)
Instruction * visitAdd(BinaryOperator &I)
bool fmulByZeroIsZero(Value *MulVal, FastMathFlags FMF, const Instruction *CtxI) const
Check if fmul MulVal, +0.0 will yield +0.0 (or signed zero is ignorable).
Instruction * foldICmpBinOpWithConstant(ICmpInst &Cmp, BinaryOperator *BO, const APInt &C)
Fold an icmp with BinaryOp and constant operand: icmp Pred BO, C.
Instruction * foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or, const APInt &C)
Fold icmp (or X, Y), C.
Instruction * canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(BinaryOperator &I)
Instruction * foldICmpTruncWithTruncOrExt(ICmpInst &Cmp, const SimplifyQuery &Q)
Fold icmp (trunc nuw/nsw X), (trunc nuw/nsw Y).
KnownFPClass computeKnownFPClass(Value *Val, FastMathFlags FMF, FPClassTest Interested=fcAllFlags, const Instruction *CtxI=nullptr, unsigned Depth=0) const
Definition InstCombineInternal.h:205
Instruction * visitLShr(BinaryOperator &I)
Instruction * foldBinOpIntoSelectOrPhi(BinaryOperator &I)
This is a convenience wrapper function for the above two functions.
Instruction * visitUDiv(BinaryOperator &I)
Instruction * visitOr(BinaryOperator &I)
Instruction * foldSignBitTest(ICmpInst &I)
Fold equality-comparison between zero and any (maybe truncated) right-shift by one-less-than-bitwidth...
Instruction * foldSelectEqualityTest(SelectInst &SI)
Instruction * visitZExt(ZExtInst &Zext)
Instruction * visitGEPOfGEP(GetElementPtrInst &GEP, GEPOperator *Src)
Instruction * foldSelectValueEquivalence(SelectInst &SI, CmpInst &CI)
Instruction * visitAddrSpaceCast(AddrSpaceCastInst &CI)
Instruction * foldPHIArgInsertValueInstructionIntoPHI(PHINode &PN)
If we have something like phi [insertvalue(a,b,0), insertvalue(c,d,0)], turn this into a phi[a,...
~InstCombinerImpl() override=default
Instruction * visitSExt(SExtInst &Sext)
Instruction * visitUnreachableInst(UnreachableInst &I)
Instruction * visitURem(BinaryOperator &I)
Instruction * foldSquareSumInt(BinaryOperator &I)
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,...
InstCombinerImpl(InstructionWorklist &Worklist, BuilderTy &Builder, Function &F, AAResults *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, TargetTransformInfo &TTI, DominatorTree &DT, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI, const DataLayout &DL, ReversePostOrderTraversal< BasicBlock * > &RPOT)
Definition InstCombineInternal.h:65
Value * insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi, bool isSigned, bool Inside)
Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise (V < Lo || V >= Hi).
void handleUnreachableFrom(Instruction *I, SmallVectorImpl< BasicBlock * > &Worklist)
Instruction * foldICmpBinOp(ICmpInst &Cmp, const SimplifyQuery &SQ)
Try to fold icmp (binop), X or icmp X, (binop).
Instruction * foldVectorSelect(SelectInst &Sel)
Instruction * foldCmpLoadFromIndexedGlobal(LoadInst *LI, GetElementPtrInst *GEP, CmpInst &ICI, ConstantInt *AndCst=nullptr)
This is called when we see this pattern: cmp pred (load (gep GV, ...)), cmpcst where GV is a global v...
Instruction * visitFreeze(FreezeInst &I)
Instruction * foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub, const APInt &C)
Fold icmp (sub X, Y), C.
Instruction * foldSelectShuffle(ShuffleVectorInst &Shuf)
Try to fold shuffles that are the equivalent of a vector select.
Instruction * visitLoadInst(LoadInst &LI)
Value * takeLog2(Value *Op, unsigned Depth, bool AssumeNonZero, bool DoFold)
Take the exact integer log2 of the value.
Instruction * visitFPToSI(FPToSIInst &FI)
Instruction * foldICmpWithClamp(ICmpInst &Cmp, Value *X, MinMaxIntrinsic *Min)
Match and fold patterns like: icmp eq/ne X, min(max(X, Lo), Hi) which represents a range check and ca...
Instruction * foldICmpInstWithConstantNotInt(ICmpInst &Cmp)
Handle icmp with constant (but not simple integer constant) RHS.
Instruction * visitAtomicRMWInst(AtomicRMWInst &SI)
Instruction * visitSRem(BinaryOperator &I)
Instruction * foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1, Value *A, Value *B, Instruction &Outer, SelectPatternFlavor SPF2, Value *C)
Instruction * visitTrunc(TruncInst &CI)
Instruction * foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1, const APInt &C2)
Handle "(icmp eq/ne (shl AP2, A), AP1)" -> (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
Instruction * foldSquareSumFP(BinaryOperator &I)
Value * reassociateShiftAmtsOfTwoSameDirectionShifts(BinaryOperator *Sh0, const SimplifyQuery &SQ, bool AnalyzeForSignBitExtraction=false)
Instruction * foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI)
We have (select c, TI, FI), and we know that TI and FI have the same opcode.
Instruction * visitUIToFP(CastInst &CI)
Instruction * foldPHIArgBinOpIntoPHI(PHINode &PN)
If we have something like phi [add (a,b), add(a,c)] and if a/b/c and the adds all have a single user,...
void handlePotentiallyDeadBlocks(SmallVectorImpl< BasicBlock * > &Worklist)
bool sinkNotIntoLogicalOp(Instruction &I)
Instruction * foldICmpEqIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II, const APInt &C)
Fold an equality icmp with LLVM intrinsic and constant operand.
Instruction * visitPtrToInt(PtrToIntInst &CI)
bool prepareWorklist(Function &F)
Perform early cleanup and prepare the InstCombine worklist.
std::optional< std::pair< Intrinsic::ID, SmallVector< Value *, 3 > > > convertOrOfShiftsToFunnelShift(Instruction &Or)
Instruction * visitFDiv(BinaryOperator &I)
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)
bool simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I)
Fold a divide or remainder with a select instruction divisor when one of the select operands is zero.
Instruction * visitSIToFP(CastInst &CI)
Instruction * visitSub(BinaryOperator &I)
Instruction * visitAShr(BinaryOperator &I)
bool replaceInInstruction(Value *V, Value *Old, Value *New, unsigned Depth=0)
Instruction * visitFree(CallInst &FI, Value *FreedOp)
Instruction * visitInsertValueInst(InsertValueInst &IV)
Try to find redundant insertvalue instructions, like the following ones: %0 = insertvalue { i8,...
Instruction * visitAnd(BinaryOperator &I)
Value * foldMultiplicationOverflowCheck(ICmpInst &Cmp)
Fold (-1 u/ x) u< y ((x * y) ?
Instruction * visitCallBrInst(CallBrInst &CBI)
Instruction * visitExtractValueInst(ExtractValueInst &EV)
Instruction * visitInsertElementInst(InsertElementInst &IE)
void handlePotentiallyDeadSuccessors(BasicBlock *BB, BasicBlock *LiveSucc)
Instruction * visitUnconditionalBranchInst(BranchInst &BI)
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
Instruction * foldICmpWithConstant(ICmpInst &Cmp)
Fold icmp Pred X, C.
CmpInst * canonicalizeICmpPredicate(CmpInst &I)
If we have a comparison with a non-canonical predicate, if we can update all the users,...
Instruction * foldBinopWithRecurrence(BinaryOperator &BO)
Try to fold binary operators whose operands are simple interleaved recurrences to a single recurrence...
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Definition InstCombineInternal.h:502
Instruction * foldICmpWithZero(ICmpInst &Cmp)
Instruction * visitExtractElementInst(ExtractElementInst &EI)
Instruction * commonIDivRemTransforms(BinaryOperator &I)
Common integer divide/remainder transforms.
Value * foldReversedIntrinsicOperands(IntrinsicInst *II)
If all arguments of the intrinsic are reverses, try to pull the reverse after the intrinsic.
Instruction * visitPHINode(PHINode &PN)
Instruction * foldICmpCommutative(CmpPredicate Pred, Value *Op0, Value *Op1, ICmpInst &CxtI)
Instruction * foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp, BinaryOperator *BO, const APInt &C)
Fold an icmp equality instruction with binary operator LHS and constant RHS: icmp eq/ne BO,...
Instruction * foldItoFPtoI(CastInst &FI)
fpto{s/u}i({u/s}itofp(X)) --> X or zext(X) or sext(X) or trunc(X) This is safe if the intermediate ty...
Instruction * foldPHIArgOpIntoPHI(PHINode &PN)
Try to rotate an operation below a PHI node, using PHI nodes for its operands.
Instruction * visitLandingPadInst(LandingPadInst &LI)
Instruction * foldICmpUsingBoolRange(ICmpInst &I)
If one operand of an icmp is effectively a bool (value range of {0,1}), then try to reduce patterns b...
Instruction * foldICmpWithTrunc(ICmpInst &Cmp)
Instruction * foldICmpIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II, const APInt &C)
Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
Instruction * visitFPTrunc(FPTruncInst &CI)
Instruction * visitStoreInst(StoreInst &SI)
Value * tryGetLog2(Value *Op, bool AssumeNonZero)
Definition InstCombineInternal.h:847
Instruction * foldPHIArgZextsIntoPHI(PHINode &PN)
TODO: This function could handle other cast types, but then it might require special-casing a cast fr...
Instruction * foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI)
Instruction * visitFenceInst(FenceInst &FI)
Value * foldPtrToIntOrAddrOfGEP(Type *IntTy, Value *Ptr)
Instruction * visitFCmpInst(FCmpInst &I)
Value * OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty, bool isNUW)
Optimize pointer differences into the same array into a size.
Instruction * visitBitCast(BitCastInst &CI)
Instruction * visitReturnInst(ReturnInst &RI)
bool sinkNotIntoOtherHandOfLogicalOp(Instruction &I)
Instruction * commonIDivTransforms(BinaryOperator &I)
This function implements the transforms common to both integer division instructions (udiv and sdiv).
Instruction * foldICmpUsingKnownBits(ICmpInst &Cmp)
Try to fold the comparison based on range information we can get by checking whether bits are known t...
Instruction * foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div, const APInt &C)
Fold icmp ({su}div X, Y), C.
Instruction * foldIRemByPowerOfTwoToBitTest(ICmpInst &I)
If we have: icmp eq/ne (urem/srem x, y), 0 iff y is a power-of-two, we can replace this with a bit te...
Instruction * foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI, Constant *RHSC)
Fold fcmp ([us]itofp x, cst) if possible.
Instruction * visitShl(BinaryOperator &I)
Instruction * visitSwitchInst(SwitchInst &SI)
Instruction * foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv, const APInt &C)
Fold icmp (udiv X, Y), C.
Instruction * visitFAdd(BinaryOperator &I)
Instruction * foldBinopWithPhiOperands(BinaryOperator &BO)
For a binary operator with 2 phi operands, try to hoist the binary operation before the phi.
Instruction * visitIntToPtr(IntToPtrInst &CI)
Instruction * foldICmpAddOpConst(Value *X, const APInt &C, CmpPredicate Pred)
Fold "icmp pred (X+C), X".
Instruction * foldICmpWithCastOp(ICmpInst &ICmp)
Handle icmp (cast x), (cast or constant).
Instruction * visitFPToUI(FPToUIInst &FI)
Instruction * foldICmpTruncConstant(ICmpInst &Cmp, TruncInst *Trunc, const APInt &C)
Fold icmp (trunc X), C.
bool mergeStoreIntoSuccessor(StoreInst &SI)
Try to transform: if () { *P = v1; } else { *P = v2 } or: *P = v1; if () { *P = v2; }...
Instruction * visitPtrToAddr(PtrToAddrInst &CI)
Instruction * visitInstruction(Instruction &I)
Specify what to return for unhandled instructions.
Definition InstCombineInternal.h:190
Instruction * foldSelectIntoOp(SelectInst &SI, Value *, Value *)
Try to fold the select into one of the operands to allow further optimization.
Instruction * foldShuffledIntrinsicOperands(IntrinsicInst *II)
If all arguments of the intrinsic are unary shuffles with the same mask, try to shuffle after the int...
Instruction * foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add, const APInt &C)
Fold icmp (add X, Y), C.
Instruction * foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul, const APInt &C)
Fold icmp (mul X, Y), C.
Instruction * visitInvokeInst(InvokeInst &II)
Value * simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted)
Try to fold a signed range checked with lower bound 0 to an unsigned icmp.
KnownFPClass computeKnownFPClass(Value *Val, FPClassTest Interested=fcAllFlags, const Instruction *CtxI=nullptr, unsigned Depth=0) const
Definition InstCombineInternal.h:214
Instruction * foldVariableSignZeroExtensionOfVariableHighBitExtract(BinaryOperator &OldAShr)
Instruction * commonShiftTransforms(BinaryOperator &I)
Instruction * visitFRem(BinaryOperator &I)
Instruction * foldPHIArgLoadIntoPHI(PHINode &PN)
Instruction * foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor, const APInt &C)
Fold icmp (xor X, Y), C.
Instruction * foldSelectICmp(CmpPredicate Pred, SelectInst *SI, Value *RHS, const ICmpInst &I)
bool foldIntegerTypedPHI(PHINode &PN)
If an integer typed PHI has only one use which is an IntToPtr operation, replace the PHI with an exis...
Instruction * foldICmpInstWithConstantAllowPoison(ICmpInst &Cmp, const APInt &C)
Try to fold integer comparisons with a constant operand: icmp Pred X, C where X is some kind of instr...
bool foldDeadPhiWeb(PHINode &PN)
If the phi is within a phi web, which is formed by the def-use chain of phis and all the phis in the ...
Instruction * foldIsMultipleOfAPowerOfTwo(ICmpInst &Cmp)
Fold icmp eq (num + mask) & ~mask, num to icmp eq (and num, mask), 0 Where mask is a low bit mask.
Instruction * visitXor(BinaryOperator &I)
Value * foldSelectWithConstOpToBinOp(ICmpInst *Cmp, Value *TrueVal, Value *FalseVal)
Value * EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned)
Given an expression that CanEvaluateTruncated or CanEvaluateSExtd returns true for,...
Instruction * simplifyBinOpSplats(ShuffleVectorInst &SVI)
void CreateNonTerminatorUnreachable(Instruction *InsertAt)
Create and insert the idiom we use to indicate a block is unreachable without having to rewrite the C...
Definition InstCombineInternal.h:489
Instruction * foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And, const APInt &C1, const APInt &C2)
Fold icmp (and (sh X, Y), C2), C1.
Value * pushFreezeToPreventPoisonFromPropagating(FreezeInst &FI)
Instruction * foldICmpBinOpWithConstantViaTruthTable(ICmpInst &Cmp, BinaryOperator *BO, const APInt &C)
Instruction * foldICmpInstWithConstant(ICmpInst &Cmp)
Try to fold integer comparisons with a constant operand: icmp Pred X, C where X is some kind of instr...
Instruction * visitSelectInst(SelectInst &SI)
Instruction * foldICmpXorShiftConst(ICmpInst &Cmp, BinaryOperator *Xor, const APInt &C)
For power-of-2 C: ((X s>> ShiftC) ^ X) u< C --> (X + C) u< (C << 1) ((X s>> ShiftC) ^ X) u> (C - 1) -...
Instruction * foldPHIArgIntToPtrToPHI(PHINode &PN)
Instruction * visitFPExt(CastInst &CI)
Instruction * foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl, const APInt &C)
Fold icmp (shl X, Y), C.
Instruction * visitFMul(BinaryOperator &I)
Instruction * foldSelectOfBools(SelectInst &SI)
Instruction * foldSelectExtConst(SelectInst &Sel)
Instruction * foldAddWithConstant(BinaryOperator &Add)
Instruction * foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And, const APInt &C)
Fold icmp (and X, Y), C.
bool run()
Run the combiner over the entire worklist until it is empty.
Instruction * foldFMulReassoc(BinaryOperator &I)
Instruction * SliceUpIllegalIntegerPHI(PHINode &PN)
This is an integer PHI and we know that it has an illegal type: see if it is only used by trunc or tr...
Instruction * foldAggregateConstructionIntoAggregateReuse(InsertValueInst &OrigIVI)
Look for chain of insertvalue's that fully define an aggregate, and trace back the values inserted,...
Instruction * foldICmpEquality(ICmpInst &Cmp)
bool removeInstructionsBeforeUnreachable(Instruction &I)
Instruction * foldPHIArgGEPIntoPHI(PHINode &PN)
Instruction * foldICmpWithMinMax(Instruction &I, MinMaxIntrinsic *MinMax, Value *Z, CmpPredicate Pred)
Fold icmp Pred min|max(X, Y), Z.
Instruction * visitShuffleVectorInst(ShuffleVectorInst &SVI)
Instruction * FoldShiftByConstant(Value *Op0, Constant *Op1, BinaryOperator &I)
void tryToSinkInstructionDbgVariableRecords(Instruction *I, BasicBlock::iterator InsertPos, BasicBlock *SrcBlock, BasicBlock *DestBlock, SmallVectorImpl< DbgVariableRecord * > &DPUsers)
bool foldAllocaCmp(AllocaInst *Alloca)
void addDeadEdge(BasicBlock *From, BasicBlock *To, SmallVectorImpl< BasicBlock * > &Worklist)
void PHIArgMergedDebugLoc(Instruction *Inst, PHINode &PN)
Helper function for FoldPHIArgXIntoPHI() to set debug location for the folded operation.
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 * visitICmpInst(ICmpInst &I)
Instruction * SimplifyAnyMemTransfer(AnyMemTransferInst *MI)
Instruction * visitGetElementPtrInst(GetElementPtrInst &GEP)
Instruction * visitBranchInst(BranchInst &BI)
Instruction * foldPowiReassoc(BinaryOperator &I)
Instruction * foldFreezeIntoRecurrence(FreezeInst &I, PHINode *PN)
Instruction * visitSDiv(BinaryOperator &I)
bool tryToSinkInstruction(Instruction *I, BasicBlock *DestBlock)
Try to move the specified instruction from its current block into the beginning of DestBlock,...
Instruction * foldPHIArgExtractValueInstructionIntoPHI(PHINode &PN)
If we have something like phi [extractvalue(a,0), extractvalue(b,0)], turn this into a phi[a,...
Instruction * foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1, const APInt &C2)
Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" -> (icmp eq/ne A, Log2(AP2/AP1)) -> (icmp eq/ne A,...
bool freezeOtherUses(FreezeInst &FI)
Instruction * visitFNeg(UnaryOperator &I)
void freelyInvertAllUsersOf(Value *V, Value *IgnoredUser=nullptr)
Freely adapt every user of V as-if V was changed to !V.
Instruction * commonIRemTransforms(BinaryOperator &I)
This function implements the transforms common to both integer remainder instructions (urem and srem)...
Instruction * visitAllocaInst(AllocaInst &AI)
Instruction * visitCallInst(CallInst &CI)
CallInst simplification.
Instruction * foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And, const APInt &C1)
Fold icmp (and X, C2), C1.
Instruction * visitFSub(BinaryOperator &I)
Instruction * foldICmpBitCast(ICmpInst &Cmp)
Instruction * foldGEPICmp(GEPOperator *GEPLHS, Value *RHS, CmpPredicate Cond, Instruction &I)
Fold comparisons between a GEP instruction and something else.
IRBuilder< TargetFolder, IRBuilderCallbackInserter > BuilderTy
An IRBuilder that automatically inserts new instructions into the worklist.
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
Instruction * InsertNewInstWith(Instruction *New, BasicBlock::iterator Old)
Same as InsertNewInstBefore, but also sets the debug loc.
BranchProbabilityInfo * BPI
virtual bool SimplifyDemandedBits(Instruction *I, unsigned OpNo, const APInt &DemandedMask, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)=0
ReversePostOrderTraversal< BasicBlock * > & RPOT
OptimizationRemarkEmitter & ORE
InstCombiner(InstructionWorklist &Worklist, BuilderTy &Builder, Function &F, AAResults *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, TargetTransformInfo &TTI, DominatorTree &DT, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI, const DataLayout &DL, ReversePostOrderTraversal< BasicBlock * > &RPOT)
const SimplifyQuery & getSimplifyQuery() const
Base class for instruction visitors.
InstructionWorklist - This is the worklist management logic for InstCombine and other simplification ...
This class represents a cast from an integer to a pointer.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
An instruction for reading from memory.
This class represents min/max intrinsics.
static Value * Negate(bool LHSIsZero, bool IsNSW, Value *Root, InstCombinerImpl &IC)
Attempt to negate Root.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Analysis providing profile information.
This class represents a cast from a pointer to an address (non-capturing ptrtoint).
This class represents a cast from a pointer to an integer.
Return a value (possibly void), from a function.
This class represents a sign extension of integer types.
This class represents the LLVM 'select' instruction.
This instruction constructs a fixed permutation of two input vectors.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
This class represents a truncation of integer types.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
This function has undefined behavior.
This represents the llvm.va_end intrinsic.
LLVM Value Representation.
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Base class of all SIMD vector types.
This class represents zero extension of integer types.
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
@ NeverOverflows
Never overflows.
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, StringRef PassName, const Function *F=nullptr)
Like setExplicitlyUnknownBranchWeights(...), but only sets unknown branch weights in the new instruct...
LLVM_ABI void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
SelectPatternFlavor
Specific patterns of select instructions we can match.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
@ Mul
Product of integers.
@ Xor
Bitwise or logical XOR of integers.
@ Sub
Subtraction of integers.
DWARFExpression::Operation Op
constexpr unsigned BitWidth
LLVM_ABI OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Value * Ptr
Common base pointer.
Definition InstCombineInternal.h:902
SmallVector< GEPOperator * > RHSGEPs
RHS GEPs until common base.
Definition InstCombineInternal.h:906
GEPNoWrapFlags LHSNW
LHS GEP NoWrapFlags until common base.
Definition InstCombineInternal.h:908
GEPNoWrapFlags RHSNW
RHS GEP NoWrapFlags until common base.
Definition InstCombineInternal.h:910
SmallVector< GEPOperator * > LHSGEPs
LHS GEPs until common base.
Definition InstCombineInternal.h:904
bool isExpensive() const
Whether expanding the GEP chains is expensive.
static CommonPointerBase compute(Value *LHS, Value *RHS)