blob: 8a082fea04ae036b958ac4266a4e1f029973757b [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
10#ifndef INSTCOMBINE_INSTCOMBINE_H
11#define INSTCOMBINE_INSTCOMBINE_H
12
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 {
26 class CallSite;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000027 class DataLayout;
Chad Rosiere6de63d2011-12-01 21:29:16 +000028 class TargetLibraryInfo;
Chris Lattner27acfcd2010-01-05 05:21:26 +000029 class DbgDeclareInst;
30 class MemIntrinsic;
31 class 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,
37 SPF_SMIN, SPF_UMIN,
38 SPF_SMAX, SPF_UMAX
39 //SPF_ABS - TODO.
40};
Jakub Staszak190db2f2013-01-14 23:16:36 +000041
Chris Lattner2188e402010-01-04 07:37:31 +000042/// getComplexity: Assign a complexity or rank value to LLVM Values...
43/// 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
44static inline unsigned getComplexity(Value *V) {
45 if (isa<Instruction>(V)) {
46 if (BinaryOperator::isNeg(V) ||
47 BinaryOperator::isFNeg(V) ||
48 BinaryOperator::isNot(V))
49 return 3;
50 return 4;
51 }
52 if (isa<Argument>(V)) return 3;
53 return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
54}
Chris Lattner35522b72010-01-04 07:12:23 +000055
Benjamin Kramer970f4952014-01-19 16:56:10 +000056/// AddOne - Add one to a Constant
57static inline Constant *AddOne(Constant *C) {
58 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
59}
60/// SubOne - Subtract one from a Constant
61static inline Constant *SubOne(Constant *C) {
62 return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
63}
64
Jakub Staszak190db2f2013-01-14 23:16:36 +000065
Chris Lattner35522b72010-01-04 07:12:23 +000066/// InstCombineIRInserter - This is an IRBuilder insertion helper that works
67/// just like the normal insertion helper, but also adds any new instructions
68/// to the instcombine worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +000069class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter
Chris Lattner35522b72010-01-04 07:12:23 +000070 : public IRBuilderDefaultInserter<true> {
71 InstCombineWorklist &Worklist;
72public:
73 InstCombineIRInserter(InstCombineWorklist &WL) : Worklist(WL) {}
Jakub Staszak190db2f2013-01-14 23:16:36 +000074
Chris Lattner35522b72010-01-04 07:12:23 +000075 void InsertHelper(Instruction *I, const Twine &Name,
76 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
77 IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
78 Worklist.Add(I);
79 }
80};
Jakub Staszak190db2f2013-01-14 23:16:36 +000081
Chris Lattner35522b72010-01-04 07:12:23 +000082/// InstCombiner - The -instcombine pass.
Duncan Sands6c5e4352010-05-11 20:16:09 +000083class LLVM_LIBRARY_VISIBILITY InstCombiner
Chris Lattner35522b72010-01-04 07:12:23 +000084 : public FunctionPass,
85 public InstVisitor<InstCombiner, Instruction*> {
Rafael Espindolaaeff8a92014-02-24 23:12:18 +000086 const DataLayout *DL;
Chad Rosiere6de63d2011-12-01 21:29:16 +000087 TargetLibraryInfo *TLI;
Chris Lattner35522b72010-01-04 07:12:23 +000088 bool MadeIRChange;
Meador Ingedf796f82012-10-13 16:45:24 +000089 LibCallSimplifier *Simplifier;
Quentin Colombet3b2db0b2013-01-07 18:37:41 +000090 bool MinimizeSize;
Chris Lattner35522b72010-01-04 07:12:23 +000091public:
92 /// Worklist - All of the instructions that need to be simplified.
93 InstCombineWorklist Worklist;
94
95 /// Builder - This is an IRBuilder that automatically inserts new
96 /// instructions into the worklist when they are created.
97 typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy;
98 BuilderTy *Builder;
Jakub Staszak190db2f2013-01-14 23:16:36 +000099
Chris Lattner35522b72010-01-04 07:12:23 +0000100 static char ID; // Pass identification, replacement for typeid
Craig Toppere73658d2014-04-28 04:05:08 +0000101 InstCombiner() : FunctionPass(ID), DL(nullptr), Builder(nullptr) {
Quentin Colombet3b2db0b2013-01-07 18:37:41 +0000102 MinimizeSize = false;
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000103 initializeInstCombinerPass(*PassRegistry::getPassRegistry());
104 }
Chris Lattner35522b72010-01-04 07:12:23 +0000105
106public:
Craig Topper3e4c6972014-03-05 09:10:37 +0000107 bool runOnFunction(Function &F) override;
Jakub Staszak190db2f2013-01-14 23:16:36 +0000108
Chris Lattner35522b72010-01-04 07:12:23 +0000109 bool DoOneIteration(Function &F, unsigned ItNum);
110
Craig Topper3e4c6972014-03-05 09:10:37 +0000111 void getAnalysisUsage(AnalysisUsage &AU) const override;
Chad Rosiere6de63d2011-12-01 21:29:16 +0000112
Rafael Espindolaaeff8a92014-02-24 23:12:18 +0000113 const DataLayout *getDataLayout() const { return DL; }
Chris Lattner35522b72010-01-04 07:12:23 +0000114
Chad Rosier43a33062011-12-02 01:26:24 +0000115 TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
116
Chris Lattner35522b72010-01-04 07:12:23 +0000117 // Visitation implementation - Implement instruction combining for different
118 // instruction types. The semantics are as follows:
119 // Return Value:
120 // null - No change was made
121 // I - Change was made, I is still valid, I may be dead though
122 // otherwise - Change was made, replace I with returned instruction
123 //
124 Instruction *visitAdd(BinaryOperator &I);
125 Instruction *visitFAdd(BinaryOperator &I);
Chris Lattner229907c2011-07-18 04:54:35 +0000126 Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
Chris Lattner35522b72010-01-04 07:12:23 +0000127 Instruction *visitSub(BinaryOperator &I);
128 Instruction *visitFSub(BinaryOperator &I);
129 Instruction *visitMul(BinaryOperator &I);
Benjamin Kramer76b15d02014-01-19 13:36:27 +0000130 Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
Shuxin Yangdf0e61e2013-01-07 21:39:23 +0000131 Instruction *InsertBefore);
Chris Lattner35522b72010-01-04 07:12:23 +0000132 Instruction *visitFMul(BinaryOperator &I);
133 Instruction *visitURem(BinaryOperator &I);
134 Instruction *visitSRem(BinaryOperator &I);
135 Instruction *visitFRem(BinaryOperator &I);
136 bool SimplifyDivRemOfSelect(BinaryOperator &I);
137 Instruction *commonRemTransforms(BinaryOperator &I);
138 Instruction *commonIRemTransforms(BinaryOperator &I);
139 Instruction *commonDivTransforms(BinaryOperator &I);
140 Instruction *commonIDivTransforms(BinaryOperator &I);
141 Instruction *visitUDiv(BinaryOperator &I);
142 Instruction *visitSDiv(BinaryOperator &I);
Frits van Bommel2a559512011-01-29 17:50:27 +0000143 Instruction *visitFDiv(BinaryOperator &I);
Chris Lattner067459c2010-03-05 08:46:26 +0000144 Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
145 Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chris Lattner35522b72010-01-04 07:12:23 +0000146 Instruction *visitAnd(BinaryOperator &I);
Chris Lattner067459c2010-03-05 08:46:26 +0000147 Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS);
148 Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
Chris Lattner35522b72010-01-04 07:12:23 +0000149 Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op,
150 Value *A, Value *B, Value *C);
151 Instruction *visitOr (BinaryOperator &I);
152 Instruction *visitXor(BinaryOperator &I);
153 Instruction *visitShl(BinaryOperator &I);
154 Instruction *visitAShr(BinaryOperator &I);
155 Instruction *visitLShr(BinaryOperator &I);
156 Instruction *commonShiftTransforms(BinaryOperator &I);
157 Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
158 Constant *RHSC);
159 Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
160 GlobalVariable *GV, CmpInst &ICI,
Craig Toppere73658d2014-04-28 04:05:08 +0000161 ConstantInt *AndCst = nullptr);
Chris Lattner35522b72010-01-04 07:12:23 +0000162 Instruction *visitFCmpInst(FCmpInst &I);
163 Instruction *visitICmpInst(ICmpInst &I);
164 Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
165 Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
166 Instruction *LHS,
167 ConstantInt *RHS);
168 Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
169 ConstantInt *DivRHS);
Chris Lattnerd369f572011-02-13 07:43:07 +0000170 Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI,
171 ConstantInt *DivRHS);
Benjamin Kramer0e2d1622013-09-20 22:12:42 +0000172 Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI,
173 ICmpInst::Predicate Pred);
Chris Lattner35522b72010-01-04 07:12:23 +0000174 Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
175 ICmpInst::Predicate Cond, Instruction &I);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000176 Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattner35522b72010-01-04 07:12:23 +0000177 BinaryOperator &I);
178 Instruction *commonCastTransforms(CastInst &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000179 Instruction *commonPointerCastTransforms(CastInst &CI);
180 Instruction *visitTrunc(TruncInst &CI);
181 Instruction *visitZExt(ZExtInst &CI);
182 Instruction *visitSExt(SExtInst &CI);
183 Instruction *visitFPTrunc(FPTruncInst &CI);
184 Instruction *visitFPExt(CastInst &CI);
185 Instruction *visitFPToUI(FPToUIInst &FI);
186 Instruction *visitFPToSI(FPToSIInst &FI);
187 Instruction *visitUIToFP(CastInst &CI);
188 Instruction *visitSIToFP(CastInst &CI);
189 Instruction *visitPtrToInt(PtrToIntInst &CI);
190 Instruction *visitIntToPtr(IntToPtrInst &CI);
191 Instruction *visitBitCast(BitCastInst &CI);
Matt Arsenaulta9e95ab2013-11-15 05:45:08 +0000192 Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000193 Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI,
194 Instruction *FI);
195 Instruction *FoldSelectIntoOp(SelectInst &SI, Value*, Value*);
196 Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
197 Value *A, Value *B, Instruction &Outer,
198 SelectPatternFlavor SPF2, Value *C);
199 Instruction *visitSelectInst(SelectInst &SI);
200 Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
201 Instruction *visitCallInst(CallInst &CI);
202 Instruction *visitInvokeInst(InvokeInst &II);
203
204 Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
205 Instruction *visitPHINode(PHINode &PN);
206 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
207 Instruction *visitAllocaInst(AllocaInst &AI);
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000208 Instruction *visitAllocSite(Instruction &FI);
Gabor Greif75f69432010-06-24 12:21:15 +0000209 Instruction *visitFree(CallInst &FI);
Chris Lattner35522b72010-01-04 07:12:23 +0000210 Instruction *visitLoadInst(LoadInst &LI);
211 Instruction *visitStoreInst(StoreInst &SI);
212 Instruction *visitBranchInst(BranchInst &BI);
213 Instruction *visitSwitchInst(SwitchInst &SI);
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000214 Instruction *visitInsertValueInst(InsertValueInst &IV);
Chris Lattner35522b72010-01-04 07:12:23 +0000215 Instruction *visitInsertElementInst(InsertElementInst &IE);
216 Instruction *visitExtractElementInst(ExtractElementInst &EI);
217 Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
218 Instruction *visitExtractValueInst(ExtractValueInst &EV);
Duncan Sands5c055792011-09-30 13:12:16 +0000219 Instruction *visitLandingPadInst(LandingPadInst &LI);
Chris Lattner35522b72010-01-04 07:12:23 +0000220
221 // visitInstruction - Specify what to return for unhandled instructions...
Craig Toppere73658d2014-04-28 04:05:08 +0000222 Instruction *visitInstruction(Instruction &I) { return nullptr; }
Chris Lattner35522b72010-01-04 07:12:23 +0000223
224private:
Chris Lattner229907c2011-07-18 04:54:35 +0000225 bool ShouldChangeType(Type *From, Type *To) const;
Chris Lattner2188e402010-01-04 07:37:31 +0000226 Value *dyn_castNegVal(Value *V) const;
Shuxin Yangf0537ab2013-01-09 00:13:41 +0000227 Value *dyn_castFNegVal(Value *V, bool NoSignedZero=false) const;
Matt Arsenaultd79f7d92013-08-19 22:17:40 +0000228 Type *FindElementAtOffset(Type *PtrTy, int64_t Offset,
229 SmallVectorImpl<Value*> &NewIndices);
Chris Lattner2b295a02010-01-04 07:53:58 +0000230 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000231
Chris Lattner4e8137d2010-02-11 06:26:33 +0000232 /// ShouldOptimizeCast - Return true if the cast from "V to Ty" actually
233 /// results in any code being generated and is interesting to optimize out. If
234 /// the cast can be eliminated by some other simple transformation, we prefer
235 /// to do the simplification first.
236 bool ShouldOptimizeCast(Instruction::CastOps opcode,const Value *V,
Chris Lattner229907c2011-07-18 04:54:35 +0000237 Type *Ty);
Chris Lattner2188e402010-01-04 07:37:31 +0000238
Chris Lattner35522b72010-01-04 07:12:23 +0000239 Instruction *visitCallSite(CallSite CS);
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000240 Instruction *tryOptimizeCall(CallInst *CI, const DataLayout *DL);
Chris Lattner35522b72010-01-04 07:12:23 +0000241 bool transformConstExprCastCall(CallSite CS);
Duncan Sandsa0984362011-09-06 13:37:06 +0000242 Instruction *transformCallThroughTrampoline(CallSite CS,
243 IntrinsicInst *Tramp);
Chris Lattner35522b72010-01-04 07:12:23 +0000244 Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
245 bool DoXform = true);
Benjamin Kramer398b8c52011-04-01 20:09:03 +0000246 Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
Chris Lattner35522b72010-01-04 07:12:23 +0000247 bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000248 Value *EmitGEPOffset(User *GEP);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000249 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
Nick Lewyckya2b77202013-05-31 00:59:42 +0000250 Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
Chris Lattner35522b72010-01-04 07:12:23 +0000251
252public:
253 // InsertNewInstBefore - insert an instruction New before instruction Old
254 // in the program. Add the new instruction to the worklist.
255 //
256 Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
Craig Toppere73658d2014-04-28 04:05:08 +0000257 assert(New && !New->getParent() &&
Chris Lattner35522b72010-01-04 07:12:23 +0000258 "New instruction already inserted into a basic block!");
259 BasicBlock *BB = Old.getParent();
260 BB->getInstList().insert(&Old, New); // Insert inst
261 Worklist.Add(New);
262 return New;
263 }
Eli Friedman6efb64e2011-05-19 01:20:42 +0000264
Jakub Staszak190db2f2013-01-14 23:16:36 +0000265 // InsertNewInstWith - same as InsertNewInstBefore, but also sets the
Eli Friedman6efb64e2011-05-19 01:20:42 +0000266 // debug loc.
267 //
268 Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
269 New->setDebugLoc(Old.getDebugLoc());
270 return InsertNewInstBefore(New, Old);
271 }
272
Chris Lattner35522b72010-01-04 07:12:23 +0000273 // ReplaceInstUsesWith - This method is to be used when an instruction is
274 // found to be dead, replacable with another preexisting expression. Here
275 // we add all uses of I to the worklist, replace all uses of I with the new
276 // value, then return I, so that the inst combiner will know that I was
277 // modified.
278 //
279 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
280 Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000281
Chris Lattner35522b72010-01-04 07:12:23 +0000282 // If we are replacing the instruction with itself, this must be in a
283 // segment of unreachable code, so just clobber the instruction.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000284 if (&I == V)
Chris Lattner35522b72010-01-04 07:12:23 +0000285 V = UndefValue::get(I.getType());
Frits van Bommeld14d9912011-03-27 23:32:31 +0000286
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000287 DEBUG(dbgs() << "IC: Replacing " << I << "\n"
Frits van Bommeld14d9912011-03-27 23:32:31 +0000288 " with " << *V << '\n');
289
Chris Lattner35522b72010-01-04 07:12:23 +0000290 I.replaceAllUsesWith(V);
291 return &I;
292 }
293
294 // EraseInstFromFunction - When dealing with an instruction that has side
295 // effects or produces a void value, we can't rely on DCE to delete the
296 // instruction. Instead, visit methods should return the value returned by
297 // this function.
298 Instruction *EraseInstFromFunction(Instruction &I) {
Matt Arsenaulte6db7602013-09-05 19:48:28 +0000299 DEBUG(dbgs() << "IC: ERASE " << I << '\n');
Chris Lattner35522b72010-01-04 07:12:23 +0000300
301 assert(I.use_empty() && "Cannot erase instruction that is used!");
302 // Make sure that we reprocess all operands now that we reduced their
303 // use counts.
304 if (I.getNumOperands() < 8) {
305 for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
306 if (Instruction *Op = dyn_cast<Instruction>(*i))
307 Worklist.Add(Op);
308 }
309 Worklist.Remove(&I);
310 I.eraseFromParent();
311 MadeIRChange = true;
Craig Toppere73658d2014-04-28 04:05:08 +0000312 return nullptr; // Don't do anything with FI
Chris Lattner35522b72010-01-04 07:12:23 +0000313 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000314
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000315 void ComputeMaskedBits(Value *V, APInt &KnownZero,
Chris Lattner35522b72010-01-04 07:12:23 +0000316 APInt &KnownOne, unsigned Depth = 0) const {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000317 return llvm::ComputeMaskedBits(V, KnownZero, KnownOne, DL, Depth);
Chris Lattner35522b72010-01-04 07:12:23 +0000318 }
Jakub Staszak190db2f2013-01-14 23:16:36 +0000319
320 bool MaskedValueIsZero(Value *V, const APInt &Mask,
Chris Lattner35522b72010-01-04 07:12:23 +0000321 unsigned Depth = 0) const {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000322 return llvm::MaskedValueIsZero(V, Mask, DL, Depth);
Chris Lattner35522b72010-01-04 07:12:23 +0000323 }
324 unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000325 return llvm::ComputeNumSignBits(Op, DL, Depth);
Chris Lattner35522b72010-01-04 07:12:23 +0000326 }
327
328private:
329
Duncan Sands641baf12010-11-13 15:10:37 +0000330 /// SimplifyAssociativeOrCommutative - This performs a few simplifications for
331 /// operators which are associative or commutative.
332 bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000333
Duncan Sandsfbb9ac32010-12-22 13:36:08 +0000334 /// SimplifyUsingDistributiveLaws - This tries to simplify binary operations
335 /// which some other binary operation distributes over either by factorizing
336 /// out common terms (eg "(A*B)+(A*C)" -> "A*(B+C)") or expanding out if this
337 /// results in simplifications (eg: "A & (B | C) -> (A&B) | (A&C)" if this is
338 /// a win). Returns the simplified value, or null if it didn't simplify.
339 Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
Duncan Sandsadc7771f2010-11-23 14:23:47 +0000340
Chris Lattner35522b72010-01-04 07:12:23 +0000341 /// SimplifyDemandedUseBits - Attempts to replace V with a simpler value
342 /// based on the demanded bits.
Jakub Staszak190db2f2013-01-14 23:16:36 +0000343 Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
Chris Lattner35522b72010-01-04 07:12:23 +0000344 APInt& KnownZero, APInt& KnownOne,
345 unsigned Depth);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000346 bool SimplifyDemandedBits(Use &U, APInt DemandedMask,
Chris Lattner35522b72010-01-04 07:12:23 +0000347 APInt& KnownZero, APInt& KnownOne,
348 unsigned Depth=0);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000349 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
350 /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
351 Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
352 APInt DemandedMask, APInt &KnownZero,
353 APInt &KnownOne);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000354
Chris Lattner35522b72010-01-04 07:12:23 +0000355 /// SimplifyDemandedInstructionBits - Inst is an integer instruction that
356 /// SimplifyDemandedBits knows about. See if the instruction has any
357 /// properties that allow us to simplify its operands.
358 bool SimplifyDemandedInstructionBits(Instruction &Inst);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000359
Chris Lattner35522b72010-01-04 07:12:23 +0000360 Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
361 APInt& UndefElts, unsigned Depth = 0);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000362
Chris Lattner35522b72010-01-04 07:12:23 +0000363 // FoldOpIntoPhi - Given a binary operator, cast instruction, or select
364 // which has a PHI node as operand #0, see if we can fold the instruction
365 // into the PHI (which is only possible if all operands to the PHI are
366 // constants).
367 //
Chris Lattnerea7131a2011-01-16 05:14:26 +0000368 Instruction *FoldOpIntoPhi(Instruction &I);
Chris Lattner35522b72010-01-04 07:12:23 +0000369
370 // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
371 // operator and they all are only used by the PHI, PHI together their
372 // inputs, and do the operation once, to the result of the PHI.
373 Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
374 Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
375 Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
376 Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
377
Jakub Staszak190db2f2013-01-14 23:16:36 +0000378
Chris Lattner35522b72010-01-04 07:12:23 +0000379 Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
380 ConstantInt *AndRHS, BinaryOperator &TheAnd);
Jakub Staszak190db2f2013-01-14 23:16:36 +0000381
Chris Lattner35522b72010-01-04 07:12:23 +0000382 Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
383 bool isSub, Instruction &I);
Chris Lattner067459c2010-03-05 08:46:26 +0000384 Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
385 bool isSigned, bool Inside);
Chris Lattner35522b72010-01-04 07:12:23 +0000386 Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
387 Instruction *MatchBSwap(BinaryOperator &I);
388 bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
389 Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
390 Instruction *SimplifyMemSet(MemSetInst *MI);
391
392
Chris Lattner229907c2011-07-18 04:54:35 +0000393 Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
Duncan Sands533c8ae2012-10-23 08:28:26 +0000394
395 /// Descale - Return a value X such that Val = X * Scale, or null if none. If
396 /// the multiplication is known not to overflow then NoSignedWrap is set.
397 Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
Chris Lattner35522b72010-01-04 07:12:23 +0000398};
399
Chris Lattner35522b72010-01-04 07:12:23 +0000400} // end namespace llvm.
401
Chandler Carruth964daaa2014-04-22 02:55:47 +0000402#undef DEBUG_TYPE
403
Chris Lattner35522b72010-01-04 07:12:23 +0000404#endif