LLVM 17.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
18#include "llvm/ADT/Statistic.h"
22#include "llvm/IR/IRBuilder.h"
23#include "llvm/IR/InstVisitor.h"
25#include "llvm/IR/Value.h"
26#include "llvm/Support/Debug.h"
30#include <cassert>
31
32#define DEBUG_TYPE "instcombine"
34
35using namespace llvm::PatternMatch;
36
37// As a default, let's assume that we want to be aggressive,
38// and attempt to traverse with no limits in attempt to sink negation.
39static constexpr unsigned NegatorDefaultMaxDepth = ~0U;
40
41// Let's guesstimate that most often we will end up visiting/producing
42// fairly small number of new instructions.
43static constexpr unsigned NegatorMaxNodesSSO = 16;
44
45namespace llvm {
46
47class AAResults;
48class APInt;
49class AssumptionCache;
51class DataLayout;
52class DominatorTree;
53class GEPOperator;
54class GlobalVariable;
55class LoopInfo;
59class User;
60
62 : public InstCombiner,
63 public InstVisitor<InstCombinerImpl, Instruction *> {
64public:
66 bool MinimizeSize, AAResults *AA, AssumptionCache &AC,
70 const DataLayout &DL, LoopInfo *LI)
71 : InstCombiner(Worklist, Builder, MinimizeSize, AA, AC, TLI, TTI, DT, ORE,
72 BFI, PSI, DL, LI) {}
73
74 virtual ~InstCombinerImpl() = default;
75
76 /// Run the combiner over the entire worklist until it is empty.
77 ///
78 /// \returns true if the IR is changed.
79 bool run();
80
81 // Visitation implementation - Implement instruction combining for different
82 // instruction types. The semantics are as follows:
83 // Return Value:
84 // null - No change was made
85 // I - Change was made, I is still valid, I may be dead though
86 // otherwise - Change was made, replace I with returned instruction
87 //
88 Instruction *visitFNeg(UnaryOperator &I);
89 Instruction *visitAdd(BinaryOperator &I);
90 Instruction *visitFAdd(BinaryOperator &I);
91 Value *OptimizePointerDifference(
92 Value *LHS, Value *RHS, Type *Ty, bool isNUW);
93 Instruction *visitSub(BinaryOperator &I);
94 Instruction *visitFSub(BinaryOperator &I);
95 Instruction *visitMul(BinaryOperator &I);
96 Instruction *visitFMul(BinaryOperator &I);
97 Instruction *visitURem(BinaryOperator &I);
98 Instruction *visitSRem(BinaryOperator &I);
99 Instruction *visitFRem(BinaryOperator &I);
100 bool simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I);
101 Instruction *commonIRemTransforms(BinaryOperator &I);
102 Instruction *commonIDivTransforms(BinaryOperator &I);
103 Instruction *visitUDiv(BinaryOperator &I);
104 Instruction *visitSDiv(BinaryOperator &I);
105 Instruction *visitFDiv(BinaryOperator &I);
106 Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
107 Instruction *visitAnd(BinaryOperator &I);
108 Instruction *visitOr(BinaryOperator &I);
109 bool sinkNotIntoLogicalOp(Instruction &I);
110 bool sinkNotIntoOtherHandOfLogicalOp(Instruction &I);
111 Instruction *visitXor(BinaryOperator &I);
112 Instruction *visitShl(BinaryOperator &I);
113 Value *reassociateShiftAmtsOfTwoSameDirectionShifts(
114 BinaryOperator *Sh0, const SimplifyQuery &SQ,
115 bool AnalyzeForSignBitExtraction = false);
116 Instruction *canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(
118 Instruction *foldVariableSignZeroExtensionOfVariableHighBitExtract(
119 BinaryOperator &OldAShr);
120 Instruction *visitAShr(BinaryOperator &I);
121 Instruction *visitLShr(BinaryOperator &I);
122 Instruction *commonShiftTransforms(BinaryOperator &I);
123 Instruction *visitFCmpInst(FCmpInst &I);
124 CmpInst *canonicalizeICmpPredicate(CmpInst &I);
125 Instruction *visitICmpInst(ICmpInst &I);
126 Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
128 Instruction *commonCastTransforms(CastInst &CI);
129 Instruction *commonPointerCastTransforms(CastInst &CI);
130 Instruction *visitTrunc(TruncInst &CI);
131 Instruction *visitZExt(ZExtInst &Zext);
132 Instruction *visitSExt(SExtInst &Sext);
133 Instruction *visitFPTrunc(FPTruncInst &CI);
134 Instruction *visitFPExt(CastInst &CI);
135 Instruction *visitFPToUI(FPToUIInst &FI);
136 Instruction *visitFPToSI(FPToSIInst &FI);
137 Instruction *visitUIToFP(CastInst &CI);
138 Instruction *visitSIToFP(CastInst &CI);
139 Instruction *visitPtrToInt(PtrToIntInst &CI);
140 Instruction *visitIntToPtr(IntToPtrInst &CI);
141 Instruction *visitBitCast(BitCastInst &CI);
142 Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
143 Instruction *foldItoFPtoI(CastInst &FI);
145 Instruction *visitCallInst(CallInst &CI);
146 Instruction *visitInvokeInst(InvokeInst &II);
147 Instruction *visitCallBrInst(CallBrInst &CBI);
148
149 Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
150 Instruction *visitPHINode(PHINode &PN);
151 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
152 Instruction *visitGEPOfGEP(GetElementPtrInst &GEP, GEPOperator *Src);
153 Instruction *visitAllocaInst(AllocaInst &AI);
154 Instruction *visitAllocSite(Instruction &FI);
155 Instruction *visitFree(CallInst &FI, Value *FreedOp);
156 Instruction *visitLoadInst(LoadInst &LI);
157 Instruction *visitStoreInst(StoreInst &SI);
158 Instruction *visitAtomicRMWInst(AtomicRMWInst &SI);
159 Instruction *visitUnconditionalBranchInst(BranchInst &BI);
160 Instruction *visitBranchInst(BranchInst &BI);
161 Instruction *visitFenceInst(FenceInst &FI);
162 Instruction *visitSwitchInst(SwitchInst &SI);
163 Instruction *visitReturnInst(ReturnInst &RI);
164 Instruction *visitUnreachableInst(UnreachableInst &I);
166 foldAggregateConstructionIntoAggregateReuse(InsertValueInst &OrigIVI);
167 Instruction *visitInsertValueInst(InsertValueInst &IV);
168 Instruction *visitInsertElementInst(InsertElementInst &IE);
169 Instruction *visitExtractElementInst(ExtractElementInst &EI);
170 Instruction *simplifyBinOpSplats(ShuffleVectorInst &SVI);
171 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
172 Instruction *visitExtractValueInst(ExtractValueInst &EV);
173 Instruction *visitLandingPadInst(LandingPadInst &LI);
174 Instruction *visitVAEndInst(VAEndInst &I);
175 Value *pushFreezeToPreventPoisonFromPropagating(FreezeInst &FI);
176 bool freezeOtherUses(FreezeInst &FI);
177 Instruction *foldFreezeIntoRecurrence(FreezeInst &I, PHINode *PN);
178 Instruction *visitFreeze(FreezeInst &I);
179
180 /// Specify what to return for unhandled instructions.
182
183 /// True when DB dominates all uses of DI except UI.
184 /// UI must be in the same block as DI.
185 /// The routine checks that the DI parent and DB are different.
186 bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
187 const BasicBlock *DB) const;
188
189 /// Try to replace select with select operand SIOpd in SI-ICmp sequence.
190 bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
191 const unsigned SIOpd);
192
193 LoadInst *combineLoadToNewType(LoadInst &LI, Type *NewTy,
194 const Twine &Suffix = "");
195
196private:
197 bool annotateAnyAllocSite(CallBase &Call, const TargetLibraryInfo *TLI);
198 bool isDesirableIntType(unsigned BitWidth) const;
199 bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
200 bool shouldChangeType(Type *From, Type *To) const;
201 Value *dyn_castNegVal(Value *V) const;
202
203 /// Classify whether a cast is worth optimizing.
204 ///
205 /// This is a helper to decide whether the simplification of
206 /// logic(cast(A), cast(B)) to cast(logic(A, B)) should be performed.
207 ///
208 /// \param CI The cast we are interested in.
209 ///
210 /// \return true if this cast actually results in any code being generated and
211 /// if it cannot already be eliminated by some other transformation.
212 bool shouldOptimizeCast(CastInst *CI);
213
214 /// Try to optimize a sequence of instructions checking if an operation
215 /// on LHS and RHS overflows.
216 ///
217 /// If this overflow check is done via one of the overflow check intrinsics,
218 /// then CtxI has to be the call instruction calling that intrinsic. If this
219 /// overflow check is done by arithmetic followed by a compare, then CtxI has
220 /// to be the arithmetic instruction.
221 ///
222 /// If a simplification is possible, stores the simplified result of the
223 /// operation in OperationResult and result of the overflow check in
224 /// OverflowResult, and return true. If no simplification is possible,
225 /// returns false.
226 bool OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp, bool IsSigned,
227 Value *LHS, Value *RHS,
228 Instruction &CtxI, Value *&OperationResult,
230
231 Instruction *visitCallBase(CallBase &Call);
232 Instruction *tryOptimizeCall(CallInst *CI);
233 bool transformConstExprCastCall(CallBase &Call);
234 Instruction *transformCallThroughTrampoline(CallBase &Call,
235 IntrinsicInst &Tramp);
236
237 Value *simplifyMaskedLoad(IntrinsicInst &II);
238 Instruction *simplifyMaskedStore(IntrinsicInst &II);
239 Instruction *simplifyMaskedGather(IntrinsicInst &II);
240 Instruction *simplifyMaskedScatter(IntrinsicInst &II);
241
242 /// Transform (zext icmp) to bitwise / integer operations in order to
243 /// eliminate it.
244 ///
245 /// \param ICI The icmp of the (zext icmp) pair we are interested in.
246 /// \parem CI The zext of the (zext icmp) pair we are interested in.
247 ///
248 /// \return null if the transformation cannot be performed. If the
249 /// transformation can be performed the new instruction that replaces the
250 /// (zext icmp) pair will be returned.
251 Instruction *transformZExtICmp(ICmpInst *Cmp, ZExtInst &Zext);
252
253 Instruction *transformSExtICmp(ICmpInst *Cmp, SExtInst &Sext);
254
255 bool willNotOverflowSignedAdd(const Value *LHS, const Value *RHS,
256 const Instruction &CxtI) const {
257 return computeOverflowForSignedAdd(LHS, RHS, &CxtI) ==
259 }
260
261 bool willNotOverflowUnsignedAdd(const Value *LHS, const Value *RHS,
262 const Instruction &CxtI) const {
263 return computeOverflowForUnsignedAdd(LHS, RHS, &CxtI) ==
265 }
266
267 bool willNotOverflowAdd(const Value *LHS, const Value *RHS,
268 const Instruction &CxtI, bool IsSigned) const {
269 return IsSigned ? willNotOverflowSignedAdd(LHS, RHS, CxtI)
270 : willNotOverflowUnsignedAdd(LHS, RHS, CxtI);
271 }
272
273 bool willNotOverflowSignedSub(const Value *LHS, const Value *RHS,
274 const Instruction &CxtI) const {
275 return computeOverflowForSignedSub(LHS, RHS, &CxtI) ==
277 }
278
279 bool willNotOverflowUnsignedSub(const Value *LHS, const Value *RHS,
280 const Instruction &CxtI) const {
281 return computeOverflowForUnsignedSub(LHS, RHS, &CxtI) ==
283 }
284
285 bool willNotOverflowSub(const Value *LHS, const Value *RHS,
286 const Instruction &CxtI, bool IsSigned) const {
287 return IsSigned ? willNotOverflowSignedSub(LHS, RHS, CxtI)
288 : willNotOverflowUnsignedSub(LHS, RHS, CxtI);
289 }
290
291 bool willNotOverflowSignedMul(const Value *LHS, const Value *RHS,
292 const Instruction &CxtI) const {
293 return computeOverflowForSignedMul(LHS, RHS, &CxtI) ==
295 }
296
297 bool willNotOverflowUnsignedMul(const Value *LHS, const Value *RHS,
298 const Instruction &CxtI) const {
299 return computeOverflowForUnsignedMul(LHS, RHS, &CxtI) ==
301 }
302
303 bool willNotOverflowMul(const Value *LHS, const Value *RHS,
304 const Instruction &CxtI, bool IsSigned) const {
305 return IsSigned ? willNotOverflowSignedMul(LHS, RHS, CxtI)
306 : willNotOverflowUnsignedMul(LHS, RHS, CxtI);
307 }
308
309 bool willNotOverflow(BinaryOperator::BinaryOps Opcode, const Value *LHS,
310 const Value *RHS, const Instruction &CxtI,
311 bool IsSigned) const {
312 switch (Opcode) {
313 case Instruction::Add: return willNotOverflowAdd(LHS, RHS, CxtI, IsSigned);
314 case Instruction::Sub: return willNotOverflowSub(LHS, RHS, CxtI, IsSigned);
315 case Instruction::Mul: return willNotOverflowMul(LHS, RHS, CxtI, IsSigned);
316 default: llvm_unreachable("Unexpected opcode for overflow query");
317 }
318 }
319
320 Value *EmitGEPOffset(User *GEP);
321 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
322 Instruction *foldBitcastExtElt(ExtractElementInst &ExtElt);
323 Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
324 Instruction *foldBinopOfSextBoolToSelect(BinaryOperator &I);
325 Instruction *narrowBinOp(TruncInst &Trunc);
326 Instruction *narrowMaskedBinOp(BinaryOperator &And);
327 Instruction *narrowMathIfNoOverflow(BinaryOperator &I);
328 Instruction *narrowFunnelShift(TruncInst &Trunc);
329 Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
330 Instruction *matchSAddSubSat(IntrinsicInst &MinMax1);
331 Instruction *foldNot(BinaryOperator &I);
332
333 void freelyInvertAllUsersOf(Value *V, Value *IgnoredUser = nullptr);
334
335 /// Determine if a pair of casts can be replaced by a single cast.
336 ///
337 /// \param CI1 The first of a pair of casts.
338 /// \param CI2 The second of a pair of casts.
339 ///
340 /// \return 0 if the cast pair cannot be eliminated, otherwise returns an
341 /// Instruction::CastOps value for a cast that can replace the pair, casting
342 /// CI1->getSrcTy() to CI2->getDstTy().
343 ///
344 /// \see CastInst::isEliminableCastPair
345 Instruction::CastOps isEliminableCastPair(const CastInst *CI1,
346 const CastInst *CI2);
347 Value *simplifyIntToPtrRoundTripCast(Value *Val);
348
349 Value *foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &I,
350 bool IsAnd, bool IsLogical = false);
351 Value *foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS, BinaryOperator &Xor);
352
353 Value *foldEqOfParts(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd);
354
355 Value *foldAndOrOfICmpsUsingRanges(ICmpInst *ICmp1, ICmpInst *ICmp2,
356 bool IsAnd);
357
358 /// Optimize (fcmp)&(fcmp) or (fcmp)|(fcmp).
359 /// NOTE: Unlike most of instcombine, this returns a Value which should
360 /// already be inserted into the function.
361 Value *foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS, bool IsAnd,
362 bool IsLogicalSelect = false);
363
364 Instruction *foldLogicOfIsFPClass(BinaryOperator &Operator, Value *LHS,
365 Value *RHS);
366
368 canonicalizeConditionalNegationViaMathToSelect(BinaryOperator &i);
369
370 Value *foldAndOrOfICmpsOfAndWithPow2(ICmpInst *LHS, ICmpInst *RHS,
371 Instruction *CxtI, bool IsAnd,
372 bool IsLogical = false);
373 Value *matchSelectFromAndOr(Value *A, Value *B, Value *C, Value *D,
374 bool InvertFalseVal = false);
375 Value *getSelectCondition(Value *A, Value *B, bool ABIsTheSame);
376
377 Instruction *foldLShrOverflowBit(BinaryOperator &I);
378 Instruction *foldExtractOfOverflowIntrinsic(ExtractValueInst &EV);
379 Instruction *foldIntrinsicWithOverflowCommon(IntrinsicInst *II);
380 Instruction *foldIntrinsicIsFPClass(IntrinsicInst &II);
381 Instruction *foldFPSignBitOps(BinaryOperator &I);
382 Instruction *foldFDivConstantDivisor(BinaryOperator &I);
383
384 // Optimize one of these forms:
385 // and i1 Op, SI / select i1 Op, i1 SI, i1 false (if IsAnd = true)
386 // or i1 Op, SI / select i1 Op, i1 true, i1 SI (if IsAnd = false)
387 // into simplier select instruction using isImpliedCondition.
388 Instruction *foldAndOrOfSelectUsingImpliedCond(Value *Op, SelectInst &SI,
389 bool IsAnd);
390
391public:
392 /// Create and insert the idiom we use to indicate a block is unreachable
393 /// without having to rewrite the CFG from within InstCombine.
395 auto &Ctx = InsertAt->getContext();
398 InsertAt);
399 }
400
401
402 /// Combiner aware instruction erasure.
403 ///
404 /// When dealing with an instruction that has side effects or produces a void
405 /// value, we can't rely on DCE to delete the instruction. Instead, visit
406 /// methods should return the value returned by this function.
408 LLVM_DEBUG(dbgs() << "IC: ERASE " << I << '\n');
409 assert(I.use_empty() && "Cannot erase instruction that is used!");
411
412 // Make sure that we reprocess all operands now that we reduced their
413 // use counts.
414 for (Use &Operand : I.operands())
415 if (auto *Inst = dyn_cast<Instruction>(Operand))
416 Worklist.add(Inst);
417
418 Worklist.remove(&I);
419 I.eraseFromParent();
420 MadeIRChange = true;
421 return nullptr; // Don't do anything with FI
422 }
423
424 OverflowResult computeOverflow(
425 Instruction::BinaryOps BinaryOp, bool IsSigned,
426 Value *LHS, Value *RHS, Instruction *CxtI) const;
427
428 /// Performs a few simplifications for operators which are associative
429 /// or commutative.
430 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
431
432 /// Tries to simplify binary operations which some other binary
433 /// operation distributes over.
434 ///
435 /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
436 /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
437 /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
438 /// value, or null if it didn't simplify.
439 Value *foldUsingDistributiveLaws(BinaryOperator &I);
440
441 /// Tries to simplify add operations using the definition of remainder.
442 ///
443 /// The definition of remainder is X % C = X - (X / C ) * C. The add
444 /// expression X % C0 + (( X / C0 ) % C1) * C0 can be simplified to
445 /// X % (C0 * C1)
446 Value *SimplifyAddWithRemainder(BinaryOperator &I);
447
448 // Binary Op helper for select operations where the expression can be
449 // efficiently reorganized.
450 Value *SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *LHS,
451 Value *RHS);
452
453 /// This tries to simplify binary operations by factorizing out common terms
454 /// (e. g. "(A*B)+(A*C)" -> "A*(B+C)").
455 Value *tryFactorizationFolds(BinaryOperator &I);
456
457 /// Match a select chain which produces one of three values based on whether
458 /// the LHS is less than, equal to, or greater than RHS respectively.
459 /// Return true if we matched a three way compare idiom. The LHS, RHS, Less,
460 /// Equal and Greater values are saved in the matching process and returned to
461 /// the caller.
462 bool matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, Value *&RHS,
463 ConstantInt *&Less, ConstantInt *&Equal,
464 ConstantInt *&Greater);
465
466 /// Attempts to replace V with a simpler value based on the demanded
467 /// bits.
468 Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, KnownBits &Known,
469 unsigned Depth, Instruction *CxtI);
470 bool SimplifyDemandedBits(Instruction *I, unsigned Op,
471 const APInt &DemandedMask, KnownBits &Known,
472 unsigned Depth = 0) override;
473
474 /// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
475 /// bits. It also tries to handle simplifications that can be done based on
476 /// DemandedMask, but without modifying the Instruction.
477 Value *SimplifyMultipleUseDemandedBits(Instruction *I,
478 const APInt &DemandedMask,
479 KnownBits &Known,
480 unsigned Depth, Instruction *CxtI);
481
482 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
483 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
484 Value *simplifyShrShlDemandedBits(
485 Instruction *Shr, const APInt &ShrOp1, Instruction *Shl,
486 const APInt &ShlOp1, const APInt &DemandedMask, KnownBits &Known);
487
488 /// Tries to simplify operands to an integer instruction based on its
489 /// demanded bits.
490 bool SimplifyDemandedInstructionBits(Instruction &Inst);
491
492 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
493 APInt &UndefElts, unsigned Depth = 0,
494 bool AllowMultipleUsers = false) override;
495
496 /// Canonicalize the position of binops relative to shufflevector.
497 Instruction *foldVectorBinop(BinaryOperator &Inst);
499 Instruction *foldSelectShuffle(ShuffleVectorInst &Shuf);
500
501 /// Given a binary operator, cast instruction, or select which has a PHI node
502 /// as operand #0, see if we can fold the instruction into the PHI (which is
503 /// only possible if all operands to the PHI are constants).
504 Instruction *foldOpIntoPhi(Instruction &I, PHINode *PN);
505
506 /// For a binary operator with 2 phi operands, try to hoist the binary
507 /// operation before the phi. This can result in fewer instructions in
508 /// patterns where at least one set of phi operands simplifies.
509 /// Example:
510 /// BB3: binop (phi [X, BB1], [C1, BB2]), (phi [Y, BB1], [C2, BB2])
511 /// -->
512 /// BB1: BO = binop X, Y
513 /// BB3: phi [BO, BB1], [(binop C1, C2), BB2]
514 Instruction *foldBinopWithPhiOperands(BinaryOperator &BO);
515
516 /// Given an instruction with a select as one operand and a constant as the
517 /// other operand, try to fold the binary operator into the select arguments.
518 /// This also works for Cast instructions, which obviously do not have a
519 /// second operand.
520 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
521 bool FoldWithMultiUse = false);
522
523 /// This is a convenience wrapper function for the above two functions.
524 Instruction *foldBinOpIntoSelectOrPhi(BinaryOperator &I);
525
526 Instruction *foldAddWithConstant(BinaryOperator &Add);
527
528 /// Try to rotate an operation below a PHI node, using PHI nodes for
529 /// its operands.
530 Instruction *foldPHIArgOpIntoPHI(PHINode &PN);
531 Instruction *foldPHIArgBinOpIntoPHI(PHINode &PN);
532 Instruction *foldPHIArgInsertValueInstructionIntoPHI(PHINode &PN);
533 Instruction *foldPHIArgExtractValueInstructionIntoPHI(PHINode &PN);
534 Instruction *foldPHIArgGEPIntoPHI(PHINode &PN);
535 Instruction *foldPHIArgLoadIntoPHI(PHINode &PN);
536 Instruction *foldPHIArgZextsIntoPHI(PHINode &PN);
537 Instruction *foldPHIArgIntToPtrToPHI(PHINode &PN);
538
539 /// If an integer typed PHI has only one use which is an IntToPtr operation,
540 /// replace the PHI with an existing pointer typed PHI if it exists. Otherwise
541 /// insert a new pointer typed PHI and replace the original one.
542 bool foldIntegerTypedPHI(PHINode &PN);
543
544 /// Helper function for FoldPHIArgXIntoPHI() to set debug location for the
545 /// folded operation.
546 void PHIArgMergedDebugLoc(Instruction *Inst, PHINode &PN);
547
548 Instruction *foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
550 Instruction *foldSelectICmp(ICmpInst::Predicate Pred, SelectInst *SI,
551 Value *RHS, const ICmpInst &I);
552 bool foldAllocaCmp(AllocaInst *Alloca);
553 Instruction *foldCmpLoadFromIndexedGlobal(LoadInst *LI,
555 GlobalVariable *GV, CmpInst &ICI,
556 ConstantInt *AndCst = nullptr);
557 Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI,
558 Constant *RHSC);
559 Instruction *foldICmpAddOpConst(Value *X, const APInt &C,
561 Instruction *foldICmpWithCastOp(ICmpInst &ICmp);
562 Instruction *foldICmpWithZextOrSext(ICmpInst &ICmp);
563
564 Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp);
566 Instruction *foldICmpWithConstant(ICmpInst &Cmp);
567 Instruction *foldICmpUsingBoolRange(ICmpInst &I);
568 Instruction *foldICmpInstWithConstant(ICmpInst &Cmp);
569 Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp);
570 Instruction *foldICmpInstWithConstantAllowUndef(ICmpInst &Cmp,
571 const APInt &C);
572 Instruction *foldICmpBinOp(ICmpInst &Cmp, const SimplifyQuery &SQ);
573 Instruction *foldICmpEquality(ICmpInst &Cmp);
574 Instruction *foldIRemByPowerOfTwoToBitTest(ICmpInst &I);
575 Instruction *foldSignBitTest(ICmpInst &I);
576 Instruction *foldICmpWithZero(ICmpInst &Cmp);
577
578 Value *foldMultiplicationOverflowCheck(ICmpInst &Cmp);
579
580 Instruction *foldICmpBinOpWithConstant(ICmpInst &Cmp, BinaryOperator *BO,
581 const APInt &C);
582 Instruction *foldICmpSelectConstant(ICmpInst &Cmp, SelectInst *Select,
583 ConstantInt *C);
584 Instruction *foldICmpTruncConstant(ICmpInst &Cmp, TruncInst *Trunc,
585 const APInt &C);
586 Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And,
587 const APInt &C);
588 Instruction *foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor,
589 const APInt &C);
590 Instruction *foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or,
591 const APInt &C);
592 Instruction *foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul,
593 const APInt &C);
594 Instruction *foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl,
595 const APInt &C);
596 Instruction *foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr,
597 const APInt &C);
598 Instruction *foldICmpSRemConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
599 const APInt &C);
600 Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
601 const APInt &C);
602 Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div,
603 const APInt &C);
604 Instruction *foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub,
605 const APInt &C);
606 Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add,
607 const APInt &C);
608 Instruction *foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And,
609 const APInt &C1);
610 Instruction *foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And,
611 const APInt &C1, const APInt &C2);
612 Instruction *foldICmpXorShiftConst(ICmpInst &Cmp, BinaryOperator *Xor,
613 const APInt &C);
614 Instruction *foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
615 const APInt &C2);
616 Instruction *foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
617 const APInt &C2);
618
619 Instruction *foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp,
620 BinaryOperator *BO,
621 const APInt &C);
622 Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II,
623 const APInt &C);
624 Instruction *foldICmpEqIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II,
625 const APInt &C);
626 Instruction *foldICmpBitCast(ICmpInst &Cmp);
627 Instruction *foldICmpWithTrunc(ICmpInst &Cmp);
628
629 // Helpers of visitSelectInst().
632 Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
633 Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *);
635 Value *A, Value *B, Instruction &Outer,
640 unsigned Depth = 0);
641
642 Value *insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi,
643 bool isSigned, bool Inside);
644 bool mergeStoreIntoSuccessor(StoreInst &SI);
645
646 /// Given an initial instruction, check to see if it is the root of a
647 /// bswap/bitreverse idiom. If so, return the equivalent bswap/bitreverse
648 /// intrinsic.
649 Instruction *matchBSwapOrBitReverse(Instruction &I, bool MatchBSwaps,
650 bool MatchBitReversals);
651
652 Instruction *SimplifyAnyMemTransfer(AnyMemTransferInst *MI);
653 Instruction *SimplifyAnyMemSet(AnyMemSetInst *MI);
654
655 Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
656
657 bool tryToSinkInstruction(Instruction *I, BasicBlock *DestBlock);
658};
659
660class Negator final {
661 /// Top-to-bottom, def-to-use negated instruction tree we produced.
663
665 BuilderTy Builder;
666
667 const DataLayout &DL;
668 AssumptionCache &AC;
669 const DominatorTree &DT;
670
671 const bool IsTrulyNegation;
672
673 SmallDenseMap<Value *, Value *> NegationsCache;
674
676 const DominatorTree &DT, bool IsTrulyNegation);
677
678#if LLVM_ENABLE_STATS
679 unsigned NumValuesVisitedInThisNegator = 0;
680 ~Negator();
681#endif
682
683 using Result = std::pair<ArrayRef<Instruction *> /*NewInstructions*/,
684 Value * /*NegatedRoot*/>;
685
686 std::array<Value *, 2> getSortedOperandsOfBinOp(Instruction *I);
687
688 [[nodiscard]] Value *visitImpl(Value *V, unsigned Depth);
689
690 [[nodiscard]] Value *negate(Value *V, unsigned Depth);
691
692 /// Recurse depth-first and attempt to sink the negation.
693 /// FIXME: use worklist?
694 [[nodiscard]] std::optional<Result> run(Value *Root);
695
696 Negator(const Negator &) = delete;
697 Negator(Negator &&) = delete;
698 Negator &operator=(const Negator &) = delete;
699 Negator &operator=(Negator &&) = delete;
700
701public:
702 /// Attempt to negate \p Root. Retuns nullptr if negation can't be performed,
703 /// otherwise returns negated value.
704 [[nodiscard]] static Value *Negate(bool LHSIsZero, Value *Root,
705 InstCombinerImpl &IC);
706};
707
708} // end namespace llvm
709
710#undef DEBUG_TYPE
711
712#endif // LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
amdgpu AMDGPU Register Bank Select
assume Assume Builder
SmallVector< MachineOperand, 4 > Cond
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static bool foldICmpWithDominatingICmp(CmpInst *Cmp, const TargetLowering &TLI)
For pattern like:
#define LLVM_LIBRARY_VISIBILITY
LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked into a shared library,...
Definition: Compiler.h:126
static bool willNotOverflow(BinaryOpIntrinsic *BO, LazyValueInfo *LVI)
#define LLVM_DEBUG(X)
Definition: Debug.h:101
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool isSigned(unsigned int Opcode)
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.
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
Value * RHS
Value * LHS
static const uint32_t IV[8]
Definition: blake3_impl.h:77
Class for arbitrary precision integers.
Definition: APInt.h:75
This class represents a conversion between pointers from one address space to another.
an instruction to allocate memory on the stack
Definition: Instructions.h:58
This class represents any memset intrinsic.
A cache of @llvm.assume calls within a function.
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:718
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
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.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1190
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:428
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:701
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:711
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:833
This is an important base class in LLVM.
Definition: Constant.h:41
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
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.
An instruction for ordering other memory operations.
Definition: Instructions.h:436
This class represents a freeze function that returns random concrete value if an operand is either a ...
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:940
This instruction compares its operands according to the predicate given to the constructor.
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.
virtual ~InstCombinerImpl()=default
Instruction * foldVectorSelect(SelectInst &Sel)
Instruction * foldSelectValueEquivalence(SelectInst &SI, ICmpInst &ICI)
Instruction * foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1, Value *A, Value *B, Instruction &Outer, SelectPatternFlavor SPF2, Value *C)
bool replaceInInstruction(Value *V, Value *Old, Value *New, unsigned Depth=0)
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Instruction * foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI)
Instruction * visitInstruction(Instruction &I)
Specify what to return for unhandled instructions.
InstCombinerImpl(InstructionWorklist &Worklist, BuilderTy &Builder, bool MinimizeSize, AAResults *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, TargetTransformInfo &TTI, DominatorTree &DT, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, const DataLayout &DL, LoopInfo *LI)
void CreateNonTerminatorUnreachable(Instruction *InsertAt)
Create and insert the idiom we use to indicate a block is unreachable without having to rewrite the C...
Instruction * visitSelectInst(SelectInst &SI)
Instruction * foldSelectOfBools(SelectInst &SI)
Instruction * foldSelectExtConst(SelectInst &Sel)
The core instruction combiner logic.
Definition: InstCombiner.h:46
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.
Definition: IntrinsicInst.h:47
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
The landingpad instruction holds all of the information necessary to generate correct exception handl...
An instruction for reading from memory.
Definition: Instructions.h:177
static Value * Negate(bool LHSIsZero, Value *Root, InstCombinerImpl &IC)
Attempt to negate Root.
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:31
The optimization diagnostic interface.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1743
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 is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
An instruction for storing to memory.
Definition: Instructions.h:301
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:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static PointerType * getInt1PtrTy(LLVMContext &C, unsigned AS=0)
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
This represents the llvm.va_end intrinsic.
LLVM Value Representation.
Definition: Value.h:74
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1069
This class represents zero extension of integer types.
#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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
OverflowResult
@ NeverOverflows
Never overflows.
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo=true)
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
OverflowResult computeOverflowForSignedAdd(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo=true)
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:1364
SelectPatternFlavor
Specific patterns of select instructions we can match.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ Xor
Bitwise or logical XOR of integers.
@ And
Bitwise or logical AND of integers.
@ Add
Sum of integers.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:184
OverflowResult computeOverflowForUnsignedAdd(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo=true)