blob: c8ed7c2b5a68f0e6d9c79dee12baafb5fa5f337a [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 Carruth452a0072014-03-04 11:59:06 +000014#include "llvm/Analysis/TargetFolder.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000015#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/IRBuilder.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000017#include "llvm/IR/InstVisitor.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/IntrinsicInst.h"
19#include "llvm/IR/Operator.h"
Chris Lattner35522b72010-01-04 07:12:23 +000020#include "llvm/Pass.h"
Meador Ingedf796f82012-10-13 16:45:24 +000021#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
Chris Lattner35522b72010-01-04 07:12:23 +000022
Chandler Carruth964daaa2014-04-22 02:55:47 +000023#define DEBUG_TYPE "instcombine"
24
Chris Lattner35522b72010-01-04 07:12:23 +000025namespace llvm {
Chandler Carruthd70cc602014-05-07 17:36:59 +000026class CallSite;
27class DataLayout;
28class TargetLibraryInfo;
29class DbgDeclareInst;
30class MemIntrinsic;
31class MemSetInst;
Jakub Staszak190db2f2013-01-14 23:16:36 +000032
Chris Lattner35522b72010-01-04 07:12:23 +000033/// SelectPatternFlavor - We can match a variety of different patterns for
34/// select operations.
35enum SelectPatternFlavor {
36 SPF_UNKNOWN = 0,
Chandler Carruthd70cc602014-05-07 17:36:59 +000037 SPF_SMIN,
38 SPF_UMIN,
39 SPF_SMAX,
Dinesh Dwivedi3217b6c2014-06-06 06:54:45 +000040 SPF_UMAX,
41 SPF_ABS,
42 SPF_NABS
Chris Lattner35522b72010-01-04 07:12:23 +000043};
Jakub Staszak190db2f2013-01-14 23:16:36 +000044
Chris Lattner2188e402010-01-04 07:37:31 +000045/// getComplexity: Assign a complexity or rank value to LLVM Values...
46/// 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
47static inline unsigned getComplexity(Value *V) {
48 if (isa<Instruction>(V)) {
Chandler Carruthd70cc602014-05-07 17:36:59 +000049 if (BinaryOperator::isNeg(V) || BinaryOperator::isFNeg(V) ||
Chris Lattner2188e402010-01-04 07:37:31 +000050 BinaryOperator::isNot(V))
51 return 3;
52 return 4;
53 }
Chandler Carruthd70cc602014-05-07 17:36:59 +000054 if (isa<Argument>(V))
55 return 3;
Chris Lattner2188e402010-01-04 07:37:31 +000056 return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
57}
Chris Lattner35522b72010-01-04 07:12:23 +000058
Benjamin Kramer970f4952014-01-19 16:56:10 +000059/// AddOne - Add one to a Constant
60static inline Constant *AddOne(Constant *C) {
61 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
62}
63/// SubOne - Subtract one from a Constant
64static inline Constant *SubOne(Constant *C) {
65 return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
66}
67
Chris Lattner35522b72010-01-04 07:12:23 +000068/// InstCombineIRInserter - This is an IRBuilder insertion helper that works
69/// just like the normal insertion helper, but also adds any new instructions
70/// to the instcombine worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +000071class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter
Chris Lattner35522b72010-01-04 07:12:23 +000072 : public IRBuilderDefaultInserter<true> {
73 InstCombineWorklist &Worklist;
Chandler Carruthd70cc602014-05-07 17:36:59 +000074
Chris Lattner35522b72010-01-04 07:12:23 +000075public:
76 InstCombineIRInserter(InstCombineWorklist &WL) : Worklist(WL) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +000077
Chandler Carruthd70cc602014-05-07 17:36:59 +000078 void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB,
79 BasicBlock::iterator InsertPt) const {
Chris Lattner35522b72010-01-04 07:12:23 +000080 IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
81 Worklist.Add(I);
82 }
83};
Jakub Staszak190db2f2013-01-14 23:16:36 +000084
Chris Lattner35522b72010-01-04 07:12:23 +000085/// InstCombiner - The -instcombine pass.
Duncan Sands6c5e4352010-05-11 20:16:09 +000086class LLVM_LIBRARY_VISIBILITY InstCombiner
Chandler Carruthd70cc602014-05-07 17:36:59 +000087 : public FunctionPass,
88 public InstVisitor<InstCombiner, Instruction *> {
Rafael Espindolaaeff8a92014-02-24 23:12:18 +000089 const DataLayout *DL;
Chad Rosiere6de63d2011-12-01 21:29:16 +000090 TargetLibraryInfo *TLI;
Chris Lattner35522b72010-01-04 07:12:23 +000091 bool MadeIRChange;
Meador Ingedf796f82012-10-13 16:45:24 +000092 LibCallSimplifier *Simplifier;
Quentin Colombet3b2db0b2013-01-07 18:37:41 +000093 bool MinimizeSize;
Chandler Carruthd70cc602014-05-07 17:36:59 +000094
Chris Lattner35522b72010-01-04 07:12:23 +000095public:
96 /// Worklist - All of the instructions that need to be simplified.
97 InstCombineWorklist Worklist;
98
99 /// Builder - This is an IRBuilder that automatically inserts new
100 /// instructions into the worklist when they are created.
101 typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy;
102 BuilderTy *Builder;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000103
Chris Lattner35522b72010-01-04 07:12:23 +0000104 static char ID; // Pass identification, replacement for typeid
Craig Toppere73658d2014-04-28 04:05:08 +0000105 InstCombiner() : FunctionPass(ID), DL(nullptr), Builder(nullptr) {
Quentin Colombet3b2db0b2013-01-07 18:37:41 +0000106 MinimizeSize = false;
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000107 initializeInstCombinerPass(*PassRegistry::getPassRegistry());
108 }
Chris Lattner35522b72010-01-04 07:12:23 +0000109
110public:
Craig Topper3e4c6972014-03-05 09:10:37 +0000111 bool runOnFunction(Function &F) override;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000112
Chris Lattner35522b72010-01-04 07:12:23 +0000113 bool DoOneIteration(Function &F, unsigned ItNum);
114
Craig Topper3e4c6972014-03-05 09:10:37 +0000115 void getAnalysisUsage(AnalysisUsage &AU) const override;
Chad Rosiere6de63d2011-12-01 21:29:16 +0000116
Rafael Espindolaaeff8a92014-02-24 23:12:18 +0000117 const DataLayout *getDataLayout() const { return DL; }
Chris Lattner35522b72010-01-04 07:12:23 +0000118
Chad Rosier43a33062011-12-02 01:26:24 +0000119 TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
120
Chris Lattner35522b72010-01-04 07:12:23 +0000121 // Visitation implementation - Implement instruction combining for different
122 // instruction types. The semantics are as follows:
123 // Return Value:
124 // null - No change was made
125 // I - Change was made, I is still valid, I may be dead though
126 // otherwise - Change was made, replace I with returned instruction
127 //
128 Instruction *visitAdd(BinaryOperator &I);
129 Instruction *visitFAdd(BinaryOperator &I);
Chris Lattner229907c2011-07-18 04:54:35 +0000130 Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
Chris Lattner35522b72010-01-04 07:12:23 +0000131 Instruction *visitSub(BinaryOperator &I);
132 Instruction *visitFSub(BinaryOperator &I);
133 Instruction *visitMul(BinaryOperator &I);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000134 Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000135 Instruction *InsertBefore);
Chris Lattner35522b72010-01-04 07:12:23 +0000136 Instruction *visitFMul(BinaryOperator &I);
137 Instruction *visitURem(BinaryOperator &I);
138 Instruction *visitSRem(BinaryOperator &I);
139 Instruction *visitFRem(BinaryOperator &I);
140 bool SimplifyDivRemOfSelect(BinaryOperator &I);
141 Instruction *commonRemTransforms(BinaryOperator &I);
142 Instruction *commonIRemTransforms(BinaryOperator &I);
143 Instruction *commonDivTransforms(BinaryOperator &I);
144 Instruction *commonIDivTransforms(BinaryOperator &I);
145 Instruction *visitUDiv(BinaryOperator &I);
146 Instruction *visitSDiv(BinaryOperator &I);
Frits van Bommel2a559512011-01-29 17:50:27 +0000147 Instruction *visitFDiv(BinaryOperator &I);
Chris Lattner067459c2010-03-05 08:46:26 +0000148 Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
149 Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chris Lattner35522b72010-01-04 07:12:23 +0000150 Instruction *visitAnd(BinaryOperator &I);
Chris Lattner067459c2010-03-05 08:46:26 +0000151 Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS);
152 Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000153 Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A,
154 Value *B, Value *C);
David Majnemer5d1aeba2014-08-21 05:14:48 +0000155 Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A,
156 Value *B, Value *C);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000157 Instruction *visitOr(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000158 Instruction *visitXor(BinaryOperator &I);
159 Instruction *visitShl(BinaryOperator &I);
160 Instruction *visitAShr(BinaryOperator &I);
161 Instruction *visitLShr(BinaryOperator &I);
162 Instruction *commonShiftTransforms(BinaryOperator &I);
163 Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
164 Constant *RHSC);
165 Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
166 GlobalVariable *GV, CmpInst &ICI,
Craig Toppere73658d2014-04-28 04:05:08 +0000167 ConstantInt *AndCst = nullptr);
Chris Lattner35522b72010-01-04 07:12:23 +0000168 Instruction *visitFCmpInst(FCmpInst &I);
169 Instruction *visitICmpInst(ICmpInst &I);
170 Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000171 Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS,
Chris Lattner35522b72010-01-04 07:12:23 +0000172 ConstantInt *RHS);
173 Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
174 ConstantInt *DivRHS);
Chris Lattnerd369f572011-02-13 07:43:07 +0000175 Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI,
176 ConstantInt *DivRHS);
Suyog Sarda3a8c2c12014-07-22 19:19:36 +0000177 Instruction *FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A,
178 ConstantInt *CI1, ConstantInt *CI2);
Benjamin Kramer0e2d1622013-09-20 22:12:42 +0000179 Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI,
180 ICmpInst::Predicate Pred);
Chris Lattner35522b72010-01-04 07:12:23 +0000181 Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
182 ICmpInst::Predicate Cond, Instruction &I);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000183 Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattner35522b72010-01-04 07:12:23 +0000184 BinaryOperator &I);
185 Instruction *commonCastTransforms(CastInst &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000186 Instruction *commonPointerCastTransforms(CastInst &CI);
187 Instruction *visitTrunc(TruncInst &CI);
188 Instruction *visitZExt(ZExtInst &CI);
189 Instruction *visitSExt(SExtInst &CI);
190 Instruction *visitFPTrunc(FPTruncInst &CI);
191 Instruction *visitFPExt(CastInst &CI);
192 Instruction *visitFPToUI(FPToUIInst &FI);
193 Instruction *visitFPToSI(FPToSIInst &FI);
194 Instruction *visitUIToFP(CastInst &CI);
195 Instruction *visitSIToFP(CastInst &CI);
196 Instruction *visitPtrToInt(PtrToIntInst &CI);
197 Instruction *visitIntToPtr(IntToPtrInst &CI);
198 Instruction *visitBitCast(BitCastInst &CI);
Matt Arsenaulta9e95ab2013-11-15 05:45:08 +0000199 Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000200 Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
201 Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *);
Chris Lattner35522b72010-01-04 07:12:23 +0000202 Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
203 Value *A, Value *B, Instruction &Outer,
204 SelectPatternFlavor SPF2, Value *C);
205 Instruction *visitSelectInst(SelectInst &SI);
206 Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
207 Instruction *visitCallInst(CallInst &CI);
208 Instruction *visitInvokeInst(InvokeInst &II);
209
210 Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
211 Instruction *visitPHINode(PHINode &PN);
212 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
213 Instruction *visitAllocaInst(AllocaInst &AI);
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000214 Instruction *visitAllocSite(Instruction &FI);
Gabor Greif75f69432010-06-24 12:21:15 +0000215 Instruction *visitFree(CallInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000216 Instruction *visitLoadInst(LoadInst &LI);
217 Instruction *visitStoreInst(StoreInst &SI);
218 Instruction *visitBranchInst(BranchInst &BI);
219 Instruction *visitSwitchInst(SwitchInst &SI);
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000220 Instruction *visitInsertValueInst(InsertValueInst &IV);
Chris Lattner35522b72010-01-04 07:12:23 +0000221 Instruction *visitInsertElementInst(InsertElementInst &IE);
222 Instruction *visitExtractElementInst(ExtractElementInst &EI);
223 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
224 Instruction *visitExtractValueInst(ExtractValueInst &EV);
Duncan Sands5c055792011-09-30 13:12:16 +0000225 Instruction *visitLandingPadInst(LandingPadInst &LI);
Chris Lattner35522b72010-01-04 07:12:23 +0000226
227 // visitInstruction - Specify what to return for unhandled instructions...
Craig Toppere73658d2014-04-28 04:05:08 +0000228 Instruction *visitInstruction(Instruction &I) { return nullptr; }
Chris Lattner35522b72010-01-04 07:12:23 +0000229
230private:
Chris Lattner229907c2011-07-18 04:54:35 +0000231 bool ShouldChangeType(Type *From, Type *To) const;
Chris Lattner2188e402010-01-04 07:37:31 +0000232 Value *dyn_castNegVal(Value *V) const;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000233 Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
Matt Arsenaultd79f7d92013-08-19 22:17:40 +0000234 Type *FindElementAtOffset(Type *PtrTy, int64_t Offset,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000235 SmallVectorImpl<Value *> &NewIndices);
Chris Lattner2b295a02010-01-04 07:53:58 +0000236 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000237
Chris Lattner4e8137d2010-02-11 06:26:33 +0000238 /// ShouldOptimizeCast - Return true if the cast from "V to Ty" actually
239 /// results in any code being generated and is interesting to optimize out. If
240 /// the cast can be eliminated by some other simple transformation, we prefer
241 /// to do the simplification first.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000242 bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V,
Chris Lattner229907c2011-07-18 04:54:35 +0000243 Type *Ty);
Chris Lattner2188e402010-01-04 07:37:31 +0000244
Chris Lattner35522b72010-01-04 07:12:23 +0000245 Instruction *visitCallSite(CallSite CS);
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000246 Instruction *tryOptimizeCall(CallInst *CI, const DataLayout *DL);
Chris Lattner35522b72010-01-04 07:12:23 +0000247 bool transformConstExprCastCall(CallSite CS);
Duncan Sandsa0984362011-09-06 13:37:06 +0000248 Instruction *transformCallThroughTrampoline(CallSite CS,
249 IntrinsicInst *Tramp);
Chris Lattner35522b72010-01-04 07:12:23 +0000250 Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
251 bool DoXform = true);
Benjamin Kramer398b8c52011-04-01 20:09:03 +0000252 Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000253 bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS);
Jingyue Wu33bd53d2014-06-17 00:42:07 +0000254 bool WillNotOverflowUnsignedAdd(Value *LHS, Value *RHS);
David Majnemer57d5bc82014-08-19 23:36:30 +0000255 bool WillNotOverflowSignedSub(Value *LHS, Value *RHS);
David Majnemer42158f32014-08-20 07:17:31 +0000256 bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000257 Value *EmitGEPOffset(User *GEP);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000258 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
Nick Lewyckya2b77202013-05-31 00:59:42 +0000259 Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
Chris Lattner35522b72010-01-04 07:12:23 +0000260
261public:
262 // InsertNewInstBefore - insert an instruction New before instruction Old
263 // in the program. Add the new instruction to the worklist.
264 //
265 Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
Craig Toppere73658d2014-04-28 04:05:08 +0000266 assert(New && !New->getParent() &&
Chris Lattner35522b72010-01-04 07:12:23 +0000267 "New instruction already inserted into a basic block!");
268 BasicBlock *BB = Old.getParent();
Chandler Carruthd70cc602014-05-07 17:36:59 +0000269 BB->getInstList().insert(&Old, New); // Insert inst
Chris Lattner35522b72010-01-04 07:12:23 +0000270 Worklist.Add(New);
271 return New;
272 }
Eli Friedman6efb64e2011-05-19 01:20:42 +0000273
Jakub Staszak190db2f2013-01-14 23:16:36 +0000274 // InsertNewInstWith - same as InsertNewInstBefore, but also sets the
Eli Friedman6efb64e2011-05-19 01:20:42 +0000275 // debug loc.
276 //
277 Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
278 New->setDebugLoc(Old.getDebugLoc());
279 return InsertNewInstBefore(New, Old);
280 }
281
Chris Lattner35522b72010-01-04 07:12:23 +0000282 // ReplaceInstUsesWith - This method is to be used when an instruction is
283 // found to be dead, replacable with another preexisting expression. Here
284 // we add all uses of I to the worklist, replace all uses of I with the new
285 // value, then return I, so that the inst combiner will know that I was
286 // modified.
287 //
288 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
Chandler Carruthd70cc602014-05-07 17:36:59 +0000289 Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000290
Chris Lattner35522b72010-01-04 07:12:23 +0000291 // If we are replacing the instruction with itself, this must be in a
292 // segment of unreachable code, so just clobber the instruction.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000293 if (&I == V)
Chris Lattner35522b72010-01-04 07:12:23 +0000294 V = UndefValue::get(I.getType());
Frits van Bommeld14d9912011-03-27 23:32:31 +0000295
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000296 DEBUG(dbgs() << "IC: Replacing " << I << "\n"
Frits van Bommeld14d9912011-03-27 23:32:31 +0000297 " with " << *V << '\n');
298
Chris Lattner35522b72010-01-04 07:12:23 +0000299 I.replaceAllUsesWith(V);
300 return &I;
301 }
302
303 // EraseInstFromFunction - When dealing with an instruction that has side
304 // effects or produces a void value, we can't rely on DCE to delete the
305 // instruction. Instead, visit methods should return the value returned by
306 // this function.
307 Instruction *EraseInstFromFunction(Instruction &I) {
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000308 DEBUG(dbgs() << "IC: ERASE " << I << '\n');
Chris Lattner35522b72010-01-04 07:12:23 +0000309
310 assert(I.use_empty() && "Cannot erase instruction that is used!");
311 // Make sure that we reprocess all operands now that we reduced their
312 // use counts.
313 if (I.getNumOperands() < 8) {
314 for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
315 if (Instruction *Op = dyn_cast<Instruction>(*i))
316 Worklist.Add(Op);
317 }
318 Worklist.Remove(&I);
319 I.eraseFromParent();
320 MadeIRChange = true;
Chandler Carruthd70cc602014-05-07 17:36:59 +0000321 return nullptr; // Don't do anything with FI
Chris Lattner35522b72010-01-04 07:12:23 +0000322 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000323
Jay Foada0653a32014-05-14 21:14:37 +0000324 void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
325 unsigned Depth = 0) const {
326 return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth);
Chris Lattner35522b72010-01-04 07:12:23 +0000327 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000328
329 bool MaskedValueIsZero(Value *V, const APInt &Mask,
Chris Lattner35522b72010-01-04 07:12:23 +0000330 unsigned Depth = 0) const {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000331 return llvm::MaskedValueIsZero(V, Mask, DL, Depth);
Chris Lattner35522b72010-01-04 07:12:23 +0000332 }
333 unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000334 return llvm::ComputeNumSignBits(Op, DL, Depth);
Chris Lattner35522b72010-01-04 07:12:23 +0000335 }
336
337private:
Duncan Sands641baf12010-11-13 15:10:37 +0000338 /// SimplifyAssociativeOrCommutative - This performs a few simplifications for
339 /// operators which are associative or commutative.
340 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000341
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000342 /// SimplifyUsingDistributiveLaws - This tries to simplify binary operations
343 /// which some other binary operation distributes over either by factorizing
344 /// out common terms (eg "(A*B)+(A*C)" -> "A*(B+C)") or expanding out if this
345 /// results in simplifications (eg: "A & (B | C) -> (A&B) | (A&C)" if this is
346 /// a win). Returns the simplified value, or null if it didn't simplify.
347 Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
Duncan Sandsadc7771f2010-11-23 14:23:47 +0000348
Chris Lattner35522b72010-01-04 07:12:23 +0000349 /// SimplifyDemandedUseBits - Attempts to replace V with a simpler value
350 /// based on the demanded bits.
Chandler Carruthd70cc602014-05-07 17:36:59 +0000351 Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero,
352 APInt &KnownOne, unsigned Depth);
353 bool SimplifyDemandedBits(Use &U, APInt DemandedMask, APInt &KnownZero,
354 APInt &KnownOne, unsigned Depth = 0);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000355 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
356 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
357 Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
358 APInt DemandedMask, APInt &KnownZero,
359 APInt &KnownOne);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000360
Chris Lattner35522b72010-01-04 07:12:23 +0000361 /// SimplifyDemandedInstructionBits - Inst is an integer instruction that
362 /// SimplifyDemandedBits knows about. See if the instruction has any
363 /// properties that allow us to simplify its operands.
364 bool SimplifyDemandedInstructionBits(Instruction &Inst);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000365
Chris Lattner35522b72010-01-04 07:12:23 +0000366 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chandler Carruthd70cc602014-05-07 17:36:59 +0000367 APInt &UndefElts, unsigned Depth = 0);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000368
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000369 Value *SimplifyVectorOp(BinaryOperator &Inst);
370
Chris Lattner35522b72010-01-04 07:12:23 +0000371 // FoldOpIntoPhi - Given a binary operator, cast instruction, or select
372 // which has a PHI node as operand #0, see if we can fold the instruction
373 // into the PHI (which is only possible if all operands to the PHI are
374 // constants).
375 //
Chris Lattnerea7131a2011-01-16 05:14:26 +0000376 Instruction *FoldOpIntoPhi(Instruction &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000377
378 // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
379 // operator and they all are only used by the PHI, PHI together their
380 // inputs, and do the operation once, to the result of the PHI.
381 Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
382 Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
383 Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
384 Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
385
Chris Lattner35522b72010-01-04 07:12:23 +0000386 Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
387 ConstantInt *AndRHS, BinaryOperator &TheAnd);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000388
Chris Lattner35522b72010-01-04 07:12:23 +0000389 Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
390 bool isSub, Instruction &I);
Chandler Carruthd70cc602014-05-07 17:36:59 +0000391 Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned,
392 bool Inside);
Chris Lattner35522b72010-01-04 07:12:23 +0000393 Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
394 Instruction *MatchBSwap(BinaryOperator &I);
395 bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
396 Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
397 Instruction *SimplifyMemSet(MemSetInst *MI);
398
Chris Lattner229907c2011-07-18 04:54:35 +0000399 Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
Duncan Sands533c8ae2012-10-23 08:28:26 +0000400
401 /// Descale - Return a value X such that Val = X * Scale, or null if none. If
402 /// the multiplication is known not to overflow then NoSignedWrap is set.
403 Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
Chris Lattner35522b72010-01-04 07:12:23 +0000404};
405
Chris Lattner35522b72010-01-04 07:12:23 +0000406} // end namespace llvm.
407
Chandler Carruth964daaa2014-04-22 02:55:47 +0000408#undef DEBUG_TYPE
409
Chris Lattner35522b72010-01-04 07:12:23 +0000410#endif