blob: e28a29ffe5d55ba4e2af7e5ed5cd5a08f6eba6b4 [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"
Meador Ingedf796f82012-10-13 16:45:24 +000025#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
Chris Lattner35522b72010-01-04 07:12:23 +000026
Chandler Carruth964daaa2014-04-22 02:55:47 +000027#define DEBUG_TYPE "instcombine"
28
Chris Lattner35522b72010-01-04 07:12:23 +000029namespace llvm {
Chandler Carruthd70cc602014-05-07 17:36:59 +000030class CallSite;
31class DataLayout;
Hal Finkel60db0582014-09-07 18:57:58 +000032class DominatorTree;
Chandler Carruthd70cc602014-05-07 17:36:59 +000033class TargetLibraryInfo;
34class DbgDeclareInst;
35class MemIntrinsic;
36class MemSetInst;
Jakub Staszak190db2f2013-01-14 23:16:36 +000037
Chris Lattner35522b72010-01-04 07:12:23 +000038/// SelectPatternFlavor - We can match a variety of different patterns for
39/// select operations.
40enum SelectPatternFlavor {
41 SPF_UNKNOWN = 0,
Chandler Carruthd70cc602014-05-07 17:36:59 +000042 SPF_SMIN,
43 SPF_UMIN,
44 SPF_SMAX,
Dinesh Dwivedi3217b6c2014-06-06 06:54:45 +000045 SPF_UMAX,
46 SPF_ABS,
47 SPF_NABS
Chris Lattner35522b72010-01-04 07:12:23 +000048};
Jakub Staszak190db2f2013-01-14 23:16:36 +000049
Chris Lattner2188e402010-01-04 07:37:31 +000050/// getComplexity: Assign a complexity or rank value to LLVM Values...
51/// 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
52static inline unsigned getComplexity(Value *V) {
53 if (isa<Instruction>(V)) {
Chandler Carruthd70cc602014-05-07 17:36:59 +000054 if (BinaryOperator::isNeg(V) || BinaryOperator::isFNeg(V) ||
Chris Lattner2188e402010-01-04 07:37:31 +000055 BinaryOperator::isNot(V))
56 return 3;
57 return 4;
58 }
Chandler Carruthd70cc602014-05-07 17:36:59 +000059 if (isa<Argument>(V))
60 return 3;
Chris Lattner2188e402010-01-04 07:37:31 +000061 return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
62}
Chris Lattner35522b72010-01-04 07:12:23 +000063
Benjamin Kramer970f4952014-01-19 16:56:10 +000064/// AddOne - Add one to a Constant
65static inline Constant *AddOne(Constant *C) {
66 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
67}
68/// SubOne - Subtract one from a Constant
69static inline Constant *SubOne(Constant *C) {
70 return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
71}
72
Chris Lattner35522b72010-01-04 07:12:23 +000073/// InstCombineIRInserter - This is an IRBuilder insertion helper that works
74/// just like the normal insertion helper, but also adds any new instructions
75/// to the instcombine worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +000076class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter
Chris Lattner35522b72010-01-04 07:12:23 +000077 : public IRBuilderDefaultInserter<true> {
78 InstCombineWorklist &Worklist;
Chandler Carruth66b31302015-01-04 12:03:27 +000079 AssumptionCache *AC;
Chandler Carruthd70cc602014-05-07 17:36:59 +000080
Chris Lattner35522b72010-01-04 07:12:23 +000081public:
Chandler Carruth66b31302015-01-04 12:03:27 +000082 InstCombineIRInserter(InstCombineWorklist &WL, AssumptionCache *AC)
83 : Worklist(WL), AC(AC) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +000084
Chandler Carruthd70cc602014-05-07 17:36:59 +000085 void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB,
86 BasicBlock::iterator InsertPt) const {
Chris Lattner35522b72010-01-04 07:12:23 +000087 IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
88 Worklist.Add(I);
Hal Finkel74c2f352014-09-07 12:44:26 +000089
90 using namespace llvm::PatternMatch;
Benjamin Kramer63207bc2014-10-25 18:09:01 +000091 if (match(I, m_Intrinsic<Intrinsic::assume>()))
Chandler Carruth66b31302015-01-04 12:03:27 +000092 AC->registerAssumption(cast<CallInst>(I));
Chris Lattner35522b72010-01-04 07:12:23 +000093 }
94};
Jakub Staszak190db2f2013-01-14 23:16:36 +000095
Chris Lattner35522b72010-01-04 07:12:23 +000096/// InstCombiner - The -instcombine pass.
Duncan Sands6c5e4352010-05-11 20:16:09 +000097class LLVM_LIBRARY_VISIBILITY InstCombiner
Chandler Carruthd70cc602014-05-07 17:36:59 +000098 : public FunctionPass,
99 public InstVisitor<InstCombiner, Instruction *> {
Chandler Carruth66b31302015-01-04 12:03:27 +0000100 AssumptionCache *AC;
Rafael Espindolaaeff8a92014-02-24 23:12:18 +0000101 const DataLayout *DL;
Gerolf Hoflehnerc0b4c202014-10-07 00:16:12 +0000102 TargetLibraryInfo *TLI;
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +0000103 DominatorTree *DT;
Chandler Carruth5175b9a2015-01-20 08:35:24 +0000104 LoopInfo *LI;
Chris Lattner35522b72010-01-04 07:12:23 +0000105 bool MadeIRChange;
Meador Ingedf796f82012-10-13 16:45:24 +0000106 LibCallSimplifier *Simplifier;
Quentin Colombet3b2db0b2013-01-07 18:37:41 +0000107 bool MinimizeSize;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000108
Chris Lattner35522b72010-01-04 07:12:23 +0000109public:
110 /// Worklist - All of the instructions that need to be simplified.
111 InstCombineWorklist Worklist;
112
113 /// Builder - This is an IRBuilder that automatically inserts new
114 /// instructions into the worklist when they are created.
115 typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy;
116 BuilderTy *Builder;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000117
Chris Lattner35522b72010-01-04 07:12:23 +0000118 static char ID; // Pass identification, replacement for typeid
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +0000119 InstCombiner()
Chandler Carruth5175b9a2015-01-20 08:35:24 +0000120 : FunctionPass(ID), DL(nullptr), DT(nullptr), LI(nullptr),
121 Builder(nullptr) {
Quentin Colombet3b2db0b2013-01-07 18:37:41 +0000122 MinimizeSize = false;
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000123 initializeInstCombinerPass(*PassRegistry::getPassRegistry());
124 }
Chris Lattner35522b72010-01-04 07:12:23 +0000125
126public:
Craig Topper3e4c6972014-03-05 09:10:37 +0000127 bool runOnFunction(Function &F) override;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000128
Chris Lattner35522b72010-01-04 07:12:23 +0000129 bool DoOneIteration(Function &F, unsigned ItNum);
130
Craig Topper3e4c6972014-03-05 09:10:37 +0000131 void getAnalysisUsage(AnalysisUsage &AU) const override;
Chad Rosiere6de63d2011-12-01 21:29:16 +0000132
Chandler Carruth66b31302015-01-04 12:03:27 +0000133 AssumptionCache *getAssumptionCache() const { return AC; }
Hal Finkel74c2f352014-09-07 12:44:26 +0000134
Rafael Espindolaaeff8a92014-02-24 23:12:18 +0000135 const DataLayout *getDataLayout() const { return DL; }
Hal Finkel60db0582014-09-07 18:57:58 +0000136
137 DominatorTree *getDominatorTree() const { return DT; }
Chris Lattner35522b72010-01-04 07:12:23 +0000138
Chandler Carruth5175b9a2015-01-20 08:35:24 +0000139 LoopInfo *getLoopInfo() const { return LI; }
140
Chad Rosier43a33062011-12-02 01:26:24 +0000141 TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
142
Chris Lattner35522b72010-01-04 07:12:23 +0000143 // Visitation implementation - Implement instruction combining for different
144 // instruction types. The semantics are as follows:
145 // Return Value:
146 // null - No change was made
147 // I - Change was made, I is still valid, I may be dead though
148 // otherwise - Change was made, replace I with returned instruction
149 //
150 Instruction *visitAdd(BinaryOperator &I);
151 Instruction *visitFAdd(BinaryOperator &I);
Chris Lattner229907c2011-07-18 04:54:35 +0000152 Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
Chris Lattner35522b72010-01-04 07:12:23 +0000153 Instruction *visitSub(BinaryOperator &I);
154 Instruction *visitFSub(BinaryOperator &I);
155 Instruction *visitMul(BinaryOperator &I);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000156 Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000157 Instruction *InsertBefore);
Chris Lattner35522b72010-01-04 07:12:23 +0000158 Instruction *visitFMul(BinaryOperator &I);
159 Instruction *visitURem(BinaryOperator &I);
160 Instruction *visitSRem(BinaryOperator &I);
161 Instruction *visitFRem(BinaryOperator &I);
162 bool SimplifyDivRemOfSelect(BinaryOperator &I);
163 Instruction *commonRemTransforms(BinaryOperator &I);
164 Instruction *commonIRemTransforms(BinaryOperator &I);
165 Instruction *commonDivTransforms(BinaryOperator &I);
166 Instruction *commonIDivTransforms(BinaryOperator &I);
167 Instruction *visitUDiv(BinaryOperator &I);
168 Instruction *visitSDiv(BinaryOperator &I);
Frits van Bommel2a559512011-01-29 17:50:27 +0000169 Instruction *visitFDiv(BinaryOperator &I);
Erik Ecksteind1817522014-12-03 10:39:15 +0000170 Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
Chris Lattner067459c2010-03-05 08:46:26 +0000171 Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
172 Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chris Lattner35522b72010-01-04 07:12:23 +0000173 Instruction *visitAnd(BinaryOperator &I);
Hal Finkel60db0582014-09-07 18:57:58 +0000174 Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction *CxtI);
Chris Lattner067459c2010-03-05 08:46:26 +0000175 Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000176 Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A,
177 Value *B, Value *C);
David Majnemer5d1aeba2014-08-21 05:14:48 +0000178 Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A,
179 Value *B, Value *C);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000180 Instruction *visitOr(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000181 Instruction *visitXor(BinaryOperator &I);
182 Instruction *visitShl(BinaryOperator &I);
183 Instruction *visitAShr(BinaryOperator &I);
184 Instruction *visitLShr(BinaryOperator &I);
185 Instruction *commonShiftTransforms(BinaryOperator &I);
186 Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
187 Constant *RHSC);
188 Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
189 GlobalVariable *GV, CmpInst &ICI,
Craig Toppere73658d2014-04-28 04:05:08 +0000190 ConstantInt *AndCst = nullptr);
Chris Lattner35522b72010-01-04 07:12:23 +0000191 Instruction *visitFCmpInst(FCmpInst &I);
192 Instruction *visitICmpInst(ICmpInst &I);
193 Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000194 Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS,
Chris Lattner35522b72010-01-04 07:12:23 +0000195 ConstantInt *RHS);
196 Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
197 ConstantInt *DivRHS);
Chris Lattnerd369f572011-02-13 07:43:07 +0000198 Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI,
199 ConstantInt *DivRHS);
Suyog Sarda3a8c2c12014-07-22 19:19:36 +0000200 Instruction *FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A,
201 ConstantInt *CI1, ConstantInt *CI2);
David Majnemer59939ac2014-10-19 08:23:08 +0000202 Instruction *FoldICmpCstShlCst(ICmpInst &I, Value *Op, Value *A,
203 ConstantInt *CI1, ConstantInt *CI2);
Benjamin Kramer0e2d1622013-09-20 22:12:42 +0000204 Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI,
205 ICmpInst::Predicate Pred);
Chris Lattner35522b72010-01-04 07:12:23 +0000206 Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
207 ICmpInst::Predicate Cond, Instruction &I);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000208 Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattner35522b72010-01-04 07:12:23 +0000209 BinaryOperator &I);
210 Instruction *commonCastTransforms(CastInst &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000211 Instruction *commonPointerCastTransforms(CastInst &CI);
212 Instruction *visitTrunc(TruncInst &CI);
213 Instruction *visitZExt(ZExtInst &CI);
214 Instruction *visitSExt(SExtInst &CI);
215 Instruction *visitFPTrunc(FPTruncInst &CI);
216 Instruction *visitFPExt(CastInst &CI);
217 Instruction *visitFPToUI(FPToUIInst &FI);
218 Instruction *visitFPToSI(FPToSIInst &FI);
219 Instruction *visitUIToFP(CastInst &CI);
220 Instruction *visitSIToFP(CastInst &CI);
221 Instruction *visitPtrToInt(PtrToIntInst &CI);
222 Instruction *visitIntToPtr(IntToPtrInst &CI);
223 Instruction *visitBitCast(BitCastInst &CI);
Matt Arsenaulta9e95ab2013-11-15 05:45:08 +0000224 Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000225 Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
226 Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *);
Chris Lattner35522b72010-01-04 07:12:23 +0000227 Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
228 Value *A, Value *B, Instruction &Outer,
229 SelectPatternFlavor SPF2, Value *C);
230 Instruction *visitSelectInst(SelectInst &SI);
231 Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
232 Instruction *visitCallInst(CallInst &CI);
233 Instruction *visitInvokeInst(InvokeInst &II);
234
235 Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
236 Instruction *visitPHINode(PHINode &PN);
237 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
238 Instruction *visitAllocaInst(AllocaInst &AI);
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000239 Instruction *visitAllocSite(Instruction &FI);
Gabor Greif75f69432010-06-24 12:21:15 +0000240 Instruction *visitFree(CallInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000241 Instruction *visitLoadInst(LoadInst &LI);
242 Instruction *visitStoreInst(StoreInst &SI);
243 Instruction *visitBranchInst(BranchInst &BI);
244 Instruction *visitSwitchInst(SwitchInst &SI);
Hal Finkel93873cc12014-09-07 21:28:34 +0000245 Instruction *visitReturnInst(ReturnInst &RI);
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000246 Instruction *visitInsertValueInst(InsertValueInst &IV);
Chris Lattner35522b72010-01-04 07:12:23 +0000247 Instruction *visitInsertElementInst(InsertElementInst &IE);
248 Instruction *visitExtractElementInst(ExtractElementInst &EI);
249 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
250 Instruction *visitExtractValueInst(ExtractValueInst &EV);
Duncan Sands5c055792011-09-30 13:12:16 +0000251 Instruction *visitLandingPadInst(LandingPadInst &LI);
Chris Lattner35522b72010-01-04 07:12:23 +0000252
253 // visitInstruction - Specify what to return for unhandled instructions...
Craig Toppere73658d2014-04-28 04:05:08 +0000254 Instruction *visitInstruction(Instruction &I) { return nullptr; }
Chris Lattner35522b72010-01-04 07:12:23 +0000255
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +0000256 // True when DB dominates all uses of DI execpt UI.
257 // UI must be in the same block as DI.
258 // The routine checks that the DI parent and DB are different.
259 bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
260 const BasicBlock *DB) const;
261
262 // Replace select with select operand SIOpd in SI-ICmp sequence when possible
263 bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
264 const unsigned SIOpd);
265
Chris Lattner35522b72010-01-04 07:12:23 +0000266private:
Chris Lattner229907c2011-07-18 04:54:35 +0000267 bool ShouldChangeType(Type *From, Type *To) const;
Chris Lattner2188e402010-01-04 07:37:31 +0000268 Value *dyn_castNegVal(Value *V) const;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000269 Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
Matt Arsenaultd79f7d92013-08-19 22:17:40 +0000270 Type *FindElementAtOffset(Type *PtrTy, int64_t Offset,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000271 SmallVectorImpl<Value *> &NewIndices);
Chris Lattner2b295a02010-01-04 07:53:58 +0000272 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000273
Chris Lattner4e8137d2010-02-11 06:26:33 +0000274 /// ShouldOptimizeCast - Return true if the cast from "V to Ty" actually
275 /// results in any code being generated and is interesting to optimize out. If
276 /// the cast can be eliminated by some other simple transformation, we prefer
277 /// to do the simplification first.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000278 bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V,
Chris Lattner229907c2011-07-18 04:54:35 +0000279 Type *Ty);
Chris Lattner2188e402010-01-04 07:37:31 +0000280
Chris Lattner35522b72010-01-04 07:12:23 +0000281 Instruction *visitCallSite(CallSite CS);
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000282 Instruction *tryOptimizeCall(CallInst *CI, const DataLayout *DL);
Chris Lattner35522b72010-01-04 07:12:23 +0000283 bool transformConstExprCastCall(CallSite CS);
Duncan Sandsa0984362011-09-06 13:37:06 +0000284 Instruction *transformCallThroughTrampoline(CallSite CS,
285 IntrinsicInst *Tramp);
Chris Lattner35522b72010-01-04 07:12:23 +0000286 Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
287 bool DoXform = true);
Benjamin Kramer398b8c52011-04-01 20:09:03 +0000288 Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
Hal Finkel60db0582014-09-07 18:57:58 +0000289 bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS, Instruction *CxtI);
Hal Finkel60db0582014-09-07 18:57:58 +0000290 bool WillNotOverflowSignedSub(Value *LHS, Value *RHS, Instruction *CxtI);
291 bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, Instruction *CxtI);
Erik Ecksteina451b9b2014-12-17 07:29:19 +0000292 bool WillNotOverflowSignedMul(Value *LHS, Value *RHS, Instruction *CxtI);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000293 Value *EmitGEPOffset(User *GEP);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000294 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
Nick Lewyckya2b77202013-05-31 00:59:42 +0000295 Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
Chris Lattner35522b72010-01-04 07:12:23 +0000296
297public:
298 // InsertNewInstBefore - insert an instruction New before instruction Old
299 // in the program. Add the new instruction to the worklist.
300 //
301 Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
Craig Toppere73658d2014-04-28 04:05:08 +0000302 assert(New && !New->getParent() &&
Chris Lattner35522b72010-01-04 07:12:23 +0000303 "New instruction already inserted into a basic block!");
304 BasicBlock *BB = Old.getParent();
Chandler Carruthd70cc602014-05-07 17:36:59 +0000305 BB->getInstList().insert(&Old, New); // Insert inst
Chris Lattner35522b72010-01-04 07:12:23 +0000306 Worklist.Add(New);
307 return New;
308 }
Eli Friedman6efb64e2011-05-19 01:20:42 +0000309
Jakub Staszak190db2f2013-01-14 23:16:36 +0000310 // InsertNewInstWith - same as InsertNewInstBefore, but also sets the
Eli Friedman6efb64e2011-05-19 01:20:42 +0000311 // debug loc.
312 //
313 Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
314 New->setDebugLoc(Old.getDebugLoc());
315 return InsertNewInstBefore(New, Old);
316 }
317
Chris Lattner35522b72010-01-04 07:12:23 +0000318 // ReplaceInstUsesWith - This method is to be used when an instruction is
319 // found to be dead, replacable with another preexisting expression. Here
320 // we add all uses of I to the worklist, replace all uses of I with the new
321 // value, then return I, so that the inst combiner will know that I was
322 // modified.
323 //
324 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
Chandler Carruthd70cc602014-05-07 17:36:59 +0000325 Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000326
Chris Lattner35522b72010-01-04 07:12:23 +0000327 // If we are replacing the instruction with itself, this must be in a
328 // segment of unreachable code, so just clobber the instruction.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000329 if (&I == V)
Chris Lattner35522b72010-01-04 07:12:23 +0000330 V = UndefValue::get(I.getType());
Frits van Bommeld14d9912011-03-27 23:32:31 +0000331
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000332 DEBUG(dbgs() << "IC: Replacing " << I << "\n"
Frits van Bommeld14d9912011-03-27 23:32:31 +0000333 " with " << *V << '\n');
334
Chris Lattner35522b72010-01-04 07:12:23 +0000335 I.replaceAllUsesWith(V);
336 return &I;
337 }
338
Erik Eckstein096ff7d2014-12-11 08:02:30 +0000339 /// Creates a result tuple for an overflow intrinsic \p II with a given
340 /// \p Result and a constant \p Overflow value. If \p ReUseName is true the
341 /// \p Result's name is taken from \p II.
342 Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result,
343 bool Overflow, bool ReUseName = true) {
344 if (ReUseName)
345 Result->takeName(II);
346 Constant *V[] = { UndefValue::get(Result->getType()),
347 Overflow ? Builder->getTrue() : Builder->getFalse() };
348 StructType *ST = cast<StructType>(II->getType());
349 Constant *Struct = ConstantStruct::get(ST, V);
350 return InsertValueInst::Create(Struct, Result, 0);
351 }
352
Chris Lattner35522b72010-01-04 07:12:23 +0000353 // EraseInstFromFunction - When dealing with an instruction that has side
354 // effects or produces a void value, we can't rely on DCE to delete the
355 // instruction. Instead, visit methods should return the value returned by
356 // this function.
357 Instruction *EraseInstFromFunction(Instruction &I) {
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000358 DEBUG(dbgs() << "IC: ERASE " << I << '\n');
Chris Lattner35522b72010-01-04 07:12:23 +0000359
360 assert(I.use_empty() && "Cannot erase instruction that is used!");
361 // Make sure that we reprocess all operands now that we reduced their
362 // use counts.
363 if (I.getNumOperands() < 8) {
364 for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
365 if (Instruction *Op = dyn_cast<Instruction>(*i))
366 Worklist.Add(Op);
367 }
368 Worklist.Remove(&I);
369 I.eraseFromParent();
370 MadeIRChange = true;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000371 return nullptr; // Don't do anything with FI
Chris Lattner35522b72010-01-04 07:12:23 +0000372 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000373
Jay Foada0653a32014-05-14 21:14:37 +0000374 void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
Hal Finkel60db0582014-09-07 18:57:58 +0000375 unsigned Depth = 0, Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000376 return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
377 DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000378 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000379
380 bool MaskedValueIsZero(Value *V, const APInt &Mask,
Hal Finkel60db0582014-09-07 18:57:58 +0000381 unsigned Depth = 0,
382 Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000383 return llvm::MaskedValueIsZero(V, Mask, DL, Depth, AC, CxtI, DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000384 }
Hal Finkel60db0582014-09-07 18:57:58 +0000385 unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0,
386 Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000387 return llvm::ComputeNumSignBits(Op, DL, Depth, AC, CxtI, DT);
Chris Lattner35522b72010-01-04 07:12:23 +0000388 }
David Majnemer54c2ca22014-12-26 09:10:14 +0000389 void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
390 unsigned Depth = 0, Instruction *CxtI = nullptr) const {
Chandler Carruth66b31302015-01-04 12:03:27 +0000391 return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
David Majnemer54c2ca22014-12-26 09:10:14 +0000392 DT);
393 }
David Majnemer491331a2015-01-02 07:29:43 +0000394 OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
395 const Instruction *CxtI) {
Chandler Carruth66b31302015-01-04 12:03:27 +0000396 return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, AC, CxtI, DT);
David Majnemer491331a2015-01-02 07:29:43 +0000397 }
David Majnemer5310c1e2015-01-07 00:39:50 +0000398 OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
399 const Instruction *CxtI) {
400 return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, AC, CxtI, DT);
401 }
Chris Lattner35522b72010-01-04 07:12:23 +0000402
403private:
Duncan Sands641baf12010-11-13 15:10:37 +0000404 /// SimplifyAssociativeOrCommutative - This performs a few simplifications for
405 /// operators which are associative or commutative.
406 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000407
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000408 /// SimplifyUsingDistributiveLaws - This tries to simplify binary operations
409 /// which some other binary operation distributes over either by factorizing
410 /// out common terms (eg "(A*B)+(A*C)" -> "A*(B+C)") or expanding out if this
411 /// results in simplifications (eg: "A & (B | C) -> (A&B) | (A&C)" if this is
412 /// a win). Returns the simplified value, or null if it didn't simplify.
413 Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
Duncan Sandsadc7771f2010-11-23 14:23:47 +0000414
Chris Lattner35522b72010-01-04 07:12:23 +0000415 /// SimplifyDemandedUseBits - Attempts to replace V with a simpler value
416 /// based on the demanded bits.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000417 Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero,
Hal Finkel60db0582014-09-07 18:57:58 +0000418 APInt &KnownOne, unsigned Depth,
419 Instruction *CxtI = nullptr);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000420 bool SimplifyDemandedBits(Use &U, APInt DemandedMask, APInt &KnownZero,
421 APInt &KnownOne, unsigned Depth = 0);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000422 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
423 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
424 Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
425 APInt DemandedMask, APInt &KnownZero,
426 APInt &KnownOne);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000427
Chris Lattner35522b72010-01-04 07:12:23 +0000428 /// SimplifyDemandedInstructionBits - Inst is an integer instruction that
429 /// SimplifyDemandedBits knows about. See if the instruction has any
430 /// properties that allow us to simplify its operands.
431 bool SimplifyDemandedInstructionBits(Instruction &Inst);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000432
Chris Lattner35522b72010-01-04 07:12:23 +0000433 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000434 APInt &UndefElts, unsigned Depth = 0);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000435
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000436 Value *SimplifyVectorOp(BinaryOperator &Inst);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +0000437 Value *SimplifyBSwap(BinaryOperator &Inst);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000438
Chris Lattner35522b72010-01-04 07:12:23 +0000439 // FoldOpIntoPhi - Given a binary operator, cast instruction, or select
440 // which has a PHI node as operand #0, see if we can fold the instruction
441 // into the PHI (which is only possible if all operands to the PHI are
442 // constants).
443 //
Chris Lattnerea7131a2011-01-16 05:14:26 +0000444 Instruction *FoldOpIntoPhi(Instruction &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000445
446 // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
447 // operator and they all are only used by the PHI, PHI together their
448 // inputs, and do the operation once, to the result of the PHI.
449 Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
450 Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
451 Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
452 Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
453
Chris Lattner35522b72010-01-04 07:12:23 +0000454 Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
455 ConstantInt *AndRHS, BinaryOperator &TheAnd);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000456
Chris Lattner35522b72010-01-04 07:12:23 +0000457 Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
458 bool isSub, Instruction &I);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000459 Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned,
460 bool Inside);
Chris Lattner35522b72010-01-04 07:12:23 +0000461 Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
462 Instruction *MatchBSwap(BinaryOperator &I);
463 bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
464 Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
465 Instruction *SimplifyMemSet(MemSetInst *MI);
466
Chris Lattner229907c2011-07-18 04:54:35 +0000467 Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
Duncan Sands533c8ae2012-10-23 08:28:26 +0000468
469 /// Descale - Return a value X such that Val = X * Scale, or null if none. If
470 /// the multiplication is known not to overflow then NoSignedWrap is set.
471 Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
Chris Lattner35522b72010-01-04 07:12:23 +0000472};
473
Chris Lattner35522b72010-01-04 07:12:23 +0000474} // end namespace llvm.
475
Chandler Carruth964daaa2014-04-22 02:55:47 +0000476#undef DEBUG_TYPE
477
Chris Lattner35522b72010-01-04 07:12:23 +0000478#endif