blob: 68bae0e5200be7c833adf627c6e73f1e4ed7fb19 [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//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chandler Carrutha9174582015-01-22 05:25:13 +00009/// \file
10///
11/// This file provides internal interfaces used to implement the InstCombine.
12///
13//===----------------------------------------------------------------------===//
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
Bjorn Steinbrink83505342015-07-10 06:55:49 +000018#include "llvm/Analysis/AliasAnalysis.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000019#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruth5175b9a2015-01-20 08:35:24 +000020#include "llvm/Analysis/LoopInfo.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"
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +000023#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/IRBuilder.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000025#include "llvm/IR/InstVisitor.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/IntrinsicInst.h"
27#include "llvm/IR/Operator.h"
Hal Finkel74c2f352014-09-07 12:44:26 +000028#include "llvm/IR/PatternMatch.h"
Chris Lattner35522b72010-01-04 07:12:23 +000029#include "llvm/Pass.h"
Chandler Carruth83ba2692015-01-24 04:19:17 +000030#include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
Chris Lattner35522b72010-01-04 07:12:23 +000031
Chandler Carruth964daaa2014-04-22 02:55:47 +000032#define DEBUG_TYPE "instcombine"
33
Chris Lattner35522b72010-01-04 07:12:23 +000034namespace llvm {
Chandler Carruthd70cc602014-05-07 17:36:59 +000035class CallSite;
36class DataLayout;
Hal Finkel60db0582014-09-07 18:57:58 +000037class DominatorTree;
Chandler Carruthd70cc602014-05-07 17:36:59 +000038class TargetLibraryInfo;
39class DbgDeclareInst;
40class MemIntrinsic;
41class MemSetInst;
Jakub Staszak190db2f2013-01-14 23:16:36 +000042
Sanjay Patel73fc8dd2017-02-03 21:43:34 +000043/// Assign a complexity or rank value to LLVM Values. This is used to reduce
44/// the amount of pattern matching needed for compares and commutative
45/// instructions. For example, if we have:
46/// icmp ugt X, Constant
47/// or
48/// xor (add X, Constant), cast Z
49///
50/// We do not have to consider the commuted variants of these patterns because
51/// canonicalization based on complexity guarantees the above ordering.
Chandler Carruth3a622162015-01-20 19:27:58 +000052///
53/// This routine maps IR values to various complexity ranks:
54/// 0 -> undef
55/// 1 -> Constants
56/// 2 -> Other non-instructions
57/// 3 -> Arguments
Sanjay Patel73fc8dd2017-02-03 21:43:34 +000058/// 4 -> Cast and (f)neg/not instructions
59/// 5 -> Other instructions
Chris Lattner2188e402010-01-04 07:37:31 +000060static inline unsigned getComplexity(Value *V) {
61 if (isa<Instruction>(V)) {
Sanjay Patel73fc8dd2017-02-03 21:43:34 +000062 if (isa<CastInst>(V) || BinaryOperator::isNeg(V) ||
63 BinaryOperator::isFNeg(V) || BinaryOperator::isNot(V))
64 return 4;
65 return 5;
Chris Lattner2188e402010-01-04 07:37:31 +000066 }
Chandler Carruthd70cc602014-05-07 17:36:59 +000067 if (isa<Argument>(V))
68 return 3;
Chris Lattner2188e402010-01-04 07:37:31 +000069 return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
70}
Chris Lattner35522b72010-01-04 07:12:23 +000071
Chandler Carruth3a622162015-01-20 19:27:58 +000072/// \brief Add one to a Constant
Benjamin Kramer970f4952014-01-19 16:56:10 +000073static inline Constant *AddOne(Constant *C) {
74 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
75}
Chandler Carruth3a622162015-01-20 19:27:58 +000076/// \brief Subtract one from a Constant
Benjamin Kramer970f4952014-01-19 16:56:10 +000077static inline Constant *SubOne(Constant *C) {
78 return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
79}
80
Sanjoy Das82ea3d42015-02-24 00:08:41 +000081/// \brief Return true if the specified value is free to invert (apply ~ to).
82/// This happens in cases where the ~ can be eliminated. If WillInvertAllUses
83/// is true, work under the assumption that the caller intends to remove all
84/// uses of V and only keep uses of ~V.
85///
86static inline bool IsFreeToInvert(Value *V, bool WillInvertAllUses) {
87 // ~(~(X)) -> X.
88 if (BinaryOperator::isNot(V))
89 return true;
90
91 // Constants can be considered to be not'ed values.
92 if (isa<ConstantInt>(V))
93 return true;
94
Sanjay Patel611f9f92016-10-27 17:30:50 +000095 // A vector of constant integers can be inverted easily.
96 Constant *CV;
97 if (V->getType()->isVectorTy() && match(V, PatternMatch::m_Constant(CV))) {
98 unsigned NumElts = V->getType()->getVectorNumElements();
99 for (unsigned i = 0; i != NumElts; ++i) {
100 Constant *Elt = CV->getAggregateElement(i);
101 if (!Elt)
102 return false;
103
104 if (isa<UndefValue>(Elt))
105 continue;
106
107 if (!isa<ConstantInt>(Elt))
108 return false;
109 }
110 return true;
111 }
112
Sanjoy Das82ea3d42015-02-24 00:08:41 +0000113 // Compares can be inverted if all of their uses are being modified to use the
114 // ~V.
115 if (isa<CmpInst>(V))
116 return WillInvertAllUses;
117
118 // If `V` is of the form `A + Constant` then `-1 - V` can be folded into `(-1
119 // - Constant) - A` if we are willing to invert all of the uses.
120 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
121 if (BO->getOpcode() == Instruction::Add ||
122 BO->getOpcode() == Instruction::Sub)
123 if (isa<Constant>(BO->getOperand(0)) || isa<Constant>(BO->getOperand(1)))
124 return WillInvertAllUses;
125
126 return false;
127}
128
Sanjoy Dasb0984472015-04-08 04:27:22 +0000129
130/// \brief Specific patterns of overflow check idioms that we match.
131enum OverflowCheckFlavor {
132 OCF_UNSIGNED_ADD,
133 OCF_SIGNED_ADD,
134 OCF_UNSIGNED_SUB,
135 OCF_SIGNED_SUB,
136 OCF_UNSIGNED_MUL,
137 OCF_SIGNED_MUL,
138
139 OCF_INVALID
140};
141
142/// \brief Returns the OverflowCheckFlavor corresponding to a overflow_with_op
143/// intrinsic.
144static inline OverflowCheckFlavor
145IntrinsicIDToOverflowCheckFlavor(unsigned ID) {
146 switch (ID) {
147 default:
148 return OCF_INVALID;
149 case Intrinsic::uadd_with_overflow:
150 return OCF_UNSIGNED_ADD;
151 case Intrinsic::sadd_with_overflow:
152 return OCF_SIGNED_ADD;
153 case Intrinsic::usub_with_overflow:
154 return OCF_UNSIGNED_SUB;
155 case Intrinsic::ssub_with_overflow:
156 return OCF_SIGNED_SUB;
157 case Intrinsic::umul_with_overflow:
158 return OCF_UNSIGNED_MUL;
159 case Intrinsic::smul_with_overflow:
160 return OCF_SIGNED_MUL;
161 }
162}
163
Chandler Carruth3a622162015-01-20 19:27:58 +0000164/// \brief The core instruction combiner logic.
165///
166/// This class provides both the logic to recursively visit instructions and
Craig Topper28ec3462016-12-28 03:12:42 +0000167/// combine them.
Duncan Sands6c5e4352010-05-11 20:16:09 +0000168class LLVM_LIBRARY_VISIBILITY InstCombiner
Chandler Carruth1edb9d62015-01-20 22:44:35 +0000169 : public InstVisitor<InstCombiner, Instruction *> {
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000170 // FIXME: These members shouldn't be public.
Chris Lattner35522b72010-01-04 07:12:23 +0000171public:
Chandler Carruth3a622162015-01-20 19:27:58 +0000172 /// \brief A worklist of the instructions that need to be simplified.
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000173 InstCombineWorklist &Worklist;
Chris Lattner35522b72010-01-04 07:12:23 +0000174
Chandler Carruth3a622162015-01-20 19:27:58 +0000175 /// \brief An IRBuilder that automatically inserts new instructions into the
176 /// worklist.
Justin Bogner19dd0da2016-08-04 23:41:01 +0000177 typedef IRBuilder<TargetFolder, IRBuilderCallbackInserter> BuilderTy;
Chris Lattner35522b72010-01-04 07:12:23 +0000178 BuilderTy *Builder;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000179
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000180private:
181 // Mode in which we are running the combiner.
182 const bool MinimizeSize;
Matthias Braunc31032d2016-03-09 18:47:11 +0000183 /// Enable combines that trigger rarely but are costly in compiletime.
184 const bool ExpensiveCombines;
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000185
Bjorn Steinbrink83505342015-07-10 06:55:49 +0000186 AliasAnalysis *AA;
187
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000188 // Required analyses.
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000189 AssumptionCache &AC;
Justin Bogner99798402016-08-05 01:06:44 +0000190 TargetLibraryInfo &TLI;
191 DominatorTree &DT;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000192 const DataLayout &DL;
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000193
194 // Optional analyses. When non-null, these can both be used to do better
195 // combining and will be updated to reflect any changes.
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000196 LoopInfo *LI;
197
198 bool MadeIRChange;
Chris Lattner35522b72010-01-04 07:12:23 +0000199
200public:
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000201 InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder,
Matthias Braunc31032d2016-03-09 18:47:11 +0000202 bool MinimizeSize, bool ExpensiveCombines, AliasAnalysis *AA,
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000203 AssumptionCache &AC, TargetLibraryInfo &TLI,
204 DominatorTree &DT, const DataLayout &DL, LoopInfo *LI)
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000205 : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000206 ExpensiveCombines(ExpensiveCombines), AA(AA), AC(AC), TLI(TLI), DT(DT),
207 DL(DL), LI(LI), MadeIRChange(false) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +0000208
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000209 /// \brief Run the combiner over the entire worklist until it is empty.
210 ///
211 /// \returns true if the IR is changed.
212 bool run();
Chris Lattner35522b72010-01-04 07:12:23 +0000213
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000214 AssumptionCache &getAssumptionCache() const { return AC; }
215
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000216 const DataLayout &getDataLayout() const { return DL; }
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000217
Justin Bogner99798402016-08-05 01:06:44 +0000218 DominatorTree &getDominatorTree() const { return DT; }
Chris Lattner35522b72010-01-04 07:12:23 +0000219
Chandler Carruth5175b9a2015-01-20 08:35:24 +0000220 LoopInfo *getLoopInfo() const { return LI; }
221
Justin Bogner99798402016-08-05 01:06:44 +0000222 TargetLibraryInfo &getTargetLibraryInfo() const { return TLI; }
Chad Rosier43a33062011-12-02 01:26:24 +0000223
Chris Lattner35522b72010-01-04 07:12:23 +0000224 // Visitation implementation - Implement instruction combining for different
225 // instruction types. The semantics are as follows:
226 // Return Value:
227 // null - No change was made
228 // I - Change was made, I is still valid, I may be dead though
229 // otherwise - Change was made, replace I with returned instruction
230 //
231 Instruction *visitAdd(BinaryOperator &I);
232 Instruction *visitFAdd(BinaryOperator &I);
Chris Lattner229907c2011-07-18 04:54:35 +0000233 Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
Chris Lattner35522b72010-01-04 07:12:23 +0000234 Instruction *visitSub(BinaryOperator &I);
235 Instruction *visitFSub(BinaryOperator &I);
236 Instruction *visitMul(BinaryOperator &I);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000237 Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000238 Instruction *InsertBefore);
Chris Lattner35522b72010-01-04 07:12:23 +0000239 Instruction *visitFMul(BinaryOperator &I);
240 Instruction *visitURem(BinaryOperator &I);
241 Instruction *visitSRem(BinaryOperator &I);
242 Instruction *visitFRem(BinaryOperator &I);
243 bool SimplifyDivRemOfSelect(BinaryOperator &I);
244 Instruction *commonRemTransforms(BinaryOperator &I);
245 Instruction *commonIRemTransforms(BinaryOperator &I);
246 Instruction *commonDivTransforms(BinaryOperator &I);
247 Instruction *commonIDivTransforms(BinaryOperator &I);
248 Instruction *visitUDiv(BinaryOperator &I);
249 Instruction *visitSDiv(BinaryOperator &I);
Frits van Bommel2a559512011-01-29 17:50:27 +0000250 Instruction *visitFDiv(BinaryOperator &I);
Erik Ecksteind1817522014-12-03 10:39:15 +0000251 Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
Chris Lattner067459c2010-03-05 08:46:26 +0000252 Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
253 Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chris Lattner35522b72010-01-04 07:12:23 +0000254 Instruction *visitAnd(BinaryOperator &I);
Hal Finkel60db0582014-09-07 18:57:58 +0000255 Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction *CxtI);
Chris Lattner067459c2010-03-05 08:46:26 +0000256 Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000257 Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A,
258 Value *B, Value *C);
David Majnemer5d1aeba2014-08-21 05:14:48 +0000259 Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A,
260 Value *B, Value *C);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000261 Instruction *visitOr(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000262 Instruction *visitXor(BinaryOperator &I);
263 Instruction *visitShl(BinaryOperator &I);
264 Instruction *visitAShr(BinaryOperator &I);
265 Instruction *visitLShr(BinaryOperator &I);
266 Instruction *commonShiftTransforms(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000267 Instruction *visitFCmpInst(FCmpInst &I);
268 Instruction *visitICmpInst(ICmpInst &I);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000269 Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattner35522b72010-01-04 07:12:23 +0000270 BinaryOperator &I);
271 Instruction *commonCastTransforms(CastInst &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000272 Instruction *commonPointerCastTransforms(CastInst &CI);
273 Instruction *visitTrunc(TruncInst &CI);
274 Instruction *visitZExt(ZExtInst &CI);
275 Instruction *visitSExt(SExtInst &CI);
276 Instruction *visitFPTrunc(FPTruncInst &CI);
277 Instruction *visitFPExt(CastInst &CI);
278 Instruction *visitFPToUI(FPToUIInst &FI);
279 Instruction *visitFPToSI(FPToSIInst &FI);
280 Instruction *visitUIToFP(CastInst &CI);
281 Instruction *visitSIToFP(CastInst &CI);
282 Instruction *visitPtrToInt(PtrToIntInst &CI);
283 Instruction *visitIntToPtr(IntToPtrInst &CI);
284 Instruction *visitBitCast(BitCastInst &CI);
Matt Arsenaulta9e95ab2013-11-15 05:45:08 +0000285 Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
Mehdi Aminib9a0fa42015-02-16 21:47:54 +0000286 Instruction *FoldItoFPtoI(Instruction &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000287 Instruction *visitSelectInst(SelectInst &SI);
Chris Lattner35522b72010-01-04 07:12:23 +0000288 Instruction *visitCallInst(CallInst &CI);
289 Instruction *visitInvokeInst(InvokeInst &II);
290
291 Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
292 Instruction *visitPHINode(PHINode &PN);
293 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
294 Instruction *visitAllocaInst(AllocaInst &AI);
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000295 Instruction *visitAllocSite(Instruction &FI);
Gabor Greif75f69432010-06-24 12:21:15 +0000296 Instruction *visitFree(CallInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000297 Instruction *visitLoadInst(LoadInst &LI);
298 Instruction *visitStoreInst(StoreInst &SI);
299 Instruction *visitBranchInst(BranchInst &BI);
Davide Italianoaec46172017-01-31 18:09:05 +0000300 Instruction *visitFenceInst(FenceInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000301 Instruction *visitSwitchInst(SwitchInst &SI);
Hal Finkel93873cc12014-09-07 21:28:34 +0000302 Instruction *visitReturnInst(ReturnInst &RI);
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000303 Instruction *visitInsertValueInst(InsertValueInst &IV);
Chris Lattner35522b72010-01-04 07:12:23 +0000304 Instruction *visitInsertElementInst(InsertElementInst &IE);
305 Instruction *visitExtractElementInst(ExtractElementInst &EI);
306 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
307 Instruction *visitExtractValueInst(ExtractValueInst &EV);
Duncan Sands5c055792011-09-30 13:12:16 +0000308 Instruction *visitLandingPadInst(LandingPadInst &LI);
Arnaud A. de Grandmaison333ef382016-05-10 09:24:49 +0000309 Instruction *visitVAStartInst(VAStartInst &I);
310 Instruction *visitVACopyInst(VACopyInst &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000311
Sanjay Patel53523312016-09-12 14:25:46 +0000312 /// Specify what to return for unhandled instructions.
Craig Toppere73658d2014-04-28 04:05:08 +0000313 Instruction *visitInstruction(Instruction &I) { return nullptr; }
Chris Lattner35522b72010-01-04 07:12:23 +0000314
Sanjay Patel53523312016-09-12 14:25:46 +0000315 /// True when DB dominates all uses of DI except UI.
316 /// UI must be in the same block as DI.
317 /// The routine checks that the DI parent and DB are different.
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +0000318 bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
319 const BasicBlock *DB) const;
320
Sanjay Patel53523312016-09-12 14:25:46 +0000321 /// Try to replace select with select operand SIOpd in SI-ICmp sequence.
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +0000322 bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
323 const unsigned SIOpd);
324
Chris Lattner35522b72010-01-04 07:12:23 +0000325private:
Sanjay Patel2217f752017-01-31 17:25:42 +0000326 bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
327 bool shouldChangeType(Type *From, Type *To) const;
Chris Lattner2188e402010-01-04 07:37:31 +0000328 Value *dyn_castNegVal(Value *V) const;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000329 Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
David Blaikie87ca1b62015-03-27 20:56:11 +0000330 Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000331 SmallVectorImpl<Value *> &NewIndices);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000332
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000333 /// Classify whether a cast is worth optimizing.
Chandler Carruth3a622162015-01-20 19:27:58 +0000334 ///
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000335 /// This is a helper to decide whether the simplification of
336 /// logic(cast(A), cast(B)) to cast(logic(A, B)) should be performed.
337 ///
338 /// \param CI The cast we are interested in.
339 ///
340 /// \return true if this cast actually results in any code being generated and
341 /// if it cannot already be eliminated by some other transformation.
342 bool shouldOptimizeCast(CastInst *CI);
Chris Lattner2188e402010-01-04 07:37:31 +0000343
Sanjoy Dasb0984472015-04-08 04:27:22 +0000344 /// \brief Try to optimize a sequence of instructions checking if an operation
345 /// on LHS and RHS overflows.
346 ///
Sanjoy Das6f5dca72015-08-28 19:09:31 +0000347 /// If this overflow check is done via one of the overflow check intrinsics,
348 /// then CtxI has to be the call instruction calling that intrinsic. If this
349 /// overflow check is done by arithmetic followed by a compare, then CtxI has
350 /// to be the arithmetic instruction.
351 ///
Sanjoy Dasb0984472015-04-08 04:27:22 +0000352 /// If a simplification is possible, stores the simplified result of the
353 /// operation in OperationResult and result of the overflow check in
354 /// OverflowResult, and return true. If no simplification is possible,
355 /// returns false.
356 bool OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS, Value *RHS,
357 Instruction &CtxI, Value *&OperationResult,
358 Constant *&OverflowResult);
359
Chris Lattner35522b72010-01-04 07:12:23 +0000360 Instruction *visitCallSite(CallSite CS);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000361 Instruction *tryOptimizeCall(CallInst *CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000362 bool transformConstExprCastCall(CallSite CS);
Duncan Sandsa0984362011-09-06 13:37:06 +0000363 Instruction *transformCallThroughTrampoline(CallSite CS,
364 IntrinsicInst *Tramp);
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000365
366 /// Transform (zext icmp) to bitwise / integer operations in order to
367 /// eliminate it.
368 ///
369 /// \param ICI The icmp of the (zext icmp) pair we are interested in.
370 /// \parem CI The zext of the (zext icmp) pair we are interested in.
371 /// \param DoTransform Pass false to just test whether the given (zext icmp)
372 /// would be transformed. Pass true to actually perform the transformation.
373 ///
374 /// \return null if the transformation cannot be performed. If the
375 /// transformation can be performed the new instruction that replaces the
376 /// (zext icmp) pair will be returned (if \p DoTransform is false the
377 /// unmodified \p ICI will be returned in this case).
378 Instruction *transformZExtICmp(ICmpInst *ICI, ZExtInst &CI,
379 bool DoTransform = true);
380
Benjamin Kramer398b8c52011-04-01 20:09:03 +0000381 Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000382 bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS, Instruction &CxtI);
383 bool WillNotOverflowSignedSub(Value *LHS, Value *RHS, Instruction &CxtI);
384 bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, Instruction &CxtI);
385 bool WillNotOverflowSignedMul(Value *LHS, Value *RHS, Instruction &CxtI);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000386 Value *EmitGEPOffset(User *GEP);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000387 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
Nick Lewyckya2b77202013-05-31 00:59:42 +0000388 Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
Sanjay Patel40e7ba02016-02-23 16:36:07 +0000389 Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
Sanjay Patelaa8b28e2016-11-30 20:48:54 +0000390 Instruction *shrinkBitwiseLogic(TruncInst &Trunc);
Guozhi Weiae541f62016-10-25 20:43:42 +0000391 Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
Chris Lattner35522b72010-01-04 07:12:23 +0000392
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000393 /// Determine if a pair of casts can be replaced by a single cast.
394 ///
395 /// \param CI1 The first of a pair of casts.
396 /// \param CI2 The second of a pair of casts.
397 ///
398 /// \return 0 if the cast pair cannot be eliminated, otherwise returns an
399 /// Instruction::CastOps value for a cast that can replace the pair, casting
400 /// CI1->getSrcTy() to CI2->getDstTy().
401 ///
402 /// \see CastInst::isEliminableCastPair
403 Instruction::CastOps isEliminableCastPair(const CastInst *CI1,
404 const CastInst *CI2);
405
Chris Lattner35522b72010-01-04 07:12:23 +0000406public:
Chandler Carruth3a622162015-01-20 19:27:58 +0000407 /// \brief Inserts an instruction \p New before instruction \p Old
408 ///
409 /// Also adds the new instruction to the worklist and returns \p New so that
410 /// it is suitable for use as the return from the visitation patterns.
Chris Lattner35522b72010-01-04 07:12:23 +0000411 Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
Craig Toppere73658d2014-04-28 04:05:08 +0000412 assert(New && !New->getParent() &&
Chris Lattner35522b72010-01-04 07:12:23 +0000413 "New instruction already inserted into a basic block!");
414 BasicBlock *BB = Old.getParent();
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000415 BB->getInstList().insert(Old.getIterator(), New); // Insert inst
Chris Lattner35522b72010-01-04 07:12:23 +0000416 Worklist.Add(New);
417 return New;
418 }
Eli Friedman6efb64e2011-05-19 01:20:42 +0000419
Chandler Carruth3a622162015-01-20 19:27:58 +0000420 /// \brief Same as InsertNewInstBefore, but also sets the debug loc.
Eli Friedman6efb64e2011-05-19 01:20:42 +0000421 Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
422 New->setDebugLoc(Old.getDebugLoc());
423 return InsertNewInstBefore(New, Old);
424 }
425
Chandler Carruth3a622162015-01-20 19:27:58 +0000426 /// \brief A combiner-aware RAUW-like routine.
427 ///
428 /// This method is to be used when an instruction is found to be dead,
Sanjay Patelf2ea8a22016-01-06 00:23:12 +0000429 /// replaceable with another preexisting expression. Here we add all uses of
Chandler Carruth3a622162015-01-20 19:27:58 +0000430 /// I to the worklist, replace all uses of I with the new value, then return
431 /// I, so that the inst combiner will know that I was modified.
Sanjay Patel4b198802016-02-01 22:23:39 +0000432 Instruction *replaceInstUsesWith(Instruction &I, Value *V) {
Owen Anderson51b75b8c2015-03-10 05:13:47 +0000433 // If there are no uses to replace, then we return nullptr to indicate that
434 // no changes were made to the program.
435 if (I.use_empty()) return nullptr;
436
Chandler Carruthd70cc602014-05-07 17:36:59 +0000437 Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000438
Chris Lattner35522b72010-01-04 07:12:23 +0000439 // If we are replacing the instruction with itself, this must be in a
440 // segment of unreachable code, so just clobber the instruction.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000441 if (&I == V)
Chris Lattner35522b72010-01-04 07:12:23 +0000442 V = UndefValue::get(I.getType());
Frits van Bommeld14d9912011-03-27 23:32:31 +0000443
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000444 DEBUG(dbgs() << "IC: Replacing " << I << "\n"
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000445 << " with " << *V << '\n');
Frits van Bommeld14d9912011-03-27 23:32:31 +0000446
Chris Lattner35522b72010-01-04 07:12:23 +0000447 I.replaceAllUsesWith(V);
448 return &I;
449 }
450
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000451 /// Creates a result tuple for an overflow intrinsic \p II with a given
Sanjoy Dasb0984472015-04-08 04:27:22 +0000452 /// \p Result and a constant \p Overflow value.
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000453 Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result,
Sanjoy Dasb0984472015-04-08 04:27:22 +0000454 Constant *Overflow) {
455 Constant *V[] = {UndefValue::get(Result->getType()), Overflow};
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000456 StructType *ST = cast<StructType>(II->getType());
457 Constant *Struct = ConstantStruct::get(ST, V);
458 return InsertValueInst::Create(Struct, Result, 0);
459 }
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000460
Chandler Carruth3a622162015-01-20 19:27:58 +0000461 /// \brief Combiner aware instruction erasure.
462 ///
463 /// When dealing with an instruction that has side effects or produces a void
464 /// value, we can't rely on DCE to delete the instruction. Instead, visit
465 /// methods should return the value returned by this function.
Sanjay Patel4b198802016-02-01 22:23:39 +0000466 Instruction *eraseInstFromFunction(Instruction &I) {
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000467 DEBUG(dbgs() << "IC: ERASE " << I << '\n');
Chris Lattner35522b72010-01-04 07:12:23 +0000468
469 assert(I.use_empty() && "Cannot erase instruction that is used!");
470 // Make sure that we reprocess all operands now that we reduced their
471 // use counts.
472 if (I.getNumOperands() < 8) {
Sanjay Patel8af7fbc32016-01-31 16:34:48 +0000473 for (Use &Operand : I.operands())
474 if (auto *Inst = dyn_cast<Instruction>(Operand))
475 Worklist.Add(Inst);
Chris Lattner35522b72010-01-04 07:12:23 +0000476 }
477 Worklist.Remove(&I);
478 I.eraseFromParent();
479 MadeIRChange = true;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000480 return nullptr; // Don't do anything with FI
Chris Lattner35522b72010-01-04 07:12:23 +0000481 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000482
Jay Foada0653a32014-05-14 21:14:37 +0000483 void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000484 unsigned Depth, Instruction *CxtI) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000485 return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, &AC, CxtI,
486 &DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000487 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000488
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000489 bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth = 0,
Hal Finkel60db0582014-09-07 18:57:58 +0000490 Instruction *CxtI = nullptr) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000491 return llvm::MaskedValueIsZero(V, Mask, DL, Depth, &AC, CxtI, &DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000492 }
Hal Finkel60db0582014-09-07 18:57:58 +0000493 unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0,
494 Instruction *CxtI = nullptr) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000495 return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000496 }
David Majnemer54c2ca22014-12-26 09:10:14 +0000497 void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
498 unsigned Depth = 0, Instruction *CxtI = nullptr) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000499 return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, &AC, CxtI,
500 &DT);
David Majnemer54c2ca22014-12-26 09:10:14 +0000501 }
David Majnemer491331a2015-01-02 07:29:43 +0000502 OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
503 const Instruction *CxtI) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000504 return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
David Majnemer491331a2015-01-02 07:29:43 +0000505 }
David Majnemer5310c1e2015-01-07 00:39:50 +0000506 OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
507 const Instruction *CxtI) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000508 return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
David Majnemer5310c1e2015-01-07 00:39:50 +0000509 }
Chris Lattner35522b72010-01-04 07:12:23 +0000510
Davide Italiano2133bf52017-02-07 17:56:50 +0000511 /// Maximum size of array considered when transforming.
David Blaikie4c01af22017-02-07 18:58:17 +0000512 uint64_t MaxArraySizeForCombine;
Davide Italiano2133bf52017-02-07 17:56:50 +0000513
Chris Lattner35522b72010-01-04 07:12:23 +0000514private:
Chandler Carruth3a622162015-01-20 19:27:58 +0000515 /// \brief Performs a few simplifications for operators which are associative
516 /// or commutative.
Duncan Sands641baf12010-11-13 15:10:37 +0000517 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000518
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000519 /// \brief Tries to simplify binary operations which some other binary
520 /// operation distributes over.
Chandler Carruth3a622162015-01-20 19:27:58 +0000521 ///
522 /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
523 /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
524 /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
525 /// value, or null if it didn't simplify.
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000526 Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
Duncan Sandsadc7771f2010-11-23 14:23:47 +0000527
Chandler Carruth3a622162015-01-20 19:27:58 +0000528 /// \brief Attempts to replace V with a simpler value based on the demanded
529 /// bits.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000530 Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero,
Hal Finkel60db0582014-09-07 18:57:58 +0000531 APInt &KnownOne, unsigned Depth,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000532 Instruction *CxtI);
Benjamin Kramerc321e532016-06-08 19:09:22 +0000533 bool SimplifyDemandedBits(Use &U, const APInt &DemandedMask, APInt &KnownZero,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000534 APInt &KnownOne, unsigned Depth = 0);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000535 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
536 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
537 Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
Benjamin Kramerc321e532016-06-08 19:09:22 +0000538 const APInt &DemandedMask, APInt &KnownZero,
Shuxin Yang63e999e2012-12-04 00:04:54 +0000539 APInt &KnownOne);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000540
Chandler Carruth3a622162015-01-20 19:27:58 +0000541 /// \brief Tries to simplify operands to an integer instruction based on its
542 /// demanded bits.
Chris Lattner35522b72010-01-04 07:12:23 +0000543 bool SimplifyDemandedInstructionBits(Instruction &Inst);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000544
Chris Lattner35522b72010-01-04 07:12:23 +0000545 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000546 APInt &UndefElts, unsigned Depth = 0);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000547
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000548 Value *SimplifyVectorOp(BinaryOperator &Inst);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +0000549 Value *SimplifyBSwap(BinaryOperator &Inst);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000550
Sanjay Pateldb0938f2017-01-10 23:49:07 +0000551
552 /// Given a binary operator, cast instruction, or select which has a PHI node
553 /// as operand #0, see if we can fold the instruction into the PHI (which is
554 /// only possible if all operands to the PHI are constants).
Chris Lattnerea7131a2011-01-16 05:14:26 +0000555 Instruction *FoldOpIntoPhi(Instruction &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000556
Sanjay Pateldb0938f2017-01-10 23:49:07 +0000557 /// Given an instruction with a select as one operand and a constant as the
558 /// other operand, try to fold the binary operator into the select arguments.
559 /// This also works for Cast instructions, which obviously do not have a
560 /// second operand.
561 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
562
563 /// This is a convenience wrapper function for the above two functions.
564 Instruction *foldOpWithConstantIntoOperand(Instruction &I);
565
Chandler Carruth3a622162015-01-20 19:27:58 +0000566 /// \brief Try to rotate an operation below a PHI node, using PHI nodes for
567 /// its operands.
Chris Lattner35522b72010-01-04 07:12:23 +0000568 Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
569 Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
570 Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
571 Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
Sanjay Patel95334072015-09-27 20:34:31 +0000572 Instruction *FoldPHIArgZextsIntoPHI(PHINode &PN);
Chris Lattner35522b72010-01-04 07:12:23 +0000573
Robert Lougher2428a402016-12-14 17:49:19 +0000574 /// Helper function for FoldPHIArgXIntoPHI() to get debug location for the
575 /// folded operation.
576 DebugLoc PHIArgMergedDebugLoc(PHINode &PN);
577
Sanjay Patel43395062016-07-21 18:07:40 +0000578 Instruction *foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
579 ICmpInst::Predicate Cond, Instruction &I);
Pete Cooper980a9352016-08-12 17:13:28 +0000580 Instruction *foldAllocaCmp(ICmpInst &ICI, const AllocaInst *Alloca,
581 const Value *Other);
Sanjay Patel43395062016-07-21 18:07:40 +0000582 Instruction *foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
583 GlobalVariable *GV, CmpInst &ICI,
584 ConstantInt *AndCst = nullptr);
585 Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI,
586 Constant *RHSC);
Sanjay Patel43395062016-07-21 18:07:40 +0000587 Instruction *foldICmpAddOpConst(Instruction &ICI, Value *X, ConstantInt *CI,
588 ICmpInst::Predicate Pred);
589 Instruction *foldICmpWithCastAndCast(ICmpInst &ICI);
Sanjay Patelf58f68c2016-09-10 15:03:44 +0000590
Sanjay Patel3151dec2016-09-12 15:24:31 +0000591 Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp);
Sanjay Patel06b127a2016-09-15 14:37:50 +0000592 Instruction *foldICmpWithConstant(ICmpInst &Cmp);
Sanjay Patelf58f68c2016-09-10 15:03:44 +0000593 Instruction *foldICmpInstWithConstant(ICmpInst &Cmp);
Sanjay Patel10494b22016-09-16 16:10:22 +0000594 Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp);
595 Instruction *foldICmpBinOp(ICmpInst &Cmp);
596 Instruction *foldICmpEquality(ICmpInst &Cmp);
Sanjay Patela3f4f082016-08-16 17:54:36 +0000597
Sanjay Patelc9196c42016-08-22 21:24:29 +0000598 Instruction *foldICmpTruncConstant(ICmpInst &Cmp, Instruction *Trunc,
599 const APInt *C);
600 Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And,
601 const APInt *C);
602 Instruction *foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor,
603 const APInt *C);
604 Instruction *foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or,
605 const APInt *C);
606 Instruction *foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul,
607 const APInt *C);
608 Instruction *foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl,
609 const APInt *C);
610 Instruction *foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr,
611 const APInt *C);
612 Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
613 const APInt *C);
614 Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div,
615 const APInt *C);
616 Instruction *foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub,
617 const APInt *C);
618 Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add,
619 const APInt *C);
Sanjay Pateld3c7bb282016-08-26 16:42:33 +0000620 Instruction *foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And,
Sanjay Patel14e0e182016-08-26 18:28:46 +0000621 const APInt *C1);
622 Instruction *foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And,
Sanjay Patel9b40f982016-09-07 22:33:03 +0000623 const APInt *C1, const APInt *C2);
Sanjay Patel8da42cc2016-09-15 22:26:31 +0000624 Instruction *foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
625 const APInt &C2);
626 Instruction *foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
627 const APInt &C2);
Sanjay Patela3f4f082016-08-16 17:54:36 +0000628
Sanjay Patelf58f68c2016-09-10 15:03:44 +0000629 Instruction *foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp,
630 BinaryOperator *BO,
631 const APInt *C);
632 Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, const APInt *C);
Sanjay Patel43395062016-07-21 18:07:40 +0000633
Sanjay Patel453ceff2016-09-29 22:18:30 +0000634 // Helpers of visitSelectInst().
Sanjay Patelf7b851f2016-09-30 19:49:22 +0000635 Instruction *foldSelectExtConst(SelectInst &Sel);
Sanjay Patel453ceff2016-09-29 22:18:30 +0000636 Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
637 Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *);
638 Instruction *foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
639 Value *A, Value *B, Instruction &Outer,
640 SelectPatternFlavor SPF2, Value *C);
641 Instruction *foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
642
Chris Lattner35522b72010-01-04 07:12:23 +0000643 Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
644 ConstantInt *AndRHS, BinaryOperator &TheAnd);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000645
Chris Lattner35522b72010-01-04 07:12:23 +0000646 Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
647 bool isSub, Instruction &I);
Sanjay Patel85d79742016-08-31 19:49:56 +0000648 Value *insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi,
649 bool isSigned, bool Inside);
Chris Lattner35522b72010-01-04 07:12:23 +0000650 Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
Chad Rosiera00df492016-05-25 16:22:14 +0000651 Instruction *MatchBSwap(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000652 bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
Igor Laevsky900ffa32017-02-08 14:32:04 +0000653
654 Instruction *SimplifyElementAtomicMemCpy(ElementAtomicMemCpyInst *AMI);
Pete Cooper67cf9a72015-11-19 05:56:52 +0000655 Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
Chris Lattner35522b72010-01-04 07:12:23 +0000656 Instruction *SimplifyMemSet(MemSetInst *MI);
657
Chris Lattner229907c2011-07-18 04:54:35 +0000658 Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
Duncan Sands533c8ae2012-10-23 08:28:26 +0000659
Chandler Carruth3a622162015-01-20 19:27:58 +0000660 /// \brief Returns a value X such that Val = X * Scale, or null if none.
661 ///
662 /// If the multiplication is known not to overflow then NoSignedWrap is set.
Duncan Sands533c8ae2012-10-23 08:28:26 +0000663 Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
Chris Lattner35522b72010-01-04 07:12:23 +0000664};
665
Chris Lattner35522b72010-01-04 07:12:23 +0000666} // end namespace llvm.
667
Chandler Carruth964daaa2014-04-22 02:55:47 +0000668#undef DEBUG_TYPE
669
Chris Lattner35522b72010-01-04 07:12:23 +0000670#endif