blob: 38ba742bf5fa66c945b8426fcafc38b317c3fe33 [file] [log] [blame]
Chandler Carrutha9174582015-01-22 05:25:13 +00001//===- InstCombineInternal.h - InstCombine pass internals -------*- C++ -*-===//
Chris Lattner35522b72010-01-04 07:12:23 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner35522b72010-01-04 07:12:23 +00006//
7//===----------------------------------------------------------------------===//
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +00008//
Chandler Carrutha9174582015-01-22 05:25:13 +00009/// \file
10///
11/// This file provides internal interfaces used to implement the InstCombine.
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000012//
Chandler Carrutha9174582015-01-22 05:25:13 +000013//===----------------------------------------------------------------------===//
Chris Lattner35522b72010-01-04 07:12:23 +000014
Chandler Carrutha9174582015-01-22 05:25:13 +000015#ifndef LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
16#define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
Chris Lattner35522b72010-01-04 07:12:23 +000017
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000018#include "llvm/ADT/ArrayRef.h"
Bjorn Steinbrink83505342015-07-10 06:55:49 +000019#include "llvm/Analysis/AliasAnalysis.h"
Daniel Berlin2c75c632017-04-26 20:56:07 +000020#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruth452a0072014-03-04 11:59:06 +000021#include "llvm/Analysis/TargetFolder.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000022#include "llvm/Analysis/ValueTracking.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000023#include "llvm/IR/Argument.h"
24#include "llvm/IR/BasicBlock.h"
25#include "llvm/IR/Constant.h"
26#include "llvm/IR/Constants.h"
27#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/IRBuilder.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000029#include "llvm/IR/InstVisitor.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000030#include "llvm/IR/InstrTypes.h"
31#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000033#include "llvm/IR/Intrinsics.h"
David Green1e44c3b2018-10-02 09:48:34 +000034#include "llvm/IR/PatternMatch.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000035#include "llvm/IR/Use.h"
36#include "llvm/IR/Value.h"
37#include "llvm/Support/Casting.h"
38#include "llvm/Support/Compiler.h"
39#include "llvm/Support/Debug.h"
Craig Topper1a36b7d2017-05-15 06:39:41 +000040#include "llvm/Support/KnownBits.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000041#include "llvm/Support/raw_ostream.h"
Chandler Carruth83ba2692015-01-24 04:19:17 +000042#include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
David Green1e44c3b2018-10-02 09:48:34 +000043#include "llvm/Transforms/Utils/Local.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000044#include <cassert>
45#include <cstdint>
Chris Lattner35522b72010-01-04 07:12:23 +000046
Chandler Carruth964daaa2014-04-22 02:55:47 +000047#define DEBUG_TYPE "instcombine"
48
David Green1e44c3b2018-10-02 09:48:34 +000049using namespace llvm::PatternMatch;
50
Chris Lattner35522b72010-01-04 07:12:23 +000051namespace llvm {
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000052
53class APInt;
54class AssumptionCache;
Hiroshi Yamauchi09e539f2019-04-15 16:49:00 +000055class BlockFrequencyInfo;
Chandler Carruthd70cc602014-05-07 17:36:59 +000056class DataLayout;
Hal Finkel60db0582014-09-07 18:57:58 +000057class DominatorTree;
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000058class GEPOperator;
59class GlobalVariable;
60class LoopInfo;
Adam Nemetea06e6e2017-07-26 19:03:18 +000061class OptimizationRemarkEmitter;
Hiroshi Yamauchi09e539f2019-04-15 16:49:00 +000062class ProfileSummaryInfo;
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000063class TargetLibraryInfo;
64class User;
Jakub Staszak190db2f2013-01-14 23:16:36 +000065
Sanjay Patel73fc8dd2017-02-03 21:43:34 +000066/// Assign a complexity or rank value to LLVM Values. This is used to reduce
67/// the amount of pattern matching needed for compares and commutative
68/// instructions. For example, if we have:
69/// icmp ugt X, Constant
70/// or
71/// xor (add X, Constant), cast Z
72///
73/// We do not have to consider the commuted variants of these patterns because
74/// canonicalization based on complexity guarantees the above ordering.
Chandler Carruth3a622162015-01-20 19:27:58 +000075///
76/// This routine maps IR values to various complexity ranks:
77/// 0 -> undef
78/// 1 -> Constants
79/// 2 -> Other non-instructions
80/// 3 -> Arguments
Sanjay Patel73fc8dd2017-02-03 21:43:34 +000081/// 4 -> Cast and (f)neg/not instructions
82/// 5 -> Other instructions
Chris Lattner2188e402010-01-04 07:37:31 +000083static inline unsigned getComplexity(Value *V) {
84 if (isa<Instruction>(V)) {
Sanjay Patel47579b22018-10-13 16:15:37 +000085 if (isa<CastInst>(V) || match(V, m_Neg(m_Value())) ||
86 match(V, m_Not(m_Value())) || match(V, m_FNeg(m_Value())))
Sanjay Patel73fc8dd2017-02-03 21:43:34 +000087 return 4;
88 return 5;
Chris Lattner2188e402010-01-04 07:37:31 +000089 }
Chandler Carruthd70cc602014-05-07 17:36:59 +000090 if (isa<Argument>(V))
91 return 3;
Chris Lattner2188e402010-01-04 07:37:31 +000092 return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
93}
Chris Lattner35522b72010-01-04 07:12:23 +000094
Sanjay Patelb2e70032017-05-17 14:21:19 +000095/// Predicate canonicalization reduces the number of patterns that need to be
96/// matched by other transforms. For example, we may swap the operands of a
97/// conditional branch or select to create a compare with a canonical (inverted)
98/// predicate which is then more likely to be matched with other values.
99static inline bool isCanonicalPredicate(CmpInst::Predicate Pred) {
100 switch (Pred) {
101 case CmpInst::ICMP_NE:
102 case CmpInst::ICMP_ULE:
103 case CmpInst::ICMP_SLE:
104 case CmpInst::ICMP_UGE:
105 case CmpInst::ICMP_SGE:
106 // TODO: There are 16 FCMP predicates. Should others be (not) canonical?
107 case CmpInst::FCMP_ONE:
108 case CmpInst::FCMP_OLE:
109 case CmpInst::FCMP_OGE:
110 return false;
111 default:
112 return true;
113 }
114}
115
Roman Lebedev0c73be52019-10-07 20:53:08 +0000116/// Given an exploded icmp instruction, return true if the comparison only
117/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if the
118/// result of the comparison is true when the input value is signed.
119inline bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,
120 bool &TrueIfSigned) {
121 switch (Pred) {
122 case ICmpInst::ICMP_SLT: // True if LHS s< 0
123 TrueIfSigned = true;
124 return RHS.isNullValue();
125 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
126 TrueIfSigned = true;
127 return RHS.isAllOnesValue();
128 case ICmpInst::ICMP_SGT: // True if LHS s> -1
129 TrueIfSigned = false;
130 return RHS.isAllOnesValue();
131 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
132 TrueIfSigned = false;
133 return RHS.isNullValue();
134 case ICmpInst::ICMP_UGT:
135 // True if LHS u> RHS and RHS == sign-bit-mask - 1
136 TrueIfSigned = true;
137 return RHS.isMaxSignedValue();
138 case ICmpInst::ICMP_UGE:
139 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
140 TrueIfSigned = true;
141 return RHS.isMinSignedValue();
142 case ICmpInst::ICMP_ULT:
143 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
144 TrueIfSigned = false;
145 return RHS.isMinSignedValue();
146 case ICmpInst::ICMP_ULE:
147 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
148 TrueIfSigned = false;
149 return RHS.isMaxSignedValue();
150 default:
151 return false;
152 }
153}
154
Roman Lebedev32f1e1a2019-08-14 09:57:20 +0000155llvm::Optional<std::pair<CmpInst::Predicate, Constant *>>
156getFlippedStrictnessPredicateAndConstant(CmpInst::Predicate Pred, Constant *C);
157
Sanjay Patele800df8e2017-06-22 15:28:01 +0000158/// Return the source operand of a potentially bitcasted value while optionally
159/// checking if it has one use. If there is no bitcast or the one use check is
160/// not met, return the input value itself.
161static inline Value *peekThroughBitcast(Value *V, bool OneUseOnly = false) {
162 if (auto *BitCast = dyn_cast<BitCastInst>(V))
163 if (!OneUseOnly || BitCast->hasOneUse())
164 return BitCast->getOperand(0);
165
166 // V is not a bitcast or V has more than one use and OneUseOnly is true.
167 return V;
168}
169
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000170/// Add one to a Constant
Benjamin Kramer970f4952014-01-19 16:56:10 +0000171static inline Constant *AddOne(Constant *C) {
172 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
173}
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000174
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000175/// Subtract one from a Constant
Benjamin Kramer970f4952014-01-19 16:56:10 +0000176static inline Constant *SubOne(Constant *C) {
177 return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
178}
179
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000180/// Return true if the specified value is free to invert (apply ~ to).
Sanjoy Das82ea3d42015-02-24 00:08:41 +0000181/// This happens in cases where the ~ can be eliminated. If WillInvertAllUses
182/// is true, work under the assumption that the caller intends to remove all
183/// uses of V and only keep uses of ~V.
Roman Lebedev2635c322019-08-13 12:49:06 +0000184///
185/// See also: canFreelyInvertAllUsersOf()
Roman Lebedev04104892019-08-13 12:49:16 +0000186static inline bool isFreeToInvert(Value *V, bool WillInvertAllUses) {
Sanjoy Das82ea3d42015-02-24 00:08:41 +0000187 // ~(~(X)) -> X.
Sanjay Patel95790c52018-10-23 16:54:28 +0000188 if (match(V, m_Not(m_Value())))
Sanjoy Das82ea3d42015-02-24 00:08:41 +0000189 return true;
190
191 // Constants can be considered to be not'ed values.
Roman Lebedev3a947652019-07-22 22:09:24 +0000192 if (match(V, m_AnyIntegralConstant()))
Sanjoy Das82ea3d42015-02-24 00:08:41 +0000193 return true;
194
195 // Compares can be inverted if all of their uses are being modified to use the
196 // ~V.
197 if (isa<CmpInst>(V))
198 return WillInvertAllUses;
199
200 // If `V` is of the form `A + Constant` then `-1 - V` can be folded into `(-1
201 // - Constant) - A` if we are willing to invert all of the uses.
202 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
203 if (BO->getOpcode() == Instruction::Add ||
204 BO->getOpcode() == Instruction::Sub)
205 if (isa<Constant>(BO->getOperand(0)) || isa<Constant>(BO->getOperand(1)))
206 return WillInvertAllUses;
207
David Green1e44c3b2018-10-02 09:48:34 +0000208 // Selects with invertible operands are freely invertible
209 if (match(V, m_Select(m_Value(), m_Not(m_Value()), m_Not(m_Value()))))
210 return WillInvertAllUses;
211
Sanjoy Das82ea3d42015-02-24 00:08:41 +0000212 return false;
213}
214
Roman Lebedev2635c322019-08-13 12:49:06 +0000215/// Given i1 V, can every user of V be freely adapted if V is changed to !V ?
216///
Roman Lebedev04104892019-08-13 12:49:16 +0000217/// See also: isFreeToInvert()
Roman Lebedev2635c322019-08-13 12:49:06 +0000218static inline bool canFreelyInvertAllUsersOf(Value *V, Value *IgnoredUser) {
219 // Look at every user of V.
220 for (User *U : V->users()) {
221 if (U == IgnoredUser)
222 continue; // Don't consider this user.
223
224 auto *I = cast<Instruction>(U);
225 switch (I->getOpcode()) {
226 case Instruction::Select:
227 case Instruction::Br:
228 break; // Free to invert by swapping true/false values/destinations.
229 case Instruction::Xor: // Can invert 'xor' if it's a 'not', by ignoring it.
230 if (!match(I, m_Not(m_Value())))
231 return false; // Not a 'not'.
232 break;
233 default:
234 return false; // Don't know, likely not freely invertible.
235 }
236 // So far all users were free to invert...
237 }
238 return true; // Can freely invert all users!
239}
240
Sanjay Patela6272532018-07-09 16:16:51 +0000241/// Some binary operators require special handling to avoid poison and undefined
Sanjay Patel3e5c0512018-06-21 14:59:35 +0000242/// behavior. If a constant vector has undef elements, replace those undefs with
Sanjay Patel69faf4642018-07-09 23:22:47 +0000243/// identity constants if possible because those are always safe to execute.
244/// If no identity constant exists, replace undef with some other safe constant.
Sanjay Patela6272532018-07-09 16:16:51 +0000245static inline Constant *getSafeVectorConstantForBinop(
Sanjay Patel69faf4642018-07-09 23:22:47 +0000246 BinaryOperator::BinaryOps Opcode, Constant *In, bool IsRHSConstant) {
247 assert(In->getType()->isVectorTy() && "Not expecting scalars here");
Sanjay Patel3e5c0512018-06-21 14:59:35 +0000248
Sanjay Patel69faf4642018-07-09 23:22:47 +0000249 Type *EltTy = In->getType()->getVectorElementType();
250 auto *SafeC = ConstantExpr::getBinOpIdentity(Opcode, EltTy, IsRHSConstant);
251 if (!SafeC) {
252 // TODO: Should this be available as a constant utility function? It is
253 // similar to getBinOpAbsorber().
254 if (IsRHSConstant) {
255 switch (Opcode) {
256 case Instruction::SRem: // X % 1 = 0
257 case Instruction::URem: // X %u 1 = 0
258 SafeC = ConstantInt::get(EltTy, 1);
259 break;
260 case Instruction::FRem: // X % 1.0 (doesn't simplify, but it is safe)
261 SafeC = ConstantFP::get(EltTy, 1.0);
262 break;
263 default:
264 llvm_unreachable("Only rem opcodes have no identity constant for RHS");
265 }
266 } else {
267 switch (Opcode) {
268 case Instruction::Shl: // 0 << X = 0
269 case Instruction::LShr: // 0 >>u X = 0
270 case Instruction::AShr: // 0 >> X = 0
271 case Instruction::SDiv: // 0 / X = 0
272 case Instruction::UDiv: // 0 /u X = 0
273 case Instruction::SRem: // 0 % X = 0
274 case Instruction::URem: // 0 %u X = 0
275 case Instruction::Sub: // 0 - X (doesn't simplify, but it is safe)
276 case Instruction::FSub: // 0.0 - X (doesn't simplify, but it is safe)
277 case Instruction::FDiv: // 0.0 / X (doesn't simplify, but it is safe)
278 case Instruction::FRem: // 0.0 % X = 0
279 SafeC = Constant::getNullValue(EltTy);
280 break;
281 default:
282 llvm_unreachable("Expected to find identity constant for opcode");
283 }
284 }
285 }
286 assert(SafeC && "Must have safe constant for binop");
287 unsigned NumElts = In->getType()->getVectorNumElements();
Sanjay Patel3e5c0512018-06-21 14:59:35 +0000288 SmallVector<Constant *, 16> Out(NumElts);
289 for (unsigned i = 0; i != NumElts; ++i) {
290 Constant *C = In->getAggregateElement(i);
Sanjay Patel69faf4642018-07-09 23:22:47 +0000291 Out[i] = isa<UndefValue>(C) ? SafeC : C;
Sanjay Patel3e5c0512018-06-21 14:59:35 +0000292 }
293 return ConstantVector::get(Out);
294}
295
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000296/// The core instruction combiner logic.
Chandler Carruth3a622162015-01-20 19:27:58 +0000297///
298/// This class provides both the logic to recursively visit instructions and
Craig Topper28ec3462016-12-28 03:12:42 +0000299/// combine them.
Duncan Sands6c5e4352010-05-11 20:16:09 +0000300class LLVM_LIBRARY_VISIBILITY InstCombiner
Chandler Carruth1edb9d62015-01-20 22:44:35 +0000301 : public InstVisitor<InstCombiner, Instruction *> {
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000302 // FIXME: These members shouldn't be public.
Chris Lattner35522b72010-01-04 07:12:23 +0000303public:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000304 /// A worklist of the instructions that need to be simplified.
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000305 InstCombineWorklist &Worklist;
Chris Lattner35522b72010-01-04 07:12:23 +0000306
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000307 /// An IRBuilder that automatically inserts new instructions into the
Chandler Carruth3a622162015-01-20 19:27:58 +0000308 /// worklist.
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000309 using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>;
Craig Topperbb4069e2017-07-07 23:16:26 +0000310 BuilderTy &Builder;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000311
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000312private:
313 // Mode in which we are running the combiner.
314 const bool MinimizeSize;
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000315
Matthias Braunc31032d2016-03-09 18:47:11 +0000316 /// Enable combines that trigger rarely but are costly in compiletime.
317 const bool ExpensiveCombines;
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000318
Bjorn Steinbrink83505342015-07-10 06:55:49 +0000319 AliasAnalysis *AA;
320
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000321 // Required analyses.
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000322 AssumptionCache &AC;
Justin Bogner99798402016-08-05 01:06:44 +0000323 TargetLibraryInfo &TLI;
324 DominatorTree &DT;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000325 const DataLayout &DL;
Daniel Berlin2c75c632017-04-26 20:56:07 +0000326 const SimplifyQuery SQ;
Adam Nemetea06e6e2017-07-26 19:03:18 +0000327 OptimizationRemarkEmitter &ORE;
Hiroshi Yamauchi09e539f2019-04-15 16:49:00 +0000328 BlockFrequencyInfo *BFI;
329 ProfileSummaryInfo *PSI;
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000330
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000331 // Optional analyses. When non-null, these can both be used to do better
332 // combining and will be updated to reflect any changes.
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000333 LoopInfo *LI;
334
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000335 bool MadeIRChange = false;
Chris Lattner35522b72010-01-04 07:12:23 +0000336
337public:
Craig Topperbb4069e2017-07-07 23:16:26 +0000338 InstCombiner(InstCombineWorklist &Worklist, BuilderTy &Builder,
Matthias Braunc31032d2016-03-09 18:47:11 +0000339 bool MinimizeSize, bool ExpensiveCombines, AliasAnalysis *AA,
Daniel Berlin2c75c632017-04-26 20:56:07 +0000340 AssumptionCache &AC, TargetLibraryInfo &TLI, DominatorTree &DT,
Hiroshi Yamauchi09e539f2019-04-15 16:49:00 +0000341 OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
342 ProfileSummaryInfo *PSI, const DataLayout &DL, LoopInfo *LI)
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000343 : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000344 ExpensiveCombines(ExpensiveCombines), AA(AA), AC(AC), TLI(TLI), DT(DT),
Hiroshi Yamauchi09e539f2019-04-15 16:49:00 +0000345 DL(DL), SQ(DL, &TLI, &DT, &AC), ORE(ORE), BFI(BFI), PSI(PSI), LI(LI) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +0000346
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000347 /// Run the combiner over the entire worklist until it is empty.
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000348 ///
349 /// \returns true if the IR is changed.
350 bool run();
Chris Lattner35522b72010-01-04 07:12:23 +0000351
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000352 AssumptionCache &getAssumptionCache() const { return AC; }
353
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000354 const DataLayout &getDataLayout() const { return DL; }
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000355
Justin Bogner99798402016-08-05 01:06:44 +0000356 DominatorTree &getDominatorTree() const { return DT; }
Chris Lattner35522b72010-01-04 07:12:23 +0000357
Chandler Carruth5175b9a2015-01-20 08:35:24 +0000358 LoopInfo *getLoopInfo() const { return LI; }
359
Justin Bogner99798402016-08-05 01:06:44 +0000360 TargetLibraryInfo &getTargetLibraryInfo() const { return TLI; }
Chad Rosier43a33062011-12-02 01:26:24 +0000361
Chris Lattner35522b72010-01-04 07:12:23 +0000362 // Visitation implementation - Implement instruction combining for different
363 // instruction types. The semantics are as follows:
364 // Return Value:
365 // null - No change was made
366 // I - Change was made, I is still valid, I may be dead though
367 // otherwise - Change was made, replace I with returned instruction
368 //
Cameron McInallye75412a2019-05-10 20:01:04 +0000369 Instruction *visitFNeg(UnaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000370 Instruction *visitAdd(BinaryOperator &I);
371 Instruction *visitFAdd(BinaryOperator &I);
Nikita Popov0e322c82020-01-01 11:11:05 +0100372 Value *OptimizePointerDifference(
373 Value *LHS, Value *RHS, Type *Ty, bool isNUW);
Chris Lattner35522b72010-01-04 07:12:23 +0000374 Instruction *visitSub(BinaryOperator &I);
375 Instruction *visitFSub(BinaryOperator &I);
376 Instruction *visitMul(BinaryOperator &I);
377 Instruction *visitFMul(BinaryOperator &I);
378 Instruction *visitURem(BinaryOperator &I);
379 Instruction *visitSRem(BinaryOperator &I);
380 Instruction *visitFRem(BinaryOperator &I);
Sanjay Patelae2e3a42017-10-06 23:20:16 +0000381 bool simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000382 Instruction *commonRemTransforms(BinaryOperator &I);
383 Instruction *commonIRemTransforms(BinaryOperator &I);
384 Instruction *commonDivTransforms(BinaryOperator &I);
385 Instruction *commonIDivTransforms(BinaryOperator &I);
386 Instruction *visitUDiv(BinaryOperator &I);
387 Instruction *visitSDiv(BinaryOperator &I);
Frits van Bommel2a559512011-01-29 17:50:27 +0000388 Instruction *visitFDiv(BinaryOperator &I);
Erik Ecksteind1817522014-12-03 10:39:15 +0000389 Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
Chris Lattner35522b72010-01-04 07:12:23 +0000390 Instruction *visitAnd(BinaryOperator &I);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000391 Instruction *visitOr(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000392 Instruction *visitXor(BinaryOperator &I);
393 Instruction *visitShl(BinaryOperator &I);
Roman Lebedev49483a32019-10-20 19:38:50 +0000394 Value *reassociateShiftAmtsOfTwoSameDirectionShifts(
395 BinaryOperator *Sh0, const SimplifyQuery &SQ,
396 bool AnalyzeForSignBitExtraction = false);
Roman Lebedev7015a5c2019-10-20 20:52:06 +0000397 Instruction *canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(
398 BinaryOperator &I);
Roman Lebedevae3315a2019-10-02 23:02:12 +0000399 Instruction *foldVariableSignZeroExtensionOfVariableHighBitExtract(
400 BinaryOperator &OldAShr);
Chris Lattner35522b72010-01-04 07:12:23 +0000401 Instruction *visitAShr(BinaryOperator &I);
402 Instruction *visitLShr(BinaryOperator &I);
403 Instruction *commonShiftTransforms(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000404 Instruction *visitFCmpInst(FCmpInst &I);
405 Instruction *visitICmpInst(ICmpInst &I);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000406 Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattner35522b72010-01-04 07:12:23 +0000407 BinaryOperator &I);
408 Instruction *commonCastTransforms(CastInst &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000409 Instruction *commonPointerCastTransforms(CastInst &CI);
410 Instruction *visitTrunc(TruncInst &CI);
411 Instruction *visitZExt(ZExtInst &CI);
412 Instruction *visitSExt(SExtInst &CI);
413 Instruction *visitFPTrunc(FPTruncInst &CI);
414 Instruction *visitFPExt(CastInst &CI);
415 Instruction *visitFPToUI(FPToUIInst &FI);
416 Instruction *visitFPToSI(FPToSIInst &FI);
417 Instruction *visitUIToFP(CastInst &CI);
418 Instruction *visitSIToFP(CastInst &CI);
419 Instruction *visitPtrToInt(PtrToIntInst &CI);
420 Instruction *visitIntToPtr(IntToPtrInst &CI);
421 Instruction *visitBitCast(BitCastInst &CI);
Matt Arsenaulta9e95ab2013-11-15 05:45:08 +0000422 Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
Mehdi Aminib9a0fa42015-02-16 21:47:54 +0000423 Instruction *FoldItoFPtoI(Instruction &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000424 Instruction *visitSelectInst(SelectInst &SI);
Chris Lattner35522b72010-01-04 07:12:23 +0000425 Instruction *visitCallInst(CallInst &CI);
426 Instruction *visitInvokeInst(InvokeInst &II);
Craig Topper784929d2019-02-08 20:48:56 +0000427 Instruction *visitCallBrInst(CallBrInst &CBI);
Chris Lattner35522b72010-01-04 07:12:23 +0000428
429 Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
430 Instruction *visitPHINode(PHINode &PN);
431 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
432 Instruction *visitAllocaInst(AllocaInst &AI);
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000433 Instruction *visitAllocSite(Instruction &FI);
Gabor Greif75f69432010-06-24 12:21:15 +0000434 Instruction *visitFree(CallInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000435 Instruction *visitLoadInst(LoadInst &LI);
436 Instruction *visitStoreInst(StoreInst &SI);
Quentin Colombet96f54de2019-02-07 21:27:23 +0000437 Instruction *visitAtomicRMWInst(AtomicRMWInst &SI);
Chris Lattner35522b72010-01-04 07:12:23 +0000438 Instruction *visitBranchInst(BranchInst &BI);
Davide Italianoaec46172017-01-31 18:09:05 +0000439 Instruction *visitFenceInst(FenceInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000440 Instruction *visitSwitchInst(SwitchInst &SI);
Hal Finkel93873cc12014-09-07 21:28:34 +0000441 Instruction *visitReturnInst(ReturnInst &RI);
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000442 Instruction *visitInsertValueInst(InsertValueInst &IV);
Chris Lattner35522b72010-01-04 07:12:23 +0000443 Instruction *visitInsertElementInst(InsertElementInst &IE);
444 Instruction *visitExtractElementInst(ExtractElementInst &EI);
445 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
446 Instruction *visitExtractValueInst(ExtractValueInst &EV);
Duncan Sands5c055792011-09-30 13:12:16 +0000447 Instruction *visitLandingPadInst(LandingPadInst &LI);
Nikita Popov56f7de52020-02-22 18:37:10 +0100448 Instruction *visitVAEndInst(VAEndInst &I);
aqjune4187cb12019-11-07 01:12:17 +0900449 Instruction *visitFreeze(FreezeInst &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000450
Sanjay Patel53523312016-09-12 14:25:46 +0000451 /// Specify what to return for unhandled instructions.
Craig Toppere73658d2014-04-28 04:05:08 +0000452 Instruction *visitInstruction(Instruction &I) { return nullptr; }
Chris Lattner35522b72010-01-04 07:12:23 +0000453
Sanjay Patel53523312016-09-12 14:25:46 +0000454 /// True when DB dominates all uses of DI except UI.
455 /// UI must be in the same block as DI.
456 /// The routine checks that the DI parent and DB are different.
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +0000457 bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
458 const BasicBlock *DB) const;
459
Sanjay Patel53523312016-09-12 14:25:46 +0000460 /// Try to replace select with select operand SIOpd in SI-ICmp sequence.
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +0000461 bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
462 const unsigned SIOpd);
463
Yaxun Liuba01ed02017-02-10 21:46:07 +0000464 /// Try to replace instruction \p I with value \p V which are pointers
465 /// in different address space.
466 /// \return true if successful.
467 bool replacePointer(Instruction &I, Value *V);
468
Nikita Popovb4dd9282020-01-13 18:56:28 +0100469 LoadInst *combineLoadToNewType(LoadInst &LI, Type *NewTy,
470 const Twine &Suffix = "");
471
Chris Lattner35522b72010-01-04 07:12:23 +0000472private:
Sanjay Patel2217f752017-01-31 17:25:42 +0000473 bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
474 bool shouldChangeType(Type *From, Type *To) const;
Chris Lattner2188e402010-01-04 07:37:31 +0000475 Value *dyn_castNegVal(Value *V) const;
Nikita Popov0b83c5a2020-01-18 16:54:43 +0100476 Value *freelyNegateValue(Value *V);
David Blaikie87ca1b62015-03-27 20:56:11 +0000477 Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000478 SmallVectorImpl<Value *> &NewIndices);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000479
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000480 /// Classify whether a cast is worth optimizing.
Chandler Carruth3a622162015-01-20 19:27:58 +0000481 ///
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000482 /// This is a helper to decide whether the simplification of
483 /// logic(cast(A), cast(B)) to cast(logic(A, B)) should be performed.
484 ///
485 /// \param CI The cast we are interested in.
486 ///
487 /// \return true if this cast actually results in any code being generated and
488 /// if it cannot already be eliminated by some other transformation.
489 bool shouldOptimizeCast(CastInst *CI);
Chris Lattner2188e402010-01-04 07:37:31 +0000490
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000491 /// Try to optimize a sequence of instructions checking if an operation
Sanjoy Dasb0984472015-04-08 04:27:22 +0000492 /// on LHS and RHS overflows.
493 ///
Sanjoy Das6f5dca72015-08-28 19:09:31 +0000494 /// If this overflow check is done via one of the overflow check intrinsics,
495 /// then CtxI has to be the call instruction calling that intrinsic. If this
496 /// overflow check is done by arithmetic followed by a compare, then CtxI has
497 /// to be the arithmetic instruction.
498 ///
Sanjoy Dasb0984472015-04-08 04:27:22 +0000499 /// If a simplification is possible, stores the simplified result of the
500 /// operation in OperationResult and result of the overflow check in
501 /// OverflowResult, and return true. If no simplification is possible,
502 /// returns false.
Nikita Popov352f5982019-05-26 11:43:31 +0000503 bool OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp, bool IsSigned,
504 Value *LHS, Value *RHS,
Sanjoy Dasb0984472015-04-08 04:27:22 +0000505 Instruction &CtxI, Value *&OperationResult,
506 Constant *&OverflowResult);
507
Craig Topperc1892ec2019-01-31 17:23:29 +0000508 Instruction *visitCallBase(CallBase &Call);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000509 Instruction *tryOptimizeCall(CallInst *CI);
Craig Topperc1892ec2019-01-31 17:23:29 +0000510 bool transformConstExprCastCall(CallBase &Call);
511 Instruction *transformCallThroughTrampoline(CallBase &Call,
512 IntrinsicInst &Tramp);
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000513
Philip Reames7c8647b2019-04-25 01:18:56 +0000514 Value *simplifyMaskedLoad(IntrinsicInst &II);
Philip Reamese4588bb2019-03-20 18:44:58 +0000515 Instruction *simplifyMaskedStore(IntrinsicInst &II);
Philip Reames7c8647b2019-04-25 01:18:56 +0000516 Instruction *simplifyMaskedGather(IntrinsicInst &II);
Philip Reamese4588bb2019-03-20 18:44:58 +0000517 Instruction *simplifyMaskedScatter(IntrinsicInst &II);
518
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000519 /// Transform (zext icmp) to bitwise / integer operations in order to
520 /// eliminate it.
521 ///
522 /// \param ICI The icmp of the (zext icmp) pair we are interested in.
523 /// \parem CI The zext of the (zext icmp) pair we are interested in.
524 /// \param DoTransform Pass false to just test whether the given (zext icmp)
525 /// would be transformed. Pass true to actually perform the transformation.
526 ///
527 /// \return null if the transformation cannot be performed. If the
528 /// transformation can be performed the new instruction that replaces the
529 /// (zext icmp) pair will be returned (if \p DoTransform is false the
530 /// unmodified \p ICI will be returned in this case).
531 Instruction *transformZExtICmp(ICmpInst *ICI, ZExtInst &CI,
532 bool DoTransform = true);
533
Benjamin Kramer398b8c52011-04-01 20:09:03 +0000534 Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000535
Craig Topper2b1fc322017-05-22 06:25:31 +0000536 bool willNotOverflowSignedAdd(const Value *LHS, const Value *RHS,
537 const Instruction &CxtI) const {
Craig Topperbb973722017-05-15 02:44:08 +0000538 return computeOverflowForSignedAdd(LHS, RHS, &CxtI) ==
539 OverflowResult::NeverOverflows;
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000540 }
541
Craig Topper2b1fc322017-05-22 06:25:31 +0000542 bool willNotOverflowUnsignedAdd(const Value *LHS, const Value *RHS,
543 const Instruction &CxtI) const {
Craig Topperbb973722017-05-15 02:44:08 +0000544 return computeOverflowForUnsignedAdd(LHS, RHS, &CxtI) ==
545 OverflowResult::NeverOverflows;
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000546 }
547
Sanjay Patel46945b92018-09-14 21:30:07 +0000548 bool willNotOverflowAdd(const Value *LHS, const Value *RHS,
549 const Instruction &CxtI, bool IsSigned) const {
550 return IsSigned ? willNotOverflowSignedAdd(LHS, RHS, CxtI)
551 : willNotOverflowUnsignedAdd(LHS, RHS, CxtI);
552 }
553
Craig Topper2b1fc322017-05-22 06:25:31 +0000554 bool willNotOverflowSignedSub(const Value *LHS, const Value *RHS,
Omer Paparo Bivasfbb83de2018-05-10 19:46:19 +0000555 const Instruction &CxtI) const {
556 return computeOverflowForSignedSub(LHS, RHS, &CxtI) ==
557 OverflowResult::NeverOverflows;
558 }
559
Craig Topper2b1fc322017-05-22 06:25:31 +0000560 bool willNotOverflowUnsignedSub(const Value *LHS, const Value *RHS,
Omer Paparo Bivasfbb83de2018-05-10 19:46:19 +0000561 const Instruction &CxtI) const {
562 return computeOverflowForUnsignedSub(LHS, RHS, &CxtI) ==
563 OverflowResult::NeverOverflows;
564 }
565
Sanjay Patel46945b92018-09-14 21:30:07 +0000566 bool willNotOverflowSub(const Value *LHS, const Value *RHS,
567 const Instruction &CxtI, bool IsSigned) const {
568 return IsSigned ? willNotOverflowSignedSub(LHS, RHS, CxtI)
569 : willNotOverflowUnsignedSub(LHS, RHS, CxtI);
570 }
571
Craig Topper2b1fc322017-05-22 06:25:31 +0000572 bool willNotOverflowSignedMul(const Value *LHS, const Value *RHS,
Omer Paparo Bivasfbb83de2018-05-10 19:46:19 +0000573 const Instruction &CxtI) const {
574 return computeOverflowForSignedMul(LHS, RHS, &CxtI) ==
575 OverflowResult::NeverOverflows;
576 }
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000577
Craig Topper2b1fc322017-05-22 06:25:31 +0000578 bool willNotOverflowUnsignedMul(const Value *LHS, const Value *RHS,
579 const Instruction &CxtI) const {
Craig Topperbb973722017-05-15 02:44:08 +0000580 return computeOverflowForUnsignedMul(LHS, RHS, &CxtI) ==
581 OverflowResult::NeverOverflows;
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000582 }
583
Sanjay Patel46945b92018-09-14 21:30:07 +0000584 bool willNotOverflowMul(const Value *LHS, const Value *RHS,
585 const Instruction &CxtI, bool IsSigned) const {
586 return IsSigned ? willNotOverflowSignedMul(LHS, RHS, CxtI)
587 : willNotOverflowUnsignedMul(LHS, RHS, CxtI);
588 }
589
Sanjay Patel90a36342018-09-14 22:23:35 +0000590 bool willNotOverflow(BinaryOperator::BinaryOps Opcode, const Value *LHS,
591 const Value *RHS, const Instruction &CxtI,
592 bool IsSigned) const {
593 switch (Opcode) {
594 case Instruction::Add: return willNotOverflowAdd(LHS, RHS, CxtI, IsSigned);
595 case Instruction::Sub: return willNotOverflowSub(LHS, RHS, CxtI, IsSigned);
596 case Instruction::Mul: return willNotOverflowMul(LHS, RHS, CxtI, IsSigned);
597 default: llvm_unreachable("Unexpected opcode for overflow query");
598 }
599 }
600
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000601 Value *EmitGEPOffset(User *GEP);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000602 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
Sanjay Patel40e7ba02016-02-23 16:36:07 +0000603 Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
Sanjay Patel94da1de2017-08-05 15:19:18 +0000604 Instruction *narrowBinOp(TruncInst &Trunc);
Sanjay Patel1d681122018-01-25 16:34:36 +0000605 Instruction *narrowMaskedBinOp(BinaryOperator &And);
Sanjay Patel90a36342018-09-14 22:23:35 +0000606 Instruction *narrowMathIfNoOverflow(BinaryOperator &I);
Sanjay Patelc50e55d2017-08-09 18:37:41 +0000607 Instruction *narrowRotate(TruncInst &Trunc);
Guozhi Weiae541f62016-10-25 20:43:42 +0000608 Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
David Green186155b2019-10-22 15:39:47 +0000609 Instruction *matchSAddSubSat(SelectInst &MinMax1);
Chris Lattner35522b72010-01-04 07:12:23 +0000610
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000611 /// Determine if a pair of casts can be replaced by a single cast.
612 ///
613 /// \param CI1 The first of a pair of casts.
614 /// \param CI2 The second of a pair of casts.
615 ///
616 /// \return 0 if the cast pair cannot be eliminated, otherwise returns an
617 /// Instruction::CastOps value for a cast that can replace the pair, casting
618 /// CI1->getSrcTy() to CI2->getDstTy().
619 ///
620 /// \see CastInst::isEliminableCastPair
621 Instruction::CastOps isEliminableCastPair(const CastInst *CI1,
622 const CastInst *CI2);
623
Craig Topperda6ea0d2017-06-16 05:10:37 +0000624 Value *foldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &CxtI);
Craig Topperf2d3e6d2017-06-15 19:09:51 +0000625 Value *foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &CxtI);
Roman Lebedev2635c322019-08-13 12:49:06 +0000626 Value *foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS, BinaryOperator &I);
Sanjay Patel5e456b92017-05-18 20:53:16 +0000627
Sanjay Patel64fc5da2017-09-02 17:53:33 +0000628 /// Optimize (fcmp)&(fcmp) or (fcmp)|(fcmp).
629 /// NOTE: Unlike most of instcombine, this returns a Value which should
630 /// already be inserted into the function.
631 Value *foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS, bool IsAnd);
632
Craig Topperda6ea0d2017-06-16 05:10:37 +0000633 Value *foldAndOrOfICmpsOfAndWithPow2(ICmpInst *LHS, ICmpInst *RHS,
634 bool JoinedByAnd, Instruction &CxtI);
Sanjay Patel3b206302018-10-24 15:17:56 +0000635 Value *matchSelectFromAndOr(Value *A, Value *B, Value *C, Value *D);
636 Value *getSelectCondition(Value *A, Value *B);
637
Nikita Popov884feb12019-03-06 18:30:00 +0000638 Instruction *foldIntrinsicWithOverflowCommon(IntrinsicInst *II);
639
Chris Lattner35522b72010-01-04 07:12:23 +0000640public:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000641 /// Inserts an instruction \p New before instruction \p Old
Chandler Carruth3a622162015-01-20 19:27:58 +0000642 ///
643 /// Also adds the new instruction to the worklist and returns \p New so that
644 /// it is suitable for use as the return from the visitation patterns.
Chris Lattner35522b72010-01-04 07:12:23 +0000645 Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
Craig Toppere73658d2014-04-28 04:05:08 +0000646 assert(New && !New->getParent() &&
Chris Lattner35522b72010-01-04 07:12:23 +0000647 "New instruction already inserted into a basic block!");
648 BasicBlock *BB = Old.getParent();
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000649 BB->getInstList().insert(Old.getIterator(), New); // Insert inst
Nikita Popove6c9ab42020-01-30 22:32:46 +0100650 Worklist.push(New);
Chris Lattner35522b72010-01-04 07:12:23 +0000651 return New;
652 }
Eli Friedman6efb64e2011-05-19 01:20:42 +0000653
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000654 /// Same as InsertNewInstBefore, but also sets the debug loc.
Eli Friedman6efb64e2011-05-19 01:20:42 +0000655 Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
656 New->setDebugLoc(Old.getDebugLoc());
657 return InsertNewInstBefore(New, Old);
658 }
659
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000660 /// A combiner-aware RAUW-like routine.
Chandler Carruth3a622162015-01-20 19:27:58 +0000661 ///
662 /// This method is to be used when an instruction is found to be dead,
Sanjay Patelf2ea8a22016-01-06 00:23:12 +0000663 /// replaceable with another preexisting expression. Here we add all uses of
Chandler Carruth3a622162015-01-20 19:27:58 +0000664 /// I to the worklist, replace all uses of I with the new value, then return
665 /// I, so that the inst combiner will know that I was modified.
Sanjay Patel4b198802016-02-01 22:23:39 +0000666 Instruction *replaceInstUsesWith(Instruction &I, Value *V) {
Owen Anderson51b75b8c2015-03-10 05:13:47 +0000667 // If there are no uses to replace, then we return nullptr to indicate that
668 // no changes were made to the program.
669 if (I.use_empty()) return nullptr;
670
Nikita Popove6c9ab42020-01-30 22:32:46 +0100671 Worklist.pushUsersToWorkList(I); // Add all modified instrs to worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000672
Chris Lattner35522b72010-01-04 07:12:23 +0000673 // If we are replacing the instruction with itself, this must be in a
674 // segment of unreachable code, so just clobber the instruction.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000675 if (&I == V)
Chris Lattner35522b72010-01-04 07:12:23 +0000676 V = UndefValue::get(I.getType());
Frits van Bommeld14d9912011-03-27 23:32:31 +0000677
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000678 LLVM_DEBUG(dbgs() << "IC: Replacing " << I << "\n"
679 << " with " << *V << '\n');
Frits van Bommeld14d9912011-03-27 23:32:31 +0000680
Chris Lattner35522b72010-01-04 07:12:23 +0000681 I.replaceAllUsesWith(V);
682 return &I;
683 }
684
Nikita Popov878cb382020-01-31 22:23:33 +0100685 /// Replace operand of instruction and add old operand to the worklist.
686 Instruction *replaceOperand(Instruction &I, unsigned OpNum, Value *V) {
687 Worklist.addValue(I.getOperand(OpNum));
688 I.setOperand(OpNum, V);
689 return &I;
690 }
691
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000692 /// Creates a result tuple for an overflow intrinsic \p II with a given
Sanjoy Dasb0984472015-04-08 04:27:22 +0000693 /// \p Result and a constant \p Overflow value.
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000694 Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result,
Sanjoy Dasb0984472015-04-08 04:27:22 +0000695 Constant *Overflow) {
696 Constant *V[] = {UndefValue::get(Result->getType()), Overflow};
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000697 StructType *ST = cast<StructType>(II->getType());
698 Constant *Struct = ConstantStruct::get(ST, V);
699 return InsertValueInst::Create(Struct, Result, 0);
700 }
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000701
Philip Reames88679712019-04-17 17:37:58 +0000702 /// Create and insert the idiom we use to indicate a block is unreachable
703 /// without having to rewrite the CFG from within InstCombine.
704 void CreateNonTerminatorUnreachable(Instruction *InsertAt) {
705 auto &Ctx = InsertAt->getContext();
706 new StoreInst(ConstantInt::getTrue(Ctx),
707 UndefValue::get(Type::getInt1PtrTy(Ctx)),
708 InsertAt);
709 }
710
711
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000712 /// Combiner aware instruction erasure.
Chandler Carruth3a622162015-01-20 19:27:58 +0000713 ///
714 /// When dealing with an instruction that has side effects or produces a void
715 /// value, we can't rely on DCE to delete the instruction. Instead, visit
716 /// methods should return the value returned by this function.
Sanjay Patel4b198802016-02-01 22:23:39 +0000717 Instruction *eraseInstFromFunction(Instruction &I) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000718 LLVM_DEBUG(dbgs() << "IC: ERASE " << I << '\n');
Adrian Prantlfa9e84e2017-03-16 20:11:54 +0000719 assert(I.use_empty() && "Cannot erase instruction that is used!");
Vedant Kumara087b782019-11-07 11:19:41 -0800720 salvageDebugInfoOrMarkUndef(I);
Adrian Prantl47ea6472017-03-16 21:14:09 +0000721
Chris Lattner35522b72010-01-04 07:12:23 +0000722 // Make sure that we reprocess all operands now that we reduced their
723 // use counts.
724 if (I.getNumOperands() < 8) {
Sanjay Patel8af7fbc32016-01-31 16:34:48 +0000725 for (Use &Operand : I.operands())
726 if (auto *Inst = dyn_cast<Instruction>(Operand))
Nikita Popov4ef272e2020-02-20 22:42:42 +0100727 Worklist.add(Inst);
Chris Lattner35522b72010-01-04 07:12:23 +0000728 }
Nikita Popove6c9ab42020-01-30 22:32:46 +0100729 Worklist.remove(&I);
Chris Lattner35522b72010-01-04 07:12:23 +0000730 I.eraseFromParent();
731 MadeIRChange = true;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000732 return nullptr; // Don't do anything with FI
Chris Lattner35522b72010-01-04 07:12:23 +0000733 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000734
Craig Topper2b1fc322017-05-22 06:25:31 +0000735 void computeKnownBits(const Value *V, KnownBits &Known,
736 unsigned Depth, const Instruction *CxtI) const {
Craig Topper26c41592017-05-15 02:30:27 +0000737 llvm::computeKnownBits(V, Known, DL, Depth, &AC, CxtI, &DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000738 }
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000739
Craig Topper2b1fc322017-05-22 06:25:31 +0000740 KnownBits computeKnownBits(const Value *V, unsigned Depth,
741 const Instruction *CxtI) const {
Craig Topper1a36b7d2017-05-15 06:39:41 +0000742 return llvm::computeKnownBits(V, DL, Depth, &AC, CxtI, &DT);
743 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000744
Craig Topperd4039f72017-05-25 21:51:12 +0000745 bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero = false,
746 unsigned Depth = 0,
747 const Instruction *CxtI = nullptr) {
748 return llvm::isKnownToBeAPowerOfTwo(V, DL, OrZero, Depth, &AC, CxtI, &DT);
749 }
750
Craig Topper2b1fc322017-05-22 06:25:31 +0000751 bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth = 0,
752 const Instruction *CxtI = nullptr) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000753 return llvm::MaskedValueIsZero(V, Mask, DL, Depth, &AC, CxtI, &DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000754 }
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000755
Craig Topper2b1fc322017-05-22 06:25:31 +0000756 unsigned ComputeNumSignBits(const Value *Op, unsigned Depth = 0,
757 const Instruction *CxtI = nullptr) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000758 return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000759 }
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000760
Craig Topper2b1fc322017-05-22 06:25:31 +0000761 OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
762 const Value *RHS,
763 const Instruction *CxtI) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000764 return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
David Majnemer491331a2015-01-02 07:29:43 +0000765 }
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000766
Omer Paparo Bivasfbb83de2018-05-10 19:46:19 +0000767 OverflowResult computeOverflowForSignedMul(const Value *LHS,
Nikita Popov39f2beb2019-05-26 11:43:37 +0000768 const Value *RHS,
Omer Paparo Bivasfbb83de2018-05-10 19:46:19 +0000769 const Instruction *CxtI) const {
770 return llvm::computeOverflowForSignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
771 }
772
Craig Topper2b1fc322017-05-22 06:25:31 +0000773 OverflowResult computeOverflowForUnsignedAdd(const Value *LHS,
774 const Value *RHS,
775 const Instruction *CxtI) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000776 return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
David Majnemer5310c1e2015-01-07 00:39:50 +0000777 }
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000778
Craig Topperbb973722017-05-15 02:44:08 +0000779 OverflowResult computeOverflowForSignedAdd(const Value *LHS,
780 const Value *RHS,
781 const Instruction *CxtI) const {
782 return llvm::computeOverflowForSignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
783 }
Chris Lattner35522b72010-01-04 07:12:23 +0000784
Omer Paparo Bivasfbb83de2018-05-10 19:46:19 +0000785 OverflowResult computeOverflowForUnsignedSub(const Value *LHS,
786 const Value *RHS,
787 const Instruction *CxtI) const {
788 return llvm::computeOverflowForUnsignedSub(LHS, RHS, DL, &AC, CxtI, &DT);
789 }
790
791 OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS,
792 const Instruction *CxtI) const {
793 return llvm::computeOverflowForSignedSub(LHS, RHS, DL, &AC, CxtI, &DT);
794 }
795
Nikita Popov39f2beb2019-05-26 11:43:37 +0000796 OverflowResult computeOverflow(
797 Instruction::BinaryOps BinaryOp, bool IsSigned,
798 Value *LHS, Value *RHS, Instruction *CxtI) const;
799
Davide Italiano2133bf52017-02-07 17:56:50 +0000800 /// Maximum size of array considered when transforming.
Simon Pilgrimb5696242019-08-28 15:19:49 +0000801 uint64_t MaxArraySizeForCombine = 0;
Davide Italiano2133bf52017-02-07 17:56:50 +0000802
Chris Lattner35522b72010-01-04 07:12:23 +0000803private:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000804 /// Performs a few simplifications for operators which are associative
Chandler Carruth3a622162015-01-20 19:27:58 +0000805 /// or commutative.
Duncan Sands641baf12010-11-13 15:10:37 +0000806 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000807
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000808 /// Tries to simplify binary operations which some other binary
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000809 /// operation distributes over.
Chandler Carruth3a622162015-01-20 19:27:58 +0000810 ///
811 /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
812 /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
813 /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
814 /// value, or null if it didn't simplify.
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000815 Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
Duncan Sandsadc7771f2010-11-23 14:23:47 +0000816
Sanjoy Das6f1937b2018-04-26 20:52:28 +0000817 /// Tries to simplify add operations using the definition of remainder.
818 ///
819 /// The definition of remainder is X % C = X - (X / C ) * C. The add
820 /// expression X % C0 + (( X / C0 ) % C1) * C0 can be simplified to
821 /// X % (C0 * C1)
822 Value *SimplifyAddWithRemainder(BinaryOperator &I);
823
Quentin Colombetaa103b32017-09-20 17:32:16 +0000824 // Binary Op helper for select operations where the expression can be
825 // efficiently reorganized.
826 Value *SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *LHS,
827 Value *RHS);
828
Daniel Berlin2c75c632017-04-26 20:56:07 +0000829 /// This tries to simplify binary operations by factorizing out common terms
830 /// (e. g. "(A*B)+(A*C)" -> "A*(B+C)").
Craig Topper47c8f662017-07-06 18:35:52 +0000831 Value *tryFactorization(BinaryOperator &, Instruction::BinaryOps, Value *,
832 Value *, Value *, Value *);
Daniel Berlin2c75c632017-04-26 20:56:07 +0000833
Anna Thomasd67165c2017-06-23 13:41:45 +0000834 /// Match a select chain which produces one of three values based on whether
835 /// the LHS is less than, equal to, or greater than RHS respectively.
836 /// Return true if we matched a three way compare idiom. The LHS, RHS, Less,
837 /// Equal and Greater values are saved in the matching process and returned to
838 /// the caller.
839 bool matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, Value *&RHS,
840 ConstantInt *&Less, ConstantInt *&Equal,
841 ConstantInt *&Greater);
842
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000843 /// Attempts to replace V with a simpler value based on the demanded
Chandler Carruth3a622162015-01-20 19:27:58 +0000844 /// bits.
Craig Topperb45eabc2017-04-26 16:39:58 +0000845 Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, KnownBits &Known,
846 unsigned Depth, Instruction *CxtI);
Craig Topper47596dd2017-03-25 06:52:52 +0000847 bool SimplifyDemandedBits(Instruction *I, unsigned Op,
Craig Topperb45eabc2017-04-26 16:39:58 +0000848 const APInt &DemandedMask, KnownBits &Known,
849 unsigned Depth = 0);
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000850
Craig Topperb0076fe2017-04-12 18:05:21 +0000851 /// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
852 /// bits. It also tries to handle simplifications that can be done based on
853 /// DemandedMask, but without modifying the Instruction.
854 Value *SimplifyMultipleUseDemandedBits(Instruction *I,
855 const APInt &DemandedMask,
Craig Topperb45eabc2017-04-26 16:39:58 +0000856 KnownBits &Known,
Craig Topperb0076fe2017-04-12 18:05:21 +0000857 unsigned Depth, Instruction *CxtI);
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000858
Shuxin Yang63e999e2012-12-04 00:04:54 +0000859 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
860 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
Sanjay Patelcc663b82017-04-20 22:37:01 +0000861 Value *simplifyShrShlDemandedBits(
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000862 Instruction *Shr, const APInt &ShrOp1, Instruction *Shl,
Craig Topperb45eabc2017-04-26 16:39:58 +0000863 const APInt &ShlOp1, const APInt &DemandedMask, KnownBits &Known);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000864
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000865 /// Tries to simplify operands to an integer instruction based on its
Chandler Carruth3a622162015-01-20 19:27:58 +0000866 /// demanded bits.
Chris Lattner35522b72010-01-04 07:12:23 +0000867 bool SimplifyDemandedInstructionBits(Instruction &Inst);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000868
Nicolai Haehnleb29ee702018-06-21 13:37:31 +0000869 Value *simplifyAMDGCNMemoryIntrinsicDemanded(IntrinsicInst *II,
870 APInt DemandedElts,
Nicolai Haehnlea69146e2019-02-04 21:24:19 +0000871 int DmaskIdx = -1);
Nicolai Haehnleb29ee702018-06-21 13:37:31 +0000872
Chris Lattner35522b72010-01-04 07:12:23 +0000873 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Piotr Sobczaka861c9a2019-10-21 08:12:47 +0000874 APInt &UndefElts, unsigned Depth = 0,
875 bool AllowMultipleUsers = false);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000876
Sanjay Patelbbc6d602018-06-02 16:27:44 +0000877 /// Canonicalize the position of binops relative to shufflevector.
Sanjay Patel79dceb22018-10-03 15:20:58 +0000878 Instruction *foldVectorBinop(BinaryOperator &Inst);
Sanjay Pateldb0938f2017-01-10 23:49:07 +0000879
880 /// Given a binary operator, cast instruction, or select which has a PHI node
881 /// as operand #0, see if we can fold the instruction into the PHI (which is
882 /// only possible if all operands to the PHI are constants).
Craig Topperfb71b7d2017-04-14 19:20:12 +0000883 Instruction *foldOpIntoPhi(Instruction &I, PHINode *PN);
Chris Lattner35522b72010-01-04 07:12:23 +0000884
Sanjay Pateldb0938f2017-01-10 23:49:07 +0000885 /// Given an instruction with a select as one operand and a constant as the
886 /// other operand, try to fold the binary operator into the select arguments.
887 /// This also works for Cast instructions, which obviously do not have a
888 /// second operand.
889 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
890
891 /// This is a convenience wrapper function for the above two functions.
Sanjay Patel8fdd87f2018-02-28 16:36:24 +0000892 Instruction *foldBinOpIntoSelectOrPhi(BinaryOperator &I);
Sanjay Pateldb0938f2017-01-10 23:49:07 +0000893
Sanjay Patel8d810fe2017-10-13 16:43:58 +0000894 Instruction *foldAddWithConstant(BinaryOperator &Add);
895
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000896 /// Try to rotate an operation below a PHI node, using PHI nodes for
Chandler Carruth3a622162015-01-20 19:27:58 +0000897 /// its operands.
Chris Lattner35522b72010-01-04 07:12:23 +0000898 Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
899 Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
900 Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
901 Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
Sanjay Patel95334072015-09-27 20:34:31 +0000902 Instruction *FoldPHIArgZextsIntoPHI(PHINode &PN);
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000903
Xinliang David Li4cdc9da2017-10-10 05:07:54 +0000904 /// If an integer typed PHI has only one use which is an IntToPtr operation,
905 /// replace the PHI with an existing pointer typed PHI if it exists. Otherwise
906 /// insert a new pointer typed PHI and replace the original one.
907 Instruction *FoldIntegerTypedPHI(PHINode &PN);
Chris Lattner35522b72010-01-04 07:12:23 +0000908
Dehao Chenf4646272017-10-02 18:13:14 +0000909 /// Helper function for FoldPHIArgXIntoPHI() to set debug location for the
Robert Lougher2428a402016-12-14 17:49:19 +0000910 /// folded operation.
Dehao Chenf4646272017-10-02 18:13:14 +0000911 void PHIArgMergedDebugLoc(Instruction *Inst, PHINode &PN);
Robert Lougher2428a402016-12-14 17:49:19 +0000912
Sanjay Patel43395062016-07-21 18:07:40 +0000913 Instruction *foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
914 ICmpInst::Predicate Cond, Instruction &I);
Pete Cooper980a9352016-08-12 17:13:28 +0000915 Instruction *foldAllocaCmp(ICmpInst &ICI, const AllocaInst *Alloca,
916 const Value *Other);
Sanjay Patel43395062016-07-21 18:07:40 +0000917 Instruction *foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
918 GlobalVariable *GV, CmpInst &ICI,
919 ConstantInt *AndCst = nullptr);
920 Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI,
921 Constant *RHSC);
Craig Topperbee74792018-08-20 23:04:25 +0000922 Instruction *foldICmpAddOpConst(Value *X, const APInt &C,
Sanjay Patel43395062016-07-21 18:07:40 +0000923 ICmpInst::Predicate Pred);
Sanjay Patela90ee0e2019-08-20 14:56:44 +0000924 Instruction *foldICmpWithCastOp(ICmpInst &ICI);
Sanjay Patelf58f68c2016-09-10 15:03:44 +0000925
Sanjay Patel3151dec2016-09-12 15:24:31 +0000926 Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp);
Sanjay Patela40bf9ff2018-12-04 15:35:17 +0000927 Instruction *foldICmpWithDominatingICmp(ICmpInst &Cmp);
Sanjay Patel06b127a2016-09-15 14:37:50 +0000928 Instruction *foldICmpWithConstant(ICmpInst &Cmp);
Sanjay Patelf58f68c2016-09-10 15:03:44 +0000929 Instruction *foldICmpInstWithConstant(ICmpInst &Cmp);
Sanjay Patel10494b22016-09-16 16:10:22 +0000930 Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp);
Roman Lebedev23646952019-09-25 19:06:40 +0000931 Instruction *foldICmpBinOp(ICmpInst &Cmp, const SimplifyQuery &SQ);
Sanjay Patel10494b22016-09-16 16:10:22 +0000932 Instruction *foldICmpEquality(ICmpInst &Cmp);
Roman Lebedevbe612ea2019-07-30 15:28:22 +0000933 Instruction *foldIRemByPowerOfTwoToBitTest(ICmpInst &I);
Roman Lebedev49483a32019-10-20 19:38:50 +0000934 Instruction *foldSignBitTest(ICmpInst &I);
Nikolai Bozhenov0e7ebbc2017-10-16 09:19:21 +0000935 Instruction *foldICmpWithZero(ICmpInst &Cmp);
Sanjay Patela3f4f082016-08-16 17:54:36 +0000936
Roman Lebedev473a0632019-08-29 12:47:20 +0000937 Value *foldUnsignedMultiplicationOverflowCheck(ICmpInst &Cmp);
938
Craig Topper524c44f2017-08-23 05:46:07 +0000939 Instruction *foldICmpSelectConstant(ICmpInst &Cmp, SelectInst *Select,
Anna Thomasd67165c2017-06-23 13:41:45 +0000940 ConstantInt *C);
Craig Topper524c44f2017-08-23 05:46:07 +0000941 Instruction *foldICmpTruncConstant(ICmpInst &Cmp, TruncInst *Trunc,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000942 const APInt &C);
Sanjay Patelc9196c42016-08-22 21:24:29 +0000943 Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000944 const APInt &C);
Sanjay Patelc9196c42016-08-22 21:24:29 +0000945 Instruction *foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000946 const APInt &C);
Sanjay Patelc9196c42016-08-22 21:24:29 +0000947 Instruction *foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000948 const APInt &C);
Sanjay Patelc9196c42016-08-22 21:24:29 +0000949 Instruction *foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000950 const APInt &C);
Sanjay Patelc9196c42016-08-22 21:24:29 +0000951 Instruction *foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000952 const APInt &C);
Sanjay Patelc9196c42016-08-22 21:24:29 +0000953 Instruction *foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000954 const APInt &C);
Sanjay Patel80bea342019-09-11 12:04:26 +0000955 Instruction *foldICmpSRemConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
956 const APInt &C);
Sanjay Patelc9196c42016-08-22 21:24:29 +0000957 Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000958 const APInt &C);
Sanjay Patelc9196c42016-08-22 21:24:29 +0000959 Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000960 const APInt &C);
Sanjay Patelc9196c42016-08-22 21:24:29 +0000961 Instruction *foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000962 const APInt &C);
Sanjay Patelc9196c42016-08-22 21:24:29 +0000963 Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000964 const APInt &C);
Sanjay Pateld3c7bb282016-08-26 16:42:33 +0000965 Instruction *foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000966 const APInt &C1);
Sanjay Patel14e0e182016-08-26 18:28:46 +0000967 Instruction *foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000968 const APInt &C1, const APInt &C2);
Sanjay Patel8da42cc2016-09-15 22:26:31 +0000969 Instruction *foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
970 const APInt &C2);
971 Instruction *foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
972 const APInt &C2);
Sanjay Patela3f4f082016-08-16 17:54:36 +0000973
Sanjay Patelf58f68c2016-09-10 15:03:44 +0000974 Instruction *foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp,
975 BinaryOperator *BO,
Craig Topper8ed1aa92017-10-03 05:31:07 +0000976 const APInt &C);
Nikita Popov6515db22019-01-19 09:56:01 +0000977 Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II,
978 const APInt &C);
979 Instruction *foldICmpEqIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II,
980 const APInt &C);
Sanjay Patel43395062016-07-21 18:07:40 +0000981
Sanjay Patel453ceff2016-09-29 22:18:30 +0000982 // Helpers of visitSelectInst().
Sanjay Patelf7b851f2016-09-30 19:49:22 +0000983 Instruction *foldSelectExtConst(SelectInst &Sel);
Sanjay Patel453ceff2016-09-29 22:18:30 +0000984 Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
985 Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *);
986 Instruction *foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
987 Value *A, Value *B, Instruction &Outer,
988 SelectPatternFlavor SPF2, Value *C);
989 Instruction *foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
990
Craig Topper70e4f432017-04-02 17:57:30 +0000991 Instruction *OptAndOp(BinaryOperator *Op, ConstantInt *OpRHS,
Chris Lattner35522b72010-01-04 07:12:23 +0000992 ConstantInt *AndRHS, BinaryOperator &TheAnd);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000993
Sanjay Patel85d79742016-08-31 19:49:56 +0000994 Value *insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi,
995 bool isSigned, bool Inside);
Chris Lattner35522b72010-01-04 07:12:23 +0000996 Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
Sanjay Patel4a12aa92018-11-10 20:29:25 +0000997 bool mergeStoreIntoSuccessor(StoreInst &SI);
Igor Laevsky900ffa32017-02-08 14:32:04 +0000998
Sanjay Patel60728422018-11-14 16:03:36 +0000999 /// Given an 'or' instruction, check to see if it is part of a bswap idiom.
1000 /// If so, return the equivalent bswap intrinsic.
1001 Instruction *matchBSwap(BinaryOperator &Or);
1002
Daniel Neilson8f30ec62018-05-11 14:30:02 +00001003 Instruction *SimplifyAnyMemTransfer(AnyMemTransferInst *MI);
Daniel Neilsonf6651d42018-05-11 20:04:50 +00001004 Instruction *SimplifyAnyMemSet(AnyMemSetInst *MI);
Chris Lattner35522b72010-01-04 07:12:23 +00001005
Chris Lattner229907c2011-07-18 04:54:35 +00001006 Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
Duncan Sands533c8ae2012-10-23 08:28:26 +00001007
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001008 /// Returns a value X such that Val = X * Scale, or null if none.
Chandler Carruth3a622162015-01-20 19:27:58 +00001009 ///
1010 /// If the multiplication is known not to overflow then NoSignedWrap is set.
Duncan Sands533c8ae2012-10-23 08:28:26 +00001011 Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
Chris Lattner35522b72010-01-04 07:12:23 +00001012};
1013
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +00001014} // end namespace llvm
Chris Lattner35522b72010-01-04 07:12:23 +00001015
Chandler Carruth964daaa2014-04-22 02:55:47 +00001016#undef DEBUG_TYPE
1017
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +00001018#endif // LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H