LLVM 22.0.0git
InstCombineInternal.h
Go to the documentation of this file.
1//===- InstCombineInternal.h - InstCombine pass internals -------*- C++ -*-===//
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/// \file
10///
11/// This file provides internal interfaces used to implement the InstCombine.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
16#define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
17
19#include "llvm/ADT/Statistic.h"
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/InstVisitor.h"
27#include "llvm/IR/Value.h"
28#include "llvm/Support/Debug.h"
33#include <cassert>
34
35#define DEBUG_TYPE "instcombine"
37
38// As a default, let's assume that we want to be aggressive,
39// and attempt to traverse with no limits in attempt to sink negation.
40static constexpr unsigned NegatorDefaultMaxDepth = ~0U;
41
42// Let's guesstimate that most often we will end up visiting/producing
43// fairly small number of new instructions.
44static constexpr unsigned NegatorMaxNodesSSO = 16;
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
62 : public InstCombiner,
63 public InstVisitor<InstCombinerImpl, Instruction *> {
64public:
74
75 virtual ~InstCombinerImpl() = default;
76
77 /// Perform early cleanup and prepare the InstCombine worklist.
79
80 /// Run the combiner over the entire worklist until it is empty.
81 ///
82 /// \returns true if the IR is changed.
83 bool run();
84
85 // Visitation implementation - Implement instruction combining for different
86 // instruction types. The semantics are as follows:
87 // Return Value:
88 // null - No change was made
89 // I - Change was made, I is still valid, I may be dead though
90 // otherwise - Change was made, replace I with returned instruction
91 //
96 Value *LHS, Value *RHS, Type *Ty, bool isNUW);
113 Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
121 BinaryOperator *Sh0, const SimplifyQuery &SQ,
122 bool AnalyzeForSignBitExtraction = false);
126 BinaryOperator &OldAShr);
156
163 Instruction *visitFree(CallInst &FI, Value *FreedOp);
184 bool freezeOtherUses(FreezeInst &FI);
187
188 /// Specify what to return for unhandled instructions.
190
191 /// True when DB dominates all uses of DI except UI.
192 /// UI must be in the same block as DI.
193 /// The routine checks that the DI parent and DB are different.
194 bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
195 const BasicBlock *DB) const;
196
197 /// Try to replace select with select operand SIOpd in SI-ICmp sequence.
198 bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
199 const unsigned SIOpd);
200
201 LoadInst *combineLoadToNewType(LoadInst &LI, Type *NewTy,
202 const Twine &Suffix = "");
203
205 FPClassTest Interested = fcAllFlags,
206 const Instruction *CtxI = nullptr,
207 unsigned Depth = 0) const {
209 Val, FMF, Interested, getSimplifyQuery().getWithInstruction(CtxI),
210 Depth);
211 }
212
214 FPClassTest Interested = fcAllFlags,
215 const Instruction *CtxI = nullptr,
216 unsigned Depth = 0) const {
218 Val, Interested, getSimplifyQuery().getWithInstruction(CtxI), Depth);
219 }
220
221 /// Check if fmul \p MulVal, +0.0 will yield +0.0 (or signed zero is
222 /// ignorable).
224 const Instruction *CtxI) const;
225
226 std::optional<std::pair<Intrinsic::ID, SmallVector<Value *, 3>>>
228
229private:
230 bool annotateAnyAllocSite(CallBase &Call, const TargetLibraryInfo *TLI);
231 bool isDesirableIntType(unsigned BitWidth) const;
232 bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
233 bool shouldChangeType(Type *From, Type *To) const;
234 Value *dyn_castNegVal(Value *V) const;
235
236 /// Classify whether a cast is worth optimizing.
237 ///
238 /// This is a helper to decide whether the simplification of
239 /// logic(cast(A), cast(B)) to cast(logic(A, B)) should be performed.
240 ///
241 /// \param CI The cast we are interested in.
242 ///
243 /// \return true if this cast actually results in any code being generated and
244 /// if it cannot already be eliminated by some other transformation.
245 bool shouldOptimizeCast(CastInst *CI);
246
247 /// Try to optimize a sequence of instructions checking if an operation
248 /// on LHS and RHS overflows.
249 ///
250 /// If this overflow check is done via one of the overflow check intrinsics,
251 /// then CtxI has to be the call instruction calling that intrinsic. If this
252 /// overflow check is done by arithmetic followed by a compare, then CtxI has
253 /// to be the arithmetic instruction.
254 ///
255 /// If a simplification is possible, stores the simplified result of the
256 /// operation in OperationResult and result of the overflow check in
257 /// OverflowResult, and return true. If no simplification is possible,
258 /// returns false.
259 bool OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp, bool IsSigned,
260 Value *LHS, Value *RHS,
261 Instruction &CtxI, Value *&OperationResult,
263
264 Instruction *visitCallBase(CallBase &Call);
265 Instruction *tryOptimizeCall(CallInst *CI);
266 bool transformConstExprCastCall(CallBase &Call);
267 Instruction *transformCallThroughTrampoline(CallBase &Call,
268 IntrinsicInst &Tramp);
269
270 /// Try to optimize a call to the result of a ptrauth intrinsic, potentially
271 /// into the ptrauth call bundle:
272 /// - call(ptrauth.resign(p)), ["ptrauth"()] -> call p, ["ptrauth"()]
273 /// - call(ptrauth.sign(p)), ["ptrauth"()] -> call p
274 /// as long as the key/discriminator are the same in sign and auth-bundle,
275 /// and we don't change the key in the bundle (to a potentially-invalid key.)
276 Instruction *foldPtrAuthIntrinsicCallee(CallBase &Call);
277
278 /// Try to optimize a call to a ptrauth constant, into its ptrauth bundle:
279 /// call(ptrauth(f)), ["ptrauth"()] -> call f
280 /// as long as the key/discriminator are the same in constant and bundle.
281 Instruction *foldPtrAuthConstantCallee(CallBase &Call);
282
283 // Return (a, b) if (LHS, RHS) is known to be (a, b) or (b, a).
284 // Otherwise, return std::nullopt
285 // Currently it matches:
286 // - LHS = (select c, a, b), RHS = (select c, b, a)
287 // - LHS = (phi [a, BB0], [b, BB1]), RHS = (phi [b, BB0], [a, BB1])
288 // - LHS = min(a, b), RHS = max(a, b)
289 std::optional<std::pair<Value *, Value *>> matchSymmetricPair(Value *LHS,
290 Value *RHS);
291
292 Value *simplifyMaskedLoad(IntrinsicInst &II);
293 Instruction *simplifyMaskedStore(IntrinsicInst &II);
294 Instruction *simplifyMaskedGather(IntrinsicInst &II);
295 Instruction *simplifyMaskedScatter(IntrinsicInst &II);
296
297 /// Transform (zext icmp) to bitwise / integer operations in order to
298 /// eliminate it.
299 ///
300 /// \param ICI The icmp of the (zext icmp) pair we are interested in.
301 /// \parem CI The zext of the (zext icmp) pair we are interested in.
302 ///
303 /// \return null if the transformation cannot be performed. If the
304 /// transformation can be performed the new instruction that replaces the
305 /// (zext icmp) pair will be returned.
306 Instruction *transformZExtICmp(ICmpInst *Cmp, ZExtInst &Zext);
307
308 Instruction *transformSExtICmp(ICmpInst *Cmp, SExtInst &Sext);
309
310 bool willNotOverflowSignedAdd(const WithCache<const Value *> &LHS,
312 const Instruction &CxtI) const {
313 return computeOverflowForSignedAdd(LHS, RHS, &CxtI) ==
315 }
316
317 bool willNotOverflowUnsignedAdd(const WithCache<const Value *> &LHS,
319 const Instruction &CxtI) const {
320 return computeOverflowForUnsignedAdd(LHS, RHS, &CxtI) ==
322 }
323
324 bool willNotOverflowAdd(const Value *LHS, const Value *RHS,
325 const Instruction &CxtI, bool IsSigned) const {
326 return IsSigned ? willNotOverflowSignedAdd(LHS, RHS, CxtI)
327 : willNotOverflowUnsignedAdd(LHS, RHS, CxtI);
328 }
329
330 bool willNotOverflowSignedSub(const Value *LHS, const Value *RHS,
331 const Instruction &CxtI) const {
332 return computeOverflowForSignedSub(LHS, RHS, &CxtI) ==
333 OverflowResult::NeverOverflows;
334 }
335
336 bool willNotOverflowUnsignedSub(const Value *LHS, const Value *RHS,
337 const Instruction &CxtI) const {
338 return computeOverflowForUnsignedSub(LHS, RHS, &CxtI) ==
339 OverflowResult::NeverOverflows;
340 }
341
342 bool willNotOverflowSub(const Value *LHS, const Value *RHS,
343 const Instruction &CxtI, bool IsSigned) const {
344 return IsSigned ? willNotOverflowSignedSub(LHS, RHS, CxtI)
345 : willNotOverflowUnsignedSub(LHS, RHS, CxtI);
346 }
347
348 bool willNotOverflowSignedMul(const Value *LHS, const Value *RHS,
349 const Instruction &CxtI) const {
350 return computeOverflowForSignedMul(LHS, RHS, &CxtI) ==
351 OverflowResult::NeverOverflows;
352 }
353
354 bool willNotOverflowUnsignedMul(const Value *LHS, const Value *RHS,
355 const Instruction &CxtI,
356 bool IsNSW = false) const {
357 return computeOverflowForUnsignedMul(LHS, RHS, &CxtI, IsNSW) ==
358 OverflowResult::NeverOverflows;
359 }
360
361 bool willNotOverflowMul(const Value *LHS, const Value *RHS,
362 const Instruction &CxtI, bool IsSigned) const {
363 return IsSigned ? willNotOverflowSignedMul(LHS, RHS, CxtI)
364 : willNotOverflowUnsignedMul(LHS, RHS, CxtI);
365 }
366
367 bool willNotOverflow(BinaryOperator::BinaryOps Opcode, const Value *LHS,
368 const Value *RHS, const Instruction &CxtI,
369 bool IsSigned) const {
370 switch (Opcode) {
371 case Instruction::Add: return willNotOverflowAdd(LHS, RHS, CxtI, IsSigned);
372 case Instruction::Sub: return willNotOverflowSub(LHS, RHS, CxtI, IsSigned);
373 case Instruction::Mul: return willNotOverflowMul(LHS, RHS, CxtI, IsSigned);
374 default: llvm_unreachable("Unexpected opcode for overflow query");
375 }
376 }
377
378 Value *EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP = false);
379 /// Emit sum of multiple GEP offsets. The GEPs are processed in reverse
380 /// order.
381 Value *EmitGEPOffsets(ArrayRef<GEPOperator *> GEPs, GEPNoWrapFlags NW,
382 Type *IdxTy, bool RewriteGEPs);
383 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
384 Instruction *foldBitcastExtElt(ExtractElementInst &ExtElt);
385 Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
386 Instruction *foldFBinOpOfIntCasts(BinaryOperator &I);
387 // Should only be called by `foldFBinOpOfIntCasts`.
388 Instruction *foldFBinOpOfIntCastsFromSign(
389 BinaryOperator &BO, bool OpsFromSigned, std::array<Value *, 2> IntOps,
390 Constant *Op1FpC, SmallVectorImpl<WithCache<const Value *>> &OpsKnown);
391 Instruction *foldBinopOfSextBoolToSelect(BinaryOperator &I);
392 Instruction *narrowBinOp(TruncInst &Trunc);
393 Instruction *narrowMaskedBinOp(BinaryOperator &And);
394 Instruction *narrowMathIfNoOverflow(BinaryOperator &I);
395 Instruction *narrowFunnelShift(TruncInst &Trunc);
396 Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
397 Instruction *matchSAddSubSat(IntrinsicInst &MinMax1);
398 Instruction *foldNot(BinaryOperator &I);
399 Instruction *foldBinOpOfDisplacedShifts(BinaryOperator &I);
400
401 /// Determine if a pair of casts can be replaced by a single cast.
402 ///
403 /// \param CI1 The first of a pair of casts.
404 /// \param CI2 The second of a pair of casts.
405 ///
406 /// \return 0 if the cast pair cannot be eliminated, otherwise returns an
407 /// Instruction::CastOps value for a cast that can replace the pair, casting
408 /// CI1->getSrcTy() to CI2->getDstTy().
409 ///
410 /// \see CastInst::isEliminableCastPair
411 Instruction::CastOps isEliminableCastPair(const CastInst *CI1,
412 const CastInst *CI2);
413 Value *simplifyIntToPtrRoundTripCast(Value *Val);
414
415 Value *foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &I,
416 bool IsAnd, bool IsLogical = false);
417 Value *foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS, BinaryOperator &Xor);
418
419 Value *foldEqOfParts(Value *Cmp0, Value *Cmp1, bool IsAnd);
420
421 Value *foldAndOrOfICmpsUsingRanges(ICmpInst *ICmp1, ICmpInst *ICmp2,
422 bool IsAnd);
423
424 /// Optimize (fcmp)&(fcmp) or (fcmp)|(fcmp).
425 /// NOTE: Unlike most of instcombine, this returns a Value which should
426 /// already be inserted into the function.
427 Value *foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS, bool IsAnd,
428 bool IsLogicalSelect = false);
429
430 Instruction *foldLogicOfIsFPClass(BinaryOperator &Operator, Value *LHS,
431 Value *RHS);
432
433 Value *foldBooleanAndOr(Value *LHS, Value *RHS, Instruction &I, bool IsAnd,
434 bool IsLogical);
435
436 Value *reassociateBooleanAndOr(Value *LHS, Value *X, Value *Y, Instruction &I,
437 bool IsAnd, bool RHSIsLogical);
438
439 Value *foldDisjointOr(Value *LHS, Value *RHS);
440
441 Value *reassociateDisjointOr(Value *LHS, Value *RHS);
442
444 canonicalizeConditionalNegationViaMathToSelect(BinaryOperator &i);
445
446 Value *matchSelectFromAndOr(Value *A, Value *B, Value *C, Value *D,
447 bool InvertFalseVal = false);
448 Value *getSelectCondition(Value *A, Value *B, bool ABIsTheSame);
449
450 Instruction *foldLShrOverflowBit(BinaryOperator &I);
451 Instruction *foldExtractOfOverflowIntrinsic(ExtractValueInst &EV);
452 Instruction *foldIntrinsicWithOverflowCommon(IntrinsicInst *II);
453 Instruction *foldIntrinsicIsFPClass(IntrinsicInst &II);
454 Instruction *foldFPSignBitOps(BinaryOperator &I);
455 Instruction *foldFDivConstantDivisor(BinaryOperator &I);
456
457 // Optimize one of these forms:
458 // and i1 Op, SI / select i1 Op, i1 SI, i1 false (if IsAnd = true)
459 // or i1 Op, SI / select i1 Op, i1 true, i1 SI (if IsAnd = false)
460 // into simplier select instruction using isImpliedCondition.
461 Instruction *foldAndOrOfSelectUsingImpliedCond(Value *Op, SelectInst &SI,
462 bool IsAnd);
463
464 Instruction *hoistFNegAboveFMulFDiv(Value *FNegOp, Instruction &FMFSource);
465
466 /// Simplify \p V given that it is known to be non-null.
467 /// Returns the simplified value if possible, otherwise returns nullptr.
468 /// If \p HasDereferenceable is true, the simplification will not perform
469 /// same object checks.
470 Value *simplifyNonNullOperand(Value *V, bool HasDereferenceable,
471 unsigned Depth = 0);
472
473 SelectInst *createSelectInst(Value *C, Value *S1, Value *S2,
474 const Twine &NameStr = "",
475 InsertPosition InsertBefore = nullptr,
476 Instruction *MDFrom = nullptr) {
477 SelectInst *SI =
478 SelectInst::Create(C, S1, S2, NameStr, InsertBefore, MDFrom);
479 if (!MDFrom)
481 return SI;
482 }
483
484public:
485 /// Create and insert the idiom we use to indicate a block is unreachable
486 /// without having to rewrite the CFG from within InstCombine.
488 auto &Ctx = InsertAt->getContext();
489 auto *SI = new StoreInst(ConstantInt::getTrue(Ctx),
491 /*isVolatile*/ false, Align(1));
492 InsertNewInstWith(SI, InsertAt->getIterator());
493 }
494
495 /// Combiner aware instruction erasure.
496 ///
497 /// When dealing with an instruction that has side effects or produces a void
498 /// value, we can't rely on DCE to delete the instruction. Instead, visit
499 /// methods should return the value returned by this function.
501 LLVM_DEBUG(dbgs() << "IC: ERASE " << I << '\n');
502 assert(I.use_empty() && "Cannot erase instruction that is used!");
504
505 // Make sure that we reprocess all operands now that we reduced their
506 // use counts.
507 SmallVector<Value *> Ops(I.operands());
508 Worklist.remove(&I);
509 DC.removeValue(&I);
510 I.eraseFromParent();
511 for (Value *Op : Ops)
512 Worklist.handleUseCountDecrement(Op);
513 MadeIRChange = true;
514 return nullptr; // Don't do anything with FI
515 }
516
517 OverflowResult computeOverflow(
518 Instruction::BinaryOps BinaryOp, bool IsSigned,
519 Value *LHS, Value *RHS, Instruction *CxtI) const;
520
521 /// Performs a few simplifications for operators which are associative
522 /// or commutative.
523 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
524
525 /// Tries to simplify binary operations which some other binary
526 /// operation distributes over.
527 ///
528 /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
529 /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
530 /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
531 /// value, or null if it didn't simplify.
532 Value *foldUsingDistributiveLaws(BinaryOperator &I);
533
534 /// Tries to simplify add operations using the definition of remainder.
535 ///
536 /// The definition of remainder is X % C = X - (X / C ) * C. The add
537 /// expression X % C0 + (( X / C0 ) % C1) * C0 can be simplified to
538 /// X % (C0 * C1)
539 Value *SimplifyAddWithRemainder(BinaryOperator &I);
540
541 // Binary Op helper for select operations where the expression can be
542 // efficiently reorganized.
543 Value *SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *LHS,
544 Value *RHS);
545
546 // If `I` has operand `(ctpop (not x))`, fold `I` with `(sub nuw nsw
547 // BitWidth(x), (ctpop x))`.
548 Instruction *tryFoldInstWithCtpopWithNot(Instruction *I);
549
550 // (Binop1 (Binop2 (logic_shift X, C), C1), (logic_shift Y, C))
551 // -> (logic_shift (Binop1 (Binop2 X, inv_logic_shift(C1, C)), Y), C)
552 // (Binop1 (Binop2 (logic_shift X, Amt), Mask), (logic_shift Y, Amt))
553 // -> (BinOp (logic_shift (BinOp X, Y)), Mask)
554 Instruction *foldBinOpShiftWithShift(BinaryOperator &I);
555
556 /// Tries to simplify binops of select and cast of the select condition.
557 ///
558 /// (Binop (cast C), (select C, T, F))
559 /// -> (select C, C0, C1)
560 Instruction *foldBinOpOfSelectAndCastOfSelectCondition(BinaryOperator &I);
561
562 /// This tries to simplify binary operations by factorizing out common terms
563 /// (e. g. "(A*B)+(A*C)" -> "A*(B+C)").
564 Value *tryFactorizationFolds(BinaryOperator &I);
565
566 /// Match a select chain which produces one of three values based on whether
567 /// the LHS is less than, equal to, or greater than RHS respectively.
568 /// Return true if we matched a three way compare idiom. The LHS, RHS, Less,
569 /// Equal and Greater values are saved in the matching process and returned to
570 /// the caller.
571 bool matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, Value *&RHS,
572 ConstantInt *&Less, ConstantInt *&Equal,
573 ConstantInt *&Greater);
574
575 /// Attempts to replace I with a simpler value based on the demanded
576 /// bits.
577 Value *SimplifyDemandedUseBits(Instruction *I, const APInt &DemandedMask,
578 KnownBits &Known, const SimplifyQuery &Q,
579 unsigned Depth = 0);
581 bool SimplifyDemandedBits(Instruction *I, unsigned Op,
582 const APInt &DemandedMask, KnownBits &Known,
583 const SimplifyQuery &Q,
584 unsigned Depth = 0) override;
585
586 /// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
587 /// bits. It also tries to handle simplifications that can be done based on
588 /// DemandedMask, but without modifying the Instruction.
589 Value *SimplifyMultipleUseDemandedBits(Instruction *I,
590 const APInt &DemandedMask,
591 KnownBits &Known,
592 const SimplifyQuery &Q,
593 unsigned Depth = 0);
594
595 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
596 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
597 Value *simplifyShrShlDemandedBits(
598 Instruction *Shr, const APInt &ShrOp1, Instruction *Shl,
599 const APInt &ShlOp1, const APInt &DemandedMask, KnownBits &Known);
600
601 /// Tries to simplify operands to an integer instruction based on its
602 /// demanded bits.
603 bool SimplifyDemandedInstructionBits(Instruction &Inst);
604 bool SimplifyDemandedInstructionBits(Instruction &Inst, KnownBits &Known);
605
606 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
607 APInt &PoisonElts, unsigned Depth = 0,
608 bool AllowMultipleUsers = false) override;
609
610 /// Attempts to replace V with a simpler value based on the demanded
611 /// floating-point classes
612 Value *SimplifyDemandedUseFPClass(Value *V, FPClassTest DemandedMask,
613 KnownFPClass &Known, Instruction *CxtI,
614 unsigned Depth = 0);
615 bool SimplifyDemandedFPClass(Instruction *I, unsigned Op,
616 FPClassTest DemandedMask, KnownFPClass &Known,
617 unsigned Depth = 0);
618
619 /// Common transforms for add / disjoint or
620 Instruction *foldAddLikeCommutative(Value *LHS, Value *RHS, bool NSW,
621 bool NUW);
622
623 /// Canonicalize the position of binops relative to shufflevector.
624 Instruction *foldVectorBinop(BinaryOperator &Inst);
628 VectorType *NewCTy);
629
630 /// Given a binary operator, cast instruction, or select which has a PHI node
631 /// as operand #0, see if we can fold the instruction into the PHI (which is
632 /// only possible if all operands to the PHI are constants).
634 bool AllowMultipleUses = false);
635
636 /// Try to fold binary operators whose operands are simple interleaved
637 /// recurrences to a single recurrence. This is a common pattern in reduction
638 /// operations.
639 /// Example:
640 /// %phi1 = phi [init1, %BB1], [%op1, %BB2]
641 /// %phi2 = phi [init2, %BB1], [%op2, %BB2]
642 /// %op1 = binop %phi1, constant1
643 /// %op2 = binop %phi2, constant2
644 /// %rdx = binop %op1, %op2
645 /// -->
646 /// %phi_combined = phi [init_combined, %BB1], [%op_combined, %BB2]
647 /// %rdx_combined = binop %phi_combined, constant_combined
649
650 /// For a binary operator with 2 phi operands, try to hoist the binary
651 /// operation before the phi. This can result in fewer instructions in
652 /// patterns where at least one set of phi operands simplifies.
653 /// Example:
654 /// BB3: binop (phi [X, BB1], [C1, BB2]), (phi [Y, BB1], [C2, BB2])
655 /// -->
656 /// BB1: BO = binop X, Y
657 /// BB3: phi [BO, BB1], [(binop C1, C2), BB2]
659
660 /// Given an instruction with a select as one operand and a constant as the
661 /// other operand, try to fold the binary operator into the select arguments.
662 /// This also works for Cast instructions, which obviously do not have a
663 /// second operand.
665 bool FoldWithMultiUse = false);
666
667 /// This is a convenience wrapper function for the above two functions.
669
671
674
675 /// Try to rotate an operation below a PHI node, using PHI nodes for
676 /// its operands.
685
686 /// If the phi is within a phi web, which is formed by the def-use chain
687 /// of phis and all the phis in the web are only used in the other phis.
688 /// In this case, these phis are dead and we will remove all of them.
689 bool foldDeadPhiWeb(PHINode &PN);
690
691 /// If an integer typed PHI has only one use which is an IntToPtr operation,
692 /// replace the PHI with an existing pointer typed PHI if it exists. Otherwise
693 /// insert a new pointer typed PHI and replace the original one.
695
696 /// Helper function for FoldPHIArgXIntoPHI() to set debug location for the
697 /// folded operation.
699
702 Instruction &I);
704 const ICmpInst &I);
705 bool foldAllocaCmp(AllocaInst *Alloca);
708 CmpInst &ICI,
709 ConstantInt *AndCst = nullptr);
711 Constant *RHSC);
715
724 const APInt &C);
727 Value *Z, CmpPredicate Pred);
732
734
736 const APInt &C);
738 ConstantInt *C);
740 const APInt &C);
742 const SimplifyQuery &Q);
744 const APInt &C);
746 const APInt &C);
748 const APInt &C);
750 const APInt &C);
752 const APInt &C);
754 const APInt &C);
756 const APInt &C);
758 const APInt &C);
760 const APInt &C);
762 const APInt &C);
764 const APInt &C);
766 const APInt &C1);
768 const APInt &C1, const APInt &C2);
770 const APInt &C);
772 const APInt &C2);
774 const APInt &C2);
775
777 BinaryOperator *BO,
778 const APInt &C);
780 BinaryOperator *BO,
781 const APInt &C);
783 const APInt &C);
785 const APInt &C);
789 ICmpInst &CxtI);
790
791 // Helpers of visitSelectInst().
799 Value *A, Value *B, Instruction &Outer,
803 Value *FalseVal);
806 unsigned Depth = 0);
807
808 Value *insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi,
809 bool isSigned, bool Inside);
811
812 /// Given an initial instruction, check to see if it is the root of a
813 /// bswap/bitreverse idiom. If so, return the equivalent bswap/bitreverse
814 /// intrinsic.
816 bool MatchBitReversals);
817
820
822
823 bool tryToSinkInstruction(Instruction *I, BasicBlock *DestBlock);
825 Instruction *I, BasicBlock::iterator InsertPos, BasicBlock *SrcBlock,
827
829 void addDeadEdge(BasicBlock *From, BasicBlock *To,
835 void freelyInvertAllUsersOf(Value *V, Value *IgnoredUser = nullptr);
836
837 /// Take the exact integer log2 of the value. If DoFold is true, create the
838 /// actual instructions, otherwise return a non-null dummy value. Return
839 /// nullptr on failure. Note, if DoFold is true the caller must ensure that
840 /// takeLog2 will succeed, otherwise it may create stray instructions.
841 Value *takeLog2(Value *Op, unsigned Depth, bool AssumeNonZero, bool DoFold);
842
843 Value *tryGetLog2(Value *Op, bool AssumeNonZero) {
844 if (takeLog2(Op, /*Depth=*/0, AssumeNonZero, /*DoFold=*/false))
845 return takeLog2(Op, /*Depth=*/0, AssumeNonZero, /*DoFold=*/true);
846 return nullptr;
847 }
848};
849
850class Negator final {
851 /// Top-to-bottom, def-to-use negated instruction tree we produced.
853
855 BuilderTy Builder;
856
857 const DominatorTree &DT;
858
859 const bool IsTrulyNegation;
860
861 SmallDenseMap<Value *, Value *> NegationsCache;
862
863 Negator(LLVMContext &C, const DataLayout &DL, const DominatorTree &DT,
864 bool IsTrulyNegation);
865
866#if LLVM_ENABLE_STATS
867 unsigned NumValuesVisitedInThisNegator = 0;
868 ~Negator();
869#endif
870
871 using Result = std::pair<ArrayRef<Instruction *> /*NewInstructions*/,
872 Value * /*NegatedRoot*/>;
873
874 std::array<Value *, 2> getSortedOperandsOfBinOp(Instruction *I);
875
876 [[nodiscard]] Value *visitImpl(Value *V, bool IsNSW, unsigned Depth);
877
878 [[nodiscard]] Value *negate(Value *V, bool IsNSW, unsigned Depth);
879
880 /// Recurse depth-first and attempt to sink the negation.
881 /// FIXME: use worklist?
882 [[nodiscard]] std::optional<Result> run(Value *Root, bool IsNSW);
883
884 Negator(const Negator &) = delete;
885 Negator(Negator &&) = delete;
886 Negator &operator=(const Negator &) = delete;
887 Negator &operator=(Negator &&) = delete;
888
889public:
890 /// Attempt to negate \p Root. Retuns nullptr if negation can't be performed,
891 /// otherwise returns negated value.
892 [[nodiscard]] static Value *Negate(bool LHSIsZero, bool IsNSW, Value *Root,
893 InstCombinerImpl &IC);
894};
895
897 /// Common base pointer.
898 Value *Ptr = nullptr;
899 /// LHS GEPs until common base.
901 /// RHS GEPs until common base.
903 /// LHS GEP NoWrapFlags until common base.
905 /// RHS GEP NoWrapFlags until common base.
907
909
910 /// Whether expanding the GEP chains is expensive.
911 bool isExpensive() const;
912};
913
914} // end namespace llvm
915
916#undef DEBUG_TYPE
917
918#endif // LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
constexpr LLT S1
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
Definition Compiler.h:137
static bool willNotOverflow(BinaryOpIntrinsic *BO, LazyValueInfo *LVI)
static bool isSigned(unsigned int Opcode)
#define DEBUG_TYPE
Hexagon Common GEP
IRTranslator LLVM IR MI
static constexpr unsigned NegatorMaxNodesSSO
static constexpr unsigned NegatorDefaultMaxDepth
This file provides the interface for the instcombine pass implementation.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
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...
#define LLVM_DEBUG(...)
Definition Debug.h:114
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)
Value * RHS
Value * LHS
static const uint32_t IV[8]
Definition blake3_impl.h:83
Class for arbitrary precision integers.
Definition APInt.h:78
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),...
Definition ArrayRef.h:41
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.
Definition BasicBlock.h:62
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
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.
Definition InstrTypes.h:448
This class is the base class for the comparison instructions.
Definition InstrTypes.h:666
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.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
This instruction extracts a single (scalar) element from a VectorType value.
This instruction extracts a struct member or array element value from an aggregate value.
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.
Definition FMF.h:22
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...
Definition IRBuilder.h:2780
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 * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false)
Given an instruction with a select as one operand and a constant as the other operand,...
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)
virtual ~InstCombinerImpl()=default
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
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,...
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)
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 * 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 * 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)
Value * foldPtrToIntOfGEP(Type *IntTy, Value *Ptr)
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.
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)
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)
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 * visitInstruction(Instruction &I)
Specify what to return for unhandled instructions.
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
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...
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.
SimplifyQuery SQ
IRBuilder< TargetFolder, IRBuilderCallbackInserter > BuilderTy
An IRBuilder that automatically inserts new instructions into the worklist.
BlockFrequencyInfo * BFI
TargetLibraryInfo & TLI
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
const DataLayout & DL
DomConditionCache DC
AssumptionCache & AC
DominatorTree & DT
ProfileSummaryInfo * PSI
BuilderTy & Builder
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.
Definition InstVisitor.h:78
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.
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
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.
The optimization diagnostic interface.
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 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.
Multiway switch.
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...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
This function has undefined behavior.
This represents the llvm.va_end intrinsic.
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1101
Base class of all SIMD vector types.
This class represents zero extension of integer types.
self_iterator getIterator()
Definition ilist_node.h:123
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
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 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...
Definition Utils.cpp:1725
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, Function &F, StringRef PassName)
Like setExplicitlyUnknownBranchWeights(...), but only sets unknown branch weights in the new instruct...
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.
Definition Debug.cpp:207
LLVM_ABI OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
TargetTransformInfo TTI
@ Mul
Product of integers.
@ Xor
Bitwise or logical XOR of integers.
@ Sub
Subtraction of integers.
@ Add
Sum 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.
Definition Alignment.h:39
Value * Ptr
Common base pointer.
SmallVector< GEPOperator * > RHSGEPs
RHS GEPs until common base.
GEPNoWrapFlags LHSNW
LHS GEP NoWrapFlags until common base.
GEPNoWrapFlags RHSNW
RHS GEP NoWrapFlags until common base.
SmallVector< GEPOperator * > LHSGEPs
LHS GEPs until common base.
bool isExpensive() const
Whether expanding the GEP chains is expensive.
static CommonPointerBase compute(Value *LHS, Value *RHS)
Matching combinators.