blob: cd9c9e9d5b5c2da610be3f08fa64cb2fd4209fcc [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
18#include "InstCombineWorklist.h"
Chandler Carruth66b31302015-01-04 12:03:27 +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"
Chris Lattner35522b72010-01-04 07:12:23 +000030
Chandler Carruth964daaa2014-04-22 02:55:47 +000031#define DEBUG_TYPE "instcombine"
32
Chris Lattner35522b72010-01-04 07:12:23 +000033namespace llvm {
Chandler Carruthd70cc602014-05-07 17:36:59 +000034class CallSite;
35class DataLayout;
Hal Finkel60db0582014-09-07 18:57:58 +000036class DominatorTree;
Chandler Carruthd70cc602014-05-07 17:36:59 +000037class TargetLibraryInfo;
38class DbgDeclareInst;
39class MemIntrinsic;
40class MemSetInst;
Jakub Staszak190db2f2013-01-14 23:16:36 +000041
Chandler Carruth3a622162015-01-20 19:27:58 +000042/// \brief Specific patterns of select instructions we can match.
Chris Lattner35522b72010-01-04 07:12:23 +000043enum SelectPatternFlavor {
44 SPF_UNKNOWN = 0,
Chandler Carruthd70cc602014-05-07 17:36:59 +000045 SPF_SMIN,
46 SPF_UMIN,
47 SPF_SMAX,
Dinesh Dwivedi3217b6c2014-06-06 06:54:45 +000048 SPF_UMAX,
49 SPF_ABS,
50 SPF_NABS
Chris Lattner35522b72010-01-04 07:12:23 +000051};
Jakub Staszak190db2f2013-01-14 23:16:36 +000052
Chandler Carruth3a622162015-01-20 19:27:58 +000053/// \brief Assign a complexity or rank value to LLVM Values.
54///
55/// This routine maps IR values to various complexity ranks:
56/// 0 -> undef
57/// 1 -> Constants
58/// 2 -> Other non-instructions
59/// 3 -> Arguments
60/// 3 -> Unary operations
61/// 4 -> Other instructions
Chris Lattner2188e402010-01-04 07:37:31 +000062static inline unsigned getComplexity(Value *V) {
63 if (isa<Instruction>(V)) {
Chandler Carruthd70cc602014-05-07 17:36:59 +000064 if (BinaryOperator::isNeg(V) || BinaryOperator::isFNeg(V) ||
Chris Lattner2188e402010-01-04 07:37:31 +000065 BinaryOperator::isNot(V))
66 return 3;
67 return 4;
68 }
Chandler Carruthd70cc602014-05-07 17:36:59 +000069 if (isa<Argument>(V))
70 return 3;
Chris Lattner2188e402010-01-04 07:37:31 +000071 return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
72}
Chris Lattner35522b72010-01-04 07:12:23 +000073
Chandler Carruth3a622162015-01-20 19:27:58 +000074/// \brief Add one to a Constant
Benjamin Kramer970f4952014-01-19 16:56:10 +000075static inline Constant *AddOne(Constant *C) {
76 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
77}
Chandler Carruth3a622162015-01-20 19:27:58 +000078/// \brief Subtract one from a Constant
Benjamin Kramer970f4952014-01-19 16:56:10 +000079static inline Constant *SubOne(Constant *C) {
80 return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
81}
82
Chandler Carruth3a622162015-01-20 19:27:58 +000083/// \brief An IRBuilder inserter that adds new instructions to the instcombine
84/// worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +000085class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter
Chris Lattner35522b72010-01-04 07:12:23 +000086 : public IRBuilderDefaultInserter<true> {
87 InstCombineWorklist &Worklist;
Chandler Carruth66b31302015-01-04 12:03:27 +000088 AssumptionCache *AC;
Chandler Carruthd70cc602014-05-07 17:36:59 +000089
Chris Lattner35522b72010-01-04 07:12:23 +000090public:
Chandler Carruth66b31302015-01-04 12:03:27 +000091 InstCombineIRInserter(InstCombineWorklist &WL, AssumptionCache *AC)
92 : Worklist(WL), AC(AC) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +000093
Chandler Carruthd70cc602014-05-07 17:36:59 +000094 void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB,
95 BasicBlock::iterator InsertPt) const {
Chris Lattner35522b72010-01-04 07:12:23 +000096 IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
97 Worklist.Add(I);
Hal Finkel74c2f352014-09-07 12:44:26 +000098
99 using namespace llvm::PatternMatch;
Benjamin Kramer63207bc2014-10-25 18:09:01 +0000100 if (match(I, m_Intrinsic<Intrinsic::assume>()))
Chandler Carruth66b31302015-01-04 12:03:27 +0000101 AC->registerAssumption(cast<CallInst>(I));
Chris Lattner35522b72010-01-04 07:12:23 +0000102 }
103};
Jakub Staszak190db2f2013-01-14 23:16:36 +0000104
Chandler Carruth3a622162015-01-20 19:27:58 +0000105/// \brief The core instruction combiner logic.
106///
107/// This class provides both the logic to recursively visit instructions and
108/// combine them, as well as the pass infrastructure for running this as part
109/// of the LLVM pass pipeline.
Duncan Sands6c5e4352010-05-11 20:16:09 +0000110class LLVM_LIBRARY_VISIBILITY InstCombiner
Chandler Carruth1edb9d62015-01-20 22:44:35 +0000111 : public InstVisitor<InstCombiner, Instruction *> {
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000112 // FIXME: These members shouldn't be public.
Chris Lattner35522b72010-01-04 07:12:23 +0000113public:
Chandler Carruth3a622162015-01-20 19:27:58 +0000114 /// \brief A worklist of the instructions that need to be simplified.
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000115 InstCombineWorklist &Worklist;
Chris Lattner35522b72010-01-04 07:12:23 +0000116
Chandler Carruth3a622162015-01-20 19:27:58 +0000117 /// \brief An IRBuilder that automatically inserts new instructions into the
118 /// worklist.
Chris Lattner35522b72010-01-04 07:12:23 +0000119 typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy;
120 BuilderTy *Builder;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000121
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000122private:
123 // Mode in which we are running the combiner.
124 const bool MinimizeSize;
125
126 // Required analyses.
127 // FIXME: These can never be null and should be references.
128 AssumptionCache *AC;
129 TargetLibraryInfo *TLI;
130 DominatorTree *DT;
131
132 // Optional analyses. When non-null, these can both be used to do better
133 // combining and will be updated to reflect any changes.
134 const DataLayout *DL;
135 LoopInfo *LI;
136
137 bool MadeIRChange;
Chris Lattner35522b72010-01-04 07:12:23 +0000138
139public:
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000140 InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder,
141 bool MinimizeSize, AssumptionCache *AC, TargetLibraryInfo *TLI,
142 DominatorTree *DT, const DataLayout *DL, LoopInfo *LI)
143 : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
144 AC(AC), TLI(TLI), DT(DT), DL(DL), LI(LI), MadeIRChange(false) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +0000145
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000146 /// \brief Run the combiner over the entire worklist until it is empty.
147 ///
148 /// \returns true if the IR is changed.
149 bool run();
Chris Lattner35522b72010-01-04 07:12:23 +0000150
Chandler Carruth66b31302015-01-04 12:03:27 +0000151 AssumptionCache *getAssumptionCache() const { return AC; }
Hal Finkel74c2f352014-09-07 12:44:26 +0000152
Rafael Espindolaaeff8a92014-02-24 23:12:18 +0000153 const DataLayout *getDataLayout() const { return DL; }
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000154
Hal Finkel60db0582014-09-07 18:57:58 +0000155 DominatorTree *getDominatorTree() const { return DT; }
Chris Lattner35522b72010-01-04 07:12:23 +0000156
Chandler Carruth5175b9a2015-01-20 08:35:24 +0000157 LoopInfo *getLoopInfo() const { return LI; }
158
Chad Rosier43a33062011-12-02 01:26:24 +0000159 TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
160
Chris Lattner35522b72010-01-04 07:12:23 +0000161 // Visitation implementation - Implement instruction combining for different
162 // instruction types. The semantics are as follows:
163 // Return Value:
164 // null - No change was made
165 // I - Change was made, I is still valid, I may be dead though
166 // otherwise - Change was made, replace I with returned instruction
167 //
168 Instruction *visitAdd(BinaryOperator &I);
169 Instruction *visitFAdd(BinaryOperator &I);
Chris Lattner229907c2011-07-18 04:54:35 +0000170 Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
Chris Lattner35522b72010-01-04 07:12:23 +0000171 Instruction *visitSub(BinaryOperator &I);
172 Instruction *visitFSub(BinaryOperator &I);
173 Instruction *visitMul(BinaryOperator &I);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000174 Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000175 Instruction *InsertBefore);
Chris Lattner35522b72010-01-04 07:12:23 +0000176 Instruction *visitFMul(BinaryOperator &I);
177 Instruction *visitURem(BinaryOperator &I);
178 Instruction *visitSRem(BinaryOperator &I);
179 Instruction *visitFRem(BinaryOperator &I);
180 bool SimplifyDivRemOfSelect(BinaryOperator &I);
181 Instruction *commonRemTransforms(BinaryOperator &I);
182 Instruction *commonIRemTransforms(BinaryOperator &I);
183 Instruction *commonDivTransforms(BinaryOperator &I);
184 Instruction *commonIDivTransforms(BinaryOperator &I);
185 Instruction *visitUDiv(BinaryOperator &I);
186 Instruction *visitSDiv(BinaryOperator &I);
Frits van Bommel2a559512011-01-29 17:50:27 +0000187 Instruction *visitFDiv(BinaryOperator &I);
Erik Ecksteind1817522014-12-03 10:39:15 +0000188 Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
Chris Lattner067459c2010-03-05 08:46:26 +0000189 Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
190 Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chris Lattner35522b72010-01-04 07:12:23 +0000191 Instruction *visitAnd(BinaryOperator &I);
Hal Finkel60db0582014-09-07 18:57:58 +0000192 Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction *CxtI);
Chris Lattner067459c2010-03-05 08:46:26 +0000193 Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000194 Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A,
195 Value *B, Value *C);
David Majnemer5d1aeba2014-08-21 05:14:48 +0000196 Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A,
197 Value *B, Value *C);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000198 Instruction *visitOr(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000199 Instruction *visitXor(BinaryOperator &I);
200 Instruction *visitShl(BinaryOperator &I);
201 Instruction *visitAShr(BinaryOperator &I);
202 Instruction *visitLShr(BinaryOperator &I);
203 Instruction *commonShiftTransforms(BinaryOperator &I);
204 Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
205 Constant *RHSC);
206 Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
207 GlobalVariable *GV, CmpInst &ICI,
Craig Toppere73658d2014-04-28 04:05:08 +0000208 ConstantInt *AndCst = nullptr);
Chris Lattner35522b72010-01-04 07:12:23 +0000209 Instruction *visitFCmpInst(FCmpInst &I);
210 Instruction *visitICmpInst(ICmpInst &I);
211 Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000212 Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS,
Chris Lattner35522b72010-01-04 07:12:23 +0000213 ConstantInt *RHS);
214 Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
215 ConstantInt *DivRHS);
Chris Lattnerd369f572011-02-13 07:43:07 +0000216 Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI,
217 ConstantInt *DivRHS);
Suyog Sarda3a8c2c12014-07-22 19:19:36 +0000218 Instruction *FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A,
219 ConstantInt *CI1, ConstantInt *CI2);
David Majnemer59939ac2014-10-19 08:23:08 +0000220 Instruction *FoldICmpCstShlCst(ICmpInst &I, Value *Op, Value *A,
221 ConstantInt *CI1, ConstantInt *CI2);
Benjamin Kramer0e2d1622013-09-20 22:12:42 +0000222 Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI,
223 ICmpInst::Predicate Pred);
Chris Lattner35522b72010-01-04 07:12:23 +0000224 Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
225 ICmpInst::Predicate Cond, Instruction &I);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000226 Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattner35522b72010-01-04 07:12:23 +0000227 BinaryOperator &I);
228 Instruction *commonCastTransforms(CastInst &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000229 Instruction *commonPointerCastTransforms(CastInst &CI);
230 Instruction *visitTrunc(TruncInst &CI);
231 Instruction *visitZExt(ZExtInst &CI);
232 Instruction *visitSExt(SExtInst &CI);
233 Instruction *visitFPTrunc(FPTruncInst &CI);
234 Instruction *visitFPExt(CastInst &CI);
235 Instruction *visitFPToUI(FPToUIInst &FI);
236 Instruction *visitFPToSI(FPToSIInst &FI);
237 Instruction *visitUIToFP(CastInst &CI);
238 Instruction *visitSIToFP(CastInst &CI);
239 Instruction *visitPtrToInt(PtrToIntInst &CI);
240 Instruction *visitIntToPtr(IntToPtrInst &CI);
241 Instruction *visitBitCast(BitCastInst &CI);
Matt Arsenaulta9e95ab2013-11-15 05:45:08 +0000242 Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000243 Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
244 Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *);
Chris Lattner35522b72010-01-04 07:12:23 +0000245 Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
246 Value *A, Value *B, Instruction &Outer,
247 SelectPatternFlavor SPF2, Value *C);
248 Instruction *visitSelectInst(SelectInst &SI);
249 Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
250 Instruction *visitCallInst(CallInst &CI);
251 Instruction *visitInvokeInst(InvokeInst &II);
252
253 Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
254 Instruction *visitPHINode(PHINode &PN);
255 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
256 Instruction *visitAllocaInst(AllocaInst &AI);
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000257 Instruction *visitAllocSite(Instruction &FI);
Gabor Greif75f69432010-06-24 12:21:15 +0000258 Instruction *visitFree(CallInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000259 Instruction *visitLoadInst(LoadInst &LI);
260 Instruction *visitStoreInst(StoreInst &SI);
261 Instruction *visitBranchInst(BranchInst &BI);
262 Instruction *visitSwitchInst(SwitchInst &SI);
Hal Finkel93873cc12014-09-07 21:28:34 +0000263 Instruction *visitReturnInst(ReturnInst &RI);
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000264 Instruction *visitInsertValueInst(InsertValueInst &IV);
Chris Lattner35522b72010-01-04 07:12:23 +0000265 Instruction *visitInsertElementInst(InsertElementInst &IE);
266 Instruction *visitExtractElementInst(ExtractElementInst &EI);
267 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
268 Instruction *visitExtractValueInst(ExtractValueInst &EV);
Duncan Sands5c055792011-09-30 13:12:16 +0000269 Instruction *visitLandingPadInst(LandingPadInst &LI);
Chris Lattner35522b72010-01-04 07:12:23 +0000270
271 // visitInstruction - Specify what to return for unhandled instructions...
Craig Toppere73658d2014-04-28 04:05:08 +0000272 Instruction *visitInstruction(Instruction &I) { return nullptr; }
Chris Lattner35522b72010-01-04 07:12:23 +0000273
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +0000274 // True when DB dominates all uses of DI execpt UI.
275 // UI must be in the same block as DI.
276 // The routine checks that the DI parent and DB are different.
277 bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
278 const BasicBlock *DB) const;
279
280 // Replace select with select operand SIOpd in SI-ICmp sequence when possible
281 bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
282 const unsigned SIOpd);
283
Chris Lattner35522b72010-01-04 07:12:23 +0000284private:
Chris Lattner229907c2011-07-18 04:54:35 +0000285 bool ShouldChangeType(Type *From, Type *To) const;
Chris Lattner2188e402010-01-04 07:37:31 +0000286 Value *dyn_castNegVal(Value *V) const;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000287 Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
Matt Arsenaultd79f7d92013-08-19 22:17:40 +0000288 Type *FindElementAtOffset(Type *PtrTy, int64_t Offset,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000289 SmallVectorImpl<Value *> &NewIndices);
Chris Lattner2b295a02010-01-04 07:53:58 +0000290 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000291
Chandler Carruth3a622162015-01-20 19:27:58 +0000292 /// \brief Classify whether a cast is worth optimizing.
293 ///
294 /// Returns true if the cast from "V to Ty" actually results in any code
295 /// being generated and is interesting to optimize out. If the cast can be
296 /// eliminated by some other simple transformation, we prefer to do the
297 /// simplification first.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000298 bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V,
Chris Lattner229907c2011-07-18 04:54:35 +0000299 Type *Ty);
Chris Lattner2188e402010-01-04 07:37:31 +0000300
Chris Lattner35522b72010-01-04 07:12:23 +0000301 Instruction *visitCallSite(CallSite CS);
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000302 Instruction *tryOptimizeCall(CallInst *CI, const DataLayout *DL);
Chris Lattner35522b72010-01-04 07:12:23 +0000303 bool transformConstExprCastCall(CallSite CS);
Duncan Sandsa0984362011-09-06 13:37:06 +0000304 Instruction *transformCallThroughTrampoline(CallSite CS,
305 IntrinsicInst *Tramp);
Chris Lattner35522b72010-01-04 07:12:23 +0000306 Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
307 bool DoXform = true);
Benjamin Kramer398b8c52011-04-01 20:09:03 +0000308 Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
Hal Finkel60db0582014-09-07 18:57:58 +0000309 bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS, Instruction *CxtI);
Hal Finkel60db0582014-09-07 18:57:58 +0000310 bool WillNotOverflowSignedSub(Value *LHS, Value *RHS, Instruction *CxtI);
311 bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, Instruction *CxtI);
Erik Ecksteina451b9b2014-12-17 07:29:19 +0000312 bool WillNotOverflowSignedMul(Value *LHS, Value *RHS, Instruction *CxtI);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000313 Value *EmitGEPOffset(User *GEP);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000314 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
Nick Lewyckya2b77202013-05-31 00:59:42 +0000315 Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
Chris Lattner35522b72010-01-04 07:12:23 +0000316
317public:
Chandler Carruth3a622162015-01-20 19:27:58 +0000318 /// \brief Inserts an instruction \p New before instruction \p Old
319 ///
320 /// Also adds the new instruction to the worklist and returns \p New so that
321 /// it is suitable for use as the return from the visitation patterns.
Chris Lattner35522b72010-01-04 07:12:23 +0000322 Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
Craig Toppere73658d2014-04-28 04:05:08 +0000323 assert(New && !New->getParent() &&
Chris Lattner35522b72010-01-04 07:12:23 +0000324 "New instruction already inserted into a basic block!");
325 BasicBlock *BB = Old.getParent();
Chandler Carruthd70cc602014-05-07 17:36:59 +0000326 BB->getInstList().insert(&Old, New); // Insert inst
Chris Lattner35522b72010-01-04 07:12:23 +0000327 Worklist.Add(New);
328 return New;
329 }
Eli Friedman6efb64e2011-05-19 01:20:42 +0000330
Chandler Carruth3a622162015-01-20 19:27:58 +0000331 /// \brief Same as InsertNewInstBefore, but also sets the debug loc.
Eli Friedman6efb64e2011-05-19 01:20:42 +0000332 Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
333 New->setDebugLoc(Old.getDebugLoc());
334 return InsertNewInstBefore(New, Old);
335 }
336
Chandler Carruth3a622162015-01-20 19:27:58 +0000337 /// \brief A combiner-aware RAUW-like routine.
338 ///
339 /// This method is to be used when an instruction is found to be dead,
340 /// replacable with another preexisting expression. Here we add all uses of
341 /// I to the worklist, replace all uses of I with the new value, then return
342 /// I, so that the inst combiner will know that I was modified.
Chris Lattner35522b72010-01-04 07:12:23 +0000343 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
Chandler Carruthd70cc602014-05-07 17:36:59 +0000344 Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000345
Chris Lattner35522b72010-01-04 07:12:23 +0000346 // If we are replacing the instruction with itself, this must be in a
347 // segment of unreachable code, so just clobber the instruction.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000348 if (&I == V)
Chris Lattner35522b72010-01-04 07:12:23 +0000349 V = UndefValue::get(I.getType());
Frits van Bommeld14d9912011-03-27 23:32:31 +0000350
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000351 DEBUG(dbgs() << "IC: Replacing " << I << "\n"
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000352 << " with " << *V << '\n');
Frits van Bommeld14d9912011-03-27 23:32:31 +0000353
Chris Lattner35522b72010-01-04 07:12:23 +0000354 I.replaceAllUsesWith(V);
355 return &I;
356 }
357
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000358 /// Creates a result tuple for an overflow intrinsic \p II with a given
359 /// \p Result and a constant \p Overflow value. If \p ReUseName is true the
360 /// \p Result's name is taken from \p II.
361 Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result,
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000362 bool Overflow, bool ReUseName = true) {
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000363 if (ReUseName)
364 Result->takeName(II);
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000365 Constant *V[] = {UndefValue::get(Result->getType()),
366 Overflow ? Builder->getTrue() : Builder->getFalse()};
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000367 StructType *ST = cast<StructType>(II->getType());
368 Constant *Struct = ConstantStruct::get(ST, V);
369 return InsertValueInst::Create(Struct, Result, 0);
370 }
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000371
Chandler Carruth3a622162015-01-20 19:27:58 +0000372 /// \brief Combiner aware instruction erasure.
373 ///
374 /// When dealing with an instruction that has side effects or produces a void
375 /// value, we can't rely on DCE to delete the instruction. Instead, visit
376 /// methods should return the value returned by this function.
Chris Lattner35522b72010-01-04 07:12:23 +0000377 Instruction *EraseInstFromFunction(Instruction &I) {
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000378 DEBUG(dbgs() << "IC: ERASE " << I << '\n');
Chris Lattner35522b72010-01-04 07:12:23 +0000379
380 assert(I.use_empty() && "Cannot erase instruction that is used!");
381 // Make sure that we reprocess all operands now that we reduced their
382 // use counts.
383 if (I.getNumOperands() < 8) {
384 for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
385 if (Instruction *Op = dyn_cast<Instruction>(*i))
386 Worklist.Add(Op);
387 }
388 Worklist.Remove(&I);
389 I.eraseFromParent();
390 MadeIRChange = true;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000391 return nullptr; // Don't do anything with FI
Chris Lattner35522b72010-01-04 07:12:23 +0000392 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000393
Jay Foada0653a32014-05-14 21:14:37 +0000394 void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
Hal Finkel60db0582014-09-07 18:57:58 +0000395 unsigned Depth = 0, Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000396 return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
397 DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000398 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000399
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000400 bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth = 0,
Hal Finkel60db0582014-09-07 18:57:58 +0000401 Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000402 return llvm::MaskedValueIsZero(V, Mask, DL, Depth, AC, CxtI, DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000403 }
Hal Finkel60db0582014-09-07 18:57:58 +0000404 unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0,
405 Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000406 return llvm::ComputeNumSignBits(Op, DL, Depth, AC, CxtI, DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000407 }
David Majnemer54c2ca22014-12-26 09:10:14 +0000408 void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
409 unsigned Depth = 0, Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000410 return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
David Majnemer54c2ca22014-12-26 09:10:14 +0000411 DT);
412 }
David Majnemer491331a2015-01-02 07:29:43 +0000413 OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
414 const Instruction *CxtI) {
Chandler Carruth66b31302015-01-04 12:03:27 +0000415 return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, AC, CxtI, DT);
David Majnemer491331a2015-01-02 07:29:43 +0000416 }
David Majnemer5310c1e2015-01-07 00:39:50 +0000417 OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
418 const Instruction *CxtI) {
419 return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, AC, CxtI, DT);
420 }
Chris Lattner35522b72010-01-04 07:12:23 +0000421
422private:
Chandler Carruth3a622162015-01-20 19:27:58 +0000423 /// \brief Performs a few simplifications for operators which are associative
424 /// or commutative.
Duncan Sands641baf12010-11-13 15:10:37 +0000425 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000426
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000427 /// \brief Tries to simplify binary operations which some other binary
428 /// operation distributes over.
Chandler Carruth3a622162015-01-20 19:27:58 +0000429 ///
430 /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
431 /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
432 /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
433 /// value, or null if it didn't simplify.
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000434 Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
Duncan Sandsadc7771f2010-11-23 14:23:47 +0000435
Chandler Carruth3a622162015-01-20 19:27:58 +0000436 /// \brief Attempts to replace V with a simpler value based on the demanded
437 /// bits.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000438 Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero,
Hal Finkel60db0582014-09-07 18:57:58 +0000439 APInt &KnownOne, unsigned Depth,
440 Instruction *CxtI = nullptr);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000441 bool SimplifyDemandedBits(Use &U, APInt DemandedMask, APInt &KnownZero,
442 APInt &KnownOne, unsigned Depth = 0);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000443 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
444 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
445 Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
446 APInt DemandedMask, APInt &KnownZero,
447 APInt &KnownOne);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000448
Chandler Carruth3a622162015-01-20 19:27:58 +0000449 /// \brief Tries to simplify operands to an integer instruction based on its
450 /// demanded bits.
Chris Lattner35522b72010-01-04 07:12:23 +0000451 bool SimplifyDemandedInstructionBits(Instruction &Inst);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000452
Chris Lattner35522b72010-01-04 07:12:23 +0000453 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000454 APInt &UndefElts, unsigned Depth = 0);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000455
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000456 Value *SimplifyVectorOp(BinaryOperator &Inst);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +0000457 Value *SimplifyBSwap(BinaryOperator &Inst);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000458
Chris Lattner35522b72010-01-04 07:12:23 +0000459 // FoldOpIntoPhi - Given a binary operator, cast instruction, or select
460 // which has a PHI node as operand #0, see if we can fold the instruction
461 // into the PHI (which is only possible if all operands to the PHI are
462 // constants).
463 //
Chris Lattnerea7131a2011-01-16 05:14:26 +0000464 Instruction *FoldOpIntoPhi(Instruction &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000465
Chandler Carruth3a622162015-01-20 19:27:58 +0000466 /// \brief Try to rotate an operation below a PHI node, using PHI nodes for
467 /// its operands.
Chris Lattner35522b72010-01-04 07:12:23 +0000468 Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
469 Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
470 Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
471 Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
472
Chris Lattner35522b72010-01-04 07:12:23 +0000473 Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
474 ConstantInt *AndRHS, BinaryOperator &TheAnd);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000475
Chris Lattner35522b72010-01-04 07:12:23 +0000476 Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
477 bool isSub, Instruction &I);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000478 Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned,
479 bool Inside);
Chris Lattner35522b72010-01-04 07:12:23 +0000480 Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
481 Instruction *MatchBSwap(BinaryOperator &I);
482 bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
483 Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
484 Instruction *SimplifyMemSet(MemSetInst *MI);
485
Chris Lattner229907c2011-07-18 04:54:35 +0000486 Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
Duncan Sands533c8ae2012-10-23 08:28:26 +0000487
Chandler Carruth3a622162015-01-20 19:27:58 +0000488 /// \brief Returns a value X such that Val = X * Scale, or null if none.
489 ///
490 /// If the multiplication is known not to overflow then NoSignedWrap is set.
Duncan Sands533c8ae2012-10-23 08:28:26 +0000491 Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
Chris Lattner35522b72010-01-04 07:12:23 +0000492};
493
Chris Lattner35522b72010-01-04 07:12:23 +0000494} // end namespace llvm.
495
Chandler Carruth964daaa2014-04-22 02:55:47 +0000496#undef DEBUG_TYPE
497
Chris Lattner35522b72010-01-04 07:12:23 +0000498#endif