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"
26#include "llvm/IR/Value.h"
27#include "llvm/Support/Debug.h"
32#include <cassert>
33
34#define DEBUG_TYPE "instcombine"
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;
50class BlockFrequencyInfo;
51class DataLayout;
52class DominatorTree;
53class GEPOperator;
54class GlobalVariable;
55class OptimizationRemarkEmitter;
56class ProfileSummaryInfo;
57class TargetLibraryInfo;
58class User;
59
61 : public InstCombiner,
62 public InstVisitor<InstCombinerImpl, Instruction *> {
63public:
65 bool MinimizeSize, AAResults *AA, AssumptionCache &AC,
69 ProfileSummaryInfo *PSI, const DataLayout &DL,
71 : InstCombiner(Worklist, Builder, MinimizeSize, AA, AC, TLI, TTI, DT, ORE,
72 BFI, BPI, PSI, DL, RPOT) {}
73
74 virtual ~InstCombinerImpl() = default;
75
76 /// Perform early cleanup and prepare the InstCombine worklist.
77 bool prepareWorklist(Function &F);
78
79 /// Run the combiner over the entire worklist until it is empty.
80 ///
81 /// \returns true if the IR is changed.
82 bool run();
83
84 // Visitation implementation - Implement instruction combining for different
85 // instruction types. The semantics are as follows:
86 // Return Value:
87 // null - No change was made
88 // I - Change was made, I is still valid, I may be dead though
89 // otherwise - Change was made, replace I with returned instruction
90 //
91 Instruction *visitFNeg(UnaryOperator &I);
92 Instruction *visitAdd(BinaryOperator &I);
93 Instruction *visitFAdd(BinaryOperator &I);
94 Value *OptimizePointerDifference(
95 Value *LHS, Value *RHS, Type *Ty, bool isNUW);
96 Instruction *visitSub(BinaryOperator &I);
97 Instruction *visitFSub(BinaryOperator &I);
98 Instruction *visitMul(BinaryOperator &I);
99 Instruction *foldPowiReassoc(BinaryOperator &I);
100 Instruction *foldFMulReassoc(BinaryOperator &I);
101 Instruction *visitFMul(BinaryOperator &I);
102 Instruction *visitURem(BinaryOperator &I);
103 Instruction *visitSRem(BinaryOperator &I);
104 Instruction *visitFRem(BinaryOperator &I);
105 bool simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I);
106 Instruction *commonIDivRemTransforms(BinaryOperator &I);
107 Instruction *commonIRemTransforms(BinaryOperator &I);
108 Instruction *commonIDivTransforms(BinaryOperator &I);
109 Instruction *visitUDiv(BinaryOperator &I);
110 Instruction *visitSDiv(BinaryOperator &I);
111 Instruction *visitFDiv(BinaryOperator &I);
112 Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
113 Instruction *visitAnd(BinaryOperator &I);
114 Instruction *visitOr(BinaryOperator &I);
115 bool sinkNotIntoLogicalOp(Instruction &I);
116 bool sinkNotIntoOtherHandOfLogicalOp(Instruction &I);
117 Instruction *visitXor(BinaryOperator &I);
118 Instruction *visitShl(BinaryOperator &I);
119 Value *reassociateShiftAmtsOfTwoSameDirectionShifts(
120 BinaryOperator *Sh0, const SimplifyQuery &SQ,
121 bool AnalyzeForSignBitExtraction = false);
122 Instruction *canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(
124 Instruction *foldVariableSignZeroExtensionOfVariableHighBitExtract(
125 BinaryOperator &OldAShr);
126 Instruction *visitAShr(BinaryOperator &I);
127 Instruction *visitLShr(BinaryOperator &I);
128 Instruction *commonShiftTransforms(BinaryOperator &I);
129 Instruction *visitFCmpInst(FCmpInst &I);
130 CmpInst *canonicalizeICmpPredicate(CmpInst &I);
131 Instruction *visitICmpInst(ICmpInst &I);
132 Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
134 Instruction *commonCastTransforms(CastInst &CI);
135 Instruction *visitTrunc(TruncInst &CI);
136 Instruction *visitZExt(ZExtInst &Zext);
137 Instruction *visitSExt(SExtInst &Sext);
138 Instruction *visitFPTrunc(FPTruncInst &CI);
139 Instruction *visitFPExt(CastInst &CI);
140 Instruction *visitFPToUI(FPToUIInst &FI);
141 Instruction *visitFPToSI(FPToSIInst &FI);
142 Instruction *visitUIToFP(CastInst &CI);
143 Instruction *visitSIToFP(CastInst &CI);
144 Instruction *visitPtrToInt(PtrToIntInst &CI);
145 Instruction *visitIntToPtr(IntToPtrInst &CI);
146 Instruction *visitBitCast(BitCastInst &CI);
147 Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
148 Instruction *foldItoFPtoI(CastInst &FI);
150 Instruction *foldShuffledIntrinsicOperands(IntrinsicInst *II);
151 Value *foldReversedIntrinsicOperands(IntrinsicInst *II);
152 Instruction *visitCallInst(CallInst &CI);
153 Instruction *visitInvokeInst(InvokeInst &II);
154 Instruction *visitCallBrInst(CallBrInst &CBI);
155
156 Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
157 Instruction *visitPHINode(PHINode &PN);
158 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
159 Instruction *visitGEPOfGEP(GetElementPtrInst &GEP, GEPOperator *Src);
160 Instruction *visitAllocaInst(AllocaInst &AI);
161 Instruction *visitAllocSite(Instruction &FI);
162 Instruction *visitFree(CallInst &FI, Value *FreedOp);
163 Instruction *visitLoadInst(LoadInst &LI);
164 Instruction *visitStoreInst(StoreInst &SI);
165 Instruction *visitAtomicRMWInst(AtomicRMWInst &SI);
166 Instruction *visitUnconditionalBranchInst(BranchInst &BI);
167 Instruction *visitBranchInst(BranchInst &BI);
168 Instruction *visitFenceInst(FenceInst &FI);
169 Instruction *visitSwitchInst(SwitchInst &SI);
170 Instruction *visitReturnInst(ReturnInst &RI);
171 Instruction *visitUnreachableInst(UnreachableInst &I);
173 foldAggregateConstructionIntoAggregateReuse(InsertValueInst &OrigIVI);
174 Instruction *visitInsertValueInst(InsertValueInst &IV);
175 Instruction *visitInsertElementInst(InsertElementInst &IE);
176 Instruction *visitExtractElementInst(ExtractElementInst &EI);
177 Instruction *simplifyBinOpSplats(ShuffleVectorInst &SVI);
178 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
179 Instruction *visitExtractValueInst(ExtractValueInst &EV);
180 Instruction *visitLandingPadInst(LandingPadInst &LI);
181 Instruction *visitVAEndInst(VAEndInst &I);
182 Value *pushFreezeToPreventPoisonFromPropagating(FreezeInst &FI);
183 bool freezeOtherUses(FreezeInst &FI);
184 Instruction *foldFreezeIntoRecurrence(FreezeInst &I, PHINode *PN);
185 Instruction *visitFreeze(FreezeInst &I);
186
187 /// Specify what to return for unhandled instructions.
189
190 /// True when DB dominates all uses of DI except UI.
191 /// UI must be in the same block as DI.
192 /// The routine checks that the DI parent and DB are different.
193 bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
194 const BasicBlock *DB) const;
195
196 /// Try to replace select with select operand SIOpd in SI-ICmp sequence.
197 bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
198 const unsigned SIOpd);
199
200 LoadInst *combineLoadToNewType(LoadInst &LI, Type *NewTy,
201 const Twine &Suffix = "");
202
204 FPClassTest Interested = fcAllFlags,
205 const Instruction *CtxI = nullptr,
206 unsigned Depth = 0) const {
208 Val, FMF, Interested, getSimplifyQuery().getWithInstruction(CtxI),
209 Depth);
210 }
211
213 FPClassTest Interested = fcAllFlags,
214 const Instruction *CtxI = nullptr,
215 unsigned Depth = 0) const {
217 Val, Interested, getSimplifyQuery().getWithInstruction(CtxI), Depth);
218 }
219
220 /// Check if fmul \p MulVal, +0.0 will yield +0.0 (or signed zero is
221 /// ignorable).
223 const Instruction *CtxI) const;
224
225 Constant *getLosslessTrunc(Constant *C, Type *TruncTy, unsigned ExtOp) {
226 Constant *TruncC = ConstantExpr::getTrunc(C, TruncTy);
227 Constant *ExtTruncC =
228 ConstantFoldCastOperand(ExtOp, TruncC, C->getType(), DL);
229 if (ExtTruncC && ExtTruncC == C)
230 return TruncC;
231 return nullptr;
232 }
233
235 return getLosslessTrunc(C, TruncTy, Instruction::ZExt);
236 }
237
239 return getLosslessTrunc(C, TruncTy, Instruction::SExt);
240 }
241
242 std::optional<std::pair<Intrinsic::ID, SmallVector<Value *, 3>>>
243 convertOrOfShiftsToFunnelShift(Instruction &Or);
244
245private:
246 bool annotateAnyAllocSite(CallBase &Call, const TargetLibraryInfo *TLI);
247 bool isDesirableIntType(unsigned BitWidth) const;
248 bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
249 bool shouldChangeType(Type *From, Type *To) const;
250 Value *dyn_castNegVal(Value *V) const;
251
252 /// Classify whether a cast is worth optimizing.
253 ///
254 /// This is a helper to decide whether the simplification of
255 /// logic(cast(A), cast(B)) to cast(logic(A, B)) should be performed.
256 ///
257 /// \param CI The cast we are interested in.
258 ///
259 /// \return true if this cast actually results in any code being generated and
260 /// if it cannot already be eliminated by some other transformation.
261 bool shouldOptimizeCast(CastInst *CI);
262
263 /// Try to optimize a sequence of instructions checking if an operation
264 /// on LHS and RHS overflows.
265 ///
266 /// If this overflow check is done via one of the overflow check intrinsics,
267 /// then CtxI has to be the call instruction calling that intrinsic. If this
268 /// overflow check is done by arithmetic followed by a compare, then CtxI has
269 /// to be the arithmetic instruction.
270 ///
271 /// If a simplification is possible, stores the simplified result of the
272 /// operation in OperationResult and result of the overflow check in
273 /// OverflowResult, and return true. If no simplification is possible,
274 /// returns false.
275 bool OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp, bool IsSigned,
276 Value *LHS, Value *RHS,
277 Instruction &CtxI, Value *&OperationResult,
279
280 Instruction *visitCallBase(CallBase &Call);
281 Instruction *tryOptimizeCall(CallInst *CI);
282 bool transformConstExprCastCall(CallBase &Call);
283 Instruction *transformCallThroughTrampoline(CallBase &Call,
284 IntrinsicInst &Tramp);
285
286 /// Try to optimize a call to the result of a ptrauth intrinsic, potentially
287 /// into the ptrauth call bundle:
288 /// - call(ptrauth.resign(p)), ["ptrauth"()] -> call p, ["ptrauth"()]
289 /// - call(ptrauth.sign(p)), ["ptrauth"()] -> call p
290 /// as long as the key/discriminator are the same in sign and auth-bundle,
291 /// and we don't change the key in the bundle (to a potentially-invalid key.)
292 Instruction *foldPtrAuthIntrinsicCallee(CallBase &Call);
293
294 /// Try to optimize a call to a ptrauth constant, into its ptrauth bundle:
295 /// call(ptrauth(f)), ["ptrauth"()] -> call f
296 /// as long as the key/discriminator are the same in constant and bundle.
297 Instruction *foldPtrAuthConstantCallee(CallBase &Call);
298
299 // Return (a, b) if (LHS, RHS) is known to be (a, b) or (b, a).
300 // Otherwise, return std::nullopt
301 // Currently it matches:
302 // - LHS = (select c, a, b), RHS = (select c, b, a)
303 // - LHS = (phi [a, BB0], [b, BB1]), RHS = (phi [b, BB0], [a, BB1])
304 // - LHS = min(a, b), RHS = max(a, b)
305 std::optional<std::pair<Value *, Value *>> matchSymmetricPair(Value *LHS,
306 Value *RHS);
307
308 Value *simplifyMaskedLoad(IntrinsicInst &II);
309 Instruction *simplifyMaskedStore(IntrinsicInst &II);
310 Instruction *simplifyMaskedGather(IntrinsicInst &II);
311 Instruction *simplifyMaskedScatter(IntrinsicInst &II);
312
313 /// Transform (zext icmp) to bitwise / integer operations in order to
314 /// eliminate it.
315 ///
316 /// \param ICI The icmp of the (zext icmp) pair we are interested in.
317 /// \parem CI The zext of the (zext icmp) pair we are interested in.
318 ///
319 /// \return null if the transformation cannot be performed. If the
320 /// transformation can be performed the new instruction that replaces the
321 /// (zext icmp) pair will be returned.
322 Instruction *transformZExtICmp(ICmpInst *Cmp, ZExtInst &Zext);
323
324 Instruction *transformSExtICmp(ICmpInst *Cmp, SExtInst &Sext);
325
326 bool willNotOverflowSignedAdd(const WithCache<const Value *> &LHS,
328 const Instruction &CxtI) const {
329 return computeOverflowForSignedAdd(LHS, RHS, &CxtI) ==
330 OverflowResult::NeverOverflows;
331 }
332
333 bool willNotOverflowUnsignedAdd(const WithCache<const Value *> &LHS,
334 const WithCache<const Value *> &RHS,
335 const Instruction &CxtI) const {
336 return computeOverflowForUnsignedAdd(LHS, RHS, &CxtI) ==
337 OverflowResult::NeverOverflows;
338 }
339
340 bool willNotOverflowAdd(const Value *LHS, const Value *RHS,
341 const Instruction &CxtI, bool IsSigned) const {
342 return IsSigned ? willNotOverflowSignedAdd(LHS, RHS, CxtI)
343 : willNotOverflowUnsignedAdd(LHS, RHS, CxtI);
344 }
345
346 bool willNotOverflowSignedSub(const Value *LHS, const Value *RHS,
347 const Instruction &CxtI) const {
348 return computeOverflowForSignedSub(LHS, RHS, &CxtI) ==
349 OverflowResult::NeverOverflows;
350 }
351
352 bool willNotOverflowUnsignedSub(const Value *LHS, const Value *RHS,
353 const Instruction &CxtI) const {
354 return computeOverflowForUnsignedSub(LHS, RHS, &CxtI) ==
355 OverflowResult::NeverOverflows;
356 }
357
358 bool willNotOverflowSub(const Value *LHS, const Value *RHS,
359 const Instruction &CxtI, bool IsSigned) const {
360 return IsSigned ? willNotOverflowSignedSub(LHS, RHS, CxtI)
361 : willNotOverflowUnsignedSub(LHS, RHS, CxtI);
362 }
363
364 bool willNotOverflowSignedMul(const Value *LHS, const Value *RHS,
365 const Instruction &CxtI) const {
366 return computeOverflowForSignedMul(LHS, RHS, &CxtI) ==
367 OverflowResult::NeverOverflows;
368 }
369
370 bool willNotOverflowUnsignedMul(const Value *LHS, const Value *RHS,
371 const Instruction &CxtI,
372 bool IsNSW = false) const {
373 return computeOverflowForUnsignedMul(LHS, RHS, &CxtI, IsNSW) ==
374 OverflowResult::NeverOverflows;
375 }
376
377 bool willNotOverflowMul(const Value *LHS, const Value *RHS,
378 const Instruction &CxtI, bool IsSigned) const {
379 return IsSigned ? willNotOverflowSignedMul(LHS, RHS, CxtI)
380 : willNotOverflowUnsignedMul(LHS, RHS, CxtI);
381 }
382
383 bool willNotOverflow(BinaryOperator::BinaryOps Opcode, const Value *LHS,
384 const Value *RHS, const Instruction &CxtI,
385 bool IsSigned) const {
386 switch (Opcode) {
387 case Instruction::Add: return willNotOverflowAdd(LHS, RHS, CxtI, IsSigned);
388 case Instruction::Sub: return willNotOverflowSub(LHS, RHS, CxtI, IsSigned);
389 case Instruction::Mul: return willNotOverflowMul(LHS, RHS, CxtI, IsSigned);
390 default: llvm_unreachable("Unexpected opcode for overflow query");
391 }
392 }
393
394 Value *EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP = false);
395 /// Emit sum of multiple GEP offsets. The GEPs are processed in reverse
396 /// order.
397 Value *EmitGEPOffsets(ArrayRef<GEPOperator *> GEPs, GEPNoWrapFlags NW,
398 Type *IdxTy, bool RewriteGEPs);
399 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
400 Instruction *foldBitcastExtElt(ExtractElementInst &ExtElt);
401 Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
402 Instruction *foldFBinOpOfIntCasts(BinaryOperator &I);
403 // Should only be called by `foldFBinOpOfIntCasts`.
404 Instruction *foldFBinOpOfIntCastsFromSign(
405 BinaryOperator &BO, bool OpsFromSigned, std::array<Value *, 2> IntOps,
406 Constant *Op1FpC, SmallVectorImpl<WithCache<const Value *>> &OpsKnown);
407 Instruction *foldBinopOfSextBoolToSelect(BinaryOperator &I);
408 Instruction *narrowBinOp(TruncInst &Trunc);
409 Instruction *narrowMaskedBinOp(BinaryOperator &And);
410 Instruction *narrowMathIfNoOverflow(BinaryOperator &I);
411 Instruction *narrowFunnelShift(TruncInst &Trunc);
412 Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
413 Instruction *matchSAddSubSat(IntrinsicInst &MinMax1);
414 Instruction *foldNot(BinaryOperator &I);
415 Instruction *foldBinOpOfDisplacedShifts(BinaryOperator &I);
416
417 /// Determine if a pair of casts can be replaced by a single cast.
418 ///
419 /// \param CI1 The first of a pair of casts.
420 /// \param CI2 The second of a pair of casts.
421 ///
422 /// \return 0 if the cast pair cannot be eliminated, otherwise returns an
423 /// Instruction::CastOps value for a cast that can replace the pair, casting
424 /// CI1->getSrcTy() to CI2->getDstTy().
425 ///
426 /// \see CastInst::isEliminableCastPair
427 Instruction::CastOps isEliminableCastPair(const CastInst *CI1,
428 const CastInst *CI2);
429 Value *simplifyIntToPtrRoundTripCast(Value *Val);
430
431 Value *foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &I,
432 bool IsAnd, bool IsLogical = false);
433 Value *foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS, BinaryOperator &Xor);
434
435 Value *foldEqOfParts(Value *Cmp0, Value *Cmp1, bool IsAnd);
436
437 Value *foldAndOrOfICmpsUsingRanges(ICmpInst *ICmp1, ICmpInst *ICmp2,
438 bool IsAnd);
439
440 /// Optimize (fcmp)&(fcmp) or (fcmp)|(fcmp).
441 /// NOTE: Unlike most of instcombine, this returns a Value which should
442 /// already be inserted into the function.
443 Value *foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS, bool IsAnd,
444 bool IsLogicalSelect = false);
445
446 Instruction *foldLogicOfIsFPClass(BinaryOperator &Operator, Value *LHS,
447 Value *RHS);
448
449 Value *foldBooleanAndOr(Value *LHS, Value *RHS, Instruction &I, bool IsAnd,
450 bool IsLogical);
451
452 Value *reassociateBooleanAndOr(Value *LHS, Value *X, Value *Y, Instruction &I,
453 bool IsAnd, bool RHSIsLogical);
454
455 Value *foldDisjointOr(Value *LHS, Value *RHS);
456
457 Value *reassociateDisjointOr(Value *LHS, Value *RHS);
458
459 Instruction *
460 canonicalizeConditionalNegationViaMathToSelect(BinaryOperator &i);
461
462 Value *matchSelectFromAndOr(Value *A, Value *B, Value *C, Value *D,
463 bool InvertFalseVal = false);
464 Value *getSelectCondition(Value *A, Value *B, bool ABIsTheSame);
465
466 Instruction *foldLShrOverflowBit(BinaryOperator &I);
467 Instruction *foldExtractOfOverflowIntrinsic(ExtractValueInst &EV);
468 Instruction *foldIntrinsicWithOverflowCommon(IntrinsicInst *II);
469 Instruction *foldIntrinsicIsFPClass(IntrinsicInst &II);
470 Instruction *foldFPSignBitOps(BinaryOperator &I);
471 Instruction *foldFDivConstantDivisor(BinaryOperator &I);
472
473 // Optimize one of these forms:
474 // and i1 Op, SI / select i1 Op, i1 SI, i1 false (if IsAnd = true)
475 // or i1 Op, SI / select i1 Op, i1 true, i1 SI (if IsAnd = false)
476 // into simplier select instruction using isImpliedCondition.
477 Instruction *foldAndOrOfSelectUsingImpliedCond(Value *Op, SelectInst &SI,
478 bool IsAnd);
479
480 Instruction *hoistFNegAboveFMulFDiv(Value *FNegOp, Instruction &FMFSource);
481
482 /// Simplify \p V given that it is known to be non-null.
483 /// Returns the simplified value if possible, otherwise returns nullptr.
484 /// If \p HasDereferenceable is true, the simplification will not perform
485 /// same object checks.
486 Value *simplifyNonNullOperand(Value *V, bool HasDereferenceable,
487 unsigned Depth = 0);
488
489public:
490 /// Create and insert the idiom we use to indicate a block is unreachable
491 /// without having to rewrite the CFG from within InstCombine.
493 auto &Ctx = InsertAt->getContext();
494 auto *SI = new StoreInst(ConstantInt::getTrue(Ctx),
495 PoisonValue::get(PointerType::getUnqual(Ctx)),
496 /*isVolatile*/ false, Align(1));
497 InsertNewInstWith(SI, InsertAt->getIterator());
498 }
499
500 /// Combiner aware instruction erasure.
501 ///
502 /// When dealing with an instruction that has side effects or produces a void
503 /// value, we can't rely on DCE to delete the instruction. Instead, visit
504 /// methods should return the value returned by this function.
506 LLVM_DEBUG(dbgs() << "IC: ERASE " << I << '\n');
507 assert(I.use_empty() && "Cannot erase instruction that is used!");
509
510 // Make sure that we reprocess all operands now that we reduced their
511 // use counts.
512 SmallVector<Value *> Ops(I.operands());
513 Worklist.remove(&I);
514 DC.removeValue(&I);
515 I.eraseFromParent();
516 for (Value *Op : Ops)
517 Worklist.handleUseCountDecrement(Op);
518 MadeIRChange = true;
519 return nullptr; // Don't do anything with FI
520 }
521
522 OverflowResult computeOverflow(
523 Instruction::BinaryOps BinaryOp, bool IsSigned,
524 Value *LHS, Value *RHS, Instruction *CxtI) const;
525
526 /// Performs a few simplifications for operators which are associative
527 /// or commutative.
528 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
529
530 /// Tries to simplify binary operations which some other binary
531 /// operation distributes over.
532 ///
533 /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
534 /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
535 /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
536 /// value, or null if it didn't simplify.
537 Value *foldUsingDistributiveLaws(BinaryOperator &I);
538
539 /// Tries to simplify add operations using the definition of remainder.
540 ///
541 /// The definition of remainder is X % C = X - (X / C ) * C. The add
542 /// expression X % C0 + (( X / C0 ) % C1) * C0 can be simplified to
543 /// X % (C0 * C1)
544 Value *SimplifyAddWithRemainder(BinaryOperator &I);
545
546 // Binary Op helper for select operations where the expression can be
547 // efficiently reorganized.
548 Value *SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *LHS,
549 Value *RHS);
550
551 // If `I` has operand `(ctpop (not x))`, fold `I` with `(sub nuw nsw
552 // BitWidth(x), (ctpop x))`.
553 Instruction *tryFoldInstWithCtpopWithNot(Instruction *I);
554
555 // (Binop1 (Binop2 (logic_shift X, C), C1), (logic_shift Y, C))
556 // -> (logic_shift (Binop1 (Binop2 X, inv_logic_shift(C1, C)), Y), C)
557 // (Binop1 (Binop2 (logic_shift X, Amt), Mask), (logic_shift Y, Amt))
558 // -> (BinOp (logic_shift (BinOp X, Y)), Mask)
559 Instruction *foldBinOpShiftWithShift(BinaryOperator &I);
560
561 /// Tries to simplify binops of select and cast of the select condition.
562 ///
563 /// (Binop (cast C), (select C, T, F))
564 /// -> (select C, C0, C1)
565 Instruction *foldBinOpOfSelectAndCastOfSelectCondition(BinaryOperator &I);
566
567 /// This tries to simplify binary operations by factorizing out common terms
568 /// (e. g. "(A*B)+(A*C)" -> "A*(B+C)").
569 Value *tryFactorizationFolds(BinaryOperator &I);
570
571 /// Match a select chain which produces one of three values based on whether
572 /// the LHS is less than, equal to, or greater than RHS respectively.
573 /// Return true if we matched a three way compare idiom. The LHS, RHS, Less,
574 /// Equal and Greater values are saved in the matching process and returned to
575 /// the caller.
576 bool matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, Value *&RHS,
577 ConstantInt *&Less, ConstantInt *&Equal,
578 ConstantInt *&Greater);
579
580 /// Attempts to replace I with a simpler value based on the demanded
581 /// bits.
582 Value *SimplifyDemandedUseBits(Instruction *I, const APInt &DemandedMask,
583 KnownBits &Known, const SimplifyQuery &Q,
584 unsigned Depth = 0);
585 using InstCombiner::SimplifyDemandedBits;
586 bool SimplifyDemandedBits(Instruction *I, unsigned Op,
587 const APInt &DemandedMask, KnownBits &Known,
588 const SimplifyQuery &Q,
589 unsigned Depth = 0) override;
590
591 /// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
592 /// bits. It also tries to handle simplifications that can be done based on
593 /// DemandedMask, but without modifying the Instruction.
594 Value *SimplifyMultipleUseDemandedBits(Instruction *I,
595 const APInt &DemandedMask,
596 KnownBits &Known,
597 const SimplifyQuery &Q,
598 unsigned Depth = 0);
599
600 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
601 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
602 Value *simplifyShrShlDemandedBits(
603 Instruction *Shr, const APInt &ShrOp1, Instruction *Shl,
604 const APInt &ShlOp1, const APInt &DemandedMask, KnownBits &Known);
605
606 /// Tries to simplify operands to an integer instruction based on its
607 /// demanded bits.
608 bool SimplifyDemandedInstructionBits(Instruction &Inst);
609 bool SimplifyDemandedInstructionBits(Instruction &Inst, KnownBits &Known);
610
611 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
612 APInt &PoisonElts, unsigned Depth = 0,
613 bool AllowMultipleUsers = false) override;
614
615 /// Attempts to replace V with a simpler value based on the demanded
616 /// floating-point classes
617 Value *SimplifyDemandedUseFPClass(Value *V, FPClassTest DemandedMask,
618 KnownFPClass &Known, Instruction *CxtI,
619 unsigned Depth = 0);
620 bool SimplifyDemandedFPClass(Instruction *I, unsigned Op,
621 FPClassTest DemandedMask, KnownFPClass &Known,
622 unsigned Depth = 0);
623
624 /// Common transforms for add / disjoint or
625 Instruction *foldAddLikeCommutative(Value *LHS, Value *RHS, bool NSW,
626 bool NUW);
627
628 /// Canonicalize the position of binops relative to shufflevector.
629 Instruction *foldVectorBinop(BinaryOperator &Inst);
631 Instruction *foldSelectShuffle(ShuffleVectorInst &Shuf);
632 Constant *unshuffleConstant(ArrayRef<int> ShMask, Constant *C,
633 VectorType *NewCTy);
634
635 /// Given a binary operator, cast instruction, or select which has a PHI node
636 /// as operand #0, see if we can fold the instruction into the PHI (which is
637 /// only possible if all operands to the PHI are constants).
638 Instruction *foldOpIntoPhi(Instruction &I, PHINode *PN,
639 bool AllowMultipleUses = false);
640
641 /// Try to fold binary operators whose operands are simple interleaved
642 /// recurrences to a single recurrence. This is a common pattern in reduction
643 /// operations.
644 /// Example:
645 /// %phi1 = phi [init1, %BB1], [%op1, %BB2]
646 /// %phi2 = phi [init2, %BB1], [%op2, %BB2]
647 /// %op1 = binop %phi1, constant1
648 /// %op2 = binop %phi2, constant2
649 /// %rdx = binop %op1, %op2
650 /// -->
651 /// %phi_combined = phi [init_combined, %BB1], [%op_combined, %BB2]
652 /// %rdx_combined = binop %phi_combined, constant_combined
653 Instruction *foldBinopWithRecurrence(BinaryOperator &BO);
654
655 /// For a binary operator with 2 phi operands, try to hoist the binary
656 /// operation before the phi. This can result in fewer instructions in
657 /// patterns where at least one set of phi operands simplifies.
658 /// Example:
659 /// BB3: binop (phi [X, BB1], [C1, BB2]), (phi [Y, BB1], [C2, BB2])
660 /// -->
661 /// BB1: BO = binop X, Y
662 /// BB3: phi [BO, BB1], [(binop C1, C2), BB2]
663 Instruction *foldBinopWithPhiOperands(BinaryOperator &BO);
664
665 /// Given an instruction with a select as one operand and a constant as the
666 /// other operand, try to fold the binary operator into the select arguments.
667 /// This also works for Cast instructions, which obviously do not have a
668 /// second operand.
669 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
670 bool FoldWithMultiUse = false);
671
672 /// This is a convenience wrapper function for the above two functions.
673 Instruction *foldBinOpIntoSelectOrPhi(BinaryOperator &I);
674
675 Instruction *foldAddWithConstant(BinaryOperator &Add);
676
677 Instruction *foldSquareSumInt(BinaryOperator &I);
678 Instruction *foldSquareSumFP(BinaryOperator &I);
679
680 /// Try to rotate an operation below a PHI node, using PHI nodes for
681 /// its operands.
682 Instruction *foldPHIArgOpIntoPHI(PHINode &PN);
683 Instruction *foldPHIArgBinOpIntoPHI(PHINode &PN);
684 Instruction *foldPHIArgInsertValueInstructionIntoPHI(PHINode &PN);
685 Instruction *foldPHIArgExtractValueInstructionIntoPHI(PHINode &PN);
686 Instruction *foldPHIArgGEPIntoPHI(PHINode &PN);
687 Instruction *foldPHIArgLoadIntoPHI(PHINode &PN);
688 Instruction *foldPHIArgZextsIntoPHI(PHINode &PN);
689 Instruction *foldPHIArgIntToPtrToPHI(PHINode &PN);
690
691 /// If the phi is within a phi web, which is formed by the def-use chain
692 /// of phis and all the phis in the web are only used in the other phis.
693 /// In this case, these phis are dead and we will remove all of them.
694 bool foldDeadPhiWeb(PHINode &PN);
695
696 /// If an integer typed PHI has only one use which is an IntToPtr operation,
697 /// replace the PHI with an existing pointer typed PHI if it exists. Otherwise
698 /// insert a new pointer typed PHI and replace the original one.
699 bool foldIntegerTypedPHI(PHINode &PN);
700
701 /// Helper function for FoldPHIArgXIntoPHI() to set debug location for the
702 /// folded operation.
703 void PHIArgMergedDebugLoc(Instruction *Inst, PHINode &PN);
704
705 Value *foldPtrToIntOfGEP(Type *IntTy, Value *Ptr);
706 Instruction *foldGEPICmp(GEPOperator *GEPLHS, Value *RHS, CmpPredicate Cond,
707 Instruction &I);
708 Instruction *foldSelectICmp(CmpPredicate Pred, SelectInst *SI, Value *RHS,
709 const ICmpInst &I);
710 bool foldAllocaCmp(AllocaInst *Alloca);
711 Instruction *foldCmpLoadFromIndexedGlobal(LoadInst *LI,
713 GlobalVariable *GV, CmpInst &ICI,
714 ConstantInt *AndCst = nullptr);
715 Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI,
716 Constant *RHSC);
717 Instruction *foldICmpAddOpConst(Value *X, const APInt &C, CmpPredicate Pred);
718 Instruction *foldICmpWithCastOp(ICmpInst &ICmp);
719 Instruction *foldICmpWithZextOrSext(ICmpInst &ICmp);
720
721 Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp);
723 Instruction *foldICmpWithConstant(ICmpInst &Cmp);
724 Instruction *foldIsMultipleOfAPowerOfTwo(ICmpInst &Cmp);
725 Instruction *foldICmpUsingBoolRange(ICmpInst &I);
726 Instruction *foldICmpInstWithConstant(ICmpInst &Cmp);
727 Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp);
728 Instruction *foldICmpInstWithConstantAllowPoison(ICmpInst &Cmp,
729 const APInt &C);
730 Instruction *foldICmpBinOp(ICmpInst &Cmp, const SimplifyQuery &SQ);
731 Instruction *foldICmpWithMinMax(Instruction &I, MinMaxIntrinsic *MinMax,
732 Value *Z, CmpPredicate Pred);
733 Instruction *foldICmpEquality(ICmpInst &Cmp);
734 Instruction *foldIRemByPowerOfTwoToBitTest(ICmpInst &I);
735 Instruction *foldSignBitTest(ICmpInst &I);
736 Instruction *foldICmpWithZero(ICmpInst &Cmp);
737
738 Value *foldMultiplicationOverflowCheck(ICmpInst &Cmp);
739
740 Instruction *foldICmpBinOpWithConstant(ICmpInst &Cmp, BinaryOperator *BO,
741 const APInt &C);
742 Instruction *foldICmpSelectConstant(ICmpInst &Cmp, SelectInst *Select,
743 ConstantInt *C);
744 Instruction *foldICmpTruncConstant(ICmpInst &Cmp, TruncInst *Trunc,
745 const APInt &C);
746 Instruction *foldICmpTruncWithTruncOrExt(ICmpInst &Cmp,
747 const SimplifyQuery &Q);
748 Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And,
749 const APInt &C);
750 Instruction *foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor,
751 const APInt &C);
752 Instruction *foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or,
753 const APInt &C);
754 Instruction *foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul,
755 const APInt &C);
756 Instruction *foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl,
757 const APInt &C);
758 Instruction *foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr,
759 const APInt &C);
760 Instruction *foldICmpSRemConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
761 const APInt &C);
762 Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
763 const APInt &C);
764 Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div,
765 const APInt &C);
766 Instruction *foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub,
767 const APInt &C);
768 Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add,
769 const APInt &C);
770 Instruction *foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And,
771 const APInt &C1);
772 Instruction *foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And,
773 const APInt &C1, const APInt &C2);
774 Instruction *foldICmpXorShiftConst(ICmpInst &Cmp, BinaryOperator *Xor,
775 const APInt &C);
776 Instruction *foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
777 const APInt &C2);
778 Instruction *foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
779 const APInt &C2);
780
781 Instruction *foldICmpBinOpWithConstantViaTruthTable(ICmpInst &Cmp,
782 BinaryOperator *BO,
783 const APInt &C);
784 Instruction *foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp,
785 BinaryOperator *BO,
786 const APInt &C);
787 Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II,
788 const APInt &C);
789 Instruction *foldICmpEqIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II,
790 const APInt &C);
791 Instruction *foldICmpBitCast(ICmpInst &Cmp);
792 Instruction *foldICmpWithTrunc(ICmpInst &Cmp);
793 Instruction *foldICmpCommutative(CmpPredicate Pred, Value *Op0, Value *Op1,
794 ICmpInst &CxtI);
795
796 // Helpers of visitSelectInst().
801 Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
802 Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *);
804 Value *A, Value *B, Instruction &Outer,
808 Value *FalseVal);
811 unsigned Depth = 0);
812
813 Value *insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi,
814 bool isSigned, bool Inside);
815 bool mergeStoreIntoSuccessor(StoreInst &SI);
816
817 /// Given an initial instruction, check to see if it is the root of a
818 /// bswap/bitreverse idiom. If so, return the equivalent bswap/bitreverse
819 /// intrinsic.
820 Instruction *matchBSwapOrBitReverse(Instruction &I, bool MatchBSwaps,
821 bool MatchBitReversals);
822
823 Instruction *SimplifyAnyMemTransfer(AnyMemTransferInst *MI);
824 Instruction *SimplifyAnyMemSet(AnyMemSetInst *MI);
825
826 Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
827
828 bool tryToSinkInstruction(Instruction *I, BasicBlock *DestBlock);
829 void tryToSinkInstructionDbgVariableRecords(
830 Instruction *I, BasicBlock::iterator InsertPos, BasicBlock *SrcBlock,
832
833 bool removeInstructionsBeforeUnreachable(Instruction &I);
834 void addDeadEdge(BasicBlock *From, BasicBlock *To,
836 void handleUnreachableFrom(Instruction *I,
838 void handlePotentiallyDeadBlocks(SmallVectorImpl<BasicBlock *> &Worklist);
839 void handlePotentiallyDeadSuccessors(BasicBlock *BB, BasicBlock *LiveSucc);
840 void freelyInvertAllUsersOf(Value *V, Value *IgnoredUser = nullptr);
841
842 /// Take the exact integer log2 of the value. If DoFold is true, create the
843 /// actual instructions, otherwise return a non-null dummy value. Return
844 /// nullptr on failure. Note, if DoFold is true the caller must ensure that
845 /// takeLog2 will succeed, otherwise it may create stray instructions.
846 Value *takeLog2(Value *Op, unsigned Depth, bool AssumeNonZero, bool DoFold);
847
848 Value *tryGetLog2(Value *Op, bool AssumeNonZero) {
849 if (takeLog2(Op, /*Depth=*/0, AssumeNonZero, /*DoFold=*/false))
850 return takeLog2(Op, /*Depth=*/0, AssumeNonZero, /*DoFold=*/true);
851 return nullptr;
852 }
853};
854
855class Negator final {
856 /// Top-to-bottom, def-to-use negated instruction tree we produced.
858
860 BuilderTy Builder;
861
862 const DominatorTree &DT;
863
864 const bool IsTrulyNegation;
865
866 SmallDenseMap<Value *, Value *> NegationsCache;
867
868 Negator(LLVMContext &C, const DataLayout &DL, const DominatorTree &DT,
869 bool IsTrulyNegation);
870
871#if LLVM_ENABLE_STATS
872 unsigned NumValuesVisitedInThisNegator = 0;
873 ~Negator();
874#endif
875
876 using Result = std::pair<ArrayRef<Instruction *> /*NewInstructions*/,
877 Value * /*NegatedRoot*/>;
878
879 std::array<Value *, 2> getSortedOperandsOfBinOp(Instruction *I);
880
881 [[nodiscard]] Value *visitImpl(Value *V, bool IsNSW, unsigned Depth);
882
883 [[nodiscard]] Value *negate(Value *V, bool IsNSW, unsigned Depth);
884
885 /// Recurse depth-first and attempt to sink the negation.
886 /// FIXME: use worklist?
887 [[nodiscard]] std::optional<Result> run(Value *Root, bool IsNSW);
888
889 Negator(const Negator &) = delete;
890 Negator(Negator &&) = delete;
891 Negator &operator=(const Negator &) = delete;
892 Negator &operator=(Negator &&) = delete;
893
894public:
895 /// Attempt to negate \p Root. Retuns nullptr if negation can't be performed,
896 /// otherwise returns negated value.
897 [[nodiscard]] static Value *Negate(bool LHSIsZero, bool IsNSW, Value *Root,
898 InstCombinerImpl &IC);
899};
900
902 /// Common base pointer.
903 Value *Ptr = nullptr;
904 /// LHS GEPs until common base.
906 /// RHS GEPs until common base.
908 /// LHS GEP NoWrapFlags until common base.
910 /// RHS GEP NoWrapFlags until common base.
912
914
915 /// Whether expanding the GEP chains is expensive.
916 bool isExpensive() const;
917};
918
919} // end namespace llvm
920
921#undef DEBUG_TYPE
922
923#endif // LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
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< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
RelocType Type
Definition: COFFYAML.cpp:410
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)
uint64_t Align
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 F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
const uint64_t BitWidth
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
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:119
static OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const AddOperator *Add, const SimplifyQuery &SQ)
Value * RHS
Value * LHS
BinaryOperator * Mul
support::ulittle16_t & Lo
Definition: aarch32.cpp:205
support::ulittle16_t & Hi
Definition: aarch32.cpp:204
static const uint32_t IV[8]
Definition: blake3_impl.h:83
A private abstract base class describing the concept of an individual alias analysis implementation.
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
Definition: Instructions.h:64
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,...
Definition: Instructions.h:709
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...
Definition: InstrTypes.h:1116
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...
Definition: CmpPredicate.h:23
This is the shared class of boolean and integer constants.
Definition: Constants.h:87
This is an important base class in LLVM.
Definition: Constant.h:43
This class represents an Operation in the Expression.
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.
Definition: Instructions.h:429
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
Definition: Instructions.h:949
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.
Instruction * foldSelectToCmp(SelectInst &SI)
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).
virtual ~InstCombinerImpl()=default
KnownFPClass computeKnownFPClass(Value *Val, FastMathFlags FMF, FPClassTest Interested=fcAllFlags, const Instruction *CtxI=nullptr, unsigned Depth=0) const
Instruction * foldSelectEqualityTest(SelectInst &SI)
Instruction * foldSelectValueEquivalence(SelectInst &SI, CmpInst &CI)
InstCombinerImpl(InstructionWorklist &Worklist, BuilderTy &Builder, bool MinimizeSize, AAResults *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, TargetTransformInfo &TTI, DominatorTree &DT, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI, const DataLayout &DL, ReversePostOrderTraversal< BasicBlock * > &RPOT)
Instruction * foldVectorSelect(SelectInst &Sel)
Instruction * foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1, Value *A, Value *B, Instruction &Outer, SelectPatternFlavor SPF2, Value *C)
Constant * getLosslessUnsignedTrunc(Constant *C, Type *TruncTy)
bool replaceInInstruction(Value *V, Value *Old, Value *New, unsigned Depth=0)
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Value * tryGetLog2(Value *Op, bool AssumeNonZero)
Instruction * foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI)
Constant * getLosslessTrunc(Constant *C, Type *TruncTy, unsigned ExtOp)
Instruction * visitInstruction(Instruction &I)
Specify what to return for unhandled instructions.
KnownFPClass computeKnownFPClass(Value *Val, FPClassTest Interested=fcAllFlags, const Instruction *CtxI=nullptr, unsigned Depth=0) const
Constant * getLosslessSignedTrunc(Constant *C, Type *TruncTy)
Value * foldSelectWithConstOpToBinOp(ICmpInst *Cmp, Value *TrueVal, Value *FalseVal)
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:48
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:49
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.
Definition: Instructions.h:180
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.
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...
Definition: SmallVector.h:574
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
An instruction for storing to memory.
Definition: Instructions.h:296
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:1098
Base class of all SIMD vector types.
Definition: DerivedTypes.h:430
This class represents zero extension of integer types.
self_iterator getIterator()
Definition: ilist_node.h:134
#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
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:1723
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)
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
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.