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