blob: d0caf344d64440c0aa2b9c7bf5c0dfd6c1e566b0 [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
Chandler Carruth66b31302015-01-04 12:03:27 +000018#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruth5175b9a2015-01-20 08:35:24 +000019#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth452a0072014-03-04 11:59:06 +000020#include "llvm/Analysis/TargetFolder.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000021#include "llvm/Analysis/ValueTracking.h"
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +000022#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/IRBuilder.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000024#include "llvm/IR/InstVisitor.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Operator.h"
Hal Finkel74c2f352014-09-07 12:44:26 +000027#include "llvm/IR/PatternMatch.h"
Chris Lattner35522b72010-01-04 07:12:23 +000028#include "llvm/Pass.h"
Chandler Carruth83ba2692015-01-24 04:19:17 +000029#include "llvm/Transforms/InstCombine/InstCombineWorklist.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
Sanjoy Das82ea3d42015-02-24 00:08:41 +000083/// \brief Return true if the specified value is free to invert (apply ~ to).
84/// This happens in cases where the ~ can be eliminated. If WillInvertAllUses
85/// is true, work under the assumption that the caller intends to remove all
86/// uses of V and only keep uses of ~V.
87///
88static inline bool IsFreeToInvert(Value *V, bool WillInvertAllUses) {
89 // ~(~(X)) -> X.
90 if (BinaryOperator::isNot(V))
91 return true;
92
93 // Constants can be considered to be not'ed values.
94 if (isa<ConstantInt>(V))
95 return true;
96
97 // Compares can be inverted if all of their uses are being modified to use the
98 // ~V.
99 if (isa<CmpInst>(V))
100 return WillInvertAllUses;
101
102 // If `V` is of the form `A + Constant` then `-1 - V` can be folded into `(-1
103 // - Constant) - A` if we are willing to invert all of the uses.
104 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
105 if (BO->getOpcode() == Instruction::Add ||
106 BO->getOpcode() == Instruction::Sub)
107 if (isa<Constant>(BO->getOperand(0)) || isa<Constant>(BO->getOperand(1)))
108 return WillInvertAllUses;
109
110 return false;
111}
112
Sanjoy Dasb0984472015-04-08 04:27:22 +0000113
114/// \brief Specific patterns of overflow check idioms that we match.
115enum OverflowCheckFlavor {
116 OCF_UNSIGNED_ADD,
117 OCF_SIGNED_ADD,
118 OCF_UNSIGNED_SUB,
119 OCF_SIGNED_SUB,
120 OCF_UNSIGNED_MUL,
121 OCF_SIGNED_MUL,
122
123 OCF_INVALID
124};
125
126/// \brief Returns the OverflowCheckFlavor corresponding to a overflow_with_op
127/// intrinsic.
128static inline OverflowCheckFlavor
129IntrinsicIDToOverflowCheckFlavor(unsigned ID) {
130 switch (ID) {
131 default:
132 return OCF_INVALID;
133 case Intrinsic::uadd_with_overflow:
134 return OCF_UNSIGNED_ADD;
135 case Intrinsic::sadd_with_overflow:
136 return OCF_SIGNED_ADD;
137 case Intrinsic::usub_with_overflow:
138 return OCF_UNSIGNED_SUB;
139 case Intrinsic::ssub_with_overflow:
140 return OCF_SIGNED_SUB;
141 case Intrinsic::umul_with_overflow:
142 return OCF_UNSIGNED_MUL;
143 case Intrinsic::smul_with_overflow:
144 return OCF_SIGNED_MUL;
145 }
146}
147
Chandler Carruth3a622162015-01-20 19:27:58 +0000148/// \brief An IRBuilder inserter that adds new instructions to the instcombine
149/// worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000150class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter
Chris Lattner35522b72010-01-04 07:12:23 +0000151 : public IRBuilderDefaultInserter<true> {
152 InstCombineWorklist &Worklist;
Chandler Carruth66b31302015-01-04 12:03:27 +0000153 AssumptionCache *AC;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000154
Chris Lattner35522b72010-01-04 07:12:23 +0000155public:
Chandler Carruth66b31302015-01-04 12:03:27 +0000156 InstCombineIRInserter(InstCombineWorklist &WL, AssumptionCache *AC)
157 : Worklist(WL), AC(AC) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +0000158
Chandler Carruthd70cc602014-05-07 17:36:59 +0000159 void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB,
160 BasicBlock::iterator InsertPt) const {
Chris Lattner35522b72010-01-04 07:12:23 +0000161 IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
162 Worklist.Add(I);
Hal Finkel74c2f352014-09-07 12:44:26 +0000163
164 using namespace llvm::PatternMatch;
Benjamin Kramer63207bc2014-10-25 18:09:01 +0000165 if (match(I, m_Intrinsic<Intrinsic::assume>()))
Chandler Carruth66b31302015-01-04 12:03:27 +0000166 AC->registerAssumption(cast<CallInst>(I));
Chris Lattner35522b72010-01-04 07:12:23 +0000167 }
168};
Jakub Staszak190db2f2013-01-14 23:16:36 +0000169
Chandler Carruth3a622162015-01-20 19:27:58 +0000170/// \brief The core instruction combiner logic.
171///
172/// This class provides both the logic to recursively visit instructions and
173/// combine them, as well as the pass infrastructure for running this as part
174/// of the LLVM pass pipeline.
Duncan Sands6c5e4352010-05-11 20:16:09 +0000175class LLVM_LIBRARY_VISIBILITY InstCombiner
Chandler Carruth1edb9d62015-01-20 22:44:35 +0000176 : public InstVisitor<InstCombiner, Instruction *> {
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000177 // FIXME: These members shouldn't be public.
Chris Lattner35522b72010-01-04 07:12:23 +0000178public:
Chandler Carruth3a622162015-01-20 19:27:58 +0000179 /// \brief A worklist of the instructions that need to be simplified.
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000180 InstCombineWorklist &Worklist;
Chris Lattner35522b72010-01-04 07:12:23 +0000181
Chandler Carruth3a622162015-01-20 19:27:58 +0000182 /// \brief An IRBuilder that automatically inserts new instructions into the
183 /// worklist.
Chris Lattner35522b72010-01-04 07:12:23 +0000184 typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy;
185 BuilderTy *Builder;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000186
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000187private:
188 // Mode in which we are running the combiner.
189 const bool MinimizeSize;
190
191 // Required analyses.
192 // FIXME: These can never be null and should be references.
193 AssumptionCache *AC;
194 TargetLibraryInfo *TLI;
195 DominatorTree *DT;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000196 const DataLayout &DL;
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000197
198 // Optional analyses. When non-null, these can both be used to do better
199 // combining and will be updated to reflect any changes.
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000200 LoopInfo *LI;
201
202 bool MadeIRChange;
Chris Lattner35522b72010-01-04 07:12:23 +0000203
204public:
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000205 InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder,
206 bool MinimizeSize, AssumptionCache *AC, TargetLibraryInfo *TLI,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000207 DominatorTree *DT, const DataLayout &DL, LoopInfo *LI)
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000208 : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
209 AC(AC), TLI(TLI), DT(DT), DL(DL), LI(LI), MadeIRChange(false) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +0000210
Chandler Carruthdf5747a2015-01-21 11:38:17 +0000211 /// \brief Run the combiner over the entire worklist until it is empty.
212 ///
213 /// \returns true if the IR is changed.
214 bool run();
Chris Lattner35522b72010-01-04 07:12:23 +0000215
Chandler Carruth66b31302015-01-04 12:03:27 +0000216 AssumptionCache *getAssumptionCache() const { return AC; }
Hal Finkel74c2f352014-09-07 12:44:26 +0000217
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000218 const DataLayout &getDataLayout() const { return DL; }
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000219
Hal Finkel60db0582014-09-07 18:57:58 +0000220 DominatorTree *getDominatorTree() const { return DT; }
Chris Lattner35522b72010-01-04 07:12:23 +0000221
Chandler Carruth5175b9a2015-01-20 08:35:24 +0000222 LoopInfo *getLoopInfo() const { return LI; }
223
Chad Rosier43a33062011-12-02 01:26:24 +0000224 TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
225
Chris Lattner35522b72010-01-04 07:12:23 +0000226 // Visitation implementation - Implement instruction combining for different
227 // instruction types. The semantics are as follows:
228 // Return Value:
229 // null - No change was made
230 // I - Change was made, I is still valid, I may be dead though
231 // otherwise - Change was made, replace I with returned instruction
232 //
233 Instruction *visitAdd(BinaryOperator &I);
234 Instruction *visitFAdd(BinaryOperator &I);
Chris Lattner229907c2011-07-18 04:54:35 +0000235 Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
Chris Lattner35522b72010-01-04 07:12:23 +0000236 Instruction *visitSub(BinaryOperator &I);
237 Instruction *visitFSub(BinaryOperator &I);
238 Instruction *visitMul(BinaryOperator &I);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000239 Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000240 Instruction *InsertBefore);
Chris Lattner35522b72010-01-04 07:12:23 +0000241 Instruction *visitFMul(BinaryOperator &I);
242 Instruction *visitURem(BinaryOperator &I);
243 Instruction *visitSRem(BinaryOperator &I);
244 Instruction *visitFRem(BinaryOperator &I);
245 bool SimplifyDivRemOfSelect(BinaryOperator &I);
246 Instruction *commonRemTransforms(BinaryOperator &I);
247 Instruction *commonIRemTransforms(BinaryOperator &I);
248 Instruction *commonDivTransforms(BinaryOperator &I);
249 Instruction *commonIDivTransforms(BinaryOperator &I);
250 Instruction *visitUDiv(BinaryOperator &I);
251 Instruction *visitSDiv(BinaryOperator &I);
Frits van Bommel2a559512011-01-29 17:50:27 +0000252 Instruction *visitFDiv(BinaryOperator &I);
Erik Ecksteind1817522014-12-03 10:39:15 +0000253 Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
Chris Lattner067459c2010-03-05 08:46:26 +0000254 Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
255 Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chris Lattner35522b72010-01-04 07:12:23 +0000256 Instruction *visitAnd(BinaryOperator &I);
Hal Finkel60db0582014-09-07 18:57:58 +0000257 Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction *CxtI);
Chris Lattner067459c2010-03-05 08:46:26 +0000258 Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000259 Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A,
260 Value *B, Value *C);
David Majnemer5d1aeba2014-08-21 05:14:48 +0000261 Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A,
262 Value *B, Value *C);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000263 Instruction *visitOr(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000264 Instruction *visitXor(BinaryOperator &I);
265 Instruction *visitShl(BinaryOperator &I);
266 Instruction *visitAShr(BinaryOperator &I);
267 Instruction *visitLShr(BinaryOperator &I);
268 Instruction *commonShiftTransforms(BinaryOperator &I);
269 Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
270 Constant *RHSC);
271 Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
272 GlobalVariable *GV, CmpInst &ICI,
Craig Toppere73658d2014-04-28 04:05:08 +0000273 ConstantInt *AndCst = nullptr);
Chris Lattner35522b72010-01-04 07:12:23 +0000274 Instruction *visitFCmpInst(FCmpInst &I);
275 Instruction *visitICmpInst(ICmpInst &I);
276 Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000277 Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS,
Chris Lattner35522b72010-01-04 07:12:23 +0000278 ConstantInt *RHS);
279 Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
280 ConstantInt *DivRHS);
Chris Lattnerd369f572011-02-13 07:43:07 +0000281 Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI,
282 ConstantInt *DivRHS);
Suyog Sarda3a8c2c12014-07-22 19:19:36 +0000283 Instruction *FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A,
284 ConstantInt *CI1, ConstantInt *CI2);
David Majnemer59939ac2014-10-19 08:23:08 +0000285 Instruction *FoldICmpCstShlCst(ICmpInst &I, Value *Op, Value *A,
286 ConstantInt *CI1, ConstantInt *CI2);
Benjamin Kramer0e2d1622013-09-20 22:12:42 +0000287 Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI,
288 ICmpInst::Predicate Pred);
Chris Lattner35522b72010-01-04 07:12:23 +0000289 Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
290 ICmpInst::Predicate Cond, Instruction &I);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000291 Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattner35522b72010-01-04 07:12:23 +0000292 BinaryOperator &I);
293 Instruction *commonCastTransforms(CastInst &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000294 Instruction *commonPointerCastTransforms(CastInst &CI);
295 Instruction *visitTrunc(TruncInst &CI);
296 Instruction *visitZExt(ZExtInst &CI);
297 Instruction *visitSExt(SExtInst &CI);
298 Instruction *visitFPTrunc(FPTruncInst &CI);
299 Instruction *visitFPExt(CastInst &CI);
300 Instruction *visitFPToUI(FPToUIInst &FI);
301 Instruction *visitFPToSI(FPToSIInst &FI);
302 Instruction *visitUIToFP(CastInst &CI);
303 Instruction *visitSIToFP(CastInst &CI);
304 Instruction *visitPtrToInt(PtrToIntInst &CI);
305 Instruction *visitIntToPtr(IntToPtrInst &CI);
306 Instruction *visitBitCast(BitCastInst &CI);
Matt Arsenaulta9e95ab2013-11-15 05:45:08 +0000307 Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000308 Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
309 Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *);
Chris Lattner35522b72010-01-04 07:12:23 +0000310 Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
311 Value *A, Value *B, Instruction &Outer,
312 SelectPatternFlavor SPF2, Value *C);
Mehdi Aminib9a0fa42015-02-16 21:47:54 +0000313 Instruction *FoldItoFPtoI(Instruction &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000314 Instruction *visitSelectInst(SelectInst &SI);
315 Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
316 Instruction *visitCallInst(CallInst &CI);
317 Instruction *visitInvokeInst(InvokeInst &II);
318
319 Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
320 Instruction *visitPHINode(PHINode &PN);
321 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
322 Instruction *visitAllocaInst(AllocaInst &AI);
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000323 Instruction *visitAllocSite(Instruction &FI);
Gabor Greif75f69432010-06-24 12:21:15 +0000324 Instruction *visitFree(CallInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000325 Instruction *visitLoadInst(LoadInst &LI);
326 Instruction *visitStoreInst(StoreInst &SI);
327 Instruction *visitBranchInst(BranchInst &BI);
328 Instruction *visitSwitchInst(SwitchInst &SI);
Hal Finkel93873cc12014-09-07 21:28:34 +0000329 Instruction *visitReturnInst(ReturnInst &RI);
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000330 Instruction *visitInsertValueInst(InsertValueInst &IV);
Chris Lattner35522b72010-01-04 07:12:23 +0000331 Instruction *visitInsertElementInst(InsertElementInst &IE);
332 Instruction *visitExtractElementInst(ExtractElementInst &EI);
333 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
334 Instruction *visitExtractValueInst(ExtractValueInst &EV);
Duncan Sands5c055792011-09-30 13:12:16 +0000335 Instruction *visitLandingPadInst(LandingPadInst &LI);
Chris Lattner35522b72010-01-04 07:12:23 +0000336
337 // visitInstruction - Specify what to return for unhandled instructions...
Craig Toppere73658d2014-04-28 04:05:08 +0000338 Instruction *visitInstruction(Instruction &I) { return nullptr; }
Chris Lattner35522b72010-01-04 07:12:23 +0000339
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +0000340 // True when DB dominates all uses of DI execpt UI.
341 // UI must be in the same block as DI.
342 // The routine checks that the DI parent and DB are different.
343 bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
344 const BasicBlock *DB) const;
345
346 // Replace select with select operand SIOpd in SI-ICmp sequence when possible
347 bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
348 const unsigned SIOpd);
349
Chris Lattner35522b72010-01-04 07:12:23 +0000350private:
Chris Lattner229907c2011-07-18 04:54:35 +0000351 bool ShouldChangeType(Type *From, Type *To) const;
Chris Lattner2188e402010-01-04 07:37:31 +0000352 Value *dyn_castNegVal(Value *V) const;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000353 Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
David Blaikie87ca1b62015-03-27 20:56:11 +0000354 Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000355 SmallVectorImpl<Value *> &NewIndices);
Chris Lattner2b295a02010-01-04 07:53:58 +0000356 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000357
Chandler Carruth3a622162015-01-20 19:27:58 +0000358 /// \brief Classify whether a cast is worth optimizing.
359 ///
360 /// Returns true if the cast from "V to Ty" actually results in any code
361 /// being generated and is interesting to optimize out. If the cast can be
362 /// eliminated by some other simple transformation, we prefer to do the
363 /// simplification first.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000364 bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V,
Chris Lattner229907c2011-07-18 04:54:35 +0000365 Type *Ty);
Chris Lattner2188e402010-01-04 07:37:31 +0000366
Sanjoy Dasb0984472015-04-08 04:27:22 +0000367 /// \brief Try to optimize a sequence of instructions checking if an operation
368 /// on LHS and RHS overflows.
369 ///
370 /// If a simplification is possible, stores the simplified result of the
371 /// operation in OperationResult and result of the overflow check in
372 /// OverflowResult, and return true. If no simplification is possible,
373 /// returns false.
374 bool OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS, Value *RHS,
375 Instruction &CtxI, Value *&OperationResult,
376 Constant *&OverflowResult);
377
Chris Lattner35522b72010-01-04 07:12:23 +0000378 Instruction *visitCallSite(CallSite CS);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000379 Instruction *tryOptimizeCall(CallInst *CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000380 bool transformConstExprCastCall(CallSite CS);
Duncan Sandsa0984362011-09-06 13:37:06 +0000381 Instruction *transformCallThroughTrampoline(CallSite CS,
382 IntrinsicInst *Tramp);
Chris Lattner35522b72010-01-04 07:12:23 +0000383 Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
384 bool DoXform = true);
Benjamin Kramer398b8c52011-04-01 20:09:03 +0000385 Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000386 bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS, Instruction &CxtI);
387 bool WillNotOverflowSignedSub(Value *LHS, Value *RHS, Instruction &CxtI);
388 bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, Instruction &CxtI);
389 bool WillNotOverflowSignedMul(Value *LHS, Value *RHS, Instruction &CxtI);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000390 Value *EmitGEPOffset(User *GEP);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000391 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
Nick Lewyckya2b77202013-05-31 00:59:42 +0000392 Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
Chris Lattner35522b72010-01-04 07:12:23 +0000393
394public:
Chandler Carruth3a622162015-01-20 19:27:58 +0000395 /// \brief Inserts an instruction \p New before instruction \p Old
396 ///
397 /// Also adds the new instruction to the worklist and returns \p New so that
398 /// it is suitable for use as the return from the visitation patterns.
Chris Lattner35522b72010-01-04 07:12:23 +0000399 Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
Craig Toppere73658d2014-04-28 04:05:08 +0000400 assert(New && !New->getParent() &&
Chris Lattner35522b72010-01-04 07:12:23 +0000401 "New instruction already inserted into a basic block!");
402 BasicBlock *BB = Old.getParent();
Chandler Carruthd70cc602014-05-07 17:36:59 +0000403 BB->getInstList().insert(&Old, New); // Insert inst
Chris Lattner35522b72010-01-04 07:12:23 +0000404 Worklist.Add(New);
405 return New;
406 }
Eli Friedman6efb64e2011-05-19 01:20:42 +0000407
Chandler Carruth3a622162015-01-20 19:27:58 +0000408 /// \brief Same as InsertNewInstBefore, but also sets the debug loc.
Eli Friedman6efb64e2011-05-19 01:20:42 +0000409 Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
410 New->setDebugLoc(Old.getDebugLoc());
411 return InsertNewInstBefore(New, Old);
412 }
413
Chandler Carruth3a622162015-01-20 19:27:58 +0000414 /// \brief A combiner-aware RAUW-like routine.
415 ///
416 /// This method is to be used when an instruction is found to be dead,
417 /// replacable with another preexisting expression. Here we add all uses of
418 /// I to the worklist, replace all uses of I with the new value, then return
419 /// I, so that the inst combiner will know that I was modified.
Chris Lattner35522b72010-01-04 07:12:23 +0000420 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
Owen Anderson51b75b8c2015-03-10 05:13:47 +0000421 // If there are no uses to replace, then we return nullptr to indicate that
422 // no changes were made to the program.
423 if (I.use_empty()) return nullptr;
424
Chandler Carruthd70cc602014-05-07 17:36:59 +0000425 Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000426
Chris Lattner35522b72010-01-04 07:12:23 +0000427 // If we are replacing the instruction with itself, this must be in a
428 // segment of unreachable code, so just clobber the instruction.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000429 if (&I == V)
Chris Lattner35522b72010-01-04 07:12:23 +0000430 V = UndefValue::get(I.getType());
Frits van Bommeld14d9912011-03-27 23:32:31 +0000431
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000432 DEBUG(dbgs() << "IC: Replacing " << I << "\n"
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000433 << " with " << *V << '\n');
Frits van Bommeld14d9912011-03-27 23:32:31 +0000434
Chris Lattner35522b72010-01-04 07:12:23 +0000435 I.replaceAllUsesWith(V);
436 return &I;
437 }
438
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000439 /// Creates a result tuple for an overflow intrinsic \p II with a given
Sanjoy Dasb0984472015-04-08 04:27:22 +0000440 /// \p Result and a constant \p Overflow value.
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000441 Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result,
Sanjoy Dasb0984472015-04-08 04:27:22 +0000442 Constant *Overflow) {
443 Constant *V[] = {UndefValue::get(Result->getType()), Overflow};
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000444 StructType *ST = cast<StructType>(II->getType());
445 Constant *Struct = ConstantStruct::get(ST, V);
446 return InsertValueInst::Create(Struct, Result, 0);
447 }
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000448
Chandler Carruth3a622162015-01-20 19:27:58 +0000449 /// \brief Combiner aware instruction erasure.
450 ///
451 /// When dealing with an instruction that has side effects or produces a void
452 /// value, we can't rely on DCE to delete the instruction. Instead, visit
453 /// methods should return the value returned by this function.
Chris Lattner35522b72010-01-04 07:12:23 +0000454 Instruction *EraseInstFromFunction(Instruction &I) {
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000455 DEBUG(dbgs() << "IC: ERASE " << I << '\n');
Chris Lattner35522b72010-01-04 07:12:23 +0000456
457 assert(I.use_empty() && "Cannot erase instruction that is used!");
458 // Make sure that we reprocess all operands now that we reduced their
459 // use counts.
460 if (I.getNumOperands() < 8) {
461 for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
462 if (Instruction *Op = dyn_cast<Instruction>(*i))
463 Worklist.Add(Op);
464 }
465 Worklist.Remove(&I);
466 I.eraseFromParent();
467 MadeIRChange = true;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000468 return nullptr; // Don't do anything with FI
Chris Lattner35522b72010-01-04 07:12:23 +0000469 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000470
Jay Foada0653a32014-05-14 21:14:37 +0000471 void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000472 unsigned Depth, Instruction *CxtI) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000473 return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
474 DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000475 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000476
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000477 bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth = 0,
Hal Finkel60db0582014-09-07 18:57:58 +0000478 Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000479 return llvm::MaskedValueIsZero(V, Mask, DL, Depth, AC, CxtI, DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000480 }
Hal Finkel60db0582014-09-07 18:57:58 +0000481 unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0,
482 Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000483 return llvm::ComputeNumSignBits(Op, DL, Depth, AC, CxtI, DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000484 }
David Majnemer54c2ca22014-12-26 09:10:14 +0000485 void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
486 unsigned Depth = 0, Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000487 return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
David Majnemer54c2ca22014-12-26 09:10:14 +0000488 DT);
489 }
David Majnemer491331a2015-01-02 07:29:43 +0000490 OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
491 const Instruction *CxtI) {
Chandler Carruth66b31302015-01-04 12:03:27 +0000492 return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, AC, CxtI, DT);
David Majnemer491331a2015-01-02 07:29:43 +0000493 }
David Majnemer5310c1e2015-01-07 00:39:50 +0000494 OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
495 const Instruction *CxtI) {
496 return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, AC, CxtI, DT);
497 }
Chris Lattner35522b72010-01-04 07:12:23 +0000498
499private:
Chandler Carruth3a622162015-01-20 19:27:58 +0000500 /// \brief Performs a few simplifications for operators which are associative
501 /// or commutative.
Duncan Sands641baf12010-11-13 15:10:37 +0000502 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000503
Chandler Carruthb3d03df2015-01-20 21:10:35 +0000504 /// \brief Tries to simplify binary operations which some other binary
505 /// operation distributes over.
Chandler Carruth3a622162015-01-20 19:27:58 +0000506 ///
507 /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
508 /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
509 /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
510 /// value, or null if it didn't simplify.
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000511 Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
Duncan Sandsadc7771f2010-11-23 14:23:47 +0000512
Chandler Carruth3a622162015-01-20 19:27:58 +0000513 /// \brief Attempts to replace V with a simpler value based on the demanded
514 /// bits.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000515 Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero,
Hal Finkel60db0582014-09-07 18:57:58 +0000516 APInt &KnownOne, unsigned Depth,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000517 Instruction *CxtI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000518 bool SimplifyDemandedBits(Use &U, APInt DemandedMask, APInt &KnownZero,
519 APInt &KnownOne, unsigned Depth = 0);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000520 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
521 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
522 Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
523 APInt DemandedMask, APInt &KnownZero,
524 APInt &KnownOne);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000525
Chandler Carruth3a622162015-01-20 19:27:58 +0000526 /// \brief Tries to simplify operands to an integer instruction based on its
527 /// demanded bits.
Chris Lattner35522b72010-01-04 07:12:23 +0000528 bool SimplifyDemandedInstructionBits(Instruction &Inst);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000529
Chris Lattner35522b72010-01-04 07:12:23 +0000530 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000531 APInt &UndefElts, unsigned Depth = 0);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000532
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000533 Value *SimplifyVectorOp(BinaryOperator &Inst);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +0000534 Value *SimplifyBSwap(BinaryOperator &Inst);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000535
Chris Lattner35522b72010-01-04 07:12:23 +0000536 // FoldOpIntoPhi - Given a binary operator, cast instruction, or select
537 // which has a PHI node as operand #0, see if we can fold the instruction
538 // into the PHI (which is only possible if all operands to the PHI are
539 // constants).
540 //
Chris Lattnerea7131a2011-01-16 05:14:26 +0000541 Instruction *FoldOpIntoPhi(Instruction &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000542
Chandler Carruth3a622162015-01-20 19:27:58 +0000543 /// \brief Try to rotate an operation below a PHI node, using PHI nodes for
544 /// its operands.
Chris Lattner35522b72010-01-04 07:12:23 +0000545 Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
546 Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
547 Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
548 Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
549
Chris Lattner35522b72010-01-04 07:12:23 +0000550 Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
551 ConstantInt *AndRHS, BinaryOperator &TheAnd);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000552
Chris Lattner35522b72010-01-04 07:12:23 +0000553 Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
554 bool isSub, Instruction &I);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000555 Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned,
556 bool Inside);
Chris Lattner35522b72010-01-04 07:12:23 +0000557 Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
558 Instruction *MatchBSwap(BinaryOperator &I);
559 bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
560 Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
561 Instruction *SimplifyMemSet(MemSetInst *MI);
562
Chris Lattner229907c2011-07-18 04:54:35 +0000563 Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
Duncan Sands533c8ae2012-10-23 08:28:26 +0000564
Chandler Carruth3a622162015-01-20 19:27:58 +0000565 /// \brief Returns a value X such that Val = X * Scale, or null if none.
566 ///
567 /// If the multiplication is known not to overflow then NoSignedWrap is set.
Duncan Sands533c8ae2012-10-23 08:28:26 +0000568 Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
Chris Lattner35522b72010-01-04 07:12:23 +0000569};
570
Chris Lattner35522b72010-01-04 07:12:23 +0000571} // end namespace llvm.
572
Chandler Carruth964daaa2014-04-22 02:55:47 +0000573#undef DEBUG_TYPE
574
Chris Lattner35522b72010-01-04 07:12:23 +0000575#endif