LLVM 18.0.0git
InstCombiner.h
Go to the documentation of this file.
1//===- InstCombiner.h - InstCombine implementation --------------*- 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/// \file
9///
10/// This file provides the interface for the instcombine pass implementation.
11/// The interface is used for generic transformations in this folder and
12/// target specific combinations in the targets.
13/// The visitor implementation is in \c InstCombinerImpl in
14/// \c InstCombineInternal.h.
15///
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINER_H
19#define LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINER_H
20
24#include "llvm/IR/IRBuilder.h"
26#include "llvm/Support/Debug.h"
28#include <cassert>
29
30#define DEBUG_TYPE "instcombine"
32
33namespace llvm {
34
35class AAResults;
36class AssumptionCache;
37class OptimizationRemarkEmitter;
38class ProfileSummaryInfo;
39class TargetLibraryInfo;
40class TargetTransformInfo;
41
42/// The core instruction combiner logic.
43///
44/// This class provides both the logic to recursively visit instructions and
45/// combine them.
47 /// Only used to call target specific intrinsic combining.
48 /// It must **NOT** be used for any other purpose, as InstCombine is a
49 /// target-independent canonicalization transform.
51
52public:
53 /// Maximum size of array considered when transforming.
54 uint64_t MaxArraySizeForCombine = 0;
55
56 /// An IRBuilder that automatically inserts new instructions into the
57 /// worklist.
60
61protected:
62 /// A worklist of the instructions that need to be simplified.
64
65 // Mode in which we are running the combiner.
66 const bool MinimizeSize;
67
69
70 // Required analyses.
74 const DataLayout &DL;
79
80 // Optional analyses. When non-null, these can both be used to do better
81 // combining and will be updated to reflect any changes.
83
84 bool MadeIRChange = false;
85
86 /// Edges that are known to never be taken.
88
89 /// Order of predecessors to canonicalize phi nodes towards.
91
92public:
94 bool MinimizeSize, AAResults *AA, AssumptionCache &AC,
98 const DataLayout &DL, LoopInfo *LI)
99 : TTI(TTI), Builder(Builder), Worklist(Worklist),
100 MinimizeSize(MinimizeSize), AA(AA), AC(AC), TLI(TLI), DT(DT), DL(DL),
101 SQ(DL, &TLI, &DT, &AC), ORE(ORE), BFI(BFI), PSI(PSI), LI(LI) {}
102
103 virtual ~InstCombiner() = default;
104
105 /// Return the source operand of a potentially bitcasted value while
106 /// optionally checking if it has one use. If there is no bitcast or the one
107 /// use check is not met, return the input value itself.
108 static Value *peekThroughBitcast(Value *V, bool OneUseOnly = false) {
109 if (auto *BitCast = dyn_cast<BitCastInst>(V))
110 if (!OneUseOnly || BitCast->hasOneUse())
111 return BitCast->getOperand(0);
112
113 // V is not a bitcast or V has more than one use and OneUseOnly is true.
114 return V;
115 }
116
117 /// Assign a complexity or rank value to LLVM Values. This is used to reduce
118 /// the amount of pattern matching needed for compares and commutative
119 /// instructions. For example, if we have:
120 /// icmp ugt X, Constant
121 /// or
122 /// xor (add X, Constant), cast Z
123 ///
124 /// We do not have to consider the commuted variants of these patterns because
125 /// canonicalization based on complexity guarantees the above ordering.
126 ///
127 /// This routine maps IR values to various complexity ranks:
128 /// 0 -> undef
129 /// 1 -> Constants
130 /// 2 -> Other non-instructions
131 /// 3 -> Arguments
132 /// 4 -> Cast and (f)neg/not instructions
133 /// 5 -> Other instructions
134 static unsigned getComplexity(Value *V) {
135 if (isa<Instruction>(V)) {
136 if (isa<CastInst>(V) || match(V, m_Neg(PatternMatch::m_Value())) ||
137 match(V, m_Not(PatternMatch::m_Value())) ||
138 match(V, m_FNeg(PatternMatch::m_Value())))
139 return 4;
140 return 5;
141 }
142 if (isa<Argument>(V))
143 return 3;
144 return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
145 }
146
147 /// Predicate canonicalization reduces the number of patterns that need to be
148 /// matched by other transforms. For example, we may swap the operands of a
149 /// conditional branch or select to create a compare with a canonical
150 /// (inverted) predicate which is then more likely to be matched with other
151 /// values.
153 switch (Pred) {
154 case CmpInst::ICMP_NE:
155 case CmpInst::ICMP_ULE:
156 case CmpInst::ICMP_SLE:
157 case CmpInst::ICMP_UGE:
158 case CmpInst::ICMP_SGE:
159 // TODO: There are 16 FCMP predicates. Should others be (not) canonical?
160 case CmpInst::FCMP_ONE:
161 case CmpInst::FCMP_OLE:
162 case CmpInst::FCMP_OGE:
163 return false;
164 default:
165 return true;
166 }
167 }
168
169 /// Given an exploded icmp instruction, return true if the comparison only
170 /// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
171 /// the result of the comparison is true when the input value is signed.
172 static bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,
173 bool &TrueIfSigned) {
174 switch (Pred) {
175 case ICmpInst::ICMP_SLT: // True if LHS s< 0
176 TrueIfSigned = true;
177 return RHS.isZero();
178 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
179 TrueIfSigned = true;
180 return RHS.isAllOnes();
181 case ICmpInst::ICMP_SGT: // True if LHS s> -1
182 TrueIfSigned = false;
183 return RHS.isAllOnes();
184 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
185 TrueIfSigned = false;
186 return RHS.isZero();
187 case ICmpInst::ICMP_UGT:
188 // True if LHS u> RHS and RHS == sign-bit-mask - 1
189 TrueIfSigned = true;
190 return RHS.isMaxSignedValue();
191 case ICmpInst::ICMP_UGE:
192 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
193 TrueIfSigned = true;
194 return RHS.isMinSignedValue();
195 case ICmpInst::ICMP_ULT:
196 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
197 TrueIfSigned = false;
198 return RHS.isMinSignedValue();
199 case ICmpInst::ICMP_ULE:
200 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
201 TrueIfSigned = false;
202 return RHS.isMaxSignedValue();
203 default:
204 return false;
205 }
206 }
207
208 /// Add one to a Constant
210 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
211 }
212
213 /// Subtract one from a Constant
215 return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
216 }
217
218 std::optional<std::pair<
220 Constant *>> static getFlippedStrictnessPredicateAndConstant(CmpInst::
221 Predicate
222 Pred,
223 Constant *C);
224
226 // a ? b : false and a ? true : b are the canonical form of logical and/or.
227 // This includes !a ? b : false and !a ? true : b. Absorbing the not into
228 // the select by swapping operands would break recognition of this pattern
229 // in other analyses, so don't do that.
230 return match(&SI, PatternMatch::m_LogicalAnd(PatternMatch::m_Value(),
231 PatternMatch::m_Value())) ||
232 match(&SI, PatternMatch::m_LogicalOr(PatternMatch::m_Value(),
233 PatternMatch::m_Value()));
234 }
235
236 /// Return nonnull value if V is free to invert under the condition of
237 /// WillInvertAllUses.
238 /// If Builder is nonnull, it will return a simplified ~V.
239 /// If Builder is null, it will return an arbitrary nonnull value (not
240 /// dereferenceable).
241 /// If the inversion will consume instructions, `DoesConsume` will be set to
242 /// true. Otherwise it will be false.
243 Value *getFreelyInvertedImpl(Value *V, bool WillInvertAllUses,
244 BuilderTy *Builder, bool &DoesConsume,
245 unsigned Depth);
246
247 Value *getFreelyInverted(Value *V, bool WillInvertAllUses,
248 BuilderTy *Builder, bool &DoesConsume) {
249 DoesConsume = false;
250 return getFreelyInvertedImpl(V, WillInvertAllUses, Builder, DoesConsume,
251 /*Depth*/ 0);
252 }
253
254 Value *getFreelyInverted(Value *V, bool WillInvertAllUses,
255 BuilderTy *Builder) {
256 bool Unused;
257 return getFreelyInverted(V, WillInvertAllUses, Builder, Unused);
258 }
259
260 /// Return true if the specified value is free to invert (apply ~ to).
261 /// This happens in cases where the ~ can be eliminated. If WillInvertAllUses
262 /// is true, work under the assumption that the caller intends to remove all
263 /// uses of V and only keep uses of ~V.
264 ///
265 /// See also: canFreelyInvertAllUsersOf()
266 bool isFreeToInvert(Value *V, bool WillInvertAllUses,
267 bool &DoesConsume) {
268 return getFreelyInverted(V, WillInvertAllUses, /*Builder*/ nullptr,
269 DoesConsume) != nullptr;
270 }
271
272 bool isFreeToInvert(Value *V, bool WillInvertAllUses) {
273 bool Unused;
274 return isFreeToInvert(V, WillInvertAllUses, Unused);
275 }
276
277 /// Given i1 V, can every user of V be freely adapted if V is changed to !V ?
278 /// InstCombine's freelyInvertAllUsersOf() must be kept in sync with this fn.
279 /// NOTE: for Instructions only!
280 ///
281 /// See also: isFreeToInvert()
283 // Look at every user of V.
284 for (Use &U : V->uses()) {
285 if (U.getUser() == IgnoredUser)
286 continue; // Don't consider this user.
287
288 auto *I = cast<Instruction>(U.getUser());
289 switch (I->getOpcode()) {
290 case Instruction::Select:
291 if (U.getOperandNo() != 0) // Only if the value is used as select cond.
292 return false;
293 if (shouldAvoidAbsorbingNotIntoSelect(*cast<SelectInst>(I)))
294 return false;
295 break;
296 case Instruction::Br:
297 assert(U.getOperandNo() == 0 && "Must be branching on that value.");
298 break; // Free to invert by swapping true/false values/destinations.
299 case Instruction::Xor: // Can invert 'xor' if it's a 'not', by ignoring
300 // it.
301 if (!match(I, m_Not(PatternMatch::m_Value())))
302 return false; // Not a 'not'.
303 break;
304 default:
305 return false; // Don't know, likely not freely invertible.
306 }
307 // So far all users were free to invert...
308 }
309 return true; // Can freely invert all users!
310 }
311
312 /// Some binary operators require special handling to avoid poison and
313 /// undefined behavior. If a constant vector has undef elements, replace those
314 /// undefs with identity constants if possible because those are always safe
315 /// to execute. If no identity constant exists, replace undef with some other
316 /// safe constant.
317 static Constant *
319 bool IsRHSConstant) {
320 auto *InVTy = cast<FixedVectorType>(In->getType());
321
322 Type *EltTy = InVTy->getElementType();
323 auto *SafeC = ConstantExpr::getBinOpIdentity(Opcode, EltTy, IsRHSConstant);
324 if (!SafeC) {
325 // TODO: Should this be available as a constant utility function? It is
326 // similar to getBinOpAbsorber().
327 if (IsRHSConstant) {
328 switch (Opcode) {
329 case Instruction::SRem: // X % 1 = 0
330 case Instruction::URem: // X %u 1 = 0
331 SafeC = ConstantInt::get(EltTy, 1);
332 break;
333 case Instruction::FRem: // X % 1.0 (doesn't simplify, but it is safe)
334 SafeC = ConstantFP::get(EltTy, 1.0);
335 break;
336 default:
338 "Only rem opcodes have no identity constant for RHS");
339 }
340 } else {
341 switch (Opcode) {
342 case Instruction::Shl: // 0 << X = 0
343 case Instruction::LShr: // 0 >>u X = 0
344 case Instruction::AShr: // 0 >> X = 0
345 case Instruction::SDiv: // 0 / X = 0
346 case Instruction::UDiv: // 0 /u X = 0
347 case Instruction::SRem: // 0 % X = 0
348 case Instruction::URem: // 0 %u X = 0
349 case Instruction::Sub: // 0 - X (doesn't simplify, but it is safe)
350 case Instruction::FSub: // 0.0 - X (doesn't simplify, but it is safe)
351 case Instruction::FDiv: // 0.0 / X (doesn't simplify, but it is safe)
352 case Instruction::FRem: // 0.0 % X = 0
353 SafeC = Constant::getNullValue(EltTy);
354 break;
355 default:
356 llvm_unreachable("Expected to find identity constant for opcode");
357 }
358 }
359 }
360 assert(SafeC && "Must have safe constant for binop");
361 unsigned NumElts = InVTy->getNumElements();
362 SmallVector<Constant *, 16> Out(NumElts);
363 for (unsigned i = 0; i != NumElts; ++i) {
364 Constant *C = In->getAggregateElement(i);
365 Out[i] = isa<UndefValue>(C) ? SafeC : C;
366 }
367 return ConstantVector::get(Out);
368 }
369
370 void addToWorklist(Instruction *I) { Worklist.push(I); }
371
372 AssumptionCache &getAssumptionCache() const { return AC; }
374 DominatorTree &getDominatorTree() const { return DT; }
375 const DataLayout &getDataLayout() const { return DL; }
376 const SimplifyQuery &getSimplifyQuery() const { return SQ; }
378 return ORE;
379 }
382 LoopInfo *getLoopInfo() const { return LI; }
383
384 // Call target specific combiners
385 std::optional<Instruction *> targetInstCombineIntrinsic(IntrinsicInst &II);
386 std::optional<Value *>
387 targetSimplifyDemandedUseBitsIntrinsic(IntrinsicInst &II, APInt DemandedMask,
388 KnownBits &Known,
389 bool &KnownBitsComputed);
390 std::optional<Value *> targetSimplifyDemandedVectorEltsIntrinsic(
391 IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts,
392 APInt &UndefElts2, APInt &UndefElts3,
393 std::function<void(Instruction *, unsigned, APInt, APInt &)>
394 SimplifyAndSetOp);
395
396 /// Inserts an instruction \p New before instruction \p Old
397 ///
398 /// Also adds the new instruction to the worklist and returns \p New so that
399 /// it is suitable for use as the return from the visitation patterns.
401 assert(New && !New->getParent() &&
402 "New instruction already inserted into a basic block!");
403 New->insertBefore(Old); // Insert inst
404 Worklist.add(New);
405 return New;
406 }
407
408 /// Same as InsertNewInstBefore, but also sets the debug loc.
410 New->setDebugLoc(Old->getDebugLoc());
411 return InsertNewInstBefore(New, Old);
412 }
413
414 /// A combiner-aware RAUW-like routine.
415 ///
416 /// This method is to be used when an instruction is found to be dead,
417 /// replaceable with another preexisting expression. Here we add all uses of
418 /// I to the worklist, replace all uses of I with the new value, then return
419 /// I, so that the inst combiner will know that I was modified.
421 // If there are no uses to replace, then we return nullptr to indicate that
422 // no changes were made to the program.
423 if (I.use_empty()) return nullptr;
424
425 Worklist.pushUsersToWorkList(I); // Add all modified instrs to worklist.
426
427 // If we are replacing the instruction with itself, this must be in a
428 // segment of unreachable code, so just clobber the instruction.
429 if (&I == V)
430 V = PoisonValue::get(I.getType());
431
432 LLVM_DEBUG(dbgs() << "IC: Replacing " << I << "\n"
433 << " with " << *V << '\n');
434
435 // If V is a new unnamed instruction, take the name from the old one.
436 if (V->use_empty() && isa<Instruction>(V) && !V->hasName() && I.hasName())
437 V->takeName(&I);
438
439 I.replaceAllUsesWith(V);
440 return &I;
441 }
442
443 /// Replace operand of instruction and add old operand to the worklist.
445 Value *OldOp = I.getOperand(OpNum);
446 I.setOperand(OpNum, V);
447 Worklist.handleUseCountDecrement(OldOp);
448 return &I;
449 }
450
451 /// Replace use and add the previously used value to the worklist.
452 void replaceUse(Use &U, Value *NewValue) {
453 Value *OldOp = U;
454 U = NewValue;
455 Worklist.handleUseCountDecrement(OldOp);
456 }
457
458 /// Combiner aware instruction erasure.
459 ///
460 /// When dealing with an instruction that has side effects or produces a void
461 /// value, we can't rely on DCE to delete the instruction. Instead, visit
462 /// methods should return the value returned by this function.
464
465 void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
466 const Instruction *CxtI) const {
468 }
469
470 KnownBits computeKnownBits(const Value *V, unsigned Depth,
471 const Instruction *CxtI) const {
473 }
474
475 bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero = false,
476 unsigned Depth = 0,
477 const Instruction *CxtI = nullptr) {
478 return llvm::isKnownToBeAPowerOfTwo(V, DL, OrZero, Depth, &AC, CxtI, &DT);
479 }
480
481 bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth = 0,
482 const Instruction *CxtI = nullptr) const {
483 return llvm::MaskedValueIsZero(V, Mask, SQ.getWithInstruction(CxtI), Depth);
484 }
485
486 unsigned ComputeNumSignBits(const Value *Op, unsigned Depth = 0,
487 const Instruction *CxtI = nullptr) const {
488 return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT);
489 }
490
491 unsigned ComputeMaxSignificantBits(const Value *Op, unsigned Depth = 0,
492 const Instruction *CxtI = nullptr) const {
493 return llvm::ComputeMaxSignificantBits(Op, DL, Depth, &AC, CxtI, &DT);
494 }
495
497 const Value *RHS,
498 const Instruction *CxtI) const {
500 SQ.getWithInstruction(CxtI));
501 }
502
504 const Instruction *CxtI) const {
506 SQ.getWithInstruction(CxtI));
507 }
508
512 const Instruction *CxtI) const {
514 SQ.getWithInstruction(CxtI));
515 }
516
520 const Instruction *CxtI) const {
522 SQ.getWithInstruction(CxtI));
523 }
524
526 const Value *RHS,
527 const Instruction *CxtI) const {
529 SQ.getWithInstruction(CxtI));
530 }
531
533 const Instruction *CxtI) const {
535 SQ.getWithInstruction(CxtI));
536 }
537
538 virtual bool SimplifyDemandedBits(Instruction *I, unsigned OpNo,
539 const APInt &DemandedMask, KnownBits &Known,
540 unsigned Depth = 0) = 0;
541 virtual Value *
542 SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &UndefElts,
543 unsigned Depth = 0,
544 bool AllowMultipleUsers = false) = 0;
545
546 bool isValidAddrSpaceCast(unsigned FromAS, unsigned ToAS) const;
547};
548
549} // namespace llvm
550
551#undef DEBUG_TYPE
552
553#endif
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
IRBuilder< TargetFolder > BuilderTy
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_LIBRARY_VISIBILITY
Definition: Compiler.h:131
#define LLVM_DEBUG(X)
Definition: Debug.h:101
#define I(x, y, z)
Definition: MD5.cpp:58
StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * RHS
Value * LHS
static constexpr uint32_t Opcode
Definition: aarch32.h:200
Class for arbitrary precision integers.
Definition: APInt.h:76
A cache of @llvm.assume calls within a function.
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:173
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:738
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:748
This is an important base class in LLVM.
Definition: Constant.h:41
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:110
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:164
The core instruction combiner logic.
Definition: InstCombiner.h:46
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:532
const DataLayout & getDataLayout() const
Definition: InstCombiner.h:375
static bool isCanonicalPredicate(CmpInst::Predicate Pred)
Predicate canonicalization reduces the number of patterns that need to be matched by other transforms...
Definition: InstCombiner.h:152
bool isFreeToInvert(Value *V, bool WillInvertAllUses)
Definition: InstCombiner.h:272
virtual Instruction * eraseInstFromFunction(Instruction &I)=0
Combiner aware instruction erasure.
bool isFreeToInvert(Value *V, bool WillInvertAllUses, bool &DoesConsume)
Return true if the specified value is free to invert (apply ~ to).
Definition: InstCombiner.h:266
DominatorTree & getDominatorTree() const
Definition: InstCombiner.h:374
virtual ~InstCombiner()=default
LoopInfo * getLoopInfo() const
Definition: InstCombiner.h:382
BlockFrequencyInfo * BFI
Definition: InstCombiner.h:77
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
Definition: InstCombiner.h:134
SmallDenseMap< BasicBlock *, SmallVector< BasicBlock * >, 8 > PredOrder
Order of predecessors to canonicalize phi nodes towards.
Definition: InstCombiner.h:90
TargetLibraryInfo & TLI
Definition: InstCombiner.h:72
TargetLibraryInfo & getTargetLibraryInfo() const
Definition: InstCombiner.h:373
BlockFrequencyInfo * getBlockFrequencyInfo() const
Definition: InstCombiner.h:380
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, unsigned Depth=0, const Instruction *CxtI=nullptr)
Definition: InstCombiner.h:475
Instruction * InsertNewInstBefore(Instruction *New, BasicBlock::iterator Old)
Inserts an instruction New before instruction Old.
Definition: InstCombiner.h:400
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:496
AAResults * AA
Definition: InstCombiner.h:68
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Definition: InstCombiner.h:420
static bool shouldAvoidAbsorbingNotIntoSelect(const SelectInst &SI)
Definition: InstCombiner.h:225
OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:518
static Constant * SubOne(Constant *C)
Subtract one from a Constant.
Definition: InstCombiner.h:214
virtual bool SimplifyDemandedBits(Instruction *I, unsigned OpNo, const APInt &DemandedMask, KnownBits &Known, unsigned Depth=0)=0
KnownBits computeKnownBits(const Value *V, unsigned Depth, const Instruction *CxtI) const
Definition: InstCombiner.h:470
void replaceUse(Use &U, Value *NewValue)
Replace use and add the previously used value to the worklist.
Definition: InstCombiner.h:452
InstCombiner(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)
Definition: InstCombiner.h:93
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:525
const SimplifyQuery SQ
Definition: InstCombiner.h:75
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
Definition: InstCombiner.h:63
Instruction * InsertNewInstWith(Instruction *New, BasicBlock::iterator Old)
Same as InsertNewInstBefore, but also sets the debug loc.
Definition: InstCombiner.h:409
const DataLayout & DL
Definition: InstCombiner.h:74
unsigned ComputeNumSignBits(const Value *Op, unsigned Depth=0, const Instruction *CxtI=nullptr) const
Definition: InstCombiner.h:486
const bool MinimizeSize
Definition: InstCombiner.h:66
virtual Value * SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &UndefElts, unsigned Depth=0, bool AllowMultipleUsers=false)=0
static Value * peekThroughBitcast(Value *V, bool OneUseOnly=false)
Return the source operand of a potentially bitcasted value while optionally checking if it has one us...
Definition: InstCombiner.h:108
bool canFreelyInvertAllUsersOf(Instruction *V, Value *IgnoredUser)
Given i1 V, can every user of V be freely adapted if V is changed to !V ? InstCombine's freelyInvertA...
Definition: InstCombiner.h:282
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder)
Definition: InstCombiner.h:254
AssumptionCache & AC
Definition: InstCombiner.h:71
void addToWorklist(Instruction *I)
Definition: InstCombiner.h:370
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
Definition: InstCombiner.h:444
DominatorTree & DT
Definition: InstCombiner.h:73
static Constant * getSafeVectorConstantForBinop(BinaryOperator::BinaryOps Opcode, Constant *In, bool IsRHSConstant)
Some binary operators require special handling to avoid poison and undefined behavior.
Definition: InstCombiner.h:318
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:503
ProfileSummaryInfo * getProfileSummaryInfo() const
Definition: InstCombiner.h:381
OptimizationRemarkEmitter & getOptimizationRemarkEmitter() const
Definition: InstCombiner.h:377
ProfileSummaryInfo * PSI
Definition: InstCombiner.h:78
static bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
Definition: InstCombiner.h:172
SmallDenseSet< std::pair< BasicBlock *, BasicBlock * >, 8 > DeadEdges
Edges that are known to never be taken.
Definition: InstCombiner.h:87
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Instruction *CxtI) const
Definition: InstCombiner.h:465
BuilderTy & Builder
Definition: InstCombiner.h:59
AssumptionCache & getAssumptionCache() const
Definition: InstCombiner.h:372
bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth=0, const Instruction *CxtI=nullptr) const
Definition: InstCombiner.h:481
OptimizationRemarkEmitter & ORE
Definition: InstCombiner.h:76
OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const Instruction *CxtI) const
Definition: InstCombiner.h:510
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
Definition: InstCombiner.h:247
const SimplifyQuery & getSimplifyQuery() const
Definition: InstCombiner.h:376
static Constant * AddOne(Constant *C)
Add one to a Constant.
Definition: InstCombiner.h:209
unsigned ComputeMaxSignificantBits(const Value *Op, unsigned Depth=0, const Instruction *CxtI=nullptr) const
Definition: InstCombiner.h:491
InstructionWorklist - This is the worklist management logic for InstCombine and other simplification ...
void pushUsersToWorkList(Instruction &I)
When an instruction is simplified, add all users of the instruction to the work lists because they mi...
void add(Instruction *I)
Add instruction to the worklist.
void push(Instruction *I)
Push the instruction onto the worklist stack.
void handleUseCountDecrement(Value *V)
Should be called after decrementing the use-count on V.
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:47
The optimization diagnostic interface.
Analysis providing profile information.
This class represents the LLVM 'select' instruction.
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition: DenseSet.h:290
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
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.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
OverflowResult
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &DL, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to have exactly one bit set when defined.
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return the number of times the sign bit of the register is replicated into the other bits.
OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Get the upper bound on bit size for this Value Op as a signed integer.
SimplifyQuery getWithInstruction(const Instruction *I) const
Definition: SimplifyQuery.h:94