xref: /aosp_15_r20/external/llvm/lib/Transforms/InstCombine/InstCombineInternal.h (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- InstCombineInternal.h - InstCombine pass internals -------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker /// \file
10*9880d681SAndroid Build Coastguard Worker ///
11*9880d681SAndroid Build Coastguard Worker /// This file provides internal interfaces used to implement the InstCombine.
12*9880d681SAndroid Build Coastguard Worker ///
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker 
15*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
16*9880d681SAndroid Build Coastguard Worker #define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
17*9880d681SAndroid Build Coastguard Worker 
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AssumptionCache.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/LoopInfo.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetFolder.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IRBuilder.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstVisitor.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Operator.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/PatternMatch.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
31*9880d681SAndroid Build Coastguard Worker 
32*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "instcombine"
33*9880d681SAndroid Build Coastguard Worker 
34*9880d681SAndroid Build Coastguard Worker namespace llvm {
35*9880d681SAndroid Build Coastguard Worker class CallSite;
36*9880d681SAndroid Build Coastguard Worker class DataLayout;
37*9880d681SAndroid Build Coastguard Worker class DominatorTree;
38*9880d681SAndroid Build Coastguard Worker class TargetLibraryInfo;
39*9880d681SAndroid Build Coastguard Worker class DbgDeclareInst;
40*9880d681SAndroid Build Coastguard Worker class MemIntrinsic;
41*9880d681SAndroid Build Coastguard Worker class MemSetInst;
42*9880d681SAndroid Build Coastguard Worker 
43*9880d681SAndroid Build Coastguard Worker /// \brief Assign a complexity or rank value to LLVM Values.
44*9880d681SAndroid Build Coastguard Worker ///
45*9880d681SAndroid Build Coastguard Worker /// This routine maps IR values to various complexity ranks:
46*9880d681SAndroid Build Coastguard Worker ///   0 -> undef
47*9880d681SAndroid Build Coastguard Worker ///   1 -> Constants
48*9880d681SAndroid Build Coastguard Worker ///   2 -> Other non-instructions
49*9880d681SAndroid Build Coastguard Worker ///   3 -> Arguments
50*9880d681SAndroid Build Coastguard Worker ///   3 -> Unary operations
51*9880d681SAndroid Build Coastguard Worker ///   4 -> Other instructions
getComplexity(Value * V)52*9880d681SAndroid Build Coastguard Worker static inline unsigned getComplexity(Value *V) {
53*9880d681SAndroid Build Coastguard Worker   if (isa<Instruction>(V)) {
54*9880d681SAndroid Build Coastguard Worker     if (BinaryOperator::isNeg(V) || BinaryOperator::isFNeg(V) ||
55*9880d681SAndroid Build Coastguard Worker         BinaryOperator::isNot(V))
56*9880d681SAndroid Build Coastguard Worker       return 3;
57*9880d681SAndroid Build Coastguard Worker     return 4;
58*9880d681SAndroid Build Coastguard Worker   }
59*9880d681SAndroid Build Coastguard Worker   if (isa<Argument>(V))
60*9880d681SAndroid Build Coastguard Worker     return 3;
61*9880d681SAndroid Build Coastguard Worker   return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
62*9880d681SAndroid Build Coastguard Worker }
63*9880d681SAndroid Build Coastguard Worker 
64*9880d681SAndroid Build Coastguard Worker /// \brief Add one to a Constant
AddOne(Constant * C)65*9880d681SAndroid Build Coastguard Worker static inline Constant *AddOne(Constant *C) {
66*9880d681SAndroid Build Coastguard Worker   return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
67*9880d681SAndroid Build Coastguard Worker }
68*9880d681SAndroid Build Coastguard Worker /// \brief Subtract one from a Constant
SubOne(Constant * C)69*9880d681SAndroid Build Coastguard Worker static inline Constant *SubOne(Constant *C) {
70*9880d681SAndroid Build Coastguard Worker   return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
71*9880d681SAndroid Build Coastguard Worker }
72*9880d681SAndroid Build Coastguard Worker 
73*9880d681SAndroid Build Coastguard Worker /// \brief Return true if the specified value is free to invert (apply ~ to).
74*9880d681SAndroid Build Coastguard Worker /// This happens in cases where the ~ can be eliminated.  If WillInvertAllUses
75*9880d681SAndroid Build Coastguard Worker /// is true, work under the assumption that the caller intends to remove all
76*9880d681SAndroid Build Coastguard Worker /// uses of V and only keep uses of ~V.
77*9880d681SAndroid Build Coastguard Worker ///
IsFreeToInvert(Value * V,bool WillInvertAllUses)78*9880d681SAndroid Build Coastguard Worker static inline bool IsFreeToInvert(Value *V, bool WillInvertAllUses) {
79*9880d681SAndroid Build Coastguard Worker   // ~(~(X)) -> X.
80*9880d681SAndroid Build Coastguard Worker   if (BinaryOperator::isNot(V))
81*9880d681SAndroid Build Coastguard Worker     return true;
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker   // Constants can be considered to be not'ed values.
84*9880d681SAndroid Build Coastguard Worker   if (isa<ConstantInt>(V))
85*9880d681SAndroid Build Coastguard Worker     return true;
86*9880d681SAndroid Build Coastguard Worker 
87*9880d681SAndroid Build Coastguard Worker   // Compares can be inverted if all of their uses are being modified to use the
88*9880d681SAndroid Build Coastguard Worker   // ~V.
89*9880d681SAndroid Build Coastguard Worker   if (isa<CmpInst>(V))
90*9880d681SAndroid Build Coastguard Worker     return WillInvertAllUses;
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker   // If `V` is of the form `A + Constant` then `-1 - V` can be folded into `(-1
93*9880d681SAndroid Build Coastguard Worker   // - Constant) - A` if we are willing to invert all of the uses.
94*9880d681SAndroid Build Coastguard Worker   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
95*9880d681SAndroid Build Coastguard Worker     if (BO->getOpcode() == Instruction::Add ||
96*9880d681SAndroid Build Coastguard Worker         BO->getOpcode() == Instruction::Sub)
97*9880d681SAndroid Build Coastguard Worker       if (isa<Constant>(BO->getOperand(0)) || isa<Constant>(BO->getOperand(1)))
98*9880d681SAndroid Build Coastguard Worker         return WillInvertAllUses;
99*9880d681SAndroid Build Coastguard Worker 
100*9880d681SAndroid Build Coastguard Worker   return false;
101*9880d681SAndroid Build Coastguard Worker }
102*9880d681SAndroid Build Coastguard Worker 
103*9880d681SAndroid Build Coastguard Worker 
104*9880d681SAndroid Build Coastguard Worker /// \brief Specific patterns of overflow check idioms that we match.
105*9880d681SAndroid Build Coastguard Worker enum OverflowCheckFlavor {
106*9880d681SAndroid Build Coastguard Worker   OCF_UNSIGNED_ADD,
107*9880d681SAndroid Build Coastguard Worker   OCF_SIGNED_ADD,
108*9880d681SAndroid Build Coastguard Worker   OCF_UNSIGNED_SUB,
109*9880d681SAndroid Build Coastguard Worker   OCF_SIGNED_SUB,
110*9880d681SAndroid Build Coastguard Worker   OCF_UNSIGNED_MUL,
111*9880d681SAndroid Build Coastguard Worker   OCF_SIGNED_MUL,
112*9880d681SAndroid Build Coastguard Worker 
113*9880d681SAndroid Build Coastguard Worker   OCF_INVALID
114*9880d681SAndroid Build Coastguard Worker };
115*9880d681SAndroid Build Coastguard Worker 
116*9880d681SAndroid Build Coastguard Worker /// \brief Returns the OverflowCheckFlavor corresponding to a overflow_with_op
117*9880d681SAndroid Build Coastguard Worker /// intrinsic.
118*9880d681SAndroid Build Coastguard Worker static inline OverflowCheckFlavor
IntrinsicIDToOverflowCheckFlavor(unsigned ID)119*9880d681SAndroid Build Coastguard Worker IntrinsicIDToOverflowCheckFlavor(unsigned ID) {
120*9880d681SAndroid Build Coastguard Worker   switch (ID) {
121*9880d681SAndroid Build Coastguard Worker   default:
122*9880d681SAndroid Build Coastguard Worker     return OCF_INVALID;
123*9880d681SAndroid Build Coastguard Worker   case Intrinsic::uadd_with_overflow:
124*9880d681SAndroid Build Coastguard Worker     return OCF_UNSIGNED_ADD;
125*9880d681SAndroid Build Coastguard Worker   case Intrinsic::sadd_with_overflow:
126*9880d681SAndroid Build Coastguard Worker     return OCF_SIGNED_ADD;
127*9880d681SAndroid Build Coastguard Worker   case Intrinsic::usub_with_overflow:
128*9880d681SAndroid Build Coastguard Worker     return OCF_UNSIGNED_SUB;
129*9880d681SAndroid Build Coastguard Worker   case Intrinsic::ssub_with_overflow:
130*9880d681SAndroid Build Coastguard Worker     return OCF_SIGNED_SUB;
131*9880d681SAndroid Build Coastguard Worker   case Intrinsic::umul_with_overflow:
132*9880d681SAndroid Build Coastguard Worker     return OCF_UNSIGNED_MUL;
133*9880d681SAndroid Build Coastguard Worker   case Intrinsic::smul_with_overflow:
134*9880d681SAndroid Build Coastguard Worker     return OCF_SIGNED_MUL;
135*9880d681SAndroid Build Coastguard Worker   }
136*9880d681SAndroid Build Coastguard Worker }
137*9880d681SAndroid Build Coastguard Worker 
138*9880d681SAndroid Build Coastguard Worker /// \brief An IRBuilder inserter that adds new instructions to the instcombine
139*9880d681SAndroid Build Coastguard Worker /// worklist.
140*9880d681SAndroid Build Coastguard Worker class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter
141*9880d681SAndroid Build Coastguard Worker     : public IRBuilderDefaultInserter {
142*9880d681SAndroid Build Coastguard Worker   InstCombineWorklist &Worklist;
143*9880d681SAndroid Build Coastguard Worker   AssumptionCache *AC;
144*9880d681SAndroid Build Coastguard Worker 
145*9880d681SAndroid Build Coastguard Worker public:
InstCombineIRInserter(InstCombineWorklist & WL,AssumptionCache * AC)146*9880d681SAndroid Build Coastguard Worker   InstCombineIRInserter(InstCombineWorklist &WL, AssumptionCache *AC)
147*9880d681SAndroid Build Coastguard Worker       : Worklist(WL), AC(AC) {}
148*9880d681SAndroid Build Coastguard Worker 
InsertHelper(Instruction * I,const Twine & Name,BasicBlock * BB,BasicBlock::iterator InsertPt)149*9880d681SAndroid Build Coastguard Worker   void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB,
150*9880d681SAndroid Build Coastguard Worker                     BasicBlock::iterator InsertPt) const {
151*9880d681SAndroid Build Coastguard Worker     IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
152*9880d681SAndroid Build Coastguard Worker     Worklist.Add(I);
153*9880d681SAndroid Build Coastguard Worker 
154*9880d681SAndroid Build Coastguard Worker     using namespace llvm::PatternMatch;
155*9880d681SAndroid Build Coastguard Worker     if (match(I, m_Intrinsic<Intrinsic::assume>()))
156*9880d681SAndroid Build Coastguard Worker       AC->registerAssumption(cast<CallInst>(I));
157*9880d681SAndroid Build Coastguard Worker   }
158*9880d681SAndroid Build Coastguard Worker };
159*9880d681SAndroid Build Coastguard Worker 
160*9880d681SAndroid Build Coastguard Worker /// \brief The core instruction combiner logic.
161*9880d681SAndroid Build Coastguard Worker ///
162*9880d681SAndroid Build Coastguard Worker /// This class provides both the logic to recursively visit instructions and
163*9880d681SAndroid Build Coastguard Worker /// combine them, as well as the pass infrastructure for running this as part
164*9880d681SAndroid Build Coastguard Worker /// of the LLVM pass pipeline.
165*9880d681SAndroid Build Coastguard Worker class LLVM_LIBRARY_VISIBILITY InstCombiner
166*9880d681SAndroid Build Coastguard Worker     : public InstVisitor<InstCombiner, Instruction *> {
167*9880d681SAndroid Build Coastguard Worker   // FIXME: These members shouldn't be public.
168*9880d681SAndroid Build Coastguard Worker public:
169*9880d681SAndroid Build Coastguard Worker   /// \brief A worklist of the instructions that need to be simplified.
170*9880d681SAndroid Build Coastguard Worker   InstCombineWorklist &Worklist;
171*9880d681SAndroid Build Coastguard Worker 
172*9880d681SAndroid Build Coastguard Worker   /// \brief An IRBuilder that automatically inserts new instructions into the
173*9880d681SAndroid Build Coastguard Worker   /// worklist.
174*9880d681SAndroid Build Coastguard Worker   typedef IRBuilder<TargetFolder, InstCombineIRInserter> BuilderTy;
175*9880d681SAndroid Build Coastguard Worker   BuilderTy *Builder;
176*9880d681SAndroid Build Coastguard Worker 
177*9880d681SAndroid Build Coastguard Worker private:
178*9880d681SAndroid Build Coastguard Worker   // Mode in which we are running the combiner.
179*9880d681SAndroid Build Coastguard Worker   const bool MinimizeSize;
180*9880d681SAndroid Build Coastguard Worker   /// Enable combines that trigger rarely but are costly in compiletime.
181*9880d681SAndroid Build Coastguard Worker   const bool ExpensiveCombines;
182*9880d681SAndroid Build Coastguard Worker 
183*9880d681SAndroid Build Coastguard Worker   AliasAnalysis *AA;
184*9880d681SAndroid Build Coastguard Worker 
185*9880d681SAndroid Build Coastguard Worker   // Required analyses.
186*9880d681SAndroid Build Coastguard Worker   // FIXME: These can never be null and should be references.
187*9880d681SAndroid Build Coastguard Worker   AssumptionCache *AC;
188*9880d681SAndroid Build Coastguard Worker   TargetLibraryInfo *TLI;
189*9880d681SAndroid Build Coastguard Worker   DominatorTree *DT;
190*9880d681SAndroid Build Coastguard Worker   const DataLayout &DL;
191*9880d681SAndroid Build Coastguard Worker 
192*9880d681SAndroid Build Coastguard Worker   // Optional analyses. When non-null, these can both be used to do better
193*9880d681SAndroid Build Coastguard Worker   // combining and will be updated to reflect any changes.
194*9880d681SAndroid Build Coastguard Worker   LoopInfo *LI;
195*9880d681SAndroid Build Coastguard Worker 
196*9880d681SAndroid Build Coastguard Worker   bool MadeIRChange;
197*9880d681SAndroid Build Coastguard Worker 
198*9880d681SAndroid Build Coastguard Worker public:
InstCombiner(InstCombineWorklist & Worklist,BuilderTy * Builder,bool MinimizeSize,bool ExpensiveCombines,AliasAnalysis * AA,AssumptionCache * AC,TargetLibraryInfo * TLI,DominatorTree * DT,const DataLayout & DL,LoopInfo * LI)199*9880d681SAndroid Build Coastguard Worker   InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder,
200*9880d681SAndroid Build Coastguard Worker                bool MinimizeSize, bool ExpensiveCombines, AliasAnalysis *AA,
201*9880d681SAndroid Build Coastguard Worker                AssumptionCache *AC, TargetLibraryInfo *TLI,
202*9880d681SAndroid Build Coastguard Worker                DominatorTree *DT, const DataLayout &DL, LoopInfo *LI)
203*9880d681SAndroid Build Coastguard Worker       : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
204*9880d681SAndroid Build Coastguard Worker         ExpensiveCombines(ExpensiveCombines), AA(AA), AC(AC), TLI(TLI), DT(DT),
205*9880d681SAndroid Build Coastguard Worker         DL(DL), LI(LI), MadeIRChange(false) {}
206*9880d681SAndroid Build Coastguard Worker 
207*9880d681SAndroid Build Coastguard Worker   /// \brief Run the combiner over the entire worklist until it is empty.
208*9880d681SAndroid Build Coastguard Worker   ///
209*9880d681SAndroid Build Coastguard Worker   /// \returns true if the IR is changed.
210*9880d681SAndroid Build Coastguard Worker   bool run();
211*9880d681SAndroid Build Coastguard Worker 
getAssumptionCache()212*9880d681SAndroid Build Coastguard Worker   AssumptionCache *getAssumptionCache() const { return AC; }
213*9880d681SAndroid Build Coastguard Worker 
getDataLayout()214*9880d681SAndroid Build Coastguard Worker   const DataLayout &getDataLayout() const { return DL; }
215*9880d681SAndroid Build Coastguard Worker 
getDominatorTree()216*9880d681SAndroid Build Coastguard Worker   DominatorTree *getDominatorTree() const { return DT; }
217*9880d681SAndroid Build Coastguard Worker 
getLoopInfo()218*9880d681SAndroid Build Coastguard Worker   LoopInfo *getLoopInfo() const { return LI; }
219*9880d681SAndroid Build Coastguard Worker 
getTargetLibraryInfo()220*9880d681SAndroid Build Coastguard Worker   TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
221*9880d681SAndroid Build Coastguard Worker 
222*9880d681SAndroid Build Coastguard Worker   // Visitation implementation - Implement instruction combining for different
223*9880d681SAndroid Build Coastguard Worker   // instruction types.  The semantics are as follows:
224*9880d681SAndroid Build Coastguard Worker   // Return Value:
225*9880d681SAndroid Build Coastguard Worker   //    null        - No change was made
226*9880d681SAndroid Build Coastguard Worker   //     I          - Change was made, I is still valid, I may be dead though
227*9880d681SAndroid Build Coastguard Worker   //   otherwise    - Change was made, replace I with returned instruction
228*9880d681SAndroid Build Coastguard Worker   //
229*9880d681SAndroid Build Coastguard Worker   Instruction *visitAdd(BinaryOperator &I);
230*9880d681SAndroid Build Coastguard Worker   Instruction *visitFAdd(BinaryOperator &I);
231*9880d681SAndroid Build Coastguard Worker   Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
232*9880d681SAndroid Build Coastguard Worker   Instruction *visitSub(BinaryOperator &I);
233*9880d681SAndroid Build Coastguard Worker   Instruction *visitFSub(BinaryOperator &I);
234*9880d681SAndroid Build Coastguard Worker   Instruction *visitMul(BinaryOperator &I);
235*9880d681SAndroid Build Coastguard Worker   Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
236*9880d681SAndroid Build Coastguard Worker                        Instruction *InsertBefore);
237*9880d681SAndroid Build Coastguard Worker   Instruction *visitFMul(BinaryOperator &I);
238*9880d681SAndroid Build Coastguard Worker   Instruction *visitURem(BinaryOperator &I);
239*9880d681SAndroid Build Coastguard Worker   Instruction *visitSRem(BinaryOperator &I);
240*9880d681SAndroid Build Coastguard Worker   Instruction *visitFRem(BinaryOperator &I);
241*9880d681SAndroid Build Coastguard Worker   bool SimplifyDivRemOfSelect(BinaryOperator &I);
242*9880d681SAndroid Build Coastguard Worker   Instruction *commonRemTransforms(BinaryOperator &I);
243*9880d681SAndroid Build Coastguard Worker   Instruction *commonIRemTransforms(BinaryOperator &I);
244*9880d681SAndroid Build Coastguard Worker   Instruction *commonDivTransforms(BinaryOperator &I);
245*9880d681SAndroid Build Coastguard Worker   Instruction *commonIDivTransforms(BinaryOperator &I);
246*9880d681SAndroid Build Coastguard Worker   Instruction *visitUDiv(BinaryOperator &I);
247*9880d681SAndroid Build Coastguard Worker   Instruction *visitSDiv(BinaryOperator &I);
248*9880d681SAndroid Build Coastguard Worker   Instruction *visitFDiv(BinaryOperator &I);
249*9880d681SAndroid Build Coastguard Worker   Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
250*9880d681SAndroid Build Coastguard Worker   Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
251*9880d681SAndroid Build Coastguard Worker   Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
252*9880d681SAndroid Build Coastguard Worker   Instruction *visitAnd(BinaryOperator &I);
253*9880d681SAndroid Build Coastguard Worker   Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction *CxtI);
254*9880d681SAndroid Build Coastguard Worker   Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
255*9880d681SAndroid Build Coastguard Worker   Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A,
256*9880d681SAndroid Build Coastguard Worker                                    Value *B, Value *C);
257*9880d681SAndroid Build Coastguard Worker   Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A,
258*9880d681SAndroid Build Coastguard Worker                                     Value *B, Value *C);
259*9880d681SAndroid Build Coastguard Worker   Instruction *visitOr(BinaryOperator &I);
260*9880d681SAndroid Build Coastguard Worker   Instruction *visitXor(BinaryOperator &I);
261*9880d681SAndroid Build Coastguard Worker   Instruction *visitShl(BinaryOperator &I);
262*9880d681SAndroid Build Coastguard Worker   Instruction *visitAShr(BinaryOperator &I);
263*9880d681SAndroid Build Coastguard Worker   Instruction *visitLShr(BinaryOperator &I);
264*9880d681SAndroid Build Coastguard Worker   Instruction *commonShiftTransforms(BinaryOperator &I);
265*9880d681SAndroid Build Coastguard Worker   Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
266*9880d681SAndroid Build Coastguard Worker                                     Constant *RHSC);
267*9880d681SAndroid Build Coastguard Worker   Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
268*9880d681SAndroid Build Coastguard Worker                                             GlobalVariable *GV, CmpInst &ICI,
269*9880d681SAndroid Build Coastguard Worker                                             ConstantInt *AndCst = nullptr);
270*9880d681SAndroid Build Coastguard Worker   Instruction *visitFCmpInst(FCmpInst &I);
271*9880d681SAndroid Build Coastguard Worker   Instruction *visitICmpInst(ICmpInst &I);
272*9880d681SAndroid Build Coastguard Worker   Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
273*9880d681SAndroid Build Coastguard Worker   Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS,
274*9880d681SAndroid Build Coastguard Worker                                               ConstantInt *RHS);
275*9880d681SAndroid Build Coastguard Worker   Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
276*9880d681SAndroid Build Coastguard Worker                               ConstantInt *DivRHS);
277*9880d681SAndroid Build Coastguard Worker   Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI,
278*9880d681SAndroid Build Coastguard Worker                               ConstantInt *DivRHS);
279*9880d681SAndroid Build Coastguard Worker   Instruction *FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A,
280*9880d681SAndroid Build Coastguard Worker                                  ConstantInt *CI1, ConstantInt *CI2);
281*9880d681SAndroid Build Coastguard Worker   Instruction *FoldICmpCstShlCst(ICmpInst &I, Value *Op, Value *A,
282*9880d681SAndroid Build Coastguard Worker                                  ConstantInt *CI1, ConstantInt *CI2);
283*9880d681SAndroid Build Coastguard Worker   Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI,
284*9880d681SAndroid Build Coastguard Worker                                 ICmpInst::Predicate Pred);
285*9880d681SAndroid Build Coastguard Worker   Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
286*9880d681SAndroid Build Coastguard Worker                            ICmpInst::Predicate Cond, Instruction &I);
287*9880d681SAndroid Build Coastguard Worker   Instruction *FoldAllocaCmp(ICmpInst &ICI, AllocaInst *Alloca, Value *Other);
288*9880d681SAndroid Build Coastguard Worker   Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
289*9880d681SAndroid Build Coastguard Worker                                    BinaryOperator &I);
290*9880d681SAndroid Build Coastguard Worker   Instruction *commonCastTransforms(CastInst &CI);
291*9880d681SAndroid Build Coastguard Worker   Instruction *commonPointerCastTransforms(CastInst &CI);
292*9880d681SAndroid Build Coastguard Worker   Instruction *visitTrunc(TruncInst &CI);
293*9880d681SAndroid Build Coastguard Worker   Instruction *visitZExt(ZExtInst &CI);
294*9880d681SAndroid Build Coastguard Worker   Instruction *visitSExt(SExtInst &CI);
295*9880d681SAndroid Build Coastguard Worker   Instruction *visitFPTrunc(FPTruncInst &CI);
296*9880d681SAndroid Build Coastguard Worker   Instruction *visitFPExt(CastInst &CI);
297*9880d681SAndroid Build Coastguard Worker   Instruction *visitFPToUI(FPToUIInst &FI);
298*9880d681SAndroid Build Coastguard Worker   Instruction *visitFPToSI(FPToSIInst &FI);
299*9880d681SAndroid Build Coastguard Worker   Instruction *visitUIToFP(CastInst &CI);
300*9880d681SAndroid Build Coastguard Worker   Instruction *visitSIToFP(CastInst &CI);
301*9880d681SAndroid Build Coastguard Worker   Instruction *visitPtrToInt(PtrToIntInst &CI);
302*9880d681SAndroid Build Coastguard Worker   Instruction *visitIntToPtr(IntToPtrInst &CI);
303*9880d681SAndroid Build Coastguard Worker   Instruction *visitBitCast(BitCastInst &CI);
304*9880d681SAndroid Build Coastguard Worker   Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
305*9880d681SAndroid Build Coastguard Worker   Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
306*9880d681SAndroid Build Coastguard Worker   Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *);
307*9880d681SAndroid Build Coastguard Worker   Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
308*9880d681SAndroid Build Coastguard Worker                             Value *A, Value *B, Instruction &Outer,
309*9880d681SAndroid Build Coastguard Worker                             SelectPatternFlavor SPF2, Value *C);
310*9880d681SAndroid Build Coastguard Worker   Instruction *FoldItoFPtoI(Instruction &FI);
311*9880d681SAndroid Build Coastguard Worker   Instruction *visitSelectInst(SelectInst &SI);
312*9880d681SAndroid Build Coastguard Worker   Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
313*9880d681SAndroid Build Coastguard Worker   Instruction *visitCallInst(CallInst &CI);
314*9880d681SAndroid Build Coastguard Worker   Instruction *visitInvokeInst(InvokeInst &II);
315*9880d681SAndroid Build Coastguard Worker 
316*9880d681SAndroid Build Coastguard Worker   Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
317*9880d681SAndroid Build Coastguard Worker   Instruction *visitPHINode(PHINode &PN);
318*9880d681SAndroid Build Coastguard Worker   Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
319*9880d681SAndroid Build Coastguard Worker   Instruction *visitAllocaInst(AllocaInst &AI);
320*9880d681SAndroid Build Coastguard Worker   Instruction *visitAllocSite(Instruction &FI);
321*9880d681SAndroid Build Coastguard Worker   Instruction *visitFree(CallInst &FI);
322*9880d681SAndroid Build Coastguard Worker   Instruction *visitLoadInst(LoadInst &LI);
323*9880d681SAndroid Build Coastguard Worker   Instruction *visitStoreInst(StoreInst &SI);
324*9880d681SAndroid Build Coastguard Worker   Instruction *visitBranchInst(BranchInst &BI);
325*9880d681SAndroid Build Coastguard Worker   Instruction *visitSwitchInst(SwitchInst &SI);
326*9880d681SAndroid Build Coastguard Worker   Instruction *visitReturnInst(ReturnInst &RI);
327*9880d681SAndroid Build Coastguard Worker   Instruction *visitInsertValueInst(InsertValueInst &IV);
328*9880d681SAndroid Build Coastguard Worker   Instruction *visitInsertElementInst(InsertElementInst &IE);
329*9880d681SAndroid Build Coastguard Worker   Instruction *visitExtractElementInst(ExtractElementInst &EI);
330*9880d681SAndroid Build Coastguard Worker   Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
331*9880d681SAndroid Build Coastguard Worker   Instruction *visitExtractValueInst(ExtractValueInst &EV);
332*9880d681SAndroid Build Coastguard Worker   Instruction *visitLandingPadInst(LandingPadInst &LI);
333*9880d681SAndroid Build Coastguard Worker   Instruction *visitVAStartInst(VAStartInst &I);
334*9880d681SAndroid Build Coastguard Worker   Instruction *visitVACopyInst(VACopyInst &I);
335*9880d681SAndroid Build Coastguard Worker 
336*9880d681SAndroid Build Coastguard Worker   // visitInstruction - Specify what to return for unhandled instructions...
visitInstruction(Instruction & I)337*9880d681SAndroid Build Coastguard Worker   Instruction *visitInstruction(Instruction &I) { return nullptr; }
338*9880d681SAndroid Build Coastguard Worker 
339*9880d681SAndroid Build Coastguard Worker   // True when DB dominates all uses of DI execpt UI.
340*9880d681SAndroid Build Coastguard Worker   // UI must be in the same block as DI.
341*9880d681SAndroid Build Coastguard Worker   // The routine checks that the DI parent and DB are different.
342*9880d681SAndroid Build Coastguard Worker   bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
343*9880d681SAndroid Build Coastguard Worker                         const BasicBlock *DB) const;
344*9880d681SAndroid Build Coastguard Worker 
345*9880d681SAndroid Build Coastguard Worker   // Replace select with select operand SIOpd in SI-ICmp sequence when possible
346*9880d681SAndroid Build Coastguard Worker   bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
347*9880d681SAndroid Build Coastguard Worker                                  const unsigned SIOpd);
348*9880d681SAndroid Build Coastguard Worker 
349*9880d681SAndroid Build Coastguard Worker private:
350*9880d681SAndroid Build Coastguard Worker   bool ShouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
351*9880d681SAndroid Build Coastguard Worker   bool ShouldChangeType(Type *From, Type *To) const;
352*9880d681SAndroid Build Coastguard Worker   Value *dyn_castNegVal(Value *V) const;
353*9880d681SAndroid Build Coastguard Worker   Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
354*9880d681SAndroid Build Coastguard Worker   Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset,
355*9880d681SAndroid Build Coastguard Worker                             SmallVectorImpl<Value *> &NewIndices);
356*9880d681SAndroid Build Coastguard Worker   Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
357*9880d681SAndroid Build Coastguard Worker 
358*9880d681SAndroid Build Coastguard Worker   /// \brief Classify whether a cast is worth optimizing.
359*9880d681SAndroid Build Coastguard Worker   ///
360*9880d681SAndroid Build Coastguard Worker   /// Returns true if the cast from "V to Ty" actually results in any code
361*9880d681SAndroid Build Coastguard Worker   /// being generated and is interesting to optimize out. If the cast can be
362*9880d681SAndroid Build Coastguard Worker   /// eliminated by some other simple transformation, we prefer to do the
363*9880d681SAndroid Build Coastguard Worker   /// simplification first.
364*9880d681SAndroid Build Coastguard Worker   bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V,
365*9880d681SAndroid Build Coastguard Worker                           Type *Ty);
366*9880d681SAndroid Build Coastguard Worker 
367*9880d681SAndroid Build Coastguard Worker   /// \brief Try to optimize a sequence of instructions checking if an operation
368*9880d681SAndroid Build Coastguard Worker   /// on LHS and RHS overflows.
369*9880d681SAndroid Build Coastguard Worker   ///
370*9880d681SAndroid Build Coastguard Worker   /// If this overflow check is done via one of the overflow check intrinsics,
371*9880d681SAndroid Build Coastguard Worker   /// then CtxI has to be the call instruction calling that intrinsic.  If this
372*9880d681SAndroid Build Coastguard Worker   /// overflow check is done by arithmetic followed by a compare, then CtxI has
373*9880d681SAndroid Build Coastguard Worker   /// to be the arithmetic instruction.
374*9880d681SAndroid Build Coastguard Worker   ///
375*9880d681SAndroid Build Coastguard Worker   /// If a simplification is possible, stores the simplified result of the
376*9880d681SAndroid Build Coastguard Worker   /// operation in OperationResult and result of the overflow check in
377*9880d681SAndroid Build Coastguard Worker   /// OverflowResult, and return true.  If no simplification is possible,
378*9880d681SAndroid Build Coastguard Worker   /// returns false.
379*9880d681SAndroid Build Coastguard Worker   bool OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS, Value *RHS,
380*9880d681SAndroid Build Coastguard Worker                              Instruction &CtxI, Value *&OperationResult,
381*9880d681SAndroid Build Coastguard Worker                              Constant *&OverflowResult);
382*9880d681SAndroid Build Coastguard Worker 
383*9880d681SAndroid Build Coastguard Worker   Instruction *visitCallSite(CallSite CS);
384*9880d681SAndroid Build Coastguard Worker   Instruction *tryOptimizeCall(CallInst *CI);
385*9880d681SAndroid Build Coastguard Worker   bool transformConstExprCastCall(CallSite CS);
386*9880d681SAndroid Build Coastguard Worker   Instruction *transformCallThroughTrampoline(CallSite CS,
387*9880d681SAndroid Build Coastguard Worker                                               IntrinsicInst *Tramp);
388*9880d681SAndroid Build Coastguard Worker   Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
389*9880d681SAndroid Build Coastguard Worker                                  bool DoXform = true);
390*9880d681SAndroid Build Coastguard Worker   Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
391*9880d681SAndroid Build Coastguard Worker   bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS, Instruction &CxtI);
392*9880d681SAndroid Build Coastguard Worker   bool WillNotOverflowSignedSub(Value *LHS, Value *RHS, Instruction &CxtI);
393*9880d681SAndroid Build Coastguard Worker   bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, Instruction &CxtI);
394*9880d681SAndroid Build Coastguard Worker   bool WillNotOverflowSignedMul(Value *LHS, Value *RHS, Instruction &CxtI);
395*9880d681SAndroid Build Coastguard Worker   Value *EmitGEPOffset(User *GEP);
396*9880d681SAndroid Build Coastguard Worker   Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
397*9880d681SAndroid Build Coastguard Worker   Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
398*9880d681SAndroid Build Coastguard Worker   Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
399*9880d681SAndroid Build Coastguard Worker 
400*9880d681SAndroid Build Coastguard Worker public:
401*9880d681SAndroid Build Coastguard Worker   /// \brief Inserts an instruction \p New before instruction \p Old
402*9880d681SAndroid Build Coastguard Worker   ///
403*9880d681SAndroid Build Coastguard Worker   /// Also adds the new instruction to the worklist and returns \p New so that
404*9880d681SAndroid Build Coastguard Worker   /// it is suitable for use as the return from the visitation patterns.
InsertNewInstBefore(Instruction * New,Instruction & Old)405*9880d681SAndroid Build Coastguard Worker   Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
406*9880d681SAndroid Build Coastguard Worker     assert(New && !New->getParent() &&
407*9880d681SAndroid Build Coastguard Worker            "New instruction already inserted into a basic block!");
408*9880d681SAndroid Build Coastguard Worker     BasicBlock *BB = Old.getParent();
409*9880d681SAndroid Build Coastguard Worker     BB->getInstList().insert(Old.getIterator(), New); // Insert inst
410*9880d681SAndroid Build Coastguard Worker     Worklist.Add(New);
411*9880d681SAndroid Build Coastguard Worker     return New;
412*9880d681SAndroid Build Coastguard Worker   }
413*9880d681SAndroid Build Coastguard Worker 
414*9880d681SAndroid Build Coastguard Worker   /// \brief Same as InsertNewInstBefore, but also sets the debug loc.
InsertNewInstWith(Instruction * New,Instruction & Old)415*9880d681SAndroid Build Coastguard Worker   Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
416*9880d681SAndroid Build Coastguard Worker     New->setDebugLoc(Old.getDebugLoc());
417*9880d681SAndroid Build Coastguard Worker     return InsertNewInstBefore(New, Old);
418*9880d681SAndroid Build Coastguard Worker   }
419*9880d681SAndroid Build Coastguard Worker 
420*9880d681SAndroid Build Coastguard Worker   /// \brief A combiner-aware RAUW-like routine.
421*9880d681SAndroid Build Coastguard Worker   ///
422*9880d681SAndroid Build Coastguard Worker   /// This method is to be used when an instruction is found to be dead,
423*9880d681SAndroid Build Coastguard Worker   /// replaceable with another preexisting expression. Here we add all uses of
424*9880d681SAndroid Build Coastguard Worker   /// I to the worklist, replace all uses of I with the new value, then return
425*9880d681SAndroid Build Coastguard Worker   /// I, so that the inst combiner will know that I was modified.
replaceInstUsesWith(Instruction & I,Value * V)426*9880d681SAndroid Build Coastguard Worker   Instruction *replaceInstUsesWith(Instruction &I, Value *V) {
427*9880d681SAndroid Build Coastguard Worker     // If there are no uses to replace, then we return nullptr to indicate that
428*9880d681SAndroid Build Coastguard Worker     // no changes were made to the program.
429*9880d681SAndroid Build Coastguard Worker     if (I.use_empty()) return nullptr;
430*9880d681SAndroid Build Coastguard Worker 
431*9880d681SAndroid Build Coastguard Worker     Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
432*9880d681SAndroid Build Coastguard Worker 
433*9880d681SAndroid Build Coastguard Worker     // If we are replacing the instruction with itself, this must be in a
434*9880d681SAndroid Build Coastguard Worker     // segment of unreachable code, so just clobber the instruction.
435*9880d681SAndroid Build Coastguard Worker     if (&I == V)
436*9880d681SAndroid Build Coastguard Worker       V = UndefValue::get(I.getType());
437*9880d681SAndroid Build Coastguard Worker 
438*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "IC: Replacing " << I << "\n"
439*9880d681SAndroid Build Coastguard Worker                  << "    with " << *V << '\n');
440*9880d681SAndroid Build Coastguard Worker 
441*9880d681SAndroid Build Coastguard Worker     I.replaceAllUsesWith(V);
442*9880d681SAndroid Build Coastguard Worker     return &I;
443*9880d681SAndroid Build Coastguard Worker   }
444*9880d681SAndroid Build Coastguard Worker 
445*9880d681SAndroid Build Coastguard Worker   /// Creates a result tuple for an overflow intrinsic \p II with a given
446*9880d681SAndroid Build Coastguard Worker   /// \p Result and a constant \p Overflow value.
CreateOverflowTuple(IntrinsicInst * II,Value * Result,Constant * Overflow)447*9880d681SAndroid Build Coastguard Worker   Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result,
448*9880d681SAndroid Build Coastguard Worker                                    Constant *Overflow) {
449*9880d681SAndroid Build Coastguard Worker     Constant *V[] = {UndefValue::get(Result->getType()), Overflow};
450*9880d681SAndroid Build Coastguard Worker     StructType *ST = cast<StructType>(II->getType());
451*9880d681SAndroid Build Coastguard Worker     Constant *Struct = ConstantStruct::get(ST, V);
452*9880d681SAndroid Build Coastguard Worker     return InsertValueInst::Create(Struct, Result, 0);
453*9880d681SAndroid Build Coastguard Worker   }
454*9880d681SAndroid Build Coastguard Worker 
455*9880d681SAndroid Build Coastguard Worker   /// \brief Combiner aware instruction erasure.
456*9880d681SAndroid Build Coastguard Worker   ///
457*9880d681SAndroid Build Coastguard Worker   /// When dealing with an instruction that has side effects or produces a void
458*9880d681SAndroid Build Coastguard Worker   /// value, we can't rely on DCE to delete the instruction. Instead, visit
459*9880d681SAndroid Build Coastguard Worker   /// methods should return the value returned by this function.
eraseInstFromFunction(Instruction & I)460*9880d681SAndroid Build Coastguard Worker   Instruction *eraseInstFromFunction(Instruction &I) {
461*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "IC: ERASE " << I << '\n');
462*9880d681SAndroid Build Coastguard Worker 
463*9880d681SAndroid Build Coastguard Worker     assert(I.use_empty() && "Cannot erase instruction that is used!");
464*9880d681SAndroid Build Coastguard Worker     // Make sure that we reprocess all operands now that we reduced their
465*9880d681SAndroid Build Coastguard Worker     // use counts.
466*9880d681SAndroid Build Coastguard Worker     if (I.getNumOperands() < 8) {
467*9880d681SAndroid Build Coastguard Worker       for (Use &Operand : I.operands())
468*9880d681SAndroid Build Coastguard Worker         if (auto *Inst = dyn_cast<Instruction>(Operand))
469*9880d681SAndroid Build Coastguard Worker           Worklist.Add(Inst);
470*9880d681SAndroid Build Coastguard Worker     }
471*9880d681SAndroid Build Coastguard Worker     Worklist.Remove(&I);
472*9880d681SAndroid Build Coastguard Worker     I.eraseFromParent();
473*9880d681SAndroid Build Coastguard Worker     MadeIRChange = true;
474*9880d681SAndroid Build Coastguard Worker     return nullptr; // Don't do anything with FI
475*9880d681SAndroid Build Coastguard Worker   }
476*9880d681SAndroid Build Coastguard Worker 
computeKnownBits(Value * V,APInt & KnownZero,APInt & KnownOne,unsigned Depth,Instruction * CxtI)477*9880d681SAndroid Build Coastguard Worker   void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
478*9880d681SAndroid Build Coastguard Worker                         unsigned Depth, Instruction *CxtI) const {
479*9880d681SAndroid Build Coastguard Worker     return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
480*9880d681SAndroid Build Coastguard Worker                                   DT);
481*9880d681SAndroid Build Coastguard Worker   }
482*9880d681SAndroid Build Coastguard Worker 
483*9880d681SAndroid Build Coastguard Worker   bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth = 0,
484*9880d681SAndroid Build Coastguard Worker                          Instruction *CxtI = nullptr) const {
485*9880d681SAndroid Build Coastguard Worker     return llvm::MaskedValueIsZero(V, Mask, DL, Depth, AC, CxtI, DT);
486*9880d681SAndroid Build Coastguard Worker   }
487*9880d681SAndroid Build Coastguard Worker   unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0,
488*9880d681SAndroid Build Coastguard Worker                               Instruction *CxtI = nullptr) const {
489*9880d681SAndroid Build Coastguard Worker     return llvm::ComputeNumSignBits(Op, DL, Depth, AC, CxtI, DT);
490*9880d681SAndroid Build Coastguard Worker   }
491*9880d681SAndroid Build Coastguard Worker   void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
492*9880d681SAndroid Build Coastguard Worker                       unsigned Depth = 0, Instruction *CxtI = nullptr) const {
493*9880d681SAndroid Build Coastguard Worker     return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
494*9880d681SAndroid Build Coastguard Worker                                 DT);
495*9880d681SAndroid Build Coastguard Worker   }
computeOverflowForUnsignedMul(Value * LHS,Value * RHS,const Instruction * CxtI)496*9880d681SAndroid Build Coastguard Worker   OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
497*9880d681SAndroid Build Coastguard Worker                                                const Instruction *CxtI) {
498*9880d681SAndroid Build Coastguard Worker     return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, AC, CxtI, DT);
499*9880d681SAndroid Build Coastguard Worker   }
computeOverflowForUnsignedAdd(Value * LHS,Value * RHS,const Instruction * CxtI)500*9880d681SAndroid Build Coastguard Worker   OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
501*9880d681SAndroid Build Coastguard Worker                                                const Instruction *CxtI) {
502*9880d681SAndroid Build Coastguard Worker     return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, AC, CxtI, DT);
503*9880d681SAndroid Build Coastguard Worker   }
504*9880d681SAndroid Build Coastguard Worker 
505*9880d681SAndroid Build Coastguard Worker private:
506*9880d681SAndroid Build Coastguard Worker   /// \brief Performs a few simplifications for operators which are associative
507*9880d681SAndroid Build Coastguard Worker   /// or commutative.
508*9880d681SAndroid Build Coastguard Worker   bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
509*9880d681SAndroid Build Coastguard Worker 
510*9880d681SAndroid Build Coastguard Worker   /// \brief Tries to simplify binary operations which some other binary
511*9880d681SAndroid Build Coastguard Worker   /// operation distributes over.
512*9880d681SAndroid Build Coastguard Worker   ///
513*9880d681SAndroid Build Coastguard Worker   /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
514*9880d681SAndroid Build Coastguard Worker   /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
515*9880d681SAndroid Build Coastguard Worker   /// & (B | C) -> (A&B) | (A&C)" if this is a win).  Returns the simplified
516*9880d681SAndroid Build Coastguard Worker   /// value, or null if it didn't simplify.
517*9880d681SAndroid Build Coastguard Worker   Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
518*9880d681SAndroid Build Coastguard Worker 
519*9880d681SAndroid Build Coastguard Worker   /// \brief Attempts to replace V with a simpler value based on the demanded
520*9880d681SAndroid Build Coastguard Worker   /// bits.
521*9880d681SAndroid Build Coastguard Worker   Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero,
522*9880d681SAndroid Build Coastguard Worker                                  APInt &KnownOne, unsigned Depth,
523*9880d681SAndroid Build Coastguard Worker                                  Instruction *CxtI);
524*9880d681SAndroid Build Coastguard Worker   bool SimplifyDemandedBits(Use &U, const APInt &DemandedMask, APInt &KnownZero,
525*9880d681SAndroid Build Coastguard Worker                             APInt &KnownOne, unsigned Depth = 0);
526*9880d681SAndroid Build Coastguard Worker   /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
527*9880d681SAndroid Build Coastguard Worker   /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
528*9880d681SAndroid Build Coastguard Worker   Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
529*9880d681SAndroid Build Coastguard Worker                                     const APInt &DemandedMask, APInt &KnownZero,
530*9880d681SAndroid Build Coastguard Worker                                     APInt &KnownOne);
531*9880d681SAndroid Build Coastguard Worker 
532*9880d681SAndroid Build Coastguard Worker   /// \brief Tries to simplify operands to an integer instruction based on its
533*9880d681SAndroid Build Coastguard Worker   /// demanded bits.
534*9880d681SAndroid Build Coastguard Worker   bool SimplifyDemandedInstructionBits(Instruction &Inst);
535*9880d681SAndroid Build Coastguard Worker 
536*9880d681SAndroid Build Coastguard Worker   Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
537*9880d681SAndroid Build Coastguard Worker                                     APInt &UndefElts, unsigned Depth = 0);
538*9880d681SAndroid Build Coastguard Worker 
539*9880d681SAndroid Build Coastguard Worker   Value *SimplifyVectorOp(BinaryOperator &Inst);
540*9880d681SAndroid Build Coastguard Worker   Value *SimplifyBSwap(BinaryOperator &Inst);
541*9880d681SAndroid Build Coastguard Worker 
542*9880d681SAndroid Build Coastguard Worker   // FoldOpIntoPhi - Given a binary operator, cast instruction, or select
543*9880d681SAndroid Build Coastguard Worker   // which has a PHI node as operand #0, see if we can fold the instruction
544*9880d681SAndroid Build Coastguard Worker   // into the PHI (which is only possible if all operands to the PHI are
545*9880d681SAndroid Build Coastguard Worker   // constants).
546*9880d681SAndroid Build Coastguard Worker   //
547*9880d681SAndroid Build Coastguard Worker   Instruction *FoldOpIntoPhi(Instruction &I);
548*9880d681SAndroid Build Coastguard Worker 
549*9880d681SAndroid Build Coastguard Worker   /// \brief Try to rotate an operation below a PHI node, using PHI nodes for
550*9880d681SAndroid Build Coastguard Worker   /// its operands.
551*9880d681SAndroid Build Coastguard Worker   Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
552*9880d681SAndroid Build Coastguard Worker   Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
553*9880d681SAndroid Build Coastguard Worker   Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
554*9880d681SAndroid Build Coastguard Worker   Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
555*9880d681SAndroid Build Coastguard Worker   Instruction *FoldPHIArgZextsIntoPHI(PHINode &PN);
556*9880d681SAndroid Build Coastguard Worker 
557*9880d681SAndroid Build Coastguard Worker   Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
558*9880d681SAndroid Build Coastguard Worker                         ConstantInt *AndRHS, BinaryOperator &TheAnd);
559*9880d681SAndroid Build Coastguard Worker 
560*9880d681SAndroid Build Coastguard Worker   Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
561*9880d681SAndroid Build Coastguard Worker                             bool isSub, Instruction &I);
562*9880d681SAndroid Build Coastguard Worker   Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned,
563*9880d681SAndroid Build Coastguard Worker                          bool Inside);
564*9880d681SAndroid Build Coastguard Worker   Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
565*9880d681SAndroid Build Coastguard Worker   Instruction *MatchBSwap(BinaryOperator &I);
566*9880d681SAndroid Build Coastguard Worker   bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
567*9880d681SAndroid Build Coastguard Worker   Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
568*9880d681SAndroid Build Coastguard Worker   Instruction *SimplifyMemSet(MemSetInst *MI);
569*9880d681SAndroid Build Coastguard Worker 
570*9880d681SAndroid Build Coastguard Worker   Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
571*9880d681SAndroid Build Coastguard Worker 
572*9880d681SAndroid Build Coastguard Worker   /// \brief Returns a value X such that Val = X * Scale, or null if none.
573*9880d681SAndroid Build Coastguard Worker   ///
574*9880d681SAndroid Build Coastguard Worker   /// If the multiplication is known not to overflow then NoSignedWrap is set.
575*9880d681SAndroid Build Coastguard Worker   Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
576*9880d681SAndroid Build Coastguard Worker };
577*9880d681SAndroid Build Coastguard Worker 
578*9880d681SAndroid Build Coastguard Worker } // end namespace llvm.
579*9880d681SAndroid Build Coastguard Worker 
580*9880d681SAndroid Build Coastguard Worker #undef DEBUG_TYPE
581*9880d681SAndroid Build Coastguard Worker 
582*9880d681SAndroid Build Coastguard Worker #endif
583