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

source code of llvm/include/llvm/Transforms/InstCombine/InstCombiner.h