blob: 1b0fe84dd4dda5cf5e2b996cbeea62d71edcb415 [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"
Daniel Berlin2c75c632017-04-26 20:56:07 +000020#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruth5175b9a2015-01-20 08:35:24 +000021#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth452a0072014-03-04 11:59:06 +000022#include "llvm/Analysis/TargetFolder.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000023#include "llvm/Analysis/ValueTracking.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000024#include "llvm/BinaryFormat/Dwarf.h"
Daniel Berlin2c75c632017-04-26 20:56:07 +000025#include "llvm/IR/DIBuilder.h"
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +000026#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/IRBuilder.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000028#include "llvm/IR/InstVisitor.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/IntrinsicInst.h"
30#include "llvm/IR/Operator.h"
Hal Finkel74c2f352014-09-07 12:44:26 +000031#include "llvm/IR/PatternMatch.h"
Chris Lattner35522b72010-01-04 07:12:23 +000032#include "llvm/Pass.h"
Craig Topper1a36b7d2017-05-15 06:39:41 +000033#include "llvm/Support/KnownBits.h"
Chandler Carruth83ba2692015-01-24 04:19:17 +000034#include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
Adrian Prantl47ea6472017-03-16 21:14:09 +000035#include "llvm/Transforms/Utils/Local.h"
Chris Lattner35522b72010-01-04 07:12:23 +000036
Chandler Carruth964daaa2014-04-22 02:55:47 +000037#define DEBUG_TYPE "instcombine"
38
Chris Lattner35522b72010-01-04 07:12:23 +000039namespace llvm {
Chandler Carruthd70cc602014-05-07 17:36:59 +000040class CallSite;
41class DataLayout;
Hal Finkel60db0582014-09-07 18:57:58 +000042class DominatorTree;
Chandler Carruthd70cc602014-05-07 17:36:59 +000043class TargetLibraryInfo;
44class DbgDeclareInst;
45class MemIntrinsic;
46class MemSetInst;
Jakub Staszak190db2f2013-01-14 23:16:36 +000047
Sanjay Patel73fc8dd2017-02-03 21:43:34 +000048/// Assign a complexity or rank value to LLVM Values. This is used to reduce
49/// the amount of pattern matching needed for compares and commutative
50/// instructions. For example, if we have:
51/// icmp ugt X, Constant
52/// or
53/// xor (add X, Constant), cast Z
54///
55/// We do not have to consider the commuted variants of these patterns because
56/// canonicalization based on complexity guarantees the above ordering.
Chandler Carruth3a622162015-01-20 19:27:58 +000057///
58/// This routine maps IR values to various complexity ranks:
59/// 0 -> undef
60/// 1 -> Constants
61/// 2 -> Other non-instructions
62/// 3 -> Arguments
Sanjay Patel73fc8dd2017-02-03 21:43:34 +000063/// 4 -> Cast and (f)neg/not instructions
64/// 5 -> Other instructions
Chris Lattner2188e402010-01-04 07:37:31 +000065static inline unsigned getComplexity(Value *V) {
66 if (isa<Instruction>(V)) {
Sanjay Patel73fc8dd2017-02-03 21:43:34 +000067 if (isa<CastInst>(V) || BinaryOperator::isNeg(V) ||
68 BinaryOperator::isFNeg(V) || BinaryOperator::isNot(V))
69 return 4;
70 return 5;
Chris Lattner2188e402010-01-04 07:37:31 +000071 }
Chandler Carruthd70cc602014-05-07 17:36:59 +000072 if (isa<Argument>(V))
73 return 3;
Chris Lattner2188e402010-01-04 07:37:31 +000074 return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
75}
Chris Lattner35522b72010-01-04 07:12:23 +000076
Sanjay Patelb2e70032017-05-17 14:21:19 +000077/// Predicate canonicalization reduces the number of patterns that need to be
78/// matched by other transforms. For example, we may swap the operands of a
79/// conditional branch or select to create a compare with a canonical (inverted)
80/// predicate which is then more likely to be matched with other values.
81static inline bool isCanonicalPredicate(CmpInst::Predicate Pred) {
82 switch (Pred) {
83 case CmpInst::ICMP_NE:
84 case CmpInst::ICMP_ULE:
85 case CmpInst::ICMP_SLE:
86 case CmpInst::ICMP_UGE:
87 case CmpInst::ICMP_SGE:
88 // TODO: There are 16 FCMP predicates. Should others be (not) canonical?
89 case CmpInst::FCMP_ONE:
90 case CmpInst::FCMP_OLE:
91 case CmpInst::FCMP_OGE:
92 return false;
93 default:
94 return true;
95 }
96}
97
Sanjay Patele800df8e2017-06-22 15:28:01 +000098/// Return the source operand of a potentially bitcasted value while optionally
99/// checking if it has one use. If there is no bitcast or the one use check is
100/// not met, return the input value itself.
101static inline Value *peekThroughBitcast(Value *V, bool OneUseOnly = false) {
102 if (auto *BitCast = dyn_cast<BitCastInst>(V))
103 if (!OneUseOnly || BitCast->hasOneUse())
104 return BitCast->getOperand(0);
105
106 // V is not a bitcast or V has more than one use and OneUseOnly is true.
107 return V;
108}
109
Chandler Carruth3a622162015-01-20 19:27:58 +0000110/// \brief Add one to a Constant
Benjamin Kramer970f4952014-01-19 16:56:10 +0000111static inline Constant *AddOne(Constant *C) {
112 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
113}
Chandler Carruth3a622162015-01-20 19:27:58 +0000114/// \brief Subtract one from a Constant
Benjamin Kramer970f4952014-01-19 16:56:10 +0000115static inline Constant *SubOne(Constant *C) {
116 return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
117}
118
Sanjoy Das82ea3d42015-02-24 00:08:41 +0000119/// \brief Return true if the specified value is free to invert (apply ~ to).
120/// This happens in cases where the ~ can be eliminated. If WillInvertAllUses
121/// is true, work under the assumption that the caller intends to remove all
122/// uses of V and only keep uses of ~V.
123///
124static inline bool IsFreeToInvert(Value *V, bool WillInvertAllUses) {
125 // ~(~(X)) -> X.
126 if (BinaryOperator::isNot(V))
127 return true;
128
129 // Constants can be considered to be not'ed values.
130 if (isa<ConstantInt>(V))
131 return true;
132
Sanjay Patel611f9f92016-10-27 17:30:50 +0000133 // A vector of constant integers can be inverted easily.
134 Constant *CV;
135 if (V->getType()->isVectorTy() && match(V, PatternMatch::m_Constant(CV))) {
136 unsigned NumElts = V->getType()->getVectorNumElements();
137 for (unsigned i = 0; i != NumElts; ++i) {
138 Constant *Elt = CV->getAggregateElement(i);
139 if (!Elt)
140 return false;
141
142 if (isa<UndefValue>(Elt))
143 continue;
144
145 if (!isa<ConstantInt>(Elt))
146 return false;
147 }
148 return true;
149 }
150
Sanjoy Das82ea3d42015-02-24 00:08:41 +0000151 // Compares can be inverted if all of their uses are being modified to use the
152 // ~V.
153 if (isa<CmpInst>(V))
154 return WillInvertAllUses;
155
156 // If `V` is of the form `A + Constant` then `-1 - V` can be folded into `(-1
157 // - Constant) - A` if we are willing to invert all of the uses.
158 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
159 if (BO->getOpcode() == Instruction::Add ||
160 BO->getOpcode() == Instruction::Sub)
161 if (isa<Constant>(BO->getOperand(0)) || isa<Constant>(BO->getOperand(1)))
162 return WillInvertAllUses;
163
164 return false;
165}
166
Sanjoy Dasb0984472015-04-08 04:27:22 +0000167
168/// \brief Specific patterns of overflow check idioms that we match.
169enum OverflowCheckFlavor {
170 OCF_UNSIGNED_ADD,
171 OCF_SIGNED_ADD,
172 OCF_UNSIGNED_SUB,
173 OCF_SIGNED_SUB,
174 OCF_UNSIGNED_MUL,
175 OCF_SIGNED_MUL,
176
177 OCF_INVALID
178};
179
180/// \brief Returns the OverflowCheckFlavor corresponding to a overflow_with_op
181/// intrinsic.
182static inline OverflowCheckFlavor
183IntrinsicIDToOverflowCheckFlavor(unsigned ID) {
184 switch (ID) {
185 default:
186 return OCF_INVALID;
187 case Intrinsic::uadd_with_overflow:
188 return OCF_UNSIGNED_ADD;
189 case Intrinsic::sadd_with_overflow:
190 return OCF_SIGNED_ADD;
191 case Intrinsic::usub_with_overflow:
192 return OCF_UNSIGNED_SUB;
193 case Intrinsic::ssub_with_overflow:
194 return OCF_SIGNED_SUB;
195 case Intrinsic::umul_with_overflow:
196 return OCF_UNSIGNED_MUL;
197 case Intrinsic::smul_with_overflow:
198 return OCF_SIGNED_MUL;
199 }
200}
201
Chandler Carruth3a622162015-01-20 19:27:58 +0000202/// \brief The core instruction combiner logic.
203///
204/// This class provides both the logic to recursively visit instructions and
Craig Topper28ec3462016-12-28 03:12:42 +0000205/// combine them.
Duncan Sands6c5e4352010-05-11 20:16:09 +0000206class LLVM_LIBRARY_VISIBILITY InstCombiner
Chandler Carruth1edb9d62015-01-20 22:44:35 +0000207 : public InstVisitor<InstCombiner, Instruction *> {
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000208 // FIXME: These members shouldn't be public.
Chris Lattner35522b72010-01-04 07:12:23 +0000209public:
Chandler Carruth3a622162015-01-20 19:27:58 +0000210 /// \brief A worklist of the instructions that need to be simplified.
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000211 InstCombineWorklist &Worklist;
Chris Lattner35522b72010-01-04 07:12:23 +0000212
Chandler Carruth3a622162015-01-20 19:27:58 +0000213 /// \brief An IRBuilder that automatically inserts new instructions into the
214 /// worklist.
Justin Bogner19dd0da2016-08-04 23:41:01 +0000215 typedef IRBuilder<TargetFolder, IRBuilderCallbackInserter> BuilderTy;
Chris Lattner35522b72010-01-04 07:12:23 +0000216 BuilderTy *Builder;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000217
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000218private:
219 // Mode in which we are running the combiner.
220 const bool MinimizeSize;
Matthias Braunc31032d2016-03-09 18:47:11 +0000221 /// Enable combines that trigger rarely but are costly in compiletime.
222 const bool ExpensiveCombines;
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000223
Bjorn Steinbrink83505342015-07-10 06:55:49 +0000224 AliasAnalysis *AA;
225
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000226 // Required analyses.
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000227 AssumptionCache &AC;
Justin Bogner99798402016-08-05 01:06:44 +0000228 TargetLibraryInfo &TLI;
229 DominatorTree &DT;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000230 const DataLayout &DL;
Daniel Berlin2c75c632017-04-26 20:56:07 +0000231 const SimplifyQuery SQ;
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000232 // Optional analyses. When non-null, these can both be used to do better
233 // combining and will be updated to reflect any changes.
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000234 LoopInfo *LI;
235
236 bool MadeIRChange;
Chris Lattner35522b72010-01-04 07:12:23 +0000237
238public:
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000239 InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder,
Matthias Braunc31032d2016-03-09 18:47:11 +0000240 bool MinimizeSize, bool ExpensiveCombines, AliasAnalysis *AA,
Daniel Berlin2c75c632017-04-26 20:56:07 +0000241 AssumptionCache &AC, TargetLibraryInfo &TLI, DominatorTree &DT,
242 const DataLayout &DL, LoopInfo *LI)
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000243 : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000244 ExpensiveCombines(ExpensiveCombines), AA(AA), AC(AC), TLI(TLI), DT(DT),
Daniel Berlin2c75c632017-04-26 20:56:07 +0000245 DL(DL), SQ(DL, &TLI, &DT, &AC), LI(LI), MadeIRChange(false) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +0000246
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000247 /// \brief Run the combiner over the entire worklist until it is empty.
248 ///
249 /// \returns true if the IR is changed.
250 bool run();
Chris Lattner35522b72010-01-04 07:12:23 +0000251
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000252 AssumptionCache &getAssumptionCache() const { return AC; }
253
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000254 const DataLayout &getDataLayout() const { return DL; }
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000255
Justin Bogner99798402016-08-05 01:06:44 +0000256 DominatorTree &getDominatorTree() const { return DT; }
Chris Lattner35522b72010-01-04 07:12:23 +0000257
Chandler Carruth5175b9a2015-01-20 08:35:24 +0000258 LoopInfo *getLoopInfo() const { return LI; }
259
Justin Bogner99798402016-08-05 01:06:44 +0000260 TargetLibraryInfo &getTargetLibraryInfo() const { return TLI; }
Chad Rosier43a33062011-12-02 01:26:24 +0000261
Chris Lattner35522b72010-01-04 07:12:23 +0000262 // Visitation implementation - Implement instruction combining for different
263 // instruction types. The semantics are as follows:
264 // Return Value:
265 // null - No change was made
266 // I - Change was made, I is still valid, I may be dead though
267 // otherwise - Change was made, replace I with returned instruction
268 //
269 Instruction *visitAdd(BinaryOperator &I);
270 Instruction *visitFAdd(BinaryOperator &I);
Chris Lattner229907c2011-07-18 04:54:35 +0000271 Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
Chris Lattner35522b72010-01-04 07:12:23 +0000272 Instruction *visitSub(BinaryOperator &I);
273 Instruction *visitFSub(BinaryOperator &I);
274 Instruction *visitMul(BinaryOperator &I);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000275 Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000276 Instruction *InsertBefore);
Chris Lattner35522b72010-01-04 07:12:23 +0000277 Instruction *visitFMul(BinaryOperator &I);
278 Instruction *visitURem(BinaryOperator &I);
279 Instruction *visitSRem(BinaryOperator &I);
280 Instruction *visitFRem(BinaryOperator &I);
281 bool SimplifyDivRemOfSelect(BinaryOperator &I);
282 Instruction *commonRemTransforms(BinaryOperator &I);
283 Instruction *commonIRemTransforms(BinaryOperator &I);
284 Instruction *commonDivTransforms(BinaryOperator &I);
285 Instruction *commonIDivTransforms(BinaryOperator &I);
286 Instruction *visitUDiv(BinaryOperator &I);
287 Instruction *visitSDiv(BinaryOperator &I);
Frits van Bommel2a559512011-01-29 17:50:27 +0000288 Instruction *visitFDiv(BinaryOperator &I);
Erik Ecksteind1817522014-12-03 10:39:15 +0000289 Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
Chris Lattner35522b72010-01-04 07:12:23 +0000290 Instruction *visitAnd(BinaryOperator &I);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000291 Instruction *visitOr(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000292 Instruction *visitXor(BinaryOperator &I);
293 Instruction *visitShl(BinaryOperator &I);
294 Instruction *visitAShr(BinaryOperator &I);
295 Instruction *visitLShr(BinaryOperator &I);
296 Instruction *commonShiftTransforms(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000297 Instruction *visitFCmpInst(FCmpInst &I);
298 Instruction *visitICmpInst(ICmpInst &I);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000299 Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattner35522b72010-01-04 07:12:23 +0000300 BinaryOperator &I);
301 Instruction *commonCastTransforms(CastInst &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000302 Instruction *commonPointerCastTransforms(CastInst &CI);
303 Instruction *visitTrunc(TruncInst &CI);
304 Instruction *visitZExt(ZExtInst &CI);
305 Instruction *visitSExt(SExtInst &CI);
306 Instruction *visitFPTrunc(FPTruncInst &CI);
307 Instruction *visitFPExt(CastInst &CI);
308 Instruction *visitFPToUI(FPToUIInst &FI);
309 Instruction *visitFPToSI(FPToSIInst &FI);
310 Instruction *visitUIToFP(CastInst &CI);
311 Instruction *visitSIToFP(CastInst &CI);
312 Instruction *visitPtrToInt(PtrToIntInst &CI);
313 Instruction *visitIntToPtr(IntToPtrInst &CI);
314 Instruction *visitBitCast(BitCastInst &CI);
Matt Arsenaulta9e95ab2013-11-15 05:45:08 +0000315 Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
Mehdi Aminib9a0fa42015-02-16 21:47:54 +0000316 Instruction *FoldItoFPtoI(Instruction &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000317 Instruction *visitSelectInst(SelectInst &SI);
Chris Lattner35522b72010-01-04 07:12:23 +0000318 Instruction *visitCallInst(CallInst &CI);
319 Instruction *visitInvokeInst(InvokeInst &II);
320
321 Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
322 Instruction *visitPHINode(PHINode &PN);
323 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
324 Instruction *visitAllocaInst(AllocaInst &AI);
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000325 Instruction *visitAllocSite(Instruction &FI);
Gabor Greif75f69432010-06-24 12:21:15 +0000326 Instruction *visitFree(CallInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000327 Instruction *visitLoadInst(LoadInst &LI);
328 Instruction *visitStoreInst(StoreInst &SI);
329 Instruction *visitBranchInst(BranchInst &BI);
Davide Italianoaec46172017-01-31 18:09:05 +0000330 Instruction *visitFenceInst(FenceInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000331 Instruction *visitSwitchInst(SwitchInst &SI);
Hal Finkel93873cc12014-09-07 21:28:34 +0000332 Instruction *visitReturnInst(ReturnInst &RI);
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000333 Instruction *visitInsertValueInst(InsertValueInst &IV);
Chris Lattner35522b72010-01-04 07:12:23 +0000334 Instruction *visitInsertElementInst(InsertElementInst &IE);
335 Instruction *visitExtractElementInst(ExtractElementInst &EI);
336 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
337 Instruction *visitExtractValueInst(ExtractValueInst &EV);
Duncan Sands5c055792011-09-30 13:12:16 +0000338 Instruction *visitLandingPadInst(LandingPadInst &LI);
Arnaud A. de Grandmaison333ef382016-05-10 09:24:49 +0000339 Instruction *visitVAStartInst(VAStartInst &I);
340 Instruction *visitVACopyInst(VACopyInst &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000341
Sanjay Patel53523312016-09-12 14:25:46 +0000342 /// Specify what to return for unhandled instructions.
Craig Toppere73658d2014-04-28 04:05:08 +0000343 Instruction *visitInstruction(Instruction &I) { return nullptr; }
Chris Lattner35522b72010-01-04 07:12:23 +0000344
Sanjay Patel53523312016-09-12 14:25:46 +0000345 /// True when DB dominates all uses of DI except UI.
346 /// UI must be in the same block as DI.
347 /// The routine checks that the DI parent and DB are different.
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +0000348 bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
349 const BasicBlock *DB) const;
350
Sanjay Patel53523312016-09-12 14:25:46 +0000351 /// Try to replace select with select operand SIOpd in SI-ICmp sequence.
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +0000352 bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
353 const unsigned SIOpd);
354
Yaxun Liuba01ed02017-02-10 21:46:07 +0000355 /// Try to replace instruction \p I with value \p V which are pointers
356 /// in different address space.
357 /// \return true if successful.
358 bool replacePointer(Instruction &I, Value *V);
359
Chris Lattner35522b72010-01-04 07:12:23 +0000360private:
Sanjay Patel2217f752017-01-31 17:25:42 +0000361 bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
362 bool shouldChangeType(Type *From, Type *To) const;
Chris Lattner2188e402010-01-04 07:37:31 +0000363 Value *dyn_castNegVal(Value *V) const;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000364 Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
David Blaikie87ca1b62015-03-27 20:56:11 +0000365 Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000366 SmallVectorImpl<Value *> &NewIndices);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000367
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000368 /// Classify whether a cast is worth optimizing.
Chandler Carruth3a622162015-01-20 19:27:58 +0000369 ///
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000370 /// This is a helper to decide whether the simplification of
371 /// logic(cast(A), cast(B)) to cast(logic(A, B)) should be performed.
372 ///
373 /// \param CI The cast we are interested in.
374 ///
375 /// \return true if this cast actually results in any code being generated and
376 /// if it cannot already be eliminated by some other transformation.
377 bool shouldOptimizeCast(CastInst *CI);
Chris Lattner2188e402010-01-04 07:37:31 +0000378
Sanjoy Dasb0984472015-04-08 04:27:22 +0000379 /// \brief Try to optimize a sequence of instructions checking if an operation
380 /// on LHS and RHS overflows.
381 ///
Sanjoy Das6f5dca72015-08-28 19:09:31 +0000382 /// If this overflow check is done via one of the overflow check intrinsics,
383 /// then CtxI has to be the call instruction calling that intrinsic. If this
384 /// overflow check is done by arithmetic followed by a compare, then CtxI has
385 /// to be the arithmetic instruction.
386 ///
Sanjoy Dasb0984472015-04-08 04:27:22 +0000387 /// If a simplification is possible, stores the simplified result of the
388 /// operation in OperationResult and result of the overflow check in
389 /// OverflowResult, and return true. If no simplification is possible,
390 /// returns false.
391 bool OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS, Value *RHS,
392 Instruction &CtxI, Value *&OperationResult,
393 Constant *&OverflowResult);
394
Chris Lattner35522b72010-01-04 07:12:23 +0000395 Instruction *visitCallSite(CallSite CS);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000396 Instruction *tryOptimizeCall(CallInst *CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000397 bool transformConstExprCastCall(CallSite CS);
Duncan Sandsa0984362011-09-06 13:37:06 +0000398 Instruction *transformCallThroughTrampoline(CallSite CS,
399 IntrinsicInst *Tramp);
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000400
401 /// Transform (zext icmp) to bitwise / integer operations in order to
402 /// eliminate it.
403 ///
404 /// \param ICI The icmp of the (zext icmp) pair we are interested in.
405 /// \parem CI The zext of the (zext icmp) pair we are interested in.
406 /// \param DoTransform Pass false to just test whether the given (zext icmp)
407 /// would be transformed. Pass true to actually perform the transformation.
408 ///
409 /// \return null if the transformation cannot be performed. If the
410 /// transformation can be performed the new instruction that replaces the
411 /// (zext icmp) pair will be returned (if \p DoTransform is false the
412 /// unmodified \p ICI will be returned in this case).
413 Instruction *transformZExtICmp(ICmpInst *ICI, ZExtInst &CI,
414 bool DoTransform = true);
415
Benjamin Kramer398b8c52011-04-01 20:09:03 +0000416 Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
Craig Topper2b1fc322017-05-22 06:25:31 +0000417 bool willNotOverflowSignedAdd(const Value *LHS, const Value *RHS,
418 const Instruction &CxtI) const {
Craig Topperbb973722017-05-15 02:44:08 +0000419 return computeOverflowForSignedAdd(LHS, RHS, &CxtI) ==
420 OverflowResult::NeverOverflows;
421 };
Craig Topper2b1fc322017-05-22 06:25:31 +0000422 bool willNotOverflowUnsignedAdd(const Value *LHS, const Value *RHS,
423 const Instruction &CxtI) const {
Craig Topperbb973722017-05-15 02:44:08 +0000424 return computeOverflowForUnsignedAdd(LHS, RHS, &CxtI) ==
425 OverflowResult::NeverOverflows;
426 };
Craig Topper2b1fc322017-05-22 06:25:31 +0000427 bool willNotOverflowSignedSub(const Value *LHS, const Value *RHS,
428 const Instruction &CxtI) const;
429 bool willNotOverflowUnsignedSub(const Value *LHS, const Value *RHS,
430 const Instruction &CxtI) const;
431 bool willNotOverflowSignedMul(const Value *LHS, const Value *RHS,
432 const Instruction &CxtI) const;
433 bool willNotOverflowUnsignedMul(const Value *LHS, const Value *RHS,
434 const Instruction &CxtI) const {
Craig Topperbb973722017-05-15 02:44:08 +0000435 return computeOverflowForUnsignedMul(LHS, RHS, &CxtI) ==
436 OverflowResult::NeverOverflows;
437 };
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000438 Value *EmitGEPOffset(User *GEP);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000439 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
Nick Lewyckya2b77202013-05-31 00:59:42 +0000440 Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
Sanjay Patel40e7ba02016-02-23 16:36:07 +0000441 Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
Sanjay Patelaa8b28e2016-11-30 20:48:54 +0000442 Instruction *shrinkBitwiseLogic(TruncInst &Trunc);
Guozhi Weiae541f62016-10-25 20:43:42 +0000443 Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
Chris Lattner35522b72010-01-04 07:12:23 +0000444
Tobias Grosser8ef834c2016-07-19 09:06:08 +0000445 /// Determine if a pair of casts can be replaced by a single cast.
446 ///
447 /// \param CI1 The first of a pair of casts.
448 /// \param CI2 The second of a pair of casts.
449 ///
450 /// \return 0 if the cast pair cannot be eliminated, otherwise returns an
451 /// Instruction::CastOps value for a cast that can replace the pair, casting
452 /// CI1->getSrcTy() to CI2->getDstTy().
453 ///
454 /// \see CastInst::isEliminableCastPair
455 Instruction::CastOps isEliminableCastPair(const CastInst *CI1,
456 const CastInst *CI2);
457
Craig Topperda6ea0d2017-06-16 05:10:37 +0000458 Value *foldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &CxtI);
Sanjay Patel5e456b92017-05-18 20:53:16 +0000459 Value *foldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Craig Topperf2d3e6d2017-06-15 19:09:51 +0000460 Value *foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &CxtI);
Sanjay Patel5e456b92017-05-18 20:53:16 +0000461 Value *foldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
462 Value *foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS);
463
Craig Topperda6ea0d2017-06-16 05:10:37 +0000464 Value *foldAndOrOfICmpsOfAndWithPow2(ICmpInst *LHS, ICmpInst *RHS,
465 bool JoinedByAnd, Instruction &CxtI);
Chris Lattner35522b72010-01-04 07:12:23 +0000466public:
Chandler Carruth3a622162015-01-20 19:27:58 +0000467 /// \brief Inserts an instruction \p New before instruction \p Old
468 ///
469 /// Also adds the new instruction to the worklist and returns \p New so that
470 /// it is suitable for use as the return from the visitation patterns.
Chris Lattner35522b72010-01-04 07:12:23 +0000471 Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
Craig Toppere73658d2014-04-28 04:05:08 +0000472 assert(New && !New->getParent() &&
Chris Lattner35522b72010-01-04 07:12:23 +0000473 "New instruction already inserted into a basic block!");
474 BasicBlock *BB = Old.getParent();
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000475 BB->getInstList().insert(Old.getIterator(), New); // Insert inst
Chris Lattner35522b72010-01-04 07:12:23 +0000476 Worklist.Add(New);
477 return New;
478 }
Eli Friedman6efb64e2011-05-19 01:20:42 +0000479
Chandler Carruth3a622162015-01-20 19:27:58 +0000480 /// \brief Same as InsertNewInstBefore, but also sets the debug loc.
Eli Friedman6efb64e2011-05-19 01:20:42 +0000481 Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
482 New->setDebugLoc(Old.getDebugLoc());
483 return InsertNewInstBefore(New, Old);
484 }
485
Chandler Carruth3a622162015-01-20 19:27:58 +0000486 /// \brief A combiner-aware RAUW-like routine.
487 ///
488 /// This method is to be used when an instruction is found to be dead,
Sanjay Patelf2ea8a22016-01-06 00:23:12 +0000489 /// replaceable with another preexisting expression. Here we add all uses of
Chandler Carruth3a622162015-01-20 19:27:58 +0000490 /// I to the worklist, replace all uses of I with the new value, then return
491 /// I, so that the inst combiner will know that I was modified.
Sanjay Patel4b198802016-02-01 22:23:39 +0000492 Instruction *replaceInstUsesWith(Instruction &I, Value *V) {
Owen Anderson51b75b8c2015-03-10 05:13:47 +0000493 // If there are no uses to replace, then we return nullptr to indicate that
494 // no changes were made to the program.
495 if (I.use_empty()) return nullptr;
496
Chandler Carruthd70cc602014-05-07 17:36:59 +0000497 Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000498
Chris Lattner35522b72010-01-04 07:12:23 +0000499 // If we are replacing the instruction with itself, this must be in a
500 // segment of unreachable code, so just clobber the instruction.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000501 if (&I == V)
Chris Lattner35522b72010-01-04 07:12:23 +0000502 V = UndefValue::get(I.getType());
Frits van Bommeld14d9912011-03-27 23:32:31 +0000503
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000504 DEBUG(dbgs() << "IC: Replacing " << I << "\n"
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000505 << " with " << *V << '\n');
Frits van Bommeld14d9912011-03-27 23:32:31 +0000506
Chris Lattner35522b72010-01-04 07:12:23 +0000507 I.replaceAllUsesWith(V);
508 return &I;
509 }
510
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000511 /// Creates a result tuple for an overflow intrinsic \p II with a given
Sanjoy Dasb0984472015-04-08 04:27:22 +0000512 /// \p Result and a constant \p Overflow value.
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000513 Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result,
Sanjoy Dasb0984472015-04-08 04:27:22 +0000514 Constant *Overflow) {
515 Constant *V[] = {UndefValue::get(Result->getType()), Overflow};
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000516 StructType *ST = cast<StructType>(II->getType());
517 Constant *Struct = ConstantStruct::get(ST, V);
518 return InsertValueInst::Create(Struct, Result, 0);
519 }
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000520
Chandler Carruth3a622162015-01-20 19:27:58 +0000521 /// \brief Combiner aware instruction erasure.
522 ///
523 /// When dealing with an instruction that has side effects or produces a void
524 /// value, we can't rely on DCE to delete the instruction. Instead, visit
525 /// methods should return the value returned by this function.
Sanjay Patel4b198802016-02-01 22:23:39 +0000526 Instruction *eraseInstFromFunction(Instruction &I) {
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000527 DEBUG(dbgs() << "IC: ERASE " << I << '\n');
Adrian Prantlfa9e84e2017-03-16 20:11:54 +0000528 assert(I.use_empty() && "Cannot erase instruction that is used!");
Adrian Prantl47ea6472017-03-16 21:14:09 +0000529 salvageDebugInfo(I);
530
Chris Lattner35522b72010-01-04 07:12:23 +0000531 // Make sure that we reprocess all operands now that we reduced their
532 // use counts.
533 if (I.getNumOperands() < 8) {
Sanjay Patel8af7fbc32016-01-31 16:34:48 +0000534 for (Use &Operand : I.operands())
535 if (auto *Inst = dyn_cast<Instruction>(Operand))
536 Worklist.Add(Inst);
Chris Lattner35522b72010-01-04 07:12:23 +0000537 }
538 Worklist.Remove(&I);
539 I.eraseFromParent();
540 MadeIRChange = true;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000541 return nullptr; // Don't do anything with FI
Chris Lattner35522b72010-01-04 07:12:23 +0000542 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000543
Craig Topper2b1fc322017-05-22 06:25:31 +0000544 void computeKnownBits(const Value *V, KnownBits &Known,
545 unsigned Depth, const Instruction *CxtI) const {
Craig Topper26c41592017-05-15 02:30:27 +0000546 llvm::computeKnownBits(V, Known, DL, Depth, &AC, CxtI, &DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000547 }
Craig Topper2b1fc322017-05-22 06:25:31 +0000548 KnownBits computeKnownBits(const Value *V, unsigned Depth,
549 const Instruction *CxtI) const {
Craig Topper1a36b7d2017-05-15 06:39:41 +0000550 return llvm::computeKnownBits(V, DL, Depth, &AC, CxtI, &DT);
551 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000552
Craig Topperd4039f72017-05-25 21:51:12 +0000553 bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero = false,
554 unsigned Depth = 0,
555 const Instruction *CxtI = nullptr) {
556 return llvm::isKnownToBeAPowerOfTwo(V, DL, OrZero, Depth, &AC, CxtI, &DT);
557 }
558
Craig Topper2b1fc322017-05-22 06:25:31 +0000559 bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth = 0,
560 const Instruction *CxtI = nullptr) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000561 return llvm::MaskedValueIsZero(V, Mask, DL, Depth, &AC, CxtI, &DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000562 }
Craig Topper2b1fc322017-05-22 06:25:31 +0000563 unsigned ComputeNumSignBits(const Value *Op, unsigned Depth = 0,
564 const Instruction *CxtI = nullptr) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000565 return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000566 }
Craig Topper2b1fc322017-05-22 06:25:31 +0000567 OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
568 const Value *RHS,
569 const Instruction *CxtI) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000570 return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
David Majnemer491331a2015-01-02 07:29:43 +0000571 }
Craig Topper2b1fc322017-05-22 06:25:31 +0000572 OverflowResult computeOverflowForUnsignedAdd(const Value *LHS,
573 const Value *RHS,
574 const Instruction *CxtI) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000575 return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
David Majnemer5310c1e2015-01-07 00:39:50 +0000576 }
Craig Topperbb973722017-05-15 02:44:08 +0000577 OverflowResult computeOverflowForSignedAdd(const Value *LHS,
578 const Value *RHS,
579 const Instruction *CxtI) const {
580 return llvm::computeOverflowForSignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
581 }
Chris Lattner35522b72010-01-04 07:12:23 +0000582
Davide Italiano2133bf52017-02-07 17:56:50 +0000583 /// Maximum size of array considered when transforming.
David Blaikie4c01af22017-02-07 18:58:17 +0000584 uint64_t MaxArraySizeForCombine;
Davide Italiano2133bf52017-02-07 17:56:50 +0000585
Chris Lattner35522b72010-01-04 07:12:23 +0000586private:
Chandler Carruth3a622162015-01-20 19:27:58 +0000587 /// \brief Performs a few simplifications for operators which are associative
588 /// or commutative.
Duncan Sands641baf12010-11-13 15:10:37 +0000589 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000590
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000591 /// \brief Tries to simplify binary operations which some other binary
592 /// operation distributes over.
Chandler Carruth3a622162015-01-20 19:27:58 +0000593 ///
594 /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
595 /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
596 /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
597 /// value, or null if it didn't simplify.
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000598 Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
Duncan Sandsadc7771f2010-11-23 14:23:47 +0000599
Daniel Berlin2c75c632017-04-26 20:56:07 +0000600 /// This tries to simplify binary operations by factorizing out common terms
601 /// (e. g. "(A*B)+(A*C)" -> "A*(B+C)").
602 Value *tryFactorization(InstCombiner::BuilderTy *, BinaryOperator &,
603 Instruction::BinaryOps, Value *, Value *, Value *,
604 Value *);
605
Anna Thomasd67165c2017-06-23 13:41:45 +0000606 /// Match a select chain which produces one of three values based on whether
607 /// the LHS is less than, equal to, or greater than RHS respectively.
608 /// Return true if we matched a three way compare idiom. The LHS, RHS, Less,
609 /// Equal and Greater values are saved in the matching process and returned to
610 /// the caller.
611 bool matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, Value *&RHS,
612 ConstantInt *&Less, ConstantInt *&Equal,
613 ConstantInt *&Greater);
614
Chandler Carruth3a622162015-01-20 19:27:58 +0000615 /// \brief Attempts to replace V with a simpler value based on the demanded
616 /// bits.
Craig Topperb45eabc2017-04-26 16:39:58 +0000617 Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, KnownBits &Known,
618 unsigned Depth, Instruction *CxtI);
Craig Topper47596dd2017-03-25 06:52:52 +0000619 bool SimplifyDemandedBits(Instruction *I, unsigned Op,
Craig Topperb45eabc2017-04-26 16:39:58 +0000620 const APInt &DemandedMask, KnownBits &Known,
621 unsigned Depth = 0);
Craig Topperb0076fe2017-04-12 18:05:21 +0000622 /// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
623 /// bits. It also tries to handle simplifications that can be done based on
624 /// DemandedMask, but without modifying the Instruction.
625 Value *SimplifyMultipleUseDemandedBits(Instruction *I,
626 const APInt &DemandedMask,
Craig Topperb45eabc2017-04-26 16:39:58 +0000627 KnownBits &Known,
Craig Topperb0076fe2017-04-12 18:05:21 +0000628 unsigned Depth, Instruction *CxtI);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000629 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
630 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
Sanjay Patelcc663b82017-04-20 22:37:01 +0000631 Value *simplifyShrShlDemandedBits(
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000632 Instruction *Shr, const APInt &ShrOp1, Instruction *Shl,
Craig Topperb45eabc2017-04-26 16:39:58 +0000633 const APInt &ShlOp1, const APInt &DemandedMask, KnownBits &Known);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000634
Chandler Carruth3a622162015-01-20 19:27:58 +0000635 /// \brief Tries to simplify operands to an integer instruction based on its
636 /// demanded bits.
Chris Lattner35522b72010-01-04 07:12:23 +0000637 bool SimplifyDemandedInstructionBits(Instruction &Inst);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000638
Chris Lattner35522b72010-01-04 07:12:23 +0000639 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000640 APInt &UndefElts, unsigned Depth = 0);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000641
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000642 Value *SimplifyVectorOp(BinaryOperator &Inst);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +0000643 Value *SimplifyBSwap(BinaryOperator &Inst);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000644
Sanjay Pateldb0938f2017-01-10 23:49:07 +0000645
646 /// Given a binary operator, cast instruction, or select which has a PHI node
647 /// as operand #0, see if we can fold the instruction into the PHI (which is
648 /// only possible if all operands to the PHI are constants).
Craig Topperfb71b7d2017-04-14 19:20:12 +0000649 Instruction *foldOpIntoPhi(Instruction &I, PHINode *PN);
Chris Lattner35522b72010-01-04 07:12:23 +0000650
Sanjay Pateldb0938f2017-01-10 23:49:07 +0000651 /// Given an instruction with a select as one operand and a constant as the
652 /// other operand, try to fold the binary operator into the select arguments.
653 /// This also works for Cast instructions, which obviously do not have a
654 /// second operand.
655 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
656
657 /// This is a convenience wrapper function for the above two functions.
Craig Topperd0b053d2017-04-03 07:08:08 +0000658 Instruction *foldOpWithConstantIntoOperand(BinaryOperator &I);
Sanjay Pateldb0938f2017-01-10 23:49:07 +0000659
Chandler Carruth3a622162015-01-20 19:27:58 +0000660 /// \brief Try to rotate an operation below a PHI node, using PHI nodes for
661 /// its operands.
Chris Lattner35522b72010-01-04 07:12:23 +0000662 Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
663 Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
664 Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
665 Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
Sanjay Patel95334072015-09-27 20:34:31 +0000666 Instruction *FoldPHIArgZextsIntoPHI(PHINode &PN);
Chris Lattner35522b72010-01-04 07:12:23 +0000667
Robert Lougher2428a402016-12-14 17:49:19 +0000668 /// Helper function for FoldPHIArgXIntoPHI() to get debug location for the
669 /// folded operation.
670 DebugLoc PHIArgMergedDebugLoc(PHINode &PN);
671
Sanjay Patel43395062016-07-21 18:07:40 +0000672 Instruction *foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
673 ICmpInst::Predicate Cond, Instruction &I);
Pete Cooper980a9352016-08-12 17:13:28 +0000674 Instruction *foldAllocaCmp(ICmpInst &ICI, const AllocaInst *Alloca,
675 const Value *Other);
Sanjay Patel43395062016-07-21 18:07:40 +0000676 Instruction *foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
677 GlobalVariable *GV, CmpInst &ICI,
678 ConstantInt *AndCst = nullptr);
679 Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI,
680 Constant *RHSC);
Sanjay Patel43395062016-07-21 18:07:40 +0000681 Instruction *foldICmpAddOpConst(Instruction &ICI, Value *X, ConstantInt *CI,
682 ICmpInst::Predicate Pred);
683 Instruction *foldICmpWithCastAndCast(ICmpInst &ICI);
Sanjay Patelf58f68c2016-09-10 15:03:44 +0000684
Sanjay Patel3151dec2016-09-12 15:24:31 +0000685 Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp);
Sanjay Patel06b127a2016-09-15 14:37:50 +0000686 Instruction *foldICmpWithConstant(ICmpInst &Cmp);
Sanjay Patelf58f68c2016-09-10 15:03:44 +0000687 Instruction *foldICmpInstWithConstant(ICmpInst &Cmp);
Sanjay Patel10494b22016-09-16 16:10:22 +0000688 Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp);
689 Instruction *foldICmpBinOp(ICmpInst &Cmp);
690 Instruction *foldICmpEquality(ICmpInst &Cmp);
Sanjay Patela3f4f082016-08-16 17:54:36 +0000691
Anna Thomasd67165c2017-06-23 13:41:45 +0000692 Instruction *foldICmpSelectConstant(ICmpInst &Cmp, Instruction *Select,
693 ConstantInt *C);
Sanjay Patelc9196c42016-08-22 21:24:29 +0000694 Instruction *foldICmpTruncConstant(ICmpInst &Cmp, Instruction *Trunc,
695 const APInt *C);
696 Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And,
697 const APInt *C);
698 Instruction *foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor,
699 const APInt *C);
700 Instruction *foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or,
701 const APInt *C);
702 Instruction *foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul,
703 const APInt *C);
704 Instruction *foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl,
705 const APInt *C);
706 Instruction *foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr,
707 const APInt *C);
708 Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
709 const APInt *C);
710 Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div,
711 const APInt *C);
712 Instruction *foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub,
713 const APInt *C);
714 Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add,
715 const APInt *C);
Sanjay Pateld3c7bb282016-08-26 16:42:33 +0000716 Instruction *foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And,
Sanjay Patel14e0e182016-08-26 18:28:46 +0000717 const APInt *C1);
718 Instruction *foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And,
Sanjay Patel9b40f982016-09-07 22:33:03 +0000719 const APInt *C1, const APInt *C2);
Sanjay Patel8da42cc2016-09-15 22:26:31 +0000720 Instruction *foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
721 const APInt &C2);
722 Instruction *foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
723 const APInt &C2);
Sanjay Patela3f4f082016-08-16 17:54:36 +0000724
Sanjay Patelf58f68c2016-09-10 15:03:44 +0000725 Instruction *foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp,
726 BinaryOperator *BO,
727 const APInt *C);
728 Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, const APInt *C);
Sanjay Patel43395062016-07-21 18:07:40 +0000729
Sanjay Patel453ceff2016-09-29 22:18:30 +0000730 // Helpers of visitSelectInst().
Sanjay Patelf7b851f2016-09-30 19:49:22 +0000731 Instruction *foldSelectExtConst(SelectInst &Sel);
Sanjay Patel453ceff2016-09-29 22:18:30 +0000732 Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
733 Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *);
734 Instruction *foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
735 Value *A, Value *B, Instruction &Outer,
736 SelectPatternFlavor SPF2, Value *C);
737 Instruction *foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
738
Craig Topper70e4f432017-04-02 17:57:30 +0000739 Instruction *OptAndOp(BinaryOperator *Op, ConstantInt *OpRHS,
Chris Lattner35522b72010-01-04 07:12:23 +0000740 ConstantInt *AndRHS, BinaryOperator &TheAnd);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000741
Sanjay Patel85d79742016-08-31 19:49:56 +0000742 Value *insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi,
743 bool isSigned, bool Inside);
Chris Lattner35522b72010-01-04 07:12:23 +0000744 Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
Chad Rosiera00df492016-05-25 16:22:14 +0000745 Instruction *MatchBSwap(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000746 bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
Igor Laevsky900ffa32017-02-08 14:32:04 +0000747
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000748 Instruction *
749 SimplifyElementUnorderedAtomicMemCpy(ElementUnorderedAtomicMemCpyInst *AMI);
Pete Cooper67cf9a72015-11-19 05:56:52 +0000750 Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
Chris Lattner35522b72010-01-04 07:12:23 +0000751 Instruction *SimplifyMemSet(MemSetInst *MI);
752
Chris Lattner229907c2011-07-18 04:54:35 +0000753 Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
Duncan Sands533c8ae2012-10-23 08:28:26 +0000754
Chandler Carruth3a622162015-01-20 19:27:58 +0000755 /// \brief Returns a value X such that Val = X * Scale, or null if none.
756 ///
757 /// If the multiplication is known not to overflow then NoSignedWrap is set.
Duncan Sands533c8ae2012-10-23 08:28:26 +0000758 Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
Chris Lattner35522b72010-01-04 07:12:23 +0000759};
760
Chris Lattner35522b72010-01-04 07:12:23 +0000761} // end namespace llvm.
762
Chandler Carruth964daaa2014-04-22 02:55:47 +0000763#undef DEBUG_TYPE
764
Chris Lattner35522b72010-01-04 07:12:23 +0000765#endif