blob: 346dcaa705dc28fa92e59fde57b221a7d98c40f5 [file] [log] [blame]
Matt Arsenault52ddb7b2013-05-17 21:43:39 +00001//===- InstCombine.h - Main InstCombine pass definition ---------*- 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//===----------------------------------------------------------------------===//
9
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000010#ifndef LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINE_H
11#define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINE_H
Chris Lattner35522b72010-01-04 07:12:23 +000012
13#include "InstCombineWorklist.h"
Chandler Carruth66b31302015-01-04 12:03:27 +000014#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruth5175b9a2015-01-20 08:35:24 +000015#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth452a0072014-03-04 11:59:06 +000016#include "llvm/Analysis/TargetFolder.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000017#include "llvm/Analysis/ValueTracking.h"
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +000018#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/IRBuilder.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000020#include "llvm/IR/InstVisitor.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/IntrinsicInst.h"
22#include "llvm/IR/Operator.h"
Hal Finkel74c2f352014-09-07 12:44:26 +000023#include "llvm/IR/PatternMatch.h"
Chris Lattner35522b72010-01-04 07:12:23 +000024#include "llvm/Pass.h"
Chris Lattner35522b72010-01-04 07:12:23 +000025
Chandler Carruth964daaa2014-04-22 02:55:47 +000026#define DEBUG_TYPE "instcombine"
27
Chris Lattner35522b72010-01-04 07:12:23 +000028namespace llvm {
Chandler Carruthd70cc602014-05-07 17:36:59 +000029class CallSite;
30class DataLayout;
Hal Finkel60db0582014-09-07 18:57:58 +000031class DominatorTree;
Chandler Carruthd70cc602014-05-07 17:36:59 +000032class TargetLibraryInfo;
33class DbgDeclareInst;
34class MemIntrinsic;
35class MemSetInst;
Jakub Staszak190db2f2013-01-14 23:16:36 +000036
Chandler Carruth3a622162015-01-20 19:27:58 +000037/// \brief Specific patterns of select instructions we can match.
Chris Lattner35522b72010-01-04 07:12:23 +000038enum SelectPatternFlavor {
39 SPF_UNKNOWN = 0,
Chandler Carruthd70cc602014-05-07 17:36:59 +000040 SPF_SMIN,
41 SPF_UMIN,
42 SPF_SMAX,
Dinesh Dwivedi3217b6c2014-06-06 06:54:45 +000043 SPF_UMAX,
44 SPF_ABS,
45 SPF_NABS
Chris Lattner35522b72010-01-04 07:12:23 +000046};
Jakub Staszak190db2f2013-01-14 23:16:36 +000047
Chandler Carruth3a622162015-01-20 19:27:58 +000048/// \brief Assign a complexity or rank value to LLVM Values.
49///
50/// This routine maps IR values to various complexity ranks:
51/// 0 -> undef
52/// 1 -> Constants
53/// 2 -> Other non-instructions
54/// 3 -> Arguments
55/// 3 -> Unary operations
56/// 4 -> Other instructions
Chris Lattner2188e402010-01-04 07:37:31 +000057static inline unsigned getComplexity(Value *V) {
58 if (isa<Instruction>(V)) {
Chandler Carruthd70cc602014-05-07 17:36:59 +000059 if (BinaryOperator::isNeg(V) || BinaryOperator::isFNeg(V) ||
Chris Lattner2188e402010-01-04 07:37:31 +000060 BinaryOperator::isNot(V))
61 return 3;
62 return 4;
63 }
Chandler Carruthd70cc602014-05-07 17:36:59 +000064 if (isa<Argument>(V))
65 return 3;
Chris Lattner2188e402010-01-04 07:37:31 +000066 return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
67}
Chris Lattner35522b72010-01-04 07:12:23 +000068
Chandler Carruth3a622162015-01-20 19:27:58 +000069/// \brief Add one to a Constant
Benjamin Kramer970f4952014-01-19 16:56:10 +000070static inline Constant *AddOne(Constant *C) {
71 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
72}
Chandler Carruth3a622162015-01-20 19:27:58 +000073/// \brief Subtract one from a Constant
Benjamin Kramer970f4952014-01-19 16:56:10 +000074static inline Constant *SubOne(Constant *C) {
75 return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
76}
77
Chandler Carruth3a622162015-01-20 19:27:58 +000078/// \brief An IRBuilder inserter that adds new instructions to the instcombine
79/// worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +000080class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter
Chris Lattner35522b72010-01-04 07:12:23 +000081 : public IRBuilderDefaultInserter<true> {
82 InstCombineWorklist &Worklist;
Chandler Carruth66b31302015-01-04 12:03:27 +000083 AssumptionCache *AC;
Chandler Carruthd70cc602014-05-07 17:36:59 +000084
Chris Lattner35522b72010-01-04 07:12:23 +000085public:
Chandler Carruth66b31302015-01-04 12:03:27 +000086 InstCombineIRInserter(InstCombineWorklist &WL, AssumptionCache *AC)
87 : Worklist(WL), AC(AC) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +000088
Chandler Carruthd70cc602014-05-07 17:36:59 +000089 void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB,
90 BasicBlock::iterator InsertPt) const {
Chris Lattner35522b72010-01-04 07:12:23 +000091 IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
92 Worklist.Add(I);
Hal Finkel74c2f352014-09-07 12:44:26 +000093
94 using namespace llvm::PatternMatch;
Benjamin Kramer63207bc2014-10-25 18:09:01 +000095 if (match(I, m_Intrinsic<Intrinsic::assume>()))
Chandler Carruth66b31302015-01-04 12:03:27 +000096 AC->registerAssumption(cast<CallInst>(I));
Chris Lattner35522b72010-01-04 07:12:23 +000097 }
98};
Jakub Staszak190db2f2013-01-14 23:16:36 +000099
Chandler Carruth3a622162015-01-20 19:27:58 +0000100/// \brief The core instruction combiner logic.
101///
102/// This class provides both the logic to recursively visit instructions and
103/// combine them, as well as the pass infrastructure for running this as part
104/// of the LLVM pass pipeline.
Duncan Sands6c5e4352010-05-11 20:16:09 +0000105class LLVM_LIBRARY_VISIBILITY InstCombiner
Chandler Carruth1edb9d62015-01-20 22:44:35 +0000106 : public InstVisitor<InstCombiner, Instruction *> {
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000107 // FIXME: These members shouldn't be public.
Chris Lattner35522b72010-01-04 07:12:23 +0000108public:
Chandler Carruth3a622162015-01-20 19:27:58 +0000109 /// \brief A worklist of the instructions that need to be simplified.
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000110 InstCombineWorklist &Worklist;
Chris Lattner35522b72010-01-04 07:12:23 +0000111
Chandler Carruth3a622162015-01-20 19:27:58 +0000112 /// \brief An IRBuilder that automatically inserts new instructions into the
113 /// worklist.
Chris Lattner35522b72010-01-04 07:12:23 +0000114 typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy;
115 BuilderTy *Builder;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000116
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000117private:
118 // Mode in which we are running the combiner.
119 const bool MinimizeSize;
120
121 // Required analyses.
122 // FIXME: These can never be null and should be references.
123 AssumptionCache *AC;
124 TargetLibraryInfo *TLI;
125 DominatorTree *DT;
126
127 // Optional analyses. When non-null, these can both be used to do better
128 // combining and will be updated to reflect any changes.
129 const DataLayout *DL;
130 LoopInfo *LI;
131
132 bool MadeIRChange;
Chris Lattner35522b72010-01-04 07:12:23 +0000133
134public:
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000135 InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder,
136 bool MinimizeSize, AssumptionCache *AC, TargetLibraryInfo *TLI,
137 DominatorTree *DT, const DataLayout *DL, LoopInfo *LI)
138 : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
139 AC(AC), TLI(TLI), DT(DT), DL(DL), LI(LI), MadeIRChange(false) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +0000140
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000141 /// \brief Run the combiner over the entire worklist until it is empty.
142 ///
143 /// \returns true if the IR is changed.
144 bool run();
Chris Lattner35522b72010-01-04 07:12:23 +0000145
Chandler Carruth66b31302015-01-04 12:03:27 +0000146 AssumptionCache *getAssumptionCache() const { return AC; }
Hal Finkel74c2f352014-09-07 12:44:26 +0000147
Rafael Espindolaaeff8a92014-02-24 23:12:18 +0000148 const DataLayout *getDataLayout() const { return DL; }
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000149
Hal Finkel60db0582014-09-07 18:57:58 +0000150 DominatorTree *getDominatorTree() const { return DT; }
Chris Lattner35522b72010-01-04 07:12:23 +0000151
Chandler Carruth5175b9a2015-01-20 08:35:24 +0000152 LoopInfo *getLoopInfo() const { return LI; }
153
Chad Rosier43a33062011-12-02 01:26:24 +0000154 TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
155
Chris Lattner35522b72010-01-04 07:12:23 +0000156 // Visitation implementation - Implement instruction combining for different
157 // instruction types. The semantics are as follows:
158 // Return Value:
159 // null - No change was made
160 // I - Change was made, I is still valid, I may be dead though
161 // otherwise - Change was made, replace I with returned instruction
162 //
163 Instruction *visitAdd(BinaryOperator &I);
164 Instruction *visitFAdd(BinaryOperator &I);
Chris Lattner229907c2011-07-18 04:54:35 +0000165 Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
Chris Lattner35522b72010-01-04 07:12:23 +0000166 Instruction *visitSub(BinaryOperator &I);
167 Instruction *visitFSub(BinaryOperator &I);
168 Instruction *visitMul(BinaryOperator &I);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000169 Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000170 Instruction *InsertBefore);
Chris Lattner35522b72010-01-04 07:12:23 +0000171 Instruction *visitFMul(BinaryOperator &I);
172 Instruction *visitURem(BinaryOperator &I);
173 Instruction *visitSRem(BinaryOperator &I);
174 Instruction *visitFRem(BinaryOperator &I);
175 bool SimplifyDivRemOfSelect(BinaryOperator &I);
176 Instruction *commonRemTransforms(BinaryOperator &I);
177 Instruction *commonIRemTransforms(BinaryOperator &I);
178 Instruction *commonDivTransforms(BinaryOperator &I);
179 Instruction *commonIDivTransforms(BinaryOperator &I);
180 Instruction *visitUDiv(BinaryOperator &I);
181 Instruction *visitSDiv(BinaryOperator &I);
Frits van Bommel2a559512011-01-29 17:50:27 +0000182 Instruction *visitFDiv(BinaryOperator &I);
Erik Ecksteind1817522014-12-03 10:39:15 +0000183 Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
Chris Lattner067459c2010-03-05 08:46:26 +0000184 Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
185 Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chris Lattner35522b72010-01-04 07:12:23 +0000186 Instruction *visitAnd(BinaryOperator &I);
Hal Finkel60db0582014-09-07 18:57:58 +0000187 Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction *CxtI);
Chris Lattner067459c2010-03-05 08:46:26 +0000188 Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000189 Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A,
190 Value *B, Value *C);
David Majnemer5d1aeba2014-08-21 05:14:48 +0000191 Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A,
192 Value *B, Value *C);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000193 Instruction *visitOr(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000194 Instruction *visitXor(BinaryOperator &I);
195 Instruction *visitShl(BinaryOperator &I);
196 Instruction *visitAShr(BinaryOperator &I);
197 Instruction *visitLShr(BinaryOperator &I);
198 Instruction *commonShiftTransforms(BinaryOperator &I);
199 Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
200 Constant *RHSC);
201 Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
202 GlobalVariable *GV, CmpInst &ICI,
Craig Toppere73658d2014-04-28 04:05:08 +0000203 ConstantInt *AndCst = nullptr);
Chris Lattner35522b72010-01-04 07:12:23 +0000204 Instruction *visitFCmpInst(FCmpInst &I);
205 Instruction *visitICmpInst(ICmpInst &I);
206 Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000207 Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS,
Chris Lattner35522b72010-01-04 07:12:23 +0000208 ConstantInt *RHS);
209 Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
210 ConstantInt *DivRHS);
Chris Lattnerd369f572011-02-13 07:43:07 +0000211 Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI,
212 ConstantInt *DivRHS);
Suyog Sarda3a8c2c12014-07-22 19:19:36 +0000213 Instruction *FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A,
214 ConstantInt *CI1, ConstantInt *CI2);
David Majnemer59939ac2014-10-19 08:23:08 +0000215 Instruction *FoldICmpCstShlCst(ICmpInst &I, Value *Op, Value *A,
216 ConstantInt *CI1, ConstantInt *CI2);
Benjamin Kramer0e2d1622013-09-20 22:12:42 +0000217 Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI,
218 ICmpInst::Predicate Pred);
Chris Lattner35522b72010-01-04 07:12:23 +0000219 Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
220 ICmpInst::Predicate Cond, Instruction &I);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000221 Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattner35522b72010-01-04 07:12:23 +0000222 BinaryOperator &I);
223 Instruction *commonCastTransforms(CastInst &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000224 Instruction *commonPointerCastTransforms(CastInst &CI);
225 Instruction *visitTrunc(TruncInst &CI);
226 Instruction *visitZExt(ZExtInst &CI);
227 Instruction *visitSExt(SExtInst &CI);
228 Instruction *visitFPTrunc(FPTruncInst &CI);
229 Instruction *visitFPExt(CastInst &CI);
230 Instruction *visitFPToUI(FPToUIInst &FI);
231 Instruction *visitFPToSI(FPToSIInst &FI);
232 Instruction *visitUIToFP(CastInst &CI);
233 Instruction *visitSIToFP(CastInst &CI);
234 Instruction *visitPtrToInt(PtrToIntInst &CI);
235 Instruction *visitIntToPtr(IntToPtrInst &CI);
236 Instruction *visitBitCast(BitCastInst &CI);
Matt Arsenaulta9e95ab2013-11-15 05:45:08 +0000237 Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000238 Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
239 Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *);
Chris Lattner35522b72010-01-04 07:12:23 +0000240 Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
241 Value *A, Value *B, Instruction &Outer,
242 SelectPatternFlavor SPF2, Value *C);
243 Instruction *visitSelectInst(SelectInst &SI);
244 Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
245 Instruction *visitCallInst(CallInst &CI);
246 Instruction *visitInvokeInst(InvokeInst &II);
247
248 Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
249 Instruction *visitPHINode(PHINode &PN);
250 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
251 Instruction *visitAllocaInst(AllocaInst &AI);
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000252 Instruction *visitAllocSite(Instruction &FI);
Gabor Greif75f69432010-06-24 12:21:15 +0000253 Instruction *visitFree(CallInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000254 Instruction *visitLoadInst(LoadInst &LI);
255 Instruction *visitStoreInst(StoreInst &SI);
256 Instruction *visitBranchInst(BranchInst &BI);
257 Instruction *visitSwitchInst(SwitchInst &SI);
Hal Finkel93873cc12014-09-07 21:28:34 +0000258 Instruction *visitReturnInst(ReturnInst &RI);
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000259 Instruction *visitInsertValueInst(InsertValueInst &IV);
Chris Lattner35522b72010-01-04 07:12:23 +0000260 Instruction *visitInsertElementInst(InsertElementInst &IE);
261 Instruction *visitExtractElementInst(ExtractElementInst &EI);
262 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
263 Instruction *visitExtractValueInst(ExtractValueInst &EV);
Duncan Sands5c055792011-09-30 13:12:16 +0000264 Instruction *visitLandingPadInst(LandingPadInst &LI);
Chris Lattner35522b72010-01-04 07:12:23 +0000265
266 // visitInstruction - Specify what to return for unhandled instructions...
Craig Toppere73658d2014-04-28 04:05:08 +0000267 Instruction *visitInstruction(Instruction &I) { return nullptr; }
Chris Lattner35522b72010-01-04 07:12:23 +0000268
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +0000269 // True when DB dominates all uses of DI execpt UI.
270 // UI must be in the same block as DI.
271 // The routine checks that the DI parent and DB are different.
272 bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
273 const BasicBlock *DB) const;
274
275 // Replace select with select operand SIOpd in SI-ICmp sequence when possible
276 bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
277 const unsigned SIOpd);
278
Chris Lattner35522b72010-01-04 07:12:23 +0000279private:
Chris Lattner229907c2011-07-18 04:54:35 +0000280 bool ShouldChangeType(Type *From, Type *To) const;
Chris Lattner2188e402010-01-04 07:37:31 +0000281 Value *dyn_castNegVal(Value *V) const;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000282 Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
Matt Arsenaultd79f7d92013-08-19 22:17:40 +0000283 Type *FindElementAtOffset(Type *PtrTy, int64_t Offset,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000284 SmallVectorImpl<Value *> &NewIndices);
Chris Lattner2b295a02010-01-04 07:53:58 +0000285 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000286
Chandler Carruth3a622162015-01-20 19:27:58 +0000287 /// \brief Classify whether a cast is worth optimizing.
288 ///
289 /// Returns true if the cast from "V to Ty" actually results in any code
290 /// being generated and is interesting to optimize out. If the cast can be
291 /// eliminated by some other simple transformation, we prefer to do the
292 /// simplification first.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000293 bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V,
Chris Lattner229907c2011-07-18 04:54:35 +0000294 Type *Ty);
Chris Lattner2188e402010-01-04 07:37:31 +0000295
Chris Lattner35522b72010-01-04 07:12:23 +0000296 Instruction *visitCallSite(CallSite CS);
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000297 Instruction *tryOptimizeCall(CallInst *CI, const DataLayout *DL);
Chris Lattner35522b72010-01-04 07:12:23 +0000298 bool transformConstExprCastCall(CallSite CS);
Duncan Sandsa0984362011-09-06 13:37:06 +0000299 Instruction *transformCallThroughTrampoline(CallSite CS,
300 IntrinsicInst *Tramp);
Chris Lattner35522b72010-01-04 07:12:23 +0000301 Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
302 bool DoXform = true);
Benjamin Kramer398b8c52011-04-01 20:09:03 +0000303 Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
Hal Finkel60db0582014-09-07 18:57:58 +0000304 bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS, Instruction *CxtI);
Hal Finkel60db0582014-09-07 18:57:58 +0000305 bool WillNotOverflowSignedSub(Value *LHS, Value *RHS, Instruction *CxtI);
306 bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, Instruction *CxtI);
Erik Ecksteina451b9b2014-12-17 07:29:19 +0000307 bool WillNotOverflowSignedMul(Value *LHS, Value *RHS, Instruction *CxtI);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000308 Value *EmitGEPOffset(User *GEP);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000309 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
Nick Lewyckya2b77202013-05-31 00:59:42 +0000310 Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
Chris Lattner35522b72010-01-04 07:12:23 +0000311
312public:
Chandler Carruth3a622162015-01-20 19:27:58 +0000313 /// \brief Inserts an instruction \p New before instruction \p Old
314 ///
315 /// Also adds the new instruction to the worklist and returns \p New so that
316 /// it is suitable for use as the return from the visitation patterns.
Chris Lattner35522b72010-01-04 07:12:23 +0000317 Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
Craig Toppere73658d2014-04-28 04:05:08 +0000318 assert(New && !New->getParent() &&
Chris Lattner35522b72010-01-04 07:12:23 +0000319 "New instruction already inserted into a basic block!");
320 BasicBlock *BB = Old.getParent();
Chandler Carruthd70cc602014-05-07 17:36:59 +0000321 BB->getInstList().insert(&Old, New); // Insert inst
Chris Lattner35522b72010-01-04 07:12:23 +0000322 Worklist.Add(New);
323 return New;
324 }
Eli Friedman6efb64e2011-05-19 01:20:42 +0000325
Chandler Carruth3a622162015-01-20 19:27:58 +0000326 /// \brief Same as InsertNewInstBefore, but also sets the debug loc.
Eli Friedman6efb64e2011-05-19 01:20:42 +0000327 Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
328 New->setDebugLoc(Old.getDebugLoc());
329 return InsertNewInstBefore(New, Old);
330 }
331
Chandler Carruth3a622162015-01-20 19:27:58 +0000332 /// \brief A combiner-aware RAUW-like routine.
333 ///
334 /// This method is to be used when an instruction is found to be dead,
335 /// replacable with another preexisting expression. Here we add all uses of
336 /// I to the worklist, replace all uses of I with the new value, then return
337 /// I, so that the inst combiner will know that I was modified.
Chris Lattner35522b72010-01-04 07:12:23 +0000338 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
Chandler Carruthd70cc602014-05-07 17:36:59 +0000339 Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000340
Chris Lattner35522b72010-01-04 07:12:23 +0000341 // If we are replacing the instruction with itself, this must be in a
342 // segment of unreachable code, so just clobber the instruction.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000343 if (&I == V)
Chris Lattner35522b72010-01-04 07:12:23 +0000344 V = UndefValue::get(I.getType());
Frits van Bommeld14d9912011-03-27 23:32:31 +0000345
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000346 DEBUG(dbgs() << "IC: Replacing " << I << "\n"
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000347 << " with " << *V << '\n');
Frits van Bommeld14d9912011-03-27 23:32:31 +0000348
Chris Lattner35522b72010-01-04 07:12:23 +0000349 I.replaceAllUsesWith(V);
350 return &I;
351 }
352
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000353 /// Creates a result tuple for an overflow intrinsic \p II with a given
354 /// \p Result and a constant \p Overflow value. If \p ReUseName is true the
355 /// \p Result's name is taken from \p II.
356 Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result,
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000357 bool Overflow, bool ReUseName = true) {
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000358 if (ReUseName)
359 Result->takeName(II);
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000360 Constant *V[] = {UndefValue::get(Result->getType()),
361 Overflow ? Builder->getTrue() : Builder->getFalse()};
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000362 StructType *ST = cast<StructType>(II->getType());
363 Constant *Struct = ConstantStruct::get(ST, V);
364 return InsertValueInst::Create(Struct, Result, 0);
365 }
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000366
Chandler Carruth3a622162015-01-20 19:27:58 +0000367 /// \brief Combiner aware instruction erasure.
368 ///
369 /// When dealing with an instruction that has side effects or produces a void
370 /// value, we can't rely on DCE to delete the instruction. Instead, visit
371 /// methods should return the value returned by this function.
Chris Lattner35522b72010-01-04 07:12:23 +0000372 Instruction *EraseInstFromFunction(Instruction &I) {
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000373 DEBUG(dbgs() << "IC: ERASE " << I << '\n');
Chris Lattner35522b72010-01-04 07:12:23 +0000374
375 assert(I.use_empty() && "Cannot erase instruction that is used!");
376 // Make sure that we reprocess all operands now that we reduced their
377 // use counts.
378 if (I.getNumOperands() < 8) {
379 for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
380 if (Instruction *Op = dyn_cast<Instruction>(*i))
381 Worklist.Add(Op);
382 }
383 Worklist.Remove(&I);
384 I.eraseFromParent();
385 MadeIRChange = true;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000386 return nullptr; // Don't do anything with FI
Chris Lattner35522b72010-01-04 07:12:23 +0000387 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000388
Jay Foada0653a32014-05-14 21:14:37 +0000389 void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
Hal Finkel60db0582014-09-07 18:57:58 +0000390 unsigned Depth = 0, Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000391 return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
392 DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000393 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000394
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000395 bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth = 0,
Hal Finkel60db0582014-09-07 18:57:58 +0000396 Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000397 return llvm::MaskedValueIsZero(V, Mask, DL, Depth, AC, CxtI, DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000398 }
Hal Finkel60db0582014-09-07 18:57:58 +0000399 unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0,
400 Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000401 return llvm::ComputeNumSignBits(Op, DL, Depth, AC, CxtI, DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000402 }
David Majnemer54c2ca22014-12-26 09:10:14 +0000403 void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
404 unsigned Depth = 0, Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000405 return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
David Majnemer54c2ca22014-12-26 09:10:14 +0000406 DT);
407 }
David Majnemer491331a2015-01-02 07:29:43 +0000408 OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
409 const Instruction *CxtI) {
Chandler Carruth66b31302015-01-04 12:03:27 +0000410 return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, AC, CxtI, DT);
David Majnemer491331a2015-01-02 07:29:43 +0000411 }
David Majnemer5310c1e2015-01-07 00:39:50 +0000412 OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
413 const Instruction *CxtI) {
414 return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, AC, CxtI, DT);
415 }
Chris Lattner35522b72010-01-04 07:12:23 +0000416
417private:
Chandler Carruth3a622162015-01-20 19:27:58 +0000418 /// \brief Performs a few simplifications for operators which are associative
419 /// or commutative.
Duncan Sands641baf12010-11-13 15:10:37 +0000420 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000421
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000422 /// \brief Tries to simplify binary operations which some other binary
423 /// operation distributes over.
Chandler Carruth3a622162015-01-20 19:27:58 +0000424 ///
425 /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
426 /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
427 /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
428 /// value, or null if it didn't simplify.
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000429 Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
Duncan Sandsadc7771f2010-11-23 14:23:47 +0000430
Chandler Carruth3a622162015-01-20 19:27:58 +0000431 /// \brief Attempts to replace V with a simpler value based on the demanded
432 /// bits.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000433 Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero,
Hal Finkel60db0582014-09-07 18:57:58 +0000434 APInt &KnownOne, unsigned Depth,
435 Instruction *CxtI = nullptr);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000436 bool SimplifyDemandedBits(Use &U, APInt DemandedMask, APInt &KnownZero,
437 APInt &KnownOne, unsigned Depth = 0);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000438 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
439 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
440 Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
441 APInt DemandedMask, APInt &KnownZero,
442 APInt &KnownOne);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000443
Chandler Carruth3a622162015-01-20 19:27:58 +0000444 /// \brief Tries to simplify operands to an integer instruction based on its
445 /// demanded bits.
Chris Lattner35522b72010-01-04 07:12:23 +0000446 bool SimplifyDemandedInstructionBits(Instruction &Inst);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000447
Chris Lattner35522b72010-01-04 07:12:23 +0000448 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000449 APInt &UndefElts, unsigned Depth = 0);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000450
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000451 Value *SimplifyVectorOp(BinaryOperator &Inst);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +0000452 Value *SimplifyBSwap(BinaryOperator &Inst);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000453
Chris Lattner35522b72010-01-04 07:12:23 +0000454 // FoldOpIntoPhi - Given a binary operator, cast instruction, or select
455 // which has a PHI node as operand #0, see if we can fold the instruction
456 // into the PHI (which is only possible if all operands to the PHI are
457 // constants).
458 //
Chris Lattnerea7131a2011-01-16 05:14:26 +0000459 Instruction *FoldOpIntoPhi(Instruction &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000460
Chandler Carruth3a622162015-01-20 19:27:58 +0000461 /// \brief Try to rotate an operation below a PHI node, using PHI nodes for
462 /// its operands.
Chris Lattner35522b72010-01-04 07:12:23 +0000463 Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
464 Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
465 Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
466 Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
467
Chris Lattner35522b72010-01-04 07:12:23 +0000468 Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
469 ConstantInt *AndRHS, BinaryOperator &TheAnd);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000470
Chris Lattner35522b72010-01-04 07:12:23 +0000471 Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
472 bool isSub, Instruction &I);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000473 Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned,
474 bool Inside);
Chris Lattner35522b72010-01-04 07:12:23 +0000475 Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
476 Instruction *MatchBSwap(BinaryOperator &I);
477 bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
478 Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
479 Instruction *SimplifyMemSet(MemSetInst *MI);
480
Chris Lattner229907c2011-07-18 04:54:35 +0000481 Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
Duncan Sands533c8ae2012-10-23 08:28:26 +0000482
Chandler Carruth3a622162015-01-20 19:27:58 +0000483 /// \brief Returns a value X such that Val = X * Scale, or null if none.
484 ///
485 /// If the multiplication is known not to overflow then NoSignedWrap is set.
Duncan Sands533c8ae2012-10-23 08:28:26 +0000486 Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
Chris Lattner35522b72010-01-04 07:12:23 +0000487};
488
Chris Lattner35522b72010-01-04 07:12:23 +0000489} // end namespace llvm.
490
Chandler Carruth964daaa2014-04-22 02:55:47 +0000491#undef DEBUG_TYPE
492
Chris Lattner35522b72010-01-04 07:12:23 +0000493#endif