blob: c56dc3c8684b16f3e8ab6f07f70a231bc5b8a6b6 [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"
Hal Finkel74c2f352014-09-07 12:44:26 +000014#include "llvm/Analysis/AssumptionTracker.h"
Chandler Carruth452a0072014-03-04 11:59:06 +000015#include "llvm/Analysis/TargetFolder.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000016#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/IRBuilder.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000018#include "llvm/IR/InstVisitor.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/IntrinsicInst.h"
20#include "llvm/IR/Operator.h"
Hal Finkel74c2f352014-09-07 12:44:26 +000021#include "llvm/IR/PatternMatch.h"
Chris Lattner35522b72010-01-04 07:12:23 +000022#include "llvm/Pass.h"
Meador Ingedf796f82012-10-13 16:45:24 +000023#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
Chris Lattner35522b72010-01-04 07:12:23 +000024
Chandler Carruth964daaa2014-04-22 02:55:47 +000025#define DEBUG_TYPE "instcombine"
26
Chris Lattner35522b72010-01-04 07:12:23 +000027namespace llvm {
Chandler Carruthd70cc602014-05-07 17:36:59 +000028class CallSite;
29class DataLayout;
30class TargetLibraryInfo;
31class DbgDeclareInst;
32class MemIntrinsic;
33class MemSetInst;
Jakub Staszak190db2f2013-01-14 23:16:36 +000034
Chris Lattner35522b72010-01-04 07:12:23 +000035/// SelectPatternFlavor - We can match a variety of different patterns for
36/// select operations.
37enum SelectPatternFlavor {
38 SPF_UNKNOWN = 0,
Chandler Carruthd70cc602014-05-07 17:36:59 +000039 SPF_SMIN,
40 SPF_UMIN,
41 SPF_SMAX,
Dinesh Dwivedi3217b6c2014-06-06 06:54:45 +000042 SPF_UMAX,
43 SPF_ABS,
44 SPF_NABS
Chris Lattner35522b72010-01-04 07:12:23 +000045};
Jakub Staszak190db2f2013-01-14 23:16:36 +000046
Chris Lattner2188e402010-01-04 07:37:31 +000047/// getComplexity: Assign a complexity or rank value to LLVM Values...
48/// 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
49static inline unsigned getComplexity(Value *V) {
50 if (isa<Instruction>(V)) {
Chandler Carruthd70cc602014-05-07 17:36:59 +000051 if (BinaryOperator::isNeg(V) || BinaryOperator::isFNeg(V) ||
Chris Lattner2188e402010-01-04 07:37:31 +000052 BinaryOperator::isNot(V))
53 return 3;
54 return 4;
55 }
Chandler Carruthd70cc602014-05-07 17:36:59 +000056 if (isa<Argument>(V))
57 return 3;
Chris Lattner2188e402010-01-04 07:37:31 +000058 return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
59}
Chris Lattner35522b72010-01-04 07:12:23 +000060
Benjamin Kramer970f4952014-01-19 16:56:10 +000061/// AddOne - Add one to a Constant
62static inline Constant *AddOne(Constant *C) {
63 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
64}
65/// SubOne - Subtract one from a Constant
66static inline Constant *SubOne(Constant *C) {
67 return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
68}
69
Chris Lattner35522b72010-01-04 07:12:23 +000070/// InstCombineIRInserter - This is an IRBuilder insertion helper that works
71/// just like the normal insertion helper, but also adds any new instructions
72/// to the instcombine worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +000073class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter
Chris Lattner35522b72010-01-04 07:12:23 +000074 : public IRBuilderDefaultInserter<true> {
75 InstCombineWorklist &Worklist;
Hal Finkel74c2f352014-09-07 12:44:26 +000076 AssumptionTracker *AT;
Chandler Carruthd70cc602014-05-07 17:36:59 +000077
Chris Lattner35522b72010-01-04 07:12:23 +000078public:
Hal Finkel74c2f352014-09-07 12:44:26 +000079 InstCombineIRInserter(InstCombineWorklist &WL, AssumptionTracker *AT)
80 : Worklist(WL), AT(AT) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +000081
Chandler Carruthd70cc602014-05-07 17:36:59 +000082 void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB,
83 BasicBlock::iterator InsertPt) const {
Chris Lattner35522b72010-01-04 07:12:23 +000084 IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
85 Worklist.Add(I);
Hal Finkel74c2f352014-09-07 12:44:26 +000086
87 using namespace llvm::PatternMatch;
88 if ((match(I, m_Intrinsic<Intrinsic::assume>(m_Value()))))
89 AT->registerAssumption(cast<CallInst>(I));
Chris Lattner35522b72010-01-04 07:12:23 +000090 }
91};
Jakub Staszak190db2f2013-01-14 23:16:36 +000092
Chris Lattner35522b72010-01-04 07:12:23 +000093/// InstCombiner - The -instcombine pass.
Duncan Sands6c5e4352010-05-11 20:16:09 +000094class LLVM_LIBRARY_VISIBILITY InstCombiner
Chandler Carruthd70cc602014-05-07 17:36:59 +000095 : public FunctionPass,
96 public InstVisitor<InstCombiner, Instruction *> {
Hal Finkel74c2f352014-09-07 12:44:26 +000097 AssumptionTracker *AT;
Rafael Espindolaaeff8a92014-02-24 23:12:18 +000098 const DataLayout *DL;
Chad Rosiere6de63d2011-12-01 21:29:16 +000099 TargetLibraryInfo *TLI;
Chris Lattner35522b72010-01-04 07:12:23 +0000100 bool MadeIRChange;
Meador Ingedf796f82012-10-13 16:45:24 +0000101 LibCallSimplifier *Simplifier;
Quentin Colombet3b2db0b2013-01-07 18:37:41 +0000102 bool MinimizeSize;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000103
Chris Lattner35522b72010-01-04 07:12:23 +0000104public:
105 /// Worklist - All of the instructions that need to be simplified.
106 InstCombineWorklist Worklist;
107
108 /// Builder - This is an IRBuilder that automatically inserts new
109 /// instructions into the worklist when they are created.
110 typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy;
111 BuilderTy *Builder;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000112
Chris Lattner35522b72010-01-04 07:12:23 +0000113 static char ID; // Pass identification, replacement for typeid
Craig Toppere73658d2014-04-28 04:05:08 +0000114 InstCombiner() : FunctionPass(ID), DL(nullptr), Builder(nullptr) {
Quentin Colombet3b2db0b2013-01-07 18:37:41 +0000115 MinimizeSize = false;
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000116 initializeInstCombinerPass(*PassRegistry::getPassRegistry());
117 }
Chris Lattner35522b72010-01-04 07:12:23 +0000118
119public:
Craig Topper3e4c6972014-03-05 09:10:37 +0000120 bool runOnFunction(Function &F) override;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000121
Chris Lattner35522b72010-01-04 07:12:23 +0000122 bool DoOneIteration(Function &F, unsigned ItNum);
123
Craig Topper3e4c6972014-03-05 09:10:37 +0000124 void getAnalysisUsage(AnalysisUsage &AU) const override;
Chad Rosiere6de63d2011-12-01 21:29:16 +0000125
Hal Finkel74c2f352014-09-07 12:44:26 +0000126 AssumptionTracker *getAssumptionTracker() const { return AT; }
127
Rafael Espindolaaeff8a92014-02-24 23:12:18 +0000128 const DataLayout *getDataLayout() const { return DL; }
Chris Lattner35522b72010-01-04 07:12:23 +0000129
Chad Rosier43a33062011-12-02 01:26:24 +0000130 TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
131
Chris Lattner35522b72010-01-04 07:12:23 +0000132 // Visitation implementation - Implement instruction combining for different
133 // instruction types. The semantics are as follows:
134 // Return Value:
135 // null - No change was made
136 // I - Change was made, I is still valid, I may be dead though
137 // otherwise - Change was made, replace I with returned instruction
138 //
139 Instruction *visitAdd(BinaryOperator &I);
140 Instruction *visitFAdd(BinaryOperator &I);
Chris Lattner229907c2011-07-18 04:54:35 +0000141 Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
Chris Lattner35522b72010-01-04 07:12:23 +0000142 Instruction *visitSub(BinaryOperator &I);
143 Instruction *visitFSub(BinaryOperator &I);
144 Instruction *visitMul(BinaryOperator &I);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000145 Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000146 Instruction *InsertBefore);
Chris Lattner35522b72010-01-04 07:12:23 +0000147 Instruction *visitFMul(BinaryOperator &I);
148 Instruction *visitURem(BinaryOperator &I);
149 Instruction *visitSRem(BinaryOperator &I);
150 Instruction *visitFRem(BinaryOperator &I);
151 bool SimplifyDivRemOfSelect(BinaryOperator &I);
152 Instruction *commonRemTransforms(BinaryOperator &I);
153 Instruction *commonIRemTransforms(BinaryOperator &I);
154 Instruction *commonDivTransforms(BinaryOperator &I);
155 Instruction *commonIDivTransforms(BinaryOperator &I);
156 Instruction *visitUDiv(BinaryOperator &I);
157 Instruction *visitSDiv(BinaryOperator &I);
Frits van Bommel2a559512011-01-29 17:50:27 +0000158 Instruction *visitFDiv(BinaryOperator &I);
Chris Lattner067459c2010-03-05 08:46:26 +0000159 Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
160 Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chris Lattner35522b72010-01-04 07:12:23 +0000161 Instruction *visitAnd(BinaryOperator &I);
Chris Lattner067459c2010-03-05 08:46:26 +0000162 Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS);
163 Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000164 Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A,
165 Value *B, Value *C);
David Majnemer5d1aeba2014-08-21 05:14:48 +0000166 Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A,
167 Value *B, Value *C);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000168 Instruction *visitOr(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000169 Instruction *visitXor(BinaryOperator &I);
170 Instruction *visitShl(BinaryOperator &I);
171 Instruction *visitAShr(BinaryOperator &I);
172 Instruction *visitLShr(BinaryOperator &I);
173 Instruction *commonShiftTransforms(BinaryOperator &I);
174 Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
175 Constant *RHSC);
176 Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
177 GlobalVariable *GV, CmpInst &ICI,
Craig Toppere73658d2014-04-28 04:05:08 +0000178 ConstantInt *AndCst = nullptr);
Chris Lattner35522b72010-01-04 07:12:23 +0000179 Instruction *visitFCmpInst(FCmpInst &I);
180 Instruction *visitICmpInst(ICmpInst &I);
181 Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000182 Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS,
Chris Lattner35522b72010-01-04 07:12:23 +0000183 ConstantInt *RHS);
184 Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
185 ConstantInt *DivRHS);
Chris Lattnerd369f572011-02-13 07:43:07 +0000186 Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI,
187 ConstantInt *DivRHS);
Suyog Sarda3a8c2c12014-07-22 19:19:36 +0000188 Instruction *FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A,
189 ConstantInt *CI1, ConstantInt *CI2);
Benjamin Kramer0e2d1622013-09-20 22:12:42 +0000190 Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI,
191 ICmpInst::Predicate Pred);
Chris Lattner35522b72010-01-04 07:12:23 +0000192 Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
193 ICmpInst::Predicate Cond, Instruction &I);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000194 Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattner35522b72010-01-04 07:12:23 +0000195 BinaryOperator &I);
196 Instruction *commonCastTransforms(CastInst &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000197 Instruction *commonPointerCastTransforms(CastInst &CI);
198 Instruction *visitTrunc(TruncInst &CI);
199 Instruction *visitZExt(ZExtInst &CI);
200 Instruction *visitSExt(SExtInst &CI);
201 Instruction *visitFPTrunc(FPTruncInst &CI);
202 Instruction *visitFPExt(CastInst &CI);
203 Instruction *visitFPToUI(FPToUIInst &FI);
204 Instruction *visitFPToSI(FPToSIInst &FI);
205 Instruction *visitUIToFP(CastInst &CI);
206 Instruction *visitSIToFP(CastInst &CI);
207 Instruction *visitPtrToInt(PtrToIntInst &CI);
208 Instruction *visitIntToPtr(IntToPtrInst &CI);
209 Instruction *visitBitCast(BitCastInst &CI);
Matt Arsenaulta9e95ab2013-11-15 05:45:08 +0000210 Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000211 Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
212 Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *);
Chris Lattner35522b72010-01-04 07:12:23 +0000213 Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
214 Value *A, Value *B, Instruction &Outer,
215 SelectPatternFlavor SPF2, Value *C);
216 Instruction *visitSelectInst(SelectInst &SI);
217 Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
218 Instruction *visitCallInst(CallInst &CI);
219 Instruction *visitInvokeInst(InvokeInst &II);
220
221 Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
222 Instruction *visitPHINode(PHINode &PN);
223 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
224 Instruction *visitAllocaInst(AllocaInst &AI);
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000225 Instruction *visitAllocSite(Instruction &FI);
Gabor Greif75f69432010-06-24 12:21:15 +0000226 Instruction *visitFree(CallInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000227 Instruction *visitLoadInst(LoadInst &LI);
228 Instruction *visitStoreInst(StoreInst &SI);
229 Instruction *visitBranchInst(BranchInst &BI);
230 Instruction *visitSwitchInst(SwitchInst &SI);
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000231 Instruction *visitInsertValueInst(InsertValueInst &IV);
Chris Lattner35522b72010-01-04 07:12:23 +0000232 Instruction *visitInsertElementInst(InsertElementInst &IE);
233 Instruction *visitExtractElementInst(ExtractElementInst &EI);
234 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
235 Instruction *visitExtractValueInst(ExtractValueInst &EV);
Duncan Sands5c055792011-09-30 13:12:16 +0000236 Instruction *visitLandingPadInst(LandingPadInst &LI);
Chris Lattner35522b72010-01-04 07:12:23 +0000237
238 // visitInstruction - Specify what to return for unhandled instructions...
Craig Toppere73658d2014-04-28 04:05:08 +0000239 Instruction *visitInstruction(Instruction &I) { return nullptr; }
Chris Lattner35522b72010-01-04 07:12:23 +0000240
241private:
Chris Lattner229907c2011-07-18 04:54:35 +0000242 bool ShouldChangeType(Type *From, Type *To) const;
Chris Lattner2188e402010-01-04 07:37:31 +0000243 Value *dyn_castNegVal(Value *V) const;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000244 Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
Matt Arsenaultd79f7d92013-08-19 22:17:40 +0000245 Type *FindElementAtOffset(Type *PtrTy, int64_t Offset,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000246 SmallVectorImpl<Value *> &NewIndices);
Chris Lattner2b295a02010-01-04 07:53:58 +0000247 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000248
Chris Lattner4e8137d2010-02-11 06:26:33 +0000249 /// ShouldOptimizeCast - Return true if the cast from "V to Ty" actually
250 /// results in any code being generated and is interesting to optimize out. If
251 /// the cast can be eliminated by some other simple transformation, we prefer
252 /// to do the simplification first.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000253 bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V,
Chris Lattner229907c2011-07-18 04:54:35 +0000254 Type *Ty);
Chris Lattner2188e402010-01-04 07:37:31 +0000255
Chris Lattner35522b72010-01-04 07:12:23 +0000256 Instruction *visitCallSite(CallSite CS);
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000257 Instruction *tryOptimizeCall(CallInst *CI, const DataLayout *DL);
Chris Lattner35522b72010-01-04 07:12:23 +0000258 bool transformConstExprCastCall(CallSite CS);
Duncan Sandsa0984362011-09-06 13:37:06 +0000259 Instruction *transformCallThroughTrampoline(CallSite CS,
260 IntrinsicInst *Tramp);
Chris Lattner35522b72010-01-04 07:12:23 +0000261 Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
262 bool DoXform = true);
Benjamin Kramer398b8c52011-04-01 20:09:03 +0000263 Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000264 bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS);
Jingyue Wu33bd53d2014-06-17 00:42:07 +0000265 bool WillNotOverflowUnsignedAdd(Value *LHS, Value *RHS);
David Majnemer57d5bc82014-08-19 23:36:30 +0000266 bool WillNotOverflowSignedSub(Value *LHS, Value *RHS);
David Majnemer42158f32014-08-20 07:17:31 +0000267 bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000268 Value *EmitGEPOffset(User *GEP);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000269 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
Nick Lewyckya2b77202013-05-31 00:59:42 +0000270 Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
Chris Lattner35522b72010-01-04 07:12:23 +0000271
272public:
273 // InsertNewInstBefore - insert an instruction New before instruction Old
274 // in the program. Add the new instruction to the worklist.
275 //
276 Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
Craig Toppere73658d2014-04-28 04:05:08 +0000277 assert(New && !New->getParent() &&
Chris Lattner35522b72010-01-04 07:12:23 +0000278 "New instruction already inserted into a basic block!");
279 BasicBlock *BB = Old.getParent();
Chandler Carruthd70cc602014-05-07 17:36:59 +0000280 BB->getInstList().insert(&Old, New); // Insert inst
Chris Lattner35522b72010-01-04 07:12:23 +0000281 Worklist.Add(New);
282 return New;
283 }
Eli Friedman6efb64e2011-05-19 01:20:42 +0000284
Jakub Staszak190db2f2013-01-14 23:16:36 +0000285 // InsertNewInstWith - same as InsertNewInstBefore, but also sets the
Eli Friedman6efb64e2011-05-19 01:20:42 +0000286 // debug loc.
287 //
288 Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
289 New->setDebugLoc(Old.getDebugLoc());
290 return InsertNewInstBefore(New, Old);
291 }
292
Chris Lattner35522b72010-01-04 07:12:23 +0000293 // ReplaceInstUsesWith - This method is to be used when an instruction is
294 // found to be dead, replacable with another preexisting expression. Here
295 // we add all uses of I to the worklist, replace all uses of I with the new
296 // value, then return I, so that the inst combiner will know that I was
297 // modified.
298 //
299 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
Chandler Carruthd70cc602014-05-07 17:36:59 +0000300 Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000301
Chris Lattner35522b72010-01-04 07:12:23 +0000302 // If we are replacing the instruction with itself, this must be in a
303 // segment of unreachable code, so just clobber the instruction.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000304 if (&I == V)
Chris Lattner35522b72010-01-04 07:12:23 +0000305 V = UndefValue::get(I.getType());
Frits van Bommeld14d9912011-03-27 23:32:31 +0000306
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000307 DEBUG(dbgs() << "IC: Replacing " << I << "\n"
Frits van Bommeld14d9912011-03-27 23:32:31 +0000308 " with " << *V << '\n');
309
Chris Lattner35522b72010-01-04 07:12:23 +0000310 I.replaceAllUsesWith(V);
311 return &I;
312 }
313
314 // EraseInstFromFunction - When dealing with an instruction that has side
315 // effects or produces a void value, we can't rely on DCE to delete the
316 // instruction. Instead, visit methods should return the value returned by
317 // this function.
318 Instruction *EraseInstFromFunction(Instruction &I) {
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000319 DEBUG(dbgs() << "IC: ERASE " << I << '\n');
Chris Lattner35522b72010-01-04 07:12:23 +0000320
321 assert(I.use_empty() && "Cannot erase instruction that is used!");
322 // Make sure that we reprocess all operands now that we reduced their
323 // use counts.
324 if (I.getNumOperands() < 8) {
325 for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
326 if (Instruction *Op = dyn_cast<Instruction>(*i))
327 Worklist.Add(Op);
328 }
329 Worklist.Remove(&I);
330 I.eraseFromParent();
331 MadeIRChange = true;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000332 return nullptr; // Don't do anything with FI
Chris Lattner35522b72010-01-04 07:12:23 +0000333 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000334
Jay Foada0653a32014-05-14 21:14:37 +0000335 void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
336 unsigned Depth = 0) const {
337 return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth);
Chris Lattner35522b72010-01-04 07:12:23 +0000338 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000339
340 bool MaskedValueIsZero(Value *V, const APInt &Mask,
Chris Lattner35522b72010-01-04 07:12:23 +0000341 unsigned Depth = 0) const {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000342 return llvm::MaskedValueIsZero(V, Mask, DL, Depth);
Chris Lattner35522b72010-01-04 07:12:23 +0000343 }
344 unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000345 return llvm::ComputeNumSignBits(Op, DL, Depth);
Chris Lattner35522b72010-01-04 07:12:23 +0000346 }
347
348private:
Duncan Sands641baf12010-11-13 15:10:37 +0000349 /// SimplifyAssociativeOrCommutative - This performs a few simplifications for
350 /// operators which are associative or commutative.
351 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000352
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000353 /// SimplifyUsingDistributiveLaws - This tries to simplify binary operations
354 /// which some other binary operation distributes over either by factorizing
355 /// out common terms (eg "(A*B)+(A*C)" -> "A*(B+C)") or expanding out if this
356 /// results in simplifications (eg: "A & (B | C) -> (A&B) | (A&C)" if this is
357 /// a win). Returns the simplified value, or null if it didn't simplify.
358 Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
Duncan Sandsadc7771f2010-11-23 14:23:47 +0000359
Chris Lattner35522b72010-01-04 07:12:23 +0000360 /// SimplifyDemandedUseBits - Attempts to replace V with a simpler value
361 /// based on the demanded bits.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000362 Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero,
363 APInt &KnownOne, unsigned Depth);
364 bool SimplifyDemandedBits(Use &U, APInt DemandedMask, APInt &KnownZero,
365 APInt &KnownOne, unsigned Depth = 0);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000366 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
367 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
368 Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
369 APInt DemandedMask, APInt &KnownZero,
370 APInt &KnownOne);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000371
Chris Lattner35522b72010-01-04 07:12:23 +0000372 /// SimplifyDemandedInstructionBits - Inst is an integer instruction that
373 /// SimplifyDemandedBits knows about. See if the instruction has any
374 /// properties that allow us to simplify its operands.
375 bool SimplifyDemandedInstructionBits(Instruction &Inst);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000376
Chris Lattner35522b72010-01-04 07:12:23 +0000377 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000378 APInt &UndefElts, unsigned Depth = 0);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000379
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000380 Value *SimplifyVectorOp(BinaryOperator &Inst);
381
Chris Lattner35522b72010-01-04 07:12:23 +0000382 // FoldOpIntoPhi - Given a binary operator, cast instruction, or select
383 // which has a PHI node as operand #0, see if we can fold the instruction
384 // into the PHI (which is only possible if all operands to the PHI are
385 // constants).
386 //
Chris Lattnerea7131a2011-01-16 05:14:26 +0000387 Instruction *FoldOpIntoPhi(Instruction &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000388
389 // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
390 // operator and they all are only used by the PHI, PHI together their
391 // inputs, and do the operation once, to the result of the PHI.
392 Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
393 Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
394 Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
395 Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
396
Chris Lattner35522b72010-01-04 07:12:23 +0000397 Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
398 ConstantInt *AndRHS, BinaryOperator &TheAnd);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000399
Chris Lattner35522b72010-01-04 07:12:23 +0000400 Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
401 bool isSub, Instruction &I);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000402 Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned,
403 bool Inside);
Chris Lattner35522b72010-01-04 07:12:23 +0000404 Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
405 Instruction *MatchBSwap(BinaryOperator &I);
406 bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
407 Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
408 Instruction *SimplifyMemSet(MemSetInst *MI);
409
Chris Lattner229907c2011-07-18 04:54:35 +0000410 Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
Duncan Sands533c8ae2012-10-23 08:28:26 +0000411
412 /// Descale - Return a value X such that Val = X * Scale, or null if none. If
413 /// the multiplication is known not to overflow then NoSignedWrap is set.
414 Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
Chris Lattner35522b72010-01-04 07:12:23 +0000415};
416
Chris Lattner35522b72010-01-04 07:12:23 +0000417} // end namespace llvm.
418
Chandler Carruth964daaa2014-04-22 02:55:47 +0000419#undef DEBUG_TYPE
420
Chris Lattner35522b72010-01-04 07:12:23 +0000421#endif